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