[Glade-users] How to get to the label field of a button
Ed Winchester
edward.e.winchester@lmco.com
Wed, 16 May 2001 10:01:51 -0400
--Boundary_(ID_oAqjk8SjIlGAtVPuRxOeQw)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT
Hey, talk about an answer. Ask what time it is, and get a thirty-minute
discourse on Egyptian water clocks. No, really. Andrae, you are a
prince! You didn't say, "You dumb s--t, don't you know anything?"
Instead, you helped me. I really appreciate the tutorial nature of
your reply. It solved my immediate problem, but even more importantly,
gave me a brief course on how to use the publicly available data to
solve my own similar problems in the future. This is what I love about
this list. I heartily agree with James' post that this should be in the
FAQ.
Thanks again,
Ed
Andrae Muys wrote:
>
>
> Jered Bolton wrote:
> >
> > Ed Winchester wrote:
> >
> > > Hi again, all,
> > >
> > > I've been trying to get my glade-generated app to compile. I
> know, from
> > > something I read, but can't remember where, that Glade itself will
> not
> > > generate code to change the label for a button. I remember seeing
>
> > > somewhere an example, but can't for the life of me remember where
> I read
> > > that.
> > >
> > > What I tried last was:
> > > gtk_label_set_text (GTK_LABEL(button->label),"New button
> label");
> > >
> > > I got the error:
> > > structure has no member named `label'
> > >
> > > Please help.
> >
> > No idea how this works (well I kind of have but....), but try the
> > following:
> >
> > GtkWidget *label;
> > GList *glist;
> >
> > widget = lookup_widget(window_widget_is_on, "slider_button"); /*
> may not
> > need this as long as you have a pointer to the widget you'll be
> okay*/
> >
> > glist = gtk_container_children( GTK_CONTAINER(widget) );
> > label = GTK_WIDGET( glist->data );
> > gtk_label_set( GTK_LABEL(label), "FRED");
> >
> > I found this after trawling through stacks of stuff on the web, all
> seemed
> > a bit mysterious to me, as I too had similar problems in
> setting/changing
> > the label for the button.
> > I found the answer in a posting from 1998 from someone complaining
> about
> > the lack of useful documentation....
> > yet I still find myself wandering how the hell did they know to A)
> use a
> > GList, and B) that is has a member called data. Half the time the
> > documentation states that the structure should only be accessed
> using the
> > functions given... which in turn can only be used with this "extra"
>
> > knowledge.
> > I realise this is more of a GTK problem than glade specific, but I'm
> sure
> > there are many other relative "newbies" like myself who feel
> slightly lost
> > at times.
>
> Actually Gtk+ is reasonably well documented (GNOME is sometimes a
> different matter). In this specific case the documentation is clear.
> (All quotes are direct cut/paste from
> http://developer.gnome.org/doc/API/ )
>
> Reading from the Gtk+ reference for GtkButton :
>
> Object Hierarchy
> GtkObject
> +----GtkWidget
> +----GtkContainer
> +----GtkBin
> +----GtkButton
>
> Args
> "label" gchar* : Read / Write
> "relief" GtkReliefStyle : Read / Write
>
> Note that a GtkButton is a GtkBin, so it is a GtkContainer that
> supports
> a single child widget (in this specific/normal case a GtkLabel).
>
> Also note that the "label" Arg is available, which provides an
> immediate
> answer to your question, specifically :
>
> gtk_object_set(GTK_OBJECT(button), "label", "goodbye",
> NULL);
>
> You can know this is a gtk_object_* function because you can look in
> GGAD, written by Havoc Pennington, and published under an Open licence
>
> (ie, you can dl/read it off the web). Dead tree version available
> from
> New Riders IIRC.
>
> If, like me, you have merely forgotten the precise syntax, you just
> need
> to look at the reference docs for GtkObject, specifically:
>
> ++++++++
> gtk_object_set ()
>
> void gtk_object_set (GtkObject *object,
> const gchar
> *first_arg_name,
> ...);
>
> This function sets multiple arguments of an object.
>
> It takes an object, then a list of name/value pairs in a list,
> followed
> by NULL.
>
> void set_box_properties(GtkBox* box)
> {
> gtk_object_set(GTK_OBJECT(box), "homogeneous", TRUE,
> "spacing", 8,
> NULL);
> }
> object :
> the object whose arguments should be set.
> first_arg_name :
> the name of the first argument to set.
> ... :
> the value of the first argument, followed
> optionally by more name/value pairs, followed by
> NULL.
> ++++++++
>
> However there are at least two other ways of achiving the same end.
> As
> the GtkButton appears to it's child as a GtkBin (see inheritance
> list),
> it would make sense that facilities for manipulating it's child would
> be
> associated with GtkBin, not GtkButton. So if we take a look at GtkBin
>
> we see:
>
> GtkBin -- a container with just one child.
> ....
> Details
>
> struct GtkBin
>
> struct GtkBin;
>
> The GtkBin struct contains the following fields. (These fields should
> be
> considered read-only. They should
> never be set by an application.)
>
> GtkWidget *child; the child widget.
>
> Which immediately suggests the following code :
>
> gtk_label_set_text(GTK_LABEL(GTK_BIN(button)->child),
> "hello");
>
> (In actual fact this was my immediate response to the question, it's
> just the GtkArg approach is most immediately obvious from the
> GtkButton
> docs)
>
> Of course there is also the child manipulation functions provided by
> GtkBin's parent class GtkContainer:
>
> GList* gtk_container_children (GtkContainer *container);
>
> I am a little surprised to find this function undocumented, although
> it
> is listed with it's full signature in the GtkContainer reference, and
> is
> sufficiently obvious to hardly require documenting. I guessed
> (correctly) from the signature that it returns a linked list of
> children
> widgets given a container widget.
>
> Taking a look at the GList reference (which is complete except for
> complexity info, that is rare in any documentation, but would be
> useful
> in the glib docs sometimes) :
>
> +++++
> Description
>
> The GList structure and its associated functions provide a standard
> doubly-linked list data structure.
>
> Each element in the list contains a piece of data, together with
> pointers which link to the previous and next
> elements in the list. <SNIP: lots of useful info on GList's>
> Details
>
> struct GList
>
> struct GList
> {
> gpointer data;
> GList *next;
> GList *prev;
> };
>
> The GList struct is used for each element in a doubly-linked list. The
>
> data field holds the element's data, which
> can be a pointer to any kind of data, or any integer value using the
> Type Conversion Macros. The next and prev
> pointers are the links to the next and previous elements in the list.
>
> +++++
>
> Which makes the following code snippet easy enough to generate:
>
> GList *child = gtk_container_children(GTK_CONTAINER(button));
> gtk_label_set_text(GTK_LABEL(child->data), "clicked");
>
> I've attached a small libglade based demo that shows the above in
> action. (Total project 115 lines in 3 files in 2.6k)
>
> Andrae Muys
>
>
--Boundary_(ID_oAqjk8SjIlGAtVPuRxOeQw)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hey, talk about an answer. Ask what time it is, and get a thirty-minute
discourse on Egyptian water clocks. No, really. Andrae, you
are a prince! You didn't say, "You dumb s--t, don't you know anything?"
Instead, you helped me. I really appreciate the tutorial nature
of your reply. It solved my immediate problem, but even more importantly,
gave me a brief course on how to use the publicly available data to solve
my own similar problems in the future. This is what I love about
this list. I heartily agree with James' post that this should be
in the FAQ.
<p>Thanks again,
<br>Ed
<p>Andrae Muys wrote:
<blockquote TYPE=CITE>
<p><font size=-1>Jered Bolton wrote:</font>
<br><font size=-1>></font>
<br><font size=-1>> Ed Winchester wrote:</font>
<br><font size=-1>></font>
<br><font size=-1>> > Hi again, all,</font>
<br><font size=-1>> ></font>
<br><font size=-1>> > I've been trying to get my glade-generated app to
compile. I know, from</font>
<br><font size=-1>> > something I read, but can't remember where, that
Glade itself will not</font>
<br><font size=-1>> > generate code to change the label for a button.
I remember seeing</font>
<br><font size=-1>> > somewhere an example, but can't for the life of me
remember where I read</font>
<br><font size=-1>> > that.</font>
<br><font size=-1>> ></font>
<br><font size=-1>> > What I tried last was:</font>
<br><font size=-1>> > gtk_label_set_text (GTK_LABEL(button->label),"New
button label");</font>
<br><font size=-1>> ></font>
<br><font size=-1>> > I got the error:</font>
<br><font size=-1>> > structure has no member named
`label'</font>
<br><font size=-1>> ></font>
<br><font size=-1>> > Please help.</font>
<br><font size=-1>></font>
<br><font size=-1>> No idea how this works (well I kind of have but....),
but try the</font>
<br><font size=-1>> following:</font>
<br><font size=-1>></font>
<br><font size=-1>> GtkWidget
*label;</font>
<br><font size=-1>> GList
*glist;</font>
<br><font size=-1>></font>
<br><font size=-1>> widget = lookup_widget(window_widget_is_on,
"slider_button"); /* may not</font>
<br><font size=-1>> need this as long as you have a pointer to the widget
you'll be okay*/</font>
<br><font size=-1>></font>
<br><font size=-1>> glist = gtk_container_children(
GTK_CONTAINER(widget) );</font>
<br><font size=-1>> label = GTK_WIDGET( glist->data );</font>
<br><font size=-1>> gtk_label_set( GTK_LABEL(label),
"FRED");</font>
<br><font size=-1>></font>
<br><font size=-1>> I found this after trawling through stacks of stuff
on the web, all seemed</font>
<br><font size=-1>> a bit mysterious to me, as I too had similar problems
in setting/changing</font>
<br><font size=-1>> the label for the button.</font>
<br><font size=-1>> I found the answer in a posting from 1998 from someone
complaining about</font>
<br><font size=-1>> the lack of useful documentation....</font>
<br><font size=-1>> yet I still find myself wandering how the hell did
they know to A) use a</font>
<br><font size=-1>> GList, and B) that is has a member called data.
Half the time the</font>
<br><font size=-1>> documentation states that the structure should only
be accessed using the</font>
<br><font size=-1>> functions given... which in turn can only be
used with this "extra"</font>
<br><font size=-1>> knowledge.</font>
<br><font size=-1>> I realise this is more of a GTK problem than glade
specific, but I'm sure</font>
<br><font size=-1>> there are many other relative "newbies" like myself
who feel slightly lost</font>
<br><font size=-1>> at times.</font>
<p><font size=-1>Actually Gtk+ is reasonably well documented (GNOME is
sometimes a</font>
<br><font size=-1>different matter). In this specific case the documentation
is clear.</font>
<br><font size=-1>(All quotes are direct cut/paste from</font>
<br><font size=-1><a href="http://developer.gnome.org/doc/API/" TARGET="_blank">http://developer.gnome.org/doc/API/</a>
)</font>
<p><font size=-1>Reading from the Gtk+ reference for GtkButton :</font>
<p><font size=-1>Object Hierarchy</font>
<br><font size=-1> GtkObject</font>
<br><font size=-1> +----GtkWidget</font>
<br><font size=-1>
+----GtkContainer</font>
<br><font size=-1>
+----GtkBin</font>
<br><font size=-1>
+----GtkButton</font>
<p><font size=-1>Args</font>
<br><font size=-1> "label"
gchar*
: Read / Write</font>
<br><font size=-1> "relief"
GtkReliefStyle : Read / Write</font>
<p><font size=-1>Note that a GtkButton is a GtkBin, so it is a GtkContainer
that supports</font>
<br><font size=-1>a single child widget (in this specific/normal case a
GtkLabel).</font>
<p><font size=-1>Also note that the "label" Arg is available, which provides
an immediate</font>
<br><font size=-1>answer to your question, specifically :</font>
<p>
<font size=-1>gtk_object_set(GTK_OBJECT(button), "label", "goodbye", NULL);</font>
<p><font size=-1>You can know this is a gtk_object_* function because you
can look in</font>
<br><font size=-1>GGAD, written by Havoc Pennington, and published under
an Open licence</font>
<br><font size=-1>(ie, you can dl/read it off the web). Dead tree
version available from</font>
<br><font size=-1>New Riders IIRC.</font>
<p><font size=-1>If, like me, you have merely forgotten the precise syntax,
you just need</font>
<br><font size=-1>to look at the reference docs for GtkObject, specifically:</font>
<p><font size=-1>++++++++</font>
<br><font size=-1>gtk_object_set ()</font>
<p><font size=-1> void gtk_object_set
(GtkObject *object,</font>
<br><font size=-1>
const gchar</font>
<br><font size=-1>*first_arg_name,</font>
<br><font size=-1>
...);</font>
<p><font size=-1>This function sets multiple arguments of an object.</font>
<p><font size=-1>It takes an object, then a list of name/value pairs in
a list, followed</font>
<br><font size=-1>by NULL.</font>
<p><font size=-1> void set_box_properties(GtkBox* box)</font>
<br><font size=-1> {</font>
<br><font size=-1> gtk_object_set(GTK_OBJECT(box), "homogeneous",
TRUE,</font>
<br><font size=-1>
"spacing", 8,</font>
<br><font size=-1>
NULL);</font>
<br><font size=-1> }</font>
<br><font size=-1>
object :</font>
<br><font size=-1>
the object whose arguments should be set.</font>
<br><font size=-1> first_arg_name :</font>
<br><font size=-1>
the name of the first argument to set.</font>
<br><font size=-1>
... :</font>
<br><font size=-1>
the value of the first argument, followed</font>
<br><font size=-1>optionally by more name/value pairs, followed by</font>
<br><font size=-1>
NULL.</font>
<br><font size=-1>++++++++</font>
<p><font size=-1>However there are at least two other ways of achiving
the same end. As</font>
<br><font size=-1>the GtkButton appears to it's child as a GtkBin (see
inheritance list),</font>
<br><font size=-1>it would make sense that facilities for manipulating
it's child would be</font>
<br><font size=-1>associated with GtkBin, not GtkButton. So if we
take a look at GtkBin</font>
<br><font size=-1>we see:</font>
<p><font size=-1>GtkBin -- a container with just one child.</font>
<br><font size=-1>....</font>
<br><font size=-1>Details</font>
<p><font size=-1>struct GtkBin</font>
<p><font size=-1> struct GtkBin;</font>
<p><font size=-1>The GtkBin struct contains the following fields. (These
fields should be</font>
<br><font size=-1>considered read-only. They should</font>
<br><font size=-1>never be set by an application.)</font>
<p><font size=-1> GtkWidget *child;
the child widget.</font>
<p><font size=-1>Which immediately suggests the following code :</font>
<p> <font size=-1>gtk_label_set_text(GTK_LABEL(GTK_BIN(button)->child),
"hello");</font>
<p><font size=-1>(In actual fact this was my immediate response to the
question, it's</font>
<br><font size=-1>just the GtkArg approach is most immediately obvious
from the GtkButton</font>
<br><font size=-1>docs)</font>
<p><font size=-1>Of course there is also the child manipulation functions
provided by</font>
<br><font size=-1>GtkBin's parent class GtkContainer:</font>
<p><font size=-1>GList* gtk_container_children
(GtkContainer *container);</font>
<p><font size=-1>I am a little surprised to find this function undocumented,
although it</font>
<br><font size=-1>is listed with it's full signature in the GtkContainer
reference, and is</font>
<br><font size=-1>sufficiently obvious to hardly require documenting.
I guessed</font>
<br><font size=-1>(correctly) from the signature that it returns a linked
list of children</font>
<br><font size=-1>widgets given a container widget.</font>
<p><font size=-1>Taking a look at the GList reference (which is complete
except for</font>
<br><font size=-1>complexity info, that is rare in any documentation, but
would be useful</font>
<br><font size=-1>in the glib docs sometimes) :</font>
<p><font size=-1>+++++</font>
<br><font size=-1>Description</font>
<p><font size=-1>The GList structure and its associated functions provide
a standard</font>
<br><font size=-1>doubly-linked list data structure.</font>
<p><font size=-1>Each element in the list contains a piece of data, together
with</font>
<br><font size=-1>pointers which link to the previous and next</font>
<br><font size=-1>elements in the list. <SNIP: lots of useful info on
GList's></font>
<br><font size=-1>Details</font>
<p><font size=-1>struct GList</font>
<p><font size=-1> struct GList</font>
<br><font size=-1> {</font>
<br><font size=-1> gpointer data;</font>
<br><font size=-1> GList *next;</font>
<br><font size=-1> GList *prev;</font>
<br><font size=-1> };</font>
<p><font size=-1>The GList struct is used for each element in a doubly-linked
list. The</font>
<br><font size=-1>data field holds the element's data, which</font>
<br><font size=-1>can be a pointer to any kind of data, or any integer
value using the</font>
<br><font size=-1>Type Conversion Macros. The next and prev</font>
<br><font size=-1>pointers are the links to the next and previous elements
in the list.</font>
<p><font size=-1>+++++</font>
<p><font size=-1>Which makes the following code snippet easy enough to
generate:</font>
<p><font size=-1> GList *child = gtk_container_children(GTK_CONTAINER(button));</font>
<br><font size=-1> gtk_label_set_text(GTK_LABEL(child->data),
"clicked");</font>
<p><font size=-1>I've attached a small libglade based demo that shows the
above in</font>
<br><font size=-1>action. (Total project 115 lines in 3 files in 2.6k)</font>
<p><font size=-1>Andrae Muys</font>
<br>
<br> </blockquote>
</html>
--Boundary_(ID_oAqjk8SjIlGAtVPuRxOeQw)--