]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
updates to minipage inset
[lyx.git] / src / insets / insettabular.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15
16 #include <cstdlib>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "insettabular.h"
23
24 #include "buffer.h"
25 #include "commandtags.h"
26 #include "debug.h"
27 #include "LaTeXFeatures.h"
28 #include "Painter.h"
29 #include "font.h"
30 #include "lyxtext.h"
31 #include "lyx_gui_misc.h"
32 #include "LyXView.h"
33 #include "lyxfunc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
36 #include "debug.h"
37 #include "lyxfunc.h"
38 #include "WorkArea.h"
39
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,
718                                                    kb_action action,
719                                                    string const & arg)
720 {
721     // We need to save the value of the_locking_inset as the call to 
722     // the_locking_inset->LocalDispatch might unlock it.
723     old_locking_inset = the_locking_inset;
724     no_selection = false;
725     UpdatableInset::RESULT result =
726             UpdatableInset::LocalDispatch(bv, action, arg);
727     if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
728         resetPos(bv);
729         return result;
730     }
731
732     if ((action < 0) && arg.empty())
733         return FINISHED;
734
735     if (the_locking_inset) {
736         result=the_locking_inset->LocalDispatch(bv, action, arg);
737         if (result == DISPATCHED_NOUPDATE) {
738             int sc = scroll();
739             resetPos(bv);
740             if (sc != scroll()) { // inset has been scrolled
741                 the_locking_inset->ToggleInsetCursor(bv);
742                 UpdateLocal(bv, FULL, false);
743                 the_locking_inset->ToggleInsetCursor(bv);
744             }
745             return result;
746         } else if (result == DISPATCHED) {
747             the_locking_inset->ToggleInsetCursor(bv);
748             UpdateLocal(bv, CELL, false);
749             the_locking_inset->ToggleInsetCursor(bv);
750             return result;
751         } else if (result == FINISHED) {
752         }
753     }
754
755     bool hs = hasSelection();
756     HideInsetCursor(bv);
757     result=DISPATCHED;
758     switch (action) {
759         // --- Cursor Movements ---------------------------------------------
760     case LFUN_RIGHTSEL:
761         if (tabular->IsLastCellInRow(actcell))
762             break;
763         moveRight(bv, false);
764         sel_cell_end = actcell;
765         UpdateLocal(bv, SELECTION, false);
766         break;
767     case LFUN_RIGHT:
768         result = moveRight(bv);
769         sel_cell_start = sel_cell_end = actcell;
770         if (hs)
771             UpdateLocal(bv, SELECTION, false);
772         break;
773     case LFUN_LEFTSEL:
774         if (tabular->IsFirstCellInRow(actcell))
775             break;
776         moveLeft(bv, false);
777         sel_cell_end = actcell;
778         UpdateLocal(bv, SELECTION, false);
779         break;
780     case LFUN_LEFT:
781         result = moveLeft(bv);
782         sel_cell_start = sel_cell_end = actcell;
783         if (hs)
784             UpdateLocal(bv, SELECTION, false);
785         break;
786     case LFUN_DOWNSEL:
787     {
788         int const ocell = actcell;
789         moveDown(bv, false);
790         if ((ocell == sel_cell_end) ||
791             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
792             sel_cell_end = tabular->GetCellBelow(sel_cell_end);
793         else
794             sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
795         UpdateLocal(bv, SELECTION, false);
796     }
797     break;
798     case LFUN_DOWN:
799         result = moveDown(bv, old_locking_inset != 0);
800         sel_cell_start = sel_cell_end = actcell;
801         if (hs)
802             UpdateLocal(bv, SELECTION, false);
803         break;
804     case LFUN_UPSEL:
805     {
806         int const ocell = actcell;
807         moveUp(bv, false);
808         if ((ocell == sel_cell_end) ||
809             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
810             sel_cell_end = tabular->GetCellAbove(sel_cell_end);
811         else
812             sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
813         UpdateLocal(bv, SELECTION, false);
814     }
815     break;
816     case LFUN_UP:
817         result = moveUp(bv, old_locking_inset != 0);
818         sel_cell_start = sel_cell_end = actcell;
819         if (hs)
820             UpdateLocal(bv, SELECTION, false);
821         break;
822     case LFUN_NEXT: {
823         int column = actcol;
824         if (the_locking_inset) {
825             UnlockInsetInInset(bv, the_locking_inset);
826             the_locking_inset = 0;
827         }
828         if (bv->text->first + bv->painter().paperHeight() <
829             (top_baseline + tabular->GetHeightOfTabular()))
830         {
831             bv->scrollCB(bv->text->first + bv->painter().paperHeight());
832             UpdateLocal(bv, FULL, false);
833             actcell = tabular->GetCellBelow(first_visible_cell) + column;
834         } else {
835             actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
836         }
837         resetPos(bv);
838         UpdateLocal(bv, CURSOR, false);
839         break;
840     }
841     case LFUN_PRIOR: {
842         int column = actcol;
843         if (the_locking_inset) {
844             UnlockInsetInInset(bv, the_locking_inset);
845             the_locking_inset = 0;
846         }
847         if (top_baseline < 0) {
848             bv->scrollCB(bv->text->first - bv->painter().paperHeight());
849             UpdateLocal(bv, FULL, false);
850             if (top_baseline > 0)
851                 actcell = column;
852             else
853                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
854         } else {
855             actcell = column;
856         }
857         resetPos(bv);
858         UpdateLocal(bv, CURSOR, false);
859         break;
860     }
861     case LFUN_BACKSPACE:
862         break;
863     case LFUN_DELETE:
864         break;
865     case LFUN_HOME:
866         break;
867     case LFUN_END:
868         break;
869     case LFUN_SHIFT_TAB:
870     case LFUN_TAB:
871     {
872         if (the_locking_inset) {
873             UnlockInsetInInset(bv, the_locking_inset);
874             the_locking_inset = 0;
875         }
876         if (action == LFUN_TAB)
877             moveNextCell(bv, old_locking_inset != 0);
878         else
879             movePrevCell(bv, old_locking_inset != 0);
880         sel_cell_start = sel_cell_end = actcell;
881         if (hs)
882             UpdateLocal(bv, SELECTION, false);
883         break;
884     }
885     case LFUN_LAYOUT_TABULAR:
886     {
887         bv->owner()->getDialogs()->showTabular(this);
888     }
889     break;
890     case LFUN_TABULAR_FEATURE:
891         if (!TabularFeatures(bv, arg))
892             result = UNDISPATCHED;
893         break;
894     case LFUN_CUT:
895         if (!copySelection(bv))
896             break;
897         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
898 #ifndef NEW_INSETS
899           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
900                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
901 #else
902           bv->text->cursor.par()->previous(),
903           bv->text->cursor.par()->next()
904 #endif
905                 );
906         cutSelection();
907         UpdateLocal(bv, INIT, true);
908         break;
909     case LFUN_COPY:
910         if (!hasSelection())
911             break;
912         bv->text->FinishUndo();
913         copySelection(bv);
914         break;
915     case LFUN_PASTESELECTION:
916     {
917         string clip(bv->workarea()->getClipboard());
918         
919         if (clip.empty())
920             break;
921         if (clip.find('\t') != string::npos) {
922             int cols = 1;
923             int rows = 1;
924             int maxCols = 1;
925             unsigned int len = clip.length();
926             string::size_type p = 0;
927
928             while((p < len) &&
929                   ((p = clip.find_first_of("\t\n", p)) != string::npos))
930             {
931                 switch(clip[p]) {
932                 case '\t':
933                     ++cols;
934                     break;
935                 case '\n':
936                     if ((p+1) < len)
937                         ++rows;
938                     maxCols = max(cols, maxCols);
939                     cols = 1;
940                     break;
941                 }
942                 ++p;
943             }
944             maxCols = max(cols, maxCols);
945             delete paste_tabular;
946             paste_tabular = new LyXTabular(this, rows, maxCols);
947             string::size_type op = 0;
948             int cell = 0;
949             int cells = paste_tabular->GetNumberOfCells();
950             p = cols = 0;
951             while((cell < cells) && (p < len) &&
952                   (p = clip.find_first_of("\t\n", p)) != string::npos)
953             {
954                 if (p >= len)
955                     break;
956                 switch(clip[p]) {
957                 case '\t':
958                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
959                     ++cols;
960                     ++cell;
961                     break;
962                 case '\n':
963                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
964                     while(cols++ < maxCols)
965                         ++cell;
966                     cols = 0;
967                     break;
968                 }
969                 ++p;
970                 op = p;
971             }
972             // check for the last cell if there is no trailing '\n'
973             if ((cell < cells) && (op < len))
974                 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
975         } else {
976             // so that the clipboard is used and it goes on to default
977             // and executes LFUN_PASTESELECTION in insettext!
978             delete paste_tabular;
979             paste_tabular = 0;
980         }
981     }
982     case LFUN_PASTE:
983         if (hasPasteBuffer()) {
984             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
985 #ifndef NEW_INSETS
986               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
987                               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
988 #else
989               bv->text->cursor.par()->previous(),
990               bv->text->cursor.par()->next()
991 #endif
992                 );
993             pasteSelection(bv);
994             UpdateLocal(bv, INIT, true);
995             break;
996         }
997     // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
998     default:
999         // we try to activate the actual inset and put this event down to
1000         // the insets dispatch function.
1001         result = UNDISPATCHED;
1002         if (the_locking_inset)
1003             break;
1004         no_draw = true;
1005         if (ActivateCellInset(bv)) {
1006             result=the_locking_inset->LocalDispatch(bv, action, arg);
1007             if ((result == UNDISPATCHED) || (result == FINISHED)) {
1008                 UnlockInsetInInset(bv, the_locking_inset);
1009                 no_draw = false;
1010                 the_locking_inset = 0;
1011                 return UNDISPATCHED;
1012             }
1013             no_draw = false;
1014             the_locking_inset->ToggleInsetCursor(bv);
1015             UpdateLocal(bv, CELL, false);
1016             the_locking_inset->ToggleInsetCursor(bv);
1017             return result;
1018         }
1019         break;
1020     }
1021     if (result!=FINISHED) {
1022         if (!the_locking_inset) {
1023             ShowInsetCursor(bv);
1024         }
1025     } else
1026         bv->unlockInset(this);
1027     return result;
1028 }
1029
1030
1031 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1032                         bool fragile, bool fp) const
1033 {
1034     return tabular->Latex(buf, os, fragile, fp);
1035 }
1036
1037
1038 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1039 {
1040     // This should be changed to a real ascii export
1041     return tabular->Ascii(buf, os);
1042 }
1043
1044
1045 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1046 {
1047     return 0;
1048 }
1049
1050
1051 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1052 {
1053     return tabular->DocBook(buf,os);
1054 }
1055
1056
1057 void InsetTabular::Validate(LaTeXFeatures & features) const
1058 {
1059     tabular->Validate(features);
1060 }
1061
1062
1063 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1064                                                  LyXFont const & font,
1065                                                  bool reinit) const
1066 {
1067     int cell = -1;
1068     int maxAsc = 0;
1069     int maxDesc = 0;
1070     InsetText * inset;
1071     bool changed = false;
1072     
1073     // if we have a locking_inset we should have to check only this cell for
1074     // change so I'll try this to have a boost, but who knows ;)
1075     if ((need_update != INIT) &&
1076         (the_locking_inset == tabular->GetCellInset(actcell))) {
1077         for(int i = 0; i < tabular->columns(); ++i) {
1078             maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1079                          maxAsc);
1080             maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1081                           maxDesc);
1082         }
1083         changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1084         changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1085         changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1086         return changed;
1087     }
1088     for (int i = 0; i < tabular->rows(); ++i) {
1089         maxAsc = 0;
1090         maxDesc = 0;
1091         for (int j= 0; j < tabular->columns(); ++j) {
1092             if (tabular->IsPartOfMultiColumn(i,j))
1093                 continue;
1094             ++cell;
1095             inset = tabular->GetCellInset(cell);
1096             if (!reinit)
1097                 inset->update(bv, font, false);
1098             maxAsc = max(maxAsc, inset->ascent(bv, font));
1099             maxDesc = max(maxDesc, inset->descent(bv, font));
1100             changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1101         }
1102         changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1103         changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1104     }
1105     return changed;
1106 }
1107
1108
1109 void InsetTabular::GetCursorPos(BufferView *,
1110                                 int & x, int & y) const
1111 {
1112     x = cursor.x() - top_x;
1113     y = cursor.y();
1114 }
1115
1116
1117 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1118 {
1119     if (the_locking_inset) {
1120         the_locking_inset->ToggleInsetCursor(bv);
1121         return;
1122     }
1123
1124     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1125
1126     int const asc = lyxfont::maxAscent(font);
1127     int const desc = lyxfont::maxDescent(font);
1128   
1129     if (cursor_visible)
1130         bv->hideLockedInsetCursor();
1131     else
1132         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1133     cursor_visible = !cursor_visible;
1134 }
1135
1136
1137 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1138 {
1139     if (!cursor_visible) {
1140         LyXFont font; // = GetFont(par, cursor.pos);
1141     
1142         int const asc = lyxfont::maxAscent(font);
1143         int const desc = lyxfont::maxDescent(font);
1144         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1145         if (show)
1146             bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1147         cursor_visible = true;
1148     }
1149 }
1150
1151
1152 void InsetTabular::HideInsetCursor(BufferView * bv)
1153 {
1154     if (cursor_visible) {
1155         bv->hideLockedInsetCursor();
1156         cursor_visible = false;
1157     }
1158 //    if (cursor_visible)
1159 //        ToggleInsetCursor(bv);
1160 }
1161
1162
1163 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1164 {
1165     cursor.y(0);
1166         
1167     actcell = actrow = actcol = 0;
1168     int ly = tabular->GetDescentOfRow(actrow);
1169
1170     // first search the right row
1171     while((ly < y) && (actrow < tabular->rows())) {
1172         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1173             tabular->GetAscentOfRow(actrow + 1) +
1174             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1175                                                                 actcol)));
1176         ++actrow;
1177         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1178     }
1179     actcell = tabular->GetCellNumber(actrow, actcol);
1180
1181     // now search the right column
1182     int lx = tabular->GetWidthOfColumn(actcell) -
1183         tabular->GetAdditionalWidth(actcell);
1184 #if 0
1185 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1186     for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1187         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1188             tabular->GetAdditionalWidth(actcell - 1));
1189 #else
1190     // Jürgen, you should check that this is correct. (Lgb)
1191     for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1192             lx += tabular->GetWidthOfColumn(actcell + 1)
1193                     + tabular->GetAdditionalWidth(actcell);
1194     }
1195     
1196 #endif
1197     cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1198     resetPos(bv);
1199 }
1200
1201
1202 int InsetTabular::getCellXPos(int cell) const
1203 {
1204     int c = cell;
1205
1206     for (; !tabular->IsFirstCellInRow(c); --c)
1207         ;
1208     int lx = tabular->GetWidthOfColumn(cell);
1209     for (; c < cell; ++c) {
1210         lx += tabular->GetWidthOfColumn(c);
1211     }
1212     return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1213 }
1214
1215
1216 void InsetTabular::resetPos(BufferView * bv) const
1217 {
1218     if (!locked)
1219         return;
1220     actcol = tabular->column_of_cell(actcell);
1221
1222     int cell = 0;
1223     actrow = 0;
1224     cursor.y(0);
1225     for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1226         if (tabular->IsLastCellInRow(cell)) {
1227             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1228                 tabular->GetAscentOfRow(actrow + 1) +
1229                 tabular->GetAdditionalHeight(cell + 1));
1230             ++actrow;
1231         }
1232     }
1233     static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1234     int new_x = getCellXPos(actcell);
1235     int old_x = cursor.x();
1236     new_x += offset;
1237     cursor.x(new_x);
1238 //    cursor.x(getCellXPos(actcell) + offset);
1239     if (scroll() && (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1240         scroll(bv, 0.0F);
1241     else if (the_locking_inset &&
1242              (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1243     {
1244             int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1245             if (xx > (bv->workWidth()-20))
1246                 scroll(bv, -(xx - bv->workWidth() + 60));
1247             else if (xx < 20) {
1248                 if (xx < 0)
1249                     xx = -xx + 60;
1250                 else
1251                     xx = 60;
1252                 scroll(bv, xx);
1253             }
1254     } else if (((cursor.x() - offset) > 20) &&
1255                ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1256                 (bv->workWidth()-20)))
1257     {
1258         scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1259         UpdateLocal(bv, FULL, false);
1260     } else if ((cursor.x() - offset) < 20) {
1261         scroll(bv, 20 - cursor.x() + offset);
1262         UpdateLocal(bv, FULL, false);
1263     } else if (scroll() && (top_x > 20) &&
1264                ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1265         scroll(bv, old_x - cursor.x());
1266     }
1267     if ((!the_locking_inset ||
1268          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1269         (actcell != oldcell)) {
1270         InsetTabular * inset = const_cast<InsetTabular *>(this);
1271         bv->owner()->getDialogs()->updateTabular(inset);
1272         oldcell = actcell;
1273     }
1274 }
1275
1276
1277 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1278 {
1279     if (lock && !old_locking_inset) {
1280         if (ActivateCellInset(bv))
1281             return DISPATCHED;
1282     } else {
1283         bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1284         if (!moved)
1285             return FINISHED;
1286         if (lock && ActivateCellInset(bv))
1287             return DISPATCHED;
1288     }
1289     resetPos(bv);
1290     return DISPATCHED_NOUPDATE;
1291 }
1292
1293
1294 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1295 {
1296     bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1297     if (!moved)
1298         return FINISHED;
1299     if (lock) {       // behind the inset
1300         if (ActivateCellInset(bv, 0, 0, 0, true))
1301             return DISPATCHED;
1302     }
1303     resetPos(bv);
1304     return DISPATCHED_NOUPDATE;
1305 }
1306
1307
1308 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1309 {
1310     int const ocell = actcell;
1311     actcell = tabular->GetCellAbove(actcell);
1312     if (actcell == ocell) // we moved out of the inset
1313         return FINISHED;
1314     resetPos(bv);
1315    if (lock) {
1316         int x = 0;
1317         int y = 0;
1318         if (old_locking_inset) {
1319             old_locking_inset->GetCursorPos(bv, x, y);
1320             x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1321         }
1322         if (ActivateCellInset(bv, x, 0))
1323             return DISPATCHED;
1324     }
1325     return DISPATCHED_NOUPDATE;
1326 }
1327
1328
1329 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1330 {
1331     int const ocell = actcell;
1332     actcell = tabular->GetCellBelow(actcell);
1333     if (actcell == ocell) // we moved out of the inset
1334         return FINISHED;
1335     resetPos(bv);
1336     if (lock) {
1337         int x = 0;
1338         int y = 0;
1339         if (old_locking_inset) {
1340             old_locking_inset->GetCursorPos(bv, x, y);
1341             x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1342         }
1343         if (ActivateCellInset(bv, x, 0))
1344             return DISPATCHED;
1345     }
1346     return DISPATCHED_NOUPDATE;
1347 }
1348
1349
1350 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1351 {
1352     if (isRightToLeft(bv)) {
1353         if (tabular->IsFirstCellInRow(actcell)) {
1354             int row = tabular->row_of_cell(actcell);
1355             if (row == tabular->rows() - 1)
1356                 return false;
1357             actcell = tabular->GetLastCellInRow(row);
1358             actcell = tabular->GetCellBelow(actcell);
1359         } else {
1360             if (!actcell)
1361                 return false;
1362             --actcell;
1363         }
1364     } else {
1365         if (tabular->IsLastCell(actcell))
1366             return false;
1367         ++actcell;
1368     }
1369     if (lock) {
1370         bool rtl = tabular->GetCellInset(actcell)->par->
1371                 isRightToLeftPar(bv->buffer()->params);
1372         ActivateCellInset(bv, 0, 0, 0, !rtl);
1373     }
1374     resetPos(bv);
1375     return true;
1376 }
1377
1378
1379 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1380 {
1381     if (isRightToLeft(bv)) {
1382         if (tabular->IsLastCellInRow(actcell)) {
1383             int row = tabular->row_of_cell(actcell);
1384             if (row == 0)
1385                 return false;
1386             actcell = tabular->GetFirstCellInRow(row);
1387             actcell = tabular->GetCellAbove(actcell);
1388         } else {
1389             if (tabular->IsLastCell(actcell))
1390                 return false;
1391             ++actcell;
1392         }
1393     } else {
1394         if (!actcell) // first cell
1395             return false;
1396         --actcell;
1397     }
1398     if (lock) {
1399         bool rtl = tabular->GetCellInset(actcell)->par->
1400                 isRightToLeftPar(bv->buffer()->params);
1401         ActivateCellInset(bv, 0, 0, 0, !rtl);
1402     }
1403     resetPos(bv);
1404     return true;
1405 }
1406
1407
1408 bool InsetTabular::Delete()
1409 {
1410     return true;
1411 }
1412
1413
1414 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1415 {
1416     if (the_locking_inset)
1417         the_locking_inset->SetFont(bv, font, tall);
1418 }
1419
1420
1421 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1422 {
1423     LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1424
1425     int i = 0;
1426     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1427         string const tmp = tabularFeatures[i].feature;
1428             
1429         if (tmp == what.substr(0, tmp.length())) {
1430         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1431               //tabularFeatures[i].feature.length())) {
1432             action = tabularFeatures[i].action;
1433             break;
1434         }
1435     }
1436     if (action == LyXTabular::LAST_ACTION)
1437         return false;
1438
1439     string const val =
1440             frontStrip(what.substr(tabularFeatures[i].feature.length()));
1441     TabularFeatures(bv, action, val);
1442     return true;
1443 }
1444
1445
1446 void InsetTabular::TabularFeatures(BufferView * bv,
1447                                    LyXTabular::Feature feature,
1448                                    string const & value)
1449 {
1450     int i;
1451     int j;
1452     int sel_col_start;
1453     int sel_col_end;
1454     int sel_row_start;
1455     int sel_row_end;
1456     int setLines = 0;
1457     LyXAlignment setAlign = LYX_ALIGN_LEFT;
1458     LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1459     int lineSet;
1460     bool what;
1461
1462     switch (feature) {
1463       case LyXTabular::M_ALIGN_LEFT:
1464       case LyXTabular::ALIGN_LEFT:
1465           setAlign=LYX_ALIGN_LEFT;
1466           break;
1467       case LyXTabular::M_ALIGN_RIGHT:
1468       case LyXTabular::ALIGN_RIGHT:
1469           setAlign=LYX_ALIGN_RIGHT;
1470           break;
1471       case LyXTabular::M_ALIGN_CENTER:
1472       case LyXTabular::ALIGN_CENTER:
1473           setAlign=LYX_ALIGN_CENTER;
1474           break;
1475       case LyXTabular::M_VALIGN_TOP:
1476       case LyXTabular::VALIGN_TOP:
1477           setVAlign=LyXTabular::LYX_VALIGN_TOP;
1478           break;
1479       case LyXTabular::M_VALIGN_BOTTOM:
1480       case LyXTabular::VALIGN_BOTTOM:
1481           setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1482           break;
1483       case LyXTabular::M_VALIGN_CENTER:
1484       case LyXTabular::VALIGN_CENTER:
1485           setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1486           break;
1487       default:
1488           break;
1489     }
1490     if (hasSelection()) {
1491         sel_col_start = tabular->column_of_cell(sel_cell_start);
1492         sel_col_end = tabular->column_of_cell(sel_cell_end);
1493         if (sel_col_start > sel_col_end) {
1494             sel_col_end = sel_col_start;
1495             sel_col_start = tabular->column_of_cell(sel_cell_end);
1496         } else {
1497             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1498         }
1499         
1500         sel_row_start = tabular->row_of_cell(sel_cell_start);
1501         sel_row_end = tabular->row_of_cell(sel_cell_end);
1502         if (sel_row_start > sel_row_end) {
1503                 //int tmp = sel_row_start;
1504                 //sel_row_start = sel_row_end;
1505                 //sel_row_end = tmp;
1506             swap(sel_row_start, sel_row_end);
1507         }
1508     } else {
1509         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1510         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1511     }
1512     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1513 #ifndef NEW_INSETS
1514               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1515                       bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1516 #else
1517               bv->text->cursor.par()->previous(),
1518               bv->text->cursor.par()->next()
1519 #endif
1520             );
1521
1522     int row = tabular->row_of_cell(actcell);
1523     int column = tabular->column_of_cell(actcell);
1524     bool flag = true;
1525     
1526     switch (feature) {
1527     case LyXTabular::SET_PWIDTH:
1528     {
1529         bool const update = (tabular->GetColumnPWidth(actcell) != value);
1530         tabular->SetColumnPWidth(actcell,value);
1531         if (update) {
1532             for (int i=0; i < tabular->rows(); ++i) {
1533                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1534                     resizeLyXText(bv);
1535             }
1536             UpdateLocal(bv, INIT, true);
1537         }
1538     }
1539     break;
1540     case LyXTabular::SET_MPWIDTH:
1541     {
1542         bool const update = (tabular->GetPWidth(actcell) != value);
1543         tabular->SetMColumnPWidth(actcell,value);
1544         if (update) {
1545             for (int i=0; i < tabular->rows(); ++i) {
1546                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1547                     resizeLyXText(bv);
1548             }
1549             UpdateLocal(bv, INIT, true);
1550         }
1551     }
1552     break;
1553     case LyXTabular::SET_SPECIAL_COLUMN:
1554     case LyXTabular::SET_SPECIAL_MULTI:
1555         tabular->SetAlignSpecial(actcell,value,feature);
1556         break;
1557     case LyXTabular::APPEND_ROW:
1558         // append the row into the tabular
1559         UnlockInsetInInset(bv, the_locking_inset);
1560         tabular->AppendRow(actcell);
1561         UpdateLocal(bv, INIT, true);
1562         break;
1563     case LyXTabular::APPEND_COLUMN:
1564         // append the column into the tabular
1565         UnlockInsetInInset(bv, the_locking_inset);
1566         tabular->AppendColumn(actcell);
1567         actcell = tabular->GetCellNumber(row, column);
1568         UpdateLocal(bv, INIT, true);
1569         break;
1570     case LyXTabular::DELETE_ROW:
1571         UnlockInsetInInset(bv, the_locking_inset);
1572         tabular->DeleteRow(tabular->row_of_cell(actcell));
1573         if ((row+1) > tabular->rows())
1574             --row;
1575         actcell = tabular->GetCellNumber(row, column);
1576         clearSelection();
1577         UpdateLocal(bv, INIT, true);
1578         break;
1579     case LyXTabular::DELETE_COLUMN:
1580         UnlockInsetInInset(bv, the_locking_inset);
1581         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1582         if ((column+1) > tabular->columns())
1583             --column;
1584         actcell = tabular->GetCellNumber(row, column);
1585         clearSelection();
1586         UpdateLocal(bv, INIT, true);
1587         break;
1588     case LyXTabular::M_TOGGLE_LINE_TOP:
1589         flag = false;
1590     case LyXTabular::TOGGLE_LINE_TOP:
1591         lineSet = !tabular->TopLine(actcell, flag);
1592         for (i=sel_row_start; i<=sel_row_end; ++i)
1593             for (j=sel_col_start; j<=sel_col_end; ++j)
1594                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1595         UpdateLocal(bv, INIT, true);
1596         break;
1597     
1598     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1599         flag = false;
1600     case LyXTabular::TOGGLE_LINE_BOTTOM:
1601         lineSet = !tabular->BottomLine(actcell, flag); 
1602         for (i=sel_row_start; i<=sel_row_end; ++i)
1603             for (j=sel_col_start; j<=sel_col_end; ++j)
1604                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1605                                        flag);
1606         UpdateLocal(bv, INIT, true);
1607         break;
1608                 
1609     case LyXTabular::M_TOGGLE_LINE_LEFT:
1610         flag = false;
1611     case LyXTabular::TOGGLE_LINE_LEFT:
1612         lineSet = !tabular->LeftLine(actcell, flag);
1613         for (i=sel_row_start; i<=sel_row_end; ++i)
1614             for (j=sel_col_start; j<=sel_col_end; ++j)
1615                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1616                                      flag);
1617         UpdateLocal(bv, INIT, true);
1618         break;
1619
1620     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1621         flag = false;
1622     case LyXTabular::TOGGLE_LINE_RIGHT:
1623         lineSet = !tabular->RightLine(actcell, flag);
1624         for (i=sel_row_start; i<=sel_row_end; ++i)
1625             for (j=sel_col_start; j<=sel_col_end; ++j)
1626                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1627                                       flag);
1628         UpdateLocal(bv, INIT, true);
1629         break;
1630     case LyXTabular::M_ALIGN_LEFT:
1631     case LyXTabular::M_ALIGN_RIGHT:
1632     case LyXTabular::M_ALIGN_CENTER:
1633         flag = false;
1634     case LyXTabular::ALIGN_LEFT:
1635     case LyXTabular::ALIGN_RIGHT:
1636     case LyXTabular::ALIGN_CENTER:
1637         for (i = sel_row_start; i <= sel_row_end; ++i)
1638             for (j = sel_col_start; j <= sel_col_end; ++j)
1639                 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1640                                       flag);
1641         UpdateLocal(bv, INIT, true);
1642         break;
1643     case LyXTabular::M_VALIGN_TOP:
1644     case LyXTabular::M_VALIGN_BOTTOM:
1645     case LyXTabular::M_VALIGN_CENTER:
1646         flag = false;
1647     case LyXTabular::VALIGN_TOP:
1648     case LyXTabular::VALIGN_BOTTOM:
1649     case LyXTabular::VALIGN_CENTER:
1650         for (i = sel_row_start; i <= sel_row_end; ++i)
1651             for (j = sel_col_start; j <= sel_col_end; ++j)
1652                 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1653                                        setVAlign, flag);
1654         UpdateLocal(bv, INIT, true);
1655         break;
1656     case LyXTabular::MULTICOLUMN:
1657     {
1658         if (sel_row_start != sel_row_end) {
1659             WriteAlert(_("Impossible Operation!"), 
1660                        _("Multicolumns can only be horizontally."), 
1661                        _("Sorry."));
1662             return;
1663         }
1664         // just multicol for one Single Cell
1665         if (!hasSelection()) {
1666             // check wether we are completly in a multicol
1667             if (tabular->IsMultiColumn(actcell)) {
1668                 tabular->UnsetMultiColumn(actcell);
1669                 UpdateLocal(bv, INIT, true);
1670             } else {
1671                 tabular->SetMultiColumn(actcell, 1);
1672                 UpdateLocal(bv, CELL, true);
1673             }
1674             return;
1675         }
1676         // we have a selection so this means we just add all this
1677         // cells to form a multicolumn cell
1678         int s_start;
1679         int s_end;
1680
1681         if (sel_cell_start > sel_cell_end) {
1682             s_start = sel_cell_end;
1683             s_end = sel_cell_start;
1684         } else {
1685             s_start = sel_cell_start;
1686             s_end = sel_cell_end;
1687         }
1688         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1689         actcell = s_start;
1690         sel_cell_end = sel_cell_start;
1691         UpdateLocal(bv, INIT, true);
1692         break;
1693     }
1694     case LyXTabular::SET_ALL_LINES:
1695         setLines = 1;
1696     case LyXTabular::UNSET_ALL_LINES:
1697         for (i=sel_row_start; i<=sel_row_end; ++i)
1698             for (j=sel_col_start; j<=sel_col_end; ++j)
1699                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1700         UpdateLocal(bv, INIT, true);
1701         break;
1702     case LyXTabular::SET_LONGTABULAR:
1703         tabular->SetLongTabular(true);
1704         UpdateLocal(bv, INIT, true); // because this toggles displayed
1705         break;
1706     case LyXTabular::UNSET_LONGTABULAR:
1707         tabular->SetLongTabular(false);
1708         UpdateLocal(bv, INIT, true); // because this toggles displayed
1709         break;
1710     case LyXTabular::SET_ROTATE_TABULAR:
1711         tabular->SetRotateTabular(true);
1712         break;
1713     case LyXTabular::UNSET_ROTATE_TABULAR:
1714         tabular->SetRotateTabular(false);
1715         break;
1716     case LyXTabular::SET_ROTATE_CELL:
1717         for (i=sel_row_start; i<=sel_row_end; ++i)
1718             for (j=sel_col_start; j<=sel_col_end; ++j)
1719                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1720         break;
1721     case LyXTabular::UNSET_ROTATE_CELL:
1722         for (i = sel_row_start; i <= sel_row_end; ++i)
1723             for (j = sel_col_start; j <= sel_col_end; ++j)
1724                 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1725         break;
1726     case LyXTabular::SET_USEBOX:
1727     {
1728         LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1729         if (val == tabular->GetUsebox(actcell))
1730             val = LyXTabular::BOX_NONE;
1731         for (i = sel_row_start; i <= sel_row_end; ++i)
1732             for (j = sel_col_start; j <= sel_col_end; ++j)
1733                 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1734         break;
1735     }
1736     case LyXTabular::SET_LTFIRSTHEAD:
1737         tabular->SetLTHead(actcell, true);
1738         break;
1739     case LyXTabular::SET_LTHEAD:
1740         tabular->SetLTHead(actcell, false);
1741         break;
1742     case LyXTabular::SET_LTFOOT:
1743         tabular->SetLTFoot(actcell, false);
1744         break;
1745     case LyXTabular::SET_LTLASTFOOT:
1746         tabular->SetLTFoot(actcell, true);
1747         break;
1748     case LyXTabular::SET_LTNEWPAGE:
1749         what = !tabular->GetLTNewPage(actcell);
1750         tabular->SetLTNewPage(actcell, what);
1751         break;
1752     // dummy stuff just to avoid warnings
1753     case LyXTabular::LAST_ACTION:
1754         break;
1755     }
1756 }
1757
1758
1759 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1760                                      bool behind)
1761 {
1762     UpdatableInset * inset =
1763         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1764     LyXFont font(LyXFont::ALL_SANE);
1765     if (behind) {
1766         x = inset->x() + inset->width(bv, font);
1767         y = inset->descent(bv, font);
1768     }
1769     //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1770     //inset_y = cursor.y();
1771     inset->Edit(bv, x,  y, button);
1772     if (!the_locking_inset)
1773         return false;
1774     UpdateLocal(bv, CELL, false);
1775     return (the_locking_inset != 0);
1776 }
1777
1778
1779 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1780                                         int button)
1781 {
1782         inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1783         inset_y = cursor.y();
1784         return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1785 }
1786
1787
1788 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1789 {
1790     return x + top_x > cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1791 }
1792
1793
1794 // This returns paperWidth() if the cell-width is unlimited or the width
1795 // in pixels if we have a pwidth for this cell.
1796 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1797 {
1798     string const s = tabular->GetPWidth(cell);
1799
1800     if (s.empty())
1801         return -1;
1802     return VSpace(s).inPixels(0, 0);
1803 }
1804
1805
1806 int InsetTabular::getMaxWidth(Painter & pain,
1807                               UpdatableInset const * inset) const
1808 {
1809     int const n = tabular->GetNumberOfCells();
1810     int cell = 0;
1811     for (; cell < n; ++cell) {
1812         if (tabular->GetCellInset(cell) == inset)
1813             break;
1814     }
1815     if (cell >= n)
1816         return -1;
1817     int w = GetMaxWidthOfCell(pain, cell);
1818     if (w > 0)
1819         // because the inset then subtracts it's top_x and owner->x()
1820         w += (inset->x() - top_x);
1821     return w;
1822 }
1823
1824
1825 void InsetTabular::resizeLyXText(BufferView *) const
1826 {
1827     need_update = FULL;
1828 }
1829
1830
1831 LyXText * InsetTabular::getLyXText(BufferView const * bv, bool const recursive) const
1832 {
1833     if (the_locking_inset)
1834         return the_locking_inset->getLyXText(bv, recursive);
1835     return Inset::getLyXText(bv, recursive);
1836 }
1837
1838
1839 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1840 {
1841     if (the_locking_inset) {
1842         InsetTabular * i = static_cast<InsetTabular *>
1843             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1844         if (i) {
1845             i->OpenLayoutDialog(bv);
1846             return;
1847         }
1848     }
1849     bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1850 }
1851
1852 //
1853 // functions returns:
1854 // 0 ... disabled
1855 // 1 ... enabled
1856 // 2 ... toggled on
1857 // 3 ... toggled off
1858 //
1859 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1860 {
1861     int action = LyXTabular::LAST_ACTION;
1862     LyXFunc::func_status status = LyXFunc::OK;
1863     
1864     int i = 0;
1865     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1866         string const tmp = tabularFeatures[i].feature;
1867         if (tmp == what.substr(0, tmp.length())) {                  
1868         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1869         //   tabularFeatures[i].feature.length())) {
1870             action = tabularFeatures[i].action;
1871             break;
1872         }
1873     }
1874     if (action == LyXTabular::LAST_ACTION)
1875         return LyXFunc::Unknown;
1876
1877     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1878
1879     int sel_row_start;
1880     int sel_row_end;
1881     int dummy;
1882     bool flag = true;
1883
1884     if (hasSelection()) {
1885         sel_row_start = tabular->row_of_cell(sel_cell_start);
1886         sel_row_end = tabular->row_of_cell(sel_cell_end);
1887         if (sel_row_start > sel_row_end) {
1888                 //int tmp = sel_row_start;
1889                 //sel_row_start = sel_row_end;
1890                 //sel_row_end = tmp;
1891             swap(sel_row_start, sel_row_end);
1892         }
1893     } else {
1894         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1895     }
1896
1897     switch (action) {
1898     case LyXTabular::SET_PWIDTH:
1899     case LyXTabular::SET_MPWIDTH:
1900     case LyXTabular::SET_SPECIAL_COLUMN:
1901     case LyXTabular::SET_SPECIAL_MULTI:
1902         status |= LyXFunc::Disabled;
1903         return status;
1904
1905     case LyXTabular::APPEND_ROW:
1906     case LyXTabular::APPEND_COLUMN:
1907     case LyXTabular::DELETE_ROW:
1908     case LyXTabular::DELETE_COLUMN:
1909     case LyXTabular::SET_ALL_LINES:
1910     case LyXTabular::UNSET_ALL_LINES:
1911         status |= LyXFunc::OK;
1912         return status;
1913
1914     case LyXTabular::MULTICOLUMN:
1915         if (tabular->IsMultiColumn(actcell))
1916             status |= LyXFunc::ToggleOn;
1917         else
1918             status |= LyXFunc::ToggleOff;
1919         break;
1920     case LyXTabular::M_TOGGLE_LINE_TOP:
1921         flag = false;
1922     case LyXTabular::TOGGLE_LINE_TOP:
1923         if (tabular->TopLine(actcell, flag))
1924             status |= LyXFunc::ToggleOn;
1925         else
1926             status |= LyXFunc::ToggleOff;
1927         break;
1928     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1929         flag = false;
1930     case LyXTabular::TOGGLE_LINE_BOTTOM:
1931         if (tabular->BottomLine(actcell, flag))
1932             status |= LyXFunc::ToggleOn;
1933         else
1934             status |= LyXFunc::ToggleOff;
1935         break;
1936     case LyXTabular::M_TOGGLE_LINE_LEFT:
1937         flag = false;
1938     case LyXTabular::TOGGLE_LINE_LEFT:
1939         if (tabular->LeftLine(actcell, flag))
1940             status |= LyXFunc::ToggleOn;
1941         else
1942             status |= LyXFunc::ToggleOff;
1943         break;
1944     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1945         flag = false;
1946     case LyXTabular::TOGGLE_LINE_RIGHT:
1947         if (tabular->RightLine(actcell, flag))
1948             status |= LyXFunc::ToggleOn;
1949         else
1950             status |= LyXFunc::ToggleOff;
1951         break;
1952     case LyXTabular::M_ALIGN_LEFT:
1953         flag = false;
1954     case LyXTabular::ALIGN_LEFT:
1955         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1956             status |= LyXFunc::ToggleOn;
1957         else
1958             status |= LyXFunc::ToggleOff;
1959         break;
1960     case LyXTabular::M_ALIGN_RIGHT:
1961         flag = false;
1962     case LyXTabular::ALIGN_RIGHT:
1963         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1964             status |= LyXFunc::ToggleOn;
1965         else
1966             status |= LyXFunc::ToggleOff;
1967         break;
1968     case LyXTabular::M_ALIGN_CENTER:
1969         flag = false;
1970     case LyXTabular::ALIGN_CENTER:
1971         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1972             status |= LyXFunc::ToggleOn;
1973         else
1974             status |= LyXFunc::ToggleOff;
1975         break;
1976     case LyXTabular::M_VALIGN_TOP:
1977         flag = false;
1978     case LyXTabular::VALIGN_TOP:
1979         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1980             status |= LyXFunc::ToggleOn;
1981         else
1982             status |= LyXFunc::ToggleOff;
1983         break;
1984     case LyXTabular::M_VALIGN_BOTTOM:
1985         flag = false;
1986     case LyXTabular::VALIGN_BOTTOM:
1987         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1988             status |= LyXFunc::ToggleOn;
1989         else
1990             status |= LyXFunc::ToggleOff;
1991         break;
1992     case LyXTabular::M_VALIGN_CENTER:
1993         flag = false;
1994     case LyXTabular::VALIGN_CENTER:
1995         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1996             status |= LyXFunc::ToggleOn;
1997         else
1998             status |= LyXFunc::ToggleOff;
1999         break;
2000     case LyXTabular::SET_LONGTABULAR:
2001         if (tabular->IsLongTabular())
2002             status |= LyXFunc::ToggleOn;
2003         else
2004             status |= LyXFunc::ToggleOff;
2005         break;
2006     case LyXTabular::UNSET_LONGTABULAR:
2007         if (!tabular->IsLongTabular())
2008             status |= LyXFunc::ToggleOn;
2009         else
2010             status |= LyXFunc::ToggleOff;
2011         break;
2012     case LyXTabular::SET_ROTATE_TABULAR:
2013         if (tabular->GetRotateTabular())
2014             status |= LyXFunc::ToggleOn;
2015         else
2016             status |= LyXFunc::ToggleOff;
2017         break;
2018     case LyXTabular::UNSET_ROTATE_TABULAR:
2019         if (!tabular->GetRotateTabular())
2020             status |= LyXFunc::ToggleOn;
2021         else
2022             status |= LyXFunc::ToggleOff;
2023         break;
2024     case LyXTabular::SET_ROTATE_CELL:
2025         if (tabular->GetRotateCell(actcell))
2026             status |= LyXFunc::ToggleOn;
2027         else
2028             status |= LyXFunc::ToggleOff;
2029         break;
2030     case LyXTabular::UNSET_ROTATE_CELL:
2031         if (!tabular->GetRotateCell(actcell))
2032             status |= LyXFunc::ToggleOn;
2033         else
2034             status |= LyXFunc::ToggleOff;
2035         break;
2036     case LyXTabular::SET_USEBOX:
2037         if (strToInt(argument) == tabular->GetUsebox(actcell))
2038             status |= LyXFunc::ToggleOn;
2039         else
2040             status |= LyXFunc::ToggleOff;
2041         break;
2042     case LyXTabular::SET_LTFIRSTHEAD:
2043         if (tabular->GetRowOfLTHead(actcell, dummy))
2044             status |= LyXFunc::ToggleOn;
2045         else
2046             status |= LyXFunc::ToggleOff;
2047         break;
2048     case LyXTabular::SET_LTHEAD:
2049         if (tabular->GetRowOfLTHead(actcell, dummy))
2050             status |= LyXFunc::ToggleOn;
2051         else
2052             status |= LyXFunc::ToggleOff;
2053         break;
2054     case LyXTabular::SET_LTFOOT:
2055         if (tabular->GetRowOfLTFoot(actcell, dummy))
2056             status |= LyXFunc::ToggleOn;
2057         else
2058             status |= LyXFunc::ToggleOff;
2059         break;
2060     case LyXTabular::SET_LTLASTFOOT:
2061         if (tabular->GetRowOfLTFoot(actcell, dummy))
2062             status |= LyXFunc::ToggleOn;
2063         else
2064             status |= LyXFunc::ToggleOff;
2065         break;
2066     case LyXTabular::SET_LTNEWPAGE:
2067         if (tabular->GetLTNewPage(actcell))
2068             status |= LyXFunc::ToggleOn;
2069         else
2070             status |= LyXFunc::ToggleOff;
2071         break;
2072     default:
2073         status = LyXFunc::Disabled;
2074         break;
2075     }
2076     return status;
2077 }
2078
2079
2080 bool InsetTabular::copySelection(BufferView * bv)
2081 {
2082     if (!hasSelection())
2083         return false;
2084     delete paste_tabular;
2085
2086     int sel_col_start;
2087     int sel_col_end;
2088     int sel_row_start;
2089     int sel_row_end;
2090
2091     sel_col_start = tabular->column_of_cell(sel_cell_start);
2092     sel_col_end = tabular->column_of_cell(sel_cell_end);
2093     if (sel_col_start > sel_col_end) {
2094         sel_col_start = sel_col_end;
2095         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2096     } else {
2097         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2098     }
2099     int columns = sel_col_end - sel_col_start + 1;
2100
2101     sel_row_start = tabular->row_of_cell(sel_cell_start);
2102     sel_row_end = tabular->row_of_cell(sel_cell_end);
2103     if (sel_row_start > sel_row_end) {
2104             //int tmp tmp = sel_row_start;
2105             //sel_row_start = sel_row_end;
2106             //sel_row_end = tmp;
2107         swap(sel_row_start, sel_row_end);
2108     }
2109     int rows = sel_row_end - sel_row_start + 1;
2110
2111     paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2112     int i;
2113     for (i=0; i < sel_row_start; ++i)
2114         paste_tabular->DeleteRow(0);
2115     while(paste_tabular->rows() > rows)
2116         paste_tabular->DeleteRow(rows);
2117     paste_tabular->SetTopLine(0, true, true);
2118     paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2119                                  true, true);
2120     for (i=0; i < sel_col_start; ++i)
2121         paste_tabular->DeleteColumn(0);
2122     while(paste_tabular->columns() > columns)
2123         paste_tabular->DeleteColumn(columns);
2124     paste_tabular->SetLeftLine(0, true, true);
2125     paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2126
2127     ostringstream sstr;
2128     paste_tabular->Ascii(bv->buffer(), sstr);
2129     bv->stuffClipboard(sstr.str().c_str());
2130     return true;
2131 }
2132
2133
2134 bool InsetTabular::pasteSelection(BufferView * bv)
2135 {
2136     if (!paste_tabular)
2137         return false;
2138
2139     for (int r1 = 0, r2 = actrow;
2140          (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2141          ++r1, ++r2)
2142     {
2143         for(int c1 = 0, c2 = actcol;
2144             (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2145             ++c1, ++c2)
2146         {
2147             if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2148                 tabular->IsPartOfMultiColumn(r2,c2))
2149                 continue;
2150             if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2151                 --c2;
2152                 continue;
2153             }
2154             if (tabular->IsPartOfMultiColumn(r2,c2)) {
2155                 --c1;
2156                 continue;
2157             }
2158             int n1 = paste_tabular->GetCellNumber(r1, c1);
2159             int n2 = tabular->GetCellNumber(r2, c2);
2160             *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2161             tabular->GetCellInset(n2)->setOwner(this);
2162             tabular->GetCellInset(n2)->deleteLyXText(bv);
2163         }
2164     }
2165     return true;
2166 }
2167
2168
2169 bool InsetTabular::cutSelection()
2170 {
2171     if (!hasSelection())
2172         return false;
2173
2174     int sel_col_start;
2175     int sel_col_end;
2176     int sel_row_start;
2177     int sel_row_end;
2178
2179     sel_col_start = tabular->column_of_cell(sel_cell_start);
2180     sel_col_end = tabular->column_of_cell(sel_cell_end);
2181     if (sel_col_start > sel_col_end) {
2182         sel_col_start = sel_col_end;
2183         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2184     } else {
2185         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2186     }
2187     sel_row_start = tabular->row_of_cell(sel_cell_start);
2188     sel_row_end = tabular->row_of_cell(sel_cell_end);
2189     if (sel_row_start > sel_row_end) {
2190             //int tmp = sel_row_start;
2191             //sel_row_start = sel_row_end;
2192             //sel_row_end = tmp;
2193         swap(sel_row_start, sel_row_end);
2194     }
2195     if (sel_cell_start > sel_cell_end) {
2196             //int tmp = sel_cell_start;
2197             //sel_cell_start = sel_cell_end;
2198             //sel_cell_end = tmp;
2199         swap(sel_cell_start, sel_cell_end);
2200     }
2201     for (int i = sel_row_start; i <= sel_row_end; ++i) {
2202         for (int j = sel_col_start; j <= sel_col_end; ++j) {
2203             tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2204         }
2205     }
2206     return true;
2207 }
2208
2209 bool InsetTabular::isRightToLeft(BufferView *bv )
2210 {
2211         return bv->getParentLanguage(this)->RightToLeft();
2212 }