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