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