]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Don't store row and col in cursor, add some documentation about idx
[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                         break;
499                 if (cmd.argument.find('.') != cmd.argument.npos)
500                         scroll(cur.bv(), static_cast<float>(convert<double>(cmd.argument)));
501                 else
502                         scroll(cur.bv(), convert<int>(cmd.argument));
503                 break;
504
505         case LFUN_RIGHTSEL:
506         case LFUN_RIGHT:
507                 cell(cur.idx())->dispatch(cur, cmd);
508                 cur.dispatched(); // override the cell's decision
509                 if (sl == cur.top())
510                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
511                 if (sl == cur.top()) {
512                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
513                         cur.undispatched();
514                         resetPos(cur);
515                 }
516                 break;
517
518         case LFUN_LEFTSEL:
519         case LFUN_LEFT:
520                 cell(cur.idx())->dispatch(cur, cmd);
521                 cur.dispatched(); // override the cell's decision
522                 if (sl == cur.top())
523                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
524                 if (sl == cur.top()) {
525                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
526                         cur.undispatched();
527                         resetPos(cur);
528                 }
529                 break;
530
531         case LFUN_DOWNSEL:
532         case LFUN_DOWN:
533                 cell(cur.idx())->dispatch(cur, cmd);
534                 cur.dispatched(); // override the cell's decision
535                 if (sl == cur.top())
536                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
537                                 cur.idx() = tabular.getCellBelow(cur.idx());
538                                 cur.pit() = 0;
539                                 cur.pos() = 0;
540                                 resetPos(cur);
541                         }
542                 if (sl == cur.top()) {
543                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
544                         cur.undispatched();
545                         resetPos(cur);
546                 }
547                 break;
548
549         case LFUN_UPSEL:
550         case LFUN_UP:
551                 cell(cur.idx())->dispatch(cur, cmd);
552                 cur.dispatched(); // override the cell's decision
553                 if (sl == cur.top())
554                         if (tabular.row_of_cell(cur.idx()) != 0) {
555                                 cur.idx() = tabular.getCellAbove(cur.idx());
556                                 cur.pit() = cur.lastpit();
557                                 cur.pos() = cur.lastpos();
558                                 resetPos(cur);
559                         }
560                 if (sl == cur.top()) {
561                         cmd = FuncRequest(LFUN_FINISHED_UP);
562                         cur.undispatched();
563                         resetPos(cur);
564                 }
565                 break;
566
567 //      case LFUN_NEXT: {
568 //              //if (hasSelection())
569 //              //      cur.selection() = false;
570 //              col_type const col = tabular.column_of_cell(cur.idx());
571 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
572 //              if (t < yo() + tabular.getHeightOfTabular()) {
573 //                      cur.bv().scrollDocView(t);
574 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
575 //              } else {
576 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
577 //              }
578 //              cur.par() = 0;
579 //              cur.pos() = 0;
580 //              resetPos(cur);
581 //              break;
582 //      }
583 //
584 //      case LFUN_PRIOR: {
585 //              //if (hasSelection())
586 //              //      cur.selection() = false;
587 //              col_type const col = tabular.column_of_cell(cur.idx());
588 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
589 //              if (yo() < 0) {
590 //                      cur.bv().scrollDocView(t);
591 //                      if (yo() > 0)
592 //                              cur.idx() = col;
593 //                      else
594 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
595 //              } else {
596 //                      cur.idx() = col;
597 //              }
598 //              cur.par() = cur.lastpar();
599 //              cur.pos() = cur.lastpos();
600 //              resetPos(cur);
601 //              break;
602 //      }
603
604         case LFUN_LAYOUT_TABULAR:
605                 InsetTabularMailer(*this).showDialog(&cur.bv());
606                 break;
607
608         case LFUN_INSET_DIALOG_UPDATE:
609                 InsetTabularMailer(*this).updateDialog(&cur.bv());
610                 break;
611
612         case LFUN_TABULAR_FEATURE:
613                 if (!tabularFeatures(cur, cmd.argument))
614                         cur.undispatched();
615                 break;
616
617         // insert file functions
618         case LFUN_FILE_INSERT_ASCII_PARA:
619         case LFUN_FILE_INSERT_ASCII: {
620                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
621                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
622                         cur.undispatched();
623                 break;
624         }
625
626         case LFUN_CUT:
627                 if (tablemode(cur)) {
628                         if (copySelection(cur)) {
629                                 recordUndo(cur, Undo::DELETE);
630                                 cutSelection(cur);
631                         }
632                 }
633                 else
634                         cell(cur.idx())->dispatch(cur, cmd);
635                 break;
636
637         case LFUN_BACKSPACE:
638         case LFUN_DELETE:
639                 recordUndo(cur, Undo::DELETE);
640                 if (tablemode(cur))
641                         cutSelection(cur);
642                 else
643                         cell(cur.idx())->dispatch(cur, cmd);
644                 break;
645
646         case LFUN_COPY:
647                 if (!cur.selection())
648                         break;
649                 if (tablemode(cur)) {
650                         finishUndo();
651                         copySelection(cur);
652                 } else
653                         cell(cur.idx())->dispatch(cur, cmd);
654                 break;
655
656         case LFUN_PASTESELECTION: {
657                 string const clip = cur.bv().getClipboard();
658                 if (clip.empty())
659                         break;
660                 if (clip.find('\t') != string::npos) {
661                         col_type cols = 1;
662                         row_type rows = 1;
663                         col_type maxCols = 1;
664                         size_t len = clip.length();
665                         for (size_t p = 0; p < len; ++p) {
666                                 p = clip.find_first_of("\t\n", p);
667                                 if (p == string::npos)
668                                         break;
669                                 switch (clip[p]) {
670                                 case '\t':
671                                         ++cols;
672                                         break;
673                                 case '\n':
674                                         if (p + 1 < len)
675                                                 ++rows;
676                                         maxCols = max(cols, maxCols);
677                                         cols = 1;
678                                         break;
679                                 }
680                         }
681                         maxCols = max(cols, maxCols);
682
683                         paste_tabular.reset(
684                                 new LyXTabular(cur.buffer().params(), rows, maxCols));
685
686                         string::size_type op = 0;
687                         idx_type cell = 0;
688                         idx_type const cells =
689                                 paste_tabular->getNumberOfCells();
690                         cols = 0;
691                         LyXFont font;
692                         for (size_t p = 0; cell < cells && p < len; ++p) {
693                                 p = clip.find_first_of("\t\n", p);
694                                 if (p == string::npos || p >= len)
695                                         break;
696                                 switch (clip[p]) {
697                                 case '\t':
698                                         paste_tabular->getCellInset(cell)->
699                                                 setText(clip.substr(op, p - op), font);
700                                         ++cols;
701                                         ++cell;
702                                         break;
703                                 case '\n':
704                                         paste_tabular->getCellInset(cell)->
705                                                 setText(clip.substr(op, p - op), font);
706                                         while (cols++ < maxCols)
707                                                 ++cell;
708                                         cols = 0;
709                                         break;
710                                 }
711                                 op = p + 1;
712                         }
713                         // check for the last cell if there is no trailing '\n'
714                         if (cell < cells && op < len)
715                                 paste_tabular->getCellInset(cell)->
716                                         setText(clip.substr(op, len - op), font);
717                 } else if (!insertAsciiString(cur.bv(), clip, true)) {
718                         // so that the clipboard is used and it goes on
719                         // to default
720                         // and executes LFUN_PASTESELECTION in insettext!
721                         paste_tabular.reset();
722                 }
723                 // fall through
724         }
725
726         case LFUN_PASTE:
727                 if (hasPasteBuffer()) {
728                         recordUndo(cur, Undo::INSERT);
729                         pasteSelection(cur);
730                         break;
731                 }
732                 cell(cur.idx())->dispatch(cur, cmd);
733                 break;
734
735         case LFUN_EMPH:
736         case LFUN_BOLD:
737         case LFUN_ROMAN:
738         case LFUN_NOUN:
739         case LFUN_ITAL:
740         case LFUN_FRAK:
741         case LFUN_CODE:
742         case LFUN_SANS:
743         case LFUN_FREEFONT_APPLY:
744         case LFUN_FREEFONT_UPDATE:
745         case LFUN_FONT_SIZE:
746         case LFUN_UNDERLINE:
747         case LFUN_LANGUAGE:
748         case LFUN_CAPITALIZE_WORD:
749         case LFUN_UPCASE_WORD:
750         case LFUN_LOWCASE_WORD:
751         case LFUN_TRANSPOSE_CHARS:
752                 if (tablemode(cur)) {
753                         row_type rs, re;
754                         col_type cs, ce;
755                         getSelection(cur, rs, re, cs, ce);
756                         for (row_type i = rs; i <= re; ++i)
757                                 for (col_type j = cs; j <= ce; ++j) {
758                                         // cursor follows cell:
759                                         cur.idx() = tabular.getCellNumber(i, j);
760                                         // select this cell only:
761                                         cur.pos() = 0;
762                                         cur.resetAnchor();
763                                         cur.pos() = cur.top().lastpos();
764                                         cur.setCursor(cur);
765                                         cur.setSelection();
766                                         cell(cur.idx())->dispatch(cur, cmd);
767                                 }
768                         // Restore original selection
769                         cur.idx() = tabular.getCellNumber(rs, cs);
770                         cur.pos() = 0;
771                         cur.resetAnchor();
772                         cur.idx() = tabular.getCellNumber(re, ce);
773                         cur.pos() = cur.top().lastpos();
774                         cur.setCursor(cur);
775                         cur.setSelection();
776                         break;
777                 } else {
778                         cell(cur.idx())->dispatch(cur, cmd);
779                         break;
780                 }
781         default:
782                 // we try to handle this event in the insets dispatch function.
783                 cell(cur.idx())->dispatch(cur, cmd);
784                 break;
785         }
786
787         InsetTabularMailer(*this).updateDialog(&cur.bv());
788 }
789
790
791 // function sets an object as defined in func_status.h:
792 // states OK, Unknown, Disabled, On, Off.
793 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
794         FuncStatus & status) const
795 {
796         switch (cmd.action) {
797         case LFUN_TABULAR_FEATURE: {
798                 int action = LyXTabular::LAST_ACTION;
799                 int i = 0;
800                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
801                         string const tmp = tabularFeature[i].feature;
802                         if (tmp == cmd.argument.substr(0, tmp.length())) {
803                                 action = tabularFeature[i].action;
804                                 break;
805                         }
806                 }
807                 if (action == LyXTabular::LAST_ACTION) {
808                         status.clear();
809                         status.unknown(true);
810                         return true;
811                 }
812
813                 string const argument
814                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
815
816                 row_type sel_row_start = 0;
817                 row_type sel_row_end = 0;
818                 col_type dummy;
819                 LyXTabular::ltType dummyltt;
820                 bool flag = true;
821
822                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
823
824                 switch (action) {
825                 case LyXTabular::SET_PWIDTH:
826                 case LyXTabular::SET_MPWIDTH:
827                 case LyXTabular::SET_SPECIAL_COLUMN:
828                 case LyXTabular::SET_SPECIAL_MULTI:
829                 case LyXTabular::APPEND_ROW:
830                 case LyXTabular::APPEND_COLUMN:
831                 case LyXTabular::DELETE_ROW:
832                 case LyXTabular::DELETE_COLUMN:
833                 case LyXTabular::SET_ALL_LINES:
834                 case LyXTabular::UNSET_ALL_LINES:
835                         status.clear();
836                         return true;
837
838                 case LyXTabular::MULTICOLUMN:
839                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
840                         break;
841
842                 case LyXTabular::M_TOGGLE_LINE_TOP:
843                         flag = false;
844                 case LyXTabular::TOGGLE_LINE_TOP:
845                         status.setOnOff(tabular.topLine(cur.idx(), flag));
846                         break;
847
848                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
849                         flag = false;
850                 case LyXTabular::TOGGLE_LINE_BOTTOM:
851                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
852                         break;
853
854                 case LyXTabular::M_TOGGLE_LINE_LEFT:
855                         flag = false;
856                 case LyXTabular::TOGGLE_LINE_LEFT:
857                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
858                         break;
859
860                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
861                         flag = false;
862                 case LyXTabular::TOGGLE_LINE_RIGHT:
863                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
864                         break;
865
866                 case LyXTabular::M_ALIGN_LEFT:
867                         flag = false;
868                 case LyXTabular::ALIGN_LEFT:
869                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
870                         break;
871
872                 case LyXTabular::M_ALIGN_RIGHT:
873                         flag = false;
874                 case LyXTabular::ALIGN_RIGHT:
875                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
876                         break;
877
878                 case LyXTabular::M_ALIGN_CENTER:
879                         flag = false;
880                 case LyXTabular::ALIGN_CENTER:
881                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
882                         break;
883
884                 case LyXTabular::ALIGN_BLOCK:
885                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
886                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
887                         break;
888
889                 case LyXTabular::M_VALIGN_TOP:
890                         flag = false;
891                 case LyXTabular::VALIGN_TOP:
892                         status.setOnOff(
893                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
894                         break;
895
896                 case LyXTabular::M_VALIGN_BOTTOM:
897                         flag = false;
898                 case LyXTabular::VALIGN_BOTTOM:
899                         status.setOnOff(
900                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
901                         break;
902
903                 case LyXTabular::M_VALIGN_MIDDLE:
904                         flag = false;
905                 case LyXTabular::VALIGN_MIDDLE:
906                         status.setOnOff(
907                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
908                         break;
909
910                 case LyXTabular::SET_LONGTABULAR:
911                         status.setOnOff(tabular.isLongTabular());
912                         break;
913
914                 case LyXTabular::UNSET_LONGTABULAR:
915                         status.setOnOff(!tabular.isLongTabular());
916                         break;
917
918                 case LyXTabular::SET_ROTATE_TABULAR:
919                         status.setOnOff(tabular.getRotateTabular());
920                         break;
921
922                 case LyXTabular::UNSET_ROTATE_TABULAR:
923                         status.setOnOff(!tabular.getRotateTabular());
924                         break;
925
926                 case LyXTabular::SET_ROTATE_CELL:
927                         status.setOnOff(tabular.getRotateCell(cur.idx()));
928                         break;
929
930                 case LyXTabular::UNSET_ROTATE_CELL:
931                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
932                         break;
933
934                 case LyXTabular::SET_USEBOX:
935                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
936                         break;
937
938                 case LyXTabular::SET_LTFIRSTHEAD:
939                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
940                         break;
941
942                 case LyXTabular::UNSET_LTFIRSTHEAD:
943                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
944                         break;
945
946                 case LyXTabular::SET_LTHEAD:
947                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
948                         break;
949
950                 case LyXTabular::UNSET_LTHEAD:
951                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
952                         break;
953
954                 case LyXTabular::SET_LTFOOT:
955                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
956                         break;
957
958                 case LyXTabular::UNSET_LTFOOT:
959                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
960                         break;
961
962                 case LyXTabular::SET_LTLASTFOOT:
963                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
964                         break;
965
966                 case LyXTabular::UNSET_LTLASTFOOT:
967                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
968                         break;
969
970                 case LyXTabular::SET_LTNEWPAGE:
971                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
972                         break;
973
974                 default:
975                         status.clear();
976                         status.enabled(false);
977                         break;
978                 }
979                 return true;
980         }
981
982         // These are only enabled inside tabular
983         case LFUN_CELL_BACKWARD:
984         case LFUN_CELL_FORWARD:
985                 status.enabled(true);
986                         return true;
987
988         // disable these with multiple cells selected
989         case LFUN_INSET_INSERT:
990         case LFUN_TABULAR_INSERT:
991         case LFUN_INSERT_CHARSTYLE:
992         case LFUN_INSET_FLOAT:
993         case LFUN_INSET_WIDE_FLOAT:
994         case LFUN_INSET_FOOTNOTE:
995         case LFUN_INSET_MARGINAL:
996         case LFUN_INSERT_MATH:
997         case LFUN_MATH_MODE:
998         case LFUN_MATH_MUTATE:
999         case LFUN_MATH_DISPLAY:
1000         case LFUN_INSERT_NOTE:
1001         case LFUN_INSET_OPTARG:
1002         case LFUN_INSERT_BOX:
1003         case LFUN_INSERT_BRANCH:
1004         case LFUN_INSET_WRAP:
1005         case LFUN_INSET_ERT: {
1006                 if (tablemode(cur)) {
1007                         status.enabled(false);
1008                         return true;
1009                 }
1010         }
1011
1012         case LFUN_INSET_MODIFY:
1013                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1014                         status.enabled(true);
1015                         return true;
1016                 }
1017                 // Fall through
1018
1019         default:
1020                 // we try to handle this event in the insets dispatch function.
1021                 return cell(cur.idx())->getStatus(cur, cmd, status);
1022         }
1023 }
1024
1025
1026 int InsetTabular::latex(Buffer const & buf, ostream & os,
1027                         OutputParams const & runparams) const
1028 {
1029         return tabular.latex(buf, os, runparams);
1030 }
1031
1032
1033 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1034                         OutputParams const & runparams) const
1035 {
1036         int const dp = runparams.linelen ? runparams.depth : 0;
1037         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1038 }
1039
1040
1041 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1042                            OutputParams const & runparams) const
1043 {
1044         return tabular.linuxdoc(buf,os, runparams);
1045 }
1046
1047
1048 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1049                           OutputParams const & runparams) const
1050 {
1051         int ret = 0;
1052         InsetBase * master = 0;
1053
1054 #ifdef WITH_WARNINGS
1055 #warning Why not pass a proper DocIterator here?
1056 #endif
1057 #if 0
1058         // if the table is inside a float it doesn't need the informaltable
1059         // wrapper. Search for it.
1060         for (master = owner(); master; master = master->owner())
1061                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1062                         break;
1063 #endif
1064
1065         if (!master) {
1066                 os << "<informaltable>";
1067                 ++ret;
1068         }
1069         ret += tabular.docbook(buf, os, runparams);
1070         if (!master) {
1071                 os << "</informaltable>";
1072                 ++ret;
1073         }
1074         return ret;
1075 }
1076
1077
1078 void InsetTabular::validate(LaTeXFeatures & features) const
1079 {
1080         tabular.validate(features);
1081 }
1082
1083
1084 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1085 {
1086         return tabular.getCellInset(idx);
1087 }
1088
1089
1090 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1091 {
1092         return tabular.getCellInset(idx);
1093 }
1094
1095
1096 void InsetTabular::getCursorPos(CursorSlice const & sl, int & x, int & y) const
1097 {
1098         cell(sl.idx())->getCursorPos(sl, x, y);
1099
1100         // y offset     correction
1101         int const row = tabular.row_of_cell(sl.idx());
1102         for (int i = 0; i <= row; ++i) {
1103                 if (i != 0) {
1104                         y += tabular.getAscentOfRow(i);
1105                         y += tabular.getAdditionalHeight(i);
1106                 }
1107                 if (i != row)
1108                         y += tabular.getDescentOfRow(i);
1109         }
1110
1111         // x offset correction
1112         int const col = tabular.column_of_cell(sl.idx());
1113         int idx = tabular.getCellNumber(row, 0);
1114         for (int j = 0; j < col; ++j) {
1115                 if (tabular.isPartOfMultiColumn(row, j))
1116                         continue;
1117                 x += tabular.getWidthOfColumn(idx);
1118                 ++idx;
1119         }
1120         x += tabular.getBeginningOfTextInCell(idx);
1121         x += ADD_TO_TABULAR_WIDTH;
1122         x += scroll();
1123 }
1124
1125
1126 int InsetTabular::dist(idx_type const cell, int x, int y) const
1127 {
1128         int xx = 0;
1129         int yy = 0;
1130         InsetBase const & inset = *tabular.getCellInset(cell);
1131         Point o = theCoords.getInsets().xy(&inset);
1132         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1133         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1134         int const ybeg = o.y_ - inset.ascent();
1135         row_type const row = tabular.row_of_cell(cell);
1136         int const rowheight = tabular.getAscentOfRow(row)
1137                         + tabular.getDescentOfRow(row)
1138                         + tabular.getAdditionalHeight(row);
1139         int const yend = ybeg + rowheight;
1140
1141         if (x < xbeg) {
1142                 xx = xbeg - x;
1143         } else if (x > xend) {
1144                 xx = x - xend;
1145         }
1146
1147         if (y < ybeg)  {
1148                 yy = ybeg - y;
1149         } else if (y > yend)  {
1150                 yy = y - yend;
1151         }
1152
1153         lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1154                << " ybeg=" << ybeg << " yend=" << yend
1155                << " xx=" << xx << " yy=" << yy
1156                << " dist=" << xx + yy << endl;
1157         return xx + yy;
1158 }
1159
1160
1161 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
1162 {
1163         //lyxerr << "InsetTabular::editXY: " << this << endl;
1164         cur.selection() = false;
1165         cur.push(const_cast<InsetTabular&>(*this));
1166         cur.idx() = getNearestCell(x, y);
1167         resetPos(cur);
1168         return cell(cur.idx())->text_.editXY(cur, x, y);
1169         //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1170 }
1171
1172
1173 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1174 {
1175         //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1176         cur.idx() = getNearestCell(x, y);
1177         resetPos(cur);
1178         return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1179 }
1180
1181
1182 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1183 {
1184         lyxerr << "# InsetTabular::getNearestCell()  x=" << x << " y=" << y << endl;
1185         idx_type idx_min = 0;
1186         int dist_min = std::numeric_limits<int>::max();
1187         for (idx_type i = 0; i < nargs(); ++i) {
1188                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1189                         int d = dist(i, x, y);
1190                         if (d < dist_min) {
1191                                 dist_min = d;
1192                                 idx_min = i;
1193                         }
1194                 }
1195         }
1196         return idx_min;
1197 }
1198
1199
1200 int InsetTabular::getCellXPos(idx_type const cell) const
1201 {
1202         idx_type c = cell;
1203
1204         for (; !tabular.isFirstCellInRow(c); --c)
1205                 ;
1206         int lx = tabular.getWidthOfColumn(cell);
1207         for (; c < cell; ++c)
1208                 lx += tabular.getWidthOfColumn(c);
1209
1210         return lx - tabular.getWidthOfColumn(cell);
1211 }
1212
1213
1214 void InsetTabular::resetPos(LCursor & cur) const
1215 {
1216         BufferView & bv = cur.bv();
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() < bv.workWidth()-20)
1226 //      {
1227 //              scroll(bv, 0.0F);
1228 //      } else if (cursorx_ - offset > 20 &&
1229 //                 cursorx_ - offset + col_width > bv.workWidth() - 20) {
1230 //              scroll(bv, - col_width - 20);
1231 //      } else if (cursorx_ - offset < 20) {
1232 //              scroll(bv, 20 - cursorx_ + offset);
1233 //      } else if (scroll() && xo() > 20 &&
1234 //                 xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1235 //              scroll(bv, old_x - cursorx_);
1236 //      }
1237
1238         if (&cur.inset() != this) {
1239                 scroll(bv, 0.0F);
1240         } else {
1241                 int const X1 = 0;
1242                 int const X2 = bv.workWidth();
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                         scroll(bv, X1 + 20 - x1);
1249                 else if (x2 > X2)
1250                         scroll(bv, 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         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1262         if (isRightToLeft(cur)) {
1263                 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1264                 if (tabular.isFirstCellInRow(cur.idx())) {
1265                         row_type const row = tabular.row_of_cell(cur.idx());
1266                         if (row == tabular.rows() - 1)
1267                                 return;
1268                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1269                 } else {
1270                         if (cur.idx() == 0)
1271                                 return;
1272                         --cur.idx();
1273                 }
1274         } else {
1275                 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1276                 if (tabular.isLastCell(cur.idx()))
1277                         return;
1278                 ++cur.idx();
1279         }
1280         cur.pit() = 0;
1281         cur.pos() = 0;
1282         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1283         resetPos(cur);
1284 }
1285
1286
1287 void InsetTabular::movePrevCell(LCursor & cur)
1288 {
1289         if (isRightToLeft(cur)) {
1290                 if (tabular.isLastCellInRow(cur.idx())) {
1291                         row_type const row = tabular.row_of_cell(cur.idx());
1292                         if (row == 0)
1293                                 return;
1294                         cur.idx() = tabular.getFirstCellInRow(row);
1295                         cur.idx() = tabular.getCellAbove(cur.idx());
1296                 } else {
1297                         if (tabular.isLastCell(cur.idx()))
1298                                 return;
1299                         ++cur.idx();
1300                 }
1301         } else {
1302                 if (cur.idx() == 0) // first cell
1303                         return;
1304                 --cur.idx();
1305         }
1306         cur.pit() = cur.lastpit();
1307         cur.pos() = cur.lastpos();
1308         resetPos(cur);
1309 }
1310
1311
1312 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1313 {
1314         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1315
1316         int i = 0;
1317         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1318                 string const tmp = tabularFeature[i].feature;
1319
1320                 if (tmp == what.substr(0, tmp.length())) {
1321                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1322                         //tabularFeatures[i].feature.length())) {
1323                         action = tabularFeature[i].action;
1324                         break;
1325                 }
1326         }
1327         if (action == LyXTabular::LAST_ACTION)
1328                 return false;
1329
1330         string const val =
1331                 ltrim(what.substr(tabularFeature[i].feature.length()));
1332         tabularFeatures(cur, action, val);
1333         return true;
1334 }
1335
1336
1337 namespace {
1338
1339 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1340                           string const & special, bool & flag)
1341 {
1342         if (special == "dl_above") {
1343                 ltt.topDL = flag;
1344                 ltt.set = false;
1345         } else if (special == "dl_below") {
1346                 ltt.bottomDL = flag;
1347                 ltt.set = false;
1348         } else if (special == "empty") {
1349                 ltt.empty = flag;
1350                 ltt.set = false;
1351         } else if (flag) {
1352                 ltt.empty = false;
1353                 ltt.set = true;
1354         }
1355 }
1356
1357 } // anon namespace
1358
1359
1360 void InsetTabular::tabularFeatures(LCursor & cur,
1361         LyXTabular::Feature feature, string const & value)
1362 {
1363         BufferView & bv = cur.bv();
1364         col_type sel_col_start;
1365         col_type sel_col_end;
1366         row_type sel_row_start;
1367         row_type sel_row_end;
1368         bool setLines = false;
1369         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1370         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1371
1372         switch (feature) {
1373
1374         case LyXTabular::M_ALIGN_LEFT:
1375         case LyXTabular::ALIGN_LEFT:
1376                 setAlign = LYX_ALIGN_LEFT;
1377                 break;
1378
1379         case LyXTabular::M_ALIGN_RIGHT:
1380         case LyXTabular::ALIGN_RIGHT:
1381                 setAlign = LYX_ALIGN_RIGHT;
1382                 break;
1383
1384         case LyXTabular::M_ALIGN_CENTER:
1385         case LyXTabular::ALIGN_CENTER:
1386                 setAlign = LYX_ALIGN_CENTER;
1387                 break;
1388
1389         case LyXTabular::ALIGN_BLOCK:
1390                 setAlign = LYX_ALIGN_BLOCK;
1391                 break;
1392
1393         case LyXTabular::M_VALIGN_TOP:
1394         case LyXTabular::VALIGN_TOP:
1395                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1396                 break;
1397
1398         case LyXTabular::M_VALIGN_BOTTOM:
1399         case LyXTabular::VALIGN_BOTTOM:
1400                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1401                 break;
1402
1403         case LyXTabular::M_VALIGN_MIDDLE:
1404         case LyXTabular::VALIGN_MIDDLE:
1405                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1406                 break;
1407
1408         default:
1409                 break;
1410         }
1411
1412         recordUndo(cur, Undo::ATOMIC);
1413
1414         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1415         row_type const row = tabular.row_of_cell(cur.idx());
1416         col_type const column = tabular.column_of_cell(cur.idx());
1417         bool flag = true;
1418         LyXTabular::ltType ltt;
1419
1420         switch (feature) {
1421
1422         case LyXTabular::SET_PWIDTH: {
1423                 LyXLength const len(value);
1424                 tabular.setColumnPWidth(cur.idx(), len);
1425                 if (len.zero()
1426                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1427                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1428                 else if (!len.zero()
1429                          && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1430                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1431                 break;
1432         }
1433
1434         case LyXTabular::SET_MPWIDTH:
1435                 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1436                 break;
1437
1438         case LyXTabular::SET_SPECIAL_COLUMN:
1439         case LyXTabular::SET_SPECIAL_MULTI:
1440                 tabular.setAlignSpecial(cur.idx(),value,feature);
1441                 break;
1442
1443         case LyXTabular::APPEND_ROW:
1444                 // append the row into the tabular
1445                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1446                 break;
1447
1448         case LyXTabular::APPEND_COLUMN:
1449                 // append the column into the tabular
1450                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1451                 cur.idx() = tabular.getCellNumber(row, column);
1452                 break;
1453
1454         case LyXTabular::DELETE_ROW:
1455                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1456                         tabular.deleteRow(sel_row_start);
1457                 if (sel_row_start >= tabular.rows())
1458                         --sel_row_start;
1459                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1460                 cur.pit() = 0;
1461                 cur.pos() = 0;
1462                 cur.selection() = false;
1463                 break;
1464
1465         case LyXTabular::DELETE_COLUMN:
1466                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1467                         tabular.deleteColumn(sel_col_start);
1468                 if (sel_col_start >= tabular.columns())
1469                         --sel_col_start;
1470                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1471                 cur.pit() = 0;
1472                 cur.pos() = 0;
1473                 cur.selection() = false;
1474                 break;
1475
1476         case LyXTabular::M_TOGGLE_LINE_TOP:
1477                 flag = false;
1478         case LyXTabular::TOGGLE_LINE_TOP: {
1479                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1480                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482                                 tabular.setTopLine(
1483                                         tabular.getCellNumber(i, j),
1484                                         lineSet, flag);
1485                 break;
1486         }
1487
1488         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1489                 flag = false;
1490         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1491                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1492                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1493                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1494                                 tabular.setBottomLine(
1495                                         tabular.getCellNumber(i, j),
1496                                         lineSet,
1497                                         flag);
1498                 break;
1499         }
1500
1501         case LyXTabular::M_TOGGLE_LINE_LEFT:
1502                 flag = false;
1503         case LyXTabular::TOGGLE_LINE_LEFT: {
1504                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1505                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1506                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1507                                 tabular.setLeftLine(
1508                                         tabular.getCellNumber(i,j),
1509                                         lineSet,
1510                                         flag);
1511                 break;
1512         }
1513
1514         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1515                 flag = false;
1516         case LyXTabular::TOGGLE_LINE_RIGHT: {
1517                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1518                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1519                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1520                                 tabular.setRightLine(
1521                                         tabular.getCellNumber(i,j),
1522                                         lineSet,
1523                                         flag);
1524                 break;
1525         }
1526
1527         case LyXTabular::M_ALIGN_LEFT:
1528         case LyXTabular::M_ALIGN_RIGHT:
1529         case LyXTabular::M_ALIGN_CENTER:
1530                 flag = false;
1531         case LyXTabular::ALIGN_LEFT:
1532         case LyXTabular::ALIGN_RIGHT:
1533         case LyXTabular::ALIGN_CENTER:
1534         case LyXTabular::ALIGN_BLOCK:
1535                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1536                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1537                                 tabular.setAlignment(
1538                                         tabular.getCellNumber(i, j),
1539                                         setAlign,
1540                                         flag);
1541                 break;
1542
1543         case LyXTabular::M_VALIGN_TOP:
1544         case LyXTabular::M_VALIGN_BOTTOM:
1545         case LyXTabular::M_VALIGN_MIDDLE:
1546                 flag = false;
1547         case LyXTabular::VALIGN_TOP:
1548         case LyXTabular::VALIGN_BOTTOM:
1549         case LyXTabular::VALIGN_MIDDLE:
1550                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1551                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1552                                 tabular.setVAlignment(
1553                                         tabular.getCellNumber(i, j),
1554                                         setVAlign, flag);
1555                 break;
1556
1557         case LyXTabular::MULTICOLUMN: {
1558                 if (sel_row_start != sel_row_end) {
1559 #ifdef WITH_WARNINGS
1560 #warning Need I say it ? This is horrible.
1561 #endif
1562                         Alert::error(_("Error setting multicolumn"),
1563                                    _("You cannot set multicolumn vertically."));
1564                         return;
1565                 }
1566                 if (!cur.selection()) {
1567                         // just multicol for one single cell
1568                         // check whether we are completely in a multicol
1569                         if (tabular.isMultiColumn(cur.idx()))
1570                                 tabular.unsetMultiColumn(cur.idx());
1571                         else
1572                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1573                         break;
1574                 }
1575                 // we have a selection so this means we just add all this
1576                 // cells to form a multicolumn cell
1577                 idx_type const s_start = cur.selBegin().idx();
1578                 idx_type const s_end = cur.selEnd().idx();
1579                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1580                 cur.idx() = s_start;
1581                 cur.pit() = 0;
1582                 cur.pos() = 0;
1583                 cur.selection() = false;
1584                 break;
1585         }
1586
1587         case LyXTabular::SET_ALL_LINES:
1588                 setLines = true;
1589         case LyXTabular::UNSET_ALL_LINES:
1590                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1591                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1592                                 tabular.setAllLines(
1593                                         tabular.getCellNumber(i,j), setLines);
1594                 break;
1595
1596         case LyXTabular::SET_LONGTABULAR:
1597                 tabular.setLongTabular(true);
1598                 break;
1599
1600         case LyXTabular::UNSET_LONGTABULAR:
1601                 tabular.setLongTabular(false);
1602                 break;
1603
1604         case LyXTabular::SET_ROTATE_TABULAR:
1605                 tabular.setRotateTabular(true);
1606                 break;
1607
1608         case LyXTabular::UNSET_ROTATE_TABULAR:
1609                 tabular.setRotateTabular(false);
1610                 break;
1611
1612         case LyXTabular::SET_ROTATE_CELL:
1613                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1614                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1615                                 tabular.setRotateCell(
1616                                         tabular.getCellNumber(i, j), true);
1617                 break;
1618
1619         case LyXTabular::UNSET_ROTATE_CELL:
1620                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1621                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1622                                 tabular.setRotateCell(
1623                                         tabular.getCellNumber(i, j), false);
1624                 break;
1625
1626         case LyXTabular::SET_USEBOX: {
1627                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1628                 if (val == tabular.getUsebox(cur.idx()))
1629                         val = LyXTabular::BOX_NONE;
1630                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1631                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1632                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1633                 break;
1634         }
1635
1636         case LyXTabular::UNSET_LTFIRSTHEAD:
1637                 flag = false;
1638         case LyXTabular::SET_LTFIRSTHEAD:
1639                 tabular.getRowOfLTFirstHead(row, ltt);
1640                 checkLongtableSpecial(ltt, value, flag);
1641                 tabular.setLTHead(row, flag, ltt, true);
1642                 break;
1643
1644         case LyXTabular::UNSET_LTHEAD:
1645                 flag = false;
1646         case LyXTabular::SET_LTHEAD:
1647                 tabular.getRowOfLTHead(row, ltt);
1648                 checkLongtableSpecial(ltt, value, flag);
1649                 tabular.setLTHead(row, flag, ltt, false);
1650                 break;
1651
1652         case LyXTabular::UNSET_LTFOOT:
1653                 flag = false;
1654         case LyXTabular::SET_LTFOOT:
1655                 tabular.getRowOfLTFoot(row, ltt);
1656                 checkLongtableSpecial(ltt, value, flag);
1657                 tabular.setLTFoot(row, flag, ltt, false);
1658                 break;
1659
1660         case LyXTabular::UNSET_LTLASTFOOT:
1661                 flag = false;
1662         case LyXTabular::SET_LTLASTFOOT:
1663                 tabular.getRowOfLTLastFoot(row, ltt);
1664                 checkLongtableSpecial(ltt, value, flag);
1665                 tabular.setLTFoot(row, flag, ltt, true);
1666                 break;
1667
1668         case LyXTabular::SET_LTNEWPAGE:
1669                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1670                 break;
1671
1672         // dummy stuff just to avoid warnings
1673         case LyXTabular::LAST_ACTION:
1674                 break;
1675         }
1676
1677         InsetTabularMailer(*this).updateDialog(&bv);
1678 }
1679
1680
1681 bool InsetTabular::showInsetDialog(BufferView * bv) const
1682 {
1683         InsetTabularMailer(*this).showDialog(bv);
1684         return true;
1685 }
1686
1687
1688 void InsetTabular::openLayoutDialog(BufferView * bv) const
1689 {
1690         InsetTabularMailer(*this).showDialog(bv);
1691 }
1692
1693
1694 void InsetTabular::getLabelList(Buffer const & buffer,
1695                                 vector<string> & list) const
1696 {
1697         tabular.getLabelList(buffer, list);
1698 }
1699
1700
1701 bool InsetTabular::copySelection(LCursor & cur)
1702 {
1703         if (!cur.selection())
1704                 return false;
1705
1706         row_type rs, re;
1707         col_type cs, ce;
1708         getSelection(cur, rs, re, cs, ce);
1709
1710         paste_tabular.reset(new LyXTabular(tabular));
1711
1712         for (row_type i = 0; i < rs; ++i)
1713                 paste_tabular->deleteRow(0);
1714
1715         row_type const rows = re - rs + 1;
1716         while (paste_tabular->rows() > rows)
1717                 paste_tabular->deleteRow(rows);
1718
1719         paste_tabular->setTopLine(0, true, true);
1720         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1721                                      true, true);
1722
1723         for (col_type i = 0; i < cs; ++i)
1724                 paste_tabular->deleteColumn(0);
1725
1726         col_type const columns = ce - cs + 1;
1727         while (paste_tabular->columns() > columns)
1728                 paste_tabular->deleteColumn(columns);
1729
1730         paste_tabular->setLeftLine(0, true, true);
1731         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1732                                     true, true);
1733
1734         ostringstream os;
1735         OutputParams const runparams;
1736         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1737         cur.bv().stuffClipboard(os.str());
1738         return true;
1739 }
1740
1741
1742 bool InsetTabular::pasteSelection(LCursor & cur)
1743 {
1744         if (!paste_tabular)
1745                 return false;
1746         col_type const actcol = tabular.column_of_cell(cur.idx());
1747         row_type const actrow = tabular.row_of_cell(cur.idx());
1748         for (row_type r1 = 0, r2 = actrow;
1749              r1 < paste_tabular->rows() && r2 < tabular.rows();
1750              ++r1, ++r2) {
1751                 for (col_type c1 = 0, c2 = actcol;
1752                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1753                     ++c1, ++c2) {
1754                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1755                             tabular.isPartOfMultiColumn(r2, c2))
1756                                 continue;
1757                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1758                                 --c2;
1759                                 continue;
1760                         }
1761                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1762                                 --c1;
1763                                 continue;
1764                         }
1765                         shared_ptr<InsetText> inset(
1766                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1767                         tabular.setCellInset(r2, c2, inset);
1768                         inset->markNew();
1769                         cur.pos() = 0;
1770                 }
1771         }
1772         return true;
1773 }
1774
1775
1776 void InsetTabular::cutSelection(LCursor & cur)
1777 {
1778         if (!cur.selection())
1779                 return;
1780
1781         bool const track = cur.buffer().params().tracking_changes;
1782         row_type rs, re;
1783         col_type cs, ce;
1784         getSelection(cur, rs, re, cs, ce);
1785         for (row_type i = rs; i <= re; ++i)
1786                 for (col_type j = cs; j <= ce; ++j)
1787                         cell(tabular.getCellNumber(i, j))->clear(track);
1788
1789         // cursor position might be invalid now
1790         cur.pos() = cur.lastpos();
1791         cur.clearSelection();
1792 }
1793
1794
1795 bool InsetTabular::isRightToLeft(LCursor & cur) const
1796 {
1797         BOOST_ASSERT(cur.depth() > 1);
1798         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1799         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1800         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1801                                          parentpos).language()->RightToLeft();
1802 }
1803
1804
1805 void InsetTabular::getSelection(LCursor & cur,
1806         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1807 {
1808         CursorSlice const & beg = cur.selBegin();
1809         CursorSlice const & end = cur.selEnd();
1810         cs = tabular.column_of_cell(beg.idx());
1811         ce = tabular.column_of_cell(end.idx());
1812         if (cs > ce) {
1813                 ce = cs;
1814                 cs = tabular.column_of_cell(end.idx());
1815         } else {
1816                 ce = tabular.right_column_of_cell(end.idx());
1817         }
1818
1819         rs = tabular.row_of_cell(beg.idx());
1820         re = tabular.row_of_cell(end.idx());
1821         if (rs > re)
1822                 swap(rs, re);
1823 }
1824
1825
1826 size_t InsetTabular::nargs() const
1827 {
1828         return tabular.getNumberOfCells();
1829 }
1830
1831
1832 LyXText * InsetTabular::getText(int idx) const
1833 {
1834         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1835 }
1836
1837
1838 void InsetTabular::markErased()
1839 {
1840         for (idx_type idx = 0; idx < nargs(); ++idx)
1841                 cell(idx)->markErased();
1842 }
1843
1844
1845 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1846 {
1847 #if 0
1848         idx_type const cell = tabular.getCellFromInset(in);
1849         // FIXME: getCellFromInset() returns now always a valid cell, so
1850         // the stuff below can be deleted, and instead we have:
1851         return tabular.getPWidth(cell).zero();
1852
1853         if (cell != npos)
1854                 return tabular.getPWidth(cell).zero();
1855
1856         // this is a workaround for a crash (New, Insert->Tabular,
1857         // Insert->FootNote)
1858         if (!owner())
1859                 return false;
1860
1861         // well we didn't obviously find it so maybe our owner knows more
1862         BOOST_ASSERT(owner());
1863         return owner()->forceDefaultParagraphs(in);
1864 #endif
1865         return false;
1866 }
1867
1868
1869 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1870                                      bool usePaste)
1871 {
1872         if (buf.length() <= 0)
1873                 return true;
1874
1875         col_type cols = 1;
1876         row_type rows = 1;
1877         col_type maxCols = 1;
1878         string::size_type const len = buf.length();
1879         string::size_type p = 0;
1880
1881         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1882                 switch (buf[p]) {
1883                 case '\t':
1884                         ++cols;
1885                         break;
1886                 case '\n':
1887                         if (p + 1 < len)
1888                                 ++rows;
1889                         maxCols = max(cols, maxCols);
1890                         cols = 1;
1891                         break;
1892                 }
1893                 ++p;
1894         }
1895         maxCols = max(cols, maxCols);
1896         LyXTabular * loctab;
1897         idx_type cell = 0;
1898         col_type ocol = 0;
1899         row_type row = 0;
1900         if (usePaste) {
1901                 paste_tabular.reset(
1902                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1903                 loctab = paste_tabular.get();
1904                 cols = 0;
1905         } else {
1906                 loctab = &tabular;
1907                 cell = bv.cursor().idx();
1908                 ocol = tabular.column_of_cell(cell);
1909                 row = tabular.row_of_cell(cell);
1910         }
1911
1912         string::size_type op = 0;
1913         idx_type const cells = loctab->getNumberOfCells();
1914         p = 0;
1915         cols = ocol;
1916         rows = loctab->rows();
1917         col_type const columns = loctab->columns();
1918
1919         while (cell < cells && p < len && row < rows &&
1920                (p = buf.find_first_of("\t\n", p)) != string::npos)
1921         {
1922                 if (p >= len)
1923                         break;
1924                 switch (buf[p]) {
1925                 case '\t':
1926                         // we can only set this if we are not too far right
1927                         if (cols < columns) {
1928                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1929                                 Paragraph & par = inset->text_.getPar(0);
1930                                 LyXFont const font = inset->text_.getFont(par, 0);
1931                                 inset->setText(buf.substr(op, p - op), font);
1932                                 ++cols;
1933                                 ++cell;
1934                         }
1935                         break;
1936                 case '\n':
1937                         // we can only set this if we are not too far right
1938                         if (cols < columns) {
1939                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1940                                 Paragraph & par = inset->text_.getPar(0);
1941                                 LyXFont const font = inset->text_.getFont(par, 0);
1942                                 inset->setText(buf.substr(op, p - op), font);
1943                         }
1944                         cols = ocol;
1945                         ++row;
1946                         if (row < rows)
1947                                 cell = loctab->getCellNumber(row, cols);
1948                         break;
1949                 }
1950                 ++p;
1951                 op = p;
1952         }
1953         // check for the last cell if there is no trailing '\n'
1954         if (cell < cells && op < len) {
1955                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1956                 Paragraph & par = inset->text_.getPar(0);
1957                 LyXFont const font = inset->text_.getFont(par, 0);
1958                 inset->setText(buf.substr(op, len - op), font);
1959         }
1960         return true;
1961 }
1962
1963
1964 void InsetTabular::addPreview(PreviewLoader & loader) const
1965 {
1966         row_type const rows = tabular.rows();
1967         col_type const columns = tabular.columns();
1968         for (row_type i = 0; i < rows; ++i) {
1969                 for (col_type j = 0; j < columns; ++j)
1970                         tabular.getCellInset(i, j)->addPreview(loader);
1971         }
1972 }
1973
1974
1975 bool InsetTabular::tablemode(LCursor & cur) const
1976 {
1977         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1978 }
1979
1980
1981
1982
1983
1984 string const InsetTabularMailer::name_("tabular");
1985
1986 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1987         : inset_(const_cast<InsetTabular &>(inset))
1988 {}
1989
1990
1991 string const InsetTabularMailer::inset2string(Buffer const &) const
1992 {
1993         return params2string(inset_);
1994 }
1995
1996
1997 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1998 {
1999         istringstream data(in);
2000         LyXLex lex(0,0);
2001         lex.setStream(data);
2002
2003         if (in.empty())
2004                 return;
2005
2006         string token;
2007         lex >> token;
2008         if (!lex || token != name_)
2009                 return print_mailer_error("InsetTabularMailer", in, 1,
2010                                           name_);
2011
2012         // This is part of the inset proper that is usually swallowed
2013         // by Buffer::readInset
2014         lex >> token;
2015         if (!lex || token != "Tabular")
2016                 return print_mailer_error("InsetTabularMailer", in, 2,
2017                                           "Tabular");
2018
2019         Buffer const & buffer = inset.buffer();
2020         inset.read(buffer, lex);
2021 }
2022
2023
2024 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2025 {
2026         ostringstream data;
2027         data << name_ << ' ';
2028         inset.write(inset.buffer(), data);
2029         data << "\\end_inset\n";
2030         return data.str();
2031 }