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