Text Editor Project

written 22 July 1999 being revised May 2006
Google site search

ContentsIntroductionBasicIntermediateAdvancedFuturePolicyInfrastructure

1   Introduction

1.1   Overview

This is a major programming project for 1st, 2nd or 3rd Year High School Students specialising in learning about computer programming.

Each student works alone to produce his/her own text editor program. The source of this program can then be used as a basis for other more complex programs.

This project can be done by a single student as a personal project, or by a class.

Most of the project tuition is taken up in explaining computer user interface concepts such as windows, selection, focus, widgets, and file access. The project shows how these ideas are implemented in the programming language Tcl.

1.2   Resources Required

A PC type computer and the freely available Tcl interpreter are required. A small slow computer is adequate for this project for a small group of students. Any of the UNIX, Windows or Mac OS's can be used and the product will port and run on all these systems.

1.3   Prerequiste knowledge

The students should already be familiar with the basic concepts of Tcl. They should have completed a webpage Tcl project such as a Tcl Webpage Guestbook.

2   Lesson 1 - Creating the text widget and scroll bars

The first stage is 4 lines of code which create a text widget with scrollbar. We add a title window in the three lines above and also some text to display within the text widget.
#texteditor: create a text widget with scroll bar, title and help text
set title "My Text Editor 1"
wm title . $title
wm iconname . $title

text .text -relief raised -bd 2 -height 8 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".text yview"
pack .text -side left -fill both -expand 1
pack .scroll -side left -fill y

.text insert 1.0 "The default bindings for text widgets allow you to manipulate 
the text in a number of ways:

You can scroll the text with the scrollbar or by scanning with mouse button 2 
in the text.

You can edit the text. For example, click with mouse button 1 to set the 
insertion cursor, then type new characters.

You can select information by dragging with mouse button 1. 

In general the default bindings follow the conventions of the host operating system. 
See the reference documentation for text widgets for complete information 
on the default bindings.

On Windows systems 
    <Ctl>-c copies the selection to the Windows clipboard 
    <Ctl>-x copies the selection to the Windows clipboard and deletes the selection
    <Ctl>-v pastes the Windows clipboard contents in place of the current selection"
Students may be surprised that they already have a tiny program that communicates with the rest of their computer system.

3   Lesson 2 - Creating the Menu Bar and New, Open, Save and Save As File Buttons, and the Help function

The first stage in this lesson is to create the Menu Bar and populate it with buttons. Commands are added for New and Help. Notice that the newFile procedure is used to initialise the application as well as to start a new file when the New button is pushed.

The Help command invokes the help procedure. This is a use of a transient child window in an application. Here we reuse the text and scrollbar code from the first session to create the Help window.

#texteditor: edit text in a text widget

set title "My Text Editor 2"

frame .menu
pack .menu -side top -fill x

button .menu.new -text New  -command newFile
button .menu.open -text Open  
button .menu.save -text "Save" 
button .menu.saveas -text "Save As" 
pack .menu.new .menu.open .menu.save .menu.saveas -side left
button .menu.help -text Help -command help
pack .menu.help -side right

text .text -relief raised -bd 2 -height 20 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".text yview"
pack .text -side left -fill both -expand 1
pack .scroll -side left -fill y

proc newFile {} { global filename title
   .text delete 1.0 end
   set filename ""
   wm title . $title
   wm iconname . $title
}

newFile

proc help {} { global title
    if  [winfo exists .help]  return
    toplevel .help
    wm title .help "$title Help"
    wm transient .help .
    text .help.text -relief raised -bd 2 -height 12 -yscrollcommand ".help.scroll set"
    scrollbar .help.scroll -command ".help.text yview"
    pack .help.text -side left -fill both -expand 1 
    pack .help.scroll -side right -fill y
    .help.text insert 1.0 "Editing commands

Button-1 sets the insertion cursor 
Button-1 and drag selects a sequence of letters
Double-Button-1 selects a word      	
Double-Button-1 and drag selects a sequence of words
Triple-Button-1] selects a line		
Triple-Button-1 and drag selects a sequence of lines
Ctl-c copies the current selection to the clipboard
Ctl-x copies the current selection to the clipboard 
		and deletes the current selection
Ctl-v pastes the clipboard at the insertion cursor"
}
Notice that the Help Window does not iconify but can still be resized. Code in the help procedure prevents the window being created twice which would cause a window naming error. Notice that the Help window can be closed by itself. Note that if the Help Window is open when the Editor is closed or iconised, that the Help Window is closed or iconised with the Editor Window. Students may be aware that with many Windows programs the Help Window gets left behind.

