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