Re: [gtk-list] GtkMenuItem: How can I retrieve the label text?



On Sun, 20 Feb 2000, Todd Shadburn wrote:

> How can I retrieve the text from a GtkMenuItem?
> 
> I used gtk_menu_item_new_with_label() to create the menu items and then
> attached the menu items
> to a GtkOptionMenu. In my callback I get the currently selected menu
> item from the option menu...
> but I haven't been able to find a way to retrieve the label text.
> 
> Any help would be appreciated! :-)

usually, if you have a menu_item given, you can retrive its child with:

if (GTK_BIN (menu_item)->child)
{
  GtkWidget *child = GTK_BIN (menu_item)->child;
  
  /* do stuff with child */
  if (GTK_IS_LABEL (child))
  {
    gchar *text;
    
    gtk_label_get (GTK_LABEL (child), &text);
    g_print ("menu item text: %s\n", text);
  }
}

to get the active menu item from an option menu, you can do:

if (GTK_OPTION_MENU (option_menu)->menu_item)
{
  GtkWidget *menu_item = GTK_OPTION_MENU (option_menu)->menu_item;
}

but the way live goes, for this specific case, you can *not* get the label
widget from menu_item with the above code, because the option menu reparents
the menu_item's child temporarily to display the currently active contents.
so to retrive the child of the currently active menu_item of an option menu,
you'll have to do:

if (GTK_BIN (option_menu)->child)
{
  GtkWidget *child = GTK_BIN (option_menu)->child;

  /* do stuff with child */
}

[FAQ: this should probably go into the FAQ (if not already there)]

> 
> Todd.
> 

---
ciaoTJ




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