]>
Commit | Line | Data |
---|---|---|
6a5fa4e0 MG |
1 | /* |
2 | * tkAtom.c -- | |
3 | * | |
4 | * This file manages a cache of X Atoms in order to avoid | |
5 | * interactions with the X server. It's much like the Xmu | |
6 | * routines, except it has a cleaner interface (caller | |
7 | * doesn't have to provide permanent storage for atom names, | |
8 | * for example). | |
9 | * | |
10 | * Copyright 1990 Regents of the University of California. | |
11 | * Permission to use, copy, modify, and distribute this | |
12 | * software and its documentation for any purpose and without | |
13 | * fee is hereby granted, provided that the above copyright | |
14 | * notice appear in all copies. The University of California | |
15 | * makes no representations about the suitability of this | |
16 | * software for any purpose. It is provided "as is" without | |
17 | * express or implied warranty. | |
18 | */ | |
19 | ||
20 | #ifndef lint | |
21 | static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkAtom.c,v 1.6 92/05/07 09:51:06 ouster Exp $ SPRITE (Berkeley)"; | |
22 | #endif | |
23 | ||
24 | #include "tkconfig.h" | |
25 | #include "tkint.h" | |
26 | ||
27 | /* | |
28 | * Forward references to procedures defined in this file: | |
29 | */ | |
30 | ||
31 | static void AtomInit _ANSI_ARGS_((TkDisplay *dispPtr)); | |
32 | \f | |
33 | /* | |
34 | *-------------------------------------------------------------- | |
35 | * | |
36 | * Tk_InternAtom -- | |
37 | * | |
38 | * Given a string, produce the equivalent X atom. This | |
39 | * procedure is equivalent to XInternAtom, except that it | |
40 | * keeps a local cache of atoms. Once a name is known, | |
41 | * the server need not be contacted again for that name. | |
42 | * | |
43 | * Results: | |
44 | * The return value is the Atom corresponding to name. | |
45 | * | |
46 | * Side effects: | |
47 | * A new entry may be added to the local atom cache. | |
48 | * | |
49 | *-------------------------------------------------------------- | |
50 | */ | |
51 | ||
52 | Atom | |
53 | Tk_InternAtom(tkwin, name) | |
54 | Tk_Window tkwin; /* Window token; map name to atom | |
55 | * for this window's display. */ | |
56 | char *name; /* Name to turn into atom. */ | |
57 | { | |
58 | register TkDisplay *dispPtr; | |
59 | register Tcl_HashEntry *hPtr; | |
60 | int new; | |
61 | ||
62 | dispPtr = ((TkWindow *) tkwin)->dispPtr; | |
63 | if (!dispPtr->atomInit) { | |
64 | AtomInit(dispPtr); | |
65 | } | |
66 | ||
67 | hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, name, &new); | |
68 | if (new) { | |
69 | Tcl_HashEntry *hPtr2; | |
70 | Atom atom; | |
71 | ||
72 | atom = XInternAtom(dispPtr->display, name, False); | |
73 | Tcl_SetHashValue(hPtr, atom); | |
74 | hPtr2 = Tcl_CreateHashEntry(&dispPtr->atomTable, (char *) atom, | |
75 | &new); | |
76 | Tcl_SetHashValue(hPtr2, Tcl_GetHashKey(&dispPtr->nameTable, hPtr)); | |
77 | } | |
78 | return (Atom) Tcl_GetHashValue(hPtr); | |
79 | } | |
80 | \f | |
81 | /* | |
82 | *-------------------------------------------------------------- | |
83 | * | |
84 | * Tk_GetAtomName -- | |
85 | * | |
86 | * This procedure is equivalent to XGetAtomName except that | |
87 | * it uses the local atom cache to avoid contacting the | |
88 | * server. | |
89 | * | |
90 | * Results: | |
91 | * The return value is a character string corresponding to | |
92 | * the atom given by "atom". This string's storage space | |
93 | * is static: it need not be freed by the caller, and should | |
94 | * not be modified by the caller. If "atom" doesn't exist | |
95 | * on tkwin's display, then the string "?bad atom?" is returned. | |
96 | * | |
97 | * Side effects: | |
98 | * None. | |
99 | * | |
100 | *-------------------------------------------------------------- | |
101 | */ | |
102 | ||
103 | char * | |
104 | Tk_GetAtomName(tkwin, atom) | |
105 | Tk_Window tkwin; /* Window token; map atom to name | |
106 | * relative to this window's | |
107 | * display. */ | |
108 | Atom atom; /* Atom whose name is wanted. */ | |
109 | { | |
110 | register TkDisplay *dispPtr; | |
111 | register Tcl_HashEntry *hPtr; | |
112 | ||
113 | dispPtr = ((TkWindow *) tkwin)->dispPtr; | |
114 | if (!dispPtr->atomInit) { | |
115 | AtomInit(dispPtr); | |
116 | } | |
117 | ||
118 | hPtr = Tcl_FindHashEntry(&dispPtr->atomTable, (char *) atom); | |
119 | if (hPtr == NULL) { | |
120 | char *name; | |
121 | Tk_ErrorHandler handler; | |
122 | int new; | |
123 | ||
124 | handler= Tk_CreateErrorHandler(dispPtr->display, BadAtom, | |
125 | -1, -1, (int (*)()) NULL, (ClientData) NULL); | |
126 | name = XGetAtomName(dispPtr->display, atom); | |
127 | if (name == NULL) { | |
128 | name = "?bad atom?"; | |
129 | } | |
130 | Tk_DeleteErrorHandler(handler); | |
131 | hPtr = Tcl_CreateHashEntry(&dispPtr->nameTable, (char *) name, | |
132 | &new); | |
133 | Tcl_SetHashValue(hPtr, atom); | |
134 | name = Tcl_GetHashKey(&dispPtr->nameTable, hPtr); | |
135 | hPtr = Tcl_CreateHashEntry(&dispPtr->atomTable, (char *) atom, | |
136 | &new); | |
137 | Tcl_SetHashValue(hPtr, name); | |
138 | } | |
139 | return (char *) Tcl_GetHashValue(hPtr); | |
140 | } | |
141 | \f | |
142 | /* | |
143 | *-------------------------------------------------------------- | |
144 | * | |
145 | * AtomInit -- | |
146 | * | |
147 | * Initialize atom-related information for a display. | |
148 | * | |
149 | * Results: | |
150 | * None. | |
151 | * | |
152 | * Side effects: | |
153 | * Tables get initialized, etc. etc.. | |
154 | * | |
155 | *-------------------------------------------------------------- | |
156 | */ | |
157 | ||
158 | static void | |
159 | AtomInit(dispPtr) | |
160 | register TkDisplay *dispPtr; /* Display to initialize. */ | |
161 | { | |
162 | dispPtr->atomInit = 1; | |
163 | Tcl_InitHashTable(&dispPtr->nameTable, TCL_STRING_KEYS); | |
164 | Tcl_InitHashTable(&dispPtr->atomTable, TCL_ONE_WORD_KEYS); | |
165 | } |