]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
use the new mouse LFUNs
[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                         lfunMouseRelease(cmd);
924                         return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
925
926                 case LFUN_SHIFT_TAB:
927                 case LFUN_TAB:
928                         hideInsetCursor(bv);
929                         unlockInsetInInset(bv, the_locking_inset);
930                         if (cmd.action == LFUN_TAB)
931                                 moveNextCell(bv, old_locking_inset != 0);
932                         else
933                                 movePrevCell(bv, old_locking_inset != 0);
934                         clearSelection();
935                         if (hs)
936                                 updateLocal(bv, SELECTION, false);
937                         if (!the_locking_inset) {
938                                 showInsetCursor(bv);
939                                 return DISPATCHED_NOUPDATE;
940                         }
941                         return result;
942                 // this to avoid compiler warnings.
943                 default:
944                         break;
945                 }
946         }
947
948         kb_action action = cmd.action;
949         string    arg    = cmd.argument;
950         if (the_locking_inset) {
951                 result = the_locking_inset->localDispatch(cmd);
952                 if (result == DISPATCHED_NOUPDATE) {
953                         int sc = scroll();
954                         resetPos(bv);
955                         if (sc != scroll()) { // inset has been scrolled
956                                 the_locking_inset->toggleInsetCursor(bv);
957                                 updateLocal(bv, FULL, false);
958                                 the_locking_inset->toggleInsetCursor(bv);
959                         }
960                         return result;
961                 } else if (result == DISPATCHED) {
962                         the_locking_inset->toggleInsetCursor(bv);
963                         updateLocal(bv, CELL, false);
964                         the_locking_inset->toggleInsetCursor(bv);
965                         return result;
966                 } else if (result == FINISHED_UP) {
967                         action = LFUN_UP;
968                 } else if (result == FINISHED_DOWN) {
969                         action = LFUN_DOWN;
970                 } else if (result == FINISHED_RIGHT) {
971                         action = LFUN_RIGHT;
972                 }
973         }
974
975         hideInsetCursor(bv);
976         result = DISPATCHED;
977         switch (action) {
978                 // --- Cursor Movements ----------------------------------
979         case LFUN_RIGHTSEL: {
980                 int const start = hasSelection() ? sel_cell_start : actcell;
981                 if (tabular->IsLastCellInRow(actcell)) {
982                         setSelection(start, actcell);
983                         break;
984                 }
985
986                 int end = actcell;
987                 // if we are starting a selection, only select
988                 // the current cell at the beginning
989                 if (hasSelection()) {
990                         moveRight(bv, false);
991                         end = actcell;
992                 }
993                 setSelection(start, end);
994                 updateLocal(bv, SELECTION, false);
995                 break;
996         }
997         case LFUN_RIGHT:
998                 result = moveRight(bv);
999                 clearSelection();
1000                 if (hs)
1001                         updateLocal(bv, SELECTION, false);
1002                 break;
1003         case LFUN_LEFTSEL: {
1004                 int const start = hasSelection() ? sel_cell_start : actcell;
1005                 if (tabular->IsFirstCellInRow(actcell)) {
1006                         setSelection(start, actcell);
1007                         break;
1008                 }
1009
1010                 int end = actcell;
1011                 // if we are starting a selection, only select
1012                 // the current cell at the beginning
1013                 if (hasSelection()) {
1014                         moveLeft(bv, false);
1015                         end = actcell;
1016                 }
1017                 setSelection(start, end);
1018                 updateLocal(bv, SELECTION, false);
1019                 break;
1020         }
1021         case LFUN_LEFT:
1022                 result = moveLeft(bv);
1023                 clearSelection();
1024                 if (hs)
1025                         updateLocal(bv, SELECTION, false);
1026                 break;
1027         case LFUN_DOWNSEL: {
1028                 int const start = hasSelection() ? sel_cell_start : actcell;
1029                 int const ocell = actcell;
1030                 // if we are starting a selection, only select
1031                 // the current cell at the beginning
1032                 if (hasSelection()) {
1033                         moveDown(bv, false);
1034                         if ((ocell == sel_cell_end) ||
1035                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1036                                 setSelection(start, tabular->GetCellBelow(sel_cell_end));
1037                         else
1038                                 setSelection(start, tabular->GetLastCellBelow(sel_cell_end));
1039                 } else {
1040                         setSelection(start, start);
1041                 }
1042                 updateLocal(bv, SELECTION, false);
1043         }
1044         break;
1045         case LFUN_DOWN:
1046                 result = moveDown(bv, old_locking_inset != 0);
1047                 clearSelection();
1048                 if (hs) {
1049                         updateLocal(bv, SELECTION, false);
1050                 }
1051                 break;
1052         case LFUN_UPSEL: {
1053                 int const start = hasSelection() ? sel_cell_start : actcell;
1054                 int const ocell = actcell;
1055                 // if we are starting a selection, only select
1056                 // the current cell at the beginning
1057                 if (hasSelection()) {
1058                         moveUp(bv, false);
1059                         if ((ocell == sel_cell_end) ||
1060                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
1061                                 setSelection(start, tabular->GetCellAbove(sel_cell_end));
1062                         else
1063                                 setSelection(start, tabular->GetLastCellAbove(sel_cell_end));
1064                 } else {
1065                         setSelection(start, start);
1066                 }
1067                 updateLocal(bv, SELECTION, false);
1068         }
1069         break;
1070         case LFUN_UP:
1071                 result = moveUp(bv, old_locking_inset != 0);
1072                 clearSelection();
1073                 if (hs)
1074                         updateLocal(bv, SELECTION, false);
1075                 break;
1076         case LFUN_NEXT: {
1077                 UpdateCodes code = CURSOR;
1078                 if (hs) {
1079                         clearSelection();
1080                         code = SELECTION;
1081                 }
1082                 int column = actcol;
1083                 unlockInsetInInset(bv, the_locking_inset);
1084                 if (bv->text->first_y + bv->painter().paperHeight() <
1085                     (top_baseline + tabular->GetHeightOfTabular()))
1086                         {
1087                                 bv->scrollDocView(bv->text->first_y + bv->painter().paperHeight());
1088                                 code = FULL;
1089                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1090                         } else {
1091                                 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
1092                         }
1093                 resetPos(bv);
1094                 updateLocal(bv, code, false);
1095                 break;
1096         }
1097         case LFUN_PRIOR: {
1098                 UpdateCodes code = CURSOR;
1099                 if (hs) {
1100                         clearSelection();
1101                         code = SELECTION;
1102                 }
1103                 int column = actcol;
1104                 unlockInsetInInset(bv, the_locking_inset);
1105                 if (top_baseline < 0) {
1106                         bv->scrollDocView(bv->text->first_y - bv->painter().paperHeight());
1107                         code = FULL;
1108                         if (top_baseline > 0)
1109                                 actcell = column;
1110                         else
1111                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
1112                 } else {
1113                         actcell = column;
1114                 }
1115                 resetPos(bv);
1116                 updateLocal(bv, code, false);
1117                 break;
1118         }
1119         // none of these make sense for insettabular,
1120         // but we must catch them to prevent any
1121         // selection from being confused
1122         case LFUN_PRIORSEL:
1123         case LFUN_NEXTSEL:
1124         case LFUN_WORDLEFT:
1125         case LFUN_WORDLEFTSEL:
1126         case LFUN_WORDRIGHT:
1127         case LFUN_WORDRIGHTSEL:
1128         case LFUN_DOWN_PARAGRAPH:
1129         case LFUN_DOWN_PARAGRAPHSEL:
1130         case LFUN_UP_PARAGRAPH:
1131         case LFUN_UP_PARAGRAPHSEL:
1132         case LFUN_BACKSPACE:
1133         case LFUN_HOME:
1134         case LFUN_HOMESEL:
1135         case LFUN_END:
1136         case LFUN_ENDSEL:
1137         case LFUN_BEGINNINGBUF:
1138         case LFUN_BEGINNINGBUFSEL:
1139         case LFUN_ENDBUF:
1140         case LFUN_ENDBUFSEL:
1141                 break;
1142         case LFUN_LAYOUT_TABULAR:
1143                 bv->owner()->getDialogs().showTabular(this);
1144                 break;
1145         case LFUN_TABULAR_FEATURE:
1146                 if (!tabularFeatures(bv, arg))
1147                         result = UNDISPATCHED;
1148                 break;
1149                 // insert file functions
1150         case LFUN_FILE_INSERT_ASCII_PARA:
1151         case LFUN_FILE_INSERT_ASCII:
1152         {
1153                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
1154                 if (tmpstr.empty())
1155                         break;
1156                 if (insertAsciiString(bv, tmpstr, false))
1157                         updateLocal(bv, INIT, true);
1158                 else
1159                         result = UNDISPATCHED;
1160                 break;
1161         }
1162         // cut and paste functions
1163         case LFUN_CUT:
1164                 if (!copySelection(bv))
1165                         break;
1166                 // no break here!
1167         case LFUN_DELETE:
1168                 setUndo(bv, Undo::DELETE,
1169                         bv->text->cursor.par(),
1170                         bv->text->cursor.par()->next());
1171                 cutSelection();
1172                 updateLocal(bv, INIT, true);
1173                 break;
1174         case LFUN_COPY:
1175                 if (!hasSelection())
1176                         break;
1177                 finishUndo();
1178                 copySelection(bv);
1179                 break;
1180         case LFUN_PASTESELECTION:
1181         {
1182                 string const clip(bv->getClipboard());
1183                         if (clip.empty())
1184                         break;
1185 #if 0
1186                 if (clip.find('\t') != string::npos) {
1187                         int cols = 1;
1188                         int rows = 1;
1189                         int maxCols = 1;
1190                         string::size_type len = clip.length();
1191                         string::size_type p = 0;
1192
1193                         while (p < len &&
1194                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1195                                 switch (clip[p]) {
1196                                 case '\t':
1197                                         ++cols;
1198                                         break;
1199                                 case '\n':
1200                                         if ((p+1) < len)
1201                                                 ++rows;
1202                                         maxCols = max(cols, maxCols);
1203                                         cols = 1;
1204                                         break;
1205                                 }
1206                                 ++p;
1207                         }
1208                         maxCols = max(cols, maxCols);
1209                         delete paste_tabular;
1210                         paste_tabular = new LyXTabular(bv->buffer()->params,
1211                                                        this, rows, maxCols);
1212                         string::size_type op = 0;
1213                         int cell = 0;
1214                         int cells = paste_tabular->GetNumberOfCells();
1215                         p = cols = 0;
1216                         while ((cell < cells) && (p < len) &&
1217                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1218                                 if (p >= len)
1219                                         break;
1220                                 switch (clip[p]) {
1221                                 case '\t':
1222                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1223                                         ++cols;
1224                                         ++cell;
1225                                         break;
1226                                 case '\n':
1227                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1228                                         while (cols++ < maxCols)
1229                                                 ++cell;
1230                                         cols = 0;
1231                                         break;
1232                                 }
1233                                 ++p;
1234                                 op = p;
1235                         }
1236                         // check for the last cell if there is no trailing '\n'
1237                         if ((cell < cells) && (op < len))
1238                                 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1239                 } else
1240 #else
1241                 if (!insertAsciiString(bv, clip, true))
1242 #endif
1243                 {
1244                         // so that the clipboard is used and it goes on
1245                         // to default
1246                         // and executes LFUN_PASTESELECTION in insettext!
1247                         delete paste_tabular;
1248                         paste_tabular = 0;
1249                 }
1250         }
1251         case LFUN_PASTE:
1252                 if (hasPasteBuffer()) {
1253                         setUndo(bv, Undo::INSERT,
1254                                 bv->text->cursor.par(),
1255                                 bv->text->cursor.par()->next());
1256                         pasteSelection(bv);
1257                         updateLocal(bv, INIT, true);
1258                         break;
1259                 }
1260                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1261         default:
1262                 // handle font changing stuff on selection before we lock the inset
1263                 // in the default part!
1264                 result = UNDISPATCHED;
1265                 if (hs) {
1266                         switch(action) {
1267                         case LFUN_LANGUAGE:
1268                         case LFUN_EMPH:
1269                         case LFUN_BOLD:
1270                         case LFUN_NOUN:
1271                         case LFUN_CODE:
1272                         case LFUN_SANS:
1273                         case LFUN_ROMAN:
1274                         case LFUN_DEFAULT:
1275                         case LFUN_UNDERLINE:
1276                         case LFUN_FONT_SIZE:
1277                                 if (bv->dispatch(FuncRequest(bv, action, arg)))
1278                                         result = DISPATCHED;
1279                                 break;
1280                         default:
1281                                 break;
1282                         }
1283                 }
1284                 // we try to activate the actual inset and put this event down to
1285                 // the insets dispatch function.
1286                 if ((result == DISPATCHED) || the_locking_inset)
1287                         break;
1288                 nodraw(true);
1289                 if (activateCellInset(bv)) {
1290                         // reset need_update setted in above function!
1291                         need_update = NONE;
1292                         result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1293                         if ((result == UNDISPATCHED) || (result >= FINISHED)) {
1294                                 unlockInsetInInset(bv, the_locking_inset);
1295                                 nodraw(false);
1296                                 // we need to update if this was requested before
1297                                 updateLocal(bv, NONE, false);
1298                                 return UNDISPATCHED;
1299                         } else if (hs) {
1300                                 clearSelection();
1301                                 // so the below CELL is not set because this is higher
1302                                 // priority and we get a full redraw
1303                                 need_update = SELECTION;
1304                         }
1305                         nodraw(false);
1306                         updateLocal(bv, CELL, false);
1307                         return result;
1308                 }
1309                 break;
1310         }
1311         if (result < FINISHED) {
1312                 if (!the_locking_inset) {
1313                         if (bv->fitCursor())
1314                                 updateLocal(bv, FULL, false);
1315                         if (locked)
1316                                 showInsetCursor(bv);
1317                 }
1318         } else
1319                 bv->unlockInset(this);
1320         return result;
1321 }
1322
1323
1324 int InsetTabular::latex(Buffer const * buf, ostream & os,
1325                         bool fragile, bool fp) const
1326 {
1327         return tabular->latex(buf, os, fragile, fp);
1328 }
1329
1330
1331 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1332 {
1333         if (ll > 0)
1334                 return tabular->ascii(buf, os, (int)parOwner()->params().depth(),
1335                                       false,0);
1336         return tabular->ascii(buf, os, 0, false,0);
1337 }
1338
1339
1340 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1341 {
1342         os << "<![CDATA[";
1343         int const ret = tabular->ascii(buf,os,
1344                                        (int)parOwner()->params().depth(),
1345                                        false, 0);
1346         os << "]]>";
1347         return ret;
1348 }
1349
1350
1351 int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1352 {
1353         int ret = 0;
1354         Inset * master;
1355
1356         // if the table is inside a float it doesn't need the informaltable
1357         // wrapper. Search for it.
1358         for(master = owner();
1359             master && master->lyxCode() != Inset::FLOAT_CODE;
1360             master = master->owner());
1361
1362         if (!master) {
1363                 os << "<informaltable>";
1364                 if (mixcont)
1365                         os << endl;
1366                 ret++;
1367         }
1368         ret+= tabular->docbook(buf, os, mixcont);
1369         if (!master) {
1370                 os << "</informaltable>";
1371                 if (mixcont)
1372                         os << endl;
1373                 ret++;
1374         }
1375         return ret;
1376 }
1377
1378
1379 void InsetTabular::validate(LaTeXFeatures & features) const
1380 {
1381         tabular->Validate(features);
1382 }
1383
1384
1385 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1386                                                  LyXFont const & font,
1387                                                  bool reinit) const
1388 {
1389         int cell = -1;
1390         int maxAsc = 0;
1391         int maxDesc = 0;
1392         InsetText * inset;
1393         bool changed = false;
1394
1395         // if we have a locking_inset we should have to check only this cell for
1396         // change so I'll try this to have a boost, but who knows ;)
1397         if ((need_update != INIT) &&
1398             (the_locking_inset == tabular->GetCellInset(actcell))) {
1399                 for(int i = 0; i < tabular->columns(); ++i) {
1400                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1401                                      maxAsc);
1402                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1403                                       maxDesc);
1404                 }
1405                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1406                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1407                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1408                 return changed;
1409         }
1410         for (int i = 0; i < tabular->rows(); ++i) {
1411                 maxAsc = 0;
1412                 maxDesc = 0;
1413                 for (int j = 0; j < tabular->columns(); ++j) {
1414                         if (tabular->IsPartOfMultiColumn(i,j))
1415                                 continue;
1416                         ++cell;
1417                         inset = tabular->GetCellInset(cell);
1418                         if (!reinit && !tabular->GetPWidth(cell).zero())
1419                                 inset->update(bv, font, false);
1420                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1421                         maxDesc = max(maxDesc, inset->descent(bv, font));
1422                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1423                 }
1424                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1425                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1426         }
1427         if (changed)
1428                 tabular->reinit();
1429         return changed;
1430 }
1431
1432
1433 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1434 {
1435         if (the_locking_inset) {
1436                 the_locking_inset->getCursorPos(bv, x, y);
1437                 return;
1438         }
1439         x = cursor_.x() - top_x;
1440         y = cursor_.y();
1441 }
1442
1443
1444 void InsetTabular::toggleInsetCursor(BufferView * bv)
1445 {
1446         if (nodraw()) {
1447                 if (isCursorVisible())
1448                         bv->hideLockedInsetCursor();
1449                 return;
1450         }
1451         if (the_locking_inset) {
1452                 the_locking_inset->toggleInsetCursor(bv);
1453                 return;
1454         }
1455
1456         LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1457
1458         int const asc = font_metrics::maxAscent(font);
1459         int const desc = font_metrics::maxDescent(font);
1460
1461         if (isCursorVisible())
1462                 bv->hideLockedInsetCursor();
1463         else
1464                 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1465         toggleCursorVisible();
1466 }
1467
1468
1469 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1470 {
1471         if (nodraw())
1472                 return;
1473         if (!isCursorVisible()) {
1474                 LyXFont font; // = GetFont(par, cursor.pos);
1475
1476                 int const asc = font_metrics::maxAscent(font);
1477                 int const desc = font_metrics::maxDescent(font);
1478                 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1479                 if (show)
1480                         bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1481                 setCursorVisible(true);
1482         }
1483 }
1484
1485
1486 void InsetTabular::hideInsetCursor(BufferView * bv)
1487 {
1488         if (isCursorVisible()) {
1489                 bv->hideLockedInsetCursor();
1490                 setCursorVisible(false);
1491         }
1492 }
1493
1494
1495 void InsetTabular::fitInsetCursor(BufferView * bv) const
1496 {
1497         if (the_locking_inset) {
1498                 int old_first_y = bv->text->first_y;
1499                 the_locking_inset->fitInsetCursor(bv);
1500                 if (old_first_y != bv->text->first_y)
1501                         need_update = FULL;
1502                 return;
1503         }
1504         LyXFont font;
1505
1506         int const asc = font_metrics::maxAscent(font);
1507         int const desc = font_metrics::maxDescent(font);
1508         resetPos(bv);
1509
1510         if (bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc))
1511                 need_update = FULL;
1512 }
1513
1514
1515 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1516 {
1517         cursor_.y(0);
1518
1519         actcell = actrow = actcol = 0;
1520         int ly = tabular->GetDescentOfRow(actrow);
1521
1522         // first search the right row
1523         while ((ly < y) && ((actrow+1) < tabular->rows())) {
1524                 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1525                                  tabular->GetAscentOfRow(actrow + 1) +
1526                                  tabular->GetAdditionalHeight(actrow + 1));
1527                 ++actrow;
1528                 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1529         }
1530         actcell = tabular->GetCellNumber(actrow, actcol);
1531
1532         // now search the right column
1533         int lx = tabular->GetWidthOfColumn(actcell) -
1534                 tabular->GetAdditionalWidth(actcell);
1535         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1536                 lx += tabular->GetWidthOfColumn(actcell + 1)
1537                         + tabular->GetAdditionalWidth(actcell);
1538         }
1539         cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1540         resetPos(bv);
1541 }
1542
1543
1544 int InsetTabular::getCellXPos(int cell) const
1545 {
1546         int c = cell;
1547
1548         for (; !tabular->IsFirstCellInRow(c); --c)
1549                 ;
1550         int lx = tabular->GetWidthOfColumn(cell);
1551         for (; c < cell; ++c) {
1552                 lx += tabular->GetWidthOfColumn(c);
1553         }
1554         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1555 }
1556
1557
1558 void InsetTabular::resetPos(BufferView * bv) const
1559 {
1560 #ifdef WITH_WARNINGS
1561 #warning This should be fixed in the right manner (20011128 Jug)
1562 #endif
1563         // fast hack to fix infinite repaintings!
1564         if (in_reset_pos > 10)
1565                 return;
1566
1567         int cell = 0;
1568         actcol = tabular->column_of_cell(actcell);
1569         actrow = 0;
1570         cursor_.y(0);
1571         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1572                 if (tabular->IsLastCellInRow(cell)) {
1573                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1574                                          tabular->GetAscentOfRow(actrow + 1) +
1575                                          tabular->GetAdditionalHeight(actrow + 1));
1576                         ++actrow;
1577                 }
1578         }
1579         if (!locked || nodraw()) {
1580                 if (the_locking_inset)
1581                         inset_y = cursor_.y();
1582                 return;
1583         }
1584         // we need this only from here on!!!
1585         ++in_reset_pos;
1586         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1587         int new_x = getCellXPos(actcell);
1588         int old_x = cursor_.x();
1589         new_x += offset;
1590         cursor_.x(new_x);
1591 //    cursor.x(getCellXPos(actcell) + offset);
1592         if ((actcol < tabular->columns()-1) && scroll(false) &&
1593                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1594         {
1595                 scroll(bv, 0.0F);
1596                 updateLocal(bv, FULL, false);
1597         } else if (the_locking_inset &&
1598                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1599         {
1600                 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1601                 if (xx > (bv->workWidth()-20)) {
1602                         scroll(bv, -(xx - bv->workWidth() + 60));
1603                         updateLocal(bv, FULL, false);
1604                 } else if (xx < 20) {
1605                         if (xx < 0)
1606                                 xx = -xx + 60;
1607                         else
1608                                 xx = 60;
1609                         scroll(bv, xx);
1610                         updateLocal(bv, FULL, false);
1611                 }
1612         } else if ((cursor_.x() - offset) > 20 &&
1613                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1614                    > (bv->workWidth() - 20)) {
1615                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1616                 updateLocal(bv, FULL, false);
1617         } else if ((cursor_.x() - offset) < 20) {
1618                 scroll(bv, 20 - cursor_.x() + offset);
1619                 updateLocal(bv, FULL, false);
1620         } else if (scroll() && top_x > 20 &&
1621                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1622                 scroll(bv, old_x - cursor_.x());
1623                 updateLocal(bv, FULL, false);
1624         }
1625         if (the_locking_inset) {
1626                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1627                 inset_y = cursor_.y();
1628         }
1629         if ((!the_locking_inset ||
1630              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1631             actcell != oldcell) {
1632                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1633                 bv->owner()->getDialogs().updateTabular(inset);
1634                 oldcell = actcell;
1635         }
1636         in_reset_pos = 0;
1637 }
1638
1639
1640 Inset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1641 {
1642         if (lock && !old_locking_inset) {
1643                 if (activateCellInset(bv))
1644                         return DISPATCHED;
1645         } else {
1646                 bool moved = isRightToLeft(bv)
1647                         ? movePrevCell(bv) : moveNextCell(bv);
1648                 if (!moved)
1649                         return FINISHED_RIGHT;
1650                 if (lock && activateCellInset(bv))
1651                         return DISPATCHED;
1652         }
1653         resetPos(bv);
1654         return DISPATCHED_NOUPDATE;
1655 }
1656
1657
1658 Inset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1659 {
1660         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1661         if (!moved)
1662                 return FINISHED;
1663         if (lock) {       // behind the inset
1664                 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1665                         return DISPATCHED;
1666         }
1667         resetPos(bv);
1668         return DISPATCHED_NOUPDATE;
1669 }
1670
1671
1672 Inset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1673 {
1674         int const ocell = actcell;
1675         actcell = tabular->GetCellAbove(actcell);
1676         if (actcell == ocell) // we moved out of the inset
1677                 return FINISHED_UP;
1678         resetPos(bv);
1679         if (lock) {
1680                 int x = 0;
1681                 int y = 0;
1682                 if (old_locking_inset) {
1683                         old_locking_inset->getCursorPos(bv, x, y);
1684                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1685                 }
1686                 if (activateCellInset(bv, x, 0))
1687                         return DISPATCHED;
1688         }
1689         return DISPATCHED_NOUPDATE;
1690 }
1691
1692
1693 Inset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1694 {
1695         int const ocell = actcell;
1696         actcell = tabular->GetCellBelow(actcell);
1697         if (actcell == ocell) // we moved out of the inset
1698                 return FINISHED_DOWN;
1699         resetPos(bv);
1700         if (lock) {
1701                 int x = 0;
1702                 int y = 0;
1703                 if (old_locking_inset) {
1704                         old_locking_inset->getCursorPos(bv, x, y);
1705                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1706                 }
1707                 if (activateCellInset(bv, x, 0))
1708                         return DISPATCHED;
1709         }
1710         return DISPATCHED_NOUPDATE;
1711 }
1712
1713
1714 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1715 {
1716         if (isRightToLeft(bv)) {
1717                 if (tabular->IsFirstCellInRow(actcell)) {
1718                         int row = tabular->row_of_cell(actcell);
1719                         if (row == tabular->rows() - 1)
1720                                 return false;
1721                         actcell = tabular->GetLastCellInRow(row);
1722                         actcell = tabular->GetCellBelow(actcell);
1723                 } else {
1724                         if (!actcell)
1725                                 return false;
1726                         --actcell;
1727                 }
1728         } else {
1729                 if (tabular->IsLastCell(actcell))
1730                         return false;
1731                 ++actcell;
1732         }
1733         if (lock) {
1734                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1735                         isRightToLeftPar(bv->buffer()->params);
1736                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1737         }
1738         resetPos(bv);
1739         return true;
1740 }
1741
1742
1743 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1744 {
1745         if (isRightToLeft(bv)) {
1746                 if (tabular->IsLastCellInRow(actcell)) {
1747                         int row = tabular->row_of_cell(actcell);
1748                         if (row == 0)
1749                                 return false;
1750                         actcell = tabular->GetFirstCellInRow(row);
1751                         actcell = tabular->GetCellAbove(actcell);
1752                 } else {
1753                         if (tabular->IsLastCell(actcell))
1754                                 return false;
1755                         ++actcell;
1756                 }
1757         } else {
1758                 if (!actcell) // first cell
1759                         return false;
1760                 --actcell;
1761         }
1762         if (lock) {
1763                 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1764                         isRightToLeftPar(bv->buffer()->params);
1765                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1766         }
1767         resetPos(bv);
1768         return true;
1769 }
1770
1771
1772 bool InsetTabular::deletable() const
1773 {
1774         return true;
1775 }
1776
1777
1778 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1779                            bool selectall)
1780 {
1781         if (selectall) {
1782                 setSelection(0, tabular->GetNumberOfCells() - 1);
1783         }
1784         if (hasSelection()) {
1785                 setUndo(bv, Undo::EDIT,
1786                         bv->text->cursor.par(),
1787                         bv->text->cursor.par()->next());
1788                 bool const frozen = undo_frozen;
1789                 if (!frozen)
1790                         freezeUndo();
1791                 // apply the fontchange on the whole selection
1792                 int sel_row_start;
1793                 int sel_row_end;
1794                 int sel_col_start;
1795                 int sel_col_end;
1796                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1797                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1798                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1799                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1800                         }
1801                 }
1802                 if (!frozen)
1803                         unFreezeUndo();
1804                 if (selectall)
1805                         clearSelection();
1806                 updateLocal(bv, INIT, true);
1807         }
1808         if (the_locking_inset)
1809                 the_locking_inset->setFont(bv, font, tall);
1810 }
1811
1812
1813 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1814 {
1815         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1816
1817         int i = 0;
1818         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1819                 string const tmp = tabularFeature[i].feature;
1820
1821                 if (tmp == what.substr(0, tmp.length())) {
1822                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1823                         //tabularFeatures[i].feature.length())) {
1824                         action = tabularFeature[i].action;
1825                         break;
1826                 }
1827         }
1828         if (action == LyXTabular::LAST_ACTION)
1829                 return false;
1830
1831         string const val =
1832                 ltrim(what.substr(tabularFeature[i].feature.length()));
1833         tabularFeatures(bv, action, val);
1834         return true;
1835 }
1836
1837 static void checkLongtableSpecial(LyXTabular::ltType & ltt,
1838                                   string const & special, bool & flag)
1839 {
1840         if (special == "dl_above") {
1841                 ltt.topDL = flag;
1842                 ltt.set = false;
1843         } else if (special == "dl_below") {
1844                 ltt.bottomDL = flag;
1845                 ltt.set = false;
1846         } else if (special == "empty") {
1847                 ltt.empty = flag;
1848                 ltt.set = false;
1849         } else if (flag) {
1850                 ltt.empty = false;
1851                 ltt.set = true;
1852         }
1853 }
1854
1855
1856 void InsetTabular::tabularFeatures(BufferView * bv,
1857                                    LyXTabular::Feature feature,
1858                                    string const & value)
1859 {
1860         int sel_col_start;
1861         int sel_col_end;
1862         int sel_row_start;
1863         int sel_row_end;
1864         bool setLines = false;
1865         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1866         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1867
1868         switch (feature) {
1869         case LyXTabular::M_ALIGN_LEFT:
1870         case LyXTabular::ALIGN_LEFT:
1871                 setAlign = LYX_ALIGN_LEFT;
1872                 break;
1873         case LyXTabular::M_ALIGN_RIGHT:
1874         case LyXTabular::ALIGN_RIGHT:
1875                 setAlign = LYX_ALIGN_RIGHT;
1876                 break;
1877         case LyXTabular::M_ALIGN_CENTER:
1878         case LyXTabular::ALIGN_CENTER:
1879                 setAlign = LYX_ALIGN_CENTER;
1880                 break;
1881         case LyXTabular::M_VALIGN_TOP:
1882         case LyXTabular::VALIGN_TOP:
1883                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1884                 break;
1885         case LyXTabular::M_VALIGN_BOTTOM:
1886         case LyXTabular::VALIGN_BOTTOM:
1887                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1888                 break;
1889         case LyXTabular::M_VALIGN_CENTER:
1890         case LyXTabular::VALIGN_CENTER:
1891                 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1892                 break;
1893         default:
1894                 break;
1895         }
1896         if (hasSelection()) {
1897                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1898         } else {
1899                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1900                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1901         }
1902         setUndo(bv, Undo::FINISH,
1903                 bv->text->cursor.par(),
1904                 bv->text->cursor.par()->next());
1905
1906         int row =  tabular->row_of_cell(actcell);
1907         int column = tabular->column_of_cell(actcell);
1908         bool flag = true;
1909         LyXTabular::ltType ltt;
1910
1911         switch (feature) {
1912         case LyXTabular::SET_PWIDTH:
1913         {
1914                 LyXLength const vallen(value);
1915                 LyXLength const & tmplen = tabular->GetColumnPWidth(actcell);
1916
1917                 bool const update = (tmplen != vallen);
1918                 tabular->SetColumnPWidth(actcell, vallen);
1919                 if (update) {
1920                         int cell;
1921                         for (int i = 0; i < tabular->rows(); ++i) {
1922                                 cell = tabular->GetCellNumber(i,column);
1923                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1924                         }
1925                         updateLocal(bv, INIT, true);
1926                 }
1927         }
1928         break;
1929         case LyXTabular::SET_MPWIDTH:
1930         {
1931                 LyXLength const vallen(value);
1932                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1933
1934                 bool const update = (tmplen != vallen);
1935                 tabular->SetMColumnPWidth(actcell, vallen);
1936                 if (update) {
1937                         for (int i = 0; i < tabular->rows(); ++i) {
1938                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1939                                         resizeLyXText(bv);
1940                         }
1941                         updateLocal(bv, INIT, true);
1942                 }
1943         }
1944         break;
1945         case LyXTabular::SET_SPECIAL_COLUMN:
1946         case LyXTabular::SET_SPECIAL_MULTI:
1947                 tabular->SetAlignSpecial(actcell,value,feature);
1948                 updateLocal(bv, FULL, true);
1949                 break;
1950         case LyXTabular::APPEND_ROW:
1951                 // append the row into the tabular
1952                 unlockInsetInInset(bv, the_locking_inset);
1953                 tabular->AppendRow(bv->buffer()->params, actcell);
1954                 updateLocal(bv, INIT, true);
1955                 break;
1956         case LyXTabular::APPEND_COLUMN:
1957                 // append the column into the tabular
1958                 unlockInsetInInset(bv, the_locking_inset);
1959                 tabular->AppendColumn(bv->buffer()->params, actcell);
1960                 actcell = tabular->GetCellNumber(row, column);
1961                 updateLocal(bv, INIT, true);
1962                 break;
1963         case LyXTabular::DELETE_ROW:
1964                 unlockInsetInInset(bv, the_locking_inset);
1965                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1966                         tabular->DeleteRow(sel_row_start);
1967                 }
1968                 if (sel_row_start >= tabular->rows())
1969                         --sel_row_start;
1970                 actcell = tabular->GetCellNumber(sel_row_start, column);
1971                 clearSelection();
1972                 updateLocal(bv, INIT, true);
1973                 break;
1974         case LyXTabular::DELETE_COLUMN:
1975                 unlockInsetInInset(bv, the_locking_inset);
1976                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1977                         tabular->DeleteColumn(sel_col_start);
1978                 }
1979                 if (sel_col_start >= tabular->columns())
1980                         --sel_col_start;
1981                 actcell = tabular->GetCellNumber(row, sel_col_start);
1982                 clearSelection();
1983                 updateLocal(bv, INIT, true);
1984                 break;
1985         case LyXTabular::M_TOGGLE_LINE_TOP:
1986                 flag = false;
1987         case LyXTabular::TOGGLE_LINE_TOP:
1988         {
1989                 bool lineSet = !tabular->TopLine(actcell, flag);
1990                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1991                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1992                                 tabular->SetTopLine(
1993                                         tabular->GetCellNumber(i, j),
1994                                         lineSet, flag);
1995                 updateLocal(bv, INIT, true);
1996                 break;
1997         }
1998
1999         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2000                 flag = false;
2001         case LyXTabular::TOGGLE_LINE_BOTTOM:
2002         {
2003                 bool lineSet = !tabular->BottomLine(actcell, flag);
2004                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2005                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2006                                 tabular->SetBottomLine(
2007                                         tabular->GetCellNumber(i, j),
2008                                         lineSet,
2009                                         flag);
2010                 updateLocal(bv, INIT, true);
2011                 break;
2012         }
2013
2014         case LyXTabular::M_TOGGLE_LINE_LEFT:
2015                 flag = false;
2016         case LyXTabular::TOGGLE_LINE_LEFT:
2017         {
2018                 bool lineSet = !tabular->LeftLine(actcell, flag);
2019                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2020                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2021                                 tabular->SetLeftLine(
2022                                         tabular->GetCellNumber(i,j),
2023                                         lineSet,
2024                                         flag);
2025                 updateLocal(bv, INIT, true);
2026                 break;
2027         }
2028
2029         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2030                 flag = false;
2031         case LyXTabular::TOGGLE_LINE_RIGHT:
2032         {
2033                 bool lineSet = !tabular->RightLine(actcell, flag);
2034                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2035                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2036                                 tabular->SetRightLine(
2037                                         tabular->GetCellNumber(i,j),
2038                                         lineSet,
2039                                         flag);
2040                 updateLocal(bv, INIT, true);
2041                 break;
2042         }
2043
2044         case LyXTabular::M_ALIGN_LEFT:
2045         case LyXTabular::M_ALIGN_RIGHT:
2046         case LyXTabular::M_ALIGN_CENTER:
2047                 flag = false;
2048         case LyXTabular::ALIGN_LEFT:
2049         case LyXTabular::ALIGN_RIGHT:
2050         case LyXTabular::ALIGN_CENTER:
2051                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2052                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2053                                 tabular->SetAlignment(
2054                                         tabular->GetCellNumber(i, j),
2055                                         setAlign,
2056                                         flag);
2057                 updateLocal(bv, INIT, true);
2058                 break;
2059         case LyXTabular::M_VALIGN_TOP:
2060         case LyXTabular::M_VALIGN_BOTTOM:
2061         case LyXTabular::M_VALIGN_CENTER:
2062                 flag = false;
2063         case LyXTabular::VALIGN_TOP:
2064         case LyXTabular::VALIGN_BOTTOM:
2065         case LyXTabular::VALIGN_CENTER:
2066                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2067                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2068                                 tabular->SetVAlignment(
2069                                         tabular->GetCellNumber(i, j),
2070                                         setVAlign, flag);
2071                 updateLocal(bv, INIT, true);
2072                 break;
2073         case LyXTabular::MULTICOLUMN:
2074         {
2075                 if (sel_row_start != sel_row_end) {
2076                         Alert::alert(_("Impossible Operation!"),
2077                                    _("Multicolumns can only be horizontally."),
2078                                    _("Sorry."));
2079                         return;
2080                 }
2081                 // just multicol for one Single Cell
2082                 if (!hasSelection()) {
2083                         // check wether we are completly in a multicol
2084                         if (tabular->IsMultiColumn(actcell)) {
2085                                 tabular->UnsetMultiColumn(actcell);
2086                                 updateLocal(bv, INIT, true);
2087                         } else {
2088                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
2089                                 updateLocal(bv, CELL, true);
2090                         }
2091                         return;
2092                 }
2093                 // we have a selection so this means we just add all this
2094                 // cells to form a multicolumn cell
2095                 int s_start;
2096                 int s_end;
2097
2098                 if (sel_cell_start > sel_cell_end) {
2099                         s_start = sel_cell_end;
2100                         s_end = sel_cell_start;
2101                 } else {
2102                         s_start = sel_cell_start;
2103                         s_end = sel_cell_end;
2104                 }
2105                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
2106                 actcell = s_start;
2107                 clearSelection();
2108                 updateLocal(bv, INIT, true);
2109                 break;
2110         }
2111         case LyXTabular::SET_ALL_LINES:
2112                 setLines = true;
2113         case LyXTabular::UNSET_ALL_LINES:
2114                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2115                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2116                                 tabular->SetAllLines(
2117                                         tabular->GetCellNumber(i,j), setLines);
2118                 updateLocal(bv, INIT, true);
2119                 break;
2120         case LyXTabular::SET_LONGTABULAR:
2121                 tabular->SetLongTabular(true);
2122                 updateLocal(bv, INIT, true); // because this toggles displayed
2123                 break;
2124         case LyXTabular::UNSET_LONGTABULAR:
2125                 tabular->SetLongTabular(false);
2126                 updateLocal(bv, INIT, true); // because this toggles displayed
2127                 break;
2128         case LyXTabular::SET_ROTATE_TABULAR:
2129                 tabular->SetRotateTabular(true);
2130                 break;
2131         case LyXTabular::UNSET_ROTATE_TABULAR:
2132                 tabular->SetRotateTabular(false);
2133                 break;
2134         case LyXTabular::SET_ROTATE_CELL:
2135                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2136                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2137                                 tabular->SetRotateCell(
2138                                         tabular->GetCellNumber(i, j),
2139                                         true);
2140                 break;
2141         case LyXTabular::UNSET_ROTATE_CELL:
2142                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2143                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2144                                 tabular->SetRotateCell(
2145                                         tabular->GetCellNumber(i, j), false);
2146                 break;
2147         case LyXTabular::SET_USEBOX:
2148         {
2149                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2150                 if (val == tabular->GetUsebox(actcell))
2151                         val = LyXTabular::BOX_NONE;
2152                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2153                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2154                                 tabular->SetUsebox(
2155                                         tabular->GetCellNumber(i, j), val);
2156                 break;
2157         }
2158         case LyXTabular::UNSET_LTFIRSTHEAD:
2159                 flag = false;
2160         case LyXTabular::SET_LTFIRSTHEAD:
2161                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2162                 checkLongtableSpecial(ltt, value, flag);
2163                 tabular->SetLTHead(row, flag, ltt, true);
2164                 break;
2165         case LyXTabular::UNSET_LTHEAD:
2166                 flag = false;
2167         case LyXTabular::SET_LTHEAD:
2168                 (void)tabular->GetRowOfLTHead(row, ltt);
2169                 checkLongtableSpecial(ltt, value, flag);
2170                 tabular->SetLTHead(row, flag, ltt, false);
2171                 break;
2172         case LyXTabular::UNSET_LTFOOT:
2173                 flag = false;
2174         case LyXTabular::SET_LTFOOT:
2175                 (void)tabular->GetRowOfLTFoot(row, ltt);
2176                 checkLongtableSpecial(ltt, value, flag);
2177                 tabular->SetLTFoot(row, flag, ltt, false);
2178                 break;
2179         case LyXTabular::UNSET_LTLASTFOOT:
2180                 flag = false;
2181         case LyXTabular::SET_LTLASTFOOT:
2182                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2183                 checkLongtableSpecial(ltt, value, flag);
2184                 tabular->SetLTFoot(row, flag, ltt, true);
2185                 break;
2186         case LyXTabular::SET_LTNEWPAGE:
2187         {
2188                 bool what = !tabular->GetLTNewPage(row);
2189                 tabular->SetLTNewPage(row, what);
2190                 break;
2191         }
2192         // dummy stuff just to avoid warnings
2193         case LyXTabular::LAST_ACTION:
2194                 break;
2195         }
2196 }
2197
2198
2199 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
2200                                      bool behind)
2201 {
2202         UpdatableInset * inset =
2203                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2204         LyXFont font(LyXFont::ALL_SANE);
2205         if (behind) {
2206                 x = inset->x() + inset->width(bv, font);
2207                 y = inset->descent(bv, font);
2208         }
2209         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2210         //inset_y = cursor.y();
2211         inset->edit(bv, x,  y, button);
2212         if (!the_locking_inset)
2213                 return false;
2214         updateLocal(bv, CELL, false);
2215         return (the_locking_inset != 0);
2216 }
2217
2218
2219 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2220                                         mouse_button::state button)
2221 {
2222         inset_x = cursor_.x()
2223                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2224         inset_y = cursor_.y();
2225         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2226 }
2227
2228
2229 bool InsetTabular::insetHit(BufferView *, int x, int) const
2230 {
2231         return (x + top_x)
2232                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2233 }
2234
2235
2236 // This returns paperWidth() if the cell-width is unlimited or the width
2237 // in pixels if we have a pwidth for this cell.
2238 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2239 {
2240         LyXLength const len = tabular->GetPWidth(cell);
2241
2242         if (len.zero())
2243                 return -1;
2244         return len.inPixels(latexTextWidth(bv), bv->text->defaultHeight());
2245 }
2246
2247
2248 int InsetTabular::getMaxWidth(BufferView * bv,
2249                               UpdatableInset const * inset) const
2250 {
2251         int cell = tabular->GetCellFromInset(inset, actcell);
2252
2253         if (cell == -1) {
2254                 lyxerr << "Own inset not found, shouldn't really happen!"
2255                        << endl;
2256                 return -1;
2257         }
2258
2259         int w = getMaxWidthOfCell(bv, cell);
2260         if (w > 0) {
2261                 // because the inset then subtracts it's top_x and owner->x()
2262                 w += (inset->x() - top_x);
2263         }
2264
2265         return w;
2266 }
2267
2268
2269 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2270 {
2271         resizeLyXText(bv, recursive);
2272 }
2273
2274
2275 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2276 {
2277         if (force) {
2278                 for(int i = 0; i < tabular->rows(); ++i) {
2279                         for(int j = 0; j < tabular->columns(); ++j) {
2280                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2281                         }
2282                 }
2283         }
2284         need_update = FULL;
2285 }
2286
2287
2288 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2289                                    bool const recursive) const
2290 {
2291         if (the_locking_inset)
2292                 return the_locking_inset->getLyXText(bv, recursive);
2293 #if 0
2294         // if we're locked lock the actual insettext and return it's LyXText!!!
2295         if (locked) {
2296                 UpdatableInset * inset =
2297                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2298                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2299                 return the_locking_inset->getLyXText(bv, recursive);
2300         }
2301 #endif
2302         return Inset::getLyXText(bv, recursive);
2303 }
2304
2305
2306 bool InsetTabular::showInsetDialog(BufferView * bv) const
2307 {
2308         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2309                 bv->owner()->getDialogs().
2310                         showTabular(const_cast<InsetTabular *>(this));
2311         return true;
2312 }
2313
2314
2315 void InsetTabular::openLayoutDialog(BufferView * bv) const
2316 {
2317         if (the_locking_inset) {
2318                 InsetTabular * i = static_cast<InsetTabular *>
2319                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2320                 if (i) {
2321                         i->openLayoutDialog(bv);
2322                         return;
2323                 }
2324         }
2325         bv->owner()->getDialogs().showTabular(
2326                 const_cast<InsetTabular *>(this));
2327 }
2328
2329
2330 //
2331 // function returns an object as defined in func_status.h:
2332 // states OK, Unknown, Disabled, On, Off.
2333 //
2334 FuncStatus InsetTabular::getStatus(string const & what) const
2335 {
2336         int action = LyXTabular::LAST_ACTION;
2337         FuncStatus status;
2338
2339         int i = 0;
2340         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2341                 string const tmp = tabularFeature[i].feature;
2342                 if (tmp == what.substr(0, tmp.length())) {
2343                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2344                         //   tabularFeatures[i].feature.length())) {
2345                         action = tabularFeature[i].action;
2346                         break;
2347                 }
2348         }
2349         if (action == LyXTabular::LAST_ACTION) {
2350                 status.clear();
2351                 return status.unknown(true);
2352         }
2353
2354         string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
2355
2356         int sel_row_start;
2357         int sel_row_end;
2358         int dummy;
2359         LyXTabular::ltType dummyltt;
2360         bool flag = true;
2361
2362         if (hasSelection()) {
2363                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2364         } else {
2365                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2366         }
2367
2368         switch (action) {
2369         case LyXTabular::SET_PWIDTH:
2370         case LyXTabular::SET_MPWIDTH:
2371         case LyXTabular::SET_SPECIAL_COLUMN:
2372         case LyXTabular::SET_SPECIAL_MULTI:
2373                 return status.disabled(true);
2374
2375         case LyXTabular::APPEND_ROW:
2376         case LyXTabular::APPEND_COLUMN:
2377         case LyXTabular::DELETE_ROW:
2378         case LyXTabular::DELETE_COLUMN:
2379         case LyXTabular::SET_ALL_LINES:
2380         case LyXTabular::UNSET_ALL_LINES:
2381                 return status.clear();
2382
2383         case LyXTabular::MULTICOLUMN:
2384                 status.setOnOff(tabular->IsMultiColumn(actcell));
2385                 break;
2386         case LyXTabular::M_TOGGLE_LINE_TOP:
2387                 flag = false;
2388         case LyXTabular::TOGGLE_LINE_TOP:
2389                 status.setOnOff(tabular->TopLine(actcell, flag));
2390                 break;
2391         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2392                 flag = false;
2393         case LyXTabular::TOGGLE_LINE_BOTTOM:
2394                 status.setOnOff(tabular->BottomLine(actcell, flag));
2395                 break;
2396         case LyXTabular::M_TOGGLE_LINE_LEFT:
2397                 flag = false;
2398         case LyXTabular::TOGGLE_LINE_LEFT:
2399                 status.setOnOff(tabular->LeftLine(actcell, flag));
2400                 break;
2401         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2402                 flag = false;
2403         case LyXTabular::TOGGLE_LINE_RIGHT:
2404                 status.setOnOff(tabular->RightLine(actcell, flag));
2405                 break;
2406         case LyXTabular::M_ALIGN_LEFT:
2407                 flag = false;
2408         case LyXTabular::ALIGN_LEFT:
2409                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2410                 break;
2411         case LyXTabular::M_ALIGN_RIGHT:
2412                 flag = false;
2413         case LyXTabular::ALIGN_RIGHT:
2414                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2415                 break;
2416         case LyXTabular::M_ALIGN_CENTER:
2417                 flag = false;
2418         case LyXTabular::ALIGN_CENTER:
2419                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2420                 break;
2421         case LyXTabular::M_VALIGN_TOP:
2422                 flag = false;
2423         case LyXTabular::VALIGN_TOP:
2424                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2425                 break;
2426         case LyXTabular::M_VALIGN_BOTTOM:
2427                 flag = false;
2428         case LyXTabular::VALIGN_BOTTOM:
2429                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2430                 break;
2431         case LyXTabular::M_VALIGN_CENTER:
2432                 flag = false;
2433         case LyXTabular::VALIGN_CENTER:
2434                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2435                 break;
2436         case LyXTabular::SET_LONGTABULAR:
2437                 status.setOnOff(tabular->IsLongTabular());
2438                 break;
2439         case LyXTabular::UNSET_LONGTABULAR:
2440                 status.setOnOff(!tabular->IsLongTabular());
2441                 break;
2442         case LyXTabular::SET_ROTATE_TABULAR:
2443                 status.setOnOff(tabular->GetRotateTabular());
2444                 break;
2445         case LyXTabular::UNSET_ROTATE_TABULAR:
2446                 status.setOnOff(!tabular->GetRotateTabular());
2447                 break;
2448         case LyXTabular::SET_ROTATE_CELL:
2449                 status.setOnOff(tabular->GetRotateCell(actcell));
2450                 break;
2451         case LyXTabular::UNSET_ROTATE_CELL:
2452                 status.setOnOff(!tabular->GetRotateCell(actcell));
2453                 break;
2454         case LyXTabular::SET_USEBOX:
2455                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2456                 break;
2457         case LyXTabular::SET_LTFIRSTHEAD:
2458                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2459                 break;
2460         case LyXTabular::SET_LTHEAD:
2461                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2462                 break;
2463         case LyXTabular::SET_LTFOOT:
2464                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2465                 break;
2466         case LyXTabular::SET_LTLASTFOOT:
2467                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2468                 break;
2469         case LyXTabular::SET_LTNEWPAGE:
2470                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2471                 break;
2472         default:
2473                 status.clear();
2474                 status.disabled(true);
2475                 break;
2476         }
2477         return status;
2478 }
2479
2480
2481 vector<string> const InsetTabular::getLabelList() const
2482 {
2483         return tabular->getLabelList();
2484 }
2485
2486
2487 bool InsetTabular::copySelection(BufferView * bv)
2488 {
2489         if (!hasSelection())
2490                 return false;
2491
2492         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2493         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2494         if (sel_col_start > sel_col_end) {
2495                 sel_col_start = sel_col_end;
2496                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2497         } else {
2498                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2499         }
2500         int const columns = sel_col_end - sel_col_start + 1;
2501
2502         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2503         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2504         if (sel_row_start > sel_row_end) {
2505                 swap(sel_row_start, sel_row_end);
2506         }
2507         int const rows = sel_row_end - sel_row_start + 1;
2508
2509         delete paste_tabular;
2510         paste_tabular = new LyXTabular(bv->buffer()->params,
2511                                        this, *tabular); // rows, columns);
2512         for (int i = 0; i < sel_row_start; ++i)
2513                 paste_tabular->DeleteRow(0);
2514         while (paste_tabular->rows() > rows)
2515                 paste_tabular->DeleteRow(rows);
2516         paste_tabular->SetTopLine(0, true, true);
2517         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2518                                      true, true);
2519         for (int i = 0; i < sel_col_start; ++i)
2520                 paste_tabular->DeleteColumn(0);
2521         while (paste_tabular->columns() > columns)
2522                 paste_tabular->DeleteColumn(columns);
2523         paste_tabular->SetLeftLine(0, true, true);
2524         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2525                                     true, true);
2526
2527         ostringstream sstr;
2528         paste_tabular->ascii(bv->buffer(), sstr,
2529                              (int)parOwner()->params().depth(), true, '\t');
2530         bv->stuffClipboard(sstr.str().c_str());
2531         return true;
2532 }
2533
2534
2535 bool InsetTabular::pasteSelection(BufferView * bv)
2536 {
2537         if (!paste_tabular)
2538                 return false;
2539
2540         for (int r1 = 0, r2 = actrow;
2541              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2542              ++r1, ++r2) {
2543                 for(int c1 = 0, c2 = actcol;
2544                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2545                     ++c1, ++c2) {
2546                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2547                             tabular->IsPartOfMultiColumn(r2,c2))
2548                                 continue;
2549                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2550                                 --c2;
2551                                 continue;
2552                         }
2553                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2554                                 --c1;
2555                                 continue;
2556                         }
2557                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2558                         int const n2 = tabular->GetCellNumber(r2, c2);
2559                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2560                         tabular->GetCellInset(n2)->setOwner(this);
2561                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2562                 }
2563         }
2564         return true;
2565 }
2566
2567
2568 bool InsetTabular::cutSelection()
2569 {
2570         if (!hasSelection())
2571                 return false;
2572
2573         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2574         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2575         if (sel_col_start > sel_col_end) {
2576                 sel_col_start = sel_col_end;
2577                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2578         } else {
2579                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2580         }
2581         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2582         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2583         if (sel_row_start > sel_row_end) {
2584                 swap(sel_row_start, sel_row_end);
2585         }
2586         if (sel_cell_start > sel_cell_end) {
2587                 swap(sel_cell_start, sel_cell_end);
2588         }
2589         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2590                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2591                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2592                 }
2593         }
2594         return true;
2595 }
2596
2597
2598 bool InsetTabular::isRightToLeft(BufferView * bv)
2599 {
2600         return bv->getParentLanguage(this)->RightToLeft();
2601 }
2602
2603
2604 bool InsetTabular::nodraw() const
2605 {
2606         if (!UpdatableInset::nodraw() && the_locking_inset)
2607                 return the_locking_inset->nodraw();
2608         return UpdatableInset::nodraw();
2609 }
2610
2611
2612 int InsetTabular::scroll(bool recursive) const
2613 {
2614         int sx = UpdatableInset::scroll(false);
2615
2616         if (recursive && the_locking_inset)
2617                 sx += the_locking_inset->scroll(recursive);
2618
2619         return sx;
2620 }
2621
2622
2623 bool InsetTabular::doClearArea() const
2624 {
2625         return !locked || (need_update & (FULL|INIT));
2626 }
2627
2628
2629 void InsetTabular::getSelection(int & srow, int & erow,
2630                                 int & scol, int & ecol) const
2631 {
2632         int const start = hasSelection() ? sel_cell_start : actcell;
2633         int const end = hasSelection() ? sel_cell_end : actcell;
2634
2635         srow = tabular->row_of_cell(start);
2636         erow = tabular->row_of_cell(end);
2637         if (srow > erow) {
2638                 swap(srow, erow);
2639         }
2640
2641         scol = tabular->column_of_cell(start);
2642         ecol = tabular->column_of_cell(end);
2643         if (scol > ecol) {
2644                 swap(scol, ecol);
2645         } else {
2646                 ecol = tabular->right_column_of_cell(end);
2647         }
2648 }
2649
2650
2651 Paragraph * InsetTabular::getParFromID(int id) const
2652 {
2653         Paragraph * result;
2654         for(int i = 0; i < tabular->rows(); ++i) {
2655                 for(int j = 0; j < tabular->columns(); ++j) {
2656                         if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2657                                 return result;
2658                 }
2659         }
2660         return 0;
2661 }
2662
2663
2664 Paragraph * InsetTabular::firstParagraph() const
2665 {
2666         if (the_locking_inset)
2667                 return the_locking_inset->firstParagraph();
2668         return 0;
2669 }
2670
2671
2672 Paragraph * InsetTabular::getFirstParagraph(int i) const
2673 {
2674         return (i < tabular->GetNumberOfCells())
2675                 ? tabular->GetCellInset(i)->getFirstParagraph(0)
2676                 : 0;
2677 }
2678
2679
2680 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2681 {
2682         if (the_locking_inset)
2683                 return the_locking_inset->cursor(bv);
2684         return Inset::cursor(bv);
2685 }
2686
2687
2688 Inset * InsetTabular::getInsetFromID(int id_arg) const
2689 {
2690         if (id_arg == id())
2691                 return const_cast<InsetTabular *>(this);
2692
2693         Inset * result;
2694         for(int i = 0; i < tabular->rows(); ++i) {
2695                 for(int j = 0; j < tabular->columns(); ++j) {
2696                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2697                                 return result;
2698                 }
2699         }
2700         return 0;
2701 }
2702
2703
2704 WordLangTuple const
2705 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2706 {
2707         nodraw(true);
2708         if (the_locking_inset) {
2709                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2710                 if (!word.word().empty()) {
2711                         nodraw(false);
2712                         return word;
2713                 }
2714                 if (tabular->IsLastCell(actcell)) {
2715                         bv->unlockInset(const_cast<InsetTabular *>(this));
2716                         nodraw(false);
2717                         return WordLangTuple();
2718                 }
2719                 ++actcell;
2720         }
2721         // otherwise we have to lock the next inset and ask for it's selecttion
2722         UpdatableInset * inset =
2723                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2724         inset->edit(bv, 0,  0, mouse_button::none);
2725         WordLangTuple word(selectNextWordInt(bv, value));
2726         nodraw(false);
2727         if (!word.word().empty())
2728                 resetPos(bv);
2729         return word;
2730 }
2731
2732
2733 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2734 {
2735         // when entering this function the inset should be ALWAYS locked!
2736         lyx::Assert(the_locking_inset);
2737
2738         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2739         if (!word.word().empty())
2740                 return word;
2741
2742         if (tabular->IsLastCell(actcell)) {
2743                 bv->unlockInset(const_cast<InsetTabular *>(this));
2744                 return WordLangTuple();
2745         }
2746
2747         // otherwise we have to lock the next inset and ask for it's selecttion
2748         UpdatableInset * inset =
2749                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2750         inset->edit(bv);
2751         return selectNextWordInt(bv, value);
2752 }
2753
2754
2755 void InsetTabular::selectSelectedWord(BufferView * bv)
2756 {
2757         if (the_locking_inset) {
2758                 the_locking_inset->selectSelectedWord(bv);
2759                 return;
2760         }
2761         return;
2762 }
2763
2764
2765 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2766 {
2767         if (the_locking_inset) {
2768                 the_locking_inset->toggleSelection(bv, kill_selection);
2769         }
2770 }
2771
2772
2773 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2774                                  bool cs, bool mw)
2775 {
2776         if (the_locking_inset) {
2777                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2778                         updateLocal(bv, CELL, false);
2779                         return true;
2780                 }
2781                 if (tabular->IsLastCell(actcell))
2782                         return false;
2783                 ++actcell;
2784         }
2785         InsetText * inset = tabular->GetCellInset(actcell);
2786         if (inset->searchForward(bv, str, cs, mw)) {
2787                 updateLocal(bv, FULL, false);
2788                 return true;
2789         }
2790         while (!tabular->IsLastCell(actcell)) {
2791                 ++actcell;
2792                 inset = tabular->GetCellInset(actcell);
2793                 if (inset->searchForward(bv, str, cs, mw)) {
2794                         updateLocal(bv, FULL, false);
2795                         return true;
2796                 }
2797         }
2798         return false;
2799 }
2800
2801
2802 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2803                                bool cs, bool mw)
2804 {
2805         if (the_locking_inset) {
2806                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2807                         updateLocal(bv, CELL, false);
2808                         return true;
2809                 }
2810         }
2811         if (!locked)
2812                 actcell = tabular->GetNumberOfCells();
2813
2814         while (actcell) {
2815                 --actcell;
2816                 InsetText * inset = tabular->GetCellInset(actcell);
2817                 if (inset->searchBackward(bv, str, cs, mw)) {
2818                         updateLocal(bv, CELL, false);
2819                         return true;
2820                 }
2821         }
2822         return false;
2823 }
2824
2825
2826 bool InsetTabular::insetAllowed(Inset::Code code) const
2827 {
2828         if (the_locking_inset)
2829                 return the_locking_inset->insetAllowed(code);
2830         // we return true here because if the inset is not locked someone
2831         // wants to insert something in one of our insettexts and we generally
2832         // allow to do so.
2833         return true;
2834 }
2835
2836
2837 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2838 {
2839         const int cell = tabular->GetCellFromInset(in, actcell);
2840
2841         if (cell != -1)
2842                 return tabular->GetPWidth(cell).zero();
2843
2844         // well we didn't obviously find it so maybe our owner knows more
2845         if (owner())
2846                 return owner()->forceDefaultParagraphs(in);
2847         // if we're here there is really something strange going on!!!
2848         return false;
2849 }
2850
2851 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2852                                      bool usePaste)
2853 {
2854         if (buf.length() <= 0)
2855                 return true;
2856
2857         int cols = 1;
2858         int rows = 1;
2859         int maxCols = 1;
2860         string::size_type len = buf.length();
2861         string::size_type p = 0;
2862
2863         while (p < len &&
2864                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2865         {
2866                 switch (buf[p]) {
2867                 case '\t':
2868                         ++cols;
2869                         break;
2870                 case '\n':
2871                         if ((p+1) < len)
2872                                 ++rows;
2873                         maxCols = max(cols, maxCols);
2874                         cols = 1;
2875                         break;
2876                 }
2877                 ++p;
2878         }
2879         maxCols = max(cols, maxCols);
2880         LyXTabular * loctab;
2881         int cell = 0;
2882         int ocol = 0;
2883         int row = 0;
2884         if (usePaste) {
2885                 delete paste_tabular;
2886                 paste_tabular = new LyXTabular(bv->buffer()->params,
2887                                                this, rows, maxCols);
2888                 loctab = paste_tabular;
2889                 cols = 0;
2890         } else {
2891                 loctab = tabular.get();
2892                 cell = actcell;
2893                 ocol = actcol;
2894                 row = actrow;
2895         }
2896         string::size_type op = 0;
2897         int cells = loctab->GetNumberOfCells();
2898         p = 0;
2899         cols = ocol;
2900         rows = loctab->rows();
2901         int const columns = loctab->columns();
2902         while ((cell < cells) && (p < len) && (row < rows) &&
2903                (p = buf.find_first_of("\t\n", p)) != string::npos)
2904         {
2905                 if (p >= len)
2906                         break;
2907                 switch (buf[p]) {
2908                 case '\t':
2909                         // we can only set this if we are not too far right
2910                         if (cols < columns) {
2911                                 InsetText * ti = loctab->GetCellInset(cell);
2912                                 LyXFont const font = ti->getLyXText(bv)->
2913                                         getFont(bv->buffer(), ti->paragraph(), 0);
2914                                 ti->setText(buf.substr(op, p-op), font);
2915                                 ++cols;
2916                                 ++cell;
2917                         }
2918                         break;
2919                 case '\n':
2920                         // we can only set this if we are not too far right
2921                         if (cols < columns) {
2922                                 InsetText * ti = loctab->GetCellInset(cell);
2923                                 LyXFont const font = ti->getLyXText(bv)->
2924                                         getFont(bv->buffer(), ti->paragraph(), 0);
2925                                 ti->setText(buf.substr(op, p-op), font);
2926                         }
2927                         cols = ocol;
2928                         ++row;
2929                         if (row < rows)
2930                                 cell = loctab->GetCellNumber(row, cols);
2931                         break;
2932                 }
2933                 ++p;
2934                 op = p;
2935         }
2936         // check for the last cell if there is no trailing '\n'
2937         if ((cell < cells) && (op < len)) {
2938                 InsetText * ti = loctab->GetCellInset(cell);
2939                 LyXFont const font = ti->getLyXText(bv)->
2940                         getFont(bv->buffer(), ti->paragraph(), 0);
2941                 ti->setText(buf.substr(op, len-op), font);
2942         }
2943
2944         return true;
2945 }
2946
2947
2948 void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
2949 {
2950         int const rows = tabular->rows();
2951         int const columns = tabular->columns();
2952         for (int i = 0; i < rows; ++i) {
2953                 for (int j = 0; j < columns; ++j) {
2954                         tabular->GetCellInset(i,j)->addPreview(loader);
2955                 }
2956         }
2957 }