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

  1. #!/bin/sh
  2. # the next line restarts using wish \
  3. exec wish "$0" "$@"
  4.  
  5. # widget --
  6. # This script demonstrates the various widgets provided by Tk,
  7. # along with many of the features of the Tk toolkit.  This file
  8. # only contains code to generate the main window for the
  9. # application, which invokes individual demonstrations.  The
  10. # code for the actual demonstrations is contained in separate
  11. # ".tcl" files is this directory, which are sourced by this script
  12. # as needed.
  13. #
  14. # @(#) widget 1.13 95/09/23 18:17:48
  15.  
  16. eval destroy [winfo child .]
  17. wm title . "Widget Demonstration"
  18.  
  19. #----------------------------------------------------------------
  20. # The code below create the main window, consisting of a menu bar
  21. # and a text widget that explains how to use the program, plus lists
  22. # all of the demos as hypertext items.
  23. #----------------------------------------------------------------
  24.  
  25. set font -*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*
  26. frame .menuBar
  27. pack .menuBar -side top -fill x
  28. menubutton .menuBar.file -text File -menu .menuBar.file.m -underline 0
  29. menu .menuBar.file.m
  30. .menuBar.file.m add command -label "Quit" -command "exit" -underline 0
  31. pack .menuBar.file -side left
  32.  
  33. scrollbar .s -orient vertical -command {.t yview}
  34. pack .s -side right -fill y
  35. text .t -yscrollcommand {.s set} -wrap word -width 60 -height 30 -font $font \
  36.     -setgrid 1
  37. pack .t -expand y -fill both
  38.  
  39. # Create a bunch of tags to use in the text widget, such as those for
  40. # section titles and demo descriptions.  Also define the bindings for
  41. # tags.
  42.  
  43. .t tag configure title -font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*
  44. .t tag configure demo -lmargin1 1c -lmargin2 1c
  45. if {[winfo depth .] == 1} {
  46.     .t tag configure hot -background black -foreground white
  47. } else {
  48.     .t tag configure hot -relief raised -borderwidth 1 -background SeaGreen3
  49. }
  50. .t tag bind demo <Button-1> {
  51.     invoke [.t index current]
  52. }
  53. set lastLine ""
  54. .t tag bind demo <Enter> {
  55.     set lastLine [.t index {@%x,%y linestart}]
  56.     .t tag add hot $lastLine "$lastLine lineend"
  57. }
  58. .t tag bind demo <Leave> {
  59.     .t tag remove hot 1.0 end
  60. }
  61. .t tag bind demo <Motion> {
  62.     set newLine [.t index {@%x,%y linestart}]
  63.     if {[string compare $newLine $lastLine] != 0} {
  64.     .t tag remove hot 1.0 end
  65.     set lastLine $newLine
  66.     .t tag add hot $lastLine "$lastLine lineend"
  67.     }
  68. }
  69.  
  70. # Create the text for the text widget.
  71.  
  72. .t insert end "Tk Widget Demonstrations\n" title
  73. .t insert end {
  74. This application provides a front end for several short scripts that demonstrate what you can do with Tk widgets.  Each of the numbered lines below describes a demonstration;  you can click on it to invoke the demonstration.  Once the demonstration window appears, you can click the "See Code" button to see the Tcl/Tk code that created the demonstration.  If you wish, you can edit the code and click the "Rerun Demo" button in the code window to reinvoke the demonstration with the modified code.
  75.  
  76. }
  77. .t insert end "Labels, buttons, checkbuttons, and radiobuttons\n" title
  78. .t insert end "1. Labels (text and bitmaps).\n" {demo demo-label}
  79. .t insert end "2. Buttons.\n" {demo demo-button}
  80. .t insert end "3. Checkbuttons (select any of a group).\n" {demo demo-check}
  81. .t insert end "4. Radiobuttons (select one of a group).\n" {demo demo-radio}
  82. .t insert end "5. A 15-puzzle game made out of buttons.\n" {demo demo-puzzle}
  83. .t insert end "6. Iconic buttons that use bitmaps.\n" {demo demo-icon}
  84. .t insert end "7. Two labels displaying images.\n" {demo demo-image1}
  85. .t insert end "8. A simple user interface for viewing images.\n" {demo demo-image2}
  86.  
  87. .t insert end \n {} "Listboxes\n" title
  88. .t insert end "1. 50 states.\n" {demo demo-states}
  89. .t insert end "2. Colors: change the color scheme for the application.\n" \
  90.     {demo demo-colors}
  91. .t insert end "3. A collection of famous sayings.\n" {demo demo-sayings}
  92.  
  93. .t insert end \n {} "Entries\n" title
  94. .t insert end "1. Without scrollbars.\n" {demo demo-entry1}
  95. .t insert end "2. With scrollbars.\n" {demo demo-entry2}
  96. .t insert end "3. Simple Rolodex-like form.\n" {demo demo-form}
  97.  
  98. .t insert end \n {} "Text\n" title
  99. .t insert end "1. Basic editable text.\n" {demo demo-text}
  100. .t insert end "2. Text display styles.\n" {demo demo-style}
  101. .t insert end "3. Hypertext (tag bindings).\n" {demo demo-bind}
  102. .t insert end "4. A text widget with embedded windows.\n" {demo demo-twind}
  103. .t insert end "5. A search tool built with a text widget.\n" {demo demo-search}
  104.  
  105. .t insert end \n {} "Canvases\n" title
  106. .t insert end "1. The canvas item types.\n" {demo demo-items}
  107. .t insert end "2. A simple 2-D plot.\n" {demo demo-plot}
  108. .t insert end "3. Text items in canvases.\n" {demo demo-ctext}
  109. .t insert end "4. An editor for arrowheads on canvas lines.\n" {demo demo-arrow}
  110. .t insert end "5. A ruler with adjustable tab stops.\n" {demo demo-ruler}
  111. .t insert end "6. A building floor plan.\n" {demo demo-floor}
  112. .t insert end "7. A simple scrollable canvas.\n" {demo demo-cscroll}
  113.  
  114. .t insert end \n {} "Scales\n" title
  115. .t insert end "1. Vertical scale.\n" {demo demo-vscale}
  116. .t insert end "2. Horizontal scale.\n" {demo demo-hscale}
  117.  
  118. .t insert end \n {} "Menus\n" title
  119. .t insert end "1. A window containing several menus and cascades.\n" \
  120.     {demo demo-menu}
  121.  
  122. .t insert end \n {} "Miscellaneous\n" title
  123. .t insert end "1. The built-in bitmaps.\n" {demo demo-bitmap}
  124. .t insert end "2. A dialog box with a local grab.\n" {demo demo-dialog1}
  125. .t insert end "3. A dialog box with a global grab.\n" {demo demo-dialog2}
  126.  
  127. .t configure -state disabled
  128.  
  129. # positionWindow --
  130. # This procedure is invoked by most of the demos to position a
  131. # new demo window.
  132. #
  133. # Arguments:
  134. # w -        The name of the window to position.
  135.  
  136. proc positionWindow w {
  137.     wm geometry $w +300+300
  138. }
  139.  
  140. # showVars --
  141. # Displays the values of one or more variables in a window, and
  142. # updates the display whenever any of the variables changes.
  143. #
  144. # Arguments:
  145. # w -        Name of new window to create for display.
  146. # args -    Any number of names of variables.
  147.  
  148. proc showVars {w args} {
  149.     catch {destroy $w}
  150.     toplevel $w
  151.     wm title $w "Variable values"
  152.     label $w.title -text "Variable values:" -width 20 -anchor center \
  153.         -font -Adobe-helvetica-medium-r-normal--*-180-*-*-*-*-*-*
  154.     pack $w.title -side top -fill x
  155.     foreach i $args {
  156.     frame $w.$i
  157.     label $w.$i.name -text "$i: "
  158.     label $w.$i.value -textvar $i -anchor w
  159.     pack $w.$i.name -side left
  160.     pack $w.$i.value -side left -expand 1 -fill x
  161.     pack $w.$i -side top -anchor w -fill x
  162.     }
  163.     button $w.ok -text OK -command "destroy $w"
  164.     pack $w.ok -side bottom -pady 2
  165. }
  166.  
  167. # invoke --
  168. # This procedure is called when the user clicks on a demo description.
  169. # It is responsible for invoking the demonstration.
  170. #
  171. # Arguments:
  172. # index -    The index of the character that the user clicked on.
  173.  
  174. proc invoke index {
  175.     global tk_library
  176.     set tags [.t tag names $index]
  177.     set i [lsearch -glob $tags demo-*]
  178.     if {$i < 0} {
  179.     return
  180.     }
  181.     .t configure -cursor watch
  182.     update
  183.     set demo [string range [lindex $tags $i] 5 end]
  184.     uplevel [list source $tk_library/demos/$demo.tcl]
  185.     update
  186.     .t configure -cursor xterm
  187. }
  188.  
  189. # showCode --
  190. # This procedure creates a toplevel window that displays the code for
  191. # a demonstration and allows it to be edited and reinvoked.
  192. #
  193. # Arguments:
  194. # w -        The name of the demonstration's window, which can be
  195. #        used to derive the name of the file containing its code.
  196.  
  197. proc showCode w {
  198.     global tk_library
  199.     set file [string range $w 1 end].tcl
  200.     if ![winfo exists .code] {
  201.     toplevel .code
  202.     frame .code.buttons
  203.     pack .code.buttons -side bottom -expand 1 -fill x
  204.     button .code.buttons.dismiss -text Dismiss -command "destroy .code"
  205.     button .code.buttons.rerun -text "Rerun Demo" -command {
  206.         eval [.code.text get 1.0 end]
  207.     }
  208.     pack .code.buttons.dismiss .code.buttons.rerun -side left \
  209.         -expand 1
  210.     text .code.text -height 40 -yscrollcommand ".code.scroll set" -setgrid 1
  211.     pack .code.text -side left -expand 1 -fill both
  212.     scrollbar .code.scroll -command ".code.text yview"
  213.     pack .code.scroll -side right -fill y
  214.     } else {
  215.     wm deiconify .code
  216.     raise .code
  217.     }
  218.     wm title .code "Demo code: $tk_library/demos/$file"
  219.     wm iconname .code $file
  220.     set id [open $tk_library/demos/$file]
  221.     .code.text delete 1.0 end
  222.     .code.text insert 1.0 [read $id]
  223.     .code.text mark set insert 1.0
  224.     close $id
  225. }
  226.