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