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