From 6e3c7e13d45672632453420096fa981b203f5aea Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 25 Oct 2009 16:58:31 +0000 Subject: [PATCH] Add event handling to the new GUI --- include/gui2/ContainerWidget.hpp | 3 +++ include/gui2/ILayout.hpp | 12 ++++------- include/gui2/Label.hpp | 1 + include/gui2/Widget.hpp | 20 +++++++++++++---- layouts/demo.xml | 11 ++++++++-- src/UIDemo.cpp | 29 +++++++++++++++++++++++-- src/gui2/ContainerWidget.cpp | 20 +++++++++++++++++ src/gui2/Label.cpp | 8 ++++++- src/gui2/Layout.cpp | 37 ++++++++++++++++++++++++++++---- src/gui2/Widget.cpp | 21 ++++++++++++++---- 10 files changed, 137 insertions(+), 25 deletions(-) diff --git a/include/gui2/ContainerWidget.hpp b/include/gui2/ContainerWidget.hpp index 0db91ce..b633600 100644 --- a/include/gui2/ContainerWidget.hpp +++ b/include/gui2/ContainerWidget.hpp @@ -31,9 +31,12 @@ namespace gui { ContainerWidget(const AttributeSet& attrs); virtual void render(RenderContext& rc) const; + virtual void adjust_for_theme(Theme& theme); void add_child(Widget* w); + virtual void handle_click(int x, int y); + protected: virtual void child_added(Widget* w) {}; diff --git a/include/gui2/ILayout.hpp b/include/gui2/ILayout.hpp index cbd4cdc..86efa57 100644 --- a/include/gui2/ILayout.hpp +++ b/include/gui2/ILayout.hpp @@ -19,6 +19,7 @@ #define INC_GUI_ILAYOUT_HPP #include "Platform.hpp" +#include "gui2/Widget.hpp" #include @@ -26,19 +27,14 @@ namespace gui { - // A generic UI element - struct IWidget { - virtual ~IWidget() {} - }; - - typedef shared_ptr IWidgetPtr; - // A complete set of UI elements struct ILayout { virtual ~ILayout() {} - virtual IWidgetPtr get(const string& path) const = 0; + virtual Widget& get(const string& path) const = 0; virtual void render() const = 0; + + virtual void click(int x, int y) = 0; }; typedef shared_ptr ILayoutPtr; diff --git a/include/gui2/Label.hpp b/include/gui2/Label.hpp index 1b77cab..c84567c 100644 --- a/include/gui2/Label.hpp +++ b/include/gui2/Label.hpp @@ -33,6 +33,7 @@ namespace gui { void text(const string& t) { text_ = t; } void render(RenderContext& rc) const; + void adjust_for_theme(Theme& theme); private: string text_; }; diff --git a/include/gui2/Widget.hpp b/include/gui2/Widget.hpp index 1f7501d..210cce8 100644 --- a/include/gui2/Widget.hpp +++ b/include/gui2/Widget.hpp @@ -21,7 +21,6 @@ // Internal header: do not include this file directly #include "Platform.hpp" -#include "gui2/ILayout.hpp" #include "gui2/RenderContext.hpp" #include "IXMLParser.hpp" @@ -30,7 +29,7 @@ namespace gui { - class Widget : public IWidget { + class Widget { public: Widget(const AttributeSet& attrs); @@ -43,16 +42,29 @@ namespace gui { void width(int w) { width_ = w; } void height(int h) { height_ = h; } - boost::any get_property(const string& key) const; - void set_property(const string& key, boost::any value); + enum Signal { + SIG_CLICK + }; + + typedef function SignalHandler; + + void connect(Signal sig, SignalHandler handler); virtual void render(RenderContext& rc) const = 0; + virtual void adjust_for_theme(Theme& theme) {} + + virtual void handle_click(int x, int y); + + protected: + void raise(Signal sig); private: static string unique_name(); string name_; int x_, y_, width_, height_; + + map handlers; static int unique_id; }; diff --git a/layouts/demo.xml b/layouts/demo.xml index c937243..6bfe784 100644 --- a/layouts/demo.xml +++ b/layouts/demo.xml @@ -4,8 +4,15 @@ name="wnd1">