Debian Codegraph @

pTk/mTk/generic/tkOption.c

raw symbols SCIP
1 /*
2 * tkOption.c --
3 *
4 * This module contains procedures to manage the option
5 * database, which allows various strings to be associated
6 * with windows either by name or by class or both.
7 *
8 * Copyright (c) 1990-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
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 * RCS: @(#) $Id: tkOption.c,v 1.15 2002/08/05 04:30:40 dgp Exp $
15 */
16
17 #include "tkPort.h"
18 #include "tkInt.h"
19
20 /*
21 * The option database is stored as one tree for each main window.
22 * Each name or class field in an option is associated with a node or
23 * leaf of the tree. For example, the options "x.y.z" and "x.y*a"
24 * each correspond to three nodes in the tree; they share the nodes
25 * "x" and "x.y", but have different leaf nodes. One of the following
26 * structures exists for each node or leaf in the option tree. It is
27 * actually stored as part of the parent node, and describes a particular
28 * child of the parent.
29 *
30 * The structure of the option db tree is a little confusing. There are
31 * four different kinds of nodes in the tree:
32 * interior class nodes
33 * interior name nodes
34 * leaf class nodes
35 * leaf name nodes
36 *
37 * All interior nodes refer to _window_ classes and names; all leaf nodes
38 * refer to _option_ classes and names. When looking for a particular option,
39 * therefore, you must compare interior node values to corresponding window
40 * values, and compare leaf node values to corresponding option values.
41 *
42 * The tree is actually stored in a collection of arrays; there is one each
43 * combination of WILDCARD/EXACT and CLASS/NAME and NODE/LEAF. The NODE arrays
44 * contain the interior nodes of the tree; each element has a pointer to an
45 * array of elements which are the leaves of the tree. The LEAF arrays, rather
46 * than holding the leaves of the tree, hold a cached subset of the option
47 * database, consisting of the values of all defined options for a single
48 * window, and some additional information about each ancestor of the window
49 * (since some options may be inherited from a parent), all the way back to the
50 * root window.
51 *
52 * Each time a call is made to Tk_GetOption, Tk will attempt to use the cached
53 * information to satisfy the lookup. If the call is for a window other than
54 * that for which options are currently cached, the portion of the cache that
55 * contains information for common ancestors of the two windows is retained and
56 * the remainder is discarded and rebuilt with new information for the new
57 * window.
58 */
59
60 typedef struct Element {
61 Tk_Uid nameUid; /* Name or class from one element of
62 * an option spec. */
63 union {
64 struct ElArray *arrayPtr; /* If this is an intermediate node,
65 * a pointer to a structure describing
66 * the remaining elements of all
67 * options whose prefixes are the
68 * same up through this element. */
69 Tk_Uid valueUid; /* For leaf nodes, this is the string
70 * value of the option. */
71 } child;
72 int priority; /* Used to select among matching
73 * options. Includes both the
74 * priority level and a serial #.
75 * Greater value means higher
76 * priority. Irrelevant except in
77 * leaf nodes. */
78 int flags; /* OR-ed combination of bits. See
79 * below for values. */
80 } Element;
81
82 /*
83 * Flags in Element structures:
84 *
85 * CLASS - Non-zero means this element refers to a class,
86 * Zero means this element refers to a name.
87 * NODE - Zero means this is a leaf element (the child
88 * field is a value, not a pointer to another node).
89 * One means this is a node element.
90 * WILDCARD - Non-zero means this there was a star in the
91 * original specification just before this element.
92 * Zero means there was a dot.
93 */
94
95 #define TYPE_MASK 0x7
96
97 #define CLASS 0x1
98 #define NODE 0x2
99 #define WILDCARD 0x4
100
101 #define EXACT_LEAF_NAME 0x0
102 #define EXACT_LEAF_CLASS 0x1
103 #define EXACT_NODE_NAME 0x2
104 #define EXACT_NODE_CLASS 0x3
105 #define WILDCARD_LEAF_NAME 0x4
106 #define WILDCARD_LEAF_CLASS 0x5
107 #define WILDCARD_NODE_NAME 0x6
108 #define WILDCARD_NODE_CLASS 0x7
109
110 /*
111 * The following structure is used to manage a dynamic array of
112 * Elements. These structures are used for two purposes: to store
113 * the contents of a node in the option tree, and for the option
114 * stacks described below.
115 */
116
117 typedef struct ElArray {
118 int arraySize; /* Number of elements actually
119 * allocated in the "els" array. */
120 int numUsed; /* Number of elements currently in
121 * use out of els. */
122 Element *nextToUse; /* Pointer to &els[numUsed]. */
123 Element els[1]; /* Array of structures describing
124 * children of this node. The
125 * array will actually contain enough
126 * elements for all of the children
127 * (and even a few extras, perhaps).
128 * This must be the last field in
129 * the structure. */
130 } ElArray;
131
132 #define EL_ARRAY_SIZE(numEls) ((unsigned) (sizeof(ElArray) \
133 + ((numEls)-1)*sizeof(Element)))
134 #define INITIAL_SIZE 5
135
136 /*
137 * In addition to the option tree, which is a relatively static structure,
138 * there are eight additional structures called "stacks", which are used
139 * to speed up queries into the option database. The stack structures
140 * are designed for the situation where an individual widget makes repeated
141 * requests for its particular options. The requests differ only in
142 * their last name/class, so during the first request we extract all
143 * the options pertaining to the particular widget and save them in a
144 * stack-like cache; subsequent requests for the same widget can search
145 * the cache relatively quickly. In fact, the cache is a hierarchical
146 * one, storing a list of relevant options for this widget and all of
147 * its ancestors up to the application root; hence the name "stack".
148 *
149 * Each of the eight stacks consists of an array of Elements, ordered in
150 * terms of levels in the window hierarchy. All the elements relevant
151 * for the top-level widget appear first in the array, followed by all
152 * those from the next-level widget on the path to the current widget,
153 * etc. down to those for the current widget.
154 *
155 * Cached information is divided into eight stacks according to the
156 * CLASS, NODE, and WILDCARD flags. Leaf and non-leaf information is
157 * kept separate to speed up individual probes (non-leaf information is
158 * only relevant when building the stacks, but isn't relevant when
159 * making probes; similarly, only non-leaf information is relevant
160 * when the stacks are being extended to the next widget down in the
161 * widget hierarchy). Wildcard elements are handled separately from
162 * "exact" elements because once they appear at a particular level in
163 * the stack they remain active for all deeper levels; exact elements
164 * are only relevant at a particular level. For example, when searching
165 * for options relevant in a particular window, the entire wildcard
166 * stacks get checked, but only the portions of the exact stacks that
167 * pertain to the window's parent. Lastly, name and class stacks are
168 * kept separate because different search keys are used when searching
169 * them; keeping them separate speeds up the searches.
170 */
171
172 #define NUM_STACKS 8
173
174 /*
175 * One of the following structures is used to keep track of each
176 * level in the stacks.
177 */
178
179 typedef struct StackLevel {
180 TkWindow *winPtr; /* Window corresponding to this stack
181 * level. */
182 int bases[NUM_STACKS]; /* For each stack, index of first
183 * element on stack corresponding to
184 * this level (used to restore "numUsed"
185 * fields when popping out of a level. */
186 } StackLevel;
187
188 typedef struct ThreadSpecificData {
189 int initialized; /* 0 means the ThreadSpecific Data structure
190 * for the current thread needs to be
191 * initialized. */
192 ElArray *stacks[NUM_STACKS];
193 TkWindow *cachedWindow;
194 /* Lowest-level window currently
195 * loaded in stacks at present.
196 * NULL means stacks have never
197 * been used, or have been
198 * invalidated because of a change
199 * to the database. */
200 /*
201 * Information about all of the stack levels that are currently
202 * active. This array grows dynamically to become as large as needed.
203 */
204
205 StackLevel *levels; /* Array describing current stack. */
206 int numLevels; /* Total space allocated. */
207 int curLevel; /* Highest level currently in use. Note:
208 * curLevel is never 0! (I don't remember
209 * why anymore...) */
210 /*
211 * The variable below is a serial number for all options entered into
212 * the database so far. It increments on each addition to the option
213 * database. It is used in computing option priorities, so that the
214 * most recent entry wins when choosing between options at the same
215 * priority level.
216 */
217
218 int serial;
219 Element defaultMatch; /* Special "no match" Element to use as
220 * default for searches.*/
221 } ThreadSpecificData;
222 static Tcl_ThreadDataKey dataKey;
223
224 /*
225 * Forward declarations for procedures defined in this file:
226 */
227
228 static int AddFromString _ANSI_ARGS_((Tcl_Interp *interp,
229 Tk_Window tkwin, char *string, int priority));
230 static void ClearOptionTree _ANSI_ARGS_((ElArray *arrayPtr));
231 static ElArray * ExtendArray _ANSI_ARGS_((ElArray *arrayPtr,
232 Element *elPtr));
233 static void ExtendStacks _ANSI_ARGS_((ElArray *arrayPtr,
234 int leaf));
235 static int GetDefaultOptions _ANSI_ARGS_((Tcl_Interp *interp,
236 TkWindow *winPtr));
237 static ElArray * NewArray _ANSI_ARGS_((int numEls));
238 static void OptionThreadExitProc _ANSI_ARGS_((
239 ClientData clientData));
240 static void OptionInit _ANSI_ARGS_((TkMainInfo *mainPtr));
241 static int ParsePriority _ANSI_ARGS_((Tcl_Interp *interp,
242 char *string));
243 static int ReadOptionFile _ANSI_ARGS_((Tcl_Interp *interp,
244 Tk_Window tkwin, char *fileName, int priority));
245 static void SetupStacks _ANSI_ARGS_((TkWindow *winPtr, int leaf));
246
247 /*
248 *--------------------------------------------------------------
249 *
250 * Tk_AddOption --
251 *
252 * Add a new option to the option database.
253 *
254 * Results:
255 * None.
256 *
257 * Side effects:
258 * Information is added to the option database.
259 *
260 *--------------------------------------------------------------
261 */
262
263 void
264 Tk_AddOption(tkwin, name, value, priority)
265 Tk_Window tkwin; /* Window token; option will be associated
266 * with main window for this window. */
267 CONST char *name; /* Multi-element name of option. */
268 CONST char *value; /* String value for option. */
269 int priority; /* Overall priority level to use for
270 * this option, such as TK_USER_DEFAULT_PRIO
271 * or TK_INTERACTIVE_PRIO. Must be between
272 * 0 and TK_MAX_PRIO. */
273 {
274 TkWindow *winPtr = ((TkWindow *) tkwin)->mainPtr->winPtr;
275 register ElArray **arrayPtrPtr;
276 register Element *elPtr;
277 Element newEl;
278 register CONST char *p;
279 CONST char *field;
280 int count, firstField, length;
281 #define TMP_SIZE 100
282 char tmp[TMP_SIZE+1];
283 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
284 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
285
286 if (winPtr->mainPtr->optionRootPtr == NULL) {
287 OptionInit(winPtr->mainPtr);
288 }
289 tsdPtr->cachedWindow = NULL; /* Invalidate the cache. */
290
291 /*
292 * Compute the priority for the new element, including both the
293 * overall level and the serial number (to disambiguate with the
294 * level).
295 */
296
297 if (priority < 0) {
298 priority = 0;
299 } else if (priority > TK_MAX_PRIO) {
300 priority = TK_MAX_PRIO;
301 }
302 newEl.priority = (priority << 24) + tsdPtr->serial;
303 tsdPtr->serial++;
304
305 /*
306 * Parse the option one field at a time.
307 */
308
309 arrayPtrPtr = &(((TkWindow *) tkwin)->mainPtr->optionRootPtr);
310 p = name;
311 for (firstField = 1; ; firstField = 0) {
312
313 /*
314 * Scan the next field from the name and convert it to a Tk_Uid.
315 * Must copy the field before calling Tk_Uid, so that a terminating
316 * NULL may be added without modifying the source string.
317 */
318
319 if (*p == '*') {
320 newEl.flags = WILDCARD;
321 p++;
322 } else {
323 newEl.flags = 0;
324 }
325 field = p;
326 while ((*p != 0) && (*p != '.') && (*p != '*')) {
327 p++;
328 }
329 length = p - field;
330 if (length > TMP_SIZE) {
331 length = TMP_SIZE;
332 }
333 strncpy(tmp, field, (size_t) length);
334 tmp[length] = 0;
335 newEl.nameUid = Tk_GetUid(tmp);
336 if (isupper(UCHAR(*field))) {
337 newEl.flags |= CLASS;
338 }
339
340 if (*p != 0) {
341
342 /*
343 * New element will be a node. If this option can't possibly
344 * apply to this main window, then just skip it. Otherwise,
345 * add it to the parent, if it isn't already there, and descend
346 * into it.
347 */
348
349 newEl.flags |= NODE;
350 if (firstField && !(newEl.flags & WILDCARD)
351 && (newEl.nameUid != winPtr->nameUid)
352 && (newEl.nameUid != winPtr->classUid)) {
353 return;
354 }
355 for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
356 ; elPtr++, count--) {
357 if (count == 0) {
358 newEl.child.arrayPtr = NewArray(5);
359 *arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
360 arrayPtrPtr = &((*arrayPtrPtr)->nextToUse[-1].child.arrayPtr);
361 break;
362 }
363 if ((elPtr->nameUid == newEl.nameUid)
364 && (elPtr->flags == newEl.flags)) {
365 arrayPtrPtr = &(elPtr->child.arrayPtr);
366 break;
367 }
368 }
369 if (*p == '.') {
370 p++;
371 }
372 } else {
373
374 /*
375 * New element is a leaf. Add it to the parent, if it isn't
376 * already there. If it exists already, keep whichever value
377 * has highest priority.
378 */
379
380 newEl.child.valueUid = Tk_GetUid(value);
381 for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
382 ; elPtr++, count--) {
383 if (count == 0) {
384 *arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
385 return;
386 }
387 if ((elPtr->nameUid == newEl.nameUid)
388 && (elPtr->flags == newEl.flags)) {
389 if (elPtr->priority < newEl.priority) {
390 elPtr->priority = newEl.priority;
391 elPtr->child.valueUid = newEl.child.valueUid;
392 }
393 return;
394 }
395 }
396 }
397 }
398 }
399
400 /*
401 *--------------------------------------------------------------
402 *
403 * Tk_GetOption --
404 *
405 * Retrieve an option from the option database.
406 *
407 * Results:
408 * The return value is the value specified in the option
409 * database for the given name and class on the given
410 * window. If there is nothing specified in the database
411 * for that option, then NULL is returned.
412 *
413 * Side effects:
414 * The internal caches used to speed up option mapping
415 * may be modified, if this tkwin is different from the
416 * last tkwin used for option retrieval.
417 *
418 *--------------------------------------------------------------
419 */
420
421 Tk_Uid
422 Tk_GetOption(tkwin, name, className)
423 Tk_Window tkwin; /* Token for window that option is
424 * associated with. */
425 CONST char *name; /* Name of option. */
426 CONST char *className; /* Class of option. NULL means there
427 * is no class for this option: just
428 * check for name. */
429 {
430 Tk_Uid nameId, classId = NULL;
431 char *masqName;
432 register Element *elPtr, *bestPtr;
433 register int count;
434 StackLevel *levelPtr;
435 int stackDepth[NUM_STACKS];
436 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
437 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
438
439 /*
440 * Note: no need to call OptionInit here: it will be done by
441 * the SetupStacks call below (squeeze out those nanoseconds).
442 */
443
444 if (tkwin != (Tk_Window) tsdPtr->cachedWindow) {
445 SetupStacks((TkWindow *) tkwin, 1);
446 }
447
448 /*
449 * Get a default "best" match.
450 */
451
452 bestPtr = &tsdPtr->defaultMatch;
453
454 /*
455 * For megawidget support, we want to have some widget options masquerade
456 * as options for other widgets. For example, a combobox has a button in
457 * it; this button ought to pick up the *Button.background, etc., options.
458 * But because the class of the widget is Combobox, our normal search
459 * won't get that option.
460 *
461 * To work around this, the option name field syntax was extended to allow
462 * for a "." in the name; if this character occurs in the name, then it
463 * indicates that this name contains a new window class and an option name,
464 * ie, "Button.foreground". If we see this form in the name field, we
465 * query the option database directly (since the option stacks will not
466 * have the information we need).
467 */
468
469 masqName = strchr(name, (int)'.');
470 if (masqName != NULL) {
471 /*
472 * This option is masquerading with a different window class.
473 * Search the stack to the depth it was before the current window's
474 * information was pushed (the value for which is stored in the bases
475 * field).
476 */
477 levelPtr = &tsdPtr->levels[tsdPtr->curLevel];
478 nameId = Tk_GetUid(masqName+1);
479 for (count = 0; count < NUM_STACKS; count++) {
480 stackDepth[count] = levelPtr->bases[count];
481 }
482 } else {
483 /*
484 * No option masquerading here. Just use the current level to get the
485 * stack depths.
486 */
487 nameId = Tk_GetUid(name);
488 for (count = 0; count < NUM_STACKS; count++) {
489 stackDepth[count] = tsdPtr->stacks[count]->numUsed;
490 }
491 }
492
493 /*
494 * Probe the stacks for matches.
495 */
496
497 for (elPtr = tsdPtr->stacks[EXACT_LEAF_NAME]->els,
498 count = stackDepth[EXACT_LEAF_NAME]; count > 0;
499 elPtr++, count--) {
500 if ((elPtr->nameUid == nameId)
501 && (elPtr->priority > bestPtr->priority)) {
502 bestPtr = elPtr;
503 }
504 }
505 for (elPtr = tsdPtr->stacks[WILDCARD_LEAF_NAME]->els,
506 count = stackDepth[WILDCARD_LEAF_NAME]; count > 0;
507 elPtr++, count--) {
508 if ((elPtr->nameUid == nameId)
509 && (elPtr->priority > bestPtr->priority)) {
510 bestPtr = elPtr;
511 }
512 }
513
514 if (className != NULL) {
515 classId = Tk_GetUid(className);
516 for (elPtr = tsdPtr->stacks[EXACT_LEAF_CLASS]->els,
517 count = stackDepth[EXACT_LEAF_CLASS]; count > 0;
518 elPtr++, count--) {
519 if ((elPtr->nameUid == classId)
520 && (elPtr->priority > bestPtr->priority)) {
521 bestPtr = elPtr;
522 }
523 }
524 for (elPtr = tsdPtr->stacks[WILDCARD_LEAF_CLASS]->els,
525 count = stackDepth[WILDCARD_LEAF_CLASS]; count > 0;
526 elPtr++, count--) {
527 if ((elPtr->nameUid == classId)
528 && (elPtr->priority > bestPtr->priority)) {
529 bestPtr = elPtr;
530 }
531 }
532 }
533
534 /*
535 * If this option was masquerading with a different window class,
536 * probe the option database now. Note that this will be inefficient
537 * if the option database is densely populated, or if the widget has many
538 * masquerading options.
539 */
540
541 if (masqName != NULL) {
542 char *masqClass;
543 Tk_Uid nodeId, winClassId, winNameId;
544 unsigned int classNameLength;
545 register Element *nodePtr, *leafPtr;
546 static int searchOrder[] = { EXACT_NODE_NAME,
547 WILDCARD_NODE_NAME,
548 EXACT_NODE_CLASS,
549 WILDCARD_NODE_CLASS,
550 -1 };
551 int *currentPtr, currentStack, leafCount;
552
553 /*
554 * Extract the masquerade class name from the name field.
555 */
556
557 classNameLength = (unsigned int)(masqName - name);
558 masqClass = (char *)ckalloc(classNameLength + 1);
559 strncpy(masqClass, name, classNameLength);
560 masqClass[classNameLength] = '\0';
561
562 winClassId = Tk_GetUid(masqClass);
563 ckfree(masqClass);
564 winNameId = ((TkWindow *)tkwin)->nameUid;
565
566 levelPtr = &tsdPtr->levels[tsdPtr->curLevel];
567
568 for (currentPtr = searchOrder; *currentPtr != -1; currentPtr++) {
569 currentStack = *currentPtr;
570 nodePtr = tsdPtr->stacks[currentStack]->els;
571 count = levelPtr->bases[currentStack];
572
573 /*
574 * For wildcard stacks, check all entries; for non-wildcard
575 * stacks, only check things that matched in the parent.
576 */
577
578 if (!(currentStack & WILDCARD)) {
579 nodePtr += levelPtr[-1].bases[currentStack];
580 count -= levelPtr[-1].bases[currentStack];
581 }
582
583 if (currentStack && CLASS) {
584 nodeId = winClassId;
585 } else {
586 nodeId = winNameId;
587 }
588
589 for ( ; count > 0; nodePtr++, count--) {
590 if (nodePtr->nameUid == nodeId) {
591 leafPtr = nodePtr->child.arrayPtr->els;
592 leafCount = nodePtr->child.arrayPtr->numUsed;
593 for ( ; leafCount > 0; leafPtr++, leafCount--) {
594 if (leafPtr->flags & CLASS && className != NULL) {
595 if (leafPtr->nameUid == classId &&
596 leafPtr->priority > bestPtr->priority) {
597 bestPtr = leafPtr;
598 }
599 } else {
600 if (leafPtr->nameUid == nameId &&
601 leafPtr->priority > bestPtr->priority) {
602 bestPtr = leafPtr;
603 }
604 }
605 }
606 }
607 }
608 }
609 }
610
611 return bestPtr->child.valueUid;
612 }
613
614 /*
615 *--------------------------------------------------------------
616 *
617 * Tk_OptionObjCmd --
618 *
619 * This procedure is invoked to process the "option" Tcl command.
620 * See the user documentation for details on what it does.
621 *
622 * Results:
623 * A standard Tcl result.
624 *
625 * Side effects:
626 * See the user documentation.
627 *
628 *--------------------------------------------------------------
629 */
630
631 int
632 Tk_OptionObjCmd(clientData, interp, objc, objv)
633 ClientData clientData; /* Main window associated with
634 * interpreter. */
635 Tcl_Interp *interp; /* Current interpreter. */
636 int objc; /* Number of Tcl_Obj arguments. */
637 Tcl_Obj *CONST objv[]; /* Tcl_Obj arguments. */
638 {
639 Tk_Window tkwin = (Tk_Window) clientData;
640 int index, result;
641 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
642 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
643
644 static CONST char *optionCmds[] = {
645 "add", "clear", "get", "readfile", NULL
646 };
647
648 enum optionVals {
649 OPTION_ADD, OPTION_CLEAR, OPTION_GET, OPTION_READFILE
650 };
651
652 if (objc < 2) {
653 Tcl_WrongNumArgs(interp, 1, objv, "cmd arg ?arg ...?");
654 return TCL_ERROR;
655 }
656
657 result = Tcl_GetIndexFromObj(interp, objv[1], optionCmds, "option", 0,
658 &index);
659 if (result != TCL_OK) {
660 return result;
661 }
662
663 result = TCL_OK;
664 switch ((enum optionVals) index) {
665 case OPTION_ADD: {
666 int priority;
667 if ((objc != 4) && (objc != 5)) {
668 Tcl_WrongNumArgs(interp, 2, objv, "pattern value ?priority?");
669 return TCL_ERROR;
670 }
671
672 if (objc == 4) {
673 priority = TK_INTERACTIVE_PRIO;
674 } else {
675 priority = ParsePriority(interp, Tcl_GetString(objv[4]));
676 if (priority < 0) {
677 return TCL_ERROR;
678 }
679 }
680 Tk_AddOption(tkwin, Tcl_GetString(objv[2]),
681 Tcl_GetString(objv[3]), priority);
682 break;
683 }
684
685 case OPTION_CLEAR: {
686 TkMainInfo *mainPtr;
687
688 if (objc != 2) {
689 Tcl_WrongNumArgs(interp, 2, objv, "");
690 return TCL_ERROR;
691 }
692 mainPtr = ((TkWindow *) tkwin)->mainPtr;
693 if (mainPtr->optionRootPtr != NULL) {
694 ClearOptionTree(mainPtr->optionRootPtr);
695 mainPtr->optionRootPtr = NULL;
696 }
697 tsdPtr->cachedWindow = NULL;
698 break;
699 }
700
701 case OPTION_GET: {
702 Tk_Window window;
703 Tk_Uid value;
704
705 if (objc != 5) {
706 Tcl_WrongNumArgs(interp, 2, objv, "window name class");
707 return TCL_ERROR;
708 }
709 window = Tk_NameToWindow(interp, Tcl_GetString(objv[2]), tkwin);
710 if (window == NULL) {
711 return TCL_ERROR;
712 }
713 value = Tk_GetOption(window, Tcl_GetString(objv[3]),
714 Tcl_GetString(objv[4]));
715 if (value != NULL) {
716 Tcl_SetResult(interp, (char *)value, TCL_STATIC);
717 }
718 break;
719 }
720
721 case OPTION_READFILE: {
722 int priority;
723
724 if ((objc != 3) && (objc != 4)) {
725 Tcl_WrongNumArgs(interp, 2, objv, "fileName ?priority?");
726 return TCL_ERROR;
727 }
728
729 if (objc == 4) {
730 priority = ParsePriority(interp, Tcl_GetString(objv[3]));
731 if (priority < 0) {
732 return TCL_ERROR;
733 }
734 } else {
735 priority = TK_INTERACTIVE_PRIO;
736 }
737 result = ReadOptionFile(interp, tkwin, Tcl_GetString(objv[2]),
738 priority);
739 break;
740 }
741 }
742 return result;
743 }
744
745 /*
746 *--------------------------------------------------------------
747 *
748 * TkOptionDeadWindow --
749 *
750 * This procedure is called whenever a window is deleted.
751 * It cleans up any option-related stuff associated with
752 * the window.
753 *
754 * Results:
755 * None.
756 *
757 * Side effects:
758 * Option-related resources are freed. See code below
759 * for details.
760 *
761 *--------------------------------------------------------------
762 */
763
764 void
765 TkOptionDeadWindow(winPtr)
766 register TkWindow *winPtr; /* Window to be cleaned up. */
767 {
768 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
769 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
770
771 /*
772 * If this window is in the option stacks, then clear the stacks.
773 */
774
775 if (winPtr->optionLevel != -1) {
776 int i;
777
778 for (i = 1; i <= tsdPtr->curLevel; i++) {
779 tsdPtr->levels[i].winPtr->optionLevel = -1;
780 }
781 tsdPtr->curLevel = -1;
782 tsdPtr->cachedWindow = NULL;
783 }
784
785 /*
786 * If this window was a main window, then delete its option
787 * database.
788 */
789
790 if ((winPtr->mainPtr != NULL) && (winPtr->mainPtr->winPtr == winPtr)
791 && (winPtr->mainPtr->optionRootPtr != NULL)) {
792 ClearOptionTree(winPtr->mainPtr->optionRootPtr);
793 winPtr->mainPtr->optionRootPtr = NULL;
794 }
795 }
796
797 /*
798 *----------------------------------------------------------------------
799 *
800 * TkOptionClassChanged --
801 *
802 * This procedure is invoked when a window's class changes. If
803 * the window is on the option cache, this procedure flushes
804 * any information for the window, since the new class could change
805 * what is relevant.
806 *
807 * Results:
808 * None.
809 *
810 * Side effects:
811 * The option cache may be flushed in part or in whole.
812 *
813 *----------------------------------------------------------------------
814 */
815
816 void
817 TkOptionClassChanged(winPtr)
818 TkWindow *winPtr; /* Window whose class changed. */
819 {
820 int i, j, *basePtr;
821 ElArray *arrayPtr;
822 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
823 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
824
825 if (winPtr->optionLevel == -1) {
826 return;
827 }
828
829 /*
830 * Find the lowest stack level that refers to this window, then
831 * flush all of the levels above the matching one.
832 */
833
834 for (i = 1; i <= tsdPtr->curLevel; i++) {
835 if (tsdPtr->levels[i].winPtr == winPtr) {
836 for (j = i; j <= tsdPtr->curLevel; j++) {
837 tsdPtr->levels[j].winPtr->optionLevel = -1;
838 }
839 tsdPtr->curLevel = i-1;
840 basePtr = tsdPtr->levels[i].bases;
841 for (j = 0; j < NUM_STACKS; j++) {
842 arrayPtr = tsdPtr->stacks[j];
843 arrayPtr->numUsed = basePtr[j];
844 arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
845 }
846 if (tsdPtr->curLevel <= 0) {
847 tsdPtr->cachedWindow = NULL;
848 } else {
849 tsdPtr->cachedWindow = tsdPtr->levels[tsdPtr->curLevel].winPtr;
850 }
851 break;
852 }
853 }
854 }
855
856 /*
857 *----------------------------------------------------------------------
858 *
859 * ParsePriority --
860 *
861 * Parse a string priority value.
862 *
863 * Results:
864 * The return value is the integer priority level corresponding
865 * to string, or -1 if string doesn't point to a valid priority level.
866 * In this case, an error message is left in the interp's result.
867 *
868 * Side effects:
869 * None.
870 *
871 *----------------------------------------------------------------------
872 */
873
874 static int
875 ParsePriority(interp, string)
876 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
877 char *string; /* Describes a priority level, either
878 * symbolically or numerically. */
879 {
880 int priority, c;
881 size_t length;
882
883 c = string[0];
884 length = strlen(string);
885 if ((c == 'w')
886 && (strncmp(string, "widgetDefault", length) == 0)) {
887 return TK_WIDGET_DEFAULT_PRIO;
888 } else if ((c == 's')
889 && (strncmp(string, "startupFile", length) == 0)) {
890 return TK_STARTUP_FILE_PRIO;
891 } else if ((c == 'u')
892 && (strncmp(string, "userDefault", length) == 0)) {
893 return TK_USER_DEFAULT_PRIO;
894 } else if ((c == 'i')
895 && (strncmp(string, "interactive", length) == 0)) {
896 return TK_INTERACTIVE_PRIO;
897 } else {
898 char *end;
899
900 priority = strtoul(string, &end, 0);
901 if ((end == string) || (*end != 0) || (priority < 0)
902 || (priority > 100)) {
903 Tcl_AppendResult(interp, "bad priority level \"", string,
904 "\": must be widgetDefault, startupFile, userDefault, ",
905 "interactive, or a number between 0 and 100",
906 (char *) NULL);
907 return -1;
908 }
909 }
910 return priority;
911 }
912
913 /*
914 *----------------------------------------------------------------------
915 *
916 * AddFromString --
917 *
918 * Given a string containing lines in the standard format for
919 * X resources (see other documentation for details on what this
920 * is), parse the resource specifications and enter them as options
921 * for tkwin's main window.
922 *
923 * Results:
924 * The return value is a standard Tcl return code. In the case of
925 * an error in parsing string, TCL_ERROR will be returned and an
926 * error message will be left in the interp's result. The memory at
927 * string is totally trashed by this procedure. If you care about
928 * its contents, make a copy before calling here.
929 *
930 * Side effects:
931 * None.
932 *
933 *----------------------------------------------------------------------
934 */
935
936 static int
937 AddFromString(interp, tkwin, string, priority)
938 Tcl_Interp *interp; /* Interpreter to use for reporting results. */
939 Tk_Window tkwin; /* Token for window: options are entered
940 * for this window's main window. */
941 char *string; /* String containing option specifiers. */
942 int priority; /* Priority level to use for options in
943 * this string, such as TK_USER_DEFAULT_PRIO
944 * or TK_INTERACTIVE_PRIO. Must be between
945 * 0 and TK_MAX_PRIO. */
946 {
947 register char *src, *dst;
948 char *name, *value;
949 int lineNum;
950
951 src = string;
952 lineNum = 1;
953 while (1) {
954
955 /*
956 * Skip leading white space and empty lines and comment lines, and
957 * check for the end of the spec.
958 */
959
960 while ((*src == ' ') || (*src == '\t')) {
961 src++;
962 }
963 if ((*src == '#') || (*src == '!')) {
964 do {
965 src++;
966 if ((src[0] == '\\') && (src[1] == '\n')) {
967 src += 2;
968 lineNum++;
969 }
970 } while ((*src != '\n') && (*src != 0));
971 }
972 if (*src == '\n') {
973 src++;
974 lineNum++;
975 continue;
976 }
977 if (*src == '\0') {
978 break;
979 }
980
981 /*
982 * Parse off the option name, collapsing out backslash-newline
983 * sequences of course.
984 */
985
986 dst = name = src;
987 while (*src != ':') {
988 if ((*src == '\0') || (*src == '\n')) {
989 char buf[32 + TCL_INTEGER_SPACE];
990
991 sprintf(buf, "missing colon on line %d", lineNum);
992 Tcl_SetResult(interp, buf, TCL_VOLATILE);
993 return TCL_ERROR;
994 }
995 if ((src[0] == '\\') && (src[1] == '\n')) {
996 src += 2;
997 lineNum++;
998 } else {
999 *dst = *src;
1000 dst++;
1001 src++;
1002 }
1003 }
1004
1005 /*
1006 * Eliminate trailing white space on the name, and null-terminate
1007 * it.
1008 */
1009
1010 while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
1011 dst--;
1012 }
1013 *dst = '\0';
1014
1015 /*
1016 * Skip white space between the name and the value.
1017 */
1018
1019 src++;
1020 while ((*src == ' ') || (*src == '\t')) {
1021 src++;
1022 }
1023 if (*src == '\0') {
1024 char buf[32 + TCL_INTEGER_SPACE];
1025
1026 sprintf(buf, "missing value on line %d", lineNum);
1027 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1028 return TCL_ERROR;
1029 }
1030
1031 /*
1032 * Parse off the value, squeezing out backslash-newline sequences
1033 * along the way.
1034 */
1035
1036 dst = value = src;
1037 while (*src != '\n') {
1038 if (*src == '\0') {
1039 char buf[32 + TCL_INTEGER_SPACE];
1040
1041 sprintf(buf, "missing newline on line %d", lineNum);
1042 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1043 return TCL_ERROR;
1044 }
1045 if ((src[0] == '\\') && (src[1] == '\n')) {
1046 src += 2;
1047 lineNum++;
1048 } else {
1049 *dst = *src;
1050 dst++;
1051 src++;
1052 }
1053 }
1054 *dst = 0;
1055
1056 /*
1057 * Enter the option into the database.
1058 */
1059
1060 Tk_AddOption(tkwin, name, value, priority);
1061 src++;
1062 lineNum++;
1063 }
1064 return TCL_OK;
1065 }
1066
1067 /*
1068 *----------------------------------------------------------------------
1069 *
1070 * ReadOptionFile --
1071 *
1072 * Read a file of options ("resources" in the old X terminology)
1073 * and load them into the option database.
1074 *
1075 * Results:
1076 * The return value is a standard Tcl return code. In the case of
1077 * an error in parsing string, TCL_ERROR will be returned and an
1078 * error message will be left in the interp's result.
1079 *
1080 * Side effects:
1081 * None.
1082 *
1083 *----------------------------------------------------------------------
1084 */
1085
1086 static int
1087 ReadOptionFile(interp, tkwin, fileName, priority)
1088 Tcl_Interp *interp; /* Interpreter to use for reporting results. */
1089 Tk_Window tkwin; /* Token for window: options are entered
1090 * for this window's main window. */
1091 char *fileName; /* Name of file containing options. */
1092 int priority; /* Priority level to use for options in
1093 * this file, such as TK_USER_DEFAULT_PRIO
1094 * or TK_INTERACTIVE_PRIO. Must be between
1095 * 0 and TK_MAX_PRIO. */
1096 {
1097 CONST char *realName;
1098 char *buffer;
1099 int result, bufferSize;
1100 Tcl_Channel chan;
1101 Tcl_DString newName;
1102
1103 /*
1104 * Prevent file system access in a safe interpreter.
1105 */
1106
1107 if (Tcl_IsSafe(interp)) {
1108 Tcl_AppendResult(interp, "can't read options from a file in a",
1109 " safe interpreter", (char *) NULL);
1110 return TCL_ERROR;
1111 }
1112
1113 realName = Tcl_TranslateFileName(interp, fileName, &newName);
1114 if (realName == NULL) {
1115 return TCL_ERROR;
1116 }
1117 chan = Tcl_OpenFileChannel(interp, realName, "r", 0);
1118 Tcl_DStringFree(&newName);
1119 if (chan == NULL) {
1120 Tcl_ResetResult(interp);
1121 Tcl_AppendResult(interp, "couldn't open \"", fileName,
1122 "\": ", Tcl_PosixError(interp), (char *) NULL);
1123 return TCL_ERROR;
1124 }
1125
1126 /*
1127 * Compute size of file by seeking to the end of the file. This will
1128 * overallocate if we are performing CRLF translation.
1129 */
1130
1131 bufferSize = (int) Tcl_Seek(chan, (Tcl_WideInt) 0, SEEK_END);
1132 (void) Tcl_Seek(chan, (Tcl_WideInt) 0, SEEK_SET);
1133
1134 if (bufferSize < 0) {
1135 Tcl_AppendResult(interp, "error seeking to end of file \"",
1136 fileName, "\":", Tcl_PosixError(interp), (char *) NULL);
1137 Tcl_Close(NULL, chan);
1138 return TCL_ERROR;
1139
1140 }
1141 buffer = (char *) ckalloc((unsigned) bufferSize+1);
1142 bufferSize = Tcl_Read(chan, buffer, bufferSize);
1143 if (bufferSize < 0) {
1144 Tcl_AppendResult(interp, "error reading file \"", fileName, "\":",
1145 Tcl_PosixError(interp), (char *) NULL);
1146 Tcl_Close(NULL, chan);
1147 return TCL_ERROR;
1148 }
1149 Tcl_Close(NULL, chan);
1150 buffer[bufferSize] = 0;
1151 result = AddFromString(interp, tkwin, buffer, priority);
1152 ckfree(buffer);
1153 return result;
1154 }
1155
1156 /*
1157 *--------------------------------------------------------------
1158 *
1159 * NewArray --
1160 *
1161 * Create a new ElArray structure of a given size.
1162 *
1163 * Results:
1164 * The return value is a pointer to a properly initialized
1165 * element array with "numEls" space. The array is marked
1166 * as having no active elements.
1167 *
1168 * Side effects:
1169 * Memory is allocated.
1170 *
1171 *--------------------------------------------------------------
1172 */
1173
1174 static ElArray *
1175 NewArray(numEls)
1176 int numEls; /* How many elements of space to allocate. */
1177 {
1178 register ElArray *arrayPtr;
1179
1180 arrayPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(numEls));
1181 arrayPtr->arraySize = numEls;
1182 arrayPtr->numUsed = 0;
1183 arrayPtr->nextToUse = arrayPtr->els;
1184 return arrayPtr;
1185 }
1186
1187 /*
1188 *--------------------------------------------------------------
1189 *
1190 * ExtendArray --
1191 *
1192 * Add a new element to an array, extending the array if
1193 * necessary.
1194 *
1195 * Results:
1196 * The return value is a pointer to the new array, which
1197 * will be different from arrayPtr if the array got expanded.
1198 *
1199 * Side effects:
1200 * Memory may be allocated or freed.
1201 *
1202 *--------------------------------------------------------------
1203 */
1204
1205 static ElArray *
1206 ExtendArray(arrayPtr, elPtr)
1207 register ElArray *arrayPtr; /* Array to be extended. */
1208 register Element *elPtr; /* Element to be copied into array. */
1209 {
1210 /*
1211 * If the current array has filled up, make it bigger.
1212 */
1213
1214 if (arrayPtr->numUsed >= arrayPtr->arraySize) {
1215 register ElArray *newPtr;
1216
1217 newPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(2*arrayPtr->arraySize));
1218 newPtr->arraySize = 2*arrayPtr->arraySize;
1219 newPtr->numUsed = arrayPtr->numUsed;
1220 newPtr->nextToUse = &newPtr->els[newPtr->numUsed];
1221 memcpy((VOID *) newPtr->els, (VOID *) arrayPtr->els,
1222 (arrayPtr->arraySize*sizeof(Element)));
1223 ckfree((char *) arrayPtr);
1224 arrayPtr = newPtr;
1225 }
1226
1227 *arrayPtr->nextToUse = *elPtr;
1228 arrayPtr->nextToUse++;
1229 arrayPtr->numUsed++;
1230 return arrayPtr;
1231 }
1232
1233 /*
1234 *--------------------------------------------------------------
1235 *
1236 * SetupStacks --
1237 *
1238 * Arrange the stacks so that they cache all the option
1239 * information for a particular window.
1240 *
1241 * Results:
1242 * None.
1243 *
1244 * Side effects:
1245 * The stacks are modified to hold information for tkwin
1246 * and all its ancestors in the window hierarchy.
1247 *
1248 *--------------------------------------------------------------
1249 */
1250
1251 static void
1252 SetupStacks(winPtr, leaf)
1253 TkWindow *winPtr; /* Window for which information is to
1254 * be cached. */
1255 int leaf; /* Non-zero means this is the leaf
1256 * window being probed. Zero means this
1257 * is an ancestor of the desired leaf. */
1258 {
1259 int level, i, *iPtr;
1260 register StackLevel *levelPtr;
1261 register ElArray *arrayPtr;
1262 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1263 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1264
1265 /*
1266 * The following array defines the order in which the current
1267 * stacks are searched to find matching entries to add to the
1268 * stacks. Given the current priority-based scheme, the order
1269 * below is no longer relevant; all that matters is that an
1270 * element is on the list *somewhere*. The ordering is a relic
1271 * of the old days when priorities were determined differently.
1272 */
1273
1274 static int searchOrder[] = {WILDCARD_NODE_CLASS, WILDCARD_NODE_NAME,
1275 EXACT_NODE_CLASS, EXACT_NODE_NAME, -1};
1276
1277 if (winPtr->mainPtr->optionRootPtr == NULL) {
1278 OptionInit(winPtr->mainPtr);
1279 }
1280
1281 /*
1282 * Step 1: make sure that options are cached for this window's
1283 * parent.
1284 */
1285
1286 if (winPtr->parentPtr != NULL) {
1287 level = winPtr->parentPtr->optionLevel;
1288 if ((level == -1) || (tsdPtr->cachedWindow == NULL)) {
1289 SetupStacks(winPtr->parentPtr, 0);
1290 level = winPtr->parentPtr->optionLevel;
1291 }
1292 level++;
1293 } else {
1294 level = 1;
1295 }
1296
1297 /*
1298 * Step 2: pop extra unneeded information off the stacks and
1299 * mark those windows as no longer having cached information.
1300 */
1301
1302 if (tsdPtr->curLevel >= level) {
1303 while (tsdPtr->curLevel >= level) {
1304 tsdPtr->levels[tsdPtr->curLevel].winPtr->optionLevel = -1;
1305 tsdPtr->curLevel--;
1306 }
1307 levelPtr = &tsdPtr->levels[level];
1308 for (i = 0; i < NUM_STACKS; i++) {
1309 arrayPtr = tsdPtr->stacks[i];
1310 arrayPtr->numUsed = levelPtr->bases[i];
1311 arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
1312 }
1313 }
1314 tsdPtr->curLevel = winPtr->optionLevel = level;
1315
1316 /*
1317 * Step 3: if the root database information isn't loaded or
1318 * isn't valid, initialize level 0 of the stack from the
1319 * database root (this only happens if winPtr is a main window).
1320 */
1321
1322 if ((tsdPtr->curLevel == 1)
1323 && ((tsdPtr->cachedWindow == NULL)
1324 || (tsdPtr->cachedWindow->mainPtr != winPtr->mainPtr))) {
1325 for (i = 0; i < NUM_STACKS; i++) {
1326 arrayPtr = tsdPtr->stacks[i];
1327 arrayPtr->numUsed = 0;
1328 arrayPtr->nextToUse = arrayPtr->els;
1329 }
1330 ExtendStacks(winPtr->mainPtr->optionRootPtr, 0);
1331 }
1332
1333 /*
1334 * Step 4: create a new stack level; grow the level array if
1335 * we've run out of levels. Clear the stacks for EXACT_LEAF_NAME
1336 * and EXACT_LEAF_CLASS (anything that was there is of no use
1337 * any more).
1338 */
1339
1340 if (tsdPtr->curLevel >= tsdPtr->numLevels) {
1341 StackLevel *newLevels;
1342
1343 newLevels = (StackLevel *) ckalloc((unsigned)
1344 (tsdPtr->numLevels*2*sizeof(StackLevel)));
1345 memcpy((VOID *) newLevels, (VOID *) tsdPtr->levels,
1346 (tsdPtr->numLevels*sizeof(StackLevel)));
1347 ckfree((char *) tsdPtr->levels);
1348 tsdPtr->numLevels *= 2;
1349 tsdPtr->levels = newLevels;
1350 }
1351 levelPtr = &tsdPtr->levels[tsdPtr->curLevel];
1352 levelPtr->winPtr = winPtr;
1353 arrayPtr = tsdPtr->stacks[EXACT_LEAF_NAME];
1354 arrayPtr->numUsed = 0;
1355 arrayPtr->nextToUse = arrayPtr->els;
1356 arrayPtr = tsdPtr->stacks[EXACT_LEAF_CLASS];
1357 arrayPtr->numUsed = 0;
1358 arrayPtr->nextToUse = arrayPtr->els;
1359 for (i = 0; i < NUM_STACKS; i++) {
1360 levelPtr->bases[i] = tsdPtr->stacks[i]->numUsed;
1361 }
1362 /*
1363 * Step 5: scan the current stack level looking for matches to this
1364 * window's name or class; where found, add new information to the
1365 * stacks.
1366 */
1367
1368 for (iPtr = searchOrder; *iPtr != -1; iPtr++) {
1369 register Element *elPtr;
1370 int count;
1371 Tk_Uid id;
1372
1373 i = *iPtr;
1374 if (i & CLASS) {
1375 id = winPtr->classUid;
1376 } else {
1377 id = winPtr->nameUid;
1378 }
1379 elPtr = tsdPtr->stacks[i]->els;
1380 count = levelPtr->bases[i];
1381
1382 /*
1383 * For wildcard stacks, check all entries; for non-wildcard
1384 * stacks, only check things that matched in the parent.
1385 */
1386
1387 if (!(i & WILDCARD)) {
1388 elPtr += levelPtr[-1].bases[i];
1389 count -= levelPtr[-1].bases[i];
1390 }
1391 for ( ; count > 0; elPtr++, count--) {
1392 if (elPtr->nameUid != id) {
1393 continue;
1394 }
1395 ExtendStacks(elPtr->child.arrayPtr, leaf);
1396 }
1397 }
1398 tsdPtr->cachedWindow = winPtr;
1399 }
1400
1401 /*
1402 *--------------------------------------------------------------
1403 *
1404 * ExtendStacks --
1405 *
1406 * Given an element array, copy all the elements from the
1407 * array onto the system stacks (except for irrelevant leaf
1408 * elements).
1409 *
1410 * Results:
1411 * None.
1412 *
1413 * Side effects:
1414 * The option stacks are extended.
1415 *
1416 *--------------------------------------------------------------
1417 */
1418
1419 static void
1420 ExtendStacks(arrayPtr, leaf)
1421 ElArray *arrayPtr; /* Array of elements to copy onto stacks. */
1422 int leaf; /* If zero, then don't copy exact leaf
1423 * elements. */
1424 {
1425 register int count;
1426 register Element *elPtr;
1427 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1428 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1429
1430 for (elPtr = arrayPtr->els, count = arrayPtr->numUsed;
1431 count > 0; elPtr++, count--) {
1432 if (!(elPtr->flags & (NODE|WILDCARD)) && !leaf) {
1433 continue;
1434 }
1435 tsdPtr->stacks[elPtr->flags] = ExtendArray(
1436 tsdPtr->stacks[elPtr->flags], elPtr);
1437 }
1438 }
1439
1440 /*
1441 *--------------------------------------------------------------
1442 *
1443 * OptionThreadExitProc --
1444 *
1445 * Free data structures for option handling.
1446 *
1447 * Results:
1448 * None.
1449 *
1450 * Side effects:
1451 * Option-related data structures get freed.
1452 *
1453 *--------------------------------------------------------------
1454 */
1455
1456 static void
1457 OptionThreadExitProc(clientData)
1458 ClientData clientData; /* not used */
1459 {
1460 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1461 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1462
1463 if (tsdPtr->initialized) {
1464 int i;
1465 for (i = 0; i < NUM_STACKS; i++) {
1466 ckfree((char *) tsdPtr->stacks[i]);
1467 }
1468 ckfree((char *) tsdPtr->levels);
1469 tsdPtr->initialized = 0;
1470 }
1471 }
1472
1473 /*
1474 *--------------------------------------------------------------
1475 *
1476 * OptionInit --
1477 *
1478 * Initialize data structures for option handling.
1479 *
1480 * Results:
1481 * None.
1482 *
1483 * Side effects:
1484 * Option-related data structures get initialized.
1485 *
1486 *--------------------------------------------------------------
1487 */
1488
1489 static void
1490 OptionInit(mainPtr)
1491 register TkMainInfo *mainPtr; /* Top-level information about
1492 * window that isn't initialized
1493 * yet. */
1494 {
1495 int i;
1496 Tcl_Interp *interp;
1497 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
1498 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
1499 Element *defaultMatchPtr = &tsdPtr->defaultMatch;
1500
1501 /*
1502 * First, once-only initialization.
1503 */
1504
1505 if (tsdPtr->initialized == 0) {
1506 tsdPtr->initialized = 1;
1507 tsdPtr->cachedWindow = NULL;
1508 tsdPtr->numLevels = 5;
1509 tsdPtr->curLevel = -1;
1510 tsdPtr->serial = 0;
1511
1512 tsdPtr->levels = (StackLevel *) ckalloc((unsigned)
1513 (5*sizeof(StackLevel)));
1514 for (i = 0; i < NUM_STACKS; i++) {
1515 tsdPtr->stacks[i] = NewArray(10);
1516 tsdPtr->levels[0].bases[i] = 0;
1517 }
1518
1519 defaultMatchPtr->nameUid = NULL;
1520 defaultMatchPtr->child.valueUid = NULL;
1521 defaultMatchPtr->priority = -1;
1522 defaultMatchPtr->flags = 0;
1523 Tcl_CreateThreadExitHandler(OptionThreadExitProc, NULL);
1524 }
1525
1526 /*
1527 * Then, per-main-window initialization. Create and delete dummy
1528 * interpreter for message logging.
1529 */
1530
1531 mainPtr->optionRootPtr = NewArray(20);
1532 interp = Tcl_CreateInterp();
1533 (void) GetDefaultOptions(interp, mainPtr->winPtr);
1534 Tcl_DeleteInterp(interp);
1535 }
1536
1537 /*
1538 *--------------------------------------------------------------
1539 *
1540 * ClearOptionTree --
1541 *
1542 * This procedure is called to erase everything in a
1543 * hierarchical option database.
1544 *
1545 * Results:
1546 * None.
1547 *
1548 * Side effects:
1549 * All the options associated with arrayPtr are deleted,
1550 * along with all option subtrees. The space pointed to
1551 * by arrayPtr is freed.
1552 *
1553 *--------------------------------------------------------------
1554 */
1555
1556 static void
1557 ClearOptionTree(arrayPtr)
1558 ElArray *arrayPtr; /* Array of options; delete everything
1559 * referred to recursively by this. */
1560 {
1561 register Element *elPtr;
1562 int count;
1563
1564 for (count = arrayPtr->numUsed, elPtr = arrayPtr->els; count > 0;
1565 count--, elPtr++) {
1566 if (elPtr->flags & NODE) {
1567 ClearOptionTree(elPtr->child.arrayPtr);
1568 }
1569 }
1570 ckfree((char *) arrayPtr);
1571 }
1572
1573 /*
1574 *--------------------------------------------------------------
1575 *
1576 * GetDefaultOptions --
1577 *
1578 * This procedure is invoked to load the default set of options
1579 * for a window.
1580 *
1581 * Results:
1582 * None.
1583 *
1584 * Side effects:
1585 * Options are added to those for winPtr's main window. If
1586 * there exists a RESOURCE_MANAGER proprety for winPtr's
1587 * display, that is used. Otherwise, the .Xdefaults file in
1588 * the user's home directory is used.
1589 *
1590 *--------------------------------------------------------------
1591 */
1592
1593 static int
1594 GetDefaultOptions(interp, winPtr)
1595 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
1596 TkWindow *winPtr; /* Fetch option defaults for main window
1597 * associated with this. */
1598 {
1599 char *regProp;
1600 int result, actualFormat;
1601 unsigned long numItems, bytesAfter;
1602 Atom actualType;
1603
1604 /*
1605 * Try the RESOURCE_MANAGER property on the root window first.
1606 */
1607
1608 regProp = NULL;
1609 result = XGetWindowProperty(winPtr->display,
1610 RootWindow(winPtr->display, 0),
1611 XA_RESOURCE_MANAGER, 0, 100000,
1612 False, XA_STRING, &actualType, &actualFormat,
1613 &numItems, &bytesAfter, (unsigned char **) &regProp);
1614
1615 if ((result == Success) && (actualType == XA_STRING)
1616 && (actualFormat == 8)) {
1617 result = AddFromString(interp, (Tk_Window) winPtr, regProp,
1618 TK_USER_DEFAULT_PRIO);
1619 XFree(regProp);
1620 return result;
1621 }
1622
1623 /*
1624 * No luck there. Try a .Xdefaults file in the user's home
1625 * directory.
1626 */
1627
1628 if (regProp != NULL) {
1629 XFree(regProp);
1630 }
1631 result = ReadOptionFile(interp, (Tk_Window) winPtr, "~/.Xdefaults",
1632 TK_USER_DEFAULT_PRIO);
1633 return result;
1634 }