Tuesday, September 14, 2010

# ==========================================================
# Add standard file and directory dialog
# ==========================================================
namespace eval ::ej {
   namespace eval text000 {
       set fileName                 ""           ; # current file name
       set default_dir              [pwd]        ; # which directory to save/load files
       ; # File extensions
       set fileTypes     {                      
            {{Text Files} {.txt}}
            {{All Files} * }
       }
   }
}

proc ::ej::fileOpen {this} {
    set file_name [tk_getOpenFile \
                   -initialdir [getV $this default_dir] \
                   -filetypes [getV $this fileTypes]    \
                   -initialfile [getV $this fileName]]
    if {[string compare $file_name {} ] == 0}  {return}
    loadText $this $file_name
    set ${this}default_dir [file dirname $file_name]
    set ${this}fileName $file_name
}

proc ::ej::fileSaveAs {this} {
    set file_name [tk_getSaveFile \
                     -initialdir [getV $this default_dir] \
                     -filetypes [getV $this fileTypes]    \
                     -initialfile [getV $this fileName]]
    if {[string compare $file_name {} ] == 0} then {return}
    saveText $this $file_name
    set ${this}default_dir [file dirname $file_name]
    set ${this}fileName  $file_name
}

proc ::ej::setDefaultDir {this} {
    set dir [tk_chooseDirectory  -initialdir [getV $this default_dir]]
    if {$dir != ""} {
       set ${this}default_dir $dir
    }
}

# =============================================================
# =============================================================

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

Saturday, September 11, 2010

Writting an editor


Writting an editor
Have you ever written an editor? I am going to show you how to write an editor which will work without recompiling on Windows, Linux, AIX, solaris, and Mac.  An editor which can safely handle unicode encoded languages and is both fast and small. 
Here is your editor.   
# In file named editor.tcl
package require Tk                                     ; # Make sure the Tk library is loaded

catch { console show } str                             ; # Show a console for command input

set rightScrollbar .sb                                 ; # symblic name of scrollbar
set textWin .text                                      ; # symbolc name of text window

scrollbar $rightScrollbar -command "$textWin yview"    ; # create a scrollbar
text $textWin -yscrollc "$rightScrollbar set"          ; # create a text window

pack $rightScrollbar -side right -fill y               ; # put the scrollbar on right
pack $textWin -side right -fill both -expand 1         ; # fill the rest of window with text

proc loadText {w fn} {                                 ; # routine to load text
    if {$fn==""} return                                    ; # check for bad input
    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 {w fn} {                                 ; # routine to save text
    if {$fn==""} return                                    ; # check for bad input
    set fp [open $fn w]                                    ; # open file     
    puts -nonewline $fp [$w get 1.0 end]                   ; # write to file
    close $fp                                              ; # close file
}

if {$argc > 0} {                                       ; # if started with filename
    loadText $textWin [lindex $argv 0]                     ; # load file
} else {
}
focus -force $textWin                                  ; # give text window the focus

Now you can edit a file with the command "wish editor.tcl fileToExit.txt".
The language used is Tcl.  Got to ActiveState and follow the directions to download and install for your platform (http://www.activestate.com/activetcl).
  

Thursday, September 2, 2010