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