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