Version 4 of Extending the eTcl console

Updated 2006-02-06 11:45:32 by suchenwi

Richard Suchenwirth 2006-02-05 - As I've decided to build my PocketPC "IDE patchwork", under the ceremonial name of Sepp, based on eTcl, here are notes on how components can be plugged in for easy reach: in the console menus, and in the toolbar.

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

First it is important to know that we're dealing with two "sibling" interpreters here, call them "Main" and "Console" neither being the slave of the other, and each being able to run code in the other:

   set mainres [console eval $consolecode]
   console eval {set consoleres [consoleinterp $maincode]}

This makes coding for both just a little bit more complicated.

Example, to add an item into an existing menu of the console (from htext as eTcl plugin):

 console eval {.menubar.help add command -label Help \
               -command {consoleinterp eval {htext::htext .h}}}

Create a new main item in the console menu:

Here is a window selector that lists in an extra "Windows" menu the toplevels that are currently there, and raises the one selected. The menu is recreated every time a window gets a <Map> or <Destroy> event, so that it always stays up-to-date.

 proc toplevels {} {
    set res {}
    foreach w [winfo children .] {
       if {[winfo toplevel $w] eq $w} {
          lappend res $w [wm title $w]
       }
    }
    set res
 }
 console eval {
    if ![winfo exists .menubar.w] {
      .menubar insert 4 cascade -menu [menu .menubar.w] -label Windows
      proc winsel m {
         $m delete 0 end
         $m add command -label Console \
             -command {wm dei .; focus .}
         $m add separator
         foreach {w title} [consoleinterp eval toplevels] {
            $m add command -label $title -command \
                "consoleinterp eval {wm dei $w; focus $w}"
         }
      }
   }
 }
 proc WinSel'update {} {
     console eval {winsel .menubar.w}
 }
 bind all <Map>      WinSel'update
 bind all <Destroy> {after 1 WinSel'update}

With good old introspection I also found out how to add items to the toolbar that appears below the main console window. You just need to hand a 16x16 GIF icon (e.g. picked from iconview as eTcl plugin) to the console interp, and pack a button with it into the toolbar (see screenshot above). As proof of concept, I picked the "house" icon and associated it with the trivial operation "change to HOME directory", which cd without arguments does anyway:

 proc home.button {} {
   console eval {
    image create photo  gohome-16 -data {
   R0lGODlhEAAQAIUAAPwCBDw6PBQWFCQiJAQCBFxeXMTCxJyanDwyLDQqLFRS
   VLSytJSSlISChCQmJERGRFRWVGxubKSmpJyenGRmZLy+vOzq7OTi5Ly6vGRi
   ZPTy9Pz6/OTm5ExOTPT29BwaHNza3NS6tJRqRGQqBNy6pIyKjDwGBPTe1JSW
   lDQyNOTGrNRiBGwmBIRaLNymdLxWBHxGFNySXCwqLKyqrNR6LKxGBNTS1NTW
   1Jw+BEweDDQ2NAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAQABAAAAao
   QIBwCAgIiEjAgAAoGA6I5DBBUBgWjIZDqnwYGgVIoTGQQgyRiGRCgZCR1nTF
   csFkHm9hBp2paDYbHAsZHW9eERkYGh4eGx4ag3gfSgMTIBshIiMkGyAlCCZT
   EpciJyQjGxcoKUQBEhcbIiorLB4XEltDrhcaLS4vtbcJra8bMDHAGrcyrTMX
   HjA0NSypEsO6EzY3IzU4OdoTzK0BCAkDMgkIOjJlAH5BACH+aENyZWF0ZWQg
   YnkgQk1QVG9HSUYgUHJvIHZlcnNpb24gMi41DQqpIERldmVsQ29yIDE5OTcs
   MTk5OC4gQWxsIHJpZ2h0cyByZXNlcnZlZC4NCmh0dHA6Ly93d3cuZGV2ZWxj
   b3IuY29tADs=
    }
    pack [button .toolbar.home -image gohome-16 -bg #C0C0EE -relief groove\
      -highlightthickness 0 \
      -command {consoleinterp eval {cd ~; puts [pwd]}}] \
   -side left
    }
 }

Category Development - Arts and crafts of Tcl-Tk programming