]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
* insettabular.C (drawSelection): revision of previous patch:
[lyx.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "language.h"
27 #include "LColor.h"
28 #include "lyx_cb.h"
29 #include "lyxlex.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "undo.h"
36
37 #include "support/convert.h"
38
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
44
45 #include <sstream>
46 #include <iostream>
47 #include <limits>
48
49 using lyx::cap::dirtyTabularStack;
50 using lyx::cap::tabularStackDirty;
51
52 using lyx::graphics::PreviewLoader;
53
54 using lyx::support::ltrim;
55
56 using lyx::frontend::Painter;
57
58 using boost::shared_ptr;
59
60 using std::auto_ptr;
61 using std::endl;
62 using std::max;
63 using std::string;
64 using std::istringstream;
65 using std::ostream;
66 using std::ostringstream;
67 using std::swap;
68 using std::vector;
69
70
71 namespace {
72
73 int const ADD_TO_HEIGHT = 2;
74 int const ADD_TO_TABULAR_WIDTH = 2;
75
76 ///
77 boost::scoped_ptr<LyXTabular> paste_tabular;
78
79
80 struct TabularFeature {
81         LyXTabular::Feature action;
82         string feature;
83 };
84
85
86 TabularFeature tabularFeature[] =
87 {
88         { LyXTabular::APPEND_ROW, "append-row" },
89         { LyXTabular::APPEND_COLUMN, "append-column" },
90         { LyXTabular::DELETE_ROW, "delete-row" },
91         { LyXTabular::DELETE_COLUMN, "delete-column" },
92         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
93         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
94         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
95         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
96         { LyXTabular::ALIGN_LEFT, "align-left" },
97         { LyXTabular::ALIGN_RIGHT, "align-right" },
98         { LyXTabular::ALIGN_CENTER, "align-center" },
99         { LyXTabular::ALIGN_BLOCK, "align-block" },
100         { LyXTabular::VALIGN_TOP, "valign-top" },
101         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
102         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
103         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
104         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
105         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
106         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
107         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
108         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
109         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
110         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
111         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
112         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
113         { LyXTabular::MULTICOLUMN, "multicolumn" },
114         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
115         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
116         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
117         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
118         { LyXTabular::SET_PWIDTH, "set-pwidth" },
119         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
120         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
121         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
122         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
123         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
124         { LyXTabular::SET_USEBOX, "set-usebox" },
125         { LyXTabular::SET_LTHEAD, "set-lthead" },
126         { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
127         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
128         { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
129         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
130         { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
131         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
132         { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
133         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
134         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
135         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
136         { LyXTabular::LAST_ACTION, "" }
137 };
138
139
140 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
141 public:
142         FeatureEqual(LyXTabular::Feature feature)
143                 : feature_(feature) {}
144         bool operator()(TabularFeature const & tf) const {
145                 return tf.action == feature_;
146         }
147 private:
148         LyXTabular::Feature feature_;
149 };
150
151 } // namespace anon
152
153
154 string const featureAsString(LyXTabular::Feature feature)
155 {
156         TabularFeature * end = tabularFeature +
157                 sizeof(tabularFeature) / sizeof(TabularFeature);
158         TabularFeature * it = std::find_if(tabularFeature, end,
159                                            FeatureEqual(feature));
160         return (it == end) ? string() : it->feature;
161 }
162
163
164 bool InsetTabular::hasPasteBuffer() const
165 {
166         return (paste_tabular.get() != 0);
167 }
168
169
170 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
171                            col_type columns)
172         : tabular(buf.params(), max(rows, row_type(1)),
173           max(columns, col_type(1))), buffer_(&buf), scx_(0)
174 {}
175
176
177 InsetTabular::InsetTabular(InsetTabular const & tab)
178         : InsetOld(tab), tabular(tab.tabular),
179                 buffer_(tab.buffer_), scx_(0)
180 {}
181
182
183 InsetTabular::~InsetTabular()
184 {
185         InsetTabularMailer(*this).hideDialog();
186 }
187
188
189 auto_ptr<InsetBase> InsetTabular::doClone() const
190 {
191         return auto_ptr<InsetBase>(new InsetTabular(*this));
192 }
193
194
195 Buffer const & InsetTabular::buffer() const
196 {
197         return *buffer_;
198 }
199
200
201 void InsetTabular::buffer(Buffer const * b)
202 {
203         buffer_ = b;
204 }
205
206
207 void InsetTabular::write(Buffer const & buf, ostream & os) const
208 {
209         os << "Tabular" << endl;
210         tabular.write(buf, os);
211 }
212
213
214 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
215 {
216         bool const old_format = (lex.getString() == "\\LyXTable");
217
218         tabular.read(buf, lex);
219
220         if (old_format)
221                 return;
222
223         lex.next();
224         string token = lex.getString();
225         while (lex.isOK() && (token != "\\end_inset")) {
226                 lex.next();
227                 token = lex.getString();
228         }
229         if (token != "\\end_inset") {
230                 lex.printError("Missing \\end_inset at this point. "
231                                "Read: `$$Token'");
232         }
233 }
234
235
236 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
237 {
238         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
239         //      mi.base.textwidth << "\n";
240         if (!mi.base.bv) {
241                 lyxerr << "InsetTabular::metrics: need bv" << endl;
242                 BOOST_ASSERT(false);
243         }
244
245         row_type i = 0;
246         for (idx_type cell = 0; i < tabular.rows(); ++i) {
247                 int maxAsc = 0;
248                 int maxDesc = 0;
249                 for (col_type j = 0; j < tabular.columns(); ++j) {
250                         if (tabular.isPartOfMultiColumn(i, j))
251                                 // Multicolumn cell, but not first one
252                                 continue;
253                         Dimension dim;
254                         MetricsInfo m = mi;
255                         LyXLength p_width;
256                         if (tabular.cell_info[i][j].multicolumn ==
257                             LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
258                                 p_width = tabular.cellinfo_of_cell(cell).p_width;
259                         else
260                                 p_width = tabular.column_info[j].p_width;
261                         if (!p_width.zero())
262                                 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
263                         tabular.getCellInset(cell)->metrics(m, dim);
264                         if (!p_width.zero())
265                                 dim.wid = m.base.textwidth;
266                         maxAsc  = max(maxAsc, dim.asc);
267                         maxDesc = max(maxDesc, dim.des);
268                         tabular.setWidthOfCell(cell, dim.wid);
269                         ++cell;
270                 }
271                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
272                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
273         }
274
275         dim.asc = tabular.getAscentOfRow(0);
276         dim.des = tabular.getHeightOfTabular() - dim.asc;
277         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
278         dim_ = dim;
279 }
280
281
282 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
283 {
284         setPosCache(pi, x, y);
285
286         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
287         BufferView * bv = pi.base.bv;
288
289         static lyx::frontend::NullPainter nop;
290         static PainterInfo nullpi(bv, nop);
291
292         //resetPos(bv->cursor());
293
294         x += scx_;
295         x += ADD_TO_TABULAR_WIDTH;
296
297         idx_type idx = 0;
298         first_visible_cell = LyXTabular::npos;
299         for (row_type i = 0; i < tabular.rows(); ++i) {
300                 int nx = x;
301                 int const a = tabular.getAscentOfRow(i);
302                 int const d = tabular.getDescentOfRow(i);
303                 idx = tabular.getCellNumber(i, 0);
304                 for (col_type j = 0; j < tabular.columns(); ++j) {
305                         if (tabular.isPartOfMultiColumn(i, j))
306                                 continue;
307                         if (first_visible_cell == LyXTabular::npos)
308                                 first_visible_cell = idx;
309
310                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
311                         if (nx + tabular.getWidthOfColumn(idx) < 0
312                             || nx > bv->workWidth()
313                             || y + d < 0
314                             || y - a > bv->workHeight()) {
315                                 cell(idx)->draw(nullpi, cx, y);
316                                 drawCellLines(nop, nx, y, i, idx, pi.erased_);
317                         } else {
318                                 cell(idx)->draw(pi, cx, y);
319                                 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
320                         }
321                         nx += tabular.getWidthOfColumn(idx);
322                         ++idx;
323                 }
324
325                 if (i + 1 < tabular.rows())
326                         y += d + tabular.getAscentOfRow(i + 1) +
327                                 tabular.getAdditionalHeight(i + 1);
328         }
329 }
330
331
332 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
333 {
334         setPosCache(pi, x, y);
335
336         LCursor & cur = pi.base.bv->cursor();
337
338         x += scx_ + ADD_TO_TABULAR_WIDTH;
339
340         // Paint background of current tabular
341         int const w = tabular.getWidthOfTabular();
342         int const h = tabular.getHeightOfTabular();
343         int yy = y - tabular.getAscentOfRow(0);
344         pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
345
346         if (!cur.selection())
347                 return;
348         if (!ptr_cmp(&cur.inset(), this))
349                 return;
350
351         //resetPos(cur);
352
353
354         if (tablemode(cur)) {
355                 row_type rs, re;
356                 col_type cs, ce;
357                 getSelection(cur, rs, re, cs, ce);
358                 y -= tabular.getAscentOfRow(0);
359                 for (row_type j = 0; j < tabular.rows(); ++j) {
360                         int const a = tabular.getAscentOfRow(j);
361                         int const h = a + tabular.getDescentOfRow(j);
362                         int xx = x;
363                         y += tabular.getAdditionalHeight(j);
364                         for (col_type i = 0; i < tabular.columns(); ++i) {
365                                 if (tabular.isPartOfMultiColumn(j, i))
366                                         continue;
367                                 idx_type const cell =
368                                         tabular.getCellNumber(j, i);
369                                 int const w = tabular.getWidthOfColumn(cell);
370                                 if (i >= cs && i <= ce && j >= rs && j <= re)
371                                         pi.pain.fillRectangle(xx, y, w, h,
372                                                               LColor::selection);
373                                 xx += w;
374
375                         }
376                         y += h;
377                 }
378
379         } else {
380                 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
381         }
382 }
383
384
385 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
386                                  row_type row, idx_type cell, bool erased) const
387 {
388         int x2 = x + tabular.getWidthOfColumn(cell);
389         bool on_off = false;
390         LColor::color col = LColor::tabularline;
391         LColor::color onoffcol = LColor::tabularonoffline;
392
393         if (erased) {
394                 col = LColor::strikeout;
395                 onoffcol = LColor::strikeout;
396         }
397
398         if (!tabular.topAlreadyDrawn(cell)) {
399                 on_off = !tabular.topLine(cell);
400                 pain.line(x, y - tabular.getAscentOfRow(row),
401                           x2, y -  tabular.getAscentOfRow(row),
402                           on_off ? onoffcol : col,
403                           on_off ? Painter::line_onoffdash : Painter::line_solid);
404         }
405         on_off = !tabular.bottomLine(cell);
406         pain.line(x, y + tabular.getDescentOfRow(row),
407                   x2, y + tabular.getDescentOfRow(row),
408                   on_off ? onoffcol : col,
409                   on_off ? Painter::line_onoffdash : Painter::line_solid);
410         if (!tabular.leftAlreadyDrawn(cell)) {
411                 on_off = !tabular.leftLine(cell);
412                 pain.line(x, y -  tabular.getAscentOfRow(row),
413                           x, y +  tabular.getDescentOfRow(row),
414                           on_off ? onoffcol : col,
415                           on_off ? Painter::line_onoffdash : Painter::line_solid);
416         }
417         on_off = !tabular.rightLine(cell);
418         pain.line(x2 - tabular.getAdditionalWidth(cell),
419                   y -  tabular.getAscentOfRow(row),
420                   x2 - tabular.getAdditionalWidth(cell),
421                   y +  tabular.getDescentOfRow(row),
422                   on_off ? onoffcol : col,
423                   on_off ? Painter::line_onoffdash : Painter::line_solid);
424 }
425
426
427 string const InsetTabular::editMessage() const
428 {
429         return _("Opened table");
430 }
431
432
433 void InsetTabular::edit(LCursor & cur, bool left)
434 {
435         //lyxerr << "InsetTabular::edit: " << this << endl;
436         finishUndo();
437         cur.selection() = false;
438         cur.push(*this);
439         if (left) {
440                 if (isRightToLeft(cur))
441                         cur.idx() = tabular.getLastCellInRow(0);
442                 else
443                         cur.idx() = 0;
444                 cur.pit() = 0;
445                 cur.pos() = 0;
446         } else {
447                 if (isRightToLeft(cur))
448                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
449                 else
450                         cur.idx() = tabular.getNumberOfCells() - 1;
451                 cur.pit() = 0;
452                 cur.pos() = cur.lastpos(); // FIXME crude guess
453         }
454         // this accesses the position cache before it is initialized
455         //resetPos(cur);
456         //cur.bv().fitCursor();
457 }
458
459
460 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
461 {
462         lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
463                              << "\n  cur:" << cur << endl;
464         CursorSlice sl = cur.top();
465         LCursor & bvcur = cur.bv().cursor();
466
467         switch (cmd.action) {
468
469         case LFUN_MOUSE_PRESS:
470                 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
471
472                 if (cmd.button() == mouse_button::button1) {
473                         cur.selection() = false;
474                         setCursorFromCoordinates(cur, cmd.x, cmd.y);
475                         cur.resetAnchor();
476                         bvcur = cur;
477                         break;
478                 }
479
480                 if (cmd.button() == mouse_button::button2) {
481                         cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
482                         doDispatch(cur, cmd);
483                         break;
484                 }
485
486                 if (cmd.button() == mouse_button::button3)
487                         bvcur.setCursor(cur);
488
489                 break;
490
491         case LFUN_MOUSE_MOTION:
492                 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
493                 if (cmd.button() == mouse_button::button1) {
494                         // only accept motions to places not deeper nested than the real anchor
495                         if (bvcur.anchor_.hasPart(cur)) {
496                                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
497                                 bvcur.setCursor(cur);
498                                 bvcur.selection() = true;
499                         } else
500                                 cur.undispatched();
501                 }
502                 break;
503
504         case LFUN_MOUSE_RELEASE:
505                 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
506                 if (cmd.button() == mouse_button::button3)
507                         InsetTabularMailer(*this).showDialog(&cur.bv());
508                 break;
509
510         case LFUN_CELL_BACKWARD:
511                 movePrevCell(cur);
512                 cur.selection() = false;
513                 break;
514
515         case LFUN_CELL_FORWARD:
516                 moveNextCell(cur);
517                 cur.selection() = false;
518                 break;
519
520         case LFUN_CHAR_FORWARD_SELECT:
521         case LFUN_CHAR_FORWARD:
522                 cell(cur.idx())->dispatch(cur, cmd);
523                 if (!cur.result().dispatched()) {
524                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
525                         if (sl == cur.top())
526                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
527                         else
528                                 cur.dispatched();
529                 }
530                 break;
531
532         case LFUN_CHAR_BACKWARD_SELECT:
533         case LFUN_CHAR_BACKWARD:
534                 cell(cur.idx())->dispatch(cur, cmd);
535                 if (!cur.result().dispatched()) {
536                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
537                         if (sl == cur.top())
538                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
539                         else
540                                 cur.dispatched();
541                 }
542                 break;
543
544         case LFUN_DOWN_SELECT:
545         case LFUN_DOWN:
546                 cell(cur.idx())->dispatch(cur, cmd);
547                 cur.dispatched(); // override the cell's decision
548                 if (sl == cur.top())
549                         // if our LyXText didn't do anything to the cursor
550                         // then we try to put the cursor into the cell below
551                         // setting also the right targetX.
552                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
553                                 cur.idx() = tabular.getCellBelow(cur.idx());
554                                 cur.pit() = 0;
555                                 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
556                                         cur.pit(), 0, cur.targetX());
557                         }
558                 if (sl == cur.top()) {
559                         // we trick it to go to the RIGHT after leaving the
560                         // tabular.
561                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
562                         cur.undispatched();
563                 }
564                 break;
565
566         case LFUN_UP_SELECT:
567         case LFUN_UP:
568                 cell(cur.idx())->dispatch(cur, cmd);
569                 cur.dispatched(); // override the cell's decision
570                 if (sl == cur.top())
571                         // if our LyXText didn't do anything to the cursor
572                         // then we try to put the cursor into the cell above
573                         // setting also the right targetX.
574                         if (tabular.row_of_cell(cur.idx()) != 0) {
575                                 cur.idx() = tabular.getCellAbove(cur.idx());
576                                 cur.pit() = cur.lastpit();
577                                 LyXText const * text = cell(cur.idx())->getText(0);
578                                 cur.pos() = text->x2pos(
579                                         cur.pit(),
580                                         text->paragraphs().back().rows().size()-1,
581                                         cur.targetX());
582                         }
583                 if (sl == cur.top()) {
584                         cmd = FuncRequest(LFUN_FINISHED_UP);
585                         cur.undispatched();
586                 }
587                 break;
588
589 //      case LFUN_SCREEN_DOWN: {
590 //              //if (hasSelection())
591 //              //      cur.selection() = false;
592 //              col_type const col = tabular.column_of_cell(cur.idx());
593 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
594 //              if (t < yo() + tabular.getHeightOfTabular()) {
595 //                      cur.bv().scrollDocView(t);
596 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
597 //              } else {
598 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
599 //              }
600 //              cur.par() = 0;
601 //              cur.pos() = 0;
602 //              break;
603 //      }
604 //
605 //      case LFUN_SCREEN_UP: {
606 //              //if (hasSelection())
607 //              //      cur.selection() = false;
608 //              col_type const col = tabular.column_of_cell(cur.idx());
609 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
610 //              if (yo() < 0) {
611 //                      cur.bv().scrollDocView(t);
612 //                      if (yo() > 0)
613 //                              cur.idx() = col;
614 //                      else
615 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
616 //              } else {
617 //                      cur.idx() = col;
618 //              }
619 //              cur.par() = cur.lastpar();
620 //              cur.pos() = cur.lastpos();
621 //              break;
622 //      }
623
624         case LFUN_LAYOUT_TABULAR:
625                 InsetTabularMailer(*this).showDialog(&cur.bv());
626                 break;
627
628         case LFUN_INSET_DIALOG_UPDATE:
629                 InsetTabularMailer(*this).updateDialog(&cur.bv());
630                 break;
631
632         case LFUN_TABULAR_FEATURE:
633                 if (!tabularFeatures(cur, cmd.argument))
634                         cur.undispatched();
635                 break;
636
637         // insert file functions
638         case LFUN_FILE_INSERT_ASCII_PARA:
639         case LFUN_FILE_INSERT_ASCII: {
640                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
641                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
642                         cur.undispatched();
643                 break;
644         }
645
646         case LFUN_CUT:
647                 if (tablemode(cur)) {
648                         if (copySelection(cur)) {
649                                 recordUndoInset(cur, Undo::DELETE);
650                                 cutSelection(cur);
651                         }
652                 }
653                 else
654                         cell(cur.idx())->dispatch(cur, cmd);
655                 break;
656
657         case LFUN_CHAR_DELETE_BACKWARD:
658         case LFUN_CHAR_DELETE_FORWARD:
659                 if (tablemode(cur)) {
660                         recordUndoInset(cur, Undo::DELETE);
661                         cutSelection(cur);
662                 }
663                 else
664                         cell(cur.idx())->dispatch(cur, cmd);
665                 break;
666
667         case LFUN_COPY:
668                 if (!cur.selection())
669                         break;
670                 if (tablemode(cur)) {
671                         finishUndo();
672                         copySelection(cur);
673                 } else
674                         cell(cur.idx())->dispatch(cur, cmd);
675                 break;
676
677         case LFUN_PRIMARY_SELECTION_PASTE: {
678                 string const clip = cur.bv().getClipboard();
679                 if (clip.empty())
680                         break;
681                 // pass to InsertAsciiString, but
682                 // only if we have multi-cell content
683                 if (clip.find_first_of("\t\n") != string::npos) {
684                         if (insertAsciiString(cur.bv(), clip, false)) {
685                                 // content has been replaced,
686                                 // so cursor might be invalid
687                                 cur.pos() = cur.lastpos();
688                                 bvcur.setCursor(cur);
689                                 break;
690                         }
691                 } else {
692                         // so that the clipboard is used and it goes on
693                         // to default
694                         // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
695                         paste_tabular.reset();
696                         dirtyTabularStack(false);
697                 }
698                 // fall through
699         }
700
701         case LFUN_PASTE:
702                 if (hasPasteBuffer() && tabularStackDirty()) {
703                         recordUndoInset(cur, Undo::INSERT);
704                         pasteSelection(cur);
705                         break;
706                 }
707                 cell(cur.idx())->dispatch(cur, cmd);
708                 // Reset pasted paragraphs:
709                 if (tabular.getPWidth(cur.idx()).zero())
710                         cell(cur.idx())->forceParagraphsToDefault(cur);
711                 break;
712
713         case LFUN_FONT_EMPH:
714         case LFUN_FONT_BOLD:
715         case LFUN_FONT_ROMAN:
716         case LFUN_FONT_NOUN:
717         case LFUN_FONT_ITAL:
718         case LFUN_FONT_FRAK:
719         case LFUN_FONT_CODE:
720         case LFUN_FONT_SANS:
721         case LFUN_FONT_FREE_APPLY:
722         case LFUN_FONT_FREE_UPDATE:
723         case LFUN_FONT_SIZE:
724         case LFUN_FONT_UNDERLINE:
725         case LFUN_LANGUAGE:
726         case LFUN_WORD_CAPITALIZE:
727         case LFUN_WORD_UPCASE:
728         case LFUN_WORD_LOWCASE:
729         case LFUN_CHARS_TRANSPOSE:
730                 if (tablemode(cur)) {
731                         row_type rs, re;
732                         col_type cs, ce;
733                         getSelection(cur, rs, re, cs, ce);
734                         for (row_type i = rs; i <= re; ++i) {
735                                 for (col_type j = cs; j <= ce; ++j) {
736                                         // cursor follows cell:
737                                         cur.idx() = tabular.getCellNumber(i, j);
738                                         // select this cell only:
739                                         cur.pos() = 0;
740                                         cur.resetAnchor();
741                                         cur.pos() = cur.top().lastpos();
742                                         cur.setCursor(cur);
743                                         cur.setSelection();
744                                         cell(cur.idx())->dispatch(cur, cmd);
745                                 }
746                         }
747                         // Restore original selection
748                         cur.idx() = tabular.getCellNumber(rs, cs);
749                         cur.pos() = 0;
750                         cur.resetAnchor();
751                         cur.idx() = tabular.getCellNumber(re, ce);
752                         cur.pos() = cur.top().lastpos();
753                         cur.setCursor(cur);
754                         cur.setSelection();
755                         break;
756                 } else {
757                         cell(cur.idx())->dispatch(cur, cmd);
758                         break;
759                 }
760         default:
761                 // we try to handle this event in the insets dispatch function.
762                 cell(cur.idx())->dispatch(cur, cmd);
763                 break;
764         }
765
766         resetPos(cur);
767         InsetTabularMailer(*this).updateDialog(&cur.bv());
768 }
769
770
771 // function sets an object as defined in func_status.h:
772 // states OK, Unknown, Disabled, On, Off.
773 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
774         FuncStatus & status) const
775 {
776         switch (cmd.action) {
777         case LFUN_TABULAR_FEATURE: {
778                 int action = LyXTabular::LAST_ACTION;
779                 int i = 0;
780                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
781                         string const tmp = tabularFeature[i].feature;
782                         if (tmp == cmd.argument.substr(0, tmp.length())) {
783                                 action = tabularFeature[i].action;
784                                 break;
785                         }
786                 }
787                 if (action == LyXTabular::LAST_ACTION) {
788                         status.clear();
789                         status.unknown(true);
790                         return true;
791                 }
792
793                 string const argument
794                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
795
796                 row_type sel_row_start = 0;
797                 row_type sel_row_end = 0;
798                 col_type dummy;
799                 LyXTabular::ltType dummyltt;
800                 bool flag = true;
801
802                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
803
804                 switch (action) {
805                 case LyXTabular::SET_PWIDTH:
806                 case LyXTabular::SET_MPWIDTH:
807                 case LyXTabular::SET_SPECIAL_COLUMN:
808                 case LyXTabular::SET_SPECIAL_MULTI:
809                 case LyXTabular::APPEND_ROW:
810                 case LyXTabular::APPEND_COLUMN:
811                 case LyXTabular::DELETE_ROW:
812                 case LyXTabular::DELETE_COLUMN:
813                 case LyXTabular::SET_ALL_LINES:
814                 case LyXTabular::UNSET_ALL_LINES:
815                         status.clear();
816                         return true;
817
818                 case LyXTabular::MULTICOLUMN:
819                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
820                         break;
821
822                 case LyXTabular::M_TOGGLE_LINE_TOP:
823                         flag = false;
824                 case LyXTabular::TOGGLE_LINE_TOP:
825                         status.setOnOff(tabular.topLine(cur.idx(), flag));
826                         break;
827
828                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
829                         flag = false;
830                 case LyXTabular::TOGGLE_LINE_BOTTOM:
831                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
832                         break;
833
834                 case LyXTabular::M_TOGGLE_LINE_LEFT:
835                         flag = false;
836                 case LyXTabular::TOGGLE_LINE_LEFT:
837                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
838                         break;
839
840                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
841                         flag = false;
842                 case LyXTabular::TOGGLE_LINE_RIGHT:
843                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
844                         break;
845
846                 case LyXTabular::M_ALIGN_LEFT:
847                         flag = false;
848                 case LyXTabular::ALIGN_LEFT:
849                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
850                         break;
851
852                 case LyXTabular::M_ALIGN_RIGHT:
853                         flag = false;
854                 case LyXTabular::ALIGN_RIGHT:
855                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
856                         break;
857
858                 case LyXTabular::M_ALIGN_CENTER:
859                         flag = false;
860                 case LyXTabular::ALIGN_CENTER:
861                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
862                         break;
863
864                 case LyXTabular::ALIGN_BLOCK:
865                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
866                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
867                         break;
868
869                 case LyXTabular::M_VALIGN_TOP:
870                         flag = false;
871                 case LyXTabular::VALIGN_TOP:
872                         status.setOnOff(
873                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
874                         break;
875
876                 case LyXTabular::M_VALIGN_BOTTOM:
877                         flag = false;
878                 case LyXTabular::VALIGN_BOTTOM:
879                         status.setOnOff(
880                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
881                         break;
882
883                 case LyXTabular::M_VALIGN_MIDDLE:
884                         flag = false;
885                 case LyXTabular::VALIGN_MIDDLE:
886                         status.setOnOff(
887                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
888                         break;
889
890                 case LyXTabular::SET_LONGTABULAR:
891                         status.setOnOff(tabular.isLongTabular());
892                         break;
893
894                 case LyXTabular::UNSET_LONGTABULAR:
895                         status.setOnOff(!tabular.isLongTabular());
896                         break;
897
898                 case LyXTabular::SET_ROTATE_TABULAR:
899                         status.setOnOff(tabular.getRotateTabular());
900                         break;
901
902                 case LyXTabular::UNSET_ROTATE_TABULAR:
903                         status.setOnOff(!tabular.getRotateTabular());
904                         break;
905
906                 case LyXTabular::SET_ROTATE_CELL:
907                         status.setOnOff(tabular.getRotateCell(cur.idx()));
908                         break;
909
910                 case LyXTabular::UNSET_ROTATE_CELL:
911                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
912                         break;
913
914                 case LyXTabular::SET_USEBOX:
915                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
916                         break;
917
918                 case LyXTabular::SET_LTFIRSTHEAD:
919                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
920                         break;
921
922                 case LyXTabular::UNSET_LTFIRSTHEAD:
923                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
924                         break;
925
926                 case LyXTabular::SET_LTHEAD:
927                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
928                         break;
929
930                 case LyXTabular::UNSET_LTHEAD:
931                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
932                         break;
933
934                 case LyXTabular::SET_LTFOOT:
935                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
936                         break;
937
938                 case LyXTabular::UNSET_LTFOOT:
939                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
940                         break;
941
942                 case LyXTabular::SET_LTLASTFOOT:
943                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
944                         break;
945
946                 case LyXTabular::UNSET_LTLASTFOOT:
947                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
948                         break;
949
950                 case LyXTabular::SET_LTNEWPAGE:
951                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
952                         break;
953
954                 default:
955                         status.clear();
956                         status.enabled(false);
957                         break;
958                 }
959                 return true;
960         }
961
962         // These are only enabled inside tabular
963         case LFUN_CELL_BACKWARD:
964         case LFUN_CELL_FORWARD:
965                 status.enabled(true);
966                 return true;
967
968         // disable these with multiple cells selected
969         case LFUN_INSET_INSERT:
970         case LFUN_TABULAR_INSERT:
971         case LFUN_CHARSTYLE_INSERT:
972         case LFUN_FLOAT_INSERT:
973         case LFUN_FLOAT_WIDE_INSERT:
974         case LFUN_FOOTNOTE_INSERT:
975         case LFUN_MARGINALNOTE_INSERT:
976         case LFUN_MATH_INSERT:
977         case LFUN_MATH_MODE:
978         case LFUN_MATH_MUTATE:
979         case LFUN_MATH_DISPLAY:
980         case LFUN_NOTE_INSERT:
981         case LFUN_OPTIONAL_INSERT:
982         case LFUN_BOX_INSERT:
983         case LFUN_BRANCH_INSERT:
984         case LFUN_WRAP_INSERT:
985         case LFUN_ERT_INSERT: {
986                 if (tablemode(cur)) {
987                         status.enabled(false);
988                         return true;
989                 } else
990                         return cell(cur.idx())->getStatus(cur, cmd, status);
991         }
992
993         // disable in non-fixed-width cells
994         case LFUN_BREAK_LINE:
995         case LFUN_BREAK_PARAGRAPH:
996         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
997         case LFUN_BREAK_PARAGRAPH_SKIP: {
998                 if (tabular.getPWidth(cur.idx()).zero()) {
999                         status.enabled(false);
1000                         return true;
1001                 } else
1002                         return cell(cur.idx())->getStatus(cur, cmd, status);
1003         }
1004
1005         case LFUN_PASTE:
1006                 if (tabularStackDirty()) {
1007                         status.enabled(true);
1008                         return true;
1009                 }
1010
1011         case LFUN_INSET_MODIFY:
1012                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1013                         status.enabled(true);
1014                         return true;
1015                 }
1016                 // Fall through
1017
1018         default:
1019                 // we try to handle this event in the insets dispatch function.
1020                 return cell(cur.idx())->getStatus(cur, cmd, status);
1021         }
1022 }
1023
1024
1025 int InsetTabular::latex(Buffer const & buf, ostream & os,
1026                         OutputParams const & runparams) const
1027 {
1028         return tabular.latex(buf, os, runparams);
1029 }
1030
1031
1032 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1033                         OutputParams const & runparams) const
1034 {
1035         int const dp = runparams.linelen ? runparams.depth : 0;
1036         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1037 }
1038
1039
1040 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1041                            OutputParams const & runparams) const
1042 {
1043         return tabular.linuxdoc(buf,os, runparams);
1044 }
1045
1046
1047 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1048                           OutputParams const & runparams) const
1049 {
1050         int ret = 0;
1051         InsetBase * master = 0;
1052
1053 #ifdef WITH_WARNINGS
1054 #warning Why not pass a proper DocIterator here?
1055 #endif
1056 #if 0
1057         // if the table is inside a float it doesn't need the informaltable
1058         // wrapper. Search for it.
1059         for (master = owner(); master; master = master->owner())
1060                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1061                         break;
1062 #endif
1063
1064         if (!master) {
1065                 os << "<informaltable>";
1066                 ++ret;
1067         }
1068         ret += tabular.docbook(buf, os, runparams);
1069         if (!master) {
1070                 os << "</informaltable>";
1071                 ++ret;
1072         }
1073         return ret;
1074 }
1075
1076
1077 void InsetTabular::validate(LaTeXFeatures & features) const
1078 {
1079         tabular.validate(features);
1080 }
1081
1082
1083 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1084 {
1085         return tabular.getCellInset(idx);
1086 }
1087
1088
1089 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1090 {
1091         return tabular.getCellInset(idx);
1092 }
1093
1094
1095 void InsetTabular::cursorPos
1096         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1097 {
1098         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1099
1100         // y offset     correction
1101         int const row = tabular.row_of_cell(sl.idx());
1102         for (int i = 0; i <= row; ++i) {
1103                 if (i != 0) {
1104                         y += tabular.getAscentOfRow(i);
1105                         y += tabular.getAdditionalHeight(i);
1106                 }
1107                 if (i != row)
1108                         y += tabular.getDescentOfRow(i);
1109         }
1110
1111         // x offset correction
1112         int const col = tabular.column_of_cell(sl.idx());
1113         int idx = tabular.getCellNumber(row, 0);
1114         for (int j = 0; j < col; ++j) {
1115                 if (tabular.isPartOfMultiColumn(row, j))
1116                         continue;
1117                 x += tabular.getWidthOfColumn(idx);
1118                 ++idx;
1119         }
1120         x += tabular.getBeginningOfTextInCell(idx);
1121         x += ADD_TO_TABULAR_WIDTH;
1122         x += scx_;
1123 }
1124
1125
1126 int InsetTabular::dist(idx_type const cell, int x, int y) const
1127 {
1128         int xx = 0;
1129         int yy = 0;
1130         InsetBase const & inset = *tabular.getCellInset(cell);
1131         Point o = theCoords.getInsets().xy(&inset);
1132         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1133         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1134         int const ybeg = o.y_ - inset.ascent();
1135         row_type const row = tabular.row_of_cell(cell);
1136         int const rowheight = tabular.getAscentOfRow(row)
1137                         + tabular.getDescentOfRow(row);
1138         int const yend = ybeg + rowheight;
1139
1140         if (x < xbeg)
1141                 xx = xbeg - x;
1142         else if (x > xend)
1143                 xx = x - xend;
1144
1145         if (y < ybeg)
1146                 yy = ybeg - y;
1147         else if (y > yend)
1148                 yy = y - yend;
1149
1150         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1151         //       << " ybeg=" << ybeg << " yend=" << yend
1152         //       << " xx=" << xx << " yy=" << yy
1153         //       << " dist=" << xx + yy << endl;
1154         return xx + yy;
1155 }
1156
1157
1158 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1159 {
1160         //lyxerr << "InsetTabular::editXY: " << this << endl;
1161         cur.selection() = false;
1162         cur.push(*this);
1163         cur.idx() = getNearestCell(x, y);
1164         resetPos(cur);
1165         return cell(cur.idx())->text_.editXY(cur, x, y);
1166 }
1167
1168
1169 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1170 {
1171         cur.idx() = getNearestCell(x, y);
1172         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1173 }
1174
1175
1176 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1177 {
1178         idx_type idx_min = 0;
1179         int dist_min = std::numeric_limits<int>::max();
1180         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1181                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1182                         int const d = dist(i, x, y);
1183                         if (d < dist_min) {
1184                                 dist_min = d;
1185                                 idx_min = i;
1186                         }
1187                 }
1188         }
1189         return idx_min;
1190 }
1191
1192
1193 int InsetTabular::getCellXPos(idx_type const cell) const
1194 {
1195         idx_type c = cell;
1196
1197         for (; !tabular.isFirstCellInRow(c); --c)
1198                 ;
1199         int lx = tabular.getWidthOfColumn(cell);
1200         for (; c < cell; ++c)
1201                 lx += tabular.getWidthOfColumn(c);
1202
1203         return lx - tabular.getWidthOfColumn(cell);
1204 }
1205
1206
1207 void InsetTabular::resetPos(LCursor & cur) const
1208 {
1209         BufferView & bv = cur.bv();
1210         int const maxwidth = bv.workWidth();
1211
1212         if (&cur.inset() != this) {
1213                 scx_ = 0;
1214         } else {
1215                 int const X1 = 0;
1216                 int const X2 = maxwidth;
1217                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1218                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1219                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1220
1221                 if (x1 < X1)
1222                         scx_ = X1 + 20 - x1;
1223                 else if (x2 > X2)
1224                         scx_ = X2 - 20 - x2;
1225                 else
1226                         scx_ = 0;
1227         }
1228
1229         cur.needsUpdate();
1230
1231         InsetTabularMailer(*this).updateDialog(&bv);
1232 }
1233
1234
1235 void InsetTabular::moveNextCell(LCursor & cur)
1236 {
1237         if (isRightToLeft(cur)) {
1238                 if (tabular.isFirstCellInRow(cur.idx())) {
1239                         row_type const row = tabular.row_of_cell(cur.idx());
1240                         if (row == tabular.rows() - 1)
1241                                 return;
1242                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1243                 } else {
1244                         if (cur.idx() == 0)
1245                                 return;
1246                         --cur.idx();
1247                 }
1248         } else {
1249                 if (tabular.isLastCell(cur.idx()))
1250                         return;
1251                 ++cur.idx();
1252         }
1253         cur.pit() = 0;
1254         cur.pos() = 0;
1255         resetPos(cur);
1256 }
1257
1258
1259 void InsetTabular::movePrevCell(LCursor & cur)
1260 {
1261         if (isRightToLeft(cur)) {
1262                 if (tabular.isLastCellInRow(cur.idx())) {
1263                         row_type const row = tabular.row_of_cell(cur.idx());
1264                         if (row == 0)
1265                                 return;
1266                         cur.idx() = tabular.getFirstCellInRow(row);
1267                         cur.idx() = tabular.getCellAbove(cur.idx());
1268                 } else {
1269                         if (tabular.isLastCell(cur.idx()))
1270                                 return;
1271                         ++cur.idx();
1272                 }
1273         } else {
1274                 if (cur.idx() == 0) // first cell
1275                         return;
1276                 --cur.idx();
1277         }
1278         cur.pit() = cur.lastpit();
1279         cur.pos() = cur.lastpos();
1280         resetPos(cur);
1281 }
1282
1283
1284 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1285 {
1286         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1287
1288         int i = 0;
1289         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1290                 string const tmp = tabularFeature[i].feature;
1291
1292                 if (tmp == what.substr(0, tmp.length())) {
1293                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1294                         //tabularFeatures[i].feature.length())) {
1295                         action = tabularFeature[i].action;
1296                         break;
1297                 }
1298         }
1299         if (action == LyXTabular::LAST_ACTION)
1300                 return false;
1301
1302         string const val =
1303                 ltrim(what.substr(tabularFeature[i].feature.length()));
1304         tabularFeatures(cur, action, val);
1305         return true;
1306 }
1307
1308
1309 namespace {
1310
1311 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1312                           string const & special, bool & flag)
1313 {
1314         if (special == "dl_above") {
1315                 ltt.topDL = flag;
1316                 ltt.set = false;
1317         } else if (special == "dl_below") {
1318                 ltt.bottomDL = flag;
1319                 ltt.set = false;
1320         } else if (special == "empty") {
1321                 ltt.empty = flag;
1322                 ltt.set = false;
1323         } else if (flag) {
1324                 ltt.empty = false;
1325                 ltt.set = true;
1326         }
1327 }
1328
1329 } // anon namespace
1330
1331
1332 void InsetTabular::tabularFeatures(LCursor & cur,
1333         LyXTabular::Feature feature, string const & value)
1334 {
1335         BufferView & bv = cur.bv();
1336         col_type sel_col_start;
1337         col_type sel_col_end;
1338         row_type sel_row_start;
1339         row_type sel_row_end;
1340         bool setLines = false;
1341         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1342         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1343
1344         switch (feature) {
1345
1346         case LyXTabular::M_ALIGN_LEFT:
1347         case LyXTabular::ALIGN_LEFT:
1348                 setAlign = LYX_ALIGN_LEFT;
1349                 break;
1350
1351         case LyXTabular::M_ALIGN_RIGHT:
1352         case LyXTabular::ALIGN_RIGHT:
1353                 setAlign = LYX_ALIGN_RIGHT;
1354                 break;
1355
1356         case LyXTabular::M_ALIGN_CENTER:
1357         case LyXTabular::ALIGN_CENTER:
1358                 setAlign = LYX_ALIGN_CENTER;
1359                 break;
1360
1361         case LyXTabular::ALIGN_BLOCK:
1362                 setAlign = LYX_ALIGN_BLOCK;
1363                 break;
1364
1365         case LyXTabular::M_VALIGN_TOP:
1366         case LyXTabular::VALIGN_TOP:
1367                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1368                 break;
1369
1370         case LyXTabular::M_VALIGN_BOTTOM:
1371         case LyXTabular::VALIGN_BOTTOM:
1372                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1373                 break;
1374
1375         case LyXTabular::M_VALIGN_MIDDLE:
1376         case LyXTabular::VALIGN_MIDDLE:
1377                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1378                 break;
1379
1380         default:
1381                 break;
1382         }
1383
1384         recordUndoInset(cur, Undo::ATOMIC);
1385
1386         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1387         row_type const row = tabular.row_of_cell(cur.idx());
1388         col_type const column = tabular.column_of_cell(cur.idx());
1389         bool flag = true;
1390         LyXTabular::ltType ltt;
1391
1392         switch (feature) {
1393
1394         case LyXTabular::SET_PWIDTH: {
1395                 LyXLength const len(value);
1396                 tabular.setColumnPWidth(cur, cur.idx(), len);
1397                 if (len.zero()
1398                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1399                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1400                 break;
1401         }
1402
1403         case LyXTabular::SET_MPWIDTH:
1404                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1405                 break;
1406
1407         case LyXTabular::SET_SPECIAL_COLUMN:
1408         case LyXTabular::SET_SPECIAL_MULTI:
1409                 tabular.setAlignSpecial(cur.idx(),value,feature);
1410                 break;
1411
1412         case LyXTabular::APPEND_ROW:
1413                 // append the row into the tabular
1414                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1415                 break;
1416
1417         case LyXTabular::APPEND_COLUMN:
1418                 // append the column into the tabular
1419                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1420                 cur.idx() = tabular.getCellNumber(row, column);
1421                 break;
1422
1423         case LyXTabular::DELETE_ROW:
1424                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1425                         tabular.deleteRow(sel_row_start);
1426                 if (sel_row_start >= tabular.rows())
1427                         --sel_row_start;
1428                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1429                 cur.pit() = 0;
1430                 cur.pos() = 0;
1431                 cur.selection() = false;
1432                 break;
1433
1434         case LyXTabular::DELETE_COLUMN:
1435                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1436                         tabular.deleteColumn(sel_col_start);
1437                 if (sel_col_start >= tabular.columns())
1438                         --sel_col_start;
1439                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1440                 cur.pit() = 0;
1441                 cur.pos() = 0;
1442                 cur.selection() = false;
1443                 break;
1444
1445         case LyXTabular::M_TOGGLE_LINE_TOP:
1446                 flag = false;
1447         case LyXTabular::TOGGLE_LINE_TOP: {
1448                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1449                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1450                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1451                                 tabular.setTopLine(
1452                                         tabular.getCellNumber(i, j),
1453                                         lineSet, flag);
1454                 break;
1455         }
1456
1457         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1458                 flag = false;
1459         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1460                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1461                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1462                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1463                                 tabular.setBottomLine(
1464                                         tabular.getCellNumber(i, j),
1465                                         lineSet,
1466                                         flag);
1467                 break;
1468         }
1469
1470         case LyXTabular::M_TOGGLE_LINE_LEFT:
1471                 flag = false;
1472         case LyXTabular::TOGGLE_LINE_LEFT: {
1473                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1474                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1475                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1476                                 tabular.setLeftLine(
1477                                         tabular.getCellNumber(i,j),
1478                                         lineSet,
1479                                         flag);
1480                 break;
1481         }
1482
1483         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1484                 flag = false;
1485         case LyXTabular::TOGGLE_LINE_RIGHT: {
1486                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1487                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1488                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1489                                 tabular.setRightLine(
1490                                         tabular.getCellNumber(i,j),
1491                                         lineSet,
1492                                         flag);
1493                 break;
1494         }
1495
1496         case LyXTabular::M_ALIGN_LEFT:
1497         case LyXTabular::M_ALIGN_RIGHT:
1498         case LyXTabular::M_ALIGN_CENTER:
1499                 flag = false;
1500         case LyXTabular::ALIGN_LEFT:
1501         case LyXTabular::ALIGN_RIGHT:
1502         case LyXTabular::ALIGN_CENTER:
1503         case LyXTabular::ALIGN_BLOCK:
1504                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1505                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1506                                 tabular.setAlignment(
1507                                         tabular.getCellNumber(i, j),
1508                                         setAlign,
1509                                         flag);
1510                 break;
1511
1512         case LyXTabular::M_VALIGN_TOP:
1513         case LyXTabular::M_VALIGN_BOTTOM:
1514         case LyXTabular::M_VALIGN_MIDDLE:
1515                 flag = false;
1516         case LyXTabular::VALIGN_TOP:
1517         case LyXTabular::VALIGN_BOTTOM:
1518         case LyXTabular::VALIGN_MIDDLE:
1519                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1520                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1521                                 tabular.setVAlignment(
1522                                         tabular.getCellNumber(i, j),
1523                                         setVAlign, flag);
1524                 break;
1525
1526         case LyXTabular::MULTICOLUMN: {
1527                 if (sel_row_start != sel_row_end) {
1528 #ifdef WITH_WARNINGS
1529 #warning Need I say it ? This is horrible.
1530 #endif
1531                         Alert::error(_("Error setting multicolumn"),
1532                                    _("You cannot set multicolumn vertically."));
1533                         return;
1534                 }
1535                 if (!cur.selection()) {
1536                         // just multicol for one single cell
1537                         // check whether we are completely in a multicol
1538                         if (tabular.isMultiColumn(cur.idx()))
1539                                 tabular.unsetMultiColumn(cur.idx());
1540                         else
1541                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1542                         break;
1543                 }
1544                 // we have a selection so this means we just add all this
1545                 // cells to form a multicolumn cell
1546                 idx_type const s_start = cur.selBegin().idx();
1547                 idx_type const s_end = cur.selEnd().idx();
1548                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1549                 cur.idx() = s_start;
1550                 cur.pit() = 0;
1551                 cur.pos() = 0;
1552                 cur.selection() = false;
1553                 break;
1554         }
1555
1556         case LyXTabular::SET_ALL_LINES:
1557                 setLines = true;
1558         case LyXTabular::UNSET_ALL_LINES:
1559                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1560                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1561                                 tabular.setAllLines(
1562                                         tabular.getCellNumber(i,j), setLines);
1563                 break;
1564
1565         case LyXTabular::SET_LONGTABULAR:
1566                 tabular.setLongTabular(true);
1567                 break;
1568
1569         case LyXTabular::UNSET_LONGTABULAR:
1570                 tabular.setLongTabular(false);
1571                 break;
1572
1573         case LyXTabular::SET_ROTATE_TABULAR:
1574                 tabular.setRotateTabular(true);
1575                 break;
1576
1577         case LyXTabular::UNSET_ROTATE_TABULAR:
1578                 tabular.setRotateTabular(false);
1579                 break;
1580
1581         case LyXTabular::SET_ROTATE_CELL:
1582                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1583                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1584                                 tabular.setRotateCell(
1585                                         tabular.getCellNumber(i, j), true);
1586                 break;
1587
1588         case LyXTabular::UNSET_ROTATE_CELL:
1589                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1590                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1591                                 tabular.setRotateCell(
1592                                         tabular.getCellNumber(i, j), false);
1593                 break;
1594
1595         case LyXTabular::SET_USEBOX: {
1596                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1597                 if (val == tabular.getUsebox(cur.idx()))
1598                         val = LyXTabular::BOX_NONE;
1599                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1600                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1601                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1602                 break;
1603         }
1604
1605         case LyXTabular::UNSET_LTFIRSTHEAD:
1606                 flag = false;
1607         case LyXTabular::SET_LTFIRSTHEAD:
1608                 tabular.getRowOfLTFirstHead(row, ltt);
1609                 checkLongtableSpecial(ltt, value, flag);
1610                 tabular.setLTHead(row, flag, ltt, true);
1611                 break;
1612
1613         case LyXTabular::UNSET_LTHEAD:
1614                 flag = false;
1615         case LyXTabular::SET_LTHEAD:
1616                 tabular.getRowOfLTHead(row, ltt);
1617                 checkLongtableSpecial(ltt, value, flag);
1618                 tabular.setLTHead(row, flag, ltt, false);
1619                 break;
1620
1621         case LyXTabular::UNSET_LTFOOT:
1622                 flag = false;
1623         case LyXTabular::SET_LTFOOT:
1624                 tabular.getRowOfLTFoot(row, ltt);
1625                 checkLongtableSpecial(ltt, value, flag);
1626                 tabular.setLTFoot(row, flag, ltt, false);
1627                 break;
1628
1629         case LyXTabular::UNSET_LTLASTFOOT:
1630                 flag = false;
1631         case LyXTabular::SET_LTLASTFOOT:
1632                 tabular.getRowOfLTLastFoot(row, ltt);
1633                 checkLongtableSpecial(ltt, value, flag);
1634                 tabular.setLTFoot(row, flag, ltt, true);
1635                 break;
1636
1637         case LyXTabular::SET_LTNEWPAGE:
1638                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1639                 break;
1640
1641         // dummy stuff just to avoid warnings
1642         case LyXTabular::LAST_ACTION:
1643                 break;
1644         }
1645
1646         InsetTabularMailer(*this).updateDialog(&bv);
1647 }
1648
1649
1650 bool InsetTabular::showInsetDialog(BufferView * bv) const
1651 {
1652         InsetTabularMailer(*this).showDialog(bv);
1653         return true;
1654 }
1655
1656
1657 void InsetTabular::openLayoutDialog(BufferView * bv) const
1658 {
1659         InsetTabularMailer(*this).showDialog(bv);
1660 }
1661
1662
1663 bool InsetTabular::copySelection(LCursor & cur)
1664 {
1665         if (!cur.selection())
1666                 return false;
1667
1668         row_type rs, re;
1669         col_type cs, ce;
1670         getSelection(cur, rs, re, cs, ce);
1671
1672         paste_tabular.reset(new LyXTabular(tabular));
1673
1674         for (row_type i = 0; i < rs; ++i)
1675                 paste_tabular->deleteRow(0);
1676
1677         row_type const rows = re - rs + 1;
1678         while (paste_tabular->rows() > rows)
1679                 paste_tabular->deleteRow(rows);
1680
1681         paste_tabular->setTopLine(0, true, true);
1682         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1683                                      true, true);
1684
1685         for (col_type i = 0; i < cs; ++i)
1686                 paste_tabular->deleteColumn(0);
1687
1688         col_type const columns = ce - cs + 1;
1689         while (paste_tabular->columns() > columns)
1690                 paste_tabular->deleteColumn(columns);
1691
1692         paste_tabular->setLeftLine(0, true, true);
1693         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1694                                     true, true);
1695
1696         ostringstream os;
1697         OutputParams const runparams;
1698         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1699         cur.bv().stuffClipboard(os.str());
1700         // mark tabular stack dirty
1701         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1702         // when we (hopefully) have a one-for-all paste mechanism.
1703         lyx::cap::dirtyTabularStack(true);
1704
1705         return true;
1706 }
1707
1708
1709 bool InsetTabular::pasteSelection(LCursor & cur)
1710 {
1711         if (!paste_tabular)
1712                 return false;
1713         col_type const actcol = tabular.column_of_cell(cur.idx());
1714         row_type const actrow = tabular.row_of_cell(cur.idx());
1715         for (row_type r1 = 0, r2 = actrow;
1716              r1 < paste_tabular->rows() && r2 < tabular.rows();
1717              ++r1, ++r2) {
1718                 for (col_type c1 = 0, c2 = actcol;
1719                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1720                     ++c1, ++c2) {
1721                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1722                             tabular.isPartOfMultiColumn(r2, c2))
1723                                 continue;
1724                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1725                                 --c2;
1726                                 continue;
1727                         }
1728                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1729                                 --c1;
1730                                 continue;
1731                         }
1732                         shared_ptr<InsetText> inset(
1733                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1734                         tabular.setCellInset(r2, c2, inset);
1735                         inset->markNew();
1736                         cur.pos() = 0;
1737                 }
1738         }
1739         return true;
1740 }
1741
1742
1743 void InsetTabular::cutSelection(LCursor & cur)
1744 {
1745         if (!cur.selection())
1746                 return;
1747
1748         row_type rs, re;
1749         col_type cs, ce;
1750         getSelection(cur, rs, re, cs, ce);
1751         for (row_type i = rs; i <= re; ++i) {
1752                 for (col_type j = cs; j <= ce; ++j) {
1753                         shared_ptr<InsetText> t
1754                                 = cell(tabular.getCellNumber(i, j));
1755                         if (cur.buffer().params().tracking_changes)
1756                                 t->markErased(true);
1757                         else
1758                                 t->clear();
1759                 }
1760         }
1761
1762         // cursor position might be invalid now
1763         cur.pos() = cur.lastpos();
1764         cur.clearSelection();
1765 }
1766
1767
1768 bool InsetTabular::isRightToLeft(LCursor & cur) const
1769 {
1770         BOOST_ASSERT(cur.depth() > 1);
1771         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1772         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1773         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1774                                          parentpos).language()->rightToLeft();
1775 }
1776
1777
1778 void InsetTabular::getSelection(LCursor & cur,
1779         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1780 {
1781         CursorSlice const & beg = cur.selBegin();
1782         CursorSlice const & end = cur.selEnd();
1783         cs = tabular.column_of_cell(beg.idx());
1784         ce = tabular.column_of_cell(end.idx());
1785         if (cs > ce) {
1786                 ce = cs;
1787                 cs = tabular.column_of_cell(end.idx());
1788         } else {
1789                 ce = tabular.right_column_of_cell(end.idx());
1790         }
1791
1792         rs = tabular.row_of_cell(beg.idx());
1793         re = tabular.row_of_cell(end.idx());
1794         if (rs > re)
1795                 swap(rs, re);
1796 }
1797
1798
1799 LyXText * InsetTabular::getText(int idx) const
1800 {
1801         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1802 }
1803
1804
1805 void InsetTabular::markErased(bool erased)
1806 {
1807         for (idx_type idx = 0; idx < nargs(); ++idx)
1808                 cell(idx)->markErased(erased);
1809 }
1810
1811
1812 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1813 {
1814         return tabular.getPWidth(cell).zero();
1815 }
1816
1817
1818 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1819                                      bool usePaste)
1820 {
1821         if (buf.length() <= 0)
1822                 return true;
1823
1824         col_type cols = 1;
1825         row_type rows = 1;
1826         col_type maxCols = 1;
1827         string::size_type const len = buf.length();
1828         string::size_type p = 0;
1829
1830         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1831                 switch (buf[p]) {
1832                 case '\t':
1833                         ++cols;
1834                         break;
1835                 case '\n':
1836                         if (p + 1 < len)
1837                                 ++rows;
1838                         maxCols = max(cols, maxCols);
1839                         cols = 1;
1840                         break;
1841                 }
1842                 ++p;
1843         }
1844         maxCols = max(cols, maxCols);
1845         LyXTabular * loctab;
1846         idx_type cell = 0;
1847         col_type ocol = 0;
1848         row_type row = 0;
1849         if (usePaste) {
1850                 paste_tabular.reset(
1851                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1852                 loctab = paste_tabular.get();
1853                 cols = 0;
1854                 dirtyTabularStack(true);
1855         } else {
1856                 loctab = &tabular;
1857                 cell = bv.cursor().idx();
1858                 ocol = tabular.column_of_cell(cell);
1859                 row = tabular.row_of_cell(cell);
1860         }
1861
1862         string::size_type op = 0;
1863         idx_type const cells = loctab->getNumberOfCells();
1864         p = 0;
1865         cols = ocol;
1866         rows = loctab->rows();
1867         col_type const columns = loctab->columns();
1868
1869         while (cell < cells && p < len && row < rows &&
1870                (p = buf.find_first_of("\t\n", p)) != string::npos)
1871         {
1872                 if (p >= len)
1873                         break;
1874                 switch (buf[p]) {
1875                 case '\t':
1876                         // we can only set this if we are not too far right
1877                         if (cols < columns) {
1878                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1879                                 inset->setViewCache(&bv);
1880                                 Paragraph & par = inset->text_.getPar(0);
1881                                 LyXFont const font = inset->text_.getFont(par, 0);
1882                                 inset->setText(buf.substr(op, p - op), font);
1883                                 ++cols;
1884                                 ++cell;
1885                         }
1886                         break;
1887                 case '\n':
1888                         // we can only set this if we are not too far right
1889                         if (cols < columns) {
1890                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1891                                 inset->setViewCache(&bv);
1892                                 Paragraph & par = inset->text_.getPar(0);
1893                                 LyXFont const font = inset->text_.getFont(par, 0);
1894                                 inset->setText(buf.substr(op, p - op), font);
1895                         }
1896                         cols = ocol;
1897                         ++row;
1898                         if (row < rows)
1899                                 cell = loctab->getCellNumber(row, cols);
1900                         break;
1901                 }
1902                 ++p;
1903                 op = p;
1904         }
1905         // check for the last cell if there is no trailing '\n'
1906         if (cell < cells && op < len) {
1907                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1908                 inset->setViewCache(&bv);
1909                 Paragraph & par = inset->text_.getPar(0);
1910                 LyXFont const font = inset->text_.getFont(par, 0);
1911                 inset->setText(buf.substr(op, len - op), font);
1912         }
1913         return true;
1914 }
1915
1916
1917 void InsetTabular::addPreview(PreviewLoader & loader) const
1918 {
1919         row_type const rows = tabular.rows();
1920         col_type const columns = tabular.columns();
1921         for (row_type i = 0; i < rows; ++i) {
1922                 for (col_type j = 0; j < columns; ++j)
1923                         tabular.getCellInset(i, j)->addPreview(loader);
1924         }
1925 }
1926
1927
1928 bool InsetTabular::tablemode(LCursor & cur) const
1929 {
1930         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1931 }
1932
1933
1934
1935
1936
1937 string const InsetTabularMailer::name_("tabular");
1938
1939 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1940         : inset_(const_cast<InsetTabular &>(inset))
1941 {}
1942
1943
1944 string const InsetTabularMailer::inset2string(Buffer const &) const
1945 {
1946         return params2string(inset_);
1947 }
1948
1949
1950 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1951 {
1952         istringstream data(in);
1953         LyXLex lex(0,0);
1954         lex.setStream(data);
1955
1956         if (in.empty())
1957                 return;
1958
1959         string token;
1960         lex >> token;
1961         if (!lex || token != name_)
1962                 return print_mailer_error("InsetTabularMailer", in, 1,
1963                                           name_);
1964
1965         // This is part of the inset proper that is usually swallowed
1966         // by Buffer::readInset
1967         lex >> token;
1968         if (!lex || token != "Tabular")
1969                 return print_mailer_error("InsetTabularMailer", in, 2,
1970                                           "Tabular");
1971
1972         Buffer const & buffer = inset.buffer();
1973         inset.read(buffer, lex);
1974 }
1975
1976
1977 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1978 {
1979         ostringstream data;
1980         data << name_ << ' ';
1981         inset.write(inset.buffer(), data);
1982         data << "\\end_inset\n";
1983         return data.str();
1984 }