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