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