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