]> git.lyx.org Git - features.git/blob - src/tabular.C
more funcs to lowerchar, adjust
[features.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "lyx_gui_misc.h"
27 #include "buffer.h"
28 #include "BufferView.h"
29 #include "Painter.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "support/LAssert.h"
34 #include "insets/insettabular.h"
35 #include "insets/insettext.h"
36 #include "gettext.h"
37
38 using std::ostream;
39 using std::istream;
40 using std::getline;
41 using std::max;
42 using std::endl;
43 using std::vector;
44
45 #ifndef CXX_GLOBAL_CSTD
46 using std::strlen;
47 #endif
48
49 namespace {
50
51         int const WIDTH_OF_LINE = 5;
52
53 } // namespace
54
55 /// Define a few methods for the inner structs
56
57 LyXTabular::cellstruct::cellstruct() 
58 {
59         cellno = 0;
60         width_of_cell = 0;
61         multicolumn = LyXTabular::CELL_NORMAL;
62         alignment = LYX_ALIGN_CENTER;
63         valignment = LYX_VALIGN_TOP;
64         top_line = true;
65         bottom_line = false;
66         left_line = true;
67         right_line = false;
68         usebox = BOX_NONE;
69         rotate = false;
70 }
71
72
73 LyXTabular::rowstruct::rowstruct() 
74 {
75         top_line = true;
76         bottom_line = false;
77         ascent_of_row = 0;
78         descent_of_row = 0;
79         newpage = false;
80 }
81
82
83 LyXTabular::columnstruct::columnstruct() 
84 {
85         left_line = true;
86         right_line = false;
87         alignment = LYX_ALIGN_CENTER;
88         valignment = LYX_VALIGN_TOP;
89         width_of_column = 0;
90 }
91
92
93 /* konstruktor */
94 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
95 {
96         owner_ = inset;
97         cur_cell = -1;
98         Init(rows_arg, columns_arg);
99 }
100
101
102 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
103 {
104         owner_ = inset;
105         cur_cell = -1;
106         Init(lt.rows_, lt.columns_, &lt);
107 #if 0
108 #ifdef WITH_WARNINGS
109 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
110 #endif
111         operator=(lt);
112 #endif
113 }
114
115
116 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
117 {
118         owner_ = inset;
119         cur_cell = -1;
120         Read(buf, lex);
121 }
122
123
124 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
125 {
126 #if 0
127 #warning This while method should look like this: (Lgb)
128
129                 LyXTabular tmp(lt);
130                 tmp.swap(*this);
131 #else
132         // If this and lt is not of the same size we have a serious bug
133         // So then it is ok to throw an exception, or for now
134         // call abort()
135         lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
136         cur_cell = -1;
137         cell_info = lt.cell_info;
138         row_info = lt.row_info;
139         column_info = lt.column_info;
140
141         // long tabular stuff
142         SetLongTabular(lt.is_long_tabular);
143         endhead = lt.endhead;
144         endfoot = lt.endfoot;
145         endfirsthead = lt.endfirsthead;
146         endlastfoot = lt.endlastfoot;
147
148         rotate = lt.rotate;
149
150         Reinit();
151 #endif
152         return *this;
153 }
154
155
156 LyXTabular * LyXTabular::clone(InsetTabular * inset)
157 {
158         LyXTabular * result = new LyXTabular(inset, *this);
159 #if 0
160         // don't know if this is good but I need to Clone also
161         // the text-insets here, this is for the Undo-facility!
162         for (int i = 0; i < rows_; ++i) {
163                 for (int j = 0; j < columns_; ++j) {
164                         result->cell_info[i][j].inset = cell_info[i][j].inset;
165                         result->cell_info[i][j].inset.setOwner(inset);
166                 }
167         }
168 #endif
169         return result;
170 }
171
172
173 /* activates all lines and sets all widths to 0 */ 
174 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
175 {
176         rows_ = rows_arg;
177         columns_ = columns_arg;
178         row_info = row_vector(rows_, rowstruct());
179         column_info = column_vector(columns_, columnstruct());
180         cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
181
182         if (lt) {
183                 operator=(*lt);
184                 return;
185         }
186
187         int cellno = 0;
188         for (int i = 0; i < rows_; ++i) {
189                 for (int j = 0; j < columns_; ++j) {
190                         cell_info[i][j].inset.setOwner(owner_);
191                         cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
192                         cell_info[i][j].cellno = cellno++;
193                 }
194                 cell_info[i].back().right_line = true;
195         }
196         row_info.back().bottom_line = true;
197         row_info.front().bottom_line = true;
198
199         for (int i = 0; i < columns_; ++i) {
200                 calculate_width_of_column(i);
201         }
202         column_info.back().right_line = true;
203    
204         calculate_width_of_tabular();
205
206         rowofcell = vector<int>();
207         columnofcell = vector<int>();
208         set_row_column_number_info();
209         is_long_tabular = false;
210         rotate = false;
211         endhead = 0;
212         endfirsthead = 0;
213         endfoot = 0;
214         endlastfoot = 0;
215 }
216
217
218 void LyXTabular::AppendRow(int cell)
219 {
220         ++rows_;
221    
222         int row = row_of_cell(cell);
223
224         row_vector::iterator rit = row_info.begin() + row;
225         row_info.insert(rit, rowstruct());
226         // now set the values of the row before
227         row_info[row] = row_info[row+1];
228
229 #if 0
230         cell_vvector::iterator cit = cell_info.begin() + row;
231         cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
232 #else
233         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
234                                                                                                                   cellstruct()));
235
236         for (int i = 0; i <= row; ++i) {
237                 for (int j = 0; j < columns_; ++j) {
238                         c_info[i][j] = cell_info[i][j];
239                 }
240         }
241         for (int i = row + 1; i < rows_; ++i) {
242                 for (int j = 0; j < columns_; ++j) {
243                         c_info[i][j] = cell_info[i-1][j];
244                 }
245         }
246         cell_info = c_info;
247         ++row;
248         for (int j = 0; j < columns_; ++j) {
249                 cell_info[row][j].inset.clear();
250         }
251 #endif
252         Reinit();
253 }
254
255
256 void LyXTabular::DeleteRow(int row)
257 {
258         // Why make it so hard? (Lgb)
259         //if (!(rows_ - 1))
260         //return;
261         if (rows_ == 1) return; // Not allowed to delete last row
262         
263         row_info.erase(row_info.begin() + row); //&row_info[row]);
264         cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
265         --rows_;
266         Reinit();
267 }
268
269
270 void LyXTabular::AppendColumn(int cell)
271 {
272         ++columns_;
273    
274         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
275                                                                                                                   cellstruct()));
276         int const column = column_of_cell(cell);
277         column_vector::iterator cit = column_info.begin() + column + 1;
278         column_info.insert(cit, columnstruct());
279         // set the column values of the column before
280         column_info[column+1] = column_info[column];
281
282         for (int i = 0; i < rows_; ++i) {
283                 for (int j = 0; j <= column; ++j) {
284                         c_info[i][j] = cell_info[i][j];
285                 }
286                 for (int j = column + 1; j < columns_; ++j) {
287                         c_info[i][j] = cell_info[i][j - 1];
288                 }
289                 // care about multicolumns
290                 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
291                         cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
292                 }
293                 if ((column + 1) == columns_ ||
294                         cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
295                         cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
296                 }
297         }
298         cell_info = c_info;
299         //++column;
300         for (int i = 0; i < rows_; ++i) {
301                 //cell_info[i][column].inset.clear();
302                 cell_info[i][column + 1].inset.clear();
303         }
304         Reinit();
305 }
306
307
308 void LyXTabular::DeleteColumn(int column)
309 {
310         // Similar to DeleteRow
311         //if (!(columns_ - 1))
312         //return;
313         if (columns_ == 1) return; // Not allowed to delete last column
314          
315         column_info.erase(column_info.begin() + column);
316         for (int i = 0; i < rows_; ++i) {
317                 cell_info[i].erase(cell_info[i].begin() + column);
318         }
319         --columns_;
320         Reinit();
321 }
322
323
324 void LyXTabular::Reinit()
325 {   
326         for (int i = 0; i < rows_; ++i) {
327                 for (int j = 0; j < columns_; ++j) {
328                         cell_info[i][j].width_of_cell = 0;
329                         cell_info[i][j].inset.setOwner(owner_);
330                 }
331         }
332   
333         for (int i = 0; i < columns_; ++i) {
334                 calculate_width_of_column(i);
335         }
336         calculate_width_of_tabular();
337
338         set_row_column_number_info();
339 }
340
341
342 void LyXTabular::set_row_column_number_info(bool oldformat)
343 {
344         numberofcells = -1;
345         for (int row = 0; row < rows_; ++row) {
346                 for (int column = 0; column<columns_; ++column) {
347                         if (cell_info[row][column].multicolumn
348                                 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
349                                 ++numberofcells;
350                         cell_info[row][column].cellno = numberofcells;
351                 }
352         }
353         ++numberofcells; // because this is one more than as we start from 0
354
355         rowofcell.resize(numberofcells);
356         columnofcell.resize(numberofcells);
357
358         for (int row = 0, column = 0, c = 0;
359                  c < numberofcells && row < rows_ && column < columns_;) {
360                 rowofcell[c] = row;
361                 columnofcell[c] = column;
362                 ++c;
363                 do {
364                         ++column;
365                 } while (column < columns_ &&
366                                  cell_info[row][column].multicolumn
367                                  == LyXTabular::CELL_PART_OF_MULTICOLUMN);
368                 if (column == columns_) {
369                         column = 0;
370                         ++row;
371                 }
372         }
373
374         for (int row = 0; row < rows_; ++row) {
375                 for (int column = 0; column < columns_; ++column) {
376                         if (IsPartOfMultiColumn(row,column))
377                                 continue;
378                         // now set the right line of multicolumns right for oldformat read
379                         if (oldformat &&
380                                 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
381                         {
382                                 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
383                                 cell_info[row][column].right_line =
384                                         cell_info[row][column+cn-1].right_line;
385                         }
386                         cell_info[row][column].inset.setAutoBreakRows(
387                                 !GetPWidth(GetCellNumber(row, column)).empty());
388                 }
389         }
390 }
391
392
393 int LyXTabular::GetNumberOfCells() const
394 {
395         return numberofcells;
396 }
397
398
399 int LyXTabular::NumberOfCellsInRow(int cell) const
400 {
401         int const row = row_of_cell(cell);
402         int result = 0;
403         for (int i = 0; i < columns_; ++i) {
404                 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
405                         ++result;
406         }
407         return result;
408 }
409
410
411 /* returns 1 if there is a topline, returns 0 if not */ 
412 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
413 {
414         int const row = row_of_cell(cell);
415         
416         if (!onlycolumn && IsMultiColumn(cell))
417                 return cellinfo_of_cell(cell)->top_line;
418         return row_info[row].top_line;
419 }
420
421
422 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
423 {
424         // no bottom line underneath non-existent cells if you please
425         // Isn't that a programming error? Is so this should
426         // be an Assert instead. (Lgb)
427         if (cell >= numberofcells)
428                 return false;
429
430         if (!onlycolumn && IsMultiColumn(cell))
431                 return cellinfo_of_cell(cell)->bottom_line;
432         return row_info[row_of_cell(cell)].bottom_line;
433 }
434
435
436 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
437 {
438         if (!onlycolumn && IsMultiColumn(cell))
439                 return cellinfo_of_cell(cell)->left_line;
440         return column_info[column_of_cell(cell)].left_line;
441 }
442
443
444 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
445 {
446         if (!onlycolumn && IsMultiColumn(cell))
447                 return cellinfo_of_cell(cell)->right_line;
448         return column_info[right_column_of_cell(cell)].right_line;
449 }
450
451
452 bool LyXTabular::TopAlreadyDrawed(int cell) const
453 {
454         int row = row_of_cell(cell);
455         if ((row > 0) && !GetAdditionalHeight(row)) {
456                 int column = column_of_cell(cell);
457                 --row;
458                 while (column
459                            && cell_info[row][column].multicolumn
460                            == LyXTabular::CELL_PART_OF_MULTICOLUMN)
461                         --column;
462                 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
463                         return row_info[row].bottom_line;
464                 else
465                         return cell_info[row][column].bottom_line;
466         }
467         return false;
468 }
469
470
471 bool LyXTabular::LeftAlreadyDrawed(int cell) const
472 {
473         int column = column_of_cell(cell);
474         if (column > 0) {
475                 int row = row_of_cell(cell);
476                 while (--column &&
477                            (cell_info[row][column].multicolumn ==
478                                 LyXTabular::CELL_PART_OF_MULTICOLUMN));
479                 if (GetAdditionalWidth(cell_info[row][column].cellno))
480                         return false;
481                 return column_info[column].right_line;
482         }
483         return false;
484 }
485
486
487 bool LyXTabular::IsLastRow(int cell) const
488 {
489         return (row_of_cell(cell) == rows_ - 1);
490 }
491
492
493 int LyXTabular::GetAdditionalHeight(int row) const
494 {
495         if (!row || row >= rows_)
496                 return 0;
497
498         bool top = true;
499         bool bottom = true;
500
501         for (int column = 0; column < columns_ - 1 && bottom; ++column) {
502                 switch (cell_info[row - 1][column].multicolumn) {
503                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
504                         bottom = cell_info[row - 1][column].bottom_line;
505                         break;
506                 case LyXTabular::CELL_NORMAL:
507                         bottom = row_info[row - 1].bottom_line;
508                 }
509         }
510         for (int column = 0; column < columns_ - 1 && top; ++column) {
511                 switch (cell_info[row][column].multicolumn){
512                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
513                         top = cell_info[row][column].top_line;
514                         break;
515                 case LyXTabular::CELL_NORMAL:
516                         top = row_info[row].top_line;
517                 }
518         }
519         if (top && bottom)
520                 return WIDTH_OF_LINE;
521         return 0;
522 }
523
524
525 int LyXTabular::GetAdditionalWidth(int cell) const
526 {
527         // internally already set in SetWidthOfCell
528         // used to get it back in text.C
529         int const col = right_column_of_cell(cell);
530         if (col < columns_ - 1 && column_info[col].right_line &&
531                 column_info[col+1].left_line)
532                 return WIDTH_OF_LINE;
533         else
534                 return 0;
535 }
536
537
538 // returns the maximum over all rows 
539 int LyXTabular::GetWidthOfColumn(int cell) const
540 {
541         int const column1 = column_of_cell(cell);
542         int const column2 = right_column_of_cell(cell);
543         int result = 0;
544         for (int i = column1; i <= column2; ++i) {
545                 result += column_info[i].width_of_column;
546         }
547         return result;
548 }
549
550
551 int LyXTabular::GetWidthOfTabular() const
552 {
553         return width_of_tabular;
554 }
555
556
557 /* returns 1 if a complete update is necessary, otherwise 0 */ 
558 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
559 {
560         if (!IsMultiColumn(cell))
561                 return false;
562         
563         int const row = row_of_cell(cell);
564         int const column1 = column_of_cell(cell);
565         int const column2 = right_column_of_cell(cell);
566
567         // first set columns to 0 so we can calculate the right width
568         for (int i = column1; i <= column2; ++i) {
569                 cell_info[row][i].width_of_cell = 0;
570         }
571         // set the width to MAX_WIDTH until width > 0
572         int width = (new_width + 2 * WIDTH_OF_LINE);
573
574         int i = column1;
575         for (; i < column2 && width > column_info[i].width_of_column; ++i) {
576                 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
577                 width -= column_info[i].width_of_column;
578         }
579         if (width > 0) {
580                 cell_info[row][i].width_of_cell = width;
581         }
582         return true;
583 }
584
585
586 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
587 {
588         int const row = row_of_cell(cell);
589         int const column1 = column_of_cell(cell);
590         int const column2 = right_column_of_cell(cell);
591
592         // first set columns to 0 so we can calculate the right width
593         int i = column1;
594         for (; i <= column2; ++i)
595                 cell_info[row][i].width_of_cell = 0;
596         for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
597                 ;
598         if (i < numberofcells)
599                 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
600         SetWidthOfMulticolCell(cell, new_width);
601 }
602
603
604 /* returns 1 if a complete update is necessary, otherwise 0 */ 
605 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
606 {
607         int const row = row_of_cell(cell);
608         int const column1 = column_of_cell(cell);
609         bool tmp = false;
610         int width = 0;
611
612         if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
613                 return false;
614         if (IsMultiColumn(cell, true)) {
615                 tmp = SetWidthOfMulticolCell(cell, new_width);
616         } else {
617                 width = (new_width + 2*WIDTH_OF_LINE);
618                 cell_info[row][column1].width_of_cell = width;
619                 if (column_info[column1].right_line && (column1 < columns_-1) &&
620                         column_info[column1+1].left_line) // additional width
621                         cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
622                 tmp = calculate_width_of_column_NMC(column1);
623         }
624         if (tmp) {
625                 int i = 0;
626                 for (; i<columns_; ++i)
627                         calculate_width_of_column_NMC(i);
628                 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
629                         ;
630                 if (i < numberofcells)
631                         recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
632                 for (i = 0; i < columns_; ++i)
633                         calculate_width_of_column(i);
634                 calculate_width_of_tabular();
635                 return true;
636         }
637         return false;
638 }
639
640
641 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
642 {
643         if (!IsMultiColumn(cell) || onlycolumn)
644                 column_info[column_of_cell(cell)].alignment = align;
645         if (!onlycolumn)
646                 cellinfo_of_cell(cell)->alignment = align;
647         return true;
648 }
649
650
651 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
652 {
653         if (!IsMultiColumn(cell) || onlycolumn)
654                 column_info[column_of_cell(cell)].valignment = align;
655         if (!onlycolumn)
656                 cellinfo_of_cell(cell)->valignment = align;
657         return true;
658 }
659
660
661 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
662 {
663         bool flag = !width.empty();
664         int const j = column_of_cell(cell);
665
666         column_info[j].p_width = width;
667         if (flag) // do this only if there is a width
668                 SetAlignment(cell, LYX_ALIGN_LEFT);
669         for (int i = 0; i < rows_; ++i) {
670                 int c = GetCellNumber(i, j);
671                 flag = !GetPWidth(c).empty(); // because of multicolumns!
672                 GetCellInset(c)->setAutoBreakRows(flag);
673         }
674         return true;
675 }
676
677
678 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
679 {
680         bool const flag = !width.empty();
681
682         cellinfo_of_cell(cell)->p_width = width;
683         if (IsMultiColumn(cell)) {
684                 GetCellInset(cell)->setAutoBreakRows(flag);
685                 return true;
686         }
687         return false;
688 }
689
690
691 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
692                                  LyXTabular::Feature what)
693 {
694         if (what == SET_SPECIAL_MULTI)
695                 cellinfo_of_cell(cell)->align_special = special;
696         else
697                 column_info[column_of_cell(cell)].align_special = special;
698         return true;
699 }
700
701
702 bool LyXTabular::SetAllLines(int cell, bool line)
703 {
704         SetTopLine(cell, line);
705         SetBottomLine(cell, line);
706         SetRightLine(cell, line);
707         SetLeftLine(cell, line);
708         return true;
709 }
710
711
712 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
713 {
714         int const row = row_of_cell(cell);
715
716         if (onlycolumn || !IsMultiColumn(cell))
717                 row_info[row].top_line = line;
718         else
719                 cellinfo_of_cell(cell)->top_line = line;
720         return true;
721 }
722
723
724 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
725 {
726         if (onlycolumn || !IsMultiColumn(cell))
727                 row_info[row_of_cell(cell)].bottom_line = line;
728         else
729                 cellinfo_of_cell(cell)->bottom_line = line;
730         return true;
731 }
732
733
734 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
735 {
736         if (onlycolumn || !IsMultiColumn(cell))
737                 column_info[column_of_cell(cell)].left_line = line;
738         else
739                 cellinfo_of_cell(cell)->left_line = line;
740         return true;
741 }
742
743
744 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
745 {
746         if (onlycolumn || !IsMultiColumn(cell))
747                 column_info[right_column_of_cell(cell)].right_line = line;
748         else
749                 cellinfo_of_cell(cell)->right_line = line;
750         return true;
751 }
752
753
754 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
755 {
756         if (!onlycolumn && IsMultiColumn(cell))
757                 return cellinfo_of_cell(cell)->alignment;
758         else
759                 return column_info[column_of_cell(cell)].alignment;
760 }
761
762
763 LyXTabular::VAlignment
764 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
765 {
766         if (!onlycolumn && IsMultiColumn(cell))
767                 return cellinfo_of_cell(cell)->valignment;
768         else
769                 return column_info[column_of_cell(cell)].valignment;
770 }
771
772
773 string const LyXTabular::GetPWidth(int cell) const
774 {
775         if (IsMultiColumn(cell))
776                 return cellinfo_of_cell(cell)->p_width;
777         return column_info[column_of_cell(cell)].p_width;
778 }
779
780
781 string const LyXTabular::GetColumnPWidth(int cell) const
782 {
783         return column_info[column_of_cell(cell)].p_width;
784 }
785
786
787 string const LyXTabular::GetMColumnPWidth(int cell) const
788 {
789         if (IsMultiColumn(cell))
790                 return cellinfo_of_cell(cell)->p_width;
791         return string();
792 }
793
794
795 string const LyXTabular::GetAlignSpecial(int cell, int what) const
796 {
797         if (what == SET_SPECIAL_MULTI)
798                 return cellinfo_of_cell(cell)->align_special;
799         return column_info[column_of_cell(cell)].align_special;
800 }
801
802
803 int LyXTabular::GetWidthOfCell(int cell) const
804 {
805         int const row = row_of_cell(cell);
806         int const column1 = column_of_cell(cell);
807         int const column2 = right_column_of_cell(cell);
808         int result = 0;
809         for (int i = column1; i <= column2; ++i) {
810                 result += cell_info[row][i].width_of_cell;
811         }
812         return result;
813 }
814
815
816 int LyXTabular::GetBeginningOfTextInCell(int cell) const
817 {
818         int x = 0;
819    
820         switch (GetAlignment(cell)){
821         case LYX_ALIGN_CENTER:
822                 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
823                 break;
824         case LYX_ALIGN_RIGHT:
825                 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
826                 // + GetAdditionalWidth(cell);
827                 break;
828         default: /* LYX_ALIGN_LEFT: nothing :-) */ 
829                 break;
830         }
831         
832         // the LaTeX Way :-(
833         x += WIDTH_OF_LINE;
834         return x;
835 }
836
837
838 bool LyXTabular::IsFirstCellInRow(int cell) const
839 {
840         return column_of_cell(cell) == 0;
841 }
842
843
844 int LyXTabular::GetFirstCellInRow(int row) const
845 {
846         if (row > (rows_-1))
847                 row = rows_ - 1;
848         return cell_info[row][0].cellno;
849 }
850
851 bool LyXTabular::IsLastCellInRow(int cell) const
852 {
853         return (right_column_of_cell(cell) == (columns_ - 1));
854 }
855
856
857 int LyXTabular::GetLastCellInRow(int row) const
858 {
859         if (row > (rows_-1))
860                 row = rows_ - 1;
861         return cell_info[row][columns_-1].cellno;
862 }
863
864
865 bool LyXTabular::calculate_width_of_column(int column)
866 {
867         int const old_column_width = column_info[column].width_of_column;
868         int maximum = 0;
869         
870         for (int i = 0; i < rows_; ++i) {
871                 maximum = max(cell_info[i][column].width_of_cell, maximum);
872         }
873         column_info[column].width_of_column = maximum;
874         return (column_info[column].width_of_column != old_column_width);
875 }
876
877
878 //
879 // calculate the with of the column without regarding REAL MultiColumn
880 // cells. This means MultiColumn-cells spanning more than 1 column.
881 //
882 bool LyXTabular::calculate_width_of_column_NMC(int column)
883 {
884         int const old_column_width = column_info[column].width_of_column;
885         int max = 0;
886         for (int i = 0; i < rows_; ++i) {
887                 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
888                         (cell_info[i][column].width_of_cell > max)) {
889                         max = cell_info[i][column].width_of_cell;
890                 }
891         }
892         column_info[column].width_of_column = max;
893         return (column_info[column].width_of_column != old_column_width);
894 }
895
896
897 void LyXTabular::calculate_width_of_tabular()
898 {
899         width_of_tabular = 0;
900         for (int i = 0; i < columns_; ++i) {
901                 width_of_tabular += column_info[i].width_of_column;
902         }
903 }
904
905
906 int LyXTabular::row_of_cell(int cell) const
907 {
908         if (cell >= numberofcells)
909                 return rows_ - 1;
910         else if (cell < 0)
911                 return 0;
912         return rowofcell[cell];
913 }
914
915
916 int LyXTabular::column_of_cell(int cell) const
917 {
918         if (cell >= numberofcells)
919                 return columns_ - 1;
920         else if (cell < 0)
921                 return 0;
922         return columnofcell[cell];
923 }
924
925
926 int LyXTabular::right_column_of_cell(int cell) const
927 {
928         int const row = row_of_cell(cell);
929         int column = column_of_cell(cell);
930         while (column < (columns_ - 1) &&
931                    cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
932                 ++column;
933         return column;
934 }
935
936
937 // Perfect case for a template... (Lgb)
938 // 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 #ifndef NO_COMPABILITY
1510                         // Here we need to insert the inset_ert_contents into the last
1511                         // cell of the tabular.
1512                         owner_->bufferOwner()->insertErtContents(par, pos, font);
1513 #endif
1514                         break;
1515                 }
1516                 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1517                                                                                                                           return_par,
1518                                                                                                                           token, pos,
1519                                                                                                                           depth, font)) {
1520                         // the_end read
1521                         lex.pushToken(token);
1522                         break;
1523                 }
1524                 if (return_par) {
1525                         lex.printError("New Paragraph allocated! This should not happen!");
1526                         lex.pushToken(token);
1527                         delete par;
1528                         par = return_par;
1529                         break;
1530                 }
1531         }
1532         // now we have the par we should fill the insets with this!
1533         int cell = 0;
1534         InsetText * inset = GetCellInset(cell);
1535         int row;
1536
1537         for (int i = 0; i < par->size(); ++i) {
1538                 if (par->isNewline(i)) {
1539                         ++cell;
1540                         if (cell > numberofcells) {
1541                                 lyxerr << "Some error in reading old table format occured!" <<
1542                                         endl << "Terminating when reading cell[" << cell << "]!" <<
1543                                         endl;
1544                                 delete par;
1545                                 return;
1546                         }
1547                         row = row_of_cell(cell);
1548                         if (cont_row_info[row]) {
1549                                 DeleteRow(row);
1550                                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1551                                 while(!IsFirstCellInRow(--cell));
1552                         } else {
1553                                 inset = GetCellInset(cell);
1554                                 continue;
1555                         }
1556                         inset = GetCellInset(cell);
1557                         row = row_of_cell(cell);
1558                         if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1559                         {
1560                                 // insert a space instead
1561                                 par->erase(i);
1562                                 par->insertChar(i, ' ');
1563                         }
1564                 }
1565                 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1566                 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1567         }
1568         delete par;
1569         Reinit();
1570 }
1571
1572
1573 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1574 {
1575         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1576                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1577 }
1578
1579
1580 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1581 {
1582         int const row = row_of_cell(cell);
1583         int const column = column_of_cell(cell);
1584         return  &cell_info[row][column];
1585 }
1586
1587
1588 void LyXTabular::SetMultiColumn(int cell, int number)
1589 {
1590         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1591         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1592         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1593         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1594         for (number--; number > 0; --number) {
1595                 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1596         }
1597         set_row_column_number_info();
1598 }
1599
1600
1601 int LyXTabular::cells_in_multicolumn(int cell) const
1602 {
1603         int const row = row_of_cell(cell);
1604         int column = column_of_cell(cell);
1605         int result = 1;
1606         ++column;
1607         while ((column < columns_) &&
1608                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1609         {
1610                 ++result;
1611                 ++column;
1612         }
1613         return result;
1614 }
1615
1616
1617 int LyXTabular::UnsetMultiColumn(int cell)
1618 {
1619         int const row = row_of_cell(cell);
1620         int column = column_of_cell(cell);
1621         
1622         int result = 0;
1623         
1624         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1625                 cell_info[row][column].multicolumn = CELL_NORMAL;
1626                 ++column;
1627                 while ((column < columns_) &&
1628                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1629                 {
1630                         cell_info[row][column].multicolumn = CELL_NORMAL;
1631                         ++column;
1632                         ++result;
1633                 }
1634         }
1635         set_row_column_number_info();
1636         return result;
1637 }
1638
1639
1640 void LyXTabular::SetLongTabular(bool what)
1641 {
1642         is_long_tabular = what;
1643 }
1644
1645
1646 bool LyXTabular::IsLongTabular() const
1647 {
1648         return is_long_tabular;
1649 }
1650
1651
1652 void LyXTabular::SetRotateTabular(bool flag)
1653 {
1654         rotate = flag;
1655 }
1656
1657
1658 bool LyXTabular::GetRotateTabular() const
1659 {
1660         return rotate;
1661 }
1662
1663
1664 void LyXTabular::SetRotateCell(int cell, bool flag)
1665 {
1666         cellinfo_of_cell(cell)->rotate = flag;
1667 }
1668
1669
1670 bool LyXTabular::GetRotateCell(int cell) const
1671 {
1672         return cellinfo_of_cell(cell)->rotate;
1673 }
1674
1675
1676 bool LyXTabular::NeedRotating() const
1677 {
1678         if (rotate)
1679                 return true;
1680         for (int i = 0; i < rows_; ++i) {
1681                 for (int j = 0; j < columns_; ++j) {
1682                         if (cell_info[i][j].rotate)
1683                                 return true;
1684                 }
1685         }
1686         return false;
1687 }
1688
1689
1690 bool LyXTabular::IsLastCell(int cell) const
1691 {
1692         if ((cell + 1) < numberofcells)
1693                 return false;
1694         return true;
1695 }
1696
1697
1698 int LyXTabular::GetCellAbove(int cell) const
1699 {
1700         if (row_of_cell(cell) > 0)
1701                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1702         return cell;
1703 }
1704
1705
1706 int LyXTabular::GetCellBelow(int cell) const
1707 {
1708         if (row_of_cell(cell) + 1 < rows_)
1709                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1710         return cell;
1711 }
1712
1713
1714 int LyXTabular::GetLastCellAbove(int cell) const
1715 {
1716         if (row_of_cell(cell) <= 0)
1717                 return cell;
1718         if (!IsMultiColumn(cell))
1719                 return GetCellAbove(cell);
1720         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1721 }
1722
1723
1724 int LyXTabular::GetLastCellBelow(int cell) const
1725 {
1726         if (row_of_cell(cell) + 1 >= rows_)
1727                 return cell;
1728         if (!IsMultiColumn(cell))
1729                 return GetCellBelow(cell);
1730         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1731 }
1732
1733
1734 int LyXTabular::GetCellNumber(int row, int column) const
1735 {
1736 #if 0
1737         if (column >= columns_)
1738                 column = columns_ - 1;
1739         else if (column < 0)
1740                 column = 0;
1741         if (row >= rows_)
1742                 row = rows_ - 1;
1743         else if (row < 0)
1744                 row = 0;
1745 #else
1746         lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1747 #endif
1748         return cell_info[row][column].cellno;
1749 }
1750
1751
1752 void LyXTabular::SetUsebox(int cell, BoxType type)
1753 {
1754         cellinfo_of_cell(cell)->usebox = type;
1755 }
1756
1757
1758 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1759 {
1760         if (column_info[column_of_cell(cell)].p_width.empty() &&
1761                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1762                 return BOX_NONE;
1763         if (cellinfo_of_cell(cell)->usebox > 1)
1764                 return cellinfo_of_cell(cell)->usebox;
1765         return UseParbox(cell);
1766 }
1767
1768
1769 void LyXTabular::SetLTHead(int cell, bool first)
1770 {
1771         int const row = row_of_cell(cell);
1772         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1773
1774         if (first) {
1775                 if (endfirsthead == val)
1776                         endfirsthead = 0;
1777                 else
1778                         endfirsthead = val;
1779         } else {
1780                 if (endhead == val)
1781                         endhead = 0;
1782                 else
1783                         endhead = val;
1784         }
1785 }
1786
1787
1788 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1789 {
1790         row = endhead;
1791         if (abs(endhead) > rows_)
1792                 return false;
1793         return (row_of_cell(cell) == abs(endhead) - 1);
1794 }
1795
1796
1797 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1798 {
1799         row = endfirsthead;
1800         if (abs(endfirsthead) > rows_)
1801                 return false;
1802         return (row_of_cell(cell) == abs(endfirsthead) - 1);
1803 }
1804
1805
1806 void LyXTabular::SetLTFoot(int cell, bool last)
1807 {
1808         int const row = row_of_cell(cell);
1809         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1810
1811         if (last) {
1812                 if (endlastfoot == val)
1813                         endlastfoot = 0;
1814                 else
1815                         endlastfoot = val;
1816         } else {
1817                 if (endfoot == val)
1818                         endfoot = 0;
1819                 else
1820                         endfoot = val;
1821         }
1822 }
1823
1824
1825 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1826 {
1827         row = endfoot;
1828         if ((endfoot + 1) > rows_)
1829                 return false;
1830         return (row_of_cell(cell) == abs(endfoot) - 1);
1831 }
1832
1833
1834 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1835 {
1836         row = endlastfoot;
1837         if (abs(endlastfoot) > rows_)
1838                 return false;
1839         return (row_of_cell(cell) == (abs(endlastfoot)-1));
1840 }
1841
1842
1843 void LyXTabular::SetLTNewPage(int cell, bool what)
1844 {
1845         row_info[row_of_cell(cell)].newpage = what;
1846 }
1847
1848
1849 bool LyXTabular::GetLTNewPage(int cell) const
1850 {
1851         return row_info[row_of_cell(cell)].newpage;
1852 }
1853
1854
1855 bool LyXTabular::SetAscentOfRow(int row, int height)
1856 {
1857         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1858                 return false;
1859         row_info[row].ascent_of_row = height;
1860         return true;
1861 }
1862
1863
1864 bool LyXTabular::SetDescentOfRow(int row, int height)
1865 {
1866         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1867                 return false;
1868         row_info[row].descent_of_row = height;
1869         return true;
1870 }
1871
1872
1873 int LyXTabular::GetAscentOfRow(int row) const
1874 {
1875         if (row >= rows_)
1876                 return 0;
1877         return row_info[row].ascent_of_row;
1878 }
1879
1880
1881 int LyXTabular::GetDescentOfRow(int row) const
1882 {
1883         if (row >= rows_)
1884                 return 0;
1885         return row_info[row].descent_of_row;
1886 }
1887
1888
1889 int LyXTabular::GetHeightOfTabular() const
1890 {
1891         int height = 0;
1892
1893         for (int row = 0; row < rows_; ++row)
1894                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1895                         GetAdditionalHeight(row);
1896         return height;
1897 }
1898
1899
1900 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1901 {
1902         if ((row >= rows_) || (column >= columns_))
1903                 return false;
1904         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1905 }
1906
1907
1908 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1909 {
1910         if ((row < 0) || (row >= rows_))
1911                 return 0;
1912
1913         int const fcell = GetFirstCellInRow(row);
1914         int const n = NumberOfCellsInRow(fcell) + fcell;
1915         int tmp = 0;
1916
1917         for (int i = fcell; i < n; ++i) {
1918                 if (TopLine(i))
1919                         ++tmp;
1920         }
1921         if (tmp == (n - fcell)){
1922                 os << "\\hline ";
1923         } else if (tmp) {
1924                 for (int i = fcell; i < n; ++i) {
1925                         if (TopLine(i)) {
1926                                 os << "\\cline{"
1927                                    << column_of_cell(i) + 1
1928                                    << '-'
1929                                    << right_column_of_cell(i) + 1
1930                                    << "} ";
1931                         }
1932                 }
1933         } else {
1934                 return 0;
1935         }
1936         os << "\n";
1937         return 1;
1938 }
1939
1940
1941 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1942 {
1943         if ((row < 0) || (row >= rows_))
1944                 return 0;
1945
1946         int const fcell = GetFirstCellInRow(row);
1947         int const n = NumberOfCellsInRow(fcell) + fcell;
1948         int tmp = 0;
1949
1950         for (int i = fcell; i < n; ++i) {
1951                 if (BottomLine(i))
1952                         ++tmp;
1953         }
1954         if (tmp == (n-fcell)){
1955                 os << "\\hline";
1956         } else if (tmp) {
1957                 for (int i = fcell; i < n; ++i) {
1958                         if (BottomLine(i)) {
1959                                 os << "\\cline{"
1960                                    << column_of_cell(i) + 1
1961                                    << '-'
1962                                    << right_column_of_cell(i) + 1
1963                                    << "} ";
1964                         }
1965                 }
1966         } else {
1967                 return 0;
1968         }
1969         os << "\n";
1970         return 1;
1971 }
1972
1973
1974 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1975 {
1976         int ret = 0;
1977
1978         if (GetRotateCell(cell)) {
1979                 os << "\\begin{sideways}\n";
1980                 ++ret;
1981         }
1982         if (IsMultiColumn(cell)) {
1983                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1984                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1985                         os << cellinfo_of_cell(cell)->align_special << "}{";
1986                 } else {
1987                         if (LeftLine(cell) &&
1988                                 (IsFirstCellInRow(cell) || 
1989                                  (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1990                                   !RightLine(cell-1, true))))
1991                         {
1992                                 os << '|';
1993                         }
1994                         if (!GetPWidth(cell).empty()) {
1995                                 switch (GetVAlignment(cell)) {
1996                                 case LYX_VALIGN_TOP:
1997                                         os << "p";
1998                                         break;
1999                                 case LYX_VALIGN_CENTER:
2000                                         os << "m";
2001                                         break;
2002                                 case LYX_VALIGN_BOTTOM:
2003                                         os << "b";
2004                                         break;
2005                                 }
2006                                 os << "{" << GetPWidth(cell) << '}';
2007                         } else {
2008                                 switch (GetAlignment(cell)) {
2009                                 case LYX_ALIGN_LEFT:
2010                                         os << 'l';
2011                                         break;
2012                                 case LYX_ALIGN_RIGHT:
2013                                         os << 'r';
2014                                         break;
2015                                 default:
2016                                         os << 'c';
2017                                         break;
2018                                 }
2019                         }
2020                         if (RightLine(cell))
2021                                 os << '|';
2022                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2023                                 LeftLine(cell+1))
2024                                 os << '|';
2025                         os << "}{";
2026                 }
2027         }
2028         if (GetUsebox(cell) == BOX_PARBOX) {
2029                 os << "\\parbox[";
2030                 switch (GetVAlignment(cell)) {
2031                 case LYX_VALIGN_TOP:
2032                         os << "t";
2033                         break;
2034                 case LYX_VALIGN_CENTER:
2035                         os << "c";
2036                         break;
2037                 case LYX_VALIGN_BOTTOM:
2038                         os << "b";
2039                         break;
2040                 }
2041                 os << "]{" << GetPWidth(cell) << "}{";
2042         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2043                 os << "\\begin{minipage}[";
2044                 switch (GetVAlignment(cell)) {
2045                 case LYX_VALIGN_TOP:
2046                         os << "t";
2047                         break;
2048                 case LYX_VALIGN_CENTER:
2049                         os << "m";
2050                         break;
2051                 case LYX_VALIGN_BOTTOM:
2052                         os << "b";
2053                         break;
2054                 }
2055                 os << "]{" << GetPWidth(cell) << "}\n";
2056                 ++ret;
2057         }
2058         return ret;
2059 }
2060
2061
2062 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2063 {
2064         int ret = 0;
2065
2066         // usual cells
2067         if (GetUsebox(cell) == BOX_PARBOX)
2068                 os << "}";
2069         else if (GetUsebox(cell) == BOX_MINIPAGE) {
2070                 os << "%\n\\end{minipage}";
2071                 ret += 2;
2072         }
2073         if (IsMultiColumn(cell)){
2074                 os << '}';
2075         }
2076         if (GetRotateCell(cell)) {
2077                 os << "%\n\\end{sideways}";
2078                 ++ret;
2079         }
2080         return ret;
2081 }
2082
2083
2084 int LyXTabular::Latex(Buffer const * buf,
2085                                           ostream & os, bool fragile, bool fp) const
2086 {
2087         int ret = 0;
2088         int cell = 0;
2089
2090         //+---------------------------------------------------------------------
2091         //+                      first the opening preamble                    +
2092         //+---------------------------------------------------------------------
2093
2094         if (rotate) {
2095                 os << "\\begin{sideways}\n";
2096                 ++ret;
2097         }
2098         if (is_long_tabular)
2099                 os << "\\begin{longtable}{";
2100         else
2101                 os << "\\begin{tabular}{";
2102         for (int i = 0; i < columns_; ++i) {
2103                 if (column_info[i].left_line)
2104                         os << '|';
2105                 if (!column_info[i].align_special.empty()) {
2106                         os << column_info[i].align_special;
2107                 } else if (!column_info[i].p_width.empty()) {
2108                         switch (column_info[i].valignment) {
2109                         case LYX_VALIGN_TOP:
2110                                 os << "p";
2111                                 break;
2112                         case LYX_VALIGN_CENTER:
2113                                 os << "m";
2114                                 break;
2115                         case LYX_VALIGN_BOTTOM:
2116                                 os << "b";
2117                                 break;
2118                         }
2119                         os << "{"
2120                            << column_info[i].p_width
2121                            << '}';
2122                 } else {
2123                         switch (column_info[i].alignment) {
2124                         case LYX_ALIGN_LEFT:
2125                                 os << 'l';
2126                                 break;
2127                         case LYX_ALIGN_RIGHT:
2128                                 os << 'r';
2129                                 break;
2130                         default:
2131                                 os << 'c';
2132                                 break;
2133                         }
2134                 }
2135                 if (column_info[i].right_line)
2136                         os << '|';
2137         }
2138         os << "}\n";
2139         ++ret;
2140
2141         //+---------------------------------------------------------------------
2142         //+                      the single row and columns (cells)            +
2143         //+---------------------------------------------------------------------
2144
2145         for (int i = 0; i < rows_; ++i) {
2146                 ret += TeXTopHLine(os, i);
2147                 int bret = ret;
2148                 if (IsLongTabular()) {
2149                         if ((endhead < 0) && (i == (abs(endhead)-1))) {
2150                                 os << "\\endhead\n";
2151                                 ++ret;
2152                         }
2153                         if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2154                                 os << "\\endfirsthead\n";
2155                                 ++ret;
2156                         }
2157                         if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2158                                 os << "\\endfoot\n";
2159                                 ++ret;
2160                         }
2161                         if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2162                                 os << "\\endlastfoot\n";
2163                                 ++ret;
2164                         }
2165                 }
2166                 if (ret > bret) {
2167                         ret += TeXBottomHLine(os, i-1);
2168                         ret += TeXTopHLine(os, i);
2169                 }
2170                 for (int j = 0; j < columns_; ++j) {
2171                         if (IsPartOfMultiColumn(i,j))
2172                                 continue;
2173                         ret += TeXCellPreamble(os, cell);
2174                         InsetText * inset = GetCellInset(cell);
2175
2176                         bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2177                                         inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2178
2179                         if (rtl)
2180                                 os << "\\R{";
2181                         ret += inset->latex(buf, os, fragile, fp);
2182                         if (rtl)
2183                                 os << "}";
2184
2185                         ret += TeXCellPostamble(os, cell);
2186                         if (!IsLastCellInRow(cell)) { // not last cell in row
2187                                 os << "&\n";
2188                                 ++ret;
2189                         }
2190                         ++cell;
2191                 }
2192                 os << "\\\\\n";
2193                 ret += TeXBottomHLine(os, i);
2194                 bret = ret;
2195                 if (IsLongTabular()) {
2196                         if ((endhead > 0) && (i == (endhead - 1))) {
2197                                 os << "\\endhead\n";
2198                                 ++ret;
2199                         }
2200                         if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2201                                 os << "\\endfirsthead\n";
2202                                 ++ret;
2203                         }
2204                         if ((endfoot > 0) && (i == (endfoot - 1))) {
2205                                 os << "\\endfoot\n";
2206                                 ++ret;
2207                         }
2208                         if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2209                                 os << "\\endlastfoot\n";
2210                                 ++ret;
2211                         }
2212 //          if (ret > bret)
2213 //              ret += TeXBottomHLine(os, i);
2214                         if (row_info[i].newpage) {
2215                                 os << "\\newpage\n";
2216                                 ++ret;
2217                         }
2218                 }
2219         }
2220
2221         //+---------------------------------------------------------------------
2222         //+                      the closing of the tabular                    +
2223         //+---------------------------------------------------------------------
2224
2225         if (is_long_tabular)
2226                 os << "\\end{longtable}";
2227         else
2228                 os << "\\end{tabular}";
2229         if (rotate) {
2230                 os << "\n\\end{sideways}";
2231                 ++ret;
2232         }
2233
2234         return ret;
2235 }
2236
2237
2238 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2239 {
2240         int ret = 0;
2241
2242         //+---------------------------------------------------------------------
2243         //+                      first the opening preamble                    +
2244         //+---------------------------------------------------------------------
2245
2246         os << "<tgroup cols=\"" << columns_
2247            << "\" colsep=\"1\" rowsep=\"1\">\n";
2248         
2249         for (int i = 0; i < columns_; ++i) {
2250                 os << "<colspec colname=\"col" << i << "\" align=\"";
2251                 switch (column_info[i].alignment) {
2252                 case LYX_ALIGN_LEFT:
2253                         os << "left";
2254                         break;
2255                 case LYX_ALIGN_RIGHT:
2256                         os << "right";
2257                         break;
2258                 default:
2259                         os << "center";
2260                         break;
2261                 }
2262                 os << "\"/>\n";
2263                 ++ret;
2264         }
2265
2266         //+---------------------------------------------------------------------
2267         //+                      the single row and columns (cells)            +
2268         //+---------------------------------------------------------------------
2269
2270         int cell = 0;
2271         os << "<tbody>\n";
2272         for (int i = 0; i < rows_; ++i) {
2273                 os << "<row>\n";
2274                 for (int j = 0; j < columns_; ++j) {
2275                         if (IsPartOfMultiColumn(i, j))
2276                                 continue;
2277                 
2278                         os << "<entry align=\"";
2279                         switch (GetAlignment(cell)) {
2280                         case LYX_ALIGN_LEFT:
2281                                 os << "left";
2282                                 break;
2283                         case LYX_ALIGN_RIGHT:
2284                                 os << "right";
2285                                 break;
2286                         default:
2287                                 os << "center";
2288                                 break;
2289                         }
2290                 
2291                         os << "\" valign=\"";
2292                         switch (GetVAlignment(cell)) {
2293                         case LYX_VALIGN_TOP:
2294                                 os << "top";
2295                                 break;
2296                         case LYX_VALIGN_BOTTOM:
2297                                 os << "bottom";
2298                                 break;
2299                         case LYX_VALIGN_CENTER:
2300                                 os << "middle";
2301                         }
2302                         os << "\"";
2303                 
2304                         if (IsMultiColumn(cell)) {
2305                                 os << " namest=\"col" << j << "\" ";
2306                                 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2307                         }
2308                 
2309                         os << ">";
2310                         ret += GetCellInset(cell)->docBook(buf, os);
2311                         os << "</entry>";
2312                         ++cell;
2313                 }
2314                 os << "</row>\n";
2315         }
2316         os << "</tbody>\n";
2317         //+---------------------------------------------------------------------
2318         //+                      the closing of the tabular                    +
2319         //+---------------------------------------------------------------------
2320
2321         os << "</tgroup>";
2322         ++ret;
2323
2324         return ret;
2325 }
2326
2327
2328 namespace {
2329
2330         inline
2331         void print_n_chars(ostream & os, unsigned char ch, int n)
2332         {
2333                 os << string(n, ch);
2334         }
2335
2336 } // namespace anon
2337
2338
2339 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2340                                                           vector<unsigned int> const & clen) const
2341 {
2342         int const fcell = GetFirstCellInRow(row);
2343         int const n = NumberOfCellsInRow(fcell) + fcell;
2344         int tmp = 0;
2345
2346         for (int i = fcell; i < n; ++i) {
2347                 if (TopLine(i)) {
2348                         ++tmp;
2349                         break;
2350                 }
2351         }
2352         if (!tmp)
2353                 return 0;
2354
2355         unsigned char ch;
2356         for (int i = fcell; i < n; ++i) {
2357                 if (TopLine(i)) {
2358                         if (LeftLine(i))
2359                                 os << "+-";
2360                         else
2361                                 os << "--";
2362                         ch = '-';
2363                 } else {
2364                         os << "  ";
2365                         ch = ' ';
2366                 }
2367                 int column = column_of_cell(i);
2368                 int len = clen[column];
2369                 while(IsPartOfMultiColumn(row, ++column))
2370                         len += clen[column] + 4;
2371                 print_n_chars(os, ch, len);
2372                 if (TopLine(i)) {
2373                         if (RightLine(i))
2374                                 os << "-+";
2375                         else
2376                                 os << "--";
2377                 } else {
2378                         os << "  ";
2379                 }
2380         }
2381         os << endl;
2382         return 1;
2383 }
2384
2385
2386 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2387                                                                  vector<unsigned int> const & clen) const
2388 {
2389         int const fcell = GetFirstCellInRow(row);
2390         int const n = NumberOfCellsInRow(fcell) + fcell;
2391         int tmp = 0;
2392
2393         for (int i = fcell; i < n; ++i) {
2394                 if (BottomLine(i)) {
2395                         ++tmp;
2396                         break;
2397                 }
2398         }
2399         if (!tmp)
2400                 return 0;
2401
2402         unsigned char ch;
2403         for (int i = fcell; i < n; ++i) {
2404                 if (BottomLine(i)) {
2405                         if (LeftLine(i))
2406                                 os << "+-";
2407                         else
2408                                 os << "--";
2409                         ch = '-';
2410                 } else {
2411                         os << "  ";
2412                         ch = ' ';
2413                 }
2414                 int column = column_of_cell(i);
2415                 int len = clen[column];
2416                 while(IsPartOfMultiColumn(row, ++column))
2417                         len += clen[column] + 4;
2418                 print_n_chars(os, ch, len);
2419                 if (BottomLine(i)) {
2420                         if (RightLine(i))
2421                                 os << "-+";
2422                         else
2423                                 os << "--";
2424                 } else {
2425                         os << "  ";
2426                 }
2427         }
2428         os << endl;
2429         return 1;
2430 }
2431
2432
2433 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2434                                                            int cell, int row, int column,
2435                                                            vector<unsigned int> const & clen) const
2436 {
2437         ostringstream sstr;
2438         int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2439
2440         if (LeftLine(cell))
2441                 os << "| ";
2442         else
2443                 os << "  ";
2444
2445         unsigned int len1 = sstr.str().length();
2446         unsigned int len2 = clen[column];
2447         while(IsPartOfMultiColumn(row, ++column))
2448                 len2 += clen[column] + 4;
2449         len2 -= len1;
2450
2451         switch (GetAlignment(cell)) {
2452         default:
2453         case LYX_ALIGN_LEFT:
2454                 len1 = 0;
2455                 break;
2456         case LYX_ALIGN_RIGHT:
2457                 len1 = len2;
2458                 len2 = 0;
2459                 break;
2460         case LYX_ALIGN_CENTER:
2461                 len1 = len2 / 2;
2462                 len2 -= len1;
2463                 break;
2464         }
2465
2466         for (unsigned int i = 0; i < len1; ++i)
2467                 os << " ";
2468         os << sstr.str();
2469         for (unsigned int i = 0; i < len2; ++i)
2470                 os << " ";
2471         if (RightLine(cell))
2472                 os << " |";
2473         else
2474                 os << "  ";
2475
2476         return ret;
2477 }
2478
2479
2480 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2481 {
2482         int ret = 0;
2483
2484         //+---------------------------------------------------------------------
2485         //+           first calculate the width of the single columns          +
2486         //+---------------------------------------------------------------------
2487         vector<unsigned int> clen(columns_);
2488
2489         // first all non (real) multicolumn cells!
2490         for (int j = 0; j < columns_; ++j) {
2491                 clen[j] = 0;
2492                 for (int i = 0; i < rows_; ++i) {
2493                         int cell = GetCellNumber(i, j);
2494                         if (IsMultiColumn(cell, true))
2495                                 continue;
2496                         ostringstream sstr;
2497                         GetCellInset(cell)->ascii(buf, sstr, 0);
2498                         if (clen[j] < sstr.str().length())
2499                                 clen[j] = sstr.str().length();
2500                 }
2501         }
2502         // then all (real) multicolumn cells!
2503         for (int j = 0; j < columns_; ++j) {
2504                 for (int i = 0; i < rows_; ++i) {
2505                         int cell = GetCellNumber(i, j);
2506                         if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2507                                 continue;
2508                         ostringstream sstr;
2509                         GetCellInset(cell)->ascii(buf, sstr, 0);
2510                         int len = int(sstr.str().length());
2511                         int const n = cells_in_multicolumn(cell);
2512                         for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2513                                 len -= clen[k];
2514                         if (len > int(clen[j + n - 1]))
2515                                 clen[j + n - 1] = len;
2516                 }
2517         }
2518         int cell = 0;
2519         for (int i = 0; i < rows_; ++i) {
2520                 AsciiTopHLine(os, i, clen);
2521                 for (int j = 0; j < columns_; ++j) {
2522                         if (IsPartOfMultiColumn(i,j))
2523                                 continue;
2524                         ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2525                         ++cell;
2526                 }
2527                 os << endl;
2528                 AsciiBottomHLine(os, i, clen);
2529         }
2530         return ret;
2531 }
2532
2533
2534 InsetText * LyXTabular::GetCellInset(int cell) const
2535 {
2536         cur_cell = cell;
2537         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2538 }
2539
2540
2541 InsetText * LyXTabular::GetCellInset(int row, int column) const
2542 {
2543         cur_cell = GetCellNumber(row, column);
2544         return & cell_info[row][column].inset;
2545 }
2546
2547
2548 void LyXTabular::Validate(LaTeXFeatures & features) const
2549 {
2550         if (IsLongTabular())
2551                 features.longtable = true;
2552         if (NeedRotating())
2553                 features.rotating = true;
2554         for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2555                 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2556                         features.array = true;
2557                 GetCellInset(cell)->validate(features);
2558         }
2559 }
2560
2561
2562 std::vector<string> const LyXTabular::getLabelList() const
2563 {
2564         std::vector<string> label_list;
2565         for (int i = 0; i < rows_; ++i)
2566                 for (int j = 0; j < columns_; ++j) {
2567                         std::vector<string> const l =
2568                                 GetCellInset(i, j)->getLabelList();
2569                         label_list.insert(label_list.end(),
2570                                           l.begin(), l.end());
2571                 }
2572         return label_list;
2573 }
2574
2575                         
2576 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2577 {
2578         Paragraph * par = GetCellInset(cell)->paragraph();
2579
2580         for (; par; par = par->next()) {
2581                 for (int i = 0; i < par->size(); ++i) {
2582                         if (par->getChar(i)     == Paragraph::META_NEWLINE)
2583                                 return BOX_PARBOX;
2584                 }
2585         }
2586         return BOX_NONE;
2587 }
2588
2589 /* Emacs:
2590  * Local variables:
2591  * tab-width: 4
2592  * End:
2593  * vi:set tabstop=4:
2594  */