How to get label for Gtk2 OptionMenu menu items?

Hi everyone,

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.

Thank you for any ideas!


#!/usr/bin/perl -w
#   With thanks to:
use strict;
use warnings;
use sigtrap;
use diagnostics;

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my ( $window,



sub cb_pos_menu_select
        my ($widget, $data) = @_;
    print "\t\$widget = $widget, \$data = $data\n\n";

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";

# Convenience functions
sub make_menu_item
        my ($name, $func, $data) = @_;
        $item = Gtk2::MenuItem->new_with_label($name);
        $item->signal_connect(activate => $func, $data);

        return $item;

sub init
        $window = Gtk2::Window->new('toplevel');
        $window->signal_connect(destroy => sub { Gtk2->main_quit; });
        $window->set_title("optionsmenu test");

        $box1 = Gtk2::VBox->new(FALSE, 0);

        $opt = Gtk2::OptionMenu->new;
        $menu = Gtk2::Menu->new;

        $item = make_menu_item("Item One", \&cb_pos_menu_select, 'item_one');
        $item = make_menu_item("Next item", \&cb_pos_menu_select, 'next_item');

        $item = make_menu_item("Another item", \&cb_pos_menu_select, 'another_item');

        $item = make_menu_item("Last item", \&cb_pos_menu_select, 'last_item');

        $box1->pack_start($opt, TRUE, TRUE, 0);

        $button = Gtk2::Button->new("Get selected menu item!");
        $button->signal_connect(clicked => \&cb_button_clicked );
        $box1->pack_start($button, TRUE, TRUE, 0);

        $button_q = Gtk2::Button->new("Quit");
        $button_q->signal_connect(clicked => sub { Gtk2->main_quit; });
        $box1->pack_start($button_q, TRUE, TRUE, 0);


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