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