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