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 emptyline, 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() && (nrows() == 1 || !emptyline))
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, emptyline, os.fragile());
1011 // append newline only if line wasn't completely empty
1012 // and this was not the last line in the grid
1013 if (!emptyline && row + 1 < end_row)
1016 // @TODO use end_row instead of nrows() ?
1017 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1029 int InsetMathGrid::colsep() const
1035 int InsetMathGrid::rowsep() const
1041 int InsetMathGrid::hlinesep() const
1047 int InsetMathGrid::vlinesep() const
1053 int InsetMathGrid::border() const
1059 void InsetMathGrid::splitCell(Cursor & cur)
1061 if (cur.idx() == cur.lastidx())
1063 MathData ar = cur.cell();
1064 ar.erase(0, cur.pos());
1065 cur.cell().erase(cur.pos(), cur.lastpos());
1068 cur.cell().insert(0, ar);
1072 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1074 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1076 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1078 switch (cmd.action) {
1080 // insert file functions
1081 case LFUN_LINE_DELETE:
1082 cur.recordUndoInset();
1083 //autocorrect_ = false;
1091 if (cur.idx() > cur.lastidx())
1092 cur.idx() = cur.lastidx();
1093 if (cur.pos() > cur.lastpos())
1094 cur.pos() = cur.lastpos();
1097 case LFUN_CELL_SPLIT:
1102 case LFUN_CELL_BACKWARD:
1104 cur.setSelection(false);
1105 if (!idxPrev(cur)) {
1106 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1111 case LFUN_CELL_FORWARD:
1112 // Can't handle selection by additional 'shift' as this is
1113 // hard bound to LFUN_CELL_BACKWARD
1114 cur.setSelection(false);
1115 if (!idxNext(cur)) {
1116 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1121 case LFUN_NEWLINE_INSERT: {
1122 cur.recordUndoInset();
1123 row_type const r = cur.row();
1127 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1128 swap(cell(index(r, c)), cell(index(r + 1, c)));
1132 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1135 cur.pos() = cur.lastpos();
1137 //mathcursor->normalize();
1138 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1142 case LFUN_TABULAR_FEATURE: {
1143 cur.recordUndoInset();
1144 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1145 istringstream is(to_utf8(cmd.argument()));
1148 if (s == "valign-top")
1149 setVerticalAlignment('t');
1150 else if (s == "valign-middle")
1151 setVerticalAlignment('c');
1152 else if (s == "valign-bottom")
1153 setVerticalAlignment('b');
1154 else if (s == "align-left")
1155 setHorizontalAlignment('l', cur.col());
1156 else if (s == "align-right")
1157 setHorizontalAlignment('r', cur.col());
1158 else if (s == "align-center")
1159 setHorizontalAlignment('c', cur.col());
1160 else if (s == "append-row")
1161 for (int i = 0, n = extractInt(is); i < n; ++i)
1163 else if (s == "delete-row") {
1164 cur.clearSelection(); // bug 4323
1165 for (int i = 0, n = extractInt(is); i < n; ++i) {
1167 if (cur.idx() >= nargs())
1168 cur.idx() -= ncols();
1170 cur.pos() = 0; // trick, see below
1172 else if (s == "copy-row") {
1173 // Here (as later) we save the cursor col/row
1174 // in order to restore it after operation.
1175 row_type const r = cur.row();
1176 col_type const c = cur.col();
1177 for (int i = 0, n = extractInt(is); i < n; ++i)
1179 cur.idx() = index(r, c);
1181 else if (s == "swap-row") {
1183 // Trick to suppress same-idx-means-different-cell
1187 else if (s == "add-hline-above")
1188 rowinfo_[cur.row()].lines_++;
1189 else if (s == "add-hline-below")
1190 rowinfo_[cur.row()+1].lines_++;
1191 else if (s == "delete-hline-above")
1192 rowinfo_[cur.row()].lines_--;
1193 else if (s == "delete-hline-below")
1194 rowinfo_[cur.row()+1].lines_--;
1195 else if (s == "append-column") {
1196 row_type const r = cur.row();
1197 col_type const c = cur.col();
1198 for (int i = 0, n = extractInt(is); i < n; ++i)
1199 addCol(cur.col() + 1);
1200 cur.idx() = index(r, c);
1202 else if (s == "delete-column") {
1203 cur.clearSelection(); // bug 4323
1204 row_type const r = cur.row();
1205 col_type const c = cur.col();
1206 for (int i = 0, n = extractInt(is); i < n; ++i)
1207 delCol(col(cur.idx()));
1208 cur.idx() = index(r, min(c, cur.ncols() - 1));
1209 cur.pos() = 0; // trick, see above
1211 else if (s == "copy-column") {
1212 row_type const r = cur.row();
1213 col_type const c = cur.col();
1215 cur.idx() = index(r, c);
1217 else if (s == "swap-column") {
1219 cur.pos() = 0; // trick, see above
1221 else if (s == "add-vline-left") {
1222 colinfo_[cur.col()].lines_++;
1223 if (!colinfo_[cur.col()].special_.empty())
1224 colinfo_[cur.col()].special_ += '|';
1226 else if (s == "add-vline-right") {
1227 colinfo_[cur.col()+1].lines_++;
1228 if (!colinfo_[cur.col()+1].special_.empty())
1229 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1231 else if (s == "delete-vline-left") {
1232 colinfo_[cur.col()].lines_--;
1233 docstring & special = colinfo_[cur.col()].special_;
1234 if (!special.empty()) {
1235 docstring::size_type i = special.rfind('|');
1236 LASSERT(i != docstring::npos, /**/);
1237 special.erase(i, 1);
1240 else if (s == "delete-vline-right") {
1241 colinfo_[cur.col()+1].lines_--;
1242 docstring & special = colinfo_[cur.col()+1].special_;
1243 if (!special.empty()) {
1244 docstring::size_type i = special.find('|');
1245 LASSERT(i != docstring::npos, /**/);
1246 special.erase(i, 1);
1253 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1254 //lyxerr << "returning FINISHED_LEFT" << endl;
1258 case LFUN_CLIPBOARD_PASTE:
1259 parseflg |= Parse::VERBATIM;
1262 if (cur.currentMode() <= TEXT_MODE)
1263 parseflg |= Parse::TEXTMODE;
1264 cur.message(_("Paste"));
1265 cap::replaceSelection(cur);
1267 if (cmd.argument().empty() && !theClipboard().isInternal())
1268 topaste = theClipboard().getAsText();
1270 idocstringstream is(cmd.argument());
1273 topaste = cap::selection(n);
1275 InsetMathGrid grid(1, 1);
1276 if (!topaste.empty())
1277 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1278 || !mathed_parse_normal(grid, topaste, parseflg)) {
1280 mathed_parse_normal(grid, topaste,
1281 parseflg | Parse::VERBATIM);
1284 if (grid.nargs() == 1) {
1285 // single cell/part of cell
1287 cur.cell().insert(cur.pos(), grid.cell(0));
1288 cur.pos() += grid.cell(0).size();
1291 cur.recordUndoInset();
1292 col_type const numcols =
1293 min(grid.ncols(), ncols() - col(cur.idx()));
1294 row_type const numrows =
1295 min(grid.nrows(), nrows() - cur.row());
1296 for (row_type r = 0; r < numrows; ++r) {
1297 for (col_type c = 0; c < numcols; ++c) {
1298 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1299 cell(i).insert(0, grid.cell(grid.index(r, c)));
1301 // append the left over horizontal cells to the last column
1302 idx_type i = index(r + cur.row(), ncols() - 1);
1303 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1304 cell(i).append(grid.cell(grid.index(r, c)));
1306 // append the left over vertical cells to the last _cell_
1307 idx_type i = nargs() - 1;
1308 for (row_type r = numrows; r < grid.nrows(); ++r)
1309 for (col_type c = 0; c < grid.ncols(); ++c)
1310 cell(i).append(grid.cell(grid.index(r, c)));
1312 cur.clearSelection(); // bug 393
1317 case LFUN_LINE_BEGIN_SELECT:
1318 case LFUN_LINE_BEGIN:
1319 case LFUN_WORD_BACKWARD_SELECT:
1320 case LFUN_WORD_BACKWARD:
1321 case LFUN_WORD_LEFT_SELECT:
1322 case LFUN_WORD_LEFT:
1323 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1324 cmd.action == LFUN_WORD_LEFT_SELECT ||
1325 cmd.action == LFUN_LINE_BEGIN_SELECT);
1326 cur.macroModeClose();
1327 if (cur.pos() != 0) {
1329 } else if (cur.idx() % cur.ncols() != 0) {
1330 cur.idx() -= cur.idx() % cur.ncols();
1332 } else if (cur.idx() != 0) {
1336 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1341 case LFUN_WORD_FORWARD_SELECT:
1342 case LFUN_WORD_FORWARD:
1343 case LFUN_WORD_RIGHT_SELECT:
1344 case LFUN_WORD_RIGHT:
1345 case LFUN_LINE_END_SELECT:
1347 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1348 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1349 cmd.action == LFUN_LINE_END_SELECT);
1350 cur.macroModeClose();
1352 if (cur.pos() != cur.lastpos()) {
1353 cur.pos() = cur.lastpos();
1354 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1355 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1356 cur.pos() = cur.lastpos();
1357 } else if (cur.idx() != cur.lastidx()) {
1358 cur.idx() = cur.lastidx();
1359 cur.pos() = cur.lastpos();
1361 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1367 InsetMathNest::doDispatch(cur, cmd);
1372 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1373 FuncStatus & status) const
1375 switch (cmd.action) {
1376 case LFUN_TABULAR_FEATURE: {
1377 string const s = cmd.getArg(0);
1378 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1379 status.setEnabled(false);
1380 status.message(from_utf8(N_("Only one row")));
1384 (s == "delete-column" || s == "swap-column")) {
1385 status.setEnabled(false);
1386 status.message(from_utf8(N_("Only one column")));
1389 if ((rowinfo_[cur.row()].lines_ == 0 &&
1390 s == "delete-hline-above") ||
1391 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1392 s == "delete-hline-below")) {
1393 status.setEnabled(false);
1394 status.message(from_utf8(N_("No hline to delete")));
1398 if ((colinfo_[cur.col()].lines_ == 0 &&
1399 s == "delete-vline-left") ||
1400 (colinfo_[cur.col() + 1].lines_ == 0 &&
1401 s == "delete-vline-right")) {
1402 status.setEnabled(false);
1403 status.message(from_utf8(N_("No vline to delete")));
1406 if (s == "valign-top" || s == "valign-middle" ||
1407 s == "valign-bottom" || s == "align-left" ||
1408 s == "align-right" || s == "align-center") {
1409 status.setEnabled(true);
1410 char const ha = horizontalAlignment(cur.col());
1411 char const va = verticalAlignment();
1412 status.setOnOff((s == "align-left" && ha == 'l')
1413 || (s == "align-right" && ha == 'r')
1414 || (s == "align-center" && ha == 'c')
1415 || (s == "valign-top" && va == 't')
1416 || (s == "valign-bottom" && va == 'b')
1417 || (s == "valign-middle" && va == 'c'));
1420 if (s == "append-row" || s == "delete-row" ||
1421 s == "copy-row" || s == "swap-row" ||
1422 s == "add-hline-above" || s == "add-hline-below" ||
1423 s == "delete-hline-above" || s == "delete-hline-below" ||
1424 s == "append-column" || s == "delete-column" ||
1425 s == "copy-column" || s == "swap-column" ||
1426 s == "add-vline-left" || s == "add-vline-right" ||
1427 s == "delete-vline-left" || s == "delete-vline-right") {
1428 status.setEnabled(true);
1430 status.setEnabled(false);
1431 status.message(bformat(
1432 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1436 // FIXME: What did this code do?
1437 // Please check whether it is still needed!
1438 // should be more precise
1439 if (v_align_ == '\0') {
1440 status.enable(true);
1443 if (cmd.argument().empty()) {
1444 status.enable(false);
1447 if (!contains("tcb", cmd.argument()[0])) {
1448 status.enable(false);
1451 status.setOnOff(cmd.argument()[0] == v_align_);
1452 status.setEnabled(true);
1457 case LFUN_CELL_SPLIT:
1458 status.setEnabled(true);
1461 case LFUN_CELL_BACKWARD:
1462 case LFUN_CELL_FORWARD:
1463 status.setEnabled(true);
1467 return InsetMathNest::getStatus(cur, cmd, status);