Debian Codegraph @

pTk/mTk/generic/tkConfig.c

raw symbols SCIP
1 /*
2 * tkConfig.c --
3 *
4 * This file contains procedures that manage configuration options
5 * for widgets and other things.
6 *
7 * Copyright (c) 1997-1998 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkConfig.c,v 1.18 2002/08/05 04:30:38 dgp Exp $
13 */
14
15 /*
16 * Temporary flag for working on new config package.
17 */
18
19 #if 0
20
21 /*
22 * used only for removing the old config code
23 */
24
25 #define __NO_OLD_CONFIG
26 #endif
27
28 #include "tk.h"
29 #include "tkInt.h"
30 #include "tkPort.h"
31 #include "tkFont.h"
32
33 #include "tkOption.h"
34 #include "tkOption.m"
35
36 /*
37 * The following definition is an AssocData key used to keep track of
38 * all of the option tables that have been created for an interpreter.
39 */
40
41 #define OPTION_HASH_KEY "TkOptionTable"
42
43 /*
44 * The following two structures are used along with Tk_OptionSpec
45 * structures to manage configuration options. Tk_OptionSpec is
46 * static templates that are compiled into the code of a widget
47 * or other object manager. However, to look up options efficiently
48 * we need to supplement the static information with additional
49 * dynamic information, and this dynamic information may be different
50 * for each application. Thus we create structures of the following
51 * two types to hold all of the dynamic information; this is done
52 * by Tk_CreateOptionTable.
53 *
54 * One of the following structures corresponds to each Tk_OptionSpec.
55 * These structures exist as arrays inside TkOptionTable structures.
56 */
57
58 typedef struct TkOption {
59 CONST Tk_OptionSpec *specPtr; /* The original spec from the template
60 * passed to Tk_CreateOptionTable.*/
61 Tk_Uid dbNameUID; /* The Uid form of the option database
62 * name. */
63 Tk_Uid dbClassUID; /* The Uid form of the option database
64 * class name. */
65 Tcl_Obj *defaultPtr; /* Default value for this option. */
66 union {
67 Tcl_Obj *monoColorPtr; /* For color and border options, this
68 * is an alternate default value to
69 * use on monochrome displays. */
70 struct TkOption *synonymPtr; /* For synonym options, this points to
71 * the master entry. */
72 struct Tk_ObjCustomOption *custom; /* For TK_OPTION_CUSTOM. */
73 } extra;
74 int flags; /* Miscellaneous flag values; see
75 * below for definitions. */
76 } Option;
77
78 /*
79 * Flag bits defined for Option structures:
80 *
81 * OPTION_NEEDS_FREEING - 1 means that FreeResources must be
82 * invoke to free resources associated with
83 * the option when it is no longer needed.
84 */
85
86 #define OPTION_NEEDS_FREEING 1
87
88 /*
89 * One of the following exists for each Tk_OptionSpec array that has
90 * been passed to Tk_CreateOptionTable.
91 */
92
93 typedef struct OptionTable {
94 int refCount; /* Counts the number of uses of this
95 * table (the number of times
96 * Tk_CreateOptionTable has returned
97 * it). This can be greater than 1 if
98 * it is shared along several option
99 * table chains, or if the same table
100 * is used for multiple purposes. */
101 Tcl_HashEntry *hashEntryPtr; /* Hash table entry that refers to this
102 * table; used to delete the entry. */
103 struct OptionTable *nextPtr; /* If templatePtr was part of a chain
104 * of templates, this points to the
105 * table corresponding to the next
106 * template in the chain. */
107 int numOptions; /* The number of items in the options
108 * array below. */
109 Option options[1]; /* Information about the individual
110 * options in the table. This must be
111 * the last field in the structure:
112 * the actual size of the array will
113 * be numOptions, not 1. */
114 } OptionTable;
115
116 /*
117 * Forward declarations for procedures defined later in this file:
118 */
119
120 static int DoObjConfig _ANSI_ARGS_((Tcl_Interp *interp,
121 char *recordPtr, Option *optionPtr,
122 Tcl_Obj *valuePtr, Tk_Window tkwin,
123 Tk_SavedOption *savePtr));
124 static void DestroyOptionHashTable _ANSI_ARGS_((
125 ClientData clientData, Tcl_Interp *interp));
126 static void FreeResources _ANSI_ARGS_((Option *optionPtr,
127 Tcl_Obj *objPtr, char *internalPtr,
128 Tk_Window tkwin));
129 static Tcl_Obj * GetConfigList _ANSI_ARGS_((Tcl_Interp *interp, char *recordPtr,
130 Option *optionPtr, Tk_Window tkwin));
131 static Tcl_Obj * GetObjectForOption _ANSI_ARGS_((Tcl_Interp *interp, char *recordPtr,
132 Option *optionPtr, Tk_Window tkwin));
133 static Option * GetOption _ANSI_ARGS_((CONST char *name,
134 OptionTable *tablePtr));
135 static Option * GetOptionFromObj _ANSI_ARGS_((Tcl_Interp *interp,
136 Tcl_Obj *objPtr, OptionTable *tablePtr));
137 static int ObjectIsEmpty _ANSI_ARGS_((Tcl_Obj *objPtr));
138 static int SetOptionFromAny _ANSI_ARGS_((Tcl_Interp *interp,
139 Tcl_Obj *objPtr));
140
141 /*
142 * The structure below defines an object type that is used to cache the
143 * result of looking up an option name. If an object has this type, then
144 * its internalPtr1 field points to the OptionTable in which it was looked up,
145 * and the internalPtr2 field points to the entry that matched.
146 */
147
148 Tcl_ObjType tkOptionObjType = {
149 "option", /* name */
150 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
151 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
152 (Tcl_UpdateStringProc *) NULL, /* updateStringProc */
153 SetOptionFromAny /* setFromAnyProc */
154 };
155
156 /*
157 *--------------------------------------------------------------
158 *
159 * Tk_CreateOptionTable --
160 *
161 * Given a template for configuration options, this procedure
162 * creates a table that may be used to look up options efficiently.
163 *
164 * Results:
165 * Returns a token to a structure that can be passed to procedures
166 * such as Tk_InitOptions, Tk_SetOptions, and Tk_FreeConfigOptions.
167 *
168 * Side effects:
169 * Storage is allocated.
170 *
171 *--------------------------------------------------------------
172 */
173
174 Tk_OptionTable
175 Tk_CreateOptionTable(interp, templatePtr)
176 Tcl_Interp *interp; /* Interpreter associated with the
177 * application in which this table
178 * will be used. */
179 CONST Tk_OptionSpec *templatePtr; /* Static information about the
180 * configuration options. */
181 {
182 Tcl_HashTable *hashTablePtr;
183 Tcl_HashEntry *hashEntryPtr;
184 int newEntry;
185 OptionTable *tablePtr;
186 CONST Tk_OptionSpec *specPtr, *specPtr2;
187 Option *optionPtr;
188 int numOptions, i;
189
190 /*
191 * We use an AssocData value in the interpreter to keep a hash
192 * table of all the option tables we've created for this application.
193 * This is used for two purposes. First, it allows us to share the
194 * tables (e.g. in several chains) and second, we use the deletion
195 * callback for the AssocData to delete all the option tables when
196 * the interpreter is deleted. The code below finds the hash table
197 * or creates a new one if it doesn't already exist.
198 */
199
200 hashTablePtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, OPTION_HASH_KEY,
201 NULL);
202 if (hashTablePtr == NULL) {
203 hashTablePtr = (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
204 Tcl_InitHashTable(hashTablePtr, TCL_ONE_WORD_KEYS);
205 Tcl_SetAssocData(interp, OPTION_HASH_KEY, DestroyOptionHashTable,
206 (ClientData) hashTablePtr);
207 }
208
209 /*
210 * See if a table has already been created for this template. If
211 * so, just reuse the existing table.
212 */
213
214 hashEntryPtr = Tcl_CreateHashEntry(hashTablePtr, (char *) templatePtr,
215 &newEntry);
216 if (!newEntry) {
217 tablePtr = (OptionTable *) Tcl_GetHashValue(hashEntryPtr);
218 tablePtr->refCount++;
219 return (Tk_OptionTable) tablePtr;
220 }
221
222 /*
223 * Count the number of options in the template, then create the
224 * table structure.
225 */
226
227 numOptions = 0;
228 for (specPtr = templatePtr; specPtr->type != TK_OPTION_END; specPtr++) {
229 numOptions++;
230 }
231 tablePtr = (OptionTable *) (ckalloc(sizeof(OptionTable)
232 + ((numOptions - 1) * sizeof(Option))));
233 tablePtr->refCount = 1;
234 tablePtr->hashEntryPtr = hashEntryPtr;
235 tablePtr->nextPtr = NULL;
236 tablePtr->numOptions = numOptions;
237
238 /*
239 * Initialize all of the Option structures in the table.
240 */
241
242 for (specPtr = templatePtr, optionPtr = tablePtr->options;
243 specPtr->type != TK_OPTION_END; specPtr++, optionPtr++) {
244 optionPtr->specPtr = specPtr;
245 optionPtr->dbNameUID = NULL;
246 optionPtr->dbClassUID = NULL;
247 optionPtr->defaultPtr = NULL;
248 optionPtr->extra.monoColorPtr = NULL;
249 optionPtr->flags = 0;
250
251 if (specPtr->type == TK_OPTION_SYNONYM) {
252 /*
253 * This is a synonym option; find the master option that it
254 * refers to and create a pointer from the synonym to the
255 * master.
256 */
257
258 for (specPtr2 = templatePtr, i = 0; ; specPtr2++, i++) {
259 if (specPtr2->type == TK_OPTION_END) {
260 panic("Tk_CreateOptionTable couldn't find synonym");
261 }
262 if (strcmp(specPtr2->optionName,
263 (char *) specPtr->clientData) == 0) {
264 optionPtr->extra.synonymPtr = tablePtr->options + i;
265 break;
266 }
267 }
268 } else {
269 if (specPtr->dbName != NULL) {
270 optionPtr->dbNameUID = Tk_GetUid(specPtr->dbName);
271 }
272 if (specPtr->dbClass != NULL) {
273 optionPtr->dbClassUID =
274 Tk_GetUid(specPtr->dbClass);
275 }
276 if (specPtr->defValue != NULL) {
277 optionPtr->defaultPtr =
278 Tcl_NewStringObj(specPtr->defValue, -1);
279 Tcl_IncrRefCount(optionPtr->defaultPtr);
280 }
281 if (((specPtr->type == TK_OPTION_COLOR)
282 || (specPtr->type == TK_OPTION_BORDER))
283 && (specPtr->clientData != NULL)) {
284 optionPtr->extra.monoColorPtr =
285 Tcl_NewStringObj((char *) specPtr->clientData, -1);
286 Tcl_IncrRefCount(optionPtr->extra.monoColorPtr);
287 }
288
289 if (specPtr->type == TK_OPTION_CUSTOM) {
290 /*
291 * Get the custom parsing, etc., functions.
292 */
293 optionPtr->extra.custom =
294 (Tk_ObjCustomOption *)specPtr->clientData;
295 }
296 }
297 if (((specPtr->type == TK_OPTION_STRING)
298 && (specPtr->internalOffset >= 0))
299 || (specPtr->type == TK_OPTION_COLOR)
300 || (specPtr->type == TK_OPTION_FONT)
301 || (specPtr->type == TK_OPTION_BITMAP)
302 || (specPtr->type == TK_OPTION_BORDER)
303 || (specPtr->type == TK_OPTION_CURSOR)
304 || (specPtr->type == TK_OPTION_OBJ)
305 || (specPtr->type == TK_OPTION_CALLBACK)
306 || (specPtr->type == TK_OPTION_SCALARVAR)
307 || (specPtr->type == TK_OPTION_ARRAYVAR)
308 || (specPtr->type == TK_OPTION_HASHVAR)
309 || (specPtr->type == TK_OPTION_CUSTOM)) {
310 optionPtr->flags |= OPTION_NEEDS_FREEING;
311 }
312 }
313 tablePtr->hashEntryPtr = hashEntryPtr;
314 Tcl_SetHashValue(hashEntryPtr, tablePtr);
315
316 /*
317 * Finally, check to see if this template chains to another template
318 * with additional options. If so, call ourselves recursively to
319 * create the next table(s).
320 */
321
322 if (specPtr->clientData != NULL) {
323 tablePtr->nextPtr = (OptionTable *) Tk_CreateOptionTable(interp,
324 (Tk_OptionSpec *) specPtr->clientData);
325 }
326
327 return (Tk_OptionTable) tablePtr;
328 }
329
330 /*
331 *----------------------------------------------------------------------
332 *
333 * Tk_DeleteOptionTable --
334 *
335 * Called to release resources used by an option table when
336 * the table is no longer needed.
337 *
338 * Results:
339 * None.
340 *
341 * Side effects:
342 * The option table and associated resources (such as additional
343 * option tables chained off it) are destroyed.
344 *
345 *----------------------------------------------------------------------
346 */
347
348 void
349 Tk_DeleteOptionTable(optionTable)
350 Tk_OptionTable optionTable; /* The option table to delete. */
351 {
352 OptionTable *tablePtr = (OptionTable *) optionTable;
353 Option *optionPtr;
354 int count;
355
356 tablePtr->refCount--;
357 if (tablePtr->refCount > 0) {
358 return;
359 }
360
361 if (tablePtr->nextPtr != NULL) {
362 Tk_DeleteOptionTable((Tk_OptionTable) tablePtr->nextPtr);
363 }
364
365 for (count = tablePtr->numOptions - 1, optionPtr = tablePtr->options;
366 count > 0; count--, optionPtr++) {
367 if (optionPtr->defaultPtr != NULL) {
368 Tcl_DecrRefCount(optionPtr->defaultPtr);
369 }
370 if (((optionPtr->specPtr->type == TK_OPTION_COLOR)
371 || (optionPtr->specPtr->type == TK_OPTION_BORDER))
372 && (optionPtr->extra.monoColorPtr != NULL)) {
373 Tcl_DecrRefCount(optionPtr->extra.monoColorPtr);
374 }
375 }
376 Tcl_DeleteHashEntry(tablePtr->hashEntryPtr);
377 ckfree((char *) tablePtr);
378 }
379
380 /*
381 *----------------------------------------------------------------------
382 *
383 * DestroyOptionHashTable --
384 *
385 * This procedure is the deletion callback associated with the
386 * AssocData entry created by Tk_CreateOptionTable. It is
387 * invoked when an interpreter is deleted, and deletes all of
388 * the option tables associated with that interpreter.
389 *
390 * Results:
391 * None.
392 *
393 * Side effects:
394 * The option hash table is destroyed along with all of the
395 * OptionTable structures that it refers to.
396 *
397 *----------------------------------------------------------------------
398 */
399
400 static void
401 DestroyOptionHashTable(clientData, interp)
402 ClientData clientData; /* The hash table we are destroying */
403 Tcl_Interp *interp; /* The interpreter we are destroying */
404 {
405 Tcl_HashTable *hashTablePtr = (Tcl_HashTable *) clientData;
406 Tcl_HashSearch search;
407 Tcl_HashEntry *hashEntryPtr;
408 OptionTable *tablePtr;
409
410 for (hashEntryPtr = Tcl_FirstHashEntry(hashTablePtr, &search);
411 hashEntryPtr != NULL;
412 hashEntryPtr = Tcl_NextHashEntry(&search)) {
413 tablePtr = (OptionTable *) Tcl_GetHashValue(hashEntryPtr);
414
415 /*
416 * The following statements do two tricky things:
417 * 1. They ensure that the option table is deleted, even if
418 * there are outstanding references to it.
419 * 2. They ensure that Tk_DeleteOptionTable doesn't delete
420 * other tables chained from this one; we'll do it when
421 * we come across the hash table entry for the chained
422 * table (in fact, the chained table may already have
423 * been deleted).
424 */
425
426 tablePtr->refCount = 1;
427 tablePtr->nextPtr = NULL;
428 Tk_DeleteOptionTable((Tk_OptionTable) tablePtr);
429 }
430 Tcl_DeleteHashTable(hashTablePtr);
431 ckfree((char *) hashTablePtr);
432 }
433
434 /*
435 *--------------------------------------------------------------
436 *
437 * Tk_InitOptions --
438 *
439 * This procedure is invoked when an object such as a widget
440 * is created. It supplies an initial value for each configuration
441 * option (the value may come from the option database, a system
442 * default, or the default in the option table).
443 *
444 * Results:
445 * The return value is TCL_OK if the procedure completed
446 * successfully, and TCL_ERROR if one of the initial values was
447 * bogus. If an error occurs and interp isn't NULL, then an
448 * error message will be left in its result.
449 *
450 * Side effects:
451 * Fields of recordPtr are filled in with initial values.
452 *
453 *--------------------------------------------------------------
454 */
455
456 int
457 Tk_InitOptions(interp, recordPtr, optionTable, tkwin)
458 Tcl_Interp *interp; /* Interpreter for error reporting. NULL
459 * means don't leave an error message. */
460 char *recordPtr; /* Pointer to the record to configure.
461 * Note: the caller should have properly
462 * initialized the record with NULL
463 * pointers for each option value. */
464 Tk_OptionTable optionTable; /* The token which matches the config
465 * specs for the widget in question. */
466 Tk_Window tkwin; /* Certain options types (such as
467 * TK_OPTION_COLOR) need fields out
468 * of the window they are used in to
469 * be able to calculate their values.
470 * Not needed unless one of these
471 * options is in the configSpecs record. */
472 {
473 OptionTable *tablePtr = (OptionTable *) optionTable;
474 Option *optionPtr;
475 int count;
476 Tk_Uid value;
477 Tcl_Obj *valuePtr;
478 enum {
479 OPTION_DATABASE, SYSTEM_DEFAULT, TABLE_DEFAULT
480 } source;
481
482 /*
483 * If this table chains to other tables, handle their initialization
484 * first. That way, if both tables refer to the same field of the
485 * record, the value in the first table will win.
486 */
487
488 if (tablePtr->nextPtr != NULL) {
489 if (Tk_InitOptions(interp, recordPtr,
490 (Tk_OptionTable) tablePtr->nextPtr, tkwin) != TCL_OK) {
491 return TCL_ERROR;
492 }
493 }
494
495 /*
496 * Iterate over all of the options in the table, initializing each in
497 * turn.
498 */
499
500 for (optionPtr = tablePtr->options, count = tablePtr->numOptions;
501 count > 0; optionPtr++, count--) {
502
503 /*
504 * If we specify TK_OPTION_DONT_SET_DEFAULT, then the user has
505 * processed and set a default for this already.
506 */
507 if ((optionPtr->specPtr->type == TK_OPTION_SYNONYM) ||
508 (optionPtr->specPtr->flags & TK_OPTION_DONT_SET_DEFAULT)) {
509 continue;
510 }
511 source = TABLE_DEFAULT;
512
513 /*
514 * We look in three places for the initial value, using the first
515 * non-NULL value that we find. First, check the option database.
516 */
517
518 valuePtr = NULL;
519 if (optionPtr->dbNameUID != NULL) {
520 value = Tk_GetOption(tkwin, optionPtr->dbNameUID,
521 optionPtr->dbClassUID);
522 if (value != NULL) {
523 valuePtr = Tcl_NewStringObj(value, -1);
524 source = OPTION_DATABASE;
525 }
526 }
527
528 /*
529 * Second, check for a system-specific default value.
530 */
531 if ((valuePtr == NULL)
532 && (optionPtr->dbNameUID != NULL)) {
533 valuePtr = TkpGetSystemDefault(tkwin, optionPtr->dbNameUID,
534 optionPtr->dbClassUID);
535 if (valuePtr != NULL) {
536 source = SYSTEM_DEFAULT;
537 }
538 }
539
540 /*
541 * Third and last, use the default value supplied by the option
542 * table. In the case of color objects, we pick one of two
543 * values depending on whether the screen is mono or color.
544 */
545
546 if (valuePtr == NULL) {
547 if ((tkwin != NULL)
548 && ((optionPtr->specPtr->type == TK_OPTION_COLOR)
549 || (optionPtr->specPtr->type == TK_OPTION_BORDER))
550 && (Tk_Depth(tkwin) <= 1)
551 && (optionPtr->extra.monoColorPtr != NULL)) {
552 valuePtr = optionPtr->extra.monoColorPtr;
553 } else {
554 valuePtr = optionPtr->defaultPtr;
555 }
556 }
557
558 if (valuePtr == NULL) {
559 continue;
560 }
561
562 /*
563 * Bump the reference count on valuePtr, so that it is strongly
564 * referenced here, and will be properly free'd when finished,
565 * regardless of what DoObjConfig does.
566 */
567 Tcl_IncrRefCount(valuePtr);
568
569 if (DoObjConfig(interp, recordPtr, optionPtr, valuePtr, tkwin,
570 (Tk_SavedOption *) NULL) != TCL_OK) {
571 if (interp != NULL) {
572 char msg[200];
573
574 switch (source) {
575 case OPTION_DATABASE:
576 sprintf(msg, "\n (database entry for \"%.50s\")",
577 optionPtr->specPtr->optionName);
578 break;
579 case SYSTEM_DEFAULT:
580 sprintf(msg, "\n (system default for \"%.50s\")",
581 optionPtr->specPtr->optionName);
582 break;
583 case TABLE_DEFAULT:
584 sprintf(msg, "\n (default value for \"%.50s\")",
585 optionPtr->specPtr->optionName);
586 }
587 if (tkwin != NULL) {
588 sprintf(msg + strlen(msg) - 1, " in widget \"%.50s\")",
589 Tk_PathName(tkwin));
590 }
591 Tcl_AddErrorInfo(interp, msg);
592 }
593 Tcl_DecrRefCount(valuePtr);
594 return TCL_ERROR;
595 }
596 Tcl_DecrRefCount(valuePtr);
597
598 if (source == OPTION_DATABASE) {
599 Tcl_DecrRefCount(valuePtr);
600 }
601
602 }
603 return TCL_OK;
604 }
605
606 /*
607 *--------------------------------------------------------------
608 *
609 * DoObjConfig --
610 *
611 * This procedure applies a new value for a configuration option
612 * to the record being configured.
613 *
614 * Results:
615 * The return value is TCL_OK if the procedure completed
616 * successfully. If an error occurred then TCL_ERROR is
617 * returned and an error message is left in interp's result, if
618 * interp isn't NULL. In addition, if oldValuePtrPtr isn't
619 * NULL then it *oldValuePtrPtr is filled in with a pointer
620 * to the option's old value.
621 *
622 * Side effects:
623 * RecordPtr gets modified to hold the new value in the form of
624 * a Tcl_Obj, an internal representation, or both. The old
625 * value is freed if oldValuePtrPtr is NULL.
626 *
627 *--------------------------------------------------------------
628 */
629
630 static int
631 DoObjConfig(interp, recordPtr, optionPtr, valuePtr, tkwin, savedOptionPtr)
632 Tcl_Interp *interp; /* Interpreter for error reporting. If
633 * NULL, then no message is left if an error
634 * occurs. */
635 char *recordPtr; /* The record to modify to hold the new
636 * option value. */
637 Option *optionPtr; /* Pointer to information about the
638 * option. */
639 Tcl_Obj *valuePtr; /* New value for option. */
640 Tk_Window tkwin; /* Window in which option will be used (needed
641 * to allocate resources for some options).
642 * May be NULL if the option doesn't
643 * require window-related resources. */
644 Tk_SavedOption *savedOptionPtr;
645 /* If NULL, the old value for the option will
646 * be freed. If non-NULL, the old value will
647 * be stored here, and it becomes the property
648 * of the caller (the caller must eventually
649 * free the old value). */
650 {
651 Tcl_Obj **slotPtrPtr, *oldPtr;
652 char *internalPtr; /* Points to location in record where
653 * internal representation of value should
654 * be stored, or NULL. */
655 char *oldInternalPtr; /* Points to location in which to save old
656 * internal representation of value. */
657 Tk_SavedOption internal; /* Used to save the old internal representation
658 * of the value if savedOptionPtr is NULL. */
659 CONST Tk_OptionSpec *specPtr;
660 int nullOK;
661
662 /*
663 * Save the old object form for the value, if there is one.
664 */
665
666 specPtr = optionPtr->specPtr;
667 if (specPtr->objOffset >= 0) {
668 slotPtrPtr = (Tcl_Obj **) (recordPtr + specPtr->objOffset);
669 oldPtr = *slotPtrPtr;
670 } else {
671 slotPtrPtr = NULL;
672 oldPtr = NULL;
673 }
674
675 /*
676 * Apply the new value in a type-specific way. Also remember the
677 * old object and internal forms, if they exist.
678 */
679
680 if (specPtr->internalOffset >= 0) {
681 internalPtr = recordPtr + specPtr->internalOffset;
682 } else {
683 internalPtr = NULL;
684 }
685 if (savedOptionPtr != NULL) {
686 savedOptionPtr->optionPtr = optionPtr;
687 savedOptionPtr->valuePtr = oldPtr;
688 oldInternalPtr = (char *) &savedOptionPtr->internalForm;
689 } else {
690 oldInternalPtr = (char *) &internal.internalForm;
691 }
692 nullOK = (optionPtr->specPtr->flags & TK_OPTION_NULL_OK);
693 switch (optionPtr->specPtr->type) {
694 case TK_OPTION_BOOLEAN: {
695 int new;
696
697 if (Tcl_GetBooleanFromObj(interp, valuePtr, &new)
698 != TCL_OK) {
699 return TCL_ERROR;
700 }
701 if (internalPtr != NULL) {
702 *((int *) oldInternalPtr) = *((int *) internalPtr);
703 *((int *) internalPtr) = new;
704 }
705 break;
706 }
707 case TK_OPTION_INT: {
708 int new;
709
710 if (Tcl_GetIntFromObj(interp, valuePtr, &new) != TCL_OK) {
711 return TCL_ERROR;
712 }
713 if (internalPtr != NULL) {
714 *((int *) oldInternalPtr) = *((int *) internalPtr);
715 *((int *) internalPtr) = new;
716 }
717 break;
718 }
719 case TK_OPTION_DOUBLE: {
720 double new;
721
722 if (nullOK && ObjectIsEmpty(valuePtr)) {
723 valuePtr = NULL;
724 new = 0;
725 } else {
726 if (Tcl_GetDoubleFromObj(interp, valuePtr, &new) != TCL_OK) {
727 return TCL_ERROR;
728 }
729 }
730
731 if (internalPtr != NULL) {
732 *((double *) oldInternalPtr) = *((double *) internalPtr);
733 *((double *) internalPtr) = new;
734 }
735 break;
736 }
737 case TK_OPTION_STRING: {
738 char *new, *value;
739 int length;
740
741 if (nullOK && ObjectIsEmpty(valuePtr)) {
742 valuePtr = NULL;
743 }
744 if (internalPtr != NULL) {
745 if (valuePtr != NULL) {
746 value = Tcl_GetStringFromObj(valuePtr, &length);
747 new = ckalloc((unsigned) (length + 1));
748 strcpy(new, value);
749 } else {
750 new = NULL;
751 }
752 *((char **) oldInternalPtr) = *((char **) internalPtr);
753 *((char **) internalPtr) = new;
754 } else {
755 /* Make sure it is string-able _now_ */
756 (void) Tcl_GetString(valuePtr);
757 }
758 break;
759 }
760 case TK_OPTION_STRING_TABLE: {
761 int new;
762
763 if (Tcl_GetIndexFromObj(interp, valuePtr,
764 (CONST char **) optionPtr->specPtr->clientData,
765 optionPtr->specPtr->optionName+1, 0, &new) != TCL_OK) {
766 return TCL_ERROR;
767 }
768 if (internalPtr != NULL) {
769 *((int *) oldInternalPtr) = *((int *) internalPtr);
770 *((int *) internalPtr) = new;
771 }
772 break;
773 }
774 case TK_OPTION_COLOR: {
775 XColor *newPtr;
776
777 if (nullOK && ObjectIsEmpty(valuePtr)) {
778 valuePtr = NULL;
779 newPtr = NULL;
780 } else {
781 newPtr = Tk_AllocColorFromObj(interp, tkwin, valuePtr);
782 if (newPtr == NULL) {
783 return TCL_ERROR;
784 }
785 }
786 if (internalPtr != NULL) {
787 *((XColor **) oldInternalPtr) = *((XColor **) internalPtr);
788 *((XColor **) internalPtr) = newPtr;
789 }
790 break;
791 }
792 case TK_OPTION_FONT: {
793 Tk_Font new;
794
795 if (nullOK && ObjectIsEmpty(valuePtr)) {
796 valuePtr = NULL;
797 new = NULL;
798 } else {
799 new = Tk_AllocFontFromObj(interp, tkwin, valuePtr);
800 if (new == NULL) {
801 return TCL_ERROR;
802 }
803 }
804 if (internalPtr != NULL) {
805 *((Tk_Font *) oldInternalPtr) = *((Tk_Font *) internalPtr);
806 *((Tk_Font *) internalPtr) = new;
807 }
808 break;
809 }
810 case TK_OPTION_STYLE: {
811 Tk_Style new;
812
813 if (nullOK && ObjectIsEmpty(valuePtr)) {
814 valuePtr = NULL;
815 new = NULL;
816 } else {
817 new = Tk_AllocStyleFromObj(interp, valuePtr);
818 if (new == NULL) {
819 return TCL_ERROR;
820 }
821 }
822 if (internalPtr != NULL) {
823 *((Tk_Style *) oldInternalPtr) = *((Tk_Style *) internalPtr);
824 *((Tk_Style *) internalPtr) = new;
825 }
826 break;
827 }
828 case TK_OPTION_BITMAP: {
829 Pixmap new;
830
831 if (nullOK && ObjectIsEmpty(valuePtr)) {
832 valuePtr = NULL;
833 new = None;
834 } else {
835 new = Tk_AllocBitmapFromObj(interp, tkwin, valuePtr);
836 if (new == None) {
837 return TCL_ERROR;
838 }
839 }
840 if (internalPtr != NULL) {
841 *((Pixmap *) oldInternalPtr) = *((Pixmap *) internalPtr);
842 *((Pixmap *) internalPtr) = new;
843 }
844 break;
845 }
846 case TK_OPTION_BORDER: {
847 Tk_3DBorder new;
848
849 if (nullOK && ObjectIsEmpty(valuePtr)) {
850 valuePtr = NULL;
851 new = NULL;
852 } else {
853 new = Tk_Alloc3DBorderFromObj(interp, tkwin, valuePtr);
854 if (new == NULL) {
855 return TCL_ERROR;
856 }
857 }
858 if (internalPtr != NULL) {
859 *((Tk_3DBorder *) oldInternalPtr) =
860 *((Tk_3DBorder *) internalPtr);
861 *((Tk_3DBorder *) internalPtr) = new;
862 }
863 break;
864 }
865 case TK_OPTION_RELIEF: {
866 int new;
867
868 if (nullOK && ObjectIsEmpty(valuePtr)) {
869 valuePtr = NULL;
870 new = TK_RELIEF_NULL;
871 } else {
872 if (Tk_GetReliefFromObj(interp, valuePtr, &new) != TCL_OK) {
873 return TCL_ERROR;
874 }
875 }
876 if (internalPtr != NULL) {
877 *((int *) oldInternalPtr) = *((int *) internalPtr);
878 *((int *) internalPtr) = new;
879 }
880 break;
881 }
882 case TK_OPTION_CURSOR: {
883 Tk_Cursor new;
884
885 if (nullOK && ObjectIsEmpty(valuePtr)) {
886 new = None;
887 valuePtr = NULL;
888 } else {
889 new = Tk_AllocCursorFromObj(interp, tkwin, valuePtr);
890 if (new == None) {
891 return TCL_ERROR;
892 }
893 }
894 if (internalPtr != NULL) {
895 *((Tk_Cursor *) oldInternalPtr) = *((Tk_Cursor *) internalPtr);
896 *((Tk_Cursor *) internalPtr) = new;
897 }
898 Tk_DefineCursor(tkwin, new);
899 break;
900 }
901 case TK_OPTION_JUSTIFY: {
902 Tk_Justify new;
903
904 if (Tk_GetJustifyFromObj(interp, valuePtr, &new) != TCL_OK) {
905 return TCL_ERROR;
906 }
907 if (internalPtr != NULL) {
908 *((Tk_Justify *) oldInternalPtr)
909 = *((Tk_Justify *) internalPtr);
910 *((Tk_Justify *) internalPtr) = new;
911 }
912 break;
913 }
914 case TK_OPTION_ANCHOR: {
915 Tk_Anchor new;
916
917 if (Tk_GetAnchorFromObj(interp, valuePtr, &new) != TCL_OK) {
918 return TCL_ERROR;
919 }
920 if (internalPtr != NULL) {
921 *((Tk_Anchor *) oldInternalPtr)
922 = *((Tk_Anchor *) internalPtr);
923 *((Tk_Anchor *) internalPtr) = new;
924 }
925 break;
926 }
927 case TK_OPTION_PIXELS: {
928 int new;
929
930 if (nullOK && ObjectIsEmpty(valuePtr)) {
931 valuePtr = NULL;
932 new = 0;
933 } else {
934 if (Tk_GetPixelsFromObj(interp, tkwin, valuePtr,
935 &new) != TCL_OK) {
936 return TCL_ERROR;
937 }
938 }
939 if (internalPtr != NULL) {
940 *((int *) oldInternalPtr) = *((int *) internalPtr);
941 *((int *) internalPtr) = new;
942 }
943 break;
944 }
945 case TK_OPTION_WINDOW: {
946 Tk_Window new;
947
948 if (nullOK && ObjectIsEmpty(valuePtr)) {
949 valuePtr = NULL;
950 new = None;
951 } else {
952 if (TkGetWindowFromObj(interp, tkwin, valuePtr, &new)
953 != TCL_OK) {
954 return TCL_ERROR;
955 }
956 }
957 if (internalPtr != NULL) {
958 *((Tk_Window *) oldInternalPtr) = *((Tk_Window *) internalPtr);
959 *((Tk_Window *) internalPtr) = new;
960 }
961 break;
962 }
963 /* As we make a copy or increment REFCNT use internal slot
964 to give us the hook to free it again
965 */
966 case TK_OPTION_SCALARVAR:
967 case TK_OPTION_ARRAYVAR:
968 case TK_OPTION_HASHVAR:
969 case TK_OPTION_CALLBACK:
970 case TK_OPTION_OBJ: {
971 int code = TCL_OK;
972 if (nullOK && (LangNull(valuePtr)||ObjectIsEmpty(valuePtr))) {
973 valuePtr = NULL;
974 }
975 if (internalPtr != NULL) {
976 *((Tcl_Obj **) oldInternalPtr) = *((Tcl_Obj **) internalPtr);
977 code = LangConfigObj(interp,(Tcl_Obj **) internalPtr,
978 valuePtr,optionPtr->specPtr->type);
979 if (code != TCL_OK) {
980 return code;
981 }
982 } else {
983 LangDebug("No attempt to save internal form of %s's %s\n",
984 Tk_Class(tkwin), specPtr->optionName);
985 abort();
986 }
987 #if 0
988 if (optionPtr->specPtr->type == TK_OPTION_SCALARVAR) {
989 LangDebug("configure %s's %s\n",
990 Tk_Class(tkwin), specPtr->optionName);
991 LangDumpVec("old",1,(Tcl_Obj **) oldInternalPtr);
992 LangDumpVec("src",1,&valuePtr);
993 LangDumpVec("new",1,(Tcl_Obj **) internalPtr);
994
995 }
996 #endif
997 break;
998 }
999 case TK_OPTION_CUSTOM: {
1000 Tk_ObjCustomOption *custom = optionPtr->extra.custom;
1001 if (custom->setProc(custom->clientData, interp, tkwin,
1002 &valuePtr, recordPtr, optionPtr->specPtr->internalOffset,
1003 (char *)oldInternalPtr,
1004 optionPtr->specPtr->flags) != TCL_OK) {
1005 return TCL_ERROR;
1006 }
1007 break;
1008 }
1009
1010 default: {
1011 char buf[40+TCL_INTEGER_SPACE];
1012 sprintf(buf, "bad config table: unknown type %d",
1013 optionPtr->specPtr->type);
1014 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1015 return TCL_ERROR;
1016 }
1017 }
1018
1019 /*
1020 * Release resources associated with the old value, if we're not
1021 * returning it to the caller, then install the new object value into
1022 * the record.
1023 */
1024
1025 if (savedOptionPtr == NULL) {
1026 if (optionPtr->flags & OPTION_NEEDS_FREEING) {
1027 FreeResources(optionPtr, oldPtr, oldInternalPtr, tkwin);
1028 }
1029 if (oldPtr != NULL) {
1030 Tcl_DecrRefCount(oldPtr);
1031 }
1032 }
1033 if (slotPtrPtr != NULL) {
1034 #ifdef _LANG
1035 *slotPtrPtr = LangCopyArg(valuePtr);
1036 #else
1037 *slotPtrPtr = valuePtr;
1038 if (valuePtr != NULL) {
1039 Tcl_IncrRefCount(valuePtr);
1040 }
1041 #endif
1042 }
1043 return TCL_OK;
1044 }
1045
1046 /*
1047 *----------------------------------------------------------------------
1048 *
1049 * ObjectIsEmpty --
1050 *
1051 * This procedure tests whether the string value of an object is
1052 * empty.
1053 *
1054 * Results:
1055 * The return value is 1 if the string value of objPtr has length
1056 * zero, and 0 otherwise.
1057 *
1058 * Side effects:
1059 * None.
1060 *
1061 *----------------------------------------------------------------------
1062 */
1063
1064 static int
1065 ObjectIsEmpty(objPtr)
1066 Tcl_Obj *objPtr; /* Object to test. May be NULL. */
1067 {
1068 int length;
1069
1070 if (objPtr == NULL) {
1071 return 1;
1072 }
1073 #ifdef USE_TCL_STRUCT
1074 if (objPtr->bytes != NULL) {
1075 return (objPtr->length == 0);
1076 }
1077 #endif
1078 #if 0
1079 Tcl_GetStringFromObj(objPtr, &length);
1080 return (length == 0);
1081 #else
1082 return LangNull(objPtr);
1083 #endif
1084 }
1085
1086 /*
1087 *----------------------------------------------------------------------
1088 *
1089 * GetOption --
1090 *
1091 * This procedure searches through a chained option table to find
1092 * the entry for a particular option name.
1093 *
1094 * Results:
1095 * The return value is a pointer to the matching entry, or NULL
1096 * if no matching entry could be found.
1097 * Note: if the matching entry is a synonym then this procedure
1098 * returns a pointer to the synonym entry, *not* the "real" entry
1099 * that the synonym refers to.
1100 *
1101 * Side effects:
1102 * None.
1103 *
1104 *----------------------------------------------------------------------
1105 */
1106
1107 static Option *
1108 GetOption(name, tablePtr)
1109 CONST char *name; /* String balue to be looked up in the
1110 * option table. */
1111 OptionTable *tablePtr; /* Table in which to look up name. */
1112 {
1113 Option *bestPtr, *optionPtr;
1114 OptionTable *tablePtr2;
1115 CONST char *p1, *p2;
1116 int count;
1117
1118 /*
1119 * Search through all of the option tables in the chain to find the
1120 * best match. Some tricky aspects:
1121 *
1122 * 1. We have to accept unique abbreviations.
1123 * 2. The same name could appear in different tables in the chain.
1124 * If this happens, we use the entry from the first table. We
1125 * have to be careful to distinguish this case from an ambiguous
1126 * abbreviation.
1127 */
1128
1129 bestPtr = NULL;
1130 for (tablePtr2 = tablePtr; tablePtr2 != NULL;
1131 tablePtr2 = tablePtr2->nextPtr) {
1132 for (optionPtr = tablePtr2->options, count = tablePtr2->numOptions;
1133 count > 0; optionPtr++, count--) {
1134 for (p1 = name, p2 = optionPtr->specPtr->optionName;
1135 *p1 == *p2; p1++, p2++) {
1136 if (*p1 == 0) {
1137 /*
1138 * This is an exact match. We're done.
1139 */
1140
1141 bestPtr = optionPtr;
1142 goto done;
1143 }
1144 }
1145 if (*p1 == 0) {
1146 /*
1147 * The name is an abbreviation for this option. Keep
1148 * to make sure that the abbreviation only matches one
1149 * option name. If we've already found a match in the
1150 * past, then it is an error unless the full names for
1151 * the two options are identical; in this case, the first
1152 * option overrides the second.
1153 */
1154
1155 if (bestPtr == NULL) {
1156 bestPtr = optionPtr;
1157 } else {
1158 if (strcmp(bestPtr->specPtr->optionName,
1159 optionPtr->specPtr->optionName) != 0) {
1160 goto error;
1161 }
1162 }
1163 }
1164 }
1165 }
1166
1167 done:
1168 return bestPtr;
1169
1170 error:
1171 return NULL;
1172 }
1173
1174 /*
1175 *----------------------------------------------------------------------
1176 *
1177 * GetOptionFromObj --
1178 *
1179 * This procedure searches through a chained option table to find
1180 * the entry for a particular option name.
1181 *
1182 * Results:
1183 * The return value is a pointer to the matching entry, or NULL
1184 * if no matching entry could be found. If NULL is returned and
1185 * interp is not NULL than an error message is left in its result.
1186 * Note: if the matching entry is a synonym then this procedure
1187 * returns a pointer to the synonym entry, *not* the "real" entry
1188 * that the synonym refers to.
1189 *
1190 * Side effects:
1191 * Information about the matching entry is cached in the object
1192 * containing the name, so that future lookups can proceed more
1193 * quickly.
1194 *
1195 *----------------------------------------------------------------------
1196 */
1197
1198 static Option *
1199 GetOptionFromObj(interp, objPtr, tablePtr)
1200 Tcl_Interp *interp; /* Used only for error reporting; if NULL
1201 * no message is left after an error. */
1202 Tcl_Obj *objPtr; /* Object whose string value is to be
1203 * looked up in the option table. */
1204 OptionTable *tablePtr; /* Table in which to look up objPtr. */
1205 {
1206 Option *bestPtr;
1207 char *name;
1208
1209 /*
1210 * First, check to see if the object already has the answer cached.
1211 */
1212
1213 if (objPtr->typePtr == &tkOptionObjType) {
1214 if (objPtr->internalRep.twoPtrValue.ptr1 == (VOID *) tablePtr) {
1215 return (Option *) objPtr->internalRep.twoPtrValue.ptr2;
1216 }
1217 }
1218
1219 /*
1220 * The answer isn't cached.
1221 */
1222
1223 name = Tcl_GetStringFromObj(objPtr, (int *) NULL);
1224 bestPtr = GetOption(name, tablePtr);
1225 if (bestPtr == NULL) {
1226 goto error;
1227 }
1228
1229 if ((objPtr->typePtr != NULL)
1230 && (objPtr->typePtr->freeIntRepProc != NULL)) {
1231 objPtr->typePtr->freeIntRepProc(objPtr);
1232 }
1233 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) tablePtr;
1234 objPtr->internalRep.twoPtrValue.ptr2 = (VOID *) bestPtr;
1235 TclObjSetType(objPtr,&tkOptionObjType);
1236 return bestPtr;
1237
1238 error:
1239 if (interp != NULL) {
1240 Tcl_AppendResult(interp, "unknown option \"", name,
1241 "\"", (char *) NULL);
1242 }
1243 return NULL;
1244 }
1245
1246 /*
1247 *----------------------------------------------------------------------
1248 *
1249 * TkGetOptionSpec --
1250 *
1251 * This procedure searches through a chained option table to find
1252 * the option spec for a particular option name.
1253 *
1254 * Results:
1255 * The return value is a pointer to the option spec of the matching
1256 * entry, or NULL if no matching entry could be found.
1257 * Note: if the matching entry is a synonym then this procedure
1258 * returns a pointer to the option spec of the synonym entry, *not*
1259 * the "real" entry that the synonym refers to.
1260 * Note: this call is primarily used by the style management code
1261 * (tkStyle.c) to look up an element's option spec into a widget's
1262 * option table.
1263 *
1264 * Side effects:
1265 * None.
1266 *
1267 *----------------------------------------------------------------------
1268 */
1269
1270 CONST Tk_OptionSpec *
1271 TkGetOptionSpec(name, optionTable)
1272 CONST char *name; /* String value to be looked up. */
1273 Tk_OptionTable optionTable; /* Table in which to look up name. */
1274 {
1275 Option *optionPtr;
1276
1277 optionPtr = GetOption(name, (OptionTable *) optionTable);
1278 if (optionPtr == NULL) {
1279 return NULL;
1280 }
1281 return optionPtr->specPtr;
1282 }
1283
1284 /*
1285 *----------------------------------------------------------------------
1286 *
1287 * SetOptionFromAny --
1288 *
1289 * This procedure is called to convert a Tcl object to option
1290 * internal form. However, this doesn't make sense (need to have a
1291 * table of options in order to do the conversion) so the
1292 * procedure always generates an error.
1293 *
1294 * Results:
1295 * The return value is always TCL_ERROR, and an error message is
1296 * left in interp's result if interp isn't NULL.
1297 *
1298 * Side effects:
1299 * None.
1300 *
1301 *----------------------------------------------------------------------
1302 */
1303
1304 static int
1305 SetOptionFromAny(interp, objPtr)
1306 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1307 register Tcl_Obj *objPtr; /* The object to convert. */
1308 {
1309 Tcl_AppendToObj(Tcl_GetObjResult(interp),
1310 "can't convert value to option except via GetOptionFromObj API",
1311 -1);
1312 return TCL_ERROR;
1313 }
1314
1315 /*
1316 *--------------------------------------------------------------
1317 *
1318 * Tk_SetOptions --
1319 *
1320 * Process one or more name-value pairs for configuration options
1321 * and fill in fields of a record with new values.
1322 *
1323 * Results:
1324 * If all goes well then TCL_OK is returned and the old values of
1325 * any modified objects are saved in *savePtr, if it isn't NULL (the
1326 * caller must eventually call Tk_RestoreSavedOptions or
1327 * Tk_FreeSavedOptions to free the contents of *savePtr). In
1328 * addition, if maskPtr isn't NULL then *maskPtr is filled in with
1329 * the OR of the typeMask bits from all modified options. If an
1330 * error occurs then TCL_ERROR is returned and a message
1331 * is left in interp's result unless interp is NULL; nothing is
1332 * saved in *savePtr or *maskPtr in this case.
1333 *
1334 * Side effects:
1335 * The fields of recordPtr get filled in with object pointers
1336 * from objc/objv. Old information in widgRec's fields gets
1337 * recycled. Information may be left at *savePtr.
1338 *
1339 *--------------------------------------------------------------
1340 */
1341
1342 int
1343 Tk_SetOptions(interp, recordPtr, optionTable, objc, objv, tkwin, savePtr,
1344 maskPtr)
1345 Tcl_Interp *interp; /* Interpreter for error reporting.
1346 * If NULL, then no error message is
1347 * returned.*/
1348 char *recordPtr; /* The record to configure. */
1349 Tk_OptionTable optionTable; /* Describes valid options. */
1350 int objc; /* The number of elements in objv. */
1351 Tcl_Obj *CONST objv[]; /* Contains one or more name-value
1352 * pairs. */
1353 Tk_Window tkwin; /* Window associated with the thing
1354 * being configured; needed for some
1355 * options (such as colors). */
1356 Tk_SavedOptions *savePtr; /* If non-NULL, the old values of
1357 * modified options are saved here
1358 * so that they can be restored
1359 * after an error. */
1360 int *maskPtr; /* It non-NULL, this word is modified
1361 * on a successful return to hold the
1362 * bit-wise OR of the typeMask fields
1363 * of all options that were modified
1364 * by this call. Used by the caller
1365 * to figure out which options
1366 * actually changed. */
1367 {
1368 OptionTable *tablePtr = (OptionTable *) optionTable;
1369 Option *optionPtr;
1370 Tk_SavedOptions *lastSavePtr, *newSavePtr;
1371 int mask;
1372
1373 #ifdef _LANG
1374 /*
1375 * If -class Foo is the first option/value pair then
1376 * reset widget class if necessary and re-set the defaults
1377 * in the option table.
1378 */
1379 if (tkwin && objc >= 2 && strcmp(Tcl_GetString(objv[0]),"-class") == 0) {
1380 char *new = Tcl_GetString(objv[1]);
1381 if (strcmp(new,Tk_Class(tkwin)) != 0) {
1382 Tk_SetClass(tkwin, Tcl_GetString(objv[1]));
1383 if (Tk_InitOptions(interp, recordPtr, optionTable, tkwin) != TCL_OK) {
1384 return TCL_ERROR;
1385 }
1386 }
1387 /* See if option was legal */
1388 optionPtr = GetOptionFromObj(interp, objv[0], tablePtr);
1389 if (optionPtr == NULL) {
1390 if (interp)
1391 Tcl_ResetResult(interp);
1392 objc -= 2;
1393 objv += 2;
1394 }
1395 }
1396 #endif
1397
1398 if (savePtr != NULL) {
1399 savePtr->recordPtr = recordPtr;
1400 savePtr->tkwin = tkwin;
1401 savePtr->numItems = 0;
1402 savePtr->nextPtr = NULL;
1403 }
1404 lastSavePtr = savePtr;
1405
1406 /*
1407 * Scan through all of the arguments, processing those
1408 * that match entries in the option table.
1409 */
1410
1411 mask = 0;
1412 for ( ; objc > 0; objc -= 2, objv += 2) {
1413 optionPtr = GetOptionFromObj(interp, objv[0], tablePtr);
1414 if (optionPtr == NULL) {
1415 goto error;
1416 }
1417 if (optionPtr->specPtr->type == TK_OPTION_SYNONYM) {
1418 optionPtr = optionPtr->extra.synonymPtr;
1419 }
1420
1421 if (objc < 2) {
1422 if (interp != NULL) {
1423 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1424 "value for \"", Tcl_GetStringFromObj(*objv, NULL),
1425 "\" missing", (char *) NULL);
1426 goto error;
1427 }
1428 }
1429 if ((savePtr != NULL)
1430 && (lastSavePtr->numItems >= TK_NUM_SAVED_OPTIONS)) {
1431 /*
1432 * We've run out of space for saving old option values. Allocate
1433 * more space.
1434 */
1435
1436 newSavePtr = (Tk_SavedOptions *) ckalloc(sizeof(
1437 Tk_SavedOptions));
1438 newSavePtr->recordPtr = recordPtr;
1439 newSavePtr->tkwin = tkwin;
1440 newSavePtr->numItems = 0;
1441 newSavePtr->nextPtr = NULL;
1442 lastSavePtr->nextPtr = newSavePtr;
1443 lastSavePtr = newSavePtr;
1444 }
1445 if (DoObjConfig(interp, recordPtr, optionPtr, objv[1], tkwin,
1446 (savePtr != NULL) ? &lastSavePtr->items[lastSavePtr->numItems]
1447 : (Tk_SavedOption *) NULL) != TCL_OK) {
1448 char msg[100];
1449
1450 sprintf(msg, "\n (processing \"%.40s\" option)",
1451 Tcl_GetStringFromObj(*objv, NULL));
1452 Tcl_AddErrorInfo(interp, msg);
1453 goto error;
1454 }
1455 if (savePtr != NULL) {
1456 lastSavePtr->numItems++;
1457 }
1458 mask |= optionPtr->specPtr->typeMask;
1459 }
1460 if (maskPtr != NULL) {
1461 *maskPtr = mask;
1462 }
1463 return TCL_OK;
1464
1465 error:
1466 if (savePtr != NULL) {
1467 Tk_RestoreSavedOptions(savePtr);
1468 }
1469 return TCL_ERROR;
1470 }
1471
1472 /*
1473 *----------------------------------------------------------------------
1474 *
1475 * Tk_RestoreSavedOptions --
1476 *
1477 * This procedure undoes the effect of a previous call to
1478 * Tk_SetOptions by restoring all of the options to their value
1479 * before the call to Tk_SetOptions.
1480 *
1481 * Results:
1482 * None.
1483 *
1484 * Side effects:
1485 * The configutation record is restored and all the information
1486 * stored in savePtr is freed.
1487 *
1488 *----------------------------------------------------------------------
1489 */
1490
1491 void
1492 Tk_RestoreSavedOptions(savePtr)
1493 Tk_SavedOptions *savePtr; /* Holds saved option information; must
1494 * have been passed to Tk_SetOptions. */
1495 {
1496 int i;
1497 Option *optionPtr;
1498 Tcl_Obj *newPtr; /* New object value of option, which we
1499 * replace with old value and free. Taken
1500 * from record. */
1501 char *internalPtr; /* Points to internal value of option in
1502 * record. */
1503 CONST Tk_OptionSpec *specPtr;
1504
1505 /*
1506 * Be sure to restore the options in the opposite order they were
1507 * set. This is important because it's possible that the same
1508 * option name was used twice in a single call to Tk_SetOptions.
1509 */
1510
1511 if (savePtr->nextPtr != NULL) {
1512 Tk_RestoreSavedOptions(savePtr->nextPtr);
1513 ckfree((char *) savePtr->nextPtr);
1514 savePtr->nextPtr = NULL;
1515 }
1516 for (i = savePtr->numItems - 1; i >= 0; i--) {
1517 optionPtr = savePtr->items[i].optionPtr;
1518 specPtr = optionPtr->specPtr;
1519
1520 /*
1521 * First free the new value of the option, which is currently
1522 * in the record.
1523 */
1524
1525 if (specPtr->objOffset >= 0) {
1526 newPtr = *((Tcl_Obj **) (savePtr->recordPtr + specPtr->objOffset));
1527 } else {
1528 newPtr = NULL;
1529 }
1530 if (specPtr->internalOffset >= 0) {
1531 internalPtr = savePtr->recordPtr + specPtr->internalOffset;
1532 } else {
1533 internalPtr = NULL;
1534 }
1535 if (optionPtr->flags & OPTION_NEEDS_FREEING) {
1536 FreeResources(optionPtr, newPtr, internalPtr, savePtr->tkwin);
1537 }
1538 if (newPtr != NULL) {
1539 Tcl_DecrRefCount(newPtr);
1540 }
1541
1542 /*
1543 * Now restore the old value of the option.
1544 */
1545
1546 if (specPtr->objOffset >= 0) {
1547 *((Tcl_Obj **) (savePtr->recordPtr + specPtr->objOffset))
1548 = savePtr->items[i].valuePtr;
1549 }
1550 if (specPtr->internalOffset >= 0) {
1551 switch (specPtr->type) {
1552 case TK_OPTION_BOOLEAN: {
1553 *((int *) internalPtr)
1554 = *((int *) &savePtr->items[i].internalForm);
1555 break;
1556 }
1557 case TK_OPTION_INT: {
1558 *((int *) internalPtr)
1559 = *((int *) &savePtr->items[i].internalForm);
1560 break;
1561 }
1562 case TK_OPTION_DOUBLE: {
1563 *((double *) internalPtr)
1564 = *((double *) &savePtr->items[i].internalForm);
1565 break;
1566 }
1567 case TK_OPTION_STRING: {
1568 *((char **) internalPtr)
1569 = *((char **) &savePtr->items[i].internalForm);
1570 break;
1571 }
1572 case TK_OPTION_STRING_TABLE: {
1573 *((int *) internalPtr)
1574 = *((int *) &savePtr->items[i].internalForm);
1575 break;
1576 }
1577 case TK_OPTION_COLOR: {
1578 *((XColor **) internalPtr)
1579 = *((XColor **) &savePtr->items[i].internalForm);
1580 break;
1581 }
1582 case TK_OPTION_FONT: {
1583 *((Tk_Font *) internalPtr)
1584 = *((Tk_Font *) &savePtr->items[i].internalForm);
1585 break;
1586 }
1587 case TK_OPTION_STYLE: {
1588 *((Tk_Style *) internalPtr)
1589 = *((Tk_Style *) &savePtr->items[i].internalForm);
1590 break;
1591 }
1592 case TK_OPTION_BITMAP: {
1593 *((Pixmap *) internalPtr)
1594 = *((Pixmap *) &savePtr->items[i].internalForm);
1595 break;
1596 }
1597 case TK_OPTION_BORDER: {
1598 *((Tk_3DBorder *) internalPtr)
1599 = *((Tk_3DBorder *) &savePtr->items[i].internalForm);
1600 break;
1601 }
1602 case TK_OPTION_RELIEF: {
1603 *((int *) internalPtr)
1604 = *((int *) &savePtr->items[i].internalForm);
1605 break;
1606 }
1607 case TK_OPTION_CURSOR: {
1608 *((Tk_Cursor *) internalPtr)
1609 = *((Tk_Cursor *) &savePtr->items[i].internalForm);
1610 Tk_DefineCursor(savePtr->tkwin,
1611 *((Tk_Cursor *) internalPtr));
1612 break;
1613 }
1614 case TK_OPTION_JUSTIFY: {
1615 *((Tk_Justify *) internalPtr)
1616 = *((Tk_Justify *) &savePtr->items[i].internalForm);
1617 break;
1618 }
1619 case TK_OPTION_ANCHOR: {
1620 *((Tk_Anchor *) internalPtr)
1621 = *((Tk_Anchor *) &savePtr->items[i].internalForm);
1622 break;
1623 }
1624 case TK_OPTION_PIXELS: {
1625 *((int *) internalPtr)
1626 = *((int *) &savePtr->items[i].internalForm);
1627 break;
1628 }
1629 case TK_OPTION_WINDOW: {
1630 *((Tk_Window *) internalPtr)
1631 = *((Tk_Window *) &savePtr->items[i].internalForm);
1632 break;
1633 }
1634 case TK_OPTION_SCALARVAR:
1635 case TK_OPTION_ARRAYVAR:
1636 case TK_OPTION_HASHVAR:
1637 case TK_OPTION_CALLBACK:
1638 case TK_OPTION_OBJ: {
1639 *((Tcl_Obj **) internalPtr)
1640 = *((Tcl_Obj **) &savePtr->items[i].internalForm);
1641 #if 0
1642 LangDebug("restore %s's %s\n",
1643 Tk_Class(savePtr->tkwin), optionPtr->specPtr->optionName);
1644 LangDumpVec("Now",1,(Tcl_Obj **) internalPtr);
1645 #endif
1646 break;
1647 }
1648 case TK_OPTION_CUSTOM: {
1649 Tk_ObjCustomOption *custom = optionPtr->extra.custom;
1650 if (custom->restoreProc != NULL) {
1651 custom->restoreProc(custom->clientData, savePtr->tkwin,
1652 internalPtr,
1653 (char *)&savePtr->items[i].internalForm);
1654 }
1655 break;
1656 }
1657 default: {
1658 panic("bad option type in Tk_RestoreSavedOptions");
1659 }
1660 }
1661 }
1662 }
1663 savePtr->numItems = 0;
1664 }
1665
1666 /*
1667 *--------------------------------------------------------------
1668 *
1669 * Tk_FreeSavedOptions --
1670 *
1671 * Free all of the saved configuration option values from a
1672 * previous call to Tk_SetOptions.
1673 *
1674 * Results:
1675 * None.
1676 *
1677 * Side effects:
1678 * Storage and system resources are freed.
1679 *
1680 *--------------------------------------------------------------
1681 */
1682
1683 void
1684 Tk_FreeSavedOptions(savePtr)
1685 Tk_SavedOptions *savePtr; /* Contains options saved in a previous
1686 * call to Tk_SetOptions. */
1687 {
1688 int count;
1689 Tk_SavedOption *savedOptionPtr;
1690
1691 if (savePtr->nextPtr != NULL) {
1692 Tk_FreeSavedOptions(savePtr->nextPtr);
1693 ckfree((char *) savePtr->nextPtr);
1694 }
1695 for (count = savePtr->numItems,
1696 savedOptionPtr = &savePtr->items[savePtr->numItems-1];
1697 count > 0; count--, savedOptionPtr--) {
1698 if (savedOptionPtr->optionPtr->flags & OPTION_NEEDS_FREEING) {
1699 FreeResources(savedOptionPtr->optionPtr, savedOptionPtr->valuePtr,
1700 (char *) &savedOptionPtr->internalForm, savePtr->tkwin);
1701 }
1702 if (savedOptionPtr->valuePtr != NULL) {
1703 Tcl_DecrRefCount(savedOptionPtr->valuePtr);
1704 }
1705 }
1706 }
1707
1708 /*
1709 *----------------------------------------------------------------------
1710 *
1711 * Tk_FreeConfigOptions --
1712 *
1713 * Free all resources associated with configuration options.
1714 *
1715 * Results:
1716 * None.
1717 *
1718 * Side effects:
1719 * All of the Tcl_Obj's in recordPtr that are controlled by
1720 * configuration options in optionTable are freed.
1721 *
1722 *----------------------------------------------------------------------
1723 */
1724
1725 /* ARGSUSED */
1726 void
1727 Tk_FreeConfigOptions(recordPtr, optionTable, tkwin)
1728 char *recordPtr; /* Record whose fields contain current
1729 * values for options. */
1730 Tk_OptionTable optionTable; /* Describes legal options. */
1731 Tk_Window tkwin; /* Window associated with recordPtr; needed
1732 * for freeing some options. */
1733 {
1734 OptionTable *tablePtr;
1735 Option *optionPtr;
1736 int count;
1737 Tcl_Obj **oldPtrPtr, *oldPtr;
1738 char *oldInternalPtr;
1739 CONST Tk_OptionSpec *specPtr;
1740
1741 for (tablePtr = (OptionTable *) optionTable; tablePtr != NULL;
1742 tablePtr = tablePtr->nextPtr) {
1743 for (optionPtr = tablePtr->options, count = tablePtr->numOptions;
1744 count > 0; optionPtr++, count--) {
1745 specPtr = optionPtr->specPtr;
1746 if (specPtr->type == TK_OPTION_SYNONYM) {
1747 continue;
1748 }
1749 if (specPtr->objOffset >= 0) {
1750 oldPtrPtr = (Tcl_Obj **) (recordPtr + specPtr->objOffset);
1751 oldPtr = *oldPtrPtr;
1752 *oldPtrPtr = NULL;
1753 } else {
1754 oldPtr = NULL;
1755 }
1756 if (specPtr->internalOffset >= 0) {
1757 oldInternalPtr = recordPtr + specPtr->internalOffset;
1758 } else {
1759 oldInternalPtr = NULL;
1760 }
1761 if (optionPtr->flags & OPTION_NEEDS_FREEING) {
1762 FreeResources(optionPtr, oldPtr, oldInternalPtr, tkwin);
1763 }
1764 if (oldPtr != NULL) {
1765 Tcl_DecrRefCount(oldPtr);
1766 }
1767 }
1768 }
1769 }
1770
1771 /*
1772 *----------------------------------------------------------------------
1773 *
1774 * FreeResources --
1775 *
1776 * Free system resources associated with a configuration option,
1777 * such as colors or fonts.
1778 *
1779 * Results:
1780 * None.
1781 *
1782 * Side effects:
1783 * Any system resources associated with objPtr are released. However,
1784 * objPtr itself is not freed.
1785 *
1786 *----------------------------------------------------------------------
1787 */
1788
1789 static void
1790 FreeResources(optionPtr, objPtr, internalPtr, tkwin)
1791 Option *optionPtr; /* Description of the configuration option. */
1792 Tcl_Obj *objPtr; /* The current value of the option, specified
1793 * as an object. */
1794 char *internalPtr; /* A pointer to an internal representation for
1795 * the option's value, such as an int or
1796 * (XColor *). Only valid if
1797 * optionPtr->specPtr->internalOffset >= 0. */
1798 Tk_Window tkwin; /* The window in which this option is used. */
1799 {
1800 int internalFormExists;
1801
1802 /*
1803 * If there exists an internal form for the value, use it to free
1804 * resources (also zero out the internal form). If there is no
1805 * internal form, then use the object form.
1806 */
1807
1808 internalFormExists = optionPtr->specPtr->internalOffset >= 0;
1809 switch (optionPtr->specPtr->type) {
1810 case TK_OPTION_STRING:
1811 if (internalFormExists) {
1812 if (*((char **) internalPtr) != NULL) {
1813 ckfree(*((char **) internalPtr));
1814 *((char **) internalPtr) = NULL;
1815 }
1816 }
1817 break;
1818 case TK_OPTION_SCALARVAR:
1819 case TK_OPTION_ARRAYVAR:
1820 case TK_OPTION_HASHVAR:
1821 case TK_OPTION_CALLBACK:
1822 case TK_OPTION_OBJ:
1823 if (internalFormExists) {
1824 if (*((Tcl_Obj **) internalPtr) != NULL) {
1825 #if 0
1826 LangDebug("free %s's %s\n",
1827 Tk_Class(tkwin), optionPtr->specPtr->optionName);
1828 LangDumpVec("Was",1,(Tcl_Obj **) internalPtr);
1829 #endif
1830 Tcl_DecrRefCount(*((Tcl_Obj **) internalPtr));
1831 *((Tcl_Obj **) internalPtr) = NULL;
1832 }
1833 }
1834 break;
1835 case TK_OPTION_COLOR:
1836 if (internalFormExists) {
1837 if (*((XColor **) internalPtr) != NULL) {
1838 Tk_FreeColor(*((XColor **) internalPtr));
1839 *((XColor **) internalPtr) = NULL;
1840 }
1841 } else if (objPtr != NULL) {
1842 Tk_FreeColorFromObj(tkwin, objPtr);
1843 }
1844 break;
1845 case TK_OPTION_FONT:
1846 if (internalFormExists) {
1847 Tk_FreeFont(*((Tk_Font *) internalPtr));
1848 *((Tk_Font *) internalPtr) = NULL;
1849 } else if (objPtr != NULL) {
1850 Tk_FreeFontFromObj(tkwin, objPtr);
1851 }
1852 break;
1853 case TK_OPTION_STYLE:
1854 if (internalFormExists) {
1855 Tk_FreeStyle(*((Tk_Style *) internalPtr));
1856 *((Tk_Style *) internalPtr) = NULL;
1857 } else if (objPtr != NULL) {
1858 Tk_FreeStyleFromObj(objPtr);
1859 }
1860 break;
1861 case TK_OPTION_BITMAP:
1862 if (internalFormExists) {
1863 if (*((Pixmap *) internalPtr) != None) {
1864 Tk_FreeBitmap(Tk_Display(tkwin), *((Pixmap *) internalPtr));
1865 *((Pixmap *) internalPtr) = None;
1866 }
1867 } else if (objPtr != NULL) {
1868 Tk_FreeBitmapFromObj(tkwin, objPtr);
1869 }
1870 break;
1871 case TK_OPTION_BORDER:
1872 if (internalFormExists) {
1873 if (*((Tk_3DBorder *) internalPtr) != NULL) {
1874 Tk_Free3DBorder(*((Tk_3DBorder *) internalPtr));
1875 *((Tk_3DBorder *) internalPtr) = NULL;
1876 }
1877 } else if (objPtr != NULL) {
1878 Tk_Free3DBorderFromObj(tkwin, objPtr);
1879 }
1880 break;
1881 case TK_OPTION_CURSOR:
1882 if (internalFormExists) {
1883 if (*((Tk_Cursor *) internalPtr) != None) {
1884 Tk_FreeCursor(Tk_Display(tkwin),
1885 *((Tk_Cursor *) internalPtr));
1886 *((Tk_Cursor *) internalPtr) = None;
1887 }
1888 } else if (objPtr != NULL) {
1889 Tk_FreeCursorFromObj(tkwin, objPtr);
1890 }
1891 break;
1892 case TK_OPTION_CUSTOM: {
1893 Tk_ObjCustomOption *custom = optionPtr->extra.custom;
1894 if (internalFormExists && custom->freeProc != NULL) {
1895 custom->freeProc(custom->clientData, tkwin, internalPtr);
1896 }
1897 break;
1898 }
1899 default:
1900 break;
1901 }
1902 }
1903
1904 /*
1905 *--------------------------------------------------------------
1906 *
1907 * Tk_GetOptionInfo --
1908 *
1909 * Returns a list object containing complete information about
1910 * either a single option or all the configuration options in a
1911 * table.
1912 *
1913 * Results:
1914 * This procedure normally returns a pointer to an object.
1915 * If namePtr isn't NULL, then the result object is a list with
1916 * five elements: the option's name, its database name, database
1917 * class, default value, and current value. If the option is a
1918 * synonym then the list will contain only two values: the option
1919 * name and the name of the option it refers to. If namePtr is
1920 * NULL, then information is returned for every option in the
1921 * option table: the result will have one sub-list (in the form
1922 * described above) for each option in the table. If an error
1923 * occurs (e.g. because namePtr isn't valid) then NULL is returned
1924 * and an error message will be left in interp's result unless
1925 * interp is NULL.
1926 *
1927 * Side effects:
1928 * None.
1929 *
1930 *--------------------------------------------------------------
1931 */
1932
1933 Tcl_Obj *
1934 Tk_GetOptionInfo(interp, recordPtr, optionTable, namePtr, tkwin)
1935 Tcl_Interp *interp; /* Interpreter for error reporting. If
1936 * NULL, then no error message is created. */
1937 char *recordPtr; /* Record whose fields contain current
1938 * values for options. */
1939 Tk_OptionTable optionTable; /* Describes all the legal options. */
1940 Tcl_Obj *namePtr; /* If non-NULL, the string value selects
1941 * a single option whose info is to be
1942 * returned. Otherwise info is returned for
1943 * all options in optionTable. */
1944 Tk_Window tkwin; /* Window associated with recordPtr; needed
1945 * to compute correct default value for some
1946 * options. */
1947 {
1948 Tcl_Obj *resultPtr;
1949 OptionTable *tablePtr = (OptionTable *) optionTable;
1950 Option *optionPtr;
1951 int count;
1952
1953 /*
1954 * If information is only wanted for a single configuration
1955 * spec, then handle that one spec specially.
1956 */
1957
1958 if (namePtr != NULL) {
1959 optionPtr = GetOptionFromObj(interp, namePtr, tablePtr);
1960 if (optionPtr == NULL) {
1961 return (Tcl_Obj *) NULL;
1962 }
1963 if (optionPtr->specPtr->type == TK_OPTION_SYNONYM) {
1964 optionPtr = optionPtr->extra.synonymPtr;
1965 }
1966 return GetConfigList(interp, recordPtr, optionPtr, tkwin);
1967 }
1968
1969 /*
1970 * Loop through all the specs, creating a big list with all
1971 * their information.
1972 */
1973
1974 resultPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1975 for (; tablePtr != NULL; tablePtr = tablePtr->nextPtr) {
1976 for (optionPtr = tablePtr->options, count = tablePtr->numOptions;
1977 count > 0; optionPtr++, count--) {
1978 Tcl_ListObjAppendElement(interp, resultPtr,
1979 GetConfigList(interp, recordPtr, optionPtr, tkwin));
1980 }
1981 }
1982 return resultPtr;
1983 }
1984
1985 /*
1986 *--------------------------------------------------------------
1987 *
1988 * GetConfigList --
1989 *
1990 * Create a valid Tcl list holding the configuration information
1991 * for a single configuration option.
1992 *
1993 * Results:
1994 * A Tcl list, dynamically allocated. The caller is expected to
1995 * arrange for this list to be freed eventually.
1996 *
1997 * Side effects:
1998 * Memory is allocated.
1999 *
2000 *--------------------------------------------------------------
2001 */
2002
2003 static Tcl_Obj *
2004 GetConfigList(interp, recordPtr, optionPtr, tkwin)
2005 Tcl_Interp *interp;
2006 char *recordPtr; /* Pointer to record holding current
2007 * values of configuration options. */
2008 Option *optionPtr; /* Pointer to information describing a
2009 * particular option. */
2010 Tk_Window tkwin; /* Window corresponding to recordPtr. */
2011 {
2012 Tcl_Obj *listPtr, *elementPtr;
2013
2014 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
2015 Tcl_ListObjAppendElement(interp, listPtr,
2016 Tcl_NewStringObj(optionPtr->specPtr->optionName, -1));
2017
2018 if (optionPtr->specPtr->type == TK_OPTION_SYNONYM) {
2019 elementPtr = Tcl_NewStringObj(
2020 optionPtr->extra.synonymPtr->specPtr->optionName, -1);
2021 Tcl_ListObjAppendElement(interp, listPtr, elementPtr);
2022 } else {
2023 if (optionPtr->dbNameUID == NULL) {
2024 elementPtr = Tcl_NewObj();
2025 } else {
2026 elementPtr = Tcl_NewStringObj(optionPtr->dbNameUID, -1);
2027 }
2028 Tcl_ListObjAppendElement(interp, listPtr, elementPtr);
2029
2030 if (optionPtr->dbClassUID == NULL) {
2031 elementPtr = Tcl_NewObj();
2032 } else {
2033 elementPtr = Tcl_NewStringObj(optionPtr->dbClassUID, -1);
2034 }
2035 Tcl_ListObjAppendElement(interp, listPtr, elementPtr);
2036
2037 if ((tkwin != NULL) && ((optionPtr->specPtr->type == TK_OPTION_COLOR)
2038 || (optionPtr->specPtr->type == TK_OPTION_BORDER))
2039 && (Tk_Depth(tkwin) <= 1)
2040 && (optionPtr->extra.monoColorPtr != NULL)) {
2041 elementPtr = optionPtr->extra.monoColorPtr;
2042 Tcl_IncrRefCount(elementPtr);
2043 } else if (optionPtr->defaultPtr != NULL) {
2044 elementPtr = optionPtr->defaultPtr;
2045 Tcl_IncrRefCount(elementPtr);
2046 } else {
2047 elementPtr = Tcl_NewObj();
2048 }
2049 Tcl_ListObjAppendElement(interp, listPtr, elementPtr);
2050
2051 if (optionPtr->specPtr->objOffset >= 0) {
2052 elementPtr = *((Tcl_Obj **) (recordPtr
2053 + optionPtr->specPtr->objOffset));
2054 if (elementPtr == NULL) {
2055 elementPtr = Tcl_NewObj();
2056 } else {
2057 Tcl_IncrRefCount(elementPtr);
2058 }
2059 } else {
2060 elementPtr = GetObjectForOption(interp, recordPtr, optionPtr, tkwin);
2061 }
2062 Tcl_ListObjAppendElement(interp, listPtr, elementPtr);
2063 }
2064 return listPtr;
2065 }
2066
2067 /*
2068 *----------------------------------------------------------------------
2069 *
2070 * GetObjectForOption --
2071 *
2072 * This procedure is called to create an object that contains the
2073 * value for an option. It is invoked by GetConfigList and
2074 * Tk_GetOptionValue when only the internal form of an option is
2075 * stored in the record.
2076 *
2077 * Results:
2078 * The return value is a pointer to a Tcl object. The caller
2079 * must call Tcl_IncrRefCount on this object to preserve it.
2080 *
2081 * Side effects:
2082 * None.
2083 *
2084 *----------------------------------------------------------------------
2085 */
2086
2087 static Tcl_Obj *
2088 GetObjectForOption(interp, recordPtr, optionPtr, tkwin)
2089 Tcl_Interp *interp;
2090 char *recordPtr; /* Pointer to record holding current
2091 * values of configuration options. */
2092 Option *optionPtr; /* Pointer to information describing an
2093 * option whose internal value is stored
2094 * in *recordPtr. */
2095 Tk_Window tkwin; /* Window corresponding to recordPtr. */
2096 {
2097 Tcl_Obj *objPtr;
2098 char *internalPtr; /* Points to internal value of option in
2099 * record. */
2100
2101 internalPtr = recordPtr + optionPtr->specPtr->internalOffset;
2102 objPtr = NULL;
2103 switch (optionPtr->specPtr->type) {
2104 case TK_OPTION_BOOLEAN: {
2105 objPtr = Tcl_NewIntObj(*((int *) internalPtr));
2106 break;
2107 }
2108 case TK_OPTION_INT: {
2109 objPtr = Tcl_NewIntObj(*((int *) internalPtr));
2110 break;
2111 }
2112 case TK_OPTION_DOUBLE: {
2113 objPtr = Tcl_NewDoubleObj(*((double *) internalPtr));
2114 break;
2115 }
2116 case TK_OPTION_STRING: {
2117 objPtr = Tcl_NewStringObj(*((char **) internalPtr), -1);
2118 break;
2119 }
2120 case TK_OPTION_STRING_TABLE: {
2121 objPtr = Tcl_NewStringObj(
2122 ((char **) optionPtr->specPtr->clientData)[
2123 *((int *) internalPtr)], -1);
2124 break;
2125 }
2126 case TK_OPTION_COLOR: {
2127 XColor *colorPtr = *((XColor **) internalPtr);
2128 if (colorPtr != NULL) {
2129 objPtr = Tcl_NewStringObj(Tk_NameOfColor(colorPtr), -1);
2130 }
2131 break;
2132 }
2133 case TK_OPTION_FONT: {
2134 Tk_Font tkfont = *((Tk_Font *) internalPtr);
2135 if (tkfont != NULL) {
2136 objPtr = LangFontObj(interp,tkfont,NULL);
2137 }
2138 break;
2139 }
2140 case TK_OPTION_STYLE: {
2141 Tk_Style style = *((Tk_Style *) internalPtr);
2142 if (style != NULL) {
2143 objPtr = Tcl_NewStringObj(Tk_NameOfStyle(style), -1);
2144 }
2145 break;
2146 }
2147 case TK_OPTION_BITMAP: {
2148 Pixmap pixmap = *((Pixmap *) internalPtr);
2149 if (pixmap != None) {
2150 objPtr = Tcl_NewStringObj(Tk_NameOfBitmap(Tk_Display(tkwin),
2151 pixmap), -1);
2152 }
2153 break;
2154 }
2155 case TK_OPTION_BORDER: {
2156 Tk_3DBorder border = *((Tk_3DBorder *) internalPtr);
2157 if (border != NULL) {
2158 objPtr = Tcl_NewStringObj(Tk_NameOf3DBorder(border), -1);
2159 }
2160 break;
2161 }
2162 case TK_OPTION_RELIEF: {
2163 objPtr = Tcl_NewStringObj(Tk_NameOfRelief(
2164 *((int *) internalPtr)), -1);
2165 break;
2166 }
2167 case TK_OPTION_CURSOR: {
2168 Tk_Cursor cursor = *((Tk_Cursor *) internalPtr);
2169 if (cursor != None) {
2170 objPtr = Tcl_NewStringObj(
2171 Tk_NameOfCursor(Tk_Display(tkwin), cursor), -1);
2172 #ifdef _LANG
2173 {
2174 Tcl_Obj **elemv;
2175 int elemc;
2176 Tcl_ListObjGetElements(interp,objPtr,&elemc,&elemv);
2177 }
2178 #endif
2179 }
2180 break;
2181 }
2182 case TK_OPTION_JUSTIFY: {
2183 objPtr = Tcl_NewStringObj(Tk_NameOfJustify(
2184 *((Tk_Justify *) internalPtr)), -1);
2185 break;
2186 }
2187 case TK_OPTION_ANCHOR: {
2188 objPtr = Tcl_NewStringObj(Tk_NameOfAnchor(
2189 *((Tk_Anchor *) internalPtr)), -1);
2190 break;
2191 }
2192 case TK_OPTION_PIXELS: {
2193 objPtr = Tcl_NewIntObj(*((int *) internalPtr));
2194 break;
2195 }
2196 case TK_OPTION_WINDOW: {
2197 Tk_Window tkwin = *((Tk_Window *) internalPtr);
2198 if (tkwin != NULL) {
2199 objPtr = LangWidgetObj(interp,tkwin);
2200 }
2201 break;
2202 }
2203 case TK_OPTION_SCALARVAR:
2204 case TK_OPTION_ARRAYVAR:
2205 case TK_OPTION_HASHVAR: {
2206 LangSetVar(&objPtr,*((Tcl_Obj **) internalPtr));
2207 break;
2208 }
2209
2210 case TK_OPTION_CALLBACK:
2211 case TK_OPTION_OBJ: {
2212 Tcl_Obj *obj = *((Tcl_Obj **) internalPtr);
2213 if (obj != NULL) {
2214 Tcl_IncrRefCount(obj);
2215 objPtr = obj;
2216 #if 0
2217 LangDebug("return %s's %s\n",
2218 Tk_Class(tkwin), optionPtr->specPtr->optionName);
2219 LangDumpVec("Now",1,&obj);
2220 #endif
2221 }
2222 break;
2223 }
2224 case TK_OPTION_CUSTOM: {
2225 Tk_ObjCustomOption *custom = optionPtr->extra.custom;
2226 objPtr = custom->getProc(custom->clientData, tkwin, recordPtr,
2227 optionPtr->specPtr->internalOffset);
2228 break;
2229 }
2230 default: {
2231 panic("bad option type in GetObjectForOption");
2232 }
2233 }
2234 if (objPtr == NULL) {
2235 objPtr = Tcl_NewObj();
2236 }
2237 return objPtr;
2238 }
2239
2240 /*
2241 *----------------------------------------------------------------------
2242 *
2243 * Tk_GetOptionValue --
2244 *
2245 * This procedure returns the current value of a configuration
2246 * option.
2247 *
2248 * Results:
2249 * The return value is the object holding the current value of
2250 * the option given by namePtr. If no such option exists, then
2251 * the return value is NULL and an error message is left in
2252 * interp's result (if interp isn't NULL).
2253 *
2254 * Side effects:
2255 * None.
2256 *
2257 *----------------------------------------------------------------------
2258 */
2259
2260 Tcl_Obj *
2261 Tk_GetOptionValue(interp, recordPtr, optionTable, namePtr, tkwin)
2262 Tcl_Interp *interp; /* Interpreter for error reporting. If
2263 * NULL then no messages are provided for
2264 * errors. */
2265 char *recordPtr; /* Record whose fields contain current
2266 * values for options. */
2267 Tk_OptionTable optionTable; /* Describes legal options. */
2268 Tcl_Obj *namePtr; /* Gives the command-line name for the
2269 * option whose value is to be returned. */
2270 Tk_Window tkwin; /* Window corresponding to recordPtr. */
2271 {
2272 OptionTable *tablePtr = (OptionTable *) optionTable;
2273 Option *optionPtr;
2274 Tcl_Obj *resultPtr;
2275
2276 optionPtr = GetOptionFromObj(interp, namePtr, tablePtr);
2277 if (optionPtr == NULL) {
2278 return NULL;
2279 }
2280 if (optionPtr->specPtr->type == TK_OPTION_SYNONYM) {
2281 optionPtr = optionPtr->extra.synonymPtr;
2282 }
2283 if (optionPtr->specPtr->objOffset >= 0) {
2284 resultPtr = *((Tcl_Obj **) (recordPtr + optionPtr->specPtr->objOffset));
2285 if (resultPtr == NULL) {
2286 /*
2287 * This option has a null value and is represented by a null
2288 * object pointer. We can't return the null pointer, since that
2289 * would indicate an error. Instead, return a new empty object.
2290 */
2291
2292 resultPtr = Tcl_NewObj();
2293 } else {
2294 Tcl_IncrRefCount(resultPtr);
2295 }
2296 } else {
2297 resultPtr = GetObjectForOption(interp, recordPtr, optionPtr, tkwin);
2298 }
2299 return resultPtr;
2300 }
2301
2302 /*
2303 *----------------------------------------------------------------------
2304 *
2305 * TkDebugConfig --
2306 *
2307 * This is a debugging procedure that returns information about
2308 * one of the configuration tables that currently exists for an
2309 * interpreter.
2310 *
2311 * Results:
2312 * If the specified table exists in the given interpreter, then a
2313 * list is returned describing the table and any other tables that
2314 * it chains to: for each table there will be three list elements
2315 * giving the reference count for the table, the number of elements
2316 * in the table, and the command-line name for the first option
2317 * in the table. If the table doesn't exist in the interpreter
2318 * then an empty object is returned. The reference count for the
2319 * returned object is 0.
2320 *
2321 * Side effects:
2322 * None.
2323 *
2324 *----------------------------------------------------------------------
2325 */
2326
2327 Tcl_Obj *
2328 TkDebugConfig(interp, table)
2329 Tcl_Interp *interp; /* Interpreter in which the table is
2330 * defined. */
2331 Tk_OptionTable table; /* Table about which information is to
2332 * be returned. May not necessarily
2333 * exist in the interpreter anymore. */
2334 {
2335 OptionTable *tablePtr = (OptionTable *) table;
2336 Tcl_HashTable *hashTablePtr;
2337 Tcl_HashEntry *hashEntryPtr;
2338 Tcl_HashSearch search;
2339 Tcl_Obj *objPtr;
2340
2341 objPtr = Tcl_NewObj();
2342 hashTablePtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, OPTION_HASH_KEY,
2343 NULL);
2344 if (hashTablePtr == NULL) {
2345 return objPtr;
2346 }
2347
2348 /*
2349 * Scan all the tables for this interpreter to make sure that the
2350 * one we want still is valid.
2351 */
2352
2353 for (hashEntryPtr = Tcl_FirstHashEntry(hashTablePtr, &search);
2354 hashEntryPtr != NULL;
2355 hashEntryPtr = Tcl_NextHashEntry(&search)) {
2356 if (tablePtr == (OptionTable *) Tcl_GetHashValue(hashEntryPtr)) {
2357 for ( ; tablePtr != NULL; tablePtr = tablePtr->nextPtr) {
2358 Tcl_ListObjAppendElement((Tcl_Interp *) NULL, objPtr,
2359 Tcl_NewIntObj(tablePtr->refCount));
2360 Tcl_ListObjAppendElement((Tcl_Interp *) NULL, objPtr,
2361 Tcl_NewIntObj(tablePtr->numOptions));
2362 Tcl_ListObjAppendElement((Tcl_Interp *) NULL, objPtr,
2363 Tcl_NewStringObj(
2364 tablePtr->options[0].specPtr->optionName,
2365 -1));
2366 }
2367 break;
2368 }
2369 }
2370 return objPtr;
2371 }
2372
2373