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.
8
* Copyright (c) 1990-1994 The Regents of the University of California.
9
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
11
* See the file "license.terms" for information on usage and redistribution
12
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14
* RCS: @(#) $Id: tkOption.c,v 1.2 1998/09/14 18:23:15 stanton Exp $
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.
31
typedef struct Element {
32
Tk_Uid nameUid; /* Name or class from one element of
35
struct ElArray *arrayPtr; /* If this is an intermediate node,
36
* a pointer to a structure describing
37
* the remaining elements of all
38
* options whose prefixes are the
39
* same up through this element. */
40
Tk_Uid valueUid; /* For leaf nodes, this is the string
41
* value of the option. */
43
int priority; /* Used to select among matching
44
* options. Includes both the
45
* priority level and a serial #.
46
* Greater value means higher
47
* priority. Irrelevant except in
49
int flags; /* OR-ed combination of bits. See
50
* below for values. */
54
* Flags in Element structures:
56
* CLASS - Non-zero means this element refers to a class,
57
* Zero means this element refers to a name.
58
* NODE - Zero means this is a leaf element (the child
59
* field is a value, not a pointer to another node).
60
* One means this is a node element.
61
* WILDCARD - Non-zero means this there was a star in the
62
* original specification just before this element.
63
* Zero means there was a dot.
72
#define EXACT_LEAF_NAME 0x0
73
#define EXACT_LEAF_CLASS 0x1
74
#define EXACT_NODE_NAME 0x2
75
#define EXACT_NODE_CLASS 0x3
76
#define WILDCARD_LEAF_NAME 0x4
77
#define WILDCARD_LEAF_CLASS 0x5
78
#define WILDCARD_NODE_NAME 0x6
79
#define WILDCARD_NODE_CLASS 0x7
82
* The following structure is used to manage a dynamic array of
83
* Elements. These structures are used for two purposes: to store
84
* the contents of a node in the option tree, and for the option
85
* stacks described below.
88
typedef struct ElArray {
89
int arraySize; /* Number of elements actually
90
* allocated in the "els" array. */
91
int numUsed; /* Number of elements currently in
93
Element *nextToUse; /* Pointer to &els[numUsed]. */
94
Element els[1]; /* Array of structures describing
95
* children of this node. The
96
* array will actually contain enough
97
* elements for all of the children
98
* (and even a few extras, perhaps).
99
* This must be the last field in
103
#define EL_ARRAY_SIZE(numEls) ((unsigned) (sizeof(ElArray) \
104
+ ((numEls)-1)*sizeof(Element)))
105
#define INITIAL_SIZE 5
108
* In addition to the option tree, which is a relatively static structure,
109
* there are eight additional structures called "stacks", which are used
110
* to speed up queries into the option database. The stack structures
111
* are designed for the situation where an individual widget makes repeated
112
* requests for its particular options. The requests differ only in
113
* their last name/class, so during the first request we extract all
114
* the options pertaining to the particular widget and save them in a
115
* stack-like cache; subsequent requests for the same widget can search
116
* the cache relatively quickly. In fact, the cache is a hierarchical
117
* one, storing a list of relevant options for this widget and all of
118
* its ancestors up to the application root; hence the name "stack".
120
* Each of the eight stacks consists of an array of Elements, ordered in
121
* terms of levels in the window hierarchy. All the elements relevant
122
* for the top-level widget appear first in the array, followed by all
123
* those from the next-level widget on the path to the current widget,
124
* etc. down to those for the current widget.
126
* Cached information is divided into eight stacks according to the
127
* CLASS, NODE, and WILDCARD flags. Leaf and non-leaf information is
128
* kept separate to speed up individual probes (non-leaf information is
129
* only relevant when building the stacks, but isn't relevant when
130
* making probes; similarly, only non-leaf information is relevant
131
* when the stacks are being extended to the next widget down in the
132
* widget hierarchy). Wildcard elements are handled separately from
133
* "exact" elements because once they appear at a particular level in
134
* the stack they remain active for all deeper levels; exact elements
135
* are only relevant at a particular level. For example, when searching
136
* for options relevant in a particular window, the entire wildcard
137
* stacks get checked, but only the portions of the exact stacks that
138
* pertain to the window's parent. Lastly, name and class stacks are
139
* kept separate because different search keys are used when searching
140
* them; keeping them separate speeds up the searches.
144
static ElArray *stacks[NUM_STACKS];
145
static TkWindow *cachedWindow = NULL; /* Lowest-level window currently
146
* loaded in stacks at present.
147
* NULL means stacks have never
148
* been used, or have been
149
* invalidated because of a change
150
* to the database. */
153
* One of the following structures is used to keep track of each
154
* level in the stacks.
157
typedef struct StackLevel {
158
TkWindow *winPtr; /* Window corresponding to this stack
160
int bases[NUM_STACKS]; /* For each stack, index of first
161
* element on stack corresponding to
162
* this level (used to restore "numUsed"
163
* fields when popping out of a level. */
167
* Information about all of the stack levels that are currently
168
* active. This array grows dynamically to become as large as needed.
171
static StackLevel *levels = NULL;
172
/* Array describing current stack. */
173
static int numLevels = 0; /* Total space allocated. */
174
static int curLevel = -1; /* Highest level currently in use. Note:
175
* curLevel is never 0! (I don't remember
179
* The variable below is a serial number for all options entered into
180
* the database so far. It increments on each addition to the option
181
* database. It is used in computing option priorities, so that the
182
* most recent entry wins when choosing between options at the same
186
static int serial = 0;
189
* Special "no match" Element to use as default for searches.
192
static Element defaultMatch;
195
* Forward declarations for procedures defined in this file:
198
static int AddFromString _ANSI_ARGS_((Tcl_Interp *interp,
199
Tk_Window tkwin, char *string, int priority));
200
static void ClearOptionTree _ANSI_ARGS_((ElArray *arrayPtr));
201
static ElArray * ExtendArray _ANSI_ARGS_((ElArray *arrayPtr,
203
static void ExtendStacks _ANSI_ARGS_((ElArray *arrayPtr,
205
static int GetDefaultOptions _ANSI_ARGS_((Tcl_Interp *interp,
207
static ElArray * NewArray _ANSI_ARGS_((int numEls));
208
static void OptionInit _ANSI_ARGS_((TkMainInfo *mainPtr));
209
static int ParsePriority _ANSI_ARGS_((Tcl_Interp *interp,
211
static int ReadOptionFile _ANSI_ARGS_((Tcl_Interp *interp,
212
Tk_Window tkwin, char *fileName, int priority));
213
static void SetupStacks _ANSI_ARGS_((TkWindow *winPtr, int leaf));
216
*--------------------------------------------------------------
220
* Add a new option to the option database.
226
* Information is added to the option database.
228
*--------------------------------------------------------------
232
Tk_AddOption(tkwin, name, value, priority)
233
Tk_Window tkwin; /* Window token; option will be associated
234
* with main window for this window. */
235
char *name; /* Multi-element name of option. */
236
char *value; /* String value for option. */
237
int priority; /* Overall priority level to use for
238
* this option, such as TK_USER_DEFAULT_PRIO
239
* or TK_INTERACTIVE_PRIO. Must be between
240
* 0 and TK_MAX_PRIO. */
242
TkWindow *winPtr = ((TkWindow *) tkwin)->mainPtr->winPtr;
243
register ElArray **arrayPtrPtr;
244
register Element *elPtr;
248
int count, firstField, length;
250
char tmp[TMP_SIZE+1];
252
if (winPtr->mainPtr->optionRootPtr == NULL) {
253
OptionInit(winPtr->mainPtr);
255
cachedWindow = NULL; /* Invalidate the cache. */
258
* Compute the priority for the new element, including both the
259
* overall level and the serial number (to disambiguate with the
265
} else if (priority > TK_MAX_PRIO) {
266
priority = TK_MAX_PRIO;
268
newEl.priority = (priority << 24) + serial;
272
* Parse the option one field at a time.
275
arrayPtrPtr = &(((TkWindow *) tkwin)->mainPtr->optionRootPtr);
277
for (firstField = 1; ; firstField = 0) {
280
* Scan the next field from the name and convert it to a Tk_Uid.
281
* Must copy the field before calling Tk_Uid, so that a terminating
282
* NULL may be added without modifying the source string.
286
newEl.flags = WILDCARD;
292
while ((*p != 0) && (*p != '.') && (*p != '*')) {
296
if (length > TMP_SIZE) {
299
strncpy(tmp, field, (size_t) length);
301
newEl.nameUid = Tk_GetUid(tmp);
302
if (isupper(UCHAR(*field))) {
303
newEl.flags |= CLASS;
309
* New element will be a node. If this option can't possibly
310
* apply to this main window, then just skip it. Otherwise,
311
* add it to the parent, if it isn't already there, and descend
316
if (firstField && !(newEl.flags & WILDCARD)
317
&& (newEl.nameUid != winPtr->nameUid)
318
&& (newEl.nameUid != winPtr->classUid)) {
321
for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
322
; elPtr++, count--) {
324
newEl.child.arrayPtr = NewArray(5);
325
*arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
326
arrayPtrPtr = &((*arrayPtrPtr)->nextToUse[-1].child.arrayPtr);
329
if ((elPtr->nameUid == newEl.nameUid)
330
&& (elPtr->flags == newEl.flags)) {
331
arrayPtrPtr = &(elPtr->child.arrayPtr);
341
* New element is a leaf. Add it to the parent, if it isn't
342
* already there. If it exists already, keep whichever value
343
* has highest priority.
346
newEl.child.valueUid = Tk_GetUid(value);
347
for (elPtr = (*arrayPtrPtr)->els, count = (*arrayPtrPtr)->numUsed;
348
; elPtr++, count--) {
350
*arrayPtrPtr = ExtendArray(*arrayPtrPtr, &newEl);
353
if ((elPtr->nameUid == newEl.nameUid)
354
&& (elPtr->flags == newEl.flags)) {
355
if (elPtr->priority < newEl.priority) {
356
elPtr->priority = newEl.priority;
357
elPtr->child.valueUid = newEl.child.valueUid;
367
*--------------------------------------------------------------
371
* Retrieve an option from the option database.
374
* The return value is the value specified in the option
375
* database for the given name and class on the given
376
* window. If there is nothing specified in the database
377
* for that option, then NULL is returned.
380
* The internal caches used to speed up option mapping
381
* may be modified, if this tkwin is different from the
382
* last tkwin used for option retrieval.
384
*--------------------------------------------------------------
388
Tk_GetOption(tkwin, name, className)
389
Tk_Window tkwin; /* Token for window that option is
390
* associated with. */
391
char *name; /* Name of option. */
392
char *className; /* Class of option. NULL means there
393
* is no class for this option: just
396
Tk_Uid nameId, classId;
397
register Element *elPtr, *bestPtr;
401
* Note: no need to call OptionInit here: it will be done by
402
* the SetupStacks call below (squeeze out those nanoseconds).
405
if (tkwin != (Tk_Window) cachedWindow) {
406
SetupStacks((TkWindow *) tkwin, 1);
409
nameId = Tk_GetUid(name);
410
bestPtr = &defaultMatch;
411
for (elPtr = stacks[EXACT_LEAF_NAME]->els,
412
count = stacks[EXACT_LEAF_NAME]->numUsed; count > 0;
414
if ((elPtr->nameUid == nameId)
415
&& (elPtr->priority > bestPtr->priority)) {
419
for (elPtr = stacks[WILDCARD_LEAF_NAME]->els,
420
count = stacks[WILDCARD_LEAF_NAME]->numUsed; count > 0;
422
if ((elPtr->nameUid == nameId)
423
&& (elPtr->priority > bestPtr->priority)) {
427
if (className != NULL) {
428
classId = Tk_GetUid(className);
429
for (elPtr = stacks[EXACT_LEAF_CLASS]->els,
430
count = stacks[EXACT_LEAF_CLASS]->numUsed; count > 0;
432
if ((elPtr->nameUid == classId)
433
&& (elPtr->priority > bestPtr->priority)) {
437
for (elPtr = stacks[WILDCARD_LEAF_CLASS]->els,
438
count = stacks[WILDCARD_LEAF_CLASS]->numUsed; count > 0;
440
if ((elPtr->nameUid == classId)
441
&& (elPtr->priority > bestPtr->priority)) {
446
return bestPtr->child.valueUid;
450
*--------------------------------------------------------------
454
* This procedure is invoked to process the "option" Tcl command.
455
* See the user documentation for details on what it does.
458
* A standard Tcl result.
461
* See the user documentation.
463
*--------------------------------------------------------------
467
Tk_OptionCmd(clientData, interp, argc, argv)
468
ClientData clientData; /* Main window associated with
470
Tcl_Interp *interp; /* Current interpreter. */
471
int argc; /* Number of arguments. */
472
char **argv; /* Argument strings. */
474
Tk_Window tkwin = (Tk_Window) clientData;
479
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
480
" cmd arg ?arg ...?\"", (char *) NULL);
484
length = strlen(argv[1]);
485
if ((c == 'a') && (strncmp(argv[1], "add", length) == 0)) {
488
if ((argc != 4) && (argc != 5)) {
489
Tcl_AppendResult(interp, "wrong # args: should be \"",
490
argv[0], " add pattern value ?priority?\"", (char *) NULL);
494
priority = TK_INTERACTIVE_PRIO;
496
priority = ParsePriority(interp, argv[4]);
501
Tk_AddOption(tkwin, argv[2], argv[3], priority);
503
} else if ((c == 'c') && (strncmp(argv[1], "clear", length) == 0)) {
507
Tcl_AppendResult(interp, "wrong # args: should be \"",
508
argv[0], " clear\"", (char *) NULL);
511
mainPtr = ((TkWindow *) tkwin)->mainPtr;
512
if (mainPtr->optionRootPtr != NULL) {
513
ClearOptionTree(mainPtr->optionRootPtr);
514
mainPtr->optionRootPtr = NULL;
518
} else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
523
Tcl_AppendResult(interp, "wrong # args: should be \"",
524
argv[0], " get window name class\"", (char *) NULL);
527
window = Tk_NameToWindow(interp, argv[2], tkwin);
528
if (window == NULL) {
531
value = Tk_GetOption(window, argv[3], argv[4]);
533
interp->result = value;
536
} else if ((c == 'r') && (strncmp(argv[1], "readfile", length) == 0)) {
539
if ((argc != 3) && (argc != 4)) {
540
Tcl_AppendResult(interp, "wrong # args: should be \"",
541
argv[0], " readfile fileName ?priority?\"",
546
priority = ParsePriority(interp, argv[3]);
551
priority = TK_INTERACTIVE_PRIO;
553
return ReadOptionFile(interp, tkwin, argv[2], priority);
555
Tcl_AppendResult(interp, "bad option \"", argv[1],
556
"\": must be add, clear, get, or readfile", (char *) NULL);
562
*--------------------------------------------------------------
564
* TkOptionDeadWindow --
566
* This procedure is called whenever a window is deleted.
567
* It cleans up any option-related stuff associated with
574
* Option-related resources are freed. See code below
577
*--------------------------------------------------------------
581
TkOptionDeadWindow(winPtr)
582
register TkWindow *winPtr; /* Window to be cleaned up. */
585
* If this window is in the option stacks, then clear the stacks.
588
if (winPtr->optionLevel != -1) {
591
for (i = 1; i <= curLevel; i++) {
592
levels[i].winPtr->optionLevel = -1;
599
* If this window was a main window, then delete its option
603
if ((winPtr->mainPtr->winPtr == winPtr)
604
&& (winPtr->mainPtr->optionRootPtr != NULL)) {
605
ClearOptionTree(winPtr->mainPtr->optionRootPtr);
606
winPtr->mainPtr->optionRootPtr = NULL;
611
*----------------------------------------------------------------------
613
* TkOptionClassChanged --
615
* This procedure is invoked when a window's class changes. If
616
* the window is on the option cache, this procedure flushes
617
* any information for the window, since the new class could change
624
* The option cache may be flushed in part or in whole.
626
*----------------------------------------------------------------------
630
TkOptionClassChanged(winPtr)
631
TkWindow *winPtr; /* Window whose class changed. */
636
if (winPtr->optionLevel == -1) {
641
* Find the lowest stack level that refers to this window, then
642
* flush all of the levels above the matching one.
645
for (i = 1; i <= curLevel; i++) {
646
if (levels[i].winPtr == winPtr) {
647
for (j = i; j <= curLevel; j++) {
648
levels[j].winPtr->optionLevel = -1;
651
basePtr = levels[i].bases;
652
for (j = 0; j < NUM_STACKS; j++) {
653
arrayPtr = stacks[j];
654
arrayPtr->numUsed = basePtr[j];
655
arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
660
cachedWindow = levels[curLevel].winPtr;
668
*----------------------------------------------------------------------
672
* Parse a string priority value.
675
* The return value is the integer priority level corresponding
676
* to string, or -1 if string doesn't point to a valid priority level.
677
* In this case, an error message is left in interp->result.
682
*----------------------------------------------------------------------
686
ParsePriority(interp, string)
687
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
688
char *string; /* Describes a priority level, either
689
* symbolically or numerically. */
695
length = strlen(string);
697
&& (strncmp(string, "widgetDefault", length) == 0)) {
698
return TK_WIDGET_DEFAULT_PRIO;
699
} else if ((c == 's')
700
&& (strncmp(string, "startupFile", length) == 0)) {
701
return TK_STARTUP_FILE_PRIO;
702
} else if ((c == 'u')
703
&& (strncmp(string, "userDefault", length) == 0)) {
704
return TK_USER_DEFAULT_PRIO;
705
} else if ((c == 'i')
706
&& (strncmp(string, "interactive", length) == 0)) {
707
return TK_INTERACTIVE_PRIO;
711
priority = strtoul(string, &end, 0);
712
if ((end == string) || (*end != 0) || (priority < 0)
713
|| (priority > 100)) {
714
Tcl_AppendResult(interp, "bad priority level \"", string,
715
"\": must be widgetDefault, startupFile, userDefault, ",
716
"interactive, or a number between 0 and 100",
725
*----------------------------------------------------------------------
729
* Given a string containing lines in the standard format for
730
* X resources (see other documentation for details on what this
731
* is), parse the resource specifications and enter them as options
732
* for tkwin's main window.
735
* The return value is a standard Tcl return code. In the case of
736
* an error in parsing string, TCL_ERROR will be returned and an
737
* error message will be left in interp->result. The memory at
738
* string is totally trashed by this procedure. If you care about
739
* its contents, make a copy before calling here.
744
*----------------------------------------------------------------------
748
AddFromString(interp, tkwin, string, priority)
749
Tcl_Interp *interp; /* Interpreter to use for reporting results. */
750
Tk_Window tkwin; /* Token for window: options are entered
751
* for this window's main window. */
752
char *string; /* String containing option specifiers. */
753
int priority; /* Priority level to use for options in
754
* this string, such as TK_USER_DEFAULT_PRIO
755
* or TK_INTERACTIVE_PRIO. Must be between
756
* 0 and TK_MAX_PRIO. */
758
register char *src, *dst;
767
* Skip leading white space and empty lines and comment lines, and
768
* check for the end of the spec.
771
while ((*src == ' ') || (*src == '\t')) {
774
if ((*src == '#') || (*src == '!')) {
777
if ((src[0] == '\\') && (src[1] == '\n')) {
781
} while ((*src != '\n') && (*src != 0));
793
* Parse off the option name, collapsing out backslash-newline
794
* sequences of course.
798
while (*src != ':') {
799
if ((*src == '\0') || (*src == '\n')) {
800
sprintf(interp->result, "missing colon on line %d",
804
if ((src[0] == '\\') && (src[1] == '\n')) {
815
* Eliminate trailing white space on the name, and null-terminate
819
while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
825
* Skip white space between the name and the value.
829
while ((*src == ' ') || (*src == '\t')) {
833
sprintf(interp->result, "missing value on line %d", lineNum);
838
* Parse off the value, squeezing out backslash-newline sequences
843
while (*src != '\n') {
845
sprintf(interp->result, "missing newline on line %d",
849
if ((src[0] == '\\') && (src[1] == '\n')) {
861
* Enter the option into the database.
864
Tk_AddOption(tkwin, name, value, priority);
872
*----------------------------------------------------------------------
876
* Read a file of options ("resources" in the old X terminology)
877
* and load them into the option database.
880
* The return value is a standard Tcl return code. In the case of
881
* an error in parsing string, TCL_ERROR will be returned and an
882
* error message will be left in interp->result.
887
*----------------------------------------------------------------------
891
ReadOptionFile(interp, tkwin, fileName, priority)
892
Tcl_Interp *interp; /* Interpreter to use for reporting results. */
893
Tk_Window tkwin; /* Token for window: options are entered
894
* for this window's main window. */
895
char *fileName; /* Name of file containing options. */
896
int priority; /* Priority level to use for options in
897
* this file, such as TK_USER_DEFAULT_PRIO
898
* or TK_INTERACTIVE_PRIO. Must be between
899
* 0 and TK_MAX_PRIO. */
901
char *realName, *buffer;
902
int result, bufferSize;
907
* Prevent file system access in a safe interpreter.
910
if (Tcl_IsSafe(interp)) {
911
Tcl_AppendResult(interp, "can't read options from a file in a",
912
" safe interpreter", (char *) NULL);
916
realName = Tcl_TranslateFileName(interp, fileName, &newName);
917
if (realName == NULL) {
920
chan = Tcl_OpenFileChannel(interp, realName, "r", 0);
921
Tcl_DStringFree(&newName);
923
Tcl_ResetResult(interp);
924
Tcl_AppendResult(interp, "couldn't open \"", fileName,
925
"\": ", Tcl_PosixError(interp), (char *) NULL);
930
* Compute size of file by seeking to the end of the file. This will
931
* overallocate if we are performing CRLF translation.
934
bufferSize = Tcl_Seek(chan, 0L, SEEK_END);
935
(void) Tcl_Seek(chan, 0L, SEEK_SET);
937
if (bufferSize < 0) {
938
Tcl_AppendResult(interp, "error seeking to end of file \"",
939
fileName, "\":", Tcl_PosixError(interp), (char *) NULL);
940
Tcl_Close(NULL, chan);
944
buffer = (char *) ckalloc((unsigned) bufferSize+1);
945
bufferSize = Tcl_Read(chan, buffer, bufferSize);
946
if (bufferSize < 0) {
947
Tcl_AppendResult(interp, "error reading file \"", fileName, "\":",
948
Tcl_PosixError(interp), (char *) NULL);
949
Tcl_Close(NULL, chan);
952
Tcl_Close(NULL, chan);
953
buffer[bufferSize] = 0;
954
result = AddFromString(interp, tkwin, buffer, priority);
960
*--------------------------------------------------------------
964
* Create a new ElArray structure of a given size.
967
* The return value is a pointer to a properly initialized
968
* element array with "numEls" space. The array is marked
969
* as having no active elements.
972
* Memory is allocated.
974
*--------------------------------------------------------------
979
int numEls; /* How many elements of space to allocate. */
981
register ElArray *arrayPtr;
983
arrayPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(numEls));
984
arrayPtr->arraySize = numEls;
985
arrayPtr->numUsed = 0;
986
arrayPtr->nextToUse = arrayPtr->els;
991
*--------------------------------------------------------------
995
* Add a new element to an array, extending the array if
999
* The return value is a pointer to the new array, which
1000
* will be different from arrayPtr if the array got expanded.
1003
* Memory may be allocated or freed.
1005
*--------------------------------------------------------------
1009
ExtendArray(arrayPtr, elPtr)
1010
register ElArray *arrayPtr; /* Array to be extended. */
1011
register Element *elPtr; /* Element to be copied into array. */
1014
* If the current array has filled up, make it bigger.
1017
if (arrayPtr->numUsed >= arrayPtr->arraySize) {
1018
register ElArray *newPtr;
1020
newPtr = (ElArray *) ckalloc(EL_ARRAY_SIZE(2*arrayPtr->arraySize));
1021
newPtr->arraySize = 2*arrayPtr->arraySize;
1022
newPtr->numUsed = arrayPtr->numUsed;
1023
newPtr->nextToUse = &newPtr->els[newPtr->numUsed];
1024
memcpy((VOID *) newPtr->els, (VOID *) arrayPtr->els,
1025
(arrayPtr->arraySize*sizeof(Element)));
1026
ckfree((char *) arrayPtr);
1030
*arrayPtr->nextToUse = *elPtr;
1031
arrayPtr->nextToUse++;
1032
arrayPtr->numUsed++;
1037
*--------------------------------------------------------------
1041
* Arrange the stacks so that they cache all the option
1042
* information for a particular window.
1048
* The stacks are modified to hold information for tkwin
1049
* and all its ancestors in the window hierarchy.
1051
*--------------------------------------------------------------
1055
SetupStacks(winPtr, leaf)
1056
TkWindow *winPtr; /* Window for which information is to
1058
int leaf; /* Non-zero means this is the leaf
1059
* window being probed. Zero means this
1060
* is an ancestor of the desired leaf. */
1062
int level, i, *iPtr;
1063
register StackLevel *levelPtr;
1064
register ElArray *arrayPtr;
1067
* The following array defines the order in which the current
1068
* stacks are searched to find matching entries to add to the
1069
* stacks. Given the current priority-based scheme, the order
1070
* below is no longer relevant; all that matters is that an
1071
* element is on the list *somewhere*. The ordering is a relic
1072
* of the old days when priorities were determined differently.
1075
static int searchOrder[] = {WILDCARD_NODE_CLASS, WILDCARD_NODE_NAME,
1076
EXACT_NODE_CLASS, EXACT_NODE_NAME, -1};
1078
if (winPtr->mainPtr->optionRootPtr == NULL) {
1079
OptionInit(winPtr->mainPtr);
1083
* Step 1: make sure that options are cached for this window's
1087
if (winPtr->parentPtr != NULL) {
1088
level = winPtr->parentPtr->optionLevel;
1089
if ((level == -1) || (cachedWindow == NULL)) {
1090
SetupStacks(winPtr->parentPtr, 0);
1091
level = winPtr->parentPtr->optionLevel;
1099
* Step 2: pop extra unneeded information off the stacks and
1100
* mark those windows as no longer having cached information.
1103
if (curLevel >= level) {
1104
while (curLevel >= level) {
1105
levels[curLevel].winPtr->optionLevel = -1;
1108
levelPtr = &levels[level];
1109
for (i = 0; i < NUM_STACKS; i++) {
1110
arrayPtr = stacks[i];
1111
arrayPtr->numUsed = levelPtr->bases[i];
1112
arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
1115
curLevel = winPtr->optionLevel = level;
1118
* Step 3: if the root database information isn't loaded or
1119
* isn't valid, initialize level 0 of the stack from the
1120
* database root (this only happens if winPtr is a main window).
1124
&& ((cachedWindow == NULL)
1125
|| (cachedWindow->mainPtr != winPtr->mainPtr))) {
1126
for (i = 0; i < NUM_STACKS; i++) {
1127
arrayPtr = stacks[i];
1128
arrayPtr->numUsed = 0;
1129
arrayPtr->nextToUse = arrayPtr->els;
1131
ExtendStacks(winPtr->mainPtr->optionRootPtr, 0);
1135
* Step 4: create a new stack level; grow the level array if
1136
* we've run out of levels. Clear the stacks for EXACT_LEAF_NAME
1137
* and EXACT_LEAF_CLASS (anything that was there is of no use
1141
if (curLevel >= numLevels) {
1142
StackLevel *newLevels;
1144
newLevels = (StackLevel *) ckalloc((unsigned)
1145
(numLevels*2*sizeof(StackLevel)));
1146
memcpy((VOID *) newLevels, (VOID *) levels,
1147
(numLevels*sizeof(StackLevel)));
1148
ckfree((char *) levels);
1152
levelPtr = &levels[curLevel];
1153
levelPtr->winPtr = winPtr;
1154
arrayPtr = stacks[EXACT_LEAF_NAME];
1155
arrayPtr->numUsed = 0;
1156
arrayPtr->nextToUse = arrayPtr->els;
1157
arrayPtr = stacks[EXACT_LEAF_CLASS];
1158
arrayPtr->numUsed = 0;
1159
arrayPtr->nextToUse = arrayPtr->els;
1160
levelPtr->bases[EXACT_LEAF_NAME] = stacks[EXACT_LEAF_NAME]->numUsed;
1161
levelPtr->bases[EXACT_LEAF_CLASS] = stacks[EXACT_LEAF_CLASS]->numUsed;
1162
levelPtr->bases[EXACT_NODE_NAME] = stacks[EXACT_NODE_NAME]->numUsed;
1163
levelPtr->bases[EXACT_NODE_CLASS] = stacks[EXACT_NODE_CLASS]->numUsed;
1164
levelPtr->bases[WILDCARD_LEAF_NAME] = stacks[WILDCARD_LEAF_NAME]->numUsed;
1165
levelPtr->bases[WILDCARD_LEAF_CLASS] = stacks[WILDCARD_LEAF_CLASS]->numUsed;
1166
levelPtr->bases[WILDCARD_NODE_NAME] = stacks[WILDCARD_NODE_NAME]->numUsed;
1167
levelPtr->bases[WILDCARD_NODE_CLASS] = stacks[WILDCARD_NODE_CLASS]->numUsed;
1171
* Step 5: scan the current stack level looking for matches to this
1172
* window's name or class; where found, add new information to the
1176
for (iPtr = searchOrder; *iPtr != -1; iPtr++) {
1177
register Element *elPtr;
1183
id = winPtr->classUid;
1185
id = winPtr->nameUid;
1187
elPtr = stacks[i]->els;
1188
count = levelPtr->bases[i];
1191
* For wildcard stacks, check all entries; for non-wildcard
1192
* stacks, only check things that matched in the parent.
1195
if (!(i & WILDCARD)) {
1196
elPtr += levelPtr[-1].bases[i];
1197
count -= levelPtr[-1].bases[i];
1199
for ( ; count > 0; elPtr++, count--) {
1200
if (elPtr->nameUid != id) {
1203
ExtendStacks(elPtr->child.arrayPtr, leaf);
1206
cachedWindow = winPtr;
1210
*--------------------------------------------------------------
1214
* Given an element array, copy all the elements from the
1215
* array onto the system stacks (except for irrelevant leaf
1222
* The option stacks are extended.
1224
*--------------------------------------------------------------
1228
ExtendStacks(arrayPtr, leaf)
1229
ElArray *arrayPtr; /* Array of elements to copy onto stacks. */
1230
int leaf; /* If zero, then don't copy exact leaf
1234
register Element *elPtr;
1236
for (elPtr = arrayPtr->els, count = arrayPtr->numUsed;
1237
count > 0; elPtr++, count--) {
1238
if (!(elPtr->flags & (NODE|WILDCARD)) && !leaf) {
1241
stacks[elPtr->flags] = ExtendArray(stacks[elPtr->flags], elPtr);
1246
*--------------------------------------------------------------
1250
* Initialize data structures for option handling.
1256
* Option-related data structures get initialized.
1258
*--------------------------------------------------------------
1263
register TkMainInfo *mainPtr; /* Top-level information about
1264
* window that isn't initialized
1271
* First, once-only initialization.
1274
if (numLevels == 0) {
1277
levels = (StackLevel *) ckalloc((unsigned) (5*sizeof(StackLevel)));
1278
for (i = 0; i < NUM_STACKS; i++) {
1279
stacks[i] = NewArray(10);
1280
levels[0].bases[i] = 0;
1283
defaultMatch.nameUid = NULL;
1284
defaultMatch.child.valueUid = NULL;
1285
defaultMatch.priority = -1;
1286
defaultMatch.flags = 0;
1290
* Then, per-main-window initialization. Create and delete dummy
1291
* interpreter for message logging.
1294
mainPtr->optionRootPtr = NewArray(20);
1295
interp = Tcl_CreateInterp();
1296
(void) GetDefaultOptions(interp, mainPtr->winPtr);
1297
Tcl_DeleteInterp(interp);
1301
*--------------------------------------------------------------
1303
* ClearOptionTree --
1305
* This procedure is called to erase everything in a
1306
* hierarchical option database.
1312
* All the options associated with arrayPtr are deleted,
1313
* along with all option subtrees. The space pointed to
1314
* by arrayPtr is freed.
1316
*--------------------------------------------------------------
1320
ClearOptionTree(arrayPtr)
1321
ElArray *arrayPtr; /* Array of options; delete everything
1322
* referred to recursively by this. */
1324
register Element *elPtr;
1327
for (count = arrayPtr->numUsed, elPtr = arrayPtr->els; count > 0;
1329
if (elPtr->flags & NODE) {
1330
ClearOptionTree(elPtr->child.arrayPtr);
1333
ckfree((char *) arrayPtr);
1337
*--------------------------------------------------------------
1339
* GetDefaultOptions --
1341
* This procedure is invoked to load the default set of options
1348
* Options are added to those for winPtr's main window. If
1349
* there exists a RESOURCE_MANAGER proprety for winPtr's
1350
* display, that is used. Otherwise, the .Xdefaults file in
1351
* the user's home directory is used.
1353
*--------------------------------------------------------------
1357
GetDefaultOptions(interp, winPtr)
1358
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
1359
TkWindow *winPtr; /* Fetch option defaults for main window
1360
* associated with this. */
1363
int result, actualFormat;
1364
unsigned long numItems, bytesAfter;
1368
* Try the RESOURCE_MANAGER property on the root window first.
1372
result = XGetWindowProperty(winPtr->display,
1373
RootWindow(winPtr->display, 0),
1374
XA_RESOURCE_MANAGER, 0, 100000,
1375
False, XA_STRING, &actualType, &actualFormat,
1376
&numItems, &bytesAfter, (unsigned char **) ®Prop);
1378
if ((result == Success) && (actualType == XA_STRING)
1379
&& (actualFormat == 8)) {
1380
result = AddFromString(interp, (Tk_Window) winPtr, regProp,
1381
TK_USER_DEFAULT_PRIO);
1387
* No luck there. Try a .Xdefaults file in the user's home
1391
if (regProp != NULL) {
1394
result = ReadOptionFile(interp, (Tk_Window) winPtr, "~/.Xdefaults",
1395
TK_USER_DEFAULT_PRIO);