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