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