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