]> git.lyx.org Git - lyx.git/blob - src/tabular.C
More ascii-export fixes and when making copy of single tabular cells now the
[lyx.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "buffer.h"
27 #include "BufferView.h"
28 #include "Painter.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
36 #include "gettext.h"
37 #include "tabular_funcs.h"
38
39 #include <algorithm>
40 #include <cstdlib>
41
42 using std::ostream;
43 using std::istream;
44 using std::getline;
45 using std::max;
46 using std::endl;
47 using std::vector;
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::strlen;
51 #endif
52
53 namespace {
54
55         int const WIDTH_OF_LINE = 5;
56
57 } // namespace
58
59 /// Define a few methods for the inner structs
60
61 LyXTabular::cellstruct::cellstruct() 
62 {
63         cellno = 0;
64         width_of_cell = 0;
65         multicolumn = LyXTabular::CELL_NORMAL;
66         alignment = LYX_ALIGN_CENTER;
67         valignment = LYX_VALIGN_TOP;
68         top_line = true;
69         bottom_line = false;
70         left_line = true;
71         right_line = false;
72         usebox = BOX_NONE;
73         rotate = false;
74 }
75
76
77 LyXTabular::rowstruct::rowstruct() 
78 {
79         top_line = true;
80         bottom_line = false;
81         ascent_of_row = 0;
82         descent_of_row = 0;
83         endhead = false;
84         endfirsthead = false;
85         endfoot = false;
86         endlastfoot = false;
87         newpage = false;
88 }
89
90
91 LyXTabular::columnstruct::columnstruct() 
92 {
93         left_line = true;
94         right_line = false;
95         alignment = LYX_ALIGN_CENTER;
96         valignment = LYX_VALIGN_TOP;
97         width_of_column = 0;
98 }
99
100
101 LyXTabular::lttype::lttype()
102 {
103         topDL = false;
104         bottomDL = false;
105         empty = false;
106 }
107
108
109 /* konstruktor */
110 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
111 {
112         owner_ = inset;
113         cur_cell = -1;
114         Init(rows_arg, columns_arg);
115 }
116
117
118 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
119                        bool same_id)
120 {
121         owner_ = inset;
122         cur_cell = -1;
123         Init(lt.rows_, lt.columns_, &lt);
124         // we really should change again to have InsetText as a pointer
125         // and allocate it then we would not have to do this stuff all
126         // double!
127         if (same_id) {
128                 for (int i = 0; i < rows_; ++i) {
129                         for (int j = 0; j < columns_; ++j) {
130                                 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
131                                 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
132                         }
133                 }
134         }
135 #if 0
136 #ifdef WITH_WARNINGS
137 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
138 #endif
139         operator=(lt);
140 #endif
141 }
142
143
144 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
145 {
146         owner_ = inset;
147         cur_cell = -1;
148         Read(buf, lex);
149 }
150
151
152 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
153 {
154 #if 0
155 #warning This while method should look like this: (Lgb)
156
157                 LyXTabular tmp(lt);
158                 tmp.swap(*this);
159 #else
160         // If this and lt is not of the same size we have a serious bug
161         // So then it is ok to throw an exception, or for now
162         // call abort()
163         lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
164         cur_cell = -1;
165         cell_info = lt.cell_info;
166         row_info = lt.row_info;
167         column_info = lt.column_info;
168         SetLongTabular(lt.is_long_tabular);
169         rotate = lt.rotate;
170
171         Reinit();
172 #endif
173         return *this;
174 }
175
176
177 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
178 {
179         LyXTabular * result = new LyXTabular(inset, *this, same_id);
180 #if 0
181         // don't know if this is good but I need to Clone also
182         // the text-insets here, this is for the Undo-facility!
183         for (int i = 0; i < rows_; ++i) {
184                 for (int j = 0; j < columns_; ++j) {
185                         result->cell_info[i][j].inset = cell_info[i][j].inset;
186                         result->cell_info[i][j].inset.setOwner(inset);
187                 }
188         }
189 #endif
190         return result;
191 }
192
193
194 /* activates all lines and sets all widths to 0 */ 
195 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
196 {
197         rows_ = rows_arg;
198         columns_ = columns_arg;
199         row_info = row_vector(rows_, rowstruct());
200         column_info = column_vector(columns_, columnstruct());
201         cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
202
203         if (lt) {
204                 operator=(*lt);
205                 return;
206         }
207
208         int cellno = 0;
209         for (int i = 0; i < rows_; ++i) {
210                 for (int j = 0; j < columns_; ++j) {
211                         cell_info[i][j].inset.setOwner(owner_);
212                         cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
213                         cell_info[i][j].cellno = cellno++;
214                 }
215                 cell_info[i].back().right_line = true;
216         }
217         row_info.back().bottom_line = true;
218         row_info.front().bottom_line = true;
219
220         for (int i = 0; i < columns_; ++i) {
221                 calculate_width_of_column(i);
222         }
223         column_info.back().right_line = true;
224    
225         calculate_width_of_tabular();
226
227         rowofcell = vector<int>();
228         columnofcell = vector<int>();
229         set_row_column_number_info();
230         is_long_tabular = false;
231         rotate = false;
232 }
233
234
235 void LyXTabular::AppendRow(int cell)
236 {
237         ++rows_;
238    
239         int row = row_of_cell(cell);
240
241         row_vector::iterator rit = row_info.begin() + row;
242         row_info.insert(rit, rowstruct());
243         // now set the values of the row before
244         row_info[row] = row_info[row+1];
245
246 #if 0
247         cell_vvector::iterator cit = cell_info.begin() + row;
248         cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
249 #else
250         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
251                                                                                                                   cellstruct()));
252
253         for (int i = 0; i <= row; ++i) {
254                 for (int j = 0; j < columns_; ++j) {
255                         c_info[i][j] = cell_info[i][j];
256                 }
257         }
258         for (int i = row + 1; i < rows_; ++i) {
259                 for (int j = 0; j < columns_; ++j) {
260                         c_info[i][j] = cell_info[i-1][j];
261                 }
262         }
263         cell_info = c_info;
264         ++row;
265         for (int j = 0; j < columns_; ++j) {
266                 cell_info[row][j].inset.clear();
267         }
268 #endif
269         Reinit();
270 }
271
272
273 void LyXTabular::DeleteRow(int row)
274 {
275         if (rows_ == 1) return; // Not allowed to delete last row
276         
277         row_info.erase(row_info.begin() + row); //&row_info[row]);
278         cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
279         --rows_;
280         Reinit();
281 }
282
283
284 void LyXTabular::AppendColumn(int cell)
285 {
286         ++columns_;
287    
288         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
289                                                                                                                   cellstruct()));
290         int const column = column_of_cell(cell);
291         column_vector::iterator cit = column_info.begin() + column + 1;
292         column_info.insert(cit, columnstruct());
293         // set the column values of the column before
294         column_info[column+1] = column_info[column];
295
296         for (int i = 0; i < rows_; ++i) {
297                 for (int j = 0; j <= column; ++j) {
298                         c_info[i][j] = cell_info[i][j];
299                 }
300                 for (int j = column + 1; j < columns_; ++j) {
301                         c_info[i][j] = cell_info[i][j - 1];
302                 }
303                 // care about multicolumns
304                 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
305                         c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
306                 }
307                 if ((column + 1) == columns_ ||
308                         c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
309                         c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
310                 }
311         }
312         cell_info = c_info;
313         //++column;
314         for (int i = 0; i < rows_; ++i) {
315                 //cell_info[i][column].inset.clear();
316                 cell_info[i][column + 1].inset.clear();
317         }
318         Reinit();
319 }
320
321
322 void LyXTabular::DeleteColumn(int column)
323 {
324         // Similar to DeleteRow
325         //if (!(columns_ - 1))
326         //return;
327         if (columns_ == 1) return; // Not allowed to delete last column
328          
329         column_info.erase(column_info.begin() + column);
330         for (int i = 0; i < rows_; ++i) {
331                 cell_info[i].erase(cell_info[i].begin() + column);
332         }
333         --columns_;
334         Reinit();
335 }
336
337
338 void LyXTabular::reinit()
339 {
340         Reinit(false);
341 }
342
343
344 void LyXTabular::Reinit(bool reset_widths)
345 {
346         if (reset_widths) {
347                 for (int i = 0; i < rows_; ++i) {
348                         for (int j = 0; j < columns_; ++j) {
349                                 cell_info[i][j].width_of_cell = 0;
350                                 cell_info[i][j].inset.setOwner(owner_);
351                         }
352                 }
353         }
354   
355         for (int i = 0; i < columns_; ++i) {
356                 calculate_width_of_column(i);
357         }
358         calculate_width_of_tabular();
359
360         set_row_column_number_info();
361 }
362
363
364 void LyXTabular::set_row_column_number_info(bool oldformat)
365 {
366         numberofcells = -1;
367         for (int row = 0; row < rows_; ++row) {
368                 for (int column = 0; column<columns_; ++column) {
369                         if (cell_info[row][column].multicolumn
370                                 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
371                                 ++numberofcells;
372                         cell_info[row][column].cellno = numberofcells;
373                 }
374         }
375         ++numberofcells; // because this is one more than as we start from 0
376
377         rowofcell.resize(numberofcells);
378         columnofcell.resize(numberofcells);
379
380         for (int row = 0, column = 0, c = 0;
381                  c < numberofcells && row < rows_ && column < columns_;) {
382                 rowofcell[c] = row;
383                 columnofcell[c] = column;
384                 ++c;
385                 do {
386                         ++column;
387                 } while (column < columns_ &&
388                                  cell_info[row][column].multicolumn
389                                  == LyXTabular::CELL_PART_OF_MULTICOLUMN);
390                 if (column == columns_) {
391                         column = 0;
392                         ++row;
393                 }
394         }
395
396         for (int row = 0; row < rows_; ++row) {
397                 for (int column = 0; column < columns_; ++column) {
398                         if (IsPartOfMultiColumn(row,column))
399                                 continue;
400                         // now set the right line of multicolumns right for oldformat read
401                         if (oldformat &&
402                                 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
403                         {
404                                 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
405                                 cell_info[row][column].right_line =
406                                         cell_info[row][column+cn-1].right_line;
407                         }
408                         cell_info[row][column].inset.setAutoBreakRows(
409                                 !GetPWidth(GetCellNumber(row, column)).zero());
410                 }
411         }
412 }
413
414
415 int LyXTabular::GetNumberOfCells() const
416 {
417         return numberofcells;
418 }
419
420
421 int LyXTabular::NumberOfCellsInRow(int cell) const
422 {
423         int const row = row_of_cell(cell);
424         int result = 0;
425         for (int i = 0; i < columns_; ++i) {
426                 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
427                         ++result;
428         }
429         return result;
430 }
431
432
433 /* returns 1 if there is a topline, returns 0 if not */ 
434 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
435 {
436         int const row = row_of_cell(cell);
437         
438         if (!onlycolumn && IsMultiColumn(cell))
439                 return cellinfo_of_cell(cell)->top_line;
440         return row_info[row].top_line;
441 }
442
443
444 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
445 {
446         // no bottom line underneath non-existent cells if you please
447         // Isn't that a programming error? Is so this should
448         // be an Assert instead. (Lgb)
449         if (cell >= numberofcells)
450                 return false;
451
452         if (!onlycolumn && IsMultiColumn(cell))
453                 return cellinfo_of_cell(cell)->bottom_line;
454         return row_info[row_of_cell(cell)].bottom_line;
455 }
456
457
458 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
459 {
460         if (!onlycolumn && IsMultiColumn(cell)) {
461 #ifdef SPECIAL_COLUM_HANDLING
462                 if (cellinfo_of_cell(cell)->align_special.empty())
463                         return cellinfo_of_cell(cell)->left_line;
464                 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
465 #else
466                 return cellinfo_of_cell(cell)->left_line;
467 #endif
468         }
469 #ifdef SPECIAL_COLUM_HANDLING
470         if (column_info[column_of_cell(cell)].align_special.empty())
471                 return column_info[column_of_cell(cell)].left_line;
472         return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
473 #else
474         return column_info[column_of_cell(cell)].left_line;
475 #endif
476 }
477
478
479 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
480 {
481         if (!onlycolumn && IsMultiColumn(cell)) {
482 #ifdef SPECIAL_COLUM_HANDLING
483                 if (cellinfo_of_cell(cell)->align_special.empty())
484                         return cellinfo_of_cell(cell)->right_line;
485                 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
486 #else
487                 return cellinfo_of_cell(cell)->right_line;
488 #endif
489         }
490 #ifdef SPECIAL_COLUM_HANDLING
491         if (column_info[column_of_cell(cell)].align_special.empty())
492                 return column_info[right_column_of_cell(cell)].right_line;
493         return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
494 #else
495         return column_info[right_column_of_cell(cell)].right_line;
496 #endif
497 }
498
499
500 bool LyXTabular::TopAlreadyDrawed(int cell) const
501 {
502         int row = row_of_cell(cell);
503         if ((row > 0) && !GetAdditionalHeight(row)) {
504                 int column = column_of_cell(cell);
505                 --row;
506                 while (column
507                            && cell_info[row][column].multicolumn
508                            == LyXTabular::CELL_PART_OF_MULTICOLUMN)
509                         --column;
510                 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
511                         return row_info[row].bottom_line;
512                 else
513                         return cell_info[row][column].bottom_line;
514         }
515         return false;
516 }
517
518
519 bool LyXTabular::LeftAlreadyDrawed(int cell) const
520 {
521         int column = column_of_cell(cell);
522         if (column > 0) {
523                 int row = row_of_cell(cell);
524                 while (--column &&
525                            (cell_info[row][column].multicolumn ==
526                                 LyXTabular::CELL_PART_OF_MULTICOLUMN));
527                 if (GetAdditionalWidth(cell_info[row][column].cellno))
528                         return false;
529 #ifdef SPECIAL_COLUM_HANDLING
530                 return column_info[column].right_line;
531 #else
532                 return RightLine(cell_info[row][column].cellno, true);
533 #endif
534         }
535         return false;
536 }
537
538
539 bool LyXTabular::IsLastRow(int cell) const
540 {
541         return (row_of_cell(cell) == rows_ - 1);
542 }
543
544
545 int LyXTabular::GetAdditionalHeight(int row) const
546 {
547         if (!row || row >= rows_)
548                 return 0;
549
550         bool top = true;
551         bool bottom = true;
552
553         for (int column = 0; column < columns_ - 1 && bottom; ++column) {
554                 switch (cell_info[row - 1][column].multicolumn) {
555                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
556                         bottom = cell_info[row - 1][column].bottom_line;
557                         break;
558                 case LyXTabular::CELL_NORMAL:
559                         bottom = row_info[row - 1].bottom_line;
560                 }
561         }
562         for (int column = 0; column < columns_ - 1 && top; ++column) {
563                 switch (cell_info[row][column].multicolumn){
564                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
565                         top = cell_info[row][column].top_line;
566                         break;
567                 case LyXTabular::CELL_NORMAL:
568                         top = row_info[row].top_line;
569                 }
570         }
571         if (top && bottom)
572                 return WIDTH_OF_LINE;
573         return 0;
574 }
575
576
577 int LyXTabular::GetAdditionalWidth(int cell) const
578 {
579         // internally already set in SetWidthOfCell
580         // used to get it back in text.C
581         int const col = right_column_of_cell(cell);
582         int const row = row_of_cell(cell);
583         if (col < columns_ - 1 && RightLine(cell, true) &&
584                 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
585         {
586                 return WIDTH_OF_LINE;
587         } else {
588                 return 0;
589         }
590 }
591
592
593 // returns the maximum over all rows 
594 int LyXTabular::GetWidthOfColumn(int cell) const
595 {
596         int const column1 = column_of_cell(cell);
597         int const column2 = right_column_of_cell(cell);
598         int result = 0;
599         for (int i = column1; i <= column2; ++i) {
600                 result += column_info[i].width_of_column;
601         }
602         return result;
603 }
604
605
606 int LyXTabular::GetWidthOfTabular() const
607 {
608         return width_of_tabular;
609 }
610
611
612 /* returns 1 if a complete update is necessary, otherwise 0 */ 
613 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
614 {
615         if (!IsMultiColumn(cell))
616                 return false;
617         
618         int const row = row_of_cell(cell);
619         int const column1 = column_of_cell(cell);
620         int const column2 = right_column_of_cell(cell);
621
622         // first set columns to 0 so we can calculate the right width
623         for (int i = column1; i <= column2; ++i) {
624                 cell_info[row][i].width_of_cell = 0;
625         }
626         // set the width to MAX_WIDTH until width > 0
627         int width = (new_width + 2 * WIDTH_OF_LINE);
628
629         int i = column1;
630         for (; i < column2 && width > column_info[i].width_of_column; ++i) {
631                 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
632                 width -= column_info[i].width_of_column;
633         }
634         if (width > 0) {
635                 cell_info[row][i].width_of_cell = width;
636         }
637         return true;
638 }
639
640
641 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
642 {
643         int const row = row_of_cell(cell);
644         int const column1 = column_of_cell(cell);
645         int const column2 = right_column_of_cell(cell);
646
647         // first set columns to 0 so we can calculate the right width
648         int i = column1;
649         for (; i <= column2; ++i)
650                 cell_info[row][i].width_of_cell = 0;
651         for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
652                 ;
653         if (i < numberofcells)
654                 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
655         SetWidthOfMulticolCell(cell, new_width);
656 }
657
658
659 /* returns 1 if a complete update is necessary, otherwise 0 */ 
660 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
661 {
662         int const row = row_of_cell(cell);
663         int const column1 = column_of_cell(cell);
664         bool tmp = false;
665         int width = 0;
666         int add_width = 0;
667
668 #ifdef SPECIAL_COLUM_HANDLING
669         if (RightLine(cell_info[row][column1].cellno, true) &&
670                 (column1 < columns_-1) &&
671                 LeftLine(cell_info[row][column1+1].cellno, true))
672 #else
673         if (column_info[column1].right_line && (column1 < columns_-1) &&
674                 column_info[column1+1].left_line) // additional width
675 #endif
676         {
677                 // additional width
678                 add_width = WIDTH_OF_LINE;
679         }
680         if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
681                 return false;
682         }
683         if (IsMultiColumn(cell, true)) {
684                 tmp = SetWidthOfMulticolCell(cell, new_width);
685         } else {
686                 width = (new_width + 2*WIDTH_OF_LINE + add_width);
687                 cell_info[row][column1].width_of_cell = width;
688                 tmp = calculate_width_of_column_NMC(column1);
689         }
690         if (tmp) {
691                 int i = 0;
692                 for (; i<columns_; ++i)
693                         calculate_width_of_column_NMC(i);
694                 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
695                         ;
696                 if (i < numberofcells)
697                         recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
698                 for (i = 0; i < columns_; ++i)
699                         calculate_width_of_column(i);
700                 calculate_width_of_tabular();
701                 return true;
702         }
703         return false;
704 }
705
706
707 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
708 {
709         if (!IsMultiColumn(cell) || onlycolumn)
710                 column_info[column_of_cell(cell)].alignment = align;
711         if (!onlycolumn)
712                 cellinfo_of_cell(cell)->alignment = align;
713         return true;
714 }
715
716
717 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
718 {
719         if (!IsMultiColumn(cell) || onlycolumn)
720                 column_info[column_of_cell(cell)].valignment = align;
721         if (!onlycolumn)
722                 cellinfo_of_cell(cell)->valignment = align;
723         return true;
724 }
725
726
727 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
728 {
729         bool flag = !width.zero();
730         int const j = column_of_cell(cell);
731
732         column_info[j].p_width = width;
733         if (flag) // do this only if there is a width
734                 SetAlignment(cell, LYX_ALIGN_LEFT);
735         for (int i = 0; i < rows_; ++i) {
736                 int c = GetCellNumber(i, j);
737                 flag = !GetPWidth(c).zero(); // because of multicolumns!
738                 GetCellInset(c)->setAutoBreakRows(flag);
739         }
740         return true;
741 }
742
743
744 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
745 {
746         bool const flag = !width.zero();
747
748         cellinfo_of_cell(cell)->p_width = width;
749         if (IsMultiColumn(cell)) {
750                 GetCellInset(cell)->setAutoBreakRows(flag);
751                 return true;
752         }
753         return false;
754 }
755
756
757 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
758                                  LyXTabular::Feature what)
759 {
760         if (what == SET_SPECIAL_MULTI)
761                 cellinfo_of_cell(cell)->align_special = special;
762         else
763                 column_info[column_of_cell(cell)].align_special = special;
764         return true;
765 }
766
767
768 bool LyXTabular::SetAllLines(int cell, bool line)
769 {
770         SetTopLine(cell, line);
771         SetBottomLine(cell, line);
772         SetRightLine(cell, line);
773         SetLeftLine(cell, line);
774         return true;
775 }
776
777
778 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
779 {
780         int const row = row_of_cell(cell);
781
782         if (onlycolumn || !IsMultiColumn(cell))
783                 row_info[row].top_line = line;
784         else
785                 cellinfo_of_cell(cell)->top_line = line;
786         return true;
787 }
788
789
790 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
791 {
792         if (onlycolumn || !IsMultiColumn(cell))
793                 row_info[row_of_cell(cell)].bottom_line = line;
794         else
795                 cellinfo_of_cell(cell)->bottom_line = line;
796         return true;
797 }
798
799
800 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
801 {
802         if (onlycolumn || !IsMultiColumn(cell))
803                 column_info[column_of_cell(cell)].left_line = line;
804         else
805                 cellinfo_of_cell(cell)->left_line = line;
806         return true;
807 }
808
809
810 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
811 {
812         if (onlycolumn || !IsMultiColumn(cell))
813                 column_info[right_column_of_cell(cell)].right_line = line;
814         else
815                 cellinfo_of_cell(cell)->right_line = line;
816         return true;
817 }
818
819
820 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
821 {
822         if (!onlycolumn && IsMultiColumn(cell))
823                 return cellinfo_of_cell(cell)->alignment;
824         else
825                 return column_info[column_of_cell(cell)].alignment;
826 }
827
828
829 LyXTabular::VAlignment
830 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
831 {
832         if (!onlycolumn && IsMultiColumn(cell))
833                 return cellinfo_of_cell(cell)->valignment;
834         else
835                 return column_info[column_of_cell(cell)].valignment;
836 }
837
838
839 LyXLength const LyXTabular::GetPWidth(int cell) const
840 {
841         if (IsMultiColumn(cell))
842                 return cellinfo_of_cell(cell)->p_width;
843         return column_info[column_of_cell(cell)].p_width;
844 }
845
846
847 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
848 {
849         return column_info[column_of_cell(cell)].p_width;
850 }
851
852
853 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
854 {
855         if (IsMultiColumn(cell))
856                 return cellinfo_of_cell(cell)->p_width;
857         return LyXLength();
858 }
859
860
861 string const LyXTabular::GetAlignSpecial(int cell, int what) const
862 {
863         if (what == SET_SPECIAL_MULTI)
864                 return cellinfo_of_cell(cell)->align_special;
865         return column_info[column_of_cell(cell)].align_special;
866 }
867
868
869 int LyXTabular::GetWidthOfCell(int cell) const
870 {
871         int const row = row_of_cell(cell);
872         int const column1 = column_of_cell(cell);
873         int const column2 = right_column_of_cell(cell);
874         int result = 0;
875         for (int i = column1; i <= column2; ++i) {
876                 result += cell_info[row][i].width_of_cell;
877         }
878         return result;
879 }
880
881
882 int LyXTabular::GetBeginningOfTextInCell(int cell) const
883 {
884         int x = 0;
885    
886         switch (GetAlignment(cell)){
887         case LYX_ALIGN_CENTER:
888                 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
889                 break;
890         case LYX_ALIGN_RIGHT:
891                 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
892                 // + GetAdditionalWidth(cell);
893                 break;
894         default: /* LYX_ALIGN_LEFT: nothing :-) */ 
895                 break;
896         }
897         
898         // the LaTeX Way :-(
899         x += WIDTH_OF_LINE;
900         return x;
901 }
902
903
904 bool LyXTabular::IsFirstCellInRow(int cell) const
905 {
906         return column_of_cell(cell) == 0;
907 }
908
909
910 int LyXTabular::GetFirstCellInRow(int row) const
911 {
912         if (row > (rows_-1))
913                 row = rows_ - 1;
914         return cell_info[row][0].cellno;
915 }
916
917 bool LyXTabular::IsLastCellInRow(int cell) const
918 {
919         return (right_column_of_cell(cell) == (columns_ - 1));
920 }
921
922
923 int LyXTabular::GetLastCellInRow(int row) const
924 {
925         if (row > (rows_-1))
926                 row = rows_ - 1;
927         return cell_info[row][columns_-1].cellno;
928 }
929
930
931 bool LyXTabular::calculate_width_of_column(int column)
932 {
933         int const old_column_width = column_info[column].width_of_column;
934         int maximum = 0;
935         
936         for (int i = 0; i < rows_; ++i) {
937                 maximum = max(cell_info[i][column].width_of_cell, maximum);
938         }
939         column_info[column].width_of_column = maximum;
940         return (column_info[column].width_of_column != old_column_width);
941 }
942
943
944 //
945 // calculate the with of the column without regarding REAL MultiColumn
946 // cells. This means MultiColumn-cells spanning more than 1 column.
947 //
948 bool LyXTabular::calculate_width_of_column_NMC(int column)
949 {
950         int const old_column_width = column_info[column].width_of_column;
951         int max = 0;
952         for (int i = 0; i < rows_; ++i) {
953                 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
954                         (cell_info[i][column].width_of_cell > max)) {
955                         max = cell_info[i][column].width_of_cell;
956                 }
957         }
958         column_info[column].width_of_column = max;
959         return (column_info[column].width_of_column != old_column_width);
960 }
961
962
963 void LyXTabular::calculate_width_of_tabular()
964 {
965         width_of_tabular = 0;
966         for (int i = 0; i < columns_; ++i) {
967                 width_of_tabular += column_info[i].width_of_column;
968         }
969 }
970
971
972 int LyXTabular::row_of_cell(int cell) const
973 {
974         if (cell >= numberofcells)
975                 return rows_ - 1;
976         else if (cell < 0)
977                 return 0;
978         return rowofcell[cell];
979 }
980
981
982 int LyXTabular::column_of_cell(int cell) const
983 {
984         if (cell >= numberofcells)
985                 return columns_ - 1;
986         else if (cell < 0)
987                 return 0;
988         return columnofcell[cell];
989 }
990
991
992 int LyXTabular::right_column_of_cell(int cell) const
993 {
994         int const row = row_of_cell(cell);
995         int column = column_of_cell(cell);
996         while (column < (columns_ - 1) &&
997                    cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
998                 ++column;
999         return column;
1000 }
1001
1002
1003 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1004 {
1005         // header line
1006         os << "<lyxtabular"
1007            << write_attribute("version", 3)
1008            << write_attribute("rows", rows_)
1009            << write_attribute("columns", columns_)
1010            << ">\n";
1011         // global longtable options
1012         os << "<features"
1013            << write_attribute("rotate", tostr(rotate))
1014            << write_attribute("islongtable", tostr(is_long_tabular))
1015            << write_attribute("firstHeadTopDL", tostr(endfirsthead.topDL))
1016            << write_attribute("firstHeadBottomDL", tostr(endfirsthead.bottomDL))
1017            << write_attribute("firstHeadEmpty", tostr(endfirsthead.empty))
1018            << write_attribute("headTopDL", tostr(endhead.topDL))
1019            << write_attribute("headBottomDL", tostr(endhead.bottomDL))
1020            << write_attribute("footTopDL", tostr(endfoot.topDL))
1021            << write_attribute("footBottomDL", tostr(endfoot.bottomDL))
1022            << write_attribute("lastFootTopDL", tostr(endlastfoot.topDL))
1023            << write_attribute("lastFootBottomDL", tostr(endlastfoot.bottomDL))
1024            << write_attribute("lastFootEmpty", tostr(endlastfoot.empty))
1025            << ">\n";
1026         for (int j = 0; j < columns_; ++j) {
1027                 os << "<column"
1028                    << write_attribute("alignment", tostr(column_info[j].alignment))
1029                    << write_attribute("valignment", tostr(column_info[j].valignment))
1030                    << write_attribute("leftline", tostr(column_info[j].left_line))
1031                    << write_attribute("rightline", tostr(column_info[j].right_line))
1032                    << write_attribute("width", column_info[j].p_width.asString())
1033                    << write_attribute("special", column_info[j].align_special)
1034                    << ">\n";
1035         }
1036         for (int i = 0; i < rows_; ++i) {
1037                 os << "<row"
1038                    << write_attribute("topline", tostr(row_info[i].top_line))
1039                    << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1040                    << write_attribute("endhead", tostr(row_info[i].endhead))
1041                    << write_attribute("endfirsthead", tostr(row_info[i].endfirsthead))
1042                    << write_attribute("endfoot", tostr(row_info[i].endfoot))
1043                    << write_attribute("endlastfoot", tostr(row_info[i].endlastfoot))
1044                    << write_attribute("newpage", tostr(row_info[i].newpage))
1045                    << ">\n";
1046                 for (int j = 0; j < columns_; ++j) {
1047                         os << "<cell"
1048                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1049                            << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1050                            << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1051                            << write_attribute("topline", tostr(cell_info[i][j].top_line))
1052                            << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1053                            << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1054                            << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1055                            << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1056                            << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1057                            << write_attribute("width", cell_info[i][j].p_width)
1058                            << write_attribute("special", cell_info[i][j].align_special)
1059                            << ">\n";
1060                         os << "\\begin_inset ";
1061                         cell_info[i][j].inset.write(buf, os);
1062                         os << "\n\\end_inset \n"
1063                            << "</cell>\n";
1064                 }
1065                 os << "</row>\n";
1066         }
1067         os << "</lyxtabular>\n";
1068 }
1069
1070
1071 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1072 {
1073         string line;
1074         istream & is = lex.getStream();
1075
1076         l_getline(is, line);
1077         if (!prefixIs(line, "<lyxtabular ")
1078                 && !prefixIs(line, "<LyXTabular ")) {
1079                 OldFormatRead(lex, line);
1080                 return;
1081         }
1082
1083         int version;
1084         if (!getTokenValue(line, "version", version))
1085                 return;
1086         if (version == 1)
1087                 ReadOld(buf, is, lex, line);
1088         else if (version >= 2)
1089                 ReadNew(buf, is, lex, line, version);
1090 }
1091
1092 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1093 {
1094         // set header info
1095         while(hr > 0) {
1096                 row_info[--hr].endhead = true;
1097         }
1098         // set firstheader info
1099         if (fhr && (fhr < rows_)) {
1100                 if (row_info[fhr].endhead) {
1101                         while(fhr > 0) {
1102                                 row_info[--fhr].endfirsthead = true;
1103                                 row_info[fhr].endhead = false;
1104                         }
1105                 } else if (row_info[fhr-1].endhead) {
1106                         endfirsthead.empty = true;
1107                 } else {
1108                         while((fhr > 0) && !row_info[--fhr].endhead) {
1109                                 row_info[fhr].endfirsthead = true;
1110                         }
1111                 }
1112         }
1113         // set footer info
1114         if (fr && (fr < rows_)) {
1115                 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1116                         while((fr > 0) && !row_info[--fr].endhead) {
1117                                 row_info[fr].endfoot = true;
1118                                 row_info[fr].endhead = false;
1119                         }
1120                 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1121                         while((fr > 0) && !row_info[--fr].endfirsthead) {
1122                                 row_info[fr].endfoot = true;
1123                                 row_info[fr].endfirsthead = false;
1124                         }
1125                 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead) {
1126                         while((fr > 0) && !row_info[--fr].endhead &&
1127                                   !row_info[fr].endfirsthead)
1128                         {
1129                                 row_info[fr].endfoot = true;
1130                         }
1131                 }
1132         }
1133         // set lastfooter info
1134         if (lfr && (lfr < rows_)) {
1135                 if (row_info[lfr].endhead && row_info[lfr-1].endhead) {
1136                         while((lfr > 0) && !row_info[--lfr].endhead) {
1137                                 row_info[lfr].endlastfoot = true;
1138                                 row_info[lfr].endhead = false;
1139                         }
1140                 } else if (row_info[lfr].endfirsthead &&
1141                                    row_info[lfr-1].endfirsthead)
1142                 {
1143                         while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1144                                 row_info[lfr].endlastfoot = true;
1145                                 row_info[lfr].endfirsthead = false;
1146                         }
1147                 } else if (row_info[lfr].endfoot && row_info[lfr-1].endfoot) {
1148                         while((lfr > 0) && !row_info[--lfr].endfoot) {
1149                                 row_info[lfr].endlastfoot = true;
1150                                 row_info[lfr].endfoot = false;
1151                         }
1152                 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead &&
1153                                    !row_info[fr-1].endfoot)
1154                 {
1155                         while((lfr > 0) &&
1156                                   !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1157                                   !row_info[lfr].endfoot)
1158                         {
1159                                 row_info[lfr].endlastfoot = true;
1160                         }
1161                 } else if (haveLTFoot()) {
1162                         endlastfoot.empty = true;
1163                 }
1164         }
1165 }
1166
1167 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1168                                                  LyXLex & lex, string const & l, int const version)
1169 {
1170         string line(l);
1171         int rows_arg;
1172         if (!getTokenValue(line, "rows", rows_arg))
1173                 return;
1174         int columns_arg;
1175         if (!getTokenValue(line, "columns", columns_arg))
1176                 return;
1177         Init(rows_arg, columns_arg);
1178         l_getline(is, line);
1179         if (!prefixIs(line, "<features")) {
1180                 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1181                         line << ")" << endl;
1182                 return;
1183         }
1184         getTokenValue(line, "rotate", rotate);
1185         getTokenValue(line, "islongtable", is_long_tabular);
1186         // compatibility read for old longtable options. Now we can make any
1187         // row part of the header/footer type we want before it was strict
1188         // sequential from the first row down (as LaTeX does it!). So now when
1189         // we find a header/footer line we have to go up the rows and set it
1190         // on all preceding rows till the first or one with already a h/f option
1191         // set. If we find a firstheader on the same line as a header or a
1192         // lastfooter on the same line as a footer then this should be set empty.
1193         // (Jug 20011220)
1194         if (version < 3) {
1195                 int hrow;
1196                 int fhrow;
1197                 int frow;
1198                 int lfrow;
1199
1200                 getTokenValue(line, "endhead", hrow);
1201                 getTokenValue(line, "endfirsthead", fhrow);
1202                 getTokenValue(line, "endfoot", frow);
1203                 getTokenValue(line, "endlastfoot", lfrow);
1204                 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1205         } else {
1206            getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1207            getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1208            getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1209            getTokenValue(line, "headTopDL", endhead.topDL);
1210            getTokenValue(line, "headBottomDL", endhead.bottomDL);
1211            getTokenValue(line, "footTopDL", endfoot.topDL);
1212            getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1213            getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1214            getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1215            getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1216         }
1217         for (int j = 0; j < columns_; ++j) {
1218                 l_getline(is,line);
1219                 if (!prefixIs(line,"<column")) {
1220                         lyxerr << "Wrong tabular format (expected <column ...> got" <<
1221                                 line << ")" << endl;
1222                         return;
1223                 }
1224                 getTokenValue(line, "alignment", column_info[j].alignment);
1225                 getTokenValue(line, "valignment", column_info[j].valignment);
1226                 getTokenValue(line, "leftline", column_info[j].left_line);
1227                 getTokenValue(line, "rightline", column_info[j].right_line);
1228                 getTokenValue(line, "width", column_info[j].p_width);
1229                 getTokenValue(line, "special", column_info[j].align_special);
1230         }
1231
1232         for (int i = 0; i < rows_; ++i) {
1233                 l_getline(is, line);
1234                 if (!prefixIs(line, "<row")) {
1235                         lyxerr << "Wrong tabular format (expected <row ...> got" <<
1236                                 line << ")" << endl;
1237                         return;
1238                 }
1239                 getTokenValue(line, "topline", row_info[i].top_line);
1240                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1241                 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1242                 getTokenValue(line, "endhead", row_info[i].endhead);
1243                 getTokenValue(line, "endfoot", row_info[i].endfoot);
1244                 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1245                 getTokenValue(line, "newpage", row_info[i].newpage);
1246                 for (int j = 0; j < columns_; ++j) {
1247                         l_getline(is, line);
1248                         if (!prefixIs(line, "<cell")) {
1249                                 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1250                                         line << ")" << endl;
1251                                 return;
1252                         }
1253                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1254                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1255                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1256                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1257                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1258                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1259                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1260                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1261                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1262                         getTokenValue(line, "width", cell_info[i][j].p_width);
1263                         getTokenValue(line, "special", cell_info[i][j].align_special);
1264                         l_getline(is, line);
1265                         if (prefixIs(line, "\\begin_inset")) {
1266                                 cell_info[i][j].inset.read(buf, lex);
1267                                 l_getline(is, line);
1268                         }
1269                         if (!prefixIs(line, "</cell>")) {
1270                                 lyxerr << "Wrong tabular format (expected </cell> got" <<
1271                                         line << ")" << endl;
1272                                 return;
1273                         }
1274                 }
1275                 l_getline(is, line);
1276                 if (!prefixIs(line, "</row>")) {
1277                         lyxerr << "Wrong tabular format (expected </row> got" <<
1278                                 line << ")" << endl;
1279                         return;
1280                 }
1281         }
1282         while (!prefixIs(line, "</lyxtabular>")) {
1283                 l_getline(is, line);
1284         }
1285         set_row_column_number_info();
1286 }
1287
1288
1289 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1290 {
1291         int version;
1292         int i;
1293         int j;
1294         int rows_arg = 0;
1295         int columns_arg = 0;
1296         int is_long_tabular_arg = false;
1297         int rotate_arg = false;
1298         int a = -1;
1299         int b = -1;
1300         int c = -1;
1301         int d = -1;
1302         int e = 0;
1303         int f = 0;
1304         int g = 0;
1305         
1306         istream & is = lex.getStream();
1307         string s(fl);
1308         if (s.length() > 8)
1309                 version = lyx::atoi(s.substr(8, string::npos));
1310         else
1311                 version = 1;
1312
1313         vector<int> cont_row_info;
1314
1315         if (version < 5) {
1316                 lyxerr << "Tabular format < 5 is not supported anymore\n"
1317                         "Get an older version of LyX (< 1.1.x) for conversion!"
1318                            << endl;
1319                 Alert::alert(_("Warning:"),
1320                                    _("Tabular format < 5 is not supported anymore\n"),
1321                                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1322                 if (version > 2) {
1323                         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1324                            >> rotate_arg >> a >> b >> c >> d;
1325                 } else
1326                         is >> rows_arg >> columns_arg;
1327                 Init(rows_arg, columns_arg);
1328                 cont_row_info = vector<int>(rows_arg);
1329                 SetLongTabular(is_long_tabular_arg);
1330                 SetRotateTabular(rotate_arg);
1331                 string tmp;
1332                 for (i = 0; i < rows_; ++i) {
1333                         getline(is, tmp);
1334                         cont_row_info[i] = false;
1335                 }
1336                 for (i = 0; i < columns_; ++i) {
1337                         getline(is, tmp);
1338                 }
1339                 for (i = 0; i < rows_; ++i) {
1340                         for (j = 0; j < columns_; ++j) {
1341                                 getline(is, tmp);
1342                         }
1343                 }
1344         } else {
1345                 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1346                    >> rotate_arg >> a >> b >> c >> d;
1347                 Init(rows_arg, columns_arg);
1348                 cont_row_info = vector<int>(rows_arg);
1349                 SetLongTabular(is_long_tabular_arg);
1350                 SetRotateTabular(rotate_arg);
1351                 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1352                 for (i = 0; i < rows_; ++i) {
1353                         a = b = c = d = e = f = g = 0;
1354                         is >> a >> b >> c >> d;
1355                         row_info[i].top_line = a;
1356                         row_info[i].bottom_line = b;
1357                         cont_row_info[i] = c;
1358                         row_info[i].newpage = d;
1359                 }
1360                 for (i = 0; i < columns_; ++i) {
1361                         string s1;
1362                         string s2;
1363                         is >> a >> b >> c;
1364 #if 1
1365                         char ch; // skip '"'
1366                         is >> ch;
1367 #else
1368                         // ignore is buggy but we will use it later (Lgb)
1369                         is.ignore(); // skip '"'
1370 #endif    
1371                         getline(is, s1, '"');
1372 #if 1
1373                         is >> ch; // skip '"'
1374 #else
1375                         // ignore is buggy but we will use it later (Lgb)
1376                         is.ignore(); // skip '"'
1377 #endif
1378                         getline(is, s2, '"');
1379                         column_info[i].alignment = static_cast<LyXAlignment>(a);
1380                         column_info[i].left_line = b;
1381                         column_info[i].right_line = c;
1382                         column_info[i].p_width = LyXLength(s1);
1383                         column_info[i].align_special = s2;
1384                 }
1385                 for (i = 0; i < rows_; ++i) {
1386                         for (j = 0; j < columns_; ++j) {
1387                                 string s1;
1388                                 string s2;
1389                                 is >> a >> b >> c >> d >> e >> f >> g;
1390 #if 1
1391                                 char ch;
1392                                 is >> ch; // skip '"'
1393 #else
1394                                 // ignore is buggy but we will use it later (Lgb)
1395                                 is.ignore(); // skip '"'
1396 #endif
1397                                 getline(is, s1, '"');
1398 #if 1
1399                                 is >> ch; // skip '"'
1400 #else
1401                                 // ignore is buggy but we will use it later (Lgb)
1402                                 is.ignore(); // skip '"'
1403 #endif
1404                                 getline(is, s2, '"');
1405                                 cell_info[i][j].multicolumn = static_cast<char>(a);
1406                                 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1407                                 cell_info[i][j].top_line = static_cast<char>(c);
1408                                 cell_info[i][j].bottom_line = static_cast<char>(d);
1409                                 cell_info[i][j].left_line = column_info[j].left_line;
1410                                 cell_info[i][j].right_line = column_info[j].right_line;
1411                                 cell_info[i][j].rotate = static_cast<bool>(f);
1412                                 cell_info[i][j].usebox = static_cast<BoxType>(g);
1413                                 cell_info[i][j].align_special = s1;
1414                                 cell_info[i][j].p_width = LyXLength(s2);
1415                         }
1416                 }
1417         }
1418         set_row_column_number_info(true);
1419
1420         Paragraph * par = new Paragraph;
1421         Paragraph * return_par = 0;
1422
1423         string tmptok;
1424         int pos = 0;
1425         Paragraph::depth_type depth = 0;
1426         LyXFont font(LyXFont::ALL_INHERIT);
1427         font.setLanguage(owner_->bufferOwner()->getLanguage());
1428
1429         while (lex.isOK()) {
1430                 lex.nextToken();
1431                 string const token = lex.getString();
1432                 if (token.empty())
1433                         continue;
1434                 if (token == "\\layout"
1435                         || token == "\\end_float"
1436                         || token == "\\end_deeper") {
1437                         lex.pushToken(token);
1438 #ifndef NO_COMPABILITY
1439                         // Here we need to insert the inset_ert_contents into the last
1440                         // cell of the tabular.
1441                         owner_->bufferOwner()->insertErtContents(par, pos, font);
1442 #endif
1443                         break;
1444                 }
1445                 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1446                                                                                                                           return_par,
1447                                                                                                                           token, pos,
1448                                                                                                                           depth, font)) {
1449                         // the_end read
1450                         lex.pushToken(token);
1451                         break;
1452                 }
1453                 if (return_par) {
1454                         lex.printError("New Paragraph allocated! This should not happen!");
1455                         lex.pushToken(token);
1456                         delete par;
1457                         par = return_par;
1458                         break;
1459                 }
1460         }
1461         // now we have the par we should fill the insets with this!
1462         int cell = 0;
1463         InsetText * inset = GetCellInset(cell);
1464         int row;
1465
1466         for (int i = 0; i < par->size(); ++i) {
1467                 if (par->isNewline(i)) {
1468                         ++cell;
1469                         if (cell > numberofcells) {
1470                                 lyxerr << "Some error in reading old table format occured!" <<
1471                                         endl << "Terminating when reading cell[" << cell << "]!" <<
1472                                         endl;
1473                                 delete par;
1474                                 return;
1475                         }
1476                         row = row_of_cell(cell);
1477                         if (cont_row_info[row]) {
1478                                 DeleteRow(row);
1479                                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1480                                 while (!IsFirstCellInRow(--cell));
1481                         } else {
1482                                 inset = GetCellInset(cell);
1483                                 continue;
1484                         }
1485                         inset = GetCellInset(cell);
1486                         row = row_of_cell(cell);
1487                         if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1488                         {
1489                                 // insert a space instead
1490                                 par->erase(i);
1491                                 par->insertChar(i, ' ');
1492                         }
1493                 }
1494                 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1495                 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1496         }
1497         delete par;
1498         Reinit();
1499 }
1500
1501
1502 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1503 {
1504         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1505                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1506 }
1507
1508
1509 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1510 {
1511         int const row = row_of_cell(cell);
1512         int const column = column_of_cell(cell);
1513         return  &cell_info[row][column];
1514 }
1515
1516
1517 void LyXTabular::SetMultiColumn(int cell, int number)
1518 {
1519         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1520         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1521         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1522         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1523         for (number--; number > 0; --number) {
1524                 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1525         }
1526         set_row_column_number_info();
1527 }
1528
1529
1530 int LyXTabular::cells_in_multicolumn(int cell) const
1531 {
1532         int const row = row_of_cell(cell);
1533         int column = column_of_cell(cell);
1534         int result = 1;
1535         ++column;
1536         while ((column < columns_) &&
1537                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1538         {
1539                 ++result;
1540                 ++column;
1541         }
1542         return result;
1543 }
1544
1545
1546 int LyXTabular::UnsetMultiColumn(int cell)
1547 {
1548         int const row = row_of_cell(cell);
1549         int column = column_of_cell(cell);
1550         
1551         int result = 0;
1552         
1553         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1554                 cell_info[row][column].multicolumn = CELL_NORMAL;
1555                 ++column;
1556                 while ((column < columns_) &&
1557                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1558                 {
1559                         cell_info[row][column].multicolumn = CELL_NORMAL;
1560                         ++column;
1561                         ++result;
1562                 }
1563         }
1564         set_row_column_number_info();
1565         return result;
1566 }
1567
1568
1569 void LyXTabular::SetLongTabular(bool what)
1570 {
1571         is_long_tabular = what;
1572 }
1573
1574
1575 bool LyXTabular::IsLongTabular() const
1576 {
1577         return is_long_tabular;
1578 }
1579
1580
1581 void LyXTabular::SetRotateTabular(bool flag)
1582 {
1583         rotate = flag;
1584 }
1585
1586
1587 bool LyXTabular::GetRotateTabular() const
1588 {
1589         return rotate;
1590 }
1591
1592
1593 void LyXTabular::SetRotateCell(int cell, bool flag)
1594 {
1595         cellinfo_of_cell(cell)->rotate = flag;
1596 }
1597
1598
1599 bool LyXTabular::GetRotateCell(int cell) const
1600 {
1601         return cellinfo_of_cell(cell)->rotate;
1602 }
1603
1604
1605 bool LyXTabular::NeedRotating() const
1606 {
1607         if (rotate)
1608                 return true;
1609         for (int i = 0; i < rows_; ++i) {
1610                 for (int j = 0; j < columns_; ++j) {
1611                         if (cell_info[i][j].rotate)
1612                                 return true;
1613                 }
1614         }
1615         return false;
1616 }
1617
1618
1619 bool LyXTabular::IsLastCell(int cell) const
1620 {
1621         if ((cell + 1) < numberofcells)
1622                 return false;
1623         return true;
1624 }
1625
1626
1627 int LyXTabular::GetCellAbove(int cell) const
1628 {
1629         if (row_of_cell(cell) > 0)
1630                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1631         return cell;
1632 }
1633
1634
1635 int LyXTabular::GetCellBelow(int cell) const
1636 {
1637         if (row_of_cell(cell) + 1 < rows_)
1638                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1639         return cell;
1640 }
1641
1642
1643 int LyXTabular::GetLastCellAbove(int cell) const
1644 {
1645         if (row_of_cell(cell) <= 0)
1646                 return cell;
1647         if (!IsMultiColumn(cell))
1648                 return GetCellAbove(cell);
1649         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1650 }
1651
1652
1653 int LyXTabular::GetLastCellBelow(int cell) const
1654 {
1655         if (row_of_cell(cell) + 1 >= rows_)
1656                 return cell;
1657         if (!IsMultiColumn(cell))
1658                 return GetCellBelow(cell);
1659         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1660 }
1661
1662
1663 int LyXTabular::GetCellNumber(int row, int column) const
1664 {
1665 #if 0
1666         if (column >= columns_)
1667                 column = columns_ - 1;
1668         else if (column < 0)
1669                 column = 0;
1670         if (row >= rows_)
1671                 row = rows_ - 1;
1672         else if (row < 0)
1673                 row = 0;
1674 #else
1675         lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1676 #endif
1677         return cell_info[row][column].cellno;
1678 }
1679
1680
1681 void LyXTabular::SetUsebox(int cell, BoxType type)
1682 {
1683         cellinfo_of_cell(cell)->usebox = type;
1684 }
1685
1686
1687 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1688 {
1689         if (column_info[column_of_cell(cell)].p_width.zero() &&
1690                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1691                 return BOX_NONE;
1692         if (cellinfo_of_cell(cell)->usebox > 1)
1693                 return cellinfo_of_cell(cell)->usebox;
1694         return UseParbox(cell);
1695 }
1696
1697
1698 ///
1699 //  This are functions used for the longtable support
1700 ///
1701 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1702 {
1703         if (first) {
1704                 endfirsthead = hd;
1705                 if (hd.set)
1706                         row_info[row].endfirsthead = flag;
1707         } else {
1708                 endhead = hd;
1709                 if (hd.set)
1710                         row_info[row].endhead = flag;
1711         }
1712 }
1713
1714
1715 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1716 {
1717         hd = endhead;
1718         hd.set = haveLTHead();
1719         return row_info[row].endhead;
1720 }
1721
1722
1723 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1724 {
1725         hd = endfirsthead;
1726         hd.set = haveLTFirstHead();
1727         return row_info[row].endfirsthead;
1728 }
1729
1730
1731 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1732 {
1733         if (last) {
1734                 endlastfoot = fd;
1735                 if (fd.set)
1736                         row_info[row].endlastfoot = flag;
1737         } else {
1738                 endfoot = fd;
1739                 if (fd.set)
1740                         row_info[row].endfoot = flag;
1741         }
1742 }
1743
1744
1745 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1746 {
1747         fd = endfoot;
1748         fd.set = haveLTFoot();
1749         return row_info[row].endfoot;
1750 }
1751
1752
1753 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1754 {
1755         fd = endlastfoot;
1756         fd.set = haveLTLastFoot();
1757         return row_info[row].endlastfoot;
1758 }
1759
1760
1761 void LyXTabular::SetLTNewPage(int row, bool what)
1762 {
1763         row_info[row].newpage = what;
1764 }
1765
1766
1767 bool LyXTabular::GetLTNewPage(int row) const
1768 {
1769         return row_info[row].newpage;
1770 }
1771
1772
1773 bool LyXTabular::haveLTHead() const
1774 {
1775         for(int i=0; i < rows_; ++i) {
1776                 if (row_info[i].endhead)
1777                         return true;
1778         }
1779         return false;
1780 }
1781
1782
1783 bool LyXTabular::haveLTFirstHead() const
1784 {
1785         if (endfirsthead.empty)
1786                 return false;
1787         for(int i=0; i < rows_; ++i) {
1788                 if (row_info[i].endfirsthead)
1789                         return true;
1790         }
1791         return false;
1792 }
1793
1794
1795 bool LyXTabular::haveLTFoot() const
1796 {
1797         for(int i=0; i < rows_; ++i) {
1798                 if (row_info[i].endfoot)
1799                         return true;
1800         }
1801         return false;
1802 }
1803
1804
1805 bool LyXTabular::haveLTLastFoot() const
1806 {
1807         if (endlastfoot.empty)
1808                 return false;
1809         for(int i=0; i < rows_; ++i) {
1810                 if (row_info[i].endlastfoot)
1811                         return true;
1812         }
1813         return false;
1814 }
1815
1816
1817 // end longtable support functions
1818
1819 bool LyXTabular::SetAscentOfRow(int row, int height)
1820 {
1821         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1822                 return false;
1823         row_info[row].ascent_of_row = height;
1824         return true;
1825 }
1826
1827
1828 bool LyXTabular::SetDescentOfRow(int row, int height)
1829 {
1830         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1831                 return false;
1832         row_info[row].descent_of_row = height;
1833         return true;
1834 }
1835
1836
1837 int LyXTabular::GetAscentOfRow(int row) const
1838 {
1839         if (row >= rows_)
1840                 return 0;
1841         return row_info[row].ascent_of_row;
1842 }
1843
1844
1845 int LyXTabular::GetDescentOfRow(int row) const
1846 {
1847         if (row >= rows_)
1848                 return 0;
1849         return row_info[row].descent_of_row;
1850 }
1851
1852
1853 int LyXTabular::GetHeightOfTabular() const
1854 {
1855         int height = 0;
1856
1857         for (int row = 0; row < rows_; ++row)
1858                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1859                         GetAdditionalHeight(row);
1860         return height;
1861 }
1862
1863
1864 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1865 {
1866         if ((row >= rows_) || (column >= columns_))
1867                 return false;
1868         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1869 }
1870
1871
1872 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1873 {
1874         if ((row < 0) || (row >= rows_))
1875                 return 0;
1876
1877         int const fcell = GetFirstCellInRow(row);
1878         int const n = NumberOfCellsInRow(fcell) + fcell;
1879         int tmp = 0;
1880
1881         for (int i = fcell; i < n; ++i) {
1882                 if (TopLine(i))
1883                         ++tmp;
1884         }
1885         if (tmp == (n - fcell)){
1886                 os << "\\hline ";
1887         } else if (tmp) {
1888                 for (int i = fcell; i < n; ++i) {
1889                         if (TopLine(i)) {
1890                                 os << "\\cline{"
1891                                    << column_of_cell(i) + 1
1892                                    << '-'
1893                                    << right_column_of_cell(i) + 1
1894                                    << "} ";
1895                         }
1896                 }
1897         } else {
1898                 return 0;
1899         }
1900         os << "\n";
1901         return 1;
1902 }
1903
1904
1905 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1906 {
1907         if ((row < 0) || (row >= rows_))
1908                 return 0;
1909
1910         int const fcell = GetFirstCellInRow(row);
1911         int const n = NumberOfCellsInRow(fcell) + fcell;
1912         int tmp = 0;
1913
1914         for (int i = fcell; i < n; ++i) {
1915                 if (BottomLine(i))
1916                         ++tmp;
1917         }
1918         if (tmp == (n-fcell)){
1919                 os << "\\hline";
1920         } else if (tmp) {
1921                 for (int i = fcell; i < n; ++i) {
1922                         if (BottomLine(i)) {
1923                                 os << "\\cline{"
1924                                    << column_of_cell(i) + 1
1925                                    << '-'
1926                                    << right_column_of_cell(i) + 1
1927                                    << "} ";
1928                         }
1929                 }
1930         } else {
1931                 return 0;
1932         }
1933         os << "\n";
1934         return 1;
1935 }
1936
1937
1938 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1939 {
1940         int ret = 0;
1941
1942         if (GetRotateCell(cell)) {
1943                 os << "\\begin{sideways}\n";
1944                 ++ret;
1945         }
1946         if (IsMultiColumn(cell)) {
1947                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1948                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1949                         os << cellinfo_of_cell(cell)->align_special << "}{";
1950                 } else {
1951                         if (LeftLine(cell) &&
1952                                 (IsFirstCellInRow(cell) || 
1953                                  (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1954                                   !RightLine(cell-1, true))))
1955                         {
1956                                 os << '|';
1957                         }
1958                         if (!GetPWidth(cell).zero()) {
1959                                 switch (GetVAlignment(cell)) {
1960                                 case LYX_VALIGN_TOP:
1961                                         os << "p";
1962                                         break;
1963                                 case LYX_VALIGN_CENTER:
1964                                         os << "m";
1965                                         break;
1966                                 case LYX_VALIGN_BOTTOM:
1967                                         os << "b";
1968                                         break;
1969                                 }
1970                                 os << "{" << GetPWidth(cell).asLatexString() << '}';
1971                         } else {
1972                                 switch (GetAlignment(cell)) {
1973                                 case LYX_ALIGN_LEFT:
1974                                         os << 'l';
1975                                         break;
1976                                 case LYX_ALIGN_RIGHT:
1977                                         os << 'r';
1978                                         break;
1979                                 default:
1980                                         os << 'c';
1981                                         break;
1982                                 }
1983                         }
1984                         if (RightLine(cell))
1985                                 os << '|';
1986                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1987                                 LeftLine(cell+1))
1988                                 os << '|';
1989                         os << "}{";
1990                 }
1991         }
1992         if (GetUsebox(cell) == BOX_PARBOX) {
1993                 os << "\\parbox[";
1994                 switch (GetVAlignment(cell)) {
1995                 case LYX_VALIGN_TOP:
1996                         os << "t";
1997                         break;
1998                 case LYX_VALIGN_CENTER:
1999                         os << "c";
2000                         break;
2001                 case LYX_VALIGN_BOTTOM:
2002                         os << "b";
2003                         break;
2004                 }
2005                 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2006         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2007                 os << "\\begin{minipage}[";
2008                 switch (GetVAlignment(cell)) {
2009                 case LYX_VALIGN_TOP:
2010                         os << "t";
2011                         break;
2012                 case LYX_VALIGN_CENTER:
2013                         os << "m";
2014                         break;
2015                 case LYX_VALIGN_BOTTOM:
2016                         os << "b";
2017                         break;
2018                 }
2019                 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2020                 ++ret;
2021         }
2022         return ret;
2023 }
2024
2025
2026 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2027 {
2028         int ret = 0;
2029
2030         // usual cells
2031         if (GetUsebox(cell) == BOX_PARBOX)
2032                 os << "}";
2033         else if (GetUsebox(cell) == BOX_MINIPAGE) {
2034                 os << "%\n\\end{minipage}";
2035                 ret += 2;
2036         }
2037         if (IsMultiColumn(cell)){
2038                 os << '}';
2039         }
2040         if (GetRotateCell(cell)) {
2041                 os << "%\n\\end{sideways}";
2042                 ++ret;
2043         }
2044         return ret;
2045 }
2046
2047
2048 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2049                                          bool fragile, bool fp) const
2050 {
2051         if (!is_long_tabular)
2052                 return 0;
2053
2054         int ret = 0;
2055         // output header info
2056         if (haveLTHead()) {
2057                 if (endhead.topDL) {
2058                         os << "\\hline\n";
2059                         ++ret;
2060                 }
2061                 for (int i = 0; i < rows_; ++i) {
2062                         if (row_info[i].endhead) {
2063                                 ret += TeXRow(os, i, buf, fragile, fp);
2064                         }
2065                 }
2066                 if (endhead.bottomDL) {
2067                         os << "\\hline\n";
2068                         ++ret;
2069                 }
2070                 os << "\\endhead\n";
2071                 ++ret;
2072                 if (endfirsthead.empty) {
2073                         os << "\\endfirsthead\n";
2074                         ++ret;
2075                 }
2076         }
2077         // output firstheader info
2078         if (haveLTFirstHead()) {
2079                 if (endfirsthead.topDL) {
2080                         os << "\\hline\n";
2081                         ++ret;
2082                 }
2083                 for (int i = 0; i < rows_; ++i) {
2084                         if (row_info[i].endfirsthead) {
2085                                 ret += TeXRow(os, i, buf, fragile, fp);
2086                         }
2087                 }
2088                 if (endfirsthead.bottomDL) {
2089                         os << "\\hline\n";
2090                         ++ret;
2091                 }
2092                 os << "\\endfirsthead\n";
2093                 ++ret;
2094         }
2095         // output footer info
2096         if (haveLTFoot()) {
2097                 if (endfoot.topDL) {
2098                         os << "\\hline\n";
2099                         ++ret;
2100                 }
2101                 for (int i = 0; i < rows_; ++i) {
2102                         if (row_info[i].endfoot) {
2103                                 ret += TeXRow(os, i, buf, fragile, fp);
2104                         }
2105                 }
2106                 if (endfoot.bottomDL) {
2107                         os << "\\hline\n";
2108                         ++ret;
2109                 }
2110                 os << "\\endfoot\n";
2111                 ++ret;
2112                 if (endlastfoot.empty) {
2113                         os << "\\endlastfoot\n";
2114                         ++ret;
2115                 }
2116         }
2117         // output lastfooter info
2118         if (haveLTLastFoot()) {
2119                 if (endlastfoot.topDL) {
2120                         os << "\\hline\n";
2121                         ++ret;
2122                 }
2123                 for (int i = 0; i < rows_; ++i) {
2124                         if (row_info[i].endlastfoot) {
2125                                 ret += TeXRow(os, i, buf, fragile, fp);
2126                         }
2127                 }
2128                 if (endlastfoot.bottomDL) {
2129                         os << "\\hline\n";
2130                         ++ret;
2131                 }
2132                 os << "\\endlastfoot\n";
2133                 ++ret;
2134         }
2135         return ret;
2136 }
2137
2138
2139 bool LyXTabular::isValidRow(int const row) const
2140 {
2141         if (!is_long_tabular)
2142                 return true;
2143         return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2144                         !row_info[row].endfoot && !row_info[row].endlastfoot);
2145 }
2146
2147
2148 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2149                        bool fragile, bool fp) const
2150 {
2151         int ret = 0;
2152         int cell = GetCellNumber(i, 0);
2153
2154         ret += TeXTopHLine(os, i);
2155         for (int j = 0; j < columns_; ++j) {
2156                 if (IsPartOfMultiColumn(i,j))
2157                         continue;
2158                 ret += TeXCellPreamble(os, cell);
2159                 InsetText * inset = GetCellInset(cell);
2160
2161                 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2162                         inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2163
2164                 if (rtl)
2165                         os << "\\R{";
2166                 ret += inset->latex(buf, os, fragile, fp);
2167                 if (rtl)
2168                         os << "}";
2169
2170                 ret += TeXCellPostamble(os, cell);
2171                 if (!IsLastCellInRow(cell)) { // not last cell in row
2172                         os << "&\n";
2173                         ++ret;
2174                 }
2175                 ++cell;
2176         }
2177         os << "\\\\\n";
2178         ++ret;
2179         ret += TeXBottomHLine(os, i);
2180         return ret;
2181 }
2182
2183
2184 int LyXTabular::latex(Buffer const * buf,
2185                                           ostream & os, bool fragile, bool fp) const
2186 {
2187         int ret = 0;
2188
2189         //+---------------------------------------------------------------------
2190         //+                      first the opening preamble                    +
2191         //+---------------------------------------------------------------------
2192
2193         if (rotate) {
2194                 os << "\\begin{sideways}\n";
2195                 ++ret;
2196         }
2197         if (is_long_tabular)
2198                 os << "\\begin{longtable}{";
2199         else
2200                 os << "\\begin{tabular}{";
2201         for (int i = 0; i < columns_; ++i) {
2202                 if (!column_info[i].align_special.empty()) {
2203                         os << column_info[i].align_special;
2204                 } else { 
2205                         if (column_info[i].left_line)
2206                                 os << '|';
2207                         if (!column_info[i].p_width.zero()) {
2208                                 switch (column_info[i].valignment) {
2209                                 case LYX_VALIGN_TOP:
2210                                         os << "p";
2211                                         break;
2212                                 case LYX_VALIGN_CENTER:
2213                                         os << "m";
2214                                         break;
2215                                 case LYX_VALIGN_BOTTOM:
2216                                         os << "b";
2217                                         break;
2218                         }
2219                                 os << "{"
2220                                    << column_info[i].p_width.asLatexString()
2221                                    << '}';
2222                         } else {
2223                                 switch (column_info[i].alignment) {
2224                                 case LYX_ALIGN_LEFT:
2225                                         os << 'l';
2226                                         break;
2227                                 case LYX_ALIGN_RIGHT:
2228                                 os << 'r';
2229                                 break;
2230                                 default:
2231                                         os << 'c';
2232                                         break;
2233                                 }
2234                         }
2235                         if (column_info[i].right_line)
2236                                 os << '|';
2237                 }
2238         }
2239         os << "}\n";
2240         ++ret;
2241
2242         ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2243         
2244         //+---------------------------------------------------------------------
2245         //+                      the single row and columns (cells)            +
2246         //+---------------------------------------------------------------------
2247
2248         for (int i = 0; i < rows_; ++i) {
2249                 if (isValidRow(i)) {
2250                         ret += TeXRow(os, i, buf, fragile, fp);
2251                         if (is_long_tabular && row_info[i].newpage) {
2252                                 os << "\\newpage\n";
2253                                 ++ret;
2254                         }
2255                 }
2256         }
2257
2258         //+---------------------------------------------------------------------
2259         //+                      the closing of the tabular                    +
2260         //+---------------------------------------------------------------------
2261
2262         if (is_long_tabular)
2263                 os << "\\end{longtable}";
2264         else
2265                 os << "\\end{tabular}";
2266         if (rotate) {
2267                 os << "\n\\end{sideways}";
2268                 ++ret;
2269         }
2270
2271         return ret;
2272 }
2273
2274
2275 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2276 {
2277         int ret = 0;
2278         int cell = GetFirstCellInRow(row);
2279         
2280         os << "<row>\n";
2281         for (int j = 0; j < columns_; ++j) {
2282                 if (IsPartOfMultiColumn(row, j))
2283                         continue;
2284
2285                 os << "<entry align=\"";
2286                 switch (GetAlignment(cell)) {
2287                 case LYX_ALIGN_LEFT:
2288                         os << "left";
2289                         break;
2290                 case LYX_ALIGN_RIGHT:
2291                         os << "right";
2292                         break;
2293                 default:
2294                         os << "center";
2295                         break;
2296                 }
2297
2298                 os << "\" valign=\"";
2299                 switch (GetVAlignment(cell)) {
2300                 case LYX_VALIGN_TOP:
2301                         os << "top";
2302                         break;
2303                 case LYX_VALIGN_BOTTOM:
2304                         os << "bottom";
2305                         break;
2306                 case LYX_VALIGN_CENTER:
2307                         os << "middle";
2308                 }
2309                 os << "\"";
2310
2311                 if (IsMultiColumn(cell)) {
2312                         os << " namest=\"col" << j << "\" ";
2313                         os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2314                 }
2315
2316                 os << ">";
2317                 ret += GetCellInset(cell)->docbook(buf, os);
2318                 os << "</entry>\n";
2319                 ++cell;
2320         }
2321         os << "</row>\n";
2322         return ret;
2323 }       
2324
2325
2326 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2327 {
2328         int ret = 0;
2329
2330         //+---------------------------------------------------------------------
2331         //+                      first the opening preamble                    +
2332         //+---------------------------------------------------------------------
2333
2334         os << "<tgroup cols=\"" << columns_
2335            << "\" colsep=\"1\" rowsep=\"1\">\n";
2336         
2337         for (int i = 0; i < columns_; ++i) {
2338                 os << "<colspec colname=\"col" << i << "\" align=\"";
2339                 switch (column_info[i].alignment) {
2340                 case LYX_ALIGN_LEFT:
2341                         os << "left";
2342                         break;
2343                 case LYX_ALIGN_RIGHT:
2344                         os << "right";
2345                         break;
2346                 default:
2347                         os << "center";
2348                         break;
2349                 }
2350                 os << "\">\n";
2351                 ++ret;
2352         }
2353
2354         //+---------------------------------------------------------------------
2355         //+                      Long Tabular case                             +
2356         //+---------------------------------------------------------------------
2357
2358 #warning Jose please have a look here I changed the longtable header/footer
2359 #warning ---- options so I had to disable the docbook code (Jug 20011219)
2360 #if 0
2361         if ( IsLongTabular() ) {
2362                 // Header
2363                 if(endhead.row || endfirsthead.row ) {
2364                         os << "<thead>\n";
2365                         if( endfirsthead.row ) {
2366                                 docbookRow( buf, os, endfirsthead.row - 1);
2367                         }
2368                         if( endhead.row && endhead.row != endfirsthead.row) {
2369                                 docbookRow(buf, os, endhead.row - 1);
2370                         }
2371                         os << "</thead>\n";
2372                 }
2373
2374                 // Footer
2375                 if( endfoot.row || endlastfoot.row ) {
2376                         os << "<tfoot>\n";
2377                         if( endfoot.row ) {
2378                                 docbookRow( buf, os, endfoot.row - 1);
2379                         }
2380                         if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2381                                 docbookRow( buf, os, endlastfoot.row - 1);
2382                         }
2383                         os << "</tfoot>\n";
2384                 }
2385         }
2386 #endif
2387         //+---------------------------------------------------------------------
2388         //+                      the single row and columns (cells)            +
2389         //+---------------------------------------------------------------------
2390
2391         os << "<tbody>\n";
2392         for (int i = 0; i < rows_; ++i) {
2393                 if(!IsLongTabular() || (
2394                    !row_info[i].endhead && !row_info[i].endfirsthead &&
2395                    !row_info[i].endfoot && !row_info[i].endlastfoot)) {
2396                         docbookRow( buf, os, i);
2397                 }
2398         }
2399         os << "</tbody>\n";
2400         //+---------------------------------------------------------------------
2401         //+                      the closing of the tabular                    +
2402         //+---------------------------------------------------------------------
2403
2404         os << "</tgroup>";
2405         ++ret;
2406
2407         return ret;
2408 }
2409
2410 //--
2411 // ASCII export function and helpers
2412 //--
2413 int LyXTabular::asciiTopHLine(ostream & os, int row,
2414                               vector<unsigned int> const & clen) const
2415 {
2416         int const fcell = GetFirstCellInRow(row);
2417         int const n = NumberOfCellsInRow(fcell) + fcell;
2418         int tmp = 0;
2419
2420         for (int i = fcell; i < n; ++i) {
2421                 if (TopLine(i)) {
2422                         ++tmp;
2423                         break;
2424                 }
2425         }
2426         if (!tmp)
2427                 return 0;
2428
2429         unsigned char ch;
2430         for (int i = fcell; i < n; ++i) {
2431                 if (TopLine(i)) {
2432                         if (LeftLine(i))
2433                                 os << "+-";
2434                         else
2435                                 os << "--";
2436                         ch = '-';
2437                 } else {
2438                         os << "  ";
2439                         ch = ' ';
2440                 }
2441                 int column = column_of_cell(i);
2442                 int len = clen[column];
2443                 while (IsPartOfMultiColumn(row, ++column))
2444                         len += clen[column] + 4;
2445                 os << string(len, ch);
2446                 if (TopLine(i)) {
2447                         if (RightLine(i))
2448                                 os << "-+";
2449                         else
2450                                 os << "--";
2451                 } else {
2452                         os << "  ";
2453                 }
2454         }
2455         os << endl;
2456         return 1;
2457 }
2458
2459
2460 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2461                                  vector<unsigned int> const & clen) const
2462 {
2463         int const fcell = GetFirstCellInRow(row);
2464         int const n = NumberOfCellsInRow(fcell) + fcell;
2465         int tmp = 0;
2466
2467         for (int i = fcell; i < n; ++i) {
2468                 if (BottomLine(i)) {
2469                         ++tmp;
2470                         break;
2471                 }
2472         }
2473         if (!tmp)
2474                 return 0;
2475
2476         unsigned char ch;
2477         for (int i = fcell; i < n; ++i) {
2478                 if (BottomLine(i)) {
2479                         if (LeftLine(i))
2480                                 os << "+-";
2481                         else
2482                                 os << "--";
2483                         ch = '-';
2484                 } else {
2485                         os << "  ";
2486                         ch = ' ';
2487                 }
2488                 int column = column_of_cell(i);
2489                 int len = clen[column];
2490                 while (IsPartOfMultiColumn(row, ++column))
2491                         len += clen[column] + 4;
2492                 os << string(len, ch);
2493                 if (BottomLine(i)) {
2494                         if (RightLine(i))
2495                                 os << "-+";
2496                         else
2497                                 os << "--";
2498                 } else {
2499                         os << "  ";
2500                 }
2501         }
2502         os << endl;
2503         return 1;
2504 }
2505
2506
2507 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2508                                int cell, int row, int column,
2509                                vector<unsigned int> const & clen,
2510                                bool onlydata) const
2511 {
2512         ostringstream sstr;
2513         int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2514
2515         if (onlydata) {
2516                 os << sstr.str();
2517                 return ret;
2518         }
2519         
2520         if (LeftLine(cell))
2521                 os << "| ";
2522         else
2523                 os << "  ";
2524
2525         unsigned int len1 = sstr.str().length();
2526         unsigned int len2 = clen[column];
2527         while (IsPartOfMultiColumn(row, ++column))
2528                 len2 += clen[column] + 4;
2529         len2 -= len1;
2530
2531         switch (GetAlignment(cell)) {
2532         default:
2533         case LYX_ALIGN_LEFT:
2534                 len1 = 0;
2535                 break;
2536         case LYX_ALIGN_RIGHT:
2537                 len1 = len2;
2538                 len2 = 0;
2539                 break;
2540         case LYX_ALIGN_CENTER:
2541                 len1 = len2 / 2;
2542                 len2 -= len1;
2543                 break;
2544         }
2545
2546         for (unsigned int i = 0; i < len1; ++i)
2547                 os << " ";
2548         os << sstr.str();
2549         for (unsigned int i = 0; i < len2; ++i)
2550                 os << " ";
2551         if (RightLine(cell))
2552                 os << " |";
2553         else
2554                 os << "  ";
2555
2556         return ret;
2557 }
2558
2559
2560 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2561                                           bool onlydata, unsigned char delim) const
2562 {
2563         int ret = 0;
2564
2565         //+---------------------------------------------------------------------
2566         //+           first calculate the width of the single columns          +
2567         //+---------------------------------------------------------------------
2568         vector<unsigned int> clen(columns_);
2569
2570         if (!onlydata) {
2571                 // first all non (real) multicolumn cells!
2572                 for (int j = 0; j < columns_; ++j) {
2573                         clen[j] = 0;
2574                         for (int i = 0; i < rows_; ++i) {
2575                                 int cell = GetCellNumber(i, j);
2576                                 if (IsMultiColumn(cell, true))
2577                                         continue;
2578                                 ostringstream sstr;
2579                                 GetCellInset(cell)->ascii(buf, sstr, 0);
2580                                 if (clen[j] < sstr.str().length())
2581                                         clen[j] = sstr.str().length();
2582                         }
2583                 }
2584                 // then all (real) multicolumn cells!
2585                 for (int j = 0; j < columns_; ++j) {
2586                         for (int i = 0; i < rows_; ++i) {
2587                                 int cell = GetCellNumber(i, j);
2588                                 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2589                                         continue;
2590                                 ostringstream sstr;
2591                                 GetCellInset(cell)->ascii(buf, sstr, 0);
2592                                 int len = int(sstr.str().length());
2593                                 int const n = cells_in_multicolumn(cell);
2594                                 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2595                                         len -= clen[k];
2596                                 if (len > int(clen[j + n - 1]))
2597                                         clen[j + n - 1] = len;
2598                         }
2599                 }
2600         }
2601         int cell = 0;
2602         for (int i = 0; i < rows_; ++i) {
2603                 if (!onlydata) {
2604                         if (asciiTopHLine(os, i, clen)) {
2605                                 for (int j = 0; j < depth; ++j)
2606                                         os << "  ";
2607                         }
2608                 }
2609                 for (int j = 0; j < columns_; ++j) {
2610                         if (IsPartOfMultiColumn(i,j))
2611                                 continue;
2612                         if (onlydata && j > 0)
2613                                 os << delim;
2614                         ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2615                         ++cell;
2616                 }
2617                 os << endl;
2618                 if (!onlydata) {
2619                         for (int j = 0; j < depth; ++j)
2620                                 os << "  ";
2621                         if (asciiBottomHLine(os, i, clen)) {
2622                                 for (int j = 0; j < depth; ++j)
2623                                         os << "  ";
2624                         }
2625                 }
2626         }
2627         return ret;
2628 }
2629 //--
2630 // end ascii export
2631 //--
2632
2633
2634 InsetText * LyXTabular::GetCellInset(int cell) const
2635 {
2636         cur_cell = cell;
2637         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2638 }
2639
2640
2641 InsetText * LyXTabular::GetCellInset(int row, int column) const
2642 {
2643         cur_cell = GetCellNumber(row, column);
2644         return & cell_info[row][column].inset;
2645 }
2646
2647
2648 void LyXTabular::Validate(LaTeXFeatures & features) const
2649 {
2650         if (IsLongTabular())
2651                 features.require("longtable");
2652         if (NeedRotating())
2653                 features.require("rotating");
2654         for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2655                 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2656                         features.require("array");
2657                 GetCellInset(cell)->validate(features);
2658         }
2659 }
2660
2661
2662 std::vector<string> const LyXTabular::getLabelList() const
2663 {
2664         std::vector<string> label_list;
2665         for (int i = 0; i < rows_; ++i)
2666                 for (int j = 0; j < columns_; ++j) {
2667                         std::vector<string> const l =
2668                                 GetCellInset(i, j)->getLabelList();
2669                         label_list.insert(label_list.end(),
2670                                           l.begin(), l.end());
2671                 }
2672         return label_list;
2673 }
2674
2675                         
2676 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2677 {
2678         Paragraph * par = GetCellInset(cell)->paragraph();
2679
2680         for (; par; par = par->next()) {
2681                 for (int i = 0; i < par->size(); ++i) {
2682                         if (par->getChar(i) == Paragraph::META_NEWLINE)
2683                                 return BOX_PARBOX;
2684                 }
2685         }
2686         return BOX_NONE;
2687 }