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"
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 void InsetMathGrid::updateLabels(ParIterator const & it, UpdateType utype)
641 for (idx_type idx = 0; idx < nargs(); ++idx)
642 cell(idx).updateLabels(it, utype);
646 docstring InsetMathGrid::eolString(row_type row, bool fragile) const
650 if (!rowinfo_[row].crskip_.zero())
651 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
652 else if(!rowinfo_[row].allow_newpage_)
655 // make sure an upcoming '[' does not break anything
656 if (row + 1 < nrows()) {
657 MathData const & c = cell(index(row + 1, 0));
658 if (c.size() && c.front()->getChar() == '[')
663 // only add \\ if necessary
664 if (eol.empty() && row + 1 == nrows())
667 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
671 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
673 if (col + 1 == lastcol)
675 return from_ascii(" & ");
679 void InsetMathGrid::addRow(row_type row)
681 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
683 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
685 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
689 void InsetMathGrid::appendRow()
691 rowinfo_.push_back(RowInfo());
692 //cells_.insert(cells_.end(), ncols(), MathData());
693 for (col_type col = 0; col < ncols(); ++col) {
694 cells_.push_back(cells_type::value_type());
695 cellinfo_.push_back(CellInfo());
700 void InsetMathGrid::delRow(row_type row)
705 cells_type::iterator it = cells_.begin() + row * ncols();
706 cells_.erase(it, it + ncols());
708 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
709 cellinfo_.erase(jt, jt + ncols());
711 rowinfo_.erase(rowinfo_.begin() + row);
715 void InsetMathGrid::copyRow(row_type row)
718 for (col_type col = 0; col < ncols(); ++col)
719 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
723 void InsetMathGrid::swapRow(row_type row)
727 if (row + 1 == nrows())
729 for (col_type col = 0; col < ncols(); ++col)
730 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
734 void InsetMathGrid::addCol(col_type newcol)
736 const col_type nc = ncols();
737 const row_type nr = nrows();
738 cells_type new_cells((nc + 1) * nr);
739 vector<CellInfo> new_cellinfo((nc + 1) * nr);
741 for (row_type row = 0; row < nr; ++row)
742 for (col_type col = 0; col < nc; ++col) {
743 new_cells[row * (nc + 1) + col + (col >= newcol)]
744 = cells_[row * nc + col];
745 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
746 = cellinfo_[row * nc + col];
748 swap(cells_, new_cells);
749 swap(cellinfo_, new_cellinfo);
752 inf.skip_ = defaultColSpace(newcol);
753 inf.align_ = defaultColAlign(newcol);
754 colinfo_.insert(colinfo_.begin() + newcol, inf);
758 void InsetMathGrid::delCol(col_type col)
764 vector<CellInfo> tmpcellinfo;
765 for (col_type i = 0; i < nargs(); ++i)
766 if (i % ncols() != col) {
767 tmpcells.push_back(cells_[i]);
768 tmpcellinfo.push_back(cellinfo_[i]);
770 swap(cells_, tmpcells);
771 swap(cellinfo_, tmpcellinfo);
773 colinfo_.erase(colinfo_.begin() + col);
777 void InsetMathGrid::copyCol(col_type col)
780 for (row_type row = 0; row < nrows(); ++row)
781 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
785 void InsetMathGrid::swapCol(col_type col)
789 if (col + 1 == ncols())
791 for (row_type row = 0; row < nrows(); ++row)
792 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
796 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
798 col_type c = col(idx);
799 int x = colinfo_[c].offset_;
800 char align = colinfo_[c].align_;
801 Dimension const & celldim = cell(idx).dimension(bv);
802 if (align == 'r' || align == 'R')
803 x += colinfo_[c].width_ - celldim.wid;
804 if (align == 'c' || align == 'C')
805 x += (colinfo_[c].width_ - celldim.wid) / 2;
810 int InsetMathGrid::cellYOffset(idx_type idx) const
812 return rowinfo_[row(idx)].offset_;
816 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
821 cur.idx() -= ncols();
823 if (cur.row() + 1 >= nrows())
825 cur.idx() += ncols();
827 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
832 bool InsetMathGrid::idxBackward(Cursor & cur) const
834 // leave matrix if at the front edge
838 cur.pos() = cur.lastpos();
843 bool InsetMathGrid::idxForward(Cursor & cur) const
845 // leave matrix if at the back edge
846 if (cur.col() + 1 == ncols())
854 bool InsetMathGrid::idxFirst(Cursor & cur) const
861 cur.idx() = (nrows() - 1) * ncols();
864 cur.idx() = ((nrows() - 1) / 2) * ncols();
871 bool InsetMathGrid::idxLast(Cursor & cur) const
875 cur.idx() = ncols() - 1;
878 cur.idx() = nargs() - 1;
881 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
883 cur.pos() = cur.lastpos();
888 bool InsetMathGrid::idxDelete(idx_type & idx)
890 // nothing to do if we have just one row
894 // nothing to do if we are in the middle of the last row of the inset
895 if (idx + ncols() > nargs())
898 // try to delete entire sequence of ncols() empty cells if possible
899 for (idx_type i = idx; i < idx + ncols(); ++i)
903 // move cells if necessary
904 for (idx_type i = index(row(idx), 0); i < idx; ++i)
905 swap(cell(i), cell(i + ncols()));
912 // undo effect of Ctrl-Tab (i.e. pull next cell)
913 //if (idx + 1 != nargs())
914 // cell(idx).swap(cell(idx + 1));
916 // we handled the event..
921 // reimplement old behaviour when pressing Delete in the last position
923 void InsetMathGrid::idxGlue(idx_type idx)
925 col_type c = col(idx);
926 if (c + 1 == ncols()) {
927 if (row(idx) + 1 != nrows()) {
928 for (col_type cc = 0; cc < ncols(); ++cc)
929 cell(idx).append(cell(idx + cc + 1));
930 delRow(row(idx) + 1);
933 cell(idx).append(cell(idx + 1));
934 for (col_type cc = c + 2; cc < ncols(); ++cc)
935 cell(idx - c + cc - 1) = cell(idx - c + cc);
936 cell(idx - c + ncols() - 1).clear();
941 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
943 return rowinfo_[row];
947 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
949 return rowinfo_[row];
953 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
955 row_type const ri = row(idx);
956 row_type const r1 = min(row(from), row(to));
957 row_type const r2 = max(row(from), row(to));
958 col_type const ci = col(idx);
959 col_type const c1 = min(col(from), col(to));
960 col_type const c2 = max(col(from), col(to));
961 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
966 void InsetMathGrid::normalize(NormalStream & os) const
969 for (row_type row = 0; row < nrows(); ++row) {
971 for (col_type col = 0; col < ncols(); ++col)
972 os << "[cell " << cell(index(row, col)) << ']';
979 void InsetMathGrid::mathmlize(MathStream & os) const
981 bool const havetable = nrows() > 1 || ncols() > 1;
983 os << MTag("mtable");
984 char const * const celltag = havetable ? "mtd" : "mrow";
985 for (row_type row = 0; row < nrows(); ++row) {
988 for (col_type col = 0; col < ncols(); ++col) {
990 os << cell(index(row, col));
997 os << ETag("mtable");
1001 void InsetMathGrid::write(WriteStream & os) const
1003 write(os, 0, 0, nrows(), ncols());
1006 void InsetMathGrid::write(WriteStream & os,
1007 row_type beg_row, col_type beg_col,
1008 row_type end_row, col_type end_col) const
1010 MathEnsurer ensurer(os, false);
1012 for (row_type row = beg_row; row < end_row; ++row) {
1013 os << verboseHLine(rowinfo_[row].lines_);
1014 // don't write & and empty cells at end of line,
1015 // unless there are vertical lines
1016 col_type lastcol = 0;
1017 bool emptyline = true;
1018 for (col_type col = beg_col; col < end_col; ++col)
1019 if (!cell(index(row, col)).empty()
1020 || colinfo_[col + 1].lines_) {
1024 for (col_type col = beg_col; col < lastcol; ++col) {
1025 os << cell(index(row, col));
1026 if (os.pendingBrace())
1027 ModeSpecifier specifier(os, TEXT_MODE);
1028 os << eocString(col, lastcol);
1030 eol = eolString(row, os.fragile());
1032 // append newline only if line wasn't completely empty
1033 // and the formula is not written on a single line
1034 bool const empty = emptyline && eol.empty();
1035 if (!empty && nrows() > 1)
1038 // @TODO use end_row instead of nrows() ?
1039 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1051 int InsetMathGrid::colsep() const
1057 int InsetMathGrid::rowsep() const
1063 int InsetMathGrid::hlinesep() const
1069 int InsetMathGrid::vlinesep() const
1075 int InsetMathGrid::border() const
1081 void InsetMathGrid::splitCell(Cursor & cur)
1083 if (cur.idx() == cur.lastidx())
1085 MathData ar = cur.cell();
1086 ar.erase(0, cur.pos());
1087 cur.cell().erase(cur.pos(), cur.lastpos());
1090 cur.cell().insert(0, ar);
1094 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1096 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1098 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1100 switch (cmd.action) {
1102 // insert file functions
1103 case LFUN_LINE_DELETE:
1104 cur.recordUndoInset();
1105 //autocorrect_ = false;
1113 if (cur.idx() > cur.lastidx())
1114 cur.idx() = cur.lastidx();
1115 if (cur.pos() > cur.lastpos())
1116 cur.pos() = cur.lastpos();
1119 case LFUN_CELL_SPLIT:
1124 case LFUN_CELL_BACKWARD:
1126 cur.setSelection(false);
1127 if (!idxPrev(cur)) {
1128 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1133 case LFUN_CELL_FORWARD:
1134 // Can't handle selection by additional 'shift' as this is
1135 // hard bound to LFUN_CELL_BACKWARD
1136 cur.setSelection(false);
1137 if (!idxNext(cur)) {
1138 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1143 case LFUN_NEWLINE_INSERT: {
1144 cur.recordUndoInset();
1145 row_type const r = cur.row();
1149 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1150 swap(cell(index(r, c)), cell(index(r + 1, c)));
1154 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1157 cur.pos() = cur.lastpos();
1159 //mathcursor->normalize();
1160 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1164 case LFUN_INSET_MODIFY: {
1165 cur.recordUndoInset();
1166 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1167 istringstream is(to_utf8(cmd.argument()));
1170 if (s != "tabular") {
1171 InsetMathNest::doDispatch(cur, cmd);
1175 if (s == "valign-top")
1176 setVerticalAlignment('t');
1177 else if (s == "valign-middle")
1178 setVerticalAlignment('c');
1179 else if (s == "valign-bottom")
1180 setVerticalAlignment('b');
1181 else if (s == "align-left")
1182 setHorizontalAlignment('l', cur.col());
1183 else if (s == "align-right")
1184 setHorizontalAlignment('r', cur.col());
1185 else if (s == "align-center")
1186 setHorizontalAlignment('c', cur.col());
1187 else if (s == "append-row")
1188 for (int i = 0, n = extractInt(is); i < n; ++i)
1190 else if (s == "delete-row") {
1191 cur.clearSelection(); // bug 4323
1192 for (int i = 0, n = extractInt(is); i < n; ++i) {
1194 if (cur.idx() >= nargs())
1195 cur.idx() -= ncols();
1197 cur.pos() = 0; // trick, see below
1199 else if (s == "copy-row") {
1200 // Here (as later) we save the cursor col/row
1201 // in order to restore it after operation.
1202 row_type const r = cur.row();
1203 col_type const c = cur.col();
1204 for (int i = 0, n = extractInt(is); i < n; ++i)
1206 cur.idx() = index(r, c);
1208 else if (s == "swap-row") {
1210 // Trick to suppress same-idx-means-different-cell
1214 else if (s == "add-hline-above")
1215 rowinfo_[cur.row()].lines_++;
1216 else if (s == "add-hline-below")
1217 rowinfo_[cur.row()+1].lines_++;
1218 else if (s == "delete-hline-above")
1219 rowinfo_[cur.row()].lines_--;
1220 else if (s == "delete-hline-below")
1221 rowinfo_[cur.row()+1].lines_--;
1222 else if (s == "append-column") {
1223 row_type const r = cur.row();
1224 col_type const c = cur.col();
1225 for (int i = 0, n = extractInt(is); i < n; ++i)
1226 addCol(cur.col() + 1);
1227 cur.idx() = index(r, c);
1229 else if (s == "delete-column") {
1230 cur.clearSelection(); // bug 4323
1231 row_type const r = cur.row();
1232 col_type const c = cur.col();
1233 for (int i = 0, n = extractInt(is); i < n; ++i)
1234 delCol(col(cur.idx()));
1235 cur.idx() = index(r, min(c, cur.ncols() - 1));
1236 cur.pos() = 0; // trick, see above
1238 else if (s == "copy-column") {
1239 row_type const r = cur.row();
1240 col_type const c = cur.col();
1242 cur.idx() = index(r, c);
1244 else if (s == "swap-column") {
1246 cur.pos() = 0; // trick, see above
1248 else if (s == "add-vline-left") {
1249 colinfo_[cur.col()].lines_++;
1250 if (!colinfo_[cur.col()].special_.empty())
1251 colinfo_[cur.col()].special_ += '|';
1253 else if (s == "add-vline-right") {
1254 colinfo_[cur.col()+1].lines_++;
1255 if (!colinfo_[cur.col()+1].special_.empty())
1256 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1258 else if (s == "delete-vline-left") {
1259 colinfo_[cur.col()].lines_--;
1260 docstring & special = colinfo_[cur.col()].special_;
1261 if (!special.empty()) {
1262 docstring::size_type i = special.rfind('|');
1263 LASSERT(i != docstring::npos, /**/);
1264 special.erase(i, 1);
1267 else if (s == "delete-vline-right") {
1268 colinfo_[cur.col()+1].lines_--;
1269 docstring & special = colinfo_[cur.col()+1].special_;
1270 if (!special.empty()) {
1271 docstring::size_type i = special.find('|');
1272 LASSERT(i != docstring::npos, /**/);
1273 special.erase(i, 1);
1280 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1281 //lyxerr << "returning FINISHED_LEFT" << endl;
1285 case LFUN_CLIPBOARD_PASTE:
1286 parseflg |= Parse::VERBATIM;
1289 if (cur.currentMode() <= TEXT_MODE)
1290 parseflg |= Parse::TEXTMODE;
1291 cur.message(_("Paste"));
1292 cap::replaceSelection(cur);
1294 if (cmd.argument().empty() && !theClipboard().isInternal())
1295 topaste = theClipboard().getAsText();
1297 idocstringstream is(cmd.argument());
1300 topaste = cap::selection(n);
1302 InsetMathGrid grid(buffer_, 1, 1);
1303 if (!topaste.empty())
1304 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1305 || !mathed_parse_normal(grid, topaste, parseflg)) {
1307 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1310 if (grid.nargs() == 1) {
1311 // single cell/part of cell
1313 cur.cell().insert(cur.pos(), grid.cell(0));
1314 cur.pos() += grid.cell(0).size();
1317 cur.recordUndoInset();
1318 col_type const numcols =
1319 min(grid.ncols(), ncols() - col(cur.idx()));
1320 row_type const numrows =
1321 min(grid.nrows(), nrows() - cur.row());
1322 for (row_type r = 0; r < numrows; ++r) {
1323 for (col_type c = 0; c < numcols; ++c) {
1324 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1325 cell(i).insert(0, grid.cell(grid.index(r, c)));
1327 // append the left over horizontal cells to the last column
1328 idx_type i = index(r + cur.row(), ncols() - 1);
1329 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1330 cell(i).append(grid.cell(grid.index(r, c)));
1332 // append the left over vertical cells to the last _cell_
1333 idx_type i = nargs() - 1;
1334 for (row_type r = numrows; r < grid.nrows(); ++r)
1335 for (col_type c = 0; c < grid.ncols(); ++c)
1336 cell(i).append(grid.cell(grid.index(r, c)));
1338 cur.clearSelection(); // bug 393
1339 // FIXME audit setBuffer/updateLabels calls
1340 cur.inset().setBuffer(*buffer_);
1341 // FIXME audit setBuffer/updateLabels calls
1342 cur.buffer()->updateLabels();
1347 case LFUN_LINE_BEGIN_SELECT:
1348 case LFUN_LINE_BEGIN:
1349 case LFUN_WORD_BACKWARD_SELECT:
1350 case LFUN_WORD_BACKWARD:
1351 case LFUN_WORD_LEFT_SELECT:
1352 case LFUN_WORD_LEFT:
1353 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1354 cmd.action == LFUN_WORD_LEFT_SELECT ||
1355 cmd.action == LFUN_LINE_BEGIN_SELECT);
1356 cur.macroModeClose();
1357 if (cur.pos() != 0) {
1359 } else if (cur.idx() % cur.ncols() != 0) {
1360 cur.idx() -= cur.idx() % cur.ncols();
1362 } else if (cur.idx() != 0) {
1366 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1371 case LFUN_WORD_FORWARD_SELECT:
1372 case LFUN_WORD_FORWARD:
1373 case LFUN_WORD_RIGHT_SELECT:
1374 case LFUN_WORD_RIGHT:
1375 case LFUN_LINE_END_SELECT:
1377 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1378 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1379 cmd.action == LFUN_LINE_END_SELECT);
1380 cur.macroModeClose();
1382 if (cur.pos() != cur.lastpos()) {
1383 cur.pos() = cur.lastpos();
1384 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1385 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1386 cur.pos() = cur.lastpos();
1387 } else if (cur.idx() != cur.lastidx()) {
1388 cur.idx() = cur.lastidx();
1389 cur.pos() = cur.lastpos();
1391 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1397 InsetMathNest::doDispatch(cur, cmd);
1402 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1403 FuncStatus & status) const
1405 switch (cmd.action) {
1406 case LFUN_INSET_MODIFY: {
1407 istringstream is(to_utf8(cmd.argument()));
1411 return InsetMathNest::getStatus(cur, cmd, status);
1413 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1414 status.setEnabled(false);
1415 status.message(from_utf8(N_("Only one row")));
1419 (s == "delete-column" || s == "swap-column")) {
1420 status.setEnabled(false);
1421 status.message(from_utf8(N_("Only one column")));
1424 if ((rowinfo_[cur.row()].lines_ == 0 &&
1425 s == "delete-hline-above") ||
1426 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1427 s == "delete-hline-below")) {
1428 status.setEnabled(false);
1429 status.message(from_utf8(N_("No hline to delete")));
1433 if ((colinfo_[cur.col()].lines_ == 0 &&
1434 s == "delete-vline-left") ||
1435 (colinfo_[cur.col() + 1].lines_ == 0 &&
1436 s == "delete-vline-right")) {
1437 status.setEnabled(false);
1438 status.message(from_utf8(N_("No vline to delete")));
1441 if (s == "valign-top" || s == "valign-middle" ||
1442 s == "valign-bottom" || s == "align-left" ||
1443 s == "align-right" || s == "align-center") {
1444 status.setEnabled(true);
1445 char const ha = horizontalAlignment(cur.col());
1446 char const va = verticalAlignment();
1447 status.setOnOff((s == "align-left" && ha == 'l')
1448 || (s == "align-right" && ha == 'r')
1449 || (s == "align-center" && ha == 'c')
1450 || (s == "valign-top" && va == 't')
1451 || (s == "valign-bottom" && va == 'b')
1452 || (s == "valign-middle" && va == 'c'));
1455 if (s == "append-row" || s == "delete-row" ||
1456 s == "copy-row" || s == "swap-row" ||
1457 s == "add-hline-above" || s == "add-hline-below" ||
1458 s == "delete-hline-above" || s == "delete-hline-below" ||
1459 s == "append-column" || s == "delete-column" ||
1460 s == "copy-column" || s == "swap-column" ||
1461 s == "add-vline-left" || s == "add-vline-right" ||
1462 s == "delete-vline-left" || s == "delete-vline-right") {
1463 status.setEnabled(true);
1465 status.setEnabled(false);
1466 status.message(bformat(
1467 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1471 // FIXME: What did this code do?
1472 // Please check whether it is still needed!
1473 // should be more precise
1474 if (v_align_ == '\0') {
1475 status.enable(true);
1478 if (cmd.argument().empty()) {
1479 status.enable(false);
1482 if (!contains("tcb", cmd.argument()[0])) {
1483 status.enable(false);
1486 status.setOnOff(cmd.argument()[0] == v_align_);
1487 status.setEnabled(true);
1492 case LFUN_CELL_SPLIT:
1493 status.setEnabled(true);
1496 case LFUN_CELL_BACKWARD:
1497 case LFUN_CELL_FORWARD:
1498 status.setEnabled(true);
1502 return InsetMathNest::getStatus(cur, cmd, status);