SOLVED:gtk eventbox - user data parsing

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

SOLVED:gtk eventbox - user data parsing

#1 Post by goingnuts »

I am trying to trap clicks on gtk labels. I can use an eventbox to do that.
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);
}
I really would like to get this working - any suggestions?
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);
}
the user data is passed. This callback does not work for the button so need two different functions.

Post Reply