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