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