| 1 |
# Copyright (c) 1996 Sun Microsystems, Inc. |
| 2 |
# See the file "license.terms" for information on usage and redistribution |
| 3 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
Tk::getOpenFile - pop up a dialog box for the user to select a file to open. |
| 8 |
|
| 9 |
=for category Popups and Dialogs |
| 10 |
|
| 11 |
=head1 SYNOPSIS |
| 12 |
|
| 13 |
S< >I<$widget>-E<gt>B<getOpenFile>(?I<-option>=E<gt>value, ...>?) |
| 14 |
|
| 15 |
S< >I<$widget>-E<gt>B<getSaveFile>(?I<-option>=E<gt>value, ...>?) |
| 16 |
|
| 17 |
=head1 DESCRIPTION |
| 18 |
|
| 19 |
The methods B<getOpenFile> and B<getSaveFile> pop up a |
| 20 |
dialog box for the user to select a file to open or save. |
| 21 |
|
| 22 |
The B<getOpenFile> method is usually associated with the B<Open> |
| 23 |
command in the B<File> menu. Its purpose is for the user to select an |
| 24 |
existing file I<only>. If the user enters an non-existent file, the |
| 25 |
dialog box gives the user an error prompt and requires the user to give |
| 26 |
an alternative selection. If an application allows the user to create |
| 27 |
new files, it should do so by providing a separate B<New> menu command. |
| 28 |
|
| 29 |
The B<getSaveFile> method is usually associated with the B<Save> |
| 30 |
as command in the B<File> menu. If the user enters a file that |
| 31 |
already exists, the dialog box prompts the user for confirmation |
| 32 |
whether the existing file should be overwritten or not. |
| 33 |
|
| 34 |
If the user selects a file, both B<getOpenFile> and |
| 35 |
B<getSaveFile> return the full pathname of this file. If the |
| 36 |
user cancels the operation, both commands return an undefined value. |
| 37 |
|
| 38 |
The following I<option-value> pairs are possible as command line |
| 39 |
arguments to these two commands: |
| 40 |
|
| 41 |
=over 4 |
| 42 |
|
| 43 |
=item B<-defaultextension> =E<gt> I<extension> |
| 44 |
|
| 45 |
Specifies a string that will be appended to the filename if the user |
| 46 |
enters a filename without an extension. The default value is the empty |
| 47 |
string, which means no extension will be appended to the filename in |
| 48 |
any case. This option is ignored on the Macintosh platform, which |
| 49 |
does not require extensions to filenames, and the UNIX implementation |
| 50 |
guesses reasonable values for this from the B<-filetypes> |
| 51 |
option when this is not supplied. |
| 52 |
|
| 53 |
=item B<-filetypes> =E<gt> [I<filePattern> ?, ...?] |
| 54 |
|
| 55 |
If a B<File types> listbox exists in the file dialog on the particular |
| 56 |
platform, this option gives the I<filetype>s in this listbox. When |
| 57 |
the user choose a filetype in the listbox, only the files of that type |
| 58 |
are listed. If this option is unspecified, or if it is set to the |
| 59 |
empty list, or if the B<File types> listbox is not supported by the |
| 60 |
particular platform then all files are listed regardless of their |
| 61 |
types. See L<"SPECIFYING FILE PATTERNS"> below for a |
| 62 |
discussion on the contents of I<filePattern>s. |
| 63 |
|
| 64 |
=item B<-initialdir> =E<gt> I<directory> |
| 65 |
|
| 66 |
Specifies that the files in I<directory> should be displayed |
| 67 |
when the dialog pops up. If this parameter is not specified, then |
| 68 |
the files in the current working directory are displayed. This |
| 69 |
option may not always work on the Macintosh. This is not a bug. |
| 70 |
Rather, the I<General Controls> control panel on the Mac allows the |
| 71 |
end user to override the application default directory. |
| 72 |
|
| 73 |
=item B<-initialfile> =E<gt> I<filename> |
| 74 |
|
| 75 |
Specifies a filename to be displayed in the dialog when it pops |
| 76 |
up. This option is ignored by the B<getOpenFile> method. |
| 77 |
|
| 78 |
=item B<-multiple> |
| 79 |
|
| 80 |
Allows the user to choose multiple files from the Open dialog. On the |
| 81 |
Macintosh, this is only available when Navigation Services are |
| 82 |
installed. |
| 83 |
|
| 84 |
=item B<-message> =E<gt> I<string> |
| 85 |
|
| 86 |
Specifies a message to include in the client area of the dialog. This |
| 87 |
is only available on the Macintosh, and only when Navigation Services |
| 88 |
are installed. |
| 89 |
|
| 90 |
=item B<-title> =E<gt> I<titleString> |
| 91 |
|
| 92 |
Specifies a string to display as the title of the dialog box. If this |
| 93 |
option is not specified, then a default title is displayed. This |
| 94 |
option is ignored on the Macintosh platform. |
| 95 |
|
| 96 |
=back |
| 97 |
|
| 98 |
=head1 SPECIFYING FILE PATTERNS |
| 99 |
|
| 100 |
The I<filePattern>s given by the B<-filetypes> option |
| 101 |
are a list of file patterns. Each file pattern is a list of the |
| 102 |
form |
| 103 |
|
| 104 |
typeName [extension ?extension ...?] ?[macType ?macType ...?]? |
| 105 |
|
| 106 |
I<typeName> is the name of the file type described by this |
| 107 |
file pattern and is the text string that appears in the B<File types> |
| 108 |
listbox. I<extension> is a file extension for this file pattern. |
| 109 |
I<macType> is a four-character Macintosh file type. The list of |
| 110 |
I<macType>s is optional and may be omitted for applications that do |
| 111 |
not need to execute on the Macintosh platform. |
| 112 |
|
| 113 |
Several file patterns may have the same I<typeName,> in which case |
| 114 |
they refer to the same file type and share the same entry in the |
| 115 |
listbox. When the user selects an entry in the listbox, all the files |
| 116 |
that match at least one of the file patterns corresponding |
| 117 |
to that entry are listed. Usually, each file pattern corresponds to a |
| 118 |
distinct type of file. The use of more than one file patterns for one |
| 119 |
type of file is necessary on the Macintosh platform only. |
| 120 |
|
| 121 |
On the Macintosh platform, a file matches a file pattern if its |
| 122 |
name matches at least one of the I<extension>(s) AND it |
| 123 |
belongs to at least one of the I<macType>(s) of the |
| 124 |
file pattern. For example, the B<C Source Files> file pattern in the |
| 125 |
sample code matches with files that have a B<\.c> extension AND |
| 126 |
belong to the I<macType> B<TEXT>. To use the OR rule instead, |
| 127 |
you can use two file patterns, one with the I<extensions> only and |
| 128 |
the other with the I<macType> only. The B<GIF Files> file type |
| 129 |
in the sample code matches files that EITHER have a B<\.gif> |
| 130 |
extension OR belong to the I<macType> B<GIFF>. |
| 131 |
|
| 132 |
On the Unix and Windows platforms, a file matches a file pattern |
| 133 |
if its name matches at at least one of the I<extension>(s) of |
| 134 |
the file pattern. The I<macType>s are ignored. |
| 135 |
|
| 136 |
=head1 SPECIFYING EXTENSIONS |
| 137 |
|
| 138 |
On the Unix and Macintosh platforms, extensions are matched using |
| 139 |
glob-style pattern matching. On the Windows platforms, extensions are |
| 140 |
matched by the underlying operating system. The types of possible |
| 141 |
extensions are: (1) the special extension * matches any |
| 142 |
file; (2) the special extension "" matches any files that |
| 143 |
do not have an extension (i.e., the filename contains no full stop |
| 144 |
character); (3) any character string that does not contain any wild |
| 145 |
card characters (* and ?). |
| 146 |
|
| 147 |
Due to the different pattern matching rules on the various platforms, |
| 148 |
to ensure portability, wild card characters are not allowed in the |
| 149 |
extensions, except as in the special extension *. Extensions |
| 150 |
without a full stop character (e.g, ~) are allowed but may not |
| 151 |
work on all platforms. |
| 152 |
|
| 153 |
=head1 CAVEATS |
| 154 |
|
| 155 |
See L<Tk::chooseDirectory/CAVEATS>. |
| 156 |
|
| 157 |
=head1 EXAMPLE |
| 158 |
|
| 159 |
my $types = [ |
| 160 |
['Text Files', ['.txt', '.text']], |
| 161 |
['TCL Scripts', '.tcl' ], |
| 162 |
['C Source Files', '.c', 'TEXT'], |
| 163 |
['GIF Files', '.gif', ], |
| 164 |
['GIF Files', '', 'GIFF'], |
| 165 |
['All Files', '*', ], |
| 166 |
]; |
| 167 |
my $filename = $widget->getOpenFile(-filetypes=>$types); |
| 168 |
|
| 169 |
if ($filename ne "") { |
| 170 |
# Open the file ... |
| 171 |
} |
| 172 |
|
| 173 |
=head1 SEE ALSO |
| 174 |
|
| 175 |
L<Tk::FBox|Tk::FBox>, L<Tk::FileSelect|Tk::FileSelect> |
| 176 |
|
| 177 |
=head1 KEYWORDS |
| 178 |
|
| 179 |
file selection dialog |
| 180 |
|
| 181 |
=cut |
| 182 |
|