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


#!/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";
    $saved_menu_state = $data;

sub cb_button_clicked
    my $selected_item           = $menu->get_active();
    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);

        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]