Primitiv.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Primitiv.tcl,v 1.7 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # Primitiv.tcl --
  6. #
  7. # This is the primitive widget. It is just a frame with proper
  8. # inheritance wrapping. All new Tix widgets will be derived from
  9. # this widget
  10. #
  11. # Copyright (c) 1993-1999 Ioi Kim Lam.
  12. # Copyright (c) 2000-2001 Tix Project Group.
  13. #
  14. # See the file "license.terms" for information on usage and redistribution
  15. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  16. #
  17. # No superclass, so the superclass switch is not used
  18. #
  19. #
  20. tixWidgetClass tixPrimitive {
  21. -virtual true
  22. -superclass {}
  23. -classname TixPrimitive
  24. -method {
  25. cget configure subwidget subwidgets
  26. }
  27. -flag {
  28. -background -borderwidth -cursor
  29. -height -highlightbackground -highlightcolor -highlightthickness
  30. -options -relief -takefocus -width -bd -bg
  31. }
  32. -static {
  33. -options
  34. }
  35. -configspec {
  36. {-background background Background #d9d9d9}
  37. {-borderwidth borderWidth BorderWidth 0}
  38. {-cursor cursor Cursor ""}
  39. {-height height Height 0}
  40. {-highlightbackground highlightBackground HighlightBackground #c3c3c3}
  41. {-highlightcolor highlightColor HighlightColor black}
  42. {-highlightthickness highlightThickness HighlightThickness 0}
  43. {-options options Options ""}
  44. {-relief relief Relief flat}
  45. {-takefocus takeFocus TakeFocus 0 tixVerifyBoolean}
  46. {-width width Width 0}
  47. }
  48. -alias {
  49. {-bd -borderwidth}
  50. {-bg -background}
  51. }
  52. }
  53. #----------------------------------------------------------------------
  54. # ClassInitialization:
  55. #----------------------------------------------------------------------
  56. # not used
  57. # Implemented in C
  58. #
  59. # Override: never
  60. proc tixPrimitive:Constructor {w args} {
  61. upvar #0 $w data
  62. upvar #0 $data(className) classRec
  63. # Set up some minimal items in the class record.
  64. #
  65. set data(w:root) $w
  66. set data(rootCmd) $w:root
  67. # We need to create the root widget in order to parse the options
  68. # database
  69. tixCallMethod $w CreateRootWidget
  70. # Parse the default options from the options database
  71. #
  72. tixPrimitive:ParseDefaultOptions $w
  73. # Parse the options supplied by the user
  74. #
  75. tixPrimitive:ParseUserOptions $w $args
  76. # Rename the widget command so that it can be use to access
  77. # the methods of this class
  78. tixPrimitive:MkWidgetCmd $w
  79. # Inistalize the Widget Record
  80. #
  81. tixCallMethod $w InitWidgetRec
  82. # Construct the compound widget
  83. #
  84. tixCallMethod $w ConstructWidget
  85. # Do the bindings
  86. #
  87. tixCallMethod $w SetBindings
  88. # Call the configuration methods for all "force call" options
  89. #
  90. foreach option $classRec(forceCall) {
  91. tixInt_ChangeOptions $w $option $data($option)
  92. }
  93. }
  94. # Create only the root widget. We need the root widget to query the option
  95. # database.
  96. #
  97. # Override: seldom. (unless you want to use a toplevel as root widget)
  98. # Chain : never.
  99. proc tixPrimitive:CreateRootWidget {w args} {
  100. upvar #0 $w data
  101. upvar #0 $data(className) classRec
  102. frame $w -class $data(ClassName)
  103. }
  104. proc tixPrimitive:ParseDefaultOptions {w} {
  105. upvar #0 $w data
  106. upvar #0 $data(className) classRec
  107. # SET UP THE INSTANCE RECORD ACCORDING TO DEFAULT VALUES IN
  108. # THE OPTIONS DATABASE
  109. #
  110. foreach option $classRec(options) {
  111. set spec [tixInt_GetOptionSpec $data(className) $option]
  112. if {[lindex $spec 0] eq "="} {
  113. continue
  114. }
  115. set o_name [lindex $spec 1]
  116. set o_class [lindex $spec 2]
  117. set o_default [lindex $spec 3]
  118. if {![catch {option get $w $o_name $o_class} db_default]} {
  119. if {$db_default ne ""} {
  120. set data($option) $db_default
  121. } else {
  122. set data($option) $o_default
  123. }
  124. } else {
  125. set data($option) $o_default
  126. }
  127. }
  128. }
  129. proc tixPrimitive:ParseUserOptions {w arglist} {
  130. upvar #0 $w data
  131. upvar #0 $data(className) classRec
  132. # SET UP THE INSTANCE RECORD ACCORDING TO COMMAND ARGUMENTS FROM
  133. # THE USER OF THE TIX LIBRARY (i.e. Application programmer:)
  134. #
  135. foreach {option arg} $arglist {
  136. if {[lsearch $classRec(options) $option] != "-1"} {
  137. set spec [tixInt_GetOptionSpec $data(className) $option]
  138. if {[lindex $spec 0] ne "="} {
  139. set data($option) $arg
  140. } else {
  141. set realOption [lindex $spec 1]
  142. set data($realOption) $arg
  143. }
  144. } else {
  145. error "unknown option $option. Should be: [tixInt_ListOptions $w]"
  146. }
  147. }
  148. }
  149. #----------------------------------------------------------------------
  150. # Initialize the widget record
  151. #
  152. #
  153. # Override: always
  154. # Chain : always, before
  155. proc tixPrimitive:InitWidgetRec {w} {
  156. # default: do nothing
  157. }
  158. #----------------------------------------------------------------------
  159. # SetBindings
  160. #
  161. #
  162. # Override: sometimes
  163. # Chain : sometimes, before
  164. #
  165. bind TixDestroyHandler <Destroy> {
  166. [tixGetMethod %W [set %W(className)] Destructor] %W
  167. }
  168. proc tixPrimitive:SetBindings {w} {
  169. upvar #0 $w data
  170. if {[winfo toplevel $w] eq $w} {
  171. bindtags $w [concat TixDestroyHandler [bindtags $w]]
  172. } else {
  173. bind $data(w:root) <Destroy> \
  174. "[tixGetMethod $w $data(className) Destructor] $w"
  175. }
  176. }
  177. #----------------------------------------------------------------------
  178. # PrivateMethod: ConstructWidget
  179. #
  180. # Construct and set up the compound widget
  181. #
  182. # Override: sometimes
  183. # Chain : sometimes, before
  184. #
  185. proc tixPrimitive:ConstructWidget {w} {
  186. upvar #0 $w data
  187. $data(rootCmd) config \
  188. -background $data(-background) \
  189. -borderwidth $data(-borderwidth) \
  190. -cursor $data(-cursor) \
  191. -relief $data(-relief)
  192. if {$data(-width) != 0} {
  193. $data(rootCmd) config -width $data(-width)
  194. }
  195. if {$data(-height) != 0} {
  196. $data(rootCmd) config -height $data(-height)
  197. }
  198. set rootname *[string range $w 1 end]
  199. foreach {spec value} $data(-options) {
  200. option add $rootname*$spec $value 100
  201. }
  202. }
  203. #----------------------------------------------------------------------
  204. # PrivateMethod: MkWidgetCmd
  205. #
  206. # Construct and set up the compound widget
  207. #
  208. # Override: sometimes
  209. # Chain : sometimes, before
  210. #
  211. proc tixPrimitive:MkWidgetCmd {w} {
  212. upvar #0 $w data
  213. rename $w $data(rootCmd)
  214. tixInt_MkInstanceCmd $w
  215. }
  216. #----------------------------------------------------------------------
  217. # ConfigOptions:
  218. #----------------------------------------------------------------------
  219. #----------------------------------------------------------------------
  220. # ConfigMethod: config
  221. #
  222. # Configure one option.
  223. #
  224. # Override: always
  225. # Chain : automatic.
  226. #
  227. # Note the hack of [winfo width] in this procedure
  228. #
  229. # The hack is necessary because of the bad interaction between TK's geometry
  230. # manager (the packer) and the frame widget. The packer determines the size
  231. # of the root widget of the ComboBox (a frame widget) according to the
  232. # requirement of the slaves inside the frame widget, NOT the -width
  233. # option of the frame widget.
  234. #
  235. # However, everytime the frame widget is
  236. # configured, it sends a geometry request to the packer according to its
  237. # -width and -height options and the packer will temporarily resize
  238. # the frame widget according to the requested size! The packer then realizes
  239. # something is wrong and revert to the size determined by the slaves. This
  240. # cause a flash on the screen.
  241. #
  242. foreach opt {-height -width -background -borderwidth -cursor
  243. -highlightbackground -highlightcolor -relief -takefocus -bd -bg} {
  244. set tixPrimOpt($opt) 1
  245. }
  246. proc tixPrimitive:config {w option value} {
  247. global tixPrimOpt
  248. upvar #0 $w data
  249. if {[info exists tixPrimOpt($option)]} {
  250. $data(rootCmd) config $option $value
  251. }
  252. }
  253. #----------------------------------------------------------------------
  254. # PublicMethods:
  255. #----------------------------------------------------------------------
  256. #----------------------------------------------------------------------
  257. # This method is used to implement the "subwidgets" widget command.
  258. # Will be re-written in C. It can't be used as a public method because
  259. # of the lame substring comparison routines used in tixClass.c
  260. #
  261. #
  262. proc tixPrimitive:subwidgets {w type args} {
  263. upvar #0 $w data
  264. case $type {
  265. -class {
  266. set name [lindex $args 0]
  267. set args [lrange $args 1 end]
  268. # access subwidgets of a particular class
  269. #
  270. # note: if $name=="Frame", will *not return the root widget as well
  271. #
  272. set sub ""
  273. foreach des [tixDescendants $w] {
  274. if {[winfo class $des] eq $name} {
  275. lappend sub $des
  276. }
  277. }
  278. # Note: if the there is no subwidget of this class, does not
  279. # cause any error.
  280. #
  281. if {$args eq ""} {
  282. return $sub
  283. } else {
  284. foreach des $sub {
  285. eval [linsert $args 0 $des]
  286. }
  287. return ""
  288. }
  289. }
  290. -group {
  291. set name [lindex $args 0]
  292. set args [lrange $args 1 end]
  293. # access subwidgets of a particular group
  294. #
  295. if {[info exists data(g:$name)]} {
  296. if {$args eq ""} {
  297. set ret ""
  298. foreach item $data(g:$name) {
  299. lappend ret $w.$item
  300. }
  301. return $ret
  302. } else {
  303. foreach item $data(g:$name) {
  304. eval [linsert $args 0 $w.$item]
  305. }
  306. return ""
  307. }
  308. } else {
  309. error "no such subwidget group $name"
  310. }
  311. }
  312. -all {
  313. set sub [tixDescendants $w]
  314. if {$args eq ""} {
  315. return $sub
  316. } else {
  317. foreach des $sub {
  318. eval [linsert $args 0 $des]
  319. }
  320. return ""
  321. }
  322. }
  323. default {
  324. error "unknown flag $type, should be -all, -class or -group"
  325. }
  326. }
  327. }
  328. #----------------------------------------------------------------------
  329. # PublicMethod: subwidget
  330. #
  331. # Access a subwidget withe a particular name
  332. #
  333. # Override: never
  334. # Chain : never
  335. #
  336. # This is implemented in native C code in tixClass.c
  337. #
  338. proc tixPrimitive:subwidget {w name args} {
  339. upvar #0 $w data
  340. if {[info exists data(w:$name)]} {
  341. if {$args eq ""} {
  342. return $data(w:$name)
  343. } else {
  344. return [eval [linsert $args 0 $data(w:$name)]]
  345. }
  346. } else {
  347. error "no such subwidget $name"
  348. }
  349. }
  350. #----------------------------------------------------------------------
  351. # PrivateMethods:
  352. #----------------------------------------------------------------------
  353. # delete the widget record and remove the command
  354. #
  355. proc tixPrimitive:Destructor {w} {
  356. upvar #0 $w data
  357. if {![info exists data(w:root)]} {
  358. return
  359. }
  360. if {[llength [info commands $w]]} {
  361. # remove the command
  362. rename $w ""
  363. }
  364. if {[llength [info commands $data(rootCmd)]]} {
  365. # remove the command of the root widget
  366. rename $data(rootCmd) ""
  367. }
  368. # delete the widget record
  369. catch {unset data}
  370. }