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