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