2 #include <msp/gl/matrix.h>
3 #include <msp/gl/meshbuilder.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::rebuild_special(const Part &part, CachedPart &cache)
57 if(part.get_name()=="cells")
59 const GL::Font &font = style->get_font();
60 float font_size = style->get_font_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 cache.texture = &font.get_texture();
73 GL::MeshBuilder bld(*cache.mesh);
74 for(unsigned i=0; i<rows; ++i)
77 for(unsigned j=0; j<columns; ++j)
82 cgeom.w = free_width/columns;
84 const string &text = data[i*columns+j];
88 rgeom.w = static_cast<unsigned>(font.get_string_width(text)*font_size);
89 rgeom.h = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
91 part.get_alignment().apply(rgeom, cgeom, part.get_margin());
93 GL::MatrixStack::Push _push(bld.matrix());
94 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
95 bld.matrix() *= GL::Matrix::scaling(font_size);
96 font.draw_string(text);
106 void Table::on_style_change()
111 Table::Loader::Loader(Table &t):
114 add("cell_text", &Loader::cell_text);
115 add("column_width", &Loader::column_width);
116 add("columns", &Loader::columns);
117 add("rows", &Loader::rows);
120 void Table::Loader::cell_text(unsigned r, unsigned c, const string &t)
122 static_cast<Table &>(obj).set_cell_text(r, c, t);
125 void Table::Loader::column_width(unsigned c, unsigned w)
127 static_cast<Table &>(obj).set_column_width(c, w);
130 void Table::Loader::columns(unsigned c)
132 static_cast<Table &>(obj).set_columns(c);
135 void Table::Loader::rows(unsigned r)
137 static_cast<Table &>(obj).set_rows(r);