gtk_text_view_im_context_filter_keypress - howto replace Tab with 4-spaces?



All,

I am having trouble using gtk_text_view_im_context_filter_keypress to catch the 'Tab' keypress and replace the tab with 4-spaces instead. I can catch the keypress just fine, but I cannot replace the tab with 4-spaces in my textview using gtk_text_view_im_context_filter_keypress. I have the following simple setup:

  window
    scrolled window
      textview


Attempting to replace the tab with spaces I connected the "key_press_event" to my 'on_keypress' callback:

    g_signal_connect (G_OBJECT (app->view), "key_press_event",
                      G_CALLBACK (on_keypress), app);


  The on_keypress callback (with a few printf debug statements included) is:

gboolean on_keypress (GtkWidget *widget, GdkEventKey *event, context *app)
{
    switch (event->keyval)
    {
        case GDK_KEY_Tab:
            printf ("key pressed: Tab, inserting '    '\n");
            GtkTextBuffer *buffer;
if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (app->view),
                                                          event)) {
                printf ("  Tab key handled by im_context\n");
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view));
                gtk_text_buffer_insert_at_cursor (buffer, "    ", -1);
                return TRUE;
            }
            else printf ("  Tab key handled by default handler\n");
        default:
            return FALSE;
    }
}

The GDK_KEY_Tab is caught as expected. "key pressed: Tab, inserting ' '\n" outputs as expected, but then gtk_text_view_im_context_filter_keypress always returns 'FALSE'.

Why is the gtk_text_view_im_context_filter_keypress returning FALSE? The parameters are correct, I can catch the 'Tab' and write 4-spaces to the buffer simply by removing the if() from before the gtk_text_buffer_insert_at_cursor call as follows:

gboolean on_keypress (GtkWidget *widget, GdkEventKey *event, context *app)
{
    switch (event->keyval)
    {
        case GDK_KEY_Tab:
            GtkTextBuffer *buffer;
            buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view));
            gtk_text_buffer_insert_at_cursor (buffer, "    ", -1);
            return TRUE;
        default:
            return FALSE;
    }
}

This works absolutely fine for what I want to do, but it seems to completely defeat the purpose of gtk_text_view_im_context_filter_keypress. Is this a proper way to handle the tab/4-space replacement, or is there some problem that gtk_text_view_im_context_filter_keypress was intended to fix here? The documentation says:

"Note that you are expected to call this function from your handler when overriding key event handling."

That's what I'm trying to do, but when I call the function, it always returns FALSE -- so what's the point?

What say the experts? Is gtk_text_view_im_context_filter_keypress needed?, or is doing the tab/4-space replacement without it OK? Thanks.


--
David C. Rankin, J.D.,P.E.


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