]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
44bd4816acbac89fed82ae9e6504e7ce661be56c
[lyx.git] / src / mathed / math_gridinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_gridinset.h"
6 #include "support/LOstream.h"
7 #include "debug.h"
8
9
10 namespace {
11
12 ///
13 int const MATH_COLSEP = 10;
14 ///
15 int const MATH_ROWSEP = 10;
16 ///
17 int const MATH_BORDER = 2;
18
19 }
20
21
22 MathGridInset::RowInfo::RowInfo()
23         : upperline_(false), lowerline_(false)
24 {}
25
26
27 MathGridInset::ColInfo::ColInfo()
28         : h_align_('c'), leftline_(false), rightline_(false)
29 {}
30
31
32 MathGridInset::MathGridInset(int m, int n, string const & nm)
33         : MathNestInset(m * n, nm), rowinfo_(n), colinfo_(m), v_align_('c')
34 {
35         if (m <= 0)
36                 lyxerr << "positve number of columns expected\n";
37         if (n <= 0)
38                 lyxerr << "positve number of rows expected\n";
39 }
40
41
42 int MathGridInset::index(int row, int col) const
43 {
44         return col + ncols() * row;
45 }
46
47
48 void MathGridInset::halign(string const & hh)
49 {
50         int n = hh.size();
51         if (n > ncols())
52                 n = ncols();
53         for (int i = 0; i < n; ++i)
54                 colinfo_[i].h_align_ = hh[i];
55 }
56
57 void MathGridInset::halign(char h, int col)
58 {
59         colinfo_[col].h_align_ = h;
60 }
61
62 char MathGridInset::halign(int col) const
63 {
64         return colinfo_[col].h_align_;
65 }
66
67 void MathGridInset::valign(char c)
68 {
69         v_align_ = c;
70 }
71
72 char MathGridInset::valign() const
73 {
74         return v_align_;
75 }
76
77 void MathGridInset::metrics(MathStyles st)
78 {
79         // let the cells adjust themselves
80         MathNestInset::metrics(st);
81         size_ = st;
82
83         // adjust vertical structure
84         for (int row = 0; row < nrows(); ++row) {
85                 int asc  = 0;
86                 int desc = 0;
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());
91                 }
92                 rowinfo_[row].ascent_  = asc;
93                 rowinfo_[row].descent_ = desc;
94
95                 if (row) 
96                         rowinfo_[row].offset_ = 
97                                 rowinfo_[row - 1].offset_ +
98                                 rowinfo_[row - 1].descent_ +
99                                 MATH_ROWSEP +
100                                 rowinfo_[row].ascent_;
101                 else 
102                         rowinfo_[row].offset_ = 0;
103         }
104
105         // adjust vertical offset
106         int h = 0;
107         switch (v_align_) {
108         case 't':
109                 h = 0;
110                 break;
111         case 'b':
112                 h = rowinfo_.back().offset_;
113                 break;
114         default:
115                 h = rowinfo_.back().offset_ / 2;
116         }
117
118         for (int row = 0; row < nrows(); ++row) {
119                 rowinfo_[row].offset_ -= h;
120                 rowinfo_[row].offset_ += MATH_BORDER;
121         }
122         
123         // adjust horizontal structure
124         for (int col = 0; col < ncols(); ++col) {
125                 int wid  = 0;
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_;
130
131                 if (col) 
132                         colinfo_[col].offset_ =
133                                 colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
134                 else
135                         colinfo_[col].offset_ = 0;
136
137                 colinfo_[col].offset_ += MATH_BORDER;
138         }
139
140         width_   =   colinfo_.back().offset_  + colinfo_.back().width_;
141         ascent_  = - rowinfo_.front().offset_ + rowinfo_.front().ascent_;
142         descent_ =   rowinfo_.back().offset_  + rowinfo_.back().descent_;
143         
144 /*      
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;
153         
154         // Adjust local tabs
155         width = MATH_COLSEP;
156         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
157                 int rg = MATH_COLSEP;
158                 int lf = 0;
159                 for (int i = 0; i < nc_; ++i) {
160                         bool isvoid = false;
161                         if (cxrow->getTab(i) <= 0) {
162                                 cxrow->setTab(i, df_width);
163                                 isvoid = true;
164                         }
165                         switch (h_align_[i]) {
166                         case 'l':
167                                 lf = 0;
168                                 break;
169                         case 'c':
170                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
171                                 break;
172                         case 'r':
173                         case 'R':
174                                 lf = ws_[i] - cxrow->getTab(i);
175                                 break;
176                         case 'C':
177                                 if (cxrow == row_.begin())
178                                         lf = 0;
179                                 else if (cxrow.is_last())
180                                         lf = ws_[i] - cxrow->getTab(i);
181                                 else
182                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
183                                 break;
184                         }
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;
190                 }
191                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
192         }
193 */
194 }
195
196 void MathGridInset::draw(Painter & pain, int x, int y)
197 {
198         xo(x);
199         yo(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);
210                 }
211         }
212 }
213
214
215 void MathGridInset::write(std::ostream & os, bool fragile) const
216 {
217         for (int row = 0; row < nrows(); ++row) {
218                 if (row)
219                         os << " \\\\\n";
220                 for (int col = 0; col < ncols(); ++col) {
221                         if (col)
222                                 os << " & ";
223                         cell(index(row, col)).write(os, fragile);
224                 }
225         }
226 }
227
228
229 void MathGridInset::addRow(int row)
230 {
231         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
232         cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
233 }
234
235 void MathGridInset::appendRow()
236 {
237         rowinfo_.push_back(RowInfo());
238         for (int i = 0; i < ncols(); ++i)
239                 cells_.push_back(cells_type::value_type());
240 }
241
242
243 void MathGridInset::delRow(int row)
244 {
245         if (nrows() == 1)
246                 return;
247
248         cells_type::iterator it = cells_.begin() + row * ncols(); 
249         cells_.erase(it, it + ncols());
250
251         rowinfo_.erase(rowinfo_.begin() + row);
252 }
253
254
255 void MathGridInset::addCol(int newcol)
256 {
257         int const nc = ncols();
258         int const nr = nrows();
259         cells_type new_cells((nc + 1) * nr);
260         
261         for (int row = 0; row < nr; ++row)
262                 for (int col = 0; col < nc; ++col)
263                         new_cells[row * (nc + 1) + col + (col > newcol)]
264                                 = cells_[row * nc + col];
265         std::swap(cells_, new_cells);
266
267         colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
268 }
269
270
271 void MathGridInset::delCol(int col)
272 {
273         if (ncols() == 1)
274                 return;
275
276         cells_type tmpcells;
277         for (int i = 0; i < nargs(); ++i) 
278                 if (i % ncols() != col)
279                         tmpcells.push_back(cells_[i]);
280         std::swap(cells_, tmpcells);
281
282         colinfo_.erase(colinfo_.begin() + col);
283 }
284
285
286 bool MathGridInset::idxUp(int & idx, int & pos) const
287 {
288         if (idx < ncols())
289                 return false;
290         idx -= ncols();
291         pos = 0;
292         return true;
293 }
294
295         
296 bool MathGridInset::idxDown(int & idx, int & pos) const
297 {
298         if (idx >= ncols() * (nrows() - 1))
299                 return false;
300         idx += ncols();
301         pos = 0;
302         return true;
303 }
304         
305         
306 bool MathGridInset::idxLeft(int & idx, int & pos) const
307 {
308         // leave matrix if on the left hand edge
309         if (col(idx) == 0)
310                 return false;
311         idx--;
312         pos = cell(idx).size();
313         return true;
314 }
315         
316         
317 bool MathGridInset::idxRight(int & idx, int & pos) const
318 {
319         // leave matrix if on the right hand edge
320         if (col(idx) == ncols() - 1)
321                 return false;
322         idx++;
323         pos = 0;
324         return true;
325 }
326
327
328 bool MathGridInset::idxFirst(int & idx, int & pos) const
329 {
330         switch (v_align_) {
331                 case 't':
332                         idx = 0;
333                         break;
334                 case 'b':
335                         idx = (nrows() - 1) * ncols();
336                         break;
337                 default: 
338                         idx = (nrows() / 2) * ncols();
339         }
340         pos = 0;
341         return true;
342 }
343
344
345 bool MathGridInset::idxLast(int & idx, int & pos) const
346 {
347         switch (v_align_) {
348                 case 't':
349                         idx = ncols() - 1;
350                         break;
351                 case 'b':
352                         idx = nargs() - 1;
353                         break;
354                 default:
355                         idx = (nrows() / 2 + 1) * ncols() - 1;
356         }
357         pos = cell(idx).size();
358         return true;
359 }
360
361
362 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
363 {
364         popit    = false;
365         deleteit = false;
366
367         // delete entire row if in first cell of empty row
368         if (col(idx) == 0 && nrows() > 1) {
369                 bool deleterow = true;
370                 for (int i = idx; i < idx + ncols(); ++i)
371                         if (cell(i).size()) {
372                                 deleterow = false;
373                                 break;
374                         }
375                 if (deleterow) 
376                         delRow(row(idx));
377
378                 if (idx >= nargs())
379                         idx = nargs() - 1;
380                 return;
381         }
382
383         // undo effect of Ctrl-Tab (i.e. pull next cell)
384         //if (idx != nargs() - 1) 
385         //      cell(idx).swap(cell(idx + 1));
386 }
387
388
389 void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
390 {
391 // leave this unimplemented unless someone wants to have it.
392 /*
393         int n = (to - from) / ncols();
394         int r = from / ncols();
395
396         if (n >= 1) {
397                 cells_type::iterator it = cells_.begin() + from;
398                 cells_.erase(it, it + n * ncols());
399                 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
400         }
401 */
402 }
403
404
405 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
406 {
407         return rowinfo_[i];
408 }
409
410
411 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
412 {
413         return rowinfo_[i];
414 }
415
416
417 std::vector<int> MathGridInset::idxBetween(int from, int to) const
418 {
419         int r1 = std::min(row(from), row(to));
420         int r2 = std::max(row(from), row(to));
421         int c1 = std::min(col(from), col(to));
422         int c2 = std::max(col(from), col(to));
423         std::vector<int> res;
424         for (int i = r1; i <= r2; ++i)
425                 for (int j = c1; j <= c2; ++j)
426                         res.push_back(index(i, j));
427         return res;
428 }