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"
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 "support/lstrings.h"
38 using support::bformat;
48 using std::istringstream;
54 docstring verboseHLine(int n)
57 for (int i = 0; i < n; ++i)
65 int extractInt(istream & is)
69 return (num == 0) ? 1 : num;
75 //////////////////////////////////////////////////////////////
78 InsetMathGrid::CellInfo::CellInfo()
85 //////////////////////////////////////////////////////////////
88 InsetMathGrid::RowInfo::RowInfo()
89 : lines_(0), skip_(0), allow_pagebreak_(true)
94 int InsetMathGrid::RowInfo::skipPixels() const
96 return crskip_.inBP();
101 //////////////////////////////////////////////////////////////
104 InsetMathGrid::ColInfo::ColInfo()
105 : align_('c'), lines_(0)
109 //////////////////////////////////////////////////////////////
112 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
113 : InsetMathNest(guessColumns(h)),
115 colinfo_(guessColumns(h) + 1),
116 cellinfo_(1 * guessColumns(h))
121 //lyxerr << "created grid with " << ncols() << " columns" << endl;
125 InsetMathGrid::InsetMathGrid()
136 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
137 : InsetMathNest(m * n),
147 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
148 : InsetMathNest(m * n),
160 auto_ptr<Inset> InsetMathGrid::doClone() const
162 return auto_ptr<Inset>(new InsetMathGrid(*this));
166 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
168 return col + ncols() * row;
172 void InsetMathGrid::setDefaults()
175 lyxerr << "positive number of columns expected" << endl;
177 // lyxerr << "positive number of rows expected" << endl;
178 for (col_type col = 0; col < ncols(); ++col) {
179 colinfo_[col].align_ = defaultColAlign(col);
180 colinfo_[col].skip_ = defaultColSpace(col);
185 void InsetMathGrid::halign(docstring const & hh)
188 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
191 colinfo_[col].lines_++;
192 } else if (col >= ncols()) {
193 // Only '|' is allowed in the last dummy column
195 } else if (c == 'c' || c == 'l' || c == 'r') {
196 colinfo_[col].align_ = (char)c;
198 colinfo_[col].lines_ = 0;
200 lyxerr << "unknown column separator: '" << c << "'" << endl;
205 col_type n = hh.size();
208 for (col_type col = 0; col < n; ++col)
209 colinfo_[col].align_ = hh[col];
214 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
217 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
218 if (*it == 'c' || *it == 'l' || *it == 'r')
220 // let's have at least one column, even if we did not recognize its
228 void InsetMathGrid::halign(char h, col_type col)
230 colinfo_[col].align_ = h;
234 char InsetMathGrid::halign(col_type col) const
236 return colinfo_[col].align_;
240 docstring InsetMathGrid::halign() const
243 for (col_type col = 0; col < ncols(); ++col) {
244 res += docstring(colinfo_[col].lines_, '|');
245 res += colinfo_[col].align_;
247 return res + docstring(colinfo_[ncols()].lines_, '|');
251 void InsetMathGrid::valign(char c)
257 char InsetMathGrid::valign() const
263 InsetMathGrid::col_type InsetMathGrid::ncols() const
265 return colinfo_.size() - 1;
269 InsetMathGrid::row_type InsetMathGrid::nrows() const
271 return rowinfo_.size() - 1;
275 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
277 return idx % ncols();
281 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
283 return idx / ncols();
287 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
289 rowinfo_[row].crskip_ = crskip;
293 Length InsetMathGrid::vcrskip(row_type row) const
295 return rowinfo_[row].crskip_;
299 void InsetMathGrid::metrics(MetricsInfo & mi) const
301 // let the cells adjust themselves
302 InsetMathNest::metrics(mi);
304 // compute absolute sizes of vertical structure
305 for (row_type row = 0; row < nrows(); ++row) {
308 for (col_type col = 0; col < ncols(); ++col) {
309 MathData const & c = cell(index(row, col));
310 asc = max(asc, c.ascent());
311 desc = max(desc, c.descent());
313 rowinfo_[row].ascent_ = asc;
314 rowinfo_[row].descent_ = desc;
316 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
317 rowinfo_[nrows()].ascent_ = 0;
318 rowinfo_[nrows()].descent_ = 0;
320 // compute vertical offsets
321 rowinfo_[0].offset_ = 0;
322 for (row_type row = 1; row <= nrows(); ++row) {
323 rowinfo_[row].offset_ =
324 rowinfo_[row - 1].offset_ +
325 rowinfo_[row - 1].descent_ +
326 rowinfo_[row - 1].skipPixels() +
328 rowinfo_[row].lines_ * hlinesep() +
329 rowinfo_[row].ascent_;
332 // adjust vertical offset
339 h = rowinfo_[nrows() - 1].offset_;
342 h = rowinfo_[nrows() - 1].offset_ / 2;
344 for (row_type row = 0; row <= nrows(); ++row)
345 rowinfo_[row].offset_ -= h;
348 // compute absolute sizes of horizontal structure
349 for (col_type col = 0; col < ncols(); ++col) {
351 for (row_type row = 0; row < nrows(); ++row)
352 wid = max(wid, cell(index(row, col)).width());
353 colinfo_[col].width_ = wid;
355 colinfo_[ncols()].width_ = 0;
357 // compute horizontal offsets
358 colinfo_[0].offset_ = border();
359 for (col_type col = 1; col <= ncols(); ++col) {
360 colinfo_[col].offset_ =
361 colinfo_[col - 1].offset_ +
362 colinfo_[col - 1].width_ +
363 colinfo_[col - 1].skip_ +
365 colinfo_[col].lines_ * vlinesep();
369 dim_.wid = colinfo_[ncols() - 1].offset_
370 + colinfo_[ncols() - 1].width_
371 + vlinesep() * colinfo_[ncols()].lines_
374 dim_.asc = - rowinfo_[0].offset_
375 + rowinfo_[0].ascent_
376 + hlinesep() * rowinfo_[0].lines_
379 dim_.des = rowinfo_[nrows() - 1].offset_
380 + rowinfo_[nrows() - 1].descent_
381 + hlinesep() * rowinfo_[nrows()].lines_
386 // Increase ws_[i] for 'R' columns (except the first one)
387 for (int i = 1; i < nc_; ++i)
388 if (align_[i] == 'R')
389 ws_[i] += 10 * df_width;
390 // Increase ws_[i] for 'C' column
391 if (align_[0] == 'C')
392 if (ws_[0] < 7 * workwidth / 8)
393 ws_[0] = 7 * workwidth / 8;
397 for (cxrow = row_.begin(); cxrow; ++cxrow) {
400 for (int i = 0; i < nc_; ++i) {
402 if (cxrow->getTab(i) <= 0) {
403 cxrow->setTab(i, df_width);
411 lf = (ws_[i] - cxrow->getTab(i))/2;
415 lf = ws_[i] - cxrow->getTab(i);
418 if (cxrow == row_.begin())
420 else if (cxrow.is_last())
421 lf = ws_[i] - cxrow->getTab(i);
423 lf = (ws_[i] - cxrow->getTab(i))/2;
426 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
427 cxrow->setTab(i, lf + rg);
428 rg = ws_[i] - ww + colsep();
429 if (cxrow == row_.begin())
430 width += ws_[i] + colsep();
432 cxrow->setBaseline(cxrow->getBaseline() - ascent);
435 metricsMarkers2(dim_);
439 bool InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
450 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
452 drawWithMargin(pi, x, y, 0, 0);
455 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
456 int lmargin, int rmargin) const
458 for (idx_type idx = 0; idx < nargs(); ++idx)
459 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
460 y + cellYOffset(idx));
462 for (row_type row = 0; row <= nrows(); ++row)
463 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
464 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
465 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
466 pi.pain.line(x + lmargin + 1, yy,
467 x + dim_.width() - rmargin - 1, yy,
471 for (col_type col = 0; col <= ncols(); ++col)
472 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
473 int xx = x + lmargin + colinfo_[col].offset_
474 - i * vlinesep() - vlinesep()/2 - colsep()/2;
475 pi.pain.line(xx, y - dim_.ascent() + 1,
476 xx, y + dim_.descent() - 1,
479 drawMarkers2(pi, x, y);
483 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
485 // let the cells adjust themselves
486 //InsetMathNest::metrics(mi);
487 for (idx_type i = 0; i < nargs(); ++i)
488 cell(i).metricsT(mi, dim);
490 // compute absolute sizes of vertical structure
491 for (row_type row = 0; row < nrows(); ++row) {
494 for (col_type col = 0; col < ncols(); ++col) {
495 MathData const & c = cell(index(row, col));
496 asc = max(asc, c.ascent());
497 desc = max(desc, c.descent());
499 rowinfo_[row].ascent_ = asc;
500 rowinfo_[row].descent_ = desc;
502 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
503 rowinfo_[nrows()].ascent_ = 0;
504 rowinfo_[nrows()].descent_ = 0;
506 // compute vertical offsets
507 rowinfo_[0].offset_ = 0;
508 for (row_type row = 1; row <= nrows(); ++row) {
509 rowinfo_[row].offset_ =
510 rowinfo_[row - 1].offset_ +
511 rowinfo_[row - 1].descent_ +
512 //rowinfo_[row - 1].skipPixels() +
514 //rowinfo_[row].lines_ * hlinesep() +
515 rowinfo_[row].ascent_;
518 // adjust vertical offset
525 h = rowinfo_[nrows() - 1].offset_;
528 h = rowinfo_[nrows() - 1].offset_ / 2;
530 for (row_type row = 0; row <= nrows(); ++row)
531 rowinfo_[row].offset_ -= h;
534 // compute absolute sizes of horizontal structure
535 for (col_type col = 0; col < ncols(); ++col) {
537 for (row_type row = 0; row < nrows(); ++row)
538 wid = max(wid, cell(index(row, col)).width());
539 colinfo_[col].width_ = wid;
541 colinfo_[ncols()].width_ = 0;
543 // compute horizontal offsets
544 colinfo_[0].offset_ = border();
545 for (col_type col = 1; col <= ncols(); ++col) {
546 colinfo_[col].offset_ =
547 colinfo_[col - 1].offset_ +
548 colinfo_[col - 1].width_ +
549 colinfo_[col - 1].skip_ +
551 //colinfo_[col].lines_ * vlinesep();
555 dim.wid = colinfo_[ncols() - 1].offset_
556 + colinfo_[ncols() - 1].width_
557 //+ vlinesep() * colinfo_[ncols()].lines_
560 dim.asc = -rowinfo_[0].offset_
561 + rowinfo_[0].ascent_
562 //+ hlinesep() * rowinfo_[0].lines_
565 dim.des = rowinfo_[nrows() - 1].offset_
566 + rowinfo_[nrows() - 1].descent_
567 //+ hlinesep() * rowinfo_[nrows()].lines_
572 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
574 for (idx_type idx = 0; idx < nargs(); ++idx)
575 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
579 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
583 if (!rowinfo_[row].crskip_.zero())
584 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
585 else if(!rowinfo_[row].allow_pagebreak_)
588 // make sure an upcoming '[' does not break anything
589 if (row + 1 < nrows()) {
590 MathData const & c = cell(index(row + 1, 0));
591 if (c.size() && c.front()->getChar() == '[')
596 // only add \\ if necessary
597 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
600 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
604 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
606 if (col + 1 == lastcol)
608 return from_ascii(" & ");
612 void InsetMathGrid::addRow(row_type row)
614 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
616 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
618 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
622 void InsetMathGrid::appendRow()
624 rowinfo_.push_back(RowInfo());
625 //cells_.insert(cells_.end(), ncols(), MathData());
626 for (col_type col = 0; col < ncols(); ++col) {
627 cells_.push_back(cells_type::value_type());
628 cellinfo_.push_back(CellInfo());
633 void InsetMathGrid::delRow(row_type row)
638 cells_type::iterator it = cells_.begin() + row * ncols();
639 cells_.erase(it, it + ncols());
641 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
642 cellinfo_.erase(jt, jt + ncols());
644 rowinfo_.erase(rowinfo_.begin() + row);
648 void InsetMathGrid::copyRow(row_type row)
651 for (col_type col = 0; col < ncols(); ++col)
652 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
656 void InsetMathGrid::swapRow(row_type row)
660 if (row + 1 == nrows())
662 for (col_type col = 0; col < ncols(); ++col)
663 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
667 void InsetMathGrid::addCol(col_type newcol)
669 const col_type nc = ncols();
670 const row_type nr = nrows();
671 cells_type new_cells((nc + 1) * nr);
672 vector<CellInfo> new_cellinfo((nc + 1) * nr);
674 for (row_type row = 0; row < nr; ++row)
675 for (col_type col = 0; col < nc; ++col) {
676 new_cells[row * (nc + 1) + col + (col > newcol)]
677 = cells_[row * nc + col];
678 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
679 = cellinfo_[row * nc + col];
681 swap(cells_, new_cells);
682 swap(cellinfo_, new_cellinfo);
685 inf.skip_ = defaultColSpace(newcol);
686 inf.align_ = defaultColAlign(newcol);
687 colinfo_.insert(colinfo_.begin() + newcol, inf);
691 void InsetMathGrid::delCol(col_type col)
697 vector<CellInfo> tmpcellinfo;
698 for (col_type i = 0; i < nargs(); ++i)
699 if (i % ncols() != col) {
700 tmpcells.push_back(cells_[i]);
701 tmpcellinfo.push_back(cellinfo_[i]);
703 swap(cells_, tmpcells);
704 swap(cellinfo_, tmpcellinfo);
706 colinfo_.erase(colinfo_.begin() + col);
710 void InsetMathGrid::copyCol(col_type col)
713 for (row_type row = 0; row < nrows(); ++row)
714 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
718 void InsetMathGrid::swapCol(col_type col)
722 if (col + 1 == ncols())
724 for (row_type row = 0; row < nrows(); ++row)
725 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
729 int InsetMathGrid::cellXOffset(idx_type idx) const
731 col_type c = col(idx);
732 int x = colinfo_[c].offset_;
733 char align = colinfo_[c].align_;
734 if (align == 'r' || align == 'R')
735 x += colinfo_[c].width_ - cell(idx).width();
736 if (align == 'c' || align == 'C')
737 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
742 int InsetMathGrid::cellYOffset(idx_type idx) const
744 return rowinfo_[row(idx)].offset_;
748 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
753 cur.idx() -= ncols();
755 if (cur.row() + 1 >= nrows())
757 cur.idx() += ncols();
759 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
764 bool InsetMathGrid::idxLeft(Cursor & cur) const
766 // leave matrix if on the left hand edge
770 cur.pos() = cur.lastpos();
775 bool InsetMathGrid::idxRight(Cursor & cur) const
777 // leave matrix if on the right hand edge
778 if (cur.col() + 1 == ncols())
786 bool InsetMathGrid::idxFirst(Cursor & cur) const
793 cur.idx() = (nrows() - 1) * ncols();
796 cur.idx() = ((nrows() - 1) / 2) * ncols();
803 bool InsetMathGrid::idxLast(Cursor & cur) const
807 cur.idx() = ncols() - 1;
810 cur.idx() = nargs() - 1;
813 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
815 cur.pos() = cur.lastpos();
820 bool InsetMathGrid::idxDelete(idx_type & idx)
822 // nothing to do if we have just one row
826 // nothing to do if we are in the middle of the last row of the inset
827 if (idx + ncols() > nargs())
830 // try to delete entire sequence of ncols() empty cells if possible
831 for (idx_type i = idx; i < idx + ncols(); ++i)
835 // move cells if necessary
836 for (idx_type i = index(row(idx), 0); i < idx; ++i)
837 swap(cell(i), cell(i + ncols()));
844 // undo effect of Ctrl-Tab (i.e. pull next cell)
845 //if (idx + 1 != nargs())
846 // cell(idx).swap(cell(idx + 1));
848 // we handled the event..
853 // reimplement old behaviour when pressing Delete in the last position
855 void InsetMathGrid::idxGlue(idx_type idx)
857 col_type c = col(idx);
858 if (c + 1 == ncols()) {
859 if (row(idx) + 1 != nrows()) {
860 for (col_type cc = 0; cc < ncols(); ++cc)
861 cell(idx).append(cell(idx + cc + 1));
862 delRow(row(idx) + 1);
865 cell(idx).append(cell(idx + 1));
866 for (col_type cc = c + 2; cc < ncols(); ++cc)
867 cell(idx - c + cc - 1) = cell(idx - c + cc);
868 cell(idx - c + ncols() - 1).clear();
873 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
875 return rowinfo_[row];
879 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
881 return rowinfo_[row];
885 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
887 row_type const ri = row(idx);
888 row_type const r1 = min(row(from), row(to));
889 row_type const r2 = max(row(from), row(to));
890 col_type const ci = col(idx);
891 col_type const c1 = min(col(from), col(to));
892 col_type const c2 = max(col(from), col(to));
893 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
898 void InsetMathGrid::normalize(NormalStream & os) const
901 for (row_type row = 0; row < nrows(); ++row) {
903 for (col_type col = 0; col < ncols(); ++col)
904 os << "[cell " << cell(index(row, col)) << ']';
911 void InsetMathGrid::mathmlize(MathStream & os) const
913 os << MTag("mtable");
914 for (row_type row = 0; row < nrows(); ++row) {
916 for (col_type col = 0; col < ncols(); ++col)
917 os << cell(index(row, col));
920 os << ETag("mtable");
924 void InsetMathGrid::write(WriteStream & os) const
927 for (row_type row = 0; row < nrows(); ++row) {
928 os << verboseHLine(rowinfo_[row].lines_);
929 // don't write & and empty cells at end of line
930 col_type lastcol = 0;
931 bool emptyline = true;
932 for (col_type col = 0; col < ncols(); ++col)
933 if (!cell(index(row, col)).empty()) {
937 for (col_type col = 0; col < lastcol; ++col)
938 os << cell(index(row, col)) << eocString(col, lastcol);
939 eol = eolString(row, emptyline, os.fragile());
941 // append newline only if line wasn't completely empty
942 // and this was not the last line in the grid
943 if (!emptyline && row + 1 < nrows())
946 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
958 int InsetMathGrid::colsep() const
964 int InsetMathGrid::rowsep() const
970 int InsetMathGrid::hlinesep() const
976 int InsetMathGrid::vlinesep() const
982 int InsetMathGrid::border() const
988 void InsetMathGrid::splitCell(Cursor & cur)
990 if (cur.idx() == cur.lastidx())
992 MathData ar = cur.cell();
993 ar.erase(0, cur.pos());
994 cur.cell().erase(cur.pos(), cur.lastpos());
997 cur.cell().insert(0, ar);
1001 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1003 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1004 switch (cmd.action) {
1006 // insert file functions
1007 case LFUN_LINE_DELETE:
1008 // FIXME: We use recordUndoInset when a change reflects more
1009 // than one cell, because recordUndo does not work for
1010 // multiple cells. Unfortunately this puts the cursor in front
1011 // of the inset after undo. This is (especilally for large
1013 recordUndoInset(cur);
1014 //autocorrect_ = false;
1022 if (cur.idx() > cur.lastidx())
1023 cur.idx() = cur.lastidx();
1024 if (cur.pos() > cur.lastpos())
1025 cur.pos() = cur.lastpos();
1028 case LFUN_CELL_SPLIT:
1033 case LFUN_CELL_BACKWARD:
1035 cur.selection() = false;
1036 if (!idxPrev(cur)) {
1037 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1042 case LFUN_CELL_FORWARD:
1043 // Can't handle selection by additional 'shift' as this is
1044 // hard bound to LFUN_CELL_BACKWARD
1045 cur.selection() = false;
1046 if (!idxNext(cur)) {
1047 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1052 case LFUN_BREAK_LINE: {
1053 recordUndoInset(cur);
1054 row_type const r = cur.row();
1058 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1059 swap(cell(index(r, c)), cell(index(r + 1, c)));
1063 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1066 cur.pos() = cur.lastpos();
1068 //mathcursor->normalize();
1069 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1073 case LFUN_TABULAR_FEATURE: {
1074 recordUndoInset(cur);
1075 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1076 istringstream is(to_utf8(cmd.argument()));
1079 if (s == "valign-top")
1081 else if (s == "valign-middle")
1083 else if (s == "valign-bottom")
1085 else if (s == "align-left")
1086 halign('l', cur.col());
1087 else if (s == "align-right")
1088 halign('r', cur.col());
1089 else if (s == "align-center")
1090 halign('c', cur.col());
1091 else if (s == "append-row")
1092 for (int i = 0, n = extractInt(is); i < n; ++i)
1094 else if (s == "delete-row") {
1095 for (int i = 0, n = extractInt(is); i < n; ++i) {
1097 if (cur.idx() >= nargs())
1098 cur.idx() -= ncols();
1100 cur.pos() = 0; // trick, see below
1102 else if (s == "copy-row") {
1103 // Here (as later) we save the cursor col/row
1104 // in order to restore it after operation.
1105 row_type const r = cur.row();
1106 col_type const c = cur.col();
1107 for (int i = 0, n = extractInt(is); i < n; ++i)
1109 cur.idx() = index(r, c);
1111 else if (s == "swap-row") {
1113 // Trick to suppress same-idx-means-different-cell
1117 else if (s == "add-hline-above")
1118 rowinfo_[cur.row()].lines_++;
1119 else if (s == "add-hline-below")
1120 rowinfo_[cur.row()+1].lines_++;
1121 else if (s == "delete-hline-above")
1122 rowinfo_[cur.row()].lines_--;
1123 else if (s == "delete-hline-below")
1124 rowinfo_[cur.row()+1].lines_--;
1125 else if (s == "append-column") {
1126 row_type const r = cur.row();
1127 col_type const c = cur.col();
1128 for (int i = 0, n = extractInt(is); i < n; ++i)
1130 cur.idx() = index(r, c);
1132 else if (s == "delete-column") {
1133 row_type const r = cur.row();
1134 col_type const c = cur.col();
1135 for (int i = 0, n = extractInt(is); i < n; ++i)
1136 delCol(col(cur.idx()));
1137 cur.idx() = index(r, min(c, cur.ncols() - 1));
1138 cur.pos() = 0; // trick, see above
1140 else if (s == "copy-column") {
1141 row_type const r = cur.row();
1142 col_type const c = cur.col();
1144 cur.idx() = index(r, c);
1146 else if (s == "swap-column") {
1148 cur.pos() = 0; // trick, see above
1150 else if (s == "add-vline-left")
1151 colinfo_[cur.col()].lines_++;
1152 else if (s == "add-vline-right")
1153 colinfo_[cur.col()+1].lines_++;
1154 else if (s == "delete-vline-left")
1155 colinfo_[cur.col()].lines_--;
1156 else if (s == "delete-vline-right")
1157 colinfo_[cur.col()+1].lines_--;
1162 lyxerr << "returning FINISHED_LEFT" << endl;
1167 cur.message(_("Paste"));
1168 cap::replaceSelection(cur);
1170 if (cmd.argument().empty() && !theClipboard().isInternal())
1171 topaste = theClipboard().getAsText();
1173 idocstringstream is(cmd.argument());
1176 topaste = cap::getSelection(cur.buffer(), n);
1178 InsetMathGrid grid(1, 1);
1179 if (!topaste.empty())
1180 mathed_parse_normal(grid, topaste);
1182 if (grid.nargs() == 1) {
1183 // single cell/part of cell
1185 cur.cell().insert(cur.pos(), grid.cell(0));
1186 cur.pos() += grid.cell(0).size();
1189 recordUndoInset(cur);
1190 col_type const numcols =
1191 min(grid.ncols(), ncols() - col(cur.idx()));
1192 row_type const numrows =
1193 min(grid.nrows(), nrows() - cur.row());
1194 for (row_type r = 0; r < numrows; ++r) {
1195 for (col_type c = 0; c < numcols; ++c) {
1196 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1197 cell(i).insert(0, grid.cell(grid.index(r, c)));
1199 // append the left over horizontal cells to the last column
1200 idx_type i = index(r + cur.row(), ncols() - 1);
1201 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1202 cell(i).append(grid.cell(grid.index(r, c)));
1204 // append the left over vertical cells to the last _cell_
1205 idx_type i = nargs() - 1;
1206 for (row_type r = numrows; r < grid.nrows(); ++r)
1207 for (col_type c = 0; c < grid.ncols(); ++c)
1208 cell(i).append(grid.cell(grid.index(r, c)));
1210 cur.clearSelection(); // bug 393
1211 cur.bv().switchKeyMap();
1216 case LFUN_LINE_BEGIN_SELECT:
1217 case LFUN_LINE_BEGIN:
1218 case LFUN_WORD_BACKWARD_SELECT:
1219 case LFUN_WORD_BACKWARD:
1220 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1221 cmd.action == LFUN_LINE_BEGIN_SELECT);
1222 cur.macroModeClose();
1223 if (cur.pos() != 0) {
1225 } else if (cur.idx() % cur.ncols() != 0) {
1226 cur.idx() -= cur.idx() % cur.ncols();
1228 } else if (cur.idx() != 0) {
1232 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1237 case LFUN_WORD_FORWARD_SELECT:
1238 case LFUN_WORD_FORWARD:
1239 case LFUN_LINE_END_SELECT:
1241 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1242 cmd.action == LFUN_LINE_END_SELECT);
1243 cur.macroModeClose();
1245 if (cur.pos() != cur.lastpos()) {
1246 cur.pos() = cur.lastpos();
1247 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1248 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1249 cur.pos() = cur.lastpos();
1250 } else if (cur.idx() != cur.lastidx()) {
1251 cur.idx() = cur.lastidx();
1252 cur.pos() = cur.lastpos();
1254 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1260 InsetMathNest::doDispatch(cur, cmd);
1265 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1266 FuncStatus & status) const
1268 switch (cmd.action) {
1269 case LFUN_TABULAR_FEATURE: {
1270 string const s = to_utf8(cmd.argument());
1271 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1272 status.enabled(false);
1273 status.message(from_utf8(N_("Only one row")));
1277 (s == "delete-column" || s == "swap-column")) {
1278 status.enabled(false);
1279 status.message(from_utf8(N_("Only one column")));
1282 if ((rowinfo_[cur.row()].lines_ == 0 &&
1283 s == "delete-hline-above") ||
1284 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1285 s == "delete-hline-below")) {
1286 status.enabled(false);
1287 status.message(from_utf8(N_("No hline to delete")));
1291 if ((colinfo_[cur.col()].lines_ == 0 &&
1292 s == "delete-vline-left") ||
1293 (colinfo_[cur.col() + 1].lines_ == 0 &&
1294 s == "delete-vline-right")) {
1295 status.enabled(false);
1296 status.message(from_utf8(N_("No vline to delete")));
1299 if (s == "valign-top" || s == "valign-middle" ||
1300 s == "valign-bottom" || s == "align-left" ||
1301 s == "align-right" || s == "align-center" ||
1302 s == "append-row" || s == "delete-row" ||
1303 s == "copy-row" || s == "swap-row" ||
1304 s == "add-hline-above" || s == "add-hline-below" ||
1305 s == "delete-hline-above" || s == "delete-hline-below" ||
1306 s == "append-column" || s == "delete-column" ||
1307 s == "copy-column" || s == "swap-column" ||
1308 s == "add-vline-left" || s == "add-vline-right" ||
1309 s == "delete-vline-left" || s == "delete-vline-right")
1310 status.enabled(true);
1312 status.enabled(false);
1313 status.message(bformat(
1314 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1317 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1318 || s == "align-right" && halign(cur.col()) == 'r'
1319 || s == "align-center" && halign(cur.col()) == 'c'
1320 || s == "valign-top" && valign() == 't'
1321 || s == "valign-bottom" && valign() == 'b'
1322 || s == "valign-middle" && valign() == 'm');
1325 // FIXME: What did this code do?
1326 // Please check whether it is still needed!
1327 // should be more precise
1328 if (v_align_ == '\0') {
1329 status.enable(true);
1332 if (cmd.argument().empty()) {
1333 status.enable(false);
1336 if (!support::contains("tcb", cmd.argument()[0])) {
1337 status.enable(false);
1340 status.setOnOff(cmd.argument()[0] == v_align_);
1341 status.enabled(true);
1346 case LFUN_CELL_SPLIT:
1347 status.enabled(true);
1350 case LFUN_CELL_BACKWARD:
1351 case LFUN_CELL_FORWARD:
1352 status.enabled(true);
1356 return InsetMathNest::getStatus(cur, cmd, status);