5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
7 #include "math_streamstr.h"
15 string verboseHLine(int n)
18 for (int i = 0; i < n; ++i)
26 //////////////////////////////////////////////////////////////
29 MathGridInset::RowInfo::RowInfo()
35 int MathGridInset::RowInfo::skipPixels() const
38 #warning fix this once the interface to LyXLength has improved
40 return int(crskip_.value());
45 //////////////////////////////////////////////////////////////
48 MathGridInset::ColInfo::ColInfo()
49 : align_('c'), leftline_(false), rightline_(false), lines_(0)
53 //////////////////////////////////////////////////////////////
56 MathGridInset::MathGridInset(char v, string const & h)
57 : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
65 MathGridInset::MathGridInset(col_type m, row_type n)
66 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
72 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
73 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
81 MathInset * MathGridInset::clone() const
83 return new MathGridInset(*this);
87 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
89 return col + ncols() * row;
93 void MathGridInset::setDefaults()
96 lyxerr << "positive number of columns expected\n";
98 lyxerr << "positive number of rows expected\n";
99 for (col_type col = 0; col < ncols(); ++col) {
100 colinfo_[col].align_ = defaultColAlign(col);
101 colinfo_[col].skip_ = defaultColSpace(col);
106 void MathGridInset::halign(string const & hh)
109 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
112 colinfo_[col].lines_++;
113 } else if (c == 'c' || c == 'l' || c == 'r') {
114 colinfo_[col].align_ = c;
116 colinfo_[col].lines_ = 0;
118 lyxerr << "unkown column separator: '" << c << "'\n";
123 col_type n = hh.size();
126 for (col_type col = 0; col < n; ++col)
127 colinfo_[col].align_ = hh[col];
132 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
135 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
136 if (*it == 'c' || *it == 'l' || *it == 'r')
142 void MathGridInset::halign(char h, col_type col)
144 colinfo_[col].align_ = h;
148 char MathGridInset::halign(col_type col) const
150 return colinfo_[col].align_;
154 string MathGridInset::halign() const
157 for (col_type col = 0; col < ncols(); ++col) {
158 res += string(colinfo_[col].lines_, '|');
159 res += colinfo_[col].align_;
161 return res + string(colinfo_[ncols()].lines_, '|');
165 void MathGridInset::valign(char c)
171 char MathGridInset::valign() const
177 MathGridInset::col_type MathGridInset::ncols() const
179 return colinfo_.size() - 1;
183 MathGridInset::row_type MathGridInset::nrows() const
185 return rowinfo_.size() - 1;
189 MathGridInset::col_type MathGridInset::col(idx_type idx) const
191 return idx % ncols();
195 MathGridInset::row_type MathGridInset::row(idx_type idx) const
197 return idx / ncols();
201 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
203 rowinfo_[row].crskip_ = crskip;
207 LyXLength MathGridInset::vcrskip(row_type row) const
209 return rowinfo_[row].crskip_;
213 void MathGridInset::metrics(MathMetricsInfo const & mi) const
215 // let the cells adjust themselves
216 MathNestInset::metrics(mi);
218 // compute absolute sizes of vertical structure
219 for (row_type row = 0; row < nrows(); ++row) {
222 for (col_type col = 0; col < ncols(); ++col) {
223 MathXArray const & c = xcell(index(row, col));
224 asc = std::max(asc, c.ascent());
225 desc = std::max(desc, c.descent());
227 rowinfo_[row].ascent_ = asc;
228 rowinfo_[row].descent_ = desc;
230 rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_;
231 rowinfo_[nrows()].ascent_ = 0;
232 rowinfo_[nrows()].descent_ = 0;
234 // compute vertical offsets
235 rowinfo_[0].offset_ = 0;
236 for (row_type row = 1; row <= nrows(); ++row) {
237 rowinfo_[row].offset_ =
238 rowinfo_[row - 1].offset_ +
239 rowinfo_[row - 1].descent_ +
240 rowinfo_[row - 1].skipPixels() +
242 rowinfo_[row].lines_ * hlinesep() +
243 rowinfo_[row].ascent_;
246 // adjust vertical offset
253 h = rowinfo_[nrows() - 1].offset_;
256 h = rowinfo_[nrows() - 1].offset_ / 2;
258 for (row_type row = 0; row <= nrows(); ++row)
259 rowinfo_[row].offset_ -= h;
262 // compute absolute sizes of horizontal structure
263 for (col_type col = 0; col < ncols(); ++col) {
265 for (row_type row = 0; row < nrows(); ++row)
266 wid = std::max(wid, xcell(index(row, col)).width());
267 colinfo_[col].width_ = wid;
269 colinfo_[ncols()].width_ = 0;
271 // compute horizontal offsets
272 colinfo_[0].offset_ = border();
273 for (col_type col = 1; col <= ncols(); ++col) {
274 colinfo_[col].offset_ =
275 colinfo_[col - 1].offset_ +
276 colinfo_[col - 1].width_ +
277 colinfo_[col - 1].skip_ +
279 colinfo_[col].lines_ * vlinesep();
283 width_ = colinfo_[ncols() - 1].offset_
284 + colinfo_[ncols() - 1].width_
285 + vlinesep() * colinfo_[ncols()].lines_
288 ascent_ = - rowinfo_[0].offset_
289 + rowinfo_[0].ascent_
290 + hlinesep() * rowinfo_[0].lines_
293 descent_ = rowinfo_[nrows() - 1].offset_
294 + rowinfo_[nrows() - 1].descent_
295 + hlinesep() * rowinfo_[nrows()].lines_
300 // Increase ws_[i] for 'R' columns (except the first one)
301 for (int i = 1; i < nc_; ++i)
302 if (align_[i] == 'R')
303 ws_[i] += 10 * df_width;
304 // Increase ws_[i] for 'C' column
305 if (align_[0] == 'C')
306 if (ws_[0] < 7 * workwidth / 8)
307 ws_[0] = 7 * workwidth / 8;
311 for (cxrow = row_.begin(); cxrow; ++cxrow) {
314 for (int i = 0; i < nc_; ++i) {
316 if (cxrow->getTab(i) <= 0) {
317 cxrow->setTab(i, df_width);
325 lf = (ws_[i] - cxrow->getTab(i))/2;
329 lf = ws_[i] - cxrow->getTab(i);
332 if (cxrow == row_.begin())
334 else if (cxrow.is_last())
335 lf = ws_[i] - cxrow->getTab(i);
337 lf = (ws_[i] - cxrow->getTab(i))/2;
340 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
341 cxrow->setTab(i, lf + rg);
342 rg = ws_[i] - ww + colsep();
343 if (cxrow == row_.begin())
344 width += ws_[i] + colsep();
346 cxrow->setBaseline(cxrow->getBaseline() - ascent);
352 void MathGridInset::draw(Painter & pain, int x, int y) const
354 for (idx_type idx = 0; idx < nargs(); ++idx)
355 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
357 for (row_type row = 0; row <= nrows(); ++row)
358 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
359 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
360 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
361 pain.line(x + 1, yy, x + width_ - 1, yy);
364 for (col_type col = 0; col <= ncols(); ++col)
365 for (int i = 0; i < colinfo_[col].lines_; ++i) {
366 int xx = x + colinfo_[col].offset_
367 - i * vlinesep() - vlinesep()/2 - colsep()/2;
368 pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
373 string MathGridInset::eolString(row_type row) const
377 if (!rowinfo_[row].crskip_.zero())
378 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
380 // make sure an upcoming '[' does not break anything
381 if (row + 1 < nrows()) {
382 MathArray const & c = cell(index(row + 1, 0));
383 if (c.size() && c.front()->getChar() == '[')
388 // only add \\ if necessary
389 if (eol.empty() && row + 1 == nrows())
392 return "\\\\" + eol + '\n';
396 string MathGridInset::eocString(col_type col) const
398 if (col + 1 == ncols())
404 void MathGridInset::addRow(row_type row)
406 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
407 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
411 void MathGridInset::appendRow()
413 rowinfo_.push_back(RowInfo());
414 //cells_.insert(cells_.end(), ncols(), MathXArray());
415 for (col_type col = 0; col < ncols(); ++col)
416 cells_.push_back(cells_type::value_type());
420 void MathGridInset::delRow(row_type row)
425 cells_type::iterator it = cells_.begin() + row * ncols();
426 cells_.erase(it, it + ncols());
428 rowinfo_.erase(rowinfo_.begin() + row);
432 void MathGridInset::addCol(col_type newcol)
434 const col_type nc = ncols();
435 const row_type nr = nrows();
436 cells_type new_cells((nc + 1) * nr);
438 for (row_type row = 0; row < nr; ++row)
439 for (col_type col = 0; col < nc; ++col)
440 new_cells[row * (nc + 1) + col + (col > newcol)]
441 = cells_[row * nc + col];
442 std::swap(cells_, new_cells);
445 inf.skip_ = defaultColSpace(newcol);
446 inf.align_ = defaultColAlign(newcol);
447 colinfo_.insert(colinfo_.begin() + newcol, inf);
451 void MathGridInset::delCol(col_type col)
457 for (col_type i = 0; i < nargs(); ++i)
458 if (i % ncols() != col)
459 tmpcells.push_back(cells_[i]);
460 std::swap(cells_, tmpcells);
462 colinfo_.erase(colinfo_.begin() + col);
466 int MathGridInset::cellXOffset(idx_type idx) const
468 col_type c = col(idx);
469 int x = colinfo_[c].offset_;
470 char align = colinfo_[c].align_;
471 if (align == 'r' || align == 'R')
472 x += colinfo_[c].width_ - xcell(idx).width();
473 if (align == 'c' || align == 'C')
474 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
479 int MathGridInset::cellYOffset(idx_type idx) const
481 return rowinfo_[row(idx)].offset_;
485 bool MathGridInset::idxUp(idx_type & idx) const
494 bool MathGridInset::idxDown(idx_type & idx) const
496 if (idx >= ncols() * (nrows() - 1))
503 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
505 // leave matrix if on the left hand edge
509 pos = cell(idx).size();
514 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
516 // leave matrix if on the right hand edge
517 if (col(idx) + 1 == ncols())
525 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
532 idx = (nrows() - 1) * ncols();
535 idx = ((nrows() - 1) / 2) * ncols();
542 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
552 idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
554 pos = cell(idx).size();
559 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
566 idx -= idx % ncols();
579 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
581 if (pos < cell(idx).size()) {
582 pos = cell(idx).size();
585 if (col(idx) < ncols() - 1) {
586 idx = idx - idx % ncols() + ncols() - 1;
587 pos = cell(idx).size();
590 if (idx < nargs() - 1) {
592 pos = cell(idx).size();
599 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
604 // nothing to do if we are in the last row of the inset
605 if (row(idx) + 1 == nrows())
608 // try to delete entire sequence of ncols() empty cells if possible
609 for (idx_type i = idx; i < idx + ncols(); ++i)
613 // move cells if necessary
614 for (idx_type i = index(row(idx), 0); i < idx; ++i)
615 cell(i).swap(cell(i + ncols()));
622 // undo effect of Ctrl-Tab (i.e. pull next cell)
623 //if (idx + 1 != nargs())
624 // cell(idx).swap(cell(idx + 1));
628 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
630 return rowinfo_[row];
634 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
636 return rowinfo_[row];
640 std::vector<MathInset::idx_type>
641 MathGridInset::idxBetween(idx_type from, idx_type to) const
643 row_type r1 = std::min(row(from), row(to));
644 row_type r2 = std::max(row(from), row(to));
645 col_type c1 = std::min(col(from), col(to));
646 col_type c2 = std::max(col(from), col(to));
647 std::vector<idx_type> res;
648 for (row_type i = r1; i <= r2; ++i)
649 for (col_type j = c1; j <= c2; ++j)
650 res.push_back(index(i, j));
656 void MathGridInset::normalize(NormalStream & os) const
659 for (row_type row = 0; row < nrows(); ++row) {
661 for (col_type col = 0; col < ncols(); ++col)
662 os << "[cell " << cell(index(row, col)) << ']';
669 void MathGridInset::mathmlize(MathMLStream & os) const
671 os << MTag("mtable");
672 for (row_type row = 0; row < nrows(); ++row) {
674 for (col_type col = 0; col < ncols(); ++col)
675 os << cell(index(row, col));
678 os << ETag("mtable");
682 void MathGridInset::write(WriteStream & os) const
684 for (row_type row = 0; row < nrows(); ++row) {
685 os << verboseHLine(rowinfo_[row].lines_);
686 for (col_type col = 0; col < ncols(); ++col)
687 os << cell(index(row, col)) << eocString(col);
688 os << eolString(row);
690 string const s = verboseHLine(rowinfo_[nrows()].lines_);
691 if (!s.empty() && s != " ")
696 int MathGridInset::colsep() const
702 int MathGridInset::rowsep() const
708 int MathGridInset::hlinesep() const
714 int MathGridInset::vlinesep() const
720 int MathGridInset::border() const