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