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 "MathStream.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
19 #include "FuncStatus.h"
23 #include "funcrequest.h"
27 #include "frontends/Painter.h"
29 #include "insets/mailinset.h"
31 #include "support/lstrings.h"
38 using support::bformat;
48 using std::istringstream;
52 class GridInsetMailer : public MailInset {
54 GridInsetMailer(InsetMathGrid & inset) : inset_(inset) {}
56 virtual string const & name() const
58 static string const theName = "tabular";
62 virtual string const inset2string(Buffer const &) const
64 odocstringstream data;
65 //data << name() << " active_cell " << inset.getActCell() << '\n';
66 data << from_utf8(name()) << " active_cell " << 0 << '\n';
69 return to_utf8(data.str());
73 InsetBase & inset() const { return inset_; }
74 InsetMathGrid & inset_;
78 void mathed_parse_normal(InsetMathGrid &, string const & argument);
82 docstring verboseHLine(int n)
85 for (int i = 0; i < n; ++i)
93 int extractInt(istream & is)
97 return (num == 0) ? 1 : num;
103 //////////////////////////////////////////////////////////////
106 InsetMathGrid::CellInfo::CellInfo()
113 //////////////////////////////////////////////////////////////
116 InsetMathGrid::RowInfo::RowInfo()
117 : lines_(0), skip_(0)
122 int InsetMathGrid::RowInfo::skipPixels() const
124 return crskip_.inBP();
129 //////////////////////////////////////////////////////////////
132 InsetMathGrid::ColInfo::ColInfo()
133 : align_('c'), lines_(0)
137 //////////////////////////////////////////////////////////////
140 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
141 : InsetMathNest(guessColumns(h)),
143 colinfo_(guessColumns(h) + 1),
144 cellinfo_(1 * guessColumns(h))
149 //lyxerr << "created grid with " << ncols() << " columns" << endl;
153 InsetMathGrid::InsetMathGrid()
164 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
165 : InsetMathNest(m * n),
175 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
176 : InsetMathNest(m * n),
188 InsetMathGrid::~InsetMathGrid()
190 GridInsetMailer mailer(*this);
195 auto_ptr<InsetBase> InsetMathGrid::doClone() const
197 return auto_ptr<InsetBase>(new InsetMathGrid(*this));
201 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
203 return col + ncols() * row;
207 void InsetMathGrid::setDefaults()
210 lyxerr << "positive number of columns expected" << endl;
212 // lyxerr << "positive number of rows expected" << endl;
213 for (col_type col = 0; col < ncols(); ++col) {
214 colinfo_[col].align_ = defaultColAlign(col);
215 colinfo_[col].skip_ = defaultColSpace(col);
220 void InsetMathGrid::halign(docstring const & hh)
223 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
226 colinfo_[col].lines_++;
227 } else if (col >= ncols()) {
228 // Only '|' is allowed in the last dummy column
230 } else if (c == 'c' || c == 'l' || c == 'r') {
231 colinfo_[col].align_ = (char)c;
233 colinfo_[col].lines_ = 0;
235 lyxerr << "unknown column separator: '" << c << "'" << endl;
240 col_type n = hh.size();
243 for (col_type col = 0; col < n; ++col)
244 colinfo_[col].align_ = hh[col];
249 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
252 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
253 if (*it == 'c' || *it == 'l' || *it == 'r')
255 // let's have at least one column, even if we did not recognize its
263 void InsetMathGrid::halign(char h, col_type col)
265 colinfo_[col].align_ = h;
269 char InsetMathGrid::halign(col_type col) const
271 return colinfo_[col].align_;
275 docstring InsetMathGrid::halign() const
278 for (col_type col = 0; col < ncols(); ++col) {
279 res += docstring(colinfo_[col].lines_, '|');
280 res += colinfo_[col].align_;
282 return res + docstring(colinfo_[ncols()].lines_, '|');
286 void InsetMathGrid::valign(char c)
292 char InsetMathGrid::valign() const
298 InsetMathGrid::col_type InsetMathGrid::ncols() const
300 return colinfo_.size() - 1;
304 InsetMathGrid::row_type InsetMathGrid::nrows() const
306 return rowinfo_.size() - 1;
310 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
312 return idx % ncols();
316 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
318 return idx / ncols();
322 void InsetMathGrid::vcrskip(LyXLength const & crskip, row_type row)
324 rowinfo_[row].crskip_ = crskip;
328 LyXLength InsetMathGrid::vcrskip(row_type row) const
330 return rowinfo_[row].crskip_;
334 void InsetMathGrid::metrics(MetricsInfo & mi) const
336 // let the cells adjust themselves
337 InsetMathNest::metrics(mi);
339 // compute absolute sizes of vertical structure
340 for (row_type row = 0; row < nrows(); ++row) {
343 for (col_type col = 0; col < ncols(); ++col) {
344 MathArray const & c = cell(index(row, col));
345 asc = max(asc, c.ascent());
346 desc = max(desc, c.descent());
348 rowinfo_[row].ascent_ = asc;
349 rowinfo_[row].descent_ = desc;
351 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
352 rowinfo_[nrows()].ascent_ = 0;
353 rowinfo_[nrows()].descent_ = 0;
355 // compute vertical offsets
356 rowinfo_[0].offset_ = 0;
357 for (row_type row = 1; row <= nrows(); ++row) {
358 rowinfo_[row].offset_ =
359 rowinfo_[row - 1].offset_ +
360 rowinfo_[row - 1].descent_ +
361 rowinfo_[row - 1].skipPixels() +
363 rowinfo_[row].lines_ * hlinesep() +
364 rowinfo_[row].ascent_;
367 // adjust vertical offset
374 h = rowinfo_[nrows() - 1].offset_;
377 h = rowinfo_[nrows() - 1].offset_ / 2;
379 for (row_type row = 0; row <= nrows(); ++row)
380 rowinfo_[row].offset_ -= h;
383 // compute absolute sizes of horizontal structure
384 for (col_type col = 0; col < ncols(); ++col) {
386 for (row_type row = 0; row < nrows(); ++row)
387 wid = max(wid, cell(index(row, col)).width());
388 colinfo_[col].width_ = wid;
390 colinfo_[ncols()].width_ = 0;
392 // compute horizontal offsets
393 colinfo_[0].offset_ = border();
394 for (col_type col = 1; col <= ncols(); ++col) {
395 colinfo_[col].offset_ =
396 colinfo_[col - 1].offset_ +
397 colinfo_[col - 1].width_ +
398 colinfo_[col - 1].skip_ +
400 colinfo_[col].lines_ * vlinesep();
404 dim_.wid = colinfo_[ncols() - 1].offset_
405 + colinfo_[ncols() - 1].width_
406 + vlinesep() * colinfo_[ncols()].lines_
409 dim_.asc = - rowinfo_[0].offset_
410 + rowinfo_[0].ascent_
411 + hlinesep() * rowinfo_[0].lines_
414 dim_.des = rowinfo_[nrows() - 1].offset_
415 + rowinfo_[nrows() - 1].descent_
416 + hlinesep() * rowinfo_[nrows()].lines_
421 // Increase ws_[i] for 'R' columns (except the first one)
422 for (int i = 1; i < nc_; ++i)
423 if (align_[i] == 'R')
424 ws_[i] += 10 * df_width;
425 // Increase ws_[i] for 'C' column
426 if (align_[0] == 'C')
427 if (ws_[0] < 7 * workwidth / 8)
428 ws_[0] = 7 * workwidth / 8;
432 for (cxrow = row_.begin(); cxrow; ++cxrow) {
435 for (int i = 0; i < nc_; ++i) {
437 if (cxrow->getTab(i) <= 0) {
438 cxrow->setTab(i, df_width);
446 lf = (ws_[i] - cxrow->getTab(i))/2;
450 lf = ws_[i] - cxrow->getTab(i);
453 if (cxrow == row_.begin())
455 else if (cxrow.is_last())
456 lf = ws_[i] - cxrow->getTab(i);
458 lf = (ws_[i] - cxrow->getTab(i))/2;
461 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
462 cxrow->setTab(i, lf + rg);
463 rg = ws_[i] - ww + colsep();
464 if (cxrow == row_.begin())
465 width += ws_[i] + colsep();
467 cxrow->setBaseline(cxrow->getBaseline() - ascent);
470 metricsMarkers2(dim_);
474 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
481 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
483 drawWithMargin(pi, x, y, 0, 0);
486 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
487 int lmargin, int rmargin) const
489 for (idx_type idx = 0; idx < nargs(); ++idx)
490 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
491 y + cellYOffset(idx));
493 for (row_type row = 0; row <= nrows(); ++row)
494 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
495 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
496 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
497 pi.pain.line(x + lmargin + 1, yy,
498 x + dim_.width() - rmargin - 1, yy,
502 for (col_type col = 0; col <= ncols(); ++col)
503 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
504 int xx = x + lmargin + colinfo_[col].offset_
505 - i * vlinesep() - vlinesep()/2 - colsep()/2;
506 pi.pain.line(xx, y - dim_.ascent() + 1,
507 xx, y + dim_.descent() - 1,
510 drawMarkers2(pi, x, y);
514 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
516 // let the cells adjust themselves
517 //InsetMathNest::metrics(mi);
518 for (idx_type i = 0; i < nargs(); ++i)
519 cell(i).metricsT(mi, dim);
521 // compute absolute sizes of vertical structure
522 for (row_type row = 0; row < nrows(); ++row) {
525 for (col_type col = 0; col < ncols(); ++col) {
526 MathArray const & c = cell(index(row, col));
527 asc = max(asc, c.ascent());
528 desc = max(desc, c.descent());
530 rowinfo_[row].ascent_ = asc;
531 rowinfo_[row].descent_ = desc;
533 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
534 rowinfo_[nrows()].ascent_ = 0;
535 rowinfo_[nrows()].descent_ = 0;
537 // compute vertical offsets
538 rowinfo_[0].offset_ = 0;
539 for (row_type row = 1; row <= nrows(); ++row) {
540 rowinfo_[row].offset_ =
541 rowinfo_[row - 1].offset_ +
542 rowinfo_[row - 1].descent_ +
543 //rowinfo_[row - 1].skipPixels() +
545 //rowinfo_[row].lines_ * hlinesep() +
546 rowinfo_[row].ascent_;
549 // adjust vertical offset
556 h = rowinfo_[nrows() - 1].offset_;
559 h = rowinfo_[nrows() - 1].offset_ / 2;
561 for (row_type row = 0; row <= nrows(); ++row)
562 rowinfo_[row].offset_ -= h;
565 // compute absolute sizes of horizontal structure
566 for (col_type col = 0; col < ncols(); ++col) {
568 for (row_type row = 0; row < nrows(); ++row)
569 wid = max(wid, cell(index(row, col)).width());
570 colinfo_[col].width_ = wid;
572 colinfo_[ncols()].width_ = 0;
574 // compute horizontal offsets
575 colinfo_[0].offset_ = border();
576 for (col_type col = 1; col <= ncols(); ++col) {
577 colinfo_[col].offset_ =
578 colinfo_[col - 1].offset_ +
579 colinfo_[col - 1].width_ +
580 colinfo_[col - 1].skip_ +
582 //colinfo_[col].lines_ * vlinesep();
586 dim.wid = colinfo_[ncols() - 1].offset_
587 + colinfo_[ncols() - 1].width_
588 //+ vlinesep() * colinfo_[ncols()].lines_
591 dim.asc = -rowinfo_[0].offset_
592 + rowinfo_[0].ascent_
593 //+ hlinesep() * rowinfo_[0].lines_
596 dim.des = rowinfo_[nrows() - 1].offset_
597 + rowinfo_[nrows() - 1].descent_
598 //+ hlinesep() * rowinfo_[nrows()].lines_
603 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
605 for (idx_type idx = 0; idx < nargs(); ++idx)
606 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
610 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
614 if (!rowinfo_[row].crskip_.zero())
615 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
617 // make sure an upcoming '[' does not break anything
618 if (row + 1 < nrows()) {
619 MathArray const & c = cell(index(row + 1, 0));
620 if (c.size() && c.front()->getChar() == '[')
625 // only add \\ if necessary
626 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
629 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
633 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
635 if (col + 1 == lastcol)
637 return from_ascii(" & ");
641 void InsetMathGrid::addRow(row_type row)
643 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
645 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
647 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
651 void InsetMathGrid::appendRow()
653 rowinfo_.push_back(RowInfo());
654 //cells_.insert(cells_.end(), ncols(), MathArray());
655 for (col_type col = 0; col < ncols(); ++col) {
656 cells_.push_back(cells_type::value_type());
657 cellinfo_.push_back(CellInfo());
662 void InsetMathGrid::delRow(row_type row)
667 cells_type::iterator it = cells_.begin() + row * ncols();
668 cells_.erase(it, it + ncols());
670 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
671 cellinfo_.erase(jt, jt + ncols());
673 rowinfo_.erase(rowinfo_.begin() + row);
677 void InsetMathGrid::copyRow(row_type row)
680 for (col_type col = 0; col < ncols(); ++col)
681 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
685 void InsetMathGrid::swapRow(row_type row)
689 if (row + 1 == nrows())
691 for (col_type col = 0; col < ncols(); ++col)
692 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
696 void InsetMathGrid::addCol(col_type newcol)
698 const col_type nc = ncols();
699 const row_type nr = nrows();
700 cells_type new_cells((nc + 1) * nr);
701 vector<CellInfo> new_cellinfo((nc + 1) * nr);
703 for (row_type row = 0; row < nr; ++row)
704 for (col_type col = 0; col < nc; ++col) {
705 new_cells[row * (nc + 1) + col + (col > newcol)]
706 = cells_[row * nc + col];
707 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
708 = cellinfo_[row * nc + col];
710 swap(cells_, new_cells);
711 swap(cellinfo_, new_cellinfo);
714 inf.skip_ = defaultColSpace(newcol);
715 inf.align_ = defaultColAlign(newcol);
716 colinfo_.insert(colinfo_.begin() + newcol, inf);
720 void InsetMathGrid::delCol(col_type col)
726 vector<CellInfo> tmpcellinfo;
727 for (col_type i = 0; i < nargs(); ++i)
728 if (i % ncols() != col) {
729 tmpcells.push_back(cells_[i]);
730 tmpcellinfo.push_back(cellinfo_[i]);
732 swap(cells_, tmpcells);
733 swap(cellinfo_, tmpcellinfo);
735 colinfo_.erase(colinfo_.begin() + col);
739 void InsetMathGrid::copyCol(col_type col)
742 for (row_type row = 0; row < nrows(); ++row)
743 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
747 void InsetMathGrid::swapCol(col_type col)
751 if (col + 1 == ncols())
753 for (row_type row = 0; row < nrows(); ++row)
754 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
758 int InsetMathGrid::cellXOffset(idx_type idx) const
760 col_type c = col(idx);
761 int x = colinfo_[c].offset_;
762 char align = colinfo_[c].align_;
763 if (align == 'r' || align == 'R')
764 x += colinfo_[c].width_ - cell(idx).width();
765 if (align == 'c' || align == 'C')
766 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
771 int InsetMathGrid::cellYOffset(idx_type idx) const
773 return rowinfo_[row(idx)].offset_;
777 bool InsetMathGrid::idxUpDown(LCursor & cur, bool up) const
782 cur.idx() -= ncols();
784 if (cur.row() + 1 >= nrows())
786 cur.idx() += ncols();
788 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
793 bool InsetMathGrid::idxLeft(LCursor & cur) const
795 // leave matrix if on the left hand edge
799 cur.pos() = cur.lastpos();
804 bool InsetMathGrid::idxRight(LCursor & cur) const
806 // leave matrix if on the right hand edge
807 if (cur.col() + 1 == ncols())
815 bool InsetMathGrid::idxFirst(LCursor & cur) const
822 cur.idx() = (nrows() - 1) * ncols();
825 cur.idx() = ((nrows() - 1) / 2) * ncols();
832 bool InsetMathGrid::idxLast(LCursor & cur) const
836 cur.idx() = ncols() - 1;
839 cur.idx() = nargs() - 1;
842 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
844 cur.pos() = cur.lastpos();
849 bool InsetMathGrid::idxDelete(idx_type & idx)
851 // nothing to do if we have just one row
855 // nothing to do if we are in the middle of the last row of the inset
856 if (idx + ncols() > nargs())
859 // try to delete entire sequence of ncols() empty cells if possible
860 for (idx_type i = idx; i < idx + ncols(); ++i)
864 // move cells if necessary
865 for (idx_type i = index(row(idx), 0); i < idx; ++i)
866 swap(cell(i), cell(i + ncols()));
873 // undo effect of Ctrl-Tab (i.e. pull next cell)
874 //if (idx + 1 != nargs())
875 // cell(idx).swap(cell(idx + 1));
877 // we handled the event..
882 // reimplement old behaviour when pressing Delete in the last position
884 void InsetMathGrid::idxGlue(idx_type idx)
886 col_type c = col(idx);
887 if (c + 1 == ncols()) {
888 if (row(idx) + 1 != nrows()) {
889 for (col_type cc = 0; cc < ncols(); ++cc)
890 cell(idx).append(cell(idx + cc + 1));
891 delRow(row(idx) + 1);
894 cell(idx).append(cell(idx + 1));
895 for (col_type cc = c + 2; cc < ncols(); ++cc)
896 cell(idx - c + cc - 1) = cell(idx - c + cc);
897 cell(idx - c + ncols() - 1).clear();
902 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
904 return rowinfo_[row];
908 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
910 return rowinfo_[row];
914 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
916 row_type const ri = row(idx);
917 row_type const r1 = min(row(from), row(to));
918 row_type const r2 = max(row(from), row(to));
919 col_type const ci = col(idx);
920 col_type const c1 = min(col(from), col(to));
921 col_type const c2 = max(col(from), col(to));
922 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
927 void InsetMathGrid::normalize(NormalStream & os) const
930 for (row_type row = 0; row < nrows(); ++row) {
932 for (col_type col = 0; col < ncols(); ++col)
933 os << "[cell " << cell(index(row, col)) << ']';
940 void InsetMathGrid::mathmlize(MathStream & os) const
942 os << MTag("mtable");
943 for (row_type row = 0; row < nrows(); ++row) {
945 for (col_type col = 0; col < ncols(); ++col)
946 os << cell(index(row, col));
949 os << ETag("mtable");
953 void InsetMathGrid::write(WriteStream & os) const
956 for (row_type row = 0; row < nrows(); ++row) {
957 os << verboseHLine(rowinfo_[row].lines_);
958 // don't write & and empty cells at end of line
959 col_type lastcol = 0;
960 bool emptyline = true;
961 for (col_type col = 0; col < ncols(); ++col)
962 if (!cell(index(row, col)).empty()) {
966 for (col_type col = 0; col < lastcol; ++col)
967 os << cell(index(row, col)) << eocString(col, lastcol);
968 eol = eolString(row, emptyline, os.fragile());
970 // append newline only if line wasn't completely empty
971 // and this was not the last line in the grid
972 if (!emptyline && row + 1 < nrows())
975 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
987 int InsetMathGrid::colsep() const
993 int InsetMathGrid::rowsep() const
999 int InsetMathGrid::hlinesep() const
1005 int InsetMathGrid::vlinesep() const
1011 int InsetMathGrid::border() const
1017 void InsetMathGrid::splitCell(LCursor & cur)
1019 if (cur.idx() == cur.lastidx())
1021 MathArray ar = cur.cell();
1022 ar.erase(0, cur.pos());
1023 cur.cell().erase(cur.pos(), cur.lastpos());
1026 cur.cell().insert(0, ar);
1030 void InsetMathGrid::doDispatch(LCursor & cur, FuncRequest & cmd)
1032 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1033 switch (cmd.action) {
1035 case LFUN_MOUSE_RELEASE:
1036 //if (cmd.button() == mouse_button::button3) {
1037 // GridInsetMailer(*this).showDialog();
1038 // return DispatchResult(true, true);
1040 InsetMathNest::doDispatch(cur, cmd);
1043 case LFUN_INSET_DIALOG_UPDATE:
1044 GridInsetMailer(*this).updateDialog(&cur.bv());
1047 // insert file functions
1048 case LFUN_LINE_DELETE:
1049 // FIXME: We use recordUndoInset when a change reflects more
1050 // than one cell, because recordUndo does not work for
1051 // multiple cells. Unfortunately this puts the cursor in front
1052 // of the inset after undo. This is (especilally for large
1054 recordUndoInset(cur);
1055 //autocorrect_ = false;
1063 if (cur.idx() > cur.lastidx())
1064 cur.idx() = cur.lastidx();
1065 if (cur.pos() > cur.lastpos())
1066 cur.pos() = cur.lastpos();
1069 case LFUN_CELL_SPLIT:
1074 case LFUN_CELL_BACKWARD:
1076 cur.selection() = false;
1077 if (!idxPrev(cur)) {
1078 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1083 case LFUN_CELL_FORWARD:
1084 // Can't handle selection by additional 'shift' as this is
1085 // hard bound to LFUN_CELL_BACKWARD
1086 cur.selection() = false;
1087 if (!idxNext(cur)) {
1088 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1093 case LFUN_BREAK_LINE: {
1094 recordUndoInset(cur);
1095 row_type const r = cur.row();
1099 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1100 swap(cell(index(r, c)), cell(index(r + 1, c)));
1104 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1107 cur.pos() = cur.lastpos();
1109 //mathcursor->normalize();
1110 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1114 case LFUN_TABULAR_FEATURE: {
1115 recordUndoInset(cur);
1116 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1117 istringstream is(to_utf8(cmd.argument()));
1120 if (s == "valign-top")
1122 else if (s == "valign-middle")
1124 else if (s == "valign-bottom")
1126 else if (s == "align-left")
1127 halign('l', cur.col());
1128 else if (s == "align-right")
1129 halign('r', cur.col());
1130 else if (s == "align-center")
1131 halign('c', cur.col());
1132 else if (s == "append-row")
1133 for (int i = 0, n = extractInt(is); i < n; ++i)
1135 else if (s == "delete-row") {
1136 for (int i = 0, n = extractInt(is); i < n; ++i) {
1138 if (cur.idx() >= nargs())
1139 cur.idx() -= ncols();
1141 cur.pos() = 0; // trick, see below
1143 else if (s == "copy-row") {
1144 // Here (as later) we save the cursor col/row
1145 // in order to restore it after operation.
1146 row_type const r = cur.row();
1147 col_type const c = cur.col();
1148 for (int i = 0, n = extractInt(is); i < n; ++i)
1150 cur.idx() = index(r, c);
1152 else if (s == "swap-row") {
1154 // Trick to suppress same-idx-means-different-cell
1158 else if (s == "add-hline-above")
1159 rowinfo_[cur.row()].lines_++;
1160 else if (s == "add-hline-below")
1161 rowinfo_[cur.row()+1].lines_++;
1162 else if (s == "delete-hline-above")
1163 rowinfo_[cur.row()].lines_--;
1164 else if (s == "delete-hline-below")
1165 rowinfo_[cur.row()+1].lines_--;
1166 else if (s == "append-column") {
1167 row_type const r = cur.row();
1168 col_type const c = cur.col();
1169 for (int i = 0, n = extractInt(is); i < n; ++i)
1171 cur.idx() = index(r, c);
1173 else if (s == "delete-column") {
1174 row_type const r = cur.row();
1175 col_type const c = cur.col();
1176 for (int i = 0, n = extractInt(is); i < n; ++i)
1177 delCol(col(cur.idx()));
1178 cur.idx() = index(r, min(c, cur.ncols() - 1));
1179 cur.pos() = 0; // trick, see above
1181 else if (s == "copy-column") {
1182 row_type const r = cur.row();
1183 col_type const c = cur.col();
1185 cur.idx() = index(r, c);
1187 else if (s == "swap-column") {
1189 cur.pos() = 0; // trick, see above
1191 else if (s == "add-vline-left")
1192 colinfo_[cur.col()].lines_++;
1193 else if (s == "add-vline-right")
1194 colinfo_[cur.col()+1].lines_++;
1195 else if (s == "delete-vline-left")
1196 colinfo_[cur.col()].lines_--;
1197 else if (s == "delete-vline-right")
1198 colinfo_[cur.col()+1].lines_--;
1203 lyxerr << "returning FINISHED_LEFT" << endl;
1208 cur.message(_("Paste"));
1209 cap::replaceSelection(cur);
1210 istringstream is(to_utf8(cmd.argument()));
1213 InsetMathGrid grid(1, 1);
1215 mathed_parse_normal(grid,
1216 to_utf8(lyx::cap::getSelection(cur.buffer(), n)));
1217 if (grid.nargs() == 1) {
1218 // single cell/part of cell
1220 cur.cell().insert(cur.pos(), grid.cell(0));
1221 cur.pos() += grid.cell(0).size();
1224 recordUndoInset(cur);
1225 col_type const numcols =
1226 min(grid.ncols(), ncols() - col(cur.idx()));
1227 row_type const numrows =
1228 min(grid.nrows(), nrows() - cur.row());
1229 for (row_type r = 0; r < numrows; ++r) {
1230 for (col_type c = 0; c < numcols; ++c) {
1231 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1232 cell(i).insert(0, grid.cell(grid.index(r, c)));
1234 // append the left over horizontal cells to the last column
1235 idx_type i = index(r + cur.row(), ncols() - 1);
1236 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1237 cell(i).append(grid.cell(grid.index(r, c)));
1239 // append the left over vertical cells to the last _cell_
1240 idx_type i = nargs() - 1;
1241 for (row_type r = numrows; r < grid.nrows(); ++r)
1242 for (col_type c = 0; c < grid.ncols(); ++c)
1243 cell(i).append(grid.cell(grid.index(r, c)));
1245 cur.clearSelection(); // bug 393
1246 cur.bv().switchKeyMap();
1251 case LFUN_LINE_BEGIN_SELECT:
1252 case LFUN_LINE_BEGIN:
1253 case LFUN_WORD_BACKWARD_SELECT:
1254 case LFUN_WORD_BACKWARD:
1255 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1256 cmd.action == LFUN_LINE_BEGIN_SELECT);
1257 cur.macroModeClose();
1258 if (cur.pos() != 0) {
1260 } else if (cur.idx() % cur.ncols() != 0) {
1261 cur.idx() -= cur.idx() % cur.ncols();
1263 } else if (cur.idx() != 0) {
1267 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1272 case LFUN_WORD_FORWARD_SELECT:
1273 case LFUN_WORD_FORWARD:
1274 case LFUN_LINE_END_SELECT:
1276 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1277 cmd.action == LFUN_LINE_END_SELECT);
1278 cur.macroModeClose();
1280 if (cur.pos() != cur.lastpos()) {
1281 cur.pos() = cur.lastpos();
1282 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1283 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1284 cur.pos() = cur.lastpos();
1285 } else if (cur.idx() != cur.lastidx()) {
1286 cur.idx() = cur.lastidx();
1287 cur.pos() = cur.lastpos();
1289 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1295 InsetMathNest::doDispatch(cur, cmd);
1300 bool InsetMathGrid::getStatus(LCursor & cur, FuncRequest const & cmd,
1301 FuncStatus & status) const
1303 switch (cmd.action) {
1304 case LFUN_TABULAR_FEATURE: {
1305 string const s = to_utf8(cmd.argument());
1306 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1307 status.enabled(false);
1308 status.message(from_utf8(N_("Only one row")));
1312 (s == "delete-column" || s == "swap-column")) {
1313 status.enabled(false);
1314 status.message(from_utf8(N_("Only one column")));
1317 if ((rowinfo_[cur.row()].lines_ == 0 &&
1318 s == "delete-hline-above") ||
1319 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1320 s == "delete-hline-below")) {
1321 status.enabled(false);
1322 status.message(from_utf8(N_("No hline to delete")));
1326 if ((colinfo_[cur.col()].lines_ == 0 &&
1327 s == "delete-vline-left") ||
1328 (colinfo_[cur.col() + 1].lines_ == 0 &&
1329 s == "delete-vline-right")) {
1330 status.enabled(false);
1331 status.message(from_utf8(N_("No vline to delete")));
1334 if (s == "valign-top" || s == "valign-middle" ||
1335 s == "valign-bottom" || s == "align-left" ||
1336 s == "align-right" || s == "align-center" ||
1337 s == "append-row" || s == "delete-row" ||
1338 s == "copy-row" || s == "swap-row" ||
1339 s == "add-hline-above" || s == "add-hline-below" ||
1340 s == "delete-hline-above" || s == "delete-hline-below" ||
1341 s == "append-column" || s == "delete-column" ||
1342 s == "copy-column" || s == "swap-column" ||
1343 s == "add-vline-left" || s == "add-vline-right" ||
1344 s == "delete-vline-left" || s == "delete-vline-right")
1345 status.enabled(true);
1347 status.enabled(false);
1348 status.message(bformat(
1349 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1352 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1353 || s == "align-right" && halign(cur.col()) == 'r'
1354 || s == "align-center" && halign(cur.col()) == 'c'
1355 || s == "valign-top" && valign() == 't'
1356 || s == "valign-bottom" && valign() == 'b'
1357 || s == "valign-middle" && valign() == 'm');
1360 // FIXME: What did this code do?
1361 // Please check whether it is still needed!
1362 // should be more precise
1363 if (v_align_ == '\0') {
1364 status.enable(true);
1367 if (cmd.argument().empty()) {
1368 status.enable(false);
1371 if (!support::contains("tcb", cmd.argument()[0])) {
1372 status.enable(false);
1375 status.setOnOff(cmd.argument()[0] == v_align_);
1376 status.enabled(true);
1381 case LFUN_CELL_SPLIT:
1382 status.enabled(true);
1385 case LFUN_CELL_BACKWARD:
1386 case LFUN_CELL_FORWARD:
1387 status.enabled(true);
1391 return InsetMathNest::getStatus(cur, cmd, status);