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