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