]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsres.cpp
bug in wxNotebook::OnSize() corrected
[wxWidgets.git] / src / gtk / utilsres.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 //#ifdef __GNUG__
13 //#pragma implementation "utils.h"
14 //#endif
15
16 #include "wx/utils.h"
17 #include "wx/string.h"
18 #include "wx/list.h"
19
20 #include <ctype.h>
21 #include <string.h>
22 #include <unistd.h>
23 #ifdef __SVR4__
24 #include <sys/systeminfo.h>
25 #endif
26
27 #include "gdk/gdkx.h" // GDK_DISPLAY
28 #include "gdk/gdkprivate.h" // gdk_progclass
29
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/Xresource.h>
33
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
51 extern wxList wxResourceCache;
52 extern XrmDatabase wxResourceDatabase;
53
54 //-----------------------------------------------------------------------------
55 // utility functions for get/write resources
56 //-----------------------------------------------------------------------------
57
58 static char *GetResourcePath(char *buf, char *name, bool create)
59 {
60 if (create && FileExists(name)) {
61 strcpy(buf, name);
62 return buf; // Exists so ...
63 }
64 if (*name == '/')
65 strcpy(buf, name);
66 else {
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));
71 }
72 if (create) {
73 // Touch the file to create it
74 FILE *fd = fopen(buf, "w");
75 if (fd) fclose(fd);
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.
83 static char *GetIniFile(char *dest, const char *filename)
84 {
85 char *home = NULL;
86 if (filename && wxIsAbsolutePath(filename))
87 {
88 strcpy(dest, filename);
89 }
90 else
91 {
92 if ((home = wxGetUserHome(wxString())) != NULL)
93 {
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);
103 }
104 else
105 {
106 dest[0] = '\0';
107 }
108 }
109 return dest;
110 }
111
112 static 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
124 // Get application defaults file, if any
125 if ((applicationDB = XrmGetFileDatabase(name)))
126 (void)XrmMergeDatabases(applicationDB, &wxResourceDatabase);
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) {
133 serverDB = XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY()));
134 } else {
135 (void)GetIniFile(filename, NULL);
136 serverDB = XrmGetFileDatabase(filename);
137 }
138 if (serverDB)
139 XrmMergeDatabases(serverDB, &wxResourceDatabase);
140
141 // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
142 // and merge into existing database
143
144 if ((environment = getenv("XENVIRONMENT")) == NULL) {
145 size_t len;
146 environment = GetIniFile(filename, NULL);
147 len = strlen(environment);
148 #if !defined(SVR4) || defined(__sgi)
149 (void)gethostname(environment + len, 1024 - len);
150 #else
151 (void)sysinfo(SI_HOSTNAME, environment + len, 1024 - len);
152 #endif
153 }
154 if ((homeDB = XrmGetFileDatabase(environment)))
155 XrmMergeDatabases(homeDB, &wxResourceDatabase);
156 }
157
158 //-----------------------------------------------------------------------------
159 // called on application exit
160 //-----------------------------------------------------------------------------
161
162 void wxFlushResources(void)
163 {
164 char nameBuffer[512];
165
166 wxNode *node = wxResourceCache.First();
167 while (node) {
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;
178 }
179 }
180
181 void wxDeleteResources(const char *file)
182 {
183 char buffer[500];
184 (void)GetIniFile(buffer, file);
185
186 wxNode *node = wxResourceCache.Find(buffer);
187 if (node) {
188 XrmDatabase database = (XrmDatabase)node->Data();
189 XrmDestroyDatabase(database);
190 delete node;
191 }
192 }
193
194 //-----------------------------------------------------------------------------
195 // resource functions
196 //-----------------------------------------------------------------------------
197
198 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file )
199 {
200 char buffer[500];
201
202 if (!entry) return FALSE;
203
204 (void)GetIniFile(buffer, file);
205
206 XrmDatabase database;
207 wxNode *node = wxResourceCache.Find(buffer);
208 if (node)
209 database = (XrmDatabase)node->Data();
210 else {
211 database = XrmGetFileDatabase(buffer);
212 wxResourceCache.Append(buffer, (wxObject *)database);
213 }
214 char resName[300];
215 strcpy(resName, !section.IsNull() ? WXSTRINGCAST section : "wxWindows");
216 strcat(resName, ".");
217 strcat(resName, entry);
218 XrmPutStringResource(&database, resName, value);
219 return TRUE;
220 };
221
222 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file )
223 {
224 char buf[50];
225 sprintf(buf, "%.4f", value);
226 return wxWriteResource(section, entry, buf, file);
227 };
228
229 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file )
230 {
231 char buf[50];
232 sprintf(buf, "%ld", value);
233 return wxWriteResource(section, entry, buf, file);
234 };
235
236 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file )
237 {
238 char buf[50];
239 sprintf(buf, "%d", value);
240 return wxWriteResource(section, entry, buf, file);
241 };
242
243 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file )
244 {
245 if (!wxResourceDatabase)
246 wxXMergeDatabases();
247
248 XrmDatabase database;
249 if (file) {
250 char buffer[500];
251 // Is this right? Trying to get it to look in the user's
252 // home directory instead of current directory -- JACS
253 (void)GetIniFile(buffer, file);
254
255 wxNode *node = wxResourceCache.Find(buffer);
256 if (node)
257 database = (XrmDatabase)node->Data();
258 else {
259 database = XrmGetFileDatabase(buffer);
260 wxResourceCache.Append(buffer, (wxObject *)database);
261 }
262 } else
263 database = wxResourceDatabase;
264
265 XrmValue xvalue;
266 char *str_type[20];
267 char buf[150];
268 strcpy(buf, section);
269 strcat(buf, ".");
270 strcat(buf, entry);
271
272 bool success = XrmGetResource(database, buf, "*", str_type, &xvalue);
273 // Try different combinations of upper/lower case, just in case...
274 if (!success) {
275 buf[0] = (isupper(buf[0]) ? tolower(buf[0]) : toupper(buf[0]));
276 success = XrmGetResource(database, buf, "*", str_type, &xvalue);
277 }
278 if (success) {
279 if (*value)
280 delete[] *value;
281 *value = new char[xvalue.size + 1];
282 strncpy(*value, xvalue.addr, (int)xvalue.size);
283 return TRUE;
284 }
285 return FALSE;
286 };
287
288 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file )
289 {
290 char *s = NULL;
291 bool succ = wxGetResource(section, entry, &s, file);
292 if (succ) {
293 *value = (float)strtod(s, NULL);
294 delete[]s;
295 return TRUE;
296 } else
297 return FALSE;
298 };
299
300 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file )
301 {
302 char *s = NULL;
303 bool succ = wxGetResource(section, entry, &s, file);
304 if (succ) {
305 *value = strtol(s, NULL, 10);
306 delete[]s;
307 return TRUE;
308 } else
309 return FALSE;
310 };
311
312 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file )
313 {
314 char *s = NULL;
315 bool succ = wxGetResource(section, entry, &s, file);
316 if (succ) {
317 // Handle True, False here
318 // True, Yes, Enables, Set or Activated
319 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
320 *value = TRUE;
321 // False, No, Disabled, Reset, Cleared, Deactivated
322 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
323 *value = FALSE;
324 // Handle as Integer
325 else
326 *value = (int)strtol(s, NULL, 10);
327 delete[]s;
328 return TRUE;
329 } else
330 return FALSE;
331 };
332