Re: radioitem




On Jul 20, 2005, at 4:14 AM, Beast wrote:

muppet wrote:

probably belongs in the FAQ:
http://mail.gnome.org/archives/gtk-perl-list/2003-September/ msg00081.html
http://mail.gnome.org/archives/gtk-perl-list/2004-April/msg00115.html
http://mail.gnome.org/archives/gtk-perl-list/2005-March/msg00039.html


Thanks, but I still have a problem. It trigger action twice on every event, what could be the reason?

It fires once per radio button or radio item that is toggled. When you select a new toggle item, one is made inactive and the other is made active; the toggled event fires immediately after each item changes state.


sub radio_callback {
  my $self = shift;
  if ($self->{menu}->get_widget('/Radio/Radio One')->get_active) {
      print "Radio One active\n";
   }
}

You're ignoring the widget that is passed in, that is, the widget which actually had the event, and unconditionally fetching the one in which you're interested. Therefore it looks like the same event fires twice, but really it's two different events that make up the same action; the difference is that different items are giving their change notifications.

You must do this in your code because you're ignoring all the arguments given to the real menu callback:

    callback => sub { $self->radio_callback }.

That is, you're ignoring all arguments and proxying the event to a method on your object, which is trapped in the closure (without seeing the rest of the code, i don't know whether that's a global or lexical or whatever).

That closure is actually passed your user data (likely undef), the action id (0, because you didn't specify one), and the reference to the menuitem widget on which the event fired. If you change your code to something more like this:

   callback => \&radio_callback,
   ...
   sub radio_callback {
     my ($user_data, $action_id, $menu_item) = @_;
# this is fired for both the radio item that is being deselected and the # one that has just been selected. the one that has been selected is
     # now "active".
     if ($menu_item->get_active) {
        print $menu_item->get_child->get_text." activated\n";
# if you specified a callback_action id, you could also use that
        # to decide which item has become active.
     }
  }

You can get $self in radio_callback by using $self as the user data in your SimpleMenu object,

    $simplemenu = Gtk2::SimpleMenu->new (
                                  menu_tree => $menu_tree,
user_data => $self, # so all callbacks get $self
    );




--
"There's a documentary that i wanted to watch on PBS. I heard about it in NPR. ... Oh my god, did i just say that?"
  -- elysse




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