]> git.lyx.org Git - lyx.git/blob - src/tabular.C
71adb1843006fa9ab5cf883705da8081b34195a2
[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 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     getline(is, str);
1229     while(str.empty())
1230         getline(is, str);
1231 }
1232
1233
1234 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1235 {
1236         string line;
1237         istream & is = lex.getStream();
1238
1239         l_getline(is, line);
1240         if (!prefixIs(line, "<lyxtabular ")
1241             && !prefixIs(line, "<LyXTabular ")) {
1242                 OldFormatRead(lex, line);
1243                 return;
1244         }
1245
1246         int version;
1247         if (!getTokenValue(line, "version", version))
1248                 return;
1249         if (version == 1)
1250                 ReadOld(buf, is, lex, line);
1251         else if (version == 2)
1252                 ReadNew(buf, is, lex, line);
1253 }
1254
1255
1256 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1257                          LyXLex & lex, string const & l)
1258 {
1259     string line(l);
1260     int rows_arg;
1261     if (!getTokenValue(line, "rows", rows_arg))
1262         return;
1263     int columns_arg;
1264     if (!getTokenValue(line, "columns", columns_arg))
1265         return;
1266     Init(rows_arg, columns_arg);
1267     l_getline(is, line);
1268     if (!prefixIs(line, "<features")) {
1269         lyxerr << "Wrong tabular format (expected <features ...> got" <<
1270             line << ")" << endl;
1271         return;
1272     }
1273     getTokenValue(line, "rotate", rotate);
1274     getTokenValue(line, "islongtable", is_long_tabular);
1275     getTokenValue(line, "endhead", endhead);
1276     getTokenValue(line, "endfirsthead", endfirsthead);
1277     getTokenValue(line, "endfoot", endfoot);
1278     getTokenValue(line, "endlastfoot", endlastfoot);
1279
1280     for (int j = 0; j < columns_; ++j) {
1281         l_getline(is,line);
1282         if (!prefixIs(line,"<column")) {
1283             lyxerr << "Wrong tabular format (expected <column ...> got" <<
1284                 line << ")" << endl;
1285             return;
1286         }
1287         getTokenValue(line, "alignment", column_info[j].alignment);
1288         getTokenValue(line, "valignment", column_info[j].valignment);
1289         getTokenValue(line, "leftline", column_info[j].left_line);
1290         getTokenValue(line, "rightline", column_info[j].right_line);
1291         getTokenValue(line, "width", column_info[j].p_width);
1292         getTokenValue(line, "special", column_info[j].align_special);
1293     }
1294
1295     for (int i = 0; i < rows_; ++i) {
1296         l_getline(is, line);
1297         if (!prefixIs(line, "<row")) {
1298             lyxerr << "Wrong tabular format (expected <row ...> got" <<
1299                 line << ")" << endl;
1300             return;
1301         }
1302         getTokenValue(line, "topline", row_info[i].top_line);
1303         getTokenValue(line, "bottomline", row_info[i].bottom_line);
1304         getTokenValue(line, "newpage", row_info[i].newpage);
1305         for (int j = 0; j < columns_; ++j) {
1306             l_getline(is, line);
1307             if (!prefixIs(line, "<cell")) {
1308                 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1309                     line << ")" << endl;
1310                 return;
1311             }
1312             getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1313             getTokenValue(line, "alignment", cell_info[i][j].alignment);
1314             getTokenValue(line, "valignment", cell_info[i][j].valignment);
1315             getTokenValue(line, "topline", cell_info[i][j].top_line);
1316             getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1317             getTokenValue(line, "leftline", cell_info[i][j].left_line);
1318             getTokenValue(line, "rightline", cell_info[i][j].right_line);
1319             getTokenValue(line, "rotate", cell_info[i][j].rotate);
1320             getTokenValue(line, "usebox", cell_info[i][j].usebox);
1321             getTokenValue(line, "width", cell_info[i][j].p_width);
1322             getTokenValue(line, "special", cell_info[i][j].align_special);
1323             l_getline(is, line);
1324             if (prefixIs(line, "\\begin_inset")) {
1325                 cell_info[i][j].inset.Read(buf, lex);
1326                 l_getline(is, line);
1327             }
1328             if (line != "</cell>") {
1329                 lyxerr << "Wrong tabular format (expected </cell> got" <<
1330                     line << ")" << endl;
1331                 return;
1332             }
1333         }
1334         l_getline(is, line);
1335         if (line != "</row>") {
1336             lyxerr << "Wrong tabular format (expected </row> got" <<
1337                 line << ")" << endl;
1338             return;
1339         }
1340     }
1341     while (line != "</lyxtabular>") {
1342         l_getline(is, line);
1343     }
1344     set_row_column_number_info();
1345 }
1346
1347
1348 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1349 {
1350     int version;
1351     int i, j;
1352     int rows_arg = 0;
1353     int columns_arg = 0;
1354     int is_long_tabular_arg = false;
1355     int rotate_arg = false;
1356     int a = -1;
1357     int b = -1;
1358     int c = -1;
1359     int d = -1;
1360     int e = 0;
1361     int f = 0;
1362     int g = 0;
1363         
1364     istream & is = lex.getStream();
1365     string s(fl);
1366     if (s.length() > 8)
1367         version = lyx::atoi(s.substr(8, string::npos));
1368     else
1369         version = 1;
1370
1371     vector<int> cont_row_info;
1372
1373     if (version < 5) {
1374         lyxerr << "Tabular format < 5 is not supported anymore\n"
1375             "Get an older version of LyX (< 1.1.x) for conversion!"
1376                << endl;
1377         WriteAlert(_("Warning:"),
1378                    _("Tabular format < 5 is not supported anymore\n"),
1379                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1380         if (version > 2) {
1381             is >> rows_arg >> columns_arg >> is_long_tabular_arg
1382                >> rotate_arg >> a >> b >> c >> d;
1383         } else
1384             is >> rows_arg >> columns_arg;
1385         Init(rows_arg, columns_arg);
1386         cont_row_info = vector<int>(rows_arg);
1387         SetLongTabular(is_long_tabular_arg);
1388         SetRotateTabular(rotate_arg);
1389         string tmp;
1390         for (i = 0; i < rows_; ++i) {
1391             getline(is, tmp);
1392             cont_row_info[i] = false;
1393         }
1394         for (i = 0; i < columns_; ++i) {
1395             getline(is, tmp);
1396         }
1397         for (i = 0; i < rows_; ++i) {
1398             for (j = 0; j < columns_; ++j) {
1399                 getline(is, tmp);
1400             }
1401         }
1402     } else {
1403         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1404            >> rotate_arg >> a >> b >> c >> d;
1405         Init(rows_arg, columns_arg);
1406         cont_row_info = vector<int>(rows_arg);
1407         SetLongTabular(is_long_tabular_arg);
1408         SetRotateTabular(rotate_arg);
1409         endhead = a + 1;
1410         endfirsthead = b + 1;
1411         endfoot = c + 1;
1412         endlastfoot = d + 1;
1413         for (i = 0; i < rows_; ++i) {
1414             a = b = c = d = e = f = g = 0;
1415             is >> a >> b >> c >> d;
1416             row_info[i].top_line = a;
1417             row_info[i].bottom_line = b;
1418             cont_row_info[i] = c;
1419             row_info[i].newpage = d;
1420         }
1421         for (i = 0; i < columns_; ++i) {
1422             string s1;
1423             string s2;
1424             is >> a >> b >> c;
1425 #if 1
1426             char ch; // skip '"'
1427             is >> ch;
1428 #else
1429             // ignore is buggy but we will use it later (Lgb)
1430             is.ignore(); // skip '"'
1431 #endif    
1432             getline(is, s1, '"');
1433 #if 1
1434             is >> ch; // skip '"'
1435 #else
1436             // ignore is buggy but we will use it later (Lgb)
1437             is.ignore(); // skip '"'
1438 #endif
1439             getline(is, s2, '"');
1440             column_info[i].alignment = static_cast<LyXAlignment>(a);
1441             column_info[i].left_line = b;
1442             column_info[i].right_line = c;
1443             column_info[i].p_width = s1;
1444             column_info[i].align_special = s2;
1445         }
1446         for (i = 0; i < rows_; ++i) {
1447             for (j = 0; j < columns_; ++j) {
1448                 string s1;
1449                 string s2;
1450                 is >> a >> b >> c >> d >> e >> f >> g;
1451 #if 1
1452                 char ch;
1453                 is >> ch; // skip '"'
1454 #else
1455                 // ignore is buggy but we will use it later (Lgb)
1456                 is.ignore(); // skip '"'
1457 #endif
1458                 getline(is, s1, '"');
1459 #if 1
1460                 is >> ch; // skip '"'
1461 #else
1462                 // ignore is buggy but we will use it later (Lgb)
1463                 is.ignore(); // skip '"'
1464 #endif
1465                 getline(is, s2, '"');
1466                 cell_info[i][j].multicolumn = static_cast<char>(a);
1467                 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1468                 cell_info[i][j].top_line = static_cast<char>(c);
1469                 cell_info[i][j].bottom_line = static_cast<char>(d);
1470                 cell_info[i][j].left_line = column_info[j].left_line;
1471                 cell_info[i][j].right_line = column_info[j].right_line;
1472                 cell_info[i][j].rotate = static_cast<bool>(f);
1473                 cell_info[i][j].usebox = static_cast<BoxType>(g);
1474                 cell_info[i][j].align_special = s1;
1475                 cell_info[i][j].p_width = s2;
1476             }
1477         }
1478     }
1479     set_row_column_number_info(true);
1480
1481     LyXParagraph * par = new LyXParagraph;
1482     LyXParagraph * return_par = 0;
1483 #ifndef NEW_INSETS
1484     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1485     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1486 #endif
1487     string tmptok;
1488     int pos = 0;
1489     char depth = 0;
1490     LyXFont font(LyXFont::ALL_INHERIT);
1491     font.setLanguage(owner_->BufferOwner()->GetLanguage());
1492
1493     while (lex.IsOK()) {
1494         lex.nextToken();
1495         string const token = lex.GetString();
1496         if (token.empty())
1497             continue;
1498         if (token == "\\layout"
1499             || token == "\\end_float"
1500             || token == "\\end_deeper") {
1501             lex.pushToken(token);
1502             break;
1503         }
1504         if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1505                                                               return_par,
1506                                                               token, pos,
1507                                                               depth, font
1508 #ifndef NEW_INSETS
1509                                                               ,
1510                                                               footnoteflag,
1511                                                               footnotekind
1512 #endif
1513                 ))
1514         {
1515             // the_end read
1516             lex.pushToken(token);
1517             break;
1518         }
1519         if (return_par) {
1520             lex.printError("New Paragraph allocated! This should not happen!");
1521             lex.pushToken(token);
1522             delete par;
1523             par = return_par;
1524             break;
1525         }
1526     }
1527     // now we have the par we should fill the insets with this!
1528     int cell = 0;
1529     InsetText * inset = GetCellInset(cell);
1530     int row;
1531
1532     for (int i = 0; i < par->Last(); ++i) {
1533         if (par->IsNewline(i)) {
1534             ++cell;
1535             if (cell > GetNumberOfCells()) {
1536                 lyxerr << "Some error in reading old table format occured!" <<
1537                     endl << "Terminating when reading cell[" << cell << "]!" <<
1538                     endl;
1539                 delete par;
1540                 return;
1541             }
1542             row = row_of_cell(cell);
1543             if (cont_row_info[row]) {
1544                 DeleteRow(row);
1545                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1546                 while(!IsFirstCellInRow(--cell));
1547             } else {
1548                 inset = GetCellInset(cell);
1549                 continue;
1550             }
1551             inset = GetCellInset(cell);
1552             row = row_of_cell(cell);
1553             if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1554             {
1555                 // insert a space instead
1556                 par->Erase(i);
1557                 par->InsertChar(i, ' ');
1558             }
1559         }
1560         par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1561         inset->par->InsertFromMinibuffer(inset->par->Last());
1562     }
1563     delete par;
1564     Reinit();
1565 }
1566
1567
1568 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1569 {
1570     int const i = isColumn ? cell : column_of_cell(cell);
1571         
1572     if (!isColumn && IsMultiColumn(cell)) {
1573        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1574            return cellinfo_of_cell(cell)->align_special;
1575        } else {
1576            switch (GetAlignment(cell)) {
1577            case LYX_ALIGN_LEFT:
1578                return "left";
1579            case LYX_ALIGN_RIGHT:
1580                return "right";
1581            default:
1582                return "center";
1583            }
1584        }
1585     } else {
1586        if (!column_info[i].align_special.empty()) {
1587            return column_info[i].align_special;
1588        }
1589 #ifdef IGNORE_THIS_FOR_NOW
1590        else if (!column_info[i].p_width.empty()) {
1591            file += "p{";
1592            file += column_info[i].p_width;
1593            file += '}';
1594        }
1595 #endif
1596        else {
1597            switch (column_info[i].alignment) {
1598            case LYX_ALIGN_LEFT:
1599                return "left";
1600            case LYX_ALIGN_RIGHT:
1601                return "right";
1602            default:
1603                return "center";
1604            }
1605        }
1606     }
1607 }
1608
1609
1610 // cell <0 will tex the preamble
1611 // returns the number of printed newlines
1612 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1613 {
1614     int ret = 0;
1615     if (IsLastCell(cell)) {
1616             os << newlineAndDepth(--depth)
1617                << "</ENTRY>"
1618                << newlineAndDepth(--depth)
1619                << "</ROW>"
1620                << newlineAndDepth(--depth)
1621                << "</TBODY>"
1622                << newlineAndDepth(--depth);
1623         if (is_long_tabular)
1624                 os << "</TGROUP>";
1625         else
1626                 os << "</TGROUP>"
1627                    << newlineAndDepth(--depth);
1628         ret += 4;
1629     } else {
1630         if (cell < 0) {
1631             // preamble
1632             if (is_long_tabular)
1633                     os << "<TGROUP ";
1634             else
1635                     os << "<TGROUP ";
1636             os << "COLS='"
1637                << columns_
1638                << "' COLSEP='1' ROWSEP='1'>"
1639                << newlineAndDepth(++depth);
1640             ++ret;
1641             for (int i = 0; i < columns_; ++i) {
1642                     os << "<COLSPEC ALIGN='"
1643                        << GetDocBookAlign(i, true)
1644                        << "' COLNAME='col"
1645                        << i + 1
1646                        << "' COLNUM='"
1647                        << i + 1
1648                        << "' COLSEP='";
1649                if (i == (columns_-1)) {
1650                        os << '1';
1651                } else {
1652                    if (column_info[i].right_line ||
1653                        column_info[i+1].left_line)
1654                            os << '1';
1655                    else
1656                            os << '0';
1657                }
1658                os << "'>"
1659                   << newlineAndDepth(depth);
1660                 ++ret;
1661 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1662                 if (column_info[i].left_line)
1663                         os << '|';
1664 #endif
1665             }
1666             os << "<TBODY>"
1667                << newlineAndDepth(++depth)
1668                << "<ROW>"
1669                << newlineAndDepth(++depth)
1670                << "<ENTRY ALIGN='"
1671                << GetDocBookAlign(0)
1672                << "'";
1673            if (IsMultiColumn(0)) {
1674                    os << " NAMEST='col1' NAMEEND='col"
1675                       << cells_in_multicolumn(0)
1676                       << "'";
1677            }
1678            os << ">"
1679               << newlineAndDepth(++depth);
1680             ret += 3;
1681         } else {
1682             if (IsLastCellInRow(cell)) {
1683                     os << newlineAndDepth(--depth)
1684                        << "</ENTRY>"
1685                        << newlineAndDepth(--depth)
1686                        << "</ROW>"
1687                        << newlineAndDepth(depth)
1688                        << "<ROW>"
1689                        << newlineAndDepth(++depth)
1690                        << "<ENTRY ALIGN='"
1691                        << GetDocBookAlign(cell + 1)
1692                        << "' VALIGN='middle'";
1693                if (IsMultiColumn(cell + 1)) {
1694                        os << " NAMEST='col"
1695                           << column_of_cell(cell + 1) + 1
1696                           << "' NAMEEND='col"
1697                           << column_of_cell(cell + 1) +
1698                                cells_in_multicolumn(cell + 1)
1699                           << "'";
1700                }
1701                os << ">"
1702                   << newlineAndDepth(++depth);
1703                 ret += 4;
1704             } else {
1705                     os << newlineAndDepth(--depth)
1706                        << "</ENTRY>"
1707                        << newlineAndDepth(depth)
1708                        << "<ENTRY ALIGN='"
1709                        << GetDocBookAlign(cell + 1)
1710                        << "' VALIGN='middle'";
1711                if (IsMultiColumn(cell + 1)) {
1712                        os << " NAMEST='col"
1713                           << column_of_cell(cell + 1) + 1
1714                           << "' NAMEEND='col"
1715                           << column_of_cell(cell + 1) +
1716                                cells_in_multicolumn(cell + 1)
1717                           << "'";
1718                }
1719                os << ">"
1720                   << newlineAndDepth(++depth);
1721                 ret += 3;
1722             }
1723         }
1724     }
1725     return ret;
1726 }
1727
1728
1729 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1730 {
1731     return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1732         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1733 }
1734
1735
1736 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1737 {
1738     int const row = row_of_cell(cell);
1739     int const column = column_of_cell(cell);
1740     return  &cell_info[row][column];
1741 }
1742
1743
1744 void LyXTabular::SetMultiColumn(int cell, int number)
1745 {
1746     cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1747     cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1748     cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1749     cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1750     for (number--; number > 0; --number) {
1751         cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1752     }
1753     set_row_column_number_info();
1754 }
1755
1756
1757 int LyXTabular::cells_in_multicolumn(int cell) const
1758 {
1759     int const row = row_of_cell(cell);
1760     int column = column_of_cell(cell);
1761     int result = 1;
1762     ++column;
1763     while ((column < columns_) &&
1764            cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1765     {
1766         ++result;
1767         ++column;
1768     }
1769     return result;
1770 }
1771
1772
1773 int LyXTabular::UnsetMultiColumn(int cell)
1774 {
1775     int const row = row_of_cell(cell);
1776     int column = column_of_cell(cell);
1777     
1778     int result = 0;
1779     
1780     if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1781         cell_info[row][column].multicolumn = CELL_NORMAL;
1782         ++column;
1783         while ((column < columns_) &&
1784                (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1785         {
1786             cell_info[row][column].multicolumn = CELL_NORMAL;
1787             ++column;
1788             ++result;
1789         }
1790     }
1791     set_row_column_number_info();
1792     return result;
1793 }
1794
1795
1796 void LyXTabular::SetLongTabular(bool what)
1797 {
1798     is_long_tabular = what;
1799 }
1800
1801
1802 bool LyXTabular::IsLongTabular() const
1803 {
1804     return is_long_tabular;
1805 }
1806
1807
1808 void LyXTabular::SetRotateTabular(bool flag)
1809 {
1810     rotate = flag;
1811 }
1812
1813
1814 bool LyXTabular::GetRotateTabular() const
1815 {
1816     return rotate;
1817 }
1818
1819
1820 void LyXTabular::SetRotateCell(int cell, bool flag)
1821 {
1822     cellinfo_of_cell(cell)->rotate = flag;
1823 }
1824
1825
1826 bool LyXTabular::GetRotateCell(int cell) const
1827 {
1828     return cellinfo_of_cell(cell)->rotate;
1829 }
1830
1831
1832 bool LyXTabular::NeedRotating() const
1833 {
1834     if (rotate)
1835         return true;
1836     for (int i = 0; i < rows_; ++i) {
1837         for (int j = 0; j < columns_; ++j) {
1838             if (cell_info[i][j].rotate)
1839                 return true;
1840         }
1841     }
1842     return false;
1843 }
1844
1845
1846 bool LyXTabular::IsLastCell(int cell) const
1847 {
1848     if ((cell + 1) < GetNumberOfCells())
1849         return false;
1850     return true;
1851 }
1852
1853
1854 int LyXTabular::GetCellAbove(int cell) const
1855 {
1856     if (row_of_cell(cell) > 0)
1857         return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1858     return cell;
1859 }
1860
1861
1862 int LyXTabular::GetCellBelow(int cell) const
1863 {
1864     if (row_of_cell(cell) + 1 < rows_)
1865         return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1866     return cell;
1867 }
1868
1869
1870 int LyXTabular::GetLastCellAbove(int cell) const
1871 {
1872     if (row_of_cell(cell) <= 0)
1873         return cell;
1874     if (!IsMultiColumn(cell))
1875         return GetCellAbove(cell);
1876     return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1877 }
1878
1879
1880 int LyXTabular::GetLastCellBelow(int cell) const
1881 {
1882     if (row_of_cell(cell) + 1 >= rows_)
1883         return cell;
1884     if (!IsMultiColumn(cell))
1885         return GetCellBelow(cell);
1886     return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1887 }
1888
1889
1890 int LyXTabular::GetCellNumber(int row, int column) const
1891 {
1892     if (column >= columns_)
1893         column = columns_ - 1;
1894     else if (column < 0)
1895         column = 0;
1896     if (row >= rows_)
1897         row = rows_ - 1;
1898     else if (row < 0)
1899         row = 0;
1900     
1901     return cell_info[row][column].cellno;
1902 }
1903
1904
1905 void LyXTabular::SetUsebox(int cell, BoxType type)
1906 {
1907     cellinfo_of_cell(cell)->usebox = type;
1908 }
1909
1910
1911 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1912 {
1913     if (column_info[column_of_cell(cell)].p_width.empty() &&
1914         !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1915         return BOX_NONE;
1916     if (cellinfo_of_cell(cell)->usebox > 1)
1917         return cellinfo_of_cell(cell)->usebox;
1918     return UseParbox(cell);
1919 }
1920
1921
1922 void LyXTabular::SetLTHead(int cell, bool first)
1923 {
1924     int const row = row_of_cell(cell);
1925     int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1926
1927     if (first) {
1928         if (endfirsthead == val)
1929             endfirsthead = 0;
1930         else
1931             endfirsthead = val;
1932     } else {
1933         if (endhead == val)
1934             endhead = 0;
1935         else
1936             endhead = val;
1937     }
1938 }
1939
1940
1941 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1942 {
1943     row = endhead;
1944     if (abs(endhead) > rows_)
1945         return false;
1946     return (row_of_cell(cell) == abs(endhead) - 1);
1947 }
1948
1949
1950 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1951 {
1952     row = endfirsthead;
1953     if (abs(endfirsthead) > rows_)
1954         return false;
1955     return (row_of_cell(cell) == abs(endfirsthead) - 1);
1956 }
1957
1958
1959 void LyXTabular::SetLTFoot(int cell, bool last)
1960 {
1961     int const row = row_of_cell(cell);
1962     int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1963
1964     if (last) {
1965         if (endlastfoot == val)
1966             endlastfoot = 0;
1967         else
1968             endlastfoot = val;
1969     } else {
1970         if (endfoot == val)
1971             endfoot = 0;
1972         else
1973             endfoot = val;
1974     }
1975 }
1976
1977
1978 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1979 {
1980     row = endfoot;
1981     if ((endfoot + 1) > rows_)
1982         return false;
1983     return (row_of_cell(cell) == abs(endfoot) - 1);
1984 }
1985
1986
1987 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1988 {
1989     row = endlastfoot;
1990     if (abs(endlastfoot) > rows_)
1991         return false;
1992     return (row_of_cell(cell) == (abs(endlastfoot)-1));
1993 }
1994
1995
1996 void LyXTabular::SetLTNewPage(int cell, bool what)
1997 {
1998     row_info[row_of_cell(cell)].newpage = what;
1999 }
2000
2001
2002 bool LyXTabular::GetLTNewPage(int cell) const
2003 {
2004     return row_info[row_of_cell(cell)].newpage;
2005 }
2006
2007
2008 bool LyXTabular::SetAscentOfRow(int row, int height)
2009 {
2010     if ((row >= rows_) || (row_info[row].ascent_of_row == height))
2011         return false;
2012     row_info[row].ascent_of_row = height;
2013     return true;
2014 }
2015
2016
2017 bool LyXTabular::SetDescentOfRow(int row, int height)
2018 {
2019     if ((row >= rows_) || (row_info[row].descent_of_row == height))
2020         return false;
2021     row_info[row].descent_of_row = height;
2022     return true;
2023 }
2024
2025
2026 int LyXTabular::GetAscentOfRow(int row) const
2027 {
2028     if (row >= rows_)
2029         return 0;
2030     return row_info[row].ascent_of_row;
2031 }
2032
2033
2034 int LyXTabular::GetDescentOfRow(int row) const
2035 {
2036     if (row >= rows_)
2037         return 0;
2038     return row_info[row].descent_of_row;
2039 }
2040
2041
2042 int LyXTabular::GetHeightOfTabular() const
2043 {
2044     int height = 0;
2045
2046     for (int row = 0; row < rows_; ++row)
2047         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
2048             GetAdditionalHeight(GetCellNumber(row, 0));
2049     return height;
2050 }
2051
2052
2053 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
2054 {
2055     if ((row >= rows_) || (column >= columns_))
2056         return false;
2057     return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
2058 }
2059
2060
2061 int LyXTabular::TeXTopHLine(ostream & os, int row) const
2062 {
2063     if ((row < 0) || (row >= rows_))
2064         return 0;
2065
2066     int const fcell = GetFirstCellInRow(row);
2067     int const n = NumberOfCellsInRow(fcell) + fcell;
2068     int tmp = 0;
2069
2070     for (int i = fcell; i < n; ++i) {
2071         if (TopLine(i))
2072             ++tmp;
2073     }
2074     if (tmp == (n - fcell)){
2075         os << "\\hline ";
2076     } else if (tmp) {
2077         for (int i = fcell; i < n; ++i) {
2078             if (TopLine(i)) {
2079                 os << "\\cline{"
2080                    << column_of_cell(i) + 1
2081                    << '-'
2082                    << right_column_of_cell(i) + 1
2083                    << "} ";
2084             }
2085         }
2086     } else {
2087         return 0;
2088     }
2089     os << "\n";
2090     return 1;
2091 }
2092
2093
2094 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
2095 {
2096     if ((row < 0) || (row >= rows_))
2097         return 0;
2098
2099     int const fcell = GetFirstCellInRow(row);
2100     int const n = NumberOfCellsInRow(fcell) + fcell;
2101     int tmp = 0;
2102
2103     for (int i = fcell; i < n; ++i) {
2104         if (BottomLine(i))
2105             ++tmp;
2106     }
2107     if (tmp == (n-fcell)){
2108         os << "\\hline";
2109     } else if (tmp) {
2110         for (int i = fcell; i < n; ++i) {
2111             if (BottomLine(i)) {
2112                 os << "\\cline{"
2113                    << column_of_cell(i) + 1
2114                    << '-'
2115                    << right_column_of_cell(i) + 1
2116                    << "} ";
2117             }
2118         }
2119     } else {
2120         return 0;
2121     }
2122     os << "\n";
2123     return 1;
2124 }
2125
2126
2127 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2128 {
2129     int ret = 0;
2130
2131     if (GetRotateCell(cell)) {
2132         os << "\\begin{sideways}\n";
2133         ++ret;
2134     }
2135     if (IsMultiColumn(cell)) {
2136         os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2137         if (!cellinfo_of_cell(cell)->align_special.empty()) {
2138             os << cellinfo_of_cell(cell)->align_special << "}{";
2139         } else {
2140             if (LeftLine(cell))
2141                 os << '|';
2142             if (!GetPWidth(cell).empty()) {
2143                 switch (GetVAlignment(cell)) {
2144                 case LYX_VALIGN_TOP:
2145                     os << "p";
2146                     break;
2147                 case LYX_VALIGN_CENTER:
2148                     os << "m";
2149                     break;
2150                 case LYX_VALIGN_BOTTOM:
2151                     os << "b";
2152                     break;
2153                 }
2154                 os << "{" << GetPWidth(cell) << '}';
2155             } else {
2156                 switch (GetAlignment(cell)) {
2157                 case LYX_ALIGN_LEFT:
2158                     os << 'l';
2159                     break;
2160                 case LYX_ALIGN_RIGHT:
2161                     os << 'r';
2162                     break;
2163                 default:
2164                     os << 'c';
2165                     break;
2166                 }
2167             }
2168             if (RightLine(cell))
2169                 os << '|';
2170             if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2171                 LeftLine(cell+1))
2172                 os << '|';
2173             os << "}{";
2174         }
2175     }
2176     if (GetUsebox(cell) == BOX_PARBOX) {
2177         os << "\\parbox[";
2178         switch (GetVAlignment(cell)) {
2179         case LYX_VALIGN_TOP:
2180             os << "t";
2181             break;
2182         case LYX_VALIGN_CENTER:
2183             os << "c";
2184             break;
2185         case LYX_VALIGN_BOTTOM:
2186             os << "b";
2187             break;
2188         }
2189         os << "]{" << GetPWidth(cell) << "}{";
2190     } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2191         os << "\\begin{minipage}[";
2192         switch (GetVAlignment(cell)) {
2193         case LYX_VALIGN_TOP:
2194             os << "t";
2195             break;
2196         case LYX_VALIGN_CENTER:
2197             os << "m";
2198             break;
2199         case LYX_VALIGN_BOTTOM:
2200             os << "b";
2201             break;
2202         }
2203         os << "]{" << GetPWidth(cell) << "}\n";
2204         ++ret;
2205     }
2206     return ret;
2207 }
2208
2209
2210 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2211 {
2212     int ret = 0;
2213
2214     // usual cells
2215     if (GetUsebox(cell) == BOX_PARBOX)
2216         os << "}";
2217     else if (GetUsebox(cell) == BOX_MINIPAGE) {
2218         os << "%\n\\end{minipage}";
2219         ret += 2;
2220     }
2221     if (IsMultiColumn(cell)){
2222         os << '}';
2223     }
2224     if (GetRotateCell(cell)) {
2225         os << "%\n\\end{sideways}";
2226         ++ret;
2227     }
2228     return ret;
2229 }
2230
2231
2232 int LyXTabular::Latex(Buffer const * buf,
2233                       ostream & os, bool fragile, bool fp) const
2234 {
2235     int ret = 0;
2236     int cell = 0;
2237
2238     //+---------------------------------------------------------------------
2239     //+                      first the opening preamble                    +
2240     //+---------------------------------------------------------------------
2241
2242     if (rotate) {
2243         os << "\\begin{sideways}\n";
2244         ++ret;
2245     }
2246     if (is_long_tabular)
2247         os << "\\begin{longtable}{";
2248     else
2249         os << "\\begin{tabular}{";
2250     for (int i = 0; i < columns_; ++i) {
2251         if (column_info[i].left_line)
2252             os << '|';
2253         if (!column_info[i].align_special.empty()) {
2254             os << column_info[i].align_special;
2255         } else if (!column_info[i].p_width.empty()) {
2256             switch (column_info[i].valignment) {
2257             case LYX_VALIGN_TOP:
2258                 os << "p";
2259                 break;
2260             case LYX_VALIGN_CENTER:
2261                 os << "m";
2262                 break;
2263             case LYX_VALIGN_BOTTOM:
2264                 os << "b";
2265                 break;
2266             }
2267             os << "{"
2268                << column_info[i].p_width
2269                << '}';
2270         } else {
2271             switch (column_info[i].alignment) {
2272             case LYX_ALIGN_LEFT:
2273                 os << 'l';
2274                 break;
2275             case LYX_ALIGN_RIGHT:
2276                 os << 'r';
2277                 break;
2278             default:
2279                 os << 'c';
2280                 break;
2281             }
2282         }
2283         if (column_info[i].right_line)
2284             os << '|';
2285     }
2286     os << "}\n";
2287     ++ret;
2288
2289     //+---------------------------------------------------------------------
2290     //+                      the single row and columns (cells)            +
2291     //+---------------------------------------------------------------------
2292
2293     for (int i = 0; i < rows_; ++i) {
2294         ret += TeXTopHLine(os, i);
2295         int bret = ret;
2296         if (IsLongTabular()) {
2297             if ((endhead < 0) && (i == (abs(endhead)-1))) {
2298                 os << "\\endhead\n";
2299                 ++ret;
2300             }
2301             if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2302                 os << "\\endfirsthead\n";
2303                 ++ret;
2304             }
2305             if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2306                 os << "\\endfoot\n";
2307                 ++ret;
2308             }
2309             if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2310                 os << "\\endlastfoot\n";
2311                 ++ret;
2312             }
2313         }
2314         if (ret > bret) {
2315             ret += TeXBottomHLine(os, i-1);
2316             ret += TeXTopHLine(os, i);
2317         }
2318         for (int j = 0; j < columns_; ++j) {
2319             if (IsPartOfMultiColumn(i,j))
2320                 continue;
2321             ret += TeXCellPreamble(os, cell);
2322             InsetText * inset = GetCellInset(cell);
2323
2324             bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2325                     inset->par->Last() > 0 && GetPWidth(cell).empty();
2326             if (rtl)
2327                 os << "\\R{";
2328             ret += inset->Latex(buf, os, fragile, fp);
2329             if (rtl)
2330                 os << "}";
2331
2332             ret += TeXCellPostamble(os, cell);
2333             if (!IsLastCellInRow(cell)) { // not last cell in row
2334                 os << "&\n";
2335                 ++ret;
2336             }
2337             ++cell;
2338         }
2339         os << "\\\\\n";
2340         ret += TeXBottomHLine(os, i);
2341         bret = ret;
2342         if (IsLongTabular()) {
2343             if ((endhead > 0) && (i == (endhead - 1))) {
2344                 os << "\\endhead\n";
2345                 ++ret;
2346             }
2347             if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2348                 os << "\\endfirsthead\n";
2349                 ++ret;
2350             }
2351             if ((endfoot > 0) && (i == (endfoot - 1))) {
2352                 os << "\\endfoot\n";
2353                 ++ret;
2354             }
2355             if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2356                 os << "\\endlastfoot\n";
2357                 ++ret;
2358             }
2359 //          if (ret > bret)
2360 //              ret += TeXBottomHLine(os, i);
2361             if (row_info[i].newpage) {
2362                 os << "\\newpage\n";
2363                 ++ret;
2364             }
2365         }
2366     }
2367
2368     //+---------------------------------------------------------------------
2369     //+                      the closing of the tabular                    +
2370     //+---------------------------------------------------------------------
2371
2372     if (is_long_tabular)
2373         os << "\\end{longtable}";
2374     else
2375         os << "\\end{tabular}";
2376     if (rotate) {
2377         os << "\n\\end{sideways}";
2378         ++ret;
2379     }
2380
2381     return ret;
2382 }
2383
2384
2385 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2386 {
2387     int ret = 0;
2388
2389     //+---------------------------------------------------------------------
2390     //+                      first the opening preamble                    +
2391     //+---------------------------------------------------------------------
2392
2393     os << "<tgroup cols=\"" << columns_
2394        << "\" colsep=\"1\" rowsep=\"1\">\n";
2395     
2396     for (int i = 0; i < columns_; ++i) {
2397         os << "<colspec colname=\"col" << i << "\" align=\"";
2398         switch (column_info[i].alignment) {
2399         case LYX_ALIGN_LEFT:
2400             os << "left";
2401             break;
2402         case LYX_ALIGN_RIGHT:
2403             os << "right";
2404             break;
2405         default:
2406             os << "center";
2407             break;
2408         }
2409         os << "\"/>\n";
2410         ++ret;
2411     }
2412
2413     //+---------------------------------------------------------------------
2414     //+                      the single row and columns (cells)            +
2415     //+---------------------------------------------------------------------
2416
2417     int cell = 0;
2418     os << "<tbody>\n";
2419     for (int i = 0; i < rows_; ++i) {
2420         os << "<row>\n";
2421         for (int j = 0; j < columns_; ++j) {
2422             if (IsPartOfMultiColumn(i, j))
2423                 continue;
2424             
2425             os << "<entry align=\"";
2426             switch (GetAlignment(cell)) {
2427             case LYX_ALIGN_LEFT:
2428                 os << "left";
2429                 break;
2430             case LYX_ALIGN_RIGHT:
2431                 os << "right";
2432                 break;
2433             default:
2434                 os << "center";
2435                 break;
2436             }
2437             
2438             os << "\" valign=\"";
2439             switch (GetVAlignment(cell)) {
2440             case LYX_VALIGN_TOP:
2441                 os << "top";
2442                 break;
2443             case LYX_VALIGN_BOTTOM:
2444                 os << "bottom";
2445                 break;
2446             case LYX_VALIGN_CENTER:
2447                 os << "middle";
2448             }
2449             os << "\"";
2450             
2451             if (IsMultiColumn(cell)) {
2452                 os << " namest=\"col" << j << "\" ";
2453                 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2454             }
2455             
2456             os << ">";
2457             ret += GetCellInset(cell)->DocBook(buf, os);
2458             os << "</entry>";
2459             ++cell;
2460         }
2461         os << "</row>\n";
2462     }
2463     os << "</tbody>\n";
2464     //+---------------------------------------------------------------------
2465     //+                      the closing of the tabular                    +
2466     //+---------------------------------------------------------------------
2467
2468     os << "</tgroup>";
2469     ++ret;
2470
2471     return ret;
2472 }
2473
2474
2475 static
2476 inline
2477 void print_n_chars(ostream & os, unsigned char ch, int n)
2478 {
2479         os << string(n, ch);
2480 }
2481
2482
2483 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2484                               vector<unsigned int> const & clen) const
2485 {
2486     int const fcell = GetFirstCellInRow(row);
2487     int const n = NumberOfCellsInRow(fcell) + fcell;
2488     int tmp = 0;
2489
2490     for (int i = fcell; i < n; ++i) {
2491         if (TopLine(i)) {
2492             ++tmp;
2493             break;
2494         }
2495     }
2496     if (!tmp)
2497         return 0;
2498
2499     unsigned char ch;
2500     for (int i = fcell; i < n; ++i) {
2501         if (TopLine(i)) {
2502             if (LeftLine(i))
2503                 os << "+-";
2504             else
2505                 os << "--";
2506             ch = '-';
2507         } else {
2508             os << "  ";
2509             ch = ' ';
2510         }
2511         int column = column_of_cell(i);
2512         int len = clen[column];
2513         while(IsPartOfMultiColumn(row, ++column))
2514             len += clen[column] + 4;
2515         print_n_chars(os, ch, len);
2516         if (TopLine(i)) {
2517             if (RightLine(i))
2518                 os << "-+";
2519             else
2520                 os << "--";
2521         } else {
2522             os << "  ";
2523         }
2524     }
2525     os << endl;
2526     return 1;
2527 }
2528
2529
2530 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2531                                  vector<unsigned int> const & clen) const
2532 {
2533     int const fcell = GetFirstCellInRow(row);
2534     int const n = NumberOfCellsInRow(fcell) + fcell;
2535     int tmp = 0;
2536
2537     for (int i = fcell; i < n; ++i) {
2538         if (BottomLine(i)) {
2539             ++tmp;
2540             break;
2541         }
2542     }
2543     if (!tmp)
2544         return 0;
2545
2546     unsigned char ch;
2547     for (int i = fcell; i < n; ++i) {
2548         if (BottomLine(i)) {
2549             if (LeftLine(i))
2550                 os << "+-";
2551             else
2552                 os << "--";
2553             ch = '-';
2554         } else {
2555             os << "  ";
2556             ch = ' ';
2557         }
2558         int column = column_of_cell(i);
2559         int len = clen[column];
2560         while(IsPartOfMultiColumn(row, ++column))
2561             len += clen[column] + 4;
2562         print_n_chars(os, ch, len);
2563         if (BottomLine(i)) {
2564             if (RightLine(i))
2565                 os << "-+";
2566             else
2567                 os << "--";
2568         } else {
2569             os << "  ";
2570         }
2571     }
2572     os << endl;
2573     return 1;
2574 }
2575
2576
2577 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2578                                int cell, int row, int column,
2579                                vector<unsigned int> const & clen) const
2580 {
2581     ostringstream sstr;
2582     int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2583
2584     if (LeftLine(cell))
2585         os << "| ";
2586     else
2587         os << "  ";
2588
2589     unsigned int len1 = sstr.str().length();
2590     unsigned int len2 = clen[column];
2591     while(IsPartOfMultiColumn(row, ++column))
2592         len2 += clen[column] + 4;
2593     len2 -= len1;
2594
2595     switch (GetAlignment(cell)) {
2596     default:
2597     case LYX_ALIGN_LEFT:
2598         len1 = 0;
2599         break;
2600     case LYX_ALIGN_RIGHT:
2601         len1 = len2;
2602         len2 = 0;
2603         break;
2604     case LYX_ALIGN_CENTER:
2605         len1 = len2 / 2;
2606         len2 -= len1;
2607         break;
2608     }
2609
2610     for (unsigned int i = 0; i < len1; ++i)
2611         os << " ";
2612     os << sstr.str();
2613     for (unsigned int i = 0; i < len2; ++i)
2614         os << " ";
2615     if (RightLine(cell))
2616         os << " |";
2617     else
2618         os << "  ";
2619
2620     return ret;
2621 }
2622
2623
2624 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2625 {
2626     int ret = 0;
2627
2628     //+---------------------------------------------------------------------
2629     //+           first calculate the width of the single columns          +
2630     //+---------------------------------------------------------------------
2631     vector<unsigned int> clen(columns_);
2632
2633     // first all non (real) multicolumn cells!
2634     for (int j = 0; j < columns_; ++j) {
2635         clen[j] = 0;
2636         for (int i = 0; i < rows_; ++i) {
2637             int cell = GetCellNumber(i, j);
2638             if (IsMultiColumn(cell, true))
2639                 continue;
2640             ostringstream sstr;
2641             GetCellInset(cell)->Ascii(buf, sstr, 0);
2642             if (clen[j] < sstr.str().length())
2643                 clen[j] = sstr.str().length();
2644         }
2645     }
2646     // then all (real) multicolumn cells!
2647     for (int j = 0; j < columns_; ++j) {
2648         for (int i = 0; i < rows_; ++i) {
2649             int cell = GetCellNumber(i, j);
2650             if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2651                 continue;
2652             ostringstream sstr;
2653             GetCellInset(cell)->Ascii(buf, sstr, 0);
2654             int len = int(sstr.str().length());
2655             int const n = cells_in_multicolumn(cell);
2656             for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2657                 len -= clen[k];
2658             if (len > int(clen[j + n - 1]))
2659                 clen[j + n - 1] = len;
2660         }
2661     }
2662     int cell = 0;
2663     for (int i = 0; i < rows_; ++i) {
2664         AsciiTopHLine(os, i, clen);
2665         for (int j = 0; j < columns_; ++j) {
2666             if (IsPartOfMultiColumn(i,j))
2667                 continue;
2668             ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2669             ++cell;
2670         }
2671         os << endl;
2672         AsciiBottomHLine(os, i, clen);
2673     }
2674     return ret;
2675 }
2676
2677
2678 InsetText * LyXTabular::GetCellInset(int cell) const
2679 {
2680     return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2681 }
2682
2683
2684 InsetText * LyXTabular::GetCellInset(int row, int column) const
2685 {
2686     return GetCellInset(GetCellNumber(row, column));
2687 }
2688
2689
2690 void LyXTabular::Validate(LaTeXFeatures & features) const
2691 {
2692     if (IsLongTabular())
2693         features.longtable = true;
2694     if (NeedRotating())
2695         features.rotating = true;
2696     for (int cell = 0; cell < numberofcells; ++cell) {
2697         if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2698             features.array = true;
2699         GetCellInset(cell)->Validate(features);
2700     }
2701 }
2702
2703
2704 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2705 {
2706     LyXParagraph * par = GetCellInset(cell)->par;
2707
2708     for (; par; par = par->next) {
2709         for (int i = 0; i < par->Last(); ++i) {
2710             if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2711                 return BOX_PARBOX;
2712         }
2713     }
2714     return BOX_NONE;
2715 }