Re: Finding widget from its name?



Hi Tomas,

Now that is what I call a seriously good explanation! Thanks a lot.

/jan

tomas tuxteam de wrote:
On Wed, Aug 16, 2006 at 06:02:27AM +0100, jan wrote:
Progss wrote:
jan talentex demon co uk napisa?(a):
Is there a function that allows one to search for a widget using its
name or label? I'm using Glade 2 to design a window (just a basic
input form), and each widget has a unique name. Unfortunately Glade
doean't save any global pointers to the widgets; I can only assume
that there must be another way to find the objects from within the
callback functions. 
Absolutely, there is - glade_xml_get_widget

e.g.
http://lists.ximian.com/pipermail/glade-users/2004-February/001790.html

Regards,
Waldek

Thanks!

Now on to the next question: What if I wasn't using Glade? The gtk+
documentation isn't exactly intuitive.

Then the widgets don't have names ;-)

Remember: you put names on the widgets with glade?

But of course, there is nothing magical about it. If you peek into any
glade project source, there is this pesky "support.c", and there you'll
see:

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

So what it's doing? First it climbs up the widget hierarchy along the
parent lines (with some special cases for menus and some extra
relationships set by glade with the help of a special "GladeParentKey"
widget prop. When this grand-grand-grandpa widget is found, it is
expected to contain a "database of widgets keyed by name" in its
properties.

Glade generates code to set up all this in interface.c, which looks
more-or-less like

|   /* Store pointers to all widgets, for use by lookup_widget(). */
|   GLADE_HOOKUP_OBJECT_NO_REF (itlist_window, itlist_window, "itlist_window");
|   GLADE_HOOKUP_OBJECT (itlist_window, vbox, "vbox");
|   GLADE_HOOKUP_OBJECT (itlist_window, menu, "menu");
|   GLADE_HOOKUP_OBJECT (itlist_window, menuitem1, "menuitem1");
|   GLADE_HOOKUP_OBJECT (itlist_window, menu1, "menu1");

(and on for many lines).

It's an exercise for the reader to find out what the macro
GLADE_HOOKUP_OBJECT does...

HTH
-- tomÃÂs



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