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