8 #include "support/LOstream.h"
17 int const MATH_COLSEP = 10;
19 int const MATH_ROWSEP = 10;
21 int const MATH_BORDER = 2;
29 MathGridInset::RowInfo::RowInfo()
30 : upperline_(false), lowerline_(false)
34 MathGridInset::ColInfo::ColInfo()
35 : h_align_('c'), leftline_(false), rightline_(false)
39 MathGridInset::MathGridInset(int m, int n, string const & nm, short ot)
40 : MathInset(nm, ot, m * n), rowinfo_(n), colinfo_(m), v_align_('c')
43 lyxerr << "positve number of columns expected\n";
45 lyxerr << "positve number of rows expected\n";
49 int MathGridInset::index(int row, int col) const
51 return col + ncols() * row;
55 void MathGridInset::halign(string const & hh)
60 for (int i = 0; i < n; ++i)
61 colinfo_[i].h_align_ = hh[i];
64 void MathGridInset::halign(char h, int col)
66 colinfo_[col].h_align_ = h;
69 void MathGridInset::valign(char c)
71 lyxerr << "setting valign to " << c << "\n";
75 void MathGridInset::Metrics(MathStyles st)
77 // let the cells adjust themselves
78 MathInset::Metrics(st);
81 // adjust vertical structure
82 for (int row = 0; row < nrows(); ++row) {
85 for (int col = 0; col < ncols(); ++col) {
86 MathXArray const & c = xcell(index(row, col));
87 asc = max(asc, c.ascent());
88 desc = max(desc, c.descent());
90 rowinfo_[row].ascent_ = asc;
91 rowinfo_[row].descent_ = desc;
94 rowinfo_[row].offset_ =
95 rowinfo_[row - 1].offset_ +
96 rowinfo_[row - 1].descent_ +
98 rowinfo_[row].ascent_;
100 rowinfo_[row].offset_ = 0;
103 // adjust vertical offset
104 lyxerr << "v_align: " << v_align_ << "\n";
111 h = rowinfo_.back().offset_;
114 h = rowinfo_.back().offset_ / 2;
117 for (int row = 0; row < nrows(); ++row) {
118 rowinfo_[row].offset_ -= h;
119 rowinfo_[row].offset_ += MATH_BORDER;
122 // adjust horizontal structure
123 for (int col = 0; col < ncols(); ++col) {
125 for (int row = 0; row < nrows(); ++row)
126 wid = max(wid, xcell(index(row, col)).width());
127 colinfo_[col].width_ = wid;
128 colinfo_[col].offset_ = colinfo_[col].width_;
131 colinfo_[col].offset_ =
132 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
134 colinfo_[col].offset_ = 0;
136 colinfo_[col].offset_ += MATH_BORDER;
139 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
140 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
141 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
143 // some extra space around
144 width_ += 2 * MATH_BORDER;
145 ascent_ += MATH_BORDER;
146 descent_ += MATH_BORDER;
149 // Increase ws_[i] for 'R' columns (except the first one)
150 for (int i = 1; i < nc_; ++i)
151 if (h_align_[i] == 'R')
152 ws_[i] += 10 * df_width;
153 // Increase ws_[i] for 'C' column
154 if (h_align_[0] == 'C')
155 if (ws_[0] < 7 * workwidth / 8)
156 ws_[0] = 7 * workwidth / 8;
160 for (cxrow = row_.begin(); cxrow; ++cxrow) {
161 int rg = MATH_COLSEP;
163 for (int i = 0; i < nc_; ++i) {
165 if (cxrow->getTab(i) <= 0) {
166 cxrow->setTab(i, df_width);
169 switch (h_align_[i]) {
174 lf = (ws_[i] - cxrow->getTab(i))/2;
178 lf = ws_[i] - cxrow->getTab(i);
181 if (cxrow == row_.begin())
183 else if (cxrow.is_last())
184 lf = ws_[i] - cxrow->getTab(i);
186 lf = (ws_[i] - cxrow->getTab(i))/2;
189 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
190 cxrow->setTab(i, lf + rg);
191 rg = ws_[i] - ww + MATH_COLSEP;
192 if (cxrow == row_.begin())
193 width += ws_[i] + MATH_COLSEP;
195 cxrow->setBaseline(cxrow->getBaseline() - ascent);
200 void MathGridInset::draw(Painter & pain, int x, int y)
204 for (int row = 0; row < nrows(); ++row) {
205 int yy = y + rowinfo_[row].offset_;
206 for (int col = 0; col < ncols(); ++col) {
207 int xx = x + colinfo_[col].offset_;
208 char align = colinfo_[col].h_align_;
209 if (align == 'r' || align == 'R')
210 xx += colinfo_[col].width_ - xcell(index(row, col)).width();
211 if (align == 'c' || align == 'C')
212 xx += (colinfo_[col].width_ - xcell(index(row, col)).width()) / 2;
213 xcell(index(row, col)).draw(pain, xx, yy);
219 void MathGridInset::Write(std::ostream & os, bool fragile) const
221 for (int row = 0; row < nrows(); ++row) {
224 for (int col = 0; col < ncols(); ++col) {
227 cell(index(row, col)).Write(os, fragile);
233 void MathGridInset::addRow(int row)
235 lyxerr << "adding row " << row << endl;
236 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
237 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
240 void MathGridInset::appendRow()
242 rowinfo_.push_back(RowInfo());
243 for (int i = 0; i < ncols(); ++i)
248 void MathGridInset::delRow(int row)
253 lyxerr << "delRow: nr: " << nrows() << " nc: " << ncols()
254 << " row: " << row << "\n";
256 cells_type::iterator it = cells_.begin() + row * ncols();
257 cells_.erase(it, it + ncols());
259 rowinfo_.erase(rowinfo_.begin() + row);
263 void MathGridInset::addCol(int newcol)
267 cells_type new_cells = cells_type((nc + 1) * nr);
269 for (int row = 0; row < nr; ++row)
270 for (int col = 0; col < nc; ++col)
271 new_cells[row * (nc + 1) + col + (col > newcol)]
272 = cells_[row * nc + col];
273 swap(cells_, new_cells);
275 colinfo_.insert(colinfo_.begin() + newcol);
279 void MathGridInset::delCol(int col)
285 for (int i = 0; i < nargs(); ++i)
286 if (i % ncols() != col)
287 tmpcells.push_back(cells_[i]);
288 swap(cells_, tmpcells);
290 colinfo_.erase(colinfo_.begin() + col);
294 bool MathGridInset::idxUp(int & idx, int & pos) const
304 bool MathGridInset::idxDown(int & idx, int & pos) const
306 if (idx >= ncols() * (nrows() - 1))
314 bool MathGridInset::idxLeft(int & idx, int & pos) const
316 // leave matrix if on the left hand edge
320 pos = cell(idx).size();
325 bool MathGridInset::idxRight(int & idx, int & pos) const
327 // leave matrix if on the right hand edge
328 if (col(idx) == ncols() - 1)
336 bool MathGridInset::idxFirst(int & idx, int & pos) const
343 idx = (nrows() - 1) * ncols();
346 idx = (nrows() / 2) * ncols();
353 bool MathGridInset::idxLast(int & idx, int & pos) const
363 idx = (nrows() / 2 + 1) * ncols() - 1;
365 pos = cell(idx).size();
371 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
377 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)