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/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
39 #include "support/lassert.h"
44 using namespace lyx::support;
50 static docstring verboseHLine(int n)
53 for (int i = 0; i < n; ++i)
61 static int extractInt(istream & is)
65 return (num == 0) ? 1 : num;
69 static void resetGrid(InsetMathGrid & grid)
71 while (grid.ncols() > 1)
72 grid.delCol(grid.ncols() - 1);
73 while (grid.nrows() > 1)
74 grid.delRow(grid.nrows() - 1);
75 grid.cell(0).erase(0, grid.cell(0).size());
81 //////////////////////////////////////////////////////////////
84 InsetMathGrid::CellInfo::CellInfo()
90 //////////////////////////////////////////////////////////////
93 InsetMathGrid::RowInfo::RowInfo()
94 : lines_(0), skip_(0), allow_newpage_(true)
99 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
101 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
102 return crskip_.inPixels(mi.base.textwidth,
103 fm.width(char_type('M')));
108 //////////////////////////////////////////////////////////////
111 InsetMathGrid::ColInfo::ColInfo()
112 : align_('c'), lines_(0)
116 //////////////////////////////////////////////////////////////
119 InsetMathGrid::InsetMathGrid(Buffer * buf)
120 : InsetMathNest(buf, 1),
130 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
131 : InsetMathNest(buf, m * n),
141 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
143 : InsetMathNest(buf, m * n),
150 setVerticalAlignment(v);
151 setHorizontalAlignments(h);
155 Inset * InsetMathGrid::clone() const
157 return new InsetMathGrid(*this);
161 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
163 return col + ncols() * row;
167 void InsetMathGrid::setDefaults()
170 lyxerr << "positive number of columns expected" << endl;
172 // lyxerr << "positive number of rows expected" << endl;
173 for (col_type col = 0; col < ncols(); ++col) {
174 colinfo_[col].align_ = defaultColAlign(col);
175 colinfo_[col].skip_ = defaultColSpace(col);
176 colinfo_[col].special_.clear();
181 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)
183 if (str == "\\hline") {
184 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
186 if (getStatus(cur, fr, status)) {
187 if (status.enabled()) {
188 rowinfo_[cur.row()].lines_++;
193 return InsetMathNest::interpretString(cur, str);
197 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
200 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
203 colinfo_[col].lines_++;
204 } else if ((c == 'p' || c == 'm' || c == 'b'||
205 c == '!' || c == '@' || c == '>' || c == '<') &&
206 it + 1 != hh.end() && *(it + 1) == '{') {
207 // @{decl.} and p{width} are standard LaTeX, the
208 // others are extensions by array.sty
209 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
211 // this declares a new column
213 // Only intercolumn stuff is allowed
214 // in the last dummy column
216 colinfo_[col].align_ = 'l';
218 // this is intercolumn stuff
219 if (colinfo_[col].special_.empty())
220 // Overtake possible lines
221 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
225 while (it != hh.end()) {
227 colinfo_[col].special_ += c;
233 if (brace_open > 0 && brace_open == brace_close)
238 colinfo_[col].lines_ = count(
239 colinfo_[col].special_.begin(),
240 colinfo_[col].special_.end(), '|');
241 LYXERR(Debug::MATHED, "special column separator: `"
242 << to_utf8(colinfo_[col].special_) << '\'');
244 colinfo_[col].lines_ = 0;
245 colinfo_[col].special_.clear();
247 } else if (col >= ncols()) {
248 // Only intercolumn stuff is allowed in the last
251 } else if (c == 'c' || c == 'l' || c == 'r') {
252 colinfo_[col].align_ = static_cast<char>(c);
253 if (!colinfo_[col].special_.empty()) {
254 colinfo_[col].special_ += c;
255 colinfo_[col].lines_ = count(
256 colinfo_[col].special_.begin(),
257 colinfo_[col].special_.end(), '|');
258 LYXERR(Debug::MATHED, "special column separator: `"
259 << to_utf8(colinfo_[col].special_) << '\'');
262 colinfo_[col].lines_ = 0;
263 colinfo_[col].special_.clear();
265 lyxerr << "unknown column separator: '" << c << "'" << endl;
270 col_type n = hh.size();
273 for (col_type col = 0; col < n; ++col)
274 colinfo_[col].align_ = hh[col];
279 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
282 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
283 if (*it == 'c' || *it == 'l' || *it == 'r'||
284 *it == 'p' || *it == 'm' || *it == 'b')
286 // let's have at least one column, even if we did not recognize its
294 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
296 colinfo_[col].align_ = h;
297 if (!colinfo_[col].special_.empty()) {
298 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
299 if (c == 'l' || c == 'c' || c == 'r')
302 // FIXME: Change alignment of p, m and b columns, too
306 char InsetMathGrid::horizontalAlignment(col_type col) const
308 return colinfo_[col].align_;
312 docstring InsetMathGrid::horizontalAlignments() const
315 for (col_type col = 0; col < ncols(); ++col) {
316 if (colinfo_[col].special_.empty()) {
317 res += docstring(colinfo_[col].lines_, '|');
318 res += colinfo_[col].align_;
320 res += colinfo_[col].special_;
322 if (colinfo_[ncols()].special_.empty())
323 return res + docstring(colinfo_[ncols()].lines_, '|');
324 return res + colinfo_[ncols()].special_;
328 void InsetMathGrid::setVerticalAlignment(char c)
334 char InsetMathGrid::verticalAlignment() const
340 InsetMathGrid::col_type InsetMathGrid::ncols() const
342 return colinfo_.size() - 1;
346 InsetMathGrid::row_type InsetMathGrid::nrows() const
348 return rowinfo_.size() - 1;
352 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
354 return idx % ncols();
358 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
360 return idx / ncols();
364 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
366 rowinfo_[row].crskip_ = crskip;
370 Length InsetMathGrid::vcrskip(row_type row) const
372 return rowinfo_[row].crskip_;
376 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
378 // let the cells adjust themselves
379 InsetMathNest::metrics(mi);
381 BufferView & bv = *mi.base.bv;
383 // compute absolute sizes of vertical structure
384 for (row_type row = 0; row < nrows(); ++row) {
387 for (col_type col = 0; col < ncols(); ++col) {
388 Dimension const & dimc = cell(index(row, col)).dimension(bv);
389 asc = max(asc, dimc.asc);
390 desc = max(desc, dimc.des);
392 rowinfo_[row].ascent_ = asc;
393 rowinfo_[row].descent_ = desc;
395 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
396 rowinfo_[nrows()].ascent_ = 0;
397 rowinfo_[nrows()].descent_ = 0;
399 // compute vertical offsets
400 rowinfo_[0].offset_ = 0;
401 for (row_type row = 1; row <= nrows(); ++row) {
402 rowinfo_[row].offset_ =
403 rowinfo_[row - 1].offset_ +
404 rowinfo_[row - 1].descent_ +
405 rowinfo_[row - 1].skipPixels(mi) +
407 rowinfo_[row].lines_ * hlinesep() +
408 rowinfo_[row].ascent_;
411 // adjust vertical offset
418 h = rowinfo_[nrows() - 1].offset_;
421 h = rowinfo_[nrows() - 1].offset_ / 2;
423 for (row_type row = 0; row <= nrows(); ++row)
424 rowinfo_[row].offset_ -= h;
427 // compute absolute sizes of horizontal structure
428 for (col_type col = 0; col < ncols(); ++col) {
430 for (row_type row = 0; row < nrows(); ++row)
431 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
432 colinfo_[col].width_ = wid;
434 colinfo_[ncols()].width_ = 0;
436 // compute horizontal offsets
437 colinfo_[0].offset_ = border();
438 for (col_type col = 1; col <= ncols(); ++col) {
439 colinfo_[col].offset_ =
440 colinfo_[col - 1].offset_ +
441 colinfo_[col - 1].width_ +
442 colinfo_[col - 1].skip_ +
444 colinfo_[col].lines_ * vlinesep();
448 dim.wid = colinfo_[ncols() - 1].offset_
449 + colinfo_[ncols() - 1].width_
450 + vlinesep() * colinfo_[ncols()].lines_
453 dim.asc = - rowinfo_[0].offset_
454 + rowinfo_[0].ascent_
455 + hlinesep() * rowinfo_[0].lines_
458 dim.des = rowinfo_[nrows() - 1].offset_
459 + rowinfo_[nrows() - 1].descent_
460 + hlinesep() * rowinfo_[nrows()].lines_
465 // Increase ws_[i] for 'R' columns (except the first one)
466 for (int i = 1; i < nc_; ++i)
467 if (align_[i] == 'R')
468 ws_[i] += 10 * df_width;
469 // Increase ws_[i] for 'C' column
470 if (align_[0] == 'C')
471 if (ws_[0] < 7 * workwidth / 8)
472 ws_[0] = 7 * workwidth / 8;
476 for (cxrow = row_.begin(); cxrow; ++cxrow) {
479 for (int i = 0; i < nc_; ++i) {
481 if (cxrow->getTab(i) <= 0) {
482 cxrow->setTab(i, df_width);
490 lf = (ws_[i] - cxrow->getTab(i))/2;
494 lf = ws_[i] - cxrow->getTab(i);
497 if (cxrow == row_.begin())
499 else if (cxrow.is_last())
500 lf = ws_[i] - cxrow->getTab(i);
502 lf = (ws_[i] - cxrow->getTab(i))/2;
505 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
506 cxrow->setTab(i, lf + rg);
507 rg = ws_[i] - ww + colsep();
508 if (cxrow == row_.begin())
509 width += ws_[i] + colsep();
511 cxrow->setBaseline(cxrow->getBaseline() - ascent);
514 metricsMarkers2(dim);
515 // Cache the inset dimension.
516 setDimCache(mi, dim);
520 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
522 drawWithMargin(pi, x, y, 1, 1);
526 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
527 int lmargin, int rmargin) const
529 Dimension const dim = dimension(*pi.base.bv);
530 BufferView const & bv = *pi.base.bv;
532 for (idx_type idx = 0; idx < nargs(); ++idx)
533 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
534 y + cellYOffset(idx));
536 for (row_type row = 0; row <= nrows(); ++row)
537 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
538 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
539 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
540 pi.pain.line(x + lmargin + 1, yy,
541 x + dim.width() - rmargin - 1, yy,
545 for (col_type col = 0; col <= ncols(); ++col)
546 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
547 int xx = x + lmargin + colinfo_[col].offset_
548 - i * vlinesep() - vlinesep()/2 - colsep()/2;
549 pi.pain.line(xx, y - dim.ascent() + 1,
550 xx, y + dim.descent() - 1,
553 drawMarkers2(pi, x, y);
557 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
559 // let the cells adjust themselves
560 //InsetMathNest::metrics(mi);
561 for (idx_type i = 0; i < nargs(); ++i)
562 cell(i).metricsT(mi, dim);
564 // compute absolute sizes of vertical structure
565 for (row_type row = 0; row < nrows(); ++row) {
568 for (col_type col = 0; col < ncols(); ++col) {
569 //MathData const & c = cell(index(row, col));
572 asc = max(asc, dimc.ascent());
573 desc = max(desc, dimc.descent());
575 rowinfo_[row].ascent_ = asc;
576 rowinfo_[row].descent_ = desc;
578 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
579 rowinfo_[nrows()].ascent_ = 0;
580 rowinfo_[nrows()].descent_ = 0;
582 // compute vertical offsets
583 rowinfo_[0].offset_ = 0;
584 for (row_type row = 1; row <= nrows(); ++row) {
585 rowinfo_[row].offset_ =
586 rowinfo_[row - 1].offset_ +
587 rowinfo_[row - 1].descent_ +
588 //rowinfo_[row - 1].skipPixels(mi) +
590 //rowinfo_[row].lines_ * hlinesep() +
591 rowinfo_[row].ascent_;
594 // adjust vertical offset
601 h = rowinfo_[nrows() - 1].offset_;
604 h = rowinfo_[nrows() - 1].offset_ / 2;
606 for (row_type row = 0; row <= nrows(); ++row)
607 rowinfo_[row].offset_ -= h;
610 // compute absolute sizes of horizontal structure
611 for (col_type col = 0; col < ncols(); ++col) {
613 for (row_type row = 0; row < nrows(); ++row) {
615 //wid = max(wid, cell(index(row, col)).width());
617 colinfo_[col].width_ = wid;
619 colinfo_[ncols()].width_ = 0;
621 // compute horizontal offsets
622 colinfo_[0].offset_ = border();
623 for (col_type col = 1; col <= ncols(); ++col) {
624 colinfo_[col].offset_ =
625 colinfo_[col - 1].offset_ +
626 colinfo_[col - 1].width_ +
627 colinfo_[col - 1].skip_ +
629 //colinfo_[col].lines_ * vlinesep();
633 dim.wid = colinfo_[ncols() - 1].offset_
634 + colinfo_[ncols() - 1].width_
635 //+ vlinesep() * colinfo_[ncols()].lines_
638 dim.asc = -rowinfo_[0].offset_
639 + rowinfo_[0].ascent_
640 //+ hlinesep() * rowinfo_[0].lines_
643 dim.des = rowinfo_[nrows() - 1].offset_
644 + rowinfo_[nrows() - 1].descent_
645 //+ hlinesep() * rowinfo_[nrows()].lines_
650 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
652 // for (idx_type idx = 0; idx < nargs(); ++idx)
653 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
657 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
660 for (idx_type idx = 0; idx < nargs(); ++idx)
661 cell(idx).updateBuffer(it, utype);
665 docstring InsetMathGrid::eolString(row_type row, bool fragile,
666 bool /*latex*/, bool last_eoln) const
670 if (!rowinfo_[row].crskip_.zero())
671 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
672 else if(!rowinfo_[row].allow_newpage_)
675 // make sure an upcoming '[' does not break anything
676 if (row + 1 < nrows()) {
677 MathData const & c = cell(index(row + 1, 0));
678 if (!c.empty() && c.front()->getChar() == '[')
683 // only add \\ if necessary
684 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
687 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
691 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
693 if (col + 1 == lastcol)
695 return from_ascii(" & ");
699 void InsetMathGrid::addRow(row_type row)
701 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
703 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
705 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
709 void InsetMathGrid::delRow(row_type row)
714 cells_type::iterator it = cells_.begin() + row * ncols();
715 cells_.erase(it, it + ncols());
717 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
718 cellinfo_.erase(jt, jt + ncols());
720 rowinfo_.erase(rowinfo_.begin() + row);
724 void InsetMathGrid::copyRow(row_type row)
727 for (col_type col = 0; col < ncols(); ++col)
728 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
732 void InsetMathGrid::swapRow(row_type row)
736 if (row + 1 == nrows())
738 for (col_type col = 0; col < ncols(); ++col)
739 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
743 void InsetMathGrid::addCol(col_type newcol)
745 const col_type nc = ncols();
746 const row_type nr = nrows();
747 cells_type new_cells((nc + 1) * nr);
748 vector<CellInfo> new_cellinfo((nc + 1) * nr);
750 for (row_type row = 0; row < nr; ++row)
751 for (col_type col = 0; col < nc; ++col) {
752 new_cells[row * (nc + 1) + col + (col >= newcol)]
753 = cells_[row * nc + col];
754 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
755 = cellinfo_[row * nc + col];
757 swap(cells_, new_cells);
758 swap(cellinfo_, new_cellinfo);
761 inf.skip_ = defaultColSpace(newcol);
762 inf.align_ = defaultColAlign(newcol);
763 colinfo_.insert(colinfo_.begin() + newcol, inf);
767 void InsetMathGrid::delCol(col_type col)
773 vector<CellInfo> tmpcellinfo;
774 for (col_type i = 0; i < nargs(); ++i)
775 if (i % ncols() != col) {
776 tmpcells.push_back(cells_[i]);
777 tmpcellinfo.push_back(cellinfo_[i]);
779 swap(cells_, tmpcells);
780 swap(cellinfo_, tmpcellinfo);
782 colinfo_.erase(colinfo_.begin() + col);
786 void InsetMathGrid::copyCol(col_type col)
789 for (row_type row = 0; row < nrows(); ++row)
790 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
794 void InsetMathGrid::swapCol(col_type col)
798 if (col + 1 == ncols())
800 for (row_type row = 0; row < nrows(); ++row)
801 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
805 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
807 col_type c = col(idx);
808 int x = colinfo_[c].offset_;
809 char align = displayColAlign(c, row(idx));
810 Dimension const & celldim = cell(idx).dimension(bv);
811 if (align == 'r' || align == 'R')
812 x += colinfo_[c].width_ - celldim.wid;
813 if (align == 'c' || align == 'C')
814 x += (colinfo_[c].width_ - celldim.wid) / 2;
819 int InsetMathGrid::cellYOffset(idx_type idx) const
821 return rowinfo_[row(idx)].offset_;
825 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
830 cur.idx() -= ncols();
832 if (cur.row() + 1 >= nrows())
834 cur.idx() += ncols();
836 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
841 bool InsetMathGrid::idxBackward(Cursor & cur) const
843 // leave matrix if at the front edge
847 cur.pos() = cur.lastpos();
852 bool InsetMathGrid::idxForward(Cursor & cur) const
854 // leave matrix if at the back edge
855 if (cur.col() + 1 == ncols())
863 bool InsetMathGrid::idxFirst(Cursor & cur) const
870 cur.idx() = (nrows() - 1) * ncols();
873 cur.idx() = ((nrows() - 1) / 2) * ncols();
880 bool InsetMathGrid::idxLast(Cursor & cur) const
884 cur.idx() = ncols() - 1;
887 cur.idx() = nargs() - 1;
890 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
892 cur.pos() = cur.lastpos();
897 bool InsetMathGrid::idxDelete(idx_type & idx)
899 // nothing to do if we have just one row
903 // nothing to do if we are in the middle of the last row of the inset
904 if (idx + ncols() > nargs())
907 // try to delete entire sequence of ncols() empty cells if possible
908 for (idx_type i = idx; i < idx + ncols(); ++i)
909 if (!cell(i).empty())
912 // move cells if necessary
913 for (idx_type i = index(row(idx), 0); i < idx; ++i)
914 swap(cell(i), cell(i + ncols()));
921 // undo effect of Ctrl-Tab (i.e. pull next cell)
922 //if (idx + 1 != nargs())
923 // cell(idx).swap(cell(idx + 1));
925 // we handled the event..
930 // reimplement old behaviour when pressing Delete in the last position
932 void InsetMathGrid::idxGlue(idx_type idx)
934 col_type c = col(idx);
935 if (c + 1 == ncols()) {
936 if (row(idx) + 1 != nrows()) {
937 for (col_type cc = 0; cc < ncols(); ++cc)
938 cell(idx).append(cell(idx + cc + 1));
939 delRow(row(idx) + 1);
942 cell(idx).append(cell(idx + 1));
943 for (col_type cc = c + 2; cc < ncols(); ++cc)
944 cell(idx - c + cc - 1) = cell(idx - c + cc);
945 cell(idx - c + ncols() - 1).clear();
950 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
952 return rowinfo_[row];
956 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
958 return rowinfo_[row];
962 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
964 row_type const ri = row(idx);
965 row_type const r1 = min(row(from), row(to));
966 row_type const r2 = max(row(from), row(to));
967 col_type const ci = col(idx);
968 col_type const c1 = min(col(from), col(to));
969 col_type const c2 = max(col(from), col(to));
970 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
975 void InsetMathGrid::normalize(NormalStream & os) const
978 for (row_type row = 0; row < nrows(); ++row) {
980 for (col_type col = 0; col < ncols(); ++col)
981 os << "[cell " << cell(index(row, col)) << ']';
988 void InsetMathGrid::mathmlize(MathStream & os) const
990 bool const havetable = nrows() > 1 || ncols() > 1;
992 os << MTag("mtable");
993 char const * const celltag = havetable ? "mtd" : "mrow";
994 for (row_type row = 0; row < nrows(); ++row) {
997 for (col_type col = 0; col < ncols(); ++col) {
999 os << cell(index(row, col));
1000 os << ETag(celltag);
1006 os << ETag("mtable");
1011 // We need to do something about alignment here.
1012 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
1014 bool const havetable = nrows() > 1 || ncols() > 1;
1016 os << cell(index(0, 0));
1019 os << MTag("table", attrib);
1020 for (row_type row = 0; row < nrows(); ++row) {
1022 for (col_type col = 0; col < ncols(); ++col) {
1024 os << cell(index(row, col));
1029 os << ETag("table");
1033 void InsetMathGrid::htmlize(HtmlStream & os) const
1035 htmlize(os, "class='mathtable'");
1039 void InsetMathGrid::write(WriteStream & os) const
1041 write(os, 0, 0, nrows(), ncols());
1044 void InsetMathGrid::write(WriteStream & os,
1045 row_type beg_row, col_type beg_col,
1046 row_type end_row, col_type end_col) const
1048 MathEnsurer ensurer(os, false);
1050 for (row_type row = beg_row; row < end_row; ++row) {
1051 os << verboseHLine(rowinfo_[row].lines_);
1052 // don't write & and empty cells at end of line,
1053 // unless there are vertical lines
1054 col_type lastcol = 0;
1055 bool emptyline = true;
1056 bool last_eoln = true;
1057 for (col_type col = beg_col; col < end_col; ++col) {
1058 bool const empty_cell = cell(index(row, col)).empty();
1061 if (!empty_cell || colinfo_[col + 1].lines_) {
1066 for (col_type col = beg_col; col < lastcol; ++col) {
1067 os << cell(index(row, col));
1068 if (os.pendingBrace())
1069 ModeSpecifier specifier(os, TEXT_MODE);
1070 os << eocString(col, lastcol);
1072 eol = eolString(row, os.fragile(), os.latex(), last_eoln);
1074 // append newline only if line wasn't completely empty
1075 // and the formula is not written on a single line
1076 bool const empty = emptyline && eol.empty();
1077 if (!empty && nrows() > 1)
1080 // @TODO use end_row instead of nrows() ?
1081 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1093 int InsetMathGrid::colsep() const
1099 int InsetMathGrid::rowsep() const
1105 int InsetMathGrid::hlinesep() const
1111 int InsetMathGrid::vlinesep() const
1117 int InsetMathGrid::border() const
1123 void InsetMathGrid::splitCell(Cursor & cur)
1125 if (cur.idx() == cur.lastidx())
1127 MathData ar = cur.cell();
1128 ar.erase(0, cur.pos());
1129 cur.cell().erase(cur.pos(), cur.lastpos());
1132 cur.cell().insert(0, ar);
1136 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1138 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1140 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1142 FuncCode const act = cmd.action();
1145 // insert file functions
1146 case LFUN_LINE_DELETE_FORWARD:
1147 cur.recordUndoInset();
1148 //autocorrect_ = false;
1156 if (cur.idx() > cur.lastidx())
1157 cur.idx() = cur.lastidx();
1158 if (cur.pos() > cur.lastpos())
1159 cur.pos() = cur.lastpos();
1162 case LFUN_CELL_SPLIT:
1167 case LFUN_CELL_BACKWARD:
1169 cur.setSelection(false);
1170 if (!idxPrev(cur)) {
1171 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1176 case LFUN_CELL_FORWARD:
1177 // Can't handle selection by additional 'shift' as this is
1178 // hard bound to LFUN_CELL_BACKWARD
1179 cur.setSelection(false);
1180 if (!idxNext(cur)) {
1181 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1186 case LFUN_NEWLINE_INSERT: {
1187 cur.recordUndoInset();
1188 row_type const r = cur.row();
1192 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1193 swap(cell(index(r, c)), cell(index(r + 1, c)));
1197 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1200 cur.pos() = cur.lastpos();
1201 cur.forceBufferUpdate();
1202 //mathcursor->normalize();
1203 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1207 case LFUN_INSET_MODIFY: {
1208 cur.recordUndoInset();
1209 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1210 istringstream is(to_utf8(cmd.argument()));
1213 if (s != "tabular") {
1214 InsetMathNest::doDispatch(cur, cmd);
1218 if (s == "valign-top")
1219 setVerticalAlignment('t');
1220 else if (s == "valign-middle")
1221 setVerticalAlignment('c');
1222 else if (s == "valign-bottom")
1223 setVerticalAlignment('b');
1224 else if (s == "align-left")
1225 setHorizontalAlignment('l', cur.col());
1226 else if (s == "align-right")
1227 setHorizontalAlignment('r', cur.col());
1228 else if (s == "align-center")
1229 setHorizontalAlignment('c', cur.col());
1230 else if (s == "append-row")
1231 for (int i = 0, n = extractInt(is); i < n; ++i)
1233 else if (s == "delete-row") {
1234 cur.clearSelection(); // bug 4323
1235 for (int i = 0, n = extractInt(is); i < n; ++i) {
1237 if (cur.idx() >= nargs())
1238 cur.idx() -= ncols();
1240 cur.pos() = 0; // trick, see below
1242 else if (s == "copy-row") {
1243 // Here (as later) we save the cursor col/row
1244 // in order to restore it after operation.
1245 row_type const r = cur.row();
1246 col_type const c = cur.col();
1247 for (int i = 0, n = extractInt(is); i < n; ++i)
1249 cur.idx() = index(r, c);
1251 else if (s == "swap-row") {
1253 // Trick to suppress same-idx-means-different-cell
1257 else if (s == "add-hline-above")
1258 rowinfo_[cur.row()].lines_++;
1259 else if (s == "add-hline-below")
1260 rowinfo_[cur.row()+1].lines_++;
1261 else if (s == "delete-hline-above")
1262 rowinfo_[cur.row()].lines_--;
1263 else if (s == "delete-hline-below")
1264 rowinfo_[cur.row()+1].lines_--;
1265 else if (s == "append-column") {
1266 row_type const r = cur.row();
1267 col_type const c = cur.col();
1268 for (int i = 0, n = extractInt(is); i < n; ++i)
1269 addCol(cur.col() + 1);
1270 cur.idx() = index(r, c);
1272 else if (s == "delete-column") {
1273 cur.clearSelection(); // bug 4323
1274 row_type const r = cur.row();
1275 col_type const c = cur.col();
1276 for (int i = 0, n = extractInt(is); i < n; ++i)
1277 delCol(col(cur.idx()));
1278 cur.idx() = index(r, min(c, cur.ncols() - 1));
1279 cur.pos() = 0; // trick, see above
1281 else if (s == "copy-column") {
1282 row_type const r = cur.row();
1283 col_type const c = cur.col();
1285 cur.idx() = index(r, c);
1287 else if (s == "swap-column") {
1289 cur.pos() = 0; // trick, see above
1291 else if (s == "add-vline-left") {
1292 colinfo_[cur.col()].lines_++;
1293 if (!colinfo_[cur.col()].special_.empty())
1294 colinfo_[cur.col()].special_ += '|';
1296 else if (s == "add-vline-right") {
1297 colinfo_[cur.col()+1].lines_++;
1298 if (!colinfo_[cur.col()+1].special_.empty())
1299 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1301 else if (s == "delete-vline-left") {
1302 colinfo_[cur.col()].lines_--;
1303 docstring & special = colinfo_[cur.col()].special_;
1304 if (!special.empty()) {
1305 docstring::size_type i = special.rfind('|');
1306 LASSERT(i != docstring::npos, break);
1307 special.erase(i, 1);
1310 else if (s == "delete-vline-right") {
1311 colinfo_[cur.col()+1].lines_--;
1312 docstring & special = colinfo_[cur.col()+1].special_;
1313 if (!special.empty()) {
1314 docstring::size_type i = special.find('|');
1315 LASSERT(i != docstring::npos, break);
1316 special.erase(i, 1);
1323 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1324 //lyxerr << "returning FINISHED_LEFT" << endl;
1328 case LFUN_CLIPBOARD_PASTE:
1329 parseflg |= Parse::VERBATIM;
1332 if (cur.currentMode() <= TEXT_MODE)
1333 parseflg |= Parse::TEXTMODE;
1334 cur.message(_("Paste"));
1335 cap::replaceSelection(cur);
1337 if (cmd.argument().empty() && !theClipboard().isInternal())
1338 topaste = theClipboard().getAsText(Clipboard::PlainTextType);
1340 idocstringstream is(cmd.argument());
1343 topaste = cap::selection(n, buffer().params().documentClassPtr());
1345 InsetMathGrid grid(buffer_, 1, 1);
1346 if (!topaste.empty())
1347 if ((topaste.size() == 1 && isAscii(topaste))
1348 || !mathed_parse_normal(grid, topaste, parseflg)) {
1350 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1353 bool hline_enabled = false;
1354 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
1356 if (getStatus(cur, fr, status))
1357 hline_enabled = status.enabled();
1358 if (grid.nargs() == 1) {
1359 // single cell/part of cell
1360 cur.recordUndoInset();
1361 cur.cell().insert(cur.pos(), grid.cell(0));
1362 cur.pos() += grid.cell(0).size();
1364 rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_;
1366 for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) {
1367 cur.cell().insert(0,
1368 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1374 cur.recordUndoInset();
1375 col_type const numcols =
1376 min(grid.ncols(), ncols() - col(cur.idx()));
1377 row_type const numrows =
1378 min(grid.nrows(), nrows() - cur.row());
1379 for (row_type r = 0; r < numrows; ++r) {
1380 for (col_type c = 0; c < numcols; ++c) {
1381 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1382 cell(i).insert(0, grid.cell(grid.index(r, c)));
1385 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1387 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1388 idx_type i = index(r + cur.row(), 0);
1390 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1393 // append the left over horizontal cells to the last column
1394 idx_type i = index(r + cur.row(), ncols() - 1);
1395 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1396 cell(i).append(grid.cell(grid.index(r, c)));
1398 // append the left over vertical cells to the last _cell_
1399 idx_type i = nargs() - 1;
1400 for (row_type r = numrows; r < grid.nrows(); ++r) {
1401 for (col_type c = 0; c < grid.ncols(); ++c)
1402 cell(i).append(grid.cell(grid.index(r, c)));
1404 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
1406 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
1408 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
1413 cur.clearSelection(); // bug 393
1414 // FIXME audit setBuffer calls
1415 cur.inset().setBuffer(*buffer_);
1416 cur.forceBufferUpdate();
1421 case LFUN_LINE_BEGIN_SELECT:
1422 case LFUN_LINE_BEGIN:
1423 case LFUN_WORD_BACKWARD_SELECT:
1424 case LFUN_WORD_BACKWARD:
1425 case LFUN_WORD_LEFT_SELECT:
1426 case LFUN_WORD_LEFT:
1427 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1428 act == LFUN_WORD_LEFT_SELECT ||
1429 act == LFUN_LINE_BEGIN_SELECT);
1430 cur.macroModeClose();
1431 if (cur.pos() != 0) {
1433 } else if (cur.idx() % cur.ncols() != 0) {
1434 cur.idx() -= cur.idx() % cur.ncols();
1436 } else if (cur.idx() != 0) {
1440 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1445 case LFUN_WORD_FORWARD_SELECT:
1446 case LFUN_WORD_FORWARD:
1447 case LFUN_WORD_RIGHT_SELECT:
1448 case LFUN_WORD_RIGHT:
1449 case LFUN_LINE_END_SELECT:
1451 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1452 act == LFUN_WORD_RIGHT_SELECT ||
1453 act == LFUN_LINE_END_SELECT);
1454 cur.macroModeClose();
1456 if (cur.pos() != cur.lastpos()) {
1457 cur.pos() = cur.lastpos();
1458 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1459 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1460 cur.pos() = cur.lastpos();
1461 } else if (cur.idx() != cur.lastidx()) {
1462 cur.idx() = cur.lastidx();
1463 cur.pos() = cur.lastpos();
1465 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1471 InsetMathNest::doDispatch(cur, cmd);
1476 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1477 FuncStatus & status) const
1479 switch (cmd.action()) {
1480 case LFUN_INSET_MODIFY: {
1481 istringstream is(to_utf8(cmd.argument()));
1484 if (s != "tabular") {
1485 // We only now about table actions here.
1488 if (&cur.inset() != this) {
1489 // Table actions requires that the cursor is _inside_ the
1491 status.setEnabled(false);
1492 status.message(from_utf8(N_("Cursor not in table")));
1496 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1497 status.setEnabled(false);
1498 status.message(from_utf8(N_("Only one row")));
1502 (s == "delete-column" || s == "swap-column")) {
1503 status.setEnabled(false);
1504 status.message(from_utf8(N_("Only one column")));
1507 if ((rowinfo_[cur.row()].lines_ == 0 &&
1508 s == "delete-hline-above") ||
1509 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1510 s == "delete-hline-below")) {
1511 status.setEnabled(false);
1512 status.message(from_utf8(N_("No hline to delete")));
1516 if ((colinfo_[cur.col()].lines_ == 0 &&
1517 s == "delete-vline-left") ||
1518 (colinfo_[cur.col() + 1].lines_ == 0 &&
1519 s == "delete-vline-right")) {
1520 status.setEnabled(false);
1521 status.message(from_utf8(N_("No vline to delete")));
1524 if (s == "valign-top" || s == "valign-middle" ||
1525 s == "valign-bottom" || s == "align-left" ||
1526 s == "align-right" || s == "align-center") {
1527 status.setEnabled(true);
1528 char const ha = horizontalAlignment(cur.col());
1529 char const va = verticalAlignment();
1530 status.setOnOff((s == "align-left" && ha == 'l')
1531 || (s == "align-right" && ha == 'r')
1532 || (s == "align-center" && ha == 'c')
1533 || (s == "valign-top" && va == 't')
1534 || (s == "valign-bottom" && va == 'b')
1535 || (s == "valign-middle" && va == 'c'));
1538 if (s == "append-row" || s == "delete-row" ||
1539 s == "copy-row" || s == "swap-row" ||
1540 s == "add-hline-above" || s == "add-hline-below" ||
1541 s == "delete-hline-above" || s == "delete-hline-below" ||
1542 s == "append-column" || s == "delete-column" ||
1543 s == "copy-column" || s == "swap-column" ||
1544 s == "add-vline-left" || s == "add-vline-right" ||
1545 s == "delete-vline-left" || s == "delete-vline-right") {
1546 status.setEnabled(true);
1548 status.setEnabled(false);
1549 status.message(bformat(
1550 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1554 // FIXME: What did this code do?
1555 // Please check whether it is still needed!
1556 // should be more precise
1557 if (v_align_ == '\0') {
1558 status.enable(true);
1561 if (cmd.argument().empty()) {
1562 status.enable(false);
1565 if (!contains("tcb", cmd.argument()[0])) {
1566 status.enable(false);
1569 status.setOnOff(cmd.argument()[0] == v_align_);
1570 status.setEnabled(true);
1575 case LFUN_CELL_SPLIT:
1576 status.setEnabled(cur.idx() != cur.lastidx());
1579 case LFUN_CELL_BACKWARD:
1580 case LFUN_CELL_FORWARD:
1581 status.setEnabled(true);
1587 return InsetMathNest::getStatus(cur, cmd, status);