]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
more cursor dispatch
[lyx.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyx_cb.h"
27 #include "lyxlex.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "undo.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
42 #include <iostream>
43
44 using lyx::graphics::PreviewLoader;
45
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
49
50 using std::auto_ptr;
51 using std::endl;
52 using std::max;
53 using std::string;
54 using std::istringstream;
55 using std::ostream;
56 using std::ostringstream;
57 using std::swap;
58 using std::vector;
59
60
61 namespace {
62
63 int const ADD_TO_HEIGHT = 2;
64 int const ADD_TO_TABULAR_WIDTH = 2;
65
66 ///
67 boost::scoped_ptr<LyXTabular> paste_tabular;
68
69
70 struct TabularFeature {
71         LyXTabular::Feature action;
72         string feature;
73 };
74
75
76 TabularFeature tabularFeature[] =
77 {
78         { LyXTabular::APPEND_ROW, "append-row" },
79         { LyXTabular::APPEND_COLUMN, "append-column" },
80         { LyXTabular::DELETE_ROW, "delete-row" },
81         { LyXTabular::DELETE_COLUMN, "delete-column" },
82         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
83         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
84         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
85         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
86         { LyXTabular::ALIGN_LEFT, "align-left" },
87         { LyXTabular::ALIGN_RIGHT, "align-right" },
88         { LyXTabular::ALIGN_CENTER, "align-center" },
89         { LyXTabular::ALIGN_BLOCK, "align-block" },
90         { LyXTabular::VALIGN_TOP, "valign-top" },
91         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
92         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
93         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
94         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
95         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
96         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
97         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
98         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
99         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
100         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
101         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
102         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
103         { LyXTabular::MULTICOLUMN, "multicolumn" },
104         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
105         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
106         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
107         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
108         { LyXTabular::SET_PWIDTH, "set-pwidth" },
109         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
110         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
111         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
112         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
113         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
114         { LyXTabular::SET_USEBOX, "set-usebox" },
115         { LyXTabular::SET_LTHEAD, "set-lthead" },
116         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
117         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
118         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
119         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
120         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
121         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
122         { LyXTabular::LAST_ACTION, "" }
123 };
124
125
126 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
127 public:
128         FeatureEqual(LyXTabular::Feature feature)
129                 : feature_(feature) {}
130         bool operator()(TabularFeature const & tf) const {
131                 return tf.action == feature_;
132         }
133 private:
134         LyXTabular::Feature feature_;
135 };
136
137 } // namespace anon
138
139
140 string const featureAsString(LyXTabular::Feature feature)
141 {
142         TabularFeature * end = tabularFeature +
143                 sizeof(tabularFeature) / sizeof(TabularFeature);
144         TabularFeature * it = std::find_if(tabularFeature, end,
145                                            FeatureEqual(feature));
146         return (it == end) ? string() : it->feature;
147 }
148
149
150 bool InsetTabular::hasPasteBuffer() const
151 {
152         return (paste_tabular.get() != 0);
153 }
154
155
156 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
157         : tabular(buf.params(), max(rows, 1), max(columns, 1)),
158           buffer_(&buf), cursorx_(0), cursory_(0), tablemode(false)
159 {
160         tabular.setOwner(this);
161         clearSelection();
162         in_reset_pos = 0;
163 }
164
165
166 InsetTabular::InsetTabular(InsetTabular const & tab)
167         : UpdatableInset(tab), tabular(tab.tabular),
168                 buffer_(tab.buffer_), cursorx_(0), cursory_(0), tablemode(false)
169 {
170         tabular.setOwner(this);
171         clearSelection();
172         in_reset_pos = 0;
173 }
174
175
176 InsetTabular::~InsetTabular()
177 {
178         InsetTabularMailer(*this).hideDialog();
179 }
180
181
182 auto_ptr<InsetBase> InsetTabular::clone() const
183 {
184         return auto_ptr<InsetBase>(new InsetTabular(*this));
185 }
186
187
188 Buffer const & InsetTabular::buffer() const
189 {
190         return *buffer_;
191 }
192
193
194 void InsetTabular::buffer(Buffer * b)
195 {
196         buffer_ = b;
197 }
198
199
200 void InsetTabular::write(Buffer const & buf, ostream & os) const
201 {
202         os << "Tabular" << endl;
203         tabular.write(buf, os);
204 }
205
206
207 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
208 {
209         bool const old_format = (lex.getString() == "\\LyXTable");
210
211         tabular.read(buf, lex);
212
213         if (old_format)
214                 return;
215
216         lex.nextToken();
217         string token = lex.getString();
218         while (lex.isOK() && (token != "\\end_inset")) {
219                 lex.nextToken();
220                 token = lex.getString();
221         }
222         if (token != "\\end_inset") {
223                 lex.printError("Missing \\end_inset at this point. "
224                                "Read: `$$Token'");
225         }
226 }
227
228
229 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
230 {
231         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
232         //      mi.base.textwidth << "\n";
233         if (!mi.base.bv) {
234                 lyxerr << "InsetTabular::metrics: need bv" << endl;
235                 BOOST_ASSERT(false);
236         }
237
238         calculate_dimensions_of_cells(mi);
239
240         dim.asc = tabular.getAscentOfRow(0);
241         dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
242         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
243         dim_ = dim;
244 }
245
246
247 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
248 {
249         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
250
251         BufferView * bv = pi.base.bv;
252         setPosCache(pi, x, y);
253
254         if (!owner())
255                 x += scroll();
256
257         x += ADD_TO_TABULAR_WIDTH;
258
259         int cell = 0;
260         first_visible_cell = -1;
261         for (int i = 0; i < tabular.rows(); ++i) {
262                 int nx = x;
263                 cell = tabular.getCellNumber(i, 0);
264                 if (y + tabular.getDescentOfRow(i) <= 0 &&
265                           y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
266                 {
267                         y += tabular.getDescentOfRow(i) +
268                                         tabular.getAscentOfRow(i + 1) +
269                                         tabular.getAdditionalHeight(i + 1);
270                         continue;
271                 }
272                 for (int j = 0; j < tabular.columns(); ++j) {
273                         if (nx > bv->workWidth())
274                                 break;
275                         if (tabular.isPartOfMultiColumn(i, j))
276                                 continue;
277                         if (first_visible_cell < 0)
278                                 first_visible_cell = cell;
279                         if (hasSelection())
280                                 drawCellSelection(pi.pain, nx, y, i, j, cell);
281
282                         int const cx = nx + tabular.getBeginningOfTextInCell(cell);
283                         tabular.getCellInset(cell).draw(pi, cx, y);
284                         drawCellLines(pi.pain, nx, y, i, cell);
285                         nx += tabular.getWidthOfColumn(cell);
286                         ++cell;
287                 }
288
289 // Would be nice, but for some completely unfathomable reason,
290 // on a col resize to a new fixed width, even though the insettexts
291 // are resized, the cell isn't, but drawing all cells in a tall table
292 // has the desired effect somehow. Complete dark magic.
293 #if 0
294                 // avoiding drawing the rest of a long table is
295                 // a pretty big speedup
296                 if (y > bv->workHeight())
297                         break;
298 #endif
299
300                 y += tabular.getDescentOfRow(i) +
301                         tabular.getAscentOfRow(i + 1) +
302                         tabular.getAdditionalHeight(i + 1);
303         }
304 }
305
306
307 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
308                                  int row, int cell) const
309 {
310         int x2 = x + tabular.getWidthOfColumn(cell);
311         bool on_off = false;
312
313         if (!tabular.topAlreadyDrawn(cell)) {
314                 on_off = !tabular.topLine(cell);
315                 pain.line(x, y - tabular.getAscentOfRow(row),
316                           x2, y -  tabular.getAscentOfRow(row),
317                           on_off ? LColor::tabularonoffline : LColor::tabularline,
318                           on_off ? Painter::line_onoffdash : Painter::line_solid);
319         }
320         on_off = !tabular.bottomLine(cell);
321         pain.line(x, y + tabular.getDescentOfRow(row),
322                   x2, y + tabular.getDescentOfRow(row),
323                   on_off ? LColor::tabularonoffline : LColor::tabularline,
324                   on_off ? Painter::line_onoffdash : Painter::line_solid);
325         if (!tabular.leftAlreadyDrawn(cell)) {
326                 on_off = !tabular.leftLine(cell);
327                 pain.line(x, y -  tabular.getAscentOfRow(row),
328                           x, y +  tabular.getDescentOfRow(row),
329                           on_off ? LColor::tabularonoffline : LColor::tabularline,
330                           on_off ? Painter::line_onoffdash : Painter::line_solid);
331         }
332         on_off = !tabular.rightLine(cell);
333         pain.line(x2 - tabular.getAdditionalWidth(cell),
334                   y -  tabular.getAscentOfRow(row),
335                   x2 - tabular.getAdditionalWidth(cell),
336                   y +  tabular.getDescentOfRow(row),
337                   on_off ? LColor::tabularonoffline : LColor::tabularline,
338                   on_off ? Painter::line_onoffdash : Painter::line_solid);
339 }
340
341
342 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
343                                      int row, int column, int cell) const
344 {
345         if (!tablemode)
346                 return;
347
348         BOOST_ASSERT(hasSelection());
349         int cs = tabular.column_of_cell(sel_cell_start);
350         int ce = tabular.column_of_cell(sel_cell_end);
351         if (cs > ce) {
352                 ce = cs;
353                 cs = tabular.column_of_cell(sel_cell_end);
354         } else {
355                 ce = tabular.right_column_of_cell(sel_cell_end);
356         }
357
358         int rs = tabular.row_of_cell(sel_cell_start);
359         int re = tabular.row_of_cell(sel_cell_end);
360         if (rs > re)
361                 swap(rs, re);
362
363         if (column >= cs && column <= ce && row >= rs && row <= re) {
364                 int w = tabular.getWidthOfColumn(cell);
365                 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
366                 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
367                                    w, h, LColor::selection);
368         }
369 }
370
371
372 string const InsetTabular::editMessage() const
373 {
374         return _("Opened table");
375 }
376
377
378 void InsetTabular::updateLocal(LCursor & cur) const
379 {
380         cur.bv().update();
381         resetPos(cur);
382 }
383
384
385 void InsetTabular::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
386 {
387         if (hasSelection() && cmd.button() == mouse_button::button3)
388                 return;
389
390         int cell = getCell(cmd.x + xo_, cmd.y + yo_);
391         clearSelection();
392
393         lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
394         if (cell == -1) {
395                 tablemode = true;
396                 //cur.cursor_ = theTempCursor;
397                 cur.push(this);
398                 cur.idx() = cell;
399         } else {
400                 tablemode = false;
401                 setPos(cur.bv(), cmd.x, cmd.y);
402                 //cur.cursor_ = theTempCursor;
403                 cur.idx() = cell;
404         }
405         cur.resetAnchor();
406         lyxerr << cur << endl;
407
408         if (cmd.button() == mouse_button::button2)
409                 dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
410 }
411
412
413 void InsetTabular::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
414 {
415         int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
416         lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
417
418         setPos(cur.bv(), cmd.x, cmd.y);
419         if (!hasSelection()) {
420                 setSelection(actcell, actcell);
421                 cur.setSelection();
422         } else {
423                 cur.idx() = actcell;
424                 setSelection(sel_cell_start, actcell);
425                 tablemode = (sel_cell_start != actcell);
426         }
427 }
428
429
430 void InsetTabular::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
431 {
432         int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
433         lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
434         if (cmd.button() == mouse_button::button3)
435                 InsetTabularMailer(*this).showDialog(&cur.bv());
436 }
437
438
439 void InsetTabular::edit(LCursor & cur, bool left)
440 {
441         lyxerr << "InsetTabular::edit: " << this << endl;
442         finishUndo();
443         //tablemode = false;
444         int cell;
445         if (left) {
446                 if (isRightToLeft(cur))
447                         cell = tabular.getLastCellInRow(0);
448                 else
449                         cell = 0;
450         } else {
451                 if (isRightToLeft(cur))
452                         cell = tabular.getFirstCellInRow(tabular.rows()-1);
453                 else
454                         cell = tabular.getNumberOfCells() - 1;
455         }
456         clearSelection();
457         resetPos(cur);
458         cur.bv().fitCursor();
459         cur.push(this);
460         cur.idx() = cell;
461 }
462
463
464 void InsetTabular::edit(LCursor & cur, int x, int y)
465 {
466         lyxerr << "InsetTabular::edit: " << this << " first cell "
467                 << &tabular.cell_info[0][0].inset << endl;
468
469         finishUndo();
470         setPos(cur.bv(), x, y);
471         clearSelection();
472         finishUndo();
473         //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
474         cur.push(this);
475         //if (x > xx)
476         //      activateCellInset(bv, cell, x - xx, y - cursory_);
477 }
478
479
480 DispatchResult
481 InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
482 {
483         lyxerr << "# InsetTabular::dispatch: " << cmd
484                 << " tablemode: " << tablemode << endl;
485
486         DispatchResult result(true, true);
487         switch (cmd.action) {
488
489         case LFUN_MOUSE_PRESS:
490                 lfunMousePress(cur, cmd);
491                 return DispatchResult(true, true);
492
493         case LFUN_MOUSE_MOTION:
494                 lfunMouseMotion(cur, cmd);
495                 return DispatchResult(true, true);
496
497         case LFUN_MOUSE_RELEASE:
498                 lfunMouseRelease(cur, cmd);
499                 return DispatchResult(true, true);
500
501         default:
502                 break;
503         }
504
505         if (!tablemode) {
506                 int const cell = cur.idx();
507                 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
508                 result = tabular.getCellInset(cell).dispatch(cur, cmd);
509
510                 switch (result.val()) {
511                 case FINISHED:
512                         if (movePrevCell(cur))
513                                 result = DispatchResult(true, true);
514                         else
515                                 result = DispatchResult(false, FINISHED);
516                         break;
517
518                 case FINISHED_RIGHT:
519                         if (moveNextCell(cur))
520                                 result = DispatchResult(true, true);
521                         else
522                                 result = DispatchResult(false, FINISHED_RIGHT);
523                         break;
524
525                 case FINISHED_UP:
526                         if (moveUpLock(cur))
527                                 result = DispatchResult(true, true);
528                         else
529                                 result = DispatchResult(false, FINISHED_UP);
530                         break;
531
532                 case FINISHED_DOWN:
533                         if (moveDownLock(cur))
534                                 result = DispatchResult(true, true);
535                         else
536                                 result = DispatchResult(false, FINISHED_UP);
537                         break;
538
539                 default:
540                         lyxerr << "# don't handle dispatch" << endl;
541                         break;
542                 }
543
544                 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
545         } else {
546
547                 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
548                 switch (cmd.action) {
549
550                 case LFUN_CELL_BACKWARD:
551                 case LFUN_CELL_FORWARD:
552                         if (cmd.action == LFUN_CELL_FORWARD)
553                                 moveNextCell(cur);
554                         else
555                                 movePrevCell(cur);
556                         clearSelection();
557                         return result;
558
559                 case LFUN_SCROLL_INSET:
560                         if (!cmd.argument.empty()) {
561                                 if (cmd.argument.find('.') != cmd.argument.npos)
562                                         scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
563                                 else
564                                         scroll(cur.bv(), strToInt(cmd.argument));
565                                 cur.bv().update();
566                                 return DispatchResult(true, true);
567                         }
568
569                 case LFUN_RIGHTSEL: {
570                         int const start = hasSelection() ? sel_cell_start : cur.idx();
571                         if (tabular.isLastCellInRow(cur.idx())) {
572                                 setSelection(start, cur.idx());
573                                 break;
574                         }
575
576                         int end = cur.idx();
577                         // if we are starting a selection, only select
578                         // the current cell at the beginning
579                         if (hasSelection()) {
580                                 moveRight(cur);
581                                 end = cur.idx();
582                         }
583                         setSelection(start, end);
584                         break;
585                 }
586
587                 case LFUN_RIGHT:
588                         if (!moveRightLock(cur))
589                                 result = DispatchResult(false, FINISHED_RIGHT);
590                         clearSelection();
591                         break;
592
593                 case LFUN_LEFTSEL: {
594                         int const start = hasSelection() ? sel_cell_start : cur.idx();
595                         if (tabular.isFirstCellInRow(cur.idx())) {
596                                 setSelection(start, cur.idx());
597                                 break;
598                         }
599
600                         int end = cur.idx();
601                         // if we are starting a selection, only select
602                         // the current cell at the beginning
603                         if (hasSelection()) {
604                                 moveLeft(cur);
605                                 end = cur.idx();
606                         }
607                         setSelection(start, end);
608                         break;
609                 }
610
611                 case LFUN_LEFT:
612                         if (!moveLeftLock(cur))
613                                 result = DispatchResult(false, FINISHED);
614                         clearSelection();
615                         break;
616
617                 case LFUN_DOWNSEL: {
618                         int const start = hasSelection() ? sel_cell_start : cur.idx();
619                         int const ocell = cur.idx();
620                         // if we are starting a selection, only select
621                         // the current cell at the beginning
622                         if (hasSelection()) {
623                                 moveDown(cur);
624                                 if (ocell == sel_cell_end ||
625                                                 tabular.column_of_cell(ocell) >
626 tabular.column_of_cell(cur.idx()))
627                                         setSelection(start, tabular.getCellBelow(sel_cell_end));
628                                 else
629                                         setSelection(start, tabular.getLastCellBelow(sel_cell_end));
630                         } else {
631                                 setSelection(start, start);
632                         }
633                         break;
634                 }
635
636                 case LFUN_DOWN:
637                         if (!moveDown(cur))
638                                 result = DispatchResult(false, FINISHED_DOWN);
639                         clearSelection();
640                         break;
641
642                 case LFUN_UPSEL: {
643                         int const start = hasSelection() ? sel_cell_start : cur.idx();
644                         int const ocell = cur.idx();
645                         // if we are starting a selection, only select
646                         // the current cell at the beginning
647                         if (hasSelection()) {
648                                 moveUp(cur);
649                                 if (ocell == sel_cell_end ||
650                                                 tabular.column_of_cell(ocell) >
651 tabular.column_of_cell(cur.idx()))
652                                         setSelection(start, tabular.getCellAbove(sel_cell_end));
653                                 else
654                                         setSelection(start, tabular.getLastCellAbove(sel_cell_end));
655                         } else {
656                                 setSelection(start, start);
657                         }
658                         break;
659                 }
660
661                 case LFUN_UP:
662                         if (!moveUp(cur))
663                                 result = DispatchResult(false, FINISHED_DOWN);
664                         clearSelection();
665                         break;
666
667                 case LFUN_NEXT: {
668                         if (hasSelection())
669                                 clearSelection();
670                         int actcell = cur.idx();
671                         int actcol = tabular.column_of_cell(actcell);
672                         int column = actcol;
673                         if (cur.bv().top_y() + cur.bv().painter().paperHeight()
674                                         < yo_ + tabular.getHeightOfTabular())
675                         {
676                                 cur.bv().scrollDocView(
677                                         cur.bv().top_y() + cur.bv().painter().paperHeight());
678                                 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
679                         } else {
680                                 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
681                         }
682                         resetPos(cur);
683                         break;
684                 }
685
686                 case LFUN_PRIOR: {
687                         if (hasSelection())
688                                 clearSelection();
689                         int column = tabular.column_of_cell(cur.idx());
690                         if (yo_ < 0) {
691                                 cur.bv().scrollDocView(
692                                         cur.bv().top_y() - cur.bv().painter().paperHeight());
693                                 if (yo_ > 0)
694                                         cur.idx() = column;
695                                 else
696                                         cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
697                         } else {
698                                 cur.idx() = column;
699                         }
700                         resetPos(cur);
701                         break;
702                 }
703
704                 // none of these make sense for insettabular,
705                 // but we must catch them to prevent any
706                 // selection from being confused
707                 case LFUN_PRIORSEL:
708                 case LFUN_NEXTSEL:
709                 case LFUN_WORDLEFT:
710                 case LFUN_WORDLEFTSEL:
711                 case LFUN_WORDRIGHT:
712                 case LFUN_WORDRIGHTSEL:
713                 case LFUN_WORDSEL:
714                 case LFUN_DOWN_PARAGRAPH:
715                 case LFUN_DOWN_PARAGRAPHSEL:
716                 case LFUN_UP_PARAGRAPH:
717                 case LFUN_UP_PARAGRAPHSEL:
718                 case LFUN_BACKSPACE:
719                 case LFUN_HOME:
720                 case LFUN_HOMESEL:
721                 case LFUN_END:
722                 case LFUN_ENDSEL:
723                 case LFUN_BEGINNINGBUF:
724                 case LFUN_BEGINNINGBUFSEL:
725                 case LFUN_ENDBUF:
726                 case LFUN_ENDBUFSEL:
727                         break;
728
729                 case LFUN_LAYOUT_TABULAR:
730                         InsetTabularMailer(*this).showDialog(&cur.bv());
731                         break;
732
733                 case LFUN_INSET_DIALOG_UPDATE:
734                         InsetTabularMailer(*this).updateDialog(&cur.bv());
735                         break;
736
737                 case LFUN_TABULAR_FEATURE:
738                         if (!tabularFeatures(cur, cmd.argument))
739                                 result = DispatchResult(false);
740                         break;
741
742                 // insert file functions
743                 case LFUN_FILE_INSERT_ASCII_PARA:
744                 case LFUN_FILE_INSERT_ASCII: {
745                         string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
746                         if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
747                                 result = DispatchResult(false);
748                         break;
749                 }
750
751                 case LFUN_LANGUAGE:
752                 case LFUN_EMPH:
753                 case LFUN_BOLD:
754                 case LFUN_NOUN:
755                 case LFUN_CODE:
756                 case LFUN_SANS:
757                 case LFUN_ROMAN:
758                 case LFUN_DEFAULT:
759                 case LFUN_UNDERLINE:
760                 case LFUN_FONT_SIZE:
761                         lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
762                         break;
763
764                 case LFUN_CUT:
765                         if (copySelection(cur.bv())) {
766                                 recordUndo(cur, Undo::DELETE);
767                                 cutSelection(cur.bv().buffer()->params());
768                         }
769                         break;
770
771                 case LFUN_DELETE:
772                         recordUndo(cur, Undo::DELETE);
773                         cutSelection(cur.bv().buffer()->params());
774                         break;
775
776                 case LFUN_COPY:
777                         if (!hasSelection())
778                                 break;
779                         finishUndo();
780                         copySelection(cur.bv());
781                         break;
782
783                 case LFUN_PASTESELECTION: {
784                         string const clip = cur.bv().getClipboard();
785                         if (clip.empty())
786                                 break;
787                         if (clip.find('\t') != string::npos) {
788                                 int cols = 1;
789                                 int rows = 1;
790                                 int maxCols = 1;
791                                 string::size_type len = clip.length();
792                                 string::size_type p = 0;
793
794                                 while (p < len &&
795                                                         (p = clip.find_first_of("\t\n", p)) != string::npos) {
796                                         switch (clip[p]) {
797                                         case '\t':
798                                                 ++cols;
799                                                 break;
800                                         case '\n':
801                                                 if (p + 1 < len)
802                                                         ++rows;
803                                                 maxCols = max(cols, maxCols);
804                                                 cols = 1;
805                                                 break;
806                                         }
807                                         ++p;
808                                 }
809                                 maxCols = max(cols, maxCols);
810
811                                 paste_tabular.reset(
812                                         new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
813
814                                 string::size_type op = 0;
815                                 int cell = 0;
816                                 int cells = paste_tabular->getNumberOfCells();
817                                 p = 0;
818                                 cols = 0;
819                                 LyXFont font;
820                                 while (cell < cells && p < len &&
821                                                         (p = clip.find_first_of("\t\n", p)) != string::npos) {
822                                         if (p >= len)
823                                                 break;
824                                         switch (clip[p]) {
825                                         case '\t':
826                                                 paste_tabular->getCellInset(cell).
827                                                         setText(clip.substr(op, p-op), font);
828                                                 ++cols;
829                                                 ++cell;
830                                                 break;
831                                         case '\n':
832                                                 paste_tabular->getCellInset(cell).
833                                                         setText(clip.substr(op, p-op), font);
834                                                 while (cols++ < maxCols)
835                                                         ++cell;
836                                                 cols = 0;
837                                                 break;
838                                         }
839                                         ++p;
840                                         op = p;
841                                 }
842                                 // check for the last cell if there is no trailing '\n'
843                                 if (cell < cells && op < len)
844                                         paste_tabular->getCellInset(cell).
845                                                 setText(clip.substr(op, len-op), font);
846                         } else if (!insertAsciiString(cur.bv(), clip, true))
847                         {
848                                 // so that the clipboard is used and it goes on
849                                 // to default
850                                 // and executes LFUN_PASTESELECTION in insettext!
851                                 paste_tabular.reset();
852                         }
853                         // fall through
854                 }
855
856                 case LFUN_PASTE:
857                         if (hasPasteBuffer()) {
858                                 recordUndo(cur, Undo::INSERT);
859                                 pasteSelection(cur.bv());
860                                 break;
861                         }
862                         // fall through
863
864                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
865
866                 default:
867                         // handle font changing stuff on selection before we lock the inset
868                         // in the default part!
869                         result = DispatchResult(false);
870                         // we try to activate the actual inset and put this event down to
871                         // the insets dispatch function.
872                         break;
873                 }
874
875                 updateLocal(cur);
876                 InsetTabularMailer(*this).updateDialog(&cur.bv());
877         }
878
879         if (cmd.action == LFUN_INSET_TOGGLE) {
880                 tablemode = !tablemode;
881                 result = DispatchResult(true, true);
882         }
883
884         return result;
885 }
886
887
888 int InsetTabular::latex(Buffer const & buf, ostream & os,
889                         OutputParams const & runparams) const
890 {
891         return tabular.latex(buf, os, runparams);
892 }
893
894
895 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
896                         OutputParams const & runparams) const
897 {
898         int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
899         return tabular.plaintext(buf, os, runparams, dp, false, 0);
900 }
901
902
903 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
904                            OutputParams const & runparams) const
905 {
906         return tabular.linuxdoc(buf,os, runparams);
907 }
908
909
910 int InsetTabular::docbook(Buffer const & buf, ostream & os,
911                           OutputParams const & runparams) const
912 {
913         int ret = 0;
914         InsetOld * master;
915
916         // if the table is inside a float it doesn't need the informaltable
917         // wrapper. Search for it.
918         for (master = owner(); master; master = master->owner())
919                 if (master->lyxCode() == InsetOld::FLOAT_CODE)
920                         break;
921
922         if (!master) {
923                 os << "<informaltable>";
924                 if (runparams.mixed_content)
925                         os << endl;
926                 ++ret;
927         }
928         ret += tabular.docbook(buf, os, runparams);
929         if (!master) {
930                 os << "</informaltable>";
931                 if (runparams.mixed_content)
932                         os << endl;
933                 ++ret;
934         }
935         return ret;
936 }
937
938
939 void InsetTabular::validate(LaTeXFeatures & features) const
940 {
941         tabular.validate(features);
942 }
943
944
945 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
946 {
947         for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
948                 int maxAsc = 0;
949                 int maxDesc = 0;
950                 for (int j = 0; j < tabular.columns(); ++j) {
951                         if (tabular.isPartOfMultiColumn(i, j))
952                                 continue;
953                         ++cell;
954                         Dimension dim;
955                         MetricsInfo m = mi;
956                         m.base.textwidth =
957                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
958                         tabular.getCellInset(cell).metrics(m, dim);
959                         maxAsc  = max(maxAsc, dim.asc);
960                         maxDesc = max(maxDesc, dim.des);
961                         tabular.setWidthOfCell(cell, dim.wid);
962                 }
963                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
964                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
965         }
966 }
967
968
969 void InsetTabular::getCursorPos(CursorSlice const & cur, int & x, int & y) const
970 {
971         tabular.getCellInset(cur.idx()).getCursorPos(cur, x, y);
972 }
973
974
975 void InsetTabular::setPos(BufferView & bv, int x, int y) const
976 {
977         int const cell = getCell(x + xo_, y + yo_);
978         lyxerr << "# InsetTabular::setPos()  cell: " << cell << endl;
979         InsetText const & inset = tabular.getCellInset(cell);
980         inset.text_.setCursorFromCoordinates(x, y);
981 #if 0
982         cursory_ = 0;
983         int actcell = 0;
984         int actrow = 0;
985         int actcol = 0;
986         int ly = tabular.getDescentOfRow(actrow);
987
988         // first search the right row
989         while (ly < y && actrow + 1 < tabular.rows()) {
990                 cursory_ += tabular.getDescentOfRow(actrow) +
991                                  tabular.getAscentOfRow(actrow + 1) +
992                                  tabular.getAdditionalHeight(actrow + 1);
993                 ++actrow;
994                 ly = cursory_ + tabular.getDescentOfRow(actrow);
995         }
996         actcell = tabular.getCellNumber(actrow, actcol);
997
998         // now search the right column
999         int lx = tabular.getWidthOfColumn(actcell) -
1000                 tabular.getAdditionalWidth(actcell);
1001
1002         for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1003                 lx += tabular.getWidthOfColumn(actcell + 1)
1004                         + tabular.getAdditionalWidth(actcell);
1005
1006         cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1007 #endif
1008         resetPos(bv.cursor());
1009 }
1010
1011
1012 int InsetTabular::getCellXPos(int cell) const
1013 {
1014         int c = cell;
1015
1016         for (; !tabular.isFirstCellInRow(c); --c)
1017                 ;
1018         int lx = tabular.getWidthOfColumn(cell);
1019         for (; c < cell; ++c)
1020                 lx += tabular.getWidthOfColumn(c);
1021
1022         return lx - tabular.getWidthOfColumn(cell) + xo_;
1023 }
1024
1025
1026 void InsetTabular::resetPos(LCursor &) const
1027 {
1028 #if 0
1029 #ifdef WITH_WARNINGS
1030 #warning This should be fixed in the right manner (20011128 Jug)
1031 #endif
1032         // fast hack to fix infinite repaintings!
1033         if (in_reset_pos > 0)
1034                 return;
1035
1036         BufferView & bv = cur.bv();
1037         int cell = 0;
1038         int actcell = cur.cell();
1039         int actcol = tabular.column_of_cell(actcell);
1040         int actrow = 0;
1041         cursory_ = 0;
1042         for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1043                 if (tabular.isLastCellInRow(cell)) {
1044                         cursory_ += tabular.getDescentOfRow(actrow) +
1045                                          tabular.getAscentOfRow(actrow + 1) +
1046                                          tabular.getAdditionalHeight(actrow + 1);
1047                         ++actrow;
1048                 }
1049         }
1050
1051         // we need this only from here on!!!
1052         ++in_reset_pos;
1053         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1054         int new_x = getCellXPos(actcell) + offset;
1055         int old_x = cursorx_;
1056         cursorx_ = new_x;
1057 //    cursor.x(getCellXPos(actcell) + offset);
1058         if (actcol < tabular.columns() - 1 && scroll(false) &&
1059                 tabular.getWidthOfTabular() < bv->workWidth()-20)
1060         {
1061                 scroll(bv, 0.0F);
1062                 updateLocal(cur);
1063         } else if (cursorx_ - offset > 20 &&
1064                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1065                    > bv.workWidth() - 20) {
1066                 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1067                 updateLocal(cur);
1068         } else if (cursorx_ - offset < 20) {
1069                 scroll(&bv, 20 - cursorx_ + offset);
1070                 updateLocal(cur);
1071         } else if (scroll() && xo_ > 20 &&
1072                    xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1073                 scroll(&bv, old_x - cursorx_);
1074                 updateLocal(cur);
1075         }
1076         InsetTabularMailer(*this).updateDialog(bv);
1077         in_reset_pos = 0;
1078 #endif
1079 }
1080
1081
1082 bool InsetTabular::moveRight(LCursor & cur)
1083 {
1084         bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1085         if (!moved)
1086                 return false;
1087         resetPos(cur);
1088         return true;
1089 }
1090
1091
1092 bool InsetTabular::moveRightLock(LCursor & cur)
1093 {
1094         bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1095         if (!moved)
1096                 return false;
1097         activateCellInset(cur, cur.idx(), false);
1098         return true;
1099 }
1100
1101
1102 bool InsetTabular::moveLeft(LCursor & cur)
1103 {
1104         bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1105         if (!moved)
1106                 return false;
1107         resetPos(cur);
1108         return true;
1109 }
1110
1111
1112 bool InsetTabular::moveLeftLock(LCursor & cur)
1113 {
1114         bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1115         if (!moved)
1116                 return false;
1117         activateCellInset(cur, cur.idx(), true);
1118         return true;
1119 }
1120
1121
1122 bool InsetTabular::moveUp(LCursor & cur)
1123 {
1124         if (tabular.row_of_cell(cur.idx()) == 0)
1125                 return false;
1126         cur.idx() = tabular.getCellAbove(cur.idx());
1127         resetPos(cur);
1128         return true;
1129 }
1130
1131
1132 bool InsetTabular::moveUpLock(LCursor & cur)
1133 {
1134         if (tabular.row_of_cell(cur.idx()) == 0)
1135                 return false;
1136         cur.idx() = tabular.getCellAbove(cur.idx());
1137         resetPos(cur);
1138         activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1139         return true;
1140 }
1141
1142
1143 bool InsetTabular::moveDown(LCursor & cur)
1144 {
1145         if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1146                 return false;
1147         cur.idx() = tabular.getCellBelow(cur.idx());
1148         resetPos(cur);
1149         return true;
1150 }
1151
1152
1153 bool InsetTabular::moveDownLock(LCursor & cur)
1154 {
1155         if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1156                 return false;
1157         cur.idx() = tabular.getCellBelow(cur.idx());
1158         resetPos(cur);
1159         activateCellInset(cur, cur.idx(), cur.x_target());
1160         return true;
1161 }
1162
1163
1164 bool InsetTabular::moveNextCell(LCursor & cur)
1165 {
1166         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1167         if (isRightToLeft(cur)) {
1168                 if (tabular.isFirstCellInRow(cur.idx())) {
1169                         int row = tabular.row_of_cell(cur.idx());
1170                         if (row == tabular.rows() - 1)
1171                                 return false;
1172                         cur.idx() = tabular.getLastCellInRow(row);
1173                         cur.idx() = tabular.getCellBelow(cur.idx());
1174                 } else {
1175                         if (cur.idx() == 0)
1176                                 return false;
1177                         --cur.idx();
1178                 }
1179         } else {
1180                 if (tabular.isLastCell(cur.idx()))
1181                         return false;
1182                 ++cur.idx();
1183         }
1184         cur.par() = 0;
1185         cur.pos() = 0;
1186         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1187         resetPos(cur);
1188         return true;
1189 }
1190
1191
1192 bool InsetTabular::movePrevCell(LCursor & cur)
1193 {
1194         if (isRightToLeft(cur)) {
1195                 if (tabular.isLastCellInRow(cur.idx())) {
1196                         int row = tabular.row_of_cell(cur.idx());
1197                         if (row == 0)
1198                                 return false;
1199                         cur.idx() = tabular.getFirstCellInRow(row);
1200                         cur.idx() = tabular.getCellAbove(cur.idx());
1201                 } else {
1202                         if (tabular.isLastCell(cur.idx()))
1203                                 return false;
1204                         ++cur.idx();
1205                 }
1206         } else {
1207                 if (cur.idx() == 0) // first cell
1208                         return false;
1209                 --cur.idx();
1210         }
1211         cur.par() = 0;
1212         cur.pos() = 0;
1213         resetPos(cur);
1214         return true;
1215 }
1216
1217
1218 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1219 {
1220         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1221
1222         int i = 0;
1223         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1224                 string const tmp = tabularFeature[i].feature;
1225
1226                 if (tmp == what.substr(0, tmp.length())) {
1227                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1228                         //tabularFeatures[i].feature.length())) {
1229                         action = tabularFeature[i].action;
1230                         break;
1231                 }
1232         }
1233         if (action == LyXTabular::LAST_ACTION)
1234                 return false;
1235
1236         string const val =
1237                 ltrim(what.substr(tabularFeature[i].feature.length()));
1238         tabularFeatures(cur, action, val);
1239         return true;
1240 }
1241
1242
1243 namespace {
1244
1245 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1246                           string const & special, bool & flag)
1247 {
1248         if (special == "dl_above") {
1249                 ltt.topDL = flag;
1250                 ltt.set = false;
1251         } else if (special == "dl_below") {
1252                 ltt.bottomDL = flag;
1253                 ltt.set = false;
1254         } else if (special == "empty") {
1255                 ltt.empty = flag;
1256                 ltt.set = false;
1257         } else if (flag) {
1258                 ltt.empty = false;
1259                 ltt.set = true;
1260         }
1261 }
1262
1263 } // anon namespace
1264
1265
1266 void InsetTabular::tabularFeatures(LCursor & cur,
1267         LyXTabular::Feature feature, string const & value)
1268 {
1269         BufferView & bv = cur.bv();
1270         int actcell = cur.idx();
1271         int sel_col_start;
1272         int sel_col_end;
1273         int sel_row_start;
1274         int sel_row_end;
1275         bool setLines = false;
1276         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1277         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1278
1279         switch (feature) {
1280
1281         case LyXTabular::M_ALIGN_LEFT:
1282         case LyXTabular::ALIGN_LEFT:
1283                 setAlign = LYX_ALIGN_LEFT;
1284                 break;
1285
1286         case LyXTabular::M_ALIGN_RIGHT:
1287         case LyXTabular::ALIGN_RIGHT:
1288                 setAlign = LYX_ALIGN_RIGHT;
1289                 break;
1290
1291         case LyXTabular::M_ALIGN_CENTER:
1292         case LyXTabular::ALIGN_CENTER:
1293                 setAlign = LYX_ALIGN_CENTER;
1294                 break;
1295
1296         case LyXTabular::ALIGN_BLOCK:
1297                 setAlign = LYX_ALIGN_BLOCK;
1298                 break;
1299
1300         case LyXTabular::M_VALIGN_TOP:
1301         case LyXTabular::VALIGN_TOP:
1302                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1303                 break;
1304
1305         case LyXTabular::M_VALIGN_BOTTOM:
1306         case LyXTabular::VALIGN_BOTTOM:
1307                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1308                 break;
1309
1310         case LyXTabular::M_VALIGN_MIDDLE:
1311         case LyXTabular::VALIGN_MIDDLE:
1312                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1313                 break;
1314
1315         default:
1316                 break;
1317         }
1318
1319         if (hasSelection()) {
1320                 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1321         } else {
1322                 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1323                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1324         }
1325         recordUndo(cur, Undo::ATOMIC);
1326
1327         int row =  tabular.row_of_cell(actcell);
1328         int column = tabular.column_of_cell(actcell);
1329         bool flag = true;
1330         LyXTabular::ltType ltt;
1331
1332         switch (feature) {
1333
1334         case LyXTabular::SET_PWIDTH: {
1335                 LyXLength const len(value);
1336                 tabular.setColumnPWidth(actcell, len);
1337                 if (len.zero()
1338                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1339                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1340                 else if (!len.zero()
1341                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1342                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1343                 break;
1344         }
1345
1346         case LyXTabular::SET_MPWIDTH:
1347                 tabular.setMColumnPWidth(actcell, LyXLength(value));
1348                 break;
1349
1350         case LyXTabular::SET_SPECIAL_COLUMN:
1351         case LyXTabular::SET_SPECIAL_MULTI:
1352                 tabular.setAlignSpecial(actcell,value,feature);
1353                 break;
1354
1355         case LyXTabular::APPEND_ROW:
1356                 // append the row into the tabular
1357                 tabular.appendRow(bv.buffer()->params(), actcell);
1358                 tabular.setOwner(this);
1359                 break;
1360
1361         case LyXTabular::APPEND_COLUMN:
1362                 // append the column into the tabular
1363                 tabular.appendColumn(bv.buffer()->params(), actcell);
1364                 tabular.setOwner(this);
1365                 actcell = tabular.getCellNumber(row, column);
1366                 break;
1367
1368         case LyXTabular::DELETE_ROW:
1369                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1370                         tabular.deleteRow(sel_row_start);
1371                 if (sel_row_start >= tabular.rows())
1372                         --sel_row_start;
1373                 actcell = tabular.getCellNumber(sel_row_start, column);
1374                 clearSelection();
1375                 break;
1376
1377         case LyXTabular::DELETE_COLUMN:
1378                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1379                         tabular.deleteColumn(sel_col_start);
1380                 if (sel_col_start >= tabular.columns())
1381                         --sel_col_start;
1382                 actcell = tabular.getCellNumber(row, sel_col_start);
1383                 clearSelection();
1384                 break;
1385
1386         case LyXTabular::M_TOGGLE_LINE_TOP:
1387                 flag = false;
1388         case LyXTabular::TOGGLE_LINE_TOP: {
1389                 bool lineSet = !tabular.topLine(actcell, flag);
1390                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1391                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1392                                 tabular.setTopLine(
1393                                         tabular.getCellNumber(i, j),
1394                                         lineSet, flag);
1395                 break;
1396         }
1397
1398         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1399                 flag = false;
1400         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1401                 bool lineSet = !tabular.bottomLine(actcell, flag);
1402                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1403                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1404                                 tabular.setBottomLine(
1405                                         tabular.getCellNumber(i, j),
1406                                         lineSet,
1407                                         flag);
1408                 break;
1409         }
1410
1411         case LyXTabular::M_TOGGLE_LINE_LEFT:
1412                 flag = false;
1413         case LyXTabular::TOGGLE_LINE_LEFT: {
1414                 bool lineSet = !tabular.leftLine(actcell, flag);
1415                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1416                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1417                                 tabular.setLeftLine(
1418                                         tabular.getCellNumber(i,j),
1419                                         lineSet,
1420                                         flag);
1421                 break;
1422         }
1423
1424         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1425                 flag = false;
1426         case LyXTabular::TOGGLE_LINE_RIGHT: {
1427                 bool lineSet = !tabular.rightLine(actcell, flag);
1428                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1429                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1430                                 tabular.setRightLine(
1431                                         tabular.getCellNumber(i,j),
1432                                         lineSet,
1433                                         flag);
1434                 break;
1435         }
1436
1437         case LyXTabular::M_ALIGN_LEFT:
1438         case LyXTabular::M_ALIGN_RIGHT:
1439         case LyXTabular::M_ALIGN_CENTER:
1440                 flag = false;
1441         case LyXTabular::ALIGN_LEFT:
1442         case LyXTabular::ALIGN_RIGHT:
1443         case LyXTabular::ALIGN_CENTER:
1444         case LyXTabular::ALIGN_BLOCK:
1445                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1446                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1447                                 tabular.setAlignment(
1448                                         tabular.getCellNumber(i, j),
1449                                         setAlign,
1450                                         flag);
1451                 break;
1452
1453         case LyXTabular::M_VALIGN_TOP:
1454         case LyXTabular::M_VALIGN_BOTTOM:
1455         case LyXTabular::M_VALIGN_MIDDLE:
1456                 flag = false;
1457         case LyXTabular::VALIGN_TOP:
1458         case LyXTabular::VALIGN_BOTTOM:
1459         case LyXTabular::VALIGN_MIDDLE:
1460                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1461                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1462                                 tabular.setVAlignment(
1463                                         tabular.getCellNumber(i, j),
1464                                         setVAlign, flag);
1465                 break;
1466
1467         case LyXTabular::MULTICOLUMN: {
1468                 if (sel_row_start != sel_row_end) {
1469 #ifdef WITH_WARNINGS
1470 #warning Need I say it ? This is horrible.
1471 #endif
1472                         Alert::error(_("Error setting multicolumn"),
1473                                    _("You cannot set multicolumn vertically."));
1474                         return;
1475                 }
1476                 // just multicol for one Single Cell
1477                 if (!hasSelection()) {
1478                         // check wether we are completly in a multicol
1479                         if (tabular.isMultiColumn(actcell))
1480                                 tabular.unsetMultiColumn(actcell);
1481                         else
1482                                 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1483                         break;
1484                 }
1485                 // we have a selection so this means we just add all this
1486                 // cells to form a multicolumn cell
1487                 int s_start;
1488                 int s_end;
1489
1490                 if (sel_cell_start > sel_cell_end) {
1491                         s_start = sel_cell_end;
1492                         s_end = sel_cell_start;
1493                 } else {
1494                         s_start = sel_cell_start;
1495                         s_end = sel_cell_end;
1496                 }
1497                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1498                 actcell = s_start;
1499                 clearSelection();
1500                 break;
1501         }
1502
1503         case LyXTabular::SET_ALL_LINES:
1504                 setLines = true;
1505         case LyXTabular::UNSET_ALL_LINES:
1506                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1507                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1508                                 tabular.setAllLines(
1509                                         tabular.getCellNumber(i,j), setLines);
1510                 break;
1511
1512         case LyXTabular::SET_LONGTABULAR:
1513                 tabular.setLongTabular(true);
1514                 break;
1515
1516         case LyXTabular::UNSET_LONGTABULAR:
1517                 tabular.setLongTabular(false);
1518                 break;
1519
1520         case LyXTabular::SET_ROTATE_TABULAR:
1521                 tabular.setRotateTabular(true);
1522                 break;
1523
1524         case LyXTabular::UNSET_ROTATE_TABULAR:
1525                 tabular.setRotateTabular(false);
1526                 break;
1527
1528         case LyXTabular::SET_ROTATE_CELL:
1529                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1530                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1531                                 tabular.setRotateCell(
1532                                         tabular.getCellNumber(i, j), true);
1533                 break;
1534
1535         case LyXTabular::UNSET_ROTATE_CELL:
1536                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1537                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1538                                 tabular.setRotateCell(
1539                                         tabular.getCellNumber(i, j), false);
1540                 break;
1541
1542         case LyXTabular::SET_USEBOX: {
1543                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1544                 if (val == tabular.getUsebox(actcell))
1545                         val = LyXTabular::BOX_NONE;
1546                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1547                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1548                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1549                 break;
1550         }
1551
1552         case LyXTabular::UNSET_LTFIRSTHEAD:
1553                 flag = false;
1554         case LyXTabular::SET_LTFIRSTHEAD:
1555                 tabular.getRowOfLTFirstHead(row, ltt);
1556                 checkLongtableSpecial(ltt, value, flag);
1557                 tabular.setLTHead(row, flag, ltt, true);
1558                 break;
1559
1560         case LyXTabular::UNSET_LTHEAD:
1561                 flag = false;
1562         case LyXTabular::SET_LTHEAD:
1563                 tabular.getRowOfLTHead(row, ltt);
1564                 checkLongtableSpecial(ltt, value, flag);
1565                 tabular.setLTHead(row, flag, ltt, false);
1566                 break;
1567
1568         case LyXTabular::UNSET_LTFOOT:
1569                 flag = false;
1570         case LyXTabular::SET_LTFOOT:
1571                 tabular.getRowOfLTFoot(row, ltt);
1572                 checkLongtableSpecial(ltt, value, flag);
1573                 tabular.setLTFoot(row, flag, ltt, false);
1574                 break;
1575
1576         case LyXTabular::UNSET_LTLASTFOOT:
1577                 flag = false;
1578         case LyXTabular::SET_LTLASTFOOT:
1579                 tabular.getRowOfLTLastFoot(row, ltt);
1580                 checkLongtableSpecial(ltt, value, flag);
1581                 tabular.setLTFoot(row, flag, ltt, true);
1582                 break;
1583
1584         case LyXTabular::SET_LTNEWPAGE:
1585                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1586                 break;
1587
1588         // dummy stuff just to avoid warnings
1589         case LyXTabular::LAST_ACTION:
1590                 break;
1591         }
1592
1593         updateLocal(cur);
1594         InsetTabularMailer(*this).updateDialog(&bv);
1595 }
1596
1597
1598 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1599 {
1600         tabular.getCellInset(cell).edit(cur, x, y);
1601         cur.idx() = cell;
1602         updateLocal(cur);
1603 }
1604
1605
1606 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1607 {
1608         tabular.getCellInset(cell).edit(cur, behind);
1609         cur.idx() = cell;
1610         updateLocal(cur);
1611 }
1612
1613
1614 bool InsetTabular::showInsetDialog(BufferView * bv) const
1615 {
1616         InsetTabularMailer(*this).showDialog(bv);
1617         return true;
1618 }
1619
1620
1621 void InsetTabular::openLayoutDialog(BufferView * bv) const
1622 {
1623         InsetTabularMailer(*this).showDialog(bv);
1624 }
1625
1626
1627 //
1628 // function returns an object as defined in func_status.h:
1629 // states OK, Unknown, Disabled, On, Off.
1630 //
1631 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1632 {
1633         FuncStatus status;
1634         int action = LyXTabular::LAST_ACTION;
1635
1636         int i = 0;
1637         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1638                 string const tmp = tabularFeature[i].feature;
1639                 if (tmp == what.substr(0, tmp.length())) {
1640                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1641                         //   tabularFeatures[i].feature.length()))
1642                         action = tabularFeature[i].action;
1643                         break;
1644                 }
1645         }
1646         if (action == LyXTabular::LAST_ACTION) {
1647                 status.clear();
1648                 status.unknown(true);
1649                 return status;
1650         }
1651
1652         string const argument
1653                 = ltrim(what.substr(tabularFeature[i].feature.length()));
1654
1655         int sel_row_start;
1656         int sel_row_end;
1657         int dummy;
1658         LyXTabular::ltType dummyltt;
1659         bool flag = true;
1660
1661         if (hasSelection())
1662                 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1663         else
1664                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1665
1666         switch (action) {
1667         case LyXTabular::SET_PWIDTH:
1668         case LyXTabular::SET_MPWIDTH:
1669         case LyXTabular::SET_SPECIAL_COLUMN:
1670         case LyXTabular::SET_SPECIAL_MULTI:
1671         case LyXTabular::APPEND_ROW:
1672         case LyXTabular::APPEND_COLUMN:
1673         case LyXTabular::DELETE_ROW:
1674         case LyXTabular::DELETE_COLUMN:
1675         case LyXTabular::SET_ALL_LINES:
1676         case LyXTabular::UNSET_ALL_LINES:
1677                 status.clear();
1678                 return status;
1679
1680         case LyXTabular::MULTICOLUMN:
1681                 status.setOnOff(tabular.isMultiColumn(actcell));
1682                 break;
1683
1684         case LyXTabular::M_TOGGLE_LINE_TOP:
1685                 flag = false;
1686         case LyXTabular::TOGGLE_LINE_TOP:
1687                 status.setOnOff(tabular.topLine(actcell, flag));
1688                 break;
1689
1690         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1691                 flag = false;
1692         case LyXTabular::TOGGLE_LINE_BOTTOM:
1693                 status.setOnOff(tabular.bottomLine(actcell, flag));
1694                 break;
1695
1696         case LyXTabular::M_TOGGLE_LINE_LEFT:
1697                 flag = false;
1698         case LyXTabular::TOGGLE_LINE_LEFT:
1699                 status.setOnOff(tabular.leftLine(actcell, flag));
1700                 break;
1701
1702         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1703                 flag = false;
1704         case LyXTabular::TOGGLE_LINE_RIGHT:
1705                 status.setOnOff(tabular.rightLine(actcell, flag));
1706                 break;
1707
1708         case LyXTabular::M_ALIGN_LEFT:
1709                 flag = false;
1710         case LyXTabular::ALIGN_LEFT:
1711                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1712                 break;
1713
1714         case LyXTabular::M_ALIGN_RIGHT:
1715                 flag = false;
1716         case LyXTabular::ALIGN_RIGHT:
1717                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1718                 break;
1719
1720         case LyXTabular::M_ALIGN_CENTER:
1721                 flag = false;
1722         case LyXTabular::ALIGN_CENTER:
1723                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1724                 break;
1725
1726         case LyXTabular::ALIGN_BLOCK:
1727                 status.disabled(tabular.getPWidth(actcell).zero());
1728                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1729                 break;
1730
1731         case LyXTabular::M_VALIGN_TOP:
1732                 flag = false;
1733         case LyXTabular::VALIGN_TOP:
1734                 status.setOnOff(
1735                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1736                 break;
1737
1738         case LyXTabular::M_VALIGN_BOTTOM:
1739                 flag = false;
1740         case LyXTabular::VALIGN_BOTTOM:
1741                 status.setOnOff(
1742                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1743                 break;
1744
1745         case LyXTabular::M_VALIGN_MIDDLE:
1746                 flag = false;
1747         case LyXTabular::VALIGN_MIDDLE:
1748                 status.setOnOff(
1749                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1750                 break;
1751
1752         case LyXTabular::SET_LONGTABULAR:
1753                 status.setOnOff(tabular.isLongTabular());
1754                 break;
1755
1756         case LyXTabular::UNSET_LONGTABULAR:
1757                 status.setOnOff(!tabular.isLongTabular());
1758                 break;
1759
1760         case LyXTabular::SET_ROTATE_TABULAR:
1761                 status.setOnOff(tabular.getRotateTabular());
1762                 break;
1763
1764         case LyXTabular::UNSET_ROTATE_TABULAR:
1765                 status.setOnOff(!tabular.getRotateTabular());
1766                 break;
1767
1768         case LyXTabular::SET_ROTATE_CELL:
1769                 status.setOnOff(tabular.getRotateCell(actcell));
1770                 break;
1771
1772         case LyXTabular::UNSET_ROTATE_CELL:
1773                 status.setOnOff(!tabular.getRotateCell(actcell));
1774                 break;
1775
1776         case LyXTabular::SET_USEBOX:
1777                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1778                 break;
1779
1780         case LyXTabular::SET_LTFIRSTHEAD:
1781                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1782                 break;
1783
1784         case LyXTabular::SET_LTHEAD:
1785                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1786                 break;
1787
1788         case LyXTabular::SET_LTFOOT:
1789                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1790                 break;
1791
1792         case LyXTabular::SET_LTLASTFOOT:
1793                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1794                 break;
1795
1796         case LyXTabular::SET_LTNEWPAGE:
1797                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1798                 break;
1799
1800         default:
1801                 status.clear();
1802                 status.disabled(true);
1803                 break;
1804         }
1805         return status;
1806 }
1807
1808
1809 void InsetTabular::getLabelList(Buffer const & buffer,
1810                                 vector<string> & list) const
1811 {
1812         tabular.getLabelList(buffer, list);
1813 }
1814
1815
1816 bool InsetTabular::copySelection(BufferView & bv)
1817 {
1818         if (!hasSelection())
1819                 return false;
1820
1821         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1822         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1823         if (sel_col_start > sel_col_end) {
1824                 sel_col_start = sel_col_end;
1825                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1826         } else {
1827                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1828         }
1829
1830         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1831         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1832         if (sel_row_start > sel_row_end)
1833                 swap(sel_row_start, sel_row_end);
1834
1835         paste_tabular.reset(new LyXTabular(tabular));
1836         paste_tabular->setOwner(this);
1837
1838         for (int i = 0; i < sel_row_start; ++i)
1839                 paste_tabular->deleteRow(0);
1840
1841         int const rows = sel_row_end - sel_row_start + 1;
1842         while (paste_tabular->rows() > rows)
1843                 paste_tabular->deleteRow(rows);
1844
1845         paste_tabular->setTopLine(0, true, true);
1846         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1847                                      true, true);
1848
1849         for (int i = 0; i < sel_col_start; ++i)
1850                 paste_tabular->deleteColumn(0);
1851
1852         int const columns = sel_col_end - sel_col_start + 1;
1853         while (paste_tabular->columns() > columns)
1854                 paste_tabular->deleteColumn(columns);
1855
1856         paste_tabular->setLeftLine(0, true, true);
1857         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1858                                     true, true);
1859
1860         ostringstream os;
1861         OutputParams const runparams;
1862         paste_tabular->plaintext(*bv.buffer(), os, runparams,
1863                                  ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1864         bv.stuffClipboard(os.str());
1865         return true;
1866 }
1867
1868
1869 bool InsetTabular::pasteSelection(BufferView & bv)
1870 {
1871         if (!paste_tabular)
1872                 return false;
1873         int actcell = bv.cursor().idx();
1874         int actcol = tabular.column_of_cell(actcell);
1875         int actrow = tabular.row_of_cell(actcell);
1876         for (int r1 = 0, r2 = actrow;
1877              r1 < paste_tabular->rows() && r2 < tabular.rows();
1878              ++r1, ++r2) {
1879                 for (int c1 = 0, c2 = actcol;
1880                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1881                     ++c1, ++c2) {
1882                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1883                             tabular.isPartOfMultiColumn(r2, c2))
1884                                 continue;
1885                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1886                                 --c2;
1887                                 continue;
1888                         }
1889                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1890                                 --c1;
1891                                 continue;
1892                         }
1893                         InsetText & inset = tabular.getCellInset(r2, c2);
1894                         inset = paste_tabular->getCellInset(r1, c1);
1895                         inset.setOwner(this);
1896                         inset.markNew();
1897                 }
1898         }
1899         return true;
1900 }
1901
1902
1903 bool InsetTabular::cutSelection(BufferParams const & bp)
1904 {
1905         if (!hasSelection())
1906                 return false;
1907
1908         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1909         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1910         if (sel_col_start > sel_col_end) {
1911                 sel_col_start = sel_col_end;
1912                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1913         } else {
1914                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1915         }
1916
1917         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1918         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1919
1920         if (sel_row_start > sel_row_end)
1921                 swap(sel_row_start, sel_row_end);
1922
1923         if (sel_cell_start > sel_cell_end)
1924                 swap(sel_cell_start, sel_cell_end);
1925
1926         for (int i = sel_row_start; i <= sel_row_end; ++i)
1927                 for (int j = sel_col_start; j <= sel_col_end; ++j)
1928                         tabular.getCellInset(tabular.getCellNumber(i, j))
1929                                 .clear(bp.tracking_changes);
1930         return true;
1931 }
1932
1933
1934 bool InsetTabular::isRightToLeft(LCursor & cur)
1935 {
1936         return cur.bv().getParentLanguage(this)->RightToLeft();
1937 }
1938
1939
1940 void InsetTabular::getSelection(int actcell,
1941         int & srow, int & erow, int & scol, int & ecol) const
1942 {
1943         int const start = hasSelection() ? sel_cell_start : actcell;
1944         int const end = hasSelection() ? sel_cell_end : actcell;
1945
1946         srow = tabular.row_of_cell(start);
1947         erow = tabular.row_of_cell(end);
1948         if (srow > erow)
1949                 swap(srow, erow);
1950
1951         scol = tabular.column_of_cell(start);
1952         ecol = tabular.column_of_cell(end);
1953         if (scol > ecol)
1954                 swap(scol, ecol);
1955         else
1956                 ecol = tabular.right_column_of_cell(end);
1957 }
1958
1959
1960 int InsetTabular::numParagraphs() const
1961 {
1962         return tabular.getNumberOfCells();
1963 }
1964
1965
1966 LyXText * InsetTabular::getText(int i) const
1967 {
1968         return i < tabular.getNumberOfCells()
1969                 ? tabular.getCellInset(i).getText(0)
1970                 : 0;
1971 }
1972
1973
1974 void InsetTabular::markErased()
1975 {
1976         for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1977                 tabular.getCellInset(cell).markErased();
1978 }
1979
1980
1981 bool InsetTabular::forceDefaultParagraphs(InsetBase const * in) const
1982 {
1983         const int cell = tabular.getCellFromInset(in);
1984
1985         if (cell != -1)
1986                 return tabular.getPWidth(cell).zero();
1987
1988         // this is a workaround for a crash (New, Insert->Tabular,
1989         // Insert->FootNote)
1990         if (!owner())
1991                 return false;
1992
1993         // well we didn't obviously find it so maybe our owner knows more
1994         BOOST_ASSERT(owner());
1995         return owner()->forceDefaultParagraphs(in);
1996 }
1997
1998
1999 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2000                                      bool usePaste)
2001 {
2002         if (buf.length() <= 0)
2003                 return true;
2004
2005         int cols = 1;
2006         int rows = 1;
2007         int maxCols = 1;
2008         string::size_type len = buf.length();
2009         string::size_type p = 0;
2010
2011         int actcell = bv.cursor().idx();
2012         int actcol = tabular.column_of_cell(actcell);
2013         int actrow = tabular.row_of_cell(actcell);
2014
2015         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2016                 switch (buf[p]) {
2017                 case '\t':
2018                         ++cols;
2019                         break;
2020                 case '\n':
2021                         if (p + 1 < len)
2022                                 ++rows;
2023                         maxCols = max(cols, maxCols);
2024                         cols = 1;
2025                         break;
2026                 }
2027                 ++p;
2028         }
2029         maxCols = max(cols, maxCols);
2030         LyXTabular * loctab;
2031         int cell = 0;
2032         int ocol = 0;
2033         int row = 0;
2034         if (usePaste) {
2035                 paste_tabular.reset(
2036                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
2037                 paste_tabular->setOwner(this);
2038                 loctab = paste_tabular.get();
2039                 cols = 0;
2040         } else {
2041                 loctab = &tabular;
2042                 cell = actcell;
2043                 ocol = actcol;
2044                 row = actrow;
2045         }
2046
2047         string::size_type op = 0;
2048         int cells = loctab->getNumberOfCells();
2049         p = 0;
2050         cols = ocol;
2051         rows = loctab->rows();
2052         int const columns = loctab->columns();
2053
2054         while (cell < cells && p < len && row < rows &&
2055                (p = buf.find_first_of("\t\n", p)) != string::npos)
2056         {
2057                 if (p >= len)
2058                         break;
2059                 switch (buf[p]) {
2060                 case '\t':
2061                         // we can only set this if we are not too far right
2062                         if (cols < columns) {
2063                                 InsetText & inset = loctab->getCellInset(cell);
2064                                 LyXFont const font = inset.text_.
2065                                         getFont(inset.paragraphs().begin(), 0);
2066                                 inset.setText(buf.substr(op, p - op), font);
2067                                 ++cols;
2068                                 ++cell;
2069                         }
2070                         break;
2071                 case '\n':
2072                         // we can only set this if we are not too far right
2073                         if (cols < columns) {
2074                                 InsetText & inset = tabular.getCellInset(cell);
2075                                 LyXFont const font = inset.text_.
2076                                         getFont(inset.paragraphs().begin(), 0);
2077                                 inset.setText(buf.substr(op, p - op), font);
2078                         }
2079                         cols = ocol;
2080                         ++row;
2081                         if (row < rows)
2082                                 cell = loctab->getCellNumber(row, cols);
2083                         break;
2084                 }
2085                 ++p;
2086                 op = p;
2087         }
2088         // check for the last cell if there is no trailing '\n'
2089         if (cell < cells && op < len) {
2090                 InsetText & inset = loctab->getCellInset(cell);
2091                 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2092                 inset.setText(buf.substr(op, len - op), font);
2093         }
2094         return true;
2095 }
2096
2097
2098 void InsetTabular::addPreview(PreviewLoader & loader) const
2099 {
2100         int const rows = tabular.rows();
2101         int const columns = tabular.columns();
2102         for (int i = 0; i < rows; ++i) {
2103                 for (int j = 0; j < columns; ++j)
2104                         tabular.getCellInset(i, j).addPreview(loader);
2105         }
2106 }
2107
2108
2109
2110 void InsetTabular::clearSelection() const
2111 {
2112         sel_cell_start = 0;
2113         sel_cell_end = 0;
2114         has_selection = false;
2115 }
2116
2117
2118 void InsetTabular::setSelection(int start, int end) const
2119 {
2120         sel_cell_start = start;
2121         sel_cell_end = end;
2122         has_selection = true;
2123 }
2124
2125
2126 string const InsetTabularMailer::name_("tabular");
2127
2128 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2129         : inset_(const_cast<InsetTabular &>(inset))
2130 {}
2131
2132
2133 string const InsetTabularMailer::inset2string(Buffer const &) const
2134 {
2135         return params2string(inset_);
2136 }
2137
2138
2139 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2140 {
2141         istringstream data(in);
2142         LyXLex lex(0,0);
2143         lex.setStream(data);
2144
2145 #warning CHECK verify that this is a sane value to return.
2146         if (in.empty())
2147                 return -1;
2148
2149         if (lex.isOK()) {
2150                 lex.next();
2151                 string const token = lex.getString();
2152                 if (token != name_)
2153                         return -1;
2154         }
2155
2156         int cell = -1;
2157         if (lex.isOK()) {
2158                 lex.next();
2159                 string const token = lex.getString();
2160                 if (token != "\\active_cell")
2161                         return -1;
2162                 lex.next();
2163                 cell = lex.getInteger();
2164         }
2165
2166         // This is part of the inset proper that is usually swallowed
2167         // by Buffer::readInset
2168         if (lex.isOK()) {
2169                 lex.next();
2170                 string const token = lex.getString();
2171                 if (token != "Tabular")
2172                         return -1;
2173         }
2174
2175         if (!lex.isOK())
2176                 return -1;
2177
2178         Buffer const & buffer = inset.buffer();
2179         inset.read(buffer, lex);
2180
2181         // We can't set the active cell, but we can tell the frontend
2182         // what it is.
2183         return cell;
2184 }
2185
2186
2187 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2188 {
2189         ostringstream data;
2190 #warning wrong!
2191         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2192         data << name_ << " \\active_cell " << 0 << '\n';
2193         inset.write(inset.buffer(), data);
2194         data << "\\end_inset\n";
2195         return data.str();
2196 }