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