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