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