home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / twind.tcl < prev    next >
Text File  |  1996-04-23  |  7KB  |  193 lines

  1. # twind.tcl --
  2. #
  3. # This demonstration script creates a text widget with a bunch of
  4. # embedded windows.
  5. #
  6. # @(#) twind.tcl 1.2 95/06/15 13:54:54
  7.  
  8. set w .twind
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Text Demonstration - Embedded Windows"
  12. wm iconname $w "Embedded Windows"
  13. positionWindow $w
  14.  
  15. frame $w.buttons
  16. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  17. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  18. button $w.buttons.code -text "See Code" -command "showCode $w"
  19. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  20.  
  21. frame $w.f -highlightthickness 2 -borderwidth 2 -relief sunken
  22. set t $w.f.text
  23. text $t -yscrollcommand "$w.scroll set" -setgrid true -font $font -width 70 \
  24.     -height 35 -wrap word -highlightthickness 0 -borderwidth 0
  25. pack $t -expand  yes -fill both
  26. scrollbar $w.scroll -command "$t yview"
  27. pack $w.scroll -side right -fill y
  28. pack $w.f -expand yes -fill both
  29. $t tag configure center -justify center -spacing1 5m -spacing3 5m
  30. $t tag configure buttons -lmargin1 1c -lmargin2 1c -rmargin 1c \
  31.     -spacing1 3m -spacing2 0 -spacing3 0
  32.  
  33. button $t.on -text "Turn On" -command "textWindOn $w" \
  34.     -cursor top_left_arrow
  35. button $t.off -text "Turn Off" -command "textWindOff $w" \
  36.     -cursor top_left_arrow
  37. button $t.click -text "Click Here" -command "textWindPlot $t" \
  38.     -cursor top_left_arrow
  39. button $t.delete -text "Delete" -command "textWindDel $w" \
  40.     -cursor top_left_arrow
  41.  
  42. $t insert end "A text widget can contain other widgets embedded "
  43. $t insert end "it.  These are called \"embedded windows\", "
  44. $t insert end "and they can consist of arbitrary widgets.  "
  45. $t insert end "For example, here are two embedded button "
  46. $t insert end "widgets.  You can click on the first button to "
  47. $t window create end -window $t.on
  48. $t insert end " horizontal scrolling, which also turns off "
  49. $t insert end "word wrapping.  Or, you can click on the second "
  50. $t insert end "button to\n"
  51. $t window create end -window $t.off
  52. $t insert end " horizontal scrolling and turn back on word wrapping.\n\n"
  53.  
  54. $t insert end "Or, here is another example.  If you "
  55. $t window create end -window $t.click
  56. $t insert end " a canvas displaying an x-y plot will appear right here."
  57. $t mark set plot insert
  58. $t mark gravity plot left
  59. $t insert end "  You can drag the data points around with the mouse, "
  60. $t insert end "or you can click here to "
  61. $t window create end -window $t.delete
  62. $t insert end " the plot again.\n\n"
  63.  
  64. $t insert end "You may also find it useful to put embedded windows in "
  65. $t insert end "a text without any actual text.  In this case the "
  66. $t insert end "text widget acts like a geometry manager.  For "
  67. $t insert end "example, here is a collection of buttons laid out "
  68. $t insert end "neatly into rows by the text widget.  These buttons "
  69. $t insert end "can be used to change the background color of the "
  70. $t insert end "text widget (\"Default\" restores the color to "
  71. $t insert end "its default).  If you click on the button labeled "
  72. $t insert end "\"Short\", it changes to a longer string so that "
  73. $t insert end "you can see how the text widget automatically "
  74. $t insert end "changes the layout.  Click on the button again "
  75. $t insert end "to restore the short string.\n"
  76.  
  77. button $t.default -text Default -command "embDefBg $t" \
  78.     -cursor top_left_arrow
  79. $t window create end -window $t.default -padx 3
  80. global embToggle
  81. set embToggle Short
  82. checkbutton $t.toggle -textvariable embToggle -indicatoron 0 \
  83.     -variable embToggle -onvalue "A much longer string" \
  84.     -offvalue "Short" -cursor top_left_arrow
  85. $t window create end -window $t.toggle -padx 3 -pady 2
  86. set i 1
  87. foreach color {AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
  88.     SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3 LightBlue1
  89.     DarkSlateGray1 Aquamarine2 DarkSeaGreen2 SeaGreen1
  90.     Yellow1 IndianRed1 IndianRed2 Tan1 Tan4} {
  91.     button $t.color$i -text $color -cursor top_left_arrow -command \
  92.         "$t configure -bg $color"
  93.     $t window create end -window $t.color$i -padx 3 -pady 2
  94.     incr i
  95. }
  96. $t tag add buttons $t.default end
  97.  
  98. proc textWindOn w {
  99.     catch {destroy $w.scroll2}
  100.     set t $w.f.text
  101.     scrollbar $w.scroll2 -orient horizontal -command "$t xview"
  102.     pack $w.scroll2 -after $w.buttons -side bottom -fill x
  103.     $t configure -xscrollcommand "$w.scroll2 set" -wrap none
  104. }
  105.  
  106. proc textWindOff w {
  107.     catch {destroy $w.scroll2}
  108.     set t $w.f.text
  109.     $t configure -xscrollcommand {} -wrap word
  110. }
  111.  
  112. proc textWindPlot t {
  113.     set c $t.c
  114.     if [winfo exists $c] {
  115.     return
  116.     }
  117.     canvas $c -relief sunken -width 450 -height 300 -cursor top_left_arrow
  118.  
  119.     set font -Adobe-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*
  120.  
  121.     $c create line 100 250 400 250 -width 2
  122.     $c create line 100 250 100 50 -width 2
  123.     $c create text 225 20 -text "A Simple Plot" -font $font -fill brown
  124.     
  125.     for {set i 0} {$i <= 10} {incr i} {
  126.     set x [expr {100 + ($i*30)}]
  127.     $c create line $x 250 $x 245 -width 2
  128.     $c create text $x 254 -text [expr 10*$i] -anchor n -font $font
  129.     }
  130.     for {set i 0} {$i <= 5} {incr i} {
  131.     set y [expr {250 - ($i*40)}]
  132.     $c create line 100 $y 105 $y -width 2
  133.     $c create text 96 $y -text [expr $i*50].0 -anchor e -font $font
  134.     }
  135.     
  136.     foreach point {{12 56} {20 94} {33 98} {32 120} {61 180}
  137.         {75 160} {98 223}} {
  138.     set x [expr {100 + (3*[lindex $point 0])}]
  139.     set y [expr {250 - (4*[lindex $point 1])/5}]
  140.     set item [$c create oval [expr $x-6] [expr $y-6] \
  141.         [expr $x+6] [expr $y+6] -width 1 -outline black \
  142.         -fill SkyBlue2]
  143.     $c addtag point withtag $item
  144.     }
  145.  
  146.     $c bind point <Any-Enter> "$c itemconfig current -fill red"
  147.     $c bind point <Any-Leave> "$c itemconfig current -fill SkyBlue2"
  148.     $c bind point <1> "embPlotDown $c %x %y"
  149.     $c bind point <ButtonRelease-1> "$c dtag selected"
  150.     bind $c <B1-Motion> "embPlotMove $c %x %y"
  151.     while {[string first [$t get plot] " \t\n"] >= 0} {
  152.     $t delete plot
  153.     }
  154.     $t insert plot "\n"
  155.     $t window create plot -window $c
  156.     $t tag add center plot
  157.     $t insert plot "\n"
  158. }
  159.  
  160. set embPlot(lastX) 0
  161. set embPlot(lastY) 0
  162.  
  163. proc embPlotDown {w x y} {
  164.     global embPlot
  165.     $w dtag selected
  166.     $w addtag selected withtag current
  167.     $w raise current
  168.     set embPlot(lastX) $x
  169.     set embPlot(lastY) $y
  170. }
  171.  
  172. proc embPlotMove {w x y} {
  173.     global embPlot
  174.     $w move selected [expr $x-$embPlot(lastX)] [expr $y-$embPlot(lastY)]
  175.     set embPlot(lastX) $x
  176.     set embPlot(lastY) $y
  177. }
  178.  
  179. proc textWindDel w {
  180.     set t $w.f.text
  181.     if [winfo exists $t.c] {
  182.     $t delete $t.c
  183.     while {[string first [$t get plot] " \t\n"] >= 0} {
  184.         $t delete plot
  185.     }
  186.     $t insert plot "  "
  187.     }
  188. }
  189.  
  190. proc embDefBg t {
  191.     $t configure -background [lindex [$t configure -background] 3]
  192. }
  193.