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