]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathGrid.cpp
0ea62cfc89eaa18eec49835cf1a078b1e8fe7304
[lyx.git] / src / mathed / InsetMathGrid.cpp
1 /**\r
2  * \file InsetMathGrid.cpp\r
3  * This file is part of LyX, the document processor.\r
4  * Licence details can be found in the file COPYING.\r
5  *\r
6  * \author André Pönitz\r
7  *\r
8  * Full author contact details are available in file CREDITS.\r
9  */\r
10 \r
11 #include <config.h>\r
12 #include <algorithm>\r
13 \r
14 #include "InsetMathGrid.h"\r
15 \r
16 #include "InsetMathUnknown.h"\r
17 #include "MathData.h"\r
18 #include "MathParser.h"\r
19 #include "MathStream.h"\r
20 #include "MetricsInfo.h"\r
21 \r
22 #include "Buffer.h"\r
23 #include "BufferParams.h"\r
24 #include "BufferView.h"\r
25 #include "CutAndPaste.h"\r
26 #include "FuncStatus.h"\r
27 #include "Cursor.h"\r
28 #include "FuncRequest.h"\r
29 \r
30 #include "frontends/Clipboard.h"\r
31 #include "frontends/Painter.h"\r
32 \r
33 #include "support/debug.h"\r
34 #include "support/docstream.h"\r
35 #include "support/gettext.h"\r
36 #include "support/lstrings.h"\r
37 \r
38 #include "support/lassert.h"\r
39 \r
40 #include <sstream>\r
41 \r
42 using namespace std;\r
43 using namespace lyx::support;\r
44 \r
45 \r
46 \r
47 namespace lyx {\r
48 \r
49 static docstring verboseHLine(int n)\r
50 {\r
51         docstring res;\r
52         for (int i = 0; i < n; ++i)\r
53                 res += "\\hline";\r
54         if (n)\r
55                 res += ' ';\r
56         return res;\r
57 }\r
58 \r
59 \r
60 static int extractInt(istream & is)\r
61 {\r
62         int num = 1;\r
63         is >> num;\r
64         return (num == 0) ? 1 : num;\r
65 }\r
66 \r
67 \r
68 static void resetGrid(InsetMathGrid & grid)\r
69 {\r
70         while (grid.ncols() > 1)\r
71                 grid.delCol(grid.ncols() - 1);\r
72         while (grid.nrows() > 1)\r
73                 grid.delRow(grid.nrows() - 1);\r
74         grid.cell(0).erase(0, grid.cell(0).size());\r
75         grid.setDefaults();\r
76 }\r
77 \r
78 \r
79 \r
80 //////////////////////////////////////////////////////////////\r
81 \r
82 \r
83 InsetMathGrid::CellInfo::CellInfo()\r
84         : multi_(CELL_NORMAL), glue_(0), begin_(0), end_(0)\r
85 {}\r
86 \r
87 \r
88 \r
89 //////////////////////////////////////////////////////////////\r
90 \r
91 \r
92 InsetMathGrid::RowInfo::RowInfo()\r
93         : descent_(0), ascent_(0), offset_(0), lines_(0), skip_(0),\r
94           allow_newpage_(true)\r
95 {}\r
96 \r
97 \r
98 \r
99 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const\r
100 {\r
101         return crskip_.inPixels(mi.base);\r
102 }\r
103 \r
104 \r
105 \r
106 //////////////////////////////////////////////////////////////\r
107 \r
108 \r
109 InsetMathGrid::ColInfo::ColInfo()\r
110         : align_('c'), width_(0), offset_(0), lines_(0), skip_(0)\r
111 {}\r
112 \r
113 \r
114 //////////////////////////////////////////////////////////////\r
115 \r
116 \r
117 InsetMathGrid::InsetMathGrid(Buffer * buf)\r
118         : InsetMathNest(buf, 1),\r
119           rowinfo_(1 + 1),\r
120                 colinfo_(1 + 1),\r
121                 cellinfo_(1),\r
122                 v_align_('c')\r
123 {\r
124         setDefaults();\r
125 }\r
126 \r
127 \r
128 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)\r
129         : InsetMathNest(buf, m * n),\r
130           rowinfo_(n + 1),\r
131                 colinfo_(m + 1),\r
132                 cellinfo_(m * n),\r
133                 v_align_('c')\r
134 {\r
135         setDefaults();\r
136 }\r
137 \r
138 \r
139 InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,\r
140         docstring const & h)\r
141         : InsetMathNest(buf, m * n),\r
142           rowinfo_(n + 1),\r
143           colinfo_(m + 1),\r
144                 cellinfo_(m * n),\r
145                 v_align_(v)\r
146 {\r
147         setDefaults();\r
148         setVerticalAlignment(v);\r
149         setHorizontalAlignments(h);\r
150 }\r
151 \r
152 \r
153 Inset * InsetMathGrid::clone() const\r
154 {\r
155         return new InsetMathGrid(*this);\r
156 }\r
157 \r
158 \r
159 InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const\r
160 {\r
161         return col + ncols() * row;\r
162 }\r
163 \r
164 \r
165 void InsetMathGrid::setDefaults()\r
166 {\r
167         if (ncols() <= 0)\r
168                 lyxerr << "positive number of columns expected" << endl;\r
169         //if (nrows() <= 0)\r
170         //      lyxerr << "positive number of rows expected" << endl;\r
171         for (col_type col = 0; col < ncols(); ++col) {\r
172                 colinfo_[col].align_ = defaultColAlign(col);\r
173                 colinfo_[col].skip_  = defaultColSpace(col);\r
174                 colinfo_[col].special_.clear();\r
175         }\r
176         for (idx_type idx = 0; idx < nargs(); ++idx)\r
177                 cellinfo_[idx].multi_ = CELL_NORMAL;\r
178 }\r
179 \r
180 \r
181 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)\r
182 {\r
183         if (str == "\\hline") {\r
184                 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");\r
185                 FuncStatus status;\r
186                 if (getStatus(cur, fr, status)) {\r
187                         if (status.enabled()) {\r
188                                 rowinfo_[cur.row()].lines_++;\r
189                                 return true;\r
190                         }\r
191                 }\r
192         }\r
193         return InsetMathNest::interpretString(cur, str);\r
194 }\r
195 \r
196 \r
197 void InsetMathGrid::setHorizontalAlignments(docstring const & hh)\r
198 {\r
199         col_type col = 0;\r
200         for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {\r
201                 char_type c = *it;\r
202                 if (c == '|') {\r
203                         colinfo_[col].lines_++;\r
204                 } else if ((c == 'p' || c == 'm' || c == 'b'||\r
205                             c == '!' || c == '@' || c == '>' || c == '<') &&\r
206                            it + 1 != hh.end() && *(it + 1) == '{') {\r
207                         // @{decl.} and p{width} are standard LaTeX, the\r
208                         // others are extensions by array.sty\r
209                         bool const newcolumn = c == 'p' || c == 'm' || c == 'b';\r
210                         if (newcolumn) {\r
211                                 // this declares a new column\r
212                                 if (col >= ncols())\r
213                                         // Only intercolumn stuff is allowed\r
214                                         // in the last dummy column\r
215                                         break;\r
216                                 colinfo_[col].align_ = 'l';\r
217                         } else {\r
218                                 // this is intercolumn stuff\r
219                                 if (colinfo_[col].special_.empty())\r
220                                         // Overtake possible lines\r
221                                         colinfo_[col].special_ = docstring(colinfo_[col].lines_, '|');\r
222                         }\r
223                         int brace_open = 0;\r
224                         int brace_close = 0;\r
225                         while (it != hh.end()) {\r
226                                 c = *it;\r
227                                 colinfo_[col].special_ += c;\r
228                                 if (c == '{')\r
229                                         ++brace_open;\r
230                                 else if (c == '}')\r
231                                         ++brace_close;\r
232                                 ++it;\r
233                                 if (brace_open > 0 && brace_open == brace_close)\r
234                                         break;\r
235                         }\r
236                         --it;\r
237                         if (newcolumn) {\r
238                                 colinfo_[col].lines_ = count(\r
239                                         colinfo_[col].special_.begin(),\r
240                                         colinfo_[col].special_.end(), '|');\r
241                                 LYXERR(Debug::MATHED, "special column separator: `"\r
242                                         << to_utf8(colinfo_[col].special_) << '\'');\r
243                                 ++col;\r
244                                 colinfo_[col].lines_ = 0;\r
245                                 colinfo_[col].special_.clear();\r
246                         }\r
247                 } else if (col >= ncols()) {\r
248                         // Only intercolumn stuff is allowed in the last\r
249                         // dummy column\r
250                         break;\r
251                 } else if (c == 'c' || c == 'l' || c == 'r') {\r
252                         colinfo_[col].align_ = static_cast<char>(c);\r
253                         if (!colinfo_[col].special_.empty()) {\r
254                                 colinfo_[col].special_ += c;\r
255                                 colinfo_[col].lines_ = count(\r
256                                                 colinfo_[col].special_.begin(),\r
257                                                 colinfo_[col].special_.end(), '|');\r
258                                 LYXERR(Debug::MATHED, "special column separator: `"\r
259                                         << to_utf8(colinfo_[col].special_) << '\'');\r
260                         }\r
261                         ++col;\r
262                         colinfo_[col].lines_ = 0;\r
263                         colinfo_[col].special_.clear();\r
264                 } else {\r
265                         lyxerr << "unknown column separator: '" << c << "'" << endl;\r
266                 }\r
267         }\r
268 \r
269 /*\r
270         col_type n = hh.size();\r
271         if (n > ncols())\r
272                 n = ncols();\r
273         for (col_type col = 0; col < n; ++col)\r
274                 colinfo_[col].align_ = hh[col];\r
275 */\r
276 }\r
277 \r
278 \r
279 InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)\r
280 {\r
281         col_type col = 0;\r
282         for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)\r
283                 if (*it == 'c' || *it == 'l' || *it == 'r'||\r
284                     *it == 'p' || *it == 'm' || *it == 'b')\r
285                         ++col;\r
286         // let's have at least one column, even if we did not recognize its\r
287         // alignment\r
288         if (col == 0)\r
289                 col = 1;\r
290         return col;\r
291 }\r
292 \r
293 \r
294 void InsetMathGrid::setHorizontalAlignment(char h, col_type col)\r
295 {\r
296         colinfo_[col].align_ = h;\r
297         if (!colinfo_[col].special_.empty()) {\r
298                 char_type & c = colinfo_[col].special_[colinfo_[col].special_.size() - 1];\r
299                 if (c == 'l' || c == 'c' || c == 'r')\r
300                         c = h;\r
301         }\r
302         // FIXME: Change alignment of p, m and b columns, too\r
303 }\r
304 \r
305 \r
306 char InsetMathGrid::horizontalAlignment(col_type col) const\r
307 {\r
308         return colinfo_[col].align_;\r
309 }\r
310 \r
311 \r
312 docstring InsetMathGrid::horizontalAlignments() const\r
313 {\r
314         docstring res;\r
315         for (col_type col = 0; col < ncols(); ++col) {\r
316                 if (colinfo_[col].special_.empty()) {\r
317                         res += docstring(colinfo_[col].lines_, '|');\r
318                         res += colinfo_[col].align_;\r
319                 } else\r
320                         res += colinfo_[col].special_;\r
321         }\r
322         if (colinfo_[ncols()].special_.empty())\r
323                 return res + docstring(colinfo_[ncols()].lines_, '|');\r
324         return res + colinfo_[ncols()].special_;\r
325 }\r
326 \r
327 \r
328 void InsetMathGrid::setVerticalAlignment(char c)\r
329 {\r
330         v_align_ = c;\r
331 }\r
332 \r
333 \r
334 char InsetMathGrid::verticalAlignment() const\r
335 {\r
336         return v_align_;\r
337 }\r
338 \r
339 \r
340 InsetMathGrid::col_type InsetMathGrid::ncols() const\r
341 {\r
342         return colinfo_.size() - 1;\r
343 }\r
344 \r
345 \r
346 InsetMathGrid::row_type InsetMathGrid::nrows() const\r
347 {\r
348         return rowinfo_.size() - 1;\r
349 }\r
350 \r
351 \r
352 InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const\r
353 {\r
354         return idx % ncols();\r
355 }\r
356 \r
357 \r
358 InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const\r
359 {\r
360         return idx / ncols();\r
361 }\r
362 \r
363 \r
364 InsetMathGrid::col_type InsetMathGrid::ncellcols(idx_type idx) const\r
365 {\r
366         col_type cols = 1;\r
367         if (cellinfo_[idx].multi_ == CELL_NORMAL)\r
368                 return cols;\r
369         // If the cell at idx is already CELL_PART_OF_MULTICOLUMN we return\r
370         // the number of remaining columns, not the ones of the complete\r
371         // multicolumn cell. This makes it possible to always go to the next\r
372         // cell with idx + ncellcols(idx) - 1.\r
373         row_type const r = row(idx);\r
374         while (idx+cols < nargs() && row(idx+cols) == r &&\r
375                cellinfo_[idx+cols].multi_ == CELL_PART_OF_MULTICOLUMN)\r
376                 cols++;\r
377         return cols;\r
378 }\r
379 \r
380 \r
381 void InsetMathGrid::vcrskip(Length const & crskip, row_type row)\r
382 {\r
383         rowinfo_[row].crskip_ = crskip;\r
384 }\r
385 \r
386 \r
387 Length InsetMathGrid::vcrskip(row_type row) const\r
388 {\r
389         return rowinfo_[row].crskip_;\r
390 }\r
391 \r
392 \r
393 void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const\r
394 {\r
395         // let the cells adjust themselves\r
396         for (idx_type i = 0; i < nargs(); ++i) {\r
397                 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
398                         Dimension dimc;\r
399                         cell(i).metrics(mi, dimc);\r
400                 }\r
401         }\r
402 \r
403         BufferView & bv = *mi.base.bv;\r
404 \r
405         // compute absolute sizes of vertical structure\r
406         for (row_type row = 0; row < nrows(); ++row) {\r
407                 int asc  = 0;\r
408                 int desc = 0;\r
409                 for (col_type col = 0; col < ncols(); ++col) {\r
410                         idx_type const i = index(row, col);\r
411                         if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
412                                 Dimension const & dimc = cell(i).dimension(bv);\r
413                                 asc  = max(asc,  dimc.asc);\r
414                                 desc = max(desc, dimc.des);\r
415                         }\r
416                 }\r
417                 rowinfo_[row].ascent_  = asc;\r
418                 rowinfo_[row].descent_ = desc;\r
419         }\r
420         rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;\r
421         rowinfo_[nrows()].ascent_  = 0;\r
422         rowinfo_[nrows()].descent_ = 0;\r
423 \r
424         // compute vertical offsets\r
425         rowinfo_[0].offset_ = 0;\r
426         for (row_type row = 1; row <= nrows(); ++row) {\r
427                 rowinfo_[row].offset_ =\r
428                         rowinfo_[row - 1].offset_ +\r
429                         rowinfo_[row - 1].descent_ +\r
430                         rowinfo_[row - 1].skipPixels(mi) +\r
431                         rowsep() +\r
432                         rowinfo_[row].lines_ * hlinesep() +\r
433                         rowinfo_[row].ascent_;\r
434         }\r
435 \r
436         // adjust vertical offset\r
437         int h = 0;\r
438         switch (v_align_) {\r
439                 case 't':\r
440                         h = 0;\r
441                         break;\r
442                 case 'b':\r
443                         h = rowinfo_[nrows() - 1].offset_;\r
444                         break;\r
445                 default:\r
446                         h = rowinfo_[nrows() - 1].offset_ / 2;\r
447         }\r
448         for (row_type row = 0; row <= nrows(); ++row)\r
449                 rowinfo_[row].offset_ -= h;\r
450 \r
451 \r
452         // multicolumn cell widths, as a map from first column to width in a\r
453         // vector of last columns.\r
454         // This is only used if the grid has more than one row, since for\r
455         // one-row grids multicolumn cells do not need special handling\r
456         vector<map<col_type, int> > mcolwidths(ncols());\r
457 \r
458         // compute absolute sizes of horizontal structure\r
459         for (col_type col = 0; col < ncols(); ++col) {\r
460                 int wid = 0;\r
461                 for (row_type row = 0; row < nrows(); ++row) {\r
462                         idx_type const i = index(row, col);\r
463                         if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
464                                 int const w = cell(i).dimension(bv).wid;\r
465                                 col_type const cols = ncellcols(i);\r
466                                 if (cols > 1 && nrows() > 1) {\r
467                                         col_type last = col+cols-1;\r
468                                         LASSERT(last < ncols(), last = ncols()-1);\r
469                                         map<col_type, int>::iterator it =\r
470                                                 mcolwidths[last].find(col);\r
471                                         if (it == mcolwidths[last].end())\r
472                                                 mcolwidths[last][col] = w;\r
473                                         else\r
474                                                 it->second = max(it->second, w);\r
475                                 } else\r
476                                         wid = max(wid, w);\r
477                         }\r
478                 }\r
479                 colinfo_[col].width_ = wid;\r
480         }\r
481         colinfo_[ncols()].width_  = 0;\r
482 \r
483         // compute horizontal offsets\r
484         colinfo_[0].offset_ = border();\r
485         for (col_type col = 1; col <= ncols(); ++col) {\r
486                 colinfo_[col].offset_ =\r
487                         colinfo_[col - 1].offset_ +\r
488                         colinfo_[col - 1].width_ +
489                         displayColSpace(col - 1) +
490                         colsep() +
491                         colinfo_[col].lines_ * vlinesep();\r
492         }\r
493 \r
494         // increase column widths for multicolumn cells if needed\r
495         // FIXME: multicolumn lines are not yet considered\r
496         for (col_type last = 0; last < ncols(); ++last) {\r
497                 map<col_type, int> const & widths = mcolwidths[last];\r
498                 // We increase the width of the last column of the multicol\r
499                 // cell (some sort of left alignment). Since we iterate through\r
500                 // the last and the first columns from left to right, we ensure\r
501                 // that increased widths of previous columns are correctly\r
502                 // taken into account for later columns, thus preventing\r
503                 // unneeded width increasing.\r
504                 for (map<col_type, int>::const_iterator it = widths.begin();\r
505                      it != widths.end(); ++it) {\r
506                         int const wid = it->second;\r
507                         col_type const first = it->first;\r
508                         int const nextoffset =\r
509                                 colinfo_[first].offset_ +\r
510                                 wid +\r
511                                 colinfo_[last].skip_ +
512                                 colsep() +\r
513                                 colinfo_[last+1].lines_ * vlinesep();\r
514                         int const dx = nextoffset - colinfo_[last+1].offset_;\r
515                         if (dx > 0) {\r
516                                 colinfo_[last].width_ += dx;\r
517                                 for (col_type col = last + 1; col <= ncols(); ++col)\r
518                                         colinfo_[col].offset_ += dx;\r
519                         }\r
520                 }\r
521         }\r
522 \r
523 \r
524         dim.wid = colinfo_[ncols() - 1].offset_\r
525                 + colinfo_[ncols() - 1].width_\r
526                 + vlinesep() * colinfo_[ncols()].lines_\r
527                 + border();\r
528 \r
529         dim.asc = - rowinfo_[0].offset_\r
530                 + rowinfo_[0].ascent_\r
531                 + hlinesep() * rowinfo_[0].lines_\r
532                 + border();\r
533 \r
534         dim.des = rowinfo_[nrows() - 1].offset_\r
535                 + rowinfo_[nrows() - 1].descent_\r
536                 + hlinesep() * rowinfo_[nrows()].lines_\r
537                 + border();\r
538 \r
539 \r
540 /*\r
541         // Increase ws_[i] for 'R' columns (except the first one)\r
542         for (int i = 1; i < nc_; ++i)\r
543                 if (align_[i] == 'R')\r
544                         ws_[i] += 10 * df_width;\r
545         // Increase ws_[i] for 'C' column\r
546         if (align_[0] == 'C')\r
547                 if (ws_[0] < 7 * workwidth / 8)\r
548                         ws_[0] = 7 * workwidth / 8;\r
549 \r
550         // Adjust local tabs\r
551         width = colsep();\r
552         for (cxrow = row_.begin(); cxrow; ++cxrow) {\r
553                 int rg = COLSEP;\r
554                 int lf = 0;\r
555                 for (int i = 0; i < nc_; ++i) {\r
556                         bool isvoid = false;\r
557                         if (cxrow->getTab(i) <= 0) {\r
558                                 cxrow->setTab(i, df_width);\r
559                                 isvoid = true;\r
560                         }\r
561                         switch (align_[i]) {\r
562                         case 'l':\r
563                                 lf = 0;\r
564                                 break;\r
565                         case 'c':\r
566                                 lf = (ws_[i] - cxrow->getTab(i))/2;\r
567                                 break;\r
568                         case 'r':\r
569                         case 'R':\r
570                                 lf = ws_[i] - cxrow->getTab(i);\r
571                                 break;\r
572                         case 'C':\r
573                                 if (cxrow == row_.begin())\r
574                                         lf = 0;\r
575                                 else if (cxrow.is_last())\r
576                                         lf = ws_[i] - cxrow->getTab(i);\r
577                                 else\r
578                                         lf = (ws_[i] - cxrow->getTab(i))/2;\r
579                                 break;\r
580                         }\r
581                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);\r
582                         cxrow->setTab(i, lf + rg);\r
583                         rg = ws_[i] - ww + colsep();\r
584                         if (cxrow == row_.begin())\r
585                                 width += ws_[i] + colsep();\r
586                 }\r
587                 cxrow->setBaseline(cxrow->getBaseline() - ascent);\r
588         }\r
589 */\r
590         metricsMarkers2(dim);\r
591         // Cache the inset dimension.\r
592         setDimCache(mi, dim);\r
593 }\r
594 \r
595 \r
596 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const\r
597 {\r
598         drawWithMargin(pi, x, y, 1, 1);\r
599 }\r
600 \r
601 \r
602 void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,\r
603         int lmargin, int rmargin) const\r
604 {\r
605         Dimension const dim = dimension(*pi.base.bv);\r
606         BufferView const & bv = *pi.base.bv;\r
607 \r
608         for (idx_type idx = 0; idx < nargs(); ++idx) {\r
609                 if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
610                         cell(idx).draw(pi,\r
611                                 x + lmargin + cellXOffset(bv, idx),\r
612                                 y + cellYOffset(idx));\r
613 \r
614                         // draw inner lines cell by cell because of possible multicolumns\r
615                         // FIXME: multicolumn lines are not yet considered\r
616                         row_type const r = row(idx);\r
617                         col_type const c = col(idx);\r
618                         if (r > 0 && r < nrows()) {\r
619                                 for (unsigned int i = 0; i < rowinfo_[r].lines_; ++i) {\r
620                                         int yy = y + rowinfo_[r].offset_\r
621                                                 - rowinfo_[r].ascent_\r
622                                                 - i * hlinesep()\r
623                                                 - hlinesep()/2 - rowsep()/2;\r
624                                         pi.pain.line(\r
625                                                 x + lmargin + colinfo_[c].offset_,\r
626                                                 yy,\r
627                                                 x + lmargin + colinfo_[c+1].offset_,\r
628                                                 yy, Color_foreground);\r
629                                 }\r
630                         }\r
631                         if (c > 0 && c < ncols()) {\r
632                                 for (unsigned int i = 0; i < colinfo_[c].lines_; ++i) {\r
633                                         int xx = x + lmargin\r
634                                                 + colinfo_[c].offset_\r
635                                                 - i * vlinesep()\r
636                                                 - vlinesep()/2 - colsep()/2;\r
637                                         pi.pain.line(xx,\r
638                                                 rowinfo_[r].offset_ - rowinfo_[r].ascent_,\r
639                                                 xx,\r
640                                                 rowinfo_[r].offset_ + rowinfo_[r].descent_,\r
641                                                 Color_foreground);\r
642                                 }\r
643                         }\r
644                 }\r
645         }\r
646 \r
647         // draw outer lines in one go\r
648         for (row_type row = 0; row <= nrows(); row += nrows())\r
649                 for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {\r
650                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_\r
651                                 - i * hlinesep() - hlinesep()/2 - rowsep()/2;\r
652                         pi.pain.line(x + lmargin + 1, yy,\r
653                                      x + dim.width() - rmargin - 1, yy,\r
654                                      Color_foreground);\r
655                 }\r
656 \r
657         for (col_type col = 0; col <= ncols(); col += ncols())\r
658                 for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {\r
659                         int xx = x + lmargin + colinfo_[col].offset_\r
660                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;\r
661                         pi.pain.line(xx, y - dim.ascent() + 1,\r
662                                      xx, y + dim.descent() - 1,\r
663                                      Color_foreground);\r
664                 }\r
665         drawMarkers2(pi, x, y);\r
666 }\r
667 \r
668 \r
669 void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const\r
670 {\r
671         // let the cells adjust themselves\r
672         for (idx_type i = 0; i < nargs(); ++i)\r
673                 if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN)\r
674                         cell(i).metricsT(mi, dim);\r
675 \r
676         // compute absolute sizes of vertical structure\r
677         for (row_type row = 0; row < nrows(); ++row) {\r
678                 int asc  = 0;\r
679                 int desc = 0;\r
680                 for (col_type col = 0; col < ncols(); ++col) {\r
681                         idx_type const i = index(row, col);\r
682                         if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
683                                 //MathData const & c = cell(i);\r
684                                 // FIXME: BROKEN!\r
685                                 Dimension dimc;\r
686                                 asc  = max(asc,  dimc.ascent());\r
687                                 desc = max(desc, dimc.descent());\r
688                         }\r
689                 }\r
690                 rowinfo_[row].ascent_  = asc;\r
691                 rowinfo_[row].descent_ = desc;\r
692         }\r
693         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;\r
694         rowinfo_[nrows()].ascent_  = 0;\r
695         rowinfo_[nrows()].descent_ = 0;\r
696 \r
697         // compute vertical offsets\r
698         rowinfo_[0].offset_ = 0;\r
699         for (row_type row = 1; row <= nrows(); ++row) {\r
700                 rowinfo_[row].offset_  =\r
701                         rowinfo_[row - 1].offset_  +\r
702                         rowinfo_[row - 1].descent_ +\r
703                         //rowinfo_[row - 1].skipPixels(mi) +\r
704                         1 + //rowsep() +\r
705                         //rowinfo_[row].lines_ * hlinesep() +\r
706                         rowinfo_[row].ascent_;\r
707         }\r
708 \r
709         // adjust vertical offset\r
710         int h = 0;\r
711         switch (v_align_) {\r
712                 case 't':\r
713                         h = 0;\r
714                         break;\r
715                 case 'b':\r
716                         h = rowinfo_[nrows() - 1].offset_;\r
717                         break;\r
718                 default:\r
719                         h = rowinfo_[nrows() - 1].offset_ / 2;\r
720         }\r
721         for (row_type row = 0; row <= nrows(); ++row)\r
722                 rowinfo_[row].offset_ -= h;\r
723 \r
724 \r
725         // compute absolute sizes of horizontal structure\r
726         for (col_type col = 0; col < ncols(); ++col) {\r
727                 int wid = 0;\r
728                 for (row_type row = 0; row < nrows(); ++row) {\r
729                         // FIXME: BROKEN!\r
730                         //idx_type const i = index(row, col);\r
731                         //if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN)\r
732                         //      wid = max(wid, cell(i).width());\r
733                 }\r
734                 colinfo_[col].width_ = wid;\r
735         }\r
736         colinfo_[ncols()].width_  = 0;\r
737 \r
738         // compute horizontal offsets\r
739         colinfo_[0].offset_ = border();\r
740         for (col_type col = 1; col <= ncols(); ++col) {\r
741                 colinfo_[col].offset_ =\r
742                         colinfo_[col - 1].offset_ +\r
743                         colinfo_[col - 1].width_ +\r
744                         colinfo_[col - 1].skip_ +
745                         1 ; //colsep() +\r
746                         //colinfo_[col].lines_ * vlinesep();\r
747         }\r
748 \r
749 \r
750         dim.wid  =  colinfo_[ncols() - 1].offset_\r
751                        + colinfo_[ncols() - 1].width_\r
752                  //+ vlinesep() * colinfo_[ncols()].lines_\r
753                        + 2;\r
754 \r
755         dim.asc  = -rowinfo_[0].offset_\r
756                        + rowinfo_[0].ascent_\r
757                  //+ hlinesep() * rowinfo_[0].lines_\r
758                        + 1;\r
759 \r
760         dim.des  =  rowinfo_[nrows() - 1].offset_\r
761                        + rowinfo_[nrows() - 1].descent_\r
762                  //+ hlinesep() * rowinfo_[nrows()].lines_\r
763                        + 1;\r
764 }\r
765 \r
766 \r
767 void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const\r
768 {\r
769 //      for (idx_type idx = 0; idx < nargs(); ++idx)\r
770 //              if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN)\r
771 //                      cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));\r
772 }\r
773 \r
774 \r
775 void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)\r
776 {\r
777         // pass down\r
778         for (idx_type idx = 0; idx < nargs(); ++idx)\r
779                 if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN)\r
780                         cell(idx).updateBuffer(it, utype);\r
781 }\r
782 \r
783 \r
784 docstring InsetMathGrid::eolString(row_type row, bool fragile,\r
785                 bool /*latex*/, bool last_eoln) const\r
786 {\r
787         docstring eol;\r
788 \r
789         if (!rowinfo_[row].crskip_.zero())\r
790                 eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';\r
791         else if(!rowinfo_[row].allow_newpage_)\r
792                 eol += '*';\r
793 \r
794         // make sure an upcoming '[' does not break anything\r
795         if (row + 1 < nrows()) {\r
796                 MathData const & c = cell(index(row + 1, 0));\r
797                 if (!c.empty() && c.front()->getChar() == '[')\r
798                         //eol += "[0pt]";\r
799                         eol += "{}";\r
800         }\r
801 \r
802         // only add \\ if necessary\r
803         if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))\r
804                 return docstring();\r
805 \r
806         return (fragile ? "\\protect\\\\" : "\\\\") + eol;\r
807 }\r
808 \r
809 \r
810 docstring InsetMathGrid::eocString(col_type col, col_type lastcol) const\r
811 {\r
812         if (col + 1 == lastcol)\r
813                 return docstring();\r
814         return from_ascii(" & ");\r
815 }\r
816 \r
817 \r
818 void InsetMathGrid::addRow(row_type row)\r
819 {\r
820         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());\r
821         cells_.insert\r
822                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());\r
823         cellinfo_.insert\r
824                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());\r
825 }\r
826 \r
827 \r
828 void InsetMathGrid::delRow(row_type row)\r
829 {\r
830         if (nrows() == 1)\r
831                 return;\r
832 \r
833         cells_type::iterator it = cells_.begin() + row * ncols();\r
834         cells_.erase(it, it + ncols());\r
835 \r
836         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();\r
837         cellinfo_.erase(jt, jt + ncols());\r
838 \r
839         rowinfo_.erase(rowinfo_.begin() + row);\r
840 }\r
841 \r
842 \r
843 void InsetMathGrid::copyRow(row_type row)\r
844 {\r
845         addRow(row);\r
846         for (col_type col = 0; col < ncols(); ++col)\r
847                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];\r
848 }\r
849 \r
850 \r
851 void InsetMathGrid::swapRow(row_type row)\r
852 {\r
853         if (nrows() == 1)\r
854                 return;\r
855         if (row + 1 == nrows())\r
856                 --row;\r
857         for (col_type col = 0; col < ncols(); ++col)\r
858                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);\r
859 }\r
860 \r
861 \r
862 void InsetMathGrid::addCol(col_type newcol)\r
863 {\r
864         const col_type nc = ncols();\r
865         const row_type nr = nrows();\r
866         cells_type new_cells((nc + 1) * nr);\r
867         vector<CellInfo> new_cellinfo((nc + 1) * nr);\r
868 \r
869         for (row_type row = 0; row < nr; ++row)\r
870                 for (col_type col = 0; col < nc; ++col) {\r
871                         new_cells[row * (nc + 1) + col + (col >= newcol)]\r
872                                 = cells_[row * nc + col];\r
873                         new_cellinfo[row * (nc + 1) + col + (col >= newcol)]\r
874                                 = cellinfo_[row * nc + col];\r
875                 }\r
876         swap(cells_, new_cells);\r
877         swap(cellinfo_, new_cellinfo);\r
878 \r
879         ColInfo inf;\r
880         inf.skip_  = defaultColSpace(newcol);\r
881         inf.align_ = defaultColAlign(newcol);\r
882         colinfo_.insert(colinfo_.begin() + newcol, inf);\r
883 }\r
884 \r
885 \r
886 void InsetMathGrid::delCol(col_type col)\r
887 {\r
888         if (ncols() == 1)\r
889                 return;\r
890 \r
891         cells_type tmpcells;\r
892         vector<CellInfo> tmpcellinfo;\r
893         for (col_type i = 0; i < nargs(); ++i)\r
894                 if (i % ncols() != col) {\r
895                         tmpcells.push_back(cells_[i]);\r
896                         tmpcellinfo.push_back(cellinfo_[i]);\r
897                 }\r
898         swap(cells_, tmpcells);\r
899         swap(cellinfo_, tmpcellinfo);\r
900 \r
901         colinfo_.erase(colinfo_.begin() + col);\r
902 }\r
903 \r
904 \r
905 void InsetMathGrid::copyCol(col_type col)\r
906 {\r
907         addCol(col+1);\r
908         for (row_type row = 0; row < nrows(); ++row)\r
909                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];\r
910 }\r
911 \r
912 \r
913 void InsetMathGrid::swapCol(col_type col)\r
914 {\r
915         if (ncols() == 1)\r
916                 return;\r
917         if (col + 1 == ncols())\r
918                 --col;\r
919         for (row_type row = 0; row < nrows(); ++row)\r
920                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);\r
921 }\r
922 \r
923 \r
924 int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const\r
925 {\r
926         if (cellinfo_[idx].multi_ == CELL_PART_OF_MULTICOLUMN)\r
927                 return 0;\r
928         col_type c = col(idx);\r
929         int x = colinfo_[c].offset_;\r
930         char align = displayColAlign(idx);\r
931         Dimension const & celldim = cell(idx).dimension(bv);\r
932         if (align == 'r' || align == 'R')\r
933                 x += cellWidth(idx) - celldim.wid;\r
934         if (align == 'c' || align == 'C')\r
935                 x += (cellWidth(idx) - celldim.wid) / 2;\r
936         return x;\r
937 }\r
938 \r
939 \r
940 int InsetMathGrid::cellYOffset(idx_type idx) const\r
941 {\r
942         return rowinfo_[row(idx)].offset_;\r
943 }\r
944 \r
945 \r
946 int InsetMathGrid::cellWidth(idx_type idx) const\r
947 {\r
948         switch (cellinfo_[idx].multi_) {\r
949         case CELL_NORMAL:\r
950                 return colinfo_[col(idx)].width_;\r
951         case CELL_BEGIN_OF_MULTICOLUMN: {\r
952                 col_type c1 = col(idx);\r
953                 col_type c2 = c1 + ncellcols(idx);\r
954                 return colinfo_[c2].offset_\r
955                         - colinfo_[c1].offset_\r
956                         - colinfo_[c2].skip_
957                         - colsep()\r
958                         - colinfo_[c2].lines_ * vlinesep();\r
959         }\r
960         case CELL_PART_OF_MULTICOLUMN:\r
961                 return 0;\r
962         }\r
963         return 0;\r
964 }\r
965 \r
966 \r
967 bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const\r
968 {\r
969         if (up) {\r
970                 if (cur.row() == 0)\r
971                         return false;\r
972                 cur.idx() -= ncols();\r
973         } else {\r
974                 if (cur.row() + 1 >= nrows())\r
975                         return false;\r
976                 cur.idx() += ncols();\r
977         }\r
978         // If we are in a multicolumn cell, move to the "real" cell\r
979         while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {\r
980                 LASSERT(cur.idx() > 0, return false);\r
981                 --cur.idx();\r
982         }\r
983         cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));\r
984         return true;\r
985 }\r
986 \r
987 \r
988 bool InsetMathGrid::idxBackward(Cursor & cur) const\r
989 {\r
990         // leave matrix if at the front edge\r
991         if (cur.col() == 0)\r
992                 return false;\r
993         --cur.idx();\r
994         // If we are in a multicolumn cell, move to the "real" cell\r
995         while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {\r
996                 LASSERT(cur.idx() > 0, return false);\r
997                 --cur.idx();\r
998         }\r
999         cur.pos() = cur.lastpos();\r
1000         return true;\r
1001 }\r
1002 \r
1003 \r
1004 bool InsetMathGrid::idxForward(Cursor & cur) const\r
1005 {\r
1006         // leave matrix if at the back edge\r
1007         if (cur.col() + 1 == ncols())\r
1008                 return false;\r
1009         ++cur.idx();\r
1010         // If we are in a multicolumn cell, move to the next cell\r
1011         while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {\r
1012                 // leave matrix if at the back edge\r
1013                 if (cur.col() + 1 == ncols())\r
1014                         return false;\r
1015                 ++cur.idx();\r
1016         }\r
1017         cur.pos() = 0;\r
1018         return true;\r
1019 }\r
1020 \r
1021 \r
1022 bool InsetMathGrid::idxFirst(Cursor & cur) const\r
1023 {\r
1024         switch (v_align_) {\r
1025                 case 't':\r
1026                         cur.idx() = 0;\r
1027                         break;\r
1028                 case 'b':\r
1029                         cur.idx() = (nrows() - 1) * ncols();\r
1030                         break;\r
1031                 default:\r
1032                         cur.idx() = ((nrows() - 1) / 2) * ncols();\r
1033         }\r
1034         // If we are in a multicolumn cell, move to the "real" cell\r
1035         while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {\r
1036                 LASSERT(cur.idx() > 0, return false);\r
1037                 --cur.idx();\r
1038         }\r
1039         cur.pos() = 0;\r
1040         return true;\r
1041 }\r
1042 \r
1043 \r
1044 bool InsetMathGrid::idxLast(Cursor & cur) const\r
1045 {\r
1046         switch (v_align_) {\r
1047                 case 't':\r
1048                         cur.idx() = ncols() - 1;\r
1049                         break;\r
1050                 case 'b':\r
1051                         cur.idx() = nargs() - 1;\r
1052                         break;\r
1053                 default:\r
1054                         cur.idx() = ((nrows() - 1) / 2 + 1) * ncols() - 1;\r
1055         }\r
1056         // If we are in a multicolumn cell, move to the "real" cell\r
1057         while (cellinfo_[cur.idx()].multi_ == CELL_PART_OF_MULTICOLUMN) {\r
1058                 LASSERT(cur.idx() > 0, return false);\r
1059                 --cur.idx();\r
1060         }\r
1061         cur.pos() = cur.lastpos();\r
1062         return true;\r
1063 }\r
1064 \r
1065 \r
1066 bool InsetMathGrid::idxDelete(idx_type & idx)\r
1067 {\r
1068         // nothing to do if we have just one row\r
1069         if (nrows() == 1)\r
1070                 return false;\r
1071 \r
1072         // nothing to do if we are in the middle of the last row of the inset\r
1073         if (idx + ncols() > nargs())\r
1074                 return false;\r
1075 \r
1076         // try to delete entire sequence of ncols() empty cells if possible\r
1077         for (idx_type i = idx; i < idx + ncols(); ++i)\r
1078                 if (!cell(i).empty())\r
1079                         return false;\r
1080 \r
1081         // move cells if necessary\r
1082         for (idx_type i = index(row(idx), 0); i < idx; ++i)\r
1083                 swap(cell(i), cell(i + ncols()));\r
1084 \r
1085         delRow(row(idx));\r
1086 \r
1087         if (idx >= nargs())\r
1088                 idx = nargs() - 1;\r
1089 \r
1090         // undo effect of Ctrl-Tab (i.e. pull next cell)\r
1091         //if (idx + 1 != nargs())\r
1092         //      cell(idx).swap(cell(idx + 1));\r
1093 \r
1094         // we handled the event..\r
1095         return true;\r
1096 }\r
1097 \r
1098 \r
1099 // reimplement old behaviour when pressing Delete in the last position\r
1100 // of a cell\r
1101 void InsetMathGrid::idxGlue(idx_type idx)\r
1102 {\r
1103         col_type c = col(idx);\r
1104         if (c + 1 == ncols()) {\r
1105                 if (row(idx) + 1 != nrows()) {\r
1106                         for (col_type cc = 0; cc < ncols(); ++cc)\r
1107                                 cell(idx).append(cell(idx + cc + 1));\r
1108                         delRow(row(idx) + 1);\r
1109                 }\r
1110         } else {\r
1111                 idx_type idx_next = idx + 1;\r
1112                 while (idx_next < nargs() &&\r
1113                        cellinfo_[idx_next].multi_ == CELL_PART_OF_MULTICOLUMN)\r
1114                         ++idx_next;\r
1115                 if (idx_next < nargs())\r
1116                         cell(idx).append(cell(idx_next));\r
1117                 col_type oldcol = c + 1;\r
1118                 for (col_type cc = c + 2; cc < ncols(); ++cc)\r
1119                         cell(idx - oldcol + cc) = cell(idx - oldcol + 1 + cc);\r
1120                 cell(idx - c + ncols() - 1).clear();\r
1121         }\r
1122 }\r
1123 \r
1124 \r
1125 InsetMathGrid::RowInfo const & InsetMathGrid::rowinfo(row_type row) const\r
1126 {\r
1127         return rowinfo_[row];\r
1128 }\r
1129 \r
1130 \r
1131 InsetMathGrid::RowInfo & InsetMathGrid::rowinfo(row_type row)\r
1132 {\r
1133         return rowinfo_[row];\r
1134 }\r
1135 \r
1136 \r
1137 bool InsetMathGrid::idxBetween(idx_type idx, idx_type from, idx_type to) const\r
1138 {\r
1139         row_type const ri = row(idx);\r
1140         row_type const r1 = min(row(from), row(to));\r
1141         row_type const r2 = max(row(from), row(to));\r
1142         col_type const ci = col(idx);\r
1143         col_type const c1 = min(col(from), col(to));\r
1144         col_type const c2 = max(col(from), col(to));\r
1145         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;\r
1146 }\r
1147 \r
1148 \r
1149 void InsetMathGrid::normalize(NormalStream & os) const\r
1150 {\r
1151         os << "[grid ";\r
1152         for (row_type row = 0; row < nrows(); ++row) {\r
1153                 os << "[row ";\r
1154                 for (col_type col = 0; col < ncols(); ++col) {\r
1155                         idx_type const i = index(row, col);\r
1156                         switch (cellinfo_[i].multi_) {\r
1157                         case CELL_NORMAL:\r
1158                                 os << "[cell " << cell(i) << ']';\r
1159                                 break;\r
1160                         case CELL_BEGIN_OF_MULTICOLUMN:\r
1161                                 os << "[cell colspan="\r
1162                                    << static_cast<int>(ncellcols(i)) << ' '\r
1163                                    << cell(i) << ']';\r
1164                                 break;\r
1165                         case CELL_PART_OF_MULTICOLUMN:\r
1166                                 break;\r
1167                         }\r
1168                 }\r
1169                 os << ']';\r
1170         }\r
1171         os << ']';\r
1172 }\r
1173 \r
1174 \r
1175 void InsetMathGrid::mathmlize(MathStream & os) const\r
1176 {\r
1177         bool const havetable = nrows() > 1 || ncols() > 1;\r
1178         if (havetable)\r
1179                 os << MTag("mtable");\r
1180         char const * const celltag = havetable ? "mtd" : "mrow";\r
1181         for (row_type row = 0; row < nrows(); ++row) {\r
1182                 if (havetable)\r
1183                         os << MTag("mtr");\r
1184                 for (col_type col = 0; col < ncols(); ++col) {\r
1185                         idx_type const i = index(row, col);\r
1186                         if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
1187                                 col_type const cellcols = ncellcols(i);\r
1188                                 ostringstream attr;\r
1189                                 if (havetable && cellcols > 1)\r
1190                                         attr << "colspan='" << cellcols << '\'';\r
1191                                 os << MTag(celltag, attr.str());\r
1192                                 os << cell(index(row, col));\r
1193                                 os << ETag(celltag);\r
1194                         }\r
1195                 }\r
1196                 if (havetable)\r
1197                         os << ETag("mtr");\r
1198         }\r
1199         if (havetable)\r
1200                 os << ETag("mtable");\r
1201 }\r
1202 \r
1203 \r
1204 // FIXME XHTML\r
1205 // We need to do something about alignment here.\r
1206 void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const\r
1207 {\r
1208         bool const havetable = nrows() > 1 || ncols() > 1;\r
1209         if (!havetable) {\r
1210                 os << cell(index(0, 0));\r
1211                 return;\r
1212         }\r
1213         os << MTag("table", attrib);\r
1214         for (row_type row = 0; row < nrows(); ++row) {\r
1215                 os << MTag("tr");\r
1216                 for (col_type col = 0; col < ncols(); ++col) {\r
1217                         idx_type const i = index(row, col);\r
1218                         if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {\r
1219                                 col_type const cellcols = ncellcols(i);\r
1220                                 ostringstream attr;\r
1221                                 if (cellcols > 1)\r
1222                                         attr << "colspan='" << cellcols << '\'';\r
1223                                 os << MTag("td", attr.str());\r
1224                                 os << cell(index(row, col));\r
1225                                 os << ETag("td");\r
1226                         }\r
1227                 }\r
1228                 os << ETag("tr");\r
1229         }\r
1230         os << ETag("table");\r
1231 }\r
1232 \r
1233 \r
1234 void InsetMathGrid::htmlize(HtmlStream & os) const\r
1235 {\r
1236         htmlize(os, "class='mathtable'");\r
1237 }\r
1238 \r
1239 \r
1240 void InsetMathGrid::write(WriteStream & os) const\r
1241 {\r
1242         write(os, 0, 0, nrows(), ncols());\r
1243 }\r
1244 \r
1245 void InsetMathGrid::write(WriteStream & os,\r
1246                           row_type beg_row, col_type beg_col,\r
1247                           row_type end_row, col_type end_col) const\r
1248 {\r
1249         MathEnsurer ensurer(os, false);\r
1250         docstring eol;\r
1251         for (row_type row = beg_row; row < end_row; ++row) {\r
1252                 os << verboseHLine(rowinfo_[row].lines_);\r
1253                 // don't write & and empty cells at end of line,\r
1254                 // unless there are vertical lines\r
1255                 col_type lastcol = 0;\r
1256                 bool emptyline = true;\r
1257                 bool last_eoln = true;\r
1258                 for (col_type col = beg_col; col < end_col; ++col) {\r
1259                         idx_type const idx = index(row, col);\r
1260                         bool const empty_cell = cell(idx).empty();\r
1261                         if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL)\r
1262                                 last_eoln = false;\r
1263                         if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL ||\r
1264                             colinfo_[col + 1].lines_) {\r
1265                                 lastcol = col + 1;\r
1266                                 emptyline = false;\r
1267                         }\r
1268                 }\r
1269                 for (col_type col = beg_col; col < end_col;) {\r
1270                         int nccols = 1;\r
1271                         idx_type const idx = index(row, col);\r
1272                         TexRow::RowEntry entry = os.texrow().mathEntry(id(),idx);\r
1273                         os.texrow().startMath(id(),idx);\r
1274                         if (col >= lastcol) {\r
1275                                 ++col;\r
1276                                 continue;\r
1277                         }\r
1278                         os.pushRowEntry(entry);\r
1279                         if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN) {\r
1280                                 size_t s = col + 1;\r
1281                                 while (s < ncols() &&\r
1282                                        cellinfo_[index(row, s)].multi_ == CELL_PART_OF_MULTICOLUMN)\r
1283                                         s++;\r
1284                                 nccols = s - col;\r
1285                                 os << "\\multicolumn{" << nccols\r
1286                                    << "}{" << cellinfo_[idx].align_\r
1287                                    << "}{";\r
1288                         }\r
1289                         os << cell(idx);\r
1290                         if (os.pendingBrace())\r
1291                                 ModeSpecifier specifier(os, TEXT_MODE);\r
1292                         if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN)\r
1293                                 os << '}';\r
1294                         os << eocString(col + nccols - 1, lastcol);\r
1295                         col += nccols;\r
1296                         os.popRowEntry();\r
1297                 }\r
1298                 eol = eolString(row, os.fragile(), os.latex(), last_eoln);\r
1299                 os << eol;\r
1300                 // append newline only if line wasn't completely empty\r
1301                 // and the formula is not written on a single line\r
1302                 bool const empty = emptyline && eol.empty();\r
1303                 if (!empty && nrows() > 1)\r
1304                         os << "\n";\r
1305         }\r
1306         // @TODO use end_row instead of nrows() ?\r
1307         docstring const s = verboseHLine(rowinfo_[nrows()].lines_);\r
1308         if (!s.empty()) {\r
1309                 if (eol.empty()) {\r
1310                         if (os.fragile())\r
1311                                 os << "\\protect";\r
1312                         os << "\\\\";\r
1313                 }\r
1314                 os << s;\r
1315         }\r
1316 }\r
1317 \r
1318 \r
1319 int InsetMathGrid::colsep() const\r
1320 {\r
1321         return 6;\r
1322 }\r
1323 \r
1324 \r
1325 int InsetMathGrid::rowsep() const\r
1326 {\r
1327         return 6;\r
1328 }\r
1329 \r
1330 \r
1331 int InsetMathGrid::hlinesep() const\r
1332 {\r
1333         return 3;\r
1334 }\r
1335 \r
1336 \r
1337 int InsetMathGrid::vlinesep() const\r
1338 {\r
1339         return 3;\r
1340 }\r
1341 \r
1342 \r
1343 int InsetMathGrid::border() const\r
1344 {\r
1345         return 1;\r
1346 }\r
1347 \r
1348 \r
1349 void InsetMathGrid::splitCell(Cursor & cur)\r
1350 {\r
1351         if (cur.idx() == cur.lastidx())\r
1352                 return;\r
1353         MathData ar = cur.cell();\r
1354         ar.erase(0, cur.pos());\r
1355         cur.cell().erase(cur.pos(), cur.lastpos());\r
1356         ++cur.idx();\r
1357         while (cur.idx() << nargs() &&\r
1358                cellinfo_[cur.idx()].multi_ == CELL_BEGIN_OF_MULTICOLUMN)\r
1359                 ++cur.idx();\r
1360         cur.pos() = 0;\r
1361         cur.cell().insert(0, ar);\r
1362 }\r
1363 \r
1364 \r
1365 char InsetMathGrid::displayColAlign(idx_type idx) const\r
1366 {\r
1367         if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN) {\r
1368                 // align_ may also contain lines like "||r|", so this is\r
1369                 // not complete, but we catch at least the simple cases.\r
1370                 if (cellinfo_[idx].align_ == "c")\r
1371                         return 'c';\r
1372                 if (cellinfo_[idx].align_ == "l")\r
1373                         return 'l';\r
1374                 if (cellinfo_[idx].align_ == "r")\r
1375                         return 'r';\r
1376         }\r
1377         return colinfo_[col(idx)].align_;\r
1378 }\r
1379 \r
1380 \r
1381 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)\r
1382 {\r
1383         //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;\r
1384 \r
1385         Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;\r
1386 \r
1387         FuncCode const act = cmd.action();\r
1388         switch (act) {\r
1389 \r
1390         // insert file functions\r
1391         case LFUN_LINE_DELETE_FORWARD:\r
1392                 cur.recordUndoInset();\r
1393                 //autocorrect_ = false;\r
1394                 //macroModeClose();\r
1395                 //if (selection_) {\r
1396                 //      selDel();\r
1397                 //      break;\r
1398                 //}\r
1399                 if (nrows() > 1)\r
1400                         delRow(cur.row());\r
1401                 if (cur.idx() > cur.lastidx())\r
1402                         cur.idx() = cur.lastidx();\r
1403                 if (cur.pos() > cur.lastpos())\r
1404                         cur.pos() = cur.lastpos();\r
1405                 break;\r
1406 \r
1407         case LFUN_CELL_SPLIT:\r
1408                 cur.recordUndo();\r
1409                 splitCell(cur);\r
1410                 break;\r
1411 \r
1412         case LFUN_CELL_BACKWARD:\r
1413                 // See below.\r
1414                 cur.setSelection(false);\r
1415                 if (!idxPrev(cur)) {\r
1416                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);\r
1417                         cur.undispatched();\r
1418                 }\r
1419                 break;\r
1420 \r
1421         case LFUN_CELL_FORWARD:\r
1422                 // Can't handle selection by additional 'shift' as this is\r
1423                 // hard bound to LFUN_CELL_BACKWARD\r
1424                 cur.setSelection(false);\r
1425                 if (!idxNext(cur)) {\r
1426                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);\r
1427                         cur.undispatched();\r
1428                 }\r
1429                 break;\r
1430 \r
1431         case LFUN_NEWLINE_INSERT: {\r
1432                 cur.recordUndoInset();\r
1433                 row_type const r = cur.row();\r
1434                 addRow(r);\r
1435 \r
1436                 // split line\r
1437                 for (col_type c = col(cur.idx()) + 1; c < ncols(); ++c)\r
1438                         swap(cell(index(r, c)), cell(index(r + 1, c)));\r
1439 \r
1440                 // split cell\r
1441                 splitCell(cur);\r
1442                 if (ncols() > 1)\r
1443                         swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));\r
1444                 if (cur.idx() > 0)\r
1445                         --cur.idx();\r
1446                 cur.pos() = cur.lastpos();\r
1447                 cur.forceBufferUpdate();\r
1448                 //mathcursor->normalize();\r
1449                 //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);\r
1450                 break;\r
1451         }\r
1452 \r
1453         case LFUN_INSET_MODIFY: {\r
1454                 cur.recordUndoInset();\r
1455                 //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;\r
1456                 istringstream is(to_utf8(cmd.argument()));\r
1457                 string s;\r
1458                 is >> s;\r
1459                 if (s != "tabular") {\r
1460                         InsetMathNest::doDispatch(cur, cmd);\r
1461                         return;\r
1462                 }\r
1463                 is >> s;\r
1464                 if (s == "valign-top")\r
1465                         setVerticalAlignment('t');\r
1466                 else if (s == "valign-middle")\r
1467                         setVerticalAlignment('c');\r
1468                 else if (s == "valign-bottom")\r
1469                         setVerticalAlignment('b');\r
1470                 else if (s == "align-left")\r
1471                         setHorizontalAlignment('l', cur.col());\r
1472                 else if (s == "align-right")\r
1473                         setHorizontalAlignment('r', cur.col());\r
1474                 else if (s == "align-center")\r
1475                         setHorizontalAlignment('c', cur.col());\r
1476                 else if (s == "append-row")\r
1477                         for (int i = 0, n = extractInt(is); i < n; ++i)\r
1478                                 addRow(cur.row());\r
1479                 else if (s == "delete-row") {\r
1480                         cur.clearSelection(); // bug 4323\r
1481                         for (int i = 0, n = extractInt(is); i < n; ++i) {\r
1482                                 delRow(cur.row());\r
1483                                 if (cur.idx() >= nargs())\r
1484                                         cur.idx() -= ncols();\r
1485                         }\r
1486                         cur.pos() = 0; // trick, see below\r
1487                 }\r
1488                 else if (s == "copy-row") {\r
1489                         // Here (as later) we save the cursor col/row\r
1490                         // in order to restore it after operation.\r
1491                         row_type const r = cur.row();\r
1492                         col_type const c = cur.col();\r
1493                         for (int i = 0, n = extractInt(is); i < n; ++i)\r
1494                                 copyRow(cur.row());\r
1495                         cur.idx() = index(r, c);\r
1496                 }\r
1497                 else if (s == "swap-row") {\r
1498                         swapRow(cur.row());\r
1499                         // Trick to suppress same-idx-means-different-cell\r
1500                         // assertion crash:\r
1501                         cur.pos() = 0;\r
1502                 }\r
1503                 else if (s == "add-hline-above")\r
1504                         rowinfo_[cur.row()].lines_++;\r
1505                 else if (s == "add-hline-below")\r
1506                         rowinfo_[cur.row()+1].lines_++;\r
1507                 else if (s == "delete-hline-above")\r
1508                         rowinfo_[cur.row()].lines_--;\r
1509                 else if (s == "delete-hline-below")\r
1510                         rowinfo_[cur.row()+1].lines_--;\r
1511                 else if (s == "append-column") {\r
1512                         row_type const r = cur.row();\r
1513                         col_type const c = cur.col();\r
1514                         for (int i = 0, n = extractInt(is); i < n; ++i)\r
1515                                 addCol(cur.col() + 1);\r
1516                         cur.idx() = index(r, c);\r
1517                 }\r
1518                 else if (s == "delete-column") {\r
1519                         cur.clearSelection(); // bug 4323\r
1520                         row_type const r = cur.row();\r
1521                         col_type const c = cur.col();\r
1522                         for (int i = 0, n = extractInt(is); i < n; ++i)\r
1523                                 delCol(col(cur.idx()));\r
1524                         cur.idx() = index(r, min(c, cur.ncols() - 1));\r
1525                         cur.pos() = 0; // trick, see above\r
1526                 }\r
1527                 else if (s == "copy-column") {\r
1528                         row_type const r = cur.row();\r
1529                         col_type const c = cur.col();\r
1530                         copyCol(cur.col());\r
1531                         cur.idx() = index(r, c);\r
1532                 }\r
1533                 else if (s == "swap-column") {\r
1534                         swapCol(cur.col());\r
1535                         cur.pos() = 0; // trick, see above\r
1536                 }\r
1537                 else if (s == "add-vline-left") {\r
1538                         colinfo_[cur.col()].lines_++;\r
1539                         if (!colinfo_[cur.col()].special_.empty())\r
1540                                 colinfo_[cur.col()].special_ += '|';\r
1541                 }\r
1542                 else if (s == "add-vline-right") {\r
1543                         colinfo_[cur.col()+1].lines_++;\r
1544                         if (!colinfo_[cur.col()+1].special_.empty())\r
1545                                 colinfo_[cur.col()+1].special_.insert(0, 1, '|');\r
1546                 }\r
1547                 else if (s == "delete-vline-left") {\r
1548                         colinfo_[cur.col()].lines_--;\r
1549                         docstring & special = colinfo_[cur.col()].special_;\r
1550                         if (!special.empty()) {\r
1551                                 docstring::size_type i = special.rfind('|');\r
1552                                 LASSERT(i != docstring::npos, break);\r
1553                                 special.erase(i, 1);\r
1554                         }\r
1555                 }\r
1556                 else if (s == "delete-vline-right") {\r
1557                         colinfo_[cur.col()+1].lines_--;\r
1558                         docstring & special = colinfo_[cur.col()+1].special_;\r
1559                         if (!special.empty()) {\r
1560                                 docstring::size_type i = special.find('|');\r
1561                                 LASSERT(i != docstring::npos, break);\r
1562                                 special.erase(i, 1);\r
1563                         }\r
1564                 }\r
1565                 else {\r
1566                         cur.undispatched();\r
1567                         break;\r
1568                 }\r
1569                 // perhaps this should be FINISHED_BACKWARD -- just for clarity?\r
1570                 //lyxerr << "returning FINISHED_LEFT" << endl;\r
1571                 break;\r
1572         }\r
1573 \r
1574         case LFUN_CLIPBOARD_PASTE:\r
1575                 parseflg |= Parse::VERBATIM;\r
1576                 // fall through\r
1577         case LFUN_PASTE: {\r
1578                 if (cur.currentMode() <= TEXT_MODE)\r
1579                         parseflg |= Parse::TEXTMODE;\r
1580                 cur.message(_("Paste"));\r
1581                 cap::replaceSelection(cur);\r
1582                 docstring topaste;\r
1583                 if (cmd.argument().empty() && !theClipboard().isInternal())\r
1584                         topaste = theClipboard().getAsText(Clipboard::PlainTextType);\r
1585                 else {\r
1586                         idocstringstream is(cmd.argument());\r
1587                         int n = 0;\r
1588                         is >> n;\r
1589                         topaste = cap::selection(n, buffer().params().documentClassPtr());\r
1590                 }\r
1591                 InsetMathGrid grid(buffer_, 1, 1);\r
1592                 if (!topaste.empty())\r
1593                         if ((topaste.size() == 1 && isAscii(topaste))\r
1594                             || !mathed_parse_normal(grid, topaste, parseflg)) {\r
1595                                 resetGrid(grid);\r
1596                                 mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);\r
1597                         }\r
1598 \r
1599                 bool hline_enabled = false;\r
1600                 FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");\r
1601                 FuncStatus status;\r
1602                 if (getStatus(cur, fr, status))\r
1603                         hline_enabled = status.enabled();\r
1604                 if (grid.nargs() == 1) {\r
1605                         // single cell/part of cell\r
1606                         cur.recordUndoInset();\r
1607                         cur.cell().insert(cur.pos(), grid.cell(0));\r
1608                         cur.pos() += grid.cell(0).size();\r
1609                         if (hline_enabled)\r
1610                                 rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_;\r
1611                         else {\r
1612                                 for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) {\r
1613                                          cur.cell().insert(0,\r
1614                                                 MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));\r
1615                                          cur.pos()++;\r
1616                                 }\r
1617                         }\r
1618                 } else {\r
1619                         // multiple cells\r
1620                         cur.recordUndoInset();\r
1621                         col_type const numcols =\r
1622                                 min(grid.ncols(), ncols() - col(cur.idx()));\r
1623                         row_type const numrows =\r
1624                                 min(grid.nrows(), nrows() - cur.row());\r
1625                         for (row_type r = 0; r < numrows; ++r) {\r
1626                                 for (col_type c = 0; c < numcols; ++c) {\r
1627                                         idx_type i = index(r + cur.row(), c + col(cur.idx()));\r
1628                                         cell(i).insert(0, grid.cell(grid.index(r, c)));\r
1629                                 }\r
1630                                 if (hline_enabled)\r
1631                                         rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;\r
1632                                 else {\r
1633                                         for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {\r
1634                                                 idx_type i = index(r + cur.row(), 0);\r
1635                                                 cell(i).insert(0,\r
1636                                                         MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));\r
1637                                         }\r
1638                                 }\r
1639                                 // append the left over horizontal cells to the last column\r
1640                                 idx_type i = index(r + cur.row(), ncols() - 1);\r
1641                                 for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)\r
1642                                         cell(i).append(grid.cell(grid.index(r, c)));\r
1643                         }\r
1644                         // append the left over vertical cells to the last _cell_\r
1645                         idx_type i = nargs() - 1;\r
1646                         for (row_type r = numrows; r < grid.nrows(); ++r) {\r
1647                                 for (col_type c = 0; c < grid.ncols(); ++c)\r
1648                                         cell(i).append(grid.cell(grid.index(r, c)));\r
1649                                 if (hline_enabled)\r
1650                                         rowinfo_[r].lines_ += grid.rowinfo_[r].lines_;\r
1651                                 else {\r
1652                                         for (unsigned int l = 0; l < grid.rowinfo_[r].lines_; ++l) {\r
1653                                                 cell(i).insert(0,\r
1654                                                         MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));\r
1655                                         }\r
1656                                 }\r
1657                         }\r
1658                 }\r
1659                 cur.clearSelection(); // bug 393\r
1660                 // FIXME audit setBuffer calls\r
1661                 cur.inset().setBuffer(*buffer_);\r
1662                 cur.forceBufferUpdate();\r
1663                 cur.finishUndo();\r
1664                 break;\r
1665         }\r
1666 \r
1667         case LFUN_LINE_BEGIN:\r
1668         case LFUN_WORD_BACKWARD:\r
1669         case LFUN_WORD_LEFT:\r
1670                 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);\r
1671                 // fall through\r
1672         case LFUN_LINE_BEGIN_SELECT:\r
1673         case LFUN_WORD_BACKWARD_SELECT:\r
1674         case LFUN_WORD_LEFT_SELECT:\r
1675                 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||\r
1676                                 act == LFUN_WORD_LEFT_SELECT ||\r
1677                                 act == LFUN_LINE_BEGIN_SELECT);\r
1678                 cur.macroModeClose();\r
1679                 if (cur.pos() != 0) {\r
1680                         cur.pos() = 0;\r
1681                 } else if (cur.idx() % cur.ncols() != 0) {\r
1682                         cur.idx() -= cur.idx() % cur.ncols();\r
1683                         cur.pos() = 0;\r
1684                 } else if (cur.idx() != 0) {\r
1685                         cur.idx() = 0;\r
1686                         cur.pos() = 0;\r
1687                 } else {\r
1688                         cmd = FuncRequest(LFUN_FINISHED_BACKWARD);\r
1689                         cur.undispatched();\r
1690                 }\r
1691                 break;\r
1692 \r
1693         case LFUN_WORD_FORWARD:\r
1694         case LFUN_WORD_RIGHT:\r
1695         case LFUN_LINE_END:\r
1696                 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);\r
1697                 // fall through\r
1698         case LFUN_WORD_FORWARD_SELECT:\r
1699         case LFUN_WORD_RIGHT_SELECT:\r
1700         case LFUN_LINE_END_SELECT:\r
1701                 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||\r
1702                                 act == LFUN_WORD_RIGHT_SELECT ||\r
1703                                 act == LFUN_LINE_END_SELECT);\r
1704                 cur.macroModeClose();\r
1705                 cur.clearTargetX();\r
1706                 if (cur.pos() != cur.lastpos()) {\r
1707                         cur.pos() = cur.lastpos();\r
1708                 } else if ((cur.idx() + 1) % cur.ncols() != 0) {\r
1709                         cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();\r
1710                         cur.pos() = cur.lastpos();\r
1711                 } else if (cur.idx() != cur.lastidx()) {\r
1712                         cur.idx() = cur.lastidx();\r
1713                         cur.pos() = cur.lastpos();\r
1714                 } else {\r
1715                         cmd = FuncRequest(LFUN_FINISHED_FORWARD);\r
1716                         cur.undispatched();\r
1717                 }\r
1718                 break;\r
1719 \r
1720         default:\r
1721                 InsetMathNest::doDispatch(cur, cmd);\r
1722         }\r
1723 }\r
1724 \r
1725 \r
1726 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,\r
1727                 FuncStatus & status) const\r
1728 {\r
1729         switch (cmd.action()) {\r
1730         case LFUN_INSET_MODIFY: {\r
1731                 istringstream is(to_utf8(cmd.argument()));\r
1732                 string s;\r
1733                 is >> s;\r
1734                 if (s != "tabular") {\r
1735                         // We only now about table actions here.\r
1736                         break;\r
1737                 }\r
1738                 if (&cur.inset() != this) {\r
1739                         // Table actions requires that the cursor is _inside_ the\r
1740                         // table.\r
1741                         status.setEnabled(false);\r
1742                         status.message(from_utf8(N_("Cursor not in table")));\r
1743                         return true;\r
1744                 }\r
1745                 is >> s;\r
1746                 if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {\r
1747                         status.setEnabled(false);\r
1748                         status.message(from_utf8(N_("Only one row")));\r
1749                         return true;\r
1750                 }\r
1751                 if (ncols() <= 1 &&\r
1752                     (s == "delete-column" || s == "swap-column")) {\r
1753                         status.setEnabled(false);\r
1754                         status.message(from_utf8(N_("Only one column")));\r
1755                         return true;\r
1756                 }\r
1757                 if ((rowinfo_[cur.row()].lines_ == 0 &&\r
1758                      s == "delete-hline-above") ||\r
1759                     (rowinfo_[cur.row() + 1].lines_ == 0 &&\r
1760                      s == "delete-hline-below")) {\r
1761                         status.setEnabled(false);\r
1762                         status.message(from_utf8(N_("No hline to delete")));\r
1763                         return true;\r
1764                 }\r
1765 \r
1766                 if ((colinfo_[cur.col()].lines_ == 0 &&\r
1767                      s == "delete-vline-left") ||\r
1768                     (colinfo_[cur.col() + 1].lines_ == 0 &&\r
1769                      s == "delete-vline-right")) {\r
1770                         status.setEnabled(false);\r
1771                         status.message(from_utf8(N_("No vline to delete")));\r
1772                         return true;\r
1773                 }\r
1774                 if (s == "valign-top" || s == "valign-middle" ||\r
1775                     s == "valign-bottom" || s == "align-left" ||\r
1776                     s == "align-right" || s == "align-center") {\r
1777                         status.setEnabled(true);\r
1778                         char const ha = horizontalAlignment(cur.col());\r
1779                         char const va = verticalAlignment();\r
1780                         status.setOnOff((s == "align-left" && ha == 'l')\r
1781                                         || (s == "align-right"   && ha == 'r')\r
1782                                         || (s == "align-center"  && ha == 'c')\r
1783                                         || (s == "valign-top"    && va == 't')\r
1784                                         || (s == "valign-bottom" && va == 'b')\r
1785                                         || (s == "valign-middle" && va == 'c'));\r
1786                         return true;\r
1787                 }\r
1788                 if (s == "append-row" || s == "delete-row" ||\r
1789                     s == "copy-row" || s == "swap-row" ||\r
1790                     s == "add-hline-above" || s == "add-hline-below" ||\r
1791                     s == "delete-hline-above" || s == "delete-hline-below" ||\r
1792                     s == "append-column" || s == "delete-column" ||\r
1793                     s == "copy-column" || s == "swap-column" ||\r
1794                     s == "add-vline-left" || s == "add-vline-right" ||\r
1795                     s == "delete-vline-left" || s == "delete-vline-right") {\r
1796                         status.setEnabled(true);\r
1797                 } else {\r
1798                         status.setEnabled(false);\r
1799                         status.message(bformat(\r
1800                                 from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));\r
1801                 }\r
1802 \r
1803 #if 0\r
1804                 // FIXME: What did this code do?\r
1805                 // Please check whether it is still needed!\r
1806                 // should be more precise\r
1807                 if (v_align_ == '\0') {\r
1808                         status.enable(true);\r
1809                         break;\r
1810                 }\r
1811                 if (cmd.argument().empty()) {\r
1812                         status.enable(false);\r
1813                         break;\r
1814                 }\r
1815                 if (!contains("tcb", cmd.argument()[0])) {\r
1816                         status.enable(false);\r
1817                         break;\r
1818                 }\r
1819                 status.setOnOff(cmd.argument()[0] == v_align_);\r
1820                 status.setEnabled(true);\r
1821 #endif\r
1822                 return true;\r
1823         }\r
1824 \r
1825         case LFUN_CELL_SPLIT:\r
1826                 status.setEnabled(cur.idx() != cur.lastidx());\r
1827                 return true;\r
1828 \r
1829         case LFUN_CELL_BACKWARD:\r
1830         case LFUN_CELL_FORWARD:\r
1831                 status.setEnabled(true);\r
1832                 return true;\r
1833 \r
1834         default:\r
1835                 break;\r
1836         }\r
1837         return InsetMathNest::getStatus(cur, cmd, status);\r
1838 }\r
1839 \r
1840 \r
1841 // static\r
1842 char InsetMathGrid::colAlign(HullType type, col_type col)\r
1843 {\r
1844         switch (type) {\r
1845         case hullEqnArray:\r
1846                 return "rcl"[col % 3];\r
1847 \r
1848         case hullMultline:\r
1849         case hullGather:\r
1850                 return 'c';\r
1851 \r
1852         case hullAlign:\r
1853         case hullAlignAt:\r
1854         case hullXAlignAt:\r
1855         case hullXXAlignAt:\r
1856         case hullFlAlign:\r
1857                 return "rl"[col & 1];\r
1858 \r
1859         default:\r
1860                 return 'c';\r
1861         }\r
1862 }\r
1863 \r
1864 \r
1865 \r
1866 } // namespace lyx\r