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