]> git.tdb.fi Git - libs/gltk.git/blob - source/grid.cpp
Add some classes for automatically creating basic layouts
[libs/gltk.git] / source / grid.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "grid.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 Grid::Grid(bool u):
16         uniform(u),
17         cur_column(0),
18         first(true),
19         skipped(false)
20 { }
21
22 void Grid::start_row()
23 {
24         cur_column = 0;
25         first = true;
26 }
27
28 void Grid::skip_cell()
29 {
30         ++cur_column;
31         skipped = true;
32         if(cur_column>=columns.size())
33                 columns.push_back(0);
34 }
35
36 Layout::Slot *Grid::create_slot(Widget &wdg)
37 {
38         Slot *slot = new Slot(*this, wdg);
39
40         slot->vert_pack.gravity = 1;
41
42         if(!slots.empty())
43         {
44                 Slot &prev = *slots.back();
45                 if(first)
46                         slot->constraints.push_back(Constraint(BELOW, prev));
47                 else
48                 {
49                         slot->constraints.push_back(Constraint(ALIGN_TOP, prev));
50                         slot->constraints.push_back(Constraint(ALIGN_BOTTOM, prev));
51                         if(!skipped)
52                                 slot->constraints.push_back(Constraint(RIGHT_OF, prev));
53                 }
54
55                 if(uniform)
56                 {
57                         slot->constraints.push_back(Constraint(COPY_WIDTH, prev));
58                         slot->constraints.push_back(Constraint(COPY_HEIGHT, prev));
59                 }
60
61                 if(cur_column<columns.size() && columns[cur_column])
62                 {
63                         Slot &col = *columns[cur_column];
64                         slot->constraints.push_back(Constraint(ALIGN_LEFT, col));
65                         slot->constraints.push_back(Constraint(ALIGN_RIGHT, col));
66                 }
67         }
68
69         if(cur_column>=columns.size())
70                 columns.push_back(slot);
71         else if(!columns[cur_column])
72                 columns[cur_column] = slot;
73
74         first = false;
75         skipped = false;
76         ++cur_column;
77
78         return slot;
79 }
80
81 } // namespace GLtk
82 } // namespace Msp