5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
19 int const HLINESEP = 3;
21 int const VLINESEP = 3;
26 string verboseHLine(int n)
29 for (int i = 0; i < n; ++i)
37 //////////////////////////////////////////////////////////////
40 MathGridInset::RowInfo::RowInfo()
46 int MathGridInset::RowInfo::skipPixels() const
49 #warning fix this once the interface to LyXLength has improved
51 return int(crskip_.value());
56 //////////////////////////////////////////////////////////////
59 MathGridInset::ColInfo::ColInfo()
60 : align_('c'), leftline_(false), rightline_(false), lines_(0)
64 //////////////////////////////////////////////////////////////
67 MathGridInset::MathGridInset(char v, string const & h)
68 : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
76 MathGridInset::MathGridInset(col_type m, row_type n)
77 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
83 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
84 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
92 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
94 return col + ncols() * row;
98 void MathGridInset::setDefaults()
101 lyxerr << "positive number of columns expected\n";
103 lyxerr << "positive number of rows expected\n";
104 for (col_type col = 0; col < ncols(); ++col) {
105 colinfo_[col].align_ = defaultColAlign(col);
106 colinfo_[col].skip_ = defaultColSpace(col);
111 void MathGridInset::halign(string const & hh)
114 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
117 colinfo_[col].lines_++;
118 } else if (c == 'c' || c == 'l' || c == 'r') {
119 colinfo_[col].align_ = c;
121 colinfo_[col].lines_ = 0;
123 lyxerr << "unkown column separator: '" << c << "'\n";
128 col_type n = hh.size();
131 for (col_type col = 0; col < n; ++col)
132 colinfo_[col].align_ = hh[col];
137 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
140 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
141 if (*it == 'c' || *it == 'l' || *it == 'r')
147 void MathGridInset::halign(char h, col_type col)
149 colinfo_[col].align_ = h;
153 char MathGridInset::halign(col_type col) const
155 return colinfo_[col].align_;
159 string MathGridInset::halign() const
162 for (col_type col = 0; col < ncols(); ++col) {
163 res += string(colinfo_[col].lines_, '|');
164 res += colinfo_[col].align_;
166 return res + string(colinfo_[ncols()].lines_, '|');
170 void MathGridInset::valign(char c)
176 char MathGridInset::valign() const
182 MathGridInset::col_type MathGridInset::ncols() const
184 return colinfo_.size() - 1;
188 MathGridInset::row_type MathGridInset::nrows() const
190 return rowinfo_.size() - 1;
194 MathGridInset::col_type MathGridInset::col(idx_type idx) const
196 return idx % ncols();
200 MathGridInset::row_type MathGridInset::row(idx_type idx) const
202 return idx / ncols();
206 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
208 rowinfo_[row].crskip_ = crskip;
212 LyXLength MathGridInset::vcrskip(row_type row) const
214 return rowinfo_[row].crskip_;
218 void MathGridInset::metrics(MathMetricsInfo const & mi) const
220 // let the cells adjust themselves
221 MathNestInset::metrics(mi);
223 // adjust vertical structure
224 rowinfo_[0].offset_ = 0;
225 for (row_type row = 0; row < nrows(); ++row) {
228 for (col_type col = 0; col < ncols(); ++col) {
229 MathXArray const & c = xcell(index(row, col));
230 asc = std::max(asc, c.ascent());
231 desc = std::max(desc, c.descent());
233 rowinfo_[row].ascent_ = asc;
234 rowinfo_[row].descent_ = desc;
235 rowinfo_[row].offset_ += asc + HLINESEP * rowinfo_[row].lines_;
237 rowinfo_[row + 1].offset_ =
238 rowinfo_[row].offset_ +
239 rowinfo_[row].descent_ +
240 rowinfo_[row].skipPixels() +
243 rowinfo_[nrows()].ascent_ = 0;
244 rowinfo_[nrows()].descent_ = 0;
245 rowinfo_[nrows()].offset_ += HLINESEP * rowinfo_[nrows()].lines_;
247 // adjust vertical offset
254 h = rowinfo_[nrows()].offset_;
257 h = rowinfo_[nrows()].offset_ / 2;
258 //lyxerr << "\nnrows: " << nrows() << ' ' << ncols() << '\n';
260 for (row_type row = 0; row <= nrows(); ++row)
261 rowinfo_[row].offset_ -= h;
264 // adjust horizontal structure
265 colinfo_[0].offset_ = BORDER;
266 for (col_type col = 0; col < ncols(); ++col) {
268 for (row_type row = 0; row < nrows(); ++row)
269 wid = std::max(wid, xcell(index(row, col)).width());
270 colinfo_[col].width_ = wid;
271 colinfo_[col].offset_ += VLINESEP * colinfo_[col].lines_;
273 colinfo_[col + 1].offset_ =
274 colinfo_[col].offset_ +
275 colinfo_[col].width_ +
276 colinfo_[col].skip_ +
279 colinfo_[ncols()].width_ = 0;
280 colinfo_[ncols()].offset_ += VLINESEP * colinfo_[ncols()].lines_;
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 //lyxerr << "i: " << i << " yy: " << yy << '\n';
362 pain.line(x + 1, yy, x + width_ - 1, yy);
365 for (col_type col = 0; col <= ncols(); ++col)
366 for (int i = 0; i < colinfo_[col].lines_; ++i) {
367 int xx = x + colinfo_[col].offset_
368 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
369 //lyxerr << "i: " << i << " xx: " << xx << '\n';
370 pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
375 string MathGridInset::eolString(row_type row) const
379 if (rowinfo_[row].crskip_.value() != 0)
380 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
382 // make sure an upcoming '[' does not break anything
383 if (row + 1 < nrows()) {
384 MathArray const & c = cell(index(row + 1, 0));
385 if (c.size() && c.front()->getChar() == '[')
389 // only add \\ if necessary
390 if (eol.empty() && row + 1 == nrows())
393 return "\\\\" + eol + '\n';
397 string MathGridInset::eocString(col_type col) const
399 if (col + 1 == ncols())
405 void MathGridInset::addRow(row_type row)
407 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
408 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
412 void MathGridInset::appendRow()
414 rowinfo_.push_back(RowInfo());
415 //cells_.insert(cells_.end(), ncols(), MathXArray());
416 for (col_type col = 0; col < ncols(); ++col)
417 cells_.push_back(cells_type::value_type());
421 void MathGridInset::delRow(row_type row)
426 cells_type::iterator it = cells_.begin() + row * ncols();
427 cells_.erase(it, it + ncols());
429 rowinfo_.erase(rowinfo_.begin() + row);
433 void MathGridInset::addCol(col_type newcol)
435 const col_type nc = ncols();
436 const row_type nr = nrows();
437 cells_type new_cells((nc + 1) * nr);
439 for (row_type row = 0; row < nr; ++row)
440 for (col_type col = 0; col < nc; ++col)
441 new_cells[row * (nc + 1) + col + (col > newcol)]
442 = cells_[row * nc + col];
443 std::swap(cells_, new_cells);
446 inf.skip_ = defaultColSpace(newcol);
447 inf.align_ = defaultColAlign(newcol);
448 colinfo_.insert(colinfo_.begin() + newcol, inf);
452 void MathGridInset::delCol(col_type col)
458 for (col_type i = 0; i < nargs(); ++i)
459 if (i % ncols() != col)
460 tmpcells.push_back(cells_[i]);
461 std::swap(cells_, tmpcells);
463 colinfo_.erase(colinfo_.begin() + col);
467 int MathGridInset::cellXOffset(idx_type idx) const
469 col_type c = col(idx);
470 int x = colinfo_[c].offset_;
471 char align = colinfo_[c].align_;
472 if (align == 'r' || align == 'R')
473 x += colinfo_[c].width_ - xcell(idx).width();
474 if (align == 'c' || align == 'C')
475 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
480 int MathGridInset::cellYOffset(idx_type idx) const
482 return rowinfo_[row(idx)].offset_;
486 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
490 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
492 pos = xcell(idx).x2pos(x - cellXOffset(idx));
497 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
499 if (idx >= ncols() * (nrows() - 1))
501 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
503 pos = xcell(idx).x2pos(x - cellXOffset(idx));
508 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
510 // leave matrix if on the left hand edge
514 pos = cell(idx).size();
519 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
521 // leave matrix if on the right hand edge
522 if (col(idx) + 1 == ncols())
530 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
537 idx = (nrows() - 1) * ncols();
540 idx = (nrows() / 2) * ncols();
547 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
557 idx = (nrows() / 2 + 1) * ncols() - 1;
559 pos = cell(idx).size();
564 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
571 idx -= idx % ncols();
584 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
586 if (pos < cell(idx).size()) {
587 pos = cell(idx).size();
590 if (col(idx) < ncols() - 1) {
591 idx = idx - idx % ncols() + ncols() - 1;
592 pos = cell(idx).size();
595 if (idx < nargs() - 1) {
597 pos = cell(idx).size();
604 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
609 // nothing to do if we are in the last row of the inset
610 if (row(idx) + 1 == nrows())
613 // try to delete entire sequence of ncols() empty cells if possible
614 for (idx_type i = idx; i < idx + ncols(); ++i)
618 // move cells if necessary
619 for (idx_type i = index(row(idx), 0); i < idx; ++i)
620 cell(i).swap(cell(i + ncols()));
627 // undo effect of Ctrl-Tab (i.e. pull next cell)
628 //if (idx + 1 != nargs())
629 // cell(idx).swap(cell(idx + 1));
633 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
635 // leave this unimplemented unless someone wants to have it.
637 int n = (to - from) / ncols();
638 int r = from / ncols();
641 cells_type::iterator it = cells_.begin() + from;
642 cells_.erase(it, it + n * ncols());
643 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
649 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
651 return rowinfo_[row];
655 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
657 return rowinfo_[row];
661 std::vector<MathInset::idx_type>
662 MathGridInset::idxBetween(idx_type from, idx_type to) const
664 row_type r1 = std::min(row(from), row(to));
665 row_type r2 = std::max(row(from), row(to));
666 col_type c1 = std::min(col(from), col(to));
667 col_type c2 = std::max(col(from), col(to));
668 std::vector<idx_type> res;
669 for (row_type i = r1; i <= r2; ++i)
670 for (col_type j = c1; j <= c2; ++j)
671 res.push_back(index(i, j));
677 void MathGridInset::normalize(NormalStream & os) const
680 for (row_type row = 0; row < nrows(); ++row) {
682 for (col_type col = 0; col < ncols(); ++col)
683 os << "[cell " << cell(index(row, col)) << ']';
690 void MathGridInset::mathmlize(MathMLStream & os) const
692 os << MTag("mtable");
693 for (row_type row = 0; row < nrows(); ++row) {
695 for (col_type col = 0; col < ncols(); ++col)
696 os << cell(index(row, col));
699 os << ETag("mtable");
703 void MathGridInset::write(WriteStream & os) const
705 for (row_type row = 0; row < nrows(); ++row) {
706 os << verboseHLine(rowinfo_[row].lines_).c_str();
707 for (col_type col = 0; col < ncols(); ++col)
708 os << cell(index(row, col)) << eocString(col).c_str();
709 os << eolString(row).c_str();
711 string s = verboseHLine(rowinfo_[nrows()].lines_);
713 os << "\\\\" << s.c_str();