comdlg.tcl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # comdlg.tcl --
  2. #
  3. # Some functions needed for the common dialog boxes. Probably need to go
  4. # in a different file.
  5. #
  6. # Copyright (c) 1996 Sun Microsystems, Inc.
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11. # tclParseConfigSpec --
  12. #
  13. # Parses a list of "-option value" pairs. If all options and
  14. # values are legal, the values are stored in
  15. # $data($option). Otherwise an error message is returned. When
  16. # an error happens, the data() array may have been partially
  17. # modified, but all the modified members of the data(0 array are
  18. # guaranteed to have valid values. This is different than
  19. # Tk_ConfigureWidget() which does not modify the value of a
  20. # widget record if any error occurs.
  21. #
  22. # Arguments:
  23. #
  24. # w = widget record to modify. Must be the pathname of a widget.
  25. #
  26. # specs = {
  27. # {-commandlineswitch resourceName ResourceClass defaultValue verifier}
  28. # {....}
  29. # }
  30. #
  31. # flags = a list of flags. Currently supported flags are:
  32. # DONTSETDEFAULTS = skip default values setting
  33. #
  34. # argList = The list of "-option value" pairs.
  35. #
  36. proc tclParseConfigSpec {w specs flags argList} {
  37. upvar #0 $w data
  38. # 1: Put the specs in associative arrays for faster access
  39. #
  40. foreach spec $specs {
  41. if {[llength $spec] < 4} {
  42. return -code error -errorcode {TK VALUE CONFIG_SPEC} \
  43. "\"spec\" should contain 5 or 4 elements"
  44. }
  45. set cmdsw [lindex $spec 0]
  46. set cmd($cmdsw) ""
  47. set rname($cmdsw) [lindex $spec 1]
  48. set rclass($cmdsw) [lindex $spec 2]
  49. set def($cmdsw) [lindex $spec 3]
  50. set verproc($cmdsw) [lindex $spec 4]
  51. }
  52. if {[llength $argList] & 1} {
  53. set cmdsw [lindex $argList end]
  54. if {![info exists cmd($cmdsw)]} {
  55. return -code error -errorcode [list TK LOOKUP OPTION $cmdsw] \
  56. "bad option \"$cmdsw\": must be [tclListValidFlags cmd]"
  57. }
  58. return -code error -errorcode {TK VALUE_MISSING} \
  59. "value for \"$cmdsw\" missing"
  60. }
  61. # 2: set the default values
  62. #
  63. if {"DONTSETDEFAULTS" ni $flags} {
  64. foreach cmdsw [array names cmd] {
  65. set data($cmdsw) $def($cmdsw)
  66. }
  67. }
  68. # 3: parse the argument list
  69. #
  70. foreach {cmdsw value} $argList {
  71. if {![info exists cmd($cmdsw)]} {
  72. return -code error -errorcode [list TK LOOKUP OPTION $cmdsw] \
  73. "bad option \"$cmdsw\": must be [tclListValidFlags cmd]"
  74. }
  75. set data($cmdsw) $value
  76. }
  77. # Done!
  78. }
  79. proc tclListValidFlags {v} {
  80. upvar $v cmd
  81. set len [llength [array names cmd]]
  82. set i 1
  83. set separator ""
  84. set errormsg ""
  85. foreach cmdsw [lsort [array names cmd]] {
  86. append errormsg "$separator$cmdsw"
  87. incr i
  88. if {$i == $len} {
  89. set separator ", or "
  90. } else {
  91. set separator ", "
  92. }
  93. }
  94. return $errormsg
  95. }
  96. #----------------------------------------------------------------------
  97. #
  98. # Focus Group
  99. #
  100. # Focus groups are used to handle the user's focusing actions inside a
  101. # toplevel.
  102. #
  103. # One example of using focus groups is: when the user focuses on an
  104. # entry, the text in the entry is highlighted and the cursor is put to
  105. # the end of the text. When the user changes focus to another widget,
  106. # the text in the previously focused entry is validated.
  107. #
  108. #----------------------------------------------------------------------
  109. # ::tk::FocusGroup_Create --
  110. #
  111. # Create a focus group. All the widgets in a focus group must be
  112. # within the same focus toplevel. Each toplevel can have only
  113. # one focus group, which is identified by the name of the
  114. # toplevel widget.
  115. #
  116. proc ::tk::FocusGroup_Create {t} {
  117. variable ::tk::Priv
  118. if {[winfo toplevel $t] ne $t} {
  119. return -code error -errorcode [list TK LOOKUP TOPLEVEL $t] \
  120. "$t is not a toplevel window"
  121. }
  122. if {![info exists Priv(fg,$t)]} {
  123. set Priv(fg,$t) 1
  124. set Priv(focus,$t) ""
  125. bind $t <FocusIn> [list tk::FocusGroup_In $t %W %d]
  126. bind $t <FocusOut> [list tk::FocusGroup_Out $t %W %d]
  127. bind $t <Destroy> [list tk::FocusGroup_Destroy $t %W]
  128. }
  129. }
  130. # ::tk::FocusGroup_BindIn --
  131. #
  132. # Add a widget into the "FocusIn" list of the focus group. The $cmd will be
  133. # called when the widget is focused on by the user.
  134. #
  135. proc ::tk::FocusGroup_BindIn {t w cmd} {
  136. variable FocusIn
  137. variable ::tk::Priv
  138. if {![info exists Priv(fg,$t)]} {
  139. return -code error -errorcode [list TK LOOKUP FOCUS_GROUP $t] \
  140. "focus group \"$t\" doesn't exist"
  141. }
  142. set FocusIn($t,$w) $cmd
  143. }
  144. # ::tk::FocusGroup_BindOut --
  145. #
  146. # Add a widget into the "FocusOut" list of the focus group. The
  147. # $cmd will be called when the widget loses the focus (User
  148. # types Tab or click on another widget).
  149. #
  150. proc ::tk::FocusGroup_BindOut {t w cmd} {
  151. variable FocusOut
  152. variable ::tk::Priv
  153. if {![info exists Priv(fg,$t)]} {
  154. return -code error -errorcode [list TK LOOKUP FOCUS_GROUP $t] \
  155. "focus group \"$t\" doesn't exist"
  156. }
  157. set FocusOut($t,$w) $cmd
  158. }
  159. # ::tk::FocusGroup_Destroy --
  160. #
  161. # Cleans up when members of the focus group is deleted, or when the
  162. # toplevel itself gets deleted.
  163. #
  164. proc ::tk::FocusGroup_Destroy {t w} {
  165. variable FocusIn
  166. variable FocusOut
  167. variable ::tk::Priv
  168. if {$t eq $w} {
  169. unset Priv(fg,$t)
  170. unset Priv(focus,$t)
  171. foreach name [array names FocusIn $t,*] {
  172. unset FocusIn($name)
  173. }
  174. foreach name [array names FocusOut $t,*] {
  175. unset FocusOut($name)
  176. }
  177. } else {
  178. if {[info exists Priv(focus,$t)] && ($Priv(focus,$t) eq $w)} {
  179. set Priv(focus,$t) ""
  180. }
  181. unset -nocomplain FocusIn($t,$w) FocusOut($t,$w)
  182. }
  183. }
  184. # ::tk::FocusGroup_In --
  185. #
  186. # Handles the <FocusIn> event. Calls the FocusIn command for the newly
  187. # focused widget in the focus group.
  188. #
  189. proc ::tk::FocusGroup_In {t w detail} {
  190. variable FocusIn
  191. variable ::tk::Priv
  192. if {$detail ne "NotifyNonlinear" && $detail ne "NotifyNonlinearVirtual"} {
  193. # This is caused by mouse moving out&in of the window *or*
  194. # ordinary keypresses some window managers (ie: CDE [Bug: 2960]).
  195. return
  196. }
  197. if {![info exists FocusIn($t,$w)]} {
  198. set FocusIn($t,$w) ""
  199. return
  200. }
  201. if {![info exists Priv(focus,$t)]} {
  202. return
  203. }
  204. if {$Priv(focus,$t) eq $w} {
  205. # This is already in focus
  206. #
  207. return
  208. } else {
  209. set Priv(focus,$t) $w
  210. eval $FocusIn($t,$w)
  211. }
  212. }
  213. # ::tk::FocusGroup_Out --
  214. #
  215. # Handles the <FocusOut> event. Checks if this is really a lose
  216. # focus event, not one generated by the mouse moving out of the
  217. # toplevel window. Calls the FocusOut command for the widget
  218. # who loses its focus.
  219. #
  220. proc ::tk::FocusGroup_Out {t w detail} {
  221. variable FocusOut
  222. variable ::tk::Priv
  223. if {$detail ne "NotifyNonlinear" && $detail ne "NotifyNonlinearVirtual"} {
  224. # This is caused by mouse moving out of the window
  225. return
  226. }
  227. if {![info exists Priv(focus,$t)]} {
  228. return
  229. }
  230. if {![info exists FocusOut($t,$w)]} {
  231. return
  232. } else {
  233. eval $FocusOut($t,$w)
  234. set Priv(focus,$t) ""
  235. }
  236. }
  237. # ::tk::FDGetFileTypes --
  238. #
  239. # Process the string given by the -filetypes option of the file
  240. # dialogs. Similar to the C function TkGetFileFilters() on the Mac
  241. # and Windows platform.
  242. #
  243. proc ::tk::FDGetFileTypes {string} {
  244. foreach t $string {
  245. if {[llength $t] < 2 || [llength $t] > 3} {
  246. return -code error -errorcode {TK VALUE FILE_TYPE} \
  247. "bad file type \"$t\", should be \"typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?\""
  248. }
  249. lappend fileTypes([lindex $t 0]) {*}[lindex $t 1]
  250. }
  251. set types {}
  252. foreach t $string {
  253. set label [lindex $t 0]
  254. set exts {}
  255. if {[info exists hasDoneType($label)]} {
  256. continue
  257. }
  258. # Validate each macType. This is to agree with the
  259. # behaviour of TkGetFileFilters(). This list may be
  260. # empty.
  261. foreach macType [lindex $t 2] {
  262. if {[string length $macType] != 4} {
  263. return -code error -errorcode {TK VALUE MAC_TYPE} \
  264. "bad Macintosh file type \"$macType\""
  265. }
  266. }
  267. set name "$label \("
  268. set sep ""
  269. set doAppend 1
  270. foreach ext $fileTypes($label) {
  271. if {$ext eq ""} {
  272. continue
  273. }
  274. regsub {^[.]} $ext "*." ext
  275. if {![info exists hasGotExt($label,$ext)]} {
  276. if {$doAppend} {
  277. if {[string length $sep] && [string length $name]>40} {
  278. set doAppend 0
  279. append name $sep...
  280. } else {
  281. append name $sep$ext
  282. }
  283. }
  284. lappend exts $ext
  285. set hasGotExt($label,$ext) 1
  286. }
  287. set sep ","
  288. }
  289. append name "\)"
  290. lappend types [list $name $exts]
  291. set hasDoneType($label) 1
  292. }
  293. return $types
  294. }