From e0945069d8bfd75674f731cd5f8091ff54f1596a Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 3 Nov 2009 21:40:36 +0000 Subject: [PATCH] Specify GUI fonts in the XML file --- include/IXMLParser.hpp | 19 +++++++++++--- include/gui2/ContainerWidget.hpp | 2 +- include/gui2/Label.hpp | 2 +- include/gui2/RenderContext.hpp | 6 ++--- include/gui2/Theme.hpp | 10 ++++++-- include/gui2/Widget.hpp | 2 +- layouts/game.xml | 5 ++++ schemas/layout.xsd | 17 +++++++++++-- src/gui2/Button.cpp | 2 +- src/gui2/ContainerWidget.cpp | 2 +- src/gui2/Label.cpp | 2 +- src/gui2/Layout.cpp | 43 +++++++++++++++++++++----------- src/gui2/RenderContext.cpp | 4 +-- src/gui2/Theme.cpp | 22 +++++++++------- 14 files changed, 95 insertions(+), 43 deletions(-) diff --git a/include/IXMLParser.hpp b/include/IXMLParser.hpp index 54c74ea..d463b25 100644 --- a/include/IXMLParser.hpp +++ b/include/IXMLParser.hpp @@ -22,10 +22,10 @@ #include #include +#include #include #include -#include // Container for attributes class AttributeSet { @@ -52,15 +52,26 @@ public: xercesc::XMLString::release(&xmlName); if (index != -1) { - char* ascii = xercesc::XMLString::transcode(myAttrs.getValue(index)); - T result = boost::lexical_cast(ascii); + char* ascii = xercesc::XMLString::transcode( + myAttrs.getValue(index)); + + istringstream ss(ascii); + T result; + + ss >> boolalpha >> result; // Is boolalpha affected by locale? + + if (ss.fail()) + throw runtime_error( + "Cannot parse attribute '" + aName + + "' with value '" + ascii + "'"); + xercesc::XMLString::release(&ascii); return result; } else throw std::runtime_error("No attribute: " + aName); } - + template void get(const std::string& aName, T& aT) const { diff --git a/include/gui2/ContainerWidget.hpp b/include/gui2/ContainerWidget.hpp index c0b793c..9f6696e 100644 --- a/include/gui2/ContainerWidget.hpp +++ b/include/gui2/ContainerWidget.hpp @@ -31,7 +31,7 @@ namespace gui { ContainerWidget(const AttributeSet& attrs); virtual void render(RenderContext& rc) const; - virtual void adjustForTheme(Theme& theme); + virtual void adjustForTheme(const Theme& theme); void addChild(Widget* w); diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp index 0a8f552..2bf56dd 100644 --- a/include/gui2/Label.hpp +++ b/include/gui2/Label.hpp @@ -35,7 +35,7 @@ namespace gui { void format(const char* fmt, ...); void render(RenderContext& rc) const; - void adjustForTheme(Theme& theme); + void adjustForTheme(const Theme& theme); private: string text_, fontName; }; diff --git a/include/gui2/RenderContext.hpp b/include/gui2/RenderContext.hpp index cd80dee..d70cf50 100644 --- a/include/gui2/RenderContext.hpp +++ b/include/gui2/RenderContext.hpp @@ -33,7 +33,7 @@ namespace gui { class RenderContext { public: - RenderContext(); + RenderContext(const Theme& theme); ~RenderContext(); void push_origin(const Widget* w); @@ -49,8 +49,8 @@ namespace gui { const Theme& theme() const { return theme_; } private: void offset(int& x, int& y) const; - - Theme theme_; + + const Theme& theme_; int origin_x, origin_y; stack origin_stack; diff --git a/include/gui2/Theme.hpp b/include/gui2/Theme.hpp index 3bee32a..34eb6a0 100644 --- a/include/gui2/Theme.hpp +++ b/include/gui2/Theme.hpp @@ -24,6 +24,8 @@ #include "Colour.hpp" #include "ft/IFont.hpp" +#include + namespace gui { using namespace ft; @@ -37,12 +39,16 @@ namespace gui { Colour border() const; // Fonts - IFontPtr normal_font() const { return normal_font_; } + IFontPtr normalFont() const { return normal_font_; } IFontPtr font(const string& fontName) const; + + void addFont(const string& name, IFontPtr f); private: IFontPtr normal_font_; - IFontPtr dropShadowFont; + + typedef map FontMap; + FontMap fonts; }; } diff --git a/include/gui2/Widget.hpp b/include/gui2/Widget.hpp index 9b5507c..7ba3ec5 100644 --- a/include/gui2/Widget.hpp +++ b/include/gui2/Widget.hpp @@ -51,7 +51,7 @@ namespace gui { void connect(Signal sig, SignalHandler handler); virtual void render(RenderContext& rc) const = 0; - virtual void adjustForTheme(Theme& theme) {} + virtual void adjustForTheme(const Theme& theme) {} virtual void handleClick(int x, int y); diff --git a/layouts/game.xml b/layouts/game.xml index c71d8c2..8f60eac 100644 --- a/layouts/game.xml +++ b/layouts/game.xml @@ -1,5 +1,10 @@ + +