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