As far as this exercise is concerned the Help function is complete and further examples will show a stub for the help procedure. But students should write up complete documentation in the Help Text widget as the Project proceeds.

4   Lesson 3 - Creating File Open Save and Save As Functions

Next the File Open Save and Save As functions are added. Here again platform-independant features are available.
#texteditor: edit text in a text widget

set initialdir "/"
proc fileDialog {w operation} { global initialdir
    #   Type names		Extension(s)	Mac File Type(s)
    #
    #---------------------------------------------------------
    set types {
	{"Text files"		{.txt .doc}	}
	{"Tcl Scripts"		{.tcl .tk}		}
	{"C Source Files"	{.c .h}		}
	{"All Source Files"	{.tcl .tk .c .h}	}
	{"All files"		*}
    }
    if {$operation == "open"} {
	set file [tk_getOpenFile -filetypes $types \
		-parent $w -initialdir $initialdir]
    } else {
	set file [tk_getSaveFile -filetypes $types \
 		-parent $w -initialdir $initialdir \
		-initialfile Untitled -defaultextension .tk]
        
    }
    if {$file != ""} {set initialdir [file dirname $file]}
    return $file
}

proc newFile {} { global filename title
# check that previous contents have been saved
   .text delete 1.0 end
   set filename {}
   wm title . $title
   wm iconname . $title
}

proc loadFile file { global filename title
   if {$file != ""} {
   set filename $file
   .text delete 1.0 end
   catch [set f [open $file]] msg
   while {![eof $f]} {
      .text insert end [read $f 1000]
   }
   close $f
   wm title . "$title - $filename"
   wm iconname . "$title - $filename"
    }
}

proc saveFile file { global filename title
   set filename $file
   set f [open $file w]
   scan [.text index end] %d numLines
   for {set i 1} {$i < $numLines} {incr i} {
       puts $f [.text get $i.0 "$i.0 lineend"]
   }
   close $f
   wm title . "$title - $filename"
   wm iconname . "$title - $filename"
}

set title "My Text Editor 5"
frame .menu
pack .menu -side top -fill x

button .menu.new -text New -command {newFile}
button .menu.open -text Open  -command {loadFile [fileDialog . open]} 
button .menu.save -text "Save" -command {saveFile $filename} 
button .menu.saveas -text "Save As" -command {saveFile [fileDialog . save]} 
pack .menu.new .menu.open .menu.save .menu.saveas -side left
button .menu.help -text Help
pack .menu.help -side right

text .text -relief raised -bd 2 -height 20 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".text yview"
pack .text -side left -fill both -expand 1
pack .scroll -side left -fill y

newFile

5   Lesson 5 - Creating the Find function

6   Lesson 6 - Creating the Substitute function

7   Lesson 7 - Fine Tuning the product - Default and Configuration Options

Students will see many areas where the product can be configured for easier and safer use. Another way to usefully extend this program is to provide browser extensions for one or more of the types of text files that it is used to edit. The most obvious example is xhtml.

It is important not to overdevelop this product. It should remain simple with an emphasis on ease of use rather than having a lot of facilities added to it. While this program is the basis for others such as a Word Processor, this program is for editing pure text files that must not be encumbered with extraneous mark-up language.

Students and the class can decide which if the programs written, they like and want to install on their computers at home or in the class. The program can be made the default editor for a class of file types such as text of tcl files.

8   Lesson 8 - Possibilities for specialist editors

This is a brainstorming session which may come up with some ideas for a Class project for a Senior High School year. Students should each write down various ideas for specialist editor programs.

One obvious example, arising from this project, is an editor for the Help pages for applications written in Tcl, based on the Tcl text widget and its tag facilities. Students short on ideas could be asked to write a description of such a product in as much detail as possible.

Even for the youngest students doing this project there are simple options which can be attempted such as expanding the "New" button to prodive for special document types. An obvious candidate is the HTML page. A pop-up window is created which asks for some basic information such as the TITLE and optionally also the author, email address, the name of a style sheet to be included and whatever else the student might wish to use to document the webpages. A file with the basic tags is then created together with copyright footer, heading and so on.

Other options are specially formatted pages for submitting homework in various forms such as writing up chemistry or physics experiments.

©2000 - 2006 WEBSCOOL This page last updated 11 May 2006. All rights reserved - including copying or distribution of any portion of this document in any form or on any medium without authorisation. For more regarding the copyright.