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.

282 lines
6.6KB

  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: EditGrid.tcl,v 1.3 2001/12/09 05:31:07 idiscovery Exp $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "widget": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program using tixwish.
  12. # Demonstrates the use of editable entries in a Grid widget.
  13. #
  14. proc RunSample {w} {
  15. global editgrid
  16. wm title $w "Doe Inc. Performance"
  17. wm geometry $w 640x300
  18. label $w.lab -justify left -text \
  19. "The left column is calculated automatically. To calculate the right column,
  20. press the \"Calculate\" button"
  21. pack $w.lab -side top -anchor c -padx 3 -pady 3
  22. # Create the buttons
  23. #
  24. set f [frame $w.f -relief flat]
  25. pack $f -side right -fill y
  26. set add [button $f.add -text "Add Row" -width 9 \
  27. -command "EditGrid_addRow"]
  28. set edit [button $f.edit -text "Edit" -width 9 \
  29. -command "EditGrid_edit"]
  30. set cal [button $f.cal -text "Calculate" -width 9 \
  31. -command "EditGrid_calculate"]
  32. set close [button $f.close -text "Close" -width 9 \
  33. -command "destroy $w"]
  34. pack $add -side top -padx 10
  35. pack $edit -side top -padx 10
  36. pack $cal -side top -padx 10 -pady 2
  37. pack $close -side bottom -padx 10
  38. # Create the grid and set options to make it editable.
  39. #
  40. tixScrolledGrid $w.g -bd 0
  41. pack $w.g -expand yes -fill both -padx 3 -pady 3
  42. set grid [$w.g subwidget grid]
  43. $grid config \
  44. -formatcmd "EditGrid_format $grid" \
  45. -editnotifycmd "EditGrid_editNotify" \
  46. -editdonecmd "EditGrid_editDone" \
  47. -selectunit cell \
  48. -selectmode single
  49. # Insert some initial data
  50. #
  51. $grid set 0 1 -text "City #1"
  52. $grid set 0 2 -text "City #2"
  53. $grid set 0 3 -text "City #3"
  54. $grid set 0 5 -text "Combined"
  55. $grid set 2 0 -text "Population"
  56. $grid set 4 0 -text "Avg. Income"
  57. $grid set 2 1 -text 125
  58. $grid set 2 2 -text 81
  59. $grid set 2 3 -text 724
  60. $grid set 4 1 -text 24432.12
  61. $grid set 4 2 -text 18290.24
  62. $grid set 4 3 -text 18906.34
  63. # Global data used by other EditGrid_ procedures.
  64. #
  65. set editgrid(g) $grid
  66. set editgrid(top) 1
  67. set editgrid(bot) 3
  68. set editgrid(result) 5
  69. EditGrid_calPop
  70. EditGrid_calIncome
  71. }
  72. # EditGrid_edit --
  73. #
  74. # Prompts the user to edit a cell.
  75. #
  76. proc EditGrid_edit {} {
  77. global editgrid
  78. set grid $editgrid(g)
  79. set ent [$grid anchor get]
  80. if [string comp $ent ""] {
  81. $grid edit set [lindex $ent 0] [lindex $ent 1]
  82. }
  83. }
  84. # EditGrid_addRow --
  85. #
  86. # Adds a new row to the table.
  87. #
  88. proc EditGrid_addRow {} {
  89. global editgrid
  90. set grid $editgrid(g)
  91. $grid edit apply
  92. $grid move row $editgrid(result) $editgrid(result) 1
  93. incr editgrid(bot)
  94. set editgrid(result) [expr $editgrid(bot) + 2]
  95. $grid set 0 $editgrid(bot) -text "City #$editgrid(bot)"
  96. $grid set 2 $editgrid(bot) -text 0
  97. $grid set 4 $editgrid(bot) -text 0.0
  98. EditGrid_calPop
  99. EditGrid_calIncome
  100. }
  101. # EditGrid_calPop --
  102. #
  103. # Calculates the total population
  104. #
  105. proc EditGrid_calPop {} {
  106. global editgrid
  107. set grid $editgrid(g)
  108. set pop 0
  109. for {set i $editgrid(top)} {$i <= $editgrid(bot)} {incr i} {
  110. incr pop [$grid entrycget 2 $i -text]
  111. }
  112. $grid set 2 $editgrid(result) -text $pop
  113. }
  114. # EditGrid_calIncome --
  115. #
  116. # Calculates the average income.
  117. #
  118. proc EditGrid_calIncome {} {
  119. global editgrid
  120. set grid $editgrid(g)
  121. set income 0
  122. set total_pop 0
  123. for {set i $editgrid(top)} {$i <= $editgrid(bot)} {incr i} {
  124. set pop [$grid entrycget 2 $i -text]
  125. set inc [$grid entrycget 4 $i -text]
  126. set income [expr $income + $pop.0 * $inc]
  127. incr total_pop $pop
  128. }
  129. $grid set 4 $editgrid(result) -text [expr $income/$total_pop]
  130. }
  131. # EditGrid_calculate --
  132. #
  133. # Recalculates both columns.
  134. #
  135. proc EditGrid_calculate {} {
  136. global editgrid
  137. set grid $editgrid(g)
  138. $grid edit apply
  139. EditGrid_calIncome
  140. }
  141. # EditGrid_editNotify --
  142. #
  143. # Returns true if an entry can be edited.
  144. #
  145. proc EditGrid_editNotify {x y} {
  146. global editgrid
  147. set grid $editgrid(g)
  148. if {$x == 2 || $x == 4} {
  149. if {$y >= $editgrid(top) && $y <= $editgrid(bot)} {
  150. set editgrid(oldValue) [$grid entrycget $x $y -text]
  151. return 1
  152. }
  153. }
  154. return 0
  155. }
  156. # EditGrid_editDone --
  157. #
  158. # Gets called when the user is done editing an entry.
  159. #
  160. proc EditGrid_editDone {x y} {
  161. global editgrid
  162. set grid $editgrid(g)
  163. if {$x == 2} {
  164. set pop [$grid entrycget $x $y -text]
  165. if [catch {
  166. format %d $pop
  167. }] {
  168. $grid entryconfig $x $y -text $editgrid(oldValue)
  169. tk_dialog .editGridWarn "" \
  170. "$pop is not an valid integer. Try again" \
  171. warning 0 Ok
  172. } else {
  173. $grid entryconfig 4 $editgrid(result) -text "-"
  174. EditGrid_calPop
  175. }
  176. } else {
  177. set income [$grid entrycget $x $y -text]
  178. if [catch {
  179. format %f $income
  180. }] {
  181. $grid entryconfig $x $y -text $editgrid(oldValue)
  182. tk_dialog .editGridWarn "" \
  183. "$income is not an valid floating number. Try again" \
  184. warning 0 Ok
  185. } else {
  186. $grid entryconfig 4 $editgrid(result) -text "-"
  187. }
  188. }
  189. }
  190. # EditGrid_format --
  191. #
  192. # This command is called whenever the background of the grid
  193. # needs to be reformatted. The x1, y1, x2, y2 sprcifies the four
  194. # corners of the area that needs to be reformatted.
  195. #
  196. proc EditGrid_format {w area x1 y1 x2 y2} {
  197. global editgrid
  198. set bg(s-margin) gray65
  199. set bg(x-margin) gray65
  200. set bg(y-margin) gray65
  201. set bg(main) gray20
  202. case $area {
  203. main {
  204. foreach col {2 4} {
  205. $w format border $col 1 $col $editgrid(bot) \
  206. -relief flat -filled 1 -yon 1 -yoff 1\
  207. -bd 0 -bg #b0b0f0 -selectbackground #a0b0ff
  208. $w format border $col 2 $col $editgrid(bot) \
  209. -relief flat -filled 1 -yon 1 -yoff 1\
  210. -bd 0 -bg #80b080 -selectbackground #80b0ff
  211. }
  212. $w format grid $x1 $y1 $x2 $y2 \
  213. -relief raised -bd 1 -bordercolor $bg($area) -filled 0 -bg red\
  214. -xon 1 -yon 1 -xoff 0 -yoff 0 -anchor se
  215. }
  216. y-margin {
  217. $w format border $x1 $y1 $x2 $y2 \
  218. -fill 1 -relief raised -bd 1 -bg $bg($area) \
  219. -selectbackground gray80
  220. }
  221. default {
  222. $w format border $x1 $y1 $x2 $y2 \
  223. -filled 1 \
  224. -relief raised -bd 1 -bg $bg($area) \
  225. -selectbackground gray80
  226. }
  227. }
  228. # case $area {
  229. # {main y-margin} {
  230. # set y [expr $editgrid(bot) + 1]
  231. # $w format border 0 $y 100 $y -bg black -filled 1 -bd 0
  232. # }
  233. # }
  234. }
  235. if {![info exists tix_demo_running]} {
  236. wm withdraw .
  237. set w .demo
  238. toplevel $w; wm transient $w ""
  239. RunSample $w
  240. bind $w <Destroy> exit
  241. }
上海开阖软件有限公司 沪ICP备12045867号-1