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