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