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