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