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