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