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