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