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