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