]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
d7851585cdf7e63a9cef879b56f745cc6508f678
[features.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 "support/std_sstream.h"
41
42 #include <iostream>
43
44 using lyx::graphics::PreviewLoader;
45
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
49
50 using std::endl;
51 using std::max;
52 using std::swap;
53 using std::string;
54 using std::auto_ptr;
55 using std::istringstream;
56 using std::ostream;
57 using std::ostringstream;
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), cursory_(0), tablemode(false)
158 {
159         tabular.setOwner(this);
160         clearSelection();
161         in_reset_pos = 0;
162 }
163
164
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166         : UpdatableInset(tab), tabular(tab.tabular),
167                 buffer_(tab.buffer_), cursorx_(0), cursory_(0), tablemode(false)
168 {
169         tabular.setOwner(this);
170         clearSelection();
171         in_reset_pos = 0;
172 }
173
174
175 InsetTabular::~InsetTabular()
176 {
177         InsetTabularMailer(*this).hideDialog();
178 }
179
180
181 auto_ptr<InsetBase> InsetTabular::clone() const
182 {
183         return auto_ptr<InsetBase>(new InsetTabular(*this));
184 }
185
186
187 Buffer const & InsetTabular::buffer() const
188 {
189         return *buffer_;
190 }
191
192
193 void InsetTabular::buffer(Buffer * b)
194 {
195         buffer_ = b;
196 }
197
198
199 void InsetTabular::write(Buffer const & buf, ostream & os) const
200 {
201         os << "Tabular" << endl;
202         tabular.write(buf, os);
203 }
204
205
206 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
207 {
208         bool const old_format = (lex.getString() == "\\LyXTable");
209
210         tabular.read(buf, lex);
211
212         if (old_format)
213                 return;
214
215         lex.nextToken();
216         string token = lex.getString();
217         while (lex.isOK() && (token != "\\end_inset")) {
218                 lex.nextToken();
219                 token = lex.getString();
220         }
221         if (token != "\\end_inset") {
222                 lex.printError("Missing \\end_inset at this point. "
223                                "Read: `$$Token'");
224         }
225 }
226
227
228 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
229 {
230         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
231         //      mi.base.textwidth << "\n";
232         if (!mi.base.bv) {
233                 lyxerr << "InsetTabular::metrics: need bv" << endl;
234                 BOOST_ASSERT(false);
235         }
236
237         calculate_dimensions_of_cells(mi);
238
239         dim.asc = tabular.getAscentOfRow(0);
240         dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
241         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
242         dim_ = dim;
243 }
244
245
246 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
247 {
248         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
249
250         BufferView * bv = pi.base.bv;
251
252         if (!owner())
253                 x += scroll();
254
255         xo_ = x;
256         yo_ = y;
257         x += ADD_TO_TABULAR_WIDTH;
258
259         int cell = 0;
260         first_visible_cell = -1;
261         for (int i = 0; i < tabular.rows(); ++i) {
262                 int nx = x;
263                 cell = tabular.getCellNumber(i, 0);
264                 if (y + tabular.getDescentOfRow(i) <= 0 &&
265                           y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
266                 {
267                         y += tabular.getDescentOfRow(i) +
268                                         tabular.getAscentOfRow(i + 1) +
269                                         tabular.getAdditionalHeight(i + 1);
270                         continue;
271                 }
272                 for (int j = 0; j < tabular.columns(); ++j) {
273                         if (nx > bv->workWidth())
274                                 break;
275                         if (tabular.isPartOfMultiColumn(i, j))
276                                 continue;
277                         if (first_visible_cell < 0)
278                                 first_visible_cell = cell;
279                         if (hasSelection())
280                                 drawCellSelection(pi.pain, nx, y, i, j, cell);
281
282                         int const cx = nx + tabular.getBeginningOfTextInCell(cell);
283                         tabular.getCellInset(cell).draw(pi, cx, y);
284                         drawCellLines(pi.pain, nx, y, i, cell);
285                         nx += tabular.getWidthOfColumn(cell);
286                         ++cell;
287                 }
288
289 // Would be nice, but for some completely unfathomable reason,
290 // on a col resize to a new fixed width, even though the insettexts
291 // are resized, the cell isn't, but drawing all cells in a tall table
292 // has the desired effect somehow. Complete dark magic.
293 #if 0
294                 // avoiding drawing the rest of a long table is
295                 // a pretty big speedup
296                 if (y > bv->workHeight())
297                         break;
298 #endif
299
300                 y += tabular.getDescentOfRow(i) +
301                         tabular.getAscentOfRow(i + 1) +
302                         tabular.getAdditionalHeight(i + 1);
303         }
304 }
305
306
307 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
308                                  int row, int cell) const
309 {
310         int x2 = x + tabular.getWidthOfColumn(cell);
311         bool on_off = false;
312
313         if (!tabular.topAlreadyDrawn(cell)) {
314                 on_off = !tabular.topLine(cell);
315                 pain.line(x, y - tabular.getAscentOfRow(row),
316                           x2, y -  tabular.getAscentOfRow(row),
317                           on_off ? LColor::tabularonoffline : LColor::tabularline,
318                           on_off ? Painter::line_onoffdash : Painter::line_solid);
319         }
320         on_off = !tabular.bottomLine(cell);
321         pain.line(x, y + tabular.getDescentOfRow(row),
322                   x2, y + tabular.getDescentOfRow(row),
323                   on_off ? LColor::tabularonoffline : LColor::tabularline,
324                   on_off ? Painter::line_onoffdash : Painter::line_solid);
325         if (!tabular.leftAlreadyDrawn(cell)) {
326                 on_off = !tabular.leftLine(cell);
327                 pain.line(x, y -  tabular.getAscentOfRow(row),
328                           x, y +  tabular.getDescentOfRow(row),
329                           on_off ? LColor::tabularonoffline : LColor::tabularline,
330                           on_off ? Painter::line_onoffdash : Painter::line_solid);
331         }
332         on_off = !tabular.rightLine(cell);
333         pain.line(x2 - tabular.getAdditionalWidth(cell),
334                   y -  tabular.getAscentOfRow(row),
335                   x2 - tabular.getAdditionalWidth(cell),
336                   y +  tabular.getDescentOfRow(row),
337                   on_off ? LColor::tabularonoffline : LColor::tabularline,
338                   on_off ? Painter::line_onoffdash : Painter::line_solid);
339 }
340
341
342 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
343                                      int row, int column, int cell) const
344 {
345         if (!tablemode)
346                 return;
347
348         BOOST_ASSERT(hasSelection());
349         int cs = tabular.column_of_cell(sel_cell_start);
350         int ce = tabular.column_of_cell(sel_cell_end);
351         if (cs > ce) {
352                 ce = cs;
353                 cs = tabular.column_of_cell(sel_cell_end);
354         } else {
355                 ce = tabular.right_column_of_cell(sel_cell_end);
356         }
357
358         int rs = tabular.row_of_cell(sel_cell_start);
359         int re = tabular.row_of_cell(sel_cell_end);
360         if (rs > re)
361                 swap(rs, re);
362
363         if (column >= cs && column <= ce && row >= rs && row <= re) {
364                 int w = tabular.getWidthOfColumn(cell);
365                 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
366                 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
367                                    w, h, LColor::selection);
368         }
369 }
370
371
372 string const InsetTabular::editMessage() const
373 {
374         return _("Opened table");
375 }
376
377
378 void InsetTabular::updateLocal(BufferView & bv) const
379 {
380         bv.update();
381         resetPos(bv);
382 }
383
384 extern LCursor theTempCursor;
385
386
387
388 void InsetTabular::lfunMousePress(BufferView & bv, FuncRequest const & cmd)
389 {
390         if (hasSelection() && cmd.button() == mouse_button::button3)
391                 return;
392
393         int cell = getCell(cmd.x + xo_, cmd.y + yo_);
394         clearSelection();
395
396         lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
397         if (cell == -1) {
398                 tablemode = true;
399                 bv.fullCursor(theTempCursor);
400                 bv.fullCursor().push(this);
401                 bv.fullCursor().idx() = cell;
402         } else {
403                 tablemode = false;
404                 setPos(bv, cmd.x, cmd.y);
405                 bv.fullCursor(theTempCursor);
406                 bv.fullCursor().idx() = cell;
407         }
408         lyxerr << bv.cursor() << endl;
409
410         if (cmd.button() == mouse_button::button2)
411                 dispatch(bv, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
412 }
413
414
415 void InsetTabular::lfunMouseMotion(BufferView & bv, FuncRequest const & cmd)
416 {
417         int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
418         lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
419
420         setPos(bv, cmd.x, cmd.y);
421         if (!hasSelection()) {
422                 setSelection(actcell, actcell);
423                 bv.setSelection();
424         } else {
425                 bv.cursor().idx() = actcell;
426                 setSelection(sel_cell_start, actcell);
427                 tablemode = (sel_cell_start != actcell);
428         }
429 }
430
431
432 void InsetTabular::lfunMouseRelease(BufferView & bv, FuncRequest const & cmd)
433 {
434         int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
435         lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
436         if (cmd.button() == mouse_button::button3)
437                 InsetTabularMailer(*this).showDialog(&bv);
438 }
439
440
441 void InsetTabular::edit(BufferView * view, bool left)
442 {
443         BufferView & bv = *view;
444         lyxerr << "InsetTabular::edit: " << this << endl;
445         finishUndo();
446         //tablemode = false;
447         int cell;
448         if (left) {
449                 if (isRightToLeft(bv))
450                         cell = tabular.getLastCellInRow(0);
451                 else
452                         cell = 0;
453         } else {
454                 if (isRightToLeft(bv))
455                         cell = tabular.getFirstCellInRow(tabular.rows()-1);
456                 else
457                         cell = tabular.getNumberOfCells() - 1;
458         }
459         clearSelection();
460         resetPos(bv);
461         bv.fitCursor();
462         bv.fullCursor().push(this);
463         bv.fullCursor().idx() = cell;
464         lyxerr << bv.cursor() << endl;
465 }
466
467
468 void InsetTabular::edit(BufferView * bv, int x, int y)
469 {
470         lyxerr << "InsetTabular::edit: " << this << " first cell "
471                 << &tabular.cell_info[0][0].inset << endl;
472
473         finishUndo();
474         setPos(*bv, x, y);
475         clearSelection();
476         finishUndo();
477         //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
478         bv->fullCursor().push(this);
479         //if (x > xx)
480         //      activateCellInset(bv, cell, x - xx, y - cursory_);
481 }
482
483
484 DispatchResult
485 InsetTabular::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
486 {
487         lyxerr << "# InsetTabular::dispatch: " << cmd
488                 << " tablemode: " << tablemode << endl;
489
490         DispatchResult result(true, true);
491         switch (cmd.action) {
492
493         case LFUN_MOUSE_PRESS:
494                 lfunMousePress(bv, cmd);
495                 return DispatchResult(true, true);
496
497         case LFUN_MOUSE_MOTION:
498                 lfunMouseMotion(bv, cmd);
499                 return DispatchResult(true, true);
500
501         case LFUN_MOUSE_RELEASE:
502                 lfunMouseRelease(bv, cmd);
503                 return DispatchResult(true, true);
504
505         default:
506                 break;
507         }
508
509         CursorSlice & cur = bv.cursor();
510
511         if (!tablemode) {
512                 
513                 int cell = cur.idx_;
514                 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
515                 result = tabular.getCellInset(cell).dispatch(bv, cmd);
516
517                 switch (result.val()) {
518                 case FINISHED:
519                         if (movePrevCell(bv, cur))
520                                 result = DispatchResult(true, true);
521                         else
522                                 result = DispatchResult(false, FINISHED);
523                         break;
524
525                 case FINISHED_RIGHT:
526                         if (moveNextCell(bv, cur))
527                                 result = DispatchResult(true, true);
528                         else
529                                 result = DispatchResult(false, FINISHED_RIGHT);
530                         break;
531
532                 case FINISHED_UP:
533                         if (moveUpLock(bv, cur))
534                                 result = DispatchResult(true, true);
535                         else
536                                 result = DispatchResult(false, FINISHED_UP);
537                         break;
538
539                 case FINISHED_DOWN:
540                         if (moveDownLock(bv, cur))
541                                 result = DispatchResult(true, true);
542                         else
543                                 result = DispatchResult(false, FINISHED_UP);
544                         break;
545
546                 default:
547                         lyxerr << "# don't handle dispatch" << endl;
548                         break;
549                 }
550
551                 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
552         } else {
553
554                 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
555                 switch (cmd.action) {
556
557                 case LFUN_CELL_BACKWARD:
558                 case LFUN_CELL_FORWARD:
559                         if (cmd.action == LFUN_CELL_FORWARD)
560                                 moveNextCell(bv, cur);
561                         else
562                                 movePrevCell(bv, cur);
563                         clearSelection();
564                         return result;
565
566                 case LFUN_SCROLL_INSET:
567                         if (!cmd.argument.empty()) {
568                                 if (cmd.argument.find('.') != cmd.argument.npos)
569                                         scroll(bv, static_cast<float>(strToDbl(cmd.argument)));
570                                 else
571                                         scroll(bv, strToInt(cmd.argument));
572                                 bv.update();
573                                 return DispatchResult(true, true);
574                         }
575
576                 case LFUN_RIGHTSEL: {
577                         int const start = hasSelection() ? sel_cell_start : cur.idx_;
578                         if (tabular.isLastCellInRow(cur.idx_)) {
579                                 setSelection(start, cur.idx_);
580                                 break;
581                         }
582
583                         int end = cur.idx_;
584                         // if we are starting a selection, only select
585                         // the current cell at the beginning
586                         if (hasSelection()) {
587                                 moveRight(bv, cur);
588                                 end = cur.idx_;
589                         }
590                         setSelection(start, end);
591                         break;
592                 }
593
594                 case LFUN_RIGHT:
595                         if (!moveRightLock(bv, cur))
596                                 result = DispatchResult(false, FINISHED_RIGHT);
597                         clearSelection();
598                         break;
599
600                 case LFUN_LEFTSEL: {
601                         int const start = hasSelection() ? sel_cell_start : cur.idx_;
602                         if (tabular.isFirstCellInRow(cur.idx_)) {
603                                 setSelection(start, cur.idx_);
604                                 break;
605                         }
606
607                         int end = cur.idx_;
608                         // if we are starting a selection, only select
609                         // the current cell at the beginning
610                         if (hasSelection()) {
611                                 moveLeft(bv, cur);
612                                 end = cur.idx_;
613                         }
614                         setSelection(start, end);
615                         break;
616                 }
617
618                 case LFUN_LEFT:
619                         if (!moveLeftLock(bv, cur))
620                                 result = DispatchResult(false, FINISHED);
621                         clearSelection();
622                         break;
623
624                 case LFUN_DOWNSEL: {
625                         int const start = hasSelection() ? sel_cell_start : cur.idx_;
626                         int const ocell = cur.idx_;
627                         // if we are starting a selection, only select
628                         // the current cell at the beginning
629                         if (hasSelection()) {
630                                 moveDown(bv, cur);
631                                 if (ocell == sel_cell_end ||
632                                                 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
633                                         setSelection(start, tabular.getCellBelow(sel_cell_end));
634                                 else
635                                         setSelection(start, tabular.getLastCellBelow(sel_cell_end));
636                         } else {
637                                 setSelection(start, start);
638                         }
639                         break;
640                 }
641
642                 case LFUN_DOWN:
643                         if (!moveDown(bv, cur))
644                                 result = DispatchResult(false, FINISHED_DOWN);
645                         clearSelection();
646                         break;
647
648                 case LFUN_UPSEL: {
649                         int const start = hasSelection() ? sel_cell_start : cur.idx_;
650                         int const ocell = cur.idx_;
651                         // if we are starting a selection, only select
652                         // the current cell at the beginning
653                         if (hasSelection()) {
654                                 moveUp(bv, cur);
655                                 if (ocell == sel_cell_end ||
656                                                 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
657                                         setSelection(start, tabular.getCellAbove(sel_cell_end));
658                                 else
659                                         setSelection(start, tabular.getLastCellAbove(sel_cell_end));
660                         } else {
661                                 setSelection(start, start);
662                         }
663                         break;
664                 }
665
666                 case LFUN_UP:
667                         if (!moveUp(bv, cur))
668                                 result = DispatchResult(false, FINISHED_DOWN);
669                         clearSelection();
670                         break;
671
672                 case LFUN_NEXT: {
673                         if (hasSelection())
674                                 clearSelection();
675                         int actcell = bv.cursor().idx();
676                         int actcol = tabular.column_of_cell(actcell);
677                         int column = actcol;
678                         if (bv.top_y() + bv.painter().paperHeight()
679                                         < yo_ + tabular.getHeightOfTabular())
680                         {
681                                 bv.scrollDocView(bv.top_y() + bv.painter().paperHeight());
682                                 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
683                         } else {
684                                 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
685                         }
686                         resetPos(bv);
687                         break;
688                 }
689
690                 case LFUN_PRIOR: {
691                         if (hasSelection())
692                                 clearSelection();
693                         int actcell = bv.cursor().idx();
694                         int actcol = tabular.column_of_cell(actcell);
695                         int column = actcol;
696                         if (yo_ < 0) {
697                                 bv.scrollDocView(bv.top_y() - bv.painter().paperHeight());
698                                 if (yo_ > 0)
699                                         cur.idx() = column;
700                                 else
701                                         cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
702                         } else {
703                                 cur.idx() = column;
704                         }
705                         resetPos(bv);
706                         break;
707                 }
708
709                 // none of these make sense for insettabular,
710                 // but we must catch them to prevent any
711                 // selection from being confused
712                 case LFUN_PRIORSEL:
713                 case LFUN_NEXTSEL:
714                 case LFUN_WORDLEFT:
715                 case LFUN_WORDLEFTSEL:
716                 case LFUN_WORDRIGHT:
717                 case LFUN_WORDRIGHTSEL:
718                 case LFUN_WORDSEL:
719                 case LFUN_DOWN_PARAGRAPH:
720                 case LFUN_DOWN_PARAGRAPHSEL:
721                 case LFUN_UP_PARAGRAPH:
722                 case LFUN_UP_PARAGRAPHSEL:
723                 case LFUN_BACKSPACE:
724                 case LFUN_HOME:
725                 case LFUN_HOMESEL:
726                 case LFUN_END:
727                 case LFUN_ENDSEL:
728                 case LFUN_BEGINNINGBUF:
729                 case LFUN_BEGINNINGBUFSEL:
730                 case LFUN_ENDBUF:
731                 case LFUN_ENDBUFSEL:
732                         break;
733
734                 case LFUN_LAYOUT_TABULAR:
735                         InsetTabularMailer(*this).showDialog(&bv);
736                         break;
737
738                 case LFUN_INSET_DIALOG_UPDATE:
739                         InsetTabularMailer(*this).updateDialog(&bv);
740                         break;
741
742                 case LFUN_TABULAR_FEATURE:
743                         if (!tabularFeatures(bv, cmd.argument))
744                                 result = DispatchResult(false);
745                         break;
746
747                 // insert file functions
748                 case LFUN_FILE_INSERT_ASCII_PARA:
749                 case LFUN_FILE_INSERT_ASCII: {
750                         string tmpstr = getContentsOfAsciiFile(&bv, cmd.argument, false);
751                         if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
752                                 result = DispatchResult(false);
753                         break;
754                 }
755
756                 case LFUN_LANGUAGE:
757                 case LFUN_EMPH:
758                 case LFUN_BOLD:
759                 case LFUN_NOUN:
760                 case LFUN_CODE:
761                 case LFUN_SANS:
762                 case LFUN_ROMAN:
763                 case LFUN_DEFAULT:
764                 case LFUN_UNDERLINE:
765                 case LFUN_FONT_SIZE:
766                         lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
767                         break;
768
769                 case LFUN_CUT:
770                         if (copySelection(bv)) {
771                                 recordUndo(bv, Undo::DELETE);
772                                 cutSelection(bv.buffer()->params());
773                         }
774                         break;
775
776                 case LFUN_DELETE:
777                         recordUndo(bv, Undo::DELETE);
778                         cutSelection(bv.buffer()->params());
779                         break;
780
781                 case LFUN_COPY:
782                         if (!hasSelection())
783                                 break;
784                         finishUndo();
785                         copySelection(bv);
786                         break;
787
788                 case LFUN_PASTESELECTION: {
789                         string const clip = bv.getClipboard();
790                         if (clip.empty())
791                                 break;
792                         if (clip.find('\t') != string::npos) {
793                                 int cols = 1;
794                                 int rows = 1;
795                                 int maxCols = 1;
796                                 string::size_type len = clip.length();
797                                 string::size_type p = 0;
798
799                                 while (p < len &&
800                                                         (p = clip.find_first_of("\t\n", p)) != string::npos) {
801                                         switch (clip[p]) {
802                                         case '\t':
803                                                 ++cols;
804                                                 break;
805                                         case '\n':
806                                                 if (p + 1 < len)
807                                                         ++rows;
808                                                 maxCols = max(cols, maxCols);
809                                                 cols = 1;
810                                                 break;
811                                         }
812                                         ++p;
813                                 }
814                                 maxCols = max(cols, maxCols);
815
816                                 paste_tabular.reset(
817                                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
818
819                                 string::size_type op = 0;
820                                 int cell = 0;
821                                 int cells = paste_tabular->getNumberOfCells();
822                                 p = 0;
823                                 cols = 0;
824                                 LyXFont font;
825                                 while (cell < cells && p < len &&
826                                                         (p = clip.find_first_of("\t\n", p)) != string::npos) {
827                                         if (p >= len)
828                                                 break;
829                                         switch (clip[p]) {
830                                         case '\t':
831                                                 paste_tabular->getCellInset(cell).
832                                                         setText(clip.substr(op, p-op), font);
833                                                 ++cols;
834                                                 ++cell;
835                                                 break;
836                                         case '\n':
837                                                 paste_tabular->getCellInset(cell).
838                                                         setText(clip.substr(op, p-op), font);
839                                                 while (cols++ < maxCols)
840                                                         ++cell;
841                                                 cols = 0;
842                                                 break;
843                                         }
844                                         ++p;
845                                         op = p;
846                                 }
847                                 // check for the last cell if there is no trailing '\n'
848                                 if (cell < cells && op < len)
849                                         paste_tabular->getCellInset(cell).
850                                                 setText(clip.substr(op, len-op), font);
851                         } else if (!insertAsciiString(bv, clip, true))
852                         {
853                                 // so that the clipboard is used and it goes on
854                                 // to default
855                                 // and executes LFUN_PASTESELECTION in insettext!
856                                 paste_tabular.reset();
857                         }
858                         // fall through
859                 }
860
861                 case LFUN_PASTE:
862                         if (hasPasteBuffer()) {
863                                 recordUndo(bv, Undo::INSERT);
864                                 pasteSelection(bv);
865                                 break;
866                         }
867                         // fall through
868
869                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
870
871                 default:
872                         // handle font changing stuff on selection before we lock the inset
873                         // in the default part!
874                         result = DispatchResult(false);
875                         // we try to activate the actual inset and put this event down to
876                         // the insets dispatch function.
877                         break;
878                 }
879
880                 updateLocal(bv);
881                 InsetTabularMailer(*this).updateDialog(&bv);
882         }
883
884         if (cmd.action == LFUN_INSET_TOGGLE) {
885                 tablemode = !tablemode;
886                 result = DispatchResult(true, true);
887         }
888
889         return result;
890 }
891
892
893 int InsetTabular::latex(Buffer const & buf, ostream & os,
894                         OutputParams const & runparams) const
895 {
896         return tabular.latex(buf, os, runparams);
897 }
898
899
900 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
901                         OutputParams const & runparams) const
902 {
903         int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
904         return tabular.plaintext(buf, os, runparams, dp, false, 0);
905 }
906
907
908 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
909                            OutputParams const & runparams) const
910 {
911         return tabular.linuxdoc(buf,os, runparams);
912 }
913
914
915 int InsetTabular::docbook(Buffer const & buf, ostream & os,
916                           OutputParams const & runparams) const
917 {
918         int ret = 0;
919         InsetOld * master;
920
921         // if the table is inside a float it doesn't need the informaltable
922         // wrapper. Search for it.
923         for (master = owner(); master; master = master->owner())
924                 if (master->lyxCode() == InsetOld::FLOAT_CODE)
925                         break;
926
927         if (!master) {
928                 os << "<informaltable>";
929                 if (runparams.mixed_content)
930                         os << endl;
931                 ++ret;
932         }
933         ret += tabular.docbook(buf, os, runparams);
934         if (!master) {
935                 os << "</informaltable>";
936                 if (runparams.mixed_content)
937                         os << endl;
938                 ++ret;
939         }
940         return ret;
941 }
942
943
944 void InsetTabular::validate(LaTeXFeatures & features) const
945 {
946         tabular.validate(features);
947 }
948
949
950 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
951 {
952         for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
953                 int maxAsc = 0;
954                 int maxDesc = 0;
955                 for (int j = 0; j < tabular.columns(); ++j) {
956                         if (tabular.isPartOfMultiColumn(i, j))
957                                 continue;
958                         ++cell;
959                         Dimension dim;
960                         MetricsInfo m = mi;
961                         m.base.textwidth =
962                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
963                         tabular.getCellInset(cell).metrics(m, dim);
964                         maxAsc  = max(maxAsc, dim.asc);
965                         maxDesc = max(maxDesc, dim.des);
966                         tabular.setWidthOfCell(cell, dim.wid);
967                 }
968                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
969                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
970         }
971 }
972
973
974 void InsetTabular::getCursorPos(int cell, int & x, int & y) const
975 {
976         InsetText const & inset = tabular.getCellInset(cell);
977         inset.getCursorPos(cell, x, y);
978         x += inset.x() - xo_;
979         y += inset.y() - yo_;
980 }
981
982
983 void InsetTabular::setPos(BufferView & bv, int x, int y) const
984 {
985         int const cell = getCell(x + xo_, y + yo_);
986         lyxerr << "# InsetTabular::setPos()  cell: " << cell << endl;
987         InsetText const & inset = tabular.getCellInset(cell);
988         inset.text_.setCursorFromCoordinates(x, y);
989 #if 0
990         cursory_ = 0;
991         int actcell = 0;
992         int actrow = 0;
993         int actcol = 0;
994         int ly = tabular.getDescentOfRow(actrow);
995
996         // first search the right row
997         while (ly < y && actrow + 1 < tabular.rows()) {
998                 cursory_ += tabular.getDescentOfRow(actrow) +
999                                  tabular.getAscentOfRow(actrow + 1) +
1000                                  tabular.getAdditionalHeight(actrow + 1);
1001                 ++actrow;
1002                 ly = cursory_ + tabular.getDescentOfRow(actrow);
1003         }
1004         actcell = tabular.getCellNumber(actrow, actcol);
1005
1006         // now search the right column
1007         int lx = tabular.getWidthOfColumn(actcell) -
1008                 tabular.getAdditionalWidth(actcell);
1009
1010         for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1011                 lx += tabular.getWidthOfColumn(actcell + 1)
1012                         + tabular.getAdditionalWidth(actcell);
1013
1014         cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1015 #endif
1016         resetPos(bv);
1017 }
1018
1019
1020 int InsetTabular::getCellXPos(int cell) const
1021 {
1022         int c = cell;
1023
1024         for (; !tabular.isFirstCellInRow(c); --c)
1025                 ;
1026         int lx = tabular.getWidthOfColumn(cell);
1027         for (; c < cell; ++c)
1028                 lx += tabular.getWidthOfColumn(c);
1029
1030         return lx - tabular.getWidthOfColumn(cell) + xo_;
1031 }
1032
1033
1034 void InsetTabular::resetPos(BufferView &) const
1035 {
1036 #if 0
1037 #ifdef WITH_WARNINGS
1038 #warning This should be fixed in the right manner (20011128 Jug)
1039 #endif
1040         // fast hack to fix infinite repaintings!
1041         if (in_reset_pos > 0)
1042                 return;
1043
1044         int cell = 0;
1045         int actcell = bv->cursor().cell();
1046         int actcol = tabular.column_of_cell(actcell);
1047         int actrow = 0;
1048         cursory_ = 0;
1049         for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1050                 if (tabular.isLastCellInRow(cell)) {
1051                         cursory_ += tabular.getDescentOfRow(actrow) +
1052                                          tabular.getAscentOfRow(actrow + 1) +
1053                                          tabular.getAdditionalHeight(actrow + 1);
1054                         ++actrow;
1055                 }
1056         }
1057
1058         // we need this only from here on!!!
1059         ++in_reset_pos;
1060         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1061         int new_x = getCellXPos(actcell) + offset;
1062         int old_x = cursorx_;
1063         cursorx_ = new_x;
1064 //    cursor.x(getCellXPos(actcell) + offset);
1065         if (actcol < tabular.columns() - 1 && scroll(false) &&
1066                 tabular.getWidthOfTabular() < bv->workWidth()-20)
1067         {
1068                 scroll(bv, 0.0F);
1069                 updateLocal(bv);
1070         } else if (cursorx_ - offset > 20 &&
1071                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1072                    > bv.workWidth() - 20) {
1073                 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1074                 updateLocal(bv);
1075         } else if (cursorx_ - offset < 20) {
1076                 scroll(&bv, 20 - cursorx_ + offset);
1077                 updateLocal(bv);
1078         } else if (scroll() && xo_ > 20 &&
1079                    xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1080                 scroll(&bv, old_x - cursorx_);
1081                 updateLocal(bv);
1082         }
1083         InsetTabularMailer(*this).updateDialog(bv);
1084         in_reset_pos = 0;
1085 #endif
1086 }
1087
1088
1089 bool InsetTabular::moveRight(BufferView & bv, CursorSlice & cur)
1090 {
1091         bool moved = isRightToLeft(bv)
1092                 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1093         if (!moved)
1094                 return false;
1095         resetPos(bv);
1096         return true;
1097 }
1098
1099
1100 bool InsetTabular::moveRightLock(BufferView & bv, CursorSlice & cur)
1101 {
1102         bool moved = isRightToLeft(bv)
1103                 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1104         if (!moved)
1105                 return false;
1106         activateCellInset(bv, cur.idx_, false);
1107         return true;
1108 }
1109
1110
1111 bool InsetTabular::moveLeft(BufferView & bv, CursorSlice & cur)
1112 {
1113         bool moved = isRightToLeft(bv)
1114                 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1115         if (!moved)
1116                 return false;
1117         resetPos(bv);
1118         return true;
1119 }
1120
1121
1122 bool InsetTabular::moveLeftLock(BufferView & bv, CursorSlice & cur)
1123 {
1124         bool moved = isRightToLeft(bv)
1125                 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1126         if (!moved)
1127                 return false;
1128         activateCellInset(bv, cur.idx_, true);
1129         return true;
1130 }
1131
1132
1133 bool InsetTabular::moveUp(BufferView & bv, CursorSlice & cur)
1134 {
1135         if (tabular.row_of_cell(cur.idx_) == 0)
1136                 return false;
1137         cur.idx_ = tabular.getCellAbove(cur.idx_);
1138         resetPos(bv);
1139         return true;
1140 }
1141
1142
1143 bool InsetTabular::moveUpLock(BufferView & bv, CursorSlice & cur)
1144 {
1145         if (tabular.row_of_cell(cur.idx_) == 0)
1146                 return false;
1147         cur.idx_ = tabular.getCellAbove(cur.idx_);
1148         resetPos(bv);
1149         activateCellInset(bv, cur.idx_, bv.x_target(), 0);
1150         return true;
1151 }
1152
1153
1154 bool InsetTabular::moveDown(BufferView & bv, CursorSlice & cur)
1155 {
1156         if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1157                 return false;
1158         cur.idx_ = tabular.getCellBelow(cur.idx_);
1159         resetPos(bv);
1160         return true;
1161 }
1162
1163
1164 bool InsetTabular::moveDownLock(BufferView & bv, CursorSlice & cur)
1165 {
1166         if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1167                 return false;
1168         cur.idx_ = tabular.getCellBelow(cur.idx_);
1169         resetPos(bv);
1170         activateCellInset(bv, cur.idx_, bv.x_target());
1171         return true;
1172 }
1173
1174
1175 bool InsetTabular::moveNextCell(BufferView & bv, CursorSlice & cur)
1176 {
1177         lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1178         if (isRightToLeft(bv)) {
1179                 if (tabular.isFirstCellInRow(cur.idx_)) {
1180                         int row = tabular.row_of_cell(cur.idx_);
1181                         if (row == tabular.rows() - 1)
1182                                 return false;
1183                         cur.idx_ = tabular.getLastCellInRow(row);
1184                         cur.idx_ = tabular.getCellBelow(cur.idx_);
1185                 } else {
1186                         if (cur.idx_ == 0)
1187                                 return false;
1188                         --cur.idx_;
1189                 }
1190         } else {
1191                 if (tabular.isLastCell(cur.idx_))
1192                         return false;
1193                 ++cur.idx_;
1194         }
1195         cur.par_ = 0;
1196         cur.pos_ = 0;
1197         lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1198         resetPos(bv);
1199         return true;
1200 }
1201
1202
1203 bool InsetTabular::movePrevCell(BufferView & bv, CursorSlice & cur)
1204 {
1205         if (isRightToLeft(bv)) {
1206                 if (tabular.isLastCellInRow(cur.idx_)) {
1207                         int row = tabular.row_of_cell(cur.idx_);
1208                         if (row == 0)
1209                                 return false;
1210                         cur.idx_ = tabular.getFirstCellInRow(row);
1211                         cur.idx_ = tabular.getCellAbove(cur.idx_);
1212                 } else {
1213                         if (tabular.isLastCell(cur.idx_))
1214                                 return false;
1215                         ++cur.idx_;
1216                 }
1217         } else {
1218                 if (cur.idx_ == 0) // first cell
1219                         return false;
1220                 --cur.idx_;
1221         }
1222         cur.par_ = 0;
1223         cur.pos_ = 0;
1224         resetPos(bv);
1225         return true;
1226 }
1227
1228
1229 bool InsetTabular::tabularFeatures(BufferView & bv, string const & what)
1230 {
1231         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1232
1233         int i = 0;
1234         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1235                 string const tmp = tabularFeature[i].feature;
1236
1237                 if (tmp == what.substr(0, tmp.length())) {
1238                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1239                         //tabularFeatures[i].feature.length())) {
1240                         action = tabularFeature[i].action;
1241                         break;
1242                 }
1243         }
1244         if (action == LyXTabular::LAST_ACTION)
1245                 return false;
1246
1247         string const val =
1248                 ltrim(what.substr(tabularFeature[i].feature.length()));
1249         tabularFeatures(bv, action, val);
1250         return true;
1251 }
1252
1253
1254 namespace {
1255
1256 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1257                           string const & special, bool & flag)
1258 {
1259         if (special == "dl_above") {
1260                 ltt.topDL = flag;
1261                 ltt.set = false;
1262         } else if (special == "dl_below") {
1263                 ltt.bottomDL = flag;
1264                 ltt.set = false;
1265         } else if (special == "empty") {
1266                 ltt.empty = flag;
1267                 ltt.set = false;
1268         } else if (flag) {
1269                 ltt.empty = false;
1270                 ltt.set = true;
1271         }
1272 }
1273
1274 } // anon namespace
1275
1276
1277 void InsetTabular::tabularFeatures(BufferView & bv,
1278         LyXTabular::Feature feature, string const & value)
1279 {
1280         int actcell = bv.cursor().idx();
1281         int sel_col_start;
1282         int sel_col_end;
1283         int sel_row_start;
1284         int sel_row_end;
1285         bool setLines = false;
1286         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1287         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1288
1289         switch (feature) {
1290
1291         case LyXTabular::M_ALIGN_LEFT:
1292         case LyXTabular::ALIGN_LEFT:
1293                 setAlign = LYX_ALIGN_LEFT;
1294                 break;
1295
1296         case LyXTabular::M_ALIGN_RIGHT:
1297         case LyXTabular::ALIGN_RIGHT:
1298                 setAlign = LYX_ALIGN_RIGHT;
1299                 break;
1300
1301         case LyXTabular::M_ALIGN_CENTER:
1302         case LyXTabular::ALIGN_CENTER:
1303                 setAlign = LYX_ALIGN_CENTER;
1304                 break;
1305
1306         case LyXTabular::ALIGN_BLOCK:
1307                 setAlign = LYX_ALIGN_BLOCK;
1308                 break;
1309
1310         case LyXTabular::M_VALIGN_TOP:
1311         case LyXTabular::VALIGN_TOP:
1312                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1313                 break;
1314
1315         case LyXTabular::M_VALIGN_BOTTOM:
1316         case LyXTabular::VALIGN_BOTTOM:
1317                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1318                 break;
1319
1320         case LyXTabular::M_VALIGN_MIDDLE:
1321         case LyXTabular::VALIGN_MIDDLE:
1322                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1323                 break;
1324
1325         default:
1326                 break;
1327         }
1328
1329         if (hasSelection()) {
1330                 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1331         } else {
1332                 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1333                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1334         }
1335         recordUndo(bv, Undo::ATOMIC);
1336
1337         int row =  tabular.row_of_cell(actcell);
1338         int column = tabular.column_of_cell(actcell);
1339         bool flag = true;
1340         LyXTabular::ltType ltt;
1341
1342         switch (feature) {
1343
1344         case LyXTabular::SET_PWIDTH: {
1345                 LyXLength const len(value);
1346                 tabular.setColumnPWidth(actcell, len);
1347                 if (len.zero()
1348                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1349                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1350                 else if (!len.zero()
1351                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1352                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1353                 break;
1354         }
1355
1356         case LyXTabular::SET_MPWIDTH:
1357                 tabular.setMColumnPWidth(actcell, LyXLength(value));
1358                 break;
1359
1360         case LyXTabular::SET_SPECIAL_COLUMN:
1361         case LyXTabular::SET_SPECIAL_MULTI:
1362                 tabular.setAlignSpecial(actcell,value,feature);
1363                 break;
1364
1365         case LyXTabular::APPEND_ROW:
1366                 // append the row into the tabular
1367                 tabular.appendRow(bv.buffer()->params(), actcell);
1368                 tabular.setOwner(this);
1369                 break;
1370
1371         case LyXTabular::APPEND_COLUMN:
1372                 // append the column into the tabular
1373                 tabular.appendColumn(bv.buffer()->params(), actcell);
1374                 tabular.setOwner(this);
1375                 actcell = tabular.getCellNumber(row, column);
1376                 break;
1377
1378         case LyXTabular::DELETE_ROW:
1379                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1380                         tabular.deleteRow(sel_row_start);
1381                 if (sel_row_start >= tabular.rows())
1382                         --sel_row_start;
1383                 actcell = tabular.getCellNumber(sel_row_start, column);
1384                 clearSelection();
1385                 break;
1386
1387         case LyXTabular::DELETE_COLUMN:
1388                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1389                         tabular.deleteColumn(sel_col_start);
1390                 if (sel_col_start >= tabular.columns())
1391                         --sel_col_start;
1392                 actcell = tabular.getCellNumber(row, sel_col_start);
1393                 clearSelection();
1394                 break;
1395
1396         case LyXTabular::M_TOGGLE_LINE_TOP:
1397                 flag = false;
1398         case LyXTabular::TOGGLE_LINE_TOP: {
1399                 bool lineSet = !tabular.topLine(actcell, flag);
1400                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1401                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1402                                 tabular.setTopLine(
1403                                         tabular.getCellNumber(i, j),
1404                                         lineSet, flag);
1405                 break;
1406         }
1407
1408         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1409                 flag = false;
1410         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1411                 bool lineSet = !tabular.bottomLine(actcell, flag);
1412                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1413                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1414                                 tabular.setBottomLine(
1415                                         tabular.getCellNumber(i, j),
1416                                         lineSet,
1417                                         flag);
1418                 break;
1419         }
1420
1421         case LyXTabular::M_TOGGLE_LINE_LEFT:
1422                 flag = false;
1423         case LyXTabular::TOGGLE_LINE_LEFT: {
1424                 bool lineSet = !tabular.leftLine(actcell, flag);
1425                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1426                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1427                                 tabular.setLeftLine(
1428                                         tabular.getCellNumber(i,j),
1429                                         lineSet,
1430                                         flag);
1431                 break;
1432         }
1433
1434         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1435                 flag = false;
1436         case LyXTabular::TOGGLE_LINE_RIGHT: {
1437                 bool lineSet = !tabular.rightLine(actcell, flag);
1438                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1439                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1440                                 tabular.setRightLine(
1441                                         tabular.getCellNumber(i,j),
1442                                         lineSet,
1443                                         flag);
1444                 break;
1445         }
1446
1447         case LyXTabular::M_ALIGN_LEFT:
1448         case LyXTabular::M_ALIGN_RIGHT:
1449         case LyXTabular::M_ALIGN_CENTER:
1450                 flag = false;
1451         case LyXTabular::ALIGN_LEFT:
1452         case LyXTabular::ALIGN_RIGHT:
1453         case LyXTabular::ALIGN_CENTER:
1454         case LyXTabular::ALIGN_BLOCK:
1455                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1456                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1457                                 tabular.setAlignment(
1458                                         tabular.getCellNumber(i, j),
1459                                         setAlign,
1460                                         flag);
1461                 break;
1462
1463         case LyXTabular::M_VALIGN_TOP:
1464         case LyXTabular::M_VALIGN_BOTTOM:
1465         case LyXTabular::M_VALIGN_MIDDLE:
1466                 flag = false;
1467         case LyXTabular::VALIGN_TOP:
1468         case LyXTabular::VALIGN_BOTTOM:
1469         case LyXTabular::VALIGN_MIDDLE:
1470                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1471                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1472                                 tabular.setVAlignment(
1473                                         tabular.getCellNumber(i, j),
1474                                         setVAlign, flag);
1475                 break;
1476
1477         case LyXTabular::MULTICOLUMN: {
1478                 if (sel_row_start != sel_row_end) {
1479 #ifdef WITH_WARNINGS
1480 #warning Need I say it ? This is horrible.
1481 #endif
1482                         Alert::error(_("Error setting multicolumn"),
1483                                    _("You cannot set multicolumn vertically."));
1484                         return;
1485                 }
1486                 // just multicol for one Single Cell
1487                 if (!hasSelection()) {
1488                         // check wether we are completly in a multicol
1489                         if (tabular.isMultiColumn(actcell))
1490                                 tabular.unsetMultiColumn(actcell);
1491                         else
1492                                 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1493                         break;
1494                 }
1495                 // we have a selection so this means we just add all this
1496                 // cells to form a multicolumn cell
1497                 int s_start;
1498                 int s_end;
1499
1500                 if (sel_cell_start > sel_cell_end) {
1501                         s_start = sel_cell_end;
1502                         s_end = sel_cell_start;
1503                 } else {
1504                         s_start = sel_cell_start;
1505                         s_end = sel_cell_end;
1506                 }
1507                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1508                 actcell = s_start;
1509                 clearSelection();
1510                 break;
1511         }
1512
1513         case LyXTabular::SET_ALL_LINES:
1514                 setLines = true;
1515         case LyXTabular::UNSET_ALL_LINES:
1516                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1517                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1518                                 tabular.setAllLines(
1519                                         tabular.getCellNumber(i,j), setLines);
1520                 break;
1521
1522         case LyXTabular::SET_LONGTABULAR:
1523                 tabular.setLongTabular(true);
1524                 break;
1525
1526         case LyXTabular::UNSET_LONGTABULAR:
1527                 tabular.setLongTabular(false);
1528                 break;
1529
1530         case LyXTabular::SET_ROTATE_TABULAR:
1531                 tabular.setRotateTabular(true);
1532                 break;
1533
1534         case LyXTabular::UNSET_ROTATE_TABULAR:
1535                 tabular.setRotateTabular(false);
1536                 break;
1537
1538         case LyXTabular::SET_ROTATE_CELL:
1539                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1540                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1541                                 tabular.setRotateCell(
1542                                         tabular.getCellNumber(i, j), true);
1543                 break;
1544
1545         case LyXTabular::UNSET_ROTATE_CELL:
1546                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1547                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1548                                 tabular.setRotateCell(
1549                                         tabular.getCellNumber(i, j), false);
1550                 break;
1551
1552         case LyXTabular::SET_USEBOX: {
1553                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1554                 if (val == tabular.getUsebox(actcell))
1555                         val = LyXTabular::BOX_NONE;
1556                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1557                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1558                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1559                 break;
1560         }
1561
1562         case LyXTabular::UNSET_LTFIRSTHEAD:
1563                 flag = false;
1564         case LyXTabular::SET_LTFIRSTHEAD:
1565                 tabular.getRowOfLTFirstHead(row, ltt);
1566                 checkLongtableSpecial(ltt, value, flag);
1567                 tabular.setLTHead(row, flag, ltt, true);
1568                 break;
1569
1570         case LyXTabular::UNSET_LTHEAD:
1571                 flag = false;
1572         case LyXTabular::SET_LTHEAD:
1573                 tabular.getRowOfLTHead(row, ltt);
1574                 checkLongtableSpecial(ltt, value, flag);
1575                 tabular.setLTHead(row, flag, ltt, false);
1576                 break;
1577
1578         case LyXTabular::UNSET_LTFOOT:
1579                 flag = false;
1580         case LyXTabular::SET_LTFOOT:
1581                 tabular.getRowOfLTFoot(row, ltt);
1582                 checkLongtableSpecial(ltt, value, flag);
1583                 tabular.setLTFoot(row, flag, ltt, false);
1584                 break;
1585
1586         case LyXTabular::UNSET_LTLASTFOOT:
1587                 flag = false;
1588         case LyXTabular::SET_LTLASTFOOT:
1589                 tabular.getRowOfLTLastFoot(row, ltt);
1590                 checkLongtableSpecial(ltt, value, flag);
1591                 tabular.setLTFoot(row, flag, ltt, true);
1592                 break;
1593
1594         case LyXTabular::SET_LTNEWPAGE:
1595                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1596                 break;
1597
1598         // dummy stuff just to avoid warnings
1599         case LyXTabular::LAST_ACTION:
1600                 break;
1601         }
1602
1603         updateLocal(bv);
1604         InsetTabularMailer(*this).updateDialog(&bv);
1605 }
1606
1607
1608 void InsetTabular::activateCellInset(BufferView & bv, int cell, int x, int y)
1609 {
1610         tabular.getCellInset(cell).edit(&bv, x, y);
1611         bv.cursor().idx(cell);
1612         updateLocal(bv);
1613 }
1614
1615
1616 void InsetTabular::activateCellInset(BufferView & bv, int cell, bool behind)
1617 {
1618         tabular.getCellInset(cell).edit(&bv, behind);
1619         bv.cursor().idx(cell);
1620         updateLocal(bv);
1621 }
1622
1623
1624 bool InsetTabular::showInsetDialog(BufferView * bv) const
1625 {
1626         InsetTabularMailer(*this).showDialog(bv);
1627         return true;
1628 }
1629
1630
1631 void InsetTabular::openLayoutDialog(BufferView * bv) const
1632 {
1633         InsetTabularMailer(*this).showDialog(bv);
1634 }
1635
1636
1637 //
1638 // function returns an object as defined in func_status.h:
1639 // states OK, Unknown, Disabled, On, Off.
1640 //
1641 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1642 {
1643         FuncStatus status;
1644         int action = LyXTabular::LAST_ACTION;
1645
1646         int i = 0;
1647         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1648                 string const tmp = tabularFeature[i].feature;
1649                 if (tmp == what.substr(0, tmp.length())) {
1650                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1651                         //   tabularFeatures[i].feature.length())) 
1652                         action = tabularFeature[i].action;
1653                         break;
1654                 }
1655         }
1656         if (action == LyXTabular::LAST_ACTION) {
1657                 status.clear();
1658                 status.unknown(true);
1659                 return status;
1660         }
1661
1662         string const argument
1663                 = ltrim(what.substr(tabularFeature[i].feature.length()));
1664
1665         int sel_row_start;
1666         int sel_row_end;
1667         int dummy;
1668         LyXTabular::ltType dummyltt;
1669         bool flag = true;
1670
1671         if (hasSelection())
1672                 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1673         else
1674                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1675
1676         switch (action) {
1677         case LyXTabular::SET_PWIDTH:
1678         case LyXTabular::SET_MPWIDTH:
1679         case LyXTabular::SET_SPECIAL_COLUMN:
1680         case LyXTabular::SET_SPECIAL_MULTI:
1681         case LyXTabular::APPEND_ROW:
1682         case LyXTabular::APPEND_COLUMN:
1683         case LyXTabular::DELETE_ROW:
1684         case LyXTabular::DELETE_COLUMN:
1685         case LyXTabular::SET_ALL_LINES:
1686         case LyXTabular::UNSET_ALL_LINES:
1687                 status.clear();
1688                 return status;
1689
1690         case LyXTabular::MULTICOLUMN:
1691                 status.setOnOff(tabular.isMultiColumn(actcell));
1692                 break;
1693
1694         case LyXTabular::M_TOGGLE_LINE_TOP:
1695                 flag = false;
1696         case LyXTabular::TOGGLE_LINE_TOP:
1697                 status.setOnOff(tabular.topLine(actcell, flag));
1698                 break;
1699
1700         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1701                 flag = false;
1702         case LyXTabular::TOGGLE_LINE_BOTTOM:
1703                 status.setOnOff(tabular.bottomLine(actcell, flag));
1704                 break;
1705
1706         case LyXTabular::M_TOGGLE_LINE_LEFT:
1707                 flag = false;
1708         case LyXTabular::TOGGLE_LINE_LEFT:
1709                 status.setOnOff(tabular.leftLine(actcell, flag));
1710                 break;
1711
1712         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1713                 flag = false;
1714         case LyXTabular::TOGGLE_LINE_RIGHT:
1715                 status.setOnOff(tabular.rightLine(actcell, flag));
1716                 break;
1717
1718         case LyXTabular::M_ALIGN_LEFT:
1719                 flag = false;
1720         case LyXTabular::ALIGN_LEFT:
1721                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1722                 break;
1723
1724         case LyXTabular::M_ALIGN_RIGHT:
1725                 flag = false;
1726         case LyXTabular::ALIGN_RIGHT:
1727                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1728                 break;
1729
1730         case LyXTabular::M_ALIGN_CENTER:
1731                 flag = false;
1732         case LyXTabular::ALIGN_CENTER:
1733                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1734                 break;
1735
1736         case LyXTabular::ALIGN_BLOCK:
1737                 status.disabled(tabular.getPWidth(actcell).zero());
1738                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1739                 break;
1740
1741         case LyXTabular::M_VALIGN_TOP:
1742                 flag = false;
1743         case LyXTabular::VALIGN_TOP:
1744                 status.setOnOff(
1745                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1746                 break;
1747
1748         case LyXTabular::M_VALIGN_BOTTOM:
1749                 flag = false;
1750         case LyXTabular::VALIGN_BOTTOM:
1751                 status.setOnOff(
1752                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1753                 break;
1754
1755         case LyXTabular::M_VALIGN_MIDDLE:
1756                 flag = false;
1757         case LyXTabular::VALIGN_MIDDLE:
1758                 status.setOnOff(
1759                         tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1760                 break;
1761
1762         case LyXTabular::SET_LONGTABULAR:
1763                 status.setOnOff(tabular.isLongTabular());
1764                 break;
1765
1766         case LyXTabular::UNSET_LONGTABULAR:
1767                 status.setOnOff(!tabular.isLongTabular());
1768                 break;
1769
1770         case LyXTabular::SET_ROTATE_TABULAR:
1771                 status.setOnOff(tabular.getRotateTabular());
1772                 break;
1773
1774         case LyXTabular::UNSET_ROTATE_TABULAR:
1775                 status.setOnOff(!tabular.getRotateTabular());
1776                 break;
1777
1778         case LyXTabular::SET_ROTATE_CELL:
1779                 status.setOnOff(tabular.getRotateCell(actcell));
1780                 break;
1781
1782         case LyXTabular::UNSET_ROTATE_CELL:
1783                 status.setOnOff(!tabular.getRotateCell(actcell));
1784                 break;
1785
1786         case LyXTabular::SET_USEBOX:
1787                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1788                 break;
1789
1790         case LyXTabular::SET_LTFIRSTHEAD:
1791                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1792                 break;
1793
1794         case LyXTabular::SET_LTHEAD:
1795                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1796                 break;
1797
1798         case LyXTabular::SET_LTFOOT:
1799                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1800                 break;
1801
1802         case LyXTabular::SET_LTLASTFOOT:
1803                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1804                 break;
1805
1806         case LyXTabular::SET_LTNEWPAGE:
1807                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1808                 break;
1809
1810         default:
1811                 status.clear();
1812                 status.disabled(true);
1813                 break;
1814         }
1815         return status;
1816 }
1817
1818
1819 void InsetTabular::getLabelList(Buffer const & buffer,
1820                                 std::vector<string> & list) const
1821 {
1822         tabular.getLabelList(buffer, list);
1823 }
1824
1825
1826 bool InsetTabular::copySelection(BufferView & bv)
1827 {
1828         if (!hasSelection())
1829                 return false;
1830
1831         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1832         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1833         if (sel_col_start > sel_col_end) {
1834                 sel_col_start = sel_col_end;
1835                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1836         } else {
1837                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1838         }
1839
1840         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1841         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1842         if (sel_row_start > sel_row_end)
1843                 swap(sel_row_start, sel_row_end);
1844
1845         paste_tabular.reset(new LyXTabular(tabular));
1846         paste_tabular->setOwner(this);
1847
1848         for (int i = 0; i < sel_row_start; ++i)
1849                 paste_tabular->deleteRow(0);
1850
1851         int const rows = sel_row_end - sel_row_start + 1;
1852         while (paste_tabular->rows() > rows)
1853                 paste_tabular->deleteRow(rows);
1854
1855         paste_tabular->setTopLine(0, true, true);
1856         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1857                                      true, true);
1858
1859         for (int i = 0; i < sel_col_start; ++i)
1860                 paste_tabular->deleteColumn(0);
1861
1862         int const columns = sel_col_end - sel_col_start + 1;
1863         while (paste_tabular->columns() > columns)
1864                 paste_tabular->deleteColumn(columns);
1865
1866         paste_tabular->setLeftLine(0, true, true);
1867         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1868                                     true, true);
1869
1870         ostringstream os;
1871         OutputParams const runparams;
1872         paste_tabular->plaintext(*bv.buffer(), os, runparams,
1873                                  ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1874         bv.stuffClipboard(os.str());
1875         return true;
1876 }
1877
1878
1879 bool InsetTabular::pasteSelection(BufferView & bv)
1880 {
1881         if (!paste_tabular)
1882                 return false;
1883         int actcell = bv.cursor().idx();
1884         int actcol = tabular.column_of_cell(actcell);
1885         int actrow = tabular.row_of_cell(actcell);
1886         for (int r1 = 0, r2 = actrow;
1887              r1 < paste_tabular->rows() && r2 < tabular.rows();
1888              ++r1, ++r2) {
1889                 for (int c1 = 0, c2 = actcol;
1890                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1891                     ++c1, ++c2) {
1892                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1893                             tabular.isPartOfMultiColumn(r2, c2))
1894                                 continue;
1895                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1896                                 --c2;
1897                                 continue;
1898                         }
1899                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1900                                 --c1;
1901                                 continue;
1902                         }
1903                         InsetText & inset = tabular.getCellInset(r2, c2);
1904                         inset = paste_tabular->getCellInset(r1, c1);
1905                         inset.setOwner(this);
1906                         inset.markNew();
1907                 }
1908         }
1909         return true;
1910 }
1911
1912
1913 bool InsetTabular::cutSelection(BufferParams const & bp)
1914 {
1915         if (!hasSelection())
1916                 return false;
1917
1918         int sel_col_start = tabular.column_of_cell(sel_cell_start);
1919         int sel_col_end = tabular.column_of_cell(sel_cell_end);
1920         if (sel_col_start > sel_col_end) {
1921                 sel_col_start = sel_col_end;
1922                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1923         } else {
1924                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1925         }
1926
1927         int sel_row_start = tabular.row_of_cell(sel_cell_start);
1928         int sel_row_end = tabular.row_of_cell(sel_cell_end);
1929
1930         if (sel_row_start > sel_row_end)
1931                 swap(sel_row_start, sel_row_end);
1932
1933         if (sel_cell_start > sel_cell_end)
1934                 swap(sel_cell_start, sel_cell_end);
1935
1936         for (int i = sel_row_start; i <= sel_row_end; ++i)
1937                 for (int j = sel_col_start; j <= sel_col_end; ++j)
1938                         tabular.getCellInset(tabular.getCellNumber(i, j))
1939                                 .clear(bp.tracking_changes);
1940         return true;
1941 }
1942
1943
1944 bool InsetTabular::isRightToLeft(BufferView & bv)
1945 {
1946         return bv.getParentLanguage(this)->RightToLeft();
1947 }
1948
1949
1950 void InsetTabular::getSelection(int actcell,
1951         int & srow, int & erow, int & scol, int & ecol) const
1952 {
1953         int const start = hasSelection() ? sel_cell_start : actcell;
1954         int const end = hasSelection() ? sel_cell_end : actcell;
1955
1956         srow = tabular.row_of_cell(start);
1957         erow = tabular.row_of_cell(end);
1958         if (srow > erow)
1959                 swap(srow, erow);
1960
1961         scol = tabular.column_of_cell(start);
1962         ecol = tabular.column_of_cell(end);
1963         if (scol > ecol)
1964                 swap(scol, ecol);
1965         else
1966                 ecol = tabular.right_column_of_cell(end);
1967 }
1968
1969
1970 int InsetTabular::numParagraphs() const
1971 {
1972         return tabular.getNumberOfCells();
1973 }
1974
1975
1976 LyXText * InsetTabular::getText(int i) const
1977 {
1978         return i < tabular.getNumberOfCells()
1979                 ? tabular.getCellInset(i).getText(0)
1980                 : 0;
1981 }
1982
1983
1984 void InsetTabular::markErased()
1985 {
1986         for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1987                 tabular.getCellInset(cell).markErased();
1988 }
1989
1990
1991 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1992 {
1993         const int cell = tabular.getCellFromInset(in);
1994
1995         if (cell != -1)
1996                 return tabular.getPWidth(cell).zero();
1997
1998         // this is a workaround for a crash (New, Insert->Tabular,
1999         // Insert->FootNote)
2000         if (!owner())
2001                 return false;
2002
2003         // well we didn't obviously find it so maybe our owner knows more
2004         BOOST_ASSERT(owner());
2005         return owner()->forceDefaultParagraphs(in);
2006 }
2007
2008
2009 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2010                                      bool usePaste)
2011 {
2012         if (buf.length() <= 0)
2013                 return true;
2014
2015         int cols = 1;
2016         int rows = 1;
2017         int maxCols = 1;
2018         string::size_type len = buf.length();
2019         string::size_type p = 0;
2020
2021         int actcell = bv.cursor().idx();
2022         int actcol = tabular.column_of_cell(actcell);
2023         int actrow = tabular.row_of_cell(actcell);
2024
2025         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2026                 switch (buf[p]) {
2027                 case '\t':
2028                         ++cols;
2029                         break;
2030                 case '\n':
2031                         if (p + 1 < len)
2032                                 ++rows;
2033                         maxCols = max(cols, maxCols);
2034                         cols = 1;
2035                         break;
2036                 }
2037                 ++p;
2038         }
2039         maxCols = max(cols, maxCols);
2040         LyXTabular * loctab;
2041         int cell = 0;
2042         int ocol = 0;
2043         int row = 0;
2044         if (usePaste) {
2045                 paste_tabular.reset(
2046                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
2047                 paste_tabular->setOwner(this);
2048                 loctab = paste_tabular.get();
2049                 cols = 0;
2050         } else {
2051                 loctab = &tabular;
2052                 cell = actcell;
2053                 ocol = actcol;
2054                 row = actrow;
2055         }
2056
2057         string::size_type op = 0;
2058         int cells = loctab->getNumberOfCells();
2059         p = 0;
2060         cols = ocol;
2061         rows = loctab->rows();
2062         int const columns = loctab->columns();
2063
2064         while (cell < cells && p < len && row < rows &&
2065                (p = buf.find_first_of("\t\n", p)) != string::npos)
2066         {
2067                 if (p >= len)
2068                         break;
2069                 switch (buf[p]) {
2070                 case '\t':
2071                         // we can only set this if we are not too far right
2072                         if (cols < columns) {
2073                                 InsetText & inset = loctab->getCellInset(cell);
2074                                 LyXFont const font = inset.text_.
2075                                         getFont(inset.paragraphs().begin(), 0);
2076                                 inset.setText(buf.substr(op, p - op), font);
2077                                 ++cols;
2078                                 ++cell;
2079                         }
2080                         break;
2081                 case '\n':
2082                         // we can only set this if we are not too far right
2083                         if (cols < columns) {
2084                                 InsetText & inset = tabular.getCellInset(cell);
2085                                 LyXFont const font = inset.text_.
2086                                         getFont(inset.paragraphs().begin(), 0);
2087                                 inset.setText(buf.substr(op, p - op), font);
2088                         }
2089                         cols = ocol;
2090                         ++row;
2091                         if (row < rows)
2092                                 cell = loctab->getCellNumber(row, cols);
2093                         break;
2094                 }
2095                 ++p;
2096                 op = p;
2097         }
2098         // check for the last cell if there is no trailing '\n'
2099         if (cell < cells && op < len) {
2100                 InsetText & inset = loctab->getCellInset(cell);
2101                 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2102                 inset.setText(buf.substr(op, len - op), font);
2103         }
2104         return true;
2105 }
2106
2107
2108 void InsetTabular::addPreview(PreviewLoader & loader) const
2109 {
2110         int const rows = tabular.rows();
2111         int const columns = tabular.columns();
2112         for (int i = 0; i < rows; ++i) {
2113                 for (int j = 0; j < columns; ++j)
2114                         tabular.getCellInset(i, j).addPreview(loader);
2115         }
2116 }
2117
2118
2119
2120 void InsetTabular::clearSelection() const
2121 {
2122         sel_cell_start = 0;
2123         sel_cell_end = 0;
2124         has_selection = false;
2125 }
2126
2127
2128 void InsetTabular::setSelection(int start, int end) const
2129 {
2130         sel_cell_start = start;
2131         sel_cell_end = end;
2132         has_selection = true;
2133 }
2134
2135
2136 string const InsetTabularMailer::name_("tabular");
2137
2138 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2139         : inset_(const_cast<InsetTabular &>(inset))
2140 {}
2141
2142
2143 string const InsetTabularMailer::inset2string(Buffer const &) const
2144 {
2145         return params2string(inset_);
2146 }
2147
2148
2149 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2150 {
2151         istringstream data(in);
2152         LyXLex lex(0,0);
2153         lex.setStream(data);
2154
2155 #warning CHECK verify that this is a sane value to return.
2156         if (in.empty())
2157                 return -1;
2158
2159         if (lex.isOK()) {
2160                 lex.next();
2161                 string const token = lex.getString();
2162                 if (token != name_)
2163                         return -1;
2164         }
2165
2166         int cell = -1;
2167         if (lex.isOK()) {
2168                 lex.next();
2169                 string const token = lex.getString();
2170                 if (token != "\\active_cell")
2171                         return -1;
2172                 lex.next();
2173                 cell = lex.getInteger();
2174         }
2175
2176         // This is part of the inset proper that is usually swallowed
2177         // by Buffer::readInset
2178         if (lex.isOK()) {
2179                 lex.next();
2180                 string const token = lex.getString();
2181                 if (token != "Tabular")
2182                         return -1;
2183         }
2184
2185         if (!lex.isOK())
2186                 return -1;
2187
2188         Buffer const & buffer = inset.buffer();
2189         inset.read(buffer, lex);
2190
2191         // We can't set the active cell, but we can tell the frontend
2192         // what it is.
2193         return cell;
2194 }
2195
2196
2197 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2198 {
2199         ostringstream data;
2200 #warning wrong!
2201         //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2202         data << name_ << " \\active_cell " << 0 << '\n';
2203         inset.write(inset.buffer(), data);
2204         data << "\\end_inset\n";
2205         return data.str();
2206 }