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