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