]> git.lyx.org Git - lyx.git/blob - src/table.C
white-space changes, removed definitions.h several enum changes because of this,...
[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         width_of_table = 0;
775         for (int i = 0; i < columns; ++i) {
776                 width_of_table += column_info[i].width_of_column;
777         }
778 }
779
780
781 int LyXTable::row_of_cell(int cell) 
782 {
783     if (cell >= numberofcells)
784         return rows-1;
785     else if (cell < 0)
786         return 0;
787     return rowofcell[cell];
788 }
789
790
791 int LyXTable::column_of_cell(int cell)
792 {
793     if (cell >= numberofcells)
794         return columns-1;
795     else if (cell < 0)
796         return 0;
797     return columnofcell[cell];
798 }
799
800
801 int LyXTable::right_column_of_cell(int cell) 
802 {
803         int row = row_of_cell(cell);
804         int column = column_of_cell(cell);
805         while (column < columns - 1 &&
806                cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
807                 column++;
808         return column;
809 }
810
811
812 void LyXTable::Write(FILE* file)
813 {
814     int i, j;
815     fprintf(file, "multicol5\n");
816     fprintf(file, "%d %d %d %d %d %d %d %d\n", rows, columns, is_long_table,
817             rotate, endhead, endfirsthead, endfoot, endlastfoot);
818     for (i = 0; i<rows; i++){
819         fprintf(file, "%d %d %d %d\n", row_info[i].top_line,
820                 row_info[i].bottom_line, row_info[i].is_cont_row,
821                 row_info[i].newpage);
822     }
823     for (i = 0; i<columns; i++){
824         fprintf(file, "%d %d %d \"%s\" \"%s\"\n",
825                 column_info[i].alignment, column_info[i].left_line,
826                 column_info[i].right_line,
827                 VSpace(column_info[i].p_width).asLyXCommand().c_str(),
828                 column_info[i].align_special.c_str());
829     }
830
831     for (i = 0; i<rows;i++){
832         for (j = 0;j<columns;j++){
833             fprintf(file, "%d %d %d %d %d %d %d \"%s\" \"%s\"\n",
834                     cell_info[i][j].multicolumn,
835                     cell_info[i][j].alignment,
836                     cell_info[i][j].top_line,
837                     cell_info[i][j].bottom_line,
838                     cell_info[i][j].has_cont_row,
839                     cell_info[i][j].rotate,
840                     cell_info[i][j].linebreaks,
841                     cell_info[i][j].align_special.c_str(),
842                     cell_info[i][j].p_width.c_str());
843         }
844     }
845 }
846
847 void LyXTable::Read(FILE* file)
848 {
849     int version;
850     int i, j;
851     int rows_arg = 0;
852     int columns_arg = 0;
853     int is_long_table_arg = false;
854     int rotate_arg = false;
855     string s;
856     int a = 0;
857     int b = 0;
858     int c = 0;
859     int d = 0;
860     int e = 0;
861     int f = 0;
862     int g = 0;
863     int h = 0;
864     char vtmp[100], stmp[100], atmp[100];
865
866     fscanf(file, "%s\n", vtmp);
867     s = vtmp;
868     if (s.length() > 8)
869         version = atoi(s.c_str()+8);
870     else
871         version = 1;
872 #ifdef WITH_WARNINGS
873 #warning Insert a error message window here that this format is not supported anymore
874 #endif
875     if (version < 5) {
876         fprintf(stderr, "Tabular format < 5 is not supported anymore\n"
877                 "Get an older version of LyX (< 1.1.x) for conversion!\n");
878         return;
879     }
880     a = b = c = d = -1;
881     fgets(vtmp, sizeof(vtmp), file);
882     sscanf(vtmp, "%d %d %d %d %d %d %d %d\n", &rows_arg, &columns_arg,
883            &is_long_table_arg, &rotate_arg, &a, &b, &c, &d);
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     for (i = 0; i<rows;i++){
925         for (j = 0;j<columns;j++){
926             *stmp = 0;
927             *atmp = 0;
928             a = b = c = d = e = f = g = 0;
929             fgets(vtmp, sizeof(vtmp), file);
930             sscanf(vtmp, "%d %d %d %d %d %d %d %s %s\n",
931                    &a, &b, &c, &d, &e, &f, &g, stmp, atmp);
932             cell_info[i][j].multicolumn = (char) a;
933             cell_info[i][j].alignment = (char) b;
934             cell_info[i][j].top_line = (char) c;
935             cell_info[i][j].bottom_line = (char) d;
936             cell_info[i][j].has_cont_row = (bool) e;
937             cell_info[i][j].rotate = (bool) f;
938             cell_info[i][j].linebreaks = (bool) g;
939             // this is only to see if I have an empty string first
940             // this clause should be always TRUE!!!
941             if (*stmp == '"') {
942                 *stmp = 0;
943                 *atmp = 0;
944                 if (stmp[1] == '"')
945                     sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d %*s \"%[^\"]\"",
946                            atmp);
947                 else // otherwise after the first empty "" read is aborded
948                     sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d \"%[^\"]\" \"%[^\"]\"",
949                            stmp, atmp);
950                 cell_info[i][j].align_special = stmp;
951                 cell_info[i][j].p_width = atmp;
952             } else if (*stmp)
953                 cell_info[i][j].align_special = stmp;
954         }
955     }
956     set_row_column_number_info();
957 }
958
959
960 // cell <0 will tex the preamble
961 // returns the number of printed newlines
962 int LyXTable::TexEndOfCell(string & file, int cell)
963 {
964     int i;
965     int ret = 0;
966     int tmp; // tmp2;
967     int fcell, nvcell;
968     if (ShouldBeVeryLastCell(cell)) {
969         // the very end at the very beginning
970         if (Linebreaks(cell))
971             file += "\\smallskip{}}";
972         if (IsMultiColumn(cell))
973             file += '}';
974         if (RotateCell(cell)) {
975             file += "\n\\end{sideways}";
976             ret++;
977         }
978         file += "\\\\\n";
979         ret++;
980     
981         tmp = 0;
982         fcell = cell; 
983         while (!IsFirstCell(fcell))fcell--;
984         for (i = 0; i < NumberOfCellsInRow(fcell); i++){
985             if (BottomLine(fcell+i))
986                 tmp++;
987         }
988         if (tmp == NumberOfCellsInRow(fcell)){
989             file += "\\hline ";
990         } else {
991             tmp = 0;
992             for (i = 0; i < NumberOfCellsInRow(fcell); i++){
993                 if (BottomLine(fcell+i)){
994                     file += "\\cline{";
995                     file += tostr(column_of_cell(fcell+i)+1);
996                     file += '-';
997                     file += tostr(right_column_of_cell(fcell+i)+1);
998                     file += "} ";
999                     tmp = 1;
1000                 }
1001             }
1002         }
1003         if (tmp){
1004             file += '\n';
1005             ret++;
1006         }
1007         if (is_long_table)
1008             file += "\\end{longtable}";
1009         else
1010             file += "\\end{tabular}";
1011         if (rotate) {
1012             file += "\n\\end{sideways}";
1013             ret++;
1014         }
1015     } else {
1016         nvcell = NextVirtualCell(cell+1);
1017         if (cell < 0){
1018             // preamble
1019             if (rotate) {
1020                 file += "\\begin{sideways}\n";
1021                 ret++;
1022             }
1023             if (is_long_table)
1024                 file += "\\begin{longtable}{";
1025             else
1026                 file += "\\begin{tabular}{";
1027             for (i = 0; i<columns;i++){
1028                 if (column_info[i].left_line)
1029                     file += '|';
1030                 if (!column_info[i].align_special.empty()) {
1031                     file += column_info[i].align_special.c_str();
1032                 } else if (!column_info[i].p_width.empty()) {
1033                     file += "p{";
1034                     file += column_info[i].p_width;
1035                     file += '}';
1036                 } else {
1037                     switch (column_info[i].alignment) {
1038                       case LYX_ALIGN_LEFT:
1039                           file += 'l';
1040                           break;
1041                       case LYX_ALIGN_RIGHT:
1042                           file += 'r';
1043                           break;
1044                       default:
1045                           file += 'c';
1046                           break;
1047                     }
1048                 }
1049                 if (column_info[i].right_line)
1050                     file += '|';
1051             }
1052             file += "}\n";
1053             ret++;
1054             tmp = 0;
1055             if (GetNumberOfCells()){
1056                 fcell = 0;
1057                 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1058                     if (TopLine(fcell+i))
1059                         tmp++;
1060                 }
1061                 if (tmp == NumberOfCellsInRow(fcell)){
1062                     file += "\\hline ";
1063                 } else {
1064                     tmp = 0;
1065                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1066                         if (TopLine(fcell+i)){
1067                             file += "\\cline{";
1068                             file += tostr(column_of_cell(fcell+i)+1);
1069                             file += '-';
1070                             file += tostr(right_column_of_cell(fcell+i)+1);
1071                             file += "} ";
1072                             tmp = 1;
1073                         }
1074                     }
1075                 }
1076                 if (tmp){
1077                     file += '\n';
1078                     ret++;
1079                 }
1080             }
1081             if (RotateCell(0)) {
1082                 file += "\\begin{sideways}\n";
1083                 ret++;
1084             }
1085         } else {
1086             // usual cells
1087             if (Linebreaks(cell))
1088                 file += "\\smallskip{}}";
1089             if (IsMultiColumn(cell)){
1090                 file += '}';
1091             }
1092             if (RotateCell(cell)) {
1093                 file += "\n\\end{sideways}";
1094                 ret++;
1095             }
1096             if (IsLastCell(cell)) {
1097                 int row = row_of_cell(cell);
1098                 string hline1, hline2;
1099                 bool print_hline = true;
1100                 bool pr_top_hline, flag1, flag2;
1101                 flag1 = IsLongTable() &&
1102                     ((row == endhead) || (row == endfirsthead) ||
1103                      (row == endfoot) || (row == endlastfoot));
1104                 row++;
1105                 flag2 = IsLongTable() &&
1106                     ((row <= endhead) || (row <= endfirsthead) ||
1107                      (row <= endfoot) || (row <= endlastfoot));
1108                 row--;
1109                 // print the bottom hline only if (otherwise it is doubled):
1110                 // - is no LongTable
1111                 // - there IS a first-header
1112                 // - the next row is no special header/footer
1113                 //   & this row is no special header/footer
1114                 // - the next row is a special header/footer
1115                 //   & this row is a special header/footer
1116                 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1117                     (endfirsthead == endhead);
1118                 file += "\\\\\n";
1119                 ret++;
1120                 tmp = 0;
1121                 fcell = cell;
1122                 while (!IsFirstCell(fcell))
1123                     fcell--;
1124                 for (i = 0; i < NumberOfCellsInRow(cell); i++){
1125                     if (BottomLine(fcell+i))
1126                         tmp++;
1127                 }
1128                 if (tmp == NumberOfCellsInRow(cell)){
1129                     file += "\\hline ";
1130                     hline1 = "\\hline ";
1131                 } else {
1132                     tmp = 0;
1133                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1134                         if (BottomLine(fcell+i)){
1135                             file += "\\cline{";
1136                             file += tostr(column_of_cell(fcell+i)+1);
1137                             file += '-';
1138                             file += tostr(right_column_of_cell(fcell+i)+1);
1139                             file += "} ";
1140                             hline1 += "\\cline{";
1141                             hline1 += tostr(column_of_cell(fcell+i)+1);
1142                             hline1 += '-';
1143                             hline1 += tostr(right_column_of_cell(fcell+i)+1);
1144                             hline1 += "} ";
1145                             tmp = 1;
1146                         }
1147                     }
1148                 }
1149                 if (tmp){
1150                     file += '\n';
1151                     ret++;
1152                 }
1153                 if (IsLongTable() && (row == endfoot)) {
1154                     file += "\\endfoot\n";
1155                     ret++;
1156                     print_hline = false; // no double line below footer
1157                 }
1158                 if (IsLongTable() && (row == endlastfoot)) {
1159                     file += "\\endlastfoot\n";
1160                     ret++;
1161                     print_hline = false; // no double line below footer
1162                 }
1163                 if (IsLongTable() && row_info[row].newpage) {
1164                     file += "\\newpage\n";
1165                     ret++;
1166                     print_hline = false; // no line below a \\newpage-command
1167                 }
1168                 tmp = 0;
1169                 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1170                     !ShouldBeVeryLastCell(cell)) {
1171                     fcell = nvcell;
1172                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1173                         if (TopLine(fcell+i))
1174                             tmp++;
1175                     }
1176                     if (tmp == NumberOfCellsInRow(fcell)){
1177                         if (print_hline)
1178                             file += "\\hline ";
1179                         hline2 = "\\hline ";
1180                     }
1181                     else {
1182                         tmp = 0;
1183                         for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1184                             if (TopLine(fcell+i)){
1185                                 if (print_hline) {
1186                                     file += "\\cline{";
1187                                     file += tostr(column_of_cell(fcell+i)+1);
1188                                     file += '-';
1189                                     file += tostr(right_column_of_cell(fcell+i)+1);
1190                                     file += "} ";
1191                                 }
1192                                 hline2 += "\\cline{";
1193                                 hline2 += tostr(column_of_cell(fcell+i)+1);
1194                                 hline2 += '-';
1195                                 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1196                                 hline2 += "} ";
1197                                 tmp = 1;
1198                             }
1199                         }
1200                     }
1201                     if (tmp && print_hline){
1202                         file += '\n';
1203                         ret++;
1204                     }
1205                 }
1206                 // the order here is important as if one defines two
1207                 // or more things in one line only the first entry is
1208                 // displayed the other are set to an empty-row. This
1209                 // is important if I have a footer and want that the
1210                 // lastfooter is NOT displayed!!!
1211                 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1212                     (row == endfoot) || (row == endlastfoot);
1213                 row--;
1214 //                sflag2 = IsLongTable() && (row >= 0) &&
1215 //                    (sflag2 || (row == endhead) || (row == endfirsthead));
1216                 row += 2;
1217                 bool sflag1 = IsLongTable() && (row != endhead) &&
1218                     (row != endfirsthead) &&
1219                     ((row == endfoot) || (row == endlastfoot));
1220                 row--;
1221                 if (IsLongTable() && (row == endhead)) {
1222                     file += "\\endhead\n";
1223                     ret++;
1224                 }
1225                 if (IsLongTable() && (row == endfirsthead)) {
1226                     file += "\\endfirsthead\n";
1227                     ret++;
1228                 }
1229                 if (sflag1) { // add the \hline for next foot row
1230                     if (!hline1.empty()) {
1231                         file += hline1 + '\n';
1232                         ret++;
1233                     }
1234                 }
1235                 // add the \hline for the first row
1236                 if (pr_top_hline && sflag2) {
1237                     if (!hline2.empty()) {
1238                         file += hline2 + '\n';
1239                         ret++;
1240                     }
1241                 }
1242                 if (nvcell < numberofcells && RotateCell(nvcell)) {
1243                     file += "\\begin{sideways}\n";
1244                     ret++;
1245                 }
1246             } else {
1247                 file += "&\n";
1248                 ret++;
1249                 if (nvcell < numberofcells && RotateCell(nvcell)) {
1250                     file += "\\begin{sideways}\n";
1251                     ret++;
1252                 }
1253             }
1254         }
1255         if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1256             file += "\\multicolumn{";
1257             file += tostr(cells_in_multicolumn(nvcell));
1258             file += "}{";
1259             if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1260                 file += cellinfo_of_cell(cell+1)->align_special;
1261                 file += "}{";
1262             } else {
1263                 if (LeftLine(nvcell))
1264                     file += '|';
1265                 if (!GetPWidth(nvcell).empty()) {
1266                     file += "p{";
1267                     file += GetPWidth(nvcell);
1268                     file += '}';
1269                 } else {
1270                     switch (GetAlignment(nvcell)) {
1271                       case LYX_ALIGN_LEFT: file += 'l'; break;
1272                       case LYX_ALIGN_RIGHT: file += 'r'; break;
1273                       default:  file += 'c'; break;
1274                     }
1275                 }
1276                 if (RightLine(nvcell))
1277                     file += '|';
1278                 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1279                 if (((nvcell+1) < numberofcells) &&
1280                     (NextVirtualCell(nvcell+1) < numberofcells) &&
1281                     (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1282                     LeftLine(NextVirtualCell(nvcell+1)))
1283                     file += '|';
1284                 file += "}{";
1285             }
1286         }
1287         if (nvcell < numberofcells && Linebreaks(nvcell)) {
1288 //            !column_info[column_of_cell(nvcell)].p_width.empty()) {
1289             file += "\\parbox{";
1290             file += GetPWidth(nvcell);
1291             file += "}{\\smallskip{}";
1292         }
1293     }
1294     return ret;
1295 }
1296
1297
1298 // cell <0 will tex the preamble
1299 // returns the number of printed newlines
1300 int LyXTable::RoffEndOfCell(FILE* file, int cell)
1301 {
1302     int i, j;
1303     int ret = 0;
1304
1305     if (cell == GetNumberOfCells() - 1){
1306         // the very end at the very beginning
1307         if (CellHasContRow(cell) >= 0) {
1308             fprintf(file, "\nT}");
1309             ret++;
1310         }
1311         fprintf(file, "\n");
1312         ret++;
1313         if (row_info[row_of_cell(cell)].bottom_line) {
1314             fprintf(file, "_\n");
1315             ret++;
1316         }
1317         fprintf(file, ".TE\n.pl 1c");
1318     } else {  
1319         if (cell < 0){
1320             int fcell = 0;
1321             // preamble
1322             fprintf(file, "\n.pl 500c\n.TS\n");
1323             for (j = 0; j<rows; j++) {
1324                 for (i = 0; i<columns;i++, fcell++) {
1325                     if (column_info[i].left_line)
1326                         fprintf(file, " | ");
1327                     if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1328                         fprintf(file, "s");
1329                     else {
1330                         switch (column_info[i].alignment) {
1331                           case LYX_ALIGN_LEFT:
1332                               fprintf(file, "l");
1333                               break;
1334                           case LYX_ALIGN_RIGHT:
1335                               fprintf(file, "r");
1336                               break;
1337                           default:
1338                               fprintf(file, "c");
1339                               break;
1340                         }
1341                     }
1342                     if (!column_info[i].p_width.empty())
1343                         fprintf(file, "w(%s)", column_info[i].p_width.c_str());
1344                     if (column_info[i].right_line)
1345                         fprintf(file, " | ");
1346                 }
1347                 if ((j+1) < rows) {
1348                     fprintf(file, "\n");
1349                     ret++;
1350                 }
1351             }
1352             fprintf(file, ".\n");
1353             ret++;
1354             if (row_info[0].top_line) {
1355                 fprintf(file, "_\n");
1356                 ret++;
1357             }
1358             if (CellHasContRow(0) >= 0) {
1359                 fprintf(file, "T{\n");
1360                 ret++;
1361             }
1362         } else {
1363             // usual cells
1364             if (CellHasContRow(cell) >= 0) {
1365                 fprintf(file, "\nT}");
1366                 ret++;
1367             }
1368             if (right_column_of_cell(cell) == columns -1){
1369                 fprintf(file, "\n");
1370                 ret++;
1371                 int row = row_of_cell(cell);
1372                 if (row_info[row++].bottom_line) {
1373                     fprintf(file, "_\n");
1374                     ret++;
1375                 }
1376                 if ((row < rows) && row_info[row].top_line) {
1377                     fprintf(file, "_\n");
1378                     ret++;
1379                 }
1380             } else
1381                 fprintf(file, "\t");
1382             if ((cell < GetNumberOfCells() - 1) &&
1383                 (CellHasContRow(cell+1) >= 0)) {
1384                 fprintf(file, "T{\n");
1385                 ret++;
1386             }
1387         }
1388     }
1389     return ret;
1390 }
1391
1392 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1393 {
1394     int i;
1395     if (isColumn)
1396        i = cell;
1397     else
1398        i = column_of_cell(cell);
1399     if (!isColumn && IsMultiColumn(cell)) {
1400        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1401            return cellinfo_of_cell(cell)->align_special.c_str();
1402        } else {
1403            switch (GetAlignment(cell)) {
1404            case LYX_ALIGN_LEFT:
1405                return "left";
1406            case LYX_ALIGN_RIGHT:
1407                return "right";
1408            default:
1409                return "center";
1410            }
1411        }
1412     } else {
1413        if (!column_info[i].align_special.empty()) {
1414            return column_info[i].align_special.c_str();
1415        }
1416 #ifdef IGNORE_THIS_FOR_NOW
1417        else if (!column_info[i].p_width.empty()) {
1418            file += "p{";
1419            file += column_info[i].p_width;
1420            file += '}';
1421        }
1422 #endif
1423        else {
1424            switch (column_info[i].alignment) {
1425            case LYX_ALIGN_LEFT:
1426                return "left";
1427            case LYX_ALIGN_RIGHT:
1428                return "right";
1429            default:
1430                return "center";
1431            }
1432        }
1433     }
1434 }
1435
1436 // cell <0 will tex the preamble
1437 // returns the number of printed newlines
1438 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1439 {
1440     int i;
1441     int ret = 0;
1442     //int tmp; // tmp2; // unused
1443     int nvcell; // fcell; // unused
1444     if (ShouldBeVeryLastCell(cell)) {
1445        addNewlineAndDepth(file,--depth);
1446         file += "</ENTRY>";
1447        addNewlineAndDepth(file,--depth);
1448         file += "</ROW>";
1449        addNewlineAndDepth(file,--depth);
1450         file += "</TBODY>";
1451        addNewlineAndDepth(file,--depth);
1452         if (is_long_table)
1453             file += "</TGROUP>";
1454         else
1455             file += "</TGROUP>";
1456        addNewlineAndDepth(file,--depth);
1457         ret += 4;
1458     } else {
1459         nvcell = NextVirtualCell(cell+1);
1460         if (cell < 0) {
1461             // preamble
1462             if (is_long_table)
1463                 file += "<TGROUP ";
1464             else
1465                 file += "<TGROUP ";
1466             file += "COLS='";
1467             file += tostr(columns);
1468             file += "' COLSEP='1' ROWSEP='1'>";
1469            addNewlineAndDepth(file,++depth);
1470             ret++;
1471             for (i = 0; i < columns; ++i) {
1472                 file += "<COLSPEC ALIGN='";
1473                file += getDocBookAlign(i, true);
1474                file += "' COLNAME='col";
1475                 file += tostr(i+1);
1476                 file += "' COLNUM='";
1477                 file += tostr(i+1);
1478                file += "' COLSEP= '";
1479                if (i == (columns-1)) {
1480                     file += '1';
1481                } else {
1482                    if (column_info[i].right_line ||
1483                        column_info[i+1].left_line)
1484                        file += '1';
1485                    else
1486                        file += '0';
1487                }
1488                file += "'>";
1489                addNewlineAndDepth(file, depth);
1490                 ret++;
1491 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1492                 if (column_info[i].left_line)
1493                     file += '|';
1494 #endif
1495             }
1496             file += "<TBODY>";
1497            addNewlineAndDepth(file,++depth);
1498             file += "<ROW>";
1499            addNewlineAndDepth(file,++depth);
1500             file += "<ENTRY ALIGN= '";
1501             file += getDocBookAlign(0);
1502            file += "'";
1503            if (IsMultiColumn(0)) {
1504                file += " NAMEST= 'col1' NAMEEND= 'col";
1505                file += tostr(cells_in_multicolumn(0));
1506                file += "'";
1507            }
1508             file += ">";
1509            addNewlineAndDepth(file,++depth);
1510             ret += 3;
1511         } else {
1512             if (IsLastCell(cell)) {
1513                addNewlineAndDepth(file,--depth);
1514                 file += "</ENTRY>";
1515                addNewlineAndDepth(file,--depth);
1516                 file += "</ROW>";
1517                addNewlineAndDepth(file, depth);
1518                file += "<ROW>";
1519                addNewlineAndDepth(file,++depth);
1520                 file += "<ENTRY ALIGN= '";
1521                 file += getDocBookAlign(cell+1);
1522                 file += "' VALIGN= 'middle'";
1523                if (IsMultiColumn(cell+1)) {
1524                    file += " NAMEST= 'col";
1525                    file += tostr(column_of_cell(cell+1) + 1);
1526                    file += "' NAMEEND= 'col";
1527                    file += tostr(column_of_cell(cell+1) +
1528                        cells_in_multicolumn(cell+1));
1529                    file += "'";
1530                }
1531                file += ">";
1532                addNewlineAndDepth(file,++depth);
1533                 ret += 4;
1534             } else {
1535                addNewlineAndDepth(file,--depth);
1536                 file += "</ENTRY>";
1537                addNewlineAndDepth(file, depth);
1538                 file += "<ENTRY ALIGN= '";
1539                 file += getDocBookAlign(cell+1);
1540                 file += "' VALIGN= 'middle'";
1541                if (IsMultiColumn(cell+1)) {
1542                    file += " NAMEST= 'col";
1543                    file += tostr(column_of_cell(cell+1) + 1);
1544                    file += "' NAMEEND= 'col";
1545                    file += tostr(column_of_cell(cell+1) +
1546                        cells_in_multicolumn(cell+1));
1547                    file += "'";
1548                }
1549                file += ">";
1550                addNewlineAndDepth(file,++depth);
1551                 ret += 3;
1552             }
1553         }
1554     }
1555     return ret;
1556 }
1557
1558
1559 bool LyXTable::IsMultiColumn(int cell)
1560 {
1561     int fvcell = FirstVirtualCell(cell);
1562
1563     return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1564 }
1565
1566
1567 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1568 {
1569     int row = row_of_cell(cell);
1570     int column = column_of_cell(cell);
1571     return  &cell_info[row][column];
1572 }
1573    
1574
1575 void LyXTable::SetMultiColumn(int cell, int number)
1576 {
1577     int fvcell = FirstVirtualCell(cell);
1578     int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1579     
1580     cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1581     cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1582     cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1583     cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1584     for (number--;number>0;number--){
1585         cellinfo_of_cell(fvcell+number)->multicolumn = 
1586             LyXTable::CELL_PART_OF_MULTICOLUMN;
1587         new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1588     }
1589     set_row_column_number_info();
1590     SetWidthOfCell(fvcell, new_width);
1591 }
1592
1593
1594 int LyXTable::cells_in_multicolumn(int cell)
1595 {
1596     int row = row_of_cell(cell);
1597     int column = column_of_cell(cell);
1598     int result = 1;
1599     column++;
1600     while (column < columns && cell_info[row][column].multicolumn
1601            == LyXTable::CELL_PART_OF_MULTICOLUMN){
1602         result++;
1603         column++;
1604     }
1605     return result;
1606 }
1607
1608
1609 int  LyXTable::UnsetMultiColumn(int cell)
1610 {
1611     int fvcell = FirstVirtualCell(cell);
1612     int row = row_of_cell(fvcell);
1613     int column = column_of_cell(fvcell);
1614     
1615     int result = 0;
1616     
1617     if (cell_info[row][column].multicolumn
1618         == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1619         cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1620         column++;
1621         while (column < columns &&
1622                cell_info[row][column].multicolumn
1623                == LyXTable::CELL_PART_OF_MULTICOLUMN){
1624             cell_info[row][column].multicolumn = 
1625                 LyXTable::CELL_NORMAL;
1626             column++;
1627             result++;
1628         }
1629     }
1630     set_row_column_number_info();
1631     return result;
1632 }
1633
1634
1635 void LyXTable::delete_column(int column)
1636 {
1637     int i, j;
1638     columnstruct *column_info2 = new columnstruct[columns-1];
1639    
1640     for (i = 0; i<column; i++){
1641         column_info2[i] = column_info[i];
1642     }
1643     for (i = column; i<columns-1; i++){
1644         column_info2[i] = column_info[i+1];
1645     }
1646    
1647     delete[] column_info;
1648     column_info = column_info2;
1649
1650     for (i = 0; i<rows;i++){
1651         cellstruct* tmp = cell_info[i];
1652         cell_info[i] = new cellstruct[columns-1];
1653         for (j = 0; j<column; j++){
1654             cell_info[i][j] = tmp[j];
1655         }
1656         for (j = column; j<columns-1; j++){
1657             cell_info[i][j] = tmp[j+1];
1658         }
1659         delete[] tmp;
1660     }
1661
1662     columns--;
1663     Reinit();
1664 }
1665
1666 void LyXTable::SetLongTable(int what)
1667 {
1668     is_long_table = what;
1669 }
1670
1671 bool LyXTable::IsLongTable()
1672 {
1673     return is_long_table;
1674 }
1675
1676 void LyXTable::SetRotateTable(int what)
1677 {
1678     rotate = what;
1679 }
1680
1681 bool LyXTable::RotateTable()
1682 {
1683     return rotate;
1684 }
1685
1686 void LyXTable::SetRotateCell(int cell, int what)
1687 {
1688     cellinfo_of_cell(cell)->rotate = what;
1689 }
1690
1691 bool LyXTable::RotateCell(int cell)
1692 {
1693     return cellinfo_of_cell(cell)->rotate;
1694 }
1695
1696 bool LyXTable::NeedRotating()
1697 {
1698     if (rotate)
1699         return true;
1700     for (int i = 0; i<rows;i++){
1701         for (int j = 0;j<columns;j++){
1702             if (cell_info[i][j].rotate)
1703                 return true;
1704         }
1705     }
1706     return false;
1707 }
1708
1709 void LyXTable::AppendContRow(int cell)
1710 {
1711     int row = row_of_cell(cell)+1;
1712
1713     if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1714         AppendRow(cell);
1715     row_info[row].is_cont_row = true;
1716     row_info[row].top_line = false;
1717     cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1718     Reinit();
1719 }
1720
1721 bool LyXTable::IsContRow(int cell)
1722 {
1723     return row_info[row_of_cell(cell)].is_cont_row;
1724 }
1725
1726 int LyXTable::CellHasContRow(int cell)
1727 {
1728     int row = row_of_cell(cell);
1729
1730     if (VeryLastRow(cell))
1731         return -1;
1732     if (cell_info[row][column_of_cell(cell)].has_cont_row)
1733         return cell_info[row+1][column_of_cell(cell)].cellno;
1734     return -1;
1735 }
1736
1737 bool LyXTable::RowHasContRow(int cell)
1738 {
1739     int row = row_of_cell(cell) + 1;
1740
1741     if (row < rows)
1742         return row_info[row].is_cont_row;
1743     return false;
1744 }
1745
1746 int LyXTable::FirstVirtualCell(int cell)
1747 {
1748     if (!IsContRow(cell))
1749         return cell;
1750     int row = row_of_cell(cell);
1751     int column = column_of_cell(cell);
1752     for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1753         ;
1754     return cell_info[row][column].cellno;
1755 }
1756
1757
1758 int LyXTable::NextVirtualCell(int cell)
1759 {
1760     if (!IsContRow(cell))
1761         return cell;
1762     int row = row_of_cell(cell);
1763     for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1764         ;
1765     // what if(row >= rows) ?
1766     return cell_info[row][0].cellno;
1767 }
1768
1769
1770 bool LyXTable::ShouldBeVeryLastCell(int cell)
1771 // "very last cell" ..of what? the row? the table?
1772 // "Cell" in this context appears to not count `virtual' cells
1773 {
1774     int fcell = cell + 1;
1775
1776     if (cell == GetNumberOfCells() - 1)
1777         return true; // not really sure if I should return false here
1778     if (!IsContRow(fcell))
1779         return false;
1780     while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1781         fcell++;
1782     if (fcell < GetNumberOfCells() - 1)
1783         return false;
1784     return true;
1785 }
1786
1787 bool LyXTable::ShouldBeVeryLastRow(int cell)
1788 {
1789     if (CellHasContRow(cell)>= 0)
1790         return false;
1791     int row = row_of_cell(cell)+1;
1792     int column = column_of_cell(cell);
1793     while((row < rows) && IsContRow(cell_info[row][column].cellno))
1794         row++;
1795     if (row < rows)
1796         return false; // found another valid row
1797     // I do not have any valid row after the actual
1798     return true;
1799 }
1800
1801 int LyXTable::GetCellAbove(int cell)
1802 {
1803     int row = row_of_cell(cell);
1804     
1805     if (row > 0)
1806         return cell_info[row-1][column_of_cell(cell)].cellno;
1807     return cell;
1808 }
1809
1810 int LyXTable::GetCellNumber(int column, int row)
1811 {
1812     if (column >= columns)
1813         column = columns - 1;
1814     else if (column < 0)
1815         column = 0;
1816     if (row >= rows)
1817         row = rows - 1;
1818     else if (row < 0)
1819         row = 0;
1820     
1821     return cell_info[row][column].cellno;
1822 }
1823
1824 void LyXTable::SetLinebreaks(int cell, bool what)
1825 {
1826     cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1827 }
1828
1829 bool LyXTable::Linebreaks(int cell)
1830 {
1831     int fvcell = FirstVirtualCell(cell);
1832
1833     if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1834         !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1835         return false;
1836     return cellinfo_of_cell(fvcell)->linebreaks;
1837 }
1838
1839 void LyXTable::SetLTHead(int cell, bool first)
1840 {
1841     int row = row_of_cell(cell);
1842
1843     if (first) {
1844         if (row == endfirsthead)
1845             endfirsthead = -1;
1846         else
1847             endfirsthead = row;
1848     } else {
1849         if (row == endhead)
1850             endhead = -1;
1851         else
1852             endhead = row;
1853     }
1854 }
1855
1856 bool LyXTable::RowOfLTHead(int cell)
1857 {
1858     if ((endhead+1) > rows)
1859         endhead = -1;
1860     return (row_of_cell(cell) == endhead);
1861 }
1862
1863 bool LyXTable::RowOfLTFirstHead(int cell)
1864 {
1865     if ((endfirsthead+1) > rows)
1866         endfirsthead = -1;
1867     return (row_of_cell(cell) == endfirsthead);
1868 }
1869
1870 void LyXTable::SetLTFoot(int cell, bool last)
1871 {
1872     int row = row_of_cell(cell);
1873
1874     if (last) {
1875         if (row == endlastfoot)
1876             endlastfoot = -1;
1877         else
1878             endlastfoot = row;
1879     } else {
1880         if (row == endfoot)
1881             endfoot = -1;
1882         else
1883             endfoot = row;
1884     }
1885 }
1886
1887 bool LyXTable::RowOfLTFoot(int cell)
1888 {
1889     if ((endfoot+1) > rows) {
1890         endfoot = -1;
1891         return false;
1892     }
1893     return (row_of_cell(cell) == endfoot);
1894 }
1895
1896 bool LyXTable::RowOfLTLastFoot(int cell)
1897 {
1898     if ((endlastfoot+1) > rows)
1899         endlastfoot = -1;
1900     return (row_of_cell(cell) == endlastfoot);
1901 }
1902
1903 void LyXTable::SetLTNewPage(int cell, bool what)
1904 {
1905     row_info[row_of_cell(cell)].newpage = what;
1906 }
1907
1908 bool LyXTable::LTNewPage(int cell)
1909 {
1910     return row_info[row_of_cell(cell)].newpage;
1911 }