home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / tcl / 1030 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  8.0 KB

  1. Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!dssv01!kennykb
  2. From: kennykb@dssv01.crd.ge.com (Kevin B. Kenny)
  3. Newsgroups: comp.lang.tcl
  4. Subject: Re: Tcl interaction in my own window
  5. Message-ID: <1992Jul22.214708.25628@crd.ge.com>
  6. Date: 22 Jul 92 21:47:08 GMT
  7. References: <PRZEMEK.92Jul21120718@rrdstrad.nist.gov> <1992Jul21.222943.5328@cpu.com>
  8. Sender: usenet@crd.ge.com (Required for NNTP)
  9. Reply-To: kennykb@crd.ge.com
  10. Organization: GE R&D, Information Architectures & Management Program
  11. Lines: 249
  12. Nntp-Posting-Host: dssv01.crd.ge.com
  13.  
  14.  
  15. In article <PRZEMEK.92Jul21120718@rrdstrad.nist.gov>, przemek@rrdstrad.nist.gov
  16. (Przemek Klosowski) writes:
  17. >  Now, the problem is that the errors are still
  18. >printed to the shell window.  Is there a way of intercepting Tcl error
  19. >messages/printouts to put them, let say, in a separate message window?
  20.  
  21. I think that the following widget does everything you're trying to
  22. accomplish, including trapping and handling all the errors.  Printouts
  23. can't be caught readily, but you can use `commandwindow.print' rather
  24. than `puts stdout' to print in the command window.
  25.  
  26. If I left out any proc's that you need, drop me a line and I'll send
  27. them to you.
  28.  
  29. proc commandwindow {{w .commandwindow}} {
  30.     global commandwindow
  31.     set commandwindow $w
  32.     toplevel $w -class CommandWindow
  33.     wm group $w .
  34.     wm withdraw $w
  35.     wm deiconify $w
  36.     wm title $w [option get $w title Title]
  37.     frame $w.top
  38.     label $w.top.label -text "Command:"
  39.     entry $w.top.entry -relief sunken
  40.     bindentry $w.top.entry "commandwindow.eval $w"
  41.     pack append $w.top $w.top.label {left} \
  42.         $w.top.entry {right expand fillx}
  43.     label $w.vallabel -text Value
  44.     frame $w.mid
  45.     scrollbar $w.mid.scroll -command "$w.mid.value view"
  46.     listbox $w.mid.value -scroll "$w.mid.scroll set" -relief sunken
  47.     pack append $w.mid $w.mid.value {left expand fill} \
  48.         $w.mid.scroll {right filly frame e}
  49.     label $w.scriptlabel -text Transcript
  50.     frame $w.bot
  51.     scrollbar $w.bot.scroll -command "$w.bot.transcript view"
  52.     listbox $w.bot.transcript -scroll "$w.bot.scroll set" -relief sunken
  53.     pack append $w.bot $w.bot.transcript {left expand fill} \
  54.         $w.bot.scroll {right filly frame e}
  55.     pack append $w $w.top {top frame n} \
  56.             $w.vallabel {top fillx} \
  57.             $w.mid {top} \
  58.             $w.scriptlabel {top fillx} \
  59.             $w.bot {bottom frame s}
  60.  
  61.     # Focusing in response to the Enter event is wrong for the
  62.     # active focus model, but the Tk 1.3 library does not reliably return
  63.     # FocusIn events.
  64.  
  65.     bind $w <Any-Enter> "focus $w.top.entry"
  66.     bind $w <Any-FocusIn> "focus $w.top.entry"
  67. }
  68.  
  69. # commandwindow.eval -- evaluate the command typed in $w's entry box
  70.  
  71. proc commandwindow.eval {w command} {
  72.     global errorInfo
  73.     $w.bot.transcript view end
  74.     $w.bot.transcript insert end "$command"
  75.     catch { $w.top.entry delete 0 end }
  76.     catch {$w.mid.value delete 0 end}
  77.     set erreur [catch {uplevel #0 $command} value]
  78.     foreach line [split $value \n] {
  79.         $w.mid.value insert end $line
  80.     }
  81.     if $erreur {
  82.         foreach line [split $errorInfo \n] {
  83.             $w.bot.transcript insert end [format ":   %s" $line]
  84.         }
  85.     } else {
  86.         foreach line [split $value \n] {
  87.             $w.bot.transcript insert end [format "=   %s" $line]
  88.         }
  89.     }
  90. }
  91.  
  92. # commandwindow.print -- print a message in the most recently created command
  93. # window.
  94.  
  95. proc commandwindow.print args {
  96.     global commandwindow
  97.     $commandwindow.bot.transcript view end
  98.     foreach line [split [join $args " "] \n] {
  99.         $commandwindow.bot.transcript insert end [format "-  %s" $line]
  100.     }
  101. }
  102.  
  103. # commandwindow.trace -- trace a variable in the command window
  104.  
  105. proc commandwindow.trace {name {ops wu}} {
  106.     uplevel trace variable $name $ops commandwindow.showtrace
  107. }
  108.  
  109. # commandwindow.untrace --untrace a variable in the command window.
  110.  
  111. proc commandwindow.untrace {name {ops wu}} {
  112.     uplevel trace vdelete $name $ops commandwindow.showtrace
  113. }
  114.  
  115. # commandwindow.showtrace -- format trace output in command window
  116.  
  117. proc commandwindow.showtrace {name sub op} {
  118.     set msg [\
  119.         case $op in {
  120.             r "Reading"
  121.             w "Writing"
  122.             u "Unsetting"
  123.             default [format "Performing unknown operation %s on" \
  124.                     op]
  125.         }\
  126.     ]
  127.     append msg " $name"
  128.     if {$sub != ""} {
  129.         append msg "($sub)"
  130.         catch { uplevel set $name($sub) } val
  131.     } else {
  132.         catch { uplevel set $name } val
  133.     }
  134.     if {$op != "u"} {
  135.         append msg " = $val"
  136.     }
  137.     commandwindow.print $msg
  138. }
  139.  
  140. option add *CommandWindow*entry.font fixed widgetDefault
  141. option add *CommandWindow*value.font fixed widgetDefault
  142. option add *CommandWindow*transcript.font fixed widgetDefault
  143. option add *CommandWindow*entry.width 72 widgetDefault
  144. option add *CommandWindow*value.geometry 80x5 widgetDefault
  145. option add *CommandWindow*transcript.geometry 80x10 widgetDefault
  146.  
  147. # Do keyboard and mouse bindings for an entry box.
  148.  
  149. proc bindentry {w {command eval}} {
  150.     bind $w <Any-KeyPress> {%W insert cursor "%A"; entry.cursor %W}
  151.     bind $w <space> {%W insert cursor " "; entry.cursor %W}
  152.     bind $w <Control-a> {%W cursor 0; %W view 0}
  153.     bind $w <Control-b> {entry.cursorleft %W}
  154.     bind $w <Control-d> {entry.deleteright %W}
  155.     bind $w <Control-e> {%W cursor end; entry.cursor %W}
  156.     bind $w <Control-f> {entry.cursorright %W}
  157.     bind $w <Control-h> {entry.deleteleft %W}
  158.     bind $w <Control-j> "$command \[%W get\]"
  159.     bind $w <Control-k> {%W delete cursor end; entry.cursor %W}
  160.     bind $w <Control-l> {entry.center %W}
  161.     bind $w <Control-m> "$command \[%W get\]"
  162. # Anyone volunteer to write the next one?
  163. #    bind $w <Control-t> {entry.twiddle %W}
  164.     bind $w <Control-u> {%W delete 0 end; %W view 0}
  165.     bind $w <Control-w> {%W delete sel.first sel.last; entry.cursor %W}
  166.     bind $w <Control-y> {%W insert cursor [selection get]; entry.cursor %W}
  167.     bind $w <BackSpace> {entry.deleteleft %W}
  168.     bind $w <Delete> {entry.deleteleft %W}
  169.     bind $w <Linefeed> "$command \[%W get\]"
  170.     bind $w <Return> "$command \[%W get\]"
  171.     # quote, backslash, and left bracket need
  172.     # to be handled specially.
  173.         bind $w <quotedbl> {%W insert cursor "\""; entry.cursor %W}
  174.         bind $w <backslash> {%W insert cursor "\\"; entry.cursor %W}
  175.         bind $w <bracketleft> {%W insert cursor "\["; entry.cursor %W}
  176.     # left, right arrows move cursor
  177.         bind $w <Left>  {entry.cursorleft %W}
  178.         bind $w <Right> {entry.cursorright %W}
  179.     bind $w <ButtonPress-1> {%W cursor @%x; focus %W; %W select from @%x}
  180.     bind $w <Button1-Motion> {%W select to @%x}
  181.     bind $w <Shift-ButtonPress-1> {%W select adjust @%x}
  182.     bind $w <Shift-Button1-Motion> {%W select to @%x}
  183.     bind $w <Double-Button-1> {%W select from 0; %W select to end}
  184.     bind $w <ButtonPress-2> {%W insert cursor [selection get]; entry.cursor %W}
  185.     bind $w <Control-ButtonPress-2> {entry.rpl %W}
  186.     bind $w <ButtonPress-3> {%W scan mark %x}
  187.     bind $w <Button3-Motion> {%W scan dragto %x}
  188. }
  189.  
  190. # Cursor left
  191.  
  192. proc entry.cursorleft {w} {
  193.     set x [expr {[$w index cursor] - 1}]
  194.     if {$x >= 0} {$w cursor $x}
  195.     entry.cursor $w
  196. }
  197.  
  198. # Cursor right
  199.  
  200. proc entry.cursorright {w} {
  201.     set x [expr {[$w index cursor] + 1}]
  202.     set xm [$w index end]
  203.     if {$x <= $xm} {$w cursor $x}
  204.     entry.cursor $w
  205. }
  206.  
  207. # Delete left
  208.  
  209. proc entry.deleteleft {w} {
  210.     set x [expr {[$w index cursor] - 1}]
  211.     if {$x >= 0} {$w delete $x}
  212.     entry.cursor $w
  213. }
  214.  
  215. # Delete character right
  216.  
  217. proc entry.deleteright {w} {
  218.     set x [$w index cursor]
  219.     set xm [$w index end]
  220.     if {$x < $xm} {$w delete $x}
  221.     entry.cursor $w
  222. }
  223.  
  224. # Center the cursor in the window
  225.  
  226. proc entry.center {win} {
  227.     set cursor_position [$win index cursor]
  228.     $win view 0
  229.     set left_extent [$win index @0]
  230.     set right_extent [$win index @[winfo width $win]]
  231.     set entry_length [expr {$right_extent - $left_extent}]
  232.     set text_length [expr [$win index end]]
  233.     if {$text_length > $entry_length} {
  234.         $win view [expr {$cursor_position - $entry_length/2 + 1}]
  235.     }
  236. }
  237.  
  238. # Move the view in the entry box to place cursor on screen
  239.  
  240. proc entry.cursor {win} {
  241.     set left_extent [$win index @0]
  242.     set right_extent [$win index @[winfo width $win]]
  243.     set cursor_position [$win index cursor]
  244.     set entry_length [expr {$right_extent - $left_extent}]
  245.     if {$cursor_position >= $right_extent \
  246.      || $cursor_position <= $left_extent} {
  247.         entry.center $win
  248.     }
  249. }
  250.  
  251. # Replace the entire entry with the selection
  252.  
  253. proc entry.rpl {win} {
  254.     set a [selection get]
  255.     $win delete 0 end
  256.     $win insert cursor $a
  257.     entry.cursor $win
  258. }
  259.  
  260.  
  261.  
  262. 73 de ke9tv/2, Kevin          There isn't any .signature virus, is there?
  263.