| 1 | # You may distribute under the terms of the GNU General Public License | ||
| 2 | # | ||
| 3 | # (C) Paul Evans, 2008-2010 -- leonerd@leonerd.org.uk | ||
| 4 | |||
| 5 | package Circle::FE::Gtk::Widget::Entry; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Variable::Disposition qw( retain_future ); | ||
| 11 | use constant type => "Entry"; | ||
| 12 | |||
| 13 | use Gtk2::Gdk::Keysyms; | ||
| 14 | |||
| 15 | sub build | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my ( $obj, $tab ) = @_; | ||
| 19 | |||
| 20 | my $tabobj = $tab->{object}; | ||
| 21 | |||
| 22 | my $widget = Gtk2::Entry->new(); | ||
| 23 | retain_future $obj->watch_property_with_initial( | ||
| 24 | "text", | ||
| 25 | on_set => sub { | ||
| 26 | my ( $text ) = @_; | ||
| 27 | $text = "" unless defined $text; | ||
| 28 | $widget->set_text( $text ); | ||
| 29 | }, | ||
| 30 | ); | ||
| 31 | |||
| 32 | retain_future $obj->watch_property_with_initial( | ||
| 33 | "history", | ||
| 34 | on_updated => sub {}, # We ignore this, we just want a local cache | ||
| 35 | ); | ||
| 36 | |||
| 37 | my $autoclear = $obj->prop("autoclear"); | ||
| 38 | |||
| 39 | my $history_index; | ||
| 40 | |||
| 41 | $widget->signal_connect( activate => | ||
| 42 | sub { | ||
| 43 | retain_future $obj->call_method( | ||
| 44 | enter => $widget->get_text, | ||
| 45 | ); | ||
| 46 | $widget->set_text( "" ) if $autoclear; | ||
| 47 | undef $history_index; | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | $widget->signal_connect( key_press_event => | ||
| 52 | sub { | ||
| 53 | my ( undef, $event ) = @_; | ||
| 54 | |||
| 55 | if( $event->keyval == $Gtk2::Gdk::Keysyms{Up} ) { | ||
| 56 | my $history = $obj->prop("history"); | ||
| 57 | if( !defined $history_index ) { | ||
| 58 | return 1 unless @$history; | ||
| 59 | $history_index = $#$history; | ||
| 60 | } | ||
| 61 | elsif( $history_index == 0 ) { | ||
| 62 | # Don't move | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | $history_index--; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $line = $history->[$history_index]; | ||
| 70 | $widget->set_text( $line ); | ||
| 71 | $widget->set_position( -1 ); | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | elsif( $event->keyval == $Gtk2::Gdk::Keysyms{Down} ) { | ||
| 76 | my $history = $obj->prop("history"); | ||
| 77 | return 1 unless defined $history_index; | ||
| 78 | if( $history_index < $#$history ) { | ||
| 79 | $history_index++; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | $widget->set_text( "" ); | ||
| 83 | undef $history_index; | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | my $line = $history->[$history_index]; | ||
| 88 | $widget->set_text( $line ); | ||
| 89 | $widget->set_position( -1 ); | ||
| 90 | |||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | $tab->activated; | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | |||
| 99 | $widget->modify_font( $tab->get_font ); | ||
| 100 | $widget->modify_base( normal => $tab->get_theme_colour( "bgcolor" ) ); | ||
| 101 | $widget->modify_text( normal => $tab->get_theme_colour( "textcolor" ) ); | ||
| 102 | |||
| 103 | if( $obj->prop("focussed") ) { | ||
| 104 | $widget->signal_connect( realize => sub { $widget->grab_focus; } ); | ||
| 105 | } | ||
| 106 | |||
| 107 | return $widget; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; |