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"
23 #include <sys/systeminfo.h>
26 #include "gdk/gdkx.h" // GDK_DISPLAY
27 #include "gdk/gdkprivate.h" // gdk_progclass
30 #include <X11/Xutil.h>
31 #include <X11/Xresource.h>
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 // Yuck this is really BOTH site and platform dependent
40 // so we should use some other strategy!
42 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
44 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
47 //-----------------------------------------------------------------------------
48 // glabal data (data.cpp)
49 //-----------------------------------------------------------------------------
51 extern wxResourceCache
*wxTheResourceCache
;
52 extern XrmDatabase wxResourceDatabase
;
54 //-----------------------------------------------------------------------------
55 // utility functions for get/write resources
56 //-----------------------------------------------------------------------------
58 static char *GetResourcePath(char *buf
, char *name
, bool create
)
60 if (create
&& FileExists(name
))
63 return buf
; // Exists so ...
69 // Put in standard place for resource files if not absolute
70 strcpy(buf
, DEFAULT_XRESOURCE_DIR
);
72 strcat(buf
, FileNameFromPath(name
));
76 // Touch the file to create it
77 FILE *fd
= fopen(buf
, "w");
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
)
88 char *home
= (char *) NULL
;
89 if (filename
&& wxIsAbsolutePath(filename
))
91 strcpy(dest
, filename
);
95 if ((home
= wxGetUserHome(wxString())) != NULL
)
98 if (dest
[strlen(dest
) - 1] != '/') strcat(dest
, "/");
101 if ((filename
= getenv("XENVIRONMENT")) == NULL
) filename
= ".Xdefaults";
104 if (*filename
!= '.') strcat(dest
, ".");
105 strcat(dest
, filename
);
115 static void wxXMergeDatabases()
117 XrmDatabase homeDB
, serverDB
, applicationDB
;
118 char filenamebuf
[1024];
120 char *filename
= &filenamebuf
[0];
122 char *classname
= gdk_progclass
; // Robert Roebling ??
124 (void)strcpy(name
, "/usr/lib/X11/app-defaults/");
125 (void)strcat(name
, classname
? classname
: "wxWindows");
127 // Get application defaults file, if any
128 if ((applicationDB
= XrmGetFileDatabase(name
)))
129 (void)XrmMergeDatabases(applicationDB
, &wxResourceDatabase
);
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
)
137 serverDB
= XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY()));
141 (void)GetIniFile(filename
, (char *) NULL
);
142 serverDB
= XrmGetFileDatabase(filename
);
145 XrmMergeDatabases(serverDB
, &wxResourceDatabase
);
147 // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
148 // and merge into existing database
150 if ((environment
= getenv("XENVIRONMENT")) == NULL
)
153 environment
= GetIniFile(filename
, (const char *) NULL
);
154 len
= strlen(environment
);
155 #if !defined(SVR4) || defined(__sgi)
156 (void)gethostname(environment
+ len
, 1024 - len
);
158 (void)sysinfo(SI_HOSTNAME
, environment
+ len
, 1024 - len
);
161 if ((homeDB
= XrmGetFileDatabase(environment
)))
162 XrmMergeDatabases(homeDB
, &wxResourceDatabase
);
165 //-----------------------------------------------------------------------------
166 // called on application exit
167 //-----------------------------------------------------------------------------
169 void wxFlushResources()
171 char nameBuffer
[512];
173 wxNode
*node
= wxTheResourceCache
->First();
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
);
180 XrmDatabase database
= (XrmDatabase
)node
->Data();
181 XrmPutFileDatabase(database
, nameBuffer
);
182 XrmDestroyDatabase(database
);
183 wxNode
*next
= node
->Next();
189 void wxDeleteResources(const char *file
)
191 wxLogTrace(wxTraceResAlloc
, "Delete: Number = %d", wxTheResourceCache
->Number());
193 (void)GetIniFile(buffer
, file
);
195 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
197 XrmDatabase database
= (XrmDatabase
)node
->Data();
198 XrmDestroyDatabase(database
);
203 //-----------------------------------------------------------------------------
204 // resource functions
205 //-----------------------------------------------------------------------------
207 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
211 if (!entry
) return FALSE
;
213 (void)GetIniFile(buffer
, file
);
215 XrmDatabase database
;
216 wxNode
*node
= wxTheResourceCache
->Find(buffer
);
218 database
= (XrmDatabase
)node
->Data();
220 database
= XrmGetFileDatabase(buffer
);
221 wxLogTrace(wxTraceResAlloc
, "Write: Number = %d", wxTheResourceCache
->Number());
222 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
225 strcpy(resName
, !section
.IsNull() ? WXSTRINGCAST section
: "wxWindows");
226 strcat(resName
, ".");
227 strcat(resName
, entry
);
228 XrmPutStringResource(&database
, resName
, value
);
232 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
235 sprintf(buf
, "%.4f", value
);
236 return wxWriteResource(section
, entry
, buf
, file
);
239 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
242 sprintf(buf
, "%ld", value
);
243 return wxWriteResource(section
, entry
, buf
, file
);
246 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
249 sprintf(buf
, "%d", value
);
250 return wxWriteResource(section
, entry
, buf
, file
);
253 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
255 if (!wxResourceDatabase
)
258 XrmDatabase database
;
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
);
266 wxNode
*node
= (wxNode
*) NULL
; /* suppress egcs warning */
267 node
= wxTheResourceCache
->Find(buffer
);
270 database
= (XrmDatabase
)node
->Data();
274 database
= XrmGetFileDatabase(buffer
);
275 wxLogTrace(wxTraceResAlloc
, "Get: Number = %d", wxTheResourceCache
->Number());
276 wxTheResourceCache
->Append(buffer
, (wxObject
*)database
);
279 database
= wxResourceDatabase
;
284 strcpy(buf
, section
);
288 bool success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
289 // Try different combinations of upper/lower case, just in case...
292 buf
[0] = (isupper(buf
[0]) ? tolower(buf
[0]) : toupper(buf
[0]));
293 success
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
);
299 *value
= new char[xvalue
.size
+ 1];
300 strncpy(*value
, xvalue
.addr
, (int)xvalue
.size
);
306 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
308 char *s
= (char *) NULL
;
309 bool succ
= wxGetResource(section
, entry
, &s
, file
);
312 *value
= (float)strtod(s
, (char **) NULL
);
319 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
321 char *s
= (char *) NULL
;
322 bool succ
= wxGetResource(section
, entry
, &s
, file
);
325 *value
= strtol(s
, (char **) NULL
, 10);
332 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
334 char *s
= (char *) NULL
;
335 bool succ
= wxGetResource(section
, entry
, &s
, file
);
338 // Handle True, False here
339 // True, Yes, Enables, Set or Activated
340 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
342 // False, No, Disabled, Reset, Cleared, Deactivated
343 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
347 *value
= (int)strtol(s
, (char **) NULL
, 10);