]> git.tdb.fi Git - libs/gltk.git/commitdiff
Support specifying layouts in datafiles
authorMikko Rasa <tdb@tdb.fi>
Sat, 15 Jun 2013 21:34:53 +0000 (00:34 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 15 Jun 2013 21:34:53 +0000 (00:34 +0300)
source/column.cpp
source/column.h
source/dialog.cpp
source/grid.cpp
source/grid.h
source/layout.cpp
source/layout.h
source/panel.cpp
source/panel.h
source/row.cpp
source/row.h

index 6c225ee994a52171fbc041a3e13c499b068b6702..d31544ab2627ed88c5fc529c733651257e49caf9 100644 (file)
@@ -48,5 +48,17 @@ void Column::finish_slot()
        first = false;
 }
 
+
+Column::Loader::Loader(Column &c):
+       DataFile::ObjectLoader<Column>(c)
+{
+       add("split", &Loader::split);
+}
+
+void Column::Loader::split()
+{
+       obj.split();
+}
+
 } // namespace GLtk
 } // namespace Msp
index c7efd3b503e63769477a92c85eadc3384fa31650..b6092bb5dded802e92fb579ad8569218dd00399e 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_COLUMN_H_
 #define MSP_GLTK_COLUMN_H_
 
+#include <msp/datafile/objectloader.h>
 #include "arrangement.h"
 
 namespace Msp {
@@ -8,6 +9,15 @@ namespace GLtk {
 
 class Column: public Arrangement
 {
+public:
+       class Loader: public DataFile::ObjectLoader<Column>
+       {
+       public:
+               Loader(Column &);
+       private:
+               void split();
+       };
+
 private:
        Edge next_bottom;
        bool first;
index dba276d32d7aa05de119fa2cdf48407cbdcf4fe9..2342e8483786b6ce5eba91f1a6c497290a70f7da 100644 (file)
@@ -49,7 +49,7 @@ void Dialog::Loader::action_button(const string &n, int c)
        RefPtr<Button> btn = new Button();
        load_sub(*btn);
        obj.add_button(*btn.get(), c);
-       wdg_map[n] = btn.release();
+       last_widget = wdg_map[n] = btn.release();
 }
 
 } // namespace GLtk
index b3e9c43c311898896638e267d13edcdc88cba1a7..6296098f511017c339058050a4a73eaba2c81de9 100644 (file)
@@ -84,5 +84,23 @@ void Grid::finish_row()
        column = 0;
 }
 
+
+Grid::Loader::Loader(Grid &g):
+       DataFile::ObjectLoader<Grid>(g)
+{
+       add("next_row", &Loader::next_row);
+       add("skip",     &Loader::skip);
+}
+
+void Grid::Loader::next_row()
+{
+       obj.next_row();
+}
+
+void Grid::Loader::skip()
+{
+       obj.skip();
+}
+
 } // namespace GLtk
 } // namespace Msp
index 0e71748b6a71bdd84da98bdd31c8bfa3957094c9..621a29435318317be9aa7d00ea930b9efb93d3c2 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_GRID_H_
 #define MSP_GLTK_GRID_H_
 
