2 #include <msp/gl/matrix.h>
3 #include <msp/gl/transform.h>
22 void Table::set_rows(unsigned r)
25 data.resize(rows*columns);
28 void Table::set_columns(unsigned c)
30 vector<string> new_data(rows*c);
31 for(unsigned i=0; i<rows; ++i)
32 for(unsigned j=0; j<min(c, columns); ++j)
33 new_data[i*c+j].swap(data[i*columns+j]);
37 col_w.resize(columns);
40 void Table::set_column_width(unsigned c, unsigned w)
43 throw invalid_argument("Table::set_column_width");
47 void Table::set_cell_text(unsigned r, unsigned c, const string &t)
49 if(r>=rows || c>=columns)
50 throw out_of_range("Table::set_cell_text");
52 data[r*columns+c] = t;
55 void Table::render_special(const Part &part) const
57 if(part.get_name()=="cells")
59 const GL::Font *const font = style->get_font();
60 const float font_size = font->get_default_size();
62 unsigned free_width = geom.w;
63 for(unsigned i=0; i<columns; ++i)
64 free_width -= col_w[i];
67 cgeom.h = geom.h/rows;
68 cgeom.y = geom.h-cgeom.h;
70 for(unsigned i=0; i<rows; ++i)
73 for(unsigned j=0; j<columns; ++j)
78 cgeom.w = free_width/columns;
80 const string &text = data[i*columns+j];
84 rgeom.w = static_cast<unsigned>(font->get_string_width(text)*font_size);
85 rgeom.h = static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
87 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
90 GL::translate(rgeom.x, rgeom.y, 0);
91 GL::scale_uniform(font_size);
92 font->draw_string(text);
103 void Table::on_style_change()
108 Table::Loader::Loader(Table &t):
111 add("cell_text", &Loader::cell_text);
112 add("column_width", &Loader::column_width);
113 add("columns", &Loader::columns);
114 add("rows", &Loader::rows);
117 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
119 static_cast<Table &>(obj).set_cell_text(r, c, t);
122 void Table::Loader::column_width(unsigned c, unsigned w)
124 static_cast<Table &>(obj).set_column_width(c, w);
127 void Table::Loader::columns(unsigned c)
129 static_cast<Table &>(obj).set_columns(c);
132 void Table::Loader::rows(unsigned r)
134 static_cast<Table &>(obj).set_rows(r);