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 "MathExtern.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
22 #include "BufferView.h"
23 #include "CutAndPaste.h"
24 #include "FuncStatus.h"
26 #include "FuncRequest.h"
28 #include "frontends/Clipboard.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
37 #include "support/lassert.h"
42 using namespace lyx::support;
47 static docstring verboseHLine(int n)
50 for (int i = 0; i < n; ++i)
58 static int extractInt(istream & is)
62 return (num == 0) ? 1 : num;
66 static void resetGrid(InsetMathGrid & grid)
68 while (grid.ncols() > 1)
69 grid.delCol(grid.ncols());
70 while (grid.nrows() > 1)
71 grid.delRow(grid.nrows());
72 grid.cell(0).erase(0, grid.cell(0).size());
78 //////////////////////////////////////////////////////////////
81 InsetMathGrid::CellInfo::CellInfo()
87 //////////////////////////////////////////////////////////////
90 InsetMathGrid::RowInfo::RowInfo()
91 : lines_(0), skip_(0), allow_newpage_(true)
96 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
98 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
99 return crskip_.inPixels(mi.base.textwidth,
100 fm.width(char_type('M')));
105 //////////////////////////////////////////////////////////////
108 InsetMathGrid::ColInfo::ColInfo()
109 : align_('c'), lines_(0)
113 //////////////////////////////////////////////////////////////
116 InsetMathGrid::InsetMathGrid(Buffer * buf)
117 : InsetMathNest(buf, 1),
127 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
128 : InsetMathNest(buf, m * n),
138 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
140 : InsetMathNest(buf, m * n),
147 setVerticalAlignment(v);
148 setHorizontalAlignments(h);
152 Inset * InsetMathGrid::clone() const
154 return new InsetMathGrid(*this);
158 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
160 return col + ncols() * row;
164 void InsetMathGrid::setDefaults()
167 lyxerr << "positive number of columns expected" << endl;
169 // lyxerr << "positive number of rows expected" << endl;
170 for (col_type col = 0; col < ncols(); ++col) {
171 colinfo_[col].align_ = defaultColAlign(col);
172 colinfo_[col].skip_ = defaultColSpace(col);
173 colinfo_[col].special_.clear();
178 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
181 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
184 colinfo_[col].lines_++;
185 } else if ((c == 'p' || c == 'm' || c == 'b'||
186 c == '!' || c == '@' || c == '>' || c == '<') &&
187 it + 1 != hh.end() && *(it + 1) == '{') {
188 // @{decl.} and p{width} are standard LaTeX, the
189 // others are extensions by array.sty
190 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
192 // this declares a new column
194 // Only intercolumn stuff is allowed
195 // in the last dummy column
197 colinfo_[col].align_ = 'l';
199 // this is intercolumn stuff
200 if (colinfo_[col].special_.empty())
201 // Overtake possible lines
202 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
206 while (it != hh.end()) {
208 colinfo_[col].special_ += c;
214 if (brace_open > 0 && brace_open == brace_close)
219 colinfo_[col].lines_ = count(
220 colinfo_[col].special_.begin(),
221 colinfo_[col].special_.end(), '|');
222 LYXERR(Debug::MATHED, "special column separator: `"
223 << to_utf8(colinfo_[col].special_) << '\'');
225 colinfo_[col].lines_ = 0;
226 colinfo_[col].special_.clear();
228 } else if (col >= ncols()) {
229 // Only intercolumn stuff is allowed in the last
232 } else if (c == 'c' || c == 'l' || c == 'r') {
233 colinfo_[col].align_ = static_cast<char>(c);
234 if (!colinfo_[col].special_.empty()) {
235 colinfo_[col].special_ += c;
236 colinfo_[col].lines_ = count(
237 colinfo_[col].special_.begin(),
238 colinfo_[col].special_.end(), '|');
239 LYXERR(Debug::MATHED, "special column separator: `"
240 << to_utf8(colinfo_[col].special_) << '\'');
243 colinfo_[col].lines_ = 0;
244 colinfo_[col].special_.clear();
246 lyxerr << "unknown column separator: '" << c << "'" << endl;
251 col_type n = hh.size();
254 for (col_type col = 0; col < n; ++col)
255 colinfo_[col].align_ = hh[col];
260 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
263 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
264 if (*it == 'c' || *it == 'l' || *it == 'r'||
265 *it == 'p' || *it == 'm' || *it == 'b')
267 // let's have at least one column, even if we did not recognize its
275 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
277 colinfo_[col].align_ = h;
278 if (!colinfo_[col].special_.empty()) {
279 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
280 if (c == 'l' || c == 'c' || c == 'r')
283 // FIXME: Change alignment of p, m and b columns, too
287 char InsetMathGrid::horizontalAlignment(col_type col) const
289 return colinfo_[col].align_;
293 docstring InsetMathGrid::horizontalAlignments() const
296 for (col_type col = 0; col < ncols(); ++col) {
297 if (colinfo_[col].special_.empty()) {
298 res += docstring(colinfo_[col].lines_, '|');
299 res += colinfo_[col].align_;
301 res += colinfo_[col].special_;
303 if (colinfo_[ncols()].special_.empty())
304 return res + docstring(colinfo_[ncols()].lines_, '|');
305 return res + colinfo_[ncols()].special_;
309 void InsetMathGrid::setVerticalAlignment(char c)
315 char InsetMathGrid::verticalAlignment() const
321 InsetMathGrid::col_type InsetMathGrid::ncols() const
323 return colinfo_.size() - 1;
327 InsetMathGrid::row_type InsetMathGrid::nrows() const
329 return rowinfo_.size() - 1;
333 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
335 return idx % ncols();
339 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
341 return idx / ncols();
345 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
347 rowinfo_[row].crskip_ = crskip;
351 Length InsetMathGrid::vcrskip(row_type row) const
353 return rowinfo_[row].crskip_;
357 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
359 // let the cells adjust themselves
360 InsetMathNest::metrics(mi);
362 BufferView & bv = *mi.base.bv;
364 // compute absolute sizes of vertical structure
365 for (row_type row = 0; row < nrows(); ++row) {
368 for (col_type col = 0; col < ncols(); ++col) {
369 Dimension const & dimc = cell(index(row, col)).dimension(bv);
370 asc = max(asc, dimc.asc);
371 desc = max(desc, dimc.des);
373 rowinfo_[row].ascent_ = asc;
374 rowinfo_[row].descent_ = desc;
376 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
377 rowinfo_[nrows()].ascent_ = 0;
378 rowinfo_[nrows()].descent_ = 0;
380 // compute vertical offsets
381 rowinfo_[0].offset_ = 0;
382 for (row_type row = 1; row <= nrows(); ++row) {
383 rowinfo_[row].offset_ =
384 rowinfo_[row - 1].offset_ +
385 rowinfo_[row - 1].descent_ +
386 rowinfo_[row - 1].skipPixels(mi) +
388 rowinfo_[row].lines_ * hlinesep() +
389 rowinfo_[row].ascent_;
392 // adjust vertical offset
399 h = rowinfo_[nrows() - 1].offset_;
402 h = rowinfo_[nrows() - 1].offset_ / 2;
404 for (row_type row = 0; row <= nrows(); ++row)
405 rowinfo_[row].offset_ -= h;
408 // compute absolute sizes of horizontal structure
409 for (col_type col = 0; col < ncols(); ++col) {
411 for (row_type row = 0; row < nrows(); ++row)
412 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
413 colinfo_[col].width_ = wid;
415 colinfo_[ncols()].width_ = 0;
417 // compute horizontal offsets
418 colinfo_[0].offset_ = border();
419 for (col_type col = 1; col <= ncols(); ++col) {
420 colinfo_[col].offset_ =
421 colinfo_[col - 1].offset_ +
422 colinfo_[col - 1].width_ +
423 colinfo_[col - 1].skip_ +
425 colinfo_[col].lines_ * vlinesep();
429 dim.wid = colinfo_[ncols() - 1].offset_
430 + colinfo_[ncols() - 1].width_
431 + vlinesep() * colinfo_[ncols()].lines_
434 dim.asc = - rowinfo_[0].offset_
435 + rowinfo_[0].ascent_
436 + hlinesep() * rowinfo_[0].lines_
439 dim.des = rowinfo_[nrows() - 1].offset_
440 + rowinfo_[nrows() - 1].descent_
441 + hlinesep() * rowinfo_[nrows()].lines_
446 // Increase ws_[i] for 'R' columns (except the first one)
447 for (int i = 1; i < nc_; ++i)
448 if (align_[i] == 'R')
449 ws_[i] += 10 * df_width;
450 // Increase ws_[i] for 'C' column
451 if (align_[0] == 'C')
452 if (ws_[0] < 7 * workwidth / 8)
453 ws_[0] = 7 * workwidth / 8;
457 for (cxrow = row_.begin(); cxrow; ++cxrow) {
460 for (int i = 0; i < nc_; ++i) {
462 if (cxrow->getTab(i) <= 0) {
463 cxrow->setTab(i, df_width);
471 lf = (ws_[i] - cxrow->getTab(i))/2;
475 lf = ws_[i] - cxrow->getTab(i);
478 if (cxrow == row_.begin())
480 else if (cxrow.is_last())
481 lf = ws_[i] - cxrow->getTab(i);
483 lf = (ws_[i] - cxrow->getTab(i))/2;
486 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
487 cxrow->setTab(i, lf + rg);
488 rg = ws_[i] - ww + colsep();
489 if (cxrow == row_.begin())
490 width += ws_[i] + colsep();
492 cxrow->setBaseline(cxrow->getBaseline() - ascent);
495 metricsMarkers2(dim);
496 // Cache the inset dimension.
497 setDimCache(mi, dim);
501 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
503 drawWithMargin(pi, x, y, 1, 1);
507 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
508 int lmargin, int rmargin) const
510 Dimension const dim = dimension(*pi.base.bv);
511 BufferView const & bv = *pi.base.bv;
513 for (idx_type idx = 0; idx < nargs(); ++idx)
514 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
515 y + cellYOffset(idx));
517 for (row_type row = 0; row <= nrows(); ++row)
518 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
519 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
520 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
521 pi.pain.line(x + lmargin + 1, yy,
522 x + dim.width() - rmargin - 1, yy,
526 for (col_type col = 0; col <= ncols(); ++col)
527 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
528 int xx = x + lmargin + colinfo_[col].offset_
529 - i * vlinesep() - vlinesep()/2 - colsep()/2;
530 pi.pain.line(xx, y - dim.ascent() + 1,
531 xx, y + dim.descent() - 1,
534 drawMarkers2(pi, x, y);
538 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
540 // let the cells adjust themselves
541 //InsetMathNest::metrics(mi);
542 for (idx_type i = 0; i < nargs(); ++i)
543 cell(i).metricsT(mi, dim);
545 // compute absolute sizes of vertical structure
546 for (row_type row = 0; row < nrows(); ++row) {
549 for (col_type col = 0; col < ncols(); ++col) {
550 //MathData const & c = cell(index(row, col));
553 asc = max(asc, dimc.ascent());
554 desc = max(desc, dimc.descent());
556 rowinfo_[row].ascent_ = asc;
557 rowinfo_[row].descent_ = desc;
559 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
560 rowinfo_[nrows()].ascent_ = 0;
561 rowinfo_[nrows()].descent_ = 0;
563 // compute vertical offsets
564 rowinfo_[0].offset_ = 0;
565 for (row_type row = 1; row <= nrows(); ++row) {
566 rowinfo_[row].offset_ =
567 rowinfo_[row - 1].offset_ +
568 rowinfo_[row - 1].descent_ +
569 //rowinfo_[row - 1].skipPixels(mi) +
571 //rowinfo_[row].lines_ * hlinesep() +
572 rowinfo_[row].ascent_;
575 // adjust vertical offset
582 h = rowinfo_[nrows() - 1].offset_;
585 h = rowinfo_[nrows() - 1].offset_ / 2;
587 for (row_type row = 0; row <= nrows(); ++row)
588 rowinfo_[row].offset_ -= h;
591 // compute absolute sizes of horizontal structure
592 for (col_type col = 0; col < ncols(); ++col) {
594 for (row_type row = 0; row < nrows(); ++row) {
596 //wid = max(wid, cell(index(row, col)).width());
598 colinfo_[col].width_ = wid;
600 colinfo_[ncols()].width_ = 0;
602 // compute horizontal offsets
603 colinfo_[0].offset_ = border();
604 for (col_type col = 1; col <= ncols(); ++col) {
605 colinfo_[col].offset_ =
606 colinfo_[col - 1].offset_ +
607 colinfo_[col - 1].width_ +
608 colinfo_[col - 1].skip_ +
610 //colinfo_[col].lines_ * vlinesep();
614 dim.wid = colinfo_[ncols() - 1].offset_
615 + colinfo_[ncols() - 1].width_
616 //+ vlinesep() * colinfo_[ncols()].lines_
619 dim.asc = -rowinfo_[0].offset_
620 + rowinfo_[0].ascent_
621 //+ hlinesep() * rowinfo_[0].lines_
624 dim.des = rowinfo_[nrows() - 1].offset_
625 + rowinfo_[nrows() - 1].descent_
626 //+ hlinesep() * rowinfo_[nrows()].lines_
631 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
633 // for (idx_type idx = 0; idx < nargs(); ++idx)
634 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
638 docstring InsetMathGrid::eolString(row_type row, bool fragile) const
642 if (!rowinfo_[row].crskip_.zero())
643 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
644 else if(!rowinfo_[row].allow_newpage_)
647 // make sure an upcoming '[' does not break anything
648 if (row + 1 < nrows()) {
649 MathData const & c = cell(index(row + 1, 0));
650 if (c.size() && c.front()->getChar() == '[')
655 // only add \\ if necessary
656 if (eol.empty() && row + 1 == nrows())
659 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
663 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
665 if (col + 1 == lastcol)
667 return from_ascii(" & ");
671 void InsetMathGrid::addRow(row_type row)
673 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
675 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
677 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
681 void InsetMathGrid::appendRow()
683 rowinfo_.push_back(RowInfo());
684 //cells_.insert(cells_.end(), ncols(), MathData());
685 for (col_type col = 0; col < ncols(); ++col) {
686 cells_.push_back(cells_type::value_type());
687 cellinfo_.push_back(CellInfo());
692 void InsetMathGrid::delRow(row_type row)
697 cells_type::iterator it = cells_.begin() + row * ncols();
698 cells_.erase(it, it + ncols());
700 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
701 cellinfo_.erase(jt, jt + ncols());
703 rowinfo_.erase(rowinfo_.begin() + row);
707 void InsetMathGrid::copyRow(row_type row)
710 for (col_type col = 0; col < ncols(); ++col)
711 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
715 void InsetMathGrid::swapRow(row_type row)
719 if (row + 1 == nrows())
721 for (col_type col = 0; col < ncols(); ++col)
722 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
726 void InsetMathGrid::addCol(col_type newcol)
728 const col_type nc = ncols();
729 const row_type nr = nrows();
730 cells_type new_cells((nc + 1) * nr);
731 vector<CellInfo> new_cellinfo((nc + 1) * nr);
733 for (row_type row = 0; row < nr; ++row)
734 for (col_type col = 0; col < nc; ++col) {
735 new_cells[row * (nc + 1) + col + (col >= newcol)]
736 = cells_[row * nc + col];
737 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
738 = cellinfo_[row * nc + col];
740 swap(cells_, new_cells);
741 swap(cellinfo_, new_cellinfo);
744 inf.skip_ = defaultColSpace(newcol);
745 inf.align_ = defaultColAlign(newcol);
746 colinfo_.insert(colinfo_.begin() + newcol, inf);
750 void InsetMathGrid::delCol(col_type col)
756 vector<CellInfo> tmpcellinfo;
757 for (col_type i = 0; i < nargs(); ++i)
758 if (i % ncols() != col) {
759 tmpcells.push_back(cells_[i]);
760 tmpcellinfo.push_back(cellinfo_[i]);
762 swap(cells_, tmpcells);
763 swap(cellinfo_, tmpcellinfo);
765 colinfo_.erase(colinfo_.begin() + col);
769 void InsetMathGrid::copyCol(col_type col)
772 for (row_type row = 0; row < nrows(); ++row)
773 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
777 void InsetMathGrid::swapCol(col_type col)
781 if (col + 1 == ncols())
783 for (row_type row = 0; row < nrows(); ++row)
784 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
788 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
790 col_type c = col(idx);
791 int x = colinfo_[c].offset_;
792 char align = colinfo_[c].align_;
793 Dimension const & celldim = cell(idx).dimension(bv);
794 if (align == 'r' || align == 'R')
795 x += colinfo_[c].width_ - celldim.wid;
796 if (align == 'c' || align == 'C')
797 x += (colinfo_[c].width_ - celldim.wid) / 2;
802 int InsetMathGrid::cellYOffset(idx_type idx) const
804 return rowinfo_[row(idx)].offset_;
808 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
813 cur.idx() -= ncols();
815 if (cur.row() + 1 >= nrows())
817 cur.idx() += ncols();
819 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
824 bool InsetMathGrid::idxBackward(Cursor & cur) const
826 // leave matrix if at the front edge
830 cur.pos() = cur.lastpos();
835 bool InsetMathGrid::idxForward(Cursor & cur) const
837 // leave matrix if at the back edge
838 if (cur.col() + 1 == ncols())
846 bool InsetMathGrid::idxFirst(Cursor & cur) const
853 cur.idx() = (nrows() - 1) * ncols();
856 cur.idx() = ((nrows() - 1) / 2) * ncols();
863 bool InsetMathGrid::idxLast(Cursor & cur) const
867 cur.idx() = ncols() - 1;
870 cur.idx() = nargs() - 1;
873 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
875 cur.pos() = cur.lastpos();
880 bool InsetMathGrid::idxDelete(idx_type & idx)
882 // nothing to do if we have just one row
886 // nothing to do if we are in the middle of the last row of the inset
887 if (idx + ncols() > nargs())
890 // try to delete entire sequence of ncols() empty cells if possible
891 for (idx_type i = idx; i < idx + ncols(); ++i)
895 // move cells if necessary
896 for (idx_type i = index(row(idx), 0); i < idx; ++i)
897 swap(cell(i), cell(i + ncols()));
904 // undo effect of Ctrl-Tab (i.e. pull next cell)
905 //if (idx + 1 != nargs())
906 // cell(idx).swap(cell(idx + 1));
908 // we handled the event..
913 // reimplement old behaviour when pressing Delete in the last position
915 void InsetMathGrid::idxGlue(idx_type idx)
917 col_type c = col(idx);
918 if (c + 1 == ncols()) {
919 if (row(idx) + 1 != nrows()) {
920 for (col_type cc = 0; cc < ncols(); ++cc)
921 cell(idx).append(cell(idx + cc + 1));
922 delRow(row(idx) + 1);
925 cell(idx).append(cell(idx + 1));
926 for (col_type cc = c + 2; cc < ncols(); ++cc)
927 cell(idx - c + cc - 1) = cell(idx - c + cc);
928 cell(idx - c + ncols() - 1).clear();
933 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
935 return rowinfo_[row];
939 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
941 return rowinfo_[row];
945 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
947 row_type const ri = row(idx);
948 row_type const r1 = min(row(from), row(to));
949 row_type const r2 = max(row(from), row(to));
950 col_type const ci = col(idx);
951 col_type const c1 = min(col(from), col(to));
952 col_type const c2 = max(col(from), col(to));
953 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
958 void InsetMathGrid::normalize(NormalStream & os) const
961 for (row_type row = 0; row < nrows(); ++row) {
963 for (col_type col = 0; col < ncols(); ++col)
964 os << "[cell " << cell(index(row, col)) << ']';
971 docstring InsetMathGrid::mathmlize(MathStream & os) const
973 bool const havetable = nrows() > 1;
975 os << MTag("mtable");
977 for (row_type row = 0; row < nrows(); ++row) {
979 for (col_type col = 0; col < ncols(); ++col)
980 rv += lyx::mathmlize(cell(index(row, col)), os);
984 os << ETag("mtable");
989 void InsetMathGrid::write(WriteStream & os) const
991 write(os, 0, 0, nrows(), ncols());
994 void InsetMathGrid::write(WriteStream & os,
995 row_type beg_row, col_type beg_col,
996 row_type end_row, col_type end_col) const
998 MathEnsurer ensurer(os, false);
1000 for (row_type row = beg_row; row < end_row; ++row) {
1001 os << verboseHLine(rowinfo_[row].lines_);
1002 // don't write & and empty cells at end of line,
1003 // unless there are vertical lines
1004 col_type lastcol = 0;
1005 bool emptyline = true;
1006 for (col_type col = beg_col; col < end_col; ++col)
1007 if (!cell(index(row, col)).empty()
1008 || colinfo_[col + 1].lines_) {
1012 for (col_type col = beg_col; col < lastcol; ++col) {
1013 os << cell(index(row, col));
1014 if (os.pendingBrace())
1015 ModeSpecifier specifier(os, TEXT_MODE);
1016 os << eocString(col, lastcol);
1018 eol = eolString(row, os.fragile());
1020 // append newline only if line wasn't completely empty
1021 // and the formula is not written on a single line
1022 bool const empty = emptyline && eol.empty();
1023 if (!empty && nrows() > 1)
1026 // @TODO use end_row instead of nrows() ?
1027 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1039 int InsetMathGrid::colsep() const
1045 int InsetMathGrid::rowsep() const
1051 int InsetMathGrid::hlinesep() const
1057 int InsetMathGrid::vlinesep() const
1063 int InsetMathGrid::border() const
1069 void InsetMathGrid::splitCell(Cursor & cur)
1071 if (cur.idx() == cur.lastidx())
1073 MathData ar = cur.cell();
1074 ar.erase(0, cur.pos());
1075 cur.cell().erase(cur.pos(), cur.lastpos());
1078 cur.cell().insert(0, ar);
1082 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1084 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1086 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1088 switch (cmd.action) {
1090 // insert file functions
1091 case LFUN_LINE_DELETE:
1092 cur.recordUndoInset();
1093 //autocorrect_ = false;
1101 if (cur.idx() > cur.lastidx())
1102 cur.idx() = cur.lastidx();
1103 if (cur.pos() > cur.lastpos())
1104 cur.pos() = cur.lastpos();
1107 case LFUN_CELL_SPLIT:
1112 case LFUN_CELL_BACKWARD:
1114 cur.setSelection(false);
1115 if (!idxPrev(cur)) {
1116 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1121 case LFUN_CELL_FORWARD:
1122 // Can't handle selection by additional 'shift' as this is
1123 // hard bound to LFUN_CELL_BACKWARD
1124 cur.setSelection(false);
1125 if (!idxNext(cur)) {
1126 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1131 case LFUN_NEWLINE_INSERT: {
1132 cur.recordUndoInset();
1133 row_type const r = cur.row();
1137 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1138 swap(cell(index(r, c)), cell(index(r + 1, c)));
1142 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1145 cur.pos() = cur.lastpos();
1147 //mathcursor->normalize();
1148 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1152 case LFUN_TABULAR_FEATURE: {
1153 cur.recordUndoInset();
1154 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1155 istringstream is(to_utf8(cmd.argument()));
1158 if (s == "valign-top")
1159 setVerticalAlignment('t');
1160 else if (s == "valign-middle")
1161 setVerticalAlignment('c');
1162 else if (s == "valign-bottom")
1163 setVerticalAlignment('b');
1164 else if (s == "align-left")
1165 setHorizontalAlignment('l', cur.col());
1166 else if (s == "align-right")
1167 setHorizontalAlignment('r', cur.col());
1168 else if (s == "align-center")
1169 setHorizontalAlignment('c', cur.col());
1170 else if (s == "append-row")
1171 for (int i = 0, n = extractInt(is); i < n; ++i)
1173 else if (s == "delete-row") {
1174 cur.clearSelection(); // bug 4323
1175 for (int i = 0, n = extractInt(is); i < n; ++i) {
1177 if (cur.idx() >= nargs())
1178 cur.idx() -= ncols();
1180 cur.pos() = 0; // trick, see below
1182 else if (s == "copy-row") {
1183 // Here (as later) we save the cursor col/row
1184 // in order to restore it after operation.
1185 row_type const r = cur.row();
1186 col_type const c = cur.col();
1187 for (int i = 0, n = extractInt(is); i < n; ++i)
1189 cur.idx() = index(r, c);
1191 else if (s == "swap-row") {
1193 // Trick to suppress same-idx-means-different-cell
1197 else if (s == "add-hline-above")
1198 rowinfo_[cur.row()].lines_++;
1199 else if (s == "add-hline-below")
1200 rowinfo_[cur.row()+1].lines_++;
1201 else if (s == "delete-hline-above")
1202 rowinfo_[cur.row()].lines_--;
1203 else if (s == "delete-hline-below")
1204 rowinfo_[cur.row()+1].lines_--;
1205 else if (s == "append-column") {
1206 row_type const r = cur.row();
1207 col_type const c = cur.col();
1208 for (int i = 0, n = extractInt(is); i < n; ++i)
1209 addCol(cur.col() + 1);
1210 cur.idx() = index(r, c);
1212 else if (s == "delete-column") {
1213 cur.clearSelection(); // bug 4323
1214 row_type const r = cur.row();
1215 col_type const c = cur.col();
1216 for (int i = 0, n = extractInt(is); i < n; ++i)
1217 delCol(col(cur.idx()));
1218 cur.idx() = index(r, min(c, cur.ncols() - 1));
1219 cur.pos() = 0; // trick, see above
1221 else if (s == "copy-column") {
1222 row_type const r = cur.row();
1223 col_type const c = cur.col();
1225 cur.idx() = index(r, c);
1227 else if (s == "swap-column") {
1229 cur.pos() = 0; // trick, see above
1231 else if (s == "add-vline-left") {
1232 colinfo_[cur.col()].lines_++;
1233 if (!colinfo_[cur.col()].special_.empty())
1234 colinfo_[cur.col()].special_ += '|';
1236 else if (s == "add-vline-right") {
1237 colinfo_[cur.col()+1].lines_++;
1238 if (!colinfo_[cur.col()+1].special_.empty())
1239 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1241 else if (s == "delete-vline-left") {
1242 colinfo_[cur.col()].lines_--;
1243 docstring & special = colinfo_[cur.col()].special_;
1244 if (!special.empty()) {
1245 docstring::size_type i = special.rfind('|');
1246 LASSERT(i != docstring::npos, /**/);
1247 special.erase(i, 1);
1250 else if (s == "delete-vline-right") {
1251 colinfo_[cur.col()+1].lines_--;
1252 docstring & special = colinfo_[cur.col()+1].special_;
1253 if (!special.empty()) {
1254 docstring::size_type i = special.find('|');
1255 LASSERT(i != docstring::npos, /**/);
1256 special.erase(i, 1);
1263 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1264 //lyxerr << "returning FINISHED_LEFT" << endl;
1268 case LFUN_CLIPBOARD_PASTE:
1269 parseflg |= Parse::VERBATIM;
1272 if (cur.currentMode() <= TEXT_MODE)
1273 parseflg |= Parse::TEXTMODE;
1274 cur.message(_("Paste"));
1275 cap::replaceSelection(cur);
1277 if (cmd.argument().empty() && !theClipboard().isInternal())
1278 topaste = theClipboard().getAsText();
1280 idocstringstream is(cmd.argument());
1283 topaste = cap::selection(n);
1285 InsetMathGrid grid(buffer_, 1, 1);
1286 if (!topaste.empty())
1287 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1288 || !mathed_parse_normal(grid, topaste, parseflg)) {
1290 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1293 if (grid.nargs() == 1) {
1294 // single cell/part of cell
1296 cur.cell().insert(cur.pos(), grid.cell(0));
1297 cur.pos() += grid.cell(0).size();
1300 cur.recordUndoInset();
1301 col_type const numcols =
1302 min(grid.ncols(), ncols() - col(cur.idx()));
1303 row_type const numrows =
1304 min(grid.nrows(), nrows() - cur.row());
1305 for (row_type r = 0; r < numrows; ++r) {
1306 for (col_type c = 0; c < numcols; ++c) {
1307 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1308 cell(i).insert(0, grid.cell(grid.index(r, c)));
1310 // append the left over horizontal cells to the last column
1311 idx_type i = index(r + cur.row(), ncols() - 1);
1312 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1313 cell(i).append(grid.cell(grid.index(r, c)));
1315 // append the left over vertical cells to the last _cell_
1316 idx_type i = nargs() - 1;
1317 for (row_type r = numrows; r < grid.nrows(); ++r)
1318 for (col_type c = 0; c < grid.ncols(); ++c)
1319 cell(i).append(grid.cell(grid.index(r, c)));
1321 cur.clearSelection(); // bug 393
1326 case LFUN_LINE_BEGIN_SELECT:
1327 case LFUN_LINE_BEGIN:
1328 case LFUN_WORD_BACKWARD_SELECT:
1329 case LFUN_WORD_BACKWARD:
1330 case LFUN_WORD_LEFT_SELECT:
1331 case LFUN_WORD_LEFT:
1332 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1333 cmd.action == LFUN_WORD_LEFT_SELECT ||
1334 cmd.action == LFUN_LINE_BEGIN_SELECT);
1335 cur.macroModeClose();
1336 if (cur.pos() != 0) {
1338 } else if (cur.idx() % cur.ncols() != 0) {
1339 cur.idx() -= cur.idx() % cur.ncols();
1341 } else if (cur.idx() != 0) {
1345 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1350 case LFUN_WORD_FORWARD_SELECT:
1351 case LFUN_WORD_FORWARD:
1352 case LFUN_WORD_RIGHT_SELECT:
1353 case LFUN_WORD_RIGHT:
1354 case LFUN_LINE_END_SELECT:
1356 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1357 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1358 cmd.action == LFUN_LINE_END_SELECT);
1359 cur.macroModeClose();
1361 if (cur.pos() != cur.lastpos()) {
1362 cur.pos() = cur.lastpos();
1363 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1364 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1365 cur.pos() = cur.lastpos();
1366 } else if (cur.idx() != cur.lastidx()) {
1367 cur.idx() = cur.lastidx();
1368 cur.pos() = cur.lastpos();
1370 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1376 InsetMathNest::doDispatch(cur, cmd);
1381 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1382 FuncStatus & status) const
1384 switch (cmd.action) {
1385 case LFUN_TABULAR_FEATURE: {
1386 string const s = cmd.getArg(0);
1387 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1388 status.setEnabled(false);
1389 status.message(from_utf8(N_("Only one row")));
1393 (s == "delete-column" || s == "swap-column")) {
1394 status.setEnabled(false);
1395 status.message(from_utf8(N_("Only one column")));
1398 if ((rowinfo_[cur.row()].lines_ == 0 &&
1399 s == "delete-hline-above") ||
1400 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1401 s == "delete-hline-below")) {
1402 status.setEnabled(false);
1403 status.message(from_utf8(N_("No hline to delete")));
1407 if ((colinfo_[cur.col()].lines_ == 0 &&
1408 s == "delete-vline-left") ||
1409 (colinfo_[cur.col() + 1].lines_ == 0 &&
1410 s == "delete-vline-right")) {
1411 status.setEnabled(false);
1412 status.message(from_utf8(N_("No vline to delete")));
1415 if (s == "valign-top" || s == "valign-middle" ||
1416 s == "valign-bottom" || s == "align-left" ||
1417 s == "align-right" || s == "align-center") {
1418 status.setEnabled(true);
1419 char const ha = horizontalAlignment(cur.col());
1420 char const va = verticalAlignment();
1421 status.setOnOff((s == "align-left" && ha == 'l')
1422 || (s == "align-right" && ha == 'r')
1423 || (s == "align-center" && ha == 'c')
1424 || (s == "valign-top" && va == 't')
1425 || (s == "valign-bottom" && va == 'b')
1426 || (s == "valign-middle" && va == 'c'));
1429 if (s == "append-row" || s == "delete-row" ||
1430 s == "copy-row" || s == "swap-row" ||
1431 s == "add-hline-above" || s == "add-hline-below" ||
1432 s == "delete-hline-above" || s == "delete-hline-below" ||
1433 s == "append-column" || s == "delete-column" ||
1434 s == "copy-column" || s == "swap-column" ||
1435 s == "add-vline-left" || s == "add-vline-right" ||
1436 s == "delete-vline-left" || s == "delete-vline-right") {
1437 status.setEnabled(true);
1439 status.setEnabled(false);
1440 status.message(bformat(
1441 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1445 // FIXME: What did this code do?
1446 // Please check whether it is still needed!
1447 // should be more precise
1448 if (v_align_ == '\0') {
1449 status.enable(true);
1452 if (cmd.argument().empty()) {
1453 status.enable(false);
1456 if (!contains("tcb", cmd.argument()[0])) {
1457 status.enable(false);
1460 status.setOnOff(cmd.argument()[0] == v_align_);
1461 status.setEnabled(true);
1466 case LFUN_CELL_SPLIT:
1467 status.setEnabled(true);
1470 case LFUN_CELL_BACKWARD:
1471 case LFUN_CELL_FORWARD:
1472 status.setEnabled(true);
1476 return InsetMathNest::getStatus(cur, cmd, status);