]> git.lyx.org Git - lyx.git/blob - src/tabular.C
*** empty log message ***
[lyx.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "lyx_gui_misc.h"
27 #include "buffer.h"
28 #include "BufferView.h"
29 #include "Painter.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "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 // or perhaps not...
939 #if 1
940 template<class T>
941 string const write_attribute(string const & name, T const & t)
942 {
943         string str = " " + name + "=\"" + tostr(t) + "\"";
944         return str;
945 }
946
947 template <>
948 string const write_attribute(string const & name, bool const & b)
949 {
950         return write_attribute(name, int(b));
951 }
952
953 #else
954
955 string const write_attribute(string const & name, int value)
956 {
957         string str = " " + name + "=\"" + tostr(value) + "\"";
958         return str;
959 }
960
961
962 string const write_attribute(string const & name, string const & value)
963 {
964         string str = " " + name + "=\"" + value + "\"";
965         return str;
966 }
967
968
969 string const write_attribute(string const & name, bool value)
970 {
971         string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
972         return str;
973 }
974 #endif
975
976
977 template<>
978 inline
979 string const tostr(LyXAlignment const & num)
980 {
981         switch(num) {
982         case LYX_ALIGN_NONE:
983                 return "none";
984         case LYX_ALIGN_BLOCK:
985                 return "block";
986         case LYX_ALIGN_LEFT:
987                 return "left";
988         case LYX_ALIGN_CENTER:
989                 return "center";
990         case LYX_ALIGN_RIGHT:
991                 return "right";
992         case LYX_ALIGN_LAYOUT:
993                 return "layout";
994         case LYX_ALIGN_SPECIAL:
995                 return "special";
996         }
997         return string();
998 }
999
1000
1001 template<>
1002 inline
1003 string const tostr(LyXTabular::VAlignment const & num)
1004 {
1005         switch(num) {
1006         case LyXTabular::LYX_VALIGN_TOP:
1007                 return "top";
1008         case LyXTabular::LYX_VALIGN_CENTER:
1009                 return "center";
1010         case LyXTabular::LYX_VALIGN_BOTTOM:
1011                 return "bottom";
1012         }
1013         return string();
1014 }
1015
1016
1017 template<>
1018 inline
1019 string const tostr(LyXTabular::BoxType const & num)
1020 {
1021         switch(num) {
1022         case LyXTabular::BOX_NONE:
1023                 return "none";
1024         case LyXTabular::BOX_PARBOX:
1025                 return "parbox";
1026         case LyXTabular::BOX_MINIPAGE:
1027                 return "minipage";
1028         }
1029         return string();
1030 }
1031
1032
1033 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1034 {
1035         // header line
1036         os << "<lyxtabular"
1037            << write_attribute("version", 2)
1038            << write_attribute("rows", rows_)
1039            << write_attribute("columns", columns_)
1040            << ">\n";
1041         // global longtable options
1042         os << "<features"
1043            << write_attribute("rotate", tostr(rotate))
1044            << write_attribute("islongtable", tostr(is_long_tabular))
1045            << write_attribute("endhead", endhead)
1046            << write_attribute("endfirsthead", endfirsthead)
1047            << write_attribute("endfoot", endfoot)
1048            << write_attribute("endlastfoot", endlastfoot)
1049            << ">\n";
1050         for (int j = 0; j < columns_; ++j) {
1051                 os << "<column"
1052                    << write_attribute("alignment", tostr(column_info[j].alignment))
1053                    << write_attribute("valignment", tostr(column_info[j].valignment))
1054                    << write_attribute("leftline", tostr(column_info[j].left_line))
1055                    << write_attribute("rightline", tostr(column_info[j].right_line))
1056                    << write_attribute("width",
1057                                                           VSpace(column_info[j].p_width)
1058                                                           .asLyXCommand())
1059                    << write_attribute("special", column_info[j].align_special)
1060                    << ">\n";
1061         }
1062         for (int i = 0; i < rows_; ++i) {
1063                 os << "<row"
1064                    << write_attribute("topline", tostr(row_info[i].top_line))
1065                    << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1066                    << write_attribute("newpage", tostr(row_info[i].newpage))
1067                    << ">\n";
1068                 for (int j = 0; j < columns_; ++j) {
1069                         os << "<cell"
1070                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1071                            << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1072                            << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1073                            << write_attribute("topline", tostr(cell_info[i][j].top_line))
1074                            << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1075                            << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1076                            << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1077                            << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1078                            << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1079                            << write_attribute("width", cell_info[i][j].p_width)
1080                            << write_attribute("special", cell_info[i][j].align_special)
1081                            << ">\n";
1082                         os << "\\begin_inset ";
1083                         cell_info[i][j].inset.write(buf, os);
1084                         os << "\n\\end_inset \n"
1085                            << "</cell>\n";
1086                 }
1087                 os << "</row>\n";
1088         }
1089         os << "</lyxtabular>\n";
1090 }
1091
1092
1093 namespace {
1094
1095 // I would have liked a fromstr template a lot better. (Lgb)
1096
1097 inline
1098 bool string2type(string const str, LyXAlignment & num)
1099 {
1100         if (str == "none")
1101                 num = LYX_ALIGN_NONE;
1102         else if (str == "block")
1103                 num = LYX_ALIGN_BLOCK;
1104         else if (str == "left")
1105                 num = LYX_ALIGN_LEFT;
1106         else if (str == "center")
1107                 num = LYX_ALIGN_CENTER;
1108         else if (str == "right")
1109                 num = LYX_ALIGN_RIGHT;
1110         else
1111                 return false;
1112         return true;
1113 }
1114
1115
1116 inline
1117 bool string2type(string const str, LyXTabular::VAlignment & num)
1118 {
1119         if (str == "top")
1120                 num = LyXTabular::LYX_VALIGN_TOP;
1121         else if (str == "center")
1122                 num = LyXTabular::LYX_VALIGN_CENTER;
1123         else if (str == "bottom")
1124                 num = LyXTabular::LYX_VALIGN_BOTTOM;
1125         else
1126                 return false;
1127         return true;
1128 }
1129
1130
1131 inline
1132 bool string2type(string const str, LyXTabular::BoxType & num)
1133 {
1134         if (str == "none")
1135                 num = LyXTabular::BOX_NONE;
1136         else if (str == "parbox")
1137                 num = LyXTabular::BOX_PARBOX;
1138         else if (str == "minipage")
1139                 num = LyXTabular::BOX_MINIPAGE;
1140         else
1141                 return false;
1142         return true;
1143 }
1144
1145
1146 inline
1147 bool string2type(string const str, bool & num)
1148 {
1149         if (str == "true")
1150                 num = true;
1151         else if (str == "false")
1152                 num = false;
1153         else
1154                 return false;
1155         return true;
1156 }
1157
1158
1159 bool getTokenValue(string const & str, const char * token, string & ret)
1160 {
1161         size_t token_length = strlen(token);
1162         string::size_type pos = str.find(token);
1163
1164         if (pos == string::npos || pos + token_length + 1 >= str.length()
1165                 || str[pos + token_length] != '=')
1166                 return false;
1167         ret.erase();
1168         pos += token_length + 1;
1169         char ch = str[pos];
1170         if ((ch != '"') && (ch != '\'')) { // only read till next space
1171                 ret += ch;
1172                 ch = ' ';
1173         }
1174         while((pos < str.length() - 1) && (str[++pos] != ch))
1175                 ret += str[pos];
1176
1177         return true;
1178 }
1179
1180
1181 bool getTokenValue(string const & str, const char * token, int & num)
1182 {
1183         string tmp;
1184         if (!getTokenValue(str, token, tmp))
1185                 return false;
1186         num = strToInt(tmp);
1187         return true;
1188 }
1189
1190
1191 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1192 {
1193         string tmp;
1194         if (!getTokenValue(str, token, tmp))
1195                 return false;
1196         return string2type(tmp, num);
1197 }
1198
1199
1200 bool getTokenValue(string const & str, const char * token,
1201                                    LyXTabular::VAlignment & num)
1202 {
1203         string tmp;
1204         if (!getTokenValue(str, token, tmp))
1205                 return false;
1206         return string2type(tmp, num);
1207 }
1208
1209
1210 bool getTokenValue(string const & str, const char * token,
1211                                    LyXTabular::BoxType & num)
1212 {
1213         string tmp;
1214         if (!getTokenValue(str, token, tmp))
1215                 return false;
1216         return string2type(tmp, num);
1217 }
1218
1219
1220 bool getTokenValue(string const & str, const char * token, bool & flag)
1221 {
1222         string tmp;
1223         if (!getTokenValue(str, token, tmp))
1224                 return false;
1225         return string2type(tmp, flag);
1226 }    
1227
1228
1229 inline
1230 void l_getline(istream & is, string & str)
1231 {
1232         str.erase();
1233         while (str.empty()) {
1234                 getline(is, str);
1235                 if (!str.empty() && str[str.length() - 1] == '\r')
1236                         str.erase(str.length() - 1);
1237         }
1238 }
1239
1240 } // namespace anon
1241
1242
1243 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1244 {
1245         string line;
1246         istream & is = lex.getStream();
1247
1248         l_getline(is, line);
1249         if (!prefixIs(line, "<lyxtabular ")
1250                 && !prefixIs(line, "<LyXTabular ")) {
1251                 OldFormatRead(lex, line);
1252                 return;
1253         }
1254
1255         int version;
1256         if (!getTokenValue(line, "version", version))
1257                 return;
1258         if (version == 1)
1259                 ReadOld(buf, is, lex, line);
1260         else if (version == 2)
1261                 ReadNew(buf, is, lex, line);
1262 }
1263
1264
1265 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1266                                                  LyXLex & lex, string const & l)
1267 {
1268         string line(l);
1269         int rows_arg;
1270         if (!getTokenValue(line, "rows", rows_arg))
1271                 return;
1272         int columns_arg;
1273         if (!getTokenValue(line, "columns", columns_arg))
1274                 return;
1275         Init(rows_arg, columns_arg);
1276         l_getline(is, line);
1277         if (!prefixIs(line, "<features")) {
1278                 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1279                         line << ")" << endl;
1280                 return;
1281         }
1282         getTokenValue(line, "rotate", rotate);
1283         getTokenValue(line, "islongtable", is_long_tabular);
1284         getTokenValue(line, "endhead", endhead);
1285         getTokenValue(line, "endfirsthead", endfirsthead);
1286         getTokenValue(line, "endfoot", endfoot);
1287         getTokenValue(line, "endlastfoot", endlastfoot);
1288
1289         for (int j = 0; j < columns_; ++j) {
1290                 l_getline(is,line);
1291                 if (!prefixIs(line,"<column")) {
1292                         lyxerr << "Wrong tabular format (expected <column ...> got" <<
1293                                 line << ")" << endl;
1294                         return;
1295                 }
1296                 getTokenValue(line, "alignment", column_info[j].alignment);
1297                 getTokenValue(line, "valignment", column_info[j].valignment);
1298                 getTokenValue(line, "leftline", column_info[j].left_line);
1299                 getTokenValue(line, "rightline", column_info[j].right_line);
1300                 getTokenValue(line, "width", column_info[j].p_width);
1301                 getTokenValue(line, "special", column_info[j].align_special);
1302         }
1303
1304         for (int i = 0; i < rows_; ++i) {
1305                 l_getline(is, line);
1306                 if (!prefixIs(line, "<row")) {
1307                         lyxerr << "Wrong tabular format (expected <row ...> got" <<
1308                                 line << ")" << endl;
1309                         return;
1310                 }
1311                 getTokenValue(line, "topline", row_info[i].top_line);
1312                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1313                 getTokenValue(line, "newpage", row_info[i].newpage);
1314                 for (int j = 0; j < columns_; ++j) {
1315                         l_getline(is, line);
1316                         if (!prefixIs(line, "<cell")) {
1317                                 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1318                                         line << ")" << endl;
1319                                 return;
1320                         }
1321                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1322                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1323                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1324                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1325                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1326                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1327                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1328                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1329                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1330                         getTokenValue(line, "width", cell_info[i][j].p_width);
1331                         getTokenValue(line, "special", cell_info[i][j].align_special);
1332                         l_getline(is, line);
1333                         if (prefixIs(line, "\\begin_inset")) {
1334                                 cell_info[i][j].inset.read(buf, lex);
1335                                 l_getline(is, line);
1336                         }
1337                         if (!prefixIs(line, "</cell>")) {
1338                                 lyxerr << "Wrong tabular format (expected </cell> got" <<
1339                                         line << ")" << endl;
1340                                 return;
1341                         }
1342                 }
1343                 l_getline(is, line);
1344                 if (!prefixIs(line, "</row>")) {
1345                         lyxerr << "Wrong tabular format (expected </row> got" <<
1346                                 line << ")" << endl;
1347                         return;
1348                 }
1349         }
1350         while (!prefixIs(line, "</lyxtabular>")) {
1351                 l_getline(is, line);
1352         }
1353         set_row_column_number_info();
1354 }
1355
1356
1357 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1358 {
1359         int version;
1360         int i;
1361         int j;
1362         int rows_arg = 0;
1363         int columns_arg = 0;
1364         int is_long_tabular_arg = false;
1365         int rotate_arg = false;
1366         int a = -1;
1367         int b = -1;
1368         int c = -1;
1369         int d = -1;
1370         int e = 0;
1371         int f = 0;
1372         int g = 0;
1373         
1374         istream & is = lex.getStream();
1375         string s(fl);
1376         if (s.length() > 8)
1377                 version = lyx::atoi(s.substr(8, string::npos));
1378         else
1379                 version = 1;
1380
1381         vector<int> cont_row_info;
1382
1383         if (version < 5) {
1384                 lyxerr << "Tabular format < 5 is not supported anymore\n"
1385                         "Get an older version of LyX (< 1.1.x) for conversion!"
1386                            << endl;
1387                 WriteAlert(_("Warning:"),
1388                                    _("Tabular format < 5 is not supported anymore\n"),
1389                                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1390                 if (version > 2) {
1391                         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1392                            >> rotate_arg >> a >> b >> c >> d;
1393                 } else
1394                         is >> rows_arg >> columns_arg;
1395                 Init(rows_arg, columns_arg);
1396                 cont_row_info = vector<int>(rows_arg);
1397                 SetLongTabular(is_long_tabular_arg);
1398                 SetRotateTabular(rotate_arg);
1399                 string tmp;
1400                 for (i = 0; i < rows_; ++i) {
1401                         getline(is, tmp);
1402                         cont_row_info[i] = false;
1403                 }
1404                 for (i = 0; i < columns_; ++i) {
1405                         getline(is, tmp);
1406                 }
1407                 for (i = 0; i < rows_; ++i) {
1408                         for (j = 0; j < columns_; ++j) {
1409                                 getline(is, tmp);
1410                         }
1411                 }
1412         } else {
1413                 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1414                    >> rotate_arg >> a >> b >> c >> d;
1415                 Init(rows_arg, columns_arg);
1416                 cont_row_info = vector<int>(rows_arg);
1417                 SetLongTabular(is_long_tabular_arg);
1418                 SetRotateTabular(rotate_arg);
1419                 endhead = a + 1;
1420                 endfirsthead = b + 1;
1421                 endfoot = c + 1;
1422                 endlastfoot = d + 1;
1423                 for (i = 0; i < rows_; ++i) {
1424                         a = b = c = d = e = f = g = 0;
1425                         is >> a >> b >> c >> d;
1426                         row_info[i].top_line = a;
1427                         row_info[i].bottom_line = b;
1428                         cont_row_info[i] = c;
1429                         row_info[i].newpage = d;
1430                 }
1431                 for (i = 0; i < columns_; ++i) {
1432                         string s1;
1433                         string s2;
1434                         is >> a >> b >> c;
1435 #if 1
1436                         char ch; // skip '"'
1437                         is >> ch;
1438 #else
1439                         // ignore is buggy but we will use it later (Lgb)
1440                         is.ignore(); // skip '"'
1441 #endif    
1442                         getline(is, s1, '"');
1443 #if 1
1444                         is >> ch; // skip '"'
1445 #else
1446                         // ignore is buggy but we will use it later (Lgb)
1447                         is.ignore(); // skip '"'
1448 #endif
1449                         getline(is, s2, '"');
1450                         column_info[i].alignment = static_cast<LyXAlignment>(a);
1451                         column_info[i].left_line = b;
1452                         column_info[i].right_line = c;
1453                         column_info[i].p_width = s1;
1454                         column_info[i].align_special = s2;
1455                 }
1456                 for (i = 0; i < rows_; ++i) {
1457                         for (j = 0; j < columns_; ++j) {
1458                                 string s1;
1459                                 string s2;
1460                                 is >> a >> b >> c >> d >> e >> f >> g;
1461 #if 1
1462                                 char ch;
1463                                 is >> ch; // skip '"'
1464 #else
1465                                 // ignore is buggy but we will use it later (Lgb)
1466                                 is.ignore(); // skip '"'
1467 #endif
1468                                 getline(is, s1, '"');
1469 #if 1
1470                                 is >> ch; // skip '"'
1471 #else
1472                                 // ignore is buggy but we will use it later (Lgb)
1473                                 is.ignore(); // skip '"'
1474 #endif
1475                                 getline(is, s2, '"');
1476                                 cell_info[i][j].multicolumn = static_cast<char>(a);
1477                                 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1478                                 cell_info[i][j].top_line = static_cast<char>(c);
1479                                 cell_info[i][j].bottom_line = static_cast<char>(d);
1480                                 cell_info[i][j].left_line = column_info[j].left_line;
1481                                 cell_info[i][j].right_line = column_info[j].right_line;
1482                                 cell_info[i][j].rotate = static_cast<bool>(f);
1483                                 cell_info[i][j].usebox = static_cast<BoxType>(g);
1484                                 cell_info[i][j].align_special = s1;
1485                                 cell_info[i][j].p_width = s2;
1486                         }
1487                 }
1488         }
1489         set_row_column_number_info(true);
1490
1491         Paragraph * par = new Paragraph;
1492         Paragraph * return_par = 0;
1493
1494         string tmptok;
1495         int pos = 0;
1496         Paragraph::depth_type depth = 0;
1497         LyXFont font(LyXFont::ALL_INHERIT);
1498         font.setLanguage(owner_->bufferOwner()->getLanguage());
1499
1500         while (lex.IsOK()) {
1501                 lex.nextToken();
1502                 string const token = lex.GetString();
1503                 if (token.empty())
1504                         continue;
1505                 if (token == "\\layout"
1506                         || token == "\\end_float"
1507                         || token == "\\end_deeper") {
1508                         lex.pushToken(token);
1509                         // Here we need to insert the inset_ert_contents into the last
1510                         // cell of the tabular.
1511                         owner_->bufferOwner()->insertErtContents(par, pos, font);
1512                         
1513                         break;
1514                 }
1515                 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1516                                                                                                                           return_par,
1517                                                                                                                           token, pos,
1518                                                                                                                           depth, font)) {
1519                         // the_end read
1520                         lex.pushToken(token);
1521                         break;
1522                 }
1523                 if (return_par) {
1524                         lex.printError("New Paragraph allocated! This should not happen!");
1525                         lex.pushToken(token);
1526                         delete par;
1527                         par = return_par;
1528                         break;
1529                 }
1530         }
1531         // now we have the par we should fill the insets with this!
1532         int cell = 0;
1533         InsetText * inset = GetCellInset(cell);
1534         int row;
1535
1536         for (int i = 0; i < par->size(); ++i) {
1537                 if (par->isNewline(i)) {
1538                         ++cell;
1539                         if (cell > numberofcells) {
1540                                 lyxerr << "Some error in reading old table format occured!" <<
1541                                         endl << "Terminating when reading cell[" << cell << "]!" <<
1542                                         endl;
1543                                 delete par;
1544                                 return;
1545                         }
1546                         row = row_of_cell(cell);
1547                         if (cont_row_info[row]) {
1548                                 DeleteRow(row);
1549                                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1550                                 while(!IsFirstCellInRow(--cell));
1551                         } else {
1552                                 inset = GetCellInset(cell);
1553                                 continue;
1554                         }
1555                         inset = GetCellInset(cell);
1556                         row = row_of_cell(cell);
1557                         if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1558                         {
1559                                 // insert a space instead
1560                                 par->erase(i);
1561                                 par->insertChar(i, ' ');
1562                         }
1563                 }
1564                 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1565                 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1566         }
1567         delete par;
1568         Reinit();
1569 }
1570
1571
1572 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1573 {
1574         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1575                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1576 }
1577
1578
1579 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1580 {
1581         int const row = row_of_cell(cell);
1582         int const column = column_of_cell(cell);
1583         return  &cell_info[row][column];
1584 }
1585
1586
1587 void LyXTabular::SetMultiColumn(int cell, int number)
1588 {
1589         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1590         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1591         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1592         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1593         for (number--; number > 0; --number) {
1594                 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1595         }
1596         set_row_column_number_info();
1597 }
1598
1599
1600 int LyXTabular::cells_in_multicolumn(int cell) const
1601 {
1602         int const row = row_of_cell(cell);
1603         int column = column_of_cell(cell);
1604         int result = 1;
1605         ++column;
1606         while ((column < columns_) &&
1607                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1608         {
1609                 ++result;
1610                 ++column;
1611         }
1612         return result;
1613 }
1614
1615
1616 int LyXTabular::UnsetMultiColumn(int cell)
1617 {
1618         int const row = row_of_cell(cell);
1619         int column = column_of_cell(cell);
1620         
1621         int result = 0;
1622         
1623         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1624                 cell_info[row][column].multicolumn = CELL_NORMAL;
1625                 ++column;
1626                 while ((column < columns_) &&
1627                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1628                 {
1629                         cell_info[row][column].multicolumn = CELL_NORMAL;
1630                         ++column;
1631                         ++result;
1632                 }
1633         }
1634         set_row_column_number_info();
1635         return result;
1636 }
1637
1638
1639 void LyXTabular::SetLongTabular(bool what)
1640 {
1641         is_long_tabular = what;
1642 }
1643
1644
1645 bool LyXTabular::IsLongTabular() const
1646 {
1647         return is_long_tabular;
1648 }
1649
1650
1651 void LyXTabular::SetRotateTabular(bool flag)
1652 {
1653         rotate = flag;
1654 }
1655
1656
1657 bool LyXTabular::GetRotateTabular() const
1658 {
1659         return rotate;
1660 }
1661
1662
1663 void LyXTabular::SetRotateCell(int cell, bool flag)
1664 {
1665         cellinfo_of_cell(cell)->rotate = flag;
1666 }
1667
1668
1669 bool LyXTabular::GetRotateCell(int cell) const
1670 {
1671         return cellinfo_of_cell(cell)->rotate;
1672 }
1673
1674
1675 bool LyXTabular::NeedRotating() const
1676 {
1677         if (rotate)
1678                 return true;
1679         for (int i = 0; i < rows_; ++i) {
1680                 for (int j = 0; j < columns_; ++j) {
1681                         if (cell_info[i][j].rotate)
1682                                 return true;
1683                 }
1684         }
1685         return false;
1686 }
1687
1688
1689 bool LyXTabular::IsLastCell(int cell) const
1690 {
1691         if ((cell + 1) < numberofcells)
1692                 return false;
1693         return true;
1694 }
1695
1696
1697 int LyXTabular::GetCellAbove(int cell) const
1698 {
1699         if (row_of_cell(cell) > 0)
1700                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1701         return cell;
1702 }
1703
1704
1705 int LyXTabular::GetCellBelow(int cell) const
1706 {
1707         if (row_of_cell(cell) + 1 < rows_)
1708                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1709         return cell;
1710 }
1711
1712
1713 int LyXTabular::GetLastCellAbove(int cell) const
1714 {
1715         if (row_of_cell(cell) <= 0)
1716                 return cell;
1717         if (!IsMultiColumn(cell))
1718                 return GetCellAbove(cell);
1719         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1720 }
1721
1722
1723 int LyXTabular::GetLastCellBelow(int cell) const
1724 {
1725         if (row_of_cell(cell) + 1 >= rows_)
1726                 return cell;
1727         if (!IsMultiColumn(cell))
1728                 return GetCellBelow(cell);
1729         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1730 }
1731
1732
1733 int LyXTabular::GetCellNumber(int row, int column) const
1734 {
1735 #if 0
1736         if (column >= columns_)
1737                 column = columns_ - 1;
1738         else if (column < 0)
1739                 column = 0;
1740         if (row >= rows_)
1741                 row = rows_ - 1;
1742         else if (row < 0)
1743                 row = 0;
1744 #else
1745         lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1746 #endif
1747         return cell_info[row][column].cellno;
1748 }
1749
1750
1751 void LyXTabular::SetUsebox(int cell, BoxType type)
1752 {
1753         cellinfo_of_cell(cell)->usebox = type;
1754 }
1755
1756
1757 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1758 {
1759         if (column_info[column_of_cell(cell)].p_width.empty() &&
1760                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1761                 return BOX_NONE;
1762         if (cellinfo_of_cell(cell)->usebox > 1)
1763                 return cellinfo_of_cell(cell)->usebox;
1764         return UseParbox(cell);
1765 }
1766
1767
1768 void LyXTabular::SetLTHead(int cell, bool first)
1769 {
1770         int const row = row_of_cell(cell);
1771         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1772
1773         if (first) {
1774                 if (endfirsthead == val)
1775                         endfirsthead = 0;
1776                 else
1777                         endfirsthead = val;
1778         } else {
1779                 if (endhead == val)
1780                         endhead = 0;
1781                 else
1782                         endhead = val;
1783         }
1784 }
1785
1786
1787 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1788 {
1789         row = endhead;
1790         if (abs(endhead) > rows_)
1791                 return false;
1792         return (row_of_cell(cell) == abs(endhead) - 1);
1793 }
1794
1795
1796 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1797 {
1798         row = endfirsthead;
1799         if (abs(endfirsthead) > rows_)
1800                 return false;
1801         return (row_of_cell(cell) == abs(endfirsthead) - 1);
1802 }
1803
1804
1805 void LyXTabular::SetLTFoot(int cell, bool last)
1806 {
1807         int const row = row_of_cell(cell);
1808         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1809
1810         if (last) {
1811                 if (endlastfoot == val)
1812                         endlastfoot = 0;
1813                 else
1814                         endlastfoot = val;
1815         } else {
1816                 if (endfoot == val)
1817                         endfoot = 0;
1818                 else
1819                         endfoot = val;
1820         }
1821 }
1822
1823
1824 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1825 {
1826         row = endfoot;
1827         if ((endfoot + 1) > rows_)
1828                 return false;
1829         return (row_of_cell(cell) == abs(endfoot) - 1);
1830 }
1831
1832
1833 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1834 {
1835         row = endlastfoot;
1836         if (abs(endlastfoot) > rows_)
1837                 return false;
1838         return (row_of_cell(cell) == (abs(endlastfoot)-1));
1839 }
1840
1841
1842 void LyXTabular::SetLTNewPage(int cell, bool what)
1843 {
1844         row_info[row_of_cell(cell)].newpage = what;
1845 }
1846
1847
1848 bool LyXTabular::GetLTNewPage(int cell) const
1849 {
1850         return row_info[row_of_cell(cell)].newpage;
1851 }
1852
1853
1854 bool LyXTabular::SetAscentOfRow(int row, int height)
1855 {
1856         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1857                 return false;
1858         row_info[row].ascent_of_row = height;
1859         return true;
1860 }
1861
1862
1863 bool LyXTabular::SetDescentOfRow(int row, int height)
1864 {
1865         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1866                 return false;
1867         row_info[row].descent_of_row = height;
1868         return true;
1869 }
1870
1871
1872 int LyXTabular::GetAscentOfRow(int row) const
1873 {
1874         if (row >= rows_)
1875                 return 0;
1876         return row_info[row].ascent_of_row;
1877 }
1878
1879
1880 int LyXTabular::GetDescentOfRow(int row) const
1881 {
1882         if (row >= rows_)
1883                 return 0;
1884         return row_info[row].descent_of_row;
1885 }
1886
1887
1888 int LyXTabular::GetHeightOfTabular() const
1889 {
1890         int height = 0;
1891
1892         for (int row = 0; row < rows_; ++row)
1893                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1894                         GetAdditionalHeight(row);
1895         return height;
1896 }
1897
1898
1899 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1900 {
1901         if ((row >= rows_) || (column >= columns_))
1902                 return false;
1903         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1904 }
1905
1906
1907 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1908 {
1909         if ((row < 0) || (row >= rows_))
1910                 return 0;
1911
1912         int const fcell = GetFirstCellInRow(row);
1913         int const n = NumberOfCellsInRow(fcell) + fcell;
1914         int tmp = 0;
1915
1916         for (int i = fcell; i < n; ++i) {
1917                 if (TopLine(i))
1918                         ++tmp;
1919         }
1920         if (tmp == (n - fcell)){
1921                 os << "\\hline ";
1922         } else if (tmp) {
1923                 for (int i = fcell; i < n; ++i) {
1924                         if (TopLine(i)) {
1925                                 os << "\\cline{"
1926                                    << column_of_cell(i) + 1
1927                                    << '-'
1928                                    << right_column_of_cell(i) + 1
1929                                    << "} ";
1930                         }
1931                 }
1932         } else {
1933                 return 0;
1934         }
1935         os << "\n";
1936         return 1;
1937 }
1938
1939
1940 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1941 {
1942         if ((row < 0) || (row >= rows_))
1943                 return 0;
1944
1945         int const fcell = GetFirstCellInRow(row);
1946         int const n = NumberOfCellsInRow(fcell) + fcell;
1947         int tmp = 0;
1948
1949         for (int i = fcell; i < n; ++i) {
1950                 if (BottomLine(i))
1951                         ++tmp;
1952         }
1953         if (tmp == (n-fcell)){
1954                 os << "\\hline";
1955         } else if (tmp) {
1956                 for (int i = fcell; i < n; ++i) {
1957                         if (BottomLine(i)) {
1958                                 os << "\\cline{"
1959                                    << column_of_cell(i) + 1
1960                                    << '-'
1961                                    << right_column_of_cell(i) + 1
1962                                    << "} ";
1963                         }
1964                 }
1965         } else {
1966                 return 0;
1967         }
1968         os << "\n";
1969         return 1;
1970 }
1971
1972
1973 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1974 {
1975         int ret = 0;
1976
1977         if (GetRotateCell(cell)) {
1978                 os << "\\begin{sideways}\n";
1979                 ++ret;
1980         }
1981         if (IsMultiColumn(cell)) {
1982                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1983                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1984                         os << cellinfo_of_cell(cell)->align_special << "}{";
1985                 } else {
1986                         if (LeftLine(cell) &&
1987                                 (IsFirstCellInRow(cell) || 
1988                                  (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1989                                   !RightLine(cell-1, true))))
1990                         {
1991                                 os << '|';
1992                         }
1993                         if (!GetPWidth(cell).empty()) {
1994                                 switch (GetVAlignment(cell)) {
1995                                 case LYX_VALIGN_TOP:
1996                                         os << "p";
1997                                         break;
1998                                 case LYX_VALIGN_CENTER:
1999                                         os << "m";
2000                                         break;
2001                                 case LYX_VALIGN_BOTTOM:
2002                                         os << "b";
2003                                         break;
2004                                 }
2005                                 os << "{" << GetPWidth(cell) << '}';
2006                         } else {
2007                                 switch (GetAlignment(cell)) {
2008                                 case LYX_ALIGN_LEFT:
2009                                         os << 'l';
2010                                         break;
2011                                 case LYX_ALIGN_RIGHT:
2012                                         os << 'r';
2013                                         break;
2014                                 default:
2015                                         os << 'c';
2016                                         break;
2017                                 }
2018                         }
2019                         if (RightLine(cell))
2020                                 os << '|';
2021                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2022                                 LeftLine(cell+1))
2023                                 os << '|';
2024                         os << "}{";
2025                 }
2026         }
2027         if (GetUsebox(cell) == BOX_PARBOX) {
2028                 os << "\\parbox[";
2029                 switch (GetVAlignment(cell)) {
2030                 case LYX_VALIGN_TOP:
2031                         os << "t";
2032                         break;
2033                 case LYX_VALIGN_CENTER:
2034                         os << "c";
2035                         break;
2036                 case LYX_VALIGN_BOTTOM:
2037                         os << "b";
2038                         break;
2039                 }
2040                 os << "]{" << GetPWidth(cell) << "}{";
2041         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2042                 os << "\\begin{minipage}[";
2043                 switch (GetVAlignment(cell)) {
2044                 case LYX_VALIGN_TOP:
2045                         os << "t";
2046                         break;
2047                 case LYX_VALIGN_CENTER:
2048                         os << "m";
2049                         break;
2050                 case LYX_VALIGN_BOTTOM:
2051                         os << "b";
2052                         break;
2053                 }
2054                 os << "]{" << GetPWidth(cell) << "}\n";
2055                 ++ret;
2056         }
2057         return ret;
2058 }
2059
2060
2061 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2062 {
2063         int ret = 0;
2064
2065         // usual cells
2066         if (GetUsebox(cell) == BOX_PARBOX)
2067                 os << "}";
2068         else if (GetUsebox(cell) == BOX_MINIPAGE) {
2069                 os << "%\n\\end{minipage}";
2070                 ret += 2;
2071         }
2072         if (IsMultiColumn(cell)){
2073                 os << '}';
2074         }
2075         if (GetRotateCell(cell)) {
2076                 os << "%\n\\end{sideways}";
2077                 ++ret;
2078         }
2079         return ret;
2080 }
2081
2082
2083 int LyXTabular::Latex(Buffer const * buf,
2084                                           ostream & os, bool fragile, bool fp) const
2085 {
2086         int ret = 0;
2087         int cell = 0;
2088
2089         //+---------------------------------------------------------------------
2090         //+                      first the opening preamble                    +
2091         //+---------------------------------------------------------------------
2092
2093         if (rotate) {
2094                 os << "\\begin{sideways}\n";
2095                 ++ret;
2096         }
2097         if (is_long_tabular)
2098                 os << "\\begin{longtable}{";
2099         else
2100                 os << "\\begin{tabular}{";
2101         for (int i = 0; i < columns_; ++i) {
2102                 if (column_info[i].left_line)
2103                         os << '|';
2104                 if (!column_info[i].align_special.empty()) {
2105                         os << column_info[i].align_special;
2106                 } else if (!column_info[i].p_width.empty()) {
2107                         switch (column_info[i].valignment) {
2108                         case LYX_VALIGN_TOP:
2109                                 os << "p";
2110                                 break;
2111                         case LYX_VALIGN_CENTER:
2112                                 os << "m";
2113                                 break;
2114                         case LYX_VALIGN_BOTTOM:
2115                                 os << "b";
2116                                 break;
2117                         }
2118                         os << "{"
2119                            << column_info[i].p_width
2120                            << '}';
2121                 } else {
2122                         switch (column_info[i].alignment) {
2123                         case LYX_ALIGN_LEFT:
2124                                 os << 'l';
2125                                 break;
2126                         case LYX_ALIGN_RIGHT:
2127                                 os << 'r';
2128                                 break;
2129                         default:
2130                                 os << 'c';
2131                                 break;
2132                         }
2133                 }
2134                 if (column_info[i].right_line)
2135                         os << '|';
2136         }
2137         os << "}\n";
2138         ++ret;
2139
2140         //+---------------------------------------------------------------------
2141         //+                      the single row and columns (cells)            +
2142         //+---------------------------------------------------------------------
2143
2144         for (int i = 0; i < rows_; ++i) {
2145                 ret += TeXTopHLine(os, i);
2146                 int bret = ret;
2147                 if (IsLongTabular()) {
2148                         if ((endhead < 0) && (i == (abs(endhead)-1))) {
2149                                 os << "\\endhead\n";
2150                                 ++ret;
2151                         }
2152                         if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2153                                 os << "\\endfirsthead\n";
2154                                 ++ret;
2155                         }
2156                         if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2157                                 os << "\\endfoot\n";
2158                                 ++ret;
2159                         }
2160                         if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2161                                 os << "\\endlastfoot\n";
2162                                 ++ret;
2163                         }
2164                 }
2165                 if (ret > bret) {
2166                         ret += TeXBottomHLine(os, i-1);
2167                         ret += TeXTopHLine(os, i);
2168                 }
2169                 for (int j = 0; j < columns_; ++j) {
2170                         if (IsPartOfMultiColumn(i,j))
2171                                 continue;
2172                         ret += TeXCellPreamble(os, cell);
2173                         InsetText * inset = GetCellInset(cell);
2174
2175                         bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2176                                         inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2177
2178                         if (rtl)
2179                                 os << "\\R{";
2180                         ret += inset->latex(buf, os, fragile, fp);
2181                         if (rtl)
2182                                 os << "}";
2183
2184                         ret += TeXCellPostamble(os, cell);
2185                         if (!IsLastCellInRow(cell)) { // not last cell in row
2186                                 os << "&\n";
2187                                 ++ret;
2188                         }
2189                         ++cell;
2190                 }
2191                 os << "\\\\\n";
2192                 ret += TeXBottomHLine(os, i);
2193                 bret = ret;
2194                 if (IsLongTabular()) {
2195                         if ((endhead > 0) && (i == (endhead - 1))) {
2196                                 os << "\\endhead\n";
2197                                 ++ret;
2198                         }
2199                         if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2200                                 os << "\\endfirsthead\n";
2201                                 ++ret;
2202                         }
2203                         if ((endfoot > 0) && (i == (endfoot - 1))) {
2204                                 os << "\\endfoot\n";
2205                                 ++ret;
2206                         }
2207                         if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2208                                 os << "\\endlastfoot\n";
2209                                 ++ret;
2210                         }
2211 //          if (ret > bret)
2212 //              ret += TeXBottomHLine(os, i);
2213                         if (row_info[i].newpage) {
2214                                 os << "\\newpage\n";
2215                                 ++ret;
2216                         }
2217                 }
2218         }
2219
2220         //+---------------------------------------------------------------------
2221         //+                      the closing of the tabular                    +
2222         //+---------------------------------------------------------------------
2223
2224         if (is_long_tabular)
2225                 os << "\\end{longtable}";
2226         else
2227                 os << "\\end{tabular}";
2228         if (rotate) {
2229                 os << "\n\\end{sideways}";
2230                 ++ret;
2231         }
2232
2233         return ret;
2234 }
2235
2236
2237 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2238 {
2239         int ret = 0;
2240
2241         //+---------------------------------------------------------------------
2242         //+                      first the opening preamble                    +
2243         //+---------------------------------------------------------------------
2244
2245         os << "<tgroup cols=\"" << columns_
2246            << "\" colsep=\"1\" rowsep=\"1\">\n";
2247         
2248         for (int i = 0; i < columns_; ++i) {
2249                 os << "<colspec colname=\"col" << i << "\" align=\"";
2250                 switch (column_info[i].alignment) {
2251                 case LYX_ALIGN_LEFT:
2252                         os << "left";
2253                         break;
2254                 case LYX_ALIGN_RIGHT:
2255                         os << "right";
2256                         break;
2257                 default:
2258                         os << "center";
2259                         break;
2260                 }
2261                 os << "\"/>\n";
2262                 ++ret;
2263         }
2264
2265         //+---------------------------------------------------------------------
2266         //+                      the single row and columns (cells)            +
2267         //+---------------------------------------------------------------------
2268
2269         int cell = 0;
2270         os << "<tbody>\n";
2271         for (int i = 0; i < rows_; ++i) {
2272                 os << "<row>\n";
2273                 for (int j = 0; j < columns_; ++j) {
2274                         if (IsPartOfMultiColumn(i, j))
2275                                 continue;
2276                 
2277                         os << "<entry align=\"";
2278                         switch (GetAlignment(cell)) {
2279                         case LYX_ALIGN_LEFT:
2280                                 os << "left";
2281                                 break;
2282                         case LYX_ALIGN_RIGHT:
2283                                 os << "right";
2284                                 break;
2285                         default:
2286                                 os << "center";
2287                                 break;
2288                         }
2289                 
2290                         os << "\" valign=\"";
2291                         switch (GetVAlignment(cell)) {
2292                         case LYX_VALIGN_TOP:
2293                                 os << "top";
2294                                 break;
2295                         case LYX_VALIGN_BOTTOM:
2296                                 os << "bottom";
2297                                 break;
2298                         case LYX_VALIGN_CENTER:
2299                                 os << "middle";
2300                         }
2301                         os << "\"";
2302                 
2303                         if (IsMultiColumn(cell)) {
2304                                 os << " namest=\"col" << j << "\" ";
2305                                 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2306                         }
2307                 
2308                         os << ">";
2309                         ret += GetCellInset(cell)->docBook(buf, os);
2310                         os << "</entry>";
2311                         ++cell;
2312                 }
2313                 os << "</row>\n";
2314         }
2315         os << "</tbody>\n";
2316         //+---------------------------------------------------------------------
2317         //+                      the closing of the tabular                    +
2318         //+---------------------------------------------------------------------
2319
2320         os << "</tgroup>";
2321         ++ret;
2322
2323         return ret;
2324 }
2325
2326
2327 namespace {
2328
2329         inline
2330         void print_n_chars(ostream & os, unsigned char ch, int n)
2331         {
2332                 os << string(n, ch);
2333         }
2334
2335 } // namespace anon
2336
2337
2338 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2339                                                           vector<unsigned int> const & clen) const
2340 {
2341         int const fcell = GetFirstCellInRow(row);
2342         int const n = NumberOfCellsInRow(fcell) + fcell;
2343         int tmp = 0;
2344
2345         for (int i = fcell; i < n; ++i) {
2346                 if (TopLine(i)) {
2347                         ++tmp;
2348                         break;
2349                 }
2350         }
2351         if (!tmp)
2352                 return 0;
2353
2354         unsigned char ch;
2355         for (int i = fcell; i < n; ++i) {
2356                 if (TopLine(i)) {
2357                         if (LeftLine(i))
2358                                 os << "+-";
2359                         else
2360                                 os << "--";
2361                         ch = '-';
2362                 } else {
2363                         os << "  ";
2364                         ch = ' ';
2365                 }
2366                 int column = column_of_cell(i);
2367                 int len = clen[column];
2368                 while(IsPartOfMultiColumn(row, ++column))
2369                         len += clen[column] + 4;
2370                 print_n_chars(os, ch, len);
2371                 if (TopLine(i)) {
2372                         if (RightLine(i))
2373                                 os << "-+";
2374                         else
2375                                 os << "--";
2376                 } else {
2377                         os << "  ";
2378                 }
2379         }
2380         os << endl;
2381         return 1;
2382 }
2383
2384
2385 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2386                                                                  vector<unsigned int> const & clen) const
2387 {
2388         int const fcell = GetFirstCellInRow(row);
2389         int const n = NumberOfCellsInRow(fcell) + fcell;
2390         int tmp = 0;
2391
2392         for (int i = fcell; i < n; ++i) {
2393                 if (BottomLine(i)) {
2394                         ++tmp;
2395                         break;
2396                 }
2397         }
2398         if (!tmp)
2399                 return 0;
2400
2401         unsigned char ch;
2402         for (int i = fcell; i < n; ++i) {
2403                 if (BottomLine(i)) {
2404                         if (LeftLine(i))
2405                                 os << "+-";
2406                         else
2407                                 os << "--";
2408                         ch = '-';
2409                 } else {
2410                         os << "  ";
2411                         ch = ' ';
2412                 }
2413                 int column = column_of_cell(i);
2414                 int len = clen[column];
2415                 while(IsPartOfMultiColumn(row, ++column))
2416                         len += clen[column] + 4;
2417                 print_n_chars(os, ch, len);
2418                 if (BottomLine(i)) {
2419                         if (RightLine(i))
2420                                 os << "-+";
2421                         else
2422                                 os << "--";
2423                 } else {
2424                         os << "  ";
2425                 }
2426         }
2427         os << endl;
2428         return 1;
2429 }
2430
2431
2432 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2433                                                            int cell, int row, int column,
2434                                                            vector<unsigned int> const & clen) const
2435 {
2436         ostringstream sstr;
2437         int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2438
2439         if (LeftLine(cell))
2440                 os << "| ";
2441         else
2442                 os << "  ";
2443
2444         unsigned int len1 = sstr.str().length();
2445         unsigned int len2 = clen[column];
2446         while(IsPartOfMultiColumn(row, ++column))
2447                 len2 += clen[column] + 4;
2448         len2 -= len1;
2449
2450         switch (GetAlignment(cell)) {
2451         default:
2452         case LYX_ALIGN_LEFT:
2453                 len1 = 0;
2454                 break;
2455         case LYX_ALIGN_RIGHT:
2456                 len1 = len2;
2457                 len2 = 0;
2458                 break;
2459         case LYX_ALIGN_CENTER:
2460                 len1 = len2 / 2;
2461                 len2 -= len1;
2462                 break;
2463         }
2464
2465         for (unsigned int i = 0; i < len1; ++i)
2466                 os << " ";
2467         os << sstr.str();
2468         for (unsigned int i = 0; i < len2; ++i)
2469                 os << " ";
2470         if (RightLine(cell))
2471                 os << " |";
2472         else
2473                 os << "  ";
2474
2475         return ret;
2476 }
2477
2478
2479 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2480 {
2481         int ret = 0;
2482
2483         //+---------------------------------------------------------------------
2484         //+           first calculate the width of the single columns          +
2485         //+---------------------------------------------------------------------
2486         vector<unsigned int> clen(columns_);
2487
2488         // first all non (real) multicolumn cells!
2489         for (int j = 0; j < columns_; ++j) {
2490                 clen[j] = 0;
2491                 for (int i = 0; i < rows_; ++i) {
2492                         int cell = GetCellNumber(i, j);
2493                         if (IsMultiColumn(cell, true))
2494                                 continue;
2495                         ostringstream sstr;
2496                         GetCellInset(cell)->ascii(buf, sstr, 0);
2497                         if (clen[j] < sstr.str().length())
2498                                 clen[j] = sstr.str().length();
2499                 }
2500         }
2501         // then all (real) multicolumn cells!
2502         for (int j = 0; j < columns_; ++j) {
2503                 for (int i = 0; i < rows_; ++i) {
2504                         int cell = GetCellNumber(i, j);
2505                         if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2506                                 continue;
2507                         ostringstream sstr;
2508                         GetCellInset(cell)->ascii(buf, sstr, 0);
2509                         int len = int(sstr.str().length());
2510                         int const n = cells_in_multicolumn(cell);
2511                         for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2512                                 len -= clen[k];
2513                         if (len > int(clen[j + n - 1]))
2514                                 clen[j + n - 1] = len;
2515                 }
2516         }
2517         int cell = 0;
2518         for (int i = 0; i < rows_; ++i) {
2519                 AsciiTopHLine(os, i, clen);
2520                 for (int j = 0; j < columns_; ++j) {
2521                         if (IsPartOfMultiColumn(i,j))
2522                                 continue;
2523                         ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2524                         ++cell;
2525                 }
2526                 os << endl;
2527                 AsciiBottomHLine(os, i, clen);
2528         }
2529         return ret;
2530 }
2531
2532
2533 InsetText * LyXTabular::GetCellInset(int cell) const
2534 {
2535         cur_cell = cell;
2536         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2537 }
2538
2539
2540 InsetText * LyXTabular::GetCellInset(int row, int column) const
2541 {
2542         cur_cell = GetCellNumber(row, column);
2543         return & cell_info[row][column].inset;
2544 }
2545
2546
2547 void LyXTabular::Validate(LaTeXFeatures & features) const
2548 {
2549         if (IsLongTabular())
2550                 features.longtable = true;
2551         if (NeedRotating())
2552                 features.rotating = true;
2553         for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2554                 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2555                         features.array = true;
2556                 GetCellInset(cell)->validate(features);
2557         }
2558 }
2559
2560
2561 std::vector<string> const LyXTabular::getLabelList() const
2562 {
2563         std::vector<string> label_list;
2564         for (int i = 0; i < rows_; ++i)
2565                 for (int j = 0; j < columns_; ++j) {
2566                         std::vector<string> const l =
2567                                 GetCellInset(i, j)->getLabelList();
2568                         label_list.insert(label_list.end(),
2569                                           l.begin(), l.end());
2570                 }
2571         return label_list;
2572 }
2573
2574                         
2575 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2576 {
2577         Paragraph * par = GetCellInset(cell)->paragraph();
2578
2579         for (; par; par = par->next()) {
2580                 for (int i = 0; i < par->size(); ++i) {
2581                         if (par->getChar(i)     == Paragraph::META_NEWLINE)
2582                                 return BOX_PARBOX;
2583                 }
2584         }
2585         return BOX_NONE;
2586 }
2587
2588 /* Emacs:
2589  * Local variables:
2590  * tab-width: 4
2591  * End:
2592  * vi:set tabstop=4:
2593  */