2 * \file InsetMathGrid.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "InsetMathGrid.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
23 #include "FuncStatus.h"
25 #include "FuncRequest.h"
27 #include "frontends/Clipboard.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "support/lassert.h"
40 using namespace lyx::support;
45 static docstring verboseHLine(int n)
48 for (int i = 0; i < n; ++i)
56 static int extractInt(istream & is)
60 return (num == 0) ? 1 : num;
65 //////////////////////////////////////////////////////////////
68 InsetMathGrid::CellInfo::CellInfo()
74 //////////////////////////////////////////////////////////////
77 InsetMathGrid::RowInfo::RowInfo()
78 : lines_(0), skip_(0), allow_newpage_(true)
83 int InsetMathGrid::RowInfo::skipPixels() const
85 return crskip_.inBP();
90 //////////////////////////////////////////////////////////////
93 InsetMathGrid::ColInfo::ColInfo()
94 : align_('c'), lines_(0)
98 //////////////////////////////////////////////////////////////
101 InsetMathGrid::InsetMathGrid()
112 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
113 : InsetMathNest(m * n),
123 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
124 : InsetMathNest(m * n),
131 setVerticalAlignment(v);
132 setHorizontalAlignments(h);
136 Inset * InsetMathGrid::clone() const
138 return new InsetMathGrid(*this);
142 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
144 return col + ncols() * row;
148 void InsetMathGrid::setDefaults()
151 lyxerr << "positive number of columns expected" << endl;
153 // lyxerr << "positive number of rows expected" << endl;
154 for (col_type col = 0; col < ncols(); ++col) {
155 colinfo_[col].align_ = defaultColAlign(col);
156 colinfo_[col].skip_ = defaultColSpace(col);
157 colinfo_[col].special_.clear();
162 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
165 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
168 colinfo_[col].lines_++;
169 } else if ((c == 'p' || c == 'm' || c == 'b'||
170 c == '!' || c == '@' || c == '>' || c == '<') &&
171 it + 1 != hh.end() && *(it + 1) == '{') {
172 // @{decl.} and p{width} are standard LaTeX, the
173 // others are extensions by array.sty
174 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
176 // this declares a new column
178 // Only intercolumn stuff is allowed
179 // in the last dummy column
181 colinfo_[col].align_ = 'l';
183 // this is intercolumn stuff
184 if (colinfo_[col].special_.empty())
185 // Overtake possible lines
186 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
190 while (it != hh.end()) {
192 colinfo_[col].special_ += c;
198 if (brace_open > 0 && brace_open == brace_close)
203 colinfo_[col].lines_ = count(
204 colinfo_[col].special_.begin(),
205 colinfo_[col].special_.end(), '|');
206 LYXERR(Debug::MATHED, "special column separator: `"
207 << to_utf8(colinfo_[col].special_) << '\'');
209 colinfo_[col].lines_ = 0;
210 colinfo_[col].special_.clear();
212 } else if (col >= ncols()) {
213 // Only intercolumn stuff is allowed in the last
216 } else if (c == 'c' || c == 'l' || c == 'r') {
217 colinfo_[col].align_ = static_cast<char>(c);
218 if (!colinfo_[col].special_.empty()) {
219 colinfo_[col].special_ += c;
220 colinfo_[col].lines_ = count(
221 colinfo_[col].special_.begin(),
222 colinfo_[col].special_.end(), '|');
223 LYXERR(Debug::MATHED, "special column separator: `"
224 << to_utf8(colinfo_[col].special_) << '\'');
227 colinfo_[col].lines_ = 0;
228 colinfo_[col].special_.clear();
230 lyxerr << "unknown column separator: '" << c << "'" << endl;
235 col_type n = hh.size();
238 for (col_type col = 0; col < n; ++col)
239 colinfo_[col].align_ = hh[col];
244 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
247 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
248 if (*it == 'c' || *it == 'l' || *it == 'r'||
249 *it == 'p' || *it == 'm' || *it == 'b')
251 // let's have at least one column, even if we did not recognize its
259 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
261 colinfo_[col].align_ = h;
262 if (!colinfo_[col].special_.empty()) {
263 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
264 if (c == 'l' || c == 'c' || c == 'r')
267 // FIXME: Change alignment of p, m and b columns, too
271 char InsetMathGrid::horizontalAlignment(col_type col) const
273 return colinfo_[col].align_;
277 docstring InsetMathGrid::horizontalAlignments() const
280 for (col_type col = 0; col < ncols(); ++col) {
281 if (colinfo_[col].special_.empty()) {
282 res += docstring(colinfo_[col].lines_, '|');
283 res += colinfo_[col].align_;
285 res += colinfo_[col].special_;
287 if (colinfo_[ncols()].special_.empty())
288 return res + docstring(colinfo_[ncols()].lines_, '|');
289 return res + colinfo_[ncols()].special_;
293 void InsetMathGrid::setVerticalAlignment(char c)
299 char InsetMathGrid::verticalAlignment() const
305 InsetMathGrid::col_type InsetMathGrid::ncols() const
307 return colinfo_.size() - 1;
311 InsetMathGrid::row_type InsetMathGrid::nrows() const
313 return rowinfo_.size() - 1;
317 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
319 return idx % ncols();
323 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
325 return idx / ncols();
329 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
331 rowinfo_[row].crskip_ = crskip;
335 Length InsetMathGrid::vcrskip(row_type row) const
337 return rowinfo_[row].crskip_;
341 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
343 // let the cells adjust themselves
344 InsetMathNest::metrics(mi);
346 BufferView & bv = *mi.base.bv;
348 // compute absolute sizes of vertical structure
349 for (row_type row = 0; row < nrows(); ++row) {
352 for (col_type col = 0; col < ncols(); ++col) {
353 Dimension const & dimc = cell(index(row, col)).dimension(bv);
354 asc = max(asc, dimc.asc);
355 desc = max(desc, dimc.des);
357 rowinfo_[row].ascent_ = asc;
358 rowinfo_[row].descent_ = desc;
360 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
361 rowinfo_[nrows()].ascent_ = 0;
362 rowinfo_[nrows()].descent_ = 0;
364 // compute vertical offsets
365 rowinfo_[0].offset_ = 0;
366 for (row_type row = 1; row <= nrows(); ++row) {
367 rowinfo_[row].offset_ =
368 rowinfo_[row - 1].offset_ +
369 rowinfo_[row - 1].descent_ +
370 rowinfo_[row - 1].skipPixels() +
372 rowinfo_[row].lines_ * hlinesep() +
373 rowinfo_[row].ascent_;
376 // adjust vertical offset
383 h = rowinfo_[nrows() - 1].offset_;
386 h = rowinfo_[nrows() - 1].offset_ / 2;
388 for (row_type row = 0; row <= nrows(); ++row)
389 rowinfo_[row].offset_ -= h;
392 // compute absolute sizes of horizontal structure
393 for (col_type col = 0; col < ncols(); ++col) {
395 for (row_type row = 0; row < nrows(); ++row)
396 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
397 colinfo_[col].width_ = wid;
399 colinfo_[ncols()].width_ = 0;
401 // compute horizontal offsets
402 colinfo_[0].offset_ = border();
403 for (col_type col = 1; col <= ncols(); ++col) {
404 colinfo_[col].offset_ =
405 colinfo_[col - 1].offset_ +
406 colinfo_[col - 1].width_ +
407 colinfo_[col - 1].skip_ +
409 colinfo_[col].lines_ * vlinesep();
413 dim.wid = colinfo_[ncols() - 1].offset_
414 + colinfo_[ncols() - 1].width_
415 + vlinesep() * colinfo_[ncols()].lines_
418 dim.asc = - rowinfo_[0].offset_
419 + rowinfo_[0].ascent_
420 + hlinesep() * rowinfo_[0].lines_
423 dim.des = rowinfo_[nrows() - 1].offset_
424 + rowinfo_[nrows() - 1].descent_
425 + hlinesep() * rowinfo_[nrows()].lines_
430 // Increase ws_[i] for 'R' columns (except the first one)
431 for (int i = 1; i < nc_; ++i)
432 if (align_[i] == 'R')
433 ws_[i] += 10 * df_width;
434 // Increase ws_[i] for 'C' column
435 if (align_[0] == 'C')
436 if (ws_[0] < 7 * workwidth / 8)
437 ws_[0] = 7 * workwidth / 8;
441 for (cxrow = row_.begin(); cxrow; ++cxrow) {
444 for (int i = 0; i < nc_; ++i) {
446 if (cxrow->getTab(i) <= 0) {
447 cxrow->setTab(i, df_width);
455 lf = (ws_[i] - cxrow->getTab(i))/2;
459 lf = ws_[i] - cxrow->getTab(i);
462 if (cxrow == row_.begin())
464 else if (cxrow.is_last())
465 lf = ws_[i] - cxrow->getTab(i);
467 lf = (ws_[i] - cxrow->getTab(i))/2;
470 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
471 cxrow->setTab(i, lf + rg);
472 rg = ws_[i] - ww + colsep();
473 if (cxrow == row_.begin())
474 width += ws_[i] + colsep();
476 cxrow->setBaseline(cxrow->getBaseline() - ascent);
479 metricsMarkers2(dim);
480 // Cache the inset dimension.
481 setDimCache(mi, dim);
485 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
487 drawWithMargin(pi, x, y, 1, 1);
491 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
492 int lmargin, int rmargin) const
494 Dimension const dim = dimension(*pi.base.bv);
495 BufferView const & bv = *pi.base.bv;
497 for (idx_type idx = 0; idx < nargs(); ++idx)
498 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
499 y + cellYOffset(idx));
501 for (row_type row = 0; row <= nrows(); ++row)
502 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
503 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
504 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
505 pi.pain.line(x + lmargin + 1, yy,
506 x + dim.width() - rmargin - 1, yy,
510 for (col_type col = 0; col <= ncols(); ++col)
511 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
512 int xx = x + lmargin + colinfo_[col].offset_
513 - i * vlinesep() - vlinesep()/2 - colsep()/2;
514 pi.pain.line(xx, y - dim.ascent() + 1,
515 xx, y + dim.descent() - 1,
518 drawMarkers2(pi, x, y);
522 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
524 // let the cells adjust themselves
525 //InsetMathNest::metrics(mi);
526 for (idx_type i = 0; i < nargs(); ++i)
527 cell(i).metricsT(mi, dim);
529 // compute absolute sizes of vertical structure
530 for (row_type row = 0; row < nrows(); ++row) {
533 for (col_type col = 0; col < ncols(); ++col) {
534 //MathData const & c = cell(index(row, col));
537 asc = max(asc, dimc.ascent());
538 desc = max(desc, dimc.descent());
540 rowinfo_[row].ascent_ = asc;
541 rowinfo_[row].descent_ = desc;
543 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
544 rowinfo_[nrows()].ascent_ = 0;
545 rowinfo_[nrows()].descent_ = 0;
547 // compute vertical offsets
548 rowinfo_[0].offset_ = 0;
549 for (row_type row = 1; row <= nrows(); ++row) {
550 rowinfo_[row].offset_ =
551 rowinfo_[row - 1].offset_ +
552 rowinfo_[row - 1].descent_ +
553 //rowinfo_[row - 1].skipPixels() +
555 //rowinfo_[row].lines_ * hlinesep() +
556 rowinfo_[row].ascent_;
559 // adjust vertical offset
566 h = rowinfo_[nrows() - 1].offset_;
569 h = rowinfo_[nrows() - 1].offset_ / 2;
571 for (row_type row = 0; row <= nrows(); ++row)
572 rowinfo_[row].offset_ -= h;
575 // compute absolute sizes of horizontal structure
576 for (col_type col = 0; col < ncols(); ++col) {
578 for (row_type row = 0; row < nrows(); ++row) {
580 //wid = max(wid, cell(index(row, col)).width());
582 colinfo_[col].width_ = wid;
584 colinfo_[ncols()].width_ = 0;
586 // compute horizontal offsets
587 colinfo_[0].offset_ = border();
588 for (col_type col = 1; col <= ncols(); ++col) {
589 colinfo_[col].offset_ =
590 colinfo_[col - 1].offset_ +
591 colinfo_[col - 1].width_ +
592 colinfo_[col - 1].skip_ +
594 //colinfo_[col].lines_ * vlinesep();
598 dim.wid = colinfo_[ncols() - 1].offset_
599 + colinfo_[ncols() - 1].width_
600 //+ vlinesep() * colinfo_[ncols()].lines_
603 dim.asc = -rowinfo_[0].offset_
604 + rowinfo_[0].ascent_
605 //+ hlinesep() * rowinfo_[0].lines_
608 dim.des = rowinfo_[nrows() - 1].offset_
609 + rowinfo_[nrows() - 1].descent_
610 //+ hlinesep() * rowinfo_[nrows()].lines_
615 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
617 // for (idx_type idx = 0; idx < nargs(); ++idx)
618 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
622 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
626 if (!rowinfo_[row].crskip_.zero())
627 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
628 else if(!rowinfo_[row].allow_newpage_)
631 // make sure an upcoming '[' does not break anything
632 if (row + 1 < nrows()) {
633 MathData const & c = cell(index(row + 1, 0));
634 if (c.size() && c.front()->getChar() == '[')
639 // only add \\ if necessary
640 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
643 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
647 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
649 if (col + 1 == lastcol)
651 return from_ascii(" & ");
655 void InsetMathGrid::addRow(row_type row)
657 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
659 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
661 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
665 void InsetMathGrid::appendRow()
667 rowinfo_.push_back(RowInfo());
668 //cells_.insert(cells_.end(), ncols(), MathData());
669 for (col_type col = 0; col < ncols(); ++col) {
670 cells_.push_back(cells_type::value_type());
671 cellinfo_.push_back(CellInfo());
676 void InsetMathGrid::delRow(row_type row)
681 cells_type::iterator it = cells_.begin() + row * ncols();
682 cells_.erase(it, it + ncols());
684 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
685 cellinfo_.erase(jt, jt + ncols());
687 rowinfo_.erase(rowinfo_.begin() + row);
691 void InsetMathGrid::copyRow(row_type row)
694 for (col_type col = 0; col < ncols(); ++col)
695 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
699 void InsetMathGrid::swapRow(row_type row)
703 if (row + 1 == nrows())
705 for (col_type col = 0; col < ncols(); ++col)
706 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
710 void InsetMathGrid::addCol(col_type newcol)
712 const col_type nc = ncols();
713 const row_type nr = nrows();
714 cells_type new_cells((nc + 1) * nr);
715 vector<CellInfo> new_cellinfo((nc + 1) * nr);
717 for (row_type row = 0; row < nr; ++row)
718 for (col_type col = 0; col < nc; ++col) {
719 new_cells[row * (nc + 1) + col + (col >= newcol)]
720 = cells_[row * nc + col];
721 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
722 = cellinfo_[row * nc + col];
724 swap(cells_, new_cells);
725 swap(cellinfo_, new_cellinfo);
728 inf.skip_ = defaultColSpace(newcol);
729 inf.align_ = defaultColAlign(newcol);
730 colinfo_.insert(colinfo_.begin() + newcol, inf);
734 void InsetMathGrid::delCol(col_type col)
740 vector<CellInfo> tmpcellinfo;
741 for (col_type i = 0; i < nargs(); ++i)
742 if (i % ncols() != col) {
743 tmpcells.push_back(cells_[i]);
744 tmpcellinfo.push_back(cellinfo_[i]);
746 swap(cells_, tmpcells);
747 swap(cellinfo_, tmpcellinfo);
749 colinfo_.erase(colinfo_.begin() + col);
753 void InsetMathGrid::copyCol(col_type col)
756 for (row_type row = 0; row < nrows(); ++row)
757 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
761 void InsetMathGrid::swapCol(col_type col)
765 if (col + 1 == ncols())
767 for (row_type row = 0; row < nrows(); ++row)
768 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
772 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
774 col_type c = col(idx);
775 int x = colinfo_[c].offset_;
776 char align = colinfo_[c].align_;
777 Dimension const & celldim = cell(idx).dimension(bv);
778 if (align == 'r' || align == 'R')
779 x += colinfo_[c].width_ - celldim.wid;
780 if (align == 'c' || align == 'C')
781 x += (colinfo_[c].width_ - celldim.wid) / 2;
786 int InsetMathGrid::cellYOffset(idx_type idx) const
788 return rowinfo_[row(idx)].offset_;
792 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
797 cur.idx() -= ncols();
799 if (cur.row() + 1 >= nrows())
801 cur.idx() += ncols();
803 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
808 bool InsetMathGrid::idxBackward(Cursor & cur) const
810 // leave matrix if at the front edge
814 cur.pos() = cur.lastpos();
819 bool InsetMathGrid::idxForward(Cursor & cur) const
821 // leave matrix if at the back edge
822 if (cur.col() + 1 == ncols())
830 bool InsetMathGrid::idxFirst(Cursor & cur) const
837 cur.idx() = (nrows() - 1) * ncols();
840 cur.idx() = ((nrows() - 1) / 2) * ncols();
847 bool InsetMathGrid::idxLast(Cursor & cur) const
851 cur.idx() = ncols() - 1;
854 cur.idx() = nargs() - 1;
857 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
859 cur.pos() = cur.lastpos();
864 bool InsetMathGrid::idxDelete(idx_type & idx)
866 // nothing to do if we have just one row
870 // nothing to do if we are in the middle of the last row of the inset
871 if (idx + ncols() > nargs())
874 // try to delete entire sequence of ncols() empty cells if possible
875 for (idx_type i = idx; i < idx + ncols(); ++i)
879 // move cells if necessary
880 for (idx_type i = index(row(idx), 0); i < idx; ++i)
881 swap(cell(i), cell(i + ncols()));
888 // undo effect of Ctrl-Tab (i.e. pull next cell)
889 //if (idx + 1 != nargs())
890 // cell(idx).swap(cell(idx + 1));
892 // we handled the event..
897 // reimplement old behaviour when pressing Delete in the last position
899 void InsetMathGrid::idxGlue(idx_type idx)
901 col_type c = col(idx);
902 if (c + 1 == ncols()) {
903 if (row(idx) + 1 != nrows()) {
904 for (col_type cc = 0; cc < ncols(); ++cc)
905 cell(idx).append(cell(idx + cc + 1));
906 delRow(row(idx) + 1);
909 cell(idx).append(cell(idx + 1));
910 for (col_type cc = c + 2; cc < ncols(); ++cc)
911 cell(idx - c + cc - 1) = cell(idx - c + cc);
912 cell(idx - c + ncols() - 1).clear();
917 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
919 return rowinfo_[row];
923 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
925 return rowinfo_[row];
929 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
931 row_type const ri = row(idx);
932 row_type const r1 = min(row(from), row(to));
933 row_type const r2 = max(row(from), row(to));
934 col_type const ci = col(idx);
935 col_type const c1 = min(col(from), col(to));
936 col_type const c2 = max(col(from), col(to));
937 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
942 void InsetMathGrid::normalize(NormalStream & os) const
945 for (row_type row = 0; row < nrows(); ++row) {
947 for (col_type col = 0; col < ncols(); ++col)
948 os << "[cell " << cell(index(row, col)) << ']';
955 void InsetMathGrid::mathmlize(MathStream & os) const
957 os << MTag("mtable");
958 for (row_type row = 0; row < nrows(); ++row) {
960 for (col_type col = 0; col < ncols(); ++col)
961 os << cell(index(row, col));
964 os << ETag("mtable");
968 void InsetMathGrid::write(WriteStream & os) const
970 bool brace = os.pendingBrace();
971 os.pendingBrace(false);
974 for (row_type row = 0; row < nrows(); ++row) {
975 os << verboseHLine(rowinfo_[row].lines_);
976 // don't write & and empty cells at end of line
977 col_type lastcol = 0;
978 bool emptyline = true;
979 for (col_type col = 0; col < ncols(); ++col)
980 if (!cell(index(row, col)).empty()) {
984 for (col_type col = 0; col < lastcol; ++col) {
985 os << cell(index(row, col));
986 if (os.pendingBrace()) {
987 os.pendingBrace(false);
991 os << eocString(col, lastcol);
993 eol = eolString(row, emptyline, os.fragile());
995 // append newline only if line wasn't completely empty
996 // and this was not the last line in the grid
997 if (!emptyline && row + 1 < nrows())
1000 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1010 os.pendingBrace(brace);
1014 int InsetMathGrid::colsep() const
1020 int InsetMathGrid::rowsep() const
1026 int InsetMathGrid::hlinesep() const
1032 int InsetMathGrid::vlinesep() const
1038 int InsetMathGrid::border() const
1044 void InsetMathGrid::splitCell(Cursor & cur)
1046 if (cur.idx() == cur.lastidx())
1048 MathData ar = cur.cell();
1049 ar.erase(0, cur.pos());
1050 cur.cell().erase(cur.pos(), cur.lastpos());
1053 cur.cell().insert(0, ar);
1057 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1059 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1060 switch (cmd.action) {
1062 // insert file functions
1063 case LFUN_LINE_DELETE:
1064 // FIXME: We use recordUndoInset when a change reflects more
1065 // than one cell, because recordUndo does not work for
1066 // multiple cells. Unfortunately this puts the cursor in front
1067 // of the inset after undo. This is (especilally for large
1069 cur.recordUndoInset();
1070 //autocorrect_ = false;
1078 if (cur.idx() > cur.lastidx())
1079 cur.idx() = cur.lastidx();
1080 if (cur.pos() > cur.lastpos())
1081 cur.pos() = cur.lastpos();
1084 case LFUN_CELL_SPLIT:
1089 case LFUN_CELL_BACKWARD:
1091 cur.selection() = false;
1092 if (!idxPrev(cur)) {
1093 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1098 case LFUN_CELL_FORWARD:
1099 // Can't handle selection by additional 'shift' as this is
1100 // hard bound to LFUN_CELL_BACKWARD
1101 cur.selection() = false;
1102 if (!idxNext(cur)) {
1103 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1108 case LFUN_NEWLINE_INSERT: {
1109 cur.recordUndoInset();
1110 row_type const r = cur.row();
1114 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1115 swap(cell(index(r, c)), cell(index(r + 1, c)));
1119 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1122 cur.pos() = cur.lastpos();
1124 //mathcursor->normalize();
1125 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1129 case LFUN_TABULAR_FEATURE: {
1130 cur.recordUndoInset();
1131 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1132 istringstream is(to_utf8(cmd.argument()));
1135 if (s == "valign-top")
1136 setVerticalAlignment('t');
1137 else if (s == "valign-middle")
1138 setVerticalAlignment('c');
1139 else if (s == "valign-bottom")
1140 setVerticalAlignment('b');
1141 else if (s == "align-left")
1142 setHorizontalAlignment('l', cur.col());
1143 else if (s == "align-right")
1144 setHorizontalAlignment('r', cur.col());
1145 else if (s == "align-center")
1146 setHorizontalAlignment('c', cur.col());
1147 else if (s == "append-row")
1148 for (int i = 0, n = extractInt(is); i < n; ++i)
1150 else if (s == "delete-row") {
1151 cur.clearSelection(); // bug 4323
1152 for (int i = 0, n = extractInt(is); i < n; ++i) {
1154 if (cur.idx() >= nargs())
1155 cur.idx() -= ncols();
1157 cur.pos() = 0; // trick, see below
1159 else if (s == "copy-row") {
1160 // Here (as later) we save the cursor col/row
1161 // in order to restore it after operation.
1162 row_type const r = cur.row();
1163 col_type const c = cur.col();
1164 for (int i = 0, n = extractInt(is); i < n; ++i)
1166 cur.idx() = index(r, c);
1168 else if (s == "swap-row") {
1170 // Trick to suppress same-idx-means-different-cell
1174 else if (s == "add-hline-above")
1175 rowinfo_[cur.row()].lines_++;
1176 else if (s == "add-hline-below")
1177 rowinfo_[cur.row()+1].lines_++;
1178 else if (s == "delete-hline-above")
1179 rowinfo_[cur.row()].lines_--;
1180 else if (s == "delete-hline-below")
1181 rowinfo_[cur.row()+1].lines_--;
1182 else if (s == "append-column") {
1183 row_type const r = cur.row();
1184 col_type const c = cur.col();
1185 for (int i = 0, n = extractInt(is); i < n; ++i)
1186 addCol(cur.col() + 1);
1187 cur.idx() = index(r, c);
1189 else if (s == "delete-column") {
1190 cur.clearSelection(); // bug 4323
1191 row_type const r = cur.row();
1192 col_type const c = cur.col();
1193 for (int i = 0, n = extractInt(is); i < n; ++i)
1194 delCol(col(cur.idx()));
1195 cur.idx() = index(r, min(c, cur.ncols() - 1));
1196 cur.pos() = 0; // trick, see above
1198 else if (s == "copy-column") {
1199 row_type const r = cur.row();
1200 col_type const c = cur.col();
1202 cur.idx() = index(r, c);
1204 else if (s == "swap-column") {
1206 cur.pos() = 0; // trick, see above
1208 else if (s == "add-vline-left") {
1209 colinfo_[cur.col()].lines_++;
1210 if (!colinfo_[cur.col()].special_.empty())
1211 colinfo_[cur.col()].special_ += '|';
1213 else if (s == "add-vline-right") {
1214 colinfo_[cur.col()+1].lines_++;
1215 if (!colinfo_[cur.col()+1].special_.empty())
1216 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1218 else if (s == "delete-vline-left") {
1219 colinfo_[cur.col()].lines_--;
1220 docstring & special = colinfo_[cur.col()].special_;
1221 if (!special.empty()) {
1222 docstring::size_type i = special.rfind('|');
1223 LASSERT(i != docstring::npos, /**/);
1224 special.erase(i, 1);
1227 else if (s == "delete-vline-right") {
1228 colinfo_[cur.col()+1].lines_--;
1229 docstring & special = colinfo_[cur.col()+1].special_;
1230 if (!special.empty()) {
1231 docstring::size_type i = special.find('|');
1232 LASSERT(i != docstring::npos, /**/);
1233 special.erase(i, 1);
1240 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1241 lyxerr << "returning FINISHED_LEFT" << endl;
1246 cur.message(_("Paste"));
1247 cap::replaceSelection(cur);
1249 if (cmd.argument().empty() && !theClipboard().isInternal())
1250 topaste = theClipboard().getAsText();
1252 idocstringstream is(cmd.argument());
1255 topaste = cap::selection(n);
1257 InsetMathGrid grid(1, 1);
1258 if (!topaste.empty())
1259 mathed_parse_normal(grid, topaste);
1261 if (grid.nargs() == 1) {
1262 // single cell/part of cell
1264 cur.cell().insert(cur.pos(), grid.cell(0));
1265 cur.pos() += grid.cell(0).size();
1268 cur.recordUndoInset();
1269 col_type const numcols =
1270 min(grid.ncols(), ncols() - col(cur.idx()));
1271 row_type const numrows =
1272 min(grid.nrows(), nrows() - cur.row());
1273 for (row_type r = 0; r < numrows; ++r) {
1274 for (col_type c = 0; c < numcols; ++c) {
1275 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1276 cell(i).insert(0, grid.cell(grid.index(r, c)));
1278 // append the left over horizontal cells to the last column
1279 idx_type i = index(r + cur.row(), ncols() - 1);
1280 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1281 cell(i).append(grid.cell(grid.index(r, c)));
1283 // append the left over vertical cells to the last _cell_
1284 idx_type i = nargs() - 1;
1285 for (row_type r = numrows; r < grid.nrows(); ++r)
1286 for (col_type c = 0; c < grid.ncols(); ++c)
1287 cell(i).append(grid.cell(grid.index(r, c)));
1289 cur.clearSelection(); // bug 393
1294 case LFUN_LINE_BEGIN_SELECT:
1295 case LFUN_LINE_BEGIN:
1296 case LFUN_WORD_BACKWARD_SELECT:
1297 case LFUN_WORD_BACKWARD:
1298 case LFUN_WORD_LEFT_SELECT:
1299 case LFUN_WORD_LEFT:
1300 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1301 cmd.action == LFUN_WORD_LEFT_SELECT ||
1302 cmd.action == LFUN_LINE_BEGIN_SELECT);
1303 cur.macroModeClose();
1304 if (cur.pos() != 0) {
1306 } else if (cur.idx() % cur.ncols() != 0) {
1307 cur.idx() -= cur.idx() % cur.ncols();
1309 } else if (cur.idx() != 0) {
1313 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1318 case LFUN_WORD_FORWARD_SELECT:
1319 case LFUN_WORD_FORWARD:
1320 case LFUN_WORD_RIGHT_SELECT:
1321 case LFUN_WORD_RIGHT:
1322 case LFUN_LINE_END_SELECT:
1324 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1325 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1326 cmd.action == LFUN_LINE_END_SELECT);
1327 cur.macroModeClose();
1329 if (cur.pos() != cur.lastpos()) {
1330 cur.pos() = cur.lastpos();
1331 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1332 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1333 cur.pos() = cur.lastpos();
1334 } else if (cur.idx() != cur.lastidx()) {
1335 cur.idx() = cur.lastidx();
1336 cur.pos() = cur.lastpos();
1338 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1344 InsetMathNest::doDispatch(cur, cmd);
1349 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1350 FuncStatus & status) const
1352 switch (cmd.action) {
1353 case LFUN_TABULAR_FEATURE: {
1354 string const s = to_utf8(cmd.argument());
1355 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1356 status.setEnabled(false);
1357 status.message(from_utf8(N_("Only one row")));
1361 (s == "delete-column" || s == "swap-column")) {
1362 status.setEnabled(false);
1363 status.message(from_utf8(N_("Only one column")));
1366 if ((rowinfo_[cur.row()].lines_ == 0 &&
1367 s == "delete-hline-above") ||
1368 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1369 s == "delete-hline-below")) {
1370 status.setEnabled(false);
1371 status.message(from_utf8(N_("No hline to delete")));
1375 if ((colinfo_[cur.col()].lines_ == 0 &&
1376 s == "delete-vline-left") ||
1377 (colinfo_[cur.col() + 1].lines_ == 0 &&
1378 s == "delete-vline-right")) {
1379 status.setEnabled(false);
1380 status.message(from_utf8(N_("No vline to delete")));
1383 if (s == "valign-top" || s == "valign-middle" ||
1384 s == "valign-bottom" || s == "align-left" ||
1385 s == "align-right" || s == "align-center" ||
1386 s == "append-row" || s == "delete-row" ||
1387 s == "copy-row" || s == "swap-row" ||
1388 s == "add-hline-above" || s == "add-hline-below" ||
1389 s == "delete-hline-above" || s == "delete-hline-below" ||
1390 s == "append-column" || s == "delete-column" ||
1391 s == "copy-column" || s == "swap-column" ||
1392 s == "add-vline-left" || s == "add-vline-right" ||
1393 s == "delete-vline-left" || s == "delete-vline-right")
1394 status.setEnabled(true);
1396 status.setEnabled(false);
1397 status.message(bformat(
1398 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1401 char const ha = horizontalAlignment(cur.col());
1402 char const va = verticalAlignment();
1403 status.setOnOff((s == "align-left" && ha == 'l')
1404 || (s == "align-right" && ha == 'r')
1405 || (s == "align-center" && ha == 'c')
1406 || (s == "valign-top" && va == 't')
1407 || (s == "valign-bottom" && va == 'b')
1408 || (s == "valign-middle" && va == 'm'));
1411 // FIXME: What did this code do?
1412 // Please check whether it is still needed!
1413 // should be more precise
1414 if (v_align_ == '\0') {
1415 status.enable(true);
1418 if (cmd.argument().empty()) {
1419 status.enable(false);
1422 if (!contains("tcb", cmd.argument()[0])) {
1423 status.enable(false);
1426 status.setOnOff(cmd.argument()[0] == v_align_);
1427 status.setEnabled(true);
1432 case LFUN_CELL_SPLIT:
1433 status.setEnabled(true);
1436 case LFUN_CELL_BACKWARD:
1437 case LFUN_CELL_FORWARD:
1438 status.setEnabled(true);
1442 return InsetMathNest::getStatus(cur, cmd, status);