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