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