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