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