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!

Steve


#!/usr/bin/perl -w
#
#   optionsmenu.pl
#
#   With thanks to:
#       http://gtk2-perl.sourceforge.net/doc/gtk2-perl-tut/sec-RangeWidgetsExample.
html
#
use strict;
use warnings;
use sigtrap;
use diagnostics;

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

my ( $window,
     $box1,
     $opt,
     $menu,
     $item,
     $button,
     $button_q,
            );

init();

Gtk2->main;

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);
        $item->show;

        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);
        $window->add($box1);
        $box1->show;

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

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

        $item = make_menu_item("Another item", \&cb_pos_menu_select, 'another_item');
        $menu->append($item);

        $item = make_menu_item("Last item", \&cb_pos_menu_select, 'last_item');
        $menu->append($item);

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

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

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

        $window->show;
}




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