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