home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jmore.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  5.4 KB  |  184 lines

  1. # jmore.tcl - text display panel
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5. ######################################################################
  6.  
  7. ### TO DO
  8. ###   j:more:button to add a button to the buttonbar
  9. ###   fix focus problems
  10.  
  11. ######################################################################
  12. # global variables:
  13. #
  14. global J_PREFS env
  15. if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
  16. if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
  17. if {! [info exists J_PREFS(printer)]} {set J_PREFS(printer) lp}
  18. #
  19. ######################################################################
  20.  
  21.  
  22. ######################################################################
  23. # j:more ?options? ?-title hdr? ?-text txt? - displays text in window
  24. # options include
  25. #   -title (default "Output")
  26. #   -text (default "" - not really optional)
  27. #   -height (default 24)
  28. #   -width (default 80)
  29. #   -font (default "default")
  30. #   -class (default "More")
  31. # this unfortunately forces focus-follows-pointer in these windows
  32. ######################################################################
  33.  
  34. proc j:more { args } {
  35.   global J_PREFS
  36.   if {[lsearch [array names J_PREFS] {scrollbarside}] == -1} {
  37.     set J_PREFS(scrollbarside) right ;# make sure it's defined
  38.   }
  39.  
  40.   j:parse_args {
  41.     {title Output}
  42.     {text {}}
  43.     {wrap char}
  44.     {height 24}
  45.     {width 80}
  46.     {font default}
  47.     {class More}
  48.   }
  49.   
  50.   global j_more
  51.  
  52.   if {[info exists j_more(count)]} then {
  53.     set j_more(count) [expr {$j_more(count) + 1}]
  54.   } else {
  55.     set j_more(count) 0
  56.   }
  57.  
  58.   set w ".more$j_more(count)"
  59.  
  60.   toplevel $w -class $class
  61.   wm title $w $title
  62.   
  63.   # using j:buttonbar for visual consistency, although we can't (easily)
  64.   # set the commands with it (because they depend on the window name):
  65.   
  66.   j:buttonbar $w.b -default ok -buttons {
  67.     {ok Done {}}
  68.     {save Save {}}
  69.     {print Print {}}
  70.     {find {Find . . .} {}}
  71.   }
  72.   $w.b.ok configure -command "destroy $w"
  73.   $w.b.save configure -command "j:more:save $w"
  74.   $w.b.print configure -command "j:more:print $w"
  75.   $w.b.find configure -width 8 -command "j:find -replace 0 $w.t"
  76.   
  77.   scrollbar $w.sb -relief flat -command "$w.t yview"
  78.   text $w.t -yscrollcommand "$w.sb set" -setgrid true -wrap word \
  79.     -height $height -wrap $wrap -width $width
  80.   j:configure_font $w.t $font
  81.   
  82.   pack \
  83.     $w.b \
  84.     [j:rule $w] \
  85.     -side bottom -fill x
  86.   pack \
  87.     $w.sb \
  88.     [j:rule $w] \
  89.     -side $J_PREFS(scrollbarside) -fill y
  90.   pack \
  91.     $w.t \
  92.     -expand yes -fill both
  93.   
  94.   $w.t insert end $text
  95.   
  96.   $w.t mark set insert 1.0
  97.  
  98.   $w.t configure -state disabled ;# prevent its being edited
  99.   
  100.   # FOLLOWING BINDINGS SHOULD BE GENERALISED! and check J_PREFS(bindings)!
  101.   #
  102.   bind $w.t <Next> "j:more:pageup $w.t"
  103.   bind $w.t <space> "j:more:pageup $w.t"
  104.   bind $w.t <Control-v> "j:more:pageup $w.t"
  105.   
  106.   bind $w.t <Prior> "j:more:pagedown $w.t"
  107.   bind $w.t <b> "j:more:pagedown $w.t"
  108.   bind $w.t <Escape><v> "j:more:pagedown $w.t"
  109.   
  110.   bind $w <Any-Enter> "focus $w.t"
  111.   
  112.   # "cancel" and "ok" amount to the same thing for this window:
  113.   j:default_button $w.b.ok $w.t
  114.   j:cancel_button $w.b.ok $w.t
  115.   
  116.   return $w.t            ;# so caller can insert things in it
  117. }
  118.  
  119. ######################################################################
  120. # j:more:save w - prompts to save the content of a j:more window
  121. #   NOTE: this adds a newline!  should check if ends in newline alr.
  122. ######################################################################
  123.  
  124. proc j:more:save { w } {
  125.   set filename [j:fs]
  126.   if {$filename == {}    } {
  127.     return 1
  128.   }
  129.   # should do error checking
  130.   set file [open $filename {w}]
  131.   puts $file [$w.t get 1.0 end]
  132.   close $file
  133. }
  134.  
  135. ######################################################################
  136. # j:more:print w - prompts to print the content of a j:more window
  137. #   command to use should be a preference!
  138. # uses J_PREFS(printer)
  139. ######################################################################
  140.  
  141. proc j:more:print { w } {
  142.   global env J_PREFS
  143.   
  144.   append J_PREFS(printer) {}            ;# make sure it's defined
  145.   if {"x$J_PREFS(printer)" == "x"} then {set J_PREFS(printer) "lp"}
  146.   
  147.   if [j:confirm -priority 100 -text "Print using lpr to $J_PREFS(printer)?"] {
  148.     # should do error checking
  149.     set file [open "|lpr -P$J_PREFS(printer)" {w}]
  150.     puts $file [$w.t get 1.0 end] nonewline
  151.     close $file
  152.   }
  153. }
  154.  
  155. ######################################################################
  156. # j:more:pageup t - scrolls text widget t up
  157. #   requires scrollbar to be sibling named "sb"
  158. #   based on procedure by Paul Raines <raines@bohr.physics.upenn.edu>
  159. ######################################################################
  160.  
  161. proc j:more:pageup { t } {
  162.   set sb "[winfo parent $t].sb"
  163.   $t mark set insert "[lindex [$sb get] 3].0"
  164.   $t yview insert
  165. }
  166.  
  167. ######################################################################
  168. # j:more:pagedown t - scrolls text widget t down
  169. #   requires scrollbar to be sibling named "sb"
  170. #   based on procedure by Paul Raines <raines@bohr.physics.upenn.edu>
  171. ######################################################################
  172.  
  173. proc j:more:pagedown { t } {
  174.   set sb "[winfo parent $t].sb"
  175.   set currentstate [$sb get]
  176.   
  177.   # following is buggy if lines wrap:
  178.   #
  179.   set newlinepos [expr {[lindex $currentstate 2]-[lindex $currentstate 1]}]
  180.   $t mark set insert "$newlinepos.0-2lines"
  181.   $t yview insert
  182. }
  183.