Previous: A simple browser application
Up: Example applications
Next: A simple timer
Previous Page: A simple browser application
Next Page: A simple timer

A simple editor application

This section shows how to write a simple text editor application. It is not intended to be a fully fledged text editor, such as vi, emacs, but demonstrates how such a tool could be built up relatively quickly using TCL/TK. An editor such as the one given here can be designed and built within a few hours. The heart of the editor is a text widget, which is suitably configured, and linked to a scroll bar. Menu buttons are attached to provide access to file handling and other options.

The code for the basic skeleton follows:

#This is a skeleton for a very simple editor, written using
   #TCL with TK widgets.

#The editor "looks" like a simple package, but none of the #procedures actually work.

#set up a frame for menubar

frame .mb menubutton .mb.button1 -text "File" -relief raised -menu .mb.button1.m menubutton .mb.button2 -text "Edit" -relief raised -menu .mb.button2.m

pack .mb.button1 .mb.button2 -side left -padx 2m -fill x -expand yes

menu .mb.button1.m menu .mb.button2.m

.mb.button1.m add command -label "New..." -command {NewFile} .mb.button1.m add command -label "Load ..." -command {LoadFile} .mb.button1.m add command -label "Append..." -command {AppendFile} .mb.button1.m add command -label "Save" -command {SaveFile} .mb.button1.m add command -label "Save As..." -command {SaveAsFile} .mb.button1.m add command -label "Quit" -command {QuitFile}

.mb.button2.m add command -label "Clear" -command {ClearEdit}

#set up a frame for text edit frame .te -relief raised -borderwidth 2

#Shows how to control text widget scrolling using #a scrollbar

#set geometry to give a reasonable window size . configure -geometry 80x30

#First set up scrollbar scrollbar .te.vscroll -relief sunken -command ".te.edit1 yview"

#Set up a text widget and link scroll text .te.edit1 -yscroll ".te.vscroll set"

#Pack editing components

pack .te.vscroll -side right -fill y pack .te.edit1 -expand yes -fill y

#Now pack everything together pack .mb .te -pady 2m -fill x

proc NewFile {} { puts "NewFile not implemented yet" }

proc LoadFile {} { puts "LoadFile not implemented yet" }

proc AppendFile {} { puts "AppendFile not implemented yet" }

proc SaveFile {} { puts "SaveFile not implemented yet" }

proc SaveAsFile {} { puts "SaveAsFile not implemented yet" }

proc QuitFile {} { puts "QuitFile not implemented yet" }

proc ClearEdit {} { puts "ClearEdit not implemented yet" }

This code should be executable, but not have much in the way of functionality.

In order to add functionality, each of the dummy procedures is associated with a few lines of code, as shown here:

proc NewFile {} {
   global filename
    #Could have anguish here - should we prompt
    #the user for confirmation?
    #Should we check to see if text exists in window?
    #Should we check to see if file has changed?
    #Should we check to see if file has been saved?

#Brutal implementation! .te.edit1 delete 1.0 end set filename "New" wm title . "EDIT: $filename" }

This code is, as the comments suggest, rather brutal in its operation. Little or no checking is carried out, which would be appropriate in a more serious implementation of an editor. The basic actions are to delete all the text from the window, and to reset the variable filename to the value "new", and to notify the window manager of the updated filename.

The next procedure loads in a new file. First the name of the text file is obtained, and the file opened. Next the text widget is cleared, using the ClearEdit command discussed later, and the text is loaded into the text widget. Lastly, the filename is set and the window manager notified of the updated filename.

proc LoadFile {} {
   global filename
    #First get text from file
    set fn [exec "getfilename.tcl"]
    set f [open $fn r]
    set x [read $f]
    #Clear the window!
    ClearEdit
    #Insert the new text
    .te.edit1 insert 1.0 $x
    set t [close $f]
    #Set global filename
    set filename $fn
    wm title . "EDIT : $filename"
    }

The code for appending a file is very similar to that for loading, except that the text widget is not cleared, and the filename is not changed.

{
   proc AppendFile {} {
       #First get text from file
    set fn [exec "getfilename.tcl"]
    set f [open $fn r]
    set x [read $f]
       #Append file at end of text
    .te.edit1 insert end $x
       #Close source file
    set t [close $f]
    }

The code for saving a file uses the global variable filename as the name of the file to save, and gets the text from the text widget. Finally it stores the text in the named file, and closes the file.

proc SaveFile {} {
   global filename
    set f [open $filename w]
       #Get text to save
    set x [.te.edit1 get 1.0 end]
       #Save it
    puts $f $x
       #and close file
    set t [close $f]
    }

The code for SaveAs is similar to that for Save, except that it obtains the name of the file for saving, and when the save has been carried out, the value of the global variable filename is ammended, and the window manager notified.

proc SaveAsFile {} {
   global filename
       #Get file name to save
    set fn [exec "getfilename.tcl"]
    set f [open $fn w]
       #Get text to save
    set x [.te.edit1 get 1.0 end]
       #Save it
    puts $f $x
       #and close file
    set t [close $f]
    #Set global filename
    set filename $fn
   wm title . "EDIT : $filename"
    }

QuitFile shuts the whole editor down - in this case without any prompting for confirmation. A more humane design would ensure that it would not be possible to exit the editor without confirmation from the user.

proc QuitFile {} {
    #Could have anguish here - should we prompt
    #the user for confirmation?
    #Should we check to see if file has changed?
    #Should we check to see if file has been saved?

#This implementation is brutal! destroy . }

The ClearEdit procedure is used to clear the text widget - again with no prompting.

proc ClearEdit {} {
    #Could have anguish here - should we prompt
    #the user for confirmation?
    #Should we check to see if file has changed?
    #Should we check to see if file has been saved?

.te.edit1 delete 1.0 end }

The editor developed here is rudimentary, but effective. To make it into a serious tool clearly requires more careful consideration to file handling. Cut and paste facilities based on mouse selection can be provided easily, and at the present time there seems no need to provide a multiple windows facility, since the text widget itself appears likely to include this in the near future. The simple editor developed here could be adequate for inclusion in other applications, given some tolerance on the part of the user,



Previous: A simple browser application
Up: Example applications
Next: A simple timer
Previous Page: A simple browser application
Next Page: A simple timer

csstddm@brunel.ac.uk
Fri Aug 19 16:55:19 BST 1994