]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
Fixes to insettabular/text.
[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         ShowInsetCursor(bv, false);
530         UpdateLocal(bv, CELL, false);
531         return true;
532     }
533     if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
534         if (inset->LyxCode() == TABULAR_CODE &&
535             !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
536         {
537             dialogs_ = bv->owner()->getDialogs();
538             dialogs_->updateTabular(this);
539             oldcell = actcell;
540         }
541         return true;
542     }
543     return false;
544 }
545
546
547 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
548 {
549     if (!the_locking_inset)
550         return false;
551     if (the_locking_inset != inset)
552         return the_locking_inset->UpdateInsetInInset(bv, inset);
553     UpdateLocal(bv, CELL, false);
554     return true;
555 }
556
557
558 unsigned int InsetTabular::InsetInInsetY()
559 {
560     if (!the_locking_inset)
561         return 0;
562
563     return (inset_y + the_locking_inset->InsetInInsetY());
564 }
565
566
567 UpdatableInset * InsetTabular::GetLockingInset()
568 {
569     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
570 }
571
572
573 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
574 {
575     if (c == LyxCode())
576         return this;
577     if (the_locking_inset)
578         return the_locking_inset->GetFirstLockingInsetOfType(c);
579     return 0;
580 }
581
582
583 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
584 {
585     if (the_locking_inset)
586         return the_locking_inset->InsertInset(bv, inset);
587     return false;
588 }
589
590
591 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
592 {
593     if (hasSelection()) {
594         sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
595         UpdateLocal(bv, SELECTION, false);
596     }
597     no_selection = false;
598
599     int const ocell = actcell;
600     int const orow = actrow;
601
602     HideInsetCursor(bv);
603     setPos(bv, x, y);
604     if (actrow != orow)
605         UpdateLocal(bv, NONE, false);
606     sel_pos_start = sel_pos_end = cursor.pos();
607     sel_cell_start = sel_cell_end = actcell;
608
609     bool const inset_hit = InsetHit(bv, x, y);
610
611     if ((ocell == actcell) && the_locking_inset && inset_hit) {
612         cursor.pos(0); // always before the inset!
613         resetPos(bv);
614         the_locking_inset->InsetButtonPress(bv,
615                                             x - inset_x, y - inset_y, button);
616         return;
617     } else if (the_locking_inset) {
618         the_locking_inset->InsetUnlock(bv);
619     }
620     the_locking_inset = 0;
621     if (inset_hit && bv->the_locking_inset) {
622         if (ActivateCellInset(bv, x, y, button))
623             the_locking_inset->InsetButtonPress(bv, x - inset_x,
624                                                 y - inset_y, button);
625         return;
626     }
627     ShowInsetCursor(bv);
628 }
629
630
631 void InsetTabular::InsetButtonRelease(BufferView * bv,
632                                       int x, int y, int button)
633 {
634     if (button == 3) {
635         if (the_locking_inset) {
636             UpdatableInset * i;
637             if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
638                 i->InsetButtonRelease(bv, x, y, button);
639                 return;
640             }
641         }
642         dialogs_ = bv->owner()->getDialogs();
643         dialogs_->showTabular(this);
644         return;
645     }
646     if (the_locking_inset) {
647         the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
648         return;
649     }
650     no_selection = false;
651 }
652
653
654 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
655 {
656     if (the_locking_inset) {
657         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
658                                              y - inset_y, button);
659         return;
660     }
661     if (!no_selection) {
662         HideInsetCursor(bv);
663         int const old_pos = sel_pos_end;
664         int const old_cell = actcell;
665
666         setPos(bv, x, y);
667         sel_pos_end = cursor.pos();
668         sel_cell_end = actcell;
669         if ((sel_cell_end != old_cell) || (old_pos != sel_pos_end))
670             UpdateLocal(bv, SELECTION, false);
671         ShowInsetCursor(bv);
672     }
673     no_selection = false;
674 }
675
676
677 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
678 {
679     if (the_locking_inset) {
680         the_locking_inset->InsetKeyPress(xke);
681         return;
682     }
683 }
684
685
686 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
687                                                    string const & arg)
688 {
689     UpdatableInset::RESULT 
690         result;
691
692     no_selection = false;
693     if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
694         || (result == DISPATCHED_NOUPDATE)) {
695
696         resetPos(bv);
697         return result;
698     }
699
700     if ((action < 0) && arg.empty())
701         return FINISHED;
702
703     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
704         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
705         cursor.x_fix(-1);
706     if (the_locking_inset) {
707         result=the_locking_inset->LocalDispatch(bv, action, arg);
708         if (result == DISPATCHED_NOUPDATE)
709             return result;
710         else if (result == DISPATCHED) {
711             the_locking_inset->ToggleInsetCursor(bv);
712             UpdateLocal(bv, CELL, false);
713             the_locking_inset->ToggleInsetCursor(bv);
714             return result;
715         } else if (result == FINISHED) {
716             if ((action == LFUN_RIGHT) || (action == -1)) {
717                 cursor.pos(inset_pos + 1);
718                 resetPos(bv);
719             }
720             sel_pos_start = sel_pos_end = cursor.pos();
721             sel_cell_start = sel_cell_end = actcell;
722             the_locking_inset=0;
723             ShowInsetCursor(bv);
724             result = DISPATCHED;
725             return result;
726         }
727     }
728
729     bool hs = hasSelection();
730     HideInsetCursor(bv);
731     result=DISPATCHED;
732     switch (action) {
733         // --- Cursor Movements ---------------------------------------------
734     case LFUN_RIGHTSEL:
735         if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
736             break;
737         moveRight(bv, false);
738 #if 0
739         if (!cellstart(cursor.pos())) {
740             sel_pos_end = 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 #else
748         sel_pos_end = cursor.pos();
749         sel_cell_end = actcell;
750 #endif
751         UpdateLocal(bv, SELECTION, false);
752         break;
753     case LFUN_RIGHT:
754         result = moveRight(bv);
755         sel_pos_start = sel_pos_end = cursor.pos();
756         sel_cell_start = sel_cell_end = actcell;
757         if (hs)
758             UpdateLocal(bv, SELECTION, false);
759         break;
760     case LFUN_LEFTSEL:
761         if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
762             break;
763         moveLeft(bv, false);
764 #if 0
765         if (cellstart(cursor.pos())) {
766             sel_pos_end = cursor.pos();
767             if (tabular->column_of_cell(sel_cell_start) >=
768                 tabular->column_of_cell(actcell))
769                 sel_cell_end = actcell;
770             else
771                 sel_cell_end = actcell-1;
772         }
773 #else
774         sel_pos_end = cursor.pos();
775         sel_cell_end = actcell;
776 #endif
777         UpdateLocal(bv, SELECTION, false);
778         break;
779     case LFUN_LEFT:
780         result = moveLeft(bv);
781         sel_pos_start = sel_pos_end = cursor.pos();
782         sel_cell_start = sel_cell_end = actcell;
783         if (hs)
784             UpdateLocal(bv, SELECTION, false);
785         break;
786     case LFUN_DOWNSEL:
787     {
788         int const ocell = actcell;
789         moveDown(bv);
790         sel_pos_end = cursor.pos();
791         if ((ocell == sel_cell_end) ||
792             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
793             sel_cell_end = tabular->GetCellBelow(sel_cell_end);
794         else
795             sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
796         UpdateLocal(bv, SELECTION, false);
797     }
798     break;
799     case LFUN_DOWN:
800         result = moveDown(bv);
801         sel_pos_start = sel_pos_end = cursor.pos();
802         sel_cell_start = sel_cell_end = actcell;
803         if (hs)
804             UpdateLocal(bv, SELECTION, false);
805         break;
806     case LFUN_UPSEL:
807     {
808         int const ocell = actcell;
809         moveUp(bv);
810         sel_pos_end = cursor.pos();
811         if ((ocell == sel_cell_end) ||
812             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
813             sel_cell_end = tabular->GetCellAbove(sel_cell_end);
814         else
815             sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
816         UpdateLocal(bv, SELECTION, false);
817     }
818     break;
819     case LFUN_UP:
820         result = moveUp(bv);
821         sel_pos_start = sel_pos_end = cursor.pos();
822         sel_cell_start = sel_cell_end = actcell;
823         if (hs)
824             UpdateLocal(bv, SELECTION, false);
825         break;
826     case LFUN_BACKSPACE:
827         break;
828     case LFUN_DELETE:
829         break;
830     case LFUN_HOME:
831         break;
832     case LFUN_END:
833         break;
834     case LFUN_SHIFT_TAB:
835     case LFUN_TAB:
836         if (the_locking_inset) {
837             UnlockInsetInInset(bv, the_locking_inset);
838             the_locking_inset = 0;
839         }
840         if (action == LFUN_TAB)
841             moveNextCell(bv);
842         else
843             movePrevCell(bv);
844         sel_pos_start = sel_pos_end = cursor.pos();
845         sel_cell_start = sel_cell_end = actcell;
846         if (hs)
847             UpdateLocal(bv, SELECTION, false);
848         break;
849     case LFUN_LAYOUT_TABULAR:
850     {
851         dialogs_ = bv->owner()->getDialogs();
852         dialogs_->showTabular(this);
853     }
854     break;
855     case LFUN_TABULAR_FEATURE:
856         if (!TabularFeatures(bv, arg))
857             result = UNDISPATCHED;
858         break;
859     case LFUN_CUT:
860         if (!copySelection())
861             break;
862         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
863 #ifndef NEW_INSETS
864           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
865           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
866 #else
867           bv->text->cursor.par()->previous,
868           bv->text->cursor.par()->next
869 #endif
870                 );
871         cutSelection();
872         UpdateLocal(bv, INIT, true);
873         break;
874     case LFUN_COPY:
875         if (!hasSelection())
876             break;
877         bv->text->FinishUndo();
878         copySelection();
879         break;
880     case LFUN_PASTE:
881         if (!hasPasteBuffer())
882             break;
883         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
884 #ifndef NEW_INSETS
885           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
886           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
887 #else
888           bv->text->cursor.par()->previous,
889           bv->text->cursor.par()->next
890 #endif
891                 );
892         pasteSelection(bv);
893         UpdateLocal(bv, INIT, true);
894         break;
895     default:
896         // we try to activate the actual inset and put this event down to
897         // the insets dispatch function.
898         result = UNDISPATCHED;
899         if (the_locking_inset)
900             break;
901         if (ActivateCellInset(bv)) {
902             result=the_locking_inset->LocalDispatch(bv, action, arg);
903             if (result == DISPATCHED_NOUPDATE)
904                 return result;
905             else if (result == DISPATCHED) {
906                 the_locking_inset->ToggleInsetCursor(bv);
907                 UpdateLocal(bv, CELL, false);
908                 the_locking_inset->ToggleInsetCursor(bv);
909                 return result;
910             } else {
911                 result = UNDISPATCHED;
912             }
913         }
914         break;
915     }
916     if (result!=FINISHED) {
917         if (!the_locking_inset) {
918             ShowInsetCursor(bv);
919         }
920     } else
921         bv->unlockInset(this);
922     return result;
923 }
924
925
926 int InsetTabular::Latex(Buffer const * buf, ostream & os,
927                         bool fragile, bool fp) const
928 {
929     return tabular->Latex(buf, os, fragile, fp);
930 }
931
932
933 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
934 {
935     // This should be changed to a real ascii export
936     return tabular->Latex(buf, os, false, false);
937 }
938
939
940 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
941 {
942     return 0;
943 }
944
945
946 int InsetTabular::DocBook(Buffer const *, ostream &) const
947 {
948     return 0;
949 }
950
951
952 void InsetTabular::Validate(LaTeXFeatures & features) const
953 {
954     tabular->Validate(features);
955 }
956
957
958 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
959                                                  LyXFont const & font,
960                                                  bool reinit) const
961 {
962     int cell = -1;
963     int maxAsc, maxDesc;
964     InsetText * inset;
965     bool changed = false;
966     
967     for(int i = 0; i < tabular->rows(); ++i) {
968         maxAsc = maxDesc = 0;
969         for(int j= 0; j < tabular->columns(); ++j) {
970             if (tabular->IsPartOfMultiColumn(i,j))
971                 continue;
972             ++cell;
973             inset = tabular->GetCellInset(cell);
974             if (!reinit)
975                 inset->update(bv, font, false);
976             maxAsc = max(maxAsc, inset->ascent(bv, font));
977             maxDesc = max(maxDesc, inset->descent(bv, font));
978             changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
979         }
980         changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
981         changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
982     }
983     return changed;
984 }
985
986
987 void InsetTabular::GetCursorPos(BufferView *,
988                                 int & x, int & y) const
989 {
990     x = cursor.x() - top_x;
991     y = cursor.y();
992 }
993
994
995 void InsetTabular::ToggleInsetCursor(BufferView * bv)
996 {
997     if (the_locking_inset) {
998         the_locking_inset->ToggleInsetCursor(bv);
999         return;
1000     }
1001
1002     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1003
1004     int const asc = lyxfont::maxAscent(font);
1005     int const desc = lyxfont::maxDescent(font);
1006   
1007     if (cursor_visible)
1008         bv->hideLockedInsetCursor();
1009     else
1010         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1011     cursor_visible = !cursor_visible;
1012 }
1013
1014
1015 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1016 {
1017     if (!cursor_visible) {
1018         LyXFont font; // = GetFont(par, cursor.pos);
1019     
1020         int const asc = lyxfont::maxAscent(font);
1021         int const desc = lyxfont::maxDescent(font);
1022         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1023         if (show)
1024             bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1025         cursor_visible = true;
1026     }
1027 }
1028
1029
1030 void InsetTabular::HideInsetCursor(BufferView * bv)
1031 {
1032     if (cursor_visible) {
1033         bv->hideLockedInsetCursor();
1034         cursor_visible = false;
1035     }
1036 //    if (cursor_visible)
1037 //        ToggleInsetCursor(bv);
1038 }
1039
1040
1041 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1042 {
1043     cursor.y(0);
1044     cursor.pos(0);
1045         
1046     actcell = actrow = actcol = 0;
1047     int ly = tabular->GetDescentOfRow(actrow);
1048
1049     // first search the right row
1050     while((ly < y) && (actrow < tabular->rows())) {
1051         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1052             tabular->GetAscentOfRow(actrow + 1) +
1053             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1054                                                                 actcol)));
1055         ++actrow;
1056         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1057     }
1058     actcell = tabular->GetCellNumber(actrow, actcol);
1059
1060     // now search the right column
1061     int lx = tabular->GetWidthOfColumn(actcell) -
1062         tabular->GetAdditionalWidth(actcell);
1063     for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
1064         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1065             tabular->GetAdditionalWidth(actcell - 1));
1066     cursor.pos(0);
1067     resetPos(bv);
1068     if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1069         cursor.x(lx + top_x - 2);
1070         cursor.pos(1);
1071     } else {
1072         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1073     }
1074     resetPos(bv);
1075 }
1076
1077
1078 int InsetTabular::getCellXPos(int cell) const
1079 {
1080     int c = cell;
1081
1082     for(; !tabular->IsFirstCellInRow(c); --c)
1083         ;
1084     int lx = tabular->GetWidthOfColumn(cell);
1085     for(; (c < cell); ++c) {
1086         lx += tabular->GetWidthOfColumn(c);
1087     }
1088     return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1089 }
1090
1091
1092 void InsetTabular::resetPos(BufferView * bv) const
1093 {
1094     if (!locked)
1095         return;
1096     actcol = tabular->column_of_cell(actcell);
1097
1098     int cell = 0;
1099     actrow = 0;
1100     cursor.y(0);
1101     for(; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1102         if (tabular->IsLastCellInRow(cell)) {
1103             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1104                 tabular->GetAscentOfRow(actrow + 1) +
1105                 tabular->GetAdditionalHeight(cell + 1));
1106             ++actrow;
1107         }
1108     }
1109     static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1110     cursor.x(getCellXPos(actcell) + offset);
1111     if (((cursor.x() - offset) > 20) &&
1112         ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1113          (bv->workWidth()-20)))
1114     {
1115         scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1116         UpdateLocal(bv, FULL, false);
1117     } else if ((cursor.x() - offset) < 20) {
1118         scroll(bv, 20 - cursor.x() + offset);
1119         UpdateLocal(bv, FULL, false);
1120     } else if (!cellstart(cursor.pos())) {
1121         LyXFont font(LyXFont::ALL_SANE);
1122         cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1123                 tabular->GetBeginningOfTextInCell(actcell));
1124     }
1125     if ((!the_locking_inset ||
1126          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1127         (actcell != oldcell)) {
1128         dialogs_ = bv->owner()->getDialogs();
1129         dialogs_->updateTabular(const_cast<InsetTabular *>(this));
1130         oldcell = actcell;
1131     }
1132 }
1133
1134
1135 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1136 {
1137     if (!cellstart(cursor.pos())) {
1138         if (tabular->IsLastCell(actcell))
1139             return FINISHED;
1140         ++actcell;
1141         cursor.pos(0);
1142     } else if (lock) {
1143         if (ActivateCellInset(bv))
1144             return DISPATCHED;
1145     } else {              // before the inset
1146         cursor.pos(1);
1147     }
1148     resetPos(bv);
1149     return DISPATCHED_NOUPDATE;
1150 }
1151
1152
1153 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1154 {
1155     if (cellstart(cursor.pos()) && !actcell)
1156         return FINISHED;
1157     if (cellstart(cursor.pos())) {
1158         --actcell;
1159         cursor.pos(1);
1160     } else if (lock) {       // behind the inset
1161         cursor.pos(0);
1162         if (ActivateCellInset(bv, 0, 0, 0, true))
1163             return DISPATCHED;
1164     } else {
1165         cursor.pos(0);
1166     }
1167     resetPos(bv);
1168     return DISPATCHED_NOUPDATE;
1169 }
1170
1171
1172 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1173 {
1174     int const ocell = actcell;
1175     actcell = tabular->GetCellAbove(actcell);
1176     if (actcell == ocell) // we moved out of the inset
1177         return FINISHED;
1178     resetPos(bv);
1179     return DISPATCHED_NOUPDATE;
1180 }
1181
1182
1183 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1184 {
1185     int const ocell = actcell;
1186     actcell = tabular->GetCellBelow(actcell);
1187     if (actcell == ocell) // we moved out of the inset
1188         return FINISHED;
1189     resetPos(bv);
1190     return DISPATCHED_NOUPDATE;
1191 }
1192
1193
1194 bool InsetTabular::moveNextCell(BufferView * bv)
1195 {
1196     if (tabular->IsLastCell(actcell))
1197         return false;
1198     ++actcell;
1199     resetPos(bv);
1200     return true;
1201 }
1202
1203
1204 bool InsetTabular::movePrevCell(BufferView * bv)
1205 {
1206     if (!actcell) // first cell
1207         return false;
1208     --actcell;
1209     resetPos(bv);
1210     return true;
1211 }
1212
1213
1214 bool InsetTabular::Delete()
1215 {
1216     return true;
1217 }
1218
1219
1220 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1221 {
1222     if (the_locking_inset)
1223         the_locking_inset->SetFont(bv, font, tall);
1224 }
1225
1226
1227 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1228 {
1229     LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1230
1231     int i = 0;
1232     for(; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1233         string const tmp = tabularFeatures[i].feature;
1234             
1235         if (tmp == what.substr(0, tmp.length())) {
1236         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1237               //tabularFeatures[i].feature.length())) {
1238             action = tabularFeatures[i].action;
1239             break;
1240         }
1241     }
1242     if (action == LyXTabular::LAST_ACTION)
1243         return false;
1244
1245     string const val =
1246             frontStrip(what.substr(tabularFeatures[i].feature.length()));
1247     TabularFeatures(bv, action, val);
1248     return true;
1249 }
1250
1251
1252 void InsetTabular::TabularFeatures(BufferView * bv,
1253                                    LyXTabular::Feature feature,
1254                                    string const & value)
1255 {
1256     int i;
1257     int j;
1258     int sel_col_start;
1259     int sel_col_end;
1260     int sel_row_start;
1261     int sel_row_end;
1262     int setLines = 0;
1263     LyXAlignment setAlign = LYX_ALIGN_LEFT;
1264     LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1265     int lineSet;
1266     bool what;
1267
1268     switch (feature) {
1269       case LyXTabular::M_ALIGN_LEFT:
1270       case LyXTabular::ALIGN_LEFT:
1271           setAlign=LYX_ALIGN_LEFT;
1272           break;
1273       case LyXTabular::M_ALIGN_RIGHT:
1274       case LyXTabular::ALIGN_RIGHT:
1275           setAlign=LYX_ALIGN_RIGHT;
1276           break;
1277       case LyXTabular::M_ALIGN_CENTER:
1278       case LyXTabular::ALIGN_CENTER:
1279           setAlign=LYX_ALIGN_CENTER;
1280           break;
1281       case LyXTabular::M_VALIGN_TOP:
1282       case LyXTabular::VALIGN_TOP:
1283           setVAlign=LyXTabular::LYX_VALIGN_TOP;
1284           break;
1285       case LyXTabular::M_VALIGN_BOTTOM:
1286       case LyXTabular::VALIGN_BOTTOM:
1287           setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1288           break;
1289       case LyXTabular::M_VALIGN_CENTER:
1290       case LyXTabular::VALIGN_CENTER:
1291           setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1292           break;
1293       default:
1294           break;
1295     }
1296     if (hasSelection()) {
1297         sel_col_start = tabular->column_of_cell(sel_cell_start);
1298         sel_col_end = tabular->column_of_cell(sel_cell_end);
1299         if (sel_col_start > sel_col_end) {
1300             sel_col_end = sel_col_start;
1301             sel_col_start = tabular->column_of_cell(sel_cell_end);
1302         } else {
1303             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1304         }
1305         
1306         sel_row_start = tabular->row_of_cell(sel_cell_start);
1307         sel_row_end = tabular->row_of_cell(sel_cell_end);
1308         if (sel_row_start > sel_row_end) {
1309                 //int tmp = sel_row_start;
1310                 //sel_row_start = sel_row_end;
1311                 //sel_row_end = tmp;
1312             swap(sel_row_start, sel_row_end);
1313         }
1314     } else {
1315         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1316         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1317     }
1318     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1319 #ifndef NEW_INSETS
1320               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1321               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1322 #else
1323               bv->text->cursor.par()->previous,
1324               bv->text->cursor.par()->next
1325 #endif
1326             );
1327
1328     int row = tabular->row_of_cell(actcell);
1329     int column = tabular->column_of_cell(actcell);
1330     bool flag = true;
1331     
1332     switch (feature) {
1333     case LyXTabular::SET_PWIDTH:
1334     {
1335         bool const update = (tabular->GetColumnPWidth(actcell) != value);
1336         tabular->SetColumnPWidth(actcell,value);
1337         if (update) {
1338             for (int i=0; i < tabular->rows(); ++i) {
1339                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1340                     resizeLyXText(bv);
1341             }
1342             UpdateLocal(bv, INIT, true);
1343         }
1344     }
1345     break;
1346     case LyXTabular::SET_MPWIDTH:
1347     {
1348         bool const update = (tabular->GetPWidth(actcell) != value);
1349         tabular->SetMColumnPWidth(actcell,value);
1350         if (update) {
1351             for (int i=0; i < tabular->rows(); ++i) {
1352                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1353                     resizeLyXText(bv);
1354             }
1355             UpdateLocal(bv, INIT, true);
1356         }
1357     }
1358     break;
1359     case LyXTabular::SET_SPECIAL_COLUMN:
1360     case LyXTabular::SET_SPECIAL_MULTI:
1361         tabular->SetAlignSpecial(actcell,value,feature);
1362         break;
1363     case LyXTabular::APPEND_ROW:
1364         // append the row into the tabular
1365         UnlockInsetInInset(bv, the_locking_inset);
1366         tabular->AppendRow(actcell);
1367         UpdateLocal(bv, INIT, true);
1368         break;
1369     case LyXTabular::APPEND_COLUMN:
1370         // append the column into the tabular
1371         tabular->AppendColumn(actcell);
1372         actcell = tabular->GetCellNumber(row, column);
1373         UpdateLocal(bv, INIT, true);
1374         break;
1375     case LyXTabular::DELETE_ROW:
1376         tabular->DeleteRow(tabular->row_of_cell(actcell));
1377         if ((row+1) > tabular->rows())
1378             --row;
1379         actcell = tabular->GetCellNumber(row, column);
1380         UpdateLocal(bv, INIT, true);
1381         break;
1382     case LyXTabular::DELETE_COLUMN:
1383         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1384         if ((column+1) > tabular->columns())
1385             --column;
1386         actcell = tabular->GetCellNumber(row, column);
1387         UpdateLocal(bv, INIT, true);
1388         break;
1389     case LyXTabular::M_TOGGLE_LINE_TOP:
1390         flag = false;
1391     case LyXTabular::TOGGLE_LINE_TOP:
1392         lineSet = !tabular->TopLine(actcell, flag);
1393         for(i=sel_row_start; i<=sel_row_end; ++i)
1394             for(j=sel_col_start; j<=sel_col_end; ++j)
1395                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1396         UpdateLocal(bv, INIT, true);
1397         break;
1398     
1399     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1400         flag = false;
1401     case LyXTabular::TOGGLE_LINE_BOTTOM:
1402         lineSet = !tabular->BottomLine(actcell, flag); 
1403         for(i=sel_row_start; i<=sel_row_end; ++i)
1404             for(j=sel_col_start; j<=sel_col_end; ++j)
1405                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1406                                        flag);
1407         UpdateLocal(bv, INIT, true);
1408         break;
1409                 
1410     case LyXTabular::M_TOGGLE_LINE_LEFT:
1411         flag = false;
1412     case LyXTabular::TOGGLE_LINE_LEFT:
1413         lineSet = !tabular->LeftLine(actcell, flag);
1414         for(i=sel_row_start; i<=sel_row_end; ++i)
1415             for(j=sel_col_start; j<=sel_col_end; ++j)
1416                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1417                                      flag);
1418         UpdateLocal(bv, INIT, true);
1419         break;
1420
1421     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1422         flag = false;
1423     case LyXTabular::TOGGLE_LINE_RIGHT:
1424         lineSet = !tabular->RightLine(actcell, flag);
1425         for(i=sel_row_start; i<=sel_row_end; ++i)
1426             for(j=sel_col_start; j<=sel_col_end; ++j)
1427                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1428                                       flag);
1429         UpdateLocal(bv, INIT, true);
1430         break;
1431     case LyXTabular::M_ALIGN_LEFT:
1432     case LyXTabular::M_ALIGN_RIGHT:
1433     case LyXTabular::M_ALIGN_CENTER:
1434         flag = false;
1435     case LyXTabular::ALIGN_LEFT:
1436     case LyXTabular::ALIGN_RIGHT:
1437     case LyXTabular::ALIGN_CENTER:
1438         for(i = sel_row_start; i <= sel_row_end; ++i)
1439             for(j = sel_col_start; j <= sel_col_end; ++j)
1440                 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1441                                       flag);
1442         if (hasSelection())
1443             UpdateLocal(bv, INIT, true);
1444         else
1445             UpdateLocal(bv, CELL, true);
1446         break;
1447     case LyXTabular::M_VALIGN_TOP:
1448     case LyXTabular::M_VALIGN_BOTTOM:
1449     case LyXTabular::M_VALIGN_CENTER:
1450         flag = false;
1451     case LyXTabular::VALIGN_TOP:
1452     case LyXTabular::VALIGN_BOTTOM:
1453     case LyXTabular::VALIGN_CENTER:
1454         for(i = sel_row_start; i <= sel_row_end; ++i)
1455             for(j = sel_col_start; j <= sel_col_end; ++j)
1456                 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1457                                        setVAlign, flag);
1458         if (hasSelection())
1459             UpdateLocal(bv, INIT, true);
1460         else
1461             UpdateLocal(bv, CELL, true);
1462         break;
1463     case LyXTabular::MULTICOLUMN:
1464     {
1465         if (sel_row_start != sel_row_end) {
1466             WriteAlert(_("Impossible Operation!"), 
1467                        _("Multicolumns can only be horizontally."), 
1468                        _("Sorry."));
1469             return;
1470         }
1471         // just multicol for one Single Cell
1472         if (!hasSelection()) {
1473             // check wether we are completly in a multicol
1474             if (tabular->IsMultiColumn(actcell)) {
1475                 tabular->UnsetMultiColumn(actcell);
1476                 UpdateLocal(bv, INIT, true);
1477             } else {
1478                 tabular->SetMultiColumn(actcell, 1);
1479                 UpdateLocal(bv, CELL, true);
1480             }
1481             return;
1482         }
1483         // we have a selection so this means we just add all this
1484         // cells to form a multicolumn cell
1485         int s_start;
1486         int s_end;
1487
1488         if (sel_cell_start > sel_cell_end) {
1489             s_start = sel_cell_end;
1490             s_end = sel_cell_start;
1491         } else {
1492             s_start = sel_cell_start;
1493             s_end = sel_cell_end;
1494         }
1495         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1496         actcell = s_start;
1497         cursor.pos(0);
1498         sel_cell_end = sel_cell_start;
1499         sel_pos_end = sel_pos_start;
1500         UpdateLocal(bv, INIT, true);
1501         break;
1502     }
1503     case LyXTabular::SET_ALL_LINES:
1504         setLines = 1;
1505     case LyXTabular::UNSET_ALL_LINES:
1506         for(i=sel_row_start; i<=sel_row_end; ++i)
1507             for(j=sel_col_start; j<=sel_col_end; ++j)
1508                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1509         UpdateLocal(bv, INIT, true);
1510         break;
1511     case LyXTabular::SET_LONGTABULAR:
1512         tabular->SetLongTabular(true);
1513         UpdateLocal(bv, INIT, true); // because this toggles displayed
1514         break;
1515     case LyXTabular::UNSET_LONGTABULAR:
1516         tabular->SetLongTabular(false);
1517         UpdateLocal(bv, INIT, true); // because this toggles displayed
1518         break;
1519     case LyXTabular::SET_ROTATE_TABULAR:
1520         tabular->SetRotateTabular(true);
1521         break;
1522     case LyXTabular::UNSET_ROTATE_TABULAR:
1523         tabular->SetRotateTabular(false);
1524         break;
1525     case LyXTabular::SET_ROTATE_CELL:
1526         for(i=sel_row_start; i<=sel_row_end; ++i)
1527             for(j=sel_col_start; j<=sel_col_end; ++j)
1528                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1529         break;
1530     case LyXTabular::UNSET_ROTATE_CELL:
1531         for(i = sel_row_start; i <= sel_row_end; ++i)
1532             for(j = sel_col_start; j <= sel_col_end; ++j)
1533                 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1534         break;
1535     case LyXTabular::SET_USEBOX:
1536     {
1537         LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1538         if (val == tabular->GetUsebox(actcell))
1539             val = LyXTabular::BOX_NONE;
1540         for(i = sel_row_start; i <= sel_row_end; ++i)
1541             for(j = sel_col_start; j <= sel_col_end; ++j)
1542                 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1543         break;
1544     }
1545     case LyXTabular::SET_LTFIRSTHEAD:
1546         tabular->SetLTHead(actcell, true);
1547         break;
1548     case LyXTabular::SET_LTHEAD:
1549         tabular->SetLTHead(actcell, false);
1550         break;
1551     case LyXTabular::SET_LTFOOT:
1552         tabular->SetLTFoot(actcell, false);
1553         break;
1554     case LyXTabular::SET_LTLASTFOOT:
1555         tabular->SetLTFoot(actcell, true);
1556         break;
1557     case LyXTabular::SET_LTNEWPAGE:
1558         what = !tabular->GetLTNewPage(actcell);
1559         tabular->SetLTNewPage(actcell, what);
1560         break;
1561     // dummy stuff just to avoid warnings
1562     case LyXTabular::LAST_ACTION:
1563         break;
1564     }
1565 }
1566
1567
1568 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1569                                      bool behind)
1570 {
1571     // the cursor.pos has to be before the inset so if it isn't now just
1572     // reset the curor pos first!
1573     if (!cellstart(cursor.pos())) {
1574         cursor.pos(0);
1575         resetPos(bv);
1576     }
1577     UpdatableInset * inset =
1578         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1579     LyXFont font(LyXFont::ALL_SANE);
1580     if (behind) {
1581         x = inset->x() + inset->width(bv, font);
1582         y = inset->descent(bv, font);
1583     }
1584     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1585     inset_y = cursor.y();
1586     inset->Edit(bv, x, y - inset_y, button);
1587     if (!the_locking_inset)
1588         return false;
1589     UpdateLocal(bv, CELL, false);
1590     return (the_locking_inset != 0);
1591 }
1592
1593
1594 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1595 {
1596     InsetText * inset = tabular->GetCellInset(actcell);
1597     int x1 = x + top_x;
1598
1599     if (!cellstart(cursor.pos())) {
1600         return (((x + top_x) < cursor.x()) &&
1601                 ((x + top_x) > (cursor.x() - inset->width(bv,
1602                                                       LyXFont(LyXFont::ALL_SANE)))));
1603     } else {
1604         int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1605         return ((x1 > x2) &&
1606                 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1607     }
1608 }
1609
1610
1611 // This returns paperWidth() if the cell-width is unlimited or the width
1612 // in pixels if we have a pwidth for this cell.
1613 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1614 {
1615     string const s = tabular->GetPWidth(cell);
1616
1617     if (s.empty())
1618         return -1;
1619     return VSpace(s).inPixels(0, 0);
1620 }
1621
1622
1623 int InsetTabular::getMaxWidth(Painter & pain,
1624                               UpdatableInset const * inset) const
1625 {
1626     int const n = tabular->GetNumberOfCells();
1627     int cell = 0;
1628     for(; cell < n; ++cell) {
1629         if (tabular->GetCellInset(cell) == inset)
1630             break;
1631     }
1632     if (cell >= n)
1633         return -1;
1634     int w = GetMaxWidthOfCell(pain, cell);
1635     if (w > 0)
1636         // because the inset then subtracts it's top_x and owner->x()
1637         w += (inset->x() - top_x);
1638     return w;
1639 }
1640
1641
1642 void InsetTabular::resizeLyXText(BufferView *) const
1643 {
1644     need_update = FULL;
1645 }
1646
1647
1648 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1649 {
1650     if (the_locking_inset)
1651         return the_locking_inset->getLyXText(bv);
1652     return Inset::getLyXText(bv);
1653 }
1654
1655
1656 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1657 {
1658     if (the_locking_inset) {
1659         InsetTabular * i = static_cast<InsetTabular *>
1660             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1661         if (i) {
1662             i->OpenLayoutDialog(bv);
1663             return;
1664         }
1665     }
1666     dialogs_ = bv->owner()->getDialogs();
1667     dialogs_->showTabular(const_cast<InsetTabular *>(this));
1668 }
1669
1670 //
1671 // functions returns:
1672 // 0 ... disabled
1673 // 1 ... enabled
1674 // 2 ... toggled on
1675 // 3 ... toggled off
1676 //
1677 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1678 {
1679     int action = LyXTabular::LAST_ACTION;
1680     LyXFunc::func_status status = LyXFunc::OK;
1681     
1682     int i = 0;
1683     for(; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1684         string const tmp = tabularFeatures[i].feature;
1685         if (tmp == what.substr(0, tmp.length())) {                  
1686         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1687         //   tabularFeatures[i].feature.length())) {
1688             action = tabularFeatures[i].action;
1689             break;
1690         }
1691     }
1692     if (action == LyXTabular::LAST_ACTION)
1693         return LyXFunc::Unknown;
1694
1695     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1696
1697     int sel_row_start, sel_row_end;
1698     int dummy;
1699     bool flag = true;
1700
1701     if (hasSelection()) {
1702         sel_row_start = tabular->row_of_cell(sel_cell_start);
1703         sel_row_end = tabular->row_of_cell(sel_cell_end);
1704         if (sel_row_start > sel_row_end) {
1705                 //int tmp = sel_row_start;
1706                 //sel_row_start = sel_row_end;
1707                 //sel_row_end = tmp;
1708             swap(sel_row_start, sel_row_end);
1709         }
1710     } else {
1711         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1712     }
1713
1714     switch (action) {
1715     case LyXTabular::SET_PWIDTH:
1716     case LyXTabular::SET_MPWIDTH:
1717     case LyXTabular::SET_SPECIAL_COLUMN:
1718     case LyXTabular::SET_SPECIAL_MULTI:
1719         status |= LyXFunc::Disabled;
1720         return status;
1721
1722     case LyXTabular::APPEND_ROW:
1723     case LyXTabular::APPEND_COLUMN:
1724     case LyXTabular::DELETE_ROW:
1725     case LyXTabular::DELETE_COLUMN:
1726     case LyXTabular::SET_ALL_LINES:
1727     case LyXTabular::UNSET_ALL_LINES:
1728         status |= LyXFunc::OK;
1729         return status;
1730
1731     case LyXTabular::MULTICOLUMN:
1732         if (tabular->IsMultiColumn(actcell))
1733             status |= LyXFunc::ToggleOn;
1734         else
1735             status |= LyXFunc::ToggleOff;
1736         break;
1737     case LyXTabular::M_TOGGLE_LINE_TOP:
1738         flag = false;
1739     case LyXTabular::TOGGLE_LINE_TOP:
1740         if (tabular->TopLine(actcell, flag))
1741             status |= LyXFunc::ToggleOn;
1742         else
1743             status |= LyXFunc::ToggleOff;
1744         break;
1745     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1746         flag = false;
1747     case LyXTabular::TOGGLE_LINE_BOTTOM:
1748         if (tabular->BottomLine(actcell, flag))
1749             status |= LyXFunc::ToggleOn;
1750         else
1751             status |= LyXFunc::ToggleOff;
1752         break;
1753     case LyXTabular::M_TOGGLE_LINE_LEFT:
1754         flag = false;
1755     case LyXTabular::TOGGLE_LINE_LEFT:
1756         if (tabular->LeftLine(actcell, flag))
1757             status |= LyXFunc::ToggleOn;
1758         else
1759             status |= LyXFunc::ToggleOff;
1760         break;
1761     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1762         flag = false;
1763     case LyXTabular::TOGGLE_LINE_RIGHT:
1764         if (tabular->RightLine(actcell, flag))
1765             status |= LyXFunc::ToggleOn;
1766         else
1767             status |= LyXFunc::ToggleOff;
1768         break;
1769     case LyXTabular::M_ALIGN_LEFT:
1770         flag = false;
1771     case LyXTabular::ALIGN_LEFT:
1772         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1773             status |= LyXFunc::ToggleOn;
1774         else
1775             status |= LyXFunc::ToggleOff;
1776         break;
1777     case LyXTabular::M_ALIGN_RIGHT:
1778         flag = false;
1779     case LyXTabular::ALIGN_RIGHT:
1780         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1781             status |= LyXFunc::ToggleOn;
1782         else
1783             status |= LyXFunc::ToggleOff;
1784         break;
1785     case LyXTabular::M_ALIGN_CENTER:
1786         flag = false;
1787     case LyXTabular::ALIGN_CENTER:
1788         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1789             status |= LyXFunc::ToggleOn;
1790         else
1791             status |= LyXFunc::ToggleOff;
1792         break;
1793     case LyXTabular::M_VALIGN_TOP:
1794         flag = false;
1795     case LyXTabular::VALIGN_TOP:
1796         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1797             status |= LyXFunc::ToggleOn;
1798         else
1799             status |= LyXFunc::ToggleOff;
1800         break;
1801     case LyXTabular::M_VALIGN_BOTTOM:
1802         flag = false;
1803     case LyXTabular::VALIGN_BOTTOM:
1804         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1805             status |= LyXFunc::ToggleOn;
1806         else
1807             status |= LyXFunc::ToggleOff;
1808         break;
1809     case LyXTabular::M_VALIGN_CENTER:
1810         flag = false;
1811     case LyXTabular::VALIGN_CENTER:
1812         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1813             status |= LyXFunc::ToggleOn;
1814         else
1815             status |= LyXFunc::ToggleOff;
1816         break;
1817     case LyXTabular::SET_LONGTABULAR:
1818         if (tabular->IsLongTabular())
1819             status |= LyXFunc::ToggleOn;
1820         else
1821             status |= LyXFunc::ToggleOff;
1822         break;
1823     case LyXTabular::UNSET_LONGTABULAR:
1824         if (!tabular->IsLongTabular())
1825             status |= LyXFunc::ToggleOn;
1826         else
1827             status |= LyXFunc::ToggleOff;
1828         break;
1829     case LyXTabular::SET_ROTATE_TABULAR:
1830         if (tabular->GetRotateTabular())
1831             status |= LyXFunc::ToggleOn;
1832         else
1833             status |= LyXFunc::ToggleOff;
1834         break;
1835     case LyXTabular::UNSET_ROTATE_TABULAR:
1836         if (!tabular->GetRotateTabular())
1837             status |= LyXFunc::ToggleOn;
1838         else
1839             status |= LyXFunc::ToggleOff;
1840         break;
1841     case LyXTabular::SET_ROTATE_CELL:
1842         if (tabular->GetRotateCell(actcell))
1843             status |= LyXFunc::ToggleOn;
1844         else
1845             status |= LyXFunc::ToggleOff;
1846         break;
1847     case LyXTabular::UNSET_ROTATE_CELL:
1848         if (!tabular->GetRotateCell(actcell))
1849             status |= LyXFunc::ToggleOn;
1850         else
1851             status |= LyXFunc::ToggleOff;
1852         break;
1853     case LyXTabular::SET_USEBOX:
1854         if (strToInt(argument) == tabular->GetUsebox(actcell))
1855             status |= LyXFunc::ToggleOn;
1856         else
1857             status |= LyXFunc::ToggleOff;
1858         break;
1859     case LyXTabular::SET_LTFIRSTHEAD:
1860         if (tabular->GetRowOfLTHead(actcell, dummy))
1861             status |= LyXFunc::ToggleOn;
1862         else
1863             status |= LyXFunc::ToggleOff;
1864         break;
1865     case LyXTabular::SET_LTHEAD:
1866         if (tabular->GetRowOfLTHead(actcell, dummy))
1867             status |= LyXFunc::ToggleOn;
1868         else
1869             status |= LyXFunc::ToggleOff;
1870         break;
1871     case LyXTabular::SET_LTFOOT:
1872         if (tabular->GetRowOfLTFoot(actcell, dummy))
1873             status |= LyXFunc::ToggleOn;
1874         else
1875             status |= LyXFunc::ToggleOff;
1876         break;
1877     case LyXTabular::SET_LTLASTFOOT:
1878         if (tabular->GetRowOfLTFoot(actcell, dummy))
1879             status |= LyXFunc::ToggleOn;
1880         else
1881             status |= LyXFunc::ToggleOff;
1882         break;
1883     case LyXTabular::SET_LTNEWPAGE:
1884         if (tabular->GetLTNewPage(actcell))
1885             status |= LyXFunc::ToggleOn;
1886         else
1887             status |= LyXFunc::ToggleOff;
1888         break;
1889     default:
1890         status = LyXFunc::Disabled;
1891         break;
1892     }
1893     return status;
1894 }
1895
1896
1897 bool InsetTabular::copySelection()
1898 {
1899     if (!hasSelection())
1900         return false;
1901     delete paste_tabular;
1902
1903     int sel_col_start, sel_col_end;
1904     int sel_row_start, sel_row_end;
1905
1906     sel_col_start = tabular->column_of_cell(sel_cell_start);
1907     sel_col_end = tabular->column_of_cell(sel_cell_end);
1908     if (sel_col_start > sel_col_end) {
1909         sel_col_start = sel_col_end;
1910         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1911     } else {
1912         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1913     }
1914     int columns = sel_col_end - sel_col_start + 1;
1915
1916     sel_row_start = tabular->row_of_cell(sel_cell_start);
1917     sel_row_end = tabular->row_of_cell(sel_cell_end);
1918     if (sel_row_start > sel_row_end) {
1919             //int tmp tmp = sel_row_start;
1920             //sel_row_start = sel_row_end;
1921             //sel_row_end = tmp;
1922         swap(sel_row_start, sel_row_end);
1923     }
1924     int rows = sel_row_end - sel_row_start + 1;
1925
1926     paste_tabular = new LyXTabular(this, rows, columns);
1927     
1928     if (sel_cell_start > sel_cell_end) {
1929             //int tmp = sel_cell_start;
1930             //sel_cell_start = sel_cell_end;
1931             //sel_cell_end = tmp;
1932         swap(sel_cell_start, sel_cell_end);
1933     }
1934     for(int i = sel_cell_start, j = 0; i <= sel_cell_end; ++i, ++j) {
1935         while(paste_tabular->row_of_cell(j) <
1936               (tabular->row_of_cell(i)-sel_row_start)) {
1937             ++j;
1938         }
1939         while(paste_tabular->row_of_cell(j) >
1940               (tabular->row_of_cell(i)-sel_row_start)) {
1941             ++i;
1942         }
1943         *(paste_tabular->GetCellInset(j)) = *(tabular->GetCellInset(i));
1944     }
1945     return true;
1946 }
1947
1948
1949 bool InsetTabular::pasteSelection(BufferView * bv)
1950 {
1951     if (!paste_tabular)
1952         return false;
1953     for(int j=0, i=actcell; j<paste_tabular->GetNumberOfCells(); ++j,++i) {
1954         while (paste_tabular->row_of_cell(j) > tabular->row_of_cell(i)-actrow)
1955             ++i;
1956         if (tabular->GetNumberOfCells() <= i)
1957             break;
1958         while (paste_tabular->row_of_cell(j) < tabular->row_of_cell(i)-actrow)
1959             ++j;
1960         if (paste_tabular->GetNumberOfCells() <= j)
1961             break;
1962         *(tabular->GetCellInset(i)) = *(paste_tabular->GetCellInset(j));
1963         tabular->GetCellInset(i)->setOwner(this);
1964         tabular->GetCellInset(i)->deleteLyXText(bv);
1965     }
1966     return true;
1967 }
1968
1969
1970 bool InsetTabular::cutSelection()
1971 {
1972     if (!hasSelection())
1973         return false;
1974
1975     int sel_col_start, sel_col_end;
1976     int sel_row_start, sel_row_end;
1977
1978     sel_col_start = tabular->column_of_cell(sel_cell_start);
1979     sel_col_end = tabular->column_of_cell(sel_cell_end);
1980     if (sel_col_start > sel_col_end) {
1981         sel_col_start = sel_col_end;
1982         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1983     } else {
1984         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1985     }
1986     sel_row_start = tabular->row_of_cell(sel_cell_start);
1987     sel_row_end = tabular->row_of_cell(sel_cell_end);
1988     if (sel_row_start > sel_row_end) {
1989             //int tmp = sel_row_start;
1990             //sel_row_start = sel_row_end;
1991             //sel_row_end = tmp;
1992         swap(sel_row_start, sel_row_end);
1993     }
1994     if (sel_cell_start > sel_cell_end) {
1995             //int tmp = sel_cell_start;
1996             //sel_cell_start = sel_cell_end;
1997             //sel_cell_end = tmp;
1998         swap(sel_cell_start, sel_cell_end);
1999     }
2000     for(int i = sel_row_start; i <= sel_row_end; ++i) {
2001         for(int j = sel_col_start; j <= sel_col_end; ++j) {
2002             tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2003         }
2004     }
2005     return true;
2006 }
2007