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