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