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