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