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