]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
a3622daa | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | //#ifdef __GNUG__ | |
13 | //#pragma implementation "utils.h" | |
14 | //#endif | |
15 | ||
16 | #include "wx/utils.h" | |
17 | #include "wx/string.h" | |
18 | #include "wx/list.h" | |
19 | ||
20 | #include <ctype.h> | |
21 | #include <string.h> | |
22 | #include <unistd.h> | |
23 | #ifdef __SVR4__ | |
24 | #include <sys/systeminfo.h> | |
25 | #endif | |
26 | ||
27 | #include "gdk/gdkx.h" // GDK_DISPLAY | |
28 | #include "gdk/gdkprivate.h" // gdk_progclass | |
29 | ||
30 | #include <X11/Xlib.h> | |
31 | #include <X11/Xutil.h> | |
32 | #include <X11/Xresource.h> | |
33 | ||
a3622daa | 34 | #include "wx/log.h" |
c801d85f KB |
35 | |
36 | //----------------------------------------------------------------------------- | |
37 | // constants | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | // Yuck this is really BOTH site and platform dependent | |
41 | // so we should use some other strategy! | |
42 | #ifdef __SUN__ | |
43 | #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults" | |
44 | #else | |
45 | #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults" | |
46 | #endif | |
47 | ||
48 | //----------------------------------------------------------------------------- | |
49 | // glabal data (data.cpp) | |
50 | //----------------------------------------------------------------------------- | |
51 | ||
a3622daa | 52 | extern wxResourceCache *wxTheResourceCache; |
c801d85f KB |
53 | extern XrmDatabase wxResourceDatabase; |
54 | ||
55 | //----------------------------------------------------------------------------- | |
56 | // utility functions for get/write resources | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | static char *GetResourcePath(char *buf, char *name, bool create) | |
60 | { | |
61 | if (create && FileExists(name)) { | |
a3622daa VZ |
62 | strcpy(buf, name); |
63 | return buf; // Exists so ... | |
c801d85f KB |
64 | } |
65 | if (*name == '/') | |
a3622daa | 66 | strcpy(buf, name); |
c801d85f | 67 | else { |
a3622daa VZ |
68 | // Put in standard place for resource files if not absolute |
69 | strcpy(buf, DEFAULT_XRESOURCE_DIR); | |
70 | strcat(buf, "/"); | |
71 | strcat(buf, FileNameFromPath(name)); | |
c801d85f KB |
72 | } |
73 | if (create) { | |
a3622daa VZ |
74 | // Touch the file to create it |
75 | FILE *fd = fopen(buf, "w"); | |
76 | if (fd) fclose(fd); | |
c801d85f KB |
77 | } |
78 | return buf; | |
79 | } | |
80 | ||
81 | // Read $HOME for what it says is home, if not | |
82 | // read $USER or $LOGNAME for user name else determine | |
83 | // the Real User, then determine the Real home dir. | |
84 | static char *GetIniFile(char *dest, const char *filename) | |
85 | { | |
86 | char *home = NULL; | |
a3622daa | 87 | if (filename && wxIsAbsolutePath(filename)) |
c801d85f KB |
88 | { |
89 | strcpy(dest, filename); | |
a3622daa | 90 | } |
c801d85f KB |
91 | else |
92 | { | |
a3622daa | 93 | if ((home = wxGetUserHome(wxString())) != NULL) |
c801d85f | 94 | { |
a3622daa VZ |
95 | strcpy(dest, home); |
96 | if (dest[strlen(dest) - 1] != '/') strcat(dest, "/"); | |
97 | if (filename == NULL) | |
98 | { | |
99 | if ((filename = getenv("XENVIRONMENT")) == NULL) filename = ".Xdefaults"; | |
100 | } | |
101 | else | |
102 | if (*filename != '.') strcat(dest, "."); | |
103 | strcat(dest, filename); | |
c801d85f | 104 | } |
a3622daa | 105 | else |
c801d85f | 106 | { |
a3622daa | 107 | dest[0] = '\0'; |
c801d85f KB |
108 | } |
109 | } | |
110 | return dest; | |
111 | } | |
112 | ||
113 | static void wxXMergeDatabases(void) | |
114 | { | |
115 | XrmDatabase homeDB, serverDB, applicationDB; | |
116 | char filenamebuf[1024]; | |
117 | ||
118 | char *filename = &filenamebuf[0]; | |
119 | char *environment; | |
120 | char *classname = gdk_progclass; // Robert Roebling ?? | |
121 | char name[256]; | |
122 | (void)strcpy(name, "/usr/lib/X11/app-defaults/"); | |
123 | (void)strcat(name, classname ? classname : "wxWindows"); | |
124 | ||
a3622daa | 125 | // Get application defaults file, if any |
c801d85f | 126 | if ((applicationDB = XrmGetFileDatabase(name))) |
a3622daa | 127 | (void)XrmMergeDatabases(applicationDB, &wxResourceDatabase); |
c801d85f KB |
128 | |
129 | // Merge server defaults, created by xrdb, loaded as a property of the root | |
130 | // window when the server initializes and loaded into the display | |
131 | // structure on XOpenDisplay; | |
132 | // if not defined, use .Xdefaults | |
133 | if (XResourceManagerString(GDK_DISPLAY()) != NULL) { | |
a3622daa | 134 | serverDB = XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY())); |
c801d85f | 135 | } else { |
a3622daa VZ |
136 | (void)GetIniFile(filename, NULL); |
137 | serverDB = XrmGetFileDatabase(filename); | |
c801d85f KB |
138 | } |
139 | if (serverDB) | |
a3622daa | 140 | XrmMergeDatabases(serverDB, &wxResourceDatabase); |
c801d85f KB |
141 | |
142 | // Open XENVIRONMENT file, or if not defined, the .Xdefaults, | |
143 | // and merge into existing database | |
144 | ||
145 | if ((environment = getenv("XENVIRONMENT")) == NULL) { | |
a3622daa VZ |
146 | size_t len; |
147 | environment = GetIniFile(filename, NULL); | |
148 | len = strlen(environment); | |
c801d85f | 149 | #if !defined(SVR4) || defined(__sgi) |
a3622daa | 150 | (void)gethostname(environment + len, 1024 - len); |
c801d85f | 151 | #else |
a3622daa | 152 | (void)sysinfo(SI_HOSTNAME, environment + len, 1024 - len); |
c801d85f KB |
153 | #endif |
154 | } | |
155 | if ((homeDB = XrmGetFileDatabase(environment))) | |
a3622daa | 156 | XrmMergeDatabases(homeDB, &wxResourceDatabase); |
c801d85f KB |
157 | } |
158 | ||
159 | //----------------------------------------------------------------------------- | |
160 | // called on application exit | |
161 | //----------------------------------------------------------------------------- | |
162 | ||
163 | void wxFlushResources(void) | |
164 | { | |
165 | char nameBuffer[512]; | |
166 | ||
a3622daa | 167 | wxNode *node = wxTheResourceCache->First(); |
c801d85f | 168 | while (node) { |
a3622daa VZ |
169 | char *file = node->key.string; |
170 | // If file doesn't exist, create it first. | |
171 | (void)GetResourcePath(nameBuffer, file, TRUE); | |
172 | ||
173 | XrmDatabase database = (XrmDatabase)node->Data(); | |
174 | XrmPutFileDatabase(database, nameBuffer); | |
175 | XrmDestroyDatabase(database); | |
176 | wxNode *next = node->Next(); | |
177 | // delete node; | |
178 | node = next; | |
c801d85f KB |
179 | } |
180 | } | |
181 | ||
182 | void wxDeleteResources(const char *file) | |
183 | { | |
a3622daa | 184 | wxLogTrace(wxTraceResAlloc, "Delete: Number = %d", wxTheResourceCache->Number()); |
c801d85f KB |
185 | char buffer[500]; |
186 | (void)GetIniFile(buffer, file); | |
187 | ||
a3622daa | 188 | wxNode *node = wxTheResourceCache->Find(buffer); |
c801d85f | 189 | if (node) { |
a3622daa VZ |
190 | XrmDatabase database = (XrmDatabase)node->Data(); |
191 | XrmDestroyDatabase(database); | |
192 | // delete node; | |
c801d85f KB |
193 | } |
194 | } | |
195 | ||
196 | //----------------------------------------------------------------------------- | |
197 | // resource functions | |
198 | //----------------------------------------------------------------------------- | |
199 | ||
200 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file ) | |
201 | { | |
202 | char buffer[500]; | |
203 | ||
204 | if (!entry) return FALSE; | |
205 | ||
206 | (void)GetIniFile(buffer, file); | |
207 | ||
208 | XrmDatabase database; | |
a3622daa | 209 | wxNode *node = wxTheResourceCache->Find(buffer); |
c801d85f | 210 | if (node) |
a3622daa | 211 | database = (XrmDatabase)node->Data(); |
c801d85f | 212 | else { |
a3622daa VZ |
213 | database = XrmGetFileDatabase(buffer); |
214 | wxLogTrace(wxTraceResAlloc, "Write: Number = %d", wxTheResourceCache->Number()); | |
215 | wxTheResourceCache->Append(buffer, (wxObject *)database); | |
c801d85f KB |
216 | } |
217 | char resName[300]; | |
218 | strcpy(resName, !section.IsNull() ? WXSTRINGCAST section : "wxWindows"); | |
219 | strcat(resName, "."); | |
220 | strcat(resName, entry); | |
221 | XrmPutStringResource(&database, resName, value); | |
222 | return TRUE; | |
223 | }; | |
224 | ||
225 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file ) | |
226 | { | |
227 | char buf[50]; | |
228 | sprintf(buf, "%.4f", value); | |
229 | return wxWriteResource(section, entry, buf, file); | |
230 | }; | |
231 | ||
232 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file ) | |
233 | { | |
234 | char buf[50]; | |
235 | sprintf(buf, "%ld", value); | |
236 | return wxWriteResource(section, entry, buf, file); | |
237 | }; | |
238 | ||
239 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file ) | |
240 | { | |
241 | char buf[50]; | |
242 | sprintf(buf, "%d", value); | |
243 | return wxWriteResource(section, entry, buf, file); | |
244 | }; | |
245 | ||
246 | bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file ) | |
247 | { | |
248 | if (!wxResourceDatabase) | |
a3622daa | 249 | wxXMergeDatabases(); |
c801d85f KB |
250 | |
251 | XrmDatabase database; | |
252 | if (file) { | |
a3622daa VZ |
253 | char buffer[500]; |
254 | // Is this right? Trying to get it to look in the user's | |
255 | // home directory instead of current directory -- JACS | |
256 | (void)GetIniFile(buffer, file); | |
257 | ||
258 | wxNode *node = wxTheResourceCache->Find(buffer); | |
259 | if (node) | |
260 | database = (XrmDatabase)node->Data(); | |
261 | else { | |
262 | database = XrmGetFileDatabase(buffer); | |
263 | wxLogTrace(wxTraceResAlloc, "Get: Number = %d", wxTheResourceCache->Number()); | |
264 | wxTheResourceCache->Append(buffer, (wxObject *)database); | |
265 | } | |
c801d85f | 266 | } else |
a3622daa | 267 | database = wxResourceDatabase; |
c801d85f KB |
268 | |
269 | XrmValue xvalue; | |
270 | char *str_type[20]; | |
271 | char buf[150]; | |
272 | strcpy(buf, section); | |
273 | strcat(buf, "."); | |
274 | strcat(buf, entry); | |
275 | ||
276 | bool success = XrmGetResource(database, buf, "*", str_type, &xvalue); | |
277 | // Try different combinations of upper/lower case, just in case... | |
278 | if (!success) { | |
a3622daa VZ |
279 | buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0])); |
280 | success = XrmGetResource(database, buf, "*", str_type, &xvalue); | |
c801d85f KB |
281 | } |
282 | if (success) { | |
a3622daa VZ |
283 | if (*value) |
284 | delete[] *value; | |
285 | *value = new char[xvalue.size + 1]; | |
286 | strncpy(*value, xvalue.addr, (int)xvalue.size); | |
287 | return TRUE; | |
c801d85f KB |
288 | } |
289 | return FALSE; | |
290 | }; | |
291 | ||
292 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file ) | |
293 | { | |
294 | char *s = NULL; | |
295 | bool succ = wxGetResource(section, entry, &s, file); | |
296 | if (succ) { | |
a3622daa VZ |
297 | *value = (float)strtod(s, NULL); |
298 | delete[]s; | |
299 | return TRUE; | |
c801d85f | 300 | } else |
a3622daa | 301 | return FALSE; |
c801d85f KB |
302 | }; |
303 | ||
304 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file ) | |
305 | { | |
306 | char *s = NULL; | |
307 | bool succ = wxGetResource(section, entry, &s, file); | |
308 | if (succ) { | |
a3622daa VZ |
309 | *value = strtol(s, NULL, 10); |
310 | delete[]s; | |
311 | return TRUE; | |
c801d85f | 312 | } else |
a3622daa | 313 | return FALSE; |
c801d85f KB |
314 | }; |
315 | ||
316 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file ) | |
317 | { | |
318 | char *s = NULL; | |
319 | bool succ = wxGetResource(section, entry, &s, file); | |
320 | if (succ) { | |
a3622daa VZ |
321 | // Handle True, False here |
322 | // True, Yes, Enables, Set or Activated | |
323 | if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A') | |
324 | *value = TRUE; | |
325 | // False, No, Disabled, Reset, Cleared, Deactivated | |
326 | else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C') | |
327 | *value = FALSE; | |
328 | // Handle as Integer | |
329 | else | |
330 | *value = (int)strtol(s, NULL, 10); | |
331 | delete[]s; | |
332 | return TRUE; | |
c801d85f | 333 | } else |
a3622daa | 334 | return FALSE; |
c801d85f KB |
335 | }; |
336 |