The building blocks of the interface are now in place, but it would be nice if some basic behavior can be specified; for example, we would like to see the scrollbar in action or be able to make a real selection from the list.
With the final file-selector (of which this is the prototype), files can be selected in two ways. The first way is to select a file from the list and click OK, the second is to enter a filename in the entry-box and click 'OK'. If CANCEL is chosen, nothing should be selected. When either button is pressed, the file-selector should vanish. In the prototype, when OK is pressed, the selected file will be printed on stdout, so we can check if the file-selector is functioning properly.
Since this is a prototype, it doesn't really matter if we create a real list of files or make one up. The first is certainly not impossible in Tcl, but for simplicity's sake, we'll take the latter approach. First, we associate the scrollbar with the listbox and vise versa:
.fileselect.files.scrolly configure -command {.fileselect.files.list yview} .fileselect.files.list configure -yscrollcommand \ {.fileselect.files.scrolly set}
The first command tells Tcl that if the scrollbar is changed (for example by clicking on one of the arrows), that the changes are passed to the listbox, which will then update its contents. The second command states that if the contents of the listbox are scrolled up or down, the change should be reflected by the position of the slider.
Next, some data is added to the listbox:
.fileselect.files.list 999 file0 .fileselect.files.list 999 file1 : .fileselect.files.list 999 file18 .fileselect.files.list 999 file19
The listbox now shows a list of 20 filenames and the contents of this listbox can be scrolled by using the scrollbar.
We now define the procedures that are called when a button is pressed:
proc do_ok {} { if [expr [ llength [ .fileselect.files.list curselection ] ] > 0 ] then \ { \ puts [ .fileselect.files.list get \ [ .fileselect.files.list curselection ] ] \ } \ else \ { \ puts [ .fileselect.entry get ] \ } destroy .fileselect } proc do_cancel {} { puts "File selection canceled" destroy .fileselect }
The first procedure should be called when the OK button is pressed. It checks if there is some selection made in the listbox and if there is, prints the selected item. It there is no selection made, the contents of the entry are printed. Either way, the file-selector window is removed. The second procedure should be called when the CANCEL button is pressed. It just prints a message that the selection is canceled and destroys the window.
These procedures can now be bound to the buttons:
.fileselect.buttons.ok configure -command do_ok .fileselect.buttons.cancel configure -command do_cancel
This will complete the file-selector. Of course, the final version must have
much more functionality, but this is only a prototype; it shows how the
file-selector will 'look and feel'. See figure 8 for the
'look'. The final script can be found in
/home/se/doc/tcltk/source/fileselector.tcl
.
Figure 8: The file-selector prototype in action