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