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