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