]> git.lyx.org Git - lyx.git/blob - src/tabular.C
the convert patch
[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 // returns 1 if a complete update is necessary, otherwise 0
821 void LyXTabular::setWidthOfCell(idx_type cell, int new_width)
822 {
823         row_type const row = row_of_cell(cell);
824         col_type const column1 = column_of_cell(cell);
825         bool tmp = false;
826         int width = 0;
827         int add_width = 0;
828
829         if (rightLine(cell_info[row][column1].cellno, true) &&
830                 column1 < columns_ - 1 &&
831                 leftLine(cell_info[row][column1+1].cellno, true))
832         {
833                 add_width = WIDTH_OF_LINE;
834         }
835
836         if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
837                 return;
838
839         if (isMultiColumnReal(cell)) {
840                 tmp = setWidthOfMulticolCell(cell, new_width);
841         } else {
842                 width = new_width + 2 * WIDTH_OF_LINE + add_width;
843                 cell_info[row][column1].width_of_cell = width;
844                 tmp = calculate_width_of_column_NMC(column1);
845                 if (tmp)
846                         recalculateMulticolumnsOfColumn(column1);
847         }
848         if (tmp) {
849                 for (col_type i = 0; i < columns_; ++i)
850                         calculate_width_of_column(i);
851                 calculate_width_of_tabular();
852         }
853 }
854
855
856 void LyXTabular::setAlignment(idx_type cell, LyXAlignment align,
857                               bool onlycolumn)
858 {
859         if (!isMultiColumn(cell) || onlycolumn)
860                 column_info[column_of_cell(cell)].alignment = align;
861         if (!onlycolumn)
862                 cellinfo_of_cell(cell).alignment = align;
863 }
864
865
866 void LyXTabular::setVAlignment(idx_type cell, VAlignment align,
867                                bool onlycolumn)
868 {
869         if (!isMultiColumn(cell) || onlycolumn)
870                 column_info[column_of_cell(cell)].valignment = align;
871         if (!onlycolumn)
872                 cellinfo_of_cell(cell).valignment = align;
873 }
874
875
876 void LyXTabular::setColumnPWidth(idx_type cell, LyXLength const & width)
877 {
878         col_type const j = column_of_cell(cell);
879
880         column_info[j].p_width = width;
881         for (row_type i = 0; i < rows_; ++i) {
882                 idx_type const cell = getCellNumber(i, j);
883                 // because of multicolumns
884                 getCellInset(cell)->setAutoBreakRows(!getPWidth(cell).zero());
885         }
886 }
887
888
889 bool LyXTabular::setMColumnPWidth(idx_type cell, LyXLength const & width)
890 {
891         if (!isMultiColumn(cell))
892                 return false;
893
894         cellinfo_of_cell(cell).p_width = width;
895         getCellInset(cell)->setAutoBreakRows(!width.zero());
896         return true;
897 }
898
899
900 void LyXTabular::setAlignSpecial(idx_type cell, string const & special,
901                                  LyXTabular::Feature what)
902 {
903         if (what == SET_SPECIAL_MULTI)
904                 cellinfo_of_cell(cell).align_special = special;
905         else
906                 column_info[column_of_cell(cell)].align_special = special;
907 }
908
909
910 void LyXTabular::setAllLines(idx_type cell, bool line)
911 {
912         setTopLine(cell, line);
913         setBottomLine(cell, line);
914         setRightLine(cell, line);
915         setLeftLine(cell, line);
916 }
917
918
919 void LyXTabular::setTopLine(idx_type cell, bool line, bool onlycolumn)
920 {
921         row_type const row = row_of_cell(cell);
922         if (onlycolumn || !isMultiColumn(cell))
923                 row_info[row].top_line = line;
924         else
925                 cellinfo_of_cell(cell).top_line = line;
926 }
927
928
929 void LyXTabular::setBottomLine(idx_type cell, bool line, bool onlycolumn)
930 {
931         if (onlycolumn || !isMultiColumn(cell))
932                 row_info[row_of_cell(cell)].bottom_line = line;
933         else
934                 cellinfo_of_cell(cell).bottom_line = line;
935 }
936
937
938 void LyXTabular::setLeftLine(idx_type cell, bool line, bool onlycolumn)
939 {
940         if (onlycolumn || !isMultiColumn(cell))
941                 column_info[column_of_cell(cell)].left_line = line;
942         else
943                 cellinfo_of_cell(cell).left_line = line;
944 }
945
946
947 void LyXTabular::setRightLine(idx_type cell, bool line, bool onlycolumn)
948 {
949         if (onlycolumn || !isMultiColumn(cell))
950                 column_info[right_column_of_cell(cell)].right_line = line;
951         else
952                 cellinfo_of_cell(cell).right_line = line;
953 }
954
955
956 LyXAlignment LyXTabular::getAlignment(idx_type cell, bool onlycolumn) const
957 {
958         if (!onlycolumn && isMultiColumn(cell))
959                 return cellinfo_of_cell(cell).alignment;
960         return column_info[column_of_cell(cell)].alignment;
961 }
962
963
964 LyXTabular::VAlignment
965 LyXTabular::getVAlignment(idx_type cell, bool onlycolumn) const
966 {
967         if (!onlycolumn && isMultiColumn(cell))
968                 return cellinfo_of_cell(cell).valignment;
969         return column_info[column_of_cell(cell)].valignment;
970 }
971
972
973 LyXLength const LyXTabular::getPWidth(idx_type cell) const
974 {
975         if (isMultiColumn(cell))
976                 return cellinfo_of_cell(cell).p_width;
977         return column_info[column_of_cell(cell)].p_width;
978 }
979
980
981 LyXLength const LyXTabular::getColumnPWidth(idx_type cell) const
982 {
983         return column_info[column_of_cell(cell)].p_width;
984 }
985
986
987 LyXLength const LyXTabular::getMColumnPWidth(idx_type cell) const
988 {
989         if (isMultiColumn(cell))
990                 return cellinfo_of_cell(cell).p_width;
991         return LyXLength();
992 }
993
994
995 string const LyXTabular::getAlignSpecial(idx_type cell, int what) const
996 {
997         if (what == SET_SPECIAL_MULTI)
998                 return cellinfo_of_cell(cell).align_special;
999         return column_info[column_of_cell(cell)].align_special;
1000 }
1001
1002
1003 int LyXTabular::getWidthOfCell(idx_type cell) const
1004 {
1005         row_type const row = row_of_cell(cell);
1006         col_type const column1 = column_of_cell(cell);
1007         col_type const column2 = right_column_of_cell(cell);
1008         int result = 0;
1009         for (col_type i = column1; i <= column2; ++i)
1010                 result += cell_info[row][i].width_of_cell;
1011         return result;
1012 }
1013
1014
1015 int LyXTabular::getBeginningOfTextInCell(idx_type cell) const
1016 {
1017         int x = 0;
1018
1019         switch (getAlignment(cell)) {
1020         case LYX_ALIGN_CENTER:
1021                 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1022                 break;
1023         case LYX_ALIGN_RIGHT:
1024                 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1025                 // + getAdditionalWidth(cell);
1026                 break;
1027         default:
1028                 // LYX_ALIGN_LEFT: nothing :-)
1029                 break;
1030         }
1031
1032         // the LaTeX Way :-(
1033         x += WIDTH_OF_LINE;
1034         return x;
1035 }
1036
1037
1038 bool LyXTabular::isFirstCellInRow(idx_type cell) const
1039 {
1040         return column_of_cell(cell) == 0;
1041 }
1042
1043
1044 LyXTabular::idx_type LyXTabular::getFirstCellInRow(row_type row) const
1045 {
1046         if (row > rows_ - 1)
1047                 row = rows_ - 1;
1048         return cell_info[row][0].cellno;
1049 }
1050
1051
1052 bool LyXTabular::isLastCellInRow(idx_type cell) const
1053 {
1054         return right_column_of_cell(cell) == columns_ - 1;
1055 }
1056
1057
1058 LyXTabular::idx_type LyXTabular::getLastCellInRow(row_type row) const
1059 {
1060         if (row > rows_ - 1)
1061                 row = rows_ - 1;
1062         return cell_info[row][columns_-1].cellno;
1063 }
1064
1065
1066 void LyXTabular::calculate_width_of_column(col_type column)
1067 {
1068         int maximum = 0;
1069         for (row_type i = 0; i < rows_; ++i)
1070                 maximum = max(cell_info[i][column].width_of_cell, maximum);
1071         column_info[column].width_of_column = maximum;
1072 }
1073
1074
1075 //
1076 // Calculate the columns regarding ONLY the normal cells and if this
1077 // column is inside a multicolumn cell then use it only if its the last
1078 // column of this multicolumn cell as this gives an added width to the
1079 // column, all the rest should be adapted!
1080 //
1081 bool LyXTabular::calculate_width_of_column_NMC(col_type column)
1082 {
1083         int const old_column_width = column_info[column].width_of_column;
1084         int max = 0;
1085         for (row_type i = 0; i < rows_; ++i) {
1086                 idx_type cell = getCellNumber(i, column);
1087                 bool ismulti = isMultiColumnReal(cell);
1088                 if ((!ismulti || column == right_column_of_cell(cell)) &&
1089                         cell_info[i][column].width_of_cell > max)
1090                 {
1091                         max = cell_info[i][column].width_of_cell;
1092                 }
1093         }
1094         column_info[column].width_of_column = max;
1095         return column_info[column].width_of_column != old_column_width;
1096 }
1097
1098
1099 void LyXTabular::calculate_width_of_tabular()
1100 {
1101         width_of_tabular = 0;
1102         for (col_type i = 0; i < columns_; ++i)
1103                 width_of_tabular += column_info[i].width_of_column;
1104 }
1105
1106
1107 LyXTabular::row_type LyXTabular::row_of_cell(idx_type cell) const
1108 {
1109         if (cell >= numberofcells)
1110                 return rows_ - 1;
1111         if (cell == npos)
1112                 return 0;
1113         return rowofcell[cell];
1114 }
1115
1116
1117 LyXTabular::col_type LyXTabular::column_of_cell(idx_type cell) const
1118 {
1119         if (cell >= numberofcells)
1120                 return columns_ - 1;
1121         if (cell == npos)
1122                 return 0;
1123         return columnofcell[cell];
1124 }
1125
1126
1127 LyXTabular::col_type LyXTabular::right_column_of_cell(idx_type cell) const
1128 {
1129         row_type const row = row_of_cell(cell);
1130         col_type column = column_of_cell(cell);
1131         while (column < columns_ - 1 &&
1132                    cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1133                 ++column;
1134         return column;
1135 }
1136
1137
1138 void LyXTabular::write(Buffer const & buf, ostream & os) const
1139 {
1140         // header line
1141         os << "<lyxtabular"
1142            << write_attribute("version", 3)
1143            << write_attribute("rows", rows_)
1144            << write_attribute("columns", columns_)
1145            << ">\n";
1146         // global longtable options
1147         os << "<features"
1148            << write_attribute("rotate", rotate)
1149            << write_attribute("islongtable", is_long_tabular)
1150            << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1151            << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1152            << write_attribute("firstHeadEmpty", endfirsthead.empty)
1153            << write_attribute("headTopDL", endhead.topDL)
1154            << write_attribute("headBottomDL", endhead.bottomDL)
1155            << write_attribute("footTopDL", endfoot.topDL)
1156            << write_attribute("footBottomDL", endfoot.bottomDL)
1157            << write_attribute("lastFootTopDL", endlastfoot.topDL)
1158            << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1159            << write_attribute("lastFootEmpty", endlastfoot.empty)
1160            << ">\n";
1161         for (col_type j = 0; j < columns_; ++j) {
1162                 os << "<column"
1163                    << write_attribute("alignment", column_info[j].alignment)
1164                    << write_attribute("valignment", column_info[j].valignment)
1165                    << write_attribute("leftline", column_info[j].left_line)
1166                    << write_attribute("rightline", column_info[j].right_line)
1167                    << write_attribute("width", column_info[j].p_width.asString())
1168                    << write_attribute("special", column_info[j].align_special)
1169                    << ">\n";
1170         }
1171         for (row_type i = 0; i < rows_; ++i) {
1172                 os << "<row"
1173                    << write_attribute("topline", row_info[i].top_line)
1174                    << write_attribute("bottomline", row_info[i].bottom_line)
1175                    << write_attribute("endhead", row_info[i].endhead)
1176                    << write_attribute("endfirsthead", row_info[i].endfirsthead)
1177                    << write_attribute("endfoot", row_info[i].endfoot)
1178                    << write_attribute("endlastfoot", row_info[i].endlastfoot)
1179                    << write_attribute("newpage", row_info[i].newpage)
1180                    << ">\n";
1181                 for (col_type j = 0; j < columns_; ++j) {
1182                         os << "<cell"
1183                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1184                            << write_attribute("alignment", cell_info[i][j].alignment)
1185                            << write_attribute("valignment", cell_info[i][j].valignment)
1186                            << write_attribute("topline", cell_info[i][j].top_line)
1187                            << write_attribute("bottomline", cell_info[i][j].bottom_line)
1188                            << write_attribute("leftline", cell_info[i][j].left_line)
1189                            << write_attribute("rightline", cell_info[i][j].right_line)
1190                            << write_attribute("rotate", cell_info[i][j].rotate)
1191                            << write_attribute("usebox", cell_info[i][j].usebox)
1192                            << write_attribute("width", cell_info[i][j].p_width)
1193                            << write_attribute("special", cell_info[i][j].align_special)
1194                            << ">\n";
1195                         os << "\\begin_inset ";
1196                         cell_info[i][j].inset->write(buf, os);
1197                         os << "\n\\end_inset\n"
1198                            << "</cell>\n";
1199                 }
1200                 os << "</row>\n";
1201         }
1202         os << "</lyxtabular>\n";
1203 }
1204
1205
1206 void LyXTabular::read(Buffer const & buf, LyXLex & lex)
1207 {
1208         string line;
1209         istream & is = lex.getStream();
1210
1211         l_getline(is, line);
1212         if (!prefixIs(line, "<lyxtabular ")
1213                 && !prefixIs(line, "<LyXTabular ")) {
1214                 BOOST_ASSERT(false);
1215                 return;
1216         }
1217
1218         int version;
1219         if (!getTokenValue(line, "version", version))
1220                 return;
1221         BOOST_ASSERT(version >= 2);
1222
1223         int rows_arg;
1224         if (!getTokenValue(line, "rows", rows_arg))
1225                 return;
1226         int columns_arg;
1227         if (!getTokenValue(line, "columns", columns_arg))
1228                 return;
1229         init(buf.params(), rows_arg, columns_arg);
1230         l_getline(is, line);
1231         if (!prefixIs(line, "<features")) {
1232                 lyxerr << "Wrong tabular format (expected <features ...> got"
1233                        << line << ')' << endl;
1234                 return;
1235         }
1236         getTokenValue(line, "rotate", rotate);
1237         getTokenValue(line, "islongtable", is_long_tabular);
1238         getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1239         getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1240         getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1241         getTokenValue(line, "headTopDL", endhead.topDL);
1242         getTokenValue(line, "headBottomDL", endhead.bottomDL);
1243         getTokenValue(line, "footTopDL", endfoot.topDL);
1244         getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1245         getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1246         getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1247         getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1248
1249         for (col_type j = 0; j < columns_; ++j) {
1250                 l_getline(is,line);
1251                 if (!prefixIs(line,"<column")) {
1252                         lyxerr << "Wrong tabular format (expected <column ...> got"
1253                                << line << ')' << endl;
1254                         return;
1255                 }
1256                 getTokenValue(line, "alignment", column_info[j].alignment);
1257                 getTokenValue(line, "valignment", column_info[j].valignment);
1258                 getTokenValue(line, "leftline", column_info[j].left_line);
1259                 getTokenValue(line, "rightline", column_info[j].right_line);
1260                 getTokenValue(line, "width", column_info[j].p_width);
1261                 getTokenValue(line, "special", column_info[j].align_special);
1262         }
1263
1264         for (row_type i = 0; i < rows_; ++i) {
1265                 l_getline(is, line);
1266                 if (!prefixIs(line, "<row")) {
1267                         lyxerr << "Wrong tabular format (expected <row ...> got"
1268                                << line << ')' << endl;
1269                         return;
1270                 }
1271                 getTokenValue(line, "topline", row_info[i].top_line);
1272                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1273                 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1274                 getTokenValue(line, "endhead", row_info[i].endhead);
1275                 getTokenValue(line, "endfoot", row_info[i].endfoot);
1276                 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1277                 getTokenValue(line, "newpage", row_info[i].newpage);
1278                 for (col_type j = 0; j < columns_; ++j) {
1279                         l_getline(is, line);
1280                         if (!prefixIs(line, "<cell")) {
1281                                 lyxerr << "Wrong tabular format (expected <cell ...> got"
1282                                        << line << ')' << endl;
1283                                 return;
1284                         }
1285                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1286                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1287                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1288                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1289                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1290                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1291                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1292                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1293                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1294                         getTokenValue(line, "width", cell_info[i][j].p_width);
1295                         getTokenValue(line, "special", cell_info[i][j].align_special);
1296                         l_getline(is, line);
1297                         if (prefixIs(line, "\\begin_inset")) {
1298                                 cell_info[i][j].inset->read(buf, lex);
1299                                 l_getline(is, line);
1300                         }
1301                         if (!prefixIs(line, "</cell>")) {
1302                                 lyxerr << "Wrong tabular format (expected </cell> got"
1303                                        << line << ')' << endl;
1304                                 return;
1305                         }
1306                 }
1307                 l_getline(is, line);
1308                 if (!prefixIs(line, "</row>")) {
1309                         lyxerr << "Wrong tabular format (expected </row> got"
1310                                << line << ')' << endl;
1311                         return;
1312                 }
1313         }
1314         while (!prefixIs(line, "</lyxtabular>")) {
1315                 l_getline(is, line);
1316         }
1317         set_row_column_number_info();
1318 }
1319
1320
1321 bool LyXTabular::isMultiColumn(idx_type cell) const
1322 {
1323         return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1324 }
1325
1326
1327 bool LyXTabular::isMultiColumnReal(idx_type cell) const
1328 {
1329         return column_of_cell(cell) != right_column_of_cell(cell) &&
1330                         cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
1331 }
1332
1333
1334 LyXTabular::cellstruct & LyXTabular::cellinfo_of_cell(idx_type cell) const
1335 {
1336         return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1337 }
1338
1339
1340 void LyXTabular::setMultiColumn(Buffer * buffer, idx_type cell,
1341                                 idx_type number)
1342 {
1343         cellstruct & cs = cellinfo_of_cell(cell);
1344         cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1345         cs.alignment = column_info[column_of_cell(cell)].alignment;
1346         cs.top_line = row_info[row_of_cell(cell)].top_line;
1347         cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1348         cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1349         for (idx_type i = 1; i < number; ++i) {
1350                 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1351                 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1352                 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1353                 cs1.inset->clear(false);
1354         }
1355         set_row_column_number_info();
1356 }
1357
1358
1359 LyXTabular::idx_type LyXTabular::cells_in_multicolumn(idx_type cell) const
1360 {
1361         row_type const row = row_of_cell(cell);
1362         col_type column = column_of_cell(cell);
1363         idx_type result = 1;
1364         ++column;
1365         while (column < columns_ &&
1366                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1367         {
1368                 ++result;
1369                 ++column;
1370         }
1371         return result;
1372 }
1373
1374
1375 LyXTabular::idx_type LyXTabular::unsetMultiColumn(idx_type cell)
1376 {
1377         row_type const row = row_of_cell(cell);
1378         col_type column = column_of_cell(cell);
1379
1380         idx_type result = 0;
1381
1382         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1383                 cell_info[row][column].multicolumn = CELL_NORMAL;
1384                 ++column;
1385                 while (column < columns_ &&
1386                            cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1387                 {
1388                         cell_info[row][column].multicolumn = CELL_NORMAL;
1389                         ++column;
1390                         ++result;
1391                 }
1392         }
1393         set_row_column_number_info();
1394         return result;
1395 }
1396
1397
1398 void LyXTabular::setLongTabular(bool what)
1399 {
1400         is_long_tabular = what;
1401 }
1402
1403
1404 bool LyXTabular::isLongTabular() const
1405 {
1406         return is_long_tabular;
1407 }
1408
1409
1410 void LyXTabular::setRotateTabular(bool flag)
1411 {
1412         rotate = flag;
1413 }
1414
1415
1416 bool LyXTabular::getRotateTabular() const
1417 {
1418         return rotate;
1419 }
1420
1421
1422 void LyXTabular::setRotateCell(idx_type cell, bool flag)
1423 {
1424         cellinfo_of_cell(cell).rotate = flag;
1425 }
1426
1427
1428 bool LyXTabular::getRotateCell(idx_type cell) const
1429 {
1430         return cellinfo_of_cell(cell).rotate;
1431 }
1432
1433
1434 bool LyXTabular::needRotating() const
1435 {
1436         if (rotate)
1437                 return true;
1438         for (row_type i = 0; i < rows_; ++i)
1439                 for (col_type j = 0; j < columns_; ++j)
1440                         if (cell_info[i][j].rotate)
1441                                 return true;
1442         return false;
1443 }
1444
1445
1446 bool LyXTabular::isLastCell(idx_type cell) const
1447 {
1448         if (cell + 1 < numberofcells)
1449                 return false;
1450         return true;
1451 }
1452
1453
1454 LyXTabular::idx_type LyXTabular::getCellAbove(idx_type cell) const
1455 {
1456         if (row_of_cell(cell) > 0)
1457                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1458         return cell;
1459 }
1460
1461
1462 LyXTabular::idx_type LyXTabular::getCellBelow(idx_type cell) const
1463 {
1464         if (row_of_cell(cell) + 1 < rows_)
1465                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1466         return cell;
1467 }
1468
1469
1470 LyXTabular::idx_type LyXTabular::getLastCellAbove(idx_type cell) const
1471 {
1472         if (row_of_cell(cell) == 0)
1473                 return cell;
1474         if (!isMultiColumn(cell))
1475                 return getCellAbove(cell);
1476         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1477 }
1478
1479
1480 LyXTabular::idx_type LyXTabular::getLastCellBelow(idx_type cell) const
1481 {
1482         if (row_of_cell(cell) + 1 >= rows_)
1483                 return cell;
1484         if (!isMultiColumn(cell))
1485                 return getCellBelow(cell);
1486         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1487 }
1488
1489
1490 LyXTabular::idx_type LyXTabular::getCellNumber(row_type row,
1491                                                col_type column) const
1492 {
1493         BOOST_ASSERT(column != npos && column < columns_ &&
1494                      row    != npos && row    < rows_);
1495         return cell_info[row][column].cellno;
1496 }
1497
1498
1499 void LyXTabular::setUsebox(idx_type cell, BoxType type)
1500 {
1501         cellinfo_of_cell(cell).usebox = type;
1502 }
1503
1504
1505 LyXTabular::BoxType LyXTabular::getUsebox(idx_type cell) const
1506 {
1507         if (column_info[column_of_cell(cell)].p_width.zero() &&
1508                 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1509                 return BOX_NONE;
1510         if (cellinfo_of_cell(cell).usebox > 1)
1511                 return cellinfo_of_cell(cell).usebox;
1512         return useParbox(cell);
1513 }
1514
1515
1516 ///
1517 //  This are functions used for the longtable support
1518 ///
1519 void LyXTabular::setLTHead(row_type row, bool flag, ltType const & hd,
1520                            bool first)
1521 {
1522         if (first) {
1523                 endfirsthead = hd;
1524                 if (hd.set)
1525                         row_info[row].endfirsthead = flag;
1526         } else {
1527                 endhead = hd;
1528                 if (hd.set)
1529                         row_info[row].endhead = flag;
1530         }
1531 }
1532
1533
1534 bool LyXTabular::getRowOfLTHead(row_type row, ltType & hd) const
1535 {
1536         hd = endhead;
1537         hd.set = haveLTHead();
1538         return row_info[row].endhead;
1539 }
1540
1541
1542 bool LyXTabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1543 {
1544         hd = endfirsthead;
1545         hd.set = haveLTFirstHead();
1546         return row_info[row].endfirsthead;
1547 }
1548
1549
1550 void LyXTabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1551                            bool last)
1552 {
1553         if (last) {
1554                 endlastfoot = fd;
1555                 if (fd.set)
1556                         row_info[row].endlastfoot = flag;
1557         } else {
1558                 endfoot = fd;
1559                 if (fd.set)
1560                         row_info[row].endfoot = flag;
1561         }
1562 }
1563
1564
1565 bool LyXTabular::getRowOfLTFoot(row_type row, ltType & fd) const
1566 {
1567         fd = endfoot;
1568         fd.set = haveLTFoot();
1569         return row_info[row].endfoot;
1570 }
1571
1572
1573 bool LyXTabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1574 {
1575         fd = endlastfoot;
1576         fd.set = haveLTLastFoot();
1577         return row_info[row].endlastfoot;
1578 }
1579
1580
1581 void LyXTabular::setLTNewPage(row_type row, bool what)
1582 {
1583         row_info[row].newpage = what;
1584 }
1585
1586
1587 bool LyXTabular::getLTNewPage(row_type row) const
1588 {
1589         return row_info[row].newpage;
1590 }
1591
1592
1593 bool LyXTabular::haveLTHead() const
1594 {
1595         for (row_type i = 0; i < rows_; ++i)
1596                 if (row_info[i].endhead)
1597                         return true;
1598         return false;
1599 }
1600
1601
1602 bool LyXTabular::haveLTFirstHead() const
1603 {
1604         if (endfirsthead.empty)
1605                 return false;
1606         for (row_type i = 0; i < rows_; ++i)
1607                 if (row_info[i].endfirsthead)
1608                         return true;
1609         return false;
1610 }
1611
1612
1613 bool LyXTabular::haveLTFoot() const
1614 {
1615         for (row_type i = 0; i < rows_; ++i)
1616                 if (row_info[i].endfoot)
1617                         return true;
1618         return false;
1619 }
1620
1621
1622 bool LyXTabular::haveLTLastFoot() const
1623 {
1624         if (endlastfoot.empty)
1625                 return false;
1626         for (row_type i = 0; i < rows_; ++i)
1627                 if (row_info[i].endlastfoot)
1628                         return true;
1629         return false;
1630 }
1631
1632
1633 // end longtable support functions
1634
1635 void LyXTabular::setAscentOfRow(row_type row, int height)
1636 {
1637         if (row >= rows_ || row_info[row].ascent_of_row == height)
1638                 return;
1639         row_info[row].ascent_of_row = height;
1640 }
1641
1642
1643 void LyXTabular::setDescentOfRow(row_type row, int height)
1644 {
1645         if (row >= rows_ || row_info[row].descent_of_row == height)
1646                 return;
1647         row_info[row].descent_of_row = height;
1648 }
1649
1650
1651 int LyXTabular::getAscentOfRow(row_type row) const
1652 {
1653         if (row >= rows_)
1654                 return 0;
1655         return row_info[row].ascent_of_row;
1656 }
1657
1658
1659 int LyXTabular::getDescentOfRow(row_type row) const
1660 {
1661         BOOST_ASSERT(row < rows_);
1662         return row_info[row].descent_of_row;
1663 }
1664
1665
1666 int LyXTabular::getHeightOfTabular() const
1667 {
1668         int height = 0;
1669         for (row_type row = 0; row < rows_; ++row)
1670                 height += getAscentOfRow(row) + getDescentOfRow(row) +
1671                         getAdditionalHeight(row);
1672         return height;
1673 }
1674
1675
1676 bool LyXTabular::isPartOfMultiColumn(row_type row, col_type column) const
1677 {
1678         BOOST_ASSERT(row < rows_);
1679         BOOST_ASSERT(column < columns_);
1680         return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1681 }
1682
1683
1684 int LyXTabular::TeXTopHLine(ostream & os, row_type row) const
1685 {
1686         // FIXME: assert or return 0 as in TeXBottomHLine()?
1687         BOOST_ASSERT(row != npos);
1688         BOOST_ASSERT(row < rows_);
1689
1690         idx_type const fcell = getFirstCellInRow(row);
1691         idx_type const n = numberOfCellsInRow(fcell) + fcell;
1692         idx_type tmp = 0;
1693
1694         for (idx_type i = fcell; i < n; ++i) {
1695                 if (topLine(i))
1696                         ++tmp;
1697         }
1698         if (tmp == n - fcell) {
1699                 os << "\\hline ";
1700         } else if (tmp) {
1701                 for (idx_type i = fcell; i < n; ++i) {
1702                         if (topLine(i)) {
1703                                 os << "\\cline{"
1704                                    << column_of_cell(i) + 1
1705                                    << '-'
1706                                    << right_column_of_cell(i) + 1
1707                                    << "} ";
1708                         }
1709                 }
1710         } else {
1711                 return 0;
1712         }
1713         os << "\n";
1714         return 1;
1715 }
1716
1717
1718 int LyXTabular::TeXBottomHLine(ostream & os, row_type row) const
1719 {
1720         // FIXME: return 0 or assert as in TeXTopHLine()?
1721         if (row == npos || row >= rows_)
1722                 return 0;
1723
1724         idx_type const fcell = getFirstCellInRow(row);
1725         idx_type const n = numberOfCellsInRow(fcell) + fcell;
1726         idx_type tmp = 0;
1727
1728         for (idx_type i = fcell; i < n; ++i) {
1729                 if (bottomLine(i))
1730                         ++tmp;
1731         }
1732         if (tmp == n - fcell) {
1733                 os << "\\hline";
1734         } else if (tmp) {
1735                 for (idx_type i = fcell; i < n; ++i) {
1736                         if (bottomLine(i)) {
1737                                 os << "\\cline{"
1738                                    << column_of_cell(i) + 1
1739                                    << '-'
1740                                    << right_column_of_cell(i) + 1
1741                                    << "} ";
1742                         }
1743                 }
1744         } else {
1745                 return 0;
1746         }
1747         os << "\n";
1748         return 1;
1749 }
1750
1751
1752 int LyXTabular::TeXCellPreamble(ostream & os, idx_type cell) const
1753 {
1754         int ret = 0;
1755
1756         if (getRotateCell(cell)) {
1757                 os << "\\begin{sideways}\n";
1758                 ++ret;
1759         }
1760         if (isMultiColumn(cell)) {
1761                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1762                 if (!cellinfo_of_cell(cell).align_special.empty()) {
1763                         os << cellinfo_of_cell(cell).align_special << "}{";
1764                 } else {
1765                         if (leftLine(cell) &&
1766                                 (isFirstCellInRow(cell) ||
1767                                  (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
1768                                   !rightLine(cell - 1, true))))
1769                         {
1770                                 os << '|';
1771                         }
1772                         if (!getPWidth(cell).zero()) {
1773                                 switch (getVAlignment(cell)) {
1774                                 case LYX_VALIGN_TOP:
1775                                         os << 'p';
1776                                         break;
1777                                 case LYX_VALIGN_MIDDLE:
1778                                         os << 'm';
1779                                         break;
1780                                 case LYX_VALIGN_BOTTOM:
1781                                         os << 'b';
1782                                         break;
1783                                 }
1784                                 os << '{'
1785                                    << getPWidth(cell).asLatexString()
1786                                    << '}';
1787                         } else {
1788                                 switch (getAlignment(cell)) {
1789                                 case LYX_ALIGN_LEFT:
1790                                         os << 'l';
1791                                         break;
1792                                 case LYX_ALIGN_RIGHT:
1793                                         os << 'r';
1794                                         break;
1795                                 default:
1796                                         os << 'c';
1797                                         break;
1798                                 }
1799                         }
1800                         if (rightLine(cell))
1801                                 os << '|';
1802                         if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1803                                 leftLine(cell+1))
1804                                 os << '|';
1805                         os << "}{";
1806                 }
1807         }
1808         if (getUsebox(cell) == BOX_PARBOX) {
1809                 os << "\\parbox[";
1810                 switch (getVAlignment(cell)) {
1811                 case LYX_VALIGN_TOP:
1812                         os << 't';
1813                         break;
1814                 case LYX_VALIGN_MIDDLE:
1815                         os << 'c';
1816                         break;
1817                 case LYX_VALIGN_BOTTOM:
1818                         os << 'b';
1819                         break;
1820                 }
1821                 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1822         } else if (getUsebox(cell) == BOX_MINIPAGE) {
1823                 os << "\\begin{minipage}[";
1824                 switch (getVAlignment(cell)) {
1825                 case LYX_VALIGN_TOP:
1826                         os << 't';
1827                         break;
1828                 case LYX_VALIGN_MIDDLE:
1829                         os << 'm';
1830                         break;
1831                 case LYX_VALIGN_BOTTOM:
1832                         os << 'b';
1833                         break;
1834                 }
1835                 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1836                 ++ret;
1837         }
1838         return ret;
1839 }
1840
1841
1842 int LyXTabular::TeXCellPostamble(ostream & os, idx_type cell) const
1843 {
1844         int ret = 0;
1845
1846         // usual cells
1847         if (getUsebox(cell) == BOX_PARBOX)
1848                 os << '}';
1849         else if (getUsebox(cell) == BOX_MINIPAGE) {
1850                 os << "%\n\\end{minipage}";
1851                 ret += 2;
1852         }
1853         if (isMultiColumn(cell)) {
1854                 os << '}';
1855         }
1856         if (getRotateCell(cell)) {
1857                 os << "%\n\\end{sideways}";
1858                 ++ret;
1859         }
1860         return ret;
1861 }
1862
1863
1864 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
1865                                          OutputParams const & runparams) const
1866 {
1867         if (!is_long_tabular)
1868                 return 0;
1869
1870         int ret = 0;
1871         // output header info
1872         if (haveLTHead()) {
1873                 if (endhead.topDL) {
1874                         os << "\\hline\n";
1875                         ++ret;
1876                 }
1877                 for (row_type i = 0; i < rows_; ++i) {
1878                         if (row_info[i].endhead) {
1879                                 ret += TeXRow(os, i, buf, runparams);
1880                         }
1881                 }
1882                 if (endhead.bottomDL) {
1883                         os << "\\hline\n";
1884                         ++ret;
1885                 }
1886                 os << "\\endhead\n";
1887                 ++ret;
1888                 if (endfirsthead.empty) {
1889                         os << "\\endfirsthead\n";
1890                         ++ret;
1891                 }
1892         }
1893         // output firstheader info
1894         if (haveLTFirstHead()) {
1895                 if (endfirsthead.topDL) {
1896                         os << "\\hline\n";
1897                         ++ret;
1898                 }
1899                 for (row_type i = 0; i < rows_; ++i) {
1900                         if (row_info[i].endfirsthead) {
1901                                 ret += TeXRow(os, i, buf, runparams);
1902                         }
1903                 }
1904                 if (endfirsthead.bottomDL) {
1905                         os << "\\hline\n";
1906                         ++ret;
1907                 }
1908                 os << "\\endfirsthead\n";
1909                 ++ret;
1910         }
1911         // output footer info
1912         if (haveLTFoot()) {
1913                 if (endfoot.topDL) {
1914                         os << "\\hline\n";
1915                         ++ret;
1916                 }
1917                 for (row_type i = 0; i < rows_; ++i) {
1918                         if (row_info[i].endfoot) {
1919                                 ret += TeXRow(os, i, buf, runparams);
1920                         }
1921                 }
1922                 if (endfoot.bottomDL) {
1923                         os << "\\hline\n";
1924                         ++ret;
1925                 }
1926                 os << "\\endfoot\n";
1927                 ++ret;
1928                 if (endlastfoot.empty) {
1929                         os << "\\endlastfoot\n";
1930                         ++ret;
1931                 }
1932         }
1933         // output lastfooter info
1934         if (haveLTLastFoot()) {
1935                 if (endlastfoot.topDL) {
1936                         os << "\\hline\n";
1937                         ++ret;
1938                 }
1939                 for (row_type i = 0; i < rows_; ++i) {
1940                         if (row_info[i].endlastfoot) {
1941                                 ret += TeXRow(os, i, buf, runparams);
1942                         }
1943                 }
1944                 if (endlastfoot.bottomDL) {
1945                         os << "\\hline\n";
1946                         ++ret;
1947                 }
1948                 os << "\\endlastfoot\n";
1949                 ++ret;
1950         }
1951         return ret;
1952 }
1953
1954
1955 bool LyXTabular::isValidRow(row_type row) const
1956 {
1957         if (!is_long_tabular)
1958                 return true;
1959         return !row_info[row].endhead && !row_info[row].endfirsthead &&
1960                         !row_info[row].endfoot && !row_info[row].endlastfoot;
1961 }
1962
1963
1964 int LyXTabular::TeXRow(ostream & os, row_type i, Buffer const & buf,
1965                        OutputParams const & runparams) const
1966 {
1967         idx_type cell = getCellNumber(i, 0);
1968
1969         int ret = TeXTopHLine(os, i);
1970         for (col_type j = 0; j < columns_; ++j) {
1971                 if (isPartOfMultiColumn(i, j))
1972                         continue;
1973                 ret += TeXCellPreamble(os, cell);
1974                 shared_ptr<InsetText> inset = getCellInset(cell);
1975
1976                 Paragraph const & par = inset->paragraphs().front();
1977                 bool rtl = par.isRightToLeftPar(buf.params())
1978                         && !par.empty()
1979                         && getPWidth(cell).zero();
1980
1981                 if (rtl)
1982                         os << "\\R{";
1983                 ret += inset->latex(buf, os, runparams);
1984                 if (rtl)
1985                         os << '}';
1986
1987                 ret += TeXCellPostamble(os, cell);
1988                 if (!isLastCellInRow(cell)) { // not last cell in row
1989                         os << "&\n";
1990                         ++ret;
1991                 }
1992                 ++cell;
1993         }
1994         os << "\\tabularnewline\n";
1995         ++ret;
1996         ret += TeXBottomHLine(os, i);
1997         return ret;
1998 }
1999
2000
2001 int LyXTabular::latex(Buffer const & buf, ostream & os,
2002                       OutputParams const & runparams) const
2003 {
2004         int ret = 0;
2005
2006         //+---------------------------------------------------------------------
2007         //+                      first the opening preamble                    +
2008         //+---------------------------------------------------------------------
2009
2010         if (rotate) {
2011                 os << "\\begin{sideways}\n";
2012                 ++ret;
2013         }
2014         if (is_long_tabular)
2015                 os << "\\begin{longtable}{";
2016         else
2017                 os << "\\begin{tabular}{";
2018         for (col_type i = 0; i < columns_; ++i) {
2019                 if (!column_info[i].align_special.empty()) {
2020                         os << column_info[i].align_special;
2021                 } else {
2022                         if (column_info[i].left_line)
2023                                 os << '|';
2024                         if (!column_info[i].p_width.zero()) {
2025                                 switch (column_info[i].alignment) {
2026                                 case LYX_ALIGN_LEFT:
2027                                         os << ">{\\raggedright}";
2028                                         break;
2029                                 case LYX_ALIGN_RIGHT:
2030                                         os << ">{\\raggedleft}";
2031                                         break;
2032                                 case LYX_ALIGN_CENTER:
2033                                         os << ">{\\centering}";
2034                                         break;
2035                                 case LYX_ALIGN_NONE:
2036                                 case LYX_ALIGN_BLOCK:
2037                                 case LYX_ALIGN_LAYOUT:
2038                                 case LYX_ALIGN_SPECIAL:
2039                                         break;
2040                                 }
2041
2042                                 switch (column_info[i].valignment) {
2043                                 case LYX_VALIGN_TOP:
2044                                         os << 'p';
2045                                         break;
2046                                 case LYX_VALIGN_MIDDLE:
2047                                         os << 'm';
2048                                         break;
2049                                 case LYX_VALIGN_BOTTOM:
2050                                         os << 'b';
2051                                         break;
2052                         }
2053                                 os << '{'
2054                                    << column_info[i].p_width.asLatexString()
2055                                    << '}';
2056                         } else {
2057                                 switch (column_info[i].alignment) {
2058                                 case LYX_ALIGN_LEFT:
2059                                         os << 'l';
2060                                         break;
2061                                 case LYX_ALIGN_RIGHT:
2062                                         os << 'r';
2063                                         break;
2064                                 default:
2065                                         os << 'c';
2066                                         break;
2067                                 }
2068                         }
2069                         if (column_info[i].right_line)
2070                                 os << '|';
2071                 }
2072         }
2073         os << "}\n";
2074         ++ret;
2075
2076         ret += TeXLongtableHeaderFooter(os, buf, runparams);
2077
2078         //+---------------------------------------------------------------------
2079         //+                      the single row and columns (cells)            +
2080         //+---------------------------------------------------------------------
2081
2082         for (row_type i = 0; i < rows_; ++i) {
2083                 if (isValidRow(i)) {
2084                         ret += TeXRow(os, i, buf, runparams);
2085                         if (is_long_tabular && row_info[i].newpage) {
2086                                 os << "\\newpage\n";
2087                                 ++ret;
2088                         }
2089                 }
2090         }
2091
2092         //+---------------------------------------------------------------------
2093         //+                      the closing of the tabular                    +
2094         //+---------------------------------------------------------------------
2095
2096         if (is_long_tabular)
2097                 os << "\\end{longtable}";
2098         else
2099                 os << "\\end{tabular}";
2100         if (rotate) {
2101                 os << "\n\\end{sideways}";
2102                 ++ret;
2103         }
2104
2105         return ret;
2106 }
2107
2108
2109 int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
2110                          const OutputParams & runparams) const
2111 {
2112         os << "<tabular ca=\"";
2113         for (col_type i = 0; i < columns_; ++i) {
2114                 switch (column_info[i].alignment) {
2115                 case LYX_ALIGN_LEFT:
2116                         os << 'l';
2117                         break;
2118                 case LYX_ALIGN_RIGHT:
2119                         os << 'r';
2120                         break;
2121                 default:
2122                         os << 'c';
2123                         break;
2124                 }
2125         }
2126         os << "\">\n";
2127         idx_type cell = 0;
2128         int ret = 0;
2129         for (row_type i = 0; i < rows_; ++i) {
2130                 for (col_type j = 0; j < columns_; ++j) {
2131                         if (isPartOfMultiColumn(i, j))
2132                                 continue;
2133                         shared_ptr<InsetText> inset = getCellInset(cell);
2134
2135                         ret += inset->linuxdoc(buf, os, runparams);
2136
2137                         if (isLastCellInRow(cell)) {
2138                                 os << "@\n";
2139                                 ++ret;
2140                         } else {
2141                                 os << "|";
2142                         }
2143                         ++cell;
2144                 }
2145         }
2146         os << "</tabular>\n";
2147         return ret;
2148 }
2149
2150
2151 int LyXTabular::docbookRow(Buffer const & buf, ostream & os, row_type row,
2152                            OutputParams const & runparams) const
2153 {
2154         int ret = 0;
2155         idx_type cell = getFirstCellInRow(row);
2156
2157         os << "<row>\n";
2158         for (col_type j = 0; j < columns_; ++j) {
2159                 if (isPartOfMultiColumn(row, j))
2160                         continue;
2161
2162                 os << "<entry align=\"";
2163                 switch (getAlignment(cell)) {
2164                 case LYX_ALIGN_LEFT:
2165                         os << "left";
2166                         break;
2167                 case LYX_ALIGN_RIGHT:
2168                         os << "right";
2169                         break;
2170                 default:
2171                         os << "center";
2172                         break;
2173                 }
2174
2175                 os << "\" valign=\"";
2176                 switch (getVAlignment(cell)) {
2177                 case LYX_VALIGN_TOP:
2178                         os << "top";
2179                         break;
2180                 case LYX_VALIGN_BOTTOM:
2181                         os << "bottom";
2182                         break;
2183                 case LYX_VALIGN_MIDDLE:
2184                         os << "middle";
2185                 }
2186                 os << '"';
2187
2188                 if (isMultiColumn(cell)) {
2189                         os << " namest=\"col" << j << "\" ";
2190                         os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2191                 }
2192
2193                 os << '>';
2194                 ret += getCellInset(cell)->docbook(buf, os, runparams);
2195                 os << "</entry>\n";
2196                 ++cell;
2197         }
2198         os << "</row>\n";
2199         return ret;
2200 }
2201
2202
2203 int LyXTabular::docbook(Buffer const & buf, ostream & os,
2204                         OutputParams const & runparams) const
2205 {
2206         int ret = 0;
2207
2208         //+---------------------------------------------------------------------
2209         //+                      first the opening preamble                    +
2210         //+---------------------------------------------------------------------
2211
2212         os << "<tgroup cols=\"" << columns_
2213            << "\" colsep=\"1\" rowsep=\"1\">\n";
2214
2215         for (col_type i = 0; i < columns_; ++i) {
2216                 os << "<colspec colname=\"col" << i << "\" align=\"";
2217                 switch (column_info[i].alignment) {
2218                 case LYX_ALIGN_LEFT:
2219                         os << "left";
2220                         break;
2221                 case LYX_ALIGN_RIGHT:
2222                         os << "right";
2223                         break;
2224                 default:
2225                         os << "center";
2226                         break;
2227                 }
2228                 os << '"';
2229                 if (runparams.flavor == OutputParams::XML)
2230                         os << '/';
2231                 os << ">\n";
2232                 ++ret;
2233         }
2234
2235         //+---------------------------------------------------------------------
2236         //+                      Long Tabular case                             +
2237         //+---------------------------------------------------------------------
2238
2239         // output header info
2240         if (haveLTHead() || haveLTFirstHead()) {
2241                 os << "<thead>\n";
2242                 ++ret;
2243                 for (row_type i = 0; i < rows_; ++i) {
2244                         if (row_info[i].endhead || row_info[i].endfirsthead) {
2245                                 ret += docbookRow(buf, os, i, runparams);
2246                         }
2247                 }
2248                 os << "</thead>\n";
2249                 ++ret;
2250         }
2251         // output footer info
2252         if (haveLTFoot() || haveLTLastFoot()) {
2253                 os << "<tfoot>\n";
2254                 ++ret;
2255                 for (row_type i = 0; i < rows_; ++i) {
2256                         if (row_info[i].endfoot || row_info[i].endlastfoot) {
2257                                 ret += docbookRow(buf, os, i, runparams);
2258                         }
2259                 }
2260                 os << "</tfoot>\n";
2261                 ++ret;
2262         }
2263
2264         //+---------------------------------------------------------------------
2265         //+                      the single row and columns (cells)            +
2266         //+---------------------------------------------------------------------
2267
2268         os << "<tbody>\n";
2269         ++ret;
2270         for (row_type i = 0; i < rows_; ++i) {
2271                 if (isValidRow(i)) {
2272                         ret += docbookRow(buf, os, i, runparams);
2273                 }
2274         }
2275         os << "</tbody>\n";
2276         ++ret;
2277         //+---------------------------------------------------------------------
2278         //+                      the closing of the tabular                    +
2279         //+---------------------------------------------------------------------
2280
2281         os << "</tgroup>";
2282         ++ret;
2283
2284         return ret;
2285 }
2286
2287
2288 int LyXTabular::asciiTopHLine(ostream & os, row_type row,
2289                               vector<unsigned int> const & clen) const
2290 {
2291         idx_type const fcell = getFirstCellInRow(row);
2292         idx_type const n = numberOfCellsInRow(fcell) + fcell;
2293         idx_type tmp = 0;
2294
2295         for (idx_type i = fcell; i < n; ++i) {
2296                 if (topLine(i)) {
2297                         ++tmp;
2298                         break;
2299                 }
2300         }
2301         if (!tmp)
2302                 return 0;
2303
2304         unsigned char ch;
2305         for (idx_type i = fcell; i < n; ++i) {
2306                 if (topLine(i)) {
2307                         if (leftLine(i))
2308                                 os << "+-";
2309                         else
2310                                 os << "--";
2311                         ch = '-';
2312                 } else {
2313                         os << "  ";
2314                         ch = ' ';
2315                 }
2316                 col_type column = column_of_cell(i);
2317                 int len = clen[column];
2318                 while (column < columns_ - 1
2319                        && isPartOfMultiColumn(row, ++column))
2320                         len += clen[column] + 4;
2321                 os << string(len, ch);
2322                 if (topLine(i)) {
2323                         if (rightLine(i))
2324                                 os << "-+";
2325                         else
2326                                 os << "--";
2327                 } else {
2328                         os << "  ";
2329                 }
2330         }
2331         os << endl;
2332         return 1;
2333 }
2334
2335
2336 int LyXTabular::asciiBottomHLine(ostream & os, row_type row,
2337                                  vector<unsigned int> const & clen) const
2338 {
2339         idx_type const fcell = getFirstCellInRow(row);
2340         idx_type const n = numberOfCellsInRow(fcell) + fcell;
2341         idx_type tmp = 0;
2342
2343         for (idx_type i = fcell; i < n; ++i) {
2344                 if (bottomLine(i)) {
2345                         ++tmp;
2346                         break;
2347                 }
2348         }
2349         if (!tmp)
2350                 return 0;
2351
2352         unsigned char ch;
2353         for (idx_type i = fcell; i < n; ++i) {
2354                 if (bottomLine(i)) {
2355                         if (leftLine(i))
2356                                 os << "+-";
2357                         else
2358                                 os << "--";
2359                         ch = '-';
2360                 } else {
2361                         os << "  ";
2362                         ch = ' ';
2363                 }
2364                 col_type column = column_of_cell(i);
2365                 int len = clen[column];
2366                 while (column < columns_ -1
2367                        && isPartOfMultiColumn(row, ++column))
2368                         len += clen[column] + 4;
2369                 os << string(len, ch);
2370                 if (bottomLine(i)) {
2371                         if (rightLine(i))
2372                                 os << "-+";
2373                         else
2374                                 os << "--";
2375                 } else {
2376                         os << "  ";
2377                 }
2378         }
2379         os << endl;
2380         return 1;
2381 }
2382
2383
2384 int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
2385                                OutputParams const & runparams,
2386                                idx_type cell, row_type row, col_type column,
2387                                vector<unsigned int> const & clen,
2388                                bool onlydata) const
2389 {
2390         ostringstream sstr;
2391         int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
2392
2393         if (onlydata) {
2394                 os << sstr.str();
2395                 return ret;
2396         }
2397
2398         if (leftLine(cell))
2399                 os << "| ";
2400         else
2401                 os << "  ";
2402
2403         unsigned int len1 = sstr.str().length();
2404         unsigned int len2 = clen[column];
2405         while (column < columns_ -1
2406                && isPartOfMultiColumn(row, ++column))
2407                 len2 += clen[column] + 4;
2408         len2 -= len1;
2409
2410         switch (getAlignment(cell)) {
2411         default:
2412         case LYX_ALIGN_LEFT:
2413                 len1 = 0;
2414                 break;
2415         case LYX_ALIGN_RIGHT:
2416                 len1 = len2;
2417                 len2 = 0;
2418                 break;
2419         case LYX_ALIGN_CENTER:
2420                 len1 = len2 / 2;
2421                 len2 -= len1;
2422                 break;
2423         }
2424
2425         os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2426
2427         if (rightLine(cell))
2428                 os << " |";
2429         else
2430                 os << "  ";
2431
2432         return ret;
2433 }
2434
2435
2436 int LyXTabular::plaintext(Buffer const & buf, ostream & os,
2437                       OutputParams const & runparams,
2438                       int const depth,
2439                       bool onlydata, unsigned char delim) const
2440 {
2441         int ret = 0;
2442
2443         // first calculate the width of the single columns
2444         vector<unsigned int> clen(columns_);
2445
2446         if (!onlydata) {
2447                 // first all non (real) multicolumn cells!
2448                 for (col_type j = 0; j < columns_; ++j) {
2449                         clen[j] = 0;
2450                         for (row_type i = 0; i < rows_; ++i) {
2451                                 idx_type cell = getCellNumber(i, j);
2452                                 if (isMultiColumnReal(cell))
2453                                         continue;
2454                                 ostringstream sstr;
2455                                 getCellInset(cell)->plaintext(buf, sstr, runparams);
2456                                 if (clen[j] < sstr.str().length())
2457                                         clen[j] = sstr.str().length();
2458                         }
2459                 }
2460                 // then all (real) multicolumn cells!
2461                 for (col_type j = 0; j < columns_; ++j) {
2462                         for (row_type i = 0; i < rows_; ++i) {
2463                                 idx_type cell = getCellNumber(i, j);
2464                                 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2465                                         continue;
2466                                 ostringstream sstr;
2467                                 getCellInset(cell)->plaintext(buf, sstr, runparams);
2468                                 int len = int(sstr.str().length());
2469                                 idx_type const n = cells_in_multicolumn(cell);
2470                                 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2471                                         len -= clen[k];
2472                                 if (len > int(clen[j + n - 1]))
2473                                         clen[j + n - 1] = len;
2474                         }
2475                 }
2476         }
2477         idx_type cell = 0;
2478         for (row_type i = 0; i < rows_; ++i) {
2479                 if (!onlydata && asciiTopHLine(os, i, clen))
2480                         os << string(depth * 2, ' ');
2481                 for (col_type j = 0; j < columns_; ++j) {
2482                         if (isPartOfMultiColumn(i, j))
2483                                 continue;
2484                         if (onlydata && j > 0)
2485                                 os << delim;
2486                         ret += asciiPrintCell(buf, os, runparams,
2487                                               cell, i, j, clen, onlydata);
2488                         ++cell;
2489                 }
2490                 os << endl;
2491                 if (!onlydata) {
2492                         os << string(depth * 2, ' ');
2493                         if (asciiBottomHLine(os, i, clen))
2494                                 os << string(depth * 2, ' ');
2495                 }
2496         }
2497         return ret;
2498 }
2499
2500
2501 shared_ptr<InsetText> LyXTabular::getCellInset(idx_type cell) const
2502 {
2503         return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2504 }
2505
2506
2507 shared_ptr<InsetText> LyXTabular::getCellInset(row_type row,
2508                                                col_type column) const
2509 {
2510         return cell_info[row][column].inset;
2511 }
2512
2513
2514 LyXTabular::idx_type
2515 LyXTabular::getCellFromInset(InsetBase const * inset) const
2516 {
2517         // is this inset part of the tabular?
2518         if (!inset) {
2519                 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2520                 BOOST_ASSERT(false);
2521         }
2522
2523         for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2524                 if (getCellInset(cell).get() == inset) {
2525                         lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
2526                                 << "cell=" << cell << endl;
2527                         return cell;
2528                 }
2529
2530         // We should have found a cell at this point
2531         lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
2532                 << inset << " not found!" << endl;
2533         BOOST_ASSERT(false);
2534         // shut up compiler
2535         return 0;
2536 }
2537
2538
2539 void LyXTabular::validate(LaTeXFeatures & features) const
2540 {
2541         features.require("NeedTabularnewline");
2542         if (isLongTabular())
2543                 features.require("longtable");
2544         if (needRotating())
2545                 features.require("rotating");
2546         for (idx_type cell = 0; cell < numberofcells; ++cell) {
2547                 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2548                      (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2549                         features.require("array");
2550                 getCellInset(cell)->validate(features);
2551         }
2552 }
2553
2554
2555 void LyXTabular::getLabelList(Buffer const & buffer,
2556                               std::vector<string> & list) const
2557 {
2558         for (row_type i = 0; i < rows_; ++i)
2559                 for (col_type j = 0; j < columns_; ++j)
2560                         getCellInset(i, j)->getLabelList(buffer, list);
2561 }
2562
2563
2564 LyXTabular::BoxType LyXTabular::useParbox(idx_type cell) const
2565 {
2566         ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2567         ParagraphList::const_iterator cit = parlist.begin();
2568         ParagraphList::const_iterator end = parlist.end();
2569
2570         for (; cit != end; ++cit)
2571                 for (int i = 0; i < cit->size(); ++i)
2572                         if (cit->isNewline(i))
2573                                 return BOX_PARBOX;
2574
2575         return BOX_NONE;
2576 }