]> git.tdb.fi Git - libs/gltk.git/blob - source/grid.cpp
Add a function to find the index of an item in ListDataStore
[libs/gltk.git] / source / grid.cpp
1 #include "grid.h"
2
3 namespace Msp {
4 namespace GLtk {
5
6 Grid::Grid(Layout &l, unsigned c):
7         Arrangement(l),
8         columns(c),
9         first_row(true),
10         column(0)
11 { }
12
13 void Grid::skip()
14 {
15         finish_slot();
16 }
17
18 void Grid::next_row()
19 {
20         if(row_bottom.empty())
21                 throw arrangement_error("empty row not allowed");
22
23         finish_row();
24 }
25
26 void Grid::process_widget(Widget &wdg, Side side, bool aligned)
27 {
28         if(side==TOP)
29         {
30                 Edge &top_edge = (first_row ? edges[TOP] : row_top);
31                 if(top_edge.aligned && aligned)
32                         add_constraint(wdg, Layout::ALIGN_TOP, top_edge);
33
34                 bool snug = (edges[BOTTOM].aligned && aligned);
35                 add_constraint(wdg, (snug ? Layout::BELOW : Layout::FAR_BELOW), BOTTOM);
36
37                 top_edge.add(wdg, aligned);
38         }
39         else if(side==BOTTOM)
40         {
41                 if(row_bottom.aligned && aligned)
42                         add_constraint(wdg, Layout::ALIGN_BOTTOM, row_bottom);
43
44                 row_bottom.add(wdg, aligned);
45         }
46         else if(side==LEFT)
47         {
48                 if(columns[column].left.aligned && aligned)
49                         add_constraint(wdg, Layout::ALIGN_LEFT, columns[column].left);
50                 else if(column>0)
51                 {
52                         bool snug = (columns[column-1].right.aligned && aligned);
53                         add_constraint(wdg, (snug ? Layout::RIGHT_OF : Layout::FAR_RIGHT_OF), columns[column-1].right);
54                 }
55
56                 edges[LEFT].add(wdg, (aligned && !column));
57                 columns[column].left.add(wdg, aligned);
58         }
59         else if(side==RIGHT)
60         {
61                 if(columns[column].right.aligned && aligned)
62                         add_constraint(wdg, Layout::ALIGN_RIGHT, columns[column].right);
63
64                 edges[RIGHT].add(wdg, (aligned && column+1==columns.size()));
65                 columns[column].right.add(wdg, aligned);
66         }
67 }
68
69 void Grid::finish_widget(Widget &wdg)
70 {
71         layout.set_gravity(wdg, -1, 1);
72 }
73
74 void Grid::finish_slot()
75 {
76         ++column;
77         if(column==columns.size())
78                 finish_row();
79 }
80
81 void Grid::finish_row()
82 {
83         edges[BOTTOM] = row_bottom;
84         row_bottom.clear();
85         row_top.clear();
86         first_row = false;
87         column = 0;
88 }
89
90
91 Grid::Loader::Loader(Grid &g):
92         DataFile::ObjectLoader<Grid>(g)
93 {
94         add("next_row", &Loader::next_row);
95         add("skip",     &Loader::skip);
96 }
97
98 void Grid::Loader::next_row()
99 {
100         obj.next_row();
101 }
102
103 void Grid::Loader::skip()
104 {
105         obj.skip();
106 }
107
108 } // namespace GLtk
109 } // namespace Msp