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