[Glade-users] globals vs locals... again

Alexandre BERAUD wrote:
It's the same problem as initializing the lists. I have
a 'void init ()' function that I could launch at start
and if my clist widget was global, I could access it
in the init() function. It's a pain if you have to
sent the clist pointer to the function because some-
times you need to send many different widgets.

The design you suggest does not sound modular, scaleable, or easy to

Here is how I would do that ...

Scenario: widgets are a main window "main_window" and a list "list"
somewhere within the main window.

Plan A: initialise the list after instantiating the main window, as
Damon posted earlier ...

  GtkWidget *main = create_main_window();
  list_initialise(lookup_widget(main, "list"));

Plan B: create a handler for list initialisation and attach it to the
"show" signal of the parent window ...

struct mw {
  GtkWidget *widget;
  GtkCList *list;
  [... other data specific to this window]

on_main_window_show                    (GtkWidget       *widget,
                                        gpointer         user_data)
  struct mw *mw = g_new(struct mw, 1);
  mw->widget = widget;
  mw->list = GTK_CLIST(lookup(mw->widget,"list"));
  gtk_object_set_data (GTK_OBJECT (mw->widget), POINTER, mw);

I use Plan A for main windows and small, unimportant, never to be
maintained programs.  I use Plan B especially for windows other than the
main window.

Advantages of Plan B

- all data is non-global, simplifying the code concepts, removing
dependencies, and allowing multiple instances of windows.

- it is easy to add more data elements specific to the window by
expanding the window structure (struct mw in the example),

- the pointer to the structure containing the data can be found by any
callback signal function called by any widget within the window,

    struct mw *mw = (struct mw *) 

James Cameron                                      (cameron stl dec com)

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