Problem with Data Passed to Call Back Function
- From: Chris <chrishax comcast net>
- To: gtklist <gtk-list gnome org>
- Subject: Problem with Data Passed to Call Back Function
- Date: Wed, 29 Nov 2006 18:30:51 -0700
Hello,
In the program below I am creating 3 buttons in a
horizontal box. The buttons get created and labeled
properly as:
Button 1
Button 2
Button 3
When I click on any of the buttons though,
the data sent to the callback function should
be the label of the button pressed.
Instead it all shows up as:
Button 3
The button labels are stored in an array of character
pointers which appears to work find in the for loop.
Once out, if I print out the contents of the array
of character pointers they are all:
Button 3
I am guessing that I am missing some simple
problem but have been unable to find it on
my own.
Any help would be appreciated.
Thanks
Chris
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
static void button_cb (GtkWidget *widget, gpointer data)
{
g_print ("Hello - %s was pressed\n", (gchar *) data);
}
static void delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer
data)
{
g_print ("A delete event has occurred\n");
gtk_main_quit();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
gchar *newstr[3];
gchar tmpstr[12];
gint i;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Test Program");
gtk_container_set_border_width (GTK_CONTAINER (window), 50);
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK
(delete_event_cb), NULL);
box1 = gtk_hbox_new (TRUE, 20);
gtk_container_add (GTK_CONTAINER (window), box1);
for (i=1; i<=3; i=i+1)
{
sprintf(tmpstr,"%s%d","Button ",i);
printf("The value of tmpstr = %s\n",tmpstr); /* Prints
Button 1, Button 2, Button 3*/
newstr[i-1] = tmpstr;
printf("The current label is %s\n",newstr[i-1]); /* Prints
Button 1, Button 2, Button 3*/
button = gtk_button_new_with_label (newstr[i-1]);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(button_cb), (gpointer) newstr[i-1]);
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
gtk_widget_show (button);
printf("The current label is %s\n",newstr[i-1]); /* Prints
Button 1, Button 2, Button 3*/
}
printf("The value of element 1 is %s\n",newstr[0]); /*
Prints Button 3*/
printf("The value of element 2 is %s\n",newstr[1]); /*
Prints Button 3*/
printf("The value of element 3 is %s\n",newstr[2]); /*
Prints Button 3*/
gtk_widget_show (box1);
gtk_widget_show (window);
gtk_main ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]