A bit more re-organization.
[lyx.git] / src / mathed / InsetMathGrid.cpp
blob46148a662888e050995abf2d684520a15b9fa4b4
1 /**
2 * \file InsetMathGrid.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
12 #include <algorithm>
14 #include "InsetMathGrid.h"
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
23 #include "FuncStatus.h"
24 #include "Cursor.h"
25 #include "FuncRequest.h"
27 #include "frontends/Clipboard.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
36 #include "support/lassert.h"
38 #include <sstream>
40 using namespace std;
41 using namespace lyx::support;
44 namespace lyx {
46 static docstring verboseHLine(int n)
48 docstring res;
49 for (int i = 0; i < n; ++i)
50 res += "\\hline";
51 if (n)
52 res += ' ';
53 return res;
57 static int extractInt(istream & is)
59 int num = 1;
60 is >> num;
61 return (num == 0) ? 1 : num;
65 static void resetGrid(InsetMathGrid & grid)
67 while (grid.ncols() > 1)
68 grid.delCol(grid.ncols());
69 while (grid.nrows() > 1)
70 grid.delRow(grid.nrows());
71 grid.cell(0).erase(0, grid.cell(0).size());
72 grid.setDefaults();
77 //////////////////////////////////////////////////////////////
80 InsetMathGrid::CellInfo::CellInfo()
81 : dummy_(false)
86 //////////////////////////////////////////////////////////////
89 InsetMathGrid::RowInfo::RowInfo()
90 : lines_(0), skip_(0), allow_newpage_(true)
95 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
97 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
98 return crskip_.inPixels(mi.base.textwidth,
99 fm.width(char_type('M')));
104 //////////////////////////////////////////////////////////////
107 InsetMathGrid::ColInfo::ColInfo()
108 : align_('c'), lines_(0)
112 //////////////////////////////////////////////////////////////
115 InsetMathGrid::InsetMathGrid(Buffer * buf)
116 : InsetMathNest(buf, 1),
117 rowinfo_(1 + 1),
118 colinfo_(1 + 1),
119 cellinfo_(1),
120 v_align_('c')
122 setDefaults();
126 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
127 : InsetMathNest(buf, m * n),
128 rowinfo_(n + 1),
129 colinfo_(m + 1),
130 cellinfo_(m * n),
131 v_align_('c')
133 setDefaults();
137 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
138 docstring const & h)
139 : InsetMathNest(buf, m * n),
140 rowinfo_(n + 1),
141 colinfo_(m + 1),
142 cellinfo_(m * n),
143 v_align_(v)
145 setDefaults();
146 setVerticalAlignment(v);
147 setHorizontalAlignments(h);
151 Inset * InsetMathGrid::clone() const
153 return new InsetMathGrid(*this);
157 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
159 return col + ncols() * row;
163 void InsetMathGrid::setDefaults()
165 if (ncols() <= 0)
166 lyxerr << "positive number of columns expected" << endl;
167 //if (nrows() <= 0)
168 // lyxerr << "positive number of rows expected" << endl;
169 for (col_type col = 0; col < ncols(); ++col) {
170 colinfo_[col].align_ = defaultColAlign(col);
171 colinfo_[col].skip_ = defaultColSpace(col);
172 colinfo_[col].special_.clear();
177 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
179 col_type col = 0;
180 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
181 char_type c = *it;
182 if (c == '|') {
183 colinfo_[col].lines_++;
184 } else if ((c == 'p' || c == 'm' || c == 'b'||
185 c == '!' || c == '@' || c == '>' || c == '<') &&
186 it + 1 != hh.end() && *(it + 1) == '{') {
187 // @{decl.} and p{width} are standard LaTeX, the
188 // others are extensions by array.sty
189 bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
190 if (newcolumn) {
191 // this declares a new column
192 if (col >= ncols())
193 // Only intercolumn stuff is allowed
194 // in the last dummy column
195 break;
196 colinfo_[col].align_ = 'l';
197 } else {
198 // this is intercolumn stuff
199 if (colinfo_[col].special_.empty())
200 // Overtake possible lines
201 colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
203 int brace_open = 0;
204 int brace_close = 0;
205 while (it != hh.end()) {
206 c = *it;
207 colinfo_[col].special_ += c;
208 if (c == '{')
209 ++brace_open;
210 else if (c == '}')
211 ++brace_close;
212 ++it;
213 if (brace_open > 0 && brace_open == brace_close)
214 break;
216 --it;
217 if (newcolumn) {
218 colinfo_[col].lines_ = count(
219 colinfo_[col].special_.begin(),
220 colinfo_[col].special_.end(), '|');
221 LYXERR(Debug::MATHED, "special column separator: `"
222 << to_utf8(colinfo_[col].special_) << '\'');
223 ++col;
224 colinfo_[col].lines_ = 0;
225 colinfo_[col].special_.clear();
227 } else if (col >= ncols()) {
228 // Only intercolumn stuff is allowed in the last
229 // dummy column
230 break;
231 } else if (c == 'c' || c == 'l' || c == 'r') {
232 colinfo_[col].align_ = static_cast<char>(c);
233 if (!colinfo_[col].special_.empty()) {
234 colinfo_[col].special_ += c;
235 colinfo_[col].lines_ = count(
236 colinfo_[col].special_.begin(),
237 colinfo_[col].special_.end(), '|');
238 LYXERR(Debug::MATHED, "special column separator: `"
239 << to_utf8(colinfo_[col].special_) << '\'');
241 ++col;
242 colinfo_[col].lines_ = 0;
243 colinfo_[col].special_.clear();
244 } else {
245 lyxerr << "unknown column separator: '" << c << "'" << endl;
250 col_type n = hh.size();
251 if (n > ncols())
252 n = ncols();
253 for (col_type col = 0; col < n; ++col)
254 colinfo_[col].align_ = hh[col];
259 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
261 col_type col = 0;
262 for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
263 if (*it == 'c' || *it == 'l' || *it == 'r'||
264 *it == 'p' || *it == 'm' || *it == 'b')
265 ++col;
266 // let's have at least one column, even if we did not recognize its
267 // alignment
268 if (col == 0)
269 col = 1;
270 return col;
274 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
276 colinfo_[col].align_ = h;
277 if (!colinfo_[col].special_.empty()) {
278 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
279 if (c == 'l' || c == 'c' || c == 'r')
280 c = h;
282 // FIXME: Change alignment of p, m and b columns, too
286 char InsetMathGrid::horizontalAlignment(col_type col) const
288 return colinfo_[col].align_;
292 docstring InsetMathGrid::horizontalAlignments() const
294 docstring res;
295 for (col_type col = 0; col < ncols(); ++col) {
296 if (colinfo_[col].special_.empty()) {
297 res += docstring(colinfo_[col].lines_, '|');
298 res += colinfo_[col].align_;
299 } else
300 res += colinfo_[col].special_;
302 if (colinfo_[ncols()].special_.empty())
303 return res + docstring(colinfo_[ncols()].lines_, '|');
304 return res + colinfo_[ncols()].special_;
308 void InsetMathGrid::setVerticalAlignment(char c)
310 v_align_ = c;
314 char InsetMathGrid::verticalAlignment() const
316 return v_align_;
320 InsetMathGrid::col_type InsetMathGrid::ncols() const
322 return colinfo_.size() - 1;
326 InsetMathGrid::row_type InsetMathGrid::nrows() const
328 return rowinfo_.size() - 1;
332 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
334 return idx % ncols();
338 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
340 return idx / ncols();
344 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
346 rowinfo_[row].crskip_ = crskip;
350 Length InsetMathGrid::vcrskip(row_type row) const
352 return rowinfo_[row].crskip_;
356 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
358 // let the cells adjust themselves
359 InsetMathNest::metrics(mi);
361 BufferView & bv = *mi.base.bv;
363 // compute absolute sizes of vertical structure
364 for (row_type row = 0; row < nrows(); ++row) {
365 int asc = 0;
366 int desc = 0;
367 for (col_type col = 0; col < ncols(); ++col) {
368 Dimension const & dimc = cell(index(row, col)).dimension(bv);
369 asc = max(asc, dimc.asc);
370 desc = max(desc, dimc.des);
372 rowinfo_[row].ascent_ = asc;
373 rowinfo_[row].descent_ = desc;
375 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
376 rowinfo_[nrows()].ascent_ = 0;
377 rowinfo_[nrows()].descent_ = 0;
379 // compute vertical offsets
380 rowinfo_[0].offset_ = 0;
381 for (row_type row = 1; row <= nrows(); ++row) {
382 rowinfo_[row].offset_ =
383 rowinfo_[row - 1].offset_ +
384 rowinfo_[row - 1].descent_ +
385 rowinfo_[row - 1].skipPixels(mi) +
386 rowsep() +
387 rowinfo_[row].lines_ * hlinesep() +
388 rowinfo_[row].ascent_;
391 // adjust vertical offset
392 int h = 0;
393 switch (v_align_) {
394 case 't':
395 h = 0;
396 break;
397 case 'b':
398 h = rowinfo_[nrows() - 1].offset_;
399 break;
400 default:
401 h = rowinfo_[nrows() - 1].offset_ / 2;
403 for (row_type row = 0; row <= nrows(); ++row)
404 rowinfo_[row].offset_ -= h;
407 // compute absolute sizes of horizontal structure
408 for (col_type col = 0; col < ncols(); ++col) {
409 int wid = 0;
410 for (row_type row = 0; row < nrows(); ++row)
411 wid = max(wid, cell(index(row, col)).dimension(bv).wid);
412 colinfo_[col].width_ = wid;
414 colinfo_[ncols()].width_ = 0;
416 // compute horizontal offsets
417 colinfo_[0].offset_ = border();
418 for (col_type col = 1; col <= ncols(); ++col) {
419 colinfo_[col].offset_ =
420 colinfo_[col - 1].offset_ +
421 colinfo_[col - 1].width_ +
422 colinfo_[col - 1].skip_ +
423 colsep() +
424 colinfo_[col].lines_ * vlinesep();
428 dim.wid = colinfo_[ncols() - 1].offset_
429 + colinfo_[ncols() - 1].width_
430 + vlinesep() * colinfo_[ncols()].lines_
431 + border();
433 dim.asc = - rowinfo_[0].offset_
434 + rowinfo_[0].ascent_
435 + hlinesep() * rowinfo_[0].lines_
436 + border();
438 dim.des = rowinfo_[nrows() - 1].offset_
439 + rowinfo_[nrows() - 1].descent_
440 + hlinesep() * rowinfo_[nrows()].lines_
441 + border();
445 // Increase ws_[i] for 'R' columns (except the first one)
446 for (int i = 1; i < nc_; ++i)
447 if (align_[i] == 'R')
448 ws_[i] += 10 * df_width;
449 // Increase ws_[i] for 'C' column
450 if (align_[0] == 'C')
451 if (ws_[0] < 7 * workwidth / 8)
452 ws_[0] = 7 * workwidth / 8;
454 // Adjust local tabs
455 width = colsep();
456 for (cxrow = row_.begin(); cxrow; ++cxrow) {
457 int rg = COLSEP;
458 int lf = 0;
459 for (int i = 0; i < nc_; ++i) {
460 bool isvoid = false;
461 if (cxrow->getTab(i) <= 0) {
462 cxrow->setTab(i, df_width);
463 isvoid = true;
465 switch (align_[i]) {
466 case 'l':
467 lf = 0;
468 break;
469 case 'c':
470 lf = (ws_[i] - cxrow->getTab(i))/2;
471 break;
472 case 'r':
473 case 'R':
474 lf = ws_[i] - cxrow->getTab(i);
475 break;
476 case 'C':
477 if (cxrow == row_.begin())
478 lf = 0;
479 else if (cxrow.is_last())
480 lf = ws_[i] - cxrow->getTab(i);
481 else
482 lf = (ws_[i] - cxrow->getTab(i))/2;
483 break;
485 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
486 cxrow->setTab(i, lf + rg);
487 rg = ws_[i] - ww + colsep();
488 if (cxrow == row_.begin())
489 width += ws_[i] + colsep();
491 cxrow->setBaseline(cxrow->getBaseline() - ascent);
494 metricsMarkers2(dim);
495 // Cache the inset dimension.
496 setDimCache(mi, dim);
500 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
502 drawWithMargin(pi, x, y, 1, 1);
506 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
507 int lmargin, int rmargin) const
509 Dimension const dim = dimension(*pi.base.bv);
510 BufferView const & bv = *pi.base.bv;
512 for (idx_type idx = 0; idx < nargs(); ++idx)
513 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
514 y + cellYOffset(idx));
516 for (row_type row = 0; row <= nrows(); ++row)
517 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
518 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
519 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
520 pi.pain.line(x + lmargin + 1, yy,
521 x + dim.width() - rmargin - 1, yy,
522 Color_foreground);
525 for (col_type col = 0; col <= ncols(); ++col)
526 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
527 int xx = x + lmargin + colinfo_[col].offset_
528 - i * vlinesep() - vlinesep()/2 - colsep()/2;
529 pi.pain.line(xx, y - dim.ascent() + 1,
530 xx, y + dim.descent() - 1,
531 Color_foreground);
533 drawMarkers2(pi, x, y);
537 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
539 // let the cells adjust themselves
540 //InsetMathNest::metrics(mi);
541 for (idx_type i = 0; i < nargs(); ++i)
542 cell(i).metricsT(mi, dim);
544 // compute absolute sizes of vertical structure
545 for (row_type row = 0; row < nrows(); ++row) {
546 int asc = 0;
547 int desc = 0;
548 for (col_type col = 0; col < ncols(); ++col) {
549 //MathData const & c = cell(index(row, col));
550 // FIXME: BROKEN!
551 Dimension dimc;
552 asc = max(asc, dimc.ascent());
553 desc = max(desc, dimc.descent());
555 rowinfo_[row].ascent_ = asc;
556 rowinfo_[row].descent_ = desc;
558 //rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
559 rowinfo_[nrows()].ascent_ = 0;
560 rowinfo_[nrows()].descent_ = 0;
562 // compute vertical offsets
563 rowinfo_[0].offset_ = 0;
564 for (row_type row = 1; row <= nrows(); ++row) {
565 rowinfo_[row].offset_ =
566 rowinfo_[row - 1].offset_ +
567 rowinfo_[row - 1].descent_ +
568 //rowinfo_[row - 1].skipPixels(mi) +
569 1 + //rowsep() +
570 //rowinfo_[row].lines_ * hlinesep() +
571 rowinfo_[row].ascent_;
574 // adjust vertical offset
575 int h = 0;
576 switch (v_align_) {
577 case 't':
578 h = 0;
579 break;
580 case 'b':
581 h = rowinfo_[nrows() - 1].offset_;
582 break;
583 default:
584 h = rowinfo_[nrows() - 1].offset_ / 2;
586 for (row_type row = 0; row <= nrows(); ++row)
587 rowinfo_[row].offset_ -= h;
590 // compute absolute sizes of horizontal structure
591 for (col_type col = 0; col < ncols(); ++col) {
592 int wid = 0;
593 for (row_type row = 0; row < nrows(); ++row) {
594 // FIXME: BROKEN!
595 //wid = max(wid, cell(index(row, col)).width());
597 colinfo_[col].width_ = wid;
599 colinfo_[ncols()].width_ = 0;
601 // compute horizontal offsets
602 colinfo_[0].offset_ = border();
603 for (col_type col = 1; col <= ncols(); ++col) {
604 colinfo_[col].offset_ =
605 colinfo_[col - 1].offset_ +
606 colinfo_[col - 1].width_ +
607 colinfo_[col - 1].skip_ +
608 1 ; //colsep() +
609 //colinfo_[col].lines_ * vlinesep();
613 dim.wid = colinfo_[ncols() - 1].offset_
614 + colinfo_[ncols() - 1].width_
615 //+ vlinesep() * colinfo_[ncols()].lines_
616 + 2;
618 dim.asc = -rowinfo_[0].offset_
619 + rowinfo_[0].ascent_
620 //+ hlinesep() * rowinfo_[0].lines_
621 + 1;
623 dim.des = rowinfo_[nrows() - 1].offset_
624 + rowinfo_[nrows() - 1].descent_
625 //+ hlinesep() * rowinfo_[nrows()].lines_
626 + 1;
630 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
632 // for (idx_type idx = 0; idx < nargs(); ++idx)
633 // cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
637 docstring InsetMathGrid::eolString(row_type row, bool fragile) const
639 docstring eol;
641 if (!rowinfo_[row].crskip_.zero())
642 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
643 else if(!rowinfo_[row].allow_newpage_)
644 eol += '*';
646 // make sure an upcoming '[' does not break anything
647 if (row + 1 < nrows()) {
648 MathData const & c = cell(index(row + 1, 0));
649 if (c.size() && c.front()->getChar() == '[')
650 //eol += "[0pt]";
651 eol += "{}";
654 // only add \\ if necessary
655 if (eol.empty() && row + 1 == nrows())
656 return docstring();
658 return (fragile ? "\\protect\\\\" : "\\\\") + eol;
662 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
664 if (col + 1 == lastcol)
665 return docstring();
666 return from_ascii(" & ");
670 void InsetMathGrid::addRow(row_type row)
672 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
673 cells_.insert
674 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
675 cellinfo_.insert
676 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
680 void InsetMathGrid::appendRow()
682 rowinfo_.push_back(RowInfo());
683 //cells_.insert(cells_.end(), ncols(), MathData());
684 for (col_type col = 0; col < ncols(); ++col) {
685 cells_.push_back(cells_type::value_type());
686 cellinfo_.push_back(CellInfo());
691 void InsetMathGrid::delRow(row_type row)
693 if (nrows() == 1)
694 return;
696 cells_type::iterator it = cells_.begin() + row * ncols();
697 cells_.erase(it, it + ncols());
699 vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
700 cellinfo_.erase(jt, jt + ncols());
702 rowinfo_.erase(rowinfo_.begin() + row);
706 void InsetMathGrid::copyRow(row_type row)
708 addRow(row);
709 for (col_type col = 0; col < ncols(); ++col)
710 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
714 void InsetMathGrid::swapRow(row_type row)
716 if (nrows() == 1)
717 return;
718 if (row + 1 == nrows())
719 --row;
720 for (col_type col = 0; col < ncols(); ++col)
721 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
725 void InsetMathGrid::addCol(col_type newcol)
727 const col_type nc = ncols();
728 const row_type nr = nrows();
729 cells_type new_cells((nc + 1) * nr);
730 vector<CellInfo> new_cellinfo((nc + 1) * nr);
732 for (row_type row = 0; row < nr; ++row)
733 for (col_type col = 0; col < nc; ++col) {
734 new_cells[row * (nc + 1) + col + (col >= newcol)]
735 = cells_[row * nc + col];
736 new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
737 = cellinfo_[row * nc + col];
739 swap(cells_, new_cells);
740 swap(cellinfo_, new_cellinfo);
742 ColInfo inf;
743 inf.skip_ = defaultColSpace(newcol);
744 inf.align_ = defaultColAlign(newcol);
745 colinfo_.insert(colinfo_.begin() + newcol, inf);
749 void InsetMathGrid::delCol(col_type col)
751 if (ncols() == 1)
752 return;
754 cells_type tmpcells;
755 vector<CellInfo> tmpcellinfo;
756 for (col_type i = 0; i < nargs(); ++i)
757 if (i % ncols() != col) {
758 tmpcells.push_back(cells_[i]);
759 tmpcellinfo.push_back(cellinfo_[i]);
761 swap(cells_, tmpcells);
762 swap(cellinfo_, tmpcellinfo);
764 colinfo_.erase(colinfo_.begin() + col);
768 void InsetMathGrid::copyCol(col_type col)
770 addCol(col+1);
771 for (row_type row = 0; row < nrows(); ++row)
772 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
776 void InsetMathGrid::swapCol(col_type col)
778 if (ncols() == 1)
779 return;
780 if (col + 1 == ncols())
781 --col;
782 for (row_type row = 0; row < nrows(); ++row)
783 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
787 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
789 col_type c = col(idx);
790 int x = colinfo_[c].offset_;
791 char align = colinfo_[c].align_;
792 Dimension const & celldim = cell(idx).dimension(bv);
793 if (align == 'r' || align == 'R')
794 x += colinfo_[c].width_ - celldim.wid;
795 if (align == 'c' || align == 'C')
796 x += (colinfo_[c].width_ - celldim.wid) / 2;
797 return x;
801 int InsetMathGrid::cellYOffset(idx_type idx) const
803 return rowinfo_[row(idx)].offset_;
807 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
809 if (up) {
810 if (cur.row() == 0)
811 return false;
812 cur.idx() -= ncols();
813 } else {
814 if (cur.row() + 1 >= nrows())
815 return false;
816 cur.idx() += ncols();
818 cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
819 return true;
823 bool InsetMathGrid::idxBackward(Cursor & cur) const
825 // leave matrix if at the front edge
826 if (cur.col() == 0)
827 return false;
828 --cur.idx();
829 cur.pos() = cur.lastpos();
830 return true;
834 bool InsetMathGrid::idxForward(Cursor & cur) const
836 // leave matrix if at the back edge
837 if (cur.col() + 1 == ncols())
838 return false;
839 ++cur.idx();
840 cur.pos() = 0;
841 return true;
845 bool InsetMathGrid::idxFirst(Cursor & cur) const
847 switch (v_align_) {
848 case 't':
849 cur.idx() = 0;
850 break;
851 case 'b':
852 cur.idx() = (nrows() - 1) * ncols();
853 break;
854 default:
855 cur.idx() = ((nrows() - 1) / 2) * ncols();
857 cur.pos() = 0;
858 return true;
862 bool InsetMathGrid::idxLast(Cursor & cur) const
864 switch (v_align_) {
865 case 't':
866 cur.idx() = ncols() - 1;
867 break;
868 case 'b':
869 cur.idx() = nargs() - 1;
870 break;
871 default:
872 cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
874 cur.pos() = cur.lastpos();
875 return true;
879 bool InsetMathGrid::idxDelete(idx_type & idx)
881 // nothing to do if we have just one row
882 if (nrows() == 1)
883 return false;
885 // nothing to do if we are in the middle of the last row of the inset
886 if (idx + ncols() > nargs())
887 return false;
889 // try to delete entire sequence of ncols() empty cells if possible
890 for (idx_type i = idx; i < idx + ncols(); ++i)
891 if (cell(i).size())
892 return false;
894 // move cells if necessary
895 for (idx_type i = index(row(idx), 0); i < idx; ++i)
896 swap(cell(i), cell(i + ncols()));
898 delRow(row(idx));
900 if (idx >= nargs())
901 idx = nargs() - 1;
903 // undo effect of Ctrl-Tab (i.e. pull next cell)
904 //if (idx + 1 != nargs())
905 // cell(idx).swap(cell(idx + 1));
907 // we handled the event..
908 return true;
912 // reimplement old behaviour when pressing Delete in the last position
913 // of a cell
914 void InsetMathGrid::idxGlue(idx_type idx)
916 col_type c = col(idx);
917 if (c + 1 == ncols()) {
918 if (row(idx) + 1 != nrows()) {
919 for (col_type cc = 0; cc < ncols(); ++cc)
920 cell(idx).append(cell(idx + cc + 1));
921 delRow(row(idx) + 1);
923 } else {
924 cell(idx).append(cell(idx + 1));
925 for (col_type cc = c + 2; cc < ncols(); ++cc)
926 cell(idx - c + cc - 1) = cell(idx - c + cc);
927 cell(idx - c + ncols() - 1).clear();
932 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
934 return rowinfo_[row];
938 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
940 return rowinfo_[row];
944 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
946 row_type const ri = row(idx);
947 row_type const r1 = min(row(from), row(to));
948 row_type const r2 = max(row(from), row(to));
949 col_type const ci = col(idx);
950 col_type const c1 = min(col(from), col(to));
951 col_type const c2 = max(col(from), col(to));
952 return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
957 void InsetMathGrid::normalize(NormalStream & os) const
959 os << "[grid ";
960 for (row_type row = 0; row < nrows(); ++row) {
961 os << "[row ";
962 for (col_type col = 0; col < ncols(); ++col)
963 os << "[cell " << cell(index(row, col)) << ']';
964 os << ']';
966 os << ']';
970 void InsetMathGrid::mathmlize(MathStream & os) const
972 os << MTag("mtable");
973 for (row_type row = 0; row < nrows(); ++row) {
974 os << MTag("mtr");
975 for (col_type col = 0; col < ncols(); ++col)
976 os << cell(index(row, col));
977 os << ETag("mtr");
979 os << ETag("mtable");
983 void InsetMathGrid::write(WriteStream & os) const
985 write(os, 0, 0, nrows(), ncols());
988 void InsetMathGrid::write(WriteStream & os,
989 row_type beg_row, col_type beg_col,
990 row_type end_row, col_type end_col) const
992 MathEnsurer ensurer(os, false);
993 docstring eol;
994 for (row_type row = beg_row; row < end_row; ++row) {
995 os << verboseHLine(rowinfo_[row].lines_);
996 // don't write & and empty cells at end of line
997 col_type lastcol = 0;
998 bool emptyline = true;
999 for (col_type col = beg_col; col < end_col; ++col)
1000 if (!cell(index(row, col)).empty()) {
1001 lastcol = col + 1;
1002 emptyline = false;
1004 for (col_type col = beg_col; col < lastcol; ++col) {
1005 os << cell(index(row, col));
1006 if (os.pendingBrace())
1007 ModeSpecifier specifier(os, TEXT_MODE);
1008 os << eocString(col, lastcol);
1010 eol = eolString(row, os.fragile());
1011 os << eol;
1012 // append newline only if line wasn't completely empty
1013 // and the formula is not written on a single line
1014 bool const empty = emptyline && eol.empty();
1015 if (!empty && nrows() > 1)
1016 os << "\n";
1018 // @TODO use end_row instead of nrows() ?
1019 docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1020 if (!s.empty()) {
1021 if (eol.empty()) {
1022 if (os.fragile())
1023 os << "\\protect";
1024 os << "\\\\";
1026 os << s;
1031 int InsetMathGrid::colsep() const
1033 return 6;
1037 int InsetMathGrid::rowsep() const
1039 return 6;
1043 int InsetMathGrid::hlinesep() const
1045 return 3;
1049 int InsetMathGrid::vlinesep() const
1051 return 3;
1055 int InsetMathGrid::border() const
1057 return 1;
1061 void InsetMathGrid::splitCell(Cursor & cur)
1063 if (cur.idx() == cur.lastidx())
1064 return;
1065 MathData ar = cur.cell();
1066 ar.erase(0, cur.pos());
1067 cur.cell().erase(cur.pos(), cur.lastpos());
1068 ++cur.idx();
1069 cur.pos() = 0;
1070 cur.cell().insert(0, ar);
1074 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1076 //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1078 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1080 switch (cmd.action) {
1082 // insert file functions
1083 case LFUN_LINE_DELETE:
1084 cur.recordUndoInset();
1085 //autocorrect_ = false;
1086 //macroModeClose();
1087 //if (selection_) {
1088 // selDel();
1089 // break;
1091 if (nrows() > 1)
1092 delRow(cur.row());
1093 if (cur.idx() > cur.lastidx())
1094 cur.idx() = cur.lastidx();
1095 if (cur.pos() > cur.lastpos())
1096 cur.pos() = cur.lastpos();
1097 break;
1099 case LFUN_CELL_SPLIT:
1100 cur.recordUndo();
1101 splitCell(cur);
1102 break;
1104 case LFUN_CELL_BACKWARD:
1105 // See below.
1106 cur.setSelection(false);
1107 if (!idxPrev(cur)) {
1108 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1109 cur.undispatched();
1111 break;
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.setSelection(false);
1117 if (!idxNext(cur)) {
1118 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1119 cur.undispatched();
1121 break;
1123 case LFUN_NEWLINE_INSERT: {
1124 cur.recordUndoInset();
1125 row_type const r = cur.row();
1126 addRow(r);
1128 // split line
1129 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1130 swap(cell(index(r, c)), cell(index(r + 1, c)));
1132 // split cell
1133 splitCell(cur);
1134 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1135 if (cur.idx() > 0)
1136 --cur.idx();
1137 cur.pos() = cur.lastpos();
1139 //mathcursor->normalize();
1140 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1141 break;
1144 case LFUN_TABULAR_FEATURE: {
1145 cur.recordUndoInset();
1146 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1147 istringstream is(to_utf8(cmd.argument()));
1148 string s;
1149 is >> s;
1150 if (s == "valign-top")
1151 setVerticalAlignment('t');
1152 else if (s == "valign-middle")
1153 setVerticalAlignment('c');
1154 else if (s == "valign-bottom")
1155 setVerticalAlignment('b');
1156 else if (s == "align-left")
1157 setHorizontalAlignment('l', cur.col());
1158 else if (s == "align-right")
1159 setHorizontalAlignment('r', cur.col());
1160 else if (s == "align-center")
1161 setHorizontalAlignment('c', cur.col());
1162 else if (s == "append-row")
1163 for (int i = 0, n = extractInt(is); i < n; ++i)
1164 addRow(cur.row());
1165 else if (s == "delete-row") {
1166 cur.clearSelection(); // bug 4323
1167 for (int i = 0, n = extractInt(is); i < n; ++i) {
1168 delRow(cur.row());
1169 if (cur.idx() >= nargs())
1170 cur.idx() -= ncols();
1172 cur.pos() = 0; // trick, see below
1174 else if (s == "copy-row") {
1175 // Here (as later) we save the cursor col/row
1176 // in order to restore it after operation.
1177 row_type const r = cur.row();
1178 col_type const c = cur.col();
1179 for (int i = 0, n = extractInt(is); i < n; ++i)
1180 copyRow(cur.row());
1181 cur.idx() = index(r, c);
1183 else if (s == "swap-row") {
1184 swapRow(cur.row());
1185 // Trick to suppress same-idx-means-different-cell
1186 // assertion crash:
1187 cur.pos() = 0;
1189 else if (s == "add-hline-above")
1190 rowinfo_[cur.row()].lines_++;
1191 else if (s == "add-hline-below")
1192 rowinfo_[cur.row()+1].lines_++;
1193 else if (s == "delete-hline-above")
1194 rowinfo_[cur.row()].lines_--;
1195 else if (s == "delete-hline-below")
1196 rowinfo_[cur.row()+1].lines_--;
1197 else if (s == "append-column") {
1198 row_type const r = cur.row();
1199 col_type const c = cur.col();
1200 for (int i = 0, n = extractInt(is); i < n; ++i)
1201 addCol(cur.col() + 1);
1202 cur.idx() = index(r, c);
1204 else if (s == "delete-column") {
1205 cur.clearSelection(); // bug 4323
1206 row_type const r = cur.row();
1207 col_type const c = cur.col();
1208 for (int i = 0, n = extractInt(is); i < n; ++i)
1209 delCol(col(cur.idx()));
1210 cur.idx() = index(r, min(c, cur.ncols() - 1));
1211 cur.pos() = 0; // trick, see above
1213 else if (s == "copy-column") {
1214 row_type const r = cur.row();
1215 col_type const c = cur.col();
1216 copyCol(cur.col());
1217 cur.idx() = index(r, c);
1219 else if (s == "swap-column") {
1220 swapCol(cur.col());
1221 cur.pos() = 0; // trick, see above
1223 else if (s == "add-vline-left") {
1224 colinfo_[cur.col()].lines_++;
1225 if (!colinfo_[cur.col()].special_.empty())
1226 colinfo_[cur.col()].special_ += '|';
1228 else if (s == "add-vline-right") {
1229 colinfo_[cur.col()+1].lines_++;
1230 if (!colinfo_[cur.col()+1].special_.empty())
1231 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1233 else if (s == "delete-vline-left") {
1234 colinfo_[cur.col()].lines_--;
1235 docstring & special = colinfo_[cur.col()].special_;
1236 if (!special.empty()) {
1237 docstring::size_type i = special.rfind('|');
1238 LASSERT(i != docstring::npos, /**/);
1239 special.erase(i, 1);
1242 else if (s == "delete-vline-right") {
1243 colinfo_[cur.col()+1].lines_--;
1244 docstring & special = colinfo_[cur.col()+1].special_;
1245 if (!special.empty()) {
1246 docstring::size_type i = special.find('|');
1247 LASSERT(i != docstring::npos, /**/);
1248 special.erase(i, 1);
1251 else {
1252 cur.undispatched();
1253 break;
1255 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1256 //lyxerr << "returning FINISHED_LEFT" << endl;
1257 break;
1260 case LFUN_CLIPBOARD_PASTE:
1261 parseflg |= Parse::VERBATIM;
1262 // fall through
1263 case LFUN_PASTE: {
1264 if (cur.currentMode() <= TEXT_MODE)
1265 parseflg |= Parse::TEXTMODE;
1266 cur.message(_("Paste"));
1267 cap::replaceSelection(cur);
1268 docstring topaste;
1269 if (cmd.argument().empty() && !theClipboard().isInternal())
1270 topaste = theClipboard().getAsText();
1271 else {
1272 idocstringstream is(cmd.argument());
1273 int n = 0;
1274 is >> n;
1275 topaste = cap::selection(n);
1277 InsetMathGrid grid(buffer_, 1, 1);
1278 if (!topaste.empty())
1279 if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1280 || !mathed_parse_normal(grid, topaste, parseflg)) {
1281 resetGrid(grid);
1282 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1285 if (grid.nargs() == 1) {
1286 // single cell/part of cell
1287 cur.recordUndo();
1288 cur.cell().insert(cur.pos(), grid.cell(0));
1289 cur.pos() += grid.cell(0).size();
1290 } else {
1291 // multiple cells
1292 cur.recordUndoInset();
1293 col_type const numcols =
1294 min(grid.ncols(), ncols() - col(cur.idx()));
1295 row_type const numrows =
1296 min(grid.nrows(), nrows() - cur.row());
1297 for (row_type r = 0; r < numrows; ++r) {
1298 for (col_type c = 0; c < numcols; ++c) {
1299 idx_type i = index(r + cur.row(), c + col(cur.idx()));
1300 cell(i).insert(0, grid.cell(grid.index(r, c)));
1302 // append the left over horizontal cells to the last column
1303 idx_type i = index(r + cur.row(), ncols() - 1);
1304 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1305 cell(i).append(grid.cell(grid.index(r, c)));
1307 // append the left over vertical cells to the last _cell_
1308 idx_type i = nargs() - 1;
1309 for (row_type r = numrows; r < grid.nrows(); ++r)
1310 for (col_type c = 0; c < grid.ncols(); ++c)
1311 cell(i).append(grid.cell(grid.index(r, c)));
1313 cur.clearSelection(); // bug 393
1314 cur.finishUndo();
1315 break;
1318 case LFUN_LINE_BEGIN_SELECT:
1319 case LFUN_LINE_BEGIN:
1320 case LFUN_WORD_BACKWARD_SELECT:
1321 case LFUN_WORD_BACKWARD:
1322 case LFUN_WORD_LEFT_SELECT:
1323 case LFUN_WORD_LEFT:
1324 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1325 cmd.action == LFUN_WORD_LEFT_SELECT ||
1326 cmd.action == LFUN_LINE_BEGIN_SELECT);
1327 cur.macroModeClose();
1328 if (cur.pos() != 0) {
1329 cur.pos() = 0;
1330 } else if (cur.idx() % cur.ncols() != 0) {
1331 cur.idx() -= cur.idx() % cur.ncols();
1332 cur.pos() = 0;
1333 } else if (cur.idx() != 0) {
1334 cur.idx() = 0;
1335 cur.pos() = 0;
1336 } else {
1337 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1338 cur.undispatched();
1340 break;
1342 case LFUN_WORD_FORWARD_SELECT:
1343 case LFUN_WORD_FORWARD:
1344 case LFUN_WORD_RIGHT_SELECT:
1345 case LFUN_WORD_RIGHT:
1346 case LFUN_LINE_END_SELECT:
1347 case LFUN_LINE_END:
1348 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1349 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1350 cmd.action == LFUN_LINE_END_SELECT);
1351 cur.macroModeClose();
1352 cur.clearTargetX();
1353 if (cur.pos() != cur.lastpos()) {
1354 cur.pos() = cur.lastpos();
1355 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1356 cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1357 cur.pos() = cur.lastpos();
1358 } else if (cur.idx() != cur.lastidx()) {
1359 cur.idx() = cur.lastidx();
1360 cur.pos() = cur.lastpos();
1361 } else {
1362 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1363 cur.undispatched();
1365 break;
1367 default:
1368 InsetMathNest::doDispatch(cur, cmd);
1373 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1374 FuncStatus & status) const
1376 switch (cmd.action) {
1377 case LFUN_TABULAR_FEATURE: {
1378 string const s = cmd.getArg(0);
1379 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1380 status.setEnabled(false);
1381 status.message(from_utf8(N_("Only one row")));
1382 return true;
1384 if (ncols() <= 1 &&
1385 (s == "delete-column" || s == "swap-column")) {
1386 status.setEnabled(false);
1387 status.message(from_utf8(N_("Only one column")));
1388 return true;
1390 if ((rowinfo_[cur.row()].lines_ == 0 &&
1391 s == "delete-hline-above") ||
1392 (rowinfo_[cur.row() + 1].lines_ == 0 &&
1393 s == "delete-hline-below")) {
1394 status.setEnabled(false);
1395 status.message(from_utf8(N_("No hline to delete")));
1396 return true;
1399 if ((colinfo_[cur.col()].lines_ == 0 &&
1400 s == "delete-vline-left") ||
1401 (colinfo_[cur.col() + 1].lines_ == 0 &&
1402 s == "delete-vline-right")) {
1403 status.setEnabled(false);
1404 status.message(from_utf8(N_("No vline to delete")));
1405 return true;
1407 if (s == "valign-top" || s == "valign-middle" ||
1408 s == "valign-bottom" || s == "align-left" ||
1409 s == "align-right" || s == "align-center") {
1410 status.setEnabled(true);
1411 char const ha = horizontalAlignment(cur.col());
1412 char const va = verticalAlignment();
1413 status.setOnOff((s == "align-left" && ha == 'l')
1414 || (s == "align-right" && ha == 'r')
1415 || (s == "align-center" && ha == 'c')
1416 || (s == "valign-top" && va == 't')
1417 || (s == "valign-bottom" && va == 'b')
1418 || (s == "valign-middle" && va == 'c'));
1419 return true;
1421 if (s == "append-row" || s == "delete-row" ||
1422 s == "copy-row" || s == "swap-row" ||
1423 s == "add-hline-above" || s == "add-hline-below" ||
1424 s == "delete-hline-above" || s == "delete-hline-below" ||
1425 s == "append-column" || s == "delete-column" ||
1426 s == "copy-column" || s == "swap-column" ||
1427 s == "add-vline-left" || s == "add-vline-right" ||
1428 s == "delete-vline-left" || s == "delete-vline-right") {
1429 status.setEnabled(true);
1430 } else {
1431 status.setEnabled(false);
1432 status.message(bformat(
1433 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1436 #if 0
1437 // FIXME: What did this code do?
1438 // Please check whether it is still needed!
1439 // should be more precise
1440 if (v_align_ == '\0') {
1441 status.enable(true);
1442 break;
1444 if (cmd.argument().empty()) {
1445 status.enable(false);
1446 break;
1448 if (!contains("tcb", cmd.argument()[0])) {
1449 status.enable(false);
1450 break;
1452 status.setOnOff(cmd.argument()[0] == v_align_);
1453 status.setEnabled(true);
1454 #endif
1455 return true;
1458 case LFUN_CELL_SPLIT:
1459 status.setEnabled(true);
1460 return true;
1462 case LFUN_CELL_BACKWARD:
1463 case LFUN_CELL_FORWARD:
1464 status.setEnabled(true);
1465 return true;
1467 default:
1468 return InsetMathNest::getStatus(cur, cmd, status);
1473 } // namespace lyx