| 1 | package Gapp::ToggleButton; | ||
| 2 | { | ||
| 3 | $Gapp::ToggleButton::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | |||
| 9 | extends 'Gapp::Button'; | ||
| 10 | with 'Gapp::Meta::Widget::Native::Role::FormField'; | ||
| 11 | |||
| 12 | has '+gclass' => ( | ||
| 13 | default => 'Gtk2::ToggleButton', | ||
| 14 | ); | ||
| 15 | |||
| 16 | has 'value' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => 'Str', | ||
| 19 | default => '1', | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'off_value' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Str', | ||
| 25 | default => '0', | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub BUILDARGS { | ||
| 30 | my $class = shift; | ||
| 31 | my %args = @_ == 1 && is_HashRef( $_[0] ) ? %{$_[0]} : @_; | ||
| 32 | |||
| 33 | if ( exists $args{active} ) { | ||
| 34 | $args{active} = [ $args{active} ]; | ||
| 35 | } | ||
| 36 | |||
| 37 | __PACKAGE__->SUPER::BUILDARGS( %args ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub get_field_value { | ||
| 41 | my $self = shift; | ||
| 42 | my $state = $self->gobject->get_active; | ||
| 43 | if ( $state ) { | ||
| 44 | return $self->value; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | return undef; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub set_field_value { | ||
| 53 | my ( $self, $value ) = @_; | ||
| 54 | if ( defined $value && $value eq $self->value ) { | ||
| 55 | $self->gobject->set_active( 1 ) | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | $self->gobject->set_active( 0 ) | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub widget_to_stash { | ||
| 63 | my ( $self, $stash ) = @_; | ||
| 64 | $stash->store( $self->field, $self->get_field_value ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub stash_to_widget { | ||
| 68 | my ( $self, $stash ) = @_; | ||
| 69 | $self->set_field_value( $stash->fetch( $self->field ) ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _connect_changed_handler { | ||
| 73 | my ( $self ) = @_; | ||
| 74 | |||
| 75 | $self->gobject->signal_connect ( | ||
| 76 | toggled => sub { $self->_widget_value_changed }, | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | __END__ |