]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/panel.h
Use a LoadableTypeRegistry to manage child types for Panel
[libs/gltk.git] / source / panel.h
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