Tix.tcl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Tix.tcl,v 1.14 2008/03/17 23:01:10 hobbs Exp $
  4. #
  5. # Tix.tcl --
  6. #
  7. # This file implements the Tix application context class
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. # Copyright (c) 2004 ActiveState
  12. #
  13. # See the file "license.terms" for information on usage and redistribution
  14. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15. #
  16. tixClass tixAppContext {
  17. -superclass {}
  18. -classname TixAppContext
  19. -method {
  20. cget configure addbitmapdir filedialog getbitmap getimage
  21. option platform resetoptions setbitmap initstyle
  22. }
  23. -flag {
  24. -binding -debug -extracmdargs -filedialog -fontset -grabmode
  25. -haspixmap -libdir -scheme -schemepriority -percentsubst
  26. }
  27. -readonly {
  28. -haspixmap
  29. }
  30. -configspec {
  31. {-binding TK}
  32. {-debug 0}
  33. {-extracmdargs 1}
  34. {-filedialog ""}
  35. {-fontset WmDefault}
  36. {-grabmode global}
  37. {-haspixmap 0}
  38. {-libdir ""}
  39. {-percentsubst 0}
  40. {-scheme WmDefault}
  41. {-schemepriority 21}
  42. }
  43. -alias {
  44. }
  45. }
  46. proc tixAppContext:Constructor {w} {
  47. upvar #0 $w data
  48. global tix_priv tix_library tixOption
  49. if {[info exists data(initialized)]} {
  50. error "tixAppContext has already be initialized"
  51. } else {
  52. set data(initialized) 1
  53. }
  54. set data(et) [string equal $tix_library ""]
  55. set data(image) 0
  56. # These options were set when Tix was loaded
  57. #
  58. set data(-binding) $tix_priv(-binding)
  59. set data(-debug) $tix_priv(-debug)
  60. set data(-fontset) $tix_priv(-fontset)
  61. set data(-scheme) $tix_priv(-scheme)
  62. set data(-schemepriority) $tix_priv(-schemepriority)
  63. if {![info exists tix_priv(isSafe)]} {
  64. set data(-libdir) [file normalize $tix_library]
  65. }
  66. set tixOption(prioLevel) $tix_priv(-schemepriority)
  67. # Compatibility stuff: the obsolete name courier_font has been changed to
  68. # fixed_font
  69. set tixOption(fixed_font) Courier
  70. set tixOption(courier_font) $tixOption(fixed_font)
  71. # Enable/Disable Intrinsics debugging
  72. #
  73. set tix_priv(debug) [string is true -strict $data(-debug)]
  74. tixAppContext:BitmapInit $w
  75. tixAppContext:FileDialogInit $w
  76. # Clean up any error message generated by the above loop
  77. set ::errorInfo ""
  78. }
  79. proc tixAppContext:initstyle {w} {
  80. # Do the init stuff here that affects styles
  81. upvar #0 $w data
  82. global tix_priv
  83. if {![info exists tix_priv(isSafe)]} {
  84. tixAppContext:config-fontset $w $data(-fontset)
  85. tixAppContext:config-scheme $w $data(-scheme)
  86. }
  87. tixAppContext:BitmapInit $w
  88. tixAppContext:FileDialogInit $w
  89. # Force the "." window to accept the new Tix options
  90. #
  91. set noconfig [list -class -colormap -container -menu -screen -use -visual]
  92. set noconfig [lsort $noconfig]
  93. foreach spec [. configure] {
  94. set flag [lindex $spec 0]
  95. if {[llength $spec] != 5
  96. || [lsearch -exact -sorted $noconfig $flag] != -1} {
  97. continue
  98. }
  99. set name [lindex $spec 1]
  100. set class [lindex $spec 2]
  101. set value [option get . $name $class]
  102. catch {. configure $flag $value}
  103. }
  104. }
  105. #----------------------------------------------------------------------
  106. # Configurations
  107. #
  108. #----------------------------------------------------------------------
  109. proc tixAppContext:resetoptions {w scheme fontset {schemePrio ""}} {
  110. upvar #0 $w data
  111. if {! $data(et)} {
  112. global tixOption
  113. option clear
  114. if {$schemePrio != ""} {
  115. set tixOption(prioLevel) $schemePrio
  116. }
  117. tixAppContext:config-scheme $w $scheme
  118. tixAppContext:config-fontset $w $fontset
  119. }
  120. }
  121. proc tixAppContext:StartupError {args} {
  122. bgerror [join $args "\n"]
  123. }
  124. proc tixAppContext:config-fontset {w value} {
  125. upvar #0 $w data
  126. global tix_priv tixOption
  127. set data(-fontset) $value
  128. #-----------------------------------
  129. # Initialization of options database
  130. #-----------------------------------
  131. # Load the fontset
  132. #
  133. if {!$data(et)} {
  134. set prefDir [file join $data(-libdir) pref]
  135. set fontSetFile [file join $prefDir $data(-fontset).fsc]
  136. if {[file exists $fontSetFile]} {
  137. source $fontSetFile
  138. tixPref:InitFontSet:$data(-fontset)
  139. tixPref:SetFontSet:$data(-fontset)
  140. } else {
  141. tixAppContext:StartupError \
  142. " Error: cannot use fontset \"$data(-fontset)\"" \
  143. " Using default fontset "
  144. tixSetDefaultFontset
  145. }
  146. } else {
  147. if [catch {
  148. tixPref:InitFontSet:$data(-fontset)
  149. tixPref:SetFontSet:$data(-fontset)
  150. }] {
  151. # User chose non-existent fontset
  152. #
  153. tixAppContext:StartupError \
  154. " Error: cannot use fontset \"$data(-fontset)\"" \
  155. " Using default fontset "
  156. tixSetDefaultFontset
  157. }
  158. }
  159. }
  160. proc tixAppContext:config-scheme {w value} {
  161. upvar #0 $w data
  162. global tix_priv
  163. set data(-scheme) $value
  164. # Load the color scheme
  165. #
  166. if {!$data(et)} {
  167. set schemeName [file join [file join $data(-libdir) pref] \
  168. $data(-scheme).csc]
  169. if {[file exists $schemeName]} {
  170. source $schemeName
  171. tixPref:SetScheme-Color:$data(-scheme)
  172. } else {
  173. tixAppContext:StartupError \
  174. " Error: cannot use color scheme \"$data(-scheme)\"" \
  175. " Using default color scheme"
  176. tixSetDefaultScheme-Color
  177. }
  178. } else {
  179. if [catch {tixPref:SetScheme-Color:$data(-scheme)}] {
  180. # User chose non-existent color scheme
  181. #
  182. tixAppContext:StartupError \
  183. " Error: cannot use color scheme \"$data(-scheme)\"" \
  184. " Using default color scheme"
  185. tixSetDefaultScheme-Color
  186. }
  187. }
  188. }
  189. #----------------------------------------------------------------------
  190. # Private methods
  191. #
  192. #----------------------------------------------------------------------
  193. proc tixAppContext:BitmapInit {w} {
  194. upvar #0 $w data
  195. # See whether we have pixmap extension
  196. #
  197. set data(-haspixmap) true
  198. # Dynamically set the bitmap directory
  199. #
  200. if {! $data(et)} {
  201. set data(bitmapdirs) [list [file join $data(-libdir) bitmaps]]
  202. } else {
  203. set data(bitmapdirs) ""
  204. }
  205. }
  206. proc tixAppContext:FileDialogInit {w} {
  207. upvar #0 $w data
  208. if {$data(-filedialog) == ""} {
  209. set data(-filedialog) [option get . fileDialog FileDialog]
  210. }
  211. if {$data(-filedialog) == ""} {
  212. set data(-filedialog) tixFileSelectDialog
  213. }
  214. }
  215. #----------------------------------------------------------------------
  216. # Public methods
  217. #----------------------------------------------------------------------
  218. proc tixAppContext:addbitmapdir {w bmpdir} {
  219. upvar #0 $w data
  220. if {[lsearch $data(bitmapdirs) $bmpdir] == -1} {
  221. lappend data(bitmapdirs) $bmpdir
  222. }
  223. }
  224. proc tixAppContext:getimage {w name} {
  225. upvar #0 $w data
  226. global tix_priv
  227. if {[info exists data(img:$name)]} {
  228. return $data(img:$name)
  229. }
  230. if {![info exists tix_priv(isSafe)]} {
  231. foreach dir $data(bitmapdirs) {
  232. foreach {ext type} {
  233. .xpm pixmap
  234. .gif photo
  235. .ppm photo
  236. .xbm bitmap
  237. "" bitmap
  238. } {
  239. set file [file join $dir $name$ext]
  240. if {[file exists $file]
  241. && ![catch {
  242. set img tiximage$data(image)
  243. set data(img:$name) \
  244. [image create $type $img -file $file]
  245. }]} {
  246. incr data(image)
  247. break
  248. }
  249. }
  250. if {[info exists data(img:$name)]} {
  251. return $data(img:$name)
  252. }
  253. }
  254. }
  255. if {![info exists data(img:$name)]} {
  256. catch {
  257. set img tiximage$data(image)
  258. # This is for compiled-in images
  259. set data(img:$name) [image create pixmap $img -id $name]
  260. } err
  261. if {[string match internal* $err]} {
  262. error $err
  263. } else {
  264. incr data(image)
  265. }
  266. }
  267. if {[info exists data(img:$name)]} {
  268. return $data(img:$name)
  269. } else {
  270. error "image file \"$name\" cannot be found"
  271. }
  272. }
  273. proc tixAppContext:getbitmap {w bitmapname} {
  274. upvar #0 $w data
  275. global tix_priv
  276. if {[info exists data(bmp:$bitmapname)]} {
  277. return $data(bmp:$bitmapname)
  278. } else {
  279. set ext [file extension $bitmapname]
  280. if {$ext == ""} {
  281. set ext .xbm
  282. }
  283. # This is the fallback value. If we can't find the bitmap in
  284. # the bitmap directories, then use the name of the bitmap
  285. # as the default value.
  286. #
  287. set data(bmp:$bitmapname) $bitmapname
  288. if {[info exists tix_priv(isSafe)]} {
  289. return $data(bmp:$bitmapname)
  290. }
  291. foreach dir $data(bitmapdirs) {
  292. if {$ext eq ".xbm" &&
  293. [file exists [file join $dir $bitmapname.xbm]]} {
  294. set data(bmp:$bitmapname) \
  295. @[file join $dir $bitmapname.xbm]
  296. break
  297. }
  298. if {[file exists [file join $dir $bitmapname]]} {
  299. set data(bmp:$bitmapname) @[file join $dir $bitmapname]
  300. break
  301. }
  302. }
  303. return $data(bmp:$bitmapname)
  304. }
  305. }
  306. proc tixAppContext:filedialog {w {type tixFileSelectDialog}} {
  307. upvar #0 $w data
  308. if {$type == ""} {
  309. set type $data(-filedialog)
  310. }
  311. if {![info exists data(filedialog,$type)]} {
  312. set data(filedialog,$type) ""
  313. }
  314. if {$data(filedialog,$type) == "" || \
  315. ![winfo exists $data(filedialog,$type)]} {
  316. set data(filedialog,$type) [$type .tixapp_filedialog_$type]
  317. }
  318. return $data(filedialog,$type)
  319. }
  320. proc tixAppContext:option {w action {option ""} {value ""}} {
  321. global tixOption
  322. if {$action eq "get"} {
  323. if {$option == ""} {return [lsort [array names tixOption]]}
  324. return $tixOption($option)
  325. }
  326. }
  327. proc tixAppContext:platform {w} {
  328. return $::tcl_platform(platform)
  329. }
  330. proc tixDebug {message {level "1"}} {
  331. set debug [tix cget -debug]
  332. if {![string is true -strict $debug]} { return }
  333. if {$debug > 0} {
  334. # use $level here
  335. if {[catch {fconfigure stderr}]} {
  336. # This will happen under PYTHONW.EXE or frozen Windows apps
  337. proc tixDebug args {}
  338. } else {
  339. puts stderr $message
  340. }
  341. }
  342. }
  343. if {![llength [info commands toplevel]]} {
  344. interp alias {} toplevel {} frame
  345. }