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