Next: Selecting Directories with Up: File Selection Dialog Previous: Specifying File Types


5.1.5 The tix filedialog Command 

TixExFileSelectDialog and TixFileSelectDialog are very similar to each other. So which one should we use? That is just a matter of taste. However, since we know that programmers usually have bad taste, clever programmers would rather step aside and let the users exercise their own taste. To do this, we can use the tix filedialog command.

For any programs based on Tix, the user can choose his preferred type of file dialog by setting the X resource FileDialog to either tixFileSelectDialog or tixExFileSelectDialog. This can usually be done by inserting a line similar to the following into the user's .Xdefaults file:

*myapp*FileDialog: tixExFileSelectDialog

When we call the command tix filedialog, it will return a file dialog widget of the user's preferred type.

The advantage of using tix filedialog is it makes coding flexible. If the management suddenly mandates that we dump the Motif look-and-feel in favor of the MS Windows look-and-feel, we don't need to dig up every line of tixFileSelectDialog calls and replace it with tixExFileSelectDialog. Also, tix filedialog creates only one copy of the file dialog, which can be shared by different parts of the program. Therefore, we can avoid creating a separate file dialog widget for each of the ``Open'', ``Save'' and ``Save As'' commands in our application. This way, we can save resource since a file dialog is a large widget and it takes up quite a bit of space.

set dialog [tix filedialog]
$dialog -title "Select A File" -command selectCmd
$dialog subwidget fsbox config -pattern "*.txt" -directory /usr/info
if {[winfo class $dialog] == "TixExFileSelectDialog"} {
$dialog subwidget fsbox config -filetypes {
{\*} {* - All files}\
{\*.txt} {*.txt - Text files}\
{\*.c} {*.c - C source files}\
$dialog popup

proc selectCmd {filename} {
puts "You have selected $filename"

(Figure 5-4) Using the tix dialog command 

The use of the tix filedialog command is shown in program 5-4 . This program is very similar to what we saw in program 5-1 , except now we aren't really sure which type of file dialog the user have chosen. Therefore, if we want to do something allowed for only one type of file dialogs, we have to be careful. At line 4 of program 5-4, we use the winfo command to see whether the type of the file dialog is TixExFileSelectDialog. If so, we set the value for the -filetypes option of its fsbox subwidget.