Problems in Typing Vietnamese in Gtk application


I have a small program (code at the end of this mail), in which I'm trying to achieve the following:
- Type Vietnamese characters, such that the key-presses are processed by a GtkLayout, and the actual text is shown on a GtkLabel.  I cannot directly use a GtkEntry or a GtkTextView, (which both work fine, if I were allowed to use them)

Now whenever a vowel's accent is typed subsequent to the vowel (e.g., if I type 'ee', this should become 'ê'), the key-press-callback returns 'e' both times.  The GtkIMContext's commit-callback returns "e" first time, and "ê" on the second time. The problem is that I'm unable to find out a way to realize that the second "ê" corresponds to the first "e", and I'm supposed to erase the "e" from the GtkLabel, and replace it with a "ê".  There are callbacks called "retrieve_surrounding" and "delete_surrounding" in the GtkIMContext which sound like they may help me out (if I get the opportunity to provide the context to the IME), but they are actually NEVER called! 

Could somebody help me understand if any of the following are possible, and how:
1) Somehow get the IME to invoke the "retrieve_surrounding" and "delete_surrounding" callbacks, so that I can provide the context to the IME, and delete the already entered characters if I'm typing a new accented version of a character (like the "ê").
2) Somehow be able to calculate that the character 'ê' is an accented version of the character 'e' (in general for any accented character in vietnamese).

If any of the above is possible, I would be able to resolve my problem.  Maybe there's a third way to do it too.  I'm using Red Hat EL Workstation 4.0, and the IME is called X-Unikey (

Sorry about the long mail, but since the problem is a bit deep, I needed to give some explanation.  Any help in this regard would be highly appreciated!!!

Thanks in advance,

----- code follows -----
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

GtkWidget *layout;
GtkIMContext *imContext = NULL;
GtkWidget *label;
gboolean keypresscallback(GtkWidget *widget, GdkEvent *event, gpointer data)
        //printf("keypresscallback, event->key = %d\n", event->key);
        //const char *full_text = gtk_label_get_label(GTK_LABEL(label));
        //int length = g_utf8_strlen(full_text, -1);
        //gtk_im_context_set_surrounding(imContext, full_text, length, length);
        if (gtk_im_context_filter_keypress(imContext, &(event->key)))
                return TRUE;

        return FALSE;

void commit_cb(GtkIMContext *context, const gchar *str, gpointer data)
        GtkWidget *entry = (GtkWidget *)data;
        printf("commit_cb [%s], %d\n", str, str[0]);
        //gtk_im_context_set_surrounding(context, full_text, length, length);
        const char *old_text = gtk_label_get_label(GTK_LABEL(label));
        int length = g_utf8_strlen(old_text, -1);
        char *full_text = (char *)malloc(length + 1 + g_utf8_strlen(str, -1));
        strcpy(full_text, old_text);
        strcat(full_text, str);
        gtk_label_set_text(GTK_LABEL(label), full_text);

void preedit_start_cb(GtkIMContext *imContext, gpointer clientData)

void preedit_changed_cb(GtkIMContext *imContext, gpointer clientData)

void preedit_end_cb(GtkIMContext *imContext, gpointer clientData)

gboolean retrieve_surrounding_cb(GtkIMContext *context, gpointer data)
        return FALSE;

gboolean delete_surrounding_cb(GtkIMContext *context, gint offset, gint n_chars, gpointer data)
        return FALSE;

int main( int   argc, char *argv[] )
        GtkWidget *window;
        GtkWidget *vbox, *hbox;

        gtk_init (&argc, &argv);

        /* create a new window */
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_size_request (GTK_WIDGET (window), 200, 100);
        gtk_window_set_title (GTK_WINDOW (window), "Typing Vietnamese");
        g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect_swapped (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_widget_destroy), G_OBJECT (window));

        vbox = gtk_vbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (window), vbox);
        gtk_widget_show (vbox);

        /* add layout */
        GdkColor layout_color = {0, 0xFFFF, 0x0000, 0x0000};
        layout = gtk_layout_new(NULL, NULL);
        gtk_widget_modify_bg(layout, GTK_STATE_NORMAL, &layout_color);
        gtk_box_pack_start (GTK_BOX (vbox), layout, TRUE, TRUE, 0);
        gtk_widget_show (layout);

        /* add label */
        label = gtk_label_new("");
        gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
        gtk_widget_show (label);

        hbox = gtk_hbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (vbox), hbox);
        gtk_widget_show (hbox);

        gtk_widget_show (window);

        /* add events to the layout */
        GdkWindow *gdkWin = GTK_LAYOUT(layout)->bin_window;
        gint eveMask = gdk_window_get_events(gdkWin);
        eveMask |= GDK_ALL_EVENTS_MASK;
        gdk_window_set_events(gdkWin, (GdkEventMask)eveMask);
        gtk_widget_add_events(layout, GDK_ALL_EVENTS_MASK);
        g_signal_connect (G_OBJECT (layout), "key-press-event", G_CALLBACK (keypresscallback), NULL);

        /* create the IM context */
        imContext = gtk_im_multicontext_new();
        g_signal_connect(G_OBJECT(imContext), "commit", G_CALLBACK(commit_cb), (gpointer)label);
        g_signal_connect(G_OBJECT(imContext), "preedit_changed", G_CALLBACK(preedit_changed_cb), (gpointer)label);
        g_signal_connect(G_OBJECT(imContext), "preedit_start", G_CALLBACK(preedit_start_cb), (gpointer)label);
        g_signal_connect(G_OBJECT(imContext), "preedit_end", G_CALLBACK(preedit_end_cb), (gpointer)label);
        g_signal_connect (G_OBJECT (imContext), "retrieve_surrounding", G_CALLBACK (retrieve_surrounding_cb), label);
        g_signal_connect (G_OBJECT (imContext), "delete_surrounding", G_CALLBACK (delete_surrounding_cb), label);
        GdkWindow *im_context_window = (GTK_IS_LAYOUT(layout) ? (GTK_LAYOUT(layout))->bin_window: layout->window);
        gtk_im_context_set_client_window (imContext, im_context_window);


        return 0;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]