]> git.lyx.org Git - features.git/blob - src/tabular.C
add a setDefaults to GUIRuntime + some const changes in tabular + some c_str to work...
[features.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "lyx_gui_misc.h"
27 #include "buffer.h"
28 #include "BufferView.h"
29 #include "Painter.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
35
36 using std::ostream;
37 using std::istream;
38 using std::getline;
39 using std::max;
40 using std::endl;
41 using std::vector;
42
43 static int const WIDTH_OF_LINE = 5;
44
45 extern BufferView * current_view;
46
47 /// Define a few methods for the inner structs
48
49 LyXTabular::cellstruct::cellstruct() 
50 {
51     cellno = 0;
52     width_of_cell = 0;
53     multicolumn = LyXTabular::CELL_NORMAL;
54     alignment = LYX_ALIGN_CENTER;
55     valignment = LYX_VALIGN_TOP;
56     top_line = false;
57     bottom_line = false;
58     left_line = false;
59     right_line = false;
60     usebox = BOX_NONE;
61     rotate = false;
62 }
63
64
65 LyXTabular::rowstruct::rowstruct() 
66 {
67     top_line = true;
68     bottom_line = false;
69     ascent_of_row = 0;
70     descent_of_row = 0;
71     newpage = false;
72 }
73
74
75 LyXTabular::columnstruct::columnstruct() 
76 {
77     left_line = true;
78     right_line = false;
79     alignment = LYX_ALIGN_CENTER;
80     valignment = LYX_VALIGN_TOP;
81     width_of_column = 0;
82 }
83
84
85 /* konstruktor */
86 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
87 {
88     owner_ = inset;
89     Init(rows_arg, columns_arg);
90 }
91
92
93 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
94 {
95     owner_ = inset;
96     Init(lt.rows_, lt.columns_);
97 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
98     operator=(lt);
99 }
100
101
102 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
103 {
104     owner_ = inset;
105     Read(buf, lex);
106 }
107
108
109 LyXTabular::~LyXTabular()
110 {
111     delete[] rowofcell;
112     delete[] columnofcell;
113 }
114
115
116 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
117 {
118     // If this and lt is not of the same size we have a serious bug
119     // So then it is ok to throw an exception, or for now
120     // call abort()
121     Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
122
123     cell_info = lt.cell_info;
124     row_info = lt.row_info;
125     column_info = lt.column_info;
126
127     // long tabular stuff
128     SetLongTabular(lt.is_long_tabular);
129     endhead = lt.endhead;
130     endfoot = lt.endfoot;
131     endfirsthead = lt.endfirsthead;
132     endlastfoot = lt.endlastfoot;
133
134     rotate = lt.rotate;
135
136     Reinit();
137     
138     return *this;
139 }
140
141
142 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
143 {
144     LyXTabular * result = new LyXTabular(inset, *this);
145     // don't know if this is good but I need to Clone also
146     // the text-insets here, this is for the Undo-facility!
147     for(int i = 0; i < rows_; ++i) {
148         for(int j = 0; j < columns_; ++j) {
149             result->cell_info[i][j].inset = cell_info[i][j].inset;
150             result->cell_info[i][j].inset.setOwner(inset);
151         }
152     }
153     return result;
154 }
155
156
157 /* activates all lines and sets all widths to 0 */ 
158 void LyXTabular::Init(int rows_arg, int columns_arg)
159 {
160
161     rows_ = rows_arg;
162     columns_ = columns_arg;
163     row_info = vector<rowstruct>(rows_, rowstruct());
164     column_info = vector<columnstruct>(columns_, columnstruct());
165     cell_info = vector<vector<cellstruct> >
166             (rows_, vector<cellstruct>(columns_, cellstruct()));
167
168     // Jürgen, use iterators.
169     int cellno = 0;
170     int i = 0;
171     for (; i < rows_; ++i) {
172         for (int j = 0; j < columns_; ++j) {
173             cell_info[i][j].inset.setOwner(owner_);
174             cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
175             cell_info[i][j].cellno = cellno++;
176         }
177     }
178     row_info[i - 1].bottom_line = true;
179     row_info[0].bottom_line = true;
180
181     for (i = 0; i < columns_; ++i) {
182         calculate_width_of_column(i);
183     }
184     column_info[columns_ - 1].right_line = true;
185    
186     calculate_width_of_tabular();
187
188     rowofcell = 0;
189     columnofcell = 0;
190     set_row_column_number_info();
191     is_long_tabular = false;
192     rotate = false;
193     endhead = 0;
194     endfirsthead = 0;
195     endfoot = 0;
196     endlastfoot = 0;
197 }
198
199
200 void LyXTabular::AppendRow(int cell )
201 {
202     ++rows_;
203    
204     int row = row_of_cell(cell);
205
206     row_vector::iterator rit = row_info.begin() + row;
207     row_info.insert(rit, rowstruct());
208
209 #if 0
210     cell_vvector::iterator cit = cell_info.begin() + row;
211     cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
212 #else
213     cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
214                                                           cellstruct()));
215
216     for(int i = 0; i <= row; ++i) {
217         for(int j = 0; j < columns_; ++j) {
218             c_info[i][j] = cell_info[i][j];
219         }
220     }
221     for(int i = row + 1; i < rows_; ++i) {
222         for(int j = 0; j < columns_; ++j) {
223             c_info[i][j] = cell_info[i-1][j];
224         }
225     }
226     cell_info = c_info;
227     ++row;
228     for (int j = 0; j < columns_; ++j) {
229         cell_info[row][j].inset.clear();
230     }
231 #endif
232     Reinit();
233 }
234
235
236 void LyXTabular::DeleteRow(int row)
237 {
238     if (!(rows_ - 1))
239         return;
240     row_info.erase(row_info.begin() + row); //&row_info[row]);
241     cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
242     --rows_;
243     Reinit();
244 }
245
246
247 void LyXTabular::AppendColumn(int cell)
248 {
249     ++columns_;
250    
251     cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
252                                                           cellstruct()));
253     int column = column_of_cell(cell);
254     column_vector::iterator cit = column_info.begin() + column + 1;
255     column_info.insert(cit, columnstruct());
256
257     for (int i = 0; i < rows_; ++i) {
258         for (int j = 0; j <= column; ++j) {
259             c_info[i][j] = cell_info[i][j];
260         }
261         for (int j = column + 1; j < columns_; ++j) {
262             c_info[i][j] = cell_info[i][j - 1];
263         }
264         // care about multicolumns
265         if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
266             cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
267         }
268         if ((column + 1) == columns_ ||
269             cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
270             cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
271         }
272     }
273     cell_info = c_info;
274     ++column;
275     for (int i = 0; i < rows_; ++i) {
276         cell_info[i][column].inset.clear();
277     }
278     Reinit();
279 }
280
281
282 void LyXTabular::DeleteColumn(int column)
283 {
284     if (!(columns_ - 1))
285         return;
286     column_info.erase(column_info.begin() + column);
287     for (int i = 0; i < rows_; ++i) {
288         cell_info[i].erase(cell_info[i].begin() + column);
289     }
290     --columns_;
291     Reinit();
292 }
293
294
295 void LyXTabular::Reinit()
296 {   
297     // Jürgen, use iterators.
298     for (int i = 0; i < rows_; ++i) {
299         for (int j = 0; j < columns_; ++j) {
300             cell_info[i][j].width_of_cell = 0;
301             cell_info[i][j].inset.setOwner(owner_);
302         }
303     }
304   
305     for (int i = 0; i < columns_; ++i) {
306         calculate_width_of_column(i);
307     }
308     calculate_width_of_tabular();
309
310     set_row_column_number_info();
311 }
312
313
314 void LyXTabular::set_row_column_number_info()
315 {
316     int c = 0;
317     int column = 0;
318     numberofcells = -1;
319     int row = 0;
320     for (; row < rows_; ++row) {
321         for (column = 0; column<columns_; ++column) {
322             if (cell_info[row][column].multicolumn
323                 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
324                 ++numberofcells;
325             cell_info[row][column].cellno = numberofcells;
326         }
327     }
328     ++numberofcells; // because this is one more than as we start from 0
329     row = 0;
330     column = 0;
331
332     delete [] rowofcell;
333     rowofcell = new int[numberofcells];
334     delete [] columnofcell;
335     columnofcell = new int[numberofcells];
336   
337     while (c < numberofcells && row < rows_ && column < columns_) {
338         rowofcell[c] = row;
339         columnofcell[c] = column;
340         ++c;
341         do {
342             ++column;
343         } while (column < columns_ &&
344                  cell_info[row][column].multicolumn
345                  == LyXTabular::CELL_PART_OF_MULTICOLUMN);
346         if (column == columns_) {
347             column = 0;
348             ++row;
349         }
350     }
351     for (row = 0; row < rows_; ++row) {
352         for (column = 0; column<columns_; ++column) {
353             if (IsPartOfMultiColumn(row,column))
354                 continue;
355             cell_info[row][column].inset.SetAutoBreakRows(
356                 !GetPWidth(GetCellNumber(row, column)).empty());
357         }
358     }
359 }
360
361
362 int LyXTabular::GetNumberOfCells() const
363 {
364     return numberofcells;
365 }
366
367
368 int LyXTabular::NumberOfCellsInRow(int cell) const
369 {
370     int row = row_of_cell(cell);
371     int result = 0;
372     for (int i = 0; i < columns_; ++i) {
373         if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
374             ++result;
375     }
376     return result;
377 }
378
379
380 /* returns 1 if there is a topline, returns 0 if not */ 
381 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
382 {
383     int row = row_of_cell(cell);
384     
385     if (!onlycolumn && IsMultiColumn(cell))
386         return cellinfo_of_cell(cell)->top_line;
387     return row_info[row].top_line;
388 }
389
390
391 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
392 {
393     //no bottom line underneath non-existent cells if you please
394     if(cell >= numberofcells)
395         return false;
396
397     if (!onlycolumn && IsMultiColumn(cell))
398         return cellinfo_of_cell(cell)->bottom_line;
399     return row_info[row_of_cell(cell)].bottom_line;
400 }
401
402
403 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
404 {
405     if (!onlycolumn && IsMultiColumn(cell))
406         return cellinfo_of_cell(cell)->left_line;
407     return column_info[column_of_cell(cell)].left_line;
408 }
409
410
411 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
412 {
413     if (!onlycolumn && IsMultiColumn(cell))
414         return cellinfo_of_cell(cell)->right_line;
415     return column_info[right_column_of_cell(cell)].right_line;
416 }
417
418
419 bool LyXTabular::TopAlreadyDrawed(int cell) const
420 {
421     if (GetAdditionalHeight(cell))
422         return false;
423     int row = row_of_cell(cell);
424     if (row > 0) {
425         int column = column_of_cell(cell);
426         --row;
427         while (column
428                && cell_info[row][column].multicolumn
429                == LyXTabular::CELL_PART_OF_MULTICOLUMN)
430             --column;
431         if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
432             return row_info[row].bottom_line;
433         else
434             return cell_info[row][column].bottom_line;
435     }
436     return false;
437 }
438
439
440 bool LyXTabular::LeftAlreadyDrawed(int cell) const
441 {
442     int column = column_of_cell(cell);
443     if (column > 0) {
444         int row = row_of_cell(cell);
445         while (--column &&
446                (cell_info[row][column].multicolumn ==
447                 LyXTabular::CELL_PART_OF_MULTICOLUMN));
448         if (GetAdditionalWidth(cell_info[row][column].cellno))
449             return false;
450         return column_info[column].right_line;
451     }
452     return false;
453 }
454
455
456 bool LyXTabular::IsLastRow(int cell) const
457 {
458     return (row_of_cell(cell) == rows_ - 1);
459 }
460
461
462 int LyXTabular::GetAdditionalHeight(int cell) const
463 {
464     int row = row_of_cell(cell);
465     if (!row) return 0;
466         
467     int top = 1; // bool top = true; ??
468     int bottom = 1; // bool bottom = true; ??
469
470     for (int column = 0; column < columns_ - 1 && bottom; ++column) {
471         switch (cell_info[row - 1][column].multicolumn) {
472         case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
473             bottom = cell_info[row - 1][column].bottom_line;
474             break;
475         case LyXTabular::CELL_NORMAL:
476             bottom = row_info[row - 1].bottom_line;
477         }
478     }
479     for (int column = 0; column < columns_ - 1 && top; ++column) {
480         switch (cell_info[row][column].multicolumn){
481         case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
482             top = cell_info[row][column].top_line;
483             break;
484         case LyXTabular::CELL_NORMAL:
485             top = row_info[row].top_line;
486         }
487     }
488     if (top && bottom)
489         return WIDTH_OF_LINE;
490     return 0;
491 }
492
493
494 int LyXTabular::GetAdditionalWidth(int cell) const
495 {
496     // internally already set in SetWidthOfCell
497     // used to get it back in text.C
498     int col = right_column_of_cell(cell);
499     if (col < columns_ - 1 && column_info[col].right_line &&
500         column_info[col+1].left_line)
501         return WIDTH_OF_LINE;
502     else
503         return 0;
504 }
505
506
507 // returns the maximum over all rows 
508 int LyXTabular::GetWidthOfColumn(int cell) const
509 {
510     int const column1 = column_of_cell(cell);
511     int const column2 = right_column_of_cell(cell);
512     int result = 0;
513     int i = column1;
514     for (; i <= column2; ++i) {
515         result += column_info[i].width_of_column;
516     }
517     return result;
518 }
519
520
521 int LyXTabular::GetWidthOfTabular() const
522 {
523     return width_of_tabular;
524 }
525
526
527 /* returns 1 if a complete update is necessary, otherwise 0 */ 
528 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
529 {
530     if (!IsMultiColumn(cell))
531         return false;
532     
533     int const row = row_of_cell(cell);
534     int const column1 = column_of_cell(cell);
535     int const column2 = right_column_of_cell(cell);
536
537     // first set columns to 0 so we can calculate the right width
538     int i = column1;
539     for (; i <= column2; ++i) {
540         cell_info[row][i].width_of_cell = 0;
541     }
542     // set the width to MAX_WIDTH until width > 0
543     int width = (new_width + 2 * WIDTH_OF_LINE);
544     for (i = column1; (i < column2) && (width>column_info[i].width_of_column);
545          ++i)
546     {
547         cell_info[row][i].width_of_cell = column_info[i].width_of_column;
548         width -= column_info[i].width_of_column;
549     }
550     if (width > 0) {
551         cell_info[row][i].width_of_cell = width;
552     }
553     return true;
554 }
555
556
557 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
558 {
559     int const row = row_of_cell(cell);
560     int const column1 = column_of_cell(cell);
561     int const column2 = right_column_of_cell(cell);
562
563     // first set columns to 0 so we can calculate the right width
564     int i = column1;
565     for (; i <= column2; ++i)
566         cell_info[row][i].width_of_cell = 0;
567     for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
568         ;
569     if (i < numberofcells)
570         recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
571     SetWidthOfMulticolCell(cell, new_width);
572 }
573
574
575 /* returns 1 if a complete update is necessary, otherwise 0 */ 
576 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
577 {
578     int const row = row_of_cell(cell);
579     int const column1 = column_of_cell(cell);
580     bool tmp = false;
581     int width = 0;
582
583     if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
584         return false;
585     if (IsMultiColumn(cell, true)) {
586         tmp = SetWidthOfMulticolCell(cell, new_width);
587     } else {
588         width = (new_width + 2*WIDTH_OF_LINE);
589         cell_info[row][column1].width_of_cell = width;
590         if (column_info[column1].right_line && (column1 < columns_-1) &&
591             column_info[column1+1].left_line) // additional width
592             cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
593         tmp = calculate_width_of_column_NMC(column1);
594     }
595     if (tmp) {
596         int i = 0;
597         for(; i<columns_; ++i)
598             calculate_width_of_column_NMC(i);
599         for(i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
600             ;
601         if (i < numberofcells)
602             recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
603         for(i = 0; i < columns_; ++i)
604             calculate_width_of_column(i);
605         calculate_width_of_tabular();
606         return true;
607     }
608     return false;
609 }
610
611
612 bool LyXTabular::SetAlignment(int cell, char align, bool onlycolumn)
613 {
614     if (!IsMultiColumn(cell) || onlycolumn)
615         column_info[column_of_cell(cell)].alignment = align;
616     if (!onlycolumn)
617         cellinfo_of_cell(cell)->alignment = align;
618     return true;
619 }
620
621
622 bool LyXTabular::SetVAlignment(int cell, char align, bool onlycolumn)
623 {
624     if (!IsMultiColumn(cell) || onlycolumn)
625         column_info[column_of_cell(cell)].valignment = align;
626     if (!onlycolumn)
627         cellinfo_of_cell(cell)->valignment = align;
628     return true;
629 }
630
631
632 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
633 {
634     bool flag = !width.empty();
635     int const j = column_of_cell(cell);
636
637     column_info[j].p_width = width;
638     if (flag) // do this only if there is a width
639         SetAlignment(cell, LYX_ALIGN_LEFT);
640     for(int i = 0; i < rows_; ++i) {
641         int c = GetCellNumber(i, j);
642         flag = !GetPWidth(c).empty(); // because of multicolumns!
643         GetCellInset(c)->SetAutoBreakRows(flag);
644     }
645     return true;
646 }
647
648
649 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
650 {
651     bool const flag = !width.empty();
652
653     cellinfo_of_cell(cell)->p_width = width;
654     if (IsMultiColumn(cell)) {
655         GetCellInset(cell)->SetAutoBreakRows(flag);
656         return true;
657     }
658     return false;
659 }
660
661
662 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
663 {
664     if (what == SET_SPECIAL_MULTI)
665         cellinfo_of_cell(cell)->align_special = special;
666     else
667         column_info[column_of_cell(cell)].align_special = special;
668     return true;
669 }
670
671
672 bool LyXTabular::SetAllLines(int cell, bool line)
673 {
674     SetTopLine(cell, line);
675     SetBottomLine(cell, line);
676     SetRightLine(cell, line);
677     SetLeftLine(cell, line);
678     return true;
679 }
680
681
682 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
683 {
684     int const row = row_of_cell(cell);
685
686     if (onlycolumn || !IsMultiColumn(cell))
687         row_info[row].top_line = line;
688     else
689         cellinfo_of_cell(cell)->top_line = line;
690     return true;
691 }
692
693
694 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
695 {
696     if (onlycolumn || !IsMultiColumn(cell))
697         row_info[row_of_cell(cell)].bottom_line = line;
698     else
699         cellinfo_of_cell(cell)->bottom_line = line;
700     return true;
701 }
702
703
704 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
705 {
706     if (onlycolumn || !IsMultiColumn(cell))
707         column_info[column_of_cell(cell)].left_line = line;
708     else
709         cellinfo_of_cell(cell)->left_line = line;
710     return true;
711 }
712
713
714 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
715 {
716     if (onlycolumn || !IsMultiColumn(cell))
717         column_info[right_column_of_cell(cell)].right_line = line;
718     else
719         cellinfo_of_cell(cell)->right_line = line;
720     return true;
721 }
722
723
724 char LyXTabular::GetAlignment(int cell, bool onlycolumn) const
725 {
726     if (!onlycolumn && IsMultiColumn(cell))
727         return cellinfo_of_cell(cell)->alignment;
728     else
729         return column_info[column_of_cell(cell)].alignment;
730 }
731
732
733 char LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
734 {
735     if (!onlycolumn && IsMultiColumn(cell))
736         return cellinfo_of_cell(cell)->valignment;
737     else
738         return column_info[column_of_cell(cell)].valignment;
739 }
740
741
742 string const LyXTabular::GetPWidth(int cell) const
743 {
744     if (IsMultiColumn(cell))
745         return cellinfo_of_cell(cell)->p_width;
746     return column_info[column_of_cell(cell)].p_width;
747 }
748
749
750 string const LyXTabular::GetColumnPWidth(int cell) const
751 {
752     return column_info[column_of_cell(cell)].p_width;
753 }
754
755
756 string const LyXTabular::GetMColumnPWidth(int cell) const
757 {
758     if (IsMultiColumn(cell))
759         return cellinfo_of_cell(cell)->p_width;
760     return string();
761 }
762
763
764 string const LyXTabular::GetAlignSpecial(int cell, int what) const
765 {
766     if (what == SET_SPECIAL_MULTI)
767         return cellinfo_of_cell(cell)->align_special;
768     return column_info[column_of_cell(cell)].align_special;
769 }
770
771
772 int LyXTabular::GetWidthOfCell(int cell) const
773 {
774     int const row = row_of_cell(cell);
775     int const column1 = column_of_cell(cell);
776     int const column2 = right_column_of_cell(cell);
777     int result = 0;
778     for (int i = column1; i <= column2; ++i) {
779         result += cell_info[row][i].width_of_cell;
780     }
781     return result;
782 }
783
784 int LyXTabular::GetBeginningOfTextInCell(int cell) const
785 {
786     int x = 0;
787    
788     switch (GetAlignment(cell)){
789     case LYX_ALIGN_CENTER:
790         x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
791         break;
792     case LYX_ALIGN_RIGHT:
793         x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
794         // + GetAdditionalWidth(cell);
795         break;
796     default: /* LYX_ALIGN_LEFT: nothing :-) */ 
797         break;
798     }
799     
800     // the LaTeX Way :-(
801     x += WIDTH_OF_LINE;
802     return x;
803 }
804
805
806 bool LyXTabular::IsFirstCellInRow(int cell) const
807 {
808     return (column_of_cell(cell) == 0);
809 }
810
811
812 int LyXTabular::GetFirstCellInRow(int row) const
813 {
814     if (row > (rows_-1))
815         row = rows_ - 1;
816     return cell_info[row][0].cellno;
817 }
818
819 bool LyXTabular::IsLastCellInRow(int cell) const
820 {
821     return (right_column_of_cell(cell) == (columns_ - 1));
822 }
823
824
825 int LyXTabular::GetLastCellInRow(int row) const
826 {
827     if (row > (rows_-1))
828         row = rows_ - 1;
829     return cell_info[row][columns_-1].cellno;
830 }
831
832
833 bool LyXTabular::calculate_width_of_column(int column)
834 {
835     int const old_column_width = column_info[column].width_of_column;
836     int maximum = 0;
837     
838     for (int i = 0; i < rows_; ++i) {
839         maximum = max(cell_info[i][column].width_of_cell, maximum);
840     }
841     column_info[column].width_of_column = maximum;
842     return (column_info[column].width_of_column != old_column_width);
843 }
844
845
846 //
847 // calculate the with of the column without regarding REAL MultiColumn
848 // cells. This means MultiColumn-cells spanning more than 1 column.
849 //
850 bool LyXTabular::calculate_width_of_column_NMC(int column)
851 {
852     int const old_column_width = column_info[column].width_of_column;
853     int max = 0;
854     for (int i = 0; i < rows_; ++i) {
855         if (!IsMultiColumn(GetCellNumber(i, column), true) &&
856             (cell_info[i][column].width_of_cell > max)) {
857             max = cell_info[i][column].width_of_cell;
858         }
859     }
860     column_info[column].width_of_column = max;
861     return (column_info[column].width_of_column != old_column_width);
862 }
863
864
865 void LyXTabular::calculate_width_of_tabular()
866 {
867     width_of_tabular = 0;
868     for (int i = 0; i < columns_; ++i) {
869         width_of_tabular += column_info[i].width_of_column;
870     }
871 }
872
873
874 int LyXTabular::row_of_cell(int cell) const
875 {
876     if (cell >= numberofcells)
877         return rows_-1;
878     else if (cell < 0)
879         return 0;
880     return rowofcell[cell];
881 }
882
883
884 int LyXTabular::column_of_cell(int cell) const
885 {
886     if (cell >= numberofcells)
887         return columns_-1;
888     else if (cell < 0)
889         return 0;
890     return columnofcell[cell];
891 }
892
893
894 int LyXTabular::right_column_of_cell(int cell) const
895 {
896     int const row = row_of_cell(cell);
897     int column = column_of_cell(cell);
898     while (column < (columns_ - 1) &&
899            cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
900         ++column;
901     return column;
902 }
903
904
905 void LyXTabular::Write(Buffer const * buf, ostream & os) const
906 {
907     // header line
908     os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
909         ">" << endl;
910     // global longtable options
911     os << "<Features rotate=" << rotate <<
912         " islongtable=" << is_long_tabular <<
913         " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
914         " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
915         ">" << endl << endl;
916     for (int i = 0; i < rows_; ++i) {
917         os << "<Row topline=" << row_info[i].top_line <<
918             " bottomline=" << row_info[i].bottom_line <<
919             " newpage=" << row_info[i].newpage <<
920             ">" << endl;
921         for (int j = 0; j < columns_; ++j) {
922             if (!i) {
923                 os << "<Column alignment=" << column_info[j].alignment <<
924                     " valignment=" << column_info[j].valignment <<
925                     " leftline=" << column_info[j].left_line <<
926                     " rightline=" << column_info[j].right_line <<
927                     " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
928                     "\" special=\"" << column_info[j].align_special <<
929                     "\">" << endl;
930             } else {
931                 os << "<Column>" << endl;
932             }
933             os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
934                 " alignment=" << cell_info[i][j].alignment <<
935                 " valignment=" << cell_info[i][j].valignment <<
936                 " topline=" << cell_info[i][j].top_line <<
937                 " bottomline=" << cell_info[i][j].bottom_line <<
938                 " leftline=" << cell_info[i][j].left_line <<
939                 " rightline=" << cell_info[i][j].right_line <<
940                 " rotate=" << cell_info[i][j].rotate <<
941                 " usebox=" << (int)cell_info[i][j].usebox <<
942                 " width=\"" << cell_info[i][j].p_width <<
943                 "\" special=\"" << cell_info[i][j].align_special <<
944                 "\">" << endl;
945             os << "\\begin_inset ";
946             cell_info[i][j].inset.Write(buf, os);
947             os << "\n\\end_inset " << endl;
948             os << "</Cell>" << endl;
949             os << "</Column>" << endl;
950         }
951         os << "</Row>" << endl;
952     }
953     os << "</LyXTabular>" << endl;
954 }
955
956
957 static
958 bool getTokenValue(string const str, const char * token, string & ret)
959 {
960     int pos = str.find(token);
961     char ch = str[pos+strlen(token)];
962
963     if ((pos < 0) || (ch != '='))
964         return false;
965     ret.erase();
966     pos += strlen(token)+1;
967     ch = str[pos];
968     if ((ch != '"') && (ch != '\'')) { // only read till next space
969         ret += ch;
970         ch = ' ';
971     }
972     while((pos < int(str.length()-1)) && (str[++pos] != ch))
973         ret += str[pos];
974
975     return true;
976 }
977
978
979 static
980 bool getTokenValue(string const str, const char * token, int & num)
981 {
982     string ret;
983     int pos = str.find(token);
984     char ch = str[pos+strlen(token)];
985
986     if ((pos < 0) || (ch != '='))
987         return false;
988     ret.erase();
989     pos += strlen(token)+1;
990     ch = str[pos];
991     if ((ch != '"') && (ch != '\'')) { // only read till next space
992         if (!isdigit(ch))
993             return false;
994         ret += ch;
995     }
996     ++pos;
997     while((pos < int(str.length()-1)) && isdigit(str[pos]))
998         ret += str[pos++];
999
1000     num = strToInt(ret);
1001     return true;
1002 }
1003
1004
1005 static
1006 bool getTokenValue(string const str, const char * token, bool & flag)
1007 {
1008     string ret;
1009     int pos = str.find(token);
1010     char ch = str[pos+strlen(token)];
1011
1012     if ((pos < 0) || (ch != '='))
1013         return false;
1014     ret.erase();
1015     pos += strlen(token)+1;
1016     ch = str[pos];
1017     if ((ch != '"') && (ch != '\'')) { // only read till next space
1018         if (!isdigit(ch))
1019             return false;
1020         ret += ch;
1021     }
1022     ++pos;
1023     while((pos < int(str.length()-1)) && isdigit(str[pos]))
1024         ret += str[pos++];
1025
1026     flag = strToInt(ret);
1027     return true;
1028 }
1029
1030
1031 static inline
1032 void l_getline(istream & is, string & str)
1033 {
1034     getline(is, str);
1035     while(str.empty())
1036         getline(is, str);
1037 }
1038
1039
1040 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1041 {
1042     string line;
1043     istream & is = lex.getStream();
1044
1045     l_getline(is, line);
1046     if (!prefixIs(line, "<LyXTabular ")) {
1047         OldFormatRead(lex, line);
1048         return;
1049     }
1050
1051     int version;
1052     int rows_arg;
1053     int columns_arg;
1054     if (!getTokenValue(line, "version", version))
1055         return;
1056     if (!getTokenValue(line, "rows", rows_arg))
1057         return;
1058     if (!getTokenValue(line, "columns", columns_arg))
1059         return;
1060     Init(rows_arg, columns_arg);
1061     l_getline(is, line);
1062     if (!prefixIs(line, "<Features ")) {
1063         lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1064             line << ")" << endl;
1065         return;
1066     }
1067     getTokenValue(line, "islongtable", is_long_tabular);
1068     getTokenValue(line, "endhead", endhead);
1069     getTokenValue(line, "endfirsthead", endfirsthead);
1070     getTokenValue(line, "endfoot", endfoot);
1071     getTokenValue(line, "endlastfoot", endlastfoot);
1072     int i, j;
1073     for(i = 0; i < rows_; ++i) {
1074         l_getline(is, line);
1075         if (!prefixIs(line, "<Row ")) {
1076             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1077                 line << ")" << endl;
1078             return;
1079         }
1080         getTokenValue(line, "topline", row_info[i].top_line);
1081         getTokenValue(line, "bottomline", row_info[i].bottom_line);
1082         getTokenValue(line, "newpage", row_info[i].newpage);
1083         for (j = 0; j < columns_; ++j) {
1084             l_getline(is,line);
1085             if (!prefixIs(line,"<Column")) {
1086                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1087                     line << ")" << endl;
1088                 return;
1089             }
1090             if (!i) {
1091                 getTokenValue(line, "alignment", column_info[j].alignment);
1092                 getTokenValue(line, "valignment", column_info[j].valignment);
1093                 getTokenValue(line, "leftline", column_info[j].left_line);
1094                 getTokenValue(line, "rightline", column_info[j].right_line);
1095                 getTokenValue(line, "width", column_info[j].p_width);
1096                 getTokenValue(line, "special", column_info[j].align_special);
1097             }
1098             l_getline(is, line);
1099             if (!prefixIs(line, "<Cell")) {
1100                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1101                     line << ")" << endl;
1102                 return;
1103             }
1104             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1105             getTokenValue(line, "alignment", cell_info[i][j].alignment);
1106             getTokenValue(line, "valignment", cell_info[i][j].valignment);
1107             getTokenValue(line, "topline", cell_info[i][j].top_line);
1108             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1109             getTokenValue(line, "leftline", cell_info[i][j].left_line);
1110             getTokenValue(line, "rightline", cell_info[i][j].right_line);
1111             getTokenValue(line, "rotate", cell_info[i][j].rotate);
1112             getTokenValue(line, "usebox", cell_info[i][j].usebox);
1113             getTokenValue(line, "width", cell_info[i][j].p_width);
1114             getTokenValue(line, "special", cell_info[i][j].align_special);
1115             l_getline(is, line);
1116             if (prefixIs(line, "\\begin_inset")) {
1117                 cell_info[i][j].inset.Read(buf, lex);
1118                 l_getline(is, line);
1119             }
1120             if (line != "</Cell>") {
1121                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1122                     line << ")" << endl;
1123                 return;
1124             }
1125             l_getline(is, line);
1126             if (line != "</Column>") {
1127                 lyxerr << "Wrong tabular format (expected </Column> got" <<
1128                     line << ")" << endl;
1129                 return;
1130             }
1131         }
1132         l_getline(is, line);
1133         if (line != "</Row>") {
1134             lyxerr << "Wrong tabular format (expected </Row> got" <<
1135                 line << ")" << endl;
1136             return;
1137         }
1138     }
1139     while (line != "</LyXTabular>") {
1140         l_getline(is, line);
1141     }
1142     set_row_column_number_info();
1143 }
1144
1145
1146 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1147 {
1148     int version;
1149     int i, j;
1150     int rows_arg = 0;
1151     int columns_arg = 0;
1152     int is_long_tabular_arg = false;
1153     int rotate_arg = false;
1154     int a = -1;
1155     int b = -1;
1156     int c = -1;
1157     int d = -1;
1158     int e = 0;
1159     int f = 0;
1160     int g = 0;
1161     int h = 0;
1162         
1163     istream & is = lex.getStream();
1164     string s(fl);
1165     if (s.length() > 8)
1166         version = atoi(s.c_str() + 8);
1167     else
1168         version = 1;
1169
1170     vector<int> cont_row_info;
1171
1172     if (version < 5) {
1173         lyxerr << "Tabular format < 5 is not supported anymore\n"
1174             "Get an older version of LyX (< 1.1.x) for conversion!"
1175                << endl;
1176         WriteAlert(_("Warning:"),
1177                    _("Tabular format < 5 is not supported anymore\n"),
1178                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1179         if (version > 2) {
1180             is >> rows_arg >> columns_arg >> is_long_tabular_arg
1181                >> rotate_arg >> a >> b >> c >> d;
1182         } else
1183             is >> rows_arg >> columns_arg;
1184         Init(rows_arg, columns_arg);
1185         cont_row_info = vector<int>(rows_arg);
1186         SetLongTabular(is_long_tabular_arg);
1187         SetRotateTabular(rotate_arg);
1188         string tmp;
1189         for (i = 0; i < rows_; ++i) {
1190             getline(is, tmp);
1191             cont_row_info[i] = false;
1192         }
1193         for (i = 0; i < columns_; ++i) {
1194             getline(is, tmp);
1195         }
1196         for (i = 0; i < rows_; ++i) {
1197             for (j = 0; j < columns_; ++j) {
1198                 getline(is, tmp);
1199             }
1200         }
1201     } else {
1202         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1203            >> rotate_arg >> a >> b >> c >> d;
1204         Init(rows_arg, columns_arg);
1205         cont_row_info = vector<int>(rows_arg);
1206         SetLongTabular(is_long_tabular_arg);
1207         SetRotateTabular(rotate_arg);
1208         endhead = a;
1209         endfirsthead = b;
1210         endfoot = c;
1211         endlastfoot = d;
1212         for (i = 0; i < rows_; ++i) {
1213             a = b = c = d = e = f = g = h = 0;
1214             is >> a >> b >> c >> d;
1215             row_info[i].top_line = a;
1216             row_info[i].bottom_line = b;
1217             cont_row_info[i] = c;
1218             row_info[i].newpage = d;
1219         }
1220         for (i = 0; i < columns_; ++i) {
1221             string s1;
1222             string s2;
1223             is >> a >> b >> c;
1224             char ch; // skip '"'
1225             is >> ch;
1226             getline(is, s1, '"');
1227             is >> ch; // skip '"'
1228             getline(is, s2, '"');
1229             column_info[i].alignment = static_cast<char>(a);
1230             column_info[i].left_line = b;
1231             column_info[i].right_line = c;
1232             column_info[i].p_width = s1;
1233             column_info[i].align_special = s2;
1234         }
1235         for (i = 0; i < rows_; ++i) {
1236             for (j = 0; j < columns_; ++j) {
1237                 string s1;
1238                 string s2;
1239                 is >> a >> b >> c >> d >> e >> f >> g;
1240                 char ch;
1241                 is >> ch; // skip '"'
1242                 getline(is, s1, '"');
1243                 is >> ch; // skip '"'
1244                 getline(is, s2, '"');
1245                 cell_info[i][j].multicolumn = static_cast<char>(a);
1246                 cell_info[i][j].alignment = static_cast<char>(b);
1247                 cell_info[i][j].top_line = static_cast<char>(c);
1248                 cell_info[i][j].bottom_line = static_cast<char>(d);
1249                 cell_info[i][j].rotate = static_cast<bool>(f);
1250                 cell_info[i][j].usebox = static_cast<bool>(g);
1251                 cell_info[i][j].align_special = s1;
1252                 cell_info[i][j].p_width = s2;
1253             }
1254         }
1255     }
1256     set_row_column_number_info();
1257
1258     LyXParagraph * par = new LyXParagraph;
1259     LyXParagraph * return_par = 0;
1260 #ifndef NEW_INSETS
1261     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1262     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1263 #endif
1264     string token, tmptok;
1265     int pos = 0;
1266     char depth = 0;
1267     LyXFont font(LyXFont::ALL_SANE);
1268
1269     while (lex.IsOK()) {
1270         lex.nextToken();
1271         token = lex.GetString();
1272         if (token.empty())
1273             continue;
1274         if ((token == "\\layout") || (token == "\\end_float") ||
1275             (token == "\\end_deeper"))
1276         {
1277             lex.pushToken(token);
1278             break;
1279         }
1280         if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1281                                                               return_par,
1282                                                               token, pos,
1283                                                               depth, font
1284 #ifndef NEW_INSETS
1285                                                               ,
1286                                                               footnoteflag,
1287                                                               footnotekind
1288 #endif
1289                 ))
1290         {
1291             // the_end read
1292             lex.pushToken(token);
1293             break;
1294         }
1295         if (return_par) {
1296             lex.printError("New Paragraph allocated! This should not happen!");
1297             lex.pushToken(token);
1298             delete par;
1299             par = return_par;
1300             break;
1301         }
1302     }
1303     // now we have the par we should fill the insets with this!
1304     int cell = 0;
1305     InsetText * inset = GetCellInset(cell);
1306     int row;
1307
1308     for(int i = 0; i < par->Last(); ++i) {
1309         if (par->IsNewline(i)) {
1310             ++cell;
1311             if (cell > GetNumberOfCells()) {
1312                 lyxerr << "Some error in reading old table format occured!" <<
1313                     endl << "Terminating when reading cell[" << cell << "]!" <<
1314                     endl;
1315                 return;
1316             }
1317             row = row_of_cell(cell);
1318             if (cont_row_info[row]) {
1319                 DeleteRow(row);
1320                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1321                 while(!IsFirstCellInRow(--cell));
1322             } else {
1323                 inset = GetCellInset(cell);
1324                 continue;
1325             }
1326             inset = GetCellInset(cell);
1327             row = row_of_cell(cell);
1328             if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1329             {
1330                 // insert a space instead
1331                 par->Erase(i);
1332                 par->InsertChar(i, ' ');
1333             }
1334         }
1335         par->CopyIntoMinibuffer(current_view->buffer()->params, i);
1336         inset->par->InsertFromMinibuffer(inset->par->Last());
1337     }
1338     Reinit();
1339 }
1340
1341
1342 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1343 {
1344     int i = isColumn ? cell : column_of_cell(cell);
1345         
1346     if (!isColumn && IsMultiColumn(cell)) {
1347        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1348            return cellinfo_of_cell(cell)->align_special;
1349        } else {
1350            switch (GetAlignment(cell)) {
1351            case LYX_ALIGN_LEFT:
1352                return "left";
1353            case LYX_ALIGN_RIGHT:
1354                return "right";
1355            default:
1356                return "center";
1357            }
1358        }
1359     } else {
1360        if (!column_info[i].align_special.empty()) {
1361            return column_info[i].align_special;
1362        }
1363 #ifdef IGNORE_THIS_FOR_NOW
1364        else if (!column_info[i].p_width.empty()) {
1365            file += "p{";
1366            file += column_info[i].p_width;
1367            file += '}';
1368        }
1369 #endif
1370        else {
1371            switch (column_info[i].alignment) {
1372            case LYX_ALIGN_LEFT:
1373                return "left";
1374            case LYX_ALIGN_RIGHT:
1375                return "right";
1376            default:
1377                return "center";
1378            }
1379        }
1380     }
1381 }
1382
1383
1384 // cell <0 will tex the preamble
1385 // returns the number of printed newlines
1386 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1387 {
1388     int ret = 0;
1389     int nvcell;
1390     if (IsLastCell(cell)) {
1391             os << newlineAndDepth(--depth)
1392                << "</ENTRY>"
1393                << newlineAndDepth(--depth)
1394                << "</ROW>"
1395                << newlineAndDepth(--depth)
1396                << "</TBODY>"
1397                << newlineAndDepth(--depth);
1398         if (is_long_tabular)
1399                 os << "</TGROUP>";
1400         else
1401                 os << "</TGROUP>"
1402                    << newlineAndDepth(--depth);
1403         ret += 4;
1404     } else {
1405         nvcell = cell + 1;
1406         if (cell < 0) {
1407             // preamble
1408             if (is_long_tabular)
1409                     os << "<TGROUP ";
1410             else
1411                     os << "<TGROUP ";
1412             os << "COLS='"
1413                << columns_
1414                << "' COLSEP='1' ROWSEP='1'>"
1415                << newlineAndDepth(++depth);
1416             ++ret;
1417             for (int i = 0; i < columns_; ++i) {
1418                     os << "<COLSPEC ALIGN='"
1419                        << GetDocBookAlign(i, true)
1420                        << "' COLNAME='col"
1421                        << i + 1
1422                        << "' COLNUM='"
1423                        << i + 1
1424                        << "' COLSEP='";
1425                if (i == (columns_-1)) {
1426                        os << '1';
1427                } else {
1428                    if (column_info[i].right_line ||
1429                        column_info[i+1].left_line)
1430                            os << '1';
1431                    else
1432                            os << '0';
1433                }
1434                os << "'>"
1435                   << newlineAndDepth(depth);
1436                 ++ret;
1437 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1438                 if (column_info[i].left_line)
1439                         os << '|';
1440 #endif
1441             }
1442             os << "<TBODY>"
1443                << newlineAndDepth(++depth)
1444                << "<ROW>"
1445                << newlineAndDepth(++depth)
1446                << "<ENTRY ALIGN='"
1447                << GetDocBookAlign(0)
1448                << "'";
1449            if (IsMultiColumn(0)) {
1450                    os << " NAMEST='col1' NAMEEND='col"
1451                       << cells_in_multicolumn(0)
1452                       << "'";
1453            }
1454            os << ">"
1455               << newlineAndDepth(++depth);
1456             ret += 3;
1457         } else {
1458             if (IsLastCellInRow(cell)) {
1459                     os << newlineAndDepth(--depth)
1460                        << "</ENTRY>"
1461                        << newlineAndDepth(--depth)
1462                        << "</ROW>"
1463                        << newlineAndDepth(depth)
1464                        << "<ROW>"
1465                        << newlineAndDepth(++depth)
1466                        << "<ENTRY ALIGN='"
1467                        << GetDocBookAlign(cell + 1)
1468                        << "' VALIGN='middle'";
1469                if (IsMultiColumn(cell + 1)) {
1470                        os << " NAMEST='col"
1471                           << column_of_cell(cell + 1) + 1
1472                           << "' NAMEEND='col"
1473                           << column_of_cell(cell + 1) +
1474                                cells_in_multicolumn(cell + 1)
1475                           << "'";
1476                }
1477                os << ">"
1478                   << newlineAndDepth(++depth);
1479                 ret += 4;
1480             } else {
1481                     os << newlineAndDepth(--depth)
1482                        << "</ENTRY>"
1483                        << newlineAndDepth(depth)
1484                        << "<ENTRY ALIGN='"
1485                        << GetDocBookAlign(cell + 1)
1486                        << "' VALIGN='middle'";
1487                if (IsMultiColumn(cell + 1)) {
1488                        os << " NAMEST='col"
1489                           << column_of_cell(cell + 1) + 1
1490                           << "' NAMEEND='col"
1491                           << column_of_cell(cell + 1) +
1492                                cells_in_multicolumn(cell + 1)
1493                           << "'";
1494                }
1495                os << ">"
1496                   << newlineAndDepth(++depth);
1497                 ret += 3;
1498             }
1499         }
1500     }
1501     return ret;
1502 }
1503
1504
1505 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1506 {
1507     return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1508         (cellinfo_of_cell(cell)->multicolumn !=LyXTabular::CELL_NORMAL));
1509 }
1510
1511
1512 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1513 {
1514     int const row = row_of_cell(cell);
1515     int const column = column_of_cell(cell);
1516     return  &cell_info[row][column];
1517 }
1518    
1519
1520 void LyXTabular::SetMultiColumn(int cell, int number)
1521 {
1522     cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1523     cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1524     cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1525     cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1526     for (number--; number > 0; --number) {
1527         cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1528     }
1529     set_row_column_number_info();
1530 }
1531
1532
1533 int LyXTabular::cells_in_multicolumn(int cell) const
1534 {
1535     int const row = row_of_cell(cell);
1536     int column = column_of_cell(cell);
1537     int result = 1;
1538     ++column;
1539     while ((column < columns_) &&
1540            cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1541     {
1542         ++result;
1543         ++column;
1544     }
1545     return result;
1546 }
1547
1548
1549 int LyXTabular::UnsetMultiColumn(int cell)
1550 {
1551     int const row = row_of_cell(cell);
1552     int column = column_of_cell(cell);
1553     
1554     int result = 0;
1555     
1556     if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1557         cell_info[row][column].multicolumn = CELL_NORMAL;
1558         ++column;
1559         while ((column < columns_) &&
1560                (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1561         {
1562             cell_info[row][column].multicolumn = CELL_NORMAL;
1563             ++column;
1564             ++result;
1565         }
1566     }
1567     set_row_column_number_info();
1568     return result;
1569 }
1570
1571
1572 void LyXTabular::SetLongTabular(int what)
1573 {
1574     is_long_tabular = what;
1575 }
1576
1577
1578 bool LyXTabular::IsLongTabular() const
1579 {
1580     return is_long_tabular;
1581 }
1582
1583
1584 void LyXTabular::SetRotateTabular(bool flag)
1585 {
1586     rotate = flag;
1587 }
1588
1589
1590 bool LyXTabular::GetRotateTabular() const
1591 {
1592     return rotate;
1593 }
1594
1595
1596 void LyXTabular::SetRotateCell(int cell, bool flag)
1597 {
1598     cellinfo_of_cell(cell)->rotate = flag;
1599 }
1600
1601
1602 bool LyXTabular::GetRotateCell(int cell) const
1603 {
1604     return cellinfo_of_cell(cell)->rotate;
1605 }
1606
1607
1608 bool LyXTabular::NeedRotating() const
1609 {
1610     if (rotate)
1611         return true;
1612     for (int i = 0; i < rows_; ++i) {
1613         for (int j = 0; j < columns_; ++j) {
1614             if (cell_info[i][j].rotate)
1615                 return true;
1616         }
1617     }
1618     return false;
1619 }
1620
1621
1622 bool LyXTabular::IsLastCell(int cell) const
1623 {
1624     if ((cell + 1) < GetNumberOfCells())
1625         return false;
1626     return true;
1627 }
1628
1629
1630 int LyXTabular::GetCellAbove(int cell) const
1631 {
1632     if (row_of_cell(cell) > 0)
1633         return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1634     return cell;
1635 }
1636
1637
1638 int LyXTabular::GetCellBelow(int cell) const
1639 {
1640     if (row_of_cell(cell) + 1 < rows_)
1641         return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1642     return cell;
1643 }
1644
1645
1646 int LyXTabular::GetLastCellAbove(int cell) const
1647 {
1648     if (row_of_cell(cell) <= 0)
1649         return cell;
1650     if (!IsMultiColumn(cell))
1651         return GetCellAbove(cell);
1652     return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1653 }
1654
1655
1656 int LyXTabular::GetLastCellBelow(int cell) const
1657 {
1658     if (row_of_cell(cell) + 1 >= rows_)
1659         return cell;
1660     if (!IsMultiColumn(cell))
1661         return GetCellBelow(cell);
1662     return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1663 }
1664
1665
1666 int LyXTabular::GetCellNumber(int row, int column) const
1667 {
1668     if (column >= columns_)
1669         column = columns_ - 1;
1670     else if (column < 0)
1671         column = 0;
1672     if (row >= rows_)
1673         row = rows_ - 1;
1674     else if (row < 0)
1675         row = 0;
1676     
1677     return cell_info[row][column].cellno;
1678 }
1679
1680
1681 void LyXTabular::SetUsebox(int cell, BoxType type)
1682 {
1683     cellinfo_of_cell(cell)->usebox = type;
1684 }
1685
1686
1687 int LyXTabular::GetUsebox(int cell) const
1688 {
1689     if (column_info[column_of_cell(cell)].p_width.empty() &&
1690         !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1691         return BOX_NONE;
1692     if (cellinfo_of_cell(cell)->usebox > 1)
1693         return cellinfo_of_cell(cell)->usebox;
1694     return UseParbox(cell);
1695 }
1696
1697
1698 void LyXTabular::SetLTHead(int cell, bool first)
1699 {
1700     int const row = row_of_cell(cell);
1701     int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1702
1703     if (first) {
1704         if (endfirsthead == val)
1705             endfirsthead = 0;
1706         else
1707             endfirsthead = val;
1708     } else {
1709         if (endhead == val)
1710             endhead = 0;
1711         else
1712             endhead = val;
1713     }
1714 }
1715
1716
1717 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1718 {
1719     row = endhead;
1720     if (abs(endhead) > rows_)
1721         return false;
1722     return (row_of_cell(cell) == abs(endhead) - 1);
1723 }
1724
1725
1726 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1727 {
1728     row = endfirsthead;
1729     if (abs(endfirsthead) > rows_)
1730         return false;
1731     return (row_of_cell(cell) == abs(endfirsthead) - 1);
1732 }
1733
1734
1735 void LyXTabular::SetLTFoot(int cell, bool last)
1736 {
1737     int const row = row_of_cell(cell);
1738     int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1739
1740     if (last) {
1741         if (endlastfoot == val)
1742             endlastfoot = 0;
1743         else
1744             endlastfoot = val;
1745     } else {
1746         if (endfoot == val)
1747             endfoot = 0;
1748         else
1749             endfoot = val;
1750     }
1751 }
1752
1753
1754 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1755 {
1756     row = endfoot;
1757     if ((endfoot + 1) > rows_)
1758         return false;
1759     return (row_of_cell(cell) == abs(endfoot) - 1);
1760 }
1761
1762
1763 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1764 {
1765     row = endlastfoot;
1766     if (abs(endlastfoot) > rows_)
1767         return false;
1768     return (row_of_cell(cell) == (abs(endlastfoot)-1));
1769 }
1770
1771
1772 void LyXTabular::SetLTNewPage(int cell, bool what)
1773 {
1774     row_info[row_of_cell(cell)].newpage = what;
1775 }
1776
1777
1778 bool LyXTabular::GetLTNewPage(int cell) const
1779 {
1780     return row_info[row_of_cell(cell)].newpage;
1781 }
1782
1783
1784 bool LyXTabular::SetAscentOfRow(int row, int height)
1785 {
1786     if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1787         return false;
1788     row_info[row].ascent_of_row = height;
1789     return true;
1790 }
1791
1792
1793 bool LyXTabular::SetDescentOfRow(int row, int height)
1794 {
1795     if ((row >= rows_) || (row_info[row].descent_of_row == height))
1796         return false;
1797     row_info[row].descent_of_row = height;
1798     return true;
1799 }
1800
1801
1802 int LyXTabular::GetAscentOfRow(int row) const
1803 {
1804     if (row >= rows_)
1805         return 0;
1806     return row_info[row].ascent_of_row;
1807 }
1808
1809
1810 int LyXTabular::GetDescentOfRow(int row) const
1811 {
1812     if (row >= rows_)
1813         return 0;
1814     return row_info[row].descent_of_row;
1815 }
1816
1817
1818 int LyXTabular::GetHeightOfTabular() const
1819 {
1820     int height = 0;
1821
1822     for(int row = 0; row < rows_; ++row)
1823         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1824             GetAdditionalHeight(GetCellNumber(row, 0));
1825     return height;
1826 }
1827
1828
1829 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1830 {
1831     if ((row >= rows_) || (column >= columns_))
1832         return false;
1833     return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1834 }
1835
1836
1837 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1838 {
1839 #warning should this return a bool? (Lgb)
1840     int const fcell = GetFirstCellInRow(row);
1841     int const n = NumberOfCellsInRow(fcell) + fcell;
1842     int tmp = 0;
1843
1844     for (int i = fcell; i < n; ++i) {
1845         if (TopLine(i))
1846             ++tmp;
1847     }
1848     if (tmp == (n - fcell)){
1849         os << "\\hline ";
1850     } else {
1851         for (int i = fcell; i < n; ++i) {
1852             if (TopLine(i)) {
1853                 os << "\\cline{"
1854                    << column_of_cell(i) + 1
1855                    << '-'
1856                    << right_column_of_cell(i) + 1
1857                    << "} ";
1858             }
1859         }
1860     }
1861     if (tmp) {
1862         os << endl;
1863         return 1;
1864     }
1865     return 0;
1866 }
1867
1868
1869 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1870 {
1871 #warning should this return a bool? (Lgb)
1872     int const fcell = GetFirstCellInRow(row);
1873     int const n = NumberOfCellsInRow(fcell) + fcell;
1874     int tmp = 0;
1875
1876     for (int i = fcell; i < n; ++i) {
1877         if (BottomLine(i))
1878             ++tmp;
1879     }
1880     if (tmp == (n-fcell)){
1881         os << "\\hline";
1882     } else {
1883         for (int i = fcell; i < n; ++i) {
1884             if (BottomLine(i)) {
1885                 os << "\\cline{"
1886                    << column_of_cell(i) + 1
1887                    << '-'
1888                    << right_column_of_cell(i) + 1
1889                    << "} ";
1890             }
1891         }
1892     }
1893     if (tmp) {
1894         os << endl;
1895         return 1;
1896     }
1897     return 0;
1898 }
1899
1900
1901 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1902 {
1903     int ret = 0;
1904
1905     if (GetRotateCell(cell)) {
1906         os << "\\begin{sideways}" << endl;
1907         ++ret;
1908     }
1909     if (IsMultiColumn(cell)) {
1910         os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1911         if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1912             os << cellinfo_of_cell(cell+1)->align_special << "}{";
1913         } else {
1914             if (LeftLine(cell))
1915                 os << '|';
1916             if (!GetPWidth(cell).empty()) {
1917                 switch(GetVAlignment(cell)) {
1918                 case LYX_VALIGN_TOP:
1919                     os << "p";
1920                     break;
1921                 case LYX_VALIGN_CENTER:
1922                     os << "m";
1923                     break;
1924                 case LYX_VALIGN_BOTTOM:
1925                     os << "b";
1926                     break;
1927                 }
1928                 os << "{" << GetPWidth(cell) << '}';
1929             } else {
1930                 switch (GetAlignment(cell)) {
1931                 case LYX_ALIGN_LEFT:
1932                     os << 'l';
1933                     break;
1934                 case LYX_ALIGN_RIGHT:
1935                     os << 'r';
1936                     break;
1937                 default:
1938                     os << 'c';
1939                     break;
1940                 }
1941             }
1942             if (RightLine(cell))
1943                 os << '|';
1944             if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1945                 LeftLine(cell+1))
1946                 os << '|';
1947             os << "}{";
1948         }
1949     }
1950     if (GetUsebox(cell) == BOX_PARBOX) {
1951         os << "\\parbox[";
1952         switch(GetVAlignment(cell)) {
1953         case LYX_VALIGN_TOP:
1954             os << "t";
1955             break;
1956         case LYX_VALIGN_CENTER:
1957             os << "c";
1958             break;
1959         case LYX_VALIGN_BOTTOM:
1960             os << "b";
1961             break;
1962         }
1963         os << "]{" << GetPWidth(cell) << "}{";
1964     } else if (GetUsebox(cell) == BOX_MINIPAGE) {
1965         os << "\\begin{minipage}[";
1966         switch(GetVAlignment(cell)) {
1967         case LYX_VALIGN_TOP:
1968             os << "t";
1969             break;
1970         case LYX_VALIGN_CENTER:
1971             os << "m";
1972             break;
1973         case LYX_VALIGN_BOTTOM:
1974             os << "b";
1975             break;
1976         }
1977         os << "]{" << GetPWidth(cell) << "}\n";
1978         ++ret;
1979     }
1980     return ret;
1981 }
1982
1983
1984 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1985 {
1986     int ret = 0;
1987
1988     // usual cells
1989     if (GetUsebox(cell) == BOX_PARBOX)
1990         os << "}";
1991     else if (GetUsebox(cell) == BOX_MINIPAGE) {
1992         os << "%\n\\end{minipage}";
1993         ret += 2;
1994     }
1995     if (IsMultiColumn(cell)){
1996         os << '}';
1997     }
1998     if (GetRotateCell(cell)) {
1999         os << "%\n\\end{sideways}";
2000         ++ret;
2001     }
2002     return ret;
2003 }
2004
2005
2006 int LyXTabular::Latex(Buffer const * buf,
2007                       ostream & os, bool fragile, bool fp) const
2008 {
2009 #warning Jürgen, this func should be split into more funcs (Lgb)
2010     int ret = 0;
2011     int i,j;
2012     int cell = 0;
2013
2014     //+---------------------------------------------------------------------
2015     //+                      first the opening preamble                    +
2016     //+---------------------------------------------------------------------
2017
2018     if (rotate) {
2019         os << "\\begin{sideways}" << endl;
2020         ++ret;
2021     }
2022     if (is_long_tabular)
2023         os << "\\begin{longtable}{";
2024     else
2025         os << "\\begin{tabular}{";
2026     for (i = 0; i < columns_; ++i) {
2027         if (column_info[i].left_line)
2028             os << '|';
2029         if (!column_info[i].align_special.empty()) {
2030             os << column_info[i].align_special;
2031         } else if (!column_info[i].p_width.empty()) {
2032             switch(column_info[i].valignment) {
2033             case LYX_VALIGN_TOP:
2034                 os << "p";
2035                 break;
2036             case LYX_VALIGN_CENTER:
2037                 os << "m";
2038                 break;
2039             case LYX_VALIGN_BOTTOM:
2040                 os << "b";
2041                 break;
2042             }
2043             os << "{"
2044                << column_info[i].p_width
2045                << '}';
2046         } else {
2047             switch (column_info[i].alignment) {
2048             case LYX_ALIGN_LEFT:
2049                 os << 'l';
2050                 break;
2051             case LYX_ALIGN_RIGHT:
2052                 os << 'r';
2053                 break;
2054             default:
2055                 os << 'c';
2056                 break;
2057             }
2058         }
2059         if (column_info[i].right_line)
2060             os << '|';
2061     }
2062     os << "}" << endl;
2063     ++ret;
2064
2065     //+---------------------------------------------------------------------
2066     //+                      the single row and columns (cells)            +
2067     //+---------------------------------------------------------------------
2068
2069     int bret;
2070     for(i=0; i < rows_; ++i) {
2071         ret += TeXTopHLine(os, i);
2072         bret = ret;
2073         if (IsLongTabular()) {
2074             if ((endhead < 0) && (i == (abs(endhead)-1))) {
2075                 os << "\\endhead\n";
2076                 ++ret;
2077             }
2078             if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2079                 os << "\\endfirsthead\n";
2080                 ++ret;
2081             }
2082             if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2083                 os << "\\endfoot\n";
2084                 ++ret;
2085             }
2086             if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2087                 os << "\\endlastfoot\n";
2088                 ++ret;
2089             }
2090         }
2091         if (ret > bret) {
2092             if (i > 0)
2093                 ret += TeXBottomHLine(os, i-1);
2094             ret += TeXTopHLine(os, i);
2095         }
2096         for(j=0; j < columns_; ++j) {
2097             if (IsPartOfMultiColumn(i,j))
2098                 continue;
2099             ret += TeXCellPreamble(os, cell);
2100             ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2101             ret += TeXCellPostamble(os, cell);
2102             if (!IsLastCellInRow(cell)) { // not last cell in row
2103                 os << "&" << endl;
2104                 ++ret;
2105             }
2106             ++cell;
2107         }
2108         os << "\\\\" << endl;
2109         ret += TeXBottomHLine(os, i);
2110         bret = ret;
2111         if (IsLongTabular()) {
2112             if ((endhead > 0) && (i == (endhead-1))) {
2113                 os << "\\endhead\n";
2114                 ++ret;
2115             }
2116             if ((endfirsthead > 0) && (i == (endfirsthead-1))) {
2117                 os << "\\endfirsthead\n";
2118                 ++ret;
2119             }
2120             if ((endfoot > 0) && (i == (endfoot-1))) {
2121                 os << "\\endfoot\n";
2122                 ++ret;
2123             }
2124             if ((endlastfoot > 0) && (i == (endlastfoot-1))) {
2125                 os << "\\endlastfoot\n";
2126                 ++ret;
2127             }
2128             if (ret > bret)
2129                 ret += TeXBottomHLine(os, i);
2130             if (row_info[i].newpage) {
2131                 os << "\\newpage\n";
2132                 ++ret;
2133             }
2134         }
2135     }
2136
2137     //+---------------------------------------------------------------------
2138     //+                      the closing of the tabular                    +
2139     //+---------------------------------------------------------------------
2140
2141     if (is_long_tabular)
2142         os << "\\end{longtable}";
2143     else
2144         os << "\\end{tabular}";
2145     if (rotate) {
2146         os << "\n\\end{sideways}";
2147         ++ret;
2148     }
2149
2150     return ret;
2151 }
2152
2153
2154 InsetText * LyXTabular::GetCellInset(int cell) const
2155 {
2156     return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2157 }
2158
2159
2160 void LyXTabular::Validate(LaTeXFeatures & features) const
2161 {
2162     if (IsLongTabular())
2163         features.longtable = true;
2164     if (NeedRotating())
2165         features.rotating = true;
2166     for(int cell = 0; cell < numberofcells; ++cell) {
2167         if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2168             features.array = true;
2169         GetCellInset(cell)->Validate(features);
2170     }
2171 }
2172
2173
2174 bool LyXTabular::UseParbox(int cell) const
2175 {
2176     LyXParagraph * par = GetCellInset(cell)->par;
2177
2178     for(; par; par = par->next) {
2179         for(int i = 0; i < par->Last(); ++i) {
2180             if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2181                 return true;
2182         }
2183     }
2184     return false;
2185 }