| 1 | package Tk::Wizard::FileSystem; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings::register; | ||
| 6 | |||
| 7 | our $VERSION = 2.78; | ||
| 8 | |||
| 9 | use Carp (); | ||
| 10 | use Tk::LabFrame; | ||
| 11 | use Tk::DirTree; | ||
| 12 | use Tk::Wizard::Image; | ||
| 13 | use File::Spec::Functions qw( rootdir ); | ||
| 14 | |||
| 15 | my $WINDOZE = ($^O =~ m/MSWin32/i); | ||
| 16 | my $dir_term = $WINDOZE ? 'folder' : 'directory'; | ||
| 17 | my $dir_term_ucf = ucfirst $dir_term; | ||
| 18 | |||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Tk::Wizard::FileSystem - C<Tk::Wizard> pages to allow end-user filesystem access | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | Currently automatically loaded by C<Tk::Wizard>, though this | ||
| 27 | behaviour is deprecated and is expected to change in 2008. | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Adds a number of methods to C<Tk::Wizard>, to allow the end-user to access | ||
| 32 | the filesystem. | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 addDirSelectPage | ||
| 37 | |||
| 38 | $wizard->addDirSelectPage ( -variable => \$chosen_dir ) | ||
| 39 | |||
| 40 | Adds a page (C<Tk::Frame>) that contains a scrollable tree list of all | ||
| 41 | directories including, on Win32, logical drives. | ||
| 42 | |||
| 43 | Supply in C<-variable> a reference to a variable to set the initial | ||
| 44 | directory, and to have set with the chosen path. | ||
| 45 | |||
| 46 | Supply C<-nowarnings> with a value of C<1> to list only drives which are | ||
| 47 | accessible, thus avoiding C<Tk::DirTree> warnings on Win32 where removable | ||
| 48 | drives have no media. | ||
| 49 | |||
| 50 | Supply in C<-nowarnings> a value other than C<1> to avoid listing drives | ||
| 51 | which are both inaccessible and - on Win32 - are | ||
| 52 | either fixed drives, network drives, or RAM drives (that is types 3, 4, and | ||
| 53 | 6, according to L<Win32API::File/GetDriveType>). | ||
| 54 | |||
| 55 | You may also specify the C<-title>, C<-subtitle> and C<-text> parameters, as | ||
| 56 | in L</blank_frame>. | ||
| 57 | |||
| 58 | An optional C<-background> argument is used as the background of the Entry and DirTree widgets | ||
| 59 | (default is white). | ||
| 60 | |||
| 61 | Also see L</callback_dirSelect>. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub Tk::Wizard::addDirSelectPage { | ||
| 66 | my $self = shift; | ||
| 67 | my $args = {@_}; | ||
| 68 | # $self->addPage( sub { $self->_page_dirSelect($args) } ); | ||
| 69 | my %btn_args = | ||
| 70 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 71 | grep { /ButtonAction$/ } | ||
| 72 | keys %$args; | ||
| 73 | return $self->addPage( sub { $self->_page_dirSelect($args) }, %btn_args ); | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | # PRIVATE METHOD _page_dirSelect | ||
| 78 | # | ||
| 79 | # It'd be nice to use FBox here, but it doesn't seem to support dir selection | ||
| 80 | # and DirSelect is broken and ugly | ||
| 81 | # | ||
| 82 | # As blank_frame plus: | ||
| 83 | # -variable => Reference to a variable to set. | ||
| 84 | # -nowarnings => 1 : chdir to each drive first and only list if accessible | ||
| 85 | # => !1: as 1, plus on types 3,4 and 6. | ||
| 86 | sub Tk::Wizard::_page_dirSelect { | ||
| 87 | my $self = shift; | ||
| 88 | my $args = shift; | ||
| 89 | |||
| 90 | if ( not $args->{-variable} ) { | ||
| 91 | Carp::croak "You must supply a -variable parameter"; | ||
| 92 | } | ||
| 93 | |||
| 94 | elsif ( not ref $args->{-variable} ) { | ||
| 95 | Carp::croak "The -variable parameter must be a reference"; | ||
| 96 | } | ||
| 97 | |||
| 98 | ${ $args->{-variable} } ||= ''; | ||
| 99 | |||
| 100 | # The DirTree can take a long time to read all the disk drives when | ||
| 101 | # populating itself: | ||
| 102 | $self->Busy; | ||
| 103 | my $_drives = sub { | ||
| 104 | return '/' if not $WINDOZE; | ||
| 105 | eval('require Win32API::File'); | ||
| 106 | return Win32API::File::getLogicalDrives(); | ||
| 107 | }; | ||
| 108 | |||
| 109 | my ( $frame, @pl ) = $self->blank_frame( | ||
| 110 | -title => $args->{-title} || "Please choose a $dir_term", | ||
| 111 | -subtitle => $args->{-subtitle} || "After you have made your choice, press Next to continue.", | ||
| 112 | -text => $args->{-text} || "", | ||
| 113 | -wait => $args->{ -wait }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | # DEBUG_FRAME && $frame->configure( -background => 'light blue' ); | ||
| 117 | |||
| 118 | my $entry = $frame->Entry( | ||
| 119 | -justify => 'left', | ||
| 120 | -font => 'FIXED', | ||
| 121 | -textvariable => $args->{-variable}, | ||
| 122 | -background => ( $args->{ -background } || 'white' ), | ||
| 123 | )->pack( | ||
| 124 | -side => 'top', | ||
| 125 | -anchor => 'w', | ||
| 126 | -fill => "x", | ||
| 127 | -padx => 15, | ||
| 128 | -pady => 4, | ||
| 129 | ); | ||
| 130 | |||
| 131 | # $entry->configure( -background => $self->cget("-background") ) if $self->cget("-background"); | ||
| 132 | my $s = shift @Tk::DirTree::ISA; | ||
| 133 | unshift @Tk::DirTree::ISA, $s if ( $s ne 'Tk::Widget' ); | ||
| 134 | my $dirsParent = $frame->Scrolled( | ||
| 135 | "DirTree", | ||
| 136 | -background => ( $args->{ -background } || 'white' ), | ||
| 137 | -scrollbars => 'osoe', | ||
| 138 | -selectbackground => "navy", | ||
| 139 | -selectforeground => "white", | ||
| 140 | -selectmode => 'browse', | ||
| 141 | -height => 7, | ||
| 142 | -browsecmd => sub { ${ $args->{-variable} } = shift }, | ||
| 143 | )->pack( | ||
| 144 | -fill => "both", | ||
| 145 | -padx => 5, | ||
| 146 | -pady => 4, | ||
| 147 | -expand => 1, | ||
| 148 | ); | ||
| 149 | |||
| 150 | # $dirsParent->configure( -background => $self->cget("-background") ) if $self->cget("-background"); | ||
| 151 | my $dirs = $dirsParent->Subwidget('scrolled'); | ||
| 152 | |||
| 153 | # Add a little margin between the tree and the buttons underneath: | ||
| 154 | $frame->Frame( | ||
| 155 | -background => $self->{background}, | ||
| 156 | -height => 5, | ||
| 157 | )->pack( -side => 'top' ); | ||
| 158 | |||
| 159 | my $mkdir = $frame->Button( | ||
| 160 | -font => $self->{defaultFont}, | ||
| 161 | -text => "New ".$dir_term_ucf, | ||
| 162 | -command => sub { | ||
| 163 | |||
| 164 | my $new_name = $self->prompt( | ||
| 165 | -title => "Create New ".$dir_term_ucf, | ||
| 166 | -text => "Enter name for new $dir_term to be created in ${$args->{-variable}}" | ||
| 167 | ); | ||
| 168 | if ($new_name) { | ||
| 169 | $new_name =~ s/[\/\\]//g; | ||
| 170 | $new_name = ${ $args->{-variable} } . "/$new_name"; | ||
| 171 | if ( $self->_cb_try_create_dir($new_name) ) { | ||
| 172 | ${ $args->{-variable} } = $new_name; | ||
| 173 | # Thanks, Martin Thurn | ||
| 174 | eval { $dirs->add_to_tree($new_name, $new_name) }; #$dirs->configure( -directory => $new_name ); | ||
| 175 | $dirs->chdir($new_name); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | }, | ||
| 180 | )->pack( | ||
| 181 | -side => 'right', | ||
| 182 | -anchor => 'w', | ||
| 183 | -padx => 10, | ||
| 184 | -ipadx => 5, | ||
| 185 | ); | ||
| 186 | |||
| 187 | $self->{wizardFrame}->update; | ||
| 188 | $self->idletasks; | ||
| 189 | |||
| 190 | if ( $self->{desktop_dir} ) { # Thanks, Slaven Rezic. | ||
| 191 | $frame->Button( | ||
| 192 | -font => $self->{defaultFont}, | ||
| 193 | -text => "Desktop", | ||
| 194 | -command => sub { | ||
| 195 | ${ $args->{-variable} } = $self->{desktop_dir}; | ||
| 196 | |||
| 197 | # $dirs->configure( -directory => $self->{desktop_dir} ); | ||
| 198 | eval { $dirs->add_to_tree($self->{desktop_dir}, $self->{desktop_dir}) }; | ||
| 199 | $dirs->chdir( $self->{desktop_dir} ); | ||
| 200 | }, | ||
| 201 | )->pack( | ||
| 202 | -side => 'right', | ||
| 203 | -anchor => 'w', | ||
| 204 | -padx => 10, | ||
| 205 | -ipadx => 5, | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | |||
| 209 | foreach my $d (&$_drives) { | ||
| 210 | # Try to prevent GUI freeze: | ||
| 211 | $self->idletasks; | ||
| 212 | $self->{wizardFrame}->update; | ||
| 213 | $self->update; | ||
| 214 | $d = $1 if ($d =~ /^(\w+:)/); # ($d) =~ /^(\w+:)/; | ||
| 215 | |||
| 216 | if ( $args->{-nowarnings} | ||
| 217 | and ( $args->{-nowarnings} eq "1" or not $WINDOZE ) | ||
| 218 | ) { | ||
| 219 | eval { $dirs->add_to_tree($d, $d) } if -d $d; # $dirs->configure( -directory => $d ) if -d $d; | ||
| 220 | } | ||
| 221 | |||
| 222 | elsif ( $args->{-nowarnings} ) { # Fixed drives only | ||
| 223 | #$dirs->configure( -directory => $d ) | ||
| 224 | eval { $dirs->add_to_tree($d, $d) } | ||
| 225 | if ( ( Win32API::File::GetDriveType($d) == 3 ) and -d $d ); | ||
| 226 | } | ||
| 227 | |||
| 228 | else { | ||
| 229 | # $dirs->configure( -directory => $d ); | ||
| 230 | eval { $dirs->add_to_tree($d, $d) }; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # Make the user's requested directory appear as the default (?): | ||
| 235 | $dirs->chdir( ${ $args->{-variable} } ) if ( ${ $args->{-variable} } ne '' ); | ||
| 236 | |||
| 237 | $self->Unbusy; | ||
| 238 | return $frame; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | sub Tk::Wizard::_cb_try_create_dir { | ||
| 243 | my $self = shift; | ||
| 244 | my $dir_to_create = shift; | ||
| 245 | my $rasError; | ||
| 246 | |||
| 247 | File::Path::mkpath( $dir_to_create, { error => \$rasError } ); | ||
| 248 | |||
| 249 | if (@$rasError) { | ||
| 250 | my $rh = shift @$rasError; | ||
| 251 | |||
| 252 | # Only report the first error encountered: | ||
| 253 | my ($sDirEntered) = keys %$rh; | ||
| 254 | my ($sError) = values %$rh; | ||
| 255 | my $sMsg = | ||
| 256 | "The $dir_term you entered ($sDirEntered) could not be created ($sError)\nPlease choose a different $dir_term."; | ||
| 257 | $self->messageBox( | ||
| 258 | -icon => 'warning', | ||
| 259 | -type => 'ok', | ||
| 260 | -title => $dir_term_ucf.' Could Not Be Created', | ||
| 261 | -message => $sMsg, | ||
| 262 | ); | ||
| 263 | return 0; | ||
| 264 | } | ||
| 265 | |||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | # Tk::DirTree sorts its folder list case-sensitively, but on Windows | ||
| 272 | # we want case-INsensitive search. We roll our own until/unless the | ||
| 273 | # author of Tk::DirTree implements a fix (bug report submitted, see | ||
| 274 | # https://rt.cpan.org/Ticket/Display.html?id=28888): | ||
| 275 | REDEFINE: | ||
| 276 | { | ||
| 277 | no warnings 'redefine'; | ||
| 278 | |||
| 279 | sub Tk::DirTree::add_to_tree { | ||
| 280 | my ( $w, $dir, $name, $parent ) = @_; | ||
| 281 | my $dirSortable = $WINDOZE? uc $dir : $dir; | ||
| 282 | my $image = $w->cget('-image'); | ||
| 283 | |||
| 284 | if ( !UNIVERSAL::isa( $image, 'Tk::Image' ) ) { | ||
| 285 | $image = $w->Getimage($image); | ||
| 286 | } | ||
| 287 | |||
| 288 | my $mode = 'none'; | ||
| 289 | $mode = 'open' if $w->has_subdir($dir); | ||
| 290 | |||
| 291 | my @args = ( -image => $image, -text => $name ); | ||
| 292 | if ($parent) { # Add in alphabetical order. | ||
| 293 | foreach my $sib ( $w->infoChildren($parent) ) { | ||
| 294 | my $sibSortable = $WINDOZE? uc $sib : $sib; | ||
| 295 | if ( $sibSortable gt $dirSortable ) { # added by Martin Thurn | ||
| 296 | push @args, ( -before => $sib ); | ||
| 297 | last; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | $w->add( $dir, @args ); | ||
| 303 | $w->setmode( $dir, $mode ); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | =head2 callback_dirSelect | ||
| 310 | |||
| 311 | A callback to check that the directory, passed as a reference in the sole | ||
| 312 | argument, exists, or can and should be created. | ||
| 313 | |||
| 314 | Will not allow the Wizard to continue unless a directory has been chosen. | ||
| 315 | If the chosen directory does not exist, a messageBox will ask if it should be created. | ||
| 316 | If the user affirms, it is created; otherwise the user is again asked to | ||
| 317 | choose a directory. | ||
| 318 | |||
| 319 | Returns a Boolean value. | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | sub Tk::Wizard::callback_dirSelect { | ||
| 324 | my $self = shift; | ||
| 325 | my $var = shift; | ||
| 326 | if ( not $$var ) { | ||
| 327 | $self->messageBox( | ||
| 328 | '-icon' => 'info', | ||
| 329 | -type => 'ok', | ||
| 330 | -title => 'Form Incomplete', | ||
| 331 | -message => "Please select a $dir_term to continue." | ||
| 332 | ); | ||
| 333 | return 0; | ||
| 334 | } | ||
| 335 | |||
| 336 | if ( !-d $$var ) { | ||
| 337 | $$var =~ s|[\\]+|/|g; | ||
| 338 | $$var =~ s|/$||g; | ||
| 339 | my $button = $self->messageBox( | ||
| 340 | -icon => 'info', | ||
| 341 | -type => 'yesno', | ||
| 342 | -title => $dir_term_ucf.' does not exist', | ||
| 343 | -message => "The $dir_term you selected does not exist.\n\n" . "Shall I create " . $$var . " ?" | ||
| 344 | ); | ||
| 345 | |||
| 346 | if ( lc $button eq 'yes' ) { | ||
| 347 | return $self->_cb_try_create_dir($$var); | ||
| 348 | } | ||
| 349 | |||
| 350 | $self->messageBox( | ||
| 351 | -icon => 'info', | ||
| 352 | -type => 'ok', | ||
| 353 | -title => $dir_term_ucf.' Required', | ||
| 354 | -message => "Please select a $dir_term so that the Wizard can install the software on your machine.", | ||
| 355 | ); | ||
| 356 | return 0; | ||
| 357 | } | ||
| 358 | |||
| 359 | return 1; | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | =head2 addFileSelectPage | ||
| 364 | |||
| 365 | $wizard->addFileSelectPage( | ||
| 366 | -directory => 'C:/Windows/System32', | ||
| 367 | -variable => \$chosen_file, | ||
| 368 | ); | ||
| 369 | |||
| 370 | Adds a page (C<Tk::Frame>) that contains a "Browse" button which pops | ||
| 371 | up a file-select dialog box. The selected file will be displayed in a | ||
| 372 | read-only Entry widget. | ||
| 373 | |||
| 374 | Supply in C<-directory> the full path of an existing folder where the | ||
| 375 | user's search shall begin. | ||
| 376 | |||
| 377 | Supply in C<-variable> a reference to a variable to have set with the | ||
| 378 | chosen file name. | ||
| 379 | |||
| 380 | You may also specify the C<-title>, C<-subtitle> and C<-text> | ||
| 381 | parameters, as in L</blank_frame>. | ||
| 382 | |||
| 383 | An optional C<-background> argument is used as the background of the Entry widget | ||
| 384 | (default is white). | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | sub Tk::Wizard::addFileSelectPage { | ||
| 389 | my $self = shift; | ||
| 390 | my $args = {@_}; | ||
| 391 | # $self->addPage( sub { $self->_page_fileSelect($args) } ); | ||
| 392 | my %btn_args = | ||
| 393 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 394 | grep { /ButtonAction$/ } | ||
| 395 | keys %$args; | ||
| 396 | return $self->addPage( sub { $self->_page_fileSelect($args) }, %btn_args ); | ||
| 397 | } | ||
| 398 | |||
| 399 | # | ||
| 400 | # PRIVATE _page_fileSelect | ||
| 401 | # | ||
| 402 | # As blank_frame plus: | ||
| 403 | # -variable => Reference to a variable to set. | ||
| 404 | # -directory => start dir | ||
| 405 | sub Tk::Wizard::_page_fileSelect { | ||
| 406 | my $self = shift; | ||
| 407 | my $args = shift; | ||
| 408 | |||
| 409 | # Verify arguments: | ||
| 410 | if ( not $args->{-variable} ) { | ||
| 411 | Carp::croak "You must supply a -variable parameter"; | ||
| 412 | } | ||
| 413 | elsif ( not ref $args->{-variable} ) { | ||
| 414 | Carp::croak "The -variable parameter must be a reference"; | ||
| 415 | } | ||
| 416 | $args->{-directory} ||= '.'; | ||
| 417 | $args->{-title} ||= "Please choose an existing file"; | ||
| 418 | $args->{-subtitle} ||= "After you have made your choice, click 'Next' to continue."; | ||
| 419 | $args->{-text} ||= ''; | ||
| 420 | |||
| 421 | # Create the mother frame: | ||
| 422 | my ( $frame, @pl ) = $self->blank_frame( | ||
| 423 | -title => $args->{-title}, | ||
| 424 | -subtitle => $args->{-subtitle}, | ||
| 425 | -text => $args->{-text}, | ||
| 426 | -wait => $args->{ -wait }, | ||
| 427 | ); | ||
| 428 | |||
| 429 | # Put some space around the embedded elements: | ||
| 430 | $frame->Frame( | ||
| 431 | -background => $frame->cget("-background"), | ||
| 432 | -width => 10, | ||
| 433 | )->pack(qw( -side left )); | ||
| 434 | $frame->Frame( | ||
| 435 | -background => $frame->cget("-background"), | ||
| 436 | -width => 10, | ||
| 437 | )->pack(qw( -side right )); | ||
| 438 | |||
| 439 | # For now (i.e. because we're lazy), don't | ||
| 440 | # let the user type in. They must click | ||
| 441 | # the Browse button: | ||
| 442 | my $entry = $frame->Entry( | ||
| 443 | -justify => 'right', | ||
| 444 | -textvariable => $args->{-variable}, | ||
| 445 | -state => 'disabled', | ||
| 446 | -background => ( $args->{ -background } || 'white' ), | ||
| 447 | )->pack( | ||
| 448 | -side => 'left', | ||
| 449 | -anchor => 'w', | ||
| 450 | -fill => "x", | ||
| 451 | -expand => 1, | ||
| 452 | -padx => 3, | ||
| 453 | ); | ||
| 454 | |||
| 455 | my $bBrowse = $frame->Button( | ||
| 456 | -font => $self->{defaultFont}, | ||
| 457 | -text => 'Browse...', | ||
| 458 | -command => sub { | ||
| 459 | # getOpenFile will croak if the | ||
| 460 | # -initialdir we give it does not | ||
| 461 | # exist: | ||
| 462 | my $sDirInit = $args->{-directory}; | ||
| 463 | if ( not -d $sDirInit ) { | ||
| 464 | $sDirInit = &File::Spec::rootdir; | ||
| 465 | } | ||
| 466 | my $sFname = $frame->getOpenFile( | ||
| 467 | -initialdir => $sDirInit, | ||
| 468 | -title => $args->{-title}, | ||
| 469 | ); | ||
| 470 | ${ $args->{-variable} } = $sFname if $sFname; | ||
| 471 | }, | ||
| 472 | )->pack(qw( -side left -padx 3)); | ||
| 473 | |||
| 474 | return $frame; | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | 1; | ||
| 479 | |||
| 480 | =head1 AUTHOR | ||
| 481 | |||
| 482 | Lee Goddard (lgoddard@cpan.org). | ||
| 483 | |||
| 484 | =head1 COPYRIGHT | ||
| 485 | |||
| 486 | Copyright (C) Lee Goddard, 11/2002 - 01/2008, 06/2015 ff. | ||
| 487 | |||
| 488 | Made available under the same terms as Perl itself. |