Back to index

plt-scheme  4.2.1
Resources.cc
Go to the documentation of this file.
00001 /*                                                      -*- C++ -*-
00002  *
00003  * Purpose: read/write .Xdefaults
00004  *
00005  * Authors: Markus Holzem and Julian Smart
00006  *
00007  * Copyright: (C) 2004-2009 PLT Scheme Inc.
00008  * Copyright: (C) 1995, AIAI, University of Edinburgh (Julian)
00009  * Copyright: (C) 1995, GNU (Markus)
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00027 #define  Uses_XLib
00028 #define  Uses_wxList
00029 #include "wx.h"
00030 
00031 #include <ctype.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 
00035 #if defined(SVR4) && !defined(__sgi)
00036 # include <sys/systeminfo.h>
00037 #endif
00038 
00039 //-----------------------------------------------------------------------------
00040 // We have a cache for writing different resource files,
00041 // which will only get flushed when we call wxFlushResources().
00042 // Build up a list of resource databases waiting to be written.
00043 //-----------------------------------------------------------------------------
00044 
00045 //-----------------------------------------------------------------------------
00046 // utility functions for get/write resources
00047 //-----------------------------------------------------------------------------
00048 
00049 static char *GetIniFile(char *dest, const char *filename)
00050 {
00051     char *home = NULL;
00052     if (filename) {
00053       strcpy(dest, filename);
00054     } else if ((home = wxGetUserHome(NULL)) != NULL) {
00055       strcpy(dest, home);
00056       if (dest[strlen(dest) - 1] != '/')
00057        strcat(dest, "/");
00058       strcat(dest, ".mred.resources");
00059     } else {
00060       dest[0] = '\0';    
00061     }
00062     return dest;
00063 }
00064 
00065 static XrmDatabase wxXrmGetFileDatabase(const char *s)
00066 {
00067   /* because directory names crash XrmGetFileDatabase */
00068   if (!wxDirExists((char *)s))
00069     return XrmGetFileDatabase(s);
00070   else
00071     return NULL;
00072 }
00073 
00074 static void wxXMergeDatabases(void)
00075 {
00076     XrmDatabase homeDB, serverDB, applicationDB, userDB;
00077     char filenamebuf[1024];
00078     char *filename = &filenamebuf[0];
00079     char *environment;
00080     char *classname = wxAPP_CLASS;
00081     char name[256];
00082     char *home, *dest;
00083 
00084     (void)strcpy(name, "/usr/lib/X11/app-defaults/");
00085     strcat(name, classname ? classname : "wxWindows");
00086 
00087     // Get application defaults file, if any 
00088     if ((applicationDB = wxXrmGetFileDatabase(name)))
00089       XrmMergeDatabases(applicationDB, &wxResourceDatabase);
00090 
00091     // Merge server defaults, created by xrdb, loaded as a property of the root
00092     // window when the server initializes and loaded into the display
00093     // structure on XOpenDisplay;
00094     // if not defined, use .Xdefaults
00095     if (XResourceManagerString(wxAPP_DISPLAY) != NULL) {
00096        serverDB = XrmGetStringDatabase(XResourceManagerString(wxAPP_DISPLAY));
00097     } else {
00098       // Get X defaults file, if any 
00099       home = wxGetUserHome(NULL);
00100       if (home) {
00101        dest = new WXGC_ATOMIC char[strlen(home) + 20];
00102        
00103        strcpy(dest, home);
00104        if (dest[strlen(dest) - 1] != '/')
00105          strcat(dest, "/");
00106        strcat(dest, ".Xdefaults");
00107        
00108        serverDB = wxXrmGetFileDatabase(dest);
00109       } else
00110        serverDB = NULL;
00111     }
00112     if (serverDB)
00113       XrmMergeDatabases(serverDB, &wxResourceDatabase);
00114 
00115     // Open XENVIRONMENT file, or if not defined, the .Xdefaults,
00116     // and merge into existing database
00117 
00118     if ((environment = getenv("XENVIRONMENT")) == NULL) {
00119       size_t len;
00120       environment = GetIniFile(filename, NULL);
00121       len = strlen(environment);
00122 #if !defined(SVR4) || defined(__sgi)
00123       gethostname(environment XFORM_OK_PLUS len, 1024 - len);
00124 #else
00125       sysinfo(SI_HOSTNAME, environment XFORM_OK_PLUS len, 1024 - len);
00126 #endif
00127     }
00128     if ((homeDB = wxXrmGetFileDatabase(environment)))
00129       XrmMergeDatabases(homeDB, &wxResourceDatabase);
00130 
00131 
00132     // Get user defaults file, if any 
00133     home = wxGetUserHome(NULL);
00134     if (home) {
00135       dest = new WXGC_ATOMIC char[strlen(home) + 20];
00136       
00137       strcpy(dest, home);
00138       if (dest[strlen(dest) - 1] != '/')
00139        strcat(dest, "/");
00140       strcat(dest, ".mred.resources");
00141       
00142       if ((userDB = wxXrmGetFileDatabase(dest)))
00143        (void)XrmMergeDatabases(userDB, &wxResourceDatabase);
00144     }
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 // write resource functions
00149 //-----------------------------------------------------------------------------
00150 Bool wxWriteResource(const char *section, const char *entry, char *value,
00151                    const char *file)
00152 {
00153     char buffer[500];
00154     char resName[300];
00155     int isnull;
00156     XrmDatabase database;
00157     wxNode *node;
00158 
00159     if (!entry)
00160       return FALSE;
00161 
00162     (void)GetIniFile(buffer, file);
00163 
00164     node = wxResourceCache->Find(buffer);
00165     if (node)
00166       database = (XrmDatabase)node->Data();
00167     else {
00168       database = wxXrmGetFileDatabase(buffer);
00169       node = wxResourceCache->Append(buffer, (wxObject *)database);
00170     }
00171     strcpy(resName, section ? section : "wxWindows");
00172     strcat(resName, ".");
00173     strcat(resName, entry);
00174 
00175     isnull = !database;
00176     XrmPutStringResource(&database, resName, value);
00177     if (isnull) {
00178       if (node)
00179        wxResourceCache->DeleteNode(node);
00180       wxResourceCache->Append(buffer, (wxObject *)database);
00181     }
00182 
00183     XrmPutFileDatabase(database, buffer);
00184 
00185     return TRUE;
00186 }
00187 
00188 Bool wxWriteResource(const char *section, const char *entry, float value,
00189                    const char *file)
00190 {
00191     char buf[50];
00192     sprintf(buf, "%.4f", value);
00193     return wxWriteResource(section, entry, buf, file);
00194 }
00195 
00196 Bool wxWriteResource(const char *section, const char *entry, long value,
00197                    const char *file)
00198 {
00199     char buf[50];
00200     sprintf(buf, "%ld", value);
00201     return wxWriteResource(section, entry, buf, file);
00202 }
00203 
00204 Bool wxWriteResource(const char *section, const char *entry, int value,
00205                    const char *file)
00206 {
00207     char buf[50];
00208     sprintf(buf, "%d", value);
00209     return wxWriteResource(section, entry, buf, file);
00210 }
00211 
00212 //-----------------------------------------------------------------------------
00213 // get resource functions
00214 //-----------------------------------------------------------------------------
00215 Bool wxGetResource(const char *section, const char *entry, char **value,
00216                  const char *file)
00217 {
00218     XrmValue xvalue;
00219     char *str_type[20];
00220     char buf[150];
00221     Bool success;
00222     XrmDatabase database;
00223 
00224     if (!wxResourceDatabase)
00225        wxXMergeDatabases();
00226 
00227     if (file) {
00228       char buffer[500];
00229       wxNode *node;
00230 
00231       (void)GetIniFile(buffer, file);
00232       
00233       node = wxResourceCache->Find(buffer);
00234       if (node)
00235        database = (XrmDatabase)node->Data();
00236       else {
00237        database = wxXrmGetFileDatabase(buffer);
00238        wxResourceCache->Append(buffer, (wxObject *)database);
00239       }
00240     } else
00241       database = wxResourceDatabase;
00242     
00243     strcpy(buf, section);
00244     strcat(buf, ".");
00245     strcat(buf, entry);
00246 
00247     success = XrmGetResource(database, buf, "*", str_type, &xvalue);
00248     if (success) {
00249       char *v;
00250       v = new WXGC_ATOMIC char[xvalue.size + 1];
00251       *value = v;
00252       strncpy(*value, xvalue.addr, (int)xvalue.size);
00253       return TRUE;
00254     }
00255     return FALSE;
00256 }
00257 
00258 Bool wxGetResource(const char *section, const char *entry, float *value,
00259                  const char *file)
00260 {
00261   char *s = NULL;
00262   Bool succ;
00263   succ = wxGetResource(section, entry, &s, file);
00264   if (succ) {
00265     float v;
00266     v = (float)strtod(s, NULL);
00267     *value = v;
00268     return TRUE;
00269   } else
00270     return FALSE;
00271 }
00272 
00273 Bool wxGetResource(const char *section, const char *entry, long *value,
00274                  const char *file)
00275 {
00276   char *s = NULL;
00277   Bool succ;
00278   succ = wxGetResource(section, entry, &s, file);
00279   if (succ) {
00280     long v;
00281     v = strtol(s, NULL, 10);
00282     *value = v;
00283     return TRUE;
00284   } else
00285     return FALSE;
00286 }
00287 
00288 Bool wxGetResource(const char *section, const char *entry, int *value,
00289                  const char *file)
00290 {
00291     char *s = NULL;
00292     Bool succ;
00293     succ = wxGetResource(section, entry, &s, file);
00294     if (succ) {
00295        // Handle True, False here 
00296        // True, Yes, Enables, Set or  Activated 
00297        if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
00298            *value = TRUE;
00299        // False, No, Disabled, Reset, Cleared, Deactivated
00300        else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
00301            *value = FALSE;
00302        // Handle as Integer
00303        else {
00304          int v;
00305          v = (int)strtol(s, NULL, 10);
00306          *value = v;
00307        }
00308        return TRUE;
00309     } else
00310        return FALSE;
00311 }