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