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"
27 #include "frontends/Painter.h"
29 #include "insets/mailinset.h"
41 using std::istringstream;
42 using std::ostringstream;
46 class GridInsetMailer : public MailInset {
48 GridInsetMailer(MathGridInset & inset) : inset_(inset) {}
50 virtual string const & name() const
52 static string const theName = "tabular";
56 virtual string const inset2string(Buffer const &) const
59 //data << name() << " active_cell " << inset.getActCell() << '\n';
60 data << name() << " active_cell " << 0 << '\n';
67 InsetBase & inset() const { return inset_; }
68 MathGridInset & inset_;
72 void mathed_parse_normal(MathGridInset &, string const & argument);
76 string verboseHLine(int n)
79 for (int i = 0; i < n; ++i)
87 int extractInt(istream & is)
91 return (num == 0) ? 1 : num;
97 //////////////////////////////////////////////////////////////
100 MathGridInset::CellInfo::CellInfo()
107 //////////////////////////////////////////////////////////////
110 MathGridInset::RowInfo::RowInfo()
111 : lines_(0), skip_(0)
116 int MathGridInset::RowInfo::skipPixels() const
118 return crskip_.inBP();
123 //////////////////////////////////////////////////////////////
126 MathGridInset::ColInfo::ColInfo()
127 : align_('c'), leftline_(false), rightline_(false), lines_(0)
131 //////////////////////////////////////////////////////////////
134 MathGridInset::MathGridInset(char v, string const & h)
135 : MathNestInset(guessColumns(h)),
137 colinfo_(guessColumns(h) + 1),
138 cellinfo_(1 * guessColumns(h))
143 //lyxerr << "created grid with " << ncols() << " columns" << endl;
147 MathGridInset::MathGridInset()
158 MathGridInset::MathGridInset(col_type m, row_type n)
159 : MathNestInset(m * n),
169 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
170 : MathNestInset(m * n),
182 MathGridInset::~MathGridInset()
184 GridInsetMailer mailer(*this);
189 auto_ptr<InsetBase> MathGridInset::doClone() const
191 return auto_ptr<InsetBase>(new MathGridInset(*this));
195 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
197 return col + ncols() * row;
201 void MathGridInset::setDefaults()
204 lyxerr << "positive number of columns expected" << endl;
206 // lyxerr << "positive number of rows expected" << endl;
207 for (col_type col = 0; col < ncols(); ++col) {
208 colinfo_[col].align_ = defaultColAlign(col);
209 colinfo_[col].skip_ = defaultColSpace(col);
214 void MathGridInset::halign(string const & hh)
217 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
222 colinfo_[col].lines_++;
223 } else if (c == 'c' || c == 'l' || c == 'r') {
224 colinfo_[col].align_ = c;
226 colinfo_[col].lines_ = 0;
228 lyxerr << "unknown column separator: '" << c << "'" << endl;
233 col_type n = hh.size();
236 for (col_type col = 0; col < n; ++col)
237 colinfo_[col].align_ = hh[col];
242 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
245 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
246 if (*it == 'c' || *it == 'l' || *it == 'r')
248 // let's have at least one column, even if we did not recognize its
256 void MathGridInset::halign(char h, col_type col)
258 colinfo_[col].align_ = h;
262 char MathGridInset::halign(col_type col) const
264 return colinfo_[col].align_;
268 string MathGridInset::halign() const
271 for (col_type col = 0; col < ncols(); ++col) {
272 res += string(colinfo_[col].lines_, '|');
273 res += colinfo_[col].align_;
275 return res + string(colinfo_[ncols()].lines_, '|');
279 void MathGridInset::valign(char c)
285 char MathGridInset::valign() const
291 MathGridInset::col_type MathGridInset::ncols() const
293 return colinfo_.size() - 1;
297 MathGridInset::row_type MathGridInset::nrows() const
299 return rowinfo_.size() - 1;
303 MathGridInset::col_type MathGridInset::col(idx_type idx) const
305 return idx % ncols();
309 MathGridInset::row_type MathGridInset::row(idx_type idx) const
311 return idx / ncols();
315 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
317 rowinfo_[row].crskip_ = crskip;
321 LyXLength MathGridInset::vcrskip(row_type row) const
323 return rowinfo_[row].crskip_;
327 void MathGridInset::metrics(MetricsInfo & mi) const
329 // let the cells adjust themselves
330 MathNestInset::metrics(mi);
332 // compute absolute sizes of vertical structure
333 for (row_type row = 0; row < nrows(); ++row) {
336 for (col_type col = 0; col < ncols(); ++col) {
337 MathArray const & c = cell(index(row, col));
338 asc = max(asc, c.ascent());
339 desc = max(desc, c.descent());
341 rowinfo_[row].ascent_ = asc;
342 rowinfo_[row].descent_ = desc;
344 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
345 rowinfo_[nrows()].ascent_ = 0;
346 rowinfo_[nrows()].descent_ = 0;
348 // compute vertical offsets
349 rowinfo_[0].offset_ = 0;
350 for (row_type row = 1; row <= nrows(); ++row) {
351 rowinfo_[row].offset_ =
352 rowinfo_[row - 1].offset_ +
353 rowinfo_[row - 1].descent_ +
354 rowinfo_[row - 1].skipPixels() +
356 rowinfo_[row].lines_ * hlinesep() +
357 rowinfo_[row].ascent_;
360 // adjust vertical offset
367 h = rowinfo_[nrows() - 1].offset_;
370 h = rowinfo_[nrows() - 1].offset_ / 2;
372 for (row_type row = 0; row <= nrows(); ++row)
373 rowinfo_[row].offset_ -= h;
376 // compute absolute sizes of horizontal structure
377 for (col_type col = 0; col < ncols(); ++col) {
379 for (row_type row = 0; row < nrows(); ++row)
380 wid = max(wid, cell(index(row, col)).width());
381 colinfo_[col].width_ = wid;
383 colinfo_[ncols()].width_ = 0;
385 // compute horizontal offsets
386 colinfo_[0].offset_ = border();
387 for (col_type col = 1; col <= ncols(); ++col) {
388 colinfo_[col].offset_ =
389 colinfo_[col - 1].offset_ +
390 colinfo_[col - 1].width_ +
391 colinfo_[col - 1].skip_ +
393 colinfo_[col].lines_ * vlinesep();
397 dim_.wid = colinfo_[ncols() - 1].offset_
398 + colinfo_[ncols() - 1].width_
399 + vlinesep() * colinfo_[ncols()].lines_
402 dim_.asc = - rowinfo_[0].offset_
403 + rowinfo_[0].ascent_
404 + hlinesep() * rowinfo_[0].lines_
407 dim_.des = rowinfo_[nrows() - 1].offset_
408 + rowinfo_[nrows() - 1].descent_
409 + hlinesep() * rowinfo_[nrows()].lines_
414 // Increase ws_[i] for 'R' columns (except the first one)
415 for (int i = 1; i < nc_; ++i)
416 if (align_[i] == 'R')
417 ws_[i] += 10 * df_width;
418 // Increase ws_[i] for 'C' column
419 if (align_[0] == 'C')
420 if (ws_[0] < 7 * workwidth / 8)
421 ws_[0] = 7 * workwidth / 8;
425 for (cxrow = row_.begin(); cxrow; ++cxrow) {
428 for (int i = 0; i < nc_; ++i) {
430 if (cxrow->getTab(i) <= 0) {
431 cxrow->setTab(i, df_width);
439 lf = (ws_[i] - cxrow->getTab(i))/2;
443 lf = ws_[i] - cxrow->getTab(i);
446 if (cxrow == row_.begin())
448 else if (cxrow.is_last())
449 lf = ws_[i] - cxrow->getTab(i);
451 lf = (ws_[i] - cxrow->getTab(i))/2;
454 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
455 cxrow->setTab(i, lf + rg);
456 rg = ws_[i] - ww + colsep();
457 if (cxrow == row_.begin())
458 width += ws_[i] + colsep();
460 cxrow->setBaseline(cxrow->getBaseline() - ascent);
463 metricsMarkers2(dim_);
467 void MathGridInset::metrics(MetricsInfo & mi, Dimension & dim) const
474 void MathGridInset::draw(PainterInfo & pi, int x, int y) const
476 for (idx_type idx = 0; idx < nargs(); ++idx)
477 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
479 for (row_type row = 0; row <= nrows(); ++row)
480 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
481 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
482 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
483 pi.pain.line(x + 1, yy,
484 x + dim_.width() - 1, yy,
488 for (col_type col = 0; col <= ncols(); ++col)
489 for (int i = 0; i < colinfo_[col].lines_; ++i) {
490 int xx = x + colinfo_[col].offset_
491 - i * vlinesep() - vlinesep()/2 - colsep()/2;
492 pi.pain.line(xx, y - dim_.ascent() + 1,
493 xx, y + dim_.descent() - 1,
496 drawMarkers2(pi, x, y);
500 void MathGridInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
502 // let the cells adjust themselves
503 //MathNestInset::metrics(mi);
504 for (idx_type i = 0; i < nargs(); ++i)
505 cell(i).metricsT(mi, dim);
507 // compute absolute sizes of vertical structure
508 for (row_type row = 0; row < nrows(); ++row) {
511 for (col_type col = 0; col < ncols(); ++col) {
512 MathArray const & c = cell(index(row, col));
513 asc = max(asc, c.ascent());
514 desc = max(desc, c.descent());
516 rowinfo_[row].ascent_ = asc;
517 rowinfo_[row].descent_ = desc;
519 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
520 rowinfo_[nrows()].ascent_ = 0;
521 rowinfo_[nrows()].descent_ = 0;
523 // compute vertical offsets
524 rowinfo_[0].offset_ = 0;
525 for (row_type row = 1; row <= nrows(); ++row) {
526 rowinfo_[row].offset_ =
527 rowinfo_[row - 1].offset_ +
528 rowinfo_[row - 1].descent_ +
529 //rowinfo_[row - 1].skipPixels() +
531 //rowinfo_[row].lines_ * hlinesep() +
532 rowinfo_[row].ascent_;
535 // adjust vertical offset
542 h = rowinfo_[nrows() - 1].offset_;
545 h = rowinfo_[nrows() - 1].offset_ / 2;
547 for (row_type row = 0; row <= nrows(); ++row)
548 rowinfo_[row].offset_ -= h;
551 // compute absolute sizes of horizontal structure
552 for (col_type col = 0; col < ncols(); ++col) {
554 for (row_type row = 0; row < nrows(); ++row)
555 wid = max(wid, cell(index(row, col)).width());
556 colinfo_[col].width_ = wid;
558 colinfo_[ncols()].width_ = 0;
560 // compute horizontal offsets
561 colinfo_[0].offset_ = border();
562 for (col_type col = 1; col <= ncols(); ++col) {
563 colinfo_[col].offset_ =
564 colinfo_[col - 1].offset_ +
565 colinfo_[col - 1].width_ +
566 colinfo_[col - 1].skip_ +
568 //colinfo_[col].lines_ * vlinesep();
572 dim.wid = colinfo_[ncols() - 1].offset_
573 + colinfo_[ncols() - 1].width_
574 //+ vlinesep() * colinfo_[ncols()].lines_
577 dim.asc = -rowinfo_[0].offset_
578 + rowinfo_[0].ascent_
579 //+ hlinesep() * rowinfo_[0].lines_
582 dim.des = rowinfo_[nrows() - 1].offset_
583 + rowinfo_[nrows() - 1].descent_
584 //+ hlinesep() * rowinfo_[nrows()].lines_
589 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
591 for (idx_type idx = 0; idx < nargs(); ++idx)
592 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
596 string MathGridInset::eolString(row_type row, bool fragile) const
600 if (!rowinfo_[row].crskip_.zero())
601 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
603 // make sure an upcoming '[' does not break anything
604 if (row + 1 < nrows()) {
605 MathArray const & c = cell(index(row + 1, 0));
606 if (c.size() && c.front()->getChar() == '[')
611 // only add \\ if necessary
612 if (eol.empty() && row + 1 == nrows())
615 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
619 string MathGridInset::eocString(col_type col, col_type lastcol) const
621 if (col + 1 == lastcol)
627 void MathGridInset::addRow(row_type row)
629 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
631 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
633 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
637 void MathGridInset::appendRow()
639 rowinfo_.push_back(RowInfo());
640 //cells_.insert(cells_.end(), ncols(), MathArray());
641 for (col_type col = 0; col < ncols(); ++col) {
642 cells_.push_back(cells_type::value_type());
643 cellinfo_.push_back(CellInfo());
648 void MathGridInset::delRow(row_type row)
653 cells_type::iterator it = cells_.begin() + row * ncols();
654 cells_.erase(it, it + ncols());
656 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
657 cellinfo_.erase(jt, jt + ncols());
659 rowinfo_.erase(rowinfo_.begin() + row);
663 void MathGridInset::copyRow(row_type row)
666 for (col_type col = 0; col < ncols(); ++col)
667 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
671 void MathGridInset::swapRow(row_type row)
675 if (row + 1 == nrows())
677 for (col_type col = 0; col < ncols(); ++col)
678 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
682 void MathGridInset::addCol(col_type newcol)
684 const col_type nc = ncols();
685 const row_type nr = nrows();
686 cells_type new_cells((nc + 1) * nr);
687 vector<CellInfo> new_cellinfo((nc + 1) * nr);
689 for (row_type row = 0; row < nr; ++row)
690 for (col_type col = 0; col < nc; ++col) {
691 new_cells[row * (nc + 1) + col + (col > newcol)]
692 = cells_[row * nc + col];
693 new_cellinfo[row * (nc + 1) + col + (col > newcol)]
694 = cellinfo_[row * nc + col];
696 swap(cells_, new_cells);
697 swap(cellinfo_, new_cellinfo);
700 inf.skip_ = defaultColSpace(newcol);
701 inf.align_ = defaultColAlign(newcol);
702 colinfo_.insert(colinfo_.begin() + newcol, inf);
706 void MathGridInset::delCol(col_type col)
712 vector<CellInfo> tmpcellinfo;
713 for (col_type i = 0; i < nargs(); ++i)
714 if (i % ncols() != col) {
715 tmpcells.push_back(cells_[i]);
716 tmpcellinfo.push_back(cellinfo_[i]);
718 swap(cells_, tmpcells);
719 swap(cellinfo_, tmpcellinfo);
721 colinfo_.erase(colinfo_.begin() + col);
725 void MathGridInset::copyCol(col_type col)
728 for (row_type row = 0; row < nrows(); ++row)
729 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
733 void MathGridInset::swapCol(col_type col)
737 if (col + 1 == ncols())
739 for (row_type row = 0; row < nrows(); ++row)
740 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
744 int MathGridInset::cellXOffset(idx_type idx) const
746 col_type c = col(idx);
747 int x = colinfo_[c].offset_;
748 char align = colinfo_[c].align_;
749 if (align == 'r' || align == 'R')
750 x += colinfo_[c].width_ - cell(idx).width();
751 if (align == 'c' || align == 'C')
752 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
757 int MathGridInset::cellYOffset(idx_type idx) const
759 return rowinfo_[row(idx)].offset_;
763 bool MathGridInset::idxUpDown(LCursor & cur, bool up) const
766 if (cur.idx() < ncols())
768 cur.idx() -= ncols();
770 if (cur.idx() >= ncols() * (nrows() - 1))
772 cur.idx() += ncols();
774 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo());
779 bool MathGridInset::idxLeft(LCursor & cur) const
781 // leave matrix if on the left hand edge
785 cur.pos() = cur.lastpos();
790 bool MathGridInset::idxRight(LCursor & cur) const
792 // leave matrix if on the right hand edge
793 if (cur.col() + 1 == ncols())
801 bool MathGridInset::idxFirst(LCursor & cur) const
808 cur.idx() = (nrows() - 1) * ncols();
811 cur.idx() = ((nrows() - 1) / 2) * ncols();
818 bool MathGridInset::idxLast(LCursor & cur) const
822 cur.idx() = ncols() - 1;
825 cur.idx() = nargs() - 1;
828 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
830 cur.pos() = cur.lastpos();
835 bool MathGridInset::idxDelete(idx_type & idx)
837 // nothing to do if we have just one row
841 // nothing to do if we are in the middle of the last row of the inset
842 if (idx + ncols() > nargs())
845 // try to delete entire sequence of ncols() empty cells if possible
846 for (idx_type i = idx; i < idx + ncols(); ++i)
850 // move cells if necessary
851 for (idx_type i = index(row(idx), 0); i < idx; ++i)
852 swap(cell(i), cell(i + ncols()));
859 // undo effect of Ctrl-Tab (i.e. pull next cell)
860 //if (idx + 1 != nargs())
861 // cell(idx).swap(cell(idx + 1));
863 // we handled the event..
868 // reimplement old behaviour when pressing Delete in the last position
870 void MathGridInset::idxGlue(idx_type idx)
872 col_type c = col(idx);
873 if (c + 1 == ncols()) {
874 if (row(idx) + 1 != nrows()) {
875 for (col_type cc = 0; cc < ncols(); ++cc)
876 cell(idx).append(cell(idx + cc + 1));
877 delRow(row(idx) + 1);
880 cell(idx).append(cell(idx + 1));
881 for (col_type cc = c + 2; cc < ncols(); ++cc)
882 cell(idx - c + cc - 1) = cell(idx - c + cc);
883 cell(idx - c + ncols() - 1).clear();
888 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
890 return rowinfo_[row];
894 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
896 return rowinfo_[row];
900 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
902 row_type const ri = row(idx);
903 row_type const r1 = min(row(from), row(to));
904 row_type const r2 = max(row(from), row(to));
905 col_type const ci = col(idx);
906 col_type const c1 = min(col(from), col(to));
907 col_type const c2 = max(col(from), col(to));
908 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
913 void MathGridInset::normalize(NormalStream & os) const
916 for (row_type row = 0; row < nrows(); ++row) {
918 for (col_type col = 0; col < ncols(); ++col)
919 os << "[cell " << cell(index(row, col)) << ']';
926 void MathGridInset::mathmlize(MathMLStream & os) const
928 os << MTag("mtable");
929 for (row_type row = 0; row < nrows(); ++row) {
931 for (col_type col = 0; col < ncols(); ++col)
932 os << cell(index(row, col));
935 os << ETag("mtable");
939 void MathGridInset::write(WriteStream & os) const
941 for (row_type row = 0; row < nrows(); ++row) {
942 os << verboseHLine(rowinfo_[row].lines_);
943 // don't write & and empty cells at end of line
944 col_type lastcol = 0;
945 bool emptyline = true;
946 for (col_type col = 0; col < ncols(); ++col)
947 if (!cell(index(row, col)).empty()) {
951 for (col_type col = 0; col < lastcol; ++col)
952 os << cell(index(row, col)) << eocString(col, lastcol);
953 os << eolString(row, os.fragile());
954 // append newline only if line wasn't completely empty
955 // and this was not the last line in the grid
956 if (!emptyline && row + 1 < nrows())
959 string const s = verboseHLine(rowinfo_[nrows()].lines_);
960 if (!s.empty() && s != " ") {
968 int MathGridInset::colsep() const
974 int MathGridInset::rowsep() const
980 int MathGridInset::hlinesep() const
986 int MathGridInset::vlinesep() const
992 int MathGridInset::border() const
998 void MathGridInset::splitCell(LCursor & cur)
1000 if (cur.idx() == cur.lastidx())
1002 MathArray ar = cur.cell();
1003 ar.erase(0, cur.pos());
1004 cur.cell().erase(cur.pos(), cur.lastpos());
1007 cur.cell().insert(0, ar);
1011 void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
1013 //lyxerr << "*** MathGridInset: request: " << cmd << endl;
1014 switch (cmd.action) {
1016 case LFUN_MOUSE_RELEASE:
1017 //if (cmd.button() == mouse_button::button3) {
1018 // GridInsetMailer(*this).showDialog();
1019 // return DispatchResult(true, true);
1021 MathNestInset::doDispatch(cur, cmd);
1024 case LFUN_INSET_DIALOG_UPDATE:
1025 GridInsetMailer(*this).updateDialog(&cur.bv());
1028 // insert file functions
1029 case LFUN_DELETE_LINE_FORWARD:
1031 //autocorrect_ = false;
1039 if (cur.idx() > cur.lastidx())
1040 cur.idx() = cur.lastidx();
1041 if (cur.pos() > cur.lastpos())
1042 cur.pos() = cur.lastpos();
1045 case LFUN_CELL_SPLIT:
1050 case LFUN_BREAKLINE: {
1052 row_type const r = cur.row();
1056 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1057 swap(cell(index(r, c)), cell(index(r + 1, c)));
1061 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1064 cur.pos() = cur.lastpos();
1066 //mathcursor->normalize();
1067 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1071 case LFUN_TABULAR_FEATURE: {
1073 //lyxerr << "handling tabular-feature " << cmd.argument << endl;
1074 istringstream is(cmd.argument);
1077 if (s == "valign-top")
1079 else if (s == "valign-middle")
1081 else if (s == "valign-bottom")
1083 else if (s == "align-left")
1084 halign('l', col(cur.idx()));
1085 else if (s == "align-right")
1086 halign('r', col(cur.idx()));
1087 else if (s == "align-center")
1088 halign('c', col(cur.idx()));
1089 else if (s == "append-row")
1090 for (int i = 0, n = extractInt(is); i < n; ++i)
1092 else if (s == "delete-row")
1093 for (int i = 0, n = extractInt(is); i < n; ++i) {
1095 if (cur.idx() > nargs())
1096 cur.idx() -= ncols();
1098 else if (s == "copy-row")
1099 for (int i = 0, n = extractInt(is); i < n; ++i)
1101 else if (s == "swap-row")
1103 else if (s == "append-column")
1104 for (int i = 0, n = extractInt(is); i < n; ++i) {
1105 row_type r = cur.row();
1106 col_type c = col(cur.idx());
1108 cur.idx() = index(r, c);
1110 else if (s == "delete-column")
1111 for (int i = 0, n = extractInt(is); i < n; ++i) {
1112 row_type r = cur.row();
1113 col_type c = col(cur.idx());
1114 delCol(col(cur.idx()));
1115 cur.idx() = index(r, c);
1116 if (cur.idx() > nargs())
1117 cur.idx() -= ncols();
1119 else if (s == "copy-column")
1120 copyCol(col(cur.idx()));
1121 else if (s == "swap-column")
1122 swapCol(col(cur.idx()));
1127 lyxerr << "returning FINISHED_LEFT" << endl;
1133 lyxerr << "MathGridInset: PASTE: " << cmd << std::endl;
1134 istringstream is(cmd.argument);
1137 MathGridInset grid(1, 1);
1138 mathed_parse_normal(grid, lyx::cap::getSelection(cur.buffer(), n));
1139 if (grid.nargs() == 1) {
1140 // single cell/part of cell
1141 cur.cell().insert(cur.pos(), grid.cell(0));
1142 cur.pos() += grid.cell(0).size();
1145 col_type const numcols =
1146 min(grid.ncols(), ncols() - col(cur.idx()));
1147 row_type const numrows =
1148 min(grid.nrows(), nrows() - cur.row());
1149 for (row_type r = 0; r < numrows; ++r) {
1150 for (col_type c = 0; c < numcols; ++c) {
1151 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1152 cell(i).insert(0, grid.cell(grid.index(r, c)));
1154 // append the left over horizontal cells to the last column
1155 idx_type i = index(r + cur.row(), ncols() - 1);
1156 for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1157 cell(i).append(grid.cell(grid.index(r, c)));
1159 // append the left over vertical cells to the last _cell_
1160 idx_type i = nargs() - 1;
1161 for (row_type r = numrows; r < grid.nrows(); ++r)
1162 for (col_type c = 0; c < grid.ncols(); ++c)
1163 cell(i).append(grid.cell(grid.index(r, c)));
1170 case LFUN_WORDLEFTSEL:
1172 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
1173 cur.macroModeClose();
1174 if (cur.pos() != 0) {
1176 } else if (cur.idx() % cur.ncols() != 0) {
1177 cur.idx() -= cur.idx() % cur.ncols();
1179 } else if (cur.idx() != 0) {
1183 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1187 case LFUN_WORDRIGHTSEL:
1188 case LFUN_WORDRIGHT:
1191 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
1192 cur.macroModeClose();
1194 if (cur.pos() != cur.lastpos()) {
1195 cur.pos() = cur.lastpos();
1196 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1197 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1198 cur.pos() = cur.lastpos();
1199 } else if (cur.idx() != cur.lastidx()) {
1200 cur.idx() = cur.lastidx();
1201 cur.pos() = cur.lastpos();
1203 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1208 MathNestInset::doDispatch(cur, cmd);
1213 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1214 FuncStatus & flag) const
1217 switch (cmd.action) {
1218 case LFUN_TABULAR_FEATURE:
1220 // should be more precise
1221 if (v_align_ == '\0') {
1225 if (cmd.argument.empty()) {
1229 if (!contains("tcb", cmd.argument[0])) {
1233 flag.setOnOff(cmd.argument[0] == v_align_);
1238 ret = MathNestInset::getStatus(cur, cmd, flag);