]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
Fixes for tabular-clipboard-paste and tabular-scroll.
[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     UpdatableInset::RESULT 
767         result;
768
769     no_selection = false;
770     if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
771         || (result == DISPATCHED_NOUPDATE)) {
772
773         resetPos(bv);
774         return result;
775     }
776
777     if ((action < 0) && arg.empty())
778         return FINISHED;
779
780     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
781         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
782         cursor.x_fix(-1);
783     if (the_locking_inset) {
784         result=the_locking_inset->LocalDispatch(bv, action, arg);
785         if (result == DISPATCHED_NOUPDATE)
786             return result;
787         else if (result == DISPATCHED) {
788             the_locking_inset->ToggleInsetCursor(bv);
789             UpdateLocal(bv, CELL, false);
790             the_locking_inset->ToggleInsetCursor(bv);
791             return result;
792         } else if (result == FINISHED) {
793             result = DISPATCHED;
794             if ((action == LFUN_RIGHT) || (action == -1)) {
795                 cursor.pos(inset_pos + 1);
796                 resetPos(bv);
797             } else if (action == LFUN_DOWN) {
798                 if (moveDown(bv) == FINISHED) {
799                     bv->unlockInset(this);
800                     result = FINISHED;
801                 }
802             } else if (action == LFUN_UP) {
803                 if (moveUp(bv) == FINISHED) {
804                     bv->unlockInset(this);
805                     result = FINISHED;
806                 }
807             }
808             sel_pos_start = sel_pos_end = cursor.pos();
809             sel_cell_start = sel_cell_end = actcell;
810             the_locking_inset=0;
811             ShowInsetCursor(bv);
812             return result;
813         }
814     }
815
816     bool hs = hasSelection();
817     HideInsetCursor(bv);
818     result=DISPATCHED;
819     switch (action) {
820         // --- Cursor Movements ---------------------------------------------
821     case LFUN_RIGHTSEL:
822         if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
823             break;
824         moveRight(bv, false);
825 #if 0
826         if (!cellstart(cursor.pos())) {
827             sel_pos_end = cursor.pos();
828             if (tabular->right_column_of_cell(sel_cell_start) >
829                 tabular->right_column_of_cell(actcell))
830                 sel_cell_end = actcell+1;
831             else
832                 sel_cell_end = actcell;
833         }
834 #else
835         sel_pos_end = cursor.pos();
836         sel_cell_end = actcell;
837 #endif
838         UpdateLocal(bv, SELECTION, false);
839         break;
840     case LFUN_RIGHT:
841         result = moveRight(bv);
842         sel_pos_start = sel_pos_end = cursor.pos();
843         sel_cell_start = sel_cell_end = actcell;
844         if (hs)
845             UpdateLocal(bv, SELECTION, false);
846         break;
847     case LFUN_LEFTSEL:
848         if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
849             break;
850         moveLeft(bv, false);
851 #if 0
852         if (cellstart(cursor.pos())) {
853             sel_pos_end = cursor.pos();
854             if (tabular->column_of_cell(sel_cell_start) >=
855                 tabular->column_of_cell(actcell))
856                 sel_cell_end = actcell;
857             else
858                 sel_cell_end = actcell-1;
859         }
860 #else
861         sel_pos_end = cursor.pos();
862         sel_cell_end = actcell;
863 #endif
864         UpdateLocal(bv, SELECTION, false);
865         break;
866     case LFUN_LEFT:
867         result = moveLeft(bv);
868         sel_pos_start = sel_pos_end = cursor.pos();
869         sel_cell_start = sel_cell_end = actcell;
870         if (hs)
871             UpdateLocal(bv, SELECTION, false);
872         break;
873     case LFUN_DOWNSEL:
874     {
875         int const ocell = actcell;
876         moveDown(bv);
877         sel_pos_end = cursor.pos();
878         if ((ocell == sel_cell_end) ||
879             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
880             sel_cell_end = tabular->GetCellBelow(sel_cell_end);
881         else
882             sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
883         UpdateLocal(bv, SELECTION, false);
884     }
885     break;
886     case LFUN_DOWN:
887         result = moveDown(bv);
888         sel_pos_start = sel_pos_end = cursor.pos();
889         sel_cell_start = sel_cell_end = actcell;
890         if (hs)
891             UpdateLocal(bv, SELECTION, false);
892         break;
893     case LFUN_UPSEL:
894     {
895         int const ocell = actcell;
896         moveUp(bv);
897         sel_pos_end = cursor.pos();
898         if ((ocell == sel_cell_end) ||
899             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
900             sel_cell_end = tabular->GetCellAbove(sel_cell_end);
901         else
902             sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
903         UpdateLocal(bv, SELECTION, false);
904     }
905     break;
906     case LFUN_UP:
907         result = moveUp(bv);
908         sel_pos_start = sel_pos_end = cursor.pos();
909         sel_cell_start = sel_cell_end = actcell;
910         if (hs)
911             UpdateLocal(bv, SELECTION, false);
912         break;
913     case LFUN_BACKSPACE:
914         break;
915     case LFUN_DELETE:
916         break;
917     case LFUN_HOME:
918         break;
919     case LFUN_END:
920         break;
921     case LFUN_SHIFT_TAB:
922     case LFUN_TAB:
923         if (the_locking_inset) {
924             UnlockInsetInInset(bv, the_locking_inset);
925             the_locking_inset = 0;
926         }
927         if (action == LFUN_TAB)
928             moveNextCell(bv);
929         else
930             movePrevCell(bv);
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     case LFUN_LAYOUT_TABULAR:
937     {
938         bv->owner()->getDialogs()->showTabular(this);
939     }
940     break;
941     case LFUN_TABULAR_FEATURE:
942         if (!TabularFeatures(bv, arg))
943             result = UNDISPATCHED;
944         break;
945     case LFUN_CUT:
946         if (!copySelection(bv))
947             break;
948         bv->text->SetUndo(bv->buffer(), Undo::DELETE,
949 #ifndef NEW_INSETS
950           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
951           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
952 #else
953           bv->text->cursor.par()->previous,
954           bv->text->cursor.par()->next
955 #endif
956                 );
957         cutSelection();
958         UpdateLocal(bv, INIT, true);
959         break;
960     case LFUN_COPY:
961         if (!hasSelection())
962             break;
963         bv->text->FinishUndo();
964         copySelection(bv);
965         break;
966     case LFUN_PASTESELECTION:
967     {
968         string clip(bv->workarea()->getClipboard());
969         
970         if (clip.empty())
971             break;
972         if (clip.find('\t') != string::npos) {
973             int cols = 1;
974             int rows = 1;
975             int maxCols = 1;
976             unsigned int len = clip.length();
977             string::size_type p = 0;
978
979             while((p < len) &&
980                   ((p = clip.find_first_of("\t\n", p)) != string::npos))
981             {
982                 switch(clip[p]) {
983                 case '\t':
984                     ++cols;
985                     break;
986                 case '\n':
987                     if ((p+1) < len)
988                         ++rows;
989                     maxCols = max(cols, maxCols);
990                     cols = 1;
991                     break;
992                 }
993                 ++p;
994             }
995             maxCols = max(cols, maxCols);
996             delete paste_tabular;
997             paste_tabular = new LyXTabular(this, rows, maxCols);
998             string::size_type op = 0;
999             int cell = 0;
1000             int cells = paste_tabular->GetNumberOfCells();
1001             p = cols = 0;
1002             while((cell < cells) && (p < len) &&
1003                   (p = clip.find_first_of("\t\n", p)) != string::npos)
1004             {
1005                 if (p >= len)
1006                     break;
1007                 switch(clip[p]) {
1008                 case '\t':
1009                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1010                     ++cols;
1011                     ++cell;
1012                     break;
1013                 case '\n':
1014                     paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1015                     while(cols++ < maxCols)
1016                         ++cell;
1017                     cols = 0;
1018                     break;
1019                 }
1020                 ++p;
1021                 op = p;
1022             }
1023             // check for the last cell if there is no trailing '\n'
1024             if ((cell < cells) && (op < len))
1025                 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1026         } else {
1027             // so that the clipboard is used and it goes on to default
1028             // and executes LFUN_PASTESELECTION in insettext!
1029             delete paste_tabular;
1030             paste_tabular = 0;
1031         }
1032     }
1033     case LFUN_PASTE:
1034         if (hasPasteBuffer()) {
1035             bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1036 #ifndef NEW_INSETS
1037               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1038               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1039 #else
1040               bv->text->cursor.par()->previous,
1041               bv->text->cursor.par()->next
1042 #endif
1043                 );
1044             pasteSelection(bv);
1045             UpdateLocal(bv, INIT, true);
1046             break;
1047         }
1048     // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1049     default:
1050         // we try to activate the actual inset and put this event down to
1051         // the insets dispatch function.
1052         result = UNDISPATCHED;
1053         if (the_locking_inset)
1054             break;
1055         if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
1056             result=the_locking_inset->LocalDispatch(bv, action, arg);
1057             if (result == DISPATCHED_NOUPDATE)
1058                 return result;
1059             else if (result == DISPATCHED) {
1060                 the_locking_inset->ToggleInsetCursor(bv);
1061                 UpdateLocal(bv, CELL, false);
1062                 the_locking_inset->ToggleInsetCursor(bv);
1063                 return result;
1064             } else {
1065                 result = UNDISPATCHED;
1066             }
1067         }
1068         break;
1069     }
1070     if (result!=FINISHED) {
1071         if (!the_locking_inset) {
1072             ShowInsetCursor(bv);
1073         }
1074     } else
1075         bv->unlockInset(this);
1076     return result;
1077 }
1078
1079
1080 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1081                         bool fragile, bool fp) const
1082 {
1083     return tabular->Latex(buf, os, fragile, fp);
1084 }
1085
1086
1087 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1088 {
1089     // This should be changed to a real ascii export
1090     return tabular->Ascii(buf, os);
1091 }
1092
1093
1094 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1095 {
1096     return 0;
1097 }
1098
1099
1100 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1101 {
1102     return tabular->DocBook(buf,os);
1103 }
1104
1105
1106 void InsetTabular::Validate(LaTeXFeatures & features) const
1107 {
1108     tabular->Validate(features);
1109 }
1110
1111
1112 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1113                                                  LyXFont const & font,
1114                                                  bool reinit) const
1115 {
1116     int cell = -1;
1117     int maxAsc = 0;
1118     int maxDesc = 0;
1119     InsetText * inset;
1120     bool changed = false;
1121     
1122     // if we have a locking_inset we should have to check only this cell for
1123     // change so I'll try this to have a boost, but who knows ;)
1124     if ((need_update != INIT) &&
1125         (the_locking_inset == tabular->GetCellInset(actcell))) {
1126         for(int i = 0; i < tabular->columns(); ++i) {
1127             maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1128                          maxAsc);
1129             maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1130                           maxDesc);
1131         }
1132         changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1133         changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1134         changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1135         return changed;
1136     }
1137     for (int i = 0; i < tabular->rows(); ++i) {
1138         maxAsc = 0;
1139         maxDesc = 0;
1140         for (int j= 0; j < tabular->columns(); ++j) {
1141             if (tabular->IsPartOfMultiColumn(i,j))
1142                 continue;
1143             ++cell;
1144             inset = tabular->GetCellInset(cell);
1145             if (!reinit)
1146                 inset->update(bv, font, false);
1147             maxAsc = max(maxAsc, inset->ascent(bv, font));
1148             maxDesc = max(maxDesc, inset->descent(bv, font));
1149             changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1150         }
1151         changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1152         changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1153     }
1154     return changed;
1155 }
1156
1157
1158 void InsetTabular::GetCursorPos(BufferView *,
1159                                 int & x, int & y) const
1160 {
1161     x = cursor.x() - top_x;
1162     y = cursor.y();
1163 }
1164
1165
1166 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1167 {
1168     if (the_locking_inset) {
1169         the_locking_inset->ToggleInsetCursor(bv);
1170         return;
1171     }
1172
1173     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1174
1175     int const asc = lyxfont::maxAscent(font);
1176     int const desc = lyxfont::maxDescent(font);
1177   
1178     if (cursor_visible)
1179         bv->hideLockedInsetCursor();
1180     else
1181         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1182     cursor_visible = !cursor_visible;
1183 }
1184
1185
1186 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1187 {
1188     if (!cursor_visible) {
1189         LyXFont font; // = GetFont(par, cursor.pos);
1190     
1191         int const asc = lyxfont::maxAscent(font);
1192         int const desc = lyxfont::maxDescent(font);
1193         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1194         if (show)
1195             bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1196         cursor_visible = true;
1197     }
1198 }
1199
1200
1201 void InsetTabular::HideInsetCursor(BufferView * bv)
1202 {
1203     if (cursor_visible) {
1204         bv->hideLockedInsetCursor();
1205         cursor_visible = false;
1206     }
1207 //    if (cursor_visible)
1208 //        ToggleInsetCursor(bv);
1209 }
1210
1211
1212 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1213 {
1214     cursor.y(0);
1215     cursor.pos(0);
1216         
1217     actcell = actrow = actcol = 0;
1218     int ly = tabular->GetDescentOfRow(actrow);
1219
1220     // first search the right row
1221     while((ly < y) && (actrow < tabular->rows())) {
1222         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1223             tabular->GetAscentOfRow(actrow + 1) +
1224             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1225                                                                 actcol)));
1226         ++actrow;
1227         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1228     }
1229     actcell = tabular->GetCellNumber(actrow, actcol);
1230
1231     // now search the right column
1232     int lx = tabular->GetWidthOfColumn(actcell) -
1233         tabular->GetAdditionalWidth(actcell);
1234 #if 0
1235 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1236     for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1237         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1238             tabular->GetAdditionalWidth(actcell - 1));
1239 #else
1240     // Jürgen, you should check that this is correct. (Lgb)
1241     for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1242             lx += tabular->GetWidthOfColumn(actcell + 1)
1243                     + tabular->GetAdditionalWidth(actcell);
1244     }
1245     
1246 #endif
1247     cursor.pos(0);
1248     resetPos(bv);
1249     if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1250         cursor.x(lx + top_x - 2);
1251         cursor.pos(1);
1252     } else {
1253         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1254     }
1255     resetPos(bv);
1256 }
1257
1258
1259 int InsetTabular::getCellXPos(int cell) const
1260 {
1261     int c = cell;
1262
1263     for (; !tabular->IsFirstCellInRow(c); --c)
1264         ;
1265     int lx = tabular->GetWidthOfColumn(cell);
1266     for (; c < cell; ++c) {
1267         lx += tabular->GetWidthOfColumn(c);
1268     }
1269     return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1270 }
1271
1272
1273 void InsetTabular::resetPos(BufferView * bv) const
1274 {
1275     if (!locked)
1276         return;
1277     actcol = tabular->column_of_cell(actcell);
1278
1279     int cell = 0;
1280     actrow = 0;
1281     cursor.y(0);
1282     for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1283         if (tabular->IsLastCellInRow(cell)) {
1284             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1285                 tabular->GetAscentOfRow(actrow + 1) +
1286                 tabular->GetAdditionalHeight(cell + 1));
1287             ++actrow;
1288         }
1289     }
1290     static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1291     int new_x = getCellXPos(actcell);
1292     int old_x = cursor.x();
1293     new_x += offset;
1294     cursor.x(new_x);
1295 //    cursor.x(getCellXPos(actcell) + offset);
1296     if (((cursor.x() - offset) > 20) &&
1297         ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1298          (bv->workWidth()-20)))
1299     {
1300         scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1301         UpdateLocal(bv, FULL, false);
1302     } else if ((cursor.x() - offset) < 20) {
1303         scroll(bv, 20 - cursor.x() + offset);
1304         UpdateLocal(bv, FULL, false);
1305     } else if (!cellstart(cursor.pos())) {
1306         LyXFont font(LyXFont::ALL_SANE);
1307         cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1308                 tabular->GetBeginningOfTextInCell(actcell));
1309     } else if (scroll() && (top_x > 20) &&
1310                ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1311         scroll(bv, old_x - cursor.x());
1312     }
1313     if ((!the_locking_inset ||
1314          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1315         (actcell != oldcell)) {
1316             InsetTabular * inset = const_cast<InsetTabular *>(this);
1317             bv->owner()->getDialogs()->updateTabular(inset);
1318             oldcell = actcell;
1319     }
1320 }
1321
1322
1323 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1324 {
1325     if (!cellstart(cursor.pos())) {
1326         if (tabular->IsLastCell(actcell))
1327             return FINISHED;
1328         ++actcell;
1329         cursor.pos(0);
1330     } else if (lock) {
1331         if (ActivateCellInset(bv))
1332             return DISPATCHED;
1333     } else {              // before the inset
1334         cursor.pos(1);
1335     }
1336     resetPos(bv);
1337     return DISPATCHED_NOUPDATE;
1338 }
1339
1340
1341 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1342 {
1343     if (cellstart(cursor.pos()) && !actcell)
1344         return FINISHED;
1345     if (cellstart(cursor.pos())) {
1346         --actcell;
1347         cursor.pos(1);
1348     } else if (lock) {       // behind the inset
1349         cursor.pos(0);
1350         if (ActivateCellInset(bv, 0, 0, 0, true))
1351             return DISPATCHED;
1352     } else {
1353         cursor.pos(0);
1354     }
1355     resetPos(bv);
1356     return DISPATCHED_NOUPDATE;
1357 }
1358
1359
1360 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1361 {
1362     int const ocell = actcell;
1363     actcell = tabular->GetCellAbove(actcell);
1364     if (actcell == ocell) // we moved out of the inset
1365         return FINISHED;
1366     resetPos(bv);
1367     return DISPATCHED_NOUPDATE;
1368 }
1369
1370
1371 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1372 {
1373     int const ocell = actcell;
1374     actcell = tabular->GetCellBelow(actcell);
1375     if (actcell == ocell) // we moved out of the inset
1376         return FINISHED;
1377     resetPos(bv);
1378     return DISPATCHED_NOUPDATE;
1379 }
1380
1381
1382 bool InsetTabular::moveNextCell(BufferView * bv)
1383 {
1384     if (tabular->IsLastCell(actcell))
1385         return false;
1386     ++actcell;
1387     resetPos(bv);
1388     return true;
1389 }
1390
1391
1392 bool InsetTabular::movePrevCell(BufferView * bv)
1393 {
1394     if (!actcell) // first cell
1395         return false;
1396     --actcell;
1397     resetPos(bv);
1398     return true;
1399 }
1400
1401
1402 bool InsetTabular::Delete()
1403 {
1404     return true;
1405 }
1406
1407
1408 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1409 {
1410     if (the_locking_inset)
1411         the_locking_inset->SetFont(bv, font, tall);
1412 }
1413
1414
1415 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1416 {
1417     LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1418
1419     int i = 0;
1420     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1421         string const tmp = tabularFeatures[i].feature;
1422             
1423         if (tmp == what.substr(0, tmp.length())) {
1424         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1425               //tabularFeatures[i].feature.length())) {
1426             action = tabularFeatures[i].action;
1427             break;
1428         }
1429     }
1430     if (action == LyXTabular::LAST_ACTION)
1431         return false;
1432
1433     string const val =
1434             frontStrip(what.substr(tabularFeatures[i].feature.length()));
1435     TabularFeatures(bv, action, val);
1436     return true;
1437 }
1438
1439
1440 void InsetTabular::TabularFeatures(BufferView * bv,
1441                                    LyXTabular::Feature feature,
1442                                    string const & value)
1443 {
1444     int i;
1445     int j;
1446     int sel_col_start;
1447     int sel_col_end;
1448     int sel_row_start;
1449     int sel_row_end;
1450     int setLines = 0;
1451     LyXAlignment setAlign = LYX_ALIGN_LEFT;
1452     LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1453     int lineSet;
1454     bool what;
1455
1456     switch (feature) {
1457       case LyXTabular::M_ALIGN_LEFT:
1458       case LyXTabular::ALIGN_LEFT:
1459           setAlign=LYX_ALIGN_LEFT;
1460           break;
1461       case LyXTabular::M_ALIGN_RIGHT:
1462       case LyXTabular::ALIGN_RIGHT:
1463           setAlign=LYX_ALIGN_RIGHT;
1464           break;
1465       case LyXTabular::M_ALIGN_CENTER:
1466       case LyXTabular::ALIGN_CENTER:
1467           setAlign=LYX_ALIGN_CENTER;
1468           break;
1469       case LyXTabular::M_VALIGN_TOP:
1470       case LyXTabular::VALIGN_TOP:
1471           setVAlign=LyXTabular::LYX_VALIGN_TOP;
1472           break;
1473       case LyXTabular::M_VALIGN_BOTTOM:
1474       case LyXTabular::VALIGN_BOTTOM:
1475           setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1476           break;
1477       case LyXTabular::M_VALIGN_CENTER:
1478       case LyXTabular::VALIGN_CENTER:
1479           setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1480           break;
1481       default:
1482           break;
1483     }
1484     if (hasSelection()) {
1485         sel_col_start = tabular->column_of_cell(sel_cell_start);
1486         sel_col_end = tabular->column_of_cell(sel_cell_end);
1487         if (sel_col_start > sel_col_end) {
1488             sel_col_end = sel_col_start;
1489             sel_col_start = tabular->column_of_cell(sel_cell_end);
1490         } else {
1491             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1492         }
1493         
1494         sel_row_start = tabular->row_of_cell(sel_cell_start);
1495         sel_row_end = tabular->row_of_cell(sel_cell_end);
1496         if (sel_row_start > sel_row_end) {
1497                 //int tmp = sel_row_start;
1498                 //sel_row_start = sel_row_end;
1499                 //sel_row_end = tmp;
1500             swap(sel_row_start, sel_row_end);
1501         }
1502     } else {
1503         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1504         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1505     }
1506     bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1507 #ifndef NEW_INSETS
1508               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1509               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1510 #else
1511               bv->text->cursor.par()->previous,
1512               bv->text->cursor.par()->next
1513 #endif
1514             );
1515
1516     int row = tabular->row_of_cell(actcell);
1517     int column = tabular->column_of_cell(actcell);
1518     bool flag = true;
1519     
1520     switch (feature) {
1521     case LyXTabular::SET_PWIDTH:
1522     {
1523         bool const update = (tabular->GetColumnPWidth(actcell) != value);
1524         tabular->SetColumnPWidth(actcell,value);
1525         if (update) {
1526             for (int i=0; i < tabular->rows(); ++i) {
1527                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1528                     resizeLyXText(bv);
1529             }
1530             UpdateLocal(bv, INIT, true);
1531         }
1532     }
1533     break;
1534     case LyXTabular::SET_MPWIDTH:
1535     {
1536         bool const update = (tabular->GetPWidth(actcell) != value);
1537         tabular->SetMColumnPWidth(actcell,value);
1538         if (update) {
1539             for (int i=0; i < tabular->rows(); ++i) {
1540                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1541                     resizeLyXText(bv);
1542             }
1543             UpdateLocal(bv, INIT, true);
1544         }
1545     }
1546     break;
1547     case LyXTabular::SET_SPECIAL_COLUMN:
1548     case LyXTabular::SET_SPECIAL_MULTI:
1549         tabular->SetAlignSpecial(actcell,value,feature);
1550         break;
1551     case LyXTabular::APPEND_ROW:
1552         // append the row into the tabular
1553         UnlockInsetInInset(bv, the_locking_inset);
1554         tabular->AppendRow(actcell);
1555         UpdateLocal(bv, INIT, true);
1556         break;
1557     case LyXTabular::APPEND_COLUMN:
1558         // append the column into the tabular
1559         tabular->AppendColumn(actcell);
1560         actcell = tabular->GetCellNumber(row, column);
1561         UpdateLocal(bv, INIT, true);
1562         break;
1563     case LyXTabular::DELETE_ROW:
1564         tabular->DeleteRow(tabular->row_of_cell(actcell));
1565         if ((row+1) > tabular->rows())
1566             --row;
1567         actcell = tabular->GetCellNumber(row, column);
1568         clearSelection();
1569         UpdateLocal(bv, INIT, true);
1570         break;
1571     case LyXTabular::DELETE_COLUMN:
1572         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1573         if ((column+1) > tabular->columns())
1574             --column;
1575         actcell = tabular->GetCellNumber(row, column);
1576         clearSelection();
1577         UpdateLocal(bv, INIT, true);
1578         break;
1579     case LyXTabular::M_TOGGLE_LINE_TOP:
1580         flag = false;
1581     case LyXTabular::TOGGLE_LINE_TOP:
1582         lineSet = !tabular->TopLine(actcell, flag);
1583         for (i=sel_row_start; i<=sel_row_end; ++i)
1584             for (j=sel_col_start; j<=sel_col_end; ++j)
1585                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1586         UpdateLocal(bv, INIT, true);
1587         break;
1588     
1589     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1590         flag = false;
1591     case LyXTabular::TOGGLE_LINE_BOTTOM:
1592         lineSet = !tabular->BottomLine(actcell, flag); 
1593         for (i=sel_row_start; i<=sel_row_end; ++i)
1594             for (j=sel_col_start; j<=sel_col_end; ++j)
1595                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1596                                        flag);
1597         UpdateLocal(bv, INIT, true);
1598         break;
1599                 
1600     case LyXTabular::M_TOGGLE_LINE_LEFT:
1601         flag = false;
1602     case LyXTabular::TOGGLE_LINE_LEFT:
1603         lineSet = !tabular->LeftLine(actcell, flag);
1604         for (i=sel_row_start; i<=sel_row_end; ++i)
1605             for (j=sel_col_start; j<=sel_col_end; ++j)
1606                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1607                                      flag);
1608         UpdateLocal(bv, INIT, true);
1609         break;
1610
1611     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1612         flag = false;
1613     case LyXTabular::TOGGLE_LINE_RIGHT:
1614         lineSet = !tabular->RightLine(actcell, flag);
1615         for (i=sel_row_start; i<=sel_row_end; ++i)
1616             for (j=sel_col_start; j<=sel_col_end; ++j)
1617                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1618                                       flag);
1619         UpdateLocal(bv, INIT, true);
1620         break;
1621     case LyXTabular::M_ALIGN_LEFT:
1622     case LyXTabular::M_ALIGN_RIGHT:
1623     case LyXTabular::M_ALIGN_CENTER:
1624         flag = false;
1625     case LyXTabular::ALIGN_LEFT:
1626     case LyXTabular::ALIGN_RIGHT:
1627     case LyXTabular::ALIGN_CENTER:
1628         for (i = sel_row_start; i <= sel_row_end; ++i)
1629             for (j = sel_col_start; j <= sel_col_end; ++j)
1630                 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1631                                       flag);
1632         if (hasSelection())
1633             UpdateLocal(bv, INIT, true);
1634         else
1635             UpdateLocal(bv, CELL, true);
1636         break;
1637     case LyXTabular::M_VALIGN_TOP:
1638     case LyXTabular::M_VALIGN_BOTTOM:
1639     case LyXTabular::M_VALIGN_CENTER:
1640         flag = false;
1641     case LyXTabular::VALIGN_TOP:
1642     case LyXTabular::VALIGN_BOTTOM:
1643     case LyXTabular::VALIGN_CENTER:
1644         for (i = sel_row_start; i <= sel_row_end; ++i)
1645             for (j = sel_col_start; j <= sel_col_end; ++j)
1646                 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1647                                        setVAlign, flag);
1648         if (hasSelection())
1649             UpdateLocal(bv, INIT, true);
1650         else
1651             UpdateLocal(bv, CELL, true);
1652         break;
1653     case LyXTabular::MULTICOLUMN:
1654     {
1655         if (sel_row_start != sel_row_end) {
1656             WriteAlert(_("Impossible Operation!"), 
1657                        _("Multicolumns can only be horizontally."), 
1658                        _("Sorry."));
1659             return;
1660         }
1661         // just multicol for one Single Cell
1662         if (!hasSelection()) {
1663             // check wether we are completly in a multicol
1664             if (tabular->IsMultiColumn(actcell)) {
1665                 tabular->UnsetMultiColumn(actcell);
1666                 UpdateLocal(bv, INIT, true);
1667             } else {
1668                 tabular->SetMultiColumn(actcell, 1);
1669                 UpdateLocal(bv, CELL, true);
1670             }
1671             return;
1672         }
1673         // we have a selection so this means we just add all this
1674         // cells to form a multicolumn cell
1675         int s_start;
1676         int s_end;
1677
1678         if (sel_cell_start > sel_cell_end) {
1679             s_start = sel_cell_end;
1680             s_end = sel_cell_start;
1681         } else {
1682             s_start = sel_cell_start;
1683             s_end = sel_cell_end;
1684         }
1685         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1686         actcell = s_start;
1687         cursor.pos(0);
1688         sel_cell_end = sel_cell_start;
1689         sel_pos_end = sel_pos_start;
1690         UpdateLocal(bv, INIT, true);
1691         break;
1692     }
1693     case LyXTabular::SET_ALL_LINES:
1694         setLines = 1;
1695     case LyXTabular::UNSET_ALL_LINES:
1696         for (i=sel_row_start; i<=sel_row_end; ++i)
1697             for (j=sel_col_start; j<=sel_col_end; ++j)
1698                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1699         UpdateLocal(bv, INIT, true);
1700         break;
1701     case LyXTabular::SET_LONGTABULAR:
1702         tabular->SetLongTabular(true);
1703         UpdateLocal(bv, INIT, true); // because this toggles displayed
1704         break;
1705     case LyXTabular::UNSET_LONGTABULAR:
1706         tabular->SetLongTabular(false);
1707         UpdateLocal(bv, INIT, true); // because this toggles displayed
1708         break;
1709     case LyXTabular::SET_ROTATE_TABULAR:
1710         tabular->SetRotateTabular(true);
1711         break;
1712     case LyXTabular::UNSET_ROTATE_TABULAR:
1713         tabular->SetRotateTabular(false);
1714         break;
1715     case LyXTabular::SET_ROTATE_CELL:
1716         for (i=sel_row_start; i<=sel_row_end; ++i)
1717             for (j=sel_col_start; j<=sel_col_end; ++j)
1718                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1719         break;
1720     case LyXTabular::UNSET_ROTATE_CELL:
1721         for (i = sel_row_start; i <= sel_row_end; ++i)
1722             for (j = sel_col_start; j <= sel_col_end; ++j)
1723                 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1724         break;
1725     case LyXTabular::SET_USEBOX:
1726     {
1727         LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1728         if (val == tabular->GetUsebox(actcell))
1729             val = LyXTabular::BOX_NONE;
1730         for (i = sel_row_start; i <= sel_row_end; ++i)
1731             for (j = sel_col_start; j <= sel_col_end; ++j)
1732                 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1733         break;
1734     }
1735     case LyXTabular::SET_LTFIRSTHEAD:
1736         tabular->SetLTHead(actcell, true);
1737         break;
1738     case LyXTabular::SET_LTHEAD:
1739         tabular->SetLTHead(actcell, false);
1740         break;
1741     case LyXTabular::SET_LTFOOT:
1742         tabular->SetLTFoot(actcell, false);
1743         break;
1744     case LyXTabular::SET_LTLASTFOOT:
1745         tabular->SetLTFoot(actcell, true);
1746         break;
1747     case LyXTabular::SET_LTNEWPAGE:
1748         what = !tabular->GetLTNewPage(actcell);
1749         tabular->SetLTNewPage(actcell, what);
1750         break;
1751     // dummy stuff just to avoid warnings
1752     case LyXTabular::LAST_ACTION:
1753         break;
1754     }
1755 }
1756
1757
1758 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1759                                      bool behind)
1760 {
1761     // the cursor.pos has to be before the inset so if it isn't now just
1762     // reset the curor pos first!
1763     if (!cellstart(cursor.pos())) {
1764         cursor.pos(0);
1765         resetPos(bv);
1766     }
1767     UpdatableInset * inset =
1768         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1769     LyXFont font(LyXFont::ALL_SANE);
1770     if (behind) {
1771         x = inset->x() + inset->width(bv, font);
1772         y = inset->descent(bv, font);
1773     }
1774     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1775     inset_y = cursor.y();
1776     inset->Edit(bv, x - inset_x, y - inset_y, button);
1777     if (!the_locking_inset)
1778         return false;
1779     UpdateLocal(bv, CELL, false);
1780     return (the_locking_inset != 0);
1781 }
1782
1783
1784 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1785 {
1786     InsetText * inset = tabular->GetCellInset(actcell);
1787     int const x1 = x + top_x;
1788
1789     if (!cellstart(cursor.pos())) {
1790         return ((x + top_x) < cursor.x() &&
1791                 (x + top_x) > (cursor.x() - inset->width(bv,
1792                                                       LyXFont(LyXFont::ALL_SANE))));
1793     } else {
1794         int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1795         return (x1 > x2 &&
1796                 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1797     }
1798 }
1799
1800
1801 // This returns paperWidth() if the cell-width is unlimited or the width
1802 // in pixels if we have a pwidth for this cell.
1803 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1804 {
1805     string const s = tabular->GetPWidth(cell);
1806
1807     if (s.empty())
1808         return -1;
1809     return VSpace(s).inPixels(0, 0);
1810 }
1811
1812
1813 int InsetTabular::getMaxWidth(Painter & pain,
1814                               UpdatableInset const * inset) const
1815 {
1816     int const n = tabular->GetNumberOfCells();
1817     int cell = 0;
1818     for (; cell < n; ++cell) {
1819         if (tabular->GetCellInset(cell) == inset)
1820             break;
1821     }
1822     if (cell >= n)
1823         return -1;
1824     int w = GetMaxWidthOfCell(pain, cell);
1825     if (w > 0)
1826         // because the inset then subtracts it's top_x and owner->x()
1827         w += (inset->x() - top_x);
1828     return w;
1829 }
1830
1831
1832 void InsetTabular::resizeLyXText(BufferView *) const
1833 {
1834     need_update = FULL;
1835 }
1836
1837
1838 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1839 {
1840     if (the_locking_inset)
1841         return the_locking_inset->getLyXText(bv);
1842     return Inset::getLyXText(bv);
1843 }
1844
1845
1846 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1847 {
1848     if (the_locking_inset) {
1849         InsetTabular * i = static_cast<InsetTabular *>
1850             (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1851         if (i) {
1852             i->OpenLayoutDialog(bv);
1853             return;
1854         }
1855     }
1856     bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1857 }
1858
1859 //
1860 // functions returns:
1861 // 0 ... disabled
1862 // 1 ... enabled
1863 // 2 ... toggled on
1864 // 3 ... toggled off
1865 //
1866 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1867 {
1868     int action = LyXTabular::LAST_ACTION;
1869     LyXFunc::func_status status = LyXFunc::OK;
1870     
1871     int i = 0;
1872     for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1873         string const tmp = tabularFeatures[i].feature;
1874         if (tmp == what.substr(0, tmp.length())) {                  
1875         //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1876         //   tabularFeatures[i].feature.length())) {
1877             action = tabularFeatures[i].action;
1878             break;
1879         }
1880     }
1881     if (action == LyXTabular::LAST_ACTION)
1882         return LyXFunc::Unknown;
1883
1884     string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1885
1886     int sel_row_start, sel_row_end;
1887     int dummy;
1888     bool flag = true;
1889
1890     if (hasSelection()) {
1891         sel_row_start = tabular->row_of_cell(sel_cell_start);
1892         sel_row_end = tabular->row_of_cell(sel_cell_end);
1893         if (sel_row_start > sel_row_end) {
1894                 //int tmp = sel_row_start;
1895                 //sel_row_start = sel_row_end;
1896                 //sel_row_end = tmp;
1897             swap(sel_row_start, sel_row_end);
1898         }
1899     } else {
1900         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1901     }
1902
1903     switch (action) {
1904     case LyXTabular::SET_PWIDTH:
1905     case LyXTabular::SET_MPWIDTH:
1906     case LyXTabular::SET_SPECIAL_COLUMN:
1907     case LyXTabular::SET_SPECIAL_MULTI:
1908         status |= LyXFunc::Disabled;
1909         return status;
1910
1911     case LyXTabular::APPEND_ROW:
1912     case LyXTabular::APPEND_COLUMN:
1913     case LyXTabular::DELETE_ROW:
1914     case LyXTabular::DELETE_COLUMN:
1915     case LyXTabular::SET_ALL_LINES:
1916     case LyXTabular::UNSET_ALL_LINES:
1917         status |= LyXFunc::OK;
1918         return status;
1919
1920     case LyXTabular::MULTICOLUMN:
1921         if (tabular->IsMultiColumn(actcell))
1922             status |= LyXFunc::ToggleOn;
1923         else
1924             status |= LyXFunc::ToggleOff;
1925         break;
1926     case LyXTabular::M_TOGGLE_LINE_TOP:
1927         flag = false;
1928     case LyXTabular::TOGGLE_LINE_TOP:
1929         if (tabular->TopLine(actcell, flag))
1930             status |= LyXFunc::ToggleOn;
1931         else
1932             status |= LyXFunc::ToggleOff;
1933         break;
1934     case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1935         flag = false;
1936     case LyXTabular::TOGGLE_LINE_BOTTOM:
1937         if (tabular->BottomLine(actcell, flag))
1938             status |= LyXFunc::ToggleOn;
1939         else
1940             status |= LyXFunc::ToggleOff;
1941         break;
1942     case LyXTabular::M_TOGGLE_LINE_LEFT:
1943         flag = false;
1944     case LyXTabular::TOGGLE_LINE_LEFT:
1945         if (tabular->LeftLine(actcell, flag))
1946             status |= LyXFunc::ToggleOn;
1947         else
1948             status |= LyXFunc::ToggleOff;
1949         break;
1950     case LyXTabular::M_TOGGLE_LINE_RIGHT:
1951         flag = false;
1952     case LyXTabular::TOGGLE_LINE_RIGHT:
1953         if (tabular->RightLine(actcell, flag))
1954             status |= LyXFunc::ToggleOn;
1955         else
1956             status |= LyXFunc::ToggleOff;
1957         break;
1958     case LyXTabular::M_ALIGN_LEFT:
1959         flag = false;
1960     case LyXTabular::ALIGN_LEFT:
1961         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1962             status |= LyXFunc::ToggleOn;
1963         else
1964             status |= LyXFunc::ToggleOff;
1965         break;
1966     case LyXTabular::M_ALIGN_RIGHT:
1967         flag = false;
1968     case LyXTabular::ALIGN_RIGHT:
1969         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1970             status |= LyXFunc::ToggleOn;
1971         else
1972             status |= LyXFunc::ToggleOff;
1973         break;
1974     case LyXTabular::M_ALIGN_CENTER:
1975         flag = false;
1976     case LyXTabular::ALIGN_CENTER:
1977         if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1978             status |= LyXFunc::ToggleOn;
1979         else
1980             status |= LyXFunc::ToggleOff;
1981         break;
1982     case LyXTabular::M_VALIGN_TOP:
1983         flag = false;
1984     case LyXTabular::VALIGN_TOP:
1985         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1986             status |= LyXFunc::ToggleOn;
1987         else
1988             status |= LyXFunc::ToggleOff;
1989         break;
1990     case LyXTabular::M_VALIGN_BOTTOM:
1991         flag = false;
1992     case LyXTabular::VALIGN_BOTTOM:
1993         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1994             status |= LyXFunc::ToggleOn;
1995         else
1996             status |= LyXFunc::ToggleOff;
1997         break;
1998     case LyXTabular::M_VALIGN_CENTER:
1999         flag = false;
2000     case LyXTabular::VALIGN_CENTER:
2001         if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2002             status |= LyXFunc::ToggleOn;
2003         else
2004             status |= LyXFunc::ToggleOff;
2005         break;
2006     case LyXTabular::SET_LONGTABULAR:
2007         if (tabular->IsLongTabular())
2008             status |= LyXFunc::ToggleOn;
2009         else
2010             status |= LyXFunc::ToggleOff;
2011         break;
2012     case LyXTabular::UNSET_LONGTABULAR:
2013         if (!tabular->IsLongTabular())
2014             status |= LyXFunc::ToggleOn;
2015         else
2016             status |= LyXFunc::ToggleOff;
2017         break;
2018     case LyXTabular::SET_ROTATE_TABULAR:
2019         if (tabular->GetRotateTabular())
2020             status |= LyXFunc::ToggleOn;
2021         else
2022             status |= LyXFunc::ToggleOff;
2023         break;
2024     case LyXTabular::UNSET_ROTATE_TABULAR:
2025         if (!tabular->GetRotateTabular())
2026             status |= LyXFunc::ToggleOn;
2027         else
2028             status |= LyXFunc::ToggleOff;
2029         break;
2030     case LyXTabular::SET_ROTATE_CELL:
2031         if (tabular->GetRotateCell(actcell))
2032             status |= LyXFunc::ToggleOn;
2033         else
2034             status |= LyXFunc::ToggleOff;
2035         break;
2036     case LyXTabular::UNSET_ROTATE_CELL:
2037         if (!tabular->GetRotateCell(actcell))
2038             status |= LyXFunc::ToggleOn;
2039         else
2040             status |= LyXFunc::ToggleOff;
2041         break;
2042     case LyXTabular::SET_USEBOX:
2043         if (strToInt(argument) == tabular->GetUsebox(actcell))
2044             status |= LyXFunc::ToggleOn;
2045         else
2046             status |= LyXFunc::ToggleOff;
2047         break;
2048     case LyXTabular::SET_LTFIRSTHEAD:
2049         if (tabular->GetRowOfLTHead(actcell, dummy))
2050             status |= LyXFunc::ToggleOn;
2051         else
2052             status |= LyXFunc::ToggleOff;
2053         break;
2054     case LyXTabular::SET_LTHEAD:
2055         if (tabular->GetRowOfLTHead(actcell, dummy))
2056             status |= LyXFunc::ToggleOn;
2057         else
2058             status |= LyXFunc::ToggleOff;
2059         break;
2060     case LyXTabular::SET_LTFOOT:
2061         if (tabular->GetRowOfLTFoot(actcell, dummy))
2062             status |= LyXFunc::ToggleOn;
2063         else
2064             status |= LyXFunc::ToggleOff;
2065         break;
2066     case LyXTabular::SET_LTLASTFOOT:
2067         if (tabular->GetRowOfLTFoot(actcell, dummy))
2068             status |= LyXFunc::ToggleOn;
2069         else
2070             status |= LyXFunc::ToggleOff;
2071         break;
2072     case LyXTabular::SET_LTNEWPAGE:
2073         if (tabular->GetLTNewPage(actcell))
2074             status |= LyXFunc::ToggleOn;
2075         else
2076             status |= LyXFunc::ToggleOff;
2077         break;
2078     default:
2079         status = LyXFunc::Disabled;
2080         break;
2081     }
2082     return status;
2083 }
2084
2085
2086 bool InsetTabular::copySelection(BufferView * bv)
2087 {
2088     if (!hasSelection())
2089         return false;
2090     delete paste_tabular;
2091
2092     int sel_col_start, sel_col_end;
2093     int sel_row_start, sel_row_end;
2094
2095     sel_col_start = tabular->column_of_cell(sel_cell_start);
2096     sel_col_end = tabular->column_of_cell(sel_cell_end);
2097     if (sel_col_start > sel_col_end) {
2098         sel_col_start = sel_col_end;
2099         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2100     } else {
2101         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2102     }
2103     int columns = sel_col_end - sel_col_start + 1;
2104
2105     sel_row_start = tabular->row_of_cell(sel_cell_start);
2106     sel_row_end = tabular->row_of_cell(sel_cell_end);
2107     if (sel_row_start > sel_row_end) {
2108             //int tmp tmp = sel_row_start;
2109             //sel_row_start = sel_row_end;
2110             //sel_row_end = tmp;
2111         swap(sel_row_start, sel_row_end);
2112     }
2113     int rows = sel_row_end - sel_row_start + 1;
2114
2115     paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2116     int i;
2117     for (i=0; i < sel_row_start; ++i)
2118         paste_tabular->DeleteRow(0);
2119     while(paste_tabular->rows() > rows)
2120         paste_tabular->DeleteRow(rows);
2121     paste_tabular->SetTopLine(0, true, true);
2122     paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2123                                  true, true);
2124     for (i=0; i < sel_col_start; ++i)
2125         paste_tabular->DeleteColumn(0);
2126     while(paste_tabular->columns() > columns)
2127         paste_tabular->DeleteColumn(columns);
2128     paste_tabular->SetLeftLine(0, true, true);
2129     paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2130
2131     ostringstream sstr;
2132     paste_tabular->Ascii(bv->buffer(), sstr);
2133     bv->stuffClipboard(sstr.str().c_str());
2134     return true;
2135 }
2136
2137
2138 bool InsetTabular::pasteSelection(BufferView * bv)
2139 {
2140     if (!paste_tabular)
2141         return false;
2142
2143     for (int r1 = 0, r2 = actrow;
2144          (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2145          ++r1, ++r2)
2146     {
2147         for(int c1 = 0, c2 = actcol;
2148             (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2149             ++c1, ++c2)
2150         {
2151             if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2152                 tabular->IsPartOfMultiColumn(r2,c2))
2153                 continue;
2154             if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2155                 --c2;
2156                 continue;
2157             }
2158             if (tabular->IsPartOfMultiColumn(r2,c2)) {
2159                 --c1;
2160                 continue;
2161             }
2162             int n1 = paste_tabular->GetCellNumber(r1, c1);
2163             int n2 = tabular->GetCellNumber(r2, c2);
2164             *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2165             tabular->GetCellInset(n2)->setOwner(this);
2166             tabular->GetCellInset(n2)->deleteLyXText(bv);
2167         }
2168     }
2169     return true;
2170 }
2171
2172
2173 bool InsetTabular::cutSelection()
2174 {
2175     if (!hasSelection())
2176         return false;
2177
2178     int sel_col_start, sel_col_end;
2179     int sel_row_start, sel_row_end;
2180
2181     sel_col_start = tabular->column_of_cell(sel_cell_start);
2182     sel_col_end = tabular->column_of_cell(sel_cell_end);
2183     if (sel_col_start > sel_col_end) {
2184         sel_col_start = sel_col_end;
2185         sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2186     } else {
2187         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2188     }
2189     sel_row_start = tabular->row_of_cell(sel_cell_start);
2190     sel_row_end = tabular->row_of_cell(sel_cell_end);
2191     if (sel_row_start > sel_row_end) {
2192             //int tmp = sel_row_start;
2193             //sel_row_start = sel_row_end;
2194             //sel_row_end = tmp;
2195         swap(sel_row_start, sel_row_end);
2196     }
2197     if (sel_cell_start > sel_cell_end) {
2198             //int tmp = sel_cell_start;
2199             //sel_cell_start = sel_cell_end;
2200             //sel_cell_end = tmp;
2201         swap(sel_cell_start, sel_cell_end);
2202     }
2203     for (int i = sel_row_start; i <= sel_row_end; ++i) {
2204         for (int j = sel_col_start; j <= sel_col_end; ++j) {
2205             tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2206         }
2207     }
2208     return true;
2209 }
2210