Re: Finding a child widget



On Thu, 9 Aug 2001, rhfreeman wrote:
>
> I know how to find a top level widget, given a widget child widget somewhere
> below. But is it possible to reverse that?

Using the ability to attach a name to a widget, here's how I
do it.  This way, I don't need global pointers for every widget
I'll ever need again, but only for all the toplevel windows.

Debby

/*--------------------------------------------------------------------*/
/*--- Find a widget by name, starting from an enclosing container ----*/
/*--------------------------------------------------------------------*/

static gboolean 
widget_name_p (GtkWidget *w, gchar *name)
{
  if (strcmp (gtk_widget_get_name (w), name) == 0) {
    return true;
  }
  else return false;
}

/*
 * parent must be a container: loop through its children, finding 
 * a widget with the name 'name'
*/
GtkWidget *
widget_find_by_name (GtkWidget *parent, gchar *name)
{
  GtkWidget *w, *namedw = NULL;
  GList *children, *l;

  if (widget_name_p (parent, name))
    namedw = parent;

  else {
    if (GTK_CONTAINER(parent)) {
      children = gtk_container_children (GTK_CONTAINER(parent));
      for (l=children; l; l=l->next) {
        if (GTK_IS_WIDGET(l->data)) {
          w = GTK_WIDGET (l->data);
          if (widget_name_p (w, name)) {
            namedw = w;
            break;
          }
          if (GTK_IS_CONTAINER (w)) {
            namedw = widget_find_by_name (w, name);
            if (namedw != NULL)
              break;
          }
        }
      }
    }
  }

  return namedw;
}





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