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