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