]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
bug + spped fixes + small stuff
[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 "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         lyxerr << "# InsetTabular::setPos()  cursor: " << bv.cursor() << endl;
978         int const cell = getCell(x + xo_, y + yo_);
979         lyxerr << "# InsetTabular::setPos()  cell: " << cell << endl;
980         InsetText const & inset = tabular.getCellInset(cell);
981         inset.text_.setCursorFromCoordinates(x, y);
982 #if 0
983         cursory_ = 0;
984         int actcell = 0;
985         int actrow = 0;
986         int actcol = 0;
987         int ly = tabular.getDescentOfRow(actrow);
988
989         // first search the right row
990         while (ly < y && actrow + 1 < tabular.rows()) {
991                 cursory_ += tabular.getDescentOfRow(actrow) +
992                                  tabular.getAscentOfRow(actrow + 1) +
993                                  tabular.getAdditionalHeight(actrow + 1);
994                 ++actrow;
995                 ly = cursory_ + tabular.getDescentOfRow(actrow);
996         }
997         actcell = tabular.getCellNumber(actrow, actcol);
998
999         // now search the right column
1000         int lx = tabular.getWidthOfColumn(actcell) -
1001                 tabular.getAdditionalWidth(actcell);
1002
1003         for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1004                 lx += tabular.getWidthOfColumn(actcell + 1)
1005                         + tabular.getAdditionalWidth(actcell);
1006
1007         cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1008 #endif
1009         resetPos(bv.cursor());
1010 }
1011
1012
1013 int InsetTabular::getCellXPos(int cell) const
1014 {
1015         int c = cell;
1016
1017         for (; !tabular.isFirstCellInRow(c); --c)
1018                 ;
1019         int lx = tabular.getWidthOfColumn(cell);
1020         for (; c < cell; ++c)
1021                 lx += tabular.getWidthOfColumn(c);
1022
1023         return lx - tabular.getWidthOfColumn(cell) + xo_;
1024 }
1025
1026
1027 void InsetTabular::resetPos(LCursor &) const
1028 {
1029 #if 0
1030 #ifdef WITH_WARNINGS
1031 #warning This should be fixed in the right manner (20011128 Jug)
1032 #endif
1033         // fast hack to fix infinite repaintings!
1034         if (in_reset_pos > 0)
1035                 return;
1036
1037         BufferView & bv = cur.bv();
1038         int cell = 0;
1039         int actcell = cur.cell();
1040         int actcol = tabular.column_of_cell(actcell);
1041         int actrow = 0;
1042         cursory_ = 0;
1043         for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1044                 if (tabular.isLastCellInRow(cell)) {
1045                         cursory_ += tabular.getDescentOfRow(actrow) +
1046                                          tabular.getAscentOfRow(actrow + 1) +
1047                                          tabular.getAdditionalHeight(actrow + 1);
1048                         ++actrow;
1049                 }
1050         }
1051
1052         // we need this only from here on!!!
1053         ++in_reset_pos;
1054         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1055         int new_x = getCellXPos(actcell) + offset;
1056         int old_x = cursorx_;
1057         cursorx_ = new_x;
1058 //    cursor.x(getCellXPos(actcell) + offset);
1059         if (actcol < tabular.columns() - 1 && scroll(false) &&
1060                 tabular.getWidthOfTabular() < bv->workWidth()-20)
1061         {
1062                 scroll(bv, 0.0F);
1063                 updateLocal(cur);
1064         } else if (cursorx_ - offset > 20 &&
1065                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1066                    > bv.workWidth() - 20) {
1067                 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1068                 updateLocal(cur);
1069         } else if (cursorx_ - offset < 20) {
1070                 scroll(&bv, 20 - cursorx_ + offset);
1071                 updateLocal(cur);
1072         } else if (scroll() && xo_ > 20 &&
1073                    xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1074                 scroll(&bv, old_x - cursorx_);
1075                 updateLocal(cur);
1076         }
1077         InsetTabularMailer(*this).updateDialog(bv);
1078         in_reset_pos = 0;
1079 #endif
1080 }
1081
1082
1083 bool InsetTabular::moveRight(LCursor & cur)
1084 {
1085         bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1086         if (!moved)
1087                 return false;
1088         resetPos(cur);
1089         return true;
1090 }
1091
1092
1093 bool InsetTabular::moveRightLock(LCursor & cur)
1094 {
1095         bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1096         if (!moved)
1097                 return false;
1098         activateCellInset(cur, cur.idx(), false);
1099         return true;
1100 }
1101
1102
1103 bool InsetTabular::moveLeft(LCursor & cur)
1104 {
1105         bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1106         if (!moved)
1107                 return false;
1108         resetPos(cur);
1109         return true;
1110 }
1111
1112
1113 bool InsetTabular::moveLeftLock(LCursor & cur)
1114 {
1115         bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1116         if (!moved)
1117                 return false;
1118         activateCellInset(cur, cur.idx(), true);
1119         return true;
1120 }
1121
1122
1123 bool InsetTabular::moveUp(LCursor & cur)
1124 {
1125         if (tabular.row_of_cell(cur.idx()) == 0)
1126                 return false;
1127         cur.idx() = tabular.getCellAbove(cur.idx());
1128         resetPos(cur);
1129         return true;
1130 }
1131
1132
1133 bool InsetTabular::moveUpLock(LCursor & cur)
1134 {
1135         if (tabular.row_of_cell(cur.idx()) == 0)
1136                 return false;
1137         cur.idx() = tabular.getCellAbove(cur.idx());
1138         resetPos(cur);
1139         activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1140         return true;
1141 }
1142
1143
1144 bool InsetTabular::moveDown(LCursor & cur)
1145 {
1146         if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1147                 return false;
1148         cur.idx() = tabular.getCellBelow(cur.idx());
1149         resetPos(cur);
1150         return true;
1151 }
1152
1153
1154 bool InsetTabular::moveDownLock(LCursor & cur)
1155 {
1156         if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1157                 return false;
1158         cur.idx() = tabular.getCellBelow(cur.idx());
1159         resetPos(cur);
1160         activateCellInset(cur, cur.idx(), cur.x_target());
1161         return true;
1162 }
1163
1164
1165 bool InsetTabular::moveNextCell(LCursor & cur)
1166 {
1167         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1168         if (isRightToLeft(cur)) {
1169                 if (tabular.isFirstCellInRow(cur.idx())) {
1170                         int row = tabular.row_of_cell(cur.idx());
1171                         if (row == tabular.rows() - 1)
1172                                 return false;
1173                         cur.idx() = tabular.getLastCellInRow(row);
1174                         cur.idx() = tabular.getCellBelow(cur.idx());
1175                 } else {
1176                         if (cur.idx() == 0)
1177                                 return false;
1178                         --cur.idx();
1179                 }
1180         } else {
1181                 if (tabular.isLastCell(cur.idx()))
1182                         return false;
1183                 ++cur.idx();
1184         }
1185         cur.par() = 0;
1186         cur.pos() = 0;
1187         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1188         resetPos(cur);
1189         return true;
1190 }
1191
1192
1193 bool InsetTabular::movePrevCell(LCursor & cur)
1194 {
1195         if (isRightToLeft(cur)) {
1196                 if (tabular.isLastCellInRow(cur.idx())) {
1197                         int row = tabular.row_of_cell(cur.idx());
1198                         if (row == 0)
1199                                 return false;
1200                         cur.idx() = tabular.getFirstCellInRow(row);
1201                         cur.idx() = tabular.getCellAbove(cur.idx());
1202                 } else {
1203                         if (tabular.isLastCell(cur.idx()))
1204                                 return false;
1205                         ++cur.idx();
1206                 }
1207         } else {
1208                 if (cur.idx() == 0) // first cell
1209                         return false;
1210                 --cur.idx();
1211         }
1212         cur.par() = 0;
1213         cur.pos() = 0;
1214         resetPos(cur);
1215         return true;
1216 }
1217
1218
1219 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1220 {
1221         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1222
1223         int i = 0;
1224         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1225                 string const tmp = tabularFeature[i].feature;
1226
1227                 if (tmp == what.substr(0, tmp.length())) {
1228                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1229                         //tabularFeatures[i].feature.length())) {
1230                         action = tabularFeature[i].action;
1231                         break;
1232                 }
1233         }
1234         if (action == LyXTabular::LAST_ACTION)
1235                 return false;
1236
1237         string const val =
1238                 ltrim(what.substr(tabularFeature[i].feature.length()));
1239         tabularFeatures(cur, action, val);
1240         return true;
1241 }
1242
1243
1244 namespace {
1245
1246 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1247                           string const & special, bool & flag)
1248 {
1249         if (special == "dl_above") {
1250                 ltt.topDL = flag;
1251                 ltt.set = false;
1252         } else if (special == "dl_below") {
1253                 ltt.bottomDL = flag;
1254                 ltt.set = false;
1255         } else if (special == "empty") {
1256                 ltt.empty = flag;
1257                 ltt.set = false;
1258         } else if (flag) {
1259                 ltt.empty = false;
1260                 ltt.set = true;
1261         }
1262 }
1263
1264 } // anon namespace
1265
1266
1267 void InsetTabular::tabularFeatures(LCursor & cur,
1268         LyXTabular::Feature feature, string const & value)
1269 {
1270         BufferView & bv = cur.bv();
1271         int actcell = cur.idx();
1272         int sel_col_start;
1273         int sel_col_end;
1274         int sel_row_start;
1275         int sel_row_end;
1276         bool setLines = false;
1277         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1278         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1279
1280         switch (feature) {
1281
1282         case LyXTabular::M_ALIGN_LEFT:
1283         case LyXTabular::ALIGN_LEFT:
1284                 setAlign = LYX_ALIGN_LEFT;
1285                 break;
1286
1287         case LyXTabular::M_ALIGN_RIGHT:
1288         case LyXTabular::ALIGN_RIGHT:
1289                 setAlign = LYX_ALIGN_RIGHT;
1290                 break;
1291
1292         case LyXTabular::M_ALIGN_CENTER:
1293         case LyXTabular::ALIGN_CENTER:
1294                 setAlign = LYX_ALIGN_CENTER;
1295                 break;
1296
1297         case LyXTabular::ALIGN_BLOCK:
1298                 setAlign = LYX_ALIGN_BLOCK;
1299                 break;
1300
1301         case LyXTabular::M_VALIGN_TOP:
1302         case LyXTabular::VALIGN_TOP:
1303                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1304                 break;
1305
1306         case LyXTabular::M_VALIGN_BOTTOM:
1307         case LyXTabular::VALIGN_BOTTOM:
1308                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1309                 break;
1310
1311         case LyXTabular::M_VALIGN_MIDDLE:
1312         case LyXTabular::VALIGN_MIDDLE:
1313                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1314                 break;
1315
1316         default:
1317                 break;
1318         }
1319
1320         if (hasSelection()) {
1321                 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1322         } else {
1323                 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1324                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1325         }
1326         recordUndo(cur, Undo::ATOMIC);
1327
1328         int row =  tabular.row_of_cell(actcell);
1329         int column = tabular.column_of_cell(actcell);
1330         bool flag = true;
1331         LyXTabular::ltType ltt;
1332
1333         switch (feature) {
1334
1335         case LyXTabular::SET_PWIDTH: {
1336                 LyXLength const len(value);
1337                 tabular.setColumnPWidth(actcell, len);
1338                 if (len.zero()
1339                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1340                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1341                 else if (!len.zero()
1342                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1343                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1344                 break;
1345         }
1346
1347         case LyXTabular::SET_MPWIDTH:
1348                 tabular.setMColumnPWidth(actcell, LyXLength(value));
1349                 break;
1350
1351         case LyXTabular::SET_SPECIAL_COLUMN:
1352         case LyXTabular::SET_SPECIAL_MULTI:
1353                 tabular.setAlignSpecial(actcell,value,feature);
1354                 break;
1355
1356         case LyXTabular::APPEND_ROW:
1357                 // append the row into the tabular
1358                 tabular.appendRow(bv.buffer()->params(), actcell);
1359                 tabular.setOwner(this);
1360                 break;
1361
1362         case LyXTabular::APPEND_COLUMN:
1363                 // append the column into the tabular
1364                 tabular.appendColumn(bv.buffer()->params(), actcell);
1365                 tabular.setOwner(this);
1366                 actcell = tabular.getCellNumber(row, column);
1367                 break;
1368
1369         case LyXTabular::DELETE_ROW:
1370                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1371                         tabular.deleteRow(sel_row_start);
1372                 if (sel_row_start >= tabular.rows())
1373                         --sel_row_start;
1374                 actcell = tabular.getCellNumber(sel_row_start, column);
1375                 clearSelection();
1376                 break;
1377
1378         case LyXTabular::DELETE_COLUMN:
1379                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1380                         tabular.deleteColumn(sel_col_start);
1381                 if (sel_col_start >= tabular.columns())
1382                         --sel_col_start;
1383                 actcell = tabular.getCellNumber(row, sel_col_start);
1384                 clearSelection();
1385                 break;
1386
1387         case LyXTabular::M_TOGGLE_LINE_TOP:
1388                 flag = false;
1389         case LyXTabular::TOGGLE_LINE_TOP: {
1390                 bool lineSet = !tabular.topLine(actcell, flag);
1391                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1392                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1393                                 tabular.setTopLine(
1394                                         tabular.getCellNumber(i, j),
1395                                         lineSet, flag);
1396                 break;
1397         }
1398
1399         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1400                 flag = false;
1401         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1402                 bool lineSet = !tabular.bottomLine(actcell, flag);
1403                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1404                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1405                                 tabular.setBottomLine(
1406                                         tabular.getCellNumber(i, j),
1407                                         lineSet,
1408                                         flag);
1409                 break;
1410         }
1411
1412         case LyXTabular::M_TOGGLE_LINE_LEFT:
1413                 flag = false;
1414         case LyXTabular::TOGGLE_LINE_LEFT: {
1415                 bool lineSet = !tabular.leftLine(actcell, flag);
1416                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1417                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1418                                 tabular.setLeftLine(
1419                                         tabular.getCellNumber(i,j),
1420                                         lineSet,
1421                                         flag);
1422                 break;
1423         }
1424
1425         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1426                 flag = false;
1427         case LyXTabular::TOGGLE_LINE_RIGHT: {
1428                 bool lineSet = !tabular.rightLine(actcell, flag);
1429                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1430                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1431                                 tabular.setRightLine(
1432                                         tabular.getCellNumber(i,j),
1433                                         lineSet,
1434                                         flag);
1435                 break;
1436         }
1437
1438         case LyXTabular::M_ALIGN_LEFT:
1439         case LyXTabular::M_ALIGN_RIGHT:
1440         case LyXTabular::M_ALIGN_CENTER:
1441                 flag = false;
1442         case LyXTabular::ALIGN_LEFT:
1443         case LyXTabular::ALIGN_RIGHT:
1444         case LyXTabular::ALIGN_CENTER:
1445         case LyXTabular::ALIGN_BLOCK:
1446                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1447                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1448                                 tabular.setAlignment(
1449                                         tabular.getCellNumber(i, j),
1450                                         setAlign,
1451                                         flag);
1452                 break;
1453
1454         case LyXTabular::M_VALIGN_TOP:
1455         case LyXTabular::M_VALIGN_BOTTOM:
1456         case LyXTabular::M_VALIGN_MIDDLE:
1457                 flag = false;
1458         case LyXTabular::VALIGN_TOP:
1459         case LyXTabular::VALIGN_BOTTOM:
1460         case LyXTabular::VALIGN_MIDDLE:
1461                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1462                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1463                                 tabular.setVAlignment(
1464                                         tabular.getCellNumber(i, j),
1465                                         setVAlign, flag);
1466                 break;
1467
1468         case LyXTabular::MULTICOLUMN: {
1469                 if (sel_row_start != sel_row_end) {
1470 #ifdef WITH_WARNINGS
1471 #warning Need I say it ? This is horrible.
1472 #endif
1473                         Alert::error(_("Error setting multicolumn"),
1474                                    _("You cannot set multicolumn vertically."));
1475                         return;
1476                 }
1477                 // just multicol for one Single Cell
1478                 if (!hasSelection()) {
1479                         // check wether we are completly in a multicol
1480                         if (tabular.isMultiColumn(actcell))
1481                                 tabular.unsetMultiColumn(actcell);
1482                         else
1483                                 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1484                         break;
1485                 }
1486                 // we have a selection so this means we just add all this
1487                 // cells to form a multicolumn cell
1488                 int s_start;
1489                 int s_end;
1490
1491                 if (sel_cell_start > sel_cell_end) {
1492                         s_start = sel_cell_end;
1493                         s_end = sel_cell_start;
1494                 } else {
1495                         s_start = sel_cell_start;
1496                         s_end = sel_cell_end;
1497                 }
1498                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1499                 actcell = s_start;
1500                 clearSelection();
1501                 break;
1502         }
1503
1504         case LyXTabular::SET_ALL_LINES:
1505                 setLines = true;
1506         case LyXTabular::UNSET_ALL_LINES:
1507                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1508                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1509                                 tabular.setAllLines(
1510                                         tabular.getCellNumber(i,j), setLines);
1511                 break;
1512
1513         case LyXTabular::SET_LONGTABULAR:
1514                 tabular.setLongTabular(true);
1515                 break;
1516
1517         case LyXTabular::UNSET_LONGTABULAR:
1518                 tabular.setLongTabular(false);
1519                 break;
1520
1521         case LyXTabular::SET_ROTATE_TABULAR:
1522                 tabular.setRotateTabular(true);
1523                 break;
1524
1525         case LyXTabular::UNSET_ROTATE_TABULAR:
1526                 tabular.setRotateTabular(false);
1527                 break;
1528
1529         case LyXTabular::SET_ROTATE_CELL:
1530                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1531                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1532                                 tabular.setRotateCell(
1533                                         tabular.getCellNumber(i, j), true);
1534                 break;
1535
1536         case LyXTabular::UNSET_ROTATE_CELL:
1537                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1538                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1539                                 tabular.setRotateCell(
1540                                         tabular.getCellNumber(i, j), false);
1541                 break;
1542
1543         case LyXTabular::SET_USEBOX: {
1544                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1545                 if (val == tabular.getUsebox(actcell))
1546                         val = LyXTabular::BOX_NONE;
1547                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1548                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1549                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1550                 break;
1551         }
1552
1553         case LyXTabular::UNSET_LTFIRSTHEAD:
1554                 flag = false;
1555         case LyXTabular::SET_LTFIRSTHEAD:
1556                 tabular.getRowOfLTFirstHead(row, ltt);
1557                 checkLongtableSpecial(ltt, value, flag);
1558                 tabular.setLTHead(row, flag, ltt, true);
1559                 break;
1560
1561         case LyXTabular::UNSET_LTHEAD:
1562                 flag = false;
1563         case LyXTabular::SET_LTHEAD:
1564                 tabular.getRowOfLTHead(row, ltt);
1565                 checkLongtableSpecial(ltt, value, flag);
1566                 tabular.setLTHead(row, flag, ltt, false);
1567                 break;
1568
1569         case LyXTabular::UNSET_LTFOOT:
1570                 flag = false;
1571         case LyXTabular::SET_LTFOOT:
1572                 tabular.getRowOfLTFoot(row, ltt);
1573                 checkLongtableSpecial(ltt, value, flag);
1574                 tabular.setLTFoot(row, flag, ltt, false);
1575                 break;
1576
1577         case LyXTabular::UNSET_LTLASTFOOT:
1578                 flag = false;
1579         case LyXTabular::SET_LTLASTFOOT:
1580                 tabular.getRowOfLTLastFoot(row, ltt);
1581                 checkLongtableSpecial(ltt, value, flag);
1582                 tabular.setLTFoot(row, flag, ltt, true);
1583                 break;
1584
1585         case LyXTabular::SET_LTNEWPAGE:
1586                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1587                 break;
1588
1589         // dummy stuff just to avoid warnings
1590         case LyXTabular::LAST_ACTION:
1591                 break;
1592         }
1593
1594         updateLocal(cur);
1595         InsetTabularMailer(*this).updateDialog(&bv);
1596 }
1597
1598
1599 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1600 {
1601         tabular.getCellInset(cell).edit(cur, x, y);
1602         cur.idx() = cell;
1603         updateLocal(cur);
1604 }
1605
1606
1607 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1608 {
1609         tabular.getCellInset(cell).edit(cur, behind);
1610         cur.idx() = cell;
1611         updateLocal(cur);
1612 }
1613
1614
1615 bool InsetTabular::showInsetDialog(BufferView * bv) const
1616 {
1617         InsetTabularMailer(*this).showDialog(bv);
1618         return true;
1619 }
1620
1621
1622 void InsetTabular::openLayoutDialog(BufferView * bv) const
1623 {
1624         InsetTabularMailer(*this).showDialog(bv);
1625 }
1626
1627
1628 //
1629 // function returns an object as defined in func_status.h:
1630 // states OK, Unknown, Disabled, On, Off.
1631 //
1632 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1633 {
1634         FuncStatus status;
1635         int action = LyXTabular::LAST_ACTION;
1636
1637         int i = 0;
1638         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1639                 string const tmp = tabularFeature[i].feature;
1640                 if (tmp == what.substr(0, tmp.length())) {
1641                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1642                         //   tabularFeatures[i].feature.length()))
1643                         action = tabularFeature[i].action;
1644                         break;
1645                 }
1646         }
1647         if (action == LyXTabular::LAST_ACTION) {
1648                 status.clear();
1649                 status.unknown(true);
1650                 return status;
1651         }
1652
1653         string const argument
1654                 = ltrim(what.substr(tabularFeature[i].feature.length()));
1655
1656         int sel_row_start;
1657         int sel_row_end;
1658         int dummy;
1659         LyXTabular::ltType dummyltt;
1660         bool flag = true;
1661
1662         if (hasSelection())
1663                 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1664         else
1665                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1666
1667         switch (action) {
1668         case LyXTabular::SET_PWIDTH:
1669         case LyXTabular::SET_MPWIDTH:
1670         case LyXTabular::SET_SPECIAL_COLUMN:
1671         case LyXTabular::SET_SPECIAL_MULTI:
1672         case LyXTabular::APPEND_ROW:
1673         case LyXTabular::APPEND_COLUMN:
1674         case LyXTabular::DELETE_ROW:
1675         case LyXTabular::DELETE_COLUMN:
1676         case LyXTabular::SET_ALL_LINES:
1677         case LyXTabular::UNSET_ALL_LINES:
1678                 status.clear();
1679                 return status;
1680
1681         case LyXTabular::MULTICOLUMN:
1682                 status.setOnOff(tabular.isMultiColumn(actcell));
1683                 break;
1684
1685         case LyXTabular::M_TOGGLE_LINE_TOP:
1686                 flag = false;
1687         case LyXTabular::TOGGLE_LINE_TOP:
1688                 status.setOnOff(tabular.topLine(actcell, flag));
1689                 break;
1690
1691         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1692                 flag = false;
1693         case LyXTabular::TOGGLE_LINE_BOTTOM:
1694                 status.setOnOff(tabular.bottomLine(actcell, flag));
1695                 break;
1696
1697         case LyXTabular::M_TOGGLE_LINE_LEFT:
1698                 flag = false;
1699         case LyXTabular::TOGGLE_LINE_LEFT:
1700                 status.setOnOff(tabular.leftLine(actcell, flag));
1701                 break;
1702
1703         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1704                 flag = false;
1705         case LyXTabular::TOGGLE_LINE_RIGHT:
1706                 status.setOnOff(tabular.rightLine(actcell, flag));
1707                 break;
1708
1709         case LyXTabular::M_ALIGN_LEFT:
1710                 flag = false;
1711         case LyXTabular::ALIGN_LEFT:
1712                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1713                 break;
1714
1715         case LyXTabular::M_ALIGN_RIGHT:
1716                 flag = false;
1717         case LyXTabular::ALIGN_RIGHT:
1718                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1719                 break;
1720
1721         case LyXTabular::M_ALIGN_CENTER:
1722                 flag = false;
1723         case LyXTabular::ALIGN_CENTER:
1724                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1725                 break;
1726
1727         case LyXTabular::ALIGN_BLOCK:
1728                 status.disabled(tabular.getPWidth(actcell).zero());
1729                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1730                 break;
1731
1732         case LyXTabular::M_VALIGN_TOP:
1733                 flag = false;
1734         case LyXTabular::VALIGN_TOP:
1735                 status.setOnOff(
1736                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1737                 break;
1738
1739         case LyXTabular::M_VALIGN_BOTTOM:
1740                 flag = false;
1741         case LyXTabular::VALIGN_BOTTOM:
1742                 status.setOnOff(
1743                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1744                 break;
1745
1746         case LyXTabular::M_VALIGN_MIDDLE:
1747                 flag = false;
1748         case LyXTabular::VALIGN_MIDDLE:
1749                 status.setOnOff(
1750                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1751                 break;
1752
1753         case LyXTabular::SET_LONGTABULAR:
1754                 status.setOnOff(tabular.isLongTabular());
1755                 break;
1756
1757         case LyXTabular::UNSET_LONGTABULAR:
1758                 status.setOnOff(!tabular.isLongTabular());
1759                 break;
1760
1761         case LyXTabular::SET_ROTATE_TABULAR:
1762                 status.setOnOff(tabular.getRotateTabular());
1763                 break;
1764
1765         case LyXTabular::UNSET_ROTATE_TABULAR:
1766                 status.setOnOff(!tabular.getRotateTabular());
1767                 break;
1768
1769         case LyXTabular::SET_ROTATE_CELL:
1770                 status.setOnOff(tabular.getRotateCell(actcell));
1771                 break;
1772
1773         case LyXTabular::UNSET_ROTATE_CELL:
1774                 status.setOnOff(!tabular.getRotateCell(actcell));
1775                 break;
1776
1777         case LyXTabular::SET_USEBOX:
1778                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1779                 break;
1780
1781         case LyXTabular::SET_LTFIRSTHEAD:
1782                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1783                 break;
1784
1785         case LyXTabular::SET_LTHEAD:
1786                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1787                 break;
1788
1789         case LyXTabular::SET_LTFOOT:
1790                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1791                 break;
1792
1793         case LyXTabular::SET_LTLASTFOOT:
1794                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1795                 break;
1796
1797         case LyXTabular::SET_LTNEWPAGE:
1798                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1799                 break;
1800
1801         default:
1802                 status.clear();
1803                 status.disabled(true);
1804                 break;
1805         }
1806         return status;
1807 }
1808
1809
1810 void InsetTabular::getLabelList(Buffer const & buffer,
1811                                 vector<string> & list) const
1812 {
1813         tabular.getLabelList(buffer, list);
1814 }
1815
1816
1817 bool InsetTabular::copySelection(BufferView & bv)
1818 {
1819         if (!hasSelection())
1820                 return false;
1821
1822         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1823         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1824         if (sel_col_start > sel_col_end) {
1825                 sel_col_start = sel_col_end;
1826                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1827         } else {
1828                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1829         }
1830
1831         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1832         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1833         if (sel_row_start > sel_row_end)
1834                 swap(sel_row_start, sel_row_end);
1835
1836         paste_tabular.reset(new LyXTabular(tabular));
1837         paste_tabular->setOwner(this);
1838
1839         for (int i = 0; i < sel_row_start; ++i)
1840                 paste_tabular->deleteRow(0);
1841
1842         int const rows = sel_row_end - sel_row_start + 1;
1843         while (paste_tabular->rows() > rows)
1844                 paste_tabular->deleteRow(rows);
1845
1846         paste_tabular->setTopLine(0, true, true);
1847         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1848                                      true, true);
1849
1850         for (int i = 0; i < sel_col_start; ++i)
1851                 paste_tabular->deleteColumn(0);
1852
1853         int const columns = sel_col_end - sel_col_start + 1;
1854         while (paste_tabular->columns() > columns)
1855                 paste_tabular->deleteColumn(columns);
1856
1857         paste_tabular->setLeftLine(0, true, true);
1858         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1859                                     true, true);
1860
1861         ostringstream os;
1862         OutputParams const runparams;
1863         paste_tabular->plaintext(*bv.buffer(), os, runparams,
1864                                  ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1865         bv.stuffClipboard(os.str());
1866         return true;
1867 }
1868
1869
1870 bool InsetTabular::pasteSelection(BufferView & bv)
1871 {
1872         if (!paste_tabular)
1873                 return false;
1874         int actcell = bv.cursor().idx();
1875         int actcol = tabular.column_of_cell(actcell);
1876         int actrow = tabular.row_of_cell(actcell);
1877         for (int r1 = 0, r2 = actrow;
1878              r1 < paste_tabular->rows() && r2 < tabular.rows();
1879              ++r1, ++r2) {
1880                 for (int c1 = 0, c2 = actcol;
1881                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1882                     ++c1, ++c2) {
1883                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1884                             tabular.isPartOfMultiColumn(r2, c2))
1885                                 continue;
1886                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1887                                 --c2;
1888                                 continue;
1889                         }
1890                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1891                                 --c1;
1892                                 continue;
1893                         }
1894                         InsetText & inset = tabular.getCellInset(r2, c2);
1895                         inset = paste_tabular->getCellInset(r1, c1);
1896                         inset.setOwner(this);
1897                         inset.markNew();
1898                 }
1899         }
1900         return true;
1901 }
1902
1903
1904 bool InsetTabular::cutSelection(BufferParams const & bp)
1905 {
1906         if (!hasSelection())
1907                 return false;
1908
1909         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1910         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1911         if (sel_col_start > sel_col_end) {
1912                 sel_col_start = sel_col_end;
1913                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1914         } else {
1915                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1916         }
1917
1918         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1919         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1920
1921         if (sel_row_start > sel_row_end)
1922                 swap(sel_row_start, sel_row_end);
1923
1924         if (sel_cell_start > sel_cell_end)
1925                 swap(sel_cell_start, sel_cell_end);
1926
1927         for (int i = sel_row_start; i <= sel_row_end; ++i)
1928                 for (int j = sel_col_start; j <= sel_col_end; ++j)
1929                         tabular.getCellInset(tabular.getCellNumber(i, j))
1930                                 .clear(bp.tracking_changes);
1931         return true;
1932 }
1933
1934
1935 bool InsetTabular::isRightToLeft(LCursor & cur)
1936 {
1937         return cur.bv().getParentLanguage(this)->RightToLeft();
1938 }
1939
1940
1941 void InsetTabular::getSelection(int actcell,
1942         int & srow, int & erow, int & scol, int & ecol) const
1943 {
1944         int const start = hasSelection() ? sel_cell_start : actcell;
1945         int const end = hasSelection() ? sel_cell_end : actcell;
1946
1947         srow = tabular.row_of_cell(start);
1948         erow = tabular.row_of_cell(end);
1949         if (srow > erow)
1950                 swap(srow, erow);
1951
1952         scol = tabular.column_of_cell(start);
1953         ecol = tabular.column_of_cell(end);
1954         if (scol > ecol)
1955                 swap(scol, ecol);
1956         else
1957                 ecol = tabular.right_column_of_cell(end);
1958 }
1959
1960
1961 int InsetTabular::numParagraphs() const
1962 {
1963         return tabular.getNumberOfCells();
1964 }
1965
1966
1967 LyXText * InsetTabular::getText(int i) const
1968 {
1969         return i < tabular.getNumberOfCells()
1970                 ? tabular.getCellInset(i).getText(0)
1971                 : 0;
1972 }
1973
1974
1975 void InsetTabular::markErased()
1976 {
1977         for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1978                 tabular.getCellInset(cell).markErased();
1979 }
1980
1981
1982 bool InsetTabular::forceDefaultParagraphs(InsetBase const * in) const
1983 {
1984         const int cell = tabular.getCellFromInset(in);
1985
1986         if (cell != -1)
1987                 return tabular.getPWidth(cell).zero();
1988
1989         // this is a workaround for a crash (New, Insert->Tabular,
1990         // Insert->FootNote)
1991         if (!owner())
1992                 return false;
1993
1994         // well we didn't obviously find it so maybe our owner knows more
1995         BOOST_ASSERT(owner());
1996         return owner()->forceDefaultParagraphs(in);
1997 }
1998
1999
2000 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2001                                      bool usePaste)
2002 {
2003         if (buf.length() <= 0)
2004                 return true;
2005
2006         int cols = 1;
2007         int rows = 1;
2008         int maxCols = 1;
2009         string::size_type len = buf.length();
2010         string::size_type p = 0;
2011
2012         int actcell = bv.cursor().idx();
2013         int actcol = tabular.column_of_cell(actcell);
2014         int actrow = tabular.row_of_cell(actcell);
2015
2016         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2017                 switch (buf[p]) {
2018                 case '\t':
2019                         ++cols;
2020                         break;
2021                 case '\n':
2022                         if (p + 1 < len)
2023                                 ++rows;
2024                         maxCols = max(cols, maxCols);
2025                         cols = 1;
2026                         break;
2027                 }
2028                 ++p;
2029         }
2030         maxCols = max(cols, maxCols);
2031         LyXTabular * loctab;
2032         int cell = 0;
2033         int ocol = 0;
2034         int row = 0;
2035         if (usePaste) {
2036                 paste_tabular.reset(
2037                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
2038                 paste_tabular->setOwner(this);
2039                 loctab = paste_tabular.get();
2040                 cols = 0;
2041         } else {
2042                 loctab = &tabular;
2043                 cell = actcell;
2044                 ocol = actcol;
2045                 row = actrow;
2046         }
2047
2048         string::size_type op = 0;
2049         int cells = loctab->getNumberOfCells();
2050         p = 0;
2051         cols = ocol;
2052         rows = loctab->rows();
2053         int const columns = loctab->columns();
2054
2055         while (cell < cells && p < len && row < rows &&
2056                (p = buf.find_first_of("\t\n", p)) != string::npos)
2057         {
2058                 if (p >= len)
2059                         break;
2060                 switch (buf[p]) {
2061                 case '\t':
2062                         // we can only set this if we are not too far right
2063                         if (cols < columns) {
2064                                 InsetText & inset = loctab->getCellInset(cell);
2065                                 LyXFont const font = inset.text_.
2066                                         getFont(inset.paragraphs().begin(), 0);
2067                                 inset.setText(buf.substr(op, p - op), font);
2068                                 ++cols;
2069                                 ++cell;
2070                         }
2071                         break;
2072                 case '\n':
2073                         // we can only set this if we are not too far right
2074                         if (cols < columns) {
2075                                 InsetText & inset = tabular.getCellInset(cell);
2076                                 LyXFont const font = inset.text_.
2077                                         getFont(inset.paragraphs().begin(), 0);
2078                                 inset.setText(buf.substr(op, p - op), font);
2079                         }
2080                         cols = ocol;
2081                         ++row;
2082                         if (row < rows)
2083                                 cell = loctab->getCellNumber(row, cols);
2084                         break;
2085                 }
2086                 ++p;
2087                 op = p;
2088         }
2089         // check for the last cell if there is no trailing '\n'
2090         if (cell < cells && op < len) {
2091                 InsetText & inset = loctab->getCellInset(cell);
2092                 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2093                 inset.setText(buf.substr(op, len - op), font);
2094         }
2095         return true;
2096 }
2097
2098
2099 void InsetTabular::addPreview(PreviewLoader & loader) const
2100 {
2101         int const rows = tabular.rows();
2102         int const columns = tabular.columns();
2103         for (int i = 0; i < rows; ++i) {
2104                 for (int j = 0; j < columns; ++j)
2105                         tabular.getCellInset(i, j).addPreview(loader);
2106         }
2107 }
2108
2109
2110
2111 void InsetTabular::clearSelection() const
2112 {
2113         sel_cell_start = 0;
2114         sel_cell_end = 0;
2115         has_selection = false;
2116 }
2117
2118
2119 void InsetTabular::setSelection(int start, int end) const
2120 {
2121         sel_cell_start = start;
2122         sel_cell_end = end;
2123         has_selection = true;
2124 }
2125
2126
2127 string const InsetTabularMailer::name_("tabular");
2128
2129 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2130         : inset_(const_cast<InsetTabular &>(inset))
2131 {}
2132
2133
2134 string const InsetTabularMailer::inset2string(Buffer const &) const
2135 {
2136         return params2string(inset_);
2137 }
2138
2139
2140 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2141 {
2142         istringstream data(in);
2143         LyXLex lex(0,0);
2144         lex.setStream(data);
2145
2146 #warning CHECK verify that this is a sane value to return.
2147         if (in.empty())
2148                 return -1;
2149
2150         if (lex.isOK()) {
2151                 lex.next();
2152                 string const token = lex.getString();
2153                 if (token != name_)
2154                         return -1;
2155         }
2156
2157         int cell = -1;
2158         if (lex.isOK()) {
2159                 lex.next();
2160                 string const token = lex.getString();
2161                 if (token != "\\active_cell")
2162                         return -1;
2163                 lex.next();
2164                 cell = lex.getInteger();
2165         }
2166
2167         // This is part of the inset proper that is usually swallowed
2168         // by Buffer::readInset
2169         if (lex.isOK()) {
2170                 lex.next();
2171                 string const token = lex.getString();
2172                 if (token != "Tabular")
2173                         return -1;
2174         }
2175
2176         if (!lex.isOK())
2177                 return -1;
2178
2179         Buffer const & buffer = inset.buffer();
2180         inset.read(buffer, lex);
2181
2182         // We can't set the active cell, but we can tell the frontend
2183         // what it is.
2184         return cell;
2185 }
2186
2187
2188 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2189 {
2190         ostringstream data;
2191 #warning wrong!
2192         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2193         data << name_ << " \\active_cell " << 0 << '\n';
2194         inset.write(inset.buffer(), data);
2195         data << "\\end_inset\n";
2196         return data.str();
2197 }