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