]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
proper cursor up/down for centered and right aligned grid columns
[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) const
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
197 void MathGridInset::draw(Painter & pain, int x, int y) const
198 {
199         xo(x);
200         yo(y);
201         for (int idx = 0; idx < nargs(); ++idx)
202                 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
203 }
204
205
206 void MathGridInset::write(std::ostream & os, bool fragile) const
207 {
208         for (int row = 0; row < nrows(); ++row) {
209                 if (row)
210                         os << " \\\\\n";
211                 for (int col = 0; col < ncols(); ++col) {
212                         if (col)
213                                 os << " & ";
214                         cell(index(row, col)).write(os, fragile);
215                 }
216         }
217 }
218
219
220 void MathGridInset::addRow(int row)
221 {
222         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
223         cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
224 }
225
226 void MathGridInset::appendRow()
227 {
228         rowinfo_.push_back(RowInfo());
229         for (int i = 0; i < ncols(); ++i)
230                 cells_.push_back(cells_type::value_type());
231 }
232
233
234 void MathGridInset::delRow(int row)
235 {
236         if (nrows() == 1)
237                 return;
238
239         cells_type::iterator it = cells_.begin() + row * ncols(); 
240         cells_.erase(it, it + ncols());
241
242         rowinfo_.erase(rowinfo_.begin() + row);
243 }
244
245
246 void MathGridInset::addCol(int newcol)
247 {
248         int const nc = ncols();
249         int const nr = nrows();
250         cells_type new_cells((nc + 1) * nr);
251         
252         for (int row = 0; row < nr; ++row)
253                 for (int col = 0; col < nc; ++col)
254                         new_cells[row * (nc + 1) + col + (col > newcol)]
255                                 = cells_[row * nc + col];
256         std::swap(cells_, new_cells);
257
258         colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
259 }
260
261
262 void MathGridInset::delCol(int col)
263 {
264         if (ncols() == 1)
265                 return;
266
267         cells_type tmpcells;
268         for (int i = 0; i < nargs(); ++i) 
269                 if (i % ncols() != col)
270                         tmpcells.push_back(cells_[i]);
271         std::swap(cells_, tmpcells);
272
273         colinfo_.erase(colinfo_.begin() + col);
274 }
275
276
277 int MathGridInset::cellXOffset(int idx) const
278 {
279         int c = col(idx);
280         int x = colinfo_[c].offset_;
281         char align = colinfo_[c].h_align_;
282         if (align == 'r' || align == 'R')
283                 x += colinfo_[c].width_ - xcell(idx).width(); 
284         if (align == 'c' || align == 'C')
285                 x += (colinfo_[c].width_ - xcell(idx).width()) / 2; 
286         return x;
287 }
288
289
290 int MathGridInset::cellYOffset(int idx) const
291 {
292         return rowinfo_[row(idx)].offset_;
293 }
294
295 bool MathGridInset::idxUp(int & idx, int & pos) const
296 {
297         if (idx < ncols())
298                 return false;
299         idx -= ncols();
300         pos = 0;
301         return true;
302 }
303
304         
305 bool MathGridInset::idxDown(int & idx, int & pos) const
306 {
307         if (idx >= ncols() * (nrows() - 1))
308                 return false;
309         idx += ncols();
310         pos = 0;
311         return true;
312 }
313         
314         
315 bool MathGridInset::idxLeft(int & idx, int & pos) const
316 {
317         // leave matrix if on the left hand edge
318         if (col(idx) == 0)
319                 return false;
320         idx--;
321         pos = cell(idx).size();
322         return true;
323 }
324         
325         
326 bool MathGridInset::idxRight(int & idx, int & pos) const
327 {
328         // leave matrix if on the right hand edge
329         if (col(idx) == ncols() - 1)
330                 return false;
331         idx++;
332         pos = 0;
333         return true;
334 }
335
336
337 bool MathGridInset::idxFirst(int & idx, int & pos) const
338 {
339         switch (v_align_) {
340                 case 't':
341                         idx = 0;
342                         break;
343                 case 'b':
344                         idx = (nrows() - 1) * ncols();
345                         break;
346                 default: 
347                         idx = (nrows() / 2) * ncols();
348         }
349         pos = 0;
350         return true;
351 }
352
353
354 bool MathGridInset::idxLast(int & idx, int & pos) const
355 {
356         switch (v_align_) {
357                 case 't':
358                         idx = ncols() - 1;
359                         break;
360                 case 'b':
361                         idx = nargs() - 1;
362                         break;
363                 default:
364                         idx = (nrows() / 2 + 1) * ncols() - 1;
365         }
366         pos = cell(idx).size();
367         return true;
368 }
369
370
371 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
372 {
373         popit    = false;
374         deleteit = false;
375
376         // delete entire row if in first cell of empty row
377         if (col(idx) == 0 && nrows() > 1) {
378                 bool deleterow = true;
379                 for (int i = idx; i < idx + ncols(); ++i)
380                         if (cell(i).size()) {
381                                 deleterow = false;
382                                 break;
383                         }
384                 if (deleterow) 
385                         delRow(row(idx));
386
387                 if (idx >= nargs())
388                         idx = nargs() - 1;
389                 return;
390         }
391
392         // undo effect of Ctrl-Tab (i.e. pull next cell)
393         //if (idx != nargs() - 1) 
394         //      cell(idx).swap(cell(idx + 1));
395 }
396
397
398 void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
399 {
400 // leave this unimplemented unless someone wants to have it.
401 /*
402         int n = (to - from) / ncols();
403         int r = from / ncols();
404
405         if (n >= 1) {
406                 cells_type::iterator it = cells_.begin() + from;
407                 cells_.erase(it, it + n * ncols());
408                 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
409         }
410 */
411 }
412
413
414 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
415 {
416         return rowinfo_[i];
417 }
418
419
420 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
421 {
422         return rowinfo_[i];
423 }
424
425
426 std::vector<int> MathGridInset::idxBetween(int from, int to) const
427 {
428         int r1 = std::min(row(from), row(to));
429         int r2 = std::max(row(from), row(to));
430         int c1 = std::min(col(from), col(to));
431         int c2 = std::max(col(from), col(to));
432         std::vector<int> res;
433         for (int i = r1; i <= r2; ++i)
434                 for (int j = c1; j <= c2; ++j)
435                         res.push_back(index(i, j));
436         return res;
437 }