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