]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
src/insets/insettabular (doDispatch, insertAsciiString): fix insertion of \t- and...
[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)), buf.text().bv()), 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, &cur.bv()));
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                 break;
749
750         case LFUN_EMPH:
751         case LFUN_BOLD:
752         case LFUN_ROMAN:
753         case LFUN_NOUN:
754         case LFUN_ITAL:
755         case LFUN_FRAK:
756         case LFUN_CODE:
757         case LFUN_SANS:
758         case LFUN_FREEFONT_APPLY:
759         case LFUN_FREEFONT_UPDATE:
760         case LFUN_FONT_SIZE:
761         case LFUN_UNDERLINE:
762         case LFUN_LANGUAGE:
763         case LFUN_CAPITALIZE_WORD:
764         case LFUN_UPCASE_WORD:
765         case LFUN_LOWCASE_WORD:
766         case LFUN_TRANSPOSE_CHARS:
767                 if (tablemode(cur)) {
768                         row_type rs, re;
769                         col_type cs, ce;
770                         getSelection(cur, rs, re, cs, ce);
771                         for (row_type i = rs; i <= re; ++i) {
772                                 for (col_type j = cs; j <= ce; ++j) {
773                                         // cursor follows cell:
774                                         cur.idx() = tabular.getCellNumber(i, j);
775                                         // select this cell only:
776                                         cur.pos() = 0;
777                                         cur.resetAnchor();
778                                         cur.pos() = cur.top().lastpos();
779                                         cur.setCursor(cur);
780                                         cur.setSelection();
781                                         cell(cur.idx())->dispatch(cur, cmd);
782                                 }
783                         }
784                         // Restore original selection
785                         cur.idx() = tabular.getCellNumber(rs, cs);
786                         cur.pos() = 0;
787                         cur.resetAnchor();
788                         cur.idx() = tabular.getCellNumber(re, ce);
789                         cur.pos() = cur.top().lastpos();
790                         cur.setCursor(cur);
791                         cur.setSelection();
792                         break;
793                 } else {
794                         cell(cur.idx())->dispatch(cur, cmd);
795                         break;
796                 }
797         default:
798                 // we try to handle this event in the insets dispatch function.
799                 cell(cur.idx())->dispatch(cur, cmd);
800                 break;
801         }
802
803         resetPos(cur);
804         InsetTabularMailer(*this).updateDialog(&cur.bv());
805 }
806
807
808 // function sets an object as defined in func_status.h:
809 // states OK, Unknown, Disabled, On, Off.
810 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
811         FuncStatus & status) const
812 {
813         switch (cmd.action) {
814         case LFUN_TABULAR_FEATURE: {
815                 int action = LyXTabular::LAST_ACTION;
816                 int i = 0;
817                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
818                         string const tmp = tabularFeature[i].feature;
819                         if (tmp == cmd.argument.substr(0, tmp.length())) {
820                                 action = tabularFeature[i].action;
821                                 break;
822                         }
823                 }
824                 if (action == LyXTabular::LAST_ACTION) {
825                         status.clear();
826                         status.unknown(true);
827                         return true;
828                 }
829
830                 string const argument
831                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
832
833                 row_type sel_row_start = 0;
834                 row_type sel_row_end = 0;
835                 col_type dummy;
836                 LyXTabular::ltType dummyltt;
837                 bool flag = true;
838
839                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
840
841                 switch (action) {
842                 case LyXTabular::SET_PWIDTH:
843                 case LyXTabular::SET_MPWIDTH:
844                 case LyXTabular::SET_SPECIAL_COLUMN:
845                 case LyXTabular::SET_SPECIAL_MULTI:
846                 case LyXTabular::APPEND_ROW:
847                 case LyXTabular::APPEND_COLUMN:
848                 case LyXTabular::DELETE_ROW:
849                 case LyXTabular::DELETE_COLUMN:
850                 case LyXTabular::SET_ALL_LINES:
851                 case LyXTabular::UNSET_ALL_LINES:
852                         status.clear();
853                         return true;
854
855                 case LyXTabular::MULTICOLUMN:
856                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
857                         break;
858
859                 case LyXTabular::M_TOGGLE_LINE_TOP:
860                         flag = false;
861                 case LyXTabular::TOGGLE_LINE_TOP:
862                         status.setOnOff(tabular.topLine(cur.idx(), flag));
863                         break;
864
865                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
866                         flag = false;
867                 case LyXTabular::TOGGLE_LINE_BOTTOM:
868                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
869                         break;
870
871                 case LyXTabular::M_TOGGLE_LINE_LEFT:
872                         flag = false;
873                 case LyXTabular::TOGGLE_LINE_LEFT:
874                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
875                         break;
876
877                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
878                         flag = false;
879                 case LyXTabular::TOGGLE_LINE_RIGHT:
880                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
881                         break;
882
883                 case LyXTabular::M_ALIGN_LEFT:
884                         flag = false;
885                 case LyXTabular::ALIGN_LEFT:
886                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
887                         break;
888
889                 case LyXTabular::M_ALIGN_RIGHT:
890                         flag = false;
891                 case LyXTabular::ALIGN_RIGHT:
892                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
893                         break;
894
895                 case LyXTabular::M_ALIGN_CENTER:
896                         flag = false;
897                 case LyXTabular::ALIGN_CENTER:
898                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
899                         break;
900
901                 case LyXTabular::ALIGN_BLOCK:
902                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
903                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
904                         break;
905
906                 case LyXTabular::M_VALIGN_TOP:
907                         flag = false;
908                 case LyXTabular::VALIGN_TOP:
909                         status.setOnOff(
910                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
911                         break;
912
913                 case LyXTabular::M_VALIGN_BOTTOM:
914                         flag = false;
915                 case LyXTabular::VALIGN_BOTTOM:
916                         status.setOnOff(
917                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
918                         break;
919
920                 case LyXTabular::M_VALIGN_MIDDLE:
921                         flag = false;
922                 case LyXTabular::VALIGN_MIDDLE:
923                         status.setOnOff(
924                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
925                         break;
926
927                 case LyXTabular::SET_LONGTABULAR:
928                         status.setOnOff(tabular.isLongTabular());
929                         break;
930
931                 case LyXTabular::UNSET_LONGTABULAR:
932                         status.setOnOff(!tabular.isLongTabular());
933                         break;
934
935                 case LyXTabular::SET_ROTATE_TABULAR:
936                         status.setOnOff(tabular.getRotateTabular());
937                         break;
938
939                 case LyXTabular::UNSET_ROTATE_TABULAR:
940                         status.setOnOff(!tabular.getRotateTabular());
941                         break;
942
943                 case LyXTabular::SET_ROTATE_CELL:
944                         status.setOnOff(tabular.getRotateCell(cur.idx()));
945                         break;
946
947                 case LyXTabular::UNSET_ROTATE_CELL:
948                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
949                         break;
950
951                 case LyXTabular::SET_USEBOX:
952                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
953                         break;
954
955                 case LyXTabular::SET_LTFIRSTHEAD:
956                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
957                         break;
958
959                 case LyXTabular::UNSET_LTFIRSTHEAD:
960                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
961                         break;
962
963                 case LyXTabular::SET_LTHEAD:
964                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
965                         break;
966
967                 case LyXTabular::UNSET_LTHEAD:
968                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
969                         break;
970
971                 case LyXTabular::SET_LTFOOT:
972                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
973                         break;
974
975                 case LyXTabular::UNSET_LTFOOT:
976                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
977                         break;
978
979                 case LyXTabular::SET_LTLASTFOOT:
980                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
981                         break;
982
983                 case LyXTabular::UNSET_LTLASTFOOT:
984                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
985                         break;
986
987                 case LyXTabular::SET_LTNEWPAGE:
988                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
989                         break;
990
991                 default:
992                         status.clear();
993                         status.enabled(false);
994                         break;
995                 }
996                 return true;
997         }
998
999         // These are only enabled inside tabular
1000         case LFUN_CELL_BACKWARD:
1001         case LFUN_CELL_FORWARD:
1002                 status.enabled(true);
1003                 return true;
1004
1005         // disable these with multiple cells selected
1006         case LFUN_INSET_INSERT:
1007         case LFUN_TABULAR_INSERT:
1008         case LFUN_INSERT_CHARSTYLE:
1009         case LFUN_INSET_FLOAT:
1010         case LFUN_INSET_WIDE_FLOAT:
1011         case LFUN_INSET_FOOTNOTE:
1012         case LFUN_INSET_MARGINAL:
1013         case LFUN_INSERT_MATH:
1014         case LFUN_MATH_MODE:
1015         case LFUN_MATH_MUTATE:
1016         case LFUN_MATH_DISPLAY:
1017         case LFUN_INSERT_NOTE:
1018         case LFUN_INSET_OPTARG:
1019         case LFUN_INSERT_BOX:
1020         case LFUN_INSERT_BRANCH:
1021         case LFUN_INSET_WRAP:
1022         case LFUN_INSET_ERT: {
1023                 if (tablemode(cur)) {
1024                         status.enabled(false);
1025                         return true;
1026                 } else
1027                         return cell(cur.idx())->getStatus(cur, cmd, status);
1028         }
1029
1030         // disable in non-fixed-width cells
1031         case LFUN_BREAKLINE:
1032         case LFUN_BREAKPARAGRAPH:
1033         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1034         case LFUN_BREAKPARAGRAPH_SKIP: {
1035                 if (tabular.getPWidth(cur.idx()).zero()) {
1036                         status.enabled(false);
1037                         return true;
1038                 } else
1039                         return cell(cur.idx())->getStatus(cur, cmd, status);
1040         }
1041
1042         case LFUN_PASTE:
1043                 if (tabularStackDirty()) {
1044                         status.enabled(true);
1045                         return true;
1046                 }
1047
1048         case LFUN_INSET_MODIFY:
1049                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1050                         status.enabled(true);
1051                         return true;
1052                 }
1053                 // Fall through
1054
1055         default:
1056                 // we try to handle this event in the insets dispatch function.
1057                 return cell(cur.idx())->getStatus(cur, cmd, status);
1058         }
1059 }
1060
1061
1062 int InsetTabular::latex(Buffer const & buf, ostream & os,
1063                         OutputParams const & runparams) const
1064 {
1065         return tabular.latex(buf, os, runparams);
1066 }
1067
1068
1069 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1070                         OutputParams const & runparams) const
1071 {
1072         int const dp = runparams.linelen ? runparams.depth : 0;
1073         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1074 }
1075
1076
1077 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1078                            OutputParams const & runparams) const
1079 {
1080         return tabular.linuxdoc(buf,os, runparams);
1081 }
1082
1083
1084 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1085                           OutputParams const & runparams) const
1086 {
1087         int ret = 0;
1088         InsetBase * master = 0;
1089
1090 #ifdef WITH_WARNINGS
1091 #warning Why not pass a proper DocIterator here?
1092 #endif
1093 #if 0
1094         // if the table is inside a float it doesn't need the informaltable
1095         // wrapper. Search for it.
1096         for (master = owner(); master; master = master->owner())
1097                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1098                         break;
1099 #endif
1100
1101         if (!master) {
1102                 os << "<informaltable>";
1103                 ++ret;
1104         }
1105         ret += tabular.docbook(buf, os, runparams);
1106         if (!master) {
1107                 os << "</informaltable>";
1108                 ++ret;
1109         }
1110         return ret;
1111 }
1112
1113
1114 void InsetTabular::validate(LaTeXFeatures & features) const
1115 {
1116         tabular.validate(features);
1117 }
1118
1119
1120 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1121 {
1122         return tabular.getCellInset(idx);
1123 }
1124
1125
1126 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1127 {
1128         return tabular.getCellInset(idx);
1129 }
1130
1131
1132 void InsetTabular::cursorPos
1133         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1134 {
1135         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1136
1137         // y offset     correction
1138         int const row = tabular.row_of_cell(sl.idx());
1139         for (int i = 0; i <= row; ++i) {
1140                 if (i != 0) {
1141                         y += tabular.getAscentOfRow(i);
1142                         y += tabular.getAdditionalHeight(i);
1143                 }
1144                 if (i != row)
1145                         y += tabular.getDescentOfRow(i);
1146         }
1147
1148         // x offset correction
1149         int const col = tabular.column_of_cell(sl.idx());
1150         int idx = tabular.getCellNumber(row, 0);
1151         for (int j = 0; j < col; ++j) {
1152                 if (tabular.isPartOfMultiColumn(row, j))
1153                         continue;
1154                 x += tabular.getWidthOfColumn(idx);
1155                 ++idx;
1156         }
1157         x += tabular.getBeginningOfTextInCell(idx);
1158         x += ADD_TO_TABULAR_WIDTH;
1159         x += scx_;
1160 }
1161
1162
1163 int InsetTabular::dist(idx_type const cell, int x, int y) const
1164 {
1165         int xx = 0;
1166         int yy = 0;
1167         InsetBase const & inset = *tabular.getCellInset(cell);
1168         Point o = theCoords.getInsets().xy(&inset);
1169         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1170         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1171         int const ybeg = o.y_ - inset.ascent();
1172         row_type const row = tabular.row_of_cell(cell);
1173         int const rowheight = tabular.getAscentOfRow(row)
1174                         + tabular.getDescentOfRow(row);
1175         int const yend = ybeg + rowheight;
1176
1177         if (x < xbeg)
1178                 xx = xbeg - x;
1179         else if (x > xend)
1180                 xx = x - xend;
1181
1182         if (y < ybeg) 
1183                 yy = ybeg - y;
1184         else if (y > yend) 
1185                 yy = y - yend;
1186
1187         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1188         //       << " ybeg=" << ybeg << " yend=" << yend
1189         //       << " xx=" << xx << " yy=" << yy
1190         //       << " dist=" << xx + yy << endl;
1191         return xx + yy;
1192 }
1193
1194
1195 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1196 {
1197         //lyxerr << "InsetTabular::editXY: " << this << endl;
1198         cur.selection() = false;
1199         cur.push(*this);
1200         cur.idx() = getNearestCell(x, y);
1201         resetPos(cur);
1202         return cell(cur.idx())->text_.editXY(cur, x, y);
1203 }
1204
1205
1206 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1207 {
1208         cur.idx() = getNearestCell(x, y);
1209         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1210 }
1211
1212
1213 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1214 {
1215         idx_type idx_min = 0;
1216         int dist_min = std::numeric_limits<int>::max();
1217         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1218                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1219                         int const d = dist(i, x, y);
1220                         if (d < dist_min) {
1221                                 dist_min = d;
1222                                 idx_min = i;
1223                         }
1224                 }
1225         }
1226         return idx_min;
1227 }
1228
1229
1230 int InsetTabular::getCellXPos(idx_type const cell) const
1231 {
1232         idx_type c = cell;
1233
1234         for (; !tabular.isFirstCellInRow(c); --c)
1235                 ;
1236         int lx = tabular.getWidthOfColumn(cell);
1237         for (; c < cell; ++c)
1238                 lx += tabular.getWidthOfColumn(c);
1239
1240         return lx - tabular.getWidthOfColumn(cell);
1241 }
1242
1243
1244 void InsetTabular::resetPos(LCursor & cur) const
1245 {
1246         BufferView & bv = cur.bv();
1247         int const maxwidth = bv.workWidth();
1248
1249         if (&cur.inset() != this) {
1250                 scx_ = 0;
1251         } else {
1252                 int const X1 = 0;
1253                 int const X2 = maxwidth;
1254                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1255                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1256                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1257
1258                 if (x1 < X1)
1259                         scx_ = X1 + 20 - x1;
1260                 else if (x2 > X2)
1261                         scx_ = X2 - 20 - x2;
1262                 else
1263                         scx_ = 0;
1264         }
1265
1266         cur.needsUpdate();
1267
1268         InsetTabularMailer(*this).updateDialog(&bv);
1269 }
1270
1271
1272 void InsetTabular::moveNextCell(LCursor & cur)
1273 {
1274         if (isRightToLeft(cur)) {
1275                 if (tabular.isFirstCellInRow(cur.idx())) {
1276                         row_type const row = tabular.row_of_cell(cur.idx());
1277                         if (row == tabular.rows() - 1)
1278                                 return;
1279                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1280                 } else {
1281                         if (cur.idx() == 0)
1282                                 return;
1283                         --cur.idx();
1284                 }
1285         } else {
1286                 if (tabular.isLastCell(cur.idx()))
1287                         return;
1288                 ++cur.idx();
1289         }
1290         cur.pit() = 0;
1291         cur.pos() = 0;
1292         resetPos(cur);
1293 }
1294
1295
1296 void InsetTabular::movePrevCell(LCursor & cur)
1297 {
1298         if (isRightToLeft(cur)) {
1299                 if (tabular.isLastCellInRow(cur.idx())) {
1300                         row_type const row = tabular.row_of_cell(cur.idx());
1301                         if (row == 0)
1302                                 return;
1303                         cur.idx() = tabular.getFirstCellInRow(row);
1304                         cur.idx() = tabular.getCellAbove(cur.idx());
1305                 } else {
1306                         if (tabular.isLastCell(cur.idx()))
1307                                 return;
1308                         ++cur.idx();
1309                 }
1310         } else {
1311                 if (cur.idx() == 0) // first cell
1312                         return;
1313                 --cur.idx();
1314         }
1315         cur.pit() = cur.lastpit();
1316         cur.pos() = cur.lastpos();
1317         resetPos(cur);
1318 }
1319
1320
1321 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1322 {
1323         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1324
1325         int i = 0;
1326         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1327                 string const tmp = tabularFeature[i].feature;
1328
1329                 if (tmp == what.substr(0, tmp.length())) {
1330                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1331                         //tabularFeatures[i].feature.length())) {
1332                         action = tabularFeature[i].action;
1333                         break;
1334                 }
1335         }
1336         if (action == LyXTabular::LAST_ACTION)
1337                 return false;
1338
1339         string const val =
1340                 ltrim(what.substr(tabularFeature[i].feature.length()));
1341         tabularFeatures(cur, action, val);
1342         return true;
1343 }
1344
1345
1346 namespace {
1347
1348 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1349                           string const & special, bool & flag)
1350 {
1351         if (special == "dl_above") {
1352                 ltt.topDL = flag;
1353                 ltt.set = false;
1354         } else if (special == "dl_below") {
1355                 ltt.bottomDL = flag;
1356                 ltt.set = false;
1357         } else if (special == "empty") {
1358                 ltt.empty = flag;
1359                 ltt.set = false;
1360         } else if (flag) {
1361                 ltt.empty = false;
1362                 ltt.set = true;
1363         }
1364 }
1365
1366 } // anon namespace
1367
1368
1369 void InsetTabular::tabularFeatures(LCursor & cur,
1370         LyXTabular::Feature feature, string const & value)
1371 {
1372         BufferView & bv = cur.bv();
1373         col_type sel_col_start;
1374         col_type sel_col_end;
1375         row_type sel_row_start;
1376         row_type sel_row_end;
1377         bool setLines = false;
1378         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1379         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1380
1381         switch (feature) {
1382
1383         case LyXTabular::M_ALIGN_LEFT:
1384         case LyXTabular::ALIGN_LEFT:
1385                 setAlign = LYX_ALIGN_LEFT;
1386                 break;
1387
1388         case LyXTabular::M_ALIGN_RIGHT:
1389         case LyXTabular::ALIGN_RIGHT:
1390                 setAlign = LYX_ALIGN_RIGHT;
1391                 break;
1392
1393         case LyXTabular::M_ALIGN_CENTER:
1394         case LyXTabular::ALIGN_CENTER:
1395                 setAlign = LYX_ALIGN_CENTER;
1396                 break;
1397
1398         case LyXTabular::ALIGN_BLOCK:
1399                 setAlign = LYX_ALIGN_BLOCK;
1400                 break;
1401
1402         case LyXTabular::M_VALIGN_TOP:
1403         case LyXTabular::VALIGN_TOP:
1404                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1405                 break;
1406
1407         case LyXTabular::M_VALIGN_BOTTOM:
1408         case LyXTabular::VALIGN_BOTTOM:
1409                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1410                 break;
1411
1412         case LyXTabular::M_VALIGN_MIDDLE:
1413         case LyXTabular::VALIGN_MIDDLE:
1414                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1415                 break;
1416
1417         default:
1418                 break;
1419         }
1420
1421         recordUndoInset(cur, Undo::ATOMIC);
1422
1423         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1424         row_type const row = tabular.row_of_cell(cur.idx());
1425         col_type const column = tabular.column_of_cell(cur.idx());
1426         bool flag = true;
1427         LyXTabular::ltType ltt;
1428
1429         switch (feature) {
1430
1431         case LyXTabular::SET_PWIDTH: {
1432                 LyXLength const len(value);
1433                 tabular.setColumnPWidth(cur, cur.idx(), len);
1434                 if (len.zero()
1435                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1436                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1437                 break;
1438         }
1439
1440         case LyXTabular::SET_MPWIDTH:
1441                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1442                 break;
1443
1444         case LyXTabular::SET_SPECIAL_COLUMN:
1445         case LyXTabular::SET_SPECIAL_MULTI:
1446                 tabular.setAlignSpecial(cur.idx(),value,feature);
1447                 break;
1448
1449         case LyXTabular::APPEND_ROW:
1450                 // append the row into the tabular
1451                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1452                 break;
1453
1454         case LyXTabular::APPEND_COLUMN:
1455                 // append the column into the tabular
1456                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1457                 cur.idx() = tabular.getCellNumber(row, column);
1458                 break;
1459
1460         case LyXTabular::DELETE_ROW:
1461                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1462                         tabular.deleteRow(sel_row_start);
1463                 if (sel_row_start >= tabular.rows())
1464                         --sel_row_start;
1465                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1466                 cur.pit() = 0;
1467                 cur.pos() = 0;
1468                 cur.selection() = false;
1469                 break;
1470
1471         case LyXTabular::DELETE_COLUMN:
1472                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1473                         tabular.deleteColumn(sel_col_start);
1474                 if (sel_col_start >= tabular.columns())
1475                         --sel_col_start;
1476                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1477                 cur.pit() = 0;
1478                 cur.pos() = 0;
1479                 cur.selection() = false;
1480                 break;
1481
1482         case LyXTabular::M_TOGGLE_LINE_TOP:
1483                 flag = false;
1484         case LyXTabular::TOGGLE_LINE_TOP: {
1485                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1486                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1487                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1488                                 tabular.setTopLine(
1489                                         tabular.getCellNumber(i, j),
1490                                         lineSet, flag);
1491                 break;
1492         }
1493
1494         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1495                 flag = false;
1496         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1497                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1498                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1499                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1500                                 tabular.setBottomLine(
1501                                         tabular.getCellNumber(i, j),
1502                                         lineSet,
1503                                         flag);
1504                 break;
1505         }
1506
1507         case LyXTabular::M_TOGGLE_LINE_LEFT:
1508                 flag = false;
1509         case LyXTabular::TOGGLE_LINE_LEFT: {
1510                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1511                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1512                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1513                                 tabular.setLeftLine(
1514                                         tabular.getCellNumber(i,j),
1515                                         lineSet,
1516                                         flag);
1517                 break;
1518         }
1519
1520         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1521                 flag = false;
1522         case LyXTabular::TOGGLE_LINE_RIGHT: {
1523                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1524                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1525                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1526                                 tabular.setRightLine(
1527                                         tabular.getCellNumber(i,j),
1528                                         lineSet,
1529                                         flag);
1530                 break;
1531         }
1532
1533         case LyXTabular::M_ALIGN_LEFT:
1534         case LyXTabular::M_ALIGN_RIGHT:
1535         case LyXTabular::M_ALIGN_CENTER:
1536                 flag = false;
1537         case LyXTabular::ALIGN_LEFT:
1538         case LyXTabular::ALIGN_RIGHT:
1539         case LyXTabular::ALIGN_CENTER:
1540         case LyXTabular::ALIGN_BLOCK:
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.setAlignment(
1544                                         tabular.getCellNumber(i, j),
1545                                         setAlign,
1546                                         flag);
1547                 break;
1548
1549         case LyXTabular::M_VALIGN_TOP:
1550         case LyXTabular::M_VALIGN_BOTTOM:
1551         case LyXTabular::M_VALIGN_MIDDLE:
1552                 flag = false;
1553         case LyXTabular::VALIGN_TOP:
1554         case LyXTabular::VALIGN_BOTTOM:
1555         case LyXTabular::VALIGN_MIDDLE:
1556                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1557                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1558                                 tabular.setVAlignment(
1559                                         tabular.getCellNumber(i, j),
1560                                         setVAlign, flag);
1561                 break;
1562
1563         case LyXTabular::MULTICOLUMN: {
1564                 if (sel_row_start != sel_row_end) {
1565 #ifdef WITH_WARNINGS
1566 #warning Need I say it ? This is horrible.
1567 #endif
1568                         Alert::error(_("Error setting multicolumn"),
1569                                    _("You cannot set multicolumn vertically."));
1570                         return;
1571                 }
1572                 if (!cur.selection()) {
1573                         // just multicol for one single cell
1574                         // check whether we are completely in a multicol
1575                         if (tabular.isMultiColumn(cur.idx()))
1576                                 tabular.unsetMultiColumn(cur.idx());
1577                         else
1578                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1579                         break;
1580                 }
1581                 // we have a selection so this means we just add all this
1582                 // cells to form a multicolumn cell
1583                 idx_type const s_start = cur.selBegin().idx();
1584                 idx_type const s_end = cur.selEnd().idx();
1585                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1586                 cur.idx() = s_start;
1587                 cur.pit() = 0;
1588                 cur.pos() = 0;
1589                 cur.selection() = false;
1590                 break;
1591         }
1592
1593         case LyXTabular::SET_ALL_LINES:
1594                 setLines = true;
1595         case LyXTabular::UNSET_ALL_LINES:
1596                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1597                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1598                                 tabular.setAllLines(
1599                                         tabular.getCellNumber(i,j), setLines);
1600                 break;
1601
1602         case LyXTabular::SET_LONGTABULAR:
1603                 tabular.setLongTabular(true);
1604                 break;
1605
1606         case LyXTabular::UNSET_LONGTABULAR:
1607                 tabular.setLongTabular(false);
1608                 break;
1609
1610         case LyXTabular::SET_ROTATE_TABULAR:
1611                 tabular.setRotateTabular(true);
1612                 break;
1613
1614         case LyXTabular::UNSET_ROTATE_TABULAR:
1615                 tabular.setRotateTabular(false);
1616                 break;
1617
1618         case LyXTabular::SET_ROTATE_CELL:
1619                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1620                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1621                                 tabular.setRotateCell(
1622                                         tabular.getCellNumber(i, j), true);
1623                 break;
1624
1625         case LyXTabular::UNSET_ROTATE_CELL:
1626                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1627                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1628                                 tabular.setRotateCell(
1629                                         tabular.getCellNumber(i, j), false);
1630                 break;
1631
1632         case LyXTabular::SET_USEBOX: {
1633                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1634                 if (val == tabular.getUsebox(cur.idx()))
1635                         val = LyXTabular::BOX_NONE;
1636                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1637                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1638                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1639                 break;
1640         }
1641
1642         case LyXTabular::UNSET_LTFIRSTHEAD:
1643                 flag = false;
1644         case LyXTabular::SET_LTFIRSTHEAD:
1645                 tabular.getRowOfLTFirstHead(row, ltt);
1646                 checkLongtableSpecial(ltt, value, flag);
1647                 tabular.setLTHead(row, flag, ltt, true);
1648                 break;
1649
1650         case LyXTabular::UNSET_LTHEAD:
1651                 flag = false;
1652         case LyXTabular::SET_LTHEAD:
1653                 tabular.getRowOfLTHead(row, ltt);
1654                 checkLongtableSpecial(ltt, value, flag);
1655                 tabular.setLTHead(row, flag, ltt, false);
1656                 break;
1657
1658         case LyXTabular::UNSET_LTFOOT:
1659                 flag = false;
1660         case LyXTabular::SET_LTFOOT:
1661                 tabular.getRowOfLTFoot(row, ltt);
1662                 checkLongtableSpecial(ltt, value, flag);
1663                 tabular.setLTFoot(row, flag, ltt, false);
1664                 break;
1665
1666         case LyXTabular::UNSET_LTLASTFOOT:
1667                 flag = false;
1668         case LyXTabular::SET_LTLASTFOOT:
1669                 tabular.getRowOfLTLastFoot(row, ltt);
1670                 checkLongtableSpecial(ltt, value, flag);
1671                 tabular.setLTFoot(row, flag, ltt, true);
1672                 break;
1673
1674         case LyXTabular::SET_LTNEWPAGE:
1675                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1676                 break;
1677
1678         // dummy stuff just to avoid warnings
1679         case LyXTabular::LAST_ACTION:
1680                 break;
1681         }
1682
1683         InsetTabularMailer(*this).updateDialog(&bv);
1684 }
1685
1686
1687 bool InsetTabular::showInsetDialog(BufferView * bv) const
1688 {
1689         InsetTabularMailer(*this).showDialog(bv);
1690         return true;
1691 }
1692
1693
1694 void InsetTabular::openLayoutDialog(BufferView * bv) const
1695 {
1696         InsetTabularMailer(*this).showDialog(bv);
1697 }
1698
1699
1700 bool InsetTabular::copySelection(LCursor & cur)
1701 {
1702         if (!cur.selection())
1703                 return false;
1704
1705         row_type rs, re;
1706         col_type cs, ce;
1707         getSelection(cur, rs, re, cs, ce);
1708
1709         paste_tabular.reset(new LyXTabular(tabular));
1710
1711         for (row_type i = 0; i < rs; ++i)
1712                 paste_tabular->deleteRow(0);
1713
1714         row_type const rows = re - rs + 1;
1715         while (paste_tabular->rows() > rows)
1716                 paste_tabular->deleteRow(rows);
1717
1718         paste_tabular->setTopLine(0, true, true);
1719         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1720                                      true, true);
1721
1722         for (col_type i = 0; i < cs; ++i)
1723                 paste_tabular->deleteColumn(0);
1724
1725         col_type const columns = ce - cs + 1;
1726         while (paste_tabular->columns() > columns)
1727                 paste_tabular->deleteColumn(columns);
1728
1729         paste_tabular->setLeftLine(0, true, true);
1730         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1731                                     true, true);
1732
1733         ostringstream os;
1734         OutputParams const runparams;
1735         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1736         cur.bv().stuffClipboard(os.str());
1737         // mark tabular stack dirty
1738         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5, 
1739         // when we (hopefully) have a one-for-all paste mechanism.
1740         lyx::cap::dirtyTabularStack(true);
1741
1742         return true;
1743 }
1744
1745
1746 bool InsetTabular::pasteSelection(LCursor & cur)
1747 {
1748         if (!paste_tabular)
1749                 return false;
1750         col_type const actcol = tabular.column_of_cell(cur.idx());
1751         row_type const actrow = tabular.row_of_cell(cur.idx());
1752         for (row_type r1 = 0, r2 = actrow;
1753              r1 < paste_tabular->rows() && r2 < tabular.rows();
1754              ++r1, ++r2) {
1755                 for (col_type c1 = 0, c2 = actcol;
1756                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1757                     ++c1, ++c2) {
1758                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1759                             tabular.isPartOfMultiColumn(r2, c2))
1760                                 continue;
1761                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1762                                 --c2;
1763                                 continue;
1764                         }
1765                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1766                                 --c1;
1767                                 continue;
1768                         }
1769                         shared_ptr<InsetText> inset(
1770                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1771                         tabular.setCellInset(r2, c2, inset);
1772                         inset->markNew();
1773                         cur.pos() = 0;
1774                 }
1775         }
1776         return true;
1777 }
1778
1779
1780 void InsetTabular::cutSelection(LCursor & cur)
1781 {
1782         if (!cur.selection())
1783                 return;
1784
1785         row_type rs, re;
1786         col_type cs, ce;
1787         getSelection(cur, rs, re, cs, ce);
1788         for (row_type i = rs; i <= re; ++i) {
1789                 for (col_type j = cs; j <= ce; ++j) {
1790                         shared_ptr<InsetText> t
1791                                 = cell(tabular.getCellNumber(i, j));
1792                         if (cur.buffer().params().tracking_changes)
1793                                 t->markErased(true);
1794                         else
1795                                 t->clear();
1796                 }
1797         }
1798
1799         // cursor position might be invalid now
1800         cur.pos() = cur.lastpos();
1801         cur.clearSelection();
1802 }
1803
1804
1805 bool InsetTabular::isRightToLeft(LCursor & cur) const
1806 {
1807         BOOST_ASSERT(cur.depth() > 1);
1808         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1809         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1810         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1811                                          parentpos).language()->RightToLeft();
1812 }
1813
1814
1815 void InsetTabular::getSelection(LCursor & cur,
1816         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1817 {
1818         CursorSlice const & beg = cur.selBegin();
1819         CursorSlice const & end = cur.selEnd();
1820         cs = tabular.column_of_cell(beg.idx());
1821         ce = tabular.column_of_cell(end.idx());
1822         if (cs > ce) {
1823                 ce = cs;
1824                 cs = tabular.column_of_cell(end.idx());
1825         } else {
1826                 ce = tabular.right_column_of_cell(end.idx());
1827         }
1828
1829         rs = tabular.row_of_cell(beg.idx());
1830         re = tabular.row_of_cell(end.idx());
1831         if (rs > re)
1832                 swap(rs, re);
1833 }
1834
1835
1836 LyXText * InsetTabular::getText(int idx) const
1837 {
1838         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1839 }
1840
1841
1842 void InsetTabular::markErased(bool erased)
1843 {
1844         for (idx_type idx = 0; idx < nargs(); ++idx)
1845                 cell(idx)->markErased(erased);
1846 }
1847
1848
1849 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1850 {
1851         return tabular.getPWidth(cell).zero();
1852 }
1853
1854
1855 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1856                                      bool usePaste)
1857 {
1858         if (buf.length() <= 0)
1859                 return true;
1860
1861         col_type cols = 1;
1862         row_type rows = 1;
1863         col_type maxCols = 1;
1864         string::size_type const len = buf.length();
1865         string::size_type p = 0;
1866
1867         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1868                 switch (buf[p]) {
1869                 case '\t':
1870                         ++cols;
1871                         break;
1872                 case '\n':
1873                         if (p + 1 < len)
1874                                 ++rows;
1875                         maxCols = max(cols, maxCols);
1876                         cols = 1;
1877                         break;
1878                 }
1879                 ++p;
1880         }
1881         maxCols = max(cols, maxCols);
1882         LyXTabular * loctab;
1883         idx_type cell = 0;
1884         col_type ocol = 0;
1885         row_type row = 0;
1886         if (usePaste) {
1887                 paste_tabular.reset(
1888                         new LyXTabular(bv.buffer()->params(), rows, maxCols, &bv));
1889                 loctab = paste_tabular.get();
1890                 cols = 0;
1891                 dirtyTabularStack(true);
1892         } else {
1893                 loctab = &tabular;
1894                 cell = bv.cursor().idx();
1895                 ocol = tabular.column_of_cell(cell);
1896                 row = tabular.row_of_cell(cell);
1897         }
1898
1899         string::size_type op = 0;
1900         idx_type const cells = loctab->getNumberOfCells();
1901         p = 0;
1902         cols = ocol;
1903         rows = loctab->rows();
1904         col_type const columns = loctab->columns();
1905
1906         while (cell < cells && p < len && row < rows &&
1907                (p = buf.find_first_of("\t\n", p)) != string::npos)
1908         {
1909                 if (p >= len)
1910                         break;
1911                 switch (buf[p]) {
1912                 case '\t':
1913                         // we can only set this if we are not too far right
1914                         if (cols < columns) {
1915                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1916                                 Paragraph & par = inset->text_.getPar(0);
1917                                 LyXFont const font = inset->text_.getFont(par, 0);
1918                                 inset->setText(buf.substr(op, p - op), font);
1919                                 ++cols;
1920                                 ++cell;
1921                         }
1922                         break;
1923                 case '\n':
1924                         // we can only set this if we are not too far right
1925                         if (cols < columns) {
1926                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1927                                 Paragraph & par = inset->text_.getPar(0);
1928                                 LyXFont const font = inset->text_.getFont(par, 0);
1929                                 inset->setText(buf.substr(op, p - op), font);
1930                         }
1931                         cols = ocol;
1932                         ++row;
1933                         if (row < rows)
1934                                 cell = loctab->getCellNumber(row, cols);
1935                         break;
1936                 }
1937                 ++p;
1938                 op = p;
1939         }
1940         // check for the last cell if there is no trailing '\n'
1941         if (cell < cells && op < len) {
1942                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1943                 Paragraph & par = inset->text_.getPar(0);
1944                 LyXFont const font = inset->text_.getFont(par, 0);
1945                 inset->setText(buf.substr(op, len - op), font);
1946         }
1947         return true;
1948 }
1949
1950
1951 void InsetTabular::addPreview(PreviewLoader & loader) const
1952 {
1953         row_type const rows = tabular.rows();
1954         col_type const columns = tabular.columns();
1955         for (row_type i = 0; i < rows; ++i) {
1956                 for (col_type j = 0; j < columns; ++j)
1957                         tabular.getCellInset(i, j)->addPreview(loader);
1958         }
1959 }
1960
1961
1962 bool InsetTabular::tablemode(LCursor & cur) const
1963 {
1964         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1965 }
1966
1967
1968
1969
1970
1971 string const InsetTabularMailer::name_("tabular");
1972
1973 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1974         : inset_(const_cast<InsetTabular &>(inset))
1975 {}
1976
1977
1978 string const InsetTabularMailer::inset2string(Buffer const &) const
1979 {
1980         return params2string(inset_);
1981 }
1982
1983
1984 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1985 {
1986         istringstream data(in);
1987         LyXLex lex(0,0);
1988         lex.setStream(data);
1989
1990         if (in.empty())
1991                 return;
1992
1993         string token;
1994         lex >> token;
1995         if (!lex || token != name_)
1996                 return print_mailer_error("InsetTabularMailer", in, 1,
1997                                           name_);
1998
1999         // This is part of the inset proper that is usually swallowed
2000         // by Buffer::readInset
2001         lex >> token;
2002         if (!lex || token != "Tabular")
2003                 return print_mailer_error("InsetTabularMailer", in, 2,
2004                                           "Tabular");
2005
2006         Buffer const & buffer = inset.buffer();
2007         inset.read(buffer, lex);
2008 }
2009
2010
2011 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2012 {
2013         ostringstream data;
2014         data << name_ << ' ';
2015         inset.write(inset.buffer(), data);
2016         data << "\\end_inset\n";
2017         return data.str();
2018 }