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