]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/utilsres.cpp
support for vertical toolbars under GTK
[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 43# define DEFAULT_XRESOURCE_DIR wxT("/usr/openwin/lib/app-defaults")
c801d85f 44#else
6ed6d123 45# define DEFAULT_XRESOURCE_DIR wxT("/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 }
223d09f6 66 if (*name == wxT('/'))
05939a81 67 wxStrcpy(buf, name);
3e61c765
RR
68 else
69 {
a3622daa 70 // Put in standard place for resource files if not absolute
05939a81 71 wxStrcpy(buf, DEFAULT_XRESOURCE_DIR);
223d09f6 72 wxStrcat(buf, wxT("/"));
05939a81 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 98 wxStrcpy(dest, home);
223d09f6 99 if (dest[wxStrlen(dest) - 1] != wxT('/')) wxStrcat(dest, wxT("/"));
a3622daa
VZ
100 if (filename == NULL)
101 {
223d09f6 102 if ((filename = wxGetenv(wxT("XENVIRONMENT"))) == NULL) filename = wxT(".Xdefaults");
a3622daa
VZ
103 }
104 else
223d09f6 105 if (*filename != wxT('.')) wxStrcat(dest, wxT("."));
05939a81 106 wxStrcat(dest, filename);
c801d85f 107 }
a3622daa 108 else
c801d85f 109 {
223d09f6 110 dest[0] = wxT('\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
223d09f6 151 if ((environment = wxGetenv(wxT("XENVIRONMENT"))) == NULL)
3e61c765 152 {
05939a81 153 environment = GetIniFile(filename, (const wxChar *) NULL);
837e96fc
VZ
154 size_t len = wxStrlen(environment);
155
5c9ea14d 156 wxChar hostbuf[1024];
837e96fc
VZ
157 (void)wxGetHostName(hostbuf, WXSIZEOF(hostbuf) - len);
158
5c9ea14d 159 wxStrcat(environment, hostbuf);
c801d85f 160 }
837e96fc 161
dcf924a3 162 if ((homeDB = XrmGetFileDatabase(wxConvCurrent->cWX2MB(environment))))
a3622daa 163 XrmMergeDatabases(homeDB, &wxResourceDatabase);
c801d85f
KB
164}
165
166//-----------------------------------------------------------------------------
167// called on application exit
168//-----------------------------------------------------------------------------
169
8bbe427f 170void wxFlushResources()
c801d85f 171{
05939a81 172 wxChar nameBuffer[512];
c801d85f 173
a3622daa 174 wxNode *node = wxTheResourceCache->First();
c801d85f 175 while (node) {
7f985bd3 176 wxString str = node->GetKeyString();
05939a81 177 wxChar *file = WXSTRINGCAST str;
a3622daa
VZ
178 // If file doesn't exist, create it first.
179 (void)GetResourcePath(nameBuffer, file, TRUE);
180
181 XrmDatabase database = (XrmDatabase)node->Data();
dcf924a3 182 XrmPutFileDatabase(database, wxConvCurrent->cWX2MB(nameBuffer));
a3622daa
VZ
183 XrmDestroyDatabase(database);
184 wxNode *next = node->Next();
185// delete node;
186 node = next;
c801d85f
KB
187 }
188}
189
05939a81 190void wxDeleteResources(const wxChar *file)
c801d85f 191{
223d09f6 192 wxLogTrace(wxTraceResAlloc, wxT("Delete: Number = %d"), wxTheResourceCache->Number());
05939a81 193 wxChar buffer[500];
c801d85f
KB
194 (void)GetIniFile(buffer, file);
195
a3622daa 196 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 197 if (node) {
a3622daa
VZ
198 XrmDatabase database = (XrmDatabase)node->Data();
199 XrmDestroyDatabase(database);
200// delete node;
c801d85f
KB
201 }
202}
203
204//-----------------------------------------------------------------------------
205// resource functions
206//-----------------------------------------------------------------------------
207
208bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file )
209{
05939a81 210 wxChar buffer[500];
c801d85f
KB
211
212 if (!entry) return FALSE;
213
214 (void)GetIniFile(buffer, file);
215
216 XrmDatabase database;
a3622daa 217 wxNode *node = wxTheResourceCache->Find(buffer);
c801d85f 218 if (node)
a3622daa 219 database = (XrmDatabase)node->Data();
c801d85f 220 else {
dcf924a3 221 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
223d09f6 222 wxLogTrace(wxTraceResAlloc, wxT("Write: Number = %d"), wxTheResourceCache->Number());
a3622daa 223 wxTheResourceCache->Append(buffer, (wxObject *)database);
c801d85f
KB
224 }
225 char resName[300];
e90c1d2a 226 strcpy(resName, !section.IsNull() ? wxMBSTRINGCAST section.mb_str() : "wxWindows");
c801d85f 227 strcat(resName, ".");
05939a81
OK
228 strcat(resName, entry.mb_str());
229 XrmPutStringResource(&database, resName, value.mb_str());
c801d85f
KB
230 return TRUE;
231};
232
233bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file )
234{
235 char buf[50];
236 sprintf(buf, "%.4f", value);
237 return wxWriteResource(section, entry, buf, file);
238};
239
240bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file )
241{
242 char buf[50];
243 sprintf(buf, "%ld", value);
244 return wxWriteResource(section, entry, buf, file);
245};
246
247bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file )
248{
249 char buf[50];
250 sprintf(buf, "%d", value);
251 return wxWriteResource(section, entry, buf, file);
252};
253
254bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file )
255{
256 if (!wxResourceDatabase)
a3622daa 257 wxXMergeDatabases();
c801d85f
KB
258
259 XrmDatabase database;
3e61c765
RR
260 if (!file.IsEmpty())
261 {
05939a81 262 wxChar buffer[500];
a3622daa
VZ
263 // Is this right? Trying to get it to look in the user's
264 // home directory instead of current directory -- JACS
265 (void)GetIniFile(buffer, file);
266
bbe0af5b 267 wxNode *node = (wxNode*) NULL; /* suppress egcs warning */
bedd04ac 268 node = wxTheResourceCache->Find(buffer);
a3622daa 269 if (node)
bedd04ac 270 {
a3622daa 271 database = (XrmDatabase)node->Data();
bedd04ac
VZ
272 }
273 else
274 {
dcf924a3 275 database = XrmGetFileDatabase(wxConvCurrent->cWX2MB(buffer));
223d09f6 276 wxLogTrace(wxTraceResAlloc, wxT("Get: Number = %d"), wxTheResourceCache->Number());
a3622daa
VZ
277 wxTheResourceCache->Append(buffer, (wxObject *)database);
278 }
c801d85f 279 } else
a3622daa 280 database = wxResourceDatabase;
c801d85f
KB
281
282 XrmValue xvalue;
283 char *str_type[20];
284 char buf[150];
05939a81 285 strcpy(buf, section.mb_str());
c801d85f 286 strcat(buf, ".");
05939a81 287 strcat(buf, entry.mb_str());
c801d85f
KB
288
289 bool success = XrmGetResource(database, buf, "*", str_type, &xvalue);
290 // Try different combinations of upper/lower case, just in case...
3e61c765
RR
291 if (!success)
292 {
a3622daa
VZ
293 buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0]));
294 success = XrmGetResource(database, buf, "*", str_type, &xvalue);
c801d85f 295 }
3e61c765
RR
296 if (success)
297 {
a3622daa
VZ
298 if (*value)
299 delete[] *value;
300 *value = new char[xvalue.size + 1];
301 strncpy(*value, xvalue.addr, (int)xvalue.size);
302 return TRUE;
c801d85f
KB
303 }
304 return FALSE;
305};
306
307bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file )
308{
c67daf87 309 char *s = (char *) NULL;
c801d85f 310 bool succ = wxGetResource(section, entry, &s, file);
3e61c765
RR
311 if (succ)
312 {
c67daf87 313 *value = (float)strtod(s, (char **) NULL);
a3622daa
VZ
314 delete[]s;
315 return TRUE;
c801d85f 316 } else
a3622daa 317 return FALSE;
c801d85f
KB
318};
319
320bool wxGetResource(const wxString& section, const wxString& entry, long *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 = strtol(s, (char **) NULL, 10);
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, int *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 {
a3622daa
VZ
339 // Handle True, False here
340 // True, Yes, Enables, Set or Activated
9b64e798 341 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
a3622daa
VZ
342 *value = TRUE;
343 // False, No, Disabled, Reset, Cleared, Deactivated
344 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
345 *value = FALSE;
346 // Handle as Integer
347 else
c67daf87 348 *value = (int)strtol(s, (char **) NULL, 10);
a3622daa
VZ
349 delete[]s;
350 return TRUE;
c801d85f 351 } else
a3622daa 352 return FALSE;
c801d85f
KB
353};
354