5 #include "math_gridinset.h"
7 #include "support/LOstream.h"
14 int const MATH_COLSEP = 10;
16 int const MATH_ROWSEP = 10;
18 int const MATH_BORDER = 2;
23 //////////////////////////////////////////////////////////////
26 MathGridInset::RowInfo::RowInfo()
27 : upperline_(false), lowerline_(false)
32 int MathGridInset::RowInfo::skipPixels() const
35 #warning fix this once the interface to LyXLength has improved
37 return int(skip_.value());
42 //////////////////////////////////////////////////////////////
45 MathGridInset::ColInfo::ColInfo()
46 : align_('c'), leftline_(false), rightline_(false), skip_(MATH_COLSEP)
50 //////////////////////////////////////////////////////////////
53 MathGridInset::MathGridInset(col_type m, row_type n)
54 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
60 MathGridInset::MathGridInset(int m, int n, char v, string const & h)
61 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_(v)
69 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
71 return col + ncols() * row;
75 void MathGridInset::setDefaults()
78 lyxerr << "positve number of columns expected\n";
80 lyxerr << "positve number of rows expected\n";
81 for (col_type col = 0; col < ncols(); ++col) {
82 colinfo_[col].align_ = defaultColAlign(col);
83 colinfo_[col].skip_ = defaultColSpace(col);
88 void MathGridInset::halign(string const & hh)
90 col_type n = hh.size();
93 for (col_type col = 0; col < n; ++col)
94 colinfo_[col].align_ = hh[col];
98 void MathGridInset::halign(char h, col_type col)
100 colinfo_[col].align_ = h;
104 char MathGridInset::halign(col_type col) const
106 return colinfo_[col].align_;
111 void MathGridInset::valign(char c)
117 char MathGridInset::valign() const
124 void MathGridInset::vskip(LyXLength const & skip, row_type row)
126 rowinfo_[row].skip_ = skip;
130 LyXLength MathGridInset::vskip(row_type row) const
132 return rowinfo_[row].skip_;
136 void MathGridInset::metrics(MathMetricsInfo const & mi) const
138 // let the cells adjust themselves
139 MathNestInset::metrics(mi);
141 // adjust vertical structure
142 for (row_type row = 0; row < nrows(); ++row) {
145 for (col_type col = 0; col < ncols(); ++col) {
146 MathXArray const & c = xcell(index(row, col));
147 asc = std::max(asc, c.ascent());
148 desc = std::max(desc, c.descent());
150 rowinfo_[row].ascent_ = asc;
151 rowinfo_[row].descent_ = desc;
154 rowinfo_[row].offset_ =
155 rowinfo_[row - 1].offset_ +
156 rowinfo_[row - 1].descent_ +
157 rowinfo_[row - 1].skipPixels() +
159 rowinfo_[row].ascent_;
161 rowinfo_[row].offset_ = 0;
164 // adjust vertical offset
171 h = rowinfo_.back().offset_;
174 h = rowinfo_.back().offset_ / 2;
177 for (row_type row = 0; row < nrows(); ++row) {
178 rowinfo_[row].offset_ -= h;
179 rowinfo_[row].offset_ += MATH_BORDER;
182 // adjust horizontal structure
183 for (col_type col = 0; col < ncols(); ++col) {
185 for (row_type row = 0; row < nrows(); ++row)
186 wid = std::max(wid, xcell(index(row, col)).width());
187 colinfo_[col].width_ = wid;
188 colinfo_[col].offset_ = colinfo_[col].width_;
191 colinfo_[col].offset_ =
192 colinfo_[col - 1].offset_ +
193 colinfo_[col - 1].width_ +
194 colinfo_[col - 1].skip_;
196 colinfo_[col].offset_ = 0;
198 colinfo_[col].offset_ += MATH_BORDER;
201 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
202 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
203 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
206 // Increase ws_[i] for 'R' columns (except the first one)
207 for (int i = 1; i < nc_; ++i)
208 if (align_[i] == 'R')
209 ws_[i] += 10 * df_width;
210 // Increase ws_[i] for 'C' column
211 if (align_[0] == 'C')
212 if (ws_[0] < 7 * workwidth / 8)
213 ws_[0] = 7 * workwidth / 8;
217 for (cxrow = row_.begin(); cxrow; ++cxrow) {
218 int rg = MATH_COLSEP;
220 for (int i = 0; i < nc_; ++i) {
222 if (cxrow->getTab(i) <= 0) {
223 cxrow->setTab(i, df_width);
231 lf = (ws_[i] - cxrow->getTab(i))/2;
235 lf = ws_[i] - cxrow->getTab(i);
238 if (cxrow == row_.begin())
240 else if (cxrow.is_last())
241 lf = ws_[i] - cxrow->getTab(i);
243 lf = (ws_[i] - cxrow->getTab(i))/2;
246 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
247 cxrow->setTab(i, lf + rg);
248 rg = ws_[i] - ww + MATH_COLSEP;
249 if (cxrow == row_.begin())
250 width += ws_[i] + MATH_COLSEP;
252 cxrow->setBaseline(cxrow->getBaseline() - ascent);
258 void MathGridInset::draw(Painter & pain, int x, int y) const
260 for (idx_type idx = 0; idx < nargs(); ++idx)
261 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
265 void MathGridInset::write(MathWriteInfo & os) const
267 for (row_type row = 0; row < nrows(); ++row) {
268 for (col_type col = 0; col < ncols(); ++col)
269 os << cell(index(row, col)) << eocString(col);
270 os << eolString(row);
275 void MathGridInset::writeNormal(std::ostream & os) const
278 for (row_type row = 0; row < nrows(); ++row) {
280 for (col_type col = 0; col < ncols(); ++col) {
282 cell(index(row, col)).writeNormal(os);
291 string MathGridInset::eolString(row_type row) const
293 if (row + 1 == nrows())
296 if (rowinfo_[row].skip_.value() != 0)
297 return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
299 // make sure an upcoming '[' does not break anything
300 MathArray const & c = cell(index(row + 1, 0));
301 if (c.size() && (*c.begin())->getChar() == '[')
302 return "\\\\[0pt]\n";
308 string MathGridInset::eocString(col_type col) const
310 if (col + 1 == ncols())
316 void MathGridInset::addRow(row_type row)
318 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
319 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
323 void MathGridInset::appendRow()
325 rowinfo_.push_back(RowInfo());
326 for (col_type col = 0; col < ncols(); ++col)
327 cells_.push_back(cells_type::value_type());
331 void MathGridInset::delRow(row_type row)
336 cells_type::iterator it = cells_.begin() + row * ncols();
337 cells_.erase(it, it + ncols());
339 rowinfo_.erase(rowinfo_.begin() + row);
343 void MathGridInset::addCol(col_type newcol)
345 const col_type nc = ncols();
346 const row_type nr = nrows();
347 cells_type new_cells((nc + 1) * nr);
349 for (row_type row = 0; row < nr; ++row)
350 for (col_type col = 0; col < nc; ++col)
351 new_cells[row * (nc + 1) + col + (col > newcol)]
352 = cells_[row * nc + col];
353 std::swap(cells_, new_cells);
356 inf.skip_ = defaultColSpace(newcol);
357 inf.align_ = defaultColAlign(newcol);
358 colinfo_.insert(colinfo_.begin() + newcol, inf);
362 void MathGridInset::delCol(col_type col)
368 for (col_type i = 0; i < nargs(); ++i)
369 if (i % ncols() != col)
370 tmpcells.push_back(cells_[i]);
371 std::swap(cells_, tmpcells);
373 colinfo_.erase(colinfo_.begin() + col);
377 int MathGridInset::cellXOffset(idx_type idx) const
379 col_type c = col(idx);
380 int x = colinfo_[c].offset_;
381 char align = colinfo_[c].align_;
382 if (align == 'r' || align == 'R')
383 x += colinfo_[c].width_ - xcell(idx).width();
384 if (align == 'c' || align == 'C')
385 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
390 int MathGridInset::cellYOffset(idx_type idx) const
392 return rowinfo_[row(idx)].offset_;
396 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
400 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
402 pos = xcell(idx).x2pos(x - cellXOffset(idx));
407 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
409 if (idx >= ncols() * (nrows() - 1))
411 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
413 pos = xcell(idx).x2pos(x - cellXOffset(idx));
418 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
420 // leave matrix if on the left hand edge
424 pos = cell(idx).size();
429 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
431 // leave matrix if on the right hand edge
432 if (col(idx) == ncols() - 1)
440 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
447 idx = (nrows() - 1) * ncols();
450 idx = (nrows() / 2) * ncols();
457 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
467 idx = (nrows() / 2 + 1) * ncols() - 1;
469 pos = cell(idx).size();
474 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
481 idx -= idx % ncols();
494 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
496 if (pos < cell(idx).size()) {
497 pos = cell(idx).size();
500 if (col(idx) < ncols() - 1) {
501 idx = idx - idx % ncols() + ncols() - 1;
502 pos = cell(idx).size();
505 if (idx < nargs() - 1) {
507 pos = cell(idx).size();
514 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
519 // delete entire sequence of ncols() empty cells if possible
520 if (idx <= index(nrows() - 1, 0)) {
521 bool deleterow = true;
522 for (idx_type i = idx; i < idx + ncols(); ++i)
523 if (cell(i).size()) {
529 // move cells if necessary
530 for (idx_type i = index(row(idx), 0); i < idx; ++i)
531 cell(i).swap(cell(i + ncols()));
541 // undo effect of Ctrl-Tab (i.e. pull next cell)
542 //if (idx != nargs() - 1)
543 // cell(idx).swap(cell(idx + 1));
547 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
549 // leave this unimplemented unless someone wants to have it.
551 int n = (to - from) / ncols();
552 int r = from / ncols();
555 cells_type::iterator it = cells_.begin() + from;
556 cells_.erase(it, it + n * ncols());
557 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
563 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
565 return rowinfo_[row];
569 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
571 return rowinfo_[row];
575 std::vector<MathInset::idx_type>
576 MathGridInset::idxBetween(idx_type from, idx_type to) const
578 row_type r1 = std::min(row(from), row(to));
579 row_type r2 = std::max(row(from), row(to));
580 col_type c1 = std::min(col(from), col(to));
581 col_type c2 = std::max(col(from), col(to));
582 std::vector<idx_type> res;
583 for (row_type i = r1; i <= r2; ++i)
584 for (col_type j = c1; j <= c2; ++j)
585 res.push_back(index(i, j));