]> git.lyx.org Git - lyx.git/blob - src/tabular.C
79e65ba99df6dba418722163686782c994f0c911
[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     linebreaks = 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;
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 > 0); ++i) {
548         cell_info[row][i].width_of_cell = column_info[i].width_of_column;
549         width -= column_info[i].width_of_column;
550     }
551     if (i == column2) {
552         cell_info[row][i].width_of_cell = width;
553     }
554     return true;
555 }
556
557
558 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
559 {
560     int row = row_of_cell(cell);
561     int column1 = column_of_cell(cell);
562     int column2 = right_column_of_cell(cell);
563
564     // first set columns to 0 so we can calculate the right width
565     int i = column1;
566     for (; i <= column2; ++i)
567         cell_info[row][i].width_of_cell = 0;
568     for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
569         ;
570     if (i < numberofcells)
571         recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
572     SetWidthOfMulticolCell(cell, new_width);
573 }
574
575
576 /* returns 1 if a complete update is necessary, otherwise 0 */ 
577 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
578 {
579     int row = row_of_cell(cell);
580     int column1 = column_of_cell(cell);
581     int tmp = 0;
582     int width = 0;
583
584     if (IsMultiColumn(cell)) {
585         tmp = SetWidthOfMulticolCell(cell, new_width);
586     } else {
587         width = (new_width + 2*WIDTH_OF_LINE);
588         cell_info[row][column1].width_of_cell = width;
589         if (column_info[column1].right_line && (column1 < columns_-1) &&
590             column_info[column1+1].left_line) // additional width
591             cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
592         tmp = calculate_width_of_column_NMC(column1);
593     }
594     if (tmp) {
595         int i;
596         for(i = 0; i<columns_;++i)
597             calculate_width_of_column_NMC(i);
598         for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
599             ;
600         if (i<numberofcells)
601             recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
602         for(i = 0; i<columns_;++i)
603             calculate_width_of_column(i);
604         calculate_width_of_tabular();
605         return true;
606     }
607     return false;
608 }
609
610
611 bool LyXTabular::SetAlignment(int cell, char align, bool onlycolumn)
612 {
613     if (!IsMultiColumn(cell) || onlycolumn)
614         column_info[column_of_cell(cell)].alignment = align;
615     if (!onlycolumn)
616         cellinfo_of_cell(cell)->alignment = align;
617     return true;
618 }
619
620
621 bool LyXTabular::SetVAlignment(int cell, char align, bool onlycolumn)
622 {
623     if (!IsMultiColumn(cell) || onlycolumn)
624         column_info[column_of_cell(cell)].valignment = align;
625     if (!onlycolumn)
626         cellinfo_of_cell(cell)->valignment = align;
627     return true;
628 }
629
630
631 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
632 {
633     bool flag = !width.empty();
634
635     int j = column_of_cell(cell);
636     int c;
637     column_info[j].p_width = width;
638     if (flag) // do this only if there is a width
639         SetAlignment(cell, LYX_ALIGN_LEFT);
640     for(int i=0; i < rows_; ++i) {
641         c = GetCellNumber(i, j);
642         flag = !GetPWidth(c).empty(); // because of multicolumns!
643         GetCellInset(c)->SetAutoBreakRows(flag);
644     }
645     return true;
646 }
647
648
649 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
650 {
651     bool flag = !width.empty();
652
653     cellinfo_of_cell(cell)->p_width = width;
654     if (IsMultiColumn(cell)) {
655         GetCellInset(cell)->SetAutoBreakRows(flag);
656         return true;
657     }
658     return false;
659 }
660
661
662 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
663 {
664     if (what == SET_SPECIAL_MULTI)
665         cellinfo_of_cell(cell)->align_special = special;
666     else
667         column_info[column_of_cell(cell)].align_special = special;
668     return true;
669 }
670
671
672 bool LyXTabular::SetAllLines(int cell, bool line)
673 {
674     SetTopLine(cell, line);
675     SetBottomLine(cell, line);
676     SetRightLine(cell, line);
677     SetLeftLine(cell, line);
678     return true;
679 }
680
681
682 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
683 {
684     int row = row_of_cell(cell);
685
686     if (onlycolumn || !IsMultiColumn(cell))
687         row_info[row].top_line = line;
688     else
689         cellinfo_of_cell(cell)->top_line = line;
690     return true;
691 }
692
693
694 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
695 {
696     if (onlycolumn || !IsMultiColumn(cell))
697         row_info[row_of_cell(cell)].bottom_line = line;
698     else
699         cellinfo_of_cell(cell)->bottom_line = line;
700     return true;
701 }
702
703
704 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
705 {
706     if (onlycolumn || !IsMultiColumn(cell))
707         column_info[column_of_cell(cell)].left_line = line;
708     else
709         cellinfo_of_cell(cell)->left_line = line;
710     return true;
711 }
712
713
714 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
715 {
716     if (onlycolumn || !IsMultiColumn(cell))
717         column_info[right_column_of_cell(cell)].right_line = line;
718     else
719         cellinfo_of_cell(cell)->right_line = line;
720     return true;
721 }
722
723
724 char LyXTabular::GetAlignment(int cell, bool onlycolumn) const
725 {
726     if (!onlycolumn && IsMultiColumn(cell))
727         return cellinfo_of_cell(cell)->alignment;
728     else
729         return column_info[column_of_cell(cell)].alignment;
730 }
731
732
733 char LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
734 {
735     if (!onlycolumn && IsMultiColumn(cell))
736         return cellinfo_of_cell(cell)->valignment;
737     else
738         return column_info[column_of_cell(cell)].valignment;
739 }
740
741
742 string LyXTabular::GetPWidth(int cell) const
743 {
744     if (IsMultiColumn(cell))
745         return cellinfo_of_cell(cell)->p_width;
746     return column_info[column_of_cell(cell)].p_width;
747 }
748
749
750 string LyXTabular::GetColumnPWidth(int cell) const
751 {
752     return column_info[column_of_cell(cell)].p_width;
753 }
754
755
756 string LyXTabular::GetMColumnPWidth(int cell) const
757 {
758     if (IsMultiColumn(cell))
759         return cellinfo_of_cell(cell)->p_width;
760     return string();
761 }
762
763
764 string LyXTabular::GetAlignSpecial(int cell, int what) const
765 {
766     if (what == SET_SPECIAL_MULTI)
767         return cellinfo_of_cell(cell)->align_special;
768     return column_info[column_of_cell(cell)].align_special;
769 }
770
771
772 int LyXTabular::GetWidthOfCell(int cell) const
773 {
774     int row = row_of_cell(cell);
775     int column1 = column_of_cell(cell);
776     int column2 = right_column_of_cell(cell);
777     int result = 0;
778     int i = column1;
779     for (; i <= column2; ++i) {
780         result += cell_info[row][i].width_of_cell;
781     }
782     
783 //    result += GetAdditionalWidth(cell);
784     
785     return result;
786 }
787
788 int LyXTabular::GetBeginningOfTextInCell(int cell) const
789 {
790     int x = 0;
791    
792     switch (GetAlignment(cell)){
793     case LYX_ALIGN_CENTER:
794         x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
795         break;
796     case LYX_ALIGN_RIGHT:
797         x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
798         // + GetAdditionalWidth(cell);
799         break;
800     default: /* LYX_ALIGN_LEFT: nothing :-) */ 
801         break;
802     }
803     
804     // the LaTeX Way :-(
805     x += WIDTH_OF_LINE;
806     return x;
807 }
808
809
810 bool LyXTabular::IsFirstCellInRow(int cell) const
811 {
812     return (column_of_cell(cell) == 0);
813 }
814
815
816 int LyXTabular::GetFirstCellInRow(int row) const
817 {
818     if (row > (rows_-1))
819         row = rows_ - 1;
820     return cell_info[row][0].cellno;
821 }
822
823 bool LyXTabular::IsLastCellInRow(int cell) const
824 {
825     return (right_column_of_cell(cell) == (columns_ - 1));
826 }
827
828
829 int LyXTabular::GetLastCellInRow(int row) const
830 {
831     if (row > (rows_-1))
832         row = rows_ - 1;
833     return cell_info[row][columns_-1].cellno;
834 }
835
836
837 bool LyXTabular::calculate_width_of_column(int column)
838 {
839     int old_column_width = column_info[column].width_of_column;
840     int maximum = 0;
841     
842     for (int i = 0; i < rows_; ++i) {
843         maximum = max(cell_info[i][column].width_of_cell, maximum);
844     }
845     column_info[column].width_of_column = maximum;
846     return (column_info[column].width_of_column != old_column_width);
847 }
848
849
850 bool LyXTabular::calculate_width_of_column_NMC(int column)
851 {
852     int old_column_width = column_info[column].width_of_column;
853     int max = 0;
854     for (int i = 0; i < rows_; ++i) {
855         if (!IsMultiColumn(GetCellNumber(i, column)) &&
856             (cell_info[i][column].width_of_cell > max)) {
857             max = cell_info[i][column].width_of_cell;
858         }
859     }
860     column_info[column].width_of_column = max;
861     return (column_info[column].width_of_column != old_column_width);
862 }
863
864
865 void LyXTabular::calculate_width_of_tabular()
866 {
867     width_of_tabular = 0;
868     for (int i = 0; i < columns_; ++i) {
869         width_of_tabular += column_info[i].width_of_column;
870     }
871 }
872
873
874 int LyXTabular::row_of_cell(int cell) const
875 {
876     if (cell >= numberofcells)
877         return rows_-1;
878     else if (cell < 0)
879         return 0;
880     return rowofcell[cell];
881 }
882
883
884 int LyXTabular::column_of_cell(int cell) const
885 {
886     if (cell >= numberofcells)
887         return columns_-1;
888     else if (cell < 0)
889         return 0;
890     return columnofcell[cell];
891 }
892
893
894 int LyXTabular::right_column_of_cell(int cell) const
895 {
896     int row = row_of_cell(cell);
897     int column = column_of_cell(cell);
898     while (column < (columns_ - 1) &&
899            cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
900         ++column;
901     return column;
902 }
903
904
905 void LyXTabular::Write(Buffer const * buf, ostream & os) const
906 {
907     int i, j;
908
909     // header line
910     os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
911         ">" << endl;
912     // global longtable options
913     os << "<Features rotate=" << rotate <<
914         " islongtable=" << is_long_tabular <<
915         " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
916         " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
917         ">" << endl << endl;
918     for (i = 0; i < rows_; ++i) {
919         os << "<Row topline=" << row_info[i].top_line <<
920             " bottomline=" << row_info[i].bottom_line <<
921             " newpage=" << row_info[i].newpage <<
922             ">" << endl;
923         for (j = 0; j < columns_; ++j) {
924             if (!i) {
925                 os << "<Column alignment=" << column_info[j].alignment <<
926                     " valignment=" << column_info[j].valignment <<
927                     " leftline=" << column_info[j].left_line <<
928                     " rightline=" << column_info[j].right_line <<
929                     " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
930                     "\" special=\"" << column_info[j].align_special <<
931                     "\">" << endl;
932             } else {
933                 os << "<Column>" << endl;
934             }
935             os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
936                 " alignment=" << cell_info[i][j].alignment <<
937                 " valignment=" << cell_info[i][j].valignment <<
938                 " topline=" << cell_info[i][j].top_line <<
939                 " bottomline=" << cell_info[i][j].bottom_line <<
940                 " leftline=" << cell_info[i][j].left_line <<
941                 " rightline=" << cell_info[i][j].right_line <<
942                 " rotate=" << cell_info[i][j].rotate <<
943                 " linebreaks=" << cell_info[i][j].linebreaks <<
944                 " width=\"" << cell_info[i][j].p_width <<
945                 "\" special=\"" << cell_info[i][j].align_special <<
946                 "\">" << endl;
947             os << "\\begin_inset ";
948             cell_info[i][j].inset.Write(buf, os);
949             os << "\n\\end_inset " << endl;
950             os << "</Cell>" << endl;
951             os << "</Column>" << endl;
952         }
953         os << "</Row>" << endl;
954     }
955     os << "</LyXTabular>" << endl;
956 }
957
958
959 static
960 bool getTokenValue(string const str, const char * token, string & ret)
961 {
962     int pos = str.find(token);
963     char ch = str[pos+strlen(token)];
964
965     if ((pos < 0) || (ch != '='))
966         return false;
967     ret.erase();
968     pos += strlen(token)+1;
969     ch = str[pos];
970     if ((ch != '"') && (ch != '\'')) { // only read till next space
971         ret += ch;
972         ch = ' ';
973     }
974     while((pos < int(str.length()-1)) && (str[++pos] != ch))
975         ret += str[pos];
976
977     return true;
978 }
979
980
981 static
982 bool getTokenValue(string const str, const char * token, int & num)
983 {
984     string ret;
985     int pos = str.find(token);
986     char ch = str[pos+strlen(token)];
987
988     if ((pos < 0) || (ch != '='))
989         return false;
990     ret.erase();
991     pos += strlen(token)+1;
992     ch = str[pos];
993     if ((ch != '"') && (ch != '\'')) { // only read till next space
994         if (!isdigit(ch))
995             return false;
996         ret += ch;
997     }
998     ++pos;
999     while((pos < int(str.length()-1)) && isdigit(str[pos]))
1000         ret += str[pos++];
1001
1002     num = strToInt(ret);
1003     return true;
1004 }
1005
1006
1007 static
1008 bool getTokenValue(string const str, const char * token, bool & flag)
1009 {
1010     string ret;
1011     int pos = str.find(token);
1012     char ch = str[pos+strlen(token)];
1013
1014     if ((pos < 0) || (ch != '='))
1015         return false;
1016     ret.erase();
1017     pos += strlen(token)+1;
1018     ch = str[pos];
1019     if ((ch != '"') && (ch != '\'')) { // only read till next space
1020         if (!isdigit(ch))
1021             return false;
1022         ret += ch;
1023     }
1024     ++pos;
1025     while((pos < int(str.length()-1)) && isdigit(str[pos]))
1026         ret += str[pos++];
1027
1028     flag = strToInt(ret);
1029     return true;
1030 }
1031
1032
1033 void l_getline(istream & is, string & str)
1034 {
1035     getline(is, str);
1036     while(str.empty())
1037         getline(is, str);
1038 }
1039
1040
1041 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1042 {
1043     string line;
1044     istream & is = lex.getStream();
1045
1046     l_getline(is, line);
1047     if (!prefixIs(line, "<LyXTabular ")) {
1048         OldFormatRead(lex, line);
1049         return;
1050     }
1051
1052     int version;
1053     int rows_arg;
1054     int columns_arg;
1055     if (!getTokenValue(line, "version", version))
1056         return;
1057     if (!getTokenValue(line, "rows", rows_arg))
1058         return;
1059     if (!getTokenValue(line, "columns", columns_arg))
1060         return;
1061     Init(rows_arg, columns_arg);
1062     l_getline(is, line);
1063     if (!prefixIs(line, "<Features ")) {
1064         lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1065             line << ")" << endl;
1066         return;
1067     }
1068     (void)getTokenValue(line, "islongtable", is_long_tabular);
1069     (void)getTokenValue(line, "endhead", endhead);
1070     (void)getTokenValue(line, "endfirsthead", endfirsthead);
1071     (void)getTokenValue(line, "endfoot", endfoot);
1072     (void)getTokenValue(line, "endlastfoot", endlastfoot);
1073     int i, j;
1074     for(i = 0; i < rows_; ++i) {
1075         l_getline(is, line);
1076         if (!prefixIs(line, "<Row ")) {
1077             lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1078                 line << ")" << endl;
1079             return;
1080         }
1081         (void)getTokenValue(line, "topline", row_info[i].top_line);
1082         (void)getTokenValue(line, "bottomline", row_info[i].bottom_line);
1083         (void)getTokenValue(line, "newpage", row_info[i].newpage);
1084         for (j = 0; j < columns_; ++j) {
1085             l_getline(is,line);
1086             if (!prefixIs(line,"<Column")) {
1087                 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1088                     line << ")" << endl;
1089                 return;
1090             }
1091             if (!i) {
1092                 (void)getTokenValue(line, "alignment", column_info[j].alignment);
1093                 (void)getTokenValue(line, "valignment", column_info[j].valignment);
1094                 (void)getTokenValue(line, "leftline", column_info[j].left_line);
1095                 (void)getTokenValue(line, "rightline", column_info[j].right_line);
1096                 (void)getTokenValue(line, "width", column_info[j].p_width);
1097                 (void)getTokenValue(line, "special", column_info[j].align_special);
1098             }
1099             l_getline(is, line);
1100             if (!prefixIs(line, "<Cell")) {
1101                 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1102                     line << ")" << endl;
1103                 return;
1104             }
1105             (void)getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1106             (void)getTokenValue(line, "alignment", cell_info[i][j].alignment);
1107             (void)getTokenValue(line, "valignment", cell_info[i][j].valignment);
1108             (void)getTokenValue(line, "topline", cell_info[i][j].top_line);
1109             (void)getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1110             (void)getTokenValue(line, "leftline", cell_info[i][j].left_line);
1111             (void)getTokenValue(line, "rightline", cell_info[i][j].right_line);
1112             (void)getTokenValue(line, "rotate", cell_info[i][j].rotate);
1113             (void)getTokenValue(line, "linebreaks", cell_info[i][j].linebreaks);
1114             (void)getTokenValue(line, "width", cell_info[i][j].p_width);
1115             (void)getTokenValue(line, "special", cell_info[i][j].align_special);
1116             l_getline(is, line);
1117             if (prefixIs(line, "\\begin_inset")) {
1118                 cell_info[i][j].inset.Read(buf, lex);
1119                 l_getline(is, line);
1120             }
1121             if (line != "</Cell>") {
1122                 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1123                     line << ")" << endl;
1124                 return;
1125             }
1126             l_getline(is, line);
1127             if (line != "</Column>") {
1128                 lyxerr << "Wrong tabular format (expected </Column> got" <<
1129                     line << ")" << endl;
1130                 return;
1131             }
1132         }
1133         l_getline(is, line);
1134         if (line != "</Row>") {
1135             lyxerr << "Wrong tabular format (expected </Row> got" <<
1136                 line << ")" << endl;
1137             return;
1138         }
1139     }
1140     while (line != "</LyXTabular>") {
1141         l_getline(is, line);
1142     }
1143     set_row_column_number_info();
1144 }
1145
1146
1147 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1148 {
1149     int version;
1150     int i, j;
1151     int rows_arg = 0;
1152     int columns_arg = 0;
1153     int is_long_tabular_arg = false;
1154     int rotate_arg = false;
1155     int a = -1;
1156     int b = -1;
1157     int c = -1;
1158     int d = -1;
1159     int e = 0;
1160     int f = 0;
1161     int g = 0;
1162     int h = 0;
1163         
1164     istream & is = lex.getStream();
1165     string s(fl);
1166     if (s.length() > 8)
1167         version = atoi(s.c_str() + 8);
1168     else
1169         version = 1;
1170
1171     vector<int> cont_row_info;
1172
1173     if (version < 5) {
1174         lyxerr << "Tabular format < 5 is not supported anymore\n"
1175             "Get an older version of LyX (< 1.1.x) for conversion!"
1176                << endl;
1177         WriteAlert(_("Warning:"),
1178                    _("Tabular format < 5 is not supported anymore\n"),
1179                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1180         if (version > 2) {
1181             is >> rows_arg >> columns_arg >> is_long_tabular_arg
1182                >> rotate_arg >> a >> b >> c >> d;
1183         } else
1184             is >> rows_arg >> columns_arg;
1185         Init(rows_arg, columns_arg);
1186         cont_row_info = vector<int>(rows_arg);
1187         SetLongTabular(is_long_tabular_arg);
1188         SetRotateTabular(rotate_arg);
1189         string tmp;
1190         for (i = 0; i < rows_; ++i) {
1191             getline(is, tmp);
1192             cont_row_info[i] = false;
1193         }
1194         for (i = 0; i < columns_; ++i) {
1195             getline(is, tmp);
1196         }
1197         for (i = 0; i < rows_; ++i) {
1198             for (j = 0; j < columns_; ++j) {
1199                 getline(is, tmp);
1200             }
1201         }
1202     } else {
1203         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1204            >> rotate_arg >> a >> b >> c >> d;
1205         Init(rows_arg, columns_arg);
1206         cont_row_info = vector<int>(rows_arg);
1207         SetLongTabular(is_long_tabular_arg);
1208         SetRotateTabular(rotate_arg);
1209         endhead = a;
1210         endfirsthead = b;
1211         endfoot = c;
1212         endlastfoot = d;
1213         for (i = 0; i < rows_; ++i) {
1214             a = b = c = d = e = f = g = h = 0;
1215             is >> a >> b >> c >> d;
1216             row_info[i].top_line = a;
1217             row_info[i].bottom_line = b;
1218             cont_row_info[i] = c;
1219             row_info[i].newpage = d;
1220         }
1221         for (i = 0; i < columns_; ++i) {
1222             string s1;
1223             string s2;
1224             is >> a >> b >> c;
1225             char ch; // skip '"'
1226             is >> ch;
1227             getline(is, s1, '"');
1228             is >> ch; // skip '"'
1229             getline(is, s2, '"');
1230             column_info[i].alignment = static_cast<char>(a);
1231             column_info[i].left_line = b;
1232             column_info[i].right_line = c;
1233             column_info[i].p_width = s1;
1234             column_info[i].align_special = s2;
1235         }
1236         for (i = 0; i < rows_; ++i) {
1237             for (j = 0; j < columns_; ++j) {
1238                 string s1;
1239                 string s2;
1240                 is >> a >> b >> c >> d >> e >> f >> g;
1241                 char ch;
1242                 is >> ch; // skip '"'
1243                 getline(is, s1, '"');
1244                 is >> ch; // skip '"'
1245                 getline(is, s2, '"');
1246                 cell_info[i][j].multicolumn = static_cast<char>(a);
1247                 cell_info[i][j].alignment = static_cast<char>(b);
1248                 cell_info[i][j].top_line = static_cast<char>(c);
1249                 cell_info[i][j].bottom_line = static_cast<char>(d);
1250                 cell_info[i][j].rotate = static_cast<bool>(f);
1251                 cell_info[i][j].linebreaks = static_cast<bool>(g);
1252                 cell_info[i][j].align_special = s1;
1253                 cell_info[i][j].p_width = s2;
1254             }
1255         }
1256     }
1257     set_row_column_number_info();
1258
1259     LyXParagraph * par = new LyXParagraph;
1260     LyXParagraph * return_par = 0;
1261     LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1262     LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1263     string token, tmptok;
1264     int pos = 0;
1265     char depth = 0;
1266     LyXFont font(LyXFont::ALL_SANE);
1267
1268     while (lex.IsOK()) {
1269         lex.nextToken();
1270         token = lex.GetString();
1271         if (token.empty())
1272             continue;
1273         if ((token == "\\layout") || (token == "\\end_float") ||
1274             (token == "\\end_deeper"))
1275         {
1276             lex.pushToken(token);
1277             break;
1278         }
1279         if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1280                                                               return_par,
1281                                                               token, pos,
1282                                                               depth, font,
1283                                                               footnoteflag,
1284                                                               footnotekind))
1285         {
1286             // the_end read
1287             lex.pushToken(token);
1288             break;
1289         }
1290         if (return_par) {
1291             lex.printError("New Paragraph allocated! This should not happen!");
1292             lex.pushToken(token);
1293             delete par;
1294             par = return_par;
1295             break;
1296         }
1297     }
1298     // now we have the par we should fill the insets with this!
1299     int cell = 0;
1300     InsetText * inset = GetCellInset(cell);
1301     int row;
1302
1303     for(int i = 0; i < par->Last(); ++i) {
1304         if (par->IsNewline(i)) {
1305             ++cell;
1306             if (cell > GetNumberOfCells()) {
1307                 lyxerr << "Some error in reading old table format occured!" <<
1308                     endl << "Terminating when reading cell[" << cell << "]!" <<
1309                     endl;
1310                 return;
1311             }
1312             row = row_of_cell(cell);
1313             if (cont_row_info[row]) {
1314                 DeleteRow(row);
1315                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1316                 while(!IsFirstCellInRow(--cell));
1317             } else {
1318                 inset = GetCellInset(cell);
1319                 continue;
1320             }
1321             inset = GetCellInset(cell);
1322             row = row_of_cell(cell);
1323             if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].linebreaks)
1324             {
1325                 // insert a space instead
1326                 par->Erase(i);
1327                 par->InsertChar(i, ' ');
1328             }
1329         }
1330         par->CopyIntoMinibuffer(current_view->buffer()->params, i);
1331         inset->par->InsertFromMinibuffer(inset->par->Last());
1332     }
1333     Reinit();
1334 }
1335
1336
1337 char const * LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1338 {
1339         int i = isColumn ? cell : column_of_cell(cell);
1340         
1341         //if (isColumn)
1342         //i = cell;
1343         //else
1344         //i = column_of_cell(cell);
1345     if (!isColumn && IsMultiColumn(cell)) {
1346        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1347            return cellinfo_of_cell(cell)->align_special.c_str();
1348        } else {
1349            switch (GetAlignment(cell)) {
1350            case LYX_ALIGN_LEFT:
1351                return "left";
1352            case LYX_ALIGN_RIGHT:
1353                return "right";
1354            default:
1355                return "center";
1356            }
1357        }
1358     } else {
1359        if (!column_info[i].align_special.empty()) {
1360            return column_info[i].align_special.c_str();
1361        }
1362 #ifdef IGNORE_THIS_FOR_NOW
1363        else if (!column_info[i].p_width.empty()) {
1364            file += "p{";
1365            file += column_info[i].p_width;
1366            file += '}';
1367        }
1368 #endif
1369        else {
1370            switch (column_info[i].alignment) {
1371            case LYX_ALIGN_LEFT:
1372                return "left";
1373            case LYX_ALIGN_RIGHT:
1374                return "right";
1375            default:
1376                return "center";
1377            }
1378        }
1379     }
1380 }
1381
1382
1383 // cell <0 will tex the preamble
1384 // returns the number of printed newlines
1385 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1386 {
1387     int i;
1388     int ret = 0;
1389     //int tmp; // tmp2; // unused
1390     int nvcell; // fcell; // unused
1391     if (IsLastCell(cell)) {
1392             os << newlineAndDepth(--depth)
1393                << "</ENTRY>"
1394                << newlineAndDepth(--depth)
1395                << "</ROW>"
1396                << newlineAndDepth(--depth)
1397                << "</TBODY>"
1398                << newlineAndDepth(--depth);
1399         if (is_long_tabular)
1400                 os << "</TGROUP>";
1401         else
1402                 os << "</TGROUP>"
1403                    << newlineAndDepth(--depth);
1404         ret += 4;
1405     } else {
1406         nvcell = cell + 1;
1407         if (cell < 0) {
1408             // preamble
1409             if (is_long_tabular)
1410                     os << "<TGROUP ";
1411             else
1412                     os << "<TGROUP ";
1413             os << "COLS='"
1414                << columns_
1415                << "' COLSEP='1' ROWSEP='1'>"
1416                << newlineAndDepth(++depth);
1417             ++ret;
1418             for (i = 0; i < columns_; ++i) {
1419                     os << "<COLSPEC ALIGN='"
1420                        << GetDocBookAlign(i, true)
1421                        << "' COLNAME='col"
1422                        << i + 1
1423                        << "' COLNUM='"
1424                        << i + 1
1425                        << "' COLSEP='";
1426                if (i == (columns_-1)) {
1427                        os << '1';
1428                } else {
1429                    if (column_info[i].right_line ||
1430                        column_info[i+1].left_line)
1431                            os << '1';
1432                    else
1433                            os << '0';
1434                }
1435                os << "'>"
1436                   << newlineAndDepth(depth);
1437                 ++ret;
1438 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1439                 if (column_info[i].left_line)
1440                         os << '|';
1441 #endif
1442             }
1443             os << "<TBODY>"
1444                << newlineAndDepth(++depth)
1445                << "<ROW>"
1446                << newlineAndDepth(++depth)
1447                << "<ENTRY ALIGN='"
1448                << GetDocBookAlign(0)
1449                << "'";
1450            if (IsMultiColumn(0)) {
1451                    os << " NAMEST='col1' NAMEEND='col"
1452                       << cells_in_multicolumn(0)
1453                       << "'";
1454            }
1455            os << ">"
1456               << newlineAndDepth(++depth);
1457             ret += 3;
1458         } else {
1459             if (IsLastCellInRow(cell)) {
1460                     os << newlineAndDepth(--depth)
1461                        << "</ENTRY>"
1462                        << newlineAndDepth(--depth)
1463                        << "</ROW>"
1464                        << newlineAndDepth(depth)
1465                        << "<ROW>"
1466                        << newlineAndDepth(++depth)
1467                        << "<ENTRY ALIGN='"
1468                        << GetDocBookAlign(cell + 1)
1469                        << "' VALIGN='middle'";
1470                if (IsMultiColumn(cell + 1)) {
1471                        os << " NAMEST='col"
1472                           << column_of_cell(cell+1) + 1
1473                           << "' NAMEEND='col"
1474                           << column_of_cell(cell + 1) +
1475                                cells_in_multicolumn(cell + 1)
1476                           << "'";
1477                }
1478                os << ">"
1479                   << newlineAndDepth(++depth);
1480                 ret += 4;
1481             } else {
1482                     os << newlineAndDepth(--depth)
1483                        << "</ENTRY>"
1484                        << newlineAndDepth(depth)
1485                        << "<ENTRY ALIGN='"
1486                        << GetDocBookAlign(cell + 1)
1487                        << "' VALIGN='middle'";
1488                if (IsMultiColumn(cell + 1)) {
1489                        os << " NAMEST='col"
1490                           << column_of_cell(cell+1) + 1
1491                           << "' NAMEEND='col"
1492                           << column_of_cell(cell+1) +
1493                                cells_in_multicolumn(cell+1)
1494                           << "'";
1495                }
1496                os << ">"
1497                   << newlineAndDepth(++depth);
1498                 ret += 3;
1499             }
1500         }
1501     }
1502     return ret;
1503 }
1504
1505
1506 bool LyXTabular::IsMultiColumn(int cell) const
1507 {
1508     return (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1509 }
1510
1511
1512 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1513 {
1514     int row = row_of_cell(cell);
1515     int column = column_of_cell(cell);
1516     return  &cell_info[row][column];
1517 }
1518    
1519
1520 void LyXTabular::SetMultiColumn(int cell, int number)
1521 {
1522     cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1523     cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1524     cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1525     cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1526     for (number--; number > 0; --number) {
1527         cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1528     }
1529     set_row_column_number_info();
1530 }
1531
1532
1533 int LyXTabular::cells_in_multicolumn(int cell) const
1534 {
1535     int row = row_of_cell(cell);
1536     int column = column_of_cell(cell);
1537     int result = 1;
1538     ++column;
1539     while ((column < columns_) &&
1540            cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1541     {
1542         ++result;
1543         ++column;
1544     }
1545     return result;
1546 }
1547
1548
1549 int LyXTabular::UnsetMultiColumn(int cell)
1550 {
1551     int row = row_of_cell(cell);
1552     int column = column_of_cell(cell);
1553     
1554     int result = 0;
1555     
1556     if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1557         cell_info[row][column].multicolumn = CELL_NORMAL;
1558         ++column;
1559         while ((column < columns_) &&
1560                (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1561         {
1562             cell_info[row][column].multicolumn = CELL_NORMAL;
1563             ++column;
1564             ++result;
1565         }
1566     }
1567     set_row_column_number_info();
1568     return result;
1569 }
1570
1571
1572 void LyXTabular::SetLongTabular(int what)
1573 {
1574     is_long_tabular = what;
1575 }
1576
1577
1578 bool LyXTabular::IsLongTabular() const
1579 {
1580     return is_long_tabular;
1581 }
1582
1583
1584 void LyXTabular::SetRotateTabular(int what)
1585 {
1586     rotate = what;
1587 }
1588
1589
1590 bool LyXTabular::GetRotateTabular() const
1591 {
1592     return rotate;
1593 }
1594
1595
1596 void LyXTabular::SetRotateCell(int cell, int what)
1597 {
1598     cellinfo_of_cell(cell)->rotate = what;
1599 }
1600
1601
1602 bool LyXTabular::GetRotateCell(int cell) const
1603 {
1604     return cellinfo_of_cell(cell)->rotate;
1605 }
1606
1607
1608 bool LyXTabular::NeedRotating() const
1609 {
1610     if (rotate)
1611         return true;
1612     for (int i = 0; i < rows_; ++i) {
1613         for (int j = 0; j < columns_; ++j) {
1614             if (cell_info[i][j].rotate)
1615                 return true;
1616         }
1617     }
1618     return false;
1619 }
1620
1621
1622 bool LyXTabular::IsLastCell(int cell) const
1623 {
1624     if ((cell+1) < GetNumberOfCells())
1625         return false;
1626     return true;
1627 }
1628
1629
1630 int LyXTabular::GetCellAbove(int cell) const
1631 {
1632     if (row_of_cell(cell) > 0)
1633         return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1634     return cell;
1635 }
1636
1637
1638 int LyXTabular::GetCellBelow(int cell) const
1639 {
1640     if (row_of_cell(cell)+1 < rows_)
1641         return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1642     return cell;
1643 }
1644
1645
1646 int LyXTabular::GetLastCellAbove(int cell) const
1647 {
1648     if (row_of_cell(cell) <= 0)
1649         return cell;
1650     if (!IsMultiColumn(cell))
1651         return GetCellAbove(cell);
1652     return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
1653 }
1654
1655
1656 int LyXTabular::GetLastCellBelow(int cell) const
1657 {
1658     if (row_of_cell(cell)+1 >= rows_)
1659         return cell;
1660     if (!IsMultiColumn(cell))
1661         return GetCellBelow(cell);
1662     return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
1663 }
1664
1665
1666 int LyXTabular::GetCellNumber(int row, int column) const
1667 {
1668     if (column >= columns_)
1669         column = columns_ - 1;
1670     else if (column < 0)
1671         column = 0;
1672     if (row >= rows_)
1673         row = rows_ - 1;
1674     else if (row < 0)
1675         row = 0;
1676     
1677     return cell_info[row][column].cellno;
1678 }
1679
1680
1681 void LyXTabular::SetLinebreaks(int cell, bool what)
1682 {
1683     cellinfo_of_cell(cell)->linebreaks = what;
1684 }
1685
1686
1687 bool LyXTabular::GetLinebreaks(int cell) const
1688 {
1689     if (column_info[column_of_cell(cell)].p_width.empty() &&
1690         !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1691         return false;
1692     return cellinfo_of_cell(cell)->linebreaks;
1693 }
1694
1695
1696 void LyXTabular::SetLTHead(int cell, bool first)
1697 {
1698     int row = row_of_cell(cell);
1699     int val = (row+1) * (column_of_cell(cell)? 1:-1);
1700
1701     if (first) {
1702         if (endfirsthead == val)
1703             endfirsthead = 0;
1704         else
1705             endfirsthead = val;
1706     } else {
1707         if (endhead == val)
1708             endhead = 0;
1709         else
1710             endhead = val;
1711     }
1712 }
1713
1714
1715 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1716 {
1717     row = endhead;
1718     if (abs(endhead) > rows_)
1719         return false;
1720     return (row_of_cell(cell) == (abs(endhead)-1));
1721 }
1722
1723
1724 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1725 {
1726     row = endfirsthead;
1727     if (abs(endfirsthead) > rows_)
1728         return false;
1729     return (row_of_cell(cell) == (abs(endfirsthead)-1));
1730 }
1731
1732
1733 void LyXTabular::SetLTFoot(int cell, bool last)
1734 {
1735     int row = row_of_cell(cell);
1736     int val = (row + 1) * (column_of_cell(cell)? 1:-1);
1737
1738     if (last) {
1739         if (endlastfoot == val)
1740             endlastfoot = 0;
1741         else
1742             endlastfoot = val;
1743     } else {
1744         if (endfoot == val)
1745             endfoot = 0;
1746         else
1747             endfoot = val;
1748     }
1749 }
1750
1751
1752 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1753 {
1754     row = endfoot;
1755     if ((endfoot+1) > rows_)
1756         return false;
1757     return (row_of_cell(cell) == (abs(endfoot)-1));
1758 }
1759
1760 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1761 {
1762     row = endlastfoot;
1763     if (abs(endlastfoot) > rows_)
1764         return false;
1765     return (row_of_cell(cell) == (abs(endlastfoot)-1));
1766 }
1767
1768
1769 void LyXTabular::SetLTNewPage(int cell, bool what)
1770 {
1771     row_info[row_of_cell(cell)].newpage = what;
1772 }
1773
1774
1775 bool LyXTabular::GetLTNewPage(int cell) const
1776 {
1777     return row_info[row_of_cell(cell)].newpage;
1778 }
1779
1780
1781 bool LyXTabular::SetAscentOfRow(int row, int height)
1782 {
1783     if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1784         return false;
1785     row_info[row].ascent_of_row = height;
1786     return true;
1787 }
1788
1789
1790 bool LyXTabular::SetDescentOfRow(int row, int height)
1791 {
1792     if ((row >= rows_) || (row_info[row].descent_of_row == height))
1793         return false;
1794     row_info[row].descent_of_row = height;
1795     return true;
1796 }
1797
1798
1799 int LyXTabular::GetAscentOfRow(int row) const
1800 {
1801     if (row >= rows_)
1802         return 0;
1803     return row_info[row].ascent_of_row;
1804 }
1805
1806
1807 int LyXTabular::GetDescentOfRow(int row) const
1808 {
1809     if (row >= rows_)
1810         return 0;
1811     return row_info[row].descent_of_row;
1812 }
1813
1814
1815 int LyXTabular::GetHeightOfTabular() const
1816 {
1817     int height = 0;
1818
1819     for(int row = 0; row < rows_; ++row)
1820         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1821             GetAdditionalHeight(GetCellNumber(row, 0));
1822     return height;
1823 }
1824
1825
1826 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1827 {
1828     if ((row >= rows_) || (column >= columns_))
1829         return false;
1830     return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1831 }
1832
1833
1834 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1835 {
1836     int fcell = GetFirstCellInRow(row);
1837     int n = NumberOfCellsInRow(fcell) + fcell;
1838     int tmp=0;
1839     int i;
1840
1841     for (i = fcell; i < n; ++i) {
1842         if (TopLine(i))
1843             ++tmp;
1844     }
1845     if (tmp == (n - fcell)){
1846         os << "\\hline ";
1847     } else {
1848         for (i = fcell; i < n; ++i) {
1849             if (TopLine(i)) {
1850                 os << "\\cline{"
1851                    << column_of_cell(i) + 1
1852                    << '-'
1853                    << right_column_of_cell(i) + 1
1854                    << "} ";
1855             }
1856         }
1857     }
1858     if (tmp) {
1859         os << endl;
1860         return 1;
1861     }
1862     return 0;
1863 }
1864
1865
1866 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1867 {
1868     int fcell = GetFirstCellInRow(row);
1869     int n = NumberOfCellsInRow(fcell) + fcell;
1870     int tmp = 0;
1871     int i;
1872
1873     for (i = fcell; i < n; ++i) {
1874         if (BottomLine(i))
1875             ++tmp;
1876     }
1877     if (tmp == (n-fcell)){
1878         os << "\\hline";
1879     } else {
1880         for (i = fcell; i < n; ++i) {
1881             if (BottomLine(i)) {
1882                 os << "\\cline{"
1883                    << column_of_cell(i) + 1
1884                    << '-'
1885                    << right_column_of_cell(i) + 1
1886                    << "} ";
1887             }
1888         }
1889     }
1890     if (tmp) {
1891         os << endl;
1892         return 1;
1893     }
1894     return 0;
1895 }
1896
1897
1898 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1899 {
1900     int ret = 0;
1901
1902     if (GetRotateCell(cell)) {
1903         os << "\\begin{sideways}" << endl;
1904         ++ret;
1905     }
1906     if (IsMultiColumn(cell)) {
1907         os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1908         if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1909             os << cellinfo_of_cell(cell+1)->align_special << "}{";
1910         } else {
1911             if (LeftLine(cell))
1912                 os << '|';
1913             if (!GetPWidth(cell).empty()) {
1914                 switch(GetVAlignment(cell)) {
1915                 case LYX_VALIGN_TOP:
1916                     os << "p";
1917                     break;
1918                 case LYX_VALIGN_CENTER:
1919                     os << "m";
1920                     break;
1921                 case LYX_VALIGN_BOTTOM:
1922                     os << "b";
1923                     break;
1924                 }
1925                 os << "{" << GetPWidth(cell) << '}';
1926             } else {
1927                 switch (GetAlignment(cell)) {
1928                 case LYX_ALIGN_LEFT:
1929                     os << 'l';
1930                     break;
1931                 case LYX_ALIGN_RIGHT:
1932                     os << 'r';
1933                     break;
1934                 default:
1935                     os << 'c';
1936                     break;
1937                 }
1938             }
1939             if (RightLine(cell))
1940                 os << '|';
1941             if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1942                 LeftLine(cell+1))
1943                 os << '|';
1944             os << "}{";
1945         }
1946     }
1947     if (GetLinebreaks(cell)) {
1948         os << "\\parbox[";
1949         switch(GetVAlignment(cell)) {
1950         case LYX_VALIGN_TOP:
1951             os << "t";
1952             break;
1953         case LYX_VALIGN_CENTER:
1954             os << "c";
1955             break;
1956         case LYX_VALIGN_BOTTOM:
1957             os << "b";
1958             break;
1959         }
1960         os << "]{" << GetPWidth(cell) << "}{\\smallskip{}";
1961     }
1962     return ret;
1963 }
1964
1965
1966 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1967 {
1968     int ret = 0;
1969
1970     // usual cells
1971     if (GetLinebreaks(cell))
1972         os << "\\smallskip{}}";
1973     if (IsMultiColumn(cell)){
1974         os << '}';
1975     }
1976     if (GetRotateCell(cell)) {
1977         os << endl << "\\end{sideways}";
1978         ++ret;
1979     }
1980     return ret;
1981 }
1982
1983
1984 int LyXTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
1985 {
1986     int ret = 0;
1987     int i,j;
1988     int cell = 0;
1989
1990     //+---------------------------------------------------------------------
1991     //+                      first the opening preamble                    +
1992     //+---------------------------------------------------------------------
1993
1994     if (rotate) {
1995         os << "\\begin{sideways}" << endl;
1996         ++ret;
1997     }
1998     if (is_long_tabular)
1999         os << "\\begin{longtable}{";
2000     else
2001         os << "\\begin{tabular}{";
2002     for (i = 0; i < columns_; ++i) {
2003         if (column_info[i].left_line)
2004             os << '|';
2005         if (!column_info[i].align_special.empty()) {
2006             os << column_info[i].align_special;
2007         } else if (!column_info[i].p_width.empty()) {
2008             switch(column_info[i].valignment) {
2009             case LYX_VALIGN_TOP:
2010                 os << "p";
2011                 break;
2012             case LYX_VALIGN_CENTER:
2013                 os << "m";
2014                 break;
2015             case LYX_VALIGN_BOTTOM:
2016                 os << "b";
2017                 break;
2018             }
2019             os << "{"
2020                << column_info[i].p_width
2021                << '}';
2022         } else {
2023             switch (column_info[i].alignment) {
2024             case LYX_ALIGN_LEFT:
2025                 os << 'l';
2026                 break;
2027             case LYX_ALIGN_RIGHT:
2028                 os << 'r';
2029                 break;
2030             default:
2031                 os << 'c';
2032                 break;
2033             }
2034         }
2035         if (column_info[i].right_line)
2036             os << '|';
2037     }
2038     os << "}" << endl;
2039     ++ret;
2040
2041     //+---------------------------------------------------------------------
2042     //+                      the single row and columns (cells)            +
2043     //+---------------------------------------------------------------------
2044
2045     int bret;
2046     for(i=0; i < rows_; ++i) {
2047         ret += TeXTopHLine(os, i);
2048         bret = ret;
2049         if (IsLongTabular()) {
2050             if ((endhead < 0) && (i == (abs(endhead)-1))) {
2051                 os << "\\endhead\n";
2052                 ++ret;
2053             }
2054             if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2055                 os << "\\endfirsthead\n";
2056                 ++ret;
2057             }
2058             if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2059                 os << "\\endfoot\n";
2060                 ++ret;
2061             }
2062             if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2063                 os << "\\endlastfoot\n";
2064                 ++ret;
2065             }
2066         }
2067         if (ret > bret) {
2068             if (i > 0)
2069                 ret += TeXBottomHLine(os, i-1);
2070             ret += TeXTopHLine(os, i);
2071         }
2072         for(j=0; j < columns_; ++j) {
2073             if (IsPartOfMultiColumn(i,j))
2074                 continue;
2075             ret += TeXCellPreamble(os, cell);
2076             ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2077             ret += TeXCellPostamble(os, cell);
2078             if (!IsLastCellInRow(cell)) { // not last cell in row
2079                 os << "&" << endl;
2080                 ++ret;
2081             }
2082             ++cell;
2083         }
2084         os << "\\\\" << endl;
2085         ret += TeXBottomHLine(os, i);
2086         bret = ret;
2087         if (IsLongTabular()) {
2088             if ((endhead > 0) && (i == (endhead-1))) {
2089                 os << "\\endhead\n";
2090                 ++ret;
2091             }
2092             if ((endfirsthead > 0) && (i == (endfirsthead-1))) {
2093                 os << "\\endfirsthead\n";
2094                 ++ret;
2095             }
2096             if ((endfoot > 0) && (i == (endfoot-1))) {
2097                 os << "\\endfoot\n";
2098                 ++ret;
2099             }
2100             if ((endlastfoot > 0) && (i == (endlastfoot-1))) {
2101                 os << "\\endlastfoot\n";
2102                 ++ret;
2103             }
2104             if (ret > bret)
2105                 ret += TeXBottomHLine(os, i);
2106             if (row_info[i].newpage) {
2107                 os << "\\newpage\n";
2108                 ++ret;
2109             }
2110         }
2111     }
2112
2113     //+---------------------------------------------------------------------
2114     //+                      the closing of the tabular                    +
2115     //+---------------------------------------------------------------------
2116
2117     if (is_long_tabular)
2118         os << "\\end{longtable}";
2119     else
2120         os << "\\end{tabular}";
2121     if (rotate) {
2122         os << "\n\\end{sideways}";
2123         ++ret;
2124     }
2125
2126     return ret;
2127 }
2128
2129
2130 InsetText * LyXTabular::GetCellInset(int cell) const
2131 {
2132     return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2133 }
2134
2135 void LyXTabular::Validate(LaTeXFeatures & features) const
2136 {
2137     if (IsLongTabular())
2138         features.longtable = true;
2139     if (NeedRotating())
2140         features.rotating = true;
2141     for(int cell = 0; cell < numberofcells; ++cell) {
2142         if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2143             features.array = true;
2144         GetCellInset(cell)->Validate(features);
2145     }
2146 }