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"
15 #include "MathParser.h"
16 #include "MathStream.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "FuncStatus.h"
24 #include "FuncRequest.h"
28 #include "frontends/Clipboard.h"
29 #include "frontends/Painter.h"
31 #include "support/lstrings.h"
38 using support::bformat;
47 using std::istringstream;
53 docstring verboseHLine(int n)
56 for (int i = 0; i < n; ++i)
64 int extractInt(istream & is)
68 return (num == 0) ? 1 : num;
74 //////////////////////////////////////////////////////////////
77 InsetMathGrid::CellInfo::CellInfo()
84 //////////////////////////////////////////////////////////////
87 InsetMathGrid::RowInfo::RowInfo()
88 : lines_(0), skip_(0), allow_pagebreak_(true)
93 int InsetMathGrid::RowInfo::skipPixels() const
95 return crskip_.inBP();
100 //////////////////////////////////////////////////////////////
103 InsetMathGrid::ColInfo::ColInfo()
104 : align_('c'), lines_(0)
108 //////////////////////////////////////////////////////////////
111 InsetMathGrid::InsetMathGrid(char v, docstring const & h)
112 : InsetMathNest(guessColumns(h)),
114 colinfo_(guessColumns(h) + 1),
115 cellinfo_(1 * guessColumns(h))
120 //lyxerr << "created grid with " << ncols() << " columns" << endl;
124 InsetMathGrid::InsetMathGrid()
135 InsetMathGrid::InsetMathGrid(col_type m, row_type n)
136 : InsetMathNest(m * n),
146 InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
147 : InsetMathNest(m * n),
159 Inset * InsetMathGrid::clone() const
161 return new InsetMathGrid(*this);
165 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
167 return col + ncols() * row;
171 void InsetMathGrid::setDefaults()
174 lyxerr << "positive number of columns expected" << endl;
176 // lyxerr << "positive number of rows expected" << endl;
177 for (col_type col = 0; col < ncols(); ++col) {
178 colinfo_[col].align_ = defaultColAlign(col);
179 colinfo_[col].skip_ = defaultColSpace(col);
180 colinfo_[col].special_.clear();
185 void InsetMathGrid::halign(docstring const & hh)
188 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
191 colinfo_[col].lines_++;
192 } else if ((c == 'p' || c == 'm' || c == 'b'||
193 c == '!' || c == '@' || c == '>' || c == '<') &&
194 it + 1 != hh.end() && *(it + 1) == '{') {
195 // @{decl.} and p{width} are standard LaTeX, the
196 // others are extensions by array.sty
197 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
199 // this declares a new column
201 // Only intercolumn stuff is allowed
202 // in the last dummy column
204 colinfo_[col].align_ = 'l';
206 // this is intercolumn stuff
207 if (colinfo_[col].special_.empty())
208 // Overtake possible lines
209 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
213 while (it != hh.end()) {
215 colinfo_[col].special_ += c;
221 if (brace_open > 0 && brace_open == brace_close)
226 colinfo_[col].lines_ = std::count(
227 colinfo_[col].special_.begin(),
228 colinfo_[col].special_.end(), '|');
229 LYXERR(Debug::MATHED)
230 << "special column separator: `"
231 << to_utf8(colinfo_[col].special_)
234 colinfo_[col].lines_ = 0;
235 colinfo_[col].special_.clear();
237 } else if (col >= ncols()) {
238 // Only intercolumn stuff is allowed in the last
241 } else if (c == 'c' || c == 'l' || c == 'r') {
242 colinfo_[col].align_ = static_cast<char>(c);
243 if (!colinfo_[col].special_.empty()) {
244 colinfo_[col].special_ += c;
245 colinfo_[col].lines_ = std::count(
246 colinfo_[col].special_.begin(),
247 colinfo_[col].special_.end(), '|');
248 LYXERR(Debug::MATHED)
249 << "special column separator: `"
250 << to_utf8(colinfo_[col].special_)
254 colinfo_[col].lines_ = 0;
255 colinfo_[col].special_.clear();
257 lyxerr << "unknown column separator: '" << c << "'" << endl;
262 col_type n = hh.size();
265 for (col_type col = 0; col < n; ++col)
266 colinfo_[col].align_ = hh[col];
271 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
274 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
275 if (*it == 'c' || *it == 'l' || *it == 'r'||
276 *it == 'p' || *it == 'm' || *it == 'b')
278 // let's have at least one column, even if we did not recognize its
286 void InsetMathGrid::halign(char h, col_type col)
288 colinfo_[col].align_ = h;
289 if (!colinfo_[col].special_.empty()) {
290 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
291 if (c == 'l' || c == 'c' || c == 'r')
294 // FIXME: Change alignment of p, m and b columns, too
298 char InsetMathGrid::halign(col_type col) const
300 return colinfo_[col].align_;
304 docstring InsetMathGrid::halign() const
307 for (col_type col = 0; col < ncols(); ++col) {
308 if (colinfo_[col].special_.empty()) {
309 res += docstring(colinfo_[col].lines_, '|');
310 res += colinfo_[col].align_;
312 res += colinfo_[col].special_;
314 if (colinfo_[ncols()].special_.empty())
315 return res + docstring(colinfo_[ncols()].lines_, '|');
316 return res + colinfo_[ncols()].special_;
320 void InsetMathGrid::valign(char c)
326 char InsetMathGrid::valign() const
332 InsetMathGrid::col_type InsetMathGrid::ncols() const
334 return colinfo_.size() - 1;
338 InsetMathGrid::row_type InsetMathGrid::nrows() const
340 return rowinfo_.size() - 1;
344 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
346 return idx % ncols();
350 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
352 return idx / ncols();
356 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
358 rowinfo_[row].crskip_ = crskip;
362 Length InsetMathGrid::vcrskip(row_type row) const
364 return rowinfo_[row].crskip_;
368 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
370 // let the cells adjust themselves
371 InsetMathNest::metrics(mi);
373 BufferView & bv = *mi.base.bv;
375 // compute absolute sizes of vertical structure
376 for (row_type row = 0; row < nrows(); ++row) {
379 for (col_type col = 0; col < ncols(); ++col) {
380 Dimension const & dimc = cell(index(row, col)).dimension(bv);
381 asc = max(asc, dimc.asc);
382 desc = max(desc, dimc.des);
384 rowinfo_[row].ascent_ = asc;
385 rowinfo_[row].descent_ = desc;
387 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
388 rowinfo_[nrows()].ascent_ = 0;
389 rowinfo_[nrows()].descent_ = 0;
391 // compute vertical offsets
392 rowinfo_[0].offset_ = 0;
393 for (row_type row = 1; row <= nrows(); ++row) {
394 rowinfo_[row].offset_ =
395 rowinfo_[row - 1].offset_ +
396 rowinfo_[row - 1].descent_ +
397 rowinfo_[row - 1].skipPixels() +
399 rowinfo_[row].lines_ * hlinesep() +
400 rowinfo_[row].ascent_;
403 // adjust vertical offset
410 h = rowinfo_[nrows() - 1].offset_;
413 h = rowinfo_[nrows() - 1].offset_ / 2;
415 for (row_type row = 0; row <= nrows(); ++row)
416 rowinfo_[row].offset_ -= h;
419 // compute absolute sizes of horizontal structure
420 for (col_type col = 0; col < ncols(); ++col) {
422 for (row_type row = 0; row < nrows(); ++row)
423 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
424 colinfo_[col].width_ = wid;
426 colinfo_[ncols()].width_ = 0;
428 // compute horizontal offsets
429 colinfo_[0].offset_ = border();
430 for (col_type col = 1; col <= ncols(); ++col) {
431 colinfo_[col].offset_ =
432 colinfo_[col - 1].offset_ +
433 colinfo_[col - 1].width_ +
434 colinfo_[col - 1].skip_ +
436 colinfo_[col].lines_ * vlinesep();
440 dim.wid = colinfo_[ncols() - 1].offset_
441 + colinfo_[ncols() - 1].width_
442 + vlinesep() * colinfo_[ncols()].lines_
445 dim.asc = - rowinfo_[0].offset_
446 + rowinfo_[0].ascent_
447 + hlinesep() * rowinfo_[0].lines_
450 dim.des = rowinfo_[nrows() - 1].offset_
451 + rowinfo_[nrows() - 1].descent_
452 + hlinesep() * rowinfo_[nrows()].lines_
457 // Increase ws_[i] for 'R' columns (except the first one)
458 for (int i = 1; i < nc_; ++i)
459 if (align_[i] == 'R')
460 ws_[i] += 10 * df_width;
461 // Increase ws_[i] for 'C' column
462 if (align_[0] == 'C')
463 if (ws_[0] < 7 * workwidth / 8)
464 ws_[0] = 7 * workwidth / 8;
468 for (cxrow = row_.begin(); cxrow; ++cxrow) {
471 for (int i = 0; i < nc_; ++i) {
473 if (cxrow->getTab(i) <= 0) {
474 cxrow->setTab(i, df_width);
482 lf = (ws_[i] - cxrow->getTab(i))/2;
486 lf = ws_[i] - cxrow->getTab(i);
489 if (cxrow == row_.begin())
491 else if (cxrow.is_last())
492 lf = ws_[i] - cxrow->getTab(i);
494 lf = (ws_[i] - cxrow->getTab(i))/2;
497 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
498 cxrow->setTab(i, lf + rg);
499 rg = ws_[i] - ww + colsep();
500 if (cxrow == row_.begin())
501 width += ws_[i] + colsep();
503 cxrow->setBaseline(cxrow->getBaseline() - ascent);
506 metricsMarkers2(dim);
507 // Cache the inset dimension.
508 setDimCache(mi, dim);
512 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
514 drawWithMargin(pi, x, y, 0, 0);
518 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
519 int lmargin, int rmargin) const
521 Dimension const dim = dimension(*pi.base.bv);
522 BufferView const & bv = *pi.base.bv;
524 for (idx_type idx = 0; idx < nargs(); ++idx)
525 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
526 y + cellYOffset(idx));
528 for (row_type row = 0; row <= nrows(); ++row)
529 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
530 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
531 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
532 pi.pain.line(x + lmargin + 1, yy,
533 x + dim.width() - rmargin - 1, yy,
537 for (col_type col = 0; col <= ncols(); ++col)
538 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
539 int xx = x + lmargin + colinfo_[col].offset_
540 - i * vlinesep() - vlinesep()/2 - colsep()/2;
541 pi.pain.line(xx, y - dim.ascent() + 1,
542 xx, y + dim.descent() - 1,
545 drawMarkers2(pi, x, y);
549 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
551 // let the cells adjust themselves
552 //InsetMathNest::metrics(mi);
553 for (idx_type i = 0; i < nargs(); ++i)
554 cell(i).metricsT(mi, dim);
556 // compute absolute sizes of vertical structure
557 for (row_type row = 0; row < nrows(); ++row) {
560 for (col_type col = 0; col < ncols(); ++col) {
561 //MathData const & c = cell(index(row, col));
564 asc = max(asc, dimc.ascent());
565 desc = max(desc, dimc.descent());
567 rowinfo_[row].ascent_ = asc;
568 rowinfo_[row].descent_ = desc;
570 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
571 rowinfo_[nrows()].ascent_ = 0;
572 rowinfo_[nrows()].descent_ = 0;
574 // compute vertical offsets
575 rowinfo_[0].offset_ = 0;
576 for (row_type row = 1; row <= nrows(); ++row) {
577 rowinfo_[row].offset_ =
578 rowinfo_[row - 1].offset_ +
579 rowinfo_[row - 1].descent_ +
580 //rowinfo_[row - 1].skipPixels() +
582 //rowinfo_[row].lines_ * hlinesep() +
583 rowinfo_[row].ascent_;
586 // adjust vertical offset
593 h = rowinfo_[nrows() - 1].offset_;
596 h = rowinfo_[nrows() - 1].offset_ / 2;
598 for (row_type row = 0; row <= nrows(); ++row)
599 rowinfo_[row].offset_ -= h;
602 // compute absolute sizes of horizontal structure
603 for (col_type col = 0; col < ncols(); ++col) {
605 for (row_type row = 0; row < nrows(); ++row) {
607 //wid = max(wid, cell(index(row, col)).width());
609 colinfo_[col].width_ = wid;
611 colinfo_[ncols()].width_ = 0;
613 // compute horizontal offsets
614 colinfo_[0].offset_ = border();
615 for (col_type col = 1; col <= ncols(); ++col) {
616 colinfo_[col].offset_ =
617 colinfo_[col - 1].offset_ +
618 colinfo_[col - 1].width_ +
619 colinfo_[col - 1].skip_ +
621 //colinfo_[col].lines_ * vlinesep();
625 dim.wid = colinfo_[ncols() - 1].offset_
626 + colinfo_[ncols() - 1].width_
627 //+ vlinesep() * colinfo_[ncols()].lines_
630 dim.asc = -rowinfo_[0].offset_
631 + rowinfo_[0].ascent_
632 //+ hlinesep() * rowinfo_[0].lines_
635 dim.des = rowinfo_[nrows() - 1].offset_
636 + rowinfo_[nrows() - 1].descent_
637 //+ hlinesep() * rowinfo_[nrows()].lines_
642 void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
644 // for (idx_type idx = 0; idx < nargs(); ++idx)
645 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
649 docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) const
653 if (!rowinfo_[row].crskip_.zero())
654 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
655 else if(!rowinfo_[row].allow_pagebreak_)
658 // make sure an upcoming '[' does not break anything
659 if (row + 1 < nrows()) {
660 MathData const & c = cell(index(row + 1, 0));
661 if (c.size() && c.front()->getChar() == '[')
666 // only add \\ if necessary
667 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
670 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
674 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
676 if (col + 1 == lastcol)
678 return from_ascii(" & ");
682 void InsetMathGrid::addRow(row_type row)
684 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
686 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
688 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
692 void InsetMathGrid::appendRow()
694 rowinfo_.push_back(RowInfo());
695 //cells_.insert(cells_.end(), ncols(), MathData());
696 for (col_type col = 0; col < ncols(); ++col) {
697 cells_.push_back(cells_type::value_type());
698 cellinfo_.push_back(CellInfo());
703 void InsetMathGrid::delRow(row_type row)
708 cells_type::iterator it = cells_.begin() + row * ncols();
709 cells_.erase(it, it + ncols());
711 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
712 cellinfo_.erase(jt, jt + ncols());
714 rowinfo_.erase(rowinfo_.begin() + row);
718 void InsetMathGrid::copyRow(row_type row)
721 for (col_type col = 0; col < ncols(); ++col)
722 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
726 void InsetMathGrid::swapRow(row_type row)
730 if (row + 1 == nrows())
732 for (col_type col = 0; col < ncols(); ++col)
733 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
737 void InsetMathGrid::addCol(col_type newcol)
739 const col_type nc = ncols();
740 const row_type nr = nrows();
741 cells_type new_cells((nc + 1) * nr);
742 vector<CellInfo> new_cellinfo((nc + 1) * nr);
744 for (row_type row = 0; row < nr; ++row)
745 for (col_type col = 0; col < nc; ++col) {
746 new_cells[row * (nc + 1) + col + (col >= newcol)]
747 = cells_[row * nc + col];
748 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
749 = cellinfo_[row * nc + col];
751 swap(cells_, new_cells);
752 swap(cellinfo_, new_cellinfo);
755 inf.skip_ = defaultColSpace(newcol);
756 inf.align_ = defaultColAlign(newcol);
757 colinfo_.insert(colinfo_.begin() + newcol, inf);
761 void InsetMathGrid::delCol(col_type col)
767 vector<CellInfo> tmpcellinfo;
768 for (col_type i = 0; i < nargs(); ++i)
769 if (i % ncols() != col) {
770 tmpcells.push_back(cells_[i]);
771 tmpcellinfo.push_back(cellinfo_[i]);
773 swap(cells_, tmpcells);
774 swap(cellinfo_, tmpcellinfo);
776 colinfo_.erase(colinfo_.begin() + col);
780 void InsetMathGrid::copyCol(col_type col)
783 for (row_type row = 0; row < nrows(); ++row)
784 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
788 void InsetMathGrid::swapCol(col_type col)
792 if (col + 1 == ncols())
794 for (row_type row = 0; row < nrows(); ++row)
795 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
799 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
801 col_type c = col(idx);
802 int x = colinfo_[c].offset_;
803 char align = colinfo_[c].align_;
804 Dimension const & celldim = cell(idx).dimension(bv);
805 if (align == 'r' || align == 'R')
806 x += colinfo_[c].width_ - celldim.wid;
807 if (align == 'c' || align == 'C')
808 x += (colinfo_[c].width_ - celldim.wid) / 2;
813 int InsetMathGrid::cellYOffset(idx_type idx) const
815 return rowinfo_[row(idx)].offset_;
819 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
824 cur.idx() -= ncols();
826 if (cur.row() + 1 >= nrows())
828 cur.idx() += ncols();
830 cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
835 bool InsetMathGrid::idxLeft(Cursor & cur) const
837 // leave matrix if on the left hand edge
841 cur.pos() = cur.lastpos();
846 bool InsetMathGrid::idxRight(Cursor & cur) const
848 // leave matrix if on the right hand edge
849 if (cur.col() + 1 == ncols())
857 bool InsetMathGrid::idxFirst(Cursor & cur) const
864 cur.idx() = (nrows() - 1) * ncols();
867 cur.idx() = ((nrows() - 1) / 2) * ncols();
874 bool InsetMathGrid::idxLast(Cursor & cur) const
878 cur.idx() = ncols() - 1;
881 cur.idx() = nargs() - 1;
884 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
886 cur.pos() = cur.lastpos();
891 bool InsetMathGrid::idxDelete(idx_type & idx)
893 // nothing to do if we have just one row
897 // nothing to do if we are in the middle of the last row of the inset
898 if (idx + ncols() > nargs())
901 // try to delete entire sequence of ncols() empty cells if possible
902 for (idx_type i = idx; i < idx + ncols(); ++i)
906 // move cells if necessary
907 for (idx_type i = index(row(idx), 0); i < idx; ++i)
908 swap(cell(i), cell(i + ncols()));
915 // undo effect of Ctrl-Tab (i.e. pull next cell)
916 //if (idx + 1 != nargs())
917 // cell(idx).swap(cell(idx + 1));
919 // we handled the event..
924 // reimplement old behaviour when pressing Delete in the last position
926 void InsetMathGrid::idxGlue(idx_type idx)
928 col_type c = col(idx);
929 if (c + 1 == ncols()) {
930 if (row(idx) + 1 != nrows()) {
931 for (col_type cc = 0; cc < ncols(); ++cc)
932 cell(idx).append(cell(idx + cc + 1));
933 delRow(row(idx) + 1);
936 cell(idx).append(cell(idx + 1));
937 for (col_type cc = c + 2; cc < ncols(); ++cc)
938 cell(idx - c + cc - 1) = cell(idx - c + cc);
939 cell(idx - c + ncols() - 1).clear();
944 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
946 return rowinfo_[row];
950 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
952 return rowinfo_[row];
956 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
958 row_type const ri = row(idx);
959 row_type const r1 = min(row(from), row(to));
960 row_type const r2 = max(row(from), row(to));
961 col_type const ci = col(idx);
962 col_type const c1 = min(col(from), col(to));
963 col_type const c2 = max(col(from), col(to));
964 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
969 void InsetMathGrid::normalize(NormalStream & os) const
972 for (row_type row = 0; row < nrows(); ++row) {
974 for (col_type col = 0; col < ncols(); ++col)
975 os << "[cell " << cell(index(row, col)) << ']';
982 void InsetMathGrid::mathmlize(MathStream & os) const
984 os << MTag("mtable");
985 for (row_type row = 0; row < nrows(); ++row) {
987 for (col_type col = 0; col < ncols(); ++col)
988 os << cell(index(row, col));
991 os << ETag("mtable");
995 void InsetMathGrid::write(WriteStream & os) const
998 for (row_type row = 0; row < nrows(); ++row) {
999 os << verboseHLine(rowinfo_[row].lines_);
1000 // don't write & and empty cells at end of line
1001 col_type lastcol = 0;
1002 bool emptyline = true;
1003 for (col_type col = 0; col < ncols(); ++col)
1004 if (!cell(index(row, col)).empty()) {
1008 for (col_type col = 0; col < lastcol; ++col)
1009 os << cell(index(row, col)) << eocString(col, lastcol);
1010 eol = eolString(row, emptyline, os.fragile());
1012 // append newline only if line wasn't completely empty
1013 // and this was not the last line in the grid
1014 if (!emptyline && row + 1 < nrows())
1017 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1029 int InsetMathGrid::colsep() const
1035 int InsetMathGrid::rowsep() const
1041 int InsetMathGrid::hlinesep() const
1047 int InsetMathGrid::vlinesep() const
1053 int InsetMathGrid::border() const
1059 void InsetMathGrid::splitCell(Cursor & cur)
1061 if (cur.idx() == cur.lastidx())
1063 MathData ar = cur.cell();
1064 ar.erase(0, cur.pos());
1065 cur.cell().erase(cur.pos(), cur.lastpos());
1068 cur.cell().insert(0, ar);
1072 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1074 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1075 switch (cmd.action) {
1077 // insert file functions
1078 case LFUN_LINE_DELETE:
1079 // FIXME: We use recordUndoInset when a change reflects more
1080 // than one cell, because recordUndo does not work for
1081 // multiple cells. Unfortunately this puts the cursor in front
1082 // of the inset after undo. This is (especilally for large
1084 recordUndoInset(cur);
1085 //autocorrect_ = false;
1093 if (cur.idx() > cur.lastidx())
1094 cur.idx() = cur.lastidx();
1095 if (cur.pos() > cur.lastpos())
1096 cur.pos() = cur.lastpos();
1099 case LFUN_CELL_SPLIT:
1104 case LFUN_CELL_BACKWARD:
1106 cur.selection() = false;
1107 if (!idxPrev(cur)) {
1108 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1113 case LFUN_CELL_FORWARD:
1114 // Can't handle selection by additional 'shift' as this is
1115 // hard bound to LFUN_CELL_BACKWARD
1116 cur.selection() = false;
1117 if (!idxNext(cur)) {
1118 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1123 case LFUN_BREAK_LINE: {
1124 recordUndoInset(cur);
1125 row_type const r = cur.row();
1129 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1130 swap(cell(index(r, c)), cell(index(r + 1, c)));
1134 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1137 cur.pos() = cur.lastpos();
1139 //mathcursor->normalize();
1140 //cmd = FuncRequest(LFUN_FINISHED_LEFT);
1144 case LFUN_TABULAR_FEATURE: {
1145 recordUndoInset(cur);
1146 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1147 istringstream is(to_utf8(cmd.argument()));
1150 if (s == "valign-top")
1152 else if (s == "valign-middle")
1154 else if (s == "valign-bottom")
1156 else if (s == "align-left")
1157 halign('l', cur.col());
1158 else if (s == "align-right")
1159 halign('r', cur.col());
1160 else if (s == "align-center")
1161 halign('c', cur.col());
1162 else if (s == "append-row")
1163 for (int i = 0, n = extractInt(is); i < n; ++i)
1165 else if (s == "delete-row") {
1166 for (int i = 0, n = extractInt(is); i < n; ++i) {
1168 if (cur.idx() >= nargs())
1169 cur.idx() -= ncols();
1171 cur.pos() = 0; // trick, see below
1173 else if (s == "copy-row") {
1174 // Here (as later) we save the cursor col/row
1175 // in order to restore it after operation.
1176 row_type const r = cur.row();
1177 col_type const c = cur.col();
1178 for (int i = 0, n = extractInt(is); i < n; ++i)
1180 cur.idx() = index(r, c);
1182 else if (s == "swap-row") {
1184 // Trick to suppress same-idx-means-different-cell
1188 else if (s == "add-hline-above")
1189 rowinfo_[cur.row()].lines_++;
1190 else if (s == "add-hline-below")
1191 rowinfo_[cur.row()+1].lines_++;
1192 else if (s == "delete-hline-above")
1193 rowinfo_[cur.row()].lines_--;
1194 else if (s == "delete-hline-below")
1195 rowinfo_[cur.row()+1].lines_--;
1196 else if (s == "append-column") {
1197 row_type const r = cur.row();
1198 col_type const c = cur.col();
1199 for (int i = 0, n = extractInt(is); i < n; ++i)
1201 cur.idx() = index(r, c);
1203 else if (s == "delete-column") {
1204 row_type const r = cur.row();
1205 col_type const c = cur.col();
1206 for (int i = 0, n = extractInt(is); i < n; ++i)
1207 delCol(col(cur.idx()));
1208 cur.idx() = index(r, min(c, cur.ncols() - 1));
1209 cur.pos() = 0; // trick, see above
1211 else if (s == "copy-column") {
1212 row_type const r = cur.row();
1213 col_type const c = cur.col();
1215 cur.idx() = index(r, c);
1217 else if (s == "swap-column") {
1219 cur.pos() = 0; // trick, see above
1221 else if (s == "add-vline-left") {
1222 colinfo_[cur.col()].lines_++;
1223 if (!colinfo_[cur.col()].special_.empty())
1224 colinfo_[cur.col()].special_ += '|';
1226 else if (s == "add-vline-right") {
1227 colinfo_[cur.col()+1].lines_++;
1228 if (!colinfo_[cur.col()+1].special_.empty())
1229 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1231 else if (s == "delete-vline-left") {
1232 colinfo_[cur.col()].lines_--;
1233 docstring & special = colinfo_[cur.col()].special_;
1234 if (!special.empty()) {
1235 docstring::size_type i = special.rfind('|');
1236 BOOST_ASSERT(i != docstring::npos);
1237 special.erase(i, 1);
1240 else if (s == "delete-vline-right") {
1241 colinfo_[cur.col()+1].lines_--;
1242 docstring & special = colinfo_[cur.col()+1].special_;
1243 if (!special.empty()) {
1244 docstring::size_type i = special.find('|');
1245 BOOST_ASSERT(i != docstring::npos);
1246 special.erase(i, 1);
1253 lyxerr << "returning FINISHED_LEFT" << endl;
1258 cur.message(_("Paste"));
1259 cap::replaceSelection(cur);
1261 if (cmd.argument().empty() && !theClipboard().isInternal())
1262 topaste = theClipboard().getAsText();
1264 idocstringstream is(cmd.argument());
1267 topaste = cap::getSelection(cur.buffer(), n);
1269 InsetMathGrid grid(1, 1);
1270 if (!topaste.empty())
1271 mathed_parse_normal(grid, topaste);
1273 if (grid.nargs() == 1) {
1274 // single cell/part of cell
1276 cur.cell().insert(cur.pos(), grid.cell(0));
1277 cur.pos() += grid.cell(0).size();
1280 recordUndoInset(cur);
1281 col_type const numcols =
1282 min(grid.ncols(), ncols() - col(cur.idx()));
1283 row_type const numrows =
1284 min(grid.nrows(), nrows() - cur.row());
1285 for (row_type r = 0; r < numrows; ++r) {
1286 for (col_type c = 0; c < numcols; ++c) {
1287 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1288 cell(i).insert(0, grid.cell(grid.index(r, c)));
1290 // append the left over horizontal cells to the last column
1291 idx_type i = index(r + cur.row(), ncols() - 1);
1292 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1293 cell(i).append(grid.cell(grid.index(r, c)));
1295 // append the left over vertical cells to the last _cell_
1296 idx_type i = nargs() - 1;
1297 for (row_type r = numrows; r < grid.nrows(); ++r)
1298 for (col_type c = 0; c < grid.ncols(); ++c)
1299 cell(i).append(grid.cell(grid.index(r, c)));
1301 cur.clearSelection(); // bug 393
1306 case LFUN_LINE_BEGIN_SELECT:
1307 case LFUN_LINE_BEGIN:
1308 case LFUN_WORD_BACKWARD_SELECT:
1309 case LFUN_WORD_BACKWARD:
1310 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1311 cmd.action == LFUN_LINE_BEGIN_SELECT);
1312 cur.macroModeClose();
1313 if (cur.pos() != 0) {
1315 } else if (cur.idx() % cur.ncols() != 0) {
1316 cur.idx() -= cur.idx() % cur.ncols();
1318 } else if (cur.idx() != 0) {
1322 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1327 case LFUN_WORD_FORWARD_SELECT:
1328 case LFUN_WORD_FORWARD:
1329 case LFUN_LINE_END_SELECT:
1331 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1332 cmd.action == LFUN_LINE_END_SELECT);
1333 cur.macroModeClose();
1335 if (cur.pos() != cur.lastpos()) {
1336 cur.pos() = cur.lastpos();
1337 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1338 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1339 cur.pos() = cur.lastpos();
1340 } else if (cur.idx() != cur.lastidx()) {
1341 cur.idx() = cur.lastidx();
1342 cur.pos() = cur.lastpos();
1344 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1350 InsetMathNest::doDispatch(cur, cmd);
1355 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1356 FuncStatus & status) const
1358 switch (cmd.action) {
1359 case LFUN_TABULAR_FEATURE: {
1360 string const s = to_utf8(cmd.argument());
1361 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1362 status.enabled(false);
1363 status.message(from_utf8(N_("Only one row")));
1367 (s == "delete-column" || s == "swap-column")) {
1368 status.enabled(false);
1369 status.message(from_utf8(N_("Only one column")));
1372 if ((rowinfo_[cur.row()].lines_ == 0 &&
1373 s == "delete-hline-above") ||
1374 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1375 s == "delete-hline-below")) {
1376 status.enabled(false);
1377 status.message(from_utf8(N_("No hline to delete")));
1381 if ((colinfo_[cur.col()].lines_ == 0 &&
1382 s == "delete-vline-left") ||
1383 (colinfo_[cur.col() + 1].lines_ == 0 &&
1384 s == "delete-vline-right")) {
1385 status.enabled(false);
1386 status.message(from_utf8(N_("No vline to delete")));
1389 if (s == "valign-top" || s == "valign-middle" ||
1390 s == "valign-bottom" || s == "align-left" ||
1391 s == "align-right" || s == "align-center" ||
1392 s == "append-row" || s == "delete-row" ||
1393 s == "copy-row" || s == "swap-row" ||
1394 s == "add-hline-above" || s == "add-hline-below" ||
1395 s == "delete-hline-above" || s == "delete-hline-below" ||
1396 s == "append-column" || s == "delete-column" ||
1397 s == "copy-column" || s == "swap-column" ||
1398 s == "add-vline-left" || s == "add-vline-right" ||
1399 s == "delete-vline-left" || s == "delete-vline-right")
1400 status.enabled(true);
1402 status.enabled(false);
1403 status.message(bformat(
1404 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1407 status.setOnOff((s == "align-left" && halign(cur.col()) == 'l')
1408 || (s == "align-right" && halign(cur.col()) == 'r')
1409 || (s == "align-center" && halign(cur.col()) == 'c')
1410 || (s == "valign-top" && valign() == 't')
1411 || (s == "valign-bottom" && valign() == 'b')
1412 || (s == "valign-middle" && valign() == 'm'));
1415 // FIXME: What did this code do?
1416 // Please check whether it is still needed!
1417 // should be more precise
1418 if (v_align_ == '\0') {
1419 status.enable(true);
1422 if (cmd.argument().empty()) {
1423 status.enable(false);
1426 if (!support::contains("tcb", cmd.argument()[0])) {
1427 status.enable(false);
1430 status.setOnOff(cmd.argument()[0] == v_align_);
1431 status.enabled(true);
1436 case LFUN_CELL_SPLIT:
1437 status.enabled(true);
1440 case LFUN_CELL_BACKWARD:
1441 case LFUN_CELL_FORWARD:
1442 status.enabled(true);
1446 return InsetMathNest::getStatus(cur, cmd, status);