]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
tab funcs changed
[features.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
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->refreshStatus() == LyXText::REFRESH_AREA)
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_CELL_BACKWARD:
816                 case LFUN_CELL_FORWARD:
817                         hideInsetCursor(bv);
818                         unlockInsetInInset(bv, the_locking_inset);
819                         if (cmd.action == LFUN_CELL_FORWARD)
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)->paragraphs.begin()->
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)->paragraphs.begin()->
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 #ifdef WITH_WARNINGS
1986 #warning Need I say it ? This is horrible.
1987 #endif
1988                         Alert::error(_("Error setting multicolumn"),
1989                                    _("You cannot set multicolumn vertically."));
1990                         return;
1991                 }
1992                 // just multicol for one Single Cell
1993                 if (!hasSelection()) {
1994                         // check wether we are completly in a multicol
1995                         if (tabular->IsMultiColumn(actcell)) {
1996                                 tabular->UnsetMultiColumn(actcell);
1997                                 updateLocal(bv, INIT);
1998                         } else {
1999                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
2000                                 updateLocal(bv, CELL);
2001                         }
2002                         return;
2003                 }
2004                 // we have a selection so this means we just add all this
2005                 // cells to form a multicolumn cell
2006                 int s_start;
2007                 int s_end;
2008
2009                 if (sel_cell_start > sel_cell_end) {
2010                         s_start = sel_cell_end;
2011                         s_end = sel_cell_start;
2012                 } else {
2013                         s_start = sel_cell_start;
2014                         s_end = sel_cell_end;
2015                 }
2016                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
2017                 actcell = s_start;
2018                 clearSelection();
2019                 updateLocal(bv, INIT);
2020                 break;
2021         }
2022         case LyXTabular::SET_ALL_LINES:
2023                 setLines = true;
2024         case LyXTabular::UNSET_ALL_LINES:
2025                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2026                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2027                                 tabular->SetAllLines(
2028                                         tabular->GetCellNumber(i,j), setLines);
2029                 updateLocal(bv, INIT);
2030                 break;
2031         case LyXTabular::SET_LONGTABULAR:
2032                 tabular->SetLongTabular(true);
2033                 updateLocal(bv, INIT); // because this toggles displayed
2034                 break;
2035         case LyXTabular::UNSET_LONGTABULAR:
2036                 tabular->SetLongTabular(false);
2037                 updateLocal(bv, INIT); // because this toggles displayed
2038                 break;
2039         case LyXTabular::SET_ROTATE_TABULAR:
2040                 tabular->SetRotateTabular(true);
2041                 break;
2042         case LyXTabular::UNSET_ROTATE_TABULAR:
2043                 tabular->SetRotateTabular(false);
2044                 break;
2045         case LyXTabular::SET_ROTATE_CELL:
2046                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2047                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2048                                 tabular->SetRotateCell(
2049                                         tabular->GetCellNumber(i, j),
2050                                         true);
2051                 break;
2052         case LyXTabular::UNSET_ROTATE_CELL:
2053                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2054                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2055                                 tabular->SetRotateCell(
2056                                         tabular->GetCellNumber(i, j), false);
2057                 break;
2058         case LyXTabular::SET_USEBOX:
2059         {
2060                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2061                 if (val == tabular->GetUsebox(actcell))
2062                         val = LyXTabular::BOX_NONE;
2063                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2064                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2065                                 tabular->SetUsebox(
2066                                         tabular->GetCellNumber(i, j), val);
2067                 break;
2068         }
2069         case LyXTabular::UNSET_LTFIRSTHEAD:
2070                 flag = false;
2071         case LyXTabular::SET_LTFIRSTHEAD:
2072                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2073                 checkLongtableSpecial(ltt, value, flag);
2074                 tabular->SetLTHead(row, flag, ltt, true);
2075                 break;
2076         case LyXTabular::UNSET_LTHEAD:
2077                 flag = false;
2078         case LyXTabular::SET_LTHEAD:
2079                 (void)tabular->GetRowOfLTHead(row, ltt);
2080                 checkLongtableSpecial(ltt, value, flag);
2081                 tabular->SetLTHead(row, flag, ltt, false);
2082                 break;
2083         case LyXTabular::UNSET_LTFOOT:
2084                 flag = false;
2085         case LyXTabular::SET_LTFOOT:
2086                 (void)tabular->GetRowOfLTFoot(row, ltt);
2087                 checkLongtableSpecial(ltt, value, flag);
2088                 tabular->SetLTFoot(row, flag, ltt, false);
2089                 break;
2090         case LyXTabular::UNSET_LTLASTFOOT:
2091                 flag = false;
2092         case LyXTabular::SET_LTLASTFOOT:
2093                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2094                 checkLongtableSpecial(ltt, value, flag);
2095                 tabular->SetLTFoot(row, flag, ltt, true);
2096                 break;
2097         case LyXTabular::SET_LTNEWPAGE:
2098         {
2099                 bool what = !tabular->GetLTNewPage(row);
2100                 tabular->SetLTNewPage(row, what);
2101                 break;
2102         }
2103         // dummy stuff just to avoid warnings
2104         case LyXTabular::LAST_ACTION:
2105                 break;
2106         }
2107 }
2108
2109
2110 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
2111                                      bool behind)
2112 {
2113         UpdatableInset * inset =
2114                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2115         LyXFont font(LyXFont::ALL_SANE);
2116         if (behind) {
2117                 x = inset->x() + inset->width(bv, font);
2118                 y = inset->descent(bv, font);
2119         }
2120         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2121         //inset_y = cursor.y();
2122         inset->edit(bv, x,  y, button);
2123         if (!the_locking_inset)
2124                 return false;
2125         updateLocal(bv, CELL);
2126         return (the_locking_inset != 0);
2127 }
2128
2129
2130 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2131                                         mouse_button::state button)
2132 {
2133         inset_x = cursor_.x()
2134                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2135         inset_y = cursor_.y();
2136         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2137 }
2138
2139
2140 bool InsetTabular::insetHit(BufferView *, int x, int) const
2141 {
2142         return (x + top_x)
2143                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2144 }
2145
2146
2147 // This returns paperWidth() if the cell-width is unlimited or the width
2148 // in pixels if we have a pwidth for this cell.
2149 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2150 {
2151         LyXLength const len = tabular->GetPWidth(cell);
2152
2153         if (len.zero())
2154                 return -1;
2155         return len.inPixels(latexTextWidth(bv));
2156 }
2157
2158
2159 int InsetTabular::getMaxWidth(BufferView * bv,
2160                               UpdatableInset const * inset) const
2161 {
2162         int cell = tabular->GetCellFromInset(inset, actcell);
2163
2164         if (cell == -1) {
2165                 lyxerr << "Own inset not found, shouldn't really happen!"
2166                        << endl;
2167                 return -1;
2168         }
2169
2170         int w = getMaxWidthOfCell(bv, cell);
2171         if (w > 0) {
2172                 // because the inset then subtracts it's top_x and owner->x()
2173                 w += (inset->x() - top_x);
2174         }
2175
2176         return w;
2177 }
2178
2179
2180 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2181 {
2182         resizeLyXText(bv, recursive);
2183 }
2184
2185
2186 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2187 {
2188         if (force) {
2189                 for(int i = 0; i < tabular->rows(); ++i) {
2190                         for(int j = 0; j < tabular->columns(); ++j) {
2191                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2192                         }
2193                 }
2194         }
2195         need_update = FULL;
2196 }
2197
2198
2199 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2200                                    bool const recursive) const
2201 {
2202         if (the_locking_inset)
2203                 return the_locking_inset->getLyXText(bv, recursive);
2204 #if 0
2205         // if we're locked lock the actual insettext and return it's LyXText!!!
2206         if (locked) {
2207                 UpdatableInset * inset =
2208                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2209                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2210                 return the_locking_inset->getLyXText(bv, recursive);
2211         }
2212 #endif
2213         return Inset::getLyXText(bv, recursive);
2214 }
2215
2216
2217 bool InsetTabular::showInsetDialog(BufferView * bv) const
2218 {
2219         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv)) {
2220                 InsetTabular * tmp = const_cast<InsetTabular *>(this);
2221                 InsetTabularMailer mailer(*tmp);
2222                 mailer.showDialog(bv);
2223         }
2224         return true;
2225 }
2226
2227
2228 void InsetTabular::openLayoutDialog(BufferView * bv) const
2229 {
2230         if (the_locking_inset) {
2231                 InsetTabular * i = static_cast<InsetTabular *>
2232                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2233                 if (i) {
2234                         i->openLayoutDialog(bv);
2235                         return;
2236                 }
2237         }
2238         InsetTabular * tmp = const_cast<InsetTabular *>(this);
2239         InsetTabularMailer mailer(*tmp);
2240         mailer.showDialog(bv);
2241 }
2242
2243
2244 //
2245 // function returns an object as defined in func_status.h:
2246 // states OK, Unknown, Disabled, On, Off.
2247 //
2248 FuncStatus InsetTabular::getStatus(string const & what) const
2249 {
2250         int action = LyXTabular::LAST_ACTION;
2251         FuncStatus status;
2252
2253         int i = 0;
2254         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2255                 string const tmp = tabularFeature[i].feature;
2256                 if (tmp == what.substr(0, tmp.length())) {
2257                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2258                         //   tabularFeatures[i].feature.length())) {
2259                         action = tabularFeature[i].action;
2260                         break;
2261                 }
2262         }
2263         if (action == LyXTabular::LAST_ACTION) {
2264                 status.clear();
2265                 return status.unknown(true);
2266         }
2267
2268         string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
2269
2270         int sel_row_start;
2271         int sel_row_end;
2272         int dummy;
2273         LyXTabular::ltType dummyltt;
2274         bool flag = true;
2275
2276         if (hasSelection()) {
2277                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2278         } else {
2279                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2280         }
2281
2282         switch (action) {
2283         case LyXTabular::SET_PWIDTH:
2284         case LyXTabular::SET_MPWIDTH:
2285         case LyXTabular::SET_SPECIAL_COLUMN:
2286         case LyXTabular::SET_SPECIAL_MULTI:
2287                 return status.disabled(true);
2288
2289         case LyXTabular::APPEND_ROW:
2290         case LyXTabular::APPEND_COLUMN:
2291         case LyXTabular::DELETE_ROW:
2292         case LyXTabular::DELETE_COLUMN:
2293         case LyXTabular::SET_ALL_LINES:
2294         case LyXTabular::UNSET_ALL_LINES:
2295                 return status.clear();
2296
2297         case LyXTabular::MULTICOLUMN:
2298                 status.setOnOff(tabular->IsMultiColumn(actcell));
2299                 break;
2300         case LyXTabular::M_TOGGLE_LINE_TOP:
2301                 flag = false;
2302         case LyXTabular::TOGGLE_LINE_TOP:
2303                 status.setOnOff(tabular->TopLine(actcell, flag));
2304                 break;
2305         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2306                 flag = false;
2307         case LyXTabular::TOGGLE_LINE_BOTTOM:
2308                 status.setOnOff(tabular->BottomLine(actcell, flag));
2309                 break;
2310         case LyXTabular::M_TOGGLE_LINE_LEFT:
2311                 flag = false;
2312         case LyXTabular::TOGGLE_LINE_LEFT:
2313                 status.setOnOff(tabular->LeftLine(actcell, flag));
2314                 break;
2315         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2316                 flag = false;
2317         case LyXTabular::TOGGLE_LINE_RIGHT:
2318                 status.setOnOff(tabular->RightLine(actcell, flag));
2319                 break;
2320         case LyXTabular::M_ALIGN_LEFT:
2321                 flag = false;
2322         case LyXTabular::ALIGN_LEFT:
2323                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2324                 break;
2325         case LyXTabular::M_ALIGN_RIGHT:
2326                 flag = false;
2327         case LyXTabular::ALIGN_RIGHT:
2328                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2329                 break;
2330         case LyXTabular::M_ALIGN_CENTER:
2331                 flag = false;
2332         case LyXTabular::ALIGN_CENTER:
2333                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2334                 break;
2335         case LyXTabular::ALIGN_BLOCK:
2336                 status.disabled(tabular->GetPWidth(actcell).zero());
2337                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2338                 break;
2339         case LyXTabular::M_VALIGN_TOP:
2340                 flag = false;
2341         case LyXTabular::VALIGN_TOP:
2342                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2343                 break;
2344         case LyXTabular::M_VALIGN_BOTTOM:
2345                 flag = false;
2346         case LyXTabular::VALIGN_BOTTOM:
2347                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2348                 break;
2349         case LyXTabular::M_VALIGN_CENTER:
2350                 flag = false;
2351         case LyXTabular::VALIGN_CENTER:
2352                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2353                 break;
2354         case LyXTabular::SET_LONGTABULAR:
2355                 status.setOnOff(tabular->IsLongTabular());
2356                 break;
2357         case LyXTabular::UNSET_LONGTABULAR:
2358                 status.setOnOff(!tabular->IsLongTabular());
2359                 break;
2360         case LyXTabular::SET_ROTATE_TABULAR:
2361                 status.setOnOff(tabular->GetRotateTabular());
2362                 break;
2363         case LyXTabular::UNSET_ROTATE_TABULAR:
2364                 status.setOnOff(!tabular->GetRotateTabular());
2365                 break;
2366         case LyXTabular::SET_ROTATE_CELL:
2367                 status.setOnOff(tabular->GetRotateCell(actcell));
2368                 break;
2369         case LyXTabular::UNSET_ROTATE_CELL:
2370                 status.setOnOff(!tabular->GetRotateCell(actcell));
2371                 break;
2372         case LyXTabular::SET_USEBOX:
2373                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2374                 break;
2375         case LyXTabular::SET_LTFIRSTHEAD:
2376                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2377                 break;
2378         case LyXTabular::SET_LTHEAD:
2379                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2380                 break;
2381         case LyXTabular::SET_LTFOOT:
2382                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2383                 break;
2384         case LyXTabular::SET_LTLASTFOOT:
2385                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2386                 break;
2387         case LyXTabular::SET_LTNEWPAGE:
2388                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2389                 break;
2390         default:
2391                 status.clear();
2392                 status.disabled(true);
2393                 break;
2394         }
2395         return status;
2396 }
2397
2398
2399 vector<string> const InsetTabular::getLabelList() const
2400 {
2401         return tabular->getLabelList();
2402 }
2403
2404
2405 bool InsetTabular::copySelection(BufferView * bv)
2406 {
2407         if (!hasSelection())
2408                 return false;
2409
2410         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2411         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2412         if (sel_col_start > sel_col_end) {
2413                 sel_col_start = sel_col_end;
2414                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2415         } else {
2416                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2417         }
2418         int const columns = sel_col_end - sel_col_start + 1;
2419
2420         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2421         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2422         if (sel_row_start > sel_row_end) {
2423                 swap(sel_row_start, sel_row_end);
2424         }
2425         int const rows = sel_row_end - sel_row_start + 1;
2426
2427         delete paste_tabular;
2428         paste_tabular = new LyXTabular(bv->buffer()->params,
2429                                        this, *tabular); // rows, columns);
2430         for (int i = 0; i < sel_row_start; ++i)
2431                 paste_tabular->DeleteRow(0);
2432         while (paste_tabular->rows() > rows)
2433                 paste_tabular->DeleteRow(rows);
2434         paste_tabular->SetTopLine(0, true, true);
2435         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2436                                      true, true);
2437         for (int i = 0; i < sel_col_start; ++i)
2438                 paste_tabular->DeleteColumn(0);
2439         while (paste_tabular->columns() > columns)
2440                 paste_tabular->DeleteColumn(columns);
2441         paste_tabular->SetLeftLine(0, true, true);
2442         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2443                                     true, true);
2444
2445         ostringstream sstr;
2446         paste_tabular->ascii(bv->buffer(), sstr,
2447                              (int)parOwner()->params().depth(), true, '\t');
2448         bv->stuffClipboard(STRCONV(sstr.str()));
2449         return true;
2450 }
2451
2452
2453 bool InsetTabular::pasteSelection(BufferView * bv)
2454 {
2455         if (!paste_tabular)
2456                 return false;
2457
2458         for (int r1 = 0, r2 = actrow;
2459              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2460              ++r1, ++r2) {
2461                 for(int c1 = 0, c2 = actcol;
2462                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2463                     ++c1, ++c2) {
2464                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2465                             tabular->IsPartOfMultiColumn(r2,c2))
2466                                 continue;
2467                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2468                                 --c2;
2469                                 continue;
2470                         }
2471                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2472                                 --c1;
2473                                 continue;
2474                         }
2475                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2476                         int const n2 = tabular->GetCellNumber(r2, c2);
2477                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2478                         tabular->GetCellInset(n2)->setOwner(this);
2479                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2480                         tabular->GetCellInset(n2)->markNew();
2481                 }
2482         }
2483         return true;
2484 }
2485
2486
2487 bool InsetTabular::cutSelection(BufferParams const & bp)
2488 {
2489         if (!hasSelection())
2490                 return false;
2491
2492         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2493         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2494         if (sel_col_start > sel_col_end) {
2495                 sel_col_start = sel_col_end;
2496                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2497         } else {
2498                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2499         }
2500         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2501         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2502         if (sel_row_start > sel_row_end) {
2503                 swap(sel_row_start, sel_row_end);
2504         }
2505         if (sel_cell_start > sel_cell_end) {
2506                 swap(sel_cell_start, sel_cell_end);
2507         }
2508         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2509                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2510                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear(bp.tracking_changes);
2511                 }
2512         }
2513         return true;
2514 }
2515
2516
2517 bool InsetTabular::isRightToLeft(BufferView * bv)
2518 {
2519         return bv->getParentLanguage(this)->RightToLeft();
2520 }
2521
2522
2523 bool InsetTabular::nodraw() const
2524 {
2525         if (!UpdatableInset::nodraw() && the_locking_inset)
2526                 return the_locking_inset->nodraw();
2527         return UpdatableInset::nodraw();
2528 }
2529
2530
2531 int InsetTabular::scroll(bool recursive) const
2532 {
2533         int sx = UpdatableInset::scroll(false);
2534
2535         if (recursive && the_locking_inset)
2536                 sx += the_locking_inset->scroll(recursive);
2537
2538         return sx;
2539 }
2540
2541
2542 void InsetTabular::getSelection(int & srow, int & erow,
2543                                 int & scol, int & ecol) const
2544 {
2545         int const start = hasSelection() ? sel_cell_start : actcell;
2546         int const end = hasSelection() ? sel_cell_end : actcell;
2547
2548         srow = tabular->row_of_cell(start);
2549         erow = tabular->row_of_cell(end);
2550         if (srow > erow) {
2551                 swap(srow, erow);
2552         }
2553
2554         scol = tabular->column_of_cell(start);
2555         ecol = tabular->column_of_cell(end);
2556         if (scol > ecol) {
2557                 swap(scol, ecol);
2558         } else {
2559                 ecol = tabular->right_column_of_cell(end);
2560         }
2561 }
2562
2563
2564 Paragraph * InsetTabular::firstParagraph() const
2565 {
2566         if (the_locking_inset)
2567                 return the_locking_inset->firstParagraph();
2568         return 0;
2569 }
2570
2571
2572 Paragraph * InsetTabular::getFirstParagraph(int i) const
2573 {
2574         return (i < tabular->GetNumberOfCells())
2575                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2576                 : 0;
2577 }
2578
2579
2580 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2581 {
2582         if (the_locking_inset)
2583                 return the_locking_inset->cursor(bv);
2584         return Inset::cursor(bv);
2585 }
2586
2587
2588 Inset * InsetTabular::getInsetFromID(int id_arg) const
2589 {
2590         if (id_arg == id())
2591                 return const_cast<InsetTabular *>(this);
2592
2593         Inset * result;
2594         for(int i = 0; i < tabular->rows(); ++i) {
2595                 for(int j = 0; j < tabular->columns(); ++j) {
2596                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2597                                 return result;
2598                 }
2599         }
2600         return 0;
2601 }
2602
2603
2604 WordLangTuple const
2605 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2606 {
2607         nodraw(true);
2608         if (the_locking_inset) {
2609                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2610                 if (!word.word().empty()) {
2611                         nodraw(false);
2612                         return word;
2613                 }
2614                 if (tabular->IsLastCell(actcell)) {
2615                         bv->unlockInset(const_cast<InsetTabular *>(this));
2616                         nodraw(false);
2617                         return WordLangTuple();
2618                 }
2619                 ++actcell;
2620         }
2621         // otherwise we have to lock the next inset and ask for it's selecttion
2622         UpdatableInset * inset =
2623                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2624         inset->edit(bv, 0,  0, mouse_button::none);
2625         WordLangTuple word(selectNextWordInt(bv, value));
2626         nodraw(false);
2627         if (!word.word().empty())
2628                 resetPos(bv);
2629         return word;
2630 }
2631
2632
2633 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2634 {
2635         // when entering this function the inset should be ALWAYS locked!
2636         lyx::Assert(the_locking_inset);
2637
2638         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2639         if (!word.word().empty())
2640                 return word;
2641
2642         if (tabular->IsLastCell(actcell)) {
2643                 bv->unlockInset(const_cast<InsetTabular *>(this));
2644                 return WordLangTuple();
2645         }
2646
2647         // otherwise we have to lock the next inset and ask for it's selecttion
2648         UpdatableInset * inset =
2649                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2650         inset->edit(bv);
2651         return selectNextWordInt(bv, value);
2652 }
2653
2654
2655 void InsetTabular::selectSelectedWord(BufferView * bv)
2656 {
2657         if (the_locking_inset) {
2658                 the_locking_inset->selectSelectedWord(bv);
2659                 return;
2660         }
2661         return;
2662 }
2663
2664
2665 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2666 {
2667         if (the_locking_inset) {
2668                 the_locking_inset->toggleSelection(bv, kill_selection);
2669         }
2670 }
2671
2672
2673 void InsetTabular::markErased()
2674 {
2675         int cell = 0;
2676
2677         while (!tabular->IsLastCell(cell)) {
2678                 ++cell;
2679                 InsetText * inset = tabular->GetCellInset(cell);
2680                 inset->markErased();
2681         }
2682 }
2683
2684
2685 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2686 {
2687         if (the_locking_inset) {
2688                 if (the_locking_inset->nextChange(bv, length)) {
2689                         updateLocal(bv, CELL);
2690                         return true;
2691                 }
2692                 if (tabular->IsLastCell(actcell))
2693                         return false;
2694                 ++actcell;
2695         }
2696         InsetText * inset = tabular->GetCellInset(actcell);
2697         if (inset->nextChange(bv, length)) {
2698                 updateLocal(bv, FULL);
2699                 return true;
2700         }
2701         while (!tabular->IsLastCell(actcell)) {
2702                 ++actcell;
2703                 inset = tabular->GetCellInset(actcell);
2704                 if (inset->nextChange(bv, length)) {
2705                         updateLocal(bv, FULL);
2706                         return true;
2707                 }
2708         }
2709         return false;
2710 }
2711
2712
2713 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2714                                  bool cs, bool mw)
2715 {
2716         if (the_locking_inset) {
2717                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2718                         updateLocal(bv, CELL);
2719                         return true;
2720                 }
2721                 if (tabular->IsLastCell(actcell))
2722                         return false;
2723                 ++actcell;
2724         }
2725         InsetText * inset = tabular->GetCellInset(actcell);
2726         if (inset->searchForward(bv, str, cs, mw)) {
2727                 updateLocal(bv, FULL);
2728                 return true;
2729         }
2730         while (!tabular->IsLastCell(actcell)) {
2731                 ++actcell;
2732                 inset = tabular->GetCellInset(actcell);
2733                 if (inset->searchForward(bv, str, cs, mw)) {
2734                         updateLocal(bv, FULL);
2735                         return true;
2736                 }
2737         }
2738         return false;
2739 }
2740
2741
2742 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2743                                bool cs, bool mw)
2744 {
2745         if (the_locking_inset) {
2746                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2747                         updateLocal(bv, CELL);
2748                         return true;
2749                 }
2750         }
2751         if (!locked)
2752                 actcell = tabular->GetNumberOfCells();
2753
2754         while (actcell) {
2755                 --actcell;
2756                 InsetText * inset = tabular->GetCellInset(actcell);
2757                 if (inset->searchBackward(bv, str, cs, mw)) {
2758                         updateLocal(bv, CELL);
2759                         return true;
2760                 }
2761         }
2762         return false;
2763 }
2764
2765
2766 bool InsetTabular::insetAllowed(Inset::Code code) const
2767 {
2768         if (the_locking_inset)
2769                 return the_locking_inset->insetAllowed(code);
2770         // we return true here because if the inset is not locked someone
2771         // wants to insert something in one of our insettexts and we generally
2772         // allow to do so.
2773         return true;
2774 }
2775
2776
2777 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2778 {
2779         const int cell = tabular->GetCellFromInset(in, actcell);
2780
2781         if (cell != -1)
2782                 return tabular->GetPWidth(cell).zero();
2783
2784         // well we didn't obviously find it so maybe our owner knows more
2785         if (owner())
2786                 return owner()->forceDefaultParagraphs(in);
2787         // if we're here there is really something strange going on!!!
2788         return false;
2789 }
2790
2791 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2792                                      bool usePaste)
2793 {
2794         if (buf.length() <= 0)
2795                 return true;
2796
2797         int cols = 1;
2798         int rows = 1;
2799         int maxCols = 1;
2800         string::size_type len = buf.length();
2801         string::size_type p = 0;
2802
2803         while (p < len &&
2804                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2805         {
2806                 switch (buf[p]) {
2807                 case '\t':
2808                         ++cols;
2809                         break;
2810                 case '\n':
2811                         if ((p+1) < len)
2812                                 ++rows;
2813                         maxCols = max(cols, maxCols);
2814                         cols = 1;
2815                         break;
2816                 }
2817                 ++p;
2818         }
2819         maxCols = max(cols, maxCols);
2820         LyXTabular * loctab;
2821         int cell = 0;
2822         int ocol = 0;
2823         int row = 0;
2824         if (usePaste) {
2825                 delete paste_tabular;
2826                 paste_tabular = new LyXTabular(bv->buffer()->params,
2827                                                this, rows, maxCols);
2828                 loctab = paste_tabular;
2829                 cols = 0;
2830         } else {
2831                 loctab = tabular.get();
2832                 cell = actcell;
2833                 ocol = actcol;
2834                 row = actrow;
2835         }
2836
2837         string::size_type op = 0;
2838         int cells = loctab->GetNumberOfCells();
2839         p = 0;
2840         cols = ocol;
2841         rows = loctab->rows();
2842         int const columns = loctab->columns();
2843
2844         while ((cell < cells) && (p < len) && (row < rows) &&
2845                (p = buf.find_first_of("\t\n", p)) != string::npos)
2846         {
2847                 if (p >= len)
2848                         break;
2849                 switch (buf[p]) {
2850                 case '\t':
2851                         // we can only set this if we are not too far right
2852                         if (cols < columns) {
2853                                 InsetText * ti = loctab->GetCellInset(cell);
2854                                 LyXFont const font = ti->getLyXText(bv)->
2855                                         getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
2856                                 ti->setText(buf.substr(op, p - op), font);
2857                                 ++cols;
2858                                 ++cell;
2859                         }
2860                         break;
2861                 case '\n':
2862                         // we can only set this if we are not too far right
2863                         if (cols < columns) {
2864                                 InsetText * ti = loctab->GetCellInset(cell);
2865                                 LyXFont const font = ti->getLyXText(bv)->
2866                                         getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
2867                                 ti->setText(buf.substr(op, p - op), font);
2868                         }
2869                         cols = ocol;
2870                         ++row;
2871                         if (row < rows)
2872                                 cell = loctab->GetCellNumber(row, cols);
2873                         break;
2874                 }
2875                 ++p;
2876                 op = p;
2877         }
2878         // check for the last cell if there is no trailing '\n'
2879         if ((cell < cells) && (op < len)) {
2880                 InsetText * ti = loctab->GetCellInset(cell);
2881                 LyXFont const font = ti->getLyXText(bv)->
2882                         getFont(bv->buffer(), &*ti->paragraphs.begin(), 0);
2883                 ti->setText(buf.substr(op, len - op), font);
2884         }
2885
2886         return true;
2887 }
2888
2889
2890 void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
2891 {
2892         int const rows = tabular->rows();
2893         int const columns = tabular->columns();
2894         for (int i = 0; i < rows; ++i) {
2895                 for (int j = 0; j < columns; ++j) {
2896                         tabular->GetCellInset(i,j)->addPreview(loader);
2897                 }
2898         }
2899 }
2900
2901
2902 string const InsetTabularMailer:: name_("tabular");
2903
2904 InsetTabularMailer::InsetTabularMailer(InsetTabular & inset)
2905         : inset_(inset)
2906 {}
2907
2908
2909 string const InsetTabularMailer::inset2string() const
2910 {
2911         return params2string(inset_);
2912 }
2913
2914
2915 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2916 {
2917         istringstream data(in);
2918         LyXLex lex(0,0);
2919         lex.setStream(data);
2920
2921         if (lex.isOK()) {
2922                 lex.next();
2923                 string const token = lex.getString();
2924                 if (token != name_)
2925                         return -1;
2926         }
2927
2928         int cell = -1;
2929         if (lex.isOK()) {
2930                 lex.next();
2931                 string const token = lex.getString();
2932                 if (token != "\\active_cell")
2933                         return -1;
2934                 lex.next();
2935                 cell = lex.getInteger();
2936         }
2937
2938         // This is part of the inset proper that is usually swallowed
2939         // by Buffer::readInset
2940         if (lex.isOK()) {
2941                 lex.next();
2942                 string const token = lex.getString();
2943                 if (token != "Tabular")
2944                         return -1;
2945         }
2946
2947         if (!lex.isOK())
2948                 return -1;
2949
2950         BufferView * const bv = inset.view();
2951         Buffer const * const buffer = bv ? bv->buffer() : 0;
2952         if (buffer)
2953                 inset.read(buffer, lex);
2954
2955         // We can't set the active cell, but we can tell the frontend
2956         // what it is.
2957         return cell;
2958 }
2959
2960
2961 string const
2962 InsetTabularMailer::params2string(InsetTabular const & inset)
2963 {
2964         BufferView * const bv = inset.view();
2965         Buffer const * const buffer = bv ? bv->buffer() : 0;
2966         if (!buffer)
2967                 return string();
2968
2969         ostringstream data;
2970         data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2971         inset.write(buffer, data);
2972         data << "\\end_inset\n";
2973
2974         return data.str();
2975 }