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))
77 //lyxerr << "created grid with " << ncols() << " columns\n";
81 MathGridInset::MathGridInset()
92 MathGridInset::MathGridInset(col_type m, row_type n)
93 : MathNestInset(m * n),
103 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
104 : MathNestInset(m * n),
116 MathInset * MathGridInset::clone() const
118 return new MathGridInset(*this);
122 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
124 return col + ncols() * row;
128 void MathGridInset::setDefaults()
131 lyxerr << "positive number of columns expected\n";
133 // lyxerr << "positive number of rows expected\n";
134 for (col_type col = 0; col < ncols(); ++col) {
135 colinfo_[col].align_ = defaultColAlign(col);
136 colinfo_[col].skip_ = defaultColSpace(col);
141 void MathGridInset::halign(string const & hh)
144 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
149 colinfo_[col].lines_++;
150 } else if (c == 'c' || c == 'l' || c == 'r') {
151 colinfo_[col].align_ = c;
153 colinfo_[col].lines_ = 0;
155 lyxerr << "unknown column separator: '" << c << "'\n";
160 col_type n = hh.size();
163 for (col_type col = 0; col < n; ++col)
164 colinfo_[col].align_ = hh[col];
169 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
172 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
173 if (*it == 'c' || *it == 'l' || *it == 'r')
175 // let's have at least one column, even if we did not recognize its
183 void MathGridInset::halign(char h, col_type col)
185 colinfo_[col].align_ = h;
189 char MathGridInset::halign(col_type col) const
191 return colinfo_[col].align_;
195 string MathGridInset::halign() const
198 for (col_type col = 0; col < ncols(); ++col) {
199 res += string(colinfo_[col].lines_, '|');
200 res += colinfo_[col].align_;
202 return res + string(colinfo_[ncols()].lines_, '|');
206 void MathGridInset::valign(char c)
212 char MathGridInset::valign() const
218 MathGridInset::col_type MathGridInset::ncols() const
220 return colinfo_.size() - 1;
224 MathGridInset::row_type MathGridInset::nrows() const
226 return rowinfo_.size() - 1;
230 MathGridInset::col_type MathGridInset::col(idx_type idx) const
232 return idx % ncols();
236 MathGridInset::row_type MathGridInset::row(idx_type idx) const
238 return idx / ncols();
242 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
244 rowinfo_[row].crskip_ = crskip;
248 LyXLength MathGridInset::vcrskip(row_type row) const
250 return rowinfo_[row].crskip_;
254 void MathGridInset::metrics(MathMetricsInfo & mi) const
256 // let the cells adjust themselves
257 MathNestInset::metrics(mi);
259 // compute absolute sizes of vertical structure
260 for (row_type row = 0; row < nrows(); ++row) {
263 for (col_type col = 0; col < ncols(); ++col) {
264 MathArray const & c = cell(index(row, col));
265 asc = max(asc, c.ascent());
266 desc = max(desc, c.descent());
268 rowinfo_[row].ascent_ = asc;
269 rowinfo_[row].descent_ = desc;
271 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
272 rowinfo_[nrows()].ascent_ = 0;
273 rowinfo_[nrows()].descent_ = 0;
275 // compute vertical offsets
276 rowinfo_[0].offset_ = 0;
277 for (row_type row = 1; row <= nrows(); ++row) {
278 rowinfo_[row].offset_ =
279 rowinfo_[row - 1].offset_ +
280 rowinfo_[row - 1].descent_ +
281 rowinfo_[row - 1].skipPixels() +
283 rowinfo_[row].lines_ * hlinesep() +
284 rowinfo_[row].ascent_;
287 // adjust vertical offset
294 h = rowinfo_[nrows() - 1].offset_;
297 h = rowinfo_[nrows() - 1].offset_ / 2;
299 for (row_type row = 0; row <= nrows(); ++row)
300 rowinfo_[row].offset_ -= h;
303 // compute absolute sizes of horizontal structure
304 for (col_type col = 0; col < ncols(); ++col) {
306 for (row_type row = 0; row < nrows(); ++row)
307 wid = max(wid, cell(index(row, col)).width());
308 colinfo_[col].width_ = wid;
310 colinfo_[ncols()].width_ = 0;
312 // compute horizontal offsets
313 colinfo_[0].offset_ = border();
314 for (col_type col = 1; col <= ncols(); ++col) {
315 colinfo_[col].offset_ =
316 colinfo_[col - 1].offset_ +
317 colinfo_[col - 1].width_ +
318 colinfo_[col - 1].skip_ +
320 colinfo_[col].lines_ * vlinesep();
324 dim_.w = colinfo_[ncols() - 1].offset_
325 + colinfo_[ncols() - 1].width_
326 + vlinesep() * colinfo_[ncols()].lines_
329 dim_.a = - rowinfo_[0].offset_
330 + rowinfo_[0].ascent_
331 + hlinesep() * rowinfo_[0].lines_
334 dim_.d = rowinfo_[nrows() - 1].offset_
335 + rowinfo_[nrows() - 1].descent_
336 + hlinesep() * rowinfo_[nrows()].lines_
341 // Increase ws_[i] for 'R' columns (except the first one)
342 for (int i = 1; i < nc_; ++i)
343 if (align_[i] == 'R')
344 ws_[i] += 10 * df_width;
345 // Increase ws_[i] for 'C' column
346 if (align_[0] == 'C')
347 if (ws_[0] < 7 * workwidth / 8)
348 ws_[0] = 7 * workwidth / 8;
352 for (cxrow = row_.begin(); cxrow; ++cxrow) {
355 for (int i = 0; i < nc_; ++i) {
357 if (cxrow->getTab(i) <= 0) {
358 cxrow->setTab(i, df_width);
366 lf = (ws_[i] - cxrow->getTab(i))/2;
370 lf = ws_[i] - cxrow->getTab(i);
373 if (cxrow == row_.begin())
375 else if (cxrow.is_last())
376 lf = ws_[i] - cxrow->getTab(i);
378 lf = (ws_[i] - cxrow->getTab(i))/2;
381 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
382 cxrow->setTab(i, lf + rg);
383 rg = ws_[i] - ww + colsep();
384 if (cxrow == row_.begin())
385 width += ws_[i] + colsep();
387 cxrow->setBaseline(cxrow->getBaseline() - ascent);
393 void MathGridInset::draw(MathPainterInfo & pi, int x, int y) const
395 for (idx_type idx = 0; idx < nargs(); ++idx)
396 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
398 for (row_type row = 0; row <= nrows(); ++row)
399 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
400 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
401 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
402 pi.pain.line(x + 1, yy, x + width() - 1, yy);
405 for (col_type col = 0; col <= ncols(); ++col)
406 for (int i = 0; i < colinfo_[col].lines_; ++i) {
407 int xx = x + colinfo_[col].offset_
408 - i * vlinesep() - vlinesep()/2 - colsep()/2;
409 pi.pain.line(xx, y - ascent() + 1, xx, y + descent() - 1);
414 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
416 // let the cells adjust themselves
417 //MathNestInset::metrics(mi);
418 for (idx_type i = 0; i < nargs(); ++i)
419 cell(i).metricsT(mi);
421 // compute absolute sizes of vertical structure
422 for (row_type row = 0; row < nrows(); ++row) {
425 for (col_type col = 0; col < ncols(); ++col) {
426 MathArray const & c = cell(index(row, col));
427 asc = max(asc, c.ascent());
428 desc = max(desc, c.descent());
430 rowinfo_[row].ascent_ = asc;
431 rowinfo_[row].descent_ = desc;
433 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
434 rowinfo_[nrows()].ascent_ = 0;
435 rowinfo_[nrows()].descent_ = 0;
437 // compute vertical offsets
438 rowinfo_[0].offset_ = 0;
439 for (row_type row = 1; row <= nrows(); ++row) {
440 rowinfo_[row].offset_ =
441 rowinfo_[row - 1].offset_ +
442 rowinfo_[row - 1].descent_ +
443 //rowinfo_[row - 1].skipPixels() +
445 //rowinfo_[row].lines_ * hlinesep() +
446 rowinfo_[row].ascent_;
449 // adjust vertical offset
456 h = rowinfo_[nrows() - 1].offset_;
459 h = rowinfo_[nrows() - 1].offset_ / 2;
461 for (row_type row = 0; row <= nrows(); ++row)
462 rowinfo_[row].offset_ -= h;
465 // compute absolute sizes of horizontal structure
466 for (col_type col = 0; col < ncols(); ++col) {
468 for (row_type row = 0; row < nrows(); ++row)
469 wid = max(wid, cell(index(row, col)).width());
470 colinfo_[col].width_ = wid;
472 colinfo_[ncols()].width_ = 0;
474 // compute horizontal offsets
475 colinfo_[0].offset_ = border();
476 for (col_type col = 1; col <= ncols(); ++col) {
477 colinfo_[col].offset_ =
478 colinfo_[col - 1].offset_ +
479 colinfo_[col - 1].width_ +
480 colinfo_[col - 1].skip_ +
482 //colinfo_[col].lines_ * vlinesep();
486 dim_.w = colinfo_[ncols() - 1].offset_
487 + colinfo_[ncols() - 1].width_
488 //+ vlinesep() * colinfo_[ncols()].lines_
491 dim_.a = -rowinfo_[0].offset_
492 + rowinfo_[0].ascent_
493 //+ hlinesep() * rowinfo_[0].lines_
496 dim_.d = rowinfo_[nrows() - 1].offset_
497 + rowinfo_[nrows() - 1].descent_
498 //+ hlinesep() * rowinfo_[nrows()].lines_
504 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
506 for (idx_type idx = 0; idx < nargs(); ++idx)
507 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
511 string MathGridInset::eolString(row_type row, bool fragile) const
515 if (!rowinfo_[row].crskip_.zero())
516 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
518 // make sure an upcoming '[' does not break anything
519 if (row + 1 < nrows()) {
520 MathArray const & c = cell(index(row + 1, 0));
521 if (c.size() && c.front()->getChar() == '[')
526 // only add \\ if necessary
527 if (eol.empty() && row + 1 == nrows())
530 return (fragile ? "\\protect\\\\" : "\\\\") + eol + '\n';
534 string MathGridInset::eocString(col_type col) const
536 if (col + 1 == ncols())
542 void MathGridInset::addRow(row_type row)
544 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
546 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
548 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
552 void MathGridInset::appendRow()
554 rowinfo_.push_back(RowInfo());
555 //cells_.insert(cells_.end(), ncols(), MathArray());
556 for (col_type col = 0; col < ncols(); ++col) {
557 cells_.push_back(cells_type::value_type());
558 cellinfo_.push_back(CellInfo());
563 void MathGridInset::delRow(row_type row)
568 cells_type::iterator it = cells_.begin() + row * ncols();
569 cells_.erase(it, it + ncols());
571 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
572 cellinfo_.erase(jt, jt + ncols());
574 rowinfo_.erase(rowinfo_.begin() + row);
578 void MathGridInset::addCol(col_type newcol)
580 const col_type nc = ncols();
581 const row_type nr = nrows();
582 cells_type new_cells((nc + 1) * nr);
583 vector<CellInfo> new_cellinfo((nc + 1) * nr);
585 for (row_type row = 0; row < nr; ++row)
586 for (col_type col = 0; col < nc; ++col) {
587 new_cells[row * (nc + 1) + col + (col > newcol)]
588 = cells_[row * nc + col];
589 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
590 = cellinfo_[row * nc + col];
592 swap(cells_, new_cells);
593 swap(cellinfo_, new_cellinfo);
596 inf.skip_ = defaultColSpace(newcol);
597 inf.align_ = defaultColAlign(newcol);
598 colinfo_.insert(colinfo_.begin() + newcol, inf);
602 void MathGridInset::delCol(col_type col)
608 vector<CellInfo> tmpcellinfo;
609 for (col_type i = 0; i < nargs(); ++i)
610 if (i % ncols() != col) {
611 tmpcells.push_back(cells_[i]);
612 tmpcellinfo.push_back(cellinfo_[i]);
614 swap(cells_, tmpcells);
615 swap(cellinfo_, tmpcellinfo);
617 colinfo_.erase(colinfo_.begin() + col);
621 int MathGridInset::cellXOffset(idx_type idx) const
623 col_type c = col(idx);
624 int x = colinfo_[c].offset_;
625 char align = colinfo_[c].align_;
626 if (align == 'r' || align == 'R')
627 x += colinfo_[c].width_ - cell(idx).width();
628 if (align == 'c' || align == 'C')
629 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
634 int MathGridInset::cellYOffset(idx_type idx) const
636 return rowinfo_[row(idx)].offset_;
640 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
647 pos = cell(idx).x2pos(targetx - cell(idx).xo());
650 if (idx >= ncols() * (nrows() - 1))
653 pos = cell(idx).x2pos(targetx - cell(idx).xo());
659 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
661 // leave matrix if on the left hand edge
665 pos = cell(idx).size();
670 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
672 // leave matrix if on the right hand edge
673 if (col(idx) + 1 == ncols())
681 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
688 idx = (nrows() - 1) * ncols();
691 idx = ((nrows() - 1) / 2) * ncols();
698 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
708 idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
710 pos = cell(idx).size();
715 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
722 idx -= idx % ncols();
735 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
737 if (pos < cell(idx).size()) {
738 pos = cell(idx).size();
741 if (col(idx) < ncols() - 1) {
742 idx = idx - idx % ncols() + ncols() - 1;
743 pos = cell(idx).size();
746 if (idx < nargs() - 1) {
748 pos = cell(idx).size();
755 bool MathGridInset::idxDelete(idx_type & idx)
757 // nothing to do if we have just one row
761 // nothing to do if we are in the middle of the last row of the inset
762 if (idx + ncols() > nargs())
765 // try to delete entire sequence of ncols() empty cells if possible
766 for (idx_type i = idx; i < idx + ncols(); ++i)
770 // move cells if necessary
771 for (idx_type i = index(row(idx), 0); i < idx; ++i)
772 std::swap(cell(i), cell(i + ncols()));
779 // undo effect of Ctrl-Tab (i.e. pull next cell)
780 //if (idx + 1 != nargs())
781 // cell(idx).swap(cell(idx + 1));
783 // we handled the event..
788 // reimplement old behaviour when pressing Delete in the last position
790 void MathGridInset::idxGlue(idx_type idx)
792 col_type c = col(idx);
793 if (c + 1 == ncols()) {
794 if (row(idx) + 1 != nrows()) {
795 for (col_type cc = 0; cc < ncols(); ++cc)
796 cell(idx).append(cell(idx + cc + 1));
797 delRow(row(idx) + 1);
800 cell(idx).append(cell(idx + 1));
801 for (col_type cc = c + 2; cc < ncols(); ++cc)
802 cell(idx - c + cc - 1) = cell(idx - c + cc);
803 cell(idx - c + ncols() - 1).clear();
808 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
810 return rowinfo_[row];
814 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
816 return rowinfo_[row];
820 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
822 row_type const ri = row(idx);
823 row_type const r1 = min(row(from), row(to));
824 row_type const r2 = max(row(from), row(to));
825 col_type const ci = col(idx);
826 col_type const c1 = min(col(from), col(to));
827 col_type const c2 = max(col(from), col(to));
828 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
833 void MathGridInset::normalize(NormalStream & os) const
836 for (row_type row = 0; row < nrows(); ++row) {
838 for (col_type col = 0; col < ncols(); ++col)
839 os << "[cell " << cell(index(row, col)) << ']';
846 void MathGridInset::mathmlize(MathMLStream & os) const
848 os << MTag("mtable");
849 for (row_type row = 0; row < nrows(); ++row) {
851 for (col_type col = 0; col < ncols(); ++col)
852 os << cell(index(row, col));
855 os << ETag("mtable");
859 void MathGridInset::write(WriteStream & os) const
861 for (row_type row = 0; row < nrows(); ++row) {
862 os << verboseHLine(rowinfo_[row].lines_);
863 for (col_type col = 0; col < ncols(); ++col)
864 os << cell(index(row, col)) << eocString(col);
865 os << eolString(row, os.fragile());
867 string const s = verboseHLine(rowinfo_[nrows()].lines_);
868 if (!s.empty() && s != " ") {
876 int MathGridInset::colsep() const
882 int MathGridInset::rowsep() const
888 int MathGridInset::hlinesep() const
894 int MathGridInset::vlinesep() const
900 int MathGridInset::border() const