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