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