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