]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
some (yet unfinished) up/down work
[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         case LFUN_INSET_MODIFY:
1010                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1011                         status.enabled(true);
1012                         return true;
1013                 }
1014                 // Fall through
1015
1016         default:
1017                 // we try to handle this event in the insets dispatch function.
1018                 return cell(cur.idx())->getStatus(cur, cmd, status);
1019         }
1020 }
1021
1022
1023 int InsetTabular::latex(Buffer const & buf, ostream & os,
1024                         OutputParams const & runparams) const
1025 {
1026         return tabular.latex(buf, os, runparams);
1027 }
1028
1029
1030 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1031                         OutputParams const & runparams) const
1032 {
1033         int const dp = runparams.linelen ? runparams.depth : 0;
1034         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1035 }
1036
1037
1038 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1039                            OutputParams const & runparams) const
1040 {
1041         return tabular.linuxdoc(buf,os, runparams);
1042 }
1043
1044
1045 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1046                           OutputParams const & runparams) const
1047 {
1048         int ret = 0;
1049         InsetBase * master = 0;
1050
1051 #ifdef WITH_WARNINGS
1052 #warning Why not pass a proper DocIterator here?
1053 #endif
1054 #if 0
1055         // if the table is inside a float it doesn't need the informaltable
1056         // wrapper. Search for it.
1057         for (master = owner(); master; master = master->owner())
1058                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1059                         break;
1060 #endif
1061
1062         if (!master) {
1063                 os << "<informaltable>";
1064                 ++ret;
1065         }
1066         ret += tabular.docbook(buf, os, runparams);
1067         if (!master) {
1068                 os << "</informaltable>";
1069                 ++ret;
1070         }
1071         return ret;
1072 }
1073
1074
1075 void InsetTabular::validate(LaTeXFeatures & features) const
1076 {
1077         tabular.validate(features);
1078 }
1079
1080
1081 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1082 {
1083         return tabular.getCellInset(idx);
1084 }
1085
1086
1087 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1088 {
1089         return tabular.getCellInset(idx);
1090 }
1091
1092
1093 void InsetTabular::cursorPos
1094         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1095 {
1096         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1097
1098         // y offset     correction
1099         int const row = tabular.row_of_cell(sl.idx());
1100         for (int i = 0; i <= row; ++i) {
1101                 if (i != 0) {
1102                         y += tabular.getAscentOfRow(i);
1103                         y += tabular.getAdditionalHeight(i);
1104                 }
1105                 if (i != row)
1106                         y += tabular.getDescentOfRow(i);
1107         }
1108
1109         // x offset correction
1110         int const col = tabular.column_of_cell(sl.idx());
1111         int idx = tabular.getCellNumber(row, 0);
1112         for (int j = 0; j < col; ++j) {
1113                 if (tabular.isPartOfMultiColumn(row, j))
1114                         continue;
1115                 x += tabular.getWidthOfColumn(idx);
1116                 ++idx;
1117         }
1118         x += tabular.getBeginningOfTextInCell(idx);
1119         x += ADD_TO_TABULAR_WIDTH;
1120         x += scx_;
1121 }
1122
1123
1124 int InsetTabular::dist(idx_type const cell, int x, int y) const
1125 {
1126         int xx = 0;
1127         int yy = 0;
1128         InsetBase const & inset = *tabular.getCellInset(cell);
1129         Point o = theCoords.getInsets().xy(&inset);
1130         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1131         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1132         int const ybeg = o.y_ - inset.ascent();
1133         row_type const row = tabular.row_of_cell(cell);
1134         int const rowheight = tabular.getAscentOfRow(row)
1135                         + tabular.getDescentOfRow(row)
1136                         + tabular.getAdditionalHeight(row);
1137         int const yend = ybeg + rowheight;
1138
1139         if (x < xbeg)
1140                 xx = xbeg - x;
1141         else if (x > xend)
1142                 xx = x - xend;
1143
1144         if (y < ybeg) 
1145                 yy = ybeg - y;
1146         else if (y > yend) 
1147                 yy = y - yend;
1148
1149         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1150         //       << " ybeg=" << ybeg << " yend=" << yend
1151         //       << " xx=" << xx << " yy=" << yy
1152         //       << " dist=" << xx + yy << endl;
1153         return xx + yy;
1154 }
1155
1156
1157 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1158 {
1159         //lyxerr << "InsetTabular::editXY: " << this << endl;
1160         cur.selection() = false;
1161         cur.push(*this);
1162         cur.idx() = getNearestCell(x, y);
1163         resetPos(cur);
1164         return cell(cur.idx())->text_.editXY(cur, x, y);
1165 }
1166
1167
1168 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1169 {
1170         cur.idx() = getNearestCell(x, y);
1171         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1172 }
1173
1174
1175 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1176 {
1177         idx_type idx_min = 0;
1178         int dist_min = std::numeric_limits<int>::max();
1179         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1180                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1181                         int const d = dist(i, x, y);
1182                         if (d < dist_min) {
1183                                 dist_min = d;
1184                                 idx_min = i;
1185                         }
1186                 }
1187         }
1188         return idx_min;
1189 }
1190
1191
1192 int InsetTabular::getCellXPos(idx_type const cell) const
1193 {
1194         idx_type c = cell;
1195
1196         for (; !tabular.isFirstCellInRow(c); --c)
1197                 ;
1198         int lx = tabular.getWidthOfColumn(cell);
1199         for (; c < cell; ++c)
1200                 lx += tabular.getWidthOfColumn(c);
1201
1202         return lx - tabular.getWidthOfColumn(cell);
1203 }
1204
1205
1206 void InsetTabular::resetPos(LCursor & cur) const
1207 {
1208         BufferView & bv = cur.bv();
1209         int const maxwidth = bv.workWidth();
1210
1211         if (&cur.inset() != this) {
1212                 scx_ = 0;
1213         } else {
1214                 int const X1 = 0;
1215                 int const X2 = maxwidth;
1216                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1217                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1218                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1219
1220                 if (x1 < X1)
1221                         scx_ = X1 + 20 - x1;
1222                 else if (x2 > X2)
1223                         scx_ = X2 - 20 - x2;
1224                 else
1225                         scx_ = 0;
1226         }
1227
1228         cur.needsUpdate();
1229
1230         InsetTabularMailer(*this).updateDialog(&bv);
1231 }
1232
1233
1234 void InsetTabular::moveNextCell(LCursor & cur)
1235 {
1236         if (isRightToLeft(cur)) {
1237                 if (tabular.isFirstCellInRow(cur.idx())) {
1238                         row_type const row = tabular.row_of_cell(cur.idx());
1239                         if (row == tabular.rows() - 1)
1240                                 return;
1241                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1242                 } else {
1243                         if (cur.idx() == 0)
1244                                 return;
1245                         --cur.idx();
1246                 }
1247         } else {
1248                 if (tabular.isLastCell(cur.idx()))
1249                         return;
1250                 ++cur.idx();
1251         }
1252         cur.pit() = 0;
1253         cur.pos() = 0;
1254         resetPos(cur);
1255 }
1256
1257
1258 void InsetTabular::movePrevCell(LCursor & cur)
1259 {
1260         if (isRightToLeft(cur)) {
1261                 if (tabular.isLastCellInRow(cur.idx())) {
1262                         row_type const row = tabular.row_of_cell(cur.idx());
1263                         if (row == 0)
1264                                 return;
1265                         cur.idx() = tabular.getFirstCellInRow(row);
1266                         cur.idx() = tabular.getCellAbove(cur.idx());
1267                 } else {
1268                         if (tabular.isLastCell(cur.idx()))
1269                                 return;
1270                         ++cur.idx();
1271                 }
1272         } else {
1273                 if (cur.idx() == 0) // first cell
1274                         return;
1275                 --cur.idx();
1276         }
1277         cur.pit() = cur.lastpit();
1278         cur.pos() = cur.lastpos();
1279         resetPos(cur);
1280 }
1281
1282
1283 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1284 {
1285         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1286
1287         int i = 0;
1288         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1289                 string const tmp = tabularFeature[i].feature;
1290
1291                 if (tmp == what.substr(0, tmp.length())) {
1292                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1293                         //tabularFeatures[i].feature.length())) {
1294                         action = tabularFeature[i].action;
1295                         break;
1296                 }
1297         }
1298         if (action == LyXTabular::LAST_ACTION)
1299                 return false;
1300
1301         string const val =
1302                 ltrim(what.substr(tabularFeature[i].feature.length()));
1303         tabularFeatures(cur, action, val);
1304         return true;
1305 }
1306
1307
1308 namespace {
1309
1310 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1311                           string const & special, bool & flag)
1312 {
1313         if (special == "dl_above") {
1314                 ltt.topDL = flag;
1315                 ltt.set = false;
1316         } else if (special == "dl_below") {
1317                 ltt.bottomDL = flag;
1318                 ltt.set = false;
1319         } else if (special == "empty") {
1320                 ltt.empty = flag;
1321                 ltt.set = false;
1322         } else if (flag) {
1323                 ltt.empty = false;
1324                 ltt.set = true;
1325         }
1326 }
1327
1328 } // anon namespace
1329
1330
1331 void InsetTabular::tabularFeatures(LCursor & cur,
1332         LyXTabular::Feature feature, string const & value)
1333 {
1334         BufferView & bv = cur.bv();
1335         col_type sel_col_start;
1336         col_type sel_col_end;
1337         row_type sel_row_start;
1338         row_type sel_row_end;
1339         bool setLines = false;
1340         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1341         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1342
1343         switch (feature) {
1344
1345         case LyXTabular::M_ALIGN_LEFT:
1346         case LyXTabular::ALIGN_LEFT:
1347                 setAlign = LYX_ALIGN_LEFT;
1348                 break;
1349
1350         case LyXTabular::M_ALIGN_RIGHT:
1351         case LyXTabular::ALIGN_RIGHT:
1352                 setAlign = LYX_ALIGN_RIGHT;
1353                 break;
1354
1355         case LyXTabular::M_ALIGN_CENTER:
1356         case LyXTabular::ALIGN_CENTER:
1357                 setAlign = LYX_ALIGN_CENTER;
1358                 break;
1359
1360         case LyXTabular::ALIGN_BLOCK:
1361                 setAlign = LYX_ALIGN_BLOCK;
1362                 break;
1363
1364         case LyXTabular::M_VALIGN_TOP:
1365         case LyXTabular::VALIGN_TOP:
1366                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1367                 break;
1368
1369         case LyXTabular::M_VALIGN_BOTTOM:
1370         case LyXTabular::VALIGN_BOTTOM:
1371                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1372                 break;
1373
1374         case LyXTabular::M_VALIGN_MIDDLE:
1375         case LyXTabular::VALIGN_MIDDLE:
1376                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1377                 break;
1378
1379         default:
1380                 break;
1381         }
1382
1383         recordUndo(cur, Undo::ATOMIC);
1384
1385         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1386         row_type const row = tabular.row_of_cell(cur.idx());
1387         col_type const column = tabular.column_of_cell(cur.idx());
1388         bool flag = true;
1389         LyXTabular::ltType ltt;
1390
1391         switch (feature) {
1392
1393         case LyXTabular::SET_PWIDTH: {
1394                 LyXLength const len(value);
1395                 tabular.setColumnPWidth(cur.idx(), len);
1396                 if (len.zero()
1397                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1398                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1399                 else if (!len.zero()
1400                          && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1401                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1402                 break;
1403         }
1404
1405         case LyXTabular::SET_MPWIDTH:
1406                 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1407                 break;
1408
1409         case LyXTabular::SET_SPECIAL_COLUMN:
1410         case LyXTabular::SET_SPECIAL_MULTI:
1411                 tabular.setAlignSpecial(cur.idx(),value,feature);
1412                 break;
1413
1414         case LyXTabular::APPEND_ROW:
1415                 // append the row into the tabular
1416                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1417                 break;
1418
1419         case LyXTabular::APPEND_COLUMN:
1420                 // append the column into the tabular
1421                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1422                 cur.idx() = tabular.getCellNumber(row, column);
1423                 break;
1424
1425         case LyXTabular::DELETE_ROW:
1426                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1427                         tabular.deleteRow(sel_row_start);
1428                 if (sel_row_start >= tabular.rows())
1429                         --sel_row_start;
1430                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1431                 cur.pit() = 0;
1432                 cur.pos() = 0;
1433                 cur.selection() = false;
1434                 break;
1435
1436         case LyXTabular::DELETE_COLUMN:
1437                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1438                         tabular.deleteColumn(sel_col_start);
1439                 if (sel_col_start >= tabular.columns())
1440                         --sel_col_start;
1441                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1442                 cur.pit() = 0;
1443                 cur.pos() = 0;
1444                 cur.selection() = false;
1445                 break;
1446
1447         case LyXTabular::M_TOGGLE_LINE_TOP:
1448                 flag = false;
1449         case LyXTabular::TOGGLE_LINE_TOP: {
1450                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1451                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1452                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1453                                 tabular.setTopLine(
1454                                         tabular.getCellNumber(i, j),
1455                                         lineSet, flag);
1456                 break;
1457         }
1458
1459         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1460                 flag = false;
1461         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1462                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1463                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1464                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1465                                 tabular.setBottomLine(
1466                                         tabular.getCellNumber(i, j),
1467                                         lineSet,
1468                                         flag);
1469                 break;
1470         }
1471
1472         case LyXTabular::M_TOGGLE_LINE_LEFT:
1473                 flag = false;
1474         case LyXTabular::TOGGLE_LINE_LEFT: {
1475                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1476                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1477                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478                                 tabular.setLeftLine(
1479                                         tabular.getCellNumber(i,j),
1480                                         lineSet,
1481                                         flag);
1482                 break;
1483         }
1484
1485         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1486                 flag = false;
1487         case LyXTabular::TOGGLE_LINE_RIGHT: {
1488                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1489                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1490                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1491                                 tabular.setRightLine(
1492                                         tabular.getCellNumber(i,j),
1493                                         lineSet,
1494                                         flag);
1495                 break;
1496         }
1497
1498         case LyXTabular::M_ALIGN_LEFT:
1499         case LyXTabular::M_ALIGN_RIGHT:
1500         case LyXTabular::M_ALIGN_CENTER:
1501                 flag = false;
1502         case LyXTabular::ALIGN_LEFT:
1503         case LyXTabular::ALIGN_RIGHT:
1504         case LyXTabular::ALIGN_CENTER:
1505         case LyXTabular::ALIGN_BLOCK:
1506                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508                                 tabular.setAlignment(
1509                                         tabular.getCellNumber(i, j),
1510                                         setAlign,
1511                                         flag);
1512                 break;
1513
1514         case LyXTabular::M_VALIGN_TOP:
1515         case LyXTabular::M_VALIGN_BOTTOM:
1516         case LyXTabular::M_VALIGN_MIDDLE:
1517                 flag = false;
1518         case LyXTabular::VALIGN_TOP:
1519         case LyXTabular::VALIGN_BOTTOM:
1520         case LyXTabular::VALIGN_MIDDLE:
1521                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1522                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1523                                 tabular.setVAlignment(
1524                                         tabular.getCellNumber(i, j),
1525                                         setVAlign, flag);
1526                 break;
1527
1528         case LyXTabular::MULTICOLUMN: {
1529                 if (sel_row_start != sel_row_end) {
1530 #ifdef WITH_WARNINGS
1531 #warning Need I say it ? This is horrible.
1532 #endif
1533                         Alert::error(_("Error setting multicolumn"),
1534                                    _("You cannot set multicolumn vertically."));
1535                         return;
1536                 }
1537                 if (!cur.selection()) {
1538                         // just multicol for one single cell
1539                         // check whether we are completely in a multicol
1540                         if (tabular.isMultiColumn(cur.idx()))
1541                                 tabular.unsetMultiColumn(cur.idx());
1542                         else
1543                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1544                         break;
1545                 }
1546                 // we have a selection so this means we just add all this
1547                 // cells to form a multicolumn cell
1548                 idx_type const s_start = cur.selBegin().idx();
1549                 idx_type const s_end = cur.selEnd().idx();
1550                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1551                 cur.idx() = s_start;
1552                 cur.pit() = 0;
1553                 cur.pos() = 0;
1554                 cur.selection() = false;
1555                 break;
1556         }
1557
1558         case LyXTabular::SET_ALL_LINES:
1559                 setLines = true;
1560         case LyXTabular::UNSET_ALL_LINES:
1561                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1562                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1563                                 tabular.setAllLines(
1564                                         tabular.getCellNumber(i,j), setLines);
1565                 break;
1566
1567         case LyXTabular::SET_LONGTABULAR:
1568                 tabular.setLongTabular(true);
1569                 break;
1570
1571         case LyXTabular::UNSET_LONGTABULAR:
1572                 tabular.setLongTabular(false);
1573                 break;
1574
1575         case LyXTabular::SET_ROTATE_TABULAR:
1576                 tabular.setRotateTabular(true);
1577                 break;
1578
1579         case LyXTabular::UNSET_ROTATE_TABULAR:
1580                 tabular.setRotateTabular(false);
1581                 break;
1582
1583         case LyXTabular::SET_ROTATE_CELL:
1584                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1585                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1586                                 tabular.setRotateCell(
1587                                         tabular.getCellNumber(i, j), true);
1588                 break;
1589
1590         case LyXTabular::UNSET_ROTATE_CELL:
1591                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1592                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1593                                 tabular.setRotateCell(
1594                                         tabular.getCellNumber(i, j), false);
1595                 break;
1596
1597         case LyXTabular::SET_USEBOX: {
1598                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1599                 if (val == tabular.getUsebox(cur.idx()))
1600                         val = LyXTabular::BOX_NONE;
1601                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1602                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1603                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1604                 break;
1605         }
1606
1607         case LyXTabular::UNSET_LTFIRSTHEAD:
1608                 flag = false;
1609         case LyXTabular::SET_LTFIRSTHEAD:
1610                 tabular.getRowOfLTFirstHead(row, ltt);
1611                 checkLongtableSpecial(ltt, value, flag);
1612                 tabular.setLTHead(row, flag, ltt, true);
1613                 break;
1614
1615         case LyXTabular::UNSET_LTHEAD:
1616                 flag = false;
1617         case LyXTabular::SET_LTHEAD:
1618                 tabular.getRowOfLTHead(row, ltt);
1619                 checkLongtableSpecial(ltt, value, flag);
1620                 tabular.setLTHead(row, flag, ltt, false);
1621                 break;
1622
1623         case LyXTabular::UNSET_LTFOOT:
1624                 flag = false;
1625         case LyXTabular::SET_LTFOOT:
1626                 tabular.getRowOfLTFoot(row, ltt);
1627                 checkLongtableSpecial(ltt, value, flag);
1628                 tabular.setLTFoot(row, flag, ltt, false);
1629                 break;
1630
1631         case LyXTabular::UNSET_LTLASTFOOT:
1632                 flag = false;
1633         case LyXTabular::SET_LTLASTFOOT:
1634                 tabular.getRowOfLTLastFoot(row, ltt);
1635                 checkLongtableSpecial(ltt, value, flag);
1636                 tabular.setLTFoot(row, flag, ltt, true);
1637                 break;
1638
1639         case LyXTabular::SET_LTNEWPAGE:
1640                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1641                 break;
1642
1643         // dummy stuff just to avoid warnings
1644         case LyXTabular::LAST_ACTION:
1645                 break;
1646         }
1647
1648         InsetTabularMailer(*this).updateDialog(&bv);
1649 }
1650
1651
1652 bool InsetTabular::showInsetDialog(BufferView * bv) const
1653 {
1654         InsetTabularMailer(*this).showDialog(bv);
1655         return true;
1656 }
1657
1658
1659 void InsetTabular::openLayoutDialog(BufferView * bv) const
1660 {
1661         InsetTabularMailer(*this).showDialog(bv);
1662 }
1663
1664
1665 void InsetTabular::getLabelList(Buffer const & buffer,
1666                                 vector<string> & list) const
1667 {
1668         tabular.getLabelList(buffer, list);
1669 }
1670
1671
1672 bool InsetTabular::copySelection(LCursor & cur)
1673 {
1674         if (!cur.selection())
1675                 return false;
1676
1677         row_type rs, re;
1678         col_type cs, ce;
1679         getSelection(cur, rs, re, cs, ce);
1680
1681         paste_tabular.reset(new LyXTabular(tabular));
1682
1683         for (row_type i = 0; i < rs; ++i)
1684                 paste_tabular->deleteRow(0);
1685
1686         row_type const rows = re - rs + 1;
1687         while (paste_tabular->rows() > rows)
1688                 paste_tabular->deleteRow(rows);
1689
1690         paste_tabular->setTopLine(0, true, true);
1691         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1692                                      true, true);
1693
1694         for (col_type i = 0; i < cs; ++i)
1695                 paste_tabular->deleteColumn(0);
1696
1697         col_type const columns = ce - cs + 1;
1698         while (paste_tabular->columns() > columns)
1699                 paste_tabular->deleteColumn(columns);
1700
1701         paste_tabular->setLeftLine(0, true, true);
1702         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1703                                     true, true);
1704
1705         ostringstream os;
1706         OutputParams const runparams;
1707         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1708         cur.bv().stuffClipboard(os.str());
1709         return true;
1710 }
1711
1712
1713 bool InsetTabular::pasteSelection(LCursor & cur)
1714 {
1715         if (!paste_tabular)
1716                 return false;
1717         col_type const actcol = tabular.column_of_cell(cur.idx());
1718         row_type const actrow = tabular.row_of_cell(cur.idx());
1719         for (row_type r1 = 0, r2 = actrow;
1720              r1 < paste_tabular->rows() && r2 < tabular.rows();
1721              ++r1, ++r2) {
1722                 for (col_type c1 = 0, c2 = actcol;
1723                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1724                     ++c1, ++c2) {
1725                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1726                             tabular.isPartOfMultiColumn(r2, c2))
1727                                 continue;
1728                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1729                                 --c2;
1730                                 continue;
1731                         }
1732                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1733                                 --c1;
1734                                 continue;
1735                         }
1736                         shared_ptr<InsetText> inset(
1737                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1738                         tabular.setCellInset(r2, c2, inset);
1739                         inset->markNew();
1740                         cur.pos() = 0;
1741                 }
1742         }
1743         return true;
1744 }
1745
1746
1747 void InsetTabular::cutSelection(LCursor & cur)
1748 {
1749         if (!cur.selection())
1750                 return;
1751
1752         bool const track = cur.buffer().params().tracking_changes;
1753         row_type rs, re;
1754         col_type cs, ce;
1755         getSelection(cur, rs, re, cs, ce);
1756         for (row_type i = rs; i <= re; ++i)
1757                 for (col_type j = cs; j <= ce; ++j)
1758                         cell(tabular.getCellNumber(i, j))->clear(track);
1759
1760         // cursor position might be invalid now
1761         cur.pos() = cur.lastpos();
1762         cur.clearSelection();
1763 }
1764
1765
1766 bool InsetTabular::isRightToLeft(LCursor & cur) const
1767 {
1768         BOOST_ASSERT(cur.depth() > 1);
1769         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1770         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1771         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1772                                          parentpos).language()->RightToLeft();
1773 }
1774
1775
1776 void InsetTabular::getSelection(LCursor & cur,
1777         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1778 {
1779         CursorSlice const & beg = cur.selBegin();
1780         CursorSlice const & end = cur.selEnd();
1781         cs = tabular.column_of_cell(beg.idx());
1782         ce = tabular.column_of_cell(end.idx());
1783         if (cs > ce) {
1784                 ce = cs;
1785                 cs = tabular.column_of_cell(end.idx());
1786         } else {
1787                 ce = tabular.right_column_of_cell(end.idx());
1788         }
1789
1790         rs = tabular.row_of_cell(beg.idx());
1791         re = tabular.row_of_cell(end.idx());
1792         if (rs > re)
1793                 swap(rs, re);
1794 }
1795
1796
1797 LyXText * InsetTabular::getText(int idx) const
1798 {
1799         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1800 }
1801
1802
1803 void InsetTabular::markErased()
1804 {
1805         for (idx_type idx = 0; idx < nargs(); ++idx)
1806                 cell(idx)->markErased();
1807 }
1808
1809
1810 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1811 {
1812 #if 0
1813         idx_type const cell = tabular.getCellFromInset(in);
1814         // FIXME: getCellFromInset() returns now always a valid cell, so
1815         // the stuff below can be deleted, and instead we have:
1816         return tabular.getPWidth(cell).zero();
1817
1818         if (cell != npos)
1819                 return tabular.getPWidth(cell).zero();
1820
1821         // this is a workaround for a crash (New, Insert->Tabular,
1822         // Insert->FootNote)
1823         if (!owner())
1824                 return false;
1825
1826         // well we didn't obviously find it so maybe our owner knows more
1827         BOOST_ASSERT(owner());
1828         return owner()->forceDefaultParagraphs(in);
1829 #endif
1830         return false;
1831 }
1832
1833
1834 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1835                                      bool usePaste)
1836 {
1837         if (buf.length() <= 0)
1838                 return true;
1839
1840         col_type cols = 1;
1841         row_type rows = 1;
1842         col_type maxCols = 1;
1843         string::size_type const len = buf.length();
1844         string::size_type p = 0;
1845
1846         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1847                 switch (buf[p]) {
1848                 case '\t':
1849                         ++cols;
1850                         break;
1851                 case '\n':
1852                         if (p + 1 < len)
1853                                 ++rows;
1854                         maxCols = max(cols, maxCols);
1855                         cols = 1;
1856                         break;
1857                 }
1858                 ++p;
1859         }
1860         maxCols = max(cols, maxCols);
1861         LyXTabular * loctab;
1862         idx_type cell = 0;
1863         col_type ocol = 0;
1864         row_type row = 0;
1865         if (usePaste) {
1866                 paste_tabular.reset(
1867                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1868                 loctab = paste_tabular.get();
1869                 cols = 0;
1870         } else {
1871                 loctab = &tabular;
1872                 cell = bv.cursor().idx();
1873                 ocol = tabular.column_of_cell(cell);
1874                 row = tabular.row_of_cell(cell);
1875         }
1876
1877         string::size_type op = 0;
1878         idx_type const cells = loctab->getNumberOfCells();
1879         p = 0;
1880         cols = ocol;
1881         rows = loctab->rows();
1882         col_type const columns = loctab->columns();
1883
1884         while (cell < cells && p < len && row < rows &&
1885                (p = buf.find_first_of("\t\n", p)) != string::npos)
1886         {
1887                 if (p >= len)
1888                         break;
1889                 switch (buf[p]) {
1890                 case '\t':
1891                         // we can only set this if we are not too far right
1892                         if (cols < columns) {
1893                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1894                                 Paragraph & par = inset->text_.getPar(0);
1895                                 LyXFont const font = inset->text_.getFont(par, 0);
1896                                 inset->setText(buf.substr(op, p - op), font);
1897                                 ++cols;
1898                                 ++cell;
1899                         }
1900                         break;
1901                 case '\n':
1902                         // we can only set this if we are not too far right
1903                         if (cols < columns) {
1904                                 shared_ptr<InsetText> inset = tabular.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                         }
1909                         cols = ocol;
1910                         ++row;
1911                         if (row < rows)
1912                                 cell = loctab->getCellNumber(row, cols);
1913                         break;
1914                 }
1915                 ++p;
1916                 op = p;
1917         }
1918         // check for the last cell if there is no trailing '\n'
1919         if (cell < cells && op < len) {
1920                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1921                 Paragraph & par = inset->text_.getPar(0);
1922                 LyXFont const font = inset->text_.getFont(par, 0);
1923                 inset->setText(buf.substr(op, len - op), font);
1924         }
1925         return true;
1926 }
1927
1928
1929 void InsetTabular::addPreview(PreviewLoader & loader) const
1930 {
1931         row_type const rows = tabular.rows();
1932         col_type const columns = tabular.columns();
1933         for (row_type i = 0; i < rows; ++i) {
1934                 for (col_type j = 0; j < columns; ++j)
1935                         tabular.getCellInset(i, j)->addPreview(loader);
1936         }
1937 }
1938
1939
1940 bool InsetTabular::tablemode(LCursor & cur) const
1941 {
1942         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1943 }
1944
1945
1946
1947
1948
1949 string const InsetTabularMailer::name_("tabular");
1950
1951 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1952         : inset_(const_cast<InsetTabular &>(inset))
1953 {}
1954
1955
1956 string const InsetTabularMailer::inset2string(Buffer const &) const
1957 {
1958         return params2string(inset_);
1959 }
1960
1961
1962 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1963 {
1964         istringstream data(in);
1965         LyXLex lex(0,0);
1966         lex.setStream(data);
1967
1968         if (in.empty())
1969                 return;
1970
1971         string token;
1972         lex >> token;
1973         if (!lex || token != name_)
1974                 return print_mailer_error("InsetTabularMailer", in, 1,
1975                                           name_);
1976
1977         // This is part of the inset proper that is usually swallowed
1978         // by Buffer::readInset
1979         lex >> token;
1980         if (!lex || token != "Tabular")
1981                 return print_mailer_error("InsetTabularMailer", in, 2,
1982                                           "Tabular");
1983
1984         Buffer const & buffer = inset.buffer();
1985         inset.read(buffer, lex);
1986 }
1987
1988
1989 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1990 {
1991         ostringstream data;
1992         data << name_ << ' ';
1993         inset.write(inset.buffer(), data);
1994         data << "\\end_inset\n";
1995         return data.str();
1996 }