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