Debian Codegraph @

pTk/mTk/tixGeneric/tixOption.c

raw symbols SCIP
1
2 /* $Id: tixOption.c,v 1.1.1.1 2000/05/17 11:08:42 idiscovery Exp $ */
3
4 /*
5 * tixOption.c --
6 *
7 * Handle the "$widget config" commands
8 *
9 * Copyright (c) 1996, Expert Interface Technologies
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 */
15
16 /* ToDo:
17 *
18 * 1) ConfigSpec structures shouldn't be shared between parent and child
19 * classes.
20 * 2) Specs array should be compacted (no duplication) and sorted according
21 * to argvName during class declaration.
22 * 3) Merge aliases with specs
23 *
24 */
25 #include <tk.h>
26 #include "tixPort.h"
27 #include "tixInt.h"
28
29 static char * FormatConfigInfo _ANSI_ARGS_((
30 Tcl_Interp *interp, TixClassRecord *cPtr,
31 char * widRec, TixConfigSpec * spec));
32 /*
33 * This is a lightweight interface for querying the value of a
34 * variable in the object. This is simpler compared to
35 * [lindex [$w config -flag] 4]
36 */
37 int Tix_GetVar (interp, cPtr, widRec, flag)
38 Tcl_Interp *interp;
39 TixClassRecord *cPtr;
40 char * widRec;
41 char * flag;
42 {
43 TixConfigSpec * spec;
44 char * value;
45
46 spec = Tix_FindConfigSpecByName(interp, cPtr, flag);
47 if (spec != NULL) {
48 if (spec->isAlias) {
49 flag = spec->realPtr->argvName;
50 } else {
51 /* The user may have specified a shorthand like -backgro */
52 flag = spec->argvName;
53 }
54 value = Tcl_GetVar2(interp, widRec, flag, TCL_GLOBAL_ONLY);
55
56 Tcl_AppendResult(interp, value, (char*)NULL);
57 return TCL_OK;
58 }
59 else {
60 return TCL_ERROR;
61 }
62 }
63
64 int
65 Tix_QueryOneOption(interp, cPtr, widRec, flag)
66 Tcl_Interp *interp;
67 TixClassRecord *cPtr;
68 char *widRec;
69 char *flag;
70 {
71 TixConfigSpec * spec;
72 char * list;
73
74 spec = Tix_FindConfigSpecByName(interp, cPtr, flag);
75 if (spec != NULL) {
76 list = FormatConfigInfo(interp, cPtr, widRec, spec);
77 Tcl_SetResult(interp, list, TCL_VOLATILE);
78
79 ckfree(list);
80 return TCL_OK;
81 }
82 else {
83 return TCL_ERROR;
84 }
85 }
86
87 /*
88 * Tix_QueryAllOptions --
89 *
90 * Note: This function does not call Tix_FindConfigSpec() because
91 * it just needs to print out the list of all configSpecs from the
92 * class structure.
93 *
94 */
95 int
96 Tix_QueryAllOptions (interp, cPtr, widRec)
97 Tcl_Interp *interp;
98 TixClassRecord * cPtr;
99 char *widRec;
100 {
101 int i, code = TCL_OK;
102 char * list;
103 char * lead = "{";
104
105 /* Iterate over all the options of class */
106 for (i=0; i<cPtr->nSpecs; i++) {
107 if (cPtr->specs[i] && cPtr->specs[i]->argvName) {
108 list = FormatConfigInfo(interp, cPtr, widRec, cPtr->specs[i]);
109 Tcl_AppendResult(interp, lead, list, "}", (char *) NULL);
110
111 ckfree(list);
112 lead = " {";
113 }
114 }
115
116 return code;
117 }
118
119 TixConfigSpec *
120 Tix_FindConfigSpecByName(interp, cPtr, flag)
121 Tcl_Interp * interp;
122 TixClassRecord * cPtr;
123 char * flag;
124 {
125 char * classRec;
126 char * key;
127 int nMatch, i;
128 size_t len;
129 Tcl_HashEntry *hashPtr;
130 TixConfigSpec *configSpec;
131
132 classRec = cPtr->className;
133
134 /*
135 * First try to look up the confifspec in a hash table,
136 * it should be faster.
137 */
138
139 key = Tix_GetConfigSpecFullName(classRec, flag);
140 hashPtr = Tcl_FindHashEntry(_TixGetHashTable(interp, "tixSpecTab", NULL),
141 key);
142 ckfree(key);
143
144 if (hashPtr) {
145 return (TixConfigSpec *) Tcl_GetHashValue(hashPtr);
146 }
147
148 /*
149 * The user may specified a partial name. Try to match, but will
150 * return error if we don't get exactly one match.
151 */
152 len = strlen(flag);
153 for (configSpec=NULL,nMatch=0,i=0; i<cPtr->nSpecs; i++) {
154 if (strncmp(flag, cPtr->specs[i]->argvName, len) == 0) {
155 if (nMatch > 0) {
156 Tcl_ResetResult(interp);
157 Tcl_AppendResult(interp, "ambiguous option \"", flag, "\"",
158 (char*)NULL);
159 return NULL;
160 } else {
161 nMatch ++;
162 configSpec = cPtr->specs[i];
163 }
164 }
165 }
166
167 if (configSpec == NULL) {
168 Tcl_ResetResult(interp);
169 Tcl_AppendResult(interp, "unknown option \"", flag, "\"", (char*)NULL);
170 return NULL;
171 } else {
172 return configSpec;
173 }
174 }
175
176 char * Tix_GetConfigSpecFullName(classRec, flag)
177 char * classRec;
178 char * flag;
179 {
180 char * buff;
181 int max;
182 int conLen;
183
184 conLen = strlen(classRec);
185 max = conLen + strlen(flag) + 1;
186 buff = (char*)ckalloc(max * sizeof(char));
187
188 strcpy(buff, classRec);
189 strcpy(buff+conLen, flag);
190
191 return buff;
192 }
193
194 int Tix_ChangeOptions(interp, cPtr, widRec, argc, argv)
195 Tcl_Interp *interp;
196 TixClassRecord *cPtr;
197 char * widRec;
198 int argc;
199 char ** argv;
200 {
201 int i, code = TCL_OK;
202 TixConfigSpec * spec;
203
204 if (argc == 0) {
205 goto done;
206 }
207
208 if ((argc %2) != 0) {
209 if (Tix_FindConfigSpecByName(interp, cPtr, argv[argc-1])) {
210 Tcl_AppendResult(interp, "value for \"", argv[argc-1],
211 "\" missing", (char*)NULL);
212 } else {
213 /* The error message is already appended by
214 * Tix_FindConfigSpecByName()
215 */
216 }
217 code = TCL_ERROR;
218 goto done;
219 }
220
221 for (i=0; i<argc; i+=2) {
222 spec = Tix_FindConfigSpecByName(interp, cPtr, argv[i]);
223
224 if (spec == NULL) {
225 code = TCL_ERROR;
226 goto done;
227 }
228 if (Tix_ChangeOneOption(interp, cPtr, widRec, spec,
229 argv[i+1], 0, 0)!=TCL_OK) {
230 code = TCL_ERROR;
231 goto done;
232 }
233 }
234
235 done:
236 return code;
237 }
238
239 int Tix_CallConfigMethod(interp, cPtr, widRec, spec, value)
240 Tcl_Interp *interp;
241 TixClassRecord *cPtr;
242 char * widRec;
243 TixConfigSpec *spec;
244 char * value;
245 {
246 char * argv[2];
247 char method[200];
248 char * context = Tix_GetContext(interp, widRec);
249 char * c;
250
251 sprintf(method, "config%s", spec->argvName);
252
253 c = Tix_FindMethod(interp, context, method);
254 if (c != NULL) {
255 argv[0] = value;
256 return Tix_CallMethod(interp, c, widRec, method, 1, argv);
257 }
258
259 c = Tix_FindMethod(interp, context, "config");
260 if (c != NULL) {
261 argv[0] = spec->argvName;
262 argv[1] = value;
263 return Tix_CallMethod(interp, c, widRec, "config", 2, argv);
264 }
265
266 return TCL_OK;
267 }
268
269 int Tix_ChangeOneOption(interp, cPtr, widRec, spec, value, isDefault, isInit)
270 Tcl_Interp *interp;
271 TixClassRecord *cPtr;
272 char * widRec;
273 TixConfigSpec *spec;
274 char * value;
275 int isDefault; /* Set to be true when Tix tries to set
276 * the options according to their default
277 * values */
278 int isInit; /* Set to true only if the option was
279 * specified at the widget creation command
280 */
281 {
282 int code = TCL_OK;
283 char * newValue = NULL;
284
285 if (spec->isAlias) {
286 spec = spec->realPtr;
287 }
288
289 /* -- STEP 1 --
290 * Check if these variables are protected.
291 * readonly means the variable can never be assigned to.
292 * static means ths variable can only ne assigned during initialization.
293 */
294
295 if (!isDefault && spec->readOnly) {
296 Tcl_AppendResult(interp, "cannot assigned to readonly variable \"",
297 spec->argvName, "\"", (char*) NULL);
298 code = TCL_ERROR;
299 goto done;
300 }
301 if (!(isInit || isDefault) && spec->isStatic) {
302 Tcl_AppendResult(interp, "cannot assigned to static variable \"",
303 spec->argvName, "\"", (char*) NULL);
304 code = TCL_ERROR;
305 goto done;
306 }
307
308 /* -- STEP 2 --
309 * Call the verify command to check whether the value is acceptable
310 *
311 */
312 if (spec->verifyCmd != NULL) {
313 char * cmdArgv[2];
314 cmdArgv[0] = spec->verifyCmd;
315 cmdArgv[1] = value;
316
317 if (Tix_EvalArgv(interp, 2, cmdArgv) != TCL_OK) {
318 code = TCL_ERROR;
319 goto done;
320 } else {
321 newValue = value = tixStrDup(interp->result);
322 }
323 }
324
325 /* -- STEP 3 --
326 * Call the configuration method of the widget. This method may
327 * override the user-supplied value. The configuration method should
328 * not be called during initialization.
329 */
330 if (isInit || isDefault) {
331 /* No need to call the configuration method */
332 Tcl_SetVar2(interp, widRec, spec->argvName, value,TCL_GLOBAL_ONLY);
333 }
334 else {
335 if (Tix_CallConfigMethod(interp, cPtr, widRec, spec, value)!=TCL_OK) {
336 code = TCL_ERROR;
337 goto done;
338 }
339 /* -- STEP 4 --
340 * If the configuration method does not override the value, set
341 * it in the widget record.
342 *
343 * If the configuration method has override the value, it will
344 * return a non-empty string. In this case, it is the configuration
345 * method's responsibility to set the value in the widget record.
346 */
347 if (interp->result && (*interp->result)) {
348 /* value was overrided. Don't do anything */
349 Tcl_ResetResult(interp);
350 } else {
351 Tcl_SetVar2(interp, widRec, spec->argvName, value,TCL_GLOBAL_ONLY);
352 }
353 }
354
355 done:
356 if (newValue) {
357 ckfree(newValue);
358 }
359 return code;
360 }
361
362 static char * FormatConfigInfo(interp, cPtr, widRec, sPtr)
363 Tcl_Interp *interp;
364 TixClassRecord *cPtr;
365 char * widRec;
366 TixConfigSpec * sPtr;
367 {
368 char *argv[6];
369
370 if (sPtr->isAlias) {
371 if (cPtr->isWidget) {
372 argv[0] = sPtr->argvName;
373 argv[1] = sPtr->realPtr->dbName;
374 } else {
375 argv[0] = sPtr->argvName;
376 argv[1] = sPtr->realPtr->argvName;
377 }
378 return Tcl_Merge(2, argv);
379 } else {
380 argv[0] = sPtr->argvName;
381 argv[1] = sPtr->dbName;
382 argv[2] = sPtr->dbClass;
383 argv[3] = sPtr->defValue;
384 argv[4] = Tcl_GetVar2(interp, widRec, argv[0], TCL_GLOBAL_ONLY);
385
386 return Tcl_Merge(5, argv);
387 }
388 }