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;
48 static docstring verboseHLine(int n)
51 for (int i = 0; i < n; ++i)
59 static int extractInt(istream & is)
63 return (num == 0) ? 1 : num;
67 static void resetGrid(InsetMathGrid & grid)
69 while (grid.ncols() > 1)
70 grid.delCol(grid.ncols());
71 while (grid.nrows() > 1)
72 grid.delRow(grid.nrows());
73 grid.cell(0).erase(0, grid.cell(0).size());
79 //////////////////////////////////////////////////////////////
82 InsetMathGrid::CellInfo::CellInfo()
88 //////////////////////////////////////////////////////////////
91 InsetMathGrid::RowInfo::RowInfo()
92 : lines_(0), skip_(0), allow_newpage_(true)
97 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
99 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
100 return crskip_.inPixels(mi.base.textwidth,
101 fm.width(char_type('M')));
106 //////////////////////////////////////////////////////////////
109 InsetMathGrid::ColInfo::ColInfo()
110 : align_('c'), lines_(0)
114 //////////////////////////////////////////////////////////////
117 InsetMathGrid::InsetMathGrid(Buffer * buf)
118 : InsetMathNest(buf, 1),
128 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
129 : InsetMathNest(buf, m * n),
139 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
141 : InsetMathNest(buf, m * n),
148 setVerticalAlignment(v);
149 setHorizontalAlignments(h);
153 Inset * InsetMathGrid::clone() const
155 return new InsetMathGrid(*this);
159 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
161 return col + ncols() * row;
165 void InsetMathGrid::setDefaults()
168 lyxerr << "positive number of columns expected" << endl;
170 // lyxerr << "positive number of rows expected" << endl;
171 for (col_type col = 0; col < ncols(); ++col) {
172 colinfo_[col].align_ = defaultColAlign(col);
173 colinfo_[col].skip_ = defaultColSpace(col);
174 colinfo_[col].special_.clear();
179 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
182 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
185 colinfo_[col].lines_++;
186 } else if ((c == 'p' || c == 'm' || c == 'b'||
187 c == '!' || c == '@' || c == '>' || c == '<') &&
188 it + 1 != hh.end() && *(it + 1) == '{') {
189 // @{decl.} and p{width} are standard LaTeX, the
190 // others are extensions by array.sty
191 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
193 // this declares a new column
195 // Only intercolumn stuff is allowed
196 // in the last dummy column
198 colinfo_[col].align_ = 'l';
200 // this is intercolumn stuff
201 if (colinfo_[col].special_.empty())
202 // Overtake possible lines
203 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
207 while (it != hh.end()) {
209 colinfo_[col].special_ += c;
215 if (brace_open > 0 && brace_open == brace_close)
220 colinfo_[col].lines_ = count(
221 colinfo_[col].special_.begin(),
222 colinfo_[col].special_.end(), '|');
223 LYXERR(Debug::MATHED, "special column separator: `"
224 << to_utf8(colinfo_[col].special_) << '\'');
226 colinfo_[col].lines_ = 0;
227 colinfo_[col].special_.clear();
229 } else if (col >= ncols()) {
230 // Only intercolumn stuff is allowed in the last
233 } else if (c == 'c' || c == 'l' || c == 'r') {
234 colinfo_[col].align_ = static_cast<char>(c);
235 if (!colinfo_[col].special_.empty()) {
236 colinfo_[col].special_ += c;
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_) << '\'');
244 colinfo_[col].lines_ = 0;
245 colinfo_[col].special_.clear();
247 lyxerr << "unknown column separator: '" << c << "'" << endl;
252 col_type n = hh.size();
255 for (col_type col = 0; col < n; ++col)
256 colinfo_[col].align_ = hh[col];
261 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
264 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
265 if (*it == 'c' || *it == 'l' || *it == 'r'||
266 *it == 'p' || *it == 'm' || *it == 'b')
268 // let's have at least one column, even if we did not recognize its
276 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
278 colinfo_[col].align_ = h;
279 if (!colinfo_[col].special_.empty()) {
280 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
281 if (c == 'l' || c == 'c' || c == 'r')
284 // FIXME: Change alignment of p, m and b columns, too
288 char InsetMathGrid::horizontalAlignment(col_type col) const
290 return colinfo_[col].align_;
294 docstring InsetMathGrid::horizontalAlignments() const
297 for (col_type col = 0; col < ncols(); ++col) {
298 if (colinfo_[col].special_.empty()) {
299 res += docstring(colinfo_[col].lines_, '|');
300 res += colinfo_[col].align_;
302 res += colinfo_[col].special_;
304 if (colinfo_[ncols()].special_.empty())
305 return res + docstring(colinfo_[ncols()].lines_, '|');
306 return res + colinfo_[ncols()].special_;
310 void InsetMathGrid::setVerticalAlignment(char c)
316 char InsetMathGrid::verticalAlignment() const
322 InsetMathGrid::col_type InsetMathGrid::ncols() const
324 return colinfo_.size() - 1;
328 InsetMathGrid::row_type InsetMathGrid::nrows() const
330 return rowinfo_.size() - 1;
334 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
336 return idx % ncols();
340 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
342 return idx / ncols();
346 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
348 rowinfo_[row].crskip_ = crskip;
352 Length InsetMathGrid::vcrskip(row_type row) const
354 return rowinfo_[row].crskip_;
358 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
360 // let the cells adjust themselves
361 InsetMathNest::metrics(mi);
363 BufferView & bv = *mi.base.bv;
365 // compute absolute sizes of vertical structure
366 for (row_type row = 0; row < nrows(); ++row) {
369 for (col_type col = 0; col < ncols(); ++col) {
370 Dimension const & dimc = cell(index(row, col)).dimension(bv);
371 asc = max(asc, dimc.asc);
372 desc = max(desc, dimc.des);
374 rowinfo_[row].ascent_ = asc;
375 rowinfo_[row].descent_ = desc;
377 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
378 rowinfo_[nrows()].ascent_ = 0;
379 rowinfo_[nrows()].descent_ = 0;
381 // compute vertical offsets
382 rowinfo_[0].offset_ = 0;
383 for (row_type row = 1; row <= nrows(); ++row) {
384 rowinfo_[row].offset_ =
385 rowinfo_[row - 1].offset_ +
386 rowinfo_[row - 1].descent_ +
387 rowinfo_[row - 1].skipPixels(mi) +
389 rowinfo_[row].lines_ * hlinesep() +
390 rowinfo_[row].ascent_;
393 // adjust vertical offset
400 h = rowinfo_[nrows() - 1].offset_;
403 h = rowinfo_[nrows() - 1].offset_ / 2;
405 for (row_type row = 0; row <= nrows(); ++row)
406 rowinfo_[row].offset_ -= h;
409 // compute absolute sizes of horizontal structure
410 for (col_type col = 0; col < ncols(); ++col) {
412 for (row_type row = 0; row < nrows(); ++row)
413 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
414 colinfo_[col].width_ = wid;
416 colinfo_[ncols()].width_ = 0;
418 // compute horizontal offsets
419 colinfo_[0].offset_ = border();
420 for (col_type col = 1; col <= ncols(); ++col) {
421 colinfo_[col].offset_ =
422 colinfo_[col - 1].offset_ +
423 colinfo_[col - 1].width_ +
424 colinfo_[col - 1].skip_ +
426 colinfo_[col].lines_ * vlinesep();
430 dim.wid = colinfo_[ncols() - 1].offset_
431 + colinfo_[ncols() - 1].width_
432 + vlinesep() * colinfo_[ncols()].lines_
435 dim.asc = - rowinfo_[0].offset_
436 + rowinfo_[0].ascent_
437 + hlinesep() * rowinfo_[0].lines_
440 dim.des = rowinfo_[nrows() - 1].offset_
441 + rowinfo_[nrows() - 1].descent_
442 + hlinesep() * rowinfo_[nrows()].lines_
447 // Increase ws_[i] for 'R' columns (except the first one)
448 for (int i = 1; i < nc_; ++i)
449 if (align_[i] == 'R')
450 ws_[i] += 10 * df_width;
451 // Increase ws_[i] for 'C' column
452 if (align_[0] == 'C')
453 if (ws_[0] < 7 * workwidth / 8)
454 ws_[0] = 7 * workwidth / 8;
458 for (cxrow = row_.begin(); cxrow; ++cxrow) {
461 for (int i = 0; i < nc_; ++i) {
463 if (cxrow->getTab(i) <= 0) {
464 cxrow->setTab(i, df_width);
472 lf = (ws_[i] - cxrow->getTab(i))/2;
476 lf = ws_[i] - cxrow->getTab(i);
479 if (cxrow == row_.begin())
481 else if (cxrow.is_last())
482 lf = ws_[i] - cxrow->getTab(i);
484 lf = (ws_[i] - cxrow->getTab(i))/2;
487 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
488 cxrow->setTab(i, lf + rg);
489 rg = ws_[i] - ww + colsep();
490 if (cxrow == row_.begin())
491 width += ws_[i] + colsep();
493 cxrow->setBaseline(cxrow->getBaseline() - ascent);
496 metricsMarkers2(dim);
497 // Cache the inset dimension.
498 setDimCache(mi, dim);
502 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
504 drawWithMargin(pi, x, y, 1, 1);
508 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
509 int lmargin, int rmargin) const
511 Dimension const dim = dimension(*pi.base.bv);
512 BufferView const & bv = *pi.base.bv;
514 for (idx_type idx = 0; idx < nargs(); ++idx)
515 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
516 y + cellYOffset(idx));
518 for (row_type row = 0; row <= nrows(); ++row)
519 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
520 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
521 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
522 pi.pain.line(x + lmargin + 1, yy,
523 x + dim.width() - rmargin - 1, yy,
527 for (col_type col = 0; col <= ncols(); ++col)
528 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
529 int xx = x + lmargin + colinfo_[col].offset_
530 - i * vlinesep() - vlinesep()/2 - colsep()/2;
531 pi.pain.line(xx, y - dim.ascent() + 1,
532 xx, y + dim.descent() - 1,
535 drawMarkers2(pi, x, y);
539 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
541 // let the cells adjust themselves
542 //InsetMathNest::metrics(mi);
543 for (idx_type i = 0; i < nargs(); ++i)
544 cell(i).metricsT(mi, dim);
546 // compute absolute sizes of vertical structure
547 for (row_type row = 0; row < nrows(); ++row) {
550 for (col_type col = 0; col < ncols(); ++col) {
551 //MathData const & c = cell(index(row, col));
554 asc = max(asc, dimc.ascent());
555 desc = max(desc, dimc.descent());
557 rowinfo_[row].ascent_ = asc;
558 rowinfo_[row].descent_ = desc;
560 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
561 rowinfo_[nrows()].ascent_ = 0;
562 rowinfo_[nrows()].descent_ = 0;
564 // compute vertical offsets
565 rowinfo_[0].offset_ = 0;
566 for (row_type row = 1; row <= nrows(); ++row) {
567 rowinfo_[row].offset_ =
568 rowinfo_[row - 1].offset_ +
569 rowinfo_[row - 1].descent_ +
570 //rowinfo_[row - 1].skipPixels(mi) +
572 //rowinfo_[row].lines_ * hlinesep() +
573 rowinfo_[row].ascent_;
576 // adjust vertical offset
583 h = rowinfo_[nrows() - 1].offset_;
586 h = rowinfo_[nrows() - 1].offset_ / 2;
588 for (row_type row = 0; row <= nrows(); ++row)
589 rowinfo_[row].offset_ -= h;
592 // compute absolute sizes of horizontal structure
593 for (col_type col = 0; col < ncols(); ++col) {
595 for (row_type row = 0; row < nrows(); ++row) {
597 //wid = max(wid, cell(index(row, col)).width());
599 colinfo_[col].width_ = wid;
601 colinfo_[ncols()].width_ = 0;
603 // compute horizontal offsets
604 colinfo_[0].offset_ = border();
605 for (col_type col = 1; col <= ncols(); ++col) {
606 colinfo_[col].offset_ =
607 colinfo_[col - 1].offset_ +
608 colinfo_[col - 1].width_ +
609 colinfo_[col - 1].skip_ +
611 //colinfo_[col].lines_ * vlinesep();
615 dim.wid = colinfo_[ncols() - 1].offset_
616 + colinfo_[ncols() - 1].width_
617 //+ vlinesep() * colinfo_[ncols()].lines_
620 dim.asc = -rowinfo_[0].offset_
621 + rowinfo_[0].ascent_
622 //+ hlinesep() * rowinfo_[0].lines_
625 dim.des = rowinfo_[nrows() - 1].offset_
626 + rowinfo_[nrows() - 1].descent_
627 //+ hlinesep() * rowinfo_[nrows()].lines_
632 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
634 // for (idx_type idx = 0; idx < nargs(); ++idx)
635 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
639 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
642 for (idx_type idx = 0; idx < nargs(); ++idx)
643 cell(idx).updateBuffer(it, utype);
647 docstring InsetMathGrid::eolString(row_type row, bool fragile, bool last_eoln) const
649 // Never add \\ on the last empty line of substack, tabular and xymatrix
650 if (asSubstackInset() || asTabularInset() || asXYMatrixInset())
655 if (!rowinfo_[row].crskip_.zero())
656 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
657 else if(!rowinfo_[row].allow_newpage_)
660 // make sure an upcoming '[' does not break anything
661 if (row + 1 < nrows()) {
662 MathData const & c = cell(index(row + 1, 0));
663 if (c.size() && c.front()->getChar() == '[')
668 // only add \\ if necessary
669 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
672 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
676 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
678 if (col + 1 == lastcol)
680 return from_ascii(" & ");
684 void InsetMathGrid::addRow(row_type row)
686 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
688 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
690 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
694 void InsetMathGrid::appendRow()
696 rowinfo_.push_back(RowInfo());
697 //cells_.insert(cells_.end(), ncols(), MathData());
698 for (col_type col = 0; col < ncols(); ++col) {
699 cells_.push_back(cells_type::value_type());
700 cellinfo_.push_back(CellInfo());
705 void InsetMathGrid::delRow(row_type row)
710 cells_type::iterator it = cells_.begin() + row * ncols();
711 cells_.erase(it, it + ncols());
713 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
714 cellinfo_.erase(jt, jt + ncols());
716 rowinfo_.erase(rowinfo_.begin() + row);
720 void InsetMathGrid::copyRow(row_type row)
723 for (col_type col = 0; col < ncols(); ++col)
724 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
728 void InsetMathGrid::swapRow(row_type row)
732 if (row + 1 == nrows())
734 for (col_type col = 0; col < ncols(); ++col)
735 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
739 void InsetMathGrid::addCol(col_type newcol)
741 const col_type nc = ncols();
742 const row_type nr = nrows();
743 cells_type new_cells((nc + 1) * nr);
744 vector<CellInfo> new_cellinfo((nc + 1) * nr);
746 for (row_type row = 0; row < nr; ++row)
747 for (col_type col = 0; col < nc; ++col) {
748 new_cells[row * (nc + 1) + col + (col >= newcol)]
749 = cells_[row * nc + col];
750 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
751 = cellinfo_[row * nc + col];
753 swap(cells_, new_cells);
754 swap(cellinfo_, new_cellinfo);
757 inf.skip_ = defaultColSpace(newcol);
758 inf.align_ = defaultColAlign(newcol);
759 colinfo_.insert(colinfo_.begin() + newcol, inf);
763 void InsetMathGrid::delCol(col_type col)
769 vector<CellInfo> tmpcellinfo;
770 for (col_type i = 0; i < nargs(); ++i)
771 if (i % ncols() != col) {
772 tmpcells.push_back(cells_[i]);
773 tmpcellinfo.push_back(cellinfo_[i]);
775 swap(cells_, tmpcells);
776 swap(cellinfo_, tmpcellinfo);
778 colinfo_.erase(colinfo_.begin() + col);
782 void InsetMathGrid::copyCol(col_type col)
785 for (row_type row = 0; row < nrows(); ++row)
786 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
790 void InsetMathGrid::swapCol(col_type col)
794 if (col + 1 == ncols())
796 for (row_type row = 0; row < nrows(); ++row)
797 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
801 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
803 col_type c = col(idx);
804 int x = colinfo_[c].offset_;
805 char align = colinfo_[c].align_;
806 Dimension const & celldim = cell(idx).dimension(bv);
807 if (align == 'r' || align == 'R')
808 x += colinfo_[c].width_ - celldim.wid;
809 if (align == 'c' || align == 'C')
810 x += (colinfo_[c].width_ - celldim.wid) / 2;
815 int InsetMathGrid::cellYOffset(idx_type idx) const
817 return rowinfo_[row(idx)].offset_;
821 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
826 cur.idx() -= ncols();
828 if (cur.row() + 1 >= nrows())
830 cur.idx() += ncols();
832 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
837 bool InsetMathGrid::idxBackward(Cursor & cur) const
839 // leave matrix if at the front edge
843 cur.pos() = cur.lastpos();
848 bool InsetMathGrid::idxForward(Cursor & cur) const
850 // leave matrix if at the back edge
851 if (cur.col() + 1 == ncols())
859 bool InsetMathGrid::idxFirst(Cursor & cur) const
866 cur.idx() = (nrows() - 1) * ncols();
869 cur.idx() = ((nrows() - 1) / 2) * ncols();
876 bool InsetMathGrid::idxLast(Cursor & cur) const
880 cur.idx() = ncols() - 1;
883 cur.idx() = nargs() - 1;
886 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
888 cur.pos() = cur.lastpos();
893 bool InsetMathGrid::idxDelete(idx_type & idx)
895 // nothing to do if we have just one row
899 // nothing to do if we are in the middle of the last row of the inset
900 if (idx + ncols() > nargs())
903 // try to delete entire sequence of ncols() empty cells if possible
904 for (idx_type i = idx; i < idx + ncols(); ++i)
908 // move cells if necessary
909 for (idx_type i = index(row(idx), 0); i < idx; ++i)
910 swap(cell(i), cell(i + ncols()));
917 // undo effect of Ctrl-Tab (i.e. pull next cell)
918 //if (idx + 1 != nargs())
919 // cell(idx).swap(cell(idx + 1));
921 // we handled the event..
926 // reimplement old behaviour when pressing Delete in the last position
928 void InsetMathGrid::idxGlue(idx_type idx)
930 col_type c = col(idx);
931 if (c + 1 == ncols()) {
932 if (row(idx) + 1 != nrows()) {
933 for (col_type cc = 0; cc < ncols(); ++cc)
934 cell(idx).append(cell(idx + cc + 1));
935 delRow(row(idx) + 1);
938 cell(idx).append(cell(idx + 1));
939 for (col_type cc = c + 2; cc < ncols(); ++cc)
940 cell(idx - c + cc - 1) = cell(idx - c + cc);
941 cell(idx - c + ncols() - 1).clear();
946 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
948 return rowinfo_[row];
952 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
954 return rowinfo_[row];
958 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
960 row_type const ri = row(idx);
961 row_type const r1 = min(row(from), row(to));
962 row_type const r2 = max(row(from), row(to));
963 col_type const ci = col(idx);
964 col_type const c1 = min(col(from), col(to));
965 col_type const c2 = max(col(from), col(to));
966 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
971 void InsetMathGrid::normalize(NormalStream & os) const
974 for (row_type row = 0; row < nrows(); ++row) {
976 for (col_type col = 0; col < ncols(); ++col)
977 os << "[cell " << cell(index(row, col)) << ']';
984 void InsetMathGrid::mathmlize(MathStream & os) const
986 bool const havetable = nrows() > 1 || ncols() > 1;
988 os << MTag("mtable");
989 char const * const celltag = havetable ? "mtd" : "mrow";
990 for (row_type row = 0; row < nrows(); ++row) {
993 for (col_type col = 0; col < ncols(); ++col) {
995 os << cell(index(row, col));
1002 os << ETag("mtable");
1007 // We need to do something about alignment here.
1008 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
1010 bool const havetable = nrows() > 1 || ncols() > 1;
1012 os << cell(index(0, 0));
1015 os << MTag("table", attrib);
1016 for (row_type row = 0; row < nrows(); ++row) {
1018 for (col_type col = 0; col < ncols(); ++col) {
1020 os << cell(index(row, col));
1025 os << ETag("table");
1029 void InsetMathGrid::htmlize(HtmlStream & os) const
1031 htmlize(os, "class='mathtable'");
1035 void InsetMathGrid::write(WriteStream & os) const
1037 write(os, 0, 0, nrows(), ncols());
1040 void InsetMathGrid::write(WriteStream & os,
1041 row_type beg_row, col_type beg_col,
1042 row_type end_row, col_type end_col) const
1044 MathEnsurer ensurer(os, false);
1046 for (row_type row = beg_row; row < end_row; ++row) {
1047 os << verboseHLine(rowinfo_[row].lines_);
1048 // don't write & and empty cells at end of line,
1049 // unless there are vertical lines
1050 col_type lastcol = 0;
1051 bool emptyline = true;
1052 bool last_eoln = true;
1053 for (col_type col = beg_col; col < end_col; ++col) {
1054 bool const empty_cell = cell(index(row, col)).empty();
1057 if (!empty_cell || colinfo_[col + 1].lines_) {
1062 for (col_type col = beg_col; col < lastcol; ++col) {
1063 os << cell(index(row, col));
1064 if (os.pendingBrace())
1065 ModeSpecifier specifier(os, TEXT_MODE);
1066 os << eocString(col, lastcol);
1068 eol = eolString(row, os.fragile(), last_eoln);
1070 // append newline only if line wasn't completely empty
1071 // and the formula is not written on a single line
1072 bool const empty = emptyline && eol.empty();
1073 if (!empty && nrows() > 1)
1076 // @TODO use end_row instead of nrows() ?
1077 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1089 int InsetMathGrid::colsep() const
1095 int InsetMathGrid::rowsep() const
1101 int InsetMathGrid::hlinesep() const
1107 int InsetMathGrid::vlinesep() const
1113 int InsetMathGrid::border() const
1119 void InsetMathGrid::splitCell(Cursor & cur)
1121 if (cur.idx() == cur.lastidx())
1123 MathData ar = cur.cell();
1124 ar.erase(0, cur.pos());
1125 cur.cell().erase(cur.pos(), cur.lastpos());
1128 cur.cell().insert(0, ar);
1132 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1134 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1136 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1138 FuncCode const act = cmd.action();
1141 // insert file functions
1142 case LFUN_LINE_DELETE:
1143 cur.recordUndoInset();
1144 //autocorrect_ = false;
1152 if (cur.idx() > cur.lastidx())
1153 cur.idx() = cur.lastidx();
1154 if (cur.pos() > cur.lastpos())
1155 cur.pos() = cur.lastpos();
1158 case LFUN_CELL_SPLIT:
1163 case LFUN_CELL_BACKWARD:
1165 cur.setSelection(false);
1166 if (!idxPrev(cur)) {
1167 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1172 case LFUN_CELL_FORWARD:
1173 // Can't handle selection by additional 'shift' as this is
1174 // hard bound to LFUN_CELL_BACKWARD
1175 cur.setSelection(false);
1176 if (!idxNext(cur)) {
1177 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1182 case LFUN_NEWLINE_INSERT: {
1183 cur.recordUndoInset();
1184 row_type const r = cur.row();
1188 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1189 swap(cell(index(r, c)), cell(index(r + 1, c)));
1193 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1196 cur.pos() = cur.lastpos();
1198 //mathcursor->normalize();
1199 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1203 case LFUN_INSET_MODIFY: {
1204 cur.recordUndoInset();
1205 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1206 istringstream is(to_utf8(cmd.argument()));
1209 if (s != "tabular") {
1210 InsetMathNest::doDispatch(cur, cmd);
1214 if (s == "valign-top")
1215 setVerticalAlignment('t');
1216 else if (s == "valign-middle")
1217 setVerticalAlignment('c');
1218 else if (s == "valign-bottom")
1219 setVerticalAlignment('b');
1220 else if (s == "align-left")
1221 setHorizontalAlignment('l', cur.col());
1222 else if (s == "align-right")
1223 setHorizontalAlignment('r', cur.col());
1224 else if (s == "align-center")
1225 setHorizontalAlignment('c', cur.col());
1226 else if (s == "append-row")
1227 for (int i = 0, n = extractInt(is); i < n; ++i)
1229 else if (s == "delete-row") {
1230 cur.clearSelection(); // bug 4323
1231 for (int i = 0, n = extractInt(is); i < n; ++i) {
1233 if (cur.idx() >= nargs())
1234 cur.idx() -= ncols();
1236 cur.pos() = 0; // trick, see below
1238 else if (s == "copy-row") {
1239 // Here (as later) we save the cursor col/row
1240 // in order to restore it after operation.
1241 row_type const r = cur.row();
1242 col_type const c = cur.col();
1243 for (int i = 0, n = extractInt(is); i < n; ++i)
1245 cur.idx() = index(r, c);
1247 else if (s == "swap-row") {
1249 // Trick to suppress same-idx-means-different-cell
1253 else if (s == "add-hline-above")
1254 rowinfo_[cur.row()].lines_++;
1255 else if (s == "add-hline-below")
1256 rowinfo_[cur.row()+1].lines_++;
1257 else if (s == "delete-hline-above")
1258 rowinfo_[cur.row()].lines_--;
1259 else if (s == "delete-hline-below")
1260 rowinfo_[cur.row()+1].lines_--;
1261 else if (s == "append-column") {
1262 row_type const r = cur.row();
1263 col_type const c = cur.col();
1264 for (int i = 0, n = extractInt(is); i < n; ++i)
1265 addCol(cur.col() + 1);
1266 cur.idx() = index(r, c);
1268 else if (s == "delete-column") {
1269 cur.clearSelection(); // bug 4323
1270 row_type const r = cur.row();
1271 col_type const c = cur.col();
1272 for (int i = 0, n = extractInt(is); i < n; ++i)
1273 delCol(col(cur.idx()));
1274 cur.idx() = index(r, min(c, cur.ncols() - 1));
1275 cur.pos() = 0; // trick, see above
1277 else if (s == "copy-column") {
1278 row_type const r = cur.row();
1279 col_type const c = cur.col();
1281 cur.idx() = index(r, c);
1283 else if (s == "swap-column") {
1285 cur.pos() = 0; // trick, see above
1287 else if (s == "add-vline-left") {
1288 colinfo_[cur.col()].lines_++;
1289 if (!colinfo_[cur.col()].special_.empty())
1290 colinfo_[cur.col()].special_ += '|';
1292 else if (s == "add-vline-right") {
1293 colinfo_[cur.col()+1].lines_++;
1294 if (!colinfo_[cur.col()+1].special_.empty())
1295 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1297 else if (s == "delete-vline-left") {
1298 colinfo_[cur.col()].lines_--;
1299 docstring & special = colinfo_[cur.col()].special_;
1300 if (!special.empty()) {
1301 docstring::size_type i = special.rfind('|');
1302 LASSERT(i != docstring::npos, /**/);
1303 special.erase(i, 1);
1306 else if (s == "delete-vline-right") {
1307 colinfo_[cur.col()+1].lines_--;
1308 docstring & special = colinfo_[cur.col()+1].special_;
1309 if (!special.empty()) {
1310 docstring::size_type i = special.find('|');
1311 LASSERT(i != docstring::npos, /**/);
1312 special.erase(i, 1);
1319 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1320 //lyxerr << "returning FINISHED_LEFT" << endl;
1324 case LFUN_CLIPBOARD_PASTE:
1325 parseflg |= Parse::VERBATIM;
1328 if (cur.currentMode() <= TEXT_MODE)
1329 parseflg |= Parse::TEXTMODE;
1330 cur.message(_("Paste"));
1331 cap::replaceSelection(cur);
1333 if (cmd.argument().empty() && !theClipboard().isInternal())
1334 topaste = theClipboard().getAsText();
1336 idocstringstream is(cmd.argument());
1339 topaste = cap::selection(n);
1341 InsetMathGrid grid(buffer_, 1, 1);
1342 if (!topaste.empty())
1343 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1344 || !mathed_parse_normal(grid, topaste, parseflg)) {
1346 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1349 if (grid.nargs() == 1) {
1350 // single cell/part of cell
1352 cur.cell().insert(cur.pos(), grid.cell(0));
1353 cur.pos() += grid.cell(0).size();
1356 cur.recordUndoInset();
1357 col_type const numcols =
1358 min(grid.ncols(), ncols() - col(cur.idx()));
1359 row_type const numrows =
1360 min(grid.nrows(), nrows() - cur.row());
1361 for (row_type r = 0; r < numrows; ++r) {
1362 for (col_type c = 0; c < numcols; ++c) {
1363 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1364 cell(i).insert(0, grid.cell(grid.index(r, c)));
1366 // append the left over horizontal cells to the last column
1367 idx_type i = index(r + cur.row(), ncols() - 1);
1368 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1369 cell(i).append(grid.cell(grid.index(r, c)));
1371 // append the left over vertical cells to the last _cell_
1372 idx_type i = nargs() - 1;
1373 for (row_type r = numrows; r < grid.nrows(); ++r)
1374 for (col_type c = 0; c < grid.ncols(); ++c)
1375 cell(i).append(grid.cell(grid.index(r, c)));
1377 cur.clearSelection(); // bug 393
1378 // FIXME audit setBuffer/updateBuffer calls
1379 cur.inset().setBuffer(*buffer_);
1380 // FIXME audit setBuffer/updateBuffer calls
1381 cur.buffer()->updateBuffer();
1386 case LFUN_LINE_BEGIN_SELECT:
1387 case LFUN_LINE_BEGIN:
1388 case LFUN_WORD_BACKWARD_SELECT:
1389 case LFUN_WORD_BACKWARD:
1390 case LFUN_WORD_LEFT_SELECT:
1391 case LFUN_WORD_LEFT:
1392 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1393 act == LFUN_WORD_LEFT_SELECT ||
1394 act == LFUN_LINE_BEGIN_SELECT);
1395 cur.macroModeClose();
1396 if (cur.pos() != 0) {
1398 } else if (cur.idx() % cur.ncols() != 0) {
1399 cur.idx() -= cur.idx() % cur.ncols();
1401 } else if (cur.idx() != 0) {
1405 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1410 case LFUN_WORD_FORWARD_SELECT:
1411 case LFUN_WORD_FORWARD:
1412 case LFUN_WORD_RIGHT_SELECT:
1413 case LFUN_WORD_RIGHT:
1414 case LFUN_LINE_END_SELECT:
1416 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1417 act == LFUN_WORD_RIGHT_SELECT ||
1418 act == LFUN_LINE_END_SELECT);
1419 cur.macroModeClose();
1421 if (cur.pos() != cur.lastpos()) {
1422 cur.pos() = cur.lastpos();
1423 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1424 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1425 cur.pos() = cur.lastpos();
1426 } else if (cur.idx() != cur.lastidx()) {
1427 cur.idx() = cur.lastidx();
1428 cur.pos() = cur.lastpos();
1430 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1436 InsetMathNest::doDispatch(cur, cmd);
1441 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1442 FuncStatus & status) const
1444 switch (cmd.action()) {
1445 case LFUN_INSET_MODIFY: {
1446 istringstream is(to_utf8(cmd.argument()));
1449 if (s != "tabular") {
1450 // We only now about table actions here.
1453 if (&cur.inset() != this) {
1454 // Table actions requires that the cursor is _inside_ the
1456 status.setEnabled(false);
1457 status.message(from_utf8(N_("Cursor not in table")));
1461 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1462 status.setEnabled(false);
1463 status.message(from_utf8(N_("Only one row")));
1467 (s == "delete-column" || s == "swap-column")) {
1468 status.setEnabled(false);
1469 status.message(from_utf8(N_("Only one column")));
1472 if ((rowinfo_[cur.row()].lines_ == 0 &&
1473 s == "delete-hline-above") ||
1474 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1475 s == "delete-hline-below")) {
1476 status.setEnabled(false);
1477 status.message(from_utf8(N_("No hline to delete")));
1481 if ((colinfo_[cur.col()].lines_ == 0 &&
1482 s == "delete-vline-left") ||
1483 (colinfo_[cur.col() + 1].lines_ == 0 &&
1484 s == "delete-vline-right")) {
1485 status.setEnabled(false);
1486 status.message(from_utf8(N_("No vline to delete")));
1489 if (s == "valign-top" || s == "valign-middle" ||
1490 s == "valign-bottom" || s == "align-left" ||
1491 s == "align-right" || s == "align-center") {
1492 status.setEnabled(true);
1493 char const ha = horizontalAlignment(cur.col());
1494 char const va = verticalAlignment();
1495 status.setOnOff((s == "align-left" && ha == 'l')
1496 || (s == "align-right" && ha == 'r')
1497 || (s == "align-center" && ha == 'c')
1498 || (s == "valign-top" && va == 't')
1499 || (s == "valign-bottom" && va == 'b')
1500 || (s == "valign-middle" && va == 'c'));
1503 if (s == "append-row" || s == "delete-row" ||
1504 s == "copy-row" || s == "swap-row" ||
1505 s == "add-hline-above" || s == "add-hline-below" ||
1506 s == "delete-hline-above" || s == "delete-hline-below" ||
1507 s == "append-column" || s == "delete-column" ||
1508 s == "copy-column" || s == "swap-column" ||
1509 s == "add-vline-left" || s == "add-vline-right" ||
1510 s == "delete-vline-left" || s == "delete-vline-right") {
1511 status.setEnabled(true);
1513 status.setEnabled(false);
1514 status.message(bformat(
1515 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1519 // FIXME: What did this code do?
1520 // Please check whether it is still needed!
1521 // should be more precise
1522 if (v_align_ == '\0') {
1523 status.enable(true);
1526 if (cmd.argument().empty()) {
1527 status.enable(false);
1530 if (!contains("tcb", cmd.argument()[0])) {
1531 status.enable(false);
1534 status.setOnOff(cmd.argument()[0] == v_align_);
1535 status.setEnabled(true);
1540 case LFUN_CELL_SPLIT:
1541 status.setEnabled(true);
1544 case LFUN_CELL_BACKWARD:
1545 case LFUN_CELL_FORWARD:
1546 status.setEnabled(true);
1552 return InsetMathNest::getStatus(cur, cmd, status);