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