]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsres.cpp
wxTopLevelWindows is now a wxWindowList, better compatibility with the old
[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"
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
a3622daa 33#include "wx/log.h"
c801d85f
KB
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 "/usr/openwin/lib/app-defaults"
43#else
44 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
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
58static char *GetResourcePath(char *buf, char *name, bool create)
59{
3e61c765
RR
60 if (create && FileExists(name))
61 {
a3622daa
VZ
62 strcpy(buf, name);
63 return buf; // Exists so ...
c801d85f
KB
64 }
65 if (*name == '/')
a3622daa 66 strcpy(buf, name);
3e61c765
RR
67 else
68 {
a3622daa
VZ
69 // Put in standard place for resource files if not absolute
70 strcpy(buf, DEFAULT_XRESOURCE_DIR);
71 strcat(buf, "/");
72 strcat(buf, FileNameFromPath(name));
c801d85f 73 }
3e61c765
RR
74 if (create)
75 {
a3622daa
VZ
76 // Touch the file to create it
77 FILE *fd = fopen(buf, "w");
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.
86static char *GetIniFile(char *dest, const char *filename)
87{
c67daf87 88 char *home = (char *) NULL;
a3622daa 89 if (filename && wxIsAbsolutePath(filename))
c801d85f
KB
90 {
91 strcpy(dest, filename);
a3622daa 92 }
c801d85f
KB
93 else
94 {
a3622daa 95 if ((home = wxGetUserHome(wxString())) != NULL)
c801d85f 96 {
a3622daa
VZ
97 strcpy(dest, home);
98 if (dest[strlen(dest) - 1] != '/') strcat(dest, "/");
99 if (filename == NULL)
100 {
101 if ((filename = getenv("XENVIRONMENT")) == NULL) filename = ".Xdefaults";
102 }
103 else
104 if (*filename != '.') strcat(dest, ".");
105 strcat(dest, filename);
c801d85f 106 }
a3622daa 107 else
c801d85f 108 {
a3622daa 109 dest[0] = '\0';
c801d85f
KB
110 }
111 }
112 return dest;
113}
114
8bbe427f 115static void wxXMergeDatabases()
c801d85f
KB
116{
117 XrmDatabase homeDB, serverDB, applicationDB;
118 char filenamebuf[1024];
119
120 char *filename = &filenamebuf[0];
121 char *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
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 {
c67daf87 141 (void)GetIniFile(filename, (char *) NULL);
a3622daa 142 serverDB = XrmGetFileDatabase(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
3e61c765
RR
150 if ((environment = getenv("XENVIRONMENT")) == NULL)
151 {
a3622daa 152 size_t len;
c67daf87 153 environment = GetIniFile(filename, (const char *) NULL);
a3622daa 154 len = strlen(environment);
c801d85f 155#if !defined(SVR4) || defined(__sgi)
a3622daa 156 (void)gethostname(environment + len, 1024 - len);
c801d85f 157#else
a3622daa 158 (void)sysinfo(SI_HOSTNAME, environment + len, 1024 - len);
c801d85f
KB
159#endif
160 }
161 if ((homeDB = XrmGetFileDatabase(environment)))
a3622daa 162 XrmMergeDatabases(homeDB, &wxResourceDatabase);
c801d85f
KB
163}
164
165//-----------------------------------------------------------------------------
166// called on application exit
167//-----------------------------------------------------------------------------
168
8bbe427f 169void wxFlushResources()
c801d85f
KB
170{
171 char nameBuffer[512];
172
a3622daa 173 wxNode *node = wxTheResourceCache->First();
c801d85f 174 while (node) {
7f985bd3
RR
175 wxString str = node->GetKeyString();
176 char *file = WXSTRINGCAST str;
a3622daa
VZ
177 // If file doesn't exist, create it first.
178 (void)GetResourcePath(nameBuffer, file, TRUE);
179
180 XrmDatabase database = (XrmDatabase)node->Data();
181 XrmPutFileDatabase(database, nameBuffer);
182 XrmDestroyDatabase(database);
183 wxNode *next = node->Next();
184// delete node;
185 node = next;
c801d85f
KB
186 }
187}
188
189void wxDeleteResources(const char *file)
190{
a3622daa 191 wxLogTrace(wxTraceResAlloc, "Delete: Number = %d", wxTheResourceCache->Number());
c801d85f
KB
192 char buffer[500];
193 (void)GetIniFile(buffer, file);
194
a3622daa 195 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 196 if (node) {
a3622daa
VZ
197 XrmDatabase database = (XrmDatabase)node->Data();
198 XrmDestroyDatabase(database);
199// delete node;
c801d85f
KB
200 }
201}
202
203//-----------------------------------------------------------------------------
204// resource functions
205//-----------------------------------------------------------------------------
206
207bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file )
208{
209 char buffer[500];
210
211 if (!entry) return FALSE;
212
213 (void)GetIniFile(buffer, file);
214
215 XrmDatabase database;
a3622daa 216 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 217 if (node)
a3622daa 218 database = (XrmDatabase)node->Data();
c801d85f 219 else {
a3622daa
VZ
220 database = XrmGetFileDatabase(buffer);
221 wxLogTrace(wxTraceResAlloc, "Write: Number = %d", wxTheResourceCache->Number());
222 wxTheResourceCache->Append(buffer, (wxObject *)database);
c801d85f
KB
223 }
224 char resName[300];
225 strcpy(resName, !section.IsNull() ? WXSTRINGCAST section : "wxWindows");
226 strcat(resName, ".");
227 strcat(resName, entry);
228 XrmPutStringResource(&database, resName, value);
229 return TRUE;
230};
231
232bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file )
233{
234 char buf[50];
235 sprintf(buf, "%.4f", value);
236 return wxWriteResource(section, entry, buf, file);
237};
238
239bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file )
240{
241 char buf[50];
242 sprintf(buf, "%ld", value);
243 return wxWriteResource(section, entry, buf, file);
244};
245
246bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file )
247{
248 char buf[50];
249 sprintf(buf, "%d", value);
250 return wxWriteResource(section, entry, buf, file);
251};
252
253bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file )
254{
255 if (!wxResourceDatabase)
a3622daa 256 wxXMergeDatabases();
c801d85f
KB
257
258 XrmDatabase database;
3e61c765
RR
259 if (!file.IsEmpty())
260 {
a3622daa
VZ
261 char buffer[500];
262 // Is this right? Trying to get it to look in the user's
263 // home directory instead of current directory -- JACS
264 (void)GetIniFile(buffer, file);
265
bbe0af5b 266 wxNode *node = (wxNode*) NULL; /* suppress egcs warning */
bedd04ac 267 node = wxTheResourceCache->Find(buffer);
a3622daa 268 if (node)
bedd04ac 269 {
a3622daa 270 database = (XrmDatabase)node->Data();
bedd04ac
VZ
271 }
272 else
273 {
a3622daa
VZ
274 database = XrmGetFileDatabase(buffer);
275 wxLogTrace(wxTraceResAlloc, "Get: Number = %d", wxTheResourceCache->Number());
276 wxTheResourceCache->Append(buffer, (wxObject *)database);
277 }
c801d85f 278 } else
a3622daa 279 database = wxResourceDatabase;
c801d85f
KB
280
281 XrmValue xvalue;
282 char *str_type[20];
283 char buf[150];
284 strcpy(buf, section);
285 strcat(buf, ".");
286 strcat(buf, entry);
287
288 bool success = XrmGetResource(database, buf, "*", str_type, &xvalue);
289 // Try different combinations of upper/lower case, just in case...
3e61c765
RR
290 if (!success)
291 {
a3622daa
VZ
292 buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0]));
293 success = XrmGetResource(database, buf, "*", str_type, &xvalue);
c801d85f 294 }
3e61c765
RR
295 if (success)
296 {
a3622daa
VZ
297 if (*value)
298 delete[] *value;
299 *value = new char[xvalue.size + 1];
300 strncpy(*value, xvalue.addr, (int)xvalue.size);
301 return TRUE;
c801d85f
KB
302 }
303 return FALSE;
304};
305
306bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file )
307{
c67daf87 308 char *s = (char *) NULL;
c801d85f 309 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
310 if (succ)
311 {
c67daf87 312 *value = (float)strtod(s, (char **) NULL);
a3622daa
VZ
313 delete[]s;
314 return TRUE;
c801d85f 315 } else
a3622daa 316 return FALSE;
c801d85f
KB
317};
318
319bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file )
320{
c67daf87 321 char *s = (char *) NULL;
c801d85f 322 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
323 if (succ)
324 {
c67daf87 325 *value = strtol(s, (char **) NULL, 10);
a3622daa
VZ
326 delete[]s;
327 return TRUE;
c801d85f 328 } else
a3622daa 329 return FALSE;
c801d85f
KB
330};
331
332bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file )
333{
c67daf87 334 char *s = (char *) NULL;
c801d85f 335 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
336 if (succ)
337 {
a3622daa
VZ
338 // Handle True, False here
339 // True, Yes, Enables, Set or Activated
340 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
341 *value = TRUE;
342 // False, No, Disabled, Reset, Cleared, Deactivated
343 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
344 *value = FALSE;
345 // Handle as Integer
346 else
c67daf87 347 *value = (int)strtol(s, (char **) NULL, 10);
a3622daa
VZ
348 delete[]s;
349 return TRUE;
c801d85f 350 } else
a3622daa 351 return FALSE;
c801d85f
KB
352};
353