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