aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Theme.cc
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/Theme.cc
parent3d18945fb5b660cef4ff32668c15f2b565f4e230 (diff)
downloadfluxbox_pavel-c8aff5576a187f3921a0db998cd5a47173b28742.zip
fluxbox_pavel-c8aff5576a187f3921a0db998cd5a47173b28742.tar.bz2
theme classes
Diffstat (limited to 'src/FbTk/Theme.cc')
-rw-r--r--src/FbTk/Theme.cc187
1 files changed, 187 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