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