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