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