aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2003-05-18 22:08:19 (GMT)
committerfluxgen <fluxgen>2003-05-18 22:08:19 (GMT)
commit0d34ca1ea0874667829891acf19ee69323d0f410 (patch)
treea56e530c5ab64d23e236446a52c3141c9455195f
parent251ca294abe7af0cbe5caf3c33052292cad42fa5 (diff)
downloadfluxbox-0d34ca1ea0874667829891acf19ee69323d0f410.zip
fluxbox-0d34ca1ea0874667829891acf19ee69323d0f410.tar.bz2
moved Resource to FbTk and change name of DirHelper to FbTk Directory
-rw-r--r--src/FbTk/Directory.cc86
-rw-r--r--src/FbTk/Directory.hh62
-rw-r--r--src/FbTk/Makefile.am3
-rw-r--r--src/FbTk/Resource.cc119
-rw-r--r--src/FbTk/Resource.hh147
-rw-r--r--src/FbTk/XrmDatabaseHelper.hh78
6 files changed, 495 insertions, 0 deletions
diff --git a/src/FbTk/Directory.cc b/src/FbTk/Directory.cc
new file mode 100644
index 0000000..8d327dd
--- /dev/null
+++ b/src/FbTk/Directory.cc
@@ -0,0 +1,86 @@
1// Directory.cc
2// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Directory.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $
23
24#include "Directory.hh"
25
26namespace FbTk {
27
28Directory::Directory(const char *dir):m_dir(0),
29m_num_entries(0) {
30 if (dir != 0)
31 open(dir);
32}
33
34Directory::~Directory() {
35 close();
36}
37
38void Directory::rewind() {
39 if (m_dir != 0)
40 rewinddir(m_dir);
41}
42
43struct dirent *Directory::read() {
44 if (m_dir == 0)
45 return 0;
46
47 return readdir(m_dir);
48}
49
50std::string Directory::readFilename() {
51 dirent *ent = read();
52 if (ent == 0)
53 return "";
54 return (ent->d_name ? ent->d_name : "");
55}
56
57void Directory::close() {
58 if (m_dir != 0) {
59 closedir(m_dir);
60 m_dir = 0;
61 m_num_entries = 0;
62 }
63}
64
65
66bool Directory::open(const char *dir) {
67 if (dir == 0)
68 return false;
69
70 if (m_dir != 0)
71 close();
72
73 m_dir = opendir(dir);
74 if (m_dir == 0) // successfull loading?
75 return false;
76
77 // get number of entries
78 while (read())
79 m_num_entries++;
80
81 rewind(); // go back to start
82
83 return true;
84}
85
86}; // end namespace FbTk
diff --git a/src/FbTk/Directory.hh b/src/FbTk/Directory.hh
new file mode 100644
index 0000000..0786214
--- /dev/null
+++ b/src/FbTk/Directory.hh
@@ -0,0 +1,62 @@
1// Directory.hh
2// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Directory.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $
23
24#ifndef FBTK_DIRECTORY_HH
25#define FBTK_DIRECTORY_HH
26
27#include "NotCopyable.hh"
28
29#include <sys/types.h>
30#include <dirent.h>
31#include <string>
32
33namespace FbTk {
34
35/// Wrapper class for DIR * routines
36class Directory: private FbTk::NotCopyable {
37public:
38 explicit Directory(const char *dir = 0);
39 ~Directory();
40 /// go to start of filelist
41 void rewind();
42 /// gets next dirent info struct in directory and
43 /// jumps to next directory entry
44 struct dirent * read();
45 /// reads next filename in directory
46 std::string readFilename();
47 /// close directory
48 void close();
49 /// open directory
50 /// @param dir the directory name
51 bool open(const char *dir);
52 /// @return number of entries in the directory
53 size_t entries() const { return m_num_entries; }
54
55private:
56 DIR *m_dir;
57 size_t m_num_entries; ///< number of file entries in directory
58};
59
60};
61
62#endif // FBTK_DIRECTORY_HH
diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am
index 4d8318b..67b7b65 100644
--- a/src/FbTk/Makefile.am
+++ b/src/FbTk/Makefile.am
@@ -8,6 +8,7 @@ xmb_SOURCE= XmbFontImp.hh XmbFontImp.cc
8endif 8endif
9 9
10libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ 10libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
11 Directory.hh Directory.cc \
11 EventHandler.hh EventManager.hh EventManager.cc \ 12 EventHandler.hh EventManager.hh EventManager.cc \
12 FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \ 13 FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \
13 ImageControl.hh ImageControl.cc \ 14 ImageControl.hh ImageControl.cc \
@@ -22,10 +23,12 @@ libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
22 Button.hh Button.cc \ 23 Button.hh Button.cc \
23 Layer.hh LayerItem.hh MultLayers.cc MultLayers.hh \ 24 Layer.hh LayerItem.hh MultLayers.cc MultLayers.hh \
24 XLayer.cc XLayer.hh XLayerItem.cc XLayerItem.hh \ 25 XLayer.cc XLayer.hh XLayerItem.cc XLayerItem.hh \
26 Resource.hh Resource.cc \
25 StringUtil.hh StringUtil.cc \ 27 StringUtil.hh StringUtil.cc \
26 Subject.hh Subject.cc Observer.hh Observer.cc \ 28 Subject.hh Subject.cc Observer.hh Observer.cc \
27 Transparent.hh Transparent.cc \ 29 Transparent.hh Transparent.cc \
28 FbPixmap.hh FbPixmap.cc \ 30 FbPixmap.hh FbPixmap.cc \
29 FbDrawable.hh FbDrawable.cc \ 31 FbDrawable.hh FbDrawable.cc \
32 XrmDatabaseHelper.hh \
30 ${xft_SOURCE} \ 33 ${xft_SOURCE} \
31 ${xmb_SOURCE} 34 ${xmb_SOURCE}
diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc
new file mode 100644
index 0000000..a222984
--- /dev/null
+++ b/src/FbTk/Resource.cc
@@ -0,0 +1,119 @@
1// Resource.cc
2// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Resource.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $
23
24#include "Resource.hh"
25#include "XrmDatabaseHelper.hh"
26
27#include <iostream>
28#include <cassert>
29
30using namespace std;
31
32namespace FbTk {
33
34bool ResourceManager::m_init = false;
35
36/**
37 loads a resourcefile
38 @return true on success else false
39*/
40bool ResourceManager::load(const char *filename) {
41 assert(filename);
42
43 ensureXrmIsInitialize();
44
45 XrmDatabaseHelper database;
46 database = XrmGetFileDatabase(filename);
47 if (database==0)
48 return false;
49
50 XrmValue value;
51 char *value_type;
52
53 //get list and go throu all the resources and load them
54 ResourceList::iterator i = m_resourcelist.begin();
55 ResourceList::iterator i_end = m_resourcelist.end();
56 for (; i != i_end; ++i) {
57
58 Resource_base *resource = *i;
59 if (XrmGetResource(*database, resource->name().c_str(),
60 resource->altName().c_str(), &value_type, &value))
61 resource->setFromString(value.addr);
62 else {
63 cerr<<"Failed to read: "<<resource->name()<<endl;
64 cerr<<"Setting default value"<<endl;
65 resource->setDefaultValue();
66 }
67 }
68
69 return true;
70}
71
72/**
73 Saves all the resource to a file
74 @return 0 on success else negative value representing the error
75*/
76bool ResourceManager::save(const char *filename, const char *mergefilename) {
77 assert(filename);
78
79 ensureXrmIsInitialize();
80
81 XrmDatabaseHelper database;
82
83 string rc_string;
84 ResourceList::iterator i = m_resourcelist.begin();
85 ResourceList::iterator i_end = m_resourcelist.end();
86 //write all resources to database
87 for (; i != i_end; ++i) {
88 Resource_base *resource = *i;
89 rc_string = resource->name() + string(": ") + resource->getString();
90 XrmPutLineResource(&*database, rc_string.c_str());
91 }
92
93 if (database==0)
94 return false;
95
96 //check if we want to merge a database
97 if (mergefilename) {
98 XrmDatabaseHelper olddatabase(mergefilename);
99 if (olddatabase == 0) // did we load the file?
100 return false;
101
102 XrmMergeDatabases(*database, &*olddatabase); // merge databases
103 XrmPutFileDatabase(*olddatabase, filename); // save database to file
104
105 *database = 0; // don't try to destroy the database
106 } else // save database to file
107 XrmPutFileDatabase(*database, filename);
108
109 return true;
110}
111
112void ResourceManager::ensureXrmIsInitialize() {
113 if (!m_init) {
114 XrmInitialize();
115 m_init = true;
116 }
117}
118
119}; // end namespace FbTk
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
new file mode 100644
index 0000000..4471225
--- /dev/null
+++ b/src/FbTk/Resource.hh
@@ -0,0 +1,147 @@
1// Resource.hh
2// Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Resource.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $
23
24#ifndef FBTK_RESOURCE_HH
25#define FBTK_RESOURCE_HH
26
27#include "NotCopyable.hh"
28
29#include <string>
30#include <list>
31
32namespace FbTk {
33
34/// Base class for resources, this is only used in ResourceManager
35class Resource_base:private FbTk::NotCopyable
36{
37public:
38 virtual ~Resource_base() { };
39
40 /// set from string value
41 virtual void setFromString(char const *strval) = 0;
42 /// set default value
43 virtual void setDefaultValue() = 0;
44 /// get string value
45 virtual std::string getString() = 0;
46 /// get alternative name of this resource
47 inline const std::string& altName() const { return m_altname; }
48 /// get name of this resource
49 inline const std::string& name() const { return m_name; }
50
51protected:
52 Resource_base(const std::string &name, const std::string &altname):
53 m_name(name), m_altname(altname)
54 { }
55
56private:
57 std::string m_name; ///< name of this resource
58 std::string m_altname; ///< alternative name
59};
60
61template <typename T>
62class Resource;
63
64class ResourceManager
65{
66public:
67 typedef std::list<Resource_base *> ResourceList;
68
69 ResourceManager() { }
70 virtual ~ResourceManager() {}
71
72 /// Load all resources registered to this class
73 /// @return true on success
74 virtual bool load(const char *filename);
75
76 /// Save all resouces registered to this class
77 /// @return true on success
78 virtual bool save(const char *filename, const char *mergefilename=0);
79
80 /// Add resource to list, only used in Resource<T>
81 template <class T>
82 void addResource(Resource<T> &r) {
83 m_resourcelist.push_back(&r);
84 m_resourcelist.unique();
85 }
86
87 /// Remove a specific resource, only used in Resource<T>
88 template <class T>
89 void removeResource(Resource<T> &r) {
90 m_resourcelist.remove(&r);
91 }
92
93protected:
94 static void ensureXrmIsInitialize();
95
96private:
97 static bool m_init;
98 ResourceList m_resourcelist;
99};
100
101
102/// Real resource class
103/**
104 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n
105 * and then implement setFromString and getString \n
106 * example: \n
107 * template <> \n
108 * void Resource<int>::setFromString(const char *str) { \n
109 * *(*this) = atoi(str); \n
110 * }
111 */
112template <typename T>
113class Resource:public Resource_base
114{
115public:
116 Resource(ResourceManager &rm, T val,
117 const std::string &name, const std::string &altname):
118 Resource_base(name, altname),
119 m_value(val), m_defaultval(val),
120 m_rm(rm)
121 {
122 m_rm.addResource(*this); // add this to resource handler
123 }
124 virtual ~Resource() {
125 m_rm.removeResource(*this); // remove this from resource handler
126 }
127
128 inline void setDefaultValue() { m_value = m_defaultval; }
129 /// sets resource from string, specialized, must be implemented
130 void setFromString(const char *strval);
131 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
132 /// specialized, must be implemented
133 /// @return string value of resource
134 std::string getString();
135
136 inline T& operator*() { return m_value; }
137 inline const T& operator*() const { return m_value; }
138 inline T *operator->() { return &m_value; }
139 inline const T *operator->() const { return &m_value; }
140private:
141 T m_value, m_defaultval;
142 ResourceManager &m_rm;
143};
144
145}; // end namespace FbTk
146
147#endif // FBTK_RESOURCE_HH
diff --git a/src/FbTk/XrmDatabaseHelper.hh b/src/FbTk/XrmDatabaseHelper.hh
new file mode 100644
index 0000000..1b2618d
--- /dev/null
+++ b/src/FbTk/XrmDatabaseHelper.hh
@@ -0,0 +1,78 @@
1// XrmDatabaseHelper.hh
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: XrmDatabaseHelper.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $
23
24// This is a helper for XrmDatabase
25// when database goes out of scope
26// the XrmDatabase variable will be destroyed.
27
28#ifndef XRMDATABASEHELPER_HH
29#define XRMDATABASEHELPER_HH
30
31#include <X11/Xlib.h>
32#include <X11/Xresource.h>
33
34/**
35 Helper class for XrmDatabase.
36*/
37class XrmDatabaseHelper
38{
39public:
40 XrmDatabaseHelper(char const * filename=0)
41 : m_database(0)
42 { if (filename != 0) load(filename); }
43
44 ~XrmDatabaseHelper() {
45 close();
46 }
47
48 /// assignment operator
49 XrmDatabaseHelper& operator=(const XrmDatabase& database) {
50 if (m_database!=0)
51 XrmDestroyDatabase(m_database);
52 m_database = database;
53 return *this;
54 }
55 bool load(const char *filename) {
56 if (filename == 0)
57 return false;
58 XrmDatabase db = XrmGetFileDatabase(filename);
59 if (db == 0)
60 return false;
61 close(); // close old database
62 m_database = db; // set new and return true
63 return true;
64 }
65 void close() {
66 if (m_database != 0) {
67 XrmDestroyDatabase(m_database);
68 m_database = 0;
69 }
70 }
71 bool operator == (const XrmDatabase& database) { return m_database == database; }
72 XrmDatabase & operator*(void) { return m_database; }
73
74private:
75 XrmDatabase m_database;
76};
77
78#endif //_XRMDATABASEHELPER_HH_