Re: Getting label of a menu item



El jue, 11 de 03 de 2004 a las 15:37, Ori Idan escribiÃ:
I created the menu items using 'gtk_menu_item_new_with_label' so I guess 
there should be a way to read the label.
I don't know why it has to be so complicated, why not add a simple 
function to read the label as I think it is a very
common task.
What I did was to add a string to the menu item using 
'g_object_set_data' but this seems to me as just adding more
complexity for something that should be done otherwise.

Ok, so you used gtk_menu_item_new_with_label(const gchar *label);

Then in this GtkMenuItem there is only one GtkLabel - this is not always
true.

The GtkMenuItem heritage is this one:

[....]
  GtkBin
    GtkItem
      GtkMenuItem

A GtkBin is nothing but a container that can have one child - in this
case your label.

There's a function to retrieve this child widget, gtk_bin_get_child()
but you can also get it as it's public member "child" (GtkBin*)
bin->child

So you can get the child label of a GtkMenuItem with :
gtk_bin_get_child( GTK_BIN(GtkMenuItem*) ).

Check the following test code:

/*
File: label.c
Compile: gcc `pkg-config --cflags --libs gtk+-2.0` label.c -o label
Run with ./label
*/ 

#include <gtk/gtk.h>

int main( int argc, char* argv[])
{
  GtkWidget* item;
  GtkWidget* label;

  gtk_init(&argc, &argv);

  item = gtk_menu_item_new_with_label("Blah");
  label = gtk_bin_get_child (GTK_BIN(item));

  if( GTK_IS_LABEL(label) ){ // just a sanity check
    gtk_label_set_text(GTK_LABEL(label),"Text");
  }else{
    g_print("is not a label");
    return 0;
  }

  g_print( "The text in the label is %s\n",
     gtk_label_get_text(GTK_LABEL(label)) );

  return 0;
}
/* end */



Hope this helps.
-- 
Iago Rubio                http://www.iagorubio.com          
GPGkey pgp.rediris.es id 0x909BD4DD  fingerprint =
D18A B950 5F03 BB9A DD89  AA75 FEDF 1978 909B D4DD
********** iago.rubio(AT)hispalinux.es  **********     
--------------------------------------------------

Attachment: signature.asc
Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada digitalmente



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