diff options
Diffstat (limited to 'src/FbTk/Theme.hh')
-rw-r--r-- | src/FbTk/Theme.hh | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/FbTk/Theme.hh b/src/FbTk/Theme.hh new file mode 100644 index 0000000..bea4680 --- /dev/null +++ b/src/FbTk/Theme.hh | |||
@@ -0,0 +1,186 @@ | |||
1 | // Theme.hh for FbTk - Fluxbox ToolKit | ||
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: Theme.hh,v 1.11 2003/11/16 22:33:56 rathnor Exp $ | ||
23 | |||
24 | /** | ||
25 | @file holds ThemeItem<T>, Theme and ThemeManager which is the base for any theme | ||
26 | */ | ||
27 | |||
28 | #ifndef FBTK_THEME_HH | ||
29 | #define FBTK_THEME_HH | ||
30 | |||
31 | #include <string> | ||
32 | #include <list> | ||
33 | #include <string> | ||
34 | |||
35 | #include "XrmDatabaseHelper.hh" | ||
36 | #include "Subject.hh" | ||
37 | |||
38 | namespace FbTk { | ||
39 | |||
40 | class Theme; | ||
41 | |||
42 | /// Base class for ThemeItem, holds name and altname | ||
43 | /** | ||
44 | @see ThemeItem | ||
45 | */ | ||
46 | class ThemeItem_base { | ||
47 | public: | ||
48 | ThemeItem_base(const std::string &name, const std::string &altname): | ||
49 | m_name(name), m_altname(altname) { } | ||
50 | virtual ~ThemeItem_base() { } | ||
51 | virtual void setFromString(const char *str) = 0; | ||
52 | virtual void setDefaultValue() = 0; | ||
53 | virtual void load() = 0; // if it needs to load additional stuff | ||
54 | const std::string &name() const { return m_name; } | ||
55 | const std::string &altName() const { return m_altname; } | ||
56 | private: | ||
57 | std::string m_name, m_altname; | ||
58 | }; | ||
59 | |||
60 | |||
61 | /// template ThemeItem class for basic theme items | ||
62 | /// to use this you need to specialize setDefaultValue, setFromString and load | ||
63 | template <typename T> | ||
64 | class ThemeItem:public ThemeItem_base { | ||
65 | public: | ||
66 | ThemeItem(FbTk::Theme &tm, const std::string &name, const std::string &altname); | ||
67 | virtual ~ThemeItem(); | ||
68 | /// specialized | ||
69 | void setDefaultValue(); | ||
70 | /// specialized | ||
71 | void setFromString(const char *strval); | ||
72 | /// specialized | ||
73 | void load(); | ||
74 | /** | ||
75 | @name access operators | ||
76 | */ | ||
77 | /**@{*/ | ||
78 | inline T& operator*() { return m_value; } | ||
79 | inline const T& operator*() const { return m_value; } | ||
80 | inline T *operator->() { return &m_value; } | ||
81 | inline const T *operator->() const { return &m_value; } | ||
82 | /**@}*/ | ||
83 | |||
84 | FbTk::Theme &theme() { return m_tm; } | ||
85 | private: | ||
86 | |||
87 | T m_value; | ||
88 | FbTk::Theme &m_tm; | ||
89 | }; | ||
90 | |||
91 | |||
92 | /// Hold ThemeItems. Use this to create a Theme set | ||
93 | class Theme { | ||
94 | public: | ||
95 | explicit Theme(int screen_num); // create a theme for a specific screen | ||
96 | virtual ~Theme(); | ||
97 | virtual void reconfigTheme() = 0; | ||
98 | int screenNum() const { return m_screen_num; } | ||
99 | std::list<ThemeItem_base *> &itemList() { return m_themeitems; } | ||
100 | const std::list<ThemeItem_base *> &itemList() const { return m_themeitems; } | ||
101 | /// add ThemeItem | ||
102 | template <typename T> | ||
103 | void add(ThemeItem<T> &item); | ||
104 | /// remove ThemeItem | ||
105 | template <typename T> | ||
106 | void remove(ThemeItem<T> &item); | ||
107 | virtual bool fallback(ThemeItem_base &base) { return false; } | ||
108 | FbTk::Subject &reconfigSig() { return m_reconfig_sig; } | ||
109 | |||
110 | |||
111 | private: | ||
112 | const int m_screen_num; | ||
113 | typedef std::list<ThemeItem_base *> ItemList; | ||
114 | ItemList m_themeitems; | ||
115 | FbTk::Subject m_reconfig_sig; | ||
116 | }; | ||
117 | |||
118 | |||
119 | /// Singleton theme manager | ||
120 | /** | ||
121 | Use this to load all the registred themes | ||
122 | */ | ||
123 | class ThemeManager { | ||
124 | public: | ||
125 | static ThemeManager &instance(); | ||
126 | |||
127 | bool load(const std::string &filename); | ||
128 | std::string resourceValue(const std::string &name, const std::string &altname); | ||
129 | void loadTheme(Theme &tm); | ||
130 | bool loadItem(ThemeItem_base &resource); | ||
131 | bool loadItem(ThemeItem_base &resource, const std::string &name, const std::string &altname); | ||
132 | |||
133 | bool verbose() const { return m_verbose; } | ||
134 | void setVerbose(bool value) { m_verbose = value; } | ||
135 | // void listItems(); | ||
136 | private: | ||
137 | ThemeManager(); | ||
138 | ~ThemeManager() { } | ||
139 | |||
140 | friend class FbTk::Theme; // so only theme can register itself in constructor | ||
141 | /// @return false if screen_num if out of | ||
142 | /// range or theme already registered, else true | ||
143 | bool registerTheme(FbTk::Theme &tm); | ||
144 | /// @return false if theme isn't registred in the manager | ||
145 | bool unregisterTheme(FbTk::Theme &tm); | ||
146 | /// map each theme manager to a screen | ||
147 | typedef std::list<FbTk::Theme *> ThemeList; | ||
148 | ThemeList m_themelist; | ||
149 | const int m_max_screens; | ||
150 | XrmDatabaseHelper m_database; | ||
151 | bool m_verbose; | ||
152 | |||
153 | std::string m_themelocation; | ||
154 | }; | ||
155 | |||
156 | |||
157 | |||
158 | template <typename T> | ||
159 | ThemeItem<T>::ThemeItem(FbTk::Theme &tm, | ||
160 | const std::string &name, const std::string &altname): | ||
161 | ThemeItem_base(name, altname), | ||
162 | m_tm(tm) { | ||
163 | tm.add(*this); | ||
164 | setDefaultValue(); | ||
165 | } | ||
166 | |||
167 | template <typename T> | ||
168 | ThemeItem<T>::~ThemeItem() { | ||
169 | m_tm.remove(*this); | ||
170 | } | ||
171 | |||
172 | template <typename T> | ||
173 | void Theme::add(ThemeItem<T> &item) { | ||
174 | m_themeitems.push_back(&item); | ||
175 | m_themeitems.unique(); | ||
176 | } | ||
177 | |||
178 | template <typename T> | ||
179 | void Theme::remove(ThemeItem<T> &item) { | ||
180 | m_themeitems.remove(&item); | ||
181 | } | ||
182 | |||
183 | }; // end namespace FbTk | ||
184 | |||
185 | #endif // FBTK_THEME_HH | ||
186 | |||