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