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