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