Commit 354811a1 authored by JP Dinger's avatar JP Dinger

Skins2: Replace some theme macros by templated code.

parent a7b6b23d
......@@ -17,9 +17,9 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "theme.hpp"
......@@ -159,77 +159,41 @@ void Theme::saveConfig()
}
// Useful macro
#define FIND_OBJECT( mapData, mapName ) \
map<string, mapData>::const_iterator it; \
it = mapName.find( id ); \
if( it == mapName.end() ) \
{ \
return NULL; \
} \
return (*it).second.get();
// This macro takes an ID of the form "id1;id2;id3", and returns the object
// Takes an ID of the form "id1;id2;id3", and returns the object
// corresponding to the first valid ID. If no ID is valid, it returns NULL.
// XXX: should we use a template method instead?
#define FIND_FIRST_OBJECT( mapDataPtr, mapName ) \
string rightPart = id; \
string::size_type pos; \
do \
{ \
pos = rightPart.find( ";" ); \
string leftPart = rightPart.substr( 0, pos ); \
map<string, mapDataPtr>::const_iterator it = mapName.find( leftPart ); \
if( it != mapName.end() ) \
{ \
return (*it).second.get(); \
break; \
} \
\
if( pos != string::npos ) \
{ \
rightPart = rightPart.substr( pos, rightPart.size() ); \
rightPart = \
rightPart.substr( rightPart.find_first_not_of( " \t;" ), \
rightPart.size() ); \
} \
} \
while( pos != string::npos ); \
return NULL;
GenericBitmap *Theme::getBitmapById( const string &id ) const
{
FIND_FIRST_OBJECT( GenericBitmapPtr, m_bitmaps );
}
GenericFont *Theme::getFontById( const string &id ) const
{
FIND_FIRST_OBJECT( GenericFontPtr, m_fonts );
}
Popup *Theme::getPopupById( const string &id ) const
// XXX The string handling here probably could be improved.
template<class T> typename T::pointer
Theme::IDmap<T>::find_first_object( const string &id ) const
{
FIND_OBJECT( PopupPtr, m_popups );
}
string rightPart = id;
string::size_type pos;
do
{
pos = rightPart.find( ";" );
string leftPart = rightPart.substr( 0, pos );
TopWindow *Theme::getWindowById( const string &id ) const
{
FIND_OBJECT( TopWindowPtr, m_windows );
}
typename T::pointer p = find_object( leftPart );
if( p ) return p;
GenericLayout *Theme::getLayoutById( const string &id ) const
{
FIND_OBJECT( GenericLayoutPtr, m_layouts );
if( pos != string::npos )
{
rightPart = rightPart.substr( pos, rightPart.size() );
rightPart =
rightPart.substr( rightPart.find_first_not_of( " \t;" ),
rightPart.size() );
}
}
while( pos != string::npos );
return NULL;
}
CtrlGeneric *Theme::getControlById( const string &id ) const
GenericBitmap *Theme::getBitmapById( const string &id ) const
{
FIND_OBJECT( CtrlGenericPtr, m_controls );
m_bitmaps.find_first_object( id );
}
Position *Theme::getPositionById( const string &id ) const
GenericFont *Theme::getFontById( const string &id ) const
{
FIND_OBJECT( PositionPtr, m_positions );
m_fonts.find_first_object( id );
}
......@@ -58,29 +58,44 @@ public:
GenericBitmap *getBitmapById( const string &id ) const;
GenericFont *getFontById( const string &id ) const;
Popup *getPopupById( const string &id ) const;
TopWindow *getWindowById( const string &id ) const;
GenericLayout *getLayoutById( const string &id ) const;
CtrlGeneric *getControlById( const string &id ) const;
Position *getPositionById( const string &id ) const;
# define ObjByID( var ) ( const string &id ) const \
{ return var.find_object( id ); }
Popup *getPopupById ObjByID( m_popups )
TopWindow *getWindowById ObjByID( m_windows )
GenericLayout *getLayoutById ObjByID( m_layouts )
CtrlGeneric *getControlById ObjByID( m_controls )
Position *getPositionById ObjByID( m_positions )
# undef ObjById
WindowManager &getWindowManager() { return m_windowManager; }
private:
template<class T> class IDmap: public std::map<string, T> {
private:
typedef typename std::map<string, T> parent;
public:
typename T::pointer find_object(const string &id) const
{
typename parent::const_iterator it = parent::find( id );
return it!=parent::end() ? it->second.get() : NULL;
}
typename T::pointer find_first_object(const string &id) const;
};
/// Store the bitmaps by ID
map<string, GenericBitmapPtr> m_bitmaps;
IDmap<GenericBitmapPtr> m_bitmaps;
/// Store the fonts by ID
map<string, GenericFontPtr> m_fonts;
IDmap<GenericFontPtr> m_fonts;
/// Store the popups by ID
map<string, PopupPtr> m_popups;
IDmap<PopupPtr> m_popups;
/// Store the windows by ID
map<string, TopWindowPtr> m_windows;
IDmap<TopWindowPtr> m_windows;
/// Store the layouts by ID
map<string, GenericLayoutPtr> m_layouts;
IDmap<GenericLayoutPtr> m_layouts;
/// Store the controls by ID
map<string, CtrlGenericPtr> m_controls;
IDmap<CtrlGenericPtr> m_controls;
/// Store the panel positions by ID
map<string, PositionPtr> m_positions;
IDmap<PositionPtr> m_positions;
/// Store the commands
list<CmdGenericPtr> m_commands;
/// Store the Bezier curves
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment