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