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