2 * \file math_gridinset.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 "math_gridinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_streamstr.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;
47 using std::ostringstream;
51 class GridInsetMailer : public MailInset {
53 GridInsetMailer(MathGridInset & inset) : inset_(inset) {}
55 virtual string const & name() const
57 static string const theName = "tabular";
61 virtual string const inset2string(Buffer const &) const
64 //data << name() << " active_cell " << inset.getActCell() << '\n';
65 data << name() << " active_cell " << 0 << '\n';
72 InsetBase & inset() const { return inset_; }
73 MathGridInset & inset_;
77 void mathed_parse_normal(MathGridInset &, string const & argument);
81 string verboseHLine(int n)
84 for (int i = 0; i < n; ++i)
92 int extractInt(istream & is)
96 return (num == 0) ? 1 : num;
102 //////////////////////////////////////////////////////////////
105 MathGridInset::CellInfo::CellInfo()
112 //////////////////////////////////////////////////////////////
115 MathGridInset::RowInfo::RowInfo()
116 : lines_(0), skip_(0)
121 int MathGridInset::RowInfo::skipPixels() const
123 return crskip_.inBP();
128 //////////////////////////////////////////////////////////////
131 MathGridInset::ColInfo::ColInfo()
132 : align_('c'), lines_(0)
136 //////////////////////////////////////////////////////////////
139 MathGridInset::MathGridInset(char v, string const & h)
140 : MathNestInset(guessColumns(h)),
142 colinfo_(guessColumns(h) + 1),
143 cellinfo_(1 * guessColumns(h))
148 //lyxerr << "created grid with " << ncols() << " columns" << endl;
152 MathGridInset::MathGridInset()
163 MathGridInset::MathGridInset(col_type m, row_type n)
164 : MathNestInset(m * n),
174 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
175 : MathNestInset(m * n),
187 MathGridInset::~MathGridInset()
189 GridInsetMailer mailer(*this);
194 auto_ptr<InsetBase> MathGridInset::doClone() const
196 return auto_ptr<InsetBase>(new MathGridInset(*this));
200 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
202 return col + ncols() * row;
206 void MathGridInset::setDefaults()
209 lyxerr << "positive number of columns expected" << endl;
211 // lyxerr << "positive number of rows expected" << endl;
212 for (col_type col = 0; col < ncols(); ++col) {
213 colinfo_[col].align_ = defaultColAlign(col);
214 colinfo_[col].skip_ = defaultColSpace(col);
219 void MathGridInset::halign(string const & hh)
222 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
225 colinfo_[col].lines_++;
226 } else if (col >= ncols()) {
227 // Only '|' is allowed in the last dummy column
229 } else if (c == 'c' || c == 'l' || c == 'r') {
230 colinfo_[col].align_ = c;
232 colinfo_[col].lines_ = 0;
234 lyxerr << "unknown column separator: '" << c << "'" << endl;
239 col_type n = hh.size();
242 for (col_type col = 0; col < n; ++col)
243 colinfo_[col].align_ = hh[col];
248 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
251 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
252 if (*it == 'c' || *it == 'l' || *it == 'r')
254 // let's have at least one column, even if we did not recognize its
262 void MathGridInset::halign(char h, col_type col)
264 colinfo_[col].align_ = h;
268 char MathGridInset::halign(col_type col) const
270 return colinfo_[col].align_;
274 string MathGridInset::halign() const
277 for (col_type col = 0; col < ncols(); ++col) {
278 res += string(colinfo_[col].lines_, '|');
279 res += colinfo_[col].align_;
281 return res + string(colinfo_[ncols()].lines_, '|');
285 void MathGridInset::valign(char c)
291 char MathGridInset::valign() const
297 MathGridInset::col_type MathGridInset::ncols() const
299 return colinfo_.size() - 1;
303 MathGridInset::row_type MathGridInset::nrows() const
305 return rowinfo_.size() - 1;
309 MathGridInset::col_type MathGridInset::col(idx_type idx) const
311 return idx % ncols();
315 MathGridInset::row_type MathGridInset::row(idx_type idx) const
317 return idx / ncols();
321 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
323 rowinfo_[row].crskip_ = crskip;
327 LyXLength MathGridInset::vcrskip(row_type row) const
329 return rowinfo_[row].crskip_;
333 void MathGridInset::metrics(MetricsInfo & mi) const
335 // let the cells adjust themselves
336 MathNestInset::metrics(mi);
338 // compute absolute sizes of vertical structure
339 for (row_type row = 0; row < nrows(); ++row) {
342 for (col_type col = 0; col < ncols(); ++col) {
343 MathArray const & c = cell(index(row, col));
344 asc = max(asc, c.ascent());
345 desc = max(desc, c.descent());
347 rowinfo_[row].ascent_ = asc;
348 rowinfo_[row].descent_ = desc;
350 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
351 rowinfo_[nrows()].ascent_ = 0;
352 rowinfo_[nrows()].descent_ = 0;
354 // compute vertical offsets
355 rowinfo_[0].offset_ = 0;
356 for (row_type row = 1; row <= nrows(); ++row) {
357 rowinfo_[row].offset_ =
358 rowinfo_[row - 1].offset_ +
359 rowinfo_[row - 1].descent_ +
360 rowinfo_[row - 1].skipPixels() +
362 rowinfo_[row].lines_ * hlinesep() +
363 rowinfo_[row].ascent_;
366 // adjust vertical offset
373 h = rowinfo_[nrows() - 1].offset_;
376 h = rowinfo_[nrows() - 1].offset_ / 2;
378 for (row_type row = 0; row <= nrows(); ++row)
379 rowinfo_[row].offset_ -= h;
382 // compute absolute sizes of horizontal structure
383 for (col_type col = 0; col < ncols(); ++col) {
385 for (row_type row = 0; row < nrows(); ++row)
386 wid = max(wid, cell(index(row, col)).width());
387 colinfo_[col].width_ = wid;
389 colinfo_[ncols()].width_ = 0;
391 // compute horizontal offsets
392 colinfo_[0].offset_ = border();
393 for (col_type col = 1; col <= ncols(); ++col) {
394 colinfo_[col].offset_ =
395 colinfo_[col - 1].offset_ +
396 colinfo_[col - 1].width_ +
397 colinfo_[col - 1].skip_ +
399 colinfo_[col].lines_ * vlinesep();
403 dim_.wid = colinfo_[ncols() - 1].offset_
404 + colinfo_[ncols() - 1].width_
405 + vlinesep() * colinfo_[ncols()].lines_
408 dim_.asc = - rowinfo_[0].offset_
409 + rowinfo_[0].ascent_
410 + hlinesep() * rowinfo_[0].lines_
413 dim_.des = rowinfo_[nrows() - 1].offset_
414 + rowinfo_[nrows() - 1].descent_
415 + hlinesep() * rowinfo_[nrows()].lines_
420 // Increase ws_[i] for 'R' columns (except the first one)
421 for (int i = 1; i < nc_; ++i)
422 if (align_[i] == 'R')
423 ws_[i] += 10 * df_width;
424 // Increase ws_[i] for 'C' column
425 if (align_[0] == 'C')
426 if (ws_[0] < 7 * workwidth / 8)
427 ws_[0] = 7 * workwidth / 8;
431 for (cxrow = row_.begin(); cxrow; ++cxrow) {
434 for (int i = 0; i < nc_; ++i) {
436 if (cxrow->getTab(i) <= 0) {
437 cxrow->setTab(i, df_width);
445 lf = (ws_[i] - cxrow->getTab(i))/2;
449 lf = ws_[i] - cxrow->getTab(i);
452 if (cxrow == row_.begin())
454 else if (cxrow.is_last())
455 lf = ws_[i] - cxrow->getTab(i);
457 lf = (ws_[i] - cxrow->getTab(i))/2;
460 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
461 cxrow->setTab(i, lf + rg);
462 rg = ws_[i] - ww + colsep();
463 if (cxrow == row_.begin())
464 width += ws_[i] + colsep();
466 cxrow->setBaseline(cxrow->getBaseline() - ascent);
469 metricsMarkers2(dim_);
473 void MathGridInset::metrics(MetricsInfo & mi, Dimension & dim) const
480 void MathGridInset::draw(PainterInfo & pi, int x, int y) const
482 drawWithMargin(pi, x, y, 0, 0);
485 void MathGridInset::drawWithMargin(PainterInfo & pi, int x, int y,
486 int lmargin, int rmargin) const
488 for (idx_type idx = 0; idx < nargs(); ++idx)
489 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
490 y + cellYOffset(idx));
492 for (row_type row = 0; row <= nrows(); ++row)
493 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
494 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
495 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
496 pi.pain.line(x + lmargin + 1, yy,
497 x + dim_.width() - rmargin - 1, yy,
501 for (col_type col = 0; col <= ncols(); ++col)
502 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
503 int xx = x + lmargin + colinfo_[col].offset_
504 - i * vlinesep() - vlinesep()/2 - colsep()/2;
505 pi.pain.line(xx, y - dim_.ascent() + 1,
506 xx, y + dim_.descent() - 1,
509 drawMarkers2(pi, x, y);
513 void MathGridInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
515 // let the cells adjust themselves
516 //MathNestInset::metrics(mi);
517 for (idx_type i = 0; i < nargs(); ++i)
518 cell(i).metricsT(mi, dim);
520 // compute absolute sizes of vertical structure
521 for (row_type row = 0; row < nrows(); ++row) {
524 for (col_type col = 0; col < ncols(); ++col) {
525 MathArray const & c = cell(index(row, col));
526 asc = max(asc, c.ascent());
527 desc = max(desc, c.descent());
529 rowinfo_[row].ascent_ = asc;
530 rowinfo_[row].descent_ = desc;
532 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
533 rowinfo_[nrows()].ascent_ = 0;
534 rowinfo_[nrows()].descent_ = 0;
536 // compute vertical offsets
537 rowinfo_[0].offset_ = 0;
538 for (row_type row = 1; row <= nrows(); ++row) {
539 rowinfo_[row].offset_ =
540 rowinfo_[row - 1].offset_ +
541 rowinfo_[row - 1].descent_ +
542 //rowinfo_[row - 1].skipPixels() +
544 //rowinfo_[row].lines_ * hlinesep() +
545 rowinfo_[row].ascent_;
548 // adjust vertical offset
555 h = rowinfo_[nrows() - 1].offset_;
558 h = rowinfo_[nrows() - 1].offset_ / 2;
560 for (row_type row = 0; row <= nrows(); ++row)
561 rowinfo_[row].offset_ -= h;
564 // compute absolute sizes of horizontal structure
565 for (col_type col = 0; col < ncols(); ++col) {
567 for (row_type row = 0; row < nrows(); ++row)
568 wid = max(wid, cell(index(row, col)).width());
569 colinfo_[col].width_ = wid;
571 colinfo_[ncols()].width_ = 0;
573 // compute horizontal offsets
574 colinfo_[0].offset_ = border();
575 for (col_type col = 1; col <= ncols(); ++col) {
576 colinfo_[col].offset_ =
577 colinfo_[col - 1].offset_ +
578 colinfo_[col - 1].width_ +
579 colinfo_[col - 1].skip_ +
581 //colinfo_[col].lines_ * vlinesep();
585 dim.wid = colinfo_[ncols() - 1].offset_
586 + colinfo_[ncols() - 1].width_
587 //+ vlinesep() * colinfo_[ncols()].lines_
590 dim.asc = -rowinfo_[0].offset_
591 + rowinfo_[0].ascent_
592 //+ hlinesep() * rowinfo_[0].lines_
595 dim.des = rowinfo_[nrows() - 1].offset_
596 + rowinfo_[nrows() - 1].descent_
597 //+ hlinesep() * rowinfo_[nrows()].lines_
602 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
604 for (idx_type idx = 0; idx < nargs(); ++idx)
605 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
609 string MathGridInset::eolString(row_type row, bool emptyline, bool fragile) const
613 if (!rowinfo_[row].crskip_.zero())
614 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
616 // make sure an upcoming '[' does not break anything
617 if (row + 1 < nrows()) {
618 MathArray const & c = cell(index(row + 1, 0));
619 if (c.size() && c.front()->getChar() == '[')
624 // only add \\ if necessary
625 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
628 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
632 string MathGridInset::eocString(col_type col, col_type lastcol) const
634 if (col + 1 == lastcol)
640 void MathGridInset::addRow(row_type row)
642 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
644 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
646 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
650 void MathGridInset::appendRow()
652 rowinfo_.push_back(RowInfo());
653 //cells_.insert(cells_.end(), ncols(), MathArray());
654 for (col_type col = 0; col < ncols(); ++col) {
655 cells_.push_back(cells_type::value_type());
656 cellinfo_.push_back(CellInfo());
661 void MathGridInset::delRow(row_type row)
666 cells_type::iterator it = cells_.begin() + row * ncols();
667 cells_.erase(it, it + ncols());
669 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
670 cellinfo_.erase(jt, jt + ncols());
672 rowinfo_.erase(rowinfo_.begin() + row);
676 void MathGridInset::copyRow(row_type row)
679 for (col_type col = 0; col < ncols(); ++col)
680 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
684 void MathGridInset::swapRow(row_type row)
688 if (row + 1 == nrows())
690 for (col_type col = 0; col < ncols(); ++col)
691 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
695 void MathGridInset::addCol(col_type newcol)
697 const col_type nc = ncols();
698 const row_type nr = nrows();
699 cells_type new_cells((nc + 1) * nr);
700 vector<CellInfo> new_cellinfo((nc + 1) * nr);
702 for (row_type row = 0; row < nr; ++row)
703 for (col_type col = 0; col < nc; ++col) {
704 new_cells[row * (nc + 1) + col + (col > newcol)]
705 = cells_[row * nc + col];
706 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
707 = cellinfo_[row * nc + col];
709 swap(cells_, new_cells);
710 swap(cellinfo_, new_cellinfo);
713 inf.skip_ = defaultColSpace(newcol);
714 inf.align_ = defaultColAlign(newcol);
715 colinfo_.insert(colinfo_.begin() + newcol, inf);
719 void MathGridInset::delCol(col_type col)
725 vector<CellInfo> tmpcellinfo;
726 for (col_type i = 0; i < nargs(); ++i)
727 if (i % ncols() != col) {
728 tmpcells.push_back(cells_[i]);
729 tmpcellinfo.push_back(cellinfo_[i]);
731 swap(cells_, tmpcells);
732 swap(cellinfo_, tmpcellinfo);
734 colinfo_.erase(colinfo_.begin() + col);
738 void MathGridInset::copyCol(col_type col)
741 for (row_type row = 0; row < nrows(); ++row)
742 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
746 void MathGridInset::swapCol(col_type col)
750 if (col + 1 == ncols())
752 for (row_type row = 0; row < nrows(); ++row)
753 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
757 int MathGridInset::cellXOffset(idx_type idx) const
759 col_type c = col(idx);
760 int x = colinfo_[c].offset_;
761 char align = colinfo_[c].align_;
762 if (align == 'r' || align == 'R')
763 x += colinfo_[c].width_ - cell(idx).width();
764 if (align == 'c' || align == 'C')
765 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
770 int MathGridInset::cellYOffset(idx_type idx) const
772 return rowinfo_[row(idx)].offset_;
776 bool MathGridInset::idxUpDown(LCursor & cur, bool up) const
781 cur.idx() -= ncols();
783 if (cur.row() + 1 >= nrows())
785 cur.idx() += ncols();
787 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo());
792 bool MathGridInset::idxLeft(LCursor & cur) const
794 // leave matrix if on the left hand edge
798 cur.pos() = cur.lastpos();
803 bool MathGridInset::idxRight(LCursor & cur) const
805 // leave matrix if on the right hand edge
806 if (cur.col() + 1 == ncols())
814 bool MathGridInset::idxFirst(LCursor & cur) const
821 cur.idx() = (nrows() - 1) * ncols();
824 cur.idx() = ((nrows() - 1) / 2) * ncols();
831 bool MathGridInset::idxLast(LCursor & cur) const
835 cur.idx() = ncols() - 1;
838 cur.idx() = nargs() - 1;
841 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
843 cur.pos() = cur.lastpos();
848 bool MathGridInset::idxDelete(idx_type & idx)
850 // nothing to do if we have just one row
854 // nothing to do if we are in the middle of the last row of the inset
855 if (idx + ncols() > nargs())
858 // try to delete entire sequence of ncols() empty cells if possible
859 for (idx_type i = idx; i < idx + ncols(); ++i)
863 // move cells if necessary
864 for (idx_type i = index(row(idx), 0); i < idx; ++i)
865 swap(cell(i), cell(i + ncols()));
872 // undo effect of Ctrl-Tab (i.e. pull next cell)
873 //if (idx + 1 != nargs())
874 // cell(idx).swap(cell(idx + 1));
876 // we handled the event..
881 // reimplement old behaviour when pressing Delete in the last position
883 void MathGridInset::idxGlue(idx_type idx)
885 col_type c = col(idx);
886 if (c + 1 == ncols()) {
887 if (row(idx) + 1 != nrows()) {
888 for (col_type cc = 0; cc < ncols(); ++cc)
889 cell(idx).append(cell(idx + cc + 1));
890 delRow(row(idx) + 1);
893 cell(idx).append(cell(idx + 1));
894 for (col_type cc = c + 2; cc < ncols(); ++cc)
895 cell(idx - c + cc - 1) = cell(idx - c + cc);
896 cell(idx - c + ncols() - 1).clear();
901 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
903 return rowinfo_[row];
907 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
909 return rowinfo_[row];
913 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
915 row_type const ri = row(idx);
916 row_type const r1 = min(row(from), row(to));
917 row_type const r2 = max(row(from), row(to));
918 col_type const ci = col(idx);
919 col_type const c1 = min(col(from), col(to));
920 col_type const c2 = max(col(from), col(to));
921 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
926 void MathGridInset::normalize(NormalStream & os) const
929 for (row_type row = 0; row < nrows(); ++row) {
931 for (col_type col = 0; col < ncols(); ++col)
932 os << "[cell " << cell(index(row, col)) << ']';
939 void MathGridInset::mathmlize(MathMLStream & os) const
941 os << MTag("mtable");
942 for (row_type row = 0; row < nrows(); ++row) {
944 for (col_type col = 0; col < ncols(); ++col)
945 os << cell(index(row, col));
948 os << ETag("mtable");
952 void MathGridInset::write(WriteStream & os) const
955 for (row_type row = 0; row < nrows(); ++row) {
956 os << verboseHLine(rowinfo_[row].lines_);
957 // don't write & and empty cells at end of line
958 col_type lastcol = 0;
959 bool emptyline = true;
960 for (col_type col = 0; col < ncols(); ++col)
961 if (!cell(index(row, col)).empty()) {
965 for (col_type col = 0; col < lastcol; ++col)
966 os << cell(index(row, col)) << eocString(col, lastcol);
967 eol = eolString(row, emptyline, os.fragile());
969 // append newline only if line wasn't completely empty
970 // and this was not the last line in the grid
971 if (!emptyline && row + 1 < nrows())
974 string const s = verboseHLine(rowinfo_[nrows()].lines_);
986 int MathGridInset::colsep() const
992 int MathGridInset::rowsep() const
998 int MathGridInset::hlinesep() const
1004 int MathGridInset::vlinesep() const
1010 int MathGridInset::border() const
1016 void MathGridInset::splitCell(LCursor & cur)
1018 if (cur.idx() == cur.lastidx())
1020 MathArray ar = cur.cell();
1021 ar.erase(0, cur.pos());
1022 cur.cell().erase(cur.pos(), cur.lastpos());
1025 cur.cell().insert(0, ar);
1029 void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
1031 //lyxerr << "*** MathGridInset: request: " << cmd << endl;
1032 switch (cmd.action) {
1034 case LFUN_MOUSE_RELEASE:
1035 //if (cmd.button() == mouse_button::button3) {
1036 // GridInsetMailer(*this).showDialog();
1037 // return DispatchResult(true, true);
1039 MathNestInset::doDispatch(cur, cmd);
1042 case LFUN_INSET_DIALOG_UPDATE:
1043 GridInsetMailer(*this).updateDialog(&cur.bv());
1046 // insert file functions
1047 case LFUN_LINE_DELETE:
1048 // FIXME: We use recordUndoInset when a change reflects more
1049 // than one cell, because recordUndo does not work for
1050 // multiple cells. Unfortunately this puts the cursor in front
1051 // of the inset after undo. This is (especilally for large
1053 recordUndoInset(cur);
1054 //autocorrect_ = false;
1062 if (cur.idx() > cur.lastidx())
1063 cur.idx() = cur.lastidx();
1064 if (cur.pos() > cur.lastpos())
1065 cur.pos() = cur.lastpos();
1068 case LFUN_CELL_SPLIT:
1073 case LFUN_CELL_BACKWARD:
1075 cur.selection() = false;
1076 if (!idxPrev(cur)) {
1077 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1082 case LFUN_CELL_FORWARD:
1083 // Can't handle selection by additional 'shift' as this is
1084 // hard bound to LFUN_CELL_BACKWARD
1085 cur.selection() = false;
1086 if (!idxNext(cur)) {
1087 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1092 case LFUN_BREAK_LINE: {
1093 recordUndoInset(cur);
1094 row_type const r = cur.row();
1098 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1099 swap(cell(index(r, c)), cell(index(r + 1, c)));
1103 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1106 cur.pos() = cur.lastpos();
1108 //mathcursor->normalize();
1109 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1113 case LFUN_TABULAR_FEATURE: {
1114 recordUndoInset(cur);
1115 //lyxerr << "handling tabular-feature " << cmd.argument << endl;
1116 istringstream is(cmd.argument);
1119 if (s == "valign-top")
1121 else if (s == "valign-middle")
1123 else if (s == "valign-bottom")
1125 else if (s == "align-left")
1126 halign('l', cur.col());
1127 else if (s == "align-right")
1128 halign('r', cur.col());
1129 else if (s == "align-center")
1130 halign('c', cur.col());
1131 else if (s == "append-row")
1132 for (int i = 0, n = extractInt(is); i < n; ++i)
1134 else if (s == "delete-row") {
1135 for (int i = 0, n = extractInt(is); i < n; ++i) {
1137 if (cur.idx() > nargs())
1138 cur.idx() -= ncols();
1140 cur.pos() = 0; // trick, see below
1142 else if (s == "copy-row") {
1143 // Here (as later) we save the cursor col/row
1144 // in order to restore it after operation.
1145 row_type const r = cur.row();
1146 col_type const c = cur.col();
1147 for (int i = 0, n = extractInt(is); i < n; ++i)
1149 cur.idx() = index(r, c);
1151 else if (s == "swap-row") {
1153 // Trick to suppress same-idx-means-different-cell
1157 else if (s == "add-hline-above")
1158 rowinfo_[cur.row()].lines_++;
1159 else if (s == "add-hline-below")
1160 rowinfo_[cur.row()+1].lines_++;
1161 else if (s == "delete-hline-above")
1162 rowinfo_[cur.row()].lines_--;
1163 else if (s == "delete-hline-below")
1164 rowinfo_[cur.row()+1].lines_--;
1165 else if (s == "append-column") {
1166 row_type const r = cur.row();
1167 col_type const c = cur.col();
1168 for (int i = 0, n = extractInt(is); i < n; ++i)
1170 cur.idx() = index(r, c);
1172 else if (s == "delete-column") {
1173 row_type const r = cur.row();
1174 col_type const c = cur.col();
1175 for (int i = 0, n = extractInt(is); i < n; ++i)
1176 delCol(col(cur.idx()));
1177 cur.idx() = index(r, min(c, cur.ncols() - 1));
1178 cur.pos() = 0; // trick, see above
1180 else if (s == "copy-column") {
1181 row_type const r = cur.row();
1182 col_type const c = cur.col();
1184 cur.idx() = index(r, c);
1186 else if (s == "swap-column") {
1188 cur.pos() = 0; // trick, see above
1190 else if (s == "add-vline-left")
1191 colinfo_[cur.col()].lines_++;
1192 else if (s == "add-vline-right")
1193 colinfo_[cur.col()+1].lines_++;
1194 else if (s == "delete-vline-left")
1195 colinfo_[cur.col()].lines_--;
1196 else if (s == "delete-vline-right")
1197 colinfo_[cur.col()+1].lines_--;
1202 lyxerr << "returning FINISHED_LEFT" << endl;
1207 cur.message(_("Paste"));
1208 lyx::cap::replaceSelection(cur);
1209 istringstream is(cmd.argument);
1212 MathGridInset grid(1, 1);
1213 mathed_parse_normal(grid, lyx::cap::getSelection(cur.buffer(), n));
1214 if (grid.nargs() == 1) {
1215 // single cell/part of cell
1217 cur.cell().insert(cur.pos(), grid.cell(0));
1218 cur.pos() += grid.cell(0).size();
1221 recordUndoInset(cur);
1222 col_type const numcols =
1223 min(grid.ncols(), ncols() - col(cur.idx()));
1224 row_type const numrows =
1225 min(grid.nrows(), nrows() - cur.row());
1226 for (row_type r = 0; r < numrows; ++r) {
1227 for (col_type c = 0; c < numcols; ++c) {
1228 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1229 cell(i).insert(0, grid.cell(grid.index(r, c)));
1231 // append the left over horizontal cells to the last column
1232 idx_type i = index(r + cur.row(), ncols() - 1);
1233 for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1234 cell(i).append(grid.cell(grid.index(r, c)));
1236 // append the left over vertical cells to the last _cell_
1237 idx_type i = nargs() - 1;
1238 for (row_type r = numrows; r < grid.nrows(); ++r)
1239 for (col_type c = 0; c < grid.ncols(); ++c)
1240 cell(i).append(grid.cell(grid.index(r, c)));
1242 cur.clearSelection(); // bug 393
1243 cur.bv().switchKeyMap();
1248 case LFUN_LINE_BEGIN_SELECT:
1249 case LFUN_LINE_BEGIN:
1250 case LFUN_WORD_BACKWARD_SELECT:
1251 case LFUN_WORD_BACKWARD:
1252 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1253 cmd.action == LFUN_LINE_BEGIN_SELECT);
1254 cur.macroModeClose();
1255 if (cur.pos() != 0) {
1257 } else if (cur.idx() % cur.ncols() != 0) {
1258 cur.idx() -= cur.idx() % cur.ncols();
1260 } else if (cur.idx() != 0) {
1264 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1269 case LFUN_WORD_FORWARD_SELECT:
1270 case LFUN_WORD_FORWARD:
1271 case LFUN_LINE_END_SELECT:
1273 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1274 cmd.action == LFUN_LINE_END_SELECT);
1275 cur.macroModeClose();
1277 if (cur.pos() != cur.lastpos()) {
1278 cur.pos() = cur.lastpos();
1279 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1280 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1281 cur.pos() = cur.lastpos();
1282 } else if (cur.idx() != cur.lastidx()) {
1283 cur.idx() = cur.lastidx();
1284 cur.pos() = cur.lastpos();
1286 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1292 MathNestInset::doDispatch(cur, cmd);
1297 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1298 FuncStatus & status) const
1300 switch (cmd.action) {
1301 case LFUN_TABULAR_FEATURE: {
1302 string const s = cmd.argument;
1303 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1304 status.enabled(false);
1305 status.message(N_("Only one row"));
1309 (s == "delete-column" || s == "swap-column")) {
1310 status.enabled(false);
1311 status.message(N_("Only one column"));
1314 if ((rowinfo_[cur.row()].lines_ == 0 &&
1315 s == "delete-hline-above") ||
1316 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1317 s == "delete-hline-below")) {
1318 status.enabled(false);
1319 status.message(N_("No hline to delete"));
1323 if ((colinfo_[cur.col()].lines_ == 0 &&
1324 s == "delete-vline-left") ||
1325 (colinfo_[cur.col() + 1].lines_ == 0 &&
1326 s == "delete-vline-right")) {
1327 status.enabled(false);
1328 status.message(N_("No vline to delete"));
1331 if (s == "valign-top" || s == "valign-middle" ||
1332 s == "valign-bottom" || s == "align-left" ||
1333 s == "align-right" || s == "align-center" ||
1334 s == "append-row" || s == "delete-row" ||
1335 s == "copy-row" || s == "swap-row" ||
1336 s == "add-hline-above" || s == "add-hline-below" ||
1337 s == "delete-hline-above" || s == "delete-hline-below" ||
1338 s == "append-column" || s == "delete-column" ||
1339 s == "copy-column" || s == "swap-column" ||
1340 s == "add-vline-left" || s == "add-vline-right" ||
1341 s == "delete-vline-left" || s == "delete-vline-right")
1342 status.enabled(true);
1344 status.enabled(false);
1345 status.message(bformat(
1346 N_("Unknown tabular feature '%1$s'"), s));
1349 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1350 || s == "align-right" && halign(cur.col()) == 'r'
1351 || s == "align-center" && halign(cur.col()) == 'c'
1352 || s == "valign-top" && valign() == 't'
1353 || s == "valign-bottom" && valign() == 'b'
1354 || s == "valign-middle" && valign() == 'm');
1357 // FIXME: What did this code do?
1358 // Please check whether it is still needed!
1359 // should be more precise
1360 if (v_align_ == '\0') {
1361 status.enable(true);
1364 if (cmd.argument.empty()) {
1365 status.enable(false);
1368 if (!lyx::support::contains("tcb", cmd.argument[0])) {
1369 status.enable(false);
1372 status.setOnOff(cmd.argument[0] == v_align_);
1373 status.enabled(true);
1378 case LFUN_CELL_SPLIT:
1379 status.enabled(true);
1382 case LFUN_CELL_BACKWARD:
1383 case LFUN_CELL_FORWARD:
1384 status.enabled(true);
1388 return MathNestInset::getStatus(cur, cmd, status);