1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 //#pragma implementation "utils.h"
16 #include "wx/string.h"
19 #include "wx/gdicmn.h"
25 //#include <sys/systeminfo.h>
28 #include "gdk/gdkx.h" // GDK_DISPLAY
29 #include "gdk/gdkprivate.h" // gdk_progclass
32 #include <X11/Xutil.h>
33 #include <X11/Xresource.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 // Yuck this is really BOTH site and platform dependent
41 // so we should use some other strategy!
43 # define DEFAULT_XRESOURCE_DIR wxT("/usr/openwin/lib/app-defaults")
44 // prototype missing in header files on Solaris 2.5
45 extern "C" { int gethostname(char *name
, size_t len
); }
47 # define DEFAULT_XRESOURCE_DIR wxT("/usr/lib/X11/app-defaults")
50 //-----------------------------------------------------------------------------
51 // glabal data (data.cpp)
52 //-----------------------------------------------------------------------------
54 extern wxResourceCache
*wxTheResourceCache
;
55 extern XrmDatabase wxResourceDatabase
;
57 //-----------------------------------------------------------------------------
58 // utility functions for get/write resources
59 //-----------------------------------------------------------------------------
61 static wxChar
*GetResourcePath(wxChar
*buf
, wxChar
*name
, bool create
)
63 if (create
&& FileExists(name
))
66 return buf
; // Exists so ...
68 if (*name
== wxT('/'))
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
));
79 // Touch the file to create it
80 FILE *fd
= fopen(wxConvCurrent
->cWX2MB(buf
), "w");
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
)
91 const wxChar
*home
= (const wxChar
*) NULL
;
92 if (filename
&& wxIsAbsolutePath(filename
))
94 wxStrcpy(dest
, filename
);
98 if ((home
= wxGetUserHome(wxString())) != NULL
)
100 wxStrcpy(dest
, home
);
101 if (dest
[wxStrlen(dest
) - 1] != wxT('/')) wxStrcat(dest
, wxT("/"));
102 if (filename
== NULL
)
104 if ((filename
= wxGetenv(wxT("XENVIRONMENT"))) == NULL
) filename
= wxT(".Xdefaults");
107 if (*filename
!= wxT('.')) wxStrcat(dest
, wxT("."));
108 wxStrcat(dest
, filename
);
118 static void wxXMergeDatabases()
120 XrmDatabase homeDB
, serverDB
, applicationDB
;
121 wxChar filenamebuf
[1024];
123 wxChar
*filename
= &filenamebuf
[0];
125 char *classname
= gdk_progclass
; // Robert Roebling ??
127 (void)strcpy(name
, "/usr/lib/X11/app-defaults/");
128 (void)strcat(name
, classname
? classname
: "wxWindows");
130 // Get application defaults file, if any
131 if ((applicationDB
= XrmGetFileDatabase(name
)))
132 (void)XrmMergeDatabases(applicationDB
, &wxResourceDatabase
);
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
)
140 serverDB
= XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY()));
144 (void)GetIniFile(filename
, (wxChar
*) NULL
);
145 serverDB
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(filename
));
148 XrmMergeDatabases(serverDB
, &wxResourceDatabase
);
150 // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
151 // and merge into existing database
153 if ((environment
= wxGetenv(wxT("XENVIRONMENT"))) == NULL
)
159 environment
= GetIniFile(filename
, (const wxChar
*) NULL
);
160 len
= wxStrlen(environment
);
161 #if !defined(SVR4) || defined(__sgi)
163 (void)gethostname(hostbuf
, 1024 - len
);
165 (void)gethostname(environment
+ len
, 1024 - len
);
169 (void)sysinfo(SI_HOSTNAME
, hostbuf
, 1024 - len
);
171 (void)sysinfo(SI_HOSTNAME
, environment
+ len
, 1024 - len
);
175 wxStrcat(environment
, wxConvCurrent
->cMB2WX(hostbuf
));
178 if ((homeDB
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(environment
))))
179 XrmMergeDatabases(homeDB
, &wxResourceDatabase
);
182 //-----------------------------------------------------------------------------
183 // called on application exit
184 //-----------------------------------------------------------------------------
186 void wxFlushResources()
188 wxChar nameBuffer
[512];
190 wxNode
*node
= wxTheResourceCache
->First();
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
);
197 XrmDatabase database
= (XrmDatabase
)node
->Data();
198 XrmPutFileDatabase(database
, wxConvCurrent
->cWX2MB(nameBuffer
));
199 XrmDestroyDatabase(database
);
200 wxNode
*next
= node
->Next();
206 void wxDeleteResources(const wxChar
*file
)
208 wxLogTrace(wxTraceResAlloc
, wxT("Delete: Number = %d"), wxTheResourceCache
->Number());
210 (void)GetIniFile(buffer
, file
);
212 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
214 XrmDatabase database
= (XrmDatabase
)node
->Data();
215 XrmDestroyDatabase(database
);
220 //-----------------------------------------------------------------------------
221 // resource functions
222 //-----------------------------------------------------------------------------
224 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
228 if (!entry
) return FALSE
;
230 (void)GetIniFile(buffer
, file
);
232 XrmDatabase database
;
233 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
235 database
= (XrmDatabase
)node
->Data();
237 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
238 wxLogTrace(wxTraceResAlloc
, wxT("Write: Number = %d"), wxTheResourceCache
->Number());
239 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
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());
249 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
252 sprintf(buf
, "%.4f", value
);
253 return wxWriteResource(section
, entry
, buf
, file
);
256 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
259 sprintf(buf
, "%ld", value
);
260 return wxWriteResource(section
, entry
, buf
, file
);
263 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
266 sprintf(buf
, "%d", value
);
267 return wxWriteResource(section
, entry
, buf
, file
);
270 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
272 if (!wxResourceDatabase
)
275 XrmDatabase database
;
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
);
283 wxNode
*node
= (wxNode
*) NULL
; /* suppress egcs warning */
284 node
= wxTheResourceCache
->Find(buffer
);
287 database
= (XrmDatabase
)node
->Data();
291 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
292 wxLogTrace(wxTraceResAlloc
, wxT("Get: Number = %d"), wxTheResourceCache
->Number());
293 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
296 database
= wxResourceDatabase
;
301 strcpy(buf
, section
.mb_str());
303 strcat(buf
, entry
.mb_str());
305 bool success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
306 // Try different combinations of upper/lower case, just in case...
309 buf
[0] = (isupper(buf
[0]) ? tolower(buf
[0]) : toupper(buf
[0]));
310 success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
316 *value
= new char[xvalue
.size
+ 1];
317 strncpy(*value
, xvalue
.addr
, (int)xvalue
.size
);
323 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
325 char *s
= (char *) NULL
;
326 bool succ
= wxGetResource(section
, entry
, &s
, file
);
329 *value
= (float)strtod(s
, (char **) NULL
);
336 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
338 char *s
= (char *) NULL
;
339 bool succ
= wxGetResource(section
, entry
, &s
, file
);
342 *value
= strtol(s
, (char **) NULL
, 10);
349 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
351 char *s
= (char *) NULL
;
352 bool succ
= wxGetResource(section
, entry
, &s
, file
);
355 // Handle True, False here
356 // True, Yes, Enables, Set or Activated
357 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
359 // False, No, Disabled, Reset, Cleared, Deactivated
360 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
364 *value
= (int)strtol(s
, (char **) NULL
, 10);