2 * \file InsetMathGrid.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathGrid.h"
15 #include "MathParser.h"
16 #include "MathStream.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "FuncStatus.h"
24 #include "FuncRequest.h"
27 #include "frontends/Clipboard.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
37 using support::bformat;
46 using std::istringstream;
52 docstring verboseHLine(int n)
55 for (int i = 0; i < n; ++i)
63 int extractInt(istream & is)
67 return (num == 0) ? 1 : num;
73 //////////////////////////////////////////////////////////////
76 InsetMathGrid::CellInfo::CellInfo()
83 //////////////////////////////////////////////////////////////
86 InsetMathGrid::RowInfo::RowInfo()
87 : lines_(0), skip_(0), allow_pagebreak_(true)
92 int InsetMathGrid::RowInfo::skipPixels() const
94 return crskip_.inBP();
99 //////////////////////////////////////////////////////////////
102 InsetMathGrid::ColInfo::ColInfo()
103 : align_('c'), lines_(0)
107 //////////////////////////////////////////////////////////////
110 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
111 : InsetMathNest(guessColumns(h)),
113 colinfo_(guessColumns(h) + 1),
114 cellinfo_(1 * guessColumns(h))
119 //lyxerr << "created grid with " << ncols() << " columns" << endl;
123 InsetMathGrid::InsetMathGrid()
134 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
135 : InsetMathNest(m * n),
145 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
146 : InsetMathNest(m * n),
158 Inset * InsetMathGrid::clone() const
160 return new InsetMathGrid(*this);
164 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
166 return col + ncols() * row;
170 void InsetMathGrid::setDefaults()
173 lyxerr << "positive number of columns expected" << endl;
175 // lyxerr << "positive number of rows expected" << endl;
176 for (col_type col = 0; col < ncols(); ++col) {
177 colinfo_[col].align_ = defaultColAlign(col);
178 colinfo_[col].skip_ = defaultColSpace(col);
179 colinfo_[col].special_.clear();
184 void InsetMathGrid::halign(docstring const & hh)
187 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
190 colinfo_[col].lines_++;
191 } else if ((c == 'p' || c == 'm' || c == 'b'||
192 c == '!' || c == '@' || c == '>' || c == '<') &&
193 it + 1 != hh.end() && *(it + 1) == '{') {
194 // @{decl.} and p{width} are standard LaTeX, the
195 // others are extensions by array.sty
196 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
198 // this declares a new column
200 // Only intercolumn stuff is allowed
201 // in the last dummy column
203 colinfo_[col].align_ = 'l';
205 // this is intercolumn stuff
206 if (colinfo_[col].special_.empty())
207 // Overtake possible lines
208 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
212 while (it != hh.end()) {
214 colinfo_[col].special_ += c;
220 if (brace_open > 0 && brace_open == brace_close)
225 colinfo_[col].lines_ = std::count(
226 colinfo_[col].special_.begin(),
227 colinfo_[col].special_.end(), '|');
228 LYXERR(Debug::MATHED)
229 << "special column separator: `"
230 << to_utf8(colinfo_[col].special_)
233 colinfo_[col].lines_ = 0;
234 colinfo_[col].special_.clear();
236 } else if (col >= ncols()) {
237 // Only intercolumn stuff is allowed in the last
240 } else if (c == 'c' || c == 'l' || c == 'r') {
241 colinfo_[col].align_ = static_cast<char>(c);
242 if (!colinfo_[col].special_.empty()) {
243 colinfo_[col].special_ += c;
244 colinfo_[col].lines_ = std::count(
245 colinfo_[col].special_.begin(),
246 colinfo_[col].special_.end(), '|');
247 LYXERR(Debug::MATHED)
248 << "special column separator: `"
249 << to_utf8(colinfo_[col].special_)
253 colinfo_[col].lines_ = 0;
254 colinfo_[col].special_.clear();
256 lyxerr << "unknown column separator: '" << c << "'" << endl;
261 col_type n = hh.size();
264 for (col_type col = 0; col < n; ++col)
265 colinfo_[col].align_ = hh[col];
270 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
273 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
274 if (*it == 'c' || *it == 'l' || *it == 'r'||
275 *it == 'p' || *it == 'm' || *it == 'b')
277 // let's have at least one column, even if we did not recognize its
285 void InsetMathGrid::halign(char h, col_type col)
287 colinfo_[col].align_ = h;
288 if (!colinfo_[col].special_.empty()) {
289 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
290 if (c == 'l' || c == 'c' || c == 'r')
293 // FIXME: Change alignment of p, m and b columns, too
297 char InsetMathGrid::halign(col_type col) const
299 return colinfo_[col].align_;
303 docstring InsetMathGrid::halign() const
306 for (col_type col = 0; col < ncols(); ++col) {
307 if (colinfo_[col].special_.empty()) {
308 res += docstring(colinfo_[col].lines_, '|');
309 res += colinfo_[col].align_;
311 res += colinfo_[col].special_;
313 if (colinfo_[ncols()].special_.empty())
314 return res + docstring(colinfo_[ncols()].lines_, '|');
315 return res + colinfo_[ncols()].special_;
319 void InsetMathGrid::valign(char c)
325 char InsetMathGrid::valign() const
331 InsetMathGrid::col_type InsetMathGrid::ncols() const
333 return colinfo_.size() - 1;
337 InsetMathGrid::row_type InsetMathGrid::nrows() const
339 return rowinfo_.size() - 1;
343 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
345 return idx % ncols();
349 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
351 return idx / ncols();
355 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
357 rowinfo_[row].crskip_ = crskip;
361 Length InsetMathGrid::vcrskip(row_type row) const
363 return rowinfo_[row].crskip_;
367 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
369 // let the cells adjust themselves
370 InsetMathNest::metrics(mi);
372 BufferView & bv = *mi.base.bv;
374 // compute absolute sizes of vertical structure
375 for (row_type row = 0; row < nrows(); ++row) {
378 for (col_type col = 0; col < ncols(); ++col) {
379 Dimension const & dimc = cell(index(row, col)).dimension(bv);
380 asc = max(asc, dimc.asc);
381 desc = max(desc, dimc.des);
383 rowinfo_[row].ascent_ = asc;
384 rowinfo_[row].descent_ = desc;
386 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
387 rowinfo_[nrows()].ascent_ = 0;
388 rowinfo_[nrows()].descent_ = 0;
390 // compute vertical offsets
391 rowinfo_[0].offset_ = 0;
392 for (row_type row = 1; row <= nrows(); ++row) {
393 rowinfo_[row].offset_ =
394 rowinfo_[row - 1].offset_ +
395 rowinfo_[row - 1].descent_ +
396 rowinfo_[row - 1].skipPixels() +
398 rowinfo_[row].lines_ * hlinesep() +
399 rowinfo_[row].ascent_;
402 // adjust vertical offset
409 h = rowinfo_[nrows() - 1].offset_;
412 h = rowinfo_[nrows() - 1].offset_ / 2;
414 for (row_type row = 0; row <= nrows(); ++row)
415 rowinfo_[row].offset_ -= h;
418 // compute absolute sizes of horizontal structure
419 for (col_type col = 0; col < ncols(); ++col) {
421 for (row_type row = 0; row < nrows(); ++row)
422 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
423 colinfo_[col].width_ = wid;
425 colinfo_[ncols()].width_ = 0;
427 // compute horizontal offsets
428 colinfo_[0].offset_ = border();
429 for (col_type col = 1; col <= ncols(); ++col) {
430 colinfo_[col].offset_ =
431 colinfo_[col - 1].offset_ +
432 colinfo_[col - 1].width_ +
433 colinfo_[col - 1].skip_ +
435 colinfo_[col].lines_ * vlinesep();
439 dim.wid = colinfo_[ncols() - 1].offset_
440 + colinfo_[ncols() - 1].width_
441 + vlinesep() * colinfo_[ncols()].lines_
444 dim.asc = - rowinfo_[0].offset_
445 + rowinfo_[0].ascent_
446 + hlinesep() * rowinfo_[0].lines_
449 dim.des = rowinfo_[nrows() - 1].offset_
450 + rowinfo_[nrows() - 1].descent_
451 + hlinesep() * rowinfo_[nrows()].lines_
456 // Increase ws_[i] for 'R' columns (except the first one)
457 for (int i = 1; i < nc_; ++i)
458 if (align_[i] == 'R')
459 ws_[i] += 10 * df_width;
460 // Increase ws_[i] for 'C' column
461 if (align_[0] == 'C')
462 if (ws_[0] < 7 * workwidth / 8)
463 ws_[0] = 7 * workwidth / 8;
467 for (cxrow = row_.begin(); cxrow; ++cxrow) {
470 for (int i = 0; i < nc_; ++i) {
472 if (cxrow->getTab(i) <= 0) {
473 cxrow->setTab(i, df_width);
481 lf = (ws_[i] - cxrow->getTab(i))/2;
485 lf = ws_[i] - cxrow->getTab(i);
488 if (cxrow == row_.begin())
490 else if (cxrow.is_last())
491 lf = ws_[i] - cxrow->getTab(i);
493 lf = (ws_[i] - cxrow->getTab(i))/2;
496 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
497 cxrow->setTab(i, lf + rg);
498 rg = ws_[i] - ww + colsep();
499 if (cxrow == row_.begin())
500 width += ws_[i] + colsep();
502 cxrow->setBaseline(cxrow->getBaseline() - ascent);
505 metricsMarkers2(dim);
506 // Cache the inset dimension.
507 setDimCache(mi, dim);
511 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
513 drawWithMargin(pi, x, y, 0, 0);
517 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
518 int lmargin, int rmargin) const
520 Dimension const dim = dimension(*pi.base.bv);
521 BufferView const & bv = *pi.base.bv;
523 for (idx_type idx = 0; idx < nargs(); ++idx)
524 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
525 y + cellYOffset(idx));
527 for (row_type row = 0; row <= nrows(); ++row)
528 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
529 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
530 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
531 pi.pain.line(x + lmargin + 1, yy,
532 x + dim.width() - rmargin - 1, yy,
536 for (col_type col = 0; col <= ncols(); ++col)
537 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
538 int xx = x + lmargin + colinfo_[col].offset_
539 - i * vlinesep() - vlinesep()/2 - colsep()/2;
540 pi.pain.line(xx, y - dim.ascent() + 1,
541 xx, y + dim.descent() - 1,
544 drawMarkers2(pi, x, y);
548 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
550 // let the cells adjust themselves
551 //InsetMathNest::metrics(mi);
552 for (idx_type i = 0; i < nargs(); ++i)
553 cell(i).metricsT(mi, dim);
555 // compute absolute sizes of vertical structure
556 for (row_type row = 0; row < nrows(); ++row) {
559 for (col_type col = 0; col < ncols(); ++col) {
560 //MathData const & c = cell(index(row, col));
563 asc = max(asc, dimc.ascent());
564 desc = max(desc, dimc.descent());
566 rowinfo_[row].ascent_ = asc;
567 rowinfo_[row].descent_ = desc;
569 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
570 rowinfo_[nrows()].ascent_ = 0;
571 rowinfo_[nrows()].descent_ = 0;
573 // compute vertical offsets
574 rowinfo_[0].offset_ = 0;
575 for (row_type row = 1; row <= nrows(); ++row) {
576 rowinfo_[row].offset_ =
577 rowinfo_[row - 1].offset_ +
578 rowinfo_[row - 1].descent_ +
579 //rowinfo_[row - 1].skipPixels() +
581 //rowinfo_[row].lines_ * hlinesep() +
582 rowinfo_[row].ascent_;
585 // adjust vertical offset
592 h = rowinfo_[nrows() - 1].offset_;
595 h = rowinfo_[nrows() - 1].offset_ / 2;
597 for (row_type row = 0; row <= nrows(); ++row)
598 rowinfo_[row].offset_ -= h;
601 // compute absolute sizes of horizontal structure
602 for (col_type col = 0; col < ncols(); ++col) {
604 for (row_type row = 0; row < nrows(); ++row) {
606 //wid = max(wid, cell(index(row, col)).width());
608 colinfo_[col].width_ = wid;
610 colinfo_[ncols()].width_ = 0;
612 // compute horizontal offsets
613 colinfo_[0].offset_ = border();
614 for (col_type col = 1; col <= ncols(); ++col) {
615 colinfo_[col].offset_ =
616 colinfo_[col - 1].offset_ +
617 colinfo_[col - 1].width_ +
618 colinfo_[col - 1].skip_ +
620 //colinfo_[col].lines_ * vlinesep();
624 dim.wid = colinfo_[ncols() - 1].offset_
625 + colinfo_[ncols() - 1].width_
626 //+ vlinesep() * colinfo_[ncols()].lines_
629 dim.asc = -rowinfo_[0].offset_
630 + rowinfo_[0].ascent_
631 //+ hlinesep() * rowinfo_[0].lines_
634 dim.des = rowinfo_[nrows() - 1].offset_
635 + rowinfo_[nrows() - 1].descent_
636 //+ hlinesep() * rowinfo_[nrows()].lines_
641 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
643 // for (idx_type idx = 0; idx < nargs(); ++idx)
644 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
648 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
652 if (!rowinfo_[row].crskip_.zero())
653 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
654 else if(!rowinfo_[row].allow_pagebreak_)
657 // make sure an upcoming '[' does not break anything
658 if (row + 1 < nrows()) {
659 MathData const & c = cell(index(row + 1, 0));
660 if (c.size() && c.front()->getChar() == '[')
665 // only add \\ if necessary
666 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
669 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
673 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
675 if (col + 1 == lastcol)
677 return from_ascii(" & ");
681 void InsetMathGrid::addRow(row_type row)
683 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
685 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
687 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
691 void InsetMathGrid::appendRow()
693 rowinfo_.push_back(RowInfo());
694 //cells_.insert(cells_.end(), ncols(), MathData());
695 for (col_type col = 0; col < ncols(); ++col) {
696 cells_.push_back(cells_type::value_type());
697 cellinfo_.push_back(CellInfo());
702 void InsetMathGrid::delRow(row_type row)
707 cells_type::iterator it = cells_.begin() + row * ncols();
708 cells_.erase(it, it + ncols());
710 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
711 cellinfo_.erase(jt, jt + ncols());
713 rowinfo_.erase(rowinfo_.begin() + row);
717 void InsetMathGrid::copyRow(row_type row)
720 for (col_type col = 0; col < ncols(); ++col)
721 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
725 void InsetMathGrid::swapRow(row_type row)
729 if (row + 1 == nrows())
731 for (col_type col = 0; col < ncols(); ++col)
732 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
736 void InsetMathGrid::addCol(col_type newcol)
738 const col_type nc = ncols();
739 const row_type nr = nrows();
740 cells_type new_cells((nc + 1) * nr);
741 vector<CellInfo> new_cellinfo((nc + 1) * nr);
743 for (row_type row = 0; row < nr; ++row)
744 for (col_type col = 0; col < nc; ++col) {
745 new_cells[row * (nc + 1) + col + (col >= newcol)]
746 = cells_[row * nc + col];
747 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
748 = cellinfo_[row * nc + col];
750 swap(cells_, new_cells);
751 swap(cellinfo_, new_cellinfo);
754 inf.skip_ = defaultColSpace(newcol);
755 inf.align_ = defaultColAlign(newcol);
756 colinfo_.insert(colinfo_.begin() + newcol, inf);
760 void InsetMathGrid::delCol(col_type col)
766 vector<CellInfo> tmpcellinfo;
767 for (col_type i = 0; i < nargs(); ++i)
768 if (i % ncols() != col) {
769 tmpcells.push_back(cells_[i]);
770 tmpcellinfo.push_back(cellinfo_[i]);
772 swap(cells_, tmpcells);
773 swap(cellinfo_, tmpcellinfo);
775 colinfo_.erase(colinfo_.begin() + col);
779 void InsetMathGrid::copyCol(col_type col)
782 for (row_type row = 0; row < nrows(); ++row)
783 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
787 void InsetMathGrid::swapCol(col_type col)
791 if (col + 1 == ncols())
793 for (row_type row = 0; row < nrows(); ++row)
794 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
798 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
800 col_type c = col(idx);
801 int x = colinfo_[c].offset_;
802 char align = colinfo_[c].align_;
803 Dimension const & celldim = cell(idx).dimension(bv);
804 if (align == 'r' || align == 'R')
805 x += colinfo_[c].width_ - celldim.wid;
806 if (align == 'c' || align == 'C')
807 x += (colinfo_[c].width_ - celldim.wid) / 2;
812 int InsetMathGrid::cellYOffset(idx_type idx) const
814 return rowinfo_[row(idx)].offset_;
818 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
823 cur.idx() -= ncols();
825 if (cur.row() + 1 >= nrows())
827 cur.idx() += ncols();
829 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
834 bool InsetMathGrid::idxLeft(Cursor & cur) const
836 // leave matrix if on the left hand edge
840 cur.pos() = cur.lastpos();
845 bool InsetMathGrid::idxRight(Cursor & cur) const
847 // leave matrix if on the right hand edge
848 if (cur.col() + 1 == ncols())
856 bool InsetMathGrid::idxFirst(Cursor & cur) const
863 cur.idx() = (nrows() - 1) * ncols();
866 cur.idx() = ((nrows() - 1) / 2) * ncols();
873 bool InsetMathGrid::idxLast(Cursor & cur) const
877 cur.idx() = ncols() - 1;
880 cur.idx() = nargs() - 1;
883 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
885 cur.pos() = cur.lastpos();
890 bool InsetMathGrid::idxDelete(idx_type & idx)
892 // nothing to do if we have just one row
896 // nothing to do if we are in the middle of the last row of the inset
897 if (idx + ncols() > nargs())
900 // try to delete entire sequence of ncols() empty cells if possible
901 for (idx_type i = idx; i < idx + ncols(); ++i)
905 // move cells if necessary
906 for (idx_type i = index(row(idx), 0); i < idx; ++i)
907 swap(cell(i), cell(i + ncols()));
914 // undo effect of Ctrl-Tab (i.e. pull next cell)
915 //if (idx + 1 != nargs())
916 // cell(idx).swap(cell(idx + 1));
918 // we handled the event..
923 // reimplement old behaviour when pressing Delete in the last position
925 void InsetMathGrid::idxGlue(idx_type idx)
927 col_type c = col(idx);
928 if (c + 1 == ncols()) {
929 if (row(idx) + 1 != nrows()) {
930 for (col_type cc = 0; cc < ncols(); ++cc)
931 cell(idx).append(cell(idx + cc + 1));
932 delRow(row(idx) + 1);
935 cell(idx).append(cell(idx + 1));
936 for (col_type cc = c + 2; cc < ncols(); ++cc)
937 cell(idx - c + cc - 1) = cell(idx - c + cc);
938 cell(idx - c + ncols() - 1).clear();
943 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
945 return rowinfo_[row];
949 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
951 return rowinfo_[row];
955 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
957 row_type const ri = row(idx);
958 row_type const r1 = min(row(from), row(to));
959 row_type const r2 = max(row(from), row(to));
960 col_type const ci = col(idx);
961 col_type const c1 = min(col(from), col(to));
962 col_type const c2 = max(col(from), col(to));
963 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
968 void InsetMathGrid::normalize(NormalStream & os) const
971 for (row_type row = 0; row < nrows(); ++row) {
973 for (col_type col = 0; col < ncols(); ++col)
974 os << "[cell " << cell(index(row, col)) << ']';
981 void InsetMathGrid::mathmlize(MathStream & os) const
983 os << MTag("mtable");
984 for (row_type row = 0; row < nrows(); ++row) {
986 for (col_type col = 0; col < ncols(); ++col)
987 os << cell(index(row, col));
990 os << ETag("mtable");
994 void InsetMathGrid::write(WriteStream & os) const
997 for (row_type row = 0; row < nrows(); ++row) {
998 os << verboseHLine(rowinfo_[row].lines_);
999 // don't write & and empty cells at end of line
1000 col_type lastcol = 0;
1001 bool emptyline = true;
1002 for (col_type col = 0; col < ncols(); ++col)
1003 if (!cell(index(row, col)).empty()) {
1007 for (col_type col = 0; col < lastcol; ++col)
1008 os << cell(index(row, col)) << eocString(col, lastcol);
1009 eol = eolString(row, emptyline, os.fragile());
1011 // append newline only if line wasn't completely empty
1012 // and this was not the last line in the grid
1013 if (!emptyline && row + 1 < nrows())
1016 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1028 int InsetMathGrid::colsep() const
1034 int InsetMathGrid::rowsep() const
1040 int InsetMathGrid::hlinesep() const
1046 int InsetMathGrid::vlinesep() const
1052 int InsetMathGrid::border() const
1058 void InsetMathGrid::splitCell(Cursor & cur)
1060 if (cur.idx() == cur.lastidx())
1062 MathData ar = cur.cell();
1063 ar.erase(0, cur.pos());
1064 cur.cell().erase(cur.pos(), cur.lastpos());
1067 cur.cell().insert(0, ar);
1071 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1073 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1074 switch (cmd.action) {
1076 // insert file functions
1077 case LFUN_LINE_DELETE:
1078 // FIXME: We use recordUndoInset when a change reflects more
1079 // than one cell, because recordUndo does not work for
1080 // multiple cells. Unfortunately this puts the cursor in front
1081 // of the inset after undo. This is (especilally for large
1083 cur.recordUndoInset();
1084 //autocorrect_ = false;
1092 if (cur.idx() > cur.lastidx())
1093 cur.idx() = cur.lastidx();
1094 if (cur.pos() > cur.lastpos())
1095 cur.pos() = cur.lastpos();
1098 case LFUN_CELL_SPLIT:
1103 case LFUN_CELL_BACKWARD:
1105 cur.selection() = false;
1106 if (!idxPrev(cur)) {
1107 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1112 case LFUN_CELL_FORWARD:
1113 // Can't handle selection by additional 'shift' as this is
1114 // hard bound to LFUN_CELL_BACKWARD
1115 cur.selection() = false;
1116 if (!idxNext(cur)) {
1117 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1122 case LFUN_BREAK_LINE: {
1123 cur.recordUndoInset();
1124 row_type const r = cur.row();
1128 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1129 swap(cell(index(r, c)), cell(index(r + 1, c)));
1133 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1136 cur.pos() = cur.lastpos();
1138 //mathcursor->normalize();
1139 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1143 case LFUN_TABULAR_FEATURE: {
1144 cur.recordUndoInset();
1145 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1146 istringstream is(to_utf8(cmd.argument()));
1149 if (s == "valign-top")
1151 else if (s == "valign-middle")
1153 else if (s == "valign-bottom")
1155 else if (s == "align-left")
1156 halign('l', cur.col());
1157 else if (s == "align-right")
1158 halign('r', cur.col());
1159 else if (s == "align-center")
1160 halign('c', cur.col());
1161 else if (s == "append-row")
1162 for (int i = 0, n = extractInt(is); i < n; ++i)
1164 else if (s == "delete-row") {
1165 for (int i = 0, n = extractInt(is); i < n; ++i) {
1167 if (cur.idx() >= nargs())
1168 cur.idx() -= ncols();
1170 cur.pos() = 0; // trick, see below
1172 else if (s == "copy-row") {
1173 // Here (as later) we save the cursor col/row
1174 // in order to restore it after operation.
1175 row_type const r = cur.row();
1176 col_type const c = cur.col();
1177 for (int i = 0, n = extractInt(is); i < n; ++i)
1179 cur.idx() = index(r, c);
1181 else if (s == "swap-row") {
1183 // Trick to suppress same-idx-means-different-cell
1187 else if (s == "add-hline-above")
1188 rowinfo_[cur.row()].lines_++;
1189 else if (s == "add-hline-below")
1190 rowinfo_[cur.row()+1].lines_++;
1191 else if (s == "delete-hline-above")
1192 rowinfo_[cur.row()].lines_--;
1193 else if (s == "delete-hline-below")
1194 rowinfo_[cur.row()+1].lines_--;
1195 else if (s == "append-column") {
1196 row_type const r = cur.row();
1197 col_type const c = cur.col();
1198 for (int i = 0, n = extractInt(is); i < n; ++i)
1200 cur.idx() = index(r, c);
1202 else if (s == "delete-column") {
1203 row_type const r = cur.row();
1204 col_type const c = cur.col();
1205 for (int i = 0, n = extractInt(is); i < n; ++i)
1206 delCol(col(cur.idx()));
1207 cur.idx() = index(r, min(c, cur.ncols() - 1));
1208 cur.pos() = 0; // trick, see above
1210 else if (s == "copy-column") {
1211 row_type const r = cur.row();
1212 col_type const c = cur.col();
1214 cur.idx() = index(r, c);
1216 else if (s == "swap-column") {
1218 cur.pos() = 0; // trick, see above
1220 else if (s == "add-vline-left") {
1221 colinfo_[cur.col()].lines_++;
1222 if (!colinfo_[cur.col()].special_.empty())
1223 colinfo_[cur.col()].special_ += '|';
1225 else if (s == "add-vline-right") {
1226 colinfo_[cur.col()+1].lines_++;
1227 if (!colinfo_[cur.col()+1].special_.empty())
1228 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1230 else if (s == "delete-vline-left") {
1231 colinfo_[cur.col()].lines_--;
1232 docstring & special = colinfo_[cur.col()].special_;
1233 if (!special.empty()) {
1234 docstring::size_type i = special.rfind('|');
1235 BOOST_ASSERT(i != docstring::npos);
1236 special.erase(i, 1);
1239 else if (s == "delete-vline-right") {
1240 colinfo_[cur.col()+1].lines_--;
1241 docstring & special = colinfo_[cur.col()+1].special_;
1242 if (!special.empty()) {
1243 docstring::size_type i = special.find('|');
1244 BOOST_ASSERT(i != docstring::npos);
1245 special.erase(i, 1);
1252 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1253 lyxerr << "returning FINISHED_LEFT" << endl;
1258 cur.message(_("Paste"));
1259 cap::replaceSelection(cur);
1261 if (cmd.argument().empty() && !theClipboard().isInternal())
1262 topaste = theClipboard().getAsText();
1264 idocstringstream is(cmd.argument());
1267 topaste = cap::getSelection(cur.buffer(), n);
1269 InsetMathGrid grid(1, 1);
1270 if (!topaste.empty())
1271 mathed_parse_normal(grid, topaste);
1273 if (grid.nargs() == 1) {
1274 // single cell/part of cell
1276 cur.cell().insert(cur.pos(), grid.cell(0));
1277 cur.pos() += grid.cell(0).size();
1280 cur.recordUndoInset();
1281 col_type const numcols =
1282 min(grid.ncols(), ncols() - col(cur.idx()));
1283 row_type const numrows =
1284 min(grid.nrows(), nrows() - cur.row());
1285 for (row_type r = 0; r < numrows; ++r) {
1286 for (col_type c = 0; c < numcols; ++c) {
1287 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1288 cell(i).insert(0, grid.cell(grid.index(r, c)));
1290 // append the left over horizontal cells to the last column
1291 idx_type i = index(r + cur.row(), ncols() - 1);
1292 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1293 cell(i).append(grid.cell(grid.index(r, c)));
1295 // append the left over vertical cells to the last _cell_
1296 idx_type i = nargs() - 1;
1297 for (row_type r = numrows; r < grid.nrows(); ++r)
1298 for (col_type c = 0; c < grid.ncols(); ++c)
1299 cell(i).append(grid.cell(grid.index(r, c)));
1301 cur.clearSelection(); // bug 393
1306 case LFUN_LINE_BEGIN_SELECT:
1307 case LFUN_LINE_BEGIN:
1308 case LFUN_WORD_BACKWARD_SELECT:
1309 case LFUN_WORD_BACKWARD:
1310 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1311 cmd.action == LFUN_LINE_BEGIN_SELECT);
1312 cur.macroModeClose();
1313 if (cur.pos() != 0) {
1315 } else if (cur.idx() % cur.ncols() != 0) {
1316 cur.idx() -= cur.idx() % cur.ncols();
1318 } else if (cur.idx() != 0) {
1322 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1327 case LFUN_WORD_FORWARD_SELECT:
1328 case LFUN_WORD_FORWARD:
1329 case LFUN_LINE_END_SELECT:
1331 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1332 cmd.action == LFUN_LINE_END_SELECT);
1333 cur.macroModeClose();
1335 if (cur.pos() != cur.lastpos()) {
1336 cur.pos() = cur.lastpos();
1337 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1338 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1339 cur.pos() = cur.lastpos();
1340 } else if (cur.idx() != cur.lastidx()) {
1341 cur.idx() = cur.lastidx();
1342 cur.pos() = cur.lastpos();
1344 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1350 InsetMathNest::doDispatch(cur, cmd);
1355 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1356 FuncStatus & status) const
1358 switch (cmd.action) {
1359 case LFUN_TABULAR_FEATURE: {
1360 string const s = to_utf8(cmd.argument());
1361 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1362 status.enabled(false);
1363 status.message(from_utf8(N_("Only one row")));
1367 (s == "delete-column" || s == "swap-column")) {
1368 status.enabled(false);
1369 status.message(from_utf8(N_("Only one column")));
1372 if ((rowinfo_[cur.row()].lines_ == 0 &&
1373 s == "delete-hline-above") ||
1374 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1375 s == "delete-hline-below")) {
1376 status.enabled(false);
1377 status.message(from_utf8(N_("No hline to delete")));
1381 if ((colinfo_[cur.col()].lines_ == 0 &&
1382 s == "delete-vline-left") ||
1383 (colinfo_[cur.col() + 1].lines_ == 0 &&
1384 s == "delete-vline-right")) {
1385 status.enabled(false);
1386 status.message(from_utf8(N_("No vline to delete")));
1389 if (s == "valign-top" || s == "valign-middle" ||
1390 s == "valign-bottom" || s == "align-left" ||
1391 s == "align-right" || s == "align-center" ||
1392 s == "append-row" || s == "delete-row" ||
1393 s == "copy-row" || s == "swap-row" ||
1394 s == "add-hline-above" || s == "add-hline-below" ||
1395 s == "delete-hline-above" || s == "delete-hline-below" ||
1396 s == "append-column" || s == "delete-column" ||
1397 s == "copy-column" || s == "swap-column" ||
1398 s == "add-vline-left" || s == "add-vline-right" ||
1399 s == "delete-vline-left" || s == "delete-vline-right")
1400 status.enabled(true);
1402 status.enabled(false);
1403 status.message(bformat(
1404 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1407 status.setOnOff((s == "align-left" && halign(cur.col()) == 'l')
1408 || (s == "align-right" && halign(cur.col()) == 'r')
1409 || (s == "align-center" && halign(cur.col()) == 'c')
1410 || (s == "valign-top" && valign() == 't')
1411 || (s == "valign-bottom" && valign() == 'b')
1412 || (s == "valign-middle" && valign() == 'm'));
1415 // FIXME: What did this code do?
1416 // Please check whether it is still needed!
1417 // should be more precise
1418 if (v_align_ == '\0') {
1419 status.enable(true);
1422 if (cmd.argument().empty()) {
1423 status.enable(false);
1426 if (!support::contains("tcb", cmd.argument()[0])) {
1427 status.enable(false);
1430 status.setOnOff(cmd.argument()[0] == v_align_);
1431 status.enabled(true);
1436 case LFUN_CELL_SPLIT:
1437 status.enabled(true);
1440 case LFUN_CELL_BACKWARD:
1441 case LFUN_CELL_FORWARD:
1442 status.enabled(true);
1446 return InsetMathNest::getStatus(cur, cmd, status);