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