]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
conmsolidate scrolling code
[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                         tabular.getCellInset(cell)->metrics(m, dim);
252                         if (!p_width.zero())
253                                 dim.wid = m.base.textwidth;
254                         maxAsc  = max(maxAsc, dim.asc);
255                         maxDesc = max(maxDesc, dim.des);
256                         tabular.setWidthOfCell(cell, dim.wid);
257                         ++cell;
258                 }
259                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
260                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
261         }
262
263         dim.asc = tabular.getAscentOfRow(0);
264         dim.des = tabular.getHeightOfTabular() - dim.asc;
265         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
266         dim_ = dim;
267 }
268
269
270 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
271 {
272         setPosCache(pi, x, y);
273
274         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
275         BufferView * bv = pi.base.bv;
276
277         static NullPainter nop;
278         static PainterInfo nullpi(bv, nop);
279
280         resetPos(bv->cursor());
281
282         x += scroll();
283         x += ADD_TO_TABULAR_WIDTH;
284
285         idx_type idx = 0;
286         first_visible_cell = LyXTabular::npos;
287         for (row_type i = 0; i < tabular.rows(); ++i) {
288                 int nx = x;
289                 int const a = tabular.getAscentOfRow(i);
290                 int const d = tabular.getDescentOfRow(i);
291                 idx = tabular.getCellNumber(i, 0);
292                 for (col_type j = 0; j < tabular.columns(); ++j) {
293                         if (tabular.isPartOfMultiColumn(i, j))
294                                 continue;
295                         if (first_visible_cell == LyXTabular::npos)
296                                 first_visible_cell = idx;
297
298                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
299                         if (nx + tabular.getWidthOfColumn(idx) < 0
300                             || nx > bv->workWidth()
301                             || y + d < 0
302                             || y - a > bv->workHeight()) {
303                                 cell(idx)->draw(nullpi, cx, y);
304                                 drawCellLines(nop, nx, y, i, idx);
305                         } else {
306                                 cell(idx)->draw(pi, cx, y);
307                                 drawCellLines(pi.pain, nx, y, i, idx);
308                         }
309                         nx += tabular.getWidthOfColumn(idx);
310                         ++idx;
311                 }
312
313                 if (i + 1 < tabular.rows())
314                         y += d + tabular.getAscentOfRow(i + 1) +
315                                 tabular.getAdditionalHeight(i + 1);
316         }
317 }
318
319
320 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
321 {
322         setPosCache(pi, x, y);
323
324         LCursor & cur = pi.base.bv->cursor();
325         if (!cur.selection())
326                 return;
327         if (!ptr_cmp(&cur.inset(), this))
328                 return;
329
330         resetPos(cur);
331
332         x += scroll();
333         x += ADD_TO_TABULAR_WIDTH;
334
335         if (tablemode(cur)) {
336                 row_type rs, re;
337                 col_type cs, ce;
338                 getSelection(cur, rs, re, cs, ce);
339                 y -= tabular.getAscentOfRow(0);
340                 for (row_type j = 0; j < tabular.rows(); ++j) {
341                         int const a = tabular.getAscentOfRow(j);
342                         int const h = a + tabular.getDescentOfRow(j);
343                         int xx = x;
344                         y += tabular.getAdditionalHeight(j);
345                         for (col_type i = 0; i < tabular.columns(); ++i) {
346                                 if (tabular.isPartOfMultiColumn(j, i))
347                                         continue;
348                                 idx_type const cell =
349                                         tabular.getCellNumber(j, i);
350                                 int const w = tabular.getWidthOfColumn(cell);
351                                 if (i >= cs && i <= ce && j >= rs && j <= re)
352                                         pi.pain.fillRectangle(xx, y, w, h,
353                                                               LColor::selection);
354                                 xx += w;
355
356                         }
357                         y += h;
358                 }
359
360         } else {
361                 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
362         }
363 }
364
365
366 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
367                                  row_type row, idx_type cell) const
368 {
369         int x2 = x + tabular.getWidthOfColumn(cell);
370         bool on_off = false;
371
372         if (!tabular.topAlreadyDrawn(cell)) {
373                 on_off = !tabular.topLine(cell);
374                 pain.line(x, y - tabular.getAscentOfRow(row),
375                           x2, y -  tabular.getAscentOfRow(row),
376                           on_off ? LColor::tabularonoffline : LColor::tabularline,
377                           on_off ? Painter::line_onoffdash : Painter::line_solid);
378         }
379         on_off = !tabular.bottomLine(cell);
380         pain.line(x, y + tabular.getDescentOfRow(row),
381                   x2, y + tabular.getDescentOfRow(row),
382                   on_off ? LColor::tabularonoffline : LColor::tabularline,
383                   on_off ? Painter::line_onoffdash : Painter::line_solid);
384         if (!tabular.leftAlreadyDrawn(cell)) {
385                 on_off = !tabular.leftLine(cell);
386                 pain.line(x, y -  tabular.getAscentOfRow(row),
387                           x, y +  tabular.getDescentOfRow(row),
388                           on_off ? LColor::tabularonoffline : LColor::tabularline,
389                           on_off ? Painter::line_onoffdash : Painter::line_solid);
390         }
391         on_off = !tabular.rightLine(cell);
392         pain.line(x2 - tabular.getAdditionalWidth(cell),
393                   y -  tabular.getAscentOfRow(row),
394                   x2 - tabular.getAdditionalWidth(cell),
395                   y +  tabular.getDescentOfRow(row),
396                   on_off ? LColor::tabularonoffline : LColor::tabularline,
397                   on_off ? Painter::line_onoffdash : Painter::line_solid);
398 }
399
400
401 string const InsetTabular::editMessage() const
402 {
403         return _("Opened table");
404 }
405
406
407 void InsetTabular::edit(LCursor & cur, bool left)
408 {
409         lyxerr << "InsetTabular::edit: " << this << endl;
410         finishUndo();
411         cur.selection() = false;
412         cur.push(*this);
413         if (left) {
414                 if (isRightToLeft(cur))
415                         cur.idx() = tabular.getLastCellInRow(0);
416                 else
417                         cur.idx() = 0;
418                 cur.pit() = 0;
419                 cur.pos() = 0;
420         } else {
421                 if (isRightToLeft(cur))
422                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
423                 else
424                         cur.idx() = tabular.getNumberOfCells() - 1;
425                 cur.pit() = 0;
426                 cur.pos() = cur.lastpos(); // FIXME crude guess
427         }
428         // this accesses the position cache before it is initialized
429         //resetPos(cur);
430         //cur.bv().fitCursor();
431 }
432
433
434 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
435 {
436         //lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
437         //lyxerr << "  cur:\n" << cur << endl;
438         CursorSlice sl = cur.top();
439         LCursor & bvcur = cur.bv().cursor();
440
441         switch (cmd.action) {
442
443         case LFUN_MOUSE_PRESS:
444                 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
445
446                 if (cmd.button() == mouse_button::button1) {
447                         cur.selection() = false;
448                         setCursorFromCoordinates(cur, cmd.x, cmd.y);
449                         cur.resetAnchor();
450                         bvcur = cur;
451                         break;
452                 }
453
454                 if (cmd.button() == mouse_button::button2) {
455                         // FIXME: pasting multiple cells (with insettabular's own
456                         // LFUN_PASTESELECTION still does not work! (jspitzm)
457                         cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
458                         cell(cur.idx())->dispatch(cur, cmd);
459                         break;
460                 }
461
462                 // we'll pop up the table dialog on release
463                 if (cmd.button() == mouse_button::button3)
464                         break;
465                 break;
466
467         case LFUN_MOUSE_MOTION:
468                 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
469                 if (cmd.button() == mouse_button::button1) {
470                         // only accept motions to places not deeper nested than the real anchor
471                         if (bvcur.anchor_.hasPart(cur)) {
472                                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
473                                 bvcur.setCursor(cur);
474                                 bvcur.selection() = true;
475                         } else
476                                 cur.undispatched();
477                 }
478                 break;
479
480         case LFUN_MOUSE_RELEASE:
481                 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
482                 if (cmd.button() == mouse_button::button3)
483                         InsetTabularMailer(*this).showDialog(&cur.bv());
484                 break;
485
486         case LFUN_CELL_BACKWARD:
487                 movePrevCell(cur);
488                 cur.selection() = false;
489                 break;
490
491         case LFUN_CELL_FORWARD:
492                 moveNextCell(cur);
493                 cur.selection() = false;
494                 break;
495
496         case LFUN_SCROLL_INSET: 
497                 if (!cmd.argument.empty()) {
498                         int const ww = cur.bv().workWidth();
499                         if (cmd.argument.find('.') != cmd.argument.npos)
500                                 setScroll(ww, static_cast<float>(convert<double>(cmd.argument)));
501                         else
502                                 setScroll(ww, convert<int>(cmd.argument));
503                 }
504                 break;
505
506         case LFUN_RIGHTSEL:
507         case LFUN_RIGHT:
508                 cell(cur.idx())->dispatch(cur, cmd);
509                 cur.dispatched(); // override the cell's decision
510                 if (sl == cur.top())
511                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
512                 if (sl == cur.top()) {
513                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
514                         cur.undispatched();
515                         resetPos(cur);
516                 }
517                 break;
518
519         case LFUN_LEFTSEL:
520         case LFUN_LEFT:
521                 cell(cur.idx())->dispatch(cur, cmd);
522                 cur.dispatched(); // override the cell's decision
523                 if (sl == cur.top())
524                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
525                 if (sl == cur.top()) {
526                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
527                         cur.undispatched();
528                         resetPos(cur);
529                 }
530                 break;
531
532         case LFUN_DOWNSEL:
533         case LFUN_DOWN:
534                 cell(cur.idx())->dispatch(cur, cmd);
535                 cur.dispatched(); // override the cell's decision
536                 if (sl == cur.top())
537                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
538                                 cur.idx() = tabular.getCellBelow(cur.idx());
539                                 cur.pit() = 0;
540                                 cur.pos() = 0;
541                                 resetPos(cur);
542                         }
543                 if (sl == cur.top()) {
544                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
545                         cur.undispatched();
546                         resetPos(cur);
547                 }
548                 break;
549
550         case LFUN_UPSEL:
551         case LFUN_UP:
552                 cell(cur.idx())->dispatch(cur, cmd);
553                 cur.dispatched(); // override the cell's decision
554                 if (sl == cur.top())
555                         if (tabular.row_of_cell(cur.idx()) != 0) {
556                                 cur.idx() = tabular.getCellAbove(cur.idx());
557                                 cur.pit() = cur.lastpit();
558                                 cur.pos() = cur.lastpos();
559                                 resetPos(cur);
560                         }
561                 if (sl == cur.top()) {
562                         cmd = FuncRequest(LFUN_FINISHED_UP);
563                         cur.undispatched();
564                         resetPos(cur);
565                 }
566                 break;
567
568 //      case LFUN_NEXT: {
569 //              //if (hasSelection())
570 //              //      cur.selection() = false;
571 //              col_type const col = tabular.column_of_cell(cur.idx());
572 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
573 //              if (t < yo() + tabular.getHeightOfTabular()) {
574 //                      cur.bv().scrollDocView(t);
575 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
576 //              } else {
577 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
578 //              }
579 //              cur.par() = 0;
580 //              cur.pos() = 0;
581 //              resetPos(cur);
582 //              break;
583 //      }
584 //
585 //      case LFUN_PRIOR: {
586 //              //if (hasSelection())
587 //              //      cur.selection() = false;
588 //              col_type const col = tabular.column_of_cell(cur.idx());
589 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
590 //              if (yo() < 0) {
591 //                      cur.bv().scrollDocView(t);
592 //                      if (yo() > 0)
593 //                              cur.idx() = col;
594 //                      else
595 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
596 //              } else {
597 //                      cur.idx() = col;
598 //              }
599 //              cur.par() = cur.lastpar();
600 //              cur.pos() = cur.lastpos();
601 //              resetPos(cur);
602 //              break;
603 //      }
604
605         case LFUN_LAYOUT_TABULAR:
606                 InsetTabularMailer(*this).showDialog(&cur.bv());
607                 break;
608
609         case LFUN_INSET_DIALOG_UPDATE:
610                 InsetTabularMailer(*this).updateDialog(&cur.bv());
611                 break;
612
613         case LFUN_TABULAR_FEATURE:
614                 if (!tabularFeatures(cur, cmd.argument))
615                         cur.undispatched();
616                 break;
617
618         // insert file functions
619         case LFUN_FILE_INSERT_ASCII_PARA:
620         case LFUN_FILE_INSERT_ASCII: {
621                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
622                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
623                         cur.undispatched();
624                 break;
625         }
626
627         case LFUN_CUT:
628                 if (tablemode(cur)) {
629                         if (copySelection(cur)) {
630                                 recordUndo(cur, Undo::DELETE);
631                                 cutSelection(cur);
632                         }
633                 }
634                 else
635                         cell(cur.idx())->dispatch(cur, cmd);
636                 break;
637
638         case LFUN_BACKSPACE:
639         case LFUN_DELETE:
640                 recordUndo(cur, Undo::DELETE);
641                 if (tablemode(cur))
642                         cutSelection(cur);
643                 else
644                         cell(cur.idx())->dispatch(cur, cmd);
645                 break;
646
647         case LFUN_COPY:
648                 if (!cur.selection())
649                         break;
650                 if (tablemode(cur)) {
651                         finishUndo();
652                         copySelection(cur);
653                 } else
654                         cell(cur.idx())->dispatch(cur, cmd);
655                 break;
656
657         case LFUN_PASTESELECTION: {
658                 string const clip = cur.bv().getClipboard();
659                 if (clip.empty())
660                         break;
661                 if (clip.find('\t') != string::npos) {
662                         col_type cols = 1;
663                         row_type rows = 1;
664                         col_type maxCols = 1;
665                         size_t len = clip.length();
666                         for (size_t p = 0; p < len; ++p) {
667                                 p = clip.find_first_of("\t\n", p);
668                                 if (p == string::npos)
669                                         break;
670                                 switch (clip[p]) {
671                                 case '\t':
672                                         ++cols;
673                                         break;
674                                 case '\n':
675                                         if (p + 1 < len)
676                                                 ++rows;
677                                         maxCols = max(cols, maxCols);
678                                         cols = 1;
679                                         break;
680                                 }
681                         }
682                         maxCols = max(cols, maxCols);
683
684                         paste_tabular.reset(
685                                 new LyXTabular(cur.buffer().params(), rows, maxCols));
686
687                         string::size_type op = 0;
688                         idx_type cell = 0;
689                         idx_type const cells =
690                                 paste_tabular->getNumberOfCells();
691                         cols = 0;
692                         LyXFont font;
693                         for (size_t p = 0; cell < cells && p < len; ++p) {
694                                 p = clip.find_first_of("\t\n", p);
695                                 if (p == string::npos || p >= len)
696                                         break;
697                                 switch (clip[p]) {
698                                 case '\t':
699                                         paste_tabular->getCellInset(cell)->
700                                                 setText(clip.substr(op, p - op), font);
701                                         ++cols;
702                                         ++cell;
703                                         break;
704                                 case '\n':
705                                         paste_tabular->getCellInset(cell)->
706                                                 setText(clip.substr(op, p - op), font);
707                                         while (cols++ < maxCols)
708                                                 ++cell;
709                                         cols = 0;
710                                         break;
711                                 }
712                                 op = p + 1;
713                         }
714                         // check for the last cell if there is no trailing '\n'
715                         if (cell < cells && op < len)
716                                 paste_tabular->getCellInset(cell)->
717                                         setText(clip.substr(op, len - op), font);
718                 } else if (!insertAsciiString(cur.bv(), clip, true)) {
719                         // so that the clipboard is used and it goes on
720                         // to default
721                         // and executes LFUN_PASTESELECTION in insettext!
722                         paste_tabular.reset();
723                 }
724                 // fall through
725         }
726
727         case LFUN_PASTE:
728                 if (hasPasteBuffer()) {
729                         recordUndo(cur, Undo::INSERT);
730                         pasteSelection(cur);
731                         break;
732                 }
733                 cell(cur.idx())->dispatch(cur, cmd);
734                 break;
735
736         case LFUN_EMPH:
737         case LFUN_BOLD:
738         case LFUN_ROMAN:
739         case LFUN_NOUN:
740         case LFUN_ITAL:
741         case LFUN_FRAK:
742         case LFUN_CODE:
743         case LFUN_SANS:
744         case LFUN_FREEFONT_APPLY:
745         case LFUN_FREEFONT_UPDATE:
746         case LFUN_FONT_SIZE:
747         case LFUN_UNDERLINE:
748         case LFUN_LANGUAGE:
749         case LFUN_CAPITALIZE_WORD:
750         case LFUN_UPCASE_WORD:
751         case LFUN_LOWCASE_WORD:
752         case LFUN_TRANSPOSE_CHARS:
753                 if (tablemode(cur)) {
754                         row_type rs, re;
755                         col_type cs, ce;
756                         getSelection(cur, rs, re, cs, ce);
757                         for (row_type i = rs; i <= re; ++i)
758                                 for (col_type j = cs; j <= ce; ++j) {
759                                         // cursor follows cell:
760                                         cur.idx() = tabular.getCellNumber(i, j);
761                                         // select this cell only:
762                                         cur.pos() = 0;
763                                         cur.resetAnchor();
764                                         cur.pos() = cur.top().lastpos();
765                                         cur.setCursor(cur);
766                                         cur.setSelection();
767                                         cell(cur.idx())->dispatch(cur, cmd);
768                                 }
769                         // Restore original selection
770                         cur.idx() = tabular.getCellNumber(rs, cs);
771                         cur.pos() = 0;
772                         cur.resetAnchor();
773                         cur.idx() = tabular.getCellNumber(re, ce);
774                         cur.pos() = cur.top().lastpos();
775                         cur.setCursor(cur);
776                         cur.setSelection();
777                         break;
778                 } else {
779                         cell(cur.idx())->dispatch(cur, cmd);
780                         break;
781                 }
782         default:
783                 // we try to handle this event in the insets dispatch function.
784                 cell(cur.idx())->dispatch(cur, cmd);
785                 break;
786         }
787
788         InsetTabularMailer(*this).updateDialog(&cur.bv());
789 }
790
791
792 // function sets an object as defined in func_status.h:
793 // states OK, Unknown, Disabled, On, Off.
794 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
795         FuncStatus & status) const
796 {
797         switch (cmd.action) {
798         case LFUN_TABULAR_FEATURE: {
799                 int action = LyXTabular::LAST_ACTION;
800                 int i = 0;
801                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
802                         string const tmp = tabularFeature[i].feature;
803                         if (tmp == cmd.argument.substr(0, tmp.length())) {
804                                 action = tabularFeature[i].action;
805                                 break;
806                         }
807                 }
808                 if (action == LyXTabular::LAST_ACTION) {
809                         status.clear();
810                         status.unknown(true);
811                         return true;
812                 }
813
814                 string const argument
815                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
816
817                 row_type sel_row_start = 0;
818                 row_type sel_row_end = 0;
819                 col_type dummy;
820                 LyXTabular::ltType dummyltt;
821                 bool flag = true;
822
823                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
824
825                 switch (action) {
826                 case LyXTabular::SET_PWIDTH:
827                 case LyXTabular::SET_MPWIDTH:
828                 case LyXTabular::SET_SPECIAL_COLUMN:
829                 case LyXTabular::SET_SPECIAL_MULTI:
830                 case LyXTabular::APPEND_ROW:
831                 case LyXTabular::APPEND_COLUMN:
832                 case LyXTabular::DELETE_ROW:
833                 case LyXTabular::DELETE_COLUMN:
834                 case LyXTabular::SET_ALL_LINES:
835                 case LyXTabular::UNSET_ALL_LINES:
836                         status.clear();
837                         return true;
838
839                 case LyXTabular::MULTICOLUMN:
840                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
841                         break;
842
843                 case LyXTabular::M_TOGGLE_LINE_TOP:
844                         flag = false;
845                 case LyXTabular::TOGGLE_LINE_TOP:
846                         status.setOnOff(tabular.topLine(cur.idx(), flag));
847                         break;
848
849                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
850                         flag = false;
851                 case LyXTabular::TOGGLE_LINE_BOTTOM:
852                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
853                         break;
854
855                 case LyXTabular::M_TOGGLE_LINE_LEFT:
856                         flag = false;
857                 case LyXTabular::TOGGLE_LINE_LEFT:
858                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
859                         break;
860
861                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
862                         flag = false;
863                 case LyXTabular::TOGGLE_LINE_RIGHT:
864                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
865                         break;
866
867                 case LyXTabular::M_ALIGN_LEFT:
868                         flag = false;
869                 case LyXTabular::ALIGN_LEFT:
870                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
871                         break;
872
873                 case LyXTabular::M_ALIGN_RIGHT:
874                         flag = false;
875                 case LyXTabular::ALIGN_RIGHT:
876                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
877                         break;
878
879                 case LyXTabular::M_ALIGN_CENTER:
880                         flag = false;
881                 case LyXTabular::ALIGN_CENTER:
882                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
883                         break;
884
885                 case LyXTabular::ALIGN_BLOCK:
886                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
887                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
888                         break;
889
890                 case LyXTabular::M_VALIGN_TOP:
891                         flag = false;
892                 case LyXTabular::VALIGN_TOP:
893                         status.setOnOff(
894                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
895                         break;
896
897                 case LyXTabular::M_VALIGN_BOTTOM:
898                         flag = false;
899                 case LyXTabular::VALIGN_BOTTOM:
900                         status.setOnOff(
901                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
902                         break;
903
904                 case LyXTabular::M_VALIGN_MIDDLE:
905                         flag = false;
906                 case LyXTabular::VALIGN_MIDDLE:
907                         status.setOnOff(
908                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
909                         break;
910
911                 case LyXTabular::SET_LONGTABULAR:
912                         status.setOnOff(tabular.isLongTabular());
913                         break;
914
915                 case LyXTabular::UNSET_LONGTABULAR:
916                         status.setOnOff(!tabular.isLongTabular());
917                         break;
918
919                 case LyXTabular::SET_ROTATE_TABULAR:
920                         status.setOnOff(tabular.getRotateTabular());
921                         break;
922
923                 case LyXTabular::UNSET_ROTATE_TABULAR:
924                         status.setOnOff(!tabular.getRotateTabular());
925                         break;
926
927                 case LyXTabular::SET_ROTATE_CELL:
928                         status.setOnOff(tabular.getRotateCell(cur.idx()));
929                         break;
930
931                 case LyXTabular::UNSET_ROTATE_CELL:
932                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
933                         break;
934
935                 case LyXTabular::SET_USEBOX:
936                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
937                         break;
938
939                 case LyXTabular::SET_LTFIRSTHEAD:
940                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941                         break;
942
943                 case LyXTabular::UNSET_LTFIRSTHEAD:
944                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
945                         break;
946
947                 case LyXTabular::SET_LTHEAD:
948                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
949                         break;
950
951                 case LyXTabular::UNSET_LTHEAD:
952                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
953                         break;
954
955                 case LyXTabular::SET_LTFOOT:
956                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
957                         break;
958
959                 case LyXTabular::UNSET_LTFOOT:
960                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
961                         break;
962
963                 case LyXTabular::SET_LTLASTFOOT:
964                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
965                         break;
966
967                 case LyXTabular::UNSET_LTLASTFOOT:
968                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
969                         break;
970
971                 case LyXTabular::SET_LTNEWPAGE:
972                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
973                         break;
974
975                 default:
976                         status.clear();
977                         status.enabled(false);
978                         break;
979                 }
980                 return true;
981         }
982
983         // These are only enabled inside tabular
984         case LFUN_CELL_BACKWARD:
985         case LFUN_CELL_FORWARD:
986                 status.enabled(true);
987                         return true;
988
989         // disable these with multiple cells selected
990         case LFUN_INSET_INSERT:
991         case LFUN_TABULAR_INSERT:
992         case LFUN_INSERT_CHARSTYLE:
993         case LFUN_INSET_FLOAT:
994         case LFUN_INSET_WIDE_FLOAT:
995         case LFUN_INSET_FOOTNOTE:
996         case LFUN_INSET_MARGINAL:
997         case LFUN_INSERT_MATH:
998         case LFUN_MATH_MODE:
999         case LFUN_MATH_MUTATE:
1000         case LFUN_MATH_DISPLAY:
1001         case LFUN_INSERT_NOTE:
1002         case LFUN_INSET_OPTARG:
1003         case LFUN_INSERT_BOX:
1004         case LFUN_INSERT_BRANCH:
1005         case LFUN_INSET_WRAP:
1006         case LFUN_INSET_ERT: {
1007                 if (tablemode(cur)) {
1008                         status.enabled(false);
1009                         return true;
1010                 }
1011         }
1012
1013         case LFUN_INSET_MODIFY:
1014                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1015                         status.enabled(true);
1016                         return true;
1017                 }
1018                 // Fall through
1019
1020         default:
1021                 // we try to handle this event in the insets dispatch function.
1022                 return cell(cur.idx())->getStatus(cur, cmd, status);
1023         }
1024 }
1025
1026
1027 int InsetTabular::latex(Buffer const & buf, ostream & os,
1028                         OutputParams const & runparams) const
1029 {
1030         return tabular.latex(buf, os, runparams);
1031 }
1032
1033
1034 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1035                         OutputParams const & runparams) const
1036 {
1037         int const dp = runparams.linelen ? runparams.depth : 0;
1038         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1039 }
1040
1041
1042 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1043                            OutputParams const & runparams) const
1044 {
1045         return tabular.linuxdoc(buf,os, runparams);
1046 }
1047
1048
1049 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1050                           OutputParams const & runparams) const
1051 {
1052         int ret = 0;
1053         InsetBase * master = 0;
1054
1055 #ifdef WITH_WARNINGS
1056 #warning Why not pass a proper DocIterator here?
1057 #endif
1058 #if 0
1059         // if the table is inside a float it doesn't need the informaltable
1060         // wrapper. Search for it.
1061         for (master = owner(); master; master = master->owner())
1062                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1063                         break;
1064 #endif
1065
1066         if (!master) {
1067                 os << "<informaltable>";
1068                 ++ret;
1069         }
1070         ret += tabular.docbook(buf, os, runparams);
1071         if (!master) {
1072                 os << "</informaltable>";
1073                 ++ret;
1074         }
1075         return ret;
1076 }
1077
1078
1079 void InsetTabular::validate(LaTeXFeatures & features) const
1080 {
1081         tabular.validate(features);
1082 }
1083
1084
1085 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1086 {
1087         return tabular.getCellInset(idx);
1088 }
1089
1090
1091 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1092 {
1093         return tabular.getCellInset(idx);
1094 }
1095
1096
1097 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1098 {
1099         cell(sl.idx())->getCursorPos(sl, x, y);
1100
1101         // y offset     correction
1102         int const row = tabular.row_of_cell(sl.idx());
1103         for (int i = 0; i <= row; ++i) {
1104                 if (i != 0) {
1105                         y += tabular.getAscentOfRow(i);
1106                         y += tabular.getAdditionalHeight(i);
1107                 }
1108                 if (i != row)
1109                         y += tabular.getDescentOfRow(i);
1110         }
1111
1112         // x offset correction
1113         int const col = tabular.column_of_cell(sl.idx());
1114         int idx = tabular.getCellNumber(row, 0);
1115         for (int j = 0; j < col; ++j) {
1116                 if (tabular.isPartOfMultiColumn(row, j))
1117                         continue;
1118                 x += tabular.getWidthOfColumn(idx);
1119                 ++idx;
1120         }
1121         x += tabular.getBeginningOfTextInCell(idx);
1122         x += ADD_TO_TABULAR_WIDTH;
1123         x += scroll();
1124 }
1125
1126
1127 int InsetTabular::dist(idx_type const cell, int x, int y) const
1128 {
1129         int xx = 0;
1130         int yy = 0;
1131         InsetBase const & inset = *tabular.getCellInset(cell);
1132         Point o = theCoords.getInsets().xy(&inset);
1133         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1134         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1135         int const ybeg = o.y_ - inset.ascent();
1136         row_type const row = tabular.row_of_cell(cell);
1137         int const rowheight = tabular.getAscentOfRow(row)
1138                         + tabular.getDescentOfRow(row)
1139                         + tabular.getAdditionalHeight(row);
1140         int const yend = ybeg + rowheight;
1141
1142         if (x < xbeg)
1143                 xx = xbeg - x;
1144         else if (x > xend)
1145                 xx = x - xend;
1146
1147         if (y < ybeg) 
1148                 yy = ybeg - y;
1149         else if (y > yend) 
1150                 yy = y - yend;
1151
1152         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1153         //       << " ybeg=" << ybeg << " yend=" << yend
1154         //       << " xx=" << xx << " yy=" << yy
1155         //       << " dist=" << xx + yy << endl;
1156         return xx + yy;
1157 }
1158
1159
1160 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1161 {
1162         //lyxerr << "InsetTabular::editXY: " << this << endl;
1163         cur.selection() = false;
1164         cur.push(*this);
1165         cur.idx() = getNearestCell(x, y);
1166         resetPos(cur);
1167         return cell(cur.idx())->text_.editXY(cur, x, y);
1168         //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1169 }
1170
1171
1172 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1173 {
1174         //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1175         cur.idx() = getNearestCell(x, y);
1176         resetPos(cur);
1177         return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1178 }
1179
1180
1181 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1182 {
1183         //lyxerr << "# InsetTabular::getNearestCell()  x=" << x << " y=" << y << endl;
1184         idx_type idx_min = 0;
1185         int dist_min = std::numeric_limits<int>::max();
1186         for (idx_type i = 0; i < nargs(); ++i) {
1187                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1188                         int d = dist(i, x, y);
1189                         if (d < dist_min) {
1190                                 dist_min = d;
1191                                 idx_min = i;
1192                         }
1193                 }
1194         }
1195         return idx_min;
1196 }
1197
1198
1199 int InsetTabular::getCellXPos(idx_type const cell) const
1200 {
1201         idx_type c = cell;
1202
1203         for (; !tabular.isFirstCellInRow(c); --c)
1204                 ;
1205         int lx = tabular.getWidthOfColumn(cell);
1206         for (; c < cell; ++c)
1207                 lx += tabular.getWidthOfColumn(c);
1208
1209         return lx - tabular.getWidthOfColumn(cell);
1210 }
1211
1212
1213 void InsetTabular::resetPos(LCursor & cur) const
1214 {
1215         BufferView & bv = cur.bv();
1216         int const maxwidth = bv.workWidth();
1217 //      col_type const actcol = tabular.column_of_cell(cur.idx());
1218 //      int const offset = ADD_TO_TABULAR_WIDTH + 2;
1219 //      int const new_x = getCellXPos(cur.idx()) + offset;
1220 //      int const old_x = cursorx_;
1221 //      int const col_width = tabular.getWidthOfColumn(cur.idx());
1222 //      cursorx_ = new_x;
1223 //    cursor.x(getCellXPos(cur.idx()) + offset);
1224 //      if (actcol < tabular.columns() - 1 && scroll(false) &&
1225 //              tabular.getWidthOfTabular() < maxwidth - 20)
1226 //      {
1227 //              setScroll(maxwidth, 0.0F);
1228 //      } else if (cursorx_ - offset > 20 &&
1229 //                 cursorx_ - offset + col_width > maxwidth - 20) {
1230 //              setScroll(maxwidth, - col_width - 20);
1231 //      } else if (cursorx_ - offset < 20) {
1232 //              setScroll(maxwidth, 20 - cursorx_ + offset);
1233 //      } else if (scroll() && xo() > 20 &&
1234 //                 xo() + tabular.getWidthOfTabular() > maxwidth - 20) {
1235 //              setScroll(maxwidth, old_x - cursorx_);
1236 //      }
1237
1238         if (&cur.inset() != this) {
1239                 setScroll(maxwidth, 0.0f);
1240         } else {
1241                 int const X1 = 0;
1242                 int const X2 = maxwidth;
1243                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1244                 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1245                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1246
1247                 if (x1 < X1)
1248                         setScroll(maxwidth, X1 + 20 - x1);
1249                 else if (x2 > X2)
1250                         setScroll(maxwidth, X2 - 20 - x2);
1251         }
1252
1253         cur.needsUpdate();
1254
1255         InsetTabularMailer(*this).updateDialog(&bv);
1256 }
1257
1258
1259 void InsetTabular::moveNextCell(LCursor & cur)
1260 {
1261         if (isRightToLeft(cur)) {
1262                 if (tabular.isFirstCellInRow(cur.idx())) {
1263                         row_type const row = tabular.row_of_cell(cur.idx());
1264                         if (row == tabular.rows() - 1)
1265                                 return;
1266                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1267                 } else {
1268                         if (cur.idx() == 0)
1269                                 return;
1270                         --cur.idx();
1271                 }
1272         } else {
1273                 if (tabular.isLastCell(cur.idx()))
1274                         return;
1275                 ++cur.idx();
1276         }
1277         cur.pit() = 0;
1278         cur.pos() = 0;
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                 cur.idx() = tabular.getCellNumber(row, column);
1448                 break;
1449
1450         case LyXTabular::DELETE_ROW:
1451                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1452                         tabular.deleteRow(sel_row_start);
1453                 if (sel_row_start >= tabular.rows())
1454                         --sel_row_start;
1455                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1456                 cur.pit() = 0;
1457                 cur.pos() = 0;
1458                 cur.selection() = false;
1459                 break;
1460
1461         case LyXTabular::DELETE_COLUMN:
1462                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1463                         tabular.deleteColumn(sel_col_start);
1464                 if (sel_col_start >= tabular.columns())
1465                         --sel_col_start;
1466                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1467                 cur.pit() = 0;
1468                 cur.pos() = 0;
1469                 cur.selection() = false;
1470                 break;
1471
1472         case LyXTabular::M_TOGGLE_LINE_TOP:
1473                 flag = false;
1474         case LyXTabular::TOGGLE_LINE_TOP: {
1475                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1476                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1477                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478                                 tabular.setTopLine(
1479                                         tabular.getCellNumber(i, j),
1480                                         lineSet, flag);
1481                 break;
1482         }
1483
1484         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1485                 flag = false;
1486         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1487                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1488                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1489                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1490                                 tabular.setBottomLine(
1491                                         tabular.getCellNumber(i, j),
1492                                         lineSet,
1493                                         flag);
1494                 break;
1495         }
1496
1497         case LyXTabular::M_TOGGLE_LINE_LEFT:
1498                 flag = false;
1499         case LyXTabular::TOGGLE_LINE_LEFT: {
1500                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1501                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1502                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1503                                 tabular.setLeftLine(
1504                                         tabular.getCellNumber(i,j),
1505                                         lineSet,
1506                                         flag);
1507                 break;
1508         }
1509
1510         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1511                 flag = false;
1512         case LyXTabular::TOGGLE_LINE_RIGHT: {
1513                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1514                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1515                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1516                                 tabular.setRightLine(
1517                                         tabular.getCellNumber(i,j),
1518                                         lineSet,
1519                                         flag);
1520                 break;
1521         }
1522
1523         case LyXTabular::M_ALIGN_LEFT:
1524         case LyXTabular::M_ALIGN_RIGHT:
1525         case LyXTabular::M_ALIGN_CENTER:
1526                 flag = false;
1527         case LyXTabular::ALIGN_LEFT:
1528         case LyXTabular::ALIGN_RIGHT:
1529         case LyXTabular::ALIGN_CENTER:
1530         case LyXTabular::ALIGN_BLOCK:
1531                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1532                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1533                                 tabular.setAlignment(
1534                                         tabular.getCellNumber(i, j),
1535                                         setAlign,
1536                                         flag);
1537                 break;
1538
1539         case LyXTabular::M_VALIGN_TOP:
1540         case LyXTabular::M_VALIGN_BOTTOM:
1541         case LyXTabular::M_VALIGN_MIDDLE:
1542                 flag = false;
1543         case LyXTabular::VALIGN_TOP:
1544         case LyXTabular::VALIGN_BOTTOM:
1545         case LyXTabular::VALIGN_MIDDLE:
1546                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1547                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1548                                 tabular.setVAlignment(
1549                                         tabular.getCellNumber(i, j),
1550                                         setVAlign, flag);
1551                 break;
1552
1553         case LyXTabular::MULTICOLUMN: {
1554                 if (sel_row_start != sel_row_end) {
1555 #ifdef WITH_WARNINGS
1556 #warning Need I say it ? This is horrible.
1557 #endif
1558                         Alert::error(_("Error setting multicolumn"),
1559                                    _("You cannot set multicolumn vertically."));
1560                         return;
1561                 }
1562                 if (!cur.selection()) {
1563                         // just multicol for one single cell
1564                         // check whether we are completely in a multicol
1565                         if (tabular.isMultiColumn(cur.idx()))
1566                                 tabular.unsetMultiColumn(cur.idx());
1567                         else
1568                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1569                         break;
1570                 }
1571                 // we have a selection so this means we just add all this
1572                 // cells to form a multicolumn cell
1573                 idx_type const s_start = cur.selBegin().idx();
1574                 idx_type const s_end = cur.selEnd().idx();
1575                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1576                 cur.idx() = s_start;
1577                 cur.pit() = 0;
1578                 cur.pos() = 0;
1579                 cur.selection() = false;
1580                 break;
1581         }
1582
1583         case LyXTabular::SET_ALL_LINES:
1584                 setLines = true;
1585         case LyXTabular::UNSET_ALL_LINES:
1586                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1587                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1588                                 tabular.setAllLines(
1589                                         tabular.getCellNumber(i,j), setLines);
1590                 break;
1591
1592         case LyXTabular::SET_LONGTABULAR:
1593                 tabular.setLongTabular(true);
1594                 break;
1595
1596         case LyXTabular::UNSET_LONGTABULAR:
1597                 tabular.setLongTabular(false);
1598                 break;
1599
1600         case LyXTabular::SET_ROTATE_TABULAR:
1601                 tabular.setRotateTabular(true);
1602                 break;
1603
1604         case LyXTabular::UNSET_ROTATE_TABULAR:
1605                 tabular.setRotateTabular(false);
1606                 break;
1607
1608         case LyXTabular::SET_ROTATE_CELL:
1609                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1610                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1611                                 tabular.setRotateCell(
1612                                         tabular.getCellNumber(i, j), true);
1613                 break;
1614
1615         case LyXTabular::UNSET_ROTATE_CELL:
1616                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1617                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1618                                 tabular.setRotateCell(
1619                                         tabular.getCellNumber(i, j), false);
1620                 break;
1621
1622         case LyXTabular::SET_USEBOX: {
1623                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1624                 if (val == tabular.getUsebox(cur.idx()))
1625                         val = LyXTabular::BOX_NONE;
1626                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1627                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1628                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1629                 break;
1630         }
1631
1632         case LyXTabular::UNSET_LTFIRSTHEAD:
1633                 flag = false;
1634         case LyXTabular::SET_LTFIRSTHEAD:
1635                 tabular.getRowOfLTFirstHead(row, ltt);
1636                 checkLongtableSpecial(ltt, value, flag);
1637                 tabular.setLTHead(row, flag, ltt, true);
1638                 break;
1639
1640         case LyXTabular::UNSET_LTHEAD:
1641                 flag = false;
1642         case LyXTabular::SET_LTHEAD:
1643                 tabular.getRowOfLTHead(row, ltt);
1644                 checkLongtableSpecial(ltt, value, flag);
1645                 tabular.setLTHead(row, flag, ltt, false);
1646                 break;
1647
1648         case LyXTabular::UNSET_LTFOOT:
1649                 flag = false;
1650         case LyXTabular::SET_LTFOOT:
1651                 tabular.getRowOfLTFoot(row, ltt);
1652                 checkLongtableSpecial(ltt, value, flag);
1653                 tabular.setLTFoot(row, flag, ltt, false);
1654                 break;
1655
1656         case LyXTabular::UNSET_LTLASTFOOT:
1657                 flag = false;
1658         case LyXTabular::SET_LTLASTFOOT:
1659                 tabular.getRowOfLTLastFoot(row, ltt);
1660                 checkLongtableSpecial(ltt, value, flag);
1661                 tabular.setLTFoot(row, flag, ltt, true);
1662                 break;
1663
1664         case LyXTabular::SET_LTNEWPAGE:
1665                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1666                 break;
1667
1668         // dummy stuff just to avoid warnings
1669         case LyXTabular::LAST_ACTION:
1670                 break;
1671         }
1672
1673         InsetTabularMailer(*this).updateDialog(&bv);
1674 }
1675
1676
1677 bool InsetTabular::showInsetDialog(BufferView * bv) const
1678 {
1679         InsetTabularMailer(*this).showDialog(bv);
1680         return true;
1681 }
1682
1683
1684 void InsetTabular::openLayoutDialog(BufferView * bv) const
1685 {
1686         InsetTabularMailer(*this).showDialog(bv);
1687 }
1688
1689
1690 void InsetTabular::getLabelList(Buffer const & buffer,
1691                                 vector<string> & list) const
1692 {
1693         tabular.getLabelList(buffer, list);
1694 }
1695
1696
1697 bool InsetTabular::copySelection(LCursor & cur)
1698 {
1699         if (!cur.selection())
1700                 return false;
1701
1702         row_type rs, re;
1703         col_type cs, ce;
1704         getSelection(cur, rs, re, cs, ce);
1705
1706         paste_tabular.reset(new LyXTabular(tabular));
1707
1708         for (row_type i = 0; i < rs; ++i)
1709                 paste_tabular->deleteRow(0);
1710
1711         row_type const rows = re - rs + 1;
1712         while (paste_tabular->rows() > rows)
1713                 paste_tabular->deleteRow(rows);
1714
1715         paste_tabular->setTopLine(0, true, true);
1716         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1717                                      true, true);
1718
1719         for (col_type i = 0; i < cs; ++i)
1720                 paste_tabular->deleteColumn(0);
1721
1722         col_type const columns = ce - cs + 1;
1723         while (paste_tabular->columns() > columns)
1724                 paste_tabular->deleteColumn(columns);
1725
1726         paste_tabular->setLeftLine(0, true, true);
1727         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1728                                     true, true);
1729
1730         ostringstream os;
1731         OutputParams const runparams;
1732         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1733         cur.bv().stuffClipboard(os.str());
1734         return true;
1735 }
1736
1737
1738 bool InsetTabular::pasteSelection(LCursor & cur)
1739 {
1740         if (!paste_tabular)
1741                 return false;
1742         col_type const actcol = tabular.column_of_cell(cur.idx());
1743         row_type const actrow = tabular.row_of_cell(cur.idx());
1744         for (row_type r1 = 0, r2 = actrow;
1745              r1 < paste_tabular->rows() && r2 < tabular.rows();
1746              ++r1, ++r2) {
1747                 for (col_type c1 = 0, c2 = actcol;
1748                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1749                     ++c1, ++c2) {
1750                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1751                             tabular.isPartOfMultiColumn(r2, c2))
1752                                 continue;
1753                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1754                                 --c2;
1755                                 continue;
1756                         }
1757                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1758                                 --c1;
1759                                 continue;
1760                         }
1761                         shared_ptr<InsetText> inset(
1762                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1763                         tabular.setCellInset(r2, c2, inset);
1764                         inset->markNew();
1765                         cur.pos() = 0;
1766                 }
1767         }
1768         return true;
1769 }
1770
1771
1772 void InsetTabular::cutSelection(LCursor & cur)
1773 {
1774         if (!cur.selection())
1775                 return;
1776
1777         bool const track = cur.buffer().params().tracking_changes;
1778         row_type rs, re;
1779         col_type cs, ce;
1780         getSelection(cur, rs, re, cs, ce);
1781         for (row_type i = rs; i <= re; ++i)
1782                 for (col_type j = cs; j <= ce; ++j)
1783                         cell(tabular.getCellNumber(i, j))->clear(track);
1784
1785         // cursor position might be invalid now
1786         cur.pos() = cur.lastpos();
1787         cur.clearSelection();
1788 }
1789
1790
1791 bool InsetTabular::isRightToLeft(LCursor & cur) const
1792 {
1793         BOOST_ASSERT(cur.depth() > 1);
1794         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1795         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1796         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1797                                          parentpos).language()->RightToLeft();
1798 }
1799
1800
1801 void InsetTabular::getSelection(LCursor & cur,
1802         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1803 {
1804         CursorSlice const & beg = cur.selBegin();
1805         CursorSlice const & end = cur.selEnd();
1806         cs = tabular.column_of_cell(beg.idx());
1807         ce = tabular.column_of_cell(end.idx());
1808         if (cs > ce) {
1809                 ce = cs;
1810                 cs = tabular.column_of_cell(end.idx());
1811         } else {
1812                 ce = tabular.right_column_of_cell(end.idx());
1813         }
1814
1815         rs = tabular.row_of_cell(beg.idx());
1816         re = tabular.row_of_cell(end.idx());
1817         if (rs > re)
1818                 swap(rs, re);
1819 }
1820
1821
1822 size_t InsetTabular::nargs() const
1823 {
1824         return tabular.getNumberOfCells();
1825 }
1826
1827
1828 LyXText * InsetTabular::getText(int idx) const
1829 {
1830         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1831 }
1832
1833
1834 void InsetTabular::markErased()
1835 {
1836         for (idx_type idx = 0; idx < nargs(); ++idx)
1837                 cell(idx)->markErased();
1838 }
1839
1840
1841 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1842 {
1843 #if 0
1844         idx_type const cell = tabular.getCellFromInset(in);
1845         // FIXME: getCellFromInset() returns now always a valid cell, so
1846         // the stuff below can be deleted, and instead we have:
1847         return tabular.getPWidth(cell).zero();
1848
1849         if (cell != npos)
1850                 return tabular.getPWidth(cell).zero();
1851
1852         // this is a workaround for a crash (New, Insert->Tabular,
1853         // Insert->FootNote)
1854         if (!owner())
1855                 return false;
1856
1857         // well we didn't obviously find it so maybe our owner knows more
1858         BOOST_ASSERT(owner());
1859         return owner()->forceDefaultParagraphs(in);
1860 #endif
1861         return false;
1862 }
1863
1864
1865 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1866                                      bool usePaste)
1867 {
1868         if (buf.length() <= 0)
1869                 return true;
1870
1871         col_type cols = 1;
1872         row_type rows = 1;
1873         col_type maxCols = 1;
1874         string::size_type const len = buf.length();
1875         string::size_type p = 0;
1876
1877         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1878                 switch (buf[p]) {
1879                 case '\t':
1880                         ++cols;
1881                         break;
1882                 case '\n':
1883                         if (p + 1 < len)
1884                                 ++rows;
1885                         maxCols = max(cols, maxCols);
1886                         cols = 1;
1887                         break;
1888                 }
1889                 ++p;
1890         }
1891         maxCols = max(cols, maxCols);
1892         LyXTabular * loctab;
1893         idx_type cell = 0;
1894         col_type ocol = 0;
1895         row_type row = 0;
1896         if (usePaste) {
1897                 paste_tabular.reset(
1898                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1899                 loctab = paste_tabular.get();
1900                 cols = 0;
1901         } else {
1902                 loctab = &tabular;
1903                 cell = bv.cursor().idx();
1904                 ocol = tabular.column_of_cell(cell);
1905                 row = tabular.row_of_cell(cell);
1906         }
1907
1908         string::size_type op = 0;
1909         idx_type const cells = loctab->getNumberOfCells();
1910         p = 0;
1911         cols = ocol;
1912         rows = loctab->rows();
1913         col_type const columns = loctab->columns();
1914
1915         while (cell < cells && p < len && row < rows &&
1916                (p = buf.find_first_of("\t\n", p)) != string::npos)
1917         {
1918                 if (p >= len)
1919                         break;
1920                 switch (buf[p]) {
1921                 case '\t':
1922                         // we can only set this if we are not too far right
1923                         if (cols < columns) {
1924                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1925                                 Paragraph & par = inset->text_.getPar(0);
1926                                 LyXFont const font = inset->text_.getFont(par, 0);
1927                                 inset->setText(buf.substr(op, p - op), font);
1928                                 ++cols;
1929                                 ++cell;
1930                         }
1931                         break;
1932                 case '\n':
1933                         // we can only set this if we are not too far right
1934                         if (cols < columns) {
1935                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1936                                 Paragraph & par = inset->text_.getPar(0);
1937                                 LyXFont const font = inset->text_.getFont(par, 0);
1938                                 inset->setText(buf.substr(op, p - op), font);
1939                         }
1940                         cols = ocol;
1941                         ++row;
1942                         if (row < rows)
1943                                 cell = loctab->getCellNumber(row, cols);
1944                         break;
1945                 }
1946                 ++p;
1947                 op = p;
1948         }
1949         // check for the last cell if there is no trailing '\n'
1950         if (cell < cells && op < len) {
1951                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1952                 Paragraph & par = inset->text_.getPar(0);
1953                 LyXFont const font = inset->text_.getFont(par, 0);
1954                 inset->setText(buf.substr(op, len - op), font);
1955         }
1956         return true;
1957 }
1958
1959
1960 void InsetTabular::addPreview(PreviewLoader & loader) const
1961 {
1962         row_type const rows = tabular.rows();
1963         col_type const columns = tabular.columns();
1964         for (row_type i = 0; i < rows; ++i) {
1965                 for (col_type j = 0; j < columns; ++j)
1966                         tabular.getCellInset(i, j)->addPreview(loader);
1967         }
1968 }
1969
1970
1971 bool InsetTabular::tablemode(LCursor & cur) const
1972 {
1973         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1974 }
1975
1976
1977
1978
1979
1980 string const InsetTabularMailer::name_("tabular");
1981
1982 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1983         : inset_(const_cast<InsetTabular &>(inset))
1984 {}
1985
1986
1987 string const InsetTabularMailer::inset2string(Buffer const &) const
1988 {
1989         return params2string(inset_);
1990 }
1991
1992
1993 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1994 {
1995         istringstream data(in);
1996         LyXLex lex(0,0);
1997         lex.setStream(data);
1998
1999         if (in.empty())
2000                 return;
2001
2002         string token;
2003         lex >> token;
2004         if (!lex || token != name_)
2005                 return print_mailer_error("InsetTabularMailer", in, 1,
2006                                           name_);
2007
2008         // This is part of the inset proper that is usually swallowed
2009         // by Buffer::readInset
2010         lex >> token;
2011         if (!lex || token != "Tabular")
2012                 return print_mailer_error("InsetTabularMailer", in, 2,
2013                                           "Tabular");
2014
2015         Buffer const & buffer = inset.buffer();
2016         inset.read(buffer, lex);
2017 }
2018
2019
2020 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2021 {
2022         ostringstream data;
2023         data << name_ << ' ';
2024         inset.write(inset.buffer(), data);
2025         data << "\\end_inset\n";
2026         return data.str();
2027 }