GRIDPLUS2 - Example 7
Home Reference Manpage Examples Download License Contact

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 {} {
Create a procedure called "main" to display the main/root window.
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}
   }
}

Create a menu called ".". Each menu option has a name and the name of the procedure to be called when the option is selected.

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} 
}

Creates a GRIDPLUS button grid called ".toolbar" using the "Toolbutton" style. Each button has an icon, explicitly defined name, associated pop-up/balloon help and a procedure to invoke.

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
Creates a text widget called ".text", which is "80" characters wide and "25" lines high. It has "xy" direction (vertical and horizontal) scrollbars. The font is set to "courier 8" and the word wrap is set to "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} {}
}
Creates a GRIDPLUS grid called ".stausbar" with "groove" style relief.

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
Lays out the GRIDPLUS elements for the root/main window and displays the window contents.

The ".statusbar,file" text is set to "Untitled"


proc new {} {
   gpset .text {}
   gpset .statusbar,file Untitled
}
Creates a procedure called "new" to set the contents of ".text" to null and the statusbar filename to "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
   }
}
Creates a procedure called "openfile". This procedure uses the "tk_getOpenFile" dialog to select a file for editing. If a file is selected, the file is opened, the value of ".text" is set to the contents of the file, the file is then closed and the value of the statusbar filename is set to the name of the file which has been selected.


proc save {} {
   global {}

   if {$(.statusbar,file) != "Untitled"} {
      set file [open $(.statusbar,file) w]
      puts $file $(.text)
      close $file
   } else {
      saveas
   }
}
Creates a procedure called "save". This procedure saves the contents of ".text" to the file from which it was read.

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
   }
}
Creates a procedure called "saveas". This procedure uses the "tk_getSaveFile" dialog to set/select the name under which the contents of ".text" is to be saved. If a filename is set/selected, the file is opened, the value of ".text" is set to the contents of the file, the file is then closed and the value of the statusbar filename is set to the name of the file which has been set/selected.


proc find {} {
   global {}

   gpfind .text $(.find,string)
}
Creates a procedure called "find". This procedure is called when the toolbar "Find" button is pressed -or- when the enter key is press when the ".find,string" toolbar entry has focus.

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
}
Creates a procedure called ".selectall". This procedure selects all of the data in ".text".


proc about {} {
   tk_messageBox -icon info -title "gpEdit: About" -message "gpEdit - Adrian Davis 2008" -type ok
}
Creates a procedure called "about". This procedue displays the "About" dialog.


main
Runs the "main" procedure to create the gpEdit display.


Copyright © 2008 Adrian Davis.