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