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