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

Re: Labels In Radio Buttons



On Fri, Oct 19, 2001 at 12:27:44PM +0100, darren adams wrote:
> Hello,
> 
> I've been inspired by the GTK+ tutorial (specifically,
> http://www.gtk.org/tutorial/ch-widgetoverview.html#SEC-CASTING), and I think
> I have an answer.
> 
> I think that because a GtkRadioButton is a descendant of GtkButton, you can
> use casting to retrieve the label. This is better served in an example, so
> have a look at the last line of this code example
> 
> Rather than:
> 
> GtkWidget *radio_button;
> gchar *radio_button_label_text;
> ...
> radio_button = gtk_radio_button_new_with_label(NULL, "This is my text");
> 
> /* This doesn't work: no label member in GtkRadioButton */
> radio_button_label_text = GTK_TOGGLE_BUTTON(radio_button)->label;
> 
> /* This might work: there's a label member in GtkButton */
> radio_button_label_text = GTK_BUTTON(radio_button)->label;
> 
  Please do not answer false statements on the list. It is really
misleading for people who need help.

  GtkButton doesn't have a 'label' field. (Well, there's a label_text
field for the GtkButton of the upcomming GTK+-2.0, but I guess you're
using GTK+-1.2, and anyway, that's another story).

  The fact is a GtkRadioButton, as all the descendant of GtkButton can
contain nearly any type of widget, and not only a label, so it would be
pointless for the GtkButton type to have a 'label' field.

  Instead, GtkButton (and such, GtkRadioButton for our problem here) is
a descendant of a GtkBin container. The 'child' field of the GtkBin is a
pointer to the widget it contains.

  When you use this code:
    radio_button = gtk_radio_button_new_with_label(NULL, "text");

  GTK really creates *two* widgets: a radio button (GtkRadioButton) and
a GtkLabel. Then the GtkLabel is put inside the radio button.

  From here, you can access the GtkLabel of the GtkRadioButton with
this:
  GTK_BIN(radio_button)->child
  
  Note that, in this particular case, child is a GtkLabel, and then you
can retrieve the text of this label by using gtk_label_get().

  To sum up,

  given you have created the radio button like this:
  radio_button = gtk_radio_button_new_with_label(NULL, "text");

  you can retrieve the text of the button with this kind of code:
  GtkWidget *label;
  gchar     *text;

  label = GTK_BIN(radio_button)->child;
  gtk_label_get(GTK_LABEL(label), &text);


      IHTH.

                DindinX

-- 
 David.Odin@bigfoot.com



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