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"
16 #include "InsetMathUnknown.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "CutAndPaste.h"
26 #include "FuncStatus.h"
28 #include "FuncRequest.h"
30 #include "frontends/Clipboard.h"
31 #include "frontends/Painter.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
38 #include "support/lassert.h"
43 using namespace lyx::support;
49 static docstring verboseHLine(int n)
52 for (int i = 0; i < n; ++i)
60 static int extractInt(istream & is)
64 return (num == 0) ? 1 : num;
68 static void resetGrid(InsetMathGrid & grid)
70 while (grid.ncols() > 1)
71 grid.delCol(grid.ncols() - 1);
72 while (grid.nrows() > 1)
73 grid.delRow(grid.nrows() - 1);
74 grid.cell(0).erase(0, grid.cell(0).size());
80 //////////////////////////////////////////////////////////////
83 InsetMathGrid::CellInfo::CellInfo()
89 //////////////////////////////////////////////////////////////
92 InsetMathGrid::RowInfo::RowInfo()
93 : lines_(0), skip_(0), allow_newpage_(true)
98 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
100 return crskip_.inPixels(mi.base);
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 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)
180 if (str == "\\hline") {
181 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
183 if (getStatus(cur, fr, status)) {
184 if (status.enabled()) {
185 rowinfo_[cur.row()].lines_++;
190 return InsetMathNest::interpretString(cur, str);
194 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
197 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
200 colinfo_[col].lines_++;
201 } else if ((c == 'p' || c == 'm' || c == 'b'||
202 c == '!' || c == '@' || c == '>' || c == '<') &&
203 it + 1 != hh.end() && *(it + 1) == '{') {
204 // @{decl.} and p{width} are standard LaTeX, the
205 // others are extensions by array.sty
206 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
208 // this declares a new column
210 // Only intercolumn stuff is allowed
211 // in the last dummy column
213 colinfo_[col].align_ = 'l';
215 // this is intercolumn stuff
216 if (colinfo_[col].special_.empty())
217 // Overtake possible lines
218 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
222 while (it != hh.end()) {
224 colinfo_[col].special_ += c;
230 if (brace_open > 0 && brace_open == brace_close)
235 colinfo_[col].lines_ = count(
236 colinfo_[col].special_.begin(),
237 colinfo_[col].special_.end(), '|');
238 LYXERR(Debug::MATHED, "special column separator: `"
239 << to_utf8(colinfo_[col].special_) << '\'');
241 colinfo_[col].lines_ = 0;
242 colinfo_[col].special_.clear();
244 } else if (col >= ncols()) {
245 // Only intercolumn stuff is allowed in the last
248 } else if (c == 'c' || c == 'l' || c == 'r') {
249 colinfo_[col].align_ = static_cast<char>(c);
250 if (!colinfo_[col].special_.empty()) {
251 colinfo_[col].special_ += c;
252 colinfo_[col].lines_ = count(
253 colinfo_[col].special_.begin(),
254 colinfo_[col].special_.end(), '|');
255 LYXERR(Debug::MATHED, "special column separator: `"
256 << to_utf8(colinfo_[col].special_) << '\'');
259 colinfo_[col].lines_ = 0;
260 colinfo_[col].special_.clear();
262 lyxerr << "unknown column separator: '" << c << "'" << endl;
267 col_type n = hh.size();
270 for (col_type col = 0; col < n; ++col)
271 colinfo_[col].align_ = hh[col];
276 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
279 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
280 if (*it == 'c' || *it == 'l' || *it == 'r'||
281 *it == 'p' || *it == 'm' || *it == 'b')
283 // let's have at least one column, even if we did not recognize its
291 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
293 colinfo_[col].align_ = h;
294 if (!colinfo_[col].special_.empty()) {
295 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
296 if (c == 'l' || c == 'c' || c == 'r')
299 // FIXME: Change alignment of p, m and b columns, too
303 char InsetMathGrid::horizontalAlignment(col_type col) const
305 return colinfo_[col].align_;
309 docstring InsetMathGrid::horizontalAlignments() const
312 for (col_type col = 0; col < ncols(); ++col) {
313 if (colinfo_[col].special_.empty()) {
314 res += docstring(colinfo_[col].lines_, '|');
315 res += colinfo_[col].align_;
317 res += colinfo_[col].special_;
319 if (colinfo_[ncols()].special_.empty())
320 return res + docstring(colinfo_[ncols()].lines_, '|');
321 return res + colinfo_[ncols()].special_;
325 void InsetMathGrid::setVerticalAlignment(char c)
331 char InsetMathGrid::verticalAlignment() const
337 InsetMathGrid::col_type InsetMathGrid::ncols() const
339 return colinfo_.size() - 1;
343 InsetMathGrid::row_type InsetMathGrid::nrows() const
345 return rowinfo_.size() - 1;
349 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
351 return idx % ncols();
355 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
357 return idx / ncols();
361 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
363 rowinfo_[row].crskip_ = crskip;
367 Length InsetMathGrid::vcrskip(row_type row) const
369 return rowinfo_[row].crskip_;
373 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
375 // let the cells adjust themselves
376 InsetMathNest::metrics(mi);
378 BufferView & bv = *mi.base.bv;
380 // compute absolute sizes of vertical structure
381 for (row_type row = 0; row < nrows(); ++row) {
384 for (col_type col = 0; col < ncols(); ++col) {
385 Dimension const & dimc = cell(index(row, col)).dimension(bv);
386 asc = max(asc, dimc.asc);
387 desc = max(desc, dimc.des);
389 rowinfo_[row].ascent_ = asc;
390 rowinfo_[row].descent_ = desc;
392 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
393 rowinfo_[nrows()].ascent_ = 0;
394 rowinfo_[nrows()].descent_ = 0;
396 // compute vertical offsets
397 rowinfo_[0].offset_ = 0;
398 for (row_type row = 1; row <= nrows(); ++row) {
399 rowinfo_[row].offset_ =
400 rowinfo_[row - 1].offset_ +
401 rowinfo_[row - 1].descent_ +
402 rowinfo_[row - 1].skipPixels(mi) +
404 rowinfo_[row].lines_ * hlinesep() +
405 rowinfo_[row].ascent_;
408 // adjust vertical offset
415 h = rowinfo_[nrows() - 1].offset_;
418 h = rowinfo_[nrows() - 1].offset_ / 2;
420 for (row_type row = 0; row <= nrows(); ++row)
421 rowinfo_[row].offset_ -= h;
424 // compute absolute sizes of horizontal structure
425 for (col_type col = 0; col < ncols(); ++col) {
427 for (row_type row = 0; row < nrows(); ++row)
428 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
429 colinfo_[col].width_ = wid;
431 colinfo_[ncols()].width_ = 0;
433 // compute horizontal offsets
434 colinfo_[0].offset_ = border();
435 for (col_type col = 1; col <= ncols(); ++col) {
436 colinfo_[col].offset_ =
437 colinfo_[col - 1].offset_ +
438 colinfo_[col - 1].width_ +
439 colinfo_[col - 1].skip_ +
441 colinfo_[col].lines_ * vlinesep();
445 dim.wid = colinfo_[ncols() - 1].offset_
446 + colinfo_[ncols() - 1].width_
447 + vlinesep() * colinfo_[ncols()].lines_
450 dim.asc = - rowinfo_[0].offset_
451 + rowinfo_[0].ascent_
452 + hlinesep() * rowinfo_[0].lines_
455 dim.des = rowinfo_[nrows() - 1].offset_
456 + rowinfo_[nrows() - 1].descent_
457 + hlinesep() * rowinfo_[nrows()].lines_
462 // Increase ws_[i] for 'R' columns (except the first one)
463 for (int i = 1; i < nc_; ++i)
464 if (align_[i] == 'R')
465 ws_[i] += 10 * df_width;
466 // Increase ws_[i] for 'C' column
467 if (align_[0] == 'C')
468 if (ws_[0] < 7 * workwidth / 8)
469 ws_[0] = 7 * workwidth / 8;
473 for (cxrow = row_.begin(); cxrow; ++cxrow) {
476 for (int i = 0; i < nc_; ++i) {
478 if (cxrow->getTab(i) <= 0) {
479 cxrow->setTab(i, df_width);
487 lf = (ws_[i] - cxrow->getTab(i))/2;
491 lf = ws_[i] - cxrow->getTab(i);
494 if (cxrow == row_.begin())
496 else if (cxrow.is_last())
497 lf = ws_[i] - cxrow->getTab(i);
499 lf = (ws_[i] - cxrow->getTab(i))/2;
502 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
503 cxrow->setTab(i, lf + rg);
504 rg = ws_[i] - ww + colsep();
505 if (cxrow == row_.begin())
506 width += ws_[i] + colsep();
508 cxrow->setBaseline(cxrow->getBaseline() - ascent);
511 metricsMarkers2(dim);
512 // Cache the inset dimension.
513 setDimCache(mi, dim);
517 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
519 drawWithMargin(pi, x, y, 1, 1);
523 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
524 int lmargin, int rmargin) const
526 Dimension const dim = dimension(*pi.base.bv);
527 BufferView const & bv = *pi.base.bv;
529 for (idx_type idx = 0; idx < nargs(); ++idx)
530 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
531 y + cellYOffset(idx));
533 for (row_type row = 0; row <= nrows(); ++row)
534 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
535 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
536 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
537 pi.pain.line(x + lmargin + 1, yy,
538 x + dim.width() - rmargin - 1, yy,
542 for (col_type col = 0; col <= ncols(); ++col)
543 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
544 int xx = x + lmargin + colinfo_[col].offset_
545 - i * vlinesep() - vlinesep()/2 - colsep()/2;
546 pi.pain.line(xx, y - dim.ascent() + 1,
547 xx, y + dim.descent() - 1,
550 drawMarkers2(pi, x, y);
554 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
556 // let the cells adjust themselves
557 //InsetMathNest::metrics(mi);
558 for (idx_type i = 0; i < nargs(); ++i)
559 cell(i).metricsT(mi, dim);
561 // compute absolute sizes of vertical structure
562 for (row_type row = 0; row < nrows(); ++row) {
565 for (col_type col = 0; col < ncols(); ++col) {
566 //MathData const & c = cell(index(row, col));
569 asc = max(asc, dimc.ascent());
570 desc = max(desc, dimc.descent());
572 rowinfo_[row].ascent_ = asc;
573 rowinfo_[row].descent_ = desc;
575 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
576 rowinfo_[nrows()].ascent_ = 0;
577 rowinfo_[nrows()].descent_ = 0;
579 // compute vertical offsets
580 rowinfo_[0].offset_ = 0;
581 for (row_type row = 1; row <= nrows(); ++row) {
582 rowinfo_[row].offset_ =
583 rowinfo_[row - 1].offset_ +
584 rowinfo_[row - 1].descent_ +
585 //rowinfo_[row - 1].skipPixels(mi) +
587 //rowinfo_[row].lines_ * hlinesep() +
588 rowinfo_[row].ascent_;
591 // adjust vertical offset
598 h = rowinfo_[nrows() - 1].offset_;
601 h = rowinfo_[nrows() - 1].offset_ / 2;
603 for (row_type row = 0; row <= nrows(); ++row)
604 rowinfo_[row].offset_ -= h;
607 // compute absolute sizes of horizontal structure
608 for (col_type col = 0; col < ncols(); ++col) {
610 for (row_type row = 0; row < nrows(); ++row) {
612 //wid = max(wid, cell(index(row, col)).width());
614 colinfo_[col].width_ = wid;
616 colinfo_[ncols()].width_ = 0;
618 // compute horizontal offsets
619 colinfo_[0].offset_ = border();
620 for (col_type col = 1; col <= ncols(); ++col) {
621 colinfo_[col].offset_ =
622 colinfo_[col - 1].offset_ +
623 colinfo_[col - 1].width_ +
624 colinfo_[col - 1].skip_ +
626 //colinfo_[col].lines_ * vlinesep();
630 dim.wid = colinfo_[ncols() - 1].offset_
631 + colinfo_[ncols() - 1].width_
632 //+ vlinesep() * colinfo_[ncols()].lines_
635 dim.asc = -rowinfo_[0].offset_
636 + rowinfo_[0].ascent_
637 //+ hlinesep() * rowinfo_[0].lines_
640 dim.des = rowinfo_[nrows() - 1].offset_
641 + rowinfo_[nrows() - 1].descent_
642 //+ hlinesep() * rowinfo_[nrows()].lines_
647 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
649 // for (idx_type idx = 0; idx < nargs(); ++idx)
650 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
654 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
657 for (idx_type idx = 0; idx < nargs(); ++idx)
658 cell(idx).updateBuffer(it, utype);
662 docstring InsetMathGrid::eolString(row_type row, bool fragile,
663 bool /*latex*/, bool last_eoln) const
667 if (!rowinfo_[row].crskip_.zero())
668 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
669 else if(!rowinfo_[row].allow_newpage_)
672 // make sure an upcoming '[' does not break anything
673 if (row + 1 < nrows()) {
674 MathData const & c = cell(index(row + 1, 0));
675 if (!c.empty() && c.front()->getChar() == '[')
680 // only add \\ if necessary
681 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
684 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
688 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
690 if (col + 1 == lastcol)
692 return from_ascii(" & ");
696 void InsetMathGrid::addRow(row_type row)
698 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
700 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
702 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
706 void InsetMathGrid::delRow(row_type row)
711 cells_type::iterator it = cells_.begin() + row * ncols();
712 cells_.erase(it, it + ncols());
714 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
715 cellinfo_.erase(jt, jt + ncols());
717 rowinfo_.erase(rowinfo_.begin() + row);
721 void InsetMathGrid::copyRow(row_type row)
724 for (col_type col = 0; col < ncols(); ++col)
725 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
729 void InsetMathGrid::swapRow(row_type row)
733 if (row + 1 == nrows())
735 for (col_type col = 0; col < ncols(); ++col)
736 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
740 void InsetMathGrid::addCol(col_type newcol)
742 const col_type nc = ncols();
743 const row_type nr = nrows();
744 cells_type new_cells((nc + 1) * nr);
745 vector<CellInfo> new_cellinfo((nc + 1) * nr);
747 for (row_type row = 0; row < nr; ++row)
748 for (col_type col = 0; col < nc; ++col) {
749 new_cells[row * (nc + 1) + col + (col >= newcol)]
750 = cells_[row * nc + col];
751 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
752 = cellinfo_[row * nc + col];
754 swap(cells_, new_cells);
755 swap(cellinfo_, new_cellinfo);
758 inf.skip_ = defaultColSpace(newcol);
759 inf.align_ = defaultColAlign(newcol);
760 colinfo_.insert(colinfo_.begin() + newcol, inf);
764 void InsetMathGrid::delCol(col_type col)
770 vector<CellInfo> tmpcellinfo;
771 for (col_type i = 0; i < nargs(); ++i)
772 if (i % ncols() != col) {
773 tmpcells.push_back(cells_[i]);
774 tmpcellinfo.push_back(cellinfo_[i]);
776 swap(cells_, tmpcells);
777 swap(cellinfo_, tmpcellinfo);
779 colinfo_.erase(colinfo_.begin() + col);
783 void InsetMathGrid::copyCol(col_type col)
786 for (row_type row = 0; row < nrows(); ++row)
787 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
791 void InsetMathGrid::swapCol(col_type col)
795 if (col + 1 == ncols())
797 for (row_type row = 0; row < nrows(); ++row)
798 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
802 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
804 col_type c = col(idx);
805 int x = colinfo_[c].offset_;
806 char align = displayColAlign(c, row(idx));
807 Dimension const & celldim = cell(idx).dimension(bv);
808 if (align == 'r' || align == 'R')
809 x += colinfo_[c].width_ - celldim.wid;
810 if (align == 'c' || align == 'C')
811 x += (colinfo_[c].width_ - celldim.wid) / 2;
816 int InsetMathGrid::cellYOffset(idx_type idx) const
818 return rowinfo_[row(idx)].offset_;
822 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
827 cur.idx() -= ncols();
829 if (cur.row() + 1 >= nrows())
831 cur.idx() += ncols();
833 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
838 bool InsetMathGrid::idxBackward(Cursor & cur) const
840 // leave matrix if at the front edge
844 cur.pos() = cur.lastpos();
849 bool InsetMathGrid::idxForward(Cursor & cur) const
851 // leave matrix if at the back edge
852 if (cur.col() + 1 == ncols())
860 bool InsetMathGrid::idxFirst(Cursor & cur) const
867 cur.idx() = (nrows() - 1) * ncols();
870 cur.idx() = ((nrows() - 1) / 2) * ncols();
877 bool InsetMathGrid::idxLast(Cursor & cur) const
881 cur.idx() = ncols() - 1;
884 cur.idx() = nargs() - 1;
887 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
889 cur.pos() = cur.lastpos();
894 bool InsetMathGrid::idxDelete(idx_type & idx)
896 // nothing to do if we have just one row
900 // nothing to do if we are in the middle of the last row of the inset
901 if (idx + ncols() > nargs())
904 // try to delete entire sequence of ncols() empty cells if possible
905 for (idx_type i = idx; i < idx + ncols(); ++i)
906 if (!cell(i).empty())
909 // move cells if necessary
910 for (idx_type i = index(row(idx), 0); i < idx; ++i)
911 swap(cell(i), cell(i + ncols()));
918 // undo effect of Ctrl-Tab (i.e. pull next cell)
919 //if (idx + 1 != nargs())
920 // cell(idx).swap(cell(idx + 1));
922 // we handled the event..
927 // reimplement old behaviour when pressing Delete in the last position
929 void InsetMathGrid::idxGlue(idx_type idx)
931 col_type c = col(idx);
932 if (c + 1 == ncols()) {
933 if (row(idx) + 1 != nrows()) {
934 for (col_type cc = 0; cc < ncols(); ++cc)
935 cell(idx).append(cell(idx + cc + 1));
936 delRow(row(idx) + 1);
939 cell(idx).append(cell(idx + 1));
940 for (col_type cc = c + 2; cc < ncols(); ++cc)
941 cell(idx - c + cc - 1) = cell(idx - c + cc);
942 cell(idx - c + ncols() - 1).clear();
947 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
949 return rowinfo_[row];
953 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
955 return rowinfo_[row];
959 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
961 row_type const ri = row(idx);
962 row_type const r1 = min(row(from), row(to));
963 row_type const r2 = max(row(from), row(to));
964 col_type const ci = col(idx);
965 col_type const c1 = min(col(from), col(to));
966 col_type const c2 = max(col(from), col(to));
967 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
972 void InsetMathGrid::normalize(NormalStream & os) const
975 for (row_type row = 0; row < nrows(); ++row) {
977 for (col_type col = 0; col < ncols(); ++col)
978 os << "[cell " << cell(index(row, col)) << ']';
985 void InsetMathGrid::mathmlize(MathStream & os) const
987 bool const havetable = nrows() > 1 || ncols() > 1;
989 os << MTag("mtable");
990 char const * const celltag = havetable ? "mtd" : "mrow";
991 for (row_type row = 0; row < nrows(); ++row) {
994 for (col_type col = 0; col < ncols(); ++col) {
996 os << cell(index(row, col));
1003 os << ETag("mtable");
1008 // We need to do something about alignment here.
1009 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
1011 bool const havetable = nrows() > 1 || ncols() > 1;
1013 os << cell(index(0, 0));
1016 os << MTag("table", attrib);
1017 for (row_type row = 0; row < nrows(); ++row) {
1019 for (col_type col = 0; col < ncols(); ++col) {
1021 os << cell(index(row, col));
1026 os << ETag("table");
1030 void InsetMathGrid::htmlize(HtmlStream & os) const
1032 htmlize(os, "class='mathtable'");
1036 void InsetMathGrid::write(WriteStream & os) const
1038 write(os, 0, 0, nrows(), ncols());
1041 void InsetMathGrid::write(WriteStream & os,
1042 row_type beg_row, col_type beg_col,
1043 row_type end_row, col_type end_col) const
1045 MathEnsurer ensurer(os, false);
1047 for (row_type row = beg_row; row < end_row; ++row) {
1048 os << verboseHLine(rowinfo_[row].lines_);
1049 // don't write & and empty cells at end of line,
1050 // unless there are vertical lines
1051 col_type lastcol = 0;
1052 bool emptyline = true;
1053 bool last_eoln = true;
1054 for (col_type col = beg_col; col < end_col; ++col) {
1055 bool const empty_cell = cell(index(row, col)).empty();
1058 if (!empty_cell || colinfo_[col + 1].lines_) {
1063 for (col_type col = beg_col; col < lastcol; ++col) {
1064 os << cell(index(row, col));
1065 if (os.pendingBrace())
1066 ModeSpecifier specifier(os, TEXT_MODE);
1067 os << eocString(col, lastcol);
1069 eol = eolString(row, os.fragile(), os.latex(), last_eoln);
1071 // append newline only if line wasn't completely empty
1072 // and the formula is not written on a single line
1073 bool const empty = emptyline && eol.empty();
1074 if (!empty && nrows() > 1)
1077 // @TODO use end_row instead of nrows() ?
1078 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1090 int InsetMathGrid::colsep() const
1096 int InsetMathGrid::rowsep() const
1102 int InsetMathGrid::hlinesep() const
1108 int InsetMathGrid::vlinesep() const
1114 int InsetMathGrid::border() const
1120 void InsetMathGrid::splitCell(Cursor & cur)
1122 if (cur.idx() == cur.lastidx())
1124 MathData ar = cur.cell();
1125 ar.erase(0, cur.pos());
1126 cur.cell().erase(cur.pos(), cur.lastpos());
1129 cur.cell().insert(0, ar);
1133 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1135 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1137 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1139 FuncCode const act = cmd.action();
1142 // insert file functions
1143 case LFUN_LINE_DELETE_FORWARD:
1144 cur.recordUndoInset();
1145 //autocorrect_ = false;
1153 if (cur.idx() > cur.lastidx())
1154 cur.idx() = cur.lastidx();
1155 if (cur.pos() > cur.lastpos())
1156 cur.pos() = cur.lastpos();
1159 case LFUN_CELL_SPLIT:
1164 case LFUN_CELL_BACKWARD:
1166 cur.setSelection(false);
1167 if (!idxPrev(cur)) {
1168 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1173 case LFUN_CELL_FORWARD:
1174 // Can't handle selection by additional 'shift' as this is
1175 // hard bound to LFUN_CELL_BACKWARD
1176 cur.setSelection(false);
1177 if (!idxNext(cur)) {
1178 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1183 case LFUN_NEWLINE_INSERT: {
1184 cur.recordUndoInset();
1185 row_type const r = cur.row();
1189 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1190 swap(cell(index(r, c)), cell(index(r + 1, c)));
1194 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1197 cur.pos() = cur.lastpos();
1198 cur.forceBufferUpdate();
1199 //mathcursor->normalize();
1200 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1204 case LFUN_INSET_MODIFY: {
1205 cur.recordUndoInset();
1206 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1207 istringstream is(to_utf8(cmd.argument()));
1210 if (s != "tabular") {
1211 InsetMathNest::doDispatch(cur, cmd);
1215 if (s == "valign-top")
1216 setVerticalAlignment('t');
1217 else if (s == "valign-middle")
1218 setVerticalAlignment('c');
1219 else if (s == "valign-bottom")
1220 setVerticalAlignment('b');
1221 else if (s == "align-left")
1222 setHorizontalAlignment('l', cur.col());
1223 else if (s == "align-right")
1224 setHorizontalAlignment('r', cur.col());
1225 else if (s == "align-center")
1226 setHorizontalAlignment('c', cur.col());
1227 else if (s == "append-row")
1228 for (int i = 0, n = extractInt(is); i < n; ++i)
1230 else if (s == "delete-row") {
1231 cur.clearSelection(); // bug 4323
1232 for (int i = 0, n = extractInt(is); i < n; ++i) {
1234 if (cur.idx() >= nargs())
1235 cur.idx() -= ncols();
1237 cur.pos() = 0; // trick, see below
1239 else if (s == "copy-row") {
1240 // Here (as later) we save the cursor col/row
1241 // in order to restore it after operation.
1242 row_type const r = cur.row();
1243 col_type const c = cur.col();
1244 for (int i = 0, n = extractInt(is); i < n; ++i)
1246 cur.idx() = index(r, c);
1248 else if (s == "swap-row") {
1250 // Trick to suppress same-idx-means-different-cell
1254 else if (s == "add-hline-above")
1255 rowinfo_[cur.row()].lines_++;
1256 else if (s == "add-hline-below")
1257 rowinfo_[cur.row()+1].lines_++;
1258 else if (s == "delete-hline-above")
1259 rowinfo_[cur.row()].lines_--;
1260 else if (s == "delete-hline-below")
1261 rowinfo_[cur.row()+1].lines_--;
1262 else if (s == "append-column") {
1263 row_type const r = cur.row();
1264 col_type const c = cur.col();
1265 for (int i = 0, n = extractInt(is); i < n; ++i)
1266 addCol(cur.col() + 1);
1267 cur.idx() = index(r, c);
1269 else if (s == "delete-column") {
1270 cur.clearSelection(); // bug 4323
1271 row_type const r = cur.row();
1272 col_type const c = cur.col();
1273 for (int i = 0, n = extractInt(is); i < n; ++i)
1274 delCol(col(cur.idx()));
1275 cur.idx() = index(r, min(c, cur.ncols() - 1));
1276 cur.pos() = 0; // trick, see above
1278 else if (s == "copy-column") {
1279 row_type const r = cur.row();
1280 col_type const c = cur.col();
1282 cur.idx() = index(r, c);
1284 else if (s == "swap-column") {
1286 cur.pos() = 0; // trick, see above
1288 else if (s == "add-vline-left") {
1289 colinfo_[cur.col()].lines_++;
1290 if (!colinfo_[cur.col()].special_.empty())
1291 colinfo_[cur.col()].special_ += '|';
1293 else if (s == "add-vline-right") {
1294 colinfo_[cur.col()+1].lines_++;
1295 if (!colinfo_[cur.col()+1].special_.empty())
1296 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1298 else if (s == "delete-vline-left") {
1299 colinfo_[cur.col()].lines_--;
1300 docstring & special = colinfo_[cur.col()].special_;
1301 if (!special.empty()) {
1302 docstring::size_type i = special.rfind('|');
1303 LASSERT(i != docstring::npos, break);
1304 special.erase(i, 1);
1307 else if (s == "delete-vline-right") {
1308 colinfo_[cur.col()+1].lines_--;
1309 docstring & special = colinfo_[cur.col()+1].special_;
1310 if (!special.empty()) {
1311 docstring::size_type i = special.find('|');
1312 LASSERT(i != docstring::npos, break);
1313 special.erase(i, 1);
1320 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1321 //lyxerr << "returning FINISHED_LEFT" << endl;
1325 case LFUN_CLIPBOARD_PASTE:
1326 parseflg |= Parse::VERBATIM;
1329 if (cur.currentMode() <= TEXT_MODE)
1330 parseflg |= Parse::TEXTMODE;
1331 cur.message(_("Paste"));
1332 cap::replaceSelection(cur);
1334 if (cmd.argument().empty() && !theClipboard().isInternal())
1335 topaste = theClipboard().getAsText(Clipboard::PlainTextType);
1337 idocstringstream is(cmd.argument());
1340 topaste = cap::selection(n, buffer().params().documentClassPtr());
1342 InsetMathGrid grid(buffer_, 1, 1);
1343 if (!topaste.empty())
1344 if ((topaste.size() == 1 && isAscii(topaste))
1345 || !mathed_parse_normal(grid, topaste, parseflg)) {
1347 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1350 bool hline_enabled = false;
1351 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
1353 if (getStatus(cur, fr, status))
1354 hline_enabled = status.enabled();
1355 if (grid.nargs() == 1) {
1356 // single cell/part of cell
1357 cur.recordUndoInset();
1358 cur.cell().insert(cur.pos(), grid.cell(0));
1359 cur.pos() += grid.cell(0).size();
1361 rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_;
1363 for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) {
1364 cur.cell().insert(0,
1365 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1371 cur.recordUndoInset();
1372 col_type const numcols =
1373 min(grid.ncols(), ncols() - col(cur.idx()));
1374 row_type const numrows =
1375 min(grid.nrows(), nrows() - cur.row());
1376 for (row_type r = 0; r < numrows; ++r) {
1377 for (col_type c = 0; c < numcols; ++c) {
1378 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1379 cell(i).insert(0, grid.cell(grid.index(r, c)));
1382 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1384 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1385 idx_type i = index(r + cur.row(), 0);
1387 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1390 // append the left over horizontal cells to the last column
1391 idx_type i = index(r + cur.row(), ncols() - 1);
1392 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1393 cell(i).append(grid.cell(grid.index(r, c)));
1395 // append the left over vertical cells to the last _cell_
1396 idx_type i = nargs() - 1;
1397 for (row_type r = numrows; r < grid.nrows(); ++r) {
1398 for (col_type c = 0; c < grid.ncols(); ++c)
1399 cell(i).append(grid.cell(grid.index(r, c)));
1401 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1403 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1405 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1410 cur.clearSelection(); // bug 393
1411 // FIXME audit setBuffer calls
1412 cur.inset().setBuffer(*buffer_);
1413 cur.forceBufferUpdate();
1418 case LFUN_LINE_BEGIN_SELECT:
1419 case LFUN_LINE_BEGIN:
1420 case LFUN_WORD_BACKWARD_SELECT:
1421 case LFUN_WORD_BACKWARD:
1422 case LFUN_WORD_LEFT_SELECT:
1423 case LFUN_WORD_LEFT:
1424 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1425 act == LFUN_WORD_LEFT_SELECT ||
1426 act == LFUN_LINE_BEGIN_SELECT);
1427 cur.macroModeClose();
1428 if (cur.pos() != 0) {
1430 } else if (cur.idx() % cur.ncols() != 0) {
1431 cur.idx() -= cur.idx() % cur.ncols();
1433 } else if (cur.idx() != 0) {
1437 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1442 case LFUN_WORD_FORWARD_SELECT:
1443 case LFUN_WORD_FORWARD:
1444 case LFUN_WORD_RIGHT_SELECT:
1445 case LFUN_WORD_RIGHT:
1446 case LFUN_LINE_END_SELECT:
1448 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1449 act == LFUN_WORD_RIGHT_SELECT ||
1450 act == LFUN_LINE_END_SELECT);
1451 cur.macroModeClose();
1453 if (cur.pos() != cur.lastpos()) {
1454 cur.pos() = cur.lastpos();
1455 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1456 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1457 cur.pos() = cur.lastpos();
1458 } else if (cur.idx() != cur.lastidx()) {
1459 cur.idx() = cur.lastidx();
1460 cur.pos() = cur.lastpos();
1462 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1468 InsetMathNest::doDispatch(cur, cmd);
1473 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1474 FuncStatus & status) const
1476 switch (cmd.action()) {
1477 case LFUN_INSET_MODIFY: {
1478 istringstream is(to_utf8(cmd.argument()));
1481 if (s != "tabular") {
1482 // We only now about table actions here.
1485 if (&cur.inset() != this) {
1486 // Table actions requires that the cursor is _inside_ the
1488 status.setEnabled(false);
1489 status.message(from_utf8(N_("Cursor not in table")));
1493 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1494 status.setEnabled(false);
1495 status.message(from_utf8(N_("Only one row")));
1499 (s == "delete-column" || s == "swap-column")) {
1500 status.setEnabled(false);
1501 status.message(from_utf8(N_("Only one column")));
1504 if ((rowinfo_[cur.row()].lines_ == 0 &&
1505 s == "delete-hline-above") ||
1506 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1507 s == "delete-hline-below")) {
1508 status.setEnabled(false);
1509 status.message(from_utf8(N_("No hline to delete")));
1513 if ((colinfo_[cur.col()].lines_ == 0 &&
1514 s == "delete-vline-left") ||
1515 (colinfo_[cur.col() + 1].lines_ == 0 &&
1516 s == "delete-vline-right")) {
1517 status.setEnabled(false);
1518 status.message(from_utf8(N_("No vline to delete")));
1521 if (s == "valign-top" || s == "valign-middle" ||
1522 s == "valign-bottom" || s == "align-left" ||
1523 s == "align-right" || s == "align-center") {
1524 status.setEnabled(true);
1525 char const ha = horizontalAlignment(cur.col());
1526 char const va = verticalAlignment();
1527 status.setOnOff((s == "align-left" && ha == 'l')
1528 || (s == "align-right" && ha == 'r')
1529 || (s == "align-center" && ha == 'c')
1530 || (s == "valign-top" && va == 't')
1531 || (s == "valign-bottom" && va == 'b')
1532 || (s == "valign-middle" && va == 'c'));
1535 if (s == "append-row" || s == "delete-row" ||
1536 s == "copy-row" || s == "swap-row" ||
1537 s == "add-hline-above" || s == "add-hline-below" ||
1538 s == "delete-hline-above" || s == "delete-hline-below" ||
1539 s == "append-column" || s == "delete-column" ||
1540 s == "copy-column" || s == "swap-column" ||
1541 s == "add-vline-left" || s == "add-vline-right" ||
1542 s == "delete-vline-left" || s == "delete-vline-right") {
1543 status.setEnabled(true);
1545 status.setEnabled(false);
1546 status.message(bformat(
1547 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1551 // FIXME: What did this code do?
1552 // Please check whether it is still needed!
1553 // should be more precise
1554 if (v_align_ == '\0') {
1555 status.enable(true);
1558 if (cmd.argument().empty()) {
1559 status.enable(false);
1562 if (!contains("tcb", cmd.argument()[0])) {
1563 status.enable(false);
1566 status.setOnOff(cmd.argument()[0] == v_align_);
1567 status.setEnabled(true);
1572 case LFUN_CELL_SPLIT:
1573 status.setEnabled(cur.idx() != cur.lastidx());
1576 case LFUN_CELL_BACKWARD:
1577 case LFUN_CELL_FORWARD:
1578 status.setEnabled(true);
1584 return InsetMathNest::getStatus(cur, cmd, status);