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