]> git.tdb.fi Git - libs/gltk.git/commitdiff
Use a LoadableTypeRegistry to manage child types for Panel
authorMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 21:03:02 +0000 (00:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 21:03:02 +0000 (00:03 +0300)
source/panel.cpp
source/panel.h

index 97e13a2591b34dd9107f2dd65c22baa86750f676..11c19f3a99bb51415f2090a21c91ab1f8468c59d 100644 (file)
@@ -24,6 +24,9 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
+DataFile::LoadableTypeRegistry<Panel::Loader, Panel::Loader::AddChildType> Panel::widget_registry;
+bool Panel::widget_registry_init_done = false;
+
 Panel::Panel():
        layout(0)
 {
@@ -210,34 +213,33 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        wdg_map(m),
        last_widget(0)
 {
-       add_child_type<Button>("button");
+       if(!widget_registry_init_done)
+       {
+               widget_registry_init_done = true;
+               register_child_type<Button>("button");
+               register_child_type<DragHandle>("draghandle");
+               register_child_type<Dropdown>("dropdown");
+               register_child_type<Entry>("entry");
+               register_child_type<HSlider>("hslider");
+               register_child_type<Image>("image");
+               register_child_type<Indicator>("indicator");
+               register_child_type<Label>("label");
+               register_child_type<List>("list");
+               register_child_type<Panel>("panel");
+               register_child_type<Toggle>("toggle");
+               register_child_type<VSlider>("vslider");
+       }
+
        add("column",    &Loader::arrangement<Column>);
        add("constraint",&Loader::constraint);
-       add_child_type<DragHandle>("draghandle");
-       add_child_type<Dropdown>("dropdown");
-       add_child_type<Entry>("entry");
        add("expand",    &Loader::expand);
        add("ghost",     &Loader::ghost);
        add("gravity",   &Loader::gravity);
        add("grid",      &Loader::grid);
-       add_child_type<HSlider>("hslider");
-       add_child_type<Image>("image");
-       add_child_type<Indicator>("indicator");
-       add_child_type<Label>("label");
        add("layout",    &Loader::layout);
-       add_child_type<List>("list");
-       add_child_type<Panel>("panel");
        add("row",       &Loader::arrangement<Row>);
        add("stack",     &Loader::arrangement<Stack>);
-       add_child_type<Toggle>("toggle");
-       add_child_type<VSlider>("vslider");
-}
-
-template<typename T>
-void Panel::Loader::add_child_type(const string &keyword)
-{
-       add(keyword, &Loader::child<T>);
-       add(keyword, &Loader::unnamed_child<T>);
+       widget_registry.add_all(*this);
 }
 
 Layout &Panel::Loader::get_layout()
@@ -264,13 +266,6 @@ void Panel::Loader::arrangement()
        load_sub_with(ldr);
 }
 
-template<typename T>
-void Panel::Loader::child(const string &n)
-{
-       unnamed_child<T>();
-       wdg_map[n] = last_widget;
-}
-
 void Panel::Loader::constraint(Layout::ConstraintType type, const string &n)
 {
        Widget &src = get_last_widget();
@@ -306,15 +301,6 @@ void Panel::Loader::layout()
        load_sub_with(ldr);
 }
 
-template<typename T>
-void Panel::Loader::unnamed_child()
-{
-       RefPtr<T> chl = new T();
-       load_sub(*chl);
-       obj.add(*chl.get());
-       last_widget = chl.release();
-}
-
 template<>
 void Panel::Loader::unnamed_child<Panel>()
 {
@@ -324,6 +310,7 @@ void Panel::Loader::unnamed_child<Panel>()
        last_widget = pnl.release();
 }
 
+
 template<typename T>
 Panel::ArrangedLoader<T>::ArrangedLoader(Loader &ldr, T &arr):
        arr_loader(arr)
index 756108a5cafce149f688906dcb196dcd4c5e5ef4..8881f8cb700adac60dfbff20b4336ae2dfe5df76 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_PANEL_H_
 #define MSP_GLTK_PANEL_H_
 
+#include <msp/datafile/loadabletyperegistry.h>
 #include "container.h"
 #include "layout.h"
 
@@ -21,6 +22,12 @@ public:
                typedef std::map<std::string, Widget *> WidgetMap;
 
        protected:
+               template<typename T>
+               struct AddChildType
+               {
+                       static void add(Loader &, const std::string &);
+               };
+
                WidgetMap &wdg_map;
                Widget *last_widget;
 
@@ -28,8 +35,6 @@ public:
                Loader(Panel &, WidgetMap &);
 
        private:
-               template<typename T>
-               void add_child_type(const std::string &);
                Layout &get_layout();
                Widget &get_last_widget();
                template<typename T>
@@ -44,6 +49,8 @@ public:
                void layout();
                template<typename T>
                void unnamed_child();
+
+               friend class Panel;
        };
 
 private:
@@ -61,12 +68,18 @@ protected:
        std::vector<Widget *> nav_order;
        Layout *layout;
 
+       static DataFile::LoadableTypeRegistry<Loader, Loader::AddChildType> widget_registry;
+       static bool widget_registry_init_done;
+
        Panel(const Panel &);
        Panel &operator=(const Panel &);
 public:
        Panel();
        virtual ~Panel();
 
+       template<typename T>
+       static void register_child_type(const std::string &);
+
        virtual const char *get_class() const { return "panel"; }
 
        void set_layout(Layout *);
@@ -87,6 +100,38 @@ protected:
        virtual void on_child_removed(Widget &);
 };
 
+
+template<typename T>
+void Panel::register_child_type(const std::string &name)
+{
+       widget_registry.register_type<T>(name);
+}
+
+
+template<typename T>
+void Panel::Loader::child(const std::string &n)
+{
+       unnamed_child<T>();
+       wdg_map[n] = last_widget;
+}
+
+template<typename T>
+void Panel::Loader::unnamed_child()
+{
+       RefPtr<T> chl = new T();
+       load_sub(*chl);
+       obj.add(*chl.get());
+       last_widget = chl.release();
+}
+
+
+template<typename T>
+void Panel::Loader::AddChildType<T>::add(Loader &ldr, const std::string &kwd)
+{
+       ldr.add(kwd, &Loader::child<T>);
+       ldr.add(kwd, &Loader::unnamed_child<T>);
+}
+
 } // namespace GLtk
 } // namespace Msp