5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
19 int const HLINESEP = 3;
21 int const VLINESEP = 3;
26 string verboseHLine(int n)
29 for (int i = 0; i < n; ++i)
37 //////////////////////////////////////////////////////////////
40 MathGridInset::RowInfo::RowInfo()
46 int MathGridInset::RowInfo::skipPixels() const
49 #warning fix this once the interface to LyXLength has improved
51 return int(crskip_.value());
56 //////////////////////////////////////////////////////////////
59 MathGridInset::ColInfo::ColInfo()
60 : align_('c'), leftline_(false), rightline_(false), lines_(0)
64 //////////////////////////////////////////////////////////////
67 MathGridInset::MathGridInset(char v, string const & h)
68 : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
76 MathGridInset::MathGridInset(col_type m, row_type n)
77 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
83 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
84 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
92 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
94 return col + ncols() * row;
98 void MathGridInset::setDefaults()
101 lyxerr << "positive number of columns expected\n";
103 lyxerr << "positive number of rows expected\n";
104 for (col_type col = 0; col < ncols(); ++col) {
105 colinfo_[col].align_ = defaultColAlign(col);
106 colinfo_[col].skip_ = defaultColSpace(col);
111 void MathGridInset::halign(string const & hh)
114 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
117 colinfo_[col].lines_++;
118 } else if (c == 'c' || c == 'l' || c == 'r') {
119 colinfo_[col].align_ = c;
121 colinfo_[col].lines_ = 0;
123 lyxerr << "unkown column separator: '" << c << "'\n";
128 col_type n = hh.size();
131 for (col_type col = 0; col < n; ++col)
132 colinfo_[col].align_ = hh[col];
137 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
140 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
141 if (*it == 'c' || *it == 'l' || *it == 'r')
147 void MathGridInset::halign(char h, col_type col)
149 colinfo_[col].align_ = h;
153 char MathGridInset::halign(col_type col) const
155 return colinfo_[col].align_;
159 string MathGridInset::halign() const
162 for (col_type col = 0; col < ncols(); ++col) {
163 res += string(colinfo_[col].lines_, '|');
164 res += colinfo_[col].align_;
166 return res + string(colinfo_[ncols()].lines_, '|');
170 void MathGridInset::valign(char c)
176 char MathGridInset::valign() const
182 MathGridInset::col_type MathGridInset::ncols() const
184 return colinfo_.size() - 1;
188 MathGridInset::row_type MathGridInset::nrows() const
190 return rowinfo_.size() - 1;
194 MathGridInset::col_type MathGridInset::col(idx_type idx) const
196 return idx % ncols();
200 MathGridInset::row_type MathGridInset::row(idx_type idx) const
202 return idx / ncols();
206 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
208 rowinfo_[row].crskip_ = crskip;
212 LyXLength MathGridInset::vcrskip(row_type row) const
214 return rowinfo_[row].crskip_;
218 void MathGridInset::metrics(MathMetricsInfo const & mi) const
220 // let the cells adjust themselves
221 MathNestInset::metrics(mi);
223 // compute absolute sizes of vertical structure
224 for (row_type row = 0; row < nrows(); ++row) {
227 for (col_type col = 0; col < ncols(); ++col) {
228 MathXArray const & c = xcell(index(row, col));
229 asc = std::max(asc, c.ascent());
230 desc = std::max(desc, c.descent());
232 rowinfo_[row].ascent_ = asc;
233 rowinfo_[row].descent_ = desc;
235 rowinfo_[0].ascent_ += HLINESEP * rowinfo_[0].lines_;
236 rowinfo_[nrows()].ascent_ = 0;
237 rowinfo_[nrows()].descent_ = 0;
239 // compute vertical offsets
240 rowinfo_[0].offset_ = 0;
241 for (row_type row = 1; row <= nrows(); ++row) {
242 rowinfo_[row].offset_ =
243 rowinfo_[row - 1].offset_ +
244 rowinfo_[row - 1].descent_ +
245 rowinfo_[row - 1].skipPixels() +
247 rowinfo_[row].lines_ * HLINESEP +
248 rowinfo_[row].ascent_;
251 // adjust vertical offset
258 h = rowinfo_[nrows() - 1].offset_;
261 h = rowinfo_[nrows() - 1].offset_ / 2;
263 for (row_type row = 0; row <= nrows(); ++row)
264 rowinfo_[row].offset_ -= h;
267 // compute absolute sizes of horizontal structure
268 for (col_type col = 0; col < ncols(); ++col) {
270 for (row_type row = 0; row < nrows(); ++row)
271 wid = std::max(wid, xcell(index(row, col)).width());
272 colinfo_[col].width_ = wid;
274 colinfo_[ncols()].width_ = 0;
276 // compute horizontal offsets
277 colinfo_[0].offset_ = BORDER;
278 for (col_type col = 1; col <= ncols(); ++col) {
279 colinfo_[col].offset_ =
280 colinfo_[col - 1].offset_ +
281 colinfo_[col - 1].width_ +
282 colinfo_[col - 1].skip_ +
284 colinfo_[col].lines_ * VLINESEP;
288 width_ = colinfo_[ncols() - 1].offset_
289 + colinfo_[ncols() - 1].width_
290 + VLINESEP * colinfo_[ncols()].lines_
293 ascent_ = - rowinfo_[0].offset_
294 + rowinfo_[0].ascent_
295 + HLINESEP * rowinfo_[0].lines_
298 descent_ = rowinfo_[nrows() - 1].offset_
299 + rowinfo_[nrows() - 1].descent_
300 + HLINESEP * rowinfo_[nrows()].lines_
305 // Increase ws_[i] for 'R' columns (except the first one)
306 for (int i = 1; i < nc_; ++i)
307 if (align_[i] == 'R')
308 ws_[i] += 10 * df_width;
309 // Increase ws_[i] for 'C' column
310 if (align_[0] == 'C')
311 if (ws_[0] < 7 * workwidth / 8)
312 ws_[0] = 7 * workwidth / 8;
316 for (cxrow = row_.begin(); cxrow; ++cxrow) {
319 for (int i = 0; i < nc_; ++i) {
321 if (cxrow->getTab(i) <= 0) {
322 cxrow->setTab(i, df_width);
330 lf = (ws_[i] - cxrow->getTab(i))/2;
334 lf = ws_[i] - cxrow->getTab(i);
337 if (cxrow == row_.begin())
339 else if (cxrow.is_last())
340 lf = ws_[i] - cxrow->getTab(i);
342 lf = (ws_[i] - cxrow->getTab(i))/2;
345 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
346 cxrow->setTab(i, lf + rg);
347 rg = ws_[i] - ww + COLSEP;
348 if (cxrow == row_.begin())
349 width += ws_[i] + COLSEP;
351 cxrow->setBaseline(cxrow->getBaseline() - ascent);
357 void MathGridInset::draw(Painter & pain, int x, int y) const
359 for (idx_type idx = 0; idx < nargs(); ++idx)
360 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
362 for (row_type row = 0; row <= nrows(); ++row)
363 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
364 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
365 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
366 pain.line(x + 1, yy, x + width_ - 1, yy);
369 for (col_type col = 0; col <= ncols(); ++col)
370 for (int i = 0; i < colinfo_[col].lines_; ++i) {
371 int xx = x + colinfo_[col].offset_
372 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
373 pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
378 string MathGridInset::eolString(row_type row) const
382 if (rowinfo_[row].crskip_.value() != 0)
383 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
385 // make sure an upcoming '[' does not break anything
386 if (row + 1 < nrows()) {
387 MathArray const & c = cell(index(row + 1, 0));
388 if (c.size() && c.front()->getChar() == '[')
392 // only add \\ if necessary
393 if (eol.empty() && row + 1 == nrows())
396 return "\\\\" + eol + '\n';
400 string MathGridInset::eocString(col_type col) const
402 if (col + 1 == ncols())
408 void MathGridInset::addRow(row_type row)
410 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
411 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
415 void MathGridInset::appendRow()
417 rowinfo_.push_back(RowInfo());
418 //cells_.insert(cells_.end(), ncols(), MathXArray());
419 for (col_type col = 0; col < ncols(); ++col)
420 cells_.push_back(cells_type::value_type());
424 void MathGridInset::delRow(row_type row)
429 cells_type::iterator it = cells_.begin() + row * ncols();
430 cells_.erase(it, it + ncols());
432 rowinfo_.erase(rowinfo_.begin() + row);
436 void MathGridInset::addCol(col_type newcol)
438 const col_type nc = ncols();
439 const row_type nr = nrows();
440 cells_type new_cells((nc + 1) * nr);
442 for (row_type row = 0; row < nr; ++row)
443 for (col_type col = 0; col < nc; ++col)
444 new_cells[row * (nc + 1) + col + (col > newcol)]
445 = cells_[row * nc + col];
446 std::swap(cells_, new_cells);
449 inf.skip_ = defaultColSpace(newcol);
450 inf.align_ = defaultColAlign(newcol);
451 colinfo_.insert(colinfo_.begin() + newcol, inf);
455 void MathGridInset::delCol(col_type col)
461 for (col_type i = 0; i < nargs(); ++i)
462 if (i % ncols() != col)
463 tmpcells.push_back(cells_[i]);
464 std::swap(cells_, tmpcells);
466 colinfo_.erase(colinfo_.begin() + col);
470 int MathGridInset::cellXOffset(idx_type idx) const
472 col_type c = col(idx);
473 int x = colinfo_[c].offset_;
474 char align = colinfo_[c].align_;
475 if (align == 'r' || align == 'R')
476 x += colinfo_[c].width_ - xcell(idx).width();
477 if (align == 'c' || align == 'C')
478 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
483 int MathGridInset::cellYOffset(idx_type idx) const
485 return rowinfo_[row(idx)].offset_;
489 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
495 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
497 pos = xcell(idx).x2pos(x - cellXOffset(idx));
503 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
507 if (idx >= ncols() * (nrows() - 1))
509 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
511 pos = xcell(idx).x2pos(x - cellXOffset(idx));
517 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
519 // leave matrix if on the left hand edge
523 pos = cell(idx).size();
528 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
530 // leave matrix if on the right hand edge
531 if (col(idx) + 1 == ncols())
539 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
546 idx = (nrows() - 1) * ncols();
549 idx = (nrows() / 2) * ncols();
556 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
566 idx = (nrows() / 2 + 1) * ncols() - 1;
568 pos = cell(idx).size();
573 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
580 idx -= idx % ncols();
593 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
595 if (pos < cell(idx).size()) {
596 pos = cell(idx).size();
599 if (col(idx) < ncols() - 1) {
600 idx = idx - idx % ncols() + ncols() - 1;
601 pos = cell(idx).size();
604 if (idx < nargs() - 1) {
606 pos = cell(idx).size();
613 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
618 // nothing to do if we are in the last row of the inset
619 if (row(idx) + 1 == nrows())
622 // try to delete entire sequence of ncols() empty cells if possible
623 for (idx_type i = idx; i < idx + ncols(); ++i)
627 // move cells if necessary
628 for (idx_type i = index(row(idx), 0); i < idx; ++i)
629 cell(i).swap(cell(i + ncols()));
636 // undo effect of Ctrl-Tab (i.e. pull next cell)
637 //if (idx + 1 != nargs())
638 // cell(idx).swap(cell(idx + 1));
642 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
644 // leave this unimplemented unless someone wants to have it.
646 int n = (to - from) / ncols();
647 int r = from / ncols();
650 cells_type::iterator it = cells_.begin() + from;
651 cells_.erase(it, it + n * ncols());
652 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
658 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
660 return rowinfo_[row];
664 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
666 return rowinfo_[row];
670 std::vector<MathInset::idx_type>
671 MathGridInset::idxBetween(idx_type from, idx_type to) const
673 row_type r1 = std::min(row(from), row(to));
674 row_type r2 = std::max(row(from), row(to));
675 col_type c1 = std::min(col(from), col(to));
676 col_type c2 = std::max(col(from), col(to));
677 std::vector<idx_type> res;
678 for (row_type i = r1; i <= r2; ++i)
679 for (col_type j = c1; j <= c2; ++j)
680 res.push_back(index(i, j));
686 void MathGridInset::normalize(NormalStream & os) const
689 for (row_type row = 0; row < nrows(); ++row) {
691 for (col_type col = 0; col < ncols(); ++col)
692 os << "[cell " << cell(index(row, col)) << ']';
699 void MathGridInset::mathmlize(MathMLStream & os) const
701 os << MTag("mtable");
702 for (row_type row = 0; row < nrows(); ++row) {
704 for (col_type col = 0; col < ncols(); ++col)
705 os << cell(index(row, col));
708 os << ETag("mtable");
712 void MathGridInset::write(WriteStream & os) const
714 for (row_type row = 0; row < nrows(); ++row) {
715 os << verboseHLine(rowinfo_[row].lines_).c_str();
716 for (col_type col = 0; col < ncols(); ++col)
717 os << cell(index(row, col)) << eocString(col).c_str();
718 os << eolString(row).c_str();
720 string s = verboseHLine(rowinfo_[nrows()].lines_);
722 os << "\\\\" << s.c_str();