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