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