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