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