5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
7 #include "math_streamstr.h"
20 int const HLINESEP = 3;
22 int const VLINESEP = 3;
27 string verboseHLine(int n)
30 for (int i = 0; i < n; ++i)
38 //////////////////////////////////////////////////////////////
41 MathGridInset::RowInfo::RowInfo()
47 int MathGridInset::RowInfo::skipPixels() const
50 #warning fix this once the interface to LyXLength has improved
52 return int(crskip_.value());
57 //////////////////////////////////////////////////////////////
60 MathGridInset::ColInfo::ColInfo()
61 : align_('c'), leftline_(false), rightline_(false), lines_(0)
65 //////////////////////////////////////////////////////////////
68 MathGridInset::MathGridInset(char v, string const & h)
69 : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
77 MathGridInset::MathGridInset(col_type m, row_type n)
78 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
84 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
85 : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
93 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
95 return col + ncols() * row;
99 void MathGridInset::setDefaults()
102 lyxerr << "positive number of columns expected\n";
104 lyxerr << "positive number of rows expected\n";
105 for (col_type col = 0; col < ncols(); ++col) {
106 colinfo_[col].align_ = defaultColAlign(col);
107 colinfo_[col].skip_ = defaultColSpace(col);
112 void MathGridInset::halign(string const & hh)
115 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
118 colinfo_[col].lines_++;
119 } else if (c == 'c' || c == 'l' || c == 'r') {
120 colinfo_[col].align_ = c;
122 colinfo_[col].lines_ = 0;
124 lyxerr << "unkown column separator: '" << c << "'\n";
129 col_type n = hh.size();
132 for (col_type col = 0; col < n; ++col)
133 colinfo_[col].align_ = hh[col];
138 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
141 for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
142 if (*it == 'c' || *it == 'l' || *it == 'r')
148 void MathGridInset::halign(char h, col_type col)
150 colinfo_[col].align_ = h;
154 char MathGridInset::halign(col_type col) const
156 return colinfo_[col].align_;
160 string MathGridInset::halign() const
163 for (col_type col = 0; col < ncols(); ++col) {
164 res += string(colinfo_[col].lines_, '|');
165 res += colinfo_[col].align_;
167 return res + string(colinfo_[ncols()].lines_, '|');
171 void MathGridInset::valign(char c)
177 char MathGridInset::valign() const
183 MathGridInset::col_type MathGridInset::ncols() const
185 return colinfo_.size() - 1;
189 MathGridInset::row_type MathGridInset::nrows() const
191 return rowinfo_.size() - 1;
195 MathGridInset::col_type MathGridInset::col(idx_type idx) const
197 return idx % ncols();
201 MathGridInset::row_type MathGridInset::row(idx_type idx) const
203 return idx / ncols();
207 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
209 rowinfo_[row].crskip_ = crskip;
213 LyXLength MathGridInset::vcrskip(row_type row) const
215 return rowinfo_[row].crskip_;
219 void MathGridInset::metrics(MathMetricsInfo const & mi) const
221 // let the cells adjust themselves
222 MathNestInset::metrics(mi);
224 // compute absolute sizes of vertical structure
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;
236 rowinfo_[0].ascent_ += HLINESEP * rowinfo_[0].lines_;
237 rowinfo_[nrows()].ascent_ = 0;
238 rowinfo_[nrows()].descent_ = 0;
240 // compute vertical offsets
241 rowinfo_[0].offset_ = 0;
242 for (row_type row = 1; row <= nrows(); ++row) {
243 rowinfo_[row].offset_ =
244 rowinfo_[row - 1].offset_ +
245 rowinfo_[row - 1].descent_ +
246 rowinfo_[row - 1].skipPixels() +
248 rowinfo_[row].lines_ * HLINESEP +
249 rowinfo_[row].ascent_;
252 // adjust vertical offset
259 h = rowinfo_[nrows() - 1].offset_;
262 h = rowinfo_[nrows() - 1].offset_ / 2;
264 for (row_type row = 0; row <= nrows(); ++row)
265 rowinfo_[row].offset_ -= h;
268 // compute absolute sizes of horizontal structure
269 for (col_type col = 0; col < ncols(); ++col) {
271 for (row_type row = 0; row < nrows(); ++row)
272 wid = std::max(wid, xcell(index(row, col)).width());
273 colinfo_[col].width_ = wid;
275 colinfo_[ncols()].width_ = 0;
277 // compute horizontal offsets
278 colinfo_[0].offset_ = BORDER;
279 for (col_type col = 1; col <= ncols(); ++col) {
280 colinfo_[col].offset_ =
281 colinfo_[col - 1].offset_ +
282 colinfo_[col - 1].width_ +
283 colinfo_[col - 1].skip_ +
285 colinfo_[col].lines_ * VLINESEP;
289 width_ = colinfo_[ncols() - 1].offset_
290 + colinfo_[ncols() - 1].width_
291 + VLINESEP * colinfo_[ncols()].lines_
294 ascent_ = - rowinfo_[0].offset_
295 + rowinfo_[0].ascent_
296 + HLINESEP * rowinfo_[0].lines_
299 descent_ = rowinfo_[nrows() - 1].offset_
300 + rowinfo_[nrows() - 1].descent_
301 + HLINESEP * rowinfo_[nrows()].lines_
306 // Increase ws_[i] for 'R' columns (except the first one)
307 for (int i = 1; i < nc_; ++i)
308 if (align_[i] == 'R')
309 ws_[i] += 10 * df_width;
310 // Increase ws_[i] for 'C' column
311 if (align_[0] == 'C')
312 if (ws_[0] < 7 * workwidth / 8)
313 ws_[0] = 7 * workwidth / 8;
317 for (cxrow = row_.begin(); cxrow; ++cxrow) {
320 for (int i = 0; i < nc_; ++i) {
322 if (cxrow->getTab(i) <= 0) {
323 cxrow->setTab(i, df_width);
331 lf = (ws_[i] - cxrow->getTab(i))/2;
335 lf = ws_[i] - cxrow->getTab(i);
338 if (cxrow == row_.begin())
340 else if (cxrow.is_last())
341 lf = ws_[i] - cxrow->getTab(i);
343 lf = (ws_[i] - cxrow->getTab(i))/2;
346 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
347 cxrow->setTab(i, lf + rg);
348 rg = ws_[i] - ww + COLSEP;
349 if (cxrow == row_.begin())
350 width += ws_[i] + COLSEP;
352 cxrow->setBaseline(cxrow->getBaseline() - ascent);
358 void MathGridInset::draw(Painter & pain, int x, int y) const
360 for (idx_type idx = 0; idx < nargs(); ++idx)
361 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
363 for (row_type row = 0; row <= nrows(); ++row)
364 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
365 int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
366 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
367 pain.line(x + 1, yy, x + width_ - 1, yy);
370 for (col_type col = 0; col <= ncols(); ++col)
371 for (int i = 0; i < colinfo_[col].lines_; ++i) {
372 int xx = x + colinfo_[col].offset_
373 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
374 pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
379 string MathGridInset::eolString(row_type row) const
383 if (rowinfo_[row].crskip_.value() != 0)
384 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
386 // make sure an upcoming '[' does not break anything
387 if (row + 1 < nrows()) {
388 MathArray const & c = cell(index(row + 1, 0));
389 if (c.size() && c.front()->getChar() == '[')
393 // only add \\ if necessary
394 if (eol.empty() && row + 1 == nrows())
397 return "\\\\" + eol + '\n';
401 string MathGridInset::eocString(col_type col) const
403 if (col + 1 == ncols())
409 void MathGridInset::addRow(row_type row)
411 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
412 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
416 void MathGridInset::appendRow()
418 rowinfo_.push_back(RowInfo());
419 //cells_.insert(cells_.end(), ncols(), MathXArray());
420 for (col_type col = 0; col < ncols(); ++col)
421 cells_.push_back(cells_type::value_type());
425 void MathGridInset::delRow(row_type row)
430 cells_type::iterator it = cells_.begin() + row * ncols();
431 cells_.erase(it, it + ncols());
433 rowinfo_.erase(rowinfo_.begin() + row);
437 void MathGridInset::addCol(col_type newcol)
439 const col_type nc = ncols();
440 const row_type nr = nrows();
441 cells_type new_cells((nc + 1) * nr);
443 for (row_type row = 0; row < nr; ++row)
444 for (col_type col = 0; col < nc; ++col)
445 new_cells[row * (nc + 1) + col + (col > newcol)]
446 = cells_[row * nc + col];
447 std::swap(cells_, new_cells);
450 inf.skip_ = defaultColSpace(newcol);
451 inf.align_ = defaultColAlign(newcol);
452 colinfo_.insert(colinfo_.begin() + newcol, inf);
456 void MathGridInset::delCol(col_type col)
462 for (col_type i = 0; i < nargs(); ++i)
463 if (i % ncols() != col)
464 tmpcells.push_back(cells_[i]);
465 std::swap(cells_, tmpcells);
467 colinfo_.erase(colinfo_.begin() + col);
471 int MathGridInset::cellXOffset(idx_type idx) const
473 col_type c = col(idx);
474 int x = colinfo_[c].offset_;
475 char align = colinfo_[c].align_;
476 if (align == 'r' || align == 'R')
477 x += colinfo_[c].width_ - xcell(idx).width();
478 if (align == 'c' || align == 'C')
479 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
484 int MathGridInset::cellYOffset(idx_type idx) const
486 return rowinfo_[row(idx)].offset_;
490 bool MathGridInset::idxUp(idx_type & /*idx*/, pos_type & /*pos*/) const
496 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
498 pos = xcell(idx).x2pos(x - cellXOffset(idx));
504 bool MathGridInset::idxDown(idx_type & /*idx*/, pos_type & /*pos*/) const
508 if (idx >= ncols() * (nrows() - 1))
510 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
512 pos = xcell(idx).x2pos(x - cellXOffset(idx));
518 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
520 // leave matrix if on the left hand edge
524 pos = cell(idx).size();
529 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
531 // leave matrix if on the right hand edge
532 if (col(idx) + 1 == ncols())
540 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
547 idx = (nrows() - 1) * ncols();
550 idx = (nrows() / 2) * ncols();
557 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
567 idx = (nrows() / 2 + 1) * ncols() - 1;
569 pos = cell(idx).size();
574 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
581 idx -= idx % ncols();
594 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
596 if (pos < cell(idx).size()) {
597 pos = cell(idx).size();
600 if (col(idx) < ncols() - 1) {
601 idx = idx - idx % ncols() + ncols() - 1;
602 pos = cell(idx).size();
605 if (idx < nargs() - 1) {
607 pos = cell(idx).size();
614 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
619 // nothing to do if we are in the last row of the inset
620 if (row(idx) + 1 == nrows())
623 // try to delete entire sequence of ncols() empty cells if possible
624 for (idx_type i = idx; i < idx + ncols(); ++i)
628 // move cells if necessary
629 for (idx_type i = index(row(idx), 0); i < idx; ++i)
630 cell(i).swap(cell(i + ncols()));
637 // undo effect of Ctrl-Tab (i.e. pull next cell)
638 //if (idx + 1 != nargs())
639 // cell(idx).swap(cell(idx + 1));
643 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
645 // leave this unimplemented unless someone wants to have it.
647 int n = (to - from) / ncols();
648 int r = from / ncols();
651 cells_type::iterator it = cells_.begin() + from;
652 cells_.erase(it, it + n * ncols());
653 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
659 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
661 return rowinfo_[row];
665 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
667 return rowinfo_[row];
671 std::vector<MathInset::idx_type>
672 MathGridInset::idxBetween(idx_type from, idx_type to) const
674 row_type r1 = std::min(row(from), row(to));
675 row_type r2 = std::max(row(from), row(to));
676 col_type c1 = std::min(col(from), col(to));
677 col_type c2 = std::max(col(from), col(to));
678 std::vector<idx_type> res;
679 for (row_type i = r1; i <= r2; ++i)
680 for (col_type j = c1; j <= c2; ++j)
681 res.push_back(index(i, j));
687 void MathGridInset::normalize(NormalStream & os) const
690 for (row_type row = 0; row < nrows(); ++row) {
692 for (col_type col = 0; col < ncols(); ++col)
693 os << "[cell " << cell(index(row, col)) << ']';
700 void MathGridInset::mathmlize(MathMLStream & os) const
702 os << MTag("mtable");
703 for (row_type row = 0; row < nrows(); ++row) {
705 for (col_type col = 0; col < ncols(); ++col)
706 os << cell(index(row, col));
709 os << ETag("mtable");
713 void MathGridInset::write(WriteStream & os) const
715 for (row_type row = 0; row < nrows(); ++row) {
716 os << verboseHLine(rowinfo_[row].lines_);
717 for (col_type col = 0; col < ncols(); ++col)
718 os << cell(index(row, col)) << eocString(col);
719 os << eolString(row);
721 string const s = verboseHLine(rowinfo_[nrows()].lines_);