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