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