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