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")
45 #define DEFAULT_XRESOURCE_DIR wxT("/usr/lib/X11/app-defaults")
48 //-----------------------------------------------------------------------------
49 // glabal data (data.cpp)
50 //-----------------------------------------------------------------------------
52 extern wxResourceCache
*wxTheResourceCache
;
53 extern XrmDatabase wxResourceDatabase
;
55 //-----------------------------------------------------------------------------
56 // utility functions for get/write resources
57 //-----------------------------------------------------------------------------
59 static wxChar
*GetResourcePath(wxChar
*buf
, wxChar
*name
, bool create
)
61 if (create
&& FileExists(name
))
64 return buf
; // Exists so ...
66 if (*name
== wxT('/'))
70 // Put in standard place for resource files if not absolute
71 wxStrcpy(buf
, DEFAULT_XRESOURCE_DIR
);
72 wxStrcat(buf
, wxT("/"));
73 wxStrcat(buf
, FileNameFromPath(name
));
77 // Touch the file to create it
78 FILE *fd
= fopen(wxConvCurrent
->cWX2MB(buf
), "w");
84 // Read $HOME for what it says is home, if not
85 // read $USER or $LOGNAME for user name else determine
86 // the Real User, then determine the Real home dir.
87 static wxChar
*GetIniFile(wxChar
*dest
, const wxChar
*filename
)
89 const wxChar
*home
= (const wxChar
*) NULL
;
90 if (filename
&& wxIsAbsolutePath(filename
))
92 wxStrcpy(dest
, filename
);
96 if ((home
= wxGetUserHome(wxString())) != NULL
)
99 if (dest
[wxStrlen(dest
) - 1] != wxT('/')) wxStrcat(dest
, wxT("/"));
100 if (filename
== NULL
)
102 if ((filename
= wxGetenv(wxT("XENVIRONMENT"))) == NULL
) filename
= wxT(".Xdefaults");
105 if (*filename
!= wxT('.')) wxStrcat(dest
, wxT("."));
106 wxStrcat(dest
, filename
);
116 static void wxXMergeDatabases()
118 XrmDatabase homeDB
, serverDB
, applicationDB
;
119 wxChar filenamebuf
[1024];
121 wxChar
*filename
= &filenamebuf
[0];
123 char *classname
= gdk_progclass
; // Robert Roebling ??
125 (void)strcpy(name
, "/usr/lib/X11/app-defaults/");
126 (void)strcat(name
, classname
? classname
: "wxWindows");
128 // Get application defaults file, if any
129 if ((applicationDB
= XrmGetFileDatabase(name
)))
130 (void)XrmMergeDatabases(applicationDB
, &wxResourceDatabase
);
132 // Merge server defaults, created by xrdb, loaded as a property of the root
133 // window when the server initializes and loaded into the display
134 // structure on XOpenDisplay;
135 // if not defined, use .Xdefaults
136 if (XResourceManagerString(GDK_DISPLAY()) != NULL
)
138 serverDB
= XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY()));
142 (void)GetIniFile(filename
, (wxChar
*) NULL
);
143 serverDB
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(filename
));
146 XrmMergeDatabases(serverDB
, &wxResourceDatabase
);
148 // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
149 // and merge into existing database
151 if ((environment
= wxGetenv(wxT("XENVIRONMENT"))) == NULL
)
157 environment
= GetIniFile(filename
, (const wxChar
*) NULL
);
158 len
= wxStrlen(environment
);
159 #if !defined(SVR4) || defined(__sgi)
161 (void)gethostname(hostbuf
, 1024 - len
);
163 (void)gethostname(environment
+ len
, 1024 - len
);
167 (void)sysinfo(SI_HOSTNAME
, hostbuf
, 1024 - len
);
169 (void)sysinfo(SI_HOSTNAME
, environment
+ len
, 1024 - len
);
173 wxStrcat(environment
, wxConvCurrent
->cMB2WX(hostbuf
));
176 if ((homeDB
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(environment
))))
177 XrmMergeDatabases(homeDB
, &wxResourceDatabase
);
180 //-----------------------------------------------------------------------------
181 // called on application exit
182 //-----------------------------------------------------------------------------
184 void wxFlushResources()
186 wxChar nameBuffer
[512];
188 wxNode
*node
= wxTheResourceCache
->First();
190 wxString str
= node
->GetKeyString();
191 wxChar
*file
= WXSTRINGCAST str
;
192 // If file doesn't exist, create it first.
193 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
195 XrmDatabase database
= (XrmDatabase
)node
->Data();
196 XrmPutFileDatabase(database
, wxConvCurrent
->cWX2MB(nameBuffer
));
197 XrmDestroyDatabase(database
);
198 wxNode
*next
= node
->Next();
204 void wxDeleteResources(const wxChar
*file
)
206 wxLogTrace(wxTraceResAlloc
, wxT("Delete: Number = %d"), wxTheResourceCache
->Number());
208 (void)GetIniFile(buffer
, file
);
210 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
212 XrmDatabase database
= (XrmDatabase
)node
->Data();
213 XrmDestroyDatabase(database
);
218 //-----------------------------------------------------------------------------
219 // resource functions
220 //-----------------------------------------------------------------------------
222 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
226 if (!entry
) return FALSE
;
228 (void)GetIniFile(buffer
, file
);
230 XrmDatabase database
;
231 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
233 database
= (XrmDatabase
)node
->Data();
235 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
236 wxLogTrace(wxTraceResAlloc
, wxT("Write: Number = %d"), wxTheResourceCache
->Number());
237 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
240 strcpy(resName
, !section
.IsNull() ? wxMBSTRINGCAST section
.mb_str() : "wxWindows");
241 strcat(resName
, ".");
242 strcat(resName
, entry
.mb_str());
243 XrmPutStringResource(&database
, resName
, value
.mb_str());
247 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
250 sprintf(buf
, "%.4f", value
);
251 return wxWriteResource(section
, entry
, buf
, file
);
254 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
257 sprintf(buf
, "%ld", value
);
258 return wxWriteResource(section
, entry
, buf
, file
);
261 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
264 sprintf(buf
, "%d", value
);
265 return wxWriteResource(section
, entry
, buf
, file
);
268 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
270 if (!wxResourceDatabase
)
273 XrmDatabase database
;
277 // Is this right? Trying to get it to look in the user's
278 // home directory instead of current directory -- JACS
279 (void)GetIniFile(buffer
, file
);
281 wxNode
*node
= (wxNode
*) NULL
; /* suppress egcs warning */
282 node
= wxTheResourceCache
->Find(buffer
);
285 database
= (XrmDatabase
)node
->Data();
289 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
290 wxLogTrace(wxTraceResAlloc
, wxT("Get: Number = %d"), wxTheResourceCache
->Number());
291 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
294 database
= wxResourceDatabase
;
299 strcpy(buf
, section
.mb_str());
301 strcat(buf
, entry
.mb_str());
303 bool success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
304 // Try different combinations of upper/lower case, just in case...
307 buf
[0] = (isupper(buf
[0]) ? tolower(buf
[0]) : toupper(buf
[0]));
308 success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
314 *value
= new char[xvalue
.size
+ 1];
315 strncpy(*value
, xvalue
.addr
, (int)xvalue
.size
);
321 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
323 char *s
= (char *) NULL
;
324 bool succ
= wxGetResource(section
, entry
, &s
, file
);
327 *value
= (float)strtod(s
, (char **) NULL
);
334 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
336 char *s
= (char *) NULL
;
337 bool succ
= wxGetResource(section
, entry
, &s
, file
);
340 *value
= strtol(s
, (char **) NULL
, 10);
347 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
349 char *s
= (char *) NULL
;
350 bool succ
= wxGetResource(section
, entry
, &s
, file
);
353 // Handle True, False here
354 // True, Yes, Enables, Set or Activated
355 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
357 // False, No, Disabled, Reset, Cleared, Deactivated
358 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
362 *value
= (int)strtol(s
, (char **) NULL
, 10);