2 * \file InsetMathGrid.C
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.
13 #include "InsetMathGrid.h"
15 #include "MathParser.h"
16 #include "MathStream.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "FuncStatus.h"
24 #include "funcrequest.h"
28 #include "frontends/Clipboard.h"
29 #include "frontends/Painter.h"
31 #include "insets/mailinset.h"
33 #include "support/lstrings.h"
40 using support::bformat;
50 using std::istringstream;
53 class GridInsetMailer : public MailInset {
55 GridInsetMailer(InsetMathGrid & inset) : inset_(inset) {}
57 virtual string const & name() const
59 static string const theName = "tabular";
63 virtual string const inset2string(Buffer const &) const
65 odocstringstream data;
66 //data << name() << " active_cell " << inset.getActCell() << '\n';
67 data << from_utf8(name()) << " active_cell " << 0 << '\n';
70 return to_utf8(data.str());
74 InsetBase & inset() const { return inset_; }
75 InsetMathGrid & inset_;
81 docstring verboseHLine(int n)
84 for (int i = 0; i < n; ++i)
92 int extractInt(istream & is)
96 return (num == 0) ? 1 : num;
102 //////////////////////////////////////////////////////////////
105 InsetMathGrid::CellInfo::CellInfo()
112 //////////////////////////////////////////////////////////////
115 InsetMathGrid::RowInfo::RowInfo()
116 : lines_(0), skip_(0), allow_pagebreak_(true)
121 int InsetMathGrid::RowInfo::skipPixels() const
123 return crskip_.inBP();
128 //////////////////////////////////////////////////////////////
131 InsetMathGrid::ColInfo::ColInfo()
132 : align_('c'), lines_(0)
136 //////////////////////////////////////////////////////////////
139 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
140 : InsetMathNest(guessColumns(h)),
142 colinfo_(guessColumns(h) + 1),
143 cellinfo_(1 * guessColumns(h))
148 //lyxerr << "created grid with " << ncols() << " columns" << endl;
152 InsetMathGrid::InsetMathGrid()
163 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
164 : InsetMathNest(m * n),
174 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
175 : InsetMathNest(m * n),
187 InsetMathGrid::~InsetMathGrid()
189 GridInsetMailer mailer(*this);
194 auto_ptr<InsetBase> InsetMathGrid::doClone() const
196 return auto_ptr<InsetBase>(new InsetMathGrid(*this));
200 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
202 return col + ncols() * row;
206 void InsetMathGrid::setDefaults()
209 lyxerr << "positive number of columns expected" << endl;
211 // lyxerr << "positive number of rows expected" << endl;
212 for (col_type col = 0; col < ncols(); ++col) {
213 colinfo_[col].align_ = defaultColAlign(col);
214 colinfo_[col].skip_ = defaultColSpace(col);
219 void InsetMathGrid::halign(docstring const & hh)
222 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
225 colinfo_[col].lines_++;
226 } else if (col >= ncols()) {
227 // Only '|' is allowed in the last dummy column
229 } else if (c == 'c' || c == 'l' || c == 'r') {
230 colinfo_[col].align_ = (char)c;
232 colinfo_[col].lines_ = 0;
234 lyxerr << "unknown column separator: '" << c << "'" << endl;
239 col_type n = hh.size();
242 for (col_type col = 0; col < n; ++col)
243 colinfo_[col].align_ = hh[col];
248 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
251 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
252 if (*it == 'c' || *it == 'l' || *it == 'r')
254 // let's have at least one column, even if we did not recognize its
262 void InsetMathGrid::halign(char h, col_type col)
264 colinfo_[col].align_ = h;
268 char InsetMathGrid::halign(col_type col) const
270 return colinfo_[col].align_;
274 docstring InsetMathGrid::halign() const
277 for (col_type col = 0; col < ncols(); ++col) {
278 res += docstring(colinfo_[col].lines_, '|');
279 res += colinfo_[col].align_;
281 return res + docstring(colinfo_[ncols()].lines_, '|');
285 void InsetMathGrid::valign(char c)
291 char InsetMathGrid::valign() const
297 InsetMathGrid::col_type InsetMathGrid::ncols() const
299 return colinfo_.size() - 1;
303 InsetMathGrid::row_type InsetMathGrid::nrows() const
305 return rowinfo_.size() - 1;
309 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
311 return idx % ncols();
315 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
317 return idx / ncols();
321 void InsetMathGrid::vcrskip(LyXLength const & crskip, row_type row)
323 rowinfo_[row].crskip_ = crskip;
327 LyXLength InsetMathGrid::vcrskip(row_type row) const
329 return rowinfo_[row].crskip_;
333 void InsetMathGrid::metrics(MetricsInfo & mi) const
335 // let the cells adjust themselves
336 InsetMathNest::metrics(mi);
338 // compute absolute sizes of vertical structure
339 for (row_type row = 0; row < nrows(); ++row) {
342 for (col_type col = 0; col < ncols(); ++col) {
343 MathArray const & c = cell(index(row, col));
344 asc = max(asc, c.ascent());
345 desc = max(desc, c.descent());
347 rowinfo_[row].ascent_ = asc;
348 rowinfo_[row].descent_ = desc;
350 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
351 rowinfo_[nrows()].ascent_ = 0;
352 rowinfo_[nrows()].descent_ = 0;
354 // compute vertical offsets
355 rowinfo_[0].offset_ = 0;
356 for (row_type row = 1; row <= nrows(); ++row) {
357 rowinfo_[row].offset_ =
358 rowinfo_[row - 1].offset_ +
359 rowinfo_[row - 1].descent_ +
360 rowinfo_[row - 1].skipPixels() +
362 rowinfo_[row].lines_ * hlinesep() +
363 rowinfo_[row].ascent_;
366 // adjust vertical offset
373 h = rowinfo_[nrows() - 1].offset_;
376 h = rowinfo_[nrows() - 1].offset_ / 2;
378 for (row_type row = 0; row <= nrows(); ++row)
379 rowinfo_[row].offset_ -= h;
382 // compute absolute sizes of horizontal structure
383 for (col_type col = 0; col < ncols(); ++col) {
385 for (row_type row = 0; row < nrows(); ++row)
386 wid = max(wid, cell(index(row, col)).width());
387 colinfo_[col].width_ = wid;
389 colinfo_[ncols()].width_ = 0;
391 // compute horizontal offsets
392 colinfo_[0].offset_ = border();
393 for (col_type col = 1; col <= ncols(); ++col) {
394 colinfo_[col].offset_ =
395 colinfo_[col - 1].offset_ +
396 colinfo_[col - 1].width_ +
397 colinfo_[col - 1].skip_ +
399 colinfo_[col].lines_ * vlinesep();
403 dim_.wid = colinfo_[ncols() - 1].offset_
404 + colinfo_[ncols() - 1].width_
405 + vlinesep() * colinfo_[ncols()].lines_
408 dim_.asc = - rowinfo_[0].offset_
409 + rowinfo_[0].ascent_
410 + hlinesep() * rowinfo_[0].lines_
413 dim_.des = rowinfo_[nrows() - 1].offset_
414 + rowinfo_[nrows() - 1].descent_
415 + hlinesep() * rowinfo_[nrows()].lines_
420 // Increase ws_[i] for 'R' columns (except the first one)
421 for (int i = 1; i < nc_; ++i)
422 if (align_[i] == 'R')
423 ws_[i] += 10 * df_width;
424 // Increase ws_[i] for 'C' column
425 if (align_[0] == 'C')
426 if (ws_[0] < 7 * workwidth / 8)
427 ws_[0] = 7 * workwidth / 8;
431 for (cxrow = row_.begin(); cxrow; ++cxrow) {
434 for (int i = 0; i < nc_; ++i) {
436 if (cxrow->getTab(i) <= 0) {
437 cxrow->setTab(i, df_width);
445 lf = (ws_[i] - cxrow->getTab(i))/2;
449 lf = ws_[i] - cxrow->getTab(i);
452 if (cxrow == row_.begin())
454 else if (cxrow.is_last())
455 lf = ws_[i] - cxrow->getTab(i);
457 lf = (ws_[i] - cxrow->getTab(i))/2;
460 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
461 cxrow->setTab(i, lf + rg);
462 rg = ws_[i] - ww + colsep();
463 if (cxrow == row_.begin())
464 width += ws_[i] + colsep();
466 cxrow->setBaseline(cxrow->getBaseline() - ascent);
469 metricsMarkers2(dim_);
473 bool InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
484 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
486 drawWithMargin(pi, x, y, 0, 0);
489 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
490 int lmargin, int rmargin) const
492 for (idx_type idx = 0; idx < nargs(); ++idx)
493 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
494 y + cellYOffset(idx));
496 for (row_type row = 0; row <= nrows(); ++row)
497 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
498 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
499 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
500 pi.pain.line(x + lmargin + 1, yy,
501 x + dim_.width() - rmargin - 1, yy,
505 for (col_type col = 0; col <= ncols(); ++col)
506 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
507 int xx = x + lmargin + colinfo_[col].offset_
508 - i * vlinesep() - vlinesep()/2 - colsep()/2;
509 pi.pain.line(xx, y - dim_.ascent() + 1,
510 xx, y + dim_.descent() - 1,
513 drawMarkers2(pi, x, y);
517 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
519 // let the cells adjust themselves
520 //InsetMathNest::metrics(mi);
521 for (idx_type i = 0; i < nargs(); ++i)
522 cell(i).metricsT(mi, dim);
524 // compute absolute sizes of vertical structure
525 for (row_type row = 0; row < nrows(); ++row) {
528 for (col_type col = 0; col < ncols(); ++col) {
529 MathArray const & c = cell(index(row, col));
530 asc = max(asc, c.ascent());
531 desc = max(desc, c.descent());
533 rowinfo_[row].ascent_ = asc;
534 rowinfo_[row].descent_ = desc;
536 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
537 rowinfo_[nrows()].ascent_ = 0;
538 rowinfo_[nrows()].descent_ = 0;
540 // compute vertical offsets
541 rowinfo_[0].offset_ = 0;
542 for (row_type row = 1; row <= nrows(); ++row) {
543 rowinfo_[row].offset_ =
544 rowinfo_[row - 1].offset_ +
545 rowinfo_[row - 1].descent_ +
546 //rowinfo_[row - 1].skipPixels() +
548 //rowinfo_[row].lines_ * hlinesep() +
549 rowinfo_[row].ascent_;
552 // adjust vertical offset
559 h = rowinfo_[nrows() - 1].offset_;
562 h = rowinfo_[nrows() - 1].offset_ / 2;
564 for (row_type row = 0; row <= nrows(); ++row)
565 rowinfo_[row].offset_ -= h;
568 // compute absolute sizes of horizontal structure
569 for (col_type col = 0; col < ncols(); ++col) {
571 for (row_type row = 0; row < nrows(); ++row)
572 wid = max(wid, cell(index(row, col)).width());
573 colinfo_[col].width_ = wid;
575 colinfo_[ncols()].width_ = 0;
577 // compute horizontal offsets
578 colinfo_[0].offset_ = border();
579 for (col_type col = 1; col <= ncols(); ++col) {
580 colinfo_[col].offset_ =
581 colinfo_[col - 1].offset_ +
582 colinfo_[col - 1].width_ +
583 colinfo_[col - 1].skip_ +
585 //colinfo_[col].lines_ * vlinesep();
589 dim.wid = colinfo_[ncols() - 1].offset_
590 + colinfo_[ncols() - 1].width_
591 //+ vlinesep() * colinfo_[ncols()].lines_
594 dim.asc = -rowinfo_[0].offset_
595 + rowinfo_[0].ascent_
596 //+ hlinesep() * rowinfo_[0].lines_
599 dim.des = rowinfo_[nrows() - 1].offset_
600 + rowinfo_[nrows() - 1].descent_
601 //+ hlinesep() * rowinfo_[nrows()].lines_
606 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
608 for (idx_type idx = 0; idx < nargs(); ++idx)
609 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
613 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
617 if (!rowinfo_[row].crskip_.zero())
618 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
619 else if(!rowinfo_[row].allow_pagebreak_)
622 // make sure an upcoming '[' does not break anything
623 if (row + 1 < nrows()) {
624 MathArray const & c = cell(index(row + 1, 0));
625 if (c.size() && c.front()->getChar() == '[')
630 // only add \\ if necessary
631 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
634 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
638 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
640 if (col + 1 == lastcol)
642 return from_ascii(" & ");
646 void InsetMathGrid::addRow(row_type row)
648 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
650 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
652 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
656 void InsetMathGrid::appendRow()
658 rowinfo_.push_back(RowInfo());
659 //cells_.insert(cells_.end(), ncols(), MathArray());
660 for (col_type col = 0; col < ncols(); ++col) {
661 cells_.push_back(cells_type::value_type());
662 cellinfo_.push_back(CellInfo());
667 void InsetMathGrid::delRow(row_type row)
672 cells_type::iterator it = cells_.begin() + row * ncols();
673 cells_.erase(it, it + ncols());
675 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
676 cellinfo_.erase(jt, jt + ncols());
678 rowinfo_.erase(rowinfo_.begin() + row);
682 void InsetMathGrid::copyRow(row_type row)
685 for (col_type col = 0; col < ncols(); ++col)
686 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
690 void InsetMathGrid::swapRow(row_type row)
694 if (row + 1 == nrows())
696 for (col_type col = 0; col < ncols(); ++col)
697 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
701 void InsetMathGrid::addCol(col_type newcol)
703 const col_type nc = ncols();
704 const row_type nr = nrows();
705 cells_type new_cells((nc + 1) * nr);
706 vector<CellInfo> new_cellinfo((nc + 1) * nr);
708 for (row_type row = 0; row < nr; ++row)
709 for (col_type col = 0; col < nc; ++col) {
710 new_cells[row * (nc + 1) + col + (col > newcol)]
711 = cells_[row * nc + col];
712 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
713 = cellinfo_[row * nc + col];
715 swap(cells_, new_cells);
716 swap(cellinfo_, new_cellinfo);
719 inf.skip_ = defaultColSpace(newcol);
720 inf.align_ = defaultColAlign(newcol);
721 colinfo_.insert(colinfo_.begin() + newcol, inf);
725 void InsetMathGrid::delCol(col_type col)
731 vector<CellInfo> tmpcellinfo;
732 for (col_type i = 0; i < nargs(); ++i)
733 if (i % ncols() != col) {
734 tmpcells.push_back(cells_[i]);
735 tmpcellinfo.push_back(cellinfo_[i]);
737 swap(cells_, tmpcells);
738 swap(cellinfo_, tmpcellinfo);
740 colinfo_.erase(colinfo_.begin() + col);
744 void InsetMathGrid::copyCol(col_type col)
747 for (row_type row = 0; row < nrows(); ++row)
748 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
752 void InsetMathGrid::swapCol(col_type col)
756 if (col + 1 == ncols())
758 for (row_type row = 0; row < nrows(); ++row)
759 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
763 int InsetMathGrid::cellXOffset(idx_type idx) const
765 col_type c = col(idx);
766 int x = colinfo_[c].offset_;
767 char align = colinfo_[c].align_;
768 if (align == 'r' || align == 'R')
769 x += colinfo_[c].width_ - cell(idx).width();
770 if (align == 'c' || align == 'C')
771 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
776 int InsetMathGrid::cellYOffset(idx_type idx) const
778 return rowinfo_[row(idx)].offset_;
782 bool InsetMathGrid::idxUpDown(LCursor & cur, bool up) const
787 cur.idx() -= ncols();
789 if (cur.row() + 1 >= nrows())
791 cur.idx() += ncols();
793 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
798 bool InsetMathGrid::idxLeft(LCursor & cur) const
800 // leave matrix if on the left hand edge
804 cur.pos() = cur.lastpos();
809 bool InsetMathGrid::idxRight(LCursor & cur) const
811 // leave matrix if on the right hand edge
812 if (cur.col() + 1 == ncols())
820 bool InsetMathGrid::idxFirst(LCursor & cur) const
827 cur.idx() = (nrows() - 1) * ncols();
830 cur.idx() = ((nrows() - 1) / 2) * ncols();
837 bool InsetMathGrid::idxLast(LCursor & cur) const
841 cur.idx() = ncols() - 1;
844 cur.idx() = nargs() - 1;
847 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
849 cur.pos() = cur.lastpos();
854 bool InsetMathGrid::idxDelete(idx_type & idx)
856 // nothing to do if we have just one row
860 // nothing to do if we are in the middle of the last row of the inset
861 if (idx + ncols() > nargs())
864 // try to delete entire sequence of ncols() empty cells if possible
865 for (idx_type i = idx; i < idx + ncols(); ++i)
869 // move cells if necessary
870 for (idx_type i = index(row(idx), 0); i < idx; ++i)
871 swap(cell(i), cell(i + ncols()));
878 // undo effect of Ctrl-Tab (i.e. pull next cell)
879 //if (idx + 1 != nargs())
880 // cell(idx).swap(cell(idx + 1));
882 // we handled the event..
887 // reimplement old behaviour when pressing Delete in the last position
889 void InsetMathGrid::idxGlue(idx_type idx)
891 col_type c = col(idx);
892 if (c + 1 == ncols()) {
893 if (row(idx) + 1 != nrows()) {
894 for (col_type cc = 0; cc < ncols(); ++cc)
895 cell(idx).append(cell(idx + cc + 1));
896 delRow(row(idx) + 1);
899 cell(idx).append(cell(idx + 1));
900 for (col_type cc = c + 2; cc < ncols(); ++cc)
901 cell(idx - c + cc - 1) = cell(idx - c + cc);
902 cell(idx - c + ncols() - 1).clear();
907 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
909 return rowinfo_[row];
913 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
915 return rowinfo_[row];
919 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
921 row_type const ri = row(idx);
922 row_type const r1 = min(row(from), row(to));
923 row_type const r2 = max(row(from), row(to));
924 col_type const ci = col(idx);
925 col_type const c1 = min(col(from), col(to));
926 col_type const c2 = max(col(from), col(to));
927 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
932 void InsetMathGrid::normalize(NormalStream & os) const
935 for (row_type row = 0; row < nrows(); ++row) {
937 for (col_type col = 0; col < ncols(); ++col)
938 os << "[cell " << cell(index(row, col)) << ']';
945 void InsetMathGrid::mathmlize(MathStream & os) const
947 os << MTag("mtable");
948 for (row_type row = 0; row < nrows(); ++row) {
950 for (col_type col = 0; col < ncols(); ++col)
951 os << cell(index(row, col));
954 os << ETag("mtable");
958 void InsetMathGrid::write(WriteStream & os) const
961 for (row_type row = 0; row < nrows(); ++row) {
962 os << verboseHLine(rowinfo_[row].lines_);
963 // don't write & and empty cells at end of line
964 col_type lastcol = 0;
965 bool emptyline = true;
966 for (col_type col = 0; col < ncols(); ++col)
967 if (!cell(index(row, col)).empty()) {
971 for (col_type col = 0; col < lastcol; ++col)
972 os << cell(index(row, col)) << eocString(col, lastcol);
973 eol = eolString(row, emptyline, os.fragile());
975 // append newline only if line wasn't completely empty
976 // and this was not the last line in the grid
977 if (!emptyline && row + 1 < nrows())
980 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
992 int InsetMathGrid::colsep() const
998 int InsetMathGrid::rowsep() const
1004 int InsetMathGrid::hlinesep() const
1010 int InsetMathGrid::vlinesep() const
1016 int InsetMathGrid::border() const
1022 void InsetMathGrid::splitCell(LCursor & cur)
1024 if (cur.idx() == cur.lastidx())
1026 MathArray ar = cur.cell();
1027 ar.erase(0, cur.pos());
1028 cur.cell().erase(cur.pos(), cur.lastpos());
1031 cur.cell().insert(0, ar);
1035 void InsetMathGrid::doDispatch(LCursor & cur, FuncRequest & cmd)
1037 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1038 switch (cmd.action) {
1040 case LFUN_MOUSE_RELEASE:
1041 //if (cmd.button() == mouse_button::button3) {
1042 // GridInsetMailer(*this).showDialog();
1043 // return DispatchResult(true, true);
1045 InsetMathNest::doDispatch(cur, cmd);
1048 case LFUN_INSET_DIALOG_UPDATE:
1049 GridInsetMailer(*this).updateDialog(&cur.bv());
1052 // insert file functions
1053 case LFUN_LINE_DELETE:
1054 // FIXME: We use recordUndoInset when a change reflects more
1055 // than one cell, because recordUndo does not work for
1056 // multiple cells. Unfortunately this puts the cursor in front
1057 // of the inset after undo. This is (especilally for large
1059 recordUndoInset(cur);
1060 //autocorrect_ = false;
1068 if (cur.idx() > cur.lastidx())
1069 cur.idx() = cur.lastidx();
1070 if (cur.pos() > cur.lastpos())
1071 cur.pos() = cur.lastpos();
1074 case LFUN_CELL_SPLIT:
1079 case LFUN_CELL_BACKWARD:
1081 cur.selection() = false;
1082 if (!idxPrev(cur)) {
1083 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1088 case LFUN_CELL_FORWARD:
1089 // Can't handle selection by additional 'shift' as this is
1090 // hard bound to LFUN_CELL_BACKWARD
1091 cur.selection() = false;
1092 if (!idxNext(cur)) {
1093 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1098 case LFUN_BREAK_LINE: {
1099 recordUndoInset(cur);
1100 row_type const r = cur.row();
1104 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1105 swap(cell(index(r, c)), cell(index(r + 1, c)));
1109 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1112 cur.pos() = cur.lastpos();
1114 //mathcursor->normalize();
1115 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1119 case LFUN_TABULAR_FEATURE: {
1120 recordUndoInset(cur);
1121 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1122 istringstream is(to_utf8(cmd.argument()));
1125 if (s == "valign-top")
1127 else if (s == "valign-middle")
1129 else if (s == "valign-bottom")
1131 else if (s == "align-left")
1132 halign('l', cur.col());
1133 else if (s == "align-right")
1134 halign('r', cur.col());
1135 else if (s == "align-center")
1136 halign('c', cur.col());
1137 else if (s == "append-row")
1138 for (int i = 0, n = extractInt(is); i < n; ++i)
1140 else if (s == "delete-row") {
1141 for (int i = 0, n = extractInt(is); i < n; ++i) {
1143 if (cur.idx() >= nargs())
1144 cur.idx() -= ncols();
1146 cur.pos() = 0; // trick, see below
1148 else if (s == "copy-row") {
1149 // Here (as later) we save the cursor col/row
1150 // in order to restore it after operation.
1151 row_type const r = cur.row();
1152 col_type const c = cur.col();
1153 for (int i = 0, n = extractInt(is); i < n; ++i)
1155 cur.idx() = index(r, c);
1157 else if (s == "swap-row") {
1159 // Trick to suppress same-idx-means-different-cell
1163 else if (s == "add-hline-above")
1164 rowinfo_[cur.row()].lines_++;
1165 else if (s == "add-hline-below")
1166 rowinfo_[cur.row()+1].lines_++;
1167 else if (s == "delete-hline-above")
1168 rowinfo_[cur.row()].lines_--;
1169 else if (s == "delete-hline-below")
1170 rowinfo_[cur.row()+1].lines_--;
1171 else if (s == "append-column") {
1172 row_type const r = cur.row();
1173 col_type const c = cur.col();
1174 for (int i = 0, n = extractInt(is); i < n; ++i)
1176 cur.idx() = index(r, c);
1178 else if (s == "delete-column") {
1179 row_type const r = cur.row();
1180 col_type const c = cur.col();
1181 for (int i = 0, n = extractInt(is); i < n; ++i)
1182 delCol(col(cur.idx()));
1183 cur.idx() = index(r, min(c, cur.ncols() - 1));
1184 cur.pos() = 0; // trick, see above
1186 else if (s == "copy-column") {
1187 row_type const r = cur.row();
1188 col_type const c = cur.col();
1190 cur.idx() = index(r, c);
1192 else if (s == "swap-column") {
1194 cur.pos() = 0; // trick, see above
1196 else if (s == "add-vline-left")
1197 colinfo_[cur.col()].lines_++;
1198 else if (s == "add-vline-right")
1199 colinfo_[cur.col()+1].lines_++;
1200 else if (s == "delete-vline-left")
1201 colinfo_[cur.col()].lines_--;
1202 else if (s == "delete-vline-right")
1203 colinfo_[cur.col()+1].lines_--;
1208 lyxerr << "returning FINISHED_LEFT" << endl;
1213 cur.message(_("Paste"));
1214 cap::replaceSelection(cur);
1216 if (cmd.argument().empty() && !theClipboard().isInternal())
1217 topaste = theClipboard().getAsText();
1219 idocstringstream is(cmd.argument());
1222 topaste = cap::getSelection(cur.buffer(), n);
1224 InsetMathGrid grid(1, 1);
1225 if (!topaste.empty())
1226 mathed_parse_normal(grid, topaste);
1228 if (grid.nargs() == 1) {
1229 // single cell/part of cell
1231 cur.cell().insert(cur.pos(), grid.cell(0));
1232 cur.pos() += grid.cell(0).size();
1235 recordUndoInset(cur);
1236 col_type const numcols =
1237 min(grid.ncols(), ncols() - col(cur.idx()));
1238 row_type const numrows =
1239 min(grid.nrows(), nrows() - cur.row());
1240 for (row_type r = 0; r < numrows; ++r) {
1241 for (col_type c = 0; c < numcols; ++c) {
1242 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1243 cell(i).insert(0, grid.cell(grid.index(r, c)));
1245 // append the left over horizontal cells to the last column
1246 idx_type i = index(r + cur.row(), ncols() - 1);
1247 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1248 cell(i).append(grid.cell(grid.index(r, c)));
1250 // append the left over vertical cells to the last _cell_
1251 idx_type i = nargs() - 1;
1252 for (row_type r = numrows; r < grid.nrows(); ++r)
1253 for (col_type c = 0; c < grid.ncols(); ++c)
1254 cell(i).append(grid.cell(grid.index(r, c)));
1256 cur.clearSelection(); // bug 393
1257 cur.bv().switchKeyMap();
1262 case LFUN_LINE_BEGIN_SELECT:
1263 case LFUN_LINE_BEGIN:
1264 case LFUN_WORD_BACKWARD_SELECT:
1265 case LFUN_WORD_BACKWARD:
1266 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1267 cmd.action == LFUN_LINE_BEGIN_SELECT);
1268 cur.macroModeClose();
1269 if (cur.pos() != 0) {
1271 } else if (cur.idx() % cur.ncols() != 0) {
1272 cur.idx() -= cur.idx() % cur.ncols();
1274 } else if (cur.idx() != 0) {
1278 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1283 case LFUN_WORD_FORWARD_SELECT:
1284 case LFUN_WORD_FORWARD:
1285 case LFUN_LINE_END_SELECT:
1287 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1288 cmd.action == LFUN_LINE_END_SELECT);
1289 cur.macroModeClose();
1291 if (cur.pos() != cur.lastpos()) {
1292 cur.pos() = cur.lastpos();
1293 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1294 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1295 cur.pos() = cur.lastpos();
1296 } else if (cur.idx() != cur.lastidx()) {
1297 cur.idx() = cur.lastidx();
1298 cur.pos() = cur.lastpos();
1300 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1306 InsetMathNest::doDispatch(cur, cmd);
1311 bool InsetMathGrid::getStatus(LCursor & cur, FuncRequest const & cmd,
1312 FuncStatus & status) const
1314 switch (cmd.action) {
1315 case LFUN_TABULAR_FEATURE: {
1316 string const s = to_utf8(cmd.argument());
1317 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1318 status.enabled(false);
1319 status.message(from_utf8(N_("Only one row")));
1323 (s == "delete-column" || s == "swap-column")) {
1324 status.enabled(false);
1325 status.message(from_utf8(N_("Only one column")));
1328 if ((rowinfo_[cur.row()].lines_ == 0 &&
1329 s == "delete-hline-above") ||
1330 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1331 s == "delete-hline-below")) {
1332 status.enabled(false);
1333 status.message(from_utf8(N_("No hline to delete")));
1337 if ((colinfo_[cur.col()].lines_ == 0 &&
1338 s == "delete-vline-left") ||
1339 (colinfo_[cur.col() + 1].lines_ == 0 &&
1340 s == "delete-vline-right")) {
1341 status.enabled(false);
1342 status.message(from_utf8(N_("No vline to delete")));
1345 if (s == "valign-top" || s == "valign-middle" ||
1346 s == "valign-bottom" || s == "align-left" ||
1347 s == "align-right" || s == "align-center" ||
1348 s == "append-row" || s == "delete-row" ||
1349 s == "copy-row" || s == "swap-row" ||
1350 s == "add-hline-above" || s == "add-hline-below" ||
1351 s == "delete-hline-above" || s == "delete-hline-below" ||
1352 s == "append-column" || s == "delete-column" ||
1353 s == "copy-column" || s == "swap-column" ||
1354 s == "add-vline-left" || s == "add-vline-right" ||
1355 s == "delete-vline-left" || s == "delete-vline-right")
1356 status.enabled(true);
1358 status.enabled(false);
1359 status.message(bformat(
1360 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1363 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1364 || s == "align-right" && halign(cur.col()) == 'r'
1365 || s == "align-center" && halign(cur.col()) == 'c'
1366 || s == "valign-top" && valign() == 't'
1367 || s == "valign-bottom" && valign() == 'b'
1368 || s == "valign-middle" && valign() == 'm');
1371 // FIXME: What did this code do?
1372 // Please check whether it is still needed!
1373 // should be more precise
1374 if (v_align_ == '\0') {
1375 status.enable(true);
1378 if (cmd.argument().empty()) {
1379 status.enable(false);
1382 if (!support::contains("tcb", cmd.argument()[0])) {
1383 status.enable(false);
1386 status.setOnOff(cmd.argument()[0] == v_align_);
1387 status.enabled(true);
1392 case LFUN_CELL_SPLIT:
1393 status.enabled(true);
1396 case LFUN_CELL_BACKWARD:
1397 case LFUN_CELL_FORWARD:
1398 status.enabled(true);
1402 return InsetMathNest::getStatus(cur, cmd, status);