The “button-press-event” signal: button-press-event signal will be emitted when a button(typically f

时间:2022-11-06 15:23:23




https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-button-press-event

The “button-press-event” signal

gboolean
user_function (GtkWidget *widget,
               GdkEvent  *event,
               gpointer   user_data)

The ::button-press-event signal will be emitted when a button(typically from a mouse) is pressed.

To receive this signal, the GdkWindow associated to thewidget needs to enable the GDK_BUTTON_PRESS_MASK mask.

This signal will be sent to the grab widget if there is one.




How to Implement a button-press-event on GtkTable

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; }
share | improve this question
 
 
What exactly are you trying to achieve? GtkTable is a container to hold other widgets, so they would most likely get the signal. If there are no widgets attached, the table size might be (0,0). –  Ancurio Mar 14 '13 at 17:53
up vote 1 down vote accepted

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);
share | improve this answer