RE: Problem with Data Passed to Call Back Function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Title: RE: Problem with Data Passed to Call Back Function

Actually the 3 elements in newstr[] are the same, i.e. tmpstr, because tmpstr is an array, it has fixed address in this routine. Therefore when you pass the address to G_CALLBACK (button_cb) in g_signal_connect, the content is still right, say "1", "2" and "3". But after the "for" loop, the content is "3", and all reference to this address is "3".

Maybe this could be better:

#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];

                gchar *tmpstr;

        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)

          {

                        tmpstr = (gchar *)g_malloc0(12);

            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;

    }

---------------------------------------

Best Wishes!

 

Zhang Hui

Email: Hui003.Zhang@xxxxxxxxxxx

Phone: 86-25-58748787-8215

Samsung Electronics(China) R&D Center

---------------------------------------

-----Original Message-----
From: gtk-list-bounces@xxxxxxxxx [mailto:gtk-list-bounces@xxxxxxxxx] On Behalf Of Chris
Sent: Thursday, November 30, 2006 9:31 AM
To: gtklist
Subject: Problem with Data Passed to Call Back Function

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;

    }

 


_______________________________________________

gtk-list mailing list

gtk-list@xxxxxxxxx

http://mail.gnome.org/mailman/listinfo/gtk-list

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux