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