+#include <msp/datafile/objectloader.h>
 #include "arrangement.h"
 
 namespace Msp {
@@ -8,6 +9,16 @@ namespace GLtk {
 
 class Grid: public Arrangement
 {
+public:
+       class Loader: public DataFile::ObjectLoader<Grid>
+       {
+       public:
+               Loader(Grid &);
+       private:
+               void next_row();
+               void skip();
+       };
+
 private:
        struct Column
        {
index c41dcb87b68172236b1bb67155b48990ca79609e..917d47a7cbc9e2f5b0c2184f0e4fd7bfe6cea85f 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <limits>
+#include <msp/strings/format.h>
 #include "arrangement.h"
 #include "container.h"
 #include "layout.h"
@@ -480,6 +481,42 @@ void Layout::Slot::visibility_changed(bool v)
 }
 
 
+void operator>>(const LexicalConverter &conv, Layout::ConstraintType &ctype)
+{
+       const string &str = conv.get();
+       if(str=="ABOVE")
+               ctype = Layout::ABOVE;
+       else if(str=="BELOW")
+               ctype = Layout::BELOW;
+       else if(str=="RIGHT_OF")
+               ctype = Layout::RIGHT_OF;
+       else if(str=="LEFT_OF")
+               ctype = Layout::LEFT_OF;
+       else if(str=="FAR_ABOVE")
+               ctype = Layout::FAR_ABOVE;
+       else if(str=="FAR_BELOW")
+               ctype = Layout::FAR_BELOW;
+       else if(str=="FAR_RIGHT_OF")
+               ctype = Layout::FAR_RIGHT_OF;
+       else if(str=="FAR_LEFT_OF")
+               ctype = Layout::FAR_LEFT_OF;
+       else if(str=="ALIGN_TOP")
+               ctype = Layout::ALIGN_TOP;
+       else if(str=="ALIGN_BOTTOM")
+               ctype = Layout::ALIGN_BOTTOM;
+       else if(str=="ALIGN_RIGHT")
+               ctype = Layout::ALIGN_RIGHT;
+       else if(str=="ALIGN_LEFT")
+               ctype = Layout::ALIGN_LEFT;
+       else if(str=="COPY_WIDTH")
+               ctype = Layout::COPY_WIDTH;
+       else if(str=="COPY_HEIGHT")
+               ctype = Layout::COPY_HEIGHT;
+       else
+               throw lexical_error(format("conversion of '%s' to ConstraintType", str));
+}
+
+
 Layout::LinearProgram::LinearProgram(unsigned s):
        n_columns(s),
        n_rows(1),
index 85d64ae4d64f4f670365a58fbc921cb7406660a6..2de4013a5d1e8b943cf939abd5b63b114e5fd1e3 100644 (file)
@@ -4,6 +4,7 @@
 #include <list>
 #include <set>
 #include <sigc++/trackable.h>
+#include <msp/strings/lexicalcast.h>
 #include "geometry.h"
 
 namespace Msp {
@@ -191,6 +192,8 @@ private:
        void solve_constraints(int, SolveMode);
 };
 
+void operator>>(const LexicalConverter &, Layout::ConstraintType &);
+
 } // namespace GLtk
 } // namespace Msp
 
index 9bc752413c84b3952feb43f76dc13758d4dd1118..c90c83b970d757dff2404331376ee777b09a2ef1 100644 (file)
@@ -1,15 +1,18 @@
 #include <algorithm>
+#include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
 #include "button.h"
+#include "column.h"
 #include "dropdown.h"
 #include "entry.h"
+#include "grid.h"
 #include "hslider.h"
 #include "indicator.h"
 #include "label.h"
-#include "layout.h"
 #include "list.h"
 #include "panel.h"
 #include "part.h"
+#include "row.h"
 #include "toggle.h"
 #include "vslider.h"
 
@@ -78,27 +81,82 @@ void Panel::on_child_removed(Widget &wdg)
 
 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        DataFile::DerivedObjectLoader<Panel, Widget::Loader>(p),
-       wdg_map(m)
+       wdg_map(m),
+       last_widget(0)
 {
        add("button",    &Loader::child<Button>);
+       add("column",    &Loader::arrangement<Column>);
+       add("constraint",&Loader::constraint);
        add("dropdown",  &Loader::child<Dropdown>);
        add("entry",     &Loader::child<Entry>);
+       add("expand",    &Loader::expand);
+       add("gravity",   &Loader::gravity);
+       add("grid",      &Loader::grid);
        add("hslider",   &Loader::child<HSlider>);
        add("indicator", &Loader::child<Indicator>);
        add("label",     &Loader::child<Label>);
        add("list",      &Loader::child<List>);
        add("panel",     &Loader::panel);
+       add("row",       &Loader::arrangement<Row>);
        add("toggle",    &Loader::child<Toggle>);
        add("vslider",   &Loader::child<VSlider>);
 }
 
+Layout &Panel::Loader::get_layout()
+{
+       if(!obj.layout)
+               obj.set_layout(new Layout);
+
+       return *obj.layout;
+}
+
+Widget &Panel::Loader::get_last_widget()
+{
+       if(!last_widget)
+               throw logic_error("no widget loaded");
+
+       return *last_widget;
+}
+
+template<typename T>
+void Panel::Loader::arrangement()
+{
+       T arr(get_layout());
+       ArrangedLoader<T> ldr(*this, arr);
+       load_sub_with(ldr);
+}
+
 template<typename T>
 void Panel::Loader::child(const string &n)
 {
        RefPtr<T> chl = new T();
        load_sub(*chl);
        obj.add(*chl.get());
-       wdg_map[n] = chl.release();
+       last_widget = wdg_map[n] = chl.release();
+}
+
+void Panel::Loader::constraint(Layout::ConstraintType type, const string &n)
+{
+       Widget &src = get_last_widget();
+       Widget &tgt = *get_item(wdg_map, n);
+       get_layout().add_constraint(src, type, tgt);
+}
+
+void Panel::Loader::expand(bool h, bool v)
+{
+       get_layout().set_expand(get_last_widget(), h, v);
+}
+
+void Panel::Loader::gravity(int h, int v)
+{
+       get_layout().set_gravity(get_last_widget(), h, v);
+}
+
+void Panel::Loader::grid(unsigned cols)
+{
+       Grid grd(get_layout(), cols);
+       ArrangedLoader<Grid> ldr(*this, grd);
+       load_sub_with(ldr);
 }
 
 void Panel::Loader::panel(const string &n)
@@ -106,7 +164,16 @@ void Panel::Loader::panel(const string &n)
        RefPtr<Panel> p = new Panel();
        load_sub(*p, wdg_map);
        obj.add(*p.get());
-       wdg_map[n] = p.release();
+       last_widget = wdg_map[n] = p.release();
+}
+
+
+template<typename T>
+Panel::ArrangedLoader<T>::ArrangedLoader(Loader &ldr, T &arr):
+       arr_loader(arr)
+{
+       add_auxiliary_loader(ldr);
+       add_auxiliary_loader(arr_loader);
 }
 
 } // namespace GLtk
index 258d0ceeede592cf19c793da33ddb6a443a7c712..830f5426bd72b0e2b9e5a6802c82d1c0f2865e1c 100644 (file)
@@ -2,12 +2,11 @@
 #define MSP_GLTK_PANEL_H_
 
 #include "container.h"
+#include "layout.h"
 
 namespace Msp {
 namespace GLtk {
 
-class Layout;
-
 /**
 Panels are containers for other widgets.  Panel styles should have a special
 part "children" to render the child widgets.  All properties of this part are
@@ -23,15 +22,35 @@ public:
 
        protected:
                WidgetMap &wdg_map;
+               Widget *last_widget;
        
        public:
                Loader(Panel &, WidgetMap &);
        private:
+               Layout &get_layout();
+               Widget &get_last_widget();
+               template<typename T>
+               void arrangement();
                template<typename T>
                void child(const std::string &);
+               void constraint(Layout::ConstraintType, const std::string &);
+               void expand(bool, bool);
+               void gravity(int, int);
+               void grid(unsigned);
                void panel(const std::string &);
        };
 
+private:
+       template<typename T>
+       class ArrangedLoader: public DataFile::Loader
+       {
+       private:
+               typename T::Loader arr_loader;
+
+       public:
+               ArrangedLoader(Loader &, T &);
+       };
+
 protected:
        Layout *layout;
 
index fc07cdbf25cb1c92cd56ca7e236edf9cb99c922f..ae13e49ec2e64f9f331db394d9c8367b56ec3c37 100644 (file)
@@ -48,5 +48,17 @@ void Row::finish_slot()
        first = false;
 }
 
+
+Row::Loader::Loader(Row &c):
+       DataFile::ObjectLoader<Row>(c)
+{
+       add("split", &Loader::split);
+}
+
+void Row::Loader::split()
+{
+       obj.split();
+}
+
 } // namespace GLtk
 } // namespace Msp
index 6391008293ecb052439bca810614e1cf8d308c4e..58480db013c16da4e5924f10f57d11beef8b5c74 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_ROW_H_
 #define MSP_GLTK_ROW_H_
 
+#include <msp/datafile/objectloader.h>
 #include "arrangement.h"
 
 namespace Msp {
@@ -8,6 +9,15 @@ namespace GLtk {
 
 class Row: public Arrangement
 {
+public:
+       class Loader: public DataFile::ObjectLoader<Row>
+       {
+       public:
+               Loader(Row &);
+       private:
+               void split();
+       };
+
 private:
        Edge next_right;
        bool first;