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