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