]> git.lyx.org Git - features.git/blob - src/tabular.C
295aaa3417065bf3aae5546879c6564642319896
[features.git] / src / tabular.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2002 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ======================================================
11  */
12
13 #include <config.h>
14
15 // temporary until verified (08/08/2001 Jug)
16 #define SPECIAL_COLUM_HANDLING 1
17
18 #include "tabular.h"
19 #include "debug.h"
20 #include "vspace.h"
21 #include "layout.h"
22 #include "buffer.h"
23 #include "BufferView.h"
24 #include "frontends/Painter.h"
25 #include "LaTeXFeatures.h"
26 #include "insets/insettabular.h"
27 #include "insets/insettext.h"
28 #include "support/lstrings.h"
29 #include "support/lyxmanip.h"
30 #include "support/LAssert.h"
31 #include "frontends/Alert.h"
32 #include "gettext.h"
33 #include "Lsstream.h"
34 #include "tabular_funcs.h"
35 #include "lyxlex.h"
36
37 #include <algorithm>
38 #include <cstdlib>
39
40 using std::abs;
41 using std::ostream;
42 using std::istream;
43 using std::getline;
44 using std::max;
45 using std::endl;
46 using std::vector;
47
48 #ifndef CXX_GLOBAL_CSTD
49 using std::strlen;
50 #endif
51
52 namespace {
53
54         int const WIDTH_OF_LINE = 5;
55
56 } // namespace
57
58 /// Define a few methods for the inner structs
59
60 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
61         : inset(bg)
62 {
63         cellno = 0;
64         width_of_cell = 0;
65         multicolumn = LyXTabular::CELL_NORMAL;
66         alignment = LYX_ALIGN_CENTER;
67         valignment = LYX_VALIGN_TOP;
68         top_line = true;
69         bottom_line = false;
70         left_line = true;
71         right_line = false;
72         usebox = BOX_NONE;
73         rotate = false;
74 }
75
76
77 LyXTabular::rowstruct::rowstruct()
78 {
79         top_line = true;
80         bottom_line = false;
81         ascent_of_row = 0;
82         descent_of_row = 0;
83         endhead = false;
84         endfirsthead = false;
85         endfoot = false;
86         endlastfoot = false;
87         newpage = false;
88 }
89
90
91 LyXTabular::columnstruct::columnstruct()
92 {
93         left_line = true;
94         right_line = false;
95         alignment = LYX_ALIGN_CENTER;
96         valignment = LYX_VALIGN_TOP;
97         width_of_column = 0;
98 }
99
100
101 LyXTabular::ltType::ltType()
102 {
103         topDL = false;
104         bottomDL = false;
105         empty = false;
106 }
107
108
109 /* konstruktor */
110 LyXTabular::LyXTabular(BufferParams const & bp,
111                        InsetTabular * inset, int rows_arg, int columns_arg)
112 {
113         owner_ = inset;
114         cur_cell = -1;
115         Init(bp, rows_arg, columns_arg);
116 }
117
118
119 LyXTabular::LyXTabular(BufferParams const & bp,
120                        InsetTabular * inset, LyXTabular const & lt)
121 {
122         owner_ = inset;
123         cur_cell = -1;
124         Init(bp, lt.rows_, lt.columns_, &lt);
125 #if 0
126 #ifdef WITH_WARNINGS
127 #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)
128 #endif
129         operator=(lt);
130 #endif
131 }
132
133
134 // LyXTabular::LyXTabular(BufferParams const & bp,
135 //                     InsetTabular * inset, LyXTabular const & lt,
136 //                     bool same_id)
137 // {
138 //      owner_ = inset;
139 //      cur_cell = -1;
140 //      Init(bp, lt.rows_, lt.columns_, &lt);
141 //      // we really should change again to have InsetText as a pointer
142 //      // and allocate it then we would not have to do this stuff all
143 //      // double!
144 //      if (same_id) {
145 //              for (int i = 0; i < rows_; ++i) {
146 //                      for (int j = 0; j < columns_; ++j) {
147 //                              cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
148 //                              cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraphs, true);
149 //                      }
150 //              }
151 //      }
152 // #if 0
153 // #ifdef WITH_WARNINGS
154 // #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)
155 // #endif
156 //      operator=(lt);
157 // #endif
158 // }
159
160
161 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
162 {
163         owner_ = inset;
164         cur_cell = -1;
165         Read(buf, lex);
166 }
167
168
169 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
170 {
171 #if 0
172 #warning This whole method should look like this: (Lgb)
173
174                 LyXTabular tmp(lt);
175                 tmp.swap(*this);
176 #else
177         // If this and lt is not of the same size we have a serious bug
178         // So then it is ok to throw an exception, or for now
179         // call abort()
180         lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
181         cur_cell = -1;
182         cell_info = lt.cell_info;
183         row_info = lt.row_info;
184         column_info = lt.column_info;
185         SetLongTabular(lt.is_long_tabular);
186         rotate = lt.rotate;
187
188         Reinit();
189 #endif
190         return *this;
191 }
192
193
194 LyXTabular * LyXTabular::clone(BufferParams const & bp,
195                                InsetTabular * inset)
196 {
197         LyXTabular * result = new LyXTabular(bp, inset, *this);
198 #if 0
199         // don't know if this is good but I need to Clone also
200         // the text-insets here, this is for the Undo-facility!
201         for (int i = 0; i < rows_; ++i) {
202                 for (int j = 0; j < columns_; ++j) {
203                         result->cell_info[i][j].inset = cell_info[i][j].inset;
204                         result->cell_info[i][j].inset.setOwner(inset);
205                 }
206         }
207 #endif
208         return result;
209 }
210
211
212 // LyXTabular * LyXTabular::clone(BufferParams const & bp,
213 //                             InsetTabular * inset, bool same_id)
214 // {
215 //      LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
216 // #if 0
217 //      // don't know if this is good but I need to Clone also
218 //      // the text-insets here, this is for the Undo-facility!
219 //      for (int i = 0; i < rows_; ++i) {
220 //              for (int j = 0; j < columns_; ++j) {
221 //                      result->cell_info[i][j].inset = cell_info[i][j].inset;
222 //                      result->cell_info[i][j].inset.setOwner(inset);
223 //              }
224 //      }
225 // #endif
226 //      return result;
227 // }
228
229
230 /* activates all lines and sets all widths to 0 */
231 void LyXTabular::Init(BufferParams const & bp,
232                       int rows_arg, int columns_arg, LyXTabular const * lt)
233 {
234         rows_ = rows_arg;
235         columns_ = columns_arg;
236         row_info = row_vector(rows_, rowstruct());
237         column_info = column_vector(columns_, columnstruct());
238         cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
239
240         if (lt) {
241                 operator=(*lt);
242                 return;
243         }
244
245         int cellno = 0;
246         for (int i = 0; i < rows_; ++i) {
247                 for (int j = 0; j < columns_; ++j) {
248                         cell_info[i][j].inset.setOwner(owner_);
249                         cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
250                         cell_info[i][j].cellno = cellno++;
251                 }
252                 cell_info[i].back().right_line = true;
253         }
254         row_info.back().bottom_line = true;
255         row_info.front().bottom_line = true;
256
257         for (int i = 0; i < columns_; ++i) {
258                 calculate_width_of_column(i);
259         }
260         column_info.back().right_line = true;
261
262         calculate_width_of_tabular();
263
264         rowofcell = vector<int>();
265         columnofcell = vector<int>();
266         set_row_column_number_info();
267         is_long_tabular = false;
268         rotate = false;
269 }
270
271
272 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
273 {
274         ++rows_;
275
276         int row = row_of_cell(cell);
277
278         row_vector::iterator rit = row_info.begin() + row;
279         row_info.insert(rit, rowstruct());
280         // now set the values of the row before
281         row_info[row] = row_info[row + 1];
282
283 #if 0
284         cell_vvector::iterator cit = cell_info.begin() + row;
285         cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
286 #else
287         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
288                                                               cellstruct(bp)));
289
290         for (int i = 0; i <= row; ++i) {
291                 for (int j = 0; j < columns_; ++j) {
292                         c_info[i][j] = cell_info[i][j];
293                 }
294         }
295         for (int i = row + 1; i < rows_; ++i) {
296                 for (int j = 0; j < columns_; ++j) {
297                         c_info[i][j] = cell_info[i-1][j];
298                 }
299         }
300         cell_info = c_info;
301         ++row;
302         for (int j = 0; j < columns_; ++j) {
303                 cell_info[row][j].inset.clear(false);
304                 if (bp.tracking_changes)
305                         cell_info[row][j].inset.markNew(true);
306         }
307 #endif
308         Reinit();
309 }
310
311
312 void LyXTabular::DeleteRow(int row)
313 {
314         if (rows_ == 1) return; // Not allowed to delete last row
315
316         row_info.erase(row_info.begin() + row); //&row_info[row]);
317         cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
318         --rows_;
319         Reinit();
320 }
321
322
323 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
324 {
325         ++columns_;
326
327         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
328                                                               cellstruct(bp)));
329         int const column = column_of_cell(cell);
330         column_vector::iterator cit = column_info.begin() + column + 1;
331         column_info.insert(cit, columnstruct());
332         // set the column values of the column before
333         column_info[column + 1] = column_info[column];
334
335         for (int i = 0; i < rows_; ++i) {
336                 for (int j = 0; j <= column; ++j) {
337                         c_info[i][j] = cell_info[i][j];
338                 }
339                 for (int j = column + 1; j < columns_; ++j) {
340                         c_info[i][j] = cell_info[i][j - 1];
341                 }
342                 // care about multicolumns
343                 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
344                 {
345                         c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
346                 }
347                 if ((column + 2) >= columns_ ||
348                         c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
349                 {
350                         c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
351                 }
352         }
353         cell_info = c_info;
354         //++column;
355         for (int i = 0; i < rows_; ++i) {
356                 cell_info[i][column + 1].inset.clear(false);
357                 if (bp.tracking_changes)
358                         cell_info[i][column + 1].inset.markNew(true);
359         }
360         Reinit();
361 }
362
363
364 void LyXTabular::DeleteColumn(int column)
365 {
366         // Similar to DeleteRow
367         //if (!(columns_ - 1))
368         //return;
369         if (columns_ == 1) return; // Not allowed to delete last column
370
371         column_info.erase(column_info.begin() + column);
372         for (int i = 0; i < rows_; ++i) {
373                 cell_info[i].erase(cell_info[i].begin() + column);
374         }
375         --columns_;
376         Reinit();
377 }
378
379
380 void LyXTabular::reinit()
381 {
382         Reinit(false);
383 }
384
385
386 void LyXTabular::Reinit(bool reset_widths)
387 {
388         if (reset_widths) {
389                 for (int i = 0; i < rows_; ++i) {
390                         for (int j = 0; j < columns_; ++j) {
391                                 cell_info[i][j].width_of_cell = 0;
392                                 cell_info[i][j].inset.setOwner(owner_);
393                         }
394                 }
395         }
396
397         for (int i = 0; i < columns_; ++i) {
398                 calculate_width_of_column(i);
399         }
400         calculate_width_of_tabular();
401
402         set_row_column_number_info();
403 }
404
405
406 void LyXTabular::set_row_column_number_info(bool oldformat)
407 {
408         numberofcells = -1;
409         for (int row = 0; row < rows_; ++row) {
410                 for (int column = 0; column<columns_; ++column) {
411                         if (cell_info[row][column].multicolumn
412                                 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
413                                 ++numberofcells;
414                         cell_info[row][column].cellno = numberofcells;
415                 }
416         }
417         ++numberofcells; // because this is one more than as we start from 0
418
419         rowofcell.resize(numberofcells);
420         columnofcell.resize(numberofcells);
421
422         for (int row = 0, column = 0, c = 0;
423                  c < numberofcells && row < rows_ && column < columns_;) {
424                 rowofcell[c] = row;
425                 columnofcell[c] = column;
426                 ++c;
427                 do {
428                         ++column;
429                 } while (column < columns_ &&
430                                  cell_info[row][column].multicolumn
431                                  == LyXTabular::CELL_PART_OF_MULTICOLUMN);
432                 if (column == columns_) {
433                         column = 0;
434                         ++row;
435                 }
436         }
437
438         for (int row = 0; row < rows_; ++row) {
439                 for (int column = 0; column < columns_; ++column) {
440                         if (IsPartOfMultiColumn(row,column))
441                                 continue;
442                         // now set the right line of multicolumns right for oldformat read
443                         if (oldformat &&
444                                 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
445                         {
446                                 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
447                                 cell_info[row][column].right_line =
448                                         cell_info[row][column+cn-1].right_line;
449                         }
450                         cell_info[row][column].inset.setAutoBreakRows(
451                                 !GetPWidth(GetCellNumber(row, column)).zero());
452                 }
453         }
454 }
455
456
457 int LyXTabular::GetNumberOfCells() const
458 {
459         return numberofcells;
460 }
461
462
463 int LyXTabular::NumberOfCellsInRow(int cell) const
464 {
465         int const row = row_of_cell(cell);
466         int result = 0;
467         for (int i = 0; i < columns_; ++i) {
468                 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
469                         ++result;
470         }
471         return result;
472 }
473
474
475 /* returns 1 if there is a topline, returns 0 if not */
476 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
477 {
478         int const row = row_of_cell(cell);
479
480         if (!onlycolumn && IsMultiColumn(cell))
481                 return cellinfo_of_cell(cell)->top_line;
482         return row_info[row].top_line;
483 }
484
485
486 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
487 {
488         // no bottom line underneath non-existent cells if you please
489         // Isn't that a programming error? Is so this should
490         // be an Assert instead. (Lgb)
491         if (cell >= numberofcells)
492                 return false;
493
494         if (!onlycolumn && IsMultiColumn(cell))
495                 return cellinfo_of_cell(cell)->bottom_line;
496         return row_info[row_of_cell(cell)].bottom_line;
497 }
498
499
500 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
501 {
502         if (!onlycolumn && IsMultiColumn(cell) &&
503                 (IsFirstCellInRow(cell) || IsMultiColumn(cell-1)))
504         {
505 #ifdef SPECIAL_COLUM_HANDLING
506                 if (cellinfo_of_cell(cell)->align_special.empty())
507                         return cellinfo_of_cell(cell)->left_line;
508                 return prefixIs(ltrim(cellinfo_of_cell(cell)->align_special), "|");
509 #else
510                 return cellinfo_of_cell(cell)->left_line;
511 #endif
512         }
513 #ifdef SPECIAL_COLUM_HANDLING
514         if (column_info[column_of_cell(cell)].align_special.empty())
515                 return column_info[column_of_cell(cell)].left_line;
516         return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
517 #else
518         return column_info[column_of_cell(cell)].left_line;
519 #endif
520 }
521
522
523 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
524 {
525         if (!onlycolumn && IsMultiColumn(cell) &&
526                 (IsLastCellInRow(cell) || IsMultiColumn(cell+1)))
527         {
528 #ifdef SPECIAL_COLUM_HANDLING
529                 if (cellinfo_of_cell(cell)->align_special.empty())
530                         return cellinfo_of_cell(cell)->right_line;
531                 return suffixIs(rtrim(cellinfo_of_cell(cell)->align_special), "|");
532 #else
533                 return cellinfo_of_cell(cell)->right_line;
534 #endif
535         }
536 #ifdef SPECIAL_COLUM_HANDLING
537         if (column_info[column_of_cell(cell)].align_special.empty())
538                 return column_info[right_column_of_cell(cell)].right_line;
539         return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
540 #else
541         return column_info[right_column_of_cell(cell)].right_line;
542 #endif
543 }
544
545
546 bool LyXTabular::topAlreadyDrawn(int cell) const
547 {
548         int row = row_of_cell(cell);
549         if ((row > 0) && !GetAdditionalHeight(row)) {
550                 int column = column_of_cell(cell);
551                 --row;
552                 while (column
553                            && cell_info[row][column].multicolumn
554                            == LyXTabular::CELL_PART_OF_MULTICOLUMN)
555                         --column;
556                 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
557                         return row_info[row].bottom_line;
558                 else
559                         return cell_info[row][column].bottom_line;
560         }
561         return false;
562 }
563
564
565 bool LyXTabular::leftAlreadyDrawn(int cell) const
566 {
567         int column = column_of_cell(cell);
568         if (column > 0) {
569                 int row = row_of_cell(cell);
570                 while (--column &&
571                            (cell_info[row][column].multicolumn ==
572                                 LyXTabular::CELL_PART_OF_MULTICOLUMN));
573                 if (GetAdditionalWidth(cell_info[row][column].cellno))
574                         return false;
575 #ifdef SPECIAL_COLUM_HANDLING
576                 return RightLine(cell_info[row][column].cellno);
577 #else
578                 return RightLine(cell_info[row][column].cellno, true);
579 #endif
580         }
581         return false;
582 }
583
584
585 bool LyXTabular::IsLastRow(int cell) const
586 {
587         return (row_of_cell(cell) == rows_ - 1);
588 }
589
590
591 int LyXTabular::GetAdditionalHeight(int row) const
592 {
593         if (!row || row >= rows_)
594                 return 0;
595
596         bool top = true;
597         bool bottom = true;
598
599         for (int column = 0; column < columns_ && bottom; ++column) {
600                 switch (cell_info[row - 1][column].multicolumn) {
601                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
602                         bottom = cell_info[row - 1][column].bottom_line;
603                         break;
604                 case LyXTabular::CELL_NORMAL:
605                         bottom = row_info[row - 1].bottom_line;
606                 }
607         }
608         for (int column = 0; column < columns_ && top; ++column) {
609                 switch (cell_info[row][column].multicolumn) {
610                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
611                         top = cell_info[row][column].top_line;
612                         break;
613                 case LyXTabular::CELL_NORMAL:
614                         top = row_info[row].top_line;
615                 }
616         }
617         if (top && bottom)
618                 return WIDTH_OF_LINE;
619         return 0;
620 }
621
622
623 int LyXTabular::GetAdditionalWidth(int cell) const
624 {
625         // internally already set in SetWidthOfCell
626         // used to get it back in text.C
627         int const col = right_column_of_cell(cell);
628         int const row = row_of_cell(cell);
629         if (col < columns_ - 1 && RightLine(cell) &&
630                 LeftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
631         {
632                 return WIDTH_OF_LINE;
633         } else {
634                 return 0;
635         }
636 }
637
638
639 // returns the maximum over all rows
640 int LyXTabular::GetWidthOfColumn(int cell) const
641 {
642         int const column1 = column_of_cell(cell);
643         int const column2 = right_column_of_cell(cell);
644         int result = 0;
645         for (int i = column1; i <= column2; ++i) {
646                 result += column_info[i].width_of_column;
647         }
648         return result;
649 }
650
651
652 int LyXTabular::GetWidthOfTabular() const
653 {
654         return width_of_tabular;
655 }
656
657
658 /* returns 1 if a complete update is necessary, otherwise 0 */
659 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
660 {
661         if (!IsMultiColumn(cell))
662                 return false;
663
664         int const row = row_of_cell(cell);
665         int const column1 = column_of_cell(cell);
666         int const column2 = right_column_of_cell(cell);
667         int const old_val = cell_info[row][column2].width_of_cell;
668
669         // first set columns to 0 so we can calculate the right width
670         for (int i = column1; i <= column2; ++i) {
671                 cell_info[row][i].width_of_cell = 0;
672         }
673         // set the width to MAX_WIDTH until width > 0
674         int width = (new_width + 2 * WIDTH_OF_LINE);
675         int i = column1;
676         for (; i < column2 && width > column_info[i].width_of_column; ++i) {
677                 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
678                 width -= column_info[i].width_of_column;
679         }
680         if (width > 0) {
681                 cell_info[row][i].width_of_cell = width;
682         }
683         if (old_val != cell_info[row][column2].width_of_cell) {
684                 // in this case we have to recalculate all multicolumn cells which
685                 // have this column as one of theirs but not as last one
686                 calculate_width_of_column_NMC(i);
687                 recalculateMulticolumnsOfColumn(i);
688                 calculate_width_of_column(i);
689         }
690         return true;
691 }
692
693
694 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
695 {
696         // the last column does not have to be recalculated because all
697         // multicolumns will have here there last multicolumn cell which
698         // always will have the whole rest of the width of the cell.
699         if (column > (columns_ - 2))
700                 return;
701         for(int row = 0; row < rows_; ++row) {
702                 int mc = cell_info[row][column].multicolumn;
703                 int nmc = cell_info[row][column+1].multicolumn;
704                 // we only have to update multicolumns which do not have this
705                 // column as their last column!
706                 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
707                         ((mc == CELL_PART_OF_MULTICOLUMN) &&
708                          (nmc == CELL_PART_OF_MULTICOLUMN)))
709                 {
710                         int const cellno = cell_info[row][column].cellno;
711                         SetWidthOfMulticolCell(cellno,
712                                                GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
713                 }
714         }
715 }
716
717
718 /* returns 1 if a complete update is necessary, otherwise 0 */
719 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
720 {
721         int const row = row_of_cell(cell);
722         int const column1 = column_of_cell(cell);
723         bool tmp = false;
724         int width = 0;
725         int add_width = 0;
726
727 #ifdef SPECIAL_COLUM_HANDLING
728         if (RightLine(cell_info[row][column1].cellno, true) &&
729                 (column1 < columns_-1) &&
730                 LeftLine(cell_info[row][column1+1].cellno, true))
731 #else
732         if (column_info[column1].right_line && (column1 < columns_-1) &&
733                 column_info[column1+1].left_line) // additional width
734 #endif
735         {
736                 // additional width
737                 add_width = WIDTH_OF_LINE;
738         }
739         if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
740                 return false;
741         }
742         if (IsMultiColumn(cell, true)) {
743                 tmp = SetWidthOfMulticolCell(cell, new_width);
744         } else {
745                 width = (new_width + 2*WIDTH_OF_LINE + add_width);
746                 cell_info[row][column1].width_of_cell = width;
747                 tmp = calculate_width_of_column_NMC(column1);
748                 if (tmp)
749                         recalculateMulticolumnsOfColumn(column1);
750         }
751         if (tmp) {
752                 for (int i = 0; i < columns_; ++i)
753                         calculate_width_of_column(i);
754                 calculate_width_of_tabular();
755                 return true;
756         }
757         return false;
758 }
759
760
761 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
762 {
763         if (!IsMultiColumn(cell) || onlycolumn)
764                 column_info[column_of_cell(cell)].alignment = align;
765         if (!onlycolumn)
766                 cellinfo_of_cell(cell)->alignment = align;
767         return true;
768 }
769
770
771 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
772 {
773         if (!IsMultiColumn(cell) || onlycolumn)
774                 column_info[column_of_cell(cell)].valignment = align;
775         if (!onlycolumn)
776                 cellinfo_of_cell(cell)->valignment = align;
777         return true;
778 }
779
780
781 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
782 {
783         bool flag = !width.zero();
784         int const j = column_of_cell(cell);
785
786         column_info[j].p_width = width;
787         // This should not ne necessary anymore
788         //      if (flag) // do this only if there is a width
789         //      SetAlignment(cell, LYX_ALIGN_LEFT);
790         for (int i = 0; i < rows_; ++i) {
791                 int c = GetCellNumber(i, j);
792                 flag = !GetPWidth(c).zero(); // because of multicolumns!
793                 GetCellInset(c)->setAutoBreakRows(flag);
794         }
795         return true;
796 }
797
798
799 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
800 {
801         bool const flag = !width.zero();
802
803         cellinfo_of_cell(cell)->p_width = width;
804         if (IsMultiColumn(cell)) {
805                 GetCellInset(cell)->setAutoBreakRows(flag);
806                 return true;
807         }
808         return false;
809 }
810
811
812 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
813                                  LyXTabular::Feature what)
814 {
815         if (what == SET_SPECIAL_MULTI)
816                 cellinfo_of_cell(cell)->align_special = special;
817         else
818                 column_info[column_of_cell(cell)].align_special = special;
819         return true;
820 }
821
822
823 bool LyXTabular::SetAllLines(int cell, bool line)
824 {
825         SetTopLine(cell, line);
826         SetBottomLine(cell, line);
827         SetRightLine(cell, line);
828         SetLeftLine(cell, line);
829         return true;
830 }
831
832
833 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
834 {
835         int const row = row_of_cell(cell);
836
837         if (onlycolumn || !IsMultiColumn(cell))
838                 row_info[row].top_line = line;
839         else
840                 cellinfo_of_cell(cell)->top_line = line;
841         return true;
842 }
843
844
845 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
846 {
847         if (onlycolumn || !IsMultiColumn(cell))
848                 row_info[row_of_cell(cell)].bottom_line = line;
849         else
850                 cellinfo_of_cell(cell)->bottom_line = line;
851         return true;
852 }
853
854
855 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
856 {
857         if (onlycolumn || !IsMultiColumn(cell))
858                 column_info[column_of_cell(cell)].left_line = line;
859         else
860                 cellinfo_of_cell(cell)->left_line = line;
861         return true;
862 }
863
864
865 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
866 {
867         if (onlycolumn || !IsMultiColumn(cell))
868                 column_info[right_column_of_cell(cell)].right_line = line;
869         else
870                 cellinfo_of_cell(cell)->right_line = line;
871         return true;
872 }
873
874
875 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
876 {
877         if (!onlycolumn && IsMultiColumn(cell))
878                 return cellinfo_of_cell(cell)->alignment;
879         else
880                 return column_info[column_of_cell(cell)].alignment;
881 }
882
883
884 LyXTabular::VAlignment
885 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
886 {
887         if (!onlycolumn && IsMultiColumn(cell))
888                 return cellinfo_of_cell(cell)->valignment;
889         else
890                 return column_info[column_of_cell(cell)].valignment;
891 }
892
893
894 LyXLength const LyXTabular::GetPWidth(int cell) const
895 {
896         if (IsMultiColumn(cell))
897                 return cellinfo_of_cell(cell)->p_width;
898         return column_info[column_of_cell(cell)].p_width;
899 }
900
901
902 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
903 {
904         return column_info[column_of_cell(cell)].p_width;
905 }
906
907
908 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
909 {
910         if (IsMultiColumn(cell))
911                 return cellinfo_of_cell(cell)->p_width;
912         return LyXLength();
913 }
914
915
916 string const LyXTabular::GetAlignSpecial(int cell, int what) const
917 {
918         if (what == SET_SPECIAL_MULTI)
919                 return cellinfo_of_cell(cell)->align_special;
920         return column_info[column_of_cell(cell)].align_special;
921 }
922
923
924 int LyXTabular::GetWidthOfCell(int cell) const
925 {
926         int const row = row_of_cell(cell);
927         int const column1 = column_of_cell(cell);
928         int const column2 = right_column_of_cell(cell);
929         int result = 0;
930         for (int i = column1; i <= column2; ++i) {
931                 result += cell_info[row][i].width_of_cell;
932         }
933         return result;
934 }
935
936
937 int LyXTabular::GetBeginningOfTextInCell(int cell) const
938 {
939         int x = 0;
940
941         switch (GetAlignment(cell)) {
942         case LYX_ALIGN_CENTER:
943                 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
944                 break;
945         case LYX_ALIGN_RIGHT:
946                 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
947                 // + GetAdditionalWidth(cell);
948                 break;
949         default: /* LYX_ALIGN_LEFT: nothing :-) */
950                 break;
951         }
952
953         // the LaTeX Way :-(
954         x += WIDTH_OF_LINE;
955         return x;
956 }
957
958
959 bool LyXTabular::IsFirstCellInRow(int cell) const
960 {
961         return column_of_cell(cell) == 0;
962 }
963
964
965 int LyXTabular::GetFirstCellInRow(int row) const
966 {
967         if (row > (rows_-1))
968                 row = rows_ - 1;
969         return cell_info[row][0].cellno;
970 }
971
972 bool LyXTabular::IsLastCellInRow(int cell) const
973 {
974         return (right_column_of_cell(cell) == (columns_ - 1));
975 }
976
977
978 int LyXTabular::GetLastCellInRow(int row) const
979 {
980         if (row > (rows_-1))
981                 row = rows_ - 1;
982         return cell_info[row][columns_-1].cellno;
983 }
984
985
986 bool LyXTabular::calculate_width_of_column(int column)
987 {
988         int const old_column_width = column_info[column].width_of_column;
989         int maximum = 0;
990
991         for (int i = 0; i < rows_; ++i) {
992                 maximum = max(cell_info[i][column].width_of_cell, maximum);
993         }
994         column_info[column].width_of_column = maximum;
995         return (column_info[column].width_of_column != old_column_width);
996 }
997
998
999 //
1000 // Calculate the columns regarding ONLY the normal cells and if this
1001 // column is inside a multicolumn cell then use it only if its the last
1002 // column of this multicolumn cell as this gives an added with to the
1003 // column, all the rest should be adapted!
1004 //
1005 bool LyXTabular::calculate_width_of_column_NMC(int column)
1006 {
1007         int const old_column_width = column_info[column].width_of_column;
1008         int max = 0;
1009         for (int i = 0; i < rows_; ++i) {
1010                 int cell = GetCellNumber(i, column);
1011                 bool ismulti = IsMultiColumn(cell, true);
1012                 if ((!ismulti || (column == right_column_of_cell(cell))) &&
1013                         (cell_info[i][column].width_of_cell > max))
1014                 {
1015                         max = cell_info[i][column].width_of_cell;
1016                 }
1017         }
1018         column_info[column].width_of_column = max;
1019         return (column_info[column].width_of_column != old_column_width);
1020 }
1021
1022
1023 void LyXTabular::calculate_width_of_tabular()
1024 {
1025         width_of_tabular = 0;
1026         for (int i = 0; i < columns_; ++i) {
1027                 width_of_tabular += column_info[i].width_of_column;
1028         }
1029 }
1030
1031
1032 int LyXTabular::row_of_cell(int cell) const
1033 {
1034         if (cell >= numberofcells)
1035                 return rows_ - 1;
1036         else if (cell < 0)
1037                 return 0;
1038         return rowofcell[cell];
1039 }
1040
1041
1042 int LyXTabular::column_of_cell(int cell) const
1043 {
1044         if (cell >= numberofcells)
1045                 return columns_ - 1;
1046         else if (cell < 0)
1047                 return 0;
1048         return columnofcell[cell];
1049 }
1050
1051
1052 int LyXTabular::right_column_of_cell(int cell) const
1053 {
1054         int const row = row_of_cell(cell);
1055         int column = column_of_cell(cell);
1056         while (column < (columns_ - 1) &&
1057                    cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1058                 ++column;
1059         return column;
1060 }
1061
1062
1063 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1064 {
1065         // header line
1066         os << "<lyxtabular"
1067            << write_attribute("version", 3)
1068            << write_attribute("rows", rows_)
1069            << write_attribute("columns", columns_)
1070            << ">\n";
1071         // global longtable options
1072         os << "<features"
1073            << write_attribute("rotate", rotate)
1074            << write_attribute("islongtable", is_long_tabular)
1075            << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1076            << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1077            << write_attribute("firstHeadEmpty", endfirsthead.empty)
1078            << write_attribute("headTopDL", endhead.topDL)
1079            << write_attribute("headBottomDL", endhead.bottomDL)
1080            << write_attribute("footTopDL", endfoot.topDL)
1081            << write_attribute("footBottomDL", endfoot.bottomDL)
1082            << write_attribute("lastFootTopDL", endlastfoot.topDL)
1083            << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1084            << write_attribute("lastFootEmpty", endlastfoot.empty)
1085            << ">\n";
1086         for (int j = 0; j < columns_; ++j) {
1087                 os << "<column"
1088                    << write_attribute("alignment", column_info[j].alignment)
1089                    << write_attribute("valignment", column_info[j].valignment)
1090                    << write_attribute("leftline", column_info[j].left_line)
1091                    << write_attribute("rightline", column_info[j].right_line)
1092                    << write_attribute("width", column_info[j].p_width.asString())
1093                    << write_attribute("special", column_info[j].align_special)
1094                    << ">\n";
1095         }
1096         for (int i = 0; i < rows_; ++i) {
1097                 os << "<row"
1098                    << write_attribute("topline", row_info[i].top_line)
1099                    << write_attribute("bottomline", row_info[i].bottom_line)
1100                    << write_attribute("endhead", row_info[i].endhead)
1101                    << write_attribute("endfirsthead", row_info[i].endfirsthead)
1102                    << write_attribute("endfoot", row_info[i].endfoot)
1103                    << write_attribute("endlastfoot", row_info[i].endlastfoot)
1104                    << write_attribute("newpage", row_info[i].newpage)
1105                    << ">\n";
1106                 for (int j = 0; j < columns_; ++j) {
1107                         os << "<cell"
1108                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1109                            << write_attribute("alignment", cell_info[i][j].alignment)
1110                            << write_attribute("valignment", cell_info[i][j].valignment)
1111                            << write_attribute("topline", cell_info[i][j].top_line)
1112                            << write_attribute("bottomline", cell_info[i][j].bottom_line)
1113                            << write_attribute("leftline", cell_info[i][j].left_line)
1114                            << write_attribute("rightline", cell_info[i][j].right_line)
1115                            << write_attribute("rotate", cell_info[i][j].rotate)
1116                            << write_attribute("usebox", cell_info[i][j].usebox)
1117                            << write_attribute("width", cell_info[i][j].p_width)
1118                            << write_attribute("special", cell_info[i][j].align_special)
1119                            << ">\n";
1120                         os << "\\begin_inset ";
1121                         cell_info[i][j].inset.write(buf, os);
1122                         os << "\n\\end_inset \n"
1123                            << "</cell>\n";
1124                 }
1125                 os << "</row>\n";
1126         }
1127         os << "</lyxtabular>\n";
1128 }
1129
1130
1131 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1132 {
1133         string line;
1134         istream & is = lex.getStream();
1135
1136         l_getline(is, line);
1137         if (!prefixIs(line, "<lyxtabular ")
1138                 && !prefixIs(line, "<LyXTabular ")) {
1139                 lyx::Assert(false);
1140                 return;
1141         }
1142
1143         int version;
1144         if (!getTokenValue(line, "version", version))
1145                 return;
1146         lyx::Assert(version >= 2);
1147         read(buf, is, lex, line, version);
1148 }
1149
1150 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1151 {
1152         // set header info
1153         while(hr > 0) {
1154                 row_info[--hr].endhead = true;
1155         }
1156         // set firstheader info
1157         if (fhr && (fhr < rows_)) {
1158                 if (row_info[fhr].endhead) {
1159                         while(fhr > 0) {
1160                                 row_info[--fhr].endfirsthead = true;
1161                                 row_info[fhr].endhead = false;
1162                         }
1163                 } else if (row_info[fhr - 1].endhead) {
1164                         endfirsthead.empty = true;
1165                 } else {
1166                         while((fhr > 0) && !row_info[--fhr].endhead) {
1167                                 row_info[fhr].endfirsthead = true;
1168                         }
1169                 }
1170         }
1171         // set footer info
1172         if (fr && (fr < rows_)) {
1173                 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1174                         while((fr > 0) && !row_info[--fr].endhead) {
1175                                 row_info[fr].endfoot = true;
1176                                 row_info[fr].endhead = false;
1177                         }
1178                 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1179                         while((fr > 0) && !row_info[--fr].endfirsthead) {
1180                                 row_info[fr].endfoot = true;
1181                                 row_info[fr].endfirsthead = false;
1182                         }
1183                 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1184                         while((fr > 0) && !row_info[--fr].endhead &&
1185                                   !row_info[fr].endfirsthead)
1186                         {
1187                                 row_info[fr].endfoot = true;
1188                         }
1189                 }
1190         }
1191         // set lastfooter info
1192         if (lfr && (lfr < rows_)) {
1193                 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1194                         while((lfr > 0) && !row_info[--lfr].endhead) {
1195                                 row_info[lfr].endlastfoot = true;
1196                                 row_info[lfr].endhead = false;
1197                         }
1198                 } else if (row_info[lfr].endfirsthead &&
1199                                    row_info[lfr - 1].endfirsthead)
1200                 {
1201                         while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1202                                 row_info[lfr].endlastfoot = true;
1203                                 row_info[lfr].endfirsthead = false;
1204                         }
1205                 } else if (row_info[lfr].endfoot
1206                            && row_info[lfr - 1].endfoot) {
1207                         while((lfr > 0) && !row_info[--lfr].endfoot) {
1208                                 row_info[lfr].endlastfoot = true;
1209                                 row_info[lfr].endfoot = false;
1210                         }
1211                 } else if (!row_info[fr - 1].endhead
1212                            && !row_info[fr - 1].endfirsthead &&
1213                                    !row_info[fr - 1].endfoot)
1214                 {
1215                         while((lfr > 0) &&
1216                                   !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1217                                   !row_info[lfr].endfoot)
1218                         {
1219                                 row_info[lfr].endlastfoot = true;
1220                         }
1221                 } else if (haveLTFoot()) {
1222                         endlastfoot.empty = true;
1223                 }
1224         }
1225 }
1226
1227 void LyXTabular::read(Buffer const * buf, istream & is,
1228                       LyXLex & lex, string const & l, int const version)
1229 {
1230         string line(l);
1231         int rows_arg;
1232         if (!getTokenValue(line, "rows", rows_arg))
1233                 return;
1234         int columns_arg;
1235         if (!getTokenValue(line, "columns", columns_arg))
1236                 return;
1237         Init(buf->params, rows_arg, columns_arg);
1238         l_getline(is, line);
1239         if (!prefixIs(line, "<features")) {
1240                 lyxerr << "Wrong tabular format (expected <features ...> got"
1241                        << line << ')' << endl;
1242                 return;
1243         }
1244         getTokenValue(line, "rotate", rotate);
1245         getTokenValue(line, "islongtable", is_long_tabular);
1246         // compatibility read for old longtable options. Now we can make any
1247         // row part of the header/footer type we want before it was strict
1248         // sequential from the first row down (as LaTeX does it!). So now when
1249         // we find a header/footer line we have to go up the rows and set it
1250         // on all preceding rows till the first or one with already a h/f option
1251         // set. If we find a firstheader on the same line as a header or a
1252         // lastfooter on the same line as a footer then this should be set empty.
1253         // (Jug 20011220)
1254         if (version < 3) {
1255                 int hrow;
1256                 int fhrow;
1257                 int frow;
1258                 int lfrow;
1259
1260                 getTokenValue(line, "endhead", hrow);
1261                 getTokenValue(line, "endfirsthead", fhrow);
1262                 getTokenValue(line, "endfoot", frow);
1263                 getTokenValue(line, "endlastfoot", lfrow);
1264                 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1265         } else {
1266            getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1267            getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1268            getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1269            getTokenValue(line, "headTopDL", endhead.topDL);
1270            getTokenValue(line, "headBottomDL", endhead.bottomDL);
1271            getTokenValue(line, "footTopDL", endfoot.topDL);
1272            getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1273            getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1274            getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1275            getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1276         }
1277         for (int j = 0; j < columns_; ++j) {
1278                 l_getline(is,line);
1279                 if (!prefixIs(line,"<column")) {
1280                         lyxerr << "Wrong tabular format (expected <column ...> got"
1281                                << line << ')' << endl;
1282                         return;
1283                 }
1284                 getTokenValue(line, "alignment", column_info[j].alignment);
1285                 getTokenValue(line, "valignment", column_info[j].valignment);
1286                 getTokenValue(line, "leftline", column_info[j].left_line);
1287                 getTokenValue(line, "rightline", column_info[j].right_line);
1288                 getTokenValue(line, "width", column_info[j].p_width);
1289                 getTokenValue(line, "special", column_info[j].align_special);
1290         }
1291
1292         for (int i = 0; i < rows_; ++i) {
1293                 l_getline(is, line);
1294                 if (!prefixIs(line, "<row")) {
1295                         lyxerr << "Wrong tabular format (expected <row ...> got"
1296                                << line << ')' << endl;
1297                         return;
1298                 }
1299                 getTokenValue(line, "topline", row_info[i].top_line);
1300                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1301                 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1302                 getTokenValue(line, "endhead", row_info[i].endhead);
1303                 getTokenValue(line, "endfoot", row_info[i].endfoot);
1304                 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1305                 getTokenValue(line, "newpage", row_info[i].newpage);
1306                 for (int j = 0; j < columns_; ++j) {
1307                         l_getline(is, line);
1308                         if (!prefixIs(line, "<cell")) {
1309                                 lyxerr << "Wrong tabular format (expected <cell ...> got"
1310                                        << line << ')' << endl;
1311                                 return;
1312                         }
1313                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1314                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1315                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1316                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1317                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1318                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1319                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1320                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1321                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1322                         getTokenValue(line, "width", cell_info[i][j].p_width);
1323                         getTokenValue(line, "special", cell_info[i][j].align_special);
1324                         l_getline(is, line);
1325                         if (prefixIs(line, "\\begin_inset")) {
1326                                 cell_info[i][j].inset.read(buf, lex);
1327                                 l_getline(is, line);
1328                         }
1329                         if (!prefixIs(line, "</cell>")) {
1330                                 lyxerr << "Wrong tabular format (expected </cell> got"
1331                                        << line << ')' << endl;
1332                                 return;
1333                         }
1334                 }
1335                 l_getline(is, line);
1336                 if (!prefixIs(line, "</row>")) {
1337                         lyxerr << "Wrong tabular format (expected </row> got"
1338                                << line << ')' << endl;
1339                         return;
1340                 }
1341         }
1342         while (!prefixIs(line, "</lyxtabular>")) {
1343                 l_getline(is, line);
1344         }
1345         set_row_column_number_info();
1346 }
1347
1348
1349 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1350 {
1351         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1352                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1353 }
1354
1355
1356 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1357 {
1358         int const row = row_of_cell(cell);
1359         int const column = column_of_cell(cell);
1360         return  &cell_info[row][column];
1361 }
1362
1363
1364 void LyXTabular::SetMultiColumn(Buffer * buffer, int cell, int number)
1365 {
1366         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1367         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1368         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1369         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1370         cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1371 #if 1
1372         for (int i = 1; i < number; ++i) {
1373                 cellinfo_of_cell(cell + i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1374                 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer,
1375                         cellinfo_of_cell(cell+i)->inset.paragraphs);
1376                 cellinfo_of_cell(cell + i)->inset.clear(false);
1377         }
1378 #else
1379         for (number--; number > 0; --number) {
1380                 cellinfo_of_cell(cell + number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1381         }
1382 #endif
1383         set_row_column_number_info();
1384 }
1385
1386
1387 int LyXTabular::cells_in_multicolumn(int cell) const
1388 {
1389         int const row = row_of_cell(cell);
1390         int column = column_of_cell(cell);
1391         int result = 1;
1392         ++column;
1393         while ((column < columns_) &&
1394                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1395         {
1396                 ++result;
1397                 ++column;
1398         }
1399         return result;
1400 }
1401
1402
1403 int LyXTabular::UnsetMultiColumn(int cell)
1404 {
1405         int const row = row_of_cell(cell);
1406         int column = column_of_cell(cell);
1407
1408         int result = 0;
1409
1410         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1411                 cell_info[row][column].multicolumn = CELL_NORMAL;
1412                 ++column;
1413                 while ((column < columns_) &&
1414                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1415                 {
1416                         cell_info[row][column].multicolumn = CELL_NORMAL;
1417                         ++column;
1418                         ++result;
1419                 }
1420         }
1421         set_row_column_number_info();
1422         return result;
1423 }
1424
1425
1426 void LyXTabular::SetLongTabular(bool what)
1427 {
1428         is_long_tabular = what;
1429 }
1430
1431
1432 bool LyXTabular::IsLongTabular() const
1433 {
1434         return is_long_tabular;
1435 }
1436
1437
1438 void LyXTabular::SetRotateTabular(bool flag)
1439 {
1440         rotate = flag;
1441 }
1442
1443
1444 bool LyXTabular::GetRotateTabular() const
1445 {
1446         return rotate;
1447 }
1448
1449
1450 void LyXTabular::SetRotateCell(int cell, bool flag)
1451 {
1452         cellinfo_of_cell(cell)->rotate = flag;
1453 }
1454
1455
1456 bool LyXTabular::GetRotateCell(int cell) const
1457 {
1458         return cellinfo_of_cell(cell)->rotate;
1459 }
1460
1461
1462 bool LyXTabular::NeedRotating() const
1463 {
1464         if (rotate)
1465                 return true;
1466         for (int i = 0; i < rows_; ++i) {
1467                 for (int j = 0; j < columns_; ++j) {
1468                         if (cell_info[i][j].rotate)
1469                                 return true;
1470                 }
1471         }
1472         return false;
1473 }
1474
1475
1476 bool LyXTabular::IsLastCell(int cell) const
1477 {
1478         if ((cell + 1) < numberofcells)
1479                 return false;
1480         return true;
1481 }
1482
1483
1484 int LyXTabular::GetCellAbove(int cell) const
1485 {
1486         if (row_of_cell(cell) > 0)
1487                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1488         return cell;
1489 }
1490
1491
1492 int LyXTabular::GetCellBelow(int cell) const
1493 {
1494         if (row_of_cell(cell) + 1 < rows_)
1495                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1496         return cell;
1497 }
1498
1499
1500 int LyXTabular::GetLastCellAbove(int cell) const
1501 {
1502         if (row_of_cell(cell) <= 0)
1503                 return cell;
1504         if (!IsMultiColumn(cell))
1505                 return GetCellAbove(cell);
1506         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1507 }
1508
1509
1510 int LyXTabular::GetLastCellBelow(int cell) const
1511 {
1512         if (row_of_cell(cell) + 1 >= rows_)
1513                 return cell;
1514         if (!IsMultiColumn(cell))
1515                 return GetCellBelow(cell);
1516         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1517 }
1518
1519
1520 int LyXTabular::GetCellNumber(int row, int column) const
1521 {
1522 #if 0
1523         if (column >= columns_)
1524                 column = columns_ - 1;
1525         else if (column < 0)
1526                 column = 0;
1527         if (row >= rows_)
1528                 row = rows_ - 1;
1529         else if (row < 0)
1530                 row = 0;
1531 #else
1532         lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1533 #endif
1534         return cell_info[row][column].cellno;
1535 }
1536
1537
1538 void LyXTabular::SetUsebox(int cell, BoxType type)
1539 {
1540         cellinfo_of_cell(cell)->usebox = type;
1541 }
1542
1543
1544 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1545 {
1546         if (column_info[column_of_cell(cell)].p_width.zero() &&
1547                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1548                 return BOX_NONE;
1549         if (cellinfo_of_cell(cell)->usebox > 1)
1550                 return cellinfo_of_cell(cell)->usebox;
1551         return UseParbox(cell);
1552 }
1553
1554
1555 ///
1556 //  This are functions used for the longtable support
1557 ///
1558 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1559 {
1560         if (first) {
1561                 endfirsthead = hd;
1562                 if (hd.set)
1563                         row_info[row].endfirsthead = flag;
1564         } else {
1565                 endhead = hd;
1566                 if (hd.set)
1567                         row_info[row].endhead = flag;
1568         }
1569 }
1570
1571
1572 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1573 {
1574         hd = endhead;
1575         hd.set = haveLTHead();
1576         return row_info[row].endhead;
1577 }
1578
1579
1580 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1581 {
1582         hd = endfirsthead;
1583         hd.set = haveLTFirstHead();
1584         return row_info[row].endfirsthead;
1585 }
1586
1587
1588 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1589 {
1590         if (last) {
1591                 endlastfoot = fd;
1592                 if (fd.set)
1593                         row_info[row].endlastfoot = flag;
1594         } else {
1595                 endfoot = fd;
1596                 if (fd.set)
1597                         row_info[row].endfoot = flag;
1598         }
1599 }
1600
1601
1602 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1603 {
1604         fd = endfoot;
1605         fd.set = haveLTFoot();
1606         return row_info[row].endfoot;
1607 }
1608
1609
1610 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1611 {
1612         fd = endlastfoot;
1613         fd.set = haveLTLastFoot();
1614         return row_info[row].endlastfoot;
1615 }
1616
1617
1618 void LyXTabular::SetLTNewPage(int row, bool what)
1619 {
1620         row_info[row].newpage = what;
1621 }
1622
1623
1624 bool LyXTabular::GetLTNewPage(int row) const
1625 {
1626         return row_info[row].newpage;
1627 }
1628
1629
1630 bool LyXTabular::haveLTHead() const
1631 {
1632         for(int i=0; i < rows_; ++i) {
1633                 if (row_info[i].endhead)
1634                         return true;
1635         }
1636         return false;
1637 }
1638
1639
1640 bool LyXTabular::haveLTFirstHead() const
1641 {
1642         if (endfirsthead.empty)
1643                 return false;
1644         for(int i=0; i < rows_; ++i) {
1645                 if (row_info[i].endfirsthead)
1646                         return true;
1647         }
1648         return false;
1649 }
1650
1651
1652 bool LyXTabular::haveLTFoot() const
1653 {
1654         for(int i=0; i < rows_; ++i) {
1655                 if (row_info[i].endfoot)
1656                         return true;
1657         }
1658         return false;
1659 }
1660
1661
1662 bool LyXTabular::haveLTLastFoot() const
1663 {
1664         if (endlastfoot.empty)
1665                 return false;
1666         for(int i=0; i < rows_; ++i) {
1667                 if (row_info[i].endlastfoot)
1668                         return true;
1669         }
1670         return false;
1671 }
1672
1673
1674 // end longtable support functions
1675
1676 bool LyXTabular::SetAscentOfRow(int row, int height)
1677 {
1678         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1679                 return false;
1680         row_info[row].ascent_of_row = height;
1681         return true;
1682 }
1683
1684
1685 bool LyXTabular::SetDescentOfRow(int row, int height)
1686 {
1687         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1688                 return false;
1689         row_info[row].descent_of_row = height;
1690         return true;
1691 }
1692
1693
1694 int LyXTabular::GetAscentOfRow(int row) const
1695 {
1696         if (row >= rows_)
1697                 return 0;
1698         return row_info[row].ascent_of_row;
1699 }
1700
1701
1702 int LyXTabular::GetDescentOfRow(int row) const
1703 {
1704         if (row >= rows_)
1705                 return 0;
1706         return row_info[row].descent_of_row;
1707 }
1708
1709
1710 int LyXTabular::GetHeightOfTabular() const
1711 {
1712         int height = 0;
1713
1714         for (int row = 0; row < rows_; ++row)
1715                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1716                         GetAdditionalHeight(row);
1717         return height;
1718 }
1719
1720
1721 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1722 {
1723         if ((row >= rows_) || (column >= columns_))
1724                 return false;
1725         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1726 }
1727
1728
1729 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1730 {
1731         if ((row < 0) || (row >= rows_))
1732                 return 0;
1733
1734         int const fcell = GetFirstCellInRow(row);
1735         int const n = NumberOfCellsInRow(fcell) + fcell;
1736         int tmp = 0;
1737
1738         for (int i = fcell; i < n; ++i) {
1739                 if (TopLine(i))
1740                         ++tmp;
1741         }
1742         if (tmp == (n - fcell)) {
1743                 os << "\\hline ";
1744         } else if (tmp) {
1745                 for (int i = fcell; i < n; ++i) {
1746                         if (TopLine(i)) {
1747                                 os << "\\cline{"
1748                                    << column_of_cell(i) + 1
1749                                    << '-'
1750                                    << right_column_of_cell(i) + 1
1751                                    << "} ";
1752                         }
1753                 }
1754         } else {
1755                 return 0;
1756         }
1757         os << "\n";
1758         return 1;
1759 }
1760
1761
1762 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1763 {
1764         if ((row < 0) || (row >= rows_))
1765                 return 0;
1766
1767         int const fcell = GetFirstCellInRow(row);
1768         int const n = NumberOfCellsInRow(fcell) + fcell;
1769         int tmp = 0;
1770
1771         for (int i = fcell; i < n; ++i) {
1772                 if (BottomLine(i))
1773                         ++tmp;
1774         }
1775         if (tmp == (n - fcell)) {
1776                 os << "\\hline";
1777         } else if (tmp) {
1778                 for (int i = fcell; i < n; ++i) {
1779                         if (BottomLine(i)) {
1780                                 os << "\\cline{"
1781                                    << column_of_cell(i) + 1
1782                                    << '-'
1783                                    << right_column_of_cell(i) + 1
1784                                    << "} ";
1785                         }
1786                 }
1787         } else {
1788                 return 0;
1789         }
1790         os << "\n";
1791         return 1;
1792 }
1793
1794
1795 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1796 {
1797         int ret = 0;
1798
1799         if (GetRotateCell(cell)) {
1800                 os << "\\begin{sideways}\n";
1801                 ++ret;
1802         }
1803         if (IsMultiColumn(cell)) {
1804                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1805                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1806                         os << cellinfo_of_cell(cell)->align_special << "}{";
1807                 } else {
1808                         if (LeftLine(cell) &&
1809                                 (IsFirstCellInRow(cell) ||
1810                                  (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1811                                   !RightLine(cell-1, true))))
1812                         {
1813                                 os << '|';
1814                         }
1815                         if (!GetPWidth(cell).zero()) {
1816                                 switch (GetVAlignment(cell)) {
1817                                 case LYX_VALIGN_TOP:
1818                                         os << 'p';
1819                                         break;
1820                                 case LYX_VALIGN_CENTER:
1821                                         os << 'm';
1822                                         break;
1823                                 case LYX_VALIGN_BOTTOM:
1824                                         os << 'b';
1825                                         break;
1826                                 }
1827                                 os << '{'
1828                                    << GetPWidth(cell).asLatexString()
1829                                    << '}';
1830                         } else {
1831                                 switch (GetAlignment(cell)) {
1832                                 case LYX_ALIGN_LEFT:
1833                                         os << 'l';
1834                                         break;
1835                                 case LYX_ALIGN_RIGHT:
1836                                         os << 'r';
1837                                         break;
1838                                 default:
1839                                         os << 'c';
1840                                         break;
1841                                 }
1842                         }
1843                         if (RightLine(cell))
1844                                 os << '|';
1845                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1846                                 LeftLine(cell+1))
1847                                 os << '|';
1848                         os << "}{";
1849                 }
1850         }
1851         if (GetUsebox(cell) == BOX_PARBOX) {
1852                 os << "\\parbox[";
1853                 switch (GetVAlignment(cell)) {
1854                 case LYX_VALIGN_TOP:
1855                         os << 't';
1856                         break;
1857                 case LYX_VALIGN_CENTER:
1858                         os << 'c';
1859                         break;
1860                 case LYX_VALIGN_BOTTOM:
1861                         os << 'b';
1862                         break;
1863                 }
1864                 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
1865         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
1866                 os << "\\begin{minipage}[";
1867                 switch (GetVAlignment(cell)) {
1868                 case LYX_VALIGN_TOP:
1869                         os << 't';
1870                         break;
1871                 case LYX_VALIGN_CENTER:
1872                         os << 'm';
1873                         break;
1874                 case LYX_VALIGN_BOTTOM:
1875                         os << 'b';
1876                         break;
1877                 }
1878                 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
1879                 ++ret;
1880         }
1881         return ret;
1882 }
1883
1884
1885 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1886 {
1887         int ret = 0;
1888
1889         // usual cells
1890         if (GetUsebox(cell) == BOX_PARBOX)
1891                 os << '}';
1892         else if (GetUsebox(cell) == BOX_MINIPAGE) {
1893                 os << "%\n\\end{minipage}";
1894                 ret += 2;
1895         }
1896         if (IsMultiColumn(cell)) {
1897                 os << '}';
1898         }
1899         if (GetRotateCell(cell)) {
1900                 os << "%\n\\end{sideways}";
1901                 ++ret;
1902         }
1903         return ret;
1904 }
1905
1906
1907 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1908                                          LatexRunParams const & runparams) const
1909 {
1910         if (!is_long_tabular)
1911                 return 0;
1912
1913         int ret = 0;
1914         // output header info
1915         if (haveLTHead()) {
1916                 if (endhead.topDL) {
1917                         os << "\\hline\n";
1918                         ++ret;
1919                 }
1920                 for (int i = 0; i < rows_; ++i) {
1921                         if (row_info[i].endhead) {
1922                                 ret += TeXRow(os, i, buf, runparams);
1923                         }
1924                 }
1925                 if (endhead.bottomDL) {
1926                         os << "\\hline\n";
1927                         ++ret;
1928                 }
1929                 os << "\\endhead\n";
1930                 ++ret;
1931                 if (endfirsthead.empty) {
1932                         os << "\\endfirsthead\n";
1933                         ++ret;
1934                 }
1935         }
1936         // output firstheader info
1937         if (haveLTFirstHead()) {
1938                 if (endfirsthead.topDL) {
1939                         os << "\\hline\n";
1940                         ++ret;
1941                 }
1942                 for (int i = 0; i < rows_; ++i) {
1943                         if (row_info[i].endfirsthead) {
1944                                 ret += TeXRow(os, i, buf, runparams);
1945                         }
1946                 }
1947                 if (endfirsthead.bottomDL) {
1948                         os << "\\hline\n";
1949                         ++ret;
1950                 }
1951                 os << "\\endfirsthead\n";
1952                 ++ret;
1953         }
1954         // output footer info
1955         if (haveLTFoot()) {
1956                 if (endfoot.topDL) {
1957                         os << "\\hline\n";
1958                         ++ret;
1959                 }
1960                 for (int i = 0; i < rows_; ++i) {
1961                         if (row_info[i].endfoot) {
1962                                 ret += TeXRow(os, i, buf, runparams);
1963                         }
1964                 }
1965                 if (endfoot.bottomDL) {
1966                         os << "\\hline\n";
1967                         ++ret;
1968                 }
1969                 os << "\\endfoot\n";
1970                 ++ret;
1971                 if (endlastfoot.empty) {
1972                         os << "\\endlastfoot\n";
1973                         ++ret;
1974                 }
1975         }
1976         // output lastfooter info
1977         if (haveLTLastFoot()) {
1978                 if (endlastfoot.topDL) {
1979                         os << "\\hline\n";
1980                         ++ret;
1981                 }
1982                 for (int i = 0; i < rows_; ++i) {
1983                         if (row_info[i].endlastfoot) {
1984                                 ret += TeXRow(os, i, buf, runparams);
1985                         }
1986                 }
1987                 if (endlastfoot.bottomDL) {
1988                         os << "\\hline\n";
1989                         ++ret;
1990                 }
1991                 os << "\\endlastfoot\n";
1992                 ++ret;
1993         }
1994         return ret;
1995 }
1996
1997
1998 bool LyXTabular::isValidRow(int const row) const
1999 {
2000         if (!is_long_tabular)
2001                 return true;
2002         return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2003                         !row_info[row].endfoot && !row_info[row].endlastfoot);
2004 }
2005
2006
2007 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2008                        LatexRunParams const & runparams) const
2009 {
2010         int ret = 0;
2011         int cell = GetCellNumber(i, 0);
2012
2013         ret += TeXTopHLine(os, i);
2014         for (int j = 0; j < columns_; ++j) {
2015                 if (IsPartOfMultiColumn(i,j))
2016                         continue;
2017                 ret += TeXCellPreamble(os, cell);
2018                 InsetText * inset = GetCellInset(cell);
2019
2020                 bool rtl = inset->paragraphs.begin()->isRightToLeftPar(buf->params) &&
2021                         !inset->paragraphs.begin()->empty() && GetPWidth(cell).zero();
2022
2023                 if (rtl)
2024                         os << "\\R{";
2025                 ret += inset->latex(buf, os, runparams);
2026                 if (rtl)
2027                         os << '}';
2028
2029                 ret += TeXCellPostamble(os, cell);
2030                 if (!IsLastCellInRow(cell)) { // not last cell in row
2031                         os << "&\n";
2032                         ++ret;
2033                 }
2034                 ++cell;
2035         }
2036         os << "\\tabularnewline\n";
2037         ++ret;
2038         ret += TeXBottomHLine(os, i);
2039         return ret;
2040 }
2041
2042
2043 int LyXTabular::latex(Buffer const * buf, ostream & os,
2044                       LatexRunParams const & runparams) const
2045 {
2046         int ret = 0;
2047
2048         //+---------------------------------------------------------------------
2049         //+                      first the opening preamble                    +
2050         //+---------------------------------------------------------------------
2051
2052         if (rotate) {
2053                 os << "\\begin{sideways}\n";
2054                 ++ret;
2055         }
2056         if (is_long_tabular)
2057                 os << "\\begin{longtable}{";
2058         else
2059                 os << "\\begin{tabular}{";
2060         for (int i = 0; i < columns_; ++i) {
2061                 if (!column_info[i].align_special.empty()) {
2062                         os << column_info[i].align_special;
2063                 } else {
2064                         if (column_info[i].left_line)
2065                                 os << '|';
2066                         if (!column_info[i].p_width.zero()) {
2067                                 switch (column_info[i].alignment) {
2068                                 case LYX_ALIGN_LEFT:
2069                                         os << ">{\\raggedright}";
2070                                         break;
2071                                 case LYX_ALIGN_RIGHT:
2072                                         os << ">{\\raggedleft}";
2073                                         break;
2074                                 case LYX_ALIGN_CENTER:
2075                                         os << ">{\\centering}";
2076                                         break;
2077                                 case LYX_ALIGN_NONE:
2078                                 case LYX_ALIGN_BLOCK:
2079                                 case LYX_ALIGN_LAYOUT:
2080                                 case LYX_ALIGN_SPECIAL:
2081                                         break;
2082                                 }
2083
2084                                 switch (column_info[i].valignment) {
2085                                 case LYX_VALIGN_TOP:
2086                                         os << 'p';
2087                                         break;
2088                                 case LYX_VALIGN_CENTER:
2089                                         os << 'm';
2090                                         break;
2091                                 case LYX_VALIGN_BOTTOM:
2092                                         os << 'b';
2093                                         break;
2094                         }
2095                                 os << '{'
2096                                    << column_info[i].p_width.asLatexString()
2097                                    << '}';
2098                         } else {
2099                                 switch (column_info[i].alignment) {
2100                                 case LYX_ALIGN_LEFT:
2101                                         os << 'l';
2102                                         break;
2103                                 case LYX_ALIGN_RIGHT:
2104                                         os << 'r';
2105                                         break;
2106                                 default:
2107                                         os << 'c';
2108                                         break;
2109                                 }
2110                         }
2111                         if (column_info[i].right_line)
2112                                 os << '|';
2113                 }
2114         }
2115         os << "}\n";
2116         ++ret;
2117
2118         ret += TeXLongtableHeaderFooter(os, buf, runparams);
2119
2120         //+---------------------------------------------------------------------
2121         //+                      the single row and columns (cells)            +
2122         //+---------------------------------------------------------------------
2123
2124         for (int i = 0; i < rows_; ++i) {
2125                 if (isValidRow(i)) {
2126                         ret += TeXRow(os, i, buf, runparams);
2127                         if (is_long_tabular && row_info[i].newpage) {
2128                                 os << "\\newpage\n";
2129                                 ++ret;
2130                         }
2131                 }
2132         }
2133
2134         //+---------------------------------------------------------------------
2135         //+                      the closing of the tabular                    +
2136         //+---------------------------------------------------------------------
2137
2138         if (is_long_tabular)
2139                 os << "\\end{longtable}";
2140         else
2141                 os << "\\end{tabular}";
2142         if (rotate) {
2143                 os << "\n\\end{sideways}";
2144                 ++ret;
2145         }
2146
2147         return ret;
2148 }
2149
2150
2151 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2152 {
2153         int ret = 0;
2154         int cell = GetFirstCellInRow(row);
2155
2156         os << "<row>\n";
2157         for (int j = 0; j < columns_; ++j) {
2158                 if (IsPartOfMultiColumn(row, j))
2159                         continue;
2160
2161                 os << "<entry align=\"";
2162                 switch (GetAlignment(cell)) {
2163                 case LYX_ALIGN_LEFT:
2164                         os << "left";
2165                         break;
2166                 case LYX_ALIGN_RIGHT:
2167                         os << "right";
2168                         break;
2169                 default:
2170                         os << "center";
2171                         break;
2172                 }
2173
2174                 os << "\" valign=\"";
2175                 switch (GetVAlignment(cell)) {
2176                 case LYX_VALIGN_TOP:
2177                         os << "top";
2178                         break;
2179                 case LYX_VALIGN_BOTTOM:
2180                         os << "bottom";
2181                         break;
2182                 case LYX_VALIGN_CENTER:
2183                         os << "middle";
2184                 }
2185                 os << '"';
2186
2187                 if (IsMultiColumn(cell)) {
2188                         os << " namest=\"col" << j << "\" ";
2189                         os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2190                 }
2191
2192                 os << '>';
2193                 ret += GetCellInset(cell)->docbook(buf, os, true);
2194                 os << "</entry>\n";
2195                 ++cell;
2196         }
2197         os << "</row>\n";
2198         return ret;
2199 }
2200
2201
2202 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2203                         bool /*mixcont*/) const
2204 {
2205         int ret = 0;
2206
2207         //+---------------------------------------------------------------------
2208         //+                      first the opening preamble                    +
2209         //+---------------------------------------------------------------------
2210
2211         os << "<tgroup cols=\"" << columns_
2212            << "\" colsep=\"1\" rowsep=\"1\">\n";
2213
2214         for (int i = 0; i < columns_; ++i) {
2215                 os << "<colspec colname=\"col" << i << "\" align=\"";
2216                 switch (column_info[i].alignment) {
2217                 case LYX_ALIGN_LEFT:
2218                         os << "left";
2219                         break;
2220                 case LYX_ALIGN_RIGHT:
2221                         os << "right";
2222                         break;
2223                 default:
2224                         os << "center";
2225                         break;
2226                 }
2227                 os << "\">\n";
2228                 ++ret;
2229         }
2230
2231         //+---------------------------------------------------------------------
2232         //+                      Long Tabular case                             +
2233         //+---------------------------------------------------------------------
2234
2235         // output header info
2236         if (haveLTHead() || haveLTFirstHead()) {
2237                 os << "<thead>\n";
2238                 ++ret;
2239                 for (int i = 0; i < rows_; ++i) {
2240                         if (row_info[i].endhead || row_info[i].endfirsthead) {
2241                                 ret += docbookRow(buf, os, i);
2242                         }
2243                 }
2244                 os << "</thead>\n";
2245                 ++ret;
2246         }
2247         // output footer info
2248         if (haveLTFoot() || haveLTLastFoot()) {
2249                 os << "<tfoot>\n";
2250                 ++ret;
2251                 for (int i = 0; i < rows_; ++i) {
2252                         if (row_info[i].endfoot || row_info[i].endlastfoot) {
2253                                 ret += docbookRow(buf, os, i);
2254                         }
2255                 }
2256                 os << "</tfoot>\n";
2257                 ++ret;
2258         }
2259
2260         //+---------------------------------------------------------------------
2261         //+                      the single row and columns (cells)            +
2262         //+---------------------------------------------------------------------
2263
2264         os << "<tbody>\n";
2265         ++ret;
2266         for (int i = 0; i < rows_; ++i) {
2267                 if (isValidRow(i)) {
2268                         ret += docbookRow(buf, os, i);
2269                 }
2270         }
2271         os << "</tbody>\n";
2272         ++ret;
2273         //+---------------------------------------------------------------------
2274         //+                      the closing of the tabular                    +
2275         //+---------------------------------------------------------------------
2276
2277         os << "</tgroup>";
2278         ++ret;
2279
2280         return ret;
2281 }
2282
2283 //--
2284 // ASCII export function and helpers
2285 //--
2286 int LyXTabular::asciiTopHLine(ostream & os, int row,
2287                               vector<unsigned int> const & clen) const
2288 {
2289         int const fcell = GetFirstCellInRow(row);
2290         int const n = NumberOfCellsInRow(fcell) + fcell;
2291         int tmp = 0;
2292
2293         for (int i = fcell; i < n; ++i) {
2294                 if (TopLine(i)) {
2295                         ++tmp;
2296                         break;
2297                 }
2298         }
2299         if (!tmp)
2300                 return 0;
2301
2302         unsigned char ch;
2303         for (int i = fcell; i < n; ++i) {
2304                 if (TopLine(i)) {
2305                         if (LeftLine(i))
2306                                 os << "+-";
2307                         else
2308                                 os << "--";
2309                         ch = '-';
2310                 } else {
2311                         os << "  ";
2312                         ch = ' ';
2313                 }
2314                 int column = column_of_cell(i);
2315                 int len = clen[column];
2316                 while (IsPartOfMultiColumn(row, ++column))
2317                         len += clen[column] + 4;
2318                 os << string(len, ch);
2319                 if (TopLine(i)) {
2320                         if (RightLine(i))
2321                                 os << "-+";
2322                         else
2323                                 os << "--";
2324                 } else {
2325                         os << "  ";
2326                 }
2327         }
2328         os << endl;
2329         return 1;
2330 }
2331
2332
2333 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2334                                  vector<unsigned int> const & clen) const
2335 {
2336         int const fcell = GetFirstCellInRow(row);
2337         int const n = NumberOfCellsInRow(fcell) + fcell;
2338         int tmp = 0;
2339
2340         for (int i = fcell; i < n; ++i) {
2341                 if (BottomLine(i)) {
2342                         ++tmp;
2343                         break;
2344                 }
2345         }
2346         if (!tmp)
2347                 return 0;
2348
2349         unsigned char ch;
2350         for (int i = fcell; i < n; ++i) {
2351                 if (BottomLine(i)) {
2352                         if (LeftLine(i))
2353                                 os << "+-";
2354                         else
2355                                 os << "--";
2356                         ch = '-';
2357                 } else {
2358                         os << "  ";
2359                         ch = ' ';
2360                 }
2361                 int column = column_of_cell(i);
2362                 int len = clen[column];
2363                 while (IsPartOfMultiColumn(row, ++column))
2364                         len += clen[column] + 4;
2365                 os << string(len, ch);
2366                 if (BottomLine(i)) {
2367                         if (RightLine(i))
2368                                 os << "-+";
2369                         else
2370                                 os << "--";
2371                 } else {
2372                         os << "  ";
2373                 }
2374         }
2375         os << endl;
2376         return 1;
2377 }
2378
2379
2380 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2381                                int cell, int row, int column,
2382                                vector<unsigned int> const & clen,
2383                                bool onlydata) const
2384 {
2385         ostringstream sstr;
2386         int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2387
2388         if (onlydata) {
2389                 os << sstr.str();
2390                 return ret;
2391         }
2392
2393         if (LeftLine(cell))
2394                 os << "| ";
2395         else
2396                 os << "  ";
2397
2398         unsigned int len1 = sstr.str().length();
2399         unsigned int len2 = clen[column];
2400         while (IsPartOfMultiColumn(row, ++column))
2401                 len2 += clen[column] + 4;
2402         len2 -= len1;
2403
2404         switch (GetAlignment(cell)) {
2405         default:
2406         case LYX_ALIGN_LEFT:
2407                 len1 = 0;
2408                 break;
2409         case LYX_ALIGN_RIGHT:
2410                 len1 = len2;
2411                 len2 = 0;
2412                 break;
2413         case LYX_ALIGN_CENTER:
2414                 len1 = len2 / 2;
2415                 len2 -= len1;
2416                 break;
2417         }
2418
2419         os << string(len1, ' ')
2420            << sstr.str()
2421            << string(len2, ' ');
2422         if (RightLine(cell))
2423                 os << " |";
2424         else
2425                 os << "  ";
2426
2427         return ret;
2428 }
2429
2430
2431 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2432                                           bool onlydata, unsigned char delim) const
2433 {
2434         int ret = 0;
2435
2436         //+---------------------------------------------------------------------
2437         //+           first calculate the width of the single columns          +
2438         //+---------------------------------------------------------------------
2439         vector<unsigned int> clen(columns_);
2440
2441         if (!onlydata) {
2442                 // first all non (real) multicolumn cells!
2443                 for (int j = 0; j < columns_; ++j) {
2444                         clen[j] = 0;
2445                         for (int i = 0; i < rows_; ++i) {
2446                                 int cell = GetCellNumber(i, j);
2447                                 if (IsMultiColumn(cell, true))
2448                                         continue;
2449                                 ostringstream sstr;
2450                                 GetCellInset(cell)->ascii(buf, sstr, 0);
2451                                 if (clen[j] < sstr.str().length())
2452                                         clen[j] = sstr.str().length();
2453                         }
2454                 }
2455                 // then all (real) multicolumn cells!
2456                 for (int j = 0; j < columns_; ++j) {
2457                         for (int i = 0; i < rows_; ++i) {
2458                                 int cell = GetCellNumber(i, j);
2459                                 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2460                                         continue;
2461                                 ostringstream sstr;
2462                                 GetCellInset(cell)->ascii(buf, sstr, 0);
2463                                 int len = int(sstr.str().length());
2464                                 int const n = cells_in_multicolumn(cell);
2465                                 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2466                                         len -= clen[k];
2467                                 if (len > int(clen[j + n - 1]))
2468                                         clen[j + n - 1] = len;
2469                         }
2470                 }
2471         }
2472         int cell = 0;
2473         for (int i = 0; i < rows_; ++i) {
2474                 if (!onlydata) {
2475                         if (asciiTopHLine(os, i, clen)) {
2476                                 for (int j = 0; j < depth; ++j)
2477                                         os << "  ";
2478                         }
2479                 }
2480                 for (int j = 0; j < columns_; ++j) {
2481                         if (IsPartOfMultiColumn(i,j))
2482                                 continue;
2483                         if (onlydata && j > 0)
2484                                 os << delim;
2485                         ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2486                         ++cell;
2487                 }
2488                 os << endl;
2489                 if (!onlydata) {
2490                         for (int j = 0; j < depth; ++j)
2491                                 os << "  ";
2492                         if (asciiBottomHLine(os, i, clen)) {
2493                                 for (int j = 0; j < depth; ++j)
2494                                         os << "  ";
2495                         }
2496                 }
2497         }
2498         return ret;
2499 }
2500 //--
2501 // end ascii export
2502 //--
2503
2504
2505 InsetText * LyXTabular::GetCellInset(int cell) const
2506 {
2507         cur_cell = cell;
2508         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2509 }
2510
2511
2512 InsetText * LyXTabular::GetCellInset(int row, int column) const
2513 {
2514         cur_cell = GetCellNumber(row, column);
2515         return & cell_info[row][column].inset;
2516 }
2517
2518
2519 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2520 {
2521         // is this inset part of the tabular?
2522         if (!inset || inset->owner() != owner_) {
2523                 lyxerr[Debug::INSETTEXT]
2524                         << "this is not a cell of the tabular!" << endl;
2525                 return -1;
2526         }
2527
2528         const int save_cur_cell = cur_cell;
2529         int cell = cur_cell;
2530         if (GetCellInset(cell) != inset) {
2531                 cell = maybe_cell;
2532                 if (cell == -1 || GetCellInset(cell) != inset) {
2533                         cell = -1;
2534                 }
2535         }
2536
2537         if (cell == -1) {
2538                 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2539                         if (GetCellInset(cell) == inset)
2540                                 break;
2541                 }
2542                 lyxerr[Debug::INSETTEXT]
2543                          << "LyXTabular::GetCellFromInset: "
2544                                     << "cell=" << cell
2545                                     << ", cur_cell=" << save_cur_cell
2546                                     << ", maybe_cell=" << maybe_cell
2547                                     << endl;
2548                 // We should have found a cell at this point
2549                 if (cell == -1) {
2550                         lyxerr << "LyXTabular::GetCellFromInset: "
2551                                << "Cell not found!" << endl;
2552                 }
2553         }
2554
2555         return cell;
2556 }
2557
2558
2559 void LyXTabular::Validate(LaTeXFeatures & features) const
2560 {
2561         features.require("NeedTabularnewline");
2562         if (IsLongTabular())
2563                 features.require("longtable");
2564         if (NeedRotating())
2565                 features.require("rotating");
2566         for (int cell = 0; cell < numberofcells; ++cell) {
2567                 if ( (GetVAlignment(cell) != LYX_VALIGN_TOP) ||
2568                      ( !(GetPWidth(cell).zero())&&!(IsMultiColumn(cell)) )
2569                    )
2570                         features.require("array");
2571                 GetCellInset(cell)->validate(features);
2572         }
2573 }
2574
2575
2576 vector<string> const LyXTabular::getLabelList() const
2577 {
2578         vector<string> label_list;
2579         for (int i = 0; i < rows_; ++i)
2580                 for (int j = 0; j < columns_; ++j) {
2581                         vector<string> const l =
2582                                 GetCellInset(i, j)->getLabelList();
2583                         label_list.insert(label_list.end(),
2584                                           l.begin(), l.end());
2585                 }
2586         return label_list;
2587 }
2588
2589
2590 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2591 {
2592         ParagraphList const & parlist = GetCellInset(cell)->paragraphs;
2593         ParagraphList::iterator cit = parlist.begin();
2594         ParagraphList::iterator end = parlist.end();
2595
2596         for (; cit != end; ++cit) {
2597                 for (int i = 0; i < cit->size(); ++i) {
2598                         if (cit->isNewline(i))
2599                                 return BOX_PARBOX;
2600                 }
2601         }
2602         return BOX_NONE;
2603 }