| GRIDPLUS2 - Example 7 | ![]() |
||||||
|
| Example Of a Complete Simple Application |
This is an example of a complete text editor application. This example is mainly intended to demonstrate the GRIDPLUS entry/text clipboard commands, text find facilities and enhanced menu options.
This example assumes familiarity with the previous/earlier examples. The more basic GRIDPLUS usage is not described here in full.
| User Guide |
Main Window:
The toolbar contains six buttons, each has appropriate pop-up/balloon help:-
![]() | Start a New file. |
![]() | Open an existing file. |
![]() | Save file. |
![]() | Cut selected text. |
![]() | Copy selected text. |
![]() | Paste text from clipboard. |
![]() | Find text matching string entered into entry to the right of this button. |
The function of the various options follow general conventions and will not be described here.
When a file is opened/saved a screen similar to the following will be displayed:-
| Source Code |
#==========Create/display GUI.
proc main {} {
gridplus menu . {
_File {
{_New ~new}
{_Open ~openfile}
{_Save ~save}
{"Save _As" ~saveas}
=
{_Quit ~exit}
}
_Edit {
{Cu_t ~gpcut}
{_Copy ~gpcopy}
{_Paste ~gppaste}
{_Delete ~gpclear}
=
{_Find "~gpfind_dialog .text"}
=
{"_Select All" ~selectall}
}
_Help {
{_About ~about}
}
}
gridplus button .toolbar -style Toolbutton -padding 0 -space 0 -takefocus 0 {
{:filenew22 .new ?New ~new} \
{:fileopen22 .open ?Open ~openfile} \
{:filesave22 .save ?Save ~save} \
| \
{:editcut22 .cut ?Cut ~gpcut} \
{:editcopy22 .copy ?Copy ~gpcopy} \
{:editpaste22 .paste ?Paste ~gppaste} \
| \
{:filefind22 .find ?Find ~find} \
{{&we: {.string 20 ~find }} .find}
}
gridplus text .text -font {courier 8} -scroll xy -width 80 -height 25 -wrap none
gridplus grid .statusbar -relief groove {
{"^File: " .file} {}
}
gridplus layout .main -wtitle gpEdit {
.toolbar
.text
.statusbar:ew
}
gpset .statusbar,file Untitled
pack .main
}
#==========Clear/initialise for new edit.
proc new {} {
gpset .text {}
gpset .statusbar,file Untitled
}
#==========Open file for editing.
proc openfile {} {
set filename [tk_getOpenFile -title "gpEdit: Open File"]
if {$filename ne ""} {
set file [open $filename r]
gpset .text [read $file]
close $file
gpset .statusbar,file $filename
}
}
#==========Save text to current file.
proc save {} {
global {}
if {$(.statusbar,file) != "Untitled"} {
set file [open $(.statusbar,file) w]
puts $file $(.text)
close $file
} else {
saveas
}
}
#==========Save text to a new file.
proc saveas {} {
global {}
set filename [tk_getSaveFile -title "gpEdit: Save File"]
if {$filename ne ""} {
set file [open $filename w]
puts $file $(.text)
close $file
gpset .statusbar,file $filename
}
}
#==========Find next occurance of specified string in text.
proc find {} {
global {}
gpfind .text $(.find,string)
}
#==========Select all text.
proc selectall {} {
.text.text tag add sel 1.0 end
}
#==========Display "About" dialog.
proc about {} {
tk_messageBox -icon info -title "gpEdit: About" -message "gpEdit - Adrian Davis 2008" -type ok
}
#==========Start application.
main
| Comments |
proc main {} {
gridplus menu . {
_File {
{_New ~new}
{_Open ~openfile}
{_Save ~save}
{"Save _As" ~saveas}
=
{_Quit ~exit}
}
_Edit {
{Cu_t ~gpcut}
{_Copy ~gpcopy}
{_Paste ~gppaste}
{_Delete ~gpclear}
=
{_Find "~gpfind_dialog .text"}
=
{"_Select All" ~selectall}
}
_Help {
{_About ~about}
}
}
The underline characters are used to specify that the character following the underline will be displayed as underlined. The menu option can be selected by pressing the indicated character.
Pressing the "Alt" key will activate menu keyboard traversal.
The gpcut/gpcopy/gppaste/gpclear procudures are GRIDPLUS clipboard/edit commands. As the name of the item to which the command applies is not specified, the commands will operate on the widget which current has focus (if it is either an entry or a text widget). In this case either the ".text" text widget or the ".find,string" entry.
The gpfind_dialog procedure invokes the GRIDPLUS "Find" dialog.
gridplus button .toolbar -style Toolbutton -padding 0 -space 0 -takefocus 0 {
{:filenew22 .new ?New ~new} \
{:fileopen22 .open ?Open ~openfile} \
{:filesave22 .save ?Save ~save} \
| \
{:editcut22 .cut ?Cut ~gpcut} \
{:editcopy22 .copy ?Copy ~gpcopy} \
{:editpaste22 .paste ?Paste ~gppaste} \
| \
{:filefind22 .find ?Find ~find} \
{{&we: {.string 20 ~find}} .find}
}
For Example: "{:filenew .new ?New ~new}".
This button will invoke a procedure called "new", will have the ICONS image called "filenew22", and has "New" as the pop-up/balloon help.
Next to the "Find" button is an embedded entry grid called ".find".
For Example: "{{&we: {.string 20 ~find}} .find}"
Creates an embedded entry widget grid "&we:". The characters after the colon (until the next space) specify the name of the style/optionset to be used for the grid. In this case the name is null, which causes the default style to be used. This is neccessary here because the button grid is set to use the "Toolbutton" style (which would normally be inhereted by the embedded widget grid).
The grid contains one entry called ".string" which is "20" characters wide. This content of this entry can be accessed as "$(.find,string)". The "~find" option causes the "find" procedure to be invoked if the Enter/Return key is pressed while the entry has keyboard focus. This is the same procedure as will be invoked by the "Find" toolbar button.
The gpcut/gpcopy/gppaste procudures are GRIDPLUS clipboard/edit commands. As the name of the item to which the command applies is not specified, the commands will operate on the widget which current has focus (if it is either an entry or a text widget). In this case either the ".text" text widget or the ".find,string" entry.
gridplus text .text -font {courier 8} -scroll xy -width 80 -height 25 -wrap none
The GRIDPLUS text widget, which by default is editable, has a right-click pop-up Cut/Copy/Paste/Find menu.
gridplus grid .statusbar -relief groove {
{"^File: " .file} {}
}
The grid has one row with two columns. The first column has a text label "File". The caret character prefix causes the label to be displayed in bold text. To the right of the label is a another label called ".file" - the value of this label (.statusbar,file) can be set using the GRIDPLUS gpset command.
gridplus layout .main -wtitle gpEdit {
.toolbar
.text
.statusbar:ew
}
gpset .statusbar,file Untitled
pack .main
The ".statusbar,file" text is set to "Untitled"
proc new {} {
gpset .text {}
gpset .statusbar,file Untitled
}
proc openfile {} {
set filename [tk_getOpenFile -title "gpEdit: Open File"]
if {$filename ne ""} {
set file [open $filename r]
gpset .text [read $file]
close $file
gpset .statusbar,file $filename
}
}
proc save {} {
global {}
if {$(.statusbar,file) != "Untitled"} {
set file [open $(.statusbar,file) w]
puts $file $(.text)
close $file
} else {
saveas
}
}
If the data hasn't been been read from a file the statusbar filename will be "Untitled", in this case the "saveas" procedure will be called.
proc saveas {} {
global {}
set filename [tk_getSaveFile -title "gpEdit: Save File"]
if {$filename ne ""} {
set file [open $filename w]
puts $file $(.text)
close $file
gpset .statusbar,file $filename
}
}
proc find {} {
global {}
gpfind .text $(.find,string)
}
The GRIDPLUS gpfind command is used to find the next occurance of ".find,string" in ".text".
proc selectall {} {
.text.text tag add sel 1.0 end
}
proc about {} {
tk_messageBox -icon info -title "gpEdit: About" -message "gpEdit - Adrian Davis 2008" -type ok
}
main