1 /**
2  *
3  * $Id: AtomMgr.c,v 1.1 2004/08/28 19:22:43 dannybackx Exp $
4  *
5  * Copyright (C) 1995 Free Software Foundation, Inc.
6  * Copyright (C) 1995-2001 LessTif Development Team
7  *
8  * This file is part of the GNU LessTif Library.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  **/
25 
26 static const char rcsid[] = "$Id: AtomMgr.c,v 1.1 2004/08/28 19:22:43 dannybackx Exp $";
27 
28 #include <LTconfig.h>
29 
30 #include <XmI/XmI.h>
31 #include <Xm/AtomMgr.h>
32 #include <XmI/AtomMgrI.h>
33 #include <Xm/XmP.h>
34 
35 #include <XmI/DebugUtil.h>
36 
37 
38 static XContext nameToAtom = (XContext)0;
39 static XContext atomToName = (XContext)0;
40 
41 /*
42  * this function is now dead
43  */
44 void
_XmFlushAtomsForDisplay(Display * display)45 _XmFlushAtomsForDisplay(Display *display)
46 {
47 }
48 
49 /*
50  * save the atom in a quark derived from the name
51  * Note that we don't bother to go to the server here...
52  */
53 void
_XmInternAtomAndName(Display * display,Atom atom,String name)54 _XmInternAtomAndName(Display *display, Atom atom, String name)
55 {
56     Atom a;
57     XrmQuark q;
58 
59     if (nameToAtom == (XContext)0)
60     {
61 	nameToAtom = XUniqueContext();
62     }
63 
64     if (atomToName == (XContext)0)
65     {
66 	atomToName = XUniqueContext();
67     }
68 
69     q = XrmStringToQuark(name);
70 
71     if (XFindContext(display, q, nameToAtom, (XPointer *)&a) != XCSUCCESS)
72     {
73 	XSaveContext(display, q, nameToAtom, (XPointer)atom);
74 
75 	XSaveContext(display, q, atomToName, (XPointer)atom);
76     }
77 }
78 
79 /*
80  * save the atom in a quark derived from the name
81  * Unlike InternAtomAndName, we do go to the server if it's not already
82  * defined.
83  */
84 Atom
XmInternAtom(Display * display,String name,Boolean only_if_exists)85 XmInternAtom(Display *display, String name, Boolean only_if_exists)
86 {
87     static Boolean initted = False;
88     XrmQuark q;
89     Atom ret;
90 
91     if (name == NULL)
92     {
93 	return None;
94     }
95 
96     if (!initted)
97     {
98 	initted = True;
99 	_XmInitAtomPairs(display);
100     }
101 
102     if (nameToAtom == (XContext)0)
103     {
104 	nameToAtom = XUniqueContext();
105     }
106 
107     if (atomToName == (XContext)0)
108     {
109 	atomToName = XUniqueContext();
110     }
111 
112     q = XrmStringToQuark(name);
113 
114     if (XFindContext(display, q, nameToAtom, (XPointer *)&ret) != XCSUCCESS)
115     {
116 
117 	ret = XInternAtom(display, name, only_if_exists);
118 
119 	if (only_if_exists && ret == None)
120 	{
121 	    return None;
122 	}
123 
124 	XSaveContext(display, q, nameToAtom, (XPointer)ret);
125 
126 	XSaveContext(display, q, atomToName, (XPointer)ret);
127     }
128 
129     return ret;
130 }
131 
132 /*
133  * get the name from a quark associated with the atom.
134  * Will go to the server if not already internal.
135  */
136 String
XmGetAtomName(Display * display,Atom atom)137 XmGetAtomName(Display *display, Atom atom)
138 {
139     XrmQuark q;
140     String ret, tmp;
141 
142     if (nameToAtom == (XContext)0)
143     {
144 	nameToAtom = XUniqueContext();
145     }
146 
147     if (XFindContext(display, atom, atomToName, (XPointer *)&q) != XCSUCCESS)
148     {
149 	ret = XGetAtomName(display, atom);
150 
151 	q = XrmStringToQuark(ret);
152 
153 	XSaveContext(display, q, nameToAtom, (XPointer)atom);
154 
155 	XSaveContext(display, q, atomToName, (XPointer)atom);
156 
157 	if (ret != NULL)
158 	{
159 	    tmp = ret;
160 
161 	    ret = XtNewString(ret);
162 
163 	    XFree(tmp);
164 	}
165 
166 	return ret;
167     }
168 
169     ret = XrmQuarkToString(q);
170 
171     if (ret != NULL)
172     {
173 	tmp = ret;
174 
175 	ret = XtNewString(ret);
176 
177 	XFree(tmp);
178     }
179 
180     return ret;
181 }
182