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 MathGridInset::ColInfo::ColInfo()
28 : h_align_('c'), leftline_(false), rightline_(false)
32 MathGridInset::MathGridInset(int m, int n, string const & nm, MathInsetTypes ot)
33 : MathInset(m * n, nm, ot), rowinfo_(n), colinfo_(m), v_align_('c')
36 lyxerr << "positve number of columns expected\n";
38 lyxerr << "positve number of rows expected\n";
42 int MathGridInset::index(int row, int col) const
44 return col + ncols() * row;
48 void MathGridInset::halign(string const & hh)
53 for (int i = 0; i < n; ++i)
54 colinfo_[i].h_align_ = hh[i];
57 void MathGridInset::halign(char h, int col)
59 colinfo_[col].h_align_ = h;
62 char MathGridInset::halign(int col) const
64 return colinfo_[col].h_align_;
67 void MathGridInset::valign(char c)
72 char MathGridInset::valign() const
77 void MathGridInset::Metrics(MathStyles st, int, int)
79 // let the cells adjust themselves
80 MathInset::Metrics(st);
83 // adjust vertical structure
84 for (int row = 0; row < nrows(); ++row) {
87 for (int col = 0; col < ncols(); ++col) {
88 MathXArray const & c = xcell(index(row, col));
89 asc = std::max(asc, c.ascent());
90 desc = std::max(desc, c.descent());
92 rowinfo_[row].ascent_ = asc;
93 rowinfo_[row].descent_ = desc;
96 rowinfo_[row].offset_ =
97 rowinfo_[row - 1].offset_ +
98 rowinfo_[row - 1].descent_ +
100 rowinfo_[row].ascent_;
102 rowinfo_[row].offset_ = 0;
105 // adjust vertical offset
112 h = rowinfo_.back().offset_;
115 h = rowinfo_.back().offset_ / 2;
118 for (int row = 0; row < nrows(); ++row) {
119 rowinfo_[row].offset_ -= h;
120 rowinfo_[row].offset_ += MATH_BORDER;
123 // adjust horizontal structure
124 for (int col = 0; col < ncols(); ++col) {
126 for (int row = 0; row < nrows(); ++row)
127 wid = std::max(wid, xcell(index(row, col)).width());
128 colinfo_[col].width_ = wid;
129 colinfo_[col].offset_ = colinfo_[col].width_;
132 colinfo_[col].offset_ =
133 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
135 colinfo_[col].offset_ = 0;
137 colinfo_[col].offset_ += MATH_BORDER;
140 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
141 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
142 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
145 // Increase ws_[i] for 'R' columns (except the first one)
146 for (int i = 1; i < nc_; ++i)
147 if (h_align_[i] == 'R')
148 ws_[i] += 10 * df_width;
149 // Increase ws_[i] for 'C' column
150 if (h_align_[0] == 'C')
151 if (ws_[0] < 7 * workwidth / 8)
152 ws_[0] = 7 * workwidth / 8;
156 for (cxrow = row_.begin(); cxrow; ++cxrow) {
157 int rg = MATH_COLSEP;
159 for (int i = 0; i < nc_; ++i) {
161 if (cxrow->getTab(i) <= 0) {
162 cxrow->setTab(i, df_width);
165 switch (h_align_[i]) {
170 lf = (ws_[i] - cxrow->getTab(i))/2;
174 lf = ws_[i] - cxrow->getTab(i);
177 if (cxrow == row_.begin())
179 else if (cxrow.is_last())
180 lf = ws_[i] - cxrow->getTab(i);
182 lf = (ws_[i] - cxrow->getTab(i))/2;
185 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
186 cxrow->setTab(i, lf + rg);
187 rg = ws_[i] - ww + MATH_COLSEP;
188 if (cxrow == row_.begin())
189 width += ws_[i] + MATH_COLSEP;
191 cxrow->setBaseline(cxrow->getBaseline() - ascent);
196 void MathGridInset::draw(Painter & pain, int x, int y)
200 for (int row = 0; row < nrows(); ++row) {
201 int yy = y + rowinfo_[row].offset_;
202 for (int col = 0; col < ncols(); ++col) {
203 int xx = x + colinfo_[col].offset_;
204 char align = colinfo_[col].h_align_;
205 if (align == 'r' || align == 'R')
206 xx += colinfo_[col].width_ - xcell(index(row, col)).width();
207 if (align == 'c' || align == 'C')
208 xx += (colinfo_[col].width_ - xcell(index(row, col)).width()) / 2;
209 xcell(index(row, col)).draw(pain, xx, yy);
215 void MathGridInset::Write(std::ostream & os, bool fragile) const
217 for (int row = 0; row < nrows(); ++row) {
220 for (int col = 0; col < ncols(); ++col) {
223 cell(index(row, col)).Write(os, fragile);
229 void MathGridInset::addRow(int row)
231 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
232 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
235 void MathGridInset::appendRow()
237 rowinfo_.push_back(RowInfo());
238 for (int i = 0; i < ncols(); ++i)
239 cells_.push_back(cells_type::value_type());
243 void MathGridInset::delRow(int row)
248 lyxerr << "delRow: nr: " << nrows() << " nc: " << ncols()
249 << " row: " << row << "\n";
251 cells_type::iterator it = cells_.begin() + row * ncols();
252 cells_.erase(it, it + ncols());
254 rowinfo_.erase(rowinfo_.begin() + row);
258 void MathGridInset::addCol(int newcol)
260 int const nc = ncols();
261 int const nr = nrows();
262 cells_type new_cells((nc + 1) * nr);
264 for (int row = 0; row < nr; ++row)
265 for (int col = 0; col < nc; ++col)
266 new_cells[row * (nc + 1) + col + (col > newcol)]
267 = cells_[row * nc + col];
268 std::swap(cells_, new_cells);
270 colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
274 void MathGridInset::delCol(int col)
280 for (int i = 0; i < nargs(); ++i)
281 if (i % ncols() != col)
282 tmpcells.push_back(cells_[i]);
283 std::swap(cells_, tmpcells);
285 colinfo_.erase(colinfo_.begin() + col);
289 bool MathGridInset::idxUp(int & idx, int & pos) const
299 bool MathGridInset::idxDown(int & idx, int & pos) const
301 if (idx >= ncols() * (nrows() - 1))
309 bool MathGridInset::idxLeft(int & idx, int & pos) const
311 // leave matrix if on the left hand edge
315 pos = cell(idx).size();
320 bool MathGridInset::idxRight(int & idx, int & pos) const
322 // leave matrix if on the right hand edge
323 if (col(idx) == ncols() - 1)
331 bool MathGridInset::idxFirst(int & idx, int & pos) const
338 idx = (nrows() - 1) * ncols();
341 idx = (nrows() / 2) * ncols();
348 bool MathGridInset::idxLast(int & idx, int & pos) const
358 idx = (nrows() / 2 + 1) * ncols() - 1;
360 pos = cell(idx).size();
365 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
370 // delete entire row if in first cell of empty row
371 if (col(idx) == 0 && nrows() > 1) {
372 bool deleterow = true;
373 for (int i = idx; i < idx + ncols(); ++i)
374 if (cell(i).size()) {
386 // undo effect of Ctrl-Tab (i.e. pull next cell)
387 //if (idx != nargs() - 1)
388 // cell(idx).swap(cell(idx + 1));
392 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
398 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)