WidgetCreator.h

00001 #ifndef XFLTK__WIDGETCREATOR_H_
00002 #define XFLTK__WIDGETCREATOR_H_
00003 
00004 #include <map>
00005 #include <fltk/widget.h>
00006 
00007 #include "utils.h"
00008 
00009 #include "GUI.h"
00010 
00011 namespace xfltk {
00012 
00016 class WidgetCreator {
00017         
00018         GUI& _gui;
00019         
00020         public:
00021         
00022         WidgetCreator(GUI& gui): _gui(gui) {};
00023         
00024         GUI& gui() { return _gui; }
00025         
00034         virtual fltk::Widget* create_begin(const char** attr) = 0;
00035         
00036         //virtual void before_child_create();
00037         
00038         //virtual void after_child_create();
00039         
00044         virtual void create_end() = 0;
00045         
00046         virtual ~WidgetCreator() {};
00047         
00048 };
00049 
00054 class WidgetFactory {
00055         
00056         typedef std::map<const char*, WidgetFactory*, utils::ltstr> widget_factories_map;
00057         
00061         static widget_factories_map widget_factories;
00062         
00063         protected:
00064         
00069         WidgetFactory(const char* name);
00070         
00071         public:
00072         
00078         static WidgetFactory* find(const char* name);
00079         
00083         static WidgetCreator* getCreator(const char* name, GUI& gui);
00084         
00092         virtual WidgetCreator* getCreator(GUI& gui) = 0;
00093         
00097         virtual ~WidgetFactory() {};
00098         
00099 };
00100 
00104 typedef void property_setter(fltk::Widget* obj, const char* value, WidgetCreator& creator);
00105 
00109 class HierarhyWidgetFactory: public WidgetFactory {
00110 
00111 protected:
00112 
00113         typedef std::map<const char*, property_setter*, utils::ltstr> widget_property_setters_map;
00114 
00115 private:
00116 
00120         HierarhyWidgetFactory* parent;
00121 
00125         widget_property_setters_map widget_property_setters;
00126 
00127 protected:
00128 
00134         HierarhyWidgetFactory(const char* name, HierarhyWidgetFactory* parent);
00135         
00139         widget_property_setters_map& getWidgetPropertySetters();
00140         
00141 public:
00142                         
00143         /*
00144          * Fill property setter map.
00145          */
00146         //virtual void fill_property_setters() { };
00147         
00148         void setProperty(fltk::Widget* obj, const char* name, const char* value, WidgetCreator& creator);
00149         
00150         void setProperties(fltk::Widget* obj, const char** attr, WidgetCreator& creator);
00151         
00152         void addPropertySetter(const char* name, property_setter setter);
00153         
00154 };
00155 
00159 class HierarhyWidgetCreatorBase: public WidgetCreator {
00160         
00161         protected:
00162         
00163         HierarhyWidgetFactory& factory;
00164         
00165         public:
00166         
00167         virtual fltk::Widget* new_widget() = 0;
00168         
00169         HierarhyWidgetCreatorBase(GUI& gui, HierarhyWidgetFactory& factory): WidgetCreator(gui), factory(factory) {}
00170         
00171         virtual fltk::Widget* create_begin(const char** attr) {
00172                 fltk::Widget* result = new_widget();
00173                 factory.setProperties(result, attr, *this);
00174                 return result;
00175         }
00176         
00177         virtual void create_end() {};   
00178         
00179 };
00180 
00181 template <typename fltkWidgetType>
00182 struct HierarhyWidgetCreator0: public HierarhyWidgetCreatorBase {
00183         
00184         HierarhyWidgetCreator0<fltkWidgetType>(GUI& gui, HierarhyWidgetFactory& factory): HierarhyWidgetCreatorBase(gui, factory) {}
00185         
00186         virtual fltk::Widget* new_widget() {
00187                 return new fltkWidgetType();
00188         }
00189         
00190 };
00191 
00192 template <typename fltkWidgetType>
00193 struct HierarhyWidgetCreator: public HierarhyWidgetCreatorBase {
00194         
00195         HierarhyWidgetCreator<fltkWidgetType>(GUI& gui, HierarhyWidgetFactory& factory): HierarhyWidgetCreatorBase(gui, factory) {}
00196         
00197         virtual fltk::Widget* new_widget() {
00198                 return new fltkWidgetType(0, 0, 0, 0);
00199         }
00200         
00201 };
00202 
00203 } //namespace xfltk
00204 
00205 #endif /*XFLTK__WIDGETCREATOR_H_*/

Generated on Tue Jan 3 14:32:37 2006 for xfltk by  doxygen 1.4.6-NO