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