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 "MathMLStream.h"
16 #include "MathStream.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "FuncStatus.h"
24 #include "funcrequest.h"
28 #include "frontends/Painter.h"
30 #include "insets/mailinset.h"
32 #include "support/lstrings.h"
36 using lyx::support::bformat;
46 using std::istringstream;
50 class GridInsetMailer : public MailInset {
52 GridInsetMailer(InsetMathGrid & inset) : inset_(inset) {}
54 virtual string const & name() const
56 static string const theName = "tabular";
60 virtual string const inset2string(Buffer const &) const
62 lyx::odocstringstream data;
63 //data << name() << " active_cell " << inset.getActCell() << '\n';
64 data << lyx::from_utf8(name()) << " active_cell " << 0 << '\n';
67 return lyx::to_utf8(data.str());
71 InsetBase & inset() const { return inset_; }
72 InsetMathGrid & inset_;
76 void mathed_parse_normal(InsetMathGrid &, string const & argument);
80 string verboseHLine(int n)
83 for (int i = 0; i < n; ++i)
91 int extractInt(istream & is)
95 return (num == 0) ? 1 : num;
101 //////////////////////////////////////////////////////////////
104 InsetMathGrid::CellInfo::CellInfo()
111 //////////////////////////////////////////////////////////////
114 InsetMathGrid::RowInfo::RowInfo()
115 : lines_(0), skip_(0)
120 int InsetMathGrid::RowInfo::skipPixels() const
122 return crskip_.inBP();
127 //////////////////////////////////////////////////////////////
130 InsetMathGrid::ColInfo::ColInfo()
131 : align_('c'), lines_(0)
135 //////////////////////////////////////////////////////////////
138 InsetMathGrid::InsetMathGrid(char v, string const & h)
139 : InsetMathNest(guessColumns(h)),
141 colinfo_(guessColumns(h) + 1),
142 cellinfo_(1 * guessColumns(h))
147 //lyxerr << "created grid with " << ncols() << " columns" << endl;
151 InsetMathGrid::InsetMathGrid()
162 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
163 : InsetMathNest(m * n),
173 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, string const & h)
174 : InsetMathNest(m * n),
186 InsetMathGrid::~InsetMathGrid()
188 GridInsetMailer mailer(*this);
193 auto_ptr<InsetBase> InsetMathGrid::doClone() const
195 return auto_ptr<InsetBase>(new InsetMathGrid(*this));
199 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
201 return col + ncols() * row;
205 void InsetMathGrid::setDefaults()
208 lyxerr << "positive number of columns expected" << endl;
210 // lyxerr << "positive number of rows expected" << endl;
211 for (col_type col = 0; col < ncols(); ++col) {
212 colinfo_[col].align_ = defaultColAlign(col);
213 colinfo_[col].skip_ = defaultColSpace(col);
218 void InsetMathGrid::halign(string const & hh)
221 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
224 colinfo_[col].lines_++;
225 } else if (col >= ncols()) {
226 // Only '|' is allowed in the last dummy column
228 } else if (c == 'c' || c == 'l' || c == 'r') {
229 colinfo_[col].align_ = c;
231 colinfo_[col].lines_ = 0;
233 lyxerr << "unknown column separator: '" << c << "'" << endl;
238 col_type n = hh.size();
241 for (col_type col = 0; col < n; ++col)
242 colinfo_[col].align_ = hh[col];
247 InsetMathGrid::col_type InsetMathGrid::guessColumns(string const & hh) const
250 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
251 if (*it == 'c' || *it == 'l' || *it == 'r')
253 // let's have at least one column, even if we did not recognize its
261 void InsetMathGrid::halign(char h, col_type col)
263 colinfo_[col].align_ = h;
267 char InsetMathGrid::halign(col_type col) const
269 return colinfo_[col].align_;
273 string InsetMathGrid::halign() const
276 for (col_type col = 0; col < ncols(); ++col) {
277 res += string(colinfo_[col].lines_, '|');
278 res += colinfo_[col].align_;
280 return res + string(colinfo_[ncols()].lines_, '|');
284 void InsetMathGrid::valign(char c)
290 char InsetMathGrid::valign() const
296 InsetMathGrid::col_type InsetMathGrid::ncols() const
298 return colinfo_.size() - 1;
302 InsetMathGrid::row_type InsetMathGrid::nrows() const
304 return rowinfo_.size() - 1;
308 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
310 return idx % ncols();
314 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
316 return idx / ncols();
320 void InsetMathGrid::vcrskip(LyXLength const & crskip, row_type row)
322 rowinfo_[row].crskip_ = crskip;
326 LyXLength InsetMathGrid::vcrskip(row_type row) const
328 return rowinfo_[row].crskip_;
332 void InsetMathGrid::metrics(MetricsInfo & mi) const
334 // let the cells adjust themselves
335 InsetMathNest::metrics(mi);
337 // compute absolute sizes of vertical structure
338 for (row_type row = 0; row < nrows(); ++row) {
341 for (col_type col = 0; col < ncols(); ++col) {
342 MathArray const & c = cell(index(row, col));
343 asc = max(asc, c.ascent());
344 desc = max(desc, c.descent());
346 rowinfo_[row].ascent_ = asc;
347 rowinfo_[row].descent_ = desc;
349 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
350 rowinfo_[nrows()].ascent_ = 0;
351 rowinfo_[nrows()].descent_ = 0;
353 // compute vertical offsets
354 rowinfo_[0].offset_ = 0;
355 for (row_type row = 1; row <= nrows(); ++row) {
356 rowinfo_[row].offset_ =
357 rowinfo_[row - 1].offset_ +
358 rowinfo_[row - 1].descent_ +
359 rowinfo_[row - 1].skipPixels() +
361 rowinfo_[row].lines_ * hlinesep() +
362 rowinfo_[row].ascent_;
365 // adjust vertical offset
372 h = rowinfo_[nrows() - 1].offset_;
375 h = rowinfo_[nrows() - 1].offset_ / 2;
377 for (row_type row = 0; row <= nrows(); ++row)
378 rowinfo_[row].offset_ -= h;
381 // compute absolute sizes of horizontal structure
382 for (col_type col = 0; col < ncols(); ++col) {
384 for (row_type row = 0; row < nrows(); ++row)
385 wid = max(wid, cell(index(row, col)).width());
386 colinfo_[col].width_ = wid;
388 colinfo_[ncols()].width_ = 0;
390 // compute horizontal offsets
391 colinfo_[0].offset_ = border();
392 for (col_type col = 1; col <= ncols(); ++col) {
393 colinfo_[col].offset_ =
394 colinfo_[col - 1].offset_ +
395 colinfo_[col - 1].width_ +
396 colinfo_[col - 1].skip_ +
398 colinfo_[col].lines_ * vlinesep();
402 dim_.wid = colinfo_[ncols() - 1].offset_
403 + colinfo_[ncols() - 1].width_
404 + vlinesep() * colinfo_[ncols()].lines_
407 dim_.asc = - rowinfo_[0].offset_
408 + rowinfo_[0].ascent_
409 + hlinesep() * rowinfo_[0].lines_
412 dim_.des = rowinfo_[nrows() - 1].offset_
413 + rowinfo_[nrows() - 1].descent_
414 + hlinesep() * rowinfo_[nrows()].lines_
419 // Increase ws_[i] for 'R' columns (except the first one)
420 for (int i = 1; i < nc_; ++i)
421 if (align_[i] == 'R')
422 ws_[i] += 10 * df_width;
423 // Increase ws_[i] for 'C' column
424 if (align_[0] == 'C')
425 if (ws_[0] < 7 * workwidth / 8)
426 ws_[0] = 7 * workwidth / 8;
430 for (cxrow = row_.begin(); cxrow; ++cxrow) {
433 for (int i = 0; i < nc_; ++i) {
435 if (cxrow->getTab(i) <= 0) {
436 cxrow->setTab(i, df_width);
444 lf = (ws_[i] - cxrow->getTab(i))/2;
448 lf = ws_[i] - cxrow->getTab(i);
451 if (cxrow == row_.begin())
453 else if (cxrow.is_last())
454 lf = ws_[i] - cxrow->getTab(i);
456 lf = (ws_[i] - cxrow->getTab(i))/2;
459 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
460 cxrow->setTab(i, lf + rg);
461 rg = ws_[i] - ww + colsep();
462 if (cxrow == row_.begin())
463 width += ws_[i] + colsep();
465 cxrow->setBaseline(cxrow->getBaseline() - ascent);
468 metricsMarkers2(dim_);
472 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
479 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
481 drawWithMargin(pi, x, y, 0, 0);
484 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
485 int lmargin, int rmargin) const
487 for (idx_type idx = 0; idx < nargs(); ++idx)
488 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
489 y + cellYOffset(idx));
491 for (row_type row = 0; row <= nrows(); ++row)
492 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
493 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
494 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
495 pi.pain.line(x + lmargin + 1, yy,
496 x + dim_.width() - rmargin - 1, yy,
500 for (col_type col = 0; col <= ncols(); ++col)
501 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
502 int xx = x + lmargin + colinfo_[col].offset_
503 - i * vlinesep() - vlinesep()/2 - colsep()/2;
504 pi.pain.line(xx, y - dim_.ascent() + 1,
505 xx, y + dim_.descent() - 1,
508 drawMarkers2(pi, x, y);
512 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
514 // let the cells adjust themselves
515 //InsetMathNest::metrics(mi);
516 for (idx_type i = 0; i < nargs(); ++i)
517 cell(i).metricsT(mi, dim);
519 // compute absolute sizes of vertical structure
520 for (row_type row = 0; row < nrows(); ++row) {
523 for (col_type col = 0; col < ncols(); ++col) {
524 MathArray const & c = cell(index(row, col));
525 asc = max(asc, c.ascent());
526 desc = max(desc, c.descent());
528 rowinfo_[row].ascent_ = asc;
529 rowinfo_[row].descent_ = desc;
531 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
532 rowinfo_[nrows()].ascent_ = 0;
533 rowinfo_[nrows()].descent_ = 0;
535 // compute vertical offsets
536 rowinfo_[0].offset_ = 0;
537 for (row_type row = 1; row <= nrows(); ++row) {
538 rowinfo_[row].offset_ =
539 rowinfo_[row - 1].offset_ +
540 rowinfo_[row - 1].descent_ +
541 //rowinfo_[row - 1].skipPixels() +
543 //rowinfo_[row].lines_ * hlinesep() +
544 rowinfo_[row].ascent_;
547 // adjust vertical offset
554 h = rowinfo_[nrows() - 1].offset_;
557 h = rowinfo_[nrows() - 1].offset_ / 2;
559 for (row_type row = 0; row <= nrows(); ++row)
560 rowinfo_[row].offset_ -= h;
563 // compute absolute sizes of horizontal structure
564 for (col_type col = 0; col < ncols(); ++col) {
566 for (row_type row = 0; row < nrows(); ++row)
567 wid = max(wid, cell(index(row, col)).width());
568 colinfo_[col].width_ = wid;
570 colinfo_[ncols()].width_ = 0;
572 // compute horizontal offsets
573 colinfo_[0].offset_ = border();
574 for (col_type col = 1; col <= ncols(); ++col) {
575 colinfo_[col].offset_ =
576 colinfo_[col - 1].offset_ +
577 colinfo_[col - 1].width_ +
578 colinfo_[col - 1].skip_ +
580 //colinfo_[col].lines_ * vlinesep();
584 dim.wid = colinfo_[ncols() - 1].offset_
585 + colinfo_[ncols() - 1].width_
586 //+ vlinesep() * colinfo_[ncols()].lines_
589 dim.asc = -rowinfo_[0].offset_
590 + rowinfo_[0].ascent_
591 //+ hlinesep() * rowinfo_[0].lines_
594 dim.des = rowinfo_[nrows() - 1].offset_
595 + rowinfo_[nrows() - 1].descent_
596 //+ hlinesep() * rowinfo_[nrows()].lines_
601 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
603 for (idx_type idx = 0; idx < nargs(); ++idx)
604 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
608 string InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
612 if (!rowinfo_[row].crskip_.zero())
613 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
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 string InsetMathGrid::eocString(col_type col, col_type lastcol) const
633 if (col + 1 == lastcol)
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(MathMLStream & 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 string 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 " << lyx::to_utf8(cmd.argument()) << endl;
1115 istringstream is(lyx::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 lyx::cap::replaceSelection(cur);
1208 istringstream is(lyx::to_utf8(cmd.argument()));
1211 InsetMathGrid grid(1, 1);
1213 mathed_parse_normal(grid,
1214 lyx::to_utf8(lyx::cap::getSelection(cur.buffer(), n)));
1215 if (grid.nargs() == 1) {
1216 // single cell/part of cell
1218 cur.cell().insert(cur.pos(), grid.cell(0));
1219 cur.pos() += grid.cell(0).size();
1222 recordUndoInset(cur);
1223 col_type const numcols =
1224 min(grid.ncols(), ncols() - col(cur.idx()));
1225 row_type const numrows =
1226 min(grid.nrows(), nrows() - cur.row());
1227 for (row_type r = 0; r < numrows; ++r) {
1228 for (col_type c = 0; c < numcols; ++c) {
1229 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1230 cell(i).insert(0, grid.cell(grid.index(r, c)));
1232 // append the left over horizontal cells to the last column
1233 idx_type i = index(r + cur.row(), ncols() - 1);
1234 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1235 cell(i).append(grid.cell(grid.index(r, c)));
1237 // append the left over vertical cells to the last _cell_
1238 idx_type i = nargs() - 1;
1239 for (row_type r = numrows; r < grid.nrows(); ++r)
1240 for (col_type c = 0; c < grid.ncols(); ++c)
1241 cell(i).append(grid.cell(grid.index(r, c)));
1243 cur.clearSelection(); // bug 393
1244 cur.bv().switchKeyMap();
1249 case LFUN_LINE_BEGIN_SELECT:
1250 case LFUN_LINE_BEGIN:
1251 case LFUN_WORD_BACKWARD_SELECT:
1252 case LFUN_WORD_BACKWARD:
1253 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1254 cmd.action == LFUN_LINE_BEGIN_SELECT);
1255 cur.macroModeClose();
1256 if (cur.pos() != 0) {
1258 } else if (cur.idx() % cur.ncols() != 0) {
1259 cur.idx() -= cur.idx() % cur.ncols();
1261 } else if (cur.idx() != 0) {
1265 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1270 case LFUN_WORD_FORWARD_SELECT:
1271 case LFUN_WORD_FORWARD:
1272 case LFUN_LINE_END_SELECT:
1274 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1275 cmd.action == LFUN_LINE_END_SELECT);
1276 cur.macroModeClose();
1278 if (cur.pos() != cur.lastpos()) {
1279 cur.pos() = cur.lastpos();
1280 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1281 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1282 cur.pos() = cur.lastpos();
1283 } else if (cur.idx() != cur.lastidx()) {
1284 cur.idx() = cur.lastidx();
1285 cur.pos() = cur.lastpos();
1287 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1293 InsetMathNest::doDispatch(cur, cmd);
1298 bool InsetMathGrid::getStatus(LCursor & cur, FuncRequest const & cmd,
1299 FuncStatus & status) const
1301 switch (cmd.action) {
1302 case LFUN_TABULAR_FEATURE: {
1303 string const s = lyx::to_utf8(cmd.argument());
1304 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1305 status.enabled(false);
1306 status.message(lyx::from_utf8(N_("Only one row")));
1310 (s == "delete-column" || s == "swap-column")) {
1311 status.enabled(false);
1312 status.message(lyx::from_utf8(N_("Only one column")));
1315 if ((rowinfo_[cur.row()].lines_ == 0 &&
1316 s == "delete-hline-above") ||
1317 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1318 s == "delete-hline-below")) {
1319 status.enabled(false);
1320 status.message(lyx::from_utf8(N_("No hline to delete")));
1324 if ((colinfo_[cur.col()].lines_ == 0 &&
1325 s == "delete-vline-left") ||
1326 (colinfo_[cur.col() + 1].lines_ == 0 &&
1327 s == "delete-vline-right")) {
1328 status.enabled(false);
1329 status.message(lyx::from_utf8(N_("No vline to delete")));
1332 if (s == "valign-top" || s == "valign-middle" ||
1333 s == "valign-bottom" || s == "align-left" ||
1334 s == "align-right" || s == "align-center" ||
1335 s == "append-row" || s == "delete-row" ||
1336 s == "copy-row" || s == "swap-row" ||
1337 s == "add-hline-above" || s == "add-hline-below" ||
1338 s == "delete-hline-above" || s == "delete-hline-below" ||
1339 s == "append-column" || s == "delete-column" ||
1340 s == "copy-column" || s == "swap-column" ||
1341 s == "add-vline-left" || s == "add-vline-right" ||
1342 s == "delete-vline-left" || s == "delete-vline-right")
1343 status.enabled(true);
1345 status.enabled(false);
1346 status.message(bformat(
1347 lyx::from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1350 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1351 || s == "align-right" && halign(cur.col()) == 'r'
1352 || s == "align-center" && halign(cur.col()) == 'c'
1353 || s == "valign-top" && valign() == 't'
1354 || s == "valign-bottom" && valign() == 'b'
1355 || s == "valign-middle" && valign() == 'm');
1358 // FIXME: What did this code do?
1359 // Please check whether it is still needed!
1360 // should be more precise
1361 if (v_align_ == '\0') {
1362 status.enable(true);
1365 if (cmd.argument().empty()) {
1366 status.enable(false);
1369 if (!lyx::support::contains("tcb", cmd.argument()[0])) {
1370 status.enable(false);
1373 status.setOnOff(cmd.argument()[0] == v_align_);
1374 status.enabled(true);
1379 case LFUN_CELL_SPLIT:
1380 status.enabled(true);
1383 case LFUN_CELL_BACKWARD:
1384 case LFUN_CELL_FORWARD:
1385 status.enabled(true);
1389 return InsetMathNest::getStatus(cur, cmd, status);