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



Hi everyone,

Well, I found a workaround I'm happy with, seems to work well.  I can
simply save the state of the label in the callback, then call $menu_item-
activate
which will force the callback to be called (and so set the global state
holder variable).

(If this bothers anyone please let me know; I'd rather do it the proper way
than use the first hack that seems to work.)

Thank you

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,
     $saved_menu_state
            );

init();

Gtk2->main;

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

sub cb_button_clicked
{
    
    my $selected_item           = $menu->get_active();
    $selected_item->activate;
    
    print   "\tcb_button_clicked, selected: $saved_menu_state\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]