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