]> git.lyx.org Git - lyx.git/blob - src/tabular.C
More fixes to the autocollapsing of paragraphs.
[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 (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
311                         c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
312                 }
313                 if ((column + 1) == columns_ ||
314                         c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
315                         c_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)).zero());
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, LyXLength const & width)
734 {
735         bool flag = !width.zero();
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).zero(); // because of multicolumns!
744                 GetCellInset(c)->setAutoBreakRows(flag);
745         }
746         return true;
747 }
748
749
750 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
751 {
752         bool const flag = !width.zero();
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 LyXLength 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 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
854 {
855         return column_info[column_of_cell(cell)].p_width;
856 }
857
858
859 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
860 {
861         if (IsMultiColumn(cell))
862                 return cellinfo_of_cell(cell)->p_width;
863         return LyXLength();
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 template <>
1026 string const write_attribute(string const & name, LyXLength const & value)
1027 {
1028         return write_attribute(name, value.asString());
1029 }
1030 #else
1031
1032 string const write_attribute(string const & name, int value)
1033 {
1034         string str = " " + name + "=\"" + tostr(value) + "\"";
1035         return str;
1036 }
1037
1038
1039 string const write_attribute(string const & name, string const & value)
1040 {
1041         string str = " " + name + "=\"" + value + "\"";
1042         return str;
1043 }
1044
1045
1046 string const write_attribute(string const & name, bool value)
1047 {
1048         string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1049         return str;
1050 }
1051
1052
1053 string const write_attribute(string const & name, LyXLength const & value)
1054 {
1055         string str = " " + name + "=\"" + value.asString() + "\"";
1056         return str;
1057 }
1058 #endif
1059
1060
1061 template<>
1062 inline
1063 string const tostr(LyXAlignment const & num)
1064 {
1065         switch(num) {
1066         case LYX_ALIGN_NONE:
1067                 return "none";
1068         case LYX_ALIGN_BLOCK:
1069                 return "block";
1070         case LYX_ALIGN_LEFT:
1071                 return "left";
1072         case LYX_ALIGN_CENTER:
1073                 return "center";
1074         case LYX_ALIGN_RIGHT:
1075                 return "right";
1076         case LYX_ALIGN_LAYOUT:
1077                 return "layout";
1078         case LYX_ALIGN_SPECIAL:
1079                 return "special";
1080         }
1081         return string();
1082 }
1083
1084
1085 template<>
1086 inline
1087 string const tostr(LyXTabular::VAlignment const & num)
1088 {
1089         switch(num) {
1090         case LyXTabular::LYX_VALIGN_TOP:
1091                 return "top";
1092         case LyXTabular::LYX_VALIGN_CENTER:
1093                 return "center";
1094         case LyXTabular::LYX_VALIGN_BOTTOM:
1095                 return "bottom";
1096         }
1097         return string();
1098 }
1099
1100
1101 template<>
1102 inline
1103 string const tostr(LyXTabular::BoxType const & num)
1104 {
1105         switch(num) {
1106         case LyXTabular::BOX_NONE:
1107                 return "none";
1108         case LyXTabular::BOX_PARBOX:
1109                 return "parbox";
1110         case LyXTabular::BOX_MINIPAGE:
1111                 return "minipage";
1112         }
1113         return string();
1114 }
1115
1116
1117 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1118 {
1119         // header line
1120         os << "<lyxtabular"
1121            << write_attribute("version", 2)
1122            << write_attribute("rows", rows_)
1123            << write_attribute("columns", columns_)
1124            << ">\n";
1125         // global longtable options
1126         os << "<features"
1127            << write_attribute("rotate", tostr(rotate))
1128            << write_attribute("islongtable", tostr(is_long_tabular))
1129            << write_attribute("endhead", endhead.row)
1130            << write_attribute("endfirsthead", endfirsthead.row)
1131            << write_attribute("endfoot", endfoot.row)
1132            << write_attribute("endlastfoot", endlastfoot.row)
1133            << ">\n";
1134         for (int j = 0; j < columns_; ++j) {
1135                 os << "<column"
1136                    << write_attribute("alignment", tostr(column_info[j].alignment))
1137                    << write_attribute("valignment", tostr(column_info[j].valignment))
1138                    << write_attribute("leftline", tostr(column_info[j].left_line))
1139                    << write_attribute("rightline", tostr(column_info[j].right_line))
1140                    << write_attribute("width", column_info[j].p_width.asString())
1141                    << write_attribute("special", column_info[j].align_special)
1142                    << ">\n";
1143         }
1144         for (int i = 0; i < rows_; ++i) {
1145                 os << "<row"
1146                    << write_attribute("topline", tostr(row_info[i].top_line))
1147                    << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1148                    << write_attribute("newpage", tostr(row_info[i].newpage))
1149                    << ">\n";
1150                 for (int j = 0; j < columns_; ++j) {
1151                         os << "<cell"
1152                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1153                            << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1154                            << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1155                            << write_attribute("topline", tostr(cell_info[i][j].top_line))
1156                            << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1157                            << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1158                            << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1159                            << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1160                            << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1161                            << write_attribute("width", cell_info[i][j].p_width)
1162                            << write_attribute("special", cell_info[i][j].align_special)
1163                            << ">\n";
1164                         os << "\\begin_inset ";
1165                         cell_info[i][j].inset.write(buf, os);
1166                         os << "\n\\end_inset \n"
1167                            << "</cell>\n";
1168                 }
1169                 os << "</row>\n";
1170         }
1171         os << "</lyxtabular>\n";
1172 }
1173
1174
1175 namespace {
1176
1177 // I would have liked a fromstr template a lot better. (Lgb)
1178
1179 inline
1180 bool string2type(string const str, LyXAlignment & num)
1181 {
1182         if (str == "none")
1183                 num = LYX_ALIGN_NONE;
1184         else if (str == "block")
1185                 num = LYX_ALIGN_BLOCK;
1186         else if (str == "left")
1187                 num = LYX_ALIGN_LEFT;
1188         else if (str == "center")
1189                 num = LYX_ALIGN_CENTER;
1190         else if (str == "right")
1191                 num = LYX_ALIGN_RIGHT;
1192         else
1193                 return false;
1194         return true;
1195 }
1196
1197
1198 inline
1199 bool string2type(string const str, LyXTabular::VAlignment & num)
1200 {
1201         if (str == "top")
1202                 num = LyXTabular::LYX_VALIGN_TOP;
1203         else if (str == "center")
1204                 num = LyXTabular::LYX_VALIGN_CENTER;
1205         else if (str == "bottom")
1206                 num = LyXTabular::LYX_VALIGN_BOTTOM;
1207         else
1208                 return false;
1209         return true;
1210 }
1211
1212
1213 inline
1214 bool string2type(string const str, LyXTabular::BoxType & num)
1215 {
1216         if (str == "none")
1217                 num = LyXTabular::BOX_NONE;
1218         else if (str == "parbox")
1219                 num = LyXTabular::BOX_PARBOX;
1220         else if (str == "minipage")
1221                 num = LyXTabular::BOX_MINIPAGE;
1222         else
1223                 return false;
1224         return true;
1225 }
1226
1227
1228 inline
1229 bool string2type(string const str, bool & num)
1230 {
1231         if (str == "true")
1232                 num = true;
1233         else if (str == "false")
1234                 num = false;
1235         else
1236                 return false;
1237         return true;
1238 }
1239
1240
1241 bool getTokenValue(string const & str, const char * token, string & ret)
1242 {
1243         size_t token_length = strlen(token);
1244         string::size_type pos = str.find(token);
1245
1246         if (pos == string::npos || pos + token_length + 1 >= str.length()
1247                 || str[pos + token_length] != '=')
1248                 return false;
1249         ret.erase();
1250         pos += token_length + 1;
1251         char ch = str[pos];
1252         if ((ch != '"') && (ch != '\'')) { // only read till next space
1253                 ret += ch;
1254                 ch = ' ';
1255         }
1256         while ((pos < str.length() - 1) && (str[++pos] != ch))
1257                 ret += str[pos];
1258
1259         return true;
1260 }
1261
1262
1263 bool getTokenValue(string const & str, const char * token, int & num)
1264 {
1265         string tmp;
1266         if (!getTokenValue(str, token, tmp))
1267                 return false;
1268         num = strToInt(tmp);
1269         return true;
1270 }
1271
1272
1273 bool getTokenValue(string const & str, const char * token, LyXAlignment & 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::VAlignment & 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,
1293                                    LyXTabular::BoxType & num)
1294 {
1295         string tmp;
1296         if (!getTokenValue(str, token, tmp))
1297                 return false;
1298         return string2type(tmp, num);
1299 }
1300
1301
1302 bool getTokenValue(string const & str, const char * token, bool & flag)
1303 {
1304         string tmp;
1305         if (!getTokenValue(str, token, tmp))
1306                 return false;
1307         return string2type(tmp, flag);
1308 }    
1309
1310
1311 bool getTokenValue(string const & str, const char * token, LyXLength & len)
1312 {
1313         string tmp;
1314         if (!getTokenValue(str, token, tmp))
1315                 return false;
1316         return isValidLength(tmp, &len);
1317 }    
1318
1319
1320 inline
1321 void l_getline(istream & is, string & str)
1322 {
1323         str.erase();
1324         while (str.empty()) {
1325                 getline(is, str);
1326                 if (!str.empty() && str[str.length() - 1] == '\r')
1327                         str.erase(str.length() - 1);
1328         }
1329 }
1330
1331 } // namespace anon
1332
1333
1334 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1335 {
1336         string line;
1337         istream & is = lex.getStream();
1338
1339         l_getline(is, line);
1340         if (!prefixIs(line, "<lyxtabular ")
1341                 && !prefixIs(line, "<LyXTabular ")) {
1342                 OldFormatRead(lex, line);
1343                 return;
1344         }
1345
1346         int version;
1347         if (!getTokenValue(line, "version", version))
1348                 return;
1349         if (version == 1)
1350                 ReadOld(buf, is, lex, line);
1351         else if (version == 2)
1352                 ReadNew(buf, is, lex, line);
1353 }
1354
1355
1356 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1357                                                  LyXLex & lex, string const & l)
1358 {
1359         string line(l);
1360         int rows_arg;
1361         if (!getTokenValue(line, "rows", rows_arg))
1362                 return;
1363         int columns_arg;
1364         if (!getTokenValue(line, "columns", columns_arg))
1365                 return;
1366         Init(rows_arg, columns_arg);
1367         l_getline(is, line);
1368         if (!prefixIs(line, "<features")) {
1369                 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1370                         line << ")" << endl;
1371                 return;
1372         }
1373         getTokenValue(line, "rotate", rotate);
1374         getTokenValue(line, "islongtable", is_long_tabular);
1375         getTokenValue(line, "endhead", endhead.row);
1376         getTokenValue(line, "endfirsthead", endfirsthead.row);
1377         getTokenValue(line, "endfoot", endfoot.row);
1378         getTokenValue(line, "endlastfoot", endlastfoot.row);
1379         endhead.row = abs(endhead.row);
1380         endfirsthead.row = abs(endfirsthead.row);
1381         endfoot.row = abs(endfoot.row);
1382         endlastfoot.row = abs(endlastfoot.row);
1383         for (int j = 0; j < columns_; ++j) {
1384                 l_getline(is,line);
1385                 if (!prefixIs(line,"<column")) {
1386                         lyxerr << "Wrong tabular format (expected <column ...> got" <<
1387                                 line << ")" << endl;
1388                         return;
1389                 }
1390                 getTokenValue(line, "alignment", column_info[j].alignment);
1391                 getTokenValue(line, "valignment", column_info[j].valignment);
1392                 getTokenValue(line, "leftline", column_info[j].left_line);
1393                 getTokenValue(line, "rightline", column_info[j].right_line);
1394                 getTokenValue(line, "width", column_info[j].p_width);
1395                 getTokenValue(line, "special", column_info[j].align_special);
1396         }
1397
1398         for (int i = 0; i < rows_; ++i) {
1399                 l_getline(is, line);
1400                 if (!prefixIs(line, "<row")) {
1401                         lyxerr << "Wrong tabular format (expected <row ...> got" <<
1402                                 line << ")" << endl;
1403                         return;
1404                 }
1405                 getTokenValue(line, "topline", row_info[i].top_line);
1406                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1407                 getTokenValue(line, "newpage", row_info[i].newpage);
1408                 for (int j = 0; j < columns_; ++j) {
1409                         l_getline(is, line);
1410                         if (!prefixIs(line, "<cell")) {
1411                                 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1412                                         line << ")" << endl;
1413                                 return;
1414                         }
1415                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1416                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1417                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1418                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1419                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1420                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1421                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1422                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1423                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1424                         getTokenValue(line, "width", cell_info[i][j].p_width);
1425                         getTokenValue(line, "special", cell_info[i][j].align_special);
1426                         l_getline(is, line);
1427                         if (prefixIs(line, "\\begin_inset")) {
1428                                 cell_info[i][j].inset.read(buf, lex);
1429                                 l_getline(is, line);
1430                         }
1431                         if (!prefixIs(line, "</cell>")) {
1432                                 lyxerr << "Wrong tabular format (expected </cell> got" <<
1433                                         line << ")" << endl;
1434                                 return;
1435                         }
1436                 }
1437                 l_getline(is, line);
1438                 if (!prefixIs(line, "</row>")) {
1439                         lyxerr << "Wrong tabular format (expected </row> got" <<
1440                                 line << ")" << endl;
1441                         return;
1442                 }
1443         }
1444         while (!prefixIs(line, "</lyxtabular>")) {
1445                 l_getline(is, line);
1446         }
1447         set_row_column_number_info();
1448 }
1449
1450
1451 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1452 {
1453         int version;
1454         int i;
1455         int j;
1456         int rows_arg = 0;
1457         int columns_arg = 0;
1458         int is_long_tabular_arg = false;
1459         int rotate_arg = false;
1460         int a = -1;
1461         int b = -1;
1462         int c = -1;
1463         int d = -1;
1464         int e = 0;
1465         int f = 0;
1466         int g = 0;
1467         
1468         istream & is = lex.getStream();
1469         string s(fl);
1470         if (s.length() > 8)
1471                 version = lyx::atoi(s.substr(8, string::npos));
1472         else
1473                 version = 1;
1474
1475         vector<int> cont_row_info;
1476
1477         if (version < 5) {
1478                 lyxerr << "Tabular format < 5 is not supported anymore\n"
1479                         "Get an older version of LyX (< 1.1.x) for conversion!"
1480                            << endl;
1481                 Alert::alert(_("Warning:"),
1482                                    _("Tabular format < 5 is not supported anymore\n"),
1483                                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1484                 if (version > 2) {
1485                         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1486                            >> rotate_arg >> a >> b >> c >> d;
1487                 } else
1488                         is >> rows_arg >> columns_arg;
1489                 Init(rows_arg, columns_arg);
1490                 cont_row_info = vector<int>(rows_arg);
1491                 SetLongTabular(is_long_tabular_arg);
1492                 SetRotateTabular(rotate_arg);
1493                 string tmp;
1494                 for (i = 0; i < rows_; ++i) {
1495                         getline(is, tmp);
1496                         cont_row_info[i] = false;
1497                 }
1498                 for (i = 0; i < columns_; ++i) {
1499                         getline(is, tmp);
1500                 }
1501                 for (i = 0; i < rows_; ++i) {
1502                         for (j = 0; j < columns_; ++j) {
1503                                 getline(is, tmp);
1504                         }
1505                 }
1506         } else {
1507                 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1508                    >> rotate_arg >> a >> b >> c >> d;
1509                 Init(rows_arg, columns_arg);
1510                 cont_row_info = vector<int>(rows_arg);
1511                 SetLongTabular(is_long_tabular_arg);
1512                 SetRotateTabular(rotate_arg);
1513                 endhead.row = a + 1;
1514                 endfirsthead.row = b + 1;
1515                 endfoot.row = c + 1;
1516                 endlastfoot.row = d + 1;
1517                 for (i = 0; i < rows_; ++i) {
1518                         a = b = c = d = e = f = g = 0;
1519                         is >> a >> b >> c >> d;
1520                         row_info[i].top_line = a;
1521                         row_info[i].bottom_line = b;
1522                         cont_row_info[i] = c;
1523                         row_info[i].newpage = d;
1524                 }
1525                 for (i = 0; i < columns_; ++i) {
1526                         string s1;
1527                         string s2;
1528                         is >> a >> b >> c;
1529 #if 1
1530                         char ch; // skip '"'
1531                         is >> ch;
1532 #else
1533                         // ignore is buggy but we will use it later (Lgb)
1534                         is.ignore(); // skip '"'
1535 #endif    
1536                         getline(is, s1, '"');
1537 #if 1
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, s2, '"');
1544                         column_info[i].alignment = static_cast<LyXAlignment>(a);
1545                         column_info[i].left_line = b;
1546                         column_info[i].right_line = c;
1547                         column_info[i].p_width = LyXLength(s1);
1548                         column_info[i].align_special = s2;
1549                 }
1550                 for (i = 0; i < rows_; ++i) {
1551                         for (j = 0; j < columns_; ++j) {
1552                                 string s1;
1553                                 string s2;
1554                                 is >> a >> b >> c >> d >> e >> f >> g;
1555 #if 1
1556                                 char ch;
1557                                 is >> ch; // skip '"'
1558 #else
1559                                 // ignore is buggy but we will use it later (Lgb)
1560                                 is.ignore(); // skip '"'
1561 #endif
1562                                 getline(is, s1, '"');
1563 #if 1
1564                                 is >> ch; // skip '"'
1565 #else
1566                                 // ignore is buggy but we will use it later (Lgb)
1567                                 is.ignore(); // skip '"'
1568 #endif
1569                                 getline(is, s2, '"');
1570                                 cell_info[i][j].multicolumn = static_cast<char>(a);
1571                                 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1572                                 cell_info[i][j].top_line = static_cast<char>(c);
1573                                 cell_info[i][j].bottom_line = static_cast<char>(d);
1574                                 cell_info[i][j].left_line = column_info[j].left_line;
1575                                 cell_info[i][j].right_line = column_info[j].right_line;
1576                                 cell_info[i][j].rotate = static_cast<bool>(f);
1577                                 cell_info[i][j].usebox = static_cast<BoxType>(g);
1578                                 cell_info[i][j].align_special = s1;
1579                                 cell_info[i][j].p_width = LyXLength(s2);
1580                         }
1581                 }
1582         }
1583         set_row_column_number_info(true);
1584
1585         Paragraph * par = new Paragraph;
1586         Paragraph * return_par = 0;
1587
1588         string tmptok;
1589         int pos = 0;
1590         Paragraph::depth_type depth = 0;
1591         LyXFont font(LyXFont::ALL_INHERIT);
1592         font.setLanguage(owner_->bufferOwner()->getLanguage());
1593
1594         while (lex.isOK()) {
1595                 lex.nextToken();
1596                 string const token = lex.getString();
1597                 if (token.empty())
1598                         continue;
1599                 if (token == "\\layout"
1600                         || token == "\\end_float"
1601                         || token == "\\end_deeper") {
1602                         lex.pushToken(token);
1603 #ifndef NO_COMPABILITY
1604                         // Here we need to insert the inset_ert_contents into the last
1605                         // cell of the tabular.
1606                         owner_->bufferOwner()->insertErtContents(par, pos, font);
1607 #endif
1608                         break;
1609                 }
1610                 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1611                                                                                                                           return_par,
1612                                                                                                                           token, pos,
1613                                                                                                                           depth, font)) {
1614                         // the_end read
1615                         lex.pushToken(token);
1616                         break;
1617                 }
1618                 if (return_par) {
1619                         lex.printError("New Paragraph allocated! This should not happen!");
1620                         lex.pushToken(token);
1621                         delete par;
1622                         par = return_par;
1623                         break;
1624                 }
1625         }
1626         // now we have the par we should fill the insets with this!
1627         int cell = 0;
1628         InsetText * inset = GetCellInset(cell);
1629         int row;
1630
1631         for (int i = 0; i < par->size(); ++i) {
1632                 if (par->isNewline(i)) {
1633                         ++cell;
1634                         if (cell > numberofcells) {
1635                                 lyxerr << "Some error in reading old table format occured!" <<
1636                                         endl << "Terminating when reading cell[" << cell << "]!" <<
1637                                         endl;
1638                                 delete par;
1639                                 return;
1640                         }
1641                         row = row_of_cell(cell);
1642                         if (cont_row_info[row]) {
1643                                 DeleteRow(row);
1644                                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1645                                 while (!IsFirstCellInRow(--cell));
1646                         } else {
1647                                 inset = GetCellInset(cell);
1648                                 continue;
1649                         }
1650                         inset = GetCellInset(cell);
1651                         row = row_of_cell(cell);
1652                         if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1653                         {
1654                                 // insert a space instead
1655                                 par->erase(i);
1656                                 par->insertChar(i, ' ');
1657                         }
1658                 }
1659                 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1660                 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1661         }
1662         delete par;
1663         Reinit();
1664 }
1665
1666
1667 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1668 {
1669         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1670                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1671 }
1672
1673
1674 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1675 {
1676         int const row = row_of_cell(cell);
1677         int const column = column_of_cell(cell);
1678         return  &cell_info[row][column];
1679 }
1680
1681
1682 void LyXTabular::SetMultiColumn(int cell, int number)
1683 {
1684         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1685         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1686         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1687         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1688         for (number--; number > 0; --number) {
1689                 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1690         }
1691         set_row_column_number_info();
1692 }
1693
1694
1695 int LyXTabular::cells_in_multicolumn(int cell) const
1696 {
1697         int const row = row_of_cell(cell);
1698         int column = column_of_cell(cell);
1699         int result = 1;
1700         ++column;
1701         while ((column < columns_) &&
1702                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1703         {
1704                 ++result;
1705                 ++column;
1706         }
1707         return result;
1708 }
1709
1710
1711 int LyXTabular::UnsetMultiColumn(int cell)
1712 {
1713         int const row = row_of_cell(cell);
1714         int column = column_of_cell(cell);
1715         
1716         int result = 0;
1717         
1718         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1719                 cell_info[row][column].multicolumn = CELL_NORMAL;
1720                 ++column;
1721                 while ((column < columns_) &&
1722                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1723                 {
1724                         cell_info[row][column].multicolumn = CELL_NORMAL;
1725                         ++column;
1726                         ++result;
1727                 }
1728         }
1729         set_row_column_number_info();
1730         return result;
1731 }
1732
1733
1734 void LyXTabular::SetLongTabular(bool what)
1735 {
1736         is_long_tabular = what;
1737 }
1738
1739
1740 bool LyXTabular::IsLongTabular() const
1741 {
1742         return is_long_tabular;
1743 }
1744
1745
1746 void LyXTabular::SetRotateTabular(bool flag)
1747 {
1748         rotate = flag;
1749 }
1750
1751
1752 bool LyXTabular::GetRotateTabular() const
1753 {
1754         return rotate;
1755 }
1756
1757
1758 void LyXTabular::SetRotateCell(int cell, bool flag)
1759 {
1760         cellinfo_of_cell(cell)->rotate = flag;
1761 }
1762
1763
1764 bool LyXTabular::GetRotateCell(int cell) const
1765 {
1766         return cellinfo_of_cell(cell)->rotate;
1767 }
1768
1769
1770 bool LyXTabular::NeedRotating() const
1771 {
1772         if (rotate)
1773                 return true;
1774         for (int i = 0; i < rows_; ++i) {
1775                 for (int j = 0; j < columns_; ++j) {
1776                         if (cell_info[i][j].rotate)
1777                                 return true;
1778                 }
1779         }
1780         return false;
1781 }
1782
1783
1784 bool LyXTabular::IsLastCell(int cell) const
1785 {
1786         if ((cell + 1) < numberofcells)
1787                 return false;
1788         return true;
1789 }
1790
1791
1792 int LyXTabular::GetCellAbove(int cell) const
1793 {
1794         if (row_of_cell(cell) > 0)
1795                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1796         return cell;
1797 }
1798
1799
1800 int LyXTabular::GetCellBelow(int cell) const
1801 {
1802         if (row_of_cell(cell) + 1 < rows_)
1803                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1804         return cell;
1805 }
1806
1807
1808 int LyXTabular::GetLastCellAbove(int cell) const
1809 {
1810         if (row_of_cell(cell) <= 0)
1811                 return cell;
1812         if (!IsMultiColumn(cell))
1813                 return GetCellAbove(cell);
1814         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1815 }
1816
1817
1818 int LyXTabular::GetLastCellBelow(int cell) const
1819 {
1820         if (row_of_cell(cell) + 1 >= rows_)
1821                 return cell;
1822         if (!IsMultiColumn(cell))
1823                 return GetCellBelow(cell);
1824         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1825 }
1826
1827
1828 int LyXTabular::GetCellNumber(int row, int column) const
1829 {
1830 #if 0
1831         if (column >= columns_)
1832                 column = columns_ - 1;
1833         else if (column < 0)
1834                 column = 0;
1835         if (row >= rows_)
1836                 row = rows_ - 1;
1837         else if (row < 0)
1838                 row = 0;
1839 #else
1840         lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1841 #endif
1842         return cell_info[row][column].cellno;
1843 }
1844
1845
1846 void LyXTabular::SetUsebox(int cell, BoxType type)
1847 {
1848         cellinfo_of_cell(cell)->usebox = type;
1849 }
1850
1851
1852 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1853 {
1854         if (column_info[column_of_cell(cell)].p_width.zero() &&
1855                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1856                 return BOX_NONE;
1857         if (cellinfo_of_cell(cell)->usebox > 1)
1858                 return cellinfo_of_cell(cell)->usebox;
1859         return UseParbox(cell);
1860 }
1861
1862 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1863 {
1864         if (!ltt.row || (ltt.row > rows_))
1865                 return false;
1866         return (row == (ltt.row - 1));
1867 }
1868
1869
1870 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1871 {
1872         if (first) {
1873                 endfirsthead = hd;
1874         } else {
1875                 endhead = hd;
1876         }
1877 }
1878
1879
1880 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1881 {
1882         hd = endhead;
1883         return checkLTType(row, hd);
1884 }
1885
1886
1887 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1888 {
1889         hd = endfirsthead;
1890         return checkLTType(row, hd);
1891 }
1892
1893
1894 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1895 {
1896         if (last) {
1897                 endlastfoot = fd;
1898         } else {
1899                 endfoot = fd;
1900         }
1901 }
1902
1903
1904 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1905 {
1906         fd = endfoot;
1907         return checkLTType(row, fd);
1908 }
1909
1910
1911 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1912 {
1913         fd = endlastfoot;
1914         return checkLTType(row, fd);
1915 }
1916
1917
1918 void LyXTabular::SetLTNewPage(int cell, bool what)
1919 {
1920         row_info[row_of_cell(cell)].newpage = what;
1921 }
1922
1923
1924 bool LyXTabular::GetLTNewPage(int cell) const
1925 {
1926         return row_info[row_of_cell(cell)].newpage;
1927 }
1928
1929
1930 bool LyXTabular::SetAscentOfRow(int row, int height)
1931 {
1932         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1933                 return false;
1934         row_info[row].ascent_of_row = height;
1935         return true;
1936 }
1937
1938
1939 bool LyXTabular::SetDescentOfRow(int row, int height)
1940 {
1941         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1942                 return false;
1943         row_info[row].descent_of_row = height;
1944         return true;
1945 }
1946
1947
1948 int LyXTabular::GetAscentOfRow(int row) const
1949 {
1950         if (row >= rows_)
1951                 return 0;
1952         return row_info[row].ascent_of_row;
1953 }
1954
1955
1956 int LyXTabular::GetDescentOfRow(int row) const
1957 {
1958         if (row >= rows_)
1959                 return 0;
1960         return row_info[row].descent_of_row;
1961 }
1962
1963
1964 int LyXTabular::GetHeightOfTabular() const
1965 {
1966         int height = 0;
1967
1968         for (int row = 0; row < rows_; ++row)
1969                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1970                         GetAdditionalHeight(row);
1971         return height;
1972 }
1973
1974
1975 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1976 {
1977         if ((row >= rows_) || (column >= columns_))
1978                 return false;
1979         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1980 }
1981
1982
1983 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1984 {
1985         if ((row < 0) || (row >= rows_))
1986                 return 0;
1987
1988         int const fcell = GetFirstCellInRow(row);
1989         int const n = NumberOfCellsInRow(fcell) + fcell;
1990         int tmp = 0;
1991
1992         for (int i = fcell; i < n; ++i) {
1993                 if (TopLine(i))
1994                         ++tmp;
1995         }
1996         if (tmp == (n - fcell)){
1997                 os << "\\hline ";
1998         } else if (tmp) {
1999                 for (int i = fcell; i < n; ++i) {
2000                         if (TopLine(i)) {
2001                                 os << "\\cline{"
2002                                    << column_of_cell(i) + 1
2003                                    << '-'
2004                                    << right_column_of_cell(i) + 1
2005                                    << "} ";
2006                         }
2007                 }
2008         } else {
2009                 return 0;
2010         }
2011         os << "\n";
2012         return 1;
2013 }
2014
2015
2016 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
2017 {
2018         if ((row < 0) || (row >= rows_))
2019                 return 0;
2020
2021         int const fcell = GetFirstCellInRow(row);
2022         int const n = NumberOfCellsInRow(fcell) + fcell;
2023         int tmp = 0;
2024
2025         for (int i = fcell; i < n; ++i) {
2026                 if (BottomLine(i))
2027                         ++tmp;
2028         }
2029         if (tmp == (n-fcell)){
2030                 os << "\\hline";
2031         } else if (tmp) {
2032                 for (int i = fcell; i < n; ++i) {
2033                         if (BottomLine(i)) {
2034                                 os << "\\cline{"
2035                                    << column_of_cell(i) + 1
2036                                    << '-'
2037                                    << right_column_of_cell(i) + 1
2038                                    << "} ";
2039                         }
2040                 }
2041         } else {
2042                 return 0;
2043         }
2044         os << "\n";
2045         return 1;
2046 }
2047
2048
2049 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2050 {
2051         int ret = 0;
2052
2053         if (GetRotateCell(cell)) {
2054                 os << "\\begin{sideways}\n";
2055                 ++ret;
2056         }
2057         if (IsMultiColumn(cell)) {
2058                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2059                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2060                         os << cellinfo_of_cell(cell)->align_special << "}{";
2061                 } else {
2062                         if (LeftLine(cell) &&
2063                                 (IsFirstCellInRow(cell) || 
2064                                  (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2065                                   !RightLine(cell-1, true))))
2066                         {
2067                                 os << '|';
2068                         }
2069                         if (!GetPWidth(cell).zero()) {
2070                                 switch (GetVAlignment(cell)) {
2071                                 case LYX_VALIGN_TOP:
2072                                         os << "p";
2073                                         break;
2074                                 case LYX_VALIGN_CENTER:
2075                                         os << "m";
2076                                         break;
2077                                 case LYX_VALIGN_BOTTOM:
2078                                         os << "b";
2079                                         break;
2080                                 }
2081                                 os << "{" << GetPWidth(cell).asLatexString() << '}';
2082                         } else {
2083                                 switch (GetAlignment(cell)) {
2084                                 case LYX_ALIGN_LEFT:
2085                                         os << 'l';
2086                                         break;
2087                                 case LYX_ALIGN_RIGHT:
2088                                         os << 'r';
2089                                         break;
2090                                 default:
2091                                         os << 'c';
2092                                         break;
2093                                 }
2094                         }
2095                         if (RightLine(cell))
2096                                 os << '|';
2097                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2098                                 LeftLine(cell+1))
2099                                 os << '|';
2100                         os << "}{";
2101                 }
2102         }
2103         if (GetUsebox(cell) == BOX_PARBOX) {
2104                 os << "\\parbox[";
2105                 switch (GetVAlignment(cell)) {
2106                 case LYX_VALIGN_TOP:
2107                         os << "t";
2108                         break;
2109                 case LYX_VALIGN_CENTER:
2110                         os << "c";
2111                         break;
2112                 case LYX_VALIGN_BOTTOM:
2113                         os << "b";
2114                         break;
2115                 }
2116                 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2117         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2118                 os << "\\begin{minipage}[";
2119                 switch (GetVAlignment(cell)) {
2120                 case LYX_VALIGN_TOP:
2121                         os << "t";
2122                         break;
2123                 case LYX_VALIGN_CENTER:
2124                         os << "m";
2125                         break;
2126                 case LYX_VALIGN_BOTTOM:
2127                         os << "b";
2128                         break;
2129                 }
2130                 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2131                 ++ret;
2132         }
2133         return ret;
2134 }
2135
2136
2137 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2138 {
2139         int ret = 0;
2140
2141         // usual cells
2142         if (GetUsebox(cell) == BOX_PARBOX)
2143                 os << "}";
2144         else if (GetUsebox(cell) == BOX_MINIPAGE) {
2145                 os << "%\n\\end{minipage}";
2146                 ret += 2;
2147         }
2148         if (IsMultiColumn(cell)){
2149                 os << '}';
2150         }
2151         if (GetRotateCell(cell)) {
2152                 os << "%\n\\end{sideways}";
2153                 ++ret;
2154         }
2155         return ret;
2156 }
2157
2158
2159 int LyXTabular::Latex(Buffer const * buf,
2160                                           ostream & os, bool fragile, bool fp) const
2161 {
2162         int ret = 0;
2163         int cell = 0;
2164
2165         //+---------------------------------------------------------------------
2166         //+                      first the opening preamble                    +
2167         //+---------------------------------------------------------------------
2168
2169         if (rotate) {
2170                 os << "\\begin{sideways}\n";
2171                 ++ret;
2172         }
2173         if (is_long_tabular)
2174                 os << "\\begin{longtable}{";
2175         else
2176                 os << "\\begin{tabular}{";
2177         for (int i = 0; i < columns_; ++i) {
2178                 if (!column_info[i].align_special.empty()) {
2179                         os << column_info[i].align_special;
2180                 } else { 
2181                         if (column_info[i].left_line)
2182                                 os << '|';
2183                         if (!column_info[i].p_width.zero()) {
2184                                 switch (column_info[i].valignment) {
2185                                 case LYX_VALIGN_TOP:
2186                                         os << "p";
2187                                         break;
2188                                 case LYX_VALIGN_CENTER:
2189                                         os << "m";
2190                                         break;
2191                                 case LYX_VALIGN_BOTTOM:
2192                                         os << "b";
2193                                         break;
2194                         }
2195                                 os << "{"
2196                                    << column_info[i].p_width.asLatexString()
2197                                    << '}';
2198                         } else {
2199                                 switch (column_info[i].alignment) {
2200                                 case LYX_ALIGN_LEFT:
2201                                         os << 'l';
2202                                         break;
2203                                 case LYX_ALIGN_RIGHT:
2204                                 os << 'r';
2205                                 break;
2206                                 default:
2207                                         os << 'c';
2208                                         break;
2209                                 }
2210                         }
2211                         if (column_info[i].right_line)
2212                                 os << '|';
2213                 }
2214         }
2215         os << "}\n";
2216         ++ret;
2217
2218         //+---------------------------------------------------------------------
2219         //+                      the single row and columns (cells)            +
2220         //+---------------------------------------------------------------------
2221
2222         for (int i = 0; i < rows_; ++i) {
2223                 ret += TeXTopHLine(os, i);
2224 #warning Implement top double lines for LT Header/Footers
2225 #if 0
2226                 if (ret > bret) {
2227                         ret += TeXBottomHLine(os, i-1);
2228                         ret += TeXTopHLine(os, i);
2229                 }
2230 #endif
2231                 for (int j = 0; j < columns_; ++j) {
2232                         if (IsPartOfMultiColumn(i,j))
2233                                 continue;
2234                         ret += TeXCellPreamble(os, cell);
2235                         InsetText * inset = GetCellInset(cell);
2236
2237                         bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2238                                         inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2239
2240                         if (rtl)
2241                                 os << "\\R{";
2242                         ret += inset->latex(buf, os, fragile, fp);
2243                         if (rtl)
2244                                 os << "}";
2245
2246                         ret += TeXCellPostamble(os, cell);
2247                         if (!IsLastCellInRow(cell)) { // not last cell in row
2248                                 os << "&\n";
2249                                 ++ret;
2250                         }
2251                         ++cell;
2252                 }
2253                 os << "\\\\\n";
2254                 ++ret;
2255                 ret += TeXBottomHLine(os, i);
2256                 if (IsLongTabular()) {
2257                         if (i == (endhead.row - 1)) {
2258                                 if (endhead.bottomDL)
2259                                         ret += TeXBottomHLine(os, i);
2260                                 os << "\\endhead\n";
2261                                 ++ret;
2262                         }
2263                         if (i == (endfirsthead.row - 1)) {
2264                                 if (endfirsthead.bottomDL)
2265                                         ret += TeXBottomHLine(os, i);
2266                                 os << "\\endfirsthead\n";
2267                                 ++ret;
2268                         }
2269                         if (i == (endfoot.row - 1)) {
2270                                 if (endfoot.bottomDL)
2271                                         ret += TeXBottomHLine(os, i);
2272                                 os << "\\endfoot\n";
2273                                 ++ret;
2274                         }
2275                         if (i == (endlastfoot.row - 1)) {
2276                                 if (endlastfoot.bottomDL)
2277                                         ret += TeXBottomHLine(os, i);
2278                                 os << "\\endlastfoot\n";
2279                                 ++ret;
2280                         }
2281                         if (row_info[i].newpage) {
2282                                 os << "\\newpage\n";
2283                                 ++ret;
2284                         }
2285                 }
2286         }
2287
2288         //+---------------------------------------------------------------------
2289         //+                      the closing of the tabular                    +
2290         //+---------------------------------------------------------------------
2291
2292         if (is_long_tabular)
2293                 os << "\\end{longtable}";
2294         else
2295                 os << "\\end{tabular}";
2296         if (rotate) {
2297                 os << "\n\\end{sideways}";
2298                 ++ret;
2299         }
2300
2301         return ret;
2302 }
2303
2304
2305 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2306 {
2307         int ret = 0;
2308         int cell = GetFirstCellInRow(row);
2309         
2310         os << "<row>\n";
2311         for (int j = 0; j < columns_; ++j) {
2312                 if (IsPartOfMultiColumn(row, j))
2313                         continue;
2314
2315                 os << "<entry align=\"";
2316                 switch (GetAlignment(cell)) {
2317                 case LYX_ALIGN_LEFT:
2318                         os << "left";
2319                         break;
2320                 case LYX_ALIGN_RIGHT:
2321                         os << "right";
2322                         break;
2323                 default:
2324                         os << "center";
2325                         break;
2326                 }
2327
2328                 os << "\" valign=\"";
2329                 switch (GetVAlignment(cell)) {
2330                 case LYX_VALIGN_TOP:
2331                         os << "top";
2332                         break;
2333                 case LYX_VALIGN_BOTTOM:
2334                         os << "bottom";
2335                         break;
2336                 case LYX_VALIGN_CENTER:
2337                         os << "middle";
2338                 }
2339                 os << "\"";
2340
2341                 if (IsMultiColumn(cell)) {
2342                         os << " namest=\"col" << j << "\" ";
2343                         os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2344                 }
2345
2346                 os << ">";
2347                 ret += GetCellInset(cell)->docbook(buf, os);
2348                 os << "</entry>\n";
2349                 ++cell;
2350         }
2351         os << "</row>\n";
2352         return ret;
2353 }       
2354
2355
2356 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2357 {
2358         int ret = 0;
2359
2360         //+---------------------------------------------------------------------
2361         //+                      first the opening preamble                    +
2362         //+---------------------------------------------------------------------
2363
2364         os << "<tgroup cols=\"" << columns_
2365            << "\" colsep=\"1\" rowsep=\"1\">\n";
2366         
2367         for (int i = 0; i < columns_; ++i) {
2368                 os << "<colspec colname=\"col" << i << "\" align=\"";
2369                 switch (column_info[i].alignment) {
2370                 case LYX_ALIGN_LEFT:
2371                         os << "left";
2372                         break;
2373                 case LYX_ALIGN_RIGHT:
2374                         os << "right";
2375                         break;
2376                 default:
2377                         os << "center";
2378                         break;
2379                 }
2380                 os << "\">\n";
2381                 ++ret;
2382         }
2383
2384         //+---------------------------------------------------------------------
2385         //+                      Long Tabular case                             +
2386         //+---------------------------------------------------------------------
2387
2388         if ( IsLongTabular() ) {
2389                 // Header
2390                 if( endhead.row || endfirsthead.row ) {
2391                         os << "<thead>\n";
2392                         if( endfirsthead.row ) {
2393                                 docbookRow( buf, os, endfirsthead.row - 1);
2394                         }
2395                         if( endhead.row && endhead.row != endfirsthead.row) {
2396                                 docbookRow(buf, os, endhead.row - 1);
2397                         }
2398                         os << "</thead>\n";
2399                 }
2400
2401                 // Footer
2402                 if( endfoot.row || endlastfoot.row ) {
2403                         os << "<tfoot>\n";
2404                         if( endfoot.row ) {
2405                                 docbookRow( buf, os, endfoot.row - 1);
2406                         }
2407                         if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2408                                 docbookRow( buf, os, endlastfoot.row - 1);
2409                         }
2410                         os << "</tfoot>\n";
2411                 }
2412         }
2413         //+---------------------------------------------------------------------
2414         //+                      the single row and columns (cells)            +
2415         //+---------------------------------------------------------------------
2416
2417         os << "<tbody>\n";
2418         for (int i = 0; i < rows_; ++i) {
2419                 if(!IsLongTabular() || (
2420                    i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2421                    i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2422                         docbookRow( buf, os, i);
2423                 }
2424         }
2425         os << "</tbody>\n";
2426         //+---------------------------------------------------------------------
2427         //+                      the closing of the tabular                    +
2428         //+---------------------------------------------------------------------
2429
2430         os << "</tgroup>";
2431         ++ret;
2432
2433         return ret;
2434 }
2435
2436
2437 namespace {
2438
2439         inline
2440         void print_n_chars(ostream & os, unsigned char ch, int n)
2441         {
2442                 os << string(n, ch);
2443         }
2444
2445 } // namespace anon
2446
2447
2448 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2449                                                           vector<unsigned int> const & clen) const
2450 {
2451         int const fcell = GetFirstCellInRow(row);
2452         int const n = NumberOfCellsInRow(fcell) + fcell;
2453         int tmp = 0;
2454
2455         for (int i = fcell; i < n; ++i) {
2456                 if (TopLine(i)) {
2457                         ++tmp;
2458                         break;
2459                 }
2460         }
2461         if (!tmp)
2462                 return 0;
2463
2464         unsigned char ch;
2465         for (int i = fcell; i < n; ++i) {
2466                 if (TopLine(i)) {
2467                         if (LeftLine(i))
2468                                 os << "+-";
2469                         else
2470                                 os << "--";
2471                         ch = '-';
2472                 } else {
2473                         os << "  ";
2474                         ch = ' ';
2475                 }
2476                 int column = column_of_cell(i);
2477                 int len = clen[column];
2478                 while (IsPartOfMultiColumn(row, ++column))
2479                         len += clen[column] + 4;
2480                 print_n_chars(os, ch, len);
2481                 if (TopLine(i)) {
2482                         if (RightLine(i))
2483                                 os << "-+";
2484                         else
2485                                 os << "--";
2486                 } else {
2487                         os << "  ";
2488                 }
2489         }
2490         os << endl;
2491         return 1;
2492 }
2493
2494
2495 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2496                                                                  vector<unsigned int> const & clen) const
2497 {
2498         int const fcell = GetFirstCellInRow(row);
2499         int const n = NumberOfCellsInRow(fcell) + fcell;
2500         int tmp = 0;
2501
2502         for (int i = fcell; i < n; ++i) {
2503                 if (BottomLine(i)) {
2504                         ++tmp;
2505                         break;
2506                 }
2507         }
2508         if (!tmp)
2509                 return 0;
2510
2511         unsigned char ch;
2512         for (int i = fcell; i < n; ++i) {
2513                 if (BottomLine(i)) {
2514                         if (LeftLine(i))
2515                                 os << "+-";
2516                         else
2517                                 os << "--";
2518                         ch = '-';
2519                 } else {
2520                         os << "  ";
2521                         ch = ' ';
2522                 }
2523                 int column = column_of_cell(i);
2524                 int len = clen[column];
2525                 while (IsPartOfMultiColumn(row, ++column))
2526                         len += clen[column] + 4;
2527                 print_n_chars(os, ch, len);
2528                 if (BottomLine(i)) {
2529                         if (RightLine(i))
2530                                 os << "-+";
2531                         else
2532                                 os << "--";
2533                 } else {
2534                         os << "  ";
2535                 }
2536         }
2537         os << endl;
2538         return 1;
2539 }
2540
2541
2542 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2543                                int cell, int row, int column,
2544                                vector<unsigned int> const & clen) const
2545 {
2546         ostringstream sstr;
2547         int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2548
2549         if (LeftLine(cell))
2550                 os << "| ";
2551         else
2552                 os << "  ";
2553
2554         unsigned int len1 = sstr.str().length();
2555         unsigned int len2 = clen[column];
2556         while (IsPartOfMultiColumn(row, ++column))
2557                 len2 += clen[column] + 4;
2558         len2 -= len1;
2559
2560         switch (GetAlignment(cell)) {
2561         default:
2562         case LYX_ALIGN_LEFT:
2563                 len1 = 0;
2564                 break;
2565         case LYX_ALIGN_RIGHT:
2566                 len1 = len2;
2567                 len2 = 0;
2568                 break;
2569         case LYX_ALIGN_CENTER:
2570                 len1 = len2 / 2;
2571                 len2 -= len1;
2572                 break;
2573         }
2574
2575         for (unsigned int i = 0; i < len1; ++i)
2576                 os << " ";
2577         os << sstr.str();
2578         for (unsigned int i = 0; i < len2; ++i)
2579                 os << " ";
2580         if (RightLine(cell))
2581                 os << " |";
2582         else
2583                 os << "  ";
2584
2585         return ret;
2586 }
2587
2588
2589 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2590 {
2591         int ret = 0;
2592
2593         //+---------------------------------------------------------------------
2594         //+           first calculate the width of the single columns          +
2595         //+---------------------------------------------------------------------
2596         vector<unsigned int> clen(columns_);
2597
2598         // first all non (real) multicolumn cells!
2599         for (int j = 0; j < columns_; ++j) {
2600                 clen[j] = 0;
2601                 for (int i = 0; i < rows_; ++i) {
2602                         int cell = GetCellNumber(i, j);
2603                         if (IsMultiColumn(cell, true))
2604                                 continue;
2605                         ostringstream sstr;
2606                         GetCellInset(cell)->ascii(buf, sstr, 0);
2607                         if (clen[j] < sstr.str().length())
2608                                 clen[j] = sstr.str().length();
2609                 }
2610         }
2611         // then all (real) multicolumn cells!
2612         for (int j = 0; j < columns_; ++j) {
2613                 for (int i = 0; i < rows_; ++i) {
2614                         int cell = GetCellNumber(i, j);
2615                         if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2616                                 continue;
2617                         ostringstream sstr;
2618                         GetCellInset(cell)->ascii(buf, sstr, 0);
2619                         int len = int(sstr.str().length());
2620                         int const n = cells_in_multicolumn(cell);
2621                         for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2622                                 len -= clen[k];
2623                         if (len > int(clen[j + n - 1]))
2624                                 clen[j + n - 1] = len;
2625                 }
2626         }
2627         int cell = 0;
2628         for (int i = 0; i < rows_; ++i) {
2629                 AsciiTopHLine(os, i, clen);
2630                 for (int j = 0; j < columns_; ++j) {
2631                         if (IsPartOfMultiColumn(i,j))
2632                                 continue;
2633                         ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2634                         ++cell;
2635                 }
2636                 os << endl;
2637                 AsciiBottomHLine(os, i, clen);
2638         }
2639         return ret;
2640 }
2641
2642
2643 InsetText * LyXTabular::GetCellInset(int cell) const
2644 {
2645         cur_cell = cell;
2646         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2647 }
2648
2649
2650 InsetText * LyXTabular::GetCellInset(int row, int column) const
2651 {
2652         cur_cell = GetCellNumber(row, column);
2653         return & cell_info[row][column].inset;
2654 }
2655
2656
2657 void LyXTabular::Validate(LaTeXFeatures & features) const
2658 {
2659         if (IsLongTabular())
2660                 features.require("longtable");
2661         if (NeedRotating())
2662                 features.require("rotating");
2663         for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2664                 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2665                         features.require("array");
2666                 GetCellInset(cell)->validate(features);
2667         }
2668 }
2669
2670
2671 std::vector<string> const LyXTabular::getLabelList() const
2672 {
2673         std::vector<string> label_list;
2674         for (int i = 0; i < rows_; ++i)
2675                 for (int j = 0; j < columns_; ++j) {
2676                         std::vector<string> const l =
2677                                 GetCellInset(i, j)->getLabelList();
2678                         label_list.insert(label_list.end(),
2679                                           l.begin(), l.end());
2680                 }
2681         return label_list;
2682 }
2683
2684                         
2685 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2686 {
2687         Paragraph * par = GetCellInset(cell)->paragraph();
2688
2689         for (; par; par = par->next()) {
2690                 for (int i = 0; i < par->size(); ++i) {
2691                         if (par->getChar(i) == Paragraph::META_NEWLINE)
2692                                 return BOX_PARBOX;
2693                 }
2694         }
2695         return BOX_NONE;
2696 }