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