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