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