3 This file is part of libmspgltk
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/transform.h>
19 Table::Table(const Resources &r):
30 void Table::set_rows(unsigned r)
33 data.resize(rows*columns);
36 void Table::set_columns(unsigned c)
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]);
45 col_w.resize(columns);
48 void Table::set_column_width(unsigned c, unsigned w)
51 throw InvalidParameterValue("Column index out of bounds");
55 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
57 if(r>=rows || c>=columns)
58 throw InvalidParameterValue("Cell coordinates out of bounds");
60 data[r*columns+c] = t;
63 void Table::render_special(const Part &part) const
65 if(part.get_name()=="cells")
67 const GL::Font *const font = style->get_font();
68 const float font_size = font->get_default_size();
70 unsigned free_width = geom.w;
71 for(unsigned i=0; i<columns; ++i)
72 free_width -= col_w[i];
75 cgeom.h = geom.h/rows;
76 cgeom.y = geom.h-cgeom.h;
78 for(unsigned i=0; i<rows; ++i)
81 for(unsigned j=0; j<columns; ++j)
86 cgeom.w = free_width/columns;
88 const string &text = data[i*columns+j];
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);
95 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
98 GL::translate(rgeom.x, rgeom.y, 0);
99 GL::scale_uniform(font_size);
100 font->draw_string(text);
111 void Table::on_style_change()
116 Table::Loader::Loader(Table &t):
119 add("cell_text", &Loader::cell_text);
120 add("column_width", &Loader::column_width);
121 add("columns", &Loader::columns);
122 add("rows", &Loader::rows);
125 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
127 static_cast<Table &>(wdg).set_cell_text(r, c, t);
130 void Table::Loader::column_width(unsigned c, unsigned w)
132 static_cast<Table &>(wdg).set_column_width(c, w);
135 void Table::Loader::columns(unsigned c)
137 static_cast<Table &>(wdg).set_columns(c);
140 void Table::Loader::rows(unsigned r)
142 static_cast<Table &>(wdg).set_rows(r);