SListBox.tcl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: SListBox.tcl,v 1.5 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # SListBox.tcl --
  6. #
  7. # This file implements Scrolled Listbox widgets
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15. # ToDo:
  16. # -anchor (none)
  17. #
  18. tixWidgetClass tixScrolledListBox {
  19. -classname TixScrolledListBox
  20. -superclass tixScrolledWidget
  21. -method {
  22. }
  23. -flag {
  24. -anchor -browsecmd -command -state
  25. }
  26. -static {
  27. -anchor
  28. }
  29. -configspec {
  30. {-anchor anchor Anchor w}
  31. {-browsecmd browseCmd BrowseCmd ""}
  32. {-command command Command ""}
  33. {-state state State normal}
  34. {-takefocus takeFocus TakeFocus 1 tixVerifyBoolean}
  35. }
  36. -default {
  37. {.scrollbar auto}
  38. {*borderWidth 1}
  39. {*listbox.highlightBackground #d9d9d9}
  40. {*listbox.relief sunken}
  41. {*listbox.background #c3c3c3}
  42. {*listbox.takeFocus 1}
  43. {*Scrollbar.takeFocus 0}
  44. }
  45. }
  46. proc tixScrolledListBox:InitWidgetRec {w} {
  47. upvar #0 $w data
  48. tixChainMethod $w InitWidgetRec
  49. set data(x-first) 0
  50. set data(x-last) 1
  51. set data(y-first) 0
  52. set data(y-last) 1
  53. }
  54. proc tixScrolledListBox:ConstructWidget {w} {
  55. upvar #0 $w data
  56. tixChainMethod $w ConstructWidget
  57. set data(w:listbox) \
  58. [listbox $w.listbox]
  59. set data(w:hsb) \
  60. [scrollbar $w.hsb -orient horizontal]
  61. set data(w:vsb) \
  62. [scrollbar $w.vsb -orient vertical ]
  63. set data(pw:client) $data(w:listbox)
  64. }
  65. proc tixScrolledListBox:SetBindings {w} {
  66. upvar #0 $w data
  67. tixChainMethod $w SetBindings
  68. $data(w:listbox) config \
  69. -xscrollcommand "tixScrolledListBox:XView $w"\
  70. -yscrollcommand "tixScrolledListBox:YView $w"
  71. $data(w:hsb) config -command "$data(w:listbox) xview"
  72. $data(w:vsb) config -command "$data(w:listbox) yview"
  73. bind $w <Configure> "+tixScrolledListBox:Configure $w"
  74. bind $w <FocusIn> "focus $data(w:listbox)"
  75. bindtags $data(w:listbox) \
  76. "$data(w:listbox) TixListboxState Listbox TixListbox [winfo toplevel $data(w:listbox)] all"
  77. tixSetMegaWidget $data(w:listbox) $w
  78. }
  79. proc tixScrolledListBoxBind {} {
  80. tixBind TixListboxState <1> {
  81. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  82. break
  83. }
  84. }
  85. tixBind TixListbox <1> {
  86. if {[string is true -strict [%W cget -takefocus]]} {
  87. focus %W
  88. }
  89. tixScrolledListBox:Browse [tixGetMegaWidget %W]
  90. }
  91. tixBind TixListboxState <B1-Motion> {
  92. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  93. break
  94. }
  95. }
  96. tixBind TixListbox <B1-Motion> {
  97. tixScrolledListBox:Browse [tixGetMegaWidget %W]
  98. }
  99. tixBind TixListboxState <Up> {
  100. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  101. break
  102. }
  103. }
  104. tixBind TixListbox <Up> {
  105. tixScrolledListBox:KeyBrowse [tixGetMegaWidget %W]
  106. }
  107. tixBind TixListboxState <Down> {
  108. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  109. break
  110. }
  111. }
  112. tixBind TixListbox <Down> {
  113. tixScrolledListBox:KeyBrowse [tixGetMegaWidget %W]
  114. }
  115. tixBind TixListboxState <Return> {
  116. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  117. break
  118. }
  119. }
  120. tixBind TixListbox <Return> {
  121. tixScrolledListBox:KeyInvoke [tixGetMegaWidget %W]
  122. }
  123. tixBind TixListboxState <Double-1> {
  124. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  125. break
  126. }
  127. }
  128. tixBind TixListbox <Double-1> {
  129. tixScrolledListBox:Invoke [tixGetMegaWidget %W]
  130. }
  131. tixBind TixListboxState <ButtonRelease-1> {
  132. if {[set [tixGetMegaWidget %W](-state)] eq "disabled"} {
  133. break
  134. }
  135. }
  136. tixBind TixListbox <ButtonRelease-1> {
  137. tixScrolledListBox:Browse [tixGetMegaWidget %W]
  138. }
  139. }
  140. proc tixScrolledListBox:Browse {w} {
  141. upvar #0 $w data
  142. if {$data(-browsecmd) != ""} {
  143. set bind(specs) {%V}
  144. set bind(%V) [$data(w:listbox) get \
  145. [$data(w:listbox) nearest [tixEvent flag y]]]
  146. tixEvalCmdBinding $w $data(-browsecmd) bind
  147. }
  148. }
  149. proc tixScrolledListBox:KeyBrowse {w} {
  150. upvar #0 $w data
  151. if {$data(-browsecmd) != ""} {
  152. set bind(specs) {%V}
  153. set bind(%V) [$data(w:listbox) get active]
  154. tixEvalCmdBinding $w $data(-browsecmd) bind
  155. }
  156. }
  157. # tixScrolledListBox:Invoke --
  158. #
  159. # The user has invoked the listbox by pressing either the <Returh>
  160. # key or double-clicking. Call the user-supplied -command function.
  161. #
  162. # For both -browsecmd and -command, it is the responsibility of the
  163. # user-supplied function to determine the current selection of the listbox
  164. #
  165. proc tixScrolledListBox:Invoke {w} {
  166. upvar #0 $w data
  167. if {$data(-command) != ""} {
  168. set bind(specs) {%V}
  169. set bind(%V) [$data(w:listbox) get \
  170. [$data(w:listbox) nearest [tixEvent flag y]]]
  171. tixEvalCmdBinding $w $data(-command) bind
  172. }
  173. }
  174. proc tixScrolledListBox:KeyInvoke {w} {
  175. upvar #0 $w data
  176. if {$data(-command) != ""} {
  177. set bind(specs) {%V}
  178. set bind(%V) [$data(w:listbox) get active]
  179. tixEvalCmdBinding $w $data(-command) bind
  180. }
  181. }
  182. #----------------------------------------------------------------------
  183. #
  184. # option configs
  185. #----------------------------------------------------------------------
  186. proc tixScrolledListBox:config-takefocus {w value} {
  187. upvar #0 $w data
  188. $data(w:listbox) config -takefocus $value
  189. }
  190. #----------------------------------------------------------------------
  191. #
  192. # Widget commands
  193. #----------------------------------------------------------------------
  194. #----------------------------------------------------------------------
  195. #
  196. # Private Methods
  197. #----------------------------------------------------------------------
  198. proc tixScrolledListBox:XView {w first last} {
  199. upvar #0 $w data
  200. set data(x-first) $first
  201. set data(x-last) $last
  202. $data(w:hsb) set $first $last
  203. tixWidgetDoWhenIdle tixScrolledWidget:Configure $w
  204. }
  205. proc tixScrolledListBox:YView {w first last} {
  206. upvar #0 $w data
  207. set data(y-first) $first
  208. set data(y-last) $last
  209. $data(w:vsb) set $first $last
  210. tixWidgetDoWhenIdle tixScrolledWidget:Configure $w
  211. # Somehow an update here must be used to advoid osscilation
  212. #
  213. update idletasks
  214. }
  215. #
  216. #----------------------------------------------------------------------
  217. # virtual functions to query the client window's scroll requirement
  218. #----------------------------------------------------------------------
  219. proc tixScrolledListBox:GeometryInfo {w mW mH} {
  220. upvar #0 $w data
  221. return [list \
  222. [list $data(x-first) $data(x-last)]\
  223. [list $data(y-first) $data(y-last)]]
  224. }
  225. proc tixScrolledListBox:Configure {w} {
  226. upvar #0 $w data
  227. tixWidgetDoWhenIdle tixScrolledListBox:TrickScrollbar $w
  228. if {$data(-anchor) eq "e"} {
  229. $data(w:listbox) xview 100000
  230. }
  231. }
  232. # This procedure is necessary because listbox does not call x,y scroll command
  233. # when its size is changed
  234. #
  235. proc tixScrolledListBox:TrickScrollbar {w} {
  236. upvar #0 $w data
  237. set inc [$data(w:listbox) select include 0]
  238. $data(w:listbox) select set 0
  239. if {!$inc} {
  240. $data(w:listbox) select clear 0
  241. }
  242. }