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