]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
insetcharstyle drawing cosmetics
[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.next();
208         string token = lex.getString();
209         while (lex.isOK() && (token != "\\end_inset")) {
210                 lex.next();
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                         y += tabular.getDescentOfRow(i)
276                                 + tabular.getAscentOfRow(i + 1)
277                                 + tabular.getAdditionalHeight(i + 1);
278                         continue;
279                 }
280                 for (int j = 0; j < tabular.columns(); ++j) {
281                         if (nx > bv->workWidth())
282                                 break;
283                         if (tabular.isPartOfMultiColumn(i, j))
284                                 continue;
285                         if (first_visible_cell < 0)
286                                 first_visible_cell = idx;
287                         if (bv->cursor().selection())
288                                 drawCellSelection(pi, nx, y, i, j, idx);
289
290                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
291                         cell(idx).draw(pi, cx, y);
292                         drawCellLines(pi.pain, nx, y, i, idx);
293                         nx += tabular.getWidthOfColumn(idx);
294                         ++idx;
295                 }
296
297 // Would be nice, but for some completely unfathomable reason,
298 // on a col resize to a new fixed width, even though the insettexts
299 // are resized, the cell isn't, but drawing all cells in a tall table
300 // has the desired effect somehow. Complete dark magic.
301 #if 0
302                 // avoiding drawing the rest of a long table is
303                 // a pretty big speedup
304                 if (y > bv->workHeight())
305                         break;
306 #endif
307
308                 y += tabular.getDescentOfRow(i) +
309                         tabular.getAscentOfRow(i + 1) +
310                         tabular.getAdditionalHeight(i + 1);
311         }
312 }
313
314
315 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
316                                  int row, int cell) const
317 {
318         int x2 = x + tabular.getWidthOfColumn(cell);
319         bool on_off = false;
320
321         if (!tabular.topAlreadyDrawn(cell)) {
322                 on_off = !tabular.topLine(cell);
323                 pain.line(x, y - tabular.getAscentOfRow(row),
324                           x2, y -  tabular.getAscentOfRow(row),
325                           on_off ? LColor::tabularonoffline : LColor::tabularline,
326                           on_off ? Painter::line_onoffdash : Painter::line_solid);
327         }
328         on_off = !tabular.bottomLine(cell);
329         pain.line(x, y + tabular.getDescentOfRow(row),
330                   x2, y + tabular.getDescentOfRow(row),
331                   on_off ? LColor::tabularonoffline : LColor::tabularline,
332                   on_off ? Painter::line_onoffdash : Painter::line_solid);
333         if (!tabular.leftAlreadyDrawn(cell)) {
334                 on_off = !tabular.leftLine(cell);
335                 pain.line(x, y -  tabular.getAscentOfRow(row),
336                           x, y +  tabular.getDescentOfRow(row),
337                           on_off ? LColor::tabularonoffline : LColor::tabularline,
338                           on_off ? Painter::line_onoffdash : Painter::line_solid);
339         }
340         on_off = !tabular.rightLine(cell);
341         pain.line(x2 - tabular.getAdditionalWidth(cell),
342                   y -  tabular.getAscentOfRow(row),
343                   x2 - tabular.getAdditionalWidth(cell),
344                   y +  tabular.getDescentOfRow(row),
345                   on_off ? LColor::tabularonoffline : LColor::tabularline,
346                   on_off ? Painter::line_onoffdash : Painter::line_solid);
347 }
348
349
350 void InsetTabular::drawCellSelection(PainterInfo & pi, int x, int y,
351                                      int row, int column, int cell) const
352 {
353         LCursor & cur = pi.base.bv->cursor();
354         BOOST_ASSERT(cur.selection());
355         if (tablemode(cur)) {
356                 int rs, re, cs, ce;
357                 getSelection(cur, rs, re, cs, ce);
358                 if (column >= cs && column <= ce && row >= rs && row <= re) {
359                         int w = tabular.getWidthOfColumn(cell);
360                         int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
361                         pi.pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
362                                                  w, h, LColor::selection);
363                 }
364         }
365 }
366
367
368 string const InsetTabular::editMessage() const
369 {
370         return _("Opened table");
371 }
372
373
374 void InsetTabular::edit(LCursor & cur, bool left)
375 {
376         lyxerr << "InsetTabular::edit: " << this << endl;
377         finishUndo();
378         cur.selection() = false;
379         cur.push(*this);
380         if (left) {
381                 if (isRightToLeft(cur))
382                         cur.idx() = tabular.getLastCellInRow(0);
383                 else
384                         cur.idx() = 0;
385                 cur.par() = 0;
386                 cur.pos() = 0;
387         } else {
388                 if (isRightToLeft(cur))
389                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
390                 else
391                         cur.idx() = tabular.getNumberOfCells() - 1;
392                 cur.par() = 0;
393                 cur.pos() = cur.lastpos(); // FIXME crude guess
394         }
395         // this accesses the position cache before it is initialized
396         //resetPos(cur);
397         //cur.bv().fitCursor();
398 }
399
400
401 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y) const
402 {
403         //lyxerr << "InsetTabular::editXY: " << this << endl;
404         cur.selection() = false;
405         cur.push(const_cast<InsetTabular&>(*this));
406         return setPos(cur, x, y);
407         //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
408 }
409
410
411 void InsetTabular::priv_dispatch(LCursor & cur, FuncRequest & cmd)
412 {
413         lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
414         //lyxerr << "  cur:\n" << cur << endl;
415         CursorSlice sl = cur.top();
416         LCursor & bvcur = cur.bv().cursor();
417
418         switch (cmd.action) {
419
420         case LFUN_MOUSE_PRESS:
421                 lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
422
423                 if (cmd.button() == mouse_button::button1) {
424                         cur.selection() = false;
425                         setPos(cur, cmd.x, cmd.y);
426                         cur.resetAnchor();
427                         bvcur = cur;
428                         break;
429                 }
430
431                 //if (cmd.button() == mouse_button::button2)
432                 //      dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
433
434                 // we'll pop up the table dialog on release
435                 if (cmd.button() == mouse_button::button3)
436                         break;
437                 break;
438
439         case LFUN_MOUSE_MOTION:
440                 lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
441                 if (cmd.button() == mouse_button::button1) {
442                         // only accept motions to places not deeper nested than the real anchor
443                         if (bvcur.anchor_.hasPart(cur)) {
444                                 setPos(cur, cmd.x, cmd.y);
445                                 bvcur.setCursor(cur);
446                                 bvcur.selection() = true;
447                         }
448                 }
449                 break;
450
451         case LFUN_MOUSE_RELEASE:
452                 lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
453                 if (cmd.button() == mouse_button::button3)
454                         InsetTabularMailer(*this).showDialog(&cur.bv());
455                 break;
456
457         case LFUN_CELL_BACKWARD:
458                 movePrevCell(cur);
459                 cur.selection() = false;
460                 break;
461
462         case LFUN_CELL_FORWARD:
463                 moveNextCell(cur);
464                 cur.selection() = false;
465                 break;
466
467         case LFUN_SCROLL_INSET:
468                 if (cmd.argument.empty())
469                         break;
470                 if (cmd.argument.find('.') != cmd.argument.npos)
471                         scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
472                 else
473                         scroll(cur.bv(), strToInt(cmd.argument));
474                 break;
475
476         case LFUN_RIGHTSEL:
477         case LFUN_RIGHT:
478                 cell(cur.idx()).dispatch(cur, cmd);
479                 cur.dispatched(); // override the cell's decision
480                 if (sl == cur.top())
481                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
482                 if (sl == cur.top()) {
483                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
484                         cur.undispatched();
485                 }
486                 break;
487
488         case LFUN_LEFTSEL:
489         case LFUN_LEFT:
490                 cell(cur.idx()).dispatch(cur, cmd);
491                 cur.dispatched(); // override the cell's decision
492                 if (sl == cur.top())
493                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
494                 if (sl == cur.top()) {
495                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
496                         cur.undispatched();
497                 }
498                 break;
499
500         case LFUN_DOWNSEL:
501         case LFUN_DOWN:
502                 cell(cur.idx()).dispatch(cur, cmd);
503                 cur.dispatched(); // override the cell's decision
504                 if (sl == cur.top())
505                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
506                                 cur.idx() = tabular.getCellBelow(cur.idx());
507                                 cur.par() = 0;
508                                 cur.pos() = 0;
509                                 resetPos(cur);
510                         }
511                 if (sl == cur.top()) {
512                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
513                         cur.undispatched();
514                 }
515                 break;
516
517         case LFUN_UPSEL:
518         case LFUN_UP:
519                 cell(cur.idx()).dispatch(cur, cmd);
520                 cur.dispatched(); // override the cell's decision
521                 if (sl == cur.top())
522                         if (tabular.row_of_cell(cur.idx()) != 0) {
523                                 cur.idx() = tabular.getCellAbove(cur.idx());
524                                 cur.par() = cur.lastpar();
525                                 cur.pos() = cur.lastpos();
526                                 resetPos(cur);
527                         }
528                 if (sl == cur.top()) {
529                         cmd = FuncRequest(LFUN_FINISHED_UP);
530                         cur.undispatched();
531                 }
532                 break;
533
534         case LFUN_NEXT: {
535                 //if (hasSelection())
536                 //      cur.selection() = false;
537                 int const col = tabular.column_of_cell(cur.idx());
538                 int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
539                 if (t < yo() + tabular.getHeightOfTabular()) {
540                         cur.bv().scrollDocView(t);
541                         cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
542                 } else {
543                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
544                 }
545                 cur.par() = 0;
546                 cur.pos() = 0;
547                 resetPos(cur);
548                 break;
549         }
550
551         case LFUN_PRIOR: {
552                 //if (hasSelection())
553                 //      cur.selection() = false;
554                 int const col = tabular.column_of_cell(cur.idx());
555                 int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
556                 if (yo() < 0) {
557                         cur.bv().scrollDocView(t);
558                         if (yo() > 0)
559                                 cur.idx() = col;
560                         else
561                                 cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
562                 } else {
563                         cur.idx() = col;
564                 }
565                 cur.par() = cur.lastpar();
566                 cur.pos() = cur.lastpos();
567                 resetPos(cur);
568                 break;
569         }
570
571         case LFUN_LAYOUT_TABULAR:
572                 InsetTabularMailer(*this).showDialog(&cur.bv());
573                 break;
574
575         case LFUN_INSET_DIALOG_UPDATE:
576                 InsetTabularMailer(*this).updateDialog(&cur.bv());
577                 break;
578
579         case LFUN_TABULAR_FEATURE:
580                 if (!tabularFeatures(cur, cmd.argument))
581                         cur.undispatched();
582                 break;
583
584         // insert file functions
585         case LFUN_FILE_INSERT_ASCII_PARA:
586         case LFUN_FILE_INSERT_ASCII: {
587                 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
588                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
589                         cur.undispatched();
590                 break;
591         }
592
593         case LFUN_CUT:
594                 if (copySelection(cur)) {
595                         recordUndo(cur, Undo::DELETE);
596                         cutSelection(cur);
597                 }
598                 break;
599
600         case LFUN_BACKSPACE:
601         case LFUN_DELETE:
602                 recordUndo(cur, Undo::DELETE);
603                 if (tablemode(cur))
604                         cutSelection(cur);
605                 else
606                         cell(cur.idx()).dispatch(cur, cmd);
607                 break;
608
609         case LFUN_COPY:
610                 if (!cur.selection())
611                         break;
612                 finishUndo();
613                 copySelection(cur);
614                 break;
615
616         case LFUN_PASTESELECTION: {
617                 string const clip = cur.bv().getClipboard();
618                 if (clip.empty())
619                         break;
620                 if (clip.find('\t') != string::npos) {
621                         int cols = 1;
622                         int rows = 1;
623                         int maxCols = 1;
624                         size_t len = clip.length();
625                         for (size_t p = 0; p < len; ++p) {
626                                 p = clip.find_first_of("\t\n", p);
627                                 if (p == string::npos)
628                                         break;
629                                 switch (clip[p]) {
630                                 case '\t':
631                                         ++cols;
632                                         break;
633                                 case '\n':
634                                         if (p + 1 < len)
635                                                 ++rows;
636                                         maxCols = max(cols, maxCols);
637                                         cols = 1;
638                                         break;
639                                 }
640                         }
641                         maxCols = max(cols, maxCols);
642
643                         paste_tabular.reset(
644                                 new LyXTabular(cur.buffer().params(), rows, maxCols));
645
646                         string::size_type op = 0;
647                         int cell = 0;
648                         int cells = paste_tabular->getNumberOfCells();
649                         cols = 0;
650                         LyXFont font;
651                         for (size_t p = 0; cell < cells && p < len; ++p) {
652                                 p = clip.find_first_of("\t\n", p);
653                                 if (p == string::npos || p >= len)
654                                         break;
655                                 switch (clip[p]) {
656                                 case '\t':
657                                         paste_tabular->getCellInset(cell).
658                                                 setText(clip.substr(op, p - op), font);
659                                         ++cols;
660                                         ++cell;
661                                         break;
662                                 case '\n':
663                                         paste_tabular->getCellInset(cell).
664                                                 setText(clip.substr(op, p - op), font);
665                                         while (cols++ < maxCols)
666                                                 ++cell;
667                                         cols = 0;
668                                         break;
669                                 }
670                                 op = p + 1;
671                         }
672                         // check for the last cell if there is no trailing '\n'
673                         if (cell < cells && op < len)
674                                 paste_tabular->getCellInset(cell).
675                                         setText(clip.substr(op, len - op), font);
676                 } else if (!insertAsciiString(cur.bv(), clip, true)) {
677                         // so that the clipboard is used and it goes on
678                         // to default
679                         // and executes LFUN_PASTESELECTION in insettext!
680                         paste_tabular.reset();
681                 }
682                 // fall through
683         }
684
685         case LFUN_PASTE:
686                 if (hasPasteBuffer()) {
687                         recordUndo(cur, Undo::INSERT);
688                         pasteSelection(cur);
689                         break;
690                 }
691                 cell(cur.idx()).dispatch(cur, cmd);
692                 break;
693
694         default:
695                 // we try to handle this event in the insets dispatch function.
696                 cell(cur.idx()).dispatch(cur, cmd);
697                 break;
698         }
699
700         InsetTabularMailer(*this).updateDialog(&cur.bv());
701 }
702
703
704 // function sets an object as defined in func_status.h:
705 // states OK, Unknown, Disabled, On, Off.
706 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
707         FuncStatus & status) const
708 {
709         switch (cmd.action) {
710         case LFUN_TABULAR_FEATURE: {
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(cur.idx()));
753                         break;
754
755                 case LyXTabular::M_TOGGLE_LINE_TOP:
756                         flag = false;
757                 case LyXTabular::TOGGLE_LINE_TOP:
758                         status.setOnOff(tabular.topLine(cur.idx(), 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(cur.idx(), 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(cur.idx(), 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(cur.idx(), flag));
777                         break;
778
779                 case LyXTabular::M_ALIGN_LEFT:
780                         flag = false;
781                 case LyXTabular::ALIGN_LEFT:
782                         status.setOnOff(tabular.getAlignment(cur.idx(), 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(cur.idx(), 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(cur.idx(), flag) == LYX_ALIGN_CENTER);
795                         break;
796
797                 case LyXTabular::ALIGN_BLOCK:
798                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
799                         status.setOnOff(tabular.getAlignment(cur.idx(), 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(cur.idx(), 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(cur.idx(), 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(cur.idx(), 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(cur.idx()));
841                         break;
842
843                 case LyXTabular::UNSET_ROTATE_CELL:
844                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
845                         break;
846
847                 case LyXTabular::SET_USEBOX:
848                         status.setOnOff(strToInt(argument) == tabular.getUsebox(cur.idx()));
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 ? runparams.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         lyxerr << "# InsetTabular::setPos()  x=" << x << " y=" << y << endl;
969         int idx_min = 0;
970         int dist_min = 1000000;
971         for (idx_type i = 0; i < nargs(); ++i) {
972                 int d = getText(i)->dist(x, y);
973                 if (d < dist_min) {
974                         dist_min = d;
975                         idx_min = i;
976                 }
977         }
978         cur.idx() = idx_min;
979         InsetBase * inset = cell(cur.idx()).text_.editXY(cur, x, y);
980         //lyxerr << "# InsetTabular::setPos()\n" << cur << endl;
981         return inset;
982 }
983
984
985 int InsetTabular::getCellXPos(int cell) const
986 {
987         int c = cell;
988
989         for (; !tabular.isFirstCellInRow(c); --c)
990                 ;
991         int lx = tabular.getWidthOfColumn(cell);
992         for (; c < cell; ++c)
993                 lx += tabular.getWidthOfColumn(c);
994
995         return lx - tabular.getWidthOfColumn(cell) + xo();
996 }
997
998
999 void InsetTabular::resetPos(LCursor & cur) const
1000 {
1001         BufferView & bv = cur.bv();
1002         int const actcol = tabular.column_of_cell(cur.idx());
1003         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1004         int const new_x = getCellXPos(cur.idx()) + offset;
1005         int const old_x = cursorx_;
1006         int const col_width = tabular.getWidthOfColumn(cur.idx());
1007         cursorx_ = new_x;
1008 //    cursor.x(getCellXPos(cur.idx()) + 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 + col_width > bv.workWidth() - 20) {
1015                 scroll(bv, - col_width - 20);
1016         } else if (cursorx_ - offset < 20) {
1017                 scroll(bv, 20 - cursorx_ + offset);
1018         } else if (scroll() && xo() > 20 &&
1019                    xo() + tabular.getWidthOfTabular() > bv.workWidth() - 20) {
1020                 scroll(bv, old_x - cursorx_);
1021         }
1022
1023         InsetTabularMailer(*this).updateDialog(&bv);
1024 }
1025
1026
1027 void InsetTabular::moveNextCell(LCursor & cur)
1028 {
1029         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur.top() << endl;
1030         if (isRightToLeft(cur)) {
1031                 lyxerr << "InsetTabular::moveNextCell A cur: " << endl;
1032                 if (tabular.isFirstCellInRow(cur.idx())) {
1033                         int row = tabular.row_of_cell(cur.idx());
1034                         if (row == tabular.rows() - 1)
1035                                 return;
1036                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1037                 } else {
1038                         if (cur.idx() == 0)
1039                                 return;
1040                         --cur.idx();
1041                 }
1042         } else {
1043                 lyxerr << "InsetTabular::moveNextCell B cur: " << endl;
1044                 if (tabular.isLastCell(cur.idx()))
1045                         return;
1046                 ++cur.idx();
1047         }
1048         cur.par() = 0;
1049         cur.pos() = 0;
1050         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur.top() << endl;
1051         resetPos(cur);
1052 }
1053
1054
1055 void InsetTabular::movePrevCell(LCursor & cur)
1056 {
1057         if (isRightToLeft(cur)) {
1058                 if (tabular.isLastCellInRow(cur.idx())) {
1059                         int row = tabular.row_of_cell(cur.idx());
1060                         if (row == 0)
1061                                 return;
1062                         cur.idx() = tabular.getFirstCellInRow(row);
1063                         cur.idx() = tabular.getCellAbove(cur.idx());
1064                 } else {
1065                         if (tabular.isLastCell(cur.idx()))
1066                                 return;
1067                         ++cur.idx();
1068                 }
1069         } else {
1070                 if (cur.idx() == 0) // first cell
1071                         return;
1072                 --cur.idx();
1073         }
1074         cur.par() = 0;
1075         cur.pos() = 0;
1076         resetPos(cur);
1077 }
1078
1079
1080 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1081 {
1082         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1083
1084         int i = 0;
1085         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1086                 string const tmp = tabularFeature[i].feature;
1087
1088                 if (tmp == what.substr(0, tmp.length())) {
1089                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1090                         //tabularFeatures[i].feature.length())) {
1091                         action = tabularFeature[i].action;
1092                         break;
1093                 }
1094         }
1095         if (action == LyXTabular::LAST_ACTION)
1096                 return false;
1097
1098         string const val =
1099                 ltrim(what.substr(tabularFeature[i].feature.length()));
1100         tabularFeatures(cur, action, val);
1101         return true;
1102 }
1103
1104
1105 namespace {
1106
1107 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1108                           string const & special, bool & flag)
1109 {
1110         if (special == "dl_above") {
1111                 ltt.topDL = flag;
1112                 ltt.set = false;
1113         } else if (special == "dl_below") {
1114                 ltt.bottomDL = flag;
1115                 ltt.set = false;
1116         } else if (special == "empty") {
1117                 ltt.empty = flag;
1118                 ltt.set = false;
1119         } else if (flag) {
1120                 ltt.empty = false;
1121                 ltt.set = true;
1122         }
1123 }
1124
1125 } // anon namespace
1126
1127
1128 void InsetTabular::tabularFeatures(LCursor & cur,
1129         LyXTabular::Feature feature, string const & value)
1130 {
1131         BufferView & bv = cur.bv();
1132         int sel_col_start;
1133         int sel_col_end;
1134         int sel_row_start;
1135         int sel_row_end;
1136         bool setLines = false;
1137         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1138         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1139
1140         switch (feature) {
1141
1142         case LyXTabular::M_ALIGN_LEFT:
1143         case LyXTabular::ALIGN_LEFT:
1144                 setAlign = LYX_ALIGN_LEFT;
1145                 break;
1146
1147         case LyXTabular::M_ALIGN_RIGHT:
1148         case LyXTabular::ALIGN_RIGHT:
1149                 setAlign = LYX_ALIGN_RIGHT;
1150                 break;
1151
1152         case LyXTabular::M_ALIGN_CENTER:
1153         case LyXTabular::ALIGN_CENTER:
1154                 setAlign = LYX_ALIGN_CENTER;
1155                 break;
1156
1157         case LyXTabular::ALIGN_BLOCK:
1158                 setAlign = LYX_ALIGN_BLOCK;
1159                 break;
1160
1161         case LyXTabular::M_VALIGN_TOP:
1162         case LyXTabular::VALIGN_TOP:
1163                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1164                 break;
1165
1166         case LyXTabular::M_VALIGN_BOTTOM:
1167         case LyXTabular::VALIGN_BOTTOM:
1168                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1169                 break;
1170
1171         case LyXTabular::M_VALIGN_MIDDLE:
1172         case LyXTabular::VALIGN_MIDDLE:
1173                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1174                 break;
1175
1176         default:
1177                 break;
1178         }
1179
1180         recordUndo(cur, Undo::ATOMIC);
1181
1182         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1183         int row = tabular.row_of_cell(cur.idx());
1184         int column = tabular.column_of_cell(cur.idx());
1185         bool flag = true;
1186         LyXTabular::ltType ltt;
1187
1188         switch (feature) {
1189
1190         case LyXTabular::SET_PWIDTH: {
1191                 LyXLength const len(value);
1192                 tabular.setColumnPWidth(cur.idx(), len);
1193                 if (len.zero()
1194                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1195                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1196                 else if (!len.zero()
1197                          && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1198                         tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1199                 break;
1200         }
1201
1202         case LyXTabular::SET_MPWIDTH:
1203                 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1204                 break;
1205
1206         case LyXTabular::SET_SPECIAL_COLUMN:
1207         case LyXTabular::SET_SPECIAL_MULTI:
1208                 tabular.setAlignSpecial(cur.idx(),value,feature);
1209                 break;
1210
1211         case LyXTabular::APPEND_ROW:
1212                 // append the row into the tabular
1213                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1214                 break;
1215
1216         case LyXTabular::APPEND_COLUMN:
1217                 // append the column into the tabular
1218                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1219                 break;
1220
1221         case LyXTabular::DELETE_ROW:
1222                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1223                         tabular.deleteRow(sel_row_start);
1224                 if (sel_row_start >= tabular.rows())
1225                         --sel_row_start;
1226                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1227                 cur.par() = 0;
1228                 cur.pos() = 0;
1229                 cur.selection() = false;
1230                 break;
1231
1232         case LyXTabular::DELETE_COLUMN:
1233                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1234                         tabular.deleteColumn(sel_col_start);
1235                 if (sel_col_start >= tabular.columns())
1236                         --sel_col_start;
1237                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1238                 cur.par() = 0;
1239                 cur.pos() = 0;
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(cur.idx(), 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(cur.idx(), 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(cur.idx(), 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(cur.idx(), 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(cur.idx()))
1338                                 tabular.unsetMultiColumn(cur.idx());
1339                         else
1340                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 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                 cur.idx() = s_start;
1357                 cur.par() = 0;
1358                 cur.pos() = 0;
1359 #endif
1360                 cur.selection() = false;
1361                 break;
1362         }
1363
1364         case LyXTabular::SET_ALL_LINES:
1365                 setLines = true;
1366         case LyXTabular::UNSET_ALL_LINES:
1367 #if 0
1368                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1369                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1370                                 tabular.setAllLines(
1371                                         tabular.getCellNumber(i,j), setLines);
1372 #endif
1373                 break;
1374
1375         case LyXTabular::SET_LONGTABULAR:
1376                 tabular.setLongTabular(true);
1377                 break;
1378
1379         case LyXTabular::UNSET_LONGTABULAR:
1380                 tabular.setLongTabular(false);
1381                 break;
1382
1383         case LyXTabular::SET_ROTATE_TABULAR:
1384                 tabular.setRotateTabular(true);
1385                 break;
1386
1387         case LyXTabular::UNSET_ROTATE_TABULAR:
1388                 tabular.setRotateTabular(false);
1389                 break;
1390
1391         case LyXTabular::SET_ROTATE_CELL:
1392                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1393                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1394                                 tabular.setRotateCell(
1395                                         tabular.getCellNumber(i, j), true);
1396                 break;
1397
1398         case LyXTabular::UNSET_ROTATE_CELL:
1399                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1400                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1401                                 tabular.setRotateCell(
1402                                         tabular.getCellNumber(i, j), false);
1403                 break;
1404
1405         case LyXTabular::SET_USEBOX: {
1406                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1407                 if (val == tabular.getUsebox(cur.idx()))
1408                         val = LyXTabular::BOX_NONE;
1409                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1410                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1411                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1412                 break;
1413         }
1414
1415         case LyXTabular::UNSET_LTFIRSTHEAD:
1416                 flag = false;
1417         case LyXTabular::SET_LTFIRSTHEAD:
1418                 tabular.getRowOfLTFirstHead(row, ltt);
1419                 checkLongtableSpecial(ltt, value, flag);
1420                 tabular.setLTHead(row, flag, ltt, true);
1421                 break;
1422
1423         case LyXTabular::UNSET_LTHEAD:
1424                 flag = false;
1425         case LyXTabular::SET_LTHEAD:
1426                 tabular.getRowOfLTHead(row, ltt);
1427                 checkLongtableSpecial(ltt, value, flag);
1428                 tabular.setLTHead(row, flag, ltt, false);
1429                 break;
1430
1431         case LyXTabular::UNSET_LTFOOT:
1432                 flag = false;
1433         case LyXTabular::SET_LTFOOT:
1434                 tabular.getRowOfLTFoot(row, ltt);
1435                 checkLongtableSpecial(ltt, value, flag);
1436                 tabular.setLTFoot(row, flag, ltt, false);
1437                 break;
1438
1439         case LyXTabular::UNSET_LTLASTFOOT:
1440                 flag = false;
1441         case LyXTabular::SET_LTLASTFOOT:
1442                 tabular.getRowOfLTLastFoot(row, ltt);
1443                 checkLongtableSpecial(ltt, value, flag);
1444                 tabular.setLTFoot(row, flag, ltt, true);
1445                 break;
1446
1447         case LyXTabular::SET_LTNEWPAGE:
1448                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1449                 break;
1450
1451         // dummy stuff just to avoid warnings
1452         case LyXTabular::LAST_ACTION:
1453                 break;
1454         }
1455
1456         InsetTabularMailer(*this).updateDialog(&bv);
1457 }
1458
1459
1460 bool InsetTabular::showInsetDialog(BufferView * bv) const
1461 {
1462         InsetTabularMailer(*this).showDialog(bv);
1463         return true;
1464 }
1465
1466
1467 void InsetTabular::openLayoutDialog(BufferView * bv) const
1468 {
1469         InsetTabularMailer(*this).showDialog(bv);
1470 }
1471
1472
1473 void InsetTabular::getLabelList(Buffer const & buffer,
1474                                 vector<string> & list) const
1475 {
1476         tabular.getLabelList(buffer, list);
1477 }
1478
1479
1480 bool InsetTabular::copySelection(LCursor & cur)
1481 {
1482         if (!cur.selection())
1483                 return false;
1484
1485         int rs, re, cs, ce;
1486         getSelection(cur, rs, re, cs, ce);
1487
1488         paste_tabular.reset(new LyXTabular(tabular));
1489
1490         for (int i = 0; i < rs; ++i)
1491                 paste_tabular->deleteRow(0);
1492
1493         int const rows = re - rs + 1;
1494         while (paste_tabular->rows() > rows)
1495                 paste_tabular->deleteRow(rows);
1496
1497         paste_tabular->setTopLine(0, true, true);
1498         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1499                                      true, true);
1500
1501         for (int i = 0; i < cs; ++i)
1502                 paste_tabular->deleteColumn(0);
1503
1504         int const columns = ce - cs + 1;
1505         while (paste_tabular->columns() > columns)
1506                 paste_tabular->deleteColumn(columns);
1507
1508         paste_tabular->setLeftLine(0, true, true);
1509         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1510                                     true, true);
1511
1512         ostringstream os;
1513         OutputParams const runparams;
1514         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1515         cur.bv().stuffClipboard(os.str());
1516         return true;
1517 }
1518
1519
1520 bool InsetTabular::pasteSelection(LCursor & cur)
1521 {
1522         if (!paste_tabular)
1523                 return false;
1524         int actcol = tabular.column_of_cell(cur.idx());
1525         int actrow = tabular.row_of_cell(cur.idx());
1526         for (int r1 = 0, r2 = actrow;
1527              r1 < paste_tabular->rows() && r2 < tabular.rows();
1528              ++r1, ++r2) {
1529                 for (int c1 = 0, c2 = actcol;
1530                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1531                     ++c1, ++c2) {
1532                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1533                             tabular.isPartOfMultiColumn(r2, c2))
1534                                 continue;
1535                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1536                                 --c2;
1537                                 continue;
1538                         }
1539                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1540                                 --c1;
1541                                 continue;
1542                         }
1543                         InsetText & inset = tabular.getCellInset(r2, c2);
1544                         inset = paste_tabular->getCellInset(r1, c1);
1545                         inset.markNew();
1546                 }
1547         }
1548         return true;
1549 }
1550
1551
1552 void InsetTabular::cutSelection(LCursor & cur)
1553 {
1554         if (!cur.selection())
1555                 return;
1556
1557         bool const track = cur.buffer().params().tracking_changes;
1558         int rs, re, cs, ce;
1559         getSelection(cur, rs, re, cs, ce);
1560         for (int i = rs; i <= re; ++i)
1561                 for (int j = cs; j <= ce; ++j)
1562                         cell(tabular.getCellNumber(i, j)).clear(track);
1563
1564         // cursor position might be invalid now
1565         cur.pos() = cur.lastpos();
1566         cur.clearSelection();
1567 }
1568
1569
1570 bool InsetTabular::isRightToLeft(LCursor & cur) const
1571 {
1572         BOOST_ASSERT(cur.size() > 1);
1573         Paragraph const & parentpar = cur[cur.size() - 2].paragraph();
1574         LCursor::pos_type const parentpos = cur[cur.size() - 2].pos();
1575         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1576                                          parentpos).language()->RightToLeft();
1577 }
1578
1579
1580 void InsetTabular::getSelection(LCursor & cur,
1581         int & rs, int & re, int & cs, int & ce) const
1582 {
1583         CursorSlice const & beg = cur.selBegin();
1584         CursorSlice const & end = cur.selEnd();
1585         cs = tabular.column_of_cell(beg.idx());
1586         ce = tabular.column_of_cell(end.idx());
1587         if (cs > ce) {
1588                 ce = cs;
1589                 cs = tabular.column_of_cell(end.idx());
1590         } else {
1591                 ce = tabular.right_column_of_cell(end.idx());
1592         }
1593
1594         rs = tabular.row_of_cell(beg.idx());
1595         re = tabular.row_of_cell(end.idx());
1596         if (rs > re)
1597                 swap(rs, re);
1598 }
1599
1600
1601 size_t InsetTabular::nargs() const
1602 {
1603         return tabular.getNumberOfCells();
1604 }
1605
1606
1607 LyXText * InsetTabular::getText(int idx) const
1608 {
1609         return size_t(idx) < nargs() ? cell(idx).getText(0) : 0;
1610 }
1611
1612
1613 void InsetTabular::markErased()
1614 {
1615         for (idx_type idx = 0; idx < nargs(); ++idx)
1616                 cell(idx).markErased();
1617 }
1618
1619
1620 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1621 {
1622 #if 0
1623         const int cell = tabular.getCellFromInset(in);
1624
1625         if (cell != -1)
1626                 return tabular.getPWidth(cell).zero();
1627
1628         // this is a workaround for a crash (New, Insert->Tabular,
1629         // Insert->FootNote)
1630         if (!owner())
1631                 return false;
1632
1633         // well we didn't obviously find it so maybe our owner knows more
1634         BOOST_ASSERT(owner());
1635         return owner()->forceDefaultParagraphs(in);
1636 #endif
1637         return false;
1638 }
1639
1640
1641 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1642                                      bool usePaste)
1643 {
1644         if (buf.length() <= 0)
1645                 return true;
1646
1647         int cols = 1;
1648         int rows = 1;
1649         int maxCols = 1;
1650         string::size_type len = buf.length();
1651         string::size_type p = 0;
1652
1653         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1654                 switch (buf[p]) {
1655                 case '\t':
1656                         ++cols;
1657                         break;
1658                 case '\n':
1659                         if (p + 1 < len)
1660                                 ++rows;
1661                         maxCols = max(cols, maxCols);
1662                         cols = 1;
1663                         break;
1664                 }
1665                 ++p;
1666         }
1667         maxCols = max(cols, maxCols);
1668         LyXTabular * loctab;
1669         int cell = 0;
1670         int ocol = 0;
1671         int row = 0;
1672         if (usePaste) {
1673                 paste_tabular.reset(
1674                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1675                 loctab = paste_tabular.get();
1676                 cols = 0;
1677         } else {
1678                 loctab = &tabular;
1679                 cell = bv.cursor().idx();
1680                 ocol = tabular.column_of_cell(cell);
1681                 row = tabular.row_of_cell(cell);
1682         }
1683
1684         string::size_type op = 0;
1685         int cells = loctab->getNumberOfCells();
1686         p = 0;
1687         cols = ocol;
1688         rows = loctab->rows();
1689         int const columns = loctab->columns();
1690
1691         while (cell < cells && p < len && row < rows &&
1692                (p = buf.find_first_of("\t\n", p)) != string::npos)
1693         {
1694                 if (p >= len)
1695                         break;
1696                 switch (buf[p]) {
1697                 case '\t':
1698                         // we can only set this if we are not too far right
1699                         if (cols < columns) {
1700                                 InsetText & inset = loctab->getCellInset(cell);
1701                                 Paragraph & par = inset.text_.getPar(0);
1702                                 LyXFont const font = inset.text_.getFont(par, 0);
1703                                 inset.setText(buf.substr(op, p - op), font);
1704                                 ++cols;
1705                                 ++cell;
1706                         }
1707                         break;
1708                 case '\n':
1709                         // we can only set this if we are not too far right
1710                         if (cols < columns) {
1711                                 InsetText & inset = tabular.getCellInset(cell);
1712                                 Paragraph & par = inset.text_.getPar(0);
1713                                 LyXFont const font = inset.text_.getFont(par, 0);
1714                                 inset.setText(buf.substr(op, p - op), font);
1715                         }
1716                         cols = ocol;
1717                         ++row;
1718                         if (row < rows)
1719                                 cell = loctab->getCellNumber(row, cols);
1720                         break;
1721                 }
1722                 ++p;
1723                 op = p;
1724         }
1725         // check for the last cell if there is no trailing '\n'
1726         if (cell < cells && op < len) {
1727                 InsetText & inset = loctab->getCellInset(cell);
1728                 Paragraph & par = inset.text_.getPar(0);
1729                 LyXFont const font = inset.text_.getFont(par, 0);
1730                 inset.setText(buf.substr(op, len - op), font);
1731         }
1732         return true;
1733 }
1734
1735
1736 void InsetTabular::addPreview(PreviewLoader & loader) const
1737 {
1738         int const rows = tabular.rows();
1739         int const columns = tabular.columns();
1740         for (int i = 0; i < rows; ++i) {
1741                 for (int j = 0; j < columns; ++j)
1742                         tabular.getCellInset(i, j).addPreview(loader);
1743         }
1744 }
1745
1746
1747 bool InsetTabular::tablemode(LCursor & cur) const
1748 {
1749         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1750 }
1751
1752
1753
1754
1755
1756 string const InsetTabularMailer::name_("tabular");
1757
1758 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1759         : inset_(const_cast<InsetTabular &>(inset))
1760 {}
1761
1762
1763 string const InsetTabularMailer::inset2string(Buffer const &) const
1764 {
1765         return params2string(inset_);
1766 }
1767
1768
1769 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1770 {
1771         istringstream data(in);
1772         LyXLex lex(0,0);
1773         lex.setStream(data);
1774
1775 #ifdef WITH_WARNINGS
1776 #warning CHECK verify that this is a sane value to return.
1777 #endif
1778         if (in.empty())
1779                 return -1;
1780
1781         if (lex.isOK()) {
1782                 lex.next();
1783                 string const token = lex.getString();
1784                 if (token != name_)
1785                         return -1;
1786         }
1787
1788         int cell = -1;
1789         if (lex.isOK()) {
1790                 lex.next();
1791                 string const token = lex.getString();
1792                 if (token != "\\active_cell")
1793                         return -1;
1794                 lex.next();
1795                 cell = lex.getInteger();
1796         }
1797
1798         // This is part of the inset proper that is usually swallowed
1799         // by Buffer::readInset
1800         if (lex.isOK()) {
1801                 lex.next();
1802                 string const token = lex.getString();
1803                 if (token != "Tabular")
1804                         return -1;
1805         }
1806
1807         if (!lex.isOK())
1808                 return -1;
1809
1810         Buffer const & buffer = inset.buffer();
1811         inset.read(buffer, lex);
1812
1813         // We can't set the active cell, but we can tell the frontend
1814         // what it is.
1815         return cell;
1816 }
1817
1818
1819 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1820 {
1821         ostringstream data;
1822 #ifdef WITH_WARNINGS
1823 #warning wrong!
1824 #endif
1825         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
1826         data << name_ << " \\active_cell " << 0 << '\n';
1827         inset.write(inset.buffer(), data);
1828         data << "\\end_inset\n";
1829         return data.str();
1830 }