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