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