5 #include "math_gridinset.h"
6 #include "support/LOstream.h"
13 int const MATH_COLSEP = 10;
15 int const MATH_ROWSEP = 10;
17 int const MATH_BORDER = 2;
22 MathGridInset::RowInfo::RowInfo()
23 : upperline_(false), lowerline_(false)
28 int MathGridInset::RowInfo::skipPixels() const
31 #warning fix this once the interface to LyXLength has improved
33 return int(skip_.value());
38 MathGridInset::ColInfo::ColInfo()
39 : align_('c'), leftline_(false), rightline_(false), skip_(MATH_COLSEP)
43 MathGridInset::MathGridInset(col_type m, row_type n)
44 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
47 lyxerr << "positve number of columns expected\n";
49 lyxerr << "positve number of rows expected\n";
54 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
56 return col + ncols() * row;
60 void MathGridInset::setDefaults()
62 for (col_type col = 0; col < ncols(); ++col) {
63 colinfo_[col].align_ = defaultColAlign(col);
64 colinfo_[col].skip_ = defaultColSpace(col);
69 void MathGridInset::halign(string const & hh)
71 col_type n = hh.size();
74 for (col_type col = 0; col < n; ++col)
75 colinfo_[col].align_ = hh[col];
79 void MathGridInset::halign(char h, col_type col)
81 colinfo_[col].align_ = h;
85 char MathGridInset::halign(col_type col) const
87 return colinfo_[col].align_;
92 void MathGridInset::valign(char c)
98 char MathGridInset::valign() const
105 void MathGridInset::vskip(LyXLength const & skip, row_type row)
107 rowinfo_[row].skip_ = skip;
111 LyXLength MathGridInset::vskip(row_type row) const
113 return rowinfo_[row].skip_;
117 void MathGridInset::metrics(MathStyles st) const
119 // let the cells adjust themselves
120 MathNestInset::metrics(st);
123 // adjust vertical structure
124 for (row_type row = 0; row < nrows(); ++row) {
127 for (col_type col = 0; col < ncols(); ++col) {
128 MathXArray const & c = xcell(index(row, col));
129 asc = std::max(asc, c.ascent());
130 desc = std::max(desc, c.descent());
132 rowinfo_[row].ascent_ = asc;
133 rowinfo_[row].descent_ = desc;
136 rowinfo_[row].offset_ =
137 rowinfo_[row - 1].offset_ +
138 rowinfo_[row - 1].descent_ +
139 rowinfo_[row - 1].skipPixels() +
141 rowinfo_[row].ascent_;
143 rowinfo_[row].offset_ = 0;
146 // adjust vertical offset
153 h = rowinfo_.back().offset_;
156 h = rowinfo_.back().offset_ / 2;
159 for (row_type row = 0; row < nrows(); ++row) {
160 rowinfo_[row].offset_ -= h;
161 rowinfo_[row].offset_ += MATH_BORDER;
164 // adjust horizontal structure
165 for (col_type col = 0; col < ncols(); ++col) {
167 for (row_type row = 0; row < nrows(); ++row)
168 wid = std::max(wid, xcell(index(row, col)).width());
169 colinfo_[col].width_ = wid;
170 colinfo_[col].offset_ = colinfo_[col].width_;
173 colinfo_[col].offset_ =
174 colinfo_[col - 1].offset_ +
175 colinfo_[col - 1].width_ +
176 colinfo_[col - 1].skip_;
178 colinfo_[col].offset_ = 0;
180 colinfo_[col].offset_ += MATH_BORDER;
183 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
184 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
185 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
188 // Increase ws_[i] for 'R' columns (except the first one)
189 for (int i = 1; i < nc_; ++i)
190 if (align_[i] == 'R')
191 ws_[i] += 10 * df_width;
192 // Increase ws_[i] for 'C' column
193 if (align_[0] == 'C')
194 if (ws_[0] < 7 * workwidth / 8)
195 ws_[0] = 7 * workwidth / 8;
199 for (cxrow = row_.begin(); cxrow; ++cxrow) {
200 int rg = MATH_COLSEP;
202 for (int i = 0; i < nc_; ++i) {
204 if (cxrow->getTab(i) <= 0) {
205 cxrow->setTab(i, df_width);
213 lf = (ws_[i] - cxrow->getTab(i))/2;
217 lf = ws_[i] - cxrow->getTab(i);
220 if (cxrow == row_.begin())
222 else if (cxrow.is_last())
223 lf = ws_[i] - cxrow->getTab(i);
225 lf = (ws_[i] - cxrow->getTab(i))/2;
228 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
229 cxrow->setTab(i, lf + rg);
230 rg = ws_[i] - ww + MATH_COLSEP;
231 if (cxrow == row_.begin())
232 width += ws_[i] + MATH_COLSEP;
234 cxrow->setBaseline(cxrow->getBaseline() - ascent);
240 void MathGridInset::draw(Painter & pain, int x, int y) const
244 for (idx_type idx = 0; idx < nargs(); ++idx)
245 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
249 void MathGridInset::write(std::ostream & os, bool fragile) const
251 for (row_type row = 0; row < nrows(); ++row) {
252 for (col_type col = 0; col < ncols(); ++col) {
253 cell(index(row, col)).write(os, fragile);
254 os << eocString(col);
256 os << eolString(row);
261 string MathGridInset::eolString(row_type row) const
263 if (row + 1 == nrows())
266 if (rowinfo_[row].skip_.value() != 0)
267 return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
269 // make sure an upcoming '[' does not break anything
270 MathArray const & c = cell(index(row + 1, 0));
271 if (c.size() && c.begin()->nucleus()->getChar() == '[')
272 return "\\\\[0pt]\n";
278 string MathGridInset::eocString(col_type col) const
280 if (col + 1 == ncols())
286 void MathGridInset::addRow(row_type row)
288 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
289 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
293 void MathGridInset::appendRow()
295 rowinfo_.push_back(RowInfo());
296 for (col_type col = 0; col < ncols(); ++col)
297 cells_.push_back(cells_type::value_type());
301 void MathGridInset::delRow(row_type row)
306 cells_type::iterator it = cells_.begin() + row * ncols();
307 cells_.erase(it, it + ncols());
309 rowinfo_.erase(rowinfo_.begin() + row);
313 void MathGridInset::addCol(col_type newcol)
315 const col_type nc = ncols();
316 const row_type nr = nrows();
317 cells_type new_cells((nc + 1) * nr);
319 for (row_type row = 0; row < nr; ++row)
320 for (col_type col = 0; col < nc; ++col)
321 new_cells[row * (nc + 1) + col + (col > newcol)]
322 = cells_[row * nc + col];
323 std::swap(cells_, new_cells);
326 inf.skip_ = defaultColSpace(newcol);
327 inf.align_ = defaultColAlign(newcol);
328 colinfo_.insert(colinfo_.begin() + newcol, inf);
332 void MathGridInset::delCol(col_type col)
338 for (col_type i = 0; i < nargs(); ++i)
339 if (i % ncols() != col)
340 tmpcells.push_back(cells_[i]);
341 std::swap(cells_, tmpcells);
343 colinfo_.erase(colinfo_.begin() + col);
347 int MathGridInset::cellXOffset(idx_type idx) const
349 col_type c = col(idx);
350 int x = colinfo_[c].offset_;
351 char align = colinfo_[c].align_;
352 if (align == 'r' || align == 'R')
353 x += colinfo_[c].width_ - xcell(idx).width();
354 if (align == 'c' || align == 'C')
355 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
360 int MathGridInset::cellYOffset(idx_type idx) const
362 return rowinfo_[row(idx)].offset_;
366 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
376 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
378 if (idx >= ncols() * (nrows() - 1))
386 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
388 // leave matrix if on the left hand edge
392 pos = cell(idx).size();
397 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
399 // leave matrix if on the right hand edge
400 if (col(idx) == ncols() - 1)
408 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
415 idx = (nrows() - 1) * ncols();
418 idx = (nrows() / 2) * ncols();
425 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
435 idx = (nrows() / 2 + 1) * ncols() - 1;
437 pos = cell(idx).size();
442 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
447 // delete entire row if in first cell of empty row
448 if (col(idx) == 0 && nrows() > 1) {
449 bool deleterow = true;
450 for (idx_type i = idx; i < idx + ncols(); ++i)
451 if (cell(i).size()) {
463 // undo effect of Ctrl-Tab (i.e. pull next cell)
464 //if (idx != nargs() - 1)
465 // cell(idx).swap(cell(idx + 1));
469 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
471 // leave this unimplemented unless someone wants to have it.
473 int n = (to - from) / ncols();
474 int r = from / ncols();
477 cells_type::iterator it = cells_.begin() + from;
478 cells_.erase(it, it + n * ncols());
479 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
485 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
487 return rowinfo_[row];
491 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
493 return rowinfo_[row];
497 std::vector<MathInset::idx_type>
498 MathGridInset::idxBetween(idx_type from, idx_type to) const
500 row_type r1 = std::min(row(from), row(to));
501 row_type r2 = std::max(row(from), row(to));
502 col_type c1 = std::min(col(from), col(to));
503 col_type c2 = std::max(col(from), col(to));
504 std::vector<idx_type> res;
505 for (row_type i = r1; i <= r2; ++i)
506 for (col_type j = c1; j <= c2; ++j)
507 res.push_back(index(i, j));