I want to parse some user data as well but cant get that working.
Below code uses the same callback function for the button and the eventbox - the button-callback parses the userdata (act) but the eventbox does not:
Code: Select all
/* example-start eventbox event_test.c */
//compile with
//gcc event_test.c $(/usr/local/bin/gtk-config --cflags) -o eventbox $(/usr/local/bin/gtk-config --libs)
#include <gtk/gtk.h>
void callback ( GtkWidget *widget, gpointer str) {
g_print ("str is: %s\n", (char *)str);
}
int main( int argc, char *argv[] ) {
GtkWidget *window, *event_box, *label, *button, *box1, *box2;
char *act="This is the content of char act";
///the usual window things
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_show (window);
///the button in box1 - sends value of act without problems.
box1 = gtk_hbox_new (FALSE, 0);
gtk_widget_show(box1);
button = gtk_button_new_with_label ("click me");
gtk_box_pack_start (GTK_BOX (box1), button, TRUE, TRUE, 0);
gtk_widget_show (button);
gtk_container_add (GTK_CONTAINER (window), box1);
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (callback), (gpointer) act);
///the event-thing in box2 - this one wont send the value of act..
box2 = gtk_hbox_new (FALSE, 0);
gtk_widget_show(box2);
event_box = gtk_event_box_new ();
gtk_box_pack_start (GTK_BOX (box2), event_box, TRUE, TRUE, 0);
gtk_widget_show (event_box);
label = gtk_label_new ("Click here to view events from the eventbox...");
gtk_container_add (GTK_CONTAINER (event_box), label);
gtk_widget_show (label);
gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
gtk_signal_connect (GTK_OBJECT(event_box), "button_press_event",
GTK_SIGNAL_FUNC (callback), (gpointer) act);
gtk_container_add (GTK_CONTAINER(box1), box2);
gtk_widget_realize (event_box);
gtk_widget_show (window);
gtk_main ();
return(0);
}
UPDATE: If I change the callback function from the eventbox to:
Code: Select all
void callback ( GtkWidget *widget, GdkEvent *event, gpointer str) {
g_print ("str is: %s\n", (char *)str);
}