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