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