]> 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                 break;
477
478         case LFUN_MOUSE_MOTION:
479                 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
480                 if (cmd.button() == mouse_button::button1) {
481                         // only accept motions to places not deeper nested than the real anchor
482                         if (bvcur.anchor_.hasPart(cur)) {
483                                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
484                                 bvcur.setCursor(cur);
485                                 bvcur.selection() = true;
486                         } else
487                                 cur.undispatched();
488                 }
489                 break;
490
491         case LFUN_MOUSE_RELEASE:
492                 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
493                 if (cmd.button() == mouse_button::button3) {
494                         bvcur.setCursor(cur);
495                         InsetTabularMailer(*this).showDialog(&cur.bv());
496                 }
497                 break;
498
499         case LFUN_CELL_BACKWARD:
500                 movePrevCell(cur);
501                 cur.selection() = false;
502                 break;
503
504         case LFUN_CELL_FORWARD:
505                 moveNextCell(cur);
506                 cur.selection() = false;
507                 break;
508
509         case LFUN_CHAR_FORWARD_SELECT:
510         case LFUN_CHAR_FORWARD:
511                 cell(cur.idx())->dispatch(cur, cmd);
512                 if (!cur.result().dispatched()) {
513                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
514                         if (sl == cur.top())
515                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
516                         else
517                                 cur.dispatched();
518                 }
519                 break;
520
521         case LFUN_CHAR_BACKWARD_SELECT:
522         case LFUN_CHAR_BACKWARD:
523                 cell(cur.idx())->dispatch(cur, cmd);
524                 if (!cur.result().dispatched()) {
525                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
526                         if (sl == cur.top())
527                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
528                         else
529                                 cur.dispatched();
530                 }
531                 break;
532
533         case LFUN_DOWN_SELECT:
534         case LFUN_DOWN:
535                 cell(cur.idx())->dispatch(cur, cmd);
536                 cur.dispatched(); // override the cell's decision
537                 if (sl == cur.top())
538                         // if our LyXText didn't do anything to the cursor
539                         // then we try to put the cursor into the cell below
540                         // setting also the right targetX.
541                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
542                                 cur.idx() = tabular.getCellBelow(cur.idx());
543                                 cur.pit() = 0;
544                                 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
545                                         cur.pit(), 0, cur.targetX());
546                         }
547                 if (sl == cur.top()) {
548                         // we trick it to go to the RIGHT after leaving the
549                         // tabular.
550                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
551                         cur.undispatched();
552                 }
553                 break;
554
555         case LFUN_UP_SELECT:
556         case LFUN_UP:
557                 cell(cur.idx())->dispatch(cur, cmd);
558                 cur.dispatched(); // override the cell's decision
559                 if (sl == cur.top())
560                         // if our LyXText didn't do anything to the cursor
561                         // then we try to put the cursor into the cell above
562                         // setting also the right targetX.
563                         if (tabular.row_of_cell(cur.idx()) != 0) {
564                                 cur.idx() = tabular.getCellAbove(cur.idx());
565                                 cur.pit() = cur.lastpit();
566                                 LyXText const * text = cell(cur.idx())->getText(0);
567                                 cur.pos() = text->x2pos(
568                                         cur.pit(),
569                                         text->paragraphs().back().rows().size()-1,
570                                         cur.targetX());
571                         }
572                 if (sl == cur.top()) {
573                         cmd = FuncRequest(LFUN_FINISHED_UP);
574                         cur.undispatched();
575                 }
576                 break;
577
578 //      case LFUN_SCREEN_DOWN: {
579 //              //if (hasSelection())
580 //              //      cur.selection() = false;
581 //              col_type const col = tabular.column_of_cell(cur.idx());
582 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
583 //              if (t < yo() + tabular.getHeightOfTabular()) {
584 //                      cur.bv().scrollDocView(t);
585 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
586 //              } else {
587 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
588 //              }
589 //              cur.par() = 0;
590 //              cur.pos() = 0;
591 //              break;
592 //      }
593 //
594 //      case LFUN_SCREEN_UP: {
595 //              //if (hasSelection())
596 //              //      cur.selection() = false;
597 //              col_type const col = tabular.column_of_cell(cur.idx());
598 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
599 //              if (yo() < 0) {
600 //                      cur.bv().scrollDocView(t);
601 //                      if (yo() > 0)
602 //                              cur.idx() = col;
603 //                      else
604 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
605 //              } else {
606 //                      cur.idx() = col;
607 //              }
608 //              cur.par() = cur.lastpar();
609 //              cur.pos() = cur.lastpos();
610 //              break;
611 //      }
612
613         case LFUN_LAYOUT_TABULAR:
614                 InsetTabularMailer(*this).showDialog(&cur.bv());
615                 break;
616
617         case LFUN_INSET_DIALOG_UPDATE:
618                 InsetTabularMailer(*this).updateDialog(&cur.bv());
619                 break;
620
621         case LFUN_TABULAR_FEATURE:
622                 if (!tabularFeatures(cur, cmd.argument))
623                         cur.undispatched();
624                 break;
625
626         // insert file functions
627         case LFUN_FILE_INSERT_ASCII_PARA:
628         case LFUN_FILE_INSERT_ASCII: {
629                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
630                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
631                         cur.undispatched();
632                 break;
633         }
634
635         case LFUN_CUT:
636                 if (tablemode(cur)) {
637                         if (copySelection(cur)) {
638                                 recordUndoInset(cur, Undo::DELETE);
639                                 cutSelection(cur);
640                         }
641                 }
642                 else
643                         cell(cur.idx())->dispatch(cur, cmd);
644                 break;
645
646         case LFUN_CHAR_DELETE_BACKWARD:
647         case LFUN_CHAR_DELETE_FORWARD:
648                 if (tablemode(cur)) {
649                         recordUndoInset(cur, Undo::DELETE);
650                         cutSelection(cur);
651                 }
652                 else
653                         cell(cur.idx())->dispatch(cur, cmd);
654                 break;
655
656         case LFUN_COPY:
657                 if (!cur.selection())
658                         break;
659                 if (tablemode(cur)) {
660                         finishUndo();
661                         copySelection(cur);
662                 } else
663                         cell(cur.idx())->dispatch(cur, cmd);
664                 break;
665
666         case LFUN_PRIMARY_SELECTION_PASTE: {
667                 string const clip = cur.bv().getClipboard();
668                 if (clip.empty())
669                         break;
670                 // pass to InsertAsciiString, but
671                 // only if we have multi-cell content
672                 if (clip.find_first_of("\t\n") != string::npos) {
673                         if (insertAsciiString(cur.bv(), clip, false)) {
674                                 // content has been replaced,
675                                 // so cursor might be invalid
676                                 cur.pos() = cur.lastpos();
677                                 bvcur.setCursor(cur);
678                                 break;
679                         }
680                 } else {
681                         // so that the clipboard is used and it goes on
682                         // to default
683                         // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
684                         paste_tabular.reset();
685                         dirtyTabularStack(false);
686                 }
687                 // fall through
688         }
689
690         case LFUN_PASTE:
691                 if (hasPasteBuffer() && tabularStackDirty()) {
692                         recordUndoInset(cur, Undo::INSERT);
693                         pasteSelection(cur);
694                         break;
695                 }
696                 cell(cur.idx())->dispatch(cur, cmd);
697                 // Reset pasted paragraphs:
698                 if (tabular.getPWidth(cur.idx()).zero())
699                         cell(cur.idx())->forceParagraphsToDefault(cur);
700                 break;
701
702         case LFUN_FONT_EMPH:
703         case LFUN_FONT_BOLD:
704         case LFUN_FONT_ROMAN:
705         case LFUN_FONT_NOUN:
706         case LFUN_FONT_ITAL:
707         case LFUN_FONT_FRAK:
708         case LFUN_FONT_CODE:
709         case LFUN_FONT_SANS:
710         case LFUN_FONT_FREE_APPLY:
711         case LFUN_FONT_FREE_UPDATE:
712         case LFUN_FONT_SIZE:
713         case LFUN_FONT_UNDERLINE:
714         case LFUN_LANGUAGE:
715         case LFUN_WORD_CAPITALIZE:
716         case LFUN_WORD_UPCASE:
717         case LFUN_WORD_LOWCASE:
718         case LFUN_CHARS_TRANSPOSE:
719                 if (tablemode(cur)) {
720                         row_type rs, re;
721                         col_type cs, ce;
722                         getSelection(cur, rs, re, cs, ce);
723                         for (row_type i = rs; i <= re; ++i) {
724                                 for (col_type j = cs; j <= ce; ++j) {
725                                         // cursor follows cell:
726                                         cur.idx() = tabular.getCellNumber(i, j);
727                                         // select this cell only:
728                                         cur.pos() = 0;
729                                         cur.resetAnchor();
730                                         cur.pos() = cur.top().lastpos();
731                                         cur.setCursor(cur);
732                                         cur.setSelection();
733                                         cell(cur.idx())->dispatch(cur, cmd);
734                                 }
735                         }
736                         // Restore original selection
737                         cur.idx() = tabular.getCellNumber(rs, cs);
738                         cur.pos() = 0;
739                         cur.resetAnchor();
740                         cur.idx() = tabular.getCellNumber(re, ce);
741                         cur.pos() = cur.top().lastpos();
742                         cur.setCursor(cur);
743                         cur.setSelection();
744                         break;
745                 } else {
746                         cell(cur.idx())->dispatch(cur, cmd);
747                         break;
748                 }
749         default:
750                 // we try to handle this event in the insets dispatch function.
751                 cell(cur.idx())->dispatch(cur, cmd);
752                 break;
753         }
754
755         resetPos(cur);
756         InsetTabularMailer(*this).updateDialog(&cur.bv());
757 }
758
759
760 // function sets an object as defined in func_status.h:
761 // states OK, Unknown, Disabled, On, Off.
762 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
763         FuncStatus & status) const
764 {
765         switch (cmd.action) {
766         case LFUN_TABULAR_FEATURE: {
767                 int action = LyXTabular::LAST_ACTION;
768                 int i = 0;
769                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
770                         string const tmp = tabularFeature[i].feature;
771                         if (tmp == cmd.argument.substr(0, tmp.length())) {
772                                 action = tabularFeature[i].action;
773                                 break;
774                         }
775                 }
776                 if (action == LyXTabular::LAST_ACTION) {
777                         status.clear();
778                         status.unknown(true);
779                         return true;
780                 }
781
782                 string const argument
783                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
784
785                 row_type sel_row_start = 0;
786                 row_type sel_row_end = 0;
787                 col_type dummy;
788                 LyXTabular::ltType dummyltt;
789                 bool flag = true;
790
791                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
792
793                 switch (action) {
794                 case LyXTabular::SET_PWIDTH:
795                 case LyXTabular::SET_MPWIDTH:
796                 case LyXTabular::SET_SPECIAL_COLUMN:
797                 case LyXTabular::SET_SPECIAL_MULTI:
798                 case LyXTabular::APPEND_ROW:
799                 case LyXTabular::APPEND_COLUMN:
800                 case LyXTabular::DELETE_ROW:
801                 case LyXTabular::DELETE_COLUMN:
802                 case LyXTabular::SET_ALL_LINES:
803                 case LyXTabular::UNSET_ALL_LINES:
804                         status.clear();
805                         return true;
806
807                 case LyXTabular::MULTICOLUMN:
808                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
809                         break;
810
811                 case LyXTabular::M_TOGGLE_LINE_TOP:
812                         flag = false;
813                 case LyXTabular::TOGGLE_LINE_TOP:
814                         status.setOnOff(tabular.topLine(cur.idx(), flag));
815                         break;
816
817                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
818                         flag = false;
819                 case LyXTabular::TOGGLE_LINE_BOTTOM:
820                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
821                         break;
822
823                 case LyXTabular::M_TOGGLE_LINE_LEFT:
824                         flag = false;
825                 case LyXTabular::TOGGLE_LINE_LEFT:
826                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
827                         break;
828
829                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
830                         flag = false;
831                 case LyXTabular::TOGGLE_LINE_RIGHT:
832                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
833                         break;
834
835                 case LyXTabular::M_ALIGN_LEFT:
836                         flag = false;
837                 case LyXTabular::ALIGN_LEFT:
838                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
839                         break;
840
841                 case LyXTabular::M_ALIGN_RIGHT:
842                         flag = false;
843                 case LyXTabular::ALIGN_RIGHT:
844                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
845                         break;
846
847                 case LyXTabular::M_ALIGN_CENTER:
848                         flag = false;
849                 case LyXTabular::ALIGN_CENTER:
850                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
851                         break;
852
853                 case LyXTabular::ALIGN_BLOCK:
854                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
855                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
856                         break;
857
858                 case LyXTabular::M_VALIGN_TOP:
859                         flag = false;
860                 case LyXTabular::VALIGN_TOP:
861                         status.setOnOff(
862                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
863                         break;
864
865                 case LyXTabular::M_VALIGN_BOTTOM:
866                         flag = false;
867                 case LyXTabular::VALIGN_BOTTOM:
868                         status.setOnOff(
869                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
870                         break;
871
872                 case LyXTabular::M_VALIGN_MIDDLE:
873                         flag = false;
874                 case LyXTabular::VALIGN_MIDDLE:
875                         status.setOnOff(
876                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
877                         break;
878
879                 case LyXTabular::SET_LONGTABULAR:
880                         status.setOnOff(tabular.isLongTabular());
881                         break;
882
883                 case LyXTabular::UNSET_LONGTABULAR:
884                         status.setOnOff(!tabular.isLongTabular());
885                         break;
886
887                 case LyXTabular::SET_ROTATE_TABULAR:
888                         status.setOnOff(tabular.getRotateTabular());
889                         break;
890
891                 case LyXTabular::UNSET_ROTATE_TABULAR:
892                         status.setOnOff(!tabular.getRotateTabular());
893                         break;
894
895                 case LyXTabular::SET_ROTATE_CELL:
896                         status.setOnOff(tabular.getRotateCell(cur.idx()));
897                         break;
898
899                 case LyXTabular::UNSET_ROTATE_CELL:
900                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
901                         break;
902
903                 case LyXTabular::SET_USEBOX:
904                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
905                         break;
906
907                 case LyXTabular::SET_LTFIRSTHEAD:
908                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
909                         break;
910
911                 case LyXTabular::UNSET_LTFIRSTHEAD:
912                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
913                         break;
914
915                 case LyXTabular::SET_LTHEAD:
916                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
917                         break;
918
919                 case LyXTabular::UNSET_LTHEAD:
920                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
921                         break;
922
923                 case LyXTabular::SET_LTFOOT:
924                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
925                         break;
926
927                 case LyXTabular::UNSET_LTFOOT:
928                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
929                         break;
930
931                 case LyXTabular::SET_LTLASTFOOT:
932                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
933                         break;
934
935                 case LyXTabular::UNSET_LTLASTFOOT:
936                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
937                         break;
938
939                 case LyXTabular::SET_LTNEWPAGE:
940                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
941                         break;
942
943                 default:
944                         status.clear();
945                         status.enabled(false);
946                         break;
947                 }
948                 return true;
949         }
950
951         // These are only enabled inside tabular
952         case LFUN_CELL_BACKWARD:
953         case LFUN_CELL_FORWARD:
954                 status.enabled(true);
955                 return true;
956
957         // disable these with multiple cells selected
958         case LFUN_INSET_INSERT:
959         case LFUN_TABULAR_INSERT:
960         case LFUN_CHARSTYLE_INSERT:
961         case LFUN_FLOAT_INSERT:
962         case LFUN_FLOAT_WIDE_INSERT:
963         case LFUN_FOOTNOTE_INSERT:
964         case LFUN_MARGINALNOTE_INSERT:
965         case LFUN_MATH_INSERT:
966         case LFUN_MATH_MODE:
967         case LFUN_MATH_MUTATE:
968         case LFUN_MATH_DISPLAY:
969         case LFUN_NOTE_INSERT:
970         case LFUN_OPTIONAL_INSERT:
971         case LFUN_BOX_INSERT:
972         case LFUN_BRANCH_INSERT:
973         case LFUN_WRAP_INSERT:
974         case LFUN_ERT_INSERT: {
975                 if (tablemode(cur)) {
976                         status.enabled(false);
977                         return true;
978                 } else
979                         return cell(cur.idx())->getStatus(cur, cmd, status);
980         }
981
982         // disable in non-fixed-width cells
983         case LFUN_BREAK_LINE:
984         case LFUN_BREAK_PARAGRAPH:
985         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
986         case LFUN_BREAK_PARAGRAPH_SKIP: {
987                 if (tabular.getPWidth(cur.idx()).zero()) {
988                         status.enabled(false);
989                         return true;
990                 } else
991                         return cell(cur.idx())->getStatus(cur, cmd, status);
992         }
993
994         case LFUN_PASTE:
995                 if (tabularStackDirty()) {
996                         status.enabled(true);
997                         return true;
998                 }
999
1000         case LFUN_INSET_MODIFY:
1001                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1002                         status.enabled(true);
1003                         return true;
1004                 }
1005                 // Fall through
1006
1007         default:
1008                 // we try to handle this event in the insets dispatch function.
1009                 return cell(cur.idx())->getStatus(cur, cmd, status);
1010         }
1011 }
1012
1013
1014 int InsetTabular::latex(Buffer const & buf, ostream & os,
1015                         OutputParams const & runparams) const
1016 {
1017         return tabular.latex(buf, os, runparams);
1018 }
1019
1020
1021 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1022                         OutputParams const & runparams) const
1023 {
1024         int const dp = runparams.linelen ? runparams.depth : 0;
1025         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1026 }
1027
1028
1029 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1030                            OutputParams const & runparams) const
1031 {
1032         return tabular.linuxdoc(buf,os, runparams);
1033 }
1034
1035
1036 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1037                           OutputParams const & runparams) const
1038 {
1039         int ret = 0;
1040         InsetBase * master = 0;
1041
1042 #ifdef WITH_WARNINGS
1043 #warning Why not pass a proper DocIterator here?
1044 #endif
1045 #if 0
1046         // if the table is inside a float it doesn't need the informaltable
1047         // wrapper. Search for it.
1048         for (master = owner(); master; master = master->owner())
1049                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1050                         break;
1051 #endif
1052
1053         if (!master) {
1054                 os << "<informaltable>";
1055                 ++ret;
1056         }
1057         ret += tabular.docbook(buf, os, runparams);
1058         if (!master) {
1059                 os << "</informaltable>";
1060                 ++ret;
1061         }
1062         return ret;
1063 }
1064
1065
1066 void InsetTabular::validate(LaTeXFeatures & features) const
1067 {
1068         tabular.validate(features);
1069 }
1070
1071
1072 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1073 {
1074         return tabular.getCellInset(idx);
1075 }
1076
1077
1078 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1079 {
1080         return tabular.getCellInset(idx);
1081 }
1082
1083
1084 void InsetTabular::cursorPos
1085         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1086 {
1087         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1088
1089         // y offset     correction
1090         int const row = tabular.row_of_cell(sl.idx());
1091         for (int i = 0; i <= row; ++i) {
1092                 if (i != 0) {
1093                         y += tabular.getAscentOfRow(i);
1094                         y += tabular.getAdditionalHeight(i);
1095                 }
1096                 if (i != row)
1097                         y += tabular.getDescentOfRow(i);
1098         }
1099
1100         // x offset correction
1101         int const col = tabular.column_of_cell(sl.idx());
1102         int idx = tabular.getCellNumber(row, 0);
1103         for (int j = 0; j < col; ++j) {
1104                 if (tabular.isPartOfMultiColumn(row, j))
1105                         continue;
1106                 x += tabular.getWidthOfColumn(idx);
1107                 ++idx;
1108         }
1109         x += tabular.getBeginningOfTextInCell(idx);
1110         x += ADD_TO_TABULAR_WIDTH;
1111         x += scx_;
1112 }
1113
1114
1115 int InsetTabular::dist(idx_type const cell, int x, int y) const
1116 {
1117         int xx = 0;
1118         int yy = 0;
1119         InsetBase const & inset = *tabular.getCellInset(cell);
1120         Point o = theCoords.getInsets().xy(&inset);
1121         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1122         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1123         int const ybeg = o.y_ - inset.ascent();
1124         row_type const row = tabular.row_of_cell(cell);
1125         int const rowheight = tabular.getAscentOfRow(row)
1126                         + tabular.getDescentOfRow(row);
1127         int const yend = ybeg + rowheight;
1128
1129         if (x < xbeg)
1130                 xx = xbeg - x;
1131         else if (x > xend)
1132                 xx = x - xend;
1133
1134         if (y < ybeg)
1135                 yy = ybeg - y;
1136         else if (y > yend)
1137                 yy = y - yend;
1138
1139         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1140         //       << " ybeg=" << ybeg << " yend=" << yend
1141         //       << " xx=" << xx << " yy=" << yy
1142         //       << " dist=" << xx + yy << endl;
1143         return xx + yy;
1144 }
1145
1146
1147 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1148 {
1149         //lyxerr << "InsetTabular::editXY: " << this << endl;
1150         cur.selection() = false;
1151         cur.push(*this);
1152         cur.idx() = getNearestCell(x, y);
1153         resetPos(cur);
1154         return cell(cur.idx())->text_.editXY(cur, x, y);
1155 }
1156
1157
1158 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1159 {
1160         cur.idx() = getNearestCell(x, y);
1161         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1162 }
1163
1164
1165 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1166 {
1167         idx_type idx_min = 0;
1168         int dist_min = std::numeric_limits<int>::max();
1169         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1170                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1171                         int const d = dist(i, x, y);
1172                         if (d < dist_min) {
1173                                 dist_min = d;
1174                                 idx_min = i;
1175                         }
1176                 }
1177         }
1178         return idx_min;
1179 }
1180
1181
1182 int InsetTabular::getCellXPos(idx_type const cell) const
1183 {
1184         idx_type c = cell;
1185
1186         for (; !tabular.isFirstCellInRow(c); --c)
1187                 ;
1188         int lx = tabular.getWidthOfColumn(cell);
1189         for (; c < cell; ++c)
1190                 lx += tabular.getWidthOfColumn(c);
1191
1192         return lx - tabular.getWidthOfColumn(cell);
1193 }
1194
1195
1196 void InsetTabular::resetPos(LCursor & cur) const
1197 {
1198         BufferView & bv = cur.bv();
1199         int const maxwidth = bv.workWidth();
1200
1201         if (&cur.inset() != this) {
1202                 scx_ = 0;
1203         } else {
1204                 int const X1 = 0;
1205                 int const X2 = maxwidth;
1206                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1207                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1208                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1209
1210                 if (x1 < X1)
1211                         scx_ = X1 + 20 - x1;
1212                 else if (x2 > X2)
1213                         scx_ = X2 - 20 - x2;
1214                 else
1215                         scx_ = 0;
1216         }
1217
1218         cur.needsUpdate();
1219
1220         InsetTabularMailer(*this).updateDialog(&bv);
1221 }
1222
1223
1224 void InsetTabular::moveNextCell(LCursor & cur)
1225 {
1226         if (isRightToLeft(cur)) {
1227                 if (tabular.isFirstCellInRow(cur.idx())) {
1228                         row_type const row = tabular.row_of_cell(cur.idx());
1229                         if (row == tabular.rows() - 1)
1230                                 return;
1231                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1232                 } else {
1233                         if (cur.idx() == 0)
1234                                 return;
1235                         --cur.idx();
1236                 }
1237         } else {
1238                 if (tabular.isLastCell(cur.idx()))
1239                         return;
1240                 ++cur.idx();
1241         }
1242         cur.pit() = 0;
1243         cur.pos() = 0;
1244         resetPos(cur);
1245 }
1246
1247
1248 void InsetTabular::movePrevCell(LCursor & cur)
1249 {
1250         if (isRightToLeft(cur)) {
1251                 if (tabular.isLastCellInRow(cur.idx())) {
1252                         row_type const row = tabular.row_of_cell(cur.idx());
1253                         if (row == 0)
1254                                 return;
1255                         cur.idx() = tabular.getFirstCellInRow(row);
1256                         cur.idx() = tabular.getCellAbove(cur.idx());
1257                 } else {
1258                         if (tabular.isLastCell(cur.idx()))
1259                                 return;
1260                         ++cur.idx();
1261                 }
1262         } else {
1263                 if (cur.idx() == 0) // first cell
1264                         return;
1265                 --cur.idx();
1266         }
1267         cur.pit() = cur.lastpit();
1268         cur.pos() = cur.lastpos();
1269         resetPos(cur);
1270 }
1271
1272
1273 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1274 {
1275         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1276
1277         int i = 0;
1278         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1279                 string const tmp = tabularFeature[i].feature;
1280
1281                 if (tmp == what.substr(0, tmp.length())) {
1282                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1283                         //tabularFeatures[i].feature.length())) {
1284                         action = tabularFeature[i].action;
1285                         break;
1286                 }
1287         }
1288         if (action == LyXTabular::LAST_ACTION)
1289                 return false;
1290
1291         string const val =
1292                 ltrim(what.substr(tabularFeature[i].feature.length()));
1293         tabularFeatures(cur, action, val);
1294         return true;
1295 }
1296
1297
1298 namespace {
1299
1300 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1301                           string const & special, bool & flag)
1302 {
1303         if (special == "dl_above") {
1304                 ltt.topDL = flag;
1305                 ltt.set = false;
1306         } else if (special == "dl_below") {
1307                 ltt.bottomDL = flag;
1308                 ltt.set = false;
1309         } else if (special == "empty") {
1310                 ltt.empty = flag;
1311                 ltt.set = false;
1312         } else if (flag) {
1313                 ltt.empty = false;
1314                 ltt.set = true;
1315         }
1316 }
1317
1318 } // anon namespace
1319
1320
1321 void InsetTabular::tabularFeatures(LCursor & cur,
1322         LyXTabular::Feature feature, string const & value)
1323 {
1324         BufferView & bv = cur.bv();
1325         col_type sel_col_start;
1326         col_type sel_col_end;
1327         row_type sel_row_start;
1328         row_type sel_row_end;
1329         bool setLines = false;
1330         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1331         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1332
1333         switch (feature) {
1334
1335         case LyXTabular::M_ALIGN_LEFT:
1336         case LyXTabular::ALIGN_LEFT:
1337                 setAlign = LYX_ALIGN_LEFT;
1338                 break;
1339
1340         case LyXTabular::M_ALIGN_RIGHT:
1341         case LyXTabular::ALIGN_RIGHT:
1342                 setAlign = LYX_ALIGN_RIGHT;
1343                 break;
1344
1345         case LyXTabular::M_ALIGN_CENTER:
1346         case LyXTabular::ALIGN_CENTER:
1347                 setAlign = LYX_ALIGN_CENTER;
1348                 break;
1349
1350         case LyXTabular::ALIGN_BLOCK:
1351                 setAlign = LYX_ALIGN_BLOCK;
1352                 break;
1353
1354         case LyXTabular::M_VALIGN_TOP:
1355         case LyXTabular::VALIGN_TOP:
1356                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1357                 break;
1358
1359         case LyXTabular::M_VALIGN_BOTTOM:
1360         case LyXTabular::VALIGN_BOTTOM:
1361                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1362                 break;
1363
1364         case LyXTabular::M_VALIGN_MIDDLE:
1365         case LyXTabular::VALIGN_MIDDLE:
1366                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1367                 break;
1368
1369         default:
1370                 break;
1371         }
1372
1373         recordUndoInset(cur, Undo::ATOMIC);
1374
1375         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1376         row_type const row = tabular.row_of_cell(cur.idx());
1377         col_type const column = tabular.column_of_cell(cur.idx());
1378         bool flag = true;
1379         LyXTabular::ltType ltt;
1380
1381         switch (feature) {
1382
1383         case LyXTabular::SET_PWIDTH: {
1384                 LyXLength const len(value);
1385                 tabular.setColumnPWidth(cur, cur.idx(), len);
1386                 if (len.zero()
1387                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1388                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1389                 break;
1390         }
1391
1392         case LyXTabular::SET_MPWIDTH:
1393                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1394                 break;
1395
1396         case LyXTabular::SET_SPECIAL_COLUMN:
1397         case LyXTabular::SET_SPECIAL_MULTI:
1398                 tabular.setAlignSpecial(cur.idx(),value,feature);
1399                 break;
1400
1401         case LyXTabular::APPEND_ROW:
1402                 // append the row into the tabular
1403                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1404                 break;
1405
1406         case LyXTabular::APPEND_COLUMN:
1407                 // append the column into the tabular
1408                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1409                 cur.idx() = tabular.getCellNumber(row, column);
1410                 break;
1411
1412         case LyXTabular::DELETE_ROW:
1413                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1414                         tabular.deleteRow(sel_row_start);
1415                 if (sel_row_start >= tabular.rows())
1416                         --sel_row_start;
1417                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1418                 cur.pit() = 0;
1419                 cur.pos() = 0;
1420                 cur.selection() = false;
1421                 break;
1422
1423         case LyXTabular::DELETE_COLUMN:
1424                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1425                         tabular.deleteColumn(sel_col_start);
1426                 if (sel_col_start >= tabular.columns())
1427                         --sel_col_start;
1428                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1429                 cur.pit() = 0;
1430                 cur.pos() = 0;
1431                 cur.selection() = false;
1432                 break;
1433
1434         case LyXTabular::M_TOGGLE_LINE_TOP:
1435                 flag = false;
1436         case LyXTabular::TOGGLE_LINE_TOP: {
1437                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1438                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1439                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1440                                 tabular.setTopLine(
1441                                         tabular.getCellNumber(i, j),
1442                                         lineSet, flag);
1443                 break;
1444         }
1445
1446         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1447                 flag = false;
1448         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1449                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1450                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1451                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1452                                 tabular.setBottomLine(
1453                                         tabular.getCellNumber(i, j),
1454                                         lineSet,
1455                                         flag);
1456                 break;
1457         }
1458
1459         case LyXTabular::M_TOGGLE_LINE_LEFT:
1460                 flag = false;
1461         case LyXTabular::TOGGLE_LINE_LEFT: {
1462                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1463                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1464                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1465                                 tabular.setLeftLine(
1466                                         tabular.getCellNumber(i,j),
1467                                         lineSet,
1468                                         flag);
1469                 break;
1470         }
1471
1472         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1473                 flag = false;
1474         case LyXTabular::TOGGLE_LINE_RIGHT: {
1475                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1476                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1477                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478                                 tabular.setRightLine(
1479                                         tabular.getCellNumber(i,j),
1480                                         lineSet,
1481                                         flag);
1482                 break;
1483         }
1484
1485         case LyXTabular::M_ALIGN_LEFT:
1486         case LyXTabular::M_ALIGN_RIGHT:
1487         case LyXTabular::M_ALIGN_CENTER:
1488                 flag = false;
1489         case LyXTabular::ALIGN_LEFT:
1490         case LyXTabular::ALIGN_RIGHT:
1491         case LyXTabular::ALIGN_CENTER:
1492         case LyXTabular::ALIGN_BLOCK:
1493                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495                                 tabular.setAlignment(
1496                                         tabular.getCellNumber(i, j),
1497                                         setAlign,
1498                                         flag);
1499                 break;
1500
1501         case LyXTabular::M_VALIGN_TOP:
1502         case LyXTabular::M_VALIGN_BOTTOM:
1503         case LyXTabular::M_VALIGN_MIDDLE:
1504                 flag = false;
1505         case LyXTabular::VALIGN_TOP:
1506         case LyXTabular::VALIGN_BOTTOM:
1507         case LyXTabular::VALIGN_MIDDLE:
1508                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1509                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1510                                 tabular.setVAlignment(
1511                                         tabular.getCellNumber(i, j),
1512                                         setVAlign, flag);
1513                 break;
1514
1515         case LyXTabular::MULTICOLUMN: {
1516                 if (sel_row_start != sel_row_end) {
1517 #ifdef WITH_WARNINGS
1518 #warning Need I say it ? This is horrible.
1519 #endif
1520                         Alert::error(_("Error setting multicolumn"),
1521                                    _("You cannot set multicolumn vertically."));
1522                         return;
1523                 }
1524                 if (!cur.selection()) {
1525                         // just multicol for one single cell
1526                         // check whether we are completely in a multicol
1527                         if (tabular.isMultiColumn(cur.idx()))
1528                                 tabular.unsetMultiColumn(cur.idx());
1529                         else
1530                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1531                         break;
1532                 }
1533                 // we have a selection so this means we just add all this
1534                 // cells to form a multicolumn cell
1535                 idx_type const s_start = cur.selBegin().idx();
1536                 idx_type const s_end = cur.selEnd().idx();
1537                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1538                 cur.idx() = s_start;
1539                 cur.pit() = 0;
1540                 cur.pos() = 0;
1541                 cur.selection() = false;
1542                 break;
1543         }
1544
1545         case LyXTabular::SET_ALL_LINES:
1546                 setLines = true;
1547         case LyXTabular::UNSET_ALL_LINES:
1548                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1549                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1550                                 tabular.setAllLines(
1551                                         tabular.getCellNumber(i,j), setLines);
1552                 break;
1553
1554         case LyXTabular::SET_LONGTABULAR:
1555                 tabular.setLongTabular(true);
1556                 break;
1557
1558         case LyXTabular::UNSET_LONGTABULAR:
1559                 tabular.setLongTabular(false);
1560                 break;
1561
1562         case LyXTabular::SET_ROTATE_TABULAR:
1563                 tabular.setRotateTabular(true);
1564                 break;
1565
1566         case LyXTabular::UNSET_ROTATE_TABULAR:
1567                 tabular.setRotateTabular(false);
1568                 break;
1569
1570         case LyXTabular::SET_ROTATE_CELL:
1571                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1572                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1573                                 tabular.setRotateCell(
1574                                         tabular.getCellNumber(i, j), true);
1575                 break;
1576
1577         case LyXTabular::UNSET_ROTATE_CELL:
1578                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1579                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1580                                 tabular.setRotateCell(
1581                                         tabular.getCellNumber(i, j), false);
1582                 break;
1583
1584         case LyXTabular::SET_USEBOX: {
1585                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1586                 if (val == tabular.getUsebox(cur.idx()))
1587                         val = LyXTabular::BOX_NONE;
1588                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1589                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1590                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1591                 break;
1592         }
1593
1594         case LyXTabular::UNSET_LTFIRSTHEAD:
1595                 flag = false;
1596         case LyXTabular::SET_LTFIRSTHEAD:
1597                 tabular.getRowOfLTFirstHead(row, ltt);
1598                 checkLongtableSpecial(ltt, value, flag);
1599                 tabular.setLTHead(row, flag, ltt, true);
1600                 break;
1601
1602         case LyXTabular::UNSET_LTHEAD:
1603                 flag = false;
1604         case LyXTabular::SET_LTHEAD:
1605                 tabular.getRowOfLTHead(row, ltt);
1606                 checkLongtableSpecial(ltt, value, flag);
1607                 tabular.setLTHead(row, flag, ltt, false);
1608                 break;
1609
1610         case LyXTabular::UNSET_LTFOOT:
1611                 flag = false;
1612         case LyXTabular::SET_LTFOOT:
1613                 tabular.getRowOfLTFoot(row, ltt);
1614                 checkLongtableSpecial(ltt, value, flag);
1615                 tabular.setLTFoot(row, flag, ltt, false);
1616                 break;
1617
1618         case LyXTabular::UNSET_LTLASTFOOT:
1619                 flag = false;
1620         case LyXTabular::SET_LTLASTFOOT:
1621                 tabular.getRowOfLTLastFoot(row, ltt);
1622                 checkLongtableSpecial(ltt, value, flag);
1623                 tabular.setLTFoot(row, flag, ltt, true);
1624                 break;
1625
1626         case LyXTabular::SET_LTNEWPAGE:
1627                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1628                 break;
1629
1630         // dummy stuff just to avoid warnings
1631         case LyXTabular::LAST_ACTION:
1632                 break;
1633         }
1634
1635         InsetTabularMailer(*this).updateDialog(&bv);
1636 }
1637
1638
1639 bool InsetTabular::showInsetDialog(BufferView * bv) const
1640 {
1641         InsetTabularMailer(*this).showDialog(bv);
1642         return true;
1643 }
1644
1645
1646 void InsetTabular::openLayoutDialog(BufferView * bv) const
1647 {
1648         InsetTabularMailer(*this).showDialog(bv);
1649 }
1650
1651
1652 bool InsetTabular::copySelection(LCursor & cur)
1653 {
1654         if (!cur.selection())
1655                 return false;
1656
1657         row_type rs, re;
1658         col_type cs, ce;
1659         getSelection(cur, rs, re, cs, ce);
1660
1661         paste_tabular.reset(new LyXTabular(tabular));
1662
1663         for (row_type i = 0; i < rs; ++i)
1664                 paste_tabular->deleteRow(0);
1665
1666         row_type const rows = re - rs + 1;
1667         while (paste_tabular->rows() > rows)
1668                 paste_tabular->deleteRow(rows);
1669
1670         paste_tabular->setTopLine(0, true, true);
1671         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1672                                      true, true);
1673
1674         for (col_type i = 0; i < cs; ++i)
1675                 paste_tabular->deleteColumn(0);
1676
1677         col_type const columns = ce - cs + 1;
1678         while (paste_tabular->columns() > columns)
1679                 paste_tabular->deleteColumn(columns);
1680
1681         paste_tabular->setLeftLine(0, true, true);
1682         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1683                                     true, true);
1684
1685         ostringstream os;
1686         OutputParams const runparams;
1687         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1688         cur.bv().stuffClipboard(os.str());
1689         // mark tabular stack dirty
1690         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1691         // when we (hopefully) have a one-for-all paste mechanism.
1692         lyx::cap::dirtyTabularStack(true);
1693
1694         return true;
1695 }
1696
1697
1698 bool InsetTabular::pasteSelection(LCursor & cur)
1699 {
1700         if (!paste_tabular)
1701                 return false;
1702         col_type const actcol = tabular.column_of_cell(cur.idx());
1703         row_type const actrow = tabular.row_of_cell(cur.idx());
1704         for (row_type r1 = 0, r2 = actrow;
1705              r1 < paste_tabular->rows() && r2 < tabular.rows();
1706              ++r1, ++r2) {
1707                 for (col_type c1 = 0, c2 = actcol;
1708                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1709                     ++c1, ++c2) {
1710                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1711                             tabular.isPartOfMultiColumn(r2, c2))
1712                                 continue;
1713                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1714                                 --c2;
1715                                 continue;
1716                         }
1717                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1718                                 --c1;
1719                                 continue;
1720                         }
1721                         shared_ptr<InsetText> inset(
1722                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1723                         tabular.setCellInset(r2, c2, inset);
1724                         inset->markNew();
1725                         cur.pos() = 0;
1726                 }
1727         }
1728         return true;
1729 }
1730
1731
1732 void InsetTabular::cutSelection(LCursor & cur)
1733 {
1734         if (!cur.selection())
1735                 return;
1736
1737         row_type rs, re;
1738         col_type cs, ce;
1739         getSelection(cur, rs, re, cs, ce);
1740         for (row_type i = rs; i <= re; ++i) {
1741                 for (col_type j = cs; j <= ce; ++j) {
1742                         shared_ptr<InsetText> t
1743                                 = cell(tabular.getCellNumber(i, j));
1744                         if (cur.buffer().params().tracking_changes)
1745                                 t->markErased(true);
1746                         else
1747                                 t->clear();
1748                 }
1749         }
1750
1751         // cursor position might be invalid now
1752         cur.pos() = cur.lastpos();
1753         cur.clearSelection();
1754 }
1755
1756
1757 bool InsetTabular::isRightToLeft(LCursor & cur) const
1758 {
1759         BOOST_ASSERT(cur.depth() > 1);
1760         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1761         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1762         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1763                                          parentpos).language()->rightToLeft();
1764 }
1765
1766
1767 void InsetTabular::getSelection(LCursor & cur,
1768         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1769 {
1770         CursorSlice const & beg = cur.selBegin();
1771         CursorSlice const & end = cur.selEnd();
1772         cs = tabular.column_of_cell(beg.idx());
1773         ce = tabular.column_of_cell(end.idx());
1774         if (cs > ce) {
1775                 ce = cs;
1776                 cs = tabular.column_of_cell(end.idx());
1777         } else {
1778                 ce = tabular.right_column_of_cell(end.idx());
1779         }
1780
1781         rs = tabular.row_of_cell(beg.idx());
1782         re = tabular.row_of_cell(end.idx());
1783         if (rs > re)
1784                 swap(rs, re);
1785 }
1786
1787
1788 LyXText * InsetTabular::getText(int idx) const
1789 {
1790         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1791 }
1792
1793
1794 void InsetTabular::markErased(bool erased)
1795 {
1796         for (idx_type idx = 0; idx < nargs(); ++idx)
1797                 cell(idx)->markErased(erased);
1798 }
1799
1800
1801 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1802 {
1803         return tabular.getPWidth(cell).zero();
1804 }
1805
1806
1807 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1808                                      bool usePaste)
1809 {
1810         if (buf.length() <= 0)
1811                 return true;
1812
1813         col_type cols = 1;
1814         row_type rows = 1;
1815         col_type maxCols = 1;
1816         string::size_type const len = buf.length();
1817         string::size_type p = 0;
1818
1819         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1820                 switch (buf[p]) {
1821                 case '\t':
1822                         ++cols;
1823                         break;
1824                 case '\n':
1825                         if (p + 1 < len)
1826                                 ++rows;
1827                         maxCols = max(cols, maxCols);
1828                         cols = 1;
1829                         break;
1830                 }
1831                 ++p;
1832         }
1833         maxCols = max(cols, maxCols);
1834         LyXTabular * loctab;
1835         idx_type cell = 0;
1836         col_type ocol = 0;
1837         row_type row = 0;
1838         if (usePaste) {
1839                 paste_tabular.reset(
1840                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1841                 loctab = paste_tabular.get();
1842                 cols = 0;
1843                 dirtyTabularStack(true);
1844         } else {
1845                 loctab = &tabular;
1846                 cell = bv.cursor().idx();
1847                 ocol = tabular.column_of_cell(cell);
1848                 row = tabular.row_of_cell(cell);
1849         }
1850
1851         string::size_type op = 0;
1852         idx_type const cells = loctab->getNumberOfCells();
1853         p = 0;
1854         cols = ocol;
1855         rows = loctab->rows();
1856         col_type const columns = loctab->columns();
1857
1858         while (cell < cells && p < len && row < rows &&
1859                (p = buf.find_first_of("\t\n", p)) != string::npos)
1860         {
1861                 if (p >= len)
1862                         break;
1863                 switch (buf[p]) {
1864                 case '\t':
1865                         // we can only set this if we are not too far right
1866                         if (cols < columns) {
1867                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1868                                 inset->setViewCache(&bv);
1869                                 Paragraph & par = inset->text_.getPar(0);
1870                                 LyXFont const font = inset->text_.getFont(par, 0);
1871                                 inset->setText(buf.substr(op, p - op), font);
1872                                 ++cols;
1873                                 ++cell;
1874                         }
1875                         break;
1876                 case '\n':
1877                         // we can only set this if we are not too far right
1878                         if (cols < columns) {
1879                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1880                                 inset->setViewCache(&bv);
1881                                 Paragraph & par = inset->text_.getPar(0);
1882                                 LyXFont const font = inset->text_.getFont(par, 0);
1883                                 inset->setText(buf.substr(op, p - op), font);
1884                         }
1885                         cols = ocol;
1886                         ++row;
1887                         if (row < rows)
1888                                 cell = loctab->getCellNumber(row, cols);
1889                         break;
1890                 }
1891                 ++p;
1892                 op = p;
1893         }
1894         // check for the last cell if there is no trailing '\n'
1895         if (cell < cells && op < len) {
1896                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1897                 inset->setViewCache(&bv);
1898                 Paragraph & par = inset->text_.getPar(0);
1899                 LyXFont const font = inset->text_.getFont(par, 0);
1900                 inset->setText(buf.substr(op, len - op), font);
1901         }
1902         return true;
1903 }
1904
1905
1906 void InsetTabular::addPreview(PreviewLoader & loader) const
1907 {
1908         row_type const rows = tabular.rows();
1909         col_type const columns = tabular.columns();
1910         for (row_type i = 0; i < rows; ++i) {
1911                 for (col_type j = 0; j < columns; ++j)
1912                         tabular.getCellInset(i, j)->addPreview(loader);
1913         }
1914 }
1915
1916
1917 bool InsetTabular::tablemode(LCursor & cur) const
1918 {
1919         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1920 }
1921
1922
1923
1924
1925
1926 string const InsetTabularMailer::name_("tabular");
1927
1928 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1929         : inset_(const_cast<InsetTabular &>(inset))
1930 {}
1931
1932
1933 string const InsetTabularMailer::inset2string(Buffer const &) const
1934 {
1935         return params2string(inset_);
1936 }
1937
1938
1939 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1940 {
1941         istringstream data(in);
1942         LyXLex lex(0,0);
1943         lex.setStream(data);
1944
1945         if (in.empty())
1946                 return;
1947
1948         string token;
1949         lex >> token;
1950         if (!lex || token != name_)
1951                 return print_mailer_error("InsetTabularMailer", in, 1,
1952                                           name_);
1953
1954         // This is part of the inset proper that is usually swallowed
1955         // by Buffer::readInset
1956         lex >> token;
1957         if (!lex || token != "Tabular")
1958                 return print_mailer_error("InsetTabularMailer", in, 2,
1959                                           "Tabular");
1960
1961         Buffer const & buffer = inset.buffer();
1962         inset.read(buffer, lex);
1963 }
1964
1965
1966 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1967 {
1968         ostringstream data;
1969         data << name_ << ' ';
1970         inset.write(inset.buffer(), data);
1971         data << "\\end_inset\n";
1972         return data.str();
1973 }