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"
16 #include "MathParser.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "FuncStatus.h"
25 #include "FuncRequest.h"
28 #include "frontends/Clipboard.h"
29 #include "frontends/Painter.h"
31 #include "support/lstrings.h"
32 #include "support/docstream.h"
39 using support::bformat;
48 using std::istringstream;
54 docstring verboseHLine(int n)
57 for (int i = 0; i < n; ++i)
65 int extractInt(istream & is)
69 return (num == 0) ? 1 : num;
75 //////////////////////////////////////////////////////////////
78 InsetMathGrid::CellInfo::CellInfo()
85 //////////////////////////////////////////////////////////////
88 InsetMathGrid::RowInfo::RowInfo()
89 : lines_(0), skip_(0), allow_pagebreak_(true)
94 int InsetMathGrid::RowInfo::skipPixels() const
96 return crskip_.inBP();
101 //////////////////////////////////////////////////////////////
104 InsetMathGrid::ColInfo::ColInfo()
105 : align_('c'), lines_(0)
109 //////////////////////////////////////////////////////////////
112 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
113 : InsetMathNest(guessColumns(h)),
115 colinfo_(guessColumns(h) + 1),
116 cellinfo_(1 * guessColumns(h))
121 //lyxerr << "created grid with " << ncols() << " columns" << endl;
125 InsetMathGrid::InsetMathGrid()
136 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
137 : InsetMathNest(m * n),
147 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
148 : InsetMathNest(m * n),
160 Inset * InsetMathGrid::clone() const
162 return new InsetMathGrid(*this);
166 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
168 return col + ncols() * row;
172 void InsetMathGrid::setDefaults()
175 lyxerr << "positive number of columns expected" << endl;
177 // lyxerr << "positive number of rows expected" << endl;
178 for (col_type col = 0; col < ncols(); ++col) {
179 colinfo_[col].align_ = defaultColAlign(col);
180 colinfo_[col].skip_ = defaultColSpace(col);
181 colinfo_[col].special_.clear();
186 void InsetMathGrid::halign(docstring const & hh)
189 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
192 colinfo_[col].lines_++;
193 } else if ((c == 'p' || c == 'm' || c == 'b'||
194 c == '!' || c == '@' || c == '>' || c == '<') &&
195 it + 1 != hh.end() && *(it + 1) == '{') {
196 // @{decl.} and p{width} are standard LaTeX, the
197 // others are extensions by array.sty
198 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
200 // this declares a new column
202 // Only intercolumn stuff is allowed
203 // in the last dummy column
205 colinfo_[col].align_ = 'l';
207 // this is intercolumn stuff
208 if (colinfo_[col].special_.empty())
209 // Overtake possible lines
210 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
214 while (it != hh.end()) {
216 colinfo_[col].special_ += c;
222 if (brace_open > 0 && brace_open == brace_close)
227 colinfo_[col].lines_ = std::count(
228 colinfo_[col].special_.begin(),
229 colinfo_[col].special_.end(), '|');
230 LYXERR(Debug::MATHED)
231 << "special column separator: `"
232 << to_utf8(colinfo_[col].special_)
235 colinfo_[col].lines_ = 0;
236 colinfo_[col].special_.clear();
238 } else if (col >= ncols()) {
239 // Only intercolumn stuff is allowed in the last
242 } else if (c == 'c' || c == 'l' || c == 'r') {
243 colinfo_[col].align_ = static_cast<char>(c);
244 if (!colinfo_[col].special_.empty()) {
245 colinfo_[col].special_ += c;
246 colinfo_[col].lines_ = std::count(
247 colinfo_[col].special_.begin(),
248 colinfo_[col].special_.end(), '|');
249 LYXERR(Debug::MATHED)
250 << "special column separator: `"
251 << to_utf8(colinfo_[col].special_)
255 colinfo_[col].lines_ = 0;
256 colinfo_[col].special_.clear();
258 lyxerr << "unknown column separator: '" << c << "'" << endl;
263 col_type n = hh.size();
266 for (col_type col = 0; col < n; ++col)
267 colinfo_[col].align_ = hh[col];
272 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
275 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
276 if (*it == 'c' || *it == 'l' || *it == 'r'||
277 *it == 'p' || *it == 'm' || *it == 'b')
279 // let's have at least one column, even if we did not recognize its
287 void InsetMathGrid::halign(char h, col_type col)
289 colinfo_[col].align_ = h;
290 if (!colinfo_[col].special_.empty()) {
291 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
292 if (c == 'l' || c == 'c' || c == 'r')
295 // FIXME: Change alignment of p, m and b columns, too
299 char InsetMathGrid::halign(col_type col) const
301 return colinfo_[col].align_;
305 docstring InsetMathGrid::halign() const
308 for (col_type col = 0; col < ncols(); ++col) {
309 if (colinfo_[col].special_.empty()) {
310 res += docstring(colinfo_[col].lines_, '|');
311 res += colinfo_[col].align_;
313 res += colinfo_[col].special_;
315 if (colinfo_[ncols()].special_.empty())
316 return res + docstring(colinfo_[ncols()].lines_, '|');
317 return res + colinfo_[ncols()].special_;
321 void InsetMathGrid::valign(char c)
327 char InsetMathGrid::valign() const
333 InsetMathGrid::col_type InsetMathGrid::ncols() const
335 return colinfo_.size() - 1;
339 InsetMathGrid::row_type InsetMathGrid::nrows() const
341 return rowinfo_.size() - 1;
345 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
347 return idx % ncols();
351 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
353 return idx / ncols();
357 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
359 rowinfo_[row].crskip_ = crskip;
363 Length InsetMathGrid::vcrskip(row_type row) const
365 return rowinfo_[row].crskip_;
369 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
371 // let the cells adjust themselves
372 InsetMathNest::metrics(mi);
374 BufferView & bv = *mi.base.bv;
376 // compute absolute sizes of vertical structure
377 for (row_type row = 0; row < nrows(); ++row) {
380 for (col_type col = 0; col < ncols(); ++col) {
381 Dimension const & dimc = cell(index(row, col)).dimension(bv);
382 asc = max(asc, dimc.asc);
383 desc = max(desc, dimc.des);
385 rowinfo_[row].ascent_ = asc;
386 rowinfo_[row].descent_ = desc;
388 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
389 rowinfo_[nrows()].ascent_ = 0;
390 rowinfo_[nrows()].descent_ = 0;
392 // compute vertical offsets
393 rowinfo_[0].offset_ = 0;
394 for (row_type row = 1; row <= nrows(); ++row) {
395 rowinfo_[row].offset_ =
396 rowinfo_[row - 1].offset_ +
397 rowinfo_[row - 1].descent_ +
398 rowinfo_[row - 1].skipPixels() +
400 rowinfo_[row].lines_ * hlinesep() +
401 rowinfo_[row].ascent_;
404 // adjust vertical offset
411 h = rowinfo_[nrows() - 1].offset_;
414 h = rowinfo_[nrows() - 1].offset_ / 2;
416 for (row_type row = 0; row <= nrows(); ++row)
417 rowinfo_[row].offset_ -= h;
420 // compute absolute sizes of horizontal structure
421 for (col_type col = 0; col < ncols(); ++col) {
423 for (row_type row = 0; row < nrows(); ++row)
424 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
425 colinfo_[col].width_ = wid;
427 colinfo_[ncols()].width_ = 0;
429 // compute horizontal offsets
430 colinfo_[0].offset_ = border();
431 for (col_type col = 1; col <= ncols(); ++col) {
432 colinfo_[col].offset_ =
433 colinfo_[col - 1].offset_ +
434 colinfo_[col - 1].width_ +
435 colinfo_[col - 1].skip_ +
437 colinfo_[col].lines_ * vlinesep();
441 dim.wid = colinfo_[ncols() - 1].offset_
442 + colinfo_[ncols() - 1].width_
443 + vlinesep() * colinfo_[ncols()].lines_
446 dim.asc = - rowinfo_[0].offset_
447 + rowinfo_[0].ascent_
448 + hlinesep() * rowinfo_[0].lines_
451 dim.des = rowinfo_[nrows() - 1].offset_
452 + rowinfo_[nrows() - 1].descent_
453 + hlinesep() * rowinfo_[nrows()].lines_
458 // Increase ws_[i] for 'R' columns (except the first one)
459 for (int i = 1; i < nc_; ++i)
460 if (align_[i] == 'R')
461 ws_[i] += 10 * df_width;
462 // Increase ws_[i] for 'C' column
463 if (align_[0] == 'C')
464 if (ws_[0] < 7 * workwidth / 8)
465 ws_[0] = 7 * workwidth / 8;
469 for (cxrow = row_.begin(); cxrow; ++cxrow) {
472 for (int i = 0; i < nc_; ++i) {
474 if (cxrow->getTab(i) <= 0) {
475 cxrow->setTab(i, df_width);
483 lf = (ws_[i] - cxrow->getTab(i))/2;
487 lf = ws_[i] - cxrow->getTab(i);
490 if (cxrow == row_.begin())
492 else if (cxrow.is_last())
493 lf = ws_[i] - cxrow->getTab(i);
495 lf = (ws_[i] - cxrow->getTab(i))/2;
498 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
499 cxrow->setTab(i, lf + rg);
500 rg = ws_[i] - ww + colsep();
501 if (cxrow == row_.begin())
502 width += ws_[i] + colsep();
504 cxrow->setBaseline(cxrow->getBaseline() - ascent);
507 metricsMarkers2(dim);
508 // Cache the inset dimension.
509 setDimCache(mi, dim);
513 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
515 drawWithMargin(pi, x, y, 0, 0);
519 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
520 int lmargin, int rmargin) const
522 Dimension const dim = dimension(*pi.base.bv);
523 BufferView const & bv = *pi.base.bv;
525 for (idx_type idx = 0; idx < nargs(); ++idx)
526 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
527 y + cellYOffset(idx));
529 for (row_type row = 0; row <= nrows(); ++row)
530 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
531 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
532 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
533 pi.pain.line(x + lmargin + 1, yy,
534 x + dim.width() - rmargin - 1, yy,
538 for (col_type col = 0; col <= ncols(); ++col)
539 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
540 int xx = x + lmargin + colinfo_[col].offset_
541 - i * vlinesep() - vlinesep()/2 - colsep()/2;
542 pi.pain.line(xx, y - dim.ascent() + 1,
543 xx, y + dim.descent() - 1,
546 drawMarkers2(pi, x, y);
550 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
552 // let the cells adjust themselves
553 //InsetMathNest::metrics(mi);
554 for (idx_type i = 0; i < nargs(); ++i)
555 cell(i).metricsT(mi, dim);
557 // compute absolute sizes of vertical structure
558 for (row_type row = 0; row < nrows(); ++row) {
561 for (col_type col = 0; col < ncols(); ++col) {
562 //MathData const & c = cell(index(row, col));
565 asc = max(asc, dimc.ascent());
566 desc = max(desc, dimc.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) {
608 //wid = max(wid, cell(index(row, col)).width());
610 colinfo_[col].width_ = wid;
612 colinfo_[ncols()].width_ = 0;
614 // compute horizontal offsets
615 colinfo_[0].offset_ = border();
616 for (col_type col = 1; col <= ncols(); ++col) {
617 colinfo_[col].offset_ =
618 colinfo_[col - 1].offset_ +
619 colinfo_[col - 1].width_ +
620 colinfo_[col - 1].skip_ +
622 //colinfo_[col].lines_ * vlinesep();
626 dim.wid = colinfo_[ncols() - 1].offset_
627 + colinfo_[ncols() - 1].width_
628 //+ vlinesep() * colinfo_[ncols()].lines_
631 dim.asc = -rowinfo_[0].offset_
632 + rowinfo_[0].ascent_
633 //+ hlinesep() * rowinfo_[0].lines_
636 dim.des = rowinfo_[nrows() - 1].offset_
637 + rowinfo_[nrows() - 1].descent_
638 //+ hlinesep() * rowinfo_[nrows()].lines_
643 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
645 // for (idx_type idx = 0; idx < nargs(); ++idx)
646 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
650 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
654 if (!rowinfo_[row].crskip_.zero())
655 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
656 else if(!rowinfo_[row].allow_pagebreak_)
659 // make sure an upcoming '[' does not break anything
660 if (row + 1 < nrows()) {
661 MathData const & c = cell(index(row + 1, 0));
662 if (c.size() && c.front()->getChar() == '[')
667 // only add \\ if necessary
668 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
671 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
675 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
677 if (col + 1 == lastcol)
679 return from_ascii(" & ");
683 void InsetMathGrid::addRow(row_type row)
685 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
687 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
689 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
693 void InsetMathGrid::appendRow()
695 rowinfo_.push_back(RowInfo());
696 //cells_.insert(cells_.end(), ncols(), MathData());
697 for (col_type col = 0; col < ncols(); ++col) {
698 cells_.push_back(cells_type::value_type());
699 cellinfo_.push_back(CellInfo());
704 void InsetMathGrid::delRow(row_type row)
709 cells_type::iterator it = cells_.begin() + row * ncols();
710 cells_.erase(it, it + ncols());
712 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
713 cellinfo_.erase(jt, jt + ncols());
715 rowinfo_.erase(rowinfo_.begin() + row);
719 void InsetMathGrid::copyRow(row_type row)
722 for (col_type col = 0; col < ncols(); ++col)
723 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
727 void InsetMathGrid::swapRow(row_type row)
731 if (row + 1 == nrows())
733 for (col_type col = 0; col < ncols(); ++col)
734 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
738 void InsetMathGrid::addCol(col_type newcol)
740 const col_type nc = ncols();
741 const row_type nr = nrows();
742 cells_type new_cells((nc + 1) * nr);
743 vector<CellInfo> new_cellinfo((nc + 1) * nr);
745 for (row_type row = 0; row < nr; ++row)
746 for (col_type col = 0; col < nc; ++col) {
747 new_cells[row * (nc + 1) + col + (col >= newcol)]
748 = cells_[row * nc + col];
749 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
750 = cellinfo_[row * nc + col];
752 swap(cells_, new_cells);
753 swap(cellinfo_, new_cellinfo);
756 inf.skip_ = defaultColSpace(newcol);
757 inf.align_ = defaultColAlign(newcol);
758 colinfo_.insert(colinfo_.begin() + newcol, inf);
762 void InsetMathGrid::delCol(col_type col)
768 vector<CellInfo> tmpcellinfo;
769 for (col_type i = 0; i < nargs(); ++i)
770 if (i % ncols() != col) {
771 tmpcells.push_back(cells_[i]);
772 tmpcellinfo.push_back(cellinfo_[i]);
774 swap(cells_, tmpcells);
775 swap(cellinfo_, tmpcellinfo);
777 colinfo_.erase(colinfo_.begin() + col);
781 void InsetMathGrid::copyCol(col_type col)
784 for (row_type row = 0; row < nrows(); ++row)
785 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
789 void InsetMathGrid::swapCol(col_type col)
793 if (col + 1 == ncols())
795 for (row_type row = 0; row < nrows(); ++row)
796 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
800 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
802 col_type c = col(idx);
803 int x = colinfo_[c].offset_;
804 char align = colinfo_[c].align_;
805 Dimension const & celldim = cell(idx).dimension(bv);
806 if (align == 'r' || align == 'R')
807 x += colinfo_[c].width_ - celldim.wid;
808 if (align == 'c' || align == 'C')
809 x += (colinfo_[c].width_ - celldim.wid) / 2;
814 int InsetMathGrid::cellYOffset(idx_type idx) const
816 return rowinfo_[row(idx)].offset_;
820 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
825 cur.idx() -= ncols();
827 if (cur.row() + 1 >= nrows())
829 cur.idx() += ncols();
831 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
836 bool InsetMathGrid::idxBackward(Cursor & cur) const
838 // leave matrix if at the front edge
842 cur.pos() = cur.lastpos();
847 bool InsetMathGrid::idxForward(Cursor & cur) const
849 // leave matrix if at the back edge
850 if (cur.col() + 1 == ncols())
858 bool InsetMathGrid::idxFirst(Cursor & cur) const
865 cur.idx() = (nrows() - 1) * ncols();
868 cur.idx() = ((nrows() - 1) / 2) * ncols();
875 bool InsetMathGrid::idxLast(Cursor & cur) const
879 cur.idx() = ncols() - 1;
882 cur.idx() = nargs() - 1;
885 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
887 cur.pos() = cur.lastpos();
892 bool InsetMathGrid::idxDelete(idx_type & idx)
894 // nothing to do if we have just one row
898 // nothing to do if we are in the middle of the last row of the inset
899 if (idx + ncols() > nargs())
902 // try to delete entire sequence of ncols() empty cells if possible
903 for (idx_type i = idx; i < idx + ncols(); ++i)
907 // move cells if necessary
908 for (idx_type i = index(row(idx), 0); i < idx; ++i)
909 swap(cell(i), cell(i + ncols()));
916 // undo effect of Ctrl-Tab (i.e. pull next cell)
917 //if (idx + 1 != nargs())
918 // cell(idx).swap(cell(idx + 1));
920 // we handled the event..
925 // reimplement old behaviour when pressing Delete in the last position
927 void InsetMathGrid::idxGlue(idx_type idx)
929 col_type c = col(idx);
930 if (c + 1 == ncols()) {
931 if (row(idx) + 1 != nrows()) {
932 for (col_type cc = 0; cc < ncols(); ++cc)
933 cell(idx).append(cell(idx + cc + 1));
934 delRow(row(idx) + 1);
937 cell(idx).append(cell(idx + 1));
938 for (col_type cc = c + 2; cc < ncols(); ++cc)
939 cell(idx - c + cc - 1) = cell(idx - c + cc);
940 cell(idx - c + ncols() - 1).clear();
945 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
947 return rowinfo_[row];
951 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
953 return rowinfo_[row];
957 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
959 row_type const ri = row(idx);
960 row_type const r1 = min(row(from), row(to));
961 row_type const r2 = max(row(from), row(to));
962 col_type const ci = col(idx);
963 col_type const c1 = min(col(from), col(to));
964 col_type const c2 = max(col(from), col(to));
965 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
970 void InsetMathGrid::normalize(NormalStream & os) const
973 for (row_type row = 0; row < nrows(); ++row) {
975 for (col_type col = 0; col < ncols(); ++col)
976 os << "[cell " << cell(index(row, col)) << ']';
983 void InsetMathGrid::mathmlize(MathStream & os) const
985 os << MTag("mtable");
986 for (row_type row = 0; row < nrows(); ++row) {
988 for (col_type col = 0; col < ncols(); ++col)
989 os << cell(index(row, col));
992 os << ETag("mtable");
996 void InsetMathGrid::write(WriteStream & os) const
999 for (row_type row = 0; row < nrows(); ++row) {
1000 os << verboseHLine(rowinfo_[row].lines_);
1001 // don't write & and empty cells at end of line
1002 col_type lastcol = 0;
1003 bool emptyline = true;
1004 for (col_type col = 0; col < ncols(); ++col)
1005 if (!cell(index(row, col)).empty()) {
1009 for (col_type col = 0; col < lastcol; ++col)
1010 os << cell(index(row, col)) << eocString(col, lastcol);
1011 eol = eolString(row, emptyline, os.fragile());
1013 // append newline only if line wasn't completely empty
1014 // and this was not the last line in the grid
1015 if (!emptyline && row + 1 < nrows())
1018 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1030 int InsetMathGrid::colsep() const
1036 int InsetMathGrid::rowsep() const
1042 int InsetMathGrid::hlinesep() const
1048 int InsetMathGrid::vlinesep() const
1054 int InsetMathGrid::border() const
1060 void InsetMathGrid::splitCell(Cursor & cur)
1062 if (cur.idx() == cur.lastidx())
1064 MathData ar = cur.cell();
1065 ar.erase(0, cur.pos());
1066 cur.cell().erase(cur.pos(), cur.lastpos());
1069 cur.cell().insert(0, ar);
1073 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1075 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1076 switch (cmd.action) {
1078 // insert file functions
1079 case LFUN_LINE_DELETE:
1080 // FIXME: We use recordUndoInset when a change reflects more
1081 // than one cell, because recordUndo does not work for
1082 // multiple cells. Unfortunately this puts the cursor in front
1083 // of the inset after undo. This is (especilally for large
1085 cur.recordUndoInset();
1086 //autocorrect_ = false;
1094 if (cur.idx() > cur.lastidx())
1095 cur.idx() = cur.lastidx();
1096 if (cur.pos() > cur.lastpos())
1097 cur.pos() = cur.lastpos();
1100 case LFUN_CELL_SPLIT:
1105 case LFUN_CELL_BACKWARD:
1107 cur.selection() = false;
1108 if (!idxPrev(cur)) {
1109 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1114 case LFUN_CELL_FORWARD:
1115 // Can't handle selection by additional 'shift' as this is
1116 // hard bound to LFUN_CELL_BACKWARD
1117 cur.selection() = false;
1118 if (!idxNext(cur)) {
1119 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1124 case LFUN_BREAK_LINE: {
1125 cur.recordUndoInset();
1126 row_type const r = cur.row();
1130 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1131 swap(cell(index(r, c)), cell(index(r + 1, c)));
1135 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1138 cur.pos() = cur.lastpos();
1140 //mathcursor->normalize();
1141 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1145 case LFUN_TABULAR_FEATURE: {
1146 cur.recordUndoInset();
1147 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1148 istringstream is(to_utf8(cmd.argument()));
1151 if (s == "valign-top")
1153 else if (s == "valign-middle")
1155 else if (s == "valign-bottom")
1157 else if (s == "align-left")
1158 halign('l', cur.col());
1159 else if (s == "align-right")
1160 halign('r', cur.col());
1161 else if (s == "align-center")
1162 halign('c', cur.col());
1163 else if (s == "append-row")
1164 for (int i = 0, n = extractInt(is); i < n; ++i)
1166 else if (s == "delete-row") {
1167 cur.clearSelection(); // bug 4323
1168 for (int i = 0, n = extractInt(is); i < n; ++i) {
1170 if (cur.idx() >= nargs())
1171 cur.idx() -= ncols();
1173 cur.pos() = 0; // trick, see below
1175 else if (s == "copy-row") {
1176 // Here (as later) we save the cursor col/row
1177 // in order to restore it after operation.
1178 row_type const r = cur.row();
1179 col_type const c = cur.col();
1180 for (int i = 0, n = extractInt(is); i < n; ++i)
1182 cur.idx() = index(r, c);
1184 else if (s == "swap-row") {
1186 // Trick to suppress same-idx-means-different-cell
1190 else if (s == "add-hline-above")
1191 rowinfo_[cur.row()].lines_++;
1192 else if (s == "add-hline-below")
1193 rowinfo_[cur.row()+1].lines_++;
1194 else if (s == "delete-hline-above")
1195 rowinfo_[cur.row()].lines_--;
1196 else if (s == "delete-hline-below")
1197 rowinfo_[cur.row()+1].lines_--;
1198 else if (s == "append-column") {
1199 row_type const r = cur.row();
1200 col_type const c = cur.col();
1201 for (int i = 0, n = extractInt(is); i < n; ++i)
1202 addCol(cur.col() + 1);
1203 cur.idx() = index(r, c);
1205 else if (s == "delete-column") {
1206 cur.clearSelection(); // bug 4323
1207 row_type const r = cur.row();
1208 col_type const c = cur.col();
1209 for (int i = 0, n = extractInt(is); i < n; ++i)
1210 delCol(col(cur.idx()));
1211 cur.idx() = index(r, min(c, cur.ncols() - 1));
1212 cur.pos() = 0; // trick, see above
1214 else if (s == "copy-column") {
1215 row_type const r = cur.row();
1216 col_type const c = cur.col();
1218 cur.idx() = index(r, c);
1220 else if (s == "swap-column") {
1222 cur.pos() = 0; // trick, see above
1224 else if (s == "add-vline-left") {
1225 colinfo_[cur.col()].lines_++;
1226 if (!colinfo_[cur.col()].special_.empty())
1227 colinfo_[cur.col()].special_ += '|';
1229 else if (s == "add-vline-right") {
1230 colinfo_[cur.col()+1].lines_++;
1231 if (!colinfo_[cur.col()+1].special_.empty())
1232 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1234 else if (s == "delete-vline-left") {
1235 colinfo_[cur.col()].lines_--;
1236 docstring & special = colinfo_[cur.col()].special_;
1237 if (!special.empty()) {
1238 docstring::size_type i = special.rfind('|');
1239 BOOST_ASSERT(i != docstring::npos);
1240 special.erase(i, 1);
1243 else if (s == "delete-vline-right") {
1244 colinfo_[cur.col()+1].lines_--;
1245 docstring & special = colinfo_[cur.col()+1].special_;
1246 if (!special.empty()) {
1247 docstring::size_type i = special.find('|');
1248 BOOST_ASSERT(i != docstring::npos);
1249 special.erase(i, 1);
1256 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1257 lyxerr << "returning FINISHED_LEFT" << endl;
1262 cur.message(_("Paste"));
1263 cap::replaceSelection(cur);
1265 if (cmd.argument().empty() && !theClipboard().isInternal())
1266 topaste = theClipboard().getAsText();
1268 idocstringstream is(cmd.argument());
1271 topaste = cap::getSelection(cur.buffer(), n);
1273 InsetMathGrid grid(1, 1);
1274 if (!topaste.empty())
1275 mathed_parse_normal(grid, topaste);
1277 if (grid.nargs() == 1) {
1278 // single cell/part of cell
1280 cur.cell().insert(cur.pos(), grid.cell(0));
1281 cur.pos() += grid.cell(0).size();
1284 cur.recordUndoInset();
1285 col_type const numcols =
1286 min(grid.ncols(), ncols() - col(cur.idx()));
1287 row_type const numrows =
1288 min(grid.nrows(), nrows() - cur.row());
1289 for (row_type r = 0; r < numrows; ++r) {
1290 for (col_type c = 0; c < numcols; ++c) {
1291 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1292 cell(i).insert(0, grid.cell(grid.index(r, c)));
1294 // append the left over horizontal cells to the last column
1295 idx_type i = index(r + cur.row(), ncols() - 1);
1296 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1297 cell(i).append(grid.cell(grid.index(r, c)));
1299 // append the left over vertical cells to the last _cell_
1300 idx_type i = nargs() - 1;
1301 for (row_type r = numrows; r < grid.nrows(); ++r)
1302 for (col_type c = 0; c < grid.ncols(); ++c)
1303 cell(i).append(grid.cell(grid.index(r, c)));
1305 cur.clearSelection(); // bug 393
1310 case LFUN_LINE_BEGIN_SELECT:
1311 case LFUN_LINE_BEGIN:
1312 case LFUN_WORD_BACKWARD_SELECT:
1313 case LFUN_WORD_BACKWARD:
1314 case LFUN_WORD_LEFT_SELECT:
1315 case LFUN_WORD_LEFT:
1316 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1317 cmd.action == LFUN_WORD_LEFT_SELECT ||
1318 cmd.action == LFUN_LINE_BEGIN_SELECT);
1319 cur.macroModeClose();
1320 if (cur.pos() != 0) {
1322 } else if (cur.idx() % cur.ncols() != 0) {
1323 cur.idx() -= cur.idx() % cur.ncols();
1325 } else if (cur.idx() != 0) {
1329 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1334 case LFUN_WORD_FORWARD_SELECT:
1335 case LFUN_WORD_FORWARD:
1336 case LFUN_WORD_RIGHT_SELECT:
1337 case LFUN_WORD_RIGHT:
1338 case LFUN_LINE_END_SELECT:
1340 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1341 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1342 cmd.action == LFUN_LINE_END_SELECT);
1343 cur.macroModeClose();
1345 if (cur.pos() != cur.lastpos()) {
1346 cur.pos() = cur.lastpos();
1347 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1348 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1349 cur.pos() = cur.lastpos();
1350 } else if (cur.idx() != cur.lastidx()) {
1351 cur.idx() = cur.lastidx();
1352 cur.pos() = cur.lastpos();
1354 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1360 InsetMathNest::doDispatch(cur, cmd);
1365 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1366 FuncStatus & status) const
1368 switch (cmd.action) {
1369 case LFUN_TABULAR_FEATURE: {
1370 string const s = to_utf8(cmd.argument());
1371 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1372 status.enabled(false);
1373 status.message(from_utf8(N_("Only one row")));
1377 (s == "delete-column" || s == "swap-column")) {
1378 status.enabled(false);
1379 status.message(from_utf8(N_("Only one column")));
1382 if ((rowinfo_[cur.row()].lines_ == 0 &&
1383 s == "delete-hline-above") ||
1384 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1385 s == "delete-hline-below")) {
1386 status.enabled(false);
1387 status.message(from_utf8(N_("No hline to delete")));
1391 if ((colinfo_[cur.col()].lines_ == 0 &&
1392 s == "delete-vline-left") ||
1393 (colinfo_[cur.col() + 1].lines_ == 0 &&
1394 s == "delete-vline-right")) {
1395 status.enabled(false);
1396 status.message(from_utf8(N_("No vline to delete")));
1399 if (s == "valign-top" || s == "valign-middle" ||
1400 s == "valign-bottom" || s == "align-left" ||
1401 s == "align-right" || s == "align-center" ||
1402 s == "append-row" || s == "delete-row" ||
1403 s == "copy-row" || s == "swap-row" ||
1404 s == "add-hline-above" || s == "add-hline-below" ||
1405 s == "delete-hline-above" || s == "delete-hline-below" ||
1406 s == "append-column" || s == "delete-column" ||
1407 s == "copy-column" || s == "swap-column" ||
1408 s == "add-vline-left" || s == "add-vline-right" ||
1409 s == "delete-vline-left" || s == "delete-vline-right")
1410 status.enabled(true);
1412 status.enabled(false);
1413 status.message(bformat(
1414 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1417 status.setOnOff((s == "align-left" && halign(cur.col()) == 'l')
1418 || (s == "align-right" && halign(cur.col()) == 'r')
1419 || (s == "align-center" && halign(cur.col()) == 'c')
1420 || (s == "valign-top" && valign() == 't')
1421 || (s == "valign-bottom" && valign() == 'b')
1422 || (s == "valign-middle" && valign() == 'm'));
1425 // FIXME: What did this code do?
1426 // Please check whether it is still needed!
1427 // should be more precise
1428 if (v_align_ == '\0') {
1429 status.enable(true);
1432 if (cmd.argument().empty()) {
1433 status.enable(false);
1436 if (!support::contains("tcb", cmd.argument()[0])) {
1437 status.enable(false);
1440 status.setOnOff(cmd.argument()[0] == v_align_);
1441 status.enabled(true);
1446 case LFUN_CELL_SPLIT:
1447 status.enabled(true);
1450 case LFUN_CELL_BACKWARD:
1451 case LFUN_CELL_FORWARD:
1452 status.enabled(true);
1456 return InsetMathNest::getStatus(cur, cmd, status);