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 "support/lassert.h"
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
970 MathEnsurer ensurer(os, false);
972 for (row_type row = 0; row < nrows(); ++row) {
973 os << verboseHLine(rowinfo_[row].lines_);
974 // don't write & and empty cells at end of line
975 col_type lastcol = 0;
976 bool emptyline = true;
977 for (col_type col = 0; col < ncols(); ++col)
978 if (!cell(index(row, col)).empty()) {
982 for (col_type col = 0; col < lastcol; ++col) {
983 os << cell(index(row, col));
984 if (os.pendingBrace())
985 ModeSpecifier specifier(os, TEXT_MODE);
986 os << eocString(col, lastcol);
988 eol = eolString(row, emptyline, os.fragile());
990 // append newline only if line wasn't completely empty
991 // and this was not the last line in the grid
992 if (!emptyline && row + 1 < nrows())
995 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1007 int InsetMathGrid::colsep() const
1013 int InsetMathGrid::rowsep() const
1019 int InsetMathGrid::hlinesep() const
1025 int InsetMathGrid::vlinesep() const
1031 int InsetMathGrid::border() const
1037 void InsetMathGrid::splitCell(Cursor & cur)
1039 if (cur.idx() == cur.lastidx())
1041 MathData ar = cur.cell();
1042 ar.erase(0, cur.pos());
1043 cur.cell().erase(cur.pos(), cur.lastpos());
1046 cur.cell().insert(0, ar);
1050 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1052 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1053 switch (cmd.action) {
1055 // insert file functions
1056 case LFUN_LINE_DELETE:
1057 // FIXME: We use recordUndoInset when a change reflects more
1058 // than one cell, because recordUndo does not work for
1059 // multiple cells. Unfortunately this puts the cursor in front
1060 // of the inset after undo. This is (especilally for large
1062 cur.recordUndoInset();
1063 //autocorrect_ = false;
1071 if (cur.idx() > cur.lastidx())
1072 cur.idx() = cur.lastidx();
1073 if (cur.pos() > cur.lastpos())
1074 cur.pos() = cur.lastpos();
1077 case LFUN_CELL_SPLIT:
1082 case LFUN_CELL_BACKWARD:
1084 cur.selection() = false;
1085 if (!idxPrev(cur)) {
1086 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1091 case LFUN_CELL_FORWARD:
1092 // Can't handle selection by additional 'shift' as this is
1093 // hard bound to LFUN_CELL_BACKWARD
1094 cur.selection() = false;
1095 if (!idxNext(cur)) {
1096 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1101 case LFUN_NEWLINE_INSERT: {
1102 cur.recordUndoInset();
1103 row_type const r = cur.row();
1107 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1108 swap(cell(index(r, c)), cell(index(r + 1, c)));
1112 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1115 cur.pos() = cur.lastpos();
1117 //mathcursor->normalize();
1118 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1122 case LFUN_TABULAR_FEATURE: {
1123 cur.recordUndoInset();
1124 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1125 istringstream is(to_utf8(cmd.argument()));
1128 if (s == "valign-top")
1129 setVerticalAlignment('t');
1130 else if (s == "valign-middle")
1131 setVerticalAlignment('c');
1132 else if (s == "valign-bottom")
1133 setVerticalAlignment('b');
1134 else if (s == "align-left")
1135 setHorizontalAlignment('l', cur.col());
1136 else if (s == "align-right")
1137 setHorizontalAlignment('r', cur.col());
1138 else if (s == "align-center")
1139 setHorizontalAlignment('c', cur.col());
1140 else if (s == "append-row")
1141 for (int i = 0, n = extractInt(is); i < n; ++i)
1143 else if (s == "delete-row") {
1144 cur.clearSelection(); // bug 4323
1145 for (int i = 0, n = extractInt(is); i < n; ++i) {
1147 if (cur.idx() >= nargs())
1148 cur.idx() -= ncols();
1150 cur.pos() = 0; // trick, see below
1152 else if (s == "copy-row") {
1153 // Here (as later) we save the cursor col/row
1154 // in order to restore it after operation.
1155 row_type const r = cur.row();
1156 col_type const c = cur.col();
1157 for (int i = 0, n = extractInt(is); i < n; ++i)
1159 cur.idx() = index(r, c);
1161 else if (s == "swap-row") {
1163 // Trick to suppress same-idx-means-different-cell
1167 else if (s == "add-hline-above")
1168 rowinfo_[cur.row()].lines_++;
1169 else if (s == "add-hline-below")
1170 rowinfo_[cur.row()+1].lines_++;
1171 else if (s == "delete-hline-above")
1172 rowinfo_[cur.row()].lines_--;
1173 else if (s == "delete-hline-below")
1174 rowinfo_[cur.row()+1].lines_--;
1175 else if (s == "append-column") {
1176 row_type const r = cur.row();
1177 col_type const c = cur.col();
1178 for (int i = 0, n = extractInt(is); i < n; ++i)
1179 addCol(cur.col() + 1);
1180 cur.idx() = index(r, c);
1182 else if (s == "delete-column") {
1183 cur.clearSelection(); // bug 4323
1184 row_type const r = cur.row();
1185 col_type const c = cur.col();
1186 for (int i = 0, n = extractInt(is); i < n; ++i)
1187 delCol(col(cur.idx()));
1188 cur.idx() = index(r, min(c, cur.ncols() - 1));
1189 cur.pos() = 0; // trick, see above
1191 else if (s == "copy-column") {
1192 row_type const r = cur.row();
1193 col_type const c = cur.col();
1195 cur.idx() = index(r, c);
1197 else if (s == "swap-column") {
1199 cur.pos() = 0; // trick, see above
1201 else if (s == "add-vline-left") {
1202 colinfo_[cur.col()].lines_++;
1203 if (!colinfo_[cur.col()].special_.empty())
1204 colinfo_[cur.col()].special_ += '|';
1206 else if (s == "add-vline-right") {
1207 colinfo_[cur.col()+1].lines_++;
1208 if (!colinfo_[cur.col()+1].special_.empty())
1209 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1211 else if (s == "delete-vline-left") {
1212 colinfo_[cur.col()].lines_--;
1213 docstring & special = colinfo_[cur.col()].special_;
1214 if (!special.empty()) {
1215 docstring::size_type i = special.rfind('|');
1216 LASSERT(i != docstring::npos, /**/);
1217 special.erase(i, 1);
1220 else if (s == "delete-vline-right") {
1221 colinfo_[cur.col()+1].lines_--;
1222 docstring & special = colinfo_[cur.col()+1].special_;
1223 if (!special.empty()) {
1224 docstring::size_type i = special.find('|');
1225 LASSERT(i != docstring::npos, /**/);
1226 special.erase(i, 1);
1233 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1234 lyxerr << "returning FINISHED_LEFT" << endl;
1239 cur.message(_("Paste"));
1240 cap::replaceSelection(cur);
1242 if (cmd.argument().empty() && !theClipboard().isInternal())
1243 topaste = theClipboard().getAsText();
1245 idocstringstream is(cmd.argument());
1248 topaste = cap::selection(n);
1250 InsetMathGrid grid(1, 1);
1251 if (!topaste.empty())
1252 mathed_parse_normal(grid, topaste);
1254 if (grid.nargs() == 1) {
1255 // single cell/part of cell
1257 cur.cell().insert(cur.pos(), grid.cell(0));
1258 cur.pos() += grid.cell(0).size();
1261 cur.recordUndoInset();
1262 col_type const numcols =
1263 min(grid.ncols(), ncols() - col(cur.idx()));
1264 row_type const numrows =
1265 min(grid.nrows(), nrows() - cur.row());
1266 for (row_type r = 0; r < numrows; ++r) {
1267 for (col_type c = 0; c < numcols; ++c) {
1268 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1269 cell(i).insert(0, grid.cell(grid.index(r, c)));
1271 // append the left over horizontal cells to the last column
1272 idx_type i = index(r + cur.row(), ncols() - 1);
1273 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1274 cell(i).append(grid.cell(grid.index(r, c)));
1276 // append the left over vertical cells to the last _cell_
1277 idx_type i = nargs() - 1;
1278 for (row_type r = numrows; r < grid.nrows(); ++r)
1279 for (col_type c = 0; c < grid.ncols(); ++c)
1280 cell(i).append(grid.cell(grid.index(r, c)));
1282 cur.clearSelection(); // bug 393
1287 case LFUN_LINE_BEGIN_SELECT:
1288 case LFUN_LINE_BEGIN:
1289 case LFUN_WORD_BACKWARD_SELECT:
1290 case LFUN_WORD_BACKWARD:
1291 case LFUN_WORD_LEFT_SELECT:
1292 case LFUN_WORD_LEFT:
1293 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1294 cmd.action == LFUN_WORD_LEFT_SELECT ||
1295 cmd.action == LFUN_LINE_BEGIN_SELECT);
1296 cur.macroModeClose();
1297 if (cur.pos() != 0) {
1299 } else if (cur.idx() % cur.ncols() != 0) {
1300 cur.idx() -= cur.idx() % cur.ncols();
1302 } else if (cur.idx() != 0) {
1306 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1311 case LFUN_WORD_FORWARD_SELECT:
1312 case LFUN_WORD_FORWARD:
1313 case LFUN_WORD_RIGHT_SELECT:
1314 case LFUN_WORD_RIGHT:
1315 case LFUN_LINE_END_SELECT:
1317 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1318 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1319 cmd.action == LFUN_LINE_END_SELECT);
1320 cur.macroModeClose();
1322 if (cur.pos() != cur.lastpos()) {
1323 cur.pos() = cur.lastpos();
1324 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1325 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1326 cur.pos() = cur.lastpos();
1327 } else if (cur.idx() != cur.lastidx()) {
1328 cur.idx() = cur.lastidx();
1329 cur.pos() = cur.lastpos();
1331 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1337 InsetMathNest::doDispatch(cur, cmd);
1342 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1343 FuncStatus & status) const
1345 switch (cmd.action) {
1346 case LFUN_TABULAR_FEATURE: {
1347 string const s = to_utf8(cmd.argument());
1348 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1349 status.setEnabled(false);
1350 status.message(from_utf8(N_("Only one row")));
1354 (s == "delete-column" || s == "swap-column")) {
1355 status.setEnabled(false);
1356 status.message(from_utf8(N_("Only one column")));
1359 if ((rowinfo_[cur.row()].lines_ == 0 &&
1360 s == "delete-hline-above") ||
1361 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1362 s == "delete-hline-below")) {
1363 status.setEnabled(false);
1364 status.message(from_utf8(N_("No hline to delete")));
1368 if ((colinfo_[cur.col()].lines_ == 0 &&
1369 s == "delete-vline-left") ||
1370 (colinfo_[cur.col() + 1].lines_ == 0 &&
1371 s == "delete-vline-right")) {
1372 status.setEnabled(false);
1373 status.message(from_utf8(N_("No vline to delete")));
1376 if (s == "valign-top" || s == "valign-middle" ||
1377 s == "valign-bottom" || s == "align-left" ||
1378 s == "align-right" || s == "align-center" ||
1379 s == "append-row" || s == "delete-row" ||
1380 s == "copy-row" || s == "swap-row" ||
1381 s == "add-hline-above" || s == "add-hline-below" ||
1382 s == "delete-hline-above" || s == "delete-hline-below" ||
1383 s == "append-column" || s == "delete-column" ||
1384 s == "copy-column" || s == "swap-column" ||
1385 s == "add-vline-left" || s == "add-vline-right" ||
1386 s == "delete-vline-left" || s == "delete-vline-right")
1387 status.setEnabled(true);
1389 status.setEnabled(false);
1390 status.message(bformat(
1391 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1394 char const ha = horizontalAlignment(cur.col());
1395 char const va = verticalAlignment();
1396 status.setOnOff((s == "align-left" && ha == 'l')
1397 || (s == "align-right" && ha == 'r')
1398 || (s == "align-center" && ha == 'c')
1399 || (s == "valign-top" && va == 't')
1400 || (s == "valign-bottom" && va == 'b')
1401 || (s == "valign-middle" && va == 'm'));
1404 // FIXME: What did this code do?
1405 // Please check whether it is still needed!
1406 // should be more precise
1407 if (v_align_ == '\0') {
1408 status.enable(true);
1411 if (cmd.argument().empty()) {
1412 status.enable(false);
1415 if (!contains("tcb", cmd.argument()[0])) {
1416 status.enable(false);
1419 status.setOnOff(cmd.argument()[0] == v_align_);
1420 status.setEnabled(true);
1425 case LFUN_CELL_SPLIT:
1426 status.setEnabled(true);
1429 case LFUN_CELL_BACKWARD:
1430 case LFUN_CELL_FORWARD:
1431 status.setEnabled(true);
1435 return InsetMathNest::getStatus(cur, cmd, status);