From c8aff5576a187f3921a0db998cd5a47173b28742 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Mon, 2 Dec 2002 19:34:54 +0000 Subject: theme classes --- src/FbTk/Theme.cc | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/FbTk/Theme.hh | 162 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 349 insertions(+) create mode 100644 src/FbTk/Theme.cc create mode 100644 src/FbTk/Theme.hh 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 @@ +// Theme.cc for FbTk - Fluxbox ToolKit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Theme.cc,v 1.1 2002/12/02 19:34:54 fluxgen Exp $ + +#include "Theme.hh" + +#include "../XrmDatabaseHelper.hh" +#include "Font.hh" +#include "Color.hh" +#include "Texture.hh" +#include "App.hh" + +#include + +using namespace std; +namespace FbTk { + +// create default handlers for Color, Font, and Texture + +template <> +void ThemeItem::setDefaultValue() { + if (!m_value.load("fixed")) { + cerr<<"FbTk::ThemeItem: Warning! Failed to load default value 'fixed'"< +void ThemeItem::setFromString(const char *str) { + if (m_value.load(str) == false) { + cerr<<"Failed to load font: "< +void ThemeItem::load() { +} + + +template <> +void ThemeItem::setFromString(const char *str) { + m_value.setFromString(str); +} + +template <> +void ThemeItem::setDefaultValue() { + m_value.setType(0); +} + +template <> +void ThemeItem::load() { + string color_name(ThemeManager::instance().resourceValue(name()+".color", altName()+".Color")); + string colorto_name(ThemeManager::instance().resourceValue(name()+".colorTo", altName()+".ColorTo")); + m_value.color().setFromString(color_name.c_str(), m_tm.screenNum()); + m_value.colorTo().setFromString(colorto_name.c_str(), m_tm.screenNum()); +} + +template <> +void ThemeItem::setFromString(const char *str) { + m_value.setFromString(str, m_tm.screenNum()); +} + +template <> +void ThemeItem::setDefaultValue() { + m_value.setPixel(0xFFFFFFFF); +} + +// does nothing +template <> +void ThemeItem::load() { } + +Theme::Theme(int screen_num):m_screen_num(screen_num) { + + if (!ThemeManager::instance().registerTheme(*this)) { + // should it be fatal or not? + cerr<<"FbTk::Theme Warning: Failed to register Theme"<display())) { + +} + +bool ThemeManager::registerTheme(Theme &tm) { + // valid screen num? + if (m_max_screens < tm.screenNum() || tm.screenNum() < 0) + return false; + // TODO: use find and return false if it's already there + // instead of unique + m_themelist.push_back(&tm); + m_themelist.unique(); + return true; +} + +bool ThemeManager::unregisterTheme(Theme &tm) { + m_themelist.remove(&tm); + return true; +} + +bool ThemeManager::load(const char *filename) { + + if (!m_database.load(filename)) + return false; + + //get list and go throu all the resources and load them + ThemeList::iterator theme_it = m_themelist.begin(); + const ThemeList::iterator theme_it_end = m_themelist.end(); + for (; theme_it != theme_it_end; ++theme_it) { + loadTheme(**theme_it); + } + + return true; +} + +void ThemeManager::loadTheme(Theme &tm) { + + XrmValue value; + char *value_type; + + std::list::iterator i = tm.itemList().begin(); + std::list::iterator i_end = tm.itemList().end(); + for (; i != i_end; ++i) { + ThemeItem_base *resource = *i; + if (XrmGetResource(*m_database, resource->name().c_str(), + resource->altName().c_str(), &value_type, &value)) { + resource->setFromString(value.addr); + resource->load(); // load additional stuff by the ThemeItem + } else { + cerr<<"Failed to read theme item: "<name()<setDefaultValue(); + } + } + // send reconfiguration signal to theme + tm.reconfigTheme(); + +} + +std::string ThemeManager::resourceValue(const std::string &name, const std::string &altname) { + XrmValue value; + char *value_type; + + if (*m_database != 0 && XrmGetResource(*m_database, name.c_str(), + altname.c_str(), &value_type, &value) && value.addr != 0) { + return string(value.addr); + } + return ""; +} + + +}; // 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 @@ +// Theme.hh for FbTk - Fluxbox ToolKit +// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Theme.hh,v 1.1 2002/12/02 19:34:51 fluxgen Exp $ + +// holds ThemeItem, Theme and ThemeManager + + +#include +#include +#include + +#include "../XrmDatabaseHelper.hh" + +namespace FbTk { + +class Theme; +/** + base class for themeitem, holds name and altname + */ +class ThemeItem_base { +public: + ThemeItem_base(const std::string &name, const std::string &altname): + m_name(name), m_altname(altname) { } + virtual ~ThemeItem_base() { } + virtual void setFromString(const char *str) = 0; + virtual void setDefaultValue() = 0; + virtual void load() = 0; // if it needs to load additional stuff + const std::string &name() const { return m_name; } + const std::string &altName() const { return m_altname; } +private: + std::string m_name, m_altname; +}; + +/** + template ThemeItem class for things like Texture and Color +*/ +template +class ThemeItem:public ThemeItem_base { +public: + ThemeItem(FbTk::Theme &tm, const std::string &name, const std::string &altname); + virtual ~ThemeItem(); + /// specialized + void setDefaultValue(); + /// specialized + void setFromString(const char *strval); + /// specialized + void load(); + /** + @name access operators + */ + /**@{*/ + inline T& operator*() { return m_value; } + inline const T& operator*() const { return m_value; } + inline T *operator->() { return &m_value; } + inline const T *operator->() const { return &m_value; } + /**@}*/ +private: + + T m_value; + FbTk::Theme &m_tm; +}; + + + +/** + Hold themeitems. Use this to create a Theme set +*/ +class Theme { +public: + explicit Theme(int screen_num); // create a theme for a specific screen + virtual ~Theme(); + virtual void reconfigTheme() = 0; + int screenNum() const { return m_screen_num; } + std::list &itemList() { return m_themeitems; } + const std::list &itemList() const { return m_themeitems; } + /// add ThemeItem + template + void add(ThemeItem &item); + /// remove ThemeItem + template + void remove(ThemeItem &item); +private: + const int m_screen_num; + typedef std::list ItemList; + ItemList m_themeitems; +}; + + +/** + singleton theme manager +*/ +class ThemeManager { +public: + static ThemeManager &instance(); + bool load(const char *filename); + std::string resourceValue(const std::string &name, const std::string &altname); + void loadTheme(Theme &tm); +private: + ThemeManager(); + ~ThemeManager() { } + + friend class FbTk::Theme; // so only theme can register itself in constructor + /// @return false if screen_num if out of + // range or theme already registered, else true + bool registerTheme(FbTk::Theme &tm); + /// @return false if theme isn't registred in the manager + bool unregisterTheme(FbTk::Theme &tm); + /// map each theme manager to a screen + typedef std::list ThemeList; + ThemeList m_themelist; + const int m_max_screens; + XrmDatabaseHelper m_database; +}; + + + +template +ThemeItem::ThemeItem(FbTk::Theme &tm, + const std::string &name, const std::string &altname): + ThemeItem_base(name, altname), + m_tm(tm) { + tm.add(*this); +} + +template +ThemeItem::~ThemeItem() { + m_tm.remove(*this); +} + +template +void Theme::add(ThemeItem &item) { + m_themeitems.push_back(&item); + m_themeitems.unique(); +} + +template +void Theme::remove(ThemeItem &item) { + m_themeitems.remove(&item); +} + +}; // end namespace FbTk + + -- cgit v0.11.2