Re: Finding a widget in a Glade interface



Nicola Fontana wrote:
On Sun, 03 Aug 2008 19:10:37 +0000
dhk <dhkuhl optonline net> wrote:
Is gtk_container_get_children() suppose to return a list of all children including children of children down to the last leaf or just the immediate children? I only seem to get the immediate children. Will gtk_container_foreach() or gtk_container_forall() go down all levels to the last leaf or just the next level?

Both work on immediate children. You must call them recursively,
something like that:

void
action(GtkWidget *widget)
{
  /* Do something */
}

void
callback(GtkWidget *widget)
{
  action(widget);
  if (GTK_IS_CONTAINER(widget))
      gtk_container_foreach(GTK_CONTAINER(widget),
                            callback, NULL);

}

gtk_container_foreach() is the way: the forall() version
traverses also internal stuff I think you don't care about.

Ciao

Almost there.  I wrote a function, findWidget() below, to find a widget
by name.  This was needed because I'm adding objects to the interface
after Glade has built the interface. It works, but I'm sure it's laeking
memory like a sieve.  Whenever I try to free the memory I have problems.
 How and where should the memory get freed?  Can I get a code review?

/* Recursivly find a widget by name and return it's reference. */
GtkWidget * findWidget(GtkWidget *p, const char *str) {
  GList *children=NULL;
  GList *child=NULL;
  GtkWidget *w=NULL;

  children=gtk_container_get_children(GTK_CONTAINER(p));
  child=children;

  while(child) {

    w=GTK_WIDGET(child->data);
    if(w!=NULL && w->name!=NULL) {
      g_debug("Name is {%s}\n", w->name);
    }

    if(w!=NULL && GTK_IS_CONTAINER(w) && w->name!=NULL && strcmp((char
*)w->name, str)!=0) {
      w=findWidget(w, str);
      if(w!=NULL && w->name!=NULL && strcmp((char *)w->name, str)==0) {
        break;
      } else {
        child=g_list_next(child);
      }
    } else {
      break;
    }

  } /* end of while() */
  child=g_list_next(child);

  // PROBLEMS freeing memory
  //g_list_foreach(children, (GFunc)freeGList, NULL); // ERROR: double
free or corruption
  //g_list_foreach(children, (GFunc)g_free, children->data); // ERROR
  //g_list_free(children); // ERROR

  return w;
}

/* Callback for freeing memory in a GList. */
void freeGList(gpointer *data, gpointer *user_data) {
  GList *gl=(GList *)data;

  g_free(gl->data);
  gl->data=NULL;
}

Thanks,

Dave






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