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