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