3 # Some functions needed for the common dialog boxes. Probably need to go
6 # Copyright (c) 1996 Sun Microsystems, Inc.
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 # tclParseConfigSpec --
14 # Parses a list of "-option value" pairs. If all options and
15 # values are legal, the values are stored in
16 # $data($option). Otherwise an error message is returned. When
17 # an error happens, the data() array may have been partially
18 # modified, but all the modified members of the data(0 array are
19 # guaranteed to have valid values. This is different than
20 # Tk_ConfigureWidget() which does not modify the value of a
21 # widget record if any error occurs.
25 # w = widget record to modify. Must be the pathname of a widget.
28 # {-commandlineswitch resourceName ResourceClass defaultValue verifier}
32 # flags = a list of flags. Currently supported flags are:
33 # DONTSETDEFAULTS = skip default values setting
35 # argList = The list of "-option value" pairs.
37 proc tclParseConfigSpec {w specs flags argList} {
40 # 1: Put the specs in associative arrays for faster access
43 if {[llength $spec] < 4} {
44 return -code error -errorcode {TK VALUE CONFIG_SPEC} \
45 "\"spec\" should contain 5 or 4 elements"
47 set cmdsw [lindex $spec 0]
49 set rname($cmdsw) [lindex $spec 1]
50 set rclass($cmdsw) [lindex $spec 2]
51 set def($cmdsw) [lindex $spec 3]
52 set verproc($cmdsw) [lindex $spec 4]
55 if {[llength $argList] & 1} {
56 set cmdsw [lindex $argList end]
57 if {![info exists cmd($cmdsw)]} {
58 return -code error -errorcode [list TK LOOKUP OPTION $cmdsw] \
59 "bad option \"$cmdsw\": must be [tclListValidFlags cmd]"
61 return -code error -errorcode {TK VALUE_MISSING} \
62 "value for \"$cmdsw\" missing"
65 # 2: set the default values
67 if {"DONTSETDEFAULTS" ni $flags} {
68 foreach cmdsw [array names cmd] {
69 set data($cmdsw) $def($cmdsw)
73 # 3: parse the argument list
75 foreach {cmdsw value} $argList {
76 if {![info exists cmd($cmdsw)]} {
77 return -code error -errorcode [list TK LOOKUP OPTION $cmdsw] \
78 "bad option \"$cmdsw\": must be [tclListValidFlags cmd]"
80 set data($cmdsw) $value
86 proc tclListValidFlags {v} {
89 set len [llength [array names cmd]]
93 foreach cmdsw [lsort [array names cmd]] {
94 append errormsg "$separator$cmdsw"
105 #----------------------------------------------------------------------
109 # Focus groups are used to handle the user's focusing actions inside a
112 # One example of using focus groups is: when the user focuses on an
113 # entry, the text in the entry is highlighted and the cursor is put to
114 # the end of the text. When the user changes focus to another widget,
115 # the text in the previously focused entry is validated.
117 #----------------------------------------------------------------------
120 # ::tk::FocusGroup_Create --
122 # Create a focus group. All the widgets in a focus group must be
123 # within the same focus toplevel. Each toplevel can have only
124 # one focus group, which is identified by the name of the
127 proc ::tk::FocusGroup_Create {t} {
129 if {[winfo toplevel $t] ne $t} {
130 return -code error -errorcode [list TK LOOKUP TOPLEVEL $t] \
131 "$t is not a toplevel window"
133 if {![info exists Priv(fg,$t)]} {
135 set Priv(focus,$t) ""
136 bind $t <FocusIn> [list tk::FocusGroup_In $t %W %d]
137 bind $t <FocusOut> [list tk::FocusGroup_Out $t %W %d]
138 bind $t <Destroy> [list tk::FocusGroup_Destroy $t %W]
142 # ::tk::FocusGroup_BindIn --
144 # Add a widget into the "FocusIn" list of the focus group. The $cmd will be
145 # called when the widget is focused on by the user.
147 proc ::tk::FocusGroup_BindIn {t w cmd} {
150 if {![info exists Priv(fg,$t)]} {
151 return -code error -errorcode [list TK LOOKUP FOCUS_GROUP $t] \
152 "focus group \"$t\" doesn't exist"
154 set FocusIn($t,$w) $cmd
158 # ::tk::FocusGroup_BindOut --
160 # Add a widget into the "FocusOut" list of the focus group. The
161 # $cmd will be called when the widget loses the focus (User
162 # types Tab or click on another widget).
164 proc ::tk::FocusGroup_BindOut {t w cmd} {
167 if {![info exists Priv(fg,$t)]} {
168 return -code error -errorcode [list TK LOOKUP FOCUS_GROUP $t] \
169 "focus group \"$t\" doesn't exist"
171 set FocusOut($t,$w) $cmd
174 # ::tk::FocusGroup_Destroy --
176 # Cleans up when members of the focus group is deleted, or when the
177 # toplevel itself gets deleted.
179 proc ::tk::FocusGroup_Destroy {t w} {
188 foreach name [array names FocusIn $t,*] {
191 foreach name [array names FocusOut $t,*] {
192 unset FocusOut($name)
195 if {[info exists Priv(focus,$t)] && ($Priv(focus,$t) eq $w)} {
196 set Priv(focus,$t) ""
198 unset -nocomplain FocusIn($t,$w) FocusOut($t,$w)
202 # ::tk::FocusGroup_In --
204 # Handles the <FocusIn> event. Calls the FocusIn command for the newly
205 # focused widget in the focus group.
207 proc ::tk::FocusGroup_In {t w detail} {
211 if {$detail ne "NotifyNonlinear" && $detail ne "NotifyNonlinearVirtual"} {
212 # This is caused by mouse moving out&in of the window *or*
213 # ordinary keypresses some window managers (ie: CDE [Bug: 2960]).
216 if {![info exists FocusIn($t,$w)]} {
217 set FocusIn($t,$w) ""
220 if {![info exists Priv(focus,$t)]} {
223 if {$Priv(focus,$t) eq $w} {
224 # This is already in focus
228 set Priv(focus,$t) $w
233 # ::tk::FocusGroup_Out --
235 # Handles the <FocusOut> event. Checks if this is really a lose
236 # focus event, not one generated by the mouse moving out of the
237 # toplevel window. Calls the FocusOut command for the widget
238 # who loses its focus.
240 proc ::tk::FocusGroup_Out {t w detail} {
244 if {$detail ne "NotifyNonlinear" && $detail ne "NotifyNonlinearVirtual"} {
245 # This is caused by mouse moving out of the window
248 if {![info exists Priv(focus,$t)]} {
251 if {![info exists FocusOut($t,$w)]} {
254 eval $FocusOut($t,$w)
255 set Priv(focus,$t) ""
259 # ::tk::FDGetFileTypes --
261 # Process the string given by the -filetypes option of the file
262 # dialogs. Similar to the C function TkGetFileFilters() on the Mac
263 # and Windows platform.
265 proc ::tk::FDGetFileTypes {string} {
267 if {[llength $t] < 2 || [llength $t] > 3} {
268 return -code error -errorcode {TK VALUE FILE_TYPE} \
269 "bad file type \"$t\", should be \"typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?\""
271 lappend fileTypes([lindex $t 0]) {*}[lindex $t 1]
276 set label [lindex $t 0]
279 if {[info exists hasDoneType($label)]} {
283 # Validate each macType. This is to agree with the
284 # behaviour of TkGetFileFilters(). This list may be
286 foreach macType [lindex $t 2] {
287 if {[string length $macType] != 4} {
288 return -code error -errorcode {TK VALUE MAC_TYPE} \
289 "bad Macintosh file type \"$macType\""
296 foreach ext $fileTypes($label) {
300 regsub {^[.]} $ext "*." ext
301 if {![info exists hasGotExt($label,$ext)]} {
303 if {[string length $sep] && [string length $name]>40} {
311 set hasGotExt($label,$ext) 1
316 lappend types [list $name $exts]
318 set hasDoneType($label) 1