aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2002-12-02 19:34:54 (GMT)
committerfluxgen <fluxgen>2002-12-02 19:34:54 (GMT)
commitc8aff5576a187f3921a0db998cd5a47173b28742 (patch)
tree17a3e25a6429caa7ff24052368c03da2182bb20a /src/FbTk
parent3d18945fb5b660cef4ff32668c15f2b565f4e230 (diff)
downloadfluxbox_paul-c8aff5576a187f3921a0db998cd5a47173b28742.zip
fluxbox_paul-c8aff5576a187f3921a0db998cd5a47173b28742.tar.bz2
theme classes
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/Theme.cc187
-rw-r--r--src/FbTk/Theme.hh162
2 files changed, 349 insertions, 0 deletions
diff --git a/src/FbTk/Theme.cc b/src/FbTk/Theme.cc
new file mode 100644
index 0000000..1e2ea3a
--- /dev/null
+++ b/src/FbTk/Theme.cc
@@ -0,0 +1,187 @@
1// Theme.cc for FbTk - Fluxbox ToolKit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen at 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: Theme.cc,v 1.1 2002/12/02 19:34:54 fluxgen Exp $
23
24#include "Theme.hh"
25
26#include "../XrmDatabaseHelper.hh"
27#include "Font.hh"
28#include "Color.hh"
29#include "Texture.hh"
30#include "App.hh"
31
32#include <iostream>
33
34using namespace std;
35namespace FbTk {
36
37// create default handlers for Color, Font, and Texture
38
39template <>
40void ThemeItem<FbTk::Font>::setDefaultValue() {
41 if (!m_value.load("fixed")) {
42 cerr<<"FbTk::ThemeItem<FbTk::Font>: Warning! Failed to load default value 'fixed'"<<endl;
43 }
44}
45
46template <>
47void ThemeItem<FbTk::Font>::setFromString(const char *str) {
48 if (m_value.load(str) == false) {
49 cerr<<"Failed to load font: "<<str<<endl;
50 cerr<<"Setting default value"<<endl;
51 setDefaultValue();
52 }
53
54}
55
56// do nothing
57template <>
58void ThemeItem<FbTk::Font>::load() {
59}
60
61
62template <>
63void ThemeItem<FbTk::Texture>::setFromString(const char *str) {
64 m_value.setFromString(str);
65}
66
67template <>
68void ThemeItem<FbTk::Texture>::setDefaultValue() {
69 m_value.setType(0);
70}
71
72template <>
73void ThemeItem<FbTk::Texture>::load() {
74 string color_name(ThemeManager::instance().resourceValue(name()+".color", altName()+".Color"));
75 string colorto_name(ThemeManager::instance().resourceValue(name()+".colorTo", altName()+".ColorTo"));
76 m_value.color().setFromString(color_name.c_str(), m_tm.screenNum());
77 m_value.colorTo().setFromString(colorto_name.c_str(), m_tm.screenNum());
78}
79
80template <>
81void ThemeItem<FbTk::Color>::setFromString(const char *str) {
82 m_value.setFromString(str, m_tm.screenNum());
83}
84
85template <>
86void ThemeItem<FbTk::Color>::setDefaultValue() {
87 m_value.setPixel(0xFFFFFFFF);
88}
89
90// does nothing
91template <>
92void ThemeItem<FbTk::Color>::load() { }
93
94Theme::Theme(int screen_num):m_screen_num(screen_num) {
95
96 if (!ThemeManager::instance().registerTheme(*this)) {
97 // should it be fatal or not?
98 cerr<<"FbTk::Theme Warning: Failed to register Theme"<<endl;
99 }
100}
101
102Theme::~Theme() {
103 if (!ThemeManager::instance().unregisterTheme(*this)) {
104#ifdef DEBUG
105 cerr<<"Warning: Theme not registered!"<<endl;
106#endif // DEBUG
107 }
108}
109
110ThemeManager &ThemeManager::instance() {
111 static ThemeManager tm;
112 return tm;
113}
114
115ThemeManager::ThemeManager():
116 m_max_screens(ScreenCount(FbTk::App::instance()->display())) {
117
118}
119
120bool ThemeManager::registerTheme(Theme &tm) {
121 // valid screen num?
122 if (m_max_screens < tm.screenNum() || tm.screenNum() < 0)
123 return false;
124 // TODO: use find and return false if it's already there
125 // instead of unique
126 m_themelist.push_back(&tm);
127 m_themelist.unique();
128 return true;
129}
130
131bool ThemeManager::unregisterTheme(Theme &tm) {
132 m_themelist.remove(&tm);
133 return true;
134}
135
136bool ThemeManager::load(const char *filename) {
137
138 if (!m_database.load(filename))
139 return false;
140
141 //get list and go throu all the resources and load them
142 ThemeList::iterator theme_it = m_themelist.begin();
143 const ThemeList::iterator theme_it_end = m_themelist.end();
144 for (; theme_it != theme_it_end; ++theme_it) {
145 loadTheme(**theme_it);
146 }
147
148 return true;
149}
150
151void ThemeManager::loadTheme(Theme &tm) {
152
153 XrmValue value;
154 char *value_type;
155
156 std::list<ThemeItem_base *>::iterator i = tm.itemList().begin();
157 std::list<ThemeItem_base *>::iterator i_end = tm.itemList().end();
158 for (; i != i_end; ++i) {
159 ThemeItem_base *resource = *i;
160 if (XrmGetResource(*m_database, resource->name().c_str(),
161 resource->altName().c_str(), &value_type, &value)) {
162 resource->setFromString(value.addr);
163 resource->load(); // load additional stuff by the ThemeItem
164 } else {
165 cerr<<"Failed to read theme item: "<<resource->name()<<endl;
166 cerr<<"Setting default value"<<endl;
167 resource->setDefaultValue();
168 }
169 }
170 // send reconfiguration signal to theme
171 tm.reconfigTheme();
172
173}
174
175std::string ThemeManager::resourceValue(const std::string &name, const std::string &altname) {
176 XrmValue value;
177 char *value_type;
178
179 if (*m_database != 0 && XrmGetResource(*m_database, name.c_str(),
180 altname.c_str(), &value_type, &value) && value.addr != 0) {
181 return string(value.addr);
182 }
183 return "";
184}
185
186
187}; // end namespace FbTk
diff --git a/src/FbTk/Theme.hh b/src/FbTk/Theme.hh
new file mode 100644
index 0000000..bd02666
--- /dev/null
+++ b/src/FbTk/Theme.hh
@@ -0,0 +1,162 @@
1// Theme.hh for FbTk - Fluxbox ToolKit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen at 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: Theme.hh,v 1.1 2002/12/02 19:34:51 fluxgen Exp $
23
24// holds ThemeItem, Theme and ThemeManager
25
26
27#include <string>
28#include <list>
29#include <string>
30
31#include "../XrmDatabaseHelper.hh"
32
33namespace FbTk {
34
35class Theme;
36/**
37 base class for themeitem, holds name and altname
38 */
39class ThemeItem_base {
40public:
41 ThemeItem_base(const std::string &name, const std::string &altname):
42 m_name(name), m_altname(altname) { }
43 virtual ~ThemeItem_base() { }
44 virtual void setFromString(const char *str) = 0;
45 virtual void setDefaultValue() = 0;
46 virtual void load() = 0; // if it needs to load additional stuff
47 const std::string &name() const { return m_name; }
48 const std::string &altName() const { return m_altname; }
49private:
50 std::string m_name, m_altname;
51};
52
53/**
54 template ThemeItem class for things like Texture and Color
55*/
56template <typename T>
57class ThemeItem:public ThemeItem_base {
58public:
59 ThemeItem(FbTk::Theme &tm, const std::string &name, const std::string &altname);
60 virtual ~ThemeItem();
61 /// specialized
62 void setDefaultValue();
63 /// specialized
64 void setFromString(const char *strval);
65 /// specialized
66 void load();
67 /**
68 @name access operators
69 */
70 /**@{*/
71 inline T& operator*() { return m_value; }
72 inline const T& operator*() const { return m_value; }
73 inline T *operator->() { return &m_value; }
74 inline const T *operator->() const { return &m_value; }
75 /**@}*/
76private:
77
78 T m_value;
79 FbTk::Theme &m_tm;
80};
81
82
83
84/**
85 Hold themeitems. Use this to create a Theme set
86*/
87class Theme {
88public:
89 explicit Theme(int screen_num); // create a theme for a specific screen
90 virtual ~Theme();
91 virtual void reconfigTheme() = 0;
92 int screenNum() const { return m_screen_num; }
93 std::list<ThemeItem_base *> &itemList() { return m_themeitems; }
94 const std::list<ThemeItem_base *> &itemList() const { return m_themeitems; }
95 /// add ThemeItem
96 template <typename T>
97 void add(ThemeItem<T> &item);
98 /// remove ThemeItem
99 template <typename T>
100 void remove(ThemeItem<T> &item);
101private:
102 const int m_screen_num;
103 typedef std::list<ThemeItem_base *> ItemList;
104 ItemList m_themeitems;
105};
106
107
108/**
109 singleton theme manager
110*/
111class ThemeManager {
112public:
113 static ThemeManager &instance();
114 bool load(const char *filename);
115 std::string resourceValue(const std::string &name, const std::string &altname);
116 void loadTheme(Theme &tm);
117private:
118 ThemeManager();
119 ~ThemeManager() { }
120
121 friend class FbTk::Theme; // so only theme can register itself in constructor
122 /// @return false if screen_num if out of
123 // range or theme already registered, else true
124 bool registerTheme(FbTk::Theme &tm);
125 /// @return false if theme isn't registred in the manager
126 bool unregisterTheme(FbTk::Theme &tm);
127 /// map each theme manager to a screen
128 typedef std::list<FbTk::Theme *> ThemeList;
129 ThemeList m_themelist;
130 const int m_max_screens;
131 XrmDatabaseHelper m_database;
132};
133
134
135
136template <typename T>
137ThemeItem<T>::ThemeItem(FbTk::Theme &tm,
138 const std::string &name, const std::string &altname):
139 ThemeItem_base(name, altname),
140 m_tm(tm) {
141 tm.add(*this);
142}
143
144template <typename T>
145ThemeItem<T>::~ThemeItem() {
146 m_tm.remove(*this);
147}
148
149template <typename T>
150void Theme::add(ThemeItem<T> &item) {
151 m_themeitems.push_back(&item);
152 m_themeitems.unique();
153}
154
155template <typename T>
156void Theme::remove(ThemeItem<T> &item) {
157 m_themeitems.remove(&item);
158}
159
160}; // end namespace FbTk
161
162