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