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