Re: easy newbie question (I think)
- From: Iago Rubio <iago rubio hispalinux es>
- To: gtk-app-devel-list gnome org
- Subject: Re: easy newbie question (I think)
- Date: Mon, 13 Sep 2004 10:54:35 +0200
On Mon, 2004-09-13 at 07:50, Brian Clark wrote:
Yep. That seems to be the general consensus, and that's what I'll do.
I'm just surprised there's not a built in way.
There're some code used by Glade, that can achieve the goal you're
looking for.
It deals with the GObject data, storing pointers to all widgets there,
and using a single function to search the Widgets by name.
// glade generated code, paste it in your project
#define GLADE_HOOKUP_OBJECT(component,widget,name) \
g_object_set_data_full (G_OBJECT (component), name, \
gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
g_object_set_data (G_OBJECT (component), name, widget)
GtkWidget*
lookup_widget (GtkWidget *widget,
const gchar *widget_name)
{
GtkWidget *parent, *found_widget;
for (;;)
{
if (GTK_IS_MENU (widget))
parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
else
parent = widget->parent;
if (!parent)
parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
"GladeParentKey");
if (parent == NULL)
break;
widget = parent;
}
found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
widget_name);
if (!found_widget)
g_warning ("Widget not found: %s", widget_name);
return found_widget;
}
// end Glade's code
// EXAMPLE
GtkWidget*
create_dialog (void)
{
GtkWidget *dialog;
GtkWidget *dialog_vbox;
GtkWidget *hbox;
GtkWidget *label_1;
GtkWidget *label_2;
GtkWidget *dialog_action_area;
GtkWidget *okbutton;
dialog = gtk_dialog_new ();
gtk_window_set_title (GTK_WINDOW (dialog), _("Miter limit"));
dialog_vbox = GTK_DIALOG (dialog)->vbox;
gtk_widget_show (dialog_vbox);
hbox = gtk_hbox_new (TRUE, 0);
gtk_widget_show (hbox);
gtk_box_pack_start (GTK_BOX (dialog_vbox), hbox, TRUE, TRUE, 0);
label_1 = gtk_label_new (_("Label1"));
gtk_widget_show (label_1);
gtk_box_pack_start (GTK_BOX (hbox), label_1, TRUE, TRUE, 0);
label_2 = gtk_label_new (_("Label2"));
gtk_widget_show (label_2);
gtk_box_pack_start (GTK_BOX (hbox), label_2, TRUE, TRUE, 0);
dialog_action_area = GTK_DIALOG (dialog)->action_area;
gtk_widget_show (dialog_action_area);
gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog), GTK_BUTTONBOX_END);
okbutton = gtk_button_new_from_stock ("gtk-ok");
gtk_widget_show (okbutton);
gtk_dialog_add_action_widget (GTK_DIALOG (dialog), okbutton,
GTK_RESPONSE_OK);
GTK_WIDGET_SET_FLAGS (okbutton, GTK_CAN_DEFAULT);
g_signal_connect ((gpointer) okbutton, "clicked",
G_CALLBACK (on_okbutton_clicked),
NULL);
// store pointers to use by lookup widget
GLADE_HOOKUP_OBJECT_NO_REF (dialog, dialog, "dialog");
GLADE_HOOKUP_OBJECT (dialog, label_1, "label_1");
GLADE_HOOKUP_OBJECT (dialog, label_2, "label_2");
GLADE_HOOKUP_OBJECT (dialog, okbutton, "okbutton");
return dialog;
}
// Examples catching the widgets
void
do_something()
{
GtkWidget* dialog;
GtkWidget* label_1;
GtkWidget* label_2;
dialog = create_dialog();
label_1 = lookup_widget(dialog, "label_1");
label_2 = lookup_widget(dialog, "label_2");
if( label_1 != NULL && label_2 != NULL ){
// ......
}
}
// callback example
void
on_okbutton_clicked(GtkButton* button, gpointer data)
{
GtkWidget* label_1;
GtkWidget* label_2;
// you can catch the widget from any widget
// in the hierarchy, not just from the top level widget
label_1 = lookup_widget(GTK_WIDGET(button), "label_1");
label_2 = lookup_widget(GTK_WIDGET(button), "label_2");
if( label_1 != NULL && label_2 != NULL ){
// ......
}
}
// EXAMPLE end
This code is generated in each Glade's project and works fine.
More on this:
http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html
http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html#g-object-set-data
http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html#g-object-set-data-full
http://developer.gnome.org/doc/API/2.0/gobject/gobject-The-Base-Object-Type.html#g-object-get-data
--
Iago Rubio
- GPG Keyserv * pgp.rediris.es id=0x909BD4DD
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]