-/*
-* We have a cache for writing different resource files,
-* which will only get flushed when we call wxFlushResources().
-* Build up a list of resource databases waiting to be written.
-*
-*/
-
-wxList wxResourceCache (wxKEY_STRING);
-
-void
-wxFlushResources (void)
-{
- char nameBuffer[512];
-
- wxNode *node = wxResourceCache.First ();
- while (node)
- {
- const char *file = node->GetKeyString();
- // If file doesn't exist, create it first.
- (void)GetResourcePath(nameBuffer, file, true);
-
- XrmDatabase database = (XrmDatabase) node->Data ();
- XrmPutFileDatabase (database, nameBuffer);
- XrmDestroyDatabase (database);
- wxNode *next = node->Next ();
- delete node;
- node = next;
- }
-}
-
-static XrmDatabase wxResourceDatabase = 0;
-
-void wxXMergeDatabases (wxApp * theApp, Display * display);
-
-bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
-{
- char buffer[500];
-
- (void) GetIniFile (buffer, file);
-
- XrmDatabase database;
- wxNode *node = wxResourceCache.Find (buffer);
- if (node)
- database = (XrmDatabase) node->Data ();
- else
- {
- database = XrmGetFileDatabase (buffer);
- wxResourceCache.Append (buffer, (wxObject *) database);
- }
-
- char resName[300];
- strcpy (resName, section.c_str());
- strcat (resName, ".");
- strcat (resName, entry.c_str());
-
- XrmPutStringResource (&database, resName, value);
- return true;
-}
-
-bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
-{
- char buf[50];
- sprintf(buf, "%.4f", value);
- return wxWriteResource(section, entry, buf, file);
-}
-
-bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
-{
- char buf[50];
- sprintf(buf, "%ld", value);
- return wxWriteResource(section, entry, buf, file);
-}
-
-bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
-{
- char buf[50];
- sprintf(buf, "%d", value);
- return wxWriteResource(section, entry, buf, file);
-}
-
-bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
-{
- if (!wxResourceDatabase)
- {
- Display *display = wxGlobalDisplay();
- wxXMergeDatabases (wxTheApp, display);
- }
-
- XrmDatabase database;
-
- if (!file.empty())
- {
- char buffer[500];
-
- // Is this right? Trying to get it to look in the user's
- // home directory instead of current directory -- JACS
- (void) GetIniFile (buffer, file);
-
- wxNode *node = wxResourceCache.Find (buffer);
- if (node)
- database = (XrmDatabase) node->Data ();
- else
- {
- database = XrmGetFileDatabase (buffer);
- wxResourceCache.Append (buffer, (wxObject *) database);
- }
- }
- else
- database = wxResourceDatabase;
-
- XrmValue xvalue;
- char *str_type[20];
- char buf[150];
- strcpy (buf, section);
- strcat (buf, ".");
- strcat (buf, entry);
-
- Bool success = XrmGetResource (database, buf, "*", str_type,
- &xvalue);
- // Try different combinations of upper/lower case, just in case...
- if (!success)
- {
- buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0]));
- success = XrmGetResource (database, buf, "*", str_type,
- &xvalue);
- }
- if (success)
- {
- if (*value)
- delete[] *value;
-
- *value = new char[xvalue.size + 1];
- strncpy (*value, xvalue.addr, (int) xvalue.size);
- return true;
- }
- return false;
-}
-
-bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
-{
- char *s = NULL;
- bool succ = wxGetResource(section, entry, (char **)&s, file);
- if (succ)
- {
- *value = (float)strtod(s, NULL);
- delete[] s;
- return true;
- }
- else return false;
-}
-
-bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
-{
- char *s = NULL;
- bool succ = wxGetResource(section, entry, (char **)&s, file);
- if (succ)
- {
- *value = strtol(s, NULL, 10);
- delete[] s;
- return true;
- }
- else return false;
-}
-
-bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
-{
- char *s = NULL;
- bool succ = wxGetResource(section, entry, (char **)&s, file);
- if (succ)
- {
- // Handle True, False here
- // True, Yes, Enables, Set or Activated
- if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
- *value = true;
- // False, No, Disabled, Reset, Cleared, Deactivated
- else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
- *value = false;
- // Handle as Integer
- else
- *value = (int) strtol (s, NULL, 10);
- delete[] s;
- return true;
- }
- else
- return false;
-}
-
-void wxXMergeDatabases (wxApp * theApp, Display * display)
-{
- XrmDatabase homeDB, serverDB, applicationDB;
- char filenamebuf[1024];
-
- char *filename = &filenamebuf[0];
- char *environment;
- wxString classname = theApp->GetClassName();
- char name[256];
- (void) strcpy (name, "/usr/lib/X11/app-defaults/");
- (void) strcat (name, classname.c_str());
-
- /* Get application defaults file, if any */
- applicationDB = XrmGetFileDatabase (name);
- (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
-
- /* Merge server defaults, created by xrdb, loaded as a property of the root
- * window when the server initializes and loaded into the display
- * structure on XOpenDisplay;
- * if not defined, use .Xdefaults
- */
-
- if (XResourceManagerString (display) != NULL)
- {
- serverDB = XrmGetStringDatabase (XResourceManagerString (display));
- }
- else
- {
- (void) GetIniFile (filename, NULL);
- serverDB = XrmGetFileDatabase (filename);
- }
- XrmMergeDatabases (serverDB, &wxResourceDatabase);
-
- /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
- * and merge into existing database
- */
-
- if ((environment = getenv ("XENVIRONMENT")) == NULL)
- {
- size_t len;
- environment = GetIniFile (filename, NULL);
- len = strlen (environment);
- wxString hostname = wxGetHostName();
- if ( !hostname.empty() )
- strncat(environment, hostname, 1024 - len);
- }
- homeDB = XrmGetFileDatabase (environment);
- XrmMergeDatabases (homeDB, &wxResourceDatabase);
-}
-
-#if 0
-
-/*
-* Not yet used but may be useful.
-*
-*/
-void
-wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
-{
- int i;
- Display *dpy = XtDisplay (w); // Retrieve the display pointer
-
- XrmDatabase rdb = NULL; // A resource data base
-
- // Create an empty resource database
- rdb = XrmGetStringDatabase ("");
-
- // Add the Component resources, prepending the name of the component
-
- i = 0;
- while (resourceSpec[i] != NULL)
- {
- char buf[1000];
-
- sprintf (buf, "*%s%s", name, resourceSpec[i++]);
- XrmPutLineResource (&rdb, buf);
- }
-
- // Merge them into the Xt database, with lowest precendence
-
- if (rdb)
- {
-#if (XlibSpecificationRelease>=5)
- XrmDatabase db = XtDatabase (dpy);
- XrmCombineDatabase (rdb, &db, False);
-#else
- XrmMergeDatabases (dpy->db, &rdb);
- dpy->db = rdb;
-#endif
- }
-}
-#endif
-// 0
-
-#endif // wxUSE_RESOURCES
-