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