5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
7 #include "math_streamstr.h"
9 #include "frontends/Painter.h"
21 string verboseHLine(int n)
24 for (int i = 0; i < n; ++i)
31 //////////////////////////////////////////////////////////////
34 MathGridInset::CellInfo::CellInfo()
41 //////////////////////////////////////////////////////////////
44 MathGridInset::RowInfo::RowInfo()
50 int MathGridInset::RowInfo::skipPixels() const
52 return crskip_.inBP();
57 //////////////////////////////////////////////////////////////
60 MathGridInset::ColInfo::ColInfo()
61 : align_('c'), leftline_(false), rightline_(false), lines_(0)
65 //////////////////////////////////////////////////////////////
68 MathGridInset::MathGridInset(char v, string const & h)
69 : MathNestInset(guessColumns(h)),
71 colinfo_(guessColumns(h) + 1),
72 cellinfo_(1 * guessColumns(h))
80 MathGridInset::MathGridInset()
91 MathGridInset::MathGridInset(col_type m, row_type n)
92 : MathNestInset(m * n),
102 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
103 : MathNestInset(m * n),
115 MathInset * MathGridInset::clone() const
117 return new MathGridInset(*this);
121 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
123 return col + ncols() * row;
127 void MathGridInset::setDefaults()
130 lyxerr << "positive number of columns expected\n";
132 // lyxerr << "positive number of rows expected\n";
133 for (col_type col = 0; col < ncols(); ++col) {
134 colinfo_[col].align_ = defaultColAlign(col);
135 colinfo_[col].skip_ = defaultColSpace(col);
140 void MathGridInset::halign(string const & hh)
143 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
146 colinfo_[col].lines_++;
147 } else if (c == 'c' || c == 'l' || c == 'r') {
148 colinfo_[col].align_ = c;
150 colinfo_[col].lines_ = 0;
152 lyxerr << "unkown column separator: '" << c << "'\n";
157 col_type n = hh.size();
160 for (col_type col = 0; col < n; ++col)
161 colinfo_[col].align_ = hh[col];
166 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
169 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
170 if (*it == 'c' || *it == 'l' || *it == 'r')
176 void MathGridInset::halign(char h, col_type col)
178 colinfo_[col].align_ = h;
182 char MathGridInset::halign(col_type col) const
184 return colinfo_[col].align_;
188 string MathGridInset::halign() const
191 for (col_type col = 0; col < ncols(); ++col) {
192 res += string(colinfo_[col].lines_, '|');
193 res += colinfo_[col].align_;
195 return res + string(colinfo_[ncols()].lines_, '|');
199 void MathGridInset::valign(char c)
205 char MathGridInset::valign() const
211 MathGridInset::col_type MathGridInset::ncols() const
213 return colinfo_.size() - 1;
217 MathGridInset::row_type MathGridInset::nrows() const
219 return rowinfo_.size() - 1;
223 MathGridInset::col_type MathGridInset::col(idx_type idx) const
225 return idx % ncols();
229 MathGridInset::row_type MathGridInset::row(idx_type idx) const
231 return idx / ncols();
235 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
237 rowinfo_[row].crskip_ = crskip;
241 LyXLength MathGridInset::vcrskip(row_type row) const
243 return rowinfo_[row].crskip_;
247 void MathGridInset::metrics(MathMetricsInfo & mi) const
249 // let the cells adjust themselves
250 MathNestInset::metrics(mi);
252 // compute absolute sizes of vertical structure
253 for (row_type row = 0; row < nrows(); ++row) {
256 for (col_type col = 0; col < ncols(); ++col) {
257 MathArray const & c = cell(index(row, col));
258 asc = max(asc, c.ascent());
259 desc = max(desc, c.descent());
261 rowinfo_[row].ascent_ = asc;
262 rowinfo_[row].descent_ = desc;
264 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
265 rowinfo_[nrows()].ascent_ = 0;
266 rowinfo_[nrows()].descent_ = 0;
268 // compute vertical offsets
269 rowinfo_[0].offset_ = 0;
270 for (row_type row = 1; row <= nrows(); ++row) {
271 rowinfo_[row].offset_ =
272 rowinfo_[row - 1].offset_ +
273 rowinfo_[row - 1].descent_ +
274 rowinfo_[row - 1].skipPixels() +
276 rowinfo_[row].lines_ * hlinesep() +
277 rowinfo_[row].ascent_;
280 // adjust vertical offset
287 h = rowinfo_[nrows() - 1].offset_;
290 h = rowinfo_[nrows() - 1].offset_ / 2;
292 for (row_type row = 0; row <= nrows(); ++row)
293 rowinfo_[row].offset_ -= h;
296 // compute absolute sizes of horizontal structure
297 for (col_type col = 0; col < ncols(); ++col) {
299 for (row_type row = 0; row < nrows(); ++row)
300 wid = max(wid, cell(index(row, col)).width());
301 colinfo_[col].width_ = wid;
303 colinfo_[ncols()].width_ = 0;
305 // compute horizontal offsets
306 colinfo_[0].offset_ = border();
307 for (col_type col = 1; col <= ncols(); ++col) {
308 colinfo_[col].offset_ =
309 colinfo_[col - 1].offset_ +
310 colinfo_[col - 1].width_ +
311 colinfo_[col - 1].skip_ +
313 colinfo_[col].lines_ * vlinesep();
317 dim_.w = colinfo_[ncols() - 1].offset_
318 + colinfo_[ncols() - 1].width_
319 + vlinesep() * colinfo_[ncols()].lines_
322 dim_.a = - rowinfo_[0].offset_
323 + rowinfo_[0].ascent_
324 + hlinesep() * rowinfo_[0].lines_
327 dim_.d = rowinfo_[nrows() - 1].offset_
328 + rowinfo_[nrows() - 1].descent_
329 + hlinesep() * rowinfo_[nrows()].lines_
334 // Increase ws_[i] for 'R' columns (except the first one)
335 for (int i = 1; i < nc_; ++i)
336 if (align_[i] == 'R')
337 ws_[i] += 10 * df_width;
338 // Increase ws_[i] for 'C' column
339 if (align_[0] == 'C')
340 if (ws_[0] < 7 * workwidth / 8)
341 ws_[0] = 7 * workwidth / 8;
345 for (cxrow = row_.begin(); cxrow; ++cxrow) {
348 for (int i = 0; i < nc_; ++i) {
350 if (cxrow->getTab(i) <= 0) {
351 cxrow->setTab(i, df_width);
359 lf = (ws_[i] - cxrow->getTab(i))/2;
363 lf = ws_[i] - cxrow->getTab(i);
366 if (cxrow == row_.begin())
368 else if (cxrow.is_last())
369 lf = ws_[i] - cxrow->getTab(i);
371 lf = (ws_[i] - cxrow->getTab(i))/2;
374 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
375 cxrow->setTab(i, lf + rg);
376 rg = ws_[i] - ww + colsep();
377 if (cxrow == row_.begin())
378 width += ws_[i] + colsep();
380 cxrow->setBaseline(cxrow->getBaseline() - ascent);
386 void MathGridInset::draw(MathPainterInfo & pi, int x, int y) const
388 for (idx_type idx = 0; idx < nargs(); ++idx)
389 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
391 for (row_type row = 0; row <= nrows(); ++row)
392 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
393 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
394 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
395 pi.pain.line(x + 1, yy, x + width() - 1, yy);
398 for (col_type col = 0; col <= ncols(); ++col)
399 for (int i = 0; i < colinfo_[col].lines_; ++i) {
400 int xx = x + colinfo_[col].offset_
401 - i * vlinesep() - vlinesep()/2 - colsep()/2;
402 pi.pain.line(xx, y - ascent() + 1, xx, y + descent() - 1);
407 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
409 // let the cells adjust themselves
410 //MathNestInset::metrics(mi);
411 for (idx_type i = 0; i < nargs(); ++i)
412 cell(i).metricsT(mi);
414 // compute absolute sizes of vertical structure
415 for (row_type row = 0; row < nrows(); ++row) {
418 for (col_type col = 0; col < ncols(); ++col) {
419 MathArray const & c = cell(index(row, col));
420 asc = max(asc, c.ascent());
421 desc = max(desc, c.descent());
423 rowinfo_[row].ascent_ = asc;
424 rowinfo_[row].descent_ = desc;
426 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
427 rowinfo_[nrows()].ascent_ = 0;
428 rowinfo_[nrows()].descent_ = 0;
430 // compute vertical offsets
431 rowinfo_[0].offset_ = 0;
432 for (row_type row = 1; row <= nrows(); ++row) {
433 rowinfo_[row].offset_ =
434 rowinfo_[row - 1].offset_ +
435 rowinfo_[row - 1].descent_ +
436 //rowinfo_[row - 1].skipPixels() +
438 //rowinfo_[row].lines_ * hlinesep() +
439 rowinfo_[row].ascent_;
442 // adjust vertical offset
449 h = rowinfo_[nrows() - 1].offset_;
452 h = rowinfo_[nrows() - 1].offset_ / 2;
454 for (row_type row = 0; row <= nrows(); ++row)
455 rowinfo_[row].offset_ -= h;
458 // compute absolute sizes of horizontal structure
459 for (col_type col = 0; col < ncols(); ++col) {
461 for (row_type row = 0; row < nrows(); ++row)
462 wid = max(wid, cell(index(row, col)).width());
463 colinfo_[col].width_ = wid;
465 colinfo_[ncols()].width_ = 0;
467 // compute horizontal offsets
468 colinfo_[0].offset_ = border();
469 for (col_type col = 1; col <= ncols(); ++col) {
470 colinfo_[col].offset_ =
471 colinfo_[col - 1].offset_ +
472 colinfo_[col - 1].width_ +
473 colinfo_[col - 1].skip_ +
475 //colinfo_[col].lines_ * vlinesep();
479 dim_.w = colinfo_[ncols() - 1].offset_
480 + colinfo_[ncols() - 1].width_
481 //+ vlinesep() * colinfo_[ncols()].lines_
484 dim_.a = -rowinfo_[0].offset_
485 + rowinfo_[0].ascent_
486 //+ hlinesep() * rowinfo_[0].lines_
489 dim_.d = rowinfo_[nrows() - 1].offset_
490 + rowinfo_[nrows() - 1].descent_
491 //+ hlinesep() * rowinfo_[nrows()].lines_
497 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
499 for (idx_type idx = 0; idx < nargs(); ++idx)
500 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
504 string MathGridInset::eolString(row_type row, bool fragile) const
508 if (!rowinfo_[row].crskip_.zero())
509 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
511 // make sure an upcoming '[' does not break anything
512 if (row + 1 < nrows()) {
513 MathArray const & c = cell(index(row + 1, 0));
514 if (c.size() && c.front()->getChar() == '[')
519 // only add \\ if necessary
520 if (eol.empty() && row + 1 == nrows())
523 return (fragile ? "\\protect\\\\" : "\\\\") + eol + '\n';
527 string MathGridInset::eocString(col_type col) const
529 if (col + 1 == ncols())
535 void MathGridInset::addRow(row_type row)
537 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
539 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
541 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
545 void MathGridInset::appendRow()
547 rowinfo_.push_back(RowInfo());
548 //cells_.insert(cells_.end(), ncols(), MathArray());
549 for (col_type col = 0; col < ncols(); ++col) {
550 cells_.push_back(cells_type::value_type());
551 cellinfo_.push_back(CellInfo());
556 void MathGridInset::delRow(row_type row)
561 cells_type::iterator it = cells_.begin() + row * ncols();
562 cells_.erase(it, it + ncols());
564 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
565 cellinfo_.erase(jt, jt + ncols());
567 rowinfo_.erase(rowinfo_.begin() + row);
571 void MathGridInset::addCol(col_type newcol)
573 const col_type nc = ncols();
574 const row_type nr = nrows();
575 cells_type new_cells((nc + 1) * nr);
576 vector<CellInfo> new_cellinfo((nc + 1) * nr);
578 for (row_type row = 0; row < nr; ++row)
579 for (col_type col = 0; col < nc; ++col) {
580 new_cells[row * (nc + 1) + col + (col > newcol)]
581 = cells_[row * nc + col];
582 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
583 = cellinfo_[row * nc + col];
585 swap(cells_, new_cells);
586 swap(cellinfo_, new_cellinfo);
589 inf.skip_ = defaultColSpace(newcol);
590 inf.align_ = defaultColAlign(newcol);
591 colinfo_.insert(colinfo_.begin() + newcol, inf);
595 void MathGridInset::delCol(col_type col)
601 vector<CellInfo> tmpcellinfo;
602 for (col_type i = 0; i < nargs(); ++i)
603 if (i % ncols() != col) {
604 tmpcells.push_back(cells_[i]);
605 tmpcellinfo.push_back(cellinfo_[i]);
607 swap(cells_, tmpcells);
608 swap(cellinfo_, tmpcellinfo);
610 colinfo_.erase(colinfo_.begin() + col);
614 int MathGridInset::cellXOffset(idx_type idx) const
616 col_type c = col(idx);
617 int x = colinfo_[c].offset_;
618 char align = colinfo_[c].align_;
619 if (align == 'r' || align == 'R')
620 x += colinfo_[c].width_ - cell(idx).width();
621 if (align == 'c' || align == 'C')
622 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
627 int MathGridInset::cellYOffset(idx_type idx) const
629 return rowinfo_[row(idx)].offset_;
633 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
640 pos = cell(idx).x2pos(targetx - cell(idx).xo());
643 if (idx >= ncols() * (nrows() - 1))
646 pos = cell(idx).x2pos(targetx - cell(idx).xo());
652 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
654 // leave matrix if on the left hand edge
658 pos = cell(idx).size();
663 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
665 // leave matrix if on the right hand edge
666 if (col(idx) + 1 == ncols())
674 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
681 idx = (nrows() - 1) * ncols();
684 idx = ((nrows() - 1) / 2) * ncols();
691 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
701 idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
703 pos = cell(idx).size();
708 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
715 idx -= idx % ncols();
728 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
730 if (pos < cell(idx).size()) {
731 pos = cell(idx).size();
734 if (col(idx) < ncols() - 1) {
735 idx = idx - idx % ncols() + ncols() - 1;
736 pos = cell(idx).size();
739 if (idx < nargs() - 1) {
741 pos = cell(idx).size();
748 bool MathGridInset::idxDelete(idx_type & idx)
750 // nothing to do if we have just one row
754 // nothing to do if we are in the middle of the last row of the inset
755 if (idx + ncols() > nargs())
758 // try to delete entire sequence of ncols() empty cells if possible
759 for (idx_type i = idx; i < idx + ncols(); ++i)
763 // move cells if necessary
764 for (idx_type i = index(row(idx), 0); i < idx; ++i)
765 std::swap(cell(i), cell(i + ncols()));
772 // undo effect of Ctrl-Tab (i.e. pull next cell)
773 //if (idx + 1 != nargs())
774 // cell(idx).swap(cell(idx + 1));
776 // we handled the event..
781 // reimplement old behaviour when pressing Delete in the last position
783 void MathGridInset::idxGlue(idx_type idx)
785 col_type c = col(idx);
786 if (c + 1 == ncols()) {
787 if (row(idx) + 1 != nrows()) {
788 for (col_type cc = 0; cc < ncols(); ++cc)
789 cell(idx).append(cell(idx + cc + 1));
790 delRow(row(idx) + 1);
793 cell(idx).append(cell(idx + 1));
794 for (col_type cc = c + 2; cc < ncols(); ++cc)
795 cell(idx - c + cc - 1) = cell(idx - c + cc);
796 cell(idx - c + ncols() - 1).clear();
801 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
803 return rowinfo_[row];
807 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
809 return rowinfo_[row];
813 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
815 row_type const ri = row(idx);
816 row_type const r1 = min(row(from), row(to));
817 row_type const r2 = max(row(from), row(to));
818 col_type const ci = col(idx);
819 col_type const c1 = min(col(from), col(to));
820 col_type const c2 = max(col(from), col(to));
821 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
826 void MathGridInset::normalize(NormalStream & os) const
829 for (row_type row = 0; row < nrows(); ++row) {
831 for (col_type col = 0; col < ncols(); ++col)
832 os << "[cell " << cell(index(row, col)) << ']';
839 void MathGridInset::mathmlize(MathMLStream & os) const
841 os << MTag("mtable");
842 for (row_type row = 0; row < nrows(); ++row) {
844 for (col_type col = 0; col < ncols(); ++col)
845 os << cell(index(row, col));
848 os << ETag("mtable");
852 void MathGridInset::write(WriteStream & os) const
854 for (row_type row = 0; row < nrows(); ++row) {
855 os << verboseHLine(rowinfo_[row].lines_);
856 for (col_type col = 0; col < ncols(); ++col)
857 os << cell(index(row, col)) << eocString(col);
858 os << eolString(row, os.fragile());
860 string const s = verboseHLine(rowinfo_[nrows()].lines_);
861 if (!s.empty() && s != " ") {
869 int MathGridInset::colsep() const
875 int MathGridInset::rowsep() const
881 int MathGridInset::hlinesep() const
887 int MathGridInset::vlinesep() const
893 int MathGridInset::border() const