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