]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Fix bug 2380:
[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)), buf.text().bv()), 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, &cur.bv()));
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_PASTE:
1040                 if (tabularStackDirty()) {
1041                         status.enabled(true);
1042                         return true;
1043                 }
1044
1045         case LFUN_INSET_MODIFY:
1046                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1047                         status.enabled(true);
1048                         return true;
1049                 }
1050                 // Fall through
1051
1052         default:
1053                 // we try to handle this event in the insets dispatch function.
1054                 return cell(cur.idx())->getStatus(cur, cmd, status);
1055         }
1056 }
1057
1058
1059 int InsetTabular::latex(Buffer const & buf, ostream & os,
1060                         OutputParams const & runparams) const
1061 {
1062         return tabular.latex(buf, os, runparams);
1063 }
1064
1065
1066 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1067                         OutputParams const & runparams) const
1068 {
1069         int const dp = runparams.linelen ? runparams.depth : 0;
1070         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1071 }
1072
1073
1074 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1075                            OutputParams const & runparams) const
1076 {
1077         return tabular.linuxdoc(buf,os, runparams);
1078 }
1079
1080
1081 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1082                           OutputParams const & runparams) const
1083 {
1084         int ret = 0;
1085         InsetBase * master = 0;
1086
1087 #ifdef WITH_WARNINGS
1088 #warning Why not pass a proper DocIterator here?
1089 #endif
1090 #if 0
1091         // if the table is inside a float it doesn't need the informaltable
1092         // wrapper. Search for it.
1093         for (master = owner(); master; master = master->owner())
1094                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1095                         break;
1096 #endif
1097
1098         if (!master) {
1099                 os << "<informaltable>";
1100                 ++ret;
1101         }
1102         ret += tabular.docbook(buf, os, runparams);
1103         if (!master) {
1104                 os << "</informaltable>";
1105                 ++ret;
1106         }
1107         return ret;
1108 }
1109
1110
1111 void InsetTabular::validate(LaTeXFeatures & features) const
1112 {
1113         tabular.validate(features);
1114 }
1115
1116
1117 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1118 {
1119         return tabular.getCellInset(idx);
1120 }
1121
1122
1123 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1124 {
1125         return tabular.getCellInset(idx);
1126 }
1127
1128
1129 void InsetTabular::cursorPos
1130         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1131 {
1132         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1133
1134         // y offset     correction
1135         int const row = tabular.row_of_cell(sl.idx());
1136         for (int i = 0; i <= row; ++i) {
1137                 if (i != 0) {
1138                         y += tabular.getAscentOfRow(i);
1139                         y += tabular.getAdditionalHeight(i);
1140                 }
1141                 if (i != row)
1142                         y += tabular.getDescentOfRow(i);
1143         }
1144
1145         // x offset correction
1146         int const col = tabular.column_of_cell(sl.idx());
1147         int idx = tabular.getCellNumber(row, 0);
1148         for (int j = 0; j < col; ++j) {
1149                 if (tabular.isPartOfMultiColumn(row, j))
1150                         continue;
1151                 x += tabular.getWidthOfColumn(idx);
1152                 ++idx;
1153         }
1154         x += tabular.getBeginningOfTextInCell(idx);
1155         x += ADD_TO_TABULAR_WIDTH;
1156         x += scx_;
1157 }
1158
1159
1160 int InsetTabular::dist(idx_type const cell, int x, int y) const
1161 {
1162         int xx = 0;
1163         int yy = 0;
1164         InsetBase const & inset = *tabular.getCellInset(cell);
1165         Point o = theCoords.getInsets().xy(&inset);
1166         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1167         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1168         int const ybeg = o.y_ - inset.ascent();
1169         row_type const row = tabular.row_of_cell(cell);
1170         int const rowheight = tabular.getAscentOfRow(row)
1171                         + tabular.getDescentOfRow(row);
1172         int const yend = ybeg + rowheight;
1173
1174         if (x < xbeg)
1175                 xx = xbeg - x;
1176         else if (x > xend)
1177                 xx = x - xend;
1178
1179         if (y < ybeg) 
1180                 yy = ybeg - y;
1181         else if (y > yend) 
1182                 yy = y - yend;
1183
1184         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1185         //       << " ybeg=" << ybeg << " yend=" << yend
1186         //       << " xx=" << xx << " yy=" << yy
1187         //       << " dist=" << xx + yy << endl;
1188         return xx + yy;
1189 }
1190
1191
1192 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1193 {
1194         //lyxerr << "InsetTabular::editXY: " << this << endl;
1195         cur.selection() = false;
1196         cur.push(*this);
1197         cur.idx() = getNearestCell(x, y);
1198         resetPos(cur);
1199         return cell(cur.idx())->text_.editXY(cur, x, y);
1200 }
1201
1202
1203 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1204 {
1205         cur.idx() = getNearestCell(x, y);
1206         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1207 }
1208
1209
1210 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1211 {
1212         idx_type idx_min = 0;
1213         int dist_min = std::numeric_limits<int>::max();
1214         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1215                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1216                         int const d = dist(i, x, y);
1217                         if (d < dist_min) {
1218                                 dist_min = d;
1219                                 idx_min = i;
1220                         }
1221                 }
1222         }
1223         return idx_min;
1224 }
1225
1226
1227 int InsetTabular::getCellXPos(idx_type const cell) const
1228 {
1229         idx_type c = cell;
1230
1231         for (; !tabular.isFirstCellInRow(c); --c)
1232                 ;
1233         int lx = tabular.getWidthOfColumn(cell);
1234         for (; c < cell; ++c)
1235                 lx += tabular.getWidthOfColumn(c);
1236
1237         return lx - tabular.getWidthOfColumn(cell);
1238 }
1239
1240
1241 void InsetTabular::resetPos(LCursor & cur) const
1242 {
1243         BufferView & bv = cur.bv();
1244         int const maxwidth = bv.workWidth();
1245
1246         if (&cur.inset() != this) {
1247                 scx_ = 0;
1248         } else {
1249                 int const X1 = 0;
1250                 int const X2 = maxwidth;
1251                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1252                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1253                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1254
1255                 if (x1 < X1)
1256                         scx_ = X1 + 20 - x1;
1257                 else if (x2 > X2)
1258                         scx_ = X2 - 20 - x2;
1259                 else
1260                         scx_ = 0;
1261         }
1262
1263         cur.needsUpdate();
1264
1265         InsetTabularMailer(*this).updateDialog(&bv);
1266 }
1267
1268
1269 void InsetTabular::moveNextCell(LCursor & cur)
1270 {
1271         if (isRightToLeft(cur)) {
1272                 if (tabular.isFirstCellInRow(cur.idx())) {
1273                         row_type const row = tabular.row_of_cell(cur.idx());
1274                         if (row == tabular.rows() - 1)
1275                                 return;
1276                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1277                 } else {
1278                         if (cur.idx() == 0)
1279                                 return;
1280                         --cur.idx();
1281                 }
1282         } else {
1283                 if (tabular.isLastCell(cur.idx()))
1284                         return;
1285                 ++cur.idx();
1286         }
1287         cur.pit() = 0;
1288         cur.pos() = 0;
1289         resetPos(cur);
1290 }
1291
1292
1293 void InsetTabular::movePrevCell(LCursor & cur)
1294 {
1295         if (isRightToLeft(cur)) {
1296                 if (tabular.isLastCellInRow(cur.idx())) {
1297                         row_type const row = tabular.row_of_cell(cur.idx());
1298                         if (row == 0)
1299                                 return;
1300                         cur.idx() = tabular.getFirstCellInRow(row);
1301                         cur.idx() = tabular.getCellAbove(cur.idx());
1302                 } else {
1303                         if (tabular.isLastCell(cur.idx()))
1304                                 return;
1305                         ++cur.idx();
1306                 }
1307         } else {
1308                 if (cur.idx() == 0) // first cell
1309                         return;
1310                 --cur.idx();
1311         }
1312         cur.pit() = cur.lastpit();
1313         cur.pos() = cur.lastpos();
1314         resetPos(cur);
1315 }
1316
1317
1318 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1319 {
1320         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1321
1322         int i = 0;
1323         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1324                 string const tmp = tabularFeature[i].feature;
1325
1326                 if (tmp == what.substr(0, tmp.length())) {
1327                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1328                         //tabularFeatures[i].feature.length())) {
1329                         action = tabularFeature[i].action;
1330                         break;
1331                 }
1332         }
1333         if (action == LyXTabular::LAST_ACTION)
1334                 return false;
1335
1336         string const val =
1337                 ltrim(what.substr(tabularFeature[i].feature.length()));
1338         tabularFeatures(cur, action, val);
1339         return true;
1340 }
1341
1342
1343 namespace {
1344
1345 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1346                           string const & special, bool & flag)
1347 {
1348         if (special == "dl_above") {
1349                 ltt.topDL = flag;
1350                 ltt.set = false;
1351         } else if (special == "dl_below") {
1352                 ltt.bottomDL = flag;
1353                 ltt.set = false;
1354         } else if (special == "empty") {
1355                 ltt.empty = flag;
1356                 ltt.set = false;
1357         } else if (flag) {
1358                 ltt.empty = false;
1359                 ltt.set = true;
1360         }
1361 }
1362
1363 } // anon namespace
1364
1365
1366 void InsetTabular::tabularFeatures(LCursor & cur,
1367         LyXTabular::Feature feature, string const & value)
1368 {
1369         BufferView & bv = cur.bv();
1370         col_type sel_col_start;
1371         col_type sel_col_end;
1372         row_type sel_row_start;
1373         row_type sel_row_end;
1374         bool setLines = false;
1375         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1376         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1377
1378         switch (feature) {
1379
1380         case LyXTabular::M_ALIGN_LEFT:
1381         case LyXTabular::ALIGN_LEFT:
1382                 setAlign = LYX_ALIGN_LEFT;
1383                 break;
1384
1385         case LyXTabular::M_ALIGN_RIGHT:
1386         case LyXTabular::ALIGN_RIGHT:
1387                 setAlign = LYX_ALIGN_RIGHT;
1388                 break;
1389
1390         case LyXTabular::M_ALIGN_CENTER:
1391         case LyXTabular::ALIGN_CENTER:
1392                 setAlign = LYX_ALIGN_CENTER;
1393                 break;
1394
1395         case LyXTabular::ALIGN_BLOCK:
1396                 setAlign = LYX_ALIGN_BLOCK;
1397                 break;
1398
1399         case LyXTabular::M_VALIGN_TOP:
1400         case LyXTabular::VALIGN_TOP:
1401                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1402                 break;
1403
1404         case LyXTabular::M_VALIGN_BOTTOM:
1405         case LyXTabular::VALIGN_BOTTOM:
1406                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1407                 break;
1408
1409         case LyXTabular::M_VALIGN_MIDDLE:
1410         case LyXTabular::VALIGN_MIDDLE:
1411                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1412                 break;
1413
1414         default:
1415                 break;
1416         }
1417
1418         recordUndoInset(cur, Undo::ATOMIC);
1419
1420         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1421         row_type const row = tabular.row_of_cell(cur.idx());
1422         col_type const column = tabular.column_of_cell(cur.idx());
1423         bool flag = true;
1424         LyXTabular::ltType ltt;
1425
1426         switch (feature) {
1427
1428         case LyXTabular::SET_PWIDTH: {
1429                 LyXLength const len(value);
1430                 tabular.setColumnPWidth(cur, cur.idx(), len);
1431                 if (len.zero()
1432                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1433                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1434                 break;
1435         }
1436
1437         case LyXTabular::SET_MPWIDTH:
1438                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1439                 break;
1440
1441         case LyXTabular::SET_SPECIAL_COLUMN:
1442         case LyXTabular::SET_SPECIAL_MULTI:
1443                 tabular.setAlignSpecial(cur.idx(),value,feature);
1444                 break;
1445
1446         case LyXTabular::APPEND_ROW:
1447                 // append the row into the tabular
1448                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1449                 break;
1450
1451         case LyXTabular::APPEND_COLUMN:
1452                 // append the column into the tabular
1453                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1454                 cur.idx() = tabular.getCellNumber(row, column);
1455                 break;
1456
1457         case LyXTabular::DELETE_ROW:
1458                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1459                         tabular.deleteRow(sel_row_start);
1460                 if (sel_row_start >= tabular.rows())
1461                         --sel_row_start;
1462                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1463                 cur.pit() = 0;
1464                 cur.pos() = 0;
1465                 cur.selection() = false;
1466                 break;
1467
1468         case LyXTabular::DELETE_COLUMN:
1469                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1470                         tabular.deleteColumn(sel_col_start);
1471                 if (sel_col_start >= tabular.columns())
1472                         --sel_col_start;
1473                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1474                 cur.pit() = 0;
1475                 cur.pos() = 0;
1476                 cur.selection() = false;
1477                 break;
1478
1479         case LyXTabular::M_TOGGLE_LINE_TOP:
1480                 flag = false;
1481         case LyXTabular::TOGGLE_LINE_TOP: {
1482                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1483                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1484                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1485                                 tabular.setTopLine(
1486                                         tabular.getCellNumber(i, j),
1487                                         lineSet, flag);
1488                 break;
1489         }
1490
1491         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1492                 flag = false;
1493         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1494                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1495                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1496                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1497                                 tabular.setBottomLine(
1498                                         tabular.getCellNumber(i, j),
1499                                         lineSet,
1500                                         flag);
1501                 break;
1502         }
1503
1504         case LyXTabular::M_TOGGLE_LINE_LEFT:
1505                 flag = false;
1506         case LyXTabular::TOGGLE_LINE_LEFT: {
1507                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1508                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1509                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1510                                 tabular.setLeftLine(
1511                                         tabular.getCellNumber(i,j),
1512                                         lineSet,
1513                                         flag);
1514                 break;
1515         }
1516
1517         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1518                 flag = false;
1519         case LyXTabular::TOGGLE_LINE_RIGHT: {
1520                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1521                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1522                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1523                                 tabular.setRightLine(
1524                                         tabular.getCellNumber(i,j),
1525                                         lineSet,
1526                                         flag);
1527                 break;
1528         }
1529
1530         case LyXTabular::M_ALIGN_LEFT:
1531         case LyXTabular::M_ALIGN_RIGHT:
1532         case LyXTabular::M_ALIGN_CENTER:
1533                 flag = false;
1534         case LyXTabular::ALIGN_LEFT:
1535         case LyXTabular::ALIGN_RIGHT:
1536         case LyXTabular::ALIGN_CENTER:
1537         case LyXTabular::ALIGN_BLOCK:
1538                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1539                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1540                                 tabular.setAlignment(
1541                                         tabular.getCellNumber(i, j),
1542                                         setAlign,
1543                                         flag);
1544                 break;
1545
1546         case LyXTabular::M_VALIGN_TOP:
1547         case LyXTabular::M_VALIGN_BOTTOM:
1548         case LyXTabular::M_VALIGN_MIDDLE:
1549                 flag = false;
1550         case LyXTabular::VALIGN_TOP:
1551         case LyXTabular::VALIGN_BOTTOM:
1552         case LyXTabular::VALIGN_MIDDLE:
1553                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1554                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1555                                 tabular.setVAlignment(
1556                                         tabular.getCellNumber(i, j),
1557                                         setVAlign, flag);
1558                 break;
1559
1560         case LyXTabular::MULTICOLUMN: {
1561                 if (sel_row_start != sel_row_end) {
1562 #ifdef WITH_WARNINGS
1563 #warning Need I say it ? This is horrible.
1564 #endif
1565                         Alert::error(_("Error setting multicolumn"),
1566                                    _("You cannot set multicolumn vertically."));
1567                         return;
1568                 }
1569                 if (!cur.selection()) {
1570                         // just multicol for one single cell
1571                         // check whether we are completely in a multicol
1572                         if (tabular.isMultiColumn(cur.idx()))
1573                                 tabular.unsetMultiColumn(cur.idx());
1574                         else
1575                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1576                         break;
1577                 }
1578                 // we have a selection so this means we just add all this
1579                 // cells to form a multicolumn cell
1580                 idx_type const s_start = cur.selBegin().idx();
1581                 idx_type const s_end = cur.selEnd().idx();
1582                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1583                 cur.idx() = s_start;
1584                 cur.pit() = 0;
1585                 cur.pos() = 0;
1586                 cur.selection() = false;
1587                 break;
1588         }
1589
1590         case LyXTabular::SET_ALL_LINES:
1591                 setLines = true;
1592         case LyXTabular::UNSET_ALL_LINES:
1593                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1594                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1595                                 tabular.setAllLines(
1596                                         tabular.getCellNumber(i,j), setLines);
1597                 break;
1598
1599         case LyXTabular::SET_LONGTABULAR:
1600                 tabular.setLongTabular(true);
1601                 break;
1602
1603         case LyXTabular::UNSET_LONGTABULAR:
1604                 tabular.setLongTabular(false);
1605                 break;
1606
1607         case LyXTabular::SET_ROTATE_TABULAR:
1608                 tabular.setRotateTabular(true);
1609                 break;
1610
1611         case LyXTabular::UNSET_ROTATE_TABULAR:
1612                 tabular.setRotateTabular(false);
1613                 break;
1614
1615         case LyXTabular::SET_ROTATE_CELL:
1616                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1617                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1618                                 tabular.setRotateCell(
1619                                         tabular.getCellNumber(i, j), true);
1620                 break;
1621
1622         case LyXTabular::UNSET_ROTATE_CELL:
1623                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1624                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1625                                 tabular.setRotateCell(
1626                                         tabular.getCellNumber(i, j), false);
1627                 break;
1628
1629         case LyXTabular::SET_USEBOX: {
1630                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1631                 if (val == tabular.getUsebox(cur.idx()))
1632                         val = LyXTabular::BOX_NONE;
1633                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1634                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1635                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1636                 break;
1637         }
1638
1639         case LyXTabular::UNSET_LTFIRSTHEAD:
1640                 flag = false;
1641         case LyXTabular::SET_LTFIRSTHEAD:
1642                 tabular.getRowOfLTFirstHead(row, ltt);
1643                 checkLongtableSpecial(ltt, value, flag);
1644                 tabular.setLTHead(row, flag, ltt, true);
1645                 break;
1646
1647         case LyXTabular::UNSET_LTHEAD:
1648                 flag = false;
1649         case LyXTabular::SET_LTHEAD:
1650                 tabular.getRowOfLTHead(row, ltt);
1651                 checkLongtableSpecial(ltt, value, flag);
1652                 tabular.setLTHead(row, flag, ltt, false);
1653                 break;
1654
1655         case LyXTabular::UNSET_LTFOOT:
1656                 flag = false;
1657         case LyXTabular::SET_LTFOOT:
1658                 tabular.getRowOfLTFoot(row, ltt);
1659                 checkLongtableSpecial(ltt, value, flag);
1660                 tabular.setLTFoot(row, flag, ltt, false);
1661                 break;
1662
1663         case LyXTabular::UNSET_LTLASTFOOT:
1664                 flag = false;
1665         case LyXTabular::SET_LTLASTFOOT:
1666                 tabular.getRowOfLTLastFoot(row, ltt);
1667                 checkLongtableSpecial(ltt, value, flag);
1668                 tabular.setLTFoot(row, flag, ltt, true);
1669                 break;
1670
1671         case LyXTabular::SET_LTNEWPAGE:
1672                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1673                 break;
1674
1675         // dummy stuff just to avoid warnings
1676         case LyXTabular::LAST_ACTION:
1677                 break;
1678         }
1679
1680         InsetTabularMailer(*this).updateDialog(&bv);
1681 }
1682
1683
1684 bool InsetTabular::showInsetDialog(BufferView * bv) const
1685 {
1686         InsetTabularMailer(*this).showDialog(bv);
1687         return true;
1688 }
1689
1690
1691 void InsetTabular::openLayoutDialog(BufferView * bv) const
1692 {
1693         InsetTabularMailer(*this).showDialog(bv);
1694 }
1695
1696
1697 bool InsetTabular::copySelection(LCursor & cur)
1698 {
1699         if (!cur.selection())
1700                 return false;
1701
1702         row_type rs, re;
1703         col_type cs, ce;
1704         getSelection(cur, rs, re, cs, ce);
1705
1706         paste_tabular.reset(new LyXTabular(tabular));
1707
1708         for (row_type i = 0; i < rs; ++i)
1709                 paste_tabular->deleteRow(0);
1710
1711         row_type const rows = re - rs + 1;
1712         while (paste_tabular->rows() > rows)
1713                 paste_tabular->deleteRow(rows);
1714
1715         paste_tabular->setTopLine(0, true, true);
1716         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1717                                      true, true);
1718
1719         for (col_type i = 0; i < cs; ++i)
1720                 paste_tabular->deleteColumn(0);
1721
1722         col_type const columns = ce - cs + 1;
1723         while (paste_tabular->columns() > columns)
1724                 paste_tabular->deleteColumn(columns);
1725
1726         paste_tabular->setLeftLine(0, true, true);
1727         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1728                                     true, true);
1729
1730         ostringstream os;
1731         OutputParams const runparams;
1732         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1733         cur.bv().stuffClipboard(os.str());
1734         // mark tabular stack dirty
1735         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5, 
1736         // when we (hopefully) have a one-for-all paste mechanism.
1737         lyx::cap::dirtyTabularStack(true);
1738
1739         return true;
1740 }
1741
1742
1743 bool InsetTabular::pasteSelection(LCursor & cur)
1744 {
1745         if (!paste_tabular)
1746                 return false;
1747         col_type const actcol = tabular.column_of_cell(cur.idx());
1748         row_type const actrow = tabular.row_of_cell(cur.idx());
1749         for (row_type r1 = 0, r2 = actrow;
1750              r1 < paste_tabular->rows() && r2 < tabular.rows();
1751              ++r1, ++r2) {
1752                 for (col_type c1 = 0, c2 = actcol;
1753                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1754                     ++c1, ++c2) {
1755                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1756                             tabular.isPartOfMultiColumn(r2, c2))
1757                                 continue;
1758                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1759                                 --c2;
1760                                 continue;
1761                         }
1762                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1763                                 --c1;
1764                                 continue;
1765                         }
1766                         shared_ptr<InsetText> inset(
1767                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1768                         tabular.setCellInset(r2, c2, inset);
1769                         inset->markNew();
1770                         cur.pos() = 0;
1771                 }
1772         }
1773         return true;
1774 }
1775
1776
1777 void InsetTabular::cutSelection(LCursor & cur)
1778 {
1779         if (!cur.selection())
1780                 return;
1781
1782         row_type rs, re;
1783         col_type cs, ce;
1784         getSelection(cur, rs, re, cs, ce);
1785         for (row_type i = rs; i <= re; ++i) {
1786                 for (col_type j = cs; j <= ce; ++j) {
1787                         shared_ptr<InsetText> t
1788                                 = cell(tabular.getCellNumber(i, j));
1789                         if (cur.buffer().params().tracking_changes)
1790                                 t->markErased(true);
1791                         else
1792                                 t->clear();
1793                 }
1794         }
1795
1796         // cursor position might be invalid now
1797         cur.pos() = cur.lastpos();
1798         cur.clearSelection();
1799 }
1800
1801
1802 bool InsetTabular::isRightToLeft(LCursor & cur) const
1803 {
1804         BOOST_ASSERT(cur.depth() > 1);
1805         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1806         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1807         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1808                                          parentpos).language()->RightToLeft();
1809 }
1810
1811
1812 void InsetTabular::getSelection(LCursor & cur,
1813         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1814 {
1815         CursorSlice const & beg = cur.selBegin();
1816         CursorSlice const & end = cur.selEnd();
1817         cs = tabular.column_of_cell(beg.idx());
1818         ce = tabular.column_of_cell(end.idx());
1819         if (cs > ce) {
1820                 ce = cs;
1821                 cs = tabular.column_of_cell(end.idx());
1822         } else {
1823                 ce = tabular.right_column_of_cell(end.idx());
1824         }
1825
1826         rs = tabular.row_of_cell(beg.idx());
1827         re = tabular.row_of_cell(end.idx());
1828         if (rs > re)
1829                 swap(rs, re);
1830 }
1831
1832
1833 LyXText * InsetTabular::getText(int idx) const
1834 {
1835         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1836 }
1837
1838
1839 void InsetTabular::markErased(bool erased)
1840 {
1841         for (idx_type idx = 0; idx < nargs(); ++idx)
1842                 cell(idx)->markErased(erased);
1843 }
1844
1845
1846 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1847 {
1848         return tabular.getPWidth(cell).zero();
1849 }
1850
1851
1852 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1853                                      bool usePaste)
1854 {
1855         if (buf.length() <= 0)
1856                 return true;
1857
1858         col_type cols = 1;
1859         row_type rows = 1;
1860         col_type maxCols = 1;
1861         string::size_type const len = buf.length();
1862         string::size_type p = 0;
1863
1864         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1865                 switch (buf[p]) {
1866                 case '\t':
1867                         ++cols;
1868                         break;
1869                 case '\n':
1870                         if (p + 1 < len)
1871                                 ++rows;
1872                         maxCols = max(cols, maxCols);
1873                         cols = 1;
1874                         break;
1875                 }
1876                 ++p;
1877         }
1878         maxCols = max(cols, maxCols);
1879         LyXTabular * loctab;
1880         idx_type cell = 0;
1881         col_type ocol = 0;
1882         row_type row = 0;
1883         if (usePaste) {
1884                 paste_tabular.reset(
1885                         new LyXTabular(bv.buffer()->params(), rows, maxCols, &bv));
1886                 loctab = paste_tabular.get();
1887                 cols = 0;
1888         } else {
1889                 loctab = &tabular;
1890                 cell = bv.cursor().idx();
1891                 ocol = tabular.column_of_cell(cell);
1892                 row = tabular.row_of_cell(cell);
1893         }
1894
1895         string::size_type op = 0;
1896         idx_type const cells = loctab->getNumberOfCells();
1897         p = 0;
1898         cols = ocol;
1899         rows = loctab->rows();
1900         col_type const columns = loctab->columns();
1901
1902         while (cell < cells && p < len && row < rows &&
1903                (p = buf.find_first_of("\t\n", p)) != string::npos)
1904         {
1905                 if (p >= len)
1906                         break;
1907                 switch (buf[p]) {
1908                 case '\t':
1909                         // we can only set this if we are not too far right
1910                         if (cols < columns) {
1911                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1912                                 Paragraph & par = inset->text_.getPar(0);
1913                                 LyXFont const font = inset->text_.getFont(par, 0);
1914                                 inset->setText(buf.substr(op, p - op), font);
1915                                 ++cols;
1916                                 ++cell;
1917                         }
1918                         break;
1919                 case '\n':
1920                         // we can only set this if we are not too far right
1921                         if (cols < columns) {
1922                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1923                                 Paragraph & par = inset->text_.getPar(0);
1924                                 LyXFont const font = inset->text_.getFont(par, 0);
1925                                 inset->setText(buf.substr(op, p - op), font);
1926                         }
1927                         cols = ocol;
1928                         ++row;
1929                         if (row < rows)
1930                                 cell = loctab->getCellNumber(row, cols);
1931                         break;
1932                 }
1933                 ++p;
1934                 op = p;
1935         }
1936         // check for the last cell if there is no trailing '\n'
1937         if (cell < cells && op < len) {
1938                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1939                 Paragraph & par = inset->text_.getPar(0);
1940                 LyXFont const font = inset->text_.getFont(par, 0);
1941                 inset->setText(buf.substr(op, len - op), font);
1942         }
1943         return true;
1944 }
1945
1946
1947 void InsetTabular::addPreview(PreviewLoader & loader) const
1948 {
1949         row_type const rows = tabular.rows();
1950         col_type const columns = tabular.columns();
1951         for (row_type i = 0; i < rows; ++i) {
1952                 for (col_type j = 0; j < columns; ++j)
1953                         tabular.getCellInset(i, j)->addPreview(loader);
1954         }
1955 }
1956
1957
1958 bool InsetTabular::tablemode(LCursor & cur) const
1959 {
1960         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1961 }
1962
1963
1964
1965
1966
1967 string const InsetTabularMailer::name_("tabular");
1968
1969 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1970         : inset_(const_cast<InsetTabular &>(inset))
1971 {}
1972
1973
1974 string const InsetTabularMailer::inset2string(Buffer const &) const
1975 {
1976         return params2string(inset_);
1977 }
1978
1979
1980 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1981 {
1982         istringstream data(in);
1983         LyXLex lex(0,0);
1984         lex.setStream(data);
1985
1986         if (in.empty())
1987                 return;
1988
1989         string token;
1990         lex >> token;
1991         if (!lex || token != name_)
1992                 return print_mailer_error("InsetTabularMailer", in, 1,
1993                                           name_);
1994
1995         // This is part of the inset proper that is usually swallowed
1996         // by Buffer::readInset
1997         lex >> token;
1998         if (!lex || token != "Tabular")
1999                 return print_mailer_error("InsetTabularMailer", in, 2,
2000                                           "Tabular");
2001
2002         Buffer const & buffer = inset.buffer();
2003         inset.read(buffer, lex);
2004 }
2005
2006
2007 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2008 {
2009         ostringstream data;
2010         data << name_ << ' ';
2011         inset.write(inset.buffer(), data);
2012         data << "\\end_inset\n";
2013         return data.str();
2014 }