| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: Dialog.pm,v 1.11 2007/09/19 18:58:17 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2001,2005 Slaven Rezic. 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: http://user.cs.tu-berlin.de/~eserte/ | ||
| 13 | # | ||
| 14 | |||
| 15 | package Tk::PathEntry::Dialog; | ||
| 16 | use Tk::PathEntry; | ||
| 17 | use base qw(Tk::DialogBox); | ||
| 18 | use strict; | ||
| 19 | use vars qw($VERSION); | ||
| 20 | $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/); | ||
| 21 | |||
| 22 | Construct Tk::Widget 'PathEntryDialog'; | ||
| 23 | |||
| 24 | sub import { | ||
| 25 | if (defined $_[1] and $_[1] eq 'as_default') { | ||
| 26 | local $^W = 0; | ||
| 27 | package # hide from PAUSE indexer | ||
| 28 | Tk; | ||
| 29 | if ($Tk::VERSION < 804) { | ||
| 30 | *FDialog = \&Tk::PathEntry::Dialog::FDialog; | ||
| 31 | *MotifFDialog = \&Tk::PathEntry::Dialog::FDialog; | ||
| 32 | } else { | ||
| 33 | *tk_getOpenFile = sub { | ||
| 34 | Tk::PathEntry::Dialog::FDialog("tk_getOpenFile", @_); | ||
| 35 | }; | ||
| 36 | *tk_getSaveFile = sub { | ||
| 37 | Tk::PathEntry::Dialog::FDialog("tk_getSaveFile", @_); | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub Populate { | ||
| 44 | my($w, $args) = @_; | ||
| 45 | |||
| 46 | $args->{-buttons} = ["OK", "Cancel"]; | ||
| 47 | # Disable default button feature of Tk::DialogBox | ||
| 48 | $args->{-default_button} = 'none'; | ||
| 49 | $args->{-title} ||= 'Select path'; # default window title | ||
| 50 | $w->SUPER::Populate($args); | ||
| 51 | |||
| 52 | my $pe = $w->add('PathEntry', | ||
| 53 | -textvariable => \$w->{PathName}, | ||
| 54 | )->pack(-expand => 1, -fill => 'x'); | ||
| 55 | $w->Advertise("PathEntry" => $pe); | ||
| 56 | $args->{-focus} = $pe; | ||
| 57 | |||
| 58 | $pe->bind("<Return>" => sub { | ||
| 59 | $w->Subwidget("B_OK")->Invoke; | ||
| 60 | }); | ||
| 61 | $w->bind("<Escape>" => sub { | ||
| 62 | $w->Subwidget("B_Cancel")->Invoke; | ||
| 63 | }); | ||
| 64 | |||
| 65 | $w->ConfigSpecs | ||
| 66 | (-create => ['PASSIVE', undef, undef, 0], | ||
| 67 | 'DEFAULT' => [$pe], | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub Show { | ||
| 72 | my $w = shift; | ||
| 73 | my @args = @_; | ||
| 74 | |||
| 75 | my $pathname; | ||
| 76 | my $pe = $w->Subwidget("PathEntry"); | ||
| 77 | |||
| 78 | while (1) { | ||
| 79 | undef $pathname; | ||
| 80 | |||
| 81 | my $r = $w->SUPER::Show(@args); | ||
| 82 | $pathname = $w->{PathName} if $r =~ /ok/i; | ||
| 83 | $pe->Finish; | ||
| 84 | |||
| 85 | if (defined $pathname && $w->cget(-create) && -f $pathname) { | ||
| 86 | |||
| 87 | # Disable default button feature of Tk::DialogBox | ||
| 88 | # (invalid option and not required for Windows) | ||
| 89 | my $noDefault = $Tk::platform eq 'MSWin32' ? '' : | ||
| 90 | "-default_button => 'none'"; | ||
| 91 | my $reply = $w->messageBox | ||
| 92 | (-icon => 'warning', | ||
| 93 | -type => 'YesNo', | ||
| 94 | eval {split(' => ', $noDefault)}, | ||
| 95 | -message => "File \"$pathname\" already exists.\nDo you want to overwrite it?"); | ||
| 96 | redo unless (lc($reply) eq 'yes'); | ||
| 97 | } | ||
| 98 | last; | ||
| 99 | } | ||
| 100 | |||
| 101 | $pathname; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub FDialog | ||
| 105 | { | ||
| 106 | my($cmd, %args) = @_; | ||
| 107 | |||
| 108 | $args{-create} = !!($cmd =~ /Save/); | ||
| 109 | |||
| 110 | delete $args{-filetypes}; | ||
| 111 | delete $args{-defaultextension}; | ||
| 112 | delete $args{-force}; | ||
| 113 | |||
| 114 | Tk::DialogWrapper('PathEntryDialog',$cmd, %args); | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ |