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