| 1 | package Gtk2::Ex::FileLocator::PathnameField; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Gtk2; | ||
| 6 | use Gtk2::Gdk::Keysyms; | ||
| 7 | use Glib qw(TRUE FALSE); | ||
| 8 | |||
| 9 | use Gtk2::Ex::FileLocator::Helper; | ||
| 10 | use Gtk2::Ex::FileLocator::FileChooser; | ||
| 11 | |||
| 12 | use Glib::Object::Subclass Gtk2::Ex::FileLocator::FileChooser::, | ||
| 13 | properties => [ | ||
| 14 | Glib::ParamSpec->object( | ||
| 15 | 'entry', 'entry', | ||
| 16 | 'The entry to use.', 'Gtk2::Entry', | ||
| 17 | [qw/readable writable/] | ||
| 18 | ), | ||
| 19 | ], | ||
| 20 | signals => | ||
| 21 | { scroll_offset_changed => { param_types => [qw(Glib::Scalar)], }, }, | ||
| 22 | ; | ||
| 23 | |||
| 24 | sub INIT_INSTANCE { | ||
| 25 | my ($this) = @_; | ||
| 26 | |||
| 27 | $this->{cursorPosition} = 0; | ||
| 28 | |||
| 29 | my $entry = new Gtk2::Entry; | ||
| 30 | $entry->add_events('key-release-mask'); | ||
| 31 | |||
| 32 | $entry->signal_connect( | ||
| 33 | 'key-release-event' => \&on_key_release_event, | ||
| 34 | $this | ||
| 35 | ); | ||
| 36 | |||
| 37 | #$entry->signal_connect_after( 'move-cursor' => \&on_move_cursor, $this ); | ||
| 38 | #$entry->signal_connect( 'event' => \&on_event, $this ); | ||
| 39 | $entry->show; | ||
| 40 | |||
| 41 | $this->set( 'entry', $entry ); | ||
| 42 | $this->add($entry); | ||
| 43 | |||
| 44 | $this->get('chooser') | ||
| 45 | ->signal_connect( 'selection_changed', sub { $this->change_text } ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub on_key_release_event { | ||
| 49 | my ( $entry, $event, $this ) = @_; | ||
| 50 | |||
| 51 | printf "on_key_release_event %s %s\n", $entry->get_text, $this->get_filename; | ||
| 52 | |||
| 53 | #printf "%s\n", $event->keyval; | ||
| 54 | |||
| 55 | return | ||
| 56 | if ($event->keyval == $Gtk2::Gdk::Keysyms{Insert} | ||
| 57 | or $event->keyval == $Gtk2::Gdk::Keysyms{KP_Enter} | ||
| 58 | or $event->keyval == $Gtk2::Gdk::Keysyms{Return} ) | ||
| 59 | and $this->_auto_complete; | ||
| 60 | |||
| 61 | return | ||
| 62 | if $event->keyval == $Gtk2::Gdk::Keysyms{KP_Enter} | ||
| 63 | and file_open( $this->get_filename ); | ||
| 64 | |||
| 65 | return | ||
| 66 | if $event->keyval == $Gtk2::Gdk::Keysyms{Return} | ||
| 67 | and file_open( $this->get_filename ); | ||
| 68 | |||
| 69 | $this->signal_emit( 'scroll_offset_changed', $entry->get("scroll-offset") ); | ||
| 70 | |||
| 71 | return if $this->get_filename eq $entry->get_text; | ||
| 72 | |||
| 73 | $this->{cursorPosition} = $this->get('entry')->get_position; | ||
| 74 | #$this->set_filename( $entry->get_text ); | ||
| 75 | |||
| 76 | my $uri = sprintf "file://%s", $entry->get_text; | ||
| 77 | $this->set_uri( -e $entry->get_text ? $uri : "" ); | ||
| 78 | |||
| 79 | #Gtk2->main_iteration while Gtk2->events_pending; | ||
| 80 | #$this->signal_emit( 'scroll_offset_changed', $entry->get("scroll-offset") ); | ||
| 81 | |||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _auto_complete { | ||
| 86 | my $this = shift; | ||
| 87 | printf "auto_complete %s\n", $this->get('entry')->get_text; | ||
| 88 | |||
| 89 | my $string = $this->get('entry')->get_text; | ||
| 90 | my $match = string_shell_complete($string); | ||
| 91 | |||
| 92 | if ( $string ne $match ) { | ||
| 93 | $this->get('entry')->set_text($match); | ||
| 94 | $this->get('entry')->set_position( length $match ); | ||
| 95 | |||
| 96 | my $uri = sprintf "file://%s", $this->get('entry')->get_text; | ||
| 97 | |||
| 98 | $this->{cursorPosition} = -1; | ||
| 99 | |||
| 100 | $this->set_uri($uri); | ||
| 101 | $this->signal_emit( 'file_activated', $uri ); | ||
| 102 | |||
| 103 | |||
| 104 | return TRUE; | ||
| 105 | } | ||
| 106 | |||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub change_text { | ||
| 111 | my ($this) = @_; | ||
| 112 | |||
| 113 | printf "PF set_text %s\n", $this->get_filename; | ||
| 114 | |||
| 115 | my $filename = string_shell_unescape($this->get_filename); | ||
| 116 | $filename .= "/" if -d $filename; | ||
| 117 | |||
| 118 | $filename =~ s|/+|/|sgo; | ||
| 119 | |||
| 120 | $filename = Unicode::MapUTF8::from_utf8( | ||
| 121 | { -string => $filename, -charset => 'ISO-8859-1' } ); | ||
| 122 | $this->{filename} = $filename; | ||
| 123 | $this->get('entry')->set_text($filename); | ||
| 124 | $this->get('entry')->set_position( $this->{cursorPosition} ); | ||
| 125 | |||
| 126 | # Gtk2->main_iteration while Gtk2->events_pending; | ||
| 127 | # $this->signal_emit( 'scroll_offset_changed', | ||
| 128 | # $this->get('entry')->get("scroll-offset") ); | ||
| 129 | } | ||
| 130 | |||
| 131 | #sub on_move_cursor { | ||
| 132 | # my ($this) = @_; | ||
| 133 | # $this->{cursorPosition} = $this->get_position; | ||
| 134 | # Gtk2->main_iteration while Gtk2->events_pending; | ||
| 135 | # $this->signal_emit( 'scroll_offset_changed', $this->get("scroll-offset") ); | ||
| 136 | # return; | ||
| 137 | #} | ||
| 138 | |||
| 139 | #sub on_event { | ||
| 140 | # my ( $this, $event ) = @_; | ||
| 141 | # return unless $event->type eq 'motion-notify' or $event->type eq 'expose'; | ||
| 142 | # | ||
| 143 | # #printf "on_event %s %s\n", $event->type, $this->get("scroll-offset"); | ||
| 144 | # $this->{cursorPosition} = $this->get('entry')->get_position; | ||
| 145 | # $this->signal_emit( 'scroll_offset_changed', | ||
| 146 | # $this->get('entry')->get("scroll-offset") ); | ||
| 147 | # 0; | ||
| 148 | #} | ||
| 149 | |||
| 150 | 1; | ||
| 151 | __END__ |