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/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
36 #include "support/lassert.h"
41 using namespace lyx::support;
46 static docstring verboseHLine(int n)
49 for (int i = 0; i < n; ++i)
57 static int extractInt(istream & is)
61 return (num == 0) ? 1 : num;
65 static void resetGrid(InsetMathGrid & grid)
67 while (grid.ncols() > 1)
68 grid.delCol(grid.ncols());
69 while (grid.nrows() > 1)
70 grid.delRow(grid.nrows());
71 grid.cell(0).erase(0, grid.cell(0).size());
77 //////////////////////////////////////////////////////////////
80 InsetMathGrid::CellInfo::CellInfo()
86 //////////////////////////////////////////////////////////////
89 InsetMathGrid::RowInfo::RowInfo()
90 : lines_(0), skip_(0), allow_newpage_(true)
95 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
97 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
98 return crskip_.inPixels(mi.base.textwidth,
99 fm.width(char_type('M')));
104 //////////////////////////////////////////////////////////////
107 InsetMathGrid::ColInfo::ColInfo()
108 : align_('c'), lines_(0)
112 //////////////////////////////////////////////////////////////
115 InsetMathGrid::InsetMathGrid()
126 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
127 : InsetMathNest(m * n),
137 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
138 : InsetMathNest(m * n),
145 setVerticalAlignment(v);
146 setHorizontalAlignments(h);
150 Inset * InsetMathGrid::clone() const
152 return new InsetMathGrid(*this);
156 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
158 return col + ncols() * row;
162 void InsetMathGrid::setDefaults()
165 lyxerr << "positive number of columns expected" << endl;
167 // lyxerr << "positive number of rows expected" << endl;
168 for (col_type col = 0; col < ncols(); ++col) {
169 colinfo_[col].align_ = defaultColAlign(col);
170 colinfo_[col].skip_ = defaultColSpace(col);
171 colinfo_[col].special_.clear();
176 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
179 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
182 colinfo_[col].lines_++;
183 } else if ((c == 'p' || c == 'm' || c == 'b'||
184 c == '!' || c == '@' || c == '>' || c == '<') &&
185 it + 1 != hh.end() && *(it + 1) == '{') {
186 // @{decl.} and p{width} are standard LaTeX, the
187 // others are extensions by array.sty
188 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
190 // this declares a new column
192 // Only intercolumn stuff is allowed
193 // in the last dummy column
195 colinfo_[col].align_ = 'l';
197 // this is intercolumn stuff
198 if (colinfo_[col].special_.empty())
199 // Overtake possible lines
200 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
204 while (it != hh.end()) {
206 colinfo_[col].special_ += c;
212 if (brace_open > 0 && brace_open == brace_close)
217 colinfo_[col].lines_ = count(
218 colinfo_[col].special_.begin(),
219 colinfo_[col].special_.end(), '|');
220 LYXERR(Debug::MATHED, "special column separator: `"
221 << to_utf8(colinfo_[col].special_) << '\'');
223 colinfo_[col].lines_ = 0;
224 colinfo_[col].special_.clear();
226 } else if (col >= ncols()) {
227 // Only intercolumn stuff is allowed in the last
230 } else if (c == 'c' || c == 'l' || c == 'r') {
231 colinfo_[col].align_ = static_cast<char>(c);
232 if (!colinfo_[col].special_.empty()) {
233 colinfo_[col].special_ += c;
234 colinfo_[col].lines_ = count(
235 colinfo_[col].special_.begin(),
236 colinfo_[col].special_.end(), '|');
237 LYXERR(Debug::MATHED, "special column separator: `"
238 << to_utf8(colinfo_[col].special_) << '\'');
241 colinfo_[col].lines_ = 0;
242 colinfo_[col].special_.clear();
244 lyxerr << "unknown column separator: '" << c << "'" << endl;
249 col_type n = hh.size();
252 for (col_type col = 0; col < n; ++col)
253 colinfo_[col].align_ = hh[col];
258 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
261 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
262 if (*it == 'c' || *it == 'l' || *it == 'r'||
263 *it == 'p' || *it == 'm' || *it == 'b')
265 // let's have at least one column, even if we did not recognize its
273 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
275 colinfo_[col].align_ = h;
276 if (!colinfo_[col].special_.empty()) {
277 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
278 if (c == 'l' || c == 'c' || c == 'r')
281 // FIXME: Change alignment of p, m and b columns, too
285 char InsetMathGrid::horizontalAlignment(col_type col) const
287 return colinfo_[col].align_;
291 docstring InsetMathGrid::horizontalAlignments() const
294 for (col_type col = 0; col < ncols(); ++col) {
295 if (colinfo_[col].special_.empty()) {
296 res += docstring(colinfo_[col].lines_, '|');
297 res += colinfo_[col].align_;
299 res += colinfo_[col].special_;
301 if (colinfo_[ncols()].special_.empty())
302 return res + docstring(colinfo_[ncols()].lines_, '|');
303 return res + colinfo_[ncols()].special_;
307 void InsetMathGrid::setVerticalAlignment(char c)
313 char InsetMathGrid::verticalAlignment() const
319 InsetMathGrid::col_type InsetMathGrid::ncols() const
321 return colinfo_.size() - 1;
325 InsetMathGrid::row_type InsetMathGrid::nrows() const
327 return rowinfo_.size() - 1;
331 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
333 return idx % ncols();
337 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
339 return idx / ncols();
343 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
345 rowinfo_[row].crskip_ = crskip;
349 Length InsetMathGrid::vcrskip(row_type row) const
351 return rowinfo_[row].crskip_;
355 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
357 // let the cells adjust themselves
358 InsetMathNest::metrics(mi);
360 BufferView & bv = *mi.base.bv;
362 // compute absolute sizes of vertical structure
363 for (row_type row = 0; row < nrows(); ++row) {
366 for (col_type col = 0; col < ncols(); ++col) {
367 Dimension const & dimc = cell(index(row, col)).dimension(bv);
368 asc = max(asc, dimc.asc);
369 desc = max(desc, dimc.des);
371 rowinfo_[row].ascent_ = asc;
372 rowinfo_[row].descent_ = desc;
374 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
375 rowinfo_[nrows()].ascent_ = 0;
376 rowinfo_[nrows()].descent_ = 0;
378 // compute vertical offsets
379 rowinfo_[0].offset_ = 0;
380 for (row_type row = 1; row <= nrows(); ++row) {
381 rowinfo_[row].offset_ =
382 rowinfo_[row - 1].offset_ +
383 rowinfo_[row - 1].descent_ +
384 rowinfo_[row - 1].skipPixels(mi) +
386 rowinfo_[row].lines_ * hlinesep() +
387 rowinfo_[row].ascent_;
390 // adjust vertical offset
397 h = rowinfo_[nrows() - 1].offset_;
400 h = rowinfo_[nrows() - 1].offset_ / 2;
402 for (row_type row = 0; row <= nrows(); ++row)
403 rowinfo_[row].offset_ -= h;
406 // compute absolute sizes of horizontal structure
407 for (col_type col = 0; col < ncols(); ++col) {
409 for (row_type row = 0; row < nrows(); ++row)
410 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
411 colinfo_[col].width_ = wid;
413 colinfo_[ncols()].width_ = 0;
415 // compute horizontal offsets
416 colinfo_[0].offset_ = border();
417 for (col_type col = 1; col <= ncols(); ++col) {
418 colinfo_[col].offset_ =
419 colinfo_[col - 1].offset_ +
420 colinfo_[col - 1].width_ +
421 colinfo_[col - 1].skip_ +
423 colinfo_[col].lines_ * vlinesep();
427 dim.wid = colinfo_[ncols() - 1].offset_
428 + colinfo_[ncols() - 1].width_
429 + vlinesep() * colinfo_[ncols()].lines_
432 dim.asc = - rowinfo_[0].offset_
433 + rowinfo_[0].ascent_
434 + hlinesep() * rowinfo_[0].lines_
437 dim.des = rowinfo_[nrows() - 1].offset_
438 + rowinfo_[nrows() - 1].descent_
439 + hlinesep() * rowinfo_[nrows()].lines_
444 // Increase ws_[i] for 'R' columns (except the first one)
445 for (int i = 1; i < nc_; ++i)
446 if (align_[i] == 'R')
447 ws_[i] += 10 * df_width;
448 // Increase ws_[i] for 'C' column
449 if (align_[0] == 'C')
450 if (ws_[0] < 7 * workwidth / 8)
451 ws_[0] = 7 * workwidth / 8;
455 for (cxrow = row_.begin(); cxrow; ++cxrow) {
458 for (int i = 0; i < nc_; ++i) {
460 if (cxrow->getTab(i) <= 0) {
461 cxrow->setTab(i, df_width);
469 lf = (ws_[i] - cxrow->getTab(i))/2;
473 lf = ws_[i] - cxrow->getTab(i);
476 if (cxrow == row_.begin())
478 else if (cxrow.is_last())
479 lf = ws_[i] - cxrow->getTab(i);
481 lf = (ws_[i] - cxrow->getTab(i))/2;
484 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
485 cxrow->setTab(i, lf + rg);
486 rg = ws_[i] - ww + colsep();
487 if (cxrow == row_.begin())
488 width += ws_[i] + colsep();
490 cxrow->setBaseline(cxrow->getBaseline() - ascent);
493 metricsMarkers2(dim);
494 // Cache the inset dimension.
495 setDimCache(mi, dim);
499 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
501 drawWithMargin(pi, x, y, 1, 1);
505 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
506 int lmargin, int rmargin) const
508 Dimension const dim = dimension(*pi.base.bv);
509 BufferView const & bv = *pi.base.bv;
511 for (idx_type idx = 0; idx < nargs(); ++idx)
512 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
513 y + cellYOffset(idx));
515 for (row_type row = 0; row <= nrows(); ++row)
516 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
517 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
518 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
519 pi.pain.line(x + lmargin + 1, yy,
520 x + dim.width() - rmargin - 1, yy,
524 for (col_type col = 0; col <= ncols(); ++col)
525 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
526 int xx = x + lmargin + colinfo_[col].offset_
527 - i * vlinesep() - vlinesep()/2 - colsep()/2;
528 pi.pain.line(xx, y - dim.ascent() + 1,
529 xx, y + dim.descent() - 1,
532 drawMarkers2(pi, x, y);
536 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
538 // let the cells adjust themselves
539 //InsetMathNest::metrics(mi);
540 for (idx_type i = 0; i < nargs(); ++i)
541 cell(i).metricsT(mi, dim);
543 // compute absolute sizes of vertical structure
544 for (row_type row = 0; row < nrows(); ++row) {
547 for (col_type col = 0; col < ncols(); ++col) {
548 //MathData const & c = cell(index(row, col));
551 asc = max(asc, dimc.ascent());
552 desc = max(desc, dimc.descent());
554 rowinfo_[row].ascent_ = asc;
555 rowinfo_[row].descent_ = desc;
557 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
558 rowinfo_[nrows()].ascent_ = 0;
559 rowinfo_[nrows()].descent_ = 0;
561 // compute vertical offsets
562 rowinfo_[0].offset_ = 0;
563 for (row_type row = 1; row <= nrows(); ++row) {
564 rowinfo_[row].offset_ =
565 rowinfo_[row - 1].offset_ +
566 rowinfo_[row - 1].descent_ +
567 //rowinfo_[row - 1].skipPixels(mi) +
569 //rowinfo_[row].lines_ * hlinesep() +
570 rowinfo_[row].ascent_;
573 // adjust vertical offset
580 h = rowinfo_[nrows() - 1].offset_;
583 h = rowinfo_[nrows() - 1].offset_ / 2;
585 for (row_type row = 0; row <= nrows(); ++row)
586 rowinfo_[row].offset_ -= h;
589 // compute absolute sizes of horizontal structure
590 for (col_type col = 0; col < ncols(); ++col) {
592 for (row_type row = 0; row < nrows(); ++row) {
594 //wid = max(wid, cell(index(row, col)).width());
596 colinfo_[col].width_ = wid;
598 colinfo_[ncols()].width_ = 0;
600 // compute horizontal offsets
601 colinfo_[0].offset_ = border();
602 for (col_type col = 1; col <= ncols(); ++col) {
603 colinfo_[col].offset_ =
604 colinfo_[col - 1].offset_ +
605 colinfo_[col - 1].width_ +
606 colinfo_[col - 1].skip_ +
608 //colinfo_[col].lines_ * vlinesep();
612 dim.wid = colinfo_[ncols() - 1].offset_
613 + colinfo_[ncols() - 1].width_
614 //+ vlinesep() * colinfo_[ncols()].lines_
617 dim.asc = -rowinfo_[0].offset_
618 + rowinfo_[0].ascent_
619 //+ hlinesep() * rowinfo_[0].lines_
622 dim.des = rowinfo_[nrows() - 1].offset_
623 + rowinfo_[nrows() - 1].descent_
624 //+ hlinesep() * rowinfo_[nrows()].lines_
629 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
631 // for (idx_type idx = 0; idx < nargs(); ++idx)
632 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
636 docstring InsetMathGrid::eolString(row_type row, bool fragile) const
640 if (!rowinfo_[row].crskip_.zero())
641 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
642 else if(!rowinfo_[row].allow_newpage_)
645 // make sure an upcoming '[' does not break anything
646 if (row + 1 < nrows()) {
647 MathData const & c = cell(index(row + 1, 0));
648 if (c.size() && c.front()->getChar() == '[')
653 // only add \\ if necessary
654 if (eol.empty() && row + 1 == nrows())
657 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
661 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
663 if (col + 1 == lastcol)
665 return from_ascii(" & ");
669 void InsetMathGrid::addRow(row_type row)
671 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
673 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
675 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
679 void InsetMathGrid::appendRow()
681 rowinfo_.push_back(RowInfo());
682 //cells_.insert(cells_.end(), ncols(), MathData());
683 for (col_type col = 0; col < ncols(); ++col) {
684 cells_.push_back(cells_type::value_type());
685 cellinfo_.push_back(CellInfo());
690 void InsetMathGrid::delRow(row_type row)
695 cells_type::iterator it = cells_.begin() + row * ncols();
696 cells_.erase(it, it + ncols());
698 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
699 cellinfo_.erase(jt, jt + ncols());
701 rowinfo_.erase(rowinfo_.begin() + row);
705 void InsetMathGrid::copyRow(row_type row)
708 for (col_type col = 0; col < ncols(); ++col)
709 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
713 void InsetMathGrid::swapRow(row_type row)
717 if (row + 1 == nrows())
719 for (col_type col = 0; col < ncols(); ++col)
720 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
724 void InsetMathGrid::addCol(col_type newcol)
726 const col_type nc = ncols();
727 const row_type nr = nrows();
728 cells_type new_cells((nc + 1) * nr);
729 vector<CellInfo> new_cellinfo((nc + 1) * nr);
731 for (row_type row = 0; row < nr; ++row)
732 for (col_type col = 0; col < nc; ++col) {
733 new_cells[row * (nc + 1) + col + (col >= newcol)]
734 = cells_[row * nc + col];
735 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
736 = cellinfo_[row * nc + col];
738 swap(cells_, new_cells);
739 swap(cellinfo_, new_cellinfo);
742 inf.skip_ = defaultColSpace(newcol);
743 inf.align_ = defaultColAlign(newcol);
744 colinfo_.insert(colinfo_.begin() + newcol, inf);
748 void InsetMathGrid::delCol(col_type col)
754 vector<CellInfo> tmpcellinfo;
755 for (col_type i = 0; i < nargs(); ++i)
756 if (i % ncols() != col) {
757 tmpcells.push_back(cells_[i]);
758 tmpcellinfo.push_back(cellinfo_[i]);
760 swap(cells_, tmpcells);
761 swap(cellinfo_, tmpcellinfo);
763 colinfo_.erase(colinfo_.begin() + col);
767 void InsetMathGrid::copyCol(col_type col)
770 for (row_type row = 0; row < nrows(); ++row)
771 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
775 void InsetMathGrid::swapCol(col_type col)
779 if (col + 1 == ncols())
781 for (row_type row = 0; row < nrows(); ++row)
782 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
786 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
788 col_type c = col(idx);
789 int x = colinfo_[c].offset_;
790 char align = colinfo_[c].align_;
791 Dimension const & celldim = cell(idx).dimension(bv);
792 if (align == 'r' || align == 'R')
793 x += colinfo_[c].width_ - celldim.wid;
794 if (align == 'c' || align == 'C')
795 x += (colinfo_[c].width_ - celldim.wid) / 2;
800 int InsetMathGrid::cellYOffset(idx_type idx) const
802 return rowinfo_[row(idx)].offset_;
806 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
811 cur.idx() -= ncols();
813 if (cur.row() + 1 >= nrows())
815 cur.idx() += ncols();
817 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
822 bool InsetMathGrid::idxBackward(Cursor & cur) const
824 // leave matrix if at the front edge
828 cur.pos() = cur.lastpos();
833 bool InsetMathGrid::idxForward(Cursor & cur) const
835 // leave matrix if at the back edge
836 if (cur.col() + 1 == ncols())
844 bool InsetMathGrid::idxFirst(Cursor & cur) const
851 cur.idx() = (nrows() - 1) * ncols();
854 cur.idx() = ((nrows() - 1) / 2) * ncols();
861 bool InsetMathGrid::idxLast(Cursor & cur) const
865 cur.idx() = ncols() - 1;
868 cur.idx() = nargs() - 1;
871 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
873 cur.pos() = cur.lastpos();
878 bool InsetMathGrid::idxDelete(idx_type & idx)
880 // nothing to do if we have just one row
884 // nothing to do if we are in the middle of the last row of the inset
885 if (idx + ncols() > nargs())
888 // try to delete entire sequence of ncols() empty cells if possible
889 for (idx_type i = idx; i < idx + ncols(); ++i)
893 // move cells if necessary
894 for (idx_type i = index(row(idx), 0); i < idx; ++i)
895 swap(cell(i), cell(i + ncols()));
902 // undo effect of Ctrl-Tab (i.e. pull next cell)
903 //if (idx + 1 != nargs())
904 // cell(idx).swap(cell(idx + 1));
906 // we handled the event..
911 // reimplement old behaviour when pressing Delete in the last position
913 void InsetMathGrid::idxGlue(idx_type idx)
915 col_type c = col(idx);
916 if (c + 1 == ncols()) {
917 if (row(idx) + 1 != nrows()) {
918 for (col_type cc = 0; cc < ncols(); ++cc)
919 cell(idx).append(cell(idx + cc + 1));
920 delRow(row(idx) + 1);
923 cell(idx).append(cell(idx + 1));
924 for (col_type cc = c + 2; cc < ncols(); ++cc)
925 cell(idx - c + cc - 1) = cell(idx - c + cc);
926 cell(idx - c + ncols() - 1).clear();
931 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
933 return rowinfo_[row];
937 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
939 return rowinfo_[row];
943 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
945 row_type const ri = row(idx);
946 row_type const r1 = min(row(from), row(to));
947 row_type const r2 = max(row(from), row(to));
948 col_type const ci = col(idx);
949 col_type const c1 = min(col(from), col(to));
950 col_type const c2 = max(col(from), col(to));
951 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
956 void InsetMathGrid::normalize(NormalStream & os) const
959 for (row_type row = 0; row < nrows(); ++row) {
961 for (col_type col = 0; col < ncols(); ++col)
962 os << "[cell " << cell(index(row, col)) << ']';
969 void InsetMathGrid::mathmlize(MathStream & os) const
971 os << MTag("mtable");
972 for (row_type row = 0; row < nrows(); ++row) {
974 for (col_type col = 0; col < ncols(); ++col)
975 os << cell(index(row, col));
978 os << ETag("mtable");
982 void InsetMathGrid::write(WriteStream & os) const
984 write(os, 0, 0, nrows(), ncols());
987 void InsetMathGrid::write(WriteStream & os,
988 row_type beg_row, col_type beg_col,
989 row_type end_row, col_type end_col) const
991 MathEnsurer ensurer(os, false);
993 for (row_type row = beg_row; row < end_row; ++row) {
994 os << verboseHLine(rowinfo_[row].lines_);
995 // don't write & and empty cells at end of line
996 col_type lastcol = 0;
997 bool emptyline = true;
998 for (col_type col = beg_col; col < end_col; ++col)
999 if (!cell(index(row, col)).empty()) {
1003 for (col_type col = beg_col; col < lastcol; ++col) {
1004 os << cell(index(row, col));
1005 if (os.pendingBrace())
1006 ModeSpecifier specifier(os, TEXT_MODE);
1007 os << eocString(col, lastcol);
1009 eol = eolString(row, os.fragile());
1011 // append newline only if line wasn't completely empty
1012 // and the formula is not written on a single line
1013 bool const empty = emptyline && eol.empty();
1014 if (!empty && nrows() > 1)
1017 // @TODO use end_row instead of 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;
1077 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1079 switch (cmd.action) {
1081 // insert file functions
1082 case LFUN_LINE_DELETE:
1083 cur.recordUndoInset();
1084 //autocorrect_ = false;
1092 if (cur.idx() > cur.lastidx())
1093 cur.idx() = cur.lastidx();
1094 if (cur.pos() > cur.lastpos())
1095 cur.pos() = cur.lastpos();
1098 case LFUN_CELL_SPLIT:
1103 case LFUN_CELL_BACKWARD:
1105 cur.setSelection(false);
1106 if (!idxPrev(cur)) {
1107 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1112 case LFUN_CELL_FORWARD:
1113 // Can't handle selection by additional 'shift' as this is
1114 // hard bound to LFUN_CELL_BACKWARD
1115 cur.setSelection(false);
1116 if (!idxNext(cur)) {
1117 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1122 case LFUN_NEWLINE_INSERT: {
1123 cur.recordUndoInset();
1124 row_type const r = cur.row();
1128 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1129 swap(cell(index(r, c)), cell(index(r + 1, c)));
1133 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1136 cur.pos() = cur.lastpos();
1138 //mathcursor->normalize();
1139 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1143 case LFUN_TABULAR_FEATURE: {
1144 cur.recordUndoInset();
1145 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1146 istringstream is(to_utf8(cmd.argument()));
1149 if (s == "valign-top")
1150 setVerticalAlignment('t');
1151 else if (s == "valign-middle")
1152 setVerticalAlignment('c');
1153 else if (s == "valign-bottom")
1154 setVerticalAlignment('b');
1155 else if (s == "align-left")
1156 setHorizontalAlignment('l', cur.col());
1157 else if (s == "align-right")
1158 setHorizontalAlignment('r', cur.col());
1159 else if (s == "align-center")
1160 setHorizontalAlignment('c', cur.col());
1161 else if (s == "append-row")
1162 for (int i = 0, n = extractInt(is); i < n; ++i)
1164 else if (s == "delete-row") {
1165 cur.clearSelection(); // bug 4323
1166 for (int i = 0, n = extractInt(is); i < n; ++i) {
1168 if (cur.idx() >= nargs())
1169 cur.idx() -= ncols();
1171 cur.pos() = 0; // trick, see below
1173 else if (s == "copy-row") {
1174 // Here (as later) we save the cursor col/row
1175 // in order to restore it after operation.
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)
1180 cur.idx() = index(r, c);
1182 else if (s == "swap-row") {
1184 // Trick to suppress same-idx-means-different-cell
1188 else if (s == "add-hline-above")
1189 rowinfo_[cur.row()].lines_++;
1190 else if (s == "add-hline-below")
1191 rowinfo_[cur.row()+1].lines_++;
1192 else if (s == "delete-hline-above")
1193 rowinfo_[cur.row()].lines_--;
1194 else if (s == "delete-hline-below")
1195 rowinfo_[cur.row()+1].lines_--;
1196 else if (s == "append-column") {
1197 row_type const r = cur.row();
1198 col_type const c = cur.col();
1199 for (int i = 0, n = extractInt(is); i < n; ++i)
1200 addCol(cur.col() + 1);
1201 cur.idx() = index(r, c);
1203 else if (s == "delete-column") {
1204 cur.clearSelection(); // bug 4323
1205 row_type const r = cur.row();
1206 col_type const c = cur.col();
1207 for (int i = 0, n = extractInt(is); i < n; ++i)
1208 delCol(col(cur.idx()));
1209 cur.idx() = index(r, min(c, cur.ncols() - 1));
1210 cur.pos() = 0; // trick, see above
1212 else if (s == "copy-column") {
1213 row_type const r = cur.row();
1214 col_type const c = cur.col();
1216 cur.idx() = index(r, c);
1218 else if (s == "swap-column") {
1220 cur.pos() = 0; // trick, see above
1222 else if (s == "add-vline-left") {
1223 colinfo_[cur.col()].lines_++;
1224 if (!colinfo_[cur.col()].special_.empty())
1225 colinfo_[cur.col()].special_ += '|';
1227 else if (s == "add-vline-right") {
1228 colinfo_[cur.col()+1].lines_++;
1229 if (!colinfo_[cur.col()+1].special_.empty())
1230 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1232 else if (s == "delete-vline-left") {
1233 colinfo_[cur.col()].lines_--;
1234 docstring & special = colinfo_[cur.col()].special_;
1235 if (!special.empty()) {
1236 docstring::size_type i = special.rfind('|');
1237 LASSERT(i != docstring::npos, /**/);
1238 special.erase(i, 1);
1241 else if (s == "delete-vline-right") {
1242 colinfo_[cur.col()+1].lines_--;
1243 docstring & special = colinfo_[cur.col()+1].special_;
1244 if (!special.empty()) {
1245 docstring::size_type i = special.find('|');
1246 LASSERT(i != docstring::npos, /**/);
1247 special.erase(i, 1);
1254 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1255 //lyxerr << "returning FINISHED_LEFT" << endl;
1259 case LFUN_CLIPBOARD_PASTE:
1260 parseflg |= Parse::VERBATIM;
1263 if (cur.currentMode() <= TEXT_MODE)
1264 parseflg |= Parse::TEXTMODE;
1265 cur.message(_("Paste"));
1266 cap::replaceSelection(cur);
1268 if (cmd.argument().empty() && !theClipboard().isInternal())
1269 topaste = theClipboard().getAsText();
1271 idocstringstream is(cmd.argument());
1274 topaste = cap::selection(n);
1276 InsetMathGrid grid(1, 1);
1277 if (!topaste.empty())
1278 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1279 || !mathed_parse_normal(grid, topaste, parseflg,
1282 mathed_parse_normal(grid, topaste,
1283 parseflg | Parse::VERBATIM,
1287 if (grid.nargs() == 1) {
1288 // single cell/part of cell
1290 cur.cell().insert(cur.pos(), grid.cell(0));
1291 cur.pos() += grid.cell(0).size();
1294 cur.recordUndoInset();
1295 col_type const numcols =
1296 min(grid.ncols(), ncols() - col(cur.idx()));
1297 row_type const numrows =
1298 min(grid.nrows(), nrows() - cur.row());
1299 for (row_type r = 0; r < numrows; ++r) {
1300 for (col_type c = 0; c < numcols; ++c) {
1301 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1302 cell(i).insert(0, grid.cell(grid.index(r, c)));
1304 // append the left over horizontal cells to the last column
1305 idx_type i = index(r + cur.row(), ncols() - 1);
1306 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1307 cell(i).append(grid.cell(grid.index(r, c)));
1309 // append the left over vertical cells to the last _cell_
1310 idx_type i = nargs() - 1;
1311 for (row_type r = numrows; r < grid.nrows(); ++r)
1312 for (col_type c = 0; c < grid.ncols(); ++c)
1313 cell(i).append(grid.cell(grid.index(r, c)));
1315 cur.clearSelection(); // bug 393
1320 case LFUN_LINE_BEGIN_SELECT:
1321 case LFUN_LINE_BEGIN:
1322 case LFUN_WORD_BACKWARD_SELECT:
1323 case LFUN_WORD_BACKWARD:
1324 case LFUN_WORD_LEFT_SELECT:
1325 case LFUN_WORD_LEFT:
1326 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1327 cmd.action == LFUN_WORD_LEFT_SELECT ||
1328 cmd.action == LFUN_LINE_BEGIN_SELECT);
1329 cur.macroModeClose();
1330 if (cur.pos() != 0) {
1332 } else if (cur.idx() % cur.ncols() != 0) {
1333 cur.idx() -= cur.idx() % cur.ncols();
1335 } else if (cur.idx() != 0) {
1339 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1344 case LFUN_WORD_FORWARD_SELECT:
1345 case LFUN_WORD_FORWARD:
1346 case LFUN_WORD_RIGHT_SELECT:
1347 case LFUN_WORD_RIGHT:
1348 case LFUN_LINE_END_SELECT:
1350 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1351 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1352 cmd.action == LFUN_LINE_END_SELECT);
1353 cur.macroModeClose();
1355 if (cur.pos() != cur.lastpos()) {
1356 cur.pos() = cur.lastpos();
1357 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1358 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1359 cur.pos() = cur.lastpos();
1360 } else if (cur.idx() != cur.lastidx()) {
1361 cur.idx() = cur.lastidx();
1362 cur.pos() = cur.lastpos();
1364 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1370 InsetMathNest::doDispatch(cur, cmd);
1375 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1376 FuncStatus & status) const
1378 switch (cmd.action) {
1379 case LFUN_TABULAR_FEATURE: {
1380 string const s = cmd.getArg(0);
1381 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1382 status.setEnabled(false);
1383 status.message(from_utf8(N_("Only one row")));
1387 (s == "delete-column" || s == "swap-column")) {
1388 status.setEnabled(false);
1389 status.message(from_utf8(N_("Only one column")));
1392 if ((rowinfo_[cur.row()].lines_ == 0 &&
1393 s == "delete-hline-above") ||
1394 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1395 s == "delete-hline-below")) {
1396 status.setEnabled(false);
1397 status.message(from_utf8(N_("No hline to delete")));
1401 if ((colinfo_[cur.col()].lines_ == 0 &&
1402 s == "delete-vline-left") ||
1403 (colinfo_[cur.col() + 1].lines_ == 0 &&
1404 s == "delete-vline-right")) {
1405 status.setEnabled(false);
1406 status.message(from_utf8(N_("No vline to delete")));
1409 if (s == "valign-top" || s == "valign-middle" ||
1410 s == "valign-bottom" || s == "align-left" ||
1411 s == "align-right" || s == "align-center") {
1412 status.setEnabled(true);
1413 char const ha = horizontalAlignment(cur.col());
1414 char const va = verticalAlignment();
1415 status.setOnOff((s == "align-left" && ha == 'l')
1416 || (s == "align-right" && ha == 'r')
1417 || (s == "align-center" && ha == 'c')
1418 || (s == "valign-top" && va == 't')
1419 || (s == "valign-bottom" && va == 'b')
1420 || (s == "valign-middle" && va == 'c'));
1423 if (s == "append-row" || s == "delete-row" ||
1424 s == "copy-row" || s == "swap-row" ||
1425 s == "add-hline-above" || s == "add-hline-below" ||
1426 s == "delete-hline-above" || s == "delete-hline-below" ||
1427 s == "append-column" || s == "delete-column" ||
1428 s == "copy-column" || s == "swap-column" ||
1429 s == "add-vline-left" || s == "add-vline-right" ||
1430 s == "delete-vline-left" || s == "delete-vline-right") {
1431 status.setEnabled(true);
1433 status.setEnabled(false);
1434 status.message(bformat(
1435 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1439 // FIXME: What did this code do?
1440 // Please check whether it is still needed!
1441 // should be more precise
1442 if (v_align_ == '\0') {
1443 status.enable(true);
1446 if (cmd.argument().empty()) {
1447 status.enable(false);
1450 if (!contains("tcb", cmd.argument()[0])) {
1451 status.enable(false);
1454 status.setOnOff(cmd.argument()[0] == v_align_);
1455 status.setEnabled(true);
1460 case LFUN_CELL_SPLIT:
1461 status.setEnabled(true);
1464 case LFUN_CELL_BACKWARD:
1465 case LFUN_CELL_FORWARD:
1466 status.setEnabled(true);
1470 return InsetMathNest::getStatus(cur, cmd, status);