]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
* insettabular.[Ch]: remove remains of the 'update' mechanism,
[lyx.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyx_cb.h"
27 #include "lyxlex.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "undo.h"
34
35 #include "frontends/Alert.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
39
40 #include "support/std_sstream.h"
41
42 #include <iostream>
43
44 using lyx::graphics::PreviewLoader;
45
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
49
50 using std::auto_ptr;
51 using std::endl;
52 using std::max;
53 using std::string;
54 using std::istringstream;
55 using std::ostream;
56 using std::ostringstream;
57 using std::swap;
58 using std::vector;
59
60
61 namespace {
62
63 int const ADD_TO_HEIGHT = 2;
64 int const ADD_TO_TABULAR_WIDTH = 2;
65
66 ///
67 boost::scoped_ptr<LyXTabular> paste_tabular;
68
69
70 struct TabularFeature {
71         LyXTabular::Feature action;
72         string feature;
73 };
74
75
76 TabularFeature tabularFeature[] =
77 {
78         { LyXTabular::APPEND_ROW, "append-row" },
79         { LyXTabular::APPEND_COLUMN, "append-column" },
80         { LyXTabular::DELETE_ROW, "delete-row" },
81         { LyXTabular::DELETE_COLUMN, "delete-column" },
82         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
83         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
84         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
85         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
86         { LyXTabular::ALIGN_LEFT, "align-left" },
87         { LyXTabular::ALIGN_RIGHT, "align-right" },
88         { LyXTabular::ALIGN_CENTER, "align-center" },
89         { LyXTabular::ALIGN_BLOCK, "align-block" },
90         { LyXTabular::VALIGN_TOP, "valign-top" },
91         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
92         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
93         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
94         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
95         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
96         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
97         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
98         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
99         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
100         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
101         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
102         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
103         { LyXTabular::MULTICOLUMN, "multicolumn" },
104         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
105         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
106         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
107         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
108         { LyXTabular::SET_PWIDTH, "set-pwidth" },
109         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
110         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
111         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
112         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
113         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
114         { LyXTabular::SET_USEBOX, "set-usebox" },
115         { LyXTabular::SET_LTHEAD, "set-lthead" },
116         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
117         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
118         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
119         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
120         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
121         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
122         { LyXTabular::LAST_ACTION, "" }
123 };
124
125
126 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
127 public:
128         FeatureEqual(LyXTabular::Feature feature)
129                 : feature_(feature) {}
130         bool operator()(TabularFeature const & tf) const {
131                 return tf.action == feature_;
132         }
133 private:
134         LyXTabular::Feature feature_;
135 };
136
137 } // namespace anon
138
139
140 string const featureAsString(LyXTabular::Feature feature)
141 {
142         TabularFeature * end = tabularFeature +
143                 sizeof(tabularFeature) / sizeof(TabularFeature);
144         TabularFeature * it = std::find_if(tabularFeature, end,
145                                            FeatureEqual(feature));
146         return (it == end) ? string() : it->feature;
147 }
148
149
150 bool InsetTabular::hasPasteBuffer() const
151 {
152         return (paste_tabular.get() != 0);
153 }
154
155
156 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
157         : tabular(buf.params(), max(rows, 1), max(columns, 1)),
158           buffer_(&buf), cursorx_(0)
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                                 resetPos(cur);
495                         }
496                 if (sl == cur.current())
497                         cur.dispatched(FINISHED_DOWN);
498                 break;
499
500         case LFUN_UPSEL:
501         case LFUN_UP:
502                 cell(cur.idx()).dispatch(cur, cmd);
503                 if (sl == cur.current())
504                         if (tabular.row_of_cell(cur.idx()) != 0) {
505                                 cur.idx() = tabular.getCellAbove(cur.idx());
506                                 resetPos(cur);
507                         }
508                 if (sl == cur.current())
509                         cur.dispatched(FINISHED_UP);
510                 break;
511
512         case LFUN_NEXT: {
513                 //if (hasSelection())
514                 //      cur.selection() = false;
515                 int actcell = cur.idx();
516                 int actcol = tabular.column_of_cell(actcell);
517                 int column = actcol;
518                 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
519                                 < yo_ + tabular.getHeightOfTabular())
520                 {
521                         cur.bv().scrollDocView(
522                                 cur.bv().top_y() + cur.bv().painter().paperHeight());
523                         cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
524                 } else {
525                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
526                 }
527                 resetPos(cur);
528                 break;
529         }
530
531         case LFUN_PRIOR: {
532                 //if (hasSelection())
533                 //      cur.selection() = false;
534                 int column = tabular.column_of_cell(cur.idx());
535                 if (yo_ < 0) {
536                         cur.bv().scrollDocView(
537                                 cur.bv().top_y() - cur.bv().painter().paperHeight());
538                         if (yo_ > 0)
539                                 cur.idx() = column;
540                         else
541                                 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
542                 } else {
543                         cur.idx() = column;
544                 }
545                 resetPos(cur);
546                 break;
547         }
548
549         case LFUN_LAYOUT_TABULAR:
550                 InsetTabularMailer(*this).showDialog(&cur.bv());
551                 break;
552
553         case LFUN_INSET_DIALOG_UPDATE:
554                 InsetTabularMailer(*this).updateDialog(&cur.bv());
555                 break;
556
557         case LFUN_TABULAR_FEATURE:
558                 if (!tabularFeatures(cur, cmd.argument))
559                         cur.notdispatched();
560                 break;
561
562         // insert file functions
563         case LFUN_FILE_INSERT_ASCII_PARA:
564         case LFUN_FILE_INSERT_ASCII: {
565                 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
566                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
567                         cur.notdispatched();
568                 break;
569         }
570
571         case LFUN_LANGUAGE:
572         case LFUN_EMPH:
573         case LFUN_BOLD:
574         case LFUN_NOUN:
575         case LFUN_CODE:
576         case LFUN_SANS:
577         case LFUN_ROMAN:
578         case LFUN_DEFAULT:
579         case LFUN_UNDERLINE:
580         case LFUN_FONT_SIZE:
581                 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
582                 break;
583
584         case LFUN_CUT:
585                 if (copySelection(cur)) {
586                         recordUndo(cur, Undo::DELETE);
587                         cutSelection(cur);
588                 }
589                 break;
590
591         case LFUN_BACKSPACE:
592         case LFUN_DELETE:
593                 recordUndo(cur, Undo::DELETE);
594                 if (tablemode(cur))
595                         cutSelection(cur);
596                 else
597                         cell(cur.idx()).dispatch(cur, cmd);
598                 break;
599
600         case LFUN_COPY:
601                 if (!cur.selection())
602                         break;
603                 finishUndo();
604                 copySelection(cur);
605                 break;
606
607         case LFUN_PASTESELECTION: {
608                 string const clip = cur.bv().getClipboard();
609                 if (clip.empty())
610                         break;
611                 if (clip.find('\t') != string::npos) {
612                         int cols = 1;
613                         int rows = 1;
614                         int maxCols = 1;
615                         size_t len = clip.length();
616                         for (size_t p = 0; p < len; ++p) {
617                                 p = clip.find_first_of("\t\n", p);
618                                 if (p == string::npos)
619                                         break;
620                                 switch (clip[p]) {
621                                 case '\t':
622                                         ++cols;
623                                         break;
624                                 case '\n':
625                                         if (p + 1 < len)
626                                                 ++rows;
627                                         maxCols = max(cols, maxCols);
628                                         cols = 1;
629                                         break;
630                                 }
631                         }
632                         maxCols = max(cols, maxCols);
633
634                         paste_tabular.reset(
635                                 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
636
637                         string::size_type op = 0;
638                         int cell = 0;
639                         int cells = paste_tabular->getNumberOfCells();
640                         cols = 0;
641                         LyXFont font;
642                         for (size_t p = 0; cell < cells && p < len; ++p) {
643                                 p = clip.find_first_of("\t\n", p);
644                                 if (p == string::npos || p >= len)
645                                         break;
646                                 switch (clip[p]) {
647                                 case '\t':
648                                         paste_tabular->getCellInset(cell).
649                                                 setText(clip.substr(op, p - op), font);
650                                         ++cols;
651                                         ++cell;
652                                         break;
653                                 case '\n':
654                                         paste_tabular->getCellInset(cell).
655                                                 setText(clip.substr(op, p - op), font);
656                                         while (cols++ < maxCols)
657                                                 ++cell;
658                                         cols = 0;
659                                         break;
660                                 }
661                                 op = p + 1;
662                         }
663                         // check for the last cell if there is no trailing '\n'
664                         if (cell < cells && op < len)
665                                 paste_tabular->getCellInset(cell).
666                                         setText(clip.substr(op, len - op), font);
667                 } else if (!insertAsciiString(cur.bv(), clip, true)) {
668                         // so that the clipboard is used and it goes on
669                         // to default
670                         // and executes LFUN_PASTESELECTION in insettext!
671                         paste_tabular.reset();
672                 }
673                 // fall through
674         }
675
676         case LFUN_PASTE:
677                 if (hasPasteBuffer()) {
678                         recordUndo(cur, Undo::INSERT);
679                         pasteSelection(cur);
680                         break;
681                 }
682                 cell(cur.idx()).dispatch(cur, cmd);
683                 break;
684
685         default:
686                 // we try to handle this event in the insets dispatch function.
687                 cell(cur.idx()).dispatch(cur, cmd);
688                 break;
689         }
690
691         InsetTabularMailer(*this).updateDialog(&cur.bv());
692 }
693
694
695 int InsetTabular::latex(Buffer const & buf, ostream & os,
696                         OutputParams const & runparams) const
697 {
698         return tabular.latex(buf, os, runparams);
699 }
700
701
702 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
703                         OutputParams const & runparams) const
704 {
705         int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
706         return tabular.plaintext(buf, os, runparams, dp, false, 0);
707 }
708
709
710 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
711                            OutputParams const & runparams) const
712 {
713         return tabular.linuxdoc(buf,os, runparams);
714 }
715
716
717 int InsetTabular::docbook(Buffer const & buf, ostream & os,
718                           OutputParams const & runparams) const
719 {
720         int ret = 0;
721         InsetOld * master;
722
723         // if the table is inside a float it doesn't need the informaltable
724         // wrapper. Search for it.
725         for (master = owner(); master; master = master->owner())
726                 if (master->lyxCode() == InsetOld::FLOAT_CODE)
727                         break;
728
729         if (!master) {
730                 os << "<informaltable>";
731                 if (runparams.mixed_content)
732                         os << endl;
733                 ++ret;
734         }
735         ret += tabular.docbook(buf, os, runparams);
736         if (!master) {
737                 os << "</informaltable>";
738                 if (runparams.mixed_content)
739                         os << endl;
740                 ++ret;
741         }
742         return ret;
743 }
744
745
746 void InsetTabular::validate(LaTeXFeatures & features) const
747 {
748         tabular.validate(features);
749 }
750
751
752 InsetText const & InsetTabular::cell(int idx) const
753 {
754         return tabular.getCellInset(idx);
755 }
756
757
758 InsetText & InsetTabular::cell(int idx)
759 {
760         return tabular.getCellInset(idx);
761 }
762
763
764 void InsetTabular::getCursorPos(CursorSlice const & cur, int & x, int & y) const
765 {
766         cell(cur.idx()).getCursorPos(cur, x, y);
767 }
768
769
770 InsetBase * InsetTabular::setPos(LCursor & cur, int x, int y) const
771 {
772         int idx_min = 0;
773         int dist_min = 1000000;
774         for (idx_type i = 0; i < nargs(); ++i) {
775                 int d = getText(i)->dist(x, y);
776                 if (d < dist_min) {
777                         dist_min = d;
778                         idx_min = i;
779                 }
780         }
781         cur.idx() = idx_min;
782         InsetBase * inset = cell(cur.idx()).text_.editXY(cur, x, y);
783         //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
784         return inset;
785 }
786
787
788 int InsetTabular::getCellXPos(int cell) const
789 {
790         int c = cell;
791
792         for (; !tabular.isFirstCellInRow(c); --c)
793                 ;
794         int lx = tabular.getWidthOfColumn(cell);
795         for (; c < cell; ++c)
796                 lx += tabular.getWidthOfColumn(c);
797
798         return lx - tabular.getWidthOfColumn(cell) + xo_;
799 }
800
801
802 void InsetTabular::resetPos(LCursor & cur) const
803 {
804         BufferView & bv = cur.bv();
805         int actcell = cur.idx();
806         int actcol = tabular.column_of_cell(actcell);
807
808         int const offset = ADD_TO_TABULAR_WIDTH + 2;
809         int new_x = getCellXPos(actcell) + offset;
810         int old_x = cursorx_;
811         cursorx_ = new_x;
812 //    cursor.x(getCellXPos(actcell) + offset);
813         if (actcol < tabular.columns() - 1 && scroll(false) &&
814                 tabular.getWidthOfTabular() < bv.workWidth()-20)
815         {
816                 scroll(bv, 0.0F);
817         } else if (cursorx_ - offset > 20 &&
818                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
819                    > bv.workWidth() - 20) {
820                 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
821         } else if (cursorx_ - offset < 20) {
822                 scroll(bv, 20 - cursorx_ + offset);
823         } else if (scroll() && xo_ > 20 &&
824                    xo_ + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
825                 scroll(bv, old_x - cursorx_);
826         }
827
828         InsetTabularMailer(*this).updateDialog(&bv);
829 }
830
831
832 void InsetTabular::moveNextCell(LCursor & cur)
833 {
834         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
835         if (isRightToLeft(cur)) {
836                 if (tabular.isFirstCellInRow(cur.idx())) {
837                         int row = tabular.row_of_cell(cur.idx());
838                         if (row == tabular.rows() - 1)
839                                 return;
840                         cur.idx() = tabular.getLastCellInRow(row);
841                         cur.idx() = tabular.getCellBelow(cur.idx());
842                 } else {
843                         if (cur.idx() == 0)
844                                 return;
845                         --cur.idx();
846                 }
847         } else {
848                 if (tabular.isLastCell(cur.idx()))
849                         return;
850                 ++cur.idx();
851         }
852         cur.par() = 0;
853         cur.pos() = 0;
854         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
855         resetPos(cur);
856 }
857
858
859 void InsetTabular::movePrevCell(LCursor & cur)
860 {
861         if (isRightToLeft(cur)) {
862                 if (tabular.isLastCellInRow(cur.idx())) {
863                         int row = tabular.row_of_cell(cur.idx());
864                         if (row == 0)
865                                 return;
866                         cur.idx() = tabular.getFirstCellInRow(row);
867                         cur.idx() = tabular.getCellAbove(cur.idx());
868                 } else {
869                         if (tabular.isLastCell(cur.idx()))
870                                 return;
871                         ++cur.idx();
872                 }
873         } else {
874                 if (cur.idx() == 0) // first cell
875                         return;
876                 --cur.idx();
877         }
878         cur.par() = 0;
879         cur.pos() = 0;
880         resetPos(cur);
881 }
882
883
884 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
885 {
886         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
887
888         int i = 0;
889         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
890                 string const tmp = tabularFeature[i].feature;
891
892                 if (tmp == what.substr(0, tmp.length())) {
893                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
894                         //tabularFeatures[i].feature.length())) {
895                         action = tabularFeature[i].action;
896                         break;
897                 }
898         }
899         if (action == LyXTabular::LAST_ACTION)
900                 return false;
901
902         string const val =
903                 ltrim(what.substr(tabularFeature[i].feature.length()));
904         tabularFeatures(cur, action, val);
905         return true;
906 }
907
908
909 namespace {
910
911 void checkLongtableSpecial(LyXTabular::ltType & ltt,
912                           string const & special, bool & flag)
913 {
914         if (special == "dl_above") {
915                 ltt.topDL = flag;
916                 ltt.set = false;
917         } else if (special == "dl_below") {
918                 ltt.bottomDL = flag;
919                 ltt.set = false;
920         } else if (special == "empty") {
921                 ltt.empty = flag;
922                 ltt.set = false;
923         } else if (flag) {
924                 ltt.empty = false;
925                 ltt.set = true;
926         }
927 }
928
929 } // anon namespace
930
931
932 void InsetTabular::tabularFeatures(LCursor & cur,
933         LyXTabular::Feature feature, string const & value)
934 {
935         BufferView & bv = cur.bv();
936         int actcell = cur.idx();
937         int sel_col_start;
938         int sel_col_end;
939         int sel_row_start;
940         int sel_row_end;
941         bool setLines = false;
942         LyXAlignment setAlign = LYX_ALIGN_LEFT;
943         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
944
945         switch (feature) {
946
947         case LyXTabular::M_ALIGN_LEFT:
948         case LyXTabular::ALIGN_LEFT:
949                 setAlign = LYX_ALIGN_LEFT;
950                 break;
951
952         case LyXTabular::M_ALIGN_RIGHT:
953         case LyXTabular::ALIGN_RIGHT:
954                 setAlign = LYX_ALIGN_RIGHT;
955                 break;
956
957         case LyXTabular::M_ALIGN_CENTER:
958         case LyXTabular::ALIGN_CENTER:
959                 setAlign = LYX_ALIGN_CENTER;
960                 break;
961
962         case LyXTabular::ALIGN_BLOCK:
963                 setAlign = LYX_ALIGN_BLOCK;
964                 break;
965
966         case LyXTabular::M_VALIGN_TOP:
967         case LyXTabular::VALIGN_TOP:
968                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
969                 break;
970
971         case LyXTabular::M_VALIGN_BOTTOM:
972         case LyXTabular::VALIGN_BOTTOM:
973                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
974                 break;
975
976         case LyXTabular::M_VALIGN_MIDDLE:
977         case LyXTabular::VALIGN_MIDDLE:
978                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
979                 break;
980
981         default:
982                 break;
983         }
984
985         recordUndo(cur, Undo::ATOMIC);
986
987         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
988         int row =  tabular.row_of_cell(actcell);
989         int column = tabular.column_of_cell(actcell);
990         bool flag = true;
991         LyXTabular::ltType ltt;
992
993         switch (feature) {
994
995         case LyXTabular::SET_PWIDTH: {
996                 LyXLength const len(value);
997                 tabular.setColumnPWidth(actcell, len);
998                 if (len.zero()
999                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1000                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1001                 else if (!len.zero()
1002                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1003                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1004                 break;
1005         }
1006
1007         case LyXTabular::SET_MPWIDTH:
1008                 tabular.setMColumnPWidth(actcell, LyXLength(value));
1009                 break;
1010
1011         case LyXTabular::SET_SPECIAL_COLUMN:
1012         case LyXTabular::SET_SPECIAL_MULTI:
1013                 tabular.setAlignSpecial(actcell,value,feature);
1014                 break;
1015
1016         case LyXTabular::APPEND_ROW:
1017                 // append the row into the tabular
1018                 tabular.appendRow(bv.buffer()->params(), actcell);
1019                 tabular.setOwner(this);
1020                 break;
1021
1022         case LyXTabular::APPEND_COLUMN:
1023                 // append the column into the tabular
1024                 tabular.appendColumn(bv.buffer()->params(), actcell);
1025                 tabular.setOwner(this);
1026                 actcell = tabular.getCellNumber(row, column);
1027                 break;
1028
1029         case LyXTabular::DELETE_ROW:
1030                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1031                         tabular.deleteRow(sel_row_start);
1032                 if (sel_row_start >= tabular.rows())
1033                         --sel_row_start;
1034                 actcell = tabular.getCellNumber(sel_row_start, column);
1035                 cur.selection() = false;
1036                 break;
1037
1038         case LyXTabular::DELETE_COLUMN:
1039                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1040                         tabular.deleteColumn(sel_col_start);
1041                 if (sel_col_start >= tabular.columns())
1042                         --sel_col_start;
1043                 actcell = tabular.getCellNumber(row, sel_col_start);
1044                 cur.selection() = false;
1045                 break;
1046
1047         case LyXTabular::M_TOGGLE_LINE_TOP:
1048                 flag = false;
1049         case LyXTabular::TOGGLE_LINE_TOP: {
1050                 bool lineSet = !tabular.topLine(actcell, flag);
1051                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1052                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1053                                 tabular.setTopLine(
1054                                         tabular.getCellNumber(i, j),
1055                                         lineSet, flag);
1056                 break;
1057         }
1058
1059         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1060                 flag = false;
1061         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1062                 bool lineSet = !tabular.bottomLine(actcell, flag);
1063                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1064                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1065                                 tabular.setBottomLine(
1066                                         tabular.getCellNumber(i, j),
1067                                         lineSet,
1068                                         flag);
1069                 break;
1070         }
1071
1072         case LyXTabular::M_TOGGLE_LINE_LEFT:
1073                 flag = false;
1074         case LyXTabular::TOGGLE_LINE_LEFT: {
1075                 bool lineSet = !tabular.leftLine(actcell, flag);
1076                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1077                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1078                                 tabular.setLeftLine(
1079                                         tabular.getCellNumber(i,j),
1080                                         lineSet,
1081                                         flag);
1082                 break;
1083         }
1084
1085         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1086                 flag = false;
1087         case LyXTabular::TOGGLE_LINE_RIGHT: {
1088                 bool lineSet = !tabular.rightLine(actcell, flag);
1089                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1090                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1091                                 tabular.setRightLine(
1092                                         tabular.getCellNumber(i,j),
1093                                         lineSet,
1094                                         flag);
1095                 break;
1096         }
1097
1098         case LyXTabular::M_ALIGN_LEFT:
1099         case LyXTabular::M_ALIGN_RIGHT:
1100         case LyXTabular::M_ALIGN_CENTER:
1101                 flag = false;
1102         case LyXTabular::ALIGN_LEFT:
1103         case LyXTabular::ALIGN_RIGHT:
1104         case LyXTabular::ALIGN_CENTER:
1105         case LyXTabular::ALIGN_BLOCK:
1106                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1107                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1108                                 tabular.setAlignment(
1109                                         tabular.getCellNumber(i, j),
1110                                         setAlign,
1111                                         flag);
1112                 break;
1113
1114         case LyXTabular::M_VALIGN_TOP:
1115         case LyXTabular::M_VALIGN_BOTTOM:
1116         case LyXTabular::M_VALIGN_MIDDLE:
1117                 flag = false;
1118         case LyXTabular::VALIGN_TOP:
1119         case LyXTabular::VALIGN_BOTTOM:
1120         case LyXTabular::VALIGN_MIDDLE:
1121                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1122                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1123                                 tabular.setVAlignment(
1124                                         tabular.getCellNumber(i, j),
1125                                         setVAlign, flag);
1126                 break;
1127
1128         case LyXTabular::MULTICOLUMN: {
1129                 if (sel_row_start != sel_row_end) {
1130 #ifdef WITH_WARNINGS
1131 #warning Need I say it ? This is horrible.
1132 #endif
1133                         Alert::error(_("Error setting multicolumn"),
1134                                    _("You cannot set multicolumn vertically."));
1135                         return;
1136                 }
1137 #if 0
1138                 // just multicol for one Single Cell
1139                 if (!hasSelection()) {
1140                         // check wether we are completly in a multicol
1141                         if (tabular.isMultiColumn(actcell))
1142                                 tabular.unsetMultiColumn(actcell);
1143                         else
1144                                 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1145                         break;
1146                 }
1147                 // we have a selection so this means we just add all this
1148                 // cells to form a multicolumn cell
1149                 int s_start;
1150                 int s_end;
1151
1152                 if (sel_cell_start > sel_cell_end) {
1153                         s_start = sel_cell_end;
1154                         s_end = sel_cell_start;
1155                 } else {
1156                         s_start = sel_cell_start;
1157                         s_end = sel_cell_end;
1158                 }
1159                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1160                 actcell = s_start;
1161 #endif
1162                 cur.selection() = false;
1163                 break;
1164         }
1165
1166         case LyXTabular::SET_ALL_LINES:
1167                 setLines = true;
1168         case LyXTabular::UNSET_ALL_LINES:
1169 #if 0
1170                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1171                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1172                                 tabular.setAllLines(
1173                                         tabular.getCellNumber(i,j), setLines);
1174 #endif
1175                 break;
1176
1177         case LyXTabular::SET_LONGTABULAR:
1178                 tabular.setLongTabular(true);
1179                 break;
1180
1181         case LyXTabular::UNSET_LONGTABULAR:
1182                 tabular.setLongTabular(false);
1183                 break;
1184
1185         case LyXTabular::SET_ROTATE_TABULAR:
1186                 tabular.setRotateTabular(true);
1187                 break;
1188
1189         case LyXTabular::UNSET_ROTATE_TABULAR:
1190                 tabular.setRotateTabular(false);
1191                 break;
1192
1193         case LyXTabular::SET_ROTATE_CELL:
1194                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1195                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1196                                 tabular.setRotateCell(
1197                                         tabular.getCellNumber(i, j), true);
1198                 break;
1199
1200         case LyXTabular::UNSET_ROTATE_CELL:
1201                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1202                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1203                                 tabular.setRotateCell(
1204                                         tabular.getCellNumber(i, j), false);
1205                 break;
1206
1207         case LyXTabular::SET_USEBOX: {
1208                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1209                 if (val == tabular.getUsebox(actcell))
1210                         val = LyXTabular::BOX_NONE;
1211                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1212                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1213                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1214                 break;
1215         }
1216
1217         case LyXTabular::UNSET_LTFIRSTHEAD:
1218                 flag = false;
1219         case LyXTabular::SET_LTFIRSTHEAD:
1220                 tabular.getRowOfLTFirstHead(row, ltt);
1221                 checkLongtableSpecial(ltt, value, flag);
1222                 tabular.setLTHead(row, flag, ltt, true);
1223                 break;
1224
1225         case LyXTabular::UNSET_LTHEAD:
1226                 flag = false;
1227         case LyXTabular::SET_LTHEAD:
1228                 tabular.getRowOfLTHead(row, ltt);
1229                 checkLongtableSpecial(ltt, value, flag);
1230                 tabular.setLTHead(row, flag, ltt, false);
1231                 break;
1232
1233         case LyXTabular::UNSET_LTFOOT:
1234                 flag = false;
1235         case LyXTabular::SET_LTFOOT:
1236                 tabular.getRowOfLTFoot(row, ltt);
1237                 checkLongtableSpecial(ltt, value, flag);
1238                 tabular.setLTFoot(row, flag, ltt, false);
1239                 break;
1240
1241         case LyXTabular::UNSET_LTLASTFOOT:
1242                 flag = false;
1243         case LyXTabular::SET_LTLASTFOOT:
1244                 tabular.getRowOfLTLastFoot(row, ltt);
1245                 checkLongtableSpecial(ltt, value, flag);
1246                 tabular.setLTFoot(row, flag, ltt, true);
1247                 break;
1248
1249         case LyXTabular::SET_LTNEWPAGE:
1250                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1251                 break;
1252
1253         // dummy stuff just to avoid warnings
1254         case LyXTabular::LAST_ACTION:
1255                 break;
1256         }
1257
1258         InsetTabularMailer(*this).updateDialog(&bv);
1259 }
1260
1261
1262 bool InsetTabular::showInsetDialog(BufferView * bv) const
1263 {
1264         InsetTabularMailer(*this).showDialog(bv);
1265         return true;
1266 }
1267
1268
1269 void InsetTabular::openLayoutDialog(BufferView * bv) const
1270 {
1271         InsetTabularMailer(*this).showDialog(bv);
1272 }
1273
1274
1275 //
1276 // function returns an object as defined in func_status.h:
1277 // states OK, Unknown, Disabled, On, Off.
1278 //
1279 FuncStatus InsetTabular::getStatus(BufferView & bv,
1280         string const & what, int actcell) const
1281 {
1282         FuncStatus status;
1283         int action = LyXTabular::LAST_ACTION;   
1284         LCursor & cur = bv.cursor();
1285
1286         int i = 0;
1287         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1288                 string const tmp = tabularFeature[i].feature;
1289                 if (tmp == what.substr(0, tmp.length())) {
1290                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1291                         //   tabularFeatures[i].feature.length()))
1292                         action = tabularFeature[i].action;
1293                         break;
1294                 }
1295         }
1296         if (action == LyXTabular::LAST_ACTION) {
1297                 status.clear();
1298                 status.unknown(true);
1299                 return status;
1300         }
1301
1302         string const argument
1303                 = ltrim(what.substr(tabularFeature[i].feature.length()));
1304
1305         int sel_row_start = 0;
1306         int sel_row_end = 0;
1307         int dummy;
1308         LyXTabular::ltType dummyltt;
1309         bool flag = true;
1310
1311         getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
1312
1313         switch (action) {
1314         case LyXTabular::SET_PWIDTH:
1315         case LyXTabular::SET_MPWIDTH:
1316         case LyXTabular::SET_SPECIAL_COLUMN:
1317         case LyXTabular::SET_SPECIAL_MULTI:
1318         case LyXTabular::APPEND_ROW:
1319         case LyXTabular::APPEND_COLUMN:
1320         case LyXTabular::DELETE_ROW:
1321         case LyXTabular::DELETE_COLUMN:
1322         case LyXTabular::SET_ALL_LINES:
1323         case LyXTabular::UNSET_ALL_LINES:
1324                 status.clear();
1325                 return status;
1326
1327         case LyXTabular::MULTICOLUMN:
1328                 status.setOnOff(tabular.isMultiColumn(actcell));
1329                 break;
1330
1331         case LyXTabular::M_TOGGLE_LINE_TOP:
1332                 flag = false;
1333         case LyXTabular::TOGGLE_LINE_TOP:
1334                 status.setOnOff(tabular.topLine(actcell, flag));
1335                 break;
1336
1337         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1338                 flag = false;
1339         case LyXTabular::TOGGLE_LINE_BOTTOM:
1340                 status.setOnOff(tabular.bottomLine(actcell, flag));
1341                 break;
1342
1343         case LyXTabular::M_TOGGLE_LINE_LEFT:
1344                 flag = false;
1345         case LyXTabular::TOGGLE_LINE_LEFT:
1346                 status.setOnOff(tabular.leftLine(actcell, flag));
1347                 break;
1348
1349         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1350                 flag = false;
1351         case LyXTabular::TOGGLE_LINE_RIGHT:
1352                 status.setOnOff(tabular.rightLine(actcell, flag));
1353                 break;
1354
1355         case LyXTabular::M_ALIGN_LEFT:
1356                 flag = false;
1357         case LyXTabular::ALIGN_LEFT:
1358                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1359                 break;
1360
1361         case LyXTabular::M_ALIGN_RIGHT:
1362                 flag = false;
1363         case LyXTabular::ALIGN_RIGHT:
1364                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1365                 break;
1366
1367         case LyXTabular::M_ALIGN_CENTER:
1368                 flag = false;
1369         case LyXTabular::ALIGN_CENTER:
1370                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1371                 break;
1372
1373         case LyXTabular::ALIGN_BLOCK:
1374                 status.disabled(tabular.getPWidth(actcell).zero());
1375                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1376                 break;
1377
1378         case LyXTabular::M_VALIGN_TOP:
1379                 flag = false;
1380         case LyXTabular::VALIGN_TOP:
1381                 status.setOnOff(
1382                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1383                 break;
1384
1385         case LyXTabular::M_VALIGN_BOTTOM:
1386                 flag = false;
1387         case LyXTabular::VALIGN_BOTTOM:
1388                 status.setOnOff(
1389                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1390                 break;
1391
1392         case LyXTabular::M_VALIGN_MIDDLE:
1393                 flag = false;
1394         case LyXTabular::VALIGN_MIDDLE:
1395                 status.setOnOff(
1396                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1397                 break;
1398
1399         case LyXTabular::SET_LONGTABULAR:
1400                 status.setOnOff(tabular.isLongTabular());
1401                 break;
1402
1403         case LyXTabular::UNSET_LONGTABULAR:
1404                 status.setOnOff(!tabular.isLongTabular());
1405                 break;
1406
1407         case LyXTabular::SET_ROTATE_TABULAR:
1408                 status.setOnOff(tabular.getRotateTabular());
1409                 break;
1410
1411         case LyXTabular::UNSET_ROTATE_TABULAR:
1412                 status.setOnOff(!tabular.getRotateTabular());
1413                 break;
1414
1415         case LyXTabular::SET_ROTATE_CELL:
1416                 status.setOnOff(tabular.getRotateCell(actcell));
1417                 break;
1418
1419         case LyXTabular::UNSET_ROTATE_CELL:
1420                 status.setOnOff(!tabular.getRotateCell(actcell));
1421                 break;
1422
1423         case LyXTabular::SET_USEBOX:
1424                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1425                 break;
1426
1427         case LyXTabular::SET_LTFIRSTHEAD:
1428                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1429                 break;
1430
1431         case LyXTabular::SET_LTHEAD:
1432                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1433                 break;
1434
1435         case LyXTabular::SET_LTFOOT:
1436                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1437                 break;
1438
1439         case LyXTabular::SET_LTLASTFOOT:
1440                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1441                 break;
1442
1443         case LyXTabular::SET_LTNEWPAGE:
1444                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1445                 break;
1446
1447         default:
1448                 status.clear();
1449                 status.disabled(true);
1450                 break;
1451         }
1452         return status;
1453 }
1454
1455
1456 void InsetTabular::getLabelList(Buffer const & buffer,
1457                                 vector<string> & list) const
1458 {
1459         tabular.getLabelList(buffer, list);
1460 }
1461
1462
1463 bool InsetTabular::copySelection(LCursor & cur)
1464 {
1465         if (!cur.selection())
1466                 return false;
1467
1468         int rs, re, cs, ce;
1469         getSelection(cur, rs, re, cs, ce);
1470
1471         paste_tabular.reset(new LyXTabular(tabular));
1472         paste_tabular->setOwner(this);
1473
1474         for (int i = 0; i < rs; ++i)
1475                 paste_tabular->deleteRow(0);
1476
1477         int const rows = re - rs + 1;
1478         while (paste_tabular->rows() > rows)
1479                 paste_tabular->deleteRow(rows);
1480
1481         paste_tabular->setTopLine(0, true, true);
1482         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1483                                      true, true);
1484
1485         for (int i = 0; i < cs; ++i)
1486                 paste_tabular->deleteColumn(0);
1487
1488         int const columns = ce - cs + 1;
1489         while (paste_tabular->columns() > columns)
1490                 paste_tabular->deleteColumn(columns);
1491
1492         paste_tabular->setLeftLine(0, true, true);
1493         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1494                                     true, true);
1495
1496         ostringstream os;
1497         OutputParams const runparams;
1498         paste_tabular->plaintext(*cur.bv().buffer(), os, runparams, 0, true, '\t');
1499         cur.bv().stuffClipboard(os.str());
1500         return true;
1501 }
1502
1503
1504 bool InsetTabular::pasteSelection(LCursor & cur)
1505 {
1506         if (!paste_tabular)
1507                 return false;
1508         int actcell = cur.idx();
1509         int actcol = tabular.column_of_cell(actcell);
1510         int actrow = tabular.row_of_cell(actcell);
1511         for (int r1 = 0, r2 = actrow;
1512              r1 < paste_tabular->rows() && r2 < tabular.rows();
1513              ++r1, ++r2) {
1514                 for (int c1 = 0, c2 = actcol;
1515                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1516                     ++c1, ++c2) {
1517                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1518                             tabular.isPartOfMultiColumn(r2, c2))
1519                                 continue;
1520                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1521                                 --c2;
1522                                 continue;
1523                         }
1524                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1525                                 --c1;
1526                                 continue;
1527                         }
1528                         InsetText & inset = tabular.getCellInset(r2, c2);
1529                         inset = paste_tabular->getCellInset(r1, c1);
1530                         inset.setOwner(this);
1531                         inset.markNew();
1532                 }
1533         }
1534         return true;
1535 }
1536
1537
1538 void InsetTabular::cutSelection(LCursor & cur)
1539 {
1540         if (!cur.selection())
1541                 return;
1542
1543         bool const track = cur.bv().buffer()->params().tracking_changes;
1544         int rs, re, cs, ce;
1545         getSelection(cur, rs, re, cs, ce); 
1546         for (int i = rs; i <= re; ++i)
1547                 for (int j = cs; j <= ce; ++j)
1548                         cell(tabular.getCellNumber(i, j)).clear(track);
1549 }
1550
1551
1552 bool InsetTabular::isRightToLeft(LCursor & cur)
1553 {
1554         return cur.bv().getParentLanguage(this)->RightToLeft();
1555 }
1556
1557
1558 void InsetTabular::getSelection(LCursor & cur,
1559         int & rs, int & re, int & cs, int & ce) const
1560 {
1561         CursorSlice & beg = cur.selBegin();
1562         CursorSlice & end = cur.selEnd();
1563         cs = tabular.column_of_cell(beg.idx());
1564         ce = tabular.column_of_cell(end.idx());
1565         if (cs > ce) {
1566                 ce = cs;
1567                 cs = tabular.column_of_cell(end.idx());
1568         } else {
1569                 ce = tabular.right_column_of_cell(end.idx());
1570         }
1571
1572         rs = tabular.row_of_cell(beg.idx());
1573         re = tabular.row_of_cell(end.idx());
1574         if (rs > re)
1575                 swap(rs, re);
1576 }
1577
1578
1579 size_t InsetTabular::nargs() const
1580 {
1581         return tabular.getNumberOfCells();
1582 }
1583
1584
1585 LyXText * InsetTabular::getText(int idx) const
1586 {
1587         return size_t(idx) < nargs() ? cell(idx).getText(0) : 0;
1588 }
1589
1590
1591 void InsetTabular::markErased()
1592 {
1593         for (idx_type idx = 0; idx < nargs(); ++idx)
1594                 cell(idx).markErased();
1595 }
1596
1597
1598 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1599 {
1600 #if 0
1601         const int cell = tabular.getCellFromInset(in);
1602
1603         if (cell != -1)
1604                 return tabular.getPWidth(cell).zero();
1605
1606         // this is a workaround for a crash (New, Insert->Tabular,
1607         // Insert->FootNote)
1608         if (!owner())
1609                 return false;
1610
1611         // well we didn't obviously find it so maybe our owner knows more
1612         BOOST_ASSERT(owner());
1613         return owner()->forceDefaultParagraphs(in);
1614 #endif
1615         return false;
1616 }
1617
1618
1619 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1620                                      bool usePaste)
1621 {
1622         if (buf.length() <= 0)
1623                 return true;
1624
1625         int cols = 1;
1626         int rows = 1;
1627         int maxCols = 1;
1628         string::size_type len = buf.length();
1629         string::size_type p = 0;
1630
1631         int actcell = bv.cursor().idx();
1632         int actcol = tabular.column_of_cell(actcell);
1633         int actrow = tabular.row_of_cell(actcell);
1634
1635         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1636                 switch (buf[p]) {
1637                 case '\t':
1638                         ++cols;
1639                         break;
1640                 case '\n':
1641                         if (p + 1 < len)
1642                                 ++rows;
1643                         maxCols = max(cols, maxCols);
1644                         cols = 1;
1645                         break;
1646                 }
1647                 ++p;
1648         }
1649         maxCols = max(cols, maxCols);
1650         LyXTabular * loctab;
1651         int cell = 0;
1652         int ocol = 0;
1653         int row = 0;
1654         if (usePaste) {
1655                 paste_tabular.reset(
1656                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1657                 paste_tabular->setOwner(this);
1658                 loctab = paste_tabular.get();
1659                 cols = 0;
1660         } else {
1661                 loctab = &tabular;
1662                 cell = actcell;
1663                 ocol = actcol;
1664                 row = actrow;
1665         }
1666
1667         string::size_type op = 0;
1668         int cells = loctab->getNumberOfCells();
1669         p = 0;
1670         cols = ocol;
1671         rows = loctab->rows();
1672         int const columns = loctab->columns();
1673
1674         while (cell < cells && p < len && row < rows &&
1675                (p = buf.find_first_of("\t\n", p)) != string::npos)
1676         {
1677                 if (p >= len)
1678                         break;
1679                 switch (buf[p]) {
1680                 case '\t':
1681                         // we can only set this if we are not too far right
1682                         if (cols < columns) {
1683                                 InsetText & inset = loctab->getCellInset(cell);
1684                                 LyXFont const font = inset.text_.
1685                                         getFont(inset.paragraphs().begin(), 0);
1686                                 inset.setText(buf.substr(op, p - op), font);
1687                                 ++cols;
1688                                 ++cell;
1689                         }
1690                         break;
1691                 case '\n':
1692                         // we can only set this if we are not too far right
1693                         if (cols < columns) {
1694                                 InsetText & inset = tabular.getCellInset(cell);
1695                                 LyXFont const font = inset.text_.
1696                                         getFont(inset.paragraphs().begin(), 0);
1697                                 inset.setText(buf.substr(op, p - op), font);
1698                         }
1699                         cols = ocol;
1700                         ++row;
1701                         if (row < rows)
1702                                 cell = loctab->getCellNumber(row, cols);
1703                         break;
1704                 }
1705                 ++p;
1706                 op = p;
1707         }
1708         // check for the last cell if there is no trailing '\n'
1709         if (cell < cells && op < len) {
1710                 InsetText & inset = loctab->getCellInset(cell);
1711                 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
1712                 inset.setText(buf.substr(op, len - op), font);
1713         }
1714         return true;
1715 }
1716
1717
1718 void InsetTabular::addPreview(PreviewLoader & loader) const
1719 {
1720         int const rows = tabular.rows();
1721         int const columns = tabular.columns();
1722         for (int i = 0; i < rows; ++i) {
1723                 for (int j = 0; j < columns; ++j)
1724                         tabular.getCellInset(i, j).addPreview(loader);
1725         }
1726 }
1727
1728
1729 bool InsetTabular::tablemode(LCursor & cur) const
1730 {
1731         return cur.selBegin().idx() != cur.selEnd().idx();
1732 }
1733
1734
1735
1736
1737
1738 string const InsetTabularMailer::name_("tabular");
1739
1740 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1741         : inset_(const_cast<InsetTabular &>(inset))
1742 {}
1743
1744
1745 string const InsetTabularMailer::inset2string(Buffer const &) const
1746 {
1747         return params2string(inset_);
1748 }
1749
1750
1751 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1752 {
1753         istringstream data(in);
1754         LyXLex lex(0,0);
1755         lex.setStream(data);
1756
1757 #warning CHECK verify that this is a sane value to return.
1758         if (in.empty())
1759                 return -1;
1760
1761         if (lex.isOK()) {
1762                 lex.next();
1763                 string const token = lex.getString();
1764                 if (token != name_)
1765                         return -1;
1766         }
1767
1768         int cell = -1;
1769         if (lex.isOK()) {
1770                 lex.next();
1771                 string const token = lex.getString();
1772                 if (token != "\\active_cell")
1773                         return -1;
1774                 lex.next();
1775                 cell = lex.getInteger();
1776         }
1777
1778         // This is part of the inset proper that is usually swallowed
1779         // by Buffer::readInset
1780         if (lex.isOK()) {
1781                 lex.next();
1782                 string const token = lex.getString();
1783                 if (token != "Tabular")
1784                         return -1;
1785         }
1786
1787         if (!lex.isOK())
1788                 return -1;
1789
1790         Buffer const & buffer = inset.buffer();
1791         inset.read(buffer, lex);
1792
1793         // We can't set the active cell, but we can tell the frontend
1794         // what it is.
1795         return cell;
1796 }
1797
1798
1799 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1800 {
1801         ostringstream data;
1802 #warning wrong!
1803         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
1804         data << name_ << " \\active_cell " << 0 << '\n';
1805         inset.write(inset.buffer(), data);
1806         data << "\\end_inset\n";
1807         return data.str();
1808 }