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 for (idx_type idx = 0; idx < nargs(); ++idx)
483 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
485 for (row_type row = 0; row <= nrows(); ++row)
486 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
487 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
488 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
489 pi.pain.line(x + 1, yy,
490 x + dim_.width() - 1, yy,
494 for (col_type col = 0; col <= ncols(); ++col)
495 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
496 int xx = x + colinfo_[col].offset_
497 - i * vlinesep() - vlinesep()/2 - colsep()/2;
498 pi.pain.line(xx, y - dim_.ascent() + 1,
499 xx, y + dim_.descent() - 1,
502 drawMarkers2(pi, x, y);
506 void MathGridInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
508 // let the cells adjust themselves
509 //MathNestInset::metrics(mi);
510 for (idx_type i = 0; i < nargs(); ++i)
511 cell(i).metricsT(mi, dim);
513 // compute absolute sizes of vertical structure
514 for (row_type row = 0; row < nrows(); ++row) {
517 for (col_type col = 0; col < ncols(); ++col) {
518 MathArray const & c = cell(index(row, col));
519 asc = max(asc, c.ascent());
520 desc = max(desc, c.descent());
522 rowinfo_[row].ascent_ = asc;
523 rowinfo_[row].descent_ = desc;
525 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
526 rowinfo_[nrows()].ascent_ = 0;
527 rowinfo_[nrows()].descent_ = 0;
529 // compute vertical offsets
530 rowinfo_[0].offset_ = 0;
531 for (row_type row = 1; row <= nrows(); ++row) {
532 rowinfo_[row].offset_ =
533 rowinfo_[row - 1].offset_ +
534 rowinfo_[row - 1].descent_ +
535 //rowinfo_[row - 1].skipPixels() +
537 //rowinfo_[row].lines_ * hlinesep() +
538 rowinfo_[row].ascent_;
541 // adjust vertical offset
548 h = rowinfo_[nrows() - 1].offset_;
551 h = rowinfo_[nrows() - 1].offset_ / 2;
553 for (row_type row = 0; row <= nrows(); ++row)
554 rowinfo_[row].offset_ -= h;
557 // compute absolute sizes of horizontal structure
558 for (col_type col = 0; col < ncols(); ++col) {
560 for (row_type row = 0; row < nrows(); ++row)
561 wid = max(wid, cell(index(row, col)).width());
562 colinfo_[col].width_ = wid;
564 colinfo_[ncols()].width_ = 0;
566 // compute horizontal offsets
567 colinfo_[0].offset_ = border();
568 for (col_type col = 1; col <= ncols(); ++col) {
569 colinfo_[col].offset_ =
570 colinfo_[col - 1].offset_ +
571 colinfo_[col - 1].width_ +
572 colinfo_[col - 1].skip_ +
574 //colinfo_[col].lines_ * vlinesep();
578 dim.wid = colinfo_[ncols() - 1].offset_
579 + colinfo_[ncols() - 1].width_
580 //+ vlinesep() * colinfo_[ncols()].lines_
583 dim.asc = -rowinfo_[0].offset_
584 + rowinfo_[0].ascent_
585 //+ hlinesep() * rowinfo_[0].lines_
588 dim.des = rowinfo_[nrows() - 1].offset_
589 + rowinfo_[nrows() - 1].descent_
590 //+ hlinesep() * rowinfo_[nrows()].lines_
595 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
597 for (idx_type idx = 0; idx < nargs(); ++idx)
598 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
602 string MathGridInset::eolString(row_type row, bool fragile) const
606 if (!rowinfo_[row].crskip_.zero())
607 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
609 // make sure an upcoming '[' does not break anything
610 if (row + 1 < nrows()) {
611 MathArray const & c = cell(index(row + 1, 0));
612 if (c.size() && c.front()->getChar() == '[')
617 // only add \\ if necessary
618 if (eol.empty() && row + 1 == nrows())
621 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
625 string MathGridInset::eocString(col_type col, col_type lastcol) const
627 if (col + 1 == lastcol)
633 void MathGridInset::addRow(row_type row)
635 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
637 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
639 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
643 void MathGridInset::appendRow()
645 rowinfo_.push_back(RowInfo());
646 //cells_.insert(cells_.end(), ncols(), MathArray());
647 for (col_type col = 0; col < ncols(); ++col) {
648 cells_.push_back(cells_type::value_type());
649 cellinfo_.push_back(CellInfo());
654 void MathGridInset::delRow(row_type row)
659 cells_type::iterator it = cells_.begin() + row * ncols();
660 cells_.erase(it, it + ncols());
662 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
663 cellinfo_.erase(jt, jt + ncols());
665 rowinfo_.erase(rowinfo_.begin() + row);
669 void MathGridInset::copyRow(row_type row)
672 for (col_type col = 0; col < ncols(); ++col)
673 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
677 void MathGridInset::swapRow(row_type row)
681 if (row + 1 == nrows())
683 for (col_type col = 0; col < ncols(); ++col)
684 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
688 void MathGridInset::addCol(col_type newcol)
690 const col_type nc = ncols();
691 const row_type nr = nrows();
692 cells_type new_cells((nc + 1) * nr);
693 vector<CellInfo> new_cellinfo((nc + 1) * nr);
695 for (row_type row = 0; row < nr; ++row)
696 for (col_type col = 0; col < nc; ++col) {
697 new_cells[row * (nc + 1) + col + (col > newcol)]
698 = cells_[row * nc + col];
699 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
700 = cellinfo_[row * nc + col];
702 swap(cells_, new_cells);
703 swap(cellinfo_, new_cellinfo);
706 inf.skip_ = defaultColSpace(newcol);
707 inf.align_ = defaultColAlign(newcol);
708 colinfo_.insert(colinfo_.begin() + newcol, inf);
712 void MathGridInset::delCol(col_type col)
718 vector<CellInfo> tmpcellinfo;
719 for (col_type i = 0; i < nargs(); ++i)
720 if (i % ncols() != col) {
721 tmpcells.push_back(cells_[i]);
722 tmpcellinfo.push_back(cellinfo_[i]);
724 swap(cells_, tmpcells);
725 swap(cellinfo_, tmpcellinfo);
727 colinfo_.erase(colinfo_.begin() + col);
731 void MathGridInset::copyCol(col_type col)
734 for (row_type row = 0; row < nrows(); ++row)
735 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
739 void MathGridInset::swapCol(col_type col)
743 if (col + 1 == ncols())
745 for (row_type row = 0; row < nrows(); ++row)
746 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
750 int MathGridInset::cellXOffset(idx_type idx) const
752 col_type c = col(idx);
753 int x = colinfo_[c].offset_;
754 char align = colinfo_[c].align_;
755 if (align == 'r' || align == 'R')
756 x += colinfo_[c].width_ - cell(idx).width();
757 if (align == 'c' || align == 'C')
758 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
763 int MathGridInset::cellYOffset(idx_type idx) const
765 return rowinfo_[row(idx)].offset_;
769 bool MathGridInset::idxUpDown(LCursor & cur, bool up) const
772 if (cur.idx() < ncols())
774 cur.idx() -= ncols();
776 if (cur.idx() >= ncols() * (nrows() - 1))
778 cur.idx() += ncols();
780 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo());
785 bool MathGridInset::idxLeft(LCursor & cur) const
787 // leave matrix if on the left hand edge
791 cur.pos() = cur.lastpos();
796 bool MathGridInset::idxRight(LCursor & cur) const
798 // leave matrix if on the right hand edge
799 if (cur.col() + 1 == ncols())
807 bool MathGridInset::idxFirst(LCursor & cur) const
814 cur.idx() = (nrows() - 1) * ncols();
817 cur.idx() = ((nrows() - 1) / 2) * ncols();
824 bool MathGridInset::idxLast(LCursor & cur) const
828 cur.idx() = ncols() - 1;
831 cur.idx() = nargs() - 1;
834 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
836 cur.pos() = cur.lastpos();
841 bool MathGridInset::idxDelete(idx_type & idx)
843 // nothing to do if we have just one row
847 // nothing to do if we are in the middle of the last row of the inset
848 if (idx + ncols() > nargs())
851 // try to delete entire sequence of ncols() empty cells if possible
852 for (idx_type i = idx; i < idx + ncols(); ++i)
856 // move cells if necessary
857 for (idx_type i = index(row(idx), 0); i < idx; ++i)
858 swap(cell(i), cell(i + ncols()));
865 // undo effect of Ctrl-Tab (i.e. pull next cell)
866 //if (idx + 1 != nargs())
867 // cell(idx).swap(cell(idx + 1));
869 // we handled the event..
874 // reimplement old behaviour when pressing Delete in the last position
876 void MathGridInset::idxGlue(idx_type idx)
878 col_type c = col(idx);
879 if (c + 1 == ncols()) {
880 if (row(idx) + 1 != nrows()) {
881 for (col_type cc = 0; cc < ncols(); ++cc)
882 cell(idx).append(cell(idx + cc + 1));
883 delRow(row(idx) + 1);
886 cell(idx).append(cell(idx + 1));
887 for (col_type cc = c + 2; cc < ncols(); ++cc)
888 cell(idx - c + cc - 1) = cell(idx - c + cc);
889 cell(idx - c + ncols() - 1).clear();
894 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
896 return rowinfo_[row];
900 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
902 return rowinfo_[row];
906 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
908 row_type const ri = row(idx);
909 row_type const r1 = min(row(from), row(to));
910 row_type const r2 = max(row(from), row(to));
911 col_type const ci = col(idx);
912 col_type const c1 = min(col(from), col(to));
913 col_type const c2 = max(col(from), col(to));
914 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
919 void MathGridInset::normalize(NormalStream & os) const
922 for (row_type row = 0; row < nrows(); ++row) {
924 for (col_type col = 0; col < ncols(); ++col)
925 os << "[cell " << cell(index(row, col)) << ']';
932 void MathGridInset::mathmlize(MathMLStream & os) const
934 os << MTag("mtable");
935 for (row_type row = 0; row < nrows(); ++row) {
937 for (col_type col = 0; col < ncols(); ++col)
938 os << cell(index(row, col));
941 os << ETag("mtable");
945 void MathGridInset::write(WriteStream & os) const
947 for (row_type row = 0; row < nrows(); ++row) {
948 os << verboseHLine(rowinfo_[row].lines_);
949 // don't write & and empty cells at end of line
950 col_type lastcol = 0;
951 bool emptyline = true;
952 for (col_type col = 0; col < ncols(); ++col)
953 if (!cell(index(row, col)).empty()) {
957 for (col_type col = 0; col < lastcol; ++col)
958 os << cell(index(row, col)) << eocString(col, lastcol);
959 os << eolString(row, os.fragile());
960 // append newline only if line wasn't completely empty
961 // and this was not the last line in the grid
962 if (!emptyline && row + 1 < nrows())
965 string const s = verboseHLine(rowinfo_[nrows()].lines_);
966 if (!s.empty() && s != " ") {
974 int MathGridInset::colsep() const
980 int MathGridInset::rowsep() const
986 int MathGridInset::hlinesep() const
992 int MathGridInset::vlinesep() const
998 int MathGridInset::border() const
1004 void MathGridInset::splitCell(LCursor & cur)
1006 if (cur.idx() == cur.lastidx())
1008 MathArray ar = cur.cell();
1009 ar.erase(0, cur.pos());
1010 cur.cell().erase(cur.pos(), cur.lastpos());
1013 cur.cell().insert(0, ar);
1017 void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
1019 //lyxerr << "*** MathGridInset: request: " << cmd << endl;
1020 switch (cmd.action) {
1022 case LFUN_MOUSE_RELEASE:
1023 //if (cmd.button() == mouse_button::button3) {
1024 // GridInsetMailer(*this).showDialog();
1025 // return DispatchResult(true, true);
1027 MathNestInset::doDispatch(cur, cmd);
1030 case LFUN_INSET_DIALOG_UPDATE:
1031 GridInsetMailer(*this).updateDialog(&cur.bv());
1034 // insert file functions
1035 case LFUN_DELETE_LINE_FORWARD:
1037 //autocorrect_ = false;
1045 if (cur.idx() > cur.lastidx())
1046 cur.idx() = cur.lastidx();
1047 if (cur.pos() > cur.lastpos())
1048 cur.pos() = cur.lastpos();
1051 case LFUN_CELL_SPLIT:
1056 case LFUN_BREAKLINE: {
1058 row_type const r = cur.row();
1062 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1063 swap(cell(index(r, c)), cell(index(r + 1, c)));
1067 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1070 cur.pos() = cur.lastpos();
1072 //mathcursor->normalize();
1073 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1077 case LFUN_TABULAR_FEATURE: {
1079 //lyxerr << "handling tabular-feature " << cmd.argument << endl;
1080 istringstream is(cmd.argument);
1083 if (s == "valign-top")
1085 else if (s == "valign-middle")
1087 else if (s == "valign-bottom")
1089 else if (s == "align-left")
1090 halign('l', cur.col());
1091 else if (s == "align-right")
1092 halign('r', cur.col());
1093 else if (s == "align-center")
1094 halign('c', cur.col());
1095 else if (s == "append-row")
1096 for (int i = 0, n = extractInt(is); i < n; ++i)
1098 else if (s == "delete-row")
1099 for (int i = 0, n = extractInt(is); i < n; ++i) {
1101 if (cur.idx() > nargs())
1102 cur.idx() -= ncols();
1104 else if (s == "copy-row")
1105 for (int i = 0, n = extractInt(is); i < n; ++i)
1107 else if (s == "swap-row")
1109 else if (s == "add-hline-above")
1110 rowinfo_[cur.row()].lines_++;
1111 else if (s == "add-hline-below")
1112 rowinfo_[cur.row()+1].lines_++;
1113 else if (s == "delete-hline-above")
1114 rowinfo_[cur.row()].lines_--;
1115 else if (s == "delete-hline-below")
1116 rowinfo_[cur.row()+1].lines_--;
1117 else if (s == "append-column")
1118 for (int i = 0, n = extractInt(is); i < n; ++i) {
1119 row_type const r = cur.row();
1120 col_type const c = cur.col();
1122 cur.idx() = index(r, c);
1124 else if (s == "delete-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();
1128 delCol(col(cur.idx()));
1129 cur.idx() = index(r, c);
1130 if (cur.idx() > nargs())
1131 cur.idx() -= ncols();
1133 else if (s == "copy-column")
1135 else if (s == "swap-column")
1137 else if (s == "add-vline-left")
1138 colinfo_[cur.col()].lines_++;
1139 else if (s == "add-vline-right")
1140 colinfo_[cur.col()+1].lines_++;
1141 else if (s == "delete-vline-left")
1142 colinfo_[cur.col()].lines_--;
1143 else if (s == "delete-vline-right")
1144 colinfo_[cur.col()+1].lines_--;
1149 lyxerr << "returning FINISHED_LEFT" << endl;
1155 lyxerr << "MathGridInset: PASTE: " << cmd << std::endl;
1156 istringstream is(cmd.argument);
1159 MathGridInset grid(1, 1);
1160 mathed_parse_normal(grid, lyx::cap::getSelection(cur.buffer(), n));
1161 if (grid.nargs() == 1) {
1162 // single cell/part of cell
1163 cur.cell().insert(cur.pos(), grid.cell(0));
1164 cur.pos() += grid.cell(0).size();
1167 col_type const numcols =
1168 min(grid.ncols(), ncols() - col(cur.idx()));
1169 row_type const numrows =
1170 min(grid.nrows(), nrows() - cur.row());
1171 for (row_type r = 0; r < numrows; ++r) {
1172 for (col_type c = 0; c < numcols; ++c) {
1173 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1174 cell(i).insert(0, grid.cell(grid.index(r, c)));
1176 // append the left over horizontal cells to the last column
1177 idx_type i = index(r + cur.row(), ncols() - 1);
1178 for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1179 cell(i).append(grid.cell(grid.index(r, c)));
1181 // append the left over vertical cells to the last _cell_
1182 idx_type i = nargs() - 1;
1183 for (row_type r = numrows; r < grid.nrows(); ++r)
1184 for (col_type c = 0; c < grid.ncols(); ++c)
1185 cell(i).append(grid.cell(grid.index(r, c)));
1192 case LFUN_WORDLEFTSEL:
1194 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
1195 cur.macroModeClose();
1196 if (cur.pos() != 0) {
1198 } else if (cur.idx() % cur.ncols() != 0) {
1199 cur.idx() -= cur.idx() % cur.ncols();
1201 } else if (cur.idx() != 0) {
1205 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1209 case LFUN_WORDRIGHTSEL:
1210 case LFUN_WORDRIGHT:
1213 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
1214 cur.macroModeClose();
1216 if (cur.pos() != cur.lastpos()) {
1217 cur.pos() = cur.lastpos();
1218 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1219 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1220 cur.pos() = cur.lastpos();
1221 } else if (cur.idx() != cur.lastidx()) {
1222 cur.idx() = cur.lastidx();
1223 cur.pos() = cur.lastpos();
1225 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1230 MathNestInset::doDispatch(cur, cmd);
1235 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1236 FuncStatus & flag) const
1238 switch (cmd.action) {
1239 case LFUN_TABULAR_FEATURE: {
1240 istringstream is(cmd.argument);
1243 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1244 flag.enabled(false);
1245 flag.message(N_("Only one row"));
1249 (s == "delete-column" || s == "swap-column")) {
1250 flag.enabled(false);
1251 flag.message(N_("Only one column"));
1254 if ((rowinfo_[cur.row()].lines_ == 0 &&
1255 s == "delete-hline-above") ||
1256 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1257 s == "delete-hline-below")) {
1258 flag.enabled(false);
1259 flag.message(N_("No hline to delete"));
1263 if ((colinfo_[cur.col()].lines_ == 0 &&
1264 s == "delete-vline-left") ||
1265 (colinfo_[cur.col() + 1].lines_ == 0 &&
1266 s == "delete-vline-right")) {
1267 flag.enabled(false);
1268 flag.message(N_("No vline to delete"));
1271 if (s == "valign-top" || s == "valign-middle" ||
1272 s == "valign-bottom" || s == "align-left" ||
1273 s == "align-right" || s == "align-center" ||
1274 s == "append-row" || s == "delete-row" ||
1275 s == "copy-row" || s == "swap-row" ||
1276 s == "add-hline-above" || s == "add-hline-below" ||
1277 s == "delete-hline-above" || s == "delete-hline-below" ||
1278 s == "append-column" || s == "delete-column" ||
1279 s == "copy-column" || s == "swap-column" ||
1280 s == "add-vline-left" || s == "add-vline-right" ||
1281 s == "delete-vline-left" || s == "delete-vline-right")
1284 flag.enabled(false);
1285 flag.message(bformat(
1286 N_("Unknown tabular feature '%1$s'"), s));
1289 // FIXME: What did this code do?
1290 // Please check wether it is still needed!
1291 // should be more precise
1292 if (v_align_ == '\0') {
1296 if (cmd.argument.empty()) {
1300 if (!lyx::support::contains("tcb", cmd.argument[0])) {
1304 flag.setOnOff(cmd.argument[0] == v_align_);
1310 return MathNestInset::getStatus(cur, cmd, flag);