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