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 improved
32 return int(skip_.value());
37 MathGridInset::ColInfo::ColInfo()
38 : align_('c'), leftline_(false), rightline_(false), skip_(MATH_COLSEP)
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";
49 for (int col = 0; col < m; ++col) {
50 colinfo_[col].skip_ = defaultColSpace(col);
51 colinfo_[col].align_ = defaultColAlign(col);
56 int MathGridInset::index(int row, int col) const
58 return col + ncols() * row;
63 void MathGridInset::halign(string const & hh)
68 for (int i = 0; i < n; ++i)
69 colinfo_[i].align_ = hh[i];
73 void MathGridInset::halign(char h, int col)
75 colinfo_[col].align_ = h;
79 char MathGridInset::halign(int col) const
81 return colinfo_[col].align_;
86 void MathGridInset::valign(char c)
92 char MathGridInset::valign() const
99 void MathGridInset::vskip(LyXLength const & skip, int row)
101 rowinfo_[row].skip_ = skip;
105 LyXLength MathGridInset::vskip(int row) const
107 return rowinfo_[row].skip_;
111 void MathGridInset::metrics(MathStyles st) const
113 // let the cells adjust themselves
114 MathNestInset::metrics(st);
117 // adjust vertical structure
118 for (int row = 0; row < nrows(); ++row) {
121 for (int col = 0; col < ncols(); ++col) {
122 MathXArray const & c = xcell(index(row, col));
123 asc = std::max(asc, c.ascent());
124 desc = std::max(desc, c.descent());
126 rowinfo_[row].ascent_ = asc;
127 rowinfo_[row].descent_ = desc;
130 rowinfo_[row].offset_ =
131 rowinfo_[row - 1].offset_ +
132 rowinfo_[row - 1].descent_ +
133 rowinfo_[row - 1].skipPixels() +
135 rowinfo_[row].ascent_;
137 rowinfo_[row].offset_ = 0;
140 // adjust vertical offset
147 h = rowinfo_.back().offset_;
150 h = rowinfo_.back().offset_ / 2;
153 for (int row = 0; row < nrows(); ++row) {
154 rowinfo_[row].offset_ -= h;
155 rowinfo_[row].offset_ += MATH_BORDER;
158 // adjust horizontal structure
159 for (int col = 0; col < ncols(); ++col) {
161 for (int row = 0; row < nrows(); ++row)
162 wid = std::max(wid, xcell(index(row, col)).width());
163 colinfo_[col].width_ = wid;
164 colinfo_[col].offset_ = colinfo_[col].width_;
167 colinfo_[col].offset_ =
168 colinfo_[col - 1].offset_ +
169 colinfo_[col - 1].width_ +
170 colinfo_[col - 1].skip_;
172 colinfo_[col].offset_ = 0;
174 colinfo_[col].offset_ += MATH_BORDER;
177 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
178 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
179 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
182 // Increase ws_[i] for 'R' columns (except the first one)
183 for (int i = 1; i < nc_; ++i)
184 if (align_[i] == 'R')
185 ws_[i] += 10 * df_width;
186 // Increase ws_[i] for 'C' column
187 if (align_[0] == 'C')
188 if (ws_[0] < 7 * workwidth / 8)
189 ws_[0] = 7 * workwidth / 8;
193 for (cxrow = row_.begin(); cxrow; ++cxrow) {
194 int rg = MATH_COLSEP;
196 for (int i = 0; i < nc_; ++i) {
198 if (cxrow->getTab(i) <= 0) {
199 cxrow->setTab(i, df_width);
207 lf = (ws_[i] - cxrow->getTab(i))/2;
211 lf = ws_[i] - cxrow->getTab(i);
214 if (cxrow == row_.begin())
216 else if (cxrow.is_last())
217 lf = ws_[i] - cxrow->getTab(i);
219 lf = (ws_[i] - cxrow->getTab(i))/2;
222 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
223 cxrow->setTab(i, lf + rg);
224 rg = ws_[i] - ww + MATH_COLSEP;
225 if (cxrow == row_.begin())
226 width += ws_[i] + MATH_COLSEP;
228 cxrow->setBaseline(cxrow->getBaseline() - ascent);
234 void MathGridInset::draw(Painter & pain, int x, int y) const
238 for (int idx = 0; idx < nargs(); ++idx)
239 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
243 void MathGridInset::write(std::ostream & os, bool fragile) const
245 for (int row = 0; row < nrows(); ++row) {
246 for (int col = 0; col < ncols(); ++col) {
247 cell(index(row, col)).write(os, fragile);
248 os << eocString(col);
250 os << eolString(row);
255 string MathGridInset::eolString(int row) const
257 if (row == nrows() - 1)
260 if (rowinfo_[row].skip_.value() != 0)
261 return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
263 // make sure an upcoming '[' does not break anything
264 MathArray const & c = cell(index(row + 1, 0));
265 if (c.size() && (*c.begin())->getChar() == '[')
266 return "\\\\[0pt]\n";
272 string MathGridInset::eocString(int col) const
274 if (col == ncols() - 1)
280 void MathGridInset::addRow(int row)
282 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
283 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
287 void MathGridInset::appendRow()
289 rowinfo_.push_back(RowInfo());
290 for (int i = 0; i < ncols(); ++i)
291 cells_.push_back(cells_type::value_type());
295 void MathGridInset::delRow(int row)
300 cells_type::iterator it = cells_.begin() + row * ncols();
301 cells_.erase(it, it + ncols());
303 rowinfo_.erase(rowinfo_.begin() + row);
307 void MathGridInset::addCol(int newcol)
309 int const nc = ncols();
310 int const nr = nrows();
311 cells_type new_cells((nc + 1) * nr);
313 for (int row = 0; row < nr; ++row)
314 for (int col = 0; col < nc; ++col)
315 new_cells[row * (nc + 1) + col + (col > newcol)]
316 = cells_[row * nc + col];
317 std::swap(cells_, new_cells);
320 inf.skip_ = defaultColSpace(newcol);
321 inf.align_ = defaultColAlign(newcol);
322 colinfo_.insert(colinfo_.begin() + newcol, inf);
326 void MathGridInset::delCol(int col)
332 for (int i = 0; i < nargs(); ++i)
333 if (i % ncols() != col)
334 tmpcells.push_back(cells_[i]);
335 std::swap(cells_, tmpcells);
337 colinfo_.erase(colinfo_.begin() + col);
341 int MathGridInset::cellXOffset(int idx) const
344 int x = colinfo_[c].offset_;
345 char align = colinfo_[c].align_;
346 if (align == 'r' || align == 'R')
347 x += colinfo_[c].width_ - xcell(idx).width();
348 if (align == 'c' || align == 'C')
349 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
354 int MathGridInset::cellYOffset(int idx) const
356 return rowinfo_[row(idx)].offset_;
360 bool MathGridInset::idxUp(int & idx, int & pos) const
370 bool MathGridInset::idxDown(int & idx, int & pos) const
372 if (idx >= ncols() * (nrows() - 1))
380 bool MathGridInset::idxLeft(int & idx, int & pos) const
382 // leave matrix if on the left hand edge
386 pos = cell(idx).size();
391 bool MathGridInset::idxRight(int & idx, int & pos) const
393 // leave matrix if on the right hand edge
394 if (col(idx) == ncols() - 1)
402 bool MathGridInset::idxFirst(int & idx, int & pos) const
409 idx = (nrows() - 1) * ncols();
412 idx = (nrows() / 2) * ncols();
419 bool MathGridInset::idxLast(int & idx, int & pos) const
429 idx = (nrows() / 2 + 1) * ncols() - 1;
431 pos = cell(idx).size();
436 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
441 // delete entire row if in first cell of empty row
442 if (col(idx) == 0 && nrows() > 1) {
443 bool deleterow = true;
444 for (int i = idx; i < idx + ncols(); ++i)
445 if (cell(i).size()) {
457 // undo effect of Ctrl-Tab (i.e. pull next cell)
458 //if (idx != nargs() - 1)
459 // cell(idx).swap(cell(idx + 1));
463 void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
465 // leave this unimplemented unless someone wants to have it.
467 int n = (to - from) / ncols();
468 int r = from / ncols();
471 cells_type::iterator it = cells_.begin() + from;
472 cells_.erase(it, it + n * ncols());
473 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
479 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
485 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
491 std::vector<int> MathGridInset::idxBetween(int from, int to) const
493 int r1 = std::min(row(from), row(to));
494 int r2 = std::max(row(from), row(to));
495 int c1 = std::min(col(from), col(to));
496 int c2 = std::max(col(from), col(to));
497 std::vector<int> res;
498 for (int i = r1; i <= r2; ++i)
499 for (int j = c1; j <= c2; ++j)
500 res.push_back(index(i, j));