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