Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give keyboard focus to a pop-up Gtk.Window

Tags:

gtk

I have a pop-up window (created using the WINDOW_POPUP type) which contains some widgets on it, including a text entry. The problem is that the entry doesn't get the focus when I click on it, so I can't type anything. Is there any flag I have to set to allow the window to get the keyboard focus?

like image 493
Lluis Sanchez Avatar asked Dec 18 '09 00:12

Lluis Sanchez


2 Answers

You can not use WINDOW_POPUP for gtk-windows that require the focus. Instead you should use a GtkWindow with type GTK_WINDOW_TOPLEVEL and call the next functions (or methods)

GtkWindow *result = g_object_new(GTK_TYPE_WINDOW, "type", GTK_WINDOW_TOPLEVEL, NULL);
gtk_widget_set_can_focus(result, TRUE);
gtk_window_set_decorated(GTK_WINDOW(result), FALSE);
gtk_window_set_type_hint(GTK_WINDOW(result), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
gtk_window_set_transient_for(GTK_WINDOW(result), main_top_level_window);

This worked for me ... unfortunately the icon in the window-list blinks short when this 'popup' is destroyed

like image 142
LittleFunnyMan Avatar answered Oct 21 '22 02:10

LittleFunnyMan


Despite the previous answers and the GTK Reference, it is possible to grab the keyboard focus when using a GTK_WINDOW_POPUP. You need to connect to the "show" event...

GtkWindow *w = gtk_window_new(GTK_WINDOW_POPUP);
g_signal_connect(G_OBJECT(w), "show", G_CALLBACK(on_window_show), NULL);

... with a callback that tries to grab the keyboard:

static void on_window_show(GtkWidget *w, gpointer user_data) {
    /* grabbing might not succeed immediately... */
    while (gdk_keyboard_grab(w->window, FALSE, GDK_CURRENT_TIME) != GDK_GRAB_SUCCESS) {
        /* ...wait a while and try again */
        sleep(0.1);
    }
}

That works for me pretty well.

like image 35
fresch Avatar answered Oct 21 '22 02:10

fresch