many changes; major ones:
[wxWidgets.git] / src / gtk1 / utilsres.cpp
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 T("/usr/openwin/lib/app-defaults")
44 #else
45 #define DEFAULT_XRESOURCE_DIR T("/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 == T('/'))
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, T("/"));
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] != T('/')) wxStrcat(dest, T("/"));
100 if (filename == NULL)
101 {
102 if ((filename = wxGetenv(T("XENVIRONMENT("))) == NULL) filename = T(".Xdefaults");
103 }
104 else
105 if (*filename != T('.')) wxStrcat(dest, T("."));
106 wxStrcat(dest, filename);
107 }
108 else
109 {
110 dest[0] = T('\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(T("XENVIRONMENT("))) == NULL)
152 {
153 size_t len;
154 #if wxUSE_UNICODE
155 char hostbuf[1024];
156 #endif
157 environment = GetIniFile(filename, (const wxChar *) NULL);
158 len = wxStrlen(environment);
159 #if !defined(SVR4) || defined(__sgi)
160 #if wxUSE_UNICODE
161 (void)gethostname(hostbuf, 1024 - len);
162 #else
163 (void)gethostname(environment + len, 1024 - len);
164 #endif
165 #else
166 #if wxUSE_UNICODE
167 (void)sysinfo(SI_HOSTNAME, hostbuf, 1024 - len);
168 #else
169 (void)sysinfo(SI_HOSTNAME, environment + len, 1024 - len);
170 #endif
171 #endif
172 #if wxUSE_UNICODE
173 wxStrcat(environment, wxConvCurrent->cMB2WX(hostbuf));
174 #endif
175 }
176 if ((homeDB = XrmGetFileDatabase(wxConvCurrent->cWX2MB(environment))))
177 XrmMergeDatabases(homeDB, &wxResourceDatabase);
178 }
179
180 //-----------------------------------------------------------------------------
181 // called on application exit
182 //-----------------------------------------------------------------------------
183
184 void wxFlushResources()
185 {
186 wxChar nameBuffer[512];
187
188 wxNode *node = wxTheResourceCache->First();
189 while (node) {
190 wxString str = node->GetKeyString();
191 wxChar *file = WXSTRINGCAST str;
192 // If file doesn't exist, create it first.
193 (void)GetResourcePath(nameBuffer, file, TRUE);
194
195 XrmDatabase database = (XrmDatabase)node->Data();
196 XrmPutFileDatabase(database, wxConvCurrent->cWX2MB(nameBuffer));
197 XrmDestroyDatabase(database);
198 wxNode *next = node->Next();
199 // delete node;
200 node = next;
201 }
202 }
203
204 void wxDeleteResources(const wxChar *file)
205 {
206 wxLogTrace(wxTraceResAlloc, T("Delete: Number = %d"), wxTheResourceCache->Number());
207 wxChar buffer[500];
208 (void)GetIniFile(buffer, file);
209
210 wxNode *node = wxTheResourceCache->Find(buffer);
211 if (node) {
212 XrmDatabase database = (XrmDatabase)node->Data();
213 XrmDestroyDatabase(database);
214 // delete node;
215 }
216 }
217
218 //-----------------------------------------------------------------------------
219 // resource functions
220 //-----------------------------------------------------------------------------
221
222 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file )
223 {
224 wxChar buffer[500];
225
226 if (!entry) return FALSE;
227
228 (void)GetIniFile(buffer, file);
229
230 XrmDatabase database;
231 wxNode *node = wxTheResourceCache->Find(buffer);
232 if (node)
233 database = (XrmDatabase)node->Data();
234 else {
235 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
236 wxLogTrace(wxTraceResAlloc, T("Write: Number = %d"), wxTheResourceCache->Number());
237 wxTheResourceCache->Append(buffer, (wxObject *)database);
238 }
239 char resName[300];
240 strcpy(resName, !section.IsNull() ? wxMBSTRINGCAST section.mb_str() : "wxWindows");
241 strcat(resName, ".");
242 strcat(resName, entry.mb_str());
243 XrmPutStringResource(&database, resName, value.mb_str());
244 return TRUE;
245 };
246
247 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file )
248 {
249 char buf[50];
250 sprintf(buf, "%.4f", value);
251 return wxWriteResource(section, entry, buf, file);
252 };
253
254 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file )
255 {
256 char buf[50];
257 sprintf(buf, "%ld", value);
258 return wxWriteResource(section, entry, buf, file);
259 };
260
261 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file )
262 {
263 char buf[50];
264 sprintf(buf, "%d", value);
265 return wxWriteResource(section, entry, buf, file);
266 };
267
268 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file )
269 {
270 if (!wxResourceDatabase)
271 wxXMergeDatabases();
272
273 XrmDatabase database;
274 if (!file.IsEmpty())
275 {
276 wxChar buffer[500];
277 // Is this right? Trying to get it to look in the user's
278 // home directory instead of current directory -- JACS
279 (void)GetIniFile(buffer, file);
280
281 wxNode *node = (wxNode*) NULL; /* suppress egcs warning */
282 node = wxTheResourceCache->Find(buffer);
283 if (node)
284 {
285 database = (XrmDatabase)node->Data();
286 }
287 else
288 {
289 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
290 wxLogTrace(wxTraceResAlloc, T("Get: Number = %d"), wxTheResourceCache->Number());
291 wxTheResourceCache->Append(buffer, (wxObject *)database);
292 }
293 } else
294 database = wxResourceDatabase;
295
296 XrmValue xvalue;
297 char *str_type[20];
298 char buf[150];
299 strcpy(buf, section.mb_str());
300 strcat(buf, ".");
301 strcat(buf, entry.mb_str());
302
303 bool success = XrmGetResource(database, buf, "*", str_type, &xvalue);
304 // Try different combinations of upper/lower case, just in case...
305 if (!success)
306 {
307 buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0]));
308 success = XrmGetResource(database, buf, "*", str_type, &xvalue);
309 }
310 if (success)
311 {
312 if (*value)
313 delete[] *value;
314 *value = new char[xvalue.size + 1];
315 strncpy(*value, xvalue.addr, (int)xvalue.size);
316 return TRUE;
317 }
318 return FALSE;
319 };
320
321 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file )
322 {
323 char *s = (char *) NULL;
324 bool succ = wxGetResource(section, entry, &s, file);
325 if (succ)
326 {
327 *value = (float)strtod(s, (char **) NULL);
328 delete[]s;
329 return TRUE;
330 } else
331 return FALSE;
332 };
333
334 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file )
335 {
336 char *s = (char *) NULL;
337 bool succ = wxGetResource(section, entry, &s, file);
338 if (succ)
339 {
340 *value = strtol(s, (char **) NULL, 10);
341 delete[]s;
342 return TRUE;
343 } else
344 return FALSE;
345 };
346
347 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file )
348 {
349 char *s = (char *) NULL;
350 bool succ = wxGetResource(section, entry, &s, file);
351 if (succ)
352 {
353 // Handle True, False here
354 // True, Yes, Enables, Set or Activated
355 if (*s == 'T(' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
356 *value = TRUE;
357 // False, No, Disabled, Reset, Cleared, Deactivated
358 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
359 *value = FALSE;
360 // Handle as Integer
361 else
362 *value = (int)strtol(s, (char **) NULL, 10);
363 delete[]s;
364 return TRUE;
365 } else
366 return FALSE;
367 };
368