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