]> git.lyx.org Git - features.git/blob - src/mathed/math_gridinset.C
reduction of header dependencies, part II (use new types
[features.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         }
259         //lyxerr << "\nnrows: " << nrows() << " h: " << h << '\n';
260         for (row_type row = 0; row <= nrows(); ++row) {
261                 rowinfo_[row].offset_ -= h;
262                 //lyxerr << "row: " << row << " off: " << rowinfo_[row].offset_  << '\n';
263         }
264
265         
266         // adjust horizontal structure
267         colinfo_[0].offset_ = BORDER;
268         for (col_type col = 0; col < ncols(); ++col) {
269                 int wid = 0;
270                 for (row_type row = 0; row < nrows(); ++row) 
271                         wid = std::max(wid, xcell(index(row, col)).width());
272                 colinfo_[col].width_  = wid;
273                 colinfo_[col].offset_ += VLINESEP * colinfo_[col].lines_;
274
275                 colinfo_[col + 1].offset_ =
276                         colinfo_[col].offset_ +
277                         colinfo_[col].width_ + 
278                         colinfo_[col].skip_ +
279                         COLSEP;
280         }
281         colinfo_[ncols()].width_  = 0;
282         colinfo_[ncols()].offset_ += VLINESEP * colinfo_[ncols()].lines_;
283
284
285         width_   =   colinfo_[ncols() - 1].offset_      
286                        + colinfo_[ncols() - 1].width_
287                  + VLINESEP * colinfo_[ncols()].lines_
288                        + BORDER;
289
290         ascent_  = - rowinfo_[0].offset_          
291                        + rowinfo_[0].ascent_
292                  + HLINESEP * rowinfo_[0].lines_
293                        + BORDER;
294
295         descent_ =   rowinfo_[nrows() - 1].offset_
296                        + rowinfo_[nrows() - 1].descent_
297                  + HLINESEP * rowinfo_[nrows()].lines_
298                        + BORDER;
299
300
301 /*      
302         // Increase ws_[i] for 'R' columns (except the first one)
303         for (int i = 1; i < nc_; ++i)
304                 if (align_[i] == 'R')
305                         ws_[i] += 10 * df_width;
306         // Increase ws_[i] for 'C' column
307         if (align_[0] == 'C')
308                 if (ws_[0] < 7 * workwidth / 8)
309                         ws_[0] = 7 * workwidth / 8;
310         
311         // Adjust local tabs
312         width = COLSEP;
313         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
314                 int rg = COLSEP;
315                 int lf = 0;
316                 for (int i = 0; i < nc_; ++i) {
317                         bool isvoid = false;
318                         if (cxrow->getTab(i) <= 0) {
319                                 cxrow->setTab(i, df_width);
320                                 isvoid = true;
321                         }
322                         switch (align_[i]) {
323                         case 'l':
324                                 lf = 0;
325                                 break;
326                         case 'c':
327                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
328                                 break;
329                         case 'r':
330                         case 'R':
331                                 lf = ws_[i] - cxrow->getTab(i);
332                                 break;
333                         case 'C':
334                                 if (cxrow == row_.begin())
335                                         lf = 0;
336                                 else if (cxrow.is_last())
337                                         lf = ws_[i] - cxrow->getTab(i);
338                                 else
339                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
340                                 break;
341                         }
342                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
343                         cxrow->setTab(i, lf + rg);
344                         rg = ws_[i] - ww + COLSEP;
345                         if (cxrow == row_.begin())
346                                 width += ws_[i] + COLSEP;
347                 }
348                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
349         }
350 */
351 }
352
353
354 void MathGridInset::draw(Painter & pain, int x, int y) const
355 {
356         for (idx_type idx = 0; idx < nargs(); ++idx)
357                 xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
358
359         for (row_type row = 0; row <= nrows(); ++row)
360                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
361                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
362                                 - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
363                         //lyxerr << "i: " << i << " yy: " << yy << '\n';
364                         pain.line(x + 1, yy, x + width_ - 1, yy);
365                 }
366
367         for (col_type col = 0; col <= ncols(); ++col)
368                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
369                         int xx = x + colinfo_[col].offset_
370                                 - i * VLINESEP - VLINESEP/2 - COLSEP/2;
371                         //lyxerr << "i: " << i << " xx: " << xx << '\n';
372                         pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
373                 }
374 }
375
376
377 string MathGridInset::eolString(row_type row) const
378 {
379         string eol;
380
381         if (rowinfo_[row].crskip_.value() != 0)
382                 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
383
384         // make sure an upcoming '[' does not break anything
385         if (row + 1 < nrows()) {
386                 MathArray const & c = cell(index(row + 1, 0));
387                 if (c.size() && c.front()->getChar() == '[')
388                         eol += "[0pt]";
389         }
390
391         // only add \\ if necessary
392         if (eol.empty() && row + 1 == nrows())
393                 return string();
394
395         return "\\\\" + eol + '\n';
396 }
397
398
399 string MathGridInset::eocString(col_type col) const
400 {
401         if (col + 1 == ncols())
402                 return string();
403         return " & ";
404 }
405
406
407 void MathGridInset::addRow(row_type row)
408 {
409         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
410         cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
411 }
412
413
414 void MathGridInset::appendRow()
415 {
416         rowinfo_.push_back(RowInfo());
417         //cells_.insert(cells_.end(), ncols(), MathXArray());
418         for (col_type col = 0; col < ncols(); ++col)
419                 cells_.push_back(cells_type::value_type());
420 }
421
422
423 void MathGridInset::delRow(row_type row)
424 {
425         if (nrows() == 1)
426                 return;
427
428         cells_type::iterator it = cells_.begin() + row * ncols(); 
429         cells_.erase(it, it + ncols());
430
431         rowinfo_.erase(rowinfo_.begin() + row);
432 }
433
434
435 void MathGridInset::addCol(col_type newcol)
436 {
437         const col_type nc = ncols();
438         const row_type nr = nrows();
439         cells_type new_cells((nc + 1) * nr);
440         
441         for (row_type row = 0; row < nr; ++row)
442                 for (col_type col = 0; col < nc; ++col)
443                         new_cells[row * (nc + 1) + col + (col > newcol)]
444                                 = cells_[row * nc + col];
445         std::swap(cells_, new_cells);
446
447         ColInfo inf;
448         inf.skip_  = defaultColSpace(newcol);
449         inf.align_ = defaultColAlign(newcol);
450         colinfo_.insert(colinfo_.begin() + newcol, inf);
451 }
452
453
454 void MathGridInset::delCol(col_type col)
455 {
456         if (ncols() == 1)
457                 return;
458
459         cells_type tmpcells;
460         for (col_type i = 0; i < nargs(); ++i) 
461                 if (i % ncols() != col)
462                         tmpcells.push_back(cells_[i]);
463         std::swap(cells_, tmpcells);
464
465         colinfo_.erase(colinfo_.begin() + col);
466 }
467
468
469 int MathGridInset::cellXOffset(idx_type idx) const
470 {
471         col_type c = col(idx);
472         int x = colinfo_[c].offset_;
473         char align = colinfo_[c].align_;
474         if (align == 'r' || align == 'R')
475                 x += colinfo_[c].width_ - xcell(idx).width(); 
476         if (align == 'c' || align == 'C')
477                 x += (colinfo_[c].width_ - xcell(idx).width()) / 2; 
478         return x;
479 }
480
481
482 int MathGridInset::cellYOffset(idx_type idx) const
483 {
484         return rowinfo_[row(idx)].offset_;
485 }
486
487
488 bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
489 {
490         if (idx < ncols())
491                 return false;
492         int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
493         idx -= ncols();
494         pos = xcell(idx).x2pos(x - cellXOffset(idx));
495         return true;
496 }
497
498         
499 bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
500 {
501         if (idx >= ncols() * (nrows() - 1))
502                 return false;
503         int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
504         idx += ncols();
505         pos = xcell(idx).x2pos(x - cellXOffset(idx));
506         return true;
507 }
508         
509         
510 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
511 {
512         // leave matrix if on the left hand edge
513         if (col(idx) == 0)
514                 return false;
515         idx--;
516         pos = cell(idx).size();
517         return true;
518 }
519         
520         
521 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
522 {
523         // leave matrix if on the right hand edge
524         if (col(idx) + 1 == ncols())
525                 return false;
526         idx++;
527         pos = 0;
528         return true;
529 }
530
531
532 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
533 {
534         switch (v_align_) {
535                 case 't':
536                         idx = 0;
537                         break;
538                 case 'b':
539                         idx = (nrows() - 1) * ncols();
540                         break;
541                 default: 
542                         idx = (nrows() / 2) * ncols();
543         }
544         pos = 0;
545         return true;
546 }
547
548
549 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
550 {
551         switch (v_align_) {
552                 case 't':
553                         idx = ncols() - 1;
554                         break;
555                 case 'b':
556                         idx = nargs() - 1;
557                         break;
558                 default:
559                         idx = (nrows() / 2 + 1) * ncols() - 1;
560         }
561         pos = cell(idx).size();
562         return true;
563 }
564
565
566 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
567 {
568         if (pos > 0) {
569                 pos = 0;
570                 return true;
571         }
572         if (col(idx) > 0) {
573                 idx -= idx % ncols();
574                 pos = 0;
575                 return true;
576         }
577         if (idx > 0) {
578                 idx = 0;
579                 pos = 0;
580                 return true;
581         }
582         return false;
583 }
584
585
586 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
587 {
588         if (pos < cell(idx).size()) {
589                 pos = cell(idx).size();
590                 return true;
591         }
592         if (col(idx) < ncols() - 1) {
593                 idx = idx - idx % ncols() + ncols() - 1;
594                 pos = cell(idx).size();
595                 return true;
596         }
597         if (idx < nargs() - 1) {
598                 idx = nargs() - 1;
599                 pos = cell(idx).size();
600                 return true;
601         }
602         return false;
603 }
604
605
606 void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
607 {
608         popit    = false;
609         deleteit = false;
610
611         // nothing to do if we are in the last row of the inset
612         if (row(idx) + 1 == nrows())
613                 return;
614
615         // try to delete entire sequence of ncols() empty cells if possible
616         for (idx_type i = idx; i < idx + ncols(); ++i)
617                 if (cell(i).size())
618                         return;
619
620         // move cells if necessary
621         for (idx_type i = index(row(idx), 0); i < idx; ++i)
622                 cell(i).swap(cell(i + ncols()));
623                 
624         delRow(row(idx));
625
626         if (idx >= nargs())
627                 idx = nargs() - 1;
628
629         // undo effect of Ctrl-Tab (i.e. pull next cell)
630         //if (idx + 1 != nargs()) 
631         //      cell(idx).swap(cell(idx + 1));
632 }
633
634
635 void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
636 {
637 // leave this unimplemented unless someone wants to have it.
638 /*
639         int n = (to - from) / ncols();
640         int r = from / ncols();
641
642         if (n >= 1) {
643                 cells_type::iterator it = cells_.begin() + from;
644                 cells_.erase(it, it + n * ncols());
645                 rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
646         }
647 */
648 }
649
650
651 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
652 {
653         return rowinfo_[row];
654 }
655
656
657 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
658 {
659         return rowinfo_[row];
660 }
661
662
663 std::vector<MathInset::idx_type>
664         MathGridInset::idxBetween(idx_type from, idx_type to) const
665 {
666         row_type r1 = std::min(row(from), row(to));
667         row_type r2 = std::max(row(from), row(to));
668         col_type c1 = std::min(col(from), col(to));
669         col_type c2 = std::max(col(from), col(to));
670         std::vector<idx_type> res;
671         for (row_type i = r1; i <= r2; ++i)
672                 for (col_type j = c1; j <= c2; ++j)
673                         res.push_back(index(i, j));
674         return res;
675 }
676
677
678
679 void MathGridInset::normalize(NormalStream & os) const
680 {
681         os << "[grid ";
682         for (row_type row = 0; row < nrows(); ++row) {
683                 os << "[row ";
684                 for (col_type col = 0; col < ncols(); ++col)
685                         os << "[cell " << cell(index(row, col)) << ']';
686                 os << ']';
687         }
688         os << ']';
689 }
690
691
692 void MathGridInset::mathmlize(MathMLStream & os) const
693 {
694         os << MTag("mtable");
695         for (row_type row = 0; row < nrows(); ++row) {
696                 os << MTag("mtr");
697                 for (col_type col = 0; col < ncols(); ++col) 
698                         os << cell(index(row, col));
699                 os << ETag("mtr");
700         }
701         os << ETag("mtable");
702 }
703
704
705 void MathGridInset::write(WriteStream & os) const
706 {
707         for (row_type row = 0; row < nrows(); ++row) {
708                 os << verboseHLine(rowinfo_[row].lines_).c_str();
709                 for (col_type col = 0; col < ncols(); ++col) 
710                         os << cell(index(row, col)) << eocString(col).c_str();
711                 os << eolString(row).c_str();
712         }
713         string s = verboseHLine(rowinfo_[nrows()].lines_);
714         if (s.size())
715                 os << "\\\\" << s.c_str();
716 }
717
718