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