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 "BufferView.h"
24 #include "CutAndPaste.h"
25 #include "FuncStatus.h"
27 #include "FuncRequest.h"
29 #include "frontends/Clipboard.h"
30 #include "frontends/FontMetrics.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 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
101 return crskip_.inPixels(mi.base.textwidth,
102 fm.width(char_type('M')));
107 //////////////////////////////////////////////////////////////
110 InsetMathGrid::ColInfo::ColInfo()
111 : align_('c'), lines_(0)
115 //////////////////////////////////////////////////////////////
118 InsetMathGrid::InsetMathGrid(Buffer * buf)
119 : InsetMathNest(buf, 1),
129 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
130 : InsetMathNest(buf, m * n),
140 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
142 : InsetMathNest(buf, m * n),
149 setVerticalAlignment(v);
150 setHorizontalAlignments(h);
154 Inset * InsetMathGrid::clone() const
156 return new InsetMathGrid(*this);
160 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
162 return col + ncols() * row;
166 void InsetMathGrid::setDefaults()
169 lyxerr << "positive number of columns expected" << endl;
171 // lyxerr << "positive number of rows expected" << endl;
172 for (col_type col = 0; col < ncols(); ++col) {
173 colinfo_[col].align_ = defaultColAlign(col);
174 colinfo_[col].skip_ = defaultColSpace(col);
175 colinfo_[col].special_.clear();
180 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)
182 if (str == "\\hline") {
183 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
185 if (getStatus(cur, fr, status)) {
186 if (status.enabled()) {
187 rowinfo_[cur.row()].lines_++;
192 return InsetMathNest::interpretString(cur, str);
196 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
199 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
202 colinfo_[col].lines_++;
203 } else if ((c == 'p' || c == 'm' || c == 'b'||
204 c == '!' || c == '@' || c == '>' || c == '<') &&
205 it + 1 != hh.end() && *(it + 1) == '{') {
206 // @{decl.} and p{width} are standard LaTeX, the
207 // others are extensions by array.sty
208 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
210 // this declares a new column
212 // Only intercolumn stuff is allowed
213 // in the last dummy column
215 colinfo_[col].align_ = 'l';
217 // this is intercolumn stuff
218 if (colinfo_[col].special_.empty())
219 // Overtake possible lines
220 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
224 while (it != hh.end()) {
226 colinfo_[col].special_ += c;
232 if (brace_open > 0 && brace_open == brace_close)
237 colinfo_[col].lines_ = count(
238 colinfo_[col].special_.begin(),
239 colinfo_[col].special_.end(), '|');
240 LYXERR(Debug::MATHED, "special column separator: `"
241 << to_utf8(colinfo_[col].special_) << '\'');
243 colinfo_[col].lines_ = 0;
244 colinfo_[col].special_.clear();
246 } else if (col >= ncols()) {
247 // Only intercolumn stuff is allowed in the last
250 } else if (c == 'c' || c == 'l' || c == 'r') {
251 colinfo_[col].align_ = static_cast<char>(c);
252 if (!colinfo_[col].special_.empty()) {
253 colinfo_[col].special_ += c;
254 colinfo_[col].lines_ = count(
255 colinfo_[col].special_.begin(),
256 colinfo_[col].special_.end(), '|');
257 LYXERR(Debug::MATHED, "special column separator: `"
258 << to_utf8(colinfo_[col].special_) << '\'');
261 colinfo_[col].lines_ = 0;
262 colinfo_[col].special_.clear();
264 lyxerr << "unknown column separator: '" << c << "'" << endl;
269 col_type n = hh.size();
272 for (col_type col = 0; col < n; ++col)
273 colinfo_[col].align_ = hh[col];
278 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
281 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
282 if (*it == 'c' || *it == 'l' || *it == 'r'||
283 *it == 'p' || *it == 'm' || *it == 'b')
285 // let's have at least one column, even if we did not recognize its
293 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
295 colinfo_[col].align_ = h;
296 if (!colinfo_[col].special_.empty()) {
297 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
298 if (c == 'l' || c == 'c' || c == 'r')
301 // FIXME: Change alignment of p, m and b columns, too
305 char InsetMathGrid::horizontalAlignment(col_type col) const
307 return colinfo_[col].align_;
311 docstring InsetMathGrid::horizontalAlignments() const
314 for (col_type col = 0; col < ncols(); ++col) {
315 if (colinfo_[col].special_.empty()) {
316 res += docstring(colinfo_[col].lines_, '|');
317 res += colinfo_[col].align_;
319 res += colinfo_[col].special_;
321 if (colinfo_[ncols()].special_.empty())
322 return res + docstring(colinfo_[ncols()].lines_, '|');
323 return res + colinfo_[ncols()].special_;
327 void InsetMathGrid::setVerticalAlignment(char c)
333 char InsetMathGrid::verticalAlignment() const
339 InsetMathGrid::col_type InsetMathGrid::ncols() const
341 return colinfo_.size() - 1;
345 InsetMathGrid::row_type InsetMathGrid::nrows() const
347 return rowinfo_.size() - 1;
351 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
353 return idx % ncols();
357 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
359 return idx / ncols();
363 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
365 rowinfo_[row].crskip_ = crskip;
369 Length InsetMathGrid::vcrskip(row_type row) const
371 return rowinfo_[row].crskip_;
375 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
377 // let the cells adjust themselves
378 InsetMathNest::metrics(mi);
380 BufferView & bv = *mi.base.bv;
382 // compute absolute sizes of vertical structure
383 for (row_type row = 0; row < nrows(); ++row) {
386 for (col_type col = 0; col < ncols(); ++col) {
387 Dimension const & dimc = cell(index(row, col)).dimension(bv);
388 asc = max(asc, dimc.asc);
389 desc = max(desc, dimc.des);
391 rowinfo_[row].ascent_ = asc;
392 rowinfo_[row].descent_ = desc;
394 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
395 rowinfo_[nrows()].ascent_ = 0;
396 rowinfo_[nrows()].descent_ = 0;
398 // compute vertical offsets
399 rowinfo_[0].offset_ = 0;
400 for (row_type row = 1; row <= nrows(); ++row) {
401 rowinfo_[row].offset_ =
402 rowinfo_[row - 1].offset_ +
403 rowinfo_[row - 1].descent_ +
404 rowinfo_[row - 1].skipPixels(mi) +
406 rowinfo_[row].lines_ * hlinesep() +
407 rowinfo_[row].ascent_;
410 // adjust vertical offset
417 h = rowinfo_[nrows() - 1].offset_;
420 h = rowinfo_[nrows() - 1].offset_ / 2;
422 for (row_type row = 0; row <= nrows(); ++row)
423 rowinfo_[row].offset_ -= h;
426 // compute absolute sizes of horizontal structure
427 for (col_type col = 0; col < ncols(); ++col) {
429 for (row_type row = 0; row < nrows(); ++row)
430 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
431 colinfo_[col].width_ = wid;
433 colinfo_[ncols()].width_ = 0;
435 // compute horizontal offsets
436 colinfo_[0].offset_ = border();
437 for (col_type col = 1; col <= ncols(); ++col) {
438 colinfo_[col].offset_ =
439 colinfo_[col - 1].offset_ +
440 colinfo_[col - 1].width_ +
441 colinfo_[col - 1].skip_ +
443 colinfo_[col].lines_ * vlinesep();
447 dim.wid = colinfo_[ncols() - 1].offset_
448 + colinfo_[ncols() - 1].width_
449 + vlinesep() * colinfo_[ncols()].lines_
452 dim.asc = - rowinfo_[0].offset_
453 + rowinfo_[0].ascent_
454 + hlinesep() * rowinfo_[0].lines_
457 dim.des = rowinfo_[nrows() - 1].offset_
458 + rowinfo_[nrows() - 1].descent_
459 + hlinesep() * rowinfo_[nrows()].lines_
464 // Increase ws_[i] for 'R' columns (except the first one)
465 for (int i = 1; i < nc_; ++i)
466 if (align_[i] == 'R')
467 ws_[i] += 10 * df_width;
468 // Increase ws_[i] for 'C' column
469 if (align_[0] == 'C')
470 if (ws_[0] < 7 * workwidth / 8)
471 ws_[0] = 7 * workwidth / 8;
475 for (cxrow = row_.begin(); cxrow; ++cxrow) {
478 for (int i = 0; i < nc_; ++i) {
480 if (cxrow->getTab(i) <= 0) {
481 cxrow->setTab(i, df_width);
489 lf = (ws_[i] - cxrow->getTab(i))/2;
493 lf = ws_[i] - cxrow->getTab(i);
496 if (cxrow == row_.begin())
498 else if (cxrow.is_last())
499 lf = ws_[i] - cxrow->getTab(i);
501 lf = (ws_[i] - cxrow->getTab(i))/2;
504 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
505 cxrow->setTab(i, lf + rg);
506 rg = ws_[i] - ww + colsep();
507 if (cxrow == row_.begin())
508 width += ws_[i] + colsep();
510 cxrow->setBaseline(cxrow->getBaseline() - ascent);
513 metricsMarkers2(dim);
514 // Cache the inset dimension.
515 setDimCache(mi, dim);
519 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
521 drawWithMargin(pi, x, y, 1, 1);
525 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
526 int lmargin, int rmargin) const
528 Dimension const dim = dimension(*pi.base.bv);
529 BufferView const & bv = *pi.base.bv;
531 for (idx_type idx = 0; idx < nargs(); ++idx)
532 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
533 y + cellYOffset(idx));
535 for (row_type row = 0; row <= nrows(); ++row)
536 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
537 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
538 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
539 pi.pain.line(x + lmargin + 1, yy,
540 x + dim.width() - rmargin - 1, yy,
544 for (col_type col = 0; col <= ncols(); ++col)
545 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
546 int xx = x + lmargin + colinfo_[col].offset_
547 - i * vlinesep() - vlinesep()/2 - colsep()/2;
548 pi.pain.line(xx, y - dim.ascent() + 1,
549 xx, y + dim.descent() - 1,
552 drawMarkers2(pi, x, y);
556 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
558 // let the cells adjust themselves
559 //InsetMathNest::metrics(mi);
560 for (idx_type i = 0; i < nargs(); ++i)
561 cell(i).metricsT(mi, dim);
563 // compute absolute sizes of vertical structure
564 for (row_type row = 0; row < nrows(); ++row) {
567 for (col_type col = 0; col < ncols(); ++col) {
568 //MathData const & c = cell(index(row, col));
571 asc = max(asc, dimc.ascent());
572 desc = max(desc, dimc.descent());
574 rowinfo_[row].ascent_ = asc;
575 rowinfo_[row].descent_ = desc;
577 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
578 rowinfo_[nrows()].ascent_ = 0;
579 rowinfo_[nrows()].descent_ = 0;
581 // compute vertical offsets
582 rowinfo_[0].offset_ = 0;
583 for (row_type row = 1; row <= nrows(); ++row) {
584 rowinfo_[row].offset_ =
585 rowinfo_[row - 1].offset_ +
586 rowinfo_[row - 1].descent_ +
587 //rowinfo_[row - 1].skipPixels(mi) +
589 //rowinfo_[row].lines_ * hlinesep() +
590 rowinfo_[row].ascent_;
593 // adjust vertical offset
600 h = rowinfo_[nrows() - 1].offset_;
603 h = rowinfo_[nrows() - 1].offset_ / 2;
605 for (row_type row = 0; row <= nrows(); ++row)
606 rowinfo_[row].offset_ -= h;
609 // compute absolute sizes of horizontal structure
610 for (col_type col = 0; col < ncols(); ++col) {
612 for (row_type row = 0; row < nrows(); ++row) {
614 //wid = max(wid, cell(index(row, col)).width());
616 colinfo_[col].width_ = wid;
618 colinfo_[ncols()].width_ = 0;
620 // compute horizontal offsets
621 colinfo_[0].offset_ = border();
622 for (col_type col = 1; col <= ncols(); ++col) {
623 colinfo_[col].offset_ =
624 colinfo_[col - 1].offset_ +
625 colinfo_[col - 1].width_ +
626 colinfo_[col - 1].skip_ +
628 //colinfo_[col].lines_ * vlinesep();
632 dim.wid = colinfo_[ncols() - 1].offset_
633 + colinfo_[ncols() - 1].width_
634 //+ vlinesep() * colinfo_[ncols()].lines_
637 dim.asc = -rowinfo_[0].offset_
638 + rowinfo_[0].ascent_
639 //+ hlinesep() * rowinfo_[0].lines_
642 dim.des = rowinfo_[nrows() - 1].offset_
643 + rowinfo_[nrows() - 1].descent_
644 //+ hlinesep() * rowinfo_[nrows()].lines_
649 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
651 // for (idx_type idx = 0; idx < nargs(); ++idx)
652 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
656 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
659 for (idx_type idx = 0; idx < nargs(); ++idx)
660 cell(idx).updateBuffer(it, utype);
664 docstring InsetMathGrid::eolString(row_type row, bool fragile,
665 bool /*latex*/, bool last_eoln) const
669 if (!rowinfo_[row].crskip_.zero())
670 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
671 else if(!rowinfo_[row].allow_newpage_)
674 // make sure an upcoming '[' does not break anything
675 if (row + 1 < nrows()) {
676 MathData const & c = cell(index(row + 1, 0));
677 if (!c.empty() && c.front()->getChar() == '[')
682 // only add \\ if necessary
683 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
686 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
690 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
692 if (col + 1 == lastcol)
694 return from_ascii(" & ");
698 void InsetMathGrid::addRow(row_type row)
700 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
702 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
704 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
708 void InsetMathGrid::delRow(row_type row)
713 cells_type::iterator it = cells_.begin() + row * ncols();
714 cells_.erase(it, it + ncols());
716 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
717 cellinfo_.erase(jt, jt + ncols());
719 rowinfo_.erase(rowinfo_.begin() + row);
723 void InsetMathGrid::copyRow(row_type row)
726 for (col_type col = 0; col < ncols(); ++col)
727 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
731 void InsetMathGrid::swapRow(row_type row)
735 if (row + 1 == nrows())
737 for (col_type col = 0; col < ncols(); ++col)
738 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
742 void InsetMathGrid::addCol(col_type newcol)
744 const col_type nc = ncols();
745 const row_type nr = nrows();
746 cells_type new_cells((nc + 1) * nr);
747 vector<CellInfo> new_cellinfo((nc + 1) * nr);
749 for (row_type row = 0; row < nr; ++row)
750 for (col_type col = 0; col < nc; ++col) {
751 new_cells[row * (nc + 1) + col + (col >= newcol)]
752 = cells_[row * nc + col];
753 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
754 = cellinfo_[row * nc + col];
756 swap(cells_, new_cells);
757 swap(cellinfo_, new_cellinfo);
760 inf.skip_ = defaultColSpace(newcol);
761 inf.align_ = defaultColAlign(newcol);
762 colinfo_.insert(colinfo_.begin() + newcol, inf);
766 void InsetMathGrid::delCol(col_type col)
772 vector<CellInfo> tmpcellinfo;
773 for (col_type i = 0; i < nargs(); ++i)
774 if (i % ncols() != col) {
775 tmpcells.push_back(cells_[i]);
776 tmpcellinfo.push_back(cellinfo_[i]);
778 swap(cells_, tmpcells);
779 swap(cellinfo_, tmpcellinfo);
781 colinfo_.erase(colinfo_.begin() + col);
785 void InsetMathGrid::copyCol(col_type col)
788 for (row_type row = 0; row < nrows(); ++row)
789 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
793 void InsetMathGrid::swapCol(col_type col)
797 if (col + 1 == ncols())
799 for (row_type row = 0; row < nrows(); ++row)
800 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
804 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
806 col_type c = col(idx);
807 int x = colinfo_[c].offset_;
808 char align = displayColAlign(c, row(idx));
809 Dimension const & celldim = cell(idx).dimension(bv);
810 if (align == 'r' || align == 'R')
811 x += colinfo_[c].width_ - celldim.wid;
812 if (align == 'c' || align == 'C')
813 x += (colinfo_[c].width_ - celldim.wid) / 2;
818 int InsetMathGrid::cellYOffset(idx_type idx) const
820 return rowinfo_[row(idx)].offset_;
824 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
829 cur.idx() -= ncols();
831 if (cur.row() + 1 >= nrows())
833 cur.idx() += ncols();
835 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
840 bool InsetMathGrid::idxBackward(Cursor & cur) const
842 // leave matrix if at the front edge
846 cur.pos() = cur.lastpos();
851 bool InsetMathGrid::idxForward(Cursor & cur) const
853 // leave matrix if at the back edge
854 if (cur.col() + 1 == ncols())
862 bool InsetMathGrid::idxFirst(Cursor & cur) const
869 cur.idx() = (nrows() - 1) * ncols();
872 cur.idx() = ((nrows() - 1) / 2) * ncols();
879 bool InsetMathGrid::idxLast(Cursor & cur) const
883 cur.idx() = ncols() - 1;
886 cur.idx() = nargs() - 1;
889 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
891 cur.pos() = cur.lastpos();
896 bool InsetMathGrid::idxDelete(idx_type & idx)
898 // nothing to do if we have just one row
902 // nothing to do if we are in the middle of the last row of the inset
903 if (idx + ncols() > nargs())
906 // try to delete entire sequence of ncols() empty cells if possible
907 for (idx_type i = idx; i < idx + ncols(); ++i)
908 if (!cell(i).empty())
911 // move cells if necessary
912 for (idx_type i = index(row(idx), 0); i < idx; ++i)
913 swap(cell(i), cell(i + ncols()));
920 // undo effect of Ctrl-Tab (i.e. pull next cell)
921 //if (idx + 1 != nargs())
922 // cell(idx).swap(cell(idx + 1));
924 // we handled the event..
929 // reimplement old behaviour when pressing Delete in the last position
931 void InsetMathGrid::idxGlue(idx_type idx)
933 col_type c = col(idx);
934 if (c + 1 == ncols()) {
935 if (row(idx) + 1 != nrows()) {
936 for (col_type cc = 0; cc < ncols(); ++cc)
937 cell(idx).append(cell(idx + cc + 1));
938 delRow(row(idx) + 1);
941 cell(idx).append(cell(idx + 1));
942 for (col_type cc = c + 2; cc < ncols(); ++cc)
943 cell(idx - c + cc - 1) = cell(idx - c + cc);
944 cell(idx - c + ncols() - 1).clear();
949 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
951 return rowinfo_[row];
955 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
957 return rowinfo_[row];
961 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
963 row_type const ri = row(idx);
964 row_type const r1 = min(row(from), row(to));
965 row_type const r2 = max(row(from), row(to));
966 col_type const ci = col(idx);
967 col_type const c1 = min(col(from), col(to));
968 col_type const c2 = max(col(from), col(to));
969 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
974 void InsetMathGrid::normalize(NormalStream & os) const
977 for (row_type row = 0; row < nrows(); ++row) {
979 for (col_type col = 0; col < ncols(); ++col)
980 os << "[cell " << cell(index(row, col)) << ']';
987 void InsetMathGrid::mathmlize(MathStream & os) const
989 bool const havetable = nrows() > 1 || ncols() > 1;
991 os << MTag("mtable");
992 char const * const celltag = havetable ? "mtd" : "mrow";
993 for (row_type row = 0; row < nrows(); ++row) {
996 for (col_type col = 0; col < ncols(); ++col) {
998 os << cell(index(row, col));
1005 os << ETag("mtable");
1010 // We need to do something about alignment here.
1011 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
1013 bool const havetable = nrows() > 1 || ncols() > 1;
1015 os << cell(index(0, 0));
1018 os << MTag("table", attrib);
1019 for (row_type row = 0; row < nrows(); ++row) {
1021 for (col_type col = 0; col < ncols(); ++col) {
1023 os << cell(index(row, col));
1028 os << ETag("table");
1032 void InsetMathGrid::htmlize(HtmlStream & os) const
1034 htmlize(os, "class='mathtable'");
1038 void InsetMathGrid::write(WriteStream & os) const
1040 write(os, 0, 0, nrows(), ncols());
1043 void InsetMathGrid::write(WriteStream & os,
1044 row_type beg_row, col_type beg_col,
1045 row_type end_row, col_type end_col) const
1047 MathEnsurer ensurer(os, false);
1049 for (row_type row = beg_row; row < end_row; ++row) {
1050 os << verboseHLine(rowinfo_[row].lines_);
1051 // don't write & and empty cells at end of line,
1052 // unless there are vertical lines
1053 col_type lastcol = 0;
1054 bool emptyline = true;
1055 bool last_eoln = true;
1056 for (col_type col = beg_col; col < end_col; ++col) {
1057 bool const empty_cell = cell(index(row, col)).empty();
1060 if (!empty_cell || colinfo_[col + 1].lines_) {
1065 for (col_type col = beg_col; col < lastcol; ++col) {
1066 os << cell(index(row, col));
1067 if (os.pendingBrace())
1068 ModeSpecifier specifier(os, TEXT_MODE);
1069 os << eocString(col, lastcol);
1071 eol = eolString(row, os.fragile(), os.latex(), last_eoln);
1073 // append newline only if line wasn't completely empty
1074 // and the formula is not written on a single line
1075 bool const empty = emptyline && eol.empty();
1076 if (!empty && nrows() > 1)
1079 // @TODO use end_row instead of nrows() ?
1080 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1092 int InsetMathGrid::colsep() const
1098 int InsetMathGrid::rowsep() const
1104 int InsetMathGrid::hlinesep() const
1110 int InsetMathGrid::vlinesep() const
1116 int InsetMathGrid::border() const
1122 void InsetMathGrid::splitCell(Cursor & cur)
1124 if (cur.idx() == cur.lastidx())
1126 MathData ar = cur.cell();
1127 ar.erase(0, cur.pos());
1128 cur.cell().erase(cur.pos(), cur.lastpos());
1131 cur.cell().insert(0, ar);
1135 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1137 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1139 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1141 FuncCode const act = cmd.action();
1144 // insert file functions
1145 case LFUN_LINE_DELETE_FORWARD:
1146 cur.recordUndoInset();
1147 //autocorrect_ = false;
1155 if (cur.idx() > cur.lastidx())
1156 cur.idx() = cur.lastidx();
1157 if (cur.pos() > cur.lastpos())
1158 cur.pos() = cur.lastpos();
1161 case LFUN_CELL_SPLIT:
1166 case LFUN_CELL_BACKWARD:
1168 cur.setSelection(false);
1169 if (!idxPrev(cur)) {
1170 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1175 case LFUN_CELL_FORWARD:
1176 // Can't handle selection by additional 'shift' as this is
1177 // hard bound to LFUN_CELL_BACKWARD
1178 cur.setSelection(false);
1179 if (!idxNext(cur)) {
1180 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1185 case LFUN_NEWLINE_INSERT: {
1186 cur.recordUndoInset();
1187 row_type const r = cur.row();
1191 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1192 swap(cell(index(r, c)), cell(index(r + 1, c)));
1196 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1199 cur.pos() = cur.lastpos();
1200 cur.forceBufferUpdate();
1201 //mathcursor->normalize();
1202 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1206 case LFUN_INSET_MODIFY: {
1207 cur.recordUndoInset();
1208 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1209 istringstream is(to_utf8(cmd.argument()));
1212 if (s != "tabular") {
1213 InsetMathNest::doDispatch(cur, cmd);
1217 if (s == "valign-top")
1218 setVerticalAlignment('t');
1219 else if (s == "valign-middle")
1220 setVerticalAlignment('c');
1221 else if (s == "valign-bottom")
1222 setVerticalAlignment('b');
1223 else if (s == "align-left")
1224 setHorizontalAlignment('l', cur.col());
1225 else if (s == "align-right")
1226 setHorizontalAlignment('r', cur.col());
1227 else if (s == "align-center")
1228 setHorizontalAlignment('c', cur.col());
1229 else if (s == "append-row")
1230 for (int i = 0, n = extractInt(is); i < n; ++i)
1232 else if (s == "delete-row") {
1233 cur.clearSelection(); // bug 4323
1234 for (int i = 0, n = extractInt(is); i < n; ++i) {
1236 if (cur.idx() >= nargs())
1237 cur.idx() -= ncols();
1239 cur.pos() = 0; // trick, see below
1241 else if (s == "copy-row") {
1242 // Here (as later) we save the cursor col/row
1243 // in order to restore it after operation.
1244 row_type const r = cur.row();
1245 col_type const c = cur.col();
1246 for (int i = 0, n = extractInt(is); i < n; ++i)
1248 cur.idx() = index(r, c);
1250 else if (s == "swap-row") {
1252 // Trick to suppress same-idx-means-different-cell
1256 else if (s == "add-hline-above")
1257 rowinfo_[cur.row()].lines_++;
1258 else if (s == "add-hline-below")
1259 rowinfo_[cur.row()+1].lines_++;
1260 else if (s == "delete-hline-above")
1261 rowinfo_[cur.row()].lines_--;
1262 else if (s == "delete-hline-below")
1263 rowinfo_[cur.row()+1].lines_--;
1264 else if (s == "append-column") {
1265 row_type const r = cur.row();
1266 col_type const c = cur.col();
1267 for (int i = 0, n = extractInt(is); i < n; ++i)
1268 addCol(cur.col() + 1);
1269 cur.idx() = index(r, c);
1271 else if (s == "delete-column") {
1272 cur.clearSelection(); // bug 4323
1273 row_type const r = cur.row();
1274 col_type const c = cur.col();
1275 for (int i = 0, n = extractInt(is); i < n; ++i)
1276 delCol(col(cur.idx()));
1277 cur.idx() = index(r, min(c, cur.ncols() - 1));
1278 cur.pos() = 0; // trick, see above
1280 else if (s == "copy-column") {
1281 row_type const r = cur.row();
1282 col_type const c = cur.col();
1284 cur.idx() = index(r, c);
1286 else if (s == "swap-column") {
1288 cur.pos() = 0; // trick, see above
1290 else if (s == "add-vline-left") {
1291 colinfo_[cur.col()].lines_++;
1292 if (!colinfo_[cur.col()].special_.empty())
1293 colinfo_[cur.col()].special_ += '|';
1295 else if (s == "add-vline-right") {
1296 colinfo_[cur.col()+1].lines_++;
1297 if (!colinfo_[cur.col()+1].special_.empty())
1298 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1300 else if (s == "delete-vline-left") {
1301 colinfo_[cur.col()].lines_--;
1302 docstring & special = colinfo_[cur.col()].special_;
1303 if (!special.empty()) {
1304 docstring::size_type i = special.rfind('|');
1305 LASSERT(i != docstring::npos, break);
1306 special.erase(i, 1);
1309 else if (s == "delete-vline-right") {
1310 colinfo_[cur.col()+1].lines_--;
1311 docstring & special = colinfo_[cur.col()+1].special_;
1312 if (!special.empty()) {
1313 docstring::size_type i = special.find('|');
1314 LASSERT(i != docstring::npos, break);
1315 special.erase(i, 1);
1322 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1323 //lyxerr << "returning FINISHED_LEFT" << endl;
1327 case LFUN_CLIPBOARD_PASTE:
1328 parseflg |= Parse::VERBATIM;
1331 if (cur.currentMode() <= TEXT_MODE)
1332 parseflg |= Parse::TEXTMODE;
1333 cur.message(_("Paste"));
1334 cap::replaceSelection(cur);
1336 if (cmd.argument().empty() && !theClipboard().isInternal())
1337 topaste = theClipboard().getAsText(Clipboard::PlainTextType);
1339 idocstringstream is(cmd.argument());
1342 topaste = cap::selection(n);
1344 InsetMathGrid grid(buffer_, 1, 1);
1345 if (!topaste.empty())
1346 if ((topaste.size() == 1 && isAscii(topaste))
1347 || !mathed_parse_normal(grid, topaste, parseflg)) {
1349 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1352 bool hline_enabled = false;
1353 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
1355 if (getStatus(cur, fr, status))
1356 hline_enabled = status.enabled();
1357 if (grid.nargs() == 1) {
1358 // single cell/part of cell
1359 cur.recordUndoInset();
1360 cur.cell().insert(cur.pos(), grid.cell(0));
1361 cur.pos() += grid.cell(0).size();
1363 rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_;
1365 for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) {
1366 cur.cell().insert(0,
1367 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1373 cur.recordUndoInset();
1374 col_type const numcols =
1375 min(grid.ncols(), ncols() - col(cur.idx()));
1376 row_type const numrows =
1377 min(grid.nrows(), nrows() - cur.row());
1378 for (row_type r = 0; r < numrows; ++r) {
1379 for (col_type c = 0; c < numcols; ++c) {
1380 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1381 cell(i).insert(0, grid.cell(grid.index(r, c)));
1384 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1386 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1387 idx_type i = index(r + cur.row(), 0);
1389 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1392 // append the left over horizontal cells to the last column
1393 idx_type i = index(r + cur.row(), ncols() - 1);
1394 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1395 cell(i).append(grid.cell(grid.index(r, c)));
1397 // append the left over vertical cells to the last _cell_
1398 idx_type i = nargs() - 1;
1399 for (row_type r = numrows; r < grid.nrows(); ++r) {
1400 for (col_type c = 0; c < grid.ncols(); ++c)
1401 cell(i).append(grid.cell(grid.index(r, c)));
1403 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1405 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1407 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1412 cur.clearSelection(); // bug 393
1413 // FIXME audit setBuffer calls
1414 cur.inset().setBuffer(*buffer_);
1415 cur.forceBufferUpdate();
1420 case LFUN_LINE_BEGIN_SELECT:
1421 case LFUN_LINE_BEGIN:
1422 case LFUN_WORD_BACKWARD_SELECT:
1423 case LFUN_WORD_BACKWARD:
1424 case LFUN_WORD_LEFT_SELECT:
1425 case LFUN_WORD_LEFT:
1426 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1427 act == LFUN_WORD_LEFT_SELECT ||
1428 act == LFUN_LINE_BEGIN_SELECT);
1429 cur.macroModeClose();
1430 if (cur.pos() != 0) {
1432 } else if (cur.idx() % cur.ncols() != 0) {
1433 cur.idx() -= cur.idx() % cur.ncols();
1435 } else if (cur.idx() != 0) {
1439 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1444 case LFUN_WORD_FORWARD_SELECT:
1445 case LFUN_WORD_FORWARD:
1446 case LFUN_WORD_RIGHT_SELECT:
1447 case LFUN_WORD_RIGHT:
1448 case LFUN_LINE_END_SELECT:
1450 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1451 act == LFUN_WORD_RIGHT_SELECT ||
1452 act == LFUN_LINE_END_SELECT);
1453 cur.macroModeClose();
1455 if (cur.pos() != cur.lastpos()) {
1456 cur.pos() = cur.lastpos();
1457 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1458 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1459 cur.pos() = cur.lastpos();
1460 } else if (cur.idx() != cur.lastidx()) {
1461 cur.idx() = cur.lastidx();
1462 cur.pos() = cur.lastpos();
1464 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1470 InsetMathNest::doDispatch(cur, cmd);
1475 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1476 FuncStatus & status) const
1478 switch (cmd.action()) {
1479 case LFUN_INSET_MODIFY: {
1480 istringstream is(to_utf8(cmd.argument()));
1483 if (s != "tabular") {
1484 // We only now about table actions here.
1487 if (&cur.inset() != this) {
1488 // Table actions requires that the cursor is _inside_ the
1490 status.setEnabled(false);
1491 status.message(from_utf8(N_("Cursor not in table")));
1495 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1496 status.setEnabled(false);
1497 status.message(from_utf8(N_("Only one row")));
1501 (s == "delete-column" || s == "swap-column")) {
1502 status.setEnabled(false);
1503 status.message(from_utf8(N_("Only one column")));
1506 if ((rowinfo_[cur.row()].lines_ == 0 &&
1507 s == "delete-hline-above") ||
1508 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1509 s == "delete-hline-below")) {
1510 status.setEnabled(false);
1511 status.message(from_utf8(N_("No hline to delete")));
1515 if ((colinfo_[cur.col()].lines_ == 0 &&
1516 s == "delete-vline-left") ||
1517 (colinfo_[cur.col() + 1].lines_ == 0 &&
1518 s == "delete-vline-right")) {
1519 status.setEnabled(false);
1520 status.message(from_utf8(N_("No vline to delete")));
1523 if (s == "valign-top" || s == "valign-middle" ||
1524 s == "valign-bottom" || s == "align-left" ||
1525 s == "align-right" || s == "align-center") {
1526 status.setEnabled(true);
1527 char const ha = horizontalAlignment(cur.col());
1528 char const va = verticalAlignment();
1529 status.setOnOff((s == "align-left" && ha == 'l')
1530 || (s == "align-right" && ha == 'r')
1531 || (s == "align-center" && ha == 'c')
1532 || (s == "valign-top" && va == 't')
1533 || (s == "valign-bottom" && va == 'b')
1534 || (s == "valign-middle" && va == 'c'));
1537 if (s == "append-row" || s == "delete-row" ||
1538 s == "copy-row" || s == "swap-row" ||
1539 s == "add-hline-above" || s == "add-hline-below" ||
1540 s == "delete-hline-above" || s == "delete-hline-below" ||
1541 s == "append-column" || s == "delete-column" ||
1542 s == "copy-column" || s == "swap-column" ||
1543 s == "add-vline-left" || s == "add-vline-right" ||
1544 s == "delete-vline-left" || s == "delete-vline-right") {
1545 status.setEnabled(true);
1547 status.setEnabled(false);
1548 status.message(bformat(
1549 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1553 // FIXME: What did this code do?
1554 // Please check whether it is still needed!
1555 // should be more precise
1556 if (v_align_ == '\0') {
1557 status.enable(true);
1560 if (cmd.argument().empty()) {
1561 status.enable(false);
1564 if (!contains("tcb", cmd.argument()[0])) {
1565 status.enable(false);
1568 status.setOnOff(cmd.argument()[0] == v_align_);
1569 status.setEnabled(true);
1574 case LFUN_CELL_SPLIT:
1575 status.setEnabled(cur.idx() != cur.lastidx());
1578 case LFUN_CELL_BACKWARD:
1579 case LFUN_CELL_FORWARD:
1580 status.setEnabled(true);
1586 return InsetMathNest::getStatus(cur, cmd, status);