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