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