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.
13 #include "InsetMathGrid.h"
16 #include "MathParser.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "FuncStatus.h"
24 #include "support/debug.h"
25 #include "FuncRequest.h"
26 #include "support/gettext.h"
28 #include "frontends/Clipboard.h"
29 #include "frontends/Painter.h"
31 #include "support/lstrings.h"
32 #include "support/docstream.h"
37 using namespace lyx::support;
43 docstring verboseHLine(int n)
46 for (int i = 0; i < n; ++i)
54 int extractInt(istream & is)
58 return (num == 0) ? 1 : num;
64 //////////////////////////////////////////////////////////////
67 InsetMathGrid::CellInfo::CellInfo()
74 //////////////////////////////////////////////////////////////
77 InsetMathGrid::RowInfo::RowInfo()
78 : lines_(0), skip_(0), allow_newpage_(true)
83 int InsetMathGrid::RowInfo::skipPixels() const
85 return crskip_.inBP();
90 //////////////////////////////////////////////////////////////
93 InsetMathGrid::ColInfo::ColInfo()
94 : align_('c'), lines_(0)
98 //////////////////////////////////////////////////////////////
101 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
102 : InsetMathNest(guessColumns(h)),
104 colinfo_(guessColumns(h) + 1),
105 cellinfo_(1 * guessColumns(h))
110 //lyxerr << "created grid with " << ncols() << " columns" << endl;
114 InsetMathGrid::InsetMathGrid()
125 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
126 : InsetMathNest(m * n),
136 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
137 : InsetMathNest(m * n),
149 Inset * InsetMathGrid::clone() const
151 return new InsetMathGrid(*this);
155 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
157 return col + ncols() * row;
161 void InsetMathGrid::setDefaults()
164 lyxerr << "positive number of columns expected" << endl;
166 // lyxerr << "positive number of rows expected" << endl;
167 for (col_type col = 0; col < ncols(); ++col) {
168 colinfo_[col].align_ = defaultColAlign(col);
169 colinfo_[col].skip_ = defaultColSpace(col);
170 colinfo_[col].special_.clear();
175 void InsetMathGrid::halign(docstring const & hh)
178 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
181 colinfo_[col].lines_++;
182 } else if ((c == 'p' || c == 'm' || c == 'b'||
183 c == '!' || c == '@' || c == '>' || c == '<') &&
184 it + 1 != hh.end() && *(it + 1) == '{') {
185 // @{decl.} and p{width} are standard LaTeX, the
186 // others are extensions by array.sty
187 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
189 // this declares a new column
191 // Only intercolumn stuff is allowed
192 // in the last dummy column
194 colinfo_[col].align_ = 'l';
196 // this is intercolumn stuff
197 if (colinfo_[col].special_.empty())
198 // Overtake possible lines
199 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
203 while (it != hh.end()) {
205 colinfo_[col].special_ += c;
211 if (brace_open > 0 && brace_open == brace_close)
216 colinfo_[col].lines_ = count(
217 colinfo_[col].special_.begin(),
218 colinfo_[col].special_.end(), '|');
219 LYXERR(Debug::MATHED, "special column separator: `"
220 << to_utf8(colinfo_[col].special_) << '\'');
222 colinfo_[col].lines_ = 0;
223 colinfo_[col].special_.clear();
225 } else if (col >= ncols()) {
226 // Only intercolumn stuff is allowed in the last
229 } else if (c == 'c' || c == 'l' || c == 'r') {
230 colinfo_[col].align_ = static_cast<char>(c);
231 if (!colinfo_[col].special_.empty()) {
232 colinfo_[col].special_ += c;
233 colinfo_[col].lines_ = count(
234 colinfo_[col].special_.begin(),
235 colinfo_[col].special_.end(), '|');
236 LYXERR(Debug::MATHED, "special column separator: `"
237 << to_utf8(colinfo_[col].special_) << '\'');
240 colinfo_[col].lines_ = 0;
241 colinfo_[col].special_.clear();
243 lyxerr << "unknown column separator: '" << c << "'" << endl;
248 col_type n = hh.size();
251 for (col_type col = 0; col < n; ++col)
252 colinfo_[col].align_ = hh[col];
257 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
260 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
261 if (*it == 'c' || *it == 'l' || *it == 'r'||
262 *it == 'p' || *it == 'm' || *it == 'b')
264 // let's have at least one column, even if we did not recognize its
272 void InsetMathGrid::halign(char h, col_type col)
274 colinfo_[col].align_ = h;
275 if (!colinfo_[col].special_.empty()) {
276 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
277 if (c == 'l' || c == 'c' || c == 'r')
280 // FIXME: Change alignment of p, m and b columns, too
284 char InsetMathGrid::halign(col_type col) const
286 return colinfo_[col].align_;
290 docstring InsetMathGrid::halign() const
293 for (col_type col = 0; col < ncols(); ++col) {
294 if (colinfo_[col].special_.empty()) {
295 res += docstring(colinfo_[col].lines_, '|');
296 res += colinfo_[col].align_;
298 res += colinfo_[col].special_;
300 if (colinfo_[ncols()].special_.empty())
301 return res + docstring(colinfo_[ncols()].lines_, '|');
302 return res + colinfo_[ncols()].special_;
306 void InsetMathGrid::valign(char c)
312 char InsetMathGrid::valign() const
318 InsetMathGrid::col_type InsetMathGrid::ncols() const
320 return colinfo_.size() - 1;
324 InsetMathGrid::row_type InsetMathGrid::nrows() const
326 return rowinfo_.size() - 1;
330 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
332 return idx % ncols();
336 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
338 return idx / ncols();
342 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
344 rowinfo_[row].crskip_ = crskip;
348 Length InsetMathGrid::vcrskip(row_type row) const
350 return rowinfo_[row].crskip_;
354 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
356 // let the cells adjust themselves
357 InsetMathNest::metrics(mi);
359 BufferView & bv = *mi.base.bv;
361 // compute absolute sizes of vertical structure
362 for (row_type row = 0; row < nrows(); ++row) {
365 for (col_type col = 0; col < ncols(); ++col) {
366 Dimension const & dimc = cell(index(row, col)).dimension(bv);
367 asc = max(asc, dimc.asc);
368 desc = max(desc, dimc.des);
370 rowinfo_[row].ascent_ = asc;
371 rowinfo_[row].descent_ = desc;
373 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
374 rowinfo_[nrows()].ascent_ = 0;
375 rowinfo_[nrows()].descent_ = 0;
377 // compute vertical offsets
378 rowinfo_[0].offset_ = 0;
379 for (row_type row = 1; row <= nrows(); ++row) {
380 rowinfo_[row].offset_ =
381 rowinfo_[row - 1].offset_ +
382 rowinfo_[row - 1].descent_ +
383 rowinfo_[row - 1].skipPixels() +
385 rowinfo_[row].lines_ * hlinesep() +
386 rowinfo_[row].ascent_;
389 // adjust vertical offset
396 h = rowinfo_[nrows() - 1].offset_;
399 h = rowinfo_[nrows() - 1].offset_ / 2;
401 for (row_type row = 0; row <= nrows(); ++row)
402 rowinfo_[row].offset_ -= h;
405 // compute absolute sizes of horizontal structure
406 for (col_type col = 0; col < ncols(); ++col) {
408 for (row_type row = 0; row < nrows(); ++row)
409 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
410 colinfo_[col].width_ = wid;
412 colinfo_[ncols()].width_ = 0;
414 // compute horizontal offsets
415 colinfo_[0].offset_ = border();
416 for (col_type col = 1; col <= ncols(); ++col) {
417 colinfo_[col].offset_ =
418 colinfo_[col - 1].offset_ +
419 colinfo_[col - 1].width_ +
420 colinfo_[col - 1].skip_ +
422 colinfo_[col].lines_ * vlinesep();
426 dim.wid = colinfo_[ncols() - 1].offset_
427 + colinfo_[ncols() - 1].width_
428 + vlinesep() * colinfo_[ncols()].lines_
431 dim.asc = - rowinfo_[0].offset_
432 + rowinfo_[0].ascent_
433 + hlinesep() * rowinfo_[0].lines_
436 dim.des = rowinfo_[nrows() - 1].offset_
437 + rowinfo_[nrows() - 1].descent_
438 + hlinesep() * rowinfo_[nrows()].lines_
443 // Increase ws_[i] for 'R' columns (except the first one)
444 for (int i = 1; i < nc_; ++i)
445 if (align_[i] == 'R')
446 ws_[i] += 10 * df_width;
447 // Increase ws_[i] for 'C' column
448 if (align_[0] == 'C')
449 if (ws_[0] < 7 * workwidth / 8)
450 ws_[0] = 7 * workwidth / 8;
454 for (cxrow = row_.begin(); cxrow; ++cxrow) {
457 for (int i = 0; i < nc_; ++i) {
459 if (cxrow->getTab(i) <= 0) {
460 cxrow->setTab(i, df_width);
468 lf = (ws_[i] - cxrow->getTab(i))/2;
472 lf = ws_[i] - cxrow->getTab(i);
475 if (cxrow == row_.begin())
477 else if (cxrow.is_last())
478 lf = ws_[i] - cxrow->getTab(i);
480 lf = (ws_[i] - cxrow->getTab(i))/2;
483 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
484 cxrow->setTab(i, lf + rg);
485 rg = ws_[i] - ww + colsep();
486 if (cxrow == row_.begin())
487 width += ws_[i] + colsep();
489 cxrow->setBaseline(cxrow->getBaseline() - ascent);
492 metricsMarkers2(dim);
493 // Cache the inset dimension.
494 setDimCache(mi, dim);
498 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
500 drawWithMargin(pi, x, y, 0, 0);
504 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
505 int lmargin, int rmargin) const
507 Dimension const dim = dimension(*pi.base.bv);
508 BufferView const & bv = *pi.base.bv;
510 for (idx_type idx = 0; idx < nargs(); ++idx)
511 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
512 y + cellYOffset(idx));
514 for (row_type row = 0; row <= nrows(); ++row)
515 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
516 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
517 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
518 pi.pain.line(x + lmargin + 1, yy,
519 x + dim.width() - rmargin - 1, yy,
523 for (col_type col = 0; col <= ncols(); ++col)
524 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
525 int xx = x + lmargin + colinfo_[col].offset_
526 - i * vlinesep() - vlinesep()/2 - colsep()/2;
527 pi.pain.line(xx, y - dim.ascent() + 1,
528 xx, y + dim.descent() - 1,
531 drawMarkers2(pi, x, y);
535 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
537 // let the cells adjust themselves
538 //InsetMathNest::metrics(mi);
539 for (idx_type i = 0; i < nargs(); ++i)
540 cell(i).metricsT(mi, dim);
542 // compute absolute sizes of vertical structure
543 for (row_type row = 0; row < nrows(); ++row) {
546 for (col_type col = 0; col < ncols(); ++col) {
547 //MathData const & c = cell(index(row, col));
550 asc = max(asc, dimc.ascent());
551 desc = max(desc, dimc.descent());
553 rowinfo_[row].ascent_ = asc;
554 rowinfo_[row].descent_ = desc;
556 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
557 rowinfo_[nrows()].ascent_ = 0;
558 rowinfo_[nrows()].descent_ = 0;
560 // compute vertical offsets
561 rowinfo_[0].offset_ = 0;
562 for (row_type row = 1; row <= nrows(); ++row) {
563 rowinfo_[row].offset_ =
564 rowinfo_[row - 1].offset_ +
565 rowinfo_[row - 1].descent_ +
566 //rowinfo_[row - 1].skipPixels() +
568 //rowinfo_[row].lines_ * hlinesep() +
569 rowinfo_[row].ascent_;
572 // adjust vertical offset
579 h = rowinfo_[nrows() - 1].offset_;
582 h = rowinfo_[nrows() - 1].offset_ / 2;
584 for (row_type row = 0; row <= nrows(); ++row)
585 rowinfo_[row].offset_ -= h;
588 // compute absolute sizes of horizontal structure
589 for (col_type col = 0; col < ncols(); ++col) {
591 for (row_type row = 0; row < nrows(); ++row) {
593 //wid = max(wid, cell(index(row, col)).width());
595 colinfo_[col].width_ = wid;
597 colinfo_[ncols()].width_ = 0;
599 // compute horizontal offsets
600 colinfo_[0].offset_ = border();
601 for (col_type col = 1; col <= ncols(); ++col) {
602 colinfo_[col].offset_ =
603 colinfo_[col - 1].offset_ +
604 colinfo_[col - 1].width_ +
605 colinfo_[col - 1].skip_ +
607 //colinfo_[col].lines_ * vlinesep();
611 dim.wid = colinfo_[ncols() - 1].offset_
612 + colinfo_[ncols() - 1].width_
613 //+ vlinesep() * colinfo_[ncols()].lines_
616 dim.asc = -rowinfo_[0].offset_
617 + rowinfo_[0].ascent_
618 //+ hlinesep() * rowinfo_[0].lines_
621 dim.des = rowinfo_[nrows() - 1].offset_
622 + rowinfo_[nrows() - 1].descent_
623 //+ hlinesep() * rowinfo_[nrows()].lines_
628 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
630 // for (idx_type idx = 0; idx < nargs(); ++idx)
631 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
635 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
639 if (!rowinfo_[row].crskip_.zero())
640 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
641 else if(!rowinfo_[row].allow_newpage_)
644 // make sure an upcoming '[' does not break anything
645 if (row + 1 < nrows()) {
646 MathData const & c = cell(index(row + 1, 0));
647 if (c.size() && c.front()->getChar() == '[')
652 // only add \\ if necessary
653 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
656 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
660 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
662 if (col + 1 == lastcol)
664 return from_ascii(" & ");
668 void InsetMathGrid::addRow(row_type row)
670 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
672 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
674 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
678 void InsetMathGrid::appendRow()
680 rowinfo_.push_back(RowInfo());
681 //cells_.insert(cells_.end(), ncols(), MathData());
682 for (col_type col = 0; col < ncols(); ++col) {
683 cells_.push_back(cells_type::value_type());
684 cellinfo_.push_back(CellInfo());
689 void InsetMathGrid::delRow(row_type row)
694 cells_type::iterator it = cells_.begin() + row * ncols();
695 cells_.erase(it, it + ncols());
697 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
698 cellinfo_.erase(jt, jt + ncols());
700 rowinfo_.erase(rowinfo_.begin() + row);
704 void InsetMathGrid::copyRow(row_type row)
707 for (col_type col = 0; col < ncols(); ++col)
708 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
712 void InsetMathGrid::swapRow(row_type row)
716 if (row + 1 == nrows())
718 for (col_type col = 0; col < ncols(); ++col)
719 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
723 void InsetMathGrid::addCol(col_type newcol)
725 const col_type nc = ncols();
726 const row_type nr = nrows();
727 cells_type new_cells((nc + 1) * nr);
728 vector<CellInfo> new_cellinfo((nc + 1) * nr);
730 for (row_type row = 0; row < nr; ++row)
731 for (col_type col = 0; col < nc; ++col) {
732 new_cells[row * (nc + 1) + col + (col >= newcol)]
733 = cells_[row * nc + col];
734 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
735 = cellinfo_[row * nc + col];
737 swap(cells_, new_cells);
738 swap(cellinfo_, new_cellinfo);
741 inf.skip_ = defaultColSpace(newcol);
742 inf.align_ = defaultColAlign(newcol);
743 colinfo_.insert(colinfo_.begin() + newcol, inf);
747 void InsetMathGrid::delCol(col_type col)
753 vector<CellInfo> tmpcellinfo;
754 for (col_type i = 0; i < nargs(); ++i)
755 if (i % ncols() != col) {
756 tmpcells.push_back(cells_[i]);
757 tmpcellinfo.push_back(cellinfo_[i]);
759 swap(cells_, tmpcells);
760 swap(cellinfo_, tmpcellinfo);
762 colinfo_.erase(colinfo_.begin() + col);
766 void InsetMathGrid::copyCol(col_type col)
769 for (row_type row = 0; row < nrows(); ++row)
770 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
774 void InsetMathGrid::swapCol(col_type col)
778 if (col + 1 == ncols())
780 for (row_type row = 0; row < nrows(); ++row)
781 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
785 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
787 col_type c = col(idx);
788 int x = colinfo_[c].offset_;
789 char align = colinfo_[c].align_;
790 Dimension const & celldim = cell(idx).dimension(bv);
791 if (align == 'r' || align == 'R')
792 x += colinfo_[c].width_ - celldim.wid;
793 if (align == 'c' || align == 'C')
794 x += (colinfo_[c].width_ - celldim.wid) / 2;
799 int InsetMathGrid::cellYOffset(idx_type idx) const
801 return rowinfo_[row(idx)].offset_;
805 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
810 cur.idx() -= ncols();
812 if (cur.row() + 1 >= nrows())
814 cur.idx() += ncols();
816 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
821 bool InsetMathGrid::idxBackward(Cursor & cur) const
823 // leave matrix if at the front edge
827 cur.pos() = cur.lastpos();
832 bool InsetMathGrid::idxForward(Cursor & cur) const
834 // leave matrix if at the back edge
835 if (cur.col() + 1 == ncols())
843 bool InsetMathGrid::idxFirst(Cursor & cur) const
850 cur.idx() = (nrows() - 1) * ncols();
853 cur.idx() = ((nrows() - 1) / 2) * ncols();
860 bool InsetMathGrid::idxLast(Cursor & cur) const
864 cur.idx() = ncols() - 1;
867 cur.idx() = nargs() - 1;
870 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
872 cur.pos() = cur.lastpos();
877 bool InsetMathGrid::idxDelete(idx_type & idx)
879 // nothing to do if we have just one row
883 // nothing to do if we are in the middle of the last row of the inset
884 if (idx + ncols() > nargs())
887 // try to delete entire sequence of ncols() empty cells if possible
888 for (idx_type i = idx; i < idx + ncols(); ++i)
892 // move cells if necessary
893 for (idx_type i = index(row(idx), 0); i < idx; ++i)
894 swap(cell(i), cell(i + ncols()));
901 // undo effect of Ctrl-Tab (i.e. pull next cell)
902 //if (idx + 1 != nargs())
903 // cell(idx).swap(cell(idx + 1));
905 // we handled the event..
910 // reimplement old behaviour when pressing Delete in the last position
912 void InsetMathGrid::idxGlue(idx_type idx)
914 col_type c = col(idx);
915 if (c + 1 == ncols()) {
916 if (row(idx) + 1 != nrows()) {
917 for (col_type cc = 0; cc < ncols(); ++cc)
918 cell(idx).append(cell(idx + cc + 1));
919 delRow(row(idx) + 1);
922 cell(idx).append(cell(idx + 1));
923 for (col_type cc = c + 2; cc < ncols(); ++cc)
924 cell(idx - c + cc - 1) = cell(idx - c + cc);
925 cell(idx - c + ncols() - 1).clear();
930 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
932 return rowinfo_[row];
936 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
938 return rowinfo_[row];
942 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
944 row_type const ri = row(idx);
945 row_type const r1 = min(row(from), row(to));
946 row_type const r2 = max(row(from), row(to));
947 col_type const ci = col(idx);
948 col_type const c1 = min(col(from), col(to));
949 col_type const c2 = max(col(from), col(to));
950 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
955 void InsetMathGrid::normalize(NormalStream & os) const
958 for (row_type row = 0; row < nrows(); ++row) {
960 for (col_type col = 0; col < ncols(); ++col)
961 os << "[cell " << cell(index(row, col)) << ']';
968 void InsetMathGrid::mathmlize(MathStream & os) const
970 os << MTag("mtable");
971 for (row_type row = 0; row < nrows(); ++row) {
973 for (col_type col = 0; col < ncols(); ++col)
974 os << cell(index(row, col));
977 os << ETag("mtable");
981 void InsetMathGrid::write(WriteStream & os) const
984 for (row_type row = 0; row < nrows(); ++row) {
985 os << verboseHLine(rowinfo_[row].lines_);
986 // don't write & and empty cells at end of line
987 col_type lastcol = 0;
988 bool emptyline = true;
989 for (col_type col = 0; col < ncols(); ++col)
990 if (!cell(index(row, col)).empty()) {
994 for (col_type col = 0; col < lastcol; ++col)
995 os << cell(index(row, col)) << eocString(col, lastcol);
996 eol = eolString(row, emptyline, os.fragile());
998 // append newline only if line wasn't completely empty
999 // and this was not the last line in the grid
1000 if (!emptyline && row + 1 < nrows())
1003 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1015 int InsetMathGrid::colsep() const
1021 int InsetMathGrid::rowsep() const
1027 int InsetMathGrid::hlinesep() const
1033 int InsetMathGrid::vlinesep() const
1039 int InsetMathGrid::border() const
1045 void InsetMathGrid::splitCell(Cursor & cur)
1047 if (cur.idx() == cur.lastidx())
1049 MathData ar = cur.cell();
1050 ar.erase(0, cur.pos());
1051 cur.cell().erase(cur.pos(), cur.lastpos());
1054 cur.cell().insert(0, ar);
1058 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1060 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1061 switch (cmd.action) {
1063 // insert file functions
1064 case LFUN_LINE_DELETE:
1065 // FIXME: We use recordUndoInset when a change reflects more
1066 // than one cell, because recordUndo does not work for
1067 // multiple cells. Unfortunately this puts the cursor in front
1068 // of the inset after undo. This is (especilally for large
1070 cur.recordUndoInset();
1071 //autocorrect_ = false;
1079 if (cur.idx() > cur.lastidx())
1080 cur.idx() = cur.lastidx();
1081 if (cur.pos() > cur.lastpos())
1082 cur.pos() = cur.lastpos();
1085 case LFUN_CELL_SPLIT:
1090 case LFUN_CELL_BACKWARD:
1092 cur.selection() = false;
1093 if (!idxPrev(cur)) {
1094 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1099 case LFUN_CELL_FORWARD:
1100 // Can't handle selection by additional 'shift' as this is
1101 // hard bound to LFUN_CELL_BACKWARD
1102 cur.selection() = false;
1103 if (!idxNext(cur)) {
1104 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1109 case LFUN_NEW_LINE: {
1110 cur.recordUndoInset();
1111 row_type const r = cur.row();
1115 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1116 swap(cell(index(r, c)), cell(index(r + 1, c)));
1120 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1123 cur.pos() = cur.lastpos();
1125 //mathcursor->normalize();
1126 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1130 case LFUN_TABULAR_FEATURE: {
1131 cur.recordUndoInset();
1132 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1133 istringstream is(to_utf8(cmd.argument()));
1136 if (s == "valign-top")
1138 else if (s == "valign-middle")
1140 else if (s == "valign-bottom")
1142 else if (s == "align-left")
1143 halign('l', cur.col());
1144 else if (s == "align-right")
1145 halign('r', cur.col());
1146 else if (s == "align-center")
1147 halign('c', cur.col());
1148 else if (s == "append-row")
1149 for (int i = 0, n = extractInt(is); i < n; ++i)
1151 else if (s == "delete-row") {
1152 cur.clearSelection(); // bug 4323
1153 for (int i = 0, n = extractInt(is); i < n; ++i) {
1155 if (cur.idx() >= nargs())
1156 cur.idx() -= ncols();
1158 cur.pos() = 0; // trick, see below
1160 else if (s == "copy-row") {
1161 // Here (as later) we save the cursor col/row
1162 // in order to restore it after operation.
1163 row_type const r = cur.row();
1164 col_type const c = cur.col();
1165 for (int i = 0, n = extractInt(is); i < n; ++i)
1167 cur.idx() = index(r, c);
1169 else if (s == "swap-row") {
1171 // Trick to suppress same-idx-means-different-cell
1175 else if (s == "add-hline-above")
1176 rowinfo_[cur.row()].lines_++;
1177 else if (s == "add-hline-below")
1178 rowinfo_[cur.row()+1].lines_++;
1179 else if (s == "delete-hline-above")
1180 rowinfo_[cur.row()].lines_--;
1181 else if (s == "delete-hline-below")
1182 rowinfo_[cur.row()+1].lines_--;
1183 else if (s == "append-column") {
1184 row_type const r = cur.row();
1185 col_type const c = cur.col();
1186 for (int i = 0, n = extractInt(is); i < n; ++i)
1187 addCol(cur.col() + 1);
1188 cur.idx() = index(r, c);
1190 else if (s == "delete-column") {
1191 cur.clearSelection(); // bug 4323
1192 row_type const r = cur.row();
1193 col_type const c = cur.col();
1194 for (int i = 0, n = extractInt(is); i < n; ++i)
1195 delCol(col(cur.idx()));
1196 cur.idx() = index(r, min(c, cur.ncols() - 1));
1197 cur.pos() = 0; // trick, see above
1199 else if (s == "copy-column") {
1200 row_type const r = cur.row();
1201 col_type const c = cur.col();
1203 cur.idx() = index(r, c);
1205 else if (s == "swap-column") {
1207 cur.pos() = 0; // trick, see above
1209 else if (s == "add-vline-left") {
1210 colinfo_[cur.col()].lines_++;
1211 if (!colinfo_[cur.col()].special_.empty())
1212 colinfo_[cur.col()].special_ += '|';
1214 else if (s == "add-vline-right") {
1215 colinfo_[cur.col()+1].lines_++;
1216 if (!colinfo_[cur.col()+1].special_.empty())
1217 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1219 else if (s == "delete-vline-left") {
1220 colinfo_[cur.col()].lines_--;
1221 docstring & special = colinfo_[cur.col()].special_;
1222 if (!special.empty()) {
1223 docstring::size_type i = special.rfind('|');
1224 BOOST_ASSERT(i != docstring::npos);
1225 special.erase(i, 1);
1228 else if (s == "delete-vline-right") {
1229 colinfo_[cur.col()+1].lines_--;
1230 docstring & special = colinfo_[cur.col()+1].special_;
1231 if (!special.empty()) {
1232 docstring::size_type i = special.find('|');
1233 BOOST_ASSERT(i != docstring::npos);
1234 special.erase(i, 1);
1241 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1242 lyxerr << "returning FINISHED_LEFT" << endl;
1247 cur.message(_("Paste"));
1248 cap::replaceSelection(cur);
1250 if (cmd.argument().empty() && !theClipboard().isInternal())
1251 topaste = theClipboard().getAsText();
1253 idocstringstream is(cmd.argument());
1256 topaste = cap::getSelection(cur.buffer(), n);
1258 InsetMathGrid grid(1, 1);
1259 if (!topaste.empty())
1260 mathed_parse_normal(grid, topaste);
1262 if (grid.nargs() == 1) {
1263 // single cell/part of cell
1265 cur.cell().insert(cur.pos(), grid.cell(0));
1266 cur.pos() += grid.cell(0).size();
1269 cur.recordUndoInset();
1270 col_type const numcols =
1271 min(grid.ncols(), ncols() - col(cur.idx()));
1272 row_type const numrows =
1273 min(grid.nrows(), nrows() - cur.row());
1274 for (row_type r = 0; r < numrows; ++r) {
1275 for (col_type c = 0; c < numcols; ++c) {
1276 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1277 cell(i).insert(0, grid.cell(grid.index(r, c)));
1279 // append the left over horizontal cells to the last column
1280 idx_type i = index(r + cur.row(), ncols() - 1);
1281 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1282 cell(i).append(grid.cell(grid.index(r, c)));
1284 // append the left over vertical cells to the last _cell_
1285 idx_type i = nargs() - 1;
1286 for (row_type r = numrows; r < grid.nrows(); ++r)
1287 for (col_type c = 0; c < grid.ncols(); ++c)
1288 cell(i).append(grid.cell(grid.index(r, c)));
1290 cur.clearSelection(); // bug 393
1295 case LFUN_LINE_BEGIN_SELECT:
1296 case LFUN_LINE_BEGIN:
1297 case LFUN_WORD_BACKWARD_SELECT:
1298 case LFUN_WORD_BACKWARD:
1299 case LFUN_WORD_LEFT_SELECT:
1300 case LFUN_WORD_LEFT:
1301 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1302 cmd.action == LFUN_WORD_LEFT_SELECT ||
1303 cmd.action == LFUN_LINE_BEGIN_SELECT);
1304 cur.macroModeClose();
1305 if (cur.pos() != 0) {
1307 } else if (cur.idx() % cur.ncols() != 0) {
1308 cur.idx() -= cur.idx() % cur.ncols();
1310 } else if (cur.idx() != 0) {
1314 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1319 case LFUN_WORD_FORWARD_SELECT:
1320 case LFUN_WORD_FORWARD:
1321 case LFUN_WORD_RIGHT_SELECT:
1322 case LFUN_WORD_RIGHT:
1323 case LFUN_LINE_END_SELECT:
1325 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1326 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1327 cmd.action == LFUN_LINE_END_SELECT);
1328 cur.macroModeClose();
1330 if (cur.pos() != cur.lastpos()) {
1331 cur.pos() = cur.lastpos();
1332 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1333 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1334 cur.pos() = cur.lastpos();
1335 } else if (cur.idx() != cur.lastidx()) {
1336 cur.idx() = cur.lastidx();
1337 cur.pos() = cur.lastpos();
1339 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1345 InsetMathNest::doDispatch(cur, cmd);
1350 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1351 FuncStatus & status) const
1353 switch (cmd.action) {
1354 case LFUN_TABULAR_FEATURE: {
1355 string const s = to_utf8(cmd.argument());
1356 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1357 status.enabled(false);
1358 status.message(from_utf8(N_("Only one row")));
1362 (s == "delete-column" || s == "swap-column")) {
1363 status.enabled(false);
1364 status.message(from_utf8(N_("Only one column")));
1367 if ((rowinfo_[cur.row()].lines_ == 0 &&
1368 s == "delete-hline-above") ||
1369 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1370 s == "delete-hline-below")) {
1371 status.enabled(false);
1372 status.message(from_utf8(N_("No hline to delete")));
1376 if ((colinfo_[cur.col()].lines_ == 0 &&
1377 s == "delete-vline-left") ||
1378 (colinfo_[cur.col() + 1].lines_ == 0 &&
1379 s == "delete-vline-right")) {
1380 status.enabled(false);
1381 status.message(from_utf8(N_("No vline to delete")));
1384 if (s == "valign-top" || s == "valign-middle" ||
1385 s == "valign-bottom" || s == "align-left" ||
1386 s == "align-right" || s == "align-center" ||
1387 s == "append-row" || s == "delete-row" ||
1388 s == "copy-row" || s == "swap-row" ||
1389 s == "add-hline-above" || s == "add-hline-below" ||
1390 s == "delete-hline-above" || s == "delete-hline-below" ||
1391 s == "append-column" || s == "delete-column" ||
1392 s == "copy-column" || s == "swap-column" ||
1393 s == "add-vline-left" || s == "add-vline-right" ||
1394 s == "delete-vline-left" || s == "delete-vline-right")
1395 status.enabled(true);
1397 status.enabled(false);
1398 status.message(bformat(
1399 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1402 status.setOnOff((s == "align-left" && halign(cur.col()) == 'l')
1403 || (s == "align-right" && halign(cur.col()) == 'r')
1404 || (s == "align-center" && halign(cur.col()) == 'c')
1405 || (s == "valign-top" && valign() == 't')
1406 || (s == "valign-bottom" && valign() == 'b')
1407 || (s == "valign-middle" && valign() == 'm'));
1410 // FIXME: What did this code do?
1411 // Please check whether it is still needed!
1412 // should be more precise
1413 if (v_align_ == '\0') {
1414 status.enable(true);
1417 if (cmd.argument().empty()) {
1418 status.enable(false);
1421 if (!contains("tcb", cmd.argument()[0])) {
1422 status.enable(false);
1425 status.setOnOff(cmd.argument()[0] == v_align_);
1426 status.enabled(true);
1431 case LFUN_CELL_SPLIT:
1432 status.enabled(true);
1435 case LFUN_CELL_BACKWARD:
1436 case LFUN_CELL_FORWARD:
1437 status.enabled(true);
1441 return InsetMathNest::getStatus(cur, cmd, status);