]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
Fixed some redraw problems in InsetText/Tabular/Collapsable. Fixed problems
[features.git] / src / insets / insettabular.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15
16 #include <cstdlib>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "insettabular.h"
23
24 #include "buffer.h"
25 #include "commandtags.h"
26 #include "debug.h"
27 #include "LaTeXFeatures.h"
28 #include "Painter.h"
29 #include "font.h"
30 #include "lyxtext.h"
31 #include "lyx_gui_misc.h"
32 #include "LyXView.h"
33 #include "lyxfunc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
36 #include "debug.h"
37 #include "lyxfunc.h"
38 #include "WorkArea.h"
39 #include "gettext.h"
40 #include "language.h"
41
42 using std::ostream;
43 using std::ifstream;
44 using std::max;
45 using std::endl;
46 using std::swap;
47 using std::max;
48
49
50 namespace {
51
52 const int ADD_TO_HEIGHT = 2;
53 const int ADD_TO_TABULAR_WIDTH = 2;
54 ///
55 LyXTabular * paste_tabular = 0;
56
57
58 struct tabular_features {
59         LyXTabular::Feature action;
60         string feature;
61 };
62
63 //tabular_features * tabularFeatures = 0;
64
65 tabular_features tabularFeatures[] =
66 {
67         { LyXTabular::APPEND_ROW, "append-row" },
68         { LyXTabular::APPEND_COLUMN, "append-column" },
69         { LyXTabular::DELETE_ROW, "delete-row" },
70         { LyXTabular::DELETE_COLUMN, "delete-column" },
71         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
72         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
73         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
74         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
75         { LyXTabular::ALIGN_LEFT, "align-left" },
76         { LyXTabular::ALIGN_RIGHT, "align-right" },
77         { LyXTabular::ALIGN_CENTER, "align-center" },
78         { LyXTabular::VALIGN_TOP, "valign-top" },
79         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
80         { LyXTabular::VALIGN_CENTER, "valign-center" },
81         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
82         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
83         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
84         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
85         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
86         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
87         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
88         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
89         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
90         { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
91         { LyXTabular::MULTICOLUMN, "multicolumn" },
92         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
93         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
94         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
95         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
96         { LyXTabular::SET_PWIDTH, "set-pwidth" },
97         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
98         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
99         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
100         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
101         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
102         { LyXTabular::SET_USEBOX, "set-usebox" },
103         { LyXTabular::SET_LTHEAD, "set-lthead" },
104         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
105         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
106         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
107         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
108         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
109         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
110         { LyXTabular::LAST_ACTION, "" }
111 };
112
113 } // namespace anon
114
115
116 bool InsetTabular::hasPasteBuffer() const
117 {
118         return (paste_tabular != 0);
119 }
120
121
122 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
123         : buffer(&buf)
124 {
125         if (rows <= 0)
126                 rows = 1;
127         if (columns <= 0)
128                 columns = 1;
129         //tabular = new LyXTabular(this, rows,columns);
130         tabular.reset(new LyXTabular(this, rows,columns));
131         // for now make it always display as display() inset
132         // just for test!!!
133         the_locking_inset = 0;
134         locked = no_selection = false;
135 #if 0
136         cursor_visible = false;
137 #endif
138         oldcell = -1;
139         actrow = actcell = 0;
140         clearSelection();
141         need_update = INIT;
142 }
143
144
145 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
146         : buffer(&buf)
147 {
148         //tabular = new LyXTabular(this, *(tab.tabular));
149         tabular.reset(new LyXTabular(this, *(tab.tabular)));
150         the_locking_inset = 0;
151         locked = no_selection = false;
152 #if 0
153         cursor_visible = false;
154 #endif
155         oldcell = -1;
156         actrow = actcell = 0;
157         sel_cell_start = sel_cell_end = 0;
158         need_update = INIT;
159 }
160
161
162 InsetTabular::~InsetTabular()
163 {
164         //delete tabular;
165         hideDialog();
166 }
167
168
169 Inset * InsetTabular::Clone(Buffer const & buf) const
170 {
171         InsetTabular * t = new InsetTabular(*this, buf);
172         //delete t->tabular;
173         //t->tabular = tabular->Clone(t);
174         t->tabular.reset(tabular->Clone(t));
175         return t;
176 }
177
178
179 void InsetTabular::Write(Buffer const * buf, ostream & os) const
180 {
181         os << " Tabular" << endl;
182         tabular->Write(buf, os);
183 }
184
185
186 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
187 {
188         bool const old_format = (lex.GetString() == "\\LyXTable");
189
190         //if (tabular)
191         //delete tabular;
192         //tabular = new LyXTabular(buf, this, lex);
193         tabular.reset(new LyXTabular(buf, this, lex));
194
195         need_update = INIT;
196
197         if (old_format)
198                 return;
199
200         lex.nextToken();
201         string token = lex.GetString();
202         while (lex.IsOK() && (token != "\\end_inset")) {
203                 lex.nextToken();
204                 token = lex.GetString();
205         }
206         if (token != "\\end_inset") {
207                 lex.printError("Missing \\end_inset at this point. "
208                                "Read: `$$Token'");
209         }
210 }
211
212
213 int InsetTabular::ascent(BufferView *, LyXFont const &) const
214 {
215         return tabular->GetAscentOfRow(0);
216 }
217
218
219 int InsetTabular::descent(BufferView *, LyXFont const &) const
220 {
221         return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
222 }
223
224
225 int InsetTabular::width(BufferView *, LyXFont const &) const
226 {
227         return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
228 }
229
230
231 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
232                         float & x, bool cleared) const
233 {
234         if (nodraw())
235                 return;
236         if (bv->text->status == LyXText::CHANGED_IN_DRAW)
237                 return;
238
239 //      lyxerr << "InsetTabular::draw(" << need_update << ")\n";
240
241         Painter & pain = bv->painter();
242         int i;
243         int j;
244         int nx;
245
246 #if 0
247         UpdatableInset::draw(bv, font, baseline, x, cleared);
248 #else
249         if (!owner())
250                 x += (float)scroll();
251 #endif
252         if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
253                          (top_x != int(x)) || (top_baseline != baseline))) {
254                 int h = ascent(bv, font) + descent(bv, font);
255                 int const tx = display() || !owner() ? 0 : top_x;
256                 int w =  tx ? width(bv, font) : pain.paperWidth();
257                 int ty = baseline - ascent(bv, font);
258         
259                 if (ty < 0)
260                         ty = 0;
261                 if ((ty + h) > pain.paperHeight())
262                         h = pain.paperHeight();
263                 if ((top_x + w) > pain.paperWidth())
264                         w = pain.paperWidth();
265                 pain.fillRectangle(tx, ty, w, h);
266                 need_update = FULL;
267                 cleared = true;
268         }
269         top_x = int(x);
270         top_baseline = baseline;
271         x += ADD_TO_TABULAR_WIDTH;
272         if (cleared) {
273                 int cell = 0;
274                 float cx;
275                 first_visible_cell = -1;
276                 for (i = 0; i < tabular->rows(); ++i) {
277                         nx = int(x);
278                         cell = tabular->GetCellNumber(i, 0);
279                         if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
280                                 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
281                         {
282                                 baseline += tabular->GetDescentOfRow(i) +
283                                         tabular->GetAscentOfRow(i + 1) +
284                                         tabular->GetAdditionalHeight(i + 1);
285                                 continue;
286                         }
287                         for (j = 0; j < tabular->columns(); ++j) {
288                                 if (nx > bv->workWidth())
289                                         break;
290                                 if (tabular->IsPartOfMultiColumn(i, j))
291                                         continue;
292                                 cx = nx + tabular->GetBeginningOfTextInCell(cell);
293                                 if (first_visible_cell < 0)
294                                         first_visible_cell = cell;
295                                 if (hasSelection())
296                                         DrawCellSelection(pain, nx, baseline, i, j, cell);
297                                 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
298                                                                                                   cleared);
299                                 DrawCellLines(pain, nx, baseline, i, cell);
300                                 nx += tabular->GetWidthOfColumn(cell);
301                                 ++cell;
302                         }
303                         baseline += tabular->GetDescentOfRow(i) +
304                                 tabular->GetAscentOfRow(i + 1) +
305                                 tabular->GetAdditionalHeight(i + 1);
306                 }
307         } else if (need_update == CELL) {
308                 int cell = 0;
309                 nx = int(x);
310                 if (the_locking_inset &&
311                         tabular->GetCellInset(actcell) != the_locking_inset)
312                 {
313                         Inset * inset = tabular->GetCellInset(cell);
314                         for (i = 0;
315                              inset != the_locking_inset && i < tabular->rows();
316                              ++i) {
317                                 for (j = 0;
318                                      inset != the_locking_inset
319                                              && j < tabular->columns();
320                                      ++j) {
321                                         if (tabular->IsPartOfMultiColumn(i, j))
322                                                 continue;
323                                         nx += tabular->GetWidthOfColumn(cell);
324                                         ++cell;
325                                         inset = tabular->GetCellInset(cell);
326                                 }
327                                 if (tabular->row_of_cell(cell) > i) {
328                                         nx = int(x);
329                                         baseline += tabular->GetDescentOfRow(i) +
330                                                 tabular->GetAscentOfRow(i + 1) +
331                                                 tabular->GetAdditionalHeight(i + 1);
332                                 }
333                         }
334                 } else {
335                         // copute baseline for actual row
336                         for (i = 0; i < actrow; ++i) {
337                                 baseline += tabular->GetDescentOfRow(i) +
338                                         tabular->GetAscentOfRow(i + 1) +
339                                         tabular->GetAdditionalHeight(i + 1);
340                         }
341                         // now compute the right x position
342                         cell = tabular->GetCellNumber(actrow, 0);
343                         for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
344                                         if (tabular->IsPartOfMultiColumn(actrow, j))
345                                                 continue;
346                                         nx += tabular->GetWidthOfColumn(cell);
347                                         ++cell;
348                         }
349                 }
350                 i = tabular->row_of_cell(cell);
351                 if (the_locking_inset != tabular->GetCellInset(cell)) {
352                         lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
353                         return;
354                 }
355                 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
356                 float cx = dx;
357                 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
358                 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
359                 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
360                         return;
361                 // clear only if we didn't have a change
362                 if (need_update == CELL) {
363                         // clear before the inset
364                         pain.fillRectangle(
365                                 nx + 1,
366                                 baseline - tabular->GetAscentOfRow(i) + 1,
367                                 int(cx - nx - 1),
368                                 tabular->GetAscentOfRow(i) +
369                                 tabular->GetDescentOfRow(i) - 1);
370                         // clear behind the inset
371                         pain.fillRectangle(
372                                 int(cx + the_locking_inset->width(bv,font) + 1),
373                                 baseline - tabular->GetAscentOfRow(i) + 1,
374                                 tabular->GetWidthOfColumn(cell) -
375                                 tabular->GetBeginningOfTextInCell(cell) -
376                                 the_locking_inset->width(bv,font) -
377                                 tabular->GetAdditionalWidth(cell) - 1,
378                                 tabular->GetAscentOfRow(i) +
379                                 tabular->GetDescentOfRow(i) - 1);
380                 }
381         }
382         x -= ADD_TO_TABULAR_WIDTH;
383         x += width(bv, font);
384         if (bv->text->status == LyXText::CHANGED_IN_DRAW)
385                 need_update = INIT;
386         else
387                 need_update = NONE;
388 }
389
390
391 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
392                                  int row, int cell) const
393 {
394         int x2 = x + tabular->GetWidthOfColumn(cell);
395         bool on_off;
396         
397         if (!tabular->TopAlreadyDrawed(cell)) {
398                 on_off = !tabular->TopLine(cell);
399                 pain.line(x, baseline - tabular->GetAscentOfRow(row),
400                           x2, baseline -  tabular->GetAscentOfRow(row),
401                           on_off ? LColor::tabularonoffline : LColor::tabularline,
402                           on_off ? Painter::line_onoffdash : Painter::line_solid);
403         }
404         on_off = !tabular->BottomLine(cell);
405         pain.line(x,baseline +  tabular->GetDescentOfRow(row),
406                   x2, baseline +  tabular->GetDescentOfRow(row),
407                   on_off ? LColor::tabularonoffline : LColor::tabularline,
408                   on_off ? Painter::line_onoffdash : Painter::line_solid);
409         if (!tabular->LeftAlreadyDrawed(cell)) {
410                 on_off = !tabular->LeftLine(cell);
411                 pain.line(x, baseline -  tabular->GetAscentOfRow(row),
412                           x, baseline +  tabular->GetDescentOfRow(row),
413                           on_off ? LColor::tabularonoffline : LColor::tabularline,
414                           on_off ? Painter::line_onoffdash : Painter::line_solid);
415         }
416         on_off = !tabular->RightLine(cell);
417         pain.line(x2 - tabular->GetAdditionalWidth(cell),
418                   baseline -  tabular->GetAscentOfRow(row),
419                   x2 - tabular->GetAdditionalWidth(cell),
420                   baseline +  tabular->GetDescentOfRow(row),
421                   on_off ? LColor::tabularonoffline : LColor::tabularline,
422                   on_off ? Painter::line_onoffdash : Painter::line_solid);
423 }
424
425
426 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
427                                      int row, int column, int cell) const
428 {
429         int cs = tabular->column_of_cell(sel_cell_start);
430         int ce = tabular->column_of_cell(sel_cell_end);
431         if (cs > ce) {
432                 ce = cs;
433                 cs = tabular->column_of_cell(sel_cell_end);
434         } else {
435                 ce = tabular->right_column_of_cell(sel_cell_end);
436         }
437         
438         int rs = tabular->row_of_cell(sel_cell_start);
439         int re = tabular->row_of_cell(sel_cell_end);
440         if (rs > re) swap(rs, re);
441         
442         if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
443                 int w = tabular->GetWidthOfColumn(cell);
444                 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
445                 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
446                                    w, h, LColor::selection);
447         }
448 }
449
450
451 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
452 {
453         if (reinit) {
454                 need_update = INIT;
455                 calculate_dimensions_of_cells(bv, font, true);
456                 if (owner())
457                         owner()->update(bv, font, true);
458                 return;
459         }
460         if (the_locking_inset)
461                 the_locking_inset->update(bv, font, reinit);
462
463         switch (need_update) {
464         case INIT:
465         case FULL:
466         case CELL:
467                 if (calculate_dimensions_of_cells(bv, font, false))
468                         need_update = INIT;
469                 break;
470         case SELECTION:
471                 need_update = INIT;
472                 break;
473         default:
474                 break;
475         }
476 }
477
478
479 string const InsetTabular::EditMessage() const
480 {
481         return _("Opened Tabular Inset");
482 }
483
484
485 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
486 {
487         UpdatableInset::Edit(bv, x, y, button);
488         
489         if (!bv->lockInset(this)) {
490                 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
491                 return;
492         }
493         locked = true;
494         the_locking_inset = 0;
495         inset_x = 0;
496         inset_y = 0;
497         setPos(bv, x, y);
498         sel_cell_start = sel_cell_end = actcell;
499         bv->text->FinishUndo();
500         if (InsetHit(bv, x, y) && (button != 3)) {
501                 ActivateCellInsetAbs(bv, x, y, button);
502         }
503 //    UpdateLocal(bv, NONE, false);
504 //    bv->getOwner()->getPopups().updateFormTabular();
505 }
506
507
508 void InsetTabular::InsetUnlock(BufferView * bv)
509 {
510         if (the_locking_inset) {
511                 the_locking_inset->InsetUnlock(bv);
512                 the_locking_inset = 0;
513         }
514         HideInsetCursor(bv);
515         no_selection = false;
516         oldcell = -1;
517         locked = false;
518         if (scroll(false) || hasSelection()) {
519                 sel_cell_start = sel_cell_end = 0;
520                 if (scroll(false)) {
521                         scroll(bv, 0.0F);
522                 }
523                 UpdateLocal(bv, FULL, false);
524         }
525 }
526
527
528 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
529                                bool mark_dirty) const
530 {
531         if (need_update < what) // only set this if it has greater update
532                 need_update = what;
533         if ((what == INIT) && hasSelection())
534                 clearSelection();
535         // Dirty Cast! (Lgb)
536         if (need_update != NONE) {
537                 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
538                 if (locked) // && (what != NONE))
539                         resetPos(bv);
540         }
541 }
542
543
544 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
545 {
546         lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
547                               << inset << "): ";
548         if (!inset)
549                 return false;
550         oldcell = -1;
551         if (inset == tabular->GetCellInset(actcell)) {
552                 lyxerr[Debug::INSETS] << "OK" << endl;
553                 the_locking_inset = tabular->GetCellInset(actcell);
554                 resetPos(bv);
555                 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
556                 inset_y = cursor.y();
557                 return true;
558         } else if (the_locking_inset && (the_locking_inset == inset)) {
559                 lyxerr[Debug::INSETS] << "OK" << endl;
560                 resetPos(bv);
561                 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
562                 inset_y = cursor.y();
563         } else if (the_locking_inset) {
564                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
565                 return the_locking_inset->LockInsetInInset(bv, inset);
566         }
567         lyxerr[Debug::INSETS] << "NOT OK" << endl;
568         return false;
569 }
570
571
572 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
573                                       bool lr)
574 {
575         if (!the_locking_inset)
576                 return false;
577         if (the_locking_inset == inset) {
578                 the_locking_inset->InsetUnlock(bv);
579                 the_locking_inset = 0;
580                 if (scroll(false))
581                         scroll(bv, 0.0F);
582                 else
583                         UpdateLocal(bv, CELL, false);
584                 ShowInsetCursor(bv, false);
585                 return true;
586         }
587         if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
588                 if (inset->LyxCode() == TABULAR_CODE &&
589                     !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
590                         bv->owner()->getDialogs()->updateTabular(this);
591                         oldcell = actcell;
592                 }
593                 return true;
594         }
595         return false;
596 }
597
598
599 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
600 {
601         if (!the_locking_inset)
602                 return false;
603         if (the_locking_inset != inset) {
604                 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
605                         return false;
606         }
607         UpdateLocal(bv, CELL, false);
608         return true;
609 }
610
611
612 unsigned int InsetTabular::InsetInInsetY()
613 {
614         if (!the_locking_inset)
615                 return 0;
616         
617         return inset_y + the_locking_inset->InsetInInsetY();
618 }
619
620
621 UpdatableInset * InsetTabular::GetLockingInset()
622 {
623         return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
624 }
625
626
627 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
628 {
629         if (c == LyxCode())
630                 return this;
631         if (the_locking_inset)
632                 return the_locking_inset->GetFirstLockingInsetOfType(c);
633         return 0;
634 }
635
636
637 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
638 {
639         if (the_locking_inset)
640                 return the_locking_inset->InsertInset(bv, inset);
641         return false;
642 }
643
644
645 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
646 {
647         if (hasSelection() && (button == 3))
648                 return;
649
650         if (hasSelection()) {
651                 clearSelection();
652                 UpdateLocal(bv, SELECTION, false);
653         }
654
655         no_selection = false;
656
657         int const ocell = actcell;
658         int const orow = actrow;
659
660         HideInsetCursor(bv);
661         setPos(bv, x, y);
662         if (actrow != orow)
663                 UpdateLocal(bv, NONE, false);
664         sel_cell_start = sel_cell_end = actcell;
665         if (button == 3) {
666                 if ((ocell != actcell) && the_locking_inset) {
667                         the_locking_inset->InsetUnlock(bv);
668                         the_locking_inset = 0;
669                 }
670                 ShowInsetCursor(bv);
671                 return;
672         }
673
674         bool const inset_hit = InsetHit(bv, x, y);
675
676         if ((ocell == actcell) && the_locking_inset && inset_hit) {
677                 resetPos(bv);
678                 the_locking_inset->InsetButtonPress(bv,
679                                                     x - inset_x, y - inset_y,
680                                                     button);
681                 return;
682         } else if (the_locking_inset) {
683                 the_locking_inset->InsetUnlock(bv);
684         }
685         the_locking_inset = 0;
686         if (button == 2) {
687                 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
688                 return;
689         }
690         if (inset_hit && bv->theLockingInset()) {
691                 if (ActivateCellInsetAbs(bv, x, y, button))
692                         the_locking_inset->InsetButtonPress(bv,
693                                                             x - inset_x,
694                                                             y - inset_y,
695                                                             button);
696                 return;
697         }
698         ShowInsetCursor(bv);
699 }
700
701
702 void InsetTabular::InsetButtonRelease(BufferView * bv,
703                                       int x, int y, int button)
704 {
705         if (button == 3) {
706                 if (the_locking_inset) {
707                         UpdatableInset * i;
708                         if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
709                                 i->InsetButtonRelease(bv, x, y, button);
710                                 return;
711                         }
712                 }
713                 bv->owner()->getDialogs()->showTabular(this);
714                 return;
715         }
716         if (the_locking_inset) {
717                 the_locking_inset->InsetButtonRelease(bv,
718                                                       x - inset_x, y - inset_y,
719                                                       button);
720                 return;
721         }
722         no_selection = false;
723 }
724
725
726 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
727 {
728         if (the_locking_inset) {
729                 the_locking_inset->InsetMotionNotify(bv,
730                                                      x - inset_x,
731                                                      y - inset_y,
732                                                      button);
733                 return;
734         }
735         if (!no_selection) {
736                 HideInsetCursor(bv);
737                 int const old_cell = actcell;
738                 
739                 setPos(bv, x, y);
740                 sel_cell_end = actcell;
741                 if (sel_cell_end != old_cell)
742                         UpdateLocal(bv, SELECTION, false);
743                 ShowInsetCursor(bv);
744         }
745         no_selection = false;
746 }
747
748
749 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
750 {
751         if (the_locking_inset) {
752                 the_locking_inset->InsetKeyPress(xke);
753                 return;
754         }
755 }
756
757
758 UpdatableInset::RESULT
759 InsetTabular::LocalDispatch(BufferView * bv,
760                             kb_action action,
761                             string const & arg)
762 {
763         // We need to save the value of the_locking_inset as the call to 
764         // the_locking_inset->LocalDispatch might unlock it.
765         old_locking_inset = the_locking_inset;
766         no_selection = false;
767         UpdatableInset::RESULT result =
768                 UpdatableInset::LocalDispatch(bv, action, arg);
769         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
770                 resetPos(bv);
771                 return result;
772         }
773
774         if ((action < 0) && arg.empty())
775                 return FINISHED;
776
777         bool hs = hasSelection();
778
779         result=DISPATCHED;
780         // this one have priority over the locked InsetText!
781         switch (action) {
782         case LFUN_SHIFT_TAB:
783         case LFUN_TAB:
784         {
785                 HideInsetCursor(bv);
786                 if (the_locking_inset) {
787                         UnlockInsetInInset(bv, the_locking_inset);
788                         the_locking_inset = 0;
789                 }
790                 if (action == LFUN_TAB)
791                         moveNextCell(bv, old_locking_inset != 0);
792                 else
793                         movePrevCell(bv, old_locking_inset != 0);
794                 sel_cell_start = sel_cell_end = actcell;
795                 if (hs)
796                         UpdateLocal(bv, SELECTION, false);
797                 if (!the_locking_inset) {
798                         ShowInsetCursor(bv);
799                         return DISPATCHED_NOUPDATE;
800                 }
801                 return result;
802         }
803         // this to avoid compiler warnings.
804         default:
805                 break;
806         }
807
808         if (the_locking_inset) {
809                 result=the_locking_inset->LocalDispatch(bv, action, arg);
810                 if (result == DISPATCHED_NOUPDATE) {
811                         int sc = scroll();
812                         resetPos(bv);
813                         if (sc != scroll()) { // inset has been scrolled
814                                 the_locking_inset->ToggleInsetCursor(bv);
815                                 UpdateLocal(bv, FULL, false);
816                                 the_locking_inset->ToggleInsetCursor(bv);
817                         }
818                         return result;
819                 } else if (result == DISPATCHED) {
820                         the_locking_inset->ToggleInsetCursor(bv);
821                         UpdateLocal(bv, CELL, false);
822                         the_locking_inset->ToggleInsetCursor(bv);
823                         return result;
824                 } else if (result == FINISHED) {
825                 }
826         }
827
828         HideInsetCursor(bv);
829         result=DISPATCHED;
830         switch (action) {
831                 // --- Cursor Movements ----------------------------------
832         case LFUN_RIGHTSEL:
833                 if (tabular->IsLastCellInRow(actcell))
834                         break;
835                 moveRight(bv, false);
836                 sel_cell_end = actcell;
837                 UpdateLocal(bv, SELECTION, false);
838                 break;
839         case LFUN_RIGHT:
840                 result = moveRight(bv);
841                 sel_cell_start = sel_cell_end = actcell;
842                 if (hs)
843                         UpdateLocal(bv, SELECTION, false);
844                 break;
845         case LFUN_LEFTSEL:
846                 if (tabular->IsFirstCellInRow(actcell))
847                         break;
848                 moveLeft(bv, false);
849                 sel_cell_end = actcell;
850                 UpdateLocal(bv, SELECTION, false);
851                 break;
852         case LFUN_LEFT:
853                 result = moveLeft(bv);
854                 sel_cell_start = sel_cell_end = actcell;
855                 if (hs)
856                         UpdateLocal(bv, SELECTION, false);
857                 break;
858         case LFUN_DOWNSEL:
859         {
860                 int const ocell = actcell;
861                 moveDown(bv, false);
862                 if ((ocell == sel_cell_end) ||
863                     (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
864                         sel_cell_end = tabular->GetCellBelow(sel_cell_end);
865                 else
866                         sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
867                 UpdateLocal(bv, SELECTION, false);
868         }
869         break;
870         case LFUN_DOWN:
871                 result = moveDown(bv, old_locking_inset != 0);
872                 sel_cell_start = sel_cell_end = actcell;
873                 if (hs)
874                         UpdateLocal(bv, SELECTION, false);
875                 break;
876         case LFUN_UPSEL:
877         {
878                 int const ocell = actcell;
879                 moveUp(bv, false);
880                 if ((ocell == sel_cell_end) ||
881                     (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
882                         sel_cell_end = tabular->GetCellAbove(sel_cell_end);
883                 else
884                         sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
885                 UpdateLocal(bv, SELECTION, false);
886         }
887         break;
888         case LFUN_UP:
889                 result = moveUp(bv, old_locking_inset != 0);
890                 sel_cell_start = sel_cell_end = actcell;
891                 if (hs)
892                         UpdateLocal(bv, SELECTION, false);
893                 break;
894         case LFUN_NEXT: {
895                 int column = actcol;
896                 if (the_locking_inset) {
897                         UnlockInsetInInset(bv, the_locking_inset);
898                         the_locking_inset = 0;
899                 }
900                 if (bv->text->first + bv->painter().paperHeight() <
901                     (top_baseline + tabular->GetHeightOfTabular()))
902                         {
903                                 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
904                                 UpdateLocal(bv, FULL, false);
905                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
906                         } else {
907                                 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
908                         }
909                 resetPos(bv);
910                 UpdateLocal(bv, CURSOR, false);
911                 break;
912         }
913         case LFUN_PRIOR: {
914                 int column = actcol;
915                 if (the_locking_inset) {
916                         UnlockInsetInInset(bv, the_locking_inset);
917                         the_locking_inset = 0;
918                 }
919                 if (top_baseline < 0) {
920                         bv->scrollCB(bv->text->first - bv->painter().paperHeight());
921                         UpdateLocal(bv, FULL, false);
922                         if (top_baseline > 0)
923                                 actcell = column;
924                         else
925                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
926                 } else {
927                         actcell = column;
928                 }
929                 resetPos(bv);
930                 UpdateLocal(bv, CURSOR, false);
931                 break;
932         }
933         case LFUN_BACKSPACE:
934                 break;
935         case LFUN_DELETE:
936                 break;
937         case LFUN_HOME:
938                 break;
939         case LFUN_END:
940                 break;
941         case LFUN_LAYOUT_TABULAR:
942         {
943                 bv->owner()->getDialogs()->showTabular(this);
944         }
945         break;
946         case LFUN_TABULAR_FEATURE:
947                 if (!TabularFeatures(bv, arg))
948                         result = UNDISPATCHED;
949                 break;
950         case LFUN_CUT:
951                 if (!copySelection(bv))
952                         break;
953                 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
954 #ifndef NEW_INSETS
955                                   bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
956                                   bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
957 #else
958                                   bv->text->cursor.par()->previous(),
959                                   bv->text->cursor.par()->next()
960 #endif
961                         );
962                 cutSelection();
963                 UpdateLocal(bv, INIT, true);
964                 break;
965         case LFUN_COPY:
966                 if (!hasSelection())
967                         break;
968                 bv->text->FinishUndo();
969                 copySelection(bv);
970                 break;
971         case LFUN_PASTESELECTION:
972         {
973                 string clip(bv->workarea()->getClipboard());
974         
975                 if (clip.empty())
976                         break;
977                 if (clip.find('\t') != string::npos) {
978                         int cols = 1;
979                         int rows = 1;
980                         int maxCols = 1;
981                         unsigned int len = clip.length();
982                         string::size_type p = 0;
983
984                         while(p < len &&
985                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
986                                 switch(clip[p]) {
987                                 case '\t':
988                                         ++cols;
989                                         break;
990                                 case '\n':
991                                         if ((p+1) < len)
992                                                 ++rows;
993                                         maxCols = max(cols, maxCols);
994                                         cols = 1;
995                                         break;
996                                 }
997                                 ++p;
998                         }
999                         maxCols = max(cols, maxCols);
1000                         delete paste_tabular;
1001                         paste_tabular = new LyXTabular(this, rows, maxCols);
1002                         string::size_type op = 0;
1003                         int cell = 0;
1004                         int cells = paste_tabular->GetNumberOfCells();
1005                         p = cols = 0;
1006                         while((cell < cells) && (p < len) &&
1007                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1008                                 if (p >= len)
1009                                         break;
1010                                 switch(clip[p]) {
1011                                 case '\t':
1012                                         paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1013                                         ++cols;
1014                                         ++cell;
1015                                         break;
1016                                 case '\n':
1017                                         paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1018                                         while(cols++ < maxCols)
1019                                                 ++cell;
1020                                         cols = 0;
1021                                         break;
1022                                 }
1023                                 ++p;
1024                                 op = p;
1025                         }
1026                         // check for the last cell if there is no trailing '\n'
1027                         if ((cell < cells) && (op < len))
1028                                 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1029                 } else {
1030                         // so that the clipboard is used and it goes on
1031                         // to default
1032                         // and executes LFUN_PASTESELECTION in insettext!
1033                         delete paste_tabular;
1034                         paste_tabular = 0;
1035                 }
1036         }
1037         case LFUN_PASTE:
1038                 if (hasPasteBuffer()) {
1039                         bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1040 #ifndef NEW_INSETS
1041                                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1042                                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1043 #else
1044                                           bv->text->cursor.par()->previous(),
1045                                           bv->text->cursor.par()->next()
1046 #endif
1047                                 );
1048                         pasteSelection(bv);
1049                         UpdateLocal(bv, INIT, true);
1050                         break;
1051                 }
1052                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1053         default:
1054                 // we try to activate the actual inset and put this event down to
1055                 // the insets dispatch function.
1056                 result = UNDISPATCHED;
1057                 if (the_locking_inset)
1058                         break;
1059                 nodraw(true);
1060                 if (ActivateCellInset(bv)) {
1061                         result = the_locking_inset->LocalDispatch(bv, action, arg);
1062                         if ((result == UNDISPATCHED) || (result == FINISHED)) {
1063                                 UnlockInsetInInset(bv, the_locking_inset);
1064                                 nodraw(false);
1065                                 the_locking_inset = 0;
1066                                 return UNDISPATCHED;
1067                         }
1068                         nodraw(false);
1069 //                      the_locking_inset->ToggleInsetCursor(bv);
1070                         UpdateLocal(bv, CELL, false);
1071 //                      the_locking_inset->ToggleInsetCursor(bv);
1072                         return result;
1073                 }
1074                 break;
1075         }
1076         if (result!=FINISHED) {
1077                 if (!the_locking_inset) {
1078                         ShowInsetCursor(bv);
1079                 }
1080         } else
1081                 bv->unlockInset(this);
1082         return result;
1083 }
1084
1085
1086 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1087                         bool fragile, bool fp) const
1088 {
1089         return tabular->Latex(buf, os, fragile, fp);
1090 }
1091
1092
1093 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1094 {
1095         // This should be changed to a real ascii export
1096         return tabular->Ascii(buf, os);
1097 }
1098
1099
1100 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1101 {
1102         return 0;
1103 }
1104
1105
1106 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1107 {
1108         return tabular->DocBook(buf,os);
1109 }
1110
1111
1112 void InsetTabular::Validate(LaTeXFeatures & features) const
1113 {
1114         tabular->Validate(features);
1115 }
1116
1117
1118 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1119                                                  LyXFont const & font,
1120                                                  bool reinit) const
1121 {
1122         int cell = -1;
1123         int maxAsc = 0;
1124         int maxDesc = 0;
1125         InsetText * inset;
1126         bool changed = false;
1127         
1128         // if we have a locking_inset we should have to check only this cell for
1129         // change so I'll try this to have a boost, but who knows ;)
1130         if ((need_update != INIT) &&
1131             (the_locking_inset == tabular->GetCellInset(actcell))) {
1132                 for(int i = 0; i < tabular->columns(); ++i) {
1133                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1134                                      maxAsc);
1135                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1136                                       maxDesc);
1137                 }
1138                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1139                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1140                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1141                 return changed;
1142         }
1143         for (int i = 0; i < tabular->rows(); ++i) {
1144                 maxAsc = 0;
1145                 maxDesc = 0;
1146                 for (int j= 0; j < tabular->columns(); ++j) {
1147                         if (tabular->IsPartOfMultiColumn(i,j))
1148                                 continue;
1149                         ++cell;
1150                         inset = tabular->GetCellInset(cell);
1151                         if (!reinit)
1152                                 inset->update(bv, font, false);
1153                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1154                         maxDesc = max(maxDesc, inset->descent(bv, font));
1155                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1156                 }
1157                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1158                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1159         }
1160         return changed;
1161 }
1162
1163
1164 void InsetTabular::GetCursorPos(BufferView *,
1165                                 int & x, int & y) const
1166 {
1167         x = cursor.x() - top_x;
1168         y = cursor.y();
1169 }
1170
1171
1172 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1173 {
1174         if (the_locking_inset) {
1175                 the_locking_inset->ToggleInsetCursor(bv);
1176                 return;
1177         }
1178         
1179         LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1180         
1181         int const asc = lyxfont::maxAscent(font);
1182         int const desc = lyxfont::maxDescent(font);
1183         
1184         if (isCursorVisible())
1185                 bv->hideLockedInsetCursor();
1186         else
1187                 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1188         toggleCursorVisible();
1189 }
1190
1191
1192 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1193 {
1194         if (!isCursorVisible()) {
1195                 LyXFont font; // = GetFont(par, cursor.pos);
1196         
1197                 int const asc = lyxfont::maxAscent(font);
1198                 int const desc = lyxfont::maxDescent(font);
1199                 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1200                 if (show)
1201                         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1202                 setCursorVisible(true);
1203         }
1204 }
1205
1206
1207 void InsetTabular::HideInsetCursor(BufferView * bv)
1208 {
1209         if (isCursorVisible()) {
1210                 bv->hideLockedInsetCursor();
1211                 setCursorVisible(false);
1212         }
1213 //    if (cursor_visible)
1214 //        ToggleInsetCursor(bv);
1215 }
1216
1217
1218 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1219 {
1220         cursor.y(0);
1221         
1222         actcell = actrow = actcol = 0;
1223         int ly = tabular->GetDescentOfRow(actrow);
1224
1225         // first search the right row
1226         while((ly < y) && (actrow < tabular->rows())) {
1227                 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1228                                  tabular->GetAscentOfRow(actrow + 1) +
1229                                  tabular->GetAdditionalHeight(actrow + 1));
1230                 ++actrow;
1231                 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1232         }
1233         actcell = tabular->GetCellNumber(actrow, actcol);
1234
1235         // now search the right column
1236         int lx = tabular->GetWidthOfColumn(actcell) -
1237                 tabular->GetAdditionalWidth(actcell);
1238 #if 0
1239 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1240         for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1241              ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1242                      tabular->GetAdditionalWidth(actcell - 1));
1243 #else
1244         // Jürgen, you should check that this is correct. (Lgb)
1245         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1246                 lx += tabular->GetWidthOfColumn(actcell + 1)
1247                         + tabular->GetAdditionalWidth(actcell);
1248         }
1249         
1250 #endif
1251         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1252         resetPos(bv);
1253 }
1254
1255
1256 int InsetTabular::getCellXPos(int cell) const
1257 {
1258         int c = cell;
1259         
1260         for (; !tabular->IsFirstCellInRow(c); --c)
1261                 ;
1262         int lx = tabular->GetWidthOfColumn(cell);
1263         for (; c < cell; ++c) {
1264                 lx += tabular->GetWidthOfColumn(c);
1265         }
1266         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1267 }
1268
1269
1270 void InsetTabular::resetPos(BufferView * bv) const
1271 {
1272         if (!locked)
1273                 return;
1274         actcol = tabular->column_of_cell(actcell);
1275
1276         int cell = 0;
1277         actrow = 0;
1278         cursor.y(0);
1279         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1280                 if (tabular->IsLastCellInRow(cell)) {
1281                         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1282                                          tabular->GetAscentOfRow(actrow + 1) +
1283                                          tabular->GetAdditionalHeight(actrow + 1));
1284                         ++actrow;
1285                 }
1286         }
1287         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1288         int new_x = getCellXPos(actcell);
1289         int old_x = cursor.x();
1290         new_x += offset;
1291         cursor.x(new_x);
1292 //    cursor.x(getCellXPos(actcell) + offset);
1293         if (scroll(false) && (tabular->GetWidthOfTabular() < bv->workWidth()-20)) {
1294                 scroll(bv, 0.0F);
1295                 UpdateLocal(bv, FULL, false);
1296         } else if (the_locking_inset &&
1297                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20)) {
1298                 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1299                 if (xx > (bv->workWidth()-20))
1300                         scroll(bv, -(xx - bv->workWidth() + 60));
1301                 else if (xx < 20) {
1302                         if (xx < 0)
1303                                 xx = -xx + 60;
1304                         else
1305                                 xx = 60;
1306                         scroll(bv, xx);
1307                 }
1308                 UpdateLocal(bv, FULL, false);
1309         } else if ((cursor.x() - offset) > 20 &&
1310                    (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1311                    > (bv->workWidth() - 20)) {
1312                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1313                 UpdateLocal(bv, FULL, false);
1314         } else if ((cursor.x() - offset) < 20) {
1315                 scroll(bv, 20 - cursor.x() + offset);
1316                 UpdateLocal(bv, FULL, false);
1317         } else if (scroll(false) && top_x > 20 &&
1318                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1319                 scroll(bv, old_x - cursor.x());
1320                 UpdateLocal(bv, FULL, false);
1321         }
1322         if ((!the_locking_inset ||
1323              !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1324             actcell != oldcell) {
1325                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1326                 bv->owner()->getDialogs()->updateTabular(inset);
1327                 oldcell = actcell;
1328         }
1329 }
1330
1331
1332 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1333 {
1334         if (lock && !old_locking_inset) {
1335                 if (ActivateCellInset(bv))
1336                         return DISPATCHED;
1337         } else {
1338                 bool moved = isRightToLeft(bv)
1339                         ? movePrevCell(bv) : moveNextCell(bv);
1340                 if (!moved)
1341                         return FINISHED;
1342                 if (lock && ActivateCellInset(bv))
1343                         return DISPATCHED;
1344         }
1345         resetPos(bv);
1346         return DISPATCHED_NOUPDATE;
1347 }
1348
1349
1350 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1351 {
1352         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1353         if (!moved)
1354                 return FINISHED;
1355         if (lock) {       // behind the inset
1356                 if (ActivateCellInset(bv, 0, 0, 0, true))
1357                         return DISPATCHED;
1358         }
1359         resetPos(bv);
1360         return DISPATCHED_NOUPDATE;
1361 }
1362
1363
1364 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1365 {
1366         int const ocell = actcell;
1367         actcell = tabular->GetCellAbove(actcell);
1368         if (actcell == ocell) // we moved out of the inset
1369                 return FINISHED;
1370         resetPos(bv);
1371         if (lock) {
1372                 int x = 0;
1373                 int y = 0;
1374                 if (old_locking_inset) {
1375                         old_locking_inset->GetCursorPos(bv, x, y);
1376                         x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1377                 }
1378                 if (ActivateCellInset(bv, x, 0))
1379                         return DISPATCHED;
1380         }
1381         return DISPATCHED_NOUPDATE;
1382 }
1383
1384
1385 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1386 {
1387         int const ocell = actcell;
1388         actcell = tabular->GetCellBelow(actcell);
1389         if (actcell == ocell) // we moved out of the inset
1390                 return FINISHED;
1391         resetPos(bv);
1392         if (lock) {
1393                 int x = 0;
1394                 int y = 0;
1395                 if (old_locking_inset) {
1396                         old_locking_inset->GetCursorPos(bv, x, y);
1397                         x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1398                 }
1399                 if (ActivateCellInset(bv, x, 0))
1400                         return DISPATCHED;
1401         }
1402         return DISPATCHED_NOUPDATE;
1403 }
1404
1405
1406 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1407 {
1408         if (isRightToLeft(bv)) {
1409                 if (tabular->IsFirstCellInRow(actcell)) {
1410                         int row = tabular->row_of_cell(actcell);
1411                         if (row == tabular->rows() - 1)
1412                                 return false;
1413                         actcell = tabular->GetLastCellInRow(row);
1414                         actcell = tabular->GetCellBelow(actcell);
1415                 } else {
1416                         if (!actcell)
1417                                 return false;
1418                         --actcell;
1419                 }
1420         } else {
1421                 if (tabular->IsLastCell(actcell))
1422                         return false;
1423                 ++actcell;
1424         }
1425         if (lock) {
1426                 bool rtl = tabular->GetCellInset(actcell)->par->
1427                         isRightToLeftPar(bv->buffer()->params);
1428                 ActivateCellInset(bv, 0, 0, 0, !rtl);
1429         }
1430         resetPos(bv);
1431         return true;
1432 }
1433
1434
1435 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1436 {
1437         if (isRightToLeft(bv)) {
1438                 if (tabular->IsLastCellInRow(actcell)) {
1439                         int row = tabular->row_of_cell(actcell);
1440                         if (row == 0)
1441                                 return false;
1442                         actcell = tabular->GetFirstCellInRow(row);
1443                         actcell = tabular->GetCellAbove(actcell);
1444                 } else {
1445                         if (tabular->IsLastCell(actcell))
1446                                 return false;
1447                         ++actcell;
1448                 }
1449         } else {
1450                 if (!actcell) // first cell
1451                         return false;
1452                 --actcell;
1453         }
1454         if (lock) {
1455                 bool rtl = tabular->GetCellInset(actcell)->par->
1456                         isRightToLeftPar(bv->buffer()->params);
1457                 ActivateCellInset(bv, 0, 0, 0, !rtl);
1458         }
1459         resetPos(bv);
1460         return true;
1461 }
1462
1463
1464 bool InsetTabular::Delete()
1465 {
1466         return true;
1467 }
1468
1469
1470 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1471 {
1472         if (the_locking_inset)
1473                 the_locking_inset->SetFont(bv, font, tall);
1474 }
1475
1476
1477 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1478 {
1479         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1480         
1481         int i = 0;
1482         for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1483                 string const tmp = tabularFeatures[i].feature;
1484                 
1485                 if (tmp == what.substr(0, tmp.length())) {
1486                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1487                         //tabularFeatures[i].feature.length())) {
1488                         action = tabularFeatures[i].action;
1489                         break;
1490                 }
1491         }
1492         if (action == LyXTabular::LAST_ACTION)
1493                 return false;
1494
1495         string const val =
1496                 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1497         TabularFeatures(bv, action, val);
1498         return true;
1499 }
1500
1501
1502 void InsetTabular::TabularFeatures(BufferView * bv,
1503                                    LyXTabular::Feature feature,
1504                                    string const & value)
1505 {
1506         //int i;
1507         //int j;
1508         int sel_col_start;
1509         int sel_col_end;
1510         int sel_row_start;
1511         int sel_row_end;
1512         bool setLines = false;
1513         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1514         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1515         //int lineSet;
1516         //bool what;
1517
1518         switch (feature) {
1519         case LyXTabular::M_ALIGN_LEFT:
1520         case LyXTabular::ALIGN_LEFT:
1521                 setAlign=LYX_ALIGN_LEFT;
1522                 break;
1523         case LyXTabular::M_ALIGN_RIGHT:
1524         case LyXTabular::ALIGN_RIGHT:
1525                 setAlign=LYX_ALIGN_RIGHT;
1526                 break;
1527         case LyXTabular::M_ALIGN_CENTER:
1528         case LyXTabular::ALIGN_CENTER:
1529                 setAlign=LYX_ALIGN_CENTER;
1530                 break;
1531         case LyXTabular::M_VALIGN_TOP:
1532         case LyXTabular::VALIGN_TOP:
1533                 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1534                 break;
1535         case LyXTabular::M_VALIGN_BOTTOM:
1536         case LyXTabular::VALIGN_BOTTOM:
1537                 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1538                 break;
1539         case LyXTabular::M_VALIGN_CENTER:
1540         case LyXTabular::VALIGN_CENTER:
1541                 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1542                 break;
1543         default:
1544                 break;
1545         }
1546         if (hasSelection()) {
1547                 sel_col_start = tabular->column_of_cell(sel_cell_start);
1548                 sel_col_end = tabular->column_of_cell(sel_cell_end);
1549                 if (sel_col_start > sel_col_end) {
1550                         sel_col_end = sel_col_start;
1551                         sel_col_start = tabular->column_of_cell(sel_cell_end);
1552                 } else {
1553                         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1554                 }
1555         
1556                 sel_row_start = tabular->row_of_cell(sel_cell_start);
1557                 sel_row_end = tabular->row_of_cell(sel_cell_end);
1558                 if (sel_row_start > sel_row_end) {
1559                         //int tmp = sel_row_start;
1560                         //sel_row_start = sel_row_end;
1561                         //sel_row_end = tmp;
1562                         swap(sel_row_start, sel_row_end);
1563                 }
1564         } else {
1565                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1566                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1567         }
1568         bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1569 #ifndef NEW_INSETS
1570                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1571                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1572 #else
1573                           bv->text->cursor.par()->previous(),
1574                           bv->text->cursor.par()->next()
1575 #endif
1576                 );
1577
1578         int row = tabular->row_of_cell(actcell);
1579         int column = tabular->column_of_cell(actcell);
1580         bool flag = true;
1581         
1582         switch (feature) {
1583         case LyXTabular::SET_PWIDTH:
1584         {
1585                 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1586                 tabular->SetColumnPWidth(actcell,value);
1587                 if (update) {
1588                         for (int i = 0; i < tabular->rows(); ++i) {
1589                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1590                                         resizeLyXText(bv);
1591                         }
1592                         UpdateLocal(bv, INIT, true);
1593                 }
1594         }
1595         break;
1596         case LyXTabular::SET_MPWIDTH:
1597         {
1598                 bool const update = (tabular->GetPWidth(actcell) != value);
1599                 tabular->SetMColumnPWidth(actcell,value);
1600                 if (update) {
1601                         for (int i = 0; i < tabular->rows(); ++i) {
1602                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1603                                         resizeLyXText(bv);
1604                         }
1605                         UpdateLocal(bv, INIT, true);
1606                 }
1607         }
1608         break;
1609         case LyXTabular::SET_SPECIAL_COLUMN:
1610         case LyXTabular::SET_SPECIAL_MULTI:
1611                 tabular->SetAlignSpecial(actcell,value,feature);
1612                 break;
1613         case LyXTabular::APPEND_ROW:
1614                 // append the row into the tabular
1615                 UnlockInsetInInset(bv, the_locking_inset);
1616                 tabular->AppendRow(actcell);
1617                 UpdateLocal(bv, INIT, true);
1618                 break;
1619         case LyXTabular::APPEND_COLUMN:
1620                 // append the column into the tabular
1621                 UnlockInsetInInset(bv, the_locking_inset);
1622                 tabular->AppendColumn(actcell);
1623                 actcell = tabular->GetCellNumber(row, column);
1624                 UpdateLocal(bv, INIT, true);
1625                 break;
1626         case LyXTabular::DELETE_ROW:
1627                 UnlockInsetInInset(bv, the_locking_inset);
1628                 tabular->DeleteRow(tabular->row_of_cell(actcell));
1629                 if ((row+1) > tabular->rows())
1630                         --row;
1631                 actcell = tabular->GetCellNumber(row, column);
1632                 clearSelection();
1633                 UpdateLocal(bv, INIT, true);
1634                 break;
1635         case LyXTabular::DELETE_COLUMN:
1636                 UnlockInsetInInset(bv, the_locking_inset);
1637                 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1638                 if ((column+1) > tabular->columns())
1639                         --column;
1640                 actcell = tabular->GetCellNumber(row, column);
1641                 clearSelection();
1642                 UpdateLocal(bv, INIT, true);
1643                 break;
1644         case LyXTabular::M_TOGGLE_LINE_TOP:
1645                 flag = false;
1646         case LyXTabular::TOGGLE_LINE_TOP:
1647         {
1648                 bool lineSet = !tabular->TopLine(actcell, flag);
1649                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1650                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1651                                 tabular->SetTopLine(
1652                                         tabular->GetCellNumber(i, j),
1653                                         lineSet, flag);
1654                 UpdateLocal(bv, INIT, true);
1655                 break;
1656         }
1657         
1658         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1659                 flag = false;
1660         case LyXTabular::TOGGLE_LINE_BOTTOM:
1661         {
1662                 bool lineSet = !tabular->BottomLine(actcell, flag); 
1663                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1664                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1665                                 tabular->SetBottomLine(
1666                                         tabular->GetCellNumber(i, j),
1667                                         lineSet,
1668                                         flag);
1669                 UpdateLocal(bv, INIT, true);
1670                 break;
1671         }
1672         
1673         case LyXTabular::M_TOGGLE_LINE_LEFT:
1674                 flag = false;
1675         case LyXTabular::TOGGLE_LINE_LEFT:
1676         {
1677                 bool lineSet = !tabular->LeftLine(actcell, flag);
1678                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1679                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1680                                 tabular->SetLeftLine(
1681                                         tabular->GetCellNumber(i,j),
1682                                         lineSet,
1683                                         flag);
1684                 UpdateLocal(bv, INIT, true);
1685                 break;
1686         }
1687         
1688         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1689                 flag = false;
1690         case LyXTabular::TOGGLE_LINE_RIGHT:
1691         {
1692                 bool lineSet = !tabular->RightLine(actcell, flag);
1693                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1694                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1695                                 tabular->SetRightLine(
1696                                         tabular->GetCellNumber(i,j),
1697                                         lineSet,
1698                                         flag);
1699                 UpdateLocal(bv, INIT, true);
1700                 break;
1701         }
1702         
1703         case LyXTabular::M_ALIGN_LEFT:
1704         case LyXTabular::M_ALIGN_RIGHT:
1705         case LyXTabular::M_ALIGN_CENTER:
1706                 flag = false;
1707         case LyXTabular::ALIGN_LEFT:
1708         case LyXTabular::ALIGN_RIGHT:
1709         case LyXTabular::ALIGN_CENTER:
1710                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1711                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1712                                 tabular->SetAlignment(
1713                                         tabular->GetCellNumber(i, j),
1714                                         setAlign,
1715                                         flag);
1716                 UpdateLocal(bv, INIT, true);
1717                 break;
1718         case LyXTabular::M_VALIGN_TOP:
1719         case LyXTabular::M_VALIGN_BOTTOM:
1720         case LyXTabular::M_VALIGN_CENTER:
1721                 flag = false;
1722         case LyXTabular::VALIGN_TOP:
1723         case LyXTabular::VALIGN_BOTTOM:
1724         case LyXTabular::VALIGN_CENTER:
1725                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1726                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1727                                 tabular->SetVAlignment(
1728                                         tabular->GetCellNumber(i, j),
1729                                         setVAlign, flag);
1730                 UpdateLocal(bv, INIT, true);
1731                 break;
1732         case LyXTabular::MULTICOLUMN:
1733         {
1734                 if (sel_row_start != sel_row_end) {
1735                         WriteAlert(_("Impossible Operation!"), 
1736                                    _("Multicolumns can only be horizontally."), 
1737                                    _("Sorry."));
1738                         return;
1739                 }
1740                 // just multicol for one Single Cell
1741                 if (!hasSelection()) {
1742                         // check wether we are completly in a multicol
1743                         if (tabular->IsMultiColumn(actcell)) {
1744                                 tabular->UnsetMultiColumn(actcell);
1745                                 UpdateLocal(bv, INIT, true);
1746                         } else {
1747                                 tabular->SetMultiColumn(actcell, 1);
1748                                 UpdateLocal(bv, CELL, true);
1749                         }
1750                         return;
1751                 }
1752                 // we have a selection so this means we just add all this
1753                 // cells to form a multicolumn cell
1754                 int s_start;
1755                 int s_end;
1756
1757                 if (sel_cell_start > sel_cell_end) {
1758                         s_start = sel_cell_end;
1759                         s_end = sel_cell_start;
1760                 } else {
1761                         s_start = sel_cell_start;
1762                         s_end = sel_cell_end;
1763                 }
1764                 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1765                 actcell = s_start;
1766                 sel_cell_end = sel_cell_start;
1767                 UpdateLocal(bv, INIT, true);
1768                 break;
1769         }
1770         case LyXTabular::SET_ALL_LINES:
1771                 setLines = true;
1772         case LyXTabular::UNSET_ALL_LINES:
1773                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1774                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1775                                 tabular->SetAllLines(
1776                                         tabular->GetCellNumber(i,j), setLines);
1777                 UpdateLocal(bv, INIT, true);
1778                 break;
1779         case LyXTabular::SET_LONGTABULAR:
1780                 tabular->SetLongTabular(true);
1781                 UpdateLocal(bv, INIT, true); // because this toggles displayed
1782                 break;
1783         case LyXTabular::UNSET_LONGTABULAR:
1784                 tabular->SetLongTabular(false);
1785                 UpdateLocal(bv, INIT, true); // because this toggles displayed
1786                 break;
1787         case LyXTabular::SET_ROTATE_TABULAR:
1788                 tabular->SetRotateTabular(true);
1789                 break;
1790         case LyXTabular::UNSET_ROTATE_TABULAR:
1791                 tabular->SetRotateTabular(false);
1792                 break;
1793         case LyXTabular::SET_ROTATE_CELL:
1794                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1795                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1796                                 tabular->SetRotateCell(
1797                                         tabular->GetCellNumber(i, j),
1798                                         true);
1799                 break;
1800         case LyXTabular::UNSET_ROTATE_CELL:
1801                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1802                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1803                                 tabular->SetRotateCell(
1804                                         tabular->GetCellNumber(i, j), false);
1805                 break;
1806         case LyXTabular::SET_USEBOX:
1807         {
1808                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1809                 if (val == tabular->GetUsebox(actcell))
1810                         val = LyXTabular::BOX_NONE;
1811                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1812                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1813                                 tabular->SetUsebox(
1814                                         tabular->GetCellNumber(i, j), val);
1815                 break;
1816         }
1817         case LyXTabular::SET_LTFIRSTHEAD:
1818                 tabular->SetLTHead(actcell, true);
1819                 break;
1820         case LyXTabular::SET_LTHEAD:
1821                 tabular->SetLTHead(actcell, false);
1822                 break;
1823         case LyXTabular::SET_LTFOOT:
1824                 tabular->SetLTFoot(actcell, false);
1825                 break;
1826         case LyXTabular::SET_LTLASTFOOT:
1827                 tabular->SetLTFoot(actcell, true);
1828                 break;
1829         case LyXTabular::SET_LTNEWPAGE:
1830         {
1831                 bool what = !tabular->GetLTNewPage(actcell);
1832                 tabular->SetLTNewPage(actcell, what);
1833                 break;
1834         }
1835         // dummy stuff just to avoid warnings
1836         case LyXTabular::LAST_ACTION:
1837                 break;
1838         }
1839 }
1840
1841
1842 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1843                                      bool behind)
1844 {
1845         UpdatableInset * inset =
1846                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1847         LyXFont font(LyXFont::ALL_SANE);
1848         if (behind) {
1849                 x = inset->x() + inset->width(bv, font);
1850                 y = inset->descent(bv, font);
1851         }
1852         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1853         //inset_y = cursor.y();
1854         inset->Edit(bv, x,  y, button);
1855         if (!the_locking_inset)
1856                 return false;
1857         UpdateLocal(bv, CELL, false);
1858         return (the_locking_inset != 0);
1859 }
1860
1861
1862 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1863                                         int button)
1864 {
1865         inset_x = cursor.x()
1866                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1867         inset_y = cursor.y();
1868         return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1869 }
1870
1871
1872 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1873 {
1874         return (x + top_x)
1875                 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1876 }
1877
1878
1879 // This returns paperWidth() if the cell-width is unlimited or the width
1880 // in pixels if we have a pwidth for this cell.
1881 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1882 {
1883         string const s = tabular->GetPWidth(cell);
1884         
1885         if (s.empty())
1886                 return -1;
1887         return VSpace(s).inPixels(bv);
1888 }
1889
1890
1891 int InsetTabular::getMaxWidth(BufferView * bv,
1892                               UpdatableInset const * inset) const
1893 {
1894         int const n = tabular->GetNumberOfCells();
1895         int cell = 0;
1896         for (; cell < n; ++cell) {
1897                 if (tabular->GetCellInset(cell) == inset)
1898                         break;
1899         }
1900         if (cell >= n)
1901                 return -1;
1902         int w = GetMaxWidthOfCell(bv, cell);
1903         if (w > 0)
1904                 // because the inset then subtracts it's top_x and owner->x()
1905                 w += (inset->x() - top_x);
1906         return w;
1907 }
1908
1909
1910 void InsetTabular::resizeLyXText(BufferView *) const
1911 {
1912         need_update = FULL;
1913 }
1914
1915
1916 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1917                                    bool const recursive) const
1918 {
1919         if (the_locking_inset)
1920                 return the_locking_inset->getLyXText(bv, recursive);
1921         return Inset::getLyXText(bv, recursive);
1922 }
1923
1924
1925 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1926 {
1927         if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1928                 bv->owner()->getDialogs()
1929                         ->showTabular(const_cast<InsetTabular *>(this));
1930         return true;
1931 }
1932
1933
1934 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1935 {
1936         if (the_locking_inset) {
1937                 InsetTabular * i = static_cast<InsetTabular *>
1938                         (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1939                 if (i) {
1940                         i->OpenLayoutDialog(bv);
1941                         return;
1942                 }
1943         }
1944         bv->owner()->getDialogs()->showTabular(
1945                 const_cast<InsetTabular *>(this));
1946 }
1947
1948 //
1949 // functions returns:
1950 // 0 ... disabled
1951 // 1 ... enabled
1952 // 2 ... toggled on
1953 // 3 ... toggled off
1954 //
1955 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1956 {
1957         int action = LyXTabular::LAST_ACTION;
1958         LyXFunc::func_status status = LyXFunc::OK;
1959         
1960         int i = 0;
1961         for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1962                 string const tmp = tabularFeatures[i].feature;
1963                 if (tmp == what.substr(0, tmp.length())) {                  
1964                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1965                         //   tabularFeatures[i].feature.length())) {
1966                         action = tabularFeatures[i].action;
1967                         break;
1968                 }
1969         }
1970         if (action == LyXTabular::LAST_ACTION)
1971                 return LyXFunc::Unknown;
1972
1973         string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1974
1975         int sel_row_start;
1976         int sel_row_end;
1977         int dummy;
1978         bool flag = true;
1979
1980         if (hasSelection()) {
1981                 sel_row_start = tabular->row_of_cell(sel_cell_start);
1982                 sel_row_end = tabular->row_of_cell(sel_cell_end);
1983                 if (sel_row_start > sel_row_end) {
1984                         //int tmp = sel_row_start;
1985                         //sel_row_start = sel_row_end;
1986                         //sel_row_end = tmp;
1987                         swap(sel_row_start, sel_row_end);
1988                 }
1989         } else {
1990                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1991         }
1992
1993         switch (action) {
1994         case LyXTabular::SET_PWIDTH:
1995         case LyXTabular::SET_MPWIDTH:
1996         case LyXTabular::SET_SPECIAL_COLUMN:
1997         case LyXTabular::SET_SPECIAL_MULTI:
1998                 status |= LyXFunc::Disabled;
1999                 return status;
2000
2001         case LyXTabular::APPEND_ROW:
2002         case LyXTabular::APPEND_COLUMN:
2003         case LyXTabular::DELETE_ROW:
2004         case LyXTabular::DELETE_COLUMN:
2005         case LyXTabular::SET_ALL_LINES:
2006         case LyXTabular::UNSET_ALL_LINES:
2007                 status |= LyXFunc::OK;
2008                 return status;
2009
2010         case LyXTabular::MULTICOLUMN:
2011                 if (tabular->IsMultiColumn(actcell))
2012                         status |= LyXFunc::ToggleOn;
2013                 else
2014                         status |= LyXFunc::ToggleOff;
2015                 break;
2016         case LyXTabular::M_TOGGLE_LINE_TOP:
2017                 flag = false;
2018         case LyXTabular::TOGGLE_LINE_TOP:
2019                 if (tabular->TopLine(actcell, flag))
2020                         status |= LyXFunc::ToggleOn;
2021                 else
2022                         status |= LyXFunc::ToggleOff;
2023                 break;
2024         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2025                 flag = false;
2026         case LyXTabular::TOGGLE_LINE_BOTTOM:
2027                 if (tabular->BottomLine(actcell, flag))
2028                         status |= LyXFunc::ToggleOn;
2029                 else
2030                         status |= LyXFunc::ToggleOff;
2031                 break;
2032         case LyXTabular::M_TOGGLE_LINE_LEFT:
2033                 flag = false;
2034         case LyXTabular::TOGGLE_LINE_LEFT:
2035                 if (tabular->LeftLine(actcell, flag))
2036                         status |= LyXFunc::ToggleOn;
2037                 else
2038                         status |= LyXFunc::ToggleOff;
2039                 break;
2040         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2041                 flag = false;
2042         case LyXTabular::TOGGLE_LINE_RIGHT:
2043                 if (tabular->RightLine(actcell, flag))
2044                         status |= LyXFunc::ToggleOn;
2045                 else
2046                         status |= LyXFunc::ToggleOff;
2047                 break;
2048         case LyXTabular::M_ALIGN_LEFT:
2049                 flag = false;
2050         case LyXTabular::ALIGN_LEFT:
2051                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2052                         status |= LyXFunc::ToggleOn;
2053                 else
2054                         status |= LyXFunc::ToggleOff;
2055                 break;
2056         case LyXTabular::M_ALIGN_RIGHT:
2057                 flag = false;
2058         case LyXTabular::ALIGN_RIGHT:
2059                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2060                         status |= LyXFunc::ToggleOn;
2061                 else
2062                         status |= LyXFunc::ToggleOff;
2063                 break;
2064         case LyXTabular::M_ALIGN_CENTER:
2065                 flag = false;
2066         case LyXTabular::ALIGN_CENTER:
2067                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2068                         status |= LyXFunc::ToggleOn;
2069                 else
2070                         status |= LyXFunc::ToggleOff;
2071                 break;
2072         case LyXTabular::M_VALIGN_TOP:
2073                 flag = false;
2074         case LyXTabular::VALIGN_TOP:
2075                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2076                         status |= LyXFunc::ToggleOn;
2077                 else
2078                         status |= LyXFunc::ToggleOff;
2079                 break;
2080         case LyXTabular::M_VALIGN_BOTTOM:
2081                 flag = false;
2082         case LyXTabular::VALIGN_BOTTOM:
2083                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2084                         status |= LyXFunc::ToggleOn;
2085                 else
2086                         status |= LyXFunc::ToggleOff;
2087                 break;
2088         case LyXTabular::M_VALIGN_CENTER:
2089                 flag = false;
2090         case LyXTabular::VALIGN_CENTER:
2091                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2092                         status |= LyXFunc::ToggleOn;
2093                 else
2094                         status |= LyXFunc::ToggleOff;
2095                 break;
2096         case LyXTabular::SET_LONGTABULAR:
2097                 if (tabular->IsLongTabular())
2098                         status |= LyXFunc::ToggleOn;
2099                 else
2100                         status |= LyXFunc::ToggleOff;
2101                 break;
2102         case LyXTabular::UNSET_LONGTABULAR:
2103                 if (!tabular->IsLongTabular())
2104                         status |= LyXFunc::ToggleOn;
2105                 else
2106                         status |= LyXFunc::ToggleOff;
2107                 break;
2108         case LyXTabular::SET_ROTATE_TABULAR:
2109                 if (tabular->GetRotateTabular())
2110                         status |= LyXFunc::ToggleOn;
2111                 else
2112                         status |= LyXFunc::ToggleOff;
2113                 break;
2114         case LyXTabular::UNSET_ROTATE_TABULAR:
2115                 if (!tabular->GetRotateTabular())
2116                         status |= LyXFunc::ToggleOn;
2117                 else
2118                         status |= LyXFunc::ToggleOff;
2119                 break;
2120         case LyXTabular::SET_ROTATE_CELL:
2121                 if (tabular->GetRotateCell(actcell))
2122                         status |= LyXFunc::ToggleOn;
2123                 else
2124                         status |= LyXFunc::ToggleOff;
2125                 break;
2126         case LyXTabular::UNSET_ROTATE_CELL:
2127                 if (!tabular->GetRotateCell(actcell))
2128                         status |= LyXFunc::ToggleOn;
2129                 else
2130                         status |= LyXFunc::ToggleOff;
2131                 break;
2132         case LyXTabular::SET_USEBOX:
2133                 if (strToInt(argument) == tabular->GetUsebox(actcell))
2134                         status |= LyXFunc::ToggleOn;
2135                 else
2136                         status |= LyXFunc::ToggleOff;
2137                 break;
2138         case LyXTabular::SET_LTFIRSTHEAD:
2139                 if (tabular->GetRowOfLTHead(actcell, dummy))
2140                         status |= LyXFunc::ToggleOn;
2141                 else
2142                         status |= LyXFunc::ToggleOff;
2143                 break;
2144         case LyXTabular::SET_LTHEAD:
2145                 if (tabular->GetRowOfLTHead(actcell, dummy))
2146                         status |= LyXFunc::ToggleOn;
2147                 else
2148                         status |= LyXFunc::ToggleOff;
2149                 break;
2150         case LyXTabular::SET_LTFOOT:
2151                 if (tabular->GetRowOfLTFoot(actcell, dummy))
2152                         status |= LyXFunc::ToggleOn;
2153                 else
2154                         status |= LyXFunc::ToggleOff;
2155                 break;
2156         case LyXTabular::SET_LTLASTFOOT:
2157                 if (tabular->GetRowOfLTFoot(actcell, dummy))
2158                         status |= LyXFunc::ToggleOn;
2159                 else
2160                         status |= LyXFunc::ToggleOff;
2161                 break;
2162         case LyXTabular::SET_LTNEWPAGE:
2163                 if (tabular->GetLTNewPage(actcell))
2164                         status |= LyXFunc::ToggleOn;
2165                 else
2166                         status |= LyXFunc::ToggleOff;
2167                 break;
2168         default:
2169                 status = LyXFunc::Disabled;
2170                 break;
2171         }
2172         return status;
2173 }
2174
2175
2176 std::vector<string> const InsetTabular::getLabelList() const
2177 {
2178         return tabular->getLabelList();
2179 }
2180
2181
2182 bool InsetTabular::copySelection(BufferView * bv)
2183 {
2184         if (!hasSelection())
2185                 return false;
2186         //delete paste_tabular;
2187
2188         //int sel_col_start;
2189         //int sel_col_end;
2190         //int sel_row_start;
2191         //int sel_row_end;
2192
2193         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2194         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2195         if (sel_col_start > sel_col_end) {
2196                 sel_col_start = sel_col_end;
2197                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2198         } else {
2199                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2200         }
2201         int const columns = sel_col_end - sel_col_start + 1;
2202
2203         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2204         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2205         if (sel_row_start > sel_row_end) {
2206                 //int tmp tmp = sel_row_start;
2207                 //sel_row_start = sel_row_end;
2208                 //sel_row_end = tmp;
2209                 swap(sel_row_start, sel_row_end);
2210         }
2211         int const rows = sel_row_end - sel_row_start + 1;
2212
2213         delete paste_tabular;
2214         paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2215         //int i;
2216         for (int i = 0; i < sel_row_start; ++i)
2217                 paste_tabular->DeleteRow(0);
2218         while(paste_tabular->rows() > rows)
2219                 paste_tabular->DeleteRow(rows);
2220         paste_tabular->SetTopLine(0, true, true);
2221         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2222                                      true, true);
2223         for (int i = 0; i < sel_col_start; ++i)
2224                 paste_tabular->DeleteColumn(0);
2225         while (paste_tabular->columns() > columns)
2226                 paste_tabular->DeleteColumn(columns);
2227         paste_tabular->SetLeftLine(0, true, true);
2228         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2229                                     true, true);
2230
2231         ostringstream sstr;
2232         paste_tabular->Ascii(bv->buffer(), sstr);
2233         bv->stuffClipboard(sstr.str().c_str());
2234         return true;
2235 }
2236
2237
2238 bool InsetTabular::pasteSelection(BufferView * bv)
2239 {
2240         if (!paste_tabular)
2241                 return false;
2242
2243         for (int r1 = 0, r2 = actrow;
2244              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2245              ++r1, ++r2) {
2246                 for(int c1 = 0, c2 = actcol;
2247                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2248                     ++c1, ++c2) {
2249                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2250                             tabular->IsPartOfMultiColumn(r2,c2))
2251                                 continue;
2252                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2253                                 --c2;
2254                                 continue;
2255                         }
2256                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2257                                 --c1;
2258                                 continue;
2259                         }
2260                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2261                         int const n2 = tabular->GetCellNumber(r2, c2);
2262                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2263                         tabular->GetCellInset(n2)->setOwner(this);
2264                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2265                 }
2266         }
2267         return true;
2268 }
2269
2270
2271 bool InsetTabular::cutSelection()
2272 {
2273         if (!hasSelection())
2274                 return false;
2275
2276         //int sel_col_start;
2277         //int sel_col_end;
2278         //int sel_row_start;
2279         //int sel_row_end;
2280
2281         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2282         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2283         if (sel_col_start > sel_col_end) {
2284                 sel_col_start = sel_col_end;
2285                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2286         } else {
2287                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2288         }
2289         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2290         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2291         if (sel_row_start > sel_row_end) {
2292                 //int tmp = sel_row_start;
2293                 //sel_row_start = sel_row_end;
2294                 //sel_row_end = tmp;
2295                 swap(sel_row_start, sel_row_end);
2296         }
2297         if (sel_cell_start > sel_cell_end) {
2298                 //int tmp = sel_cell_start;
2299                 //sel_cell_start = sel_cell_end;
2300                 //sel_cell_end = tmp;
2301                 swap(sel_cell_start, sel_cell_end);
2302         }
2303         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2304                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2305                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2306                 }
2307         }
2308         return true;
2309 }
2310
2311
2312 bool InsetTabular::isRightToLeft(BufferView *bv )
2313 {
2314         return bv->getParentLanguage(this)->RightToLeft();
2315 }
2316
2317 bool InsetTabular::nodraw() const
2318 {
2319         if (the_locking_inset)
2320                 return the_locking_inset->nodraw();
2321         return UpdatableInset::nodraw();
2322 }
2323
2324 int InsetTabular::scroll(bool recursive) const
2325 {
2326         int sx = UpdatableInset::scroll(false);
2327
2328         if (recursive && the_locking_inset)
2329                 sx += the_locking_inset->scroll(recursive);
2330
2331         return sx;
2332 }
2333
2334 bool InsetTabular::doClearArea() const
2335 {
2336         return !locked || (need_update & (FULL|INIT));
2337 }
2338 /* Emacs:
2339  * Local variables:
2340  * tab-width: 4
2341  * End:
2342  * vi:set tabstop=4:
2343  */