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