]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
Port the tabular dialog to the new scheme based on class Dialog.
[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
14 #include "insettabular.h"
15 #include "insettext.h"
16
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "commandtags.h"
20 #include "debug.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "LaTeXFeatures.h"
25 #include "lyx_cb.h"
26 #include "lyxfunc.h"
27 #include "lyxlength.h"
28 #include "lyxlex.h"
29 #include "lyxtext.h"
30 #include "ParagraphParameters.h"
31 #include "undo_funcs.h"
32 #include "WordLangTuple.h"
33
34 #include "frontends/Alert.h"
35 #include "frontends/Dialogs.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
39
40 #include "support/LAssert.h"
41 #include "support/lstrings.h"
42
43 #include <fstream>
44 #include <algorithm>
45 #include <cstdlib>
46 #include <map>
47 //#include <signal.h>
48
49
50 using std::vector;
51 using std::ostream;
52 using std::ifstream;
53 using std::max;
54 using std::endl;
55 using std::swap;
56 using std::max;
57
58 namespace {
59
60 int const ADD_TO_HEIGHT = 2;
61 int const ADD_TO_TABULAR_WIDTH = 2;
62
63 ///
64 LyXTabular * paste_tabular = 0;
65
66
67 struct TabularFeature {
68         LyXTabular::Feature action;
69         string feature;
70 };
71
72
73 TabularFeature tabularFeature[] =
74 {
75         { LyXTabular::APPEND_ROW, "append-row" },
76         { LyXTabular::APPEND_COLUMN, "append-column" },
77         { LyXTabular::DELETE_ROW, "delete-row" },
78         { LyXTabular::DELETE_COLUMN, "delete-column" },
79         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
80         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
81         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
82         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
83         { LyXTabular::ALIGN_LEFT, "align-left" },
84         { LyXTabular::ALIGN_RIGHT, "align-right" },
85         { LyXTabular::ALIGN_CENTER, "align-center" },
86         { LyXTabular::ALIGN_BLOCK, "align-block" },
87         { LyXTabular::VALIGN_TOP, "valign-top" },
88         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
89         { LyXTabular::VALIGN_CENTER, "valign-center" },
90         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
91         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
92         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
93         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
94         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
95         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
96         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
97         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
98         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
99         { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
100         { LyXTabular::MULTICOLUMN, "multicolumn" },
101         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
102         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
103         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
104         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
105         { LyXTabular::SET_PWIDTH, "set-pwidth" },
106         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
107         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
108         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
109         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
110         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
111         { LyXTabular::SET_USEBOX, "set-usebox" },
112         { LyXTabular::SET_LTHEAD, "set-lthead" },
113         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
114         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
115         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
116         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
117         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
118         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
119         { LyXTabular::LAST_ACTION, "" }
120 };
121
122 struct FindFeature {
123         FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
124         bool operator()(TabularFeature & tf)
125         {
126                 return tf.action == feature_;
127         }
128 private:
129         LyXTabular::Feature feature_;
130 };
131
132 } // namespace anon
133
134
135 string const featureAsString(LyXTabular::Feature feature)
136 {
137         TabularFeature * it  = tabularFeature;
138         TabularFeature * end = it +
139                 sizeof(tabularFeature) / sizeof(TabularFeature);
140         it = std::find_if(it, end, FindFeature(feature));
141         return (it == end) ? string() : it->feature;
142 }
143
144
145 bool InsetTabular::hasPasteBuffer() const
146 {
147         return (paste_tabular != 0);
148 }
149
150
151 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
152         : buffer(&buf)
153 {
154         if (rows <= 0)
155                 rows = 1;
156         if (columns <= 0)
157                 columns = 1;
158         tabular.reset(new LyXTabular(buf.params, this, rows, columns));
159         // for now make it always display as display() inset
160         // just for test!!!
161         the_locking_inset = 0;
162         old_locking_inset = 0;
163         locked = false;
164         oldcell = -1;
165         actrow = actcell = 0;
166         clearSelection();
167         need_update = INIT;
168         in_update = false;
169         in_reset_pos = 0;
170         inset_x = 0;
171         inset_y = 0;
172 }
173
174
175 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
176                                                    bool same_id)
177         : UpdatableInset(tab, same_id), buffer(&buf)
178 {
179         tabular.reset(new LyXTabular(buf.params,
180                                      this, *(tab.tabular), same_id));
181         the_locking_inset = 0;
182         old_locking_inset = 0;
183         locked = false;
184         oldcell = -1;
185         actrow = actcell = 0;
186         clearSelection();
187         need_update = INIT;
188         in_update = false;
189         in_reset_pos = 0;
190         inset_x = 0;
191         inset_y = 0;
192 }
193
194
195 InsetTabular::~InsetTabular()
196 {
197         InsetTabularMailer mailer(*this);
198         mailer.hideDialog();
199 }
200
201
202 Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
203 {
204         return new InsetTabular(*this, buf, same_id);
205 }
206
207
208 BufferView * InsetTabular::view() const
209 {
210         return buffer->getUser();
211 }
212
213
214 void InsetTabular::write(Buffer const * buf, ostream & os) const
215 {
216         os << " Tabular" << endl;
217         tabular->Write(buf, os);
218 }
219
220
221 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
222 {
223         bool const old_format = (lex.getString() == "\\LyXTable");
224
225         tabular.reset(new LyXTabular(buf, this, lex));
226
227         need_update = INIT;
228
229         if (old_format)
230                 return;
231
232         lex.nextToken();
233         string token = lex.getString();
234         while (lex.isOK() && (token != "\\end_inset")) {
235                 lex.nextToken();
236                 token = lex.getString();
237         }
238         if (token != "\\end_inset") {
239                 lex.printError("Missing \\end_inset at this point. "
240                                "Read: `$$Token'");
241         }
242 }
243
244
245 int InsetTabular::ascent(BufferView *, LyXFont const &) const
246 {
247         return tabular->GetAscentOfRow(0);
248 }
249
250
251 int InsetTabular::descent(BufferView *, LyXFont const &) const
252 {
253         return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
254 }
255
256
257 int InsetTabular::width(BufferView *, LyXFont const &) const
258 {
259         return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
260 }
261
262
263 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
264                         float & x, bool cleared) const
265 {
266         if (nodraw()) {
267                 if (cleared)
268                         need_update = FULL;
269                 return;
270         }
271 #if 0
272         if (need_update == INIT) {
273                 if (calculate_dimensions_of_cells(bv, font, true))
274                         bv->text->status = LyXText::CHANGED_IN_DRAW;
275                 need_update = FULL;
276         }
277 #endif
278
279         Painter & pain = bv->painter();
280         int i;
281         int j;
282         int nx;
283
284 #if 0
285         UpdatableInset::draw(bv, font, baseline, x, cleared);
286 #else
287         if (!owner())
288                 x += static_cast<float>(scroll());
289 #endif
290         if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
291                          (top_x != int(x)) || (top_baseline != baseline)))
292         {
293                 int h = ascent(bv, font) + descent(bv, font);
294                 int const tx = display() || !owner() ? 0 : top_x;
295                 int w =  tx ? width(bv, font) : pain.paperWidth();
296                 int ty = baseline - ascent(bv, font);
297
298                 if (ty < 0)
299                         ty = 0;
300                 if ((ty + h) > pain.paperHeight())
301                         h = pain.paperHeight();
302                 if ((top_x + w) > pain.paperWidth())
303                         w = pain.paperWidth();
304                 pain.fillRectangle(tx, ty, w, h, backgroundColor());
305                 need_update = FULL;
306                 cleared = true;
307         }
308         top_x = int(x);
309         topx_set = true;
310         top_baseline = baseline;
311         x += ADD_TO_TABULAR_WIDTH;
312         if (cleared) {
313                 int cell = 0;
314                 float cx;
315                 first_visible_cell = -1;
316                 for (i = 0; i < tabular->rows(); ++i) {
317                         nx = int(x);
318                         cell = tabular->GetCellNumber(i, 0);
319                         if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
320                                 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
321                         {
322                                 baseline += tabular->GetDescentOfRow(i) +
323                                         tabular->GetAscentOfRow(i + 1) +
324                                         tabular->GetAdditionalHeight(i + 1);
325                                 continue;
326                         }
327                         for (j = 0; j < tabular->columns(); ++j) {
328                                 if (nx > bv->workWidth())
329                                         break;
330                                 if (tabular->IsPartOfMultiColumn(i, j))
331                                         continue;
332                                 cx = nx + tabular->GetBeginningOfTextInCell(cell);
333                                 if (first_visible_cell < 0)
334                                         first_visible_cell = cell;
335                                 if (hasSelection()) {
336                                         drawCellSelection(pain, nx, baseline, i, j, cell);
337                                 }
338
339                                 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx, cleared);
340                                 drawCellLines(pain, nx, baseline, i, cell);
341                                 nx += tabular->GetWidthOfColumn(cell);
342                                 ++cell;
343                         }
344                         baseline += tabular->GetDescentOfRow(i) +
345                                 tabular->GetAscentOfRow(i + 1) +
346                                 tabular->GetAdditionalHeight(i + 1);
347                 }
348         } else if (need_update == CELL) {
349                 int cell = 0;
350                 nx = int(x);
351                 if (the_locking_inset &&
352                         tabular->GetCellInset(actcell) != the_locking_inset)
353                 {
354                         Inset * inset = tabular->GetCellInset(cell);
355                         for (i = 0;
356                              inset != the_locking_inset && i < tabular->rows();
357                              ++i)
358                         {
359                                 for (j = 0;
360                                      inset != the_locking_inset && j < tabular->columns();
361                                      ++j)
362                                 {
363                                         if (tabular->IsPartOfMultiColumn(i, j))
364                                                 continue;
365                                         nx += tabular->GetWidthOfColumn(cell);
366                                         ++cell;
367                                         inset = tabular->GetCellInset(cell);
368                                 }
369                                 if (tabular->row_of_cell(cell) > i) {
370                                         nx = int(x);
371                                         baseline += tabular->GetDescentOfRow(i) +
372                                                 tabular->GetAscentOfRow(i + 1) +
373                                                 tabular->GetAdditionalHeight(i + 1);
374                                 }
375                         }
376                 } else {
377                         // compute baseline for actual row
378                         for (i = 0; i < actrow; ++i) {
379                                 baseline += tabular->GetDescentOfRow(i) +
380                                         tabular->GetAscentOfRow(i + 1) +
381                                         tabular->GetAdditionalHeight(i + 1);
382                         }
383                         // now compute the right x position
384                         cell = tabular->GetCellNumber(actrow, 0);
385                         for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
386                                         if (tabular->IsPartOfMultiColumn(actrow, j))
387                                                 continue;
388                                         nx += tabular->GetWidthOfColumn(cell);
389                                         ++cell;
390                         }
391                 }
392                 i = tabular->row_of_cell(cell);
393                 if (the_locking_inset != tabular->GetCellInset(cell)) {
394                         lyxerr[Debug::INSETTEXT] << "ERROR this shouldn't happen\n";
395                         return;
396                 }
397                 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
398                 float cx = dx;
399                 tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
400                 //
401                 // Here we use rectangular backgroundColor patches to clean up
402                 // within a cell around the cell's red inset box. As follows:
403                 //
404                 //  +--------------------+
405                 //  |         C          |   The rectangles are A, B and C
406                 //  | A |------------| B |   below, origin top left (tx, ty),
407                 //  |   |  inset box |   |   dimensions w(idth), h(eight).
408                 //  +---+------------+---+   x grows rightward, y downward
409                 //  |         D          |
410                 //  +--------------------+
411                 //
412 #if 0
413                 // clear only if we didn't have a change
414                 if (bv->text->status() != LyXText::CHANGED_IN_DRAW) {
415 #endif
416                         // clear before the inset
417                         int tx, ty, w, h;
418                         tx = nx + 1;
419                         ty = baseline - tabular->GetAscentOfRow(i) + 1;
420                         w = int(cx - nx - 1);
421                         h = tabular->GetAscentOfRow(i) +
422                                 tabular->GetDescentOfRow(i) - 1;
423                         pain.fillRectangle(tx, ty, w, h, backgroundColor());
424                         // clear behind the inset
425                         tx = int(cx + the_locking_inset->width(bv,font) + 1);
426                         ty = baseline - tabular->GetAscentOfRow(i) + 1;
427                         w = tabular->GetWidthOfColumn(cell) -
428                                 tabular->GetBeginningOfTextInCell(cell) -
429                                 the_locking_inset->width(bv,font) -
430                                 tabular->GetAdditionalWidth(cell) - 1;
431                         h = tabular->GetAscentOfRow(i) + tabular->GetDescentOfRow(i) - 1;
432                         pain.fillRectangle(tx, ty, w, h, backgroundColor());
433                         // clear below the inset
434                         tx = nx + 1;
435                         ty = baseline + the_locking_inset->descent(bv, font) + 1;
436                         w = tabular->GetWidthOfColumn(cell) -
437                                 tabular->GetAdditionalWidth(cell) - 1;
438                         h = tabular->GetDescentOfRow(i) -
439                                 the_locking_inset->descent(bv, font) - 1;
440                         pain.fillRectangle(tx, ty, w, h, backgroundColor());
441                         // clear above the inset
442                         tx = nx + 1;
443                         ty = baseline - tabular->GetAscentOfRow(i) + 1;
444                         w = tabular->GetWidthOfColumn(cell) -
445                                 tabular->GetAdditionalWidth(cell) - 1;
446                         h = tabular->GetAscentOfRow(i) - the_locking_inset->ascent(bv, font);
447                         pain.fillRectangle(tx, ty, w, h, backgroundColor());
448 #if 0
449                 }
450 #endif
451         }
452         x -= ADD_TO_TABULAR_WIDTH;
453         x += width(bv, font);
454         if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
455                 int i = 0;
456                 for(Inset * inset = owner(); inset; ++i)
457                         inset = inset->owner();
458                 if (calculate_dimensions_of_cells(bv, font, false))
459                         need_update = INIT;
460         } else {
461                 need_update = NONE;
462         }
463 }
464
465
466 void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
467                                  int row, int cell) const
468 {
469         int x2 = x + tabular->GetWidthOfColumn(cell);
470         bool on_off;
471
472         if (!tabular->topAlreadyDrawn(cell)) {
473                 on_off = !tabular->TopLine(cell);
474                 pain.line(x, baseline - tabular->GetAscentOfRow(row),
475                           x2, baseline -  tabular->GetAscentOfRow(row),
476                           on_off ? LColor::tabularonoffline : LColor::tabularline,
477                           on_off ? Painter::line_onoffdash : Painter::line_solid);
478         }
479         on_off = !tabular->BottomLine(cell);
480         pain.line(x, baseline + tabular->GetDescentOfRow(row),
481                   x2, baseline + tabular->GetDescentOfRow(row),
482                   on_off ? LColor::tabularonoffline : LColor::tabularline,
483                   on_off ? Painter::line_onoffdash : Painter::line_solid);
484         if (!tabular->leftAlreadyDrawn(cell)) {
485                 on_off = !tabular->LeftLine(cell);
486                 pain.line(x, baseline -  tabular->GetAscentOfRow(row),
487                           x, baseline +  tabular->GetDescentOfRow(row),
488                           on_off ? LColor::tabularonoffline : LColor::tabularline,
489                           on_off ? Painter::line_onoffdash : Painter::line_solid);
490         }
491         on_off = !tabular->RightLine(cell);
492         pain.line(x2 - tabular->GetAdditionalWidth(cell),
493                   baseline -  tabular->GetAscentOfRow(row),
494                   x2 - tabular->GetAdditionalWidth(cell),
495                   baseline +  tabular->GetDescentOfRow(row),
496                   on_off ? LColor::tabularonoffline : LColor::tabularline,
497                   on_off ? Painter::line_onoffdash : Painter::line_solid);
498 }
499
500
501 void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
502                                      int row, int column, int cell) const
503 {
504         lyx::Assert(hasSelection());
505         int cs = tabular->column_of_cell(sel_cell_start);
506         int ce = tabular->column_of_cell(sel_cell_end);
507         if (cs > ce) {
508                 ce = cs;
509                 cs = tabular->column_of_cell(sel_cell_end);
510         } else {
511                 ce = tabular->right_column_of_cell(sel_cell_end);
512         }
513
514         int rs = tabular->row_of_cell(sel_cell_start);
515         int re = tabular->row_of_cell(sel_cell_end);
516         if (rs > re)
517                 swap(rs, re);
518
519         if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
520                 int w = tabular->GetWidthOfColumn(cell);
521                 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row)-1;
522                 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row) + 1,
523                                    w, h, LColor::selection);
524         }
525 }
526
527
528 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
529 {
530         if (in_update) {
531                 if (reinit) {
532                         resetPos(bv);
533                         if (owner())
534                                 owner()->update(bv, font, true);
535                 }
536                 return;
537         }
538         in_update = true;
539         if (reinit) {
540                 need_update = INIT;
541                 if (calculate_dimensions_of_cells(bv, font, true))
542                         resetPos(bv);
543                 if (owner())
544                         owner()->update(bv, font, true);
545                 in_update = false;
546                 return;
547         }
548         if (the_locking_inset)
549                 the_locking_inset->update(bv, font, reinit);
550         if (need_update < FULL &&
551                 bv->text->status() == LyXText::NEED_MORE_REFRESH)
552         {
553                 need_update = FULL;
554         }
555
556         switch (need_update) {
557         case INIT:
558         case FULL:
559         case CELL:
560                 if (calculate_dimensions_of_cells(bv, font, false)) {
561                         need_update = INIT;
562                         resetPos(bv);
563                 }
564                 break;
565         case SELECTION:
566                 need_update = FULL;
567                 break;
568         default:
569                 break;
570         }
571         in_update = false;
572 }
573
574
575 string const InsetTabular::editMessage() const
576 {
577         return _("Opened Tabular Inset");
578 }
579
580
581 void InsetTabular::edit(BufferView * bv, int x, int y, mouse_button::state button)
582 {
583         UpdatableInset::edit(bv, x, y, button);
584
585         if (!bv->lockInset(this)) {
586                 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
587                 return;
588         }
589         locked = true;
590         the_locking_inset = 0;
591         inset_x = 0;
592         inset_y = 0;
593         setPos(bv, x, y);
594         clearSelection();
595         finishUndo();
596         if (insetHit(bv, x, y) && (button != mouse_button::button3)) {
597                 activateCellInsetAbs(bv, x, y, button);
598         }
599 }
600
601
602 void InsetTabular::edit(BufferView * bv, bool front)
603 {
604         UpdatableInset::edit(bv, front);
605
606         if (!bv->lockInset(this)) {
607                 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
608                 return;
609         }
610         finishUndo();
611         locked = true;
612         the_locking_inset = 0;
613         inset_x = 0;
614         inset_y = 0;
615         if (front) {
616                 if (isRightToLeft(bv))
617                         actcell = tabular->GetLastCellInRow(0);
618                 else
619                         actcell = 0;
620         } else {
621                 if (isRightToLeft(bv))
622                         actcell = tabular->GetFirstCellInRow(tabular->rows()-1);
623                 else
624                         actcell = tabular->GetNumberOfCells() - 1;
625         }
626         clearSelection();
627         resetPos(bv);
628         bv->fitCursor();
629 }
630
631
632 void InsetTabular::insetUnlock(BufferView * bv)
633 {
634         if (the_locking_inset) {
635                 the_locking_inset->insetUnlock(bv);
636                 updateLocal(bv, CELL, false);
637                 the_locking_inset = 0;
638         }
639         hideInsetCursor(bv);
640         actcell = 0;
641         oldcell = -1;
642         locked = false;
643         if (scroll(false) || hasSelection()) {
644                 clearSelection();
645                 if (scroll(false)) {
646                         scroll(bv, 0.0F);
647                 }
648                 updateLocal(bv, FULL, false);
649         }
650 }
651
652
653 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
654                                bool mark_dirty) const
655 {
656         if (what == INIT) {
657                 LyXFont font;
658                 calculate_dimensions_of_cells(bv, font, true);
659         }
660         if (!locked && what == CELL)
661                 what = FULL;
662         if (need_update < what) // only set this if it has greater update
663                 need_update = what;
664         // Dirty Cast! (Lgb)
665         if (need_update != NONE) {
666                 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
667                 if (locked)
668                         resetPos(bv);
669         }
670 }
671
672
673 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
674 {
675         lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
676                               << inset << "): ";
677         if (!inset)
678                 return false;
679         oldcell = -1;
680         if (inset == tabular->GetCellInset(actcell)) {
681                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
682                 the_locking_inset = tabular->GetCellInset(actcell);
683                 resetPos(bv);
684                 return true;
685         } else if (!the_locking_inset) {
686                 int const n = tabular->GetNumberOfCells();
687                 int const id = inset->id();
688                 for (int i = 0; i < n; ++i) {
689                         InsetText * in = tabular->GetCellInset(i);
690                         if (inset == in) {
691                                 actcell = i;
692                                 the_locking_inset = in;
693                                 locked = true;
694                                 resetPos(bv);
695                                 return true;
696                         }
697                         if (in->getInsetFromID(id)) {
698                                 actcell = i;
699                                 in->edit(bv);
700                                 return the_locking_inset->lockInsetInInset(bv, inset);
701                         }
702                 }
703         } else if (the_locking_inset && (the_locking_inset == inset)) {
704                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
705                 resetPos(bv);
706         } else if (the_locking_inset) {
707                 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
708                 return the_locking_inset->lockInsetInInset(bv, inset);
709         }
710         lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
711         return false;
712 }
713
714
715 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
716                                       bool lr)
717 {
718         if (!the_locking_inset)
719                 return false;
720         if (the_locking_inset == inset) {
721                 the_locking_inset->insetUnlock(bv);
722 #ifdef WITH_WARNINGS
723 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
724 #endif
725 #if 0
726                 if (scroll(false))
727                         scroll(bv, 0.0F);
728 #endif
729                 updateLocal(bv, CELL, false);
730                 // this has to be here otherwise we don't redraw the cell!
731                 the_locking_inset = 0;
732 //              showInsetCursor(bv, false);
733                 return true;
734         }
735         if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
736                 if (inset->lyxCode() == TABULAR_CODE &&
737                     !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
738                         InsetTabularMailer mailer(*this);
739                         mailer.updateDialog();
740                         oldcell = actcell;
741                 }
742                 return true;
743         }
744         return false;
745 }
746
747
748 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
749 {
750         Inset * tl_inset = inset;
751         // look if this inset is really inside myself!
752         while(tl_inset->owner() && tl_inset->owner() != this)
753                 tl_inset = tl_inset->owner();
754         // if we enter here it's not ower inset
755         if (!tl_inset->owner())
756                 return false;
757         // we only have to do this if this is a subinset of our cells
758         if (tl_inset != inset) {
759                 if (!static_cast<InsetText *>(tl_inset)->updateInsetInInset(bv, inset))
760                         return false;
761         }
762         updateLocal(bv, CELL, false);
763         return true;
764 }
765
766
767 int InsetTabular::insetInInsetY() const
768 {
769         if (!the_locking_inset)
770                 return 0;
771         return inset_y + the_locking_inset->insetInInsetY();
772 }
773
774
775 UpdatableInset * InsetTabular::getLockingInset() const
776 {
777         return the_locking_inset ? the_locking_inset->getLockingInset() :
778                 const_cast<InsetTabular *>(this);
779 }
780
781
782 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
783 {
784         if (c == lyxCode())
785                 return this;
786         if (the_locking_inset)
787                 return the_locking_inset->getFirstLockingInsetOfType(c);
788         return 0;
789 }
790
791
792 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
793 {
794         if (the_locking_inset)
795                 return the_locking_inset->insertInset(bv, inset);
796         return false;
797 }
798
799
800 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
801 {
802         if (hasSelection() && cmd.button() == mouse_button::button3)
803                 return;
804
805         if (hasSelection()) {
806                 clearSelection();
807                 updateLocal(cmd.view(), SELECTION, false);
808         }
809
810         int const ocell = actcell;
811         int const orow = actrow;
812         BufferView * bv = cmd.view();
813
814         hideInsetCursor(bv);
815         if (!locked) {
816                 locked = true;
817                 the_locking_inset = 0;
818                 inset_x = 0;
819                 inset_y = 0;
820         }
821         setPos(bv, cmd.x, cmd.y);
822         if (actrow != orow)
823                 updateLocal(bv, NONE, false);
824         clearSelection();
825 #if 0
826         if (cmd.button() == mouse_button::button3) {
827                 if ((ocell != actcell) && the_locking_inset) {
828                         the_locking_inset->insetUnlock(bv);
829                         updateLocal(bv, CELL, false);
830                         the_locking_inset = 0;
831                 }
832                 showInsetCursor(bv);
833                 return;
834         }
835 #endif
836
837         bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
838
839         if ((ocell == actcell) && the_locking_inset && inset_hit) {
840                 resetPos(bv);
841                 FuncRequest cmd1 = cmd;
842                 cmd1.x -= inset_x;
843                 cmd1.y -= inset_y;
844                 the_locking_inset->localDispatch(cmd1);
845                 return;
846         }
847
848         if (the_locking_inset) {
849                 the_locking_inset->insetUnlock(bv);
850                 updateLocal(bv, CELL, false);
851                 the_locking_inset = 0;
852         }
853
854         if (cmd.button() == mouse_button::button2) {
855                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
856                 return;
857         }
858
859         if (inset_hit && bv->theLockingInset()) {
860                 if (!bv->lockInset(static_cast<UpdatableInset*>
861                                 (tabular->GetCellInset(actcell))))
862                 {
863                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
864                         return;
865                 }
866                 FuncRequest cmd1 = cmd;
867                 cmd1.x -= inset_x;
868                 cmd1.y -= inset_y;
869                 the_locking_inset->localDispatch(cmd1);
870                 return;
871         }
872         showInsetCursor(bv);
873 }
874
875
876 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
877 {
878         bool ret = false;
879         if (the_locking_inset) {
880                 FuncRequest cmd1 = cmd;
881                 cmd1.x -= inset_x;
882                 cmd1.y -= inset_y;
883                 ret = the_locking_inset->localDispatch(cmd1);
884         }
885         if (cmd.button() == mouse_button::button3 && !ret) {
886                 InsetTabularMailer mailer(*this);
887                 mailer.showDialog();
888                 return true;
889         }
890         return ret;
891 }
892
893
894 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
895 {
896         if (the_locking_inset) {
897                 FuncRequest cmd1 = cmd;
898                 cmd1.x -= inset_x;
899                 cmd1.y -= inset_y;
900                 the_locking_inset->localDispatch(cmd1);
901                 return;
902         }
903
904         BufferView * bv = cmd.view();
905         hideInsetCursor(bv);
906         int const old_cell = actcell;
907
908         setPos(bv, cmd.x, cmd.y);
909         if (!hasSelection()) {
910                 setSelection(actcell, actcell);
911                 updateLocal(bv, SELECTION, false);
912         } else if (old_cell != actcell) {
913                 setSelection(sel_cell_start, actcell);
914                 updateLocal(bv, SELECTION, false);
915         }
916         showInsetCursor(bv);
917 }
918
919
920 Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
921 {
922         // We need to save the value of the_locking_inset as the call to
923         // the_locking_inset->localDispatch might unlock it.
924         old_locking_inset = the_locking_inset;
925         RESULT result = UpdatableInset::localDispatch(cmd);
926
927         BufferView * bv = cmd.view();
928         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
929                 resetPos(bv);
930                 return result;
931         }
932
933         if (cmd.action < 0 && cmd.argument.empty())
934                 return FINISHED;
935
936         bool hs = hasSelection();
937
938         result = DISPATCHED;
939         // this one have priority over the locked InsetText, if we're not already
940         // inside another tabular then that one get's priority!
941         if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) == this) {
942                 switch (cmd.action) {
943                 case LFUN_MOUSE_PRESS:
944                         lfunMousePress(cmd);
945                         return DISPATCHED;
946
947                 case LFUN_MOUSE_MOTION:
948                         lfunMouseMotion(cmd);
949                         return DISPATCHED;
950
951                 case LFUN_MOUSE_RELEASE:
952                         return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
953
954                 case LFUN_SHIFT_TAB:
955                 case LFUN_TAB:
956                         hideInsetCursor(bv);
957                         unlockInsetInInset(bv, the_locking_inset);
958                         if (cmd.action == LFUN_TAB)
959                                 moveNextCell(bv, old_locking_inset != 0);
960                         else
961                                 movePrevCell(bv, old_locking_inset != 0);
962                         clearSelection();
963                         if (hs)
964                                 updateLocal(bv, SELECTION, false);
965                         if (!the_locking_inset) {
966                                 showInsetCursor(bv);
967                                 return DISPATCHED_NOUPDATE;
968                         }
969                         return result;
970                 // this to avoid compiler warnings.
971                 default:
972                         break;
973                 }
974         }
975
976         kb_action action = cmd.action;
977         string    arg    = cmd.argument;
978         if (the_locking_inset) {
979                 result = the_locking_inset->localDispatch(cmd);
980                 if (result == DISPATCHED_NOUPDATE) {
981                         int sc = scroll();
982                         resetPos(bv);
983                         if (sc != scroll()) { // inset has been scrolled
984                                 the_locking_inset->toggleInsetCursor(bv);
985                                 updateLocal(bv, FULL, false);
986                                 the_locking_inset->toggleInsetCursor(bv);
987                         }
988                         return result;
989                 } else if (result == DISPATCHED) {
990                         the_locking_inset->toggleInsetCursor(bv);
991                         updateLocal(bv, CELL, false);
992                         the_locking_inset->toggleInsetCursor(bv);
993                         return result;
994                 } else if (result == FINISHED_UP) {
995                         action = LFUN_UP;
996                 } else if (result == FINISHED_DOWN) {
997                         action = LFUN_DOWN;
998                 } else if (result == FINISHED_RIGHT) {
999                         action = LFUN_RIGHT;
1000                 }
1001         }
1002
1003         hideInsetCursor(bv);
1004         result = DISPATCHED;
1005         switch (action) {
1006                 // --- Cursor Movements ----------------------------------
1007         case LFUN_RIGHTSEL: {
1008                 int const start = hasSelection() ? sel_cell_start : actcell;
1009                 if (tabular->IsLastCellInRow(actcell)) {
1010                         setSelection(start, actcell);
1011                         break;
1012                 }
1013
1014                 int end = actcell;
1015                 // if we are starting a selection, only select
1016                 // the current cell at the beginning
1017                 if (hasSelection()) {
1018                         moveRight(bv, false);
1019                         end = actcell;
1020                 }
1021                 setSelection(start, end);
1022                 updateLocal(bv, SELECTION, false);
1023                 break;
1024         }
1025         case LFUN_RIGHT:
1026                 result = moveRight(bv);
1027                 clearSelection();
1028                 if (hs)
1029                         updateLocal(bv, SELECTION, false);
1030                 break;
1031         case LFUN_LEFTSEL: {
1032                 int const start = hasSelection() ? sel_cell_start : actcell;
1033                 if (tabular->IsFirstCellInRow(actcell)) {
1034                         setSelection(start, actcell);
1035                         break;
1036                 }
1037
1038                 int end = actcell;
1039                 // if we are starting a selection, only select
1040                 // the current cell at the beginning
1041                 if (hasSelection()) {
1042                         moveLeft(bv, false);
1043                         end = actcell;
1044                 }
1045                 setSelection(start, end);
1046                 updateLocal(bv, SELECTION, false);
1047                 break;
1048         }
1049         case LFUN_LEFT:
1050                 result = moveLeft(bv);
1051                 clearSelection();
1052                 if (hs)
1053                         updateLocal(bv, SELECTION, false);
1054                 break;
1055         case LFUN_DOWNSEL: {
1056                 int const start = hasSelection() ? sel_cell_start : actcell;
1057                 int const ocell = actcell;
1058                 // if we are starting a selection, only select
1059                 // the current cell at the beginning
1060                 if (hasSelection()) {
1061                         moveDown(bv, false);
1062                         if ((ocell == sel_cell_end) ||
1063                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1064                                 setSelection(start, tabular->GetCellBelow(sel_cell_end));
1065                         else
1066                                 setSelection(start, tabular->GetLastCellBelow(sel_cell_end));
1067                 } else {
1068                         setSelection(start, start);
1069                 }
1070                 updateLocal(bv, SELECTION, false);
1071         }
1072         break;
1073         case LFUN_DOWN:
1074                 result = moveDown(bv, old_locking_inset != 0);
1075                 clearSelection();
1076                 if (hs) {
1077                         updateLocal(bv, SELECTION, false);
1078                 }
1079                 break;
1080         case LFUN_UPSEL: {
1081                 int const start = hasSelection() ? sel_cell_start : actcell;
1082                 int const ocell = actcell;
1083                 // if we are starting a selection, only select
1084                 // the current cell at the beginning
1085                 if (hasSelection()) {
1086                         moveUp(bv, false);
1087                         if ((ocell == sel_cell_end) ||
1088                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1089                                 setSelection(start, tabular->GetCellAbove(sel_cell_end));
1090                         else
1091                                 setSelection(start, tabular->GetLastCellAbove(sel_cell_end));
1092                 } else {
1093                         setSelection(start, start);
1094                 }
1095                 updateLocal(bv, SELECTION, false);
1096         }
1097         break;
1098         case LFUN_UP:
1099                 result = moveUp(bv, old_locking_inset != 0);
1100                 clearSelection();
1101                 if (hs)
1102                         updateLocal(bv, SELECTION, false);
1103                 break;
1104         case LFUN_NEXT: {
1105                 UpdateCodes code = CURSOR;
1106                 if (hs) {
1107                         clearSelection();
1108                         code = SELECTION;
1109                 }
1110                 int column = actcol;
1111                 unlockInsetInInset(bv, the_locking_inset);
1112                 if (bv->text->top_y() + bv->painter().paperHeight() <
1113                     (top_baseline + tabular->GetHeightOfTabular()))
1114                         {
1115                                 bv->scrollDocView(bv->text->top_y() + bv->painter().paperHeight());
1116                                 code = FULL;
1117                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1118                         } else {
1119                                 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
1120                         }
1121                 resetPos(bv);
1122                 updateLocal(bv, code, false);
1123                 break;
1124         }
1125         case LFUN_PRIOR: {
1126                 UpdateCodes code = CURSOR;
1127                 if (hs) {
1128                         clearSelection();
1129                         code = SELECTION;
1130                 }
1131                 int column = actcol;
1132                 unlockInsetInInset(bv, the_locking_inset);
1133                 if (top_baseline < 0) {
1134                         bv->scrollDocView(bv->text->top_y() - bv->painter().paperHeight());
1135                         code = FULL;
1136                         if (top_baseline > 0)
1137                                 actcell = column;
1138                         else
1139                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1140                 } else {
1141                         actcell = column;
1142                 }
1143                 resetPos(bv);
1144                 updateLocal(bv, code, false);
1145                 break;
1146         }
1147         // none of these make sense for insettabular,
1148         // but we must catch them to prevent any
1149         // selection from being confused
1150         case LFUN_PRIORSEL:
1151         case LFUN_NEXTSEL:
1152         case LFUN_WORDLEFT:
1153         case LFUN_WORDLEFTSEL:
1154         case LFUN_WORDRIGHT:
1155         case LFUN_WORDRIGHTSEL:
1156         case LFUN_WORDSEL:
1157         case LFUN_DOWN_PARAGRAPH:
1158         case LFUN_DOWN_PARAGRAPHSEL:
1159         case LFUN_UP_PARAGRAPH:
1160         case LFUN_UP_PARAGRAPHSEL:
1161         case LFUN_BACKSPACE:
1162         case LFUN_HOME:
1163         case LFUN_HOMESEL:
1164         case LFUN_END:
1165         case LFUN_ENDSEL:
1166         case LFUN_BEGINNINGBUF:
1167         case LFUN_BEGINNINGBUFSEL:
1168         case LFUN_ENDBUF:
1169         case LFUN_ENDBUFSEL:
1170                 break;
1171         case LFUN_LAYOUT_TABULAR: {
1172                 InsetTabularMailer mailer(*this);
1173                 mailer.showDialog();
1174                 break;
1175         }
1176         case LFUN_INSET_DIALOG_UPDATE: {
1177                 InsetTabularMailer mailer(*this);
1178                 mailer.updateDialog();
1179                 break;
1180         }
1181         case LFUN_TABULAR_FEATURE:
1182                 if (!tabularFeatures(bv, arg))
1183                         result = UNDISPATCHED;
1184                 break;
1185                 // insert file functions
1186         case LFUN_FILE_INSERT_ASCII_PARA:
1187         case LFUN_FILE_INSERT_ASCII:
1188         {
1189                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
1190                 if (tmpstr.empty())
1191                         break;
1192                 if (insertAsciiString(bv, tmpstr, false))
1193                         updateLocal(bv, INIT, true);
1194                 else
1195                         result = UNDISPATCHED;
1196                 break;
1197         }
1198         // cut and paste functions
1199         case LFUN_CUT:
1200                 if (!copySelection(bv))
1201                         break;
1202                 // no break here!
1203         case LFUN_DELETE:
1204                 setUndo(bv, Undo::DELETE,
1205                         bv->text->cursor.par(),
1206                         bv->text->cursor.par()->next());
1207                 cutSelection(bv->buffer()->params);
1208                 updateLocal(bv, INIT, true);
1209                 break;
1210         case LFUN_COPY:
1211                 if (!hasSelection())
1212                         break;
1213                 finishUndo();
1214                 copySelection(bv);
1215                 break;
1216         case LFUN_PASTESELECTION:
1217         {
1218                 string const clip(bv->getClipboard());
1219                         if (clip.empty())
1220                         break;
1221 #if 0
1222                 if (clip.find('\t') != string::npos) {
1223                         int cols = 1;
1224                         int rows = 1;
1225                         int maxCols = 1;
1226                         string::size_type len = clip.length();
1227                         string::size_type p = 0;
1228
1229                         while (p < len &&
1230                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1231                                 switch (clip[p]) {
1232                                 case '\t':
1233                                         ++cols;
1234                                         break;
1235                                 case '\n':
1236                                         if ((p+1) < len)
1237                                                 ++rows;
1238                                         maxCols = max(cols, maxCols);
1239                                         cols = 1;
1240                                         break;
1241                                 }
1242                                 ++p;
1243                         }
1244                         maxCols = max(cols, maxCols);
1245                         delete paste_tabular;
1246                         paste_tabular = new LyXTabular(bv->buffer()->params,
1247                                                        this, rows, maxCols);
1248                         string::size_type op = 0;
1249                         int cell = 0;
1250                         int cells = paste_tabular->GetNumberOfCells();
1251                         p = cols = 0;
1252                         while ((cell < cells) && (p < len) &&
1253                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1254                                 if (p >= len)
1255                                         break;
1256                                 switch (clip[p]) {
1257                                 case '\t':
1258                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1259                                         ++cols;
1260                                         ++cell;
1261                                         break;
1262                                 case '\n':
1263                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1264                                         while (cols++ < maxCols)
1265                                                 ++cell;
1266                                         cols = 0;
1267                                         break;
1268                                 }
1269                                 ++p;
1270                                 op = p;
1271                         }
1272                         // check for the last cell if there is no trailing '\n'
1273                         if ((cell < cells) && (op < len))
1274                                 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1275                 } else
1276 #else
1277                 if (!insertAsciiString(bv, clip, true))
1278 #endif
1279                 {
1280                         // so that the clipboard is used and it goes on
1281                         // to default
1282                         // and executes LFUN_PASTESELECTION in insettext!
1283                         delete paste_tabular;
1284                         paste_tabular = 0;
1285                 }
1286         }
1287         case LFUN_PASTE:
1288                 if (hasPasteBuffer()) {
1289                         setUndo(bv, Undo::INSERT,
1290                                 bv->text->cursor.par(),
1291                                 bv->text->cursor.par()->next());
1292                         pasteSelection(bv);
1293                         updateLocal(bv, INIT, true);
1294                         break;
1295                 }
1296                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1297         default:
1298                 // handle font changing stuff on selection before we lock the inset
1299                 // in the default part!
1300                 result = UNDISPATCHED;
1301                 if (hs) {
1302                         switch(action) {
1303                         case LFUN_LANGUAGE:
1304                         case LFUN_EMPH:
1305                         case LFUN_BOLD:
1306                         case LFUN_NOUN:
1307                         case LFUN_CODE:
1308                         case LFUN_SANS:
1309                         case LFUN_ROMAN:
1310                         case LFUN_DEFAULT:
1311                         case LFUN_UNDERLINE:
1312                         case LFUN_FONT_SIZE:
1313                                 if (bv->dispatch(FuncRequest(bv, action, arg)))
1314                                         result = DISPATCHED;
1315                                 break;
1316                         default:
1317                                 break;
1318                         }
1319                 }
1320                 // we try to activate the actual inset and put this event down to
1321                 // the insets dispatch function.
1322                 if ((result == DISPATCHED) || the_locking_inset)
1323                         break;
1324                 nodraw(true);
1325                 if (activateCellInset(bv)) {
1326                         // reset need_update setted in above function!
1327                         need_update = NONE;
1328                         result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1329                         if ((result == UNDISPATCHED) || (result >= FINISHED)) {
1330                                 unlockInsetInInset(bv, the_locking_inset);
1331                                 nodraw(false);
1332                                 // we need to update if this was requested before
1333                                 updateLocal(bv, NONE, false);
1334                                 return UNDISPATCHED;
1335                         } else if (hs) {
1336                                 clearSelection();
1337                                 // so the below CELL is not set because this is higher
1338                                 // priority and we get a full redraw
1339                                 need_update = SELECTION;
1340                         }
1341                         nodraw(false);
1342                         updateLocal(bv, CELL, false);
1343                         return result;
1344                 }
1345                 break;
1346         }
1347         if (result < FINISHED) {
1348                 if (!the_locking_inset) {
1349                         if (bv->fitCursor())
1350                                 updateLocal(bv, FULL, false);
1351                         if (locked)
1352                                 showInsetCursor(bv);
1353                 }
1354         } else
1355                 bv->unlockInset(this);
1356         return result;
1357 }
1358
1359
1360 int InsetTabular::latex(Buffer const * buf, ostream & os,
1361                         bool fragile, bool fp) const
1362 {
1363         return tabular->latex(buf, os, fragile, fp);
1364 }
1365
1366
1367 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1368 {
1369         if (ll > 0)
1370                 return tabular->ascii(buf, os, (int)parOwner()->params().depth(),
1371                                       false,0);
1372         return tabular->ascii(buf, os, 0, false,0);
1373 }
1374
1375
1376 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1377 {
1378         os << "<![CDATA[";
1379         int const ret = tabular->ascii(buf,os,
1380                                        (int)parOwner()->params().depth(),
1381                                        false, 0);
1382         os << "]]>";
1383         return ret;
1384 }
1385
1386
1387 int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1388 {
1389         int ret = 0;
1390         Inset * master;
1391
1392         // if the table is inside a float it doesn't need the informaltable
1393         // wrapper. Search for it.
1394         for(master = owner();
1395             master && master->lyxCode() != Inset::FLOAT_CODE;
1396             master = master->owner());
1397
1398         if (!master) {
1399                 os << "<informaltable>";
1400                 if (mixcont)
1401                         os << endl;
1402                 ret++;
1403         }
1404         ret+= tabular->docbook(buf, os, mixcont);
1405         if (!master) {
1406                 os << "</informaltable>";
1407                 if (mixcont)
1408                         os << endl;
1409                 ret++;
1410         }
1411         return ret;
1412 }
1413
1414
1415 void InsetTabular::validate(LaTeXFeatures & features) const
1416 {
1417         tabular->Validate(features);
1418 }
1419
1420
1421 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1422                                                  LyXFont const & font,
1423                                                  bool reinit) const
1424 {
1425         int cell = -1;
1426         int maxAsc = 0;
1427         int maxDesc = 0;
1428         InsetText * inset;
1429         bool changed = false;
1430
1431         // if we have a locking_inset we should have to check only this cell for
1432         // change so I'll try this to have a boost, but who knows ;)
1433         if ((need_update != INIT) &&
1434             (the_locking_inset == tabular->GetCellInset(actcell))) {
1435                 for(int i = 0; i < tabular->columns(); ++i) {
1436                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1437                                      maxAsc);
1438                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1439                                       maxDesc);
1440                 }
1441                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1442                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1443                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1444                 return changed;
1445         }
1446         for (int i = 0; i < tabular->rows(); ++i) {
1447                 maxAsc = 0;
1448                 maxDesc = 0;
1449                 for (int j = 0; j < tabular->columns(); ++j) {
1450                         if (tabular->IsPartOfMultiColumn(i,j))
1451                                 continue;
1452                         ++cell;
1453                         inset = tabular->GetCellInset(cell);
1454                         if (!reinit && !tabular->GetPWidth(cell).zero())
1455                                 inset->update(bv, font, false);
1456                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1457                         maxDesc = max(maxDesc, inset->descent(bv, font));
1458                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1459                 }
1460                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1461                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1462         }
1463         if (changed)
1464                 tabular->reinit();
1465         return changed;
1466 }
1467
1468
1469 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1470 {
1471         if (the_locking_inset) {
1472                 the_locking_inset->getCursorPos(bv, x, y);
1473                 return;
1474         }
1475         x = cursor_.x() - top_x;
1476         y = cursor_.y();
1477 }
1478
1479
1480 void InsetTabular::toggleInsetCursor(BufferView * bv)
1481 {
1482         if (nodraw()) {
1483                 if (isCursorVisible())
1484                         bv->hideLockedInsetCursor();
1485                 return;
1486         }
1487         if (the_locking_inset) {
1488                 the_locking_inset->toggleInsetCursor(bv);
1489                 return;
1490         }
1491
1492         LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1493
1494         int const asc = font_metrics::maxAscent(font);
1495         int const desc = font_metrics::maxDescent(font);
1496
1497         if (isCursorVisible())
1498                 bv->hideLockedInsetCursor();
1499         else
1500                 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1501         toggleCursorVisible();
1502 }
1503
1504
1505 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1506 {
1507         if (nodraw())
1508                 return;
1509         if (!isCursorVisible()) {
1510                 LyXFont font; // = GetFont(par, cursor.pos);
1511
1512                 int const asc = font_metrics::maxAscent(font);
1513                 int const desc = font_metrics::maxDescent(font);
1514                 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1515                 if (show)
1516                         bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1517                 setCursorVisible(true);
1518         }
1519 }
1520
1521
1522 void InsetTabular::hideInsetCursor(BufferView * bv)
1523 {
1524         if (isCursorVisible()) {
1525                 bv->hideLockedInsetCursor();
1526                 setCursorVisible(false);
1527         }
1528 }
1529
1530
1531 void InsetTabular::fitInsetCursor(BufferView * bv) const
1532 {
1533         if (the_locking_inset) {
1534                 int old_top_y = bv->text->top_y();
1535                 the_locking_inset->fitInsetCursor(bv);
1536                 if (old_top_y != bv->text->top_y())
1537                         need_update = FULL;
1538                 return;
1539         }
1540         LyXFont font;
1541
1542         int const asc = font_metrics::maxAscent(font);
1543         int const desc = font_metrics::maxDescent(font);
1544         resetPos(bv);
1545
1546         if (bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc))
1547                 need_update = FULL;
1548 }
1549
1550
1551 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1552 {
1553         cursor_.y(0);
1554
1555         actcell = actrow = actcol = 0;
1556         int ly = tabular->GetDescentOfRow(actrow);
1557
1558         // first search the right row
1559         while ((ly < y) && ((actrow+1) < tabular->rows())) {
1560                 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1561                                  tabular->GetAscentOfRow(actrow + 1) +
1562                                  tabular->GetAdditionalHeight(actrow + 1));
1563                 ++actrow;
1564                 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1565         }
1566         actcell = tabular->GetCellNumber(actrow, actcol);
1567
1568         // now search the right column
1569         int lx = tabular->GetWidthOfColumn(actcell) -
1570                 tabular->GetAdditionalWidth(actcell);
1571         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1572                 lx += tabular->GetWidthOfColumn(actcell + 1)
1573                         + tabular->GetAdditionalWidth(actcell);
1574         }
1575         cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1576         resetPos(bv);
1577 }
1578
1579
1580 int InsetTabular::getCellXPos(int cell) const
1581 {
1582         int c = cell;
1583
1584         for (; !tabular->IsFirstCellInRow(c); --c)
1585                 ;
1586         int lx = tabular->GetWidthOfColumn(cell);
1587         for (; c < cell; ++c) {
1588                 lx += tabular->GetWidthOfColumn(c);
1589         }
1590         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1591 }
1592
1593
1594 void InsetTabular::resetPos(BufferView * bv) const
1595 {
1596 #ifdef WITH_WARNINGS
1597 #warning This should be fixed in the right manner (20011128 Jug)
1598 #endif
1599         // fast hack to fix infinite repaintings!
1600         if (in_reset_pos > 0)
1601                 return;
1602
1603         int cell = 0;
1604         actcol = tabular->column_of_cell(actcell);
1605         actrow = 0;
1606         cursor_.y(0);
1607         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1608                 if (tabular->IsLastCellInRow(cell)) {
1609                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1610                                          tabular->GetAscentOfRow(actrow + 1) +
1611                                          tabular->GetAdditionalHeight(actrow + 1));
1612                         ++actrow;
1613                 }
1614         }
1615         if (!locked || nodraw()) {
1616                 if (the_locking_inset)
1617                         inset_y = cursor_.y();
1618                 return;
1619         }
1620         // we need this only from here on!!!
1621         ++in_reset_pos;
1622         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1623         int new_x = getCellXPos(actcell);
1624         int old_x = cursor_.x();
1625         new_x += offset;
1626         cursor_.x(new_x);
1627 //    cursor.x(getCellXPos(actcell) + offset);
1628         if ((actcol < tabular->columns() - 1) && scroll(false) &&
1629                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1630         {
1631                 scroll(bv, 0.0F);
1632                 updateLocal(bv, FULL, false);
1633         } else if (the_locking_inset &&
1634                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1635         {
1636                 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1637                 if (xx > (bv->workWidth()-20)) {
1638                         scroll(bv, -(xx - bv->workWidth() + 60));
1639                         updateLocal(bv, FULL, false);
1640                 } else if (xx < 20) {
1641                         if (xx < 0)
1642                                 xx = -xx + 60;
1643                         else
1644                                 xx = 60;
1645                         scroll(bv, xx);
1646                         updateLocal(bv, FULL, false);
1647                 }
1648         } else if ((cursor_.x() - offset) > 20 &&
1649                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1650                    > (bv->workWidth() - 20)) {
1651                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1652                 updateLocal(bv, FULL, false);
1653         } else if ((cursor_.x() - offset) < 20) {
1654                 scroll(bv, 20 - cursor_.x() + offset);
1655                 updateLocal(bv, FULL, false);
1656         } else if (scroll() && top_x > 20 &&
1657                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1658                 scroll(bv, old_x - cursor_.x());
1659                 updateLocal(bv, FULL, false);
1660         }
1661         if (the_locking_inset) {
1662                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1663                 inset_y = cursor_.y();
1664         }
1665         if ((!the_locking_inset ||
1666              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1667             actcell != oldcell) {
1668                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1669                 InsetTabularMailer mailer(*inset);
1670                 mailer.updateDialog();
1671                 oldcell = actcell;
1672         }
1673         in_reset_pos = 0;
1674 }
1675
1676
1677 Inset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1678 {
1679         if (lock && !old_locking_inset) {
1680                 if (activateCellInset(bv))
1681                         return DISPATCHED;
1682         } else {
1683                 bool moved = isRightToLeft(bv)
1684                         ? movePrevCell(bv) : moveNextCell(bv);
1685                 if (!moved)
1686                         return FINISHED_RIGHT;
1687                 if (lock && activateCellInset(bv))
1688                         return DISPATCHED;
1689         }
1690         resetPos(bv);
1691         return DISPATCHED_NOUPDATE;
1692 }
1693
1694
1695 Inset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1696 {
1697         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1698         if (!moved)
1699                 return FINISHED;
1700         if (lock) {       // behind the inset
1701                 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1702                         return DISPATCHED;
1703         }
1704         resetPos(bv);
1705         return DISPATCHED_NOUPDATE;
1706 }
1707
1708
1709 Inset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1710 {
1711         int const ocell = actcell;
1712         actcell = tabular->GetCellAbove(actcell);
1713         if (actcell == ocell) // we moved out of the inset
1714                 return FINISHED_UP;
1715         resetPos(bv);
1716         if (lock) {
1717                 int x = 0;
1718                 int y = 0;
1719                 if (old_locking_inset) {
1720                         old_locking_inset->getCursorPos(bv, x, y);
1721                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1722                 }
1723                 if (activateCellInset(bv, x, 0))
1724                         return DISPATCHED;
1725         }
1726         return DISPATCHED_NOUPDATE;
1727 }
1728
1729
1730 Inset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1731 {
1732         int const ocell = actcell;
1733         actcell = tabular->GetCellBelow(actcell);
1734         if (actcell == ocell) // we moved out of the inset
1735                 return FINISHED_DOWN;
1736         resetPos(bv);
1737         if (lock) {
1738                 int x = 0;
1739                 int y = 0;
1740                 if (old_locking_inset) {
1741                         old_locking_inset->getCursorPos(bv, x, y);
1742                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1743                 }
1744                 if (activateCellInset(bv, x, 0))
1745                         return DISPATCHED;
1746         }
1747         return DISPATCHED_NOUPDATE;
1748 }
1749
1750
1751 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1752 {
1753         if (isRightToLeft(bv)) {
1754                 if (tabular->IsFirstCellInRow(actcell)) {
1755                         int row = tabular->row_of_cell(actcell);
1756                         if (row == tabular->rows() - 1)
1757                                 return false;
1758                         actcell = tabular->GetLastCellInRow(row);
1759                         actcell = tabular->GetCellBelow(actcell);
1760                 } else {
1761                         if (!actcell)
1762                                 return false;
1763                         --actcell;
1764                 }
1765         } else {
1766                 if (tabular->IsLastCell(actcell))
1767                         return false;
1768                 ++actcell;
1769         }
1770         if (lock) {
1771                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1772                         isRightToLeftPar(bv->buffer()->params);
1773                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1774         }
1775         resetPos(bv);
1776         return true;
1777 }
1778
1779
1780 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1781 {
1782         if (isRightToLeft(bv)) {
1783                 if (tabular->IsLastCellInRow(actcell)) {
1784                         int row = tabular->row_of_cell(actcell);
1785                         if (row == 0)
1786                                 return false;
1787                         actcell = tabular->GetFirstCellInRow(row);
1788                         actcell = tabular->GetCellAbove(actcell);
1789                 } else {
1790                         if (tabular->IsLastCell(actcell))
1791                                 return false;
1792                         ++actcell;
1793                 }
1794         } else {
1795                 if (!actcell) // first cell
1796                         return false;
1797                 --actcell;
1798         }
1799         if (lock) {
1800                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1801                         isRightToLeftPar(bv->buffer()->params);
1802                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1803         }
1804         resetPos(bv);
1805         return true;
1806 }
1807
1808
1809 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1810                            bool selectall)
1811 {
1812         if (selectall) {
1813                 setSelection(0, tabular->GetNumberOfCells() - 1);
1814         }
1815         if (hasSelection()) {
1816                 setUndo(bv, Undo::EDIT,
1817                         bv->text->cursor.par(),
1818                         bv->text->cursor.par()->next());
1819                 bool const frozen = undo_frozen;
1820                 if (!frozen)
1821                         freezeUndo();
1822                 // apply the fontchange on the whole selection
1823                 int sel_row_start;
1824                 int sel_row_end;
1825                 int sel_col_start;
1826                 int sel_col_end;
1827                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1828                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1829                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1830                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1831                         }
1832                 }
1833                 if (!frozen)
1834                         unFreezeUndo();
1835                 if (selectall)
1836                         clearSelection();
1837                 updateLocal(bv, INIT, true);
1838         }
1839         if (the_locking_inset)
1840                 the_locking_inset->setFont(bv, font, tall);
1841 }
1842
1843
1844 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1845 {
1846         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1847
1848         int i = 0;
1849         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1850                 string const tmp = tabularFeature[i].feature;
1851
1852                 if (tmp == what.substr(0, tmp.length())) {
1853                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1854                         //tabularFeatures[i].feature.length())) {
1855                         action = tabularFeature[i].action;
1856                         break;
1857                 }
1858         }
1859         if (action == LyXTabular::LAST_ACTION)
1860                 return false;
1861
1862         string const val =
1863                 ltrim(what.substr(tabularFeature[i].feature.length()));
1864         tabularFeatures(bv, action, val);
1865         return true;
1866 }
1867
1868 namespace {
1869
1870 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1871                           string const & special, bool & flag)
1872 {
1873         if (special == "dl_above") {
1874                 ltt.topDL = flag;
1875                 ltt.set = false;
1876         } else if (special == "dl_below") {
1877                 ltt.bottomDL = flag;
1878                 ltt.set = false;
1879         } else if (special == "empty") {
1880                 ltt.empty = flag;
1881                 ltt.set = false;
1882         } else if (flag) {
1883                 ltt.empty = false;
1884                 ltt.set = true;
1885         }
1886 }
1887
1888 }
1889
1890
1891 void InsetTabular::tabularFeatures(BufferView * bv,
1892                                    LyXTabular::Feature feature,
1893                                    string const & value)
1894 {
1895         int sel_col_start;
1896         int sel_col_end;
1897         int sel_row_start;
1898         int sel_row_end;
1899         bool setLines = false;
1900         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1901         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1902
1903         switch (feature) {
1904         case LyXTabular::M_ALIGN_LEFT:
1905         case LyXTabular::ALIGN_LEFT:
1906                 setAlign = LYX_ALIGN_LEFT;
1907                 break;
1908         case LyXTabular::M_ALIGN_RIGHT:
1909         case LyXTabular::ALIGN_RIGHT:
1910                 setAlign = LYX_ALIGN_RIGHT;
1911                 break;
1912         case LyXTabular::M_ALIGN_CENTER:
1913         case LyXTabular::ALIGN_CENTER:
1914                 setAlign = LYX_ALIGN_CENTER;
1915                 break;
1916         case LyXTabular::ALIGN_BLOCK:
1917                 setAlign = LYX_ALIGN_BLOCK;
1918                 break;
1919         case LyXTabular::M_VALIGN_TOP:
1920         case LyXTabular::VALIGN_TOP:
1921                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1922                 break;
1923         case LyXTabular::M_VALIGN_BOTTOM:
1924         case LyXTabular::VALIGN_BOTTOM:
1925                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1926                 break;
1927         case LyXTabular::M_VALIGN_CENTER:
1928         case LyXTabular::VALIGN_CENTER:
1929                 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1930                 break;
1931         default:
1932                 break;
1933         }
1934         if (hasSelection()) {
1935                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1936         } else {
1937                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1938                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1939         }
1940         setUndo(bv, Undo::FINISH,
1941                 bv->text->cursor.par(),
1942                 bv->text->cursor.par()->next());
1943
1944         int row =  tabular->row_of_cell(actcell);
1945         int column = tabular->column_of_cell(actcell);
1946         bool flag = true;
1947         LyXTabular::ltType ltt;
1948
1949         switch (feature) {
1950         case LyXTabular::SET_PWIDTH:
1951         {
1952                 LyXLength const vallen(value);
1953                 LyXLength const & tmplen = tabular->GetColumnPWidth(actcell);
1954
1955                 bool const update = (tmplen != vallen);
1956                 tabular->SetColumnPWidth(actcell, vallen);
1957                 if (update) {
1958                         int cell;
1959                         for (int i = 0; i < tabular->rows(); ++i) {
1960                                 cell = tabular->GetCellNumber(i,column);
1961                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1962                         }
1963                         updateLocal(bv, INIT, true);
1964                 }
1965
1966                 if (vallen.zero()
1967                     && tabular->GetAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1968                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1969                 else if (!vallen.zero()
1970                          && tabular->GetAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1971                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1972         }
1973         break;
1974         case LyXTabular::SET_MPWIDTH:
1975         {
1976                 LyXLength const vallen(value);
1977                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1978
1979                 bool const update = (tmplen != vallen);
1980                 tabular->SetMColumnPWidth(actcell, vallen);
1981                 if (update) {
1982                         for (int i = 0; i < tabular->rows(); ++i) {
1983                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1984                                         resizeLyXText(bv);
1985                         }
1986                         updateLocal(bv, INIT, true);
1987                 }
1988         }
1989         break;
1990         case LyXTabular::SET_SPECIAL_COLUMN:
1991         case LyXTabular::SET_SPECIAL_MULTI:
1992                 tabular->SetAlignSpecial(actcell,value,feature);
1993                 updateLocal(bv, FULL, true);
1994                 break;
1995         case LyXTabular::APPEND_ROW:
1996                 // append the row into the tabular
1997                 unlockInsetInInset(bv, the_locking_inset);
1998                 tabular->AppendRow(bv->buffer()->params, actcell);
1999                 updateLocal(bv, INIT, true);
2000                 break;
2001         case LyXTabular::APPEND_COLUMN:
2002                 // append the column into the tabular
2003                 unlockInsetInInset(bv, the_locking_inset);
2004                 tabular->AppendColumn(bv->buffer()->params, actcell);
2005                 actcell = tabular->GetCellNumber(row, column);
2006                 updateLocal(bv, INIT, true);
2007                 break;
2008         case LyXTabular::DELETE_ROW:
2009                 unlockInsetInInset(bv, the_locking_inset);
2010                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
2011                         tabular->DeleteRow(sel_row_start);
2012                 }
2013                 if (sel_row_start >= tabular->rows())
2014                         --sel_row_start;
2015                 actcell = tabular->GetCellNumber(sel_row_start, column);
2016                 clearSelection();
2017                 updateLocal(bv, INIT, true);
2018                 break;
2019         case LyXTabular::DELETE_COLUMN:
2020                 unlockInsetInInset(bv, the_locking_inset);
2021                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
2022                         tabular->DeleteColumn(sel_col_start);
2023                 }
2024                 if (sel_col_start >= tabular->columns())
2025                         --sel_col_start;
2026                 actcell = tabular->GetCellNumber(row, sel_col_start);
2027                 clearSelection();
2028                 updateLocal(bv, INIT, true);
2029                 break;
2030         case LyXTabular::M_TOGGLE_LINE_TOP:
2031                 flag = false;
2032         case LyXTabular::TOGGLE_LINE_TOP:
2033         {
2034                 bool lineSet = !tabular->TopLine(actcell, flag);
2035                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2036                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2037                                 tabular->SetTopLine(
2038                                         tabular->GetCellNumber(i, j),
2039                                         lineSet, flag);
2040                 updateLocal(bv, INIT, true);
2041                 break;
2042         }
2043
2044         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2045                 flag = false;
2046         case LyXTabular::TOGGLE_LINE_BOTTOM:
2047         {
2048                 bool lineSet = !tabular->BottomLine(actcell, flag);
2049                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2050                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2051                                 tabular->SetBottomLine(
2052                                         tabular->GetCellNumber(i, j),
2053                                         lineSet,
2054                                         flag);
2055                 updateLocal(bv, INIT, true);
2056                 break;
2057         }
2058
2059         case LyXTabular::M_TOGGLE_LINE_LEFT:
2060                 flag = false;
2061         case LyXTabular::TOGGLE_LINE_LEFT:
2062         {
2063                 bool lineSet = !tabular->LeftLine(actcell, flag);
2064                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2065                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2066                                 tabular->SetLeftLine(
2067                                         tabular->GetCellNumber(i,j),
2068                                         lineSet,
2069                                         flag);
2070                 updateLocal(bv, INIT, true);
2071                 break;
2072         }
2073
2074         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2075                 flag = false;
2076         case LyXTabular::TOGGLE_LINE_RIGHT:
2077         {
2078                 bool lineSet = !tabular->RightLine(actcell, flag);
2079                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2080                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2081                                 tabular->SetRightLine(
2082                                         tabular->GetCellNumber(i,j),
2083                                         lineSet,
2084                                         flag);
2085                 updateLocal(bv, INIT, true);
2086                 break;
2087         }
2088
2089         case LyXTabular::M_ALIGN_LEFT:
2090         case LyXTabular::M_ALIGN_RIGHT:
2091         case LyXTabular::M_ALIGN_CENTER:
2092                 flag = false;
2093         case LyXTabular::ALIGN_LEFT:
2094         case LyXTabular::ALIGN_RIGHT:
2095         case LyXTabular::ALIGN_CENTER:
2096         case LyXTabular::ALIGN_BLOCK:
2097                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2098                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2099                                 tabular->SetAlignment(
2100                                         tabular->GetCellNumber(i, j),
2101                                         setAlign,
2102                                         flag);
2103                 updateLocal(bv, INIT, true);
2104                 break;
2105         case LyXTabular::M_VALIGN_TOP:
2106         case LyXTabular::M_VALIGN_BOTTOM:
2107         case LyXTabular::M_VALIGN_CENTER:
2108                 flag = false;
2109         case LyXTabular::VALIGN_TOP:
2110         case LyXTabular::VALIGN_BOTTOM:
2111         case LyXTabular::VALIGN_CENTER:
2112                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2113                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2114                                 tabular->SetVAlignment(
2115                                         tabular->GetCellNumber(i, j),
2116                                         setVAlign, flag);
2117                 updateLocal(bv, INIT, true);
2118                 break;
2119         case LyXTabular::MULTICOLUMN:
2120         {
2121                 if (sel_row_start != sel_row_end) {
2122                         Alert::alert(_("Impossible operation!"),
2123                                    _("Multicolumns can only be horizontally."),
2124                                    _("Sorry."));
2125                         return;
2126                 }
2127                 // just multicol for one Single Cell
2128                 if (!hasSelection()) {
2129                         // check wether we are completly in a multicol
2130                         if (tabular->IsMultiColumn(actcell)) {
2131                                 tabular->UnsetMultiColumn(actcell);
2132                                 updateLocal(bv, INIT, true);
2133                         } else {
2134                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
2135                                 updateLocal(bv, CELL, true);
2136                         }
2137                         return;
2138                 }
2139                 // we have a selection so this means we just add all this
2140                 // cells to form a multicolumn cell
2141                 int s_start;
2142                 int s_end;
2143
2144                 if (sel_cell_start > sel_cell_end) {
2145                         s_start = sel_cell_end;
2146                         s_end = sel_cell_start;
2147                 } else {
2148                         s_start = sel_cell_start;
2149                         s_end = sel_cell_end;
2150                 }
2151                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
2152                 actcell = s_start;
2153                 clearSelection();
2154                 updateLocal(bv, INIT, true);
2155                 break;
2156         }
2157         case LyXTabular::SET_ALL_LINES:
2158                 setLines = true;
2159         case LyXTabular::UNSET_ALL_LINES:
2160                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2161                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2162                                 tabular->SetAllLines(
2163                                         tabular->GetCellNumber(i,j), setLines);
2164                 updateLocal(bv, INIT, true);
2165                 break;
2166         case LyXTabular::SET_LONGTABULAR:
2167                 tabular->SetLongTabular(true);
2168                 updateLocal(bv, INIT, true); // because this toggles displayed
2169                 break;
2170         case LyXTabular::UNSET_LONGTABULAR:
2171                 tabular->SetLongTabular(false);
2172                 updateLocal(bv, INIT, true); // because this toggles displayed
2173                 break;
2174         case LyXTabular::SET_ROTATE_TABULAR:
2175                 tabular->SetRotateTabular(true);
2176                 break;
2177         case LyXTabular::UNSET_ROTATE_TABULAR:
2178                 tabular->SetRotateTabular(false);
2179                 break;
2180         case LyXTabular::SET_ROTATE_CELL:
2181                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2182                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2183                                 tabular->SetRotateCell(
2184                                         tabular->GetCellNumber(i, j),
2185                                         true);
2186                 break;
2187         case LyXTabular::UNSET_ROTATE_CELL:
2188                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2189                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2190                                 tabular->SetRotateCell(
2191                                         tabular->GetCellNumber(i, j), false);
2192                 break;
2193         case LyXTabular::SET_USEBOX:
2194         {
2195                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2196                 if (val == tabular->GetUsebox(actcell))
2197                         val = LyXTabular::BOX_NONE;
2198                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2199                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2200                                 tabular->SetUsebox(
2201                                         tabular->GetCellNumber(i, j), val);
2202                 break;
2203         }
2204         case LyXTabular::UNSET_LTFIRSTHEAD:
2205                 flag = false;
2206         case LyXTabular::SET_LTFIRSTHEAD:
2207                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2208                 checkLongtableSpecial(ltt, value, flag);
2209                 tabular->SetLTHead(row, flag, ltt, true);
2210                 break;
2211         case LyXTabular::UNSET_LTHEAD:
2212                 flag = false;
2213         case LyXTabular::SET_LTHEAD:
2214                 (void)tabular->GetRowOfLTHead(row, ltt);
2215                 checkLongtableSpecial(ltt, value, flag);
2216                 tabular->SetLTHead(row, flag, ltt, false);
2217                 break;
2218         case LyXTabular::UNSET_LTFOOT:
2219                 flag = false;
2220         case LyXTabular::SET_LTFOOT:
2221                 (void)tabular->GetRowOfLTFoot(row, ltt);
2222                 checkLongtableSpecial(ltt, value, flag);
2223                 tabular->SetLTFoot(row, flag, ltt, false);
2224                 break;
2225         case LyXTabular::UNSET_LTLASTFOOT:
2226                 flag = false;
2227         case LyXTabular::SET_LTLASTFOOT:
2228                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2229                 checkLongtableSpecial(ltt, value, flag);
2230                 tabular->SetLTFoot(row, flag, ltt, true);
2231                 break;
2232         case LyXTabular::SET_LTNEWPAGE:
2233         {
2234                 bool what = !tabular->GetLTNewPage(row);
2235                 tabular->SetLTNewPage(row, what);
2236                 break;
2237         }
2238         // dummy stuff just to avoid warnings
2239         case LyXTabular::LAST_ACTION:
2240                 break;
2241         }
2242 }
2243
2244
2245 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
2246                                      bool behind)
2247 {
2248         UpdatableInset * inset =
2249                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2250         LyXFont font(LyXFont::ALL_SANE);
2251         if (behind) {
2252                 x = inset->x() + inset->width(bv, font);
2253                 y = inset->descent(bv, font);
2254         }
2255         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2256         //inset_y = cursor.y();
2257         inset->edit(bv, x,  y, button);
2258         if (!the_locking_inset)
2259                 return false;
2260         updateLocal(bv, CELL, false);
2261         return (the_locking_inset != 0);
2262 }
2263
2264
2265 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2266                                         mouse_button::state button)
2267 {
2268         inset_x = cursor_.x()
2269                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2270         inset_y = cursor_.y();
2271         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2272 }
2273
2274
2275 bool InsetTabular::insetHit(BufferView *, int x, int) const
2276 {
2277         return (x + top_x)
2278                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2279 }
2280
2281
2282 // This returns paperWidth() if the cell-width is unlimited or the width
2283 // in pixels if we have a pwidth for this cell.
2284 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2285 {
2286         LyXLength const len = tabular->GetPWidth(cell);
2287
2288         if (len.zero())
2289                 return -1;
2290         return len.inPixels(latexTextWidth(bv));
2291 }
2292
2293
2294 int InsetTabular::getMaxWidth(BufferView * bv,
2295                               UpdatableInset const * inset) const
2296 {
2297         int cell = tabular->GetCellFromInset(inset, actcell);
2298
2299         if (cell == -1) {
2300                 lyxerr << "Own inset not found, shouldn't really happen!"
2301                        << endl;
2302                 return -1;
2303         }
2304
2305         int w = getMaxWidthOfCell(bv, cell);
2306         if (w > 0) {
2307                 // because the inset then subtracts it's top_x and owner->x()
2308                 w += (inset->x() - top_x);
2309         }
2310
2311         return w;
2312 }
2313
2314
2315 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2316 {
2317         resizeLyXText(bv, recursive);
2318 }
2319
2320
2321 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2322 {
2323         if (force) {
2324                 for(int i = 0; i < tabular->rows(); ++i) {
2325                         for(int j = 0; j < tabular->columns(); ++j) {
2326                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2327                         }
2328                 }
2329         }
2330         need_update = FULL;
2331 }
2332
2333
2334 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2335                                    bool const recursive) const
2336 {
2337         if (the_locking_inset)
2338                 return the_locking_inset->getLyXText(bv, recursive);
2339 #if 0
2340         // if we're locked lock the actual insettext and return it's LyXText!!!
2341         if (locked) {
2342                 UpdatableInset * inset =
2343                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2344                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2345                 return the_locking_inset->getLyXText(bv, recursive);
2346         }
2347 #endif
2348         return Inset::getLyXText(bv, recursive);
2349 }
2350
2351
2352 bool InsetTabular::showInsetDialog(BufferView * bv) const
2353 {
2354         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv)) {
2355                 InsetTabular * tmp = const_cast<InsetTabular *>(this);
2356                 InsetTabularMailer mailer(*tmp);
2357                 mailer.showDialog();
2358         }
2359         return true;
2360 }
2361
2362
2363 void InsetTabular::openLayoutDialog(BufferView * bv) const
2364 {
2365         if (the_locking_inset) {
2366                 InsetTabular * i = static_cast<InsetTabular *>
2367                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2368                 if (i) {
2369                         i->openLayoutDialog(bv);
2370                         return;
2371                 }
2372         }
2373         InsetTabular * tmp = const_cast<InsetTabular *>(this);
2374         InsetTabularMailer mailer(*tmp);
2375         mailer.showDialog();
2376 }
2377
2378
2379 //
2380 // function returns an object as defined in func_status.h:
2381 // states OK, Unknown, Disabled, On, Off.
2382 //
2383 FuncStatus InsetTabular::getStatus(string const & what) const
2384 {
2385         int action = LyXTabular::LAST_ACTION;
2386         FuncStatus status;
2387
2388         int i = 0;
2389         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2390                 string const tmp = tabularFeature[i].feature;
2391                 if (tmp == what.substr(0, tmp.length())) {
2392                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2393                         //   tabularFeatures[i].feature.length())) {
2394                         action = tabularFeature[i].action;
2395                         break;
2396                 }
2397         }
2398         if (action == LyXTabular::LAST_ACTION) {
2399                 status.clear();
2400                 return status.unknown(true);
2401         }
2402
2403         string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
2404
2405         int sel_row_start;
2406         int sel_row_end;
2407         int dummy;
2408         LyXTabular::ltType dummyltt;
2409         bool flag = true;
2410
2411         if (hasSelection()) {
2412                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2413         } else {
2414                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2415         }
2416
2417         switch (action) {
2418         case LyXTabular::SET_PWIDTH:
2419         case LyXTabular::SET_MPWIDTH:
2420         case LyXTabular::SET_SPECIAL_COLUMN:
2421         case LyXTabular::SET_SPECIAL_MULTI:
2422                 return status.disabled(true);
2423
2424         case LyXTabular::APPEND_ROW:
2425         case LyXTabular::APPEND_COLUMN:
2426         case LyXTabular::DELETE_ROW:
2427         case LyXTabular::DELETE_COLUMN:
2428         case LyXTabular::SET_ALL_LINES:
2429         case LyXTabular::UNSET_ALL_LINES:
2430                 return status.clear();
2431
2432         case LyXTabular::MULTICOLUMN:
2433                 status.setOnOff(tabular->IsMultiColumn(actcell));
2434                 break;
2435         case LyXTabular::M_TOGGLE_LINE_TOP:
2436                 flag = false;
2437         case LyXTabular::TOGGLE_LINE_TOP:
2438                 status.setOnOff(tabular->TopLine(actcell, flag));
2439                 break;
2440         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2441                 flag = false;
2442         case LyXTabular::TOGGLE_LINE_BOTTOM:
2443                 status.setOnOff(tabular->BottomLine(actcell, flag));
2444                 break;
2445         case LyXTabular::M_TOGGLE_LINE_LEFT:
2446                 flag = false;
2447         case LyXTabular::TOGGLE_LINE_LEFT:
2448                 status.setOnOff(tabular->LeftLine(actcell, flag));
2449                 break;
2450         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2451                 flag = false;
2452         case LyXTabular::TOGGLE_LINE_RIGHT:
2453                 status.setOnOff(tabular->RightLine(actcell, flag));
2454                 break;
2455         case LyXTabular::M_ALIGN_LEFT:
2456                 flag = false;
2457         case LyXTabular::ALIGN_LEFT:
2458                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2459                 break;
2460         case LyXTabular::M_ALIGN_RIGHT:
2461                 flag = false;
2462         case LyXTabular::ALIGN_RIGHT:
2463                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2464                 break;
2465         case LyXTabular::M_ALIGN_CENTER:
2466                 flag = false;
2467         case LyXTabular::ALIGN_CENTER:
2468                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2469                 break;
2470         case LyXTabular::ALIGN_BLOCK:
2471                 status.disabled(tabular->GetPWidth(actcell).zero());
2472                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2473                 break;
2474         case LyXTabular::M_VALIGN_TOP:
2475                 flag = false;
2476         case LyXTabular::VALIGN_TOP:
2477                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2478                 break;
2479         case LyXTabular::M_VALIGN_BOTTOM:
2480                 flag = false;
2481         case LyXTabular::VALIGN_BOTTOM:
2482                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2483                 break;
2484         case LyXTabular::M_VALIGN_CENTER:
2485                 flag = false;
2486         case LyXTabular::VALIGN_CENTER:
2487                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2488                 break;
2489         case LyXTabular::SET_LONGTABULAR:
2490                 status.setOnOff(tabular->IsLongTabular());
2491                 break;
2492         case LyXTabular::UNSET_LONGTABULAR:
2493                 status.setOnOff(!tabular->IsLongTabular());
2494                 break;
2495         case LyXTabular::SET_ROTATE_TABULAR:
2496                 status.setOnOff(tabular->GetRotateTabular());
2497                 break;
2498         case LyXTabular::UNSET_ROTATE_TABULAR:
2499                 status.setOnOff(!tabular->GetRotateTabular());
2500                 break;
2501         case LyXTabular::SET_ROTATE_CELL:
2502                 status.setOnOff(tabular->GetRotateCell(actcell));
2503                 break;
2504         case LyXTabular::UNSET_ROTATE_CELL:
2505                 status.setOnOff(!tabular->GetRotateCell(actcell));
2506                 break;
2507         case LyXTabular::SET_USEBOX:
2508                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2509                 break;
2510         case LyXTabular::SET_LTFIRSTHEAD:
2511                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2512                 break;
2513         case LyXTabular::SET_LTHEAD:
2514                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2515                 break;
2516         case LyXTabular::SET_LTFOOT:
2517                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2518                 break;
2519         case LyXTabular::SET_LTLASTFOOT:
2520                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2521                 break;
2522         case LyXTabular::SET_LTNEWPAGE:
2523                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2524                 break;
2525         default:
2526                 status.clear();
2527                 status.disabled(true);
2528                 break;
2529         }
2530         return status;
2531 }
2532
2533
2534 vector<string> const InsetTabular::getLabelList() const
2535 {
2536         return tabular->getLabelList();
2537 }
2538
2539
2540 bool InsetTabular::copySelection(BufferView * bv)
2541 {
2542         if (!hasSelection())
2543                 return false;
2544
2545         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2546         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2547         if (sel_col_start > sel_col_end) {
2548                 sel_col_start = sel_col_end;
2549                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2550         } else {
2551                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2552         }
2553         int const columns = sel_col_end - sel_col_start + 1;
2554
2555         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2556         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2557         if (sel_row_start > sel_row_end) {
2558                 swap(sel_row_start, sel_row_end);
2559         }
2560         int const rows = sel_row_end - sel_row_start + 1;
2561
2562         delete paste_tabular;
2563         paste_tabular = new LyXTabular(bv->buffer()->params,
2564                                        this, *tabular); // rows, columns);
2565         for (int i = 0; i < sel_row_start; ++i)
2566                 paste_tabular->DeleteRow(0);
2567         while (paste_tabular->rows() > rows)
2568                 paste_tabular->DeleteRow(rows);
2569         paste_tabular->SetTopLine(0, true, true);
2570         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2571                                      true, true);
2572         for (int i = 0; i < sel_col_start; ++i)
2573                 paste_tabular->DeleteColumn(0);
2574         while (paste_tabular->columns() > columns)
2575                 paste_tabular->DeleteColumn(columns);
2576         paste_tabular->SetLeftLine(0, true, true);
2577         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2578                                     true, true);
2579
2580         ostringstream sstr;
2581         paste_tabular->ascii(bv->buffer(), sstr,
2582                              (int)parOwner()->params().depth(), true, '\t');
2583         bv->stuffClipboard(STRCONV(sstr.str()));
2584         return true;
2585 }
2586
2587
2588 bool InsetTabular::pasteSelection(BufferView * bv)
2589 {
2590         if (!paste_tabular)
2591                 return false;
2592
2593         for (int r1 = 0, r2 = actrow;
2594              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2595              ++r1, ++r2) {
2596                 for(int c1 = 0, c2 = actcol;
2597                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2598                     ++c1, ++c2) {
2599                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2600                             tabular->IsPartOfMultiColumn(r2,c2))
2601                                 continue;
2602                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2603                                 --c2;
2604                                 continue;
2605                         }
2606                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2607                                 --c1;
2608                                 continue;
2609                         }
2610                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2611                         int const n2 = tabular->GetCellNumber(r2, c2);
2612                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2613                         tabular->GetCellInset(n2)->setOwner(this);
2614                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2615                         tabular->GetCellInset(n2)->markNew();
2616                 }
2617         }
2618         return true;
2619 }
2620
2621
2622 bool InsetTabular::cutSelection(BufferParams const & bp)
2623 {
2624         if (!hasSelection())
2625                 return false;
2626
2627         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2628         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2629         if (sel_col_start > sel_col_end) {
2630                 sel_col_start = sel_col_end;
2631                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2632         } else {
2633                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2634         }
2635         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2636         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2637         if (sel_row_start > sel_row_end) {
2638                 swap(sel_row_start, sel_row_end);
2639         }
2640         if (sel_cell_start > sel_cell_end) {
2641                 swap(sel_cell_start, sel_cell_end);
2642         }
2643         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2644                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2645                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear(bp.tracking_changes);
2646                 }
2647         }
2648         return true;
2649 }
2650
2651
2652 bool InsetTabular::isRightToLeft(BufferView * bv)
2653 {
2654         return bv->getParentLanguage(this)->RightToLeft();
2655 }
2656
2657
2658 bool InsetTabular::nodraw() const
2659 {
2660         if (!UpdatableInset::nodraw() && the_locking_inset)
2661                 return the_locking_inset->nodraw();
2662         return UpdatableInset::nodraw();
2663 }
2664
2665
2666 int InsetTabular::scroll(bool recursive) const
2667 {
2668         int sx = UpdatableInset::scroll(false);
2669
2670         if (recursive && the_locking_inset)
2671                 sx += the_locking_inset->scroll(recursive);
2672
2673         return sx;
2674 }
2675
2676
2677 bool InsetTabular::doClearArea() const
2678 {
2679         return !locked || (need_update & (FULL|INIT));
2680 }
2681
2682
2683 void InsetTabular::getSelection(int & srow, int & erow,
2684                                 int & scol, int & ecol) const
2685 {
2686         int const start = hasSelection() ? sel_cell_start : actcell;
2687         int const end = hasSelection() ? sel_cell_end : actcell;
2688
2689         srow = tabular->row_of_cell(start);
2690         erow = tabular->row_of_cell(end);
2691         if (srow > erow) {
2692                 swap(srow, erow);
2693         }
2694
2695         scol = tabular->column_of_cell(start);
2696         ecol = tabular->column_of_cell(end);
2697         if (scol > ecol) {
2698                 swap(scol, ecol);
2699         } else {
2700                 ecol = tabular->right_column_of_cell(end);
2701         }
2702 }
2703
2704
2705 Paragraph * InsetTabular::firstParagraph() const
2706 {
2707         if (the_locking_inset)
2708                 return the_locking_inset->firstParagraph();
2709         return 0;
2710 }
2711
2712
2713 Paragraph * InsetTabular::getFirstParagraph(int i) const
2714 {
2715         return (i < tabular->GetNumberOfCells())
2716                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2717                 : 0;
2718 }
2719
2720
2721 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2722 {
2723         if (the_locking_inset)
2724                 return the_locking_inset->cursor(bv);
2725         return Inset::cursor(bv);
2726 }
2727
2728
2729 Inset * InsetTabular::getInsetFromID(int id_arg) const
2730 {
2731         if (id_arg == id())
2732                 return const_cast<InsetTabular *>(this);
2733
2734         Inset * result;
2735         for(int i = 0; i < tabular->rows(); ++i) {
2736                 for(int j = 0; j < tabular->columns(); ++j) {
2737                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2738                                 return result;
2739                 }
2740         }
2741         return 0;
2742 }
2743
2744
2745 WordLangTuple const
2746 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2747 {
2748         nodraw(true);
2749         if (the_locking_inset) {
2750                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2751                 if (!word.word().empty()) {
2752                         nodraw(false);
2753                         return word;
2754                 }
2755                 if (tabular->IsLastCell(actcell)) {
2756                         bv->unlockInset(const_cast<InsetTabular *>(this));
2757                         nodraw(false);
2758                         return WordLangTuple();
2759                 }
2760                 ++actcell;
2761         }
2762         // otherwise we have to lock the next inset and ask for it's selecttion
2763         UpdatableInset * inset =
2764                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2765         inset->edit(bv, 0,  0, mouse_button::none);
2766         WordLangTuple word(selectNextWordInt(bv, value));
2767         nodraw(false);
2768         if (!word.word().empty())
2769                 resetPos(bv);
2770         return word;
2771 }
2772
2773
2774 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2775 {
2776         // when entering this function the inset should be ALWAYS locked!
2777         lyx::Assert(the_locking_inset);
2778
2779         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2780         if (!word.word().empty())
2781                 return word;
2782
2783         if (tabular->IsLastCell(actcell)) {
2784                 bv->unlockInset(const_cast<InsetTabular *>(this));
2785                 return WordLangTuple();
2786         }
2787
2788         // otherwise we have to lock the next inset and ask for it's selecttion
2789         UpdatableInset * inset =
2790                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2791         inset->edit(bv);
2792         return selectNextWordInt(bv, value);
2793 }
2794
2795
2796 void InsetTabular::selectSelectedWord(BufferView * bv)
2797 {
2798         if (the_locking_inset) {
2799                 the_locking_inset->selectSelectedWord(bv);
2800                 return;
2801         }
2802         return;
2803 }
2804
2805
2806 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2807 {
2808         if (the_locking_inset) {
2809                 the_locking_inset->toggleSelection(bv, kill_selection);
2810         }
2811 }
2812
2813
2814 void InsetTabular::markErased()
2815 {
2816         int cell = 0;
2817
2818         while (!tabular->IsLastCell(cell)) {
2819                 ++cell;
2820                 InsetText * inset = tabular->GetCellInset(cell);
2821                 inset->markErased();
2822         }
2823 }
2824
2825
2826 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2827 {
2828         if (the_locking_inset) {
2829                 if (the_locking_inset->nextChange(bv, length)) {
2830                         updateLocal(bv, CELL, false);
2831                         return true;
2832                 }
2833                 if (tabular->IsLastCell(actcell))
2834                         return false;
2835                 ++actcell;
2836         }
2837         InsetText * inset = tabular->GetCellInset(actcell);
2838         if (inset->nextChange(bv, length)) {
2839                 updateLocal(bv, FULL, false);
2840                 return true;
2841         }
2842         while (!tabular->IsLastCell(actcell)) {
2843                 ++actcell;
2844                 inset = tabular->GetCellInset(actcell);
2845                 if (inset->nextChange(bv, length)) {
2846                         updateLocal(bv, FULL, false);
2847                         return true;
2848                 }
2849         }
2850         return false;
2851 }
2852
2853
2854 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2855                                  bool cs, bool mw)
2856 {
2857         if (the_locking_inset) {
2858                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2859                         updateLocal(bv, CELL, false);
2860                         return true;
2861                 }
2862                 if (tabular->IsLastCell(actcell))
2863                         return false;
2864                 ++actcell;
2865         }
2866         InsetText * inset = tabular->GetCellInset(actcell);
2867         if (inset->searchForward(bv, str, cs, mw)) {
2868                 updateLocal(bv, FULL, false);
2869                 return true;
2870         }
2871         while (!tabular->IsLastCell(actcell)) {
2872                 ++actcell;
2873                 inset = tabular->GetCellInset(actcell);
2874                 if (inset->searchForward(bv, str, cs, mw)) {
2875                         updateLocal(bv, FULL, false);
2876                         return true;
2877                 }
2878         }
2879         return false;
2880 }
2881
2882
2883 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2884                                bool cs, bool mw)
2885 {
2886         if (the_locking_inset) {
2887                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2888                         updateLocal(bv, CELL, false);
2889                         return true;
2890                 }
2891         }
2892         if (!locked)
2893                 actcell = tabular->GetNumberOfCells();
2894
2895         while (actcell) {
2896                 --actcell;
2897                 InsetText * inset = tabular->GetCellInset(actcell);
2898                 if (inset->searchBackward(bv, str, cs, mw)) {
2899                         updateLocal(bv, CELL, false);
2900                         return true;
2901                 }
2902         }
2903         return false;
2904 }
2905
2906
2907 bool InsetTabular::insetAllowed(Inset::Code code) const
2908 {
2909         if (the_locking_inset)
2910                 return the_locking_inset->insetAllowed(code);
2911         // we return true here because if the inset is not locked someone
2912         // wants to insert something in one of our insettexts and we generally
2913         // allow to do so.
2914         return true;
2915 }
2916
2917
2918 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2919 {
2920         const int cell = tabular->GetCellFromInset(in, actcell);
2921
2922         if (cell != -1)
2923                 return tabular->GetPWidth(cell).zero();
2924
2925         // well we didn't obviously find it so maybe our owner knows more
2926         if (owner())
2927                 return owner()->forceDefaultParagraphs(in);
2928         // if we're here there is really something strange going on!!!
2929         return false;
2930 }
2931
2932 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2933                                      bool usePaste)
2934 {
2935         if (buf.length() <= 0)
2936                 return true;
2937
2938         int cols = 1;
2939         int rows = 1;
2940         int maxCols = 1;
2941         string::size_type len = buf.length();
2942         string::size_type p = 0;
2943
2944         while (p < len &&
2945                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2946         {
2947                 switch (buf[p]) {
2948                 case '\t':
2949                         ++cols;
2950                         break;
2951                 case '\n':
2952                         if ((p+1) < len)
2953                                 ++rows;
2954                         maxCols = max(cols, maxCols);
2955                         cols = 1;
2956                         break;
2957                 }
2958                 ++p;
2959         }
2960         maxCols = max(cols, maxCols);
2961         LyXTabular * loctab;
2962         int cell = 0;
2963         int ocol = 0;
2964         int row = 0;
2965         if (usePaste) {
2966                 delete paste_tabular;
2967                 paste_tabular = new LyXTabular(bv->buffer()->params,
2968                                                this, rows, maxCols);
2969                 loctab = paste_tabular;
2970                 cols = 0;
2971         } else {
2972                 loctab = tabular.get();
2973                 cell = actcell;
2974                 ocol = actcol;
2975                 row = actrow;
2976         }
2977
2978         string::size_type op = 0;
2979         int cells = loctab->GetNumberOfCells();
2980         p = 0;
2981         cols = ocol;
2982         rows = loctab->rows();
2983         int const columns = loctab->columns();
2984
2985         while ((cell < cells) && (p < len) && (row < rows) &&
2986                (p = buf.find_first_of("\t\n", p)) != string::npos)
2987         {
2988                 if (p >= len)
2989                         break;
2990                 switch (buf[p]) {
2991                 case '\t':
2992                         // we can only set this if we are not too far right
2993                         if (cols < columns) {
2994                                 InsetText * ti = loctab->GetCellInset(cell);
2995                                 LyXFont const font = ti->getLyXText(bv)->
2996                                         getFont(bv->buffer(), ti->paragraph(), 0);
2997                                 ti->setText(buf.substr(op, p-op), font);
2998                                 ++cols;
2999                                 ++cell;
3000                         }
3001                         break;
3002                 case '\n':
3003                         // we can only set this if we are not too far right
3004                         if (cols < columns) {
3005                                 InsetText * ti = loctab->GetCellInset(cell);
3006                                 LyXFont const font = ti->getLyXText(bv)->
3007                                         getFont(bv->buffer(), ti->paragraph(), 0);
3008                                 ti->setText(buf.substr(op, p-op), font);
3009                         }
3010                         cols = ocol;
3011                         ++row;
3012                         if (row < rows)
3013                                 cell = loctab->GetCellNumber(row, cols);
3014                         break;
3015                 }
3016                 ++p;
3017                 op = p;
3018         }
3019         // check for the last cell if there is no trailing '\n'
3020         if ((cell < cells) && (op < len)) {
3021                 InsetText * ti = loctab->GetCellInset(cell);
3022                 LyXFont const font = ti->getLyXText(bv)->
3023                         getFont(bv->buffer(), ti->paragraph(), 0);
3024                 ti->setText(buf.substr(op, len-op), font);
3025         }
3026
3027         return true;
3028 }
3029
3030
3031 void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
3032 {
3033         int const rows = tabular->rows();
3034         int const columns = tabular->columns();
3035         for (int i = 0; i < rows; ++i) {
3036                 for (int j = 0; j < columns; ++j) {
3037                         tabular->GetCellInset(i,j)->addPreview(loader);
3038                 }
3039         }
3040 }
3041
3042
3043 string const InsetTabularMailer:: name_("tabular");
3044
3045 InsetTabularMailer::InsetTabularMailer(InsetTabular & inset)
3046         : inset_(inset)
3047 {}
3048
3049
3050 string const InsetTabularMailer::inset2string() const
3051 {
3052         return params2string(inset_);
3053 }
3054
3055
3056 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
3057 {
3058         istringstream data(in);
3059         LyXLex lex(0,0);
3060         lex.setStream(data);
3061
3062         if (lex.isOK()) {
3063                 lex.next();
3064                 string const token = lex.getString();
3065                 if (token != name_)
3066                         return -1;
3067         }
3068
3069         int cell = -1;
3070         if (lex.isOK()) {
3071                 lex.next();
3072                 string const token = lex.getString();
3073                 if (token != "active_cell")
3074                         return -1;
3075                 lex.next();
3076                 cell = lex.getInteger();
3077         }
3078
3079         // This is part of the inset proper that is usually swallowed
3080         // by Buffer::readInset
3081         if (lex.isOK()) {
3082                 lex.next();
3083                 string const token = lex.getString();
3084                 if (token != "Tabular")
3085                         return -1;
3086         }
3087
3088         BufferView * const bv = inset.view();
3089         Buffer const * const buffer = bv ? bv->buffer() : 0;
3090         if (buffer)
3091                 inset.read(buffer, lex);
3092
3093         // We can't set the active cell, but we can tell the frontend
3094         // what it is.
3095         return cell;
3096 }
3097
3098
3099 string const
3100 InsetTabularMailer::params2string(InsetTabular const & inset)
3101 {
3102         BufferView * const bv = inset.view();
3103         Buffer const * const buffer = bv ? bv->buffer() : 0;
3104         if (!buffer)
3105                 return string();
3106
3107         ostringstream data;
3108         data << name_ << " active_cell " << inset.getActCell() << '\n';
3109         inset.write(buffer, data);
3110         data << "\\end_inset\n";
3111
3112         return data.str();
3113 }