Re: How to get label for Gtk2 OptionMenu menu items?

steve madsen virgin net said:
I've searched the archives but I'm still unsure how to get the underlying
state for a menu item?  In the sample program attached below, all I need
to do is find out what method to use to punch down inside the active menu
item and get its label.

In general, the way to get to the label of a menu item is just to grab the
child.  A GtkMenuItem isa GtkBin, and you typically have a GtkAccelLabel
(which isa GtkLabel) inside.  So, this normally works:

  $child = $menuitem->get_child;
  if ($child->isa ('Gtk2::Label')) {
      $text = $child->get_label;

And, in fact, this works in cb_pos_menu_select() and just after creation.

However, there are some interesting complications in your program.  You're
using a GtkOptionMenu (which is deprecated in favor of GtkComboBox, btw), and
trying to fetch the child of the currently-selected menu item in the
optionmenu's menu in a callback that fires while the menu is not visible.

It just so happens that whenever you do this, the active menu item has no
children.  Why?  Because to avoid duplicating the contents of the active menu
item, and to avoid the buggy, broken code involved in having it draw the same
thing in two places, GtkOptionMenu actually reparents the active MenuItem's
child to be the contents of the OptionMenu.  So, while the menu is not up, the
selected menu item doesn't have any children; its child is actually at 

sub cb_button_clicked
    #  Want to retrieve the label for the active menu item here, but
    #  not sure what method(s) or extra setup to use to do this...
    my $selected_item           = $menu->get_active();

    print   "\tcb_button_clicked, selected: $selected_item\n";

     print "\t".($opt->get_child->get_label)."\n";


muppet <scott at asofyet dot org>

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