]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
uses references instead of returning vectors
[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 "frontends/Painter.h"
10 #include "debug.h"
11
12
13 using std::swap;
14 using std::max;
15 using std::min;
16 using std::vector;
17
18
19 namespace {
20
21 string verboseHLine(int n)
22 {
23         string res;
24         for (int i = 0; i < n; ++i)
25                 res += "\\hline";
26         return res + ' ';
27 }
28
29 }
30
31 //////////////////////////////////////////////////////////////
32
33
34 MathGridInset::CellInfo::CellInfo()
35         : dummy_(false)
36 {}
37
38
39
40
41 //////////////////////////////////////////////////////////////
42
43
44 MathGridInset::RowInfo::RowInfo()
45         : lines_(0), skip_(0)
46 {}
47
48
49
50 int MathGridInset::RowInfo::skipPixels() const
51 {
52         return crskip_.inBP();
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)),
70           rowinfo_(2),
71           colinfo_(guessColumns(h) + 1),
72           cellinfo_(1 * guessColumns(h))
73 {
74         setDefaults();
75         valign(v);
76         halign(h);
77 }
78
79
80 MathGridInset::MathGridInset()
81         : MathNestInset(1),
82           rowinfo_(1 + 1),
83                 colinfo_(1 + 1),
84                 cellinfo_(1),
85                 v_align_('c')
86 {
87         setDefaults();
88 }
89
90
91 MathGridInset::MathGridInset(col_type m, row_type n)
92         : MathNestInset(m * n),
93           rowinfo_(n + 1),
94                 colinfo_(m + 1),
95                 cellinfo_(m * n),
96                 v_align_('c')
97 {
98         setDefaults();
99 }
100
101
102 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
103         : MathNestInset(m * n),
104           rowinfo_(n + 1),
105           colinfo_(m + 1),
106                 cellinfo_(m * n),
107                 v_align_(v)
108 {
109         setDefaults();
110         valign(v);
111         halign(h);
112 }
113
114
115 MathInset * MathGridInset::clone() const
116 {
117         return new MathGridInset(*this);
118 }
119
120
121 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
122 {
123         return col + ncols() * row;
124 }
125
126
127 void MathGridInset::setDefaults()
128 {
129         if (ncols() <= 0)
130                 lyxerr << "positive number of columns expected\n";
131         //if (nrows() <= 0)
132         //      lyxerr << "positive number of rows expected\n";
133         for (col_type col = 0; col < ncols(); ++col) {
134                 colinfo_[col].align_ = defaultColAlign(col);
135                 colinfo_[col].skip_  = defaultColSpace(col);
136         }
137 }
138
139
140 void MathGridInset::halign(string const & hh)
141 {
142         col_type col = 0;
143         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
144                 char c = *it;
145                 if (c == '|') {
146                         colinfo_[col].lines_++;
147                 } else if (c == 'c' || c == 'l' || c == 'r') {
148                         colinfo_[col].align_ = c;
149                         ++col;
150                         colinfo_[col].lines_ = 0;
151                 } else {
152                         lyxerr << "unkown column separator: '" << c << "'\n";
153                 }
154         }
155
156 /*
157         col_type n = hh.size();
158         if (n > ncols())
159                 n = ncols();
160         for (col_type col = 0; col < n; ++col)
161                 colinfo_[col].align_ = hh[col];
162 */
163 }
164
165
166 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
167 {
168         col_type col = 0;
169         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
170                 if (*it == 'c' || *it == 'l' || *it == 'r')
171                         ++col;
172         return col;
173 }
174
175
176 void MathGridInset::halign(char h, col_type col)
177 {
178         colinfo_[col].align_ = h;
179 }
180
181
182 char MathGridInset::halign(col_type col) const
183 {
184         return colinfo_[col].align_;
185 }
186
187
188 string MathGridInset::halign() const
189 {
190         string res;
191         for (col_type col = 0; col < ncols(); ++col) {
192                 res += string(colinfo_[col].lines_, '|');
193                 res += colinfo_[col].align_;
194         }
195         return res + string(colinfo_[ncols()].lines_, '|');
196 }
197
198
199 void MathGridInset::valign(char c)
200 {
201         v_align_ = c;
202 }
203
204
205 char MathGridInset::valign() const
206 {
207         return v_align_;
208 }
209
210
211 MathGridInset::col_type MathGridInset::ncols() const
212 {
213         return colinfo_.size() - 1;
214 }
215
216
217 MathGridInset::row_type MathGridInset::nrows() const
218 {
219         return rowinfo_.size() - 1;
220 }
221
222
223 MathGridInset::col_type MathGridInset::col(idx_type idx) const
224 {
225         return idx % ncols();
226 }
227
228
229 MathGridInset::row_type MathGridInset::row(idx_type idx) const
230 {
231         return idx / ncols();
232 }
233
234
235 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
236 {
237         rowinfo_[row].crskip_ = crskip;
238 }
239
240
241 LyXLength MathGridInset::vcrskip(row_type row) const
242 {
243         return rowinfo_[row].crskip_;
244 }
245
246
247 void MathGridInset::metrics(MathMetricsInfo & mi) const
248 {
249         // let the cells adjust themselves
250         MathNestInset::metrics(mi);
251
252         // compute absolute sizes of vertical structure
253         for (row_type row = 0; row < nrows(); ++row) {
254                 int asc  = 0;
255                 int desc = 0;
256                 for (col_type col = 0; col < ncols(); ++col) {
257                         MathXArray const & c = xcell(index(row, col));
258                         asc  = max(asc,  c.ascent());
259                         desc = max(desc, c.descent());
260                 }
261                 rowinfo_[row].ascent_  = asc;
262                 rowinfo_[row].descent_ = desc;
263         }
264         rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
265         rowinfo_[nrows()].ascent_  = 0;
266         rowinfo_[nrows()].descent_ = 0;
267
268         // compute vertical offsets
269         rowinfo_[0].offset_ = 0;
270         for (row_type row = 1; row <= nrows(); ++row) {
271                 rowinfo_[row].offset_  =
272                         rowinfo_[row - 1].offset_  +
273                         rowinfo_[row - 1].descent_ +
274                         rowinfo_[row - 1].skipPixels() +
275                         rowsep() +
276                         rowinfo_[row].lines_ * hlinesep() +
277                         rowinfo_[row].ascent_;
278         }
279
280         // adjust vertical offset
281         int h = 0;
282         switch (v_align_) {
283                 case 't':
284                         h = 0;
285                         break;
286                 case 'b':
287                         h = rowinfo_[nrows() - 1].offset_;
288                         break;
289                 default:
290                         h = rowinfo_[nrows() - 1].offset_ / 2;
291         }
292         for (row_type row = 0; row <= nrows(); ++row)
293                 rowinfo_[row].offset_ -= h;
294
295
296         // compute absolute sizes of horizontal structure
297         for (col_type col = 0; col < ncols(); ++col) {
298                 int wid = 0;
299                 for (row_type row = 0; row < nrows(); ++row)
300                         wid = max(wid, xcell(index(row, col)).width());
301                 colinfo_[col].width_ = wid;
302         }
303         colinfo_[ncols()].width_  = 0;
304
305         // compute horizontal offsets
306         colinfo_[0].offset_ = border();
307         for (col_type col = 1; col <= ncols(); ++col) {
308                 colinfo_[col].offset_ =
309                         colinfo_[col - 1].offset_ +
310                         colinfo_[col - 1].width_ +
311                         colinfo_[col - 1].skip_ +
312                         colsep() +
313                         colinfo_[col].lines_ * vlinesep();
314         }
315
316
317         dim_.w   =   colinfo_[ncols() - 1].offset_
318                        + colinfo_[ncols() - 1].width_
319                  + vlinesep() * colinfo_[ncols()].lines_
320                        + border();
321
322         dim_.a  = - rowinfo_[0].offset_
323                        + rowinfo_[0].ascent_
324                  + hlinesep() * rowinfo_[0].lines_
325                        + border();
326
327         dim_.d =   rowinfo_[nrows() - 1].offset_
328                        + rowinfo_[nrows() - 1].descent_
329                  + hlinesep() * rowinfo_[nrows()].lines_
330                        + border();
331
332
333 /*
334         // Increase ws_[i] for 'R' columns (except the first one)
335         for (int i = 1; i < nc_; ++i)
336                 if (align_[i] == 'R')
337                         ws_[i] += 10 * df_width;
338         // Increase ws_[i] for 'C' column
339         if (align_[0] == 'C')
340                 if (ws_[0] < 7 * workwidth / 8)
341                         ws_[0] = 7 * workwidth / 8;
342
343         // Adjust local tabs
344         width = colsep();
345         for (cxrow = row_.begin(); cxrow; ++cxrow) {
346                 int rg = COLSEP;
347                 int lf = 0;
348                 for (int i = 0; i < nc_; ++i) {
349                         bool isvoid = false;
350                         if (cxrow->getTab(i) <= 0) {
351                                 cxrow->setTab(i, df_width);
352                                 isvoid = true;
353                         }
354                         switch (align_[i]) {
355                         case 'l':
356                                 lf = 0;
357                                 break;
358                         case 'c':
359                                 lf = (ws_[i] - cxrow->getTab(i))/2;
360                                 break;
361                         case 'r':
362                         case 'R':
363                                 lf = ws_[i] - cxrow->getTab(i);
364                                 break;
365                         case 'C':
366                                 if (cxrow == row_.begin())
367                                         lf = 0;
368                                 else if (cxrow.is_last())
369                                         lf = ws_[i] - cxrow->getTab(i);
370                                 else
371                                         lf = (ws_[i] - cxrow->getTab(i))/2;
372                                 break;
373                         }
374                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
375                         cxrow->setTab(i, lf + rg);
376                         rg = ws_[i] - ww + colsep();
377                         if (cxrow == row_.begin())
378                                 width += ws_[i] + colsep();
379                 }
380                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
381         }
382 */
383 }
384
385
386 void MathGridInset::draw(MathPainterInfo & pi, int x, int y) const
387 {
388         for (idx_type idx = 0; idx < nargs(); ++idx)
389                 xcell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
390
391         for (row_type row = 0; row <= nrows(); ++row)
392                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
393                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
394                                 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
395                         pi.pain.line(x + 1, yy, x + width() - 1, yy);
396                 }
397
398         for (col_type col = 0; col <= ncols(); ++col)
399                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
400                         int xx = x + colinfo_[col].offset_
401                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;
402                         pi.pain.line(xx, y - ascent() + 1, xx, y + descent() - 1);
403                 }
404 }
405
406
407 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
408 {
409         // let the cells adjust themselves
410         //MathNestInset::metrics(mi);
411         for (idx_type i = 0; i < nargs(); ++i)
412                 xcell(i).metricsT(mi);
413
414         // compute absolute sizes of vertical structure
415         for (row_type row = 0; row < nrows(); ++row) {
416                 int asc  = 0;
417                 int desc = 0;
418                 for (col_type col = 0; col < ncols(); ++col) {
419                         MathXArray const & c = xcell(index(row, col));
420                         asc  = max(asc,  c.ascent());
421                         desc = max(desc, c.descent());
422                 }
423                 rowinfo_[row].ascent_  = asc;
424                 rowinfo_[row].descent_ = desc;
425         }
426         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
427         rowinfo_[nrows()].ascent_  = 0;
428         rowinfo_[nrows()].descent_ = 0;
429
430         // compute vertical offsets
431         rowinfo_[0].offset_ = 0;
432         for (row_type row = 1; row <= nrows(); ++row) {
433                 rowinfo_[row].offset_  =
434                         rowinfo_[row - 1].offset_  +
435                         rowinfo_[row - 1].descent_ +
436                         //rowinfo_[row - 1].skipPixels() +
437                         1 + //rowsep() +
438                         //rowinfo_[row].lines_ * hlinesep() +
439                         rowinfo_[row].ascent_;
440         }
441
442         // adjust vertical offset
443         int h = 0;
444         switch (v_align_) {
445                 case 't':
446                         h = 0;
447                         break;
448                 case 'b':
449                         h = rowinfo_[nrows() - 1].offset_;
450                         break;
451                 default:
452                         h = rowinfo_[nrows() - 1].offset_ / 2;
453         }
454         for (row_type row = 0; row <= nrows(); ++row)
455                 rowinfo_[row].offset_ -= h;
456
457
458         // compute absolute sizes of horizontal structure
459         for (col_type col = 0; col < ncols(); ++col) {
460                 int wid = 0;
461                 for (row_type row = 0; row < nrows(); ++row)
462                         wid = max(wid, xcell(index(row, col)).width());
463                 colinfo_[col].width_ = wid;
464         }
465         colinfo_[ncols()].width_  = 0;
466
467         // compute horizontal offsets
468         colinfo_[0].offset_ = border();
469         for (col_type col = 1; col <= ncols(); ++col) {
470                 colinfo_[col].offset_ =
471                         colinfo_[col - 1].offset_ +
472                         colinfo_[col - 1].width_ +
473                         colinfo_[col - 1].skip_ +
474                         1 ; //colsep() +
475                         //colinfo_[col].lines_ * vlinesep();
476         }
477
478
479         dim_.w  =  colinfo_[ncols() - 1].offset_
480                        + colinfo_[ncols() - 1].width_
481                  //+ vlinesep() * colinfo_[ncols()].lines_
482                        + 2;
483
484         dim_.a  = -rowinfo_[0].offset_
485                        + rowinfo_[0].ascent_
486                  //+ hlinesep() * rowinfo_[0].lines_
487                        + 1;
488
489         dim_.d  =  rowinfo_[nrows() - 1].offset_
490                        + rowinfo_[nrows() - 1].descent_
491                  //+ hlinesep() * rowinfo_[nrows()].lines_
492                        + 1;
493
494 }
495
496
497 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
498 {
499         for (idx_type idx = 0; idx < nargs(); ++idx)
500                 xcell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
501 }
502
503
504 string MathGridInset::eolString(row_type row, bool fragile) const
505 {
506         string eol;
507
508         if (!rowinfo_[row].crskip_.zero())
509                 eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
510
511         // make sure an upcoming '[' does not break anything
512         if (row + 1 < nrows()) {
513                 MathArray const & c = cell(index(row + 1, 0));
514                 if (c.size() && c.front()->getChar() == '[')
515                         //eol += "[0pt]";
516                         eol += "{}";
517         }
518
519         // only add \\ if necessary
520         if (eol.empty() && row + 1 == nrows())
521                 return string();
522
523         return (fragile ? "\\protect\\\\" : "\\\\") + eol + '\n';
524 }
525
526
527 string MathGridInset::eocString(col_type col) const
528 {
529         if (col + 1 == ncols())
530                 return string();
531         return " & ";
532 }
533
534
535 void MathGridInset::addRow(row_type row)
536 {
537         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
538         cells_.insert
539                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
540         cellinfo_.insert
541                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
542 }
543
544
545 void MathGridInset::appendRow()
546 {
547         rowinfo_.push_back(RowInfo());
548         //cells_.insert(cells_.end(), ncols(), MathXArray());
549         for (col_type col = 0; col < ncols(); ++col) {
550                 cells_.push_back(cells_type::value_type());
551                 cellinfo_.push_back(CellInfo());
552         }
553 }
554
555
556 void MathGridInset::delRow(row_type row)
557 {
558         if (nrows() == 1)
559                 return;
560
561         cells_type::iterator it = cells_.begin() + row * ncols();
562         cells_.erase(it, it + ncols());
563
564         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
565         cellinfo_.erase(jt, jt + ncols());
566
567         rowinfo_.erase(rowinfo_.begin() + row);
568 }
569
570
571 void MathGridInset::addCol(col_type newcol)
572 {
573         const col_type nc = ncols();
574         const row_type nr = nrows();
575         cells_type new_cells((nc + 1) * nr);
576         vector<CellInfo> new_cellinfo((nc + 1) * nr);
577
578         for (row_type row = 0; row < nr; ++row)
579                 for (col_type col = 0; col < nc; ++col) {
580                         new_cells[row * (nc + 1) + col + (col > newcol)]
581                                 = cells_[row * nc + col];
582                         new_cellinfo[row * (nc + 1) + col + (col > newcol)]
583                                 = cellinfo_[row * nc + col];
584                 }
585         swap(cells_, new_cells);
586         swap(cellinfo_, new_cellinfo);
587
588         ColInfo inf;
589         inf.skip_  = defaultColSpace(newcol);
590         inf.align_ = defaultColAlign(newcol);
591         colinfo_.insert(colinfo_.begin() + newcol, inf);
592 }
593
594
595 void MathGridInset::delCol(col_type col)
596 {
597         if (ncols() == 1)
598                 return;
599
600         cells_type tmpcells;
601         vector<CellInfo> tmpcellinfo;
602         for (col_type i = 0; i < nargs(); ++i)
603                 if (i % ncols() != col) {
604                         tmpcells.push_back(cells_[i]);
605                         tmpcellinfo.push_back(cellinfo_[i]);
606                 }
607         swap(cells_, tmpcells);
608         swap(cellinfo_, tmpcellinfo);
609
610         colinfo_.erase(colinfo_.begin() + col);
611 }
612
613
614 int MathGridInset::cellXOffset(idx_type idx) const
615 {
616         col_type c = col(idx);
617         int x = colinfo_[c].offset_;
618         char align = colinfo_[c].align_;
619         if (align == 'r' || align == 'R')
620                 x += colinfo_[c].width_ - xcell(idx).width();
621         if (align == 'c' || align == 'C')
622                 x += (colinfo_[c].width_ - xcell(idx).width()) / 2;
623         return x;
624 }
625
626
627 int MathGridInset::cellYOffset(idx_type idx) const
628 {
629         return rowinfo_[row(idx)].offset_;
630 }
631
632
633 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
634         int targetx) const
635 {
636         if (up) {
637                 if (idx < ncols())
638                         return false;
639                 idx -= ncols();
640                 pos = xcell(idx).x2pos(targetx - xcell(idx).xo());
641                 return true;
642         } else {
643                 if (idx >= ncols() * (nrows() - 1))
644                         return false;
645                 idx += ncols();
646                 pos = xcell(idx).x2pos(targetx - xcell(idx).xo());
647                 return true;
648         }
649 }
650
651
652 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
653 {
654         // leave matrix if on the left hand edge
655         if (col(idx) == 0)
656                 return false;
657         --idx;
658         pos = cell(idx).size();
659         return true;
660 }
661
662
663 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
664 {
665         // leave matrix if on the right hand edge
666         if (col(idx) + 1 == ncols())
667                 return false;
668         ++idx;
669         pos = 0;
670         return true;
671 }
672
673
674 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
675 {
676         switch (v_align_) {
677                 case 't':
678                         idx = 0;
679                         break;
680                 case 'b':
681                         idx = (nrows() - 1) * ncols();
682                         break;
683                 default:
684                         idx = ((nrows() - 1) / 2) * ncols();
685         }
686         pos = 0;
687         return true;
688 }
689
690
691 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
692 {
693         switch (v_align_) {
694                 case 't':
695                         idx = ncols() - 1;
696                         break;
697                 case 'b':
698                         idx = nargs() - 1;
699                         break;
700                 default:
701                         idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
702         }
703         pos = cell(idx).size();
704         return true;
705 }
706
707
708 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
709 {
710         if (pos > 0) {
711                 pos = 0;
712                 return true;
713         }
714         if (col(idx) > 0) {
715                 idx -= idx % ncols();
716                 pos = 0;
717                 return true;
718         }
719         if (idx > 0) {
720                 idx = 0;
721                 pos = 0;
722                 return true;
723         }
724         return false;
725 }
726
727
728 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
729 {
730         if (pos < cell(idx).size()) {
731                 pos = cell(idx).size();
732                 return true;
733         }
734         if (col(idx) < ncols() - 1) {
735                 idx = idx - idx % ncols() + ncols() - 1;
736                 pos = cell(idx).size();
737                 return true;
738         }
739         if (idx < nargs() - 1) {
740                 idx = nargs() - 1;
741                 pos = cell(idx).size();
742                 return true;
743         }
744         return false;
745 }
746
747
748 bool MathGridInset::idxDelete(idx_type & idx)
749 {
750         // nothing to do if we have just one row
751         if (nrows() == 1)
752                 return false;
753
754         // nothing to do if we are in the middle of the last row of the inset
755         if (idx + ncols() > nargs())
756                 return false;
757
758         // try to delete entire sequence of ncols() empty cells if possible
759         for (idx_type i = idx; i < idx + ncols(); ++i)
760                 if (cell(i).size())
761                         return false;
762
763         // move cells if necessary
764         for (idx_type i = index(row(idx), 0); i < idx; ++i)
765                 std::swap(cell(i), cell(i + ncols()));
766
767         delRow(row(idx));
768
769         if (idx >= nargs())
770                 idx = nargs() - 1;
771
772         // undo effect of Ctrl-Tab (i.e. pull next cell)
773         //if (idx + 1 != nargs())
774         //      cell(idx).swap(cell(idx + 1));
775
776         // we handled the event..
777         return true;
778 }
779
780
781 // reimplement old behaviour when pressing Delete in the last position
782 // of a cell
783 void MathGridInset::idxGlue(idx_type idx)
784 {
785         col_type c = col(idx);
786         if (c + 1 == ncols()) {
787                 if (row(idx) + 1 != nrows()) {
788                         for (col_type cc = 0; cc < ncols(); ++cc)
789                                 cell(idx).append(cell(idx + cc + 1));
790                         delRow(row(idx) + 1);
791                 }
792         } else {
793                 cell(idx).append(cell(idx + 1));
794                 for (col_type cc = c + 2; cc < ncols(); ++cc)
795                         cell(idx - c + cc - 1) = cell(idx - c + cc);
796                 cell(idx - c + ncols() - 1).clear();
797         }
798 }
799
800
801 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
802 {
803         return rowinfo_[row];
804 }
805
806
807 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
808 {
809         return rowinfo_[row];
810 }
811
812
813 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
814 {
815         row_type const ri = row(idx);
816         row_type const r1 = min(row(from), row(to));
817         row_type const r2 = max(row(from), row(to));
818         col_type const ci = col(idx);
819         col_type const c1 = min(col(from), col(to));
820         col_type const c2 = max(col(from), col(to));
821         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
822 }
823
824
825
826 void MathGridInset::normalize(NormalStream & os) const
827 {
828         os << "[grid ";
829         for (row_type row = 0; row < nrows(); ++row) {
830                 os << "[row ";
831                 for (col_type col = 0; col < ncols(); ++col)
832                         os << "[cell " << cell(index(row, col)) << ']';
833                 os << ']';
834         }
835         os << ']';
836 }
837
838
839 void MathGridInset::mathmlize(MathMLStream & os) const
840 {
841         os << MTag("mtable");
842         for (row_type row = 0; row < nrows(); ++row) {
843                 os << MTag("mtr");
844                 for (col_type col = 0; col < ncols(); ++col)
845                         os << cell(index(row, col));
846                 os << ETag("mtr");
847         }
848         os << ETag("mtable");
849 }
850
851
852 void MathGridInset::write(WriteStream & os) const
853 {
854         for (row_type row = 0; row < nrows(); ++row) {
855                 os << verboseHLine(rowinfo_[row].lines_);
856                 for (col_type col = 0; col < ncols(); ++col)
857                         os << cell(index(row, col)) << eocString(col);
858                 os << eolString(row, os.fragile());
859         }
860         string const s = verboseHLine(rowinfo_[nrows()].lines_);
861         if (!s.empty() && s != " ") {
862                 if (os.fragile())
863                         os << "\\protect";
864                 os << "\\\\" << s;
865         }
866 }
867
868
869 int MathGridInset::colsep() const
870 {
871         return 6;
872 }
873
874
875 int MathGridInset::rowsep() const
876 {
877         return 6;
878 }
879
880
881 int MathGridInset::hlinesep() const
882 {
883         return 3;
884 }
885
886
887 int MathGridInset::vlinesep() const
888 {
889         return 3;
890 }
891
892
893 int MathGridInset::border() const
894 {
895         return 1;
896 }
897