2 * \file InsetMathGrid.cpp
\r
3 * This file is part of LyX, the document processor.
\r
4 * Licence details can be found in the file COPYING.
\r
6 * \author André Pönitz
\r
8 * Full author contact details are available in file CREDITS.
\r
12 #include <algorithm>
\r
14 #include "InsetMathGrid.h"
\r
16 #include "InsetMathUnknown.h"
\r
17 #include "MathData.h"
\r
18 #include "MathParser.h"
\r
19 #include "MathStream.h"
\r
20 #include "MetricsInfo.h"
\r
23 #include "BufferParams.h"
\r
24 #include "BufferView.h"
\r
25 #include "CutAndPaste.h"
\r
26 #include "FuncStatus.h"
\r
28 #include "FuncRequest.h"
\r
30 #include "frontends/Clipboard.h"
\r
31 #include "frontends/Painter.h"
\r
33 #include "support/debug.h"
\r
34 #include "support/docstream.h"
\r
35 #include "support/gettext.h"
\r
36 #include "support/lstrings.h"
\r
38 #include "support/lassert.h"
\r
42 using namespace std;
\r
43 using namespace lyx::support;
\r
49 static docstring verboseHLine(int n)
\r
52 for (int i = 0; i < n; ++i)
\r
60 static int extractInt(istream & is)
\r
64 return (num == 0) ? 1 : num;
\r
68 static void resetGrid(InsetMathGrid & grid)
\r
70 while (grid.ncols() > 1)
\r
71 grid.delCol(grid.ncols() - 1);
\r
72 while (grid.nrows() > 1)
\r
73 grid.delRow(grid.nrows() - 1);
\r
74 grid.cell(0).erase(0, grid.cell(0).size());
\r
80 //////////////////////////////////////////////////////////////
\r
83 InsetMathGrid::CellInfo::CellInfo()
\r
84 : multi_(CELL_NORMAL), glue_(0), begin_(0), end_(0)
\r
89 //////////////////////////////////////////////////////////////
\r
92 InsetMathGrid::RowInfo::RowInfo()
\r
93 : descent_(0), ascent_(0), offset_(0), lines_(0), skip_(0),
\r
94 allow_newpage_(true)
\r
99 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
\r
101 return crskip_.inPixels(mi.base);
\r
106 //////////////////////////////////////////////////////////////
\r
109 InsetMathGrid::ColInfo::ColInfo()
\r
110 : align_('c'), width_(0), offset_(0), lines_(0), skip_(0)
\r
114 //////////////////////////////////////////////////////////////
\r
117 InsetMathGrid::InsetMathGrid(Buffer * buf)
\r
118 : InsetMathNest(buf, 1),
\r
128 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
\r
129 : InsetMathNest(buf, m * n),
\r
139 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
\r
140 docstring const & h)
\r
141 : InsetMathNest(buf, m * n),
\r
148 setVerticalAlignment(v);
\r
149 setHorizontalAlignments(h);
\r
153 Inset * InsetMathGrid::clone() const
\r
155 return new InsetMathGrid(*this);
\r
159 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
\r
161 return col + ncols() * row;
\r
165 void InsetMathGrid::setDefaults()
\r
168 lyxerr << "positive number of columns expected" << endl;
\r
169 //if (nrows() <= 0)
\r
170 // lyxerr << "positive number of rows expected" << endl;
\r
171 for (col_type col = 0; col < ncols(); ++col) {
\r
172 colinfo_[col].align_ = defaultColAlign(col);
\r
173 colinfo_[col].skip_ = defaultColSpace(col);
\r
174 colinfo_[col].special_.clear();
\r
176 for (idx_type idx = 0; idx < nargs(); ++idx)
\r
177 cellinfo_[idx].multi_ = CELL_NORMAL;
\r
181 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)
\r
183 if (str == "\\hline") {
\r
184 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
\r
186 if (getStatus(cur, fr, status)) {
\r
187 if (status.enabled()) {
\r
188 rowinfo_[cur.row()].lines_++;
\r
193 return InsetMathNest::interpretString(cur, str);
\r
197 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
\r
200 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
\r
203 colinfo_[col].lines_++;
\r
204 } else if ((c == 'p' || c == 'm' || c == 'b'||
\r
205 c == '!' || c == '@' || c == '>' || c == '<') &&
\r
206 it + 1 != hh.end() && *(it + 1) == '{') {
\r
207 // @{decl.} and p{width} are standard LaTeX, the
\r
208 // others are extensions by array.sty
\r
209 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
\r
211 // this declares a new column
\r
212 if (col >= ncols())
\r
213 // Only intercolumn stuff is allowed
\r
214 // in the last dummy column
\r
216 colinfo_[col].align_ = 'l';
\r
218 // this is intercolumn stuff
\r
219 if (colinfo_[col].special_.empty())
\r
220 // Overtake possible lines
\r
221 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
\r
223 int brace_open = 0;
\r
224 int brace_close = 0;
\r
225 while (it != hh.end()) {
\r
227 colinfo_[col].special_ += c;
\r
233 if (brace_open > 0 && brace_open == brace_close)
\r
238 colinfo_[col].lines_ = count(
\r
239 colinfo_[col].special_.begin(),
\r
240 colinfo_[col].special_.end(), '|');
\r
241 LYXERR(Debug::MATHED, "special column separator: `"
\r
242 << to_utf8(colinfo_[col].special_) << '\'');
\r
244 colinfo_[col].lines_ = 0;
\r
245 colinfo_[col].special_.clear();
\r
247 } else if (col >= ncols()) {
\r
248 // Only intercolumn stuff is allowed in the last
\r
251 } else if (c == 'c' || c == 'l' || c == 'r') {
\r
252 colinfo_[col].align_ = static_cast<char>(c);
\r
253 if (!colinfo_[col].special_.empty()) {
\r
254 colinfo_[col].special_ += c;
\r
255 colinfo_[col].lines_ = count(
\r
256 colinfo_[col].special_.begin(),
\r
257 colinfo_[col].special_.end(), '|');
\r
258 LYXERR(Debug::MATHED, "special column separator: `"
\r
259 << to_utf8(colinfo_[col].special_) << '\'');
\r
262 colinfo_[col].lines_ = 0;
\r
263 colinfo_[col].special_.clear();
\r
265 lyxerr << "unknown column separator: '" << c << "'" << endl;
\r
270 col_type n = hh.size();
\r
273 for (col_type col = 0; col < n; ++col)
\r
274 colinfo_[col].align_ = hh[col];
\r
279 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
\r
282 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
\r
283 if (*it == 'c' || *it == 'l' || *it == 'r'||
\r
284 *it == 'p' || *it == 'm' || *it == 'b')
\r
286 // let's have at least one column, even if we did not recognize its
\r
294 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
\r
296 colinfo_[col].align_ = h;
\r
297 if (!colinfo_[col].special_.empty()) {
\r
298 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
\r
299 if (c == 'l' || c == 'c' || c == 'r')
\r
302 // FIXME: Change alignment of p, m and b columns, too
\r
306 char InsetMathGrid::horizontalAlignment(col_type col) const
\r
308 return colinfo_[col].align_;
\r
312 docstring InsetMathGrid::horizontalAlignments() const
\r
315 for (col_type col = 0; col < ncols(); ++col) {
\r
316 if (colinfo_[col].special_.empty()) {
\r
317 res += docstring(colinfo_[col].lines_, '|');
\r
318 res += colinfo_[col].align_;
\r
320 res += colinfo_[col].special_;
\r
322 if (colinfo_[ncols()].special_.empty())
\r
323 return res + docstring(colinfo_[ncols()].lines_, '|');
\r
324 return res + colinfo_[ncols()].special_;
\r
328 void InsetMathGrid::setVerticalAlignment(char c)
\r
334 char InsetMathGrid::verticalAlignment() const
\r
340 InsetMathGrid::col_type InsetMathGrid::ncols() const
\r
342 return colinfo_.size() - 1;
\r
346 InsetMathGrid::row_type InsetMathGrid::nrows() const
\r
348 return rowinfo_.size() - 1;
\r
352 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
\r
354 return idx % ncols();
\r
358 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
\r
360 return idx / ncols();
\r
364 InsetMathGrid::col_type InsetMathGrid::ncellcols(idx_type idx) const
\r
367 if (cellinfo_[idx].multi_ == CELL_NORMAL)
\r
369 // If the cell at idx is already CELL_PART_OF_MULTICOLUMN we return
\r
370 // the number of remaining columns, not the ones of the complete
\r
371 // multicolumn cell. This makes it possible to always go to the next
\r
372 // cell with idx + ncellcols(idx) - 1.
\r
373 row_type const r = row(idx);
\r
374 while (idx+cols < nargs() && row(idx+cols) == r &&
\r
375 cellinfo_[idx+cols].multi_ == CELL_PART_OF_MULTICOLUMN)
\r
381 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
\r
383 rowinfo_[row].crskip_ = crskip;
\r
387 Length InsetMathGrid::vcrskip(row_type row) const
\r
389 return rowinfo_[row].crskip_;
\r
393 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
\r
395 // let the cells adjust themselves
\r
396 for (idx_type i = 0; i < nargs(); ++i) {
\r
397 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
399 cell(i).metrics(mi, dimc);
\r
403 BufferView & bv = *mi.base.bv;
\r
405 // compute absolute sizes of vertical structure
\r
406 for (row_type row = 0; row < nrows(); ++row) {
\r
409 for (col_type col = 0; col < ncols(); ++col) {
\r
410 idx_type const i = index(row, col);
\r
411 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
412 Dimension const & dimc = cell(i).dimension(bv);
\r
413 asc = max(asc, dimc.asc);
\r
414 desc = max(desc, dimc.des);
\r
417 rowinfo_[row].ascent_ = asc;
\r
418 rowinfo_[row].descent_ = desc;
\r
420 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
\r
421 rowinfo_[nrows()].ascent_ = 0;
\r
422 rowinfo_[nrows()].descent_ = 0;
\r
424 // compute vertical offsets
\r
425 rowinfo_[0].offset_ = 0;
\r
426 for (row_type row = 1; row <= nrows(); ++row) {
\r
427 rowinfo_[row].offset_ =
\r
428 rowinfo_[row - 1].offset_ +
\r
429 rowinfo_[row - 1].descent_ +
\r
430 rowinfo_[row - 1].skipPixels(mi) +
\r
432 rowinfo_[row].lines_ * hlinesep() +
\r
433 rowinfo_[row].ascent_;
\r
436 // adjust vertical offset
\r
438 switch (v_align_) {
\r
443 h = rowinfo_[nrows() - 1].offset_;
\r
446 h = rowinfo_[nrows() - 1].offset_ / 2;
\r
448 for (row_type row = 0; row <= nrows(); ++row)
\r
449 rowinfo_[row].offset_ -= h;
\r
452 // multicolumn cell widths, as a map from first column to width in a
\r
453 // vector of last columns.
\r
454 // This is only used if the grid has more than one row, since for
\r
455 // one-row grids multicolumn cells do not need special handling
\r
456 vector<map<col_type, int> > mcolwidths(ncols());
\r
458 // compute absolute sizes of horizontal structure
\r
459 for (col_type col = 0; col < ncols(); ++col) {
\r
461 for (row_type row = 0; row < nrows(); ++row) {
\r
462 idx_type const i = index(row, col);
\r
463 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
464 int const w = cell(i).dimension(bv).wid;
\r
465 col_type const cols = ncellcols(i);
\r
466 if (cols > 1 && nrows() > 1) {
\r
467 col_type last = col+cols-1;
\r
468 LASSERT(last < ncols(), last = ncols()-1);
\r
469 map<col_type, int>::iterator it =
\r
470 mcolwidths[last].find(col);
\r
471 if (it == mcolwidths[last].end())
\r
472 mcolwidths[last][col] = w;
\r
474 it->second = max(it->second, w);
\r
479 colinfo_[col].width_ = wid;
\r
481 colinfo_[ncols()].width_ = 0;
\r
483 // compute horizontal offsets
\r
484 colinfo_[0].offset_ = border();
\r
485 for (col_type col = 1; col <= ncols(); ++col) {
\r
486 colinfo_[col].offset_ =
\r
487 colinfo_[col - 1].offset_ +
\r
488 colinfo_[col - 1].width_ +
\r
489 displayColSpace(col - 1) +
\r
491 colinfo_[col].lines_ * vlinesep();
\r
494 // increase column widths for multicolumn cells if needed
\r
495 // FIXME: multicolumn lines are not yet considered
\r
496 for (col_type last = 0; last < ncols(); ++last) {
\r
497 map<col_type, int> const & widths = mcolwidths[last];
\r
498 // We increase the width of the last column of the multicol
\r
499 // cell (some sort of left alignment). Since we iterate through
\r
500 // the last and the first columns from left to right, we ensure
\r
501 // that increased widths of previous columns are correctly
\r
502 // taken into account for later columns, thus preventing
\r
503 // unneeded width increasing.
\r
504 for (map<col_type, int>::const_iterator it = widths.begin();
\r
505 it != widths.end(); ++it) {
\r
506 int const wid = it->second;
\r
507 col_type const first = it->first;
\r
508 int const nextoffset =
\r
509 colinfo_[first].offset_ +
\r
511 displayColSpace(last) +
\r
513 colinfo_[last+1].lines_ * vlinesep();
\r
514 int const dx = nextoffset - colinfo_[last+1].offset_;
\r
516 colinfo_[last].width_ += dx;
\r
517 for (col_type col = last + 1; col <= ncols(); ++col)
\r
518 colinfo_[col].offset_ += dx;
\r
524 dim.wid = colinfo_[ncols() - 1].offset_
\r
525 + colinfo_[ncols() - 1].width_
\r
526 + vlinesep() * colinfo_[ncols()].lines_
\r
529 dim.asc = - rowinfo_[0].offset_
\r
530 + rowinfo_[0].ascent_
\r
531 + hlinesep() * rowinfo_[0].lines_
\r
534 dim.des = rowinfo_[nrows() - 1].offset_
\r
535 + rowinfo_[nrows() - 1].descent_
\r
536 + hlinesep() * rowinfo_[nrows()].lines_
\r
541 // Increase ws_[i] for 'R' columns (except the first one)
\r
542 for (int i = 1; i < nc_; ++i)
\r
543 if (align_[i] == 'R')
\r
544 ws_[i] += 10 * df_width;
\r
545 // Increase ws_[i] for 'C' column
\r
546 if (align_[0] == 'C')
\r
547 if (ws_[0] < 7 * workwidth / 8)
\r
548 ws_[0] = 7 * workwidth / 8;
\r
550 // Adjust local tabs
\r
552 for (cxrow = row_.begin(); cxrow; ++cxrow) {
\r
555 for (int i = 0; i < nc_; ++i) {
\r
556 bool isvoid = false;
\r
557 if (cxrow->getTab(i) <= 0) {
\r
558 cxrow->setTab(i, df_width);
\r
561 switch (align_[i]) {
\r
566 lf = (ws_[i] - cxrow->getTab(i))/2;
\r
570 lf = ws_[i] - cxrow->getTab(i);
\r
573 if (cxrow == row_.begin())
\r
575 else if (cxrow.is_last())
\r
576 lf = ws_[i] - cxrow->getTab(i);
\r
578 lf = (ws_[i] - cxrow->getTab(i))/2;
\r
581 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
\r
582 cxrow->setTab(i, lf + rg);
\r
583 rg = ws_[i] - ww + colsep();
\r
584 if (cxrow == row_.begin())
\r
585 width += ws_[i] + colsep();
\r
587 cxrow->setBaseline(cxrow->getBaseline() - ascent);
\r
590 metricsMarkers2(dim);
\r
591 // Cache the inset dimension.
\r
592 setDimCache(mi, dim);
\r
596 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
\r
598 drawWithMargin(pi, x, y, 1, 1);
\r
602 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
\r
603 int lmargin, int rmargin) const
\r
605 Dimension const dim = dimension(*pi.base.bv);
\r
606 BufferView const & bv = *pi.base.bv;
\r
608 for (idx_type idx = 0; idx < nargs(); ++idx) {
\r
609 if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
611 x + lmargin + cellXOffset(bv, idx),
\r
612 y + cellYOffset(idx));
\r
614 // draw inner lines cell by cell because of possible multicolumns
\r
615 // FIXME: multicolumn lines are not yet considered
\r
616 row_type const r = row(idx);
\r
617 col_type const c = col(idx);
\r
618 if (r > 0 && r < nrows()) {
\r
619 for (unsigned int i = 0; i < rowinfo_[r].lines_; ++i) {
\r
620 int yy = y + rowinfo_[r].offset_
\r
621 - rowinfo_[r].ascent_
\r
623 - hlinesep()/2 - rowsep()/2;
\r
625 x + lmargin + colinfo_[c].offset_,
\r
627 x + lmargin + colinfo_[c+1].offset_,
\r
628 yy, Color_foreground);
\r
631 if (c > 0 && c < ncols()) {
\r
632 for (unsigned int i = 0; i < colinfo_[c].lines_; ++i) {
\r
633 int xx = x + lmargin
\r
634 + colinfo_[c].offset_
\r
636 - vlinesep()/2 - colsep()/2;
\r
638 rowinfo_[r].offset_ - rowinfo_[r].ascent_,
\r
640 rowinfo_[r].offset_ + rowinfo_[r].descent_,
\r
647 // draw outer lines in one go
\r
648 for (row_type row = 0; row <= nrows(); row += nrows())
\r
649 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
\r
650 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
\r
651 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
\r
652 pi.pain.line(x + lmargin + 1, yy,
\r
653 x + dim.width() - rmargin - 1, yy,
\r
657 for (col_type col = 0; col <= ncols(); col += ncols())
\r
658 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
\r
659 int xx = x + lmargin + colinfo_[col].offset_
\r
660 - i * vlinesep() - vlinesep()/2 - colsep()/2;
\r
661 pi.pain.line(xx, y - dim.ascent() + 1,
\r
662 xx, y + dim.descent() - 1,
\r
665 drawMarkers2(pi, x, y);
\r
669 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
\r
671 // let the cells adjust themselves
\r
672 for (idx_type i = 0; i < nargs(); ++i)
\r
673 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN)
\r
674 cell(i).metricsT(mi, dim);
\r
676 // compute absolute sizes of vertical structure
\r
677 for (row_type row = 0; row < nrows(); ++row) {
\r
680 for (col_type col = 0; col < ncols(); ++col) {
\r
681 idx_type const i = index(row, col);
\r
682 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
683 //MathData const & c = cell(i);
\r
686 asc = max(asc, dimc.ascent());
\r
687 desc = max(desc, dimc.descent());
\r
690 rowinfo_[row].ascent_ = asc;
\r
691 rowinfo_[row].descent_ = desc;
\r
693 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
\r
694 rowinfo_[nrows()].ascent_ = 0;
\r
695 rowinfo_[nrows()].descent_ = 0;
\r
697 // compute vertical offsets
\r
698 rowinfo_[0].offset_ = 0;
\r
699 for (row_type row = 1; row <= nrows(); ++row) {
\r
700 rowinfo_[row].offset_ =
\r
701 rowinfo_[row - 1].offset_ +
\r
702 rowinfo_[row - 1].descent_ +
\r
703 //rowinfo_[row - 1].skipPixels(mi) +
\r
705 //rowinfo_[row].lines_ * hlinesep() +
\r
706 rowinfo_[row].ascent_;
\r
709 // adjust vertical offset
\r
711 switch (v_align_) {
\r
716 h = rowinfo_[nrows() - 1].offset_;
\r
719 h = rowinfo_[nrows() - 1].offset_ / 2;
\r
721 for (row_type row = 0; row <= nrows(); ++row)
\r
722 rowinfo_[row].offset_ -= h;
\r
725 // compute absolute sizes of horizontal structure
\r
726 for (col_type col = 0; col < ncols(); ++col) {
\r
728 for (row_type row = 0; row < nrows(); ++row) {
\r
730 //idx_type const i = index(row, col);
\r
731 //if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN)
\r
732 // wid = max(wid, cell(i).width());
\r
734 colinfo_[col].width_ = wid;
\r
736 colinfo_[ncols()].width_ = 0;
\r
738 // compute horizontal offsets
\r
739 colinfo_[0].offset_ = border();
\r
740 for (col_type col = 1; col <= ncols(); ++col) {
\r
741 colinfo_[col].offset_ =
\r
742 colinfo_[col - 1].offset_ +
\r
743 colinfo_[col - 1].width_ +
\r
744 displayColSpace(col - 1) +
\r
746 //colinfo_[col].lines_ * vlinesep();
\r
750 dim.wid = colinfo_[ncols() - 1].offset_
\r
751 + colinfo_[ncols() - 1].width_
\r
752 //+ vlinesep() * colinfo_[ncols()].lines_
\r
755 dim.asc = -rowinfo_[0].offset_
\r
756 + rowinfo_[0].ascent_
\r
757 //+ hlinesep() * rowinfo_[0].lines_
\r
760 dim.des = rowinfo_[nrows() - 1].offset_
\r
761 + rowinfo_[nrows() - 1].descent_
\r
762 //+ hlinesep() * rowinfo_[nrows()].lines_
\r
767 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
\r
769 // for (idx_type idx = 0; idx < nargs(); ++idx)
\r
770 // if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN)
\r
771 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
\r
775 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
\r
778 for (idx_type idx = 0; idx < nargs(); ++idx)
\r
779 if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN)
\r
780 cell(idx).updateBuffer(it, utype);
\r
784 docstring InsetMathGrid::eolString(row_type row, bool fragile,
\r
785 bool /*latex*/, bool last_eoln) const
\r
789 if (!rowinfo_[row].crskip_.zero())
\r
790 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
\r
791 else if(!rowinfo_[row].allow_newpage_)
\r
794 // make sure an upcoming '[' does not break anything
\r
795 if (row + 1 < nrows()) {
\r
796 MathData const & c = cell(index(row + 1, 0));
\r
797 if (!c.empty() && c.front()->getChar() == '[')
\r
802 // only add \\ if necessary
\r
803 if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
\r
804 return docstring();
\r
806 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
\r
810 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
\r
812 if (col + 1 == lastcol)
\r
813 return docstring();
\r
814 return from_ascii(" & ");
\r
818 void InsetMathGrid::addRow(row_type row)
\r
820 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
\r
822 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
\r
824 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
\r
828 void InsetMathGrid::delRow(row_type row)
\r
833 cells_type::iterator it = cells_.begin() + row * ncols();
\r
834 cells_.erase(it, it + ncols());
\r
836 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
\r
837 cellinfo_.erase(jt, jt + ncols());
\r
839 rowinfo_.erase(rowinfo_.begin() + row);
\r
843 void InsetMathGrid::copyRow(row_type row)
\r
846 for (col_type col = 0; col < ncols(); ++col)
\r
847 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
\r
851 void InsetMathGrid::swapRow(row_type row)
\r
855 if (row + 1 == nrows())
\r
857 for (col_type col = 0; col < ncols(); ++col)
\r
858 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
\r
862 void InsetMathGrid::addCol(col_type newcol)
\r
864 const col_type nc = ncols();
\r
865 const row_type nr = nrows();
\r
866 cells_type new_cells((nc + 1) * nr);
\r
867 vector<CellInfo> new_cellinfo((nc + 1) * nr);
\r
869 for (row_type row = 0; row < nr; ++row)
\r
870 for (col_type col = 0; col < nc; ++col) {
\r
871 new_cells[row * (nc + 1) + col + (col >= newcol)]
\r
872 = cells_[row * nc + col];
\r
873 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
\r
874 = cellinfo_[row * nc + col];
\r
876 swap(cells_, new_cells);
\r
877 swap(cellinfo_, new_cellinfo);
\r
880 inf.skip_ = defaultColSpace(newcol);
\r
881 inf.align_ = defaultColAlign(newcol);
\r
882 colinfo_.insert(colinfo_.begin() + newcol, inf);
\r
886 void InsetMathGrid::delCol(col_type col)
\r
891 cells_type tmpcells;
\r
892 vector<CellInfo> tmpcellinfo;
\r
893 for (col_type i = 0; i < nargs(); ++i)
\r
894 if (i % ncols() != col) {
\r
895 tmpcells.push_back(cells_[i]);
\r
896 tmpcellinfo.push_back(cellinfo_[i]);
\r
898 swap(cells_, tmpcells);
\r
899 swap(cellinfo_, tmpcellinfo);
\r
901 colinfo_.erase(colinfo_.begin() + col);
\r
905 void InsetMathGrid::copyCol(col_type col)
\r
908 for (row_type row = 0; row < nrows(); ++row)
\r
909 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
\r
913 void InsetMathGrid::swapCol(col_type col)
\r
917 if (col + 1 == ncols())
\r
919 for (row_type row = 0; row < nrows(); ++row)
\r
920 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
\r
924 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
\r
926 if (cellinfo_[idx].multi_ == CELL_PART_OF_MULTICOLUMN)
\r
928 col_type c = col(idx);
\r
929 int x = colinfo_[c].offset_;
\r
930 char align = displayColAlign(idx);
\r
931 Dimension const & celldim = cell(idx).dimension(bv);
\r
932 if (align == 'r' || align == 'R')
\r
933 x += cellWidth(idx) - celldim.wid;
\r
934 if (align == 'c' || align == 'C')
\r
935 x += (cellWidth(idx) - celldim.wid) / 2;
\r
940 int InsetMathGrid::cellYOffset(idx_type idx) const
\r
942 return rowinfo_[row(idx)].offset_;
\r
946 int InsetMathGrid::cellWidth(idx_type idx) const
\r
948 switch (cellinfo_[idx].multi_) {
\r
950 return colinfo_[col(idx)].width_;
\r
951 case CELL_BEGIN_OF_MULTICOLUMN: {
\r
952 col_type c1 = col(idx);
\r
953 col_type c2 = c1 + ncellcols(idx);
\r
954 return colinfo_[c2].offset_
\r
955 - colinfo_[c1].offset_
\r
956 - displayColSpace(c2)
\r
958 - colinfo_[c2].lines_ * vlinesep();
\r
960 case CELL_PART_OF_MULTICOLUMN:
\r
967 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
\r
970 if (cur.row() == 0)
\r
972 cur.idx() -= ncols();
\r
974 if (cur.row() + 1 >= nrows())
\r
976 cur.idx() += ncols();
\r
978 // If we are in a multicolumn cell, move to the "real" cell
\r
979 while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {
\r
980 LASSERT(cur.idx() > 0, return false);
\r
983 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
\r
988 bool InsetMathGrid::idxBackward(Cursor & cur) const
\r
990 // leave matrix if at the front edge
\r
991 if (cur.col() == 0)
\r
994 // If we are in a multicolumn cell, move to the "real" cell
\r
995 while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {
\r
996 LASSERT(cur.idx() > 0, return false);
\r
999 cur.pos() = cur.lastpos();
\r
1004 bool InsetMathGrid::idxForward(Cursor & cur) const
\r
1006 // leave matrix if at the back edge
\r
1007 if (cur.col() + 1 == ncols())
\r
1010 // If we are in a multicolumn cell, move to the next cell
\r
1011 while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {
\r
1012 // leave matrix if at the back edge
\r
1013 if (cur.col() + 1 == ncols())
\r
1022 bool InsetMathGrid::idxFirst(Cursor & cur) const
\r
1024 switch (v_align_) {
\r
1029 cur.idx() = (nrows() - 1) * ncols();
\r
1032 cur.idx() = ((nrows() - 1) / 2) * ncols();
\r
1034 // If we are in a multicolumn cell, move to the "real" cell
\r
1035 while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {
\r
1036 LASSERT(cur.idx() > 0, return false);
\r
1044 bool InsetMathGrid::idxLast(Cursor & cur) const
\r
1046 switch (v_align_) {
\r
1048 cur.idx() = ncols() - 1;
\r
1051 cur.idx() = nargs() - 1;
\r
1054 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
\r
1056 // If we are in a multicolumn cell, move to the "real" cell
\r
1057 while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {
\r
1058 LASSERT(cur.idx() > 0, return false);
\r
1061 cur.pos() = cur.lastpos();
\r
1066 bool InsetMathGrid::idxDelete(idx_type & idx)
\r
1068 // nothing to do if we have just one row
\r
1072 // nothing to do if we are in the middle of the last row of the inset
\r
1073 if (idx + ncols() > nargs())
\r
1076 // try to delete entire sequence of ncols() empty cells if possible
\r
1077 for (idx_type i = idx; i < idx + ncols(); ++i)
\r
1078 if (!cell(i).empty())
\r
1081 // move cells if necessary
\r
1082 for (idx_type i = index(row(idx), 0); i < idx; ++i)
\r
1083 swap(cell(i), cell(i + ncols()));
\r
1087 if (idx >= nargs())
\r
1088 idx = nargs() - 1;
\r
1090 // undo effect of Ctrl-Tab (i.e. pull next cell)
\r
1091 //if (idx + 1 != nargs())
\r
1092 // cell(idx).swap(cell(idx + 1));
\r
1094 // we handled the event..
\r
1099 // reimplement old behaviour when pressing Delete in the last position
\r
1101 void InsetMathGrid::idxGlue(idx_type idx)
\r
1103 col_type c = col(idx);
\r
1104 if (c + 1 == ncols()) {
\r
1105 if (row(idx) + 1 != nrows()) {
\r
1106 for (col_type cc = 0; cc < ncols(); ++cc)
\r
1107 cell(idx).append(cell(idx + cc + 1));
\r
1108 delRow(row(idx) + 1);
\r
1111 idx_type idx_next = idx + 1;
\r
1112 while (idx_next < nargs() &&
\r
1113 cellinfo_[idx_next].multi_ == CELL_PART_OF_MULTICOLUMN)
\r
1115 if (idx_next < nargs())
\r
1116 cell(idx).append(cell(idx_next));
\r
1117 col_type oldcol = c + 1;
\r
1118 for (col_type cc = c + 2; cc < ncols(); ++cc)
\r
1119 cell(idx - oldcol + cc) = cell(idx - oldcol + 1 + cc);
\r
1120 cell(idx - c + ncols() - 1).clear();
\r
1125 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
\r
1127 return rowinfo_[row];
\r
1131 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
\r
1133 return rowinfo_[row];
\r
1137 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
\r
1139 row_type const ri = row(idx);
\r
1140 row_type const r1 = min(row(from), row(to));
\r
1141 row_type const r2 = max(row(from), row(to));
\r
1142 col_type const ci = col(idx);
\r
1143 col_type const c1 = min(col(from), col(to));
\r
1144 col_type const c2 = max(col(from), col(to));
\r
1145 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
\r
1149 void InsetMathGrid::normalize(NormalStream & os) const
\r
1152 for (row_type row = 0; row < nrows(); ++row) {
\r
1154 for (col_type col = 0; col < ncols(); ++col) {
\r
1155 idx_type const i = index(row, col);
\r
1156 switch (cellinfo_[i].multi_) {
\r
1158 os << "[cell " << cell(i) << ']';
\r
1160 case CELL_BEGIN_OF_MULTICOLUMN:
\r
1161 os << "[cell colspan="
\r
1162 << static_cast<int>(ncellcols(i)) << ' '
\r
1163 << cell(i) << ']';
\r
1165 case CELL_PART_OF_MULTICOLUMN:
\r
1175 void InsetMathGrid::mathmlize(MathStream & os) const
\r
1177 bool const havetable = nrows() > 1 || ncols() > 1;
\r
1179 os << MTag("mtable");
\r
1180 char const * const celltag = havetable ? "mtd" : "mrow";
\r
1181 for (row_type row = 0; row < nrows(); ++row) {
\r
1183 os << MTag("mtr");
\r
1184 for (col_type col = 0; col < ncols(); ++col) {
\r
1185 idx_type const i = index(row, col);
\r
1186 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
1187 col_type const cellcols = ncellcols(i);
\r
1188 ostringstream attr;
\r
1189 if (havetable && cellcols > 1)
\r
1190 attr << "colspan='" << cellcols << '\'';
\r
1191 os << MTag(celltag, attr.str());
\r
1192 os << cell(index(row, col));
\r
1193 os << ETag(celltag);
\r
1197 os << ETag("mtr");
\r
1200 os << ETag("mtable");
\r
1205 // We need to do something about alignment here.
\r
1206 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
\r
1208 bool const havetable = nrows() > 1 || ncols() > 1;
\r
1210 os << cell(index(0, 0));
\r
1213 os << MTag("table", attrib);
\r
1214 for (row_type row = 0; row < nrows(); ++row) {
\r
1216 for (col_type col = 0; col < ncols(); ++col) {
\r
1217 idx_type const i = index(row, col);
\r
1218 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
\r
1219 col_type const cellcols = ncellcols(i);
\r
1220 ostringstream attr;
\r
1222 attr << "colspan='" << cellcols << '\'';
\r
1223 os << MTag("td", attr.str());
\r
1224 os << cell(index(row, col));
\r
1230 os << ETag("table");
\r
1234 void InsetMathGrid::htmlize(HtmlStream & os) const
\r
1236 htmlize(os, "class='mathtable'");
\r
1240 void InsetMathGrid::write(WriteStream & os) const
\r
1242 write(os, 0, 0, nrows(), ncols());
\r
1245 void InsetMathGrid::write(WriteStream & os,
\r
1246 row_type beg_row, col_type beg_col,
\r
1247 row_type end_row, col_type end_col) const
\r
1249 MathEnsurer ensurer(os, false);
\r
1251 for (row_type row = beg_row; row < end_row; ++row) {
\r
1252 os << verboseHLine(rowinfo_[row].lines_);
\r
1253 // don't write & and empty cells at end of line,
\r
1254 // unless there are vertical lines
\r
1255 col_type lastcol = 0;
\r
1256 bool emptyline = true;
\r
1257 bool last_eoln = true;
\r
1258 for (col_type col = beg_col; col < end_col; ++col) {
\r
1259 idx_type const idx = index(row, col);
\r
1260 bool const empty_cell = cell(idx).empty();
\r
1261 if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL)
\r
1262 last_eoln = false;
\r
1263 if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL ||
\r
1264 colinfo_[col + 1].lines_) {
\r
1265 lastcol = col + 1;
\r
1266 emptyline = false;
\r
1269 for (col_type col = beg_col; col < end_col;) {
\r
1271 idx_type const idx = index(row, col);
\r
1272 TexRow::RowEntry entry = os.texrow().mathEntry(id(),idx);
\r
1273 os.texrow().startMath(id(),idx);
\r
1274 if (col >= lastcol) {
\r
1278 os.pushRowEntry(entry);
\r
1279 if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN) {
\r
1280 size_t s = col + 1;
\r
1281 while (s < ncols() &&
\r
1282 cellinfo_[index(row, s)].multi_ == CELL_PART_OF_MULTICOLUMN)
\r
1285 os << "\\multicolumn{" << nccols
\r
1286 << "}{" << cellinfo_[idx].align_
\r
1290 if (os.pendingBrace())
\r
1291 ModeSpecifier specifier(os, TEXT_MODE);
\r
1292 if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN)
\r
1294 os << eocString(col + nccols - 1, lastcol);
\r
1298 eol = eolString(row, os.fragile(), os.latex(), last_eoln);
\r
1300 // append newline only if line wasn't completely empty
\r
1301 // and the formula is not written on a single line
\r
1302 bool const empty = emptyline && eol.empty();
\r
1303 if (!empty && nrows() > 1)
\r
1306 // @TODO use end_row instead of nrows() ?
\r
1307 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
\r
1309 if (eol.empty()) {
\r
1311 os << "\\protect";
\r
1319 int InsetMathGrid::colsep() const
\r
1325 int InsetMathGrid::rowsep() const
\r
1331 int InsetMathGrid::hlinesep() const
\r
1337 int InsetMathGrid::vlinesep() const
\r
1343 int InsetMathGrid::border() const
\r
1349 void InsetMathGrid::splitCell(Cursor & cur)
\r
1351 if (cur.idx() == cur.lastidx())
\r
1353 MathData ar = cur.cell();
\r
1354 ar.erase(0, cur.pos());
\r
1355 cur.cell().erase(cur.pos(), cur.lastpos());
\r
1357 while (cur.idx() << nargs() &&
\r
1358 cellinfo_[cur.idx()].multi_ == CELL_BEGIN_OF_MULTICOLUMN)
\r
1361 cur.cell().insert(0, ar);
\r
1365 char InsetMathGrid::displayColAlign(idx_type idx) const
\r
1367 if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN) {
\r
1368 // align_ may also contain lines like "||r|", so this is
\r
1369 // not complete, but we catch at least the simple cases.
\r
1370 if (cellinfo_[idx].align_ == "c")
\r
1372 if (cellinfo_[idx].align_ == "l")
\r
1374 if (cellinfo_[idx].align_ == "r")
\r
1377 return colinfo_[col(idx)].align_;
\r
1381 int InsetMathGrid::displayColSpace(col_type col) const
\r
1383 return colinfo_[col].skip_;
\r
1386 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
\r
1388 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
\r
1390 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
\r
1392 FuncCode const act = cmd.action();
\r
1395 // insert file functions
\r
1396 case LFUN_LINE_DELETE_FORWARD:
\r
1397 cur.recordUndoInset();
\r
1398 //autocorrect_ = false;
\r
1399 //macroModeClose();
\r
1400 //if (selection_) {
\r
1405 delRow(cur.row());
\r
1406 if (cur.idx() > cur.lastidx())
\r
1407 cur.idx() = cur.lastidx();
\r
1408 if (cur.pos() > cur.lastpos())
\r
1409 cur.pos() = cur.lastpos();
\r
1412 case LFUN_CELL_SPLIT:
\r
1417 case LFUN_CELL_BACKWARD:
\r
1419 cur.setSelection(false);
\r
1420 if (!idxPrev(cur)) {
\r
1421 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
\r
1422 cur.undispatched();
\r
1426 case LFUN_CELL_FORWARD:
\r
1427 // Can't handle selection by additional 'shift' as this is
\r
1428 // hard bound to LFUN_CELL_BACKWARD
\r
1429 cur.setSelection(false);
\r
1430 if (!idxNext(cur)) {
\r
1431 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
\r
1432 cur.undispatched();
\r
1436 case LFUN_NEWLINE_INSERT: {
\r
1437 cur.recordUndoInset();
\r
1438 row_type const r = cur.row();
\r
1442 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
\r
1443 swap(cell(index(r, c)), cell(index(r + 1, c)));
\r
1448 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
\r
1449 if (cur.idx() > 0)
\r
1451 cur.pos() = cur.lastpos();
\r
1452 cur.forceBufferUpdate();
\r
1453 //mathcursor->normalize();
\r
1454 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
\r
1458 case LFUN_INSET_MODIFY: {
\r
1459 cur.recordUndoInset();
\r
1460 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
\r
1461 istringstream is(to_utf8(cmd.argument()));
\r
1464 if (s != "tabular") {
\r
1465 InsetMathNest::doDispatch(cur, cmd);
\r
1469 if (s == "valign-top")
\r
1470 setVerticalAlignment('t');
\r
1471 else if (s == "valign-middle")
\r
1472 setVerticalAlignment('c');
\r
1473 else if (s == "valign-bottom")
\r
1474 setVerticalAlignment('b');
\r
1475 else if (s == "align-left")
\r
1476 setHorizontalAlignment('l', cur.col());
\r
1477 else if (s == "align-right")
\r
1478 setHorizontalAlignment('r', cur.col());
\r
1479 else if (s == "align-center")
\r
1480 setHorizontalAlignment('c', cur.col());
\r
1481 else if (s == "append-row")
\r
1482 for (int i = 0, n = extractInt(is); i < n; ++i)
\r
1483 addRow(cur.row());
\r
1484 else if (s == "delete-row") {
\r
1485 cur.clearSelection(); // bug 4323
\r
1486 for (int i = 0, n = extractInt(is); i < n; ++i) {
\r
1487 delRow(cur.row());
\r
1488 if (cur.idx() >= nargs())
\r
1489 cur.idx() -= ncols();
\r
1491 cur.pos() = 0; // trick, see below
\r
1493 else if (s == "copy-row") {
\r
1494 // Here (as later) we save the cursor col/row
\r
1495 // in order to restore it after operation.
\r
1496 row_type const r = cur.row();
\r
1497 col_type const c = cur.col();
\r
1498 for (int i = 0, n = extractInt(is); i < n; ++i)
\r
1499 copyRow(cur.row());
\r
1500 cur.idx() = index(r, c);
\r
1502 else if (s == "swap-row") {
\r
1503 swapRow(cur.row());
\r
1504 // Trick to suppress same-idx-means-different-cell
\r
1505 // assertion crash:
\r
1508 else if (s == "add-hline-above")
\r
1509 rowinfo_[cur.row()].lines_++;
\r
1510 else if (s == "add-hline-below")
\r
1511 rowinfo_[cur.row()+1].lines_++;
\r
1512 else if (s == "delete-hline-above")
\r
1513 rowinfo_[cur.row()].lines_--;
\r
1514 else if (s == "delete-hline-below")
\r
1515 rowinfo_[cur.row()+1].lines_--;
\r
1516 else if (s == "append-column") {
\r
1517 row_type const r = cur.row();
\r
1518 col_type const c = cur.col();
\r
1519 for (int i = 0, n = extractInt(is); i < n; ++i)
\r
1520 addCol(cur.col() + 1);
\r
1521 cur.idx() = index(r, c);
\r
1523 else if (s == "delete-column") {
\r
1524 cur.clearSelection(); // bug 4323
\r
1525 row_type const r = cur.row();
\r
1526 col_type const c = cur.col();
\r
1527 for (int i = 0, n = extractInt(is); i < n; ++i)
\r
1528 delCol(col(cur.idx()));
\r
1529 cur.idx() = index(r, min(c, cur.ncols() - 1));
\r
1530 cur.pos() = 0; // trick, see above
\r
1532 else if (s == "copy-column") {
\r
1533 row_type const r = cur.row();
\r
1534 col_type const c = cur.col();
\r
1535 copyCol(cur.col());
\r
1536 cur.idx() = index(r, c);
\r
1538 else if (s == "swap-column") {
\r
1539 swapCol(cur.col());
\r
1540 cur.pos() = 0; // trick, see above
\r
1542 else if (s == "add-vline-left") {
\r
1543 colinfo_[cur.col()].lines_++;
\r
1544 if (!colinfo_[cur.col()].special_.empty())
\r
1545 colinfo_[cur.col()].special_ += '|';
\r
1547 else if (s == "add-vline-right") {
\r
1548 colinfo_[cur.col()+1].lines_++;
\r
1549 if (!colinfo_[cur.col()+1].special_.empty())
\r
1550 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
\r
1552 else if (s == "delete-vline-left") {
\r
1553 colinfo_[cur.col()].lines_--;
\r
1554 docstring & special = colinfo_[cur.col()].special_;
\r
1555 if (!special.empty()) {
\r
1556 docstring::size_type i = special.rfind('|');
\r
1557 LASSERT(i != docstring::npos, break);
\r
1558 special.erase(i, 1);
\r
1561 else if (s == "delete-vline-right") {
\r
1562 colinfo_[cur.col()+1].lines_--;
\r
1563 docstring & special = colinfo_[cur.col()+1].special_;
\r
1564 if (!special.empty()) {
\r
1565 docstring::size_type i = special.find('|');
\r
1566 LASSERT(i != docstring::npos, break);
\r
1567 special.erase(i, 1);
\r
1571 cur.undispatched();
\r
1574 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
\r
1575 //lyxerr << "returning FINISHED_LEFT" << endl;
\r
1579 case LFUN_CLIPBOARD_PASTE:
\r
1580 parseflg |= Parse::VERBATIM;
\r
1582 case LFUN_PASTE: {
\r
1583 if (cur.currentMode() <= TEXT_MODE)
\r
1584 parseflg |= Parse::TEXTMODE;
\r
1585 cur.message(_("Paste"));
\r
1586 cap::replaceSelection(cur);
\r
1587 docstring topaste;
\r
1588 if (cmd.argument().empty() && !theClipboard().isInternal())
\r
1589 topaste = theClipboard().getAsText(Clipboard::PlainTextType);
\r
1591 idocstringstream is(cmd.argument());
\r
1594 topaste = cap::selection(n, buffer().params().documentClassPtr());
\r
1596 InsetMathGrid grid(buffer_, 1, 1);
\r
1597 if (!topaste.empty())
\r
1598 if ((topaste.size() == 1 && isAscii(topaste))
\r
1599 || !mathed_parse_normal(grid, topaste, parseflg)) {
\r
1601 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
\r
1604 bool hline_enabled = false;
\r
1605 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
\r
1606 FuncStatus status;
\r
1607 if (getStatus(cur, fr, status))
\r
1608 hline_enabled = status.enabled();
\r
1609 if (grid.nargs() == 1) {
\r
1610 // single cell/part of cell
\r
1611 cur.recordUndoInset();
\r
1612 cur.cell().insert(cur.pos(), grid.cell(0));
\r
1613 cur.pos() += grid.cell(0).size();
\r
1614 if (hline_enabled)
\r
1615 rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_;
\r
1617 for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) {
\r
1618 cur.cell().insert(0,
\r
1619 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
\r
1625 cur.recordUndoInset();
\r
1626 col_type const numcols =
\r
1627 min(grid.ncols(), ncols() - col(cur.idx()));
\r
1628 row_type const numrows =
\r
1629 min(grid.nrows(), nrows() - cur.row());
\r
1630 for (row_type r = 0; r < numrows; ++r) {
\r
1631 for (col_type c = 0; c < numcols; ++c) {
\r
1632 idx_type i = index(r + cur.row(), c + col(cur.idx()));
\r
1633 cell(i).insert(0, grid.cell(grid.index(r, c)));
\r
1635 if (hline_enabled)
\r
1636 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
\r
1638 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
\r
1639 idx_type i = index(r + cur.row(), 0);
\r
1641 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
\r
1644 // append the left over horizontal cells to the last column
\r
1645 idx_type i = index(r + cur.row(), ncols() - 1);
\r
1646 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
\r
1647 cell(i).append(grid.cell(grid.index(r, c)));
\r
1649 // append the left over vertical cells to the last _cell_
\r
1650 idx_type i = nargs() - 1;
\r
1651 for (row_type r = numrows; r < grid.nrows(); ++r) {
\r
1652 for (col_type c = 0; c < grid.ncols(); ++c)
\r
1653 cell(i).append(grid.cell(grid.index(r, c)));
\r
1654 if (hline_enabled)
\r
1655 rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;
\r
1657 for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {
\r
1659 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
\r
1664 cur.clearSelection(); // bug 393
\r
1665 // FIXME audit setBuffer calls
\r
1666 cur.inset().setBuffer(*buffer_);
\r
1667 cur.forceBufferUpdate();
\r
1672 case LFUN_LINE_BEGIN:
\r
1673 case LFUN_WORD_BACKWARD:
\r
1674 case LFUN_WORD_LEFT:
\r
1675 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
\r
1677 case LFUN_LINE_BEGIN_SELECT:
\r
1678 case LFUN_WORD_BACKWARD_SELECT:
\r
1679 case LFUN_WORD_LEFT_SELECT:
\r
1680 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
\r
1681 act == LFUN_WORD_LEFT_SELECT ||
\r
1682 act == LFUN_LINE_BEGIN_SELECT);
\r
1683 cur.macroModeClose();
\r
1684 if (cur.pos() != 0) {
\r
1686 } else if (cur.idx() % cur.ncols() != 0) {
\r
1687 cur.idx() -= cur.idx() % cur.ncols();
\r
1689 } else if (cur.idx() != 0) {
\r
1693 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
\r
1694 cur.undispatched();
\r
1698 case LFUN_WORD_FORWARD:
\r
1699 case LFUN_WORD_RIGHT:
\r
1700 case LFUN_LINE_END:
\r
1701 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
\r
1703 case LFUN_WORD_FORWARD_SELECT:
\r
1704 case LFUN_WORD_RIGHT_SELECT:
\r
1705 case LFUN_LINE_END_SELECT:
\r
1706 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
\r
1707 act == LFUN_WORD_RIGHT_SELECT ||
\r
1708 act == LFUN_LINE_END_SELECT);
\r
1709 cur.macroModeClose();
\r
1710 cur.clearTargetX();
\r
1711 if (cur.pos() != cur.lastpos()) {
\r
1712 cur.pos() = cur.lastpos();
\r
1713 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
\r
1714 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
\r
1715 cur.pos() = cur.lastpos();
\r
1716 } else if (cur.idx() != cur.lastidx()) {
\r
1717 cur.idx() = cur.lastidx();
\r
1718 cur.pos() = cur.lastpos();
\r
1720 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
\r
1721 cur.undispatched();
\r
1726 InsetMathNest::doDispatch(cur, cmd);
\r
1731 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
\r
1732 FuncStatus & status) const
\r
1734 switch (cmd.action()) {
\r
1735 case LFUN_INSET_MODIFY: {
\r
1736 istringstream is(to_utf8(cmd.argument()));
\r
1739 if (s != "tabular") {
\r
1740 // We only now about table actions here.
\r
1743 if (&cur.inset() != this) {
\r
1744 // Table actions requires that the cursor is _inside_ the
\r
1746 status.setEnabled(false);
\r
1747 status.message(from_utf8(N_("Cursor not in table")));
\r
1751 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
\r
1752 status.setEnabled(false);
\r
1753 status.message(from_utf8(N_("Only one row")));
\r
1756 if (ncols() <= 1 &&
\r
1757 (s == "delete-column" || s == "swap-column")) {
\r
1758 status.setEnabled(false);
\r
1759 status.message(from_utf8(N_("Only one column")));
\r
1762 if ((rowinfo_[cur.row()].lines_ == 0 &&
\r
1763 s == "delete-hline-above") ||
\r
1764 (rowinfo_[cur.row() + 1].lines_ == 0 &&
\r
1765 s == "delete-hline-below")) {
\r
1766 status.setEnabled(false);
\r
1767 status.message(from_utf8(N_("No hline to delete")));
\r
1771 if ((colinfo_[cur.col()].lines_ == 0 &&
\r
1772 s == "delete-vline-left") ||
\r
1773 (colinfo_[cur.col() + 1].lines_ == 0 &&
\r
1774 s == "delete-vline-right")) {
\r
1775 status.setEnabled(false);
\r
1776 status.message(from_utf8(N_("No vline to delete")));
\r
1779 if (s == "valign-top" || s == "valign-middle" ||
\r
1780 s == "valign-bottom" || s == "align-left" ||
\r
1781 s == "align-right" || s == "align-center") {
\r
1782 status.setEnabled(true);
\r
1783 char const ha = horizontalAlignment(cur.col());
\r
1784 char const va = verticalAlignment();
\r
1785 status.setOnOff((s == "align-left" && ha == 'l')
\r
1786 || (s == "align-right" && ha == 'r')
\r
1787 || (s == "align-center" && ha == 'c')
\r
1788 || (s == "valign-top" && va == 't')
\r
1789 || (s == "valign-bottom" && va == 'b')
\r
1790 || (s == "valign-middle" && va == 'c'));
\r
1793 if (s == "append-row" || s == "delete-row" ||
\r
1794 s == "copy-row" || s == "swap-row" ||
\r
1795 s == "add-hline-above" || s == "add-hline-below" ||
\r
1796 s == "delete-hline-above" || s == "delete-hline-below" ||
\r
1797 s == "append-column" || s == "delete-column" ||
\r
1798 s == "copy-column" || s == "swap-column" ||
\r
1799 s == "add-vline-left" || s == "add-vline-right" ||
\r
1800 s == "delete-vline-left" || s == "delete-vline-right") {
\r
1801 status.setEnabled(true);
\r
1803 status.setEnabled(false);
\r
1804 status.message(bformat(
\r
1805 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
\r
1809 // FIXME: What did this code do?
\r
1810 // Please check whether it is still needed!
\r
1811 // should be more precise
\r
1812 if (v_align_ == '\0') {
\r
1813 status.enable(true);
\r
1816 if (cmd.argument().empty()) {
\r
1817 status.enable(false);
\r
1820 if (!contains("tcb", cmd.argument()[0])) {
\r
1821 status.enable(false);
\r
1824 status.setOnOff(cmd.argument()[0] == v_align_);
\r
1825 status.setEnabled(true);
\r
1830 case LFUN_CELL_SPLIT:
\r
1831 status.setEnabled(cur.idx() != cur.lastidx());
\r
1834 case LFUN_CELL_BACKWARD:
\r
1835 case LFUN_CELL_FORWARD:
\r
1836 status.setEnabled(true);
\r
1842 return InsetMathNest::getStatus(cur, cmd, status);
\r
1847 char InsetMathGrid::colAlign(HullType type, col_type col)
\r
1850 case hullEqnArray:
\r
1851 return "rcl"[col % 3];
\r
1853 case hullMultline:
\r
1859 case hullXAlignAt:
\r
1860 case hullXXAlignAt:
\r
1862 return "rl"[col & 1];
\r
1871 int InsetMathGrid::colSpace(HullType type, col_type col)
\r
1873 int alignInterSpace;
\r
1875 case hullEqnArray:
\r
1879 alignInterSpace = 20;
\r
1882 alignInterSpace = 0;
\r
1884 case hullXAlignAt:
\r
1885 alignInterSpace = 40;
\r
1887 case hullXXAlignAt:
\r
1889 alignInterSpace = 60;
\r
1894 return (col % 2) ? alignInterSpace : 0;
\r
1898 } // namespace lyx
\r