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