]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
fix double/triple clicking to select (bug 1811)
[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         case LFUN_INSET_MODIFY:
991                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
992                         status.enabled(true);
993                         return true;
994                 }
995                 // Fall through
996
997         default:
998                 // we try to handle this event in the insets dispatch function.
999                 return cell(cur.idx())->getStatus(cur, cmd, status);
1000         }
1001 }
1002
1003
1004 int InsetTabular::latex(Buffer const & buf, ostream & os,
1005                         OutputParams const & runparams) const
1006 {
1007         return tabular.latex(buf, os, runparams);
1008 }
1009
1010
1011 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1012                         OutputParams const & runparams) const
1013 {
1014         int const dp = runparams.linelen ? runparams.depth : 0;
1015         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1016 }
1017
1018
1019 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1020                            OutputParams const & runparams) const
1021 {
1022         return tabular.linuxdoc(buf,os, runparams);
1023 }
1024
1025
1026 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1027                           OutputParams const & runparams) const
1028 {
1029         int ret = 0;
1030         InsetBase * master = 0;
1031
1032 #ifdef WITH_WARNINGS
1033 #warning Why not pass a proper DocIterator here?
1034 #endif
1035 #if 0
1036         // if the table is inside a float it doesn't need the informaltable
1037         // wrapper. Search for it.
1038         for (master = owner(); master; master = master->owner())
1039                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1040                         break;
1041 #endif
1042
1043         if (!master) {
1044                 os << "<informaltable>";
1045                 ++ret;
1046         }
1047         ret += tabular.docbook(buf, os, runparams);
1048         if (!master) {
1049                 os << "</informaltable>";
1050                 ++ret;
1051         }
1052         return ret;
1053 }
1054
1055
1056 void InsetTabular::validate(LaTeXFeatures & features) const
1057 {
1058         tabular.validate(features);
1059 }
1060
1061
1062 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1063 {
1064         return tabular.getCellInset(idx);
1065 }
1066
1067
1068 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1069 {
1070         return tabular.getCellInset(idx);
1071 }
1072
1073
1074 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1075 {
1076         cell(sl.idx())->getCursorPos(sl, x, y);
1077
1078         // y offset     correction
1079         int const row = tabular.row_of_cell(sl.idx());
1080         for (int i = 0; i <= row; ++i) {
1081                 if (i != 0) {
1082                         y += tabular.getAscentOfRow(i);
1083                         y += tabular.getAdditionalHeight(i);
1084                 }
1085                 if (i != row)
1086                         y += tabular.getDescentOfRow(i);
1087         }
1088
1089         // x offset correction
1090         int const col = tabular.column_of_cell(sl.idx());
1091         int idx = tabular.getCellNumber(row, 0);
1092         for (int j = 0; j < col; ++j) {
1093                 if (tabular.isPartOfMultiColumn(row, j))
1094                         continue;
1095                 x += tabular.getWidthOfColumn(idx);
1096                 ++idx;
1097         }
1098         x += tabular.getBeginningOfTextInCell(idx);
1099         x += ADD_TO_TABULAR_WIDTH;
1100         x += scroll();
1101 }
1102
1103
1104 namespace  {
1105
1106
1107 // Manhattan distance to nearest corner
1108 int dist(InsetOld const & inset, int x, int y)
1109 {
1110         int xx = 0;
1111         int yy = 0;
1112         Point o = theCoords.getInsets().xy(&inset);
1113         int const xo = o.x_;
1114         int const yo = o.y_;
1115
1116         if (x < xo)
1117                 xx = xo - x;
1118         else if (x > xo + inset.width())
1119                 xx = x - xo - inset.width();
1120
1121         if (y < yo - inset.ascent())
1122                 yy = yo - inset.ascent() - y;
1123         else if (y > yo + inset.descent())
1124                 yy = y - yo - inset.descent();
1125
1126         lyxerr << " xo_=" << xo << "  yo_=" << yo
1127                << " width_=" << inset.width() << " ascent=" << inset.ascent()
1128                << " descent=" << inset.descent()
1129                << " dist=" << xx + yy << endl;
1130         return xx + yy;
1131 }
1132
1133
1134 } //namespace anon
1135
1136
1137 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1138 {
1139         //lyxerr << "InsetTabular::editXY: " << this << endl;
1140         cur.selection() = false;
1141         cur.push(const_cast<InsetTabular&>(*this));
1142         cur.idx() = getNearestCell(x, y);
1143         resetPos(cur);
1144         return cell(cur.idx())->text_.editXY(cur, x, y);
1145         //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1146 }
1147
1148
1149 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1150 {
1151         //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1152         cur.idx() = getNearestCell(x, y);
1153         resetPos(cur);
1154         return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1155 }
1156
1157
1158 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1159 {
1160         lyxerr << "# InsetTabular::getNearestCell()  x=" << x << " y=" << y << endl;
1161         idx_type idx_min = 0;
1162         int dist_min = std::numeric_limits<int>::max();
1163         for (idx_type i = 0; i < nargs(); ++i) {
1164                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1165                         int d = dist(*tabular.getCellInset(i), x, y);
1166                         if (d < dist_min) {
1167                                 dist_min = d;
1168                                 idx_min = i;
1169                         }
1170                 }
1171         }
1172         return idx_min;
1173 }
1174
1175
1176 int InsetTabular::getCellXPos(idx_type const cell) const
1177 {
1178         idx_type c = cell;
1179
1180         for (; !tabular.isFirstCellInRow(c); --c)
1181                 ;
1182         int lx = tabular.getWidthOfColumn(cell);
1183         for (; c < cell; ++c)
1184                 lx += tabular.getWidthOfColumn(c);
1185
1186         return lx - tabular.getWidthOfColumn(cell);
1187 }
1188
1189
1190 void InsetTabular::resetPos(LCursor & cur) const
1191 {
1192         BufferView & bv = cur.bv();
1193 //      col_type const actcol = tabular.column_of_cell(cur.idx());
1194 //      int const offset = ADD_TO_TABULAR_WIDTH + 2;
1195 //      int const new_x = getCellXPos(cur.idx()) + offset;
1196 //      int const old_x = cursorx_;
1197 //      int const col_width = tabular.getWidthOfColumn(cur.idx());
1198 //      cursorx_ = new_x;
1199 //    cursor.x(getCellXPos(cur.idx()) + offset);
1200 //      if (actcol < tabular.columns() - 1 && scroll(false) &&
1201 //              tabular.getWidthOfTabular() < bv.workWidth()-20)
1202 //      {
1203 //              scroll(bv, 0.0F);
1204 //      } else if (cursorx_ - offset > 20 &&
1205 //                 cursorx_ - offset + col_width > bv.workWidth() - 20) {
1206 //              scroll(bv, - col_width - 20);
1207 //      } else if (cursorx_ - offset < 20) {
1208 //              scroll(bv, 20 - cursorx_ + offset);
1209 //      } else if (scroll() && xo() > 20 &&
1210 //                 xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1211 //              scroll(bv, old_x - cursorx_);
1212 //      }
1213
1214         if (&cur.inset() != this) {
1215                 scroll(bv, 0.0F);
1216         } else {
1217                 int const X1 = 0;
1218                 int const X2 = bv.workWidth();
1219                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1220                 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1221                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1222
1223                 if (x1 < X1)
1224                         scroll(bv, X1 + 20 - x1);
1225                 else if (x2 > X2)
1226                         scroll(bv, X2 - 20 - x2);
1227         }
1228
1229         cur.needsUpdate();
1230
1231         InsetTabularMailer(*this).updateDialog(&bv);
1232 }
1233
1234
1235 void InsetTabular::moveNextCell(LCursor & cur)
1236 {
1237         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1238         if (isRightToLeft(cur)) {
1239                 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1240                 if (tabular.isFirstCellInRow(cur.idx())) {
1241                         row_type const row = tabular.row_of_cell(cur.idx());
1242                         if (row == tabular.rows() - 1)
1243                                 return;
1244                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1245                 } else {
1246                         if (cur.idx() == 0)
1247                                 return;
1248                         --cur.idx();
1249                 }
1250         } else {
1251                 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1252                 if (tabular.isLastCell(cur.idx()))
1253                         return;
1254                 ++cur.idx();
1255         }
1256         cur.pit() = 0;
1257         cur.pos() = 0;
1258         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1259         resetPos(cur);
1260 }
1261
1262
1263 void InsetTabular::movePrevCell(LCursor & cur)
1264 {
1265         if (isRightToLeft(cur)) {
1266                 if (tabular.isLastCellInRow(cur.idx())) {
1267                         row_type const row = tabular.row_of_cell(cur.idx());
1268                         if (row == 0)
1269                                 return;
1270                         cur.idx() = tabular.getFirstCellInRow(row);
1271                         cur.idx() = tabular.getCellAbove(cur.idx());
1272                 } else {
1273                         if (tabular.isLastCell(cur.idx()))
1274                                 return;
1275                         ++cur.idx();
1276                 }
1277         } else {
1278                 if (cur.idx() == 0) // first cell
1279                         return;
1280                 --cur.idx();
1281         }
1282         cur.pit() = cur.lastpit();
1283         cur.pos() = cur.lastpos();
1284         resetPos(cur);
1285 }
1286
1287
1288 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1289 {
1290         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1291
1292         int i = 0;
1293         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1294                 string const tmp = tabularFeature[i].feature;
1295
1296                 if (tmp == what.substr(0, tmp.length())) {
1297                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1298                         //tabularFeatures[i].feature.length())) {
1299                         action = tabularFeature[i].action;
1300                         break;
1301                 }
1302         }
1303         if (action == LyXTabular::LAST_ACTION)
1304                 return false;
1305
1306         string const val =
1307                 ltrim(what.substr(tabularFeature[i].feature.length()));
1308         tabularFeatures(cur, action, val);
1309         return true;
1310 }
1311
1312
1313 namespace {
1314
1315 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1316                           string const & special, bool & flag)
1317 {
1318         if (special == "dl_above") {
1319                 ltt.topDL = flag;
1320                 ltt.set = false;
1321         } else if (special == "dl_below") {
1322                 ltt.bottomDL = flag;
1323                 ltt.set = false;
1324         } else if (special == "empty") {
1325                 ltt.empty = flag;
1326                 ltt.set = false;
1327         } else if (flag) {
1328                 ltt.empty = false;
1329                 ltt.set = true;
1330         }
1331 }
1332
1333 } // anon namespace
1334
1335
1336 void InsetTabular::tabularFeatures(LCursor & cur,
1337         LyXTabular::Feature feature, string const & value)
1338 {
1339         BufferView & bv = cur.bv();
1340         col_type sel_col_start;
1341         col_type sel_col_end;
1342         row_type sel_row_start;
1343         row_type sel_row_end;
1344         bool setLines = false;
1345         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1346         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1347
1348         switch (feature) {
1349
1350         case LyXTabular::M_ALIGN_LEFT:
1351         case LyXTabular::ALIGN_LEFT:
1352                 setAlign = LYX_ALIGN_LEFT;
1353                 break;
1354
1355         case LyXTabular::M_ALIGN_RIGHT:
1356         case LyXTabular::ALIGN_RIGHT:
1357                 setAlign = LYX_ALIGN_RIGHT;
1358                 break;
1359
1360         case LyXTabular::M_ALIGN_CENTER:
1361         case LyXTabular::ALIGN_CENTER:
1362                 setAlign = LYX_ALIGN_CENTER;
1363                 break;
1364
1365         case LyXTabular::ALIGN_BLOCK:
1366                 setAlign = LYX_ALIGN_BLOCK;
1367                 break;
1368
1369         case LyXTabular::M_VALIGN_TOP:
1370         case LyXTabular::VALIGN_TOP:
1371                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1372                 break;
1373
1374         case LyXTabular::M_VALIGN_BOTTOM:
1375         case LyXTabular::VALIGN_BOTTOM:
1376                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1377                 break;
1378
1379         case LyXTabular::M_VALIGN_MIDDLE:
1380         case LyXTabular::VALIGN_MIDDLE:
1381                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1382                 break;
1383
1384         default:
1385                 break;
1386         }
1387
1388         recordUndo(cur, Undo::ATOMIC);
1389
1390         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1391         row_type const row = tabular.row_of_cell(cur.idx());
1392         col_type const column = tabular.column_of_cell(cur.idx());
1393         bool flag = true;
1394         LyXTabular::ltType ltt;
1395
1396         switch (feature) {
1397
1398         case LyXTabular::SET_PWIDTH: {
1399                 LyXLength const len(value);
1400                 tabular.setColumnPWidth(cur.idx(), len);
1401                 if (len.zero()
1402                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1403                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1404                 else if (!len.zero()
1405                          && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1406                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1407                 break;
1408         }
1409
1410         case LyXTabular::SET_MPWIDTH:
1411                 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1412                 break;
1413
1414         case LyXTabular::SET_SPECIAL_COLUMN:
1415         case LyXTabular::SET_SPECIAL_MULTI:
1416                 tabular.setAlignSpecial(cur.idx(),value,feature);
1417                 break;
1418
1419         case LyXTabular::APPEND_ROW:
1420                 // append the row into the tabular
1421                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1422                 break;
1423
1424         case LyXTabular::APPEND_COLUMN:
1425                 // append the column into the tabular
1426                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1427                 break;
1428
1429         case LyXTabular::DELETE_ROW:
1430                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1431                         tabular.deleteRow(sel_row_start);
1432                 if (sel_row_start >= tabular.rows())
1433                         --sel_row_start;
1434                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1435                 cur.pit() = 0;
1436                 cur.pos() = 0;
1437                 cur.selection() = false;
1438                 break;
1439
1440         case LyXTabular::DELETE_COLUMN:
1441                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1442                         tabular.deleteColumn(sel_col_start);
1443                 if (sel_col_start >= tabular.columns())
1444                         --sel_col_start;
1445                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1446                 cur.pit() = 0;
1447                 cur.pos() = 0;
1448                 cur.selection() = false;
1449                 break;
1450
1451         case LyXTabular::M_TOGGLE_LINE_TOP:
1452                 flag = false;
1453         case LyXTabular::TOGGLE_LINE_TOP: {
1454                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1455                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1456                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1457                                 tabular.setTopLine(
1458                                         tabular.getCellNumber(i, j),
1459                                         lineSet, flag);
1460                 break;
1461         }
1462
1463         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1464                 flag = false;
1465         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1466                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1467                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1468                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1469                                 tabular.setBottomLine(
1470                                         tabular.getCellNumber(i, j),
1471                                         lineSet,
1472                                         flag);
1473                 break;
1474         }
1475
1476         case LyXTabular::M_TOGGLE_LINE_LEFT:
1477                 flag = false;
1478         case LyXTabular::TOGGLE_LINE_LEFT: {
1479                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1480                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482                                 tabular.setLeftLine(
1483                                         tabular.getCellNumber(i,j),
1484                                         lineSet,
1485                                         flag);
1486                 break;
1487         }
1488
1489         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1490                 flag = false;
1491         case LyXTabular::TOGGLE_LINE_RIGHT: {
1492                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1493                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495                                 tabular.setRightLine(
1496                                         tabular.getCellNumber(i,j),
1497                                         lineSet,
1498                                         flag);
1499                 break;
1500         }
1501
1502         case LyXTabular::M_ALIGN_LEFT:
1503         case LyXTabular::M_ALIGN_RIGHT:
1504         case LyXTabular::M_ALIGN_CENTER:
1505                 flag = false;
1506         case LyXTabular::ALIGN_LEFT:
1507         case LyXTabular::ALIGN_RIGHT:
1508         case LyXTabular::ALIGN_CENTER:
1509         case LyXTabular::ALIGN_BLOCK:
1510                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1511                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1512                                 tabular.setAlignment(
1513                                         tabular.getCellNumber(i, j),
1514                                         setAlign,
1515                                         flag);
1516                 break;
1517
1518         case LyXTabular::M_VALIGN_TOP:
1519         case LyXTabular::M_VALIGN_BOTTOM:
1520         case LyXTabular::M_VALIGN_MIDDLE:
1521                 flag = false;
1522         case LyXTabular::VALIGN_TOP:
1523         case LyXTabular::VALIGN_BOTTOM:
1524         case LyXTabular::VALIGN_MIDDLE:
1525                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1526                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1527                                 tabular.setVAlignment(
1528                                         tabular.getCellNumber(i, j),
1529                                         setVAlign, flag);
1530                 break;
1531
1532         case LyXTabular::MULTICOLUMN: {
1533                 if (sel_row_start != sel_row_end) {
1534 #ifdef WITH_WARNINGS
1535 #warning Need I say it ? This is horrible.
1536 #endif
1537                         Alert::error(_("Error setting multicolumn"),
1538                                    _("You cannot set multicolumn vertically."));
1539                         return;
1540                 }
1541                 if (!cur.selection()) {
1542                         // just multicol for one single cell
1543                         // check whether we are completely in a multicol
1544                         if (tabular.isMultiColumn(cur.idx()))
1545                                 tabular.unsetMultiColumn(cur.idx());
1546                         else
1547                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1548                         break;
1549                 }
1550                 // we have a selection so this means we just add all this
1551                 // cells to form a multicolumn cell
1552                 idx_type const s_start = cur.selBegin().idx();
1553                 idx_type const s_end = cur.selEnd().idx();
1554                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1555                 cur.idx() = s_start;
1556                 cur.pit() = 0;
1557                 cur.pos() = 0;
1558                 cur.selection() = false;
1559                 break;
1560         }
1561
1562         case LyXTabular::SET_ALL_LINES:
1563                 setLines = true;
1564         case LyXTabular::UNSET_ALL_LINES:
1565                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1566                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1567                                 tabular.setAllLines(
1568                                         tabular.getCellNumber(i,j), setLines);
1569                 break;
1570
1571         case LyXTabular::SET_LONGTABULAR:
1572                 tabular.setLongTabular(true);
1573                 break;
1574
1575         case LyXTabular::UNSET_LONGTABULAR:
1576                 tabular.setLongTabular(false);
1577                 break;
1578
1579         case LyXTabular::SET_ROTATE_TABULAR:
1580                 tabular.setRotateTabular(true);
1581                 break;
1582
1583         case LyXTabular::UNSET_ROTATE_TABULAR:
1584                 tabular.setRotateTabular(false);
1585                 break;
1586
1587         case LyXTabular::SET_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), true);
1592                 break;
1593
1594         case LyXTabular::UNSET_ROTATE_CELL:
1595                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1596                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1597                                 tabular.setRotateCell(
1598                                         tabular.getCellNumber(i, j), false);
1599                 break;
1600
1601         case LyXTabular::SET_USEBOX: {
1602                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1603                 if (val == tabular.getUsebox(cur.idx()))
1604                         val = LyXTabular::BOX_NONE;
1605                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1606                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1607                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1608                 break;
1609         }
1610
1611         case LyXTabular::UNSET_LTFIRSTHEAD:
1612                 flag = false;
1613         case LyXTabular::SET_LTFIRSTHEAD:
1614                 tabular.getRowOfLTFirstHead(row, ltt);
1615                 checkLongtableSpecial(ltt, value, flag);
1616                 tabular.setLTHead(row, flag, ltt, true);
1617                 break;
1618
1619         case LyXTabular::UNSET_LTHEAD:
1620                 flag = false;
1621         case LyXTabular::SET_LTHEAD:
1622                 tabular.getRowOfLTHead(row, ltt);
1623                 checkLongtableSpecial(ltt, value, flag);
1624                 tabular.setLTHead(row, flag, ltt, false);
1625                 break;
1626
1627         case LyXTabular::UNSET_LTFOOT:
1628                 flag = false;
1629         case LyXTabular::SET_LTFOOT:
1630                 tabular.getRowOfLTFoot(row, ltt);
1631                 checkLongtableSpecial(ltt, value, flag);
1632                 tabular.setLTFoot(row, flag, ltt, false);
1633                 break;
1634
1635         case LyXTabular::UNSET_LTLASTFOOT:
1636                 flag = false;
1637         case LyXTabular::SET_LTLASTFOOT:
1638                 tabular.getRowOfLTLastFoot(row, ltt);
1639                 checkLongtableSpecial(ltt, value, flag);
1640                 tabular.setLTFoot(row, flag, ltt, true);
1641                 break;
1642
1643         case LyXTabular::SET_LTNEWPAGE:
1644                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1645                 break;
1646
1647         // dummy stuff just to avoid warnings
1648         case LyXTabular::LAST_ACTION:
1649                 break;
1650         }
1651
1652         InsetTabularMailer(*this).updateDialog(&bv);
1653 }
1654
1655
1656 bool InsetTabular::showInsetDialog(BufferView * bv) const
1657 {
1658         InsetTabularMailer(*this).showDialog(bv);
1659         return true;
1660 }
1661
1662
1663 void InsetTabular::openLayoutDialog(BufferView * bv) const
1664 {
1665         InsetTabularMailer(*this).showDialog(bv);
1666 }
1667
1668
1669 void InsetTabular::getLabelList(Buffer const & buffer,
1670                                 vector<string> & list) const
1671 {
1672         tabular.getLabelList(buffer, list);
1673 }
1674
1675
1676 bool InsetTabular::copySelection(LCursor & cur)
1677 {
1678         if (!cur.selection())
1679                 return false;
1680
1681         row_type rs, re;
1682         col_type cs, ce;
1683         getSelection(cur, rs, re, cs, ce);
1684
1685         paste_tabular.reset(new LyXTabular(tabular));
1686
1687         for (row_type i = 0; i < rs; ++i)
1688                 paste_tabular->deleteRow(0);
1689
1690         row_type const rows = re - rs + 1;
1691         while (paste_tabular->rows() > rows)
1692                 paste_tabular->deleteRow(rows);
1693
1694         paste_tabular->setTopLine(0, true, true);
1695         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1696                                      true, true);
1697
1698         for (col_type i = 0; i < cs; ++i)
1699                 paste_tabular->deleteColumn(0);
1700
1701         col_type const columns = ce - cs + 1;
1702         while (paste_tabular->columns() > columns)
1703                 paste_tabular->deleteColumn(columns);
1704
1705         paste_tabular->setLeftLine(0, true, true);
1706         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1707                                     true, true);
1708
1709         ostringstream os;
1710         OutputParams const runparams;
1711         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1712         cur.bv().stuffClipboard(os.str());
1713         return true;
1714 }
1715
1716
1717 bool InsetTabular::pasteSelection(LCursor & cur)
1718 {
1719         if (!paste_tabular)
1720                 return false;
1721         col_type const actcol = tabular.column_of_cell(cur.idx());
1722         row_type const actrow = tabular.row_of_cell(cur.idx());
1723         for (row_type r1 = 0, r2 = actrow;
1724              r1 < paste_tabular->rows() && r2 < tabular.rows();
1725              ++r1, ++r2) {
1726                 for (col_type c1 = 0, c2 = actcol;
1727                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1728                     ++c1, ++c2) {
1729                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1730                             tabular.isPartOfMultiColumn(r2, c2))
1731                                 continue;
1732                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1733                                 --c2;
1734                                 continue;
1735                         }
1736                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1737                                 --c1;
1738                                 continue;
1739                         }
1740                         shared_ptr<InsetText> inset = tabular.getCellInset(r2, c2);
1741                         inset = paste_tabular->getCellInset(r1, c1);
1742                         inset->markNew();
1743                 }
1744         }
1745         return true;
1746 }
1747
1748
1749 void InsetTabular::cutSelection(LCursor & cur)
1750 {
1751         if (!cur.selection())
1752                 return;
1753
1754         bool const track = cur.buffer().params().tracking_changes;
1755         row_type rs, re;
1756         col_type cs, ce;
1757         getSelection(cur, rs, re, cs, ce);
1758         for (row_type i = rs; i <= re; ++i)
1759                 for (col_type j = cs; j <= ce; ++j)
1760                         cell(tabular.getCellNumber(i, j))->clear(track);
1761
1762         // cursor position might be invalid now
1763         cur.pos() = cur.lastpos();
1764         cur.clearSelection();
1765 }
1766
1767
1768 bool InsetTabular::isRightToLeft(LCursor & cur) const
1769 {
1770         BOOST_ASSERT(cur.depth() > 1);
1771         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1772         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1773         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1774                                          parentpos).language()->RightToLeft();
1775 }
1776
1777
1778 void InsetTabular::getSelection(LCursor & cur,
1779         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1780 {
1781         CursorSlice const & beg = cur.selBegin();
1782         CursorSlice const & end = cur.selEnd();
1783         cs = tabular.column_of_cell(beg.idx());
1784         ce = tabular.column_of_cell(end.idx());
1785         if (cs > ce) {
1786                 ce = cs;
1787                 cs = tabular.column_of_cell(end.idx());
1788         } else {
1789                 ce = tabular.right_column_of_cell(end.idx());
1790         }
1791
1792         rs = tabular.row_of_cell(beg.idx());
1793         re = tabular.row_of_cell(end.idx());
1794         if (rs > re)
1795                 swap(rs, re);
1796 }
1797
1798
1799 size_t InsetTabular::nargs() const
1800 {
1801         return tabular.getNumberOfCells();
1802 }
1803
1804
1805 LyXText * InsetTabular::getText(int idx) const
1806 {
1807         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1808 }
1809
1810
1811 void InsetTabular::markErased()
1812 {
1813         for (idx_type idx = 0; idx < nargs(); ++idx)
1814                 cell(idx)->markErased();
1815 }
1816
1817
1818 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1819 {
1820 #if 0
1821         idx_type const cell = tabular.getCellFromInset(in);
1822         // FIXME: getCellFromInset() returns now always a valid cell, so
1823         // the stuff below can be deleted, and instead we have:
1824         return tabular.getPWidth(cell).zero();
1825
1826         if (cell != npos)
1827                 return tabular.getPWidth(cell).zero();
1828
1829         // this is a workaround for a crash (New, Insert->Tabular,
1830         // Insert->FootNote)
1831         if (!owner())
1832                 return false;
1833
1834         // well we didn't obviously find it so maybe our owner knows more
1835         BOOST_ASSERT(owner());
1836         return owner()->forceDefaultParagraphs(in);
1837 #endif
1838         return false;
1839 }
1840
1841
1842 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1843                                      bool usePaste)
1844 {
1845         if (buf.length() <= 0)
1846                 return true;
1847
1848         col_type cols = 1;
1849         row_type rows = 1;
1850         col_type maxCols = 1;
1851         string::size_type const len = buf.length();
1852         string::size_type p = 0;
1853
1854         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1855                 switch (buf[p]) {
1856                 case '\t':
1857                         ++cols;
1858                         break;
1859                 case '\n':
1860                         if (p + 1 < len)
1861                                 ++rows;
1862                         maxCols = max(cols, maxCols);
1863                         cols = 1;
1864                         break;
1865                 }
1866                 ++p;
1867         }
1868         maxCols = max(cols, maxCols);
1869         LyXTabular * loctab;
1870         idx_type cell = 0;
1871         col_type ocol = 0;
1872         row_type row = 0;
1873         if (usePaste) {
1874                 paste_tabular.reset(
1875                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1876                 loctab = paste_tabular.get();
1877                 cols = 0;
1878         } else {
1879                 loctab = &tabular;
1880                 cell = bv.cursor().idx();
1881                 ocol = tabular.column_of_cell(cell);
1882                 row = tabular.row_of_cell(cell);
1883         }
1884
1885         string::size_type op = 0;
1886         idx_type const cells = loctab->getNumberOfCells();
1887         p = 0;
1888         cols = ocol;
1889         rows = loctab->rows();
1890         col_type const columns = loctab->columns();
1891
1892         while (cell < cells && p < len && row < rows &&
1893                (p = buf.find_first_of("\t\n", p)) != string::npos)
1894         {
1895                 if (p >= len)
1896                         break;
1897                 switch (buf[p]) {
1898                 case '\t':
1899                         // we can only set this if we are not too far right
1900                         if (cols < columns) {
1901                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1902                                 Paragraph & par = inset->text_.getPar(0);
1903                                 LyXFont const font = inset->text_.getFont(par, 0);
1904                                 inset->setText(buf.substr(op, p - op), font);
1905                                 ++cols;
1906                                 ++cell;
1907                         }
1908                         break;
1909                 case '\n':
1910                         // we can only set this if we are not too far right
1911                         if (cols < columns) {
1912                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1913                                 Paragraph & par = inset->text_.getPar(0);
1914                                 LyXFont const font = inset->text_.getFont(par, 0);
1915                                 inset->setText(buf.substr(op, p - op), font);
1916                         }
1917                         cols = ocol;
1918                         ++row;
1919                         if (row < rows)
1920                                 cell = loctab->getCellNumber(row, cols);
1921                         break;
1922                 }
1923                 ++p;
1924                 op = p;
1925         }
1926         // check for the last cell if there is no trailing '\n'
1927         if (cell < cells && op < len) {
1928                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1929                 Paragraph & par = inset->text_.getPar(0);
1930                 LyXFont const font = inset->text_.getFont(par, 0);
1931                 inset->setText(buf.substr(op, len - op), font);
1932         }
1933         return true;
1934 }
1935
1936
1937 void InsetTabular::addPreview(PreviewLoader & loader) const
1938 {
1939         row_type const rows = tabular.rows();
1940         col_type const columns = tabular.columns();
1941         for (row_type i = 0; i < rows; ++i) {
1942                 for (col_type j = 0; j < columns; ++j)
1943                         tabular.getCellInset(i, j)->addPreview(loader);
1944         }
1945 }
1946
1947
1948 bool InsetTabular::tablemode(LCursor & cur) const
1949 {
1950         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1951 }
1952
1953
1954
1955
1956
1957 string const InsetTabularMailer::name_("tabular");
1958
1959 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1960         : inset_(const_cast<InsetTabular &>(inset))
1961 {}
1962
1963
1964 string const InsetTabularMailer::inset2string(Buffer const &) const
1965 {
1966         return params2string(inset_);
1967 }
1968
1969
1970 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1971 {
1972         istringstream data(in);
1973         LyXLex lex(0,0);
1974         lex.setStream(data);
1975
1976         if (in.empty())
1977                 return;
1978
1979         string token;
1980         lex >> token;
1981         if (!lex || token != name_)
1982                 return print_mailer_error("InsetTabularMailer", in, 1,
1983                                           name_);
1984
1985         // This is part of the inset proper that is usually swallowed
1986         // by Buffer::readInset
1987         lex >> token;
1988         if (!lex || token != "Tabular")
1989                 return print_mailer_error("InsetTabularMailer", in, 2,
1990                                           "Tabular");
1991
1992         Buffer const & buffer = inset.buffer();
1993         inset.read(buffer, lex);
1994 }
1995
1996
1997 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1998 {
1999         ostringstream data;
2000         data << name_ << ' ';
2001         inset.write(inset.buffer(), data);
2002         data << "\\end_inset\n";
2003         return data.str();
2004 }