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