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