2009-12-18 12 views
6

Tengo una ventana emergente (creada usando el tipo WINDOW_POPUP) que contiene algunos widgets, incluida una entrada de texto. El problema es que la entrada no se enfoca cuando hago clic en ella, así que no puedo escribir nada. ¿Hay alguna bandera que deba establecer para permitir que la ventana enfoque el teclado?Cómo dar el foco del teclado a una ventana emergente Gtk.Window

Respuesta

-1
#include <gtk/gtk.h> 

static gboolean delete_event(GtkWidget *widget, 
           GdkEvent *event, 
           gpointer data) 
{ 
    g_print ("delete event occurred\n"); 
    gtk_main_quit(); 
    return TRUE; 
} 

static void destroy(GtkWidget *widget, 
        gpointer data) 
{ 
    gtk_main_quit(); 
} 

int main(int argc, 
      char *argv[]) 
{ 
    GtkWidget *window; 
    GtkWidget *windowpopup; 
    GtkWidget *button; 

    gtk_init (&argc, &argv); 

    /* create a new window */ 
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    windowpopup = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_transient_for(GTK_WINDOW(windowpopup),GTK_WINDOW(window)); 
    gtk_window_set_destroy_with_parent(GTK_WINDOW(windowpopup),TRUE); 
    gtk_widget_show (windowpopup); 

    g_signal_connect (G_OBJECT (window), "delete_event", 
       G_CALLBACK (delete_event), NULL); 
    g_signal_connect (G_OBJECT (window), "destroy", 
       G_CALLBACK (destroy), NULL); 

    /* Creates a new button with the label "Hello World". */ 
    button = gtk_button_new_with_label ("Hello World"); 

    g_signal_connect_swapped (G_OBJECT (button), "clicked", 
        G_CALLBACK (gtk_widget_destroy), 
           G_OBJECT (window)); 

    gtk_container_add (GTK_CONTAINER (window), button); 

    gtk_widget_show (button); 
    gtk_widget_show (window); 

    gtk_main(); 

    return 0; 
} 
4

No puede usar WINDOW_POPUP para gtk-windows que requieren el enfoque. En su lugar debe utilizar un GtkWindow con el tipo GTK_WINDOW_TOPLEVEL y llamar a las siguientes funciones (o métodos)

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); 

Esto funcionó para mí ... por desgracia el icono en la lista de la ventana corta parpadea cuando este 'emergente' se destruye

1

A pesar de las respuestas anteriores y la referencia GTK, es posible tomar el foco del teclado cuando se utiliza un GTK_WINDOW_POPUP. Es necesario conectar al evento "espectáculo" ...

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

... con una devolución de llamada que trata de agarrar el teclado:

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); 
    } 
} 

que me funciona bastante bien.

+0

Esto agarrará el teclado exclusivamente. Al hacer clic en las ventanas de otros programas con el mouse, no se transferirá el foco del teclado hacia ellos. Puede haber trabajo alrededor, pero no estoy al tanto de ninguno. –

Cuestiones relacionadas