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