home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeMastering Perl/TkSearch this book

23.3. Dialog Boxes

There are several reasons you might want to create a dialog box in Perl/Tk. This section will show you what your choices are and how to configure dialog boxes.

23.3.2. The messageBox Method

If you need only a one-shot dialog, you can use the messageBox method. The options are slightly different, but the result looks almost the same, shown in Figure 23-5. Keep in mind that every time you call messageBox, a new Dialog is created and there is no Show method.

Figure 23-5

Figure 23-5. Dialog created using messageBox

Like a Dialog widget, messageBox returns the selected Button's string, but its case may vary depending on your operating system. Therefore, it's best to treat the response in a case-insensitive manner.

The messageBox Buttons are specified with the -type option, so, for example, a -type of AbortRetryIgnore produces three Buttons with the labels Abort, Retry, and Ignore. Any of these Button labels can be specified as the default Button. Once again, case issues are present; see -default. It's anticipated that these case issues will be taken care of in Tk 800.024.

   $answer => $mw->messageBox(-title => 'Please Reply', 
     -message => 'Would you like to continue?', 
     -type => 'YesNo', -icon => 'question', -default => 'yes');

The options used with messageBox are as follows:

-default => string
The string specified will be used as the default button. Depending on the version of messageBox you have, the default string needs to be all lowercase on a Win32 system, whereas on Unix the first letter should be capitalized. Either way, if the string doesn't match properly, you'll get an error.

-icon => bitmap
The bitmap to display to the left of the message text. The bitmap displayed is operating system dependent.

-message => message
The message to display above the buttons on the dialog.

-title => title
The title for the dialog.

-type => buttontypes
Specifies a predefined set of buttons to be displayed. The following values are possible: 'AbortRetryIgnore', 'OK', 'OKCancel', 'RetryCancel', 'YesNo', or 'YesNoCancel'. An invalid type will result in an error.

23.3.6. getOpenFile and getSaveFile Dialogs

Often you will want the user to locate an existing file or the location for a new file on her filesystem. The methods getOpenFile and getSaveFile let you do this easily. Figure 23-10 illustrates the getOpenFile dialog.

Figure 23-10

Figure 23-10. Version of getOpenFile (getSaveFile looks the same except for the title)

To give you a quick idea of how these are used, getOpenFile is most commonly associated with the "Open" command in the File menu, and getSaveFile is usually associated with the "Save as..." command in the File menu. In either case, if the user selects a file, both methods return the full pathname of this file. If the user cancels the operation, both methods return an undefined value.

The easiest way to invoke them both is:

my $file = $mw->getOpenFile( );
&do_something($file) if defined $file;

my $sfile = $mw->getSaveFile( );
&do_somethingelse($sfile) if defined $sfile;

The only real difference between the two methods is the error handling they perform. For instance, if you try to select a preexisting file from the Save dialog, you will be asked if you want to overwrite that file. Your program is still required to actually create or open the returned filename; these dialogs are just a consistent way of requesting the information from the user.

If you would like to customize your open or save dialog, use one or more of the following optional arguments:

-defaultextension => string
Specifies a string that will be appended to the filename if the user enters a filename without an extension.

-filetypes => [ filePatternList ]
You can specify some predefined file patterns for the user to choose from (if applicable on your platform). The filePatternList is a list itself, so you end up with a list of lists. Each inner list should contain a file type description, an extension (or list of extensions), and an optional type. Here's an example to clarify:

my $types = [
  ['Text Files',       ['.txt', '.text']],
  ['Java Source Files', '.java'          ],
  ['C Source Files',   '.c',      'TEXT'],
  ['GIF Files',        '.gif',          ],
  ['All Files',        '*',             ],

my $filename = $mw->getOpenFile(-filetypes => $types);
-initialdir => directory
The starting directory for the user to browse from. If not specified, the files in the current working directory are displayed.

-initialfile => filename
Default filename to start with. This option is ignored by the getOpenFile method.

-title => string
A title to use for the dialog. A default title will be used if this isn't specified.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.