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