]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Forgot to save changed file ;)
[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                 the_locking_inset = 0;
601                 updateLocal(bv, CELL, false);
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                 the_locking_inset = 0;
691 #ifdef WITH_WARNINGS
692 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
693 #endif
694 #if 0
695                 if (scroll(false))
696                         scroll(bv, 0.0F);
697 #endif
698                 updateLocal(bv, CELL, false);
699 //              showInsetCursor(bv, false);
700                 return true;
701         }
702         if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
703                 if (inset->lyxCode() == TABULAR_CODE &&
704                     !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
705                         bv->owner()->getDialogs()->updateTabular(this);
706                         oldcell = actcell;
707                 }
708                 return true;
709         }
710         return false;
711 }
712
713
714 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
715 {
716         Inset * tl_inset = inset;
717         // look if this inset is really inside myself!
718         while(tl_inset->owner() && tl_inset->owner() != this)
719                 tl_inset = tl_inset->owner();
720         // if we enter here it's not ower inset
721         if (!tl_inset->owner())
722                 return false;
723         // we only have to do this if this is a subinset of our cells
724         if (tl_inset != inset) {
725                 if (!static_cast<InsetText *>(tl_inset)->updateInsetInInset(bv, inset))
726                         return false;
727         }
728         updateLocal(bv, CELL, false);
729         return true;
730 }
731
732
733 int InsetTabular::insetInInsetY() const
734 {
735         if (!the_locking_inset)
736                 return 0;
737         return inset_y + the_locking_inset->insetInInsetY();
738 }
739
740
741 UpdatableInset * InsetTabular::getLockingInset() const
742 {
743         return the_locking_inset ? the_locking_inset->getLockingInset() :
744                 const_cast<InsetTabular *>(this);
745 }
746
747
748 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
749 {
750         if (c == lyxCode())
751                 return this;
752         if (the_locking_inset)
753                 return the_locking_inset->getFirstLockingInsetOfType(c);
754         return 0;
755 }
756
757
758 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
759 {
760         if (the_locking_inset)
761                 return the_locking_inset->insertInset(bv, inset);
762         return false;
763 }
764
765
766 void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
767 {
768         if (hasSelection() && (button == 3))
769                 return;
770
771         if (hasSelection()) {
772                 clearSelection();
773                 updateLocal(bv, SELECTION, false);
774         }
775
776         int const ocell = actcell;
777         int const orow = actrow;
778
779         hideInsetCursor(bv);
780         if (!locked) {
781                 locked = true;
782                 the_locking_inset = 0;
783                 inset_x = 0;
784                 inset_y = 0;
785         }
786         setPos(bv, x, y);
787         if (actrow != orow)
788                 updateLocal(bv, NONE, false);
789         clearSelection();
790 #if 0
791         if (button == 3) {
792                 if ((ocell != actcell) && the_locking_inset) {
793                         the_locking_inset->insetUnlock(bv);
794                         the_locking_inset = 0;
795                 }
796                 showInsetCursor(bv);
797                 return;
798         }
799 #endif
800
801         bool const inset_hit = insetHit(bv, x, y);
802
803         if ((ocell == actcell) && the_locking_inset && inset_hit) {
804                 resetPos(bv);
805                 the_locking_inset->insetButtonPress(bv,
806                                                     x - inset_x, y - inset_y,
807                                                     button);
808                 return;
809         } else if (the_locking_inset) {
810                 the_locking_inset->insetUnlock(bv);
811                 the_locking_inset = 0;
812                 updateLocal(bv, CELL, false);
813         }
814         if (button == 2) {
815                 localDispatch(bv, LFUN_PASTESELECTION, "paragraph");
816                 return;
817         }
818         if (inset_hit && bv->theLockingInset()) {
819                 if (!bv->lockInset(static_cast<UpdatableInset*>(tabular->GetCellInset(actcell)))) {
820                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
821                         return;
822                 }
823                 the_locking_inset->insetButtonPress(
824                         bv, x - inset_x, y - inset_y, button);
825                 return;
826         }
827         showInsetCursor(bv);
828 }
829
830
831 bool InsetTabular::insetButtonRelease(BufferView * bv,
832                                       int x, int y, int button)
833 {
834         bool ret = false;
835         if (the_locking_inset)
836                 ret = the_locking_inset->insetButtonRelease(bv, x - inset_x,
837                                                                                                         y - inset_y, button);
838         if (button == 3 && !ret) {
839                 bv->owner()->getDialogs()->showTabular(this);
840                 return true;
841         }
842         return ret;
843 }
844
845
846 void InsetTabular::insetMotionNotify(BufferView * bv, int x, int y, int button)
847 {
848         if (the_locking_inset) {
849                 the_locking_inset->insetMotionNotify(bv,
850                                                      x - inset_x,
851                                                      y - inset_y,
852                                                      button);
853                 return;
854         }
855
856         hideInsetCursor(bv);
857 //      int const old_cell = actcell;
858
859         setPos(bv, x, y);
860         if (!hasSelection()) {
861                 setSelection(actcell, actcell);
862         } else {
863                 setSelection(sel_cell_start, actcell);
864         }
865         updateLocal(bv, SELECTION, false);
866         showInsetCursor(bv);
867 }
868
869
870 void InsetTabular::insetKeyPress(XKeyEvent * xke)
871 {
872         if (the_locking_inset) {
873                 the_locking_inset->insetKeyPress(xke);
874                 return;
875         }
876 }
877
878
879 UpdatableInset::RESULT
880 InsetTabular::localDispatch(BufferView * bv, kb_action action,
881                             string const & arg)
882 {
883         // We need to save the value of the_locking_inset as the call to
884         // the_locking_inset->LocalDispatch might unlock it.
885         old_locking_inset = the_locking_inset;
886         UpdatableInset::RESULT result =
887                 UpdatableInset::localDispatch(bv, action, arg);
888         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
889                 resetPos(bv);
890                 return result;
891         }
892
893         if ((action < 0) && arg.empty())
894                 return FINISHED;
895
896         bool hs = hasSelection();
897
898         result = DISPATCHED;
899         // this one have priority over the locked InsetText, if we're not already
900         // inside another tabular then that one get's priority!
901         if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) == this) {
902                 switch (action) {
903                 case LFUN_SHIFT_TAB:
904                 case LFUN_TAB:
905                         hideInsetCursor(bv);
906                         unlockInsetInInset(bv, the_locking_inset);
907                         if (action == LFUN_TAB)
908                                 moveNextCell(bv, old_locking_inset != 0);
909                         else
910                                 movePrevCell(bv, old_locking_inset != 0);
911                         clearSelection();
912                         if (hs)
913                                 updateLocal(bv, SELECTION, false);
914                         if (!the_locking_inset) {
915                                 showInsetCursor(bv);
916                                 return DISPATCHED_NOUPDATE;
917                         }
918                         return result;
919                 // this to avoid compiler warnings.
920                 default:
921                         break;
922                 }
923         }
924
925         if (the_locking_inset) {
926                 result = the_locking_inset->localDispatch(bv, action, arg);
927                 if (result == DISPATCHED_NOUPDATE) {
928                         int sc = scroll();
929                         resetPos(bv);
930                         if (sc != scroll()) { // inset has been scrolled
931                                 the_locking_inset->toggleInsetCursor(bv);
932                                 updateLocal(bv, FULL, false);
933                                 the_locking_inset->toggleInsetCursor(bv);
934                         }
935                         return result;
936                 } else if (result == DISPATCHED) {
937                         the_locking_inset->toggleInsetCursor(bv);
938                         updateLocal(bv, CELL, false);
939                         the_locking_inset->toggleInsetCursor(bv);
940                         return result;
941                 } else if (result == FINISHED_UP) {
942                         action = LFUN_UP;
943                 } else if (result == FINISHED_DOWN) {
944                         action = LFUN_DOWN;
945                 } else if (result == FINISHED_RIGHT) {
946                         action = LFUN_RIGHT;
947                 }
948         }
949
950         hideInsetCursor(bv);
951         result = DISPATCHED;
952         switch (action) {
953                 // --- Cursor Movements ----------------------------------
954         case LFUN_RIGHTSEL: {
955                 int const start = hasSelection() ? sel_cell_start : actcell;
956                 if (tabular->IsLastCellInRow(actcell)) {
957                         setSelection(start, actcell);
958                         break;
959                 }
960
961                 int end = actcell;
962                 // if we are starting a selection, only select
963                 // the current cell at the beginning
964                 if (hasSelection()) {
965                         moveRight(bv, false);
966                         end = actcell;
967                 }
968                 setSelection(start, end);
969                 updateLocal(bv, SELECTION, false);
970                 break;
971         }
972         case LFUN_RIGHT:
973                 result = moveRight(bv);
974                 clearSelection();
975                 if (hs)
976                         updateLocal(bv, SELECTION, false);
977                 break;
978         case LFUN_LEFTSEL: {
979                 int const start = hasSelection() ? sel_cell_start : actcell;
980                 if (tabular->IsFirstCellInRow(actcell)) {
981                         setSelection(start, actcell);
982                         break;
983                 }
984
985                 int end = actcell;
986                 // if we are starting a selection, only select
987                 // the current cell at the beginning
988                 if (hasSelection()) {
989                         moveLeft(bv, false);
990                         end = actcell;
991                 }
992                 setSelection(start, end);
993                 updateLocal(bv, SELECTION, false);
994                 break;
995         }
996         case LFUN_LEFT:
997                 result = moveLeft(bv);
998                 clearSelection();
999                 if (hs)
1000                         updateLocal(bv, SELECTION, false);
1001                 break;
1002         case LFUN_DOWNSEL: {
1003                 int const start = hasSelection() ? sel_cell_start : actcell;
1004                 int const ocell = actcell;
1005                 // if we are starting a selection, only select
1006                 // the current cell at the beginning
1007                 if (hasSelection()) {
1008                         moveDown(bv, false);
1009                         if ((ocell == sel_cell_end) ||
1010                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1011                                 setSelection(start, tabular->GetCellBelow(sel_cell_end));
1012                         else
1013                                 setSelection(start, tabular->GetLastCellBelow(sel_cell_end));
1014                 } else {
1015                         setSelection(start, start);
1016                 }
1017                 updateLocal(bv, SELECTION, false);
1018         }
1019         break;
1020         case LFUN_DOWN:
1021                 result = moveDown(bv, old_locking_inset != 0);
1022                 clearSelection();
1023                 if (hs) {
1024                         updateLocal(bv, SELECTION, false);
1025                 }
1026                 break;
1027         case LFUN_UPSEL: {
1028                 int const start = hasSelection() ? sel_cell_start : actcell;
1029                 int const ocell = actcell;
1030                 // if we are starting a selection, only select
1031                 // the current cell at the beginning
1032                 if (hasSelection()) {
1033                         moveUp(bv, false);
1034                         if ((ocell == sel_cell_end) ||
1035                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1036                                 setSelection(start, tabular->GetCellAbove(sel_cell_end));
1037                         else
1038                                 setSelection(start, tabular->GetLastCellAbove(sel_cell_end));
1039                 } else {
1040                         setSelection(start, start);
1041                 }
1042                 updateLocal(bv, SELECTION, false);
1043         }
1044         break;
1045         case LFUN_UP:
1046                 result = moveUp(bv, old_locking_inset != 0);
1047                 clearSelection();
1048                 if (hs)
1049                         updateLocal(bv, SELECTION, false);
1050                 break;
1051         case LFUN_NEXT: {
1052                 UpdateCodes code = CURSOR;
1053                 if (hs) {
1054                         clearSelection();
1055                         code = SELECTION;
1056                 }
1057                 int column = actcol;
1058                 unlockInsetInInset(bv, the_locking_inset);
1059                 if (bv->text->first_y + bv->painter().paperHeight() <
1060                     (top_baseline + tabular->GetHeightOfTabular()))
1061                         {
1062                                 bv->scrollCB(bv->text->first_y + bv->painter().paperHeight());
1063                                 code = FULL;
1064                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1065                         } else {
1066                                 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
1067                         }
1068                 resetPos(bv);
1069                 updateLocal(bv, code, false);
1070                 break;
1071         }
1072         case LFUN_PRIOR: {
1073                 UpdateCodes code = CURSOR;
1074                 if (hs) {
1075                         clearSelection();
1076                         code = SELECTION;
1077                 }
1078                 int column = actcol;
1079                 unlockInsetInInset(bv, the_locking_inset);
1080                 if (top_baseline < 0) {
1081                         bv->scrollCB(bv->text->first_y - bv->painter().paperHeight());
1082                         code = FULL;
1083                         if (top_baseline > 0)
1084                                 actcell = column;
1085                         else
1086                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1087                 } else {
1088                         actcell = column;
1089                 }
1090                 resetPos(bv);
1091                 updateLocal(bv, code, false);
1092                 break;
1093         }
1094         // none of these make sense for insettabular,
1095         // but we must catch them to prevent any
1096         // selection from being confused
1097         case LFUN_PRIORSEL:
1098         case LFUN_NEXTSEL:
1099         case LFUN_WORDLEFT:
1100         case LFUN_WORDLEFTSEL:
1101         case LFUN_WORDRIGHT:
1102         case LFUN_WORDRIGHTSEL:
1103         case LFUN_DOWN_PARAGRAPH:
1104         case LFUN_DOWN_PARAGRAPHSEL:
1105         case LFUN_UP_PARAGRAPH:
1106         case LFUN_UP_PARAGRAPHSEL:
1107         case LFUN_BACKSPACE:
1108         case LFUN_DELETE:
1109         case LFUN_HOME:
1110         case LFUN_HOMESEL:
1111         case LFUN_END:
1112         case LFUN_ENDSEL:
1113         case LFUN_BEGINNINGBUF:
1114         case LFUN_BEGINNINGBUFSEL:
1115         case LFUN_ENDBUF:
1116         case LFUN_ENDBUFSEL:
1117                 break;
1118         case LFUN_LAYOUT_TABULAR:
1119                 bv->owner()->getDialogs()->showTabular(this);
1120                 break;
1121         case LFUN_TABULAR_FEATURE:
1122                 if (!tabularFeatures(bv, arg))
1123                         result = UNDISPATCHED;
1124                 break;
1125                 // insert file functions
1126         case LFUN_FILE_INSERT_ASCII_PARA:
1127         case LFUN_FILE_INSERT_ASCII:
1128         {
1129                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
1130                 if (tmpstr.empty())
1131                         break;
1132                 if (insertAsciiString(bv, tmpstr, false))
1133                         updateLocal(bv, INIT, true);
1134                 else
1135                         result = UNDISPATCHED;
1136                 break;
1137         }
1138         // cut and paste functions
1139         case LFUN_CUT:
1140                 if (!copySelection(bv))
1141                         break;
1142                 setUndo(bv, Undo::DELETE,
1143                         bv->text->cursor.par(),
1144                         bv->text->cursor.par()->next());
1145                 cutSelection();
1146                 updateLocal(bv, INIT, true);
1147                 break;
1148         case LFUN_COPY:
1149                 if (!hasSelection())
1150                         break;
1151                 finishUndo();
1152                 copySelection(bv);
1153                 break;
1154         case LFUN_PASTESELECTION:
1155         {
1156                 string const clip(bv->getClipboard());
1157                         if (clip.empty())
1158                         break;
1159 #if 0
1160                 if (clip.find('\t') != string::npos) {
1161                         int cols = 1;
1162                         int rows = 1;
1163                         int maxCols = 1;
1164                         string::size_type len = clip.length();
1165                         string::size_type p = 0;
1166
1167                         while (p < len &&
1168                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1169                                 switch (clip[p]) {
1170                                 case '\t':
1171                                         ++cols;
1172                                         break;
1173                                 case '\n':
1174                                         if ((p+1) < len)
1175                                                 ++rows;
1176                                         maxCols = max(cols, maxCols);
1177                                         cols = 1;
1178                                         break;
1179                                 }
1180                                 ++p;
1181                         }
1182                         maxCols = max(cols, maxCols);
1183                         delete paste_tabular;
1184                         paste_tabular = new LyXTabular(bv->buffer()->params,
1185                                                        this, rows, maxCols);
1186                         string::size_type op = 0;
1187                         int cell = 0;
1188                         int cells = paste_tabular->GetNumberOfCells();
1189                         p = cols = 0;
1190                         while ((cell < cells) && (p < len) &&
1191                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1192                                 if (p >= len)
1193                                         break;
1194                                 switch (clip[p]) {
1195                                 case '\t':
1196                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1197                                         ++cols;
1198                                         ++cell;
1199                                         break;
1200                                 case '\n':
1201                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1202                                         while (cols++ < maxCols)
1203                                                 ++cell;
1204                                         cols = 0;
1205                                         break;
1206                                 }
1207                                 ++p;
1208                                 op = p;
1209                         }
1210                         // check for the last cell if there is no trailing '\n'
1211                         if ((cell < cells) && (op < len))
1212                                 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1213                 } else
1214 #else
1215                 if (!insertAsciiString(bv, clip, true))
1216 #endif
1217                 {
1218                         // so that the clipboard is used and it goes on
1219                         // to default
1220                         // and executes LFUN_PASTESELECTION in insettext!
1221                         delete paste_tabular;
1222                         paste_tabular = 0;
1223                 }
1224         }
1225         case LFUN_PASTE:
1226                 if (hasPasteBuffer()) {
1227                         setUndo(bv, Undo::INSERT,
1228                                 bv->text->cursor.par(),
1229                                 bv->text->cursor.par()->next());
1230                         pasteSelection(bv);
1231                         updateLocal(bv, INIT, true);
1232                         break;
1233                 }
1234                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1235         default:
1236                 // handle font changing stuff on selection before we lock the inset
1237                 // in the default part!
1238                 result = UNDISPATCHED;
1239                 if (hs) {
1240                         switch(action) {
1241                         case LFUN_LANGUAGE:
1242                         case LFUN_EMPH:
1243                         case LFUN_BOLD:
1244                         case LFUN_NOUN:
1245                         case LFUN_CODE:
1246                         case LFUN_SANS:
1247                         case LFUN_ROMAN:
1248                         case LFUN_DEFAULT:
1249                         case LFUN_UNDERLINE:
1250                         case LFUN_FONT_SIZE:
1251                                 if (bv->Dispatch(action, arg))
1252                                         result = DISPATCHED;
1253                                 break;
1254                         default:
1255                                 break;
1256                         }
1257                 }
1258                 // we try to activate the actual inset and put this event down to
1259                 // the insets dispatch function.
1260                 if ((result == DISPATCHED) || the_locking_inset)
1261                         break;
1262                 nodraw(true);
1263                 if (activateCellInset(bv)) {
1264                         // reset need_update setted in above function!
1265                         need_update = NONE;
1266                         result = the_locking_inset->localDispatch(bv, action, arg);
1267                         if ((result == UNDISPATCHED) || (result >= FINISHED)) {
1268                                 unlockInsetInInset(bv, the_locking_inset);
1269                                 nodraw(false);
1270                                 // we need to update if this was requested before
1271                                 updateLocal(bv, NONE, false);
1272                                 return UNDISPATCHED;
1273                         } else if (hs) {
1274                                 clearSelection();
1275                                 // so the below CELL is not set because this is higher
1276                                 // priority and we get a full redraw
1277                                 need_update = SELECTION;
1278                         }
1279                         nodraw(false);
1280                         updateLocal(bv, CELL, false);
1281                         return result;
1282                 }
1283                 break;
1284         }
1285         if (result < FINISHED) {
1286                 if (!the_locking_inset) {
1287                         if (bv->fitCursor())
1288                                 updateLocal(bv, FULL, false);
1289                         if (locked)
1290                                 showInsetCursor(bv);
1291                 }
1292         } else
1293                 bv->unlockInset(this);
1294         return result;
1295 }
1296
1297
1298 int InsetTabular::latex(Buffer const * buf, ostream & os,
1299                         bool fragile, bool fp) const
1300 {
1301         return tabular->latex(buf, os, fragile, fp);
1302 }
1303
1304
1305 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1306 {
1307         if (ll > 0)
1308                 return tabular->ascii(buf, os, (int)parOwner()->params().depth(),
1309                                       false,0);
1310         return tabular->ascii(buf, os, 0, false,0);
1311 }
1312
1313
1314 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1315 {
1316         os << "<![CDATA[";
1317         int const ret = tabular->ascii(buf,os,
1318                                        (int)parOwner()->params().depth(),
1319                                        false, 0);
1320         os << "]]>";
1321         return ret;
1322 }
1323
1324
1325 int InsetTabular::docbook(Buffer const * buf, ostream & os) const
1326 {
1327         int ret = 0;
1328         Inset * master;
1329
1330         // if the table is inside a float it doesn't need the informaltable
1331         // wrapper. Search for it.
1332         for(master = owner();
1333             master && master->lyxCode() != Inset::FLOAT_CODE;
1334             master = master->owner());
1335
1336         if (!master) {
1337                 os << "<informaltable>\n";
1338                 ret++;
1339         }
1340         ret+= tabular->docBook(buf,os);
1341         if (!master) {
1342                 os << "</informaltable>\n";
1343                 ret++;
1344         }
1345         return ret;
1346 }
1347
1348
1349 void InsetTabular::validate(LaTeXFeatures & features) const
1350 {
1351         tabular->Validate(features);
1352 }
1353
1354
1355 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1356                                                  LyXFont const & font,
1357                                                  bool reinit) const
1358 {
1359         int cell = -1;
1360         int maxAsc = 0;
1361         int maxDesc = 0;
1362         InsetText * inset;
1363         bool changed = false;
1364
1365         // if we have a locking_inset we should have to check only this cell for
1366         // change so I'll try this to have a boost, but who knows ;)
1367         if ((need_update != INIT) &&
1368             (the_locking_inset == tabular->GetCellInset(actcell))) {
1369                 for(int i = 0; i < tabular->columns(); ++i) {
1370                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1371                                      maxAsc);
1372                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1373                                       maxDesc);
1374                 }
1375                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1376                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1377                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1378                 return changed;
1379         }
1380         for (int i = 0; i < tabular->rows(); ++i) {
1381                 maxAsc = 0;
1382                 maxDesc = 0;
1383                 for (int j = 0; j < tabular->columns(); ++j) {
1384                         if (tabular->IsPartOfMultiColumn(i,j))
1385                                 continue;
1386                         ++cell;
1387                         inset = tabular->GetCellInset(cell);
1388                         if (!reinit && !tabular->GetPWidth(cell).zero())
1389                                 inset->update(bv, font, false);
1390                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1391                         maxDesc = max(maxDesc, inset->descent(bv, font));
1392                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1393                 }
1394                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1395                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1396         }
1397         if (changed)
1398                 tabular->reinit();
1399         return changed;
1400 }
1401
1402
1403 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1404 {
1405         if (the_locking_inset) {
1406                 the_locking_inset->getCursorPos(bv, x, y);
1407                 return;
1408         }
1409         x = cursor_.x() - top_x;
1410         y = cursor_.y();
1411 }
1412
1413
1414 void InsetTabular::toggleInsetCursor(BufferView * bv)
1415 {
1416         if (nodraw()) {
1417                 if (isCursorVisible())
1418                         bv->hideLockedInsetCursor();
1419                 return;
1420         }
1421         if (the_locking_inset) {
1422                 the_locking_inset->toggleInsetCursor(bv);
1423                 return;
1424         }
1425
1426         LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1427
1428         int const asc = lyxfont::maxAscent(font);
1429         int const desc = lyxfont::maxDescent(font);
1430
1431         if (isCursorVisible())
1432                 bv->hideLockedInsetCursor();
1433         else
1434                 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1435         toggleCursorVisible();
1436 }
1437
1438
1439 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1440 {
1441         if (nodraw())
1442                 return;
1443         if (!isCursorVisible()) {
1444                 LyXFont font; // = GetFont(par, cursor.pos);
1445
1446                 int const asc = lyxfont::maxAscent(font);
1447                 int const desc = lyxfont::maxDescent(font);
1448                 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1449                 if (show)
1450                         bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1451                 setCursorVisible(true);
1452         }
1453 }
1454
1455
1456 void InsetTabular::hideInsetCursor(BufferView * bv)
1457 {
1458         if (isCursorVisible()) {
1459                 bv->hideLockedInsetCursor();
1460                 setCursorVisible(false);
1461         }
1462 }
1463
1464
1465 void InsetTabular::fitInsetCursor(BufferView * bv) const
1466 {
1467         if (the_locking_inset) {
1468                 int old_first_y = bv->text->first_y;
1469                 the_locking_inset->fitInsetCursor(bv);
1470                 if (old_first_y != bv->text->first_y)
1471                         need_update = FULL;
1472                 return;
1473         }
1474         LyXFont font;
1475
1476         int const asc = lyxfont::maxAscent(font);
1477         int const desc = lyxfont::maxDescent(font);
1478         if (bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc))
1479                 need_update = FULL;
1480 }
1481
1482
1483 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1484 {
1485         cursor_.y(0);
1486
1487         actcell = actrow = actcol = 0;
1488         int ly = tabular->GetDescentOfRow(actrow);
1489
1490         // first search the right row
1491         while ((ly < y) && ((actrow+1) < tabular->rows())) {
1492                 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1493                                  tabular->GetAscentOfRow(actrow + 1) +
1494                                  tabular->GetAdditionalHeight(actrow + 1));
1495                 ++actrow;
1496                 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1497         }
1498         actcell = tabular->GetCellNumber(actrow, actcol);
1499
1500         // now search the right column
1501         int lx = tabular->GetWidthOfColumn(actcell) -
1502                 tabular->GetAdditionalWidth(actcell);
1503         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1504                 lx += tabular->GetWidthOfColumn(actcell + 1)
1505                         + tabular->GetAdditionalWidth(actcell);
1506         }
1507         cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1508         resetPos(bv);
1509 }
1510
1511
1512 int InsetTabular::getCellXPos(int cell) const
1513 {
1514         int c = cell;
1515
1516         for (; !tabular->IsFirstCellInRow(c); --c)
1517                 ;
1518         int lx = tabular->GetWidthOfColumn(cell);
1519         for (; c < cell; ++c) {
1520                 lx += tabular->GetWidthOfColumn(c);
1521         }
1522         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1523 }
1524
1525
1526 void InsetTabular::resetPos(BufferView * bv) const
1527 {
1528 #ifdef WITH_WARNINGS
1529 #warning This should be fixed in the right manner (20011128 Jug)
1530 #endif
1531         // fast hack to fix infinite repaintings!
1532         if (in_reset_pos)
1533                 return;
1534
1535         int cell = 0;
1536         actcol = tabular->column_of_cell(actcell);
1537         actrow = 0;
1538         cursor_.y(0);
1539         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1540                 if (tabular->IsLastCellInRow(cell)) {
1541                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1542                                          tabular->GetAscentOfRow(actrow + 1) +
1543                                          tabular->GetAdditionalHeight(actrow + 1));
1544                         ++actrow;
1545                 }
1546         }
1547         if (!locked || nodraw()) {
1548                 if (the_locking_inset)
1549                         inset_y = cursor_.y();
1550                 return;
1551         }
1552         // we need this only from here on!!!
1553         in_reset_pos = true;
1554         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1555         int new_x = getCellXPos(actcell);
1556         int old_x = cursor_.x();
1557         new_x += offset;
1558         cursor_.x(new_x);
1559 //    cursor.x(getCellXPos(actcell) + offset);
1560         if ((actcol < tabular->columns()-1) && scroll(false) &&
1561                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1562         {
1563                 scroll(bv, 0.0F);
1564                 updateLocal(bv, FULL, false);
1565         } else if (the_locking_inset &&
1566                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1567         {
1568                 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1569                 if (xx > (bv->workWidth()-20)) {
1570                         scroll(bv, -(xx - bv->workWidth() + 60));
1571                         updateLocal(bv, FULL, false);
1572                 } else if (xx < 20) {
1573                         if (xx < 0)
1574                                 xx = -xx + 60;
1575                         else
1576                                 xx = 60;
1577                         scroll(bv, xx);
1578                         updateLocal(bv, FULL, false);
1579                 }
1580         } else if ((cursor_.x() - offset) > 20 &&
1581                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1582                    > (bv->workWidth() - 20)) {
1583                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1584                 updateLocal(bv, FULL, false);
1585         } else if ((cursor_.x() - offset) < 20) {
1586                 scroll(bv, 20 - cursor_.x() + offset);
1587                 updateLocal(bv, FULL, false);
1588         } else if (scroll(false) && top_x > 20 &&
1589                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1590                 scroll(bv, old_x - cursor_.x());
1591                 updateLocal(bv, FULL, false);
1592         }
1593         if (the_locking_inset) {
1594                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1595                 inset_y = cursor_.y();
1596         }
1597         if ((!the_locking_inset ||
1598              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1599             actcell != oldcell) {
1600                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1601                 bv->owner()->getDialogs()->updateTabular(inset);
1602                 oldcell = actcell;
1603         }
1604         in_reset_pos = false;
1605 }
1606
1607
1608 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1609 {
1610         if (lock && !old_locking_inset) {
1611                 if (activateCellInset(bv))
1612                         return DISPATCHED;
1613         } else {
1614                 bool moved = isRightToLeft(bv)
1615                         ? movePrevCell(bv) : moveNextCell(bv);
1616                 if (!moved)
1617                         return FINISHED_RIGHT;
1618                 if (lock && activateCellInset(bv))
1619                         return DISPATCHED;
1620         }
1621         resetPos(bv);
1622         return DISPATCHED_NOUPDATE;
1623 }
1624
1625
1626 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1627 {
1628         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1629         if (!moved)
1630                 return FINISHED;
1631         if (lock) {       // behind the inset
1632                 if (activateCellInset(bv, 0, 0, 0, true))
1633                         return DISPATCHED;
1634         }
1635         resetPos(bv);
1636         return DISPATCHED_NOUPDATE;
1637 }
1638
1639
1640 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1641 {
1642         int const ocell = actcell;
1643         actcell = tabular->GetCellAbove(actcell);
1644         if (actcell == ocell) // we moved out of the inset
1645                 return FINISHED_UP;
1646         resetPos(bv);
1647         if (lock) {
1648                 int x = 0;
1649                 int y = 0;
1650                 if (old_locking_inset) {
1651                         old_locking_inset->getCursorPos(bv, x, y);
1652                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1653                 }
1654                 if (activateCellInset(bv, x, 0))
1655                         return DISPATCHED;
1656         }
1657         return DISPATCHED_NOUPDATE;
1658 }
1659
1660
1661 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1662 {
1663         int const ocell = actcell;
1664         actcell = tabular->GetCellBelow(actcell);
1665         if (actcell == ocell) // we moved out of the inset
1666                 return FINISHED_DOWN;
1667         resetPos(bv);
1668         if (lock) {
1669                 int x = 0;
1670                 int y = 0;
1671                 if (old_locking_inset) {
1672                         old_locking_inset->getCursorPos(bv, x, y);
1673                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1674                 }
1675                 if (activateCellInset(bv, x, 0))
1676                         return DISPATCHED;
1677         }
1678         return DISPATCHED_NOUPDATE;
1679 }
1680
1681
1682 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1683 {
1684         if (isRightToLeft(bv)) {
1685                 if (tabular->IsFirstCellInRow(actcell)) {
1686                         int row = tabular->row_of_cell(actcell);
1687                         if (row == tabular->rows() - 1)
1688                                 return false;
1689                         actcell = tabular->GetLastCellInRow(row);
1690                         actcell = tabular->GetCellBelow(actcell);
1691                 } else {
1692                         if (!actcell)
1693                                 return false;
1694                         --actcell;
1695                 }
1696         } else {
1697                 if (tabular->IsLastCell(actcell))
1698                         return false;
1699                 ++actcell;
1700         }
1701         if (lock) {
1702                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1703                         isRightToLeftPar(bv->buffer()->params);
1704                 activateCellInset(bv, 0, 0, 0, !rtl);
1705         }
1706         resetPos(bv);
1707         return true;
1708 }
1709
1710
1711 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1712 {
1713         if (isRightToLeft(bv)) {
1714                 if (tabular->IsLastCellInRow(actcell)) {
1715                         int row = tabular->row_of_cell(actcell);
1716                         if (row == 0)
1717                                 return false;
1718                         actcell = tabular->GetFirstCellInRow(row);
1719                         actcell = tabular->GetCellAbove(actcell);
1720                 } else {
1721                         if (tabular->IsLastCell(actcell))
1722                                 return false;
1723                         ++actcell;
1724                 }
1725         } else {
1726                 if (!actcell) // first cell
1727                         return false;
1728                 --actcell;
1729         }
1730         if (lock) {
1731                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1732                         isRightToLeftPar(bv->buffer()->params);
1733                 activateCellInset(bv, 0, 0, 0, !rtl);
1734         }
1735         resetPos(bv);
1736         return true;
1737 }
1738
1739
1740 bool InsetTabular::deletable() const
1741 {
1742         return true;
1743 }
1744
1745
1746 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1747                            bool selectall)
1748 {
1749         if (selectall) {
1750                 setSelection(0, tabular->GetNumberOfCells() - 1);
1751         }
1752         if (hasSelection()) {
1753                 setUndo(bv, Undo::EDIT,
1754                         bv->text->cursor.par(),
1755                         bv->text->cursor.par()->next());
1756                 bool frozen = undo_frozen;
1757                 if (!frozen)
1758                         freezeUndo();
1759                 // apply the fontchange on the whole selection
1760                 int sel_row_start;
1761                 int sel_row_end;
1762                 int sel_col_start;
1763                 int sel_col_end;
1764                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1765                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1766                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1767                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1768                         }
1769                 }
1770                 if (!frozen)
1771                         unFreezeUndo();
1772                 updateLocal(bv, INIT, true);
1773         }
1774         if (the_locking_inset)
1775                 the_locking_inset->setFont(bv, font, tall);
1776 }
1777
1778
1779 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1780 {
1781         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1782
1783         int i = 0;
1784         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1785                 string const tmp = tabularFeature[i].feature;
1786
1787                 if (tmp == what.substr(0, tmp.length())) {
1788                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1789                         //tabularFeatures[i].feature.length())) {
1790                         action = tabularFeature[i].action;
1791                         break;
1792                 }
1793         }
1794         if (action == LyXTabular::LAST_ACTION)
1795                 return false;
1796
1797         string const val =
1798                 frontStrip(what.substr(tabularFeature[i].feature.length()));
1799         tabularFeatures(bv, action, val);
1800         return true;
1801 }
1802
1803 static void checkLongtableSpecial(LyXTabular::ltType & ltt,
1804                                   string const & special, bool & flag)
1805 {
1806         if (special == "dl_above") {
1807                 ltt.topDL = flag;
1808                 ltt.set = false;
1809         } else if (special == "dl_below") {
1810                 ltt.bottomDL = flag;
1811                 ltt.set = false;
1812         } else if (special == "empty") {
1813                 ltt.empty = flag;
1814                 ltt.set = false;
1815         } else if (flag) {
1816                 ltt.empty = false;
1817                 ltt.set = true;
1818         }
1819 }
1820
1821
1822 void InsetTabular::tabularFeatures(BufferView * bv,
1823                                    LyXTabular::Feature feature,
1824                                    string const & value)
1825 {
1826         int sel_col_start;
1827         int sel_col_end;
1828         int sel_row_start;
1829         int sel_row_end;
1830         bool setLines = false;
1831         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1832         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1833
1834         switch (feature) {
1835         case LyXTabular::M_ALIGN_LEFT:
1836         case LyXTabular::ALIGN_LEFT:
1837                 setAlign = LYX_ALIGN_LEFT;
1838                 break;
1839         case LyXTabular::M_ALIGN_RIGHT:
1840         case LyXTabular::ALIGN_RIGHT:
1841                 setAlign = LYX_ALIGN_RIGHT;
1842                 break;
1843         case LyXTabular::M_ALIGN_CENTER:
1844         case LyXTabular::ALIGN_CENTER:
1845                 setAlign = LYX_ALIGN_CENTER;
1846                 break;
1847         case LyXTabular::M_VALIGN_TOP:
1848         case LyXTabular::VALIGN_TOP:
1849                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1850                 break;
1851         case LyXTabular::M_VALIGN_BOTTOM:
1852         case LyXTabular::VALIGN_BOTTOM:
1853                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1854                 break;
1855         case LyXTabular::M_VALIGN_CENTER:
1856         case LyXTabular::VALIGN_CENTER:
1857                 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1858                 break;
1859         default:
1860                 break;
1861         }
1862         if (hasSelection()) {
1863                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1864         } else {
1865                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1866                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1867         }
1868         setUndo(bv, Undo::FINISH,
1869                 bv->text->cursor.par(),
1870                 bv->text->cursor.par()->next());
1871
1872         int row =  tabular->row_of_cell(actcell);
1873         int column = tabular->column_of_cell(actcell);
1874         bool flag = true;
1875         LyXTabular::ltType ltt;
1876
1877         switch (feature) {
1878         case LyXTabular::SET_PWIDTH:
1879         {
1880                 LyXLength const vallen(value);
1881                 LyXLength const & tmplen = tabular->GetColumnPWidth(actcell);
1882
1883                 bool const update = (tmplen != vallen);
1884                 tabular->SetColumnPWidth(actcell, vallen);
1885                 if (update) {
1886                         int cell;
1887                         for (int i = 0; i < tabular->rows(); ++i) {
1888                                 cell = tabular->GetCellNumber(i,column);
1889                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1890                         }
1891                         updateLocal(bv, INIT, true);
1892                 }
1893         }
1894         break;
1895         case LyXTabular::SET_MPWIDTH:
1896         {
1897                 LyXLength const vallen(value);
1898                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1899
1900                 bool const update = (tmplen != vallen);
1901                 tabular->SetMColumnPWidth(actcell, vallen);
1902                 if (update) {
1903                         for (int i = 0; i < tabular->rows(); ++i) {
1904                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1905                                         resizeLyXText(bv);
1906                         }
1907                         updateLocal(bv, INIT, true);
1908                 }
1909         }
1910         break;
1911         case LyXTabular::SET_SPECIAL_COLUMN:
1912         case LyXTabular::SET_SPECIAL_MULTI:
1913                 tabular->SetAlignSpecial(actcell,value,feature);
1914                 updateLocal(bv, FULL, true);
1915                 break;
1916         case LyXTabular::APPEND_ROW:
1917                 // append the row into the tabular
1918                 unlockInsetInInset(bv, the_locking_inset);
1919                 tabular->AppendRow(bv->buffer()->params, actcell);
1920                 updateLocal(bv, INIT, true);
1921                 break;
1922         case LyXTabular::APPEND_COLUMN:
1923                 // append the column into the tabular
1924                 unlockInsetInInset(bv, the_locking_inset);
1925                 tabular->AppendColumn(bv->buffer()->params, actcell);
1926                 actcell = tabular->GetCellNumber(row, column);
1927                 updateLocal(bv, INIT, true);
1928                 break;
1929         case LyXTabular::DELETE_ROW:
1930                 unlockInsetInInset(bv, the_locking_inset);
1931                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1932                         tabular->DeleteRow(sel_row_start);
1933                 }
1934                 if (sel_row_start >= tabular->rows())
1935                         --sel_row_start;
1936                 actcell = tabular->GetCellNumber(sel_row_start, column);
1937                 clearSelection();
1938                 updateLocal(bv, INIT, true);
1939                 break;
1940         case LyXTabular::DELETE_COLUMN:
1941                 unlockInsetInInset(bv, the_locking_inset);
1942                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1943                         tabular->DeleteColumn(sel_col_start);
1944                 }
1945                 if (sel_col_start >= tabular->columns())
1946                         --sel_col_start;
1947                 actcell = tabular->GetCellNumber(row, sel_col_start);
1948                 clearSelection();
1949                 updateLocal(bv, INIT, true);
1950                 break;
1951         case LyXTabular::M_TOGGLE_LINE_TOP:
1952                 flag = false;
1953         case LyXTabular::TOGGLE_LINE_TOP:
1954         {
1955                 bool lineSet = !tabular->TopLine(actcell, flag);
1956                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1957                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1958                                 tabular->SetTopLine(
1959                                         tabular->GetCellNumber(i, j),
1960                                         lineSet, flag);
1961                 updateLocal(bv, INIT, true);
1962                 break;
1963         }
1964
1965         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1966                 flag = false;
1967         case LyXTabular::TOGGLE_LINE_BOTTOM:
1968         {
1969                 bool lineSet = !tabular->BottomLine(actcell, flag);
1970                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1971                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1972                                 tabular->SetBottomLine(
1973                                         tabular->GetCellNumber(i, j),
1974                                         lineSet,
1975                                         flag);
1976                 updateLocal(bv, INIT, true);
1977                 break;
1978         }
1979
1980         case LyXTabular::M_TOGGLE_LINE_LEFT:
1981                 flag = false;
1982         case LyXTabular::TOGGLE_LINE_LEFT:
1983         {
1984                 bool lineSet = !tabular->LeftLine(actcell, flag);
1985                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1986                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1987                                 tabular->SetLeftLine(
1988                                         tabular->GetCellNumber(i,j),
1989                                         lineSet,
1990                                         flag);
1991                 updateLocal(bv, INIT, true);
1992                 break;
1993         }
1994
1995         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1996                 flag = false;
1997         case LyXTabular::TOGGLE_LINE_RIGHT:
1998         {
1999                 bool lineSet = !tabular->RightLine(actcell, flag);
2000                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2001                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2002                                 tabular->SetRightLine(
2003                                         tabular->GetCellNumber(i,j),
2004                                         lineSet,
2005                                         flag);
2006                 updateLocal(bv, INIT, true);
2007                 break;
2008         }
2009
2010         case LyXTabular::M_ALIGN_LEFT:
2011         case LyXTabular::M_ALIGN_RIGHT:
2012         case LyXTabular::M_ALIGN_CENTER:
2013                 flag = false;
2014         case LyXTabular::ALIGN_LEFT:
2015         case LyXTabular::ALIGN_RIGHT:
2016         case LyXTabular::ALIGN_CENTER:
2017                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2018                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2019                                 tabular->SetAlignment(
2020                                         tabular->GetCellNumber(i, j),
2021                                         setAlign,
2022                                         flag);
2023                 updateLocal(bv, INIT, true);
2024                 break;
2025         case LyXTabular::M_VALIGN_TOP:
2026         case LyXTabular::M_VALIGN_BOTTOM:
2027         case LyXTabular::M_VALIGN_CENTER:
2028                 flag = false;
2029         case LyXTabular::VALIGN_TOP:
2030         case LyXTabular::VALIGN_BOTTOM:
2031         case LyXTabular::VALIGN_CENTER:
2032                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2033                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2034                                 tabular->SetVAlignment(
2035                                         tabular->GetCellNumber(i, j),
2036                                         setVAlign, flag);
2037                 updateLocal(bv, INIT, true);
2038                 break;
2039         case LyXTabular::MULTICOLUMN:
2040         {
2041                 if (sel_row_start != sel_row_end) {
2042                         Alert::alert(_("Impossible Operation!"),
2043                                    _("Multicolumns can only be horizontally."),
2044                                    _("Sorry."));
2045                         return;
2046                 }
2047                 // just multicol for one Single Cell
2048                 if (!hasSelection()) {
2049                         // check wether we are completly in a multicol
2050                         if (tabular->IsMultiColumn(actcell)) {
2051                                 tabular->UnsetMultiColumn(actcell);
2052                                 updateLocal(bv, INIT, true);
2053                         } else {
2054                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
2055                                 updateLocal(bv, CELL, true);
2056                         }
2057                         return;
2058                 }
2059                 // we have a selection so this means we just add all this
2060                 // cells to form a multicolumn cell
2061                 int s_start;
2062                 int s_end;
2063
2064                 if (sel_cell_start > sel_cell_end) {
2065                         s_start = sel_cell_end;
2066                         s_end = sel_cell_start;
2067                 } else {
2068                         s_start = sel_cell_start;
2069                         s_end = sel_cell_end;
2070                 }
2071                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
2072                 actcell = s_start;
2073                 clearSelection();
2074                 updateLocal(bv, INIT, true);
2075                 break;
2076         }
2077         case LyXTabular::SET_ALL_LINES:
2078                 setLines = true;
2079         case LyXTabular::UNSET_ALL_LINES:
2080                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2081                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2082                                 tabular->SetAllLines(
2083                                         tabular->GetCellNumber(i,j), setLines);
2084                 updateLocal(bv, INIT, true);
2085                 break;
2086         case LyXTabular::SET_LONGTABULAR:
2087                 tabular->SetLongTabular(true);
2088                 updateLocal(bv, INIT, true); // because this toggles displayed
2089                 break;
2090         case LyXTabular::UNSET_LONGTABULAR:
2091                 tabular->SetLongTabular(false);
2092                 updateLocal(bv, INIT, true); // because this toggles displayed
2093                 break;
2094         case LyXTabular::SET_ROTATE_TABULAR:
2095                 tabular->SetRotateTabular(true);
2096                 break;
2097         case LyXTabular::UNSET_ROTATE_TABULAR:
2098                 tabular->SetRotateTabular(false);
2099                 break;
2100         case LyXTabular::SET_ROTATE_CELL:
2101                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2102                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2103                                 tabular->SetRotateCell(
2104                                         tabular->GetCellNumber(i, j),
2105                                         true);
2106                 break;
2107         case LyXTabular::UNSET_ROTATE_CELL:
2108                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2109                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2110                                 tabular->SetRotateCell(
2111                                         tabular->GetCellNumber(i, j), false);
2112                 break;
2113         case LyXTabular::SET_USEBOX:
2114         {
2115                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2116                 if (val == tabular->GetUsebox(actcell))
2117                         val = LyXTabular::BOX_NONE;
2118                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2119                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2120                                 tabular->SetUsebox(
2121                                         tabular->GetCellNumber(i, j), val);
2122                 break;
2123         }
2124         case LyXTabular::UNSET_LTFIRSTHEAD:
2125                 flag = false;
2126         case LyXTabular::SET_LTFIRSTHEAD:
2127                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2128                 checkLongtableSpecial(ltt, value, flag);
2129                 tabular->SetLTHead(row, flag, ltt, true);
2130                 break;
2131         case LyXTabular::UNSET_LTHEAD:
2132                 flag = false;
2133         case LyXTabular::SET_LTHEAD:
2134                 (void)tabular->GetRowOfLTHead(row, ltt);
2135                 checkLongtableSpecial(ltt, value, flag);
2136                 tabular->SetLTHead(row, flag, ltt, false);
2137                 break;
2138         case LyXTabular::UNSET_LTFOOT:
2139                 flag = false;
2140         case LyXTabular::SET_LTFOOT:
2141                 (void)tabular->GetRowOfLTFoot(row, ltt);
2142                 checkLongtableSpecial(ltt, value, flag);
2143                 tabular->SetLTFoot(row, flag, ltt, false);
2144                 break;
2145         case LyXTabular::UNSET_LTLASTFOOT:
2146                 flag = false;
2147         case LyXTabular::SET_LTLASTFOOT:
2148                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2149                 checkLongtableSpecial(ltt, value, flag);
2150                 tabular->SetLTFoot(row, flag, ltt, true);
2151                 break;
2152         case LyXTabular::SET_LTNEWPAGE:
2153         {
2154                 bool what = !tabular->GetLTNewPage(row);
2155                 tabular->SetLTNewPage(row, what);
2156                 break;
2157         }
2158         // dummy stuff just to avoid warnings
2159         case LyXTabular::LAST_ACTION:
2160                 break;
2161         }
2162 }
2163
2164
2165 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
2166                                      bool behind)
2167 {
2168         UpdatableInset * inset =
2169                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2170         LyXFont font(LyXFont::ALL_SANE);
2171         if (behind) {
2172                 x = inset->x() + inset->width(bv, font);
2173                 y = inset->descent(bv, font);
2174         }
2175         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2176         //inset_y = cursor.y();
2177         inset->edit(bv, x,  y, button);
2178         if (!the_locking_inset)
2179                 return false;
2180         updateLocal(bv, CELL, false);
2181         return (the_locking_inset != 0);
2182 }
2183
2184
2185 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2186                                         int button)
2187 {
2188         inset_x = cursor_.x()
2189                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2190         inset_y = cursor_.y();
2191         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2192 }
2193
2194
2195 bool InsetTabular::insetHit(BufferView *, int x, int) const
2196 {
2197         return (x + top_x)
2198                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2199 }
2200
2201
2202 // This returns paperWidth() if the cell-width is unlimited or the width
2203 // in pixels if we have a pwidth for this cell.
2204 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2205 {
2206         LyXLength const len = tabular->GetPWidth(cell);
2207
2208         if (len.zero())
2209                 return -1;
2210         return len.inPixels(latexTextWidth(bv), bv->text->defaultHeight());
2211 }
2212
2213
2214 int InsetTabular::getMaxWidth(BufferView * bv,
2215                               UpdatableInset const * inset) const
2216 {
2217         int cell = tabular->GetCellFromInset(inset, actcell);
2218
2219         if (cell == -1) {
2220                 lyxerr << "Own inset not found, shouldn't really happen!"
2221                        << endl;
2222                 return -1;
2223         }
2224
2225         int w = getMaxWidthOfCell(bv, cell);
2226         if (w > 0) {
2227                 // because the inset then subtracts it's top_x and owner->x()
2228                 w += (inset->x() - top_x);
2229         }
2230
2231         return w;
2232 }
2233
2234
2235 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2236 {
2237         resizeLyXText(bv, recursive);
2238 }
2239
2240
2241 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2242 {
2243         if (force) {
2244                 for(int i = 0; i < tabular->rows(); ++i) {
2245                         for(int j = 0; j < tabular->columns(); ++j) {
2246                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2247                         }
2248                 }
2249         }
2250         need_update = FULL;
2251 }
2252
2253
2254 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2255                                    bool const recursive) const
2256 {
2257         if (the_locking_inset)
2258                 return the_locking_inset->getLyXText(bv, recursive);
2259 #if 0
2260         // if we're locked lock the actual insettext and return it's LyXText!!!
2261         if (locked) {
2262                 UpdatableInset * inset =
2263                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2264                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2265                 return the_locking_inset->getLyXText(bv, recursive);
2266         }
2267 #endif
2268         return Inset::getLyXText(bv, recursive);
2269 }
2270
2271
2272 bool InsetTabular::showInsetDialog(BufferView * bv) const
2273 {
2274         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2275                 bv->owner()->getDialogs()
2276                         ->showTabular(const_cast<InsetTabular *>(this));
2277         return true;
2278 }
2279
2280
2281 void InsetTabular::openLayoutDialog(BufferView * bv) const
2282 {
2283         if (the_locking_inset) {
2284                 InsetTabular * i = static_cast<InsetTabular *>
2285                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2286                 if (i) {
2287                         i->openLayoutDialog(bv);
2288                         return;
2289                 }
2290         }
2291         bv->owner()->getDialogs()->showTabular(
2292                 const_cast<InsetTabular *>(this));
2293 }
2294
2295
2296 //
2297 // function returns an object as defined in func_status.h:
2298 // states OK, Unknown, Disabled, On, Off.
2299 //
2300 FuncStatus InsetTabular::getStatus(string const & what) const
2301 {
2302         int action = LyXTabular::LAST_ACTION;
2303         FuncStatus status;
2304
2305         int i = 0;
2306         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2307                 string const tmp = tabularFeature[i].feature;
2308                 if (tmp == what.substr(0, tmp.length())) {
2309                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2310                         //   tabularFeatures[i].feature.length())) {
2311                         action = tabularFeature[i].action;
2312                         break;
2313                 }
2314         }
2315         if (action == LyXTabular::LAST_ACTION) {
2316                 status.clear();
2317                 return status.unknown(true);
2318         }
2319
2320         string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2321
2322         int sel_row_start;
2323         int sel_row_end;
2324         int dummy;
2325         LyXTabular::ltType dummyltt;
2326         bool flag = true;
2327
2328         if (hasSelection()) {
2329                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2330         } else {
2331                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2332         }
2333
2334         switch (action) {
2335         case LyXTabular::SET_PWIDTH:
2336         case LyXTabular::SET_MPWIDTH:
2337         case LyXTabular::SET_SPECIAL_COLUMN:
2338         case LyXTabular::SET_SPECIAL_MULTI:
2339                 return status.disabled(true);
2340
2341         case LyXTabular::APPEND_ROW:
2342         case LyXTabular::APPEND_COLUMN:
2343         case LyXTabular::DELETE_ROW:
2344         case LyXTabular::DELETE_COLUMN:
2345         case LyXTabular::SET_ALL_LINES:
2346         case LyXTabular::UNSET_ALL_LINES:
2347                 return status.clear();
2348
2349         case LyXTabular::MULTICOLUMN:
2350                 status.setOnOff(tabular->IsMultiColumn(actcell));
2351                 break;
2352         case LyXTabular::M_TOGGLE_LINE_TOP:
2353                 flag = false;
2354         case LyXTabular::TOGGLE_LINE_TOP:
2355                 status.setOnOff(tabular->TopLine(actcell, flag));
2356                 break;
2357         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2358                 flag = false;
2359         case LyXTabular::TOGGLE_LINE_BOTTOM:
2360                 status.setOnOff(tabular->BottomLine(actcell, flag));
2361                 break;
2362         case LyXTabular::M_TOGGLE_LINE_LEFT:
2363                 flag = false;
2364         case LyXTabular::TOGGLE_LINE_LEFT:
2365                 status.setOnOff(tabular->LeftLine(actcell, flag));
2366                 break;
2367         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2368                 flag = false;
2369         case LyXTabular::TOGGLE_LINE_RIGHT:
2370                 status.setOnOff(tabular->RightLine(actcell, flag));
2371                 break;
2372         case LyXTabular::M_ALIGN_LEFT:
2373                 flag = false;
2374         case LyXTabular::ALIGN_LEFT:
2375                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2376                 break;
2377         case LyXTabular::M_ALIGN_RIGHT:
2378                 flag = false;
2379         case LyXTabular::ALIGN_RIGHT:
2380                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2381                 break;
2382         case LyXTabular::M_ALIGN_CENTER:
2383                 flag = false;
2384         case LyXTabular::ALIGN_CENTER:
2385                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2386                 break;
2387         case LyXTabular::M_VALIGN_TOP:
2388                 flag = false;
2389         case LyXTabular::VALIGN_TOP:
2390                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2391                 break;
2392         case LyXTabular::M_VALIGN_BOTTOM:
2393                 flag = false;
2394         case LyXTabular::VALIGN_BOTTOM:
2395                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2396                 break;
2397         case LyXTabular::M_VALIGN_CENTER:
2398                 flag = false;
2399         case LyXTabular::VALIGN_CENTER:
2400                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2401                 break;
2402         case LyXTabular::SET_LONGTABULAR:
2403                 status.setOnOff(tabular->IsLongTabular());
2404                 break;
2405         case LyXTabular::UNSET_LONGTABULAR:
2406                 status.setOnOff(!tabular->IsLongTabular());
2407                 break;
2408         case LyXTabular::SET_ROTATE_TABULAR:
2409                 status.setOnOff(tabular->GetRotateTabular());
2410                 break;
2411         case LyXTabular::UNSET_ROTATE_TABULAR:
2412                 status.setOnOff(!tabular->GetRotateTabular());
2413                 break;
2414         case LyXTabular::SET_ROTATE_CELL:
2415                 status.setOnOff(tabular->GetRotateCell(actcell));
2416                 break;
2417         case LyXTabular::UNSET_ROTATE_CELL:
2418                 status.setOnOff(!tabular->GetRotateCell(actcell));
2419                 break;
2420         case LyXTabular::SET_USEBOX:
2421                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2422                 break;
2423         case LyXTabular::SET_LTFIRSTHEAD:
2424                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2425                 break;
2426         case LyXTabular::SET_LTHEAD:
2427                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2428                 break;
2429         case LyXTabular::SET_LTFOOT:
2430                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2431                 break;
2432         case LyXTabular::SET_LTLASTFOOT:
2433                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2434                 break;
2435         case LyXTabular::SET_LTNEWPAGE:
2436                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2437                 break;
2438         default:
2439                 status.clear();
2440                 status.disabled(true);
2441                 break;
2442         }
2443         return status;
2444 }
2445
2446
2447 vector<string> const InsetTabular::getLabelList() const
2448 {
2449         return tabular->getLabelList();
2450 }
2451
2452
2453 bool InsetTabular::copySelection(BufferView * bv)
2454 {
2455         if (!hasSelection())
2456                 return false;
2457
2458         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2459         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2460         if (sel_col_start > sel_col_end) {
2461                 sel_col_start = sel_col_end;
2462                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2463         } else {
2464                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2465         }
2466         int const columns = sel_col_end - sel_col_start + 1;
2467
2468         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2469         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2470         if (sel_row_start > sel_row_end) {
2471                 swap(sel_row_start, sel_row_end);
2472         }
2473         int const rows = sel_row_end - sel_row_start + 1;
2474
2475         delete paste_tabular;
2476         paste_tabular = new LyXTabular(bv->buffer()->params,
2477                                        this, *tabular); // rows, columns);
2478         for (int i = 0; i < sel_row_start; ++i)
2479                 paste_tabular->DeleteRow(0);
2480         while (paste_tabular->rows() > rows)
2481                 paste_tabular->DeleteRow(rows);
2482         paste_tabular->SetTopLine(0, true, true);
2483         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2484                                      true, true);
2485         for (int i = 0; i < sel_col_start; ++i)
2486                 paste_tabular->DeleteColumn(0);
2487         while (paste_tabular->columns() > columns)
2488                 paste_tabular->DeleteColumn(columns);
2489         paste_tabular->SetLeftLine(0, true, true);
2490         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2491                                     true, true);
2492
2493         ostringstream sstr;
2494         paste_tabular->ascii(bv->buffer(), sstr,
2495                              (int)parOwner()->params().depth(), true, '\t');
2496         bv->stuffClipboard(sstr.str().c_str());
2497         return true;
2498 }
2499
2500
2501 bool InsetTabular::pasteSelection(BufferView * bv)
2502 {
2503         if (!paste_tabular)
2504                 return false;
2505
2506         for (int r1 = 0, r2 = actrow;
2507              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2508              ++r1, ++r2) {
2509                 for(int c1 = 0, c2 = actcol;
2510                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2511                     ++c1, ++c2) {
2512                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2513                             tabular->IsPartOfMultiColumn(r2,c2))
2514                                 continue;
2515                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2516                                 --c2;
2517                                 continue;
2518                         }
2519                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2520                                 --c1;
2521                                 continue;
2522                         }
2523                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2524                         int const n2 = tabular->GetCellNumber(r2, c2);
2525                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2526                         tabular->GetCellInset(n2)->setOwner(this);
2527                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2528                 }
2529         }
2530         return true;
2531 }
2532
2533
2534 bool InsetTabular::cutSelection()
2535 {
2536         if (!hasSelection())
2537                 return false;
2538
2539         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2540         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2541         if (sel_col_start > sel_col_end) {
2542                 sel_col_start = sel_col_end;
2543                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2544         } else {
2545                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2546         }
2547         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2548         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2549         if (sel_row_start > sel_row_end) {
2550                 swap(sel_row_start, sel_row_end);
2551         }
2552         if (sel_cell_start > sel_cell_end) {
2553                 swap(sel_cell_start, sel_cell_end);
2554         }
2555         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2556                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2557                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2558                 }
2559         }
2560         return true;
2561 }
2562
2563
2564 bool InsetTabular::isRightToLeft(BufferView * bv)
2565 {
2566         return bv->getParentLanguage(this)->RightToLeft();
2567 }
2568
2569
2570 bool InsetTabular::nodraw() const
2571 {
2572         if (!UpdatableInset::nodraw() && the_locking_inset)
2573                 return the_locking_inset->nodraw();
2574         return UpdatableInset::nodraw();
2575 }
2576
2577
2578 int InsetTabular::scroll(bool recursive) const
2579 {
2580         int sx = UpdatableInset::scroll(false);
2581
2582         if (recursive && the_locking_inset)
2583                 sx += the_locking_inset->scroll(recursive);
2584
2585         return sx;
2586 }
2587
2588
2589 bool InsetTabular::doClearArea() const
2590 {
2591         return !locked || (need_update & (FULL|INIT));
2592 }
2593
2594
2595 void InsetTabular::getSelection(int & srow, int & erow,
2596                                 int & scol, int & ecol) const
2597 {
2598         int const start = hasSelection() ? sel_cell_start : actcell;
2599         int const end = hasSelection() ? sel_cell_end : actcell;
2600
2601         srow = tabular->row_of_cell(start);
2602         erow = tabular->row_of_cell(end);
2603         if (srow > erow) {
2604                 swap(srow, erow);
2605         }
2606
2607         scol = tabular->column_of_cell(start);
2608         ecol = tabular->column_of_cell(end);
2609         if (scol > ecol) {
2610                 swap(scol, ecol);
2611         } else {
2612                 ecol = tabular->right_column_of_cell(end);
2613         }
2614 }
2615
2616
2617 Paragraph * InsetTabular::getParFromID(int id) const
2618 {
2619         Paragraph * result;
2620         for(int i = 0; i < tabular->rows(); ++i) {
2621                 for(int j = 0; j < tabular->columns(); ++j) {
2622                         if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2623                                 return result;
2624                 }
2625         }
2626         return 0;
2627 }
2628
2629
2630 Paragraph * InsetTabular::firstParagraph() const
2631 {
2632         if (the_locking_inset)
2633                 return the_locking_inset->firstParagraph();
2634         return 0;
2635 }
2636
2637
2638 Paragraph * InsetTabular::getFirstParagraph(int i) const
2639 {
2640         return (i < tabular->GetNumberOfCells())
2641                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2642                 : 0;
2643 }
2644
2645
2646 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2647 {
2648         if (the_locking_inset)
2649                 return the_locking_inset->cursor(bv);
2650         return Inset::cursor(bv);
2651 }
2652
2653
2654 Inset * InsetTabular::getInsetFromID(int id_arg) const
2655 {
2656         if (id_arg == id())
2657                 return const_cast<InsetTabular *>(this);
2658
2659         Inset * result;
2660         for(int i = 0; i < tabular->rows(); ++i) {
2661                 for(int j = 0; j < tabular->columns(); ++j) {
2662                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2663                                 return result;
2664                 }
2665         }
2666         return 0;
2667 }
2668
2669
2670 string const
2671 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2672 {
2673         nodraw(true);
2674         if (the_locking_inset) {
2675                 string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2676                 if (!str.empty()) {
2677                         nodraw(false);
2678                         return str;
2679                 }
2680                 if (tabular->IsLastCell(actcell)) {
2681                         bv->unlockInset(const_cast<InsetTabular *>(this));
2682                         nodraw(false);
2683                         return string();
2684                 }
2685                 ++actcell;
2686         }
2687         // otherwise we have to lock the next inset and ask for it's selecttion
2688         UpdatableInset * inset =
2689                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2690         inset->edit(bv, 0,  0, 0);
2691         string const str(selectNextWordInt(bv, value));
2692         nodraw(false);
2693         if (!str.empty())
2694                 resetPos(bv);
2695         return str;
2696 }
2697
2698
2699 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2700 {
2701         // when entering this function the inset should be ALWAYS locked!
2702         lyx::Assert(the_locking_inset);
2703
2704         string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2705         if (!str.empty())
2706                 return str;
2707
2708         if (tabular->IsLastCell(actcell)) {
2709                 bv->unlockInset(const_cast<InsetTabular *>(this));
2710                 return string();
2711         }
2712
2713         // otherwise we have to lock the next inset and ask for it's selecttion
2714         UpdatableInset * inset =
2715                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2716         inset->edit(bv);
2717         return selectNextWordInt(bv, value);
2718 }
2719
2720
2721 void InsetTabular::selectSelectedWord(BufferView * bv)
2722 {
2723         if (the_locking_inset) {
2724                 the_locking_inset->selectSelectedWord(bv);
2725                 return;
2726         }
2727         return;
2728 }
2729
2730
2731 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2732 {
2733         if (the_locking_inset) {
2734                 the_locking_inset->toggleSelection(bv, kill_selection);
2735         }
2736 }
2737
2738
2739 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2740                                  bool cs, bool mw)
2741 {
2742         nodraw(true);
2743         if (the_locking_inset) {
2744                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2745                         nodraw(false);
2746                         updateLocal(bv, CELL, false);
2747                         return true;
2748                 }
2749                 if (tabular->IsLastCell(actcell)) {
2750                         nodraw(false);
2751                         bv->unlockInset(const_cast<InsetTabular *>(this));
2752                         return false;
2753                 }
2754                 ++actcell;
2755         }
2756         // otherwise we have to lock the next inset and search there
2757         UpdatableInset * inset =
2758                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2759         inset->edit(bv);
2760         bool const ret = searchForward(bv, str, cs, mw);
2761         nodraw(false);
2762         updateLocal(bv, CELL, false);
2763         return ret;
2764 }
2765
2766
2767 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2768                                bool cs, bool mw)
2769 {
2770         nodraw(true);
2771         if (the_locking_inset) {
2772                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2773                         nodraw(false);
2774                         updateLocal(bv, CELL, false);
2775                         return true;
2776                 }
2777                 if (!actcell) { // we are already in the first cell
2778                         nodraw(false);
2779                         bv->unlockInset(const_cast<InsetTabular *>(this));
2780                         return false;
2781                 }
2782                 --actcell;
2783         }
2784         // otherwise we have to lock the next inset and search there
2785         UpdatableInset * inset =
2786                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2787         inset->edit(bv, false);
2788         bool const ret = searchBackward(bv, str, cs, mw);
2789         nodraw(false);
2790         updateLocal(bv, CELL, false);
2791         return ret;
2792 }
2793
2794
2795 bool InsetTabular::insetAllowed(Inset::Code code) const
2796 {
2797         if (the_locking_inset)
2798                 return the_locking_inset->insetAllowed(code);
2799         // we return true here because if the inset is not locked someone
2800         // wants to insert something in one of our insettexts and we generally
2801         // allow to do so.
2802         return true;
2803 }
2804
2805
2806 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2807 {
2808         const int cell = tabular->GetCellFromInset(in, actcell);
2809
2810         if (cell != -1)
2811                 return tabular->GetPWidth(cell).zero();
2812
2813         // well we didn't obviously find it so maybe our owner knows more
2814         if (owner())
2815                 return owner()->forceDefaultParagraphs(in);
2816         // if we're here there is really something strange going on!!!
2817         return false;
2818 }
2819
2820 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2821                                      bool usePaste)
2822 {
2823         if (buf.length() <= 0)
2824                 return true;
2825         
2826         int cols = 1;
2827         int rows = 1;
2828         int maxCols = 1;
2829         string::size_type len = buf.length();
2830         string::size_type p = 0;
2831
2832         while (p < len &&
2833                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2834         {
2835                 switch (buf[p]) {
2836                 case '\t':
2837                         ++cols;
2838                         break;
2839                 case '\n':
2840                         if ((p+1) < len)
2841                                 ++rows;
2842                         maxCols = max(cols, maxCols);
2843                         cols = 1;
2844                         break;
2845                 }
2846                 ++p;
2847         }
2848         maxCols = max(cols, maxCols);
2849         LyXTabular * loctab;
2850         int cell = 0;
2851         int ocol = 0;
2852         int row = 0;
2853         if (usePaste) {
2854                 delete paste_tabular;
2855                 paste_tabular = new LyXTabular(bv->buffer()->params,
2856                                                this, rows, maxCols);
2857                 loctab = paste_tabular;
2858                 cols = 0;
2859         } else {
2860                 loctab = tabular.get();
2861                 cell = actcell;
2862                 ocol = actcol;
2863                 row = actrow;
2864         }
2865         string::size_type op = 0;
2866         int cells = loctab->GetNumberOfCells();
2867         p = 0;
2868         cols = ocol;
2869         rows = loctab->rows();
2870         int const columns = loctab->columns();
2871         while ((cell < cells) && (p < len) && (row < rows) &&
2872                (p = buf.find_first_of("\t\n", p)) != string::npos)
2873         {
2874                 if (p >= len)
2875                         break;
2876                 switch (buf[p]) {
2877                 case '\t':
2878                         // we can only set this if we are not too far right
2879                         if (cols < columns) {
2880                                 InsetText * ti = loctab->GetCellInset(cell);
2881                                 LyXFont const font = ti->getLyXText(bv)->
2882                                         getFont(bv->buffer(), ti->paragraph(), 0);
2883                                 ti->setText(buf.substr(op, p-op), font);
2884                                 ++cols;
2885                                 ++cell;
2886                         }
2887                         break;
2888                 case '\n':
2889                         // we can only set this if we are not too far right
2890                         if (cols < columns) {
2891                                 InsetText * ti = loctab->GetCellInset(cell);
2892                                 LyXFont const font = ti->getLyXText(bv)->
2893                                         getFont(bv->buffer(), ti->paragraph(), 0);
2894                                 ti->setText(buf.substr(op, p-op), font);
2895                         }
2896                         cols = ocol;
2897                         ++row;
2898                         if (row < rows)
2899                                 cell = loctab->GetCellNumber(row, cols);
2900                         break;
2901                 }
2902                 ++p;
2903                 op = p;
2904         }
2905         // check for the last cell if there is no trailing '\n'
2906         if ((cell < cells) && (op < len)) {
2907                 InsetText * ti = loctab->GetCellInset(cell);
2908                 LyXFont const font = ti->getLyXText(bv)->
2909                         getFont(bv->buffer(), ti->paragraph(), 0);
2910                 ti->setText(buf.substr(op, len-op), font);
2911         }
2912
2913         return true;
2914 }