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