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