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_newpage_(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, "special column separator: `"
231 << 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, "special column separator: `"
248 << to_utf8(colinfo_[col].special_) << '\'');
251 colinfo_[col].lines_ = 0;
252 colinfo_[col].special_.clear();
254 lyxerr << "unknown column separator: '" << c << "'" << endl;
259 col_type n = hh.size();
262 for (col_type col = 0; col < n; ++col)
263 colinfo_[col].align_ = hh[col];
268 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
271 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
272 if (*it == 'c' || *it == 'l' || *it == 'r'||
273 *it == 'p' || *it == 'm' || *it == 'b')
275 // let's have at least one column, even if we did not recognize its
283 void InsetMathGrid::halign(char h, col_type col)
285 colinfo_[col].align_ = h;
286 if (!colinfo_[col].special_.empty()) {
287 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
288 if (c == 'l' || c == 'c' || c == 'r')
291 // FIXME: Change alignment of p, m and b columns, too
295 char InsetMathGrid::halign(col_type col) const
297 return colinfo_[col].align_;
301 docstring InsetMathGrid::halign() const
304 for (col_type col = 0; col < ncols(); ++col) {
305 if (colinfo_[col].special_.empty()) {
306 res += docstring(colinfo_[col].lines_, '|');
307 res += colinfo_[col].align_;
309 res += colinfo_[col].special_;
311 if (colinfo_[ncols()].special_.empty())
312 return res + docstring(colinfo_[ncols()].lines_, '|');
313 return res + colinfo_[ncols()].special_;
317 void InsetMathGrid::valign(char c)
323 char InsetMathGrid::valign() const
329 InsetMathGrid::col_type InsetMathGrid::ncols() const
331 return colinfo_.size() - 1;
335 InsetMathGrid::row_type InsetMathGrid::nrows() const
337 return rowinfo_.size() - 1;
341 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
343 return idx % ncols();
347 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
349 return idx / ncols();
353 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
355 rowinfo_[row].crskip_ = crskip;
359 Length InsetMathGrid::vcrskip(row_type row) const
361 return rowinfo_[row].crskip_;
365 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
367 // let the cells adjust themselves
368 InsetMathNest::metrics(mi);
370 BufferView & bv = *mi.base.bv;
372 // compute absolute sizes of vertical structure
373 for (row_type row = 0; row < nrows(); ++row) {
376 for (col_type col = 0; col < ncols(); ++col) {
377 Dimension const & dimc = cell(index(row, col)).dimension(bv);
378 asc = max(asc, dimc.asc);
379 desc = max(desc, dimc.des);
381 rowinfo_[row].ascent_ = asc;
382 rowinfo_[row].descent_ = desc;
384 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
385 rowinfo_[nrows()].ascent_ = 0;
386 rowinfo_[nrows()].descent_ = 0;
388 // compute vertical offsets
389 rowinfo_[0].offset_ = 0;
390 for (row_type row = 1; row <= nrows(); ++row) {
391 rowinfo_[row].offset_ =
392 rowinfo_[row - 1].offset_ +
393 rowinfo_[row - 1].descent_ +
394 rowinfo_[row - 1].skipPixels() +
396 rowinfo_[row].lines_ * hlinesep() +
397 rowinfo_[row].ascent_;
400 // adjust vertical offset
407 h = rowinfo_[nrows() - 1].offset_;
410 h = rowinfo_[nrows() - 1].offset_ / 2;
412 for (row_type row = 0; row <= nrows(); ++row)
413 rowinfo_[row].offset_ -= h;
416 // compute absolute sizes of horizontal structure
417 for (col_type col = 0; col < ncols(); ++col) {
419 for (row_type row = 0; row < nrows(); ++row)
420 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
421 colinfo_[col].width_ = wid;
423 colinfo_[ncols()].width_ = 0;
425 // compute horizontal offsets
426 colinfo_[0].offset_ = border();
427 for (col_type col = 1; col <= ncols(); ++col) {
428 colinfo_[col].offset_ =
429 colinfo_[col - 1].offset_ +
430 colinfo_[col - 1].width_ +
431 colinfo_[col - 1].skip_ +
433 colinfo_[col].lines_ * vlinesep();
437 dim.wid = colinfo_[ncols() - 1].offset_
438 + colinfo_[ncols() - 1].width_
439 + vlinesep() * colinfo_[ncols()].lines_
442 dim.asc = - rowinfo_[0].offset_
443 + rowinfo_[0].ascent_
444 + hlinesep() * rowinfo_[0].lines_
447 dim.des = rowinfo_[nrows() - 1].offset_
448 + rowinfo_[nrows() - 1].descent_
449 + hlinesep() * rowinfo_[nrows()].lines_
454 // Increase ws_[i] for 'R' columns (except the first one)
455 for (int i = 1; i < nc_; ++i)
456 if (align_[i] == 'R')
457 ws_[i] += 10 * df_width;
458 // Increase ws_[i] for 'C' column
459 if (align_[0] == 'C')
460 if (ws_[0] < 7 * workwidth / 8)
461 ws_[0] = 7 * workwidth / 8;
465 for (cxrow = row_.begin(); cxrow; ++cxrow) {
468 for (int i = 0; i < nc_; ++i) {
470 if (cxrow->getTab(i) <= 0) {
471 cxrow->setTab(i, df_width);
479 lf = (ws_[i] - cxrow->getTab(i))/2;
483 lf = ws_[i] - cxrow->getTab(i);
486 if (cxrow == row_.begin())
488 else if (cxrow.is_last())
489 lf = ws_[i] - cxrow->getTab(i);
491 lf = (ws_[i] - cxrow->getTab(i))/2;
494 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
495 cxrow->setTab(i, lf + rg);
496 rg = ws_[i] - ww + colsep();
497 if (cxrow == row_.begin())
498 width += ws_[i] + colsep();
500 cxrow->setBaseline(cxrow->getBaseline() - ascent);
503 metricsMarkers2(dim);
504 // Cache the inset dimension.
505 setDimCache(mi, dim);
509 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
511 drawWithMargin(pi, x, y, 0, 0);
515 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
516 int lmargin, int rmargin) const
518 Dimension const dim = dimension(*pi.base.bv);
519 BufferView const & bv = *pi.base.bv;
521 for (idx_type idx = 0; idx < nargs(); ++idx)
522 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
523 y + cellYOffset(idx));
525 for (row_type row = 0; row <= nrows(); ++row)
526 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
527 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
528 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
529 pi.pain.line(x + lmargin + 1, yy,
530 x + dim.width() - rmargin - 1, yy,
534 for (col_type col = 0; col <= ncols(); ++col)
535 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
536 int xx = x + lmargin + colinfo_[col].offset_
537 - i * vlinesep() - vlinesep()/2 - colsep()/2;
538 pi.pain.line(xx, y - dim.ascent() + 1,
539 xx, y + dim.descent() - 1,
542 drawMarkers2(pi, x, y);
546 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
548 // let the cells adjust themselves
549 //InsetMathNest::metrics(mi);
550 for (idx_type i = 0; i < nargs(); ++i)
551 cell(i).metricsT(mi, dim);
553 // compute absolute sizes of vertical structure
554 for (row_type row = 0; row < nrows(); ++row) {
557 for (col_type col = 0; col < ncols(); ++col) {
558 //MathData const & c = cell(index(row, col));
561 asc = max(asc, dimc.ascent());
562 desc = max(desc, dimc.descent());
564 rowinfo_[row].ascent_ = asc;
565 rowinfo_[row].descent_ = desc;
567 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
568 rowinfo_[nrows()].ascent_ = 0;
569 rowinfo_[nrows()].descent_ = 0;
571 // compute vertical offsets
572 rowinfo_[0].offset_ = 0;
573 for (row_type row = 1; row <= nrows(); ++row) {
574 rowinfo_[row].offset_ =
575 rowinfo_[row - 1].offset_ +
576 rowinfo_[row - 1].descent_ +
577 //rowinfo_[row - 1].skipPixels() +
579 //rowinfo_[row].lines_ * hlinesep() +
580 rowinfo_[row].ascent_;
583 // adjust vertical offset
590 h = rowinfo_[nrows() - 1].offset_;
593 h = rowinfo_[nrows() - 1].offset_ / 2;
595 for (row_type row = 0; row <= nrows(); ++row)
596 rowinfo_[row].offset_ -= h;
599 // compute absolute sizes of horizontal structure
600 for (col_type col = 0; col < ncols(); ++col) {
602 for (row_type row = 0; row < nrows(); ++row) {
604 //wid = max(wid, cell(index(row, col)).width());
606 colinfo_[col].width_ = wid;
608 colinfo_[ncols()].width_ = 0;
610 // compute horizontal offsets
611 colinfo_[0].offset_ = border();
612 for (col_type col = 1; col <= ncols(); ++col) {
613 colinfo_[col].offset_ =
614 colinfo_[col - 1].offset_ +
615 colinfo_[col - 1].width_ +
616 colinfo_[col - 1].skip_ +
618 //colinfo_[col].lines_ * vlinesep();
622 dim.wid = colinfo_[ncols() - 1].offset_
623 + colinfo_[ncols() - 1].width_
624 //+ vlinesep() * colinfo_[ncols()].lines_
627 dim.asc = -rowinfo_[0].offset_
628 + rowinfo_[0].ascent_
629 //+ hlinesep() * rowinfo_[0].lines_
632 dim.des = rowinfo_[nrows() - 1].offset_
633 + rowinfo_[nrows() - 1].descent_
634 //+ hlinesep() * rowinfo_[nrows()].lines_
639 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
641 // for (idx_type idx = 0; idx < nargs(); ++idx)
642 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
646 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
650 if (!rowinfo_[row].crskip_.zero())
651 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
652 else if(!rowinfo_[row].allow_newpage_)
655 // make sure an upcoming '[' does not break anything
656 if (row + 1 < nrows()) {
657 MathData const & c = cell(index(row + 1, 0));
658 if (c.size() && c.front()->getChar() == '[')
663 // only add \\ if necessary
664 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
667 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
671 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
673 if (col + 1 == lastcol)
675 return from_ascii(" & ");
679 void InsetMathGrid::addRow(row_type row)
681 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
683 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
685 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
689 void InsetMathGrid::appendRow()
691 rowinfo_.push_back(RowInfo());
692 //cells_.insert(cells_.end(), ncols(), MathData());
693 for (col_type col = 0; col < ncols(); ++col) {
694 cells_.push_back(cells_type::value_type());
695 cellinfo_.push_back(CellInfo());
700 void InsetMathGrid::delRow(row_type row)
705 cells_type::iterator it = cells_.begin() + row * ncols();
706 cells_.erase(it, it + ncols());
708 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
709 cellinfo_.erase(jt, jt + ncols());
711 rowinfo_.erase(rowinfo_.begin() + row);
715 void InsetMathGrid::copyRow(row_type row)
718 for (col_type col = 0; col < ncols(); ++col)
719 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
723 void InsetMathGrid::swapRow(row_type row)
727 if (row + 1 == nrows())
729 for (col_type col = 0; col < ncols(); ++col)
730 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
734 void InsetMathGrid::addCol(col_type newcol)
736 const col_type nc = ncols();
737 const row_type nr = nrows();
738 cells_type new_cells((nc + 1) * nr);
739 vector<CellInfo> new_cellinfo((nc + 1) * nr);
741 for (row_type row = 0; row < nr; ++row)
742 for (col_type col = 0; col < nc; ++col) {
743 new_cells[row * (nc + 1) + col + (col >= newcol)]
744 = cells_[row * nc + col];
745 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
746 = cellinfo_[row * nc + col];
748 swap(cells_, new_cells);
749 swap(cellinfo_, new_cellinfo);
752 inf.skip_ = defaultColSpace(newcol);
753 inf.align_ = defaultColAlign(newcol);
754 colinfo_.insert(colinfo_.begin() + newcol, inf);
758 void InsetMathGrid::delCol(col_type col)
764 vector<CellInfo> tmpcellinfo;
765 for (col_type i = 0; i < nargs(); ++i)
766 if (i % ncols() != col) {
767 tmpcells.push_back(cells_[i]);
768 tmpcellinfo.push_back(cellinfo_[i]);
770 swap(cells_, tmpcells);
771 swap(cellinfo_, tmpcellinfo);
773 colinfo_.erase(colinfo_.begin() + col);
777 void InsetMathGrid::copyCol(col_type col)
780 for (row_type row = 0; row < nrows(); ++row)
781 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
785 void InsetMathGrid::swapCol(col_type col)
789 if (col + 1 == ncols())
791 for (row_type row = 0; row < nrows(); ++row)
792 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
796 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
798 col_type c = col(idx);
799 int x = colinfo_[c].offset_;
800 char align = colinfo_[c].align_;
801 Dimension const & celldim = cell(idx).dimension(bv);
802 if (align == 'r' || align == 'R')
803 x += colinfo_[c].width_ - celldim.wid;
804 if (align == 'c' || align == 'C')
805 x += (colinfo_[c].width_ - celldim.wid) / 2;
810 int InsetMathGrid::cellYOffset(idx_type idx) const
812 return rowinfo_[row(idx)].offset_;
816 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
821 cur.idx() -= ncols();
823 if (cur.row() + 1 >= nrows())
825 cur.idx() += ncols();
827 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
832 bool InsetMathGrid::idxBackward(Cursor & cur) const
834 // leave matrix if at the front edge
838 cur.pos() = cur.lastpos();
843 bool InsetMathGrid::idxForward(Cursor & cur) const
845 // leave matrix if at the back edge
846 if (cur.col() + 1 == ncols())
854 bool InsetMathGrid::idxFirst(Cursor & cur) const
861 cur.idx() = (nrows() - 1) * ncols();
864 cur.idx() = ((nrows() - 1) / 2) * ncols();
871 bool InsetMathGrid::idxLast(Cursor & cur) const
875 cur.idx() = ncols() - 1;
878 cur.idx() = nargs() - 1;
881 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
883 cur.pos() = cur.lastpos();
888 bool InsetMathGrid::idxDelete(idx_type & idx)
890 // nothing to do if we have just one row
894 // nothing to do if we are in the middle of the last row of the inset
895 if (idx + ncols() > nargs())
898 // try to delete entire sequence of ncols() empty cells if possible
899 for (idx_type i = idx; i < idx + ncols(); ++i)
903 // move cells if necessary
904 for (idx_type i = index(row(idx), 0); i < idx; ++i)
905 swap(cell(i), cell(i + ncols()));
912 // undo effect of Ctrl-Tab (i.e. pull next cell)
913 //if (idx + 1 != nargs())
914 // cell(idx).swap(cell(idx + 1));
916 // we handled the event..
921 // reimplement old behaviour when pressing Delete in the last position
923 void InsetMathGrid::idxGlue(idx_type idx)
925 col_type c = col(idx);
926 if (c + 1 == ncols()) {
927 if (row(idx) + 1 != nrows()) {
928 for (col_type cc = 0; cc < ncols(); ++cc)
929 cell(idx).append(cell(idx + cc + 1));
930 delRow(row(idx) + 1);
933 cell(idx).append(cell(idx + 1));
934 for (col_type cc = c + 2; cc < ncols(); ++cc)
935 cell(idx - c + cc - 1) = cell(idx - c + cc);
936 cell(idx - c + ncols() - 1).clear();
941 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
943 return rowinfo_[row];
947 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
949 return rowinfo_[row];
953 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
955 row_type const ri = row(idx);
956 row_type const r1 = min(row(from), row(to));
957 row_type const r2 = max(row(from), row(to));
958 col_type const ci = col(idx);
959 col_type const c1 = min(col(from), col(to));
960 col_type const c2 = max(col(from), col(to));
961 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
966 void InsetMathGrid::normalize(NormalStream & os) const
969 for (row_type row = 0; row < nrows(); ++row) {
971 for (col_type col = 0; col < ncols(); ++col)
972 os << "[cell " << cell(index(row, col)) << ']';
979 void InsetMathGrid::mathmlize(MathStream & os) const
981 os << MTag("mtable");
982 for (row_type row = 0; row < nrows(); ++row) {
984 for (col_type col = 0; col < ncols(); ++col)
985 os << cell(index(row, col));
988 os << ETag("mtable");
992 void InsetMathGrid::write(WriteStream & os) const
995 for (row_type row = 0; row < nrows(); ++row) {
996 os << verboseHLine(rowinfo_[row].lines_);
997 // don't write & and empty cells at end of line
998 col_type lastcol = 0;
999 bool emptyline = true;
1000 for (col_type col = 0; col < ncols(); ++col)
1001 if (!cell(index(row, col)).empty()) {
1005 for (col_type col = 0; col < lastcol; ++col)
1006 os << cell(index(row, col)) << eocString(col, lastcol);
1007 eol = eolString(row, emptyline, os.fragile());
1009 // append newline only if line wasn't completely empty
1010 // and this was not the last line in the grid
1011 if (!emptyline && row + 1 < nrows())
1014 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1026 int InsetMathGrid::colsep() const
1032 int InsetMathGrid::rowsep() const
1038 int InsetMathGrid::hlinesep() const
1044 int InsetMathGrid::vlinesep() const
1050 int InsetMathGrid::border() const
1056 void InsetMathGrid::splitCell(Cursor & cur)
1058 if (cur.idx() == cur.lastidx())
1060 MathData ar = cur.cell();
1061 ar.erase(0, cur.pos());
1062 cur.cell().erase(cur.pos(), cur.lastpos());
1065 cur.cell().insert(0, ar);
1069 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1071 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1072 switch (cmd.action) {
1074 // insert file functions
1075 case LFUN_LINE_DELETE:
1076 // FIXME: We use recordUndoInset when a change reflects more
1077 // than one cell, because recordUndo does not work for
1078 // multiple cells. Unfortunately this puts the cursor in front
1079 // of the inset after undo. This is (especilally for large
1081 cur.recordUndoInset();
1082 //autocorrect_ = false;
1090 if (cur.idx() > cur.lastidx())
1091 cur.idx() = cur.lastidx();
1092 if (cur.pos() > cur.lastpos())
1093 cur.pos() = cur.lastpos();
1096 case LFUN_CELL_SPLIT:
1101 case LFUN_CELL_BACKWARD:
1103 cur.selection() = false;
1104 if (!idxPrev(cur)) {
1105 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1110 case LFUN_CELL_FORWARD:
1111 // Can't handle selection by additional 'shift' as this is
1112 // hard bound to LFUN_CELL_BACKWARD
1113 cur.selection() = false;
1114 if (!idxNext(cur)) {
1115 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1120 case LFUN_BREAK_LINE: {
1121 cur.recordUndoInset();
1122 row_type const r = cur.row();
1126 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1127 swap(cell(index(r, c)), cell(index(r + 1, c)));
1131 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1134 cur.pos() = cur.lastpos();
1136 //mathcursor->normalize();
1137 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1141 case LFUN_TABULAR_FEATURE: {
1142 cur.recordUndoInset();
1143 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1144 istringstream is(to_utf8(cmd.argument()));
1147 if (s == "valign-top")
1149 else if (s == "valign-middle")
1151 else if (s == "valign-bottom")
1153 else if (s == "align-left")
1154 halign('l', cur.col());
1155 else if (s == "align-right")
1156 halign('r', cur.col());
1157 else if (s == "align-center")
1158 halign('c', cur.col());
1159 else if (s == "append-row")
1160 for (int i = 0, n = extractInt(is); i < n; ++i)
1162 else if (s == "delete-row") {
1163 cur.clearSelection(); // bug 4323
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)
1198 addCol(cur.col() + 1);
1199 cur.idx() = index(r, c);
1201 else if (s == "delete-column") {
1202 cur.clearSelection(); // bug 4323
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 case LFUN_WORD_LEFT_SELECT:
1311 case LFUN_WORD_LEFT:
1312 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1313 cmd.action == LFUN_WORD_LEFT_SELECT ||
1314 cmd.action == LFUN_LINE_BEGIN_SELECT);
1315 cur.macroModeClose();
1316 if (cur.pos() != 0) {
1318 } else if (cur.idx() % cur.ncols() != 0) {
1319 cur.idx() -= cur.idx() % cur.ncols();
1321 } else if (cur.idx() != 0) {
1325 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1330 case LFUN_WORD_FORWARD_SELECT:
1331 case LFUN_WORD_FORWARD:
1332 case LFUN_WORD_RIGHT_SELECT:
1333 case LFUN_WORD_RIGHT:
1334 case LFUN_LINE_END_SELECT:
1336 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1337 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1338 cmd.action == LFUN_LINE_END_SELECT);
1339 cur.macroModeClose();
1341 if (cur.pos() != cur.lastpos()) {
1342 cur.pos() = cur.lastpos();
1343 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1344 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1345 cur.pos() = cur.lastpos();
1346 } else if (cur.idx() != cur.lastidx()) {
1347 cur.idx() = cur.lastidx();
1348 cur.pos() = cur.lastpos();
1350 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1356 InsetMathNest::doDispatch(cur, cmd);
1361 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1362 FuncStatus & status) const
1364 switch (cmd.action) {
1365 case LFUN_TABULAR_FEATURE: {
1366 string const s = to_utf8(cmd.argument());
1367 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1368 status.enabled(false);
1369 status.message(from_utf8(N_("Only one row")));
1373 (s == "delete-column" || s == "swap-column")) {
1374 status.enabled(false);
1375 status.message(from_utf8(N_("Only one column")));
1378 if ((rowinfo_[cur.row()].lines_ == 0 &&
1379 s == "delete-hline-above") ||
1380 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1381 s == "delete-hline-below")) {
1382 status.enabled(false);
1383 status.message(from_utf8(N_("No hline to delete")));
1387 if ((colinfo_[cur.col()].lines_ == 0 &&
1388 s == "delete-vline-left") ||
1389 (colinfo_[cur.col() + 1].lines_ == 0 &&
1390 s == "delete-vline-right")) {
1391 status.enabled(false);
1392 status.message(from_utf8(N_("No vline to delete")));
1395 if (s == "valign-top" || s == "valign-middle" ||
1396 s == "valign-bottom" || s == "align-left" ||
1397 s == "align-right" || s == "align-center" ||
1398 s == "append-row" || s == "delete-row" ||
1399 s == "copy-row" || s == "swap-row" ||
1400 s == "add-hline-above" || s == "add-hline-below" ||
1401 s == "delete-hline-above" || s == "delete-hline-below" ||
1402 s == "append-column" || s == "delete-column" ||
1403 s == "copy-column" || s == "swap-column" ||
1404 s == "add-vline-left" || s == "add-vline-right" ||
1405 s == "delete-vline-left" || s == "delete-vline-right")
1406 status.enabled(true);
1408 status.enabled(false);
1409 status.message(bformat(
1410 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1413 status.setOnOff((s == "align-left" && halign(cur.col()) == 'l')
1414 || (s == "align-right" && halign(cur.col()) == 'r')
1415 || (s == "align-center" && halign(cur.col()) == 'c')
1416 || (s == "valign-top" && valign() == 't')
1417 || (s == "valign-bottom" && valign() == 'b')
1418 || (s == "valign-middle" && valign() == 'm'));
1421 // FIXME: What did this code do?
1422 // Please check whether it is still needed!
1423 // should be more precise
1424 if (v_align_ == '\0') {
1425 status.enable(true);
1428 if (cmd.argument().empty()) {
1429 status.enable(false);
1432 if (!support::contains("tcb", cmd.argument()[0])) {
1433 status.enable(false);
1436 status.setOnOff(cmd.argument()[0] == v_align_);
1437 status.enabled(true);
1442 case LFUN_CELL_SPLIT:
1443 status.enabled(true);
1446 case LFUN_CELL_BACKWARD:
1447 case LFUN_CELL_FORWARD:
1448 status.enabled(true);
1452 return InsetMathNest::getStatus(cur, cmd, status);