| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (c) 2001,2002,2003,2007,2008,2009,2017,2018,2019 Slaven Rezic. All rights reserved. | ||
| 7 | # Copyright (c) 2007,2009 Klaus Wittrock. All rights reserved. | ||
| 8 | # This package is free software; you can redistribute it and/or | ||
| 9 | # modify it under the same terms as Perl itself. | ||
| 10 | # | ||
| 11 | # Mail: srezic@cpan.org | ||
| 12 | # WWW: https://github.com/eserte/tk-pathentry | ||
| 13 | # | ||
| 14 | |||
| 15 | package Tk::PathEntry; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use vars qw($VERSION); | ||
| 19 | $VERSION = '3.08'; | ||
| 20 | |||
| 21 | use base qw(Tk::Derived Tk::Entry); | ||
| 22 | |||
| 23 | Construct Tk::Widget 'PathEntry'; | ||
| 24 | |||
| 25 | sub ClassInit { | ||
| 26 | my($class,$mw) = @_; | ||
| 27 | $class->SUPER::ClassInit($mw); | ||
| 28 | |||
| 29 | # <Down> - popup the choices window | ||
| 30 | $mw->bind($class,"<Down>" => sub { | ||
| 31 | my $w = shift; | ||
| 32 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 33 | # If the popup list is not displayed, display it if possible | ||
| 34 | $w->_popup_on_key($w->get()) if $choices_t->state eq 'withdrawn'; | ||
| 35 | if ($choices_t && $choices_t->state ne 'withdrawn') { | ||
| 36 | my $choices_l = $w->Subwidget("ChoicesLabel"); | ||
| 37 | $choices_l->focus(); | ||
| 38 | my @sel = $choices_l->curselection; | ||
| 39 | if (!@sel) { | ||
| 40 | $choices_l->selectionSet(0); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | }); | ||
| 44 | |||
| 45 | for ("Meta", "Alt") { | ||
| 46 | $mw->bind($class,"<$_-BackSpace>" => '_delete_last_path_component'); | ||
| 47 | $mw->bind($class,"<$_-d>" => '_delete_next_path_component'); | ||
| 48 | $mw->bind($class,"<$_-f>" => '_forward_path_component'); | ||
| 49 | $mw->bind($class,"<$_-b>" => '_backward_path_component'); | ||
| 50 | $mw->bind($class,"<$_-Delete>" => '_delete_next_path_component'); | ||
| 51 | $mw->bind($class,"<$_-Right>" => '_forward_path_component'); | ||
| 52 | $mw->bind($class,"<$_-Left>" => '_backward_path_component'); | ||
| 53 | } | ||
| 54 | |||
| 55 | # Withdraw the choices listbox when the PathEntry looses focus | ||
| 56 | $mw->bind($class,"<FocusOut>" => \&_bind_focusout); | ||
| 57 | |||
| 58 | # On <Return> in the Entry withdraw the choices listbox and invoke the selectcmd callback | ||
| 59 | $mw->bind($class,"<Return>" => \&_bind_return); | ||
| 60 | |||
| 61 | $class; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Class binding for <FocusOut> | ||
| 65 | |||
| 66 | sub _bind_focusout { | ||
| 67 | my $w = shift; | ||
| 68 | # Note: A button 1 mouse click in the choices listbox will invoke the | ||
| 69 | # FocusOut callback of the Entry widget before invoking the Button-1 callback | ||
| 70 | # of the choices listbox, if the focus is in the Entry at that moment. | ||
| 71 | # In this case $w->focusCurrent is undefined. The Button-1 callback will | ||
| 72 | # withdraw the choices listbox. | ||
| 73 | # Also $w->focusCurrent is undefined if the focus is passed to a | ||
| 74 | # different window. | ||
| 75 | return unless defined $w->focusCurrent; | ||
| 76 | |||
| 77 | # Don't withdraw the choices listbox if the focus just has been passed to it. | ||
| 78 | return if $w->focusCurrent eq $w->Subwidget("ChoicesLabel"); | ||
| 79 | |||
| 80 | # Sometimes the focus is passed from the PathEntry to the PathEntry | ||
| 81 | # (that means $w->focusCurrent eq $w). | ||
| 82 | # This queer situation can be reproduced on Windows OS as follows: | ||
| 83 | # Klick on a directory that is displayed below the PathEntry::Dialog; | ||
| 84 | # Enter <Tab> or / to get the next choices list; | ||
| 85 | # Select the 2nd choice at a point above the OK button. | ||
| 86 | # Apparently this situation doesn't do any harm here. | ||
| 87 | |||
| 88 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 89 | $choices_t->withdraw; | ||
| 90 | }; | ||
| 91 | |||
| 92 | # Class binding for <Return> in the Entry | ||
| 93 | |||
| 94 | sub _bind_return { | ||
| 95 | my $w = shift; | ||
| 96 | $w->Finish; | ||
| 97 | $w->_exec_selectcmd; | ||
| 98 | }; | ||
| 99 | |||
| 100 | sub Populate { | ||
| 101 | my($w, $args) = @_; | ||
| 102 | |||
| 103 | # Set proper encoding for file operations | ||
| 104 | if ($Tk::VERSION >= 804 && eval { require Encode; 1 }) { | ||
| 105 | my $encoding; | ||
| 106 | if ($^O eq 'MSWin32') {$encoding = Win32_encod()} | ||
| 107 | # elsif ($^O eq '.....') {$encoding = '.....'} # add known encodings for other platforms | ||
| 108 | # elsif ($^O eq '.....') {$encoding = '.....'} # see perldoc perlport for OS names | ||
| 109 | else {$encoding = 'iso-8859-1'} | ||
| 110 | $w->{Encoding} = $encoding; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $choices_t = $w->Component("Toplevel" => "ChoicesToplevel"); | ||
| 114 | $choices_t->overrideredirect(1); | ||
| 115 | $choices_t->withdraw; | ||
| 116 | |||
| 117 | my $choices_l = $choices_t->Listbox( | ||
| 118 | -border => 0, | ||
| 119 | -width => 0, # use autowidth feature | ||
| 120 | )->pack(-fill => "both", | ||
| 121 | -expand => 1); | ||
| 122 | if ($Tk::platform eq 'MSWin32') { | ||
| 123 | $choices_l->configure(-border => 1, -relief => 'solid'); | ||
| 124 | } else { | ||
| 125 | $choices_l->configure(-background => 'yellow'); | ||
| 126 | } | ||
| 127 | $w->Advertise("ChoicesLabel" => $choices_l); | ||
| 128 | |||
| 129 | # <Button-1> in the Listbox | ||
| 130 | $choices_l->bind("<1>" => sub { | ||
| 131 | my $lb = shift; | ||
| 132 | (my $sel) = $lb->curselection; | ||
| 133 | # Take full path from CurrentChoices array | ||
| 134 | my $path = $w->{CurrentChoices}[$sel]; | ||
| 135 | # append separator to directory | ||
| 136 | $path .= $w->{sep} if $w->Callback(-isdircmd => $w, $path); | ||
| 137 | $w->_set_text($path); | ||
| 138 | $w->Finish; | ||
| 139 | }); | ||
| 140 | |||
| 141 | # <Return> in the Listbox | ||
| 142 | $choices_l->bind("<Return>" => sub { | ||
| 143 | # Transfer the selection to the Entry widget | ||
| 144 | my $lb = shift; | ||
| 145 | my @sel = $lb->curselection; | ||
| 146 | if (@sel) { | ||
| 147 | # Take full path from CurrentChoices array | ||
| 148 | my $path = $w->{CurrentChoices}[$sel[0]]; | ||
| 149 | # append separator to directory | ||
| 150 | $path .= $w->{sep} if $w->Callback(-isdircmd => $w, $path); | ||
| 151 | $w->_set_text($path); | ||
| 152 | } | ||
| 153 | $w->Finish; | ||
| 154 | }); | ||
| 155 | # <Return> in the Entry is now a class binding | ||
| 156 | |||
| 157 | # <Escape> in the Listbox | ||
| 158 | $choices_l->bind("<Escape>" => sub { | ||
| 159 | $w->Finish; | ||
| 160 | }); | ||
| 161 | |||
| 162 | # <Escape> in the Entry | ||
| 163 | $w->bind("<Escape>" => sub { | ||
| 164 | $w->Finish; | ||
| 165 | $w->Callback(-cancelcmd => $w); | ||
| 166 | }); | ||
| 167 | |||
| 168 | # <FocusIn> in the Entry | ||
| 169 | $w->bind("<FocusIn>" => sub { | ||
| 170 | # If the focus is passed to the entry widget by <Tab> or <Shift-Tab>, | ||
| 171 | # all text in the widget gets selected. This might lateron cause | ||
| 172 | # unintended deletion when pressing a key. | ||
| 173 | $w->selectionClear(); | ||
| 174 | }); | ||
| 175 | |||
| 176 | # <FocusOut> in the Listbox | ||
| 177 | # Hide the Listbox window if visible | ||
| 178 | $choices_l->bind("<FocusOut>" => sub { | ||
| 179 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 180 | return unless $choices_t->state eq 'normal'; | ||
| 181 | $choices_t->withdraw; | ||
| 182 | }); | ||
| 183 | |||
| 184 | if (exists $args->{-vcmd} || | ||
| 185 | exists $args->{-validatecommand} || | ||
| 186 | exists $args->{-validate}) { | ||
| 187 | die "-vcmd, -validatecommand or -validate are not allowed with PathEntry"; | ||
| 188 | } | ||
| 189 | |||
| 190 | $args->{-vcmd} = sub { | ||
| 191 | my($pathname) = $_[0]; | ||
| 192 | my($action) = $_[4]; | ||
| 193 | $action -= 7 if $action > 5; # replace actual by official value | ||
| 194 | return 1 if $action == -1; # nothing on forced validation | ||
| 195 | |||
| 196 | # validate directory on input of separator | ||
| 197 | if ($action == 1) { | ||
| 198 | my $pos_sep_rx = $w->{pos_sep_rx}; | ||
| 199 | my $case_rx = $w->cget(-casesensitive) ? "" : "(?i)"; | ||
| 200 | $w->_valid_dir('Path', @_) if $pathname =~ /$case_rx$pos_sep_rx$/; | ||
| 201 | } | ||
| 202 | $w->_popup_on_key($pathname); | ||
| 203 | |||
| 204 | if ($action == 1 && # only on INSERT | ||
| 205 | $w->{CurrentChoices} && @{$w->{CurrentChoices}} == 1 && | ||
| 206 | $w->cget(-autocomplete)) { | ||
| 207 | # autocomplete annoys when the user is entering a drive name | ||
| 208 | return 1 if ($^O eq 'MSWin32' && length($pathname) == 1); | ||
| 209 | # XXX the afterIdle is hackish | ||
| 210 | $w->afterIdle(sub { $w->_set_text($w->{CurrentChoices}[0]) }); | ||
| 211 | return 0; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 | }; | ||
| 216 | $args->{-validate} = 'key'; | ||
| 217 | |||
| 218 | if (!exists $args->{-textvariable}) { | ||
| 219 | my $pathname; | ||
| 220 | $args->{-textvariable} = \$pathname; | ||
| 221 | } | ||
| 222 | # avoid undefined initial pathname | ||
| 223 | # needed when the user enters <Return> right at the beginning | ||
| 224 | ${$args->{-textvariable}} = '' unless defined ${$args->{-textvariable}}; | ||
| 225 | |||
| 226 | # validate directory color | ||
| 227 | undef $@; | ||
| 228 | eval {$w->rgb($args->{-dircolor})} if exists $args->{-dircolor}; | ||
| 229 | if ($@) { | ||
| 230 | (my $msg = $@) =~ s/ at .+/ replaced by "blue"/s; | ||
| 231 | $w->afterIdle(sub {$w->Callback(-messagecmd => $w, "Option -dircolor: $msg")}); | ||
| 232 | $args->{-dircolor} = 'blue'; | ||
| 233 | } | ||
| 234 | |||
| 235 | $w->ConfigSpecs | ||
| 236 | (-initialdir => ['PASSIVE', undef, undef, undef], | ||
| 237 | -initialfile => ['PASSIVE', undef, undef, undef], | ||
| 238 | -separator => ['PASSIVE', undef, undef, | ||
| 239 | $^O eq "MSWin32" ? ["/", "\\"] : "/" | ||
| 240 | ], | ||
| 241 | -casesensitive => ['PASSIVE', undef, undef, | ||
| 242 | $^O eq "MSWin32" ? 0 : 1 | ||
| 243 | ], | ||
| 244 | -autocomplete => ['PASSIVE'], | ||
| 245 | -isdircmd => ['CALLBACK', undef, undef, ['_is_dir']], | ||
| 246 | -isdirectorycommand => '-isdircmd', | ||
| 247 | -choicescmd => ['CALLBACK', undef, undef, ['_get_choices']], | ||
| 248 | -choicescommand => '-choicescmd', | ||
| 249 | -selectcmd => ['CALLBACK'], | ||
| 250 | -selectcommand => '-selectcmd', | ||
| 251 | -cancelcmd => ['CALLBACK'], | ||
| 252 | -cancelcommand => '-cancelcmd', | ||
| 253 | -messagecmd => ['CALLBACK', undef, undef, ['_show_msg']], | ||
| 254 | -messagecommand => '-messagecmd', | ||
| 255 | -pathcompl => ['PASSIVE', undef, undef, '<Tab>'], | ||
| 256 | -pathcompletion => '-pathcompl', | ||
| 257 | -height => [$choices_l, qw/height Height 10/], | ||
| 258 | -dircolor => ['PASSIVE', undef, undef, undef], | ||
| 259 | ); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub ConfigChanged { | ||
| 263 | my($w,$args) = @_; | ||
| 264 | |||
| 265 | # Call separator subroutines only once. | ||
| 266 | unless (defined $w->{sep}) { | ||
| 267 | $w->{sep} = $w->_sep; | ||
| 268 | $w->{pos_sep_rx} = $w->_pos_sep_rx; | ||
| 269 | $w->{neg_sep_rx} = $w->_neg_sep_rx; | ||
| 270 | } | ||
| 271 | |||
| 272 | # Set initial path | ||
| 273 | $w->_set_intial_path ($w->cget(-initialdir), $w->cget(-initialfile)); | ||
| 274 | |||
| 275 | # Bind the user-defined completion key | ||
| 276 | $w->_bind_completion($args->{-pathcompl}) if defined $args->{-pathcompl}; | ||
| 277 | $w->{max_show} = $w->cget(-height); # save original height of the listbox | ||
| 278 | } | ||
| 279 | |||
| 280 | # Finish is meant for "terminating" Events (<Return>, <Escape>, <Button-1>), | ||
| 281 | # but not for cases where "$choices_t->withdraw;" will do. Beware of the | ||
| 282 | # focus change in Finish. | ||
| 283 | sub Finish { | ||
| 284 | my $w = shift; | ||
| 285 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 286 | return unless $choices_t->state eq 'normal'; | ||
| 287 | my $focus_curr = $choices_t->focusCurrent; | ||
| 288 | $choices_t->withdraw; | ||
| 289 | $choices_t->idletasks; | ||
| 290 | delete $w->{CurrentChoices}; | ||
| 291 | my $top = $w->toplevel; | ||
| 292 | return unless $top->state eq 'normal'; | ||
| 293 | $top->deiconify(); # ensure the visiblity of the container window | ||
| 294 | $top->raise(); | ||
| 295 | my $choices_l = $w->Subwidget("ChoicesLabel"); | ||
| 296 | # pass focus back to the Entry widget (required for Linux) | ||
| 297 | $w->focus() if $focus_curr eq $choices_l; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub _popup_on_key { | ||
| 301 | my($w, $pathname) = @_; | ||
| 302 | if ($w->ismapped) { | ||
| 303 | if ($w->{Encoding}) { | ||
| 304 | $pathname = Encode::encode("$w->{Encoding}", $pathname); | ||
| 305 | } | ||
| 306 | $w->{CurrentChoices} = $w->Callback(-choicescmd => $w, $pathname); | ||
| 307 | if ($w->{CurrentChoices} && @{$w->{CurrentChoices}} > 1) { | ||
| 308 | my $choices_l = $w->Subwidget("ChoicesLabel"); | ||
| 309 | $choices_l->delete(0, 'end'); | ||
| 310 | $w->_insert_pathnames($choices_l); | ||
| 311 | # Mark directories if requested | ||
| 312 | $w->_dircolor($choices_l, $w->cget(-dircolor)) if defined $w->cget(-dircolor); | ||
| 313 | # When the focus is passed to the Listbox, the last entry is | ||
| 314 | # active, because the lines were inserted as a list. So pressing | ||
| 315 | # the down arrow would select the last entry. | ||
| 316 | $choices_l->activate(0); | ||
| 317 | $w->_show_choices($w->rootx); | ||
| 318 | } else { | ||
| 319 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 320 | $choices_t->withdraw; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _sep { | ||
| 326 | my $w = shift; | ||
| 327 | my $sep = $w->cget(-separator); | ||
| 328 | if (ref $sep eq 'ARRAY') { | ||
| 329 | $sep->[0]; | ||
| 330 | } else { | ||
| 331 | $sep; | ||
| 332 | } | ||
| 333 | |||
| 334 | } | ||
| 335 | |||
| 336 | sub _pos_sep_rx { | ||
| 337 | my $w = shift; | ||
| 338 | my $sep = $w->cget(-separator); | ||
| 339 | if (ref $sep eq 'ARRAY') { | ||
| 340 | "[" . join("", map { quotemeta } @$sep) . "]"; | ||
| 341 | } else { | ||
| 342 | quotemeta $sep; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | sub _neg_sep_rx { | ||
| 347 | my $w = shift; | ||
| 348 | my $sep = $w->cget(-separator); | ||
| 349 | if (ref $sep eq 'ARRAY') { | ||
| 350 | "[^" . join("", map { quotemeta } @$sep) . "]"; | ||
| 351 | } else { | ||
| 352 | "[^" . quotemeta($sep) . "]"; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | sub _delete_last_path_component { | ||
| 357 | my $w = shift; | ||
| 358 | |||
| 359 | my $before_cursor = substr($w->get, 0, $w->index("insert")); | ||
| 360 | my $after_cursor = substr($w->get, $w->index("insert")); | ||
| 361 | my $pos_sep = $w->{pos_sep_rx}; | ||
| 362 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 363 | $before_cursor =~ s|$neg_sep+$pos_sep?$||; | ||
| 364 | my $pathref = $w->cget(-textvariable); | ||
| 365 | $$pathref = $before_cursor . $after_cursor; | ||
| 366 | $w->icursor(length $before_cursor); | ||
| 367 | $w->_popup_on_key($$pathref); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub _delete_next_path_component { | ||
| 371 | my $w = shift; | ||
| 372 | |||
| 373 | my $before_cursor = substr($w->get, 0, $w->index("insert")); | ||
| 374 | my $after_cursor = substr($w->get, $w->index("insert")); | ||
| 375 | my $pos_sep = $w->{pos_sep_rx}; | ||
| 376 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 377 | $after_cursor =~ s|^$pos_sep?$neg_sep+||; | ||
| 378 | my $pathref = $w->cget(-textvariable); | ||
| 379 | $$pathref = $before_cursor . $after_cursor; | ||
| 380 | $w->icursor(length $before_cursor); | ||
| 381 | $w->_popup_on_key($$pathref); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub _forward_path_component { | ||
| 385 | my $w = shift; | ||
| 386 | my $after_cursor = substr($w->get, $w->index("insert")); | ||
| 387 | my $pos_sep = $w->{pos_sep_rx}; | ||
| 388 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 389 | if ($after_cursor =~ m|^($pos_sep?$neg_sep+)|) { | ||
| 390 | $w->icursor($w->index("insert") + length $1); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | sub _backward_path_component { | ||
| 395 | my $w = shift; | ||
| 396 | my $before_cursor = substr($w->get, 0, $w->index("insert")); | ||
| 397 | my $pos_sep = $w->{pos_sep_rx}; | ||
| 398 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 399 | if ($before_cursor =~ m|($neg_sep+$pos_sep?)$|) { | ||
| 400 | $w->icursor($w->index("insert") - length $1); | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _common_match { | ||
| 405 | my $w = shift; | ||
| 406 | my $choices = $w->{CurrentChoices}; | ||
| 407 | my $case_sensitive = $w->cget(-casesensitive); | ||
| 408 | my $common = $choices->[0]; | ||
| 409 | $common = lc $common if !$case_sensitive; | ||
| 410 | foreach my $j (1 .. $#{$choices}) { | ||
| 411 | my $choice = $case_sensitive ? $choices->[$j] : lc $choices->[$j]; | ||
| 412 | if (length $choice < length $common) { | ||
| 413 | substr($common, length $choice) = ''; | ||
| 414 | } | ||
| 415 | for my $i (0 .. length($common) - 1) { | ||
| 416 | if (substr($choice, $i, 1) ne substr($common, $i, 1)) { | ||
| 417 | return "" if $i == 0; | ||
| 418 | $common = substr($choice, 0, $i); | ||
| 419 | last; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | } | ||
| 423 | # Restore original case | ||
| 424 | $common = substr($choices->[0], 0, length($common)) if !$case_sensitive; | ||
| 425 | $common; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub _get_choices { | ||
| 429 | my($w, $pathname) = @_; | ||
| 430 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 431 | if ($pathname =~ m|^~($neg_sep+)$|) { | ||
| 432 | my $userglob = $1; | ||
| 433 | my @users; | ||
| 434 | my $sep = $w->{sep}; | ||
| 435 | while(my $user = getpwent) { | ||
| 436 | if ($user =~ /^$userglob/) { | ||
| 437 | push @users, "~$user$sep"; | ||
| 438 | last if $#users > 50; # XXX make better optimization! | ||
| 439 | } | ||
| 440 | } | ||
| 441 | endpwent; | ||
| 442 | if (@users) { | ||
| 443 | \@users; | ||
| 444 | } else { | ||
| 445 | [$pathname]; | ||
| 446 | } | ||
| 447 | } else { | ||
| 448 | [ <$pathname*> ]; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | sub _show_choices { | ||
| 453 | my($w, $x_pos) = @_; | ||
| 454 | my $choices_t = $w->Subwidget("ChoicesToplevel"); | ||
| 455 | |||
| 456 | # Set dynamic height of listbox | ||
| 457 | my $choices_height = $w->{max_show}; | ||
| 458 | if ($choices_height < 0) { | ||
| 459 | my $max_height = @{$w->{CurrentChoices}}; | ||
| 460 | if ($max_height > -$choices_height) { | ||
| 461 | $max_height = -$choices_height; | ||
| 462 | } | ||
| 463 | my $choices_l = $w->Subwidget("ChoicesLabel"); | ||
| 464 | $choices_l->configure(-height => $max_height); | ||
| 465 | } | ||
| 466 | |||
| 467 | if (defined $x_pos) { | ||
| 468 | $choices_t->geometry("+" . $x_pos . "+" . ($w->rooty+$w->height)); | ||
| 469 | $choices_t->deiconify; | ||
| 470 | $choices_t->raise; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
| 474 | sub _is_dir { -d $_[1] } | ||
| 475 | |||
| 476 | # Bind the user-defined completion key | ||
| 477 | |||
| 478 | sub _bind_completion { | ||
| 479 | my($w, $compl_event) = @_; | ||
| 480 | |||
| 481 | # When Tk::PathEntry is called indirectly via Tk::PathEntry::Dialog->Show, | ||
| 482 | # sub Populate will get called twice, first with the default value of the | ||
| 483 | # -pathcompl option, then with the user supplied value. Filter out the | ||
| 484 | # correct call. | ||
| 485 | return unless $compl_event eq $w->cget(-pathcompl); | ||
| 486 | |||
| 487 | if ($Tk::platform eq 'MSWin32' and $compl_event eq '<Alt-Tab>') { | ||
| 488 | my $msg = 'Event "Alt-Tab" is reserved by the Operating System; use "Tab" instead.'; | ||
| 489 | $w->Callback(-messagecmd => $w, "Option -pathcompletion: $msg"); | ||
| 490 | $compl_event = '<Tab>'; | ||
| 491 | } | ||
| 492 | eval {$w->bind($compl_event => \&_complete_current_path)}; | ||
| 493 | if ($@) { | ||
| 494 | (my $msg = $@) =~ s/ at .+/; use "Tab" instead./s; # cut off line info | ||
| 495 | $w->Callback(-messagecmd => $w, "Option -pathcompletion: $msg"); | ||
| 496 | $compl_event = '<Tab>'; | ||
| 497 | $w->bind('<Tab>' => \&_complete_current_path); | ||
| 498 | } | ||
| 499 | # Restore standard behaviour of Shift-Tab | ||
| 500 | if ($compl_event eq '<Tab>') { | ||
| 501 | $w->bind('<Shift-Tab>' => sub {$w->focusPrev}); | ||
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 | # Callback to force the completion of the current path | ||
| 506 | |||
| 507 | sub _complete_current_path { | ||
| 508 | my $w = shift; | ||
| 509 | |||
| 510 | if (!defined $w->{CurrentChoices}) { | ||
| 511 | # this is called only on init: | ||
| 512 | my $pathref = $w->cget(-textvariable); | ||
| 513 | my $pathname = $$pathref; | ||
| 514 | if ($w->{Encoding}) { | ||
| 515 | $pathname = Encode::encode("$w->{Encoding}", $pathname); | ||
| 516 | } | ||
| 517 | $w->{CurrentChoices} = $w->Callback(-choicescmd => $w, $pathname); | ||
| 518 | } | ||
| 519 | if (@{$w->{CurrentChoices}} > 0) { | ||
| 520 | my $pos_sep_rx = $w->{pos_sep_rx}; | ||
| 521 | my $common = $w->_common_match; | ||
| 522 | my $case_rx = $w->cget(-casesensitive) ? "" : "(?i)"; | ||
| 523 | if ($w->Callback(-isdircmd => $w, $common) && | ||
| 524 | $common !~ m/$case_rx$pos_sep_rx$/ && | ||
| 525 | @{$w->{CurrentChoices}} == 1 | ||
| 526 | ) { | ||
| 527 | my $sep = $w->{sep}; | ||
| 528 | $common .= $sep; | ||
| 529 | } | ||
| 530 | $w->_set_text($common); | ||
| 531 | $w->_popup_on_key($common); | ||
| 532 | } else { | ||
| 533 | $w->bell; | ||
| 534 | } | ||
| 535 | Tk->break; | ||
| 536 | } | ||
| 537 | |||
| 538 | # Execute the -selectcmd callback | ||
| 539 | # pass encoded path as 2nd parameter | ||
| 540 | |||
| 541 | sub _exec_selectcmd { | ||
| 542 | my $w = shift; | ||
| 543 | |||
| 544 | my $pathname = $ {$w->cget(-textvariable)}; | ||
| 545 | if ($w->{Encoding}) { | ||
| 546 | $pathname = Encode::encode("$w->{Encoding}", $pathname); | ||
| 547 | } | ||
| 548 | $w->Callback(-selectcmd => $w, $pathname); | ||
| 549 | } | ||
| 550 | |||
| 551 | # Replace text in widget and position the cursor to the end | ||
| 552 | |||
| 553 | sub _set_text { | ||
| 554 | my ($w, $text) = @_; | ||
| 555 | |||
| 556 | $ {$w->cget(-textvariable)} = $text; | ||
| 557 | $w->icursor("end"); | ||
| 558 | $w->xview("end"); | ||
| 559 | } | ||
| 560 | |||
| 561 | # Warn if "directory" exists as a plain file | ||
| 562 | |||
| 563 | sub _valid_dir { | ||
| 564 | my ($w, $type, $pathname) = @_; | ||
| 565 | |||
| 566 | # remove trailing separators | ||
| 567 | my $pos_sep_rx = $w->{pos_sep_rx}; | ||
| 568 | my $case_rx = $w->cget(-casesensitive) ? "" : "(?i)"; | ||
| 569 | $pathname =~ s/$case_rx$pos_sep_rx+$//; | ||
| 570 | return unless $pathname; | ||
| 571 | if (-e $pathname && ! $w->Callback(-isdircmd => $w, $pathname)) { | ||
| 572 | # $type is 'Path' or 'Initial directory'. | ||
| 573 | $w->Callback(-messagecmd => $w, | ||
| 574 | "$type $pathname\nis not a directory\n(exists as a plain file)"); | ||
| 575 | # Don't suppress or attempt to autocorrect the directory. | ||
| 576 | # Give the user the chance to correct a typo error. | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | # Insert last component of path names into listbox | ||
| 581 | |||
| 582 | sub _insert_pathnames { | ||
| 583 | my ($w, $choices_l) = @_; | ||
| 584 | my $choices = $w->{CurrentChoices}; | ||
| 585 | |||
| 586 | # Show last component of file names | ||
| 587 | my $pos_sep_rx = $w->{pos_sep_rx}; | ||
| 588 | my $case_rx = $w->cget(-casesensitive) ? "" : "(?i)"; | ||
| 589 | if (@{$choices}[0] =~ /(.*$case_rx$pos_sep_rx)./) { | ||
| 590 | my $first = length($1); | ||
| 591 | $choices_l->insert("end", | ||
| 592 | map {substr($_, $first)} @{$choices}); | ||
| 593 | } else { | ||
| 594 | $choices_l->insert("end", @{$choices}); | ||
| 595 | } | ||
| 596 | } | ||
| 597 | |||
| 598 | # Display directories in a different color | ||
| 599 | |||
| 600 | sub _dircolor { | ||
| 601 | my ($w, $choices_l, $dircolor) = @_; | ||
| 602 | my $path; | ||
| 603 | |||
| 604 | foreach (0 .. $choices_l->size - 1) { | ||
| 605 | # Take full path from CurrentChoices array | ||
| 606 | $path = $w->{CurrentChoices}[$_]; | ||
| 607 | $choices_l->itemconfigure($_, -foreground => $dircolor) | ||
| 608 | if $w->Callback(-isdircmd => $w, $path); | ||
| 609 | } | ||
| 610 | } | ||
| 611 | |||
| 612 | # Show message by default in messageBox | ||
| 613 | |||
| 614 | sub _show_msg { | ||
| 615 | my ($w, $msg) = @_; | ||
| 616 | |||
| 617 | $w->messageBox(-title => $msg =~ /^Error:/ ? 'Error' : 'Warning', | ||
| 618 | -icon => 'warning', -message => $msg); | ||
| 619 | } | ||
| 620 | |||
| 621 | # For Windows OS, find out the encoding of perl's console window | ||
| 622 | |||
| 623 | sub Win32_encod { | ||
| 624 | return eval { require Win32::Console; 1 } ? | ||
| 625 | 'windows-' . Win32::Console->new()->OutputCP : 'windows-850'; | ||
| 626 | } | ||
| 627 | |||
| 628 | # Set initial path in Entry widget | ||
| 629 | |||
| 630 | sub _set_intial_path { | ||
| 631 | my ($w, $initdir, $initfile) = @_; | ||
| 632 | |||
| 633 | return if exists $w->{initpath}; | ||
| 634 | $w->{initpath} = ''; # no need to re-execute on further calls of ConfigChanged | ||
| 635 | my $initpath = ''; | ||
| 636 | if (defined $initdir) { | ||
| 637 | # Warn if "directory" exists as a plain file | ||
| 638 | $w->_valid_dir('Initial directory', $initdir); | ||
| 639 | $initpath = $initdir; | ||
| 640 | my $neg_sep = $w->{neg_sep_rx}; | ||
| 641 | # add separator after existing dir or if $initfile will be appended | ||
| 642 | # unless there is already a separator | ||
| 643 | if (($w->Callback(-isdircmd => $w, $initdir) or | ||
| 644 | (defined $initfile and $initfile ne '')) | ||
| 645 | and ($initdir =~ /$neg_sep$/)) { | ||
| 646 | $initpath .= $w->{sep}; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | $initpath .= $initfile if defined $initfile; | ||
| 650 | $w->_set_text($initpath) unless $initpath eq ''; | ||
| 651 | } | ||
| 652 | |||
| 653 | 1; | ||
| 654 | |||
| 655 | __END__ |