[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
change.

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 ...

main()
{
  GtkWidget *main = create_main_window();
  list_initialise(lookup_widget(main, "list"));
  gtk_widget_show(main);
  [...]
}


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]
}

void
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"));
  list_initialise(mw->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 *) 
                    gtk_object_get_data(             
                      GTK_OBJECT(gtk_widget_get_toplevel(widget)),
                      POINTER
                    );

-- 
James Cameron                                      (cameron stl dec com)





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