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