| 1 | package Gapp::TextView; | ||
| 2 | { | ||
| 3 | $Gapp::TextView::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | |||
| 9 | |||
| 10 | extends 'Gapp::Widget'; | ||
| 11 | with 'Gapp::Meta::Widget::Native::Role::FormField'; | ||
| 12 | |||
| 13 | use Gapp::TextBuffer; | ||
| 14 | |||
| 15 | has '+gclass' => ( | ||
| 16 | default => 'Gtk2::TextView', | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'buffer' => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Maybe[Gapp::TextBuffer]', | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'get_hidden_chars' => ( | ||
| 25 | is => 'rw', | ||
| 26 | isa => 'Bool', | ||
| 27 | default => 0, | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub BUILDARGS { | ||
| 31 | my $class = shift; | ||
| 32 | my %args = @_ == 1 && is_HashRef( $_[0] ) ? %{$_[0]} : @_; | ||
| 33 | |||
| 34 | for my $att ( qw( accepts_tabs editable cursor_visible indent justification left_margin overwrite tabs wrap-mode ) ) { | ||
| 35 | $args{properties}{$att} = delete $args{$att} if exists $args{$att}; | ||
| 36 | } | ||
| 37 | |||
| 38 | __PACKAGE__->SUPER::BUILDARGS( %args ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # returns the value of the widget | ||
| 42 | sub get_field_value { | ||
| 43 | my ( $self ) = @_; | ||
| 44 | my $buffer = $self->gobject->get_buffer; | ||
| 45 | $buffer->get_text( $buffer->get_start_iter, $buffer->get_end_iter, $self->get_hidden_chars ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub set_field_value { | ||
| 49 | my ( $self, $value ) = @_; | ||
| 50 | my $buffer = $self->gobject->get_buffer; | ||
| 51 | $buffer->set_text( defined $value ? $value : '' ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub widget_to_stash { | ||
| 55 | my ( $self, $stash ) = @_; | ||
| 56 | $stash->store( $self->field, $self->get_field_value ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub stash_to_widget { | ||
| 60 | my ( $self, $stash ) = @_; | ||
| 61 | $self->set_field_value( $stash->fetch( $self->field ) ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _connect_changed_handler { | ||
| 65 | my ( $self ) = @_; | ||
| 66 | |||
| 67 | $self->gobject->get_buffer->signal_connect ( | ||
| 68 | changed => sub { $self->_widget_value_changed }, | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | |||
| 75 | __END__ |