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