How can I retrieve the text from a GtkMenuItem?

GTK+ FAQ
<<< Previous | Development with GTK+: widget specific questions | Next >>>

# How can I retrieve the text from a GtkMenuItem?

You can usually retrieve the label of a specific GtkMenuItem 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 a GtkOptionMenu you can do:

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

But, there's a catch. 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 */
        }

<<< Previous | Home | Next >>>
How do I add some underlined accelerators to menu items? | Up | How do I right (or otherwise) justify a GtkLabel?