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