]> git.lyx.org Git - features.git/blob - src/insets/insettabular.C
fix doxygen operation ; stop #warning causing errors on Sun CC 6.0
[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 #ifdef WITH_WARNINGS
1240 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1241 #endif
1242         for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1243              ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1244                      tabular->GetAdditionalWidth(actcell - 1));
1245 #else
1246         // Jürgen, you should check that this is correct. (Lgb)
1247         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1248                 lx += tabular->GetWidthOfColumn(actcell + 1)
1249                         + tabular->GetAdditionalWidth(actcell);
1250         }
1251         
1252 #endif
1253         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1254         resetPos(bv);
1255 }
1256
1257
1258 int InsetTabular::getCellXPos(int cell) const
1259 {
1260         int c = cell;
1261         
1262         for (; !tabular->IsFirstCellInRow(c); --c)
1263                 ;
1264         int lx = tabular->GetWidthOfColumn(cell);
1265         for (; c < cell; ++c) {
1266                 lx += tabular->GetWidthOfColumn(c);
1267         }
1268         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1269 }
1270
1271
1272 void InsetTabular::resetPos(BufferView * bv) const
1273 {
1274         if (!locked)
1275                 return;
1276         actcol = tabular->column_of_cell(actcell);
1277
1278         int cell = 0;
1279         actrow = 0;
1280         cursor.y(0);
1281         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1282                 if (tabular->IsLastCellInRow(cell)) {
1283                         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1284                                          tabular->GetAscentOfRow(actrow + 1) +
1285                                          tabular->GetAdditionalHeight(actrow + 1));
1286                         ++actrow;
1287                 }
1288         }
1289         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1290         int new_x = getCellXPos(actcell);
1291         int old_x = cursor.x();
1292         new_x += offset;
1293         cursor.x(new_x);
1294 //    cursor.x(getCellXPos(actcell) + offset);
1295         if (scroll(false) && (tabular->GetWidthOfTabular() < bv->workWidth()-20)) {
1296                 scroll(bv, 0.0F);
1297                 UpdateLocal(bv, FULL, false);
1298         } else if (the_locking_inset &&
1299                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20)) {
1300                 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1301                 if (xx > (bv->workWidth()-20))
1302                         scroll(bv, -(xx - bv->workWidth() + 60));
1303                 else if (xx < 20) {
1304                         if (xx < 0)
1305                                 xx = -xx + 60;
1306                         else
1307                                 xx = 60;
1308                         scroll(bv, xx);
1309                 }
1310                 UpdateLocal(bv, FULL, false);
1311         } else if ((cursor.x() - offset) > 20 &&
1312                    (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1313                    > (bv->workWidth() - 20)) {
1314                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1315                 UpdateLocal(bv, FULL, false);
1316         } else if ((cursor.x() - offset) < 20) {
1317                 scroll(bv, 20 - cursor.x() + offset);
1318                 UpdateLocal(bv, FULL, false);
1319         } else if (scroll(false) && top_x > 20 &&
1320                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1321                 scroll(bv, old_x - cursor.x());
1322                 UpdateLocal(bv, FULL, false);
1323         }
1324         if ((!the_locking_inset ||
1325              !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1326             actcell != oldcell) {
1327                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1328                 bv->owner()->getDialogs()->updateTabular(inset);
1329                 oldcell = actcell;
1330         }
1331 }
1332
1333
1334 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1335 {
1336         if (lock && !old_locking_inset) {
1337                 if (ActivateCellInset(bv))
1338                         return DISPATCHED;
1339         } else {
1340                 bool moved = isRightToLeft(bv)
1341                         ? movePrevCell(bv) : moveNextCell(bv);
1342                 if (!moved)
1343                         return FINISHED;
1344                 if (lock && ActivateCellInset(bv))
1345                         return DISPATCHED;
1346         }
1347         resetPos(bv);
1348         return DISPATCHED_NOUPDATE;
1349 }
1350
1351
1352 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1353 {
1354         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1355         if (!moved)
1356                 return FINISHED;
1357         if (lock) {       // behind the inset
1358                 if (ActivateCellInset(bv, 0, 0, 0, true))
1359                         return DISPATCHED;
1360         }
1361         resetPos(bv);
1362         return DISPATCHED_NOUPDATE;
1363 }
1364
1365
1366 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1367 {
1368         int const ocell = actcell;
1369         actcell = tabular->GetCellAbove(actcell);
1370         if (actcell == ocell) // we moved out of the inset
1371                 return FINISHED;
1372         resetPos(bv);
1373         if (lock) {
1374                 int x = 0;
1375                 int y = 0;
1376                 if (old_locking_inset) {
1377                         old_locking_inset->GetCursorPos(bv, x, y);
1378                         x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1379                 }
1380                 if (ActivateCellInset(bv, x, 0))
1381                         return DISPATCHED;
1382         }
1383         return DISPATCHED_NOUPDATE;
1384 }
1385
1386
1387 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1388 {
1389         int const ocell = actcell;
1390         actcell = tabular->GetCellBelow(actcell);
1391         if (actcell == ocell) // we moved out of the inset
1392                 return FINISHED;
1393         resetPos(bv);
1394         if (lock) {
1395                 int x = 0;
1396                 int y = 0;
1397                 if (old_locking_inset) {
1398                         old_locking_inset->GetCursorPos(bv, x, y);
1399                         x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1400                 }
1401                 if (ActivateCellInset(bv, x, 0))
1402                         return DISPATCHED;
1403         }
1404         return DISPATCHED_NOUPDATE;
1405 }
1406
1407
1408 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1409 {
1410         if (isRightToLeft(bv)) {
1411                 if (tabular->IsFirstCellInRow(actcell)) {
1412                         int row = tabular->row_of_cell(actcell);
1413                         if (row == tabular->rows() - 1)
1414                                 return false;
1415                         actcell = tabular->GetLastCellInRow(row);
1416                         actcell = tabular->GetCellBelow(actcell);
1417                 } else {
1418                         if (!actcell)
1419                                 return false;
1420                         --actcell;
1421                 }
1422         } else {
1423                 if (tabular->IsLastCell(actcell))
1424                         return false;
1425                 ++actcell;
1426         }
1427         if (lock) {
1428                 bool rtl = tabular->GetCellInset(actcell)->par->
1429                         isRightToLeftPar(bv->buffer()->params);
1430                 ActivateCellInset(bv, 0, 0, 0, !rtl);
1431         }
1432         resetPos(bv);
1433         return true;
1434 }
1435
1436
1437 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1438 {
1439         if (isRightToLeft(bv)) {
1440                 if (tabular->IsLastCellInRow(actcell)) {
1441                         int row = tabular->row_of_cell(actcell);
1442                         if (row == 0)
1443                                 return false;
1444                         actcell = tabular->GetFirstCellInRow(row);
1445                         actcell = tabular->GetCellAbove(actcell);
1446                 } else {
1447                         if (tabular->IsLastCell(actcell))
1448                                 return false;
1449                         ++actcell;
1450                 }
1451         } else {
1452                 if (!actcell) // first cell
1453                         return false;
1454                 --actcell;
1455         }
1456         if (lock) {
1457                 bool rtl = tabular->GetCellInset(actcell)->par->
1458                         isRightToLeftPar(bv->buffer()->params);
1459                 ActivateCellInset(bv, 0, 0, 0, !rtl);
1460         }
1461         resetPos(bv);
1462         return true;
1463 }
1464
1465
1466 bool InsetTabular::Delete()
1467 {
1468         return true;
1469 }
1470
1471
1472 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1473 {
1474         if (the_locking_inset)
1475                 the_locking_inset->SetFont(bv, font, tall);
1476 }
1477
1478
1479 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1480 {
1481         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1482         
1483         int i = 0;
1484         for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1485                 string const tmp = tabularFeatures[i].feature;
1486                 
1487                 if (tmp == what.substr(0, tmp.length())) {
1488                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1489                         //tabularFeatures[i].feature.length())) {
1490                         action = tabularFeatures[i].action;
1491                         break;
1492                 }
1493         }
1494         if (action == LyXTabular::LAST_ACTION)
1495                 return false;
1496
1497         string const val =
1498                 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1499         TabularFeatures(bv, action, val);
1500         return true;
1501 }
1502
1503
1504 void InsetTabular::TabularFeatures(BufferView * bv,
1505                                    LyXTabular::Feature feature,
1506                                    string const & value)
1507 {
1508         //int i;
1509         //int j;
1510         int sel_col_start;
1511         int sel_col_end;
1512         int sel_row_start;
1513         int sel_row_end;
1514         bool setLines = false;
1515         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1516         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1517         //int lineSet;
1518         //bool what;
1519
1520         switch (feature) {
1521         case LyXTabular::M_ALIGN_LEFT:
1522         case LyXTabular::ALIGN_LEFT:
1523                 setAlign=LYX_ALIGN_LEFT;
1524                 break;
1525         case LyXTabular::M_ALIGN_RIGHT:
1526         case LyXTabular::ALIGN_RIGHT:
1527                 setAlign=LYX_ALIGN_RIGHT;
1528                 break;
1529         case LyXTabular::M_ALIGN_CENTER:
1530         case LyXTabular::ALIGN_CENTER:
1531                 setAlign=LYX_ALIGN_CENTER;
1532                 break;
1533         case LyXTabular::M_VALIGN_TOP:
1534         case LyXTabular::VALIGN_TOP:
1535                 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1536                 break;
1537         case LyXTabular::M_VALIGN_BOTTOM:
1538         case LyXTabular::VALIGN_BOTTOM:
1539                 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1540                 break;
1541         case LyXTabular::M_VALIGN_CENTER:
1542         case LyXTabular::VALIGN_CENTER:
1543                 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1544                 break;
1545         default:
1546                 break;
1547         }
1548         if (hasSelection()) {
1549                 sel_col_start = tabular->column_of_cell(sel_cell_start);
1550                 sel_col_end = tabular->column_of_cell(sel_cell_end);
1551                 if (sel_col_start > sel_col_end) {
1552                         sel_col_end = sel_col_start;
1553                         sel_col_start = tabular->column_of_cell(sel_cell_end);
1554                 } else {
1555                         sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1556                 }
1557         
1558                 sel_row_start = tabular->row_of_cell(sel_cell_start);
1559                 sel_row_end = tabular->row_of_cell(sel_cell_end);
1560                 if (sel_row_start > sel_row_end) {
1561                         //int tmp = sel_row_start;
1562                         //sel_row_start = sel_row_end;
1563                         //sel_row_end = tmp;
1564                         swap(sel_row_start, sel_row_end);
1565                 }
1566         } else {
1567                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1568                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1569         }
1570         bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1571 #ifndef NEW_INSETS
1572                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1573                           bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1574 #else
1575                           bv->text->cursor.par()->previous(),
1576                           bv->text->cursor.par()->next()
1577 #endif
1578                 );
1579
1580         int row = tabular->row_of_cell(actcell);
1581         int column = tabular->column_of_cell(actcell);
1582         bool flag = true;
1583         
1584         switch (feature) {
1585         case LyXTabular::SET_PWIDTH:
1586         {
1587                 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1588                 tabular->SetColumnPWidth(actcell,value);
1589                 if (update) {
1590                         for (int i = 0; i < tabular->rows(); ++i) {
1591                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1592                                         resizeLyXText(bv);
1593                         }
1594                         UpdateLocal(bv, INIT, true);
1595                 }
1596         }
1597         break;
1598         case LyXTabular::SET_MPWIDTH:
1599         {
1600                 bool const update = (tabular->GetPWidth(actcell) != value);
1601                 tabular->SetMColumnPWidth(actcell,value);
1602                 if (update) {
1603                         for (int i = 0; i < tabular->rows(); ++i) {
1604                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1605                                         resizeLyXText(bv);
1606                         }
1607                         UpdateLocal(bv, INIT, true);
1608                 }
1609         }
1610         break;
1611         case LyXTabular::SET_SPECIAL_COLUMN:
1612         case LyXTabular::SET_SPECIAL_MULTI:
1613                 tabular->SetAlignSpecial(actcell,value,feature);
1614                 break;
1615         case LyXTabular::APPEND_ROW:
1616                 // append the row into the tabular
1617                 UnlockInsetInInset(bv, the_locking_inset);
1618                 tabular->AppendRow(actcell);
1619                 UpdateLocal(bv, INIT, true);
1620                 break;
1621         case LyXTabular::APPEND_COLUMN:
1622                 // append the column into the tabular
1623                 UnlockInsetInInset(bv, the_locking_inset);
1624                 tabular->AppendColumn(actcell);
1625                 actcell = tabular->GetCellNumber(row, column);
1626                 UpdateLocal(bv, INIT, true);
1627                 break;
1628         case LyXTabular::DELETE_ROW:
1629                 UnlockInsetInInset(bv, the_locking_inset);
1630                 tabular->DeleteRow(tabular->row_of_cell(actcell));
1631                 if ((row+1) > tabular->rows())
1632                         --row;
1633                 actcell = tabular->GetCellNumber(row, column);
1634                 clearSelection();
1635                 UpdateLocal(bv, INIT, true);
1636                 break;
1637         case LyXTabular::DELETE_COLUMN:
1638                 UnlockInsetInInset(bv, the_locking_inset);
1639                 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1640                 if ((column+1) > tabular->columns())
1641                         --column;
1642                 actcell = tabular->GetCellNumber(row, column);
1643                 clearSelection();
1644                 UpdateLocal(bv, INIT, true);
1645                 break;
1646         case LyXTabular::M_TOGGLE_LINE_TOP:
1647                 flag = false;
1648         case LyXTabular::TOGGLE_LINE_TOP:
1649         {
1650                 bool lineSet = !tabular->TopLine(actcell, flag);
1651                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1652                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1653                                 tabular->SetTopLine(
1654                                         tabular->GetCellNumber(i, j),
1655                                         lineSet, flag);
1656                 UpdateLocal(bv, INIT, true);
1657                 break;
1658         }
1659         
1660         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1661                 flag = false;
1662         case LyXTabular::TOGGLE_LINE_BOTTOM:
1663         {
1664                 bool lineSet = !tabular->BottomLine(actcell, flag); 
1665                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1666                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1667                                 tabular->SetBottomLine(
1668                                         tabular->GetCellNumber(i, j),
1669                                         lineSet,
1670                                         flag);
1671                 UpdateLocal(bv, INIT, true);
1672                 break;
1673         }
1674         
1675         case LyXTabular::M_TOGGLE_LINE_LEFT:
1676                 flag = false;
1677         case LyXTabular::TOGGLE_LINE_LEFT:
1678         {
1679                 bool lineSet = !tabular->LeftLine(actcell, flag);
1680                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1681                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1682                                 tabular->SetLeftLine(
1683                                         tabular->GetCellNumber(i,j),
1684                                         lineSet,
1685                                         flag);
1686                 UpdateLocal(bv, INIT, true);
1687                 break;
1688         }
1689         
1690         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1691                 flag = false;
1692         case LyXTabular::TOGGLE_LINE_RIGHT:
1693         {
1694                 bool lineSet = !tabular->RightLine(actcell, flag);
1695                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1696                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1697                                 tabular->SetRightLine(
1698                                         tabular->GetCellNumber(i,j),
1699                                         lineSet,
1700                                         flag);
1701                 UpdateLocal(bv, INIT, true);
1702                 break;
1703         }
1704         
1705         case LyXTabular::M_ALIGN_LEFT:
1706         case LyXTabular::M_ALIGN_RIGHT:
1707         case LyXTabular::M_ALIGN_CENTER:
1708                 flag = false;
1709         case LyXTabular::ALIGN_LEFT:
1710         case LyXTabular::ALIGN_RIGHT:
1711         case LyXTabular::ALIGN_CENTER:
1712                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1713                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1714                                 tabular->SetAlignment(
1715                                         tabular->GetCellNumber(i, j),
1716                                         setAlign,
1717                                         flag);
1718                 UpdateLocal(bv, INIT, true);
1719                 break;
1720         case LyXTabular::M_VALIGN_TOP:
1721         case LyXTabular::M_VALIGN_BOTTOM:
1722         case LyXTabular::M_VALIGN_CENTER:
1723                 flag = false;
1724         case LyXTabular::VALIGN_TOP:
1725         case LyXTabular::VALIGN_BOTTOM:
1726         case LyXTabular::VALIGN_CENTER:
1727                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1728                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1729                                 tabular->SetVAlignment(
1730                                         tabular->GetCellNumber(i, j),
1731                                         setVAlign, flag);
1732                 UpdateLocal(bv, INIT, true);
1733                 break;
1734         case LyXTabular::MULTICOLUMN:
1735         {
1736                 if (sel_row_start != sel_row_end) {
1737                         WriteAlert(_("Impossible Operation!"), 
1738                                    _("Multicolumns can only be horizontally."), 
1739                                    _("Sorry."));
1740                         return;
1741                 }
1742                 // just multicol for one Single Cell
1743                 if (!hasSelection()) {
1744                         // check wether we are completly in a multicol
1745                         if (tabular->IsMultiColumn(actcell)) {
1746                                 tabular->UnsetMultiColumn(actcell);
1747                                 UpdateLocal(bv, INIT, true);
1748                         } else {
1749                                 tabular->SetMultiColumn(actcell, 1);
1750                                 UpdateLocal(bv, CELL, true);
1751                         }
1752                         return;
1753                 }
1754                 // we have a selection so this means we just add all this
1755                 // cells to form a multicolumn cell
1756                 int s_start;
1757                 int s_end;
1758
1759                 if (sel_cell_start > sel_cell_end) {
1760                         s_start = sel_cell_end;
1761                         s_end = sel_cell_start;
1762                 } else {
1763                         s_start = sel_cell_start;
1764                         s_end = sel_cell_end;
1765                 }
1766                 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1767                 actcell = s_start;
1768                 sel_cell_end = sel_cell_start;
1769                 UpdateLocal(bv, INIT, true);
1770                 break;
1771         }
1772         case LyXTabular::SET_ALL_LINES:
1773                 setLines = true;
1774         case LyXTabular::UNSET_ALL_LINES:
1775                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1776                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1777                                 tabular->SetAllLines(
1778                                         tabular->GetCellNumber(i,j), setLines);
1779                 UpdateLocal(bv, INIT, true);
1780                 break;
1781         case LyXTabular::SET_LONGTABULAR:
1782                 tabular->SetLongTabular(true);
1783                 UpdateLocal(bv, INIT, true); // because this toggles displayed
1784                 break;
1785         case LyXTabular::UNSET_LONGTABULAR:
1786                 tabular->SetLongTabular(false);
1787                 UpdateLocal(bv, INIT, true); // because this toggles displayed
1788                 break;
1789         case LyXTabular::SET_ROTATE_TABULAR:
1790                 tabular->SetRotateTabular(true);
1791                 break;
1792         case LyXTabular::UNSET_ROTATE_TABULAR:
1793                 tabular->SetRotateTabular(false);
1794                 break;
1795         case LyXTabular::SET_ROTATE_CELL:
1796                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1797                         for (int j = sel_col_start; j<=sel_col_end; ++j)
1798                                 tabular->SetRotateCell(
1799                                         tabular->GetCellNumber(i, j),
1800                                         true);
1801                 break;
1802         case LyXTabular::UNSET_ROTATE_CELL:
1803                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1804                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1805                                 tabular->SetRotateCell(
1806                                         tabular->GetCellNumber(i, j), false);
1807                 break;
1808         case LyXTabular::SET_USEBOX:
1809         {
1810                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1811                 if (val == tabular->GetUsebox(actcell))
1812                         val = LyXTabular::BOX_NONE;
1813                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1814                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1815                                 tabular->SetUsebox(
1816                                         tabular->GetCellNumber(i, j), val);
1817                 break;
1818         }
1819         case LyXTabular::SET_LTFIRSTHEAD:
1820                 tabular->SetLTHead(actcell, true);
1821                 break;
1822         case LyXTabular::SET_LTHEAD:
1823                 tabular->SetLTHead(actcell, false);
1824                 break;
1825         case LyXTabular::SET_LTFOOT:
1826                 tabular->SetLTFoot(actcell, false);
1827                 break;
1828         case LyXTabular::SET_LTLASTFOOT:
1829                 tabular->SetLTFoot(actcell, true);
1830                 break;
1831         case LyXTabular::SET_LTNEWPAGE:
1832         {
1833                 bool what = !tabular->GetLTNewPage(actcell);
1834                 tabular->SetLTNewPage(actcell, what);
1835                 break;
1836         }
1837         // dummy stuff just to avoid warnings
1838         case LyXTabular::LAST_ACTION:
1839                 break;
1840         }
1841 }
1842
1843
1844 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1845                                      bool behind)
1846 {
1847         UpdatableInset * inset =
1848                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1849         LyXFont font(LyXFont::ALL_SANE);
1850         if (behind) {
1851                 x = inset->x() + inset->width(bv, font);
1852                 y = inset->descent(bv, font);
1853         }
1854         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1855         //inset_y = cursor.y();
1856         inset->Edit(bv, x,  y, button);
1857         if (!the_locking_inset)
1858                 return false;
1859         UpdateLocal(bv, CELL, false);
1860         return (the_locking_inset != 0);
1861 }
1862
1863
1864 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1865                                         int button)
1866 {
1867         inset_x = cursor.x()
1868                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1869         inset_y = cursor.y();
1870         return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1871 }
1872
1873
1874 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1875 {
1876         return (x + top_x)
1877                 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1878 }
1879
1880
1881 // This returns paperWidth() if the cell-width is unlimited or the width
1882 // in pixels if we have a pwidth for this cell.
1883 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1884 {
1885         string const s = tabular->GetPWidth(cell);
1886         
1887         if (s.empty())
1888                 return -1;
1889         return VSpace(s).inPixels(bv);
1890 }
1891
1892
1893 int InsetTabular::getMaxWidth(BufferView * bv,
1894                               UpdatableInset const * inset) const
1895 {
1896         int const n = tabular->GetNumberOfCells();
1897         int cell = 0;
1898         for (; cell < n; ++cell) {
1899                 if (tabular->GetCellInset(cell) == inset)
1900                         break;
1901         }
1902         if (cell >= n)
1903                 return -1;
1904         int w = GetMaxWidthOfCell(bv, cell);
1905         if (w > 0)
1906                 // because the inset then subtracts it's top_x and owner->x()
1907                 w += (inset->x() - top_x);
1908         return w;
1909 }
1910
1911
1912 void InsetTabular::resizeLyXText(BufferView *) const
1913 {
1914         need_update = FULL;
1915 }
1916
1917
1918 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1919                                    bool const recursive) const
1920 {
1921         if (the_locking_inset)
1922                 return the_locking_inset->getLyXText(bv, recursive);
1923         return Inset::getLyXText(bv, recursive);
1924 }
1925
1926
1927 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1928 {
1929         if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1930                 bv->owner()->getDialogs()
1931                         ->showTabular(const_cast<InsetTabular *>(this));
1932         return true;
1933 }
1934
1935
1936 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1937 {
1938         if (the_locking_inset) {
1939                 InsetTabular * i = static_cast<InsetTabular *>
1940                         (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1941                 if (i) {
1942                         i->OpenLayoutDialog(bv);
1943                         return;
1944                 }
1945         }
1946         bv->owner()->getDialogs()->showTabular(
1947                 const_cast<InsetTabular *>(this));
1948 }
1949
1950 //
1951 // functions returns:
1952 // 0 ... disabled
1953 // 1 ... enabled
1954 // 2 ... toggled on
1955 // 3 ... toggled off
1956 //
1957 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1958 {
1959         int action = LyXTabular::LAST_ACTION;
1960         LyXFunc::func_status status = LyXFunc::OK;
1961         
1962         int i = 0;
1963         for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1964                 string const tmp = tabularFeatures[i].feature;
1965                 if (tmp == what.substr(0, tmp.length())) {                  
1966                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1967                         //   tabularFeatures[i].feature.length())) {
1968                         action = tabularFeatures[i].action;
1969                         break;
1970                 }
1971         }
1972         if (action == LyXTabular::LAST_ACTION)
1973                 return LyXFunc::Unknown;
1974
1975         string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1976
1977         int sel_row_start;
1978         int sel_row_end;
1979         int dummy;
1980         bool flag = true;
1981
1982         if (hasSelection()) {
1983                 sel_row_start = tabular->row_of_cell(sel_cell_start);
1984                 sel_row_end = tabular->row_of_cell(sel_cell_end);
1985                 if (sel_row_start > sel_row_end) {
1986                         //int tmp = sel_row_start;
1987                         //sel_row_start = sel_row_end;
1988                         //sel_row_end = tmp;
1989                         swap(sel_row_start, sel_row_end);
1990                 }
1991         } else {
1992                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1993         }
1994
1995         switch (action) {
1996         case LyXTabular::SET_PWIDTH:
1997         case LyXTabular::SET_MPWIDTH:
1998         case LyXTabular::SET_SPECIAL_COLUMN:
1999         case LyXTabular::SET_SPECIAL_MULTI:
2000                 status |= LyXFunc::Disabled;
2001                 return status;
2002
2003         case LyXTabular::APPEND_ROW:
2004         case LyXTabular::APPEND_COLUMN:
2005         case LyXTabular::DELETE_ROW:
2006         case LyXTabular::DELETE_COLUMN:
2007         case LyXTabular::SET_ALL_LINES:
2008         case LyXTabular::UNSET_ALL_LINES:
2009                 status |= LyXFunc::OK;
2010                 return status;
2011
2012         case LyXTabular::MULTICOLUMN:
2013                 if (tabular->IsMultiColumn(actcell))
2014                         status |= LyXFunc::ToggleOn;
2015                 else
2016                         status |= LyXFunc::ToggleOff;
2017                 break;
2018         case LyXTabular::M_TOGGLE_LINE_TOP:
2019                 flag = false;
2020         case LyXTabular::TOGGLE_LINE_TOP:
2021                 if (tabular->TopLine(actcell, flag))
2022                         status |= LyXFunc::ToggleOn;
2023                 else
2024                         status |= LyXFunc::ToggleOff;
2025                 break;
2026         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2027                 flag = false;
2028         case LyXTabular::TOGGLE_LINE_BOTTOM:
2029                 if (tabular->BottomLine(actcell, flag))
2030                         status |= LyXFunc::ToggleOn;
2031                 else
2032                         status |= LyXFunc::ToggleOff;
2033                 break;
2034         case LyXTabular::M_TOGGLE_LINE_LEFT:
2035                 flag = false;
2036         case LyXTabular::TOGGLE_LINE_LEFT:
2037                 if (tabular->LeftLine(actcell, flag))
2038                         status |= LyXFunc::ToggleOn;
2039                 else
2040                         status |= LyXFunc::ToggleOff;
2041                 break;
2042         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2043                 flag = false;
2044         case LyXTabular::TOGGLE_LINE_RIGHT:
2045                 if (tabular->RightLine(actcell, flag))
2046                         status |= LyXFunc::ToggleOn;
2047                 else
2048                         status |= LyXFunc::ToggleOff;
2049                 break;
2050         case LyXTabular::M_ALIGN_LEFT:
2051                 flag = false;
2052         case LyXTabular::ALIGN_LEFT:
2053                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2054                         status |= LyXFunc::ToggleOn;
2055                 else
2056                         status |= LyXFunc::ToggleOff;
2057                 break;
2058         case LyXTabular::M_ALIGN_RIGHT:
2059                 flag = false;
2060         case LyXTabular::ALIGN_RIGHT:
2061                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2062                         status |= LyXFunc::ToggleOn;
2063                 else
2064                         status |= LyXFunc::ToggleOff;
2065                 break;
2066         case LyXTabular::M_ALIGN_CENTER:
2067                 flag = false;
2068         case LyXTabular::ALIGN_CENTER:
2069                 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2070                         status |= LyXFunc::ToggleOn;
2071                 else
2072                         status |= LyXFunc::ToggleOff;
2073                 break;
2074         case LyXTabular::M_VALIGN_TOP:
2075                 flag = false;
2076         case LyXTabular::VALIGN_TOP:
2077                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2078                         status |= LyXFunc::ToggleOn;
2079                 else
2080                         status |= LyXFunc::ToggleOff;
2081                 break;
2082         case LyXTabular::M_VALIGN_BOTTOM:
2083                 flag = false;
2084         case LyXTabular::VALIGN_BOTTOM:
2085                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2086                         status |= LyXFunc::ToggleOn;
2087                 else
2088                         status |= LyXFunc::ToggleOff;
2089                 break;
2090         case LyXTabular::M_VALIGN_CENTER:
2091                 flag = false;
2092         case LyXTabular::VALIGN_CENTER:
2093                 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2094                         status |= LyXFunc::ToggleOn;
2095                 else
2096                         status |= LyXFunc::ToggleOff;
2097                 break;
2098         case LyXTabular::SET_LONGTABULAR:
2099                 if (tabular->IsLongTabular())
2100                         status |= LyXFunc::ToggleOn;
2101                 else
2102                         status |= LyXFunc::ToggleOff;
2103                 break;
2104         case LyXTabular::UNSET_LONGTABULAR:
2105                 if (!tabular->IsLongTabular())
2106                         status |= LyXFunc::ToggleOn;
2107                 else
2108                         status |= LyXFunc::ToggleOff;
2109                 break;
2110         case LyXTabular::SET_ROTATE_TABULAR:
2111                 if (tabular->GetRotateTabular())
2112                         status |= LyXFunc::ToggleOn;
2113                 else
2114                         status |= LyXFunc::ToggleOff;
2115                 break;
2116         case LyXTabular::UNSET_ROTATE_TABULAR:
2117                 if (!tabular->GetRotateTabular())
2118                         status |= LyXFunc::ToggleOn;
2119                 else
2120                         status |= LyXFunc::ToggleOff;
2121                 break;
2122         case LyXTabular::SET_ROTATE_CELL:
2123                 if (tabular->GetRotateCell(actcell))
2124                         status |= LyXFunc::ToggleOn;
2125                 else
2126                         status |= LyXFunc::ToggleOff;
2127                 break;
2128         case LyXTabular::UNSET_ROTATE_CELL:
2129                 if (!tabular->GetRotateCell(actcell))
2130                         status |= LyXFunc::ToggleOn;
2131                 else
2132                         status |= LyXFunc::ToggleOff;
2133                 break;
2134         case LyXTabular::SET_USEBOX:
2135                 if (strToInt(argument) == tabular->GetUsebox(actcell))
2136                         status |= LyXFunc::ToggleOn;
2137                 else
2138                         status |= LyXFunc::ToggleOff;
2139                 break;
2140         case LyXTabular::SET_LTFIRSTHEAD:
2141                 if (tabular->GetRowOfLTHead(actcell, dummy))
2142                         status |= LyXFunc::ToggleOn;
2143                 else
2144                         status |= LyXFunc::ToggleOff;
2145                 break;
2146         case LyXTabular::SET_LTHEAD:
2147                 if (tabular->GetRowOfLTHead(actcell, dummy))
2148                         status |= LyXFunc::ToggleOn;
2149                 else
2150                         status |= LyXFunc::ToggleOff;
2151                 break;
2152         case LyXTabular::SET_LTFOOT:
2153                 if (tabular->GetRowOfLTFoot(actcell, dummy))
2154                         status |= LyXFunc::ToggleOn;
2155                 else
2156                         status |= LyXFunc::ToggleOff;
2157                 break;
2158         case LyXTabular::SET_LTLASTFOOT:
2159                 if (tabular->GetRowOfLTFoot(actcell, dummy))
2160                         status |= LyXFunc::ToggleOn;
2161                 else
2162                         status |= LyXFunc::ToggleOff;
2163                 break;
2164         case LyXTabular::SET_LTNEWPAGE:
2165                 if (tabular->GetLTNewPage(actcell))
2166                         status |= LyXFunc::ToggleOn;
2167                 else
2168                         status |= LyXFunc::ToggleOff;
2169                 break;
2170         default:
2171                 status = LyXFunc::Disabled;
2172                 break;
2173         }
2174         return status;
2175 }
2176
2177
2178 std::vector<string> const InsetTabular::getLabelList() const
2179 {
2180         return tabular->getLabelList();
2181 }
2182
2183
2184 bool InsetTabular::copySelection(BufferView * bv)
2185 {
2186         if (!hasSelection())
2187                 return false;
2188         //delete paste_tabular;
2189
2190         //int sel_col_start;
2191         //int sel_col_end;
2192         //int sel_row_start;
2193         //int sel_row_end;
2194
2195         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2196         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2197         if (sel_col_start > sel_col_end) {
2198                 sel_col_start = sel_col_end;
2199                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2200         } else {
2201                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2202         }
2203         int const columns = sel_col_end - sel_col_start + 1;
2204
2205         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2206         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2207         if (sel_row_start > sel_row_end) {
2208                 //int tmp tmp = sel_row_start;
2209                 //sel_row_start = sel_row_end;
2210                 //sel_row_end = tmp;
2211                 swap(sel_row_start, sel_row_end);
2212         }
2213         int const rows = sel_row_end - sel_row_start + 1;
2214
2215         delete paste_tabular;
2216         paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2217         //int i;
2218         for (int i = 0; i < sel_row_start; ++i)
2219                 paste_tabular->DeleteRow(0);
2220         while(paste_tabular->rows() > rows)
2221                 paste_tabular->DeleteRow(rows);
2222         paste_tabular->SetTopLine(0, true, true);
2223         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2224                                      true, true);
2225         for (int i = 0; i < sel_col_start; ++i)
2226                 paste_tabular->DeleteColumn(0);
2227         while (paste_tabular->columns() > columns)
2228                 paste_tabular->DeleteColumn(columns);
2229         paste_tabular->SetLeftLine(0, true, true);
2230         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2231                                     true, true);
2232
2233         ostringstream sstr;
2234         paste_tabular->Ascii(bv->buffer(), sstr);
2235         bv->stuffClipboard(sstr.str().c_str());
2236         return true;
2237 }
2238
2239
2240 bool InsetTabular::pasteSelection(BufferView * bv)
2241 {
2242         if (!paste_tabular)
2243                 return false;
2244
2245         for (int r1 = 0, r2 = actrow;
2246              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2247              ++r1, ++r2) {
2248                 for(int c1 = 0, c2 = actcol;
2249                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2250                     ++c1, ++c2) {
2251                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2252                             tabular->IsPartOfMultiColumn(r2,c2))
2253                                 continue;
2254                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2255                                 --c2;
2256                                 continue;
2257                         }
2258                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2259                                 --c1;
2260                                 continue;
2261                         }
2262                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2263                         int const n2 = tabular->GetCellNumber(r2, c2);
2264                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2265                         tabular->GetCellInset(n2)->setOwner(this);
2266                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2267                 }
2268         }
2269         return true;
2270 }
2271
2272
2273 bool InsetTabular::cutSelection()
2274 {
2275         if (!hasSelection())
2276                 return false;
2277
2278         //int sel_col_start;
2279         //int sel_col_end;
2280         //int sel_row_start;
2281         //int sel_row_end;
2282
2283         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2284         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2285         if (sel_col_start > sel_col_end) {
2286                 sel_col_start = sel_col_end;
2287                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2288         } else {
2289                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2290         }
2291         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2292         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2293         if (sel_row_start > sel_row_end) {
2294                 //int tmp = sel_row_start;
2295                 //sel_row_start = sel_row_end;
2296                 //sel_row_end = tmp;
2297                 swap(sel_row_start, sel_row_end);
2298         }
2299         if (sel_cell_start > sel_cell_end) {
2300                 //int tmp = sel_cell_start;
2301                 //sel_cell_start = sel_cell_end;
2302                 //sel_cell_end = tmp;
2303                 swap(sel_cell_start, sel_cell_end);
2304         }
2305         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2306                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2307                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2308                 }
2309         }
2310         return true;
2311 }
2312
2313
2314 bool InsetTabular::isRightToLeft(BufferView *bv )
2315 {
2316         return bv->getParentLanguage(this)->RightToLeft();
2317 }
2318
2319 bool InsetTabular::nodraw() const
2320 {
2321         if (the_locking_inset)
2322                 return the_locking_inset->nodraw();
2323         return UpdatableInset::nodraw();
2324 }
2325
2326 int InsetTabular::scroll(bool recursive) const
2327 {
2328         int sx = UpdatableInset::scroll(false);
2329
2330         if (recursive && the_locking_inset)
2331                 sx += the_locking_inset->scroll(recursive);
2332
2333         return sx;
2334 }
2335
2336 bool InsetTabular::doClearArea() const
2337 {
2338         return !locked || (need_update & (FULL|INIT));
2339 }
2340 /* Emacs:
2341  * Local variables:
2342  * tab-width: 4
2343  * End:
2344  * vi:set tabstop=4:
2345  */