diff options
Diffstat (limited to 'src/FbTk/Theme.hh')
-rw-r--r-- | src/FbTk/Theme.hh | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/FbTk/Theme.hh b/src/FbTk/Theme.hh new file mode 100644 index 0000000..ef9a13f --- /dev/null +++ b/src/FbTk/Theme.hh | |||
@@ -0,0 +1,175 @@ | |||
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.7 2003/08/19 21:25:26 fluxgen 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 | private: | ||
110 | const int m_screen_num; | ||
111 | typedef std::list<ThemeItem_base *> ItemList; | ||
112 | ItemList m_themeitems; | ||
113 | FbTk::Subject m_reconfig_sig; | ||
114 | }; | ||
115 | |||
116 | |||
117 | /// Singleton theme manager | ||
118 | /** | ||
119 | Use this to load all the registred themes | ||
120 | */ | ||
121 | class ThemeManager { | ||
122 | public: | ||
123 | static ThemeManager &instance(); | ||
124 | bool load(const std::string &filename); | ||
125 | std::string resourceValue(const std::string &name, const std::string &altname); | ||
126 | void loadTheme(Theme &tm); | ||
127 | bool loadItem(ThemeItem_base &resource); | ||
128 | bool loadItem(ThemeItem_base &resource, const std::string &name, const std::string &altname); | ||
129 | private: | ||
130 | ThemeManager(); | ||
131 | ~ThemeManager() { } | ||
132 | |||
133 | friend class FbTk::Theme; // so only theme can register itself in constructor | ||
134 | /// @return false if screen_num if out of | ||
135 | /// range or theme already registered, else true | ||
136 | bool registerTheme(FbTk::Theme &tm); | ||
137 | /// @return false if theme isn't registred in the manager | ||
138 | bool unregisterTheme(FbTk::Theme &tm); | ||
139 | /// map each theme manager to a screen | ||
140 | typedef std::list<FbTk::Theme *> ThemeList; | ||
141 | ThemeList m_themelist; | ||
142 | const int m_max_screens; | ||
143 | XrmDatabaseHelper m_database; | ||
144 | }; | ||
145 | |||
146 | |||
147 | |||
148 | template <typename T> | ||
149 | ThemeItem<T>::ThemeItem(FbTk::Theme &tm, | ||
150 | const std::string &name, const std::string &altname): | ||
151 | ThemeItem_base(name, altname), | ||
152 | m_tm(tm) { | ||
153 | tm.add(*this); | ||
154 | } | ||
155 | |||
156 | template <typename T> | ||
157 | ThemeItem<T>::~ThemeItem() { | ||
158 | m_tm.remove(*this); | ||
159 | } | ||
160 | |||
161 | template <typename T> | ||
162 | void Theme::add(ThemeItem<T> &item) { | ||
163 | m_themeitems.push_back(&item); | ||
164 | m_themeitems.unique(); | ||
165 | } | ||
166 | |||
167 | template <typename T> | ||
168 | void Theme::remove(ThemeItem<T> &item) { | ||
169 | m_themeitems.remove(&item); | ||
170 | } | ||
171 | |||
172 | }; // end namespace FbTk | ||
173 | |||
174 | #endif // FBTK_THEME_HH | ||
175 | |||