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