]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Small fixes to compile with compaq cxx
[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
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 & pain, LyXFont const & font) const
135 {
136     if (init_inset) {
137         calculate_width_of_cells(pain, font);
138         init_inset = false;
139     }
140     return tabular->GetAscentOfRow(0);
141 }
142
143
144 int InsetTabular::descent(Painter & pain, LyXFont const & font) const
145 {
146     if (init_inset) {
147         calculate_width_of_cells(pain, font);
148         init_inset = false;
149     }
150     return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
151 }
152
153
154 int InsetTabular::width(Painter & pain, LyXFont const & font) const
155 {
156     if (init_inset) {
157         calculate_width_of_cells(pain, font);
158         init_inset = false;
159     }
160     return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
161 }
162
163
164 void InsetTabular::draw(Painter & pain, const LyXFont & font, int baseline,
165                         float & x) const
166 {
167     int i, j, cell=0;
168     int nx;
169     float cx;
170     bool reinit = false;
171
172     UpdatableInset::draw(pain,font,baseline,x);
173     if (init_inset || (top_x != int(x)) || (top_baseline != baseline)) {
174         int ox = top_x;
175         init_inset = false;
176         top_x = int(x);
177         top_baseline = baseline;
178         if (ox != top_x)
179             recomputeTextInsets(pain, font);
180         calculate_width_of_cells(pain, font);
181         resetPos(pain);
182         reinit = true;
183     }
184     x += ADD_TO_TABULAR_WIDTH;
185     for(i=0;i<tabular->rows();++i) {
186         nx = int(x);
187         for(j=0;j<tabular->columns();++j) {
188             if (tabular->IsPartOfMultiColumn(i,j))
189                 continue;
190             cx = nx + tabular->GetBeginningOfTextInCell(cell);
191             if (hasSelection())
192                 DrawCellSelection(pain, nx, baseline, i, j, cell);
193             tabular->GetCellInset(cell)->draw(pain, font, baseline, cx);
194             DrawCellLines(pain, nx, baseline, i, cell);
195             nx += tabular->GetWidthOfColumn(cell);
196             ++cell;
197         }
198         baseline += tabular->GetDescentOfRow(i) + tabular->GetAscentOfRow(i+1)
199             + tabular->GetAdditionalHeight(cell+1);
200     }
201     x += width(pain, font);
202 }
203
204
205 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
206                                  int row, int cell) const
207 {
208     int  x2 = x + tabular->GetWidthOfColumn(cell);
209     bool on_off;
210
211     if (!tabular->TopAlreadyDrawed(cell)) {
212         on_off = !tabular->TopLine(cell);
213         pain.line(x, baseline - tabular->GetAscentOfRow(row),
214                   x2, baseline -  tabular->GetAscentOfRow(row),
215                   on_off ? LColor::tabularonoffline:LColor::tabularline,
216                   on_off ? Painter::line_onoffdash:Painter::line_solid);
217     }
218     on_off = !tabular->BottomLine(cell);
219     pain.line(x,baseline +  tabular->GetDescentOfRow(row),
220               x2, baseline +  tabular->GetDescentOfRow(row),
221               on_off ? LColor::tabularonoffline:LColor::tabularline,
222               on_off ? Painter::line_onoffdash:Painter::line_solid);
223     if (!tabular->LeftAlreadyDrawed(cell)) {
224         on_off = !tabular->LeftLine(cell);
225         pain.line(x, baseline -  tabular->GetAscentOfRow(row),
226                   x, baseline +  tabular->GetDescentOfRow(row),
227                   on_off ? LColor::tabularonoffline:LColor::tabularline,
228                   on_off ? Painter::line_onoffdash:Painter::line_solid);
229     }
230     on_off = !tabular->RightLine(cell);
231     pain.line(x2 - tabular->GetAdditionalWidth(cell),
232               baseline -  tabular->GetAscentOfRow(row),
233               x2 - tabular->GetAdditionalWidth(cell),
234               baseline +  tabular->GetDescentOfRow(row),
235               on_off ? LColor::tabularonoffline:LColor::tabularline,
236               on_off ? Painter::line_onoffdash:Painter::line_solid);
237 }
238
239
240 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
241                                      int row, int column, int cell) const
242 {
243     int tmp;
244
245     int cs = tabular->column_of_cell(sel_cell_start);
246     int ce = tabular->column_of_cell(sel_cell_end);
247     if (cs > ce) {
248         ce = cs;
249         cs = tabular->column_of_cell(sel_cell_end);
250     } else {
251         ce = tabular->right_column_of_cell(sel_cell_end);
252     }
253
254     int rs = tabular->row_of_cell(sel_cell_start);
255     int re = tabular->row_of_cell(sel_cell_end);
256     if (rs > re) {
257         tmp = rs;
258         rs = re;
259         re = tmp;
260     }
261
262     if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
263         int w = tabular->GetWidthOfColumn(cell);
264         int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
265         pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
266                            w, h, LColor::selection);
267     }
268 }
269
270
271 char const * InsetTabular::EditMessage() const
272 {
273     return _("Opened Tabular Inset");
274 }
275
276
277 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
278 {
279     UpdatableInset::Edit(bv, x, y, button);
280
281     if (!bv->lockInset(this)) {
282         lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
283         return;
284     }
285     locked = true;
286     the_locking_inset = 0;
287     inset_pos = inset_x = inset_y = 0;
288     setPos(bv->painter(), x, y);
289     sel_pos_start = sel_pos_end = cursor.pos();
290     sel_cell_start = sel_cell_end = actcell;
291     bv->text->FinishUndo();
292     if (InsetHit(bv, x, y)) {
293         ActivateCellInset(bv, x, y, button);
294     }
295     UpdateLocal(bv, true);
296 //    bv->getOwner()->getPopups().updateFormTabular();
297 }
298
299
300 void InsetTabular::InsetUnlock(BufferView * bv)
301 {
302     TabularOptClose();
303     if (the_locking_inset) {
304         the_locking_inset->InsetUnlock(bv);
305         the_locking_inset = 0;
306     }
307     HideInsetCursor(bv);
308     if (hasSelection()) {
309         sel_pos_start = sel_pos_end = cursor.pos();
310         sel_cell_start = sel_cell_end = actcell;
311         UpdateLocal(bv, false);
312     }
313     no_selection = false;
314     oldcell = -1;
315     locked = false;
316 }
317
318 void InsetTabular::UpdateLocal(BufferView * bv, bool flag)
319 {
320     if (flag)
321         calculate_width_of_cells(bv->painter(), LyXFont(LyXFont::ALL_SANE));
322     bv->updateInset(this, flag);
323     if (flag)
324         resetPos(bv->painter());
325 }
326
327 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
328 {
329     lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
330     if (!inset)
331         return false;
332     oldcell = -1;
333     if (inset == tabular->GetCellInset(actcell)) {
334         lyxerr[Debug::INSETS] << "OK" << endl;
335         the_locking_inset = tabular->GetCellInset(actcell);
336         resetPos(bv->painter());
337         inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
338         inset_y = cursor.y();
339         inset_pos = cursor.pos();
340         return true;
341     } else if (the_locking_inset && (the_locking_inset == inset)) {
342         if (cursor.pos() == inset_pos) {
343             lyxerr[Debug::INSETS] << "OK" << endl;
344             resetPos(bv->painter());
345             inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
346             inset_y = cursor.y();
347         } else {
348             lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
349         }
350     } else if (the_locking_inset) {
351         lyxerr[Debug::INSETS] << "MAYBE" << endl;
352         return the_locking_inset->LockInsetInInset(bv, inset);
353     }
354     lyxerr[Debug::INSETS] << "NOT OK" << endl;
355     return false;
356 }
357
358 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
359                                    bool lr)
360 {
361     if (!the_locking_inset)
362         return false;
363     if (the_locking_inset == inset) {
364         the_locking_inset->InsetUnlock(bv);
365         the_locking_inset = 0;
366         if (lr)
367             moveRight(bv, false);
368         return true;
369     }
370     if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
371         if ((inset->LyxCode() == TABULAR_CODE) &&
372             !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
373         {
374             UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
375             oldcell = actcell;
376         }
377         return true;
378     }
379     return false;
380 }
381
382 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
383 {
384     if (!the_locking_inset)
385         return false;
386     if (the_locking_inset != inset)
387         return the_locking_inset->UpdateInsetInInset(bv, inset);
388     UpdateLocal(bv);
389     return true;
390 }
391
392
393 int InsetTabular::InsetInInsetY()
394 {
395     if (!the_locking_inset)
396         return 0;
397
398     return (inset_y + the_locking_inset->InsetInInsetY());
399 }
400
401
402 UpdatableInset * InsetTabular::GetLockingInset()
403 {
404     return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
405 }
406
407
408 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
409 {
410     if (c == LyxCode())
411         return this;
412     if (the_locking_inset)
413         return the_locking_inset->GetFirstLockingInsetOfType(c);
414     return 0;
415 }
416
417
418 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
419 {
420     if (the_locking_inset)
421         return the_locking_inset->InsertInset(bv, inset);
422     return false;
423 }
424
425
426 void InsetTabular::InsetButtonRelease(BufferView * bv,
427                                       int x, int y, int button)
428 {
429     if (button == 3) {
430         if (the_locking_inset) {
431             UpdatableInset * i;
432             if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
433                 i->InsetButtonRelease(bv, x, y, button);
434                 return;
435             }
436         }
437         MenuLayoutTabular(true, this);
438         return;
439     }
440     if (the_locking_inset) {
441         the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
442         return;
443     }
444     no_selection = false;
445 }
446
447
448 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
449 {
450     if (hasSelection()) {
451         sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
452         UpdateLocal(bv, false);
453     }
454     no_selection = false;
455
456     int ocell = actcell;
457
458     setPos(bv->painter(), x, y);
459     sel_pos_start = sel_pos_end = cursor.pos();
460     sel_cell_start = sel_cell_end = actcell;
461
462     bool inset_hit = InsetHit(bv, x, y);
463
464     if ((ocell == actcell) && the_locking_inset && inset_hit) {
465         the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
466         return;
467     } else if (the_locking_inset) {
468         the_locking_inset->InsetUnlock(bv);
469     }
470     the_locking_inset = 0;
471     if (inset_hit && bv->the_locking_inset) {
472         ActivateCellInset(bv, x, y, button);
473         the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
474     }
475     
476 #if 0
477     if (button == 3)
478         bview->getOwner()->getPopups().showFormTabular();
479     else if (ocell != actcell)
480         bview->getOwner()->getPopups().updateFormTabular();
481 #endif
482 }
483
484
485 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
486 {
487     if (the_locking_inset) {
488         the_locking_inset->InsetMotionNotify(bv, x - inset_x,
489                                              y - inset_y, button);
490         return;
491     }
492     if (!no_selection) {
493             // int ocell = actcell,
494             int old = sel_pos_end;
495
496         setPos(bv->painter(), x, y);
497         sel_pos_end = cursor.pos();
498         sel_cell_end = actcell;
499         if (old != sel_pos_end)
500             UpdateLocal(bv, false);
501 #if 0
502         if (ocell != actcell)
503             bview->getOwner()->getPopups().updateFormTabular();
504 #endif
505     }
506     no_selection = false;
507 }
508
509
510 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
511 {
512     if (the_locking_inset) {
513         the_locking_inset->InsetKeyPress(xke);
514         return;
515     }
516 }
517
518
519 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
520                                                    string const & arg)
521 {
522     UpdatableInset::RESULT 
523         result;
524
525     no_selection = false;
526     if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
527         || (result == DISPATCHED_NOUPDATE)) {
528
529         resetPos(bv->painter());
530         return result;
531     }
532     result=DISPATCHED;
533
534     if ((action < 0) && arg.empty())
535         return FINISHED;
536
537     if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
538         (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
539         cursor.x_fix(-1);
540     if (the_locking_inset) {
541         result=the_locking_inset->LocalDispatch(bv, action, arg);
542         if (result == DISPATCHED_NOUPDATE)
543             return result;
544         else if (result == DISPATCHED) {
545             bool upd = SetCellDimensions(bv->painter(), actcell, actrow);
546             the_locking_inset->ToggleInsetCursor(bv);
547             UpdateLocal(bv, upd);
548             the_locking_inset->ToggleInsetCursor(bv);
549             return result;
550         } else if (result == FINISHED) {
551             if ((action == LFUN_RIGHT) || (action == -1)) {
552                 cursor.pos(inset_pos + 1);
553                 resetPos(bv->painter());
554             }
555             sel_pos_start = sel_pos_end = cursor.pos();
556             sel_cell_start = sel_cell_end = actcell;
557             the_locking_inset=0;
558             result = DISPATCHED;
559             return result;
560         }
561     }
562
563     bool hs = hasSelection();
564     HideInsetCursor(bv);
565     switch (action) {
566         // Normal chars not handled here
567     case -1:
568         break;
569         // --- Cursor Movements ---------------------------------------------
570     case LFUN_RIGHTSEL:
571         if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
572             break;
573         moveRight(bv, false);
574         sel_pos_end = cursor.pos();
575         if (!cellstart(cursor.pos())) {
576             if (tabular->right_column_of_cell(sel_cell_start) >
577                 tabular->right_column_of_cell(actcell))
578                 sel_cell_end = actcell+1;
579             else
580                 sel_cell_end = actcell;
581         }
582         UpdateLocal(bv, false);
583         break;
584     case LFUN_RIGHT:
585         result = moveRight(bv);
586         sel_pos_start = sel_pos_end = cursor.pos();
587         sel_cell_start = sel_cell_end = actcell;
588         if (hs)
589             UpdateLocal(bv, false);
590         break;
591     case LFUN_LEFTSEL:
592         if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
593             break;
594         moveLeft(bv, false);
595         sel_pos_end = cursor.pos();
596         if (cellstart(cursor.pos())) {
597             if (tabular->column_of_cell(sel_cell_start) >=
598                 tabular->column_of_cell(actcell))
599                 sel_cell_end = actcell;
600             else
601                 sel_cell_end = actcell-1;
602         }
603         UpdateLocal(bv, false);
604         break;
605     case LFUN_LEFT:
606         result = moveLeft(bv);
607         sel_pos_start = sel_pos_end = cursor.pos();
608         sel_cell_start = sel_cell_end = actcell;
609         if (hs)
610             UpdateLocal(bv, false);
611         break;
612     case LFUN_DOWNSEL:
613     {
614         int ocell = actcell;
615         moveDown(bv);
616         sel_pos_end = cursor.pos();
617         if ((ocell == sel_cell_end) ||
618             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
619             sel_cell_end = tabular->GetCellBelow(sel_cell_end);
620         else
621             sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
622         UpdateLocal(bv, false);
623     }
624     break;
625     case LFUN_DOWN:
626         result= moveDown(bv);
627         sel_pos_start = sel_pos_end = cursor.pos();
628         sel_cell_start = sel_cell_end = actcell;
629         if (hs)
630             UpdateLocal(bv, false);
631         break;
632     case LFUN_UPSEL:
633     {
634         int ocell = actcell;
635         moveUp(bv);
636         sel_pos_end = cursor.pos();
637         if ((ocell == sel_cell_end) ||
638             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
639             sel_cell_end = tabular->GetCellAbove(sel_cell_end);
640         else
641             sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
642         UpdateLocal(bv, false);
643     }
644     break;
645     case LFUN_UP:
646         result= moveUp(bv);
647         sel_pos_start = sel_pos_end = cursor.pos();
648         sel_cell_start = sel_cell_end = actcell;
649         if (hs)
650             UpdateLocal(bv, false);
651         break;
652     case LFUN_BACKSPACE:
653         break;
654     case LFUN_DELETE:
655         break;
656     case LFUN_HOME:
657         break;
658     case LFUN_END:
659         break;
660     case LFUN_SHIFT_TAB:
661     case LFUN_TAB:
662         if (the_locking_inset) {
663             the_locking_inset->InsetUnlock(bv);
664         }
665         the_locking_inset = 0;
666         if (action == LFUN_TAB)
667             moveNextCell(bv);
668         else
669             movePrevCell(bv);
670         sel_pos_start = sel_pos_end = cursor.pos();
671         sel_cell_start = sel_cell_end = actcell;
672         if (hs)
673             UpdateLocal(bv, false);
674         break;
675     case LFUN_LAYOUT_TABLE:
676     {
677         int flag = (arg == "true");
678         MenuLayoutTabular(flag, this);
679     }
680     break;
681     default:
682         result = UNDISPATCHED;
683         break;
684     }
685     if (result!=FINISHED) {
686         if (!the_locking_inset) {
687 #if 0       
688             if (ocell != actcell)
689                 bview->getOwner()->getPopups().updateFormTabular();
690 #endif
691             ShowInsetCursor(bv);
692         }
693     } else
694         bv->unlockInset(this);
695     return result;
696 }
697
698
699 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
700 {
701     return tabular->Latex(buf, os, fragile, fp);
702 }
703
704
705 int InsetTabular::Ascii(Buffer const *, ostream &) const
706 {
707     return 0;
708 }
709
710 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
711 {
712     return 0;
713 }
714
715
716 int InsetTabular::DocBook(Buffer const *, ostream &) const
717 {
718     return 0;
719 }
720
721
722 void InsetTabular::Validate(LaTeXFeatures & features) const
723 {
724     tabular->Validate(features);
725 }
726
727
728 void InsetTabular::calculate_width_of_cells(Painter & pain,
729                                             LyXFont const & font) const
730 {
731     int cell = -1;
732     int maxAsc, maxDesc;
733     InsetText * inset;
734     
735     for(int i = 0; i < tabular->rows(); ++i) {
736         maxAsc = maxDesc = 0;
737         for(int j= 0; j < tabular->columns(); ++j) {
738             if (tabular->IsPartOfMultiColumn(i,j))
739                 continue;
740             ++cell;
741             inset = tabular->GetCellInset(cell);
742             maxAsc = max(maxAsc, inset->ascent(pain, font));
743             maxDesc = max(maxDesc, inset->descent(pain, font));
744             tabular->SetWidthOfCell(cell, inset->width(pain, 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(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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
1148             } else {
1149                 tabular->SetMultiColumn(actcell, 1);
1150                 UpdateLocal(bv, false);
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);
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);
1181         break;
1182     case LyXTabular::SET_LONGTABULAR:
1183         tabular->SetLongTabular(true);
1184         UpdateLocal(bv, true); // because this toggles displayed
1185         break;
1186     case LyXTabular::UNSET_LONGTABULAR:
1187         tabular->SetLongTabular(false);
1188         UpdateLocal(bv, 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);
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 w;
1278
1279     if ((w=tabular->GetPWidth(cell)).empty())
1280         return -1;
1281     return VSpace(w).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(Painter & pain, const LyXFont & font) const
1304 {
1305     InsetText * inset;
1306     int cx, 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->computeTextRows(pain);
1316             tabular->SetWidthOfCell(cell, inset->width(pain, font));
1317         }
1318         cell = tabular->GetCellNumber(0, j);
1319         cx += tabular->GetWidthOfColumn(cell);
1320     }
1321 }