]> git.lyx.org Git - lyx.git/blob - src/mathed/math_grid.C
prepare infrastructure for multicell selection
[lyx.git] / src / mathed / math_grid.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_grid.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, MathInsetTypes ot)
33         : MathInset(m * n, nm, ot), 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, int, int)
78 {
79         // let the cells adjust themselves
80         MathInset::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         lyxerr << "delRow: nr: " << nrows() << "  nc: " << ncols()
249                 << "  row: " << row << "\n";
250
251         cells_type::iterator it = cells_.begin() + row * ncols(); 
252         cells_.erase(it, it + ncols());
253
254         rowinfo_.erase(rowinfo_.begin() + row);
255 }
256
257
258 void MathGridInset::addCol(int newcol)
259 {
260         int const nc = ncols();
261         int const nr = nrows();
262         cells_type new_cells((nc + 1) * nr);
263         
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);
269
270         colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
271 }
272
273
274 void MathGridInset::delCol(int col)
275 {
276         if (ncols() == 1)
277                 return;
278
279         cells_type tmpcells;
280         for (int i = 0; i < nargs(); ++i) 
281                 if (i % ncols() != col)
282                         tmpcells.push_back(cells_[i]);
283         std::swap(cells_, tmpcells);
284
285         colinfo_.erase(colinfo_.begin() + col);
286 }
287
288
289 bool MathGridInset::idxUp(int & idx, int & pos) const
290 {
291         if (idx < ncols())
292                 return false;
293         idx -= ncols();
294         pos = 0;
295         return true;
296 }
297
298         
299 bool MathGridInset::idxDown(int & idx, int & pos) const
300 {
301         if (idx >= ncols() * (nrows() - 1))
302                 return false;
303         idx += ncols();
304         pos = 0;
305         return true;
306 }
307         
308         
309 bool MathGridInset::idxLeft(int & idx, int & pos) const
310 {
311         // leave matrix if on the left hand edge
312         if (col(idx) == 0)
313                 return false;
314         idx--;
315         pos = cell(idx).size();
316         return true;
317 }
318         
319         
320 bool MathGridInset::idxRight(int & idx, int & pos) const
321 {
322         // leave matrix if on the right hand edge
323         if (col(idx) == ncols() - 1)
324                 return false;
325         idx++;
326         pos = 0;
327         return true;
328 }
329
330
331 bool MathGridInset::idxFirst(int & idx, int & pos) const
332 {
333         switch (v_align_) {
334                 case 't':
335                         idx = 0;
336                         break;
337                 case 'b':
338                         idx = (nrows() - 1) * ncols();
339                         break;
340                 default: 
341                         idx = (nrows() / 2) * ncols();
342         }
343         pos = 0;
344         return true;
345 }
346
347
348 bool MathGridInset::idxLast(int & idx, int & pos) const
349 {
350         switch (v_align_) {
351                 case 't':
352                         idx = ncols() - 1;
353                         break;
354                 case 'b':
355                         idx = nargs() - 1;
356                         break;
357                 default:
358                         idx = (nrows() / 2 + 1) * ncols() - 1;
359         }
360         pos = cell(idx).size();
361         return true;
362 }
363
364
365 void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
366 {
367         popit    = false;
368         deleteit = false;
369
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()) {
375                                 deleterow = false;
376                                 break;
377                         }
378                 if (deleterow) 
379                         delRow(row(idx));
380
381                 if (idx >= nargs())
382                         idx = nargs() - 1;
383                 return;
384         }
385
386         // undo effect of Ctrl-Tab (i.e. pull next cell)
387         //if (idx != nargs() - 1) 
388         //      cell(idx).swap(cell(idx + 1));
389 }
390
391
392 MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
393 {
394         return rowinfo_[i];
395 }
396
397
398 MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
399 {
400         return rowinfo_[i];
401 }