Re: [gtk-list] C++ and gtk+




Hi,

Not all calbacks have the same number of arguments, but all have a
"data" argument.

In the GtkDrawinfArea case, for example the "expose_event" has the
following signature:

gboolean  expose_event (GdkWidget *widget, 
                        GdkEventExpose *event, 
                        gpointer data)

So you can always use the data argument as a pointer to your object.

The simplest strategy to use to wrap GTK widgets in C++ classes is to
use static members as callback functions, each having the "data"
member a pointer to the C++ object. The callback function will usually
invoke a method in the object.

The problem is, however, that you have no other data argument left to
pass aditional information to the callback (and believe me, you will
need it :)

Another strategy is to use "gtk_object_set_user_data" on the widget,
that is:

class A {
        A()

        // other membetrs
        static void a_callback(GtkWidget* w, [other args], gpointer data);
private:
        GtkWidget *w;
};

in the constructor:

A::A()
{

        [...] // construct the widget

        // set the user data in the widget to point to the
        // coresponding object
        gtk_object_set_user_data(w, this);
}

in the callback;

void A::a_callback(GtkWidget *w, [other args], gpointer data)
{
        A * the_object = reinterpret_cast<A*>(gtk_object_get_user_data(w));

        // now you have the object that is connected to the widget and
        // data can be used to pass other "data" to the callback

        [...]
}


This approach is better, IMHO, since even is the user data is already
used for something else, you can use a "named user data" (or whatever
the terminology is) via the gtk_object_set/get_data:

in the constuctor:
gtk_object_set_data(w, "the_corresponding_object", this);

in the callback:
A *a = reinterpret_cast<A*>(gtk_object_get_data(w,
                                              "the_corresponding_object));

Have a nice day,
alex.

>>>>> "David" == David Pettersson <dapet@mai.liu.se> writes:

    David> Hi!

    David> I tried to do a little program in c++ with gtk+. I have a
    David> 'class A' which contains a 'widget *W'. If I have more then
    David> one instance of 'A' I can for some signals (e.g. "clicked")
    David> send 'this' last data argument to 'gtk_signal_connect()',
    David> and thus know what object the signal refer to.  With other
    David> signals (e.g. "configure_event") the last argument in the
    David> callback function is used for other data, so my question
    David> is, how do I know which of my objects the signal refers to?


    David> David.

    David> -- To unsubscribe: mail -s unsubscribe
    David> gtk-list-request@redhat.com < /dev/null


-- 



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