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