1 #include <msp/gl/matrix.h>
2 #include <msp/gl/transform.h>
21 void Table::set_rows(unsigned r)
24 data.resize(rows*columns);
27 void Table::set_columns(unsigned c)
29 vector<string> new_data(rows*c);
30 for(unsigned i=0; i<rows; ++i)
31 for(unsigned j=0; j<min(c, columns); ++j)
32 new_data[i*c+j].swap(data[i*columns+j]);
36 col_w.resize(columns);
39 void Table::set_column_width(unsigned c, unsigned w)
42 throw InvalidParameterValue("Column index out of bounds");
46 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
48 if(r>=rows || c>=columns)
49 throw InvalidParameterValue("Cell coordinates out of bounds");
51 data[r*columns+c] = t;
54 void Table::render_special(const Part &part) const
56 if(part.get_name()=="cells")
58 const GL::Font *const font = style->get_font();
59 const float font_size = font->get_default_size();
61 unsigned free_width = geom.w;
62 for(unsigned i=0; i<columns; ++i)
63 free_width -= col_w[i];
66 cgeom.h = geom.h/rows;
67 cgeom.y = geom.h-cgeom.h;
69 for(unsigned i=0; i<rows; ++i)
72 for(unsigned j=0; j<columns; ++j)
77 cgeom.w = free_width/columns;
79 const string &text = data[i*columns+j];
83 rgeom.w = static_cast<unsigned>(font->get_string_width(text)*font_size);
84 rgeom.h = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
86 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
89 GL::translate(rgeom.x, rgeom.y, 0);
90 GL::scale_uniform(font_size);
91 font->draw_string(text);
102 void Table::on_style_change()
107 Table::Loader::Loader(Table &t):
110 add("cell_text", &Loader::cell_text);
111 add("column_width", &Loader::column_width);
112 add("columns", &Loader::columns);
113 add("rows", &Loader::rows);
116 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
118 static_cast<Table &>(obj).set_cell_text(r, c, t);
121 void Table::Loader::column_width(unsigned c, unsigned w)
123 static_cast<Table &>(obj).set_column_width(c, w);
126 void Table::Loader::columns(unsigned c)
128 static_cast<Table &>(obj).set_columns(c);
131 void Table::Loader::rows(unsigned r)
133 static_cast<Table &>(obj).set_rows(r);