]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Fixed crash when deleting column and having the inset locked.
[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
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 = 1;
974             int rows = 1;
975             int maxCols = 1;
976             unsigned int len = clip.length();
977             string::size_type p = 0;
978
979             while((p < len) &&
980                   ((p = clip.find_first_of("\t\n", p)) != string::npos))
981             {
982                 switch(clip[p]) {
983                 case '\t':
984                     ++cols;
985                     break;
986                 case '\n':
987                     if ((p+1) < len)
988                         ++rows;
989                     maxCols = max(cols, maxCols);
990                     cols = 1;
991                     break;
992                 }
993                 ++p;
994             }
995             maxCols = max(cols, maxCols);
996             delete paste_tabular;
997             paste_tabular = new LyXTabular(this, rows, maxCols);
998             string::size_type op = 0;
999             int cell = 0;
1000             int cells = paste_tabular->GetNumberOfCells();
1001             p = cols = 0;
1002             while((cell < cells) && (p < len) &&
1003                   (p = clip.find_first_of("\t\n", p)) != string::npos)
1004             {
1005                 if (p >= len)
1006                     break;
1007                 switch(clip[p]) {
1008                 case '\t':
1009                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1010                     ++cols;
1011                     ++cell;
1012                     break;
1013                 case '\n':
1014                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1015                     while(cols++ < maxCols)
1016                         ++cell;
1017                     cols = 0;
1018                     break;
1019                 }
1020                 ++p;
1021                 op = p;
1022             }
1023             // check for the last cell if there is no trailing '\n'
1024             if ((cell < cells) && (op < len))
1025                 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1026         } else {
1027             // so that the clipboard is used and it goes on to default
1028             // and executes LFUN_PASTESELECTION in insettext!
1029             delete paste_tabular;
1030             paste_tabular = 0;
1031         }
1032     }
1033     case LFUN_PASTE:
1034         if (hasPasteBuffer()) {
1035             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1036 #ifndef NEW_INSETS
1037               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1038               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1039 #else
1040               bv->text->cursor.par()->previous,
1041               bv->text->cursor.par()->next
1042 #endif
1043                 );
1044             pasteSelection(bv);
1045             UpdateLocal(bv, INIT, true);
1046             break;
1047         }
1048     // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1049     default:
1050         // we try to activate the actual inset and put this event down to
1051         // the insets dispatch function.
1052         result = UNDISPATCHED;
1053         if (the_locking_inset)
1054             break;
1055         if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
1056             result=the_locking_inset->LocalDispatch(bv, action, arg);
1057             if (result == DISPATCHED_NOUPDATE)
1058                 return result;
1059             else if (result == DISPATCHED) {
1060                 the_locking_inset->ToggleInsetCursor(bv);
1061                 UpdateLocal(bv, CELL, false);
1062                 the_locking_inset->ToggleInsetCursor(bv);
1063                 return result;
1064             } else {
1065                 result = UNDISPATCHED;
1066             }
1067         }
1068         break;
1069     }
1070     if (result!=FINISHED) {
1071         if (!the_locking_inset) {
1072             ShowInsetCursor(bv);
1073         }
1074     } else
1075         bv->unlockInset(this);
1076     return result;
1077 }
1078
1079
1080 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1081                         bool fragile, bool fp) const
1082 {
1083     return tabular->Latex(buf, os, fragile, fp);
1084 }
1085
1086
1087 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1088 {
1089     // This should be changed to a real ascii export
1090     return tabular->Ascii(buf, os);
1091 }
1092
1093
1094 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1095 {
1096     return 0;
1097 }
1098
1099
1100 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1101 {
1102     return tabular->DocBook(buf,os);
1103 }
1104
1105
1106 void InsetTabular::Validate(LaTeXFeatures & features) const
1107 {
1108     tabular->Validate(features);
1109 }
1110
1111
1112 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1113                                                  LyXFont const & font,
1114                                                  bool reinit) const
1115 {
1116     int cell = -1;
1117     int maxAsc = 0;
1118     int maxDesc = 0;
1119     InsetText * inset;
1120     bool changed = false;
1121     
1122     // if we have a locking_inset we should have to check only this cell for
1123     // change so I'll try this to have a boost, but who knows ;)
1124     if ((need_update != INIT) &&
1125         (the_locking_inset == tabular->GetCellInset(actcell))) {
1126         for(int i = 0; i < tabular->columns(); ++i) {
1127             maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1128                          maxAsc);
1129             maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1130                           maxDesc);
1131         }
1132         changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1133         changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1134         changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1135         return changed;
1136     }
1137     for (int i = 0; i < tabular->rows(); ++i) {
1138         maxAsc = 0;
1139         maxDesc = 0;
1140         for (int j= 0; j < tabular->columns(); ++j) {
1141             if (tabular->IsPartOfMultiColumn(i,j))
1142                 continue;
1143             ++cell;
1144             inset = tabular->GetCellInset(cell);
1145             if (!reinit)
1146                 inset->update(bv, font, false);
1147             maxAsc = max(maxAsc, inset->ascent(bv, font));
1148             maxDesc = max(maxDesc, inset->descent(bv, font));
1149             changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1150         }
1151         changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1152         changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1153     }
1154     return changed;
1155 }
1156
1157
1158 void InsetTabular::GetCursorPos(BufferView *,
1159                                 int & x, int & y) const
1160 {
1161     x = cursor.x() - top_x;
1162     y = cursor.y();
1163 }
1164
1165
1166 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1167 {
1168     if (the_locking_inset) {
1169         the_locking_inset->ToggleInsetCursor(bv);
1170         return;
1171     }
1172
1173     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1174
1175     int const asc = lyxfont::maxAscent(font);
1176     int const desc = lyxfont::maxDescent(font);
1177   
1178     if (cursor_visible)
1179         bv->hideLockedInsetCursor();
1180     else
1181         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1182     cursor_visible = !cursor_visible;
1183 }
1184
1185
1186 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1187 {
1188     if (!cursor_visible) {
1189         LyXFont font; // = GetFont(par, cursor.pos);
1190     
1191         int const asc = lyxfont::maxAscent(font);
1192         int const desc = lyxfont::maxDescent(font);
1193         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1194         if (show)
1195             bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1196         cursor_visible = true;
1197     }
1198 }
1199
1200
1201 void InsetTabular::HideInsetCursor(BufferView * bv)
1202 {
1203     if (cursor_visible) {
1204         bv->hideLockedInsetCursor();
1205         cursor_visible = false;
1206     }
1207 //    if (cursor_visible)
1208 //        ToggleInsetCursor(bv);
1209 }
1210
1211
1212 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1213 {
1214     cursor.y(0);
1215     cursor.pos(0);
1216         
1217     actcell = actrow = actcol = 0;
1218     int ly = tabular->GetDescentOfRow(actrow);
1219
1220     // first search the right row
1221     while((ly < y) && (actrow < tabular->rows())) {
1222         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1223             tabular->GetAscentOfRow(actrow + 1) +
1224             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1225                                                                 actcol)));
1226         ++actrow;
1227         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1228     }
1229     actcell = tabular->GetCellNumber(actrow, actcol);
1230
1231     // now search the right column
1232     int lx = tabular->GetWidthOfColumn(actcell) -
1233         tabular->GetAdditionalWidth(actcell);
1234 #if 0
1235 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1236     for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1237         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1238             tabular->GetAdditionalWidth(actcell - 1));
1239 #else
1240     // Jürgen, you should check that this is correct. (Lgb)
1241     for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1242             lx += tabular->GetWidthOfColumn(actcell + 1)
1243                     + tabular->GetAdditionalWidth(actcell);
1244     }
1245     
1246 #endif
1247     cursor.pos(0);
1248     resetPos(bv);
1249     if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1250         cursor.x(lx + top_x - 2);
1251         cursor.pos(1);
1252     } else {
1253         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1254     }
1255     resetPos(bv);
1256 }
1257
1258
1259 int InsetTabular::getCellXPos(int cell) const
1260 {
1261     int c = cell;
1262
1263     for (; !tabular->IsFirstCellInRow(c); --c)
1264         ;
1265     int lx = tabular->GetWidthOfColumn(cell);
1266     for (; c < cell; ++c) {
1267         lx += tabular->GetWidthOfColumn(c);
1268     }
1269     return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1270 }
1271
1272
1273 void InsetTabular::resetPos(BufferView * bv) const
1274 {
1275     if (!locked)
1276         return;
1277     actcol = tabular->column_of_cell(actcell);
1278
1279     int cell = 0;
1280     actrow = 0;
1281     cursor.y(0);
1282     for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1283         if (tabular->IsLastCellInRow(cell)) {
1284             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1285                 tabular->GetAscentOfRow(actrow + 1) +
1286                 tabular->GetAdditionalHeight(cell + 1));
1287             ++actrow;
1288         }
1289     }
1290     static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1291     int new_x = getCellXPos(actcell);
1292     int old_x = cursor.x();
1293     new_x += offset;
1294     cursor.x(new_x);
1295 //    cursor.x(getCellXPos(actcell) + offset);
1296     if (((cursor.x() - offset) > 20) &&
1297         ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1298          (bv->workWidth()-20)))
1299     {
1300         scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1301         UpdateLocal(bv, FULL, false);
1302     } else if ((cursor.x() - offset) < 20) {
1303         scroll(bv, 20 - cursor.x() + offset);
1304         UpdateLocal(bv, FULL, false);
1305     } else if (!cellstart(cursor.pos())) {
1306         LyXFont font(LyXFont::ALL_SANE);
1307         cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1308                 tabular->GetBeginningOfTextInCell(actcell));
1309     } else if (scroll() && (top_x > 20) &&
1310                ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1311         scroll(bv, old_x - cursor.x());
1312     }
1313     if ((!the_locking_inset ||
1314          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1315         (actcell != oldcell)) {
1316             InsetTabular * inset = const_cast<InsetTabular *>(this);
1317             bv->owner()->getDialogs()->updateTabular(inset);
1318             oldcell = actcell;
1319     }
1320 }
1321
1322
1323 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1324 {
1325     if (!cellstart(cursor.pos())) {
1326         if (tabular->IsLastCell(actcell))
1327             return FINISHED;
1328         ++actcell;
1329         cursor.pos(0);
1330     } else if (lock) {
1331         if (ActivateCellInset(bv))
1332             return DISPATCHED;
1333     } else {              // before the inset
1334         cursor.pos(1);
1335     }
1336     resetPos(bv);
1337     return DISPATCHED_NOUPDATE;
1338 }
1339
1340
1341 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1342 {
1343     if (cellstart(cursor.pos()) && !actcell)
1344         return FINISHED;
1345     if (cellstart(cursor.pos())) {
1346         --actcell;
1347         cursor.pos(1);
1348     } else if (lock) {       // behind the inset
1349         cursor.pos(0);
1350         if (ActivateCellInset(bv, 0, 0, 0, true))
1351             return DISPATCHED;
1352     } else {
1353         cursor.pos(0);
1354     }
1355     resetPos(bv);
1356     return DISPATCHED_NOUPDATE;
1357 }
1358
1359
1360 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1361 {
1362     int const ocell = actcell;
1363     actcell = tabular->GetCellAbove(actcell);
1364     if (actcell == ocell) // we moved out of the inset
1365         return FINISHED;
1366     resetPos(bv);
1367     return DISPATCHED_NOUPDATE;
1368 }
1369
1370
1371 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1372 {
1373     int const ocell = actcell;
1374     actcell = tabular->GetCellBelow(actcell);
1375     if (actcell == ocell) // we moved out of the inset
1376         return FINISHED;
1377     resetPos(bv);
1378     return DISPATCHED_NOUPDATE;
1379 }
1380
1381
1382 bool InsetTabular::moveNextCell(BufferView * bv)
1383 {
1384     if (tabular->IsLastCell(actcell))
1385         return false;
1386     ++actcell;
1387     resetPos(bv);
1388     return true;
1389 }
1390
1391
1392 bool InsetTabular::movePrevCell(BufferView * bv)
1393 {
1394     if (!actcell) // first cell
1395         return false;
1396     --actcell;
1397     resetPos(bv);
1398     return true;
1399 }
1400
1401
1402 bool InsetTabular::Delete()
1403 {
1404     return true;
1405 }
1406
1407
1408 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1409 {
1410     if (the_locking_inset)
1411         the_locking_inset->SetFont(bv, font, tall);
1412 }
1413
1414
1415 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1416 {
1417     LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1418
1419     int i = 0;
1420     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1421         string const tmp = tabularFeatures[i].feature;
1422             
1423         if (tmp == what.substr(0, tmp.length())) {
1424         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1425               //tabularFeatures[i].feature.length())) {
1426             action = tabularFeatures[i].action;
1427             break;
1428         }
1429     }
1430     if (action == LyXTabular::LAST_ACTION)
1431         return false;
1432
1433     string const val =
1434             frontStrip(what.substr(tabularFeatures[i].feature.length()));
1435     TabularFeatures(bv, action, val);
1436     return true;
1437 }
1438
1439
1440 void InsetTabular::TabularFeatures(BufferView * bv,
1441                                    LyXTabular::Feature feature,
1442                                    string const & value)
1443 {
1444     int i;
1445     int j;
1446     int sel_col_start;
1447     int sel_col_end;
1448     int sel_row_start;
1449     int sel_row_end;
1450     int setLines = 0;
1451     LyXAlignment setAlign = LYX_ALIGN_LEFT;
1452     LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1453     int lineSet;
1454     bool what;
1455
1456     switch (feature) {
1457       case LyXTabular::M_ALIGN_LEFT:
1458       case LyXTabular::ALIGN_LEFT:
1459           setAlign=LYX_ALIGN_LEFT;
1460           break;
1461       case LyXTabular::M_ALIGN_RIGHT:
1462       case LyXTabular::ALIGN_RIGHT:
1463           setAlign=LYX_ALIGN_RIGHT;
1464           break;
1465       case LyXTabular::M_ALIGN_CENTER:
1466       case LyXTabular::ALIGN_CENTER:
1467           setAlign=LYX_ALIGN_CENTER;
1468           break;
1469       case LyXTabular::M_VALIGN_TOP:
1470       case LyXTabular::VALIGN_TOP:
1471           setVAlign=LyXTabular::LYX_VALIGN_TOP;
1472           break;
1473       case LyXTabular::M_VALIGN_BOTTOM:
1474       case LyXTabular::VALIGN_BOTTOM:
1475           setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1476           break;
1477       case LyXTabular::M_VALIGN_CENTER:
1478       case LyXTabular::VALIGN_CENTER:
1479           setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1480           break;
1481       default:
1482           break;
1483     }
1484     if (hasSelection()) {
1485         sel_col_start = tabular->column_of_cell(sel_cell_start);
1486         sel_col_end = tabular->column_of_cell(sel_cell_end);
1487         if (sel_col_start > sel_col_end) {
1488             sel_col_end = sel_col_start;
1489             sel_col_start = tabular->column_of_cell(sel_cell_end);
1490         } else {
1491             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1492         }
1493         
1494         sel_row_start = tabular->row_of_cell(sel_cell_start);
1495         sel_row_end = tabular->row_of_cell(sel_cell_end);
1496         if (sel_row_start > sel_row_end) {
1497                 //int tmp = sel_row_start;
1498                 //sel_row_start = sel_row_end;
1499                 //sel_row_end = tmp;
1500             swap(sel_row_start, sel_row_end);
1501         }
1502     } else {
1503         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1504         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1505     }
1506     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1507 #ifndef NEW_INSETS
1508               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1509               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1510 #else
1511               bv->text->cursor.par()->previous,
1512               bv->text->cursor.par()->next
1513 #endif
1514             );
1515
1516     int row = tabular->row_of_cell(actcell);
1517     int column = tabular->column_of_cell(actcell);
1518     bool flag = true;
1519     
1520     switch (feature) {
1521     case LyXTabular::SET_PWIDTH:
1522     {
1523         bool const update = (tabular->GetColumnPWidth(actcell) != value);
1524         tabular->SetColumnPWidth(actcell,value);
1525         if (update) {
1526             for (int i=0; i < tabular->rows(); ++i) {
1527                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1528                     resizeLyXText(bv);
1529             }
1530             UpdateLocal(bv, INIT, true);
1531         }
1532     }
1533     break;
1534     case LyXTabular::SET_MPWIDTH:
1535     {
1536         bool const update = (tabular->GetPWidth(actcell) != value);
1537         tabular->SetMColumnPWidth(actcell,value);
1538         if (update) {
1539             for (int i=0; i < tabular->rows(); ++i) {
1540                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1541                     resizeLyXText(bv);
1542             }
1543             UpdateLocal(bv, INIT, true);
1544         }
1545     }
1546     break;
1547     case LyXTabular::SET_SPECIAL_COLUMN:
1548     case LyXTabular::SET_SPECIAL_MULTI:
1549         tabular->SetAlignSpecial(actcell,value,feature);
1550         break;
1551     case LyXTabular::APPEND_ROW:
1552         // append the row into the tabular
1553         UnlockInsetInInset(bv, the_locking_inset);
1554         tabular->AppendRow(actcell);
1555         UpdateLocal(bv, INIT, true);
1556         break;
1557     case LyXTabular::APPEND_COLUMN:
1558         // append the column into the tabular
1559         UnlockInsetInInset(bv, the_locking_inset);
1560         tabular->AppendColumn(actcell);
1561         actcell = tabular->GetCellNumber(row, column);
1562         UpdateLocal(bv, INIT, true);
1563         break;
1564     case LyXTabular::DELETE_ROW:
1565         UnlockInsetInInset(bv, the_locking_inset);
1566         tabular->DeleteRow(tabular->row_of_cell(actcell));
1567         if ((row+1) > tabular->rows())
1568             --row;
1569         actcell = tabular->GetCellNumber(row, column);
1570         clearSelection();
1571         UpdateLocal(bv, INIT, true);
1572         break;
1573     case LyXTabular::DELETE_COLUMN:
1574         UnlockInsetInInset(bv, the_locking_inset);
1575         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1576         if ((column+1) > tabular->columns())
1577             --column;
1578         actcell = tabular->GetCellNumber(row, column);
1579         clearSelection();
1580         UpdateLocal(bv, INIT, true);
1581         break;
1582     case LyXTabular::M_TOGGLE_LINE_TOP:
1583         flag = false;
1584     case LyXTabular::TOGGLE_LINE_TOP:
1585         lineSet = !tabular->TopLine(actcell, flag);
1586         for (i=sel_row_start; i<=sel_row_end; ++i)
1587             for (j=sel_col_start; j<=sel_col_end; ++j)
1588                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1589         UpdateLocal(bv, INIT, true);
1590         break;
1591     
1592     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1593         flag = false;
1594     case LyXTabular::TOGGLE_LINE_BOTTOM:
1595         lineSet = !tabular->BottomLine(actcell, flag); 
1596         for (i=sel_row_start; i<=sel_row_end; ++i)
1597             for (j=sel_col_start; j<=sel_col_end; ++j)
1598                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1599                                        flag);
1600         UpdateLocal(bv, INIT, true);
1601         break;
1602                 
1603     case LyXTabular::M_TOGGLE_LINE_LEFT:
1604         flag = false;
1605     case LyXTabular::TOGGLE_LINE_LEFT:
1606         lineSet = !tabular->LeftLine(actcell, flag);
1607         for (i=sel_row_start; i<=sel_row_end; ++i)
1608             for (j=sel_col_start; j<=sel_col_end; ++j)
1609                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1610                                      flag);
1611         UpdateLocal(bv, INIT, true);
1612         break;
1613
1614     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1615         flag = false;
1616     case LyXTabular::TOGGLE_LINE_RIGHT:
1617         lineSet = !tabular->RightLine(actcell, flag);
1618         for (i=sel_row_start; i<=sel_row_end; ++i)
1619             for (j=sel_col_start; j<=sel_col_end; ++j)
1620                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1621                                       flag);
1622         UpdateLocal(bv, INIT, true);
1623         break;
1624     case LyXTabular::M_ALIGN_LEFT:
1625     case LyXTabular::M_ALIGN_RIGHT:
1626     case LyXTabular::M_ALIGN_CENTER:
1627         flag = false;
1628     case LyXTabular::ALIGN_LEFT:
1629     case LyXTabular::ALIGN_RIGHT:
1630     case LyXTabular::ALIGN_CENTER:
1631         for (i = sel_row_start; i <= sel_row_end; ++i)
1632             for (j = sel_col_start; j <= sel_col_end; ++j)
1633                 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1634                                       flag);
1635         if (hasSelection())
1636             UpdateLocal(bv, INIT, true);
1637         else
1638             UpdateLocal(bv, CELL, true);
1639         break;
1640     case LyXTabular::M_VALIGN_TOP:
1641     case LyXTabular::M_VALIGN_BOTTOM:
1642     case LyXTabular::M_VALIGN_CENTER:
1643         flag = false;
1644     case LyXTabular::VALIGN_TOP:
1645     case LyXTabular::VALIGN_BOTTOM:
1646     case LyXTabular::VALIGN_CENTER:
1647         for (i = sel_row_start; i <= sel_row_end; ++i)
1648             for (j = sel_col_start; j <= sel_col_end; ++j)
1649                 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1650                                        setVAlign, flag);
1651         if (hasSelection())
1652             UpdateLocal(bv, INIT, true);
1653         else
1654             UpdateLocal(bv, CELL, 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         cursor.pos(0);
1691         sel_cell_end = sel_cell_start;
1692         sel_pos_end = sel_pos_start;
1693         UpdateLocal(bv, INIT, true);
1694         break;
1695     }
1696     case LyXTabular::SET_ALL_LINES:
1697         setLines = 1;
1698     case LyXTabular::UNSET_ALL_LINES:
1699         for (i=sel_row_start; i<=sel_row_end; ++i)
1700             for (j=sel_col_start; j<=sel_col_end; ++j)
1701                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1702         UpdateLocal(bv, INIT, true);
1703         break;
1704     case LyXTabular::SET_LONGTABULAR:
1705         tabular->SetLongTabular(true);
1706         UpdateLocal(bv, INIT, true); // because this toggles displayed
1707         break;
1708     case LyXTabular::UNSET_LONGTABULAR:
1709         tabular->SetLongTabular(false);
1710         UpdateLocal(bv, INIT, true); // because this toggles displayed
1711         break;
1712     case LyXTabular::SET_ROTATE_TABULAR:
1713         tabular->SetRotateTabular(true);
1714         break;
1715     case LyXTabular::UNSET_ROTATE_TABULAR:
1716         tabular->SetRotateTabular(false);
1717         break;
1718     case LyXTabular::SET_ROTATE_CELL:
1719         for (i=sel_row_start; i<=sel_row_end; ++i)
1720             for (j=sel_col_start; j<=sel_col_end; ++j)
1721                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1722         break;
1723     case LyXTabular::UNSET_ROTATE_CELL:
1724         for (i = sel_row_start; i <= sel_row_end; ++i)
1725             for (j = sel_col_start; j <= sel_col_end; ++j)
1726                 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1727         break;
1728     case LyXTabular::SET_USEBOX:
1729     {
1730         LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1731         if (val == tabular->GetUsebox(actcell))
1732             val = LyXTabular::BOX_NONE;
1733         for (i = sel_row_start; i <= sel_row_end; ++i)
1734             for (j = sel_col_start; j <= sel_col_end; ++j)
1735                 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1736         break;
1737     }
1738     case LyXTabular::SET_LTFIRSTHEAD:
1739         tabular->SetLTHead(actcell, true);
1740         break;
1741     case LyXTabular::SET_LTHEAD:
1742         tabular->SetLTHead(actcell, false);
1743         break;
1744     case LyXTabular::SET_LTFOOT:
1745         tabular->SetLTFoot(actcell, false);
1746         break;
1747     case LyXTabular::SET_LTLASTFOOT:
1748         tabular->SetLTFoot(actcell, true);
1749         break;
1750     case LyXTabular::SET_LTNEWPAGE:
1751         what = !tabular->GetLTNewPage(actcell);
1752         tabular->SetLTNewPage(actcell, what);
1753         break;
1754     // dummy stuff just to avoid warnings
1755     case LyXTabular::LAST_ACTION:
1756         break;
1757     }
1758 }
1759
1760
1761 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1762                                      bool behind)
1763 {
1764     // the cursor.pos has to be before the inset so if it isn't now just
1765     // reset the curor pos first!
1766     if (!cellstart(cursor.pos())) {
1767         cursor.pos(0);
1768         resetPos(bv);
1769     }
1770     UpdatableInset * inset =
1771         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1772     LyXFont font(LyXFont::ALL_SANE);
1773     if (behind) {
1774         x = inset->x() + inset->width(bv, font);
1775         y = inset->descent(bv, font);
1776     }
1777     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1778     inset_y = cursor.y();
1779     inset->Edit(bv, x - inset_x, y - inset_y, button);
1780     if (!the_locking_inset)
1781         return false;
1782     UpdateLocal(bv, CELL, false);
1783     return (the_locking_inset != 0);
1784 }
1785
1786
1787 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1788 {
1789     InsetText * inset = tabular->GetCellInset(actcell);
1790     int const x1 = x + top_x;
1791
1792     if (!cellstart(cursor.pos())) {
1793         return ((x + top_x) < cursor.x() &&
1794                 (x + top_x) > (cursor.x() - inset->width(bv,
1795                                                       LyXFont(LyXFont::ALL_SANE))));
1796     } else {
1797         int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1798         return (x1 > x2 &&
1799                 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1800     }
1801 }
1802
1803
1804 // This returns paperWidth() if the cell-width is unlimited or the width
1805 // in pixels if we have a pwidth for this cell.
1806 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1807 {
1808     string const s = tabular->GetPWidth(cell);
1809
1810     if (s.empty())
1811         return -1;
1812     return VSpace(s).inPixels(0, 0);
1813 }
1814
1815
1816 int InsetTabular::getMaxWidth(Painter & pain,
1817                               UpdatableInset const * inset) const
1818 {
1819     int const n = tabular->GetNumberOfCells();
1820     int cell = 0;
1821     for (; cell < n; ++cell) {
1822         if (tabular->GetCellInset(cell) == inset)
1823             break;
1824     }
1825     if (cell >= n)
1826         return -1;
1827     int w = GetMaxWidthOfCell(pain, cell);
1828     if (w > 0)
1829         // because the inset then subtracts it's top_x and owner->x()
1830         w += (inset->x() - top_x);
1831     return w;
1832 }
1833
1834
1835 void InsetTabular::resizeLyXText(BufferView *) const
1836 {
1837     need_update = FULL;
1838 }
1839
1840
1841 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1842 {
1843     if (the_locking_inset)
1844         return the_locking_inset->getLyXText(bv);
1845     return Inset::getLyXText(bv);
1846 }
1847
1848
1849 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1850 {
1851     if (the_locking_inset) {
1852         InsetTabular * i = static_cast<InsetTabular *>
1853             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1854         if (i) {
1855             i->OpenLayoutDialog(bv);
1856             return;
1857         }
1858     }
1859     bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1860 }
1861
1862 //
1863 // functions returns:
1864 // 0 ... disabled
1865 // 1 ... enabled
1866 // 2 ... toggled on
1867 // 3 ... toggled off
1868 //
1869 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1870 {
1871     int action = LyXTabular::LAST_ACTION;
1872     LyXFunc::func_status status = LyXFunc::OK;
1873     
1874     int i = 0;
1875     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1876         string const tmp = tabularFeatures[i].feature;
1877         if (tmp == what.substr(0, tmp.length())) {                  
1878         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1879         //   tabularFeatures[i].feature.length())) {
1880             action = tabularFeatures[i].action;
1881             break;
1882         }
1883     }
1884     if (action == LyXTabular::LAST_ACTION)
1885         return LyXFunc::Unknown;
1886
1887     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1888
1889     int sel_row_start, sel_row_end;
1890     int dummy;
1891     bool flag = true;
1892
1893     if (hasSelection()) {
1894         sel_row_start = tabular->row_of_cell(sel_cell_start);
1895         sel_row_end = tabular->row_of_cell(sel_cell_end);
1896         if (sel_row_start > sel_row_end) {
1897                 //int tmp = sel_row_start;
1898                 //sel_row_start = sel_row_end;
1899                 //sel_row_end = tmp;
1900             swap(sel_row_start, sel_row_end);
1901         }
1902     } else {
1903         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1904     }
1905
1906     switch (action) {
1907     case LyXTabular::SET_PWIDTH:
1908     case LyXTabular::SET_MPWIDTH:
1909     case LyXTabular::SET_SPECIAL_COLUMN:
1910     case LyXTabular::SET_SPECIAL_MULTI:
1911         status |= LyXFunc::Disabled;
1912         return status;
1913
1914     case LyXTabular::APPEND_ROW:
1915     case LyXTabular::APPEND_COLUMN:
1916     case LyXTabular::DELETE_ROW:
1917     case LyXTabular::DELETE_COLUMN:
1918     case LyXTabular::SET_ALL_LINES:
1919     case LyXTabular::UNSET_ALL_LINES:
1920         status |= LyXFunc::OK;
1921         return status;
1922
1923     case LyXTabular::MULTICOLUMN:
1924         if (tabular->IsMultiColumn(actcell))
1925             status |= LyXFunc::ToggleOn;
1926         else
1927             status |= LyXFunc::ToggleOff;
1928         break;
1929     case LyXTabular::M_TOGGLE_LINE_TOP:
1930         flag = false;
1931     case LyXTabular::TOGGLE_LINE_TOP:
1932         if (tabular->TopLine(actcell, flag))
1933             status |= LyXFunc::ToggleOn;
1934         else
1935             status |= LyXFunc::ToggleOff;
1936         break;
1937     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1938         flag = false;
1939     case LyXTabular::TOGGLE_LINE_BOTTOM:
1940         if (tabular->BottomLine(actcell, flag))
1941             status |= LyXFunc::ToggleOn;
1942         else
1943             status |= LyXFunc::ToggleOff;
1944         break;
1945     case LyXTabular::M_TOGGLE_LINE_LEFT:
1946         flag = false;
1947     case LyXTabular::TOGGLE_LINE_LEFT:
1948         if (tabular->LeftLine(actcell, flag))
1949             status |= LyXFunc::ToggleOn;
1950         else
1951             status |= LyXFunc::ToggleOff;
1952         break;
1953     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1954         flag = false;
1955     case LyXTabular::TOGGLE_LINE_RIGHT:
1956         if (tabular->RightLine(actcell, flag))
1957             status |= LyXFunc::ToggleOn;
1958         else
1959             status |= LyXFunc::ToggleOff;
1960         break;
1961     case LyXTabular::M_ALIGN_LEFT:
1962         flag = false;
1963     case LyXTabular::ALIGN_LEFT:
1964         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1965             status |= LyXFunc::ToggleOn;
1966         else
1967             status |= LyXFunc::ToggleOff;
1968         break;
1969     case LyXTabular::M_ALIGN_RIGHT:
1970         flag = false;
1971     case LyXTabular::ALIGN_RIGHT:
1972         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1973             status |= LyXFunc::ToggleOn;
1974         else
1975             status |= LyXFunc::ToggleOff;
1976         break;
1977     case LyXTabular::M_ALIGN_CENTER:
1978         flag = false;
1979     case LyXTabular::ALIGN_CENTER:
1980         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1981             status |= LyXFunc::ToggleOn;
1982         else
1983             status |= LyXFunc::ToggleOff;
1984         break;
1985     case LyXTabular::M_VALIGN_TOP:
1986         flag = false;
1987     case LyXTabular::VALIGN_TOP:
1988         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1989             status |= LyXFunc::ToggleOn;
1990         else
1991             status |= LyXFunc::ToggleOff;
1992         break;
1993     case LyXTabular::M_VALIGN_BOTTOM:
1994         flag = false;
1995     case LyXTabular::VALIGN_BOTTOM:
1996         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1997             status |= LyXFunc::ToggleOn;
1998         else
1999             status |= LyXFunc::ToggleOff;
2000         break;
2001     case LyXTabular::M_VALIGN_CENTER:
2002         flag = false;
2003     case LyXTabular::VALIGN_CENTER:
2004         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2005             status |= LyXFunc::ToggleOn;
2006         else
2007             status |= LyXFunc::ToggleOff;
2008         break;
2009     case LyXTabular::SET_LONGTABULAR:
2010         if (tabular->IsLongTabular())
2011             status |= LyXFunc::ToggleOn;
2012         else
2013             status |= LyXFunc::ToggleOff;
2014         break;
2015     case LyXTabular::UNSET_LONGTABULAR:
2016         if (!tabular->IsLongTabular())
2017             status |= LyXFunc::ToggleOn;
2018         else
2019             status |= LyXFunc::ToggleOff;
2020         break;
2021     case LyXTabular::SET_ROTATE_TABULAR:
2022         if (tabular->GetRotateTabular())
2023             status |= LyXFunc::ToggleOn;
2024         else
2025             status |= LyXFunc::ToggleOff;
2026         break;
2027     case LyXTabular::UNSET_ROTATE_TABULAR:
2028         if (!tabular->GetRotateTabular())
2029             status |= LyXFunc::ToggleOn;
2030         else
2031             status |= LyXFunc::ToggleOff;
2032         break;
2033     case LyXTabular::SET_ROTATE_CELL:
2034         if (tabular->GetRotateCell(actcell))
2035             status |= LyXFunc::ToggleOn;
2036         else
2037             status |= LyXFunc::ToggleOff;
2038         break;
2039     case LyXTabular::UNSET_ROTATE_CELL:
2040         if (!tabular->GetRotateCell(actcell))
2041             status |= LyXFunc::ToggleOn;
2042         else
2043             status |= LyXFunc::ToggleOff;
2044         break;
2045     case LyXTabular::SET_USEBOX:
2046         if (strToInt(argument) == tabular->GetUsebox(actcell))
2047             status |= LyXFunc::ToggleOn;
2048         else
2049             status |= LyXFunc::ToggleOff;
2050         break;
2051     case LyXTabular::SET_LTFIRSTHEAD:
2052         if (tabular->GetRowOfLTHead(actcell, dummy))
2053             status |= LyXFunc::ToggleOn;
2054         else
2055             status |= LyXFunc::ToggleOff;
2056         break;
2057     case LyXTabular::SET_LTHEAD:
2058         if (tabular->GetRowOfLTHead(actcell, dummy))
2059             status |= LyXFunc::ToggleOn;
2060         else
2061             status |= LyXFunc::ToggleOff;
2062         break;
2063     case LyXTabular::SET_LTFOOT:
2064         if (tabular->GetRowOfLTFoot(actcell, dummy))
2065             status |= LyXFunc::ToggleOn;
2066         else
2067             status |= LyXFunc::ToggleOff;
2068         break;
2069     case LyXTabular::SET_LTLASTFOOT:
2070         if (tabular->GetRowOfLTFoot(actcell, dummy))
2071             status |= LyXFunc::ToggleOn;
2072         else
2073             status |= LyXFunc::ToggleOff;
2074         break;
2075     case LyXTabular::SET_LTNEWPAGE:
2076         if (tabular->GetLTNewPage(actcell))
2077             status |= LyXFunc::ToggleOn;
2078         else
2079             status |= LyXFunc::ToggleOff;
2080         break;
2081     default:
2082         status = LyXFunc::Disabled;
2083         break;
2084     }
2085     return status;
2086 }
2087
2088
2089 bool InsetTabular::copySelection(BufferView * bv)
2090 {
2091     if (!hasSelection())
2092         return false;
2093     delete paste_tabular;
2094
2095     int sel_col_start, sel_col_end;
2096     int sel_row_start, sel_row_end;
2097
2098     sel_col_start = tabular->column_of_cell(sel_cell_start);
2099     sel_col_end = tabular->column_of_cell(sel_cell_end);
2100     if (sel_col_start > sel_col_end) {
2101         sel_col_start = sel_col_end;
2102         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2103     } else {
2104         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2105     }
2106     int columns = sel_col_end - sel_col_start + 1;
2107
2108     sel_row_start = tabular->row_of_cell(sel_cell_start);
2109     sel_row_end = tabular->row_of_cell(sel_cell_end);
2110     if (sel_row_start > sel_row_end) {
2111             //int tmp tmp = sel_row_start;
2112             //sel_row_start = sel_row_end;
2113             //sel_row_end = tmp;
2114         swap(sel_row_start, sel_row_end);
2115     }
2116     int rows = sel_row_end - sel_row_start + 1;
2117
2118     paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2119     int i;
2120     for (i=0; i < sel_row_start; ++i)
2121         paste_tabular->DeleteRow(0);
2122     while(paste_tabular->rows() > rows)
2123         paste_tabular->DeleteRow(rows);
2124     paste_tabular->SetTopLine(0, true, true);
2125     paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2126                                  true, true);
2127     for (i=0; i < sel_col_start; ++i)
2128         paste_tabular->DeleteColumn(0);
2129     while(paste_tabular->columns() > columns)
2130         paste_tabular->DeleteColumn(columns);
2131     paste_tabular->SetLeftLine(0, true, true);
2132     paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2133
2134     ostringstream sstr;
2135     paste_tabular->Ascii(bv->buffer(), sstr);
2136     bv->stuffClipboard(sstr.str().c_str());
2137     return true;
2138 }
2139
2140
2141 bool InsetTabular::pasteSelection(BufferView * bv)
2142 {
2143     if (!paste_tabular)
2144         return false;
2145
2146     for (int r1 = 0, r2 = actrow;
2147          (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2148          ++r1, ++r2)
2149     {
2150         for(int c1 = 0, c2 = actcol;
2151             (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2152             ++c1, ++c2)
2153         {
2154             if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2155                 tabular->IsPartOfMultiColumn(r2,c2))
2156                 continue;
2157             if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2158                 --c2;
2159                 continue;
2160             }
2161             if (tabular->IsPartOfMultiColumn(r2,c2)) {
2162                 --c1;
2163                 continue;
2164             }
2165             int n1 = paste_tabular->GetCellNumber(r1, c1);
2166             int n2 = tabular->GetCellNumber(r2, c2);
2167             *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2168             tabular->GetCellInset(n2)->setOwner(this);
2169             tabular->GetCellInset(n2)->deleteLyXText(bv);
2170         }
2171     }
2172     return true;
2173 }
2174
2175
2176 bool InsetTabular::cutSelection()
2177 {
2178     if (!hasSelection())
2179         return false;
2180
2181     int sel_col_start, sel_col_end;
2182     int sel_row_start, sel_row_end;
2183
2184     sel_col_start = tabular->column_of_cell(sel_cell_start);
2185     sel_col_end = tabular->column_of_cell(sel_cell_end);
2186     if (sel_col_start > sel_col_end) {
2187         sel_col_start = sel_col_end;
2188         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2189     } else {
2190         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2191     }
2192     sel_row_start = tabular->row_of_cell(sel_cell_start);
2193     sel_row_end = tabular->row_of_cell(sel_cell_end);
2194     if (sel_row_start > sel_row_end) {
2195             //int tmp = sel_row_start;
2196             //sel_row_start = sel_row_end;
2197             //sel_row_end = tmp;
2198         swap(sel_row_start, sel_row_end);
2199     }
2200     if (sel_cell_start > sel_cell_end) {
2201             //int tmp = sel_cell_start;
2202             //sel_cell_start = sel_cell_end;
2203             //sel_cell_end = tmp;
2204         swap(sel_cell_start, sel_cell_end);
2205     }
2206     for (int i = sel_row_start; i <= sel_row_end; ++i) {
2207         for (int j = sel_col_start; j <= sel_col_end; ++j) {
2208             tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2209         }
2210     }
2211     return true;
2212 }
2213