]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathGrid.cpp
8629568037fc8acdcb522557b475b443add97cc8
[lyx.git] / src / mathed / InsetMathGrid.cpp
1 /**
2  * \file InsetMathGrid.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12 #include <algorithm>
13
14 #include "InsetMathGrid.h"
15
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
20
21 #include "Buffer.h"
22 #include "BufferView.h"
23 #include "CutAndPaste.h"
24 #include "FuncStatus.h"
25 #include "Cursor.h"
26 #include "FuncRequest.h"
27
28 #include "frontends/Clipboard.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 #include "support/lassert.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44
45 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 void InsetMathGrid::updateLabels(ParIterator const & it, UpdateType utype)
639 {
640         // pass down
641         for (idx_type idx = 0; idx < nargs(); ++idx)
642                 cell(idx).updateLabels(it, utype);
643 }
644
645
646 docstring InsetMathGrid::eolString(row_type row, bool fragile) const
647 {
648         docstring eol;
649
650         if (!rowinfo_[row].crskip_.zero())
651                 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
652         else if(!rowinfo_[row].allow_newpage_)
653                 eol += '*';
654
655         // make sure an upcoming '[' does not break anything
656         if (row + 1 < nrows()) {
657                 MathData const & c = cell(index(row + 1, 0));
658                 if (c.size() && c.front()->getChar() == '[')
659                         //eol += "[0pt]";
660                         eol += "{}";
661         }
662
663         // only add \\ if necessary
664         if (eol.empty() && row + 1 == nrows())
665                 return docstring();
666
667         return (fragile ? "\\protect\\\\" : "\\\\") + eol;
668 }
669
670
671 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const
672 {
673         if (col + 1 == lastcol)
674                 return docstring();
675         return from_ascii(" & ");
676 }
677
678
679 void InsetMathGrid::addRow(row_type row)
680 {
681         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
682         cells_.insert
683                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
684         cellinfo_.insert
685                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
686 }
687
688
689 void InsetMathGrid::appendRow()
690 {
691         rowinfo_.push_back(RowInfo());
692         //cells_.insert(cells_.end(), ncols(), MathData());
693         for (col_type col = 0; col < ncols(); ++col) {
694                 cells_.push_back(cells_type::value_type());
695                 cellinfo_.push_back(CellInfo());
696         }
697 }
698
699
700 void InsetMathGrid::delRow(row_type row)
701 {
702         if (nrows() == 1)
703                 return;
704
705         cells_type::iterator it = cells_.begin() + row * ncols();
706         cells_.erase(it, it + ncols());
707
708         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
709         cellinfo_.erase(jt, jt + ncols());
710
711         rowinfo_.erase(rowinfo_.begin() + row);
712 }
713
714
715 void InsetMathGrid::copyRow(row_type row)
716 {
717         addRow(row);
718         for (col_type col = 0; col < ncols(); ++col)
719                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
720 }
721
722
723 void InsetMathGrid::swapRow(row_type row)
724 {
725         if (nrows() == 1)
726                 return;
727         if (row + 1 == nrows())
728                 --row;
729         for (col_type col = 0; col < ncols(); ++col)
730                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
731 }
732
733
734 void InsetMathGrid::addCol(col_type newcol)
735 {
736         const col_type nc = ncols();
737         const row_type nr = nrows();
738         cells_type new_cells((nc + 1) * nr);
739         vector<CellInfo> new_cellinfo((nc + 1) * nr);
740
741         for (row_type row = 0; row < nr; ++row)
742                 for (col_type col = 0; col < nc; ++col) {
743                         new_cells[row * (nc + 1) + col + (col >= newcol)]
744                                 = cells_[row * nc + col];
745                         new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
746                                 = cellinfo_[row * nc + col];
747                 }
748         swap(cells_, new_cells);
749         swap(cellinfo_, new_cellinfo);
750
751         ColInfo inf;
752         inf.skip_  = defaultColSpace(newcol);
753         inf.align_ = defaultColAlign(newcol);
754         colinfo_.insert(colinfo_.begin() + newcol, inf);
755 }
756
757
758 void InsetMathGrid::delCol(col_type col)
759 {
760         if (ncols() == 1)
761                 return;
762
763         cells_type tmpcells;
764         vector<CellInfo> tmpcellinfo;
765         for (col_type i = 0; i < nargs(); ++i)
766                 if (i % ncols() != col) {
767                         tmpcells.push_back(cells_[i]);
768                         tmpcellinfo.push_back(cellinfo_[i]);
769                 }
770         swap(cells_, tmpcells);
771         swap(cellinfo_, tmpcellinfo);
772
773         colinfo_.erase(colinfo_.begin() + col);
774 }
775
776
777 void InsetMathGrid::copyCol(col_type col)
778 {
779         addCol(col+1);
780         for (row_type row = 0; row < nrows(); ++row)
781                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
782 }
783
784
785 void InsetMathGrid::swapCol(col_type col)
786 {
787         if (ncols() == 1)
788                 return;
789         if (col + 1 == ncols())
790                 --col;
791         for (row_type row = 0; row < nrows(); ++row)
792                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
793 }
794
795
796 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const
797 {
798         col_type c = col(idx);
799         int x = colinfo_[c].offset_;
800         char align = colinfo_[c].align_;
801         Dimension const & celldim = cell(idx).dimension(bv);
802         if (align == 'r' || align == 'R')
803                 x += colinfo_[c].width_ - celldim.wid;
804         if (align == 'c' || align == 'C')
805                 x += (colinfo_[c].width_ - celldim.wid) / 2;
806         return x;
807 }
808
809
810 int InsetMathGrid::cellYOffset(idx_type idx) const
811 {
812         return rowinfo_[row(idx)].offset_;
813 }
814
815
816 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
817 {
818         if (up) {
819                 if (cur.row() == 0)
820                         return false;
821                 cur.idx() -= ncols();
822         } else {
823                 if (cur.row() + 1 >= nrows())
824                         return false;
825                 cur.idx() += ncols();
826         }
827         cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
828         return true;
829 }
830
831
832 bool InsetMathGrid::idxBackward(Cursor & cur) const
833 {
834         // leave matrix if at the front edge
835         if (cur.col() == 0)
836                 return false;
837         --cur.idx();
838         cur.pos() = cur.lastpos();
839         return true;
840 }
841
842
843 bool InsetMathGrid::idxForward(Cursor & cur) const
844 {
845         // leave matrix if at the back edge
846         if (cur.col() + 1 == ncols())
847                 return false;
848         ++cur.idx();
849         cur.pos() = 0;
850         return true;
851 }
852
853
854 bool InsetMathGrid::idxFirst(Cursor & cur) const
855 {
856         switch (v_align_) {
857                 case 't':
858                         cur.idx() = 0;
859                         break;
860                 case 'b':
861                         cur.idx() = (nrows() - 1) * ncols();
862                         break;
863                 default:
864                         cur.idx() = ((nrows() - 1) / 2) * ncols();
865         }
866         cur.pos() = 0;
867         return true;
868 }
869
870
871 bool InsetMathGrid::idxLast(Cursor & cur) const
872 {
873         switch (v_align_) {
874                 case 't':
875                         cur.idx() = ncols() - 1;
876                         break;
877                 case 'b':
878                         cur.idx() = nargs() - 1;
879                         break;
880                 default:
881                         cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;
882         }
883         cur.pos() = cur.lastpos();
884         return true;
885 }
886
887
888 bool InsetMathGrid::idxDelete(idx_type & idx)
889 {
890         // nothing to do if we have just one row
891         if (nrows() == 1)
892                 return false;
893
894         // nothing to do if we are in the middle of the last row of the inset
895         if (idx + ncols() > nargs())
896                 return false;
897
898         // try to delete entire sequence of ncols() empty cells if possible
899         for (idx_type i = idx; i < idx + ncols(); ++i)
900                 if (cell(i).size())
901                         return false;
902
903         // move cells if necessary
904         for (idx_type i = index(row(idx), 0); i < idx; ++i)
905                 swap(cell(i), cell(i + ncols()));
906
907         delRow(row(idx));
908
909         if (idx >= nargs())
910                 idx = nargs() - 1;
911
912         // undo effect of Ctrl-Tab (i.e. pull next cell)
913         //if (idx + 1 != nargs())
914         //      cell(idx).swap(cell(idx + 1));
915
916         // we handled the event..
917         return true;
918 }
919
920
921 // reimplement old behaviour when pressing Delete in the last position
922 // of a cell
923 void InsetMathGrid::idxGlue(idx_type idx)
924 {
925         col_type c = col(idx);
926         if (c + 1 == ncols()) {
927                 if (row(idx) + 1 != nrows()) {
928                         for (col_type cc = 0; cc < ncols(); ++cc)
929                                 cell(idx).append(cell(idx + cc + 1));
930                         delRow(row(idx) + 1);
931                 }
932         } else {
933                 cell(idx).append(cell(idx + 1));
934                 for (col_type cc = c + 2; cc < ncols(); ++cc)
935                         cell(idx - c + cc - 1) = cell(idx - c + cc);
936                 cell(idx - c + ncols() - 1).clear();
937         }
938 }
939
940
941 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const
942 {
943         return rowinfo_[row];
944 }
945
946
947 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)
948 {
949         return rowinfo_[row];
950 }
951
952
953 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const
954 {
955         row_type const ri = row(idx);
956         row_type const r1 = min(row(from), row(to));
957         row_type const r2 = max(row(from), row(to));
958         col_type const ci = col(idx);
959         col_type const c1 = min(col(from), col(to));
960         col_type const c2 = max(col(from), col(to));
961         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
962 }
963
964
965
966 void InsetMathGrid::normalize(NormalStream & os) const
967 {
968         os << "[grid ";
969         for (row_type row = 0; row < nrows(); ++row) {
970                 os << "[row ";
971                 for (col_type col = 0; col < ncols(); ++col)
972                         os << "[cell " << cell(index(row, col)) << ']';
973                 os << ']';
974         }
975         os << ']';
976 }
977
978
979 void InsetMathGrid::mathmlize(MathStream & os) const
980 {
981         bool const havetable = nrows() > 1 || ncols() > 1;
982         if (havetable)
983                 os << MTag("mtable");
984         char const * const celltag = havetable ? "mtd" : "mrow";
985         for (row_type row = 0; row < nrows(); ++row) {
986                 if (havetable)
987                         os << MTag("mtr");;
988                 for (col_type col = 0; col < ncols(); ++col) {
989                         os << MTag(celltag);
990                         os << cell(index(row, col));
991                         os << ETag(celltag);
992                 }
993                 if (havetable)
994                         os << ETag("mtr");;
995         }
996         if (havetable)
997                 os << ETag("mtable");
998 }
999
1000
1001 void InsetMathGrid::write(WriteStream & os) const
1002 {
1003         write(os, 0, 0, nrows(), ncols());
1004 }
1005
1006 void InsetMathGrid::write(WriteStream & os,
1007                           row_type beg_row, col_type beg_col,
1008                           row_type end_row, col_type end_col) const
1009 {
1010         MathEnsurer ensurer(os, false);
1011         docstring eol;
1012         for (row_type row = beg_row; row < end_row; ++row) {
1013                 os << verboseHLine(rowinfo_[row].lines_);
1014                 // don't write & and empty cells at end of line,
1015                 // unless there are vertical lines
1016                 col_type lastcol = 0;
1017                 bool emptyline = true;
1018                 for (col_type col = beg_col; col < end_col; ++col)
1019                         if (!cell(index(row, col)).empty()
1020                                   || colinfo_[col + 1].lines_) {
1021                                 lastcol = col + 1;
1022                                 emptyline = false;
1023                         }
1024                 for (col_type col = beg_col; col < lastcol; ++col) {
1025                         os << cell(index(row, col));
1026                         if (os.pendingBrace())
1027                                 ModeSpecifier specifier(os, TEXT_MODE);
1028                         os << eocString(col, lastcol);
1029                 }
1030                 eol = eolString(row, os.fragile());
1031                 os << eol;
1032                 // append newline only if line wasn't completely empty
1033                 // and the formula is not written on a single line
1034                 bool const empty = emptyline && eol.empty();
1035                 if (!empty && nrows() > 1)
1036                         os << "\n";
1037         }
1038         // @TODO use end_row instead of nrows() ?
1039         docstring const s = verboseHLine(rowinfo_[nrows()].lines_);
1040         if (!s.empty()) {
1041                 if (eol.empty()) {
1042                         if (os.fragile())
1043                                 os << "\\protect";
1044                         os << "\\\\";
1045                 }
1046                 os << s;
1047         }
1048 }
1049
1050
1051 int InsetMathGrid::colsep() const
1052 {
1053         return 6;
1054 }
1055
1056
1057 int InsetMathGrid::rowsep() const
1058 {
1059         return 6;
1060 }
1061
1062
1063 int InsetMathGrid::hlinesep() const
1064 {
1065         return 3;
1066 }
1067
1068
1069 int InsetMathGrid::vlinesep() const
1070 {
1071         return 3;
1072 }
1073
1074
1075 int InsetMathGrid::border() const
1076 {
1077         return 1;
1078 }
1079
1080
1081 void InsetMathGrid::splitCell(Cursor & cur)
1082 {
1083         if (cur.idx() == cur.lastidx())
1084                 return;
1085         MathData ar = cur.cell();
1086         ar.erase(0, cur.pos());
1087         cur.cell().erase(cur.pos(), cur.lastpos());
1088         ++cur.idx();
1089         cur.pos() = 0;
1090         cur.cell().insert(0, ar);
1091 }
1092
1093
1094 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
1095 {
1096         //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
1097
1098         Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
1099
1100         switch (cmd.action) {
1101
1102         // insert file functions
1103         case LFUN_LINE_DELETE:
1104                 cur.recordUndoInset();
1105                 //autocorrect_ = false;
1106                 //macroModeClose();
1107                 //if (selection_) {
1108                 //      selDel();
1109                 //      break;
1110                 //}
1111                 if (nrows() > 1)
1112                         delRow(cur.row());
1113                 if (cur.idx() > cur.lastidx())
1114                         cur.idx() = cur.lastidx();
1115                 if (cur.pos() > cur.lastpos())
1116                         cur.pos() = cur.lastpos();
1117                 break;
1118
1119         case LFUN_CELL_SPLIT:
1120                 cur.recordUndo();
1121                 splitCell(cur);
1122                 break;
1123
1124         case LFUN_CELL_BACKWARD:
1125                 // See below.
1126                 cur.setSelection(false);
1127                 if (!idxPrev(cur)) {
1128                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1129                         cur.undispatched();
1130                 }
1131                 break;
1132
1133         case LFUN_CELL_FORWARD:
1134                 // Can't handle selection by additional 'shift' as this is
1135                 // hard bound to LFUN_CELL_BACKWARD
1136                 cur.setSelection(false);
1137                 if (!idxNext(cur)) {
1138                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1139                         cur.undispatched();
1140                 }
1141                 break;
1142
1143         case LFUN_NEWLINE_INSERT: {
1144                 cur.recordUndoInset();
1145                 row_type const r = cur.row();
1146                 addRow(r);
1147
1148                 // split line
1149                 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)
1150                         swap(cell(index(r, c)), cell(index(r + 1, c)));
1151
1152                 // split cell
1153                 splitCell(cur);
1154                 swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
1155                 if (cur.idx() > 0)
1156                         --cur.idx();
1157                 cur.pos() = cur.lastpos();
1158
1159                 //mathcursor->normalize();
1160                 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1161                 break;
1162         }
1163
1164         case LFUN_TABULAR_FEATURE: {
1165                 cur.recordUndoInset();
1166                 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
1167                 istringstream is(to_utf8(cmd.argument()));
1168                 string s;
1169                 is >> s;
1170                 if (s == "valign-top")
1171                         setVerticalAlignment('t');
1172                 else if (s == "valign-middle")
1173                         setVerticalAlignment('c');
1174                 else if (s == "valign-bottom")
1175                         setVerticalAlignment('b');
1176                 else if (s == "align-left")
1177                         setHorizontalAlignment('l', cur.col());
1178                 else if (s == "align-right")
1179                         setHorizontalAlignment('r', cur.col());
1180                 else if (s == "align-center")
1181                         setHorizontalAlignment('c', cur.col());
1182                 else if (s == "append-row")
1183                         for (int i = 0, n = extractInt(is); i < n; ++i)
1184                                 addRow(cur.row());
1185                 else if (s == "delete-row") {
1186                         cur.clearSelection(); // bug 4323
1187                         for (int i = 0, n = extractInt(is); i < n; ++i) {
1188                                 delRow(cur.row());
1189                                 if (cur.idx() >= nargs())
1190                                         cur.idx() -= ncols();
1191                         }
1192                         cur.pos() = 0; // trick, see below
1193                 }
1194                 else if (s == "copy-row") {
1195                         // Here (as later) we save the cursor col/row
1196                         // in order to restore it after operation.
1197                         row_type const r = cur.row();
1198                         col_type const c = cur.col();
1199                         for (int i = 0, n = extractInt(is); i < n; ++i)
1200                                 copyRow(cur.row());
1201                         cur.idx() = index(r, c);
1202                 }
1203                 else if (s == "swap-row") {
1204                         swapRow(cur.row());
1205                         // Trick to suppress same-idx-means-different-cell
1206                         // assertion crash:
1207                         cur.pos() = 0;
1208                 }
1209                 else if (s == "add-hline-above")
1210                         rowinfo_[cur.row()].lines_++;
1211                 else if (s == "add-hline-below")
1212                         rowinfo_[cur.row()+1].lines_++;
1213                 else if (s == "delete-hline-above")
1214                         rowinfo_[cur.row()].lines_--;
1215                 else if (s == "delete-hline-below")
1216                         rowinfo_[cur.row()+1].lines_--;
1217                 else if (s == "append-column") {
1218                         row_type const r = cur.row();
1219                         col_type const c = cur.col();
1220                         for (int i = 0, n = extractInt(is); i < n; ++i)
1221                                 addCol(cur.col() + 1);
1222                         cur.idx() = index(r, c);
1223                 }
1224                 else if (s == "delete-column") {
1225                         cur.clearSelection(); // bug 4323
1226                         row_type const r = cur.row();
1227                         col_type const c = cur.col();
1228                         for (int i = 0, n = extractInt(is); i < n; ++i)
1229                                 delCol(col(cur.idx()));
1230                         cur.idx() = index(r, min(c, cur.ncols() - 1));
1231                         cur.pos() = 0; // trick, see above
1232                 }
1233                 else if (s == "copy-column") {
1234                         row_type const r = cur.row();
1235                         col_type const c = cur.col();
1236                         copyCol(cur.col());
1237                         cur.idx() = index(r, c);
1238                 }
1239                 else if (s == "swap-column") {
1240                         swapCol(cur.col());
1241                         cur.pos() = 0; // trick, see above
1242                 }
1243                 else if (s == "add-vline-left") {
1244                         colinfo_[cur.col()].lines_++;
1245                         if (!colinfo_[cur.col()].special_.empty())
1246                                 colinfo_[cur.col()].special_ += '|';
1247                 }
1248                 else if (s == "add-vline-right") {
1249                         colinfo_[cur.col()+1].lines_++;
1250                         if (!colinfo_[cur.col()+1].special_.empty())
1251                                 colinfo_[cur.col()+1].special_.insert(0, 1, '|');
1252                 }
1253                 else if (s == "delete-vline-left") {
1254                         colinfo_[cur.col()].lines_--;
1255                         docstring & special = colinfo_[cur.col()].special_;
1256                         if (!special.empty()) {
1257                                 docstring::size_type i = special.rfind('|');
1258                                 LASSERT(i != docstring::npos, /**/);
1259                                 special.erase(i, 1);
1260                         }
1261                 }
1262                 else if (s == "delete-vline-right") {
1263                         colinfo_[cur.col()+1].lines_--;
1264                         docstring & special = colinfo_[cur.col()+1].special_;
1265                         if (!special.empty()) {
1266                                 docstring::size_type i = special.find('|');
1267                                 LASSERT(i != docstring::npos, /**/);
1268                                 special.erase(i, 1);
1269                         }
1270                 }
1271                 else {
1272                         cur.undispatched();
1273                         break;
1274                 }
1275                 // perhaps this should be FINISHED_BACKWARD -- just for clarity?
1276                 //lyxerr << "returning FINISHED_LEFT" << endl;
1277                 break;
1278         }
1279
1280         case LFUN_CLIPBOARD_PASTE:
1281                 parseflg |= Parse::VERBATIM;
1282                 // fall through
1283         case LFUN_PASTE: {
1284                 if (cur.currentMode() <= TEXT_MODE)
1285                         parseflg |= Parse::TEXTMODE;
1286                 cur.message(_("Paste"));
1287                 cap::replaceSelection(cur);
1288                 docstring topaste;
1289                 if (cmd.argument().empty() && !theClipboard().isInternal())
1290                         topaste = theClipboard().getAsText();
1291                 else {
1292                         idocstringstream is(cmd.argument());
1293                         int n = 0;
1294                         is >> n;
1295                         topaste = cap::selection(n);
1296                 }
1297                 InsetMathGrid grid(buffer_, 1, 1);
1298                 if (!topaste.empty())
1299                         if ((topaste.size() == 1 && topaste.at(0) < 0x80)
1300                             || !mathed_parse_normal(grid, topaste, parseflg)) {
1301                                 resetGrid(grid);
1302                                 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
1303                         }
1304
1305                 if (grid.nargs() == 1) {
1306                         // single cell/part of cell
1307                         cur.recordUndo();
1308                         cur.cell().insert(cur.pos(), grid.cell(0));
1309                         cur.pos() += grid.cell(0).size();
1310                 } else {
1311                         // multiple cells
1312                         cur.recordUndoInset();
1313                         col_type const numcols =
1314                                 min(grid.ncols(), ncols() - col(cur.idx()));
1315                         row_type const numrows =
1316                                 min(grid.nrows(), nrows() - cur.row());
1317                         for (row_type r = 0; r < numrows; ++r) {
1318                                 for (col_type c = 0; c < numcols; ++c) {
1319                                         idx_type i = index(r + cur.row(), c + col(cur.idx()));
1320                                         cell(i).insert(0, grid.cell(grid.index(r, c)));
1321                                 }
1322                                 // append the left over horizontal cells to the last column
1323                                 idx_type i = index(r + cur.row(), ncols() - 1);
1324                                 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
1325                                         cell(i).append(grid.cell(grid.index(r, c)));
1326                         }
1327                         // append the left over vertical cells to the last _cell_
1328                         idx_type i = nargs() - 1;
1329                         for (row_type r = numrows; r < grid.nrows(); ++r)
1330                                 for (col_type c = 0; c < grid.ncols(); ++c)
1331                                         cell(i).append(grid.cell(grid.index(r, c)));
1332                 }
1333                 cur.clearSelection(); // bug 393
1334                 // FIXME audit setBuffer/updateLabels calls
1335                 cur.inset().setBuffer(*buffer_);
1336                 // FIXME audit setBuffer/updateLabels calls
1337                 cur.buffer()->updateLabels();
1338                 cur.finishUndo();
1339                 break;
1340         }
1341
1342         case LFUN_LINE_BEGIN_SELECT:
1343         case LFUN_LINE_BEGIN:
1344         case LFUN_WORD_BACKWARD_SELECT:
1345         case LFUN_WORD_BACKWARD:
1346         case LFUN_WORD_LEFT_SELECT:
1347         case LFUN_WORD_LEFT:
1348                 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
1349                                 cmd.action == LFUN_WORD_LEFT_SELECT ||
1350                                 cmd.action == LFUN_LINE_BEGIN_SELECT);
1351                 cur.macroModeClose();
1352                 if (cur.pos() != 0) {
1353                         cur.pos() = 0;
1354                 } else if (cur.idx() % cur.ncols() != 0) {
1355                         cur.idx() -= cur.idx() % cur.ncols();
1356                         cur.pos() = 0;
1357                 } else if (cur.idx() != 0) {
1358                         cur.idx() = 0;
1359                         cur.pos() = 0;
1360                 } else {
1361                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1362                         cur.undispatched();
1363                 }
1364                 break;
1365
1366         case LFUN_WORD_FORWARD_SELECT:
1367         case LFUN_WORD_FORWARD:
1368         case LFUN_WORD_RIGHT_SELECT:
1369         case LFUN_WORD_RIGHT:
1370         case LFUN_LINE_END_SELECT:
1371         case LFUN_LINE_END:
1372                 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
1373                                 cmd.action == LFUN_WORD_RIGHT_SELECT ||
1374                                 cmd.action == LFUN_LINE_END_SELECT);
1375                 cur.macroModeClose();
1376                 cur.clearTargetX();
1377                 if (cur.pos() != cur.lastpos()) {
1378                         cur.pos() = cur.lastpos();
1379                 } else if ((cur.idx() + 1) % cur.ncols() != 0) {
1380                         cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
1381                         cur.pos() = cur.lastpos();
1382                 } else if (cur.idx() != cur.lastidx()) {
1383                         cur.idx() = cur.lastidx();
1384                         cur.pos() = cur.lastpos();
1385                 } else {
1386                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1387                         cur.undispatched();
1388                 }
1389                 break;
1390
1391         default:
1392                 InsetMathNest::doDispatch(cur, cmd);
1393         }
1394 }
1395
1396
1397 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
1398                 FuncStatus & status) const
1399 {
1400         switch (cmd.action) {
1401         case LFUN_TABULAR_FEATURE: {
1402                 string const s = cmd.getArg(0);
1403                 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
1404                         status.setEnabled(false);
1405                         status.message(from_utf8(N_("Only one row")));
1406                         return true;
1407                 }
1408                 if (ncols() <= 1 &&
1409                     (s == "delete-column" || s == "swap-column")) {
1410                         status.setEnabled(false);
1411                         status.message(from_utf8(N_("Only one column")));
1412                         return true;
1413                 }
1414                 if ((rowinfo_[cur.row()].lines_ == 0 &&
1415                      s == "delete-hline-above") ||
1416                     (rowinfo_[cur.row() + 1].lines_ == 0 &&
1417                      s == "delete-hline-below")) {
1418                         status.setEnabled(false);
1419                         status.message(from_utf8(N_("No hline to delete")));
1420                         return true;
1421                 }
1422
1423                 if ((colinfo_[cur.col()].lines_ == 0 &&
1424                      s == "delete-vline-left") ||
1425                     (colinfo_[cur.col() + 1].lines_ == 0 &&
1426                      s == "delete-vline-right")) {
1427                         status.setEnabled(false);
1428                         status.message(from_utf8(N_("No vline to delete")));
1429                         return true;
1430                 }
1431                 if (s == "valign-top" || s == "valign-middle" ||
1432                     s == "valign-bottom" || s == "align-left" ||
1433                     s == "align-right" || s == "align-center") {
1434                         status.setEnabled(true);
1435                         char const ha = horizontalAlignment(cur.col());
1436                         char const va = verticalAlignment();
1437                         status.setOnOff((s == "align-left" && ha == 'l')
1438                                         || (s == "align-right"   && ha == 'r')
1439                                         || (s == "align-center"  && ha == 'c')
1440                                         || (s == "valign-top"    && va == 't')
1441                                         || (s == "valign-bottom" && va == 'b')
1442                                         || (s == "valign-middle" && va == 'c'));
1443                         return true;
1444                 }
1445                 if (s == "append-row" || s == "delete-row" ||
1446                     s == "copy-row" || s == "swap-row" ||
1447                     s == "add-hline-above" || s == "add-hline-below" ||
1448                     s == "delete-hline-above" || s == "delete-hline-below" ||
1449                     s == "append-column" || s == "delete-column" ||
1450                     s == "copy-column" || s == "swap-column" ||
1451                     s == "add-vline-left" || s == "add-vline-right" ||
1452                     s == "delete-vline-left" || s == "delete-vline-right") {
1453                         status.setEnabled(true);
1454                 } else {
1455                         status.setEnabled(false);
1456                         status.message(bformat(
1457                                 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
1458                 }
1459
1460 #if 0
1461                 // FIXME: What did this code do?
1462                 // Please check whether it is still needed!
1463                 // should be more precise
1464                 if (v_align_ == '\0') {
1465                         status.enable(true);
1466                         break;
1467                 }
1468                 if (cmd.argument().empty()) {
1469                         status.enable(false);
1470                         break;
1471                 }
1472                 if (!contains("tcb", cmd.argument()[0])) {
1473                         status.enable(false);
1474                         break;
1475                 }
1476                 status.setOnOff(cmd.argument()[0] == v_align_);
1477                 status.setEnabled(true);
1478 #endif
1479                 return true;
1480         }
1481
1482         case LFUN_CELL_SPLIT:
1483                 status.setEnabled(true);
1484                 return true;
1485
1486         case LFUN_CELL_BACKWARD:
1487         case LFUN_CELL_FORWARD:
1488                 status.setEnabled(true);
1489                 return true;
1490
1491         default:
1492                 return InsetMathNest::getStatus(cur, cmd, status);
1493         }
1494 }
1495
1496
1497 } // namespace lyx