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