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