Monday, September 13, 2010

 Organize the code so it doesn't depend on globals but puts data in namespaces and code in another namespace.  Separate creation of widgets from all the other logic.  And add support for Unix lauching by embedded command.   And you get.

#!/bin/sh

# \
exec wish "$0" ${1+"$@"}
# The /bin/sh stuff is for Unix systems to so you can launch it by "> editor.tcl"
package require Tk ; # Make sure the Tk library is loaded
namespace eval ::ej {                         ; # Create a namespace for our editor
   namespace eval text000 {                   ; # Create a namespace for instance data
      set top ""                              ; # If we have widgets above .t put it here
      set rightScrollbar ${top}.y             ; # Name of right scrollbar
      set textWin ${top}.t                    ; # Name of text widget
   }
   proc getV {this name} { return [set ${this}::${name}] } ; # Get value from ::ej::text000 namespace
   proc setup {this} {                        ; # Setup widgets
      catch { console show } str                 ; # Show a console for command input
      set sb [getV $this rightScrollbar]         ; # Get name of scrollbar
      set tx [getV $this textWin]                ; # Get name of text widget
      scrollbar $sb -command "$tx yview"         ; # create a scrollbar
      text $tx -yscrollcommand "$sb set"         ; # create a text window


      pack $sb -side right -fill y               ; # put the scrollbar on right
      pack $tx -side right -fill both -expand 1  ; # create a text window
   }
   proc loadText {this fn} {                  ; # routine to load text
      if {$fn==""} return                        ; # check for bad input
      set w [getV $this textWin]                 ; # get name of text widget
      global fileName                            ; # make a global named fileName
      set fileName $fn                           ; # save file name for later
      wm title . [file tail $fn]                 ; # set title of window to filename
      set fp [open $fn]                          ; # open the file
      $w delete 1.0 end                          ; # delete any current text
      $w insert end [read $fp]                   ; # insert new text
      close $fp                                  ; # close file
   }
   proc saveText {this fn} {                 ; # routine to save text
      if {$fn==""} return                        ; # check for bad input
      set w [getV $this textWin]                 ; # get name of text widget
      set fp [open $fn w]                        ; # open file
      puts -nonewline $fp [$w get 1.0 end]       ; # write to file
      close $fp                                  ; # close file
   }
   proc main {this argv} {                   ; # startup routine
      set tx [getV $this textWin]                ; # get name of text widget
      if {[llength $argv] > 0} {                 ; # if started with filename
          loadText $tx [lindex $argv 0]          ; # load file
      }
      focus -force $tx                           ; # give text window the focus
   }
}


set name ::ej::text000


::ej::setup $name
::ej::main $name $argv

No comments:

Post a Comment