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 //////////////////////////////////////////////////////////////
25 MathGridInset::RowInfo::RowInfo()
26 : upperline_(false), lowerline_(false)
31 int MathGridInset::RowInfo::skipPixels() const
34 #warning fix this once the interface to LyXLength has improved
36 return int(skip_.value());
41 //////////////////////////////////////////////////////////////
44 MathGridInset::ColInfo::ColInfo()
45 : align_('c'), leftline_(false), rightline_(false), skip_(MATH_COLSEP)
49 //////////////////////////////////////////////////////////////
52 MathGridInset::MathGridInset(col_type m, row_type n)
53 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
59 MathGridInset::MathGridInset(int m, int n, char v, string const & h)
60 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_(v)
68 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
70 return col + ncols() * row;
74 void MathGridInset::setDefaults()
77 lyxerr << "positve number of columns expected\n";
79 lyxerr << "positve number of rows expected\n";
80 for (col_type col = 0; col < ncols(); ++col) {
81 colinfo_[col].align_ = defaultColAlign(col);
82 colinfo_[col].skip_ = defaultColSpace(col);
87 void MathGridInset::halign(string const & hh)
89 col_type n = hh.size();
92 for (col_type col = 0; col < n; ++col)
93 colinfo_[col].align_ = hh[col];
97 void MathGridInset::halign(char h, col_type col)
99 colinfo_[col].align_ = h;
103 char MathGridInset::halign(col_type col) const
105 return colinfo_[col].align_;
110 void MathGridInset::valign(char c)
116 char MathGridInset::valign() const
123 void MathGridInset::vskip(LyXLength const & skip, row_type row)
125 rowinfo_[row].skip_ = skip;
129 LyXLength MathGridInset::vskip(row_type row) const
131 return rowinfo_[row].skip_;
135 void MathGridInset::metrics(MathStyles st) const
137 // let the cells adjust themselves
138 MathNestInset::metrics(st);
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
262 for (idx_type idx = 0; idx < nargs(); ++idx)
263 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
267 void MathGridInset::write(std::ostream & os, bool fragile) const
269 for (row_type row = 0; row < nrows(); ++row) {
270 for (col_type col = 0; col < ncols(); ++col) {
271 cell(index(row, col)).write(os, fragile);
272 os << eocString(col);
274 os << eolString(row);
279 string MathGridInset::eolString(row_type row) const
281 if (row + 1 == nrows())
284 if (rowinfo_[row].skip_.value() != 0)
285 return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
287 // make sure an upcoming '[' does not break anything
288 MathArray const & c = cell(index(row + 1, 0));
289 if (c.size() && (*c.begin())->getChar() == '[')
290 return "\\\\[0pt]\n";
296 string MathGridInset::eocString(col_type col) const
298 if (col + 1 == ncols())
304 void MathGridInset::addRow(row_type row)
306 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
307 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
311 void MathGridInset::appendRow()
313 rowinfo_.push_back(RowInfo());
314 for (col_type col = 0; col < ncols(); ++col)
315 cells_.push_back(cells_type::value_type());
319 void MathGridInset::delRow(row_type row)
324 cells_type::iterator it = cells_.begin() + row * ncols();
325 cells_.erase(it, it + ncols());
327 rowinfo_.erase(rowinfo_.begin() + row);
331 void MathGridInset::addCol(col_type newcol)
333 const col_type nc = ncols();
334 const row_type nr = nrows();
335 cells_type new_cells((nc + 1) * nr);
337 for (row_type row = 0; row < nr; ++row)
338 for (col_type col = 0; col < nc; ++col)
339 new_cells[row * (nc + 1) + col + (col > newcol)]
340 = cells_[row * nc + col];
341 std::swap(cells_, new_cells);
344 inf.skip_ = defaultColSpace(newcol);
345 inf.align_ = defaultColAlign(newcol);
346 colinfo_.insert(colinfo_.begin() + newcol, inf);
350 void MathGridInset::delCol(col_type col)
356 for (col_type i = 0; i < nargs(); ++i)
357 if (i % ncols() != col)
358 tmpcells.push_back(cells_[i]);
359 std::swap(cells_, tmpcells);
361 colinfo_.erase(colinfo_.begin() + col);
365 int MathGridInset::cellXOffset(idx_type idx) const
367 col_type c = col(idx);
368 int x = colinfo_[c].offset_;
369 char align = colinfo_[c].align_;
370 if (align == 'r' || align == 'R')
371 x += colinfo_[c].width_ - xcell(idx).width();
372 if (align == 'c' || align == 'C')
373 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
378 int MathGridInset::cellYOffset(idx_type idx) const
380 return rowinfo_[row(idx)].offset_;
384 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
388 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
390 pos = xcell(idx).x2pos(x - cellXOffset(idx));
395 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
397 if (idx >= ncols() * (nrows() - 1))
399 int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
401 pos = xcell(idx).x2pos(x - cellXOffset(idx));
406 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
408 // leave matrix if on the left hand edge
412 pos = cell(idx).size();
417 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
419 // leave matrix if on the right hand edge
420 if (col(idx) == ncols() - 1)
428 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
435 idx = (nrows() - 1) * ncols();
438 idx = (nrows() / 2) * ncols();
445 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
455 idx = (nrows() / 2 + 1) * ncols() - 1;
457 pos = cell(idx).size();
462 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
467 // delete entire row if in first cell of empty row
468 if (col(idx) == 0 && nrows() > 1) {
469 bool deleterow = true;
470 for (idx_type i = idx; i < idx + ncols(); ++i)
471 if (cell(i).size()) {
483 // undo effect of Ctrl-Tab (i.e. pull next cell)
484 //if (idx != nargs() - 1)
485 // cell(idx).swap(cell(idx + 1));
489 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
491 // leave this unimplemented unless someone wants to have it.
493 int n = (to - from) / ncols();
494 int r = from / ncols();
497 cells_type::iterator it = cells_.begin() + from;
498 cells_.erase(it, it + n * ncols());
499 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
505 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
507 return rowinfo_[row];
511 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
513 return rowinfo_[row];
517 std::vector<MathInset::idx_type>
518 MathGridInset::idxBetween(idx_type from, idx_type to) const
520 row_type r1 = std::min(row(from), row(to));
521 row_type r2 = std::max(row(from), row(to));
522 col_type c1 = std::min(col(from), col(to));
523 col_type c2 = std::max(col(from), col(to));
524 std::vector<idx_type> res;
525 for (row_type i = r1; i <= r2; ++i)
526 for (col_type j = c1; j <= c2; ++j)
527 res.push_back(index(i, j));