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