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