Skip to content

Instantly share code, notes, and snippets.

@learning
Created October 31, 2016 02:51
Show Gist options
  • Save learning/660a5772eafdb822797b12c621a68b1e to your computer and use it in GitHub Desktop.
Save learning/660a5772eafdb822797b12c621a68b1e to your computer and use it in GitHub Desktop.
Test GtkIMContext in gtk+2.0
#include <stdio.h>
#include <gtk/gtk.h>
static gboolean focus_callback(GtkWidget *gtk_window,
GdkEvent *event,
GtkIMContext *im_context) {
gtk_widget_grab_focus(gtk_window);
gtk_im_context_focus_in(im_context);
gtk_im_context_reset(im_context);
return TRUE;
}
static gboolean blur_callback(GtkWidget *widget,
GdkEvent *event,
GtkIMContext *im_context) {
gtk_im_context_focus_out(im_context);
return TRUE;
}
static void commit_callback(GtkIMContext *context,
const gchar *str,
gpointer data) {
printf("commit_callback: %s\n.", str);
}
static void preedit_changed_callback(GtkIMContext *context,
gpointer data) {
printf("preedit_changed_callback.\n");
}
static gboolean retrieve_surrounding_callback(GtkIMContext *context,
gpointer data) {
printf("retrieve_surrounding_callback.\n");
return TRUE;
}
static gboolean delete_surrounding_callback(GtkIMContext *context,
gint offset,
gint n_chars,
gpointer data) {
printf("delete_surrounding_callback.\n");
return TRUE;
}
int main(int argc, char *argv[]) {
GtkWidget *gtk_window;
GtkIMContext *im_context;
GdkWindow *gdk_window;
gtk_init(&argc, &argv);
gtk_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(gtk_window);
g_signal_connect(GTK_WINDOW(gtk_window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
im_context = gtk_im_multicontext_new();
gdk_window = gtk_widget_get_window(GTK_WIDGET(gtk_window));
gtk_im_context_set_client_window(im_context, gdk_window);
g_signal_connect(gtk_window, "focus-in-event",
G_CALLBACK(focus_callback), im_context);
g_signal_connect(gtk_window, "focus-out-event",
G_CALLBACK(blur_callback), im_context);
g_signal_connect(im_context, "commit",
G_CALLBACK(commit_callback), NULL);
g_signal_connect(im_context, "preedit-changed",
G_CALLBACK(preedit_changed_callback), NULL);
g_signal_connect(im_context, "retrieve-surrounding",
G_CALLBACK(retrieve_surrounding_callback), NULL);
g_signal_connect(im_context, "delete-surrounding",
G_CALLBACK(delete_surrounding_callback), NULL);
gtk_main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment