]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
fix tabular crashs
[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)
159 {
160         tabular.setOwner(this);
161 }
162
163
164 InsetTabular::InsetTabular(InsetTabular const & tab)
165         : UpdatableInset(tab), tabular(tab.tabular),
166                 buffer_(tab.buffer_), cursorx_(0)
167 {
168         tabular.setOwner(this);
169 }
170
171
172 InsetTabular::~InsetTabular()
173 {
174         InsetTabularMailer(*this).hideDialog();
175 }
176
177
178 auto_ptr<InsetBase> InsetTabular::clone() const
179 {
180         return auto_ptr<InsetBase>(new InsetTabular(*this));
181 }
182
183
184 Buffer const & InsetTabular::buffer() const
185 {
186         return *buffer_;
187 }
188
189
190 void InsetTabular::buffer(Buffer * b)
191 {
192         buffer_ = b;
193 }
194
195
196 void InsetTabular::write(Buffer const & buf, ostream & os) const
197 {
198         os << "Tabular" << endl;
199         tabular.write(buf, os);
200 }
201
202
203 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
204 {
205         bool const old_format = (lex.getString() == "\\LyXTable");
206
207         tabular.read(buf, lex);
208
209         if (old_format)
210                 return;
211
212         lex.nextToken();
213         string token = lex.getString();
214         while (lex.isOK() && (token != "\\end_inset")) {
215                 lex.nextToken();
216                 token = lex.getString();
217         }
218         if (token != "\\end_inset") {
219                 lex.printError("Missing \\end_inset at this point. "
220                                "Read: `$$Token'");
221         }
222 }
223
224
225 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
226 {
227         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
228         //      mi.base.textwidth << "\n";
229         if (!mi.base.bv) {
230                 lyxerr << "InsetTabular::metrics: need bv" << endl;
231                 BOOST_ASSERT(false);
232         }
233
234         for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
235                 int maxAsc = 0;
236                 int maxDesc = 0;
237                 for (int j = 0; j < tabular.columns(); ++j) {
238                         if (tabular.isPartOfMultiColumn(i, j))
239                                 continue;
240                         ++cell;
241                         Dimension dim;
242                         MetricsInfo m = mi;
243                         m.base.textwidth =
244                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
245                         tabular.getCellInset(cell).metrics(m, dim);
246                         maxAsc  = max(maxAsc, dim.asc);
247                         maxDesc = max(maxDesc, dim.des);
248                         tabular.setWidthOfCell(cell, dim.wid);
249                 }
250                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
251                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
252         }
253
254         dim.asc = tabular.getAscentOfRow(0);
255         dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
256         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
257         dim_ = dim;
258 }
259
260
261 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
262 {
263         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
264
265         BufferView * bv = pi.base.bv;
266         setPosCache(pi, x, y);
267
268         if (!owner())
269                 x += scroll();
270
271         x += ADD_TO_TABULAR_WIDTH;
272
273         int idx = 0;
274         first_visible_cell = -1;
275         for (int i = 0; i < tabular.rows(); ++i) {
276                 int nx = x;
277                 idx = tabular.getCellNumber(i, 0);
278                 if (y + tabular.getDescentOfRow(i) <= 0 &&
279                           y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
280                 {
281                         y += tabular.getDescentOfRow(i) +
282                                         tabular.getAscentOfRow(i + 1) +
283                                         tabular.getAdditionalHeight(i + 1);
284                         continue;
285                 }
286                 for (int j = 0; j < tabular.columns(); ++j) {
287                         if (nx > bv->workWidth())
288                                 break;
289                         if (tabular.isPartOfMultiColumn(i, j))
290                                 continue;
291                         if (first_visible_cell < 0)
292                                 first_visible_cell = idx;
293                         if (bv->cursor().selection())
294                                 drawCellSelection(pi, nx, y, i, j, idx);
295
296                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
297                         cell(idx).draw(pi, cx, y);
298                         drawCellLines(pi.pain, nx, y, i, idx);
299                         nx += tabular.getWidthOfColumn(idx);
300                         ++idx;
301                 }
302
303 // Would be nice, but for some completely unfathomable reason,
304 // on a col resize to a new fixed width, even though the insettexts
305 // are resized, the cell isn't, but drawing all cells in a tall table
306 // has the desired effect somehow. Complete dark magic.
307 #if 0
308                 // avoiding drawing the rest of a long table is
309                 // a pretty big speedup
310                 if (y > bv->workHeight())
311                         break;
312 #endif
313
314                 y += tabular.getDescentOfRow(i) +
315                         tabular.getAscentOfRow(i + 1) +
316                         tabular.getAdditionalHeight(i + 1);
317         }
318 }
319
320
321 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
322                                  int row, int cell) const
323 {
324         int x2 = x + tabular.getWidthOfColumn(cell);
325         bool on_off = false;
326
327         if (!tabular.topAlreadyDrawn(cell)) {
328                 on_off = !tabular.topLine(cell);
329                 pain.line(x, y - tabular.getAscentOfRow(row),
330                           x2, y -  tabular.getAscentOfRow(row),
331                           on_off ? LColor::tabularonoffline : LColor::tabularline,
332                           on_off ? Painter::line_onoffdash : Painter::line_solid);
333         }
334         on_off = !tabular.bottomLine(cell);
335         pain.line(x, y + tabular.getDescentOfRow(row),
336                   x2, y + tabular.getDescentOfRow(row),
337                   on_off ? LColor::tabularonoffline : LColor::tabularline,
338                   on_off ? Painter::line_onoffdash : Painter::line_solid);
339         if (!tabular.leftAlreadyDrawn(cell)) {
340                 on_off = !tabular.leftLine(cell);
341                 pain.line(x, y -  tabular.getAscentOfRow(row),
342                           x, y +  tabular.getDescentOfRow(row),
343                           on_off ? LColor::tabularonoffline : LColor::tabularline,
344                           on_off ? Painter::line_onoffdash : Painter::line_solid);
345         }
346         on_off = !tabular.rightLine(cell);
347         pain.line(x2 - tabular.getAdditionalWidth(cell),
348                   y -  tabular.getAscentOfRow(row),
349                   x2 - tabular.getAdditionalWidth(cell),
350                   y +  tabular.getDescentOfRow(row),
351                   on_off ? LColor::tabularonoffline : LColor::tabularline,
352                   on_off ? Painter::line_onoffdash : Painter::line_solid);
353 }
354
355
356 void InsetTabular::drawCellSelection(PainterInfo & pi, int x, int y,
357                                      int row, int column, int cell) const
358 {
359         LCursor & cur = pi.base.bv->cursor();
360         BOOST_ASSERT(cur.selection());
361         if (tablemode(cur)) {
362                 int rs, re, cs, ce;
363                 getSelection(cur, rs, re, cs, ce);
364                 if (column >= cs && column <= ce && row >= rs && row <= re) {
365                         int w = tabular.getWidthOfColumn(cell);
366                         int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
367                         pi.pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
368                                                  w, h, LColor::selection);
369                 }
370         }
371 }
372
373
374 string const InsetTabular::editMessage() const
375 {
376         return _("Opened table");
377 }
378
379
380 void InsetTabular::edit(LCursor & cur, bool left)
381 {
382         lyxerr << "InsetTabular::edit: " << this << endl;
383         finishUndo();
384         int cell;
385         if (left) {
386                 if (isRightToLeft(cur))
387                         cell = tabular.getLastCellInRow(0);
388                 else
389                         cell = 0;
390         } else {
391                 if (isRightToLeft(cur))
392                         cell = tabular.getFirstCellInRow(tabular.rows()-1);
393                 else
394                         cell = tabular.getNumberOfCells() - 1;
395         }
396         cur.selection() = false;
397         resetPos(cur);
398         cur.bv().fitCursor();
399         cur.push(this);
400         cur.idx() = cell;
401 }
402
403
404 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
405 {
406         //lyxerr << "InsetTabular::editXY: " << this << endl;
407         cur.selection() = false;
408         cur.push(this);
409         return setPos(cur, x, y);
410         //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
411 }
412
413
414 void InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
415 {
416         lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
417         //lyxerr << "  cur:\n" << cur << endl;
418         CursorSlice sl = cur.current();
419
420         switch (cmd.action) {
421
422         case LFUN_MOUSE_PRESS:
423                 // we'll pop up the table dialog on release
424                 if (cmd.button() == mouse_button::button3)
425                         break;
426                 cur.selection() = false;
427                 setPos(cur, cmd.x, cmd.y);
428                 cur.resetAnchor();
429                 cur.bv().cursor() = cur;
430                 //if (cmd.button() == mouse_button::button2)
431                 //      dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
432                 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
433                 break;
434
435         case LFUN_MOUSE_MOTION:
436                 if (cmd.button() != mouse_button::button1)
437                         break;
438                 setPos(cur, cmd.x, cmd.y);
439                 cur.bv().cursor().cursor_ = cur.cursor_;
440                 cur.bv().cursor().selection() = true;
441                 //lyxerr << "# InsetTabular::MouseMotion\n" << cur.bv().cursor() << endl;
442                 break;
443
444         case LFUN_MOUSE_RELEASE:
445                 //lyxerr << "# InsetTabular::MouseRelease\n" << cur.bv().cursor() << endl;
446                 if (cmd.button() == mouse_button::button3)
447                         InsetTabularMailer(*this).showDialog(&cur.bv());
448                 break;
449
450         case LFUN_CELL_BACKWARD:
451                 movePrevCell(cur);
452                 cur.selection() = false;
453                 break;
454
455         case LFUN_CELL_FORWARD:
456                 moveNextCell(cur);
457                 cur.selection() = false;
458                 break;
459
460         case LFUN_SCROLL_INSET:
461                 if (cmd.argument.empty())
462                         break;
463                 if (cmd.argument.find('.') != cmd.argument.npos)
464                         scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
465                 else
466                         scroll(cur.bv(), strToInt(cmd.argument));
467                 cur.update();
468                 break;
469
470         case LFUN_RIGHTSEL:
471         case LFUN_RIGHT:
472                 cell(cur.idx()).dispatch(cur, cmd);
473                 if (sl == cur.current())
474                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
475                 if (sl == cur.current())
476                         cur.dispatched(FINISHED_RIGHT);
477                 break;
478
479         case LFUN_LEFTSEL: 
480         case LFUN_LEFT:
481                 cell(cur.idx()).dispatch(cur, cmd);
482                 if (sl == cur.current())
483                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
484                 if (sl == cur.current())
485                         cur.dispatched(FINISHED);
486                 break;
487
488         case LFUN_DOWNSEL:
489         case LFUN_DOWN:
490                 cell(cur.idx()).dispatch(cur, cmd);
491                 if (sl == cur.current())
492                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
493                                 cur.idx() = tabular.getCellBelow(cur.idx());
494                                 cur.par() = 0;
495                                 cur.pos() = 0;
496                                 resetPos(cur);
497                         }
498                 if (sl == cur.current())
499                         cur.dispatched(FINISHED_DOWN);
500                 break;
501
502         case LFUN_UPSEL:
503         case LFUN_UP:
504                 cell(cur.idx()).dispatch(cur, cmd);
505                 if (sl == cur.current())
506                         if (tabular.row_of_cell(cur.idx()) != 0) {
507                                 cur.idx() = tabular.getCellAbove(cur.idx());
508                                 cur.par() = cur.lastpar();
509                                 cur.pos() = cur.lastpos();
510                                 resetPos(cur);
511                         }
512                 if (sl == cur.current())
513                         cur.dispatched(FINISHED_UP);
514                 break;
515
516         case LFUN_NEXT: {
517                 //if (hasSelection())
518                 //      cur.selection() = false;
519                 int actcell = cur.idx();
520                 int actcol = tabular.column_of_cell(actcell);
521                 int column = actcol;
522                 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
523                                 < yo_ + tabular.getHeightOfTabular())
524                 {
525                         cur.bv().scrollDocView(
526                                 cur.bv().top_y() + cur.bv().painter().paperHeight());
527                         cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
528                 } else {
529                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
530                 }
531                 resetPos(cur);
532                 break;
533         }
534
535         case LFUN_PRIOR: {
536                 //if (hasSelection())
537                 //      cur.selection() = false;
538                 int column = tabular.column_of_cell(cur.idx());
539                 if (yo_ < 0) {
540                         cur.bv().scrollDocView(
541                                 cur.bv().top_y() - cur.bv().painter().paperHeight());
542                         if (yo_ > 0)
543                                 cur.idx() = column;
544                         else
545                                 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
546                 } else {
547                         cur.idx() = column;
548                 }
549                 resetPos(cur);
550                 break;
551         }
552
553         case LFUN_LAYOUT_TABULAR:
554                 InsetTabularMailer(*this).showDialog(&cur.bv());
555                 break;
556
557         case LFUN_INSET_DIALOG_UPDATE:
558                 InsetTabularMailer(*this).updateDialog(&cur.bv());
559                 break;
560
561         case LFUN_TABULAR_FEATURE:
562                 if (!tabularFeatures(cur, cmd.argument))
563                         cur.notdispatched();
564                 break;
565
566         // insert file functions
567         case LFUN_FILE_INSERT_ASCII_PARA:
568         case LFUN_FILE_INSERT_ASCII: {
569                 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
570                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
571                         cur.notdispatched();
572                 break;
573         }
574
575         case LFUN_LANGUAGE:
576         case LFUN_EMPH:
577         case LFUN_BOLD:
578         case LFUN_NOUN:
579         case LFUN_CODE:
580         case LFUN_SANS:
581         case LFUN_ROMAN:
582         case LFUN_DEFAULT:
583         case LFUN_UNDERLINE:
584         case LFUN_FONT_SIZE:
585                 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
586                 break;
587
588         case LFUN_CUT:
589                 if (copySelection(cur)) {
590                         recordUndo(cur, Undo::DELETE);
591                         cutSelection(cur);
592                 }
593                 break;
594
595         case LFUN_BACKSPACE:
596         case LFUN_DELETE:
597                 recordUndo(cur, Undo::DELETE);
598                 if (tablemode(cur))
599                         cutSelection(cur);
600                 else
601                         cell(cur.idx()).dispatch(cur, cmd);
602                 break;
603
604         case LFUN_COPY:
605                 if (!cur.selection())
606                         break;
607                 finishUndo();
608                 copySelection(cur);
609                 break;
610
611         case LFUN_PASTESELECTION: {
612                 string const clip = cur.bv().getClipboard();
613                 if (clip.empty())
614                         break;
615                 if (clip.find('\t') != string::npos) {
616                         int cols = 1;
617                         int rows = 1;
618                         int maxCols = 1;
619                         size_t len = clip.length();
620                         for (size_t p = 0; p < len; ++p) {
621                                 p = clip.find_first_of("\t\n", p);
622                                 if (p == string::npos)
623                                         break;
624                                 switch (clip[p]) {
625                                 case '\t':
626                                         ++cols;
627                                         break;
628                                 case '\n':
629                                         if (p + 1 < len)
630                                                 ++rows;
631                                         maxCols = max(cols, maxCols);
632                                         cols = 1;
633                                         break;
634                                 }
635                         }
636                         maxCols = max(cols, maxCols);
637
638                         paste_tabular.reset(
639                                 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
640
641                         string::size_type op = 0;
642                         int cell = 0;
643                         int cells = paste_tabular->getNumberOfCells();
644                         cols = 0;
645                         LyXFont font;
646                         for (size_t p = 0; cell < cells && p < len; ++p) {
647                                 p = clip.find_first_of("\t\n", p);
648                                 if (p == string::npos || p >= len)
649                                         break;
650                                 switch (clip[p]) {
651                                 case '\t':
652                                         paste_tabular->getCellInset(cell).
653                                                 setText(clip.substr(op, p - op), font);
654                                         ++cols;
655                                         ++cell;
656                                         break;
657                                 case '\n':
658                                         paste_tabular->getCellInset(cell).
659                                                 setText(clip.substr(op, p - op), font);
660                                         while (cols++ < maxCols)
661                                                 ++cell;
662                                         cols = 0;
663                                         break;
664                                 }
665                                 op = p + 1;
666                         }
667                         // check for the last cell if there is no trailing '\n'
668                         if (cell < cells && op < len)
669                                 paste_tabular->getCellInset(cell).
670                                         setText(clip.substr(op, len - op), font);
671                 } else if (!insertAsciiString(cur.bv(), clip, true)) {
672                         // so that the clipboard is used and it goes on
673                         // to default
674                         // and executes LFUN_PASTESELECTION in insettext!
675                         paste_tabular.reset();
676                 }
677                 // fall through
678         }
679
680         case LFUN_PASTE:
681                 if (hasPasteBuffer()) {
682                         recordUndo(cur, Undo::INSERT);
683                         pasteSelection(cur);
684                         break;
685                 }
686                 cell(cur.idx()).dispatch(cur, cmd);
687                 break;
688
689         default:
690                 // we try to handle this event in the insets dispatch function.
691                 cell(cur.idx()).dispatch(cur, cmd);
692                 break;
693         }
694
695         InsetTabularMailer(*this).updateDialog(&cur.bv());
696 }
697
698
699 int InsetTabular::latex(Buffer const & buf, ostream & os,
700                         OutputParams const & runparams) const
701 {
702         return tabular.latex(buf, os, runparams);
703 }
704
705
706 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
707                         OutputParams const & runparams) const
708 {
709         int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
710         return tabular.plaintext(buf, os, runparams, dp, false, 0);
711 }
712
713
714 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
715                            OutputParams const & runparams) const
716 {
717         return tabular.linuxdoc(buf,os, runparams);
718 }
719
720
721 int InsetTabular::docbook(Buffer const & buf, ostream & os,
722                           OutputParams const & runparams) const
723 {
724         int ret = 0;
725         InsetOld * master;
726
727         // if the table is inside a float it doesn't need the informaltable
728         // wrapper. Search for it.
729         for (master = owner(); master; master = master->owner())
730                 if (master->lyxCode() == InsetOld::FLOAT_CODE)
731                         break;
732
733         if (!master) {
734                 os << "<informaltable>";
735                 if (runparams.mixed_content)
736                         os << endl;
737                 ++ret;
738         }
739         ret += tabular.docbook(buf, os, runparams);
740         if (!master) {
741                 os << "</informaltable>";
742                 if (runparams.mixed_content)
743                         os << endl;
744                 ++ret;
745         }
746         return ret;
747 }
748
749
750 void InsetTabular::validate(LaTeXFeatures & features) const
751 {
752         tabular.validate(features);
753 }
754
755
756 InsetText const & InsetTabular::cell(int idx) const
757 {
758         return tabular.getCellInset(idx);
759 }
760
761
762 InsetText & InsetTabular::cell(int idx)
763 {
764         return tabular.getCellInset(idx);
765 }
766
767
768 void InsetTabular::getCursorPos(CursorSlice const & cur, int & x, int & y) const
769 {
770         cell(cur.idx()).getCursorPos(cur, x, y);
771 }
772
773
774 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
775 {
776         int idx_min = 0;
777         int dist_min = 1000000;
778         for (idx_type i = 0; i < nargs(); ++i) {
779                 int d = getText(i)->dist(x, y);
780                 if (d < dist_min) {
781                         dist_min = d;
782                         idx_min = i;
783                 }
784         }
785         cur.idx() = idx_min;
786         InsetBase * inset = cell(cur.idx()).text_.editXY(cur, x, y);
787         //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
788         return inset;
789 }
790
791
792 int InsetTabular::getCellXPos(int cell) const
793 {
794         int c = cell;
795
796         for (; !tabular.isFirstCellInRow(c); --c)
797                 ;
798         int lx = tabular.getWidthOfColumn(cell);
799         for (; c < cell; ++c)
800                 lx += tabular.getWidthOfColumn(c);
801
802         return lx - tabular.getWidthOfColumn(cell) + xo_;
803 }
804
805
806 void InsetTabular::resetPos(LCursor & cur) const
807 {
808         BufferView & bv = cur.bv();
809         int actcell = cur.idx();
810         int actcol = tabular.column_of_cell(actcell);
811
812         int const offset = ADD_TO_TABULAR_WIDTH + 2;
813         int new_x = getCellXPos(actcell) + offset;
814         int old_x = cursorx_;
815         cursorx_ = new_x;
816 //    cursor.x(getCellXPos(actcell) + offset);
817         if (actcol < tabular.columns() - 1 && scroll(false) &&
818                 tabular.getWidthOfTabular() < bv.workWidth()-20)
819         {
820                 scroll(bv, 0.0F);
821         } else if (cursorx_ - offset > 20 &&
822                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
823                    > bv.workWidth() - 20) {
824                 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
825         } else if (cursorx_ - offset < 20) {
826                 scroll(bv, 20 - cursorx_ + offset);
827         } else if (scroll() && xo_ > 20 &&
828                    xo_ + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
829                 scroll(bv, old_x - cursorx_);
830         }
831
832         InsetTabularMailer(*this).updateDialog(&bv);
833 }
834
835
836 void InsetTabular::moveNextCell(LCursor & cur)
837 {
838         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
839         if (isRightToLeft(cur)) {
840                 if (tabular.isFirstCellInRow(cur.idx())) {
841                         int row = tabular.row_of_cell(cur.idx());
842                         if (row == tabular.rows() - 1)
843                                 return;
844                         cur.idx() = tabular.getLastCellInRow(row);
845                         cur.idx() = tabular.getCellBelow(cur.idx());
846                 } else {
847                         if (cur.idx() == 0)
848                                 return;
849                         --cur.idx();
850                 }
851         } else {
852                 if (tabular.isLastCell(cur.idx()))
853                         return;
854                 ++cur.idx();
855         }
856         cur.par() = 0;
857         cur.pos() = 0;
858         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
859         resetPos(cur);
860 }
861
862
863 void InsetTabular::movePrevCell(LCursor & cur)
864 {
865         if (isRightToLeft(cur)) {
866                 if (tabular.isLastCellInRow(cur.idx())) {
867                         int row = tabular.row_of_cell(cur.idx());
868                         if (row == 0)
869                                 return;
870                         cur.idx() = tabular.getFirstCellInRow(row);
871                         cur.idx() = tabular.getCellAbove(cur.idx());
872                 } else {
873                         if (tabular.isLastCell(cur.idx()))
874                                 return;
875                         ++cur.idx();
876                 }
877         } else {
878                 if (cur.idx() == 0) // first cell
879                         return;
880                 --cur.idx();
881         }
882         cur.par() = 0;
883         cur.pos() = 0;
884         resetPos(cur);
885 }
886
887
888 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
889 {
890         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
891
892         int i = 0;
893         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
894                 string const tmp = tabularFeature[i].feature;
895
896                 if (tmp == what.substr(0, tmp.length())) {
897                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
898                         //tabularFeatures[i].feature.length())) {
899                         action = tabularFeature[i].action;
900                         break;
901                 }
902         }
903         if (action == LyXTabular::LAST_ACTION)
904                 return false;
905
906         string const val =
907                 ltrim(what.substr(tabularFeature[i].feature.length()));
908         tabularFeatures(cur, action, val);
909         return true;
910 }
911
912
913 namespace {
914
915 void checkLongtableSpecial(LyXTabular::ltType & ltt,
916                           string const & special, bool & flag)
917 {
918         if (special == "dl_above") {
919                 ltt.topDL = flag;
920                 ltt.set = false;
921         } else if (special == "dl_below") {
922                 ltt.bottomDL = flag;
923                 ltt.set = false;
924         } else if (special == "empty") {
925                 ltt.empty = flag;
926                 ltt.set = false;
927         } else if (flag) {
928                 ltt.empty = false;
929                 ltt.set = true;
930         }
931 }
932
933 } // anon namespace
934
935
936 void InsetTabular::tabularFeatures(LCursor & cur,
937         LyXTabular::Feature feature, string const & value)
938 {
939         BufferView & bv = cur.bv();
940         int actcell = cur.idx();
941         int sel_col_start;
942         int sel_col_end;
943         int sel_row_start;
944         int sel_row_end;
945         bool setLines = false;
946         LyXAlignment setAlign = LYX_ALIGN_LEFT;
947         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
948
949         switch (feature) {
950
951         case LyXTabular::M_ALIGN_LEFT:
952         case LyXTabular::ALIGN_LEFT:
953                 setAlign = LYX_ALIGN_LEFT;
954                 break;
955
956         case LyXTabular::M_ALIGN_RIGHT:
957         case LyXTabular::ALIGN_RIGHT:
958                 setAlign = LYX_ALIGN_RIGHT;
959                 break;
960
961         case LyXTabular::M_ALIGN_CENTER:
962         case LyXTabular::ALIGN_CENTER:
963                 setAlign = LYX_ALIGN_CENTER;
964                 break;
965
966         case LyXTabular::ALIGN_BLOCK:
967                 setAlign = LYX_ALIGN_BLOCK;
968                 break;
969
970         case LyXTabular::M_VALIGN_TOP:
971         case LyXTabular::VALIGN_TOP:
972                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
973                 break;
974
975         case LyXTabular::M_VALIGN_BOTTOM:
976         case LyXTabular::VALIGN_BOTTOM:
977                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
978                 break;
979
980         case LyXTabular::M_VALIGN_MIDDLE:
981         case LyXTabular::VALIGN_MIDDLE:
982                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
983                 break;
984
985         default:
986                 break;
987         }
988
989         recordUndo(cur, Undo::ATOMIC);
990
991         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
992         int row =  tabular.row_of_cell(actcell);
993         int column = tabular.column_of_cell(actcell);
994         bool flag = true;
995         LyXTabular::ltType ltt;
996
997         switch (feature) {
998
999         case LyXTabular::SET_PWIDTH: {
1000                 LyXLength const len(value);
1001                 tabular.setColumnPWidth(actcell, len);
1002                 if (len.zero()
1003                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1004                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1005                 else if (!len.zero()
1006                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1007                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1008                 break;
1009         }
1010
1011         case LyXTabular::SET_MPWIDTH:
1012                 tabular.setMColumnPWidth(actcell, LyXLength(value));
1013                 break;
1014
1015         case LyXTabular::SET_SPECIAL_COLUMN:
1016         case LyXTabular::SET_SPECIAL_MULTI:
1017                 tabular.setAlignSpecial(actcell,value,feature);
1018                 break;
1019
1020         case LyXTabular::APPEND_ROW:
1021                 // append the row into the tabular
1022                 tabular.appendRow(bv.buffer()->params(), actcell);
1023                 tabular.setOwner(this);
1024                 break;
1025
1026         case LyXTabular::APPEND_COLUMN:
1027                 // append the column into the tabular
1028                 tabular.appendColumn(bv.buffer()->params(), actcell);
1029                 tabular.setOwner(this);
1030                 actcell = tabular.getCellNumber(row, column);
1031                 break;
1032
1033         case LyXTabular::DELETE_ROW:
1034                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1035                         tabular.deleteRow(sel_row_start);
1036                 if (sel_row_start >= tabular.rows())
1037                         --sel_row_start;
1038                 actcell = tabular.getCellNumber(sel_row_start, column);
1039                 cur.selection() = false;
1040                 break;
1041
1042         case LyXTabular::DELETE_COLUMN:
1043                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1044                         tabular.deleteColumn(sel_col_start);
1045                 if (sel_col_start >= tabular.columns())
1046                         --sel_col_start;
1047                 actcell = tabular.getCellNumber(row, sel_col_start);
1048                 cur.selection() = false;
1049                 break;
1050
1051         case LyXTabular::M_TOGGLE_LINE_TOP:
1052                 flag = false;
1053         case LyXTabular::TOGGLE_LINE_TOP: {
1054                 bool lineSet = !tabular.topLine(actcell, flag);
1055                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1056                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1057                                 tabular.setTopLine(
1058                                         tabular.getCellNumber(i, j),
1059                                         lineSet, flag);
1060                 break;
1061         }
1062
1063         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1064                 flag = false;
1065         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1066                 bool lineSet = !tabular.bottomLine(actcell, flag);
1067                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1068                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1069                                 tabular.setBottomLine(
1070                                         tabular.getCellNumber(i, j),
1071                                         lineSet,
1072                                         flag);
1073                 break;
1074         }
1075
1076         case LyXTabular::M_TOGGLE_LINE_LEFT:
1077                 flag = false;
1078         case LyXTabular::TOGGLE_LINE_LEFT: {
1079                 bool lineSet = !tabular.leftLine(actcell, flag);
1080                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1081                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1082                                 tabular.setLeftLine(
1083                                         tabular.getCellNumber(i,j),
1084                                         lineSet,
1085                                         flag);
1086                 break;
1087         }
1088
1089         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1090                 flag = false;
1091         case LyXTabular::TOGGLE_LINE_RIGHT: {
1092                 bool lineSet = !tabular.rightLine(actcell, flag);
1093                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1094                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1095                                 tabular.setRightLine(
1096                                         tabular.getCellNumber(i,j),
1097                                         lineSet,
1098                                         flag);
1099                 break;
1100         }
1101
1102         case LyXTabular::M_ALIGN_LEFT:
1103         case LyXTabular::M_ALIGN_RIGHT:
1104         case LyXTabular::M_ALIGN_CENTER:
1105                 flag = false;
1106         case LyXTabular::ALIGN_LEFT:
1107         case LyXTabular::ALIGN_RIGHT:
1108         case LyXTabular::ALIGN_CENTER:
1109         case LyXTabular::ALIGN_BLOCK:
1110                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1111                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1112                                 tabular.setAlignment(
1113                                         tabular.getCellNumber(i, j),
1114                                         setAlign,
1115                                         flag);
1116                 break;
1117
1118         case LyXTabular::M_VALIGN_TOP:
1119         case LyXTabular::M_VALIGN_BOTTOM:
1120         case LyXTabular::M_VALIGN_MIDDLE:
1121                 flag = false;
1122         case LyXTabular::VALIGN_TOP:
1123         case LyXTabular::VALIGN_BOTTOM:
1124         case LyXTabular::VALIGN_MIDDLE:
1125                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1126                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1127                                 tabular.setVAlignment(
1128                                         tabular.getCellNumber(i, j),
1129                                         setVAlign, flag);
1130                 break;
1131
1132         case LyXTabular::MULTICOLUMN: {
1133                 if (sel_row_start != sel_row_end) {
1134 #ifdef WITH_WARNINGS
1135 #warning Need I say it ? This is horrible.
1136 #endif
1137                         Alert::error(_("Error setting multicolumn"),
1138                                    _("You cannot set multicolumn vertically."));
1139                         return;
1140                 }
1141 #if 0
1142                 // just multicol for one Single Cell
1143                 if (!hasSelection()) {
1144                         // check wether we are completly in a multicol
1145                         if (tabular.isMultiColumn(actcell))
1146                                 tabular.unsetMultiColumn(actcell);
1147                         else
1148                                 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1149                         break;
1150                 }
1151                 // we have a selection so this means we just add all this
1152                 // cells to form a multicolumn cell
1153                 int s_start;
1154                 int s_end;
1155
1156                 if (sel_cell_start > sel_cell_end) {
1157                         s_start = sel_cell_end;
1158                         s_end = sel_cell_start;
1159                 } else {
1160                         s_start = sel_cell_start;
1161                         s_end = sel_cell_end;
1162                 }
1163                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1164                 actcell = s_start;
1165 #endif
1166                 cur.selection() = false;
1167                 break;
1168         }
1169
1170         case LyXTabular::SET_ALL_LINES:
1171                 setLines = true;
1172         case LyXTabular::UNSET_ALL_LINES:
1173 #if 0
1174                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1175                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1176                                 tabular.setAllLines(
1177                                         tabular.getCellNumber(i,j), setLines);
1178 #endif
1179                 break;
1180
1181         case LyXTabular::SET_LONGTABULAR:
1182                 tabular.setLongTabular(true);
1183                 break;
1184
1185         case LyXTabular::UNSET_LONGTABULAR:
1186                 tabular.setLongTabular(false);
1187                 break;
1188
1189         case LyXTabular::SET_ROTATE_TABULAR:
1190                 tabular.setRotateTabular(true);
1191                 break;
1192
1193         case LyXTabular::UNSET_ROTATE_TABULAR:
1194                 tabular.setRotateTabular(false);
1195                 break;
1196
1197         case LyXTabular::SET_ROTATE_CELL:
1198                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1199                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1200                                 tabular.setRotateCell(
1201                                         tabular.getCellNumber(i, j), true);
1202                 break;
1203
1204         case LyXTabular::UNSET_ROTATE_CELL:
1205                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1206                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1207                                 tabular.setRotateCell(
1208                                         tabular.getCellNumber(i, j), false);
1209                 break;
1210
1211         case LyXTabular::SET_USEBOX: {
1212                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1213                 if (val == tabular.getUsebox(actcell))
1214                         val = LyXTabular::BOX_NONE;
1215                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1216                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1217                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1218                 break;
1219         }
1220
1221         case LyXTabular::UNSET_LTFIRSTHEAD:
1222                 flag = false;
1223         case LyXTabular::SET_LTFIRSTHEAD:
1224                 tabular.getRowOfLTFirstHead(row, ltt);
1225                 checkLongtableSpecial(ltt, value, flag);
1226                 tabular.setLTHead(row, flag, ltt, true);
1227                 break;
1228
1229         case LyXTabular::UNSET_LTHEAD:
1230                 flag = false;
1231         case LyXTabular::SET_LTHEAD:
1232                 tabular.getRowOfLTHead(row, ltt);
1233                 checkLongtableSpecial(ltt, value, flag);
1234                 tabular.setLTHead(row, flag, ltt, false);
1235                 break;
1236
1237         case LyXTabular::UNSET_LTFOOT:
1238                 flag = false;
1239         case LyXTabular::SET_LTFOOT:
1240                 tabular.getRowOfLTFoot(row, ltt);
1241                 checkLongtableSpecial(ltt, value, flag);
1242                 tabular.setLTFoot(row, flag, ltt, false);
1243                 break;
1244
1245         case LyXTabular::UNSET_LTLASTFOOT:
1246                 flag = false;
1247         case LyXTabular::SET_LTLASTFOOT:
1248                 tabular.getRowOfLTLastFoot(row, ltt);
1249                 checkLongtableSpecial(ltt, value, flag);
1250                 tabular.setLTFoot(row, flag, ltt, true);
1251                 break;
1252
1253         case LyXTabular::SET_LTNEWPAGE:
1254                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1255                 break;
1256
1257         // dummy stuff just to avoid warnings
1258         case LyXTabular::LAST_ACTION:
1259                 break;
1260         }
1261
1262         InsetTabularMailer(*this).updateDialog(&bv);
1263 }
1264
1265
1266 bool InsetTabular::showInsetDialog(BufferView * bv) const
1267 {
1268         InsetTabularMailer(*this).showDialog(bv);
1269         return true;
1270 }
1271
1272
1273 void InsetTabular::openLayoutDialog(BufferView * bv) const
1274 {
1275         InsetTabularMailer(*this).showDialog(bv);
1276 }
1277
1278
1279 //
1280 // function returns an object as defined in func_status.h:
1281 // states OK, Unknown, Disabled, On, Off.
1282 //
1283 FuncStatus InsetTabular::getStatus(BufferView & bv,
1284         string const & what, int actcell) const
1285 {
1286         FuncStatus status;
1287         int action = LyXTabular::LAST_ACTION;   
1288         LCursor & cur = bv.cursor();
1289
1290         int i = 0;
1291         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1292                 string const tmp = tabularFeature[i].feature;
1293                 if (tmp == what.substr(0, tmp.length())) {
1294                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1295                         //   tabularFeatures[i].feature.length()))
1296                         action = tabularFeature[i].action;
1297                         break;
1298                 }
1299         }
1300         if (action == LyXTabular::LAST_ACTION) {
1301                 status.clear();
1302                 status.unknown(true);
1303                 return status;
1304         }
1305
1306         string const argument
1307                 = ltrim(what.substr(tabularFeature[i].feature.length()));
1308
1309         int sel_row_start = 0;
1310         int sel_row_end = 0;
1311         int dummy;
1312         LyXTabular::ltType dummyltt;
1313         bool flag = true;
1314
1315         getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
1316
1317         switch (action) {
1318         case LyXTabular::SET_PWIDTH:
1319         case LyXTabular::SET_MPWIDTH:
1320         case LyXTabular::SET_SPECIAL_COLUMN:
1321         case LyXTabular::SET_SPECIAL_MULTI:
1322         case LyXTabular::APPEND_ROW:
1323         case LyXTabular::APPEND_COLUMN:
1324         case LyXTabular::DELETE_ROW:
1325         case LyXTabular::DELETE_COLUMN:
1326         case LyXTabular::SET_ALL_LINES:
1327         case LyXTabular::UNSET_ALL_LINES:
1328                 status.clear();
1329                 return status;
1330
1331         case LyXTabular::MULTICOLUMN:
1332                 status.setOnOff(tabular.isMultiColumn(actcell));
1333                 break;
1334
1335         case LyXTabular::M_TOGGLE_LINE_TOP:
1336                 flag = false;
1337         case LyXTabular::TOGGLE_LINE_TOP:
1338                 status.setOnOff(tabular.topLine(actcell, flag));
1339                 break;
1340
1341         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1342                 flag = false;
1343         case LyXTabular::TOGGLE_LINE_BOTTOM:
1344                 status.setOnOff(tabular.bottomLine(actcell, flag));
1345                 break;
1346
1347         case LyXTabular::M_TOGGLE_LINE_LEFT:
1348                 flag = false;
1349         case LyXTabular::TOGGLE_LINE_LEFT:
1350                 status.setOnOff(tabular.leftLine(actcell, flag));
1351                 break;
1352
1353         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1354                 flag = false;
1355         case LyXTabular::TOGGLE_LINE_RIGHT:
1356                 status.setOnOff(tabular.rightLine(actcell, flag));
1357                 break;
1358
1359         case LyXTabular::M_ALIGN_LEFT:
1360                 flag = false;
1361         case LyXTabular::ALIGN_LEFT:
1362                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1363                 break;
1364
1365         case LyXTabular::M_ALIGN_RIGHT:
1366                 flag = false;
1367         case LyXTabular::ALIGN_RIGHT:
1368                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1369                 break;
1370
1371         case LyXTabular::M_ALIGN_CENTER:
1372                 flag = false;
1373         case LyXTabular::ALIGN_CENTER:
1374                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1375                 break;
1376
1377         case LyXTabular::ALIGN_BLOCK:
1378                 status.disabled(tabular.getPWidth(actcell).zero());
1379                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1380                 break;
1381
1382         case LyXTabular::M_VALIGN_TOP:
1383                 flag = false;
1384         case LyXTabular::VALIGN_TOP:
1385                 status.setOnOff(
1386                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1387                 break;
1388
1389         case LyXTabular::M_VALIGN_BOTTOM:
1390                 flag = false;
1391         case LyXTabular::VALIGN_BOTTOM:
1392                 status.setOnOff(
1393                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1394                 break;
1395
1396         case LyXTabular::M_VALIGN_MIDDLE:
1397                 flag = false;
1398         case LyXTabular::VALIGN_MIDDLE:
1399                 status.setOnOff(
1400                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1401                 break;
1402
1403         case LyXTabular::SET_LONGTABULAR:
1404                 status.setOnOff(tabular.isLongTabular());
1405                 break;
1406
1407         case LyXTabular::UNSET_LONGTABULAR:
1408                 status.setOnOff(!tabular.isLongTabular());
1409                 break;
1410
1411         case LyXTabular::SET_ROTATE_TABULAR:
1412                 status.setOnOff(tabular.getRotateTabular());
1413                 break;
1414
1415         case LyXTabular::UNSET_ROTATE_TABULAR:
1416                 status.setOnOff(!tabular.getRotateTabular());
1417                 break;
1418
1419         case LyXTabular::SET_ROTATE_CELL:
1420                 status.setOnOff(tabular.getRotateCell(actcell));
1421                 break;
1422
1423         case LyXTabular::UNSET_ROTATE_CELL:
1424                 status.setOnOff(!tabular.getRotateCell(actcell));
1425                 break;
1426
1427         case LyXTabular::SET_USEBOX:
1428                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1429                 break;
1430
1431         case LyXTabular::SET_LTFIRSTHEAD:
1432                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1433                 break;
1434
1435         case LyXTabular::SET_LTHEAD:
1436                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1437                 break;
1438
1439         case LyXTabular::SET_LTFOOT:
1440                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1441                 break;
1442
1443         case LyXTabular::SET_LTLASTFOOT:
1444                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1445                 break;
1446
1447         case LyXTabular::SET_LTNEWPAGE:
1448                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1449                 break;
1450
1451         default:
1452                 status.clear();
1453                 status.disabled(true);
1454                 break;
1455         }
1456         return status;
1457 }
1458
1459
1460 void InsetTabular::getLabelList(Buffer const & buffer,
1461                                 vector<string> & list) const
1462 {
1463         tabular.getLabelList(buffer, list);
1464 }
1465
1466
1467 bool InsetTabular::copySelection(LCursor & cur)
1468 {
1469         if (!cur.selection())
1470                 return false;
1471
1472         int rs, re, cs, ce;
1473         getSelection(cur, rs, re, cs, ce);
1474
1475         paste_tabular.reset(new LyXTabular(tabular));
1476         paste_tabular->setOwner(this);
1477
1478         for (int i = 0; i < rs; ++i)
1479                 paste_tabular->deleteRow(0);
1480
1481         int const rows = re - rs + 1;
1482         while (paste_tabular->rows() > rows)
1483                 paste_tabular->deleteRow(rows);
1484
1485         paste_tabular->setTopLine(0, true, true);
1486         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1487                                      true, true);
1488
1489         for (int i = 0; i < cs; ++i)
1490                 paste_tabular->deleteColumn(0);
1491
1492         int const columns = ce - cs + 1;
1493         while (paste_tabular->columns() > columns)
1494                 paste_tabular->deleteColumn(columns);
1495
1496         paste_tabular->setLeftLine(0, true, true);
1497         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1498                                     true, true);
1499
1500         ostringstream os;
1501         OutputParams const runparams;
1502         paste_tabular->plaintext(*cur.bv().buffer(), os, runparams, 0, true, '\t');
1503         cur.bv().stuffClipboard(os.str());
1504         return true;
1505 }
1506
1507
1508 bool InsetTabular::pasteSelection(LCursor & cur)
1509 {
1510         if (!paste_tabular)
1511                 return false;
1512         int actcell = cur.idx();
1513         int actcol = tabular.column_of_cell(actcell);
1514         int actrow = tabular.row_of_cell(actcell);
1515         for (int r1 = 0, r2 = actrow;
1516              r1 < paste_tabular->rows() && r2 < tabular.rows();
1517              ++r1, ++r2) {
1518                 for (int c1 = 0, c2 = actcol;
1519                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1520                     ++c1, ++c2) {
1521                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1522                             tabular.isPartOfMultiColumn(r2, c2))
1523                                 continue;
1524                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1525                                 --c2;
1526                                 continue;
1527                         }
1528                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1529                                 --c1;
1530                                 continue;
1531                         }
1532                         InsetText & inset = tabular.getCellInset(r2, c2);
1533                         inset = paste_tabular->getCellInset(r1, c1);
1534                         inset.setOwner(this);
1535                         inset.markNew();
1536                 }
1537         }
1538         return true;
1539 }
1540
1541
1542 void InsetTabular::cutSelection(LCursor & cur)
1543 {
1544         if (!cur.selection())
1545                 return;
1546
1547         bool const track = cur.bv().buffer()->params().tracking_changes;
1548         int rs, re, cs, ce;
1549         getSelection(cur, rs, re, cs, ce); 
1550         for (int i = rs; i <= re; ++i)
1551                 for (int j = cs; j <= ce; ++j)
1552                         cell(tabular.getCellNumber(i, j)).clear(track);
1553 }
1554
1555
1556 bool InsetTabular::isRightToLeft(LCursor & cur)
1557 {
1558         return cur.bv().getParentLanguage(this)->RightToLeft();
1559 }
1560
1561
1562 void InsetTabular::getSelection(LCursor & cur,
1563         int & rs, int & re, int & cs, int & ce) const
1564 {
1565         CursorSlice & beg = cur.selBegin();
1566         CursorSlice & end = cur.selEnd();
1567         cs = tabular.column_of_cell(beg.idx());
1568         ce = tabular.column_of_cell(end.idx());
1569         if (cs > ce) {
1570                 ce = cs;
1571                 cs = tabular.column_of_cell(end.idx());
1572         } else {
1573                 ce = tabular.right_column_of_cell(end.idx());
1574         }
1575
1576         rs = tabular.row_of_cell(beg.idx());
1577         re = tabular.row_of_cell(end.idx());
1578         if (rs > re)
1579                 swap(rs, re);
1580 }
1581
1582
1583 size_t InsetTabular::nargs() const
1584 {
1585         return tabular.getNumberOfCells();
1586 }
1587
1588
1589 LyXText * InsetTabular::getText(int idx) const
1590 {
1591         return size_t(idx) < nargs() ? cell(idx).getText(0) : 0;
1592 }
1593
1594
1595 void InsetTabular::markErased()
1596 {
1597         for (idx_type idx = 0; idx < nargs(); ++idx)
1598                 cell(idx).markErased();
1599 }
1600
1601
1602 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1603 {
1604 #if 0
1605         const int cell = tabular.getCellFromInset(in);
1606
1607         if (cell != -1)
1608                 return tabular.getPWidth(cell).zero();
1609
1610         // this is a workaround for a crash (New, Insert->Tabular,
1611         // Insert->FootNote)
1612         if (!owner())
1613                 return false;
1614
1615         // well we didn't obviously find it so maybe our owner knows more
1616         BOOST_ASSERT(owner());
1617         return owner()->forceDefaultParagraphs(in);
1618 #endif
1619         return false;
1620 }
1621
1622
1623 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1624                                      bool usePaste)
1625 {
1626         if (buf.length() <= 0)
1627                 return true;
1628
1629         int cols = 1;
1630         int rows = 1;
1631         int maxCols = 1;
1632         string::size_type len = buf.length();
1633         string::size_type p = 0;
1634
1635         int actcell = bv.cursor().idx();
1636         int actcol = tabular.column_of_cell(actcell);
1637         int actrow = tabular.row_of_cell(actcell);
1638
1639         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1640                 switch (buf[p]) {
1641                 case '\t':
1642                         ++cols;
1643                         break;
1644                 case '\n':
1645                         if (p + 1 < len)
1646                                 ++rows;
1647                         maxCols = max(cols, maxCols);
1648                         cols = 1;
1649                         break;
1650                 }
1651                 ++p;
1652         }
1653         maxCols = max(cols, maxCols);
1654         LyXTabular * loctab;
1655         int cell = 0;
1656         int ocol = 0;
1657         int row = 0;
1658         if (usePaste) {
1659                 paste_tabular.reset(
1660                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1661                 paste_tabular->setOwner(this);
1662                 loctab = paste_tabular.get();
1663                 cols = 0;
1664         } else {
1665                 loctab = &tabular;
1666                 cell = actcell;
1667                 ocol = actcol;
1668                 row = actrow;
1669         }
1670
1671         string::size_type op = 0;
1672         int cells = loctab->getNumberOfCells();
1673         p = 0;
1674         cols = ocol;
1675         rows = loctab->rows();
1676         int const columns = loctab->columns();
1677
1678         while (cell < cells && p < len && row < rows &&
1679                (p = buf.find_first_of("\t\n", p)) != string::npos)
1680         {
1681                 if (p >= len)
1682                         break;
1683                 switch (buf[p]) {
1684                 case '\t':
1685                         // we can only set this if we are not too far right
1686                         if (cols < columns) {
1687                                 InsetText & inset = loctab->getCellInset(cell);
1688                                 LyXFont const font = inset.text_.
1689                                         getFont(inset.paragraphs().begin(), 0);
1690                                 inset.setText(buf.substr(op, p - op), font);
1691                                 ++cols;
1692                                 ++cell;
1693                         }
1694                         break;
1695                 case '\n':
1696                         // we can only set this if we are not too far right
1697                         if (cols < columns) {
1698                                 InsetText & inset = tabular.getCellInset(cell);
1699                                 LyXFont const font = inset.text_.
1700                                         getFont(inset.paragraphs().begin(), 0);
1701                                 inset.setText(buf.substr(op, p - op), font);
1702                         }
1703                         cols = ocol;
1704                         ++row;
1705                         if (row < rows)
1706                                 cell = loctab->getCellNumber(row, cols);
1707                         break;
1708                 }
1709                 ++p;
1710                 op = p;
1711         }
1712         // check for the last cell if there is no trailing '\n'
1713         if (cell < cells && op < len) {
1714                 InsetText & inset = loctab->getCellInset(cell);
1715                 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
1716                 inset.setText(buf.substr(op, len - op), font);
1717         }
1718         return true;
1719 }
1720
1721
1722 void InsetTabular::addPreview(PreviewLoader & loader) const
1723 {
1724         int const rows = tabular.rows();
1725         int const columns = tabular.columns();
1726         for (int i = 0; i < rows; ++i) {
1727                 for (int j = 0; j < columns; ++j)
1728                         tabular.getCellInset(i, j).addPreview(loader);
1729         }
1730 }
1731
1732
1733 bool InsetTabular::tablemode(LCursor & cur) const
1734 {
1735         return cur.selBegin().idx() != cur.selEnd().idx();
1736 }
1737
1738
1739
1740
1741
1742 string const InsetTabularMailer::name_("tabular");
1743
1744 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1745         : inset_(const_cast<InsetTabular &>(inset))
1746 {}
1747
1748
1749 string const InsetTabularMailer::inset2string(Buffer const &) const
1750 {
1751         return params2string(inset_);
1752 }
1753
1754
1755 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1756 {
1757         istringstream data(in);
1758         LyXLex lex(0,0);
1759         lex.setStream(data);
1760
1761 #warning CHECK verify that this is a sane value to return.
1762         if (in.empty())
1763                 return -1;
1764
1765         if (lex.isOK()) {
1766                 lex.next();
1767                 string const token = lex.getString();
1768                 if (token != name_)
1769                         return -1;
1770         }
1771
1772         int cell = -1;
1773         if (lex.isOK()) {
1774                 lex.next();
1775                 string const token = lex.getString();
1776                 if (token != "\\active_cell")
1777                         return -1;
1778                 lex.next();
1779                 cell = lex.getInteger();
1780         }
1781
1782         // This is part of the inset proper that is usually swallowed
1783         // by Buffer::readInset
1784         if (lex.isOK()) {
1785                 lex.next();
1786                 string const token = lex.getString();
1787                 if (token != "Tabular")
1788                         return -1;
1789         }
1790
1791         if (!lex.isOK())
1792                 return -1;
1793
1794         Buffer const & buffer = inset.buffer();
1795         inset.read(buffer, lex);
1796
1797         // We can't set the active cell, but we can tell the frontend
1798         // what it is.
1799         return cell;
1800 }
1801
1802
1803 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1804 {
1805         ostringstream data;
1806 #warning wrong!
1807         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
1808         data << name_ << " \\active_cell " << 0 << '\n';
1809         inset.write(inset.buffer(), data);
1810         data << "\\end_inset\n";
1811         return data.str();
1812 }