]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
cursor changes
[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 "debug.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "latexrunparams.h"
25 #include "LColor.h"
26 #include "lyx_cb.h"
27 #include "lyxlex.h"
28 #include "metricsinfo.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
32 #include "undo.h"
33
34 #include "frontends/Alert.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Painter.h"
38
39 #include "support/std_sstream.h"
40 #include <iostream>
41
42 using lyx::graphics::PreviewLoader;
43
44 using lyx::support::ltrim;
45 using lyx::support::strToInt;
46
47 using std::endl;
48 using std::max;
49 using std::swap;
50 using std::string;
51 using std::auto_ptr;
52 using std::istringstream;
53 using std::ostream;
54 using std::ostringstream;
55
56
57 namespace {
58
59 int const ADD_TO_HEIGHT = 2;
60 int const ADD_TO_TABULAR_WIDTH = 2;
61
62 ///
63 boost::scoped_ptr<LyXTabular> paste_tabular;
64
65
66 struct TabularFeature {
67         LyXTabular::Feature action;
68         string feature;
69 };
70
71
72 TabularFeature tabularFeature[] =
73 {
74         { LyXTabular::APPEND_ROW, "append-row" },
75         { LyXTabular::APPEND_COLUMN, "append-column" },
76         { LyXTabular::DELETE_ROW, "delete-row" },
77         { LyXTabular::DELETE_COLUMN, "delete-column" },
78         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
79         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
80         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
81         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
82         { LyXTabular::ALIGN_LEFT, "align-left" },
83         { LyXTabular::ALIGN_RIGHT, "align-right" },
84         { LyXTabular::ALIGN_CENTER, "align-center" },
85         { LyXTabular::ALIGN_BLOCK, "align-block" },
86         { LyXTabular::VALIGN_TOP, "valign-top" },
87         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
88         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
89         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
90         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
91         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
92         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
93         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
94         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
95         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
96         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
97         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
98         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
99         { LyXTabular::MULTICOLUMN, "multicolumn" },
100         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
101         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
102         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
103         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
104         { LyXTabular::SET_PWIDTH, "set-pwidth" },
105         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
106         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
107         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
108         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
109         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
110         { LyXTabular::SET_USEBOX, "set-usebox" },
111         { LyXTabular::SET_LTHEAD, "set-lthead" },
112         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
113         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
114         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
115         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
116         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
117         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
118         { LyXTabular::LAST_ACTION, "" }
119 };
120
121 struct FindFeature {
122         FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
123         bool operator()(TabularFeature & tf)
124         {
125                 return tf.action == feature_;
126         }
127 private:
128         LyXTabular::Feature feature_;
129 };
130
131 } // namespace anon
132
133
134 string const featureAsString(LyXTabular::Feature feature)
135 {
136         TabularFeature * it  = tabularFeature;
137         TabularFeature * end = it +
138                 sizeof(tabularFeature) / sizeof(TabularFeature);
139         it = std::find_if(it, end, FindFeature(feature));
140         return (it == end) ? string() : it->feature;
141 }
142
143
144 bool InsetTabular::hasPasteBuffer() const
145 {
146         return (paste_tabular.get() != 0);
147 }
148
149
150 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
151         : tabular(buf.params(), max(rows, 1), max(columns, 1)),
152           buffer_(&buf), cursorx_(0), cursory_(0)
153 {
154         tabular.setOwner(this);
155         // for now make it always display as display() inset
156         // just for test!!!
157         the_locking_inset = 0;
158         old_locking_inset = 0;
159         locked = false;
160         oldcell = -1;
161         actrow = actcell = 0;
162         clearSelection();
163         in_reset_pos = 0;
164         inset_x = 0;
165         inset_y = 0;
166 }
167
168
169 InsetTabular::InsetTabular(InsetTabular const & tab)
170         : UpdatableInset(tab), tabular(tab.tabular),
171                 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
172 {
173         tabular.setOwner(this);
174         the_locking_inset = 0;
175         old_locking_inset = 0;
176         locked = false;
177         oldcell = -1;
178         actrow = actcell = 0;
179         clearSelection();
180         in_reset_pos = 0;
181         inset_x = 0;
182         inset_y = 0;
183 }
184
185
186 InsetTabular::~InsetTabular()
187 {
188         InsetTabularMailer(*this).hideDialog();
189 }
190
191
192 auto_ptr<InsetBase> InsetTabular::clone() const
193 {
194         return auto_ptr<InsetBase>(new InsetTabular(*this));
195 }
196
197
198 Buffer const & InsetTabular::buffer() const
199 {
200         return *buffer_;
201 }
202
203
204 BufferView * InsetTabular::view() const
205 {
206         BOOST_ASSERT(false);
207         return 0;
208 }
209
210
211 void InsetTabular::buffer(Buffer * b)
212 {
213         buffer_ = b;
214 }
215
216
217 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 {
219         os << "Tabular" << endl;
220         tabular.write(buf, os);
221 }
222
223
224 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 {
226         bool const old_format = (lex.getString() == "\\LyXTable");
227
228         tabular.read(buf, lex);
229
230         if (old_format)
231                 return;
232
233         lex.nextToken();
234         string token = lex.getString();
235         while (lex.isOK() && (token != "\\end_inset")) {
236                 lex.nextToken();
237                 token = lex.getString();
238         }
239         if (token != "\\end_inset") {
240                 lex.printError("Missing \\end_inset at this point. "
241                                "Read: `$$Token'");
242         }
243 }
244
245
246 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 {
248         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
249         //      mi.base.textwidth << "\n";
250         if (!mi.base.bv) {
251                 lyxerr << "InsetTabular::metrics: need bv" << endl;
252                 BOOST_ASSERT(false);
253         }
254
255         calculate_dimensions_of_cells(mi);
256
257         dim.asc = tabular.getAscentOfRow(0);
258         dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
259         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
260         dim_ = dim;
261 }
262
263
264 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
265 {
266         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
267
268         BufferView * bv = pi.base.bv;
269
270 #if 0
271         UpdatableInset::draw(pi, x, y);
272 #else
273         if (!owner())
274                 x += scroll();
275 #endif
276
277         top_x = x;
278         top_baseline = y;
279         x += ADD_TO_TABULAR_WIDTH;
280
281         int cell = 0;
282         first_visible_cell = -1;
283         for (int i = 0; i < tabular.rows(); ++i) {
284                 int nx = x;
285                 cell = tabular.getCellNumber(i, 0);
286                 if (y + tabular.getDescentOfRow(i) <= 0 &&
287                           y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
288                 {
289                         y += tabular.getDescentOfRow(i) +
290                                         tabular.getAscentOfRow(i + 1) +
291                                         tabular.getAdditionalHeight(i + 1);
292                         continue;
293                 }
294                 for (int j = 0; j < tabular.columns(); ++j) {
295                         if (nx > bv->workWidth())
296                                 break;
297                         if (tabular.isPartOfMultiColumn(i, j))
298                                 continue;
299                         int cx = nx + tabular.getBeginningOfTextInCell(cell);
300                         if (first_visible_cell < 0)
301                                 first_visible_cell = cell;
302                         if (hasSelection()) {
303                                 drawCellSelection(pi.pain, nx, y, i, j, cell);
304                         }
305
306                         tabular.getCellInset(cell).draw(pi, cx, y);
307                         drawCellLines(pi.pain, nx, y, i, cell);
308                         nx += tabular.getWidthOfColumn(cell);
309                         ++cell;
310                 }
311
312 // Would be nice, but for some completely unfathomable reason,
313 // on a col resize to a new fixed width, even though the insettexts
314 // are resized, the cell isn't, but drawing all cells in a tall table
315 // has the desired effect somehow. Complete dark magic.
316 #if 0
317                 // avoiding drawing the rest of a long table is
318                 // a pretty big speedup
319                 if (y > bv->workHeight())
320                         break;
321 #endif
322
323                 y += tabular.getDescentOfRow(i) +
324                         tabular.getAscentOfRow(i + 1) +
325                         tabular.getAdditionalHeight(i + 1);
326         }
327 }
328
329
330 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
331                                  int row, int cell) const
332 {
333         int x2 = x + tabular.getWidthOfColumn(cell);
334         bool on_off;
335
336         if (!tabular.topAlreadyDrawn(cell)) {
337                 on_off = !tabular.topLine(cell);
338                 pain.line(x, y - tabular.getAscentOfRow(row),
339                           x2, y -  tabular.getAscentOfRow(row),
340                           on_off ? LColor::tabularonoffline : LColor::tabularline,
341                           on_off ? Painter::line_onoffdash : Painter::line_solid);
342         }
343         on_off = !tabular.bottomLine(cell);
344         pain.line(x, y + tabular.getDescentOfRow(row),
345                   x2, y + tabular.getDescentOfRow(row),
346                   on_off ? LColor::tabularonoffline : LColor::tabularline,
347                   on_off ? Painter::line_onoffdash : Painter::line_solid);
348         if (!tabular.leftAlreadyDrawn(cell)) {
349                 on_off = !tabular.leftLine(cell);
350                 pain.line(x, y -  tabular.getAscentOfRow(row),
351                           x, y +  tabular.getDescentOfRow(row),
352                           on_off ? LColor::tabularonoffline : LColor::tabularline,
353                           on_off ? Painter::line_onoffdash : Painter::line_solid);
354         }
355         on_off = !tabular.rightLine(cell);
356         pain.line(x2 - tabular.getAdditionalWidth(cell),
357                   y -  tabular.getAscentOfRow(row),
358                   x2 - tabular.getAdditionalWidth(cell),
359                   y +  tabular.getDescentOfRow(row),
360                   on_off ? LColor::tabularonoffline : LColor::tabularline,
361                   on_off ? Painter::line_onoffdash : Painter::line_solid);
362 }
363
364
365 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
366                                      int row, int column, int cell) const
367 {
368         BOOST_ASSERT(hasSelection());
369         int cs = tabular.column_of_cell(sel_cell_start);
370         int ce = tabular.column_of_cell(sel_cell_end);
371         if (cs > ce) {
372                 ce = cs;
373                 cs = tabular.column_of_cell(sel_cell_end);
374         } else {
375                 ce = tabular.right_column_of_cell(sel_cell_end);
376         }
377
378         int rs = tabular.row_of_cell(sel_cell_start);
379         int re = tabular.row_of_cell(sel_cell_end);
380         if (rs > re)
381                 swap(rs, re);
382
383         if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
384                 int w = tabular.getWidthOfColumn(cell);
385                 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
386                 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
387                                    w, h, LColor::selection);
388         }
389 }
390
391
392 string const InsetTabular::editMessage() const
393 {
394         return _("Opened table");
395 }
396
397
398 void InsetTabular::insetUnlock(BufferView * bv)
399 {
400         if (the_locking_inset) {
401                 the_locking_inset->insetUnlock(bv);
402                 updateLocal(bv);
403                 the_locking_inset = 0;
404         }
405         actcell = 0;
406         oldcell = -1;
407         locked = false;
408         if (scroll(false) || hasSelection()) {
409                 clearSelection();
410                 if (scroll(false))
411                         scroll(bv, 0.0F);
412                 updateLocal(bv);
413         }
414 }
415
416
417 void InsetTabular::updateLocal(BufferView * bv) const
418 {
419         bv->updateInset(this);
420         if (locked)
421                 resetPos(bv);
422 }
423
424
425 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
426 {
427         lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
428                               << inset << "): ";
429         if (!inset)
430                 return false;
431
432         oldcell = -1;
433
434         if (inset == &tabular.getCellInset(actcell)) {
435                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
436                 the_locking_inset = &tabular.getCellInset(actcell);
437                 resetPos(bv);
438                 return true;
439         }
440
441         if (!the_locking_inset) {
442                 int const n = tabular.getNumberOfCells();
443                 for (int i = 0; i < n; ++i) {
444                         InsetText * in = &tabular.getCellInset(i);
445                         if (inset == in) {
446                                 actcell = i;
447                                 the_locking_inset = in;
448                                 locked = true;
449                                 resetPos(bv);
450                                 return true;
451                         }
452                         if (in->lockInsetInInset(bv, inset)) {
453                                 actcell = i;
454                                 the_locking_inset = in;
455                                 locked = true;
456                                 resetPos(bv);
457                                 in->edit(bv, true);
458                                 return true;
459                         }
460                 }
461                 return false;
462         }
463
464         if (the_locking_inset && (the_locking_inset == inset)) {
465                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
466                 resetPos(bv);
467                 return false;
468         }
469
470         if (the_locking_inset) {
471                 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
472                 return the_locking_inset->lockInsetInInset(bv, inset);
473         }
474
475         lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
476         return false;
477 }
478
479
480 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
481                                       bool lr)
482 {
483         if (!the_locking_inset)
484                 return false;
485         if (the_locking_inset == inset) {
486                 the_locking_inset->insetUnlock(bv);
487 #ifdef WITH_WARNINGS
488 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
489 #endif
490 #if 0
491                 if (scroll(false))
492                         scroll(bv, 0.0F);
493 #endif
494                 updateLocal(bv);
495                 // this has to be here otherwise we don't redraw the cell!
496                 the_locking_inset = 0;
497                 return true;
498         }
499         if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
500                 if (inset->lyxCode() == TABULAR_CODE &&
501                     !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
502                         InsetTabularMailer(*this).updateDialog(bv);
503                         oldcell = actcell;
504                 }
505                 return true;
506         }
507         return false;
508 }
509
510
511 int InsetTabular::insetInInsetY() const
512 {
513         if (!the_locking_inset)
514                 return 0;
515         return inset_y + the_locking_inset->insetInInsetY();
516 }
517
518
519 UpdatableInset * InsetTabular::getLockingInset() const
520 {
521         return the_locking_inset ? the_locking_inset->getLockingInset() :
522                 const_cast<InsetTabular *>(this);
523 }
524
525
526 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
527 {
528         if (c == lyxCode())
529                 return this;
530         if (the_locking_inset)
531                 return the_locking_inset->getFirstLockingInsetOfType(c);
532         return 0;
533 }
534
535
536 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
537 {
538         if (the_locking_inset)
539                 return the_locking_inset->insertInset(bv, inset);
540         return false;
541 }
542
543
544 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
545 {
546         if (hasSelection() && cmd.button() == mouse_button::button3)
547                 return;
548
549         if (hasSelection()) {
550                 clearSelection();
551                 updateLocal(cmd.view());
552         }
553
554         int const ocell = actcell;
555         BufferView * bv = cmd.view();
556
557         if (!locked) {
558                 locked = true;
559                 the_locking_inset = 0;
560                 inset_x = 0;
561                 inset_y = 0;
562         }
563         setPos(bv, cmd.x, cmd.y);
564         clearSelection();
565
566         bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
567
568         if (ocell == actcell && the_locking_inset && inset_hit) {
569                 resetPos(bv);
570                 FuncRequest cmd1 = cmd;
571                 cmd1.x -= inset_x;
572                 cmd1.y -= inset_y;
573                 the_locking_inset->dispatch(cmd1);
574                 return;
575         }
576
577         if (the_locking_inset) {
578                 the_locking_inset->insetUnlock(bv);
579                 updateLocal(bv);
580                 the_locking_inset = 0;
581         }
582
583         if (cmd.button() == mouse_button::button2) {
584                 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
585                 return;
586         }
587
588         if (inset_hit && bv->theLockingInset()) {
589                 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
590                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
591                         return;
592                 }
593                 FuncRequest cmd1 = cmd;
594                 cmd1.x -= inset_x;
595                 cmd1.y -= inset_y;
596                 the_locking_inset->dispatch(cmd1);
597         }
598 }
599
600
601 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
602 {
603         DispatchResult ret(false);
604         if (the_locking_inset) {
605                 FuncRequest cmd1 = cmd;
606                 cmd1.x -= inset_x;
607                 cmd1.y -= inset_y;
608                 ret = the_locking_inset->dispatch(cmd1);
609         }
610         if (cmd.button() == mouse_button::button3 && ret == DispatchResult(false)) {
611                 InsetTabularMailer(*this).showDialog(cmd.view());
612                 return true;
613         }
614         return ret.dispatched() || ret.val() > FINISHED;
615 }
616
617
618 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
619 {
620         if (the_locking_inset) {
621                 FuncRequest cmd1 = cmd;
622                 cmd1.x -= inset_x;
623                 cmd1.y -= inset_y;
624                 the_locking_inset->dispatch(cmd1);
625                 return;
626         }
627
628         BufferView * bv = cmd.view();
629         int const old_cell = actcell;
630
631         setPos(bv, cmd.x, cmd.y);
632         if (!hasSelection()) {
633                 setSelection(actcell, actcell);
634                 updateLocal(bv);
635         } else if (old_cell != actcell) {
636                 setSelection(sel_cell_start, actcell);
637                 updateLocal(bv);
638         }
639 }
640
641
642 void InsetTabular::edit(BufferView * bv, bool left)
643 {
644         lyxerr << "InsetTabular::edit: " << this << " first cell: "
645                 << &tabular.cell_info[0][0].inset << endl;
646
647         if (!bv->lockInset(this)) {
648                 lyxerr << "InsetTabular::Cannot lock inset" << endl;
649                 return;
650         }
651
652         finishUndo();
653         locked = true;
654         the_locking_inset = 0;
655         inset_x = 0;
656         inset_y = 0;
657
658         if (left) {
659                 if (isRightToLeft(bv))
660                         actcell = tabular.getLastCellInRow(0);
661                 else
662                         actcell = 0;
663         } else {
664                 if (isRightToLeft(bv))
665                         actcell = tabular.getFirstCellInRow(tabular.rows()-1);
666                 else
667                         actcell = tabular.getNumberOfCells() - 1;
668         }
669         clearSelection();
670         resetPos(bv);
671         bv->fitCursor();
672 }
673
674
675 void InsetTabular::edit(BufferView * bv, int x, int y)
676 {
677         lyxerr << "InsetTabular::edit: " << this << " first cell "
678                 << &tabular.cell_info[0][0].inset << endl;
679
680         if (!bv->lockInset(this)) {
681                 lyxerr << "InsetTabular::Cannot lock inset" << endl;
682                 return;
683         }
684
685         finishUndo();
686         locked = true;
687         the_locking_inset = 0;
688         inset_x = 0;
689         inset_y = 0;
690
691         setPos(bv, x, y);
692         clearSelection();
693         finishUndo();
694         if (insetHit(bv, x, y)) {
695                 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
696                 inset_y = cursory_;
697                 activateCellInset(bv, x - inset_x, y - inset_y);
698         }
699 }
700
701
702 DispatchResult
703 InsetTabular::priv_dispatch(FuncRequest const & cmd,
704                             idx_type & idx, pos_type & pos)
705 {
706         // We need to save the value of the_locking_inset as the call to
707         // the_locking_inset->localDispatch might unlock it.
708         old_locking_inset = the_locking_inset;
709         DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
710         BufferView * bv = cmd.view();
711
712         if (result.dispatched()) {
713                 resetPos(bv);
714                 return result;
715         }
716
717         if (cmd.action < 0 && cmd.argument.empty())
718                 return DispatchResult(false, FINISHED);
719
720         bool hs = hasSelection();
721
722         result = DispatchResult(true, true);
723         // this one have priority over the locked InsetText, if we're not already
724         // inside another tabular then that one get's priority!
725         if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
726                 switch (cmd.action) {
727                 case LFUN_MOUSE_PRESS:
728                         lfunMousePress(cmd);
729                         return DispatchResult(true, true);
730
731                 case LFUN_MOUSE_MOTION:
732                         lfunMouseMotion(cmd);
733                         return DispatchResult(true, true);
734
735                 case LFUN_MOUSE_RELEASE:
736                         return DispatchResult(lfunMouseRelease(cmd));
737
738                 case LFUN_CELL_BACKWARD:
739                 case LFUN_CELL_FORWARD:
740                         unlockInsetInInset(bv, the_locking_inset);
741                         if (cmd.action == LFUN_CELL_FORWARD)
742                                 moveNextCell(bv, old_locking_inset != 0);
743                         else
744                                 movePrevCell(bv, old_locking_inset != 0);
745                         clearSelection();
746                         if (hs)
747                                 updateLocal(bv);
748                         if (!the_locking_inset)
749                                 return DispatchResult(true);
750                         return result;
751                 // this to avoid compiler warnings.
752                 default:
753                         break;
754                 }
755         }
756
757         kb_action action = cmd.action;
758         string    arg    = cmd.argument;
759         if (the_locking_inset) {
760                 result = the_locking_inset->dispatch(cmd);
761                 if (result.dispatched()) {
762                         if (result.update()) {
763                                 updateLocal(bv);
764                         } else {
765                                 int const sc = scroll();
766                                 resetPos(bv);
767                                 if (sc != scroll()) {
768                                         // inset has been scrolled
769                                         updateLocal(bv);
770                                 }
771                         }
772                         return result;
773                 } else if (result.val() == FINISHED_UP) {
774                         action = LFUN_UP;
775                         // Make sure to reset status message after
776                         // exiting, e.g. math inset
777                         bv->owner()->clearMessage();
778                 } else if (result.val() == FINISHED_DOWN) {
779                         action = LFUN_DOWN;
780                         bv->owner()->clearMessage();
781                 } else if (result.val() == FINISHED_RIGHT) {
782                         action = LFUN_RIGHT;
783                         bv->owner()->clearMessage();
784                 } else if (result.val() == FINISHED) {
785                         bv->owner()->clearMessage();
786                 }
787         }
788
789         result = DispatchResult(true, true);
790         switch (action) {
791                 // --- Cursor Movements ----------------------------------
792         case LFUN_RIGHTSEL: {
793                 int const start = hasSelection() ? sel_cell_start : actcell;
794                 if (tabular.isLastCellInRow(actcell)) {
795                         setSelection(start, actcell);
796                         break;
797                 }
798
799                 int end = actcell;
800                 // if we are starting a selection, only select
801                 // the current cell at the beginning
802                 if (hasSelection()) {
803                         moveRight(bv, false);
804                         end = actcell;
805                 }
806                 setSelection(start, end);
807                 updateLocal(bv);
808                 break;
809         }
810         case LFUN_RIGHT:
811                 result = moveRight(bv);
812                 clearSelection();
813                 if (hs)
814                         updateLocal(bv);
815                 break;
816         case LFUN_LEFTSEL: {
817                 int const start = hasSelection() ? sel_cell_start : actcell;
818                 if (tabular.isFirstCellInRow(actcell)) {
819                         setSelection(start, actcell);
820                         break;
821                 }
822
823                 int end = actcell;
824                 // if we are starting a selection, only select
825                 // the current cell at the beginning
826                 if (hasSelection()) {
827                         moveLeft(bv, false);
828                         end = actcell;
829                 }
830                 setSelection(start, end);
831                 updateLocal(bv);
832                 break;
833         }
834         case LFUN_LEFT:
835                 result = moveLeft(bv);
836                 clearSelection();
837                 if (hs)
838                         updateLocal(bv);
839                 break;
840         case LFUN_DOWNSEL: {
841                 int const start = hasSelection() ? sel_cell_start : actcell;
842                 int const ocell = actcell;
843                 // if we are starting a selection, only select
844                 // the current cell at the beginning
845                 if (hasSelection()) {
846                         moveDown(bv, false);
847                         if (ocell == sel_cell_end ||
848                             tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
849                                 setSelection(start, tabular.getCellBelow(sel_cell_end));
850                         else
851                                 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
852                 } else {
853                         setSelection(start, start);
854                 }
855                 updateLocal(bv);
856         }
857         break;
858         case LFUN_DOWN:
859                 result = moveDown(bv, old_locking_inset != 0);
860                 clearSelection();
861                 if (hs)
862                         updateLocal(bv);
863                 break;
864         case LFUN_UPSEL: {
865                 int const start = hasSelection() ? sel_cell_start : actcell;
866                 int const ocell = actcell;
867                 // if we are starting a selection, only select
868                 // the current cell at the beginning
869                 if (hasSelection()) {
870                         moveUp(bv, false);
871                         if ((ocell == sel_cell_end) ||
872                             (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
873                                 setSelection(start, tabular.getCellAbove(sel_cell_end));
874                         else
875                                 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
876                 } else {
877                         setSelection(start, start);
878                 }
879                 updateLocal(bv);
880         }
881         break;
882         case LFUN_UP:
883                 result = moveUp(bv, old_locking_inset != 0);
884                 clearSelection();
885                 if (hs)
886                         updateLocal(bv);
887                 break;
888         case LFUN_NEXT: {
889                 if (hs)
890                         clearSelection();
891                 int column = actcol;
892                 unlockInsetInInset(bv, the_locking_inset);
893                 if (bv->top_y() + bv->painter().paperHeight() <
894                     top_baseline + tabular.getHeightOfTabular())
895                         {
896                                 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
897                                 actcell = tabular.getCellBelow(first_visible_cell) + column;
898                         } else {
899                                 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
900                         }
901                 resetPos(bv);
902                 updateLocal(bv);
903                 break;
904         }
905         case LFUN_PRIOR: {
906                 if (hs)
907                         clearSelection();
908                 int column = actcol;
909                 unlockInsetInInset(bv, the_locking_inset);
910                 if (top_baseline < 0) {
911                         bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
912                         if (top_baseline > 0)
913                                 actcell = column;
914                         else
915                                 actcell = tabular.getCellBelow(first_visible_cell) + column;
916                 } else {
917                         actcell = column;
918                 }
919                 resetPos(bv);
920                 updateLocal(bv);
921                 break;
922         }
923         // none of these make sense for insettabular,
924         // but we must catch them to prevent any
925         // selection from being confused
926         case LFUN_PRIORSEL:
927         case LFUN_NEXTSEL:
928         case LFUN_WORDLEFT:
929         case LFUN_WORDLEFTSEL:
930         case LFUN_WORDRIGHT:
931         case LFUN_WORDRIGHTSEL:
932         case LFUN_WORDSEL:
933         case LFUN_DOWN_PARAGRAPH:
934         case LFUN_DOWN_PARAGRAPHSEL:
935         case LFUN_UP_PARAGRAPH:
936         case LFUN_UP_PARAGRAPHSEL:
937         case LFUN_BACKSPACE:
938         case LFUN_HOME:
939         case LFUN_HOMESEL:
940         case LFUN_END:
941         case LFUN_ENDSEL:
942         case LFUN_BEGINNINGBUF:
943         case LFUN_BEGINNINGBUFSEL:
944         case LFUN_ENDBUF:
945         case LFUN_ENDBUFSEL:
946                 break;
947         case LFUN_LAYOUT_TABULAR:
948                 InsetTabularMailer(*this).showDialog(bv);
949                 break;
950         case LFUN_INSET_DIALOG_UPDATE:
951                 InsetTabularMailer(*this).updateDialog(bv);
952                 break;
953         case LFUN_TABULAR_FEATURE:
954                 if (!tabularFeatures(bv, arg))
955                         result = DispatchResult(false);
956                 break;
957                 // insert file functions
958         case LFUN_FILE_INSERT_ASCII_PARA:
959         case LFUN_FILE_INSERT_ASCII:
960         {
961                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
962                 if (tmpstr.empty())
963                         break;
964                 if (insertAsciiString(bv, tmpstr, false))
965                         updateLocal(bv);
966                 else
967                         result = DispatchResult(false);
968                 break;
969         }
970         // cut and paste functions
971         case LFUN_CUT:
972                 if (!copySelection(bv))
973                         break;
974                 // no break here!
975         case LFUN_DELETE:
976                 recordUndo(bv, Undo::DELETE);
977                 cutSelection(bv->buffer()->params());
978                 updateLocal(bv);
979                 break;
980         case LFUN_COPY:
981                 if (!hasSelection())
982                         break;
983                 finishUndo();
984                 copySelection(bv);
985                 break;
986         case LFUN_PASTESELECTION:
987         {
988                 string const clip = bv->getClipboard();
989                 if (clip.empty())
990                         break;
991 #if 0
992                 if (clip.find('\t') != string::npos) {
993                         int cols = 1;
994                         int rows = 1;
995                         int maxCols = 1;
996                         string::size_type len = clip.length();
997                         string::size_type p = 0;
998
999                         while (p < len &&
1000                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1001                                 switch (clip[p]) {
1002                                 case '\t':
1003                                         ++cols;
1004                                         break;
1005                                 case '\n':
1006                                         if ((p+1) < len)
1007                                                 ++rows;
1008                                         maxCols = max(cols, maxCols);
1009                                         cols = 1;
1010                                         break;
1011                                 }
1012                                 ++p;
1013                         }
1014                         maxCols = max(cols, maxCols);
1015
1016                         paste_tabular.reset(
1017                                 new LyXTabular(bv->buffer()->params(),
1018                                                this, rows, maxCols)
1019                                 );
1020
1021                         string::size_type op = 0;
1022                         int cell = 0;
1023                         int cells = paste_tabular->getNumberOfCells();
1024                         p = cols = 0;
1025                         while ((cell < cells) && (p < len) &&
1026                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1027                                 if (p >= len)
1028                                         break;
1029                                 switch (clip[p]) {
1030                                 case '\t':
1031                                         paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1032                                         ++cols;
1033                                         ++cell;
1034                                         break;
1035                                 case '\n':
1036                                         paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1037                                         while (cols++ < maxCols)
1038                                                 ++cell;
1039                                         cols = 0;
1040                                         break;
1041                                 }
1042                                 ++p;
1043                                 op = p;
1044                         }
1045                         // check for the last cell if there is no trailing '\n'
1046                         if ((cell < cells) && (op < len))
1047                                 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1048                 } else
1049 #else
1050                 if (!insertAsciiString(bv, clip, true))
1051 #endif
1052                 {
1053                         // so that the clipboard is used and it goes on
1054                         // to default
1055                         // and executes LFUN_PASTESELECTION in insettext!
1056                         paste_tabular.reset();
1057                 }
1058         }
1059         case LFUN_PASTE:
1060                 if (hasPasteBuffer()) {
1061                         recordUndo(bv, Undo::INSERT);
1062                         pasteSelection(bv);
1063                         updateLocal(bv);
1064                         break;
1065                 }
1066                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1067         default:
1068                 // handle font changing stuff on selection before we lock the inset
1069                 // in the default part!
1070                 result = DispatchResult(false);
1071                 if (hs) {
1072                         switch(action) {
1073                         case LFUN_LANGUAGE:
1074                         case LFUN_EMPH:
1075                         case LFUN_BOLD:
1076                         case LFUN_NOUN:
1077                         case LFUN_CODE:
1078                         case LFUN_SANS:
1079                         case LFUN_ROMAN:
1080                         case LFUN_DEFAULT:
1081                         case LFUN_UNDERLINE:
1082                         case LFUN_FONT_SIZE:
1083                                 if (bv->dispatch(FuncRequest(bv, action, arg)))
1084                                         result = DispatchResult(true, true);
1085                                 break;
1086                         default:
1087                                 break;
1088                         }
1089                 }
1090                 // we try to activate the actual inset and put this event down to
1091                 // the insets dispatch function.
1092                 if (result.dispatched() || the_locking_inset)
1093                         break;
1094                 if (activateCellInset(bv)) {
1095                         result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1096                         if (!result.dispatched()) {
1097                                 unlockInsetInInset(bv, the_locking_inset);
1098                                 // we need to update if this was requested before
1099                                 updateLocal(bv);
1100                                 return DispatchResult(false);
1101                         }
1102                         if (hs)
1103                                 clearSelection();
1104                         updateLocal(bv);
1105                         return result;
1106                 }
1107                 break;
1108         }
1109
1110         if (result.val() >= FINISHED)
1111                 bv->unlockInset(this);
1112         else if (!the_locking_inset && bv->fitCursor())
1113                 updateLocal(bv);
1114
1115         return result;
1116 }
1117
1118
1119 int InsetTabular::latex(Buffer const & buf, ostream & os,
1120                         LatexRunParams const & runparams) const
1121 {
1122         return tabular.latex(buf, os, runparams);
1123 }
1124
1125
1126 int InsetTabular::ascii(Buffer const & buf, ostream & os,
1127                         LatexRunParams const & runparams) const
1128 {
1129         if (runparams.linelen > 0)
1130                 return tabular.ascii(buf, os, runparams,
1131                                      ownerPar(buf, this).params().depth(),
1132                                      false, 0);
1133         return tabular.ascii(buf, os, runparams, 0, false, 0);
1134 }
1135
1136
1137 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1138                            LatexRunParams const & runparams) const
1139 {
1140         return tabular.linuxdoc(buf,os, runparams);
1141 }
1142
1143
1144 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1145                           LatexRunParams const & runparams) const
1146 {
1147         int ret = 0;
1148         InsetOld * master;
1149
1150         // if the table is inside a float it doesn't need the informaltable
1151         // wrapper. Search for it.
1152         for (master = owner();
1153              master && master->lyxCode() != InsetOld::FLOAT_CODE;
1154              master = master->owner());
1155
1156         if (!master) {
1157                 os << "<informaltable>";
1158                 if (runparams.mixed_content)
1159                         os << endl;
1160                 ++ret;
1161         }
1162         ret += tabular.docbook(buf, os, runparams);
1163         if (!master) {
1164                 os << "</informaltable>";
1165                 if (runparams.mixed_content)
1166                         os << endl;
1167                 ++ret;
1168         }
1169         return ret;
1170 }
1171
1172
1173 void InsetTabular::validate(LaTeXFeatures & features) const
1174 {
1175         tabular.validate(features);
1176 }
1177
1178
1179 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1180 {
1181 #if 1
1182         // if we have a locking_inset we should have to check only this cell for
1183         // change so I'll try this to have a boost, but who knows ;) (Jug?)
1184         // This is _really_ important (André)
1185         if (the_locking_inset == &tabular.getCellInset(actcell)) {
1186                 int maxAsc = 0;
1187                 int maxDesc = 0;
1188                 for (int j = 0; j < tabular.columns(); ++j) {
1189                         Dimension dim;
1190                         MetricsInfo m = mi;
1191                         m.base.textwidth =
1192                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1193                         tabular.getCellInset(actrow, j).metrics(m, dim);
1194                         maxAsc  = max(dim.asc, maxAsc);
1195                         maxDesc = max(dim.des, maxDesc);
1196                 }
1197                 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1198                 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1199                 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1200                 return;
1201         }
1202 #endif
1203
1204         int cell = -1;
1205         for (int i = 0; i < tabular.rows(); ++i) {
1206                 int maxAsc = 0;
1207                 int maxDesc = 0;
1208                 for (int j = 0; j < tabular.columns(); ++j) {
1209                         if (tabular.isPartOfMultiColumn(i, j))
1210                                 continue;
1211                         ++cell;
1212                         Dimension dim;
1213                         MetricsInfo m = mi;
1214                         m.base.textwidth =
1215                                 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1216                         tabular.getCellInset(cell).metrics(m, dim);
1217                         maxAsc  = max(maxAsc, dim.asc);
1218                         maxDesc = max(maxDesc, dim.des);
1219                         tabular.setWidthOfCell(cell, dim.wid);
1220                 }
1221                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1222                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1223         }
1224 }
1225
1226
1227 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1228 {
1229         if (the_locking_inset) {
1230                 the_locking_inset->getCursor(bv, x, y);
1231                 return;
1232         }
1233
1234         x = cursorx_;
1235         y = cursory_ + InsetTabular::y();
1236
1237         // Fun stuff
1238         int desc = tabular.getDescentOfRow(actrow);
1239         y += desc;
1240         int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1241         y -= ascdesc / 2;
1242         y += ADD_TO_HEIGHT * 2;
1243         y += TEXT_TO_INSET_OFFSET;
1244 }
1245
1246
1247 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1248 {
1249         if (the_locking_inset) {
1250                 the_locking_inset->getCursorPos(bv, x, y);
1251                 return;
1252         }
1253         x = cursorx_ - top_x;
1254         y = cursory_;
1255 }
1256
1257
1258 void InsetTabular::fitInsetCursor(BufferView * bv) const
1259 {
1260         if (the_locking_inset) {
1261                 the_locking_inset->fitInsetCursor(bv);
1262                 return;
1263         }
1264
1265         LyXFont font;
1266         int const asc = font_metrics::maxAscent(font);
1267         int const desc = font_metrics::maxDescent(font);
1268         resetPos(bv);
1269
1270         bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1271 }
1272
1273
1274 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1275 {
1276         cursory_ = 0;
1277         actcell = actrow = actcol = 0;
1278         int ly = tabular.getDescentOfRow(actrow);
1279
1280         // first search the right row
1281         while (ly < y && actrow + 1 < tabular.rows()) {
1282                 cursory_ += tabular.getDescentOfRow(actrow) +
1283                                  tabular.getAscentOfRow(actrow + 1) +
1284                                  tabular.getAdditionalHeight(actrow + 1);
1285                 ++actrow;
1286                 ly = cursory_ + tabular.getDescentOfRow(actrow);
1287         }
1288         actcell = tabular.getCellNumber(actrow, actcol);
1289
1290         // now search the right column
1291         int lx = tabular.getWidthOfColumn(actcell) -
1292                 tabular.getAdditionalWidth(actcell);
1293
1294         for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1295                 lx += tabular.getWidthOfColumn(actcell + 1)
1296                         + tabular.getAdditionalWidth(actcell);
1297
1298         cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1299         resetPos(bv);
1300 }
1301
1302
1303 int InsetTabular::getCellXPos(int cell) const
1304 {
1305         int c = cell;
1306
1307         for (; !tabular.isFirstCellInRow(c); --c)
1308                 ;
1309         int lx = tabular.getWidthOfColumn(cell);
1310         for (; c < cell; ++c)
1311                 lx += tabular.getWidthOfColumn(c);
1312
1313         return (lx - tabular.getWidthOfColumn(cell) + top_x);
1314 }
1315
1316
1317 void InsetTabular::resetPos(BufferView * bv) const
1318 {
1319 #ifdef WITH_WARNINGS
1320 #warning This should be fixed in the right manner (20011128 Jug)
1321 #endif
1322         // fast hack to fix infinite repaintings!
1323         if (in_reset_pos > 0)
1324                 return;
1325
1326         int cell = 0;
1327         actcol = tabular.column_of_cell(actcell);
1328         actrow = 0;
1329         cursory_ = 0;
1330         for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1331                 if (tabular.isLastCellInRow(cell)) {
1332                         cursory_ += tabular.getDescentOfRow(actrow) +
1333                                          tabular.getAscentOfRow(actrow + 1) +
1334                                          tabular.getAdditionalHeight(actrow + 1);
1335                         ++actrow;
1336                 }
1337         }
1338         if (!locked) {
1339                 if (the_locking_inset)
1340                         inset_y = cursory_;
1341                 return;
1342         }
1343         // we need this only from here on!!!
1344         ++in_reset_pos;
1345         int const offset = ADD_TO_TABULAR_WIDTH + 2;
1346         int new_x = getCellXPos(actcell);
1347         int old_x = cursorx_;
1348         new_x += offset;
1349         cursorx_ = new_x;
1350 //    cursor.x(getCellXPos(actcell) + offset);
1351         if (actcol < tabular.columns() - 1 && scroll(false) &&
1352                 tabular.getWidthOfTabular() < bv->workWidth()-20)
1353         {
1354                 scroll(bv, 0.0F);
1355                 updateLocal(bv);
1356         } else if (the_locking_inset &&
1357                  tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1358         {
1359                 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1360                 if (xx > bv->workWidth()-20) {
1361                         scroll(bv, -(xx - bv->workWidth() + 60));
1362                         updateLocal(bv);
1363                 } else if (xx < 20) {
1364                         if (xx < 0)
1365                                 xx = -xx + 60;
1366                         else
1367                                 xx = 60;
1368                         scroll(bv, xx);
1369                         updateLocal(bv);
1370                 }
1371         } else if (cursorx_ - offset > 20 &&
1372                    cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1373                    > bv->workWidth() - 20) {
1374                 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1375                 updateLocal(bv);
1376         } else if (cursorx_ - offset < 20) {
1377                 scroll(bv, 20 - cursorx_ + offset);
1378                 updateLocal(bv);
1379         } else if (scroll() && top_x > 20 &&
1380                    (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1381                 scroll(bv, old_x - cursorx_);
1382                 updateLocal(bv);
1383         }
1384         if (the_locking_inset) {
1385                 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1386                 inset_y = cursory_;
1387         }
1388         if ((!the_locking_inset ||
1389              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1390             actcell != oldcell) {
1391                 InsetTabularMailer(*this).updateDialog(bv);
1392                 oldcell = actcell;
1393         }
1394         in_reset_pos = 0;
1395 }
1396
1397
1398 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1399 {
1400         if (lock && !old_locking_inset) {
1401                 if (activateCellInset(bv))
1402                         return DispatchResult(true, true);
1403         } else {
1404                 bool moved = isRightToLeft(bv)
1405                         ? movePrevCell(bv) : moveNextCell(bv);
1406                 if (!moved)
1407                         return DispatchResult(false, FINISHED_RIGHT);
1408                 if (lock && activateCellInset(bv))
1409                         return DispatchResult(true, true);
1410         }
1411         resetPos(bv);
1412         return DispatchResult(true);
1413 }
1414
1415
1416 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1417 {
1418         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1419         if (!moved)
1420                 return DispatchResult(false, FINISHED);
1421         // behind the inset
1422         if (lock && activateCellInset(bv, 0, 0, true))
1423                 return DispatchResult(true, true);
1424         resetPos(bv);
1425         return DispatchResult(true);
1426 }
1427
1428
1429 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1430 {
1431         int const ocell = actcell;
1432         actcell = tabular.getCellAbove(actcell);
1433         if (actcell == ocell) // we moved out of the inset
1434                 return DispatchResult(false, FINISHED_UP);
1435         resetPos(bv);
1436         if (lock) {
1437                 int x = 0;
1438                 int y = 0;
1439                 if (old_locking_inset) {
1440                         old_locking_inset->getCursorPos(bv, x, y);
1441                         x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1442                 }
1443                 if (activateCellInset(bv, x, 0))
1444                         return DispatchResult(true, true);
1445         }
1446         return DispatchResult(true);
1447 }
1448
1449
1450 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1451 {
1452         int const ocell = actcell;
1453         actcell = tabular.getCellBelow(actcell);
1454         if (actcell == ocell) // we moved out of the inset
1455                 return DispatchResult(false, FINISHED_DOWN);
1456         resetPos(bv);
1457         if (lock) {
1458                 int x = 0;
1459                 int y = 0;
1460                 if (old_locking_inset) {
1461                         old_locking_inset->getCursorPos(bv, x, y);
1462                         x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1463                 }
1464                 if (activateCellInset(bv, x, 0))
1465                         return DispatchResult(true, true);
1466         }
1467         return DispatchResult(true);
1468 }
1469
1470
1471 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1472 {
1473         if (isRightToLeft(bv)) {
1474                 if (tabular.isFirstCellInRow(actcell)) {
1475                         int row = tabular.row_of_cell(actcell);
1476                         if (row == tabular.rows() - 1)
1477                                 return false;
1478                         actcell = tabular.getLastCellInRow(row);
1479                         actcell = tabular.getCellBelow(actcell);
1480                 } else {
1481                         if (!actcell)
1482                                 return false;
1483                         --actcell;
1484                 }
1485         } else {
1486                 if (tabular.isLastCell(actcell))
1487                         return false;
1488                 ++actcell;
1489         }
1490         if (lock) {
1491                 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1492                         isRightToLeftPar(bv->buffer()->params());
1493                 activateCellInset(bv, 0, 0, !rtl);
1494         }
1495         resetPos(bv);
1496         return true;
1497 }
1498
1499
1500 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1501 {
1502         if (isRightToLeft(bv)) {
1503                 if (tabular.isLastCellInRow(actcell)) {
1504                         int row = tabular.row_of_cell(actcell);
1505                         if (row == 0)
1506                                 return false;
1507                         actcell = tabular.getFirstCellInRow(row);
1508                         actcell = tabular.getCellAbove(actcell);
1509                 } else {
1510                         if (tabular.isLastCell(actcell))
1511                                 return false;
1512                         ++actcell;
1513                 }
1514         } else {
1515                 if (!actcell) // first cell
1516                         return false;
1517                 --actcell;
1518         }
1519         if (lock) {
1520                 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1521                         isRightToLeftPar(bv->buffer()->params());
1522                 activateCellInset(bv, 0, 0, !rtl);
1523         }
1524         resetPos(bv);
1525         return true;
1526 }
1527
1528
1529 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1530                            bool selectall)
1531 {
1532         if (selectall) {
1533                 setSelection(0, tabular.getNumberOfCells() - 1);
1534         }
1535         if (hasSelection()) {
1536                 recordUndo(bv, Undo::ATOMIC);
1537                 bool const frozen = undo_frozen;
1538                 if (!frozen)
1539                         freezeUndo();
1540                 // apply the fontchange on the whole selection
1541                 int sel_row_start;
1542                 int sel_row_end;
1543                 int sel_col_start;
1544                 int sel_col_end;
1545                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
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.getCellInset(i, j).setFont(bv, font, tall, true);
1549
1550                 if (!frozen)
1551                         unFreezeUndo();
1552                 if (selectall)
1553                         clearSelection();
1554                 updateLocal(bv);
1555         }
1556         if (the_locking_inset)
1557                 the_locking_inset->setFont(bv, font, tall);
1558 }
1559
1560
1561 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1562 {
1563         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1564
1565         int i = 0;
1566         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1567                 string const tmp = tabularFeature[i].feature;
1568
1569                 if (tmp == what.substr(0, tmp.length())) {
1570                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1571                         //tabularFeatures[i].feature.length())) {
1572                         action = tabularFeature[i].action;
1573                         break;
1574                 }
1575         }
1576         if (action == LyXTabular::LAST_ACTION)
1577                 return false;
1578
1579         string const val =
1580                 ltrim(what.substr(tabularFeature[i].feature.length()));
1581         tabularFeatures(bv, action, val);
1582         return true;
1583 }
1584
1585 namespace {
1586
1587 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1588                           string const & special, bool & flag)
1589 {
1590         if (special == "dl_above") {
1591                 ltt.topDL = flag;
1592                 ltt.set = false;
1593         } else if (special == "dl_below") {
1594                 ltt.bottomDL = flag;
1595                 ltt.set = false;
1596         } else if (special == "empty") {
1597                 ltt.empty = flag;
1598                 ltt.set = false;
1599         } else if (flag) {
1600                 ltt.empty = false;
1601                 ltt.set = true;
1602         }
1603 }
1604
1605 } // anon namespace
1606
1607
1608 void InsetTabular::tabularFeatures(BufferView * bv,
1609         LyXTabular::Feature feature, string const & value)
1610 {
1611         int sel_col_start;
1612         int sel_col_end;
1613         int sel_row_start;
1614         int sel_row_end;
1615         bool setLines = false;
1616         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1617         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1618
1619         switch (feature) {
1620
1621         case LyXTabular::M_ALIGN_LEFT:
1622         case LyXTabular::ALIGN_LEFT:
1623                 setAlign = LYX_ALIGN_LEFT;
1624                 break;
1625
1626         case LyXTabular::M_ALIGN_RIGHT:
1627         case LyXTabular::ALIGN_RIGHT:
1628                 setAlign = LYX_ALIGN_RIGHT;
1629                 break;
1630
1631         case LyXTabular::M_ALIGN_CENTER:
1632         case LyXTabular::ALIGN_CENTER:
1633                 setAlign = LYX_ALIGN_CENTER;
1634                 break;
1635
1636         case LyXTabular::ALIGN_BLOCK:
1637                 setAlign = LYX_ALIGN_BLOCK;
1638                 break;
1639
1640         case LyXTabular::M_VALIGN_TOP:
1641         case LyXTabular::VALIGN_TOP:
1642                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1643                 break;
1644
1645         case LyXTabular::M_VALIGN_BOTTOM:
1646         case LyXTabular::VALIGN_BOTTOM:
1647                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1648                 break;
1649
1650         case LyXTabular::M_VALIGN_MIDDLE:
1651         case LyXTabular::VALIGN_MIDDLE:
1652                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1653                 break;
1654
1655         default:
1656                 break;
1657         }
1658
1659         if (hasSelection()) {
1660                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1661         } else {
1662                 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1663                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1664         }
1665         recordUndo(bv, Undo::ATOMIC);
1666
1667         int row =  tabular.row_of_cell(actcell);
1668         int column = tabular.column_of_cell(actcell);
1669         bool flag = true;
1670         LyXTabular::ltType ltt;
1671
1672         switch (feature) {
1673
1674         case LyXTabular::SET_PWIDTH: {
1675                 LyXLength const len(value);
1676                 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1677
1678                 tabular.setColumnPWidth(actcell, len);
1679                 if (oldlen != len) {
1680                         // We need this otherwise we won't resize
1681                         // the insettext of the active cell (if any)
1682                         // until later (see InsetText::do_resize)
1683                         unlockInsetInInset(bv, the_locking_inset);
1684                         bv->update();
1685                 }
1686
1687                 if (len.zero()
1688                     && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1689                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1690                 else if (!len.zero()
1691                          && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1692                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1693                 break;
1694         }
1695
1696         case LyXTabular::SET_MPWIDTH:
1697         {
1698                 LyXLength const len(value);
1699                 LyXLength const & oldlen = tabular.getPWidth(actcell);
1700                 tabular.setMColumnPWidth(actcell, len);
1701                 if (oldlen != len) {
1702                         // We need this otherwise we won't resize
1703                         // the insettext of the active cell (if any)
1704                         // until later (see InsetText::do_resize)
1705                         unlockInsetInInset(bv, the_locking_inset);
1706                         updateLocal(bv);
1707                 }
1708         }
1709         break;
1710
1711         case LyXTabular::SET_SPECIAL_COLUMN:
1712         case LyXTabular::SET_SPECIAL_MULTI:
1713                 tabular.setAlignSpecial(actcell,value,feature);
1714                 updateLocal(bv);
1715                 break;
1716
1717         case LyXTabular::APPEND_ROW:
1718                 // append the row into the tabular
1719                 unlockInsetInInset(bv, the_locking_inset);
1720                 tabular.appendRow(bv->buffer()->params(), actcell);
1721                 tabular.setOwner(this);
1722                 updateLocal(bv);
1723                 break;
1724
1725         case LyXTabular::APPEND_COLUMN:
1726                 // append the column into the tabular
1727                 unlockInsetInInset(bv, the_locking_inset);
1728                 tabular.appendColumn(bv->buffer()->params(), actcell);
1729                 tabular.setOwner(this);
1730                 actcell = tabular.getCellNumber(row, column);
1731                 updateLocal(bv);
1732                 break;
1733
1734         case LyXTabular::DELETE_ROW:
1735                 unlockInsetInInset(bv, the_locking_inset);
1736                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737                         tabular.deleteRow(sel_row_start);
1738                 if (sel_row_start >= tabular.rows())
1739                         --sel_row_start;
1740                 actcell = tabular.getCellNumber(sel_row_start, column);
1741                 clearSelection();
1742                 updateLocal(bv);
1743                 break;
1744
1745         case LyXTabular::DELETE_COLUMN:
1746                 unlockInsetInInset(bv, the_locking_inset);
1747                 for (int i = sel_col_start; i <= sel_col_end; ++i)
1748                         tabular.deleteColumn(sel_col_start);
1749                 if (sel_col_start >= tabular.columns())
1750                         --sel_col_start;
1751                 actcell = tabular.getCellNumber(row, sel_col_start);
1752                 clearSelection();
1753                 updateLocal(bv);
1754                 break;
1755
1756         case LyXTabular::M_TOGGLE_LINE_TOP:
1757                 flag = false;
1758         case LyXTabular::TOGGLE_LINE_TOP: {
1759                 bool lineSet = !tabular.topLine(actcell, flag);
1760                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1761                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1762                                 tabular.setTopLine(
1763                                         tabular.getCellNumber(i, j),
1764                                         lineSet, flag);
1765                 updateLocal(bv);
1766                 break;
1767         }
1768
1769         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1770                 flag = false;
1771         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1772                 bool lineSet = !tabular.bottomLine(actcell, flag);
1773                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1774                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1775                                 tabular.setBottomLine(
1776                                         tabular.getCellNumber(i, j),
1777                                         lineSet,
1778                                         flag);
1779                 updateLocal(bv);
1780                 break;
1781         }
1782
1783         case LyXTabular::M_TOGGLE_LINE_LEFT:
1784                 flag = false;
1785         case LyXTabular::TOGGLE_LINE_LEFT: {
1786                 bool lineSet = !tabular.leftLine(actcell, flag);
1787                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1788                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1789                                 tabular.setLeftLine(
1790                                         tabular.getCellNumber(i,j),
1791                                         lineSet,
1792                                         flag);
1793                 updateLocal(bv);
1794                 break;
1795         }
1796
1797         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1798                 flag = false;
1799         case LyXTabular::TOGGLE_LINE_RIGHT: {
1800                 bool lineSet = !tabular.rightLine(actcell, flag);
1801                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1802                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1803                                 tabular.setRightLine(
1804                                         tabular.getCellNumber(i,j),
1805                                         lineSet,
1806                                         flag);
1807                 updateLocal(bv);
1808                 break;
1809         }
1810
1811         case LyXTabular::M_ALIGN_LEFT:
1812         case LyXTabular::M_ALIGN_RIGHT:
1813         case LyXTabular::M_ALIGN_CENTER:
1814                 flag = false;
1815         case LyXTabular::ALIGN_LEFT:
1816         case LyXTabular::ALIGN_RIGHT:
1817         case LyXTabular::ALIGN_CENTER:
1818         case LyXTabular::ALIGN_BLOCK:
1819                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1820                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1821                                 tabular.setAlignment(
1822                                         tabular.getCellNumber(i, j),
1823                                         setAlign,
1824                                         flag);
1825                 updateLocal(bv);
1826                 break;
1827
1828         case LyXTabular::M_VALIGN_TOP:
1829         case LyXTabular::M_VALIGN_BOTTOM:
1830         case LyXTabular::M_VALIGN_MIDDLE:
1831                 flag = false;
1832         case LyXTabular::VALIGN_TOP:
1833         case LyXTabular::VALIGN_BOTTOM:
1834         case LyXTabular::VALIGN_MIDDLE:
1835                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1836                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1837                                 tabular.setVAlignment(
1838                                         tabular.getCellNumber(i, j),
1839                                         setVAlign, flag);
1840                 updateLocal(bv);
1841                 break;
1842
1843         case LyXTabular::MULTICOLUMN: {
1844                 if (sel_row_start != sel_row_end) {
1845 #ifdef WITH_WARNINGS
1846 #warning Need I say it ? This is horrible.
1847 #endif
1848                         Alert::error(_("Error setting multicolumn"),
1849                                    _("You cannot set multicolumn vertically."));
1850                         return;
1851                 }
1852                 // just multicol for one Single Cell
1853                 if (!hasSelection()) {
1854                         // check wether we are completly in a multicol
1855                         if (tabular.isMultiColumn(actcell))
1856                                 tabular.unsetMultiColumn(actcell);
1857                         else
1858                                 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1859                         updateLocal(bv);
1860                         break;
1861                 }
1862                 // we have a selection so this means we just add all this
1863                 // cells to form a multicolumn cell
1864                 int s_start;
1865                 int s_end;
1866
1867                 if (sel_cell_start > sel_cell_end) {
1868                         s_start = sel_cell_end;
1869                         s_end = sel_cell_start;
1870                 } else {
1871                         s_start = sel_cell_start;
1872                         s_end = sel_cell_end;
1873                 }
1874                 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1875                 actcell = s_start;
1876                 clearSelection();
1877                 updateLocal(bv);
1878                 break;
1879         }
1880
1881         case LyXTabular::SET_ALL_LINES:
1882                 setLines = true;
1883         case LyXTabular::UNSET_ALL_LINES:
1884                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1885                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1886                                 tabular.setAllLines(
1887                                         tabular.getCellNumber(i,j), setLines);
1888                 updateLocal(bv);
1889                 break;
1890
1891         case LyXTabular::SET_LONGTABULAR:
1892                 tabular.setLongTabular(true);
1893                 updateLocal(bv); // because this toggles displayed
1894                 break;
1895
1896         case LyXTabular::UNSET_LONGTABULAR:
1897                 tabular.setLongTabular(false);
1898                 updateLocal(bv); // because this toggles displayed
1899                 break;
1900
1901         case LyXTabular::SET_ROTATE_TABULAR:
1902                 tabular.setRotateTabular(true);
1903                 break;
1904
1905         case LyXTabular::UNSET_ROTATE_TABULAR:
1906                 tabular.setRotateTabular(false);
1907                 break;
1908
1909         case LyXTabular::SET_ROTATE_CELL:
1910                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1911                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1912                                 tabular.setRotateCell(
1913                                         tabular.getCellNumber(i, j), true);
1914                 break;
1915
1916         case LyXTabular::UNSET_ROTATE_CELL:
1917                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1918                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1919                                 tabular.setRotateCell(
1920                                         tabular.getCellNumber(i, j), false);
1921                 break;
1922
1923         case LyXTabular::SET_USEBOX: {
1924                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1925                 if (val == tabular.getUsebox(actcell))
1926                         val = LyXTabular::BOX_NONE;
1927                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1928                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1929                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1930                 break;
1931         }
1932
1933         case LyXTabular::UNSET_LTFIRSTHEAD:
1934                 flag = false;
1935         case LyXTabular::SET_LTFIRSTHEAD:
1936                 tabular.getRowOfLTFirstHead(row, ltt);
1937                 checkLongtableSpecial(ltt, value, flag);
1938                 tabular.setLTHead(row, flag, ltt, true);
1939                 break;
1940
1941         case LyXTabular::UNSET_LTHEAD:
1942                 flag = false;
1943         case LyXTabular::SET_LTHEAD:
1944                 tabular.getRowOfLTHead(row, ltt);
1945                 checkLongtableSpecial(ltt, value, flag);
1946                 tabular.setLTHead(row, flag, ltt, false);
1947                 break;
1948
1949         case LyXTabular::UNSET_LTFOOT:
1950                 flag = false;
1951         case LyXTabular::SET_LTFOOT:
1952                 tabular.getRowOfLTFoot(row, ltt);
1953                 checkLongtableSpecial(ltt, value, flag);
1954                 tabular.setLTFoot(row, flag, ltt, false);
1955                 break;
1956
1957         case LyXTabular::UNSET_LTLASTFOOT:
1958                 flag = false;
1959         case LyXTabular::SET_LTLASTFOOT:
1960                 tabular.getRowOfLTLastFoot(row, ltt);
1961                 checkLongtableSpecial(ltt, value, flag);
1962                 tabular.setLTFoot(row, flag, ltt, true);
1963                 break;
1964
1965         case LyXTabular::SET_LTNEWPAGE:
1966                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1967                 break;
1968
1969         // dummy stuff just to avoid warnings
1970         case LyXTabular::LAST_ACTION:
1971                 break;
1972         }
1973
1974         InsetTabularMailer(*this).updateDialog(bv);
1975 }
1976
1977
1978 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1979 {
1980         UpdatableInset & inset = tabular.getCellInset(actcell);
1981         if (behind) {
1982 #warning metrics?
1983                 x = inset.x() + inset.width();
1984                 y = inset.descent();
1985         }
1986         //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1987         //inset_y = cursory_;
1988         inset.edit(bv, x, y);
1989         if (!the_locking_inset)
1990                 return false;
1991         updateLocal(bv);
1992         return the_locking_inset;
1993 }
1994
1995
1996 bool InsetTabular::insetHit(BufferView *, int x, int) const
1997 {
1998         return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1999 }
2000
2001
2002 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2003 {
2004         //resizeLyXText(bv, recursive);
2005 }
2006
2007
2008 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2009                                    bool const recursive) const
2010 {
2011         if (the_locking_inset)
2012                 return the_locking_inset->getLyXText(bv, recursive);
2013         return InsetOld::getLyXText(bv, recursive);
2014 }
2015
2016
2017 bool InsetTabular::showInsetDialog(BufferView * bv) const
2018 {
2019         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2020                 InsetTabularMailer(*this).showDialog(bv);
2021         return true;
2022 }
2023
2024
2025 void InsetTabular::openLayoutDialog(BufferView * bv) const
2026 {
2027         if (the_locking_inset) {
2028                 InsetTabular * inset = static_cast<InsetTabular *>
2029                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2030                 if (inset) {
2031                         inset->openLayoutDialog(bv);
2032                         return;
2033                 }
2034         }
2035         InsetTabularMailer(*this).showDialog(bv);
2036 }
2037
2038
2039 //
2040 // function returns an object as defined in func_status.h:
2041 // states OK, Unknown, Disabled, On, Off.
2042 //
2043 FuncStatus InsetTabular::getStatus(string const & what) const
2044 {
2045         int action = LyXTabular::LAST_ACTION;
2046         FuncStatus status;
2047
2048         int i = 0;
2049         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2050                 string const tmp = tabularFeature[i].feature;
2051                 if (tmp == what.substr(0, tmp.length())) {
2052                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2053                         //   tabularFeatures[i].feature.length())) {
2054                         action = tabularFeature[i].action;
2055                         break;
2056                 }
2057         }
2058         if (action == LyXTabular::LAST_ACTION) {
2059                 status.clear();
2060                 status.unknown(true);
2061                 return status;
2062         }
2063
2064         string const argument
2065                 = ltrim(what.substr(tabularFeature[i].feature.length()));
2066
2067         int sel_row_start;
2068         int sel_row_end;
2069         int dummy;
2070         LyXTabular::ltType dummyltt;
2071         bool flag = true;
2072
2073         if (hasSelection())
2074                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2075         else
2076                 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2077
2078         switch (action) {
2079         case LyXTabular::SET_PWIDTH:
2080         case LyXTabular::SET_MPWIDTH:
2081         case LyXTabular::SET_SPECIAL_COLUMN:
2082         case LyXTabular::SET_SPECIAL_MULTI:
2083         case LyXTabular::APPEND_ROW:
2084         case LyXTabular::APPEND_COLUMN:
2085         case LyXTabular::DELETE_ROW:
2086         case LyXTabular::DELETE_COLUMN:
2087         case LyXTabular::SET_ALL_LINES:
2088         case LyXTabular::UNSET_ALL_LINES:
2089                 status.clear();
2090                 return status;
2091
2092         case LyXTabular::MULTICOLUMN:
2093                 status.setOnOff(tabular.isMultiColumn(actcell));
2094                 break;
2095
2096         case LyXTabular::M_TOGGLE_LINE_TOP:
2097                 flag = false;
2098         case LyXTabular::TOGGLE_LINE_TOP:
2099                 status.setOnOff(tabular.topLine(actcell, flag));
2100                 break;
2101
2102         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2103                 flag = false;
2104         case LyXTabular::TOGGLE_LINE_BOTTOM:
2105                 status.setOnOff(tabular.bottomLine(actcell, flag));
2106                 break;
2107
2108         case LyXTabular::M_TOGGLE_LINE_LEFT:
2109                 flag = false;
2110         case LyXTabular::TOGGLE_LINE_LEFT:
2111                 status.setOnOff(tabular.leftLine(actcell, flag));
2112                 break;
2113
2114         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2115                 flag = false;
2116         case LyXTabular::TOGGLE_LINE_RIGHT:
2117                 status.setOnOff(tabular.rightLine(actcell, flag));
2118                 break;
2119
2120         case LyXTabular::M_ALIGN_LEFT:
2121                 flag = false;
2122         case LyXTabular::ALIGN_LEFT:
2123                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2124                 break;
2125
2126         case LyXTabular::M_ALIGN_RIGHT:
2127                 flag = false;
2128         case LyXTabular::ALIGN_RIGHT:
2129                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2130                 break;
2131
2132         case LyXTabular::M_ALIGN_CENTER:
2133                 flag = false;
2134         case LyXTabular::ALIGN_CENTER:
2135                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2136                 break;
2137
2138         case LyXTabular::ALIGN_BLOCK:
2139                 status.disabled(tabular.getPWidth(actcell).zero());
2140                 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2141                 break;
2142
2143         case LyXTabular::M_VALIGN_TOP:
2144                 flag = false;
2145         case LyXTabular::VALIGN_TOP:
2146                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2147                 break;
2148
2149         case LyXTabular::M_VALIGN_BOTTOM:
2150                 flag = false;
2151         case LyXTabular::VALIGN_BOTTOM:
2152                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2153                 break;
2154
2155         case LyXTabular::M_VALIGN_MIDDLE:
2156                 flag = false;
2157         case LyXTabular::VALIGN_MIDDLE:
2158                 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2159                 break;
2160
2161         case LyXTabular::SET_LONGTABULAR:
2162                 status.setOnOff(tabular.isLongTabular());
2163                 break;
2164
2165         case LyXTabular::UNSET_LONGTABULAR:
2166                 status.setOnOff(!tabular.isLongTabular());
2167                 break;
2168
2169         case LyXTabular::SET_ROTATE_TABULAR:
2170                 status.setOnOff(tabular.getRotateTabular());
2171                 break;
2172
2173         case LyXTabular::UNSET_ROTATE_TABULAR:
2174                 status.setOnOff(!tabular.getRotateTabular());
2175                 break;
2176
2177         case LyXTabular::SET_ROTATE_CELL:
2178                 status.setOnOff(tabular.getRotateCell(actcell));
2179                 break;
2180
2181         case LyXTabular::UNSET_ROTATE_CELL:
2182                 status.setOnOff(!tabular.getRotateCell(actcell));
2183                 break;
2184
2185         case LyXTabular::SET_USEBOX:
2186                 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2187                 break;
2188
2189         case LyXTabular::SET_LTFIRSTHEAD:
2190                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2191                 break;
2192
2193         case LyXTabular::SET_LTHEAD:
2194                 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2195                 break;
2196
2197         case LyXTabular::SET_LTFOOT:
2198                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2199                 break;
2200
2201         case LyXTabular::SET_LTLASTFOOT:
2202                 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2203                 break;
2204
2205         case LyXTabular::SET_LTNEWPAGE:
2206                 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2207                 break;
2208
2209         default:
2210                 status.clear();
2211                 status.disabled(true);
2212                 break;
2213         }
2214         return status;
2215 }
2216
2217
2218 void InsetTabular::getLabelList(Buffer const & buffer,
2219                                 std::vector<string> & list) const
2220 {
2221         tabular.getLabelList(buffer, list);
2222 }
2223
2224
2225 bool InsetTabular::copySelection(BufferView * bv)
2226 {
2227         if (!hasSelection())
2228                 return false;
2229
2230         int sel_col_start = tabular.column_of_cell(sel_cell_start);
2231         int sel_col_end = tabular.column_of_cell(sel_cell_end);
2232         if (sel_col_start > sel_col_end) {
2233                 sel_col_start = sel_col_end;
2234                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2235         } else {
2236                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2237         }
2238
2239         int sel_row_start = tabular.row_of_cell(sel_cell_start);
2240         int sel_row_end = tabular.row_of_cell(sel_cell_end);
2241         if (sel_row_start > sel_row_end)
2242                 swap(sel_row_start, sel_row_end);
2243
2244         paste_tabular.reset(new LyXTabular(tabular));
2245         paste_tabular->setOwner(this);
2246
2247         for (int i = 0; i < sel_row_start; ++i)
2248                 paste_tabular->deleteRow(0);
2249
2250         int const rows = sel_row_end - sel_row_start + 1;
2251         while (paste_tabular->rows() > rows)
2252                 paste_tabular->deleteRow(rows);
2253
2254         paste_tabular->setTopLine(0, true, true);
2255         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2256                                      true, true);
2257
2258         for (int i = 0; i < sel_col_start; ++i)
2259                 paste_tabular->deleteColumn(0);
2260
2261         int const columns = sel_col_end - sel_col_start + 1;
2262         while (paste_tabular->columns() > columns)
2263                 paste_tabular->deleteColumn(columns);
2264
2265         paste_tabular->setLeftLine(0, true, true);
2266         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2267                                     true, true);
2268
2269         ostringstream os;
2270         LatexRunParams const runparams;
2271         paste_tabular->ascii(*bv->buffer(), os, runparams,
2272                              ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2273         bv->stuffClipboard(os.str());
2274         return true;
2275 }
2276
2277
2278 bool InsetTabular::pasteSelection(BufferView * bv)
2279 {
2280         if (!paste_tabular)
2281                 return false;
2282
2283         for (int r1 = 0, r2 = actrow;
2284              r1 < paste_tabular->rows() && r2 < tabular.rows();
2285              ++r1, ++r2) {
2286                 for (int c1 = 0, c2 = actcol;
2287                     c1 < paste_tabular->columns() && c2 < tabular.columns();
2288                     ++c1, ++c2) {
2289                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2290                             tabular.isPartOfMultiColumn(r2, c2))
2291                                 continue;
2292                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2293                                 --c2;
2294                                 continue;
2295                         }
2296                         if (tabular.isPartOfMultiColumn(r2, c2)) {
2297                                 --c1;
2298                                 continue;
2299                         }
2300                         InsetText & inset = tabular.getCellInset(r2, c2);
2301                         inset = paste_tabular->getCellInset(r1, c1);
2302                         inset.setOwner(this);
2303                         inset.deleteLyXText(bv);
2304                         inset.markNew();
2305                 }
2306         }
2307         return true;
2308 }
2309
2310
2311 bool InsetTabular::cutSelection(BufferParams const & bp)
2312 {
2313         if (!hasSelection())
2314                 return false;
2315
2316         int sel_col_start = tabular.column_of_cell(sel_cell_start);
2317         int sel_col_end = tabular.column_of_cell(sel_cell_end);
2318         if (sel_col_start > sel_col_end) {
2319                 sel_col_start = sel_col_end;
2320                 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2321         } else {
2322                 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2323         }
2324
2325         int sel_row_start = tabular.row_of_cell(sel_cell_start);
2326         int sel_row_end = tabular.row_of_cell(sel_cell_end);
2327
2328         if (sel_row_start > sel_row_end)
2329                 swap(sel_row_start, sel_row_end);
2330
2331         if (sel_cell_start > sel_cell_end)
2332                 swap(sel_cell_start, sel_cell_end);
2333
2334         for (int i = sel_row_start; i <= sel_row_end; ++i)
2335                 for (int j = sel_col_start; j <= sel_col_end; ++j)
2336                         tabular.getCellInset(tabular.getCellNumber(i, j))
2337                                 .clear(bp.tracking_changes);
2338         return true;
2339 }
2340
2341
2342 bool InsetTabular::isRightToLeft(BufferView * bv)
2343 {
2344         return bv->getParentLanguage(this)->RightToLeft();
2345 }
2346
2347
2348 int InsetTabular::scroll(bool recursive) const
2349 {
2350         int sx = UpdatableInset::scroll(false);
2351
2352         if (recursive && the_locking_inset)
2353                 sx += the_locking_inset->scroll(recursive);
2354
2355         return sx;
2356 }
2357
2358
2359 void InsetTabular::getSelection(int & srow, int & erow,
2360                                 int & scol, int & ecol) const
2361 {
2362         int const start = hasSelection() ? sel_cell_start : actcell;
2363         int const end = hasSelection() ? sel_cell_end : actcell;
2364
2365         srow = tabular.row_of_cell(start);
2366         erow = tabular.row_of_cell(end);
2367         if (srow > erow)
2368                 swap(srow, erow);
2369
2370         scol = tabular.column_of_cell(start);
2371         ecol = tabular.column_of_cell(end);
2372         if (scol > ecol)
2373                 swap(scol, ecol);
2374         else
2375                 ecol = tabular.right_column_of_cell(end);
2376 }
2377
2378
2379 ParagraphList * InsetTabular::getParagraphs(int i) const
2380 {
2381         return i < tabular.getNumberOfCells()
2382                 ? tabular.getCellInset(i).getParagraphs(0)
2383                 : 0;
2384 }
2385
2386
2387 int InsetTabular::numParagraphs() const
2388 {
2389         return tabular.getNumberOfCells();
2390 }
2391
2392
2393 LyXText * InsetTabular::getText(int i) const
2394 {
2395         return i < tabular.getNumberOfCells()
2396                 ?  tabular.getCellInset(i).getText(0)
2397                 : 0;
2398 }
2399
2400
2401 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2402 {
2403         if (the_locking_inset)
2404                 return the_locking_inset->cursor(bv);
2405         return InsetOld::cursor(bv);
2406 }
2407
2408
2409 void InsetTabular::markErased()
2410 {
2411         for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2412                 tabular.getCellInset(cell).markErased();
2413 }
2414
2415
2416 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2417 {
2418         if (the_locking_inset)
2419                 return the_locking_inset->insetAllowed(code);
2420         // we return true here because if the inset is not locked someone
2421         // wants to insert something in one of our insettexts and we generally
2422         // allow to do so.
2423         return true;
2424 }
2425
2426
2427 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2428 {
2429         const int cell = tabular.getCellFromInset(in);
2430
2431         if (cell != -1)
2432                 return tabular.getPWidth(cell).zero();
2433
2434         // this is a workaround for a crash (New, Insert->Tabular,
2435         // Insert->FootNote)
2436         if (!owner())
2437                 return false;
2438
2439         // well we didn't obviously find it so maybe our owner knows more
2440         BOOST_ASSERT(owner());
2441         return owner()->forceDefaultParagraphs(in);
2442 }
2443
2444
2445 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2446                                      bool usePaste)
2447 {
2448         if (buf.length() <= 0)
2449                 return true;
2450
2451         int cols = 1;
2452         int rows = 1;
2453         int maxCols = 1;
2454         string::size_type len = buf.length();
2455         string::size_type p = 0;
2456
2457         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2458                 switch (buf[p]) {
2459                 case '\t':
2460                         ++cols;
2461                         break;
2462                 case '\n':
2463                         if (p + 1 < len)
2464                                 ++rows;
2465                         maxCols = max(cols, maxCols);
2466                         cols = 1;
2467                         break;
2468                 }
2469                 ++p;
2470         }
2471         maxCols = max(cols, maxCols);
2472         LyXTabular * loctab;
2473         int cell = 0;
2474         int ocol = 0;
2475         int row = 0;
2476         if (usePaste) {
2477                 paste_tabular.reset(
2478                         new LyXTabular(bv->buffer()->params(), rows, maxCols)
2479                         );
2480
2481                 paste_tabular->setOwner(this);
2482                 loctab = paste_tabular.get();
2483                 cols = 0;
2484         } else {
2485                 loctab = &tabular;
2486                 cell = actcell;
2487                 ocol = actcol;
2488                 row = actrow;
2489         }
2490
2491         string::size_type op = 0;
2492         int cells = loctab->getNumberOfCells();
2493         p = 0;
2494         cols = ocol;
2495         rows = loctab->rows();
2496         int const columns = loctab->columns();
2497
2498         while (cell < cells && p < len && row < rows &&
2499                (p = buf.find_first_of("\t\n", p)) != string::npos)
2500         {
2501                 if (p >= len)
2502                         break;
2503                 switch (buf[p]) {
2504                 case '\t':
2505                         // we can only set this if we are not too far right
2506                         if (cols < columns) {
2507                                 InsetText & inset = loctab->getCellInset(cell);
2508                                 LyXFont const font = inset.getLyXText(bv)->
2509                                         getFont(inset.paragraphs.begin(), 0);
2510                                 inset.setText(buf.substr(op, p - op), font);
2511                                 ++cols;
2512                                 ++cell;
2513                         }
2514                         break;
2515                 case '\n':
2516                         // we can only set this if we are not too far right
2517                         if (cols < columns) {
2518                                 InsetText & inset = tabular.getCellInset(cell);
2519                                 LyXFont const font = inset.getLyXText(bv)->
2520                                         getFont(inset.paragraphs.begin(), 0);
2521                                 inset.setText(buf.substr(op, p - op), font);
2522                         }
2523                         cols = ocol;
2524                         ++row;
2525                         if (row < rows)
2526                                 cell = loctab->getCellNumber(row, cols);
2527                         break;
2528                 }
2529                 ++p;
2530                 op = p;
2531         }
2532         // check for the last cell if there is no trailing '\n'
2533         if (cell < cells && op < len) {
2534                 InsetText & inset = loctab->getCellInset(cell);
2535                 LyXFont const font = inset.getLyXText(bv)->
2536                         getFont(inset.paragraphs.begin(), 0);
2537                 inset.setText(buf.substr(op, len - op), font);
2538         }
2539
2540         return true;
2541 }
2542
2543
2544 void InsetTabular::addPreview(PreviewLoader & loader) const
2545 {
2546         int const rows = tabular.rows();
2547         int const columns = tabular.columns();
2548         for (int i = 0; i < rows; ++i)
2549                 for (int j = 0; j < columns; ++j)
2550                         tabular.getCellInset(i, j).addPreview(loader);
2551 }
2552
2553
2554 string const InsetTabularMailer::name_("tabular");
2555
2556 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2557         : inset_(const_cast<InsetTabular &>(inset))
2558 {}
2559
2560
2561 string const InsetTabularMailer::inset2string(Buffer const &) const
2562 {
2563         return params2string(inset_);
2564 }
2565
2566
2567 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2568 {
2569         istringstream data(in);
2570         LyXLex lex(0,0);
2571         lex.setStream(data);
2572
2573 #warning CHECK verify that this is a sane value to return.
2574         if (in.empty())
2575                 return -1;
2576
2577         if (lex.isOK()) {
2578                 lex.next();
2579                 string const token = lex.getString();
2580                 if (token != name_)
2581                         return -1;
2582         }
2583
2584         int cell = -1;
2585         if (lex.isOK()) {
2586                 lex.next();
2587                 string const token = lex.getString();
2588                 if (token != "\\active_cell")
2589                         return -1;
2590                 lex.next();
2591                 cell = lex.getInteger();
2592         }
2593
2594         // This is part of the inset proper that is usually swallowed
2595         // by Buffer::readInset
2596         if (lex.isOK()) {
2597                 lex.next();
2598                 string const token = lex.getString();
2599                 if (token != "Tabular")
2600                         return -1;
2601         }
2602
2603         if (!lex.isOK())
2604                 return -1;
2605
2606         Buffer const & buffer = inset.buffer();
2607         inset.read(buffer, lex);
2608
2609         // We can't set the active cell, but we can tell the frontend
2610         // what it is.
2611         return cell;
2612 }
2613
2614
2615 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2616 {
2617         Buffer const & buffer = inset.buffer();
2618
2619         ostringstream data;
2620         data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2621         inset.write(buffer, data);
2622         data << "\\end_inset\n";
2623         return data.str();
2624 }