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