]> git.tdb.fi Git - libs/gltk.git/blob - source/table.cpp
Add child_added/removed events to Container
[libs/gltk.git] / source / table.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/transform.h>
10 #include "part.h"
11 #include "style.h"
12 #include "table.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GLtk {
18
19 Table::Table():
20         rows(1),
21         columns(1),
22         data(1),
23         col_w(1)
24 {
25         focusable = false;
26 }
27
28 void Table::set_rows(unsigned r)
29 {
30         rows = r;
31         data.resize(rows*columns);
32 }
33
34 void Table::set_columns(unsigned c)
35 {
36         vector<string> new_data(rows*c);
37         for(unsigned i=0; i<rows; ++i)
38                 for(unsigned j=0; j<min(c, columns); ++j)
39                         new_data[i*c+j].swap(data[i*columns+j]);
40         data.swap(new_data);
41         columns = c;
42
43         col_w.resize(columns);
44 }
45
46 void Table::set_column_width(unsigned c, unsigned w)
47 {
48         if(c>=columns)
49                 throw InvalidParameterValue("Column index out of bounds");
50         col_w[c] = w;
51 }
52
53 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
54 {
55         if(r>=rows || c>=columns)
56                 throw InvalidParameterValue("Cell coordinates out of bounds");
57
58         data[r*columns+c] = t;
59 }
60
61 void Table::render_special(const Part &part) const
62 {
63         if(part.get_name()=="cells")
64         {
65                 const GL::Font *const font = style->get_font();
66                 const float font_size = font->get_default_size();
67
68                 unsigned free_width = geom.w;
69                 for(unsigned i=0; i<columns; ++i)
70                         free_width -= col_w[i];
71
72                 Geometry cgeom;
73                 cgeom.h = geom.h/rows;
74                 cgeom.y = geom.h-cgeom.h;
75
76                 for(unsigned i=0; i<rows; ++i)
77                 {
78                         cgeom.x = 0;
79                         for(unsigned j=0; j<columns; ++j)
80                         {
81                                 if(col_w[j])
82                                         cgeom.w = col_w[j];
83                                 else
84                                         cgeom.w = free_width/columns;
85
86                                 const string &text = data[i*columns+j];
87                                 Geometry rgeom;
88                                 rgeom.x = cgeom.x;
89                                 rgeom.y = cgeom.y;
90                                 rgeom.w = static_cast<unsigned>(font->get_string_width(text)*font_size);
91                                 rgeom.h = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
92
93                                 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
94
95                                 GL::push_matrix();
96                                 GL::translate(rgeom.x, rgeom.y, 0);
97                                 GL::scale_uniform(font_size);
98                                 font->draw_string(text);
99                                 GL::pop_matrix();
100
101                                 cgeom.x += cgeom.w;
102                         }
103
104                         cgeom.y -= cgeom.h;
105                 }
106         }
107 }
108
109 void Table::on_style_change()
110 {
111 }
112
113
114 Table::Loader::Loader(Table &t):
115         Widget::Loader(t)
116 {
117         add("cell_text",    &Loader::cell_text);
118         add("column_width", &Loader::column_width);
119         add("columns",      &Loader::columns);
120         add("rows",         &Loader::rows);
121 }
122
123 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
124 {
125         static_cast<Table &>(obj).set_cell_text(r, c, t);
126 }
127
128 void Table::Loader::column_width(unsigned c, unsigned w)
129 {
130         static_cast<Table &>(obj).set_column_width(c, w);
131 }
132
133 void Table::Loader::columns(unsigned c)
134 {
135         static_cast<Table &>(obj).set_columns(c);
136 }
137
138 void Table::Loader::rows(unsigned r)
139 {
140         static_cast<Table &>(obj).set_rows(r);
141 }
142
143 } // namespace GLtk
144 } // namespace Msp