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;
54 class GridInsetMailer : public MailInset {
56 GridInsetMailer(InsetMathGrid & inset) : inset_(inset) {}
58 virtual string const & name() const
60 static string const theName = "tabular";
64 virtual string const inset2string(Buffer const &) const
66 odocstringstream data;
67 //data << name() << " active_cell " << inset.getActCell() << '\n';
68 data << from_utf8(name()) << " active_cell " << 0 << '\n';
71 return to_utf8(data.str());
75 InsetBase & inset() const { return inset_; }
76 InsetMathGrid & inset_;
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 bool InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
485 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
487 drawWithMargin(pi, x, y, 0, 0);
490 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
491 int lmargin, int rmargin) const
493 for (idx_type idx = 0; idx < nargs(); ++idx)
494 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
495 y + cellYOffset(idx));
497 for (row_type row = 0; row <= nrows(); ++row)
498 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
499 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
500 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
501 pi.pain.line(x + lmargin + 1, yy,
502 x + dim_.width() - rmargin - 1, yy,
506 for (col_type col = 0; col <= ncols(); ++col)
507 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
508 int xx = x + lmargin + colinfo_[col].offset_
509 - i * vlinesep() - vlinesep()/2 - colsep()/2;
510 pi.pain.line(xx, y - dim_.ascent() + 1,
511 xx, y + dim_.descent() - 1,
514 drawMarkers2(pi, x, y);
518 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
520 // let the cells adjust themselves
521 //InsetMathNest::metrics(mi);
522 for (idx_type i = 0; i < nargs(); ++i)
523 cell(i).metricsT(mi, dim);
525 // compute absolute sizes of vertical structure
526 for (row_type row = 0; row < nrows(); ++row) {
529 for (col_type col = 0; col < ncols(); ++col) {
530 MathArray const & c = cell(index(row, col));
531 asc = max(asc, c.ascent());
532 desc = max(desc, c.descent());
534 rowinfo_[row].ascent_ = asc;
535 rowinfo_[row].descent_ = desc;
537 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
538 rowinfo_[nrows()].ascent_ = 0;
539 rowinfo_[nrows()].descent_ = 0;
541 // compute vertical offsets
542 rowinfo_[0].offset_ = 0;
543 for (row_type row = 1; row <= nrows(); ++row) {
544 rowinfo_[row].offset_ =
545 rowinfo_[row - 1].offset_ +
546 rowinfo_[row - 1].descent_ +
547 //rowinfo_[row - 1].skipPixels() +
549 //rowinfo_[row].lines_ * hlinesep() +
550 rowinfo_[row].ascent_;
553 // adjust vertical offset
560 h = rowinfo_[nrows() - 1].offset_;
563 h = rowinfo_[nrows() - 1].offset_ / 2;
565 for (row_type row = 0; row <= nrows(); ++row)
566 rowinfo_[row].offset_ -= h;
569 // compute absolute sizes of horizontal structure
570 for (col_type col = 0; col < ncols(); ++col) {
572 for (row_type row = 0; row < nrows(); ++row)
573 wid = max(wid, cell(index(row, col)).width());
574 colinfo_[col].width_ = wid;
576 colinfo_[ncols()].width_ = 0;
578 // compute horizontal offsets
579 colinfo_[0].offset_ = border();
580 for (col_type col = 1; col <= ncols(); ++col) {
581 colinfo_[col].offset_ =
582 colinfo_[col - 1].offset_ +
583 colinfo_[col - 1].width_ +
584 colinfo_[col - 1].skip_ +
586 //colinfo_[col].lines_ * vlinesep();
590 dim.wid = colinfo_[ncols() - 1].offset_
591 + colinfo_[ncols() - 1].width_
592 //+ vlinesep() * colinfo_[ncols()].lines_
595 dim.asc = -rowinfo_[0].offset_
596 + rowinfo_[0].ascent_
597 //+ hlinesep() * rowinfo_[0].lines_
600 dim.des = rowinfo_[nrows() - 1].offset_
601 + rowinfo_[nrows() - 1].descent_
602 //+ hlinesep() * rowinfo_[nrows()].lines_
607 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
609 for (idx_type idx = 0; idx < nargs(); ++idx)
610 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
614 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
618 if (!rowinfo_[row].crskip_.zero())
619 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
621 // make sure an upcoming '[' does not break anything
622 if (row + 1 < nrows()) {
623 MathArray const & c = cell(index(row + 1, 0));
624 if (c.size() && c.front()->getChar() == '[')
629 // only add \\ if necessary
630 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
633 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
637 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
639 if (col + 1 == lastcol)
641 return from_ascii(" & ");
645 void InsetMathGrid::addRow(row_type row)
647 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
649 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
651 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
655 void InsetMathGrid::appendRow()
657 rowinfo_.push_back(RowInfo());
658 //cells_.insert(cells_.end(), ncols(), MathArray());
659 for (col_type col = 0; col < ncols(); ++col) {
660 cells_.push_back(cells_type::value_type());
661 cellinfo_.push_back(CellInfo());
666 void InsetMathGrid::delRow(row_type row)
671 cells_type::iterator it = cells_.begin() + row * ncols();
672 cells_.erase(it, it + ncols());
674 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
675 cellinfo_.erase(jt, jt + ncols());
677 rowinfo_.erase(rowinfo_.begin() + row);
681 void InsetMathGrid::copyRow(row_type row)
684 for (col_type col = 0; col < ncols(); ++col)
685 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
689 void InsetMathGrid::swapRow(row_type row)
693 if (row + 1 == nrows())
695 for (col_type col = 0; col < ncols(); ++col)
696 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
700 void InsetMathGrid::addCol(col_type newcol)
702 const col_type nc = ncols();
703 const row_type nr = nrows();
704 cells_type new_cells((nc + 1) * nr);
705 vector<CellInfo> new_cellinfo((nc + 1) * nr);
707 for (row_type row = 0; row < nr; ++row)
708 for (col_type col = 0; col < nc; ++col) {
709 new_cells[row * (nc + 1) + col + (col > newcol)]
710 = cells_[row * nc + col];
711 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
712 = cellinfo_[row * nc + col];
714 swap(cells_, new_cells);
715 swap(cellinfo_, new_cellinfo);
718 inf.skip_ = defaultColSpace(newcol);
719 inf.align_ = defaultColAlign(newcol);
720 colinfo_.insert(colinfo_.begin() + newcol, inf);
724 void InsetMathGrid::delCol(col_type col)
730 vector<CellInfo> tmpcellinfo;
731 for (col_type i = 0; i < nargs(); ++i)
732 if (i % ncols() != col) {
733 tmpcells.push_back(cells_[i]);
734 tmpcellinfo.push_back(cellinfo_[i]);
736 swap(cells_, tmpcells);
737 swap(cellinfo_, tmpcellinfo);
739 colinfo_.erase(colinfo_.begin() + col);
743 void InsetMathGrid::copyCol(col_type col)
746 for (row_type row = 0; row < nrows(); ++row)
747 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
751 void InsetMathGrid::swapCol(col_type col)
755 if (col + 1 == ncols())
757 for (row_type row = 0; row < nrows(); ++row)
758 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
762 int InsetMathGrid::cellXOffset(idx_type idx) const
764 col_type c = col(idx);
765 int x = colinfo_[c].offset_;
766 char align = colinfo_[c].align_;
767 if (align == 'r' || align == 'R')
768 x += colinfo_[c].width_ - cell(idx).width();
769 if (align == 'c' || align == 'C')
770 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
775 int InsetMathGrid::cellYOffset(idx_type idx) const
777 return rowinfo_[row(idx)].offset_;
781 bool InsetMathGrid::idxUpDown(LCursor & cur, bool up) const
786 cur.idx() -= ncols();
788 if (cur.row() + 1 >= nrows())
790 cur.idx() += ncols();
792 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
797 bool InsetMathGrid::idxLeft(LCursor & cur) const
799 // leave matrix if on the left hand edge
803 cur.pos() = cur.lastpos();
808 bool InsetMathGrid::idxRight(LCursor & cur) const
810 // leave matrix if on the right hand edge
811 if (cur.col() + 1 == ncols())
819 bool InsetMathGrid::idxFirst(LCursor & cur) const
826 cur.idx() = (nrows() - 1) * ncols();
829 cur.idx() = ((nrows() - 1) / 2) * ncols();
836 bool InsetMathGrid::idxLast(LCursor & cur) const
840 cur.idx() = ncols() - 1;
843 cur.idx() = nargs() - 1;
846 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
848 cur.pos() = cur.lastpos();
853 bool InsetMathGrid::idxDelete(idx_type & idx)
855 // nothing to do if we have just one row
859 // nothing to do if we are in the middle of the last row of the inset
860 if (idx + ncols() > nargs())
863 // try to delete entire sequence of ncols() empty cells if possible
864 for (idx_type i = idx; i < idx + ncols(); ++i)
868 // move cells if necessary
869 for (idx_type i = index(row(idx), 0); i < idx; ++i)
870 swap(cell(i), cell(i + ncols()));
877 // undo effect of Ctrl-Tab (i.e. pull next cell)
878 //if (idx + 1 != nargs())
879 // cell(idx).swap(cell(idx + 1));
881 // we handled the event..
886 // reimplement old behaviour when pressing Delete in the last position
888 void InsetMathGrid::idxGlue(idx_type idx)
890 col_type c = col(idx);
891 if (c + 1 == ncols()) {
892 if (row(idx) + 1 != nrows()) {
893 for (col_type cc = 0; cc < ncols(); ++cc)
894 cell(idx).append(cell(idx + cc + 1));
895 delRow(row(idx) + 1);
898 cell(idx).append(cell(idx + 1));
899 for (col_type cc = c + 2; cc < ncols(); ++cc)
900 cell(idx - c + cc - 1) = cell(idx - c + cc);
901 cell(idx - c + ncols() - 1).clear();
906 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
908 return rowinfo_[row];
912 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
914 return rowinfo_[row];
918 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
920 row_type const ri = row(idx);
921 row_type const r1 = min(row(from), row(to));
922 row_type const r2 = max(row(from), row(to));
923 col_type const ci = col(idx);
924 col_type const c1 = min(col(from), col(to));
925 col_type const c2 = max(col(from), col(to));
926 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
931 void InsetMathGrid::normalize(NormalStream & os) const
934 for (row_type row = 0; row < nrows(); ++row) {
936 for (col_type col = 0; col < ncols(); ++col)
937 os << "[cell " << cell(index(row, col)) << ']';
944 void InsetMathGrid::mathmlize(MathStream & os) const
946 os << MTag("mtable");
947 for (row_type row = 0; row < nrows(); ++row) {
949 for (col_type col = 0; col < ncols(); ++col)
950 os << cell(index(row, col));
953 os << ETag("mtable");
957 void InsetMathGrid::write(WriteStream & os) const
960 for (row_type row = 0; row < nrows(); ++row) {
961 os << verboseHLine(rowinfo_[row].lines_);
962 // don't write & and empty cells at end of line
963 col_type lastcol = 0;
964 bool emptyline = true;
965 for (col_type col = 0; col < ncols(); ++col)
966 if (!cell(index(row, col)).empty()) {
970 for (col_type col = 0; col < lastcol; ++col)
971 os << cell(index(row, col)) << eocString(col, lastcol);
972 eol = eolString(row, emptyline, os.fragile());
974 // append newline only if line wasn't completely empty
975 // and this was not the last line in the grid
976 if (!emptyline && row + 1 < nrows())
979 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
991 int InsetMathGrid::colsep() const
997 int InsetMathGrid::rowsep() const
1003 int InsetMathGrid::hlinesep() const
1009 int InsetMathGrid::vlinesep() const
1015 int InsetMathGrid::border() const
1021 void InsetMathGrid::splitCell(LCursor & cur)
1023 if (cur.idx() == cur.lastidx())
1025 MathArray ar = cur.cell();
1026 ar.erase(0, cur.pos());
1027 cur.cell().erase(cur.pos(), cur.lastpos());
1030 cur.cell().insert(0, ar);
1034 void InsetMathGrid::doDispatch(LCursor & cur, FuncRequest & cmd)
1036 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1037 switch (cmd.action) {
1039 case LFUN_MOUSE_RELEASE:
1040 //if (cmd.button() == mouse_button::button3) {
1041 // GridInsetMailer(*this).showDialog();
1042 // return DispatchResult(true, true);
1044 InsetMathNest::doDispatch(cur, cmd);
1047 case LFUN_INSET_DIALOG_UPDATE:
1048 GridInsetMailer(*this).updateDialog(&cur.bv());
1051 // insert file functions
1052 case LFUN_LINE_DELETE:
1053 // FIXME: We use recordUndoInset when a change reflects more
1054 // than one cell, because recordUndo does not work for
1055 // multiple cells. Unfortunately this puts the cursor in front
1056 // of the inset after undo. This is (especilally for large
1058 recordUndoInset(cur);
1059 //autocorrect_ = false;
1067 if (cur.idx() > cur.lastidx())
1068 cur.idx() = cur.lastidx();
1069 if (cur.pos() > cur.lastpos())
1070 cur.pos() = cur.lastpos();
1073 case LFUN_CELL_SPLIT:
1078 case LFUN_CELL_BACKWARD:
1080 cur.selection() = false;
1081 if (!idxPrev(cur)) {
1082 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1087 case LFUN_CELL_FORWARD:
1088 // Can't handle selection by additional 'shift' as this is
1089 // hard bound to LFUN_CELL_BACKWARD
1090 cur.selection() = false;
1091 if (!idxNext(cur)) {
1092 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1097 case LFUN_BREAK_LINE: {
1098 recordUndoInset(cur);
1099 row_type const r = cur.row();
1103 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1104 swap(cell(index(r, c)), cell(index(r + 1, c)));
1108 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1111 cur.pos() = cur.lastpos();
1113 //mathcursor->normalize();
1114 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1118 case LFUN_TABULAR_FEATURE: {
1119 recordUndoInset(cur);
1120 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1121 istringstream is(to_utf8(cmd.argument()));
1124 if (s == "valign-top")
1126 else if (s == "valign-middle")
1128 else if (s == "valign-bottom")
1130 else if (s == "align-left")
1131 halign('l', cur.col());
1132 else if (s == "align-right")
1133 halign('r', cur.col());
1134 else if (s == "align-center")
1135 halign('c', cur.col());
1136 else if (s == "append-row")
1137 for (int i = 0, n = extractInt(is); i < n; ++i)
1139 else if (s == "delete-row") {
1140 for (int i = 0, n = extractInt(is); i < n; ++i) {
1142 if (cur.idx() >= nargs())
1143 cur.idx() -= ncols();
1145 cur.pos() = 0; // trick, see below
1147 else if (s == "copy-row") {
1148 // Here (as later) we save the cursor col/row
1149 // in order to restore it after operation.
1150 row_type const r = cur.row();
1151 col_type const c = cur.col();
1152 for (int i = 0, n = extractInt(is); i < n; ++i)
1154 cur.idx() = index(r, c);
1156 else if (s == "swap-row") {
1158 // Trick to suppress same-idx-means-different-cell
1162 else if (s == "add-hline-above")
1163 rowinfo_[cur.row()].lines_++;
1164 else if (s == "add-hline-below")
1165 rowinfo_[cur.row()+1].lines_++;
1166 else if (s == "delete-hline-above")
1167 rowinfo_[cur.row()].lines_--;
1168 else if (s == "delete-hline-below")
1169 rowinfo_[cur.row()+1].lines_--;
1170 else if (s == "append-column") {
1171 row_type const r = cur.row();
1172 col_type const c = cur.col();
1173 for (int i = 0, n = extractInt(is); i < n; ++i)
1175 cur.idx() = index(r, c);
1177 else if (s == "delete-column") {
1178 row_type const r = cur.row();
1179 col_type const c = cur.col();
1180 for (int i = 0, n = extractInt(is); i < n; ++i)
1181 delCol(col(cur.idx()));
1182 cur.idx() = index(r, min(c, cur.ncols() - 1));
1183 cur.pos() = 0; // trick, see above
1185 else if (s == "copy-column") {
1186 row_type const r = cur.row();
1187 col_type const c = cur.col();
1189 cur.idx() = index(r, c);
1191 else if (s == "swap-column") {
1193 cur.pos() = 0; // trick, see above
1195 else if (s == "add-vline-left")
1196 colinfo_[cur.col()].lines_++;
1197 else if (s == "add-vline-right")
1198 colinfo_[cur.col()+1].lines_++;
1199 else if (s == "delete-vline-left")
1200 colinfo_[cur.col()].lines_--;
1201 else if (s == "delete-vline-right")
1202 colinfo_[cur.col()+1].lines_--;
1207 lyxerr << "returning FINISHED_LEFT" << endl;
1212 cur.message(_("Paste"));
1213 cap::replaceSelection(cur);
1215 if (cmd.argument().empty() && !theClipboard().isInternal())
1216 topaste = theClipboard().getAsText();
1218 idocstringstream is(cmd.argument());
1221 topaste = cap::getSelection(cur.buffer(), n);
1223 InsetMathGrid grid(1, 1);
1224 if (!topaste.empty())
1225 mathed_parse_normal(grid, topaste);
1227 if (grid.nargs() == 1) {
1228 // single cell/part of cell
1230 cur.cell().insert(cur.pos(), grid.cell(0));
1231 cur.pos() += grid.cell(0).size();
1234 recordUndoInset(cur);
1235 col_type const numcols =
1236 min(grid.ncols(), ncols() - col(cur.idx()));
1237 row_type const numrows =
1238 min(grid.nrows(), nrows() - cur.row());
1239 for (row_type r = 0; r < numrows; ++r) {
1240 for (col_type c = 0; c < numcols; ++c) {
1241 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1242 cell(i).insert(0, grid.cell(grid.index(r, c)));
1244 // append the left over horizontal cells to the last column
1245 idx_type i = index(r + cur.row(), ncols() - 1);
1246 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1247 cell(i).append(grid.cell(grid.index(r, c)));
1249 // append the left over vertical cells to the last _cell_
1250 idx_type i = nargs() - 1;
1251 for (row_type r = numrows; r < grid.nrows(); ++r)
1252 for (col_type c = 0; c < grid.ncols(); ++c)
1253 cell(i).append(grid.cell(grid.index(r, c)));
1255 cur.clearSelection(); // bug 393
1256 cur.bv().switchKeyMap();
1261 case LFUN_LINE_BEGIN_SELECT:
1262 case LFUN_LINE_BEGIN:
1263 case LFUN_WORD_BACKWARD_SELECT:
1264 case LFUN_WORD_BACKWARD:
1265 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1266 cmd.action == LFUN_LINE_BEGIN_SELECT);
1267 cur.macroModeClose();
1268 if (cur.pos() != 0) {
1270 } else if (cur.idx() % cur.ncols() != 0) {
1271 cur.idx() -= cur.idx() % cur.ncols();
1273 } else if (cur.idx() != 0) {
1277 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1282 case LFUN_WORD_FORWARD_SELECT:
1283 case LFUN_WORD_FORWARD:
1284 case LFUN_LINE_END_SELECT:
1286 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1287 cmd.action == LFUN_LINE_END_SELECT);
1288 cur.macroModeClose();
1290 if (cur.pos() != cur.lastpos()) {
1291 cur.pos() = cur.lastpos();
1292 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1293 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1294 cur.pos() = cur.lastpos();
1295 } else if (cur.idx() != cur.lastidx()) {
1296 cur.idx() = cur.lastidx();
1297 cur.pos() = cur.lastpos();
1299 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1305 InsetMathNest::doDispatch(cur, cmd);
1310 bool InsetMathGrid::getStatus(LCursor & cur, FuncRequest const & cmd,
1311 FuncStatus & status) const
1313 switch (cmd.action) {
1314 case LFUN_TABULAR_FEATURE: {
1315 string const s = to_utf8(cmd.argument());
1316 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1317 status.enabled(false);
1318 status.message(from_utf8(N_("Only one row")));
1322 (s == "delete-column" || s == "swap-column")) {
1323 status.enabled(false);
1324 status.message(from_utf8(N_("Only one column")));
1327 if ((rowinfo_[cur.row()].lines_ == 0 &&
1328 s == "delete-hline-above") ||
1329 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1330 s == "delete-hline-below")) {
1331 status.enabled(false);
1332 status.message(from_utf8(N_("No hline to delete")));
1336 if ((colinfo_[cur.col()].lines_ == 0 &&
1337 s == "delete-vline-left") ||
1338 (colinfo_[cur.col() + 1].lines_ == 0 &&
1339 s == "delete-vline-right")) {
1340 status.enabled(false);
1341 status.message(from_utf8(N_("No vline to delete")));
1344 if (s == "valign-top" || s == "valign-middle" ||
1345 s == "valign-bottom" || s == "align-left" ||
1346 s == "align-right" || s == "align-center" ||
1347 s == "append-row" || s == "delete-row" ||
1348 s == "copy-row" || s == "swap-row" ||
1349 s == "add-hline-above" || s == "add-hline-below" ||
1350 s == "delete-hline-above" || s == "delete-hline-below" ||
1351 s == "append-column" || s == "delete-column" ||
1352 s == "copy-column" || s == "swap-column" ||
1353 s == "add-vline-left" || s == "add-vline-right" ||
1354 s == "delete-vline-left" || s == "delete-vline-right")
1355 status.enabled(true);
1357 status.enabled(false);
1358 status.message(bformat(
1359 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1362 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1363 || s == "align-right" && halign(cur.col()) == 'r'
1364 || s == "align-center" && halign(cur.col()) == 'c'
1365 || s == "valign-top" && valign() == 't'
1366 || s == "valign-bottom" && valign() == 'b'
1367 || s == "valign-middle" && valign() == 'm');
1370 // FIXME: What did this code do?
1371 // Please check whether it is still needed!
1372 // should be more precise
1373 if (v_align_ == '\0') {
1374 status.enable(true);
1377 if (cmd.argument().empty()) {
1378 status.enable(false);
1381 if (!support::contains("tcb", cmd.argument()[0])) {
1382 status.enable(false);
1385 status.setOnOff(cmd.argument()[0] == v_align_);
1386 status.enabled(true);
1391 case LFUN_CELL_SPLIT:
1392 status.enabled(true);
1395 case LFUN_CELL_BACKWARD:
1396 case LFUN_CELL_FORWARD:
1397 status.enabled(true);
1401 return InsetMathNest::getStatus(cur, cmd, status);