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;
66 //////////////////////////////////////////////////////////////
69 InsetMathGrid::CellInfo::CellInfo()
75 //////////////////////////////////////////////////////////////
78 InsetMathGrid::RowInfo::RowInfo()
79 : lines_(0), skip_(0), allow_newpage_(true)
84 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
86 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
87 return crskip_.inPixels(mi.base.textwidth,
88 fm.width(char_type('M')));
93 //////////////////////////////////////////////////////////////
96 InsetMathGrid::ColInfo::ColInfo()
97 : align_('c'), lines_(0)
101 //////////////////////////////////////////////////////////////
104 InsetMathGrid::InsetMathGrid()
115 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
116 : InsetMathNest(m * n),
126 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
127 : InsetMathNest(m * n),
134 setVerticalAlignment(v);
135 setHorizontalAlignments(h);
139 Inset * InsetMathGrid::clone() const
141 return new InsetMathGrid(*this);
145 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
147 return col + ncols() * row;
151 void InsetMathGrid::setDefaults()
154 lyxerr << "positive number of columns expected" << endl;
156 // lyxerr << "positive number of rows expected" << endl;
157 for (col_type col = 0; col < ncols(); ++col) {
158 colinfo_[col].align_ = defaultColAlign(col);
159 colinfo_[col].skip_ = defaultColSpace(col);
160 colinfo_[col].special_.clear();
165 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
168 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
171 colinfo_[col].lines_++;
172 } else if ((c == 'p' || c == 'm' || c == 'b'||
173 c == '!' || c == '@' || c == '>' || c == '<') &&
174 it + 1 != hh.end() && *(it + 1) == '{') {
175 // @{decl.} and p{width} are standard LaTeX, the
176 // others are extensions by array.sty
177 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
179 // this declares a new column
181 // Only intercolumn stuff is allowed
182 // in the last dummy column
184 colinfo_[col].align_ = 'l';
186 // this is intercolumn stuff
187 if (colinfo_[col].special_.empty())
188 // Overtake possible lines
189 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
193 while (it != hh.end()) {
195 colinfo_[col].special_ += c;
201 if (brace_open > 0 && brace_open == brace_close)
206 colinfo_[col].lines_ = count(
207 colinfo_[col].special_.begin(),
208 colinfo_[col].special_.end(), '|');
209 LYXERR(Debug::MATHED, "special column separator: `"
210 << to_utf8(colinfo_[col].special_) << '\'');
212 colinfo_[col].lines_ = 0;
213 colinfo_[col].special_.clear();
215 } else if (col >= ncols()) {
216 // Only intercolumn stuff is allowed in the last
219 } else if (c == 'c' || c == 'l' || c == 'r') {
220 colinfo_[col].align_ = static_cast<char>(c);
221 if (!colinfo_[col].special_.empty()) {
222 colinfo_[col].special_ += c;
223 colinfo_[col].lines_ = count(
224 colinfo_[col].special_.begin(),
225 colinfo_[col].special_.end(), '|');
226 LYXERR(Debug::MATHED, "special column separator: `"
227 << to_utf8(colinfo_[col].special_) << '\'');
230 colinfo_[col].lines_ = 0;
231 colinfo_[col].special_.clear();
233 lyxerr << "unknown column separator: '" << c << "'" << endl;
238 col_type n = hh.size();
241 for (col_type col = 0; col < n; ++col)
242 colinfo_[col].align_ = hh[col];
247 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
250 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
251 if (*it == 'c' || *it == 'l' || *it == 'r'||
252 *it == 'p' || *it == 'm' || *it == 'b')
254 // let's have at least one column, even if we did not recognize its
262 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
264 colinfo_[col].align_ = h;
265 if (!colinfo_[col].special_.empty()) {
266 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
267 if (c == 'l' || c == 'c' || c == 'r')
270 // FIXME: Change alignment of p, m and b columns, too
274 char InsetMathGrid::horizontalAlignment(col_type col) const
276 return colinfo_[col].align_;
280 docstring InsetMathGrid::horizontalAlignments() const
283 for (col_type col = 0; col < ncols(); ++col) {
284 if (colinfo_[col].special_.empty()) {
285 res += docstring(colinfo_[col].lines_, '|');
286 res += colinfo_[col].align_;
288 res += colinfo_[col].special_;
290 if (colinfo_[ncols()].special_.empty())
291 return res + docstring(colinfo_[ncols()].lines_, '|');
292 return res + colinfo_[ncols()].special_;
296 void InsetMathGrid::setVerticalAlignment(char c)
302 char InsetMathGrid::verticalAlignment() const
308 InsetMathGrid::col_type InsetMathGrid::ncols() const
310 return colinfo_.size() - 1;
314 InsetMathGrid::row_type InsetMathGrid::nrows() const
316 return rowinfo_.size() - 1;
320 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
322 return idx % ncols();
326 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
328 return idx / ncols();
332 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
334 rowinfo_[row].crskip_ = crskip;
338 Length InsetMathGrid::vcrskip(row_type row) const
340 return rowinfo_[row].crskip_;
344 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
346 // let the cells adjust themselves
347 InsetMathNest::metrics(mi);
349 BufferView & bv = *mi.base.bv;
351 // compute absolute sizes of vertical structure
352 for (row_type row = 0; row < nrows(); ++row) {
355 for (col_type col = 0; col < ncols(); ++col) {
356 Dimension const & dimc = cell(index(row, col)).dimension(bv);
357 asc = max(asc, dimc.asc);
358 desc = max(desc, dimc.des);
360 rowinfo_[row].ascent_ = asc;
361 rowinfo_[row].descent_ = desc;
363 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
364 rowinfo_[nrows()].ascent_ = 0;
365 rowinfo_[nrows()].descent_ = 0;
367 // compute vertical offsets
368 rowinfo_[0].offset_ = 0;
369 for (row_type row = 1; row <= nrows(); ++row) {
370 rowinfo_[row].offset_ =
371 rowinfo_[row - 1].offset_ +
372 rowinfo_[row - 1].descent_ +
373 rowinfo_[row - 1].skipPixels(mi) +
375 rowinfo_[row].lines_ * hlinesep() +
376 rowinfo_[row].ascent_;
379 // adjust vertical offset
386 h = rowinfo_[nrows() - 1].offset_;
389 h = rowinfo_[nrows() - 1].offset_ / 2;
391 for (row_type row = 0; row <= nrows(); ++row)
392 rowinfo_[row].offset_ -= h;
395 // compute absolute sizes of horizontal structure
396 for (col_type col = 0; col < ncols(); ++col) {
398 for (row_type row = 0; row < nrows(); ++row)
399 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
400 colinfo_[col].width_ = wid;
402 colinfo_[ncols()].width_ = 0;
404 // compute horizontal offsets
405 colinfo_[0].offset_ = border();
406 for (col_type col = 1; col <= ncols(); ++col) {
407 colinfo_[col].offset_ =
408 colinfo_[col - 1].offset_ +
409 colinfo_[col - 1].width_ +
410 colinfo_[col - 1].skip_ +
412 colinfo_[col].lines_ * vlinesep();
416 dim.wid = colinfo_[ncols() - 1].offset_
417 + colinfo_[ncols() - 1].width_
418 + vlinesep() * colinfo_[ncols()].lines_
421 dim.asc = - rowinfo_[0].offset_
422 + rowinfo_[0].ascent_
423 + hlinesep() * rowinfo_[0].lines_
426 dim.des = rowinfo_[nrows() - 1].offset_
427 + rowinfo_[nrows() - 1].descent_
428 + hlinesep() * rowinfo_[nrows()].lines_
433 // Increase ws_[i] for 'R' columns (except the first one)
434 for (int i = 1; i < nc_; ++i)
435 if (align_[i] == 'R')
436 ws_[i] += 10 * df_width;
437 // Increase ws_[i] for 'C' column
438 if (align_[0] == 'C')
439 if (ws_[0] < 7 * workwidth / 8)
440 ws_[0] = 7 * workwidth / 8;
444 for (cxrow = row_.begin(); cxrow; ++cxrow) {
447 for (int i = 0; i < nc_; ++i) {
449 if (cxrow->getTab(i) <= 0) {
450 cxrow->setTab(i, df_width);
458 lf = (ws_[i] - cxrow->getTab(i))/2;
462 lf = ws_[i] - cxrow->getTab(i);
465 if (cxrow == row_.begin())
467 else if (cxrow.is_last())
468 lf = ws_[i] - cxrow->getTab(i);
470 lf = (ws_[i] - cxrow->getTab(i))/2;
473 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
474 cxrow->setTab(i, lf + rg);
475 rg = ws_[i] - ww + colsep();
476 if (cxrow == row_.begin())
477 width += ws_[i] + colsep();
479 cxrow->setBaseline(cxrow->getBaseline() - ascent);
482 metricsMarkers2(dim);
483 // Cache the inset dimension.
484 setDimCache(mi, dim);
488 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
490 drawWithMargin(pi, x, y, 1, 1);
494 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
495 int lmargin, int rmargin) const
497 Dimension const dim = dimension(*pi.base.bv);
498 BufferView const & bv = *pi.base.bv;
500 for (idx_type idx = 0; idx < nargs(); ++idx)
501 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
502 y + cellYOffset(idx));
504 for (row_type row = 0; row <= nrows(); ++row)
505 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
506 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
507 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
508 pi.pain.line(x + lmargin + 1, yy,
509 x + dim.width() - rmargin - 1, yy,
513 for (col_type col = 0; col <= ncols(); ++col)
514 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
515 int xx = x + lmargin + colinfo_[col].offset_
516 - i * vlinesep() - vlinesep()/2 - colsep()/2;
517 pi.pain.line(xx, y - dim.ascent() + 1,
518 xx, y + dim.descent() - 1,
521 drawMarkers2(pi, x, y);
525 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
527 // let the cells adjust themselves
528 //InsetMathNest::metrics(mi);
529 for (idx_type i = 0; i < nargs(); ++i)
530 cell(i).metricsT(mi, dim);
532 // compute absolute sizes of vertical structure
533 for (row_type row = 0; row < nrows(); ++row) {
536 for (col_type col = 0; col < ncols(); ++col) {
537 //MathData const & c = cell(index(row, col));
540 asc = max(asc, dimc.ascent());
541 desc = max(desc, dimc.descent());
543 rowinfo_[row].ascent_ = asc;
544 rowinfo_[row].descent_ = desc;
546 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
547 rowinfo_[nrows()].ascent_ = 0;
548 rowinfo_[nrows()].descent_ = 0;
550 // compute vertical offsets
551 rowinfo_[0].offset_ = 0;
552 for (row_type row = 1; row <= nrows(); ++row) {
553 rowinfo_[row].offset_ =
554 rowinfo_[row - 1].offset_ +
555 rowinfo_[row - 1].descent_ +
556 //rowinfo_[row - 1].skipPixels(mi) +
558 //rowinfo_[row].lines_ * hlinesep() +
559 rowinfo_[row].ascent_;
562 // adjust vertical offset
569 h = rowinfo_[nrows() - 1].offset_;
572 h = rowinfo_[nrows() - 1].offset_ / 2;
574 for (row_type row = 0; row <= nrows(); ++row)
575 rowinfo_[row].offset_ -= h;
578 // compute absolute sizes of horizontal structure
579 for (col_type col = 0; col < ncols(); ++col) {
581 for (row_type row = 0; row < nrows(); ++row) {
583 //wid = max(wid, cell(index(row, col)).width());
585 colinfo_[col].width_ = wid;
587 colinfo_[ncols()].width_ = 0;
589 // compute horizontal offsets
590 colinfo_[0].offset_ = border();
591 for (col_type col = 1; col <= ncols(); ++col) {
592 colinfo_[col].offset_ =
593 colinfo_[col - 1].offset_ +
594 colinfo_[col - 1].width_ +
595 colinfo_[col - 1].skip_ +
597 //colinfo_[col].lines_ * vlinesep();
601 dim.wid = colinfo_[ncols() - 1].offset_
602 + colinfo_[ncols() - 1].width_
603 //+ vlinesep() * colinfo_[ncols()].lines_
606 dim.asc = -rowinfo_[0].offset_
607 + rowinfo_[0].ascent_
608 //+ hlinesep() * rowinfo_[0].lines_
611 dim.des = rowinfo_[nrows() - 1].offset_
612 + rowinfo_[nrows() - 1].descent_
613 //+ hlinesep() * rowinfo_[nrows()].lines_
618 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
620 // for (idx_type idx = 0; idx < nargs(); ++idx)
621 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
625 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
629 if (!rowinfo_[row].crskip_.zero())
630 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
631 else if(!rowinfo_[row].allow_newpage_)
634 // make sure an upcoming '[' does not break anything
635 if (row + 1 < nrows()) {
636 MathData const & c = cell(index(row + 1, 0));
637 if (c.size() && c.front()->getChar() == '[')
642 // only add \\ if necessary
643 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
646 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
650 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
652 if (col + 1 == lastcol)
654 return from_ascii(" & ");
658 void InsetMathGrid::addRow(row_type row)
660 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
662 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
664 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
668 void InsetMathGrid::appendRow()
670 rowinfo_.push_back(RowInfo());
671 //cells_.insert(cells_.end(), ncols(), MathData());
672 for (col_type col = 0; col < ncols(); ++col) {
673 cells_.push_back(cells_type::value_type());
674 cellinfo_.push_back(CellInfo());
679 void InsetMathGrid::delRow(row_type row)
684 cells_type::iterator it = cells_.begin() + row * ncols();
685 cells_.erase(it, it + ncols());
687 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
688 cellinfo_.erase(jt, jt + ncols());
690 rowinfo_.erase(rowinfo_.begin() + row);
694 void InsetMathGrid::copyRow(row_type row)
697 for (col_type col = 0; col < ncols(); ++col)
698 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
702 void InsetMathGrid::swapRow(row_type row)
706 if (row + 1 == nrows())
708 for (col_type col = 0; col < ncols(); ++col)
709 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
713 void InsetMathGrid::addCol(col_type newcol)
715 const col_type nc = ncols();
716 const row_type nr = nrows();
717 cells_type new_cells((nc + 1) * nr);
718 vector<CellInfo> new_cellinfo((nc + 1) * nr);
720 for (row_type row = 0; row < nr; ++row)
721 for (col_type col = 0; col < nc; ++col) {
722 new_cells[row * (nc + 1) + col + (col >= newcol)]
723 = cells_[row * nc + col];
724 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
725 = cellinfo_[row * nc + col];
727 swap(cells_, new_cells);
728 swap(cellinfo_, new_cellinfo);
731 inf.skip_ = defaultColSpace(newcol);
732 inf.align_ = defaultColAlign(newcol);
733 colinfo_.insert(colinfo_.begin() + newcol, inf);
737 void InsetMathGrid::delCol(col_type col)
743 vector<CellInfo> tmpcellinfo;
744 for (col_type i = 0; i < nargs(); ++i)
745 if (i % ncols() != col) {
746 tmpcells.push_back(cells_[i]);
747 tmpcellinfo.push_back(cellinfo_[i]);
749 swap(cells_, tmpcells);
750 swap(cellinfo_, tmpcellinfo);
752 colinfo_.erase(colinfo_.begin() + col);
756 void InsetMathGrid::copyCol(col_type col)
759 for (row_type row = 0; row < nrows(); ++row)
760 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
764 void InsetMathGrid::swapCol(col_type col)
768 if (col + 1 == ncols())
770 for (row_type row = 0; row < nrows(); ++row)
771 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
775 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
777 col_type c = col(idx);
778 int x = colinfo_[c].offset_;
779 char align = colinfo_[c].align_;
780 Dimension const & celldim = cell(idx).dimension(bv);
781 if (align == 'r' || align == 'R')
782 x += colinfo_[c].width_ - celldim.wid;
783 if (align == 'c' || align == 'C')
784 x += (colinfo_[c].width_ - celldim.wid) / 2;
789 int InsetMathGrid::cellYOffset(idx_type idx) const
791 return rowinfo_[row(idx)].offset_;
795 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
800 cur.idx() -= ncols();
802 if (cur.row() + 1 >= nrows())
804 cur.idx() += ncols();
806 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
811 bool InsetMathGrid::idxBackward(Cursor & cur) const
813 // leave matrix if at the front edge
817 cur.pos() = cur.lastpos();
822 bool InsetMathGrid::idxForward(Cursor & cur) const
824 // leave matrix if at the back edge
825 if (cur.col() + 1 == ncols())
833 bool InsetMathGrid::idxFirst(Cursor & cur) const
840 cur.idx() = (nrows() - 1) * ncols();
843 cur.idx() = ((nrows() - 1) / 2) * ncols();
850 bool InsetMathGrid::idxLast(Cursor & cur) const
854 cur.idx() = ncols() - 1;
857 cur.idx() = nargs() - 1;
860 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
862 cur.pos() = cur.lastpos();
867 bool InsetMathGrid::idxDelete(idx_type & idx)
869 // nothing to do if we have just one row
873 // nothing to do if we are in the middle of the last row of the inset
874 if (idx + ncols() > nargs())
877 // try to delete entire sequence of ncols() empty cells if possible
878 for (idx_type i = idx; i < idx + ncols(); ++i)
882 // move cells if necessary
883 for (idx_type i = index(row(idx), 0); i < idx; ++i)
884 swap(cell(i), cell(i + ncols()));
891 // undo effect of Ctrl-Tab (i.e. pull next cell)
892 //if (idx + 1 != nargs())
893 // cell(idx).swap(cell(idx + 1));
895 // we handled the event..
900 // reimplement old behaviour when pressing Delete in the last position
902 void InsetMathGrid::idxGlue(idx_type idx)
904 col_type c = col(idx);
905 if (c + 1 == ncols()) {
906 if (row(idx) + 1 != nrows()) {
907 for (col_type cc = 0; cc < ncols(); ++cc)
908 cell(idx).append(cell(idx + cc + 1));
909 delRow(row(idx) + 1);
912 cell(idx).append(cell(idx + 1));
913 for (col_type cc = c + 2; cc < ncols(); ++cc)
914 cell(idx - c + cc - 1) = cell(idx - c + cc);
915 cell(idx - c + ncols() - 1).clear();
920 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
922 return rowinfo_[row];
926 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
928 return rowinfo_[row];
932 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
934 row_type const ri = row(idx);
935 row_type const r1 = min(row(from), row(to));
936 row_type const r2 = max(row(from), row(to));
937 col_type const ci = col(idx);
938 col_type const c1 = min(col(from), col(to));
939 col_type const c2 = max(col(from), col(to));
940 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
945 void InsetMathGrid::normalize(NormalStream & os) const
948 for (row_type row = 0; row < nrows(); ++row) {
950 for (col_type col = 0; col < ncols(); ++col)
951 os << "[cell " << cell(index(row, col)) << ']';
958 void InsetMathGrid::mathmlize(MathStream & os) const
960 os << MTag("mtable");
961 for (row_type row = 0; row < nrows(); ++row) {
963 for (col_type col = 0; col < ncols(); ++col)
964 os << cell(index(row, col));
967 os << ETag("mtable");
971 void InsetMathGrid::write(WriteStream & os) const
973 MathEnsurer ensurer(os, false);
975 for (row_type row = 0; row < nrows(); ++row) {
976 os << verboseHLine(rowinfo_[row].lines_);
977 // don't write & and empty cells at end of line
978 col_type lastcol = 0;
979 bool emptyline = true;
980 for (col_type col = 0; col < ncols(); ++col)
981 if (!cell(index(row, col)).empty()) {
985 for (col_type col = 0; col < lastcol; ++col) {
986 os << cell(index(row, col));
987 if (os.pendingBrace())
988 ModeSpecifier specifier(os, TEXT_MODE);
989 os << eocString(col, lastcol);
991 eol = eolString(row, emptyline, os.fragile());
993 // append newline only if line wasn't completely empty
994 // and this was not the last line in the grid
995 if (!emptyline && row + 1 < nrows())
998 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1010 int InsetMathGrid::colsep() const
1016 int InsetMathGrid::rowsep() const
1022 int InsetMathGrid::hlinesep() const
1028 int InsetMathGrid::vlinesep() const
1034 int InsetMathGrid::border() const
1040 void InsetMathGrid::splitCell(Cursor & cur)
1042 if (cur.idx() == cur.lastidx())
1044 MathData ar = cur.cell();
1045 ar.erase(0, cur.pos());
1046 cur.cell().erase(cur.pos(), cur.lastpos());
1049 cur.cell().insert(0, ar);
1053 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1055 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1056 switch (cmd.action) {
1058 // insert file functions
1059 case LFUN_LINE_DELETE:
1060 cur.recordUndoInset();
1061 //autocorrect_ = false;
1069 if (cur.idx() > cur.lastidx())
1070 cur.idx() = cur.lastidx();
1071 if (cur.pos() > cur.lastpos())
1072 cur.pos() = cur.lastpos();
1075 case LFUN_CELL_SPLIT:
1080 case LFUN_CELL_BACKWARD:
1082 cur.setSelection(false);
1083 if (!idxPrev(cur)) {
1084 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1089 case LFUN_CELL_FORWARD:
1090 // Can't handle selection by additional 'shift' as this is
1091 // hard bound to LFUN_CELL_BACKWARD
1092 cur.setSelection(false);
1093 if (!idxNext(cur)) {
1094 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1099 case LFUN_NEWLINE_INSERT: {
1100 cur.recordUndoInset();
1101 row_type const r = cur.row();
1105 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1106 swap(cell(index(r, c)), cell(index(r + 1, c)));
1110 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1113 cur.pos() = cur.lastpos();
1115 //mathcursor->normalize();
1116 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1120 case LFUN_TABULAR_FEATURE: {
1121 cur.recordUndoInset();
1122 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1123 istringstream is(to_utf8(cmd.argument()));
1126 if (s == "valign-top")
1127 setVerticalAlignment('t');
1128 else if (s == "valign-middle")
1129 setVerticalAlignment('c');
1130 else if (s == "valign-bottom")
1131 setVerticalAlignment('b');
1132 else if (s == "align-left")
1133 setHorizontalAlignment('l', cur.col());
1134 else if (s == "align-right")
1135 setHorizontalAlignment('r', cur.col());
1136 else if (s == "align-center")
1137 setHorizontalAlignment('c', cur.col());
1138 else if (s == "append-row")
1139 for (int i = 0, n = extractInt(is); i < n; ++i)
1141 else if (s == "delete-row") {
1142 cur.clearSelection(); // bug 4323
1143 for (int i = 0, n = extractInt(is); i < n; ++i) {
1145 if (cur.idx() >= nargs())
1146 cur.idx() -= ncols();
1148 cur.pos() = 0; // trick, see below
1150 else if (s == "copy-row") {
1151 // Here (as later) we save the cursor col/row
1152 // in order to restore it after operation.
1153 row_type const r = cur.row();
1154 col_type const c = cur.col();
1155 for (int i = 0, n = extractInt(is); i < n; ++i)
1157 cur.idx() = index(r, c);
1159 else if (s == "swap-row") {
1161 // Trick to suppress same-idx-means-different-cell
1165 else if (s == "add-hline-above")
1166 rowinfo_[cur.row()].lines_++;
1167 else if (s == "add-hline-below")
1168 rowinfo_[cur.row()+1].lines_++;
1169 else if (s == "delete-hline-above")
1170 rowinfo_[cur.row()].lines_--;
1171 else if (s == "delete-hline-below")
1172 rowinfo_[cur.row()+1].lines_--;
1173 else if (s == "append-column") {
1174 row_type const r = cur.row();
1175 col_type const c = cur.col();
1176 for (int i = 0, n = extractInt(is); i < n; ++i)
1177 addCol(cur.col() + 1);
1178 cur.idx() = index(r, c);
1180 else if (s == "delete-column") {
1181 cur.clearSelection(); // bug 4323
1182 row_type const r = cur.row();
1183 col_type const c = cur.col();
1184 for (int i = 0, n = extractInt(is); i < n; ++i)
1185 delCol(col(cur.idx()));
1186 cur.idx() = index(r, min(c, cur.ncols() - 1));
1187 cur.pos() = 0; // trick, see above
1189 else if (s == "copy-column") {
1190 row_type const r = cur.row();
1191 col_type const c = cur.col();
1193 cur.idx() = index(r, c);
1195 else if (s == "swap-column") {
1197 cur.pos() = 0; // trick, see above
1199 else if (s == "add-vline-left") {
1200 colinfo_[cur.col()].lines_++;
1201 if (!colinfo_[cur.col()].special_.empty())
1202 colinfo_[cur.col()].special_ += '|';
1204 else if (s == "add-vline-right") {
1205 colinfo_[cur.col()+1].lines_++;
1206 if (!colinfo_[cur.col()+1].special_.empty())
1207 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1209 else if (s == "delete-vline-left") {
1210 colinfo_[cur.col()].lines_--;
1211 docstring & special = colinfo_[cur.col()].special_;
1212 if (!special.empty()) {
1213 docstring::size_type i = special.rfind('|');
1214 LASSERT(i != docstring::npos, /**/);
1215 special.erase(i, 1);
1218 else if (s == "delete-vline-right") {
1219 colinfo_[cur.col()+1].lines_--;
1220 docstring & special = colinfo_[cur.col()+1].special_;
1221 if (!special.empty()) {
1222 docstring::size_type i = special.find('|');
1223 LASSERT(i != docstring::npos, /**/);
1224 special.erase(i, 1);
1231 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1232 lyxerr << "returning FINISHED_LEFT" << endl;
1237 cur.message(_("Paste"));
1238 cap::replaceSelection(cur);
1240 if (cmd.argument().empty() && !theClipboard().isInternal())
1241 topaste = theClipboard().getAsText();
1243 idocstringstream is(cmd.argument());
1246 topaste = cap::selection(n);
1248 InsetMathGrid grid(1, 1);
1249 if (!topaste.empty())
1250 mathed_parse_normal(grid, topaste);
1252 if (grid.nargs() == 1) {
1253 // single cell/part of cell
1255 cur.cell().insert(cur.pos(), grid.cell(0));
1256 cur.pos() += grid.cell(0).size();
1259 cur.recordUndoInset();
1260 col_type const numcols =
1261 min(grid.ncols(), ncols() - col(cur.idx()));
1262 row_type const numrows =
1263 min(grid.nrows(), nrows() - cur.row());
1264 for (row_type r = 0; r < numrows; ++r) {
1265 for (col_type c = 0; c < numcols; ++c) {
1266 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1267 cell(i).insert(0, grid.cell(grid.index(r, c)));
1269 // append the left over horizontal cells to the last column
1270 idx_type i = index(r + cur.row(), ncols() - 1);
1271 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1272 cell(i).append(grid.cell(grid.index(r, c)));
1274 // append the left over vertical cells to the last _cell_
1275 idx_type i = nargs() - 1;
1276 for (row_type r = numrows; r < grid.nrows(); ++r)
1277 for (col_type c = 0; c < grid.ncols(); ++c)
1278 cell(i).append(grid.cell(grid.index(r, c)));
1280 cur.clearSelection(); // bug 393
1285 case LFUN_LINE_BEGIN_SELECT:
1286 case LFUN_LINE_BEGIN:
1287 case LFUN_WORD_BACKWARD_SELECT:
1288 case LFUN_WORD_BACKWARD:
1289 case LFUN_WORD_LEFT_SELECT:
1290 case LFUN_WORD_LEFT:
1291 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1292 cmd.action == LFUN_WORD_LEFT_SELECT ||
1293 cmd.action == LFUN_LINE_BEGIN_SELECT);
1294 cur.macroModeClose();
1295 if (cur.pos() != 0) {
1297 } else if (cur.idx() % cur.ncols() != 0) {
1298 cur.idx() -= cur.idx() % cur.ncols();
1300 } else if (cur.idx() != 0) {
1304 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1309 case LFUN_WORD_FORWARD_SELECT:
1310 case LFUN_WORD_FORWARD:
1311 case LFUN_WORD_RIGHT_SELECT:
1312 case LFUN_WORD_RIGHT:
1313 case LFUN_LINE_END_SELECT:
1315 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1316 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1317 cmd.action == LFUN_LINE_END_SELECT);
1318 cur.macroModeClose();
1320 if (cur.pos() != cur.lastpos()) {
1321 cur.pos() = cur.lastpos();
1322 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1323 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1324 cur.pos() = cur.lastpos();
1325 } else if (cur.idx() != cur.lastidx()) {
1326 cur.idx() = cur.lastidx();
1327 cur.pos() = cur.lastpos();
1329 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1335 InsetMathNest::doDispatch(cur, cmd);
1340 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1341 FuncStatus & status) const
1343 switch (cmd.action) {
1344 case LFUN_TABULAR_FEATURE: {
1345 string const s = to_utf8(cmd.argument());
1346 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1347 status.setEnabled(false);
1348 status.message(from_utf8(N_("Only one row")));
1352 (s == "delete-column" || s == "swap-column")) {
1353 status.setEnabled(false);
1354 status.message(from_utf8(N_("Only one column")));
1357 if ((rowinfo_[cur.row()].lines_ == 0 &&
1358 s == "delete-hline-above") ||
1359 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1360 s == "delete-hline-below")) {
1361 status.setEnabled(false);
1362 status.message(from_utf8(N_("No hline to delete")));
1366 if ((colinfo_[cur.col()].lines_ == 0 &&
1367 s == "delete-vline-left") ||
1368 (colinfo_[cur.col() + 1].lines_ == 0 &&
1369 s == "delete-vline-right")) {
1370 status.setEnabled(false);
1371 status.message(from_utf8(N_("No vline to delete")));
1374 if (s == "valign-top" || s == "valign-middle" ||
1375 s == "valign-bottom" || s == "align-left" ||
1376 s == "align-right" || s == "align-center" ||
1377 s == "append-row" || s == "delete-row" ||
1378 s == "copy-row" || s == "swap-row" ||
1379 s == "add-hline-above" || s == "add-hline-below" ||
1380 s == "delete-hline-above" || s == "delete-hline-below" ||
1381 s == "append-column" || s == "delete-column" ||
1382 s == "copy-column" || s == "swap-column" ||
1383 s == "add-vline-left" || s == "add-vline-right" ||
1384 s == "delete-vline-left" || s == "delete-vline-right")
1385 status.setEnabled(true);
1387 status.setEnabled(false);
1388 status.message(bformat(
1389 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1392 char const ha = horizontalAlignment(cur.col());
1393 char const va = verticalAlignment();
1394 status.setOnOff((s == "align-left" && ha == 'l')
1395 || (s == "align-right" && ha == 'r')
1396 || (s == "align-center" && ha == 'c')
1397 || (s == "valign-top" && va == 't')
1398 || (s == "valign-bottom" && va == 'b')
1399 || (s == "valign-middle" && va == 'm'));
1402 // FIXME: What did this code do?
1403 // Please check whether it is still needed!
1404 // should be more precise
1405 if (v_align_ == '\0') {
1406 status.enable(true);
1409 if (cmd.argument().empty()) {
1410 status.enable(false);
1413 if (!contains("tcb", cmd.argument()[0])) {
1414 status.enable(false);
1417 status.setOnOff(cmd.argument()[0] == v_align_);
1418 status.setEnabled(true);
1423 case LFUN_CELL_SPLIT:
1424 status.setEnabled(true);
1427 case LFUN_CELL_BACKWARD:
1428 case LFUN_CELL_FORWARD:
1429 status.setEnabled(true);
1433 return InsetMathNest::getStatus(cur, cmd, status);