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