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