]> git.lyx.org Git - lyx.git/blob - src/tabular.C
use the new sstream return non-pods as const, use string instead of char * in a lot...
[lyx.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "lyx_gui_misc.h"
27 #include "buffer.h"
28 #include "BufferView.h"
29 #include "Painter.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
35
36 using std::ostream;
37 using std::istream;
38 using std::getline;
39 using std::max;
40 using std::endl;
41 using std::vector;
42
43 static int const WIDTH_OF_LINE = 5;
44
45 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                 return;
1316             }
1317             row = row_of_cell(cell);
1318             if (cont_row_info[row]) {
1319                 DeleteRow(row);
1320                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1321                 while(!IsFirstCellInRow(--cell));
1322             } else {
1323                 inset = GetCellInset(cell);
1324                 continue;
1325             }
1326             inset = GetCellInset(cell);
1327             row = row_of_cell(cell);
1328             if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1329             {
1330                 // insert a space instead
1331                 par->Erase(i);
1332                 par->InsertChar(i, ' ');
1333             }
1334         }
1335         par->CopyIntoMinibuffer(current_view->buffer()->params, i);
1336         inset->par->InsertFromMinibuffer(inset->par->Last());
1337     }
1338     Reinit();
1339 }
1340
1341
1342 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1343 {
1344         int i = isColumn ? cell : column_of_cell(cell);
1345         
1346         //if (isColumn)
1347         //i = cell;
1348         //else
1349         //i = column_of_cell(cell);
1350     if (!isColumn && IsMultiColumn(cell)) {
1351        if (!cellinfo_of_cell(cell)->align_special.empty()) {
1352            return cellinfo_of_cell(cell)->align_special;
1353        } else {
1354            switch (GetAlignment(cell)) {
1355            case LYX_ALIGN_LEFT:
1356                return "left";
1357            case LYX_ALIGN_RIGHT:
1358                return "right";
1359            default:
1360                return "center";
1361            }
1362        }
1363     } else {
1364        if (!column_info[i].align_special.empty()) {
1365            return column_info[i].align_special;
1366        }
1367 #ifdef IGNORE_THIS_FOR_NOW
1368        else if (!column_info[i].p_width.empty()) {
1369            file += "p{";
1370            file += column_info[i].p_width;
1371            file += '}';
1372        }
1373 #endif
1374        else {
1375            switch (column_info[i].alignment) {
1376            case LYX_ALIGN_LEFT:
1377                return "left";
1378            case LYX_ALIGN_RIGHT:
1379                return "right";
1380            default:
1381                return "center";
1382            }
1383        }
1384     }
1385 }
1386
1387
1388 // cell <0 will tex the preamble
1389 // returns the number of printed newlines
1390 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1391 {
1392     int i;
1393     int ret = 0;
1394     //int tmp; // tmp2; // unused
1395     int nvcell; // fcell; // unused
1396     if (IsLastCell(cell)) {
1397             os << newlineAndDepth(--depth)
1398                << "</ENTRY>"
1399                << newlineAndDepth(--depth)
1400                << "</ROW>"
1401                << newlineAndDepth(--depth)
1402                << "</TBODY>"
1403                << newlineAndDepth(--depth);
1404         if (is_long_tabular)
1405                 os << "</TGROUP>";
1406         else
1407                 os << "</TGROUP>"
1408                    << newlineAndDepth(--depth);
1409         ret += 4;
1410     } else {
1411         nvcell = cell + 1;
1412         if (cell < 0) {
1413             // preamble
1414             if (is_long_tabular)
1415                     os << "<TGROUP ";
1416             else
1417                     os << "<TGROUP ";
1418             os << "COLS='"
1419                << columns_
1420                << "' COLSEP='1' ROWSEP='1'>"
1421                << newlineAndDepth(++depth);
1422             ++ret;
1423             for (i = 0; i < columns_; ++i) {
1424                     os << "<COLSPEC ALIGN='"
1425                        << GetDocBookAlign(i, true)
1426                        << "' COLNAME='col"
1427                        << i + 1
1428                        << "' COLNUM='"
1429                        << i + 1
1430                        << "' COLSEP='";
1431                if (i == (columns_-1)) {
1432                        os << '1';
1433                } else {
1434                    if (column_info[i].right_line ||
1435                        column_info[i+1].left_line)
1436                            os << '1';
1437                    else
1438                            os << '0';
1439                }
1440                os << "'>"
1441                   << newlineAndDepth(depth);
1442                 ++ret;
1443 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1444                 if (column_info[i].left_line)
1445                         os << '|';
1446 #endif
1447             }
1448             os << "<TBODY>"
1449                << newlineAndDepth(++depth)
1450                << "<ROW>"
1451                << newlineAndDepth(++depth)
1452                << "<ENTRY ALIGN='"
1453                << GetDocBookAlign(0)
1454                << "'";
1455            if (IsMultiColumn(0)) {
1456                    os << " NAMEST='col1' NAMEEND='col"
1457                       << cells_in_multicolumn(0)
1458                       << "'";
1459            }
1460            os << ">"
1461               << newlineAndDepth(++depth);
1462             ret += 3;
1463         } else {
1464             if (IsLastCellInRow(cell)) {
1465                     os << newlineAndDepth(--depth)
1466                        << "</ENTRY>"
1467                        << newlineAndDepth(--depth)
1468                        << "</ROW>"
1469                        << newlineAndDepth(depth)
1470                        << "<ROW>"
1471                        << newlineAndDepth(++depth)
1472                        << "<ENTRY ALIGN='"
1473                        << GetDocBookAlign(cell + 1)
1474                        << "' VALIGN='middle'";
1475                if (IsMultiColumn(cell + 1)) {
1476                        os << " NAMEST='col"
1477                           << column_of_cell(cell+1) + 1
1478                           << "' NAMEEND='col"
1479                           << column_of_cell(cell + 1) +
1480                                cells_in_multicolumn(cell + 1)
1481                           << "'";
1482                }
1483                os << ">"
1484                   << newlineAndDepth(++depth);
1485                 ret += 4;
1486             } else {
1487                     os << newlineAndDepth(--depth)
1488                        << "</ENTRY>"
1489                        << newlineAndDepth(depth)
1490                        << "<ENTRY ALIGN='"
1491                        << GetDocBookAlign(cell + 1)
1492                        << "' VALIGN='middle'";
1493                if (IsMultiColumn(cell + 1)) {
1494                        os << " NAMEST='col"
1495                           << column_of_cell(cell+1) + 1
1496                           << "' NAMEEND='col"
1497                           << column_of_cell(cell+1) +
1498                                cells_in_multicolumn(cell+1)
1499                           << "'";
1500                }
1501                os << ">"
1502                   << newlineAndDepth(++depth);
1503                 ret += 3;
1504             }
1505         }
1506     }
1507     return ret;
1508 }
1509
1510
1511 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1512 {
1513     return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1514         (cellinfo_of_cell(cell)->multicolumn !=LyXTabular::CELL_NORMAL));
1515 }
1516
1517
1518 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1519 {
1520     int row = row_of_cell(cell);
1521     int column = column_of_cell(cell);
1522     return  &cell_info[row][column];
1523 }
1524    
1525
1526 void LyXTabular::SetMultiColumn(int cell, int number)
1527 {
1528     cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1529     cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1530     cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1531     cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1532     for (number--; number > 0; --number) {
1533         cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1534     }
1535     set_row_column_number_info();
1536 }
1537
1538
1539 int LyXTabular::cells_in_multicolumn(int cell) const
1540 {
1541     int row = row_of_cell(cell);
1542     int column = column_of_cell(cell);
1543     int result = 1;
1544     ++column;
1545     while ((column < columns_) &&
1546            cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1547     {
1548         ++result;
1549         ++column;
1550     }
1551     return result;
1552 }
1553
1554
1555 int LyXTabular::UnsetMultiColumn(int cell)
1556 {
1557     int row = row_of_cell(cell);
1558     int column = column_of_cell(cell);
1559     
1560     int result = 0;
1561     
1562     if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1563         cell_info[row][column].multicolumn = CELL_NORMAL;
1564         ++column;
1565         while ((column < columns_) &&
1566                (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1567         {
1568             cell_info[row][column].multicolumn = CELL_NORMAL;
1569             ++column;
1570             ++result;
1571         }
1572     }
1573     set_row_column_number_info();
1574     return result;
1575 }
1576
1577
1578 void LyXTabular::SetLongTabular(int what)
1579 {
1580     is_long_tabular = what;
1581 }
1582
1583
1584 bool LyXTabular::IsLongTabular() const
1585 {
1586     return is_long_tabular;
1587 }
1588
1589
1590 void LyXTabular::SetRotateTabular(bool flag)
1591 {
1592     rotate = flag;
1593 }
1594
1595
1596 bool LyXTabular::GetRotateTabular() const
1597 {
1598     return rotate;
1599 }
1600
1601
1602 void LyXTabular::SetRotateCell(int cell, bool flag)
1603 {
1604     cellinfo_of_cell(cell)->rotate = flag;
1605 }
1606
1607
1608 bool LyXTabular::GetRotateCell(int cell) const
1609 {
1610     return cellinfo_of_cell(cell)->rotate;
1611 }
1612
1613
1614 bool LyXTabular::NeedRotating() const
1615 {
1616     if (rotate)
1617         return true;
1618     for (int i = 0; i < rows_; ++i) {
1619         for (int j = 0; j < columns_; ++j) {
1620             if (cell_info[i][j].rotate)
1621                 return true;
1622         }
1623     }
1624     return false;
1625 }
1626
1627
1628 bool LyXTabular::IsLastCell(int cell) const
1629 {
1630     if ((cell+1) < GetNumberOfCells())
1631         return false;
1632     return true;
1633 }
1634
1635
1636 int LyXTabular::GetCellAbove(int cell) const
1637 {
1638     if (row_of_cell(cell) > 0)
1639         return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1640     return cell;
1641 }
1642
1643
1644 int LyXTabular::GetCellBelow(int cell) const
1645 {
1646     if (row_of_cell(cell)+1 < rows_)
1647         return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1648     return cell;
1649 }
1650
1651
1652 int LyXTabular::GetLastCellAbove(int cell) const
1653 {
1654     if (row_of_cell(cell) <= 0)
1655         return cell;
1656     if (!IsMultiColumn(cell))
1657         return GetCellAbove(cell);
1658     return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
1659 }
1660
1661
1662 int LyXTabular::GetLastCellBelow(int cell) const
1663 {
1664     if (row_of_cell(cell)+1 >= rows_)
1665         return cell;
1666     if (!IsMultiColumn(cell))
1667         return GetCellBelow(cell);
1668     return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
1669 }
1670
1671
1672 int LyXTabular::GetCellNumber(int row, int column) const
1673 {
1674     if (column >= columns_)
1675         column = columns_ - 1;
1676     else if (column < 0)
1677         column = 0;
1678     if (row >= rows_)
1679         row = rows_ - 1;
1680     else if (row < 0)
1681         row = 0;
1682     
1683     return cell_info[row][column].cellno;
1684 }
1685
1686
1687 void LyXTabular::SetUsebox(int cell, BoxType type)
1688 {
1689     cellinfo_of_cell(cell)->usebox = type;
1690 }
1691
1692
1693 int LyXTabular::GetUsebox(int cell) const
1694 {
1695     if (column_info[column_of_cell(cell)].p_width.empty() &&
1696         !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1697         return BOX_NONE;
1698     if (cellinfo_of_cell(cell)->usebox > 1)
1699         return cellinfo_of_cell(cell)->usebox;
1700     return UseParbox(cell);
1701 }
1702
1703
1704 void LyXTabular::SetLTHead(int cell, bool first)
1705 {
1706     int const row = row_of_cell(cell);
1707     int const val = (row+1) * (column_of_cell(cell)? 1:-1);
1708
1709     if (first) {
1710         if (endfirsthead == val)
1711             endfirsthead = 0;
1712         else
1713             endfirsthead = val;
1714     } else {
1715         if (endhead == val)
1716             endhead = 0;
1717         else
1718             endhead = val;
1719     }
1720 }
1721
1722
1723 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1724 {
1725     row = endhead;
1726     if (abs(endhead) > rows_)
1727         return false;
1728     return (row_of_cell(cell) == (abs(endhead)-1));
1729 }
1730
1731
1732 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1733 {
1734     row = endfirsthead;
1735     if (abs(endfirsthead) > rows_)
1736         return false;
1737     return (row_of_cell(cell) == (abs(endfirsthead)-1));
1738 }
1739
1740
1741 void LyXTabular::SetLTFoot(int cell, bool last)
1742 {
1743     int const row = row_of_cell(cell);
1744     int const val = (row + 1) * (column_of_cell(cell)? 1:-1);
1745
1746     if (last) {
1747         if (endlastfoot == val)
1748             endlastfoot = 0;
1749         else
1750             endlastfoot = val;
1751     } else {
1752         if (endfoot == val)
1753             endfoot = 0;
1754         else
1755             endfoot = val;
1756     }
1757 }
1758
1759
1760 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1761 {
1762     row = endfoot;
1763     if ((endfoot+1) > rows_)
1764         return false;
1765     return (row_of_cell(cell) == (abs(endfoot)-1));
1766 }
1767
1768
1769 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1770 {
1771     row = endlastfoot;
1772     if (abs(endlastfoot) > rows_)
1773         return false;
1774     return (row_of_cell(cell) == (abs(endlastfoot)-1));
1775 }
1776
1777
1778 void LyXTabular::SetLTNewPage(int cell, bool what)
1779 {
1780     row_info[row_of_cell(cell)].newpage = what;
1781 }
1782
1783
1784 bool LyXTabular::GetLTNewPage(int cell) const
1785 {
1786     return row_info[row_of_cell(cell)].newpage;
1787 }
1788
1789
1790 bool LyXTabular::SetAscentOfRow(int row, int height)
1791 {
1792     if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1793         return false;
1794     row_info[row].ascent_of_row = height;
1795     return true;
1796 }
1797
1798
1799 bool LyXTabular::SetDescentOfRow(int row, int height)
1800 {
1801     if ((row >= rows_) || (row_info[row].descent_of_row == height))
1802         return false;
1803     row_info[row].descent_of_row = height;
1804     return true;
1805 }
1806
1807
1808 int LyXTabular::GetAscentOfRow(int row) const
1809 {
1810     if (row >= rows_)
1811         return 0;
1812     return row_info[row].ascent_of_row;
1813 }
1814
1815
1816 int LyXTabular::GetDescentOfRow(int row) const
1817 {
1818     if (row >= rows_)
1819         return 0;
1820     return row_info[row].descent_of_row;
1821 }
1822
1823
1824 int LyXTabular::GetHeightOfTabular() const
1825 {
1826     int height = 0;
1827
1828     for(int row = 0; row < rows_; ++row)
1829         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1830             GetAdditionalHeight(GetCellNumber(row, 0));
1831     return height;
1832 }
1833
1834
1835 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1836 {
1837     if ((row >= rows_) || (column >= columns_))
1838         return false;
1839     return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1840 }
1841
1842
1843 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1844 {
1845     int fcell = GetFirstCellInRow(row);
1846     int n = NumberOfCellsInRow(fcell) + fcell;
1847     int tmp=0;
1848     int i;
1849
1850     for (i = fcell; i < n; ++i) {
1851         if (TopLine(i))
1852             ++tmp;
1853     }
1854     if (tmp == (n - fcell)){
1855         os << "\\hline ";
1856     } else {
1857         for (i = fcell; i < n; ++i) {
1858             if (TopLine(i)) {
1859                 os << "\\cline{"
1860                    << column_of_cell(i) + 1
1861                    << '-'
1862                    << right_column_of_cell(i) + 1
1863                    << "} ";
1864             }
1865         }
1866     }
1867     if (tmp) {
1868         os << endl;
1869         return 1;
1870     }
1871     return 0;
1872 }
1873
1874
1875 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1876 {
1877     int const fcell = GetFirstCellInRow(row);
1878     int const n = NumberOfCellsInRow(fcell) + fcell;
1879     int tmp = 0;
1880
1881     for (int i = fcell; i < n; ++i) {
1882         if (BottomLine(i))
1883             ++tmp;
1884     }
1885     if (tmp == (n-fcell)){
1886         os << "\\hline";
1887     } else {
1888         for (int i = fcell; i < n; ++i) {
1889             if (BottomLine(i)) {
1890                 os << "\\cline{"
1891                    << column_of_cell(i) + 1
1892                    << '-'
1893                    << right_column_of_cell(i) + 1
1894                    << "} ";
1895             }
1896         }
1897     }
1898     if (tmp) {
1899         os << endl;
1900         return 1;
1901     }
1902     return 0;
1903 }
1904
1905
1906 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1907 {
1908     int ret = 0;
1909
1910     if (GetRotateCell(cell)) {
1911         os << "\\begin{sideways}" << endl;
1912         ++ret;
1913     }
1914     if (IsMultiColumn(cell)) {
1915         os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1916         if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1917             os << cellinfo_of_cell(cell+1)->align_special << "}{";
1918         } else {
1919             if (LeftLine(cell))
1920                 os << '|';
1921             if (!GetPWidth(cell).empty()) {
1922                 switch(GetVAlignment(cell)) {
1923                 case LYX_VALIGN_TOP:
1924                     os << "p";
1925                     break;
1926                 case LYX_VALIGN_CENTER:
1927                     os << "m";
1928                     break;
1929                 case LYX_VALIGN_BOTTOM:
1930                     os << "b";
1931                     break;
1932                 }
1933                 os << "{" << GetPWidth(cell) << '}';
1934             } else {
1935                 switch (GetAlignment(cell)) {
1936                 case LYX_ALIGN_LEFT:
1937                     os << 'l';
1938                     break;
1939                 case LYX_ALIGN_RIGHT:
1940                     os << 'r';
1941                     break;
1942                 default:
1943                     os << 'c';
1944                     break;
1945                 }
1946             }
1947             if (RightLine(cell))
1948                 os << '|';
1949             if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1950                 LeftLine(cell+1))
1951                 os << '|';
1952             os << "}{";
1953         }
1954     }
1955     if (GetUsebox(cell) == BOX_PARBOX) {
1956         os << "\\parbox[";
1957         switch(GetVAlignment(cell)) {
1958         case LYX_VALIGN_TOP:
1959             os << "t";
1960             break;
1961         case LYX_VALIGN_CENTER:
1962             os << "c";
1963             break;
1964         case LYX_VALIGN_BOTTOM:
1965             os << "b";
1966             break;
1967         }
1968         os << "]{" << GetPWidth(cell) << "}{";
1969     } else if (GetUsebox(cell) == BOX_MINIPAGE) {
1970         os << "\\begin{minipage}[";
1971         switch(GetVAlignment(cell)) {
1972         case LYX_VALIGN_TOP:
1973             os << "t";
1974             break;
1975         case LYX_VALIGN_CENTER:
1976             os << "m";
1977             break;
1978         case LYX_VALIGN_BOTTOM:
1979             os << "b";
1980             break;
1981         }
1982         os << "]{" << GetPWidth(cell) << "}\n";
1983         ++ret;
1984     }
1985     return ret;
1986 }
1987
1988
1989 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1990 {
1991     int ret = 0;
1992
1993     // usual cells
1994     if (GetUsebox(cell) == BOX_PARBOX)
1995         os << "}";
1996     else if (GetUsebox(cell) == BOX_MINIPAGE) {
1997         os << "%\n\\end{minipage}";
1998         ret += 2;
1999     }
2000     if (IsMultiColumn(cell)){
2001         os << '}';
2002     }
2003     if (GetRotateCell(cell)) {
2004         os << "%\n\\end{sideways}";
2005         ++ret;
2006     }
2007     return ret;
2008 }
2009
2010
2011 int LyXTabular::Latex(Buffer const * buf,
2012                       ostream & os, bool fragile, bool fp) const
2013 {
2014 #warning Jürgen, this func should be split into more funcs (Lgb)
2015     int ret = 0;
2016     int i,j;
2017     int cell = 0;
2018
2019     //+---------------------------------------------------------------------
2020     //+                      first the opening preamble                    +
2021     //+---------------------------------------------------------------------
2022
2023     if (rotate) {
2024         os << "\\begin{sideways}" << endl;
2025         ++ret;
2026     }
2027     if (is_long_tabular)
2028         os << "\\begin{longtable}{";
2029     else
2030         os << "\\begin{tabular}{";
2031     for (i = 0; i < columns_; ++i) {
2032         if (column_info[i].left_line)
2033             os << '|';
2034         if (!column_info[i].align_special.empty()) {
2035             os << column_info[i].align_special;
2036         } else if (!column_info[i].p_width.empty()) {
2037             switch(column_info[i].valignment) {
2038             case LYX_VALIGN_TOP:
2039                 os << "p";
2040                 break;
2041             case LYX_VALIGN_CENTER:
2042                 os << "m";
2043                 break;
2044             case LYX_VALIGN_BOTTOM:
2045                 os << "b";
2046                 break;
2047             }
2048             os << "{"
2049                << column_info[i].p_width
2050                << '}';
2051         } else {
2052             switch (column_info[i].alignment) {
2053             case LYX_ALIGN_LEFT:
2054                 os << 'l';
2055                 break;
2056             case LYX_ALIGN_RIGHT:
2057                 os << 'r';
2058                 break;
2059             default:
2060                 os << 'c';
2061                 break;
2062             }
2063         }
2064         if (column_info[i].right_line)
2065             os << '|';
2066     }
2067     os << "}" << endl;
2068     ++ret;
2069
2070     //+---------------------------------------------------------------------
2071     //+                      the single row and columns (cells)            +
2072     //+---------------------------------------------------------------------
2073
2074     int bret;
2075     for(i=0; i < rows_; ++i) {
2076         ret += TeXTopHLine(os, i);
2077         bret = ret;
2078         if (IsLongTabular()) {
2079             if ((endhead < 0) && (i == (abs(endhead)-1))) {
2080                 os << "\\endhead\n";
2081                 ++ret;
2082             }
2083             if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2084                 os << "\\endfirsthead\n";
2085                 ++ret;
2086             }
2087             if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2088                 os << "\\endfoot\n";
2089                 ++ret;
2090             }
2091             if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2092                 os << "\\endlastfoot\n";
2093                 ++ret;
2094             }
2095         }
2096         if (ret > bret) {
2097             if (i > 0)
2098                 ret += TeXBottomHLine(os, i-1);
2099             ret += TeXTopHLine(os, i);
2100         }
2101         for(j=0; j < columns_; ++j) {
2102             if (IsPartOfMultiColumn(i,j))
2103                 continue;
2104             ret += TeXCellPreamble(os, cell);
2105             ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2106             ret += TeXCellPostamble(os, cell);
2107             if (!IsLastCellInRow(cell)) { // not last cell in row
2108                 os << "&" << endl;
2109                 ++ret;
2110             }
2111             ++cell;
2112         }
2113         os << "\\\\" << endl;
2114         ret += TeXBottomHLine(os, i);
2115         bret = ret;
2116         if (IsLongTabular()) {
2117             if ((endhead > 0) && (i == (endhead-1))) {
2118                 os << "\\endhead\n";
2119                 ++ret;
2120             }
2121             if ((endfirsthead > 0) && (i == (endfirsthead-1))) {
2122                 os << "\\endfirsthead\n";
2123                 ++ret;
2124             }
2125             if ((endfoot > 0) && (i == (endfoot-1))) {
2126                 os << "\\endfoot\n";
2127                 ++ret;
2128             }
2129             if ((endlastfoot > 0) && (i == (endlastfoot-1))) {
2130                 os << "\\endlastfoot\n";
2131                 ++ret;
2132             }
2133             if (ret > bret)
2134                 ret += TeXBottomHLine(os, i);
2135             if (row_info[i].newpage) {
2136                 os << "\\newpage\n";
2137                 ++ret;
2138             }
2139         }
2140     }
2141
2142     //+---------------------------------------------------------------------
2143     //+                      the closing of the tabular                    +
2144     //+---------------------------------------------------------------------
2145
2146     if (is_long_tabular)
2147         os << "\\end{longtable}";
2148     else
2149         os << "\\end{tabular}";
2150     if (rotate) {
2151         os << "\n\\end{sideways}";
2152         ++ret;
2153     }
2154
2155     return ret;
2156 }
2157
2158
2159 InsetText * LyXTabular::GetCellInset(int cell) const
2160 {
2161     return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2162 }
2163
2164
2165 void LyXTabular::Validate(LaTeXFeatures & features) const
2166 {
2167     if (IsLongTabular())
2168         features.longtable = true;
2169     if (NeedRotating())
2170         features.rotating = true;
2171     for(int cell = 0; cell < numberofcells; ++cell) {
2172         if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2173             features.array = true;
2174         GetCellInset(cell)->Validate(features);
2175     }
2176 }
2177
2178
2179 bool LyXTabular::UseParbox(int cell) const
2180 {
2181     LyXParagraph *par = GetCellInset(cell)->par;
2182
2183     for(;par; par = par->next) {
2184         for(int i = 0; i < par->Last(); ++i) {
2185             if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2186                 return true;
2187         }
2188     }
2189     return false;
2190 }