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