moved Resource to FbTk and change name of DirHelper to FbTk Directory
fluxgen fluxgen
6 files changed,
495 insertions(+),
0 deletions(-)
A
src/FbTk/Directory.cc
@@ -0,0 +1,86 @@
+// Directory.cc +// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Directory.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ + +#include "Directory.hh" + +namespace FbTk { + +Directory::Directory(const char *dir):m_dir(0), +m_num_entries(0) { + if (dir != 0) + open(dir); +} + +Directory::~Directory() { + close(); +} + +void Directory::rewind() { + if (m_dir != 0) + rewinddir(m_dir); +} + +struct dirent *Directory::read() { + if (m_dir == 0) + return 0; + + return readdir(m_dir); +} + +std::string Directory::readFilename() { + dirent *ent = read(); + if (ent == 0) + return ""; + return (ent->d_name ? ent->d_name : ""); +} + +void Directory::close() { + if (m_dir != 0) { + closedir(m_dir); + m_dir = 0; + m_num_entries = 0; + } +} + + +bool Directory::open(const char *dir) { + if (dir == 0) + return false; + + if (m_dir != 0) + close(); + + m_dir = opendir(dir); + if (m_dir == 0) // successfull loading? + return false; + + // get number of entries + while (read()) + m_num_entries++; + + rewind(); // go back to start + + return true; +} + +}; // end namespace FbTk
A
src/FbTk/Directory.hh
@@ -0,0 +1,62 @@
+// Directory.hh +// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Directory.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ + +#ifndef FBTK_DIRECTORY_HH +#define FBTK_DIRECTORY_HH + +#include "NotCopyable.hh" + +#include <sys/types.h> +#include <dirent.h> +#include <string> + +namespace FbTk { + +/// Wrapper class for DIR * routines +class Directory: private FbTk::NotCopyable { +public: + explicit Directory(const char *dir = 0); + ~Directory(); + /// go to start of filelist + void rewind(); + /// gets next dirent info struct in directory and + /// jumps to next directory entry + struct dirent * read(); + /// reads next filename in directory + std::string readFilename(); + /// close directory + void close(); + /// open directory + /// @param dir the directory name + bool open(const char *dir); + /// @return number of entries in the directory + size_t entries() const { return m_num_entries; } + +private: + DIR *m_dir; + size_t m_num_entries; ///< number of file entries in directory +}; + +}; + +#endif // FBTK_DIRECTORY_HH
M
src/FbTk/Makefile.am
→
src/FbTk/Makefile.am
@@ -8,6 +8,7 @@ xmb_SOURCE= XmbFontImp.hh XmbFontImp.cc
endif libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ + Directory.hh Directory.cc \ EventHandler.hh EventManager.hh EventManager.cc \ FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \ ImageControl.hh ImageControl.cc \@@ -22,10 +23,12 @@ XFontImp.cc XFontImp.hh \
Button.hh Button.cc \ Layer.hh LayerItem.hh MultLayers.cc MultLayers.hh \ XLayer.cc XLayer.hh XLayerItem.cc XLayerItem.hh \ + Resource.hh Resource.cc \ StringUtil.hh StringUtil.cc \ Subject.hh Subject.cc Observer.hh Observer.cc \ Transparent.hh Transparent.cc \ FbPixmap.hh FbPixmap.cc \ FbDrawable.hh FbDrawable.cc \ + XrmDatabaseHelper.hh \ ${xft_SOURCE} \ ${xmb_SOURCE}
A
src/FbTk/Resource.cc
@@ -0,0 +1,119 @@
+// Resource.cc +// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Resource.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ + +#include "Resource.hh" +#include "XrmDatabaseHelper.hh" + +#include <iostream> +#include <cassert> + +using namespace std; + +namespace FbTk { + +bool ResourceManager::m_init = false; + +/** + loads a resourcefile + @return true on success else false +*/ +bool ResourceManager::load(const char *filename) { + assert(filename); + + ensureXrmIsInitialize(); + + XrmDatabaseHelper database; + database = XrmGetFileDatabase(filename); + if (database==0) + return false; + + XrmValue value; + char *value_type; + + //get list and go throu all the resources and load them + ResourceList::iterator i = m_resourcelist.begin(); + ResourceList::iterator i_end = m_resourcelist.end(); + for (; i != i_end; ++i) { + + Resource_base *resource = *i; + if (XrmGetResource(*database, resource->name().c_str(), + resource->altName().c_str(), &value_type, &value)) + resource->setFromString(value.addr); + else { + cerr<<"Failed to read: "<<resource->name()<<endl; + cerr<<"Setting default value"<<endl; + resource->setDefaultValue(); + } + } + + return true; +} + +/** + Saves all the resource to a file + @return 0 on success else negative value representing the error +*/ +bool ResourceManager::save(const char *filename, const char *mergefilename) { + assert(filename); + + ensureXrmIsInitialize(); + + XrmDatabaseHelper database; + + string rc_string; + ResourceList::iterator i = m_resourcelist.begin(); + ResourceList::iterator i_end = m_resourcelist.end(); + //write all resources to database + for (; i != i_end; ++i) { + Resource_base *resource = *i; + rc_string = resource->name() + string(": ") + resource->getString(); + XrmPutLineResource(&*database, rc_string.c_str()); + } + + if (database==0) + return false; + + //check if we want to merge a database + if (mergefilename) { + XrmDatabaseHelper olddatabase(mergefilename); + if (olddatabase == 0) // did we load the file? + return false; + + XrmMergeDatabases(*database, &*olddatabase); // merge databases + XrmPutFileDatabase(*olddatabase, filename); // save database to file + + *database = 0; // don't try to destroy the database + } else // save database to file + XrmPutFileDatabase(*database, filename); + + return true; +} + +void ResourceManager::ensureXrmIsInitialize() { + if (!m_init) { + XrmInitialize(); + m_init = true; + } +} + +}; // end namespace FbTk
A
src/FbTk/Resource.hh
@@ -0,0 +1,147 @@
+// Resource.hh +// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Resource.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ + +#ifndef FBTK_RESOURCE_HH +#define FBTK_RESOURCE_HH + +#include "NotCopyable.hh" + +#include <string> +#include <list> + +namespace FbTk { + +/// Base class for resources, this is only used in ResourceManager +class Resource_base:private FbTk::NotCopyable +{ +public: + virtual ~Resource_base() { }; + + /// set from string value + virtual void setFromString(char const *strval) = 0; + /// set default value + virtual void setDefaultValue() = 0; + /// get string value + virtual std::string getString() = 0; + /// get alternative name of this resource + inline const std::string& altName() const { return m_altname; } + /// get name of this resource + inline const std::string& name() const { return m_name; } + +protected: + Resource_base(const std::string &name, const std::string &altname): + m_name(name), m_altname(altname) + { } + +private: + std::string m_name; ///< name of this resource + std::string m_altname; ///< alternative name +}; + +template <typename T> +class Resource; + +class ResourceManager +{ +public: + typedef std::list<Resource_base *> ResourceList; + + ResourceManager() { } + virtual ~ResourceManager() {} + + /// Load all resources registered to this class + /// @return true on success + virtual bool load(const char *filename); + + /// Save all resouces registered to this class + /// @return true on success + virtual bool save(const char *filename, const char *mergefilename=0); + + /// Add resource to list, only used in Resource<T> + template <class T> + void addResource(Resource<T> &r) { + m_resourcelist.push_back(&r); + m_resourcelist.unique(); + } + + /// Remove a specific resource, only used in Resource<T> + template <class T> + void removeResource(Resource<T> &r) { + m_resourcelist.remove(&r); + } + +protected: + static void ensureXrmIsInitialize(); + +private: + static bool m_init; + ResourceList m_resourcelist; +}; + + +/// Real resource class +/** + * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n + * and then implement setFromString and getString \n + * example: \n + * template <> \n + * void Resource<int>::setFromString(const char *str) { \n + * *(*this) = atoi(str); \n + * } + */ +template <typename T> +class Resource:public Resource_base +{ +public: + Resource(ResourceManager &rm, T val, + const std::string &name, const std::string &altname): + Resource_base(name, altname), + m_value(val), m_defaultval(val), + m_rm(rm) + { + m_rm.addResource(*this); // add this to resource handler + } + virtual ~Resource() { + m_rm.removeResource(*this); // remove this from resource handler + } + + inline void setDefaultValue() { m_value = m_defaultval; } + /// sets resource from string, specialized, must be implemented + void setFromString(const char *strval); + inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} + /// specialized, must be implemented + /// @return string value of resource + std::string getString(); + + inline T& operator*() { return m_value; } + inline const T& operator*() const { return m_value; } + inline T *operator->() { return &m_value; } + inline const T *operator->() const { return &m_value; } +private: + T m_value, m_defaultval; + ResourceManager &m_rm; +}; + +}; // end namespace FbTk + +#endif // FBTK_RESOURCE_HH
A
src/FbTk/XrmDatabaseHelper.hh
@@ -0,0 +1,78 @@
+// XrmDatabaseHelper.hh +// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: XrmDatabaseHelper.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ + +// This is a helper for XrmDatabase +// when database goes out of scope +// the XrmDatabase variable will be destroyed. + +#ifndef XRMDATABASEHELPER_HH +#define XRMDATABASEHELPER_HH + +#include <X11/Xlib.h> +#include <X11/Xresource.h> + +/** + Helper class for XrmDatabase. +*/ +class XrmDatabaseHelper +{ +public: + XrmDatabaseHelper(char const * filename=0) + : m_database(0) + { if (filename != 0) load(filename); } + + ~XrmDatabaseHelper() { + close(); + } + + /// assignment operator + XrmDatabaseHelper& operator=(const XrmDatabase& database) { + if (m_database!=0) + XrmDestroyDatabase(m_database); + m_database = database; + return *this; + } + bool load(const char *filename) { + if (filename == 0) + return false; + XrmDatabase db = XrmGetFileDatabase(filename); + if (db == 0) + return false; + close(); // close old database + m_database = db; // set new and return true + return true; + } + void close() { + if (m_database != 0) { + XrmDestroyDatabase(m_database); + m_database = 0; + } + } + bool operator == (const XrmDatabase& database) { return m_database == database; } + XrmDatabase & operator*(void) { return m_database; } + +private: + XrmDatabase m_database; +}; + +#endif //_XRMDATABASEHELPER_HH_