]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
re-enable automatic deletion of empty super/subscripts;
[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 "math_streamstr.h"
8 #include "lyxfont.h"
9 #include "Painter.h"
10 #include "debug.h"
11
12
13 namespace {
14
15 ///
16 int const COLSEP = 6;
17 ///
18 int const ROWSEP = 6;
19 ///
20 int const HLINESEP = 3;
21 ///
22 int const VLINESEP = 3;
23 ///
24 int const BORDER = 2;
25
26
27 string verboseHLine(int n)
28 {
29         string res;
30         for (int i = 0; i < n; ++i)
31                 res += "\\hline";
32         return res + ' ';
33 }
34
35 }
36
37
38 ////////////////////////////////////////////////////////////// 
39
40
41 MathGridInset::RowInfo::RowInfo()
42         : lines_(0), skip_(0)
43 {}
44
45
46
47 int MathGridInset::RowInfo::skipPixels() const
48 {
49 #ifdef WITH_WARNINGS
50 #warning fix this once the interface to LyXLength has improved
51 #endif
52         return int(crskip_.value());
53 }
54
55
56
57 ////////////////////////////////////////////////////////////// 
58
59
60 MathGridInset::ColInfo::ColInfo()
61         : align_('c'), leftline_(false), rightline_(false), lines_(0)
62 {}
63
64
65 ////////////////////////////////////////////////////////////// 
66
67
68 MathGridInset::MathGridInset(char v, string const & h)
69         : MathNestInset(guessColumns(h)), rowinfo_(2), colinfo_(guessColumns(h) + 1)
70 {
71         setDefaults();
72         valign(v);
73         halign(h);
74 }
75
76
77 MathGridInset::MathGridInset(col_type m, row_type n)
78         : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_('c')
79 {
80         setDefaults();
81 }
82
83
84 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
85         : MathNestInset(m * n), rowinfo_(n + 1), colinfo_(m + 1), v_align_(v)
86 {
87         setDefaults();
88         valign(v);
89         halign(h);
90 }
91
92
93 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
94 {
95         return col + ncols() * row;
96 }
97
98
99 void MathGridInset::setDefaults()
100 {
101         if (ncols() <= 0)
102                 lyxerr << "positive number of columns expected\n";
103         if (nrows() <= 0)
104                 lyxerr << "positive number of rows expected\n";
105         for (col_type col = 0; col < ncols(); ++col) {
106                 colinfo_[col].align_ = defaultColAlign(col);
107                 colinfo_[col].skip_  = defaultColSpace(col);
108         }
109 }
110
111
112 void MathGridInset::halign(string const & hh)
113 {
114         col_type col = 0;
115         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
116                 char c = *it;
117                 if (c == '|') {
118                         colinfo_[col].lines_++;
119                 } else if (c == 'c' || c == 'l' || c == 'r') {
120                         colinfo_[col].align_ = c;
121                         ++col;
122                         colinfo_[col].lines_ = 0;
123                 } else {
124                         lyxerr << "unkown column separator: '" << c << "'\n";
125                 }
126         }
127                         
128 /*
129         col_type n = hh.size();
130         if (n > ncols())
131                 n = ncols();
132         for (col_type col = 0; col < n; ++col)
133                 colinfo_[col].align_ = hh[col];
134 */
135 }
136
137
138 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
139 {
140         col_type col = 0;
141         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
142                 if (*it == 'c' || *it == 'l' || *it == 'r')
143                         ++col;
144         return col;
145 }
146
147
148 void MathGridInset::halign(char h, col_type col)
149 {
150         colinfo_[col].align_ = h;
151 }
152
153
154 char MathGridInset::halign(col_type col) const
155 {
156         return colinfo_[col].align_;
157 }
158
159
160 string MathGridInset::halign() const
161 {
162         string res;
163         for (col_type col = 0; col < ncols(); ++col) {
164                 res += string(colinfo_[col].lines_, '|');
165                 res += colinfo_[col].align_;
166         } 
167         return res + string(colinfo_[ncols()].lines_, '|');
168 }
169
170
171 void MathGridInset::valign(char c)
172 {
173         v_align_ = c;
174 }
175
176
177 char MathGridInset::valign() const
178 {
179         return v_align_;
180 }
181
182
183 MathGridInset::col_type MathGridInset::ncols() const
184 {
185         return colinfo_.size() - 1;
186 }
187
188
189 MathGridInset::row_type MathGridInset::nrows() const
190 {
191         return rowinfo_.size() - 1;
192 }
193
194
195 MathGridInset::col_type MathGridInset::col(idx_type idx) const
196 {
197         return idx % ncols();
198 }
199
200
201 MathGridInset::row_type MathGridInset::row(idx_type idx) const
202 {
203         return idx / ncols();
204 }
205
206
207 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
208 {
209         rowinfo_[row].crskip_ = crskip;
210 }
211
212
213 LyXLength MathGridInset::vcrskip(row_type row) const
214 {
215         return rowinfo_[row].crskip_;
216 }
217
218
219 void MathGridInset::metrics(MathMetricsInfo const & mi) const
220 {
221         // let the cells adjust themselves
222         MathNestInset::metrics(mi);
223
224         // compute absolute sizes of vertical structure
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         }
236         rowinfo_[0].ascent_       += HLINESEP * rowinfo_[0].lines_;
237         rowinfo_[nrows()].ascent_  = 0;
238         rowinfo_[nrows()].descent_ = 0;
239
240         // compute vertical offsets
241         rowinfo_[0].offset_ = 0;
242         for (row_type row = 1; row <= nrows(); ++row) {
243                 rowinfo_[row].offset_  =        
244                         rowinfo_[row - 1].offset_  +
245                         rowinfo_[row - 1].descent_ +
246                         rowinfo_[row - 1].skipPixels() +
247                         ROWSEP +
248                         rowinfo_[row].lines_ * HLINESEP +
249                         rowinfo_[row].ascent_;
250         }
251
252         // adjust vertical offset
253         int h = 0;
254         switch (v_align_) {
255                 case 't':
256                         h = 0;
257                         break;
258                 case 'b':
259                         h = rowinfo_[nrows() - 1].offset_;
260                         break;
261                 default:
262                         h = rowinfo_[nrows() - 1].offset_ / 2;
263         }
264         for (row_type row = 0; row <= nrows(); ++row)
265                 rowinfo_[row].offset_ -= h;
266         
267
268         // compute absolute sizes of horizontal structure
269         for (col_type col = 0; col < ncols(); ++col) {
270                 int wid = 0;
271                 for (row_type row = 0; row < nrows(); ++row) 
272                         wid = std::max(wid, xcell(index(row, col)).width());
273                 colinfo_[col].width_ = wid;
274         }
275         colinfo_[ncols()].width_  = 0;
276
277         // compute horizontal offsets
278         colinfo_[0].offset_ = BORDER;
279         for (col_type col = 1; col <= ncols(); ++col) {
280                 colinfo_[col].offset_ =
281                         colinfo_[col - 1].offset_ +
282                         colinfo_[col - 1].width_ + 
283                         colinfo_[col - 1].skip_ +
284                         COLSEP + 
285                         colinfo_[col].lines_ * VLINESEP;
286         }
287
288
289         width_   =   colinfo_[ncols() - 1].offset_      
290                        + colinfo_[ncols() - 1].width_
291                  + VLINESEP * colinfo_[ncols()].lines_
292                        + BORDER;
293
294         ascent_  = - rowinfo_[0].offset_          
295                        + rowinfo_[0].ascent_
296                  + HLINESEP * rowinfo_[0].lines_
297                        + BORDER;
298
299         descent_ =   rowinfo_[nrows() - 1].offset_
300                        + rowinfo_[nrows() - 1].descent_
301                  + HLINESEP * rowinfo_[nrows()].lines_
302                        + BORDER;
303
304
305 /*      
306         // Increase ws_[i] for 'R' columns (except the first one)
307         for (int i = 1; i < nc_; ++i)
308                 if (align_[i] == 'R')
309                         ws_[i] += 10 * df_width;
310         // Increase ws_[i] for 'C' column
311         if (align_[0] == 'C')
312                 if (ws_[0] < 7 * workwidth / 8)
313                         ws_[0] = 7 * workwidth / 8;
314         
315         // Adjust local tabs
316         width = COLSEP;
317         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
318                 int rg = COLSEP;
319                 int lf = 0;
320                 for (int i = 0; i < nc_; ++i) {
321                         bool isvoid = false;
322                         if (cxrow->getTab(i) <= 0) {
323                                 cxrow->setTab(i, df_width);
324                                 isvoid = true;
325                         }
326                         switch (align_[i]) {
327                         case 'l':
328                                 lf = 0;
329                                 break;
330                         case 'c':
331                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
332                                 break;
333                         case 'r':
334                         case 'R':
335                                 lf = ws_[i] - cxrow->getTab(i);
336                                 break;
337                         case 'C':
338                                 if (cxrow == row_.begin())
339                                         lf = 0;
340                                 else if (cxrow.is_last())
341                                         lf = ws_[i] - cxrow->getTab(i);
342                                 else
343                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
344                                 break;
345                         }
346                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
347                         cxrow->setTab(i, lf + rg);
348                         rg = ws_[i] - ww + COLSEP;
349                         if (cxrow == row_.begin())
350                                 width += ws_[i] + COLSEP;
351                 }
352                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
353         }
354 */
355 }
356
357
358 void MathGridInset::draw(Painter & pain, int x, int y) const
359 {
360         for (idx_type idx = 0; idx < nargs(); ++idx)
361                 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
362
363         for (row_type row = 0; row <= nrows(); ++row)
364                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
365                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
366                                 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
367                         pain.line(x + 1, yy, x + width_ - 1, yy);
368                 }
369
370         for (col_type col = 0; col <= ncols(); ++col)
371                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
372                         int xx = x + colinfo_[col].offset_
373                                 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
374                         pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
375                 }
376 }
377
378
379 string MathGridInset::eolString(row_type row) const
380 {
381         string eol;
382
383         if (!rowinfo_[row].crskip_.zero())
384                 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
385
386         // make sure an upcoming '[' does not break anything
387         if (row + 1 < nrows()) {
388                 MathArray const & c = cell(index(row + 1, 0));
389                 if (c.size() && c.front()->getChar() == '[')
390                         eol += "[0pt]";
391         }
392
393         // only add \\ if necessary
394         if (eol.empty() && row + 1 == nrows())
395                 return string();
396
397         return "\\\\" + eol + '\n';
398 }
399
400
401 string MathGridInset::eocString(col_type col) const
402 {
403         if (col + 1 == ncols())
404                 return string();
405         return " & ";
406 }
407
408
409 void MathGridInset::addRow(row_type row)
410 {
411         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
412         cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
413 }
414
415
416 void MathGridInset::appendRow()
417 {
418         rowinfo_.push_back(RowInfo());
419         //cells_.insert(cells_.end(), ncols(), MathXArray());
420         for (col_type col = 0; col < ncols(); ++col)
421                 cells_.push_back(cells_type::value_type());
422 }
423
424
425 void MathGridInset::delRow(row_type row)
426 {
427         if (nrows() == 1)
428                 return;
429
430         cells_type::iterator it = cells_.begin() + row * ncols(); 
431         cells_.erase(it, it + ncols());
432
433         rowinfo_.erase(rowinfo_.begin() + row);
434 }
435
436
437 void MathGridInset::addCol(col_type newcol)
438 {
439         const col_type nc = ncols();
440         const row_type nr = nrows();
441         cells_type new_cells((nc + 1) * nr);
442         
443         for (row_type row = 0; row < nr; ++row)
444                 for (col_type col = 0; col < nc; ++col)
445                         new_cells[row * (nc + 1) + col + (col > newcol)]
446                                 = cells_[row * nc + col];
447         std::swap(cells_, new_cells);
448
449         ColInfo inf;
450         inf.skip_  = defaultColSpace(newcol);
451         inf.align_ = defaultColAlign(newcol);
452         colinfo_.insert(colinfo_.begin() + newcol, inf);
453 }
454
455
456 void MathGridInset::delCol(col_type col)
457 {
458         if (ncols() == 1)
459                 return;
460
461         cells_type tmpcells;
462         for (col_type i = 0; i < nargs(); ++i) 
463                 if (i % ncols() != col)
464                         tmpcells.push_back(cells_[i]);
465         std::swap(cells_, tmpcells);
466
467         colinfo_.erase(colinfo_.begin() + col);
468 }
469
470
471 int MathGridInset::cellXOffset(idx_type idx) const
472 {
473         col_type c = col(idx);
474         int x = colinfo_[c].offset_;
475         char align = colinfo_[c].align_;
476         if (align == 'r' || align == 'R')
477                 x += colinfo_[c].width_ - xcell(idx).width(); 
478         if (align == 'c' || align == 'C')
479                 x += (colinfo_[c].width_ - xcell(idx).width()) / 2; 
480         return x;
481 }
482
483
484 int MathGridInset::cellYOffset(idx_type idx) const
485 {
486         return rowinfo_[row(idx)].offset_;
487 }
488
489
490 bool MathGridInset::idxUp(idx_type & idx) const
491 {
492         if (idx < ncols())
493                 return false;
494         idx -= ncols();
495         return true;
496 }
497
498         
499 bool MathGridInset::idxDown(idx_type & idx) const
500 {
501         if (idx >= ncols() * (nrows() - 1))
502                 return false;
503         idx += ncols();
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_);
707                 for (col_type col = 0; col < ncols(); ++col) 
708                         os << cell(index(row, col)) << eocString(col);
709                 os << eolString(row);
710         }
711         string const s = verboseHLine(rowinfo_[nrows()].lines_);
712         if (!s.empty())
713                 os << "\\\\" << s;
714 }
715