]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
fix column width resize
[features.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14 #include "insettext.h"
15
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "lfuns.h"
19 #include "debug.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "LaTeXFeatures.h"
25 #include "Lsstream.h"
26 #include "lyx_cb.h"
27 #include "lyxfunc.h"
28 #include "lyxlength.h"
29 #include "lyxlex.h"
30 #include "lyxtext.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "metricsinfo.h"
35
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Painter.h"
41
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
44
45 #include <fstream>
46 #include <algorithm>
47 #include <cstdlib>
48 #include <map>
49 //#include <signal.h>
50
51
52 using 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::metrics(MetricsInfo &,
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                         // We need this otherwise we won't resize
1758                         // the insettext of the active cell (if any)
1759                         // until later (see InsetText::do_resize)
1760                         unlockInsetInInset(bv, the_locking_inset);
1761
1762                         int cell;
1763                         for (int i = 0; i < tabular->rows(); ++i) {
1764                                 cell = tabular->GetCellNumber(i,column);
1765                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1766                         }
1767                         updateLocal(bv, INIT);
1768                 }
1769
1770                 if (vallen.zero()
1771                     && tabular->GetAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1772                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1773                 else if (!vallen.zero()
1774                          && tabular->GetAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1775                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1776         }
1777         break;
1778         case LyXTabular::SET_MPWIDTH:
1779         {
1780                 LyXLength const vallen(value);
1781                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1782
1783                 bool const update = (tmplen != vallen);
1784                 tabular->SetMColumnPWidth(actcell, vallen);
1785                 if (update) {
1786                         // We need this otherwise we won't resize
1787                         // the insettext of the active cell (if any)
1788                         // until later (see InsetText::do_resize)
1789                         unlockInsetInInset(bv, the_locking_inset);
1790
1791                         for (int i = 0; i < tabular->rows(); ++i) {
1792                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1793                                         resizeLyXText(bv);
1794                         }
1795                         updateLocal(bv, INIT);
1796                 }
1797         }
1798         break;
1799         case LyXTabular::SET_SPECIAL_COLUMN:
1800         case LyXTabular::SET_SPECIAL_MULTI:
1801                 tabular->SetAlignSpecial(actcell,value,feature);
1802                 updateLocal(bv, FULL);
1803                 break;
1804         case LyXTabular::APPEND_ROW:
1805                 // append the row into the tabular
1806                 unlockInsetInInset(bv, the_locking_inset);
1807                 tabular->AppendRow(bv->buffer()->params, actcell);
1808                 updateLocal(bv, INIT);
1809                 break;
1810         case LyXTabular::APPEND_COLUMN:
1811                 // append the column into the tabular
1812                 unlockInsetInInset(bv, the_locking_inset);
1813                 tabular->AppendColumn(bv->buffer()->params, actcell);
1814                 actcell = tabular->GetCellNumber(row, column);
1815                 updateLocal(bv, INIT);
1816                 break;
1817         case LyXTabular::DELETE_ROW:
1818                 unlockInsetInInset(bv, the_locking_inset);
1819                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1820                         tabular->DeleteRow(sel_row_start);
1821                 }
1822                 if (sel_row_start >= tabular->rows())
1823                         --sel_row_start;
1824                 actcell = tabular->GetCellNumber(sel_row_start, column);
1825                 clearSelection();
1826                 updateLocal(bv, INIT);
1827                 break;
1828         case LyXTabular::DELETE_COLUMN:
1829                 unlockInsetInInset(bv, the_locking_inset);
1830                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1831                         tabular->DeleteColumn(sel_col_start);
1832                 }
1833                 if (sel_col_start >= tabular->columns())
1834                         --sel_col_start;
1835                 actcell = tabular->GetCellNumber(row, sel_col_start);
1836                 clearSelection();
1837                 updateLocal(bv, INIT);
1838                 break;
1839         case LyXTabular::M_TOGGLE_LINE_TOP:
1840                 flag = false;
1841         case LyXTabular::TOGGLE_LINE_TOP:
1842         {
1843                 bool lineSet = !tabular->TopLine(actcell, flag);
1844                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1845                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1846                                 tabular->SetTopLine(
1847                                         tabular->GetCellNumber(i, j),
1848                                         lineSet, flag);
1849                 updateLocal(bv, INIT);
1850                 break;
1851         }
1852
1853         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1854                 flag = false;
1855         case LyXTabular::TOGGLE_LINE_BOTTOM:
1856         {
1857                 bool lineSet = !tabular->BottomLine(actcell, flag);
1858                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1859                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1860                                 tabular->SetBottomLine(
1861                                         tabular->GetCellNumber(i, j),
1862                                         lineSet,
1863                                         flag);
1864                 updateLocal(bv, INIT);
1865                 break;
1866         }
1867
1868         case LyXTabular::M_TOGGLE_LINE_LEFT:
1869                 flag = false;
1870         case LyXTabular::TOGGLE_LINE_LEFT:
1871         {
1872                 bool lineSet = !tabular->LeftLine(actcell, flag);
1873                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1874                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1875                                 tabular->SetLeftLine(
1876                                         tabular->GetCellNumber(i,j),
1877                                         lineSet,
1878                                         flag);
1879                 updateLocal(bv, INIT);
1880                 break;
1881         }
1882
1883         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1884                 flag = false;
1885         case LyXTabular::TOGGLE_LINE_RIGHT:
1886         {
1887                 bool lineSet = !tabular->RightLine(actcell, flag);
1888                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1889                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1890                                 tabular->SetRightLine(
1891                                         tabular->GetCellNumber(i,j),
1892                                         lineSet,
1893                                         flag);
1894                 updateLocal(bv, INIT);
1895                 break;
1896         }
1897
1898         case LyXTabular::M_ALIGN_LEFT:
1899         case LyXTabular::M_ALIGN_RIGHT:
1900         case LyXTabular::M_ALIGN_CENTER:
1901                 flag = false;
1902         case LyXTabular::ALIGN_LEFT:
1903         case LyXTabular::ALIGN_RIGHT:
1904         case LyXTabular::ALIGN_CENTER:
1905         case LyXTabular::ALIGN_BLOCK:
1906                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1907                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1908                                 tabular->SetAlignment(
1909                                         tabular->GetCellNumber(i, j),
1910                                         setAlign,
1911                                         flag);
1912                 updateLocal(bv, INIT);
1913                 break;
1914         case LyXTabular::M_VALIGN_TOP:
1915         case LyXTabular::M_VALIGN_BOTTOM:
1916         case LyXTabular::M_VALIGN_CENTER:
1917                 flag = false;
1918         case LyXTabular::VALIGN_TOP:
1919         case LyXTabular::VALIGN_BOTTOM:
1920         case LyXTabular::VALIGN_CENTER:
1921                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1922                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1923                                 tabular->SetVAlignment(
1924                                         tabular->GetCellNumber(i, j),
1925                                         setVAlign, flag);
1926                 updateLocal(bv, INIT);
1927                 break;
1928         case LyXTabular::MULTICOLUMN:
1929         {
1930                 if (sel_row_start != sel_row_end) {
1931 #ifdef WITH_WARNINGS
1932 #warning Need I say it ? This is horrible.
1933 #endif
1934                         Alert::error(_("Error setting multicolumn"),
1935                                    _("You cannot set multicolumn vertically."));
1936                         return;
1937                 }
1938                 // just multicol for one Single Cell
1939                 if (!hasSelection()) {
1940                         // check wether we are completly in a multicol
1941                         if (tabular->IsMultiColumn(actcell)) {
1942                                 tabular->UnsetMultiColumn(actcell);
1943                                 updateLocal(bv, INIT);
1944                         } else {
1945                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
1946                                 updateLocal(bv, CELL);
1947                         }
1948                         break;
1949                 }
1950                 // we have a selection so this means we just add all this
1951                 // cells to form a multicolumn cell
1952                 int s_start;
1953                 int s_end;
1954
1955                 if (sel_cell_start > sel_cell_end) {
1956                         s_start = sel_cell_end;
1957                         s_end = sel_cell_start;
1958                 } else {
1959                         s_start = sel_cell_start;
1960                         s_end = sel_cell_end;
1961                 }
1962                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1963                 actcell = s_start;
1964                 clearSelection();
1965                 updateLocal(bv, INIT);
1966                 break;
1967         }
1968         case LyXTabular::SET_ALL_LINES:
1969                 setLines = true;
1970         case LyXTabular::UNSET_ALL_LINES:
1971                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1972                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1973                                 tabular->SetAllLines(
1974                                         tabular->GetCellNumber(i,j), setLines);
1975                 updateLocal(bv, INIT);
1976                 break;
1977         case LyXTabular::SET_LONGTABULAR:
1978                 tabular->SetLongTabular(true);
1979                 updateLocal(bv, INIT); // because this toggles displayed
1980                 break;
1981         case LyXTabular::UNSET_LONGTABULAR:
1982                 tabular->SetLongTabular(false);
1983                 updateLocal(bv, INIT); // because this toggles displayed
1984                 break;
1985         case LyXTabular::SET_ROTATE_TABULAR:
1986                 tabular->SetRotateTabular(true);
1987                 break;
1988         case LyXTabular::UNSET_ROTATE_TABULAR:
1989                 tabular->SetRotateTabular(false);
1990                 break;
1991         case LyXTabular::SET_ROTATE_CELL:
1992                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1993                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1994                                 tabular->SetRotateCell(
1995                                         tabular->GetCellNumber(i, j),
1996                                         true);
1997                 break;
1998         case LyXTabular::UNSET_ROTATE_CELL:
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->SetRotateCell(
2002                                         tabular->GetCellNumber(i, j), false);
2003                 break;
2004         case LyXTabular::SET_USEBOX:
2005         {
2006                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2007                 if (val == tabular->GetUsebox(actcell))
2008                         val = LyXTabular::BOX_NONE;
2009                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2010                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2011                                 tabular->SetUsebox(
2012                                         tabular->GetCellNumber(i, j), val);
2013                 break;
2014         }
2015         case LyXTabular::UNSET_LTFIRSTHEAD:
2016                 flag = false;
2017         case LyXTabular::SET_LTFIRSTHEAD:
2018                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2019                 checkLongtableSpecial(ltt, value, flag);
2020                 tabular->SetLTHead(row, flag, ltt, true);
2021                 break;
2022         case LyXTabular::UNSET_LTHEAD:
2023                 flag = false;
2024         case LyXTabular::SET_LTHEAD:
2025                 (void)tabular->GetRowOfLTHead(row, ltt);
2026                 checkLongtableSpecial(ltt, value, flag);
2027                 tabular->SetLTHead(row, flag, ltt, false);
2028                 break;
2029         case LyXTabular::UNSET_LTFOOT:
2030                 flag = false;
2031         case LyXTabular::SET_LTFOOT:
2032                 (void)tabular->GetRowOfLTFoot(row, ltt);
2033                 checkLongtableSpecial(ltt, value, flag);
2034                 tabular->SetLTFoot(row, flag, ltt, false);
2035                 break;
2036         case LyXTabular::UNSET_LTLASTFOOT:
2037                 flag = false;
2038         case LyXTabular::SET_LTLASTFOOT:
2039                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2040                 checkLongtableSpecial(ltt, value, flag);
2041                 tabular->SetLTFoot(row, flag, ltt, true);
2042                 break;
2043         case LyXTabular::SET_LTNEWPAGE:
2044         {
2045                 bool what = !tabular->GetLTNewPage(row);
2046                 tabular->SetLTNewPage(row, what);
2047                 break;
2048         }
2049         // dummy stuff just to avoid warnings
2050         case LyXTabular::LAST_ACTION:
2051                 break;
2052         }
2053
2054         InsetTabularMailer mailer(*this);
2055         mailer.updateDialog(bv);
2056 }
2057
2058
2059 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
2060                                      bool behind)
2061 {
2062         UpdatableInset * inset =
2063                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2064         LyXFont font(LyXFont::ALL_SANE);
2065         if (behind) {
2066                 x = inset->x() + inset->width(bv, font);
2067                 y = inset->descent(bv, font);
2068         }
2069         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2070         //inset_y = cursor.y();
2071         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x,  y, button));
2072         if (!the_locking_inset)
2073                 return false;
2074         updateLocal(bv, CELL);
2075         return (the_locking_inset != 0);
2076 }
2077
2078
2079 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2080                                         mouse_button::state button)
2081 {
2082         inset_x = cursor_.x()
2083                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2084         inset_y = cursor_.y();
2085         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2086 }
2087
2088
2089 bool InsetTabular::insetHit(BufferView *, int x, int) const
2090 {
2091         return (x + top_x)
2092                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2093 }
2094
2095
2096 // This returns paperWidth() if the cell-width is unlimited or the width
2097 // in pixels if we have a pwidth for this cell.
2098 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2099 {
2100         LyXLength const len = tabular->GetPWidth(cell);
2101
2102         if (len.zero())
2103                 return -1;
2104         return len.inPixels(latexTextWidth(bv));
2105 }
2106
2107
2108 int InsetTabular::getMaxWidth(BufferView * bv,
2109                               UpdatableInset const * inset) const
2110 {
2111         int cell = tabular->GetCellFromInset(inset, actcell);
2112
2113         if (cell == -1) {
2114                 lyxerr << "Own inset not found, shouldn't really happen!"
2115                        << endl;
2116                 return -1;
2117         }
2118
2119         int w = getMaxWidthOfCell(bv, cell);
2120         if (w > 0) {
2121                 // because the inset then subtracts it's top_x and owner->x()
2122                 w += (inset->x() - top_x);
2123         }
2124
2125         return w;
2126 }
2127
2128
2129 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2130 {
2131         resizeLyXText(bv, recursive);
2132 }
2133
2134
2135 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2136 {
2137         if (force) {
2138                 for(int i = 0; i < tabular->rows(); ++i) {
2139                         for(int j = 0; j < tabular->columns(); ++j) {
2140                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2141                         }
2142                 }
2143         }
2144         need_update = FULL;
2145 }
2146
2147
2148 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2149                                    bool const recursive) const
2150 {
2151         if (the_locking_inset)
2152                 return the_locking_inset->getLyXText(bv, recursive);
2153 #if 0
2154         // if we're locked lock the actual insettext and return it's LyXText!!!
2155         if (locked) {
2156                 UpdatableInset * inset =
2157                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2158                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2159                 return the_locking_inset->getLyXText(bv, recursive);
2160         }
2161 #endif
2162         return Inset::getLyXText(bv, recursive);
2163 }
2164
2165
2166 bool InsetTabular::showInsetDialog(BufferView * bv) const
2167 {
2168         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv)) {
2169                 InsetTabular * tmp = const_cast<InsetTabular *>(this);
2170                 InsetTabularMailer mailer(*tmp);
2171                 mailer.showDialog(bv);
2172         }
2173         return true;
2174 }
2175
2176
2177 void InsetTabular::openLayoutDialog(BufferView * bv) const
2178 {
2179         if (the_locking_inset) {
2180                 InsetTabular * i = static_cast<InsetTabular *>
2181                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2182                 if (i) {
2183                         i->openLayoutDialog(bv);
2184                         return;
2185                 }
2186         }
2187         InsetTabular * tmp = const_cast<InsetTabular *>(this);
2188         InsetTabularMailer mailer(*tmp);
2189         mailer.showDialog(bv);
2190 }
2191
2192
2193 //
2194 // function returns an object as defined in func_status.h:
2195 // states OK, Unknown, Disabled, On, Off.
2196 //
2197 FuncStatus InsetTabular::getStatus(string const & what) const
2198 {
2199         int action = LyXTabular::LAST_ACTION;
2200         FuncStatus status;
2201
2202         int i = 0;
2203         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2204                 string const tmp = tabularFeature[i].feature;
2205                 if (tmp == what.substr(0, tmp.length())) {
2206                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2207                         //   tabularFeatures[i].feature.length())) {
2208                         action = tabularFeature[i].action;
2209                         break;
2210                 }
2211         }
2212         if (action == LyXTabular::LAST_ACTION) {
2213                 status.clear();
2214                 return status.unknown(true);
2215         }
2216
2217         string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
2218
2219         int sel_row_start;
2220         int sel_row_end;
2221         int dummy;
2222         LyXTabular::ltType dummyltt;
2223         bool flag = true;
2224
2225         if (hasSelection()) {
2226                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2227         } else {
2228                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2229         }
2230
2231         switch (action) {
2232         case LyXTabular::SET_PWIDTH:
2233         case LyXTabular::SET_MPWIDTH:
2234         case LyXTabular::SET_SPECIAL_COLUMN:
2235         case LyXTabular::SET_SPECIAL_MULTI:
2236         case LyXTabular::APPEND_ROW:
2237         case LyXTabular::APPEND_COLUMN:
2238         case LyXTabular::DELETE_ROW:
2239         case LyXTabular::DELETE_COLUMN:
2240         case LyXTabular::SET_ALL_LINES:
2241         case LyXTabular::UNSET_ALL_LINES:
2242                 return status.clear();
2243
2244         case LyXTabular::MULTICOLUMN:
2245                 status.setOnOff(tabular->IsMultiColumn(actcell));
2246                 break;
2247         case LyXTabular::M_TOGGLE_LINE_TOP:
2248                 flag = false;
2249         case LyXTabular::TOGGLE_LINE_TOP:
2250                 status.setOnOff(tabular->TopLine(actcell, flag));
2251                 break;
2252         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2253                 flag = false;
2254         case LyXTabular::TOGGLE_LINE_BOTTOM:
2255                 status.setOnOff(tabular->BottomLine(actcell, flag));
2256                 break;
2257         case LyXTabular::M_TOGGLE_LINE_LEFT:
2258                 flag = false;
2259         case LyXTabular::TOGGLE_LINE_LEFT:
2260                 status.setOnOff(tabular->LeftLine(actcell, flag));
2261                 break;
2262         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2263                 flag = false;
2264         case LyXTabular::TOGGLE_LINE_RIGHT:
2265                 status.setOnOff(tabular->RightLine(actcell, flag));
2266                 break;
2267         case LyXTabular::M_ALIGN_LEFT:
2268                 flag = false;
2269         case LyXTabular::ALIGN_LEFT:
2270                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2271                 break;
2272         case LyXTabular::M_ALIGN_RIGHT:
2273                 flag = false;
2274         case LyXTabular::ALIGN_RIGHT:
2275                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2276                 break;
2277         case LyXTabular::M_ALIGN_CENTER:
2278                 flag = false;
2279         case LyXTabular::ALIGN_CENTER:
2280                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2281                 break;
2282         case LyXTabular::ALIGN_BLOCK:
2283                 status.disabled(tabular->GetPWidth(actcell).zero());
2284                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2285                 break;
2286         case LyXTabular::M_VALIGN_TOP:
2287                 flag = false;
2288         case LyXTabular::VALIGN_TOP:
2289                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2290                 break;
2291         case LyXTabular::M_VALIGN_BOTTOM:
2292                 flag = false;
2293         case LyXTabular::VALIGN_BOTTOM:
2294                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2295                 break;
2296         case LyXTabular::M_VALIGN_CENTER:
2297                 flag = false;
2298         case LyXTabular::VALIGN_CENTER:
2299                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2300                 break;
2301         case LyXTabular::SET_LONGTABULAR:
2302                 status.setOnOff(tabular->IsLongTabular());
2303                 break;
2304         case LyXTabular::UNSET_LONGTABULAR:
2305                 status.setOnOff(!tabular->IsLongTabular());
2306                 break;
2307         case LyXTabular::SET_ROTATE_TABULAR:
2308                 status.setOnOff(tabular->GetRotateTabular());
2309                 break;
2310         case LyXTabular::UNSET_ROTATE_TABULAR:
2311                 status.setOnOff(!tabular->GetRotateTabular());
2312                 break;
2313         case LyXTabular::SET_ROTATE_CELL:
2314                 status.setOnOff(tabular->GetRotateCell(actcell));
2315                 break;
2316         case LyXTabular::UNSET_ROTATE_CELL:
2317                 status.setOnOff(!tabular->GetRotateCell(actcell));
2318                 break;
2319         case LyXTabular::SET_USEBOX:
2320                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2321                 break;
2322         case LyXTabular::SET_LTFIRSTHEAD:
2323                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2324                 break;
2325         case LyXTabular::SET_LTHEAD:
2326                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2327                 break;
2328         case LyXTabular::SET_LTFOOT:
2329                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2330                 break;
2331         case LyXTabular::SET_LTLASTFOOT:
2332                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2333                 break;
2334         case LyXTabular::SET_LTNEWPAGE:
2335                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2336                 break;
2337         default:
2338                 status.clear();
2339                 status.disabled(true);
2340                 break;
2341         }
2342         return status;
2343 }
2344
2345
2346 vector<string> const InsetTabular::getLabelList() const
2347 {
2348         return tabular->getLabelList();
2349 }
2350
2351
2352 bool InsetTabular::copySelection(BufferView * bv)
2353 {
2354         if (!hasSelection())
2355                 return false;
2356
2357         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2358         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2359         if (sel_col_start > sel_col_end) {
2360                 sel_col_start = sel_col_end;
2361                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2362         } else {
2363                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2364         }
2365         int const columns = sel_col_end - sel_col_start + 1;
2366
2367         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2368         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2369         if (sel_row_start > sel_row_end) {
2370                 swap(sel_row_start, sel_row_end);
2371         }
2372         int const rows = sel_row_end - sel_row_start + 1;
2373
2374         delete paste_tabular;
2375         paste_tabular = new LyXTabular(bv->buffer()->params,
2376                                        this, *tabular); // rows, columns);
2377         for (int i = 0; i < sel_row_start; ++i)
2378                 paste_tabular->DeleteRow(0);
2379         while (paste_tabular->rows() > rows)
2380                 paste_tabular->DeleteRow(rows);
2381         paste_tabular->SetTopLine(0, true, true);
2382         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2383                                      true, true);
2384         for (int i = 0; i < sel_col_start; ++i)
2385                 paste_tabular->DeleteColumn(0);
2386         while (paste_tabular->columns() > columns)
2387                 paste_tabular->DeleteColumn(columns);
2388         paste_tabular->SetLeftLine(0, true, true);
2389         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2390                                     true, true);
2391
2392         ostringstream sstr;
2393         paste_tabular->ascii(bv->buffer(), sstr,
2394                              (int)parOwner()->params().depth(), true, '\t');
2395         bv->stuffClipboard(STRCONV(sstr.str()));
2396         return true;
2397 }
2398
2399
2400 bool InsetTabular::pasteSelection(BufferView * bv)
2401 {
2402         if (!paste_tabular)
2403                 return false;
2404
2405         for (int r1 = 0, r2 = actrow;
2406              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2407              ++r1, ++r2) {
2408                 for(int c1 = 0, c2 = actcol;
2409                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2410                     ++c1, ++c2) {
2411                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2412                             tabular->IsPartOfMultiColumn(r2,c2))
2413                                 continue;
2414                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2415                                 --c2;
2416                                 continue;
2417                         }
2418                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2419                                 --c1;
2420                                 continue;
2421                         }
2422                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2423                         int const n2 = tabular->GetCellNumber(r2, c2);
2424                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2425                         tabular->GetCellInset(n2)->setOwner(this);
2426                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2427                         tabular->GetCellInset(n2)->markNew();
2428                 }
2429         }
2430         return true;
2431 }
2432
2433
2434 bool InsetTabular::cutSelection(BufferParams const & bp)
2435 {
2436         if (!hasSelection())
2437                 return false;
2438
2439         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2440         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2441         if (sel_col_start > sel_col_end) {
2442                 sel_col_start = sel_col_end;
2443                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2444         } else {
2445                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2446         }
2447         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2448         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2449         if (sel_row_start > sel_row_end) {
2450                 swap(sel_row_start, sel_row_end);
2451         }
2452         if (sel_cell_start > sel_cell_end) {
2453                 swap(sel_cell_start, sel_cell_end);
2454         }
2455         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2456                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2457                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear(bp.tracking_changes);
2458                 }
2459         }
2460         return true;
2461 }
2462
2463
2464 bool InsetTabular::isRightToLeft(BufferView * bv)
2465 {
2466         return bv->getParentLanguage(this)->RightToLeft();
2467 }
2468
2469
2470 bool InsetTabular::nodraw() const
2471 {
2472         if (!UpdatableInset::nodraw() && the_locking_inset)
2473                 return the_locking_inset->nodraw();
2474         return UpdatableInset::nodraw();
2475 }
2476
2477
2478 int InsetTabular::scroll(bool recursive) const
2479 {
2480         int sx = UpdatableInset::scroll(false);
2481
2482         if (recursive && the_locking_inset)
2483                 sx += the_locking_inset->scroll(recursive);
2484
2485         return sx;
2486 }
2487
2488
2489 void InsetTabular::getSelection(int & srow, int & erow,
2490                                 int & scol, int & ecol) const
2491 {
2492         int const start = hasSelection() ? sel_cell_start : actcell;
2493         int const end = hasSelection() ? sel_cell_end : actcell;
2494
2495         srow = tabular->row_of_cell(start);
2496         erow = tabular->row_of_cell(end);
2497         if (srow > erow) {
2498                 swap(srow, erow);
2499         }
2500
2501         scol = tabular->column_of_cell(start);
2502         ecol = tabular->column_of_cell(end);
2503         if (scol > ecol) {
2504                 swap(scol, ecol);
2505         } else {
2506                 ecol = tabular->right_column_of_cell(end);
2507         }
2508 }
2509
2510
2511 ParagraphList * InsetTabular::getParagraphs(int i) const
2512 {
2513         return (i < tabular->GetNumberOfCells())
2514                 ? tabular->GetCellInset(i)->getParagraphs(0)
2515                 : 0;
2516 }
2517
2518
2519 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2520 {
2521         if (the_locking_inset)
2522                 return the_locking_inset->cursor(bv);
2523         return Inset::cursor(bv);
2524 }
2525
2526
2527 Inset * InsetTabular::getInsetFromID(int id_arg) const
2528 {
2529         if (id_arg == id())
2530                 return const_cast<InsetTabular *>(this);
2531
2532         Inset * result;
2533         for(int i = 0; i < tabular->rows(); ++i) {
2534                 for(int j = 0; j < tabular->columns(); ++j) {
2535                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2536                                 return result;
2537                 }
2538         }
2539         return 0;
2540 }
2541
2542
2543 WordLangTuple const
2544 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2545 {
2546         nodraw(true);
2547         if (the_locking_inset) {
2548                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2549                 if (!word.word().empty()) {
2550                         nodraw(false);
2551                         return word;
2552                 }
2553                 if (tabular->IsLastCell(actcell)) {
2554                         bv->unlockInset(const_cast<InsetTabular *>(this));
2555                         nodraw(false);
2556                         return WordLangTuple();
2557                 }
2558                 ++actcell;
2559         }
2560         // otherwise we have to lock the next inset and ask for it's selecttion
2561         UpdatableInset * inset =
2562                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2563         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2564         WordLangTuple word(selectNextWordInt(bv, value));
2565         nodraw(false);
2566         if (!word.word().empty())
2567                 resetPos(bv);
2568         return word;
2569 }
2570
2571
2572 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2573 {
2574         // when entering this function the inset should be ALWAYS locked!
2575         lyx::Assert(the_locking_inset);
2576
2577         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2578         if (!word.word().empty())
2579                 return word;
2580
2581         if (tabular->IsLastCell(actcell)) {
2582                 bv->unlockInset(const_cast<InsetTabular *>(this));
2583                 return WordLangTuple();
2584         }
2585
2586         // otherwise we have to lock the next inset and ask for it's selecttion
2587         UpdatableInset * inset =
2588                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2589         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2590         return selectNextWordInt(bv, value);
2591 }
2592
2593
2594 void InsetTabular::selectSelectedWord(BufferView * bv)
2595 {
2596         if (the_locking_inset) {
2597                 the_locking_inset->selectSelectedWord(bv);
2598                 return;
2599         }
2600         return;
2601 }
2602
2603
2604 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2605 {
2606         if (the_locking_inset) {
2607                 the_locking_inset->toggleSelection(bv, kill_selection);
2608         }
2609 }
2610
2611
2612 void InsetTabular::markErased()
2613 {
2614         int cell = 0;
2615
2616         while (!tabular->IsLastCell(cell)) {
2617                 ++cell;
2618                 InsetText * inset = tabular->GetCellInset(cell);
2619                 inset->markErased();
2620         }
2621 }
2622
2623
2624 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2625 {
2626         if (the_locking_inset) {
2627                 if (the_locking_inset->nextChange(bv, length)) {
2628                         updateLocal(bv, CELL);
2629                         return true;
2630                 }
2631                 if (tabular->IsLastCell(actcell))
2632                         return false;
2633                 ++actcell;
2634         }
2635         InsetText * inset = tabular->GetCellInset(actcell);
2636         if (inset->nextChange(bv, length)) {
2637                 updateLocal(bv, FULL);
2638                 return true;
2639         }
2640         while (!tabular->IsLastCell(actcell)) {
2641                 ++actcell;
2642                 inset = tabular->GetCellInset(actcell);
2643                 if (inset->nextChange(bv, length)) {
2644                         updateLocal(bv, FULL);
2645                         return true;
2646                 }
2647         }
2648         return false;
2649 }
2650
2651
2652 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2653                                  bool cs, bool mw)
2654 {
2655         int cell = 0;
2656         if (the_locking_inset) {
2657                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2658                         updateLocal(bv, CELL);
2659                         return true;
2660                 }
2661                 if (tabular->IsLastCell(actcell))
2662                         return false;
2663                 cell = actcell + 1;
2664         }
2665         InsetText * inset = tabular->GetCellInset(cell);
2666         if (inset->searchForward(bv, str, cs, mw)) {
2667                 updateLocal(bv, FULL);
2668                 return true;
2669         }
2670         while (!tabular->IsLastCell(cell)) {
2671                 ++cell;
2672                 inset = tabular->GetCellInset(cell);
2673                 if (inset->searchForward(bv, str, cs, mw)) {
2674                         updateLocal(bv, FULL);
2675                         return true;
2676                 }
2677         }
2678         return false;
2679 }
2680
2681
2682 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2683                                bool cs, bool mw)
2684 {
2685         int cell = tabular->GetNumberOfCells();
2686         if (the_locking_inset) {
2687                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2688                         updateLocal(bv, CELL);
2689                         return true;
2690                 }
2691                 cell = actcell;
2692         }
2693
2694         while (cell) {
2695                 --cell;
2696                 InsetText * inset = tabular->GetCellInset(cell);
2697                 if (inset->searchBackward(bv, str, cs, mw)) {
2698                         updateLocal(bv, CELL);
2699                         return true;
2700                 }
2701         }
2702         return false;
2703 }
2704
2705
2706 bool InsetTabular::insetAllowed(Inset::Code code) const
2707 {
2708         if (the_locking_inset)
2709                 return the_locking_inset->insetAllowed(code);
2710         // we return true here because if the inset is not locked someone
2711         // wants to insert something in one of our insettexts and we generally
2712         // allow to do so.
2713         return true;
2714 }
2715
2716
2717 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2718 {
2719         const int cell = tabular->GetCellFromInset(in, actcell);
2720
2721         if (cell != -1)
2722                 return tabular->GetPWidth(cell).zero();
2723
2724         // well we didn't obviously find it so maybe our owner knows more
2725         if (owner())
2726                 return owner()->forceDefaultParagraphs(in);
2727         // if we're here there is really something strange going on!!!
2728         return false;
2729 }
2730
2731 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2732                                      bool usePaste)
2733 {
2734         if (buf.length() <= 0)
2735                 return true;
2736
2737         int cols = 1;
2738         int rows = 1;
2739         int maxCols = 1;
2740         string::size_type len = buf.length();
2741         string::size_type p = 0;
2742
2743         while (p < len &&
2744                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2745         {
2746                 switch (buf[p]) {
2747                 case '\t':
2748                         ++cols;
2749                         break;
2750                 case '\n':
2751                         if ((p+1) < len)
2752                                 ++rows;
2753                         maxCols = max(cols, maxCols);
2754                         cols = 1;
2755                         break;
2756                 }
2757                 ++p;
2758         }
2759         maxCols = max(cols, maxCols);
2760         LyXTabular * loctab;
2761         int cell = 0;
2762         int ocol = 0;
2763         int row = 0;
2764         if (usePaste) {
2765                 delete paste_tabular;
2766                 paste_tabular = new LyXTabular(bv->buffer()->params,
2767                                                this, rows, maxCols);
2768                 loctab = paste_tabular;
2769                 cols = 0;
2770         } else {
2771                 loctab = tabular.get();
2772                 cell = actcell;
2773                 ocol = actcol;
2774                 row = actrow;
2775         }
2776
2777         string::size_type op = 0;
2778         int cells = loctab->GetNumberOfCells();
2779         p = 0;
2780         cols = ocol;
2781         rows = loctab->rows();
2782         int const columns = loctab->columns();
2783
2784         while ((cell < cells) && (p < len) && (row < rows) &&
2785                (p = buf.find_first_of("\t\n", p)) != string::npos)
2786         {
2787                 if (p >= len)
2788                         break;
2789                 switch (buf[p]) {
2790                 case '\t':
2791                         // we can only set this if we are not too far right
2792                         if (cols < columns) {
2793                                 InsetText * ti = loctab->GetCellInset(cell);
2794                                 LyXFont const font = ti->getLyXText(bv)->
2795                                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2796                                 ti->setText(buf.substr(op, p - op), font);
2797                                 ++cols;
2798                                 ++cell;
2799                         }
2800                         break;
2801                 case '\n':
2802                         // we can only set this if we are not too far right
2803                         if (cols < columns) {
2804                                 InsetText * ti = loctab->GetCellInset(cell);
2805                                 LyXFont const font = ti->getLyXText(bv)->
2806                                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2807                                 ti->setText(buf.substr(op, p - op), font);
2808                         }
2809                         cols = ocol;
2810                         ++row;
2811                         if (row < rows)
2812                                 cell = loctab->GetCellNumber(row, cols);
2813                         break;
2814                 }
2815                 ++p;
2816                 op = p;
2817         }
2818         // check for the last cell if there is no trailing '\n'
2819         if ((cell < cells) && (op < len)) {
2820                 InsetText * ti = loctab->GetCellInset(cell);
2821                 LyXFont const font = ti->getLyXText(bv)->
2822                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2823                 ti->setText(buf.substr(op, len - op), font);
2824         }
2825
2826         return true;
2827 }
2828
2829
2830 void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
2831 {
2832         int const rows = tabular->rows();
2833         int const columns = tabular->columns();
2834         for (int i = 0; i < rows; ++i) {
2835                 for (int j = 0; j < columns; ++j) {
2836                         tabular->GetCellInset(i,j)->addPreview(loader);
2837                 }
2838         }
2839 }
2840
2841
2842 string const InsetTabularMailer:: name_("tabular");
2843
2844 InsetTabularMailer::InsetTabularMailer(InsetTabular & inset)
2845         : inset_(inset)
2846 {}
2847
2848
2849 string const InsetTabularMailer::inset2string() const
2850 {
2851         return params2string(inset_);
2852 }
2853
2854
2855 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2856 {
2857         istringstream data(STRCONV(in));
2858         LyXLex lex(0,0);
2859         lex.setStream(data);
2860
2861 #warning CHECK verify that this is a sane value to return.
2862         if (in.empty())
2863                 return -1;
2864
2865         if (lex.isOK()) {
2866                 lex.next();
2867                 string const token = lex.getString();
2868                 if (token != name_)
2869                         return -1;
2870         }
2871
2872         int cell = -1;
2873         if (lex.isOK()) {
2874                 lex.next();
2875                 string const token = lex.getString();
2876                 if (token != "\\active_cell")
2877                         return -1;
2878                 lex.next();
2879                 cell = lex.getInteger();
2880         }
2881
2882         // This is part of the inset proper that is usually swallowed
2883         // by Buffer::readInset
2884         if (lex.isOK()) {
2885                 lex.next();
2886                 string const token = lex.getString();
2887                 if (token != "Tabular")
2888                         return -1;
2889         }
2890
2891         if (!lex.isOK())
2892                 return -1;
2893
2894         BufferView * const bv = inset.view();
2895         Buffer const * const buffer = bv ? bv->buffer() : 0;
2896         if (buffer)
2897                 inset.read(buffer, lex);
2898
2899         // We can't set the active cell, but we can tell the frontend
2900         // what it is.
2901         return cell;
2902 }
2903
2904
2905 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2906 {
2907         BufferView * const bv = inset.view();
2908         Buffer const * const buffer = bv ? bv->buffer() : 0;
2909         if (!buffer)
2910                 return string();
2911
2912         ostringstream data;
2913         data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2914         inset.write(buffer, data);
2915         data << "\\end_inset\n";
2916         return STRCONV(data.str());
2917 }