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