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