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