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