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