StyleManager
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
// Code by: Jeremy Moles (cubicool) 2007-2008
#ifndef OSGWIDGET_STYLE_MANAGER
#define OSGWIDGET_STYLE_MANAGER
#include <map>
#include <osgDB/Input>
#include <osgWidget/Box>
#include <osgWidget/Frame>
#include <osgWidget/Input>
#include <osgWidget/Canvas>
namespace osgWidget {
typedef osgDB::FieldReaderIterator& Reader;
class OSGWIDGET_EXPORT Style: public osg::Object
{
public:
META_Object(osgWidget, Style);
// Class and contents...
Style (const std::string& = "", const std::string& = "");
Style (const Style&, const osg::CopyOp&);
virtual bool applyStyle (Widget*, Reader);
virtual bool applyStyle (Label*, Reader);
virtual bool applyStyle (Input*, Reader);
virtual bool applyStyle (Window*, Reader);
virtual bool applyStyle (Window::EmbeddedWindow*, Reader);
virtual bool applyStyle (Box*, Reader);
virtual bool applyStyle (Frame::Corner*, Reader);
virtual bool applyStyle (Frame::Border*, Reader);
virtual bool applyStyle (Canvas*, Reader);
void setStyle(const std::string& style) {
_style = style;
}
std::string& getStyle() {
return _style;
}
const std::string& getStyle() const {
return _style;
}
static Widget::Layer strToLayer (const std::string&);
static Widget::VerticalAlignment strToVAlign (const std::string&);
static Widget::HorizontalAlignment strToHAlign (const std::string&);
static Widget::CoordinateMode strToCoordMode (const std::string&);
static bool strToFill (const std::string&);
protected:
std::string _style;
bool _match(const char* seq, Reader r) {
if(r.matchSequence(seq)) {
++r;
return true;
}
return false;
}
};
class OSGWIDGET_EXPORT StyleManager: public osg::Object
{
public:
typedef std::map<std::string, osg::ref_ptr<Style> > Styles;
typedef Styles::iterator Iterator;
typedef Styles::const_iterator ConstIterator;
META_Object(osgWidget, StyleManager);
StyleManager ();
StyleManager (const StyleManager&, const osg::CopyOp&);
bool addStyle(Style*);
bool applyStyles(Widget* widget) {
return _applyStyles(widget);
}
bool applyStyles(Window* window) {
return _applyStyles(window);
}
private:
Styles _styles;
template<typename T>
bool _applySpecificStyle(T* t, const std::string& style)
{
osgDB::FieldReaderIterator r;
std::istringstream input(_styles[style]->getStyle());
r.attach(&input);
bool inc = false;
while(!r.eof())
{
if(_styles[style]->applyStyle(t, r))
inc = true;
else
r.advanceOverCurrentFieldOrBlock();
}
return inc;
}
template<typename T>
bool _coerceAndApply(
osg::Object* obj,
const std::string& style,
const std::string& className
) {
T* t = dynamic_cast<T*>(obj);
if(!t) {
warn()
<< "An attempt was made to coerce Object [" << obj->getName()
<< "] into a " << className << " but failed." << std::endl
;
return 0;
}
return _applySpecificStyle(t, style);
}
bool _applyStyleToObject(osg::Object*, const std::string&);
// 1. Check and see if the explicit FULL path is available.
// 2. Check and see if each component working backward--minus the last--is available.
// 3. Check to see if just the className() is available.
template<typename T>
bool _applyStyles(T* t)
{
osg::Object* obj = dynamic_cast<osg::Object*>(t);
if(!obj)
{
warn()
<< "Cannot call StyleManager::applyStyle with a NULL object or coerce object into osg::Object."
<< std::endl;
return false;
}
std::string c = obj->className();
// Case 3; there's no explicit Style set, so see if there's one for the class.
if(t->getStyle().empty())
{
// Couldn't find the className, so we exit.
if(_styles.find(c) == _styles.end()) return false;
return _applyStyleToObject(obj, c);
}
// Case 1...
if(_styles.find(t->getStyle()) != _styles.end()) return _applyStyleToObject(
obj,
t->getStyle()
);
return false;
}
};
}
#endif
Box
Canvas
Frame
osgDB::FieldReaderIterator
Definition osgDB/Input:185
osgDB::FieldReaderIterator::attach
void attach(std::istream *input)
osgDB::FieldReaderIterator::eof
virtual bool eof() const
osgDB::FieldReaderIterator::advanceOverCurrentFieldOrBlock
void advanceOverCurrentFieldOrBlock()
osgDB::FieldReaderIterator::matchSequence
bool matchSequence(const char *str)
osgWidget::Box
Definition Box:28
osgWidget::Canvas
Definition Canvas:24
osgWidget::Frame::Border
Definition Frame:122
osgWidget::Frame::Corner
Definition Frame:90
osgWidget::Input
Definition osgWidget/Input:29
osgWidget::Label
Definition osgWidget/Label:26
osgWidget::StyleManager::META_Object
META_Object(osgWidget, StyleManager)
osgWidget::StyleManager::StyleManager
StyleManager()
osgWidget::StyleManager::applyStyles
bool applyStyles(Widget *widget)
Definition StyleManager:98
osgWidget::StyleManager::Styles
std::map< std::string, osg::ref_ptr< Style > > Styles
Definition StyleManager:87
osgWidget::StyleManager::ConstIterator
Styles::const_iterator ConstIterator
Definition StyleManager:89
osgWidget::StyleManager::StyleManager
StyleManager(const StyleManager &, const osg::CopyOp &)
osgWidget::StyleManager::applyStyles
bool applyStyles(Window *window)
Definition StyleManager:102
osgWidget::StyleManager::Iterator
Styles::iterator Iterator
Definition StyleManager:88
osgWidget::StyleManager::addStyle
bool addStyle(Style *)
osgWidget::Style
Definition StyleManager:31
osgWidget::Style::strToFill
static bool strToFill(const std::string &)
osgWidget::Style::applyStyle
virtual bool applyStyle(Input *, Reader)
osgWidget::Style::applyStyle
virtual bool applyStyle(Canvas *, Reader)
osgWidget::Style::strToLayer
static Widget::Layer strToLayer(const std::string &)
osgWidget::Style::applyStyle
virtual bool applyStyle(Label *, Reader)
osgWidget::Style::applyStyle
virtual bool applyStyle(Widget *, Reader)
osgWidget::Style::applyStyle
virtual bool applyStyle(Frame::Corner *, Reader)
osgWidget::Style::applyStyle
virtual bool applyStyle(Window *, Reader)
osgWidget::Style::META_Object
META_Object(osgWidget, Style)
osgWidget::Style::Style
Style(const Style &, const osg::CopyOp &)
osgWidget::Style::getStyle
const std::string & getStyle() const
Definition StyleManager:59
osgWidget::Style::getStyle
std::string & getStyle()
Definition StyleManager:55
osgWidget::Style::applyStyle
virtual bool applyStyle(Window::EmbeddedWindow *, Reader)
osgWidget::Style::applyStyle
virtual bool applyStyle(Frame::Border *, Reader)
osgWidget::Style::_match
bool _match(const char *seq, Reader r)
Definition StyleManager:73
osgWidget::Style::strToHAlign
static Widget::HorizontalAlignment strToHAlign(const std::string &)
osgWidget::Style::Style
Style(const std::string &="", const std::string &="")
osgWidget::Style::strToVAlign
static Widget::VerticalAlignment strToVAlign(const std::string &)
osgWidget::Style::setStyle
void setStyle(const std::string &style)
Definition StyleManager:51
osgWidget::Style::_style
std::string _style
Definition StyleManager:71
osgWidget::Style::applyStyle
virtual bool applyStyle(Box *, Reader)
osgWidget::Style::strToCoordMode
static Widget::CoordinateMode strToCoordMode(const std::string &)
osgWidget::Widget
Definition osgWidget/Widget:35
osgWidget::Widget::HorizontalAlignment
HorizontalAlignment
Definition osgWidget/Widget:63
osgWidget::Widget::Layer
Layer
Definition osgWidget/Widget:49
osgWidget::Widget::CoordinateMode
CoordinateMode
Definition osgWidget/Widget:69
osgWidget::Widget::VerticalAlignment
VerticalAlignment
Definition osgWidget/Widget:57
osgWidget::Window::EmbeddedWindow
Definition Window:60
osgWidget::Window
Definition Window:44
osg::CopyOp
Definition CopyOp:41
osg::Object
Definition Object:61
osg::Object::className
virtual const char * className() const =0
osg::Object::getName
const std::string & getName() const
Definition Object:213
osgWidget
Definition Box:21
osgWidget::warn
std::ostream & warn()
Definition Util:41
osgWidget::Reader
osgDB::FieldReaderIterator & Reader
Definition StyleManager:28
Input
OSGWIDGET_EXPORT
#define OSGWIDGET_EXPORT
Definition osgWidget/Export:42
Input