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