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.
14 #include "InsetMathGrid.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
23 #include "FuncStatus.h"
25 #include "FuncRequest.h"
27 #include "frontends/Clipboard.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include <boost/assert.hpp>
40 using namespace lyx::support;
45 static docstring verboseHLine(int n)
48 for (int i = 0; i < n; ++i)
56 static int extractInt(istream & is)
60 return (num == 0) ? 1 : num;
65 //////////////////////////////////////////////////////////////
68 InsetMathGrid::CellInfo::CellInfo()
74 //////////////////////////////////////////////////////////////
77 InsetMathGrid::RowInfo::RowInfo()
78 : lines_(0), skip_(0), allow_newpage_(true)
83 int InsetMathGrid::RowInfo::skipPixels() const
85 return crskip_.inBP();
90 //////////////////////////////////////////////////////////////
93 InsetMathGrid::ColInfo::ColInfo()
94 : align_('c'), lines_(0)
98 //////////////////////////////////////////////////////////////
101 InsetMathGrid::InsetMathGrid()
112 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
113 : InsetMathNest(m * n),
123 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
124 : InsetMathNest(m * n),
131 setVerticalAlignment(v);
132 setHorizontalAlignments(h);
136 Inset * InsetMathGrid::clone() const
138 return new InsetMathGrid(*this);
142 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
144 return col + ncols() * row;
148 void InsetMathGrid::setDefaults()
151 lyxerr << "positive number of columns expected" << endl;
153 // lyxerr << "positive number of rows expected" << endl;
154 for (col_type col = 0; col < ncols(); ++col) {
155 colinfo_[col].align_ = defaultColAlign(col);
156 colinfo_[col].skip_ = defaultColSpace(col);
157 colinfo_[col].special_.clear();
162 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
165 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
168 colinfo_[col].lines_++;
169 } else if ((c == 'p' || c == 'm' || c == 'b'||
170 c == '!' || c == '@' || c == '>' || c == '<') &&
171 it + 1 != hh.end() && *(it + 1) == '{') {
172 // @{decl.} and p{width} are standard LaTeX, the
173 // others are extensions by array.sty
174 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
176 // this declares a new column
178 // Only intercolumn stuff is allowed
179 // in the last dummy column
181 colinfo_[col].align_ = 'l';
183 // this is intercolumn stuff
184 if (colinfo_[col].special_.empty())
185 // Overtake possible lines
186 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
190 while (it != hh.end()) {
192 colinfo_[col].special_ += c;
198 if (brace_open > 0 && brace_open == brace_close)
203 colinfo_[col].lines_ = count(
204 colinfo_[col].special_.begin(),
205 colinfo_[col].special_.end(), '|');
206 LYXERR(Debug::MATHED, "special column separator: `"
207 << to_utf8(colinfo_[col].special_) << '\'');
209 colinfo_[col].lines_ = 0;
210 colinfo_[col].special_.clear();
212 } else if (col >= ncols()) {
213 // Only intercolumn stuff is allowed in the last
216 } else if (c == 'c' || c == 'l' || c == 'r') {
217 colinfo_[col].align_ = static_cast<char>(c);
218 if (!colinfo_[col].special_.empty()) {
219 colinfo_[col].special_ += c;
220 colinfo_[col].lines_ = count(
221 colinfo_[col].special_.begin(),
222 colinfo_[col].special_.end(), '|');
223 LYXERR(Debug::MATHED, "special column separator: `"
224 << to_utf8(colinfo_[col].special_) << '\'');
227 colinfo_[col].lines_ = 0;
228 colinfo_[col].special_.clear();
230 lyxerr << "unknown column separator: '" << c << "'" << endl;
235 col_type n = hh.size();
238 for (col_type col = 0; col < n; ++col)
239 colinfo_[col].align_ = hh[col];
244 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
247 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
248 if (*it == 'c' || *it == 'l' || *it == 'r'||
249 *it == 'p' || *it == 'm' || *it == 'b')
251 // let's have at least one column, even if we did not recognize its
259 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
261 colinfo_[col].align_ = h;
262 if (!colinfo_[col].special_.empty()) {
263 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
264 if (c == 'l' || c == 'c' || c == 'r')
267 // FIXME: Change alignment of p, m and b columns, too
271 char InsetMathGrid::horizontalAlignment(col_type col) const
273 return colinfo_[col].align_;
277 docstring InsetMathGrid::horizontalAlignments() const
280 for (col_type col = 0; col < ncols(); ++col) {
281 if (colinfo_[col].special_.empty()) {
282 res += docstring(colinfo_[col].lines_, '|');
283 res += colinfo_[col].align_;
285 res += colinfo_[col].special_;
287 if (colinfo_[ncols()].special_.empty())
288 return res + docstring(colinfo_[ncols()].lines_, '|');
289 return res + colinfo_[ncols()].special_;
293 void InsetMathGrid::setVerticalAlignment(char c)
299 char InsetMathGrid::verticalAlignment() const
305 InsetMathGrid::col_type InsetMathGrid::ncols() const
307 return colinfo_.size() - 1;
311 InsetMathGrid::row_type InsetMathGrid::nrows() const
313 return rowinfo_.size() - 1;
317 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
319 return idx % ncols();
323 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
325 return idx / ncols();
329 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
331 rowinfo_[row].crskip_ = crskip;
335 Length InsetMathGrid::vcrskip(row_type row) const
337 return rowinfo_[row].crskip_;
341 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
343 // let the cells adjust themselves
344 InsetMathNest::metrics(mi);
346 BufferView & bv = *mi.base.bv;
348 // compute absolute sizes of vertical structure
349 for (row_type row = 0; row < nrows(); ++row) {
352 for (col_type col = 0; col < ncols(); ++col) {
353 Dimension const & dimc = cell(index(row, col)).dimension(bv);
354 asc = max(asc, dimc.asc);
355 desc = max(desc, dimc.des);
357 rowinfo_[row].ascent_ = asc;
358 rowinfo_[row].descent_ = desc;
360 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
361 rowinfo_[nrows()].ascent_ = 0;
362 rowinfo_[nrows()].descent_ = 0;
364 // compute vertical offsets
365 rowinfo_[0].offset_ = 0;
366 for (row_type row = 1; row <= nrows(); ++row) {
367 rowinfo_[row].offset_ =
368 rowinfo_[row - 1].offset_ +
369 rowinfo_[row - 1].descent_ +
370 rowinfo_[row - 1].skipPixels() +
372 rowinfo_[row].lines_ * hlinesep() +
373 rowinfo_[row].ascent_;
376 // adjust vertical offset
383 h = rowinfo_[nrows() - 1].offset_;
386 h = rowinfo_[nrows() - 1].offset_ / 2;
388 for (row_type row = 0; row <= nrows(); ++row)
389 rowinfo_[row].offset_ -= h;
392 // compute absolute sizes of horizontal structure
393 for (col_type col = 0; col < ncols(); ++col) {
395 for (row_type row = 0; row < nrows(); ++row)
396 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
397 colinfo_[col].width_ = wid;
399 colinfo_[ncols()].width_ = 0;
401 // compute horizontal offsets
402 colinfo_[0].offset_ = border();
403 for (col_type col = 1; col <= ncols(); ++col) {
404 colinfo_[col].offset_ =
405 colinfo_[col - 1].offset_ +
406 colinfo_[col - 1].width_ +
407 colinfo_[col - 1].skip_ +
409 colinfo_[col].lines_ * vlinesep();
413 dim.wid = colinfo_[ncols() - 1].offset_
414 + colinfo_[ncols() - 1].width_
415 + vlinesep() * colinfo_[ncols()].lines_
418 dim.asc = - rowinfo_[0].offset_
419 + rowinfo_[0].ascent_
420 + hlinesep() * rowinfo_[0].lines_
423 dim.des = rowinfo_[nrows() - 1].offset_
424 + rowinfo_[nrows() - 1].descent_
425 + hlinesep() * rowinfo_[nrows()].lines_
430 // Increase ws_[i] for 'R' columns (except the first one)
431 for (int i = 1; i < nc_; ++i)
432 if (align_[i] == 'R')
433 ws_[i] += 10 * df_width;
434 // Increase ws_[i] for 'C' column
435 if (align_[0] == 'C')
436 if (ws_[0] < 7 * workwidth / 8)
437 ws_[0] = 7 * workwidth / 8;
441 for (cxrow = row_.begin(); cxrow; ++cxrow) {
444 for (int i = 0; i < nc_; ++i) {
446 if (cxrow->getTab(i) <= 0) {
447 cxrow->setTab(i, df_width);
455 lf = (ws_[i] - cxrow->getTab(i))/2;
459 lf = ws_[i] - cxrow->getTab(i);
462 if (cxrow == row_.begin())
464 else if (cxrow.is_last())
465 lf = ws_[i] - cxrow->getTab(i);
467 lf = (ws_[i] - cxrow->getTab(i))/2;
470 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
471 cxrow->setTab(i, lf + rg);
472 rg = ws_[i] - ww + colsep();
473 if (cxrow == row_.begin())
474 width += ws_[i] + colsep();
476 cxrow->setBaseline(cxrow->getBaseline() - ascent);
479 metricsMarkers2(dim);
480 // Cache the inset dimension.
481 setDimCache(mi, dim);
485 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
487 drawWithMargin(pi, x, y, 1, 1);
491 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
492 int lmargin, int rmargin) const
494 Dimension const dim = dimension(*pi.base.bv);
495 BufferView const & bv = *pi.base.bv;
497 for (idx_type idx = 0; idx < nargs(); ++idx)
498 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
499 y + cellYOffset(idx));
501 for (row_type row = 0; row <= nrows(); ++row)
502 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
503 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
504 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
505 pi.pain.line(x + lmargin + 1, yy,
506 x + dim.width() - rmargin - 1, yy,
510 for (col_type col = 0; col <= ncols(); ++col)
511 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
512 int xx = x + lmargin + colinfo_[col].offset_
513 - i * vlinesep() - vlinesep()/2 - colsep()/2;
514 pi.pain.line(xx, y - dim.ascent() + 1,
515 xx, y + dim.descent() - 1,
518 drawMarkers2(pi, x, y);
522 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
524 // let the cells adjust themselves
525 //InsetMathNest::metrics(mi);
526 for (idx_type i = 0; i < nargs(); ++i)
527 cell(i).metricsT(mi, dim);
529 // compute absolute sizes of vertical structure
530 for (row_type row = 0; row < nrows(); ++row) {
533 for (col_type col = 0; col < ncols(); ++col) {
534 //MathData const & c = cell(index(row, col));
537 asc = max(asc, dimc.ascent());
538 desc = max(desc, dimc.descent());
540 rowinfo_[row].ascent_ = asc;
541 rowinfo_[row].descent_ = desc;
543 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
544 rowinfo_[nrows()].ascent_ = 0;
545 rowinfo_[nrows()].descent_ = 0;
547 // compute vertical offsets
548 rowinfo_[0].offset_ = 0;
549 for (row_type row = 1; row <= nrows(); ++row) {
550 rowinfo_[row].offset_ =
551 rowinfo_[row - 1].offset_ +
552 rowinfo_[row - 1].descent_ +
553 //rowinfo_[row - 1].skipPixels() +
555 //rowinfo_[row].lines_ * hlinesep() +
556 rowinfo_[row].ascent_;
559 // adjust vertical offset
566 h = rowinfo_[nrows() - 1].offset_;
569 h = rowinfo_[nrows() - 1].offset_ / 2;
571 for (row_type row = 0; row <= nrows(); ++row)
572 rowinfo_[row].offset_ -= h;
575 // compute absolute sizes of horizontal structure
576 for (col_type col = 0; col < ncols(); ++col) {
578 for (row_type row = 0; row < nrows(); ++row) {
580 //wid = max(wid, cell(index(row, col)).width());
582 colinfo_[col].width_ = wid;
584 colinfo_[ncols()].width_ = 0;
586 // compute horizontal offsets
587 colinfo_[0].offset_ = border();
588 for (col_type col = 1; col <= ncols(); ++col) {
589 colinfo_[col].offset_ =
590 colinfo_[col - 1].offset_ +
591 colinfo_[col - 1].width_ +
592 colinfo_[col - 1].skip_ +
594 //colinfo_[col].lines_ * vlinesep();
598 dim.wid = colinfo_[ncols() - 1].offset_
599 + colinfo_[ncols() - 1].width_
600 //+ vlinesep() * colinfo_[ncols()].lines_
603 dim.asc = -rowinfo_[0].offset_
604 + rowinfo_[0].ascent_
605 //+ hlinesep() * rowinfo_[0].lines_
608 dim.des = rowinfo_[nrows() - 1].offset_
609 + rowinfo_[nrows() - 1].descent_
610 //+ hlinesep() * rowinfo_[nrows()].lines_
615 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
617 // for (idx_type idx = 0; idx < nargs(); ++idx)
618 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
622 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
626 if (!rowinfo_[row].crskip_.zero())
627 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
628 else if(!rowinfo_[row].allow_newpage_)
631 // make sure an upcoming '[' does not break anything
632 if (row + 1 < nrows()) {
633 MathData const & c = cell(index(row + 1, 0));
634 if (c.size() && c.front()->getChar() == '[')
639 // only add \\ if necessary
640 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
643 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
647 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
649 if (col + 1 == lastcol)
651 return from_ascii(" & ");
655 void InsetMathGrid::addRow(row_type row)
657 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
659 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
661 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
665 void InsetMathGrid::appendRow()
667 rowinfo_.push_back(RowInfo());
668 //cells_.insert(cells_.end(), ncols(), MathData());
669 for (col_type col = 0; col < ncols(); ++col) {
670 cells_.push_back(cells_type::value_type());
671 cellinfo_.push_back(CellInfo());
676 void InsetMathGrid::delRow(row_type row)
681 cells_type::iterator it = cells_.begin() + row * ncols();
682 cells_.erase(it, it + ncols());
684 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
685 cellinfo_.erase(jt, jt + ncols());
687 rowinfo_.erase(rowinfo_.begin() + row);
691 void InsetMathGrid::copyRow(row_type row)
694 for (col_type col = 0; col < ncols(); ++col)
695 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
699 void InsetMathGrid::swapRow(row_type row)
703 if (row + 1 == nrows())
705 for (col_type col = 0; col < ncols(); ++col)
706 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
710 void InsetMathGrid::addCol(col_type newcol)
712 const col_type nc = ncols();
713 const row_type nr = nrows();
714 cells_type new_cells((nc + 1) * nr);
715 vector<CellInfo> new_cellinfo((nc + 1) * nr);
717 for (row_type row = 0; row < nr; ++row)
718 for (col_type col = 0; col < nc; ++col) {
719 new_cells[row * (nc + 1) + col + (col >= newcol)]
720 = cells_[row * nc + col];
721 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
722 = cellinfo_[row * nc + col];
724 swap(cells_, new_cells);
725 swap(cellinfo_, new_cellinfo);
728 inf.skip_ = defaultColSpace(newcol);
729 inf.align_ = defaultColAlign(newcol);
730 colinfo_.insert(colinfo_.begin() + newcol, inf);
734 void InsetMathGrid::delCol(col_type col)
740 vector<CellInfo> tmpcellinfo;
741 for (col_type i = 0; i < nargs(); ++i)
742 if (i % ncols() != col) {
743 tmpcells.push_back(cells_[i]);
744 tmpcellinfo.push_back(cellinfo_[i]);
746 swap(cells_, tmpcells);
747 swap(cellinfo_, tmpcellinfo);
749 colinfo_.erase(colinfo_.begin() + col);
753 void InsetMathGrid::copyCol(col_type col)
756 for (row_type row = 0; row < nrows(); ++row)
757 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
761 void InsetMathGrid::swapCol(col_type col)
765 if (col + 1 == ncols())
767 for (row_type row = 0; row < nrows(); ++row)
768 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
772 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
774 col_type c = col(idx);
775 int x = colinfo_[c].offset_;
776 char align = colinfo_[c].align_;
777 Dimension const & celldim = cell(idx).dimension(bv);
778 if (align == 'r' || align == 'R')
779 x += colinfo_[c].width_ - celldim.wid;
780 if (align == 'c' || align == 'C')
781 x += (colinfo_[c].width_ - celldim.wid) / 2;
786 int InsetMathGrid::cellYOffset(idx_type idx) const
788 return rowinfo_[row(idx)].offset_;
792 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
797 cur.idx() -= ncols();
799 if (cur.row() + 1 >= nrows())
801 cur.idx() += ncols();
803 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
808 bool InsetMathGrid::idxBackward(Cursor & cur) const
810 // leave matrix if at the front edge
814 cur.pos() = cur.lastpos();
819 bool InsetMathGrid::idxForward(Cursor & cur) const
821 // leave matrix if at the back edge
822 if (cur.col() + 1 == ncols())
830 bool InsetMathGrid::idxFirst(Cursor & cur) const
837 cur.idx() = (nrows() - 1) * ncols();
840 cur.idx() = ((nrows() - 1) / 2) * ncols();
847 bool InsetMathGrid::idxLast(Cursor & cur) const
851 cur.idx() = ncols() - 1;
854 cur.idx() = nargs() - 1;
857 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
859 cur.pos() = cur.lastpos();
864 bool InsetMathGrid::idxDelete(idx_type & idx)
866 // nothing to do if we have just one row
870 // nothing to do if we are in the middle of the last row of the inset
871 if (idx + ncols() > nargs())
874 // try to delete entire sequence of ncols() empty cells if possible
875 for (idx_type i = idx; i < idx + ncols(); ++i)
879 // move cells if necessary
880 for (idx_type i = index(row(idx), 0); i < idx; ++i)
881 swap(cell(i), cell(i + ncols()));
888 // undo effect of Ctrl-Tab (i.e. pull next cell)
889 //if (idx + 1 != nargs())
890 // cell(idx).swap(cell(idx + 1));
892 // we handled the event..
897 // reimplement old behaviour when pressing Delete in the last position
899 void InsetMathGrid::idxGlue(idx_type idx)
901 col_type c = col(idx);
902 if (c + 1 == ncols()) {
903 if (row(idx) + 1 != nrows()) {
904 for (col_type cc = 0; cc < ncols(); ++cc)
905 cell(idx).append(cell(idx + cc + 1));
906 delRow(row(idx) + 1);
909 cell(idx).append(cell(idx + 1));
910 for (col_type cc = c + 2; cc < ncols(); ++cc)
911 cell(idx - c + cc - 1) = cell(idx - c + cc);
912 cell(idx - c + ncols() - 1).clear();
917 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
919 return rowinfo_[row];
923 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
925 return rowinfo_[row];
929 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
931 row_type const ri = row(idx);
932 row_type const r1 = min(row(from), row(to));
933 row_type const r2 = max(row(from), row(to));
934 col_type const ci = col(idx);
935 col_type const c1 = min(col(from), col(to));
936 col_type const c2 = max(col(from), col(to));
937 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
942 void InsetMathGrid::normalize(NormalStream & os) const
945 for (row_type row = 0; row < nrows(); ++row) {
947 for (col_type col = 0; col < ncols(); ++col)
948 os << "[cell " << cell(index(row, col)) << ']';
955 void InsetMathGrid::mathmlize(MathStream & os) const
957 os << MTag("mtable");
958 for (row_type row = 0; row < nrows(); ++row) {
960 for (col_type col = 0; col < ncols(); ++col)
961 os << cell(index(row, col));
964 os << ETag("mtable");
968 void InsetMathGrid::write(WriteStream & os) const
971 for (row_type row = 0; row < nrows(); ++row) {
972 os << verboseHLine(rowinfo_[row].lines_);
973 // don't write & and empty cells at end of line
974 col_type lastcol = 0;
975 bool emptyline = true;
976 for (col_type col = 0; col < ncols(); ++col)
977 if (!cell(index(row, col)).empty()) {
981 for (col_type col = 0; col < lastcol; ++col)
982 os << cell(index(row, col)) << eocString(col, lastcol);
983 eol = eolString(row, emptyline, os.fragile());
985 // append newline only if line wasn't completely empty
986 // and this was not the last line in the grid
987 if (!emptyline && row + 1 < nrows())
990 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1002 int InsetMathGrid::colsep() const
1008 int InsetMathGrid::rowsep() const
1014 int InsetMathGrid::hlinesep() const
1020 int InsetMathGrid::vlinesep() const
1026 int InsetMathGrid::border() const
1032 void InsetMathGrid::splitCell(Cursor & cur)
1034 if (cur.idx() == cur.lastidx())
1036 MathData ar = cur.cell();
1037 ar.erase(0, cur.pos());
1038 cur.cell().erase(cur.pos(), cur.lastpos());
1041 cur.cell().insert(0, ar);
1045 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1047 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1048 switch (cmd.action) {
1050 // insert file functions
1051 case LFUN_LINE_DELETE:
1052 // FIXME: We use recordUndoInset when a change reflects more
1053 // than one cell, because recordUndo does not work for
1054 // multiple cells. Unfortunately this puts the cursor in front
1055 // of the inset after undo. This is (especilally for large
1057 cur.recordUndoInset();
1058 //autocorrect_ = false;
1066 if (cur.idx() > cur.lastidx())
1067 cur.idx() = cur.lastidx();
1068 if (cur.pos() > cur.lastpos())
1069 cur.pos() = cur.lastpos();
1072 case LFUN_CELL_SPLIT:
1077 case LFUN_CELL_BACKWARD:
1079 cur.selection() = false;
1080 if (!idxPrev(cur)) {
1081 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1086 case LFUN_CELL_FORWARD:
1087 // Can't handle selection by additional 'shift' as this is
1088 // hard bound to LFUN_CELL_BACKWARD
1089 cur.selection() = false;
1090 if (!idxNext(cur)) {
1091 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1096 case LFUN_NEW_LINE: {
1097 cur.recordUndoInset();
1098 row_type const r = cur.row();
1102 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1103 swap(cell(index(r, c)), cell(index(r + 1, c)));
1107 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1110 cur.pos() = cur.lastpos();
1112 //mathcursor->normalize();
1113 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1117 case LFUN_TABULAR_FEATURE: {
1118 cur.recordUndoInset();
1119 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1120 istringstream is(to_utf8(cmd.argument()));
1123 if (s == "valign-top")
1124 setVerticalAlignment('t');
1125 else if (s == "valign-middle")
1126 setVerticalAlignment('c');
1127 else if (s == "valign-bottom")
1128 setVerticalAlignment('b');
1129 else if (s == "align-left")
1130 setHorizontalAlignment('l', cur.col());
1131 else if (s == "align-right")
1132 setHorizontalAlignment('r', cur.col());
1133 else if (s == "align-center")
1134 setHorizontalAlignment('c', cur.col());
1135 else if (s == "append-row")
1136 for (int i = 0, n = extractInt(is); i < n; ++i)
1138 else if (s == "delete-row") {
1139 cur.clearSelection(); // bug 4323
1140 for (int i = 0, n = extractInt(is); i < n; ++i) {
1142 if (cur.idx() >= nargs())
1143 cur.idx() -= ncols();
1145 cur.pos() = 0; // trick, see below
1147 else if (s == "copy-row") {
1148 // Here (as later) we save the cursor col/row
1149 // in order to restore it after operation.
1150 row_type const r = cur.row();
1151 col_type const c = cur.col();
1152 for (int i = 0, n = extractInt(is); i < n; ++i)
1154 cur.idx() = index(r, c);
1156 else if (s == "swap-row") {
1158 // Trick to suppress same-idx-means-different-cell
1162 else if (s == "add-hline-above")
1163 rowinfo_[cur.row()].lines_++;
1164 else if (s == "add-hline-below")
1165 rowinfo_[cur.row()+1].lines_++;
1166 else if (s == "delete-hline-above")
1167 rowinfo_[cur.row()].lines_--;
1168 else if (s == "delete-hline-below")
1169 rowinfo_[cur.row()+1].lines_--;
1170 else if (s == "append-column") {
1171 row_type const r = cur.row();
1172 col_type const c = cur.col();
1173 for (int i = 0, n = extractInt(is); i < n; ++i)
1174 addCol(cur.col() + 1);
1175 cur.idx() = index(r, c);
1177 else if (s == "delete-column") {
1178 cur.clearSelection(); // bug 4323
1179 row_type const r = cur.row();
1180 col_type const c = cur.col();
1181 for (int i = 0, n = extractInt(is); i < n; ++i)
1182 delCol(col(cur.idx()));
1183 cur.idx() = index(r, min(c, cur.ncols() - 1));
1184 cur.pos() = 0; // trick, see above
1186 else if (s == "copy-column") {
1187 row_type const r = cur.row();
1188 col_type const c = cur.col();
1190 cur.idx() = index(r, c);
1192 else if (s == "swap-column") {
1194 cur.pos() = 0; // trick, see above
1196 else if (s == "add-vline-left") {
1197 colinfo_[cur.col()].lines_++;
1198 if (!colinfo_[cur.col()].special_.empty())
1199 colinfo_[cur.col()].special_ += '|';
1201 else if (s == "add-vline-right") {
1202 colinfo_[cur.col()+1].lines_++;
1203 if (!colinfo_[cur.col()+1].special_.empty())
1204 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1206 else if (s == "delete-vline-left") {
1207 colinfo_[cur.col()].lines_--;
1208 docstring & special = colinfo_[cur.col()].special_;
1209 if (!special.empty()) {
1210 docstring::size_type i = special.rfind('|');
1211 BOOST_ASSERT(i != docstring::npos);
1212 special.erase(i, 1);
1215 else if (s == "delete-vline-right") {
1216 colinfo_[cur.col()+1].lines_--;
1217 docstring & special = colinfo_[cur.col()+1].special_;
1218 if (!special.empty()) {
1219 docstring::size_type i = special.find('|');
1220 BOOST_ASSERT(i != docstring::npos);
1221 special.erase(i, 1);
1228 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1229 lyxerr << "returning FINISHED_LEFT" << endl;
1234 cur.message(_("Paste"));
1235 cap::replaceSelection(cur);
1237 if (cmd.argument().empty() && !theClipboard().isInternal())
1238 topaste = theClipboard().getAsText();
1240 idocstringstream is(cmd.argument());
1243 topaste = cap::selection(n);
1245 InsetMathGrid grid(1, 1);
1246 if (!topaste.empty())
1247 mathed_parse_normal(grid, topaste);
1249 if (grid.nargs() == 1) {
1250 // single cell/part of cell
1252 cur.cell().insert(cur.pos(), grid.cell(0));
1253 cur.pos() += grid.cell(0).size();
1256 cur.recordUndoInset();
1257 col_type const numcols =
1258 min(grid.ncols(), ncols() - col(cur.idx()));
1259 row_type const numrows =
1260 min(grid.nrows(), nrows() - cur.row());
1261 for (row_type r = 0; r < numrows; ++r) {
1262 for (col_type c = 0; c < numcols; ++c) {
1263 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1264 cell(i).insert(0, grid.cell(grid.index(r, c)));
1266 // append the left over horizontal cells to the last column
1267 idx_type i = index(r + cur.row(), ncols() - 1);
1268 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1269 cell(i).append(grid.cell(grid.index(r, c)));
1271 // append the left over vertical cells to the last _cell_
1272 idx_type i = nargs() - 1;
1273 for (row_type r = numrows; r < grid.nrows(); ++r)
1274 for (col_type c = 0; c < grid.ncols(); ++c)
1275 cell(i).append(grid.cell(grid.index(r, c)));
1277 cur.clearSelection(); // bug 393
1282 case LFUN_LINE_BEGIN_SELECT:
1283 case LFUN_LINE_BEGIN:
1284 case LFUN_WORD_BACKWARD_SELECT:
1285 case LFUN_WORD_BACKWARD:
1286 case LFUN_WORD_LEFT_SELECT:
1287 case LFUN_WORD_LEFT:
1288 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1289 cmd.action == LFUN_WORD_LEFT_SELECT ||
1290 cmd.action == LFUN_LINE_BEGIN_SELECT);
1291 cur.macroModeClose();
1292 if (cur.pos() != 0) {
1294 } else if (cur.idx() % cur.ncols() != 0) {
1295 cur.idx() -= cur.idx() % cur.ncols();
1297 } else if (cur.idx() != 0) {
1301 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1306 case LFUN_WORD_FORWARD_SELECT:
1307 case LFUN_WORD_FORWARD:
1308 case LFUN_WORD_RIGHT_SELECT:
1309 case LFUN_WORD_RIGHT:
1310 case LFUN_LINE_END_SELECT:
1312 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1313 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1314 cmd.action == LFUN_LINE_END_SELECT);
1315 cur.macroModeClose();
1317 if (cur.pos() != cur.lastpos()) {
1318 cur.pos() = cur.lastpos();
1319 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1320 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1321 cur.pos() = cur.lastpos();
1322 } else if (cur.idx() != cur.lastidx()) {
1323 cur.idx() = cur.lastidx();
1324 cur.pos() = cur.lastpos();
1326 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1332 InsetMathNest::doDispatch(cur, cmd);
1337 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1338 FuncStatus & status) const
1340 switch (cmd.action) {
1341 case LFUN_TABULAR_FEATURE: {
1342 string const s = to_utf8(cmd.argument());
1343 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1344 status.enabled(false);
1345 status.message(from_utf8(N_("Only one row")));
1349 (s == "delete-column" || s == "swap-column")) {
1350 status.enabled(false);
1351 status.message(from_utf8(N_("Only one column")));
1354 if ((rowinfo_[cur.row()].lines_ == 0 &&
1355 s == "delete-hline-above") ||
1356 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1357 s == "delete-hline-below")) {
1358 status.enabled(false);
1359 status.message(from_utf8(N_("No hline to delete")));
1363 if ((colinfo_[cur.col()].lines_ == 0 &&
1364 s == "delete-vline-left") ||
1365 (colinfo_[cur.col() + 1].lines_ == 0 &&
1366 s == "delete-vline-right")) {
1367 status.enabled(false);
1368 status.message(from_utf8(N_("No vline to delete")));
1371 if (s == "valign-top" || s == "valign-middle" ||
1372 s == "valign-bottom" || s == "align-left" ||
1373 s == "align-right" || s == "align-center" ||
1374 s == "append-row" || s == "delete-row" ||
1375 s == "copy-row" || s == "swap-row" ||
1376 s == "add-hline-above" || s == "add-hline-below" ||
1377 s == "delete-hline-above" || s == "delete-hline-below" ||
1378 s == "append-column" || s == "delete-column" ||
1379 s == "copy-column" || s == "swap-column" ||
1380 s == "add-vline-left" || s == "add-vline-right" ||
1381 s == "delete-vline-left" || s == "delete-vline-right")
1382 status.enabled(true);
1384 status.enabled(false);
1385 status.message(bformat(
1386 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1389 char const ha = horizontalAlignment(cur.col());
1390 char const va = verticalAlignment();
1391 status.setOnOff((s == "align-left" && ha == 'l')
1392 || (s == "align-right" && ha == 'r')
1393 || (s == "align-center" && ha == 'c')
1394 || (s == "valign-top" && va == 't')
1395 || (s == "valign-bottom" && va == 'b')
1396 || (s == "valign-middle" && va == 'm'));
1399 // FIXME: What did this code do?
1400 // Please check whether it is still needed!
1401 // should be more precise
1402 if (v_align_ == '\0') {
1403 status.enable(true);
1406 if (cmd.argument().empty()) {
1407 status.enable(false);
1410 if (!contains("tcb", cmd.argument()[0])) {
1411 status.enable(false);
1414 status.setOnOff(cmd.argument()[0] == v_align_);
1415 status.enabled(true);
1420 case LFUN_CELL_SPLIT:
1421 status.enabled(true);
1424 case LFUN_CELL_BACKWARD:
1425 case LFUN_CELL_FORWARD:
1426 status.enabled(true);
1430 return InsetMathNest::getStatus(cur, cmd, status);