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