OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tk8.6.12 / generic / tkAtom.c
1 /*
2  * tkAtom.c --
3  *
4  *      This file manages a cache of X Atoms in order to avoid interactions
5  *      with the X server. It's much like the Xmu routines, except it has a
6  *      cleaner interface (caller doesn't have to provide permanent storage
7  *      for atom names, for example).
8  *
9  * Copyright (c) 1990-1994 The Regents of the University of California.
10  * Copyright (c) 1994 Sun Microsystems, Inc.
11  *
12  * See the file "license.terms" for information on usage and redistribution of
13  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
14  */
15
16 #include "tkInt.h"
17
18 /*
19  * The following are a list of the predefined atom strings. They should match
20  * those found in xatom.h
21  */
22
23 static const char *const atomNameArray[] = {
24     "PRIMARY",          "SECONDARY",            "ARC",
25     "ATOM",             "BITMAP",               "CARDINAL",
26     "COLORMAP",         "CURSOR",               "CUT_BUFFER0",
27     "CUT_BUFFER1",      "CUT_BUFFER2",          "CUT_BUFFER3",
28     "CUT_BUFFER4",      "CUT_BUFFER5",          "CUT_BUFFER6",
29     "CUT_BUFFER7",      "DRAWABLE",             "FONT",
30     "INTEGER",          "PIXMAP",               "POINT",
31     "RECTANGLE",        "RESOURCE_MANAGER",     "RGB_COLOR_MAP",
32     "RGB_BEST_MAP",     "RGB_BLUE_MAP",         "RGB_DEFAULT_MAP",
33     "RGB_GRAY_MAP",     "RGB_GREEN_MAP",        "RGB_RED_MAP",
34     "STRING",           "VISUALID",             "WINDOW",
35     "WM_COMMAND",       "WM_HINTS",             "WM_CLIENT_MACHINE",
36     "WM_ICON_NAME",     "WM_ICON_SIZE",         "WM_NAME",
37     "WM_NORMAL_HINTS",  "WM_SIZE_HINTS",        "WM_ZOOM_HINTS",
38     "MIN_SPACE",        "NORM_SPACE",           "MAX_SPACE",
39     "END_SPACE",        "SUPERSCRIPT_X",        "SUPERSCRIPT_Y",
40     "SUBSCRIPT_X",      "SUBSCRIPT_Y",          "UNDERLINE_POSITION",
41     "UNDERLINE_THICKNESS", "STRIKEOUT_ASCENT",  "STRIKEOUT_DESCENT",
42     "ITALIC_ANGLE",     "X_HEIGHT",             "QUAD_WIDTH",
43     "WEIGHT",           "POINT_SIZE",           "RESOLUTION",
44     "COPYRIGHT",        "NOTICE",               "FONT_NAME",
45     "FAMILY_NAME",      "FULL_NAME",            "CAP_HEIGHT",
46     "WM_CLASS",         "WM_TRANSIENT_FOR",
47     NULL
48 };
49
50 /*
51  * Forward references to functions defined in this file:
52  */
53
54 static void     AtomInit(TkDisplay *dispPtr);
55 \f
56 /*
57  *--------------------------------------------------------------
58  *
59  * Tk_InternAtom --
60  *
61  *      Given a string, produce the equivalent X atom. This function is
62  *      equivalent to XInternAtom, except that it keeps a local cache of
63  *      atoms. Once a name is known, the server need not be contacted again
64  *      for that name.
65  *
66  * Results:
67  *      The return value is the Atom corresponding to name.
68  *
69  * Side effects:
70  *      A new entry may be added to the local atom cache.
71  *
72  *--------------------------------------------------------------
73  */
74
75 Atom
76 Tk_InternAtom(
77     Tk_Window tkwin,            /* Window token; map name to atom for this
78                                  * window's display. */
79     const char *name)           /* Name to turn into atom. */
80 {
81     TkDisplay *dispPtr;
82     Tcl_HashEntry *hPtr;
83     int isNew;
84
85     dispPtr = ((TkWindow *) tkwin)->dispPtr;
86     if (!dispPtr->atomInit) {
87         AtomInit(dispPtr);
88     }
89
90     hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, name, &isNew);
91     if (isNew) {
92         Tcl_HashEntry *hPtr2;
93         Atom atom;
94
95         atom = XInternAtom(dispPtr->display, name, False);
96         Tcl_SetHashValue(hPtr, INT2PTR(atom));
97         hPtr2 = Tcl_CreateHashEntry(&dispPtr->atomTable, INT2PTR(atom), &isNew);
98         Tcl_SetHashValue(hPtr2, Tcl_GetHashKey(&dispPtr->nameTable, hPtr));
99     }
100     return (Atom)PTR2INT(Tcl_GetHashValue(hPtr));
101 }
102 \f
103 /*
104  *--------------------------------------------------------------
105  *
106  * Tk_GetAtomName --
107  *
108  *      This function is equivalent to XGetAtomName except that it uses the
109  *      local atom cache to avoid contacting the server.
110  *
111  * Results:
112  *      The return value is a character string corresponding to the atom given
113  *      by "atom". This string's storage space is static: it need not be freed
114  *      by the caller, and should not be modified by the caller. If "atom"
115  *      doesn't exist on tkwin's display, then the string "?bad atom?"  is
116  *      returned.
117  *
118  * Side effects:
119  *      None.
120  *
121  *--------------------------------------------------------------
122  */
123
124 const char *
125 Tk_GetAtomName(
126     Tk_Window tkwin,            /* Window token; map atom to name relative to
127                                  * this window's display. */
128     Atom atom)                  /* Atom whose name is wanted. */
129 {
130     TkDisplay *dispPtr;
131     Tcl_HashEntry *hPtr;
132
133     dispPtr = ((TkWindow *) tkwin)->dispPtr;
134     if (!dispPtr->atomInit) {
135         AtomInit(dispPtr);
136     }
137
138     hPtr = Tcl_FindHashEntry(&dispPtr->atomTable, INT2PTR(atom));
139     if (hPtr == NULL) {
140         const char *name;
141         Tk_ErrorHandler handler;
142         int isNew;
143         char *mustFree = NULL;
144
145         handler = Tk_CreateErrorHandler(dispPtr->display, BadAtom, -1, -1,
146                 NULL, NULL);
147         name = mustFree = XGetAtomName(dispPtr->display, atom);
148         if (name == NULL) {
149             name = "?bad atom?";
150         }
151         Tk_DeleteErrorHandler(handler);
152         hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, name, &isNew);
153         Tcl_SetHashValue(hPtr, INT2PTR(atom));
154         if (mustFree) {
155             XFree(mustFree);
156         }
157         name = (const char *)Tcl_GetHashKey(&dispPtr->nameTable, hPtr);
158         hPtr = Tcl_CreateHashEntry(&dispPtr->atomTable, INT2PTR(atom), &isNew);
159         Tcl_SetHashValue(hPtr, name);
160     }
161     return (const char *)Tcl_GetHashValue(hPtr);
162 }
163 \f
164 /*
165  *--------------------------------------------------------------
166  *
167  * AtomInit --
168  *
169  *      Initialize atom-related information for a display.
170  *
171  * Results:
172  *      None.
173  *
174  * Side effects:
175  *      Tables get initialized, etc. etc..
176  *
177  *--------------------------------------------------------------
178  */
179
180 static void
181 AtomInit(
182     TkDisplay *dispPtr)/* Display to initialize. */
183 {
184     Tcl_HashEntry *hPtr;
185     Atom atom;
186
187     dispPtr->atomInit = 1;
188     Tcl_InitHashTable(&dispPtr->nameTable, TCL_STRING_KEYS);
189     Tcl_InitHashTable(&dispPtr->atomTable, TCL_ONE_WORD_KEYS);
190
191     for (atom = 1; atom <= XA_LAST_PREDEFINED; atom++) {
192         const char *name;
193         int isNew;
194
195         hPtr = Tcl_FindHashEntry(&dispPtr->atomTable, INT2PTR(atom));
196         if (hPtr != NULL) {
197             continue;
198         }
199
200         name = atomNameArray[atom - 1];
201         hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, name, &isNew);
202         Tcl_SetHashValue(hPtr, INT2PTR(atom));
203         name = (const char *)Tcl_GetHashKey(&dispPtr->nameTable, hPtr);
204         hPtr = Tcl_CreateHashEntry(&dispPtr->atomTable, INT2PTR(atom), &isNew);
205         Tcl_SetHashValue(hPtr, name);
206     }
207 }
208 \f
209 /*
210  * Local Variables:
211  * mode: c
212  * c-basic-offset: 4
213  * fill-column: 78
214  * End:
215  */