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;
259 //lyxerr << "\nnrows: " << nrows() << " h: " << h << '\n';
260 for (row_type row = 0; row <= nrows(); ++row) {
261 rowinfo_[row].offset_ -= h;
262 //lyxerr << "row: " << row << " off: " << rowinfo_[row].offset_ << '\n';
266 // adjust horizontal structure
267 colinfo_[0].offset_ = BORDER;
268 for (col_type col = 0; col < ncols(); ++col) {
270 for (row_type row = 0; row < nrows(); ++row)
271 wid = std::max(wid, xcell(index(row, col)).width());
272 colinfo_[col].width_ = wid;
273 colinfo_[col].offset_ += VLINESEP * colinfo_[col].lines_;
275 colinfo_[col + 1].offset_ =
276 colinfo_[col].offset_ +
277 colinfo_[col].width_ +
278 colinfo_[col].skip_ +
281 colinfo_[ncols()].width_ = 0;
282 colinfo_[ncols()].offset_ += VLINESEP * colinfo_[ncols()].lines_;
285 width_ = colinfo_[ncols() - 1].offset_
286 + colinfo_[ncols() - 1].width_
287 + VLINESEP * colinfo_[ncols()].lines_
290 ascent_ = - rowinfo_[0].offset_
291 + rowinfo_[0].ascent_
292 + HLINESEP * rowinfo_[0].lines_
295 descent_ = rowinfo_[nrows() - 1].offset_
296 + rowinfo_[nrows() - 1].descent_
297 + HLINESEP * rowinfo_[nrows()].lines_
302 // Increase ws_[i] for 'R' columns (except the first one)
303 for (int i = 1; i < nc_; ++i)
304 if (align_[i] == 'R')
305 ws_[i] += 10 * df_width;
306 // Increase ws_[i] for 'C' column
307 if (align_[0] == 'C')
308 if (ws_[0] < 7 * workwidth / 8)
309 ws_[0] = 7 * workwidth / 8;
313 for (cxrow = row_.begin(); cxrow; ++cxrow) {
316 for (int i = 0; i < nc_; ++i) {
318 if (cxrow->getTab(i) <= 0) {
319 cxrow->setTab(i, df_width);
327 lf = (ws_[i] - cxrow->getTab(i))/2;
331 lf = ws_[i] - cxrow->getTab(i);
334 if (cxrow == row_.begin())
336 else if (cxrow.is_last())
337 lf = ws_[i] - cxrow->getTab(i);
339 lf = (ws_[i] - cxrow->getTab(i))/2;
342 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
343 cxrow->setTab(i, lf + rg);
344 rg = ws_[i] - ww + COLSEP;
345 if (cxrow == row_.begin())
346 width += ws_[i] + COLSEP;
348 cxrow->setBaseline(cxrow->getBaseline() - ascent);
354 void MathGridInset::draw(Painter & pain, int x, int y) const
356 for (idx_type idx = 0; idx < nargs(); ++idx)
357 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
359 for (row_type row = 0; row <= nrows(); ++row)
360 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
361 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
362 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
363 //lyxerr << "i: " << i << " yy: " << yy << '\n';
364 pain.line(x + 1, yy, x + width_ - 1, yy);
367 for (col_type col = 0; col <= ncols(); ++col)
368 for (int i = 0; i < colinfo_[col].lines_; ++i) {
369 int xx = x + colinfo_[col].offset_
370 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
371 //lyxerr << "i: " << i << " xx: " << xx << '\n';
372 pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
377 string MathGridInset::eolString(row_type row) const
381 if (rowinfo_[row].crskip_.value() != 0)
382 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
384 // make sure an upcoming '[' does not break anything
385 if (row + 1 < nrows()) {
386 MathArray const & c = cell(index(row + 1, 0));
387 if (c.size() && c.front()->getChar() == '[')
391 // only add \\ if necessary
392 if (eol.empty() && row + 1 == nrows())
395 return "\\\\" + eol + '\n';
399 string MathGridInset::eocString(col_type col) const
401 if (col + 1 == ncols())
407 void MathGridInset::addRow(row_type row)
409 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
410 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
414 void MathGridInset::appendRow()
416 rowinfo_.push_back(RowInfo());
417 //cells_.insert(cells_.end(), ncols(), MathXArray());
418 for (col_type col = 0; col < ncols(); ++col)
419 cells_.push_back(cells_type::value_type());
423 void MathGridInset::delRow(row_type row)
428 cells_type::iterator it = cells_.begin() + row * ncols();
429 cells_.erase(it, it + ncols());
431 rowinfo_.erase(rowinfo_.begin() + row);
435 void MathGridInset::addCol(col_type newcol)
437 const col_type nc = ncols();
438 const row_type nr = nrows();
439 cells_type new_cells((nc + 1) * nr);
441 for (row_type row = 0; row < nr; ++row)
442 for (col_type col = 0; col < nc; ++col)
443 new_cells[row * (nc + 1) + col + (col > newcol)]
444 = cells_[row * nc + col];
445 std::swap(cells_, new_cells);
448 inf.skip_ = defaultColSpace(newcol);
449 inf.align_ = defaultColAlign(newcol);
450 colinfo_.insert(colinfo_.begin() + newcol, inf);
454 void MathGridInset::delCol(col_type col)
460 for (col_type i = 0; i < nargs(); ++i)
461 if (i % ncols() != col)
462 tmpcells.push_back(cells_[i]);
463 std::swap(cells_, tmpcells);
465 colinfo_.erase(colinfo_.begin() + col);
469 int MathGridInset::cellXOffset(idx_type idx) const
471 col_type c = col(idx);
472 int x = colinfo_[c].offset_;
473 char align = colinfo_[c].align_;
474 if (align == 'r' || align == 'R')
475 x += colinfo_[c].width_ - xcell(idx).width();
476 if (align == 'c' || align == 'C')
477 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
482 int MathGridInset::cellYOffset(idx_type idx) const
484 return rowinfo_[row(idx)].offset_;
488 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
492 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
494 pos = xcell(idx).x2pos(x - cellXOffset(idx));
499 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
501 if (idx >= ncols() * (nrows() - 1))
503 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
505 pos = xcell(idx).x2pos(x - cellXOffset(idx));
510 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
512 // leave matrix if on the left hand edge
516 pos = cell(idx).size();
521 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
523 // leave matrix if on the right hand edge
524 if (col(idx) + 1 == ncols())
532 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
539 idx = (nrows() - 1) * ncols();
542 idx = (nrows() / 2) * ncols();
549 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
559 idx = (nrows() / 2 + 1) * ncols() - 1;
561 pos = cell(idx).size();
566 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
573 idx -= idx % ncols();
586 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
588 if (pos < cell(idx).size()) {
589 pos = cell(idx).size();
592 if (col(idx) < ncols() - 1) {
593 idx = idx - idx % ncols() + ncols() - 1;
594 pos = cell(idx).size();
597 if (idx < nargs() - 1) {
599 pos = cell(idx).size();
606 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
611 // nothing to do if we are in the last row of the inset
612 if (row(idx) + 1 == nrows())
615 // try to delete entire sequence of ncols() empty cells if possible
616 for (idx_type i = idx; i < idx + ncols(); ++i)
620 // move cells if necessary
621 for (idx_type i = index(row(idx), 0); i < idx; ++i)
622 cell(i).swap(cell(i + ncols()));
629 // undo effect of Ctrl-Tab (i.e. pull next cell)
630 //if (idx + 1 != nargs())
631 // cell(idx).swap(cell(idx + 1));
635 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
637 // leave this unimplemented unless someone wants to have it.
639 int n = (to - from) / ncols();
640 int r = from / ncols();
643 cells_type::iterator it = cells_.begin() + from;
644 cells_.erase(it, it + n * ncols());
645 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
651 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
653 return rowinfo_[row];
657 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
659 return rowinfo_[row];
663 std::vector<MathInset::idx_type>
664 MathGridInset::idxBetween(idx_type from, idx_type to) const
666 row_type r1 = std::min(row(from), row(to));
667 row_type r2 = std::max(row(from), row(to));
668 col_type c1 = std::min(col(from), col(to));
669 col_type c2 = std::max(col(from), col(to));
670 std::vector<idx_type> res;
671 for (row_type i = r1; i <= r2; ++i)
672 for (col_type j = c1; j <= c2; ++j)
673 res.push_back(index(i, j));
679 void MathGridInset::normalize(NormalStream & os) const
682 for (row_type row = 0; row < nrows(); ++row) {
684 for (col_type col = 0; col < ncols(); ++col)
685 os << "[cell " << cell(index(row, col)) << ']';
692 void MathGridInset::mathmlize(MathMLStream & os) const
694 os << MTag("mtable");
695 for (row_type row = 0; row < nrows(); ++row) {
697 for (col_type col = 0; col < ncols(); ++col)
698 os << cell(index(row, col));
701 os << ETag("mtable");
705 void MathGridInset::write(WriteStream & os) const
707 for (row_type row = 0; row < nrows(); ++row) {
708 os << verboseHLine(rowinfo_[row].lines_).c_str();
709 for (col_type col = 0; col < ncols(); ++col)
710 os << cell(index(row, col)) << eocString(col).c_str();
711 os << eolString(row).c_str();
713 string s = verboseHLine(rowinfo_[nrows()].lines_);
715 os << "\\\\" << s.c_str();