]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathGrid.cpp
InsetLine.cpp: remove unused include
[lyx.git] / src / mathed / InsetMathGrid.cpp
1 /**
2  * \file InsetMathGrid.cpp
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 #include <algorithm>
13
14 #include "InsetMathGrid.h"
15
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
20
21 #include "Buffer.h"
22 #include "BufferView.h"
23 #include "CutAndPaste.h"
24 #include "FuncStatus.h"
25 #include "Cursor.h"
26 #include "FuncRequest.h"
27
28 #include "frontends/Clipboard.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 #include "support/lassert.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44
45
46 namespace lyx {
47
48 static docstring verboseHLine(int n)
49 {
50         docstring res;
51         for (int i = 0; i < n; ++i)
52                 res += "\\hline";
53         if (n)
54                 res += ' ';
55         return res;
56 }
57
58
59 static int extractInt(istream & is)
60 {
61         int num = 1;
62         is >> num;
63         return (num == 0) ? 1 : num;
64 }
65
66
67 static void resetGrid(InsetMathGrid & grid)
68 {
69         while (grid.ncols() > 1)
70                 grid.delCol(grid.ncols());
71         while (grid.nrows() > 1)
72                 grid.delRow(grid.nrows());
73         grid.cell(0).erase(0, grid.cell(0).size());
74         grid.setDefaults();
75 }
76
77
78
79 //////////////////////////////////////////////////////////////
80
81
82 InsetMathGrid::CellInfo::CellInfo()
83         : dummy_(false)
84 {}
85
86
87
88 //////////////////////////////////////////////////////////////
89
90
91 InsetMathGrid::RowInfo::RowInfo()
92         : lines_(0), skip_(0), allow_newpage_(true)
93 {}
94
95
96
97 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
98 {
99         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
100         return crskip_.inPixels(mi.base.textwidth,
101                                 fm.width(char_type('M')));
102 }
103
104
105
106 //////////////////////////////////////////////////////////////
107
108
109 InsetMathGrid::ColInfo::ColInfo()
110         : align_('c'), lines_(0)
111 {}
112
113
114 //////////////////////////////////////////////////////////////
115
116
117 InsetMathGrid::InsetMathGrid(Buffer * buf)
118         : InsetMathNest(buf, 1),
119           rowinfo_(1 + 1),
120                 colinfo_(1 + 1),
121                 cellinfo_(1),
122                 v_align_('c')
123 {
124         setDefaults();
125 }
126
127
128 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
129         : InsetMathNest(buf, m * n),
130           rowinfo_(n + 1),
131                 colinfo_(m + 1),
132                 cellinfo_(m * n),
133                 v_align_('c')
134 {
135         setDefaults();
136 }
137
138
139 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
140         docstring const & h)
141         : InsetMathNest(buf, m * n),
142           rowinfo_(n + 1),
143           colinfo_(m + 1),
144                 cellinfo_(m * n),
145                 v_align_(v)
146 {
147         setDefaults();
148         setVerticalAlignment(v);
149         setHorizontalAlignments(h);
150 }
151
152
153 Inset * InsetMathGrid::clone() const
154 {
155         return new InsetMathGrid(*this);
156 }
157
158
159 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
160 {
161         return col + ncols() * row;
162 }
163
164
165 void InsetMathGrid::setDefaults()
166 {
167         if (ncols() <= 0)
168                 lyxerr << "positive number of columns expected" << endl;
169         //if (nrows() <= 0)
170         //      lyxerr << "positive number of rows expected" << endl;
171         for (col_type col = 0; col < ncols(); ++col) {
172                 colinfo_[col].align_ = defaultColAlign(col);
173                 colinfo_[col].skip_  = defaultColSpace(col);
174                 colinfo_[col].special_.clear();
175         }
176 }
177
178
179 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
180 {
181         col_type col = 0;
182         for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
183                 char_type c = *it;
184                 if (c == '|') {
185                         colinfo_[col].lines_++;
186                 } else if ((c == 'p' || c == 'm' || c == 'b'||
187                             c == '!' || c == '@' || c == '>' || c == '<') &&
188                            it + 1 != hh.end() && *(it + 1) == '{') {
189                         // @{decl.} and p{width} are standard LaTeX, the
190                         // others are extensions by array.sty
191                         bool const newcolumn = c == 'p' || c == 'm' || c == 'b';
192                         if (newcolumn) {
193                                 // this declares a new column
194                                 if (col >= ncols())
195                                         // Only intercolumn stuff is allowed
196                                         // in the last dummy column
197                                         break;
198                                 colinfo_[col].align_ = 'l';
199                         } else {
200                                 // this is intercolumn stuff
201                                 if (colinfo_[col].special_.empty())
202                                         // Overtake possible lines
203                                         colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');
204                         }
205                         int brace_open = 0;
206                         int brace_close = 0;
207                         while (it != hh.end()) {
208                                 c = *it;
209                                 colinfo_[col].special_ += c;
210                                 if (c == '{')
211                                         ++brace_open;
212                                 else if (c == '}')
213                                         ++brace_close;
214                                 ++it;
215                                 if (brace_open > 0 && brace_open == brace_close)
216                                         break;
217                         }
218                         --it;
219                         if (newcolumn) {
220                                 colinfo_[col].lines_ = count(
221                                         colinfo_[col].special_.begin(),
222                                         colinfo_[col].special_.end(), '|');
223                                 LYXERR(Debug::MATHED, "special column separator: `"
224                                         << to_utf8(colinfo_[col].special_) << '\'');
225                                 ++col;
226                                 colinfo_[col].lines_ = 0;
227                                 colinfo_[col].special_.clear();
228                         }
229                 } else if (col >= ncols()) {
230                         // Only intercolumn stuff is allowed in the last
231                         // dummy column
232                         break;
233                 } else if (c == 'c' || c == 'l' || c == 'r') {
234                         colinfo_[col].align_ = static_cast<char>(c);
235                         if (!colinfo_[col].special_.empty()) {
236                                 colinfo_[col].special_ += c;
237                                 colinfo_[col].lines_ = count(
238                                                 colinfo_[col].special_.begin(),
239                                                 colinfo_[col].special_.end(), '|');
240                                 LYXERR(Debug::MATHED, "special column separator: `"
241                                         << to_utf8(colinfo_[col].special_) << '\'');
242                         }
243                         ++col;
244                         colinfo_[col].lines_ = 0;
245                         colinfo_[col].special_.clear();
246                 } else {
247                         lyxerr << "unknown column separator: '" << c << "'" << endl;
248                 }
249         }
250
251 /*
252         col_type n = hh.size();
253         if (n > ncols())
254                 n = ncols();
255         for (col_type col = 0; col < n; ++col)
256                 colinfo_[col].align_ = hh[col];
257 */
258 }
259
260
261 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
262 {
263         col_type col = 0;
264         for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
265                 if (*it == 'c' || *it == 'l' || *it == 'r'||
266                     *it == 'p' || *it == 'm' || *it == 'b')
267                         ++col;
268         // let's have at least one column, even if we did not recognize its
269         // alignment
270         if (col == 0)
271                 col = 1;
272         return col;
273 }
274
275
276 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
277 {
278         colinfo_[col].align_ = h;
279         if (!colinfo_[col].special_.empty()) {
280                 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];
281                 if (c == 'l' || c == 'c' || c == 'r')
282                         c = h;
283         }
284         // FIXME: Change alignment of p, m and b columns, too
285 }
286
287
288 char InsetMathGrid::horizontalAlignment(col_type col) const
289 {
290         return colinfo_[col].align_;
291 }
292
293
294 docstring InsetMathGrid::horizontalAlignments() const
295 {
296         docstring res;
297         for (col_type col = 0; col < ncols(); ++col) {
298                 if (colinfo_[col].special_.empty()) {
299                         res += docstring(colinfo_[col].lines_, '|');
300                         res += colinfo_[col].align_;
301                 } else
302                         res += colinfo_[col].special_;
303         }
304         if (colinfo_[ncols()].special_.empty())
305                 return res + docstring(colinfo_[ncols()].lines_, '|');
306         return res + colinfo_[ncols()].special_;
307 }
308
309
310 void InsetMathGrid::setVerticalAlignment(char c)
311 {
312         v_align_ = c;
313 }
314
315
316 char InsetMathGrid::verticalAlignment() const
317 {
318         return v_align_;
319 }
320
321
322 InsetMathGrid::col_type InsetMathGrid::ncols() const
323 {
324         return colinfo_.size() - 1;
325 }
326
327
328 InsetMathGrid::row_type InsetMathGrid::nrows() const
329 {
330         return rowinfo_.size() - 1;
331 }
332
333
334 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
335 {
336         return idx % ncols();
337 }
338
339
340 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
341 {
342         return idx / ncols();
343 }
344
345
346 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)
347 {
348         rowinfo_[row].crskip_ = crskip;
349 }
350
351
352 Length InsetMathGrid::vcrskip(row_type row) const
353 {
354         return rowinfo_[row].crskip_;
355 }
356
357
358 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
359 {
360         // let the cells adjust themselves
361         InsetMathNest::metrics(mi);
362
363         BufferView & bv = *mi.base.bv;
364
365         // compute absolute sizes of vertical structure
366         for (row_type row = 0; row < nrows(); ++row) {
367                 int asc  = 0;
368                 int desc = 0;
369                 for (col_type col = 0; col < ncols(); ++col) {
370                         Dimension const & dimc = cell(index(row, col)).dimension(bv);
371                         asc  = max(asc,  dimc.asc);
372                         desc = max(desc, dimc.des);
373                 }
374                 rowinfo_[row].ascent_  = asc;
375                 rowinfo_[row].descent_ = desc;
376         }
377         rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
378         rowinfo_[nrows()].ascent_  = 0;
379         rowinfo_[nrows()].descent_ = 0;
380
381         // compute vertical offsets
382         rowinfo_[0].offset_ = 0;
383         for (row_type row = 1; row <= nrows(); ++row) {
384                 rowinfo_[row].offset_  =
385                         rowinfo_[row - 1].offset_  +
386                         rowinfo_[row - 1].descent_ +
387                         rowinfo_[row - 1].skipPixels(mi) +
388                         rowsep() +
389                         rowinfo_[row].lines_ * hlinesep() +
390                         rowinfo_[row].ascent_;
391         }
392
393         // adjust vertical offset
394         int h = 0;
395         switch (v_align_) {
396                 case 't':
397                         h = 0;
398                         break;
399                 case 'b':
400                         h = rowinfo_[nrows() - 1].offset_;
401                         break;
402                 default:
403                         h = rowinfo_[nrows() - 1].offset_ / 2;
404         }
405         for (row_type row = 0; row <= nrows(); ++row)
406                 rowinfo_[row].offset_ -= h;
407
408
409         // compute absolute sizes of horizontal structure
410         for (col_type col = 0; col < ncols(); ++col) {
411                 int wid = 0;
412                 for (row_type row = 0; row < nrows(); ++row)
413                         wid = max(wid, cell(index(row, col)).dimension(bv).wid);
414                 colinfo_[col].width_ = wid;
415         }
416         colinfo_[ncols()].width_  = 0;
417
418         // compute horizontal offsets
419         colinfo_[0].offset_ = border();
420         for (col_type col = 1; col <= ncols(); ++col) {
421                 colinfo_[col].offset_ =
422                         colinfo_[col - 1].offset_ +
423                         colinfo_[col - 1].width_ +
424                         colinfo_[col - 1].skip_ +
425                         colsep() +
426                         colinfo_[col].lines_ * vlinesep();
427         }
428
429
430         dim.wid   =   colinfo_[ncols() - 1].offset_
431                        + colinfo_[ncols() - 1].width_
432                  + vlinesep() * colinfo_[ncols()].lines_
433                        + border();
434
435         dim.asc  = - rowinfo_[0].offset_
436                        + rowinfo_[0].ascent_
437                  + hlinesep() * rowinfo_[0].lines_
438                        + border();
439
440         dim.des =   rowinfo_[nrows() - 1].offset_
441                        + rowinfo_[nrows() - 1].descent_
442                  + hlinesep() * rowinfo_[nrows()].lines_
443                        + border();
444
445
446 /*
447         // Increase ws_[i] for 'R' columns (except the first one)
448         for (int i = 1; i < nc_; ++i)
449                 if (align_[i] == 'R')
450                         ws_[i] += 10 * df_width;
451         // Increase ws_[i] for 'C' column
452         if (align_[0] == 'C')
453                 if (ws_[0] < 7 * workwidth / 8)
454                         ws_[0] = 7 * workwidth / 8;
455
456         // Adjust local tabs
457         width = colsep();
458         for (cxrow = row_.begin(); cxrow; ++cxrow) {
459                 int rg = COLSEP;
460                 int lf = 0;
461                 for (int i = 0; i < nc_; ++i) {
462                         bool isvoid = false;
463                         if (cxrow->getTab(i) <= 0) {
464                                 cxrow->setTab(i, df_width);
465                                 isvoid = true;
466                         }
467                         switch (align_[i]) {
468                         case 'l':
469                                 lf = 0;
470                                 break;
471                         case 'c':
472                                 lf = (ws_[i] - cxrow->getTab(i))/2;
473                                 break;
474                         case 'r':
475                         case 'R':
476                                 lf = ws_[i] - cxrow->getTab(i);
477                                 break;
478                         case 'C':
479                                 if (cxrow == row_.begin())
480                                         lf = 0;
481                                 else if (cxrow.is_last())
482                                         lf = ws_[i] - cxrow->getTab(i);
483                                 else
484                                         lf = (ws_[i] - cxrow->getTab(i))/2;
485                                 break;
486                         }
487                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
488                         cxrow->setTab(i, lf + rg);
489                         rg = ws_[i] - ww + colsep();
490                         if (cxrow == row_.begin())
491                                 width += ws_[i] + colsep();
492                 }
493                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
494         }
495 */
496         metricsMarkers2(dim);
497         // Cache the inset dimension.
498         setDimCache(mi, dim);
499 }
500
501
502 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
503 {
504         drawWithMargin(pi, x, y, 1, 1);
505 }
506
507
508 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
509         int lmargin, int rmargin) const
510 {
511         Dimension const dim = dimension(*pi.base.bv);
512         BufferView const & bv = *pi.base.bv;
513
514         for (idx_type idx = 0; idx < nargs(); ++idx)
515                 cell(idx).draw(pi, x + lmargin + cellXOffset(bv, idx),
516                         y + cellYOffset(idx));
517
518         for (row_type row = 0; row <= nrows(); ++row)
519                 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
520                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
521                                 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
522                         pi.pain.line(x + lmargin + 1, yy,
523                                      x + dim.width() - rmargin - 1, yy,
524                                      Color_foreground);
525                 }
526
527         for (col_type col = 0; col <= ncols(); ++col)
528                 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
529                         int xx = x + lmargin + colinfo_[col].offset_
530                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;
531                         pi.pain.line(xx, y - dim.ascent() + 1,
532                                      xx, y + dim.descent() - 1,
533                                      Color_foreground);
534                 }
535         drawMarkers2(pi, x, y);
536 }
537
538
539 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
540 {
541         // let the cells adjust themselves
542         //InsetMathNest::metrics(mi);
543         for (idx_type i = 0; i < nargs(); ++i)
544                 cell(i).metricsT(mi, dim);
545
546         // compute absolute sizes of vertical structure
547         for (row_type row = 0; row < nrows(); ++row) {
548                 int asc  = 0;
549                 int desc = 0;
550                 for (col_type col = 0; col < ncols(); ++col) {
551                         //MathData const & c = cell(index(row, col));
552                         // FIXME: BROKEN!
553                         Dimension dimc;
554                         asc  = max(asc,  dimc.ascent());
555                         desc = max(desc, dimc.descent());
556                 }
557                 rowinfo_[row].ascent_  = asc;
558                 rowinfo_[row].descent_ = desc;
559         }
560         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
561         rowinfo_[nrows()].ascent_  = 0;
562         rowinfo_[nrows()].descent_ = 0;
563
564         // compute vertical offsets
565         rowinfo_[0].offset_ = 0;
566         for (row_type row = 1; row <= nrows(); ++row) {
567                 rowinfo_[row].offset_  =
568                         rowinfo_[row - 1].offset_  +
569                         rowinfo_[row - 1].descent_ +
570                         //rowinfo_[row - 1].skipPixels(mi) +
571                         1 + //rowsep() +
572                         //rowinfo_[row].lines_ * hlinesep() +
573                         rowinfo_[row].ascent_;
574         }
575
576         // adjust vertical offset
577         int h = 0;
578         switch (v_align_) {
579                 case 't':
580                         h = 0;
581                         break;
582                 case 'b':
583                         h = rowinfo_[nrows() - 1].offset_;
584                         break;
585                 default:
586                         h = rowinfo_[nrows() - 1].offset_ / 2;
587         }
588         for (row_type row = 0; row <= nrows(); ++row)
589                 rowinfo_[row].offset_ -= h;
590
591
592         // compute absolute sizes of horizontal structure
593         for (col_type col = 0; col < ncols(); ++col) {
594                 int wid = 0;
595                 for (row_type row = 0; row < nrows(); ++row) {
596                         // FIXME: BROKEN!
597                         //wid = max(wid, cell(index(row, col)).width());
598                 }
599                 colinfo_[col].width_ = wid;
600         }
601         colinfo_[ncols()].width_  = 0;
602
603         // compute horizontal offsets
604         colinfo_[0].offset_ = border();
605         for (col_type col = 1; col <= ncols(); ++col) {
606                 colinfo_[col].offset_ =
607                         colinfo_[col - 1].offset_ +
608                         colinfo_[col - 1].width_ +
609                         colinfo_[col - 1].skip_ +
610                         1 ; //colsep() +
611                         //colinfo_[col].lines_ * vlinesep();
612         }
613
614
615         dim.wid  =  colinfo_[ncols() - 1].offset_
616                        + colinfo_[ncols() - 1].width_
617                  //+ vlinesep() * colinfo_[ncols()].lines_
618                        + 2;
619
620         dim.asc  = -rowinfo_[0].offset_
621                        + rowinfo_[0].ascent_
622                  //+ hlinesep() * rowinfo_[0].lines_
623                        + 1;
624
625         dim.des  =  rowinfo_[nrows() - 1].offset_
626                        + rowinfo_[nrows() - 1].descent_
627                  //+ hlinesep() * rowinfo_[nrows()].lines_
628                        + 1;
629 }
630
631
632 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
633 {
634 //      for (idx_type idx = 0; idx < nargs(); ++idx)
635 //              cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
636 }
637
638
639 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
640 {
641         // pass down
642         for (idx_type idx = 0; idx < nargs(); ++idx)
643                 cell(idx).updateBuffer(it, utype);
644 }
645
646
647 docstring InsetMathGrid::eolString(row_type row, bool fragile, bool last_eoln) const
648 {
649         docstring eol;
650
651         if (!rowinfo_[row].crskip_.zero())
652                 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
653         else if(!rowinfo_[row].allow_newpage_)
654                 eol += '*';
655
656         // make sure an upcoming '[' does not break anything
657         if (row + 1 < nrows()) {
658                 MathData const & c = cell(index(row + 1, 0));
659                 if (c.size() && c.front()->getChar() == '[')
660                         //eol += "[0pt]";
661                         eol += "{}";
662         }
663
664         // only add \\ if necessary
665         if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
666                 return docstring();
667
668         return (fragile ? "\\protect\\\\" : "\\\\") + eol;
669 }
670
671
672 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
673 {
674         if (col + 1 == lastcol)
675                 return docstring();
676         return from_ascii(" & ");
677 }
678
679
680 void InsetMathGrid::addRow(row_type row)
681 {
682         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
683         cells_.insert
684                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
685         cellinfo_.insert
686                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
687 }
688
689
690 void InsetMathGrid::appendRow()
691 {
692         rowinfo_.push_back(RowInfo());
693         //cells_.insert(cells_.end(), ncols(), MathData());
694         for (col_type col = 0; col < ncols(); ++col) {
695                 cells_.push_back(cells_type::value_type());
696                 cellinfo_.push_back(CellInfo());
697         }
698 }
699
700
701 void InsetMathGrid::delRow(row_type row)
702 {
703         if (nrows() == 1)
704                 return;
705
706         cells_type::iterator it = cells_.begin() + row * ncols();
707         cells_.erase(it, it + ncols());
708
709         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
710         cellinfo_.erase(jt, jt + ncols());
711
712         rowinfo_.erase(rowinfo_.begin() + row);
713 }
714
715
716 void InsetMathGrid::copyRow(row_type row)
717 {
718         addRow(row);
719         for (col_type col = 0; col < ncols(); ++col)
720                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
721 }
722
723
724 void InsetMathGrid::swapRow(row_type row)
725 {
726         if (nrows() == 1)
727                 return;
728         if (row + 1 == nrows())
729                 --row;
730         for (col_type col = 0; col < ncols(); ++col)
731                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
732 }
733
734
735 void InsetMathGrid::addCol(col_type newcol)
736 {
737         const col_type nc = ncols();
738         const row_type nr = nrows();
739         cells_type new_cells((nc + 1) * nr);
740         vector<CellInfo> new_cellinfo((nc + 1) * nr);
741
742         for (row_type row = 0; row < nr; ++row)
743                 for (col_type col = 0; col < nc; ++col) {
744                         new_cells[row * (nc + 1) + col + (col >= newcol)]
745                                 = cells_[row * nc + col];
746                         new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
747                                 = cellinfo_[row * nc + col];
748                 }
749         swap(cells_, new_cells);
750         swap(cellinfo_, new_cellinfo);
751
752         ColInfo inf;
753         inf.skip_  = defaultColSpace(newcol);
754         inf.align_ = defaultColAlign(newcol);
755         colinfo_.insert(colinfo_.begin() + newcol, inf);
756 }
757
758
759 void InsetMathGrid::delCol(col_type col)
760 {
761         if (ncols() == 1)
762                 return;
763
764         cells_type tmpcells;
765         vector<CellInfo> tmpcellinfo;
766         for (col_type i = 0; i < nargs(); ++i)
767                 if (i % ncols() != col) {
768                         tmpcells.push_back(cells_[i]);
769                         tmpcellinfo.push_back(cellinfo_[i]);
770                 }
771         swap(cells_, tmpcells);
772         swap(cellinfo_, tmpcellinfo);
773
774         colinfo_.erase(colinfo_.begin() + col);
775 }
776
777
778 void InsetMathGrid::copyCol(col_type col)
779 {
780         addCol(col+1);
781         for (row_type row = 0; row < nrows(); ++row)
782                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
783 }
784
785
786 void InsetMathGrid::swapCol(col_type col)
787 {
788         if (ncols() == 1)
789                 return;
790         if (col + 1 == ncols())
791                 --col;
792         for (row_type row = 0; row < nrows(); ++row)
793                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
794 }
795
796
797 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
798 {
799         col_type c = col(idx);
800         int x = colinfo_[c].offset_;
801         char align = colinfo_[c].align_;
802         Dimension const & celldim = cell(idx).dimension(bv);
803         if (align == 'r' || align == 'R')
804                 x += colinfo_[c].width_ - celldim.wid;
805         if (align == 'c' || align == 'C')
806                 x += (colinfo_[c].width_ - celldim.wid) / 2;
807         return x;
808 }
809
810
811 int InsetMathGrid::cellYOffset(idx_type idx) const
812 {
813         return rowinfo_[row(idx)].offset_;
814 }
815
816
817 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
818 {
819         if (up) {
820                 if (cur.row() == 0)
821                         return false;
822                 cur.idx() -= ncols();
823         } else {
824                 if (cur.row() + 1 >= nrows())
825                         return false;
826                 cur.idx() += ncols();
827         }
828         cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
829         return true;
830 }
831
832
833 bool InsetMathGrid::idxBackward(Cursor & cur) const
834 {
835         // leave matrix if at the front edge
836         if (cur.col() == 0)
837                 return false;
838         --cur.idx();
839         cur.pos() = cur.lastpos();
840         return true;
841 }
842
843
844 bool InsetMathGrid::idxForward(Cursor & cur) const
845 {
846         // leave matrix if at the back edge
847         if (cur.col() + 1 == ncols())
848                 return false;
849         ++cur.idx();
850         cur.pos() = 0;
851         return true;
852 }
853
854
855 bool InsetMathGrid::idxFirst(Cursor & cur) const
856 {
857         switch (v_align_) {
858                 case 't':
859                         cur.idx() = 0;
860                         break;
861                 case 'b':
862                         cur.idx() = (nrows() - 1) * ncols();
863                         break;
864                 default:
865                         cur.idx() = ((nrows() - 1) / 2) * ncols();
866         }
867         cur.pos() = 0;
868         return true;
869 }
870
871
872 bool InsetMathGrid::idxLast(Cursor & cur) const
873 {
874         switch (v_align_) {
875                 case 't':
876                         cur.idx() = ncols() - 1;
877                         break;
878                 case 'b':
879                         cur.idx() = nargs() - 1;
880                         break;
881                 default:
882                         cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
883         }
884         cur.pos() = cur.lastpos();
885         return true;
886 }
887
888
889 bool InsetMathGrid::idxDelete(idx_type & idx)
890 {
891         // nothing to do if we have just one row
892         if (nrows() == 1)
893                 return false;
894
895         // nothing to do if we are in the middle of the last row of the inset
896         if (idx + ncols() > nargs())
897                 return false;
898
899         // try to delete entire sequence of ncols() empty cells if possible
900         for (idx_type i = idx; i < idx + ncols(); ++i)
901                 if (cell(i).size())
902                         return false;
903
904         // move cells if necessary
905         for (idx_type i = index(row(idx), 0); i < idx; ++i)
906                 swap(cell(i), cell(i + ncols()));
907
908         delRow(row(idx));
909
910         if (idx >= nargs())
911                 idx = nargs() - 1;
912
913         // undo effect of Ctrl-Tab (i.e. pull next cell)
914         //if (idx + 1 != nargs())
915         //      cell(idx).swap(cell(idx + 1));
916
917         // we handled the event..
918         return true;
919 }
920
921
922 // reimplement old behaviour when pressing Delete in the last position
923 // of a cell
924 void InsetMathGrid::idxGlue(idx_type idx)
925 {
926         col_type c = col(idx);
927         if (c + 1 == ncols()) {
928                 if (row(idx) + 1 != nrows()) {
929                         for (col_type cc = 0; cc < ncols(); ++cc)
930                                 cell(idx).append(cell(idx + cc + 1));
931                         delRow(row(idx) + 1);
932                 }
933         } else {
934                 cell(idx).append(cell(idx + 1));
935                 for (col_type cc = c + 2; cc < ncols(); ++cc)
936                         cell(idx - c + cc - 1) = cell(idx - c + cc);
937                 cell(idx - c + ncols() - 1).clear();
938         }
939 }
940
941
942 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
943 {
944         return rowinfo_[row];
945 }
946
947
948 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
949 {
950         return rowinfo_[row];
951 }
952
953
954 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
955 {
956         row_type const ri = row(idx);
957         row_type const r1 = min(row(from), row(to));
958         row_type const r2 = max(row(from), row(to));
959         col_type const ci = col(idx);
960         col_type const c1 = min(col(from), col(to));
961         col_type const c2 = max(col(from), col(to));
962         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
963 }
964
965
966
967 void InsetMathGrid::normalize(NormalStream & os) const
968 {
969         os << "[grid ";
970         for (row_type row = 0; row < nrows(); ++row) {
971                 os << "[row ";
972                 for (col_type col = 0; col < ncols(); ++col)
973                         os << "[cell " << cell(index(row, col)) << ']';
974                 os << ']';
975         }
976         os << ']';
977 }
978
979
980 void InsetMathGrid::mathmlize(MathStream & os) const
981 {
982         bool const havetable = nrows() > 1 || ncols() > 1;
983         if (havetable)
984                 os << MTag("mtable");
985         char const * const celltag = havetable ? "mtd" : "mrow";
986         for (row_type row = 0; row < nrows(); ++row) {
987                 if (havetable)
988                         os << MTag("mtr");;
989                 for (col_type col = 0; col < ncols(); ++col) {
990                         os << MTag(celltag);
991                         os << cell(index(row, col));
992                         os << ETag(celltag);
993                 }
994                 if (havetable)
995                         os << ETag("mtr");;
996         }
997         if (havetable)
998                 os << ETag("mtable");
999 }
1000
1001
1002 // FIXME XHTML
1003 // We need to do something about alignment here.
1004 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
1005 {
1006         bool const havetable = nrows() > 1 || ncols() > 1;
1007         if (!havetable) {
1008                 os << cell(index(0, 0));
1009                 return;
1010         }
1011         os << MTag("table", attrib);
1012         for (row_type row = 0; row < nrows(); ++row) {
1013                 os << MTag("tr");;
1014                 for (col_type col = 0; col < ncols(); ++col) {
1015                         os << MTag("td");
1016                         os << cell(index(row, col));
1017                         os << ETag("td");
1018                 }
1019                 os << ETag("tr");;
1020         }
1021         os << ETag("table");
1022 }
1023
1024
1025 void InsetMathGrid::htmlize(HtmlStream & os) const
1026 {
1027         htmlize(os, "class='mathtable'");
1028 }
1029
1030
1031 void InsetMathGrid::write(WriteStream & os) const
1032 {
1033         write(os, 0, 0, nrows(), ncols());
1034 }
1035
1036 void InsetMathGrid::write(WriteStream & os,
1037                           row_type beg_row, col_type beg_col,
1038                           row_type end_row, col_type end_col) const
1039 {
1040         MathEnsurer ensurer(os, false);
1041         docstring eol;
1042         for (row_type row = beg_row; row < end_row; ++row) {
1043                 os << verboseHLine(rowinfo_[row].lines_);
1044                 // don't write & and empty cells at end of line,
1045                 // unless there are vertical lines
1046                 col_type lastcol = 0;
1047                 bool emptyline = true;
1048                 bool last_eoln = true;
1049                 for (col_type col = beg_col; col < end_col; ++col) {
1050                         bool const empty_cell = cell(index(row, col)).empty();
1051                         if (!empty_cell)
1052                                 last_eoln = false;
1053                         if (!empty_cell || colinfo_[col + 1].lines_) {
1054                                 lastcol = col + 1;
1055                                 emptyline = false;
1056                         }
1057                 }
1058                 for (col_type col = beg_col; col < lastcol; ++col) {
1059                         os << cell(index(row, col));
1060                         if (os.pendingBrace())
1061                                 ModeSpecifier specifier(os, TEXT_MODE);
1062                         os << eocString(col, lastcol);
1063                 }
1064                 eol = eolString(row, os.fragile(), last_eoln);
1065                 os << eol;
1066                 // append newline only if line wasn't completely empty
1067                 // and the formula is not written on a single line
1068                 bool const empty = emptyline && eol.empty();
1069                 if (!empty && nrows() > 1)
1070                         os << "\n";
1071         }
1072         // @TODO use end_row instead of nrows() ?
1073         docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1074         if (!s.empty()) {
1075                 if (eol.empty()) {
1076                         if (os.fragile())
1077                                 os << "\\protect";
1078                         os << "\\\\";
1079                 }
1080                 os << s;
1081         }
1082 }
1083
1084
1085 int InsetMathGrid::colsep() const
1086 {
1087         return 6;
1088 }
1089
1090
1091 int InsetMathGrid::rowsep() const
1092 {
1093         return 6;
1094 }
1095
1096
1097 int InsetMathGrid::hlinesep() const
1098 {
1099         return 3;
1100 }
1101
1102
1103 int InsetMathGrid::vlinesep() const
1104 {
1105         return 3;
1106 }
1107
1108
1109 int InsetMathGrid::border() const
1110 {
1111         return 1;
1112 }
1113
1114
1115 void InsetMathGrid::splitCell(Cursor & cur)
1116 {
1117         if (cur.idx() == cur.lastidx())
1118                 return;
1119         MathData ar = cur.cell();
1120         ar.erase(0, cur.pos());
1121         cur.cell().erase(cur.pos(), cur.lastpos());
1122         ++cur.idx();
1123         cur.pos() = 0;
1124         cur.cell().insert(0, ar);
1125 }
1126
1127
1128 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1129 {
1130         //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1131
1132         Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1133
1134         FuncCode const act = cmd.action();
1135         switch (act) {
1136
1137         // insert file functions
1138         case LFUN_LINE_DELETE:
1139                 cur.recordUndoInset();
1140                 //autocorrect_ = false;
1141                 //macroModeClose();
1142                 //if (selection_) {
1143                 //      selDel();
1144                 //      break;
1145                 //}
1146                 if (nrows() > 1)
1147                         delRow(cur.row());
1148                 if (cur.idx() > cur.lastidx())
1149                         cur.idx() = cur.lastidx();
1150                 if (cur.pos() > cur.lastpos())
1151                         cur.pos() = cur.lastpos();
1152                 break;
1153
1154         case LFUN_CELL_SPLIT:
1155                 cur.recordUndo();
1156                 splitCell(cur);
1157                 break;
1158
1159         case LFUN_CELL_BACKWARD:
1160                 // See below.
1161                 cur.setSelection(false);
1162                 if (!idxPrev(cur)) {
1163                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1164                         cur.undispatched();
1165                 }
1166                 break;
1167
1168         case LFUN_CELL_FORWARD:
1169                 // Can't handle selection by additional 'shift' as this is
1170                 // hard bound to LFUN_CELL_BACKWARD
1171                 cur.setSelection(false);
1172                 if (!idxNext(cur)) {
1173                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1174                         cur.undispatched();
1175                 }
1176                 break;
1177
1178         case LFUN_NEWLINE_INSERT: {
1179                 cur.recordUndoInset();
1180                 row_type const r = cur.row();
1181                 addRow(r);
1182
1183                 // split line
1184                 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1185                         swap(cell(index(r, c)), cell(index(r + 1, c)));
1186
1187                 // split cell
1188                 splitCell(cur);
1189                 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1190                 if (cur.idx() > 0)
1191                         --cur.idx();
1192                 cur.pos() = cur.lastpos();
1193
1194                 //mathcursor->normalize();
1195                 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1196                 break;
1197         }
1198
1199         case LFUN_INSET_MODIFY: {
1200                 cur.recordUndoInset();
1201                 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1202                 istringstream is(to_utf8(cmd.argument()));
1203                 string s;
1204                 is >> s;
1205                 if (s != "tabular") {
1206                         InsetMathNest::doDispatch(cur, cmd);
1207                         return;
1208                 }
1209                 is >> s;
1210                 if (s == "valign-top")
1211                         setVerticalAlignment('t');
1212                 else if (s == "valign-middle")
1213                         setVerticalAlignment('c');
1214                 else if (s == "valign-bottom")
1215                         setVerticalAlignment('b');
1216                 else if (s == "align-left")
1217                         setHorizontalAlignment('l', cur.col());
1218                 else if (s == "align-right")
1219                         setHorizontalAlignment('r', cur.col());
1220                 else if (s == "align-center")
1221                         setHorizontalAlignment('c', cur.col());
1222                 else if (s == "append-row")
1223                         for (int i = 0, n = extractInt(is); i < n; ++i)
1224                                 addRow(cur.row());
1225                 else if (s == "delete-row") {
1226                         cur.clearSelection(); // bug 4323
1227                         for (int i = 0, n = extractInt(is); i < n; ++i) {
1228                                 delRow(cur.row());
1229                                 if (cur.idx() >= nargs())
1230                                         cur.idx() -= ncols();
1231                         }
1232                         cur.pos() = 0; // trick, see below
1233                 }
1234                 else if (s == "copy-row") {
1235                         // Here (as later) we save the cursor col/row
1236                         // in order to restore it after operation.
1237                         row_type const r = cur.row();
1238                         col_type const c = cur.col();
1239                         for (int i = 0, n = extractInt(is); i < n; ++i)
1240                                 copyRow(cur.row());
1241                         cur.idx() = index(r, c);
1242                 }
1243                 else if (s == "swap-row") {
1244                         swapRow(cur.row());
1245                         // Trick to suppress same-idx-means-different-cell
1246                         // assertion crash:
1247                         cur.pos() = 0;
1248                 }
1249                 else if (s == "add-hline-above")
1250                         rowinfo_[cur.row()].lines_++;
1251                 else if (s == "add-hline-below")
1252                         rowinfo_[cur.row()+1].lines_++;
1253                 else if (s == "delete-hline-above")
1254                         rowinfo_[cur.row()].lines_--;
1255                 else if (s == "delete-hline-below")
1256                         rowinfo_[cur.row()+1].lines_--;
1257                 else if (s == "append-column") {
1258                         row_type const r = cur.row();
1259                         col_type const c = cur.col();
1260                         for (int i = 0, n = extractInt(is); i < n; ++i)
1261                                 addCol(cur.col() + 1);
1262                         cur.idx() = index(r, c);
1263                 }
1264                 else if (s == "delete-column") {
1265                         cur.clearSelection(); // bug 4323
1266                         row_type const r = cur.row();
1267                         col_type const c = cur.col();
1268                         for (int i = 0, n = extractInt(is); i < n; ++i)
1269                                 delCol(col(cur.idx()));
1270                         cur.idx() = index(r, min(c, cur.ncols() - 1));
1271                         cur.pos() = 0; // trick, see above
1272                 }
1273                 else if (s == "copy-column") {
1274                         row_type const r = cur.row();
1275                         col_type const c = cur.col();
1276                         copyCol(cur.col());
1277                         cur.idx() = index(r, c);
1278                 }
1279                 else if (s == "swap-column") {
1280                         swapCol(cur.col());
1281                         cur.pos() = 0; // trick, see above
1282                 }
1283                 else if (s == "add-vline-left") {
1284                         colinfo_[cur.col()].lines_++;
1285                         if (!colinfo_[cur.col()].special_.empty())
1286                                 colinfo_[cur.col()].special_ += '|';
1287                 }
1288                 else if (s == "add-vline-right") {
1289                         colinfo_[cur.col()+1].lines_++;
1290                         if (!colinfo_[cur.col()+1].special_.empty())
1291                                 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1292                 }
1293                 else if (s == "delete-vline-left") {
1294                         colinfo_[cur.col()].lines_--;
1295                         docstring & special = colinfo_[cur.col()].special_;
1296                         if (!special.empty()) {
1297                                 docstring::size_type i = special.rfind('|');
1298                                 LASSERT(i != docstring::npos, /**/);
1299                                 special.erase(i, 1);
1300                         }
1301                 }
1302                 else if (s == "delete-vline-right") {
1303                         colinfo_[cur.col()+1].lines_--;
1304                         docstring & special = colinfo_[cur.col()+1].special_;
1305                         if (!special.empty()) {
1306                                 docstring::size_type i = special.find('|');
1307                                 LASSERT(i != docstring::npos, /**/);
1308                                 special.erase(i, 1);
1309                         }
1310                 }
1311                 else {
1312                         cur.undispatched();
1313                         break;
1314                 }
1315                 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1316                 //lyxerr << "returning FINISHED_LEFT" << endl;
1317                 break;
1318         }
1319
1320         case LFUN_CLIPBOARD_PASTE:
1321                 parseflg |= Parse::VERBATIM;
1322                 // fall through
1323         case LFUN_PASTE: {
1324                 if (cur.currentMode() <= TEXT_MODE)
1325                         parseflg |= Parse::TEXTMODE;
1326                 cur.message(_("Paste"));
1327                 cap::replaceSelection(cur);
1328                 docstring topaste;
1329                 if (cmd.argument().empty() && !theClipboard().isInternal())
1330                         topaste = theClipboard().getAsText();
1331                 else {
1332                         idocstringstream is(cmd.argument());
1333                         int n = 0;
1334                         is >> n;
1335                         topaste = cap::selection(n);
1336                 }
1337                 InsetMathGrid grid(buffer_, 1, 1);
1338                 if (!topaste.empty())
1339                         if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1340                             || !mathed_parse_normal(grid, topaste, parseflg)) {
1341                                 resetGrid(grid);
1342                                 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1343                         }
1344
1345                 if (grid.nargs() == 1) {
1346                         // single cell/part of cell
1347                         cur.recordUndo();
1348                         cur.cell().insert(cur.pos(), grid.cell(0));
1349                         cur.pos() += grid.cell(0).size();
1350                 } else {
1351                         // multiple cells
1352                         cur.recordUndoInset();
1353                         col_type const numcols =
1354                                 min(grid.ncols(), ncols() - col(cur.idx()));
1355                         row_type const numrows =
1356                                 min(grid.nrows(), nrows() - cur.row());
1357                         for (row_type r = 0; r < numrows; ++r) {
1358                                 for (col_type c = 0; c < numcols; ++c) {
1359                                         idx_type i = index(r + cur.row(), c + col(cur.idx()));
1360                                         cell(i).insert(0, grid.cell(grid.index(r, c)));
1361                                 }
1362                                 // append the left over horizontal cells to the last column
1363                                 idx_type i = index(r + cur.row(), ncols() - 1);
1364                                 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1365                                         cell(i).append(grid.cell(grid.index(r, c)));
1366                         }
1367                         // append the left over vertical cells to the last _cell_
1368                         idx_type i = nargs() - 1;
1369                         for (row_type r = numrows; r < grid.nrows(); ++r)
1370                                 for (col_type c = 0; c < grid.ncols(); ++c)
1371                                         cell(i).append(grid.cell(grid.index(r, c)));
1372                 }
1373                 cur.clearSelection(); // bug 393
1374                 // FIXME audit setBuffer calls
1375                 cur.inset().setBuffer(*buffer_);
1376                 cur.forceBufferUpdate();
1377                 cur.finishUndo();
1378                 break;
1379         }
1380
1381         case LFUN_LINE_BEGIN_SELECT:
1382         case LFUN_LINE_BEGIN:
1383         case LFUN_WORD_BACKWARD_SELECT:
1384         case LFUN_WORD_BACKWARD:
1385         case LFUN_WORD_LEFT_SELECT:
1386         case LFUN_WORD_LEFT:
1387                 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1388                                 act == LFUN_WORD_LEFT_SELECT ||
1389                                 act == LFUN_LINE_BEGIN_SELECT);
1390                 cur.macroModeClose();
1391                 if (cur.pos() != 0) {
1392                         cur.pos() = 0;
1393                 } else if (cur.idx() % cur.ncols() != 0) {
1394                         cur.idx() -= cur.idx() % cur.ncols();
1395                         cur.pos() = 0;
1396                 } else if (cur.idx() != 0) {
1397                         cur.idx() = 0;
1398                         cur.pos() = 0;
1399                 } else {
1400                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1401                         cur.undispatched();
1402                 }
1403                 break;
1404
1405         case LFUN_WORD_FORWARD_SELECT:
1406         case LFUN_WORD_FORWARD:
1407         case LFUN_WORD_RIGHT_SELECT:
1408         case LFUN_WORD_RIGHT:
1409         case LFUN_LINE_END_SELECT:
1410         case LFUN_LINE_END:
1411                 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1412                                 act == LFUN_WORD_RIGHT_SELECT ||
1413                                 act == LFUN_LINE_END_SELECT);
1414                 cur.macroModeClose();
1415                 cur.clearTargetX();
1416                 if (cur.pos() != cur.lastpos()) {
1417                         cur.pos() = cur.lastpos();
1418                 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1419                         cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1420                         cur.pos() = cur.lastpos();
1421                 } else if (cur.idx() != cur.lastidx()) {
1422                         cur.idx() = cur.lastidx();
1423                         cur.pos() = cur.lastpos();
1424                 } else {
1425                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1426                         cur.undispatched();
1427                 }
1428                 break;
1429
1430         default:
1431                 InsetMathNest::doDispatch(cur, cmd);
1432         }
1433 }
1434
1435
1436 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1437                 FuncStatus & status) const
1438 {
1439         switch (cmd.action()) {
1440         case LFUN_INSET_MODIFY: {
1441                 istringstream is(to_utf8(cmd.argument()));
1442                 string s;
1443                 is >> s;
1444                 if (s != "tabular") {
1445                         // We only now about table actions here.
1446                         break;
1447                 }
1448                 if (&cur.inset() != this) {
1449                         // Table actions requires that the cursor is _inside_ the
1450                         // table.
1451                         status.setEnabled(false);
1452                         status.message(from_utf8(N_("Cursor not in table")));
1453                         return true;
1454                 }
1455                 is >> s;
1456                 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1457                         status.setEnabled(false);
1458                         status.message(from_utf8(N_("Only one row")));
1459                         return true;
1460                 }
1461                 if (ncols() <= 1 &&
1462                     (s == "delete-column" || s == "swap-column")) {
1463                         status.setEnabled(false);
1464                         status.message(from_utf8(N_("Only one column")));
1465                         return true;
1466                 }
1467                 if ((rowinfo_[cur.row()].lines_ == 0 &&
1468                      s == "delete-hline-above") ||
1469                     (rowinfo_[cur.row() + 1].lines_ == 0 &&
1470                      s == "delete-hline-below")) {
1471                         status.setEnabled(false);
1472                         status.message(from_utf8(N_("No hline to delete")));
1473                         return true;
1474                 }
1475
1476                 if ((colinfo_[cur.col()].lines_ == 0 &&
1477                      s == "delete-vline-left") ||
1478                     (colinfo_[cur.col() + 1].lines_ == 0 &&
1479                      s == "delete-vline-right")) {
1480                         status.setEnabled(false);
1481                         status.message(from_utf8(N_("No vline to delete")));
1482                         return true;
1483                 }
1484                 if (s == "valign-top" || s == "valign-middle" ||
1485                     s == "valign-bottom" || s == "align-left" ||
1486                     s == "align-right" || s == "align-center") {
1487                         status.setEnabled(true);
1488                         char const ha = horizontalAlignment(cur.col());
1489                         char const va = verticalAlignment();
1490                         status.setOnOff((s == "align-left" && ha == 'l')
1491                                         || (s == "align-right"   && ha == 'r')
1492                                         || (s == "align-center"  && ha == 'c')
1493                                         || (s == "valign-top"    && va == 't')
1494                                         || (s == "valign-bottom" && va == 'b')
1495                                         || (s == "valign-middle" && va == 'c'));
1496                         return true;
1497                 }
1498                 if (s == "append-row" || s == "delete-row" ||
1499                     s == "copy-row" || s == "swap-row" ||
1500                     s == "add-hline-above" || s == "add-hline-below" ||
1501                     s == "delete-hline-above" || s == "delete-hline-below" ||
1502                     s == "append-column" || s == "delete-column" ||
1503                     s == "copy-column" || s == "swap-column" ||
1504                     s == "add-vline-left" || s == "add-vline-right" ||
1505                     s == "delete-vline-left" || s == "delete-vline-right") {
1506                         status.setEnabled(true);
1507                 } else {
1508                         status.setEnabled(false);
1509                         status.message(bformat(
1510                                 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1511                 }
1512
1513 #if 0
1514                 // FIXME: What did this code do?
1515                 // Please check whether it is still needed!
1516                 // should be more precise
1517                 if (v_align_ == '\0') {
1518                         status.enable(true);
1519                         break;
1520                 }
1521                 if (cmd.argument().empty()) {
1522                         status.enable(false);
1523                         break;
1524                 }
1525                 if (!contains("tcb", cmd.argument()[0])) {
1526                         status.enable(false);
1527                         break;
1528                 }
1529                 status.setOnOff(cmd.argument()[0] == v_align_);
1530                 status.setEnabled(true);
1531 #endif
1532                 return true;
1533         }
1534
1535         case LFUN_CELL_SPLIT:
1536                 status.setEnabled(true);
1537                 return true;
1538
1539         case LFUN_CELL_BACKWARD:
1540         case LFUN_CELL_FORWARD:
1541                 status.setEnabled(true);
1542                 return true;
1543
1544         default:
1545                 break;
1546         }
1547         return InsetMathNest::getStatus(cur, cmd, status);
1548 }
1549
1550
1551 } // namespace lyx