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