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
)
153 environment
= GetIniFile(filename
, (const wxChar
*) NULL
);
154 size_t len
= wxStrlen(environment
);
156 wxChar hostbuf
[1024];
157 (void)wxGetHostName(hostbuf
, WXSIZEOF(hostbuf
) - len
);
159 wxStrcat(environment
, hostbuf
);
162 if ((homeDB
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(environment
))))
163 XrmMergeDatabases(homeDB
, &wxResourceDatabase
);
166 //-----------------------------------------------------------------------------
167 // called on application exit
168 //-----------------------------------------------------------------------------
170 void wxFlushResources()
172 wxChar nameBuffer
[512];
174 wxNode
*node
= wxTheResourceCache
->First();
176 wxString str
= node
->GetKeyString();
177 wxChar
*file
= WXSTRINGCAST str
;
178 // If file doesn't exist, create it first.
179 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
181 XrmDatabase database
= (XrmDatabase
)node
->Data();
182 XrmPutFileDatabase(database
, wxConvCurrent
->cWX2MB(nameBuffer
));
183 XrmDestroyDatabase(database
);
184 wxNode
*next
= node
->Next();
190 void wxDeleteResources(const wxChar
*file
)
192 wxLogTrace(wxTraceResAlloc
, wxT("Delete: Number = %d"), wxTheResourceCache
->Number());
194 (void)GetIniFile(buffer
, file
);
196 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
198 XrmDatabase database
= (XrmDatabase
)node
->Data();
199 XrmDestroyDatabase(database
);
204 //-----------------------------------------------------------------------------
205 // resource functions
206 //-----------------------------------------------------------------------------
208 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
212 if (!entry
) return FALSE
;
214 (void)GetIniFile(buffer
, file
);
216 XrmDatabase database
;
217 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
219 database
= (XrmDatabase
)node
->Data();
221 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
222 wxLogTrace(wxTraceResAlloc
, wxT("Write: Number = %d"), wxTheResourceCache
->Number());
223 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
226 strcpy(resName
, !section
.IsNull() ? wxMBSTRINGCAST section
.mb_str() : "wxWindows");
227 strcat(resName
, ".");
228 strcat(resName
, entry
.mb_str());
229 XrmPutStringResource(&database
, resName
, value
.mb_str());
233 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
236 sprintf(buf
, "%.4f", value
);
237 return wxWriteResource(section
, entry
, buf
, file
);
240 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
243 sprintf(buf
, "%ld", value
);
244 return wxWriteResource(section
, entry
, buf
, file
);
247 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
250 sprintf(buf
, "%d", value
);
251 return wxWriteResource(section
, entry
, buf
, file
);
254 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
256 if (!wxResourceDatabase
)
259 XrmDatabase database
;
263 // Is this right? Trying to get it to look in the user's
264 // home directory instead of current directory -- JACS
265 (void)GetIniFile(buffer
, file
);
267 wxNode
*node
= (wxNode
*) NULL
; /* suppress egcs warning */
268 node
= wxTheResourceCache
->Find(buffer
);
271 database
= (XrmDatabase
)node
->Data();
275 database
= XrmGetFileDatabase(wxConvCurrent
->cWX2MB(buffer
));
276 wxLogTrace(wxTraceResAlloc
, wxT("Get: Number = %d"), wxTheResourceCache
->Number());
277 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
280 database
= wxResourceDatabase
;
285 strcpy(buf
, section
.mb_str());
287 strcat(buf
, entry
.mb_str());
289 bool success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
290 // Try different combinations of upper/lower case, just in case...
293 buf
[0] = (isupper(buf
[0]) ? tolower(buf
[0]) : toupper(buf
[0]));
294 success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
300 *value
= new char[xvalue
.size
+ 1];
301 strncpy(*value
, xvalue
.addr
, (int)xvalue
.size
);
307 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
309 char *s
= (char *) NULL
;
310 bool succ
= wxGetResource(section
, entry
, &s
, file
);
313 *value
= (float)strtod(s
, (char **) NULL
);
320 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
322 char *s
= (char *) NULL
;
323 bool succ
= wxGetResource(section
, entry
, &s
, file
);
326 *value
= strtol(s
, (char **) NULL
, 10);
333 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
335 char *s
= (char *) NULL
;
336 bool succ
= wxGetResource(section
, entry
, &s
, file
);
339 // Handle True, False here
340 // True, Yes, Enables, Set or Activated
341 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
343 // False, No, Disabled, Reset, Cleared, Deactivated
344 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
348 *value
= (int)strtol(s
, (char **) NULL
, 10);