| 1 | # Copyright 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-ComboBoxBits. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-ComboBoxBits is free software; you can redistribute it and/or | ||
| 6 | # modify it under the terms of the GNU General Public License as published | ||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||
| 8 | # later version. | ||
| 9 | # | ||
| 10 | # Gtk2-Ex-ComboBoxBits is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-ComboBoxBits. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package Gtk2::Ex::Menu::EnumRadio::Item; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Glib::Ex::ObjectBits; | ||
| 23 | use Gtk2; | ||
| 24 | |||
| 25 | # uncomment this to run the ### lines | ||
| 26 | #use Smart::Comments; | ||
| 27 | |||
| 28 | our $VERSION = 32; | ||
| 29 | |||
| 30 | # Gtk2::RadioMenuItem is no good as the base class since it insists on at | ||
| 31 | # least one item active all the time. | ||
| 32 | # | ||
| 33 | # The _do_activate() class handler here allows a parent notify without a | ||
| 34 | # separate activate signal connection on each item. | ||
| 35 | |||
| 36 | use Glib::Object::Subclass | ||
| 37 | 'Gtk2::CheckMenuItem', | ||
| 38 | signals => { activate => \&_do_activate }, | ||
| 39 | properties => [ Glib::ParamSpec->string | ||
| 40 | ('nick', | ||
| 41 | 'Enum nick', | ||
| 42 | 'The enum nick for this item.', | ||
| 43 | (eval {Glib->VERSION(1.240);1} | ||
| 44 | ? undef # default | ||
| 45 | : ''), # no undef/NULL before Perl-Glib 1.240 | ||
| 46 | Glib::G_PARAM_READWRITE) | ||
| 47 | ]; | ||
| 48 | |||
| 49 | use Gtk2::Ex::MenuItem::Subclass; | ||
| 50 | unshift our @ISA, 'Gtk2::Ex::MenuItem::Subclass'; | ||
| 51 | |||
| 52 | sub INIT_INSTANCE { | ||
| 53 | my ($self) = @_; | ||
| 54 | Glib::Ex::ObjectBits::set_property_maybe ($self, draw_as_radio => 1); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _do_activate { | ||
| 58 | my ($self) = @_; | ||
| 59 | ### EnumRadio-Item _do_activate() | ||
| 60 | $self->signal_chain_from_overridden; | ||
| 61 | |||
| 62 | if ($self->get_active) { | ||
| 63 | if (my $menu = $self->get_parent) { # perhaps orphaned during destroy | ||
| 64 | foreach my $menuitem ($menu->get_children) { | ||
| 65 | if ($menuitem != $self && $menuitem->isa(__PACKAGE__)) { | ||
| 66 | $menuitem->set_active(0); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | if (my $menu = $self->parent) { | ||
| 71 | $menu->notify('active-nick'); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | __END__ |