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