]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/grid.cpp
Add some classes for automatically creating basic layouts
[libs/gltk.git] / source / grid.cpp
diff --git a/source/grid.cpp b/source/grid.cpp
new file mode 100644 (file)
index 0000000..4bf0167
--- /dev/null
@@ -0,0 +1,82 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2011  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "grid.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+Grid::Grid(bool u):
+       uniform(u),
+       cur_column(0),
+       first(true),
+       skipped(false)
+{ }
+
+void Grid::start_row()
+{
+       cur_column = 0;
+       first = true;
+}
+
+void Grid::skip_cell()
+{
+       ++cur_column;
+       skipped = true;
+       if(cur_column>=columns.size())
+               columns.push_back(0);
+}
+
+Layout::Slot *Grid::create_slot(Widget &wdg)
+{
+       Slot *slot = new Slot(*this, wdg);
+
+       slot->vert_pack.gravity = 1;
+
+       if(!slots.empty())
+       {
+               Slot &prev = *slots.back();
+               if(first)
+                       slot->constraints.push_back(Constraint(BELOW, prev));
+               else
+               {
+                       slot->constraints.push_back(Constraint(ALIGN_TOP, prev));
+                       slot->constraints.push_back(Constraint(ALIGN_BOTTOM, prev));
+                       if(!skipped)
+                               slot->constraints.push_back(Constraint(RIGHT_OF, prev));
+               }
+
+               if(uniform)
+               {
+                       slot->constraints.push_back(Constraint(COPY_WIDTH, prev));
+                       slot->constraints.push_back(Constraint(COPY_HEIGHT, prev));
+               }
+
+               if(cur_column<columns.size() && columns[cur_column])
+               {
+                       Slot &col = *columns[cur_column];
+                       slot->constraints.push_back(Constraint(ALIGN_LEFT, col));
+                       slot->constraints.push_back(Constraint(ALIGN_RIGHT, col));
+               }
+       }
+
+       if(cur_column>=columns.size())
+               columns.push_back(slot);
+       else if(!columns[cur_column])
+               columns[cur_column] = slot;
+
+       first = false;
+       skipped = false;
+       ++cur_column;
+
+       return slot;
+}
+
+} // namespace GLtk
+} // namespace Msp