Re: calling non-static function as callback

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

 



What you need to do is use the this pointer when you connect the signal
to the static function. You can use that pointer to call the correct non
static function. In a sense your static function is a wrapper that
chooses which instance of the object you wish to call a function on. 

For example in a program I am writing I have a heap of Tab objects which
are tabs in a window and each has a button on the tab that detatches it
from the notebook. In the tab class I have a static function that is the
callback for this button. When i connect the signal to the button in the
constructor for the tab I use the following.

g_signal_connect( G_OBJECT(button), "clicked", G_CALLBACK(cb_switch),
gpointer(this));

Then in my static function I do the following
gboolean Tab::cb_switch(GtkWidget* widget, gpointer thetab)
{
        if(((Tab*)thetab)->attached)
                ((Tab*)thetab)->detach();
...
...
Hope this helps.

This is from the introduction in the gtk+-2 tutorial on gtk.org:
If you're developing your GTK application in C++, a few extra notes are
in order. There's a C++ binding to GTK called GTK--, which provides a
more C++-like interface to GTK; you should probably look into this
instead. If you don't like that approach for whatever reason, there are
two alternatives for using GTK. First, you can use only the C subset of
C++ when interfacing with GTK and then use the C interface as described
in this tutorial. Second, you can use GTK and C++ together by declaring
all callbacks as static functions in C++ classes, and again calling GTK
using its C interface. If you choose this last approach, you can include
as the callback's data value a pointer to the object to be manipulated
(the so-called "this" value). Selecting between these options is simply
a matter of preference, since in all three approaches you get C++ and
GTK. None of these approaches requires the use of a specialized
preprocessor, so no matter what you choose you can use standard C++ with
GTK.

Shouldn't this be in gtk-app-devel?

On Wed, 2004-09-01 at 13:37, Sisodia Rohit-A5903C wrote:
> hi freinds,
> 
> i m designing a WAP browser using C++.I m facing a problem regarding calling a callback function.
> since it is needed that callback function should be static one.I will not be able to call any non-static
> function or non-static data which i want to call in this callback function.Is there any way out there
> to call a non-static callback function.or some other way round.
> 
> 
> Rohit Sisodia
> Software Trainee
> Motorola India Electronics Limited
> Bangalore
> Ph-26014728
> 
> _______________________________________________
> 
> gtk-list@xxxxxxxxx
> http://mail.gnome.org/mailman/listinfo/gtk-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