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