]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathGrid.cpp
432a39a2f26b0be7d58f1a5805d2cea7864c291e
[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 "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 void InsetMathGrid::mathmlize(MathStream & os) const
971 {
972         bool const havetable = nrows() > 1;
973         if (havetable)
974                 os << MTag("mtable");
975         for (row_type row = 0; row < nrows(); ++row) {
976                 os << MTag("mrow");
977                 for (col_type col = 0; col < ncols(); ++col)
978                         os << cell(index(row, col));
979                 os << ETag("mrow");
980         }
981         if (havetable)
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                 // unless there are vertical lines
1001                 col_type lastcol = 0;
1002                 bool emptyline = true;
1003                 for (col_type col = beg_col; col < end_col; ++col)
1004                         if (!cell(index(row, col)).empty()
1005                                   || colinfo_[col + 1].lines_) {
1006                                 lastcol = col + 1;
1007                                 emptyline = false;
1008                         }
1009                 for (col_type col = beg_col; col < lastcol; ++col) {
1010                         os << cell(index(row, col));
1011                         if (os.pendingBrace())
1012                                 ModeSpecifier specifier(os, TEXT_MODE);
1013                         os << eocString(col, lastcol);
1014                 }
1015                 eol = eolString(row, os.fragile());
1016                 os << eol;
1017                 // append newline only if line wasn't completely empty
1018                 // and the formula is not written on a single line
1019                 bool const empty = emptyline && eol.empty();
1020                 if (!empty && nrows() > 1)
1021                         os << "\n";
1022         }
1023         // @TODO use end_row instead of nrows() ?
1024         docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1025         if (!s.empty()) {
1026                 if (eol.empty()) {
1027                         if (os.fragile())
1028                                 os << "\\protect";
1029                         os << "\\\\";
1030                 }
1031                 os << s;
1032         }
1033 }
1034
1035
1036 int InsetMathGrid::colsep() const
1037 {
1038         return 6;
1039 }
1040
1041
1042 int InsetMathGrid::rowsep() const
1043 {
1044         return 6;
1045 }
1046
1047
1048 int InsetMathGrid::hlinesep() const
1049 {
1050         return 3;
1051 }
1052
1053
1054 int InsetMathGrid::vlinesep() const
1055 {
1056         return 3;
1057 }
1058
1059
1060 int InsetMathGrid::border() const
1061 {
1062         return 1;
1063 }
1064
1065
1066 void InsetMathGrid::splitCell(Cursor & cur)
1067 {
1068         if (cur.idx() == cur.lastidx())
1069                 return;
1070         MathData ar = cur.cell();
1071         ar.erase(0, cur.pos());
1072         cur.cell().erase(cur.pos(), cur.lastpos());
1073         ++cur.idx();
1074         cur.pos() = 0;
1075         cur.cell().insert(0, ar);
1076 }
1077
1078
1079 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1080 {
1081         //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1082
1083         Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1084
1085         switch (cmd.action) {
1086
1087         // insert file functions
1088         case LFUN_LINE_DELETE:
1089                 cur.recordUndoInset();
1090                 //autocorrect_ = false;
1091                 //macroModeClose();
1092                 //if (selection_) {
1093                 //      selDel();
1094                 //      break;
1095                 //}
1096                 if (nrows() > 1)
1097                         delRow(cur.row());
1098                 if (cur.idx() > cur.lastidx())
1099                         cur.idx() = cur.lastidx();
1100                 if (cur.pos() > cur.lastpos())
1101                         cur.pos() = cur.lastpos();
1102                 break;
1103
1104         case LFUN_CELL_SPLIT:
1105                 cur.recordUndo();
1106                 splitCell(cur);
1107                 break;
1108
1109         case LFUN_CELL_BACKWARD:
1110                 // See below.
1111                 cur.setSelection(false);
1112                 if (!idxPrev(cur)) {
1113                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1114                         cur.undispatched();
1115                 }
1116                 break;
1117
1118         case LFUN_CELL_FORWARD:
1119                 // Can't handle selection by additional 'shift' as this is
1120                 // hard bound to LFUN_CELL_BACKWARD
1121                 cur.setSelection(false);
1122                 if (!idxNext(cur)) {
1123                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1124                         cur.undispatched();
1125                 }
1126                 break;
1127
1128         case LFUN_NEWLINE_INSERT: {
1129                 cur.recordUndoInset();
1130                 row_type const r = cur.row();
1131                 addRow(r);
1132
1133                 // split line
1134                 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1135                         swap(cell(index(r, c)), cell(index(r + 1, c)));
1136
1137                 // split cell
1138                 splitCell(cur);
1139                 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1140                 if (cur.idx() > 0)
1141                         --cur.idx();
1142                 cur.pos() = cur.lastpos();
1143
1144                 //mathcursor->normalize();
1145                 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1146                 break;
1147         }
1148
1149         case LFUN_TABULAR_FEATURE: {
1150                 cur.recordUndoInset();
1151                 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1152                 istringstream is(to_utf8(cmd.argument()));
1153                 string s;
1154                 is >> s;
1155                 if (s == "valign-top")
1156                         setVerticalAlignment('t');
1157                 else if (s == "valign-middle")
1158                         setVerticalAlignment('c');
1159                 else if (s == "valign-bottom")
1160                         setVerticalAlignment('b');
1161                 else if (s == "align-left")
1162                         setHorizontalAlignment('l', cur.col());
1163                 else if (s == "align-right")
1164                         setHorizontalAlignment('r', cur.col());
1165                 else if (s == "align-center")
1166                         setHorizontalAlignment('c', cur.col());
1167                 else if (s == "append-row")
1168                         for (int i = 0, n = extractInt(is); i < n; ++i)
1169                                 addRow(cur.row());
1170                 else if (s == "delete-row") {
1171                         cur.clearSelection(); // bug 4323
1172                         for (int i = 0, n = extractInt(is); i < n; ++i) {
1173                                 delRow(cur.row());
1174                                 if (cur.idx() >= nargs())
1175                                         cur.idx() -= ncols();
1176                         }
1177                         cur.pos() = 0; // trick, see below
1178                 }
1179                 else if (s == "copy-row") {
1180                         // Here (as later) we save the cursor col/row
1181                         // in order to restore it after operation.
1182                         row_type const r = cur.row();
1183                         col_type const c = cur.col();
1184                         for (int i = 0, n = extractInt(is); i < n; ++i)
1185                                 copyRow(cur.row());
1186                         cur.idx() = index(r, c);
1187                 }
1188                 else if (s == "swap-row") {
1189                         swapRow(cur.row());
1190                         // Trick to suppress same-idx-means-different-cell
1191                         // assertion crash:
1192                         cur.pos() = 0;
1193                 }
1194                 else if (s == "add-hline-above")
1195                         rowinfo_[cur.row()].lines_++;
1196                 else if (s == "add-hline-below")
1197                         rowinfo_[cur.row()+1].lines_++;
1198                 else if (s == "delete-hline-above")
1199                         rowinfo_[cur.row()].lines_--;
1200                 else if (s == "delete-hline-below")
1201                         rowinfo_[cur.row()+1].lines_--;
1202                 else if (s == "append-column") {
1203                         row_type const r = cur.row();
1204                         col_type const c = cur.col();
1205                         for (int i = 0, n = extractInt(is); i < n; ++i)
1206                                 addCol(cur.col() + 1);
1207                         cur.idx() = index(r, c);
1208                 }
1209                 else if (s == "delete-column") {
1210                         cur.clearSelection(); // bug 4323
1211                         row_type const r = cur.row();
1212                         col_type const c = cur.col();
1213                         for (int i = 0, n = extractInt(is); i < n; ++i)
1214                                 delCol(col(cur.idx()));
1215                         cur.idx() = index(r, min(c, cur.ncols() - 1));
1216                         cur.pos() = 0; // trick, see above
1217                 }
1218                 else if (s == "copy-column") {
1219                         row_type const r = cur.row();
1220                         col_type const c = cur.col();
1221                         copyCol(cur.col());
1222                         cur.idx() = index(r, c);
1223                 }
1224                 else if (s == "swap-column") {
1225                         swapCol(cur.col());
1226                         cur.pos() = 0; // trick, see above
1227                 }
1228                 else if (s == "add-vline-left") {
1229                         colinfo_[cur.col()].lines_++;
1230                         if (!colinfo_[cur.col()].special_.empty())
1231                                 colinfo_[cur.col()].special_ += '|';
1232                 }
1233                 else if (s == "add-vline-right") {
1234                         colinfo_[cur.col()+1].lines_++;
1235                         if (!colinfo_[cur.col()+1].special_.empty())
1236                                 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1237                 }
1238                 else if (s == "delete-vline-left") {
1239                         colinfo_[cur.col()].lines_--;
1240                         docstring & special = colinfo_[cur.col()].special_;
1241                         if (!special.empty()) {
1242                                 docstring::size_type i = special.rfind('|');
1243                                 LASSERT(i != docstring::npos, /**/);
1244                                 special.erase(i, 1);
1245                         }
1246                 }
1247                 else if (s == "delete-vline-right") {
1248                         colinfo_[cur.col()+1].lines_--;
1249                         docstring & special = colinfo_[cur.col()+1].special_;
1250                         if (!special.empty()) {
1251                                 docstring::size_type i = special.find('|');
1252                                 LASSERT(i != docstring::npos, /**/);
1253                                 special.erase(i, 1);
1254                         }
1255                 }
1256                 else {
1257                         cur.undispatched();
1258                         break;
1259                 }
1260                 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1261                 //lyxerr << "returning FINISHED_LEFT" << endl;
1262                 break;
1263         }
1264
1265         case LFUN_CLIPBOARD_PASTE:
1266                 parseflg |= Parse::VERBATIM;
1267                 // fall through
1268         case LFUN_PASTE: {
1269                 if (cur.currentMode() <= TEXT_MODE)
1270                         parseflg |= Parse::TEXTMODE;
1271                 cur.message(_("Paste"));
1272                 cap::replaceSelection(cur);
1273                 docstring topaste;
1274                 if (cmd.argument().empty() && !theClipboard().isInternal())
1275                         topaste = theClipboard().getAsText();
1276                 else {
1277                         idocstringstream is(cmd.argument());
1278                         int n = 0;
1279                         is >> n;
1280                         topaste = cap::selection(n);
1281                 }
1282                 InsetMathGrid grid(buffer_, 1, 1);
1283                 if (!topaste.empty())
1284                         if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1285                             || !mathed_parse_normal(grid, topaste, parseflg)) {
1286                                 resetGrid(grid);
1287                                 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1288                         }
1289
1290                 if (grid.nargs() == 1) {
1291                         // single cell/part of cell
1292                         cur.recordUndo();
1293                         cur.cell().insert(cur.pos(), grid.cell(0));
1294                         cur.pos() += grid.cell(0).size();
1295                 } else {
1296                         // multiple cells
1297                         cur.recordUndoInset();
1298                         col_type const numcols =
1299                                 min(grid.ncols(), ncols() - col(cur.idx()));
1300                         row_type const numrows =
1301                                 min(grid.nrows(), nrows() - cur.row());
1302                         for (row_type r = 0; r < numrows; ++r) {
1303                                 for (col_type c = 0; c < numcols; ++c) {
1304                                         idx_type i = index(r + cur.row(), c + col(cur.idx()));
1305                                         cell(i).insert(0, grid.cell(grid.index(r, c)));
1306                                 }
1307                                 // append the left over horizontal cells to the last column
1308                                 idx_type i = index(r + cur.row(), ncols() - 1);
1309                                 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1310                                         cell(i).append(grid.cell(grid.index(r, c)));
1311                         }
1312                         // append the left over vertical cells to the last _cell_
1313                         idx_type i = nargs() - 1;
1314                         for (row_type r = numrows; r < grid.nrows(); ++r)
1315                                 for (col_type c = 0; c < grid.ncols(); ++c)
1316                                         cell(i).append(grid.cell(grid.index(r, c)));
1317                 }
1318                 cur.clearSelection(); // bug 393
1319                 cur.finishUndo();
1320                 break;
1321         }
1322
1323         case LFUN_LINE_BEGIN_SELECT:
1324         case LFUN_LINE_BEGIN:
1325         case LFUN_WORD_BACKWARD_SELECT:
1326         case LFUN_WORD_BACKWARD:
1327         case LFUN_WORD_LEFT_SELECT:
1328         case LFUN_WORD_LEFT:
1329                 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1330                                 cmd.action == LFUN_WORD_LEFT_SELECT ||
1331                                 cmd.action == LFUN_LINE_BEGIN_SELECT);
1332                 cur.macroModeClose();
1333                 if (cur.pos() != 0) {
1334                         cur.pos() = 0;
1335                 } else if (cur.idx() % cur.ncols() != 0) {
1336                         cur.idx() -= cur.idx() % cur.ncols();
1337                         cur.pos() = 0;
1338                 } else if (cur.idx() != 0) {
1339                         cur.idx() = 0;
1340                         cur.pos() = 0;
1341                 } else {
1342                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1343                         cur.undispatched();
1344                 }
1345                 break;
1346
1347         case LFUN_WORD_FORWARD_SELECT:
1348         case LFUN_WORD_FORWARD:
1349         case LFUN_WORD_RIGHT_SELECT:
1350         case LFUN_WORD_RIGHT:
1351         case LFUN_LINE_END_SELECT:
1352         case LFUN_LINE_END:
1353                 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1354                                 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1355                                 cmd.action == LFUN_LINE_END_SELECT);
1356                 cur.macroModeClose();
1357                 cur.clearTargetX();
1358                 if (cur.pos() != cur.lastpos()) {
1359                         cur.pos() = cur.lastpos();
1360                 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1361                         cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1362                         cur.pos() = cur.lastpos();
1363                 } else if (cur.idx() != cur.lastidx()) {
1364                         cur.idx() = cur.lastidx();
1365                         cur.pos() = cur.lastpos();
1366                 } else {
1367                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1368                         cur.undispatched();
1369                 }
1370                 break;
1371
1372         default:
1373                 InsetMathNest::doDispatch(cur, cmd);
1374         }
1375 }
1376
1377
1378 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1379                 FuncStatus & status) const
1380 {
1381         switch (cmd.action) {
1382         case LFUN_TABULAR_FEATURE: {
1383                 string const s = cmd.getArg(0);
1384                 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1385                         status.setEnabled(false);
1386                         status.message(from_utf8(N_("Only one row")));
1387                         return true;
1388                 }
1389                 if (ncols() <= 1 &&
1390                     (s == "delete-column" || s == "swap-column")) {
1391                         status.setEnabled(false);
1392                         status.message(from_utf8(N_("Only one column")));
1393                         return true;
1394                 }
1395                 if ((rowinfo_[cur.row()].lines_ == 0 &&
1396                      s == "delete-hline-above") ||
1397                     (rowinfo_[cur.row() + 1].lines_ == 0 &&
1398                      s == "delete-hline-below")) {
1399                         status.setEnabled(false);
1400                         status.message(from_utf8(N_("No hline to delete")));
1401                         return true;
1402                 }
1403
1404                 if ((colinfo_[cur.col()].lines_ == 0 &&
1405                      s == "delete-vline-left") ||
1406                     (colinfo_[cur.col() + 1].lines_ == 0 &&
1407                      s == "delete-vline-right")) {
1408                         status.setEnabled(false);
1409                         status.message(from_utf8(N_("No vline to delete")));
1410                         return true;
1411                 }
1412                 if (s == "valign-top" || s == "valign-middle" ||
1413                     s == "valign-bottom" || s == "align-left" ||
1414                     s == "align-right" || s == "align-center") {
1415                         status.setEnabled(true);
1416                         char const ha = horizontalAlignment(cur.col());
1417                         char const va = verticalAlignment();
1418                         status.setOnOff((s == "align-left" && ha == 'l')
1419                                         || (s == "align-right"   && ha == 'r')
1420                                         || (s == "align-center"  && ha == 'c')
1421                                         || (s == "valign-top"    && va == 't')
1422                                         || (s == "valign-bottom" && va == 'b')
1423                                         || (s == "valign-middle" && va == 'c'));
1424                         return true;
1425                 }
1426                 if (s == "append-row" || s == "delete-row" ||
1427                     s == "copy-row" || s == "swap-row" ||
1428                     s == "add-hline-above" || s == "add-hline-below" ||
1429                     s == "delete-hline-above" || s == "delete-hline-below" ||
1430                     s == "append-column" || s == "delete-column" ||
1431                     s == "copy-column" || s == "swap-column" ||
1432                     s == "add-vline-left" || s == "add-vline-right" ||
1433                     s == "delete-vline-left" || s == "delete-vline-right") {
1434                         status.setEnabled(true);
1435                 } else {
1436                         status.setEnabled(false);
1437                         status.message(bformat(
1438                                 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1439                 }
1440
1441 #if 0
1442                 // FIXME: What did this code do?
1443                 // Please check whether it is still needed!
1444                 // should be more precise
1445                 if (v_align_ == '\0') {
1446                         status.enable(true);
1447                         break;
1448                 }
1449                 if (cmd.argument().empty()) {
1450                         status.enable(false);
1451                         break;
1452                 }
1453                 if (!contains("tcb", cmd.argument()[0])) {
1454                         status.enable(false);
1455                         break;
1456                 }
1457                 status.setOnOff(cmd.argument()[0] == v_align_);
1458                 status.setEnabled(true);
1459 #endif
1460                 return true;
1461         }
1462
1463         case LFUN_CELL_SPLIT:
1464                 status.setEnabled(true);
1465                 return true;
1466
1467         case LFUN_CELL_BACKWARD:
1468         case LFUN_CELL_FORWARD:
1469                 status.setEnabled(true);
1470                 return true;
1471
1472         default:
1473                 return InsetMathNest::getStatus(cur, cmd, status);
1474         }
1475 }
1476
1477
1478 } // namespace lyx