]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
use the new sstream return non-pods as const, use string instead of char * in a lot...
[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
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 string 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 what;
1213
1214     switch (feature) {
1215       case LyXTabular::M_ALIGN_LEFT:
1216       case LyXTabular::ALIGN_LEFT:
1217           setAlign=LYX_ALIGN_LEFT;
1218           break;
1219       case LyXTabular::M_ALIGN_RIGHT:
1220       case LyXTabular::ALIGN_RIGHT:
1221           setAlign=LYX_ALIGN_RIGHT;
1222           break;
1223       case LyXTabular::M_ALIGN_CENTER:
1224       case LyXTabular::ALIGN_CENTER:
1225           setAlign=LYX_ALIGN_CENTER;
1226           break;
1227       case LyXTabular::M_VALIGN_TOP:
1228       case LyXTabular::VALIGN_TOP:
1229           setAlign=LyXTabular::LYX_VALIGN_TOP;
1230           break;
1231       case LyXTabular::M_VALIGN_BOTTOM:
1232       case LyXTabular::VALIGN_BOTTOM:
1233           setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1234           break;
1235       case LyXTabular::M_VALIGN_CENTER:
1236       case LyXTabular::VALIGN_CENTER:
1237           setAlign=LyXTabular::LYX_VALIGN_CENTER;
1238           break;
1239       default:
1240           break;
1241     }
1242     if (hasSelection()) {
1243         int tmp;
1244         sel_col_start = tabular->column_of_cell(sel_cell_start);
1245         sel_col_end = tabular->column_of_cell(sel_cell_end);
1246         if (sel_col_start > sel_col_end) {
1247             sel_col_end = sel_col_start;
1248             sel_col_start = tabular->column_of_cell(sel_cell_end);
1249         } else {
1250             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1251         }
1252         
1253         sel_row_start = tabular->row_of_cell(sel_cell_start);
1254         sel_row_end = tabular->row_of_cell(sel_cell_end);
1255         if (sel_row_start > sel_row_end) {
1256             tmp = sel_row_start;
1257             sel_row_start = sel_row_end;
1258             sel_row_end = tmp;
1259         }
1260     } else {
1261         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1262         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1263     }
1264     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1265 #ifndef NEW_INSETS
1266               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1267               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1268 #else
1269               bv->text->cursor.par()->previous,
1270               bv->text->cursor.par()->next
1271 #endif
1272             );
1273
1274     int row = tabular->row_of_cell(actcell);
1275     int column = tabular->column_of_cell(actcell);
1276     bool flag = true;
1277     
1278     switch (feature) {
1279     case LyXTabular::SET_PWIDTH:
1280     {
1281         bool update = (tabular->GetColumnPWidth(actcell) != value);
1282         tabular->SetColumnPWidth(actcell,value);
1283         if (update) {
1284             for (int i=0; i < tabular->rows(); ++i) {
1285                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1286                     resizeLyXText(bv);
1287             }
1288             UpdateLocal(bv, INIT, true);
1289         }
1290     }
1291     break;
1292     case LyXTabular::SET_MPWIDTH:
1293     {
1294         bool update = (tabular->GetPWidth(actcell) != value);
1295         tabular->SetMColumnPWidth(actcell,value);
1296         if (update) {
1297             for (int i=0; i < tabular->rows(); ++i) {
1298                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1299                     resizeLyXText(bv);
1300             }
1301             UpdateLocal(bv, INIT, true);
1302         }
1303     }
1304     break;
1305     case LyXTabular::SET_SPECIAL_COLUMN:
1306     case LyXTabular::SET_SPECIAL_MULTI:
1307         tabular->SetAlignSpecial(actcell,value,feature);
1308         break;
1309     case LyXTabular::APPEND_ROW:
1310         // append the row into the tabular
1311         UnlockInsetInInset(bv, the_locking_inset);
1312         tabular->AppendRow(actcell);
1313         UpdateLocal(bv, INIT, true);
1314         break;
1315     case LyXTabular::APPEND_COLUMN:
1316         // append the column into the tabular
1317         tabular->AppendColumn(actcell);
1318         actcell = tabular->GetCellNumber(row, column);
1319         UpdateLocal(bv, INIT, true);
1320         break;
1321     case LyXTabular::DELETE_ROW:
1322         tabular->DeleteRow(tabular->row_of_cell(actcell));
1323         if ((row+1) > tabular->rows())
1324             --row;
1325         actcell = tabular->GetCellNumber(row, column);
1326         UpdateLocal(bv, INIT, true);
1327         break;
1328     case LyXTabular::DELETE_COLUMN:
1329         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1330         if ((column+1) > tabular->columns())
1331             --column;
1332         actcell = tabular->GetCellNumber(row, column);
1333         UpdateLocal(bv, INIT, true);
1334         break;
1335     case LyXTabular::M_TOGGLE_LINE_TOP:
1336         flag = false;
1337     case LyXTabular::TOGGLE_LINE_TOP:
1338         lineSet = !tabular->TopLine(actcell, flag);
1339         for(i=sel_row_start; i<=sel_row_end; ++i)
1340             for(j=sel_col_start; j<=sel_col_end; ++j)
1341                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1342         UpdateLocal(bv, INIT, true);
1343         break;
1344     
1345     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1346         flag = false;
1347     case LyXTabular::TOGGLE_LINE_BOTTOM:
1348         lineSet = !tabular->BottomLine(actcell, flag); 
1349         for(i=sel_row_start; i<=sel_row_end; ++i)
1350             for(j=sel_col_start; j<=sel_col_end; ++j)
1351                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1352                                        flag);
1353         UpdateLocal(bv, INIT, true);
1354         break;
1355                 
1356     case LyXTabular::M_TOGGLE_LINE_LEFT:
1357         flag = false;
1358     case LyXTabular::TOGGLE_LINE_LEFT:
1359         lineSet = !tabular->LeftLine(actcell, flag);
1360         for(i=sel_row_start; i<=sel_row_end; ++i)
1361             for(j=sel_col_start; j<=sel_col_end; ++j)
1362                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1363                                      flag);
1364         UpdateLocal(bv, INIT, true);
1365         break;
1366
1367     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1368         flag = false;
1369     case LyXTabular::TOGGLE_LINE_RIGHT:
1370         lineSet = !tabular->RightLine(actcell, flag);
1371         for(i=sel_row_start; i<=sel_row_end; ++i)
1372             for(j=sel_col_start; j<=sel_col_end; ++j)
1373                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1374                                       flag);
1375         UpdateLocal(bv, INIT, true);
1376         break;
1377     case LyXTabular::M_ALIGN_LEFT:
1378     case LyXTabular::M_ALIGN_RIGHT:
1379     case LyXTabular::M_ALIGN_CENTER:
1380         flag = false;
1381     case LyXTabular::ALIGN_LEFT:
1382     case LyXTabular::ALIGN_RIGHT:
1383     case LyXTabular::ALIGN_CENTER:
1384         for(i=sel_row_start; i<=sel_row_end; ++i)
1385             for(j=sel_col_start; j<=sel_col_end; ++j)
1386                 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1387                                       flag);
1388         if (hasSelection())
1389             UpdateLocal(bv, INIT, true);
1390         else
1391             UpdateLocal(bv, CELL, true);
1392         break;
1393     case LyXTabular::M_VALIGN_TOP:
1394     case LyXTabular::M_VALIGN_BOTTOM:
1395     case LyXTabular::M_VALIGN_CENTER:
1396         flag = false;
1397     case LyXTabular::VALIGN_TOP:
1398     case LyXTabular::VALIGN_BOTTOM:
1399     case LyXTabular::VALIGN_CENTER:
1400         for(i=sel_row_start; i<=sel_row_end; ++i)
1401             for(j=sel_col_start; j<=sel_col_end; ++j)
1402                 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1403                                        flag);
1404         if (hasSelection())
1405             UpdateLocal(bv, INIT, true);
1406         else
1407             UpdateLocal(bv, CELL, true);
1408         break;
1409     case LyXTabular::MULTICOLUMN:
1410     {
1411         if (sel_row_start != sel_row_end) {
1412             WriteAlert(_("Impossible Operation!"), 
1413                        _("Multicolumns can only be horizontally."), 
1414                        _("Sorry."));
1415             return;
1416         }
1417         // just multicol for one Single Cell
1418         if (!hasSelection()) {
1419             // check wether we are completly in a multicol
1420             if (tabular->IsMultiColumn(actcell)) {
1421                 tabular->UnsetMultiColumn(actcell);
1422                 UpdateLocal(bv, INIT, true);
1423             } else {
1424                 tabular->SetMultiColumn(actcell, 1);
1425                 UpdateLocal(bv, CELL, true);
1426             }
1427             return;
1428         }
1429         // we have a selection so this means we just add all this
1430         // cells to form a multicolumn cell
1431         int
1432             s_start, s_end;
1433
1434         if (sel_cell_start > sel_cell_end) {
1435             s_start = sel_cell_end;
1436             s_end = sel_cell_start;
1437         } else {
1438             s_start = sel_cell_start;
1439             s_end = sel_cell_end;
1440         }
1441         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1442         actcell = s_start;
1443         cursor.pos(0);
1444         sel_cell_end = sel_cell_start;
1445         sel_pos_end = sel_pos_start;
1446         UpdateLocal(bv, INIT, true);
1447         break;
1448     }
1449     case LyXTabular::SET_ALL_LINES:
1450         setLines = 1;
1451     case LyXTabular::UNSET_ALL_LINES:
1452         for(i=sel_row_start; i<=sel_row_end; ++i)
1453             for(j=sel_col_start; j<=sel_col_end; ++j)
1454                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1455         UpdateLocal(bv, INIT, true);
1456         break;
1457     case LyXTabular::SET_LONGTABULAR:
1458         tabular->SetLongTabular(true);
1459         UpdateLocal(bv, INIT, true); // because this toggles displayed
1460         break;
1461     case LyXTabular::UNSET_LONGTABULAR:
1462         tabular->SetLongTabular(false);
1463         UpdateLocal(bv, INIT, true); // because this toggles displayed
1464         break;
1465     case LyXTabular::SET_ROTATE_TABULAR:
1466         tabular->SetRotateTabular(true);
1467         break;
1468     case LyXTabular::UNSET_ROTATE_TABULAR:
1469         tabular->SetRotateTabular(false);
1470         break;
1471     case LyXTabular::SET_ROTATE_CELL:
1472         for(i=sel_row_start; i<=sel_row_end; ++i)
1473             for(j=sel_col_start; j<=sel_col_end; ++j)
1474                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1475         break;
1476     case LyXTabular::UNSET_ROTATE_CELL:
1477         for(i=sel_row_start; i<=sel_row_end; ++i)
1478             for(j=sel_col_start; j<=sel_col_end; ++j)
1479                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1480         break;
1481     case LyXTabular::SET_USEBOX:
1482     {
1483         LyXTabular::BoxType val = static_cast<LyXTabular::BoxType>
1484             (strToInt(value));
1485         if (val == tabular->GetUsebox(actcell))
1486             val = LyXTabular::BOX_NONE;
1487         for(i=sel_row_start; i<=sel_row_end; ++i)
1488             for(j=sel_col_start; j<=sel_col_end; ++j)
1489                 tabular->SetUsebox(tabular->GetCellNumber(i,j),val);
1490         break;
1491     }
1492     case LyXTabular::SET_LTFIRSTHEAD:
1493         tabular->SetLTHead(actcell,true);
1494         break;
1495     case LyXTabular::SET_LTHEAD:
1496         tabular->SetLTHead(actcell,false);
1497         break;
1498     case LyXTabular::SET_LTFOOT:
1499         tabular->SetLTFoot(actcell,false);
1500         break;
1501     case LyXTabular::SET_LTLASTFOOT:
1502         tabular->SetLTFoot(actcell,true);
1503         break;
1504     case LyXTabular::SET_LTNEWPAGE:
1505         what = !tabular->GetLTNewPage(actcell);
1506         tabular->SetLTNewPage(actcell,what);
1507         break;
1508     }
1509 }
1510
1511
1512 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1513                                      bool behind)
1514 {
1515     // the cursor.pos has to be before the inset so if it isn't now just
1516     // reset the curor pos first!
1517     if (!cellstart(cursor.pos())) {
1518         cursor.pos((cursor.pos() - 1) % 2);
1519         resetPos(bv);
1520     }
1521     UpdatableInset * inset =
1522         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1523     LyXFont font(LyXFont::ALL_SANE);
1524     if (behind) {
1525         x = inset->x() + inset->width(bv, font);
1526         y = inset->descent(bv, font);
1527     }
1528     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1529     inset_y = cursor.y();
1530     inset->Edit(bv, x - inset_x, y - inset_y, button);
1531     if (!the_locking_inset)
1532         return false;
1533     UpdateLocal(bv, CELL, false);
1534     return true;
1535 }
1536
1537
1538 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1539 {
1540     InsetText * inset = tabular->GetCellInset(actcell);
1541     int x1 = x + top_x;
1542
1543     if (!cellstart(cursor.pos())) {
1544         return (((x + top_x) < cursor.x()) &&
1545                 ((x + top_x) > (cursor.x() - inset->width(bv,
1546                                                       LyXFont(LyXFont::ALL_SANE)))));
1547     } else {
1548         int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1549         return ((x1 > x2) &&
1550                 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1551     }
1552 }
1553
1554
1555 // This returns paperWidth() if the cell-width is unlimited or the width
1556 // in pixels if we have a pwidth for this cell.
1557 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1558 {
1559     string s = tabular->GetPWidth(cell);
1560
1561     if (s.empty())
1562         return -1;
1563     return VSpace(s).inPixels( 0, 0);
1564 }
1565
1566
1567 int InsetTabular::getMaxWidth(Painter & pain,
1568                               UpdatableInset const * inset) const
1569 {
1570     int cell;
1571     int n = tabular->GetNumberOfCells();
1572     for(cell=0; cell < n; ++cell) {
1573         if (tabular->GetCellInset(cell) == inset)
1574             break;
1575     }
1576     if (cell >= n)
1577         return -1;
1578     int w = GetMaxWidthOfCell(pain, cell);
1579     if (w > 0)
1580         // because the inset then subtracts it's top_x and owner->x()
1581         w += (inset->x() - top_x);
1582     return w;
1583 }
1584
1585
1586 void InsetTabular::resizeLyXText(BufferView *) const
1587 {
1588     need_update = FULL;
1589 }
1590
1591 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1592 {
1593     if (the_locking_inset)
1594         return the_locking_inset->getLyXText(bv);
1595     return Inset::getLyXText(bv);
1596 }
1597
1598
1599 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1600 {
1601     if (the_locking_inset) {
1602         InsetTabular * i = static_cast<InsetTabular *>
1603             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1604         if (i) {
1605             i->OpenLayoutDialog(bv);
1606             return;
1607         }
1608     }
1609     dialogs_ = bv->owner()->getDialogs();
1610     dialogs_->showTabular(const_cast<InsetTabular *>(this));
1611 }
1612
1613 //
1614 // functions returns:
1615 // 0 ... disabled
1616 // 1 ... enabled
1617 // 2 ... toggled on
1618 // 3 ... toggled off
1619 //
1620 LyXFunc::func_status InsetTabular::getStatus(string what) const
1621 {
1622     int action = LyXTabular::LAST_ACTION;
1623     string argument;
1624     int i;
1625     LyXFunc::func_status status = LyXFunc::OK;
1626     
1627     for(i=0; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1628         if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1629                      tabularFeatures[i].feature.length())) {
1630             action = tabularFeatures[i].action;
1631             break;
1632         }
1633     }
1634     if (action == LyXTabular::LAST_ACTION)
1635         return LyXFunc::Unknown;
1636
1637     argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1638
1639     int sel_row_start, sel_row_end;
1640     int dummy;
1641     bool flag = true;
1642
1643     if (hasSelection()) {
1644         int tmp;
1645         sel_row_start = tabular->row_of_cell(sel_cell_start);
1646         sel_row_end = tabular->row_of_cell(sel_cell_end);
1647         if (sel_row_start > sel_row_end) {
1648             tmp = sel_row_start;
1649             sel_row_start = sel_row_end;
1650             sel_row_end = tmp;
1651         }
1652     } else {
1653         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1654     }
1655
1656     switch (action) {
1657     case LyXTabular::SET_PWIDTH:
1658     case LyXTabular::SET_MPWIDTH:
1659     case LyXTabular::SET_SPECIAL_COLUMN:
1660     case LyXTabular::SET_SPECIAL_MULTI:
1661         status |= LyXFunc::Disabled;
1662         return status;
1663
1664     case LyXTabular::APPEND_ROW:
1665     case LyXTabular::APPEND_COLUMN:
1666     case LyXTabular::DELETE_ROW:
1667     case LyXTabular::DELETE_COLUMN:
1668     case LyXTabular::SET_ALL_LINES:
1669     case LyXTabular::UNSET_ALL_LINES:
1670         status |= LyXFunc::OK;
1671         return status;
1672
1673     case LyXTabular::MULTICOLUMN:
1674         if (tabular->IsMultiColumn(actcell))
1675             status |= LyXFunc::ToggleOn;
1676         else
1677             status |= LyXFunc::ToggleOff;
1678         break;
1679     case LyXTabular::M_TOGGLE_LINE_TOP:
1680         flag = false;
1681     case LyXTabular::TOGGLE_LINE_TOP:
1682         if (tabular->TopLine(actcell, flag))
1683             status |= LyXFunc::ToggleOn;
1684         else
1685             status |= LyXFunc::ToggleOff;
1686         break;
1687     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1688         flag = false;
1689     case LyXTabular::TOGGLE_LINE_BOTTOM:
1690         if (tabular->BottomLine(actcell, flag))
1691             status |= LyXFunc::ToggleOn;
1692         else
1693             status |= LyXFunc::ToggleOff;
1694         break;
1695     case LyXTabular::M_TOGGLE_LINE_LEFT:
1696         flag = false;
1697     case LyXTabular::TOGGLE_LINE_LEFT:
1698         if (tabular->LeftLine(actcell, flag))
1699             status |= LyXFunc::ToggleOn;
1700         else
1701             status |= LyXFunc::ToggleOff;
1702         break;
1703     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1704         flag = false;
1705     case LyXTabular::TOGGLE_LINE_RIGHT:
1706         if (tabular->RightLine(actcell, flag))
1707             status |= LyXFunc::ToggleOn;
1708         else
1709             status |= LyXFunc::ToggleOff;
1710         break;
1711     case LyXTabular::M_ALIGN_LEFT:
1712         flag = false;
1713     case LyXTabular::ALIGN_LEFT:
1714         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1715             status |= LyXFunc::ToggleOn;
1716         else
1717             status |= LyXFunc::ToggleOff;
1718         break;
1719     case LyXTabular::M_ALIGN_RIGHT:
1720         flag = false;
1721     case LyXTabular::ALIGN_RIGHT:
1722         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1723             status |= LyXFunc::ToggleOn;
1724         else
1725             status |= LyXFunc::ToggleOff;
1726         break;
1727     case LyXTabular::M_ALIGN_CENTER:
1728         flag = false;
1729     case LyXTabular::ALIGN_CENTER:
1730         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1731             status |= LyXFunc::ToggleOn;
1732         else
1733             status |= LyXFunc::ToggleOff;
1734         break;
1735     case LyXTabular::M_VALIGN_TOP:
1736         flag = false;
1737     case LyXTabular::VALIGN_TOP:
1738         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1739             status |= LyXFunc::ToggleOn;
1740         else
1741             status |= LyXFunc::ToggleOff;
1742         break;
1743     case LyXTabular::M_VALIGN_BOTTOM:
1744         flag = false;
1745     case LyXTabular::VALIGN_BOTTOM:
1746         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1747             status |= LyXFunc::ToggleOn;
1748         else
1749             status |= LyXFunc::ToggleOff;
1750         break;
1751     case LyXTabular::M_VALIGN_CENTER:
1752         flag = false;
1753     case LyXTabular::VALIGN_CENTER:
1754         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1755             status |= LyXFunc::ToggleOn;
1756         else
1757             status |= LyXFunc::ToggleOff;
1758         break;
1759     case LyXTabular::SET_LONGTABULAR:
1760         if (tabular->IsLongTabular())
1761             status |= LyXFunc::ToggleOn;
1762         else
1763             status |= LyXFunc::ToggleOff;
1764         break;
1765     case LyXTabular::UNSET_LONGTABULAR:
1766         if (!tabular->IsLongTabular())
1767             status |= LyXFunc::ToggleOn;
1768         else
1769             status |= LyXFunc::ToggleOff;
1770         break;
1771     case LyXTabular::SET_ROTATE_TABULAR:
1772         if (tabular->GetRotateTabular())
1773             status |= LyXFunc::ToggleOn;
1774         else
1775             status |= LyXFunc::ToggleOff;
1776         break;
1777     case LyXTabular::UNSET_ROTATE_TABULAR:
1778         if (!tabular->GetRotateTabular())
1779             status |= LyXFunc::ToggleOn;
1780         else
1781             status |= LyXFunc::ToggleOff;
1782         break;
1783     case LyXTabular::SET_ROTATE_CELL:
1784         if (tabular->GetRotateCell(actcell))
1785             status |= LyXFunc::ToggleOn;
1786         else
1787             status |= LyXFunc::ToggleOff;
1788         break;
1789     case LyXTabular::UNSET_ROTATE_CELL:
1790         if (!tabular->GetRotateCell(actcell))
1791             status |= LyXFunc::ToggleOn;
1792         else
1793             status |= LyXFunc::ToggleOff;
1794         break;
1795     case LyXTabular::SET_USEBOX:
1796         if (strToInt(argument) == tabular->GetUsebox(actcell))
1797             status |= LyXFunc::ToggleOn;
1798         else
1799             status |= LyXFunc::ToggleOff;
1800         break;
1801     case LyXTabular::SET_LTFIRSTHEAD:
1802         if (tabular->GetRowOfLTHead(actcell, dummy))
1803             status |= LyXFunc::ToggleOn;
1804         else
1805             status |= LyXFunc::ToggleOff;
1806         break;
1807     case LyXTabular::SET_LTHEAD:
1808         if (tabular->GetRowOfLTHead(actcell, dummy))
1809             status |= LyXFunc::ToggleOn;
1810         else
1811             status |= LyXFunc::ToggleOff;
1812         break;
1813     case LyXTabular::SET_LTFOOT:
1814         if (tabular->GetRowOfLTFoot(actcell, dummy))
1815             status |= LyXFunc::ToggleOn;
1816         else
1817             status |= LyXFunc::ToggleOff;
1818         break;
1819     case LyXTabular::SET_LTLASTFOOT:
1820         if (tabular->GetRowOfLTFoot(actcell, dummy))
1821             status |= LyXFunc::ToggleOn;
1822         else
1823             status |= LyXFunc::ToggleOff;
1824         break;
1825     case LyXTabular::SET_LTNEWPAGE:
1826         if (tabular->GetLTNewPage(actcell))
1827             status |= LyXFunc::ToggleOn;
1828         else
1829             status |= LyXFunc::ToggleOff;
1830         break;
1831     default:
1832         status = LyXFunc::Disabled;
1833         break;
1834     }
1835     return status;
1836 }
1837
1838
1839 bool InsetTabular::copySelection()
1840 {
1841     if (!hasSelection())
1842         return false;
1843     delete paste_tabular;
1844
1845     int sel_col_start, sel_col_end;
1846     int sel_row_start, sel_row_end;
1847
1848     sel_col_start = tabular->column_of_cell(sel_cell_start);
1849     sel_col_end = tabular->column_of_cell(sel_cell_end);
1850     if (sel_col_start > sel_col_end) {
1851         sel_col_start = sel_col_end;
1852         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1853     } else {
1854         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1855     }
1856     int columns = sel_col_end - sel_col_start + 1;
1857
1858     sel_row_start = tabular->row_of_cell(sel_cell_start);
1859     sel_row_end = tabular->row_of_cell(sel_cell_end);
1860     if (sel_row_start > sel_row_end) {
1861         int tmp;
1862         tmp = sel_row_start;
1863         sel_row_start = sel_row_end;
1864         sel_row_end = tmp;
1865     }
1866     int rows = sel_row_end - sel_row_start + 1;
1867
1868     paste_tabular = new LyXTabular(this, rows, columns);
1869     
1870     if (sel_cell_start > sel_cell_end) {
1871         int tmp = sel_cell_start;
1872         sel_cell_start = sel_cell_end;
1873         sel_cell_end = tmp;
1874     }
1875     for(int i=sel_cell_start, j=0; i <= sel_cell_end; ++i, ++j) {
1876         while(paste_tabular->row_of_cell(j) <
1877               (tabular->row_of_cell(i)-sel_row_start)) {
1878             ++j;
1879         }
1880         while(paste_tabular->row_of_cell(j) >
1881               (tabular->row_of_cell(i)-sel_row_start)) {
1882             ++i;
1883         }
1884         *(paste_tabular->GetCellInset(j)) = *(tabular->GetCellInset(i));
1885     }
1886     return true;
1887 }
1888
1889
1890 bool InsetTabular::pasteSelection(BufferView * bv)
1891 {
1892     if (!paste_tabular)
1893         return false;
1894     for(int j=0, i=actcell; j<paste_tabular->GetNumberOfCells(); ++j,++i) {
1895         while (paste_tabular->row_of_cell(j) > tabular->row_of_cell(i)-actrow)
1896             ++i;
1897         if (tabular->GetNumberOfCells() <= i)
1898             break;
1899         while (paste_tabular->row_of_cell(j) < tabular->row_of_cell(i)-actrow)
1900             ++j;
1901         if (paste_tabular->GetNumberOfCells() <= j)
1902             break;
1903         *(tabular->GetCellInset(i)) = *(paste_tabular->GetCellInset(j));
1904         tabular->GetCellInset(i)->setOwner(this);
1905         tabular->GetCellInset(i)->deleteLyXText(bv);
1906     }
1907     return true;
1908 }
1909
1910
1911 bool InsetTabular::cutSelection()
1912 {
1913     if (!hasSelection())
1914         return false;
1915     for(int i=sel_cell_start; i < sel_cell_end; ++i) {
1916         tabular->GetCellInset(i)->clear();
1917     }
1918     return true;
1919 }
1920