]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
some common structures in extra file
[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() - 1;
215 }
216
217
218 MathGridInset::row_type MathGridInset::nrows() const
219 {
220         return rowinfo_.size() - 1;
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         rowinfo_[nrows()].ascent_  = 0;
267         rowinfo_[nrows()].descent_ = 0;
268
269         // compute vertical offsets
270         rowinfo_[0].offset_ = 0;
271         for (row_type row = 1; row <= nrows(); ++row) {
272                 rowinfo_[row].offset_  =
273                         rowinfo_[row - 1].offset_  +
274                         rowinfo_[row - 1].descent_ +
275                         rowinfo_[row - 1].skipPixels() +
276                         rowsep() +
277                         rowinfo_[row].lines_ * hlinesep() +
278                         rowinfo_[row].ascent_;
279         }
280
281         // adjust vertical offset
282         int h = 0;
283         switch (v_align_) {
284                 case 't':
285                         h = 0;
286                         break;
287                 case 'b':
288                         h = rowinfo_[nrows() - 1].offset_;
289                         break;
290                 default:
291                         h = rowinfo_[nrows() - 1].offset_ / 2;
292         }
293         for (row_type row = 0; row <= nrows(); ++row)
294                 rowinfo_[row].offset_ -= h;
295
296
297         // compute absolute sizes of horizontal structure
298         for (col_type col = 0; col < ncols(); ++col) {
299                 int wid = 0;
300                 for (row_type row = 0; row < nrows(); ++row)
301                         wid = max(wid, cell(index(row, col)).width());
302                 colinfo_[col].width_ = wid;
303         }
304         colinfo_[ncols()].width_  = 0;
305
306         // compute horizontal offsets
307         colinfo_[0].offset_ = border();
308         for (col_type col = 1; col <= ncols(); ++col) {
309                 colinfo_[col].offset_ =
310                         colinfo_[col - 1].offset_ +
311                         colinfo_[col - 1].width_ +
312                         colinfo_[col - 1].skip_ +
313                         colsep() +
314                         colinfo_[col].lines_ * vlinesep();
315         }
316
317
318         dim_.w   =   colinfo_[ncols() - 1].offset_
319                        + colinfo_[ncols() - 1].width_
320                  + vlinesep() * colinfo_[ncols()].lines_
321                        + border();
322
323         dim_.a  = - rowinfo_[0].offset_
324                        + rowinfo_[0].ascent_
325                  + hlinesep() * rowinfo_[0].lines_
326                        + border();
327
328         dim_.d =   rowinfo_[nrows() - 1].offset_
329                        + rowinfo_[nrows() - 1].descent_
330                  + hlinesep() * rowinfo_[nrows()].lines_
331                        + border();
332
333
334 /*
335         // Increase ws_[i] for 'R' columns (except the first one)
336         for (int i = 1; i < nc_; ++i)
337                 if (align_[i] == 'R')
338                         ws_[i] += 10 * df_width;
339         // Increase ws_[i] for 'C' column
340         if (align_[0] == 'C')
341                 if (ws_[0] < 7 * workwidth / 8)
342                         ws_[0] = 7 * workwidth / 8;
343
344         // Adjust local tabs
345         width = colsep();
346         for (cxrow = row_.begin(); cxrow; ++cxrow) {
347                 int rg = COLSEP;
348                 int lf = 0;
349                 for (int i = 0; i < nc_; ++i) {
350                         bool isvoid = false;
351                         if (cxrow->getTab(i) <= 0) {
352                                 cxrow->setTab(i, df_width);
353                                 isvoid = true;
354                         }
355                         switch (align_[i]) {
356                         case 'l':
357                                 lf = 0;
358                                 break;
359                         case 'c':
360                                 lf = (ws_[i] - cxrow->getTab(i))/2;
361                                 break;
362                         case 'r':
363                         case 'R':
364                                 lf = ws_[i] - cxrow->getTab(i);
365                                 break;
366                         case 'C':
367                                 if (cxrow == row_.begin())
368                                         lf = 0;
369                                 else if (cxrow.is_last())
370                                         lf = ws_[i] - cxrow->getTab(i);
371                                 else
372                                         lf = (ws_[i] - cxrow->getTab(i))/2;
373                                 break;
374                         }
375                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
376                         cxrow->setTab(i, lf + rg);
377                         rg = ws_[i] - ww + colsep();
378                         if (cxrow == row_.begin())
379                                 width += ws_[i] + colsep();
380                 }
381                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
382         }
383 */
384 }
385
386
387 void MathGridInset::draw(PainterInfo & pi, int x, int y) const
388 {
389         for (idx_type idx = 0; idx < nargs(); ++idx)
390                 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
391
392         for (row_type row = 0; row <= nrows(); ++row)
393                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
394                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
395                                 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
396                         pi.pain.line(x + 1, yy, x + width() - 1, yy);
397                 }
398
399         for (col_type col = 0; col <= ncols(); ++col)
400                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
401                         int xx = x + colinfo_[col].offset_
402                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;
403                         pi.pain.line(xx, y - ascent() + 1, xx, y + descent() - 1);
404                 }
405 }
406
407
408 void MathGridInset::metricsT(TextMetricsInfo const & mi) const
409 {
410         // let the cells adjust themselves
411         //MathNestInset::metrics(mi);
412         for (idx_type i = 0; i < nargs(); ++i)
413                 cell(i).metricsT(mi);
414
415         // compute absolute sizes of vertical structure
416         for (row_type row = 0; row < nrows(); ++row) {
417                 int asc  = 0;
418                 int desc = 0;
419                 for (col_type col = 0; col < ncols(); ++col) {
420                         MathArray const & c = cell(index(row, col));
421                         asc  = max(asc,  c.ascent());
422                         desc = max(desc, c.descent());
423                 }
424                 rowinfo_[row].ascent_  = asc;
425                 rowinfo_[row].descent_ = desc;
426         }
427         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
428         rowinfo_[nrows()].ascent_  = 0;
429         rowinfo_[nrows()].descent_ = 0;
430
431         // compute vertical offsets
432         rowinfo_[0].offset_ = 0;
433         for (row_type row = 1; row <= nrows(); ++row) {
434                 rowinfo_[row].offset_  =
435                         rowinfo_[row - 1].offset_  +
436                         rowinfo_[row - 1].descent_ +
437                         //rowinfo_[row - 1].skipPixels() +
438                         1 + //rowsep() +
439                         //rowinfo_[row].lines_ * hlinesep() +
440                         rowinfo_[row].ascent_;
441         }
442
443         // adjust vertical offset
444         int h = 0;
445         switch (v_align_) {
446                 case 't':
447                         h = 0;
448                         break;
449                 case 'b':
450                         h = rowinfo_[nrows() - 1].offset_;
451                         break;
452                 default:
453                         h = rowinfo_[nrows() - 1].offset_ / 2;
454         }
455         for (row_type row = 0; row <= nrows(); ++row)
456                 rowinfo_[row].offset_ -= h;
457
458
459         // compute absolute sizes of horizontal structure
460         for (col_type col = 0; col < ncols(); ++col) {
461                 int wid = 0;
462                 for (row_type row = 0; row < nrows(); ++row)
463                         wid = max(wid, cell(index(row, col)).width());
464                 colinfo_[col].width_ = wid;
465         }
466         colinfo_[ncols()].width_  = 0;
467
468         // compute horizontal offsets
469         colinfo_[0].offset_ = border();
470         for (col_type col = 1; col <= ncols(); ++col) {
471                 colinfo_[col].offset_ =
472                         colinfo_[col - 1].offset_ +
473                         colinfo_[col - 1].width_ +
474                         colinfo_[col - 1].skip_ +
475                         1 ; //colsep() +
476                         //colinfo_[col].lines_ * vlinesep();
477         }
478
479
480         dim_.w  =  colinfo_[ncols() - 1].offset_
481                        + colinfo_[ncols() - 1].width_
482                  //+ vlinesep() * colinfo_[ncols()].lines_
483                        + 2;
484
485         dim_.a  = -rowinfo_[0].offset_
486                        + rowinfo_[0].ascent_
487                  //+ hlinesep() * rowinfo_[0].lines_
488                        + 1;
489
490         dim_.d  =  rowinfo_[nrows() - 1].offset_
491                        + rowinfo_[nrows() - 1].descent_
492                  //+ hlinesep() * rowinfo_[nrows()].lines_
493                        + 1;
494
495 }
496
497
498 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
499 {
500         for (idx_type idx = 0; idx < nargs(); ++idx)
501                 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
502 }
503
504
505 string MathGridInset::eolString(row_type row, bool fragile) const
506 {
507         string eol;
508
509         if (!rowinfo_[row].crskip_.zero())
510                 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
511
512         // make sure an upcoming '[' does not break anything
513         if (row + 1 < nrows()) {
514                 MathArray const & c = cell(index(row + 1, 0));
515                 if (c.size() && c.front()->getChar() == '[')
516                         //eol += "[0pt]";
517                         eol += "{}";
518         }
519
520         // only add \\ if necessary
521         if (eol.empty() && row + 1 == nrows())
522                 return string();
523
524         return (fragile ? "\\protect\\\\" : "\\\\") + eol;
525 }
526
527
528 string MathGridInset::eocString(col_type col, col_type lastcol) const
529 {
530         if (col + 1 == lastcol)
531                 return string();
532         return " & ";
533 }
534
535
536 void MathGridInset::addRow(row_type row)
537 {
538         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
539         cells_.insert
540                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
541         cellinfo_.insert
542                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
543 }
544
545
546 void MathGridInset::appendRow()
547 {
548         rowinfo_.push_back(RowInfo());
549         //cells_.insert(cells_.end(), ncols(), MathArray());
550         for (col_type col = 0; col < ncols(); ++col) {
551                 cells_.push_back(cells_type::value_type());
552                 cellinfo_.push_back(CellInfo());
553         }
554 }
555
556
557 void MathGridInset::delRow(row_type row)
558 {
559         if (nrows() == 1)
560                 return;
561
562         cells_type::iterator it = cells_.begin() + row * ncols();
563         cells_.erase(it, it + ncols());
564
565         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
566         cellinfo_.erase(jt, jt + ncols());
567
568         rowinfo_.erase(rowinfo_.begin() + row);
569 }
570
571
572 void MathGridInset::copyRow(row_type row)
573 {
574         addRow(row);
575         for (col_type col = 0; col < ncols(); ++col)
576                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
577 }
578
579
580 void MathGridInset::swapRow(row_type row)
581 {
582         if (nrows() == 1)
583                 return;
584         if (row + 1 == nrows())
585                 --row;
586         for (col_type col = 0; col < ncols(); ++col)
587                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
588 }
589
590
591 void MathGridInset::addCol(col_type newcol)
592 {
593         const col_type nc = ncols();
594         const row_type nr = nrows();
595         cells_type new_cells((nc + 1) * nr);
596         vector<CellInfo> new_cellinfo((nc + 1) * nr);
597
598         for (row_type row = 0; row < nr; ++row)
599                 for (col_type col = 0; col < nc; ++col) {
600                         new_cells[row * (nc + 1) + col + (col > newcol)]
601                                 = cells_[row * nc + col];
602                         new_cellinfo[row * (nc + 1) + col + (col > newcol)]
603                                 = cellinfo_[row * nc + col];
604                 }
605         swap(cells_, new_cells);
606         swap(cellinfo_, new_cellinfo);
607
608         ColInfo inf;
609         inf.skip_ = defaultColSpace(newcol);
610         inf.align = defaultColAlign(newcol);
611         colinfo_.insert(colinfo_.begin() + newcol, inf);
612 }
613
614
615 void MathGridInset::delCol(col_type col)
616 {
617         if (ncols() == 1)
618                 return;
619
620         cells_type tmpcells;
621         vector<CellInfo> tmpcellinfo;
622         for (col_type i = 0; i < nargs(); ++i)
623                 if (i % ncols() != col) {
624                         tmpcells.push_back(cells_[i]);
625                         tmpcellinfo.push_back(cellinfo_[i]);
626                 }
627         swap(cells_, tmpcells);
628         swap(cellinfo_, tmpcellinfo);
629
630         colinfo_.erase(colinfo_.begin() + col);
631 }
632
633
634 void MathGridInset::copyCol(col_type col)
635 {
636         addCol(col);
637         for (row_type row = 0; row < nrows(); ++row)
638                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
639 }
640
641
642 void MathGridInset::swapCol(col_type col)
643 {
644         if (ncols() == 1)
645                 return;
646         if (col + 1 == ncols())
647                 --col;
648         for (row_type row = 0; row < nrows(); ++row)
649                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
650 }
651
652
653 int MathGridInset::cellXOffset(idx_type idx) const
654 {
655         col_type c = col(idx);
656         int x = colinfo_[c].offset_;
657         char align = colinfo_[c].align;
658         if (align == 'r' || align == 'R')
659                 x += colinfo_[c].width_ - cell(idx).width();
660         if (align == 'c' || align == 'C')
661                 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
662         return x;
663 }
664
665
666 int MathGridInset::cellYOffset(idx_type idx) const
667 {
668         return rowinfo_[row(idx)].offset_;
669 }
670
671
672 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
673         int targetx) const
674 {
675         if (up) {
676                 if (idx < ncols())
677                         return false;
678                 idx -= ncols();
679                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
680                 return true;
681         } else {
682                 if (idx >= ncols() * (nrows() - 1))
683                         return false;
684                 idx += ncols();
685                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
686                 return true;
687         }
688 }
689
690
691 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
692 {
693         // leave matrix if on the left hand edge
694         if (col(idx) == 0)
695                 return false;
696         --idx;
697         pos = cell(idx).size();
698         return true;
699 }
700
701
702 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
703 {
704         // leave matrix if on the right hand edge
705         if (col(idx) + 1 == ncols())
706                 return false;
707         ++idx;
708         pos = 0;
709         return true;
710 }
711
712
713 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
714 {
715         switch (v_align_) {
716                 case 't':
717                         idx = 0;
718                         break;
719                 case 'b':
720                         idx = (nrows() - 1) * ncols();
721                         break;
722                 default:
723                         idx = ((nrows() - 1) / 2) * ncols();
724         }
725         pos = 0;
726         return true;
727 }
728
729
730 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
731 {
732         switch (v_align_) {
733                 case 't':
734                         idx = ncols() - 1;
735                         break;
736                 case 'b':
737                         idx = nargs() - 1;
738                         break;
739                 default:
740                         idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
741         }
742         pos = cell(idx).size();
743         return true;
744 }
745
746
747 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
748 {
749         if (pos > 0) {
750                 pos = 0;
751                 return true;
752         }
753         if (col(idx) > 0) {
754                 idx -= idx % ncols();
755                 pos = 0;
756                 return true;
757         }
758         if (idx > 0) {
759                 idx = 0;
760                 pos = 0;
761                 return true;
762         }
763         return false;
764 }
765
766
767 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
768 {
769         if (pos < cell(idx).size()) {
770                 pos = cell(idx).size();
771                 return true;
772         }
773         if (col(idx) < ncols() - 1) {
774                 idx = idx - idx % ncols() + ncols() - 1;
775                 pos = cell(idx).size();
776                 return true;
777         }
778         if (idx < nargs() - 1) {
779                 idx = nargs() - 1;
780                 pos = cell(idx).size();
781                 return true;
782         }
783         return false;
784 }
785
786
787 bool MathGridInset::idxDelete(idx_type & idx)
788 {
789         // nothing to do if we have just one row
790         if (nrows() == 1)
791                 return false;
792
793         // nothing to do if we are in the middle of the last row of the inset
794         if (idx + ncols() > nargs())
795                 return false;
796
797         // try to delete entire sequence of ncols() empty cells if possible
798         for (idx_type i = idx; i < idx + ncols(); ++i)
799                 if (cell(i).size())
800                         return false;
801
802         // move cells if necessary
803         for (idx_type i = index(row(idx), 0); i < idx; ++i)
804                 std::swap(cell(i), cell(i + ncols()));
805
806         delRow(row(idx));
807
808         if (idx >= nargs())
809                 idx = nargs() - 1;
810
811         // undo effect of Ctrl-Tab (i.e. pull next cell)
812         //if (idx + 1 != nargs())
813         //      cell(idx).swap(cell(idx + 1));
814
815         // we handled the event..
816         return true;
817 }
818
819
820 // reimplement old behaviour when pressing Delete in the last position
821 // of a cell
822 void MathGridInset::idxGlue(idx_type idx)
823 {
824         col_type c = col(idx);
825         if (c + 1 == ncols()) {
826                 if (row(idx) + 1 != nrows()) {
827                         for (col_type cc = 0; cc < ncols(); ++cc)
828                                 cell(idx).append(cell(idx + cc + 1));
829                         delRow(row(idx) + 1);
830                 }
831         } else {
832                 cell(idx).append(cell(idx + 1));
833                 for (col_type cc = c + 2; cc < ncols(); ++cc)
834                         cell(idx - c + cc - 1) = cell(idx - c + cc);
835                 cell(idx - c + ncols() - 1).clear();
836         }
837 }
838
839
840 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
841 {
842         return rowinfo_[row];
843 }
844
845
846 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
847 {
848         return rowinfo_[row];
849 }
850
851
852 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
853 {
854         row_type const ri = row(idx);
855         row_type const r1 = min(row(from), row(to));
856         row_type const r2 = max(row(from), row(to));
857         col_type const ci = col(idx);
858         col_type const c1 = min(col(from), col(to));
859         col_type const c2 = max(col(from), col(to));
860         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
861 }
862
863
864
865 void MathGridInset::normalize(NormalStream & os) const
866 {
867         os << "[grid ";
868         for (row_type row = 0; row < nrows(); ++row) {
869                 os << "[row ";
870                 for (col_type col = 0; col < ncols(); ++col)
871                         os << "[cell " << cell(index(row, col)) << ']';
872                 os << ']';
873         }
874         os << ']';
875 }
876
877
878 void MathGridInset::mathmlize(MathMLStream & os) const
879 {
880         os << MTag("mtable");
881         for (row_type row = 0; row < nrows(); ++row) {
882                 os << MTag("mtr");
883                 for (col_type col = 0; col < ncols(); ++col)
884                         os << cell(index(row, col));
885                 os << ETag("mtr");
886         }
887         os << ETag("mtable");
888 }
889
890
891 void MathGridInset::write(WriteStream & os) const
892 {
893         for (row_type row = 0; row < nrows(); ++row) {
894                 os << verboseHLine(rowinfo_[row].lines_);
895                 // don't write & and empty cells at end of line
896                 col_type lastcol = 0;
897                 bool emptyline = true;
898                 for (col_type col = 0; col < ncols(); ++col)
899                         if (!cell(index(row, col)).empty()) {
900                                 lastcol = col + 1;
901                                 emptyline = false;
902                         }
903                 for (col_type col = 0; col < lastcol; ++col)
904                         os << cell(index(row, col)) << eocString(col, lastcol);
905                 os << eolString(row, os.fragile());
906                 // append newline only if line wasn't completely empty
907                 // and this was not the last line in the grid
908                 if (!emptyline && row + 1 < nrows())
909                         os << "\n";
910         }
911         string const s = verboseHLine(rowinfo_[nrows()].lines_);
912         if (!s.empty() && s != " ") {
913                 if (os.fragile())
914                         os << "\\protect";
915                 os << "\\\\" << s;
916         }
917 }
918
919
920 int MathGridInset::colsep() const
921 {
922         return 6;
923 }
924
925
926 int MathGridInset::rowsep() const
927 {
928         return 6;
929 }
930
931
932 int MathGridInset::hlinesep() const
933 {
934         return 3;
935 }
936
937
938 int MathGridInset::vlinesep() const
939 {
940         return 3;
941 }
942
943
944 int MathGridInset::border() const
945 {
946         return 1;
947 }
948
949
950 void MathGridInset::splitCell(idx_type & idx, pos_type & pos)
951 {
952         if (idx + 1 == nargs())
953                 return;
954         MathArray ar = cell(idx);
955         ar.erase(0, pos);
956         cell(idx).erase(pos, cell(idx).size());
957         ++idx;
958         pos = 0;
959         cell(idx).insert(0, ar);
960 }
961
962
963 dispatch_result MathGridInset::dispatch
964         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
965 {
966         switch (cmd.action) {
967
968                 case LFUN_MOUSE_RELEASE:
969                         //if (cmd.button() == mouse_button::button3) {
970                         //      GridInsetMailer mailer(*this);
971                         //      mailer.showDialog();
972                         //      return DISPATCHED;
973                         //}
974                         break;
975
976                 case LFUN_INSET_DIALOG_UPDATE: {
977                         GridInsetMailer mailer(*this);
978                         mailer.updateDialog(cmd.view());
979                         break;
980                 }
981
982                 // insert file functions
983                 case LFUN_DELETE_LINE_FORWARD:
984                         //autocorrect_ = false;
985                         //macroModeClose();
986                         //if (selection_) {
987                         //      selDel();
988                         //      return;
989                         //}
990                         if (nrows() > 1)
991                                 delRow(row(idx));
992                         if (idx >= nargs())
993                                 idx = nargs() - 1;
994                         if (pos > cell(idx).size())
995                                 pos = cell(idx).size();
996                         return DISPATCHED_POP;
997
998                 case LFUN_CELL_SPLIT:
999                         //bv->lockedInsetStoreUndo(Undo::EDIT);
1000                         splitCell(idx, pos);
1001                         return DISPATCHED_POP;
1002
1003                 case LFUN_BREAKLINE: {
1004                         //bv->lockedInsetStoreUndo(Undo::INSERT);
1005                         row_type const r = row(idx);
1006                         addRow(r);
1007
1008                         // split line
1009                         for (col_type c = col(idx) + 1; c < ncols(); ++c)
1010                                 std::swap(cell(index(r, c)), cell(index(r + 1, c)));
1011
1012                         // split cell
1013                         splitCell(idx, pos);
1014                         std::swap(cell(idx), cell(idx + ncols() - 1));
1015                         if (idx > 0)
1016                                 --idx;
1017                         pos = cell(idx).size();
1018
1019                         //mathcursor->normalize();
1020                         return DISPATCHED_POP;
1021                 }
1022
1023                 case LFUN_TABULAR_FEATURE: {
1024                         //lyxerr << "handling tabular-feature " << cmd.argument << "\n";
1025                         istringstream is(cmd.argument);
1026                         string s;
1027                         is >> s;
1028                         if (s == "valign-top")
1029                                 valign('t');
1030                         else if (s == "valign-center")
1031                                 valign('c');
1032                         else if (s == "valign-bottom")
1033                                 valign('b');
1034                         else if (s == "align-left")
1035                                 halign('l', col(idx));
1036                         else if (s == "align-right")
1037                                 halign('r', col(idx));
1038                         else if (s == "align-center")
1039                                 halign('c', col(idx));
1040                         else if (s == "append-row")
1041                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1042                                         addRow(row(idx));
1043                         else if (s == "delete-row") 
1044                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1045                                         delRow(row(idx));
1046                                         if (idx > nargs())
1047                                                 idx -= ncols();
1048                                 }
1049                         else if (s == "copy-row") 
1050                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1051                                         copyRow(row(idx));
1052                         else if (s == "swap-row")
1053                                 swapRow(row(idx));
1054                         else if (s == "append-column") 
1055                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1056                                         row_type r = row(idx);
1057                                         col_type c = col(idx);
1058                                         addCol(c);
1059                                         idx = index(r, c);
1060                                 }
1061                         else if (s == "delete-column") 
1062                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1063                                         row_type r = row(idx);
1064                                         col_type c = col(idx);
1065                                         delCol(col(idx));
1066                                         idx = index(r, c);
1067                                         if (idx > nargs())
1068                                                 idx -= ncols();
1069                                 }
1070                         else if (s == "copy-column")
1071                                 copyCol(col(idx));
1072                         else if (s == "swap-column")
1073                                 swapCol(col(idx));
1074                         else
1075                                 return UNDISPATCHED;
1076                         lyxerr << "returning DISPATCHED_POP\n";
1077                         return DISPATCHED_POP;
1078                 }
1079
1080                 case LFUN_PASTE: {
1081                         //lyxerr << "pasting '" << cmd.argument << "'\n";
1082                         MathGridInset grid(1, 1);
1083                         mathed_parse_normal(grid, cmd.argument);
1084                         if (grid.nargs() == 1) {
1085                                 // single cell/part of cell
1086                                 cell(idx).insert(pos, grid.cell(0));
1087                                 pos += grid.cell(0).size();
1088                         } else {
1089                                 // multiple cells
1090                                 col_type const numcols = min(grid.ncols(), ncols() - col(idx));
1091                                 row_type const numrows = min(grid.nrows(), nrows() - row(idx));
1092                                 for (row_type r = 0; r < numrows; ++r) {
1093                                         for (col_type c = 0; c < numcols; ++c) {
1094                                                 idx_type i = index(r + row(idx), c + col(idx));
1095                                                 cell(i).append(grid.cell(grid.index(r, c)));
1096                                         }
1097                                         // append the left over horizontal cells to the last column
1098                                         idx_type i = index(r + row(idx), ncols() - 1);
1099                                         for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1100                                                 cell(i).append(grid.cell(grid.index(r, c)));
1101                                 }
1102                                 // append the left over vertical cells to the last _cell_
1103                                 idx_type i = nargs() - 1;
1104                                 for (row_type r = numrows; r < grid.nrows(); ++r)
1105                                         for (col_type c = 0; c < grid.ncols(); ++c)
1106                                                 cell(i).append(grid.cell(grid.index(r, c)));
1107                         }
1108                         return DISPATCHED_POP;
1109                 }
1110
1111                 default:
1112                         return MathNestInset::dispatch(cmd, idx, pos);
1113         }
1114         return UNDISPATCHED;
1115 }