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