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, MathInsetTypes 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 char MathGridInset::halign(int col) const
71 return colinfo_[col].h_align_;
74 void MathGridInset::valign(char c)
79 char MathGridInset::valign() const
84 void MathGridInset::Metrics(MathStyles st)
86 // let the cells adjust themselves
87 MathInset::Metrics(st);
90 // adjust vertical structure
91 for (int row = 0; row < nrows(); ++row) {
94 for (int col = 0; col < ncols(); ++col) {
95 MathXArray const & c = xcell(index(row, col));
96 asc = max(asc, c.ascent());
97 desc = max(desc, c.descent());
99 rowinfo_[row].ascent_ = asc;
100 rowinfo_[row].descent_ = desc;
103 rowinfo_[row].offset_ =
104 rowinfo_[row - 1].offset_ +
105 rowinfo_[row - 1].descent_ +
107 rowinfo_[row].ascent_;
109 rowinfo_[row].offset_ = 0;
112 // adjust vertical offset
119 h = rowinfo_.back().offset_;
122 h = rowinfo_.back().offset_ / 2;
125 for (int row = 0; row < nrows(); ++row) {
126 rowinfo_[row].offset_ -= h;
127 rowinfo_[row].offset_ += MATH_BORDER;
130 // adjust horizontal structure
131 for (int col = 0; col < ncols(); ++col) {
133 for (int row = 0; row < nrows(); ++row)
134 wid = max(wid, xcell(index(row, col)).width());
135 colinfo_[col].width_ = wid;
136 colinfo_[col].offset_ = colinfo_[col].width_;
139 colinfo_[col].offset_ =
140 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
142 colinfo_[col].offset_ = 0;
144 colinfo_[col].offset_ += MATH_BORDER;
147 width_ = colinfo_.back().offset_ + colinfo_.back().width_;
148 ascent_ = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
149 descent_ = rowinfo_.back().offset_ + rowinfo_.back().descent_;
152 // Increase ws_[i] for 'R' columns (except the first one)
153 for (int i = 1; i < nc_; ++i)
154 if (h_align_[i] == 'R')
155 ws_[i] += 10 * df_width;
156 // Increase ws_[i] for 'C' column
157 if (h_align_[0] == 'C')
158 if (ws_[0] < 7 * workwidth / 8)
159 ws_[0] = 7 * workwidth / 8;
163 for (cxrow = row_.begin(); cxrow; ++cxrow) {
164 int rg = MATH_COLSEP;
166 for (int i = 0; i < nc_; ++i) {
168 if (cxrow->getTab(i) <= 0) {
169 cxrow->setTab(i, df_width);
172 switch (h_align_[i]) {
177 lf = (ws_[i] - cxrow->getTab(i))/2;
181 lf = ws_[i] - cxrow->getTab(i);
184 if (cxrow == row_.begin())
186 else if (cxrow.is_last())
187 lf = ws_[i] - cxrow->getTab(i);
189 lf = (ws_[i] - cxrow->getTab(i))/2;
192 int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
193 cxrow->setTab(i, lf + rg);
194 rg = ws_[i] - ww + MATH_COLSEP;
195 if (cxrow == row_.begin())
196 width += ws_[i] + MATH_COLSEP;
198 cxrow->setBaseline(cxrow->getBaseline() - ascent);
203 void MathGridInset::draw(Painter & pain, int x, int y)
207 for (int row = 0; row < nrows(); ++row) {
208 int yy = y + rowinfo_[row].offset_;
209 for (int col = 0; col < ncols(); ++col) {
210 int xx = x + colinfo_[col].offset_;
211 char align = colinfo_[col].h_align_;
212 if (align == 'r' || align == 'R')
213 xx += colinfo_[col].width_ - xcell(index(row, col)).width();
214 if (align == 'c' || align == 'C')
215 xx += (colinfo_[col].width_ - xcell(index(row, col)).width()) / 2;
216 xcell(index(row, col)).draw(pain, xx, yy);
222 void MathGridInset::Write(std::ostream & os, bool fragile) const
224 for (int row = 0; row < nrows(); ++row) {
227 for (int col = 0; col < ncols(); ++col) {
230 cell(index(row, col)).Write(os, fragile);
236 void MathGridInset::addRow(int row)
238 rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
239 cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
242 void MathGridInset::appendRow()
244 rowinfo_.push_back(RowInfo());
245 for (int i = 0; i < ncols(); ++i)
250 void MathGridInset::delRow(int row)
255 lyxerr << "delRow: nr: " << nrows() << " nc: " << ncols()
256 << " row: " << row << "\n";
258 cells_type::iterator it = cells_.begin() + row * ncols();
259 cells_.erase(it, it + ncols());
261 rowinfo_.erase(rowinfo_.begin() + row);
265 void MathGridInset::addCol(int newcol)
269 cells_type new_cells = cells_type((nc + 1) * nr);
271 for (int row = 0; row < nr; ++row)
272 for (int col = 0; col < nc; ++col)
273 new_cells[row * (nc + 1) + col + (col > newcol)]
274 = cells_[row * nc + col];
275 swap(cells_, new_cells);
277 colinfo_.insert(colinfo_.begin() + newcol);
281 void MathGridInset::delCol(int col)
287 for (int i = 0; i < nargs(); ++i)
288 if (i % ncols() != col)
289 tmpcells.push_back(cells_[i]);
290 swap(cells_, tmpcells);
292 colinfo_.erase(colinfo_.begin() + col);
296 bool MathGridInset::idxUp(int & idx, int & pos) const
306 bool MathGridInset::idxDown(int & idx, int & pos) const
308 if (idx >= ncols() * (nrows() - 1))
316 bool MathGridInset::idxLeft(int & idx, int & pos) const
318 // leave matrix if on the left hand edge
322 pos = cell(idx).size();
327 bool MathGridInset::idxRight(int & idx, int & pos) const
329 // leave matrix if on the right hand edge
330 if (col(idx) == ncols() - 1)
338 bool MathGridInset::idxFirst(int & idx, int & pos) const
345 idx = (nrows() - 1) * ncols();
348 idx = (nrows() / 2) * ncols();
355 bool MathGridInset::idxLast(int & idx, int & pos) const
365 idx = (nrows() / 2 + 1) * ncols() - 1;
367 pos = cell(idx).size();
373 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
379 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)