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