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