]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/grid.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / grid.cpp
index 4bf01678c4a226b0ca3ff0c9a82bef857c78af83..eb32cc91d8c4ec2f0c757d36b8f2ceb8f7d3cdea 100644 (file)
-/* $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)
+Grid::Grid(Layout &l, size_t c):
+       Arrangement(l),
+       columns(c)
 { }
 
-void Grid::start_row()
+void Grid::skip()
 {
-       cur_column = 0;
-       first = true;
+       finish_slot();
 }
 
-void Grid::skip_cell()
+void Grid::next_row()
 {
-       ++cur_column;
-       skipped = true;
-       if(cur_column>=columns.size())
-               columns.push_back(0);
+       if(row_bottom.empty())
+               throw arrangement_error("empty row not allowed");
+
+       finish_row();
 }
 
-Layout::Slot *Grid::create_slot(Widget &wdg)
+void Grid::process_widget(Widget &wdg, Side side, bool aligned)
 {
-       Slot *slot = new Slot(*this, wdg);
+       if(side==TOP)
+       {
+               Edge &top_edge = (first_row ? edges[TOP] : row_top);
+               if(top_edge.aligned && aligned)
+                       add_constraint(wdg, Layout::ALIGN_TOP, top_edge);
 
-       slot->vert_pack.gravity = 1;
+               bool snug = (edges[BOTTOM].aligned && aligned);
+               add_constraint(wdg, (snug ? Layout::BELOW : Layout::FAR_BELOW), BOTTOM);
 
-       if(!slots.empty())
+               top_edge.add(wdg, aligned);
+       }
+       else if(side==BOTTOM)
        {
-               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(row_bottom.aligned && aligned)
+                       add_constraint(wdg, Layout::ALIGN_BOTTOM, row_bottom);
 
-               if(uniform)
+               row_bottom.add(wdg, aligned);
+       }
+       else if(side==LEFT)
+       {
+               if(columns[column].left.aligned && aligned)
+                       add_constraint(wdg, Layout::ALIGN_LEFT, columns[column].left);
+               else if(column>0)
                {
-                       slot->constraints.push_back(Constraint(COPY_WIDTH, prev));
-                       slot->constraints.push_back(Constraint(COPY_HEIGHT, prev));
+                       bool snug = (columns[column-1].right.aligned && aligned);
+                       add_constraint(wdg, (snug ? Layout::RIGHT_OF : Layout::FAR_RIGHT_OF), columns[column-1].right);
                }
 
-               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));
-               }
+               edges[LEFT].add(wdg, (aligned && !column));
+               columns[column].left.add(wdg, aligned);
        }
+       else if(side==RIGHT)
+       {
+               if(columns[column].right.aligned && aligned)
+                       add_constraint(wdg, Layout::ALIGN_RIGHT, columns[column].right);
 
-       if(cur_column>=columns.size())
-               columns.push_back(slot);
-       else if(!columns[cur_column])
-               columns[cur_column] = slot;
+               edges[RIGHT].add(wdg, (aligned && column+1==columns.size()));
+               columns[column].right.add(wdg, aligned);
+       }
+}
+
+void Grid::finish_widget(Widget &wdg)
+{
+       layout.set_gravity(wdg, -1, 1);
+}
+
+void Grid::finish_slot()
+{
+       ++column;
+       if(column==columns.size())
+               finish_row();
+}
+
+void Grid::finish_row()
+{
+       edges[BOTTOM] = row_bottom;
+       row_bottom.clear();
+       row_top.clear();
+       first_row = false;
+       column = 0;
+}
 
-       first = false;
-       skipped = false;
-       ++cur_column;
 
-       return slot;
+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