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.
13 #include "InsetMathGrid.h"
14 #include "MathArray.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 auto_ptr<InsetBase> InsetMathGrid::doClone() const
189 return auto_ptr<InsetBase>(new InsetMathGrid(*this));
193 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
195 return col + ncols() * row;
199 void InsetMathGrid::setDefaults()
202 lyxerr << "positive number of columns expected" << endl;
204 // lyxerr << "positive number of rows expected" << endl;
205 for (col_type col = 0; col < ncols(); ++col) {
206 colinfo_[col].align_ = defaultColAlign(col);
207 colinfo_[col].skip_ = defaultColSpace(col);
212 void InsetMathGrid::halign(docstring const & hh)
215 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
218 colinfo_[col].lines_++;
219 } else if (col >= ncols()) {
220 // Only '|' is allowed in the last dummy column
222 } else if (c == 'c' || c == 'l' || c == 'r') {
223 colinfo_[col].align_ = (char)c;
225 colinfo_[col].lines_ = 0;
227 lyxerr << "unknown column separator: '" << c << "'" << endl;
232 col_type n = hh.size();
235 for (col_type col = 0; col < n; ++col)
236 colinfo_[col].align_ = hh[col];
241 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
244 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
245 if (*it == 'c' || *it == 'l' || *it == 'r')
247 // let's have at least one column, even if we did not recognize its
255 void InsetMathGrid::halign(char h, col_type col)
257 colinfo_[col].align_ = h;
261 char InsetMathGrid::halign(col_type col) const
263 return colinfo_[col].align_;
267 docstring InsetMathGrid::halign() const
270 for (col_type col = 0; col < ncols(); ++col) {
271 res += docstring(colinfo_[col].lines_, '|');
272 res += colinfo_[col].align_;
274 return res + docstring(colinfo_[ncols()].lines_, '|');
278 void InsetMathGrid::valign(char c)
284 char InsetMathGrid::valign() const
290 InsetMathGrid::col_type InsetMathGrid::ncols() const
292 return colinfo_.size() - 1;
296 InsetMathGrid::row_type InsetMathGrid::nrows() const
298 return rowinfo_.size() - 1;
302 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
304 return idx % ncols();
308 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
310 return idx / ncols();
314 void InsetMathGrid::vcrskip(LyXLength const & crskip, row_type row)
316 rowinfo_[row].crskip_ = crskip;
320 LyXLength InsetMathGrid::vcrskip(row_type row) const
322 return rowinfo_[row].crskip_;
326 void InsetMathGrid::metrics(MetricsInfo & mi) const
328 // let the cells adjust themselves
329 InsetMathNest::metrics(mi);
331 // compute absolute sizes of vertical structure
332 for (row_type row = 0; row < nrows(); ++row) {
335 for (col_type col = 0; col < ncols(); ++col) {
336 MathArray const & c = cell(index(row, col));
337 asc = max(asc, c.ascent());
338 desc = max(desc, c.descent());
340 rowinfo_[row].ascent_ = asc;
341 rowinfo_[row].descent_ = desc;
343 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
344 rowinfo_[nrows()].ascent_ = 0;
345 rowinfo_[nrows()].descent_ = 0;
347 // compute vertical offsets
348 rowinfo_[0].offset_ = 0;
349 for (row_type row = 1; row <= nrows(); ++row) {
350 rowinfo_[row].offset_ =
351 rowinfo_[row - 1].offset_ +
352 rowinfo_[row - 1].descent_ +
353 rowinfo_[row - 1].skipPixels() +
355 rowinfo_[row].lines_ * hlinesep() +
356 rowinfo_[row].ascent_;
359 // adjust vertical offset
366 h = rowinfo_[nrows() - 1].offset_;
369 h = rowinfo_[nrows() - 1].offset_ / 2;
371 for (row_type row = 0; row <= nrows(); ++row)
372 rowinfo_[row].offset_ -= h;
375 // compute absolute sizes of horizontal structure
376 for (col_type col = 0; col < ncols(); ++col) {
378 for (row_type row = 0; row < nrows(); ++row)
379 wid = max(wid, cell(index(row, col)).width());
380 colinfo_[col].width_ = wid;
382 colinfo_[ncols()].width_ = 0;
384 // compute horizontal offsets
385 colinfo_[0].offset_ = border();
386 for (col_type col = 1; col <= ncols(); ++col) {
387 colinfo_[col].offset_ =
388 colinfo_[col - 1].offset_ +
389 colinfo_[col - 1].width_ +
390 colinfo_[col - 1].skip_ +
392 colinfo_[col].lines_ * vlinesep();
396 dim_.wid = colinfo_[ncols() - 1].offset_
397 + colinfo_[ncols() - 1].width_
398 + vlinesep() * colinfo_[ncols()].lines_
401 dim_.asc = - rowinfo_[0].offset_
402 + rowinfo_[0].ascent_
403 + hlinesep() * rowinfo_[0].lines_
406 dim_.des = rowinfo_[nrows() - 1].offset_
407 + rowinfo_[nrows() - 1].descent_
408 + hlinesep() * rowinfo_[nrows()].lines_
413 // Increase ws_[i] for 'R' columns (except the first one)
414 for (int i = 1; i < nc_; ++i)
415 if (align_[i] == 'R')
416 ws_[i] += 10 * df_width;
417 // Increase ws_[i] for 'C' column
418 if (align_[0] == 'C')
419 if (ws_[0] < 7 * workwidth / 8)
420 ws_[0] = 7 * workwidth / 8;
424 for (cxrow = row_.begin(); cxrow; ++cxrow) {
427 for (int i = 0; i < nc_; ++i) {
429 if (cxrow->getTab(i) <= 0) {
430 cxrow->setTab(i, df_width);
438 lf = (ws_[i] - cxrow->getTab(i))/2;
442 lf = ws_[i] - cxrow->getTab(i);
445 if (cxrow == row_.begin())
447 else if (cxrow.is_last())
448 lf = ws_[i] - cxrow->getTab(i);
450 lf = (ws_[i] - cxrow->getTab(i))/2;
453 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
454 cxrow->setTab(i, lf + rg);
455 rg = ws_[i] - ww + colsep();
456 if (cxrow == row_.begin())
457 width += ws_[i] + colsep();
459 cxrow->setBaseline(cxrow->getBaseline() - ascent);
462 metricsMarkers2(dim_);
466 bool InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
477 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
479 drawWithMargin(pi, x, y, 0, 0);
482 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
483 int lmargin, int rmargin) const
485 for (idx_type idx = 0; idx < nargs(); ++idx)
486 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
487 y + cellYOffset(idx));
489 for (row_type row = 0; row <= nrows(); ++row)
490 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
491 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
492 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
493 pi.pain.line(x + lmargin + 1, yy,
494 x + dim_.width() - rmargin - 1, yy,
498 for (col_type col = 0; col <= ncols(); ++col)
499 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
500 int xx = x + lmargin + colinfo_[col].offset_
501 - i * vlinesep() - vlinesep()/2 - colsep()/2;
502 pi.pain.line(xx, y - dim_.ascent() + 1,
503 xx, y + dim_.descent() - 1,
506 drawMarkers2(pi, x, y);
510 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
512 // let the cells adjust themselves
513 //InsetMathNest::metrics(mi);
514 for (idx_type i = 0; i < nargs(); ++i)
515 cell(i).metricsT(mi, dim);
517 // compute absolute sizes of vertical structure
518 for (row_type row = 0; row < nrows(); ++row) {
521 for (col_type col = 0; col < ncols(); ++col) {
522 MathArray const & c = cell(index(row, col));
523 asc = max(asc, c.ascent());
524 desc = max(desc, c.descent());
526 rowinfo_[row].ascent_ = asc;
527 rowinfo_[row].descent_ = desc;
529 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
530 rowinfo_[nrows()].ascent_ = 0;
531 rowinfo_[nrows()].descent_ = 0;
533 // compute vertical offsets
534 rowinfo_[0].offset_ = 0;
535 for (row_type row = 1; row <= nrows(); ++row) {
536 rowinfo_[row].offset_ =
537 rowinfo_[row - 1].offset_ +
538 rowinfo_[row - 1].descent_ +
539 //rowinfo_[row - 1].skipPixels() +
541 //rowinfo_[row].lines_ * hlinesep() +
542 rowinfo_[row].ascent_;
545 // adjust vertical offset
552 h = rowinfo_[nrows() - 1].offset_;
555 h = rowinfo_[nrows() - 1].offset_ / 2;
557 for (row_type row = 0; row <= nrows(); ++row)
558 rowinfo_[row].offset_ -= h;
561 // compute absolute sizes of horizontal structure
562 for (col_type col = 0; col < ncols(); ++col) {
564 for (row_type row = 0; row < nrows(); ++row)
565 wid = max(wid, cell(index(row, col)).width());
566 colinfo_[col].width_ = wid;
568 colinfo_[ncols()].width_ = 0;
570 // compute horizontal offsets
571 colinfo_[0].offset_ = border();
572 for (col_type col = 1; col <= ncols(); ++col) {
573 colinfo_[col].offset_ =
574 colinfo_[col - 1].offset_ +
575 colinfo_[col - 1].width_ +
576 colinfo_[col - 1].skip_ +
578 //colinfo_[col].lines_ * vlinesep();
582 dim.wid = colinfo_[ncols() - 1].offset_
583 + colinfo_[ncols() - 1].width_
584 //+ vlinesep() * colinfo_[ncols()].lines_
587 dim.asc = -rowinfo_[0].offset_
588 + rowinfo_[0].ascent_
589 //+ hlinesep() * rowinfo_[0].lines_
592 dim.des = rowinfo_[nrows() - 1].offset_
593 + rowinfo_[nrows() - 1].descent_
594 //+ hlinesep() * rowinfo_[nrows()].lines_
599 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
601 for (idx_type idx = 0; idx < nargs(); ++idx)
602 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
606 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
610 if (!rowinfo_[row].crskip_.zero())
611 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
612 else if(!rowinfo_[row].allow_pagebreak_)
615 // make sure an upcoming '[' does not break anything
616 if (row + 1 < nrows()) {
617 MathArray const & c = cell(index(row + 1, 0));
618 if (c.size() && c.front()->getChar() == '[')
623 // only add \\ if necessary
624 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
627 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
631 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
633 if (col + 1 == lastcol)
635 return from_ascii(" & ");
639 void InsetMathGrid::addRow(row_type row)
641 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
643 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
645 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
649 void InsetMathGrid::appendRow()
651 rowinfo_.push_back(RowInfo());
652 //cells_.insert(cells_.end(), ncols(), MathArray());
653 for (col_type col = 0; col < ncols(); ++col) {
654 cells_.push_back(cells_type::value_type());
655 cellinfo_.push_back(CellInfo());
660 void InsetMathGrid::delRow(row_type row)
665 cells_type::iterator it = cells_.begin() + row * ncols();
666 cells_.erase(it, it + ncols());
668 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
669 cellinfo_.erase(jt, jt + ncols());
671 rowinfo_.erase(rowinfo_.begin() + row);
675 void InsetMathGrid::copyRow(row_type row)
678 for (col_type col = 0; col < ncols(); ++col)
679 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
683 void InsetMathGrid::swapRow(row_type row)
687 if (row + 1 == nrows())
689 for (col_type col = 0; col < ncols(); ++col)
690 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
694 void InsetMathGrid::addCol(col_type newcol)
696 const col_type nc = ncols();
697 const row_type nr = nrows();
698 cells_type new_cells((nc + 1) * nr);
699 vector<CellInfo> new_cellinfo((nc + 1) * nr);
701 for (row_type row = 0; row < nr; ++row)
702 for (col_type col = 0; col < nc; ++col) {
703 new_cells[row * (nc + 1) + col + (col > newcol)]
704 = cells_[row * nc + col];
705 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
706 = cellinfo_[row * nc + col];
708 swap(cells_, new_cells);
709 swap(cellinfo_, new_cellinfo);
712 inf.skip_ = defaultColSpace(newcol);
713 inf.align_ = defaultColAlign(newcol);
714 colinfo_.insert(colinfo_.begin() + newcol, inf);
718 void InsetMathGrid::delCol(col_type col)
724 vector<CellInfo> tmpcellinfo;
725 for (col_type i = 0; i < nargs(); ++i)
726 if (i % ncols() != col) {
727 tmpcells.push_back(cells_[i]);
728 tmpcellinfo.push_back(cellinfo_[i]);
730 swap(cells_, tmpcells);
731 swap(cellinfo_, tmpcellinfo);
733 colinfo_.erase(colinfo_.begin() + col);
737 void InsetMathGrid::copyCol(col_type col)
740 for (row_type row = 0; row < nrows(); ++row)
741 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
745 void InsetMathGrid::swapCol(col_type col)
749 if (col + 1 == ncols())
751 for (row_type row = 0; row < nrows(); ++row)
752 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
756 int InsetMathGrid::cellXOffset(idx_type idx) const
758 col_type c = col(idx);
759 int x = colinfo_[c].offset_;
760 char align = colinfo_[c].align_;
761 if (align == 'r' || align == 'R')
762 x += colinfo_[c].width_ - cell(idx).width();
763 if (align == 'c' || align == 'C')
764 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
769 int InsetMathGrid::cellYOffset(idx_type idx) const
771 return rowinfo_[row(idx)].offset_;
775 bool InsetMathGrid::idxUpDown(LCursor & cur, bool up) const
780 cur.idx() -= ncols();
782 if (cur.row() + 1 >= nrows())
784 cur.idx() += ncols();
786 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
791 bool InsetMathGrid::idxLeft(LCursor & cur) const
793 // leave matrix if on the left hand edge
797 cur.pos() = cur.lastpos();
802 bool InsetMathGrid::idxRight(LCursor & cur) const
804 // leave matrix if on the right hand edge
805 if (cur.col() + 1 == ncols())
813 bool InsetMathGrid::idxFirst(LCursor & cur) const
820 cur.idx() = (nrows() - 1) * ncols();
823 cur.idx() = ((nrows() - 1) / 2) * ncols();
830 bool InsetMathGrid::idxLast(LCursor & cur) const
834 cur.idx() = ncols() - 1;
837 cur.idx() = nargs() - 1;
840 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
842 cur.pos() = cur.lastpos();
847 bool InsetMathGrid::idxDelete(idx_type & idx)
849 // nothing to do if we have just one row
853 // nothing to do if we are in the middle of the last row of the inset
854 if (idx + ncols() > nargs())
857 // try to delete entire sequence of ncols() empty cells if possible
858 for (idx_type i = idx; i < idx + ncols(); ++i)
862 // move cells if necessary
863 for (idx_type i = index(row(idx), 0); i < idx; ++i)
864 swap(cell(i), cell(i + ncols()));
871 // undo effect of Ctrl-Tab (i.e. pull next cell)
872 //if (idx + 1 != nargs())
873 // cell(idx).swap(cell(idx + 1));
875 // we handled the event..
880 // reimplement old behaviour when pressing Delete in the last position
882 void InsetMathGrid::idxGlue(idx_type idx)
884 col_type c = col(idx);
885 if (c + 1 == ncols()) {
886 if (row(idx) + 1 != nrows()) {
887 for (col_type cc = 0; cc < ncols(); ++cc)
888 cell(idx).append(cell(idx + cc + 1));
889 delRow(row(idx) + 1);
892 cell(idx).append(cell(idx + 1));
893 for (col_type cc = c + 2; cc < ncols(); ++cc)
894 cell(idx - c + cc - 1) = cell(idx - c + cc);
895 cell(idx - c + ncols() - 1).clear();
900 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
902 return rowinfo_[row];
906 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
908 return rowinfo_[row];
912 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
914 row_type const ri = row(idx);
915 row_type const r1 = min(row(from), row(to));
916 row_type const r2 = max(row(from), row(to));
917 col_type const ci = col(idx);
918 col_type const c1 = min(col(from), col(to));
919 col_type const c2 = max(col(from), col(to));
920 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
925 void InsetMathGrid::normalize(NormalStream & os) const
928 for (row_type row = 0; row < nrows(); ++row) {
930 for (col_type col = 0; col < ncols(); ++col)
931 os << "[cell " << cell(index(row, col)) << ']';
938 void InsetMathGrid::mathmlize(MathStream & os) const
940 os << MTag("mtable");
941 for (row_type row = 0; row < nrows(); ++row) {
943 for (col_type col = 0; col < ncols(); ++col)
944 os << cell(index(row, col));
947 os << ETag("mtable");
951 void InsetMathGrid::write(WriteStream & os) const
954 for (row_type row = 0; row < nrows(); ++row) {
955 os << verboseHLine(rowinfo_[row].lines_);
956 // don't write & and empty cells at end of line
957 col_type lastcol = 0;
958 bool emptyline = true;
959 for (col_type col = 0; col < ncols(); ++col)
960 if (!cell(index(row, col)).empty()) {
964 for (col_type col = 0; col < lastcol; ++col)
965 os << cell(index(row, col)) << eocString(col, lastcol);
966 eol = eolString(row, emptyline, os.fragile());
968 // append newline only if line wasn't completely empty
969 // and this was not the last line in the grid
970 if (!emptyline && row + 1 < nrows())
973 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
985 int InsetMathGrid::colsep() const
991 int InsetMathGrid::rowsep() const
997 int InsetMathGrid::hlinesep() const
1003 int InsetMathGrid::vlinesep() const
1009 int InsetMathGrid::border() const
1015 void InsetMathGrid::splitCell(LCursor & cur)
1017 if (cur.idx() == cur.lastidx())
1019 MathArray ar = cur.cell();
1020 ar.erase(0, cur.pos());
1021 cur.cell().erase(cur.pos(), cur.lastpos());
1024 cur.cell().insert(0, ar);
1028 void InsetMathGrid::doDispatch(LCursor & cur, FuncRequest & cmd)
1030 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1031 switch (cmd.action) {
1033 case LFUN_MOUSE_RELEASE:
1034 //if (cmd.button() == mouse_button::button3) {
1035 // GridInsetMailer(*this).showDialog();
1036 // return DispatchResult(true, true);
1038 InsetMathNest::doDispatch(cur, cmd);
1041 case LFUN_INSET_DIALOG_UPDATE:
1042 GridInsetMailer(*this).updateDialog(&cur.bv());
1045 // insert file functions
1046 case LFUN_LINE_DELETE:
1047 // FIXME: We use recordUndoInset when a change reflects more
1048 // than one cell, because recordUndo does not work for
1049 // multiple cells. Unfortunately this puts the cursor in front
1050 // of the inset after undo. This is (especilally for large
1052 recordUndoInset(cur);
1053 //autocorrect_ = false;
1061 if (cur.idx() > cur.lastidx())
1062 cur.idx() = cur.lastidx();
1063 if (cur.pos() > cur.lastpos())
1064 cur.pos() = cur.lastpos();
1067 case LFUN_CELL_SPLIT:
1072 case LFUN_CELL_BACKWARD:
1074 cur.selection() = false;
1075 if (!idxPrev(cur)) {
1076 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1081 case LFUN_CELL_FORWARD:
1082 // Can't handle selection by additional 'shift' as this is
1083 // hard bound to LFUN_CELL_BACKWARD
1084 cur.selection() = false;
1085 if (!idxNext(cur)) {
1086 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1091 case LFUN_BREAK_LINE: {
1092 recordUndoInset(cur);
1093 row_type const r = cur.row();
1097 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1098 swap(cell(index(r, c)), cell(index(r + 1, c)));
1102 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1105 cur.pos() = cur.lastpos();
1107 //mathcursor->normalize();
1108 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1112 case LFUN_TABULAR_FEATURE: {
1113 recordUndoInset(cur);
1114 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1115 istringstream is(to_utf8(cmd.argument()));
1118 if (s == "valign-top")
1120 else if (s == "valign-middle")
1122 else if (s == "valign-bottom")
1124 else if (s == "align-left")
1125 halign('l', cur.col());
1126 else if (s == "align-right")
1127 halign('r', cur.col());
1128 else if (s == "align-center")
1129 halign('c', cur.col());
1130 else if (s == "append-row")
1131 for (int i = 0, n = extractInt(is); i < n; ++i)
1133 else if (s == "delete-row") {
1134 for (int i = 0, n = extractInt(is); i < n; ++i) {
1136 if (cur.idx() >= nargs())
1137 cur.idx() -= ncols();
1139 cur.pos() = 0; // trick, see below
1141 else if (s == "copy-row") {
1142 // Here (as later) we save the cursor col/row
1143 // in order to restore it after operation.
1144 row_type const r = cur.row();
1145 col_type const c = cur.col();
1146 for (int i = 0, n = extractInt(is); i < n; ++i)
1148 cur.idx() = index(r, c);
1150 else if (s == "swap-row") {
1152 // Trick to suppress same-idx-means-different-cell
1156 else if (s == "add-hline-above")
1157 rowinfo_[cur.row()].lines_++;
1158 else if (s == "add-hline-below")
1159 rowinfo_[cur.row()+1].lines_++;
1160 else if (s == "delete-hline-above")
1161 rowinfo_[cur.row()].lines_--;
1162 else if (s == "delete-hline-below")
1163 rowinfo_[cur.row()+1].lines_--;
1164 else if (s == "append-column") {
1165 row_type const r = cur.row();
1166 col_type const c = cur.col();
1167 for (int i = 0, n = extractInt(is); i < n; ++i)
1169 cur.idx() = index(r, c);
1171 else if (s == "delete-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)
1175 delCol(col(cur.idx()));
1176 cur.idx() = index(r, min(c, cur.ncols() - 1));
1177 cur.pos() = 0; // trick, see above
1179 else if (s == "copy-column") {
1180 row_type const r = cur.row();
1181 col_type const c = cur.col();
1183 cur.idx() = index(r, c);
1185 else if (s == "swap-column") {
1187 cur.pos() = 0; // trick, see above
1189 else if (s == "add-vline-left")
1190 colinfo_[cur.col()].lines_++;
1191 else if (s == "add-vline-right")
1192 colinfo_[cur.col()+1].lines_++;
1193 else if (s == "delete-vline-left")
1194 colinfo_[cur.col()].lines_--;
1195 else if (s == "delete-vline-right")
1196 colinfo_[cur.col()+1].lines_--;
1201 lyxerr << "returning FINISHED_LEFT" << endl;
1206 cur.message(_("Paste"));
1207 cap::replaceSelection(cur);
1209 if (cmd.argument().empty() && !theClipboard().isInternal())
1210 topaste = theClipboard().getAsText();
1212 idocstringstream is(cmd.argument());
1215 topaste = cap::getSelection(cur.buffer(), n);
1217 InsetMathGrid grid(1, 1);
1218 if (!topaste.empty())
1219 mathed_parse_normal(grid, topaste);
1221 if (grid.nargs() == 1) {
1222 // single cell/part of cell
1224 cur.cell().insert(cur.pos(), grid.cell(0));
1225 cur.pos() += grid.cell(0).size();
1228 recordUndoInset(cur);
1229 col_type const numcols =
1230 min(grid.ncols(), ncols() - col(cur.idx()));
1231 row_type const numrows =
1232 min(grid.nrows(), nrows() - cur.row());
1233 for (row_type r = 0; r < numrows; ++r) {
1234 for (col_type c = 0; c < numcols; ++c) {
1235 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1236 cell(i).insert(0, grid.cell(grid.index(r, c)));
1238 // append the left over horizontal cells to the last column
1239 idx_type i = index(r + cur.row(), ncols() - 1);
1240 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1241 cell(i).append(grid.cell(grid.index(r, c)));
1243 // append the left over vertical cells to the last _cell_
1244 idx_type i = nargs() - 1;
1245 for (row_type r = numrows; r < grid.nrows(); ++r)
1246 for (col_type c = 0; c < grid.ncols(); ++c)
1247 cell(i).append(grid.cell(grid.index(r, c)));
1249 cur.clearSelection(); // bug 393
1250 cur.bv().switchKeyMap();
1255 case LFUN_LINE_BEGIN_SELECT:
1256 case LFUN_LINE_BEGIN:
1257 case LFUN_WORD_BACKWARD_SELECT:
1258 case LFUN_WORD_BACKWARD:
1259 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1260 cmd.action == LFUN_LINE_BEGIN_SELECT);
1261 cur.macroModeClose();
1262 if (cur.pos() != 0) {
1264 } else if (cur.idx() % cur.ncols() != 0) {
1265 cur.idx() -= cur.idx() % cur.ncols();
1267 } else if (cur.idx() != 0) {
1271 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1276 case LFUN_WORD_FORWARD_SELECT:
1277 case LFUN_WORD_FORWARD:
1278 case LFUN_LINE_END_SELECT:
1280 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1281 cmd.action == LFUN_LINE_END_SELECT);
1282 cur.macroModeClose();
1284 if (cur.pos() != cur.lastpos()) {
1285 cur.pos() = cur.lastpos();
1286 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1287 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1288 cur.pos() = cur.lastpos();
1289 } else if (cur.idx() != cur.lastidx()) {
1290 cur.idx() = cur.lastidx();
1291 cur.pos() = cur.lastpos();
1293 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1299 InsetMathNest::doDispatch(cur, cmd);
1304 bool InsetMathGrid::getStatus(LCursor & cur, FuncRequest const & cmd,
1305 FuncStatus & status) const
1307 switch (cmd.action) {
1308 case LFUN_TABULAR_FEATURE: {
1309 string const s = to_utf8(cmd.argument());
1310 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1311 status.enabled(false);
1312 status.message(from_utf8(N_("Only one row")));
1316 (s == "delete-column" || s == "swap-column")) {
1317 status.enabled(false);
1318 status.message(from_utf8(N_("Only one column")));
1321 if ((rowinfo_[cur.row()].lines_ == 0 &&
1322 s == "delete-hline-above") ||
1323 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1324 s == "delete-hline-below")) {
1325 status.enabled(false);
1326 status.message(from_utf8(N_("No hline to delete")));
1330 if ((colinfo_[cur.col()].lines_ == 0 &&
1331 s == "delete-vline-left") ||
1332 (colinfo_[cur.col() + 1].lines_ == 0 &&
1333 s == "delete-vline-right")) {
1334 status.enabled(false);
1335 status.message(from_utf8(N_("No vline to delete")));
1338 if (s == "valign-top" || s == "valign-middle" ||
1339 s == "valign-bottom" || s == "align-left" ||
1340 s == "align-right" || s == "align-center" ||
1341 s == "append-row" || s == "delete-row" ||
1342 s == "copy-row" || s == "swap-row" ||
1343 s == "add-hline-above" || s == "add-hline-below" ||
1344 s == "delete-hline-above" || s == "delete-hline-below" ||
1345 s == "append-column" || s == "delete-column" ||
1346 s == "copy-column" || s == "swap-column" ||
1347 s == "add-vline-left" || s == "add-vline-right" ||
1348 s == "delete-vline-left" || s == "delete-vline-right")
1349 status.enabled(true);
1351 status.enabled(false);
1352 status.message(bformat(
1353 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1356 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1357 || s == "align-right" && halign(cur.col()) == 'r'
1358 || s == "align-center" && halign(cur.col()) == 'c'
1359 || s == "valign-top" && valign() == 't'
1360 || s == "valign-bottom" && valign() == 'b'
1361 || s == "valign-middle" && valign() == 'm');
1364 // FIXME: What did this code do?
1365 // Please check whether it is still needed!
1366 // should be more precise
1367 if (v_align_ == '\0') {
1368 status.enable(true);
1371 if (cmd.argument().empty()) {
1372 status.enable(false);
1375 if (!support::contains("tcb", cmd.argument()[0])) {
1376 status.enable(false);
1379 status.setOnOff(cmd.argument()[0] == v_align_);
1380 status.enabled(true);
1385 case LFUN_CELL_SPLIT:
1386 status.enabled(true);
1389 case LFUN_CELL_BACKWARD:
1390 case LFUN_CELL_FORWARD:
1391 status.enabled(true);
1395 return InsetMathNest::getStatus(cur, cmd, status);