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