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"
28 #include "frontends/Clipboard.h"
29 #include "frontends/Painter.h"
31 #include "support/lstrings.h"
38 using support::bformat;
47 using std::istringstream;
53 docstring verboseHLine(int n)
56 for (int i = 0; i < n; ++i)
64 int extractInt(istream & is)
68 return (num == 0) ? 1 : num;
74 //////////////////////////////////////////////////////////////
77 InsetMathGrid::CellInfo::CellInfo()
84 //////////////////////////////////////////////////////////////
87 InsetMathGrid::RowInfo::RowInfo()
88 : lines_(0), skip_(0), allow_pagebreak_(true)
93 int InsetMathGrid::RowInfo::skipPixels() const
95 return crskip_.inBP();
100 //////////////////////////////////////////////////////////////
103 InsetMathGrid::ColInfo::ColInfo()
104 : align_('c'), lines_(0)
108 //////////////////////////////////////////////////////////////
111 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
112 : InsetMathNest(guessColumns(h)),
114 colinfo_(guessColumns(h) + 1),
115 cellinfo_(1 * guessColumns(h))
120 //lyxerr << "created grid with " << ncols() << " columns" << endl;
124 InsetMathGrid::InsetMathGrid()
135 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
136 : InsetMathNest(m * n),
146 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
147 : InsetMathNest(m * n),
159 Inset * InsetMathGrid::clone() const
161 return new InsetMathGrid(*this);
165 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
167 return col + ncols() * row;
171 void InsetMathGrid::setDefaults()
174 lyxerr << "positive number of columns expected" << endl;
176 // lyxerr << "positive number of rows expected" << endl;
177 for (col_type col = 0; col < ncols(); ++col) {
178 colinfo_[col].align_ = defaultColAlign(col);
179 colinfo_[col].skip_ = defaultColSpace(col);
180 colinfo_[col].special_.clear();
185 void InsetMathGrid::halign(docstring const & hh)
188 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
191 colinfo_[col].lines_++;
192 } else if ((c == 'p' || c == 'm' || c == 'b'||
193 c == '!' || c == '@' || c == '>' || c == '<') &&
194 it + 1 != hh.end() && *(it + 1) == '{') {
195 // @{decl.} and p{width} are standard LaTeX, the
196 // others are extensions by array.sty
197 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
199 // this declares a new column
201 // Only intercolumn stuff is allowed
202 // in the last dummy column
204 colinfo_[col].align_ = 'l';
206 // this is intercolumn stuff
207 if (colinfo_[col].special_.empty())
208 // Overtake possible lines
209 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
213 while (it != hh.end()) {
215 colinfo_[col].special_ += c;
221 if (brace_open > 0 && brace_open == brace_close)
226 colinfo_[col].lines_ = std::count(
227 colinfo_[col].special_.begin(),
228 colinfo_[col].special_.end(), '|');
229 LYXERR(Debug::MATHED)
230 << "special column separator: `"
231 << to_utf8(colinfo_[col].special_)
234 colinfo_[col].lines_ = 0;
235 colinfo_[col].special_.clear();
237 } else if (col >= ncols()) {
238 // Only intercolumn stuff is allowed in the last
241 } else if (c == 'c' || c == 'l' || c == 'r') {
242 colinfo_[col].align_ = static_cast<char>(c);
243 if (!colinfo_[col].special_.empty()) {
244 colinfo_[col].special_ += c;
245 colinfo_[col].lines_ = std::count(
246 colinfo_[col].special_.begin(),
247 colinfo_[col].special_.end(), '|');
248 LYXERR(Debug::MATHED)
249 << "special column separator: `"
250 << to_utf8(colinfo_[col].special_)
254 colinfo_[col].lines_ = 0;
255 colinfo_[col].special_.clear();
257 lyxerr << "unknown column separator: '" << c << "'" << endl;
262 col_type n = hh.size();
265 for (col_type col = 0; col < n; ++col)
266 colinfo_[col].align_ = hh[col];
271 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
274 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
275 if (*it == 'c' || *it == 'l' || *it == 'r'||
276 *it == 'p' || *it == 'm' || *it == 'b')
278 // let's have at least one column, even if we did not recognize its
286 void InsetMathGrid::halign(char h, col_type col)
288 colinfo_[col].align_ = h;
289 if (!colinfo_[col].special_.empty()) {
290 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
291 if (c == 'l' || c == 'c' || c == 'r')
294 // FIXME: Change alignment of p, m and b columns, too
298 char InsetMathGrid::halign(col_type col) const
300 return colinfo_[col].align_;
304 docstring InsetMathGrid::halign() const
307 for (col_type col = 0; col < ncols(); ++col) {
308 if (colinfo_[col].special_.empty()) {
309 res += docstring(colinfo_[col].lines_, '|');
310 res += colinfo_[col].align_;
312 res += colinfo_[col].special_;
314 if (colinfo_[ncols()].special_.empty())
315 return res + docstring(colinfo_[ncols()].lines_, '|');
316 return res + colinfo_[ncols()].special_;
320 void InsetMathGrid::valign(char c)
326 char InsetMathGrid::valign() const
332 InsetMathGrid::col_type InsetMathGrid::ncols() const
334 return colinfo_.size() - 1;
338 InsetMathGrid::row_type InsetMathGrid::nrows() const
340 return rowinfo_.size() - 1;
344 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
346 return idx % ncols();
350 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
352 return idx / ncols();
356 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
358 rowinfo_[row].crskip_ = crskip;
362 Length InsetMathGrid::vcrskip(row_type row) const
364 return rowinfo_[row].crskip_;
368 void InsetMathGrid::metrics(MetricsInfo & mi) const
370 // let the cells adjust themselves
371 InsetMathNest::metrics(mi);
373 // compute absolute sizes of vertical structure
374 for (row_type row = 0; row < nrows(); ++row) {
377 for (col_type col = 0; col < ncols(); ++col) {
378 MathData const & c = cell(index(row, col));
379 asc = max(asc, c.ascent());
380 desc = max(desc, c.descent());
382 rowinfo_[row].ascent_ = asc;
383 rowinfo_[row].descent_ = desc;
385 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
386 rowinfo_[nrows()].ascent_ = 0;
387 rowinfo_[nrows()].descent_ = 0;
389 // compute vertical offsets
390 rowinfo_[0].offset_ = 0;
391 for (row_type row = 1; row <= nrows(); ++row) {
392 rowinfo_[row].offset_ =
393 rowinfo_[row - 1].offset_ +
394 rowinfo_[row - 1].descent_ +
395 rowinfo_[row - 1].skipPixels() +
397 rowinfo_[row].lines_ * hlinesep() +
398 rowinfo_[row].ascent_;
401 // adjust vertical offset
408 h = rowinfo_[nrows() - 1].offset_;
411 h = rowinfo_[nrows() - 1].offset_ / 2;
413 for (row_type row = 0; row <= nrows(); ++row)
414 rowinfo_[row].offset_ -= h;
417 // compute absolute sizes of horizontal structure
418 for (col_type col = 0; col < ncols(); ++col) {
420 for (row_type row = 0; row < nrows(); ++row)
421 wid = max(wid, cell(index(row, col)).width());
422 colinfo_[col].width_ = wid;
424 colinfo_[ncols()].width_ = 0;
426 // compute horizontal offsets
427 colinfo_[0].offset_ = border();
428 for (col_type col = 1; col <= ncols(); ++col) {
429 colinfo_[col].offset_ =
430 colinfo_[col - 1].offset_ +
431 colinfo_[col - 1].width_ +
432 colinfo_[col - 1].skip_ +
434 colinfo_[col].lines_ * vlinesep();
438 dim_.wid = colinfo_[ncols() - 1].offset_
439 + colinfo_[ncols() - 1].width_
440 + vlinesep() * colinfo_[ncols()].lines_
443 dim_.asc = - rowinfo_[0].offset_
444 + rowinfo_[0].ascent_
445 + hlinesep() * rowinfo_[0].lines_
448 dim_.des = rowinfo_[nrows() - 1].offset_
449 + rowinfo_[nrows() - 1].descent_
450 + hlinesep() * rowinfo_[nrows()].lines_
455 // Increase ws_[i] for 'R' columns (except the first one)
456 for (int i = 1; i < nc_; ++i)
457 if (align_[i] == 'R')
458 ws_[i] += 10 * df_width;
459 // Increase ws_[i] for 'C' column
460 if (align_[0] == 'C')
461 if (ws_[0] < 7 * workwidth / 8)
462 ws_[0] = 7 * workwidth / 8;
466 for (cxrow = row_.begin(); cxrow; ++cxrow) {
469 for (int i = 0; i < nc_; ++i) {
471 if (cxrow->getTab(i) <= 0) {
472 cxrow->setTab(i, df_width);
480 lf = (ws_[i] - cxrow->getTab(i))/2;
484 lf = ws_[i] - cxrow->getTab(i);
487 if (cxrow == row_.begin())
489 else if (cxrow.is_last())
490 lf = ws_[i] - cxrow->getTab(i);
492 lf = (ws_[i] - cxrow->getTab(i))/2;
495 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
496 cxrow->setTab(i, lf + rg);
497 rg = ws_[i] - ww + colsep();
498 if (cxrow == row_.begin())
499 width += ws_[i] + colsep();
501 cxrow->setBaseline(cxrow->getBaseline() - ascent);
504 metricsMarkers2(dim_);
508 bool InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
519 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
521 drawWithMargin(pi, x, y, 0, 0);
524 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
525 int lmargin, int rmargin) const
527 for (idx_type idx = 0; idx < nargs(); ++idx)
528 cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
529 y + cellYOffset(idx));
531 for (row_type row = 0; row <= nrows(); ++row)
532 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
533 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
534 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
535 pi.pain.line(x + lmargin + 1, yy,
536 x + dim_.width() - rmargin - 1, yy,
540 for (col_type col = 0; col <= ncols(); ++col)
541 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
542 int xx = x + lmargin + colinfo_[col].offset_
543 - i * vlinesep() - vlinesep()/2 - colsep()/2;
544 pi.pain.line(xx, y - dim_.ascent() + 1,
545 xx, y + dim_.descent() - 1,
548 drawMarkers2(pi, x, y);
552 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
554 // let the cells adjust themselves
555 //InsetMathNest::metrics(mi);
556 for (idx_type i = 0; i < nargs(); ++i)
557 cell(i).metricsT(mi, dim);
559 // compute absolute sizes of vertical structure
560 for (row_type row = 0; row < nrows(); ++row) {
563 for (col_type col = 0; col < ncols(); ++col) {
564 MathData const & c = cell(index(row, col));
565 asc = max(asc, c.ascent());
566 desc = max(desc, c.descent());
568 rowinfo_[row].ascent_ = asc;
569 rowinfo_[row].descent_ = desc;
571 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
572 rowinfo_[nrows()].ascent_ = 0;
573 rowinfo_[nrows()].descent_ = 0;
575 // compute vertical offsets
576 rowinfo_[0].offset_ = 0;
577 for (row_type row = 1; row <= nrows(); ++row) {
578 rowinfo_[row].offset_ =
579 rowinfo_[row - 1].offset_ +
580 rowinfo_[row - 1].descent_ +
581 //rowinfo_[row - 1].skipPixels() +
583 //rowinfo_[row].lines_ * hlinesep() +
584 rowinfo_[row].ascent_;
587 // adjust vertical offset
594 h = rowinfo_[nrows() - 1].offset_;
597 h = rowinfo_[nrows() - 1].offset_ / 2;
599 for (row_type row = 0; row <= nrows(); ++row)
600 rowinfo_[row].offset_ -= h;
603 // compute absolute sizes of horizontal structure
604 for (col_type col = 0; col < ncols(); ++col) {
606 for (row_type row = 0; row < nrows(); ++row)
607 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(idx_type idx) const
800 col_type c = col(idx);
801 int x = colinfo_[c].offset_;
802 char align = colinfo_[c].align_;
803 if (align == 'r' || align == 'R')
804 x += colinfo_[c].width_ - cell(idx).width();
805 if (align == 'c' || align == 'C')
806 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
811 int InsetMathGrid::cellYOffset(idx_type idx) const
813 return rowinfo_[row(idx)].offset_;
817 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
822 cur.idx() -= ncols();
824 if (cur.row() + 1 >= nrows())
826 cur.idx() += ncols();
828 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
833 bool InsetMathGrid::idxLeft(Cursor & cur) const
835 // leave matrix if on the left hand edge
839 cur.pos() = cur.lastpos();
844 bool InsetMathGrid::idxRight(Cursor & cur) const
846 // leave matrix if on the right hand edge
847 if (cur.col() + 1 == ncols())
855 bool InsetMathGrid::idxFirst(Cursor & cur) const
862 cur.idx() = (nrows() - 1) * ncols();
865 cur.idx() = ((nrows() - 1) / 2) * ncols();
872 bool InsetMathGrid::idxLast(Cursor & cur) const
876 cur.idx() = ncols() - 1;
879 cur.idx() = nargs() - 1;
882 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
884 cur.pos() = cur.lastpos();
889 bool InsetMathGrid::idxDelete(idx_type & idx)
891 // nothing to do if we have just one row
895 // nothing to do if we are in the middle of the last row of the inset
896 if (idx + ncols() > nargs())
899 // try to delete entire sequence of ncols() empty cells if possible
900 for (idx_type i = idx; i < idx + ncols(); ++i)
904 // move cells if necessary
905 for (idx_type i = index(row(idx), 0); i < idx; ++i)
906 swap(cell(i), cell(i + ncols()));
913 // undo effect of Ctrl-Tab (i.e. pull next cell)
914 //if (idx + 1 != nargs())
915 // cell(idx).swap(cell(idx + 1));
917 // we handled the event..
922 // reimplement old behaviour when pressing Delete in the last position
924 void InsetMathGrid::idxGlue(idx_type idx)
926 col_type c = col(idx);
927 if (c + 1 == ncols()) {
928 if (row(idx) + 1 != nrows()) {
929 for (col_type cc = 0; cc < ncols(); ++cc)
930 cell(idx).append(cell(idx + cc + 1));
931 delRow(row(idx) + 1);
934 cell(idx).append(cell(idx + 1));
935 for (col_type cc = c + 2; cc < ncols(); ++cc)
936 cell(idx - c + cc - 1) = cell(idx - c + cc);
937 cell(idx - c + ncols() - 1).clear();
942 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
944 return rowinfo_[row];
948 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
950 return rowinfo_[row];
954 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
956 row_type const ri = row(idx);
957 row_type const r1 = min(row(from), row(to));
958 row_type const r2 = max(row(from), row(to));
959 col_type const ci = col(idx);
960 col_type const c1 = min(col(from), col(to));
961 col_type const c2 = max(col(from), col(to));
962 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
967 void InsetMathGrid::normalize(NormalStream & os) const
970 for (row_type row = 0; row < nrows(); ++row) {
972 for (col_type col = 0; col < ncols(); ++col)
973 os << "[cell " << cell(index(row, col)) << ']';
980 void InsetMathGrid::mathmlize(MathStream & os) const
982 os << MTag("mtable");
983 for (row_type row = 0; row < nrows(); ++row) {
985 for (col_type col = 0; col < ncols(); ++col)
986 os << cell(index(row, col));
989 os << ETag("mtable");
993 void InsetMathGrid::write(WriteStream & os) const
996 for (row_type row = 0; row < nrows(); ++row) {
997 os << verboseHLine(rowinfo_[row].lines_);
998 // don't write & and empty cells at end of line
999 col_type lastcol = 0;
1000 bool emptyline = true;
1001 for (col_type col = 0; col < ncols(); ++col)
1002 if (!cell(index(row, col)).empty()) {
1006 for (col_type col = 0; col < lastcol; ++col)
1007 os << cell(index(row, col)) << eocString(col, lastcol);
1008 eol = eolString(row, emptyline, os.fragile());
1010 // append newline only if line wasn't completely empty
1011 // and this was not the last line in the grid
1012 if (!emptyline && row + 1 < nrows())
1015 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1027 int InsetMathGrid::colsep() const
1033 int InsetMathGrid::rowsep() const
1039 int InsetMathGrid::hlinesep() const
1045 int InsetMathGrid::vlinesep() const
1051 int InsetMathGrid::border() const
1057 void InsetMathGrid::splitCell(Cursor & cur)
1059 if (cur.idx() == cur.lastidx())
1061 MathData ar = cur.cell();
1062 ar.erase(0, cur.pos());
1063 cur.cell().erase(cur.pos(), cur.lastpos());
1066 cur.cell().insert(0, ar);
1070 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1072 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1073 switch (cmd.action) {
1075 // insert file functions
1076 case LFUN_LINE_DELETE:
1077 // FIXME: We use recordUndoInset when a change reflects more
1078 // than one cell, because recordUndo does not work for
1079 // multiple cells. Unfortunately this puts the cursor in front
1080 // of the inset after undo. This is (especilally for large
1082 recordUndoInset(cur);
1083 //autocorrect_ = false;
1091 if (cur.idx() > cur.lastidx())
1092 cur.idx() = cur.lastidx();
1093 if (cur.pos() > cur.lastpos())
1094 cur.pos() = cur.lastpos();
1097 case LFUN_CELL_SPLIT:
1102 case LFUN_CELL_BACKWARD:
1104 cur.selection() = false;
1105 if (!idxPrev(cur)) {
1106 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1111 case LFUN_CELL_FORWARD:
1112 // Can't handle selection by additional 'shift' as this is
1113 // hard bound to LFUN_CELL_BACKWARD
1114 cur.selection() = false;
1115 if (!idxNext(cur)) {
1116 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1121 case LFUN_BREAK_LINE: {
1122 recordUndoInset(cur);
1123 row_type const r = cur.row();
1127 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1128 swap(cell(index(r, c)), cell(index(r + 1, c)));
1132 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1135 cur.pos() = cur.lastpos();
1137 //mathcursor->normalize();
1138 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1142 case LFUN_TABULAR_FEATURE: {
1143 recordUndoInset(cur);
1144 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1145 istringstream is(to_utf8(cmd.argument()));
1148 if (s == "valign-top")
1150 else if (s == "valign-middle")
1152 else if (s == "valign-bottom")
1154 else if (s == "align-left")
1155 halign('l', cur.col());
1156 else if (s == "align-right")
1157 halign('r', cur.col());
1158 else if (s == "align-center")
1159 halign('c', cur.col());
1160 else if (s == "append-row")
1161 for (int i = 0, n = extractInt(is); i < n; ++i)
1163 else if (s == "delete-row") {
1164 for (int i = 0, n = extractInt(is); i < n; ++i) {
1166 if (cur.idx() >= nargs())
1167 cur.idx() -= ncols();
1169 cur.pos() = 0; // trick, see below
1171 else if (s == "copy-row") {
1172 // Here (as later) we save the cursor col/row
1173 // in order to restore it after operation.
1174 row_type const r = cur.row();
1175 col_type const c = cur.col();
1176 for (int i = 0, n = extractInt(is); i < n; ++i)
1178 cur.idx() = index(r, c);
1180 else if (s == "swap-row") {
1182 // Trick to suppress same-idx-means-different-cell
1186 else if (s == "add-hline-above")
1187 rowinfo_[cur.row()].lines_++;
1188 else if (s == "add-hline-below")
1189 rowinfo_[cur.row()+1].lines_++;
1190 else if (s == "delete-hline-above")
1191 rowinfo_[cur.row()].lines_--;
1192 else if (s == "delete-hline-below")
1193 rowinfo_[cur.row()+1].lines_--;
1194 else if (s == "append-column") {
1195 row_type const r = cur.row();
1196 col_type const c = cur.col();
1197 for (int i = 0, n = extractInt(is); i < n; ++i)
1199 cur.idx() = index(r, c);
1201 else if (s == "delete-column") {
1202 row_type const r = cur.row();
1203 col_type const c = cur.col();
1204 for (int i = 0, n = extractInt(is); i < n; ++i)
1205 delCol(col(cur.idx()));
1206 cur.idx() = index(r, min(c, cur.ncols() - 1));
1207 cur.pos() = 0; // trick, see above
1209 else if (s == "copy-column") {
1210 row_type const r = cur.row();
1211 col_type const c = cur.col();
1213 cur.idx() = index(r, c);
1215 else if (s == "swap-column") {
1217 cur.pos() = 0; // trick, see above
1219 else if (s == "add-vline-left") {
1220 colinfo_[cur.col()].lines_++;
1221 if (!colinfo_[cur.col()].special_.empty())
1222 colinfo_[cur.col()].special_ += '|';
1224 else if (s == "add-vline-right") {
1225 colinfo_[cur.col()+1].lines_++;
1226 if (!colinfo_[cur.col()+1].special_.empty())
1227 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1229 else if (s == "delete-vline-left") {
1230 colinfo_[cur.col()].lines_--;
1231 docstring & special = colinfo_[cur.col()].special_;
1232 if (!special.empty()) {
1233 docstring::size_type i = special.rfind('|');
1234 BOOST_ASSERT(i != docstring::npos);
1235 special.erase(i, 1);
1238 else if (s == "delete-vline-right") {
1239 colinfo_[cur.col()+1].lines_--;
1240 docstring & special = colinfo_[cur.col()+1].special_;
1241 if (!special.empty()) {
1242 docstring::size_type i = special.find('|');
1243 BOOST_ASSERT(i != docstring::npos);
1244 special.erase(i, 1);
1251 lyxerr << "returning FINISHED_LEFT" << endl;
1256 cur.message(_("Paste"));
1257 cap::replaceSelection(cur);
1259 if (cmd.argument().empty() && !theClipboard().isInternal())
1260 topaste = theClipboard().getAsText();
1262 idocstringstream is(cmd.argument());
1265 topaste = cap::getSelection(cur.buffer(), n);
1267 InsetMathGrid grid(1, 1);
1268 if (!topaste.empty())
1269 mathed_parse_normal(grid, topaste);
1271 if (grid.nargs() == 1) {
1272 // single cell/part of cell
1274 cur.cell().insert(cur.pos(), grid.cell(0));
1275 cur.pos() += grid.cell(0).size();
1278 recordUndoInset(cur);
1279 col_type const numcols =
1280 min(grid.ncols(), ncols() - col(cur.idx()));
1281 row_type const numrows =
1282 min(grid.nrows(), nrows() - cur.row());
1283 for (row_type r = 0; r < numrows; ++r) {
1284 for (col_type c = 0; c < numcols; ++c) {
1285 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1286 cell(i).insert(0, grid.cell(grid.index(r, c)));
1288 // append the left over horizontal cells to the last column
1289 idx_type i = index(r + cur.row(), ncols() - 1);
1290 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1291 cell(i).append(grid.cell(grid.index(r, c)));
1293 // append the left over vertical cells to the last _cell_
1294 idx_type i = nargs() - 1;
1295 for (row_type r = numrows; r < grid.nrows(); ++r)
1296 for (col_type c = 0; c < grid.ncols(); ++c)
1297 cell(i).append(grid.cell(grid.index(r, c)));
1299 cur.clearSelection(); // bug 393
1304 case LFUN_LINE_BEGIN_SELECT:
1305 case LFUN_LINE_BEGIN:
1306 case LFUN_WORD_BACKWARD_SELECT:
1307 case LFUN_WORD_BACKWARD:
1308 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1309 cmd.action == LFUN_LINE_BEGIN_SELECT);
1310 cur.macroModeClose();
1311 if (cur.pos() != 0) {
1313 } else if (cur.idx() % cur.ncols() != 0) {
1314 cur.idx() -= cur.idx() % cur.ncols();
1316 } else if (cur.idx() != 0) {
1320 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1325 case LFUN_WORD_FORWARD_SELECT:
1326 case LFUN_WORD_FORWARD:
1327 case LFUN_LINE_END_SELECT:
1329 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1330 cmd.action == LFUN_LINE_END_SELECT);
1331 cur.macroModeClose();
1333 if (cur.pos() != cur.lastpos()) {
1334 cur.pos() = cur.lastpos();
1335 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1336 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1337 cur.pos() = cur.lastpos();
1338 } else if (cur.idx() != cur.lastidx()) {
1339 cur.idx() = cur.lastidx();
1340 cur.pos() = cur.lastpos();
1342 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1348 InsetMathNest::doDispatch(cur, cmd);
1353 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1354 FuncStatus & status) const
1356 switch (cmd.action) {
1357 case LFUN_TABULAR_FEATURE: {
1358 string const s = to_utf8(cmd.argument());
1359 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1360 status.enabled(false);
1361 status.message(from_utf8(N_("Only one row")));
1365 (s == "delete-column" || s == "swap-column")) {
1366 status.enabled(false);
1367 status.message(from_utf8(N_("Only one column")));
1370 if ((rowinfo_[cur.row()].lines_ == 0 &&
1371 s == "delete-hline-above") ||
1372 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1373 s == "delete-hline-below")) {
1374 status.enabled(false);
1375 status.message(from_utf8(N_("No hline to delete")));
1379 if ((colinfo_[cur.col()].lines_ == 0 &&
1380 s == "delete-vline-left") ||
1381 (colinfo_[cur.col() + 1].lines_ == 0 &&
1382 s == "delete-vline-right")) {
1383 status.enabled(false);
1384 status.message(from_utf8(N_("No vline to delete")));
1387 if (s == "valign-top" || s == "valign-middle" ||
1388 s == "valign-bottom" || s == "align-left" ||
1389 s == "align-right" || s == "align-center" ||
1390 s == "append-row" || s == "delete-row" ||
1391 s == "copy-row" || s == "swap-row" ||
1392 s == "add-hline-above" || s == "add-hline-below" ||
1393 s == "delete-hline-above" || s == "delete-hline-below" ||
1394 s == "append-column" || s == "delete-column" ||
1395 s == "copy-column" || s == "swap-column" ||
1396 s == "add-vline-left" || s == "add-vline-right" ||
1397 s == "delete-vline-left" || s == "delete-vline-right")
1398 status.enabled(true);
1400 status.enabled(false);
1401 status.message(bformat(
1402 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1405 status.setOnOff(s == "align-left" && halign(cur.col()) == 'l'
1406 || s == "align-right" && halign(cur.col()) == 'r'
1407 || s == "align-center" && halign(cur.col()) == 'c'
1408 || s == "valign-top" && valign() == 't'
1409 || s == "valign-bottom" && valign() == 'b'
1410 || s == "valign-middle" && valign() == 'm');
1413 // FIXME: What did this code do?
1414 // Please check whether it is still needed!
1415 // should be more precise
1416 if (v_align_ == '\0') {
1417 status.enable(true);
1420 if (cmd.argument().empty()) {
1421 status.enable(false);
1424 if (!support::contains("tcb", cmd.argument()[0])) {
1425 status.enable(false);
1428 status.setOnOff(cmd.argument()[0] == v_align_);
1429 status.enabled(true);
1434 case LFUN_CELL_SPLIT:
1435 status.enabled(true);
1438 case LFUN_CELL_BACKWARD:
1439 case LFUN_CELL_FORWARD:
1440 status.enabled(true);
1444 return InsetMathNest::getStatus(cur, cmd, status);