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