]> git.lyx.org Git - lyx.git/blob - src/table.C
fix some of the bugs reported, should hopefully be a bit better now.
[lyx.git] / src / table.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *        Copyright (C) 1995 Matthias Ettrich
7  *        Copyright (C) 1995-1998 The LyX Team.
8  *
9  * ====================================================== 
10  */
11
12 #include <config.h>
13
14 #include <cstdlib>
15 #include "table.h"
16 #include "vspace.h"
17 #include "layout.h"
18 #include "support/lstrings.h"
19
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23
24 extern void addNewlineAndDepth(string & file, int const depth); // Jug 990923
25
26 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)
296                 delete [] rowofcell;
297         rowofcell = new int[numberofcells];
298         if (columnofcell)
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         int top = 1;
443         int bottom = 1;
444         int column;
445         if (row){
446                 for (column = 0;column < columns-1 && bottom;column++){
447                         switch (cell_info[row-1][column].multicolumn){
448                         case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
449                                 bottom = cell_info[row-1][column].bottom_line;
450                                 break;
451                         case LyXTable::CELL_NORMAL:
452                                 bottom = row_info[row-1].bottom_line;
453                         }
454                 }
455                 for (column = 0;column < columns-1 && top;column++){
456                         switch (cell_info[row][column].multicolumn){
457                         case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
458                                 top = cell_info[row][column].top_line;
459                                 break;
460                         case LyXTable::CELL_NORMAL:
461                                 top = row_info[row].top_line;
462                         }
463                 }
464                 if (top && bottom)
465                         return WIDTH_OF_LINE;
466         }
467         return 0;
468 }
469
470
471 int LyXTable::AdditionalWidth(int cell)
472 {
473         // internally already set in SetWidthOfCell
474         // used to get it back in text.C
475         int col = right_column_of_cell(cell);
476         if (col < columns - 1 && column_info[col].right_line &&
477             column_info[col+1].left_line)
478                 return WIDTH_OF_LINE;
479         else
480                 return 0;
481 }
482
483
484 // returns the maximum over all rows 
485 int LyXTable::WidthOfColumn(int cell)
486 {
487         int column1 = column_of_cell(cell);
488         int column2 = right_column_of_cell(cell);
489         int i;
490         int result = 0;
491         for (i = column1; i<= column2;i++){
492                 result += column_info[i].width_of_column;
493         }
494         return result;
495 }
496
497
498 int LyXTable::WidthOfTable()
499 {
500         return width_of_table;
501 }
502
503 /* returns 1 if a complete update is necessary, otherwise 0 */ 
504 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
505 {
506     if (!IsMultiColumn(cell))
507         return false;
508     
509     int row = row_of_cell(cell);
510     int column1 = column_of_cell(cell);
511     int column2 = right_column_of_cell(cell);
512     int i;
513     int width = 0;
514
515     // first set columns to 0 so we can calculate the right width
516     for (i = column1; i<= column2;i++) {
517         cell_info[row][i].width_of_cell = 0;
518     }
519     // set the width to MAX_WIDTH until width > 0
520     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 void LyXTable::recalculateMulticolCells(int cell, int new_width)
532 {
533     int
534         row = row_of_cell(cell),
535         column1 = column_of_cell(cell),
536         column2 = right_column_of_cell(cell),
537         i;
538
539     // first set columns to 0 so we can calculate the right width
540     for (i = column1; 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 /* returns 1 if a complete update is necessary, otherwise 0 */ 
550 bool LyXTable::SetWidthOfCell(int cell, int new_width)
551 {
552     int row = row_of_cell(cell);
553     int column1 = column_of_cell(cell);
554     int tmp = 0;
555     int width = 0;
556
557     if (IsMultiColumn(cell)) {
558         tmp = SetWidthOfMulticolCell(cell, new_width);
559     } else {
560         width = (new_width + 2*WIDTH_OF_LINE);
561         cell_info[row][column1].width_of_cell = width;
562         if (column_info[column1].right_line && (column1 < columns-1) &&
563             column_info[column1+1].left_line) // additional width
564             cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
565         tmp = calculate_width_of_column_NMC(column1);
566     }
567     if (tmp) {
568         int i;
569         for(i = 0; i<columns;++i)
570             calculate_width_of_column_NMC(i);
571         for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
572             ;
573         if (i<numberofcells)
574             recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
575         for(i = 0; i<columns;++i)
576             calculate_width_of_column(i);
577         calculate_width_of_table();
578         return true;
579     }
580     return false;
581 }
582
583
584 bool LyXTable::SetAlignment(int cell, char align)
585 {
586     if (!IsMultiColumn(cell))
587         column_info[column_of_cell(cell)].alignment = align;
588     cellinfo_of_cell(cell)->alignment = align;
589     return true;
590 }
591
592 bool LyXTable::SetPWidth(int cell, string width)
593 {
594     int fvcell = FirstVirtualCell(cell);
595
596     if (IsMultiColumn(fvcell)) {
597 //        if (column_info[column_of_cell(cell)].p_width.empty())
598 //            column_info[column_of_cell(cell)].p_width = width;
599         cellinfo_of_cell(fvcell)->p_width = width;
600     } else {
601         column_info[column_of_cell(fvcell)].p_width = width;
602         if (!width.empty()) // do this only if there is a width
603                 SetAlignment(cell, LYX_ALIGN_LEFT);
604     }
605     return true;
606 }
607
608 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
609 {
610     if (what == SET_SPECIAL_MULTI)
611         cellinfo_of_cell(cell)->align_special = special;
612     else
613         column_info[column_of_cell(cell)].align_special = special;
614     return true;
615 }
616
617 bool LyXTable::SetAllLines(int cell, bool line)
618 {
619     SetTopLine(cell, line);
620     SetBottomLine(cell, line);
621     SetRightLine(cell, line);
622     SetLeftLine(cell, line);
623     return true;
624 }
625
626 bool LyXTable::SetTopLine(int cell, bool line)
627 {
628     int row = row_of_cell(cell);
629
630     if (IsContRow(cell))
631         SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
632     else if (!IsMultiColumn(cell))
633         row_info[row].top_line = line;
634     else
635         cellinfo_of_cell(cell)->top_line = line;
636     return true;
637 }
638
639
640 bool LyXTable::SetBottomLine(int cell, bool line)
641 {
642     int row = row_of_cell(cell);
643
644     if (RowHasContRow(cell))
645         SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
646     else if (!IsMultiColumn(cell))
647         row_info[row_of_cell(cell)].bottom_line = line;
648     else
649         cellinfo_of_cell(cell)->bottom_line = line;
650     return true;
651 }
652
653
654 bool LyXTable::SetLeftLine(int cell, bool line)
655 {
656         column_info[column_of_cell(cell)].left_line = line;
657         return true;
658 }
659
660
661 bool LyXTable::SetRightLine(int cell, bool line)
662 {
663         column_info[right_column_of_cell(cell)].right_line = line;
664         return true;
665 }
666
667
668 char LyXTable::GetAlignment(int cell)
669 {
670         if (IsMultiColumn(cell))
671                 return cellinfo_of_cell(cell)->alignment;
672         else
673                 return column_info[column_of_cell(cell)].alignment;
674 }
675
676 string LyXTable::GetPWidth(int cell)
677 {
678         int fvcell = FirstVirtualCell(cell);
679         
680         if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
681                 return cellinfo_of_cell(fvcell)->p_width;
682         return column_info[column_of_cell(fvcell)].p_width;
683 }
684
685 string LyXTable::GetAlignSpecial(int cell, int what)
686 {
687     if (what == SET_SPECIAL_MULTI)
688         return cellinfo_of_cell(cell)->align_special;
689     return column_info[column_of_cell(cell)].align_special;
690 }
691
692 int LyXTable::GetWidthOfCell(int cell)
693 {
694         int row = row_of_cell(cell);
695         int column1 = column_of_cell(cell);
696         int column2 = right_column_of_cell(cell);
697         int i;
698         int result = 0;
699         for (i = column1; i<= column2;i++){
700                 result += cell_info[row][i].width_of_cell;
701         }
702   
703         result += AdditionalWidth(cell);
704   
705         return result;
706 }
707
708
709 int LyXTable::GetBeginningOfTextInCell(int cell)
710 {
711         int x = 0;
712    
713         switch (GetAlignment(cell)){
714         case LYX_ALIGN_CENTER:
715                 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
716                 break;
717         case LYX_ALIGN_RIGHT:
718                 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
719                 break;
720         default: /* LYX_ALIGN_LEFT: nothing :-) */ 
721                 break;
722         }
723
724         // the LaTeX Way :-(
725         x += WIDTH_OF_LINE;
726         return x;
727 }
728
729
730 bool LyXTable::IsFirstCell(int cell)
731 {
732         return (column_of_cell(cell) == 0);
733 }
734
735 bool LyXTable::IsLastCell(int cell)
736 {
737         return (right_column_of_cell(cell) == (columns-1));
738 }
739
740
741 bool LyXTable::calculate_width_of_column(int column)
742 {
743         int i, max;
744         int old_column_width = column_info[column].width_of_column;
745         max = 0;
746         for (i = 0; i<rows; i++) {
747                 if (cell_info[i][column].width_of_cell > max) {
748                         max = cell_info[i][column].width_of_cell;
749                 }
750         }
751         column_info[column].width_of_column = max;
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 i, max;
758     int old_column_width = column_info[column].width_of_column;
759     max = 0;
760     for (i = 0; i<rows; ++i) {
761         if (!IsMultiColumn(GetCellNumber(column, i)) &&
762             (cell_info[i][column].width_of_cell > max)) {
763             max = cell_info[i][column].width_of_cell;
764         }
765     }
766     column_info[column].width_of_column = max;
767     return (column_info[column].width_of_column != old_column_width);
768 }
769
770 void LyXTable::calculate_width_of_table()
771 {
772         width_of_table = 0;
773         for (int i = 0; i < columns; ++i) {
774                 width_of_table += column_info[i].width_of_column;
775         }
776 }
777
778
779 int LyXTable::row_of_cell(int cell) 
780 {
781     if (cell >= numberofcells)
782         return rows-1;
783     else if (cell < 0)
784         return 0;
785     return rowofcell[cell];
786 }
787
788
789 int LyXTable::column_of_cell(int cell)
790 {
791     if (cell >= numberofcells)
792         return columns-1;
793     else if (cell < 0)
794         return 0;
795     return columnofcell[cell];
796 }
797
798
799 int LyXTable::right_column_of_cell(int cell) 
800 {
801         int row = row_of_cell(cell);
802         int column = column_of_cell(cell);
803         while (column < columns - 1 &&
804                cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
805                 column++;
806         return column;
807 }
808
809
810 void LyXTable::Write(ostream & os)
811 {
812     int i, j;
813     os << "multicol5\n"
814        << rows << " " << columns << " " << is_long_table << " "
815        << rotate << " " << endhead << " " << endfirsthead << " "
816        << endfoot << " " << endlastfoot << "\n";
817     for (i = 0; i < rows; ++i) {
818             os << row_info[i].top_line << " "
819                << row_info[i].bottom_line << " "
820                << row_info[i].is_cont_row << " "
821                << row_info[i].newpage << "\n";
822     }
823     for (i = 0; i < columns; ++i) {
824             os << column_info[i].alignment << " "
825                << column_info[i].left_line << " "
826                << column_info[i].right_line << " \""
827                << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
828                << column_info[i].align_special << "\"\n";
829     }
830
831     for (i = 0; i < rows; ++i) {
832         for (j = 0; j < columns; ++j) {
833                 os << cell_info[i][j].multicolumn << " "
834                    << cell_info[i][j].alignment << " "
835                    << cell_info[i][j].top_line << " "
836                    << cell_info[i][j].bottom_line << " "
837                    << cell_info[i][j].has_cont_row << " "
838                    << cell_info[i][j].rotate << " "
839                    << cell_info[i][j].linebreaks << " \""
840                    << cell_info[i][j].align_special << "\" \""
841                    << cell_info[i][j].p_width << "\"\n";
842         }
843     }
844 }
845
846
847 void LyXTable::Read(istream & is)
848 {
849         int version;
850         int i, j;
851         int rows_arg = 0;
852         int columns_arg = 0;
853         int is_long_table_arg = false;
854         int rotate_arg = false;
855         int a = -1;
856         int b = -1;
857         int c = -1;
858         int d = -1;
859         int e = 0;
860         int f = 0;
861         int g = 0;
862         int h = 0;
863         
864         string s;
865         getline(is, s);
866         if (s.length() > 8)
867                 version = atoi(s.c_str() + 8);
868         else
869                 version = 1;
870 #ifdef WITH_WARNINGS
871 #warning Insert a error message window here that this format is not supported anymore
872 #endif
873         if (version < 5) {
874                 lyxerr << "Tabular format < 5 is not supported anymore\n"
875                         "Get an older version of LyX (< 1.1.x) for conversion!"
876                        << endl;
877                 if (version > 2) {
878                         is >> rows_arg >> columns_arg >> is_long_table_arg
879                            >> rotate_arg >> a >> b >> c >> d;
880                 } else
881                         is >> rows_arg >> columns_arg;
882                 Init(rows_arg, columns_arg);
883                 SetLongTable(is_long_table_arg);
884                 SetRotateTable(rotate_arg);
885                 string tmp;
886                 for (i = 0; i < rows; ++i) {
887                         getline(is, tmp);
888                 }
889                 for (i = 0; i < columns; ++i) {
890                         getline(is, tmp);
891                 }
892                 for (i = 0; i < rows; ++i) {
893                         for (j = 0; j < columns; ++j) {
894                                 getline(is, tmp);
895                         }
896                 }
897                 set_row_column_number_info();
898                 return;
899         }
900         is >> rows_arg >> columns_arg >> is_long_table_arg
901            >> rotate_arg >> a >> b >> c >> d;
902         Init(rows_arg, columns_arg);
903         SetLongTable(is_long_table_arg);
904         SetRotateTable(rotate_arg);
905         endhead = a;
906         endfirsthead = b;
907         endfoot = c;
908         endlastfoot = d;
909         for (i = 0; i < rows; ++i) {
910                 a = b = c = d = e = f = g = h = 0;
911                 is >> a >> b >> c >> d;
912                 row_info[i].top_line = a;
913                 row_info[i].bottom_line = b;
914                 row_info[i].is_cont_row = c;
915                 row_info[i].newpage = d;
916         }
917         for (i = 0; i < columns; ++i) {
918                 string s1;
919                 string s2;
920                 is >> a >> b >> c;
921                 char ch; // skip '"'
922                 is >> ch;
923                 getline(is, s1, '"');
924                 is >> ch; // skip '"'
925                 getline(is, s2, '"');
926                 column_info[i].alignment = static_cast<char>(a);
927                 column_info[i].left_line = b;
928                 column_info[i].right_line = c;
929                 column_info[i].p_width = s1;
930                 column_info[i].align_special = s2;
931         }
932         for (i = 0; i < rows; ++i) {
933                 for (j = 0; j < columns; ++j) {
934                         string s1;
935                         string s2;
936                         is >> a >> b >> c >> d >> e >> f >> g;
937                         char ch;
938                         is >> ch; // skip '"'
939                         getline(is, s1, '"');
940                         is >> ch; // skip '"'
941                         getline(is, s2, '"');
942                         cell_info[i][j].multicolumn = static_cast<char>(a);
943                         cell_info[i][j].alignment = static_cast<char>(b);
944                         cell_info[i][j].top_line = static_cast<char>(c);
945                         cell_info[i][j].bottom_line = static_cast<char>(d);
946                         cell_info[i][j].has_cont_row = static_cast<bool>(e);
947                         cell_info[i][j].rotate = static_cast<bool>(f);
948                         cell_info[i][j].linebreaks = static_cast<bool>(g);
949                         cell_info[i][j].align_special = s1;
950                         cell_info[i][j].p_width = s2;
951                 }
952         }
953         set_row_column_number_info();
954 }
955
956
957 // cell <0 will tex the preamble
958 // returns the number of printed newlines
959 int LyXTable::TexEndOfCell(string & file, int cell)
960 {
961     int i;
962     int ret = 0;
963     int tmp; // tmp2;
964     int fcell, nvcell;
965     if (ShouldBeVeryLastCell(cell)) {
966         // the very end at the very beginning
967         if (Linebreaks(cell))
968             file += "\\smallskip{}}";
969         if (IsMultiColumn(cell))
970             file += '}';
971         if (RotateCell(cell)) {
972             file += "\n\\end{sideways}";
973             ret++;
974         }
975         file += "\\\\\n";
976         ret++;
977     
978         tmp = 0;
979         fcell = cell; 
980         while (!IsFirstCell(fcell))fcell--;
981         for (i = 0; i < NumberOfCellsInRow(fcell); i++){
982             if (BottomLine(fcell+i))
983                 tmp++;
984         }
985         if (tmp == NumberOfCellsInRow(fcell)){
986             file += "\\hline ";
987         } else {
988             tmp = 0;
989             for (i = 0; i < NumberOfCellsInRow(fcell); i++){
990                 if (BottomLine(fcell+i)){
991                     file += "\\cline{";
992                     file += tostr(column_of_cell(fcell+i)+1);
993                     file += '-';
994                     file += tostr(right_column_of_cell(fcell+i)+1);
995                     file += "} ";
996                     tmp = 1;
997                 }
998             }
999         }
1000         if (tmp){
1001             file += '\n';
1002             ret++;
1003         }
1004         if (is_long_table)
1005             file += "\\end{longtable}";
1006         else
1007             file += "\\end{tabular}";
1008         if (rotate) {
1009             file += "\n\\end{sideways}";
1010             ret++;
1011         }
1012     } else {
1013         nvcell = NextVirtualCell(cell+1);
1014         if (cell < 0){
1015             // preamble
1016             if (rotate) {
1017                 file += "\\begin{sideways}\n";
1018                 ret++;
1019             }
1020             if (is_long_table)
1021                 file += "\\begin{longtable}{";
1022             else
1023                 file += "\\begin{tabular}{";
1024             for (i = 0; i<columns;i++){
1025                 if (column_info[i].left_line)
1026                     file += '|';
1027                 if (!column_info[i].align_special.empty()) {
1028                     file += column_info[i].align_special.c_str();
1029                 } else if (!column_info[i].p_width.empty()) {
1030                     file += "p{";
1031                     file += column_info[i].p_width;
1032                     file += '}';
1033                 } else {
1034                     switch (column_info[i].alignment) {
1035                       case LYX_ALIGN_LEFT:
1036                           file += 'l';
1037                           break;
1038                       case LYX_ALIGN_RIGHT:
1039                           file += 'r';
1040                           break;
1041                       default:
1042                           file += 'c';
1043                           break;
1044                     }
1045                 }
1046                 if (column_info[i].right_line)
1047                     file += '|';
1048             }
1049             file += "}\n";
1050             ret++;
1051             tmp = 0;
1052             if (GetNumberOfCells()){
1053                 fcell = 0;
1054                 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1055                     if (TopLine(fcell+i))
1056                         tmp++;
1057                 }
1058                 if (tmp == NumberOfCellsInRow(fcell)){
1059                     file += "\\hline ";
1060                 } else {
1061                     tmp = 0;
1062                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1063                         if (TopLine(fcell+i)){
1064                             file += "\\cline{";
1065                             file += tostr(column_of_cell(fcell+i)+1);
1066                             file += '-';
1067                             file += tostr(right_column_of_cell(fcell+i)+1);
1068                             file += "} ";
1069                             tmp = 1;
1070                         }
1071                     }
1072                 }
1073                 if (tmp){
1074                     file += '\n';
1075                     ret++;
1076                 }
1077             }
1078             if (RotateCell(0)) {
1079                 file += "\\begin{sideways}\n";
1080                 ret++;
1081             }
1082         } else {
1083             // usual cells
1084             if (Linebreaks(cell))
1085                 file += "\\smallskip{}}";
1086             if (IsMultiColumn(cell)){
1087                 file += '}';
1088             }
1089             if (RotateCell(cell)) {
1090                 file += "\n\\end{sideways}";
1091                 ret++;
1092             }
1093             if (IsLastCell(cell)) {
1094                 int row = row_of_cell(cell);
1095                 string hline1, hline2;
1096                 bool print_hline = true;
1097                 bool pr_top_hline, flag1, flag2;
1098                 flag1 = IsLongTable() &&
1099                     ((row == endhead) || (row == endfirsthead) ||
1100                      (row == endfoot) || (row == endlastfoot));
1101                 row++;
1102                 flag2 = IsLongTable() &&
1103                     ((row <= endhead) || (row <= endfirsthead) ||
1104                      (row <= endfoot) || (row <= endlastfoot));
1105                 row--;
1106                 // print the bottom hline only if (otherwise it is doubled):
1107                 // - is no LongTable
1108                 // - there IS a first-header
1109                 // - the next row is no special header/footer
1110                 //   & this row is no special header/footer
1111                 // - the next row is a special header/footer
1112                 //   & this row is a special header/footer
1113                 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1114                     (endfirsthead == endhead);
1115                 file += "\\\\\n";
1116                 ret++;
1117                 tmp = 0;
1118                 fcell = cell;
1119                 while (!IsFirstCell(fcell))
1120                     fcell--;
1121                 for (i = 0; i < NumberOfCellsInRow(cell); i++){
1122                     if (BottomLine(fcell+i))
1123                         tmp++;
1124                 }
1125                 if (tmp == NumberOfCellsInRow(cell)){
1126                     file += "\\hline ";
1127                     hline1 = "\\hline ";
1128                 } else {
1129                     tmp = 0;
1130                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1131                         if (BottomLine(fcell+i)){
1132                             file += "\\cline{";
1133                             file += tostr(column_of_cell(fcell+i)+1);
1134                             file += '-';
1135                             file += tostr(right_column_of_cell(fcell+i)+1);
1136                             file += "} ";
1137                             hline1 += "\\cline{";
1138                             hline1 += tostr(column_of_cell(fcell+i)+1);
1139                             hline1 += '-';
1140                             hline1 += tostr(right_column_of_cell(fcell+i)+1);
1141                             hline1 += "} ";
1142                             tmp = 1;
1143                         }
1144                     }
1145                 }
1146                 if (tmp){
1147                     file += '\n';
1148                     ret++;
1149                 }
1150                 if (IsLongTable() && (row == endfoot)) {
1151                     file += "\\endfoot\n";
1152                     ret++;
1153                     print_hline = false; // no double line below footer
1154                 }
1155                 if (IsLongTable() && (row == endlastfoot)) {
1156                     file += "\\endlastfoot\n";
1157                     ret++;
1158                     print_hline = false; // no double line below footer
1159                 }
1160                 if (IsLongTable() && row_info[row].newpage) {
1161                     file += "\\newpage\n";
1162                     ret++;
1163                     print_hline = false; // no line below a \\newpage-command
1164                 }
1165                 tmp = 0;
1166                 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1167                     !ShouldBeVeryLastCell(cell)) {
1168                     fcell = nvcell;
1169                     for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1170                         if (TopLine(fcell+i))
1171                             tmp++;
1172                     }
1173                     if (tmp == NumberOfCellsInRow(fcell)){
1174                         if (print_hline)
1175                             file += "\\hline ";
1176                         hline2 = "\\hline ";
1177                     }
1178                     else {
1179                         tmp = 0;
1180                         for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1181                             if (TopLine(fcell+i)){
1182                                 if (print_hline) {
1183                                     file += "\\cline{";
1184                                     file += tostr(column_of_cell(fcell+i)+1);
1185                                     file += '-';
1186                                     file += tostr(right_column_of_cell(fcell+i)+1);
1187                                     file += "} ";
1188                                 }
1189                                 hline2 += "\\cline{";
1190                                 hline2 += tostr(column_of_cell(fcell+i)+1);
1191                                 hline2 += '-';
1192                                 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1193                                 hline2 += "} ";
1194                                 tmp = 1;
1195                             }
1196                         }
1197                     }
1198                     if (tmp && print_hline){
1199                         file += '\n';
1200                         ret++;
1201                     }
1202                 }
1203                 // the order here is important as if one defines two
1204                 // or more things in one line only the first entry is
1205                 // displayed the other are set to an empty-row. This
1206                 // is important if I have a footer and want that the
1207                 // lastfooter is NOT displayed!!!
1208                 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1209                     (row == endfoot) || (row == endlastfoot);
1210                 row--;
1211 //                sflag2 = IsLongTable() && (row >= 0) &&
1212 //                    (sflag2 || (row == endhead) || (row == endfirsthead));
1213                 row += 2;
1214                 bool sflag1 = IsLongTable() && (row != endhead) &&
1215                     (row != endfirsthead) &&
1216                     ((row == endfoot) || (row == endlastfoot));
1217                 row--;
1218                 if (IsLongTable() && (row == endhead)) {
1219                     file += "\\endhead\n";
1220                     ret++;
1221                 }
1222                 if (IsLongTable() && (row == endfirsthead)) {
1223                     file += "\\endfirsthead\n";
1224                     ret++;
1225                 }
1226                 if (sflag1) { // add the \hline for next foot row
1227                     if (!hline1.empty()) {
1228                         file += hline1 + '\n';
1229                         ret++;
1230                     }
1231                 }
1232                 // add the \hline for the first row
1233                 if (pr_top_hline && sflag2) {
1234                     if (!hline2.empty()) {
1235                         file += hline2 + '\n';
1236                         ret++;
1237                     }
1238                 }
1239                 if (nvcell < numberofcells && RotateCell(nvcell)) {
1240                     file += "\\begin{sideways}\n";
1241                     ret++;
1242                 }
1243             } else {
1244                 file += "&\n";
1245                 ret++;
1246                 if (nvcell < numberofcells && RotateCell(nvcell)) {
1247                     file += "\\begin{sideways}\n";
1248                     ret++;
1249                 }
1250             }
1251         }
1252         if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1253             file += "\\multicolumn{";
1254             file += tostr(cells_in_multicolumn(nvcell));
1255             file += "}{";
1256             if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1257                 file += cellinfo_of_cell(cell+1)->align_special;
1258                 file += "}{";
1259             } else {
1260                 if (LeftLine(nvcell))
1261                     file += '|';
1262                 if (!GetPWidth(nvcell).empty()) {
1263                     file += "p{";
1264                     file += GetPWidth(nvcell);
1265                     file += '}';
1266                 } else {
1267                     switch (GetAlignment(nvcell)) {
1268                       case LYX_ALIGN_LEFT: file += 'l'; break;
1269                       case LYX_ALIGN_RIGHT: file += 'r'; break;
1270                       default:  file += 'c'; break;
1271                     }
1272                 }
1273                 if (RightLine(nvcell))
1274                     file += '|';
1275                 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1276                 if (((nvcell+1) < numberofcells) &&
1277                     (NextVirtualCell(nvcell+1) < numberofcells) &&
1278                     (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1279                     LeftLine(NextVirtualCell(nvcell+1)))
1280                     file += '|';
1281                 file += "}{";
1282             }
1283         }
1284         if (nvcell < numberofcells && Linebreaks(nvcell)) {
1285 //            !column_info[column_of_cell(nvcell)].p_width.empty()) {
1286             file += "\\parbox{";
1287             file += GetPWidth(nvcell);
1288             file += "}{\\smallskip{}";
1289         }
1290     }
1291     return ret;
1292 }
1293
1294
1295 // cell <0 will tex the preamble
1296 // returns the number of printed newlines
1297 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1298 {
1299     int i, j;
1300     int ret = 0;
1301
1302     if (cell == GetNumberOfCells() - 1){
1303         // the very end at the very beginning
1304         if (CellHasContRow(cell) >= 0) {
1305                 os << "\nT}";
1306             ++ret;
1307         }
1308         os << "\n";
1309         ret++;
1310         if (row_info[row_of_cell(cell)].bottom_line) {
1311                 os << "_\n";
1312             ++ret;
1313         }
1314         os << ".TE\n.pl 1c";
1315     } else {  
1316         if (cell < 0){
1317             int fcell = 0;
1318             // preamble
1319             os << "\n.pl 500c\n.TS\n";
1320             for (j = 0; j<rows; ++j) {
1321                 for (i = 0; i<columns; ++i, ++fcell) {
1322                     if (column_info[i].left_line)
1323                             os << " | ";
1324                     if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1325                             os << "s";
1326                     else {
1327                         switch (column_info[i].alignment) {
1328                           case LYX_ALIGN_LEFT:
1329                                   os << "l";
1330                               break;
1331                           case LYX_ALIGN_RIGHT:
1332                                   os << "r";
1333                               break;
1334                           default:
1335                                   os << "c";
1336                               break;
1337                         }
1338                     }
1339                     if (!column_info[i].p_width.empty())
1340                             os << "w(" << column_info[i].p_width << ")";
1341                     if (column_info[i].right_line)
1342                             os << " | ";
1343                 }
1344                 if ((j + 1) < rows) {
1345                         os << "\n";
1346                     ++ret;
1347                 }
1348             }
1349             os << ".\n";
1350             ++ret;
1351             if (row_info[0].top_line) {
1352                     os << "_\n";
1353                 ++ret;
1354             }
1355             if (CellHasContRow(0) >= 0) {
1356                     os << "T{\n";
1357                 ++ret;
1358             }
1359         } else {
1360             // usual cells
1361             if (CellHasContRow(cell) >= 0) {
1362                     os << "\nT}";
1363                 ++ret;
1364             }
1365             if (right_column_of_cell(cell) == columns -1){
1366                     os << "\n";
1367                 ++ret;
1368                 int row = row_of_cell(cell);
1369                 if (row_info[row++].bottom_line) {
1370                         os << "_\n";
1371                     ++ret;
1372                 }
1373                 if ((row < rows) && row_info[row].top_line) {
1374                         os << "_\n";
1375                     ++ret;
1376                 }
1377             } else
1378                     os << "\t";
1379             if ((cell < GetNumberOfCells() - 1) &&
1380                 (CellHasContRow(cell+1) >= 0)) {
1381                     os << "T{\n";
1382                 ++ret;
1383             }
1384         }
1385     }
1386     return ret;
1387 }
1388
1389
1390 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1391 {
1392     int i;
1393     if (isColumn)
1394        i = cell;
1395     else
1396        i = column_of_cell(cell);
1397     if (!isColumn && IsMultiColumn(cell)) {
1398        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1399            return cellinfo_of_cell(cell)->align_special.c_str();
1400        } else {
1401            switch (GetAlignment(cell)) {
1402            case LYX_ALIGN_LEFT:
1403                return "left";
1404            case LYX_ALIGN_RIGHT:
1405                return "right";
1406            default:
1407                return "center";
1408            }
1409        }
1410     } else {
1411        if (!column_info[i].align_special.empty()) {
1412            return column_info[i].align_special.c_str();
1413        }
1414 #ifdef IGNORE_THIS_FOR_NOW
1415        else if (!column_info[i].p_width.empty()) {
1416            file += "p{";
1417            file += column_info[i].p_width;
1418            file += '}';
1419        }
1420 #endif
1421        else {
1422            switch (column_info[i].alignment) {
1423            case LYX_ALIGN_LEFT:
1424                return "left";
1425            case LYX_ALIGN_RIGHT:
1426                return "right";
1427            default:
1428                return "center";
1429            }
1430        }
1431     }
1432 }
1433
1434 // cell <0 will tex the preamble
1435 // returns the number of printed newlines
1436 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1437 {
1438     int i;
1439     int ret = 0;
1440     //int tmp; // tmp2; // unused
1441     int nvcell; // fcell; // unused
1442     if (ShouldBeVeryLastCell(cell)) {
1443        addNewlineAndDepth(file,--depth);
1444         file += "</ENTRY>";
1445        addNewlineAndDepth(file,--depth);
1446         file += "</ROW>";
1447        addNewlineAndDepth(file,--depth);
1448         file += "</TBODY>";
1449        addNewlineAndDepth(file,--depth);
1450         if (is_long_table)
1451             file += "</TGROUP>";
1452         else
1453             file += "</TGROUP>";
1454        addNewlineAndDepth(file,--depth);
1455         ret += 4;
1456     } else {
1457         nvcell = NextVirtualCell(cell+1);
1458         if (cell < 0) {
1459             // preamble
1460             if (is_long_table)
1461                 file += "<TGROUP ";
1462             else
1463                 file += "<TGROUP ";
1464             file += "COLS='";
1465             file += tostr(columns);
1466             file += "' COLSEP='1' ROWSEP='1'>";
1467            addNewlineAndDepth(file,++depth);
1468             ret++;
1469             for (i = 0; i < columns; ++i) {
1470                 file += "<COLSPEC ALIGN='";
1471                file += getDocBookAlign(i, true);
1472                file += "' COLNAME='col";
1473                 file += tostr(i+1);
1474                 file += "' COLNUM='";
1475                 file += tostr(i+1);
1476                file += "' COLSEP='";
1477                if (i == (columns-1)) {
1478                     file += '1';
1479                } else {
1480                    if (column_info[i].right_line ||
1481                        column_info[i+1].left_line)
1482                        file += '1';
1483                    else
1484                        file += '0';
1485                }
1486                file += "'>";
1487                addNewlineAndDepth(file, depth);
1488                 ret++;
1489 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1490                 if (column_info[i].left_line)
1491                     file += '|';
1492 #endif
1493             }
1494             file += "<TBODY>";
1495            addNewlineAndDepth(file,++depth);
1496             file += "<ROW>";
1497            addNewlineAndDepth(file,++depth);
1498             file += "<ENTRY ALIGN='";
1499             file += getDocBookAlign(0);
1500            file += "'";
1501            if (IsMultiColumn(0)) {
1502                file += " NAMEST='col1' NAMEEND='col";
1503                file += tostr(cells_in_multicolumn(0));
1504                file += "'";
1505            }
1506             file += ">";
1507            addNewlineAndDepth(file,++depth);
1508             ret += 3;
1509         } else {
1510             if (IsLastCell(cell)) {
1511                addNewlineAndDepth(file,--depth);
1512                 file += "</ENTRY>";
1513                addNewlineAndDepth(file,--depth);
1514                 file += "</ROW>";
1515                addNewlineAndDepth(file, depth);
1516                file += "<ROW>";
1517                addNewlineAndDepth(file,++depth);
1518                 file += "<ENTRY ALIGN='";
1519                 file += getDocBookAlign(cell+1);
1520                 file += "' VALIGN='middle'";
1521                if (IsMultiColumn(cell+1)) {
1522                    file += " NAMEST='col";
1523                    file += tostr(column_of_cell(cell+1) + 1);
1524                    file += "' NAMEEND='col";
1525                    file += tostr(column_of_cell(cell+1) +
1526                        cells_in_multicolumn(cell+1));
1527                    file += "'";
1528                }
1529                file += ">";
1530                addNewlineAndDepth(file,++depth);
1531                 ret += 4;
1532             } else {
1533                addNewlineAndDepth(file,--depth);
1534                 file += "</ENTRY>";
1535                addNewlineAndDepth(file, depth);
1536                 file += "<ENTRY ALIGN='";
1537                 file += getDocBookAlign(cell+1);
1538                 file += "' VALIGN='middle'";
1539                if (IsMultiColumn(cell+1)) {
1540                    file += " NAMEST='col";
1541                    file += tostr(column_of_cell(cell+1) + 1);
1542                    file += "' NAMEEND='col";
1543                    file += tostr(column_of_cell(cell+1) +
1544                        cells_in_multicolumn(cell+1));
1545                    file += "'";
1546                }
1547                file += ">";
1548                addNewlineAndDepth(file,++depth);
1549                 ret += 3;
1550             }
1551         }
1552     }
1553     return ret;
1554 }
1555
1556
1557 bool LyXTable::IsMultiColumn(int cell)
1558 {
1559     int fvcell = FirstVirtualCell(cell);
1560
1561     return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1562 }
1563
1564
1565 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1566 {
1567     int row = row_of_cell(cell);
1568     int column = column_of_cell(cell);
1569     return  &cell_info[row][column];
1570 }
1571    
1572
1573 void LyXTable::SetMultiColumn(int cell, int number)
1574 {
1575     int fvcell = FirstVirtualCell(cell);
1576     int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1577     
1578     cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1579     cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1580     cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1581     cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1582     for (number--;number>0;number--){
1583         cellinfo_of_cell(fvcell+number)->multicolumn = 
1584             LyXTable::CELL_PART_OF_MULTICOLUMN;
1585         new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1586     }
1587     set_row_column_number_info();
1588     SetWidthOfCell(fvcell, new_width);
1589 }
1590
1591
1592 int LyXTable::cells_in_multicolumn(int cell)
1593 {
1594     int row = row_of_cell(cell);
1595     int column = column_of_cell(cell);
1596     int result = 1;
1597     column++;
1598     while (column < columns && cell_info[row][column].multicolumn
1599            == LyXTable::CELL_PART_OF_MULTICOLUMN){
1600         result++;
1601         column++;
1602     }
1603     return result;
1604 }
1605
1606
1607 int  LyXTable::UnsetMultiColumn(int cell)
1608 {
1609     int fvcell = FirstVirtualCell(cell);
1610     int row = row_of_cell(fvcell);
1611     int column = column_of_cell(fvcell);
1612     
1613     int result = 0;
1614     
1615     if (cell_info[row][column].multicolumn
1616         == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1617         cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1618         column++;
1619         while (column < columns &&
1620                cell_info[row][column].multicolumn
1621                == LyXTable::CELL_PART_OF_MULTICOLUMN){
1622             cell_info[row][column].multicolumn = 
1623                 LyXTable::CELL_NORMAL;
1624             column++;
1625             result++;
1626         }
1627     }
1628     set_row_column_number_info();
1629     return result;
1630 }
1631
1632
1633 void LyXTable::delete_column(int column)
1634 {
1635     int i, j;
1636     columnstruct *column_info2 = new columnstruct[columns-1];
1637    
1638     for (i = 0; i<column; i++){
1639         column_info2[i] = column_info[i];
1640     }
1641     for (i = column; i<columns-1; i++){
1642         column_info2[i] = column_info[i+1];
1643     }
1644    
1645     delete[] column_info;
1646     column_info = column_info2;
1647
1648     for (i = 0; i<rows;i++){
1649         cellstruct* tmp = cell_info[i];
1650         cell_info[i] = new cellstruct[columns-1];
1651         for (j = 0; j<column; j++){
1652             cell_info[i][j] = tmp[j];
1653         }
1654         for (j = column; j<columns-1; j++){
1655             cell_info[i][j] = tmp[j+1];
1656         }
1657         delete[] tmp;
1658     }
1659
1660     columns--;
1661     Reinit();
1662 }
1663
1664 void LyXTable::SetLongTable(int what)
1665 {
1666     is_long_table = what;
1667 }
1668
1669 bool LyXTable::IsLongTable()
1670 {
1671     return is_long_table;
1672 }
1673
1674 void LyXTable::SetRotateTable(int what)
1675 {
1676     rotate = what;
1677 }
1678
1679 bool LyXTable::RotateTable()
1680 {
1681     return rotate;
1682 }
1683
1684 void LyXTable::SetRotateCell(int cell, int what)
1685 {
1686     cellinfo_of_cell(cell)->rotate = what;
1687 }
1688
1689 bool LyXTable::RotateCell(int cell)
1690 {
1691     return cellinfo_of_cell(cell)->rotate;
1692 }
1693
1694 bool LyXTable::NeedRotating()
1695 {
1696     if (rotate)
1697         return true;
1698     for (int i = 0; i<rows;i++){
1699         for (int j = 0;j<columns;j++){
1700             if (cell_info[i][j].rotate)
1701                 return true;
1702         }
1703     }
1704     return false;
1705 }
1706
1707 void LyXTable::AppendContRow(int cell)
1708 {
1709     int row = row_of_cell(cell)+1;
1710
1711     if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1712         AppendRow(cell);
1713     row_info[row].is_cont_row = true;
1714     row_info[row].top_line = false;
1715     cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1716     Reinit();
1717 }
1718
1719 bool LyXTable::IsContRow(int cell)
1720 {
1721     return row_info[row_of_cell(cell)].is_cont_row;
1722 }
1723
1724 int LyXTable::CellHasContRow(int cell)
1725 {
1726     int row = row_of_cell(cell);
1727
1728     if (VeryLastRow(cell))
1729         return -1;
1730     if (cell_info[row][column_of_cell(cell)].has_cont_row)
1731         return cell_info[row+1][column_of_cell(cell)].cellno;
1732     return -1;
1733 }
1734
1735 bool LyXTable::RowHasContRow(int cell)
1736 {
1737     int row = row_of_cell(cell) + 1;
1738
1739     if (row < rows)
1740         return row_info[row].is_cont_row;
1741     return false;
1742 }
1743
1744 int LyXTable::FirstVirtualCell(int cell)
1745 {
1746     if (!IsContRow(cell))
1747         return cell;
1748     int row = row_of_cell(cell);
1749     int column = column_of_cell(cell);
1750     for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1751         ;
1752     return cell_info[row][column].cellno;
1753 }
1754
1755
1756 int LyXTable::NextVirtualCell(int cell)
1757 {
1758     if (!IsContRow(cell))
1759         return cell;
1760     int row = row_of_cell(cell);
1761     for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1762         ;
1763     // what if(row >= rows) ?
1764     return cell_info[row][0].cellno;
1765 }
1766
1767
1768 bool LyXTable::ShouldBeVeryLastCell(int cell)
1769 // "very last cell" ..of what? the row? the table?
1770 // "Cell" in this context appears to not count `virtual' cells
1771 {
1772     int fcell = cell + 1;
1773
1774     if (cell == GetNumberOfCells() - 1)
1775         return true; // not really sure if I should return false here
1776     if (!IsContRow(fcell))
1777         return false;
1778     while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1779         fcell++;
1780     if (fcell < GetNumberOfCells() - 1)
1781         return false;
1782     return true;
1783 }
1784
1785 bool LyXTable::ShouldBeVeryLastRow(int cell)
1786 {
1787     if (CellHasContRow(cell)>= 0)
1788         return false;
1789     int row = row_of_cell(cell)+1;
1790     int column = column_of_cell(cell);
1791     while((row < rows) && IsContRow(cell_info[row][column].cellno))
1792         row++;
1793     if (row < rows)
1794         return false; // found another valid row
1795     // I do not have any valid row after the actual
1796     return true;
1797 }
1798
1799 int LyXTable::GetCellAbove(int cell)
1800 {
1801     int row = row_of_cell(cell);
1802     
1803     if (row > 0)
1804         return cell_info[row-1][column_of_cell(cell)].cellno;
1805     return cell;
1806 }
1807
1808 int LyXTable::GetCellNumber(int column, int row)
1809 {
1810     if (column >= columns)
1811         column = columns - 1;
1812     else if (column < 0)
1813         column = 0;
1814     if (row >= rows)
1815         row = rows - 1;
1816     else if (row < 0)
1817         row = 0;
1818     
1819     return cell_info[row][column].cellno;
1820 }
1821
1822 void LyXTable::SetLinebreaks(int cell, bool what)
1823 {
1824     cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1825 }
1826
1827 bool LyXTable::Linebreaks(int cell)
1828 {
1829     int fvcell = FirstVirtualCell(cell);
1830
1831     if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1832         !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1833         return false;
1834     return cellinfo_of_cell(fvcell)->linebreaks;
1835 }
1836
1837 void LyXTable::SetLTHead(int cell, bool first)
1838 {
1839     int row = row_of_cell(cell);
1840
1841     if (first) {
1842         if (row == endfirsthead)
1843             endfirsthead = -1;
1844         else
1845             endfirsthead = row;
1846     } else {
1847         if (row == endhead)
1848             endhead = -1;
1849         else
1850             endhead = row;
1851     }
1852 }
1853
1854 bool LyXTable::RowOfLTHead(int cell)
1855 {
1856     if ((endhead+1) > rows)
1857         endhead = -1;
1858     return (row_of_cell(cell) == endhead);
1859 }
1860
1861 bool LyXTable::RowOfLTFirstHead(int cell)
1862 {
1863     if ((endfirsthead+1) > rows)
1864         endfirsthead = -1;
1865     return (row_of_cell(cell) == endfirsthead);
1866 }
1867
1868 void LyXTable::SetLTFoot(int cell, bool last)
1869 {
1870     int row = row_of_cell(cell);
1871
1872     if (last) {
1873         if (row == endlastfoot)
1874             endlastfoot = -1;
1875         else
1876             endlastfoot = row;
1877     } else {
1878         if (row == endfoot)
1879             endfoot = -1;
1880         else
1881             endfoot = row;
1882     }
1883 }
1884
1885 bool LyXTable::RowOfLTFoot(int cell)
1886 {
1887     if ((endfoot+1) > rows) {
1888         endfoot = -1;
1889         return false;
1890     }
1891     return (row_of_cell(cell) == endfoot);
1892 }
1893
1894 bool LyXTable::RowOfLTLastFoot(int cell)
1895 {
1896     if ((endlastfoot+1) > rows)
1897         endlastfoot = -1;
1898     return (row_of_cell(cell) == endlastfoot);
1899 }
1900
1901 void LyXTable::SetLTNewPage(int cell, bool what)
1902 {
1903     row_info[row_of_cell(cell)].newpage = what;
1904 }
1905
1906 bool LyXTable::LTNewPage(int cell)
1907 {
1908     return row_info[row_of_cell(cell)].newpage;
1909 }