// Theme.cc for FbTk - Fluxbox ToolKit // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) // // 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.18 2003/09/10 21:22:25 fluxgen Exp $ #include "Theme.hh" #include "XrmDatabaseHelper.hh" #include "Font.hh" #include "Color.hh" #include "Texture.hh" #include "App.hh" #include "Image.hh" #include "PixmapWithMask.hh" #include "StringUtil.hh" #include #include #include using namespace std; namespace FbTk { // create default handlers for Color, Font, Texture, int and string template <> void FbTk::ThemeItem::load() { } template <> void FbTk::ThemeItem::setDefaultValue() { *(*this) = ""; } template <> void FbTk::ThemeItem::setFromString(const char *str) { *(*this) = (str ? str : ""); } template <> void FbTk::ThemeItem::load() { } template <> void FbTk::ThemeItem::setDefaultValue() { *(*this) = 0; } template <> void FbTk::ThemeItem::setFromString(const char *str) { if (str == 0) return; sscanf(str, "%d", &m_value); } 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) { if (FbTk::ThemeManager::instance().verbose()) { cerr<<"FbTk::Theme: Error loading font "<< ((m_value.isAntialias() || m_value.utf8()) ? "(" : "")<< (m_value.isAntialias() ? "antialias" : "")<< (m_value.utf8() ? " utf8" : "")<< ((m_value.isAntialias() || m_value.utf8()) ? ") " : "")<< "for \""< void ThemeItem::load() { } template <> void ThemeItem::load() { string color_name(ThemeManager::instance(). resourceValue(name()+".color", altName()+".Color")); string colorto_name(ThemeManager::instance(). resourceValue(name()+".colorTo", altName()+".ColorTo")); string pixmap_name(ThemeManager::instance(). resourceValue(name()+".pixmap", altName()+".Pixmap")); // set default value if we failed to load color if (!m_value.color().setFromString(color_name.c_str(), m_tm.screenNum())) m_value.color().setFromString("darkgray", m_tm.screenNum()); if (!m_value.colorTo().setFromString(colorto_name.c_str(), m_tm.screenNum())) m_value.colorTo().setFromString("white", m_tm.screenNum()); std::auto_ptr pm(Image::load(pixmap_name, m_tm.screenNum())); if (pm.get() == 0) { if (FbTk::ThemeManager::instance().verbose()) cerr<<"Resource("<pixmap().release(); } template <> void ThemeItem::setDefaultValue() { m_value.setType(FbTk::Texture::FLAT | FbTk::Texture::SOLID); load(); // one might forget to add line something: so we try to load something.*: too } template <> void ThemeItem::setFromString(const char *str) { m_value.setFromString(str); if (m_value.type() == 0) // failed to set value setDefaultValue(); } // not used template <> void FbTk::ThemeItem:: load() { } template <> void FbTk::ThemeItem:: setDefaultValue() { // create empty pixmap (*this)->pixmap() = 0; (*this)->mask() = 0; } template <> void FbTk::ThemeItem:: setFromString(const char *str) { if (str == 0) setDefaultValue(); else { std::auto_ptr pm(Image::load(str, m_tm.screenNum())); if (pm.get() == 0) setDefaultValue(); else { (*this)->pixmap() = pm->pixmap().release(); (*this)->mask() = pm->mask().release(); } } } template <> void ThemeItem::setDefaultValue() { m_value.setFromString("white", m_tm.screenNum()); } template <> void ThemeItem::setFromString(const char *str) { if (!m_value.setFromString(str, m_tm.screenNum())) { if (FbTk::ThemeManager::instance().verbose()) cerr<<"FbTk::Theme: Error loading color value for \""< void ThemeItem::load() { } Theme::Theme(int screen_num):m_screen_num(screen_num) { ThemeManager::instance().registerTheme(*this); } Theme::~Theme() { ThemeManager::instance().unregisterTheme(*this); } ThemeManager &ThemeManager::instance() { static ThemeManager tm; return tm; } ThemeManager::ThemeManager(): m_max_screens(ScreenCount(FbTk::App::instance()->display())), m_verbose(false) { } 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 std::string &filename) { if (!m_database.load(FbTk::StringUtil::expandFilename(filename).c_str())) 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); } // notify all themes that we reconfigured theme_it = m_themelist.begin(); for (; theme_it != theme_it_end; ++theme_it) { // send reconfiguration signal to theme and listeners (*theme_it)->reconfigTheme(); (*theme_it)->reconfigSig().notify(); } return true; } void ThemeManager::loadTheme(Theme &tm) { 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 (!loadItem(*resource)) { // try fallback resource in theme if (!tm.fallback(*resource)) { if (verbose()) cerr<<"Failed to read theme item: "<name()<setDefaultValue(); } } } // send reconfiguration signal to theme and listeners } bool ThemeManager::loadItem(ThemeItem_base &resource) { return loadItem(resource, resource.name(), resource.altName()); } /// handles resource item loading with specific name/altname bool ThemeManager::loadItem(ThemeItem_base &resource, const std::string &name, const std::string &alt_name) { XrmValue value; char *value_type; if (XrmGetResource(*m_database, name.c_str(), alt_name.c_str(), &value_type, &value)) { resource.setFromString(value.addr); resource.load(); // load additional stuff by the ThemeItem } else return false; return true; } 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 ""; } /* void ThemeManager::listItems() { ThemeList::iterator it = m_themelist.begin(); ThemeList::iterator it_end = m_themelist.end(); for (; it != it_end; ++it) { std::list::iterator item = (*it)->itemList().begin(); std::list::iterator item_end = (*it)->itemList().end(); for (; item != item_end; ++item) { cerr<<(*item)->name()<<":"<)) { cerr<<(*item)->name()<<".pixmap:"<name()<<".color:"<name()<<".colorTo:"<