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