Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement a button-press-event on GtkTable

Tags:

c

gtk

Searching the web for answers dosen't get me through my problem:

I wan't my GtkTable to throw an event, if i click one cell.

Since there is no click event, accept for GtkButton's, i wanted to implement a GDK_BUTTON_PRESS_MASK and GDK_BUTTON_RELEASE_MASK to catch the position of the mouse on the Table during click. Works great with GtkDrawingArea!

Tryed the snipet bellow, but nothing happend, maybe someone can give me a clue :)

little sample:

static void table_press(GtkWidget *widget, GdkEventButton *event)
{
    printf("table pressed");
} 

int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget* table;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW (window), "table click");

    table = gtk_table_new(2, 5, TRUE);

    gtk_container_add(GTK_CONTAINER (window), table);

    gtk_widget_add_events(table, GDK_BUTTON_PRESS_MASK);

    g_signal_connect(GTK_OBJECT (table), "button-press-event",
        G_CALLBACK (table_press), NULL);
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));

    gtk_widget_show_all(window);

    gtk_main();
    main_exit();
    return 0;
}
like image 332
Justus Schmidt Avatar asked Oct 24 '25 02:10

Justus Schmidt


1 Answers

You don't receive events because GtkTable does not have a GdkWindow associated with it. You can use GtkEventBox which lets you accept events on widgets that would not normally accept events. This is derived from GtkBin so the interesting code would look like this.

table = gtk_table_new(2, 5, TRUE);
event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER (window), event_box);
gtk_container_add(GTK_CONTAINER (event_box), table);
g_signal_connect(GTK_OBJECT (event_box), "button-press-event",
    G_CALLBACK (table_press), NULL);
like image 133
Rob Bradford Avatar answered Oct 26 '25 18:10

Rob Bradford