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