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 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())
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
779 if (cur.idx() < ncols())
781 cur.idx() -= ncols();
783 if (cur.idx() >= ncols() * (nrows() - 1))
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
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 os << eolString(row, os.fragile());
967 // append newline only if line wasn't completely empty
968 // and this was not the last line in the grid
969 if (!emptyline && row + 1 < nrows())
972 string const s = verboseHLine(rowinfo_[nrows()].lines_);
973 if (!s.empty() && s != " ") {
981 int MathGridInset::colsep() const
987 int MathGridInset::rowsep() const
993 int MathGridInset::hlinesep() const
999 int MathGridInset::vlinesep() const
1005 int MathGridInset::border() const
1011 void MathGridInset::splitCell(LCursor & cur)
1013 if (cur.idx() == cur.lastidx())
1015 MathArray ar = cur.cell();
1016 ar.erase(0, cur.pos());
1017 cur.cell().erase(cur.pos(), cur.lastpos());
1020 cur.cell().insert(0, ar);
1024 void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
1026 //lyxerr << "*** MathGridInset: request: " << cmd << endl;
1027 switch (cmd.action) {
1029 case LFUN_MOUSE_RELEASE:
1030 //if (cmd.button() == mouse_button::button3) {
1031 // GridInsetMailer(*this).showDialog();
1032 // return DispatchResult(true, true);
1034 MathNestInset::doDispatch(cur, cmd);
1037 case LFUN_INSET_DIALOG_UPDATE:
1038 GridInsetMailer(*this).updateDialog(&cur.bv());
1041 // insert file functions
1042 case LFUN_DELETE_LINE_FORWARD:
1044 //autocorrect_ = false;
1052 if (cur.idx() > cur.lastidx())
1053 cur.idx() = cur.lastidx();
1054 if (cur.pos() > cur.lastpos())
1055 cur.pos() = cur.lastpos();
1058 case LFUN_CELL_SPLIT:
1063 case LFUN_BREAKLINE: {
1065 row_type const r = cur.row();
1069 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1070 swap(cell(index(r, c)), cell(index(r + 1, c)));
1074 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1077 cur.pos() = cur.lastpos();
1079 //mathcursor->normalize();
1080 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1084 case LFUN_TABULAR_FEATURE: {
1086 //lyxerr << "handling tabular-feature " << cmd.argument << endl;
1087 istringstream is(cmd.argument);
1090 if (s == "valign-top")
1092 else if (s == "valign-middle")
1094 else if (s == "valign-bottom")
1096 else if (s == "align-left")
1097 halign('l', cur.col());
1098 else if (s == "align-right")
1099 halign('r', cur.col());
1100 else if (s == "align-center")
1101 halign('c', cur.col());
1102 else if (s == "append-row")
1103 for (int i = 0, n = extractInt(is); i < n; ++i)
1105 else if (s == "delete-row")
1106 for (int i = 0, n = extractInt(is); i < n; ++i) {
1108 if (cur.idx() > nargs())
1109 cur.idx() -= ncols();
1111 else if (s == "copy-row")
1112 for (int i = 0, n = extractInt(is); i < n; ++i)
1114 else if (s == "swap-row")
1116 else if (s == "add-hline-above")
1117 rowinfo_[cur.row()].lines_++;
1118 else if (s == "add-hline-below")
1119 rowinfo_[cur.row()+1].lines_++;
1120 else if (s == "delete-hline-above")
1121 rowinfo_[cur.row()].lines_--;
1122 else if (s == "delete-hline-below")
1123 rowinfo_[cur.row()+1].lines_--;
1124 else if (s == "append-column")
1125 for (int i = 0, n = extractInt(is); i < n; ++i) {
1126 row_type const r = cur.row();
1127 col_type const c = cur.col();
1129 cur.idx() = index(r, c);
1131 else if (s == "delete-column")
1132 for (int i = 0, n = extractInt(is); i < n; ++i) {
1133 row_type const r = cur.row();
1134 col_type const c = cur.col();
1135 delCol(col(cur.idx()));
1136 cur.idx() = index(r, c);
1137 if (cur.idx() > nargs())
1138 cur.idx() -= ncols();
1140 else if (s == "copy-column")
1142 else if (s == "swap-column")
1144 else if (s == "add-vline-left")
1145 colinfo_[cur.col()].lines_++;
1146 else if (s == "add-vline-right")
1147 colinfo_[cur.col()+1].lines_++;
1148 else if (s == "delete-vline-left")
1149 colinfo_[cur.col()].lines_--;
1150 else if (s == "delete-vline-right")
1151 colinfo_[cur.col()+1].lines_--;
1156 lyxerr << "returning FINISHED_LEFT" << endl;
1162 lyxerr << "MathGridInset: PASTE: " << cmd << std::endl;
1163 istringstream is(cmd.argument);
1166 MathGridInset grid(1, 1);
1167 mathed_parse_normal(grid, lyx::cap::getSelection(cur.buffer(), n));
1168 if (grid.nargs() == 1) {
1169 // single cell/part of cell
1170 cur.cell().insert(cur.pos(), grid.cell(0));
1171 cur.pos() += grid.cell(0).size();
1174 col_type const numcols =
1175 min(grid.ncols(), ncols() - col(cur.idx()));
1176 row_type const numrows =
1177 min(grid.nrows(), nrows() - cur.row());
1178 for (row_type r = 0; r < numrows; ++r) {
1179 for (col_type c = 0; c < numcols; ++c) {
1180 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1181 cell(i).insert(0, grid.cell(grid.index(r, c)));
1183 // append the left over horizontal cells to the last column
1184 idx_type i = index(r + cur.row(), ncols() - 1);
1185 for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1186 cell(i).append(grid.cell(grid.index(r, c)));
1188 // append the left over vertical cells to the last _cell_
1189 idx_type i = nargs() - 1;
1190 for (row_type r = numrows; r < grid.nrows(); ++r)
1191 for (col_type c = 0; c < grid.ncols(); ++c)
1192 cell(i).append(grid.cell(grid.index(r, c)));
1199 case LFUN_WORDLEFTSEL:
1201 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
1202 cur.macroModeClose();
1203 if (cur.pos() != 0) {
1205 } else if (cur.idx() % cur.ncols() != 0) {
1206 cur.idx() -= cur.idx() % cur.ncols();
1208 } else if (cur.idx() != 0) {
1212 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1217 case LFUN_WORDRIGHTSEL:
1218 case LFUN_WORDRIGHT:
1221 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
1222 cur.macroModeClose();
1224 if (cur.pos() != cur.lastpos()) {
1225 cur.pos() = cur.lastpos();
1226 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1227 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1228 cur.pos() = cur.lastpos();
1229 } else if (cur.idx() != cur.lastidx()) {
1230 cur.idx() = cur.lastidx();
1231 cur.pos() = cur.lastpos();
1233 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1239 MathNestInset::doDispatch(cur, cmd);
1244 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1245 FuncStatus & flag) const
1247 switch (cmd.action) {
1248 case LFUN_TABULAR_FEATURE: {
1249 string const s = cmd.argument;
1250 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1251 flag.enabled(false);
1252 flag.message(N_("Only one row"));
1256 (s == "delete-column" || s == "swap-column")) {
1257 flag.enabled(false);
1258 flag.message(N_("Only one column"));
1261 if ((rowinfo_[cur.row()].lines_ == 0 &&
1262 s == "delete-hline-above") ||
1263 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1264 s == "delete-hline-below")) {
1265 flag.enabled(false);
1266 flag.message(N_("No hline to delete"));
1270 if ((colinfo_[cur.col()].lines_ == 0 &&
1271 s == "delete-vline-left") ||
1272 (colinfo_[cur.col() + 1].lines_ == 0 &&
1273 s == "delete-vline-right")) {
1274 flag.enabled(false);
1275 flag.message(N_("No vline to delete"));
1278 if (s == "valign-top" || s == "valign-middle" ||
1279 s == "valign-bottom" || s == "align-left" ||
1280 s == "align-right" || s == "align-center" ||
1281 s == "append-row" || s == "delete-row" ||
1282 s == "copy-row" || s == "swap-row" ||
1283 s == "add-hline-above" || s == "add-hline-below" ||
1284 s == "delete-hline-above" || s == "delete-hline-below" ||
1285 s == "append-column" || s == "delete-column" ||
1286 s == "copy-column" || s == "swap-column" ||
1287 s == "add-vline-left" || s == "add-vline-right" ||
1288 s == "delete-vline-left" || s == "delete-vline-right")
1291 flag.enabled(false);
1292 flag.message(bformat(
1293 N_("Unknown tabular feature '%1$s'"), s));
1296 flag.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1297 || s == "align-right" && halign(cur.col()) == 'r'
1298 || s == "align-center" && halign(cur.col()) == 'c'
1299 || s == "valign-top" && valign() == 't'
1300 || s == "valign-bottom" && valign() == 'b'
1301 || s == "valign-middle" && valign() == 'm');
1304 // FIXME: What did this code do?
1305 // Please check wether it is still needed!
1306 // should be more precise
1307 if (v_align_ == '\0') {
1311 if (cmd.argument.empty()) {
1315 if (!lyx::support::contains("tcb", cmd.argument[0])) {
1319 flag.setOnOff(cmd.argument[0] == v_align_);
1324 case LFUN_CELL_SPLIT:
1328 return MathNestInset::getStatus(cur, cmd, flag);