Tcl/Tk Cookbook - Text Editor


Step 3: Create Pop up dialogue boxes

Script

Append the following to ed.tcl to include the source of the script that create the three dialogue windows and their associated behaviour. The scripts are in the three files message.tcl, filesel.tcl and popup.tcl:



#source some of the auxillary scripts we will be using
#note these can be source in appropriate procedures too

source filesel.tcl

source message.tcl

source popup.tcl


Use of FileSelectonbox

For this editor, the fileselectionbox is popped up when the user wants to specify the name of a file to be opened or the text in the text widget to be saved into one.

The fileselectionbox script is contributed software with some minor changes. The script is in the Cokkbook's code subdirectory under ch3 and is named filesel.tcl. Copy that file into your current working directory. The reader should be able to use it as an independent unit.

The fileselectionbox is simple and looks like:

Warning message

When the user selects a "SaveAs" option but subsequently changes his mind and cancels the fileselectionbox, a pop-up warning message that the contents of the text widget will be stored under the old filename.

The code for this script is as follows and is placed in the file mes.tcl:



proc showMessage {mess} {

toplevel .messpop -width 10c -height 4c
grab .messpop
wm title .messpop "Warning"
message .messpop.msg -relief raised -bd 2 -text $mess

button .messpop.okb -text OK \
	-com {destroy .messpop ; return 0}
pack .messpop.msg .messpop.okb -side top 
}


showMessage takes an argument which is a message string (the procedure is essebtially parametrized so you can vary the message but use the same procedure for all message dialogues whereever you want). It puts up a message widget which is created with the Tk command message with the message string passed as input. A "OK" button with the command to destroy this toplevel when the user clicks on the button is also included.

Modal Interaction

The Tk command toplevel creates a toplevel window for the application. The toplevel window is the child of the applications root (main) window but can be used as additional windows for the application.

Toplevel windows can also be used as popup message windows. In this case, in the procedure creating the popup message window, the Tk command grab with the name of the message window as argument is given. This will grab the keyboard focus to ensure user's attention. In the calling procedure, the Tk command tkwait window is called with the message window name as the argument. This suspends processing in the calling procedure, until the user undertakes the necessary interaction with the poppped up window and explicitly closes (destroys) it. The command for any OK, DISMISS or Cancel button in popup or toplevel window is to destroy it.

The message window created with the script for warning about a file being overwriiten looks like:

Search & Replace Window

The third popup window is used when the user wants to do a search and replace or search and tag or tag all operations on the displayed text. Note that the tagging is temporary and not saved in the file.


proc FindPopup {} {

global seltxt repltxt

toplevel .fpop -width 10c -height 4c

grab .fpop
wm title .fpop "Find Text"


label .fpop.lab1 -text "Find :           " 
place .fpop.lab1 -in .fpop -x 2 -y 6
entry .fpop.en1 -width 20 -relief sunken -textvariable seltxt
place .fpop.en1 -in .fpop -x 72  -y 6

label .fpop.lab2 -text "Replace :        "
place .fpop.lab2 -in .fpop -x 2 -y 50
entry .fpop.en2 -width 20 -relief sunken -textvariable repltxt
place .fpop.en2 -in .fpop -x 72 -y 50

menubutton .fpop.finb -text Find -menu .fpop.finb.menu
place .fpop.finb -in .fpop -x 2 -y 90
menu .fpop.finb.menu 
.fpop.finb.menu add command -label Forward -com {FindWord  -forwards $seltxt}
.fpop.finb.menu add command -label Backward -com {FindWord -backwards $seltxt}

menubutton .fpop.finrb -text "Find and Replace" -menu .fpop.finrb.menu
place .fpop.finrb -in .fpop -x 38 -y 90
menu .fpop.finrb.menu
.fpop.finrb.menu add command -label Forward  -com {ReplaceSelection -forwards}
.fpop.finrb.menu add command -label Backward -com {ReplaceSelection -backwards}

button .fpop.repall -text "Replace All" -com {ReplaceAll}
place .fpop.repall -in .fpop -x 150 -y 90

button .fpop.tagall -text "Tag All" -com {TagAll}
place .fpop.tagall -in .fpop -x 250 -y 36

button .fpop.dismis -text Dismiss -com {destroy .fpop}
place .fpop.dismis -in .fpop -x 250 -y 90


focus .fpop.en1
}

This code is similar to what has already been described in creating the main menubar as well as the toplevel window of the message box. Note that the the command "place" takes two options "-x" and "-y" whose values are integers (pixels in this case) and asks the packer to place the widget at that location inside the widget which is the value given for the option "-in".

In this popup window the menubuttons Find and Find and Replace both have pulldown menus to make the search forward and back. The procedures are defined in the next section. If you want to execute this script and test it, you can comment out the lines just before the -com option begins. This will stop the Tk interpreter complaining about undefined commands.

The final result when you execute this script should be: