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