]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsres.cpp
Compile fixes.
[wxWidgets.git] / src / gtk / utilsres.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
c801d85f 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
a3622daa 7// Licence: wxWindows licence
c801d85f
KB
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"
c693edf3 18#include "wx/log.h"
c801d85f
KB
19
20#include <ctype.h>
21#include <string.h>
22#include <unistd.h>
c693edf3
RR
23//#ifdef __SVR4__
24//#include <sys/systeminfo.h>
25//#endif
c801d85f
KB
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
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__
05939a81 42 #define DEFAULT_XRESOURCE_DIR _T("/usr/openwin/lib/app-defaults")
c801d85f 43#else
05939a81 44 #define DEFAULT_XRESOURCE_DIR _T("/usr/lib/X11/app-defaults")
c801d85f
KB
45#endif
46
47//-----------------------------------------------------------------------------
48// glabal data (data.cpp)
49//-----------------------------------------------------------------------------
50
a3622daa 51extern wxResourceCache *wxTheResourceCache;
c801d85f
KB
52extern XrmDatabase wxResourceDatabase;
53
54//-----------------------------------------------------------------------------
55// utility functions for get/write resources
56//-----------------------------------------------------------------------------
57
05939a81 58static wxChar *GetResourcePath(wxChar *buf, wxChar *name, bool create)
c801d85f 59{
3e61c765
RR
60 if (create && FileExists(name))
61 {
05939a81 62 wxStrcpy(buf, name);
a3622daa 63 return buf; // Exists so ...
c801d85f 64 }
05939a81
OK
65 if (*name == _T('/'))
66 wxStrcpy(buf, name);
3e61c765
RR
67 else
68 {
a3622daa 69 // Put in standard place for resource files if not absolute
05939a81
OK
70 wxStrcpy(buf, DEFAULT_XRESOURCE_DIR);
71 wxStrcat(buf, _T("/"));
72 wxStrcat(buf, FileNameFromPath(name));
c801d85f 73 }
3e61c765
RR
74 if (create)
75 {
a3622daa 76 // Touch the file to create it
dcf924a3 77 FILE *fd = fopen(wxConvCurrent->cWX2MB(buf), "w");
a3622daa 78 if (fd) fclose(fd);
c801d85f
KB
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.
05939a81 86static wxChar *GetIniFile(wxChar *dest, const wxChar *filename)
c801d85f 87{
2aaf2049 88 const wxChar *home = (const wxChar *) NULL;
a3622daa 89 if (filename && wxIsAbsolutePath(filename))
c801d85f 90 {
05939a81 91 wxStrcpy(dest, filename);
a3622daa 92 }
c801d85f
KB
93 else
94 {
a3622daa 95 if ((home = wxGetUserHome(wxString())) != NULL)
c801d85f 96 {
05939a81
OK
97 wxStrcpy(dest, home);
98 if (dest[wxStrlen(dest) - 1] != _T('/')) wxStrcat(dest, _T("/"));
a3622daa
VZ
99 if (filename == NULL)
100 {
05939a81 101 if ((filename = wxGetenv(_T("XENVIRONMENT"))) == NULL) filename = _T(".Xdefaults");
a3622daa
VZ
102 }
103 else
05939a81
OK
104 if (*filename != _T('.')) wxStrcat(dest, _T("."));
105 wxStrcat(dest, filename);
c801d85f 106 }
a3622daa 107 else
c801d85f 108 {
05939a81 109 dest[0] = _T('\0');
c801d85f
KB
110 }
111 }
112 return dest;
113}
114
8bbe427f 115static void wxXMergeDatabases()
c801d85f
KB
116{
117 XrmDatabase homeDB, serverDB, applicationDB;
05939a81 118 wxChar filenamebuf[1024];
c801d85f 119
05939a81
OK
120 wxChar *filename = &filenamebuf[0];
121 wxChar *environment;
c801d85f
KB
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
a3622daa 127 // Get application defaults file, if any
c801d85f 128 if ((applicationDB = XrmGetFileDatabase(name)))
a3622daa 129 (void)XrmMergeDatabases(applicationDB, &wxResourceDatabase);
c801d85f
KB
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
3e61c765
RR
135 if (XResourceManagerString(GDK_DISPLAY()) != NULL)
136 {
a3622daa 137 serverDB = XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY()));
3e61c765
RR
138 }
139 else
140 {
05939a81 141 (void)GetIniFile(filename, (wxChar *) NULL);
dcf924a3 142 serverDB = XrmGetFileDatabase(wxConvCurrent->cWX2MB(filename));
c801d85f
KB
143 }
144 if (serverDB)
a3622daa 145 XrmMergeDatabases(serverDB, &wxResourceDatabase);
c801d85f
KB
146
147 // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
148 // and merge into existing database
149
05939a81 150 if ((environment = wxGetenv(_T("XENVIRONMENT"))) == NULL)
3e61c765 151 {
a3622daa 152 size_t len;
05939a81
OK
153#if wxUSE_UNICODE
154 char hostbuf[1024];
155#endif
156 environment = GetIniFile(filename, (const wxChar *) NULL);
157 len = wxStrlen(environment);
c801d85f 158#if !defined(SVR4) || defined(__sgi)
05939a81
OK
159#if wxUSE_UNICODE
160 (void)gethostname(hostbuf, 1024 - len);
161#else
a3622daa 162 (void)gethostname(environment + len, 1024 - len);
05939a81
OK
163#endif
164#else
165#if wxUSE_UNICODE
166 (void)sysinfo(SI_HOSTNAME, hostbuf, 1024 - len);
c801d85f 167#else
a3622daa 168 (void)sysinfo(SI_HOSTNAME, environment + len, 1024 - len);
05939a81
OK
169#endif
170#endif
171#if wxUSE_UNICODE
dcf924a3 172 wxStrcat(environment, wxConvCurrent->cMB2WX(hostbuf));
c801d85f
KB
173#endif
174 }
dcf924a3 175 if ((homeDB = XrmGetFileDatabase(wxConvCurrent->cWX2MB(environment))))
a3622daa 176 XrmMergeDatabases(homeDB, &wxResourceDatabase);
c801d85f
KB
177}
178
179//-----------------------------------------------------------------------------
180// called on application exit
181//-----------------------------------------------------------------------------
182
8bbe427f 183void wxFlushResources()
c801d85f 184{
05939a81 185 wxChar nameBuffer[512];
c801d85f 186
a3622daa 187 wxNode *node = wxTheResourceCache->First();
c801d85f 188 while (node) {
7f985bd3 189 wxString str = node->GetKeyString();
05939a81 190 wxChar *file = WXSTRINGCAST str;
a3622daa
VZ
191 // If file doesn't exist, create it first.
192 (void)GetResourcePath(nameBuffer, file, TRUE);
193
194 XrmDatabase database = (XrmDatabase)node->Data();
dcf924a3 195 XrmPutFileDatabase(database, wxConvCurrent->cWX2MB(nameBuffer));
a3622daa
VZ
196 XrmDestroyDatabase(database);
197 wxNode *next = node->Next();
198// delete node;
199 node = next;
c801d85f
KB
200 }
201}
202
05939a81 203void wxDeleteResources(const wxChar *file)
c801d85f 204{
05939a81
OK
205 wxLogTrace(wxTraceResAlloc, _T("Delete: Number = %d"), wxTheResourceCache->Number());
206 wxChar buffer[500];
c801d85f
KB
207 (void)GetIniFile(buffer, file);
208
a3622daa 209 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 210 if (node) {
a3622daa
VZ
211 XrmDatabase database = (XrmDatabase)node->Data();
212 XrmDestroyDatabase(database);
213// delete node;
c801d85f
KB
214 }
215}
216
217//-----------------------------------------------------------------------------
218// resource functions
219//-----------------------------------------------------------------------------
220
221bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file )
222{
05939a81 223 wxChar buffer[500];
c801d85f
KB
224
225 if (!entry) return FALSE;
226
227 (void)GetIniFile(buffer, file);
228
229 XrmDatabase database;
a3622daa 230 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 231 if (node)
a3622daa 232 database = (XrmDatabase)node->Data();
c801d85f 233 else {
dcf924a3 234 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
05939a81 235 wxLogTrace(wxTraceResAlloc, _T("Write: Number = %d"), wxTheResourceCache->Number());
a3622daa 236 wxTheResourceCache->Append(buffer, (wxObject *)database);
c801d85f
KB
237 }
238 char resName[300];
05939a81 239 strcpy(resName, !section.IsNull() ? MBSTRINGCAST section.mb_str() : "wxWindows");
c801d85f 240 strcat(resName, ".");
05939a81
OK
241 strcat(resName, entry.mb_str());
242 XrmPutStringResource(&database, resName, value.mb_str());
c801d85f
KB
243 return TRUE;
244};
245
246bool 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
253bool 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
260bool 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
267bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file )
268{
269 if (!wxResourceDatabase)
a3622daa 270 wxXMergeDatabases();
c801d85f
KB
271
272 XrmDatabase database;
3e61c765
RR
273 if (!file.IsEmpty())
274 {
05939a81 275 wxChar buffer[500];
a3622daa
VZ
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
bbe0af5b 280 wxNode *node = (wxNode*) NULL; /* suppress egcs warning */
bedd04ac 281 node = wxTheResourceCache->Find(buffer);
a3622daa 282 if (node)
bedd04ac 283 {
a3622daa 284 database = (XrmDatabase)node->Data();
bedd04ac
VZ
285 }
286 else
287 {
dcf924a3 288 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
05939a81 289 wxLogTrace(wxTraceResAlloc, _T("Get: Number = %d"), wxTheResourceCache->Number());
a3622daa
VZ
290 wxTheResourceCache->Append(buffer, (wxObject *)database);
291 }
c801d85f 292 } else
a3622daa 293 database = wxResourceDatabase;
c801d85f
KB
294
295 XrmValue xvalue;
296 char *str_type[20];
297 char buf[150];
05939a81 298 strcpy(buf, section.mb_str());
c801d85f 299 strcat(buf, ".");
05939a81 300 strcat(buf, entry.mb_str());
c801d85f
KB
301
302 bool success = XrmGetResource(database, buf, "*", str_type, &xvalue);
303 // Try different combinations of upper/lower case, just in case...
3e61c765
RR
304 if (!success)
305 {
a3622daa
VZ
306 buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0]));
307 success = XrmGetResource(database, buf, "*", str_type, &xvalue);
c801d85f 308 }
3e61c765
RR
309 if (success)
310 {
a3622daa
VZ
311 if (*value)
312 delete[] *value;
313 *value = new char[xvalue.size + 1];
314 strncpy(*value, xvalue.addr, (int)xvalue.size);
315 return TRUE;
c801d85f
KB
316 }
317 return FALSE;
318};
319
320bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file )
321{
c67daf87 322 char *s = (char *) NULL;
c801d85f 323 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
324 if (succ)
325 {
c67daf87 326 *value = (float)strtod(s, (char **) NULL);
a3622daa
VZ
327 delete[]s;
328 return TRUE;
c801d85f 329 } else
a3622daa 330 return FALSE;
c801d85f
KB
331};
332
333bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file )
334{
c67daf87 335 char *s = (char *) NULL;
c801d85f 336 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
337 if (succ)
338 {
c67daf87 339 *value = strtol(s, (char **) NULL, 10);
a3622daa
VZ
340 delete[]s;
341 return TRUE;
c801d85f 342 } else
a3622daa 343 return FALSE;
c801d85f
KB
344};
345
346bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file )
347{
c67daf87 348 char *s = (char *) NULL;
c801d85f 349 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
350 if (succ)
351 {
a3622daa
VZ
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
c67daf87 361 *value = (int)strtol(s, (char **) NULL, 10);
a3622daa
VZ
362 delete[]s;
363 return TRUE;
c801d85f 364 } else
a3622daa 365 return FALSE;
c801d85f
KB
366};
367