Re: gtk_signal_connect_object () problem



Quoted by a bunch of orange chickens
For `Tzunga Ma <freekathmandu yahoo com>' 
On Thursday, 14 August 2003 (08:43):

but since i'm new to this gtk programming world, can you point me to a sample
code using struct or object_set_data ? i tried both method last night, but
failed.


Using a structure
-----------------

Declare the struct:

        typedef struct _MyStruct MyStruct;
        struct _MyStruct {
                gchar *a;
                gchar *b;
                gint   c;
                guint  d;
        };

Allocate one struct and set the values:

        GtkWidget *button;
        MyStruct *mystruct;

        mystruct = g_new0(MyStruct, 1);

        mystruct->a = g_strdup("w00t");
        mystruct->b = g_strdup("some string");
        mystruct->c = -50;
        mystruct->d = 13933;

        button = gtk_button_new_with_label("Click Me");
        g_signal_connect(G_OBJECT(button), "clicked", (GCallback)my_callback,
mystruct);

Create the callback:

        void my_callback(GtkWidget *button, gpointer data)
        {
                MyStruct *mystruct = (MyStruct *) data;

                g_print("a = %s\n", mystruct->a);
                g_print("b = %s\n", mystruct->b);
                g_print("c = %d\n", c);
                g_print("d = %d\n", d);
        }

Using object_set_data
---------------------

Create the widget and set the data:

        GtkWidget *button;

        button = gtk_button_new_with_label("Click Me");
        g_object_set_data(G_OBJECT(button), "a", "w00t");
        g_object_set_data(G_OBJECT(button), "b", "some string");
        g_object_set_data(G_OBJECT(button), "c", GINT_TO_POINTER(-50));
        g_object_set_data(G_OBJECT(button), "d", GUINT_TO_POINTER(1333));

        g_signal_connect(G_OBJECT(button), "clicked", (GCallback)my_callback, NULL);

And the callback:

        void my_callback(GtkWidget *button, gpointer data)
        {
                gchar *a, *b;
                gint   c;
                guint  d;

                a = (gchar*)g_object_get_data(G_OBJECT(button), "a");
                b = (gchar*)g_object_get_data(G_OBJECT(button), "b");
                c = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "c");
                d = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "d");

                printf("%s %s %d %d\n", a, b, c, d);
        }
        

        Hope this helps :)

-- 

 Leandro Pereira              (oO)           <leandro linuxmag com br>
                              /||\                  www.mindcrisis.tk

        "O medo é uma coisa boa. Se você não tiver medo, pode
           acabar pulando pela janela" --Keith Richards  




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