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