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