]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
This commit replaces BufferView->LyXView->Gui->[selection,clipboard] with theApp...
[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/LyXView.h"
42 #include "frontends/Clipboard.h"
43 #include "frontends/Painter.h"
44 #include "frontends/Selection.h"
45 #include "frontends/nullpainter.h"
46
47 #include <sstream>
48 #include <iostream>
49 #include <limits>
50
51 using lyx::docstring;
52
53 using lyx::cap::dirtyTabularStack;
54 using lyx::cap::tabularStackDirty;
55
56 using lyx::graphics::PreviewLoader;
57
58 using lyx::support::ltrim;
59
60 using lyx::frontend::Painter;
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                         tabular.setWidthOfCell(cell, dim.wid);
278                         if (p_width.zero()) {
279                                 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
280                                 // FIXME there must be a way to get rid of
281                                 // the second metrics call
282                                 tabular.getCellInset(cell)->metrics(m, dim);
283                         }
284                         maxAsc  = max(maxAsc, dim.asc);
285                         maxDesc = max(maxDesc, dim.des);
286                         ++cell;
287                 }
288                 int const top_space = tabular.row_info[i].top_space_default ?
289                         default_line_space :
290                         tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
291                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
292                 int const bottom_space = tabular.row_info[i].bottom_space_default ?
293                         default_line_space :
294                         tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
295                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
296         }
297
298         dim.asc = tabular.getAscentOfRow(0);
299         dim.des = tabular.getHeightOfTabular() - dim.asc;
300         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
301         dim_ = dim;
302 }
303
304
305 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
306 {
307         setPosCache(pi, x, y);
308
309         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
310         BufferView * bv = pi.base.bv;
311
312         static lyx::frontend::NullPainter nop;
313         static PainterInfo nullpi(bv, nop);
314
315         //resetPos(bv->cursor());
316
317         x += scx_;
318         x += ADD_TO_TABULAR_WIDTH;
319
320         idx_type idx = 0;
321         first_visible_cell = LyXTabular::npos;
322         for (row_type i = 0; i < tabular.rows(); ++i) {
323                 int nx = x;
324                 int const a = tabular.getAscentOfRow(i);
325                 int const d = tabular.getDescentOfRow(i);
326                 idx = tabular.getCellNumber(i, 0);
327                 for (col_type j = 0; j < tabular.columns(); ++j) {
328                         if (tabular.isPartOfMultiColumn(i, j))
329                                 continue;
330                         if (first_visible_cell == LyXTabular::npos)
331                                 first_visible_cell = idx;
332
333                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
334                         if (nx + tabular.getWidthOfColumn(idx) < 0
335                             || nx > bv->workWidth()
336                             || y + d < 0
337                             || y - a > bv->workHeight()) {
338                                 cell(idx)->draw(nullpi, cx, y);
339                                 drawCellLines(nop, nx, y, i, idx, pi.erased_);
340                         } else {
341                                 cell(idx)->draw(pi, cx, y);
342                                 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
343                         }
344                         nx += tabular.getWidthOfColumn(idx);
345                         ++idx;
346                 }
347
348                 if (i + 1 < tabular.rows())
349                         y += d + tabular.getAscentOfRow(i + 1) +
350                                 tabular.getAdditionalHeight(i + 1);
351         }
352 }
353
354
355 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
356 {
357         setPosCache(pi, x, y);
358
359         LCursor & cur = pi.base.bv->cursor();
360
361         x += scx_ + ADD_TO_TABULAR_WIDTH;
362
363         // Paint background of current tabular
364         int const w = tabular.getWidthOfTabular();
365         int const h = tabular.getHeightOfTabular();
366         int yy = y - tabular.getAscentOfRow(0);
367         pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
368
369         if (!cur.selection())
370                 return;
371         if (!ptr_cmp(&cur.inset(), this))
372                 return;
373
374         //resetPos(cur);
375
376
377         if (tablemode(cur)) {
378                 row_type rs, re;
379                 col_type cs, ce;
380                 getSelection(cur, rs, re, cs, ce);
381                 y -= tabular.getAscentOfRow(0);
382                 for (row_type j = 0; j < tabular.rows(); ++j) {
383                         int const a = tabular.getAscentOfRow(j);
384                         int const h = a + tabular.getDescentOfRow(j);
385                         int xx = x;
386                         y += tabular.getAdditionalHeight(j);
387                         for (col_type i = 0; i < tabular.columns(); ++i) {
388                                 if (tabular.isPartOfMultiColumn(j, i))
389                                         continue;
390                                 idx_type const cell =
391                                         tabular.getCellNumber(j, i);
392                                 int const w = tabular.getWidthOfColumn(cell);
393                                 if (i >= cs && i <= ce && j >= rs && j <= re)
394                                         pi.pain.fillRectangle(xx, y, w, h,
395                                                               LColor::selection);
396                                 xx += w;
397
398                         }
399                         y += h;
400                 }
401
402         } else {
403                 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
404         }
405 }
406
407
408 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
409                                  row_type row, idx_type cell, bool erased) const
410 {
411         int x2 = x + tabular.getWidthOfColumn(cell);
412         bool on_off = false;
413         LColor::color col = LColor::tabularline;
414         LColor::color onoffcol = LColor::tabularonoffline;
415
416         if (erased) {
417                 col = LColor::strikeout;
418                 onoffcol = LColor::strikeout;
419         }
420
421         if (!tabular.topAlreadyDrawn(cell)) {
422                 on_off = !tabular.topLine(cell);
423                 pain.line(x, y - tabular.getAscentOfRow(row),
424                           x2, y -  tabular.getAscentOfRow(row),
425                           on_off ? onoffcol : col,
426                           on_off ? Painter::line_onoffdash : Painter::line_solid);
427         }
428         on_off = !tabular.bottomLine(cell);
429         pain.line(x, y + tabular.getDescentOfRow(row),
430                   x2, y + tabular.getDescentOfRow(row),
431                   on_off ? onoffcol : col,
432                   on_off ? Painter::line_onoffdash : Painter::line_solid);
433         if (!tabular.leftAlreadyDrawn(cell)) {
434                 on_off = !tabular.leftLine(cell);
435                 pain.line(x, y -  tabular.getAscentOfRow(row),
436                           x, y +  tabular.getDescentOfRow(row),
437                           on_off ? onoffcol : col,
438                           on_off ? Painter::line_onoffdash : Painter::line_solid);
439         }
440         on_off = !tabular.rightLine(cell);
441         pain.line(x2 - tabular.getAdditionalWidth(cell),
442                   y -  tabular.getAscentOfRow(row),
443                   x2 - tabular.getAdditionalWidth(cell),
444                   y +  tabular.getDescentOfRow(row),
445                   on_off ? onoffcol : col,
446                   on_off ? Painter::line_onoffdash : Painter::line_solid);
447 }
448
449
450 docstring const InsetTabular::editMessage() const
451 {
452         return _("Opened table");
453 }
454
455
456 void InsetTabular::edit(LCursor & cur, bool left)
457 {
458         //lyxerr << "InsetTabular::edit: " << this << endl;
459         finishUndo();
460         cur.selection() = false;
461         cur.push(*this);
462         if (left) {
463                 if (isRightToLeft(cur))
464                         cur.idx() = tabular.getLastCellInRow(0);
465                 else
466                         cur.idx() = 0;
467                 cur.pit() = 0;
468                 cur.pos() = 0;
469         } else {
470                 if (isRightToLeft(cur))
471                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
472                 else
473                         cur.idx() = tabular.getNumberOfCells() - 1;
474                 cur.pit() = 0;
475                 cur.pos() = cur.lastpos(); // FIXME crude guess
476         }
477         // this accesses the position cache before it is initialized
478         //resetPos(cur);
479         //cur.bv().fitCursor();
480 }
481
482
483 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
484 {
485         lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
486                              << "\n  cur:" << cur << endl;
487         CursorSlice sl = cur.top();
488         LCursor & bvcur = cur.bv().cursor();
489
490         switch (cmd.action) {
491
492         case LFUN_MOUSE_PRESS:
493                 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
494
495                 if (cmd.button() == mouse_button::button1
496                     || cmd.button() == mouse_button::button3) {
497                         cur.selection() = false;
498                         setCursorFromCoordinates(cur, cmd.x, cmd.y);
499                         cur.resetAnchor();
500                         bvcur = cur;
501                         break;
502                 }
503
504                 if (cmd.button() == mouse_button::button2) {
505                         cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
506                         doDispatch(cur, cmd);
507                 }
508                 break;
509
510         case LFUN_MOUSE_MOTION:
511                 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
512                 if (cmd.button() == mouse_button::button1) {
513                         // only accept motions to places not deeper nested than the real anchor
514                         if (bvcur.anchor_.hasPart(cur)) {
515                                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
516                                 bvcur.setCursor(cur);
517                                 bvcur.selection() = true;
518                         } else
519                                 cur.undispatched();
520                 }
521                 break;
522
523         case LFUN_MOUSE_RELEASE:
524                 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
525                 if (cmd.button() == mouse_button::button3)
526                         InsetTabularMailer(*this).showDialog(&cur.bv());
527                 break;
528
529         case LFUN_CELL_BACKWARD:
530                 movePrevCell(cur);
531                 cur.selection() = false;
532                 break;
533
534         case LFUN_CELL_FORWARD:
535                 moveNextCell(cur);
536                 cur.selection() = false;
537                 break;
538
539         case LFUN_CHAR_FORWARD_SELECT:
540         case LFUN_CHAR_FORWARD:
541                 cell(cur.idx())->dispatch(cur, cmd);
542                 if (!cur.result().dispatched()) {
543                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
544                         if (sl == cur.top())
545                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
546                         else
547                                 cur.dispatched();
548                 }
549                 break;
550
551         case LFUN_CHAR_BACKWARD_SELECT:
552         case LFUN_CHAR_BACKWARD:
553                 cell(cur.idx())->dispatch(cur, cmd);
554                 if (!cur.result().dispatched()) {
555                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
556                         if (sl == cur.top())
557                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
558                         else
559                                 cur.dispatched();
560                 }
561                 break;
562
563         case LFUN_DOWN_SELECT:
564         case LFUN_DOWN:
565                 cell(cur.idx())->dispatch(cur, cmd);
566                 cur.dispatched(); // override the cell's decision
567                 if (sl == cur.top())
568                         // if our LyXText didn't do anything to the cursor
569                         // then we try to put the cursor into the cell below
570                         // setting also the right targetX.
571                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
572                                 cur.idx() = tabular.getCellBelow(cur.idx());
573                                 cur.pit() = 0;
574                                 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
575                                         cur.pit(), 0, cur.targetX());
576                         }
577                 if (sl == cur.top()) {
578                         // we trick it to go to the RIGHT after leaving the
579                         // tabular.
580                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
581                         cur.undispatched();
582                 }
583                 break;
584
585         case LFUN_UP_SELECT:
586         case LFUN_UP:
587                 cell(cur.idx())->dispatch(cur, cmd);
588                 cur.dispatched(); // override the cell's decision
589                 if (sl == cur.top())
590                         // if our LyXText didn't do anything to the cursor
591                         // then we try to put the cursor into the cell above
592                         // setting also the right targetX.
593                         if (tabular.row_of_cell(cur.idx()) != 0) {
594                                 cur.idx() = tabular.getCellAbove(cur.idx());
595                                 cur.pit() = cur.lastpit();
596                                 LyXText const * text = cell(cur.idx())->getText(0);
597                                 cur.pos() = text->x2pos(
598                                         cur.pit(),
599                                         text->paragraphs().back().rows().size()-1,
600                                         cur.targetX());
601                         }
602                 if (sl == cur.top()) {
603                         cmd = FuncRequest(LFUN_FINISHED_UP);
604                         cur.undispatched();
605                 }
606                 break;
607
608 //      case LFUN_SCREEN_DOWN: {
609 //              //if (hasSelection())
610 //              //      cur.selection() = false;
611 //              col_type const col = tabular.column_of_cell(cur.idx());
612 //              int const t =   cur.bv().top_y() + cur.bv().height();
613 //              if (t < yo() + tabular.getHeightOfTabular()) {
614 //                      cur.bv().scrollDocView(t);
615 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
616 //              } else {
617 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
618 //              }
619 //              cur.par() = 0;
620 //              cur.pos() = 0;
621 //              break;
622 //      }
623 //
624 //      case LFUN_SCREEN_UP: {
625 //              //if (hasSelection())
626 //              //      cur.selection() = false;
627 //              col_type const col = tabular.column_of_cell(cur.idx());
628 //              int const t =   cur.bv().top_y() + cur.bv().height();
629 //              if (yo() < 0) {
630 //                      cur.bv().scrollDocView(t);
631 //                      if (yo() > 0)
632 //                              cur.idx() = col;
633 //                      else
634 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
635 //              } else {
636 //                      cur.idx() = col;
637 //              }
638 //              cur.par() = cur.lastpar();
639 //              cur.pos() = cur.lastpos();
640 //              break;
641 //      }
642
643         case LFUN_LAYOUT_TABULAR:
644                 InsetTabularMailer(*this).showDialog(&cur.bv());
645                 break;
646
647         case LFUN_INSET_DIALOG_UPDATE:
648                 InsetTabularMailer(*this).updateDialog(&cur.bv());
649                 break;
650
651         case LFUN_TABULAR_FEATURE:
652                 if (!tabularFeatures(cur, lyx::to_utf8(cmd.argument())))
653                         cur.undispatched();
654                 break;
655
656         // insert file functions
657         case LFUN_FILE_INSERT_ASCII_PARA:
658         case LFUN_FILE_INSERT_ASCII: {
659                 // FIXME: We don't know the encoding of filenames
660                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), lyx::to_utf8(cmd.argument()), false);
661                 // FIXME: We don't know the encoding of the file
662                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), lyx::from_utf8(tmpstr), false))
663                         cur.undispatched();
664                 break;
665         }
666
667         case LFUN_CUT:
668                 if (tablemode(cur)) {
669                         if (copySelection(cur)) {
670                                 recordUndoInset(cur, Undo::DELETE);
671                                 cutSelection(cur);
672                         }
673                 }
674                 else
675                         cell(cur.idx())->dispatch(cur, cmd);
676                 break;
677
678         case LFUN_CHAR_DELETE_BACKWARD:
679         case LFUN_CHAR_DELETE_FORWARD:
680                 if (tablemode(cur)) {
681                         recordUndoInset(cur, Undo::DELETE);
682                         cutSelection(cur);
683                 }
684                 else
685                         cell(cur.idx())->dispatch(cur, cmd);
686                 break;
687
688         case LFUN_COPY:
689                 if (!cur.selection())
690                         break;
691                 if (tablemode(cur)) {
692                         finishUndo();
693                         copySelection(cur);
694                 } else
695                         cell(cur.idx())->dispatch(cur, cmd);
696                 break;
697
698         case LFUN_CLIPBOARD_PASTE:
699         case LFUN_PRIMARY_SELECTION_PASTE: {
700                 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
701                         theApp->clipboard().get() :
702                         theApp->selection().get();
703                 if (clip.empty())
704                         break;
705                 // pass to InsertAsciiString, but
706                 // only if we have multi-cell content
707                 if (clip.find_first_of(lyx::from_ascii("\t\n")) != docstring::npos) {
708                         if (insertAsciiString(cur.bv(), clip, false)) {
709                                 // content has been replaced,
710                                 // so cursor might be invalid
711                                 cur.pos() = cur.lastpos();
712                                 bvcur.setCursor(cur);
713                                 break;
714                         }
715                 } else {
716                         // so that the clipboard is used and it goes on
717                         // to default
718                         // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
719                         paste_tabular.reset();
720                         dirtyTabularStack(false);
721                 }
722                 // fall through
723         }
724
725         case LFUN_PASTE:
726                 if (hasPasteBuffer() && tabularStackDirty()) {
727                         recordUndoInset(cur, Undo::INSERT);
728                         pasteSelection(cur);
729                         break;
730                 }
731                 cell(cur.idx())->dispatch(cur, cmd);
732                 break;
733
734         case LFUN_FONT_EMPH:
735         case LFUN_FONT_BOLD:
736         case LFUN_FONT_ROMAN:
737         case LFUN_FONT_NOUN:
738         case LFUN_FONT_ITAL:
739         case LFUN_FONT_FRAK:
740         case LFUN_FONT_CODE:
741         case LFUN_FONT_SANS:
742         case LFUN_FONT_FREE_APPLY:
743         case LFUN_FONT_FREE_UPDATE:
744         case LFUN_FONT_SIZE:
745         case LFUN_FONT_UNDERLINE:
746         case LFUN_LANGUAGE:
747         case LFUN_WORD_CAPITALIZE:
748         case LFUN_WORD_UPCASE:
749         case LFUN_WORD_LOWCASE:
750         case LFUN_CHARS_TRANSPOSE:
751                 if (tablemode(cur)) {
752                         row_type rs, re;
753                         col_type cs, ce;
754                         getSelection(cur, rs, re, cs, ce);
755                         LCursor tmpcur = cur;
756                         for (row_type i = rs; i <= re; ++i) {
757                                 for (col_type j = cs; j <= ce; ++j) {
758                                         // cursor follows cell:
759                                         tmpcur.idx() = tabular.getCellNumber(i, j);
760                                         // select this cell only:
761                                         tmpcur.pit() = 0;
762                                         tmpcur.pos() = 0;
763                                         tmpcur.resetAnchor();
764                                         tmpcur.pit() = tmpcur.lastpit();
765                                         tmpcur.pos() = tmpcur.top().lastpos();
766                                         tmpcur.setCursor(tmpcur);
767                                         tmpcur.setSelection();
768                                         cell(tmpcur.idx())->dispatch(tmpcur, cmd);
769                                 }
770                         }
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 == lyx::to_utf8(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(lyx::to_utf8(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::docbook(Buffer const & buf, ostream & os,
1068                           OutputParams const & runparams) const
1069 {
1070         int ret = 0;
1071         InsetBase * master = 0;
1072
1073 #ifdef WITH_WARNINGS
1074 #warning Why not pass a proper DocIterator here?
1075 #endif
1076 #if 0
1077         // if the table is inside a float it doesn't need the informaltable
1078         // wrapper. Search for it.
1079         for (master = owner(); master; master = master->owner())
1080                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1081                         break;
1082 #endif
1083
1084         if (!master) {
1085                 os << "<informaltable>";
1086                 ++ret;
1087         }
1088         ret += tabular.docbook(buf, os, runparams);
1089         if (!master) {
1090                 os << "</informaltable>";
1091                 ++ret;
1092         }
1093         return ret;
1094 }
1095
1096
1097 void InsetTabular::validate(LaTeXFeatures & features) const
1098 {
1099         tabular.validate(features);
1100 }
1101
1102
1103 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1104 {
1105         return tabular.getCellInset(idx);
1106 }
1107
1108
1109 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1110 {
1111         return tabular.getCellInset(idx);
1112 }
1113
1114
1115 void InsetTabular::cursorPos
1116         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1117 {
1118         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1119
1120         // y offset     correction
1121         int const row = tabular.row_of_cell(sl.idx());
1122         for (int i = 0; i <= row; ++i) {
1123                 if (i != 0) {
1124                         y += tabular.getAscentOfRow(i);
1125                         y += tabular.getAdditionalHeight(i);
1126                 }
1127                 if (i != row)
1128                         y += tabular.getDescentOfRow(i);
1129         }
1130
1131         // x offset correction
1132         int const col = tabular.column_of_cell(sl.idx());
1133         int idx = tabular.getCellNumber(row, 0);
1134         for (int j = 0; j < col; ++j) {
1135                 if (tabular.isPartOfMultiColumn(row, j))
1136                         continue;
1137                 x += tabular.getWidthOfColumn(idx);
1138                 ++idx;
1139         }
1140         x += tabular.getBeginningOfTextInCell(idx);
1141         x += ADD_TO_TABULAR_WIDTH;
1142         x += scx_;
1143 }
1144
1145
1146 int InsetTabular::dist(idx_type const cell, int x, int y) const
1147 {
1148         int xx = 0;
1149         int yy = 0;
1150         InsetBase const & inset = *tabular.getCellInset(cell);
1151         Point o = theCoords.getInsets().xy(&inset);
1152         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1153         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1154         row_type const row = tabular.row_of_cell(cell);
1155         int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1156                          tabular.getAdditionalHeight(row);
1157         int const yend = o.y_ + tabular.getDescentOfRow(row);
1158
1159         if (x < xbeg)
1160                 xx = xbeg - x;
1161         else if (x > xend)
1162                 xx = x - xend;
1163
1164         if (y < ybeg)
1165                 yy = ybeg - y;
1166         else if (y > yend)
1167                 yy = y - yend;
1168
1169         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1170         //       << " ybeg=" << ybeg << " yend=" << yend
1171         //       << " xx=" << xx << " yy=" << yy
1172         //       << " dist=" << xx + yy << endl;
1173         return xx + yy;
1174 }
1175
1176
1177 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1178 {
1179         //lyxerr << "InsetTabular::editXY: " << this << endl;
1180         cur.selection() = false;
1181         cur.push(*this);
1182         cur.idx() = getNearestCell(x, y);
1183         resetPos(cur);
1184         return cell(cur.idx())->text_.editXY(cur, x, y);
1185 }
1186
1187
1188 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1189 {
1190         cur.idx() = getNearestCell(x, y);
1191         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1192 }
1193
1194
1195 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1196 {
1197         idx_type idx_min = 0;
1198         int dist_min = std::numeric_limits<int>::max();
1199         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1200                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1201                         int const d = dist(i, x, y);
1202                         if (d < dist_min) {
1203                                 dist_min = d;
1204                                 idx_min = i;
1205                         }
1206                 }
1207         }
1208         return idx_min;
1209 }
1210
1211
1212 int InsetTabular::getCellXPos(idx_type const cell) const
1213 {
1214         idx_type c = cell;
1215
1216         for (; !tabular.isFirstCellInRow(c); --c)
1217                 ;
1218         int lx = tabular.getWidthOfColumn(cell);
1219         for (; c < cell; ++c)
1220                 lx += tabular.getWidthOfColumn(c);
1221
1222         return lx - tabular.getWidthOfColumn(cell);
1223 }
1224
1225
1226 void InsetTabular::resetPos(LCursor & cur) const
1227 {
1228         BufferView & bv = cur.bv();
1229         int const maxwidth = bv.workWidth();
1230
1231         if (&cur.inset() != this) {
1232                 scx_ = 0;
1233         } else {
1234                 int const X1 = 0;
1235                 int const X2 = maxwidth;
1236                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1237                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1238                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1239
1240                 if (x1 < X1)
1241                         scx_ = X1 + 20 - x1;
1242                 else if (x2 > X2)
1243                         scx_ = X2 - 20 - x2;
1244                 else
1245                         scx_ = 0;
1246         }
1247
1248         cur.needsUpdate();
1249
1250         InsetTabularMailer(*this).updateDialog(&bv);
1251 }
1252
1253
1254 void InsetTabular::moveNextCell(LCursor & cur)
1255 {
1256         if (isRightToLeft(cur)) {
1257                 if (tabular.isFirstCellInRow(cur.idx())) {
1258                         row_type const row = tabular.row_of_cell(cur.idx());
1259                         if (row == tabular.rows() - 1)
1260                                 return;
1261                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1262                 } else {
1263                         if (cur.idx() == 0)
1264                                 return;
1265                         --cur.idx();
1266                 }
1267         } else {
1268                 if (tabular.isLastCell(cur.idx()))
1269                         return;
1270                 ++cur.idx();
1271         }
1272         cur.pit() = 0;
1273         cur.pos() = 0;
1274         resetPos(cur);
1275 }
1276
1277
1278 void InsetTabular::movePrevCell(LCursor & cur)
1279 {
1280         if (isRightToLeft(cur)) {
1281                 if (tabular.isLastCellInRow(cur.idx())) {
1282                         row_type const row = tabular.row_of_cell(cur.idx());
1283                         if (row == 0)
1284                                 return;
1285                         cur.idx() = tabular.getFirstCellInRow(row);
1286                         cur.idx() = tabular.getCellAbove(cur.idx());
1287                 } else {
1288                         if (tabular.isLastCell(cur.idx()))
1289                                 return;
1290                         ++cur.idx();
1291                 }
1292         } else {
1293                 if (cur.idx() == 0) // first cell
1294                         return;
1295                 --cur.idx();
1296         }
1297         cur.pit() = cur.lastpit();
1298         cur.pos() = cur.lastpos();
1299         resetPos(cur);
1300 }
1301
1302
1303 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1304 {
1305         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1306
1307         int i = 0;
1308         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1309                 string const tmp = tabularFeature[i].feature;
1310
1311                 if (tmp == what.substr(0, tmp.length())) {
1312                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1313                         //tabularFeatures[i].feature.length())) {
1314                         action = tabularFeature[i].action;
1315                         break;
1316                 }
1317         }
1318         if (action == LyXTabular::LAST_ACTION)
1319                 return false;
1320
1321         string const val =
1322                 ltrim(what.substr(tabularFeature[i].feature.length()));
1323         tabularFeatures(cur, action, val);
1324         return true;
1325 }
1326
1327
1328 namespace {
1329
1330 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1331                           string const & special, bool & flag)
1332 {
1333         if (special == "dl_above") {
1334                 ltt.topDL = flag;
1335                 ltt.set = false;
1336         } else if (special == "dl_below") {
1337                 ltt.bottomDL = flag;
1338                 ltt.set = false;
1339         } else if (special == "empty") {
1340                 ltt.empty = flag;
1341                 ltt.set = false;
1342         } else if (flag) {
1343                 ltt.empty = false;
1344                 ltt.set = true;
1345         }
1346 }
1347
1348 } // anon namespace
1349
1350
1351 void InsetTabular::tabularFeatures(LCursor & cur,
1352         LyXTabular::Feature feature, string const & value)
1353 {
1354         BufferView & bv = cur.bv();
1355         col_type sel_col_start;
1356         col_type sel_col_end;
1357         row_type sel_row_start;
1358         row_type sel_row_end;
1359         bool setLines = false;
1360         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1361         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1362
1363         switch (feature) {
1364
1365         case LyXTabular::M_ALIGN_LEFT:
1366         case LyXTabular::ALIGN_LEFT:
1367                 setAlign = LYX_ALIGN_LEFT;
1368                 break;
1369
1370         case LyXTabular::M_ALIGN_RIGHT:
1371         case LyXTabular::ALIGN_RIGHT:
1372                 setAlign = LYX_ALIGN_RIGHT;
1373                 break;
1374
1375         case LyXTabular::M_ALIGN_CENTER:
1376         case LyXTabular::ALIGN_CENTER:
1377                 setAlign = LYX_ALIGN_CENTER;
1378                 break;
1379
1380         case LyXTabular::ALIGN_BLOCK:
1381                 setAlign = LYX_ALIGN_BLOCK;
1382                 break;
1383
1384         case LyXTabular::M_VALIGN_TOP:
1385         case LyXTabular::VALIGN_TOP:
1386                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1387                 break;
1388
1389         case LyXTabular::M_VALIGN_BOTTOM:
1390         case LyXTabular::VALIGN_BOTTOM:
1391                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1392                 break;
1393
1394         case LyXTabular::M_VALIGN_MIDDLE:
1395         case LyXTabular::VALIGN_MIDDLE:
1396                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1397                 break;
1398
1399         default:
1400                 break;
1401         }
1402
1403         recordUndoInset(cur, Undo::ATOMIC);
1404
1405         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1406         row_type const row = tabular.row_of_cell(cur.idx());
1407         col_type const column = tabular.column_of_cell(cur.idx());
1408         bool flag = true;
1409         LyXTabular::ltType ltt;
1410
1411         switch (feature) {
1412
1413         case LyXTabular::SET_PWIDTH: {
1414                 LyXLength const len(value);
1415                 tabular.setColumnPWidth(cur, cur.idx(), len);
1416                 if (len.zero()
1417                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1418                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1419                 break;
1420         }
1421
1422         case LyXTabular::SET_MPWIDTH:
1423                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1424                 break;
1425
1426         case LyXTabular::SET_SPECIAL_COLUMN:
1427         case LyXTabular::SET_SPECIAL_MULTI:
1428                 tabular.setAlignSpecial(cur.idx(),value,feature);
1429                 break;
1430
1431         case LyXTabular::APPEND_ROW:
1432                 // append the row into the tabular
1433                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1434                 break;
1435
1436         case LyXTabular::APPEND_COLUMN:
1437                 // append the column into the tabular
1438                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1439                 cur.idx() = tabular.getCellNumber(row, column);
1440                 break;
1441
1442         case LyXTabular::DELETE_ROW:
1443                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1444                         tabular.deleteRow(sel_row_start);
1445                 if (sel_row_start >= tabular.rows())
1446                         --sel_row_start;
1447                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1448                 cur.pit() = 0;
1449                 cur.pos() = 0;
1450                 cur.selection() = false;
1451                 break;
1452
1453         case LyXTabular::DELETE_COLUMN:
1454                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1455                         tabular.deleteColumn(sel_col_start);
1456                 if (sel_col_start >= tabular.columns())
1457                         --sel_col_start;
1458                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1459                 cur.pit() = 0;
1460                 cur.pos() = 0;
1461                 cur.selection() = false;
1462                 break;
1463
1464         case LyXTabular::M_TOGGLE_LINE_TOP:
1465                 flag = false;
1466         case LyXTabular::TOGGLE_LINE_TOP: {
1467                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1468                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1469                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1470                                 tabular.setTopLine(
1471                                         tabular.getCellNumber(i, j),
1472                                         lineSet, flag);
1473                 break;
1474         }
1475
1476         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1477                 flag = false;
1478         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1479                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1480                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482                                 tabular.setBottomLine(
1483                                         tabular.getCellNumber(i, j),
1484                                         lineSet,
1485                                         flag);
1486                 break;
1487         }
1488
1489         case LyXTabular::M_TOGGLE_LINE_LEFT:
1490                 flag = false;
1491         case LyXTabular::TOGGLE_LINE_LEFT: {
1492                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1493                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495                                 tabular.setLeftLine(
1496                                         tabular.getCellNumber(i,j),
1497                                         lineSet,
1498                                         flag);
1499                 break;
1500         }
1501
1502         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1503                 flag = false;
1504         case LyXTabular::TOGGLE_LINE_RIGHT: {
1505                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1506                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508                                 tabular.setRightLine(
1509                                         tabular.getCellNumber(i,j),
1510                                         lineSet,
1511                                         flag);
1512                 break;
1513         }
1514
1515         case LyXTabular::M_ALIGN_LEFT:
1516         case LyXTabular::M_ALIGN_RIGHT:
1517         case LyXTabular::M_ALIGN_CENTER:
1518                 flag = false;
1519         case LyXTabular::ALIGN_LEFT:
1520         case LyXTabular::ALIGN_RIGHT:
1521         case LyXTabular::ALIGN_CENTER:
1522         case LyXTabular::ALIGN_BLOCK:
1523                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1524                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1525                                 tabular.setAlignment(
1526                                         tabular.getCellNumber(i, j),
1527                                         setAlign,
1528                                         flag);
1529                 break;
1530
1531         case LyXTabular::M_VALIGN_TOP:
1532         case LyXTabular::M_VALIGN_BOTTOM:
1533         case LyXTabular::M_VALIGN_MIDDLE:
1534                 flag = false;
1535         case LyXTabular::VALIGN_TOP:
1536         case LyXTabular::VALIGN_BOTTOM:
1537         case LyXTabular::VALIGN_MIDDLE:
1538                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1539                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1540                                 tabular.setVAlignment(
1541                                         tabular.getCellNumber(i, j),
1542                                         setVAlign, flag);
1543                 break;
1544
1545         case LyXTabular::MULTICOLUMN: {
1546                 if (sel_row_start != sel_row_end) {
1547 #ifdef WITH_WARNINGS
1548 #warning Need I say it ? This is horrible.
1549 #endif
1550                         // FIXME UNICODE
1551                         Alert::error(_("Error setting multicolumn"),
1552                                      _("You cannot set multicolumn vertically."));
1553                         return;
1554                 }
1555                 if (!cur.selection()) {
1556                         // just multicol for one single cell
1557                         // check whether we are completely in a multicol
1558                         if (tabular.isMultiColumn(cur.idx()))
1559                                 tabular.unsetMultiColumn(cur.idx());
1560                         else
1561                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1562                         break;
1563                 }
1564                 // we have a selection so this means we just add all this
1565                 // cells to form a multicolumn cell
1566                 idx_type const s_start = cur.selBegin().idx();
1567                 idx_type const s_end = cur.selEnd().idx();
1568                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1569                 cur.idx() = s_start;
1570                 cur.pit() = 0;
1571                 cur.pos() = 0;
1572                 cur.selection() = false;
1573                 break;
1574         }
1575
1576         case LyXTabular::SET_ALL_LINES:
1577                 setLines = true;
1578         case LyXTabular::UNSET_ALL_LINES:
1579                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1580                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1581                                 tabular.setAllLines(
1582                                         tabular.getCellNumber(i,j), setLines);
1583                 break;
1584
1585         case LyXTabular::SET_LONGTABULAR:
1586                 tabular.setLongTabular(true);
1587                 break;
1588
1589         case LyXTabular::UNSET_LONGTABULAR:
1590                 tabular.setLongTabular(false);
1591                 break;
1592
1593         case LyXTabular::SET_ROTATE_TABULAR:
1594                 tabular.setRotateTabular(true);
1595                 break;
1596
1597         case LyXTabular::UNSET_ROTATE_TABULAR:
1598                 tabular.setRotateTabular(false);
1599                 break;
1600
1601         case LyXTabular::SET_ROTATE_CELL:
1602                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1603                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1604                                 tabular.setRotateCell(
1605                                         tabular.getCellNumber(i, j), true);
1606                 break;
1607
1608         case LyXTabular::UNSET_ROTATE_CELL:
1609                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1610                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1611                                 tabular.setRotateCell(
1612                                         tabular.getCellNumber(i, j), false);
1613                 break;
1614
1615         case LyXTabular::SET_USEBOX: {
1616                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1617                 if (val == tabular.getUsebox(cur.idx()))
1618                         val = LyXTabular::BOX_NONE;
1619                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1620                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1621                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1622                 break;
1623         }
1624
1625         case LyXTabular::UNSET_LTFIRSTHEAD:
1626                 flag = false;
1627         case LyXTabular::SET_LTFIRSTHEAD:
1628                 tabular.getRowOfLTFirstHead(row, ltt);
1629                 checkLongtableSpecial(ltt, value, flag);
1630                 tabular.setLTHead(row, flag, ltt, true);
1631                 break;
1632
1633         case LyXTabular::UNSET_LTHEAD:
1634                 flag = false;
1635         case LyXTabular::SET_LTHEAD:
1636                 tabular.getRowOfLTHead(row, ltt);
1637                 checkLongtableSpecial(ltt, value, flag);
1638                 tabular.setLTHead(row, flag, ltt, false);
1639                 break;
1640
1641         case LyXTabular::UNSET_LTFOOT:
1642                 flag = false;
1643         case LyXTabular::SET_LTFOOT:
1644                 tabular.getRowOfLTFoot(row, ltt);
1645                 checkLongtableSpecial(ltt, value, flag);
1646                 tabular.setLTFoot(row, flag, ltt, false);
1647                 break;
1648
1649         case LyXTabular::UNSET_LTLASTFOOT:
1650                 flag = false;
1651         case LyXTabular::SET_LTLASTFOOT:
1652                 tabular.getRowOfLTLastFoot(row, ltt);
1653                 checkLongtableSpecial(ltt, value, flag);
1654                 tabular.setLTFoot(row, flag, ltt, true);
1655                 break;
1656
1657         case LyXTabular::SET_LTNEWPAGE:
1658                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1659                 break;
1660
1661         case LyXTabular::SET_BOOKTABS:
1662                 tabular.setBookTabs(true);
1663                 break;
1664
1665         case LyXTabular::UNSET_BOOKTABS:
1666                 tabular.setBookTabs(false);
1667                 break;
1668
1669         case LyXTabular::SET_TOP_SPACE: {
1670                 LyXLength len;
1671                 if (value == "default")
1672                         for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1673                                 tabular.row_info[i].top_space_default = true;
1674                 else if (isValidLength(value, &len))
1675                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1676                                 tabular.row_info[i].top_space_default = false;
1677                                 tabular.row_info[i].top_space = len;
1678                         }
1679                 else
1680                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1681                                 tabular.row_info[i].top_space_default = false;
1682                                 tabular.row_info[i].top_space = len;
1683                         }
1684                 break;
1685         }
1686
1687         case LyXTabular::SET_BOTTOM_SPACE: {
1688                 LyXLength len;
1689                 if (value == "default")
1690                         for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1691                                 tabular.row_info[i].bottom_space_default = true;
1692                 else if (isValidLength(value, &len))
1693                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1694                                 tabular.row_info[i].bottom_space_default = false;
1695                                 tabular.row_info[i].bottom_space = len;
1696                         }
1697                 else
1698                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1699                                 tabular.row_info[i].bottom_space_default = false;
1700                                 tabular.row_info[i].bottom_space = len;
1701                         }
1702                 break;
1703         }
1704
1705         case LyXTabular::SET_INTERLINE_SPACE: {
1706                 LyXLength len;
1707                 if (value == "default")
1708                         for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1709                                 tabular.row_info[i].interline_space_default = true;
1710                 else if (isValidLength(value, &len))
1711                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1712                                 tabular.row_info[i].interline_space_default = false;
1713                                 tabular.row_info[i].interline_space = len;
1714                         }
1715                 else
1716                         for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1717                                 tabular.row_info[i].interline_space_default = false;
1718                                 tabular.row_info[i].interline_space = len;
1719                         }
1720                 break;
1721         }
1722
1723         // dummy stuff just to avoid warnings
1724         case LyXTabular::LAST_ACTION:
1725                 break;
1726         }
1727
1728         InsetTabularMailer(*this).updateDialog(&bv);
1729 }
1730
1731
1732 bool InsetTabular::showInsetDialog(BufferView * bv) const
1733 {
1734         InsetTabularMailer(*this).showDialog(bv);
1735         return true;
1736 }
1737
1738
1739 void InsetTabular::openLayoutDialog(BufferView * bv) const
1740 {
1741         InsetTabularMailer(*this).showDialog(bv);
1742 }
1743
1744
1745 bool InsetTabular::copySelection(LCursor & cur)
1746 {
1747         if (!cur.selection())
1748                 return false;
1749
1750         row_type rs, re;
1751         col_type cs, ce;
1752         getSelection(cur, rs, re, cs, ce);
1753
1754         paste_tabular.reset(new LyXTabular(tabular));
1755
1756         for (row_type i = 0; i < rs; ++i)
1757                 paste_tabular->deleteRow(0);
1758
1759         row_type const rows = re - rs + 1;
1760         while (paste_tabular->rows() > rows)
1761                 paste_tabular->deleteRow(rows);
1762
1763         paste_tabular->setTopLine(0, true, true);
1764         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1765                                      true, true);
1766
1767         for (col_type i = 0; i < cs; ++i)
1768                 paste_tabular->deleteColumn(0);
1769
1770         col_type const columns = ce - cs + 1;
1771         while (paste_tabular->columns() > columns)
1772                 paste_tabular->deleteColumn(columns);
1773
1774         paste_tabular->setLeftLine(0, true, true);
1775         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1776                                     true, true);
1777
1778         ostringstream os;
1779         OutputParams const runparams;
1780         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1781         theApp->clipboard().put(lyx::from_utf8(os.str()));
1782         // mark tabular stack dirty
1783         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1784         // when we (hopefully) have a one-for-all paste mechanism.
1785         lyx::cap::dirtyTabularStack(true);
1786
1787         return true;
1788 }
1789
1790
1791 bool InsetTabular::pasteSelection(LCursor & cur)
1792 {
1793         if (!paste_tabular)
1794                 return false;
1795         col_type const actcol = tabular.column_of_cell(cur.idx());
1796         row_type const actrow = tabular.row_of_cell(cur.idx());
1797         for (row_type r1 = 0, r2 = actrow;
1798              r1 < paste_tabular->rows() && r2 < tabular.rows();
1799              ++r1, ++r2) {
1800                 for (col_type c1 = 0, c2 = actcol;
1801                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1802                     ++c1, ++c2) {
1803                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1804                             tabular.isPartOfMultiColumn(r2, c2))
1805                                 continue;
1806                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1807                                 --c2;
1808                                 continue;
1809                         }
1810                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1811                                 --c1;
1812                                 continue;
1813                         }
1814                         shared_ptr<InsetText> inset(
1815                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1816                         tabular.setCellInset(r2, c2, inset);
1817                         inset->markNew();
1818                         cur.pos() = 0;
1819                 }
1820         }
1821         return true;
1822 }
1823
1824
1825 void InsetTabular::cutSelection(LCursor & cur)
1826 {
1827         if (!cur.selection())
1828                 return;
1829
1830         row_type rs, re;
1831         col_type cs, ce;
1832         getSelection(cur, rs, re, cs, ce);
1833         for (row_type i = rs; i <= re; ++i) {
1834                 for (col_type j = cs; j <= ce; ++j) {
1835                         shared_ptr<InsetText> t
1836                                 = cell(tabular.getCellNumber(i, j));
1837                         if (cur.buffer().params().tracking_changes)
1838                                 t->markErased(true);
1839                         else
1840                                 t->clear();
1841                 }
1842         }
1843
1844         // cursor position might be invalid now
1845         if (cur.pit() > cur.lastpit())
1846                 cur.pit() = cur.lastpit();
1847         if (cur.pos() > cur.lastpos())
1848                 cur.pos() = cur.lastpos();
1849         cur.clearSelection();
1850 }
1851
1852
1853 bool InsetTabular::isRightToLeft(LCursor & cur) const
1854 {
1855         BOOST_ASSERT(cur.depth() > 1);
1856         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1857         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1858         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1859                                          parentpos).language()->rightToLeft();
1860 }
1861
1862
1863 void InsetTabular::getSelection(LCursor & cur,
1864         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1865 {
1866         CursorSlice const & beg = cur.selBegin();
1867         CursorSlice const & end = cur.selEnd();
1868         cs = tabular.column_of_cell(beg.idx());
1869         ce = tabular.column_of_cell(end.idx());
1870         if (cs > ce) {
1871                 ce = cs;
1872                 cs = tabular.column_of_cell(end.idx());
1873         } else {
1874                 ce = tabular.right_column_of_cell(end.idx());
1875         }
1876
1877         rs = tabular.row_of_cell(beg.idx());
1878         re = tabular.row_of_cell(end.idx());
1879         if (rs > re)
1880                 swap(rs, re);
1881 }
1882
1883
1884 LyXText * InsetTabular::getText(int idx) const
1885 {
1886         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1887 }
1888
1889
1890 void InsetTabular::markErased(bool erased)
1891 {
1892         for (idx_type idx = 0; idx < nargs(); ++idx)
1893                 cell(idx)->markErased(erased);
1894 }
1895
1896
1897 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1898 {
1899         return tabular.getPWidth(cell).zero();
1900 }
1901
1902
1903 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1904                                      bool usePaste)
1905 {
1906         if (buf.length() <= 0)
1907                 return true;
1908
1909         col_type cols = 1;
1910         row_type rows = 1;
1911         col_type maxCols = 1;
1912         docstring::size_type const len = buf.length();
1913         docstring::size_type p = 0;
1914
1915         while (p < len &&
1916                (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos) {
1917                 switch (buf[p]) {
1918                 case '\t':
1919                         ++cols;
1920                         break;
1921                 case '\n':
1922                         if (p + 1 < len)
1923                                 ++rows;
1924                         maxCols = max(cols, maxCols);
1925                         cols = 1;
1926                         break;
1927                 }
1928                 ++p;
1929         }
1930         maxCols = max(cols, maxCols);
1931         LyXTabular * loctab;
1932         idx_type cell = 0;
1933         col_type ocol = 0;
1934         row_type row = 0;
1935         if (usePaste) {
1936                 paste_tabular.reset(
1937                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1938                 loctab = paste_tabular.get();
1939                 cols = 0;
1940                 dirtyTabularStack(true);
1941         } else {
1942                 loctab = &tabular;
1943                 cell = bv.cursor().idx();
1944                 ocol = tabular.column_of_cell(cell);
1945                 row = tabular.row_of_cell(cell);
1946         }
1947
1948         docstring::size_type op = 0;
1949         idx_type const cells = loctab->getNumberOfCells();
1950         p = 0;
1951         cols = ocol;
1952         rows = loctab->rows();
1953         col_type const columns = loctab->columns();
1954
1955         while (cell < cells && p < len && row < rows &&
1956                (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos)
1957         {
1958                 if (p >= len)
1959                         break;
1960                 switch (buf[p]) {
1961                 case '\t':
1962                         // we can only set this if we are not too far right
1963                         if (cols < columns) {
1964                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1965                                 inset->setViewCache(&bv);
1966                                 Paragraph & par = inset->text_.getPar(0);
1967                                 LyXFont const font = inset->text_.getFont(par, 0);
1968                                 inset->setText(buf.substr(op, p - op), font);
1969                                 ++cols;
1970                                 ++cell;
1971                         }
1972                         break;
1973                 case '\n':
1974                         // we can only set this if we are not too far right
1975                         if (cols < columns) {
1976                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1977                                 inset->setViewCache(&bv);
1978                                 Paragraph & par = inset->text_.getPar(0);
1979                                 LyXFont const font = inset->text_.getFont(par, 0);
1980                                 inset->setText(buf.substr(op, p - op), font);
1981                         }
1982                         cols = ocol;
1983                         ++row;
1984                         if (row < rows)
1985                                 cell = loctab->getCellNumber(row, cols);
1986                         break;
1987                 }
1988                 ++p;
1989                 op = p;
1990         }
1991         // check for the last cell if there is no trailing '\n'
1992         if (cell < cells && op < len) {
1993                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1994                 inset->setViewCache(&bv);
1995                 Paragraph & par = inset->text_.getPar(0);
1996                 LyXFont const font = inset->text_.getFont(par, 0);
1997                 inset->setText(buf.substr(op, len - op), font);
1998         }
1999         return true;
2000 }
2001
2002
2003 void InsetTabular::addPreview(PreviewLoader & loader) const
2004 {
2005         row_type const rows = tabular.rows();
2006         col_type const columns = tabular.columns();
2007         for (row_type i = 0; i < rows; ++i) {
2008                 for (col_type j = 0; j < columns; ++j)
2009                         tabular.getCellInset(i, j)->addPreview(loader);
2010         }
2011 }
2012
2013
2014 bool InsetTabular::tablemode(LCursor & cur) const
2015 {
2016         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2017 }
2018
2019
2020
2021
2022
2023 string const InsetTabularMailer::name_("tabular");
2024
2025 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2026         : inset_(const_cast<InsetTabular &>(inset))
2027 {}
2028
2029
2030 string const InsetTabularMailer::inset2string(Buffer const &) const
2031 {
2032         return params2string(inset_);
2033 }
2034
2035
2036 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2037 {
2038         istringstream data(in);
2039         LyXLex lex(0,0);
2040         lex.setStream(data);
2041
2042         if (in.empty())
2043                 return;
2044
2045         string token;
2046         lex >> token;
2047         if (!lex || token != name_)
2048                 return print_mailer_error("InsetTabularMailer", in, 1,
2049                                           name_);
2050
2051         // This is part of the inset proper that is usually swallowed
2052         // by Buffer::readInset
2053         lex >> token;
2054         if (!lex || token != "Tabular")
2055                 return print_mailer_error("InsetTabularMailer", in, 2,
2056                                           "Tabular");
2057
2058         Buffer const & buffer = inset.buffer();
2059         inset.read(buffer, lex);
2060 }
2061
2062
2063 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2064 {
2065         ostringstream data;
2066         data << name_ << ' ';
2067         inset.write(inset.buffer(), data);
2068         data << "\\end_inset\n";
2069         return data.str();
2070 }