Re: Setting a RadioItem created with ItemFactory




On Jan 21, 2007, at 10:57 AM, Jeffrey Ratcliffe wrote:

On 21/01/07, Jeffrey Ratcliffe <jeffrey ratcliffe gmail com> wrote:
On 04/01/07, Emmanuele Bassi <ebassi gmail com> wrote:
Using Gtk2::UIManager would make this a bit simpler: just set the value of the active RadioAction when adding the actions to the ActionGroup:

OK. Having done this, how do I get the active RadioAction? Or do I
have to keep a variable around with the active action, and update when
the callback is called?

Obviously, I can do:

$uimanager->get_widget('/MenuBar/<path to RadioItem>')->get_active

for each item, but there must be a better way.

It really depends on what you're doing. The synopsis in the manpage for Gtk2::RadioButton shows how to iterate over a radio button group, given one member of that group.

Alternatively, you can use a "toggled" callback to keep a variable synchronized with the active member at all times. The startup sequence is the sticky point, but you can figure something out there. ;-)

Here's an example:

#!/usr/bin/perl -w

use strict;
use Gtk2 -init;

my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub { Gtk2->main_quit });
my $vbox = Gtk2::VBox->new;
$window->add ($vbox);

my $label = Gtk2::Label->new ("<nothing selected>");
$vbox->add ($label);

#
# We'll give this sub to each radio button in the group for the "toggled" # signal. When a radio button is toggled to active, we'll store its handle # in the var. That will give us a variable will always contains the active
# radio button from the group, without having to search the group.  Note
# that the variable is passed in, not referred to as a global.
#
sub radio_toggled {
        my ($radio, $var_ref) = @_;
        print "radio_toggled ".$radio->get_label." ".$radio->get_active."\n";
        $$var_ref = $radio if $radio->get_active;

        # And, so we can see what's going on, update the label.
        $label->set_text ($$var_ref
                          ? $$var_ref->get_label
                          : "<nothing selected>");
}

#
# We took pains to avoid using globals above, but here, we'll just be lazy.
# Doing this without globals is left as an exercise for the reader.  :-)
#

# a reference to the active radio button.
my $active;

# a member of the radio button group.
my $radio = undef;
foreach (qw(One Two Three Four Five)) {
        $radio = Gtk2::RadioButton->new ($radio, $_);
        $radio->signal_connect (toggled => \&radio_toggled, \$active);
        $vbox->add ($radio);
}

# activate the last member of the group, to fire the toggled
# callback the first time.
$radio->set_active (1);

$window->show_all;
Gtk2->main;
__END__


--
If the monkey could type one keystroke every nanosecond, the expected waiting time until the monkey types out Hamlet is so long that the estimated age of the universe is insignificant by comparison ... this is not a practical method for writing plays.
  -- Gian-Carlo Rota





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