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)
27 int MathGridInset::RowInfo::skipPixels() const
30 #warning fix this once the interface to LyXLength has oimproved
32 return int(skip_.value());
37 MathGridInset::ColInfo::ColInfo()
38 : h_align_('c'), leftline_(false), rightline_(false)
42 MathGridInset::MathGridInset(int m, int n)
43 : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
46 lyxerr << "positve number of columns expected\n";
48 lyxerr << "positve number of rows expected\n";
52 int MathGridInset::index(int row, int col) const
54 return col + ncols() * row;
59 void MathGridInset::halign(string const & hh)
64 for (int i = 0; i < n; ++i)
65 colinfo_[i].h_align_ = hh[i];
69 void MathGridInset::halign(char h, int col)
71 colinfo_[col].h_align_ = h;
75 char MathGridInset::halign(int col) const
77 return colinfo_[col].h_align_;
82 void MathGridInset::valign(char c)
88 char MathGridInset::valign() const
95 void MathGridInset::vskip(LyXLength const & skip, int row)
97 rowinfo_[row].skip_ = skip;
101 LyXLength MathGridInset::vskip(int row) const
103 return rowinfo_[row].skip_;
107 void MathGridInset::metrics(MathStyles st) const
109 // let the cells adjust themselves
110 MathNestInset::metrics(st);
113 // adjust vertical structure
114 for (int row = 0; row < nrows(); ++row) {
117 for (int col = 0; col < ncols(); ++col) {
118 MathXArray const & c = xcell(index(row, col));
119 asc = std::max(asc, c.ascent());
120 desc = std::max(desc, c.descent());
122 rowinfo_[row].ascent_ = asc;
123 rowinfo_[row].descent_ = desc;
126 rowinfo_[row].offset_ =
127 rowinfo_[row - 1].offset_ +
128 rowinfo_[row - 1].descent_ +
129 rowinfo_[row - 1].skipPixels() +
131 rowinfo_[row].ascent_;
133 rowinfo_[row].offset_ = 0;
136 // adjust vertical offset
143 h = rowinfo_.back().offset_;
146 h = rowinfo_.back().offset_ / 2;
149 for (int row = 0; row < nrows(); ++row) {
150 rowinfo_[row].offset_ -= h;
151 rowinfo_[row].offset_ += MATH_BORDER;
154 // adjust horizontal structure
155 for (int col = 0; col < ncols(); ++col) {
157 for (int row = 0; row < nrows(); ++row)
158 wid = std::max(wid, xcell(index(row, col)).width());
159 colinfo_[col].width_ = wid;
160 colinfo_[col].offset_ = colinfo_[col].width_;
163 colinfo_[col].offset_ =
164 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
166 colinfo_[col].offset_ = 0;
168 colinfo_[col].offset_ += MATH_BORDER;
171 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
172 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
173 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
176 // Increase ws_[i] for 'R' columns (except the first one)
177 for (int i = 1; i < nc_; ++i)
178 if (h_align_[i] == 'R')
179 ws_[i] += 10 * df_width;
180 // Increase ws_[i] for 'C' column
181 if (h_align_[0] == 'C')
182 if (ws_[0] < 7 * workwidth / 8)
183 ws_[0] = 7 * workwidth / 8;
187 for (cxrow = row_.begin(); cxrow; ++cxrow) {
188 int rg = MATH_COLSEP;
190 for (int i = 0; i < nc_; ++i) {
192 if (cxrow->getTab(i) <= 0) {
193 cxrow->setTab(i, df_width);
196 switch (h_align_[i]) {
201 lf = (ws_[i] - cxrow->getTab(i))/2;
205 lf = ws_[i] - cxrow->getTab(i);
208 if (cxrow == row_.begin())
210 else if (cxrow.is_last())
211 lf = ws_[i] - cxrow->getTab(i);
213 lf = (ws_[i] - cxrow->getTab(i))/2;
216 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
217 cxrow->setTab(i, lf + rg);
218 rg = ws_[i] - ww + MATH_COLSEP;
219 if (cxrow == row_.begin())
220 width += ws_[i] + MATH_COLSEP;
222 cxrow->setBaseline(cxrow->getBaseline() - ascent);
228 void MathGridInset::draw(Painter & pain, int x, int y) const
232 for (int idx = 0; idx < nargs(); ++idx)
233 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
237 void MathGridInset::write(std::ostream & os, bool fragile) const
239 for (int row = 0; row < nrows(); ++row) {
240 for (int col = 0; col < ncols(); ++col) {
241 cell(index(row, col)).write(os, fragile);
242 os << eocString(col);
244 os << eolString(row);
249 string MathGridInset::eolString(int row) const
251 if (row == nrows() - 1)
254 if (rowinfo_[row].skip_ != LyXLength())
255 return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
257 // make sure an upcoming '[' does not break anything
258 MathArray const & c = cell(index(row + 1, 0));
259 if (c.size() && (*c.begin())->getChar() == '[')
260 return "\\\\[0pt]\n";
266 string MathGridInset::eocString(int col) const
268 if (col == ncols() - 1)
274 void MathGridInset::addRow(int row)
276 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
277 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
281 void MathGridInset::appendRow()
283 rowinfo_.push_back(RowInfo());
284 for (int i = 0; i < ncols(); ++i)
285 cells_.push_back(cells_type::value_type());
289 void MathGridInset::delRow(int row)
294 cells_type::iterator it = cells_.begin() + row * ncols();
295 cells_.erase(it, it + ncols());
297 rowinfo_.erase(rowinfo_.begin() + row);
301 void MathGridInset::addCol(int newcol)
303 int const nc = ncols();
304 int const nr = nrows();
305 cells_type new_cells((nc + 1) * nr);
307 for (int row = 0; row < nr; ++row)
308 for (int col = 0; col < nc; ++col)
309 new_cells[row * (nc + 1) + col + (col > newcol)]
310 = cells_[row * nc + col];
311 std::swap(cells_, new_cells);
313 colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
317 void MathGridInset::delCol(int col)
323 for (int i = 0; i < nargs(); ++i)
324 if (i % ncols() != col)
325 tmpcells.push_back(cells_[i]);
326 std::swap(cells_, tmpcells);
328 colinfo_.erase(colinfo_.begin() + col);
332 int MathGridInset::cellXOffset(int idx) const
335 int x = colinfo_[c].offset_;
336 char align = colinfo_[c].h_align_;
337 if (align == 'r' || align == 'R')
338 x += colinfo_[c].width_ - xcell(idx).width();
339 if (align == 'c' || align == 'C')
340 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
345 int MathGridInset::cellYOffset(int idx) const
347 return rowinfo_[row(idx)].offset_;
351 bool MathGridInset::idxUp(int & idx, int & pos) const
361 bool MathGridInset::idxDown(int & idx, int & pos) const
363 if (idx >= ncols() * (nrows() - 1))
371 bool MathGridInset::idxLeft(int & idx, int & pos) const
373 // leave matrix if on the left hand edge
377 pos = cell(idx).size();
382 bool MathGridInset::idxRight(int & idx, int & pos) const
384 // leave matrix if on the right hand edge
385 if (col(idx) == ncols() - 1)
393 bool MathGridInset::idxFirst(int & idx, int & pos) const
400 idx = (nrows() - 1) * ncols();
403 idx = (nrows() / 2) * ncols();
410 bool MathGridInset::idxLast(int & idx, int & pos) const
420 idx = (nrows() / 2 + 1) * ncols() - 1;
422 pos = cell(idx).size();
427 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
432 // delete entire row if in first cell of empty row
433 if (col(idx) == 0 && nrows() > 1) {
434 bool deleterow = true;
435 for (int i = idx; i < idx + ncols(); ++i)
436 if (cell(i).size()) {
448 // undo effect of Ctrl-Tab (i.e. pull next cell)
449 //if (idx != nargs() - 1)
450 // cell(idx).swap(cell(idx + 1));
454 void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
456 // leave this unimplemented unless someone wants to have it.
458 int n = (to - from) / ncols();
459 int r = from / ncols();
462 cells_type::iterator it = cells_.begin() + from;
463 cells_.erase(it, it + n * ncols());
464 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
470 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
476 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
482 std::vector<int> MathGridInset::idxBetween(int from, int to) const
484 int r1 = std::min(row(from), row(to));
485 int r2 = std::max(row(from), row(to));
486 int c1 = std::min(col(from), col(to));
487 int c2 = std::max(col(from), col(to));
488 std::vector<int> res;
489 for (int i = r1; i <= r2; ++i)
490 for (int j = c1; j <= c2; ++j)
491 res.push_back(index(i, j));