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