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