Re: GTK PROGRAM STRUCTURE



Dave Reed wrote:

My standard method (not necessarily the best, but it works nicely for
me) is to create a class (call it AppGTK) that has all the widgets as
private data members.  The callbacks are static member functions of
the class (solves your problem below).  I then have one global
variable app_gtk.  In each callback, I can then access any of the
widgets using that global variable and since the callbacks are members
of the class, they can legally access all the private data.

HTH,
Dave



Date: Fri, 09 Mar 2001 10:10:21 -0500
From: CyborgHead netscape net

Hi.
    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 );

and this did not work.
Since most of my functions need access to alot of the GtkWidgets I have made nearly all my variables global so they can be accessed by all the functions, but this is crap, and need to improve it cause it is an important project for college. How do you think I should structure the program to have good programming practive and have the variables available to numerous functions.

Ciaran


As Dave mentioned, you can connect callbacks to static member functions. As a further refinement, you can pass the "this" pointer to your class as the (void*) userdata member in gtk_signal_connect(), then cast it back to a class instance pointer in the static method, and use that to call the "real", non-static callback method. Of course, this makes for a lot of typing, since each callback has to be written "twice".

Alternatively, you could use a C++ library such as SDPGTK (www.k-3d.com), which manages all of this for you by hiding the GTK+ callback mechanism behind named "events" - you derive your classes from sdpGtkObjectContainer, override the "OnEvent()" method, and dispatch events however you like, based on name. Makes for a very clean, OO way of doing things, and sdpGtkObjectContainers can be combined at runtime allowing you to encapsulate portions of your UI as "components" at any desired level of granularity. SDPGTK also relies on an XML markup, "GTKML" as a very compact, flexible, and end-user-modifiable way to specify the layout of your widgets.

Regards,
Timothy M. Shead





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