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