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