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