TIP #242: Preselect Filter on tk_get*File Dialogs

Title:Preselect Filter on tk_get*File Dialogs
Version:$Revision: 1.8 $
Author:Brian Griffin <bgriffin at model dot com>
Created:Thursday, 03 March 2005


This TIP proposes adding an -typevariable option to the tk_getOpenFile and tk_getSaveFile dialog box commands. This option will preselect the filter from the -filetypes list based on the type name.


The standard Open and Save dialog boxes currently allow you to seed the initial directory and the initial file. The path returned by the call can be saved and used to seed future calls to these dialgo boxes. However, the selected filter cannot be choosen nor read, nor is examining the returned file path sufficient to guess at the filter setting.

One common aspect of good U/I design is remembering prior selections so the user does not have to repeat actions each time a dialog is used. The initial directory and initial file options provide partially for this ability in the File dialog boxes, but the selected filter type is not controllable.

The filter selection can also be used for format selection when the file extension is insufficient or not present as is typical in the Mac OS X environment. One example might be a file with the extension .xml, where the file contents may be data, schema, or a dtd. By selecting "XML Schema", over "XML DTD", the user can control the formatting of the .xml file.

By giving the application access to the filter selection, the UI can be made friendlier to the user.

Proposed Change

Since the requirement is to be able to set and read the filter selection, the proposal is to use a variable to control and access this feature of the dialog boxes. This is done by adding a -typevariable option to tk_getOpenFile and tk_getSaveFile.

The variable specified is read once at the begining to select the appropriate filter. If the variable does not exist, or it's value does not match any filter typename, or is empty ({}), the dialog box will revert to the default behavior of selecting the first filter in the list.

When the dialog box is closed by using the Ok button, the variable is set to the typename value of the currently selected filter. The variable will not be updated while the dialog box is open and the user makes various selections. If the dialog box is closed using the Cancel button, the variable is not changed.


    set types {
        {{Text Files}       {.txt}        }
        {{TCL Scripts}      {.tcl}        }
        {{C Source Files}   {.c}      TEXT}
        {{GIF Files}        {.gif}        }
        {{GIF Files}        {}        GIFF}
        {{All Files}        *             }
    set type {C Source Files}
    tk_getOpenFile -filetypes $types -typevariable type
    set type
 => {TCL Scripts}

To preselect .c files for the filter, set type {C Source Files}. If the filter is changed to {TCL Scripts}, for example, the variable "type" is modified to the value {TCL Scripts}.

Reference Implementation

A reference implementation will be posted to Tk patches[1]. I have modified tkfbox.tcl and tkWinDialog.c. I have not looked at MacOSX yet.


Thanks to Michael Kirkham and Donal Fellows for their valuable suggestions.


This document has been placed in the public domain.

Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows