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