Re: [gtk-list] Re: which way is better?



Andrae Muys wrote:
> 
> On Thu, 18 Nov 1999, Matthew D Allen wrote:
> 
> >
> > >be better to use global variables for Gtk widgets, and have all
> > >functions in that file use them, or create separate widgets for each
> > >function?
> >
> > Well, if it's a quick program that isn't going to get big, global is ok
> > but it's not really stellar coding style and doesn't scale well at all.
> > In fact, it becomes an ugly mess pretty fast if you're not really careful.
> >
> 
> An alternative that I use regularly that makes a reasonable compromise is
> to use file static globals.

Yet another way is to make a struct containing all the widgets of a
top-level window, so you can pass the group around.  This is very
useful, as you might need to change one widget as a result of
interaction with another widget.  A common example is adjusting the
sensitivity of buttons depending on whtether there is a selection in a
CList.

You simply make a (this in a purely invented example)

struct MyWindow {
	GtkWindow *window;
	GtkButton *button;
	GtkCList *list;

	gpointer user_data;
};
typedef struct MyWindow MyWindow;

The allocate the data with win = g_new(MyWindow, 1) and fill in the
fields as you build you window.  When you define a call-back, use this
struct as the user_data, so your call-back function will have access to
all the other widgets.  Alternatively, attach it to the top-level window
with gtk_object_set_user_data().  The MyWindow user_data field is just
for convenience, you can leave it out if you want to.

Having such a piece of dynamically allocated data for each top level
widget is very handy and it scales well.


-- 
René Seindal (rene@seindal.dk)			http://www.seindal.dk/rene/



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