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