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