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