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