5 #include "math_gridinset.h"
6 #include "support/LOstream.h"
13 int const MATH_COLSEP = 10;
15 int const MATH_ROWSEP = 10;
17 int const MATH_BORDER = 2;
22 MathGridInset::RowInfo::RowInfo()
23 : upperline_(false), lowerline_(false)
27 MathGridInset::ColInfo::ColInfo()
28 : h_align_('c'), leftline_(false), rightline_(false)
32 MathGridInset::MathGridInset(int m, int n, string const & nm, MathInsetTypes ot)
33 : MathInset(m * n, nm, ot), rowinfo_(n), colinfo_(m), v_align_('c')
36 lyxerr << "positve number of columns expected\n";
38 lyxerr << "positve number of rows expected\n";
42 int MathGridInset::index(int row, int col) const
44 return col + ncols() * row;
48 void MathGridInset::halign(string const & hh)
53 for (int i = 0; i < n; ++i)
54 colinfo_[i].h_align_ = hh[i];
57 void MathGridInset::halign(char h, int col)
59 colinfo_[col].h_align_ = h;
62 char MathGridInset::halign(int col) const
64 return colinfo_[col].h_align_;
67 void MathGridInset::valign(char c)
72 char MathGridInset::valign() const
77 void MathGridInset::Metrics(MathStyles st, int, int)
79 // let the cells adjust themselves
80 MathInset::Metrics(st);
83 // adjust vertical structure
84 for (int row = 0; row < nrows(); ++row) {
87 for (int col = 0; col < ncols(); ++col) {
88 MathXArray const & c = xcell(index(row, col));
89 asc = std::max(asc, c.ascent());
90 desc = std::max(desc, c.descent());
92 rowinfo_[row].ascent_ = asc;
93 rowinfo_[row].descent_ = desc;
96 rowinfo_[row].offset_ =
97 rowinfo_[row - 1].offset_ +
98 rowinfo_[row - 1].descent_ +
100 rowinfo_[row].ascent_;
102 rowinfo_[row].offset_ = 0;
105 // adjust vertical offset
112 h = rowinfo_.back().offset_;
115 h = rowinfo_.back().offset_ / 2;
118 for (int row = 0; row < nrows(); ++row) {
119 rowinfo_[row].offset_ -= h;
120 rowinfo_[row].offset_ += MATH_BORDER;
123 // adjust horizontal structure
124 for (int col = 0; col < ncols(); ++col) {
126 for (int row = 0; row < nrows(); ++row)
127 wid = std::max(wid, xcell(index(row, col)).width());
128 colinfo_[col].width_ = wid;
129 colinfo_[col].offset_ = colinfo_[col].width_;
132 colinfo_[col].offset_ =
133 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
135 colinfo_[col].offset_ = 0;
137 colinfo_[col].offset_ += MATH_BORDER;
140 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
141 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
142 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
145 // Increase ws_[i] for 'R' columns (except the first one)
146 for (int i = 1; i < nc_; ++i)
147 if (h_align_[i] == 'R')
148 ws_[i] += 10 * df_width;
149 // Increase ws_[i] for 'C' column
150 if (h_align_[0] == 'C')
151 if (ws_[0] < 7 * workwidth / 8)
152 ws_[0] = 7 * workwidth / 8;
156 for (cxrow = row_.begin(); cxrow; ++cxrow) {
157 int rg = MATH_COLSEP;
159 for (int i = 0; i < nc_; ++i) {
161 if (cxrow->getTab(i) <= 0) {
162 cxrow->setTab(i, df_width);
165 switch (h_align_[i]) {
170 lf = (ws_[i] - cxrow->getTab(i))/2;
174 lf = ws_[i] - cxrow->getTab(i);
177 if (cxrow == row_.begin())
179 else if (cxrow.is_last())
180 lf = ws_[i] - cxrow->getTab(i);
182 lf = (ws_[i] - cxrow->getTab(i))/2;
185 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
186 cxrow->setTab(i, lf + rg);
187 rg = ws_[i] - ww + MATH_COLSEP;
188 if (cxrow == row_.begin())
189 width += ws_[i] + MATH_COLSEP;
191 cxrow->setBaseline(cxrow->getBaseline() - ascent);
196 void MathGridInset::draw(Painter & pain, int x, int y)
200 for (int row = 0; row < nrows(); ++row) {
201 int yy = y + rowinfo_[row].offset_;
202 for (int col = 0; col < ncols(); ++col) {
203 int xx = x + colinfo_[col].offset_;
204 char align = colinfo_[col].h_align_;
205 if (align == 'r' || align == 'R')
206 xx += colinfo_[col].width_ - xcell(index(row, col)).width();
207 if (align == 'c' || align == 'C')
208 xx += (colinfo_[col].width_ - xcell(index(row, col)).width()) / 2;
209 xcell(index(row, col)).draw(pain, xx, yy);
215 void MathGridInset::Write(std::ostream & os, bool fragile) const
217 for (int row = 0; row < nrows(); ++row) {
220 for (int col = 0; col < ncols(); ++col) {
223 cell(index(row, col)).Write(os, fragile);
229 void MathGridInset::addRow(int row)
231 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
232 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
235 void MathGridInset::appendRow()
237 rowinfo_.push_back(RowInfo());
238 for (int i = 0; i < ncols(); ++i)
239 cells_.push_back(cells_type::value_type());
243 void MathGridInset::delRow(int row)
248 cells_type::iterator it = cells_.begin() + row * ncols();
249 cells_.erase(it, it + ncols());
251 rowinfo_.erase(rowinfo_.begin() + row);
255 void MathGridInset::addCol(int newcol)
257 int const nc = ncols();
258 int const nr = nrows();
259 cells_type new_cells((nc + 1) * nr);
261 for (int row = 0; row < nr; ++row)
262 for (int col = 0; col < nc; ++col)
263 new_cells[row * (nc + 1) + col + (col > newcol)]
264 = cells_[row * nc + col];
265 std::swap(cells_, new_cells);
267 colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
271 void MathGridInset::delCol(int col)
277 for (int i = 0; i < nargs(); ++i)
278 if (i % ncols() != col)
279 tmpcells.push_back(cells_[i]);
280 std::swap(cells_, tmpcells);
282 colinfo_.erase(colinfo_.begin() + col);
286 bool MathGridInset::idxUp(int & idx, int & pos) const
296 bool MathGridInset::idxDown(int & idx, int & pos) const
298 if (idx >= ncols() * (nrows() - 1))
306 bool MathGridInset::idxLeft(int & idx, int & pos) const
308 // leave matrix if on the left hand edge
312 pos = cell(idx).size();
317 bool MathGridInset::idxRight(int & idx, int & pos) const
319 // leave matrix if on the right hand edge
320 if (col(idx) == ncols() - 1)
328 bool MathGridInset::idxFirst(int & idx, int & pos) const
335 idx = (nrows() - 1) * ncols();
338 idx = (nrows() / 2) * ncols();
345 bool MathGridInset::idxLast(int & idx, int & pos) const
355 idx = (nrows() / 2 + 1) * ncols() - 1;
357 pos = cell(idx).size();
362 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
367 // delete entire row if in first cell of empty row
368 if (col(idx) == 0 && nrows() > 1) {
369 bool deleterow = true;
370 for (int i = idx; i < idx + ncols(); ++i)
371 if (cell(i).size()) {
383 // undo effect of Ctrl-Tab (i.e. pull next cell)
384 //if (idx != nargs() - 1)
385 // cell(idx).swap(cell(idx + 1));
389 void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
391 // leave this unimplemented unless someone wants to have it.
393 int n = (to - from) / ncols();
394 int r = from / ncols();
397 cells_type::iterator it = cells_.begin() + from;
398 cells_.erase(it, it + n * ncols());
399 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
405 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
411 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
417 std::vector<int> MathGridInset::idxBetween(int from, int to) const
419 int r1 = std::min(row(from), row(to));
420 int r2 = std::max(row(from), row(to));
421 int c1 = std::min(col(from), col(to));
422 int c2 = std::max(col(from), col(to));
423 std::vector<int> res;
424 for (int i = r1; i <= r2; ++i)
425 for (int j = c1; j <= c2; ++j)
426 res.push_back(index(i, j));