| 1 | package Gapp::ComboBox; | ||
| 2 | { | ||
| 3 | $Gapp::ComboBox::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | |||
| 9 | use Gapp::CellRenderer; | ||
| 10 | use Gapp::Types qw( GappCellRenderer ); | ||
| 11 | |||
| 12 | extends 'Gapp::Widget'; | ||
| 13 | with 'Gapp::Meta::Widget::Native::Role::FormField'; | ||
| 14 | |||
| 15 | use Gapp::Model::SimpleList; | ||
| 16 | |||
| 17 | has '+gclass' => ( | ||
| 18 | default => 'Gtk2::ComboBox', | ||
| 19 | ); | ||
| 20 | |||
| 21 | has 'data_column' => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Int|Undef', | ||
| 24 | default => 0, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has 'data_func' => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => 'Str|CodeRef|Undef', | ||
| 30 | ); | ||
| 31 | |||
| 32 | has 'model' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Maybe[Object]', | ||
| 35 | #default => sub { Gapp::Model::SimpleList->new }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'renderer' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => GappCellRenderer, | ||
| 41 | default => sub { Gapp::CellRenderer->new( gclass => 'Gtk2::CellRendererText', property => 'markup' ) }, | ||
| 42 | coerce => 1, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has 'values' => ( | ||
| 46 | is => 'rw', | ||
| 47 | isa => 'ArrayRef|CodeRef|Undef', | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | # returns the value of the widget | ||
| 52 | sub get_field_value { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | my $iter = $self->gobject->get_active_iter; | ||
| 56 | return undef if ! $iter; | ||
| 57 | |||
| 58 | my $value = $self->gobject->get_model->get( $iter, $self->data_column ); | ||
| 59 | return $value; | ||
| 60 | } | ||
| 61 | |||
| 62 | # sets the value of the widget | ||
| 63 | sub set_field_value { | ||
| 64 | my ( $self, $value ) = @_; | ||
| 65 | |||
| 66 | # clear widget if no value | ||
| 67 | if ( ! defined $value ) { | ||
| 68 | $self->gobject->set_active( -1 ); | ||
| 69 | } | ||
| 70 | |||
| 71 | # find value and set appropriately | ||
| 72 | else { | ||
| 73 | $self->gobject->get_model->foreach( sub{ | ||
| 74 | my ( $model, $path, $iter ) = @_; | ||
| 75 | my $check_value = $model->get( $iter, $self->data_column ); | ||
| 76 | |||
| 77 | if ( ! defined $value && defined $check_value || defined $value && ! defined $check_value ) { | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | elsif ( ! defined $value && ! defined $check_value || $value eq $check_value ) { | ||
| 81 | $self->gobject->set_active_iter( $iter ); | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | }); | ||
| 85 | } | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | sub widget_to_stash { | ||
| 90 | my ( $self, $stash ) = @_; | ||
| 91 | $stash->store( $self->field, $self->get_field_value ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub stash_to_widget { | ||
| 95 | my ( $self, $stash ) = @_; | ||
| 96 | $self->set_field_value( $stash->fetch( $self->field ) ); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _connect_changed_handler { | ||
| 100 | my ( $self ) = @_; | ||
| 101 | |||
| 102 | $self->gobject->signal_connect ( | ||
| 103 | changed => sub { $self->_widget_value_changed }, | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | __END__ |