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):
29 void Table::set_rows(unsigned r)
32 data.resize(rows*columns);
35 void Table::set_columns(unsigned c)
37 vector<string> new_data(rows*c);
38 for(unsigned i=0; i<rows; ++i)
39 for(unsigned j=0; j<min(c, columns); ++j)
40 new_data[i*c+j].swap(data[i*columns+j]);
44 col_w.resize(columns);
47 void Table::set_column_width(unsigned c, unsigned w)
50 throw InvalidParameterValue("Column index out of bounds");
54 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
56 if(r>=rows || c>=columns)
57 throw InvalidParameterValue("Cell coordinates out of bounds");
62 void Table::render_special(const Part &part) const
64 if(part.get_name()=="cells")
66 const GL::Font *const font=style->get_font();
67 const float font_size=font->get_default_size();
69 unsigned free_width=geom.w;
70 for(unsigned i=0; i<columns; ++i)
75 cgeom.y=geom.h-cgeom.h;
77 for(unsigned i=0; i<rows; ++i)
80 for(unsigned j=0; j<columns; ++j)
85 cgeom.w=free_width/columns;
87 const string &text=data[i*columns+j];
91 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
92 rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
94 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
97 GL::translate(rgeom.x, rgeom.y, 0);
98 GL::scale_uniform(font_size);
99 font->draw_string(text);
110 void Table::on_style_change()
115 Table::Loader::Loader(Table &t):
118 add("cell_text", &Loader::cell_text);
119 add("column_width", &Loader::column_width);
120 add("columns", &Loader::columns);
121 add("rows", &Loader::rows);
124 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
126 static_cast<Table &>(wdg).set_cell_text(r, c, t);
129 void Table::Loader::column_width(unsigned c, unsigned w)
131 static_cast<Table &>(wdg).set_column_width(c, w);
134 void Table::Loader::columns(unsigned c)
136 static_cast<Table &>(wdg).set_columns(c);
139 void Table::Loader::rows(unsigned r)
141 static_cast<Table &>(wdg).set_rows(r);