]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
Reduce Michael's buglist.
[lyx.git] / src / mathed / math_gridinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_gridinset.h"
6 #include "math_mathmlstream.h"
7 #include "lyxfont.h"
8 #include "Painter.h"
9 #include "debug.h"
10
11
12 namespace {
13
14 ///
15 int const COLSEP = 6;
16 ///
17 int const ROWSEP = 6;
18 ///
19 int const HLINESEP = 3;
20 ///
21 int const VLINESEP = 3;
22 ///
23 int const BORDER = 2;
24
25
26 string verboseHLine(int n)
27 {
28         string res;
29         for (int i = 0; i < n; ++i)
30                 res += "\\hline";
31         return res + ' ';
32 }
33
34 }
35
36
37 ////////////////////////////////////////////////////////////// 
38
39
40 MathGridInset::RowInfo::RowInfo()
41         : skip_(0), lines_(0)
42 {}
43
44
45
46 int MathGridInset::RowInfo::skipPixels() const
47 {
48 #ifdef WITH_WARNINGS
49 #warning fix this once the interface to LyXLength has improved
50 #endif
51         return int(crskip_.value());
52 }
53
54
55
56 ////////////////////////////////////////////////////////////// 
57
58
59 MathGridInset::ColInfo::ColInfo()
60         : align_('c'), leftline_(false), rightline_(false), lines_(0)
61 {}
62
63
64 ////////////////////////////////////////////////////////////// 
65
66
67 MathGridInset::MathGridInset(char v, string const & h)
68         : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
69 {
70         setDefaults();
71         valign(v);
72         halign(h);
73 }
74
75
76 MathGridInset::MathGridInset(col_type m, row_type n)
77         : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
78 {
79         setDefaults();
80 }
81
82
83 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
84         : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
85 {
86         setDefaults();
87         valign(v);
88         halign(h);
89 }
90
91
92 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
93 {
94         return col + ncols() * row;
95 }
96
97
98 void MathGridInset::setDefaults()
99 {
100         if (ncols() <= 0)
101                 lyxerr << "positive number of columns expected\n";
102         if (nrows() <= 0)
103                 lyxerr << "positive number of rows expected\n";
104         for (col_type col = 0; col < ncols(); ++col) {
105                 colinfo_[col].align_ = defaultColAlign(col);
106                 colinfo_[col].skip_  = defaultColSpace(col);
107         }
108 }
109
110
111 void MathGridInset::halign(string const & hh)
112 {
113         col_type col = 0;
114         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
115                 char c = *it;
116                 if (c == '|') {
117                         colinfo_[col].lines_++;
118                 } else if (c == 'c' || c == 'l' || c == 'r') {
119                         colinfo_[col].align_ = c;
120                         ++col;
121                         colinfo_[col].lines_ = 0;
122                 } else {
123                         lyxerr << "unkown column separator: '" << c << "'\n";
124                 }
125         }
126                         
127 /*
128         col_type n = hh.size();
129         if (n > ncols())
130                 n = ncols();
131         for (col_type col = 0; col < n; ++col)
132                 colinfo_[col].align_ = hh[col];
133 */
134 }
135
136
137 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
138 {
139         col_type col = 0;
140         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
141                 if (*it == 'c' || *it == 'l' || *it == 'r')
142                         ++col;
143         return col;
144 }
145
146
147 void MathGridInset::halign(char h, col_type col)
148 {
149         colinfo_[col].align_ = h;
150 }
151
152
153 char MathGridInset::halign(col_type col) const
154 {
155         return colinfo_[col].align_;
156 }
157
158
159 string MathGridInset::halign() const
160 {
161         string res;
162         for (col_type col = 0; col < ncols(); ++col) {
163                 res += string(colinfo_[col].lines_, '|');
164                 res += colinfo_[col].align_;
165         } 
166         return res + string(colinfo_[ncols()].lines_, '|');
167 }
168
169
170 void MathGridInset::valign(char c)
171 {
172         v_align_ = c;
173 }
174
175
176 char MathGridInset::valign() const
177 {
178         return v_align_;
179 }
180
181
182 MathGridInset::col_type MathGridInset::ncols() const
183 {
184         return colinfo_.size() - 1;
185 }
186
187
188 MathGridInset::row_type MathGridInset::nrows() const
189 {
190         return rowinfo_.size() - 1;
191 }
192
193
194 MathGridInset::col_type MathGridInset::col(idx_type idx) const
195 {
196         return idx % ncols();
197 }
198
199
200 MathGridInset::row_type MathGridInset::row(idx_type idx) const
201 {
202         return idx / ncols();
203 }
204
205
206 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
207 {
208         rowinfo_[row].crskip_ = crskip;
209 }
210
211
212 LyXLength MathGridInset::vcrskip(row_type row) const
213 {
214         return rowinfo_[row].crskip_;
215 }
216
217
218 void MathGridInset::metrics(MathMetricsInfo const & mi) const
219 {
220         // let the cells adjust themselves
221         MathNestInset::metrics(mi);
222
223         // adjust vertical structure
224         rowinfo_[0].offset_ = 0;
225         for (row_type row = 0; row < nrows(); ++row) {
226                 int asc  = 0;
227                 int desc = 0;
228                 for (col_type col = 0; col < ncols(); ++col) {
229                         MathXArray const & c = xcell(index(row, col));
230                         asc  = std::max(asc,  c.ascent());
231                         desc = std::max(desc, c.descent());
232                 }
233                 rowinfo_[row].ascent_  = asc;
234                 rowinfo_[row].descent_ = desc;
235                 rowinfo_[row].offset_  += asc + HLINESEP * rowinfo_[row].lines_;
236
237                 rowinfo_[row + 1].offset_ =
238                                 rowinfo_[row].offset_ +
239                                 rowinfo_[row].descent_ +
240                                 rowinfo_[row].skipPixels() +
241                                 ROWSEP;
242         }
243         rowinfo_[nrows()].ascent_  = 0;
244         rowinfo_[nrows()].descent_ = 0;
245         rowinfo_[nrows()].offset_  += HLINESEP * rowinfo_[nrows()].lines_;
246
247         // adjust vertical offset
248         int h = 0;
249         switch (v_align_) {
250                 case 't':
251                         h = 0;
252                         break;
253                 case 'b':
254                         h = rowinfo_[nrows()].offset_;
255                         break;
256                 default:
257                         h = rowinfo_[nrows()].offset_ / 2;
258                         //lyxerr << "\nnrows: " << nrows() << ' ' << ncols() << '\n';
259         }
260         for (row_type row = 0; row <= nrows(); ++row)
261                 rowinfo_[row].offset_ -= h;
262
263         
264         // adjust horizontal structure
265         colinfo_[0].offset_ = BORDER;
266         for (col_type col = 0; col < ncols(); ++col) {
267                 int wid = 0;
268                 for (row_type row = 0; row < nrows(); ++row) 
269                         wid = std::max(wid, xcell(index(row, col)).width());
270                 colinfo_[col].width_  = wid;
271                 colinfo_[col].offset_ += VLINESEP * colinfo_[col].lines_;
272
273                 colinfo_[col + 1].offset_ =
274                         colinfo_[col].offset_ +
275                         colinfo_[col].width_ + 
276                         colinfo_[col].skip_ +
277                         COLSEP;
278         }
279         colinfo_[ncols()].width_  = 0;
280         colinfo_[ncols()].offset_ += VLINESEP * colinfo_[ncols()].lines_;
281
282
283         width_   =   colinfo_[ncols() - 1].offset_      
284                        + colinfo_[ncols() - 1].width_
285                  + VLINESEP * colinfo_[ncols()].lines_
286                        + BORDER;
287
288         ascent_  = - rowinfo_[0].offset_          
289                        + rowinfo_[0].ascent_
290                  + HLINESEP * rowinfo_[0].lines_
291                        + BORDER;
292
293         descent_ =   rowinfo_[nrows() - 1].offset_
294                        + rowinfo_[nrows() - 1].descent_
295                  + HLINESEP * rowinfo_[nrows()].lines_
296                        + BORDER;
297
298
299 /*      
300         // Increase ws_[i] for 'R' columns (except the first one)
301         for (int i = 1; i < nc_; ++i)
302                 if (align_[i] == 'R')
303                         ws_[i] += 10 * df_width;
304         // Increase ws_[i] for 'C' column
305         if (align_[0] == 'C')
306                 if (ws_[0] < 7 * workwidth / 8)
307                         ws_[0] = 7 * workwidth / 8;
308         
309         // Adjust local tabs
310         width = COLSEP;
311         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
312                 int rg = COLSEP;
313                 int lf = 0;
314                 for (int i = 0; i < nc_; ++i) {
315                         bool isvoid = false;
316                         if (cxrow->getTab(i) <= 0) {
317                                 cxrow->setTab(i, df_width);
318                                 isvoid = true;
319                         }
320                         switch (align_[i]) {
321                         case 'l':
322                                 lf = 0;
323                                 break;
324                         case 'c':
325                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
326                                 break;
327                         case 'r':
328                         case 'R':
329                                 lf = ws_[i] - cxrow->getTab(i);
330                                 break;
331                         case 'C':
332                                 if (cxrow == row_.begin())
333                                         lf = 0;
334                                 else if (cxrow.is_last())
335                                         lf = ws_[i] - cxrow->getTab(i);
336                                 else
337                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
338                                 break;
339                         }
340                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
341                         cxrow->setTab(i, lf + rg);
342                         rg = ws_[i] - ww + COLSEP;
343                         if (cxrow == row_.begin())
344                                 width += ws_[i] + COLSEP;
345                 }
346                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
347         }
348 */
349 }
350
351
352 void MathGridInset::draw(Painter & pain, int x, int y) const
353 {
354         for (idx_type idx = 0; idx < nargs(); ++idx)
355                 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
356
357         for (row_type row = 0; row <= nrows(); ++row)
358                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
359                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
360                                 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
361                         //lyxerr << "i: " << i << " yy: " << yy << '\n';
362                         pain.line(x + 1, yy, x + width_ - 1, yy);
363                 }
364
365         for (col_type col = 0; col <= ncols(); ++col)
366                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
367                         int xx = x + colinfo_[col].offset_
368                                 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
369                         //lyxerr << "i: " << i << " xx: " << xx << '\n';
370                         pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
371                 }
372 }
373
374
375 string MathGridInset::eolString(row_type row) const
376 {
377         string eol;
378
379         if (rowinfo_[row].crskip_.value() != 0)
380                 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
381
382         // make sure an upcoming '[' does not break anything
383         if (row + 1 < nrows()) {
384                 MathArray const & c = cell(index(row + 1, 0));
385                 if (c.size() && c.front()->getChar() == '[')
386                         eol += "[0pt]";
387         }
388
389         // only add \\ if necessary
390         if (eol.empty() && row + 1 == nrows())
391                 return string();
392
393         return "\\\\" + eol + '\n';
394 }
395
396
397 string MathGridInset::eocString(col_type col) const
398 {
399         if (col + 1 == ncols())
400                 return string();
401         return " & ";
402 }
403
404
405 void MathGridInset::addRow(row_type row)
406 {
407         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
408         cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
409 }
410
411
412 void MathGridInset::appendRow()
413 {
414         rowinfo_.push_back(RowInfo());
415         //cells_.insert(cells_.end(), ncols(), MathXArray());
416         for (col_type col = 0; col < ncols(); ++col)
417                 cells_.push_back(cells_type::value_type());
418 }
419
420
421 void MathGridInset::delRow(row_type row)
422 {
423         if (nrows() == 1)
424                 return;
425
426         cells_type::iterator it = cells_.begin() + row * ncols(); 
427         cells_.erase(it, it + ncols());
428
429         rowinfo_.erase(rowinfo_.begin() + row);
430 }
431
432
433 void MathGridInset::addCol(col_type newcol)
434 {
435         const col_type nc = ncols();
436         const row_type nr = nrows();
437         cells_type new_cells((nc + 1) * nr);
438         
439         for (row_type row = 0; row < nr; ++row)
440                 for (col_type col = 0; col < nc; ++col)
441                         new_cells[row * (nc + 1) + col + (col > newcol)]
442                                 = cells_[row * nc + col];
443         std::swap(cells_, new_cells);
444
445         ColInfo inf;
446         inf.skip_  = defaultColSpace(newcol);
447         inf.align_ = defaultColAlign(newcol);
448         colinfo_.insert(colinfo_.begin() + newcol, inf);
449 }
450
451
452 void MathGridInset::delCol(col_type col)
453 {
454         if (ncols() == 1)
455                 return;
456
457         cells_type tmpcells;
458         for (col_type i = 0; i < nargs(); ++i) 
459                 if (i % ncols() != col)
460                         tmpcells.push_back(cells_[i]);
461         std::swap(cells_, tmpcells);
462
463         colinfo_.erase(colinfo_.begin() + col);
464 }
465
466
467 int MathGridInset::cellXOffset(idx_type idx) const
468 {
469         col_type c = col(idx);
470         int x = colinfo_[c].offset_;
471         char align = colinfo_[c].align_;
472         if (align == 'r' || align == 'R')
473                 x += colinfo_[c].width_ - xcell(idx).width(); 
474         if (align == 'c' || align == 'C')
475                 x += (colinfo_[c].width_ - xcell(idx).width()) / 2; 
476         return x;
477 }
478
479
480 int MathGridInset::cellYOffset(idx_type idx) const
481 {
482         return rowinfo_[row(idx)].offset_;
483 }
484
485
486 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
487 {
488         if (idx < ncols())
489                 return false;
490         int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
491         idx -= ncols();
492         pos = xcell(idx).x2pos(x - cellXOffset(idx));
493         return true;
494 }
495
496         
497 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
498 {
499         if (idx >= ncols() * (nrows() - 1))
500                 return false;
501         int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
502         idx += ncols();
503         pos = xcell(idx).x2pos(x - cellXOffset(idx));
504         return true;
505 }
506         
507         
508 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
509 {
510         // leave matrix if on the left hand edge
511         if (col(idx) == 0)
512                 return false;
513         idx--;
514         pos = cell(idx).size();
515         return true;
516 }
517         
518         
519 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
520 {
521         // leave matrix if on the right hand edge
522         if (col(idx) + 1 == ncols())
523                 return false;
524         idx++;
525         pos = 0;
526         return true;
527 }
528
529
530 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
531 {
532         switch (v_align_) {
533                 case 't':
534                         idx = 0;
535                         break;
536                 case 'b':
537                         idx = (nrows() - 1) * ncols();
538                         break;
539                 default: 
540                         idx = (nrows() / 2) * ncols();
541         }
542         pos = 0;
543         return true;
544 }
545
546
547 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
548 {
549         switch (v_align_) {
550                 case 't':
551                         idx = ncols() - 1;
552                         break;
553                 case 'b':
554                         idx = nargs() - 1;
555                         break;
556                 default:
557                         idx = (nrows() / 2 + 1) * ncols() - 1;
558         }
559         pos = cell(idx).size();
560         return true;
561 }
562
563
564 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
565 {
566         if (pos > 0) {
567                 pos = 0;
568                 return true;
569         }
570         if (col(idx) > 0) {
571                 idx -= idx % ncols();
572                 pos = 0;
573                 return true;
574         }
575         if (idx > 0) {
576                 idx = 0;
577                 pos = 0;
578                 return true;
579         }
580         return false;
581 }
582
583
584 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
585 {
586         if (pos < cell(idx).size()) {
587                 pos = cell(idx).size();
588                 return true;
589         }
590         if (col(idx) < ncols() - 1) {
591                 idx = idx - idx % ncols() + ncols() - 1;
592                 pos = cell(idx).size();
593                 return true;
594         }
595         if (idx < nargs() - 1) {
596                 idx = nargs() - 1;
597                 pos = cell(idx).size();
598                 return true;
599         }
600         return false;
601 }
602
603
604 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
605 {
606         popit    = false;
607         deleteit = false;
608
609         // nothing to do if we are in the last row of the inset
610         if (row(idx) + 1 == nrows())
611                 return;
612
613         // try to delete entire sequence of ncols() empty cells if possible
614         for (idx_type i = idx; i < idx + ncols(); ++i)
615                 if (cell(i).size())
616                         return;
617
618         // move cells if necessary
619         for (idx_type i = index(row(idx), 0); i < idx; ++i)
620                 cell(i).swap(cell(i + ncols()));
621                 
622         delRow(row(idx));
623
624         if (idx >= nargs())
625                 idx = nargs() - 1;
626
627         // undo effect of Ctrl-Tab (i.e. pull next cell)
628         //if (idx + 1 != nargs()) 
629         //      cell(idx).swap(cell(idx + 1));
630 }
631
632
633 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
634 {
635 // leave this unimplemented unless someone wants to have it.
636 /*
637         int n = (to - from) / ncols();
638         int r = from / ncols();
639
640         if (n >= 1) {
641                 cells_type::iterator it = cells_.begin() + from;
642                 cells_.erase(it, it + n * ncols());
643                 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
644         }
645 */
646 }
647
648
649 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
650 {
651         return rowinfo_[row];
652 }
653
654
655 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
656 {
657         return rowinfo_[row];
658 }
659
660
661 std::vector<MathInset::idx_type>
662         MathGridInset::idxBetween(idx_type from, idx_type to) const
663 {
664         row_type r1 = std::min(row(from), row(to));
665         row_type r2 = std::max(row(from), row(to));
666         col_type c1 = std::min(col(from), col(to));
667         col_type c2 = std::max(col(from), col(to));
668         std::vector<idx_type> res;
669         for (row_type i = r1; i <= r2; ++i)
670                 for (col_type j = c1; j <= c2; ++j)
671                         res.push_back(index(i, j));
672         return res;
673 }
674
675
676
677 void MathGridInset::normalize(NormalStream & os) const
678 {
679         os << "[grid ";
680         for (row_type row = 0; row < nrows(); ++row) {
681                 os << "[row ";
682                 for (col_type col = 0; col < ncols(); ++col)
683                         os << "[cell " << cell(index(row, col)) << ']';
684                 os << ']';
685         }
686         os << ']';
687 }
688
689
690 void MathGridInset::mathmlize(MathMLStream & os) const
691 {
692         os << MTag("mtable");
693         for (row_type row = 0; row < nrows(); ++row) {
694                 os << MTag("mtr");
695                 for (col_type col = 0; col < ncols(); ++col) 
696                         os << cell(index(row, col));
697                 os << ETag("mtr");
698         }
699         os << ETag("mtable");
700 }
701
702
703 void MathGridInset::write(WriteStream & os) const
704 {
705         for (row_type row = 0; row < nrows(); ++row) {
706                 os << verboseHLine(rowinfo_[row].lines_).c_str();
707                 for (col_type col = 0; col < ncols(); ++col) 
708                         os << cell(index(row, col)) << eocString(col).c_str();
709                 os << eolString(row).c_str();
710         }
711         string s = verboseHLine(rowinfo_[nrows()].lines_);
712         if (s.size())
713                 os << "\\\\" << s.c_str();
714 }
715
716