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