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>
28 void Table::set_rows(unsigned r)
31 data.resize(rows*columns);
34 void Table::set_columns(unsigned c)
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]);
43 col_w.resize(columns);
46 void Table::set_column_width(unsigned c, unsigned w)
49 throw InvalidParameterValue("Column index out of bounds");
53 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
55 if(r>=rows || c>=columns)
56 throw InvalidParameterValue("Cell coordinates out of bounds");
58 data[r*columns+c] = t;
61 void Table::render_special(const Part &part) const
63 if(part.get_name()=="cells")
65 const GL::Font *const font = style->get_font();
66 const float font_size = font->get_default_size();
68 unsigned free_width = geom.w;
69 for(unsigned i=0; i<columns; ++i)
70 free_width -= col_w[i];
73 cgeom.h = geom.h/rows;
74 cgeom.y = geom.h-cgeom.h;
76 for(unsigned i=0; i<rows; ++i)
79 for(unsigned j=0; j<columns; ++j)
84 cgeom.w = free_width/columns;
86 const string &text = data[i*columns+j];
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);
93 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
96 GL::translate(rgeom.x, rgeom.y, 0);
97 GL::scale_uniform(font_size);
98 font->draw_string(text);
109 void Table::on_style_change()
114 Table::Loader::Loader(Table &t):
117 add("cell_text", &Loader::cell_text);
118 add("column_width", &Loader::column_width);
119 add("columns", &Loader::columns);
120 add("rows", &Loader::rows);
123 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
125 static_cast<Table &>(obj).set_cell_text(r, c, t);
128 void Table::Loader::column_width(unsigned c, unsigned w)
130 static_cast<Table &>(obj).set_column_width(c, w);
133 void Table::Loader::columns(unsigned c)
135 static_cast<Table &>(obj).set_columns(c);
138 void Table::Loader::rows(unsigned r)
140 static_cast<Table &>(obj).set_rows(r);