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