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