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