]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Clear selection on PgUp/Down in tabulars (fix #216).
[lyx.git] / src / insets / insettabular.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insettabular.h"
18
19 #include "buffer.h"
20 #include "commandtags.h"
21 #include "lyxfunc.h"
22 #include "debug.h"
23 #include "LaTeXFeatures.h"
24 #include "Painter.h"
25 #include "font.h"
26 #include "lyxtext.h"
27 #include "lyx_gui_misc.h"
28 #include "LyXView.h"
29 #include "insets/insettext.h"
30 #include "debug.h"
31 #include "WorkArea.h"
32 #include "gettext.h"
33 #include "language.h"
34 #include "BufferView.h"
35 #include "undo_funcs.h"
36 #include "lyxlength.h"
37 #include "ParagraphParameters.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/Alert.h"
41
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
44
45 #include <fstream>
46 #include <algorithm>
47 #include <cstdlib>
48 #include <map>
49 //#include <signal.h>
50
51
52 using std::vector;
53 using std::ostream;
54 using std::ifstream;
55 using std::max;
56 using std::endl;
57 using std::swap;
58 using std::max;
59
60 namespace {
61
62 int const ADD_TO_HEIGHT = 2;
63 int const ADD_TO_TABULAR_WIDTH = 2;
64
65 ///
66 LyXTabular * paste_tabular = 0;
67
68
69 struct TabularFeature {
70         LyXTabular::Feature action;
71         string feature;
72 };
73
74
75 TabularFeature tabularFeature[] =
76 {
77         { LyXTabular::APPEND_ROW, "append-row" },
78         { LyXTabular::APPEND_COLUMN, "append-column" },
79         { LyXTabular::DELETE_ROW, "delete-row" },
80         { LyXTabular::DELETE_COLUMN, "delete-column" },
81         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
82         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
83         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
84         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
85         { LyXTabular::ALIGN_LEFT, "align-left" },
86         { LyXTabular::ALIGN_RIGHT, "align-right" },
87         { LyXTabular::ALIGN_CENTER, "align-center" },
88         { LyXTabular::VALIGN_TOP, "valign-top" },
89         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
90         { LyXTabular::VALIGN_CENTER, "valign-center" },
91         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
92         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
93         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
94         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
95         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
96         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
97         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
98         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
99         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
100         { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
101         { LyXTabular::MULTICOLUMN, "multicolumn" },
102         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
103         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
104         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
105         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
106         { LyXTabular::SET_PWIDTH, "set-pwidth" },
107         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
108         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
109         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
110         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
111         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
112         { LyXTabular::SET_USEBOX, "set-usebox" },
113         { LyXTabular::SET_LTHEAD, "set-lthead" },
114         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
115         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
116         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
117         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
118         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
119         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
120         { LyXTabular::LAST_ACTION, "" }
121 };
122
123 } // namespace anon
124
125
126 bool InsetTabular::hasPasteBuffer() const
127 {
128         return (paste_tabular != 0);
129 }
130
131
132 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
133         : buffer(&buf)
134 {
135         if (rows <= 0)
136                 rows = 1;
137         if (columns <= 0)
138                 columns = 1;
139         tabular.reset(new LyXTabular(this, rows, columns));
140         // for now make it always display as display() inset
141         // just for test!!!
142         the_locking_inset = 0;
143         old_locking_inset = 0;
144         locked = false;
145         oldcell = -1;
146         actrow = actcell = 0;
147         clearSelection();
148         need_update = INIT;
149         in_update = false;
150         in_reset_pos = false;
151         inset_x = 0;
152         inset_y = 0;
153 }
154
155
156 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
157                                                    bool same_id)
158         : UpdatableInset(tab, same_id), buffer(&buf)
159 {
160         tabular.reset(new LyXTabular(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 + bv->painter().paperHeight() <
1042                     (top_baseline + tabular->GetHeightOfTabular()))
1043                         {
1044                                 bv->scrollCB(bv->text->first + 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 - 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(this, rows, maxCols);
1153                         string::size_type op = 0;
1154                         int cell = 0;
1155                         int cells = paste_tabular->GetNumberOfCells();
1156                         p = cols = 0;
1157                         while ((cell < cells) && (p < len) &&
1158                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1159                                 if (p >= len)
1160                                         break;
1161                                 switch (clip[p]) {
1162                                 case '\t':
1163                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1164                                         ++cols;
1165                                         ++cell;
1166                                         break;
1167                                 case '\n':
1168                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1169                                         while (cols++ < maxCols)
1170                                                 ++cell;
1171                                         cols = 0;
1172                                         break;
1173                                 }
1174                                 ++p;
1175                                 op = p;
1176                         }
1177                         // check for the last cell if there is no trailing '\n'
1178                         if ((cell < cells) && (op < len))
1179                                 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1180                 } else {
1181                         // so that the clipboard is used and it goes on
1182                         // to default
1183                         // and executes LFUN_PASTESELECTION in insettext!
1184                         delete paste_tabular;
1185                         paste_tabular = 0;
1186                 }
1187         }
1188         case LFUN_PASTE:
1189                 if (hasPasteBuffer()) {
1190                         setUndo(bv, Undo::INSERT,
1191                                 bv->text->cursor.par(),
1192                                 bv->text->cursor.par()->next());
1193                         pasteSelection(bv);
1194                         updateLocal(bv, INIT, true);
1195                         break;
1196                 }
1197                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1198         default:
1199                 // handle font changing stuff on selection before we lock the inset
1200                 // in the default part!
1201                 result = UNDISPATCHED;
1202                 if (hs) {
1203                         switch(action) {
1204                         case LFUN_LANGUAGE:
1205                         case LFUN_EMPH:
1206                         case LFUN_BOLD:
1207                         case LFUN_NOUN:
1208                         case LFUN_CODE:
1209                         case LFUN_SANS:
1210                         case LFUN_ROMAN:
1211                         case LFUN_DEFAULT:
1212                         case LFUN_UNDERLINE:
1213                         case LFUN_FONT_SIZE:
1214                                 if (bv->Dispatch(action, arg))
1215                                         result = DISPATCHED;
1216                                 break;
1217                         default:
1218                                 break;
1219                         }
1220                 }
1221                 // we try to activate the actual inset and put this event down to
1222                 // the insets dispatch function.
1223                 if ((result == DISPATCHED) || the_locking_inset)
1224                         break;
1225                 nodraw(true);
1226                 if (activateCellInset(bv)) {
1227                         // reset need_update setted in above function!
1228                         need_update = NONE;
1229                         result = the_locking_inset->localDispatch(bv, action, arg);
1230                         if ((result == UNDISPATCHED) || (result >= FINISHED)) {
1231                                 unlockInsetInInset(bv, the_locking_inset);
1232                                 nodraw(false);
1233                                 // we need to update if this was requested before
1234                                 updateLocal(bv, NONE, false);
1235                                 return UNDISPATCHED;
1236                         } else if (hs) {
1237                                 clearSelection();
1238                                 // so the below CELL is not set because this is higher
1239                                 // priority and we get a full redraw
1240                                 need_update = SELECTION;
1241                         }
1242                         nodraw(false);
1243                         updateLocal(bv, CELL, false);
1244                         return result;
1245                 }
1246                 break;
1247         }
1248         if (result < FINISHED) {
1249                 if (!the_locking_inset) {
1250                         showInsetCursor(bv);
1251                 }
1252         } else
1253                 bv->unlockInset(this);
1254         return result;
1255 }
1256
1257
1258 int InsetTabular::latex(Buffer const * buf, ostream & os,
1259                         bool fragile, bool fp) const
1260 {
1261         return tabular->latex(buf, os, fragile, fp);
1262 }
1263
1264
1265 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1266 {
1267         if (ll > 0)
1268                 return tabular->ascii(buf, os, (int)parOwner()->params().depth(),
1269                                       false,0);
1270         return tabular->ascii(buf, os, 0, false,0);
1271 }
1272
1273
1274 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1275 {
1276         os << "<![CDATA[";
1277         int const ret = tabular->ascii(buf,os,
1278                                        (int)parOwner()->params().depth(),
1279                                        false, 0);
1280         os << "]]>";
1281         return ret;
1282 }
1283
1284
1285 int InsetTabular::docbook(Buffer const * buf, ostream & os) const
1286 {
1287         int ret = 0;
1288         Inset * master;
1289
1290         // if the table is inside a float it doesn't need the informaltable
1291         // wrapper. Search for it.
1292         for(master = owner();
1293             master && master->lyxCode() != Inset::FLOAT_CODE;
1294             master = master->owner());
1295
1296         if (!master) {
1297                 os << "<informaltable>\n";
1298                 ret++;
1299         }
1300         ret+= tabular->docBook(buf,os);
1301         if (!master) {
1302                 os << "</informaltable>\n";
1303                 ret++;
1304         }
1305         return ret;
1306 }
1307
1308
1309 void InsetTabular::validate(LaTeXFeatures & features) const
1310 {
1311         tabular->Validate(features);
1312 }
1313
1314
1315 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1316                                                  LyXFont const & font,
1317                                                  bool reinit) const
1318 {
1319         int cell = -1;
1320         int maxAsc = 0;
1321         int maxDesc = 0;
1322         InsetText * inset;
1323         bool changed = false;
1324         
1325         // if we have a locking_inset we should have to check only this cell for
1326         // change so I'll try this to have a boost, but who knows ;)
1327         if ((need_update != INIT) &&
1328             (the_locking_inset == tabular->GetCellInset(actcell))) {
1329                 for(int i = 0; i < tabular->columns(); ++i) {
1330                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1331                                      maxAsc);
1332                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1333                                       maxDesc);
1334                 }
1335                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1336                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1337                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1338                 return changed;
1339         }
1340         for (int i = 0; i < tabular->rows(); ++i) {
1341                 maxAsc = 0;
1342                 maxDesc = 0;
1343                 for (int j = 0; j < tabular->columns(); ++j) {
1344                         if (tabular->IsPartOfMultiColumn(i,j))
1345                                 continue;
1346                         ++cell;
1347                         inset = tabular->GetCellInset(cell);
1348                         if (!reinit && !tabular->GetPWidth(cell).zero())
1349                                 inset->update(bv, font, false);
1350                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1351                         maxDesc = max(maxDesc, inset->descent(bv, font));
1352                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1353                 }
1354                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1355                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1356         }
1357         if (changed)
1358                 tabular->reinit();
1359         return changed;
1360 }
1361
1362
1363 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1364 {
1365         if (the_locking_inset) {
1366                 the_locking_inset->getCursorPos(bv, x, y);
1367                 return;
1368         }
1369         x = cursor_.x() - top_x;
1370         y = cursor_.y();
1371 }
1372
1373
1374 void InsetTabular::toggleInsetCursor(BufferView * bv)
1375 {
1376         if (nodraw()) {
1377                 if (isCursorVisible())
1378                         bv->hideLockedInsetCursor();
1379                 return;
1380         }
1381         if (the_locking_inset) {
1382                 the_locking_inset->toggleInsetCursor(bv);
1383                 return;
1384         }
1385         
1386         LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1387         
1388         int const asc = lyxfont::maxAscent(font);
1389         int const desc = lyxfont::maxDescent(font);
1390         
1391         if (isCursorVisible())
1392                 bv->hideLockedInsetCursor();
1393         else
1394                 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1395         toggleCursorVisible();
1396 }
1397
1398
1399 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1400 {
1401         if (nodraw())
1402                 return;
1403         if (!isCursorVisible()) {
1404                 LyXFont font; // = GetFont(par, cursor.pos);
1405         
1406                 int const asc = lyxfont::maxAscent(font);
1407                 int const desc = lyxfont::maxDescent(font);
1408                 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1409                 if (show)
1410                         bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1411                 setCursorVisible(true);
1412         }
1413 }
1414
1415
1416 void InsetTabular::hideInsetCursor(BufferView * bv)
1417 {
1418         if (isCursorVisible()) {
1419                 bv->hideLockedInsetCursor();
1420                 setCursorVisible(false);
1421         }
1422 }
1423
1424
1425 void InsetTabular::fitInsetCursor(BufferView * bv) const
1426 {
1427         if (the_locking_inset) {
1428                 the_locking_inset->fitInsetCursor(bv);
1429                 return;
1430         }
1431         LyXFont font;
1432         
1433         int const asc = lyxfont::maxAscent(font);
1434         int const desc = lyxfont::maxDescent(font);
1435         bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1436 }
1437
1438
1439 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1440 {
1441         cursor_.y(0);
1442         
1443         actcell = actrow = actcol = 0;
1444         int ly = tabular->GetDescentOfRow(actrow);
1445
1446         // first search the right row
1447         while ((ly < y) && ((actrow+1) < tabular->rows())) {
1448                 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1449                                  tabular->GetAscentOfRow(actrow + 1) +
1450                                  tabular->GetAdditionalHeight(actrow + 1));
1451                 ++actrow;
1452                 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1453         }
1454         actcell = tabular->GetCellNumber(actrow, actcol);
1455
1456         // now search the right column
1457         int lx = tabular->GetWidthOfColumn(actcell) -
1458                 tabular->GetAdditionalWidth(actcell);
1459         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1460                 lx += tabular->GetWidthOfColumn(actcell + 1)
1461                         + tabular->GetAdditionalWidth(actcell);
1462         }
1463         cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1464         resetPos(bv);
1465 }
1466
1467
1468 int InsetTabular::getCellXPos(int cell) const
1469 {
1470         int c = cell;
1471         
1472         for (; !tabular->IsFirstCellInRow(c); --c)
1473                 ;
1474         int lx = tabular->GetWidthOfColumn(cell);
1475         for (; c < cell; ++c) {
1476                 lx += tabular->GetWidthOfColumn(c);
1477         }
1478         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1479 }
1480
1481
1482 void InsetTabular::resetPos(BufferView * bv) const
1483 {
1484 #warning This should be fixed in the right manner (20011128 Jug)
1485         // fast hack to fix infinite repaintings!
1486         if (in_reset_pos)
1487                 return;
1488
1489         int cell = 0;
1490         actcol = tabular->column_of_cell(actcell);
1491         actrow = 0;
1492         cursor_.y(0);
1493         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1494                 if (tabular->IsLastCellInRow(cell)) {
1495                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1496                                          tabular->GetAscentOfRow(actrow + 1) +
1497                                          tabular->GetAdditionalHeight(actrow + 1));
1498                         ++actrow;
1499                 }
1500         }
1501         if (!locked || nodraw()) {
1502                 if (the_locking_inset)
1503                         inset_y = cursor_.y();
1504                 return;
1505         }
1506         // we need this only from here on!!!
1507         in_reset_pos = true;
1508         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1509         int new_x = getCellXPos(actcell);
1510         int old_x = cursor_.x();
1511         new_x += offset;
1512         cursor_.x(new_x);
1513 //    cursor.x(getCellXPos(actcell) + offset);
1514         if ((actcol < tabular->columns()-1) && scroll(false) &&
1515                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1516         {
1517                 scroll(bv, 0.0F);
1518                 updateLocal(bv, FULL, false);
1519         } else if (the_locking_inset &&
1520                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1521         {
1522                 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1523                 if (xx > (bv->workWidth()-20)) {
1524                         scroll(bv, -(xx - bv->workWidth() + 60));
1525                         updateLocal(bv, FULL, false);
1526                 } else if (xx < 20) {
1527                         if (xx < 0)
1528                                 xx = -xx + 60;
1529                         else
1530                                 xx = 60;
1531                         scroll(bv, xx);
1532                         updateLocal(bv, FULL, false);
1533                 }
1534         } else if ((cursor_.x() - offset) > 20 &&
1535                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1536                    > (bv->workWidth() - 20)) {
1537                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1538                 updateLocal(bv, FULL, false);
1539         } else if ((cursor_.x() - offset) < 20) {
1540                 scroll(bv, 20 - cursor_.x() + offset);
1541                 updateLocal(bv, FULL, false);
1542         } else if (scroll(false) && top_x > 20 &&
1543                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1544                 scroll(bv, old_x - cursor_.x());
1545                 updateLocal(bv, FULL, false);
1546         }
1547         if (the_locking_inset) {
1548                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1549                 inset_y = cursor_.y();
1550         }
1551         if ((!the_locking_inset ||
1552              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1553             actcell != oldcell) {
1554                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1555                 bv->owner()->getDialogs()->updateTabular(inset);
1556                 oldcell = actcell;
1557         }
1558         in_reset_pos = false;
1559 }
1560
1561
1562 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1563 {
1564         if (lock && !old_locking_inset) {
1565                 if (activateCellInset(bv))
1566                         return DISPATCHED;
1567         } else {
1568                 bool moved = isRightToLeft(bv)
1569                         ? movePrevCell(bv) : moveNextCell(bv);
1570                 if (!moved)
1571                         return FINISHED_RIGHT;
1572                 if (lock && activateCellInset(bv))
1573                         return DISPATCHED;
1574         }
1575         resetPos(bv);
1576         return DISPATCHED_NOUPDATE;
1577 }
1578
1579
1580 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1581 {
1582         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1583         if (!moved)
1584                 return FINISHED;
1585         if (lock) {       // behind the inset
1586                 if (activateCellInset(bv, 0, 0, 0, true))
1587                         return DISPATCHED;
1588         }
1589         resetPos(bv);
1590         return DISPATCHED_NOUPDATE;
1591 }
1592
1593
1594 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1595 {
1596         int const ocell = actcell;
1597         actcell = tabular->GetCellAbove(actcell);
1598         if (actcell == ocell) // we moved out of the inset
1599                 return FINISHED_UP;
1600         resetPos(bv);
1601         if (lock) {
1602                 int x = 0;
1603                 int y = 0;
1604                 if (old_locking_inset) {
1605                         old_locking_inset->getCursorPos(bv, x, y);
1606                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1607                 }
1608                 if (activateCellInset(bv, x, 0))
1609                         return DISPATCHED;
1610         }
1611         return DISPATCHED_NOUPDATE;
1612 }
1613
1614
1615 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1616 {
1617         int const ocell = actcell;
1618         actcell = tabular->GetCellBelow(actcell);
1619         if (actcell == ocell) // we moved out of the inset
1620                 return FINISHED_DOWN;
1621         resetPos(bv);
1622         if (lock) {
1623                 int x = 0;
1624                 int y = 0;
1625                 if (old_locking_inset) {
1626                         old_locking_inset->getCursorPos(bv, x, y);
1627                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1628                 }
1629                 if (activateCellInset(bv, x, 0))
1630                         return DISPATCHED;
1631         }
1632         return DISPATCHED_NOUPDATE;
1633 }
1634
1635
1636 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1637 {
1638         if (isRightToLeft(bv)) {
1639                 if (tabular->IsFirstCellInRow(actcell)) {
1640                         int row = tabular->row_of_cell(actcell);
1641                         if (row == tabular->rows() - 1)
1642                                 return false;
1643                         actcell = tabular->GetLastCellInRow(row);
1644                         actcell = tabular->GetCellBelow(actcell);
1645                 } else {
1646                         if (!actcell)
1647                                 return false;
1648                         --actcell;
1649                 }
1650         } else {
1651                 if (tabular->IsLastCell(actcell))
1652                         return false;
1653                 ++actcell;
1654         }
1655         if (lock) {
1656                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1657                         isRightToLeftPar(bv->buffer()->params);
1658                 activateCellInset(bv, 0, 0, 0, !rtl);
1659         }
1660         resetPos(bv);
1661         return true;
1662 }
1663
1664
1665 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1666 {
1667         if (isRightToLeft(bv)) {
1668                 if (tabular->IsLastCellInRow(actcell)) {
1669                         int row = tabular->row_of_cell(actcell);
1670                         if (row == 0)
1671                                 return false;
1672                         actcell = tabular->GetFirstCellInRow(row);
1673                         actcell = tabular->GetCellAbove(actcell);
1674                 } else {
1675                         if (tabular->IsLastCell(actcell))
1676                                 return false;
1677                         ++actcell;
1678                 }
1679         } else {
1680                 if (!actcell) // first cell
1681                         return false;
1682                 --actcell;
1683         }
1684         if (lock) {
1685                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1686                         isRightToLeftPar(bv->buffer()->params);
1687                 activateCellInset(bv, 0, 0, 0, !rtl);
1688         }
1689         resetPos(bv);
1690         return true;
1691 }
1692
1693
1694 bool InsetTabular::deletable() const
1695 {
1696         return true;
1697 }
1698
1699
1700 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1701                            bool selectall)
1702 {
1703         if (selectall) {
1704                 setSelection(0, tabular->GetNumberOfCells() - 1);
1705         }
1706         if (hasSelection()) {
1707                 setUndo(bv, Undo::EDIT,
1708                         bv->text->cursor.par(),
1709                         bv->text->cursor.par()->next());
1710                 bool frozen = undo_frozen;
1711                 if (!frozen)
1712                         freezeUndo();
1713                 // apply the fontchange on the whole selection
1714                 int sel_row_start;
1715                 int sel_row_end;
1716                 int sel_col_start;
1717                 int sel_col_end;
1718                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1719                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1720                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1721                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1722                         }
1723                 }
1724                 if (!frozen)
1725                         unFreezeUndo();
1726                 updateLocal(bv, INIT, true);
1727         }
1728         if (the_locking_inset)
1729                 the_locking_inset->setFont(bv, font, tall);
1730 }
1731
1732
1733 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1734 {
1735         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1736         
1737         int i = 0;
1738         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1739                 string const tmp = tabularFeature[i].feature;
1740                 
1741                 if (tmp == what.substr(0, tmp.length())) {
1742                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1743                         //tabularFeatures[i].feature.length())) {
1744                         action = tabularFeature[i].action;
1745                         break;
1746                 }
1747         }
1748         if (action == LyXTabular::LAST_ACTION)
1749                 return false;
1750
1751         string const val =
1752                 frontStrip(what.substr(tabularFeature[i].feature.length()));
1753         tabularFeatures(bv, action, val);
1754         return true;
1755 }
1756
1757 static void checkLongtableSpecial(LyXTabular::ltType & ltt,
1758                                   string const & special, bool & flag)
1759 {
1760         if (special == "dl_above") {
1761                 ltt.topDL = flag;
1762                 ltt.set = false;
1763         } else if (special == "dl_below") {
1764                 ltt.bottomDL = flag;
1765                 ltt.set = false;
1766         } else if (special == "empty") {
1767                 ltt.empty = flag;
1768                 ltt.set = false;
1769         } else if (flag) {
1770                 ltt.empty = false;
1771                 ltt.set = true;
1772         }
1773 }
1774
1775
1776 void InsetTabular::tabularFeatures(BufferView * bv,
1777                                    LyXTabular::Feature feature,
1778                                    string const & value)
1779 {
1780         int sel_col_start;
1781         int sel_col_end;
1782         int sel_row_start;
1783         int sel_row_end;
1784         bool setLines = false;
1785         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1786         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1787
1788         switch (feature) {
1789         case LyXTabular::M_ALIGN_LEFT:
1790         case LyXTabular::ALIGN_LEFT:
1791                 setAlign = LYX_ALIGN_LEFT;
1792                 break;
1793         case LyXTabular::M_ALIGN_RIGHT:
1794         case LyXTabular::ALIGN_RIGHT:
1795                 setAlign = LYX_ALIGN_RIGHT;
1796                 break;
1797         case LyXTabular::M_ALIGN_CENTER:
1798         case LyXTabular::ALIGN_CENTER:
1799                 setAlign = LYX_ALIGN_CENTER;
1800                 break;
1801         case LyXTabular::M_VALIGN_TOP:
1802         case LyXTabular::VALIGN_TOP:
1803                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1804                 break;
1805         case LyXTabular::M_VALIGN_BOTTOM:
1806         case LyXTabular::VALIGN_BOTTOM:
1807                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1808                 break;
1809         case LyXTabular::M_VALIGN_CENTER:
1810         case LyXTabular::VALIGN_CENTER:
1811                 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1812                 break;
1813         default:
1814                 break;
1815         }
1816         if (hasSelection()) {
1817                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1818         } else {
1819                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1820                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1821         }
1822         setUndo(bv, Undo::FINISH,
1823                 bv->text->cursor.par(),
1824                 bv->text->cursor.par()->next());
1825
1826         int row =  tabular->row_of_cell(actcell);
1827         int column = tabular->column_of_cell(actcell);
1828         bool flag = true;
1829         LyXTabular::ltType ltt;
1830         
1831         switch (feature) {
1832         case LyXTabular::SET_PWIDTH:
1833         {
1834                 LyXLength const vallen(value);
1835                 LyXLength const & tmplen = tabular->GetColumnPWidth(actcell);
1836                 
1837                 bool const update = (tmplen != vallen);
1838                 tabular->SetColumnPWidth(actcell, vallen);
1839                 if (update) {
1840                         int cell;
1841                         for (int i = 0; i < tabular->rows(); ++i) {
1842                                 cell = tabular->GetCellNumber(i,column);
1843                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1844                         }
1845                         updateLocal(bv, INIT, true);
1846                 }
1847         }
1848         break;
1849         case LyXTabular::SET_MPWIDTH:
1850         {
1851                 LyXLength const vallen(value);
1852                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1853                 
1854                 bool const update = (tmplen != vallen);
1855                 tabular->SetMColumnPWidth(actcell, vallen);
1856                 if (update) {
1857                         for (int i = 0; i < tabular->rows(); ++i) {
1858                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1859                                         resizeLyXText(bv);
1860                         }
1861                         updateLocal(bv, INIT, true);
1862                 }
1863         }
1864         break;
1865         case LyXTabular::SET_SPECIAL_COLUMN:
1866         case LyXTabular::SET_SPECIAL_MULTI:
1867                 tabular->SetAlignSpecial(actcell,value,feature);
1868                 updateLocal(bv, FULL, true);
1869                 break;
1870         case LyXTabular::APPEND_ROW:
1871                 // append the row into the tabular
1872                 unlockInsetInInset(bv, the_locking_inset);
1873                 tabular->AppendRow(actcell);
1874                 updateLocal(bv, INIT, true);
1875                 break;
1876         case LyXTabular::APPEND_COLUMN:
1877                 // append the column into the tabular
1878                 unlockInsetInInset(bv, the_locking_inset);
1879                 tabular->AppendColumn(actcell);
1880                 actcell = tabular->GetCellNumber(row, column);
1881                 updateLocal(bv, INIT, true);
1882                 break;
1883         case LyXTabular::DELETE_ROW:
1884                 unlockInsetInInset(bv, the_locking_inset);
1885                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1886                         tabular->DeleteRow(sel_row_start);
1887                 }
1888                 if (sel_row_start >= tabular->rows())
1889                         --sel_row_start;
1890                 actcell = tabular->GetCellNumber(sel_row_start, column);
1891                 clearSelection();
1892                 updateLocal(bv, INIT, true);
1893                 break;
1894         case LyXTabular::DELETE_COLUMN:
1895                 unlockInsetInInset(bv, the_locking_inset);
1896                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1897                         tabular->DeleteColumn(sel_col_start);
1898                 }
1899                 if (sel_col_start >= tabular->columns())
1900                         --sel_col_start;
1901                 actcell = tabular->GetCellNumber(row, sel_col_start);
1902                 clearSelection();
1903                 updateLocal(bv, INIT, true);
1904                 break;
1905         case LyXTabular::M_TOGGLE_LINE_TOP:
1906                 flag = false;
1907         case LyXTabular::TOGGLE_LINE_TOP:
1908         {
1909                 bool lineSet = !tabular->TopLine(actcell, flag);
1910                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1911                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1912                                 tabular->SetTopLine(
1913                                         tabular->GetCellNumber(i, j),
1914                                         lineSet, flag);
1915                 updateLocal(bv, INIT, true);
1916                 break;
1917         }
1918         
1919         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1920                 flag = false;
1921         case LyXTabular::TOGGLE_LINE_BOTTOM:
1922         {
1923                 bool lineSet = !tabular->BottomLine(actcell, flag); 
1924                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1925                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1926                                 tabular->SetBottomLine(
1927                                         tabular->GetCellNumber(i, j),
1928                                         lineSet,
1929                                         flag);
1930                 updateLocal(bv, INIT, true);
1931                 break;
1932         }
1933         
1934         case LyXTabular::M_TOGGLE_LINE_LEFT:
1935                 flag = false;
1936         case LyXTabular::TOGGLE_LINE_LEFT:
1937         {
1938                 bool lineSet = !tabular->LeftLine(actcell, flag);
1939                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1940                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1941                                 tabular->SetLeftLine(
1942                                         tabular->GetCellNumber(i,j),
1943                                         lineSet,
1944                                         flag);
1945                 updateLocal(bv, INIT, true);
1946                 break;
1947         }
1948         
1949         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1950                 flag = false;
1951         case LyXTabular::TOGGLE_LINE_RIGHT:
1952         {
1953                 bool lineSet = !tabular->RightLine(actcell, flag);
1954                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1955                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1956                                 tabular->SetRightLine(
1957                                         tabular->GetCellNumber(i,j),
1958                                         lineSet,
1959                                         flag);
1960                 updateLocal(bv, INIT, true);
1961                 break;
1962         }
1963         
1964         case LyXTabular::M_ALIGN_LEFT:
1965         case LyXTabular::M_ALIGN_RIGHT:
1966         case LyXTabular::M_ALIGN_CENTER:
1967                 flag = false;
1968         case LyXTabular::ALIGN_LEFT:
1969         case LyXTabular::ALIGN_RIGHT:
1970         case LyXTabular::ALIGN_CENTER:
1971                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1972                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1973                                 tabular->SetAlignment(
1974                                         tabular->GetCellNumber(i, j),
1975                                         setAlign,
1976                                         flag);
1977                 updateLocal(bv, INIT, true);
1978                 break;
1979         case LyXTabular::M_VALIGN_TOP:
1980         case LyXTabular::M_VALIGN_BOTTOM:
1981         case LyXTabular::M_VALIGN_CENTER:
1982                 flag = false;
1983         case LyXTabular::VALIGN_TOP:
1984         case LyXTabular::VALIGN_BOTTOM:
1985         case LyXTabular::VALIGN_CENTER:
1986                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1987                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1988                                 tabular->SetVAlignment(
1989                                         tabular->GetCellNumber(i, j),
1990                                         setVAlign, flag);
1991                 updateLocal(bv, INIT, true);
1992                 break;
1993         case LyXTabular::MULTICOLUMN:
1994         {
1995                 if (sel_row_start != sel_row_end) {
1996                         Alert::alert(_("Impossible Operation!"), 
1997                                    _("Multicolumns can only be horizontally."), 
1998                                    _("Sorry."));
1999                         return;
2000                 }
2001                 // just multicol for one Single Cell
2002                 if (!hasSelection()) {
2003                         // check wether we are completly in a multicol
2004                         if (tabular->IsMultiColumn(actcell)) {
2005                                 tabular->UnsetMultiColumn(actcell);
2006                                 updateLocal(bv, INIT, true);
2007                         } else {
2008                                 tabular->SetMultiColumn(actcell, 1);
2009                                 updateLocal(bv, CELL, true);
2010                         }
2011                         return;
2012                 }
2013                 // we have a selection so this means we just add all this
2014                 // cells to form a multicolumn cell
2015                 int s_start;
2016                 int s_end;
2017
2018                 if (sel_cell_start > sel_cell_end) {
2019                         s_start = sel_cell_end;
2020                         s_end = sel_cell_start;
2021                 } else {
2022                         s_start = sel_cell_start;
2023                         s_end = sel_cell_end;
2024                 }
2025                 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
2026                 actcell = s_start;
2027                 clearSelection();
2028                 updateLocal(bv, INIT, true);
2029                 break;
2030         }
2031         case LyXTabular::SET_ALL_LINES:
2032                 setLines = true;
2033         case LyXTabular::UNSET_ALL_LINES:
2034                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2035                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2036                                 tabular->SetAllLines(
2037                                         tabular->GetCellNumber(i,j), setLines);
2038                 updateLocal(bv, INIT, true);
2039                 break;
2040         case LyXTabular::SET_LONGTABULAR:
2041                 tabular->SetLongTabular(true);
2042                 updateLocal(bv, INIT, true); // because this toggles displayed
2043                 break;
2044         case LyXTabular::UNSET_LONGTABULAR:
2045                 tabular->SetLongTabular(false);
2046                 updateLocal(bv, INIT, true); // because this toggles displayed
2047                 break;
2048         case LyXTabular::SET_ROTATE_TABULAR:
2049                 tabular->SetRotateTabular(true);
2050                 break;
2051         case LyXTabular::UNSET_ROTATE_TABULAR:
2052                 tabular->SetRotateTabular(false);
2053                 break;
2054         case LyXTabular::SET_ROTATE_CELL:
2055                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2056                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2057                                 tabular->SetRotateCell(
2058                                         tabular->GetCellNumber(i, j),
2059                                         true);
2060                 break;
2061         case LyXTabular::UNSET_ROTATE_CELL:
2062                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2063                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2064                                 tabular->SetRotateCell(
2065                                         tabular->GetCellNumber(i, j), false);
2066                 break;
2067         case LyXTabular::SET_USEBOX:
2068         {
2069                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2070                 if (val == tabular->GetUsebox(actcell))
2071                         val = LyXTabular::BOX_NONE;
2072                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2073                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2074                                 tabular->SetUsebox(
2075                                         tabular->GetCellNumber(i, j), val);
2076                 break;
2077         }
2078         case LyXTabular::UNSET_LTFIRSTHEAD:
2079                 flag = false;
2080         case LyXTabular::SET_LTFIRSTHEAD:
2081                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2082                 checkLongtableSpecial(ltt, value, flag);
2083                 tabular->SetLTHead(row, flag, ltt, true);
2084                 break;
2085         case LyXTabular::UNSET_LTHEAD:
2086                 flag = false;
2087         case LyXTabular::SET_LTHEAD:
2088                 (void)tabular->GetRowOfLTHead(row, ltt);
2089                 checkLongtableSpecial(ltt, value, flag);
2090                 tabular->SetLTHead(row, flag, ltt, false);
2091                 break;
2092         case LyXTabular::UNSET_LTFOOT:
2093                 flag = false;
2094         case LyXTabular::SET_LTFOOT:
2095                 (void)tabular->GetRowOfLTFoot(row, ltt);
2096                 checkLongtableSpecial(ltt, value, flag);
2097                 tabular->SetLTFoot(row, flag, ltt, false);
2098                 break;
2099         case LyXTabular::UNSET_LTLASTFOOT:
2100                 flag = false;
2101         case LyXTabular::SET_LTLASTFOOT:
2102                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2103                 checkLongtableSpecial(ltt, value, flag);
2104                 tabular->SetLTFoot(row, flag, ltt, true);
2105                 break;
2106         case LyXTabular::SET_LTNEWPAGE:
2107         {
2108                 bool what = !tabular->GetLTNewPage(row);
2109                 tabular->SetLTNewPage(row, what);
2110                 break;
2111         }
2112         // dummy stuff just to avoid warnings
2113         case LyXTabular::LAST_ACTION:
2114                 break;
2115         }
2116 }
2117
2118
2119 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
2120                                      bool behind)
2121 {
2122         UpdatableInset * inset =
2123                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2124         LyXFont font(LyXFont::ALL_SANE);
2125         if (behind) {
2126                 x = inset->x() + inset->width(bv, font);
2127                 y = inset->descent(bv, font);
2128         }
2129         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2130         //inset_y = cursor.y();
2131         inset->edit(bv, x,  y, button);
2132         if (!the_locking_inset)
2133                 return false;
2134         updateLocal(bv, CELL, false);
2135         return (the_locking_inset != 0);
2136 }
2137
2138
2139 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2140                                         int button)
2141 {
2142         inset_x = cursor_.x()
2143                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2144         inset_y = cursor_.y();
2145         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2146 }
2147
2148
2149 bool InsetTabular::insetHit(BufferView *, int x, int) const
2150 {
2151         return (x + top_x)
2152                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2153 }
2154
2155
2156 // This returns paperWidth() if the cell-width is unlimited or the width
2157 // in pixels if we have a pwidth for this cell.
2158 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2159 {
2160         LyXLength const len = tabular->GetPWidth(cell);
2161         
2162         if (len.zero())
2163                 return -1;
2164         return len.inPixels(latexTextWidth(bv), bv->text->defaultHeight());
2165 }
2166
2167
2168 int InsetTabular::getMaxWidth(BufferView * bv,
2169                               UpdatableInset const * inset) const
2170 {
2171         int cell = tabular->GetCellFromInset(inset, actcell);
2172
2173         if (cell == -1) {
2174                 lyxerr << "Own inset not found, shouldn't really happen!"
2175                        << endl;
2176                 return -1;
2177         }
2178         
2179         int w = getMaxWidthOfCell(bv, cell);
2180         if (w > 0) {
2181                 // because the inset then subtracts it's top_x and owner->x()
2182                 w += (inset->x() - top_x);
2183         }
2184         
2185         return w;
2186 }
2187
2188
2189 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2190 {
2191         resizeLyXText(bv, recursive);
2192 }
2193
2194
2195 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2196 {
2197         if (force) {
2198                 for(int i = 0; i < tabular->rows(); ++i) {
2199                         for(int j = 0; j < tabular->columns(); ++j) {
2200                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2201                         }
2202                 }
2203         }
2204         need_update = FULL;
2205 }
2206
2207
2208 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2209                                    bool const recursive) const
2210 {
2211         if (the_locking_inset)
2212                 return the_locking_inset->getLyXText(bv, recursive);
2213 #if 0
2214         // if we're locked lock the actual insettext and return it's LyXText!!!
2215         if (locked) {
2216                 UpdatableInset * inset =
2217                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2218                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2219                 return the_locking_inset->getLyXText(bv, recursive);
2220         }
2221 #endif
2222         return Inset::getLyXText(bv, recursive);
2223 }
2224
2225
2226 bool InsetTabular::showInsetDialog(BufferView * bv) const
2227 {
2228         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2229                 bv->owner()->getDialogs()
2230                         ->showTabular(const_cast<InsetTabular *>(this));
2231         return true;
2232 }
2233
2234
2235 void InsetTabular::openLayoutDialog(BufferView * bv) const
2236 {
2237         if (the_locking_inset) {
2238                 InsetTabular * i = static_cast<InsetTabular *>
2239                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2240                 if (i) {
2241                         i->openLayoutDialog(bv);
2242                         return;
2243                 }
2244         }
2245         bv->owner()->getDialogs()->showTabular(
2246                 const_cast<InsetTabular *>(this));
2247 }
2248
2249
2250 //
2251 // function returns an object as defined in func_status.h:
2252 // states OK, Unknown, Disabled, On, Off.
2253 //
2254 FuncStatus InsetTabular::getStatus(string const & what) const
2255 {
2256         int action = LyXTabular::LAST_ACTION;
2257         FuncStatus status;
2258         
2259         int i = 0;
2260         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2261                 string const tmp = tabularFeature[i].feature;
2262                 if (tmp == what.substr(0, tmp.length())) {                  
2263                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2264                         //   tabularFeatures[i].feature.length())) {
2265                         action = tabularFeature[i].action;
2266                         break;
2267                 }
2268         }
2269         if (action == LyXTabular::LAST_ACTION) {
2270                 status.clear();
2271                 return status.unknown(true);
2272         }
2273         
2274         string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2275
2276         int sel_row_start;
2277         int sel_row_end;
2278         int dummy;
2279         LyXTabular::ltType dummyltt;
2280         bool flag = true;
2281
2282         if (hasSelection()) {
2283                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2284         } else {
2285                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2286         }
2287
2288         switch (action) {
2289         case LyXTabular::SET_PWIDTH:
2290         case LyXTabular::SET_MPWIDTH:
2291         case LyXTabular::SET_SPECIAL_COLUMN:
2292         case LyXTabular::SET_SPECIAL_MULTI:
2293                 return status.disabled(true);
2294
2295         case LyXTabular::APPEND_ROW:
2296         case LyXTabular::APPEND_COLUMN:
2297         case LyXTabular::DELETE_ROW:
2298         case LyXTabular::DELETE_COLUMN:
2299         case LyXTabular::SET_ALL_LINES:
2300         case LyXTabular::UNSET_ALL_LINES:
2301                 return status.clear();
2302
2303         case LyXTabular::MULTICOLUMN:
2304                 status.setOnOff(tabular->IsMultiColumn(actcell));
2305                 break;
2306         case LyXTabular::M_TOGGLE_LINE_TOP:
2307                 flag = false;
2308         case LyXTabular::TOGGLE_LINE_TOP:
2309                 status.setOnOff(tabular->TopLine(actcell, flag));
2310                 break;
2311         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2312                 flag = false;
2313         case LyXTabular::TOGGLE_LINE_BOTTOM:
2314                 status.setOnOff(tabular->BottomLine(actcell, flag));
2315                 break;
2316         case LyXTabular::M_TOGGLE_LINE_LEFT:
2317                 flag = false;
2318         case LyXTabular::TOGGLE_LINE_LEFT:
2319                 status.setOnOff(tabular->LeftLine(actcell, flag));
2320                 break;
2321         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2322                 flag = false;
2323         case LyXTabular::TOGGLE_LINE_RIGHT:
2324                 status.setOnOff(tabular->RightLine(actcell, flag));
2325                 break;
2326         case LyXTabular::M_ALIGN_LEFT:
2327                 flag = false;
2328         case LyXTabular::ALIGN_LEFT:
2329                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2330                 break;
2331         case LyXTabular::M_ALIGN_RIGHT:
2332                 flag = false;
2333         case LyXTabular::ALIGN_RIGHT:
2334                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2335                 break;
2336         case LyXTabular::M_ALIGN_CENTER:
2337                 flag = false;
2338         case LyXTabular::ALIGN_CENTER:
2339                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2340                 break;
2341         case LyXTabular::M_VALIGN_TOP:
2342                 flag = false;
2343         case LyXTabular::VALIGN_TOP:
2344                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2345                 break;
2346         case LyXTabular::M_VALIGN_BOTTOM:
2347                 flag = false;
2348         case LyXTabular::VALIGN_BOTTOM:
2349                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2350                 break;
2351         case LyXTabular::M_VALIGN_CENTER:
2352                 flag = false;
2353         case LyXTabular::VALIGN_CENTER:
2354                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2355                 break;
2356         case LyXTabular::SET_LONGTABULAR:
2357                 status.setOnOff(tabular->IsLongTabular());
2358                 break;
2359         case LyXTabular::UNSET_LONGTABULAR:
2360                 status.setOnOff(!tabular->IsLongTabular());
2361                 break;
2362         case LyXTabular::SET_ROTATE_TABULAR:
2363                 status.setOnOff(tabular->GetRotateTabular());
2364                 break;
2365         case LyXTabular::UNSET_ROTATE_TABULAR:
2366                 status.setOnOff(!tabular->GetRotateTabular());
2367                 break;
2368         case LyXTabular::SET_ROTATE_CELL:
2369                 status.setOnOff(tabular->GetRotateCell(actcell));
2370                 break;
2371         case LyXTabular::UNSET_ROTATE_CELL:
2372                 status.setOnOff(!tabular->GetRotateCell(actcell));
2373                 break;
2374         case LyXTabular::SET_USEBOX:
2375                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2376                 break;
2377         case LyXTabular::SET_LTFIRSTHEAD:
2378                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2379                 break;
2380         case LyXTabular::SET_LTHEAD:
2381                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2382                 break;
2383         case LyXTabular::SET_LTFOOT:
2384                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2385                 break;
2386         case LyXTabular::SET_LTLASTFOOT:
2387                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2388                 break;
2389         case LyXTabular::SET_LTNEWPAGE:
2390                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2391                 break;
2392         default:
2393                 status.clear();
2394                 status.disabled(true);
2395                 break;
2396         }
2397         return status;
2398 }
2399
2400
2401 vector<string> const InsetTabular::getLabelList() const
2402 {
2403         return tabular->getLabelList();
2404 }
2405
2406
2407 bool InsetTabular::copySelection(BufferView * bv)
2408 {
2409         if (!hasSelection())
2410                 return false;
2411
2412         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2413         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2414         if (sel_col_start > sel_col_end) {
2415                 sel_col_start = sel_col_end;
2416                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2417         } else {
2418                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2419         }
2420         int const columns = sel_col_end - sel_col_start + 1;
2421
2422         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2423         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2424         if (sel_row_start > sel_row_end) {
2425                 swap(sel_row_start, sel_row_end);
2426         }
2427         int const rows = sel_row_end - sel_row_start + 1;
2428
2429         delete paste_tabular;
2430         paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2431         for (int i = 0; i < sel_row_start; ++i)
2432                 paste_tabular->DeleteRow(0);
2433         while (paste_tabular->rows() > rows)
2434                 paste_tabular->DeleteRow(rows);
2435         paste_tabular->SetTopLine(0, true, true);
2436         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2437                                      true, true);
2438         for (int i = 0; i < sel_col_start; ++i)
2439                 paste_tabular->DeleteColumn(0);
2440         while (paste_tabular->columns() > columns)
2441                 paste_tabular->DeleteColumn(columns);
2442         paste_tabular->SetLeftLine(0, true, true);
2443         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2444                                     true, true);
2445
2446         ostringstream sstr;
2447         paste_tabular->ascii(bv->buffer(), sstr,
2448                              (int)parOwner()->params().depth(), true, '\t');
2449         bv->stuffClipboard(sstr.str().c_str());
2450         return true;
2451 }
2452
2453
2454 bool InsetTabular::pasteSelection(BufferView * bv)
2455 {
2456         if (!paste_tabular)
2457                 return false;
2458
2459         for (int r1 = 0, r2 = actrow;
2460              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2461              ++r1, ++r2) {
2462                 for(int c1 = 0, c2 = actcol;
2463                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2464                     ++c1, ++c2) {
2465                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2466                             tabular->IsPartOfMultiColumn(r2,c2))
2467                                 continue;
2468                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2469                                 --c2;
2470                                 continue;
2471                         }
2472                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2473                                 --c1;
2474                                 continue;
2475                         }
2476                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2477                         int const n2 = tabular->GetCellNumber(r2, c2);
2478                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2479                         tabular->GetCellInset(n2)->setOwner(this);
2480                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2481                 }
2482         }
2483         return true;
2484 }
2485
2486
2487 bool InsetTabular::cutSelection()
2488 {
2489         if (!hasSelection())
2490                 return false;
2491
2492         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2493         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2494         if (sel_col_start > sel_col_end) {
2495                 sel_col_start = sel_col_end;
2496                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2497         } else {
2498                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2499         }
2500         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2501         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2502         if (sel_row_start > sel_row_end) {
2503                 swap(sel_row_start, sel_row_end);
2504         }
2505         if (sel_cell_start > sel_cell_end) {
2506                 swap(sel_cell_start, sel_cell_end);
2507         }
2508         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2509                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2510                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2511                 }
2512         }
2513         return true;
2514 }
2515
2516
2517 bool InsetTabular::isRightToLeft(BufferView * bv)
2518 {
2519         return bv->getParentLanguage(this)->RightToLeft();
2520 }
2521
2522
2523 bool InsetTabular::nodraw() const
2524 {
2525         if (!UpdatableInset::nodraw() && the_locking_inset)
2526                 return the_locking_inset->nodraw();
2527         return UpdatableInset::nodraw();
2528 }
2529
2530
2531 int InsetTabular::scroll(bool recursive) const
2532 {
2533         int sx = UpdatableInset::scroll(false);
2534
2535         if (recursive && the_locking_inset)
2536                 sx += the_locking_inset->scroll(recursive);
2537
2538         return sx;
2539 }
2540
2541
2542 bool InsetTabular::doClearArea() const
2543 {
2544         return !locked || (need_update & (FULL|INIT));
2545 }
2546
2547
2548 void InsetTabular::getSelection(int & srow, int & erow,
2549                                 int & scol, int & ecol) const
2550 {
2551         int const start = hasSelection() ? sel_cell_start : actcell;
2552         int const end = hasSelection() ? sel_cell_end : actcell;
2553  
2554         srow = tabular->row_of_cell(start);
2555         erow = tabular->row_of_cell(end);
2556         if (srow > erow) {
2557                 swap(srow, erow);
2558         }
2559
2560         scol = tabular->column_of_cell(start);
2561         ecol = tabular->column_of_cell(end);
2562         if (scol > ecol) {
2563                 swap(scol, ecol);
2564         } else {
2565                 ecol = tabular->right_column_of_cell(end);
2566         }
2567 }
2568
2569
2570 Paragraph * InsetTabular::getParFromID(int id) const
2571 {
2572         Paragraph * result;
2573         for(int i = 0; i < tabular->rows(); ++i) {
2574                 for(int j = 0; j < tabular->columns(); ++j) {
2575                         if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2576                                 return result;
2577                 }
2578         }
2579         return 0;
2580 }
2581
2582
2583 Paragraph * InsetTabular::firstParagraph() const
2584 {
2585         if (the_locking_inset)
2586                 return the_locking_inset->firstParagraph();
2587         return 0;
2588 }
2589
2590
2591 Paragraph * InsetTabular::getFirstParagraph(int i) const
2592 {
2593         return (i < tabular->GetNumberOfCells())
2594                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2595                 : 0;
2596 }
2597
2598
2599 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2600 {
2601         if (the_locking_inset)
2602                 return the_locking_inset->cursor(bv);
2603         return Inset::cursor(bv);
2604 }
2605
2606
2607 Inset * InsetTabular::getInsetFromID(int id_arg) const
2608 {
2609         if (id_arg == id())
2610                 return const_cast<InsetTabular *>(this);
2611
2612         Inset * result;
2613         for(int i = 0; i < tabular->rows(); ++i) {
2614                 for(int j = 0; j < tabular->columns(); ++j) {
2615                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2616                                 return result;
2617                 }
2618         }
2619         return 0;
2620 }
2621
2622
2623 string const
2624 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2625 {
2626         nodraw(true);
2627         if (the_locking_inset) {
2628                 string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2629                 if (!str.empty()) {
2630                         nodraw(false);
2631                         return str;
2632                 }
2633                 if (tabular->IsLastCell(actcell)) {
2634                         bv->unlockInset(const_cast<InsetTabular *>(this));
2635                         nodraw(false);
2636                         return string();
2637                 }
2638                 ++actcell;
2639         }
2640         // otherwise we have to lock the next inset and ask for it's selecttion
2641         UpdatableInset * inset =
2642                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2643         inset->edit(bv, 0,  0, 0);
2644         string const str(selectNextWordInt(bv, value));
2645         nodraw(false);
2646         if (!str.empty())
2647                 resetPos(bv);
2648         return str;
2649 }
2650
2651
2652 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2653 {
2654         // when entering this function the inset should be ALWAYS locked!
2655         lyx::Assert(the_locking_inset);
2656
2657         string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2658         if (!str.empty())
2659                 return str;
2660
2661         if (tabular->IsLastCell(actcell)) {
2662                 bv->unlockInset(const_cast<InsetTabular *>(this));
2663                 return string();
2664         }
2665         
2666         // otherwise we have to lock the next inset and ask for it's selecttion
2667         UpdatableInset * inset =
2668                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2669         inset->edit(bv);
2670         return selectNextWordInt(bv, value);
2671 }
2672
2673
2674 void InsetTabular::selectSelectedWord(BufferView * bv)
2675 {
2676         if (the_locking_inset) {
2677                 the_locking_inset->selectSelectedWord(bv);
2678                 return;
2679         }
2680         return;
2681 }
2682
2683
2684 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2685 {
2686         if (the_locking_inset) {
2687                 the_locking_inset->toggleSelection(bv, kill_selection);
2688         }
2689 }
2690
2691
2692 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2693                                  bool cs, bool mw)
2694 {
2695         nodraw(true);
2696         if (the_locking_inset) {
2697                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2698                         nodraw(false);
2699                         updateLocal(bv, CELL, false);
2700                         return true;
2701                 }
2702                 if (tabular->IsLastCell(actcell)) {
2703                         nodraw(false);
2704                         bv->unlockInset(const_cast<InsetTabular *>(this));
2705                         return false;
2706                 }
2707                 ++actcell;
2708         }
2709         // otherwise we have to lock the next inset and search there
2710         UpdatableInset * inset =
2711                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2712         inset->edit(bv);
2713         bool const ret = searchForward(bv, str, cs, mw);
2714         nodraw(false);
2715         updateLocal(bv, CELL, false);
2716         return ret;
2717 }
2718
2719
2720 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2721                                bool cs, bool mw)
2722 {
2723         nodraw(true);
2724         if (the_locking_inset) {
2725                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2726                         nodraw(false);
2727                         updateLocal(bv, CELL, false);
2728                         return true;
2729                 }
2730                 if (!actcell) { // we are already in the first cell
2731                         nodraw(false);
2732                         bv->unlockInset(const_cast<InsetTabular *>(this));
2733                         return false;
2734                 }
2735                 --actcell;
2736         }
2737         // otherwise we have to lock the next inset and search there
2738         UpdatableInset * inset =
2739                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2740         inset->edit(bv, false);
2741         bool const ret = searchBackward(bv, str, cs, mw);
2742         nodraw(false);
2743         updateLocal(bv, CELL, false);
2744         return ret;
2745 }
2746
2747
2748 bool InsetTabular::insetAllowed(Inset::Code code) const
2749 {
2750         if (the_locking_inset)
2751                 return the_locking_inset->insetAllowed(code);
2752         return false;
2753 }
2754
2755
2756 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2757 {
2758         const int cell = tabular->GetCellFromInset(in, actcell);
2759
2760         if (cell != -1)
2761                 return tabular->GetPWidth(cell).zero();
2762
2763         // well we didn't obviously find it so maybe our owner knows more
2764         if (owner())
2765                 return owner()->forceDefaultParagraphs(in);
2766         // if we're here there is really something strange going on!!!
2767         return false;
2768 }