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