]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_gridinset.C
1 #include "math_gridinset.h"
2 #include "math_mathmlstream.h"
3 #include "math_streamstr.h"
4 #include "lyxfont.h"
5 #include "funcrequest.h"
6 #include "frontends/Painter.h"
7 #include "debug.h"
8
9
10 using std::swap;
11 using std::max;
12 using std::min;
13 using std::vector;
14
15
16 void mathed_parse_normal(MathGridInset &, string const & argument);
17
18 namespace {
19
20 string verboseHLine(int n)
21 {
22         string res;
23         for (int i = 0; i < n; ++i)
24                 res += "\\hline";
25         if (n)
26                 res += ' ';
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;
532 }
533
534
535 string MathGridInset::eocString(col_type col, col_type lastcol) const
536 {
537         if (col + 1 == lastcol)
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::copyRow(row_type row)
580 {
581         addRow(row);
582         for (col_type col = 0; col < ncols(); ++col)
583                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
584 }
585
586
587 void MathGridInset::swapRow(row_type row)
588 {
589         if (nrows() == 1)
590                 return;
591         if (row + 1 == nrows())
592                 --row;
593         for (col_type col = 0; col < ncols(); ++col)
594                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
595 }
596
597
598 void MathGridInset::addCol(col_type newcol)
599 {
600         const col_type nc = ncols();
601         const row_type nr = nrows();
602         cells_type new_cells((nc + 1) * nr);
603         vector<CellInfo> new_cellinfo((nc + 1) * nr);
604
605         for (row_type row = 0; row < nr; ++row)
606                 for (col_type col = 0; col < nc; ++col) {
607                         new_cells[row * (nc + 1) + col + (col > newcol)]
608                                 = cells_[row * nc + col];
609                         new_cellinfo[row * (nc + 1) + col + (col > newcol)]
610                                 = cellinfo_[row * nc + col];
611                 }
612         swap(cells_, new_cells);
613         swap(cellinfo_, new_cellinfo);
614
615         ColInfo inf;
616         inf.skip_  = defaultColSpace(newcol);
617         inf.align_ = defaultColAlign(newcol);
618         colinfo_.insert(colinfo_.begin() + newcol, inf);
619 }
620
621
622 void MathGridInset::delCol(col_type col)
623 {
624         if (ncols() == 1)
625                 return;
626
627         cells_type tmpcells;
628         vector<CellInfo> tmpcellinfo;
629         for (col_type i = 0; i < nargs(); ++i)
630                 if (i % ncols() != col) {
631                         tmpcells.push_back(cells_[i]);
632                         tmpcellinfo.push_back(cellinfo_[i]);
633                 }
634         swap(cells_, tmpcells);
635         swap(cellinfo_, tmpcellinfo);
636
637         colinfo_.erase(colinfo_.begin() + col);
638 }
639
640
641 void MathGridInset::copyCol(col_type col)
642 {
643         addCol(col);
644         for (row_type row = 0; row < nrows(); ++row)
645                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
646 }
647
648
649 void MathGridInset::swapCol(col_type col)
650 {
651         if (ncols() == 1)
652                 return;
653         if (col + 1 == ncols())
654                 --col;
655         for (row_type row = 0; row < nrows(); ++row)
656                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
657 }
658
659
660 int MathGridInset::cellXOffset(idx_type idx) const
661 {
662         col_type c = col(idx);
663         int x = colinfo_[c].offset_;
664         char align = colinfo_[c].align_;
665         if (align == 'r' || align == 'R')
666                 x += colinfo_[c].width_ - cell(idx).width();
667         if (align == 'c' || align == 'C')
668                 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
669         return x;
670 }
671
672
673 int MathGridInset::cellYOffset(idx_type idx) const
674 {
675         return rowinfo_[row(idx)].offset_;
676 }
677
678
679 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
680         int targetx) const
681 {
682         if (up) {
683                 if (idx < ncols())
684                         return false;
685                 idx -= ncols();
686                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
687                 return true;
688         } else {
689                 if (idx >= ncols() * (nrows() - 1))
690                         return false;
691                 idx += ncols();
692                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
693                 return true;
694         }
695 }
696
697
698 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
699 {
700         // leave matrix if on the left hand edge
701         if (col(idx) == 0)
702                 return false;
703         --idx;
704         pos = cell(idx).size();
705         return true;
706 }
707
708
709 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
710 {
711         // leave matrix if on the right hand edge
712         if (col(idx) + 1 == ncols())
713                 return false;
714         ++idx;
715         pos = 0;
716         return true;
717 }
718
719
720 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
721 {
722         switch (v_align_) {
723                 case 't':
724                         idx = 0;
725                         break;
726                 case 'b':
727                         idx = (nrows() - 1) * ncols();
728                         break;
729                 default:
730                         idx = ((nrows() - 1) / 2) * ncols();
731         }
732         pos = 0;
733         return true;
734 }
735
736
737 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
738 {
739         switch (v_align_) {
740                 case 't':
741                         idx = ncols() - 1;
742                         break;
743                 case 'b':
744                         idx = nargs() - 1;
745                         break;
746                 default:
747                         idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
748         }
749         pos = cell(idx).size();
750         return true;
751 }
752
753
754 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
755 {
756         if (pos > 0) {
757                 pos = 0;
758                 return true;
759         }
760         if (col(idx) > 0) {
761                 idx -= idx % ncols();
762                 pos = 0;
763                 return true;
764         }
765         if (idx > 0) {
766                 idx = 0;
767                 pos = 0;
768                 return true;
769         }
770         return false;
771 }
772
773
774 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
775 {
776         if (pos < cell(idx).size()) {
777                 pos = cell(idx).size();
778                 return true;
779         }
780         if (col(idx) < ncols() - 1) {
781                 idx = idx - idx % ncols() + ncols() - 1;
782                 pos = cell(idx).size();
783                 return true;
784         }
785         if (idx < nargs() - 1) {
786                 idx = nargs() - 1;
787                 pos = cell(idx).size();
788                 return true;
789         }
790         return false;
791 }
792
793
794 bool MathGridInset::idxDelete(idx_type & idx)
795 {
796         // nothing to do if we have just one row
797         if (nrows() == 1)
798                 return false;
799
800         // nothing to do if we are in the middle of the last row of the inset
801         if (idx + ncols() > nargs())
802                 return false;
803
804         // try to delete entire sequence of ncols() empty cells if possible
805         for (idx_type i = idx; i < idx + ncols(); ++i)
806                 if (cell(i).size())
807                         return false;
808
809         // move cells if necessary
810         for (idx_type i = index(row(idx), 0); i < idx; ++i)
811                 std::swap(cell(i), cell(i + ncols()));
812
813         delRow(row(idx));
814
815         if (idx >= nargs())
816                 idx = nargs() - 1;
817
818         // undo effect of Ctrl-Tab (i.e. pull next cell)
819         //if (idx + 1 != nargs())
820         //      cell(idx).swap(cell(idx + 1));
821
822         // we handled the event..
823         return true;
824 }
825
826
827 // reimplement old behaviour when pressing Delete in the last position
828 // of a cell
829 void MathGridInset::idxGlue(idx_type idx)
830 {
831         col_type c = col(idx);
832         if (c + 1 == ncols()) {
833                 if (row(idx) + 1 != nrows()) {
834                         for (col_type cc = 0; cc < ncols(); ++cc)
835                                 cell(idx).append(cell(idx + cc + 1));
836                         delRow(row(idx) + 1);
837                 }
838         } else {
839                 cell(idx).append(cell(idx + 1));
840                 for (col_type cc = c + 2; cc < ncols(); ++cc)
841                         cell(idx - c + cc - 1) = cell(idx - c + cc);
842                 cell(idx - c + ncols() - 1).clear();
843         }
844 }
845
846
847 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
848 {
849         return rowinfo_[row];
850 }
851
852
853 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
854 {
855         return rowinfo_[row];
856 }
857
858
859 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
860 {
861         row_type const ri = row(idx);
862         row_type const r1 = min(row(from), row(to));
863         row_type const r2 = max(row(from), row(to));
864         col_type const ci = col(idx);
865         col_type const c1 = min(col(from), col(to));
866         col_type const c2 = max(col(from), col(to));
867         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
868 }
869
870
871
872 void MathGridInset::normalize(NormalStream & os) const
873 {
874         os << "[grid ";
875         for (row_type row = 0; row < nrows(); ++row) {
876                 os << "[row ";
877                 for (col_type col = 0; col < ncols(); ++col)
878                         os << "[cell " << cell(index(row, col)) << ']';
879                 os << ']';
880         }
881         os << ']';
882 }
883
884
885 void MathGridInset::mathmlize(MathMLStream & os) const
886 {
887         os << MTag("mtable");
888         for (row_type row = 0; row < nrows(); ++row) {
889                 os << MTag("mtr");
890                 for (col_type col = 0; col < ncols(); ++col)
891                         os << cell(index(row, col));
892                 os << ETag("mtr");
893         }
894         os << ETag("mtable");
895 }
896
897
898 void MathGridInset::write(WriteStream & os) const
899 {
900         for (row_type row = 0; row < nrows(); ++row) {
901                 os << verboseHLine(rowinfo_[row].lines_);
902                 // don't write & and empty cells at end of line
903                 col_type lastcol = 0;
904                 bool emptyline = true;
905                 for (col_type col = 0; col < ncols(); ++col)
906                         if (!cell(index(row, col)).empty()) {
907                                 lastcol = col + 1;
908                                 emptyline = false;
909                         }
910                 for (col_type col = 0; col < lastcol; ++col)
911                         os << cell(index(row, col)) << eocString(col, lastcol);
912                 os << eolString(row, os.fragile());
913                 // append newline only if line wasn't completely empty
914                 // and this was not the last line in the grid
915                 if (!emptyline && row + 1 < nrows())
916                         os << "\n";
917         }
918         string const s = verboseHLine(rowinfo_[nrows()].lines_);
919         if (!s.empty() && s != " ") {
920                 if (os.fragile())
921                         os << "\\protect";
922                 os << "\\\\" << s;
923         }
924 }
925
926
927 int MathGridInset::colsep() const
928 {
929         return 6;
930 }
931
932
933 int MathGridInset::rowsep() const
934 {
935         return 6;
936 }
937
938
939 int MathGridInset::hlinesep() const
940 {
941         return 3;
942 }
943
944
945 int MathGridInset::vlinesep() const
946 {
947         return 3;
948 }
949
950
951 int MathGridInset::border() const
952 {
953         return 1;
954 }
955
956
957 void MathGridInset::splitCell(idx_type & idx, pos_type & pos)
958 {
959         if (idx + 1 == nargs())
960                 return;
961         MathArray ar = cell(idx);
962         ar.erase(0, pos);
963         cell(idx).erase(pos, cell(idx).size());
964         ++idx;
965         pos = 0;
966         cell(idx).insert(0, ar);
967 }
968
969
970 dispatch_result MathGridInset::dispatch
971         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
972 {
973         switch (cmd.action) {
974
975                 case LFUN_DELETE_LINE_FORWARD:
976                         //autocorrect_ = false;
977                         //macroModeClose();
978                         //if (selection_) {
979                         //      selDel();
980                         //      return;
981                         //}
982                         if (nrows() > 1)
983                                 delRow(row(idx));
984                         if (idx >= nargs())
985                                 idx = nargs() - 1;
986                         if (pos > cell(idx).size())
987                                 pos = cell(idx).size();
988                         return DISPATCHED_POP;
989
990                 case LFUN_TABINSERT:
991                         //bv->lockedInsetStoreUndo(Undo::EDIT);
992                         splitCell(idx, pos);
993                         //updateLocal(bv, true);
994                         return DISPATCHED_POP;
995
996                 case LFUN_BREAKLINE: {
997                         //bv->lockedInsetStoreUndo(Undo::INSERT);
998                         row_type const r = row(idx);
999                         addRow(r);
1000
1001                         // split line
1002                         for (col_type c = col(idx) + 1; c < ncols(); ++c)
1003                                 std::swap(cell(index(r, c)), cell(index(r + 1, c)));
1004
1005                         // split cell
1006                         splitCell(idx, pos);
1007                         std::swap(cell(idx), cell(idx + ncols() - 1));
1008                         if (idx > 0)
1009                                 --idx;
1010                         pos = cell(idx).size();
1011
1012                         //mathcursor->normalize();
1013                         //updateLocal(bv, true);
1014                         return DISPATCHED_POP;
1015                 }
1016
1017                 case LFUN_TABULAR_FEATURE:
1018                         //lyxerr << "handling tabular-feature " << cmd.argument << "\n";
1019                         if (cmd.argument == "valign-top")
1020                                 valign('t');
1021                         else if (cmd.argument == "valign-center")
1022                                 valign('c');
1023                         else if (cmd.argument == "valign-bottom")
1024                                 valign('b');
1025                         else if (cmd.argument == "align-left")
1026                                 halign('l', col(idx));
1027                         else if (cmd.argument == "align-right")
1028                                 halign('r', col(idx));
1029                         else if (cmd.argument == "align-center")
1030                                 halign('c', col(idx));
1031                         else if (cmd.argument == "append-row")
1032                                 addRow(row(idx));
1033                         else if (cmd.argument == "delete-row") {
1034                                 delRow(row(idx));
1035                                 if (idx > nargs())
1036                                         idx -= ncols();
1037                         } else if (cmd.argument == "copy-row")
1038                                 copyRow(row(idx));
1039                         else if (cmd.argument == "swap-row")
1040                                 swapRow(row(idx));
1041                         else if (cmd.argument == "append-column") {
1042                                 row_type r = row(idx);
1043                                 col_type c = col(idx);
1044                                 addCol(c);
1045                                 idx = index(r, c);
1046                         } else if (cmd.argument == "delete-column") {
1047                                 row_type r = row(idx);
1048                                 col_type c = col(idx);
1049                                 delCol(col(idx));
1050                                 idx = index(r, c);
1051                                 if (idx > nargs())
1052                                         idx -= ncols();
1053                         } else if (cmd.argument == "copy-column")
1054                                 copyCol(col(idx));
1055                         else if (cmd.argument == "swap-column")
1056                                 swapCol(col(idx));
1057                         else
1058                                 return UNDISPATCHED;
1059                         return DISPATCHED_POP;
1060
1061                 case LFUN_PASTE: {
1062                         //lyxerr << "pasting '" << cmd.argument << "'\n";
1063                         MathGridInset grid(1, 1);
1064                         mathed_parse_normal(grid, cmd.argument);
1065                         if (grid.nargs() == 1) {
1066                                 // single cell/part of cell
1067                                 cell(idx).insert(pos, grid.cell(0));
1068                                 pos += grid.cell(0).size();
1069                         } else {
1070                                 // multiple cells
1071                                 col_type const numcols = min(grid.ncols(), ncols() - col(idx));
1072                                 row_type const numrows = min(grid.nrows(), nrows() - row(idx));
1073                                 for (row_type r = 0; r < numrows; ++r) {
1074                                         for (col_type c = 0; c < numcols; ++c) {
1075                                                 idx_type i = index(r + row(idx), c + col(idx));
1076                                                 cell(i).append(grid.cell(grid.index(r, c)));
1077                                         }
1078                                         // append the left over horizontal cells to the last column
1079                                         idx_type i = index(r + row(idx), ncols() - 1);
1080                                         for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1081                                                 cell(i).append(grid.cell(grid.index(r, c)));
1082                                 }
1083                                 // append the left over vertical cells to the last _cell_
1084                                 idx_type i = nargs() - 1;
1085                                 for (row_type r = numrows; r < grid.nrows(); ++r)
1086                                         for (col_type c = 0; c < grid.ncols(); ++c)
1087                                                 cell(i).append(grid.cell(grid.index(r, c)));
1088                         }
1089                         return DISPATCHED_POP;
1090                 }
1091
1092                 default:
1093                         return MathNestInset::dispatch(cmd, idx, pos);
1094         }
1095         return UNDISPATCHED;
1096 }