gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 line
13KB

  1. #!/bin/sh
  2. # the next line restarts using wish \
  3. exec wish "$0" "$@"
  4. # widget --
  5. #
  6. # This script demonstrates the various widgets provided by Tix,
  7. # along with many of the features of the Tix library. 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 in the samples/ subdirectory, which are sourced
  12. # by this script as needed.
  13. #
  14. # Copyright (c) 1992-1994 The Regents of the University of California.
  15. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  16. # Copyright (c) 1998-2000 Scriptics Corporation.
  17. # Copyright (c) 2000-2001 Tix Project Group.
  18. #
  19. # See the file "license.terms" for information on usage and redistribution
  20. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  21. # $Id: widget,v 1.7 2008/03/17 22:58:51 hobbs Exp $
  22. package require Tix
  23. tix initstyle
  24. eval destroy [winfo child .]
  25. wm title . "Tix Widget Tour"
  26. set tix_demo_running 1
  27. set demo_dir [file dirname [info script]]
  28. tix addbitmapdir [file join $demo_dir bitmaps]
  29. # createMainWindow --
  30. #
  31. # Creates the main window, consisting of a menu bar and a text
  32. # widget that explains how to use the program, plus lists all of
  33. # the demos as hypertext items.
  34. proc createMainWindow {} {
  35. global tcl_platform old_cursor
  36. switch $tcl_platform(platform) {
  37. "windows" {
  38. set font {Arial 12}
  39. }
  40. "unix" {
  41. set font {Helvetica 12}
  42. }
  43. default {
  44. set font {Helvetica 12}
  45. }
  46. }
  47. menu .menuBar -tearoff 0
  48. .menuBar add cascade -menu .menuBar.file -label "File" -underline 0
  49. menu .menuBar.file -tearoff 0
  50. # On the Mac use the specia .apple menu for the about item
  51. if {$tcl_platform(platform) eq "macintosh"} {
  52. .menuBar add cascade -menu .menuBar.apple
  53. menu .menuBar.apple -tearoff 0
  54. .menuBar.apple add command -label "About ..." -command "aboutBox"
  55. } else {
  56. .menuBar.file add command -label "About ..." -command "aboutBox"
  57. .menuBar.file add sep
  58. }
  59. .menuBar.file add command -label "Exit" -command "exit"
  60. . configure -menu .menuBar
  61. frame .statusBar
  62. label .statusBar.lab -text " " -relief sunken -bd 1 \
  63. -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
  64. label .statusBar.foo -width 8 -relief sunken -bd 1 \
  65. -font -*-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-* -anchor w
  66. pack .statusBar.lab -side left -padx 2 -expand yes -fill both
  67. pack .statusBar.foo -side left -padx 2
  68. pack .statusBar -side bottom -fill x -pady 2
  69. frame .textFrame
  70. scrollbar .s -orient vertical -command {.t yview} -highlightthickness 0 \
  71. -takefocus 1
  72. pack .s -in .textFrame -side right -fill y
  73. text .t -yscrollcommand {.s set} -wrap word -width 55 -height 30 \
  74. -font $font \
  75. -setgrid 1 -highlightthickness 0 -padx 4 -pady 2 -takefocus 0
  76. pack .t -in .textFrame -expand y -fill both -padx 1
  77. pack .textFrame -expand yes -fill both
  78. if {$tcl_platform(platform) eq "windows"} {
  79. #
  80. # Make the scrollbar look win32
  81. #
  82. .textFrame config -bd 2 -relief sunken
  83. .t config -bd 0
  84. pack .t -padx 0
  85. }
  86. set old_cursor [.t cget -cursor]
  87. # Create a bunch of tags to use in the text widget, such as those for
  88. # section titles and demo descriptions. Also define the bindings for
  89. # tags.
  90. .t tag configure title -font {Helvetica 18 bold} -justify center
  91. .t tag configure header -font {Helvetica 14 bold}
  92. # We put some "space" characters to the left and right of each
  93. # demo description so that the descriptions are highlighted only
  94. # when the mouse cursor is right over them (but not when the
  95. # cursor is to their left or right)
  96. #
  97. .t tag configure demospace -lmargin1 1c -lmargin2 1c -spacing1 1
  98. .t tag configure codeicon -lmargin1 1c -lmargin2 1c
  99. if {[winfo depth .] == 1} {
  100. .t tag configure demo -lmargin1 1c -lmargin2 1c \
  101. -underline 1
  102. .t tag configure visited -lmargin1 1c -lmargin2 1c \
  103. -underline 1
  104. .t tag configure hot -background black -foreground white
  105. } else {
  106. .t tag configure demo -lmargin1 1c -lmargin2 1c \
  107. -foreground blue -underline 1
  108. .t tag configure visited -lmargin1 1c -lmargin2 1c \
  109. -foreground #303080 -underline 1
  110. .t tag configure hot -foreground red -underline 1
  111. }
  112. .t tag bind demo <ButtonRelease-1> {
  113. invoke [.t index {@%x,%y}]
  114. }
  115. .t tag bind codeicon <ButtonRelease-1> {
  116. showCode [.t index [list {@%x,%y} +2 chars]]
  117. }
  118. global lastLine
  119. set lastLine ""
  120. .t tag bind demo <Enter> {
  121. set lastLine [.t index {@%x,%y linestart}]
  122. .t tag add hot [list $lastLine +3 chars] \
  123. [list $lastLine lineend -1 chars]
  124. .t config -cursor hand2
  125. showStatus run [.t index {@%x,%y}]
  126. }
  127. .t tag bind demo <Leave> {
  128. .t tag remove hot 1.0 end
  129. .t config -cursor $old_cursor
  130. .statusBar.lab config -text ""
  131. }
  132. .t tag bind demo <Motion> {
  133. set newLine [.t index {@%x,%y linestart}]
  134. if {[string compare $newLine $lastLine] != 0} {
  135. .t tag remove hot 1.0 end
  136. set lastLine $newLine
  137. set tags [.t tag names {@%x,%y}]
  138. set i [lsearch -glob $tags demo-*]
  139. if {$i >= 0} {
  140. .t tag add hot [list $lastLine +3 chars] \
  141. [list $lastLine lineend -1 chars]
  142. }
  143. }
  144. showStatus run [.t index {@%x,%y}]
  145. }
  146. .t tag bind codeicon <Enter> {
  147. .t config -cursor hand2
  148. }
  149. .t tag bind codeicon <Leave> {
  150. .t config -cursor $old_cursor
  151. }
  152. .t tag bind codeicon <Motion> {
  153. set tags [.t tag names [list {@%x,%y} +2 chars]]
  154. set i [lsearch -glob $tags demo-*]
  155. if {$i >= 0} {
  156. showStatus code [.t index [list {@%x,%y} +2 chars]]
  157. } else {
  158. showStatus code ""
  159. }
  160. }
  161. # Create the text for the text widget.
  162. .t insert end "Tix Widget Tour\n" title
  163. addNewLine .t
  164. addText .t {
  165. This program demonstrates the features of the Tix
  166. library. Click on one of the highlighted lines below to run
  167. the sample program and click on the
  168. }
  169. addSpace .t
  170. .t image create end -image [tix getimage code]
  171. addSpace .t
  172. addText .t {
  173. icon to view its source code.
  174. }
  175. addNewLine .t
  176. addNewLine .t
  177. addHeader .t "Hierachical ListBox"
  178. addDemo .t HList1.tcl "Simple HList"
  179. addDemo .t ChkList.tcl "CheckList"
  180. addDemo .t SHList.tcl "ScrolledHList (1)"
  181. addDemo .t SHList2.tcl "ScrolledHList (2)"
  182. addDemo .t Tree.tcl "Simple Tree"
  183. # TODO
  184. # addDemo .t "Dynamic Tree" DynTree.tcl
  185. addHeader .t "Tabular ListBox"
  186. addDemo .t STList1.tcl "ScrolledTList (1)"
  187. addDemo .t STList2.tcl "ScrolledTList (2)"
  188. addDemo .t STList3.tcl "TList File Viewer"
  189. addHeader .t "Grid Widget"
  190. addDemo .t SGrid0.tcl "Simple Grid"
  191. addDemo .t SGrid1.tcl "ScrolledGrid"
  192. addDemo .t EditGrid.tcl "Editable Grid"
  193. addHeader .t "Manager Widgets"
  194. addDemo .t ListNBK.tcl ListNoteBook
  195. addDemo .t NoteBook.tcl NoteBook
  196. addDemo .t PanedWin.tcl PanedWindow
  197. addHeader .t "Scrolled Widgets"
  198. addDemo .t SListBox.tcl ScrolledListBox
  199. addDemo .t SText.tcl ScrolledText
  200. addDemo .t SWindow.tcl ScrolledWindow
  201. addDemo .t CObjView.tcl "Canvas Object View"
  202. addHeader .t "Miscellaneous Widgets"
  203. addDemo .t Balloon.tcl Balloon
  204. addDemo .t BtnBox.tcl ButtonBox
  205. addDemo .t ComboBox.tcl ComboBox
  206. addDemo .t Control.tcl Control
  207. addDemo .t LabEntry.tcl LabelEntry
  208. addDemo .t LabFrame.tcl LabelFrame
  209. addDemo .t Meter.tcl Meter
  210. addDemo .t OptMenu.tcl OptionMenu
  211. addDemo .t PopMenu.tcl PopupMenu
  212. addDemo .t Select.tcl Select
  213. addDemo .t StdBBox.tcl StdButtonBox
  214. addHeader .t "Image Types"
  215. addDemo .t CmpImg.tcl "Compound image in buttons"
  216. addDemo .t CmpImg3.tcl "Compound image in icons"
  217. #addDemo .t CmpImg2.tcl "Compound image in notebook"
  218. #addDemo .t CmpImg4.tcl \
  219. # "Create color tabs in notebook using compound image"
  220. addDemo .t Xpm.tcl "XPM pixmap image in buttons"
  221. addDemo .t Xpm1.tcl "XPM pixmap image in menu"
  222. .t configure -state disabled
  223. focus .s
  224. #
  225. # Because .t is disabled and not focused, we have to do the
  226. # following hacks to make the scrolling work well
  227. #
  228. bind .s <MouseWheel> {
  229. .t yview scroll [expr {- (%D / 120) * 2}] units
  230. }
  231. bind .s <Up> {
  232. .t yview scroll -1 units
  233. }
  234. bind .s <Down> {
  235. .t yview scroll 1 units
  236. }
  237. bind .s <Prior> {
  238. .t yview scroll -1 page
  239. }
  240. bind .s <Next> {
  241. .t yview scroll 1 page
  242. }
  243. bind .s <Home> {
  244. .t yview 1.0
  245. }
  246. bind .s <End> {
  247. .t yview end
  248. }
  249. }
  250. # invoke --
  251. # This procedure is called when the user clicks on a demo description.
  252. # It is responsible for invoking the demonstration.
  253. #
  254. # Arguments:
  255. # index - The index of the character that the user clicked on.
  256. proc invoke {index} {
  257. global demo_dir
  258. # Find out which sample to run
  259. set tags [.t tag names $index]
  260. set i [lsearch -glob $tags demo-*]
  261. if {$i < 0} {
  262. return
  263. }
  264. set demo [string range [lindex $tags $i] 5 end]
  265. set title [string trim [.t get [list $index linestart +3 chars] \
  266. [list $index lineend]]]
  267. # Get the name of this sample
  268. set w .[lindex [split $demo .] 0]
  269. set w [string tolower $w]
  270. if [winfo exists $w] {
  271. wm deiconify $w
  272. raise $w
  273. return
  274. }
  275. # Load the sample if it's not running
  276. set cursor [.t cget -cursor]
  277. .t configure -cursor watch
  278. update
  279. uplevel #0 [list source [file join $demo_dir samples $demo]]
  280. toplevel $w
  281. wm title $w $title
  282. RunSample $w
  283. update
  284. .t configure -cursor $cursor
  285. .t tag add visited "$index linestart +1 chars" "$index lineend -1 chars"
  286. }
  287. # showStatus --
  288. #
  289. # Show the name of the demo program in the status bar. This procedure
  290. # is called when the user moves the cursor over a demo description.
  291. #
  292. proc showStatus {which index} {
  293. set tags [.t tag names $index]
  294. set i [lsearch -glob $tags demo-*]
  295. set cursor [.t cget -cursor]
  296. if {$i < 0} {
  297. .statusBar.lab config -text " "
  298. set newcursor xterm
  299. } else {
  300. set demo [string range [lindex $tags $i] 5 end]
  301. if {"$which" == "run"} {
  302. set text "Run the \"$demo\" sample program"
  303. } else {
  304. set text "Show code of the \"$demo\" sample program"
  305. }
  306. .statusBar.lab config -text $text
  307. set newcursor hand2
  308. }
  309. if [string compare $cursor $newcursor] {
  310. .t config -cursor $newcursor
  311. }
  312. }
  313. # showCode --
  314. # This procedure is called when the user clicks on the "code" icon.
  315. # It is responsible for displaying the code of the selected sample program.
  316. #
  317. # Arguments:
  318. # index - The index of the character that the user clicked on.
  319. proc showCode {index} {
  320. global demo_dir
  321. set tags [.t tag names $index]
  322. set i [lsearch -glob $tags demo-*]
  323. if {$i < 0} {
  324. return
  325. }
  326. set cursor [.t cget -cursor]
  327. .t configure -cursor watch
  328. update
  329. set demo [string range [lindex $tags $i] 5 end]
  330. # Create the .code window
  331. if {![winfo exists .code]} {
  332. toplevel .code
  333. frame .code.f
  334. tixScrolledText .code.st
  335. button .code.close -text Close -width 6 -command "wm withdraw .code"
  336. pack .code.f -side bottom -fill x
  337. pack .code.st -side top -fill both -expand yes
  338. pack .code.close -in .code.f -side right -padx 10 -pady 10
  339. }
  340. set text [.code.st subwidget text]
  341. $text delete 1.0 end
  342. set fd [open [file join $demo_dir samples $demo]]
  343. set data [read $fd]
  344. close $fd
  345. $text insert end $data
  346. wm deiconify .code
  347. wm title .code [file nativename [file join $demo_dir samples $demo]]
  348. update
  349. .t configure -cursor $cursor
  350. }
  351. proc addText {t text} {
  352. regsub -all \n+ $text " " text
  353. regsub -all {[ ]+} $text " " text
  354. $t insert end [string trim $text]
  355. }
  356. proc addHeader {t text} {
  357. addNewLine $t
  358. $t insert end [string trim $text] header
  359. addNewLine $t
  360. }
  361. proc addNewLine {t} {
  362. $t insert end "\n" {demospace}
  363. }
  364. proc addSpace {t} {
  365. $t insert end " " {demospace}
  366. }
  367. proc addDemo {t name text} {
  368. $t insert end " " demospace
  369. $t image create end -image [tix getimage code]
  370. $t tag add codeicon [list end -2 chars] [list end -1 chars]
  371. $t insert end " " demospace
  372. $t insert end $text [list demo demo-$name]
  373. $t insert end " " demospace
  374. addNewLine $t
  375. }
  376. # aboutBox --
  377. #
  378. # Pops up a message box with an "about" message
  379. #
  380. proc aboutBox {} {
  381. tk_messageBox -icon info -type ok -title "About Widget Tour" -message \
  382. "Tix widget tour\n\nCopyright (c) 2000-2001 Tix Project Group."
  383. }
  384. #
  385. # Start the program
  386. #
  387. createMainWindow
上海开阖软件有限公司 沪ICP备12045867号-1