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