]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
disable open dialogs if applying them is not allowed
[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_INSET_INSERT:
969         case LFUN_INSERT_CHARSTYLE:
970         case LFUN_INSET_FLOAT:
971         case LFUN_INSET_WIDE_FLOAT:
972         case LFUN_INSET_FOOTNOTE:
973         case LFUN_INSET_MARGINAL:
974         case LFUN_INSERT_MATH:
975         case LFUN_MATH_MODE:
976         case LFUN_MATH_MUTATE:
977         case LFUN_MATH_DISPLAY:
978         case LFUN_INSERT_NOTE:
979         case LFUN_INSET_OPTARG:
980         case LFUN_INSERT_BOX:
981         case LFUN_INSERT_BRANCH:
982         case LFUN_INSET_WRAP:
983         case LFUN_INSET_ERT: {
984                 if (tablemode(cur)) {
985                         status.enabled(false);
986                         return true;
987                 }
988         }
989
990         default:
991                 // we try to handle this event in the insets dispatch function.
992                 return cell(cur.idx())->getStatus(cur, cmd, status);
993         }
994 }
995
996
997 int InsetTabular::latex(Buffer const & buf, ostream & os,
998                         OutputParams const & runparams) const
999 {
1000         return tabular.latex(buf, os, runparams);
1001 }
1002
1003
1004 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1005                         OutputParams const & runparams) const
1006 {
1007         int const dp = runparams.linelen ? runparams.depth : 0;
1008         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1009 }
1010
1011
1012 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1013                            OutputParams const & runparams) const
1014 {
1015         return tabular.linuxdoc(buf,os, runparams);
1016 }
1017
1018
1019 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1020                           OutputParams const & runparams) const
1021 {
1022         int ret = 0;
1023         InsetBase * master = 0;
1024
1025 #ifdef WITH_WARNINGS
1026 #warning Why not pass a proper DocIterator here?
1027 #endif
1028 #if 0
1029         // if the table is inside a float it doesn't need the informaltable
1030         // wrapper. Search for it.
1031         for (master = owner(); master; master = master->owner())
1032                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1033                         break;
1034 #endif
1035
1036         if (!master) {
1037                 os << "<informaltable>";
1038                 ++ret;
1039         }
1040         ret += tabular.docbook(buf, os, runparams);
1041         if (!master) {
1042                 os << "</informaltable>";
1043                 ++ret;
1044         }
1045         return ret;
1046 }
1047
1048
1049 void InsetTabular::validate(LaTeXFeatures & features) const
1050 {
1051         tabular.validate(features);
1052 }
1053
1054
1055 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1056 {
1057         return tabular.getCellInset(idx);
1058 }
1059
1060
1061 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1062 {
1063         return tabular.getCellInset(idx);
1064 }
1065
1066
1067 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1068 {
1069         cell(sl.idx())->getCursorPos(sl, x, y);
1070
1071         // y offset     correction
1072         int const row = tabular.row_of_cell(sl.idx());
1073         for (int i = 0; i <= row; ++i) {
1074                 if (i != 0) {
1075                         y += tabular.getAscentOfRow(i);
1076                         y += tabular.getAdditionalHeight(i);
1077                 }
1078                 if (i != row)
1079                         y += tabular.getDescentOfRow(i);
1080         }
1081
1082         // x offset correction
1083         int const col = tabular.column_of_cell(sl.idx());
1084         int idx = tabular.getCellNumber(row, 0);
1085         for (int j = 0; j < col; ++j) {
1086                 if (tabular.isPartOfMultiColumn(row, j))
1087                         continue;
1088                 x += tabular.getWidthOfColumn(idx);
1089                 ++idx;
1090         }
1091         x += tabular.getBeginningOfTextInCell(idx);
1092         x += ADD_TO_TABULAR_WIDTH;
1093         x += scroll();
1094 }
1095
1096
1097 namespace  {
1098
1099
1100 // Manhattan distance to nearest corner
1101 int dist(InsetOld const & inset, int x, int y)
1102 {
1103         int xx = 0;
1104         int yy = 0;
1105         Point o = theCoords.getInsets().xy(&inset);
1106         int const xo = o.x_;
1107         int const yo = o.y_;
1108
1109         if (x < xo)
1110                 xx = xo - x;
1111         else if (x > xo + inset.width())
1112                 xx = x - xo - inset.width();
1113
1114         if (y < yo - inset.ascent())
1115                 yy = yo - inset.ascent() - y;
1116         else if (y > yo + inset.descent())
1117                 yy = y - yo - inset.descent();
1118
1119         lyxerr << " xo_=" << xo << "  yo_=" << yo
1120                << " width_=" << inset.width() << " ascent=" << inset.ascent()
1121                << " descent=" << inset.descent()
1122                << " dist=" << xx + yy << endl;
1123         return xx + yy;
1124 }
1125
1126
1127 } //namespace anon
1128
1129
1130 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1131 {
1132         //lyxerr << "InsetTabular::editXY: " << this << endl;
1133         cur.selection() = false;
1134         cur.push(const_cast<InsetTabular&>(*this));
1135         cur.idx() = getNearestCell(x, y);
1136         resetPos(cur);
1137         return cell(cur.idx())->text_.editXY(cur, x, y);
1138         //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1139 }
1140
1141
1142 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1143 {
1144         //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1145         cur.idx() = getNearestCell(x, y);
1146         resetPos(cur);
1147         return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1148 }
1149
1150
1151 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1152 {
1153         lyxerr << "# InsetTabular::getNearestCell()  x=" << x << " y=" << y << endl;
1154         idx_type idx_min = 0;
1155         int dist_min = std::numeric_limits<int>::max();
1156         for (idx_type i = 0; i < nargs(); ++i) {
1157                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1158                         int d = dist(*tabular.getCellInset(i), x, y);
1159                         if (d < dist_min) {
1160                                 dist_min = d;
1161                                 idx_min = i;
1162                         }
1163                 }
1164         }
1165         return idx_min;
1166 }
1167
1168
1169 int InsetTabular::getCellXPos(idx_type const cell) const
1170 {
1171         idx_type c = cell;
1172
1173         for (; !tabular.isFirstCellInRow(c); --c)
1174                 ;
1175         int lx = tabular.getWidthOfColumn(cell);
1176         for (; c < cell; ++c)
1177                 lx += tabular.getWidthOfColumn(c);
1178
1179         return lx - tabular.getWidthOfColumn(cell);
1180 }
1181
1182
1183 void InsetTabular::resetPos(LCursor & cur) const
1184 {
1185         BufferView & bv = cur.bv();
1186 //      col_type const actcol = tabular.column_of_cell(cur.idx());
1187 //      int const offset = ADD_TO_TABULAR_WIDTH + 2;
1188 //      int const new_x = getCellXPos(cur.idx()) + offset;
1189 //      int const old_x = cursorx_;
1190 //      int const col_width = tabular.getWidthOfColumn(cur.idx());
1191 //      cursorx_ = new_x;
1192 //    cursor.x(getCellXPos(cur.idx()) + offset);
1193 //      if (actcol < tabular.columns() - 1 && scroll(false) &&
1194 //              tabular.getWidthOfTabular() < bv.workWidth()-20)
1195 //      {
1196 //              scroll(bv, 0.0F);
1197 //      } else if (cursorx_ - offset > 20 &&
1198 //                 cursorx_ - offset + col_width > bv.workWidth() - 20) {
1199 //              scroll(bv, - col_width - 20);
1200 //      } else if (cursorx_ - offset < 20) {
1201 //              scroll(bv, 20 - cursorx_ + offset);
1202 //      } else if (scroll() && xo() > 20 &&
1203 //                 xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1204 //              scroll(bv, old_x - cursorx_);
1205 //      }
1206
1207         if (&cur.inset() != this) {
1208                 scroll(bv, 0.0F);
1209         } else {
1210                 int const X1 = 0;
1211                 int const X2 = bv.workWidth();
1212                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1213                 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1214                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1215
1216                 if (x1 < X1)
1217                         scroll(bv, X1 + 20 - x1);
1218                 else if (x2 > X2)
1219                         scroll(bv, X2 - 20 - x2);
1220         }
1221
1222         cur.needsUpdate();
1223
1224         InsetTabularMailer(*this).updateDialog(&bv);
1225 }
1226
1227
1228 void InsetTabular::moveNextCell(LCursor & cur)
1229 {
1230         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1231         if (isRightToLeft(cur)) {
1232                 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1233                 if (tabular.isFirstCellInRow(cur.idx())) {
1234                         row_type const row = tabular.row_of_cell(cur.idx());
1235                         if (row == tabular.rows() - 1)
1236                                 return;
1237                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1238                 } else {
1239                         if (cur.idx() == 0)
1240                                 return;
1241                         --cur.idx();
1242                 }
1243         } else {
1244                 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1245                 if (tabular.isLastCell(cur.idx()))
1246                         return;
1247                 ++cur.idx();
1248         }
1249         cur.pit() = 0;
1250         cur.pos() = 0;
1251         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1252         resetPos(cur);
1253 }
1254
1255
1256 void InsetTabular::movePrevCell(LCursor & cur)
1257 {
1258         if (isRightToLeft(cur)) {
1259                 if (tabular.isLastCellInRow(cur.idx())) {
1260                         row_type const row = tabular.row_of_cell(cur.idx());
1261                         if (row == 0)
1262                                 return;
1263                         cur.idx() = tabular.getFirstCellInRow(row);
1264                         cur.idx() = tabular.getCellAbove(cur.idx());
1265                 } else {
1266                         if (tabular.isLastCell(cur.idx()))
1267                                 return;
1268                         ++cur.idx();
1269                 }
1270         } else {
1271                 if (cur.idx() == 0) // first cell
1272                         return;
1273                 --cur.idx();
1274         }
1275         cur.pit() = cur.lastpit();
1276         cur.pos() = cur.lastpos();
1277         resetPos(cur);
1278 }
1279
1280
1281 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1282 {
1283         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1284
1285         int i = 0;
1286         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1287                 string const tmp = tabularFeature[i].feature;
1288
1289                 if (tmp == what.substr(0, tmp.length())) {
1290                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1291                         //tabularFeatures[i].feature.length())) {
1292                         action = tabularFeature[i].action;
1293                         break;
1294                 }
1295         }
1296         if (action == LyXTabular::LAST_ACTION)
1297                 return false;
1298
1299         string const val =
1300                 ltrim(what.substr(tabularFeature[i].feature.length()));
1301         tabularFeatures(cur, action, val);
1302         return true;
1303 }
1304
1305
1306 namespace {
1307
1308 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1309                           string const & special, bool & flag)
1310 {
1311         if (special == "dl_above") {
1312                 ltt.topDL = flag;
1313                 ltt.set = false;
1314         } else if (special == "dl_below") {
1315                 ltt.bottomDL = flag;
1316                 ltt.set = false;
1317         } else if (special == "empty") {
1318                 ltt.empty = flag;
1319                 ltt.set = false;
1320         } else if (flag) {
1321                 ltt.empty = false;
1322                 ltt.set = true;
1323         }
1324 }
1325
1326 } // anon namespace
1327
1328
1329 void InsetTabular::tabularFeatures(LCursor & cur,
1330         LyXTabular::Feature feature, string const & value)
1331 {
1332         BufferView & bv = cur.bv();
1333         col_type sel_col_start;
1334         col_type sel_col_end;
1335         row_type sel_row_start;
1336         row_type sel_row_end;
1337         bool setLines = false;
1338         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1339         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1340
1341         switch (feature) {
1342
1343         case LyXTabular::M_ALIGN_LEFT:
1344         case LyXTabular::ALIGN_LEFT:
1345                 setAlign = LYX_ALIGN_LEFT;
1346                 break;
1347
1348         case LyXTabular::M_ALIGN_RIGHT:
1349         case LyXTabular::ALIGN_RIGHT:
1350                 setAlign = LYX_ALIGN_RIGHT;
1351                 break;
1352
1353         case LyXTabular::M_ALIGN_CENTER:
1354         case LyXTabular::ALIGN_CENTER:
1355                 setAlign = LYX_ALIGN_CENTER;
1356                 break;
1357
1358         case LyXTabular::ALIGN_BLOCK:
1359                 setAlign = LYX_ALIGN_BLOCK;
1360                 break;
1361
1362         case LyXTabular::M_VALIGN_TOP:
1363         case LyXTabular::VALIGN_TOP:
1364                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1365                 break;
1366
1367         case LyXTabular::M_VALIGN_BOTTOM:
1368         case LyXTabular::VALIGN_BOTTOM:
1369                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1370                 break;
1371
1372         case LyXTabular::M_VALIGN_MIDDLE:
1373         case LyXTabular::VALIGN_MIDDLE:
1374                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1375                 break;
1376
1377         default:
1378                 break;
1379         }
1380
1381         recordUndo(cur, Undo::ATOMIC);
1382
1383         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1384         row_type const row = tabular.row_of_cell(cur.idx());
1385         col_type const column = tabular.column_of_cell(cur.idx());
1386         bool flag = true;
1387         LyXTabular::ltType ltt;
1388
1389         switch (feature) {
1390
1391         case LyXTabular::SET_PWIDTH: {
1392                 LyXLength const len(value);
1393                 tabular.setColumnPWidth(cur.idx(), len);
1394                 if (len.zero()
1395                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1396                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1397                 else if (!len.zero()
1398                          && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1399                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1400                 break;
1401         }
1402
1403         case LyXTabular::SET_MPWIDTH:
1404                 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1405                 break;
1406
1407         case LyXTabular::SET_SPECIAL_COLUMN:
1408         case LyXTabular::SET_SPECIAL_MULTI:
1409                 tabular.setAlignSpecial(cur.idx(),value,feature);
1410                 break;
1411
1412         case LyXTabular::APPEND_ROW:
1413                 // append the row into the tabular
1414                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1415                 break;
1416
1417         case LyXTabular::APPEND_COLUMN:
1418                 // append the column into the tabular
1419                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1420                 break;
1421
1422         case LyXTabular::DELETE_ROW:
1423                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1424                         tabular.deleteRow(sel_row_start);
1425                 if (sel_row_start >= tabular.rows())
1426                         --sel_row_start;
1427                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1428                 cur.pit() = 0;
1429                 cur.pos() = 0;
1430                 cur.selection() = false;
1431                 break;
1432
1433         case LyXTabular::DELETE_COLUMN:
1434                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1435                         tabular.deleteColumn(sel_col_start);
1436                 if (sel_col_start >= tabular.columns())
1437                         --sel_col_start;
1438                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1439                 cur.pit() = 0;
1440                 cur.pos() = 0;
1441                 cur.selection() = false;
1442                 break;
1443
1444         case LyXTabular::M_TOGGLE_LINE_TOP:
1445                 flag = false;
1446         case LyXTabular::TOGGLE_LINE_TOP: {
1447                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1448                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1449                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1450                                 tabular.setTopLine(
1451                                         tabular.getCellNumber(i, j),
1452                                         lineSet, flag);
1453                 break;
1454         }
1455
1456         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1457                 flag = false;
1458         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1459                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1460                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1461                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1462                                 tabular.setBottomLine(
1463                                         tabular.getCellNumber(i, j),
1464                                         lineSet,
1465                                         flag);
1466                 break;
1467         }
1468
1469         case LyXTabular::M_TOGGLE_LINE_LEFT:
1470                 flag = false;
1471         case LyXTabular::TOGGLE_LINE_LEFT: {
1472                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1473                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1474                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1475                                 tabular.setLeftLine(
1476                                         tabular.getCellNumber(i,j),
1477                                         lineSet,
1478                                         flag);
1479                 break;
1480         }
1481
1482         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1483                 flag = false;
1484         case LyXTabular::TOGGLE_LINE_RIGHT: {
1485                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1486                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1487                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1488                                 tabular.setRightLine(
1489                                         tabular.getCellNumber(i,j),
1490                                         lineSet,
1491                                         flag);
1492                 break;
1493         }
1494
1495         case LyXTabular::M_ALIGN_LEFT:
1496         case LyXTabular::M_ALIGN_RIGHT:
1497         case LyXTabular::M_ALIGN_CENTER:
1498                 flag = false;
1499         case LyXTabular::ALIGN_LEFT:
1500         case LyXTabular::ALIGN_RIGHT:
1501         case LyXTabular::ALIGN_CENTER:
1502         case LyXTabular::ALIGN_BLOCK:
1503                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1504                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1505                                 tabular.setAlignment(
1506                                         tabular.getCellNumber(i, j),
1507                                         setAlign,
1508                                         flag);
1509                 break;
1510
1511         case LyXTabular::M_VALIGN_TOP:
1512         case LyXTabular::M_VALIGN_BOTTOM:
1513         case LyXTabular::M_VALIGN_MIDDLE:
1514                 flag = false;
1515         case LyXTabular::VALIGN_TOP:
1516         case LyXTabular::VALIGN_BOTTOM:
1517         case LyXTabular::VALIGN_MIDDLE:
1518                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1519                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1520                                 tabular.setVAlignment(
1521                                         tabular.getCellNumber(i, j),
1522                                         setVAlign, flag);
1523                 break;
1524
1525         case LyXTabular::MULTICOLUMN: {
1526                 if (sel_row_start != sel_row_end) {
1527 #ifdef WITH_WARNINGS
1528 #warning Need I say it ? This is horrible.
1529 #endif
1530                         Alert::error(_("Error setting multicolumn"),
1531                                    _("You cannot set multicolumn vertically."));
1532                         return;
1533                 }
1534                 if (!cur.selection()) {
1535                         // just multicol for one single cell
1536                         // check whether we are completely in a multicol
1537                         if (tabular.isMultiColumn(cur.idx()))
1538                                 tabular.unsetMultiColumn(cur.idx());
1539                         else
1540                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1541                         break;
1542                 }
1543                 // we have a selection so this means we just add all this
1544                 // cells to form a multicolumn cell
1545                 idx_type const s_start = cur.selBegin().idx();
1546                 idx_type const s_end = cur.selEnd().idx();
1547                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1548                 cur.idx() = s_start;
1549                 cur.pit() = 0;
1550                 cur.pos() = 0;
1551                 cur.selection() = false;
1552                 break;
1553         }
1554
1555         case LyXTabular::SET_ALL_LINES:
1556                 setLines = true;
1557         case LyXTabular::UNSET_ALL_LINES:
1558                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1559                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1560                                 tabular.setAllLines(
1561                                         tabular.getCellNumber(i,j), setLines);
1562                 break;
1563
1564         case LyXTabular::SET_LONGTABULAR:
1565                 tabular.setLongTabular(true);
1566                 break;
1567
1568         case LyXTabular::UNSET_LONGTABULAR:
1569                 tabular.setLongTabular(false);
1570                 break;
1571
1572         case LyXTabular::SET_ROTATE_TABULAR:
1573                 tabular.setRotateTabular(true);
1574                 break;
1575
1576         case LyXTabular::UNSET_ROTATE_TABULAR:
1577                 tabular.setRotateTabular(false);
1578                 break;
1579
1580         case LyXTabular::SET_ROTATE_CELL:
1581                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1582                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1583                                 tabular.setRotateCell(
1584                                         tabular.getCellNumber(i, j), true);
1585                 break;
1586
1587         case LyXTabular::UNSET_ROTATE_CELL:
1588                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1589                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1590                                 tabular.setRotateCell(
1591                                         tabular.getCellNumber(i, j), false);
1592                 break;
1593
1594         case LyXTabular::SET_USEBOX: {
1595                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1596                 if (val == tabular.getUsebox(cur.idx()))
1597                         val = LyXTabular::BOX_NONE;
1598                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1599                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1600                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1601                 break;
1602         }
1603
1604         case LyXTabular::UNSET_LTFIRSTHEAD:
1605                 flag = false;
1606         case LyXTabular::SET_LTFIRSTHEAD:
1607                 tabular.getRowOfLTFirstHead(row, ltt);
1608                 checkLongtableSpecial(ltt, value, flag);
1609                 tabular.setLTHead(row, flag, ltt, true);
1610                 break;
1611
1612         case LyXTabular::UNSET_LTHEAD:
1613                 flag = false;
1614         case LyXTabular::SET_LTHEAD:
1615                 tabular.getRowOfLTHead(row, ltt);
1616                 checkLongtableSpecial(ltt, value, flag);
1617                 tabular.setLTHead(row, flag, ltt, false);
1618                 break;
1619
1620         case LyXTabular::UNSET_LTFOOT:
1621                 flag = false;
1622         case LyXTabular::SET_LTFOOT:
1623                 tabular.getRowOfLTFoot(row, ltt);
1624                 checkLongtableSpecial(ltt, value, flag);
1625                 tabular.setLTFoot(row, flag, ltt, false);
1626                 break;
1627
1628         case LyXTabular::UNSET_LTLASTFOOT:
1629                 flag = false;
1630         case LyXTabular::SET_LTLASTFOOT:
1631                 tabular.getRowOfLTLastFoot(row, ltt);
1632                 checkLongtableSpecial(ltt, value, flag);
1633                 tabular.setLTFoot(row, flag, ltt, true);
1634                 break;
1635
1636         case LyXTabular::SET_LTNEWPAGE:
1637                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1638                 break;
1639
1640         // dummy stuff just to avoid warnings
1641         case LyXTabular::LAST_ACTION:
1642                 break;
1643         }
1644
1645         InsetTabularMailer(*this).updateDialog(&bv);
1646 }
1647
1648
1649 bool InsetTabular::showInsetDialog(BufferView * bv) const
1650 {
1651         InsetTabularMailer(*this).showDialog(bv);
1652         return true;
1653 }
1654
1655
1656 void InsetTabular::openLayoutDialog(BufferView * bv) const
1657 {
1658         InsetTabularMailer(*this).showDialog(bv);
1659 }
1660
1661
1662 void InsetTabular::getLabelList(Buffer const & buffer,
1663                                 vector<string> & list) const
1664 {
1665         tabular.getLabelList(buffer, list);
1666 }
1667
1668
1669 bool InsetTabular::copySelection(LCursor & cur)
1670 {
1671         if (!cur.selection())
1672                 return false;
1673
1674         row_type rs, re;
1675         col_type cs, ce;
1676         getSelection(cur, rs, re, cs, ce);
1677
1678         paste_tabular.reset(new LyXTabular(tabular));
1679
1680         for (row_type i = 0; i < rs; ++i)
1681                 paste_tabular->deleteRow(0);
1682
1683         row_type const rows = re - rs + 1;
1684         while (paste_tabular->rows() > rows)
1685                 paste_tabular->deleteRow(rows);
1686
1687         paste_tabular->setTopLine(0, true, true);
1688         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1689                                      true, true);
1690
1691         for (col_type i = 0; i < cs; ++i)
1692                 paste_tabular->deleteColumn(0);
1693
1694         col_type const columns = ce - cs + 1;
1695         while (paste_tabular->columns() > columns)
1696                 paste_tabular->deleteColumn(columns);
1697
1698         paste_tabular->setLeftLine(0, true, true);
1699         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1700                                     true, true);
1701
1702         ostringstream os;
1703         OutputParams const runparams;
1704         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1705         cur.bv().stuffClipboard(os.str());
1706         return true;
1707 }
1708
1709
1710 bool InsetTabular::pasteSelection(LCursor & cur)
1711 {
1712         if (!paste_tabular)
1713                 return false;
1714         col_type const actcol = tabular.column_of_cell(cur.idx());
1715         row_type const actrow = tabular.row_of_cell(cur.idx());
1716         for (row_type r1 = 0, r2 = actrow;
1717              r1 < paste_tabular->rows() && r2 < tabular.rows();
1718              ++r1, ++r2) {
1719                 for (col_type c1 = 0, c2 = actcol;
1720                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1721                     ++c1, ++c2) {
1722                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1723                             tabular.isPartOfMultiColumn(r2, c2))
1724                                 continue;
1725                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1726                                 --c2;
1727                                 continue;
1728                         }
1729                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1730                                 --c1;
1731                                 continue;
1732                         }
1733                         shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1734                         inset = paste_tabular->getCellInset(r1, c1);
1735                         inset->markNew();
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 }