]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
Reduce Michael's buglist.
[features.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         // fast hack to fix infinite repaintings!
1356         if (in_reset_pos)
1357                 return;
1358         in_reset_pos = true;
1359
1360         actcol = tabular->column_of_cell(actcell);
1361
1362         int cell = 0;
1363         actrow = 0;
1364         cursor_.y(0);
1365         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1366                 if (tabular->IsLastCellInRow(cell)) {
1367                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1368                                          tabular->GetAscentOfRow(actrow + 1) +
1369                                          tabular->GetAdditionalHeight(actrow + 1));
1370                         ++actrow;
1371                 }
1372         }
1373         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1374         int new_x = getCellXPos(actcell);
1375         int old_x = cursor_.x();
1376         new_x += offset;
1377         cursor_.x(new_x);
1378 //    cursor.x(getCellXPos(actcell) + offset);
1379         if ((actcol < tabular->columns()-1) && scroll(false) &&
1380                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1381         {
1382                 scroll(bv, 0.0F);
1383                 updateLocal(bv, FULL, false);
1384         } else if (the_locking_inset &&
1385                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1386         {
1387                 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1388                 if (xx > (bv->workWidth()-20)) {
1389                         scroll(bv, -(xx - bv->workWidth() + 60));
1390                         updateLocal(bv, FULL, false);
1391                 } else if (xx < 20) {
1392                         if (xx < 0)
1393                                 xx = -xx + 60;
1394                         else
1395                                 xx = 60;
1396                         scroll(bv, xx);
1397                         updateLocal(bv, FULL, false);
1398                 }
1399         } else if ((cursor_.x() - offset) > 20 &&
1400                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1401                    > (bv->workWidth() - 20)) {
1402                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1403                 updateLocal(bv, FULL, false);
1404         } else if ((cursor_.x() - offset) < 20) {
1405                 scroll(bv, 20 - cursor_.x() + offset);
1406                 updateLocal(bv, FULL, false);
1407         } else if (scroll(false) && top_x > 20 &&
1408                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1409                 scroll(bv, old_x - cursor_.x());
1410                 updateLocal(bv, FULL, false);
1411         }
1412         if (the_locking_inset) {
1413                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1414                 inset_y = cursor_.y();
1415         }
1416         if ((!the_locking_inset ||
1417              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1418             actcell != oldcell) {
1419                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1420                 bv->owner()->getDialogs()->updateTabular(inset);
1421                 oldcell = actcell;
1422         }
1423         in_reset_pos = false;
1424 }
1425
1426
1427 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1428 {
1429         if (lock && !old_locking_inset) {
1430                 if (activateCellInset(bv))
1431                         return DISPATCHED;
1432         } else {
1433                 bool moved = isRightToLeft(bv)
1434                         ? movePrevCell(bv) : moveNextCell(bv);
1435                 if (!moved)
1436                         return FINISHED_RIGHT;
1437                 if (lock && activateCellInset(bv))
1438                         return DISPATCHED;
1439         }
1440         resetPos(bv);
1441         return DISPATCHED_NOUPDATE;
1442 }
1443
1444
1445 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1446 {
1447         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1448         if (!moved)
1449                 return FINISHED;
1450         if (lock) {       // behind the inset
1451                 if (activateCellInset(bv, 0, 0, 0, true))
1452                         return DISPATCHED;
1453         }
1454         resetPos(bv);
1455         return DISPATCHED_NOUPDATE;
1456 }
1457
1458
1459 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1460 {
1461         int const ocell = actcell;
1462         actcell = tabular->GetCellAbove(actcell);
1463         if (actcell == ocell) // we moved out of the inset
1464                 return FINISHED_UP;
1465         resetPos(bv);
1466         if (lock) {
1467                 int x = 0;
1468                 int y = 0;
1469                 if (old_locking_inset) {
1470                         old_locking_inset->getCursorPos(bv, x, y);
1471                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1472                 }
1473                 if (activateCellInset(bv, x, 0))
1474                         return DISPATCHED;
1475         }
1476         return DISPATCHED_NOUPDATE;
1477 }
1478
1479
1480 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1481 {
1482         int const ocell = actcell;
1483         actcell = tabular->GetCellBelow(actcell);
1484         if (actcell == ocell) // we moved out of the inset
1485                 return FINISHED_DOWN;
1486         resetPos(bv);
1487         if (lock) {
1488                 int x = 0;
1489                 int y = 0;
1490                 if (old_locking_inset) {
1491                         old_locking_inset->getCursorPos(bv, x, y);
1492                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1493                 }
1494                 if (activateCellInset(bv, x, 0))
1495                         return DISPATCHED;
1496         }
1497         return DISPATCHED_NOUPDATE;
1498 }
1499
1500
1501 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1502 {
1503         if (isRightToLeft(bv)) {
1504                 if (tabular->IsFirstCellInRow(actcell)) {
1505                         int row = tabular->row_of_cell(actcell);
1506                         if (row == tabular->rows() - 1)
1507                                 return false;
1508                         actcell = tabular->GetLastCellInRow(row);
1509                         actcell = tabular->GetCellBelow(actcell);
1510                 } else {
1511                         if (!actcell)
1512                                 return false;
1513                         --actcell;
1514                 }
1515         } else {
1516                 if (tabular->IsLastCell(actcell))
1517                         return false;
1518                 ++actcell;
1519         }
1520         if (lock) {
1521                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1522                         isRightToLeftPar(bv->buffer()->params);
1523                 activateCellInset(bv, 0, 0, 0, !rtl);
1524         }
1525         resetPos(bv);
1526         return true;
1527 }
1528
1529
1530 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1531 {
1532         if (isRightToLeft(bv)) {
1533                 if (tabular->IsLastCellInRow(actcell)) {
1534                         int row = tabular->row_of_cell(actcell);
1535                         if (row == 0)
1536                                 return false;
1537                         actcell = tabular->GetFirstCellInRow(row);
1538                         actcell = tabular->GetCellAbove(actcell);
1539                 } else {
1540                         if (tabular->IsLastCell(actcell))
1541                                 return false;
1542                         ++actcell;
1543                 }
1544         } else {
1545                 if (!actcell) // first cell
1546                         return false;
1547                 --actcell;
1548         }
1549         if (lock) {
1550                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1551                         isRightToLeftPar(bv->buffer()->params);
1552                 activateCellInset(bv, 0, 0, 0, !rtl);
1553         }
1554         resetPos(bv);
1555         return true;
1556 }
1557
1558
1559 bool InsetTabular::deletable() const
1560 {
1561         return true;
1562 }
1563
1564
1565 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1566                            bool selectall)
1567 {
1568         if (selectall) {
1569                 sel_cell_start = 0;
1570                 sel_cell_end = tabular->GetNumberOfCells() - 1;
1571         }
1572         if (hasSelection()) {
1573                 setUndo(bv, Undo::EDIT,
1574                         bv->text->cursor.par(),
1575                         bv->text->cursor.par()->next());
1576                 bool frozen = undo_frozen;
1577                 if (!frozen)
1578                         freezeUndo();
1579                 // apply the fontchange on the whole selection
1580                 int sel_row_start;
1581                 int sel_row_end;
1582                 int sel_col_start;
1583                 int sel_col_end;
1584                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1585                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1586                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1587                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1588                         }
1589                 }
1590                 if (!frozen)
1591                         unFreezeUndo();
1592                 updateLocal(bv, INIT, true);
1593         }
1594         if (the_locking_inset)
1595                 the_locking_inset->setFont(bv, font, tall);
1596 }
1597
1598
1599 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1600 {
1601         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1602         
1603         int i = 0;
1604         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1605                 string const tmp = tabularFeature[i].feature;
1606                 
1607                 if (tmp == what.substr(0, tmp.length())) {
1608                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1609                         //tabularFeatures[i].feature.length())) {
1610                         action = tabularFeature[i].action;
1611                         break;
1612                 }
1613         }
1614         if (action == LyXTabular::LAST_ACTION)
1615                 return false;
1616
1617         string const val =
1618                 frontStrip(what.substr(tabularFeature[i].feature.length()));
1619         tabularFeatures(bv, action, val);
1620         return true;
1621 }
1622
1623
1624 void InsetTabular::tabularFeatures(BufferView * bv,
1625                                    LyXTabular::Feature feature,
1626                                    string const & value)
1627 {
1628         int sel_col_start;
1629         int sel_col_end;
1630         int sel_row_start;
1631         int sel_row_end;
1632         bool setLines = false;
1633         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1634         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1635
1636         switch (feature) {
1637         case LyXTabular::M_ALIGN_LEFT:
1638         case LyXTabular::ALIGN_LEFT:
1639                 setAlign=LYX_ALIGN_LEFT;
1640                 break;
1641         case LyXTabular::M_ALIGN_RIGHT:
1642         case LyXTabular::ALIGN_RIGHT:
1643                 setAlign=LYX_ALIGN_RIGHT;
1644                 break;
1645         case LyXTabular::M_ALIGN_CENTER:
1646         case LyXTabular::ALIGN_CENTER:
1647                 setAlign=LYX_ALIGN_CENTER;
1648                 break;
1649         case LyXTabular::M_VALIGN_TOP:
1650         case LyXTabular::VALIGN_TOP:
1651                 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1652                 break;
1653         case LyXTabular::M_VALIGN_BOTTOM:
1654         case LyXTabular::VALIGN_BOTTOM:
1655                 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1656                 break;
1657         case LyXTabular::M_VALIGN_CENTER:
1658         case LyXTabular::VALIGN_CENTER:
1659                 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1660                 break;
1661         default:
1662                 break;
1663         }
1664         if (hasSelection()) {
1665                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1666         } else {
1667                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1668                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1669         }
1670         setUndo(bv, Undo::FINISH,
1671                 bv->text->cursor.par(),
1672                 bv->text->cursor.par()->next());
1673
1674         LyXTabular::ltType ltt;
1675         int row = ltt.row = tabular->row_of_cell(actcell);
1676         int column = tabular->column_of_cell(actcell);
1677         bool flag = true;
1678
1679         switch (feature) {
1680         case LyXTabular::SET_PWIDTH:
1681         {
1682                 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1683                 tabular->SetColumnPWidth(actcell,value);
1684                 if (update) {
1685                         for (int i = 0; i < tabular->rows(); ++i) {
1686                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1687                                         resizeLyXText(bv);
1688                         }
1689                         updateLocal(bv, INIT, true);
1690                 }
1691         }
1692         break;
1693         case LyXTabular::SET_MPWIDTH:
1694         {
1695                 bool const update = (tabular->GetPWidth(actcell) != value);
1696                 tabular->SetMColumnPWidth(actcell,value);
1697                 if (update) {
1698                         for (int i = 0; i < tabular->rows(); ++i) {
1699                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1700                                         resizeLyXText(bv);
1701                         }
1702                         updateLocal(bv, INIT, true);
1703                 }
1704         }
1705         break;
1706         case LyXTabular::SET_SPECIAL_COLUMN:
1707         case LyXTabular::SET_SPECIAL_MULTI:
1708                 tabular->SetAlignSpecial(actcell,value,feature);
1709                 updateLocal(bv, FULL, true);
1710                 break;
1711         case LyXTabular::APPEND_ROW:
1712                 // append the row into the tabular
1713                 unlockInsetInInset(bv, the_locking_inset);
1714                 tabular->AppendRow(actcell);
1715                 updateLocal(bv, INIT, true);
1716                 break;
1717         case LyXTabular::APPEND_COLUMN:
1718                 // append the column into the tabular
1719                 unlockInsetInInset(bv, the_locking_inset);
1720                 tabular->AppendColumn(actcell);
1721                 actcell = tabular->GetCellNumber(row, column);
1722                 updateLocal(bv, INIT, true);
1723                 break;
1724         case LyXTabular::DELETE_ROW:
1725                 unlockInsetInInset(bv, the_locking_inset);
1726                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1727                         tabular->DeleteRow(sel_row_start);
1728                 }
1729                 if (sel_row_start >= tabular->rows())
1730                         --sel_row_start;
1731                 actcell = tabular->GetCellNumber(sel_row_start, column);
1732                 clearSelection();
1733                 updateLocal(bv, INIT, true);
1734                 break;
1735         case LyXTabular::DELETE_COLUMN:
1736                 unlockInsetInInset(bv, the_locking_inset);
1737                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1738                         tabular->DeleteColumn(sel_col_start);
1739                 }
1740                 if (sel_col_start >= tabular->columns())
1741                         --sel_col_start;
1742                 actcell = tabular->GetCellNumber(row, sel_col_start);
1743                 clearSelection();
1744                 updateLocal(bv, INIT, true);
1745                 break;
1746         case LyXTabular::M_TOGGLE_LINE_TOP:
1747                 flag = false;
1748         case LyXTabular::TOGGLE_LINE_TOP:
1749         {
1750                 bool lineSet = !tabular->TopLine(actcell, flag);
1751                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1752                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1753                                 tabular->SetTopLine(
1754                                         tabular->GetCellNumber(i, j),
1755                                         lineSet, flag);
1756                 updateLocal(bv, INIT, true);
1757                 break;
1758         }
1759         
1760         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1761                 flag = false;
1762         case LyXTabular::TOGGLE_LINE_BOTTOM:
1763         {
1764                 bool lineSet = !tabular->BottomLine(actcell, flag); 
1765                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1766                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1767                                 tabular->SetBottomLine(
1768                                         tabular->GetCellNumber(i, j),
1769                                         lineSet,
1770                                         flag);
1771                 updateLocal(bv, INIT, true);
1772                 break;
1773         }
1774         
1775         case LyXTabular::M_TOGGLE_LINE_LEFT:
1776                 flag = false;
1777         case LyXTabular::TOGGLE_LINE_LEFT:
1778         {
1779                 bool lineSet = !tabular->LeftLine(actcell, flag);
1780                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1781                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1782                                 tabular->SetLeftLine(
1783                                         tabular->GetCellNumber(i,j),
1784                                         lineSet,
1785                                         flag);
1786                 updateLocal(bv, INIT, true);
1787                 break;
1788         }
1789         
1790         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1791                 flag = false;
1792         case LyXTabular::TOGGLE_LINE_RIGHT:
1793         {
1794                 bool lineSet = !tabular->RightLine(actcell, flag);
1795                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1796                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1797                                 tabular->SetRightLine(
1798                                         tabular->GetCellNumber(i,j),
1799                                         lineSet,
1800                                         flag);
1801                 updateLocal(bv, INIT, true);
1802                 break;
1803         }
1804         
1805         case LyXTabular::M_ALIGN_LEFT:
1806         case LyXTabular::M_ALIGN_RIGHT:
1807         case LyXTabular::M_ALIGN_CENTER:
1808                 flag = false;
1809         case LyXTabular::ALIGN_LEFT:
1810         case LyXTabular::ALIGN_RIGHT:
1811         case LyXTabular::ALIGN_CENTER:
1812                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1813                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1814                                 tabular->SetAlignment(
1815                                         tabular->GetCellNumber(i, j),
1816                                         setAlign,
1817                                         flag);
1818                 updateLocal(bv, INIT, true);
1819                 break;
1820         case LyXTabular::M_VALIGN_TOP:
1821         case LyXTabular::M_VALIGN_BOTTOM:
1822         case LyXTabular::M_VALIGN_CENTER:
1823                 flag = false;
1824         case LyXTabular::VALIGN_TOP:
1825         case LyXTabular::VALIGN_BOTTOM:
1826         case LyXTabular::VALIGN_CENTER:
1827                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1828                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1829                                 tabular->SetVAlignment(
1830                                         tabular->GetCellNumber(i, j),
1831                                         setVAlign, flag);
1832                 updateLocal(bv, INIT, true);
1833                 break;
1834         case LyXTabular::MULTICOLUMN:
1835         {
1836                 if (sel_row_start != sel_row_end) {
1837                         Alert::alert(_("Impossible Operation!"), 
1838                                    _("Multicolumns can only be horizontally."), 
1839                                    _("Sorry."));
1840                         return;
1841                 }
1842                 // just multicol for one Single Cell
1843                 if (!hasSelection()) {
1844                         // check wether we are completly in a multicol
1845                         if (tabular->IsMultiColumn(actcell)) {
1846                                 tabular->UnsetMultiColumn(actcell);
1847                                 updateLocal(bv, INIT, true);
1848                         } else {
1849                                 tabular->SetMultiColumn(actcell, 1);
1850                                 updateLocal(bv, CELL, true);
1851                         }
1852                         return;
1853                 }
1854                 // we have a selection so this means we just add all this
1855                 // cells to form a multicolumn cell
1856                 int s_start;
1857                 int s_end;
1858
1859                 if (sel_cell_start > sel_cell_end) {
1860                         s_start = sel_cell_end;
1861                         s_end = sel_cell_start;
1862                 } else {
1863                         s_start = sel_cell_start;
1864                         s_end = sel_cell_end;
1865                 }
1866                 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1867                 actcell = s_start;
1868                 sel_cell_end = sel_cell_start;
1869                 updateLocal(bv, INIT, true);
1870                 break;
1871         }
1872         case LyXTabular::SET_ALL_LINES:
1873                 setLines = true;
1874         case LyXTabular::UNSET_ALL_LINES:
1875                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1876                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1877                                 tabular->SetAllLines(
1878                                         tabular->GetCellNumber(i,j), setLines);
1879                 updateLocal(bv, INIT, true);
1880                 break;
1881         case LyXTabular::SET_LONGTABULAR:
1882                 tabular->SetLongTabular(true);
1883                 updateLocal(bv, INIT, true); // because this toggles displayed
1884                 break;
1885         case LyXTabular::UNSET_LONGTABULAR:
1886                 tabular->SetLongTabular(false);
1887                 updateLocal(bv, INIT, true); // because this toggles displayed
1888                 break;
1889         case LyXTabular::SET_ROTATE_TABULAR:
1890                 tabular->SetRotateTabular(true);
1891                 break;
1892         case LyXTabular::UNSET_ROTATE_TABULAR:
1893                 tabular->SetRotateTabular(false);
1894                 break;
1895         case LyXTabular::SET_ROTATE_CELL:
1896                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1897                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1898                                 tabular->SetRotateCell(
1899                                         tabular->GetCellNumber(i, j),
1900                                         true);
1901                 break;
1902         case LyXTabular::UNSET_ROTATE_CELL:
1903                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1904                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1905                                 tabular->SetRotateCell(
1906                                         tabular->GetCellNumber(i, j), false);
1907                 break;
1908         case LyXTabular::SET_USEBOX:
1909         {
1910                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1911                 if (val == tabular->GetUsebox(actcell))
1912                         val = LyXTabular::BOX_NONE;
1913                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1914                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1915                                 tabular->SetUsebox(
1916                                         tabular->GetCellNumber(i, j), val);
1917                 break;
1918         }
1919         case LyXTabular::UNSET_LTFIRSTHEAD:
1920                 ltt.row = 0;
1921         case LyXTabular::SET_LTFIRSTHEAD:
1922                 tabular->SetLTHead(ltt, true);
1923                 break;
1924         case LyXTabular::UNSET_LTHEAD:
1925                 ltt.row = 0;
1926         case LyXTabular::SET_LTHEAD:
1927                 tabular->SetLTHead(ltt, false);
1928                 break;
1929         case LyXTabular::UNSET_LTFOOT:
1930                 ltt.row = 0;
1931         case LyXTabular::SET_LTFOOT:
1932                 tabular->SetLTFoot(ltt, false);
1933                 break;
1934         case LyXTabular::UNSET_LTLASTFOOT:
1935                 ltt.row = 0;
1936         case LyXTabular::SET_LTLASTFOOT:
1937                 tabular->SetLTFoot(ltt, true);
1938                 break;
1939         case LyXTabular::SET_LTNEWPAGE:
1940         {
1941                 bool what = !tabular->GetLTNewPage(actcell);
1942                 tabular->SetLTNewPage(actcell, what);
1943                 break;
1944         }
1945         // dummy stuff just to avoid warnings
1946         case LyXTabular::LAST_ACTION:
1947                 break;
1948         }
1949 }
1950
1951
1952 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
1953                                      bool behind)
1954 {
1955         UpdatableInset * inset =
1956                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1957         LyXFont font(LyXFont::ALL_SANE);
1958         if (behind) {
1959                 x = inset->x() + inset->width(bv, font);
1960                 y = inset->descent(bv, font);
1961         }
1962         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1963         //inset_y = cursor.y();
1964         inset->edit(bv, x,  y, button);
1965         if (!the_locking_inset)
1966                 return false;
1967         updateLocal(bv, CELL, false);
1968         return (the_locking_inset != 0);
1969 }
1970
1971
1972 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1973                                         int button)
1974 {
1975         inset_x = cursor_.x()
1976                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1977         inset_y = cursor_.y();
1978         return activateCellInset(bv, x - inset_x, y - inset_y, button);
1979 }
1980
1981
1982 bool InsetTabular::insetHit(BufferView *, int x, int) const
1983 {
1984         return (x + top_x)
1985                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
1986 }
1987
1988
1989 // This returns paperWidth() if the cell-width is unlimited or the width
1990 // in pixels if we have a pwidth for this cell.
1991 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
1992 {
1993         string const s = tabular->GetPWidth(cell);
1994         
1995         if (s.empty())
1996                 return -1;
1997         return VSpace(s).inPixels(bv);
1998 }
1999
2000
2001 int InsetTabular::getMaxWidth(BufferView * bv,
2002                               UpdatableInset const * inset) const
2003 {
2004         int cell = tabular->cur_cell;
2005         if (tabular->GetCellInset(cell) != inset) {
2006                 cell = actcell;
2007                 if (tabular->GetCellInset(cell) != inset) {
2008                         
2009                         lyxerr[Debug::INSETTEXT] << "Actcell not equal to actual cell!\n";
2010                         cell = -1;
2011                 }
2012         }
2013         
2014         int const n = tabular->GetNumberOfCells();
2015
2016         if (cell == -1) {
2017                 for (cell = 0; cell < n; ++cell) {
2018                         if (tabular->GetCellInset(cell) == inset)
2019                                 break;
2020                 }
2021         }
2022         
2023         if (cell >= n) {
2024                 lyxerr << "Own inset not found, shouldn't really happen!\n";
2025                 return -1;
2026         }
2027         
2028         int w = getMaxWidthOfCell(bv, cell);
2029         if (w > 0) {
2030                 // because the inset then subtracts it's top_x and owner->x()
2031                 w += (inset->x() - top_x);
2032         }
2033         
2034         return w;
2035 }
2036
2037
2038 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2039 {
2040         resizeLyXText(bv, recursive);
2041 }
2042
2043
2044 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2045 {
2046         if (force) {
2047                 for(int i = 0; i < tabular->rows(); ++i) {
2048                         for(int j = 0; j < tabular->columns(); ++j) {
2049                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2050                         }
2051                 }
2052         }
2053         need_update = FULL;
2054 }
2055
2056
2057 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2058                                    bool const recursive) const
2059 {
2060         if (the_locking_inset)
2061                 return the_locking_inset->getLyXText(bv, recursive);
2062 #if 0
2063         // if we're locked lock the actual insettext and return it's LyXText!!!
2064         if (locked) {
2065                 UpdatableInset * inset =
2066                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2067                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2068                 return the_locking_inset->getLyXText(bv, recursive);
2069         }
2070 #endif
2071         return Inset::getLyXText(bv, recursive);
2072 }
2073
2074
2075 bool InsetTabular::showInsetDialog(BufferView * bv) const
2076 {
2077         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2078                 bv->owner()->getDialogs()
2079                         ->showTabular(const_cast<InsetTabular *>(this));
2080         return true;
2081 }
2082
2083
2084 void InsetTabular::openLayoutDialog(BufferView * bv) const
2085 {
2086         if (the_locking_inset) {
2087                 InsetTabular * i = static_cast<InsetTabular *>
2088                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2089                 if (i) {
2090                         i->openLayoutDialog(bv);
2091                         return;
2092                 }
2093         }
2094         bv->owner()->getDialogs()->showTabular(
2095                 const_cast<InsetTabular *>(this));
2096 }
2097
2098
2099 //
2100 // functions returns:
2101 // 0 ... disabled
2102 // 1 ... enabled
2103 // 2 ... toggled on
2104 // 3 ... toggled off
2105 //
2106 func_status::value_type InsetTabular::getStatus(string const & what) const
2107 {
2108         int action = LyXTabular::LAST_ACTION;
2109         func_status::value_type status = func_status::OK;
2110         
2111         int i = 0;
2112         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2113                 string const tmp = tabularFeature[i].feature;
2114                 if (tmp == what.substr(0, tmp.length())) {                  
2115                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2116                         //   tabularFeatures[i].feature.length())) {
2117                         action = tabularFeature[i].action;
2118                         break;
2119                 }
2120         }
2121         if (action == LyXTabular::LAST_ACTION)
2122                 return func_status::Unknown;
2123
2124         string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2125
2126         int sel_row_start;
2127         int sel_row_end;
2128         int dummy;
2129         LyXTabular::ltType dummyltt;
2130         bool flag = true;
2131
2132         if (hasSelection()) {
2133                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2134         } else {
2135                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2136         }
2137
2138         switch (action) {
2139         case LyXTabular::SET_PWIDTH:
2140         case LyXTabular::SET_MPWIDTH:
2141         case LyXTabular::SET_SPECIAL_COLUMN:
2142         case LyXTabular::SET_SPECIAL_MULTI:
2143                 status |= func_status::Disabled;
2144                 return status;
2145
2146         case LyXTabular::APPEND_ROW:
2147         case LyXTabular::APPEND_COLUMN:
2148         case LyXTabular::DELETE_ROW:
2149         case LyXTabular::DELETE_COLUMN:
2150         case LyXTabular::SET_ALL_LINES:
2151         case LyXTabular::UNSET_ALL_LINES:
2152                 status |= func_status::OK;
2153                 return status;
2154
2155         case LyXTabular::MULTICOLUMN:
2156                 if (tabular->IsMultiColumn(actcell))
2157                         status |= func_status::ToggleOn;
2158                 else
2159                         status |= func_status::ToggleOff;
2160                 break;
2161         case LyXTabular::M_TOGGLE_LINE_TOP:
2162                 flag = false;
2163         case LyXTabular::TOGGLE_LINE_TOP:
2164                 if (tabular->TopLine(actcell, flag))
2165                         status |= func_status::ToggleOn;
2166                 else
2167                         status |= func_status::ToggleOff;
2168                 break;
2169         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2170                 flag = false;
2171         case LyXTabular::TOGGLE_LINE_BOTTOM:
2172                 if (tabular->BottomLine(actcell, flag))
2173                         status |= func_status::ToggleOn;
2174                 else
2175                         status |= func_status::ToggleOff;
2176                 break;
2177         case LyXTabular::M_TOGGLE_LINE_LEFT:
2178                 flag = false;
2179         case LyXTabular::TOGGLE_LINE_LEFT:
2180                 if (tabular->LeftLine(actcell, flag))
2181                         status |= func_status::ToggleOn;
2182                 else
2183                         status |= func_status::ToggleOff;
2184                 break;
2185         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2186                 flag = false;
2187         case LyXTabular::TOGGLE_LINE_RIGHT:
2188                 if (tabular->RightLine(actcell, flag))
2189                         status |= func_status::ToggleOn;
2190                 else
2191                         status |= func_status::ToggleOff;
2192                 break;
2193         case LyXTabular::M_ALIGN_LEFT:
2194                 flag = false;
2195         case LyXTabular::ALIGN_LEFT:
2196                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2197                         status |= func_status::ToggleOn;
2198                 else
2199                         status |= func_status::ToggleOff;
2200                 break;
2201         case LyXTabular::M_ALIGN_RIGHT:
2202                 flag = false;
2203         case LyXTabular::ALIGN_RIGHT:
2204                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2205                         status |= func_status::ToggleOn;
2206                 else
2207                         status |= func_status::ToggleOff;
2208                 break;
2209         case LyXTabular::M_ALIGN_CENTER:
2210                 flag = false;
2211         case LyXTabular::ALIGN_CENTER:
2212                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2213                         status |= func_status::ToggleOn;
2214                 else
2215                         status |= func_status::ToggleOff;
2216                 break;
2217         case LyXTabular::M_VALIGN_TOP:
2218                 flag = false;
2219         case LyXTabular::VALIGN_TOP:
2220                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2221                         status |= func_status::ToggleOn;
2222                 else
2223                         status |= func_status::ToggleOff;
2224                 break;
2225         case LyXTabular::M_VALIGN_BOTTOM:
2226                 flag = false;
2227         case LyXTabular::VALIGN_BOTTOM:
2228                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2229                         status |= func_status::ToggleOn;
2230                 else
2231                         status |= func_status::ToggleOff;
2232                 break;
2233         case LyXTabular::M_VALIGN_CENTER:
2234                 flag = false;
2235         case LyXTabular::VALIGN_CENTER:
2236                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2237                         status |= func_status::ToggleOn;
2238                 else
2239                         status |= func_status::ToggleOff;
2240                 break;
2241         case LyXTabular::SET_LONGTABULAR:
2242                 if (tabular->IsLongTabular())
2243                         status |= func_status::ToggleOn;
2244                 else
2245                         status |= func_status::ToggleOff;
2246                 break;
2247         case LyXTabular::UNSET_LONGTABULAR:
2248                 if (!tabular->IsLongTabular())
2249                         status |= func_status::ToggleOn;
2250                 else
2251                         status |= func_status::ToggleOff;
2252                 break;
2253         case LyXTabular::SET_ROTATE_TABULAR:
2254                 if (tabular->GetRotateTabular())
2255                         status |= func_status::ToggleOn;
2256                 else
2257                         status |= func_status::ToggleOff;
2258                 break;
2259         case LyXTabular::UNSET_ROTATE_TABULAR:
2260                 if (!tabular->GetRotateTabular())
2261                         status |= func_status::ToggleOn;
2262                 else
2263                         status |= func_status::ToggleOff;
2264                 break;
2265         case LyXTabular::SET_ROTATE_CELL:
2266                 if (tabular->GetRotateCell(actcell))
2267                         status |= func_status::ToggleOn;
2268                 else
2269                         status |= func_status::ToggleOff;
2270                 break;
2271         case LyXTabular::UNSET_ROTATE_CELL:
2272                 if (!tabular->GetRotateCell(actcell))
2273                         status |= func_status::ToggleOn;
2274                 else
2275                         status |= func_status::ToggleOff;
2276                 break;
2277         case LyXTabular::SET_USEBOX:
2278                 if (strToInt(argument) == tabular->GetUsebox(actcell))
2279                         status |= func_status::ToggleOn;
2280                 else
2281                         status |= func_status::ToggleOff;
2282                 break;
2283         case LyXTabular::SET_LTFIRSTHEAD:
2284                 if (tabular->GetRowOfLTHead(actcell, dummyltt))
2285                         status |= func_status::ToggleOn;
2286                 else
2287                         status |= func_status::ToggleOff;
2288                 break;
2289         case LyXTabular::SET_LTHEAD:
2290                 if (tabular->GetRowOfLTHead(actcell, dummyltt))
2291                         status |= func_status::ToggleOn;
2292                 else
2293                         status |= func_status::ToggleOff;
2294                 break;
2295         case LyXTabular::SET_LTFOOT:
2296                 if (tabular->GetRowOfLTFoot(actcell, dummyltt))
2297                         status |= func_status::ToggleOn;
2298                 else
2299                         status |= func_status::ToggleOff;
2300                 break;
2301         case LyXTabular::SET_LTLASTFOOT:
2302                 if (tabular->GetRowOfLTFoot(actcell, dummyltt))
2303                         status |= func_status::ToggleOn;
2304                 else
2305                         status |= func_status::ToggleOff;
2306                 break;
2307         case LyXTabular::SET_LTNEWPAGE:
2308                 if (tabular->GetLTNewPage(actcell))
2309                         status |= func_status::ToggleOn;
2310                 else
2311                         status |= func_status::ToggleOff;
2312                 break;
2313         default:
2314                 status = func_status::Disabled;
2315                 break;
2316         }
2317         return status;
2318 }
2319
2320
2321 std::vector<string> const InsetTabular::getLabelList() const
2322 {
2323         return tabular->getLabelList();
2324 }
2325
2326
2327 bool InsetTabular::copySelection(BufferView * bv)
2328 {
2329         if (!hasSelection())
2330                 return false;
2331
2332         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2333         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2334         if (sel_col_start > sel_col_end) {
2335                 sel_col_start = sel_col_end;
2336                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2337         } else {
2338                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2339         }
2340         int const columns = sel_col_end - sel_col_start + 1;
2341
2342         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2343         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2344         if (sel_row_start > sel_row_end) {
2345                 swap(sel_row_start, sel_row_end);
2346         }
2347         int const rows = sel_row_end - sel_row_start + 1;
2348
2349         delete paste_tabular;
2350         paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2351         for (int i = 0; i < sel_row_start; ++i)
2352                 paste_tabular->DeleteRow(0);
2353         while(paste_tabular->rows() > rows)
2354                 paste_tabular->DeleteRow(rows);
2355         paste_tabular->SetTopLine(0, true, true);
2356         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2357                                      true, true);
2358         for (int i = 0; i < sel_col_start; ++i)
2359                 paste_tabular->DeleteColumn(0);
2360         while (paste_tabular->columns() > columns)
2361                 paste_tabular->DeleteColumn(columns);
2362         paste_tabular->SetLeftLine(0, true, true);
2363         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2364                                     true, true);
2365
2366         ostringstream sstr;
2367         paste_tabular->Ascii(bv->buffer(), sstr);
2368         bv->stuffClipboard(sstr.str().c_str());
2369         return true;
2370 }
2371
2372
2373 bool InsetTabular::pasteSelection(BufferView * bv)
2374 {
2375         if (!paste_tabular)
2376                 return false;
2377
2378         for (int r1 = 0, r2 = actrow;
2379              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2380              ++r1, ++r2) {
2381                 for(int c1 = 0, c2 = actcol;
2382                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2383                     ++c1, ++c2) {
2384                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2385                             tabular->IsPartOfMultiColumn(r2,c2))
2386                                 continue;
2387                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2388                                 --c2;
2389                                 continue;
2390                         }
2391                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2392                                 --c1;
2393                                 continue;
2394                         }
2395                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2396                         int const n2 = tabular->GetCellNumber(r2, c2);
2397                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2398                         tabular->GetCellInset(n2)->setOwner(this);
2399                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2400                 }
2401         }
2402         return true;
2403 }
2404
2405
2406 bool InsetTabular::cutSelection()
2407 {
2408         if (!hasSelection())
2409                 return false;
2410
2411         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2412         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2413         if (sel_col_start > sel_col_end) {
2414                 sel_col_start = sel_col_end;
2415                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2416         } else {
2417                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2418         }
2419         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2420         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2421         if (sel_row_start > sel_row_end) {
2422                 swap(sel_row_start, sel_row_end);
2423         }
2424         if (sel_cell_start > sel_cell_end) {
2425                 swap(sel_cell_start, sel_cell_end);
2426         }
2427         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2428                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2429                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2430                 }
2431         }
2432         return true;
2433 }
2434
2435
2436 bool InsetTabular::isRightToLeft(BufferView *bv )
2437 {
2438         return bv->getParentLanguage(this)->RightToLeft();
2439 }
2440
2441
2442 bool InsetTabular::nodraw() const
2443 {
2444         if (!UpdatableInset::nodraw() && the_locking_inset)
2445                 return the_locking_inset->nodraw();
2446         return UpdatableInset::nodraw();
2447 }
2448
2449
2450 int InsetTabular::scroll(bool recursive) const
2451 {
2452         int sx = UpdatableInset::scroll(false);
2453
2454         if (recursive && the_locking_inset)
2455                 sx += the_locking_inset->scroll(recursive);
2456
2457         return sx;
2458 }
2459
2460
2461 bool InsetTabular::doClearArea() const
2462 {
2463         return !locked || (need_update & (FULL|INIT));
2464 }
2465
2466
2467 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2468 {
2469                 srow = tabular->row_of_cell(sel_cell_start);
2470                 erow = tabular->row_of_cell(sel_cell_end);
2471                 if (srow > erow)
2472                         swap(srow, erow);
2473
2474                 scol = tabular->column_of_cell(sel_cell_start);
2475                 ecol = tabular->column_of_cell(sel_cell_end);
2476                 if (scol > ecol)
2477                         swap(scol, ecol);
2478                 else
2479                         ecol = tabular->right_column_of_cell(sel_cell_end);
2480 }
2481
2482
2483 Paragraph * InsetTabular::getParFromID(int id) const
2484 {
2485         Paragraph * result;
2486         for(int i = 0; i < tabular->rows(); ++i) {
2487                 for(int j = 0; j < tabular->columns(); ++j) {
2488                         if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2489                                 return result;
2490                 }
2491         }
2492         return 0;
2493 }
2494
2495
2496 Paragraph * InsetTabular::firstParagraph() const
2497 {
2498         if (the_locking_inset)
2499                 return the_locking_inset->firstParagraph();
2500         return 0;
2501 }
2502
2503
2504 Paragraph * InsetTabular::getFirstParagraph(int i) const
2505 {
2506         return (i < tabular->GetNumberOfCells())
2507                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2508                 : 0;
2509 }
2510
2511
2512 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2513 {
2514         if (the_locking_inset)
2515                 return the_locking_inset->cursor(bv);
2516         return Inset::cursor(bv);
2517 }
2518
2519
2520 Inset * InsetTabular::getInsetFromID(int id_arg) const
2521 {
2522         if (id_arg == id())
2523                 return const_cast<InsetTabular *>(this);
2524
2525         Inset * result;
2526         for(int i=0; i < tabular->rows(); ++i) {
2527                 for(int j=0; j < tabular->columns(); ++j) {
2528                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2529                                 return result;
2530                 }
2531         }
2532         return 0;
2533 }
2534
2535
2536 string const InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2537 {
2538         if (the_locking_inset) {
2539                 string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2540                 if (!str.empty())
2541                         return str;
2542                 if (tabular->IsLastCell(actcell)) {
2543                         bv->unlockInset(const_cast<InsetTabular *>(this));
2544                         return string();
2545                 }
2546                 ++actcell;
2547         }
2548         nodraw(true);
2549         // otherwise we have to lock the next inset and ask for it's selecttion
2550         UpdatableInset * inset =
2551                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2552         inset->edit(bv, 0,  0, 0);
2553         string const str(selectNextWordInt(bv, value));
2554         nodraw(false);
2555         if (!str.empty())
2556                 resetPos(bv);
2557         return str;
2558 }
2559
2560
2561 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2562 {
2563         // when entering this function the inset should be ALWAYS locked!
2564         lyx::Assert(the_locking_inset);
2565
2566         string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2567         if (!str.empty())
2568                 return str;
2569
2570         if (tabular->IsLastCell(actcell)) {
2571                 bv->unlockInset(const_cast<InsetTabular *>(this));
2572                 return string();
2573         }
2574         
2575         // otherwise we have to lock the next inset and ask for it's selecttion
2576         UpdatableInset * inset =
2577                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2578         inset->edit(bv);
2579         return selectNextWordInt(bv, value);
2580 }
2581
2582
2583 void InsetTabular::selectSelectedWord(BufferView * bv)
2584 {
2585         if (the_locking_inset) {
2586                 the_locking_inset->selectSelectedWord(bv);
2587                 return;
2588         }
2589         return;
2590 }
2591
2592
2593 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2594 {
2595         if (the_locking_inset) {
2596                 the_locking_inset->toggleSelection(bv, kill_selection);
2597         }
2598 }
2599
2600
2601 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2602                                  bool const & cs, bool const & mw)
2603 {
2604         if (the_locking_inset) {
2605                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2606                         return true;
2607                 if (tabular->IsLastCell(actcell)) {
2608                         bv->unlockInset(const_cast<InsetTabular *>(this));
2609                         return false;
2610                 }
2611                 ++actcell;
2612         }
2613         // otherwise we have to lock the next inset and search there
2614         UpdatableInset * inset =
2615                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2616         inset->edit(bv);
2617 #if 0
2618         bool const res = searchForward(bv, str, cs, mw);
2619         updateLocal(bv, NONE, false);
2620         bv->updateInset(const_cast<InsetTabular *>(this), false);
2621         return res;
2622 #else
2623         return searchForward(bv, str, cs, mw);
2624 #endif
2625 }
2626
2627
2628 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2629                                bool const & cs, bool const & mw)
2630 {
2631         if (the_locking_inset) {
2632                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2633                         return true;
2634                 if (!actcell) { // we are already in the first cell
2635                         bv->unlockInset(const_cast<InsetTabular *>(this));
2636                         return false;
2637                 }
2638                 --actcell;
2639         }
2640         // otherwise we have to lock the next inset and search there
2641         UpdatableInset * inset =
2642                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2643         inset->edit(bv, false);
2644 #if 0
2645         bool const res = searchBackward(bv, str, cs, mw);
2646         bv->updateInset(const_cast<InsetTabular *>(this), false);
2647         return res;
2648 #else
2649         return searchBackward(bv, str, cs, mw);
2650 #endif
2651 }
2652
2653
2654 bool InsetTabular::insetAllowed(Inset::Code code) const
2655 {
2656         if (the_locking_inset)
2657                 return the_locking_inset->insetAllowed(code);
2658         return false;
2659 }