Version 1 of encoview as eTcl plugin

Updated 2006-01-20 09:56:26 by suchenwi

Richard Suchenwirth 2006-01-20 - As eTcl comes with all the encodings that regular Tcl offers, I felt tempted to write a little plugin for inspecting such codepages (currently only for 8-bit encodings, with codes 0..255). Also added was a generic listbox to select a name. When a codepage is displayed, you can click on a character to see it and its hex value in the title bar.

http://mini.net/files/encoview.jpg


 proc encoview {{name ""}} {
   if {$name eq ""} {
      set name [lb'select [lsort [encoding names]] "Select encoding"]
   }
   if {$name ne ""} {
      wce siphide
      set w [toplevel .[clock click]]
      wm title $w $name
      bind $w <FocusOut> {destroy %W}
      pack [canvas $w.c -bg lightyellow]

      set 0_15 {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
       set x 26
       set y 6
      foreach col $0_15 {
         $w.c create text $x $y -text [format %X $col] -fill blue
         incr x 13
      }
      incr y 12
      foreach row $0_15 {
         set x 10
         $w.c create text $x $y -text [format %X0 $row] -fill blue
         incr x 16
         foreach col $0_15 {
            set i  [expr $row*16+$col]
            set c [encoding convertfrom $name [format %c $i]]
            $w.c create text $x $y -text  $c -tag [list $c = [format 0x%02x $i]] -font {{Bitstream Cyberbit} 10}
             incr x 13
         }
         incr y 16
      }
   }
   $w.c bind = <1> {encoview'title %W}
   raise $w; focus -force $w
 }

#-- Display the tags of the current item (which happen to look like "X = 0x58"

 proc encoview'title w {
   set c [lrange [$w gettags current] 0 2]
   wm title [winfo parent $w] $c
 }

#-- Generic listbox selector, will go in a different file later

 proc lb'select {list {title "Select one:"}} {
   set w [toplevel .[clock click]]
   wm title $w $title
   bind $w <FocusOut> {set _ ""}
   listbox $w.l -yscr "$w.y set" -font {Tahoma 9}
   scrollbar $w.y -comm "$w.l yview"
   pack $w.y $w.l -side right -fill y -padx 3 -pady 3
   eval $w.l insert end $list
   bind $w.l <ButtonRelease-1> {set ::_ [%W get @%x,%y]}
   raise $w; focus -force $w
   vwait ::_
   catch {destroy $w}
   return $::_
 }