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