| 1 |
# -*- perl -*- |
| 2 |
# |
| 3 |
# tkfbox.tcl -- |
| 4 |
# |
| 5 |
# Implements the "TK" standard file selection dialog box. This |
| 6 |
# dialog box is used on the Unix platforms whenever the tk_strictMotif |
| 7 |
# flag is not set. |
| 8 |
# |
| 9 |
# The "TK" standard file selection dialog box is similar to the |
| 10 |
# file selection dialog box on Win95(TM). The user can navigate |
| 11 |
# the directories by clicking on the folder icons or by |
| 12 |
# selecting the "Directory" option menu. The user can select |
| 13 |
# files by clicking on the file icons or by entering a filename |
| 14 |
# in the "Filename:" entry. |
| 15 |
# |
| 16 |
# Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 17 |
# |
| 18 |
# See the file "license.terms" for information on usage and redistribution |
| 19 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 20 |
# |
| 21 |
# Translated to perl/Tk by Slaven Rezic <[email protected]>. |
| 22 |
# |
| 23 |
|
| 24 |
#---------------------------------------------------------------------- |
| 25 |
# |
| 26 |
# F I L E D I A L O G |
| 27 |
# |
| 28 |
#---------------------------------------------------------------------- |
| 29 |
# tkFDialog -- |
| 30 |
# |
| 31 |
# Implements the TK file selection dialog. This dialog is used when |
| 32 |
# the tk_strictMotif flag is set to false. This procedure shouldn't |
| 33 |
# be called directly. Call tk_getOpenFile or tk_getSaveFile instead. |
| 34 |
# |
| 35 |
|
| 36 |
use 5.005; # qr// |
| 37 |
|
| 38 |
package Tk::FBox; |
| 39 |
require Tk::Toplevel; |
| 40 |
|
| 41 |
use strict; |
| 42 |
use vars qw($VERSION $updirImage $folderImage $fileImage); |
| 43 |
|
| 44 |
#$VERSION = sprintf '4.%03d', q$Revision: #18 $ =~ /\D(\d+)\s*$/; |
| 45 |
$VERSION = '4.021'; |
| 46 |
|
| 47 |
use base qw(Tk::Toplevel); |
| 48 |
|
| 49 |
Construct Tk::Widget 'FBox'; |
| 50 |
|
| 51 |
sub import { |
| 52 |
if (defined $_[1] and $_[1] eq 'as_default') { |
| 53 |
local $^W = 0; |
| 54 |
package Tk; |
| 55 |
if ($Tk::VERSION < 804) { |
| 56 |
*FDialog = \&Tk::FBox::FDialog; |
| 57 |
*MotifFDialog = \&Tk::FBox::FDialog; |
| 58 |
} else { |
| 59 |
*tk_getOpenFile = sub { |
| 60 |
Tk::FBox::FDialog("tk_getOpenFile", @_); |
| 61 |
}; |
| 62 |
*tk_getSaveFile = sub { |
| 63 |
Tk::FBox::FDialog("tk_getSaveFile", @_); |
| 64 |
}; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
# Note that -sortcmd is experimental and the interface is likely to change. |
| 70 |
# Using -sortcmd is really strange :-( |
| 71 |
# $top->getOpenFile(-sortcmd => sub { package Tk::FBox; uc $b cmp uc $a}); |
| 72 |
# or, un-perlish, but useable (now activated in code): |
| 73 |
# $top->getOpenFile(-sortcmd => sub { uc $_[1] cmp uc $_[0]}); |
| 74 |
|
| 75 |
sub Populate { |
| 76 |
my($w, $args) = @_; |
| 77 |
|
| 78 |
require Tk::IconList; |
| 79 |
require File::Basename; |
| 80 |
require Cwd; |
| 81 |
|
| 82 |
$w->SUPER::Populate($args); |
| 83 |
|
| 84 |
$w->{'encoding'} = $w->getEncoding('iso_8859_1'); |
| 85 |
|
| 86 |
# f1: the frame with the directory option menu |
| 87 |
my $f1 = $w->Frame; |
| 88 |
my $lab = $f1->Label(-text => 'Directory:', -underline => 0); |
| 89 |
$w->{'dirMenu'} = my $dirMenu = |
| 90 |
$f1->Optionmenu(-variable => \$w->{'selectPath'}, |
| 91 |
-textvariable => \$w->{'selectPath'}, |
| 92 |
-command => ['SetPath', $w]); |
| 93 |
my $upBtn = $f1->Button; |
| 94 |
if (!defined $updirImage->{$w->MainWindow}) { |
| 95 |
$updirImage->{$w->MainWindow} = $w->Bitmap(-data => <<EOF); |
| 96 |
#define updir_width 28 |
| 97 |
#define updir_height 16 |
| 98 |
static char updir_bits[] = { |
| 99 |
0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, |
| 100 |
0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01, |
| 101 |
0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01, |
| 102 |
0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, |
| 103 |
0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01, |
| 104 |
0xf0, 0xff, 0xff, 0x01}; |
| 105 |
EOF |
| 106 |
} |
| 107 |
$upBtn->configure(-image => $updirImage->{$w->MainWindow}); |
| 108 |
$dirMenu->configure(-takefocus => 1, -highlightthickness => 2); |
| 109 |
$upBtn->pack(-side => 'right', -padx => 4, -fill => 'both'); |
| 110 |
$lab->pack(-side => 'left', -padx => 4, -fill => 'both'); |
| 111 |
$dirMenu->pack(-expand => 'yes', -fill => 'both', -padx => 4); |
| 112 |
|
| 113 |
$w->{'icons'} = my $icons = |
| 114 |
$w->IconList(-command => ['OkCmd', $w, 'iconlist'], |
| 115 |
); |
| 116 |
$icons->bind('<<ListboxSelect>>' => [$w, 'ListBrowse']); |
| 117 |
|
| 118 |
# f2: the frame with the OK button and the "file name" field |
| 119 |
my $f2 = $w->Frame(-bd => 0); |
| 120 |
#XXX File name => File names if multiple |
| 121 |
my $f2_lab = $f2->Label(-text => 'File name:', -anchor => 'e', |
| 122 |
-width => 14, -underline => 5, -pady => 0); |
| 123 |
$w->{'ent'} = my $ent = $f2->Entry; |
| 124 |
|
| 125 |
# The font to use for the icons. The default Canvas font on Unix |
| 126 |
# is just deviant. |
| 127 |
# $w->{'icons'}{'font'} = $ent->cget(-font); |
| 128 |
$w->{'icons'}->configure(-font => $ent->cget(-font)); |
| 129 |
|
| 130 |
# f3: the frame with the cancel button and the file types field |
| 131 |
my $f3 = $w->Frame(-bd => 0); |
| 132 |
|
| 133 |
# The "File of types:" label needs to be grayed-out when |
| 134 |
# -filetypes are not specified. The label widget does not support |
| 135 |
# grayed-out text on monochrome displays. Therefore, we have to |
| 136 |
# use a button widget to emulate a label widget (by setting its |
| 137 |
# bindtags) |
| 138 |
$w->{'typeMenuLab'} = my $typeMenuLab = $f3->Button |
| 139 |
(-text => 'Files of type:', |
| 140 |
-anchor => 'e', |
| 141 |
-width => 14, |
| 142 |
-underline => 9, |
| 143 |
-bd => $f2_lab->cget(-bd), |
| 144 |
-highlightthickness => $f2_lab->cget(-highlightthickness), |
| 145 |
-relief => $f2_lab->cget(-relief), |
| 146 |
-padx => $f2_lab->cget(-padx), |
| 147 |
-pady => $f2_lab->cget(-pady), |
| 148 |
-takefocus => 0, |
| 149 |
); |
| 150 |
$typeMenuLab->bindtags([$typeMenuLab, 'Label', |
| 151 |
$typeMenuLab->toplevel, 'all']); |
| 152 |
$w->{'typeMenuBtn'} = my $typeMenuBtn = |
| 153 |
$f3->Menubutton(-indicatoron => 1, -tearoff => 0); |
| 154 |
$typeMenuBtn->configure(-takefocus => 1, |
| 155 |
-highlightthickness => 2, |
| 156 |
-relief => 'raised', |
| 157 |
-bd => 2, |
| 158 |
-anchor => 'w', |
| 159 |
); |
| 160 |
|
| 161 |
# the okBtn is created after the typeMenu so that the keyboard traversal |
| 162 |
# is in the right order |
| 163 |
$w->{'okBtn'} = my $okBtn = $f2->Button |
| 164 |
(-text => 'OK', |
| 165 |
-underline => 0, |
| 166 |
-width => 6, |
| 167 |
-default => 'active', |
| 168 |
-pady => 3, |
| 169 |
); |
| 170 |
my $cancelBtn = $f3->Button |
| 171 |
(-text => 'Cancel', |
| 172 |
-underline => 0, |
| 173 |
-width => 6, |
| 174 |
-default => 'normal', |
| 175 |
-pady => 3, |
| 176 |
); |
| 177 |
|
| 178 |
# pack the widgets in f2 and f3 |
| 179 |
$okBtn->pack(-side => 'right', -padx => 4, -anchor => 'e'); |
| 180 |
$f2_lab->pack(-side => 'left', -padx => 4); |
| 181 |
$ent->pack(-expand => 'yes', -fill => 'x', -padx => 2, -pady => 0); |
| 182 |
$cancelBtn->pack(-side => 'right', -padx => 4, -anchor => 'w'); |
| 183 |
$typeMenuLab->pack(-side => 'left', -padx => 4); |
| 184 |
$typeMenuBtn->pack(-expand => 'yes', -fill => 'x', -side => 'right'); |
| 185 |
|
| 186 |
# Pack all the frames together. We are done with widget construction. |
| 187 |
$f1->pack(-side => 'top', -fill => 'x', -pady => 4); |
| 188 |
$f3->pack(-side => 'bottom', -fill => 'x'); |
| 189 |
$f2->pack(-side => 'bottom', -fill => 'x'); |
| 190 |
$icons->pack(-expand => 'yes', -fill => 'both', -padx => 4, -pady => 1); |
| 191 |
|
| 192 |
# Set up the event handlers |
| 193 |
$ent->bind('<Return>',[$w,'ActivateEnt']); |
| 194 |
$upBtn->configure(-command => ['UpDirCmd', $w]); |
| 195 |
$okBtn->configure(-command => ['OkCmd', $w]); |
| 196 |
$cancelBtn->configure(-command, ['CancelCmd', $w]); |
| 197 |
|
| 198 |
$w->bind('<Alt-d>',[$dirMenu,'focus']); |
| 199 |
$w->bind('<Alt-t>',sub { |
| 200 |
if ($typeMenuBtn->cget(-state) eq 'normal') { |
| 201 |
$typeMenuBtn->focus; |
| 202 |
} }); |
| 203 |
$w->bind('<Alt-n>',[$ent,'focus']); |
| 204 |
$w->bind('<KeyPress-Escape>',[$cancelBtn,'invoke']); |
| 205 |
$w->bind('<Alt-c>',[$cancelBtn,'invoke']); |
| 206 |
$w->bind('<Alt-o>',['InvokeBtn','Open']); |
| 207 |
$w->bind('<Alt-s>',['InvokeBtn','Save']); |
| 208 |
$w->protocol('WM_DELETE_WINDOW', ['CancelCmd', $w]); |
| 209 |
$w->OnDestroy(['CancelCmd', $w]); |
| 210 |
|
| 211 |
# Build the focus group for all the entries |
| 212 |
$w->FG_Create; |
| 213 |
$w->FG_BindIn($ent, ['EntFocusIn', $w]); |
| 214 |
$w->FG_BindOut($ent, ['EntFocusOut', $w]); |
| 215 |
|
| 216 |
$w->SetPath(_cwd()); |
| 217 |
|
| 218 |
$w->ConfigSpecs(-defaultextension => ['PASSIVE', undef, undef, undef], |
| 219 |
-filetypes => ['PASSIVE', undef, undef, undef], |
| 220 |
-initialdir => ['PASSIVE', undef, undef, undef], |
| 221 |
-initialfile => ['PASSIVE', undef, undef, undef], |
| 222 |
# -sortcmd => ['PASSIVE', undef, undef, sub { lc($a) cmp lc($b) }], |
| 223 |
-sortcmd => ['PASSIVE', undef, undef, sub { lc($_[0]) cmp lc($_[1]) }], |
| 224 |
-title => ['PASSIVE', undef, undef, undef], |
| 225 |
-type => ['PASSIVE', undef, undef, 'open'], |
| 226 |
-filter => ['PASSIVE', undef, undef, '*'], |
| 227 |
-force => ['PASSIVE', undef, undef, 0], |
| 228 |
-multiple => ['PASSIVE', undef, undef, 0], |
| 229 |
'DEFAULT' => [$icons], |
| 230 |
); |
| 231 |
# So-far-failed attempt to break reference loops ... |
| 232 |
$w->_OnDestroy(qw(dirMenu icons typeMenuLab typeMenuBtn okBtn ent updateId)); |
| 233 |
$w; |
| 234 |
} |
| 235 |
|
| 236 |
# -initialdir fix with ResolveFile |
| 237 |
sub Show { |
| 238 |
my $w = shift; |
| 239 |
|
| 240 |
$w->configure(@_); |
| 241 |
|
| 242 |
# Dialog boxes should be transient with respect to their parent, |
| 243 |
# so that they will always stay on top of their parent window. However, |
| 244 |
# some window managers will create the window as withdrawn if the parent |
| 245 |
# window is withdrawn or iconified. Combined with the grab we put on the |
| 246 |
# window, this can hang the entire application. Therefore we only make |
| 247 |
# the dialog transient if the parent is viewable. |
| 248 |
|
| 249 |
if (Tk::Exists($w->Parent) && $w->Parent->viewable) { |
| 250 |
$w->transient($w->Parent); |
| 251 |
} |
| 252 |
|
| 253 |
# set the default directory and selection according to the -initial |
| 254 |
# settings |
| 255 |
{ |
| 256 |
my $initialdir = $w->cget(-initialdir); |
| 257 |
if (defined $initialdir) { |
| 258 |
my ($flag, $path, $file) = ResolveFile($initialdir, 'junk'); |
| 259 |
if ($flag eq 'OK' or $flag eq 'FILE') { |
| 260 |
$w->{'selectPath'} = $path; |
| 261 |
} else { |
| 262 |
$w->Error("\"$initialdir\" is not a valid directory"); |
| 263 |
} |
| 264 |
} |
| 265 |
$w->{'selectFile'} = $w->cget(-initialfile); |
| 266 |
} |
| 267 |
|
| 268 |
# Set -multiple to a one or zero value (not other boolean types |
| 269 |
# like "yes") so we can use it in tests more easily. |
| 270 |
if ($w->cget('-type') ne 'open') { |
| 271 |
$w->configure(-multiple => 0); |
| 272 |
} else { |
| 273 |
$w->configure(-multiple => !!$w->cget('-multiple')); |
| 274 |
} |
| 275 |
$w->{'icons'}->configure(-multiple => $w->cget('-multiple')); |
| 276 |
|
| 277 |
# Initialize the file types menu |
| 278 |
my $typeMenuBtn = $w->{'typeMenuBtn'}; |
| 279 |
my $typeMenuLab = $w->{'typeMenuLab'}; |
| 280 |
if (defined $w->cget('-filetypes')) { |
| 281 |
my(@filetypes) = GetFileTypes($w->cget('-filetypes')); |
| 282 |
my $typeMenu = $typeMenuBtn->cget(-menu); |
| 283 |
$typeMenu->delete(0, 'end'); |
| 284 |
foreach my $ft (@filetypes) { |
| 285 |
my $title = $ft->[0]; |
| 286 |
my $filter = join(' ', @{ $ft->[1] }); |
| 287 |
$typeMenuBtn->command |
| 288 |
(-label => $title, |
| 289 |
-command => ['SetFilter', $w, $title, $filter], |
| 290 |
); |
| 291 |
} |
| 292 |
$w->SetFilter($filetypes[0]->[0], join(' ', @{ $filetypes[0]->[1] })); |
| 293 |
$typeMenuBtn->configure(-state => 'normal'); |
| 294 |
$typeMenuLab->configure(-state => 'normal'); |
| 295 |
} else { |
| 296 |
#XXX $w->configure(-filter => '*'); |
| 297 |
$typeMenuBtn->configure(-state => 'disabled', |
| 298 |
-takefocus => 0); |
| 299 |
$typeMenuLab->configure(-state => 'disabled'); |
| 300 |
} |
| 301 |
$w->UpdateWhenIdle; |
| 302 |
|
| 303 |
{ |
| 304 |
my $title = $w->cget(-title); |
| 305 |
if (!defined $title) { |
| 306 |
my $type = $w->cget(-type); |
| 307 |
$title = ($type eq 'dir') ? 'Choose Directory' |
| 308 |
: ($type eq 'save') ? 'Save As' : 'Open'; |
| 309 |
} |
| 310 |
$w->title($title); |
| 311 |
} |
| 312 |
|
| 313 |
# Withdraw the window, then update all the geometry information |
| 314 |
# so we know how big it wants to be, then center the window in the |
| 315 |
# display and de-iconify it. |
| 316 |
$w->withdraw; |
| 317 |
$w->idletasks; |
| 318 |
if (0) |
| 319 |
{ |
| 320 |
#XXX use Tk::Wm::Popup? or Tk::PlaceWindow? |
| 321 |
my $x = int($w->screenwidth / 2 - $w->reqwidth / 2 - $w->parent->vrootx); |
| 322 |
my $y = int($w->screenheight / 2 - $w->reqheight / 2 - $w->parent->vrooty); |
| 323 |
$w->geometry("+$x+$y"); |
| 324 |
$w->deiconify; |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
$w->Popup; |
| 329 |
} |
| 330 |
|
| 331 |
# Set a grab and claim the focus too. |
| 332 |
#XXX use Tk::setFocusGrab when it's available |
| 333 |
my $oldFocus = $w->focusCurrent; |
| 334 |
my $oldGrab = $w->grabCurrent; |
| 335 |
my $grabStatus; $grabStatus = $oldGrab->grabStatus if ($oldGrab); |
| 336 |
$w->grab; |
| 337 |
my $ent = $w->{'ent'}; |
| 338 |
$ent->focus; |
| 339 |
$ent->delete(0, 'end'); |
| 340 |
if (defined $w->{'selectFile'} && $w->{'selectFile'} ne '') { |
| 341 |
$ent->insert(0, $w->{'selectFile'}); |
| 342 |
$ent->selectionRange(0,'end'); |
| 343 |
$ent->icursor('end'); |
| 344 |
} |
| 345 |
|
| 346 |
# 8. Wait for the user to respond, then restore the focus and |
| 347 |
# return the index of the selected button. Restore the focus |
| 348 |
# before deleting the window, since otherwise the window manager |
| 349 |
# may take the focus away so we can't redirect it. Finally, |
| 350 |
# restore any grab that was in effect. |
| 351 |
$w->waitVariable(\$w->{'selectFilePath'}); |
| 352 |
eval { |
| 353 |
$oldFocus->focus if $oldFocus; |
| 354 |
}; |
| 355 |
if (Tk::Exists($w)) { # widget still exists |
| 356 |
$w->grabRelease; |
| 357 |
$w->withdraw; |
| 358 |
} |
| 359 |
if (Tk::Exists($oldGrab) && $oldGrab->viewable) { |
| 360 |
if ($grabStatus eq 'global') { |
| 361 |
$oldGrab->grabGlobal; |
| 362 |
} else { |
| 363 |
$oldGrab->grab; |
| 364 |
} |
| 365 |
} |
| 366 |
return $w->{'selectFilePath'}; |
| 367 |
} |
| 368 |
|
| 369 |
# tkFDialog_UpdateWhenIdle -- |
| 370 |
# |
| 371 |
# Creates an idle event handler which updates the dialog in idle |
| 372 |
# time. This is important because loading the directory may take a long |
| 373 |
# time and we don't want to load the same directory for multiple times |
| 374 |
# due to multiple concurrent events. |
| 375 |
# |
| 376 |
sub UpdateWhenIdle { |
| 377 |
my $w = shift; |
| 378 |
if (exists $w->{'updateId'}) { |
| 379 |
return; |
| 380 |
} else { |
| 381 |
$w->{'updateId'} = $w->after('idle', [$w, 'Update']); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
# tkFDialog_Update -- |
| 386 |
# |
| 387 |
# Loads the files and directories into the IconList widget. Also |
| 388 |
# sets up the directory option menu for quick access to parent |
| 389 |
# directories. |
| 390 |
# |
| 391 |
sub Update { |
| 392 |
my $w = shift; |
| 393 |
my $dataName = $w->name; |
| 394 |
|
| 395 |
# This proc may be called within an idle handler. Make sure that the |
| 396 |
# window has not been destroyed before this proc is called |
| 397 |
if (!Tk::Exists($w) || $w->class ne 'FBox') { |
| 398 |
return; |
| 399 |
} else { |
| 400 |
delete $w->{'updateId'}; |
| 401 |
} |
| 402 |
unless (defined $folderImage->{$w->MainWindow}) { |
| 403 |
require Tk::Pixmap; |
| 404 |
$folderImage->{$w->MainWindow} = $w->Pixmap(-file => Tk->findINC('folder.xpm')); |
| 405 |
$fileImage->{$w->MainWindow} = $w->Pixmap(-file => Tk->findINC('file.xpm')); |
| 406 |
} |
| 407 |
my $folder = $folderImage->{$w->MainWindow}; |
| 408 |
my $file = $fileImage->{$w->MainWindow}; |
| 409 |
my $appPWD = _cwd(); |
| 410 |
if (!ext_chdir($w->_get_select_path)) { |
| 411 |
# We cannot change directory to $data(selectPath). $data(selectPath) |
| 412 |
# should have been checked before tkFDialog_Update is called, so |
| 413 |
# we normally won't come to here. Anyways, give an error and abort |
| 414 |
# action. |
| 415 |
$w->messageBox(-type => 'OK', |
| 416 |
-message => 'Cannot change to the directory "' . |
| 417 |
$w->_get_select_path . "\".\nPermission denied.", |
| 418 |
-icon => 'warning', |
| 419 |
); |
| 420 |
ext_chdir($appPWD); |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
# Turn on the busy cursor. BUG?? We haven't disabled X events, though, |
| 425 |
# so the user may still click and cause havoc ... |
| 426 |
my $ent = $w->{'ent'}; |
| 427 |
my $entCursor = $ent->cget(-cursor); |
| 428 |
my $dlgCursor = $w->cget(-cursor); |
| 429 |
$ent->configure(-cursor => 'watch'); |
| 430 |
$w->configure(-cursor => 'watch'); |
| 431 |
$w->idletasks; |
| 432 |
my $icons = $w->{'icons'}; |
| 433 |
$icons->DeleteAll; |
| 434 |
|
| 435 |
# Make the dir & file list |
| 436 |
my $cwd = _cwd(); |
| 437 |
local *FDIR; |
| 438 |
if (opendir(FDIR, $cwd)) { |
| 439 |
my @files; |
| 440 |
# my $sortcmd = $w->cget(-sortcmd); |
| 441 |
my $sortcmd = sub { $w->cget(-sortcmd)->($a,$b) }; |
| 442 |
my $flt = $w->cget(-filter); |
| 443 |
my $fltcb; |
| 444 |
if (ref $flt eq 'CODE') { |
| 445 |
$fltcb = $flt; |
| 446 |
} else { |
| 447 |
$flt = _rx_to_glob($flt); |
| 448 |
} |
| 449 |
my $type_dir = $w->cget(-type) eq 'dir'; |
| 450 |
foreach my $f (sort $sortcmd readdir(FDIR)) { |
| 451 |
next if $f eq '.' or $f eq '..'; |
| 452 |
next if $type_dir && ! -d "$cwd/$f"; # XXX use File::Spec? |
| 453 |
if ($fltcb) { |
| 454 |
next if !$fltcb->($w, $f, $cwd); |
| 455 |
} else { |
| 456 |
next if !-d $f && $f !~ m!$flt!s; |
| 457 |
} |
| 458 |
if (-d $f) { |
| 459 |
$icons->Add($folder, $f); |
| 460 |
} else { |
| 461 |
push @files, $f; |
| 462 |
} |
| 463 |
} |
| 464 |
closedir(FDIR); |
| 465 |
$icons->Add($file, @files); |
| 466 |
} |
| 467 |
|
| 468 |
$icons->Arrange; |
| 469 |
|
| 470 |
# Update the Directory: option menu |
| 471 |
my @list; |
| 472 |
my $dir = ''; |
| 473 |
foreach my $subdir (TclFileSplit($w->_get_select_path)) { |
| 474 |
$dir = TclFileJoin($dir, $subdir); |
| 475 |
push @list, $dir; |
| 476 |
} |
| 477 |
my $dirMenu = $w->{'dirMenu'}; |
| 478 |
$dirMenu->configure(-options => \@list); |
| 479 |
|
| 480 |
# Restore the PWD to the application's PWD |
| 481 |
ext_chdir($appPWD); |
| 482 |
|
| 483 |
# Restore the Save label |
| 484 |
if ($w->cget(-type) eq 'save') { |
| 485 |
$w->{'okBtn'}->configure(-text => 'Save'); |
| 486 |
} |
| 487 |
|
| 488 |
# turn off the busy cursor. |
| 489 |
$ent->configure(-cursor => $entCursor); |
| 490 |
$w->configure(-cursor => $dlgCursor); |
| 491 |
} |
| 492 |
|
| 493 |
# tkFDialog_SetPathSilently -- |
| 494 |
# |
| 495 |
# Sets data(selectPath) without invoking the trace procedure |
| 496 |
# |
| 497 |
sub SetPathSilently { |
| 498 |
my($w, $path) = @_; |
| 499 |
|
| 500 |
$w->{'selectPath'} = $path; |
| 501 |
} |
| 502 |
|
| 503 |
# This proc gets called whenever data(selectPath) is set |
| 504 |
# |
| 505 |
sub SetPath { |
| 506 |
my $w = shift; |
| 507 |
$w->{'selectPath'} = $_[0] if @_; |
| 508 |
$w->UpdateWhenIdle; |
| 509 |
} |
| 510 |
|
| 511 |
# This proc gets called whenever data(filter) is set |
| 512 |
# |
| 513 |
#XXX here's much more code in the tcl version ... check it out |
| 514 |
sub SetFilter { |
| 515 |
my($w, $title, $filter) = @_; |
| 516 |
$w->configure(-filter => $filter); |
| 517 |
$w->{'typeMenuBtn'}->configure(-text => $title, |
| 518 |
-indicatoron => 1); |
| 519 |
$w->{'icons'}->Subwidget('sbar')->set(0.0, 0.0); |
| 520 |
$w->UpdateWhenIdle; |
| 521 |
} |
| 522 |
|
| 523 |
# tkFDialogResolveFile -- |
| 524 |
# |
| 525 |
# Interpret the user's text input in a file selection dialog. |
| 526 |
# Performs: |
| 527 |
# |
| 528 |
# (1) ~ substitution |
| 529 |
# (2) resolve all instances of . and .. |
| 530 |
# (3) check for non-existent files/directories |
| 531 |
# (4) check for chdir permissions |
| 532 |
# |
| 533 |
# Arguments: |
| 534 |
# context: the current directory you are in |
| 535 |
# text: the text entered by the user |
| 536 |
# defaultext: the default extension to add to files with no extension |
| 537 |
# |
| 538 |
# Return value: |
| 539 |
# [list $flag $directory $file] |
| 540 |
# |
| 541 |
# flag = OK : valid input |
| 542 |
# = PATTERN : valid directory/pattern |
| 543 |
# = PATH : the directory does not exist |
| 544 |
# = FILE : the directory exists but the file doesn't |
| 545 |
# exist |
| 546 |
# = CHDIR : Cannot change to the directory |
| 547 |
# = ERROR : Invalid entry |
| 548 |
# |
| 549 |
# directory : valid only if flag = OK or PATTERN or FILE |
| 550 |
# file : valid only if flag = OK or PATTERN |
| 551 |
# |
| 552 |
# directory may not be the same as context, because text may contain |
| 553 |
# a subdirectory name |
| 554 |
# |
| 555 |
sub ResolveFile { |
| 556 |
my($context, $text, $defaultext) = @_; |
| 557 |
my $appPWD = _cwd(); |
| 558 |
my $path = JoinFile($context, $text); |
| 559 |
# If the file has no extension, append the default. Be careful not |
| 560 |
# to do this for directories, otherwise typing a dirname in the box |
| 561 |
# will give back "dirname.extension" instead of trying to change dir. |
| 562 |
if (!-d $path && $text !~ /\..+$/s && defined $defaultext) { |
| 563 |
$path = "$path$defaultext"; |
| 564 |
} |
| 565 |
# Cannot just test for existance here as non-existing files are |
| 566 |
# not an error for getSaveFile type dialogs. |
| 567 |
# return ('ERROR', $path, "") if (!-e $path); |
| 568 |
my($directory, $file, $flag); |
| 569 |
if (-e $path) { |
| 570 |
if (-d $path) { |
| 571 |
if (!ext_chdir($path)) { |
| 572 |
return ('CHDIR', $path, ''); |
| 573 |
} |
| 574 |
$directory = _cwd(); |
| 575 |
$file = ''; |
| 576 |
$flag = 'OK'; |
| 577 |
ext_chdir($appPWD); |
| 578 |
} else { |
| 579 |
my $dirname = File::Basename::dirname($path); |
| 580 |
if (!ext_chdir($dirname)) { |
| 581 |
return ('CHDIR', $dirname, ''); |
| 582 |
} |
| 583 |
$directory = _cwd(); |
| 584 |
$file = File::Basename::basename($path); |
| 585 |
$flag = 'OK'; |
| 586 |
ext_chdir($appPWD); |
| 587 |
} |
| 588 |
} else { |
| 589 |
my $dirname = File::Basename::dirname($path); |
| 590 |
if (-e $dirname) { |
| 591 |
if (!ext_chdir($dirname)) { |
| 592 |
return ('CHDIR', $dirname, ''); |
| 593 |
} |
| 594 |
$directory = _cwd(); |
| 595 |
$file = File::Basename::basename($path); |
| 596 |
if ($file =~ /[*?]/) { |
| 597 |
$flag = 'PATTERN'; |
| 598 |
} else { |
| 599 |
$flag = 'FILE'; |
| 600 |
} |
| 601 |
ext_chdir($appPWD); |
| 602 |
} else { |
| 603 |
$directory = $dirname; |
| 604 |
$file = File::Basename::basename($path); |
| 605 |
$flag = 'PATH'; |
| 606 |
} |
| 607 |
} |
| 608 |
return ($flag,$directory,$file); |
| 609 |
} |
| 610 |
|
| 611 |
# Gets called when the entry box gets keyboard focus. We clear the selection |
| 612 |
# from the icon list . This way the user can be certain that the input in the |
| 613 |
# entry box is the selection. |
| 614 |
# |
| 615 |
sub EntFocusIn { |
| 616 |
my $w = shift; |
| 617 |
my $ent = $w->{'ent'}; |
| 618 |
if ($ent->get ne '') { |
| 619 |
$ent->selectionRange(0, 'end'); |
| 620 |
$ent->icursor('end'); |
| 621 |
} else { |
| 622 |
$ent->selectionClear; |
| 623 |
} |
| 624 |
#XXX is this missing in the tcl version, too??? $w->{'icons'}->Selection('clear'); |
| 625 |
my $okBtn = $w->{'okBtn'}; |
| 626 |
if ($w->cget(-type) ne 'save') { |
| 627 |
$okBtn->configure(-text => 'Open'); |
| 628 |
} else { |
| 629 |
$okBtn->configure(-text => 'Save'); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
sub EntFocusOut { |
| 634 |
my $w = shift; |
| 635 |
$w->{'ent'}->selectionClear; |
| 636 |
} |
| 637 |
|
| 638 |
# Gets called when user presses Return in the "File name" entry. |
| 639 |
# |
| 640 |
sub ActivateEnt { |
| 641 |
my $w = shift; |
| 642 |
if ($w->cget(-multiple)) { |
| 643 |
# For the multiple case we have to be careful to get the file |
| 644 |
# names as a true list, watching out for a single file with a |
| 645 |
# space in the name. Thus we query the IconList directly. |
| 646 |
|
| 647 |
$w->{'selectFile'} = []; |
| 648 |
for my $item ($w->{'icons'}->Curselection) { |
| 649 |
$w->VerifyFileName($w->_get_from_icons($item)); |
| 650 |
} |
| 651 |
} else { |
| 652 |
my $ent = $w->{'ent'}; |
| 653 |
my $text = $w->_encode_filename($ent->get); |
| 654 |
$w->VerifyFileName($text); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
# Verification procedure |
| 659 |
# |
| 660 |
sub VerifyFileName { |
| 661 |
my($w, $text) = @_; |
| 662 |
#XXX leave this here? |
| 663 |
# $text =~ s/^\s+//; |
| 664 |
# $text =~ s/\s+$//; |
| 665 |
my($flag, $path, $file) = ResolveFile($w->_get_select_path, $text, |
| 666 |
$w->cget(-defaultextension)); |
| 667 |
my $ent = $w->{'ent'}; |
| 668 |
if ($flag eq 'OK') { |
| 669 |
if ($file eq '') { |
| 670 |
# user has entered an existing (sub)directory |
| 671 |
$w->SetPath($path); |
| 672 |
$ent->delete(0, 'end'); |
| 673 |
} else { |
| 674 |
$w->SetPathSilently($path); |
| 675 |
if ($w->cget(-multiple)) { |
| 676 |
push @{ $w->{'selectFile'} }, $file; |
| 677 |
} else { |
| 678 |
$w->{'selectFile'} = $file; |
| 679 |
} |
| 680 |
$w->Done; |
| 681 |
} |
| 682 |
} elsif ($flag eq 'PATTERN') { |
| 683 |
$w->SetPath($path); |
| 684 |
$w->configure(-filter => $file); |
| 685 |
} elsif ($flag eq 'FILE') { |
| 686 |
if ($w->cget(-type) eq 'open') { |
| 687 |
$w->messageBox(-icon => 'warning', |
| 688 |
-type => 'OK', |
| 689 |
-message => 'File "' . TclFileJoin($path, $file) |
| 690 |
. '" does not exist.'); |
| 691 |
$ent->selectionRange(0, 'end'); |
| 692 |
$ent->icursor('end'); |
| 693 |
} elsif ($w->cget(-type) eq 'save') { |
| 694 |
$w->SetPathSilently($path); |
| 695 |
if ($w->cget(-multiple)) { |
| 696 |
push @{ $w->{'selectFile'} }, $file; |
| 697 |
} else { |
| 698 |
$w->{'selectFile'} = $file; |
| 699 |
} |
| 700 |
$w->Done; |
| 701 |
} |
| 702 |
} elsif ($flag eq 'PATH') { |
| 703 |
$w->messageBox(-icon => 'warning', |
| 704 |
-type => 'OK', |
| 705 |
-message => "Directory \'$path\' does not exist."); |
| 706 |
$ent->selectionRange(0, 'end'); |
| 707 |
$ent->icursor('end'); |
| 708 |
} elsif ($flag eq 'CHDIR') { |
| 709 |
$w->messageBox(-type => 'OK', |
| 710 |
-message => "Cannot change to the directory \"$path\".\nPermission denied.", |
| 711 |
-icon => 'warning'); |
| 712 |
$ent->selectionRange(0, 'end'); |
| 713 |
$ent->icursor('end'); |
| 714 |
} elsif ($flag eq 'ERROR') { |
| 715 |
$w->messageBox(-type => 'OK', |
| 716 |
-message => "Invalid file name \"$path\".", |
| 717 |
-icon => 'warning'); |
| 718 |
$ent->selectionRange(0, 'end'); |
| 719 |
$ent->icursor('end'); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
# Gets called when user presses the Alt-s or Alt-o keys. |
| 724 |
# |
| 725 |
sub InvokeBtn { |
| 726 |
my($w, $key) = @_; |
| 727 |
my $okBtn = $w->{'okBtn'}; |
| 728 |
$okBtn->invoke if ($okBtn->cget(-text) eq $key); |
| 729 |
} |
| 730 |
|
| 731 |
# Gets called when user presses the "parent directory" button |
| 732 |
# |
| 733 |
sub UpDirCmd { |
| 734 |
my $w = shift; |
| 735 |
$w->SetPath(File::Basename::dirname($w->_get_select_path)) |
| 736 |
unless ($w->_get_select_path eq '/'); |
| 737 |
} |
| 738 |
|
| 739 |
# Join a file name to a path name. The "file join" command will break |
| 740 |
# if the filename begins with ~ |
| 741 |
sub JoinFile { |
| 742 |
my($path, $file) = @_; |
| 743 |
if ($file =~ /^~/ && -e "$path/$file") { |
| 744 |
TclFileJoin($path, "./$file"); |
| 745 |
} else { |
| 746 |
TclFileJoin($path, $file); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
# XXX replace with File::Spec if possible |
| 751 |
sub TclFileJoin { |
| 752 |
my $path = ''; |
| 753 |
foreach (@_) { |
| 754 |
if (m|^/|) { |
| 755 |
$path = $_; |
| 756 |
} elsif (m|^[a-z]:/|i) { # DOS-ish |
| 757 |
$path = $_; |
| 758 |
} elsif ($_ eq '~') { |
| 759 |
$path = _get_homedir(); |
| 760 |
} elsif (m|^~/(.*)|) { |
| 761 |
$path = _get_homedir() . "/" . $1; |
| 762 |
} elsif (m|^~([^/]+)(.*)|) { |
| 763 |
my($user, $p) = ($1, $2); |
| 764 |
my $dir = _get_homedir($user); |
| 765 |
if (!defined $dir) { |
| 766 |
$path = "~$user$p"; |
| 767 |
} else { |
| 768 |
$path = $dir . $p; |
| 769 |
} |
| 770 |
} elsif ($path eq '/' or $path eq '') { |
| 771 |
$path .= $_; |
| 772 |
} else { |
| 773 |
$path .= "/$_"; |
| 774 |
} |
| 775 |
} |
| 776 |
$path; |
| 777 |
} |
| 778 |
|
| 779 |
sub TclFileSplit { |
| 780 |
my $path = shift; |
| 781 |
my @comp; |
| 782 |
$path =~ s|/+|/|g; # strip multiple slashes |
| 783 |
if ($path =~ m|^/|) { |
| 784 |
push @comp, '/'; |
| 785 |
$path = substr($path, 1); |
| 786 |
} |
| 787 |
push @comp, split /\//, $path; |
| 788 |
@comp; |
| 789 |
} |
| 790 |
|
| 791 |
# Gets called when user presses the "OK" button |
| 792 |
# |
| 793 |
sub OkCmd { |
| 794 |
my $w = shift; |
| 795 |
my $from = shift || "button"; |
| 796 |
|
| 797 |
my $filenames = []; |
| 798 |
for my $item ($w->{'icons'}->Curselection) { |
| 799 |
push @$filenames, $w->_get_from_icons($item); |
| 800 |
} |
| 801 |
|
| 802 |
my $filename = $filenames->[0]; |
| 803 |
$filename = "" if !defined $filename; |
| 804 |
if ($w->cget('-type') eq 'dir' && $from ne "iconlist") { |
| 805 |
my $file = $filename eq '' ? $w->_get_select_path : JoinFile($w->_get_select_path, $filename); |
| 806 |
$w->Done($file); |
| 807 |
} elsif ((@$filenames && !$w->cget('-multiple')) || |
| 808 |
($w->cget('-multiple') && @$filenames == 1)) { |
| 809 |
my $file = JoinFile($w->_get_select_path, $filename); |
| 810 |
if (-d $file) { |
| 811 |
$w->ListInvoke($filename); |
| 812 |
return; |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
$w->ActivateEnt; |
| 817 |
} |
| 818 |
|
| 819 |
# Gets called when user presses the "Cancel" button |
| 820 |
# |
| 821 |
sub CancelCmd { |
| 822 |
my $w = shift; |
| 823 |
undef $w->{'selectFilePath'}; |
| 824 |
} |
| 825 |
|
| 826 |
# Gets called when user browses the IconList widget (dragging mouse, arrow |
| 827 |
# keys, etc) |
| 828 |
# |
| 829 |
sub ListBrowse { |
| 830 |
my($w) = @_; |
| 831 |
|
| 832 |
my $text = []; |
| 833 |
for my $item ($w->{'icons'}->Curselection) { |
| 834 |
push @$text, $w->_get_from_icons($item); |
| 835 |
} |
| 836 |
return if @$text == 0; |
| 837 |
my $isDir; |
| 838 |
if (@$text > 1) { |
| 839 |
my $newtext = []; |
| 840 |
for my $file (@$text) { |
| 841 |
my $fullfile = JoinFile($w->_get_select_path, $file); |
| 842 |
if (!-d $fullfile) { |
| 843 |
push @$newtext, $file; |
| 844 |
} |
| 845 |
} |
| 846 |
$text = $newtext; |
| 847 |
$isDir = 0; |
| 848 |
} else { |
| 849 |
my $file = JoinFile($w->_get_select_path, $text->[0]); |
| 850 |
$isDir = -d $file; |
| 851 |
} |
| 852 |
my $ent = $w->{'ent'}; |
| 853 |
my $okBtn = $w->{'okBtn'}; |
| 854 |
if (!$isDir) { |
| 855 |
$ent->delete(qw(0 end)); |
| 856 |
$ent->insert(0, "@$text"); # XXX quote! |
| 857 |
|
| 858 |
if ($w->cget('-type') ne 'save') { |
| 859 |
$okBtn->configure(-text => 'Open'); |
| 860 |
} else { |
| 861 |
$okBtn->configure(-text => 'Save'); |
| 862 |
} |
| 863 |
} else { |
| 864 |
$okBtn->configure(-text => 'Open'); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
# Gets called when user invokes the IconList widget (double-click, |
| 869 |
# Return key, etc) |
| 870 |
# |
| 871 |
sub ListInvoke { |
| 872 |
my($w, @filenames) = @_; |
| 873 |
return if !@filenames; |
| 874 |
my $file = JoinFile($w->_get_select_path, $filenames[0]); |
| 875 |
if (-d $file) { |
| 876 |
my $appPWD = _cwd(); |
| 877 |
if (!ext_chdir($file)) { |
| 878 |
$w->messageBox(-type => 'OK', |
| 879 |
-message => "Cannot change to the directory \"$file\".\nPermission denied.", |
| 880 |
-icon => 'warning'); |
| 881 |
} else { |
| 882 |
ext_chdir($appPWD); |
| 883 |
$w->SetPath($file); |
| 884 |
} |
| 885 |
} else { |
| 886 |
if ($w->cget('-multiple')) { |
| 887 |
$w->{'selectFile'} = [@filenames]; |
| 888 |
} else { |
| 889 |
$w->{'selectFile'} = $file; |
| 890 |
} |
| 891 |
$w->Done; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
# tkFDialog_Done -- |
| 896 |
# |
| 897 |
# Gets called when user has input a valid filename. Pops up a |
| 898 |
# dialog box to confirm selection when necessary. Sets the |
| 899 |
# tkPriv(selectFilePath) variable, which will break the "tkwait" |
| 900 |
# loop in tkFDialog and return the selected filename to the |
| 901 |
# script that calls tk_getOpenFile or tk_getSaveFile |
| 902 |
# |
| 903 |
sub Done { |
| 904 |
my $w = shift; |
| 905 |
my $selectFilePath = (@_) ? shift : ''; |
| 906 |
if ($selectFilePath eq '') { |
| 907 |
if ($w->cget('-multiple')) { |
| 908 |
$selectFilePath = []; |
| 909 |
for my $f (@{ $w->{'selectFile'} }) { |
| 910 |
push @$selectFilePath, JoinFile($w->_get_select_path, $f); |
| 911 |
} |
| 912 |
} else { |
| 913 |
$selectFilePath = JoinFile($w->_get_select_path, |
| 914 |
$w->{'selectFile'}); |
| 915 |
} |
| 916 |
if ($w->cget(-type) eq 'save' and |
| 917 |
-e $selectFilePath and |
| 918 |
!$w->cget(-force)) { |
| 919 |
my $reply = $w->messageBox |
| 920 |
(-icon => 'warning', |
| 921 |
-type => 'YesNo', |
| 922 |
-message => "File \"$selectFilePath\" already exists.\nDo you want to overwrite it?"); |
| 923 |
return unless (lc($reply) eq 'yes'); |
| 924 |
} |
| 925 |
} |
| 926 |
$w->{'selectFilePath'} = ($selectFilePath ne '' ? $selectFilePath : undef); |
| 927 |
} |
| 928 |
|
| 929 |
sub FDialog { |
| 930 |
my $cmd = shift; |
| 931 |
if ($cmd =~ /Save/) { |
| 932 |
push @_, -type => 'save'; |
| 933 |
} elsif ($cmd =~ /Directory/) { |
| 934 |
push @_, -type => 'dir'; |
| 935 |
} |
| 936 |
Tk::DialogWrapper('FBox', $cmd, @_); |
| 937 |
} |
| 938 |
|
| 939 |
# tkFDGetFileTypes -- |
| 940 |
# |
| 941 |
# Process the string given by the -filetypes option of the file |
| 942 |
# dialogs. Similar to the C function TkGetFileFilters() on the Mac |
| 943 |
# and Windows platform. |
| 944 |
# |
| 945 |
sub GetFileTypes { |
| 946 |
my $in = shift; |
| 947 |
my %fileTypes; |
| 948 |
foreach my $t (@$in) { |
| 949 |
if (@$t < 2 || @$t > 3) { |
| 950 |
require Carp; |
| 951 |
Carp::croak("bad file type \"$t\", should be \"typeName [extension ?extensions ...?] ?[macType ?macTypes ...?]?\""); |
| 952 |
} |
| 953 |
push @{ $fileTypes{$t->[0]} }, (ref $t->[1] eq 'ARRAY' |
| 954 |
? @{ $t->[1] } |
| 955 |
: $t->[1]); |
| 956 |
} |
| 957 |
|
| 958 |
my @types; |
| 959 |
my %hasDoneType; |
| 960 |
my %hasGotExt; |
| 961 |
foreach my $t (@$in) { |
| 962 |
my $label = $t->[0]; |
| 963 |
my @exts; |
| 964 |
|
| 965 |
next if (exists $hasDoneType{$label}); |
| 966 |
|
| 967 |
my $name = "$label ("; |
| 968 |
my $sep = ''; |
| 969 |
foreach my $ext (@{ $fileTypes{$label} }) { |
| 970 |
next if ($ext eq ''); |
| 971 |
$ext =~ s/^\./*./; |
| 972 |
if (!exists $hasGotExt{$label}->{$ext}) { |
| 973 |
$name .= "$sep$ext"; |
| 974 |
push @exts, $ext; |
| 975 |
$hasGotExt{$label}->{$ext}++; |
| 976 |
} |
| 977 |
$sep = ','; |
| 978 |
} |
| 979 |
$name .= ')'; |
| 980 |
push @types, [$name, \@exts]; |
| 981 |
|
| 982 |
$hasDoneType{$label}++; |
| 983 |
} |
| 984 |
|
| 985 |
return @types; |
| 986 |
} |
| 987 |
|
| 988 |
# ext_chdir -- |
| 989 |
# |
| 990 |
# Change directory with tilde substitution |
| 991 |
# |
| 992 |
sub ext_chdir { |
| 993 |
my $dir = shift; |
| 994 |
if ($dir eq '~') { |
| 995 |
chdir _get_homedir(); |
| 996 |
} elsif ($dir =~ m|^~/(.*)|s) { |
| 997 |
chdir _get_homedir() . "/" . $1; |
| 998 |
} elsif ($dir =~ m|^~([^/]+(.*))|s) { |
| 999 |
chdir _get_homedir($1) . $2; |
| 1000 |
} else { |
| 1001 |
chdir $dir; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
# _get_homedir -- |
| 1006 |
# |
| 1007 |
# Get home directory of the current user |
| 1008 |
# |
| 1009 |
sub _get_homedir { |
| 1010 |
my($user) = @_; |
| 1011 |
if (!defined $user) { |
| 1012 |
eval { |
| 1013 |
local $SIG{__DIE__}; |
| 1014 |
(getpwuid($<))[7]; |
| 1015 |
} || $ENV{HOME} || undef; # chdir undef changes to home directory, too |
| 1016 |
} else { |
| 1017 |
eval { |
| 1018 |
local $SIG{__DIE__}; |
| 1019 |
(getpwnam($user))[7]; |
| 1020 |
}; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
sub _cwd { |
| 1025 |
#Cwd::cwd(); |
| 1026 |
Cwd::fastcwd(); # this is taint-safe |
| 1027 |
} |
| 1028 |
|
| 1029 |
sub _untaint { |
| 1030 |
my $s = shift; |
| 1031 |
$s =~ /^(.*)$/; |
| 1032 |
$1; |
| 1033 |
} |
| 1034 |
|
| 1035 |
sub _rx_to_glob { |
| 1036 |
my $arg = shift; |
| 1037 |
$arg =~ s!([.+^()|\${}\[\]\\])!\\$1!g; |
| 1038 |
$arg = join('|', map {"^$_\\z"} split(' ', $arg)); |
| 1039 |
$arg =~ s!\*!.*!g;$arg =~ s!\?!.!g; |
| 1040 |
$arg = qr/$arg/s; |
| 1041 |
$arg; |
| 1042 |
} |
| 1043 |
|
| 1044 |
sub _get_from_icons { |
| 1045 |
my($w, $item) = @_; |
| 1046 |
$w->_encode_filename($w->{'icons'}->Get($item)); |
| 1047 |
} |
| 1048 |
|
| 1049 |
sub _get_select_path { |
| 1050 |
my($w) = @_; |
| 1051 |
$w->_encode_filename($w->{'selectPath'}); |
| 1052 |
} |
| 1053 |
|
| 1054 |
sub _encode_filename { |
| 1055 |
my($w, $filename) = @_; |
| 1056 |
$filename = $w->{encoding}->encode($filename); |
| 1057 |
$filename; |
| 1058 |
} |
| 1059 |
|
| 1060 |
1; |
| 1061 |
|