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