1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Robert Roebling 
   7 // Copyright:   (c) 1998 Robert Roebling, Julian Smart and Markus Holzem 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  13 //#pragma implementation "utils.h" 
  17 #include "wx/string.h" 
  24 #include <sys/systeminfo.h> 
  28 #include <X11/Xutil.h> 
  29 #include <X11/Xresource.h> 
  32 //----------------------------------------------------------------------------- 
  34 //----------------------------------------------------------------------------- 
  36 // Yuck this is really BOTH site and platform dependent 
  37 // so we should use some other strategy! 
  39     #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults" 
  41     #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults" 
  44 //----------------------------------------------------------------------------- 
  45 // glabal data (data.cpp) 
  46 //----------------------------------------------------------------------------- 
  48 extern wxList wxResourceCache
; 
  49 extern XrmDatabase wxResourceDatabase
; 
  51 //----------------------------------------------------------------------------- 
  52 // utility functions for get/write resources 
  53 //----------------------------------------------------------------------------- 
  55 static char *GetResourcePath(char *buf
, char *name
, bool create
) 
  57     if (create 
&& FileExists(name
)) { 
  59         return buf
; // Exists so ... 
  64         // Put in standard place for resource files if not absolute 
  65         strcpy(buf
, DEFAULT_XRESOURCE_DIR
); 
  67         strcat(buf
, FileNameFromPath(name
)); 
  70         // Touch the file to create it 
  71         FILE *fd 
= fopen(buf
, "w"); 
  77 // Read $HOME for what it says is home, if not 
  78 // read $USER or $LOGNAME for user name else determine 
  79 // the Real User, then determine the Real home dir. 
  80 static char *GetIniFile(char *dest
, const char *filename
) 
  83     if (filename 
&& wxIsAbsolutePath(filename
))  
  85       strcpy(dest
, filename
); 
  89       if ((home 
= wxGetUserHome(wxString())) != NULL
)  
  92         if (dest
[strlen(dest
) - 1] != '/') strcat(dest
, "/"); 
  95           if ((filename 
= getenv("XENVIRONMENT")) == NULL
) filename 
= ".Xdefaults"; 
  98           if (*filename 
!= '.') strcat(dest
, "."); 
  99         strcat(dest
, filename
); 
 109 static void wxXMergeDatabases(void) 
 111     XrmDatabase homeDB
, serverDB
, applicationDB
; 
 112     char filenamebuf
[1024]; 
 114     char *filename 
= &filenamebuf
[0]; 
 116 //    char *classname = gdk_progclass;               // Robert Roebling ?? 
 119     (void)strcpy(name
, "/usr/lib/X11/app-defaults/"); 
 120     (void)strcat(name
, classname 
? classname 
: "wxWindows"); 
 122     // Get application defaults file, if any  
 123     if ((applicationDB 
= XrmGetFileDatabase(name
))) 
 124         (void)XrmMergeDatabases(applicationDB
, &wxResourceDatabase
); 
 126     // Merge server defaults, created by xrdb, loaded as a property of the root 
 127     // window when the server initializes and loaded into the display 
 128     // structure on XOpenDisplay; 
 129     // if not defined, use .Xdefaults 
 131 /*    if (XResourceManagerString(GDK_DISPLAY()) != NULL) { 
 132         serverDB = XrmGetStringDatabase(XResourceManagerString(GDK_DISPLAY())); 
 134         (void)GetIniFile(filename, NULL); 
 135         serverDB = XrmGetFileDatabase(filename); 
 139         XrmMergeDatabases(serverDB
, &wxResourceDatabase
); 
 141     // Open XENVIRONMENT file, or if not defined, the .Xdefaults, 
 142     // and merge into existing database 
 144     if ((environment 
= getenv("XENVIRONMENT")) == NULL
) { 
 146         environment 
= GetIniFile(filename
, NULL
); 
 147         len 
= strlen(environment
); 
 148 #if !defined(SVR4) || defined(__sgi) 
 149         (void)gethostname(environment 
+ len
, 1024 - len
); 
 151         (void)sysinfo(SI_HOSTNAME
, environment 
+ len
, 1024 - len
); 
 154     if ((homeDB 
= XrmGetFileDatabase(environment
))) 
 155         XrmMergeDatabases(homeDB
, &wxResourceDatabase
); 
 158 //----------------------------------------------------------------------------- 
 159 // called on application exit 
 160 //----------------------------------------------------------------------------- 
 162 void wxFlushResources(void) 
 164     char nameBuffer
[512]; 
 166     wxNode 
*node 
= wxResourceCache
.First(); 
 168         char *file 
= node
->key
.string
; 
 169         // If file doesn't exist, create it first. 
 170         (void)GetResourcePath(nameBuffer
, file
, TRUE
); 
 172         XrmDatabase database 
= (XrmDatabase
)node
->Data(); 
 173         XrmPutFileDatabase(database
, nameBuffer
); 
 174         XrmDestroyDatabase(database
); 
 175         wxNode 
*next 
= node
->Next(); 
 181 void wxDeleteResources(const char *file
) 
 184     (void)GetIniFile(buffer
, file
); 
 186     wxNode 
*node 
= wxResourceCache
.Find(buffer
); 
 188         XrmDatabase database 
= (XrmDatabase
)node
->Data(); 
 189         XrmDestroyDatabase(database
); 
 194 //----------------------------------------------------------------------------- 
 195 // resource functions 
 196 //----------------------------------------------------------------------------- 
 198 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file 
) 
 202     if (!entry
) return FALSE
; 
 204     (void)GetIniFile(buffer
, file
); 
 206     XrmDatabase database
; 
 207     wxNode 
*node 
= wxResourceCache
.Find(buffer
); 
 209         database 
= (XrmDatabase
)node
->Data(); 
 211         database 
= XrmGetFileDatabase(buffer
); 
 212         wxResourceCache
.Append(buffer
, (wxObject 
*)database
); 
 215     strcpy(resName
, !section
.IsNull() ? WXSTRINGCAST section 
: "wxWindows"); 
 216     strcat(resName
, "."); 
 217     strcat(resName
, entry
); 
 218     XrmPutStringResource(&database
, resName
, value
); 
 222 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file 
) 
 225     sprintf(buf
, "%.4f", value
); 
 226     return wxWriteResource(section
, entry
, buf
, file
); 
 229 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file 
) 
 232     sprintf(buf
, "%ld", value
); 
 233     return wxWriteResource(section
, entry
, buf
, file
); 
 236 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file 
) 
 239     sprintf(buf
, "%d", value
); 
 240     return wxWriteResource(section
, entry
, buf
, file
); 
 243 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file 
) 
 245     if (!wxResourceDatabase
) 
 248     XrmDatabase database
; 
 251         // Is this right? Trying to get it to look in the user's 
 252         // home directory instead of current directory -- JACS 
 253         (void)GetIniFile(buffer
, file
); 
 255         wxNode 
*node 
= wxResourceCache
.Find(buffer
); 
 257             database 
= (XrmDatabase
)node
->Data(); 
 259             database 
= XrmGetFileDatabase(buffer
); 
 260             wxResourceCache
.Append(buffer
, (wxObject 
*)database
); 
 263         database 
= wxResourceDatabase
; 
 268     strcpy(buf
, section
); 
 272     bool success 
= XrmGetResource(database
, buf
, "*", str_type
, &xvalue
); 
 273     // Try different combinations of upper/lower case, just in case... 
 275         buf
[0] = (isupper(buf
[0]) ? tolower(buf
[0]) : toupper(buf
[0])); 
 276         success 
= XrmGetResource(database
, buf
, "*", str_type
,  &xvalue
); 
 281         *value 
= new char[xvalue
.size 
+ 1]; 
 282         strncpy(*value
, xvalue
.addr
, (int)xvalue
.size
); 
 288 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file 
) 
 291     bool succ 
= wxGetResource(section
, entry
, &s
, file
); 
 293         *value 
= (float)strtod(s
, NULL
); 
 300 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file 
) 
 303     bool succ 
= wxGetResource(section
, entry
, &s
, file
); 
 305         *value 
= strtol(s
, NULL
, 10); 
 312 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file 
) 
 315     bool succ 
= wxGetResource(section
, entry
, &s
, file
); 
 317         // Handle True, False here  
 318         // True, Yes, Enables, Set or  Activated  
 319         if (*s 
== 'T' || *s 
== 'Y' || *s 
== 'E' || *s 
== 'S' || *s 
== 'A') 
 321         // False, No, Disabled, Reset, Cleared, Deactivated 
 322         else if (*s 
== 'F' || *s 
== 'N' || *s 
== 'D' || *s 
== 'R' || *s 
== 'C') 
 326             *value 
= (int)strtol(s
, NULL
, 10);