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