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