Re: GnomeDialog




On Fri, 4 Dec 1998, Rodrigo Moya wrote:
> >
> >You have to add the children, so keep a pointer to them.
> >
> What do you mean by 'add the children', to the callback gpointer parameter
> maybe (in a structure or g_list or something like that)?
>

You have to create them and pack them into the dialog's vbox. e.g., say
you want a dialog with a label in it:

GtkWidget* label = gtk_label_new("Hello");
GtkWidget* dialog = gnome_dialog_new("Hi", GNOME_STOCK_BUTTON_OK, NULL);

gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox),
                   label,
                   TRUE, TRUE, 0);
 
> >The exception is the vbox, which you should pack your children into.
> >To access that just use GNOME_DIALOG(dialog)->vbox.
> >
> All widgets are packed into GNOME_DIALOG(dialog)->vbox, so there is not a
> way to get all children that have been packed into it, is it?
> 

You could call:

GList* children =
gtk_container_children(GTK_CONTAINER(GNOME_DIALOG(dialog)->vbox));

but IMHO this is almost always a terrible way to write the code. The
problem is that the code breaks gruesomely and without warning if you or
the Gtk people change anything; you are making assumptions about the order
of children in the box, the type of children in the box, etc. with no real
check that those assumptions are correct. Say you want to add a second
label or a text widget or a pixmap; you have to worry about that now when
you extract children. 

Instead, I would recommend keeping pointers to the children you care about
in your own data structures - create a nice struct for the purpose, use a
global variable, whatever seems nice. Often it's convenient to do this:

gtk_object_set_data(GTK_OBJECT(dialog), "message_label", label);

....

(in the callback)

GtkWidget* message_label = gtk_object_get_data(GTK_OBJECT(dialog),"message_label");

Notice that you can now add a second label, or a button, without changing
the callback code.

> So what is the best way to do it?
> 

It depends on what exactly you're doing. It is just bad form in general to
let the GUI implicitly store data for you, since it's very fragile and
becomes a maintenance headache.

Havoc




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