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