]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
* BufferView_pimpl.C:
[features.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 "debug.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "LColor.h"
24 #include "lyx_cb.h"
25 #include "lyxlex.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "undo_funcs.h"
31 #include "WordLangTuple.h"
32
33 #include "frontends/Alert.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Painter.h"
37
38 #include "support/std_sstream.h"
39 #include <iostream>
40
41 using lyx::graphics::PreviewLoader;
42
43 using lyx::support::ltrim;
44 using lyx::support::strToInt;
45
46 using std::endl;
47 using std::max;
48 using std::swap;
49 using std::string;
50 using std::auto_ptr;
51 using std::istringstream;
52 using std::ostream;
53 using std::ostringstream;
54
55
56 namespace {
57
58 int const ADD_TO_HEIGHT = 2;
59 int const ADD_TO_TABULAR_WIDTH = 2;
60
61 ///
62 LyXTabular * paste_tabular = 0;
63
64
65 struct TabularFeature {
66         LyXTabular::Feature action;
67         string feature;
68 };
69
70
71 TabularFeature tabularFeature[] =
72 {
73         { LyXTabular::APPEND_ROW, "append-row" },
74         { LyXTabular::APPEND_COLUMN, "append-column" },
75         { LyXTabular::DELETE_ROW, "delete-row" },
76         { LyXTabular::DELETE_COLUMN, "delete-column" },
77         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
78         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
79         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
80         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
81         { LyXTabular::ALIGN_LEFT, "align-left" },
82         { LyXTabular::ALIGN_RIGHT, "align-right" },
83         { LyXTabular::ALIGN_CENTER, "align-center" },
84         { LyXTabular::ALIGN_BLOCK, "align-block" },
85         { LyXTabular::VALIGN_TOP, "valign-top" },
86         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
87         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
88         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
89         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
90         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
91         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
92         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
93         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
94         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
95         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
96         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
97         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
98         { LyXTabular::MULTICOLUMN, "multicolumn" },
99         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
100         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
101         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
102         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
103         { LyXTabular::SET_PWIDTH, "set-pwidth" },
104         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
105         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
106         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
107         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
108         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
109         { LyXTabular::SET_USEBOX, "set-usebox" },
110         { LyXTabular::SET_LTHEAD, "set-lthead" },
111         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
112         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
113         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
114         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
115         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
116         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
117         { LyXTabular::LAST_ACTION, "" }
118 };
119
120 struct FindFeature {
121         FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
122         bool operator()(TabularFeature & tf)
123         {
124                 return tf.action == feature_;
125         }
126 private:
127         LyXTabular::Feature feature_;
128 };
129
130 } // namespace anon
131
132
133 string const featureAsString(LyXTabular::Feature feature)
134 {
135         TabularFeature * it  = tabularFeature;
136         TabularFeature * end = it +
137                 sizeof(tabularFeature) / sizeof(TabularFeature);
138         it = std::find_if(it, end, FindFeature(feature));
139         return (it == end) ? string() : it->feature;
140 }
141
142
143 bool InsetTabular::hasPasteBuffer() const
144 {
145         return (paste_tabular != 0);
146 }
147
148
149 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
150         : tabular(buf.params(), max(rows, 1), max(columns, 1)),
151           buffer_(&buf), cursorx_(0), cursory_(0)
152 {
153         tabular.setOwner(this);
154         // for now make it always display as display() inset
155         // just for test!!!
156         the_locking_inset = 0;
157         old_locking_inset = 0;
158         locked = false;
159         oldcell = -1;
160         actrow = actcell = 0;
161         clearSelection();
162         in_reset_pos = 0;
163         inset_x = 0;
164         inset_y = 0;
165 }
166
167
168 InsetTabular::InsetTabular(InsetTabular const & tab)
169         : UpdatableInset(tab), tabular(tab.tabular),
170                 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
171 {
172         tabular.setOwner(this);
173         the_locking_inset = 0;
174         old_locking_inset = 0;
175         locked = false;
176         oldcell = -1;
177         actrow = actcell = 0;
178         clearSelection();
179         in_reset_pos = 0;
180         inset_x = 0;
181         inset_y = 0;
182 }
183
184
185 InsetTabular::~InsetTabular()
186 {
187         InsetTabularMailer(*this).hideDialog();
188 }
189
190
191 auto_ptr<InsetBase> InsetTabular::clone() const
192 {
193         return auto_ptr<InsetBase>(new InsetTabular(*this));
194 }
195
196
197 Buffer const & InsetTabular::buffer() const
198 {
199         return *buffer_;
200 }
201
202
203 BufferView * InsetTabular::view() const
204 {
205         BOOST_ASSERT(false);
206         return 0;
207 }
208
209
210 void InsetTabular::buffer(Buffer * b)
211 {
212         buffer_ = b;
213 }
214
215
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
217 {
218         os << "Tabular" << endl;
219         tabular.write(buf, os);
220 }
221
222
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
224 {
225         bool const old_format = (lex.getString() == "\\LyXTable");
226
227         tabular.read(buf, lex);
228
229         if (old_format)
230                 return;
231
232         lex.nextToken();
233         string token = lex.getString();
234         while (lex.isOK() && (token != "\\end_inset")) {
235                 lex.nextToken();
236                 token = lex.getString();
237         }
238         if (token != "\\end_inset") {
239                 lex.printError("Missing \\end_inset at this point. "
240                                "Read: `$$Token'");
241         }
242 }
243
244
245 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
246 {
247         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248         //      mi.base.textwidth << "\n";
249         if (!mi.base.bv) {
250                 lyxerr << "InsetTabular::metrics: need bv" << endl;
251                 BOOST_ASSERT(false);
252         }
253
254         calculate_dimensions_of_cells(mi);
255
256         dim.asc = tabular.getAscentOfRow(0);
257         dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
258         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
259         dim_ = dim;
260 }
261
262
263 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
264 {
265         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
266
267         BufferView * bv = pi.base.bv;
268
269 #if 0
270         UpdatableInset::draw(pi, x, y);
271 #else
272         if (!owner())
273                 x += scroll();
274 #endif
275
276         top_x = x;
277         top_baseline = y;
278         x += ADD_TO_TABULAR_WIDTH;
279
280         int cell = 0;
281         first_visible_cell = -1;
282         for (int i = 0; i < tabular.rows(); ++i) {
283                 int nx = x;
284                 cell = tabular.getCellNumber(i, 0);
285                 if (y + tabular.getDescentOfRow(i) <= 0 &&
286                           y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
287                 {
288                         y += tabular.getDescentOfRow(i) +
289                                         tabular.getAscentOfRow(i + 1) +
290                                         tabular.getAdditionalHeight(i + 1);
291                         continue;
292                 }
293                 for (int j = 0; j < tabular.columns(); ++j) {
294                         if (nx > bv->workWidth())
295                                 break;
296                         if (tabular.isPartOfMultiColumn(i, j))
297                                 continue;
298                         int cx = nx + tabular.getBeginningOfTextInCell(cell);
299                         if (first_visible_cell < 0)
300                                 first_visible_cell = cell;
301                         if (hasSelection()) {
302                                 drawCellSelection(pi.pain, nx, y, i, j, cell);
303                         }
304
305                         tabular.getCellInset(cell).draw(pi, cx, y);
306                         drawCellLines(pi.pain, nx, y, i, cell);
307                         nx += tabular.getWidthOfColumn(cell);
308                         ++cell;
309                 }
310
311 // Would be nice, but for some completely unfathomable reason,
312 // on a col resize to a new fixed width, even though the insettexts
313 // are resized, the cell isn't, but drawing all cells in a tall table
314 // has the desired effect somehow. Complete dark magic.
315 #if 0
316                 // avoiding drawing the rest of a long table is
317                 // a pretty big speedup
318                 if (y > bv->workHeight())
319                         break;
320 #endif
321
322                 y += tabular.getDescentOfRow(i) +
323                         tabular.getAscentOfRow(i + 1) +
324                         tabular.getAdditionalHeight(i + 1);
325         }
326 }
327
328
329 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
330                                  int row, int cell) const
331 {
332         int x2 = x + tabular.getWidthOfColumn(cell);
333         bool on_off;
334
335         if (!tabular.topAlreadyDrawn(cell)) {
336                 on_off = !tabular.topLine(cell);
337                 pain.line(x, y - tabular.getAscentOfRow(row),
338                           x2, y -  tabular.getAscentOfRow(row),
339                           on_off ? LColor::tabularonoffline : LColor::tabularline,
340                           on_off ? Painter::line_onoffdash : Painter::line_solid);
341         }
342         on_off = !tabular.bottomLine(cell);
343         pain.line(x, y + tabular.getDescentOfRow(row),
344                   x2, y + tabular.getDescentOfRow(row),
345                   on_off ? LColor::tabularonoffline : LColor::tabularline,
346                   on_off ? Painter::line_onoffdash : Painter::line_solid);
347         if (!tabular.leftAlreadyDrawn(cell)) {
348                 on_off = !tabular.leftLine(cell);
349                 pain.line(x, y -  tabular.getAscentOfRow(row),
350                           x, y +  tabular.getDescentOfRow(row),
351                           on_off ? LColor::tabularonoffline : LColor::tabularline,
352                           on_off ? Painter::line_onoffdash : Painter::line_solid);
353         }
354         on_off = !tabular.rightLine(cell);
355         pain.line(x2 - tabular.getAdditionalWidth(cell),
356                   y -  tabular.getAscentOfRow(row),
357                   x2 - tabular.getAdditionalWidth(cell),
358                   y +  tabular.getDescentOfRow(row),
359                   on_off ? LColor::tabularonoffline : LColor::tabularline,
360                   on_off ? Painter::line_onoffdash : Painter::line_solid);
361 }
362
363
364 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
365                                      int row, int column, int cell) const
366 {
367         BOOST_ASSERT(hasSelection());
368         int cs = tabular.column_of_cell(sel_cell_start);
369         int ce = tabular.column_of_cell(sel_cell_end);
370         if (cs > ce) {
371                 ce = cs;
372                 cs = tabular.column_of_cell(sel_cell_end);
373         } else {
374                 ce = tabular.right_column_of_cell(sel_cell_end);
375         }
376
377         int rs = tabular.row_of_cell(sel_cell_start);
378         int re = tabular.row_of_cell(sel_cell_end);
379         if (rs > re)
380                 swap(rs, re);
381
382         if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
383                 int w = tabular.getWidthOfColumn(cell);
384                 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
385                 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
386                                    w, h, LColor::selection);
387         }
388 }
389
390
391 string const InsetTabular::editMessage() const
392 {
393         return _("Opened table");
394 }
395
396
397 void InsetTabular::insetUnlock(BufferView * bv)
398 {
399         if (the_locking_inset) {
400                 the_locking_inset->insetUnlock(bv);
401                 updateLocal(bv);
402                 the_locking_inset = 0;
403         }
404         actcell = 0;
405         oldcell = -1;
406         locked = false;
407         if (scroll(false) || hasSelection()) {
408                 clearSelection();
409                 if (scroll(false))
410                         scroll(bv, 0.0F);
411                 updateLocal(bv);
412         }
413 }
414
415
416 void InsetTabular::updateLocal(BufferView * bv) const
417 {
418         bv->updateInset(this);
419         if (locked)
420                 resetPos(bv);
421 }
422
423
424 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
425 {
426         lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
427                               << inset << "): ";
428         if (!inset)
429                 return false;
430         oldcell = -1;
431         if (inset == &tabular.getCellInset(actcell)) {
432                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
433                 the_locking_inset = &tabular.getCellInset(actcell);
434                 resetPos(bv);
435                 return true;
436         } else if (!the_locking_inset) {
437                 int const n = tabular.getNumberOfCells();
438                 int const id = inset->id();
439                 for (int i = 0; i < n; ++i) {
440                         InsetText * in = &tabular.getCellInset(i);
441                         if (inset == in) {
442                                 actcell = i;
443                                 the_locking_inset = in;
444                                 locked = true;
445                                 resetPos(bv);
446                                 return true;
447                         }
448                         if (in->getInsetFromID(id)) {
449                                 actcell = i;
450                                 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
451                                 return the_locking_inset->lockInsetInInset(bv, inset);
452                         }
453                 }
454         } else if (the_locking_inset && (the_locking_inset == inset)) {
455                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
456                 resetPos(bv);
457         } else if (the_locking_inset) {
458                 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
459                 return the_locking_inset->lockInsetInInset(bv, inset);
460         }
461         lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
462         return false;
463 }
464
465
466 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
467                                       bool lr)
468 {
469         if (!the_locking_inset)
470                 return false;
471         if (the_locking_inset == inset) {
472                 the_locking_inset->insetUnlock(bv);
473 #ifdef WITH_WARNINGS
474 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
475 #endif
476 #if 0
477                 if (scroll(false))
478                         scroll(bv, 0.0F);
479 #endif
480                 updateLocal(bv);
481                 // this has to be here otherwise we don't redraw the cell!
482                 the_locking_inset = 0;
483                 return true;
484         }
485         if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
486                 if (inset->lyxCode() == TABULAR_CODE &&
487                     !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
488                         InsetTabularMailer(*this).updateDialog(bv);
489                         oldcell = actcell;
490                 }
491                 return true;
492         }
493         return false;
494 }
495
496
497 int InsetTabular::insetInInsetY() const
498 {
499         if (!the_locking_inset)
500                 return 0;
501         return inset_y + the_locking_inset->insetInInsetY();
502 }
503
504
505 UpdatableInset * InsetTabular::getLockingInset() const
506 {
507         return the_locking_inset ? the_locking_inset->getLockingInset() :
508                 const_cast<InsetTabular *>(this);
509 }
510
511
512 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
513 {
514         if (c == lyxCode())
515                 return this;
516         if (the_locking_inset)
517                 return the_locking_inset->getFirstLockingInsetOfType(c);
518         return 0;
519 }
520
521
522 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
523 {
524         if (the_locking_inset)
525                 return the_locking_inset->insertInset(bv, inset);
526         return false;
527 }
528
529
530 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
531 {
532         if (hasSelection() && cmd.button() == mouse_button::button3)
533                 return;
534
535         if (hasSelection()) {
536                 clearSelection();
537                 updateLocal(cmd.view());
538         }
539
540         int const ocell = actcell;
541         BufferView * bv = cmd.view();
542
543         if (!locked) {
544                 locked = true;
545                 the_locking_inset = 0;
546                 inset_x = 0;
547                 inset_y = 0;
548         }
549         setPos(bv, cmd.x, cmd.y);
550         clearSelection();
551
552         bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
553
554         if ((ocell == actcell) && the_locking_inset && inset_hit) {
555                 resetPos(bv);
556                 FuncRequest cmd1 = cmd;
557                 cmd1.x -= inset_x;
558                 cmd1.y -= inset_y;
559                 the_locking_inset->localDispatch(cmd1);
560                 return;
561         }
562
563         if (the_locking_inset) {
564                 the_locking_inset->insetUnlock(bv);
565                 updateLocal(bv);
566                 the_locking_inset = 0;
567         }
568
569         if (cmd.button() == mouse_button::button2) {
570                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
571                 return;
572         }
573
574         if (inset_hit && bv->theLockingInset()) {
575                 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
576                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
577                         return;
578                 }
579                 FuncRequest cmd1 = cmd;
580                 cmd1.x -= inset_x;
581                 cmd1.y -= inset_y;
582                 the_locking_inset->localDispatch(cmd1);
583         }
584 }
585
586
587 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
588 {
589         bool ret = false;
590         if (the_locking_inset) {
591                 FuncRequest cmd1 = cmd;
592                 cmd1.x -= inset_x;
593                 cmd1.y -= inset_y;
594                 ret = the_locking_inset->localDispatch(cmd1);
595         }
596         if (cmd.button() == mouse_button::button3 && !ret) {
597                 InsetTabularMailer(*this).showDialog(cmd.view());
598                 return true;
599         }
600         return ret;
601 }
602
603
604 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
605 {
606         if (the_locking_inset) {
607                 FuncRequest cmd1 = cmd;
608                 cmd1.x -= inset_x;
609                 cmd1.y -= inset_y;
610                 the_locking_inset->localDispatch(cmd1);
611                 return;
612         }
613
614         BufferView * bv = cmd.view();
615         int const old_cell = actcell;
616
617         setPos(bv, cmd.x, cmd.y);
618         if (!hasSelection()) {
619                 setSelection(actcell, actcell);
620                 updateLocal(bv);
621         } else if (old_cell != actcell) {
622                 setSelection(sel_cell_start, actcell);
623                 updateLocal(bv);
624         }
625 }
626
627
628 dispatch_result InsetTabular::localDispatch(FuncRequest const & cmd)
629 {
630         // We need to save the value of the_locking_inset as the call to
631         // the_locking_inset->localDispatch might unlock it.
632         old_locking_inset = the_locking_inset;
633         dispatch_result result = UpdatableInset::localDispatch(cmd);
634         BufferView * bv = cmd.view();
635
636         if (cmd.action == LFUN_INSET_EDIT) {
637
638                 lyxerr << "InsetTabular::edit: " << this << " args: '"
639                         << cmd.argument << "'  first cell: "
640                         << &tabular.cell_info[0][0].inset << endl;
641
642                 if (!bv->lockInset(this)) {
643                         lyxerr << "InsetTabular::Cannot lock inset" << endl;
644                         return DISPATCHED;
645                 }
646
647                 finishUndo();
648                 locked = true;
649                 the_locking_inset = 0;
650                 inset_x = 0;
651                 inset_y = 0;
652
653                 if (cmd.argument.size()) {
654                         if (cmd.argument == "left") {
655                                 if (isRightToLeft(bv))
656                                         actcell = tabular.getLastCellInRow(0);
657                                 else
658                                         actcell = 0;
659                         } else {
660                                 if (isRightToLeft(bv))
661                                         actcell = tabular.getFirstCellInRow(tabular.rows()-1);
662                                 else
663                                         actcell = tabular.getNumberOfCells() - 1;
664                         }
665                         clearSelection();
666                         resetPos(bv);
667                         bv->fitCursor();
668                 }
669
670                 else {
671                         setPos(bv, cmd.x, cmd.y);
672                         clearSelection();
673                         finishUndo();
674                         if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
675                                 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
676                         }
677                 }
678                 return DISPATCHED;
679         }
680
681         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
682                 resetPos(bv);
683                 return result;
684         }
685
686         if (cmd.action < 0 && cmd.argument.empty())
687                 return FINISHED;
688
689         bool hs = hasSelection();
690
691         result = DISPATCHED;
692         // this one have priority over the locked InsetText, if we're not already
693         // inside another tabular then that one get's priority!
694         if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
695                 switch (cmd.action) {
696                 case LFUN_MOUSE_PRESS:
697                         lfunMousePress(cmd);
698                         return DISPATCHED;
699
700                 case LFUN_MOUSE_MOTION:
701                         lfunMouseMotion(cmd);
702                         return DISPATCHED;
703
704                 case LFUN_MOUSE_RELEASE:
705                         return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
706
707                 case LFUN_CELL_BACKWARD:
708                 case LFUN_CELL_FORWARD:
709                         unlockInsetInInset(bv, the_locking_inset);
710                         if (cmd.action == LFUN_CELL_FORWARD)
711                                 moveNextCell(bv, old_locking_inset != 0);
712                         else
713                                 movePrevCell(bv, old_locking_inset != 0);
714                         clearSelection();
715                         if (hs)
716                                 updateLocal(bv);
717                         if (!the_locking_inset)
718                                 return DISPATCHED_NOUPDATE;
719                         return result;
720                 // this to avoid compiler warnings.
721                 default:
722                         break;
723                 }
724         }
725
726         kb_action action = cmd.action;
727         string    arg    = cmd.argument;
728         if (the_locking_inset) {
729                 result = the_locking_inset->localDispatch(cmd);
730                 if (result == DISPATCHED_NOUPDATE) {
731                         int sc = scroll();
732                         resetPos(bv);
733                         if (sc != scroll()) { // inset has been scrolled
734                                 updateLocal(bv);
735                         }
736                         return result;
737                 } else if (result == DISPATCHED) {
738                         updateLocal(bv);
739                         return result;
740                 } else if (result == FINISHED_UP) {
741                         action = LFUN_UP;
742                         // Make sure to reset status message after
743                         // exiting, e.g. math inset
744                         bv->owner()->clearMessage();
745                 } else if (result == FINISHED_DOWN) {
746                         action = LFUN_DOWN;
747                         bv->owner()->clearMessage();
748                 } else if (result == FINISHED_RIGHT) {
749                         action = LFUN_RIGHT;
750                         bv->owner()->clearMessage();
751                 } else if (result == FINISHED) {
752                         bv->owner()->clearMessage();
753                 }
754         }
755
756         result = DISPATCHED;
757         switch (action) {
758                 // --- Cursor Movements ----------------------------------
759         case LFUN_RIGHTSEL: {
760                 int const start = hasSelection() ? sel_cell_start : actcell;
761                 if (tabular.isLastCellInRow(actcell)) {
762                         setSelection(start, actcell);
763                         break;
764                 }
765
766                 int end = actcell;
767                 // if we are starting a selection, only select
768                 // the current cell at the beginning
769                 if (hasSelection()) {
770                         moveRight(bv, false);
771                         end = actcell;
772                 }
773                 setSelection(start, end);
774                 updateLocal(bv);
775                 break;
776         }
777         case LFUN_RIGHT:
778                 result = moveRight(bv);
779                 clearSelection();
780                 if (hs)
781                         updateLocal(bv);
782                 break;
783         case LFUN_LEFTSEL: {
784                 int const start = hasSelection() ? sel_cell_start : actcell;
785                 if (tabular.isFirstCellInRow(actcell)) {
786                         setSelection(start, actcell);
787                         break;
788                 }
789
790                 int end = actcell;
791                 // if we are starting a selection, only select
792                 // the current cell at the beginning
793                 if (hasSelection()) {
794                         moveLeft(bv, false);
795                         end = actcell;
796                 }
797                 setSelection(start, end);
798                 updateLocal(bv);
799                 break;
800         }
801         case LFUN_LEFT:
802                 result = moveLeft(bv);
803                 clearSelection();
804                 if (hs)
805                         updateLocal(bv);
806                 break;
807         case LFUN_DOWNSEL: {
808                 int const start = hasSelection() ? sel_cell_start : actcell;
809                 int const ocell = actcell;
810                 // if we are starting a selection, only select
811                 // the current cell at the beginning
812                 if (hasSelection()) {
813                         moveDown(bv, false);
814                         if (ocell == sel_cell_end ||
815                             tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
816                                 setSelection(start, tabular.getCellBelow(sel_cell_end));
817                         else
818                                 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
819                 } else {
820                         setSelection(start, start);
821                 }
822                 updateLocal(bv);
823         }
824         break;
825         case LFUN_DOWN:
826                 result = moveDown(bv, old_locking_inset != 0);
827                 clearSelection();
828                 if (hs)
829                         updateLocal(bv);
830                 break;
831         case LFUN_UPSEL: {
832                 int const start = hasSelection() ? sel_cell_start : actcell;
833                 int const ocell = actcell;
834                 // if we are starting a selection, only select
835                 // the current cell at the beginning
836                 if (hasSelection()) {
837                         moveUp(bv, false);
838                         if ((ocell == sel_cell_end) ||
839                             (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
840                                 setSelection(start, tabular.getCellAbove(sel_cell_end));
841                         else
842                                 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
843                 } else {
844                         setSelection(start, start);
845                 }
846                 updateLocal(bv);
847         }
848         break;
849         case LFUN_UP:
850                 result = moveUp(bv, old_locking_inset != 0);
851                 clearSelection();
852                 if (hs)
853                         updateLocal(bv);
854                 break;
855         case LFUN_NEXT: {
856                 if (hs)
857                         clearSelection();
858                 int column = actcol;
859                 unlockInsetInInset(bv, the_locking_inset);
860                 if (bv->top_y() + bv->painter().paperHeight() <
861                     top_baseline + tabular.getHeightOfTabular())
862                         {
863                                 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
864                                 actcell = tabular.getCellBelow(first_visible_cell) + column;
865                         } else {
866                                 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
867                         }
868                 resetPos(bv);
869                 updateLocal(bv);
870                 break;
871         }
872         case LFUN_PRIOR: {
873                 if (hs)
874                         clearSelection();
875                 int column = actcol;
876                 unlockInsetInInset(bv, the_locking_inset);
877                 if (top_baseline < 0) {
878                         bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
879                         if (top_baseline > 0)
880                                 actcell = column;
881                         else
882                                 actcell = tabular.getCellBelow(first_visible_cell) + column;
883                 } else {
884                         actcell = column;
885                 }
886                 resetPos(bv);
887                 updateLocal(bv);
888                 break;
889         }
890         // none of these make sense for insettabular,
891         // but we must catch them to prevent any
892         // selection from being confused
893         case LFUN_PRIORSEL:
894         case LFUN_NEXTSEL:
895         case LFUN_WORDLEFT:
896         case LFUN_WORDLEFTSEL:
897         case LFUN_WORDRIGHT:
898         case LFUN_WORDRIGHTSEL:
899         case LFUN_WORDSEL:
900         case LFUN_DOWN_PARAGRAPH:
901         case LFUN_DOWN_PARAGRAPHSEL:
902         case LFUN_UP_PARAGRAPH:
903         case LFUN_UP_PARAGRAPHSEL:
904         case LFUN_BACKSPACE:
905         case LFUN_HOME:
906         case LFUN_HOMESEL:
907         case LFUN_END:
908         case LFUN_ENDSEL:
909         case LFUN_BEGINNINGBUF:
910         case LFUN_BEGINNINGBUFSEL:
911         case LFUN_ENDBUF:
912         case LFUN_ENDBUFSEL:
913                 break;
914         case LFUN_LAYOUT_TABULAR: {
915                 InsetTabularMailer(*this).showDialog(bv);
916                 break;
917         }
918         case LFUN_INSET_DIALOG_UPDATE: {
919                 InsetTabularMailer(*this).updateDialog(bv);
920                 break;
921         }
922         case LFUN_TABULAR_FEATURE:
923                 if (!tabularFeatures(bv, arg))
924                         result = UNDISPATCHED;
925                 break;
926                 // insert file functions
927         case LFUN_FILE_INSERT_ASCII_PARA:
928         case LFUN_FILE_INSERT_ASCII:
929         {
930                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
931                 if (tmpstr.empty())
932                         break;
933                 if (insertAsciiString(bv, tmpstr, false))
934                         updateLocal(bv);
935                 else
936                         result = UNDISPATCHED;
937                 break;
938         }
939         // cut and paste functions
940         case LFUN_CUT:
941                 if (!copySelection(bv))
942                         break;
943                 // no break here!
944         case LFUN_DELETE:
945                 recordUndo(bv, Undo::DELETE);
946                 cutSelection(bv->buffer()->params());
947                 updateLocal(bv);
948                 break;
949         case LFUN_COPY:
950                 if (!hasSelection())
951                         break;
952                 finishUndo();
953                 copySelection(bv);
954                 break;
955         case LFUN_PASTESELECTION:
956         {
957                 string const clip(bv->getClipboard());
958                         if (clip.empty())
959                         break;
960 #if 0
961                 if (clip.find('\t') != string::npos) {
962                         int cols = 1;
963                         int rows = 1;
964                         int maxCols = 1;
965                         string::size_type len = clip.length();
966                         string::size_type p = 0;
967
968                         while (p < len &&
969                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
970                                 switch (clip[p]) {
971                                 case '\t':
972                                         ++cols;
973                                         break;
974                                 case '\n':
975                                         if ((p+1) < len)
976                                                 ++rows;
977                                         maxCols = max(cols, maxCols);
978                                         cols = 1;
979                                         break;
980                                 }
981                                 ++p;
982                         }
983                         maxCols = max(cols, maxCols);
984                         delete paste_tabular;
985                         paste_tabular = new LyXTabular(bv->buffer()->params(),
986                                                        this, rows, maxCols);
987                         string::size_type op = 0;
988                         int cell = 0;
989                         int cells = paste_tabular->getNumberOfCells();
990                         p = cols = 0;
991                         while ((cell < cells) && (p < len) &&
992                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
993                                 if (p >= len)
994                                         break;
995                                 switch (clip[p]) {
996                                 case '\t':
997                                         paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
998                                         ++cols;
999                                         ++cell;
1000                                         break;
1001                                 case '\n':
1002                                         paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1003                                         while (cols++ < maxCols)
1004                                                 ++cell;
1005                                         cols = 0;
1006                                         break;
1007                                 }
1008                                 ++p;
1009                                 op = p;
1010                         }
1011                         // check for the last cell if there is no trailing '\n'
1012                         if ((cell < cells) && (op < len))
1013                                 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1014                 } else
1015 #else
1016                 if (!insertAsciiString(bv, clip, true))
1017 #endif
1018                 {
1019                         // so that the clipboard is used and it goes on
1020                         // to default
1021                         // and executes LFUN_PASTESELECTION in insettext!
1022                         delete paste_tabular;
1023                         paste_tabular = 0;
1024                 }
1025         }
1026         case LFUN_PASTE:
1027                 if (hasPasteBuffer()) {
1028                         recordUndo(bv, Undo::INSERT);
1029                         pasteSelection(bv);
1030                         updateLocal(bv);
1031                         break;
1032                 }
1033                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1034         default:
1035                 // handle font changing stuff on selection before we lock the inset
1036                 // in the default part!
1037                 result = UNDISPATCHED;
1038                 if (hs) {
1039                         switch(action) {
1040                         case LFUN_LANGUAGE:
1041                         case LFUN_EMPH:
1042                         case LFUN_BOLD:
1043                         case LFUN_NOUN:
1044                         case LFUN_CODE:
1045                         case LFUN_SANS:
1046                         case LFUN_ROMAN:
1047                         case LFUN_DEFAULT:
1048                         case LFUN_UNDERLINE:
1049                         case LFUN_FONT_SIZE:
1050                                 if (bv->dispatch(FuncRequest(bv, action, arg)))
1051                                         result = DISPATCHED;
1052                                 break;
1053                         default:
1054                                 break;
1055                         }
1056                 }
1057                 // we try to activate the actual inset and put this event down to
1058                 // the insets dispatch function.
1059                 if (result == DISPATCHED || the_locking_inset)
1060                         break;
1061                 if (activateCellInset(bv)) {
1062                         result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1063                         if (result == UNDISPATCHED || result >= FINISHED) {
1064                                 unlockInsetInInset(bv, the_locking_inset);
1065                                 // we need to update if this was requested before
1066                                 updateLocal(bv);
1067                                 return UNDISPATCHED;
1068                         }
1069                         if (hs)
1070                                 clearSelection();
1071                         updateLocal(bv);
1072                         return result;
1073                 }
1074                 break;
1075         }
1076         if (result < FINISHED) {
1077                 if (!the_locking_inset && bv->fitCursor())
1078                         updateLocal(bv);
1079         } else
1080                 bv->unlockInset(this);
1081         return result;
1082 }
1083
1084
1085 int InsetTabular::latex(Buffer const & buf, ostream & os,
1086                         LatexRunParams const & runparams) const
1087 {
1088         return tabular.latex(buf, os, runparams);
1089 }
1090
1091
1092 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1093 {
1094         if (ll > 0)
1095                 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1096                                       false, 0);
1097         return tabular.ascii(buf, os, 0, false, 0);
1098 }
1099
1100
1101 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1102 {
1103         return tabular.linuxdoc(buf,os);
1104 }
1105
1106
1107 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1108 {
1109         int ret = 0;
1110         InsetOld * master;
1111
1112         // if the table is inside a float it doesn't need the informaltable
1113         // wrapper. Search for it.
1114         for (master = owner();
1115              master && master->lyxCode() != InsetOld::FLOAT_CODE;
1116              master = master->owner());
1117
1118         if (!master) {
1119                 os << "<informaltable>";
1120                 if (mixcont)
1121                         os << endl;
1122                 ++ret;
1123         }
1124         ret += tabular.docbook(buf, os, mixcont);
1125         if (!master) {
1126                 os << "</informaltable>";
1127                 if (mixcont)
1128                         os << endl;
1129                 ++ret;
1130         }
1131         return ret;
1132 }
1133
1134
1135 void InsetTabular::validate(LaTeXFeatures & features) const
1136 {
1137         tabular.validate(features);
1138 }
1139
1140
1141 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1142 {
1143 #if 1
1144         // if we have a locking_inset we should have to check only this cell for
1145         // change so I'll try this to have a boost, but who knows ;) (Jug?)
1146         // This is _really_ important (André)
1147         if (the_locking_inset == &tabular.getCellInset(actcell)) {
1148                 int maxAsc = 0;
1149                 int maxDesc = 0;
1150                 for (int j = 0; j < tabular.columns(); ++j) {
1151                         Dimension dim;
1152                         MetricsInfo m = mi;
1153                         m.base.textwidth =
1154                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1155                         tabular.getCellInset(actrow, j).metrics(m, dim);
1156                         maxAsc  = max(dim.asc, maxAsc);
1157                         maxDesc = max(dim.des, maxDesc);
1158                 }
1159                 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1160                 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1161                 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1162                 return;
1163         }
1164 #endif
1165
1166         int cell = -1;
1167         for (int i = 0; i < tabular.rows(); ++i) {
1168                 int maxAsc = 0;
1169                 int maxDesc = 0;
1170                 for (int j = 0; j < tabular.columns(); ++j) {
1171                         if (tabular.isPartOfMultiColumn(i, j))
1172                                 continue;
1173                         ++cell;
1174                         Dimension dim;
1175                         MetricsInfo m = mi;
1176                         m.base.textwidth =
1177                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1178                         tabular.getCellInset(cell).metrics(m, dim);
1179                         maxAsc  = max(maxAsc, dim.asc);
1180                         maxDesc = max(maxDesc, dim.des);
1181                         tabular.setWidthOfCell(cell, dim.wid);
1182                 }
1183                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1184                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1185         }
1186 }
1187
1188
1189 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1190 {
1191         if (the_locking_inset) {
1192                 the_locking_inset->getCursor(bv, x, y);
1193                 return;
1194         }
1195
1196         x = cursorx_;
1197         y = cursory_ + InsetTabular::y();
1198
1199         // Fun stuff
1200         int desc = tabular.getDescentOfRow(actrow);
1201         y += desc;
1202         int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1203         y -= ascdesc / 2;
1204         y += ADD_TO_HEIGHT * 2;
1205         y += TEXT_TO_INSET_OFFSET;
1206 }
1207
1208
1209 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1210 {
1211         if (the_locking_inset) {
1212                 the_locking_inset->getCursorPos(bv, x, y);
1213                 return;
1214         }
1215         x = cursorx_ - top_x;
1216         y = cursory_;
1217 }
1218
1219
1220 void InsetTabular::fitInsetCursor(BufferView * bv) const
1221 {
1222         if (the_locking_inset) {
1223                 the_locking_inset->fitInsetCursor(bv);
1224                 return;
1225         }
1226
1227         LyXFont font;
1228         int const asc = font_metrics::maxAscent(font);
1229         int const desc = font_metrics::maxDescent(font);
1230         resetPos(bv);
1231
1232         bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1233 }
1234
1235
1236 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1237 {
1238         cursory_ = 0;
1239         actcell = actrow = actcol = 0;
1240         int ly = tabular.getDescentOfRow(actrow);
1241
1242         // first search the right row
1243         while (ly < y && actrow + 1 < tabular.rows()) {
1244                 cursory_ += tabular.getDescentOfRow(actrow) +
1245                                  tabular.getAscentOfRow(actrow + 1) +
1246                                  tabular.getAdditionalHeight(actrow + 1);
1247                 ++actrow;
1248                 ly = cursory_ + tabular.getDescentOfRow(actrow);
1249         }
1250         actcell = tabular.getCellNumber(actrow, actcol);
1251
1252         // now search the right column
1253         int lx = tabular.getWidthOfColumn(actcell) -
1254                 tabular.getAdditionalWidth(actcell);
1255
1256         for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1257                 lx += tabular.getWidthOfColumn(actcell + 1)
1258                         + tabular.getAdditionalWidth(actcell);
1259
1260         cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1261         resetPos(bv);
1262 }
1263
1264
1265 int InsetTabular::getCellXPos(int cell) const
1266 {
1267         int c = cell;
1268
1269         for (; !tabular.isFirstCellInRow(c); --c)
1270                 ;
1271         int lx = tabular.getWidthOfColumn(cell);
1272         for (; c < cell; ++c)
1273                 lx += tabular.getWidthOfColumn(c);
1274
1275         return (lx - tabular.getWidthOfColumn(cell) + top_x);
1276 }
1277
1278
1279 void InsetTabular::resetPos(BufferView * bv) const
1280 {
1281 #ifdef WITH_WARNINGS
1282 #warning This should be fixed in the right manner (20011128 Jug)
1283 #endif
1284         // fast hack to fix infinite repaintings!
1285         if (in_reset_pos > 0)
1286                 return;
1287
1288         int cell = 0;
1289         actcol = tabular.column_of_cell(actcell);
1290         actrow = 0;
1291         cursory_ = 0;
1292         for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1293                 if (tabular.isLastCellInRow(cell)) {
1294                         cursory_ += tabular.getDescentOfRow(actrow) +
1295                                          tabular.getAscentOfRow(actrow + 1) +
1296                                          tabular.getAdditionalHeight(actrow + 1);
1297                         ++actrow;
1298                 }
1299         }
1300         if (!locked) {
1301                 if (the_locking_inset)
1302                         inset_y = cursory_;
1303                 return;
1304         }
1305         // we need this only from here on!!!
1306         ++in_reset_pos;
1307         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1308         int new_x = getCellXPos(actcell);
1309         int old_x = cursorx_;
1310         new_x += offset;
1311         cursorx_ = new_x;
1312 //    cursor.x(getCellXPos(actcell) + offset);
1313         if (actcol < tabular.columns() - 1 && scroll(false) &&
1314                 tabular.getWidthOfTabular() < bv->workWidth()-20)
1315         {
1316                 scroll(bv, 0.0F);
1317                 updateLocal(bv);
1318         } else if (the_locking_inset &&
1319                  tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1320         {
1321                 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1322                 if (xx > bv->workWidth()-20) {
1323                         scroll(bv, -(xx - bv->workWidth() + 60));
1324                         updateLocal(bv);
1325                 } else if (xx < 20) {
1326                         if (xx < 0)
1327                                 xx = -xx + 60;
1328                         else
1329                                 xx = 60;
1330                         scroll(bv, xx);
1331                         updateLocal(bv);
1332                 }
1333         } else if (cursorx_ - offset > 20 &&
1334                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1335                    > bv->workWidth() - 20) {
1336                 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1337                 updateLocal(bv);
1338         } else if (cursorx_ - offset < 20) {
1339                 scroll(bv, 20 - cursorx_ + offset);
1340                 updateLocal(bv);
1341         } else if (scroll() && top_x > 20 &&
1342                    (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1343                 scroll(bv, old_x - cursorx_);
1344                 updateLocal(bv);
1345         }
1346         if (the_locking_inset) {
1347                 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1348                 inset_y = cursory_;
1349         }
1350         if ((!the_locking_inset ||
1351              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1352             actcell != oldcell) {
1353                 InsetTabularMailer(*this).updateDialog(bv);
1354                 oldcell = actcell;
1355         }
1356         in_reset_pos = 0;
1357 }
1358
1359
1360 dispatch_result InsetTabular::moveRight(BufferView * bv, bool lock)
1361 {
1362         if (lock && !old_locking_inset) {
1363                 if (activateCellInset(bv))
1364                         return DISPATCHED;
1365         } else {
1366                 bool moved = isRightToLeft(bv)
1367                         ? movePrevCell(bv) : moveNextCell(bv);
1368                 if (!moved)
1369                         return FINISHED_RIGHT;
1370                 if (lock && activateCellInset(bv))
1371                         return DISPATCHED;
1372         }
1373         resetPos(bv);
1374         return DISPATCHED_NOUPDATE;
1375 }
1376
1377
1378 dispatch_result InsetTabular::moveLeft(BufferView * bv, bool lock)
1379 {
1380         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1381         if (!moved)
1382                 return FINISHED;
1383         if (lock) {       // behind the inset
1384                 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1385                         return DISPATCHED;
1386         }
1387         resetPos(bv);
1388         return DISPATCHED_NOUPDATE;
1389 }
1390
1391
1392 dispatch_result InsetTabular::moveUp(BufferView * bv, bool lock)
1393 {
1394         int const ocell = actcell;
1395         actcell = tabular.getCellAbove(actcell);
1396         if (actcell == ocell) // we moved out of the inset
1397                 return FINISHED_UP;
1398         resetPos(bv);
1399         if (lock) {
1400                 int x = 0;
1401                 int y = 0;
1402                 if (old_locking_inset) {
1403                         old_locking_inset->getCursorPos(bv, x, y);
1404                         x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1405                 }
1406                 if (activateCellInset(bv, x, 0))
1407                         return DISPATCHED;
1408         }
1409         return DISPATCHED_NOUPDATE;
1410 }
1411
1412
1413 dispatch_result InsetTabular::moveDown(BufferView * bv, bool lock)
1414 {
1415         int const ocell = actcell;
1416         actcell = tabular.getCellBelow(actcell);
1417         if (actcell == ocell) // we moved out of the inset
1418                 return FINISHED_DOWN;
1419         resetPos(bv);
1420         if (lock) {
1421                 int x = 0;
1422                 int y = 0;
1423                 if (old_locking_inset) {
1424                         old_locking_inset->getCursorPos(bv, x, y);
1425                         x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1426                 }
1427                 if (activateCellInset(bv, x, 0))
1428                         return DISPATCHED;
1429         }
1430         return DISPATCHED_NOUPDATE;
1431 }
1432
1433
1434 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1435 {
1436         if (isRightToLeft(bv)) {
1437                 if (tabular.isFirstCellInRow(actcell)) {
1438                         int row = tabular.row_of_cell(actcell);
1439                         if (row == tabular.rows() - 1)
1440                                 return false;
1441                         actcell = tabular.getLastCellInRow(row);
1442                         actcell = tabular.getCellBelow(actcell);
1443                 } else {
1444                         if (!actcell)
1445                                 return false;
1446                         --actcell;
1447                 }
1448         } else {
1449                 if (tabular.isLastCell(actcell))
1450                         return false;
1451                 ++actcell;
1452         }
1453         if (lock) {
1454                 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1455                         isRightToLeftPar(bv->buffer()->params());
1456                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1457         }
1458         resetPos(bv);
1459         return true;
1460 }
1461
1462
1463 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1464 {
1465         if (isRightToLeft(bv)) {
1466                 if (tabular.isLastCellInRow(actcell)) {
1467                         int row = tabular.row_of_cell(actcell);
1468                         if (row == 0)
1469                                 return false;
1470                         actcell = tabular.getFirstCellInRow(row);
1471                         actcell = tabular.getCellAbove(actcell);
1472                 } else {
1473                         if (tabular.isLastCell(actcell))
1474                                 return false;
1475                         ++actcell;
1476                 }
1477         } else {
1478                 if (!actcell) // first cell
1479                         return false;
1480                 --actcell;
1481         }
1482         if (lock) {
1483                 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1484                         isRightToLeftPar(bv->buffer()->params());
1485                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1486         }
1487         resetPos(bv);
1488         return true;
1489 }
1490
1491
1492 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1493                            bool selectall)
1494 {
1495         if (selectall) {
1496                 setSelection(0, tabular.getNumberOfCells() - 1);
1497         }
1498         if (hasSelection()) {
1499                 recordUndo(bv, Undo::ATOMIC);
1500                 bool const frozen = undo_frozen;
1501                 if (!frozen)
1502                         freezeUndo();
1503                 // apply the fontchange on the whole selection
1504                 int sel_row_start;
1505                 int sel_row_end;
1506                 int sel_col_start;
1507                 int sel_col_end;
1508                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1509                 for(int i = sel_row_start; i <= sel_row_end; ++i)
1510                         for(int j = sel_col_start; j <= sel_col_end; ++j)
1511                                 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1512
1513                 if (!frozen)
1514                         unFreezeUndo();
1515                 if (selectall)
1516                         clearSelection();
1517                 updateLocal(bv);
1518         }
1519         if (the_locking_inset)
1520                 the_locking_inset->setFont(bv, font, tall);
1521 }
1522
1523
1524 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1525 {
1526         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1527
1528         int i = 0;
1529         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1530                 string const tmp = tabularFeature[i].feature;
1531
1532                 if (tmp == what.substr(0, tmp.length())) {
1533                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1534                         //tabularFeatures[i].feature.length())) {
1535                         action = tabularFeature[i].action;
1536                         break;
1537                 }
1538         }
1539         if (action == LyXTabular::LAST_ACTION)
1540                 return false;
1541
1542         string const val =
1543                 ltrim(what.substr(tabularFeature[i].feature.length()));
1544         tabularFeatures(bv, action, val);
1545         return true;
1546 }
1547
1548 namespace {
1549
1550 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1551                           string const & special, bool & flag)
1552 {
1553         if (special == "dl_above") {
1554                 ltt.topDL = flag;
1555                 ltt.set = false;
1556         } else if (special == "dl_below") {
1557                 ltt.bottomDL = flag;
1558                 ltt.set = false;
1559         } else if (special == "empty") {
1560                 ltt.empty = flag;
1561                 ltt.set = false;
1562         } else if (flag) {
1563                 ltt.empty = false;
1564                 ltt.set = true;
1565         }
1566 }
1567
1568 }
1569
1570
1571 void InsetTabular::tabularFeatures(BufferView * bv,
1572                                    LyXTabular::Feature feature,
1573                                    string const & value)
1574 {
1575         int sel_col_start;
1576         int sel_col_end;
1577         int sel_row_start;
1578         int sel_row_end;
1579         bool setLines = false;
1580         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1581         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1582
1583         switch (feature) {
1584
1585         case LyXTabular::M_ALIGN_LEFT:
1586         case LyXTabular::ALIGN_LEFT:
1587                 setAlign = LYX_ALIGN_LEFT;
1588                 break;
1589
1590         case LyXTabular::M_ALIGN_RIGHT:
1591         case LyXTabular::ALIGN_RIGHT:
1592                 setAlign = LYX_ALIGN_RIGHT;
1593                 break;
1594
1595         case LyXTabular::M_ALIGN_CENTER:
1596         case LyXTabular::ALIGN_CENTER:
1597                 setAlign = LYX_ALIGN_CENTER;
1598                 break;
1599
1600         case LyXTabular::ALIGN_BLOCK:
1601                 setAlign = LYX_ALIGN_BLOCK;
1602                 break;
1603
1604         case LyXTabular::M_VALIGN_TOP:
1605         case LyXTabular::VALIGN_TOP:
1606                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1607                 break;
1608
1609         case LyXTabular::M_VALIGN_BOTTOM:
1610         case LyXTabular::VALIGN_BOTTOM:
1611                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1612                 break;
1613
1614         case LyXTabular::M_VALIGN_MIDDLE:
1615         case LyXTabular::VALIGN_MIDDLE:
1616                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1617                 break;
1618
1619         default:
1620                 break;
1621         }
1622
1623         if (hasSelection()) {
1624                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1625         } else {
1626                 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1627                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1628         }
1629         recordUndo(bv, Undo::ATOMIC);
1630
1631         int row =  tabular.row_of_cell(actcell);
1632         int column = tabular.column_of_cell(actcell);
1633         bool flag = true;
1634         LyXTabular::ltType ltt;
1635
1636         switch (feature) {
1637
1638         case LyXTabular::SET_PWIDTH:
1639         {
1640                 LyXLength const len(value);
1641                 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1642
1643                 tabular.setColumnPWidth(actcell, len);
1644                 if (oldlen != len) {
1645                         // We need this otherwise we won't resize
1646                         // the insettext of the active cell (if any)
1647                         // until later (see InsetText::do_resize)
1648                         unlockInsetInInset(bv, the_locking_inset);
1649                         bv->update();
1650                 }
1651
1652                 if (len.zero()
1653                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1654                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1655                 else if (!len.zero()
1656                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1657                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1658                 break;
1659         }
1660
1661         case LyXTabular::SET_MPWIDTH:
1662         {
1663                 LyXLength const len(value);
1664                 LyXLength const & oldlen = tabular.getPWidth(actcell);
1665                 tabular.setMColumnPWidth(actcell, len);
1666                 if (oldlen != len) {
1667                         // We need this otherwise we won't resize
1668                         // the insettext of the active cell (if any)
1669                         // until later (see InsetText::do_resize)
1670                         unlockInsetInInset(bv, the_locking_inset);
1671                         updateLocal(bv);
1672                 }
1673         }
1674         break;
1675         case LyXTabular::SET_SPECIAL_COLUMN:
1676         case LyXTabular::SET_SPECIAL_MULTI:
1677                 tabular.setAlignSpecial(actcell,value,feature);
1678                 updateLocal(bv);
1679                 break;
1680         case LyXTabular::APPEND_ROW:
1681                 // append the row into the tabular
1682                 unlockInsetInInset(bv, the_locking_inset);
1683                 tabular.appendRow(bv->buffer()->params(), actcell);
1684                 tabular.setOwner(this);
1685                 updateLocal(bv);
1686                 break;
1687         case LyXTabular::APPEND_COLUMN:
1688                 // append the column into the tabular
1689                 unlockInsetInInset(bv, the_locking_inset);
1690                 tabular.appendColumn(bv->buffer()->params(), actcell);
1691                 tabular.setOwner(this);
1692                 actcell = tabular.getCellNumber(row, column);
1693                 updateLocal(bv);
1694                 break;
1695         case LyXTabular::DELETE_ROW:
1696                 unlockInsetInInset(bv, the_locking_inset);
1697                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1698                         tabular.deleteRow(sel_row_start);
1699                 }
1700                 if (sel_row_start >= tabular.rows())
1701                         --sel_row_start;
1702                 actcell = tabular.getCellNumber(sel_row_start, column);
1703                 clearSelection();
1704                 updateLocal(bv);
1705                 break;
1706         case LyXTabular::DELETE_COLUMN:
1707                 unlockInsetInInset(bv, the_locking_inset);
1708                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1709                         tabular.deleteColumn(sel_col_start);
1710                 }
1711                 if (sel_col_start >= tabular.columns())
1712                         --sel_col_start;
1713                 actcell = tabular.getCellNumber(row, sel_col_start);
1714                 clearSelection();
1715                 updateLocal(bv);
1716                 break;
1717         case LyXTabular::M_TOGGLE_LINE_TOP:
1718                 flag = false;
1719         case LyXTabular::TOGGLE_LINE_TOP:
1720         {
1721                 bool lineSet = !tabular.topLine(actcell, flag);
1722                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1723                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1724                                 tabular.setTopLine(
1725                                         tabular.getCellNumber(i, j),
1726                                         lineSet, flag);
1727                 updateLocal(bv);
1728                 break;
1729         }
1730
1731         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1732                 flag = false;
1733         case LyXTabular::TOGGLE_LINE_BOTTOM:
1734         {
1735                 bool lineSet = !tabular.bottomLine(actcell, flag);
1736                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1738                                 tabular.setBottomLine(
1739                                         tabular.getCellNumber(i, j),
1740                                         lineSet,
1741                                         flag);
1742                 updateLocal(bv);
1743                 break;
1744         }
1745
1746         case LyXTabular::M_TOGGLE_LINE_LEFT:
1747                 flag = false;
1748         case LyXTabular::TOGGLE_LINE_LEFT:
1749         {
1750                 bool lineSet = !tabular.leftLine(actcell, flag);
1751                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1752                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1753                                 tabular.setLeftLine(
1754                                         tabular.getCellNumber(i,j),
1755                                         lineSet,
1756                                         flag);
1757                 updateLocal(bv);
1758                 break;
1759         }
1760
1761         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1762                 flag = false;
1763         case LyXTabular::TOGGLE_LINE_RIGHT:
1764         {
1765                 bool lineSet = !tabular.rightLine(actcell, flag);
1766                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1767                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1768                                 tabular.setRightLine(
1769                                         tabular.getCellNumber(i,j),
1770                                         lineSet,
1771                                         flag);
1772                 updateLocal(bv);
1773                 break;
1774         }
1775
1776         case LyXTabular::M_ALIGN_LEFT:
1777         case LyXTabular::M_ALIGN_RIGHT:
1778         case LyXTabular::M_ALIGN_CENTER:
1779                 flag = false;
1780         case LyXTabular::ALIGN_LEFT:
1781         case LyXTabular::ALIGN_RIGHT:
1782         case LyXTabular::ALIGN_CENTER:
1783         case LyXTabular::ALIGN_BLOCK:
1784                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1785                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1786                                 tabular.setAlignment(
1787                                         tabular.getCellNumber(i, j),
1788                                         setAlign,
1789                                         flag);
1790                 updateLocal(bv);
1791                 break;
1792
1793         case LyXTabular::M_VALIGN_TOP:
1794         case LyXTabular::M_VALIGN_BOTTOM:
1795         case LyXTabular::M_VALIGN_MIDDLE:
1796                 flag = false;
1797         case LyXTabular::VALIGN_TOP:
1798         case LyXTabular::VALIGN_BOTTOM:
1799         case LyXTabular::VALIGN_MIDDLE:
1800                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1801                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1802                                 tabular.setVAlignment(
1803                                         tabular.getCellNumber(i, j),
1804                                         setVAlign, flag);
1805                 updateLocal(bv);
1806                 break;
1807
1808         case LyXTabular::MULTICOLUMN: {
1809                 if (sel_row_start != sel_row_end) {
1810 #ifdef WITH_WARNINGS
1811 #warning Need I say it ? This is horrible.
1812 #endif
1813                         Alert::error(_("Error setting multicolumn"),
1814                                    _("You cannot set multicolumn vertically."));
1815                         return;
1816                 }
1817                 // just multicol for one Single Cell
1818                 if (!hasSelection()) {
1819                         // check wether we are completly in a multicol
1820                         if (tabular.isMultiColumn(actcell))
1821                                 tabular.unsetMultiColumn(actcell);
1822                         else
1823                                 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1824                         updateLocal(bv);
1825                         break;
1826                 }
1827                 // we have a selection so this means we just add all this
1828                 // cells to form a multicolumn cell
1829                 int s_start;
1830                 int s_end;
1831
1832                 if (sel_cell_start > sel_cell_end) {
1833                         s_start = sel_cell_end;
1834                         s_end = sel_cell_start;
1835                 } else {
1836                         s_start = sel_cell_start;
1837                         s_end = sel_cell_end;
1838                 }
1839                 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1840                 actcell = s_start;
1841                 clearSelection();
1842                 updateLocal(bv);
1843                 break;
1844         }
1845
1846         case LyXTabular::SET_ALL_LINES:
1847                 setLines = true;
1848         case LyXTabular::UNSET_ALL_LINES:
1849                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1850                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1851                                 tabular.setAllLines(
1852                                         tabular.getCellNumber(i,j), setLines);
1853                 updateLocal(bv);
1854                 break;
1855
1856         case LyXTabular::SET_LONGTABULAR:
1857                 tabular.setLongTabular(true);
1858                 updateLocal(bv); // because this toggles displayed
1859                 break;
1860
1861         case LyXTabular::UNSET_LONGTABULAR:
1862                 tabular.setLongTabular(false);
1863                 updateLocal(bv); // because this toggles displayed
1864                 break;
1865
1866         case LyXTabular::SET_ROTATE_TABULAR:
1867                 tabular.setRotateTabular(true);
1868                 break;
1869
1870         case LyXTabular::UNSET_ROTATE_TABULAR:
1871                 tabular.setRotateTabular(false);
1872                 break;
1873
1874         case LyXTabular::SET_ROTATE_CELL:
1875                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1876                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1877                                 tabular.setRotateCell(
1878                                         tabular.getCellNumber(i, j), true);
1879                 break;
1880
1881         case LyXTabular::UNSET_ROTATE_CELL:
1882                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1883                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1884                                 tabular.setRotateCell(
1885                                         tabular.getCellNumber(i, j), false);
1886                 break;
1887
1888         case LyXTabular::SET_USEBOX: {
1889                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1890                 if (val == tabular.getUsebox(actcell))
1891                         val = LyXTabular::BOX_NONE;
1892                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1893                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1894                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1895                 break;
1896         }
1897
1898         case LyXTabular::UNSET_LTFIRSTHEAD:
1899                 flag = false;
1900         case LyXTabular::SET_LTFIRSTHEAD:
1901                 tabular.getRowOfLTFirstHead(row, ltt);
1902                 checkLongtableSpecial(ltt, value, flag);
1903                 tabular.setLTHead(row, flag, ltt, true);
1904                 break;
1905
1906         case LyXTabular::UNSET_LTHEAD:
1907                 flag = false;
1908         case LyXTabular::SET_LTHEAD:
1909                 tabular.getRowOfLTHead(row, ltt);
1910                 checkLongtableSpecial(ltt, value, flag);
1911                 tabular.setLTHead(row, flag, ltt, false);
1912                 break;
1913
1914         case LyXTabular::UNSET_LTFOOT:
1915                 flag = false;
1916         case LyXTabular::SET_LTFOOT:
1917                 tabular.getRowOfLTFoot(row, ltt);
1918                 checkLongtableSpecial(ltt, value, flag);
1919                 tabular.setLTFoot(row, flag, ltt, false);
1920                 break;
1921
1922         case LyXTabular::UNSET_LTLASTFOOT:
1923                 flag = false;
1924         case LyXTabular::SET_LTLASTFOOT:
1925                 tabular.getRowOfLTLastFoot(row, ltt);
1926                 checkLongtableSpecial(ltt, value, flag);
1927                 tabular.setLTFoot(row, flag, ltt, true);
1928                 break;
1929
1930         case LyXTabular::SET_LTNEWPAGE: {
1931                 bool what = !tabular.getLTNewPage(row);
1932                 tabular.setLTNewPage(row, what);
1933                 break;
1934         }
1935
1936         // dummy stuff just to avoid warnings
1937         case LyXTabular::LAST_ACTION:
1938                 break;
1939         }
1940
1941         InsetTabularMailer(*this).updateDialog(bv);
1942 }
1943
1944
1945 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1946         mouse_button::state button, bool behind)
1947 {
1948         UpdatableInset & inset = tabular.getCellInset(actcell);
1949         if (behind) {
1950 #warning metrics?
1951                 x = inset.x() + inset.width();
1952                 y = inset.descent();
1953         }
1954         //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1955         //inset_y = cursory_;
1956         inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x,  y, button));
1957         if (!the_locking_inset)
1958                 return false;
1959         updateLocal(bv);
1960         return the_locking_inset;
1961 }
1962
1963
1964 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1965                                         mouse_button::state button)
1966 {
1967         inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1968         inset_y = cursory_;
1969         return activateCellInset(bv, x - inset_x, y - inset_y, button);
1970 }
1971
1972
1973 bool InsetTabular::insetHit(BufferView *, int x, int) const
1974 {
1975         return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1976 }
1977
1978
1979 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1980 {
1981         //resizeLyXText(bv, recursive);
1982 }
1983
1984
1985 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1986                                    bool const recursive) const
1987 {
1988         if (the_locking_inset)
1989                 return the_locking_inset->getLyXText(bv, recursive);
1990         return InsetOld::getLyXText(bv, recursive);
1991 }
1992
1993
1994 bool InsetTabular::showInsetDialog(BufferView * bv) const
1995 {
1996         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1997                 InsetTabularMailer(*this).showDialog(bv);
1998         return true;
1999 }
2000
2001
2002 void InsetTabular::openLayoutDialog(BufferView * bv) const
2003 {
2004         if (the_locking_inset) {
2005                 InsetTabular * inset = static_cast<InsetTabular *>
2006                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2007                 if (inset) {
2008                         inset->openLayoutDialog(bv);
2009                         return;
2010                 }
2011         }
2012         InsetTabularMailer(*this).showDialog(bv);
2013 }
2014
2015
2016 //
2017 // function returns an object as defined in func_status.h:
2018 // states OK, Unknown, Disabled, On, Off.
2019 //
2020 FuncStatus InsetTabular::getStatus(string const & what) const
2021 {
2022         int action = LyXTabular::LAST_ACTION;
2023         FuncStatus status;
2024
2025         int i = 0;
2026         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2027                 string const tmp = tabularFeature[i].feature;
2028                 if (tmp == what.substr(0, tmp.length())) {
2029                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2030                         //   tabularFeatures[i].feature.length())) {
2031                         action = tabularFeature[i].action;
2032                         break;
2033                 }
2034         }
2035         if (action == LyXTabular::LAST_ACTION) {
2036                 status.clear();
2037                 return status.unknown(true);
2038         }
2039
2040         string const argument
2041                 = ltrim(what.substr(tabularFeature[i].feature.length()));
2042
2043         int sel_row_start;
2044         int sel_row_end;
2045         int dummy;
2046         LyXTabular::ltType dummyltt;
2047         bool flag = true;
2048
2049         if (hasSelection())
2050                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2051         else
2052                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2053
2054         switch (action) {
2055         case LyXTabular::SET_PWIDTH:
2056         case LyXTabular::SET_MPWIDTH:
2057         case LyXTabular::SET_SPECIAL_COLUMN:
2058         case LyXTabular::SET_SPECIAL_MULTI:
2059         case LyXTabular::APPEND_ROW:
2060         case LyXTabular::APPEND_COLUMN:
2061         case LyXTabular::DELETE_ROW:
2062         case LyXTabular::DELETE_COLUMN:
2063         case LyXTabular::SET_ALL_LINES:
2064         case LyXTabular::UNSET_ALL_LINES:
2065                 return status.clear();
2066
2067         case LyXTabular::MULTICOLUMN:
2068                 status.setOnOff(tabular.isMultiColumn(actcell));
2069                 break;
2070
2071         case LyXTabular::M_TOGGLE_LINE_TOP:
2072                 flag = false;
2073         case LyXTabular::TOGGLE_LINE_TOP:
2074                 status.setOnOff(tabular.topLine(actcell, flag));
2075                 break;
2076
2077         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2078                 flag = false;
2079         case LyXTabular::TOGGLE_LINE_BOTTOM:
2080                 status.setOnOff(tabular.bottomLine(actcell, flag));
2081                 break;
2082
2083         case LyXTabular::M_TOGGLE_LINE_LEFT:
2084                 flag = false;
2085         case LyXTabular::TOGGLE_LINE_LEFT:
2086                 status.setOnOff(tabular.leftLine(actcell, flag));
2087                 break;
2088
2089         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2090                 flag = false;
2091         case LyXTabular::TOGGLE_LINE_RIGHT:
2092                 status.setOnOff(tabular.rightLine(actcell, flag));
2093                 break;
2094
2095         case LyXTabular::M_ALIGN_LEFT:
2096                 flag = false;
2097         case LyXTabular::ALIGN_LEFT:
2098                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2099                 break;
2100
2101         case LyXTabular::M_ALIGN_RIGHT:
2102                 flag = false;
2103         case LyXTabular::ALIGN_RIGHT:
2104                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2105                 break;
2106
2107         case LyXTabular::M_ALIGN_CENTER:
2108                 flag = false;
2109         case LyXTabular::ALIGN_CENTER:
2110                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2111                 break;
2112
2113         case LyXTabular::ALIGN_BLOCK:
2114                 status.disabled(tabular.getPWidth(actcell).zero());
2115                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2116                 break;
2117
2118         case LyXTabular::M_VALIGN_TOP:
2119                 flag = false;
2120         case LyXTabular::VALIGN_TOP:
2121                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2122                 break;
2123
2124         case LyXTabular::M_VALIGN_BOTTOM:
2125                 flag = false;
2126         case LyXTabular::VALIGN_BOTTOM:
2127                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2128                 break;
2129
2130         case LyXTabular::M_VALIGN_MIDDLE:
2131                 flag = false;
2132         case LyXTabular::VALIGN_MIDDLE:
2133                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2134                 break;
2135
2136         case LyXTabular::SET_LONGTABULAR:
2137                 status.setOnOff(tabular.isLongTabular());
2138                 break;
2139
2140         case LyXTabular::UNSET_LONGTABULAR:
2141                 status.setOnOff(!tabular.isLongTabular());
2142                 break;
2143
2144         case LyXTabular::SET_ROTATE_TABULAR:
2145                 status.setOnOff(tabular.getRotateTabular());
2146                 break;
2147
2148         case LyXTabular::UNSET_ROTATE_TABULAR:
2149                 status.setOnOff(!tabular.getRotateTabular());
2150                 break;
2151
2152         case LyXTabular::SET_ROTATE_CELL:
2153                 status.setOnOff(tabular.getRotateCell(actcell));
2154                 break;
2155
2156         case LyXTabular::UNSET_ROTATE_CELL:
2157                 status.setOnOff(!tabular.getRotateCell(actcell));
2158                 break;
2159
2160         case LyXTabular::SET_USEBOX:
2161                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2162                 break;
2163
2164         case LyXTabular::SET_LTFIRSTHEAD:
2165                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2166                 break;
2167
2168         case LyXTabular::SET_LTHEAD:
2169                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2170                 break;
2171
2172         case LyXTabular::SET_LTFOOT:
2173                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2174                 break;
2175
2176         case LyXTabular::SET_LTLASTFOOT:
2177                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2178                 break;
2179
2180         case LyXTabular::SET_LTNEWPAGE:
2181                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2182                 break;
2183
2184         default:
2185                 status.clear();
2186                 status.disabled(true);
2187                 break;
2188         }
2189         return status;
2190 }
2191
2192
2193 void InsetTabular::getLabelList(Buffer const & buffer,
2194                                 std::vector<string> & list) const
2195 {
2196         tabular.getLabelList(buffer, list);
2197 }
2198
2199
2200 bool InsetTabular::copySelection(BufferView * bv)
2201 {
2202         if (!hasSelection())
2203                 return false;
2204
2205         int sel_col_start = tabular.column_of_cell(sel_cell_start);
2206         int sel_col_end = tabular.column_of_cell(sel_cell_end);
2207         if (sel_col_start > sel_col_end) {
2208                 sel_col_start = sel_col_end;
2209                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2210         } else {
2211                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2212         }
2213
2214         int sel_row_start = tabular.row_of_cell(sel_cell_start);
2215         int sel_row_end = tabular.row_of_cell(sel_cell_end);
2216         if (sel_row_start > sel_row_end)
2217                 swap(sel_row_start, sel_row_end);
2218
2219         delete paste_tabular;
2220         paste_tabular = new LyXTabular(tabular);
2221         paste_tabular->setOwner(this);
2222
2223         for (int i = 0; i < sel_row_start; ++i)
2224                 paste_tabular->deleteRow(0);
2225
2226         int const rows = sel_row_end - sel_row_start + 1;
2227         while (paste_tabular->rows() > rows)
2228                 paste_tabular->deleteRow(rows);
2229
2230         paste_tabular->setTopLine(0, true, true);
2231         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2232                                      true, true);
2233
2234         for (int i = 0; i < sel_col_start; ++i)
2235                 paste_tabular->deleteColumn(0);
2236
2237         int const columns = sel_col_end - sel_col_start + 1;
2238         while (paste_tabular->columns() > columns)
2239                 paste_tabular->deleteColumn(columns);
2240
2241         paste_tabular->setLeftLine(0, true, true);
2242         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2243                                     true, true);
2244
2245         ostringstream os;
2246         paste_tabular->ascii(*bv->buffer(), os,
2247                              ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2248         bv->stuffClipboard(os.str());
2249         return true;
2250 }
2251
2252
2253 bool InsetTabular::pasteSelection(BufferView * bv)
2254 {
2255         if (!paste_tabular)
2256                 return false;
2257
2258         for (int r1 = 0, r2 = actrow;
2259              r1 < paste_tabular->rows() && r2 < tabular.rows();
2260              ++r1, ++r2) {
2261                 for (int c1 = 0, c2 = actcol;
2262                     c1 < paste_tabular->columns() && c2 < tabular.columns();
2263                     ++c1, ++c2) {
2264                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2265                             tabular.isPartOfMultiColumn(r2, c2))
2266                                 continue;
2267                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2268                                 --c2;
2269                                 continue;
2270                         }
2271                         if (tabular.isPartOfMultiColumn(r2, c2)) {
2272                                 --c1;
2273                                 continue;
2274                         }
2275                         InsetText & inset = tabular.getCellInset(r2, c2);
2276                         inset = paste_tabular->getCellInset(r1, c1);
2277                         inset.setOwner(this);
2278                         inset.deleteLyXText(bv);
2279                         inset.markNew();
2280                 }
2281         }
2282         return true;
2283 }
2284
2285
2286 bool InsetTabular::cutSelection(BufferParams const & bp)
2287 {
2288         if (!hasSelection())
2289                 return false;
2290
2291         int sel_col_start = tabular.column_of_cell(sel_cell_start);
2292         int sel_col_end = tabular.column_of_cell(sel_cell_end);
2293         if (sel_col_start > sel_col_end) {
2294                 sel_col_start = sel_col_end;
2295                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2296         } else {
2297                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2298         }
2299
2300         int sel_row_start = tabular.row_of_cell(sel_cell_start);
2301         int sel_row_end = tabular.row_of_cell(sel_cell_end);
2302
2303         if (sel_row_start > sel_row_end)
2304                 swap(sel_row_start, sel_row_end);
2305
2306         if (sel_cell_start > sel_cell_end)
2307                 swap(sel_cell_start, sel_cell_end);
2308
2309         for (int i = sel_row_start; i <= sel_row_end; ++i)
2310                 for (int j = sel_col_start; j <= sel_col_end; ++j)
2311                         tabular.getCellInset(tabular.getCellNumber(i, j))
2312                                 .clear(bp.tracking_changes);
2313         return true;
2314 }
2315
2316
2317 bool InsetTabular::isRightToLeft(BufferView * bv)
2318 {
2319         return bv->getParentLanguage(this)->RightToLeft();
2320 }
2321
2322
2323 int InsetTabular::scroll(bool recursive) const
2324 {
2325         int sx = UpdatableInset::scroll(false);
2326
2327         if (recursive && the_locking_inset)
2328                 sx += the_locking_inset->scroll(recursive);
2329
2330         return sx;
2331 }
2332
2333
2334 void InsetTabular::getSelection(int & srow, int & erow,
2335                                 int & scol, int & ecol) const
2336 {
2337         int const start = hasSelection() ? sel_cell_start : actcell;
2338         int const end = hasSelection() ? sel_cell_end : actcell;
2339
2340         srow = tabular.row_of_cell(start);
2341         erow = tabular.row_of_cell(end);
2342         if (srow > erow)
2343                 swap(srow, erow);
2344
2345         scol = tabular.column_of_cell(start);
2346         ecol = tabular.column_of_cell(end);
2347         if (scol > ecol)
2348                 swap(scol, ecol);
2349         else
2350                 ecol = tabular.right_column_of_cell(end);
2351 }
2352
2353
2354 ParagraphList * InsetTabular::getParagraphs(int i) const
2355 {
2356         return (i < tabular.getNumberOfCells())
2357                 ? tabular.getCellInset(i).getParagraphs(0)
2358                 : 0;
2359 }
2360
2361
2362 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2363 {
2364         if (the_locking_inset)
2365                 return the_locking_inset->cursor(bv);
2366         return InsetOld::cursor(bv);
2367 }
2368
2369
2370 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2371 {
2372         if (id_arg == id())
2373                 return const_cast<InsetTabular *>(this);
2374
2375         for (int i = 0; i < tabular.rows(); ++i) {
2376                 for (int j = 0; j < tabular.columns(); ++j) {
2377                         InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2378                         if (inset)
2379                                 return inset;
2380                 }
2381         }
2382         return 0;
2383 }
2384
2385
2386 WordLangTuple const
2387 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2388 {
2389         if (the_locking_inset) {
2390                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2391                 if (!word.word().empty())
2392                         return word;
2393                 if (tabular.isLastCell(actcell)) {
2394                         bv->unlockInset(const_cast<InsetTabular *>(this));
2395                         return WordLangTuple();
2396                 }
2397                 ++actcell;
2398         }
2399         // otherwise we have to lock the next inset and ask for it's selecttion
2400         tabular.getCellInset(actcell)
2401                 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2402         WordLangTuple word(selectNextWordInt(bv, value));
2403         if (!word.word().empty())
2404                 resetPos(bv);
2405         return word;
2406 }
2407
2408
2409 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2410 {
2411         // when entering this function the inset should be ALWAYS locked!
2412         BOOST_ASSERT(the_locking_inset);
2413
2414         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2415         if (!word.word().empty())
2416                 return word;
2417
2418         if (tabular.isLastCell(actcell)) {
2419                 bv->unlockInset(const_cast<InsetTabular *>(this));
2420                 return WordLangTuple();
2421         }
2422
2423         // otherwise we have to lock the next inset and ask for it's selecttion
2424         ++actcell;
2425         tabular.getCellInset(actcell)
2426                 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2427         return selectNextWordInt(bv, value);
2428 }
2429
2430
2431 void InsetTabular::selectSelectedWord(BufferView * bv)
2432 {
2433         if (the_locking_inset)
2434                 the_locking_inset->selectSelectedWord(bv);
2435 }
2436
2437
2438 void InsetTabular::markErased()
2439 {
2440         for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2441                 tabular.getCellInset(cell).markErased();
2442 }
2443
2444
2445 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2446 {
2447         if (the_locking_inset) {
2448                 if (the_locking_inset->nextChange(bv, length)) {
2449                         updateLocal(bv);
2450                         return true;
2451                 }
2452                 if (tabular.isLastCell(actcell))
2453                         return false;
2454                 ++actcell;
2455         }
2456         InsetText & inset = tabular.getCellInset(actcell);
2457         if (inset.nextChange(bv, length)) {
2458                 updateLocal(bv);
2459                 return true;
2460         }
2461         while (!tabular.isLastCell(actcell)) {
2462                 ++actcell;
2463                 InsetText & inset = tabular.getCellInset(actcell);
2464                 if (inset.nextChange(bv, length)) {
2465                         updateLocal(bv);
2466                         return true;
2467                 }
2468         }
2469         return false;
2470 }
2471
2472
2473 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2474                                  bool cs, bool mw)
2475 {
2476         int cell = 0;
2477         if (the_locking_inset) {
2478                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2479                         updateLocal(bv);
2480                         return true;
2481                 }
2482                 if (tabular.isLastCell(actcell))
2483                         return false;
2484                 cell = actcell + 1;
2485         }
2486         InsetText & inset = tabular.getCellInset(cell);
2487         if (inset.searchForward(bv, str, cs, mw)) {
2488                 updateLocal(bv);
2489                 return true;
2490         }
2491         while (!tabular.isLastCell(cell)) {
2492                 ++cell;
2493                 InsetText & inset = tabular.getCellInset(cell);
2494                 if (inset.searchForward(bv, str, cs, mw)) {
2495                         updateLocal(bv);
2496                         return true;
2497                 }
2498         }
2499         return false;
2500 }
2501
2502
2503 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2504                                bool cs, bool mw)
2505 {
2506         int cell = tabular.getNumberOfCells();
2507         if (the_locking_inset) {
2508                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2509                         updateLocal(bv);
2510                         return true;
2511                 }
2512                 cell = actcell;
2513         }
2514
2515         while (cell) {
2516                 --cell;
2517                 InsetText & inset = tabular.getCellInset(cell);
2518                 if (inset.searchBackward(bv, str, cs, mw)) {
2519                         updateLocal(bv);
2520                         return true;
2521                 }
2522         }
2523         return false;
2524 }
2525
2526
2527 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2528 {
2529         if (the_locking_inset)
2530                 return the_locking_inset->insetAllowed(code);
2531         // we return true here because if the inset is not locked someone
2532         // wants to insert something in one of our insettexts and we generally
2533         // allow to do so.
2534         return true;
2535 }
2536
2537
2538 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2539 {
2540         const int cell = tabular.getCellFromInset(in);
2541
2542         if (cell != -1)
2543                 return tabular.getPWidth(cell).zero();
2544
2545         // this is a workaround for a crash (New, Insert->Tabular,
2546         // Insert->FootNote)
2547         if (!owner()) 
2548                 return false;
2549
2550         // well we didn't obviously find it so maybe our owner knows more
2551         BOOST_ASSERT(owner());
2552         return owner()->forceDefaultParagraphs(in);
2553 }
2554
2555
2556 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2557                                      bool usePaste)
2558 {
2559         if (buf.length() <= 0)
2560                 return true;
2561
2562         int cols = 1;
2563         int rows = 1;
2564         int maxCols = 1;
2565         string::size_type len = buf.length();
2566         string::size_type p = 0;
2567
2568         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2569                 switch (buf[p]) {
2570                 case '\t':
2571                         ++cols;
2572                         break;
2573                 case '\n':
2574                         if ((p+1) < len)
2575                                 ++rows;
2576                         maxCols = max(cols, maxCols);
2577                         cols = 1;
2578                         break;
2579                 }
2580                 ++p;
2581         }
2582         maxCols = max(cols, maxCols);
2583         LyXTabular * loctab;
2584         int cell = 0;
2585         int ocol = 0;
2586         int row = 0;
2587         if (usePaste) {
2588                 delete paste_tabular;
2589                 paste_tabular = new LyXTabular(bv->buffer()->params(),
2590                                                rows, maxCols);
2591                 paste_tabular->setOwner(this);
2592                 loctab = paste_tabular;
2593                 cols = 0;
2594         } else {
2595                 loctab = &tabular;
2596                 cell = actcell;
2597                 ocol = actcol;
2598                 row = actrow;
2599         }
2600
2601         string::size_type op = 0;
2602         int cells = loctab->getNumberOfCells();
2603         p = 0;
2604         cols = ocol;
2605         rows = loctab->rows();
2606         int const columns = loctab->columns();
2607
2608         while (cell < cells && p < len && row < rows &&
2609                (p = buf.find_first_of("\t\n", p)) != string::npos)
2610         {
2611                 if (p >= len)
2612                         break;
2613                 switch (buf[p]) {
2614                 case '\t':
2615                         // we can only set this if we are not too far right
2616                         if (cols < columns) {
2617                                 InsetText & inset = loctab->getCellInset(cell);
2618                                 LyXFont const font = inset.getLyXText(bv)->
2619                                         getFont(inset.paragraphs.begin(), 0);
2620                                 inset.setText(buf.substr(op, p - op), font);
2621                                 ++cols;
2622                                 ++cell;
2623                         }
2624                         break;
2625                 case '\n':
2626                         // we can only set this if we are not too far right
2627                         if (cols < columns) {
2628                                 InsetText & inset = tabular.getCellInset(cell);
2629                                 LyXFont const font = inset.getLyXText(bv)->
2630                                         getFont(inset.paragraphs.begin(), 0);
2631                                 inset.setText(buf.substr(op, p - op), font);
2632                         }
2633                         cols = ocol;
2634                         ++row;
2635                         if (row < rows)
2636                                 cell = loctab->getCellNumber(row, cols);
2637                         break;
2638                 }
2639                 ++p;
2640                 op = p;
2641         }
2642         // check for the last cell if there is no trailing '\n'
2643         if (cell < cells && op < len) {
2644                 InsetText & inset = loctab->getCellInset(cell);
2645                 LyXFont const font = inset.getLyXText(bv)->
2646                         getFont(inset.paragraphs.begin(), 0);
2647                 inset.setText(buf.substr(op, len - op), font);
2648         }
2649
2650         return true;
2651 }
2652
2653
2654 void InsetTabular::addPreview(PreviewLoader & loader) const
2655 {
2656         int const rows = tabular.rows();
2657         int const columns = tabular.columns();
2658         for (int i = 0; i < rows; ++i)
2659                 for (int j = 0; j < columns; ++j)
2660                         tabular.getCellInset(i, j).addPreview(loader);
2661 }
2662
2663
2664 string const InsetTabularMailer::name_("tabular");
2665
2666 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2667         : inset_(const_cast<InsetTabular &>(inset))
2668 {}
2669
2670
2671 string const InsetTabularMailer::inset2string(Buffer const &) const
2672 {
2673         return params2string(inset_);
2674 }
2675
2676
2677 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2678 {
2679         istringstream data(in);
2680         LyXLex lex(0,0);
2681         lex.setStream(data);
2682
2683 #warning CHECK verify that this is a sane value to return.
2684         if (in.empty())
2685                 return -1;
2686
2687         if (lex.isOK()) {
2688                 lex.next();
2689                 string const token = lex.getString();
2690                 if (token != name_)
2691                         return -1;
2692         }
2693
2694         int cell = -1;
2695         if (lex.isOK()) {
2696                 lex.next();
2697                 string const token = lex.getString();
2698                 if (token != "\\active_cell")
2699                         return -1;
2700                 lex.next();
2701                 cell = lex.getInteger();
2702         }
2703
2704         // This is part of the inset proper that is usually swallowed
2705         // by Buffer::readInset
2706         if (lex.isOK()) {
2707                 lex.next();
2708                 string const token = lex.getString();
2709                 if (token != "Tabular")
2710                         return -1;
2711         }
2712
2713         if (!lex.isOK())
2714                 return -1;
2715
2716         Buffer const & buffer = inset.buffer();
2717         inset.read(buffer, lex);
2718
2719         // We can't set the active cell, but we can tell the frontend
2720         // what it is.
2721         return cell;
2722 }
2723
2724
2725 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2726 {
2727         Buffer const & buffer = inset.buffer();
2728
2729         ostringstream data;
2730         data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2731         inset.write(buffer, data);
2732         data << "\\end_inset\n";
2733         return data.str();
2734 }