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