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