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