2008-07-26 04:04:41 +08:00
|
|
|
/* -*-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.
|
|
|
|
*/
|
|
|
|
|
2008-11-28 22:35:33 +08:00
|
|
|
// Code by: Jeremy Moles (cubicool) 2007-2008
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
#ifndef OSGWIDGET_STYLE_MANAGER
|
|
|
|
#define OSGWIDGET_STYLE_MANAGER
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <osgDB/FieldReaderIterator>
|
|
|
|
#include <osgWidget/Box>
|
|
|
|
#include <osgWidget/Frame>
|
|
|
|
#include <osgWidget/Input>
|
2008-08-18 19:21:41 +08:00
|
|
|
#include <osgWidget/Canvas>
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
namespace osgWidget {
|
|
|
|
|
|
|
|
typedef osgDB::FieldReaderIterator& Reader;
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
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);
|
2008-08-18 19:21:41 +08:00
|
|
|
virtual bool applyStyle (Canvas*, Reader);
|
|
|
|
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
|
|
|
|
void setStyle(const std::string& style) {
|
|
|
|
_style = style;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string& getStyle() {
|
|
|
|
return _style;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getStyle() const {
|
|
|
|
return _style;
|
|
|
|
}
|
|
|
|
|
2008-07-26 04:50:42 +08:00
|
|
|
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&);
|
2008-07-26 03:57:17 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
};
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
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;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
META_Object(osgWidget, StyleManager);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
StyleManager ();
|
|
|
|
StyleManager (const StyleManager&, const osg::CopyOp&);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool addStyle(Style*);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool applyStyles(Widget* widget) {
|
|
|
|
return _applyStyles(widget);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool applyStyles(Window* window) {
|
|
|
|
return _applyStyles(window);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
private:
|
|
|
|
Styles _styles;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
template<typename T>
|
2008-08-18 19:21:41 +08:00
|
|
|
bool _applySpecificStyle(T* t, const std::string& style)
|
|
|
|
{
|
2008-07-26 03:57:17 +08:00
|
|
|
osgDB::FieldReaderIterator r;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
std::istringstream input(_styles[style]->getStyle());
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
r.attach(&input);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool inc = false;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-08-18 19:21:41 +08:00
|
|
|
while(!r.eof())
|
|
|
|
{
|
|
|
|
if(_styles[style]->applyStyle(t, r))
|
|
|
|
inc = true;
|
|
|
|
else
|
|
|
|
r.advanceOverCurrentFieldOrBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return inc;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
template<typename T>
|
|
|
|
bool _coerceAndApply(
|
|
|
|
osg::Object* obj,
|
|
|
|
const std::string& style,
|
|
|
|
const std::string& className
|
|
|
|
) {
|
|
|
|
T* t = dynamic_cast<T*>(obj);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
if(!t) {
|
|
|
|
warn()
|
|
|
|
<< "An attempt was made to coerce Object [" << obj->getName()
|
|
|
|
<< "] into a " << className << " but failed." << std::endl
|
|
|
|
;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return _applySpecificStyle(t, style);
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
bool _applyStyleToObject(osg::Object*, const std::string&);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
if(!t)
|
|
|
|
{
|
|
|
|
warn()
|
|
|
|
<< "Cannot call StyleManager::applyStyle with a NULL object."
|
|
|
|
<< std::endl
|
|
|
|
;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
osg::Object* obj = dynamic_cast<osg::Object*>(t);
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
if(!obj)
|
|
|
|
{
|
|
|
|
warn()
|
|
|
|
<< "Cannot coerce object into osg::Object in StyleManager:::applyStyle"
|
|
|
|
<< std::endl
|
|
|
|
;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
std::string c = obj->className();
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
// 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;
|
2008-07-16 01:21:25 +08:00
|
|
|
|
2008-07-26 03:57:17 +08:00
|
|
|
return _applyStyleToObject(obj, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 1...
|
|
|
|
if(_styles.find(t->getStyle()) != _styles.end()) return _applyStyleToObject(
|
|
|
|
obj,
|
|
|
|
t->getStyle()
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-16 01:21:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|