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
53 #warning fix this once the interface to LyXLength has improved
55 return int(crskip_.value());
60 //////////////////////////////////////////////////////////////
63 MathGridInset::ColInfo::ColInfo()
64 : align_('c'), leftline_(false), rightline_(false), lines_(0)
68 //////////////////////////////////////////////////////////////
71 MathGridInset::MathGridInset(char v, string const & h)
72 : MathNestInset(guessColumns(h)),
74 colinfo_(guessColumns(h) + 1),
75 cellinfo_(1 * guessColumns(h))
83 MathGridInset::MathGridInset()
94 MathGridInset::MathGridInset(col_type m, row_type n)
95 : MathNestInset(m * n),
105 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
106 : MathNestInset(m * n),
118 MathInset * MathGridInset::clone() const
120 return new MathGridInset(*this);
124 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
126 return col + ncols() * row;
130 void MathGridInset::setDefaults()
133 lyxerr << "positive number of columns expected\n";
135 lyxerr << "positive number of rows expected\n";
136 for (col_type col = 0; col < ncols(); ++col) {
137 colinfo_[col].align_ = defaultColAlign(col);
138 colinfo_[col].skip_ = defaultColSpace(col);
143 void MathGridInset::halign(string const & hh)
146 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 << "unkown 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')
179 void MathGridInset::halign(char h, col_type col)
181 colinfo_[col].align_ = h;
185 char MathGridInset::halign(col_type col) const
187 return colinfo_[col].align_;
191 string MathGridInset::halign() const
194 for (col_type col = 0; col < ncols(); ++col) {
195 res += string(colinfo_[col].lines_, '|');
196 res += colinfo_[col].align_;
198 return res + string(colinfo_[ncols()].lines_, '|');
202 void MathGridInset::valign(char c)
208 char MathGridInset::valign() const
214 MathGridInset::col_type MathGridInset::ncols() const
216 return colinfo_.size() - 1;
220 MathGridInset::row_type MathGridInset::nrows() const
222 return rowinfo_.size() - 1;
226 MathGridInset::col_type MathGridInset::col(idx_type idx) const
228 return idx % ncols();
232 MathGridInset::row_type MathGridInset::row(idx_type idx) const
234 return idx / ncols();
238 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
240 rowinfo_[row].crskip_ = crskip;
244 LyXLength MathGridInset::vcrskip(row_type row) const
246 return rowinfo_[row].crskip_;
250 void MathGridInset::metrics(MathMetricsInfo & mi) const
252 // let the cells adjust themselves
253 MathNestInset::metrics(mi);
255 // compute absolute sizes of vertical structure
256 for (row_type row = 0; row < nrows(); ++row) {
259 for (col_type col = 0; col < ncols(); ++col) {
260 MathXArray const & c = xcell(index(row, col));
261 asc = max(asc, c.ascent());
262 desc = max(desc, c.descent());
264 rowinfo_[row].ascent_ = asc;
265 rowinfo_[row].descent_ = desc;
267 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
268 rowinfo_[nrows()].ascent_ = 0;
269 rowinfo_[nrows()].descent_ = 0;
271 // compute vertical offsets
272 rowinfo_[0].offset_ = 0;
273 for (row_type row = 1; row <= nrows(); ++row) {
274 rowinfo_[row].offset_ =
275 rowinfo_[row - 1].offset_ +
276 rowinfo_[row - 1].descent_ +
277 rowinfo_[row - 1].skipPixels() +
279 rowinfo_[row].lines_ * hlinesep() +
280 rowinfo_[row].ascent_;
283 // adjust vertical offset
290 h = rowinfo_[nrows() - 1].offset_;
293 h = rowinfo_[nrows() - 1].offset_ / 2;
295 for (row_type row = 0; row <= nrows(); ++row)
296 rowinfo_[row].offset_ -= h;
299 // compute absolute sizes of horizontal structure
300 for (col_type col = 0; col < ncols(); ++col) {
302 for (row_type row = 0; row < nrows(); ++row)
303 wid = max(wid, xcell(index(row, col)).width());
304 colinfo_[col].width_ = wid;
306 colinfo_[ncols()].width_ = 0;
308 // compute horizontal offsets
309 colinfo_[0].offset_ = border();
310 for (col_type col = 1; col <= ncols(); ++col) {
311 colinfo_[col].offset_ =
312 colinfo_[col - 1].offset_ +
313 colinfo_[col - 1].width_ +
314 colinfo_[col - 1].skip_ +
316 colinfo_[col].lines_ * vlinesep();
320 width_ = colinfo_[ncols() - 1].offset_
321 + colinfo_[ncols() - 1].width_
322 + vlinesep() * colinfo_[ncols()].lines_
325 ascent_ = - rowinfo_[0].offset_
326 + rowinfo_[0].ascent_
327 + hlinesep() * rowinfo_[0].lines_
330 descent_ = rowinfo_[nrows() - 1].offset_
331 + rowinfo_[nrows() - 1].descent_
332 + hlinesep() * rowinfo_[nrows()].lines_
337 // Increase ws_[i] for 'R' columns (except the first one)
338 for (int i = 1; i < nc_; ++i)
339 if (align_[i] == 'R')
340 ws_[i] += 10 * df_width;
341 // Increase ws_[i] for 'C' column
342 if (align_[0] == 'C')
343 if (ws_[0] < 7 * workwidth / 8)
344 ws_[0] = 7 * workwidth / 8;
348 for (cxrow = row_.begin(); cxrow; ++cxrow) {
351 for (int i = 0; i < nc_; ++i) {
353 if (cxrow->getTab(i) <= 0) {
354 cxrow->setTab(i, df_width);
362 lf = (ws_[i] - cxrow->getTab(i))/2;
366 lf = ws_[i] - cxrow->getTab(i);
369 if (cxrow == row_.begin())
371 else if (cxrow.is_last())
372 lf = ws_[i] - cxrow->getTab(i);
374 lf = (ws_[i] - cxrow->getTab(i))/2;
377 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
378 cxrow->setTab(i, lf + rg);
379 rg = ws_[i] - ww + colsep();
380 if (cxrow == row_.begin())
381 width += ws_[i] + colsep();
383 cxrow->setBaseline(cxrow->getBaseline() - ascent);
389 void MathGridInset::draw(MathPainterInfo & pi, int x, int y) const
391 for (idx_type idx = 0; idx < nargs(); ++idx)
392 xcell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
394 for (row_type row = 0; row <= nrows(); ++row)
395 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
396 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
397 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
398 pi.pain.line(x + 1, yy, x + width_ - 1, yy);
401 for (col_type col = 0; col <= ncols(); ++col)
402 for (int i = 0; i < colinfo_[col].lines_; ++i) {
403 int xx = x + colinfo_[col].offset_
404 - i * vlinesep() - vlinesep()/2 - colsep()/2;
405 pi.pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
410 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
412 // let the cells adjust themselves
413 //MathNestInset::metrics(mi);
414 for (idx_type i = 0; i < nargs(); ++i)
415 xcell(i).metricsT(mi);
417 // compute absolute sizes of vertical structure
418 for (row_type row = 0; row < nrows(); ++row) {
421 for (col_type col = 0; col < ncols(); ++col) {
422 MathXArray const & c = xcell(index(row, col));
423 asc = max(asc, c.ascent());
424 desc = max(desc, c.descent());
426 rowinfo_[row].ascent_ = asc;
427 rowinfo_[row].descent_ = desc;
429 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
430 rowinfo_[nrows()].ascent_ = 0;
431 rowinfo_[nrows()].descent_ = 0;
433 // compute vertical offsets
434 rowinfo_[0].offset_ = 0;
435 for (row_type row = 1; row <= nrows(); ++row) {
436 rowinfo_[row].offset_ =
437 rowinfo_[row - 1].offset_ +
438 rowinfo_[row - 1].descent_ +
439 //rowinfo_[row - 1].skipPixels() +
441 //rowinfo_[row].lines_ * hlinesep() +
442 rowinfo_[row].ascent_;
445 // adjust vertical offset
452 h = rowinfo_[nrows() - 1].offset_;
455 h = rowinfo_[nrows() - 1].offset_ / 2;
457 for (row_type row = 0; row <= nrows(); ++row)
458 rowinfo_[row].offset_ -= h;
461 // compute absolute sizes of horizontal structure
462 for (col_type col = 0; col < ncols(); ++col) {
464 for (row_type row = 0; row < nrows(); ++row)
465 wid = max(wid, xcell(index(row, col)).width());
466 colinfo_[col].width_ = wid;
468 colinfo_[ncols()].width_ = 0;
470 // compute horizontal offsets
471 colinfo_[0].offset_ = border();
472 for (col_type col = 1; col <= ncols(); ++col) {
473 colinfo_[col].offset_ =
474 colinfo_[col - 1].offset_ +
475 colinfo_[col - 1].width_ +
476 colinfo_[col - 1].skip_ +
478 //colinfo_[col].lines_ * vlinesep();
482 width_ = colinfo_[ncols() - 1].offset_
483 + colinfo_[ncols() - 1].width_
484 //+ vlinesep() * colinfo_[ncols()].lines_
487 ascent_ = - rowinfo_[0].offset_
488 + rowinfo_[0].ascent_
489 //+ hlinesep() * rowinfo_[0].lines_
492 descent_ = rowinfo_[nrows() - 1].offset_
493 + rowinfo_[nrows() - 1].descent_
494 //+ hlinesep() * rowinfo_[nrows()].lines_
500 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
502 for (idx_type idx = 0; idx < nargs(); ++idx)
503 xcell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
507 string MathGridInset::eolString(row_type row) const
511 if (!rowinfo_[row].crskip_.zero())
512 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
514 // make sure an upcoming '[' does not break anything
515 if (row + 1 < nrows()) {
516 MathArray const & c = cell(index(row + 1, 0));
517 if (c.size() && c.front()->getChar() == '[')
522 // only add \\ if necessary
523 if (eol.empty() && row + 1 == nrows())
526 return "\\\\" + eol + '\n';
530 string MathGridInset::eocString(col_type col) const
532 if (col + 1 == ncols())
538 void MathGridInset::addRow(row_type row)
540 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
542 (cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
544 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
548 void MathGridInset::appendRow()
550 rowinfo_.push_back(RowInfo());
551 //cells_.insert(cells_.end(), ncols(), MathXArray());
552 for (col_type col = 0; col < ncols(); ++col) {
553 cells_.push_back(cells_type::value_type());
554 cellinfo_.push_back(CellInfo());
559 void MathGridInset::delRow(row_type row)
564 cells_type::iterator it = cells_.begin() + row * ncols();
565 cells_.erase(it, it + ncols());
567 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
568 cellinfo_.erase(jt, jt + ncols());
570 rowinfo_.erase(rowinfo_.begin() + row);
574 void MathGridInset::addCol(col_type newcol)
576 const col_type nc = ncols();
577 const row_type nr = nrows();
578 cells_type new_cells((nc + 1) * nr);
579 vector<CellInfo> new_cellinfo((nc + 1) * nr);
581 for (row_type row = 0; row < nr; ++row)
582 for (col_type col = 0; col < nc; ++col) {
583 new_cells[row * (nc + 1) + col + (col > newcol)]
584 = cells_[row * nc + col];
585 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
586 = cellinfo_[row * nc + col];
588 swap(cells_, new_cells);
589 swap(cellinfo_, new_cellinfo);
592 inf.skip_ = defaultColSpace(newcol);
593 inf.align_ = defaultColAlign(newcol);
594 colinfo_.insert(colinfo_.begin() + newcol, inf);
598 void MathGridInset::delCol(col_type col)
604 vector<CellInfo> tmpcellinfo;
605 for (col_type i = 0; i < nargs(); ++i)
606 if (i % ncols() != col) {
607 tmpcells.push_back(cells_[i]);
608 tmpcellinfo.push_back(cellinfo_[i]);
610 swap(cells_, tmpcells);
611 swap(cellinfo_, tmpcellinfo);
613 colinfo_.erase(colinfo_.begin() + col);
617 int MathGridInset::cellXOffset(idx_type idx) const
619 col_type c = col(idx);
620 int x = colinfo_[c].offset_;
621 char align = colinfo_[c].align_;
622 if (align == 'r' || align == 'R')
623 x += colinfo_[c].width_ - xcell(idx).width();
624 if (align == 'c' || align == 'C')
625 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
630 int MathGridInset::cellYOffset(idx_type idx) const
632 return rowinfo_[row(idx)].offset_;
636 bool MathGridInset::idxUpDown(idx_type & idx, bool up) const
644 if (idx >= ncols() * (nrows() - 1))
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 cell(i).swap(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).push_back(cell(idx + cc + 1));
790 delRow(row(idx) + 1);
793 cell(idx).push_back(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).erase();
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 vector<MathInset::idx_type>
814 MathGridInset::idxBetween(idx_type from, idx_type to) const
816 row_type r1 = min(row(from), row(to));
817 row_type r2 = max(row(from), row(to));
818 col_type c1 = min(col(from), col(to));
819 col_type c2 = max(col(from), col(to));
820 vector<idx_type> res;
821 for (row_type i = r1; i <= r2; ++i)
822 for (col_type j = c1; j <= c2; ++j)
823 res.push_back(index(i, j));
829 void MathGridInset::normalize(NormalStream & os) const
832 for (row_type row = 0; row < nrows(); ++row) {
834 for (col_type col = 0; col < ncols(); ++col)
835 os << "[cell " << cell(index(row, col)) << ']';
842 void MathGridInset::mathmlize(MathMLStream & os) const
844 os << MTag("mtable");
845 for (row_type row = 0; row < nrows(); ++row) {
847 for (col_type col = 0; col < ncols(); ++col)
848 os << cell(index(row, col));
851 os << ETag("mtable");
855 void MathGridInset::write(WriteStream & os) const
857 for (row_type row = 0; row < nrows(); ++row) {
858 os << verboseHLine(rowinfo_[row].lines_);
859 for (col_type col = 0; col < ncols(); ++col)
860 os << cell(index(row, col)) << eocString(col);
861 os << eolString(row);
863 string const s = verboseHLine(rowinfo_[nrows()].lines_);
864 if (!s.empty() && s != " ") {
872 int MathGridInset::colsep() const
878 int MathGridInset::rowsep() const
884 int MathGridInset::hlinesep() const
890 int MathGridInset::vlinesep() const
896 int MathGridInset::border() const