]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
remove redundant code
[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).empty())
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                 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1692                 tabular->SetColumnPWidth(actcell,value);
1693                 if (update) {
1694                         for (int i = 0; i < tabular->rows(); ++i) {
1695                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1696                                         resizeLyXText(bv);
1697                         }
1698                         updateLocal(bv, INIT, true);
1699                 }
1700         }
1701         break;
1702         case LyXTabular::SET_MPWIDTH:
1703         {
1704                 bool const update = (tabular->GetPWidth(actcell) != value);
1705                 tabular->SetMColumnPWidth(actcell,value);
1706                 if (update) {
1707                         for (int i = 0; i < tabular->rows(); ++i) {
1708                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1709                                         resizeLyXText(bv);
1710                         }
1711                         updateLocal(bv, INIT, true);
1712                 }
1713         }
1714         break;
1715         case LyXTabular::SET_SPECIAL_COLUMN:
1716         case LyXTabular::SET_SPECIAL_MULTI:
1717                 tabular->SetAlignSpecial(actcell,value,feature);
1718                 updateLocal(bv, FULL, true);
1719                 break;
1720         case LyXTabular::APPEND_ROW:
1721                 // append the row into the tabular
1722                 unlockInsetInInset(bv, the_locking_inset);
1723                 tabular->AppendRow(actcell);
1724                 updateLocal(bv, INIT, true);
1725                 break;
1726         case LyXTabular::APPEND_COLUMN:
1727                 // append the column into the tabular
1728                 unlockInsetInInset(bv, the_locking_inset);
1729                 tabular->AppendColumn(actcell);
1730                 actcell = tabular->GetCellNumber(row, column);
1731                 updateLocal(bv, INIT, true);
1732                 break;
1733         case LyXTabular::DELETE_ROW:
1734                 unlockInsetInInset(bv, the_locking_inset);
1735                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1736                         tabular->DeleteRow(sel_row_start);
1737                 }
1738                 if (sel_row_start >= tabular->rows())
1739                         --sel_row_start;
1740                 actcell = tabular->GetCellNumber(sel_row_start, column);
1741                 clearSelection();
1742                 updateLocal(bv, INIT, true);
1743                 break;
1744         case LyXTabular::DELETE_COLUMN:
1745                 unlockInsetInInset(bv, the_locking_inset);
1746                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1747                         tabular->DeleteColumn(sel_col_start);
1748                 }
1749                 if (sel_col_start >= tabular->columns())
1750                         --sel_col_start;
1751                 actcell = tabular->GetCellNumber(row, sel_col_start);
1752                 clearSelection();
1753                 updateLocal(bv, INIT, true);
1754                 break;
1755         case LyXTabular::M_TOGGLE_LINE_TOP:
1756                 flag = false;
1757         case LyXTabular::TOGGLE_LINE_TOP:
1758         {
1759                 bool lineSet = !tabular->TopLine(actcell, flag);
1760                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1761                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1762                                 tabular->SetTopLine(
1763                                         tabular->GetCellNumber(i, j),
1764                                         lineSet, flag);
1765                 updateLocal(bv, INIT, true);
1766                 break;
1767         }
1768         
1769         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1770                 flag = false;
1771         case LyXTabular::TOGGLE_LINE_BOTTOM:
1772         {
1773                 bool lineSet = !tabular->BottomLine(actcell, flag); 
1774                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1775                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1776                                 tabular->SetBottomLine(
1777                                         tabular->GetCellNumber(i, j),
1778                                         lineSet,
1779                                         flag);
1780                 updateLocal(bv, INIT, true);
1781                 break;
1782         }
1783         
1784         case LyXTabular::M_TOGGLE_LINE_LEFT:
1785                 flag = false;
1786         case LyXTabular::TOGGLE_LINE_LEFT:
1787         {
1788                 bool lineSet = !tabular->LeftLine(actcell, flag);
1789                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1790                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1791                                 tabular->SetLeftLine(
1792                                         tabular->GetCellNumber(i,j),
1793                                         lineSet,
1794                                         flag);
1795                 updateLocal(bv, INIT, true);
1796                 break;
1797         }
1798         
1799         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1800                 flag = false;
1801         case LyXTabular::TOGGLE_LINE_RIGHT:
1802         {
1803                 bool lineSet = !tabular->RightLine(actcell, flag);
1804                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1805                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1806                                 tabular->SetRightLine(
1807                                         tabular->GetCellNumber(i,j),
1808                                         lineSet,
1809                                         flag);
1810                 updateLocal(bv, INIT, true);
1811                 break;
1812         }
1813         
1814         case LyXTabular::M_ALIGN_LEFT:
1815         case LyXTabular::M_ALIGN_RIGHT:
1816         case LyXTabular::M_ALIGN_CENTER:
1817                 flag = false;
1818         case LyXTabular::ALIGN_LEFT:
1819         case LyXTabular::ALIGN_RIGHT:
1820         case LyXTabular::ALIGN_CENTER:
1821                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1822                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1823                                 tabular->SetAlignment(
1824                                         tabular->GetCellNumber(i, j),
1825                                         setAlign,
1826                                         flag);
1827                 updateLocal(bv, INIT, true);
1828                 break;
1829         case LyXTabular::M_VALIGN_TOP:
1830         case LyXTabular::M_VALIGN_BOTTOM:
1831         case LyXTabular::M_VALIGN_CENTER:
1832                 flag = false;
1833         case LyXTabular::VALIGN_TOP:
1834         case LyXTabular::VALIGN_BOTTOM:
1835         case LyXTabular::VALIGN_CENTER:
1836                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1837                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1838                                 tabular->SetVAlignment(
1839                                         tabular->GetCellNumber(i, j),
1840                                         setVAlign, flag);
1841                 updateLocal(bv, INIT, true);
1842                 break;
1843         case LyXTabular::MULTICOLUMN:
1844         {
1845                 if (sel_row_start != sel_row_end) {
1846                         Alert::alert(_("Impossible Operation!"), 
1847                                    _("Multicolumns can only be horizontally."), 
1848                                    _("Sorry."));
1849                         return;
1850                 }
1851                 // just multicol for one Single Cell
1852                 if (!hasSelection()) {
1853                         // check wether we are completly in a multicol
1854                         if (tabular->IsMultiColumn(actcell)) {
1855                                 tabular->UnsetMultiColumn(actcell);
1856                                 updateLocal(bv, INIT, true);
1857                         } else {
1858                                 tabular->SetMultiColumn(actcell, 1);
1859                                 updateLocal(bv, CELL, true);
1860                         }
1861                         return;
1862                 }
1863                 // we have a selection so this means we just add all this
1864                 // cells to form a multicolumn cell
1865                 int s_start;
1866                 int s_end;
1867
1868                 if (sel_cell_start > sel_cell_end) {
1869                         s_start = sel_cell_end;
1870                         s_end = sel_cell_start;
1871                 } else {
1872                         s_start = sel_cell_start;
1873                         s_end = sel_cell_end;
1874                 }
1875                 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1876                 actcell = s_start;
1877                 sel_cell_end = sel_cell_start;
1878                 updateLocal(bv, INIT, true);
1879                 break;
1880         }
1881         case LyXTabular::SET_ALL_LINES:
1882                 setLines = true;
1883         case LyXTabular::UNSET_ALL_LINES:
1884                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1885                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1886                                 tabular->SetAllLines(
1887                                         tabular->GetCellNumber(i,j), setLines);
1888                 updateLocal(bv, INIT, true);
1889                 break;
1890         case LyXTabular::SET_LONGTABULAR:
1891                 tabular->SetLongTabular(true);
1892                 updateLocal(bv, INIT, true); // because this toggles displayed
1893                 break;
1894         case LyXTabular::UNSET_LONGTABULAR:
1895                 tabular->SetLongTabular(false);
1896                 updateLocal(bv, INIT, true); // because this toggles displayed
1897                 break;
1898         case LyXTabular::SET_ROTATE_TABULAR:
1899                 tabular->SetRotateTabular(true);
1900                 break;
1901         case LyXTabular::UNSET_ROTATE_TABULAR:
1902                 tabular->SetRotateTabular(false);
1903                 break;
1904         case LyXTabular::SET_ROTATE_CELL:
1905                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1906                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1907                                 tabular->SetRotateCell(
1908                                         tabular->GetCellNumber(i, j),
1909                                         true);
1910                 break;
1911         case LyXTabular::UNSET_ROTATE_CELL:
1912                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1913                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1914                                 tabular->SetRotateCell(
1915                                         tabular->GetCellNumber(i, j), false);
1916                 break;
1917         case LyXTabular::SET_USEBOX:
1918         {
1919                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1920                 if (val == tabular->GetUsebox(actcell))
1921                         val = LyXTabular::BOX_NONE;
1922                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1923                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1924                                 tabular->SetUsebox(
1925                                         tabular->GetCellNumber(i, j), val);
1926                 break;
1927         }
1928         case LyXTabular::UNSET_LTFIRSTHEAD:
1929                 ltt.row = 0;
1930         case LyXTabular::SET_LTFIRSTHEAD:
1931                 tabular->SetLTHead(ltt, true);
1932                 break;
1933         case LyXTabular::UNSET_LTHEAD:
1934                 ltt.row = 0;
1935         case LyXTabular::SET_LTHEAD:
1936                 tabular->SetLTHead(ltt, false);
1937                 break;
1938         case LyXTabular::UNSET_LTFOOT:
1939                 ltt.row = 0;
1940         case LyXTabular::SET_LTFOOT:
1941                 tabular->SetLTFoot(ltt, false);
1942                 break;
1943         case LyXTabular::UNSET_LTLASTFOOT:
1944                 ltt.row = 0;
1945         case LyXTabular::SET_LTLASTFOOT:
1946                 tabular->SetLTFoot(ltt, true);
1947                 break;
1948         case LyXTabular::SET_LTNEWPAGE:
1949         {
1950                 bool what = !tabular->GetLTNewPage(actcell);
1951                 tabular->SetLTNewPage(actcell, what);
1952                 break;
1953         }
1954         // dummy stuff just to avoid warnings
1955         case LyXTabular::LAST_ACTION:
1956                 break;
1957         }
1958 }
1959
1960
1961 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
1962                                      bool behind)
1963 {
1964         UpdatableInset * inset =
1965                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1966         LyXFont font(LyXFont::ALL_SANE);
1967         if (behind) {
1968                 x = inset->x() + inset->width(bv, font);
1969                 y = inset->descent(bv, font);
1970         }
1971         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1972         //inset_y = cursor.y();
1973         inset->edit(bv, x,  y, button);
1974         if (!the_locking_inset)
1975                 return false;
1976         updateLocal(bv, CELL, false);
1977         return (the_locking_inset != 0);
1978 }
1979
1980
1981 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1982                                         int button)
1983 {
1984         inset_x = cursor_.x()
1985                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1986         inset_y = cursor_.y();
1987         return activateCellInset(bv, x - inset_x, y - inset_y, button);
1988 }
1989
1990
1991 bool InsetTabular::insetHit(BufferView *, int x, int) const
1992 {
1993         return (x + top_x)
1994                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
1995 }
1996
1997
1998 // This returns paperWidth() if the cell-width is unlimited or the width
1999 // in pixels if we have a pwidth for this cell.
2000 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2001 {
2002         string const s = tabular->GetPWidth(cell);
2003         
2004         if (s.empty())
2005                 return -1;
2006         return VSpace(s).inPixels(bv);
2007 }
2008
2009
2010 int InsetTabular::getMaxWidth(BufferView * bv,
2011                               UpdatableInset const * inset) const
2012 {
2013         int cell = tabular->cur_cell;
2014         if (tabular->GetCellInset(cell) != inset) {
2015                 cell = actcell;
2016                 if (tabular->GetCellInset(cell) != inset) {
2017                         
2018                         lyxerr[Debug::INSETTEXT] << "Actcell not equal to actual cell!\n";
2019                         cell = -1;
2020                 }
2021         }
2022         
2023         int const n = tabular->GetNumberOfCells();
2024
2025         if (cell == -1) {
2026                 for (cell = 0; cell < n; ++cell) {
2027                         if (tabular->GetCellInset(cell) == inset)
2028                                 break;
2029                 }
2030         }
2031         
2032         if (cell >= n) {
2033                 lyxerr << "Own inset not found, shouldn't really happen!\n";
2034                 return -1;
2035         }
2036         
2037         int w = getMaxWidthOfCell(bv, cell);
2038         if (w > 0) {
2039                 // because the inset then subtracts it's top_x and owner->x()
2040                 w += (inset->x() - top_x);
2041         }
2042         
2043         return w;
2044 }
2045
2046
2047 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2048 {
2049         resizeLyXText(bv, recursive);
2050 }
2051
2052
2053 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2054 {
2055         if (force) {
2056                 for(int i = 0; i < tabular->rows(); ++i) {
2057                         for(int j = 0; j < tabular->columns(); ++j) {
2058                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2059                         }
2060                 }
2061         }
2062         need_update = FULL;
2063 }
2064
2065
2066 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2067                                    bool const recursive) const
2068 {
2069         if (the_locking_inset)
2070                 return the_locking_inset->getLyXText(bv, recursive);
2071 #if 0
2072         // if we're locked lock the actual insettext and return it's LyXText!!!
2073         if (locked) {
2074                 UpdatableInset * inset =
2075                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2076                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2077                 return the_locking_inset->getLyXText(bv, recursive);
2078         }
2079 #endif
2080         return Inset::getLyXText(bv, recursive);
2081 }
2082
2083
2084 bool InsetTabular::showInsetDialog(BufferView * bv) const
2085 {
2086         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2087                 bv->owner()->getDialogs()
2088                         ->showTabular(const_cast<InsetTabular *>(this));
2089         return true;
2090 }
2091
2092
2093 void InsetTabular::openLayoutDialog(BufferView * bv) const
2094 {
2095         if (the_locking_inset) {
2096                 InsetTabular * i = static_cast<InsetTabular *>
2097                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2098                 if (i) {
2099                         i->openLayoutDialog(bv);
2100                         return;
2101                 }
2102         }
2103         bv->owner()->getDialogs()->showTabular(
2104                 const_cast<InsetTabular *>(this));
2105 }
2106
2107
2108 //
2109 // functions returns:
2110 // 0 ... disabled
2111 // 1 ... enabled
2112 // 2 ... toggled on
2113 // 3 ... toggled off
2114 //
2115 func_status::value_type InsetTabular::getStatus(string const & what) const
2116 {
2117         int action = LyXTabular::LAST_ACTION;
2118         func_status::value_type status = func_status::OK;
2119         
2120         int i = 0;
2121         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2122                 string const tmp = tabularFeature[i].feature;
2123                 if (tmp == what.substr(0, tmp.length())) {                  
2124                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2125                         //   tabularFeatures[i].feature.length())) {
2126                         action = tabularFeature[i].action;
2127                         break;
2128                 }
2129         }
2130         if (action == LyXTabular::LAST_ACTION)
2131                 return func_status::Unknown;
2132
2133         string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2134
2135         int sel_row_start;
2136         int sel_row_end;
2137         int dummy;
2138         LyXTabular::ltType dummyltt;
2139         bool flag = true;
2140
2141         if (hasSelection()) {
2142                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2143         } else {
2144                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2145         }
2146
2147         switch (action) {
2148         case LyXTabular::SET_PWIDTH:
2149         case LyXTabular::SET_MPWIDTH:
2150         case LyXTabular::SET_SPECIAL_COLUMN:
2151         case LyXTabular::SET_SPECIAL_MULTI:
2152                 status |= func_status::Disabled;
2153                 return status;
2154
2155         case LyXTabular::APPEND_ROW:
2156         case LyXTabular::APPEND_COLUMN:
2157         case LyXTabular::DELETE_ROW:
2158         case LyXTabular::DELETE_COLUMN:
2159         case LyXTabular::SET_ALL_LINES:
2160         case LyXTabular::UNSET_ALL_LINES:
2161                 status |= func_status::OK;
2162                 return status;
2163
2164         case LyXTabular::MULTICOLUMN:
2165                 if (tabular->IsMultiColumn(actcell))
2166                         status |= func_status::ToggleOn;
2167                 else
2168                         status |= func_status::ToggleOff;
2169                 break;
2170         case LyXTabular::M_TOGGLE_LINE_TOP:
2171                 flag = false;
2172         case LyXTabular::TOGGLE_LINE_TOP:
2173                 if (tabular->TopLine(actcell, flag))
2174                         status |= func_status::ToggleOn;
2175                 else
2176                         status |= func_status::ToggleOff;
2177                 break;
2178         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2179                 flag = false;
2180         case LyXTabular::TOGGLE_LINE_BOTTOM:
2181                 if (tabular->BottomLine(actcell, flag))
2182                         status |= func_status::ToggleOn;
2183                 else
2184                         status |= func_status::ToggleOff;
2185                 break;
2186         case LyXTabular::M_TOGGLE_LINE_LEFT:
2187                 flag = false;
2188         case LyXTabular::TOGGLE_LINE_LEFT:
2189                 if (tabular->LeftLine(actcell, flag))
2190                         status |= func_status::ToggleOn;
2191                 else
2192                         status |= func_status::ToggleOff;
2193                 break;
2194         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2195                 flag = false;
2196         case LyXTabular::TOGGLE_LINE_RIGHT:
2197                 if (tabular->RightLine(actcell, flag))
2198                         status |= func_status::ToggleOn;
2199                 else
2200                         status |= func_status::ToggleOff;
2201                 break;
2202         case LyXTabular::M_ALIGN_LEFT:
2203                 flag = false;
2204         case LyXTabular::ALIGN_LEFT:
2205                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2206                         status |= func_status::ToggleOn;
2207                 else
2208                         status |= func_status::ToggleOff;
2209                 break;
2210         case LyXTabular::M_ALIGN_RIGHT:
2211                 flag = false;
2212         case LyXTabular::ALIGN_RIGHT:
2213                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2214                         status |= func_status::ToggleOn;
2215                 else
2216                         status |= func_status::ToggleOff;
2217                 break;
2218         case LyXTabular::M_ALIGN_CENTER:
2219                 flag = false;
2220         case LyXTabular::ALIGN_CENTER:
2221                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2222                         status |= func_status::ToggleOn;
2223                 else
2224                         status |= func_status::ToggleOff;
2225                 break;
2226         case LyXTabular::M_VALIGN_TOP:
2227                 flag = false;
2228         case LyXTabular::VALIGN_TOP:
2229                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2230                         status |= func_status::ToggleOn;
2231                 else
2232                         status |= func_status::ToggleOff;
2233                 break;
2234         case LyXTabular::M_VALIGN_BOTTOM:
2235                 flag = false;
2236         case LyXTabular::VALIGN_BOTTOM:
2237                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2238                         status |= func_status::ToggleOn;
2239                 else
2240                         status |= func_status::ToggleOff;
2241                 break;
2242         case LyXTabular::M_VALIGN_CENTER:
2243                 flag = false;
2244         case LyXTabular::VALIGN_CENTER:
2245                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2246                         status |= func_status::ToggleOn;
2247                 else
2248                         status |= func_status::ToggleOff;
2249                 break;
2250         case LyXTabular::SET_LONGTABULAR:
2251                 if (tabular->IsLongTabular())
2252                         status |= func_status::ToggleOn;
2253                 else
2254                         status |= func_status::ToggleOff;
2255                 break;
2256         case LyXTabular::UNSET_LONGTABULAR:
2257                 if (!tabular->IsLongTabular())
2258                         status |= func_status::ToggleOn;
2259                 else
2260                         status |= func_status::ToggleOff;
2261                 break;
2262         case LyXTabular::SET_ROTATE_TABULAR:
2263                 if (tabular->GetRotateTabular())
2264                         status |= func_status::ToggleOn;
2265                 else
2266                         status |= func_status::ToggleOff;
2267                 break;
2268         case LyXTabular::UNSET_ROTATE_TABULAR:
2269                 if (!tabular->GetRotateTabular())
2270                         status |= func_status::ToggleOn;
2271                 else
2272                         status |= func_status::ToggleOff;
2273                 break;
2274         case LyXTabular::SET_ROTATE_CELL:
2275                 if (tabular->GetRotateCell(actcell))
2276                         status |= func_status::ToggleOn;
2277                 else
2278                         status |= func_status::ToggleOff;
2279                 break;
2280         case LyXTabular::UNSET_ROTATE_CELL:
2281                 if (!tabular->GetRotateCell(actcell))
2282                         status |= func_status::ToggleOn;
2283                 else
2284                         status |= func_status::ToggleOff;
2285                 break;
2286         case LyXTabular::SET_USEBOX:
2287                 if (strToInt(argument) == tabular->GetUsebox(actcell))
2288                         status |= func_status::ToggleOn;
2289                 else
2290                         status |= func_status::ToggleOff;
2291                 break;
2292         case LyXTabular::SET_LTFIRSTHEAD:
2293                 if (tabular->GetRowOfLTHead(actcell, dummyltt))
2294                         status |= func_status::ToggleOn;
2295                 else
2296                         status |= func_status::ToggleOff;
2297                 break;
2298         case LyXTabular::SET_LTHEAD:
2299                 if (tabular->GetRowOfLTHead(actcell, dummyltt))
2300                         status |= func_status::ToggleOn;
2301                 else
2302                         status |= func_status::ToggleOff;
2303                 break;
2304         case LyXTabular::SET_LTFOOT:
2305                 if (tabular->GetRowOfLTFoot(actcell, dummyltt))
2306                         status |= func_status::ToggleOn;
2307                 else
2308                         status |= func_status::ToggleOff;
2309                 break;
2310         case LyXTabular::SET_LTLASTFOOT:
2311                 if (tabular->GetRowOfLTFoot(actcell, dummyltt))
2312                         status |= func_status::ToggleOn;
2313                 else
2314                         status |= func_status::ToggleOff;
2315                 break;
2316         case LyXTabular::SET_LTNEWPAGE:
2317                 if (tabular->GetLTNewPage(actcell))
2318                         status |= func_status::ToggleOn;
2319                 else
2320                         status |= func_status::ToggleOff;
2321                 break;
2322         default:
2323                 status = func_status::Disabled;
2324                 break;
2325         }
2326         return status;
2327 }
2328
2329
2330 std::vector<string> const InsetTabular::getLabelList() const
2331 {
2332         return tabular->getLabelList();
2333 }
2334
2335
2336 bool InsetTabular::copySelection(BufferView * bv)
2337 {
2338         if (!hasSelection())
2339                 return false;
2340
2341         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2342         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2343         if (sel_col_start > sel_col_end) {
2344                 sel_col_start = sel_col_end;
2345                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2346         } else {
2347                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2348         }
2349         int const columns = sel_col_end - sel_col_start + 1;
2350
2351         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2352         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2353         if (sel_row_start > sel_row_end) {
2354                 swap(sel_row_start, sel_row_end);
2355         }
2356         int const rows = sel_row_end - sel_row_start + 1;
2357
2358         delete paste_tabular;
2359         paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2360         for (int i = 0; i < sel_row_start; ++i)
2361                 paste_tabular->DeleteRow(0);
2362         while (paste_tabular->rows() > rows)
2363                 paste_tabular->DeleteRow(rows);
2364         paste_tabular->SetTopLine(0, true, true);
2365         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2366                                      true, true);
2367         for (int i = 0; i < sel_col_start; ++i)
2368                 paste_tabular->DeleteColumn(0);
2369         while (paste_tabular->columns() > columns)
2370                 paste_tabular->DeleteColumn(columns);
2371         paste_tabular->SetLeftLine(0, true, true);
2372         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2373                                     true, true);
2374
2375         ostringstream sstr;
2376         paste_tabular->Ascii(bv->buffer(), sstr);
2377         bv->stuffClipboard(sstr.str().c_str());
2378         return true;
2379 }
2380
2381
2382 bool InsetTabular::pasteSelection(BufferView * bv)
2383 {
2384         if (!paste_tabular)
2385                 return false;
2386
2387         for (int r1 = 0, r2 = actrow;
2388              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2389              ++r1, ++r2) {
2390                 for(int c1 = 0, c2 = actcol;
2391                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2392                     ++c1, ++c2) {
2393                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2394                             tabular->IsPartOfMultiColumn(r2,c2))
2395                                 continue;
2396                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2397                                 --c2;
2398                                 continue;
2399                         }
2400                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2401                                 --c1;
2402                                 continue;
2403                         }
2404                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2405                         int const n2 = tabular->GetCellNumber(r2, c2);
2406                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2407                         tabular->GetCellInset(n2)->setOwner(this);
2408                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2409                 }
2410         }
2411         return true;
2412 }
2413
2414
2415 bool InsetTabular::cutSelection()
2416 {
2417         if (!hasSelection())
2418                 return false;
2419
2420         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2421         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2422         if (sel_col_start > sel_col_end) {
2423                 sel_col_start = sel_col_end;
2424                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2425         } else {
2426                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2427         }
2428         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2429         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2430         if (sel_row_start > sel_row_end) {
2431                 swap(sel_row_start, sel_row_end);
2432         }
2433         if (sel_cell_start > sel_cell_end) {
2434                 swap(sel_cell_start, sel_cell_end);
2435         }
2436         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2437                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2438                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2439                 }
2440         }
2441         return true;
2442 }
2443
2444
2445 bool InsetTabular::isRightToLeft(BufferView *bv )
2446 {
2447         return bv->getParentLanguage(this)->RightToLeft();
2448 }
2449
2450
2451 bool InsetTabular::nodraw() const
2452 {
2453         if (!UpdatableInset::nodraw() && the_locking_inset)
2454                 return the_locking_inset->nodraw();
2455         return UpdatableInset::nodraw();
2456 }
2457
2458
2459 int InsetTabular::scroll(bool recursive) const
2460 {
2461         int sx = UpdatableInset::scroll(false);
2462
2463         if (recursive && the_locking_inset)
2464                 sx += the_locking_inset->scroll(recursive);
2465
2466         return sx;
2467 }
2468
2469
2470 bool InsetTabular::doClearArea() const
2471 {
2472         return !locked || (need_update & (FULL|INIT));
2473 }
2474
2475
2476 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2477 {
2478                 srow = tabular->row_of_cell(sel_cell_start);
2479                 erow = tabular->row_of_cell(sel_cell_end);
2480                 if (srow > erow)
2481                         swap(srow, erow);
2482
2483                 scol = tabular->column_of_cell(sel_cell_start);
2484                 ecol = tabular->column_of_cell(sel_cell_end);
2485                 if (scol > ecol)
2486                         swap(scol, ecol);
2487                 else
2488                         ecol = tabular->right_column_of_cell(sel_cell_end);
2489 }
2490
2491
2492 Paragraph * InsetTabular::getParFromID(int id) const
2493 {
2494         Paragraph * result;
2495         for(int i = 0; i < tabular->rows(); ++i) {
2496                 for(int j = 0; j < tabular->columns(); ++j) {
2497                         if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2498                                 return result;
2499                 }
2500         }
2501         return 0;
2502 }
2503
2504
2505 Paragraph * InsetTabular::firstParagraph() const
2506 {
2507         if (the_locking_inset)
2508                 return the_locking_inset->firstParagraph();
2509         return 0;
2510 }
2511
2512
2513 Paragraph * InsetTabular::getFirstParagraph(int i) const
2514 {
2515         return (i < tabular->GetNumberOfCells())
2516                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2517                 : 0;
2518 }
2519
2520
2521 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2522 {
2523         if (the_locking_inset)
2524                 return the_locking_inset->cursor(bv);
2525         return Inset::cursor(bv);
2526 }
2527
2528
2529 Inset * InsetTabular::getInsetFromID(int id_arg) const
2530 {
2531         if (id_arg == id())
2532                 return const_cast<InsetTabular *>(this);
2533
2534         Inset * result;
2535         for(int i=0; i < tabular->rows(); ++i) {
2536                 for(int j=0; j < tabular->columns(); ++j) {
2537                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2538                                 return result;
2539                 }
2540         }
2541         return 0;
2542 }
2543
2544
2545 string const InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2546 {
2547         if (the_locking_inset) {
2548                 string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2549                 if (!str.empty())
2550                         return str;
2551                 if (tabular->IsLastCell(actcell)) {
2552                         bv->unlockInset(const_cast<InsetTabular *>(this));
2553                         return string();
2554                 }
2555                 ++actcell;
2556         }
2557         nodraw(true);
2558         // otherwise we have to lock the next inset and ask for it's selecttion
2559         UpdatableInset * inset =
2560                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2561         inset->edit(bv, 0,  0, 0);
2562         string const str(selectNextWordInt(bv, value));
2563         nodraw(false);
2564         if (!str.empty())
2565                 resetPos(bv);
2566         return str;
2567 }
2568
2569
2570 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2571 {
2572         // when entering this function the inset should be ALWAYS locked!
2573         lyx::Assert(the_locking_inset);
2574
2575         string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2576         if (!str.empty())
2577                 return str;
2578
2579         if (tabular->IsLastCell(actcell)) {
2580                 bv->unlockInset(const_cast<InsetTabular *>(this));
2581                 return string();
2582         }
2583         
2584         // otherwise we have to lock the next inset and ask for it's selecttion
2585         UpdatableInset * inset =
2586                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2587         inset->edit(bv);
2588         return selectNextWordInt(bv, value);
2589 }
2590
2591
2592 void InsetTabular::selectSelectedWord(BufferView * bv)
2593 {
2594         if (the_locking_inset) {
2595                 the_locking_inset->selectSelectedWord(bv);
2596                 return;
2597         }
2598         return;
2599 }
2600
2601
2602 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2603 {
2604         if (the_locking_inset) {
2605                 the_locking_inset->toggleSelection(bv, kill_selection);
2606         }
2607 }
2608
2609
2610 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2611                                  bool const & cs, bool const & mw)
2612 {
2613         if (the_locking_inset) {
2614                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2615                         return true;
2616                 if (tabular->IsLastCell(actcell)) {
2617                         bv->unlockInset(const_cast<InsetTabular *>(this));
2618                         return false;
2619                 }
2620                 ++actcell;
2621         }
2622         // otherwise we have to lock the next inset and search there
2623         UpdatableInset * inset =
2624                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2625         inset->edit(bv);
2626 #if 0
2627         bool const res = searchForward(bv, str, cs, mw);
2628         updateLocal(bv, NONE, false);
2629         bv->updateInset(const_cast<InsetTabular *>(this), false);
2630         return res;
2631 #else
2632         return searchForward(bv, str, cs, mw);
2633 #endif
2634 }
2635
2636
2637 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2638                                bool const & cs, bool const & mw)
2639 {
2640         if (the_locking_inset) {
2641                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2642                         return true;
2643                 if (!actcell) { // we are already in the first cell
2644                         bv->unlockInset(const_cast<InsetTabular *>(this));
2645                         return false;
2646                 }
2647                 --actcell;
2648         }
2649         // otherwise we have to lock the next inset and search there
2650         UpdatableInset * inset =
2651                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2652         inset->edit(bv, false);
2653 #if 0
2654         bool const res = searchBackward(bv, str, cs, mw);
2655         bv->updateInset(const_cast<InsetTabular *>(this), false);
2656         return res;
2657 #else
2658         return searchBackward(bv, str, cs, mw);
2659 #endif
2660 }
2661
2662
2663 bool InsetTabular::insetAllowed(Inset::Code code) const
2664 {
2665         if (the_locking_inset)
2666                 return the_locking_inset->insetAllowed(code);
2667         return false;
2668 }