Re: GTK PROGRAM STRUCTURE



CyborgHead netscape net wrote:
>     A quick question, which is more one of style and programming 
> practice. What is the best way to structure a GTK Program. I have 
> written a program which contains many dialog boxes, all which hold 
> buttons, text boxes etc.. I felt that the best way to program this 
> would be to use classes as I am used to object orientated 
> programming however I found that it could not be effectively done.

> Having created an instance of a class , b I tried to call
> 
> gtk_signal_connect_object( GTK_OBJECT( button_setup ), "clicked",
>         GTK_SIGNAL_FUNC( b.setup ), NULL );

> This did not work, I then tried to pass the class as a parameter ie.

> gtk_signal_connect_object( GTK_OBJECT( button_setup ), "clicked",
>         GTK_SIGNAL_FUNC( global_void_function ), b );

You didn't state what programming language you are using, but I am going
to assume that you mean C++, since you mentioned classes and object
orientation. You were headed in the right direction, but kind of need to
combine the 2 ideas.

The first thing you need to do is make a static member function. This is
called a class method in other OO languages. That means that the
function pertains to the whole class, and not to any particular
instance. So there is no 'this' pointer, so you will have to pass the
object in question.

In your example, you'd have something like:

class MyClass
{
public:
 static void clicked_cb(GtkWidget *w, gpointer p)
 {
   MyClass *m=(MyClass *)p;
   m->clicked();
 }
 void clicked()
 {
   // Whatever.
 }
 GtkWidget * widget()
 {
  return wid;
 }
private:
 GtkWidget * wid;
}

Then you set up the callback like this:

MyClass *mc;
...
gtk_signal_connect( GTK_OBJECT( mc->widget() ), "clicked",
                    GTK_SIGNAL_FUNC( mc->clicked_cb ), mc );

Note that mc->clicked_cb and MyClass.clicked_cb are 2 different ways to
access the same function.

Hope this helps,
Craig M. Buchek




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