]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
read the Changelog
[lyx.git] / src / insets / insettabular.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15
16 #include <cstdlib>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "insettabular.h"
23
24 #include "buffer.h"
25 #include "commandtags.h"
26 #include "LaTeXFeatures.h"
27 #include "Painter.h"
28 #include "font.h"
29 #include "lyxtext.h"
30 #include "lyx_gui_misc.h"
31 #include "LyXView.h"
32 #include "lyxfunc.h"
33 #include "insets/insettext.h"
34 #include "frontends/Dialogs.h"
35
36 const int ADD_TO_HEIGHT = 2;
37 const int ADD_TO_TABULAR_WIDTH = 2;
38
39 using std::ostream;
40 using std::ifstream;
41 using std::max;
42 using std::endl;
43 using std::swap;
44
45 #define cellstart(p) ((p % 2) == 0)
46
47 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
48 {
49     if (rows <= 0)
50         rows = 1;
51     if (columns <= 0)
52         columns = 1;
53     buffer = buf; // set this first!!!
54     tabular = new LyXTabular(this, rows,columns);
55     // for now make it always display as display() inset
56     // just for test!!!
57     the_locking_inset = 0;
58     locked = no_selection = cursor_visible = false;
59     cursor.x_fix(-1);
60     oldcell = -1;
61     actcell = 0;
62     cursor.pos(0);
63     sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
64     dialogs_ = 0;
65     need_update = INIT;
66 }
67
68
69 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
70 {
71     buffer = buf; // set this first
72     tabular = new LyXTabular(this, *(tab.tabular));
73     the_locking_inset = 0;
74     locked = no_selection = cursor_visible = false;
75     cursor.x_fix(-1);
76     oldcell = -1;
77     actcell = 0;
78     cursor.pos(0);
79     sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
80     dialogs_ = 0;
81     need_update = INIT;
82 }
83
84
85 InsetTabular::~InsetTabular()
86 {
87     delete tabular;
88     if (dialogs_)
89         dialogs_->hideTabular(this);
90 }
91
92
93 Inset * InsetTabular::Clone() const
94 {
95     InsetTabular * t = new InsetTabular(*this, buffer);
96     delete t->tabular;
97     t->tabular = tabular->Clone(t);
98     return t;
99 }
100
101
102 void InsetTabular::Write(Buffer const * buf, ostream & os) const
103 {
104     os << " Tabular" << endl;
105     tabular->Write(buf, os);
106 }
107
108
109 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
110 {
111     bool old_format = (lex.GetString() == "\\LyXTable");
112     string token;
113
114     if (tabular)
115         delete tabular;
116     tabular = new LyXTabular(buf, this, lex);
117
118     need_update = INIT;
119
120     if (old_format)
121         return;
122
123     lex.nextToken();
124     token = lex.GetString();
125     while (lex.IsOK() && (token != "\\end_inset")) {
126         lex.nextToken();
127         token = lex.GetString();
128     }
129     if (token != "\\end_inset") {
130         lex.printError("Missing \\end_inset at this point. "
131                        "Read: `$$Token'");
132     }
133 }
134
135
136 int InsetTabular::ascent(BufferView *, LyXFont const &) const
137 {
138     return tabular->GetAscentOfRow(0);
139 }
140
141
142 int InsetTabular::descent(BufferView *, LyXFont const &) const
143 {
144     return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
145 }
146
147
148 int InsetTabular::width(BufferView *, LyXFont const &) const
149 {
150     return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
151 }
152
153
154 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
155                         float & x, bool cleared) const
156 {
157     Painter & pain = bv->painter();
158     int i, j, cell = 0;
159     int nx;
160     float cx;
161
162     UpdatableInset::draw(bv,font,baseline,x,cleared);
163     if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
164                      (top_x != int(x)) || (top_baseline != baseline))) {
165         int h = ascent(bv, font) + descent(bv, font);
166         int tx = display()||!owner()? 0:top_x;
167         int w =  tx? width(bv, font):pain.paperWidth();
168         int ty = baseline - ascent(bv, font);
169         
170         if (ty < 0)
171             ty = 0;
172         if ((ty + h) > pain.paperHeight())
173             h = pain.paperHeight();
174         if ((top_x + w) > pain.paperWidth())
175             w = pain.paperWidth();
176         pain.fillRectangle(tx, ty, w, h);
177         need_update = FULL;
178         cleared = true;
179     }
180     top_x = int(x);
181     top_baseline = baseline;
182     bool dodraw;
183     x += ADD_TO_TABULAR_WIDTH;
184     if (cleared || (need_update == FULL) || (need_update == CELL)) {
185         for(i=0;i<tabular->rows();++i) {
186             nx = int(x);
187             dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
188                     (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
189             for(j=0;j<tabular->columns();++j) {
190                 if (tabular->IsPartOfMultiColumn(i,j))
191                     continue;
192                 cx = nx + tabular->GetBeginningOfTextInCell(cell);
193                 if (hasSelection())
194                     DrawCellSelection(pain, nx, baseline, i, j, cell);
195                 if (dodraw && !cleared && locked && the_locking_inset) {
196                     if (the_locking_inset == tabular->GetCellInset(cell))
197                         tabular->GetCellInset(cell)->draw(bv, font,
198                                                           baseline, cx,
199                                                           cleared);
200                 } else if (dodraw) {
201                     tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
202                                                       cleared);
203                     DrawCellLines(pain, nx, baseline, i, cell);
204                 }
205                 nx += tabular->GetWidthOfColumn(cell);
206                 ++cell;
207             }
208             baseline += tabular->GetDescentOfRow(i) +
209                 tabular->GetAscentOfRow(i+1)+
210                 tabular->GetAdditionalHeight(cell+1);
211         }
212     }
213     x += width(bv, font);
214     need_update = NONE;
215 }
216
217
218 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
219                                  int row, int cell) const
220 {
221     int  x2 = x + tabular->GetWidthOfColumn(cell);
222     bool on_off;
223
224     if (!tabular->TopAlreadyDrawed(cell)) {
225         on_off = !tabular->TopLine(cell);
226         pain.line(x, baseline - tabular->GetAscentOfRow(row),
227                   x2, baseline -  tabular->GetAscentOfRow(row),
228                   on_off ? LColor::tabularonoffline:LColor::tabularline,
229                   on_off ? Painter::line_onoffdash:Painter::line_solid);
230     }
231     on_off = !tabular->BottomLine(cell);
232     pain.line(x,baseline +  tabular->GetDescentOfRow(row),
233               x2, baseline +  tabular->GetDescentOfRow(row),
234               on_off ? LColor::tabularonoffline:LColor::tabularline,
235               on_off ? Painter::line_onoffdash:Painter::line_solid);
236     if (!tabular->LeftAlreadyDrawed(cell)) {
237         on_off = !tabular->LeftLine(cell);
238         pain.line(x, baseline -  tabular->GetAscentOfRow(row),
239                   x, baseline +  tabular->GetDescentOfRow(row),
240                   on_off ? LColor::tabularonoffline:LColor::tabularline,
241                   on_off ? Painter::line_onoffdash:Painter::line_solid);
242     }
243     on_off = !tabular->RightLine(cell);
244     pain.line(x2 - tabular->GetAdditionalWidth(cell),
245               baseline -  tabular->GetAscentOfRow(row),
246               x2 - tabular->GetAdditionalWidth(cell),
247               baseline +  tabular->GetDescentOfRow(row),
248               on_off ? LColor::tabularonoffline:LColor::tabularline,
249               on_off ? Painter::line_onoffdash:Painter::line_solid);
250 }
251
252
253 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
254                                      int row, int column, int cell) const
255 {
256     int cs = tabular->column_of_cell(sel_cell_start);
257     int ce = tabular->column_of_cell(sel_cell_end);
258     if (cs > ce) {
259         ce = cs;
260         cs = tabular->column_of_cell(sel_cell_end);
261     } else {
262         ce = tabular->right_column_of_cell(sel_cell_end);
263     }
264
265     int rs = tabular->row_of_cell(sel_cell_start);
266     int re = tabular->row_of_cell(sel_cell_end);
267     if (rs > re) swap(rs, re);
268
269     if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
270         int w = tabular->GetWidthOfColumn(cell);
271         int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
272         pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
273                            w, h, LColor::selection);
274     }
275 }
276
277
278 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
279 {
280     if (reinit) {
281         need_update = INIT;
282         calculate_dimensions_of_cells(bv, font, true);
283         if (owner())
284             owner()->update(bv, font, true);
285         return;
286     }
287     if (the_locking_inset)
288         the_locking_inset->update(bv, font, reinit);
289     switch(need_update) {
290     case INIT:
291     case FULL:
292     case CELL:
293         if (calculate_dimensions_of_cells(bv, font, false))
294             need_update = INIT;
295         break;
296     case SELECTION:
297         need_update = INIT;
298         break;
299     default:
300         break;
301     }
302 }
303
304
305 char const * InsetTabular::EditMessage() const
306 {
307     return _("Opened Tabular Inset");
308 }
309
310
311 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
312 {
313     UpdatableInset::Edit(bv, x, y, button);
314
315     if (!bv->lockInset(this)) {
316         lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
317         return;
318     }
319     locked = true;
320     the_locking_inset = 0;
321     inset_pos = inset_x = inset_y = 0;
322     setPos(bv, x, y);
323     sel_pos_start = sel_pos_end = cursor.pos();
324     sel_cell_start = sel_cell_end = actcell;
325     bv->text->FinishUndo();
326     if (InsetHit(bv, x, y)) {
327         ActivateCellInset(bv, x, y, button);
328     }
329     UpdateLocal(bv, NONE, false);
330 //    bv->getOwner()->getPopups().updateFormTabular();
331 }
332
333
334 void InsetTabular::InsetUnlock(BufferView * bv)
335 {
336     if (the_locking_inset) {
337         the_locking_inset->InsetUnlock(bv);
338         the_locking_inset = 0;
339     }
340     HideInsetCursor(bv);
341     if (hasSelection()) {
342         sel_pos_start = sel_pos_end = 0;
343         sel_cell_start = sel_cell_end = 0;
344         UpdateLocal(bv, FULL, false);
345     }
346     no_selection = false;
347     oldcell = -1;
348     locked = false;
349 }
350
351
352 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
353                                bool mark_dirty)
354 {
355     need_update = what;
356     bv->updateInset(this, mark_dirty);
357     if (what != NONE)
358         resetPos(bv);
359 }
360
361
362 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
363 {
364     lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
365     if (!inset)
366         return false;
367     oldcell = -1;
368     if (inset == tabular->GetCellInset(actcell)) {
369         lyxerr[Debug::INSETS] << "OK" << endl;
370         the_locking_inset = tabular->GetCellInset(actcell);
371         resetPos(bv);
372         inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
373         inset_y = cursor.y();
374         inset_pos = cursor.pos();
375         return true;
376     } else if (the_locking_inset && (the_locking_inset == inset)) {
377         if (cursor.pos() == inset_pos) {
378             lyxerr[Debug::INSETS] << "OK" << endl;
379             resetPos(bv);
380             inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
381             inset_y = cursor.y();
382         } else {
383             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
384         }
385     } else if (the_locking_inset) {
386         lyxerr[Debug::INSETS] << "MAYBE" << endl;
387         return the_locking_inset->LockInsetInInset(bv, inset);
388     }
389     lyxerr[Debug::INSETS] << "NOT OK" << endl;
390     return false;
391 }
392
393
394 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
395                                    bool lr)
396 {
397     if (!the_locking_inset)
398         return false;
399     if (the_locking_inset == inset) {
400         the_locking_inset->InsetUnlock(bv);
401         the_locking_inset = 0;
402         if (lr)
403             moveRight(bv, false);
404         UpdateLocal(bv, CELL, false);
405         return true;
406     }
407     if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
408         if ((inset->LyxCode() == TABULAR_CODE) &&
409             !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
410         {
411             dialogs_ = bv->owner()->getDialogs();
412             dialogs_->updateTabular(const_cast<InsetTabular *>(this));
413             oldcell = actcell;
414         }
415         return true;
416     }
417     return false;
418 }
419
420
421 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
422 {
423     if (!the_locking_inset)
424         return false;
425     if (the_locking_inset != inset)
426         return the_locking_inset->UpdateInsetInInset(bv, inset);
427     UpdateLocal(bv, CELL, false);
428     return true;
429 }
430
431
432 int InsetTabular::InsetInInsetY()
433 {
434     if (!the_locking_inset)
435         return 0;
436
437     return (inset_y + the_locking_inset->InsetInInsetY());
438 }
439
440
441 UpdatableInset * InsetTabular::GetLockingInset()
442 {
443     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
444 }
445
446
447 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
448 {
449     if (c == LyxCode())
450         return this;
451     if (the_locking_inset)
452         return the_locking_inset->GetFirstLockingInsetOfType(c);
453     return 0;
454 }
455
456
457 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
458 {
459     if (the_locking_inset)
460         return the_locking_inset->InsertInset(bv, inset);
461     return false;
462 }
463
464
465 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
466 {
467     if (hasSelection()) {
468         sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
469         UpdateLocal(bv, SELECTION, false);
470     }
471     no_selection = false;
472
473     int ocell = actcell;
474
475     setPos(bv, x, y);
476     sel_pos_start = sel_pos_end = cursor.pos();
477     sel_cell_start = sel_cell_end = actcell;
478
479     bool inset_hit = InsetHit(bv, x, y);
480
481     if ((ocell == actcell) && the_locking_inset && inset_hit) {
482         the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
483         return;
484     } else if (the_locking_inset) {
485         the_locking_inset->InsetUnlock(bv);
486     }
487     the_locking_inset = 0;
488     if (inset_hit && bv->the_locking_inset) {
489         ActivateCellInset(bv, x, y, button);
490         the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
491     }
492 }
493
494
495 void InsetTabular::InsetButtonRelease(BufferView * bv,
496                                       int x, int y, int button)
497 {
498     if (button == 3) {
499         if (the_locking_inset) {
500             UpdatableInset * i;
501             if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
502                 i->InsetButtonRelease(bv, x, y, button);
503                 return;
504             }
505         }
506         dialogs_ = bv->owner()->getDialogs();
507         dialogs_->showTabular(this);
508 #if 0
509         else if (ocell != actcell)
510                 bview->getOwner()->getPopups().updateTabular();
511 #endif
512         return;
513     }
514     if (the_locking_inset) {
515         the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
516         return;
517     }
518     no_selection = false;
519 }
520
521
522 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
523 {
524     if (the_locking_inset) {
525         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
526                                              y - inset_y, button);
527         return;
528     }
529     if (!no_selection) {
530             // int ocell = actcell,
531             int old = sel_pos_end;
532
533         setPos(bv, x, y);
534         sel_pos_end = cursor.pos();
535         sel_cell_end = actcell;
536         if (old != sel_pos_end)
537             UpdateLocal(bv, SELECTION, false);
538 #if 0
539         if (ocell != actcell)
540             bview->getOwner()->getPopups().updateFormTabular();
541 #endif
542     }
543     no_selection = false;
544 }
545
546
547 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
548 {
549     if (the_locking_inset) {
550         the_locking_inset->InsetKeyPress(xke);
551         return;
552     }
553 }
554
555
556 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
557                                                    string const & arg)
558 {
559     UpdatableInset::RESULT 
560         result;
561
562     no_selection = false;
563     if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
564         || (result == DISPATCHED_NOUPDATE)) {
565
566         resetPos(bv);
567         return result;
568     }
569
570     if ((action < 0) && arg.empty())
571         return FINISHED;
572
573     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
574         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
575         cursor.x_fix(-1);
576     if (the_locking_inset) {
577         result=the_locking_inset->LocalDispatch(bv, action, arg);
578         if (result == DISPATCHED_NOUPDATE)
579             return result;
580         else if (result == DISPATCHED) {
581             the_locking_inset->ToggleInsetCursor(bv);
582             UpdateLocal(bv, CELL, false);
583             the_locking_inset->ToggleInsetCursor(bv);
584             return result;
585         } else if (result == FINISHED) {
586             if ((action == LFUN_RIGHT) || (action == -1)) {
587                 cursor.pos(inset_pos + 1);
588                 resetPos(bv);
589             }
590             sel_pos_start = sel_pos_end = cursor.pos();
591             sel_cell_start = sel_cell_end = actcell;
592             the_locking_inset=0;
593             result = DISPATCHED;
594             return result;
595         }
596     }
597
598     bool hs = hasSelection();
599     HideInsetCursor(bv);
600     result=DISPATCHED;
601     switch (action) {
602         // Normal chars not handled here
603     case -1:
604         break;
605         // --- Cursor Movements ---------------------------------------------
606     case LFUN_RIGHTSEL:
607         if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
608             break;
609         moveRight(bv, false);
610         sel_pos_end = cursor.pos();
611         if (!cellstart(cursor.pos())) {
612             if (tabular->right_column_of_cell(sel_cell_start) >
613                 tabular->right_column_of_cell(actcell))
614                 sel_cell_end = actcell+1;
615             else
616                 sel_cell_end = actcell;
617         }
618         UpdateLocal(bv, SELECTION, false);
619         break;
620     case LFUN_RIGHT:
621         result = moveRight(bv);
622         sel_pos_start = sel_pos_end = cursor.pos();
623         sel_cell_start = sel_cell_end = actcell;
624         if (hs)
625             UpdateLocal(bv, SELECTION, false);
626         break;
627     case LFUN_LEFTSEL:
628         if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
629             break;
630         moveLeft(bv, false);
631         sel_pos_end = cursor.pos();
632         if (cellstart(cursor.pos())) {
633             if (tabular->column_of_cell(sel_cell_start) >=
634                 tabular->column_of_cell(actcell))
635                 sel_cell_end = actcell;
636             else
637                 sel_cell_end = actcell-1;
638         }
639         UpdateLocal(bv, SELECTION, false);
640         break;
641     case LFUN_LEFT:
642         result = moveLeft(bv);
643         sel_pos_start = sel_pos_end = cursor.pos();
644         sel_cell_start = sel_cell_end = actcell;
645         if (hs)
646             UpdateLocal(bv, SELECTION, false);
647         break;
648     case LFUN_DOWNSEL:
649     {
650         int ocell = actcell;
651         moveDown(bv);
652         sel_pos_end = cursor.pos();
653         if ((ocell == sel_cell_end) ||
654             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
655             sel_cell_end = tabular->GetCellBelow(sel_cell_end);
656         else
657             sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
658         UpdateLocal(bv, SELECTION, false);
659     }
660     break;
661     case LFUN_DOWN:
662         result= moveDown(bv);
663         sel_pos_start = sel_pos_end = cursor.pos();
664         sel_cell_start = sel_cell_end = actcell;
665         if (hs)
666             UpdateLocal(bv, SELECTION, false);
667         break;
668     case LFUN_UPSEL:
669     {
670         int ocell = actcell;
671         moveUp(bv);
672         sel_pos_end = cursor.pos();
673         if ((ocell == sel_cell_end) ||
674             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
675             sel_cell_end = tabular->GetCellAbove(sel_cell_end);
676         else
677             sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
678         UpdateLocal(bv, SELECTION, false);
679     }
680     break;
681     case LFUN_UP:
682         result= moveUp(bv);
683         sel_pos_start = sel_pos_end = cursor.pos();
684         sel_cell_start = sel_cell_end = actcell;
685         if (hs)
686             UpdateLocal(bv, SELECTION, false);
687         break;
688     case LFUN_BACKSPACE:
689         break;
690     case LFUN_DELETE:
691         break;
692     case LFUN_HOME:
693         break;
694     case LFUN_END:
695         break;
696     case LFUN_SHIFT_TAB:
697     case LFUN_TAB:
698         if (the_locking_inset) {
699             UnlockInsetInInset(bv, the_locking_inset);
700             the_locking_inset = 0;
701         }
702         if (action == LFUN_TAB)
703             moveNextCell(bv);
704         else
705             movePrevCell(bv);
706         sel_pos_start = sel_pos_end = cursor.pos();
707         sel_cell_start = sel_cell_end = actcell;
708         if (hs)
709             UpdateLocal(bv, SELECTION, false);
710         break;
711     case LFUN_LAYOUT_TABLE:
712     {
713         dialogs_ = bv->owner()->getDialogs();
714         dialogs_->showTabular(this);
715     }
716     break;
717     default:
718         result = UNDISPATCHED;
719         break;
720     }
721     if (result!=FINISHED) {
722         if (!the_locking_inset) {
723 #if 0       
724             if (ocell != actcell)
725                 bview->getOwner()->getPopups().updateFormTabular();
726 #endif
727             ShowInsetCursor(bv);
728         }
729     } else
730         bv->unlockInset(this);
731     return result;
732 }
733
734
735 int InsetTabular::Latex(Buffer const * buf, ostream & os,
736                         bool fragile, bool fp) const
737 {
738     return tabular->Latex(buf, os, fragile, fp);
739 }
740
741
742 int InsetTabular::Ascii(Buffer const *, ostream &) const
743 {
744     return 0;
745 }
746
747
748 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
749 {
750     return 0;
751 }
752
753
754 int InsetTabular::DocBook(Buffer const *, ostream &) const
755 {
756     return 0;
757 }
758
759
760 void InsetTabular::Validate(LaTeXFeatures & features) const
761 {
762     tabular->Validate(features);
763 }
764
765
766 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
767                                                  LyXFont const & font,
768                                                  bool reinit) const
769 {
770     int cell = -1;
771     int maxAsc, maxDesc;
772     InsetText * inset;
773     bool changed = false;
774     
775     for(int i = 0; i < tabular->rows(); ++i) {
776         maxAsc = maxDesc = 0;
777         for(int j= 0; j < tabular->columns(); ++j) {
778             if (tabular->IsPartOfMultiColumn(i,j))
779                 continue;
780             ++cell;
781             inset = tabular->GetCellInset(cell);
782             if (!reinit)
783                 inset->update(bv, font, false);
784             maxAsc = max(maxAsc, inset->ascent(bv, font));
785             maxDesc = max(maxDesc, inset->descent(bv, font));
786             changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
787         }
788         changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
789         changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
790     }
791     return changed;
792 }
793
794
795 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
796 {
797     x = cursor.x() - top_x;
798     y = cursor.y();
799 }
800
801
802 void InsetTabular::ToggleInsetCursor(BufferView * bv)
803 {
804     if (the_locking_inset) {
805         the_locking_inset->ToggleInsetCursor(bv);
806         return;
807     }
808
809     LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
810
811     int asc = lyxfont::maxAscent(font);
812     int desc = lyxfont::maxDescent(font);
813   
814     if (cursor_visible)
815         bv->hideLockedInsetCursor();
816     else
817         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
818     cursor_visible = !cursor_visible;
819 }
820
821
822 void InsetTabular::ShowInsetCursor(BufferView * bv)
823 {
824     if (!cursor_visible) {
825         LyXFont font; // = GetFont(par, cursor.pos);
826     
827         int asc = lyxfont::maxAscent(font);
828         int desc = lyxfont::maxDescent(font);
829         bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
830         bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
831         cursor_visible = true;
832     }
833 }
834
835
836 void InsetTabular::HideInsetCursor(BufferView * bv)
837 {
838     if (cursor_visible)
839         ToggleInsetCursor(bv);
840 }
841
842
843 void InsetTabular::setPos(BufferView * bv, int x, int y) const
844 {
845         cursor.y(0);
846         cursor.pos(0);
847         
848         actcell = actrow = actcol = 0;
849     int ly = tabular->GetDescentOfRow(actrow);
850
851     // first search the right row
852     while((ly < y) && (actrow < tabular->rows())) {
853         cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
854             tabular->GetAscentOfRow(actrow+1) +
855             tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
856                                                                 actcol)));
857         ++actrow;
858         ly = cursor.y() + tabular->GetDescentOfRow(actrow);
859     }
860     actcell = tabular->GetCellNumber(actrow, actcol);
861
862     // now search the right column
863     int lx = tabular->GetWidthOfColumn(actcell) -
864         tabular->GetAdditionalWidth(actcell);
865     for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
866         ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
867             tabular->GetAdditionalWidth(actcell - 1));
868     cursor.pos(((actcell+1) * 2) - 1);
869     resetPos(bv);
870     if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
871         cursor.x(lx + top_x - 2);
872     } else {
873         cursor.pos(cursor.pos() - 1);
874         cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
875     }
876     resetPos(bv);
877 }
878
879
880 int InsetTabular::getCellXPos(int cell) const
881 {
882     int c;
883
884     for(c=cell;!tabular->IsFirstCellInRow(c);--c)
885         ;
886     int lx = tabular->GetWidthOfColumn(cell);
887     for(; (c < cell); ++c) {
888         lx += tabular->GetWidthOfColumn(c);
889     }
890     return (lx - tabular->GetWidthOfColumn(cell) + top_x +
891             ADD_TO_TABULAR_WIDTH);
892 }
893
894
895 void InsetTabular::resetPos(BufferView * bv) const
896 {
897     if (!locked)
898         return;
899     actcol = tabular->column_of_cell(actcell);
900
901     int cell = 0;
902     actrow = 0;
903     cursor.y(0);
904     for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
905         if (tabular->IsLastCellInRow(cell)) {
906             cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
907                 tabular->GetAscentOfRow(actrow + 1) +
908                 tabular->GetAdditionalHeight(cell + 1));
909             ++actrow;
910         }
911     }
912     cursor.x(getCellXPos(actcell) + 2);
913     if (cursor.pos() % 2) {
914         LyXFont font(LyXFont::ALL_SANE);
915         cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
916                 tabular->GetBeginningOfTextInCell(actcell));
917     }
918     if ((!the_locking_inset ||
919          !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
920         (actcell != oldcell)) {
921         dialogs_ = bv->owner()->getDialogs();
922         dialogs_->updateTabular(const_cast<InsetTabular *>(this));
923         oldcell = actcell;
924     }
925 }
926
927
928 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
929 {
930     if (!cellstart(cursor.pos())) {
931         if (tabular->IsLastCell(actcell))
932             return FINISHED;
933         ++actcell;
934         cursor.pos(cursor.pos() + 1);
935     } else if (lock) {
936         if (ActivateCellInset(bv))
937             return DISPATCHED;
938     } else {              // before the inset
939         cursor.pos(cursor.pos() + 1);
940     }
941     resetPos(bv);
942     return DISPATCHED_NOUPDATE;
943 }
944
945
946 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
947 {
948     if (!cursor.pos()) {
949         if (!actcell)
950             return FINISHED;
951         cursor.pos(2);
952     }
953     cursor.pos(cursor.pos() - 1);
954     if (!cellstart(cursor.pos())) {
955         --actcell;
956     } else if (lock) {       // behind the inset
957         if (ActivateCellInset(bv, 0, 0, 0, true))
958             return DISPATCHED;
959     }
960     resetPos(bv);
961     return DISPATCHED_NOUPDATE;
962 }
963
964
965 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
966 {
967     int ocell = actcell;
968     actcell = tabular->GetCellAbove(actcell);
969     if (actcell == ocell) // we moved out of the inset
970         return FINISHED;
971     resetPos(bv);
972     return DISPATCHED_NOUPDATE;
973 }
974
975
976 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
977 {
978     int ocell = actcell;
979     actcell = tabular->GetCellBelow(actcell);
980     if (actcell == ocell) // we moved out of the inset
981         return FINISHED;
982     resetPos(bv);
983     return DISPATCHED_NOUPDATE;
984 }
985
986
987 bool InsetTabular::moveNextCell(BufferView * bv)
988 {
989     if (tabular->IsLastCell(actcell))
990         return false;
991     ++actcell;
992     cursor.pos(cursor.pos() + 1);
993     if (!cellstart(cursor.pos()))
994         cursor.pos(cursor.pos() + 1);
995     resetPos(bv);
996     return true;
997 }
998
999
1000 bool InsetTabular::movePrevCell(BufferView * bv)
1001 {
1002     if (!actcell) // first cell
1003         return false;
1004     --actcell;
1005     cursor.pos(cursor.pos() - 1);
1006     if (cellstart(cursor.pos()))
1007         cursor.pos(cursor.pos() - 1);
1008     resetPos(bv);
1009     return true;
1010 }
1011
1012
1013 bool InsetTabular::Delete()
1014 {
1015     return true;
1016 }
1017
1018
1019 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1020 {
1021     if (the_locking_inset)
1022         the_locking_inset->SetFont(bv, font, tall);
1023 }
1024
1025
1026 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1027 {
1028     int
1029         i, j,
1030         sel_col_start,
1031         sel_col_end,
1032         sel_row_start,
1033         sel_row_end,
1034         setLines = 0,
1035         setAlign = LYX_ALIGN_LEFT,
1036         lineSet;
1037     bool
1038         what;
1039
1040     switch (feature) {
1041       case LyXTabular::ALIGN_LEFT:
1042           setAlign=LYX_ALIGN_LEFT;
1043           break;
1044       case LyXTabular::ALIGN_RIGHT:
1045           setAlign=LYX_ALIGN_RIGHT;
1046           break;
1047       case LyXTabular::ALIGN_CENTER:
1048           setAlign=LYX_ALIGN_CENTER;
1049           break;
1050       default:
1051           break;
1052     }
1053     if (hasSelection()) {
1054         int tmp;
1055         sel_col_start = tabular->column_of_cell(sel_cell_start);
1056         sel_col_end = tabular->column_of_cell(sel_cell_end);
1057         if (sel_col_start > sel_col_end) {
1058             sel_col_end = sel_col_start;
1059             sel_col_start = tabular->column_of_cell(sel_cell_end);
1060         } else {
1061             sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1062         }
1063         
1064         sel_row_start = tabular->row_of_cell(sel_cell_start);
1065         sel_row_end = tabular->row_of_cell(sel_cell_end);
1066         if (sel_row_start > sel_row_end) {
1067             tmp = sel_row_start;
1068             sel_row_start = sel_row_end;
1069             sel_row_end = tmp;
1070         }
1071     } else {
1072         sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1073         sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1074     }
1075     bv->text->SetUndo(bv->buffer(), Undo::FINISH, 
1076               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1077               bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1078
1079     int row = tabular->row_of_cell(actcell);
1080     int column = tabular->column_of_cell(actcell);
1081
1082     switch (feature) {
1083     case LyXTabular::SET_PWIDTH:
1084     {
1085         bool update = (tabular->GetPWidth(actcell) != val);
1086         tabular->SetPWidth(actcell,val);
1087         if (update) {
1088             for (int i=0; i < tabular->rows(); ++i) {
1089                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1090                     deleteLyXText(bv);
1091             }
1092             UpdateLocal(bv, INIT, true);
1093         }
1094     }
1095     break;
1096     case LyXTabular::SET_SPECIAL_COLUMN:
1097     case LyXTabular::SET_SPECIAL_MULTI:
1098         tabular->SetAlignSpecial(actcell,val,feature);
1099         break;
1100     case LyXTabular::APPEND_ROW:
1101         // append the row into the tabular
1102         UnlockInsetInInset(bv, the_locking_inset);
1103         tabular->AppendRow(actcell);
1104         UpdateLocal(bv, INIT, true);
1105         break;
1106     case LyXTabular::APPEND_COLUMN:
1107         // append the column into the tabular
1108         tabular->AppendColumn(actcell);
1109         actcell = tabular->GetCellNumber(row, column);
1110         UpdateLocal(bv, INIT, true);
1111         break;
1112     case LyXTabular::DELETE_ROW:
1113         tabular->DeleteRow(tabular->row_of_cell(actcell));
1114         if ((row+1) > tabular->rows())
1115             --row;
1116         actcell = tabular->GetCellNumber(row, column);
1117         UpdateLocal(bv, INIT, true);
1118         break;
1119     case LyXTabular::DELETE_COLUMN:
1120         tabular->DeleteColumn(tabular->column_of_cell(actcell));
1121         if ((column+1) > tabular->columns())
1122             --column;
1123         actcell = tabular->GetCellNumber(row, column);
1124         UpdateLocal(bv, INIT, true);
1125         break;
1126     case LyXTabular::TOGGLE_LINE_TOP:
1127         lineSet = !tabular->TopLine(actcell);
1128         for(i=sel_row_start; i<=sel_row_end; ++i)
1129             for(j=sel_col_start; j<=sel_col_end; ++j)
1130                 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1131         UpdateLocal(bv, INIT, true);
1132         break;
1133     
1134     case LyXTabular::TOGGLE_LINE_BOTTOM:
1135         lineSet = !tabular->BottomLine(actcell); 
1136         for(i=sel_row_start; i<=sel_row_end; ++i)
1137             for(j=sel_col_start; j<=sel_col_end; ++j)
1138                 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1139         UpdateLocal(bv, INIT, true);
1140         break;
1141                 
1142     case LyXTabular::TOGGLE_LINE_LEFT:
1143         lineSet = !tabular->LeftLine(actcell);
1144         for(i=sel_row_start; i<=sel_row_end; ++i)
1145             for(j=sel_col_start; j<=sel_col_end; ++j)
1146                 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1147         UpdateLocal(bv, INIT, true);
1148         break;
1149
1150     case LyXTabular::TOGGLE_LINE_RIGHT:
1151         lineSet = !tabular->RightLine(actcell);
1152         for(i=sel_row_start; i<=sel_row_end; ++i)
1153             for(j=sel_col_start; j<=sel_col_end; ++j)
1154                 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1155         UpdateLocal(bv, INIT, true);
1156         break;
1157     case LyXTabular::ALIGN_LEFT:
1158     case LyXTabular::ALIGN_RIGHT:
1159     case LyXTabular::ALIGN_CENTER:
1160         for(i=sel_row_start; i<=sel_row_end; ++i)
1161             for(j=sel_col_start; j<=sel_col_end; ++j)
1162                 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1163         if (hasSelection())
1164             UpdateLocal(bv, INIT, true);
1165         else
1166             UpdateLocal(bv, CELL, true);
1167         break;
1168     case LyXTabular::MULTICOLUMN:
1169     {
1170         if (sel_row_start != sel_row_end) {
1171             WriteAlert(_("Impossible Operation!"), 
1172                        _("Multicolumns can only be horizontally."), 
1173                        _("Sorry."));
1174             return;
1175         }
1176         // just multicol for one Single Cell
1177         if (!hasSelection()) {
1178             // check wether we are completly in a multicol
1179             if (tabular->IsMultiColumn(actcell)) {
1180                 tabular->UnsetMultiColumn(actcell);
1181                 UpdateLocal(bv, INIT, true);
1182             } else {
1183                 tabular->SetMultiColumn(actcell, 1);
1184                 UpdateLocal(bv, CELL, true);
1185             }
1186             return;
1187         }
1188         // we have a selection so this means we just add all this
1189         // cells to form a multicolumn cell
1190         int
1191             s_start, s_end;
1192
1193         if (sel_cell_start > sel_cell_end) {
1194             s_start = sel_cell_end;
1195             s_end = sel_cell_start;
1196         } else {
1197             s_start = sel_cell_start;
1198             s_end = sel_cell_end;
1199         }
1200         tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1201         actcell = s_start;
1202         cursor.pos(0);
1203         sel_cell_end = sel_cell_start;
1204         sel_pos_end = sel_pos_start;
1205         UpdateLocal(bv, INIT, true);
1206         break;
1207     }
1208     case LyXTabular::SET_ALL_LINES:
1209         setLines = 1;
1210     case LyXTabular::UNSET_ALL_LINES:
1211         for(i=sel_row_start; i<=sel_row_end; ++i)
1212             for(j=sel_col_start; j<=sel_col_end; ++j)
1213                 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1214         UpdateLocal(bv, INIT, true);
1215         break;
1216     case LyXTabular::SET_LONGTABULAR:
1217         tabular->SetLongTabular(true);
1218         UpdateLocal(bv, INIT, true); // because this toggles displayed
1219         break;
1220     case LyXTabular::UNSET_LONGTABULAR:
1221         tabular->SetLongTabular(false);
1222         UpdateLocal(bv, INIT, true); // because this toggles displayed
1223         break;
1224     case LyXTabular::SET_ROTATE_TABULAR:
1225         tabular->SetRotateTabular(true);
1226         break;
1227     case LyXTabular::UNSET_ROTATE_TABULAR:
1228         tabular->SetRotateTabular(false);
1229         break;
1230     case LyXTabular::SET_ROTATE_CELL:
1231         for(i=sel_row_start; i<=sel_row_end; ++i)
1232             for(j=sel_col_start; j<=sel_col_end; ++j)
1233                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1234         break;
1235     case LyXTabular::UNSET_ROTATE_CELL:
1236         for(i=sel_row_start; i<=sel_row_end; ++i)
1237             for(j=sel_col_start; j<=sel_col_end; ++j)
1238                 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1239         break;
1240     case LyXTabular::SET_LINEBREAKS:
1241         what = !tabular->GetLinebreaks(actcell);
1242         for(i=sel_row_start; i<=sel_row_end; ++i)
1243             for(j=sel_col_start; j<=sel_col_end; ++j)
1244                 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1245         break;
1246     case LyXTabular::SET_LTFIRSTHEAD:
1247         tabular->SetLTHead(actcell,true);
1248         break;
1249     case LyXTabular::SET_LTHEAD:
1250         tabular->SetLTHead(actcell,false);
1251         break;
1252     case LyXTabular::SET_LTFOOT:
1253         tabular->SetLTFoot(actcell,false);
1254         break;
1255     case LyXTabular::SET_LTLASTFOOT:
1256         tabular->SetLTFoot(actcell,true);
1257         break;
1258     case LyXTabular::SET_LTNEWPAGE:
1259         what = !tabular->GetLTNewPage(actcell);
1260         tabular->SetLTNewPage(actcell,what);
1261         break;
1262     }
1263 }
1264
1265
1266 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1267                                      bool behind)
1268 {
1269     // the cursor.pos has to be before the inset so if it isn't now just
1270     // reset the curor pos first!
1271     if (cursor.pos() % 2) { // behind the inset
1272         cursor.pos(cursor.pos() - 1);
1273         resetPos(bv);
1274     }
1275     UpdatableInset * inset =
1276         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1277     LyXFont font(LyXFont::ALL_SANE);
1278     if (behind) {
1279         x = inset->x() + inset->width(bv, font);
1280         y = inset->descent(bv, font);
1281     }
1282     inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1283     inset_y = cursor.y();
1284     inset->Edit(bv, x - inset_x, y - inset_y, button);
1285     if (!the_locking_inset)
1286         return false;
1287     UpdateLocal(bv, CELL, false);
1288     return true;
1289 }
1290
1291
1292 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1293 {
1294     InsetText * inset = tabular->GetCellInset(actcell);
1295     int x1 = x + top_x;
1296
1297     if (cursor.pos() % 2) { // behind the inset
1298         return (((x + top_x) < cursor.x()) &&
1299                 ((x + top_x) > (cursor.x() - inset->width(bv,
1300                                                       LyXFont(LyXFont::ALL_SANE)))));
1301     } else {
1302         int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1303         return ((x1 > x2) &&
1304                 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1305     }
1306 }
1307
1308
1309 // This returns paperWidth() if the cell-width is unlimited or the width
1310 // in pixels if we have a pwidth for this cell.
1311 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1312 {
1313     string s = tabular->GetPWidth(cell);
1314
1315     if (s.empty())
1316         return -1;
1317     return VSpace(s).inPixels( 0, 0);
1318 }
1319
1320
1321 int InsetTabular::getMaxWidth(Painter & pain,
1322                               UpdatableInset const * inset) const
1323 {
1324     int cell;
1325     int n = tabular->GetNumberOfCells();
1326     for(cell=0; cell < n; ++cell) {
1327         if (tabular->GetCellInset(cell) == inset)
1328             break;
1329     }
1330     if (cell >= n)
1331         return -1;
1332     int w = GetMaxWidthOfCell(pain, cell);
1333     // this because text insets remove the xpos from the maxwidth because
1334     // otherwise the would not break good!!!
1335 //    w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1336 //    w += inset->x();
1337     return w;
1338 }
1339
1340
1341 void InsetTabular::recomputeTextInsets(BufferView * bv,
1342                                        LyXFont const & font) const
1343 {
1344     InsetText * inset;
1345     int cell;
1346
1347 //    cx = top_x;
1348     for(int j= 0; j < tabular->columns(); ++j) {
1349         for(int i = 0; i < tabular->rows(); ++i) {
1350             if (tabular->IsPartOfMultiColumn(i,j))
1351                 continue;
1352             cell = tabular->GetCellNumber(i,j);
1353             inset = tabular->GetCellInset(cell);
1354             inset->update(bv, font);
1355             tabular->SetWidthOfCell(cell, inset->width(bv, font));
1356         }
1357 //      cell = tabular->GetCellNumber(0, j);
1358 //      cx += tabular->GetWidthOfColumn(cell);
1359     }
1360 }
1361
1362
1363 void InsetTabular::resizeLyXText(BufferView *) const
1364 {
1365     need_update = FULL;
1366 }