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