]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
get rid of dead code, some new functions constify variables.
[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) 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 *, int & x, int & y) const
954 {
955     x = cursor.x() - top_x;
956     y = cursor.y();
957 }
958
959
960 void InsetTabular::ToggleInsetCursor(BufferView * bv)
961 {
962     if (the_locking_inset) {
963         the_locking_inset->ToggleInsetCursor(bv);
964         return;
965     }
966
967     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
968
969     int const asc = lyxfont::maxAscent(font);
970     int const desc = lyxfont::maxDescent(font);
971   
972     if (cursor_visible)
973         bv->hideLockedInsetCursor();
974     else
975         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
976     cursor_visible = !cursor_visible;
977 }
978
979
980 void InsetTabular::ShowInsetCursor(BufferView * bv)
981 {
982     if (!cursor_visible) {
983         LyXFont font; // = GetFont(par, cursor.pos);
984     
985         int const asc = lyxfont::maxAscent(font);
986         int const desc = lyxfont::maxDescent(font);
987         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
988         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
989         cursor_visible = true;
990     }
991 }
992
993
994 void InsetTabular::HideInsetCursor(BufferView * bv)
995 {
996     if (cursor_visible) {
997         bv->hideLockedInsetCursor();
998         cursor_visible = false;
999     }
1000 //    if (cursor_visible)
1001 //        ToggleInsetCursor(bv);
1002 }
1003
1004
1005 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1006 {
1007     cursor.y(0);
1008     cursor.pos(0);
1009         
1010     actcell = actrow = actcol = 0;
1011     int ly = tabular->GetDescentOfRow(actrow);
1012
1013     // first search the right row
1014     while((ly < y) && (actrow < tabular->rows())) {
1015         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1016             tabular->GetAscentOfRow(actrow+1) +
1017             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1018                                                                 actcol)));
1019         ++actrow;
1020         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1021     }
1022     actcell = tabular->GetCellNumber(actrow, actcol);
1023
1024     // now search the right column
1025     int lx = tabular->GetWidthOfColumn(actcell) -
1026         tabular->GetAdditionalWidth(actcell);
1027     for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
1028         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1029             tabular->GetAdditionalWidth(actcell - 1));
1030     cursor.pos(0);
1031     resetPos(bv);
1032     if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1033         cursor.x(lx + top_x - 2);
1034         cursor.pos(1);
1035     } else {
1036         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1037     }
1038     resetPos(bv);
1039 }
1040
1041
1042 int InsetTabular::getCellXPos(int cell) const
1043 {
1044     int c = cell;
1045
1046     for(; !tabular->IsFirstCellInRow(c); --c)
1047         ;
1048     int lx = tabular->GetWidthOfColumn(cell);
1049     for(; (c < cell); ++c) {
1050         lx += tabular->GetWidthOfColumn(c);
1051     }
1052     return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1053 }
1054
1055
1056 void InsetTabular::resetPos(BufferView * bv) const
1057 {
1058     if (!locked)
1059         return;
1060     actcol = tabular->column_of_cell(actcell);
1061
1062     int cell = 0;
1063     actrow = 0;
1064     cursor.y(0);
1065     for(; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1066         if (tabular->IsLastCellInRow(cell)) {
1067             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1068                 tabular->GetAscentOfRow(actrow + 1) +
1069                 tabular->GetAdditionalHeight(cell + 1));
1070             ++actrow;
1071         }
1072     }
1073     static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1074     cursor.x(getCellXPos(actcell) + offset);
1075     if (((cursor.x() - offset) > 20) &&
1076         ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1077          (bv->workWidth()-20)))
1078     {
1079         scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1080         UpdateLocal(bv, FULL, false);
1081     } else if ((cursor.x() - offset) < 20) {
1082         scroll(bv, 20 - cursor.x() + offset);
1083         UpdateLocal(bv, FULL, false);
1084     } else if (!cellstart(cursor.pos())) {
1085         LyXFont font(LyXFont::ALL_SANE);
1086         cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1087                 tabular->GetBeginningOfTextInCell(actcell));
1088     }
1089     if ((!the_locking_inset ||
1090          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1091         (actcell != oldcell)) {
1092         dialogs_ = bv->owner()->getDialogs();
1093         dialogs_->updateTabular(const_cast<InsetTabular *>(this));
1094         oldcell = actcell;
1095     }
1096 }
1097
1098
1099 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1100 {
1101     if (!cellstart(cursor.pos())) {
1102         if (tabular->IsLastCell(actcell))
1103             return FINISHED;
1104         ++actcell;
1105         cursor.pos((cursor.pos() + 1) % 2);
1106     } else if (lock) {
1107         if (ActivateCellInset(bv))
1108             return DISPATCHED;
1109     } else {              // before the inset
1110         cursor.pos((cursor.pos() + 1) % 2);
1111     }
1112     resetPos(bv);
1113     return DISPATCHED_NOUPDATE;
1114 }
1115
1116
1117 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1118 {
1119     if (!cursor.pos()) {
1120         if (!actcell)
1121             return FINISHED;
1122         cursor.pos(0);
1123     }
1124     cursor.pos((cursor.pos() - 1) % 2);
1125     if (!cellstart(cursor.pos())) {
1126         --actcell;
1127     } else if (lock) {       // behind the inset
1128         if (ActivateCellInset(bv, 0, 0, 0, true))
1129             return DISPATCHED;
1130     }
1131     resetPos(bv);
1132     return DISPATCHED_NOUPDATE;
1133 }
1134
1135
1136 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1137 {
1138     int const ocell = actcell;
1139     actcell = tabular->GetCellAbove(actcell);
1140     if (actcell == ocell) // we moved out of the inset
1141         return FINISHED;
1142     resetPos(bv);
1143     return DISPATCHED_NOUPDATE;
1144 }
1145
1146
1147 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1148 {
1149     int const ocell = actcell;
1150     actcell = tabular->GetCellBelow(actcell);
1151     if (actcell == ocell) // we moved out of the inset
1152         return FINISHED;
1153     resetPos(bv);
1154     return DISPATCHED_NOUPDATE;
1155 }
1156
1157
1158 bool InsetTabular::moveNextCell(BufferView * bv)
1159 {
1160     if (tabular->IsLastCell(actcell))
1161         return false;
1162     ++actcell;
1163     cursor.pos((cursor.pos() + 1) % 2);
1164     if (!cellstart(cursor.pos()))
1165         cursor.pos((cursor.pos() + 1) % 2);
1166     resetPos(bv);
1167     return true;
1168 }
1169
1170
1171 bool InsetTabular::movePrevCell(BufferView * bv)
1172 {
1173     if (!actcell) // first cell
1174         return false;
1175     --actcell;
1176     cursor.pos((cursor.pos() - 1) % 2);
1177     if (cellstart(cursor.pos()))
1178         cursor.pos((cursor.pos() - 1) % 2);
1179     resetPos(bv);
1180     return true;
1181 }
1182
1183
1184 bool InsetTabular::Delete()
1185 {
1186     return true;
1187 }
1188
1189
1190 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1191 {
1192     if (the_locking_inset)
1193         the_locking_inset->SetFont(bv, font, tall);
1194 }
1195
1196
1197 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1198 {
1199     int action = LyXTabular::LAST_ACTION;
1200
1201     int i = 0;
1202     for(; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1203         string const tmp = tabularFeatures[i].feature;
1204             
1205         if (tmp == what.substr(0, tmp.length())) {
1206         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1207               //tabularFeatures[i].feature.length())) {
1208             action = tabularFeatures[i].action;
1209             break;
1210         }
1211     }
1212     if (action == LyXTabular::LAST_ACTION)
1213         return false;
1214
1215     string const val =
1216             frontStrip(what.substr(tabularFeatures[i].feature.length()));
1217     TabularFeatures(bv, action, val);
1218     return true;
1219 }
1220
1221
1222 void InsetTabular::TabularFeatures(BufferView * bv, int feature,
1223                                    string const & value)
1224 {
1225     int
1226         i, j,
1227         sel_col_start,
1228         sel_col_end,
1229         sel_row_start,
1230         sel_row_end,
1231         setLines = 0,
1232         setAlign = LYX_ALIGN_LEFT,
1233         lineSet;
1234     bool what;
1235
1236     switch (feature) {
1237       case LyXTabular::M_ALIGN_LEFT:
1238       case LyXTabular::ALIGN_LEFT:
1239           setAlign=LYX_ALIGN_LEFT;
1240           break;
1241       case LyXTabular::M_ALIGN_RIGHT:
1242       case LyXTabular::ALIGN_RIGHT:
1243           setAlign=LYX_ALIGN_RIGHT;
1244           break;
1245       case LyXTabular::M_ALIGN_CENTER:
1246       case LyXTabular::ALIGN_CENTER:
1247           setAlign=LYX_ALIGN_CENTER;
1248           break;
1249       case LyXTabular::M_VALIGN_TOP:
1250       case LyXTabular::VALIGN_TOP:
1251           setAlign=LyXTabular::LYX_VALIGN_TOP;
1252           break;
1253       case LyXTabular::M_VALIGN_BOTTOM:
1254       case LyXTabular::VALIGN_BOTTOM:
1255           setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1256           break;
1257       case LyXTabular::M_VALIGN_CENTER:
1258       case LyXTabular::VALIGN_CENTER:
1259           setAlign=LyXTabular::LYX_VALIGN_CENTER;
1260           break;
1261       default:
1262           break;
1263     }
1264     if (hasSelection()) {
1265         sel_col_start = tabular->column_of_cell(sel_cell_start);
1266         sel_col_end = tabular->column_of_cell(sel_cell_end);
1267         if (sel_col_start > sel_col_end) {
1268             sel_col_end = sel_col_start;
1269             sel_col_start = tabular->column_of_cell(sel_cell_end);
1270         } else {
1271             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1272         }
1273         
1274         sel_row_start = tabular->row_of_cell(sel_cell_start);
1275         sel_row_end = tabular->row_of_cell(sel_cell_end);
1276         if (sel_row_start > sel_row_end) {
1277                 //int tmp = sel_row_start;
1278                 //sel_row_start = sel_row_end;
1279                 //sel_row_end = tmp;
1280             swap(sel_row_start, sel_row_end);
1281         }
1282     } else {
1283         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1284         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1285     }
1286     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1287 #ifndef NEW_INSETS
1288               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1289               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1290 #else
1291               bv->text->cursor.par()->previous,
1292               bv->text->cursor.par()->next
1293 #endif
1294             );
1295
1296     int row = tabular->row_of_cell(actcell);
1297     int column = tabular->column_of_cell(actcell);
1298     bool flag = true;
1299     
1300     switch (feature) {
1301     case LyXTabular::SET_PWIDTH:
1302     {
1303         bool const update = (tabular->GetColumnPWidth(actcell) != value);
1304         tabular->SetColumnPWidth(actcell,value);
1305         if (update) {
1306             for (int i=0; i < tabular->rows(); ++i) {
1307                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1308                     resizeLyXText(bv);
1309             }
1310             UpdateLocal(bv, INIT, true);
1311         }
1312     }
1313     break;
1314     case LyXTabular::SET_MPWIDTH:
1315     {
1316         bool const update = (tabular->GetPWidth(actcell) != value);
1317         tabular->SetMColumnPWidth(actcell,value);
1318         if (update) {
1319             for (int i=0; i < tabular->rows(); ++i) {
1320                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1321                     resizeLyXText(bv);
1322             }
1323             UpdateLocal(bv, INIT, true);
1324         }
1325     }
1326     break;
1327     case LyXTabular::SET_SPECIAL_COLUMN:
1328     case LyXTabular::SET_SPECIAL_MULTI:
1329         tabular->SetAlignSpecial(actcell,value,feature);
1330         break;
1331     case LyXTabular::APPEND_ROW:
1332         // append the row into the tabular
1333         UnlockInsetInInset(bv, the_locking_inset);
1334         tabular->AppendRow(actcell);
1335         UpdateLocal(bv, INIT, true);
1336         break;
1337     case LyXTabular::APPEND_COLUMN:
1338         // append the column into the tabular
1339         tabular->AppendColumn(actcell);
1340         actcell = tabular->GetCellNumber(row, column);
1341         UpdateLocal(bv, INIT, true);
1342         break;
1343     case LyXTabular::DELETE_ROW:
1344         tabular->DeleteRow(tabular->row_of_cell(actcell));
1345         if ((row+1) > tabular->rows())
1346             --row;
1347         actcell = tabular->GetCellNumber(row, column);
1348         UpdateLocal(bv, INIT, true);
1349         break;
1350     case LyXTabular::DELETE_COLUMN:
1351         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1352         if ((column+1) > tabular->columns())
1353             --column;
1354         actcell = tabular->GetCellNumber(row, column);
1355         UpdateLocal(bv, INIT, true);
1356         break;
1357     case LyXTabular::M_TOGGLE_LINE_TOP:
1358         flag = false;
1359     case LyXTabular::TOGGLE_LINE_TOP:
1360         lineSet = !tabular->TopLine(actcell, flag);
1361         for(i=sel_row_start; i<=sel_row_end; ++i)
1362             for(j=sel_col_start; j<=sel_col_end; ++j)
1363                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1364         UpdateLocal(bv, INIT, true);
1365         break;
1366     
1367     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1368         flag = false;
1369     case LyXTabular::TOGGLE_LINE_BOTTOM:
1370         lineSet = !tabular->BottomLine(actcell, flag); 
1371         for(i=sel_row_start; i<=sel_row_end; ++i)
1372             for(j=sel_col_start; j<=sel_col_end; ++j)
1373                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1374                                        flag);
1375         UpdateLocal(bv, INIT, true);
1376         break;
1377                 
1378     case LyXTabular::M_TOGGLE_LINE_LEFT:
1379         flag = false;
1380     case LyXTabular::TOGGLE_LINE_LEFT:
1381         lineSet = !tabular->LeftLine(actcell, flag);
1382         for(i=sel_row_start; i<=sel_row_end; ++i)
1383             for(j=sel_col_start; j<=sel_col_end; ++j)
1384                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1385                                      flag);
1386         UpdateLocal(bv, INIT, true);
1387         break;
1388
1389     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1390         flag = false;
1391     case LyXTabular::TOGGLE_LINE_RIGHT:
1392         lineSet = !tabular->RightLine(actcell, flag);
1393         for(i=sel_row_start; i<=sel_row_end; ++i)
1394             for(j=sel_col_start; j<=sel_col_end; ++j)
1395                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1396                                       flag);
1397         UpdateLocal(bv, INIT, true);
1398         break;
1399     case LyXTabular::M_ALIGN_LEFT:
1400     case LyXTabular::M_ALIGN_RIGHT:
1401     case LyXTabular::M_ALIGN_CENTER:
1402         flag = false;
1403     case LyXTabular::ALIGN_LEFT:
1404     case LyXTabular::ALIGN_RIGHT:
1405     case LyXTabular::ALIGN_CENTER:
1406         for(i=sel_row_start; i<=sel_row_end; ++i)
1407             for(j=sel_col_start; j<=sel_col_end; ++j)
1408                 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1409                                       flag);
1410         if (hasSelection())
1411             UpdateLocal(bv, INIT, true);
1412         else
1413             UpdateLocal(bv, CELL, true);
1414         break;
1415     case LyXTabular::M_VALIGN_TOP:
1416     case LyXTabular::M_VALIGN_BOTTOM:
1417     case LyXTabular::M_VALIGN_CENTER:
1418         flag = false;
1419     case LyXTabular::VALIGN_TOP:
1420     case LyXTabular::VALIGN_BOTTOM:
1421     case LyXTabular::VALIGN_CENTER:
1422         for(i=sel_row_start; i<=sel_row_end; ++i)
1423             for(j=sel_col_start; j<=sel_col_end; ++j)
1424                 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1425                                        flag);
1426         if (hasSelection())
1427             UpdateLocal(bv, INIT, true);
1428         else
1429             UpdateLocal(bv, CELL, true);
1430         break;
1431     case LyXTabular::MULTICOLUMN:
1432     {
1433         if (sel_row_start != sel_row_end) {
1434             WriteAlert(_("Impossible Operation!"), 
1435                        _("Multicolumns can only be horizontally."), 
1436                        _("Sorry."));
1437             return;
1438         }
1439         // just multicol for one Single Cell
1440         if (!hasSelection()) {
1441             // check wether we are completly in a multicol
1442             if (tabular->IsMultiColumn(actcell)) {
1443                 tabular->UnsetMultiColumn(actcell);
1444                 UpdateLocal(bv, INIT, true);
1445             } else {
1446                 tabular->SetMultiColumn(actcell, 1);
1447                 UpdateLocal(bv, CELL, true);
1448             }
1449             return;
1450         }
1451         // we have a selection so this means we just add all this
1452         // cells to form a multicolumn cell
1453         int s_start;
1454         int s_end;
1455
1456         if (sel_cell_start > sel_cell_end) {
1457             s_start = sel_cell_end;
1458             s_end = sel_cell_start;
1459         } else {
1460             s_start = sel_cell_start;
1461             s_end = sel_cell_end;
1462         }
1463         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1464         actcell = s_start;
1465         cursor.pos(0);
1466         sel_cell_end = sel_cell_start;
1467         sel_pos_end = sel_pos_start;
1468         UpdateLocal(bv, INIT, true);
1469         break;
1470     }
1471     case LyXTabular::SET_ALL_LINES:
1472         setLines = 1;
1473     case LyXTabular::UNSET_ALL_LINES:
1474         for(i=sel_row_start; i<=sel_row_end; ++i)
1475             for(j=sel_col_start; j<=sel_col_end; ++j)
1476                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1477         UpdateLocal(bv, INIT, true);
1478         break;
1479     case LyXTabular::SET_LONGTABULAR:
1480         tabular->SetLongTabular(true);
1481         UpdateLocal(bv, INIT, true); // because this toggles displayed
1482         break;
1483     case LyXTabular::UNSET_LONGTABULAR:
1484         tabular->SetLongTabular(false);
1485         UpdateLocal(bv, INIT, true); // because this toggles displayed
1486         break;
1487     case LyXTabular::SET_ROTATE_TABULAR:
1488         tabular->SetRotateTabular(true);
1489         break;
1490     case LyXTabular::UNSET_ROTATE_TABULAR:
1491         tabular->SetRotateTabular(false);
1492         break;
1493     case LyXTabular::SET_ROTATE_CELL:
1494         for(i=sel_row_start; i<=sel_row_end; ++i)
1495             for(j=sel_col_start; j<=sel_col_end; ++j)
1496                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1497         break;
1498     case LyXTabular::UNSET_ROTATE_CELL:
1499         for(i = sel_row_start; i <= sel_row_end; ++i)
1500             for(j = sel_col_start; j <= sel_col_end; ++j)
1501                 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1502         break;
1503     case LyXTabular::SET_USEBOX:
1504     {
1505         LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1506         if (val == tabular->GetUsebox(actcell))
1507             val = LyXTabular::BOX_NONE;
1508         for(i = sel_row_start; i <= sel_row_end; ++i)
1509             for(j = sel_col_start; j <= sel_col_end; ++j)
1510                 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1511         break;
1512     }
1513     case LyXTabular::SET_LTFIRSTHEAD:
1514         tabular->SetLTHead(actcell, true);
1515         break;
1516     case LyXTabular::SET_LTHEAD:
1517         tabular->SetLTHead(actcell, false);
1518         break;
1519     case LyXTabular::SET_LTFOOT:
1520         tabular->SetLTFoot(actcell, false);
1521         break;
1522     case LyXTabular::SET_LTLASTFOOT:
1523         tabular->SetLTFoot(actcell, true);
1524         break;
1525     case LyXTabular::SET_LTNEWPAGE:
1526         what = !tabular->GetLTNewPage(actcell);
1527         tabular->SetLTNewPage(actcell, what);
1528         break;
1529     }
1530 }
1531
1532
1533 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1534                                      bool behind)
1535 {
1536     // the cursor.pos has to be before the inset so if it isn't now just
1537     // reset the curor pos first!
1538     if (!cellstart(cursor.pos())) {
1539         cursor.pos((cursor.pos() - 1) % 2);
1540         resetPos(bv);
1541     }
1542     UpdatableInset * inset =
1543         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1544     LyXFont font(LyXFont::ALL_SANE);
1545     if (behind) {
1546         x = inset->x() + inset->width(bv, font);
1547         y = inset->descent(bv, font);
1548     }
1549     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1550     inset_y = cursor.y();
1551     inset->Edit(bv, x - inset_x, y - inset_y, button);
1552     if (!the_locking_inset)
1553         return false;
1554     UpdateLocal(bv, CELL, false);
1555     return (the_locking_inset != 0);
1556 }
1557
1558
1559 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1560 {
1561     InsetText * inset = tabular->GetCellInset(actcell);
1562     int x1 = x + top_x;
1563
1564     if (!cellstart(cursor.pos())) {
1565         return (((x + top_x) < cursor.x()) &&
1566                 ((x + top_x) > (cursor.x() - inset->width(bv,
1567                                                       LyXFont(LyXFont::ALL_SANE)))));
1568     } else {
1569         int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1570         return ((x1 > x2) &&
1571                 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1572     }
1573 }
1574
1575
1576 // This returns paperWidth() if the cell-width is unlimited or the width
1577 // in pixels if we have a pwidth for this cell.
1578 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1579 {
1580     string const s = tabular->GetPWidth(cell);
1581
1582     if (s.empty())
1583         return -1;
1584     return VSpace(s).inPixels(0, 0);
1585 }
1586
1587
1588 int InsetTabular::getMaxWidth(Painter & pain,
1589                               UpdatableInset const * inset) const
1590 {
1591     int const n = tabular->GetNumberOfCells();
1592     int cell = 0;
1593     for(; cell < n; ++cell) {
1594         if (tabular->GetCellInset(cell) == inset)
1595             break;
1596     }
1597     if (cell >= n)
1598         return -1;
1599     int w = GetMaxWidthOfCell(pain, cell);
1600     if (w > 0)
1601         // because the inset then subtracts it's top_x and owner->x()
1602         w += (inset->x() - top_x);
1603     return w;
1604 }
1605
1606
1607 void InsetTabular::resizeLyXText(BufferView *) const
1608 {
1609     need_update = FULL;
1610 }
1611
1612
1613 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1614 {
1615     if (the_locking_inset)
1616         return the_locking_inset->getLyXText(bv);
1617     return Inset::getLyXText(bv);
1618 }
1619
1620
1621 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1622 {
1623     if (the_locking_inset) {
1624         InsetTabular * i = static_cast<InsetTabular *>
1625             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1626         if (i) {
1627             i->OpenLayoutDialog(bv);
1628             return;
1629         }
1630     }
1631     dialogs_ = bv->owner()->getDialogs();
1632     dialogs_->showTabular(const_cast<InsetTabular *>(this));
1633 }
1634
1635 //
1636 // functions returns:
1637 // 0 ... disabled
1638 // 1 ... enabled
1639 // 2 ... toggled on
1640 // 3 ... toggled off
1641 //
1642 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1643 {
1644     int action = LyXTabular::LAST_ACTION;
1645     LyXFunc::func_status status = LyXFunc::OK;
1646     
1647     int i = 0;
1648     for(; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1649         string const tmp = tabularFeatures[i].feature;
1650         if (tmp == what.substr(0, tmp.length())) {                  
1651         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1652         //   tabularFeatures[i].feature.length())) {
1653             action = tabularFeatures[i].action;
1654             break;
1655         }
1656     }
1657     if (action == LyXTabular::LAST_ACTION)
1658         return LyXFunc::Unknown;
1659
1660     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1661
1662     int sel_row_start, sel_row_end;
1663     int dummy;
1664     bool flag = true;
1665
1666     if (hasSelection()) {
1667         sel_row_start = tabular->row_of_cell(sel_cell_start);
1668         sel_row_end = tabular->row_of_cell(sel_cell_end);
1669         if (sel_row_start > sel_row_end) {
1670                 //int tmp = sel_row_start;
1671                 //sel_row_start = sel_row_end;
1672                 //sel_row_end = tmp;
1673             swap(sel_row_start, sel_row_end);
1674         }
1675     } else {
1676         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1677     }
1678
1679     switch (action) {
1680     case LyXTabular::SET_PWIDTH:
1681     case LyXTabular::SET_MPWIDTH:
1682     case LyXTabular::SET_SPECIAL_COLUMN:
1683     case LyXTabular::SET_SPECIAL_MULTI:
1684         status |= LyXFunc::Disabled;
1685         return status;
1686
1687     case LyXTabular::APPEND_ROW:
1688     case LyXTabular::APPEND_COLUMN:
1689     case LyXTabular::DELETE_ROW:
1690     case LyXTabular::DELETE_COLUMN:
1691     case LyXTabular::SET_ALL_LINES:
1692     case LyXTabular::UNSET_ALL_LINES:
1693         status |= LyXFunc::OK;
1694         return status;
1695
1696     case LyXTabular::MULTICOLUMN:
1697         if (tabular->IsMultiColumn(actcell))
1698             status |= LyXFunc::ToggleOn;
1699         else
1700             status |= LyXFunc::ToggleOff;
1701         break;
1702     case LyXTabular::M_TOGGLE_LINE_TOP:
1703         flag = false;
1704     case LyXTabular::TOGGLE_LINE_TOP:
1705         if (tabular->TopLine(actcell, flag))
1706             status |= LyXFunc::ToggleOn;
1707         else
1708             status |= LyXFunc::ToggleOff;
1709         break;
1710     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1711         flag = false;
1712     case LyXTabular::TOGGLE_LINE_BOTTOM:
1713         if (tabular->BottomLine(actcell, flag))
1714             status |= LyXFunc::ToggleOn;
1715         else
1716             status |= LyXFunc::ToggleOff;
1717         break;
1718     case LyXTabular::M_TOGGLE_LINE_LEFT:
1719         flag = false;
1720     case LyXTabular::TOGGLE_LINE_LEFT:
1721         if (tabular->LeftLine(actcell, flag))
1722             status |= LyXFunc::ToggleOn;
1723         else
1724             status |= LyXFunc::ToggleOff;
1725         break;
1726     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1727         flag = false;
1728     case LyXTabular::TOGGLE_LINE_RIGHT:
1729         if (tabular->RightLine(actcell, flag))
1730             status |= LyXFunc::ToggleOn;
1731         else
1732             status |= LyXFunc::ToggleOff;
1733         break;
1734     case LyXTabular::M_ALIGN_LEFT:
1735         flag = false;
1736     case LyXTabular::ALIGN_LEFT:
1737         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1738             status |= LyXFunc::ToggleOn;
1739         else
1740             status |= LyXFunc::ToggleOff;
1741         break;
1742     case LyXTabular::M_ALIGN_RIGHT:
1743         flag = false;
1744     case LyXTabular::ALIGN_RIGHT:
1745         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1746             status |= LyXFunc::ToggleOn;
1747         else
1748             status |= LyXFunc::ToggleOff;
1749         break;
1750     case LyXTabular::M_ALIGN_CENTER:
1751         flag = false;
1752     case LyXTabular::ALIGN_CENTER:
1753         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1754             status |= LyXFunc::ToggleOn;
1755         else
1756             status |= LyXFunc::ToggleOff;
1757         break;
1758     case LyXTabular::M_VALIGN_TOP:
1759         flag = false;
1760     case LyXTabular::VALIGN_TOP:
1761         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1762             status |= LyXFunc::ToggleOn;
1763         else
1764             status |= LyXFunc::ToggleOff;
1765         break;
1766     case LyXTabular::M_VALIGN_BOTTOM:
1767         flag = false;
1768     case LyXTabular::VALIGN_BOTTOM:
1769         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1770             status |= LyXFunc::ToggleOn;
1771         else
1772             status |= LyXFunc::ToggleOff;
1773         break;
1774     case LyXTabular::M_VALIGN_CENTER:
1775         flag = false;
1776     case LyXTabular::VALIGN_CENTER:
1777         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1778             status |= LyXFunc::ToggleOn;
1779         else
1780             status |= LyXFunc::ToggleOff;
1781         break;
1782     case LyXTabular::SET_LONGTABULAR:
1783         if (tabular->IsLongTabular())
1784             status |= LyXFunc::ToggleOn;
1785         else
1786             status |= LyXFunc::ToggleOff;
1787         break;
1788     case LyXTabular::UNSET_LONGTABULAR:
1789         if (!tabular->IsLongTabular())
1790             status |= LyXFunc::ToggleOn;
1791         else
1792             status |= LyXFunc::ToggleOff;
1793         break;
1794     case LyXTabular::SET_ROTATE_TABULAR:
1795         if (tabular->GetRotateTabular())
1796             status |= LyXFunc::ToggleOn;
1797         else
1798             status |= LyXFunc::ToggleOff;
1799         break;
1800     case LyXTabular::UNSET_ROTATE_TABULAR:
1801         if (!tabular->GetRotateTabular())
1802             status |= LyXFunc::ToggleOn;
1803         else
1804             status |= LyXFunc::ToggleOff;
1805         break;
1806     case LyXTabular::SET_ROTATE_CELL:
1807         if (tabular->GetRotateCell(actcell))
1808             status |= LyXFunc::ToggleOn;
1809         else
1810             status |= LyXFunc::ToggleOff;
1811         break;
1812     case LyXTabular::UNSET_ROTATE_CELL:
1813         if (!tabular->GetRotateCell(actcell))
1814             status |= LyXFunc::ToggleOn;
1815         else
1816             status |= LyXFunc::ToggleOff;
1817         break;
1818     case LyXTabular::SET_USEBOX:
1819         if (strToInt(argument) == tabular->GetUsebox(actcell))
1820             status |= LyXFunc::ToggleOn;
1821         else
1822             status |= LyXFunc::ToggleOff;
1823         break;
1824     case LyXTabular::SET_LTFIRSTHEAD:
1825         if (tabular->GetRowOfLTHead(actcell, dummy))
1826             status |= LyXFunc::ToggleOn;
1827         else
1828             status |= LyXFunc::ToggleOff;
1829         break;
1830     case LyXTabular::SET_LTHEAD:
1831         if (tabular->GetRowOfLTHead(actcell, dummy))
1832             status |= LyXFunc::ToggleOn;
1833         else
1834             status |= LyXFunc::ToggleOff;
1835         break;
1836     case LyXTabular::SET_LTFOOT:
1837         if (tabular->GetRowOfLTFoot(actcell, dummy))
1838             status |= LyXFunc::ToggleOn;
1839         else
1840             status |= LyXFunc::ToggleOff;
1841         break;
1842     case LyXTabular::SET_LTLASTFOOT:
1843         if (tabular->GetRowOfLTFoot(actcell, dummy))
1844             status |= LyXFunc::ToggleOn;
1845         else
1846             status |= LyXFunc::ToggleOff;
1847         break;
1848     case LyXTabular::SET_LTNEWPAGE:
1849         if (tabular->GetLTNewPage(actcell))
1850             status |= LyXFunc::ToggleOn;
1851         else
1852             status |= LyXFunc::ToggleOff;
1853         break;
1854     default:
1855         status = LyXFunc::Disabled;
1856         break;
1857     }
1858     return status;
1859 }
1860
1861
1862 bool InsetTabular::copySelection()
1863 {
1864     if (!hasSelection())
1865         return false;
1866     delete paste_tabular;
1867
1868     int sel_col_start, sel_col_end;
1869     int sel_row_start, sel_row_end;
1870
1871     sel_col_start = tabular->column_of_cell(sel_cell_start);
1872     sel_col_end = tabular->column_of_cell(sel_cell_end);
1873     if (sel_col_start > sel_col_end) {
1874         sel_col_start = sel_col_end;
1875         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1876     } else {
1877         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1878     }
1879     int columns = sel_col_end - sel_col_start + 1;
1880
1881     sel_row_start = tabular->row_of_cell(sel_cell_start);
1882     sel_row_end = tabular->row_of_cell(sel_cell_end);
1883     if (sel_row_start > sel_row_end) {
1884             //int tmp tmp = sel_row_start;
1885             //sel_row_start = sel_row_end;
1886             //sel_row_end = tmp;
1887         swap(sel_row_start, sel_row_end);
1888     }
1889     int rows = sel_row_end - sel_row_start + 1;
1890
1891     paste_tabular = new LyXTabular(this, rows, columns);
1892     
1893     if (sel_cell_start > sel_cell_end) {
1894             //int tmp = sel_cell_start;
1895             //sel_cell_start = sel_cell_end;
1896             //sel_cell_end = tmp;
1897         swap(sel_cell_start, sel_cell_end);
1898     }
1899     for(int i = sel_cell_start, j = 0; i <= sel_cell_end; ++i, ++j) {
1900         while(paste_tabular->row_of_cell(j) <
1901               (tabular->row_of_cell(i)-sel_row_start)) {
1902             ++j;
1903         }
1904         while(paste_tabular->row_of_cell(j) >
1905               (tabular->row_of_cell(i)-sel_row_start)) {
1906             ++i;
1907         }
1908         *(paste_tabular->GetCellInset(j)) = *(tabular->GetCellInset(i));
1909     }
1910     return true;
1911 }
1912
1913
1914 bool InsetTabular::pasteSelection(BufferView * bv)
1915 {
1916     if (!paste_tabular)
1917         return false;
1918     for(int j=0, i=actcell; j<paste_tabular->GetNumberOfCells(); ++j,++i) {
1919         while (paste_tabular->row_of_cell(j) > tabular->row_of_cell(i)-actrow)
1920             ++i;
1921         if (tabular->GetNumberOfCells() <= i)
1922             break;
1923         while (paste_tabular->row_of_cell(j) < tabular->row_of_cell(i)-actrow)
1924             ++j;
1925         if (paste_tabular->GetNumberOfCells() <= j)
1926             break;
1927         *(tabular->GetCellInset(i)) = *(paste_tabular->GetCellInset(j));
1928         tabular->GetCellInset(i)->setOwner(this);
1929         tabular->GetCellInset(i)->deleteLyXText(bv);
1930     }
1931     return true;
1932 }
1933
1934
1935 bool InsetTabular::cutSelection()
1936 {
1937     if (!hasSelection())
1938         return false;
1939
1940     int sel_col_start, sel_col_end;
1941     int sel_row_start, sel_row_end;
1942
1943     sel_col_start = tabular->column_of_cell(sel_cell_start);
1944     sel_col_end = tabular->column_of_cell(sel_cell_end);
1945     if (sel_col_start > sel_col_end) {
1946         sel_col_start = sel_col_end;
1947         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1948     } else {
1949         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1950     }
1951     sel_row_start = tabular->row_of_cell(sel_cell_start);
1952     sel_row_end = tabular->row_of_cell(sel_cell_end);
1953     if (sel_row_start > sel_row_end) {
1954             //int tmp = sel_row_start;
1955             //sel_row_start = sel_row_end;
1956             //sel_row_end = tmp;
1957         swap(sel_row_start, sel_row_end);
1958     }
1959     if (sel_cell_start > sel_cell_end) {
1960             //int tmp = sel_cell_start;
1961             //sel_cell_start = sel_cell_end;
1962             //sel_cell_end = tmp;
1963         swap(sel_cell_start, sel_cell_end);
1964     }
1965     for(int i = sel_row_start; i <= sel_row_end; ++i) {
1966         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1967             tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
1968         }
1969     }
1970     return true;
1971 }
1972