5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
7 #include "math_streamstr.h"
9 #include "funcrequest.h"
10 #include "frontends/Painter.h"
20 void mathed_parse_normal(MathGridInset &, string const & argument);
24 string verboseHLine(int n)
27 for (int i = 0; i < n; ++i)
34 //////////////////////////////////////////////////////////////
37 MathGridInset::CellInfo::CellInfo()
44 //////////////////////////////////////////////////////////////
47 MathGridInset::RowInfo::RowInfo()
53 int MathGridInset::RowInfo::skipPixels() const
55 return crskip_.inBP();
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))
80 //lyxerr << "created grid with " << ncols() << " columns\n";
84 MathGridInset::MathGridInset()
95 MathGridInset::MathGridInset(col_type m, row_type n)
96 : MathNestInset(m * n),
106 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
107 : MathNestInset(m * n),
119 MathInset * MathGridInset::clone() const
121 return new MathGridInset(*this);
125 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
127 return col + ncols() * row;
131 void MathGridInset::setDefaults()
134 lyxerr << "positive number of columns expected\n";
136 // lyxerr << "positive number of rows expected\n";
137 for (col_type col = 0; col < ncols(); ++col) {
138 colinfo_[col].align_ = defaultColAlign(col);
139 colinfo_[col].skip_ = defaultColSpace(col);
144 void MathGridInset::halign(string const & hh)
147 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
152 colinfo_[col].lines_++;
153 } else if (c == 'c' || c == 'l' || c == 'r') {
154 colinfo_[col].align_ = c;
156 colinfo_[col].lines_ = 0;
158 lyxerr << "unknown column separator: '" << c << "'\n";
163 col_type n = hh.size();
166 for (col_type col = 0; col < n; ++col)
167 colinfo_[col].align_ = hh[col];
172 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
175 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
176 if (*it == 'c' || *it == 'l' || *it == 'r')
178 // let's have at least one column, even if we did not recognize its
186 void MathGridInset::halign(char h, col_type col)
188 colinfo_[col].align_ = h;
192 char MathGridInset::halign(col_type col) const
194 return colinfo_[col].align_;
198 string MathGridInset::halign() const
201 for (col_type col = 0; col < ncols(); ++col) {
202 res += string(colinfo_[col].lines_, '|');
203 res += colinfo_[col].align_;
205 return res + string(colinfo_[ncols()].lines_, '|');
209 void MathGridInset::valign(char c)
215 char MathGridInset::valign() const
221 MathGridInset::col_type MathGridInset::ncols() const
223 return colinfo_.size() - 1;
227 MathGridInset::row_type MathGridInset::nrows() const
229 return rowinfo_.size() - 1;
233 MathGridInset::col_type MathGridInset::col(idx_type idx) const
235 return idx % ncols();
239 MathGridInset::row_type MathGridInset::row(idx_type idx) const
241 return idx / ncols();
245 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
247 rowinfo_[row].crskip_ = crskip;
251 LyXLength MathGridInset::vcrskip(row_type row) const
253 return rowinfo_[row].crskip_;
257 void MathGridInset::metrics(MathMetricsInfo & mi) const
259 // let the cells adjust themselves
260 MathNestInset::metrics(mi);
262 // compute absolute sizes of vertical structure
263 for (row_type row = 0; row < nrows(); ++row) {
266 for (col_type col = 0; col < ncols(); ++col) {
267 MathArray const & c = cell(index(row, col));
268 asc = max(asc, c.ascent());
269 desc = max(desc, c.descent());
271 rowinfo_[row].ascent_ = asc;
272 rowinfo_[row].descent_ = desc;
274 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
275 rowinfo_[nrows()].ascent_ = 0;
276 rowinfo_[nrows()].descent_ = 0;
278 // compute vertical offsets
279 rowinfo_[0].offset_ = 0;
280 for (row_type row = 1; row <= nrows(); ++row) {
281 rowinfo_[row].offset_ =
282 rowinfo_[row - 1].offset_ +
283 rowinfo_[row - 1].descent_ +
284 rowinfo_[row - 1].skipPixels() +
286 rowinfo_[row].lines_ * hlinesep() +
287 rowinfo_[row].ascent_;
290 // adjust vertical offset
297 h = rowinfo_[nrows() - 1].offset_;
300 h = rowinfo_[nrows() - 1].offset_ / 2;
302 for (row_type row = 0; row <= nrows(); ++row)
303 rowinfo_[row].offset_ -= h;
306 // compute absolute sizes of horizontal structure
307 for (col_type col = 0; col < ncols(); ++col) {
309 for (row_type row = 0; row < nrows(); ++row)
310 wid = max(wid, cell(index(row, col)).width());
311 colinfo_[col].width_ = wid;
313 colinfo_[ncols()].width_ = 0;
315 // compute horizontal offsets
316 colinfo_[0].offset_ = border();
317 for (col_type col = 1; col <= ncols(); ++col) {
318 colinfo_[col].offset_ =
319 colinfo_[col - 1].offset_ +
320 colinfo_[col - 1].width_ +
321 colinfo_[col - 1].skip_ +
323 colinfo_[col].lines_ * vlinesep();
327 dim_.w = colinfo_[ncols() - 1].offset_
328 + colinfo_[ncols() - 1].width_
329 + vlinesep() * colinfo_[ncols()].lines_
332 dim_.a = - rowinfo_[0].offset_
333 + rowinfo_[0].ascent_
334 + hlinesep() * rowinfo_[0].lines_
337 dim_.d = rowinfo_[nrows() - 1].offset_
338 + rowinfo_[nrows() - 1].descent_
339 + hlinesep() * rowinfo_[nrows()].lines_
344 // Increase ws_[i] for 'R' columns (except the first one)
345 for (int i = 1; i < nc_; ++i)
346 if (align_[i] == 'R')
347 ws_[i] += 10 * df_width;
348 // Increase ws_[i] for 'C' column
349 if (align_[0] == 'C')
350 if (ws_[0] < 7 * workwidth / 8)
351 ws_[0] = 7 * workwidth / 8;
355 for (cxrow = row_.begin(); cxrow; ++cxrow) {
358 for (int i = 0; i < nc_; ++i) {
360 if (cxrow->getTab(i) <= 0) {
361 cxrow->setTab(i, df_width);
369 lf = (ws_[i] - cxrow->getTab(i))/2;
373 lf = ws_[i] - cxrow->getTab(i);
376 if (cxrow == row_.begin())
378 else if (cxrow.is_last())
379 lf = ws_[i] - cxrow->getTab(i);
381 lf = (ws_[i] - cxrow->getTab(i))/2;
384 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
385 cxrow->setTab(i, lf + rg);
386 rg = ws_[i] - ww + colsep();
387 if (cxrow == row_.begin())
388 width += ws_[i] + colsep();
390 cxrow->setBaseline(cxrow->getBaseline() - ascent);
396 void MathGridInset::draw(MathPainterInfo & pi, int x, int y) const
398 for (idx_type idx = 0; idx < nargs(); ++idx)
399 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
401 for (row_type row = 0; row <= nrows(); ++row)
402 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
403 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
404 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
405 pi.pain.line(x + 1, yy, x + width() - 1, yy);
408 for (col_type col = 0; col <= ncols(); ++col)
409 for (int i = 0; i < colinfo_[col].lines_; ++i) {
410 int xx = x + colinfo_[col].offset_
411 - i * vlinesep() - vlinesep()/2 - colsep()/2;
412 pi.pain.line(xx, y - ascent() + 1, xx, y + descent() - 1);
417 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
419 // let the cells adjust themselves
420 //MathNestInset::metrics(mi);
421 for (idx_type i = 0; i < nargs(); ++i)
422 cell(i).metricsT(mi);
424 // compute absolute sizes of vertical structure
425 for (row_type row = 0; row < nrows(); ++row) {
428 for (col_type col = 0; col < ncols(); ++col) {
429 MathArray const & c = cell(index(row, col));
430 asc = max(asc, c.ascent());
431 desc = max(desc, c.descent());
433 rowinfo_[row].ascent_ = asc;
434 rowinfo_[row].descent_ = desc;
436 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
437 rowinfo_[nrows()].ascent_ = 0;
438 rowinfo_[nrows()].descent_ = 0;
440 // compute vertical offsets
441 rowinfo_[0].offset_ = 0;
442 for (row_type row = 1; row <= nrows(); ++row) {
443 rowinfo_[row].offset_ =
444 rowinfo_[row - 1].offset_ +
445 rowinfo_[row - 1].descent_ +
446 //rowinfo_[row - 1].skipPixels() +
448 //rowinfo_[row].lines_ * hlinesep() +
449 rowinfo_[row].ascent_;
452 // adjust vertical offset
459 h = rowinfo_[nrows() - 1].offset_;
462 h = rowinfo_[nrows() - 1].offset_ / 2;
464 for (row_type row = 0; row <= nrows(); ++row)
465 rowinfo_[row].offset_ -= h;
468 // compute absolute sizes of horizontal structure
469 for (col_type col = 0; col < ncols(); ++col) {
471 for (row_type row = 0; row < nrows(); ++row)
472 wid = max(wid, cell(index(row, col)).width());
473 colinfo_[col].width_ = wid;
475 colinfo_[ncols()].width_ = 0;
477 // compute horizontal offsets
478 colinfo_[0].offset_ = border();
479 for (col_type col = 1; col <= ncols(); ++col) {
480 colinfo_[col].offset_ =
481 colinfo_[col - 1].offset_ +
482 colinfo_[col - 1].width_ +
483 colinfo_[col - 1].skip_ +
485 //colinfo_[col].lines_ * vlinesep();
489 dim_.w = colinfo_[ncols() - 1].offset_
490 + colinfo_[ncols() - 1].width_
491 //+ vlinesep() * colinfo_[ncols()].lines_
494 dim_.a = -rowinfo_[0].offset_
495 + rowinfo_[0].ascent_
496 //+ hlinesep() * rowinfo_[0].lines_
499 dim_.d = rowinfo_[nrows() - 1].offset_
500 + rowinfo_[nrows() - 1].descent_
501 //+ hlinesep() * rowinfo_[nrows()].lines_
507 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
509 for (idx_type idx = 0; idx < nargs(); ++idx)
510 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
514 string MathGridInset::eolString(row_type row, bool fragile) const
518 if (!rowinfo_[row].crskip_.zero())
519 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
521 // make sure an upcoming '[' does not break anything
522 if (row + 1 < nrows()) {
523 MathArray const & c = cell(index(row + 1, 0));
524 if (c.size() && c.front()->getChar() == '[')
529 // only add \\ if necessary
530 if (eol.empty() && row + 1 == nrows())
533 return (fragile ? "\\protect\\\\" : "\\\\") + eol + '\n';
537 string MathGridInset::eocString(col_type col) const
539 if (col + 1 == ncols())
545 void MathGridInset::addRow(row_type row)
547 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
549 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
551 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
555 void MathGridInset::appendRow()
557 rowinfo_.push_back(RowInfo());
558 //cells_.insert(cells_.end(), ncols(), MathArray());
559 for (col_type col = 0; col < ncols(); ++col) {
560 cells_.push_back(cells_type::value_type());
561 cellinfo_.push_back(CellInfo());
566 void MathGridInset::delRow(row_type row)
571 cells_type::iterator it = cells_.begin() + row * ncols();
572 cells_.erase(it, it + ncols());
574 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
575 cellinfo_.erase(jt, jt + ncols());
577 rowinfo_.erase(rowinfo_.begin() + row);
581 void MathGridInset::addCol(col_type newcol)
583 const col_type nc = ncols();
584 const row_type nr = nrows();
585 cells_type new_cells((nc + 1) * nr);
586 vector<CellInfo> new_cellinfo((nc + 1) * nr);
588 for (row_type row = 0; row < nr; ++row)
589 for (col_type col = 0; col < nc; ++col) {
590 new_cells[row * (nc + 1) + col + (col > newcol)]
591 = cells_[row * nc + col];
592 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
593 = cellinfo_[row * nc + col];
595 swap(cells_, new_cells);
596 swap(cellinfo_, new_cellinfo);
599 inf.skip_ = defaultColSpace(newcol);
600 inf.align_ = defaultColAlign(newcol);
601 colinfo_.insert(colinfo_.begin() + newcol, inf);
605 void MathGridInset::delCol(col_type col)
611 vector<CellInfo> tmpcellinfo;
612 for (col_type i = 0; i < nargs(); ++i)
613 if (i % ncols() != col) {
614 tmpcells.push_back(cells_[i]);
615 tmpcellinfo.push_back(cellinfo_[i]);
617 swap(cells_, tmpcells);
618 swap(cellinfo_, tmpcellinfo);
620 colinfo_.erase(colinfo_.begin() + col);
624 int MathGridInset::cellXOffset(idx_type idx) const
626 col_type c = col(idx);
627 int x = colinfo_[c].offset_;
628 char align = colinfo_[c].align_;
629 if (align == 'r' || align == 'R')
630 x += colinfo_[c].width_ - cell(idx).width();
631 if (align == 'c' || align == 'C')
632 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
637 int MathGridInset::cellYOffset(idx_type idx) const
639 return rowinfo_[row(idx)].offset_;
643 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
650 pos = cell(idx).x2pos(targetx - cell(idx).xo());
653 if (idx >= ncols() * (nrows() - 1))
656 pos = cell(idx).x2pos(targetx - cell(idx).xo());
662 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
664 // leave matrix if on the left hand edge
668 pos = cell(idx).size();
673 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
675 // leave matrix if on the right hand edge
676 if (col(idx) + 1 == ncols())
684 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
691 idx = (nrows() - 1) * ncols();
694 idx = ((nrows() - 1) / 2) * ncols();
701 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
711 idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
713 pos = cell(idx).size();
718 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
725 idx -= idx % ncols();
738 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
740 if (pos < cell(idx).size()) {
741 pos = cell(idx).size();
744 if (col(idx) < ncols() - 1) {
745 idx = idx - idx % ncols() + ncols() - 1;
746 pos = cell(idx).size();
749 if (idx < nargs() - 1) {
751 pos = cell(idx).size();
758 bool MathGridInset::idxDelete(idx_type & idx)
760 // nothing to do if we have just one row
764 // nothing to do if we are in the middle of the last row of the inset
765 if (idx + ncols() > nargs())
768 // try to delete entire sequence of ncols() empty cells if possible
769 for (idx_type i = idx; i < idx + ncols(); ++i)
773 // move cells if necessary
774 for (idx_type i = index(row(idx), 0); i < idx; ++i)
775 std::swap(cell(i), cell(i + ncols()));
782 // undo effect of Ctrl-Tab (i.e. pull next cell)
783 //if (idx + 1 != nargs())
784 // cell(idx).swap(cell(idx + 1));
786 // we handled the event..
791 // reimplement old behaviour when pressing Delete in the last position
793 void MathGridInset::idxGlue(idx_type idx)
795 col_type c = col(idx);
796 if (c + 1 == ncols()) {
797 if (row(idx) + 1 != nrows()) {
798 for (col_type cc = 0; cc < ncols(); ++cc)
799 cell(idx).append(cell(idx + cc + 1));
800 delRow(row(idx) + 1);
803 cell(idx).append(cell(idx + 1));
804 for (col_type cc = c + 2; cc < ncols(); ++cc)
805 cell(idx - c + cc - 1) = cell(idx - c + cc);
806 cell(idx - c + ncols() - 1).clear();
811 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
813 return rowinfo_[row];
817 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
819 return rowinfo_[row];
823 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
825 row_type const ri = row(idx);
826 row_type const r1 = min(row(from), row(to));
827 row_type const r2 = max(row(from), row(to));
828 col_type const ci = col(idx);
829 col_type const c1 = min(col(from), col(to));
830 col_type const c2 = max(col(from), col(to));
831 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
836 void MathGridInset::normalize(NormalStream & os) const
839 for (row_type row = 0; row < nrows(); ++row) {
841 for (col_type col = 0; col < ncols(); ++col)
842 os << "[cell " << cell(index(row, col)) << ']';
849 void MathGridInset::mathmlize(MathMLStream & os) const
851 os << MTag("mtable");
852 for (row_type row = 0; row < nrows(); ++row) {
854 for (col_type col = 0; col < ncols(); ++col)
855 os << cell(index(row, col));
858 os << ETag("mtable");
862 void MathGridInset::write(WriteStream & os) const
864 for (row_type row = 0; row < nrows(); ++row) {
865 os << verboseHLine(rowinfo_[row].lines_);
866 for (col_type col = 0; col < ncols(); ++col)
867 os << cell(index(row, col)) << eocString(col);
868 os << eolString(row, os.fragile());
870 string const s = verboseHLine(rowinfo_[nrows()].lines_);
871 if (!s.empty() && s != " ") {
879 int MathGridInset::colsep() const
885 int MathGridInset::rowsep() const
891 int MathGridInset::hlinesep() const
897 int MathGridInset::vlinesep() const
903 int MathGridInset::border() const
909 void MathGridInset::splitCell(idx_type & idx, pos_type & pos)
911 if (idx + 1 == nargs())
913 MathArray ar = cell(idx);
915 cell(idx).erase(pos, cell(idx).size());
918 cell(idx).insert(0, ar);
922 MathInset::result_type MathGridInset::dispatch
923 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
925 switch (cmd.action) {
927 case LFUN_DELETE_LINE_FORWARD:
928 //autocorrect_ = false;
938 if (pos > cell(idx).size())
939 pos = cell(idx).size();
940 return DISPATCHED_POP;
943 //bv->lockedInsetStoreUndo(Undo::EDIT);
945 //updateLocal(bv, true);
946 return DISPATCHED_POP;
948 case LFUN_BREAKLINE: {
949 //bv->lockedInsetStoreUndo(Undo::INSERT);
950 row_type const r = row(idx);
954 for (col_type c = col(idx) + 1; c < ncols(); ++c)
955 std::swap(cell(index(r, c)), cell(index(r + 1, c)));
959 std::swap(cell(idx), cell(idx + ncols() - 1));
961 //mathcursor->normalize();
962 //updateLocal(bv, true);
963 return DISPATCHED_POP;
966 case LFUN_MATH_HALIGN:
967 halign((cmd.argument + "c")[0], col(idx));
968 return DISPATCHED_POP;
970 case LFUN_MATH_VALIGN:
971 valign((cmd.argument + "c")[0]);
972 return DISPATCHED_POP;
974 case LFUN_MATH_ROW_INSERT:
976 return DISPATCHED_POP;
978 case LFUN_MATH_ROW_DELETE:
982 return DISPATCHED_POP;
984 case LFUN_MATH_COLUMN_INSERT: {
985 row_type r = row(idx);
986 col_type c = col(idx);
989 return DISPATCHED_POP;
992 case LFUN_MATH_COLUMN_DELETE: {
993 row_type r = row(idx);
994 col_type c = col(idx);
995 delFancyCol(col(idx));
999 return DISPATCHED_POP;
1003 //lyxerr << "pasting '" << cmd.argument << "'\n";
1004 MathGridInset grid(1, 1);
1005 mathed_parse_normal(grid, cmd.argument);
1006 if (grid.nargs() == 1) {
1007 // single cell/part of cell
1008 cell(idx).insert(pos, grid.cell(0));
1009 pos += grid.cell(0).size();
1012 col_type const numcols = min(grid.ncols(), ncols() - col(idx));
1013 row_type const numrows = min(grid.nrows(), nrows() - row(idx));
1014 for (row_type r = 0; r < numrows; ++r) {
1015 for (col_type c = 0; c < numcols; ++c) {
1016 idx_type i = index(r + row(idx), c + col(idx));
1017 cell(i).append(grid.cell(grid.index(r, c)));
1019 // append the left over horizontal cells to the last column
1020 idx_type i = index(r + row(idx), ncols() - 1);
1021 for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1022 cell(i).append(grid.cell(grid.index(r, c)));
1024 // append the left over vertical cells to the last _cell_
1025 idx_type i = nargs() - 1;
1026 for (row_type r = numrows; r < grid.nrows(); ++r)
1027 for (col_type c = 0; c < grid.ncols(); ++c)
1028 cell(i).append(grid.cell(grid.index(r, c)));
1030 return DISPATCHED_POP;
1034 return MathNestInset::dispatch(cmd, idx, pos);
1036 return UNDISPATCHED;