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