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