Finding a widget in a Glade interface

2008-07-30 Thread dhk
How do you look up a widget when the interface is created with 
glade_xml_new()?


There use to be a lookup_widget() function that worked if you called one 
of the two glade macros:  GLADE_HOOKUP_OBJECT() or 
GLADE_HOOKUP_OBJECT_NO_REF().  Now unless you build the interface 
yourself and hook up each object it doesn't work.


Is there another function embedded somewhere that will find an object 
after calling glade_xml_new()?


Thanks,

dave
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-30 Thread Tristan Van Berkom
for future reference, this could easily have been pulled out
of the docs/header files: glade_xml_get_widget()

type libglade and "feel lucky" its right there.

-Tristan

note, remember to unref your GladeXML object early (unlike
shown in said example).

On Wed, Jul 30, 2008 at 7:10 AM, dhk <[EMAIL PROTECTED]> wrote:
> How do you look up a widget when the interface is created with
> glade_xml_new()?
>
> There use to be a lookup_widget() function that worked if you called one of
> the two glade macros:  GLADE_HOOKUP_OBJECT() or
> GLADE_HOOKUP_OBJECT_NO_REF().  Now unless you build the interface yourself
> and hook up each object it doesn't work.
>
> Is there another function embedded somewhere that will find an object after
> calling glade_xml_new()?
>
> Thanks,
>
> dave
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-30 Thread Tristan Van Berkom
On Wed, Jul 30, 2008 at 3:40 PM, Tristan Van Berkom <[EMAIL PROTECTED]> wrote:
> for future reference, this could easily have been pulled out
> of the docs/header files: glade_xml_get_widget()
>
> type libglade and "feel lucky" its right there.

oops, I obviously meant in google here...
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-30 Thread dhk

Tristan Van Berkom wrote:

On Wed, Jul 30, 2008 at 3:40 PM, Tristan Van Berkom <[EMAIL PROTECTED]> wrote:

for future reference, this could easily have been pulled out
of the docs/header files: glade_xml_get_widget()

type libglade and "feel lucky" its right there.


oops, I obviously meant in google here...


I should have mentioned that by time I want to call
glade_xml_get_widget() the GladeXML *xml created by xml =
glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
 open the file again to get the widget it has no affect on the running
window.  I need to get the widget from the running window from some
callback.  Any ideas?

Thanks,

Dave




___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-30 Thread Tomas Carnecky

dhk wrote:

Tristan Van Berkom wrote:
On Wed, Jul 30, 2008 at 3:40 PM, Tristan Van Berkom <[EMAIL PROTECTED]> 
wrote:

for future reference, this could easily have been pulled out
of the docs/header files: glade_xml_get_widget()

type libglade and "feel lucky" its right there.


oops, I obviously meant in google here...


I should have mentioned that by time I want to call
glade_xml_get_widget() the GladeXML *xml created by xml =
glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
 open the file again to get the widget it has no affect on the running
window.  I need to get the widget from the running window from some
callback.  Any ideas?


Make 'GladeXML *xml' static or otherwise accessible from all parts of 
the sourcecode.


tom

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-30 Thread Jim George
> I should have mentioned that by time I want to call
>> glade_xml_get_widget() the GladeXML *xml created by xml =
>> glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
>>  open the file again to get the widget it has no affect on the running
>> window.  I need to get the widget from the running window from some
>> callback.  Any ideas?
>
> Make 'GladeXML *xml' static or otherwise accessible from all parts of the
> sourcecode.
>
> tom
>

Not a good idea. I had a similar question some time back, the xml
structure returned by glade_xml_new is too "heavyweight" to be
retained for a long time by your program, and it contains no useful
information aside from being able to pull out pointers to widgets. A
more apt way to do this would be to store pointers to all the widgets
you need soon after glade_xml_new (perhaps as members of a struct),
then dereference the GladeXML object. When you set up your callbacks,
have this structure passed as the "user" pointer. Now all callbacks
have access to the widgets in your window.

In addition to the examples posted earlier, you can also download the
source to any standard gtk program for additional examples. I used the
devhelp source for this.
-Jim
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-31 Thread dhk

Jim George wrote:

I should have mentioned that by time I want to call

glade_xml_get_widget() the GladeXML *xml created by xml =
glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
 open the file again to get the widget it has no affect on the running
window.  I need to get the widget from the running window from some
callback.  Any ideas?

Make 'GladeXML *xml' static or otherwise accessible from all parts of the
sourcecode.

tom



Not a good idea. I had a similar question some time back, the xml
structure returned by glade_xml_new is too "heavyweight" to be
retained for a long time by your program, and it contains no useful
information aside from being able to pull out pointers to widgets. A
more apt way to do this would be to store pointers to all the widgets
you need soon after glade_xml_new (perhaps as members of a struct),
then dereference the GladeXML object. When you set up your callbacks,
have this structure passed as the "user" pointer. Now all callbacks
have access to the widgets in your window.

In addition to the examples posted earlier, you can also download the
source to any standard gtk program for additional examples. I used the
devhelp source for this.
-Jim

I wasn't a fan of keeping the GladeXml object around due to the 
resources it used.  Even though it's not as efficient as storing 
pointer, I wish I could just traverse up and down the the object tree 
for my pointer.  I guess I'll be storing the pointers that I need.  That 
would mean that after calling glade_xml_new() I need to call 
glade_xml_get_widget() on each object I will need and then save that 
pointer . . . right?


Thanks,

dave
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-31 Thread Eduardo M KALINOWSKI
dhk wrote:
> I should have mentioned that by time I want to call
> glade_xml_get_widget() the GladeXML *xml created by xml =
> glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
>  open the file again to get the widget it has no affect on the running
> window.  I need to get the widget from the running window from some
> callback.  Any ideas?

If you have a widget produced by that tree, you can retrieve the tree
again with glade_get_widget_tree() [0] and then use
glade_xml_get_widget() on the return value.

[0]http://library.gnome.org/devel/libglade/stable/GladeXML.html#glade-get-widget-tree


-- 
No one can have a higher opinion of him than I have, and I think he's a
dirty little beast.
-- W. S. Gilbert

Eduardo M KALINOWSKI
[EMAIL PROTECTED]
http://move.to/hpkb

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-31 Thread Nicola Fontana
On Thu, 31 Jul 2008 11:07:34 +
dhk <[EMAIL PROTECTED]> wrote:

> Jim George wrote:
> I wasn't a fan of keeping the GladeXml object around due to the 
> resources it used.  Even though it's not as efficient as storing 
> pointer, I wish I could just traverse up and down the the object tree 
> for my pointer.  I guess I'll be storing the pointers that I need.  That 
> would mean that after calling glade_xml_new() I need to call 
> glade_xml_get_widget() on each object I will need and then save that 
> pointer . . . right?

You don't need special API to traverse Gtk widgets, and GladeXml
is less than needed: just use GTK_IS_CONTAINER() and
gtk_container_get_children() or gtk_container_forall().

If you want to go from the leafs to the root, use
gtk_widget_get_parent().

-- 
Nicola
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-07-31 Thread Michael Torrie
dhk wrote:
> I should have mentioned that by time I want to call
> glade_xml_get_widget() the GladeXML *xml created by xml =
> glade_xml_new("filename.glade", NULL, NULL); is out of scope.  If I call
>   open the file again to get the widget it has no affect on the running
> window.  I need to get the widget from the running window from some
> callback.  Any ideas?

In the old days glade would assign data points to each widget that
worked out to essentially astatic string.  You could then take the
widget that was passed to the callback, look for the parent until you
got to the head of the widget tree, then start searching for the data
point you were looking for.  If I recall, glade used to provide a
function to make this searching easy.  I am pretty sure libglade still
allows for something similar.  Actually, looking at the docs, I'm pretty
sure that if you traced your way up to the root node in the widget tree,
and used the glade_get_widget_name() on all the children nodes, you
should be able to find the widget by name (string).
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-03 Thread dhk

Nicola Fontana wrote:

On Thu, 31 Jul 2008 11:07:34 +
dhk <[EMAIL PROTECTED]> wrote:


Jim George wrote:
I wasn't a fan of keeping the GladeXml object around due to the 
resources it used.  Even though it's not as efficient as storing 
pointer, I wish I could just traverse up and down the the object tree 
for my pointer.  I guess I'll be storing the pointers that I need.  That 
would mean that after calling glade_xml_new() I need to call 
glade_xml_get_widget() on each object I will need and then save that 
pointer . . . right?


You don't need special API to traverse Gtk widgets, and GladeXml
is less than needed: just use GTK_IS_CONTAINER() and
gtk_container_get_children() or gtk_container_forall().

If you want to go from the leafs to the root, use
gtk_widget_get_parent().

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?


Thanks,
Dave
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-03 Thread Nicola Fontana
On Sun, 03 Aug 2008 19:10:37 +
dhk <[EMAIL PROTECTED]> 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
-- 
Nicola
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-07 Thread dhk

Nicola Fontana wrote:

On Sun, 03 Aug 2008 19:10:37 +
dhk <[EMAIL PROTECTED]> 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



___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-09 Thread dhk

dhk wrote:

Nicola Fontana wrote:

On Sun, 03 Aug 2008 19:10:37 +
dhk <[EMAIL PROTECTED]> 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



___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



Does anyone have any ideas on how to free the memory correctly?

Thanks,
dave

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-09 Thread Chris Vine
On Sat, 09 Aug 2008 10:06:57 +
dhk <[EMAIL PROTECTED]> wrote:
[snip]
> Does anyone have any ideas on how to free the memory correctly?

g_list_foreach() operates on the data held by the list element, not on
the element itself, so your freeGList() function is wrong, even if
freeing the data is correct.

However, if the list elements reference widgets, as I assume, then you
shouldn't be freeing them anyway.  I strongly suspect you should not
even be decrementing the reference count, as I doubt that
gtk_container_get_children() passes over ownership, but inspecting the
source code will reveal that.

Chris
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-09 Thread dhk

Chris Vine wrote:

On Sat, 09 Aug 2008 10:06:57 +
dhk <[EMAIL PROTECTED]> wrote:
[snip]

Does anyone have any ideas on how to free the memory correctly?


g_list_foreach() operates on the data held by the list element, not on
the element itself, so your freeGList() function is wrong, even if
freeing the data is correct.

However, if the list elements reference widgets, as I assume, then you
shouldn't be freeing them anyway.  I strongly suspect you should not
even be decrementing the reference count, as I doubt that
gtk_container_get_children() passes over ownership, but inspecting the
source code will reveal that.

Chris

Referencing the widgets is what I'm trying to do.  So, are you saying, 
the function is ok without any freeing?  Also, am I decrementing the 
reference count? I don't see it.


thanks,
dave
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-10 Thread Chris Vine
On Sat, 09 Aug 2008 18:18:40 +
dhk <[EMAIL PROTECTED]> wrote:
> Referencing the widgets is what I'm trying to do.  So, are you
> saying, the function is ok without any freeing?  Also, am I
> decrementing the reference count? I don't see it.

You will need to free the list: your mistake was in trying to free its
contents. As I said, I do not think that you need to decrement any
reference counts on the widgets, but if you are in doubt you should
look at the source code (by which I meant, look at the GTK+ source
code).

To understand GTK+ memory management more generally, try this:
http://library.gnome.org/devel/gtk/stable/GtkObject.html
and as further background, this:
http://library.gnome.org/devel/gobject/stable/gobject-The-Base-Object-Type.html

You should generally never call g_free() on any object derived from
GObject/GInitiallyUnowned/GtkObject (which includes anything derived
from GtkWidget), as memory and lifetime management is done by reference
counting.

Chris

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Finding a widget in a Glade interface

2008-08-10 Thread dhk

Chris Vine wrote:

On Sat, 09 Aug 2008 18:18:40 +
dhk <[EMAIL PROTECTED]> wrote:

Referencing the widgets is what I'm trying to do.  So, are you
saying, the function is ok without any freeing?  Also, am I
decrementing the reference count? I don't see it.


You will need to free the list: your mistake was in trying to free its
contents. As I said, I do not think that you need to decrement any
reference counts on the widgets, but if you are in doubt you should
look at the source code (by which I meant, look at the GTK+ source
code).

To understand GTK+ memory management more generally, try this:
http://library.gnome.org/devel/gtk/stable/GtkObject.html
and as further background, this:
http://library.gnome.org/devel/gobject/stable/gobject-The-Base-Object-Type.html

You should generally never call g_free() on any object derived from
GObject/GInitiallyUnowned/GtkObject (which includes anything derived
from GtkWidget), as memory and lifetime management is done by reference
counting.

Chris



Great, that should do it.

Thanks,
dave
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list