Re: How to add callback to tell owner something



rion10 (sent by Nabble.com) wrote:
could you give me a simple example, I'm a gtk+ beginner. Thanks.

Not really, like I said; GSignal is complex;

so heres a complex example:

First of all; you need to have a function to be called
by default on your class:

-----------------------------------
struct _MyObjectClass {
    GObjectClass parent_class;
    /* ... */

    void  (* event_happened)  (MyObject *, gint);
}
-----------------------------------

Now, you must keep track of all the signals you declare
in your object, in C; usually you do this globally:
-----------------------------------
enum {
    EVENT_HAPPENED,
    /* ... other signals ... */
    N_SIGNALS
}

guint my_object_signals[N_SIGNALS];
-----------------------------------

Now you need to actually declare them in your
class initializer:
-----------------------------------
void
my_object_class_init (MyObjectClass *class)
{
    /* ... */

    /* Assign a base class handler for the "event_happened"
     * signal
     */
    class->event_happened = my_object_event_happened;

    /* ... */

    /* Declare the signal that returns VOID and
     * takes one INT after its implicit OBJECT parameter.
     */
    my_object_signals[EVENT_HAPPENED] =
        g_signal_new ("event-happened",
                      G_TYPE_FROM_CLASS (object_class),
                      G_SIGNAL_RUN_LAST,
                      G_STRUCT_OFFSET (MyObjectClass,
                                       event_happened),
                      NULL, NULL,
                      g_cclosure_marshal_VOID__INT,
                      G_TYPE_NONE, 1, G_TYPE_INT);
}
-----------------------------------

Now you can fire the signal with:
-----------------------------------
    g_signal_emit (G_OBJECT (my_object),
                   my_object_signals[EVENT_HAPPENED],
                   0, 5);
-----------------------------------

And clients of your object can connect to the
signal using g_signal_connect.

Now that I've written a short book this morning,
you'll have to delve into the API docs to figure
out more precicely all the meanings of all these
parameters to all these functions, some pointers
for you are "marshalers"
(g_cclosure_marshal_VOID__INT is a marshaler,
generated by glib-genmarshal)

and accumulators.. which arent used in this example
but are used to handle signal return values; usually
these are written by hand.

You shouldn't need to delve into GClosure to use the API.

Ummm and also... read this:
http://developer.gnome.org/doc/API/2.0/gobject/index.html

Cheers,
                            -Tristan




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