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