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