]> git.tdb.fi Git - libs/gltk.git/commitdiff
Allow loading generic layout properties from datafiles
authorMikko Rasa <tdb@tdb.fi>
Sun, 16 Jun 2013 19:33:52 +0000 (22:33 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 16 Jun 2013 19:33:52 +0000 (22:33 +0300)
source/layout.cpp
source/layout.h
source/panel.cpp
source/panel.h

index 917d47a7cbc9e2f5b0c2184f0e4fd7bfe6cea85f..c616ff6fc7caff9833070cf1c30e7c7710e20ad9 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <limits>
+#include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #include "arrangement.h"
 #include "container.h"
@@ -481,6 +482,74 @@ void Layout::Slot::visibility_changed(bool v)
 }
 
 
+Layout::Loader::Loader(Layout &l, const WidgetMap &wm):
+       DataFile::ObjectLoader<Layout>(l),
+       wdg_map(wm)
+{
+       add("column_spacing", &Loader::column_spacing);
+       add("margin",         &Loader::margin);
+       add("row_spacing",    &Loader::row_spacing);
+       add("spacing",        &Loader::spacing);
+       add("widget",         &Loader::widget);
+}
+
+void Layout::Loader::column_spacing(unsigned s)
+{
+       obj.set_column_spacing(s);
+}
+
+void Layout::Loader::margin()
+{
+       Sides sides;
+       load_sub(sides);
+       obj.set_margin(sides);
+}
+
+void Layout::Loader::spacing(unsigned s)
+{
+       obj.set_spacing(s);
+}
+
+void Layout::Loader::row_spacing(unsigned s)
+{
+       obj.set_row_spacing(s);
+}
+
+void Layout::Loader::widget(const string &n)
+{
+       Widget &wdg = *get_item(wdg_map, n);
+       WidgetLoader ldr(obj, wdg, wdg_map);
+       load_sub_with(ldr);
+}
+
+
+Layout::WidgetLoader::WidgetLoader(Layout &l, Widget &w, const Layout::Loader::WidgetMap &wm):
+       layout(l),
+       widget(w),
+       wdg_map(wm)
+{
+       add("constraint", &WidgetLoader::constraint);
+       add("expand",     &WidgetLoader::expand);
+       add("gravity",    &WidgetLoader::gravity);
+}
+
+void Layout::WidgetLoader::constraint(ConstraintType type, const string &n)
+{
+       Widget &target = *get_item(wdg_map, n);
+       layout.add_constraint(widget, type, target);
+}
+
+void Layout::WidgetLoader::expand(bool h, bool v)
+{
+       layout.set_expand(widget, h, v);
+}
+
+void Layout::WidgetLoader::gravity(int h, int v)
+{
+       layout.set_gravity(widget, h, v);
+}
+
+
 void operator>>(const LexicalConverter &conv, Layout::ConstraintType &ctype)
 {
        const string &str = conv.get();
index 2de4013a5d1e8b943cf939abd5b63b114e5fd1e3..b8c4eddf51c526222538457b64a971d6e6e83ad2 100644 (file)
@@ -86,7 +86,42 @@ public:
                COPY_HEIGHT = VERTICAL|SELF_DIM|TARGET_DIM
        };
 
+       class Loader: public DataFile::ObjectLoader<Layout>
+       {
+       public:
+               typedef std::map<std::string, Widget *> WidgetMap;
+
+       private:
+               const WidgetMap &wdg_map;
+
+       public:
+               Loader(Layout &, const WidgetMap &);
+
+       private:
+               void column_spacing(unsigned);
+               void margin();
+               void row_spacing(unsigned);
+               void spacing(unsigned);
+               void widget(const std::string &);
+       };
+
 private:
+       class WidgetLoader: public DataFile::Loader
+       {
+       private:
+               Layout &layout;
+               Widget &widget;
+               const Layout::Loader::WidgetMap &wdg_map;
+
+       public:
+               WidgetLoader(Layout &, Widget &, const Layout::Loader::WidgetMap &);
+
+       private:
+               void constraint(ConstraintType, const std::string &);
+               void expand(bool, bool);
+               void gravity(int, int);
+       };
+
        struct Slot;
 
        struct Constraint
index c90c83b970d757dff2404331376ee777b09a2ef1..8c5dd38f26be2d400640258882da93c24a50690a 100644 (file)
@@ -95,6 +95,7 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        add("hslider",   &Loader::child<HSlider>);
        add("indicator", &Loader::child<Indicator>);
        add("label",     &Loader::child<Label>);
+       add("layout",    &Loader::layout);
        add("list",      &Loader::child<List>);
        add("panel",     &Loader::panel);
        add("row",       &Loader::arrangement<Row>);
@@ -159,6 +160,12 @@ void Panel::Loader::grid(unsigned cols)
        load_sub_with(ldr);
 }
 
+void Panel::Loader::layout()
+{
+       Layout::Loader ldr(get_layout(), wdg_map);
+       load_sub_with(ldr);
+}
+
 void Panel::Loader::panel(const string &n)
 {
        RefPtr<Panel> p = new Panel();
index 830f5426bd72b0e2b9e5a6802c82d1c0f2865e1c..79e8bd455a3b36cbd76f9f113641cb81baa79731 100644 (file)
@@ -37,6 +37,7 @@ public:
                void expand(bool, bool);
                void gravity(int, int);
                void grid(unsigned);
+               void layout();
                void panel(const std::string &);
        };