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