ChkList.tcl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: ChkList.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. # This program demonstrates the use of the tixCheckList widget.
  13. #
  14. proc RunSample {w} {
  15. set top [frame $w.f -bd 1 -relief raised]
  16. set box [tixButtonBox $w.b -bd 1 -relief raised]
  17. pack $box -side bottom -fill both
  18. pack $top -side top -fill both -expand yes
  19. #------------------------------------------------------------
  20. # Create the 1st CheckList (Multiple Selection)
  21. #
  22. set f [frame $top.f1]
  23. pack $f -side left -expand yes -fill both -padx 4
  24. set l [label $f.l -text "Choose languages: "]
  25. pack $l -side top -fill x -padx 4 -pady 4
  26. set c1 [tixCheckList $f.c -scrollbar auto]
  27. pack $c1 -expand yes -fill both -padx 4 -pady 4
  28. set b1 [button $f.btn -text "Results >>" -command "ChkList_Result $c1"]
  29. pack $b1 -anchor c
  30. #------------------------------------------------------------
  31. # Create the 2nd CheckList (Single Selection, using the -radio option)
  32. #
  33. set f [frame $top.f2]
  34. pack $f -side left -expand yes -fill both -padx 4
  35. set l [label $f.l -text "Choose one language: "]
  36. pack $l -side top -fill x -padx 4 -pady 4
  37. set c2 [tixCheckList $f.c -scrollbar auto -radio true]
  38. pack $c2 -expand yes -fill both -padx 4 -pady 4
  39. # Fill up the two checklists with languages
  40. #
  41. set names(1) "Ada"
  42. set names(2) "BCPL"
  43. set names(3) "C"
  44. set names(4) "Dylan"
  45. set names(5) "Eiffel"
  46. set names(6) "Fortran"
  47. set names(7) "Incr Tcl"
  48. set names(8) "Python"
  49. set names(9) "Scheme"
  50. set names(0) "TCL"
  51. set h1 [$c1 subwidget hlist]
  52. set h2 [$c2 subwidget hlist]
  53. foreach ent {1 2 3 4 5 6 7 8 9 0} {
  54. $h1 add $ent -itemtype imagetext -text $names($ent)
  55. }
  56. foreach ent {1 2 3 4 5 6 7 8 9 0} {
  57. $h2 add $ent -itemtype imagetext -text $names($ent)
  58. $c2 setstatus $ent off
  59. }
  60. $c1 setstatus 1 on
  61. $c1 setstatus 2 on
  62. $c1 setstatus 3 default
  63. $c1 setstatus 4 off
  64. $c1 setstatus 5 off
  65. $c1 setstatus 6 on
  66. $c1 setstatus 7 off
  67. $c1 setstatus 8 on
  68. $c1 setstatus 9 on
  69. $c1 setstatus 0 default
  70. #------------------------------------------------------------
  71. # Create the 3nd CheckList (a tree). Also, we disable some
  72. # sub-selections if the top-level selections are not selected.
  73. # i.e., if the user doesn't like any functional languages,
  74. # make sure he doesn't select Lisp.
  75. #
  76. set f [frame $top.f3]
  77. pack $f -side left -expand yes -fill both -padx 4
  78. set l [label $f.l -text "Choose languages: "]
  79. pack $l -side top -fill x -padx 4 -pady 4
  80. set c3 [tixCheckList $f.c -scrollbar auto -options {
  81. hlist.indicator 1
  82. hlist.indent 20
  83. }]
  84. pack $c3 -expand yes -fill both -padx 4 -pady 4
  85. set h3 [$c3 subwidget hlist]
  86. $h3 add 0 -itemtype imagetext -text "Functional Languages"
  87. $h3 add 1 -itemtype imagetext -text "Imperative Languages"
  88. $h3 add 0.0 -itemtype imagetext -text Lisp
  89. $h3 add 0.1 -itemtype imagetext -text Scheme
  90. $h3 add 1.0 -itemtype imagetext -text C
  91. $h3 add 1.1 -itemtype imagetext -text Python
  92. $c3 setstatus 0 on
  93. $c3 setstatus 1 on
  94. $c3 setstatus 0.0 off
  95. $c3 setstatus 0.1 off
  96. $c3 setstatus 1.0 on
  97. $c3 setstatus 1.1 off
  98. $c3 config -browsecmd "ChkList:Monitor $c3"
  99. $c3 config -command "ChkList:Monitor $c3"
  100. $c3 autosetmode
  101. global chklist tixOption
  102. set chklist(disabled) [tixDisplayStyle imagetext -fg $tixOption(disabled_fg) \
  103. -refwindow [$c3 subwidget hlist]]
  104. set chklist(normal) [tixDisplayStyle imagetext -fg black \
  105. -refwindow [$c3 subwidget hlist]]
  106. # Create the buttons
  107. #
  108. $box add ok -text Ok -command "destroy $w" -width 6
  109. $box add cancel -text Cancel -command "destroy $w" -width 6
  110. }
  111. proc ChkList_Result {clist} {
  112. tixDemo:Status "Selected items: [$clist getselection on]"
  113. tixDemo:Status "Unselected items: [$clist getselection off]"
  114. tixDemo:Status "Default items: [$clist getselection default]"
  115. }
  116. # This function monitors if any of the two "general groups"
  117. # (functional and imperative languages) are de-selected. If so, it
  118. # sets all the sub-selections to non-selectable by setting their -state
  119. # to disabled.
  120. #
  121. proc ChkList:Monitor {c3 ent} {
  122. global chklist
  123. set h [$c3 subwidget hlist]
  124. if {[$c3 getstatus 0] == "on"} {
  125. set state normal
  126. } else {
  127. set state disabled
  128. }
  129. $h entryconfig 0.0 -state $state -style $chklist($state)
  130. $h entryconfig 0.1 -state $state -style $chklist($state)
  131. if {[$c3 getstatus 1] == "on"} {
  132. set state normal
  133. } else {
  134. set state disabled
  135. }
  136. $h entryconfig 1.0 -state $state -style $chklist($state)
  137. $h entryconfig 1.1 -state $state -style $chklist($state)
  138. }
  139. if {![info exists tix_demo_running]} {
  140. wm withdraw .
  141. set w .demo
  142. toplevel $w; wm transient $w ""
  143. RunSample $w
  144. bind $w <Destroy> exit
  145. }