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