1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
27 #include "BufferView.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
37 #include "tabular_funcs.h"
49 #ifndef CXX_GLOBAL_CSTD
55 int const WIDTH_OF_LINE = 5;
59 /// Define a few methods for the inner structs
61 LyXTabular::cellstruct::cellstruct()
65 multicolumn = LyXTabular::CELL_NORMAL;
66 alignment = LYX_ALIGN_CENTER;
67 valignment = LYX_VALIGN_TOP;
77 LyXTabular::rowstruct::rowstruct()
91 LyXTabular::columnstruct::columnstruct()
95 alignment = LYX_ALIGN_CENTER;
96 valignment = LYX_VALIGN_TOP;
101 LyXTabular::lttype::lttype()
110 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
114 Init(rows_arg, columns_arg);
118 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
123 Init(lt.rows_, lt.columns_, <);
124 // we really should change again to have InsetText as a pointer
125 // and allocate it then we would not have to do this stuff all
128 for (int i = 0; i < rows_; ++i) {
129 for (int j = 0; j < columns_; ++j) {
130 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
131 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
137 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
144 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
152 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
155 #warning This while method should look like this: (Lgb)
160 // If this and lt is not of the same size we have a serious bug
161 // So then it is ok to throw an exception, or for now
163 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
165 cell_info = lt.cell_info;
166 row_info = lt.row_info;
167 column_info = lt.column_info;
168 SetLongTabular(lt.is_long_tabular);
177 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
179 LyXTabular * result = new LyXTabular(inset, *this, same_id);
181 // don't know if this is good but I need to Clone also
182 // the text-insets here, this is for the Undo-facility!
183 for (int i = 0; i < rows_; ++i) {
184 for (int j = 0; j < columns_; ++j) {
185 result->cell_info[i][j].inset = cell_info[i][j].inset;
186 result->cell_info[i][j].inset.setOwner(inset);
194 /* activates all lines and sets all widths to 0 */
195 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
198 columns_ = columns_arg;
199 row_info = row_vector(rows_, rowstruct());
200 column_info = column_vector(columns_, columnstruct());
201 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
209 for (int i = 0; i < rows_; ++i) {
210 for (int j = 0; j < columns_; ++j) {
211 cell_info[i][j].inset.setOwner(owner_);
212 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
213 cell_info[i][j].cellno = cellno++;
215 cell_info[i].back().right_line = true;
217 row_info.back().bottom_line = true;
218 row_info.front().bottom_line = true;
220 for (int i = 0; i < columns_; ++i) {
221 calculate_width_of_column(i);
223 column_info.back().right_line = true;
225 calculate_width_of_tabular();
227 rowofcell = vector<int>();
228 columnofcell = vector<int>();
229 set_row_column_number_info();
230 is_long_tabular = false;
235 void LyXTabular::AppendRow(int cell)
239 int row = row_of_cell(cell);
241 row_vector::iterator rit = row_info.begin() + row;
242 row_info.insert(rit, rowstruct());
243 // now set the values of the row before
244 row_info[row] = row_info[row+1];
247 cell_vvector::iterator cit = cell_info.begin() + row;
248 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
250 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
253 for (int i = 0; i <= row; ++i) {
254 for (int j = 0; j < columns_; ++j) {
255 c_info[i][j] = cell_info[i][j];
258 for (int i = row + 1; i < rows_; ++i) {
259 for (int j = 0; j < columns_; ++j) {
260 c_info[i][j] = cell_info[i-1][j];
265 for (int j = 0; j < columns_; ++j) {
266 cell_info[row][j].inset.clear();
273 void LyXTabular::DeleteRow(int row)
275 if (rows_ == 1) return; // Not allowed to delete last row
277 row_info.erase(row_info.begin() + row); //&row_info[row]);
278 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
284 void LyXTabular::AppendColumn(int cell)
288 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
290 int const column = column_of_cell(cell);
291 column_vector::iterator cit = column_info.begin() + column + 1;
292 column_info.insert(cit, columnstruct());
293 // set the column values of the column before
294 column_info[column+1] = column_info[column];
296 for (int i = 0; i < rows_; ++i) {
297 for (int j = 0; j <= column; ++j) {
298 c_info[i][j] = cell_info[i][j];
300 for (int j = column + 1; j < columns_; ++j) {
301 c_info[i][j] = cell_info[i][j - 1];
303 // care about multicolumns
304 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
306 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
308 if ((column + 2) >= columns_ ||
309 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
311 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
316 for (int i = 0; i < rows_; ++i) {
317 //cell_info[i][column].inset.clear();
318 cell_info[i][column + 1].inset.clear();
324 void LyXTabular::DeleteColumn(int column)
326 // Similar to DeleteRow
327 //if (!(columns_ - 1))
329 if (columns_ == 1) return; // Not allowed to delete last column
331 column_info.erase(column_info.begin() + column);
332 for (int i = 0; i < rows_; ++i) {
333 cell_info[i].erase(cell_info[i].begin() + column);
340 void LyXTabular::reinit()
346 void LyXTabular::Reinit(bool reset_widths)
349 for (int i = 0; i < rows_; ++i) {
350 for (int j = 0; j < columns_; ++j) {
351 cell_info[i][j].width_of_cell = 0;
352 cell_info[i][j].inset.setOwner(owner_);
357 for (int i = 0; i < columns_; ++i) {
358 calculate_width_of_column(i);
360 calculate_width_of_tabular();
362 set_row_column_number_info();
366 void LyXTabular::set_row_column_number_info(bool oldformat)
369 for (int row = 0; row < rows_; ++row) {
370 for (int column = 0; column<columns_; ++column) {
371 if (cell_info[row][column].multicolumn
372 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
374 cell_info[row][column].cellno = numberofcells;
377 ++numberofcells; // because this is one more than as we start from 0
379 rowofcell.resize(numberofcells);
380 columnofcell.resize(numberofcells);
382 for (int row = 0, column = 0, c = 0;
383 c < numberofcells && row < rows_ && column < columns_;) {
385 columnofcell[c] = column;
389 } while (column < columns_ &&
390 cell_info[row][column].multicolumn
391 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
392 if (column == columns_) {
398 for (int row = 0; row < rows_; ++row) {
399 for (int column = 0; column < columns_; ++column) {
400 if (IsPartOfMultiColumn(row,column))
402 // now set the right line of multicolumns right for oldformat read
404 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
406 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
407 cell_info[row][column].right_line =
408 cell_info[row][column+cn-1].right_line;
410 cell_info[row][column].inset.setAutoBreakRows(
411 !GetPWidth(GetCellNumber(row, column)).zero());
417 int LyXTabular::GetNumberOfCells() const
419 return numberofcells;
423 int LyXTabular::NumberOfCellsInRow(int cell) const
425 int const row = row_of_cell(cell);
427 for (int i = 0; i < columns_; ++i) {
428 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
435 /* returns 1 if there is a topline, returns 0 if not */
436 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
438 int const row = row_of_cell(cell);
440 if (!onlycolumn && IsMultiColumn(cell))
441 return cellinfo_of_cell(cell)->top_line;
442 return row_info[row].top_line;
446 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
448 // no bottom line underneath non-existent cells if you please
449 // Isn't that a programming error? Is so this should
450 // be an Assert instead. (Lgb)
451 if (cell >= numberofcells)
454 if (!onlycolumn && IsMultiColumn(cell))
455 return cellinfo_of_cell(cell)->bottom_line;
456 return row_info[row_of_cell(cell)].bottom_line;
460 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
462 if (!onlycolumn && IsMultiColumn(cell)) {
463 #ifdef SPECIAL_COLUM_HANDLING
464 if (cellinfo_of_cell(cell)->align_special.empty())
465 return cellinfo_of_cell(cell)->left_line;
466 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
468 return cellinfo_of_cell(cell)->left_line;
471 #ifdef SPECIAL_COLUM_HANDLING
472 if (column_info[column_of_cell(cell)].align_special.empty())
473 return column_info[column_of_cell(cell)].left_line;
474 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
476 return column_info[column_of_cell(cell)].left_line;
481 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
483 if (!onlycolumn && IsMultiColumn(cell)) {
484 #ifdef SPECIAL_COLUM_HANDLING
485 if (cellinfo_of_cell(cell)->align_special.empty())
486 return cellinfo_of_cell(cell)->right_line;
487 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
489 return cellinfo_of_cell(cell)->right_line;
492 #ifdef SPECIAL_COLUM_HANDLING
493 if (column_info[column_of_cell(cell)].align_special.empty())
494 return column_info[right_column_of_cell(cell)].right_line;
495 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
497 return column_info[right_column_of_cell(cell)].right_line;
502 bool LyXTabular::TopAlreadyDrawed(int cell) const
504 int row = row_of_cell(cell);
505 if ((row > 0) && !GetAdditionalHeight(row)) {
506 int column = column_of_cell(cell);
509 && cell_info[row][column].multicolumn
510 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
512 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
513 return row_info[row].bottom_line;
515 return cell_info[row][column].bottom_line;
521 bool LyXTabular::LeftAlreadyDrawed(int cell) const
523 int column = column_of_cell(cell);
525 int row = row_of_cell(cell);
527 (cell_info[row][column].multicolumn ==
528 LyXTabular::CELL_PART_OF_MULTICOLUMN));
529 if (GetAdditionalWidth(cell_info[row][column].cellno))
531 #ifdef SPECIAL_COLUM_HANDLING
532 return column_info[column].right_line;
534 return RightLine(cell_info[row][column].cellno, true);
541 bool LyXTabular::IsLastRow(int cell) const
543 return (row_of_cell(cell) == rows_ - 1);
547 int LyXTabular::GetAdditionalHeight(int row) const
549 if (!row || row >= rows_)
555 for (int column = 0; column < columns_ && bottom; ++column) {
556 switch (cell_info[row - 1][column].multicolumn) {
557 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
558 bottom = cell_info[row - 1][column].bottom_line;
560 case LyXTabular::CELL_NORMAL:
561 bottom = row_info[row - 1].bottom_line;
564 for (int column = 0; column < columns_ && top; ++column) {
565 switch (cell_info[row][column].multicolumn){
566 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
567 top = cell_info[row][column].top_line;
569 case LyXTabular::CELL_NORMAL:
570 top = row_info[row].top_line;
574 return WIDTH_OF_LINE;
579 int LyXTabular::GetAdditionalWidth(int cell) const
581 // internally already set in SetWidthOfCell
582 // used to get it back in text.C
583 int const col = right_column_of_cell(cell);
584 int const row = row_of_cell(cell);
585 if (col < columns_ - 1 && RightLine(cell, true) &&
586 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
588 return WIDTH_OF_LINE;
595 // returns the maximum over all rows
596 int LyXTabular::GetWidthOfColumn(int cell) const
598 int const column1 = column_of_cell(cell);
599 int const column2 = right_column_of_cell(cell);
601 for (int i = column1; i <= column2; ++i) {
602 result += column_info[i].width_of_column;
608 int LyXTabular::GetWidthOfTabular() const
610 return width_of_tabular;
614 /* returns 1 if a complete update is necessary, otherwise 0 */
615 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
617 if (!IsMultiColumn(cell))
620 int const row = row_of_cell(cell);
621 int const column1 = column_of_cell(cell);
622 int const column2 = right_column_of_cell(cell);
624 // first set columns to 0 so we can calculate the right width
625 for (int i = column1; i <= column2; ++i) {
626 cell_info[row][i].width_of_cell = 0;
628 // set the width to MAX_WIDTH until width > 0
629 int width = (new_width + 2 * WIDTH_OF_LINE);
632 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
633 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
634 width -= column_info[i].width_of_column;
637 cell_info[row][i].width_of_cell = width;
643 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
645 int const row = row_of_cell(cell);
646 int const column1 = column_of_cell(cell);
647 int const column2 = right_column_of_cell(cell);
649 // first set columns to 0 so we can calculate the right width
651 for (; i <= column2; ++i)
652 cell_info[row][i].width_of_cell = 0;
653 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
655 if (i < numberofcells)
656 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
657 SetWidthOfMulticolCell(cell, new_width);
661 /* returns 1 if a complete update is necessary, otherwise 0 */
662 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
664 int const row = row_of_cell(cell);
665 int const column1 = column_of_cell(cell);
670 #ifdef SPECIAL_COLUM_HANDLING
671 if (RightLine(cell_info[row][column1].cellno, true) &&
672 (column1 < columns_-1) &&
673 LeftLine(cell_info[row][column1+1].cellno, true))
675 if (column_info[column1].right_line && (column1 < columns_-1) &&
676 column_info[column1+1].left_line) // additional width
680 add_width = WIDTH_OF_LINE;
682 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
685 if (IsMultiColumn(cell, true)) {
686 tmp = SetWidthOfMulticolCell(cell, new_width);
688 width = (new_width + 2*WIDTH_OF_LINE + add_width);
689 cell_info[row][column1].width_of_cell = width;
690 tmp = calculate_width_of_column_NMC(column1);
694 for (; i<columns_; ++i)
695 calculate_width_of_column_NMC(i);
696 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
698 if (i < numberofcells)
699 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
700 for (i = 0; i < columns_; ++i)
701 calculate_width_of_column(i);
702 calculate_width_of_tabular();
709 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
711 if (!IsMultiColumn(cell) || onlycolumn)
712 column_info[column_of_cell(cell)].alignment = align;
714 cellinfo_of_cell(cell)->alignment = align;
719 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
721 if (!IsMultiColumn(cell) || onlycolumn)
722 column_info[column_of_cell(cell)].valignment = align;
724 cellinfo_of_cell(cell)->valignment = align;
729 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
731 bool flag = !width.zero();
732 int const j = column_of_cell(cell);
734 column_info[j].p_width = width;
735 if (flag) // do this only if there is a width
736 SetAlignment(cell, LYX_ALIGN_LEFT);
737 for (int i = 0; i < rows_; ++i) {
738 int c = GetCellNumber(i, j);
739 flag = !GetPWidth(c).zero(); // because of multicolumns!
740 GetCellInset(c)->setAutoBreakRows(flag);
746 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
748 bool const flag = !width.zero();
750 cellinfo_of_cell(cell)->p_width = width;
751 if (IsMultiColumn(cell)) {
752 GetCellInset(cell)->setAutoBreakRows(flag);
759 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
760 LyXTabular::Feature what)
762 if (what == SET_SPECIAL_MULTI)
763 cellinfo_of_cell(cell)->align_special = special;
765 column_info[column_of_cell(cell)].align_special = special;
770 bool LyXTabular::SetAllLines(int cell, bool line)
772 SetTopLine(cell, line);
773 SetBottomLine(cell, line);
774 SetRightLine(cell, line);
775 SetLeftLine(cell, line);
780 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
782 int const row = row_of_cell(cell);
784 if (onlycolumn || !IsMultiColumn(cell))
785 row_info[row].top_line = line;
787 cellinfo_of_cell(cell)->top_line = line;
792 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
794 if (onlycolumn || !IsMultiColumn(cell))
795 row_info[row_of_cell(cell)].bottom_line = line;
797 cellinfo_of_cell(cell)->bottom_line = line;
802 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
804 if (onlycolumn || !IsMultiColumn(cell))
805 column_info[column_of_cell(cell)].left_line = line;
807 cellinfo_of_cell(cell)->left_line = line;
812 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
814 if (onlycolumn || !IsMultiColumn(cell))
815 column_info[right_column_of_cell(cell)].right_line = line;
817 cellinfo_of_cell(cell)->right_line = line;
822 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
824 if (!onlycolumn && IsMultiColumn(cell))
825 return cellinfo_of_cell(cell)->alignment;
827 return column_info[column_of_cell(cell)].alignment;
831 LyXTabular::VAlignment
832 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
834 if (!onlycolumn && IsMultiColumn(cell))
835 return cellinfo_of_cell(cell)->valignment;
837 return column_info[column_of_cell(cell)].valignment;
841 LyXLength const LyXTabular::GetPWidth(int cell) const
843 if (IsMultiColumn(cell))
844 return cellinfo_of_cell(cell)->p_width;
845 return column_info[column_of_cell(cell)].p_width;
849 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
851 return column_info[column_of_cell(cell)].p_width;
855 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
857 if (IsMultiColumn(cell))
858 return cellinfo_of_cell(cell)->p_width;
863 string const LyXTabular::GetAlignSpecial(int cell, int what) const
865 if (what == SET_SPECIAL_MULTI)
866 return cellinfo_of_cell(cell)->align_special;
867 return column_info[column_of_cell(cell)].align_special;
871 int LyXTabular::GetWidthOfCell(int cell) const
873 int const row = row_of_cell(cell);
874 int const column1 = column_of_cell(cell);
875 int const column2 = right_column_of_cell(cell);
877 for (int i = column1; i <= column2; ++i) {
878 result += cell_info[row][i].width_of_cell;
884 int LyXTabular::GetBeginningOfTextInCell(int cell) const
888 switch (GetAlignment(cell)){
889 case LYX_ALIGN_CENTER:
890 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
892 case LYX_ALIGN_RIGHT:
893 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
894 // + GetAdditionalWidth(cell);
896 default: /* LYX_ALIGN_LEFT: nothing :-) */
906 bool LyXTabular::IsFirstCellInRow(int cell) const
908 return column_of_cell(cell) == 0;
912 int LyXTabular::GetFirstCellInRow(int row) const
916 return cell_info[row][0].cellno;
919 bool LyXTabular::IsLastCellInRow(int cell) const
921 return (right_column_of_cell(cell) == (columns_ - 1));
925 int LyXTabular::GetLastCellInRow(int row) const
929 return cell_info[row][columns_-1].cellno;
933 bool LyXTabular::calculate_width_of_column(int column)
935 int const old_column_width = column_info[column].width_of_column;
938 for (int i = 0; i < rows_; ++i) {
939 maximum = max(cell_info[i][column].width_of_cell, maximum);
941 column_info[column].width_of_column = maximum;
942 return (column_info[column].width_of_column != old_column_width);
947 // calculate the with of the column without regarding REAL MultiColumn
948 // cells. This means MultiColumn-cells spanning more than 1 column.
950 bool LyXTabular::calculate_width_of_column_NMC(int column)
952 int const old_column_width = column_info[column].width_of_column;
954 for (int i = 0; i < rows_; ++i) {
955 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
956 (cell_info[i][column].width_of_cell > max)) {
957 max = cell_info[i][column].width_of_cell;
960 column_info[column].width_of_column = max;
961 return (column_info[column].width_of_column != old_column_width);
965 void LyXTabular::calculate_width_of_tabular()
967 width_of_tabular = 0;
968 for (int i = 0; i < columns_; ++i) {
969 width_of_tabular += column_info[i].width_of_column;
974 int LyXTabular::row_of_cell(int cell) const
976 if (cell >= numberofcells)
980 return rowofcell[cell];
984 int LyXTabular::column_of_cell(int cell) const
986 if (cell >= numberofcells)
990 return columnofcell[cell];
994 int LyXTabular::right_column_of_cell(int cell) const
996 int const row = row_of_cell(cell);
997 int column = column_of_cell(cell);
998 while (column < (columns_ - 1) &&
999 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1005 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1009 << write_attribute("version", 3)
1010 << write_attribute("rows", rows_)
1011 << write_attribute("columns", columns_)
1013 // global longtable options
1015 << write_attribute("rotate", tostr(rotate))
1016 << write_attribute("islongtable", tostr(is_long_tabular))
1017 << write_attribute("firstHeadTopDL", tostr(endfirsthead.topDL))
1018 << write_attribute("firstHeadBottomDL", tostr(endfirsthead.bottomDL))
1019 << write_attribute("firstHeadEmpty", tostr(endfirsthead.empty))
1020 << write_attribute("headTopDL", tostr(endhead.topDL))
1021 << write_attribute("headBottomDL", tostr(endhead.bottomDL))
1022 << write_attribute("footTopDL", tostr(endfoot.topDL))
1023 << write_attribute("footBottomDL", tostr(endfoot.bottomDL))
1024 << write_attribute("lastFootTopDL", tostr(endlastfoot.topDL))
1025 << write_attribute("lastFootBottomDL", tostr(endlastfoot.bottomDL))
1026 << write_attribute("lastFootEmpty", tostr(endlastfoot.empty))
1028 for (int j = 0; j < columns_; ++j) {
1030 << write_attribute("alignment", tostr(column_info[j].alignment))
1031 << write_attribute("valignment", tostr(column_info[j].valignment))
1032 << write_attribute("leftline", tostr(column_info[j].left_line))
1033 << write_attribute("rightline", tostr(column_info[j].right_line))
1034 << write_attribute("width", column_info[j].p_width.asString())
1035 << write_attribute("special", column_info[j].align_special)
1038 for (int i = 0; i < rows_; ++i) {
1040 << write_attribute("topline", tostr(row_info[i].top_line))
1041 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1042 << write_attribute("endhead", tostr(row_info[i].endhead))
1043 << write_attribute("endfirsthead", tostr(row_info[i].endfirsthead))
1044 << write_attribute("endfoot", tostr(row_info[i].endfoot))
1045 << write_attribute("endlastfoot", tostr(row_info[i].endlastfoot))
1046 << write_attribute("newpage", tostr(row_info[i].newpage))
1048 for (int j = 0; j < columns_; ++j) {
1050 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1051 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1052 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1053 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1054 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1055 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1056 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1057 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1058 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1059 << write_attribute("width", cell_info[i][j].p_width)
1060 << write_attribute("special", cell_info[i][j].align_special)
1062 os << "\\begin_inset ";
1063 cell_info[i][j].inset.write(buf, os);
1064 os << "\n\\end_inset \n"
1069 os << "</lyxtabular>\n";
1073 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1076 istream & is = lex.getStream();
1078 l_getline(is, line);
1079 if (!prefixIs(line, "<lyxtabular ")
1080 && !prefixIs(line, "<LyXTabular ")) {
1081 OldFormatRead(lex, line);
1086 if (!getTokenValue(line, "version", version))
1089 ReadOld(buf, is, lex, line);
1090 else if (version >= 2)
1091 ReadNew(buf, is, lex, line, version);
1094 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1098 row_info[--hr].endhead = true;
1100 // set firstheader info
1101 if (fhr && (fhr < rows_)) {
1102 if (row_info[fhr].endhead) {
1104 row_info[--fhr].endfirsthead = true;
1105 row_info[fhr].endhead = false;
1107 } else if (row_info[fhr-1].endhead) {
1108 endfirsthead.empty = true;
1110 while((fhr > 0) && !row_info[--fhr].endhead) {
1111 row_info[fhr].endfirsthead = true;
1116 if (fr && (fr < rows_)) {
1117 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1118 while((fr > 0) && !row_info[--fr].endhead) {
1119 row_info[fr].endfoot = true;
1120 row_info[fr].endhead = false;
1122 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1123 while((fr > 0) && !row_info[--fr].endfirsthead) {
1124 row_info[fr].endfoot = true;
1125 row_info[fr].endfirsthead = false;
1127 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead) {
1128 while((fr > 0) && !row_info[--fr].endhead &&
1129 !row_info[fr].endfirsthead)
1131 row_info[fr].endfoot = true;
1135 // set lastfooter info
1136 if (lfr && (lfr < rows_)) {
1137 if (row_info[lfr].endhead && row_info[lfr-1].endhead) {
1138 while((lfr > 0) && !row_info[--lfr].endhead) {
1139 row_info[lfr].endlastfoot = true;
1140 row_info[lfr].endhead = false;
1142 } else if (row_info[lfr].endfirsthead &&
1143 row_info[lfr-1].endfirsthead)
1145 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1146 row_info[lfr].endlastfoot = true;
1147 row_info[lfr].endfirsthead = false;
1149 } else if (row_info[lfr].endfoot && row_info[lfr-1].endfoot) {
1150 while((lfr > 0) && !row_info[--lfr].endfoot) {
1151 row_info[lfr].endlastfoot = true;
1152 row_info[lfr].endfoot = false;
1154 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead &&
1155 !row_info[fr-1].endfoot)
1158 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1159 !row_info[lfr].endfoot)
1161 row_info[lfr].endlastfoot = true;
1163 } else if (haveLTFoot()) {
1164 endlastfoot.empty = true;
1169 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1170 LyXLex & lex, string const & l, int const version)
1174 if (!getTokenValue(line, "rows", rows_arg))
1177 if (!getTokenValue(line, "columns", columns_arg))
1179 Init(rows_arg, columns_arg);
1180 l_getline(is, line);
1181 if (!prefixIs(line, "<features")) {
1182 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1183 line << ")" << endl;
1186 getTokenValue(line, "rotate", rotate);
1187 getTokenValue(line, "islongtable", is_long_tabular);
1188 // compatibility read for old longtable options. Now we can make any
1189 // row part of the header/footer type we want before it was strict
1190 // sequential from the first row down (as LaTeX does it!). So now when
1191 // we find a header/footer line we have to go up the rows and set it
1192 // on all preceding rows till the first or one with already a h/f option
1193 // set. If we find a firstheader on the same line as a header or a
1194 // lastfooter on the same line as a footer then this should be set empty.
1202 getTokenValue(line, "endhead", hrow);
1203 getTokenValue(line, "endfirsthead", fhrow);
1204 getTokenValue(line, "endfoot", frow);
1205 getTokenValue(line, "endlastfoot", lfrow);
1206 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1208 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1209 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1210 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1211 getTokenValue(line, "headTopDL", endhead.topDL);
1212 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1213 getTokenValue(line, "footTopDL", endfoot.topDL);
1214 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1215 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1216 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1217 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1219 for (int j = 0; j < columns_; ++j) {
1221 if (!prefixIs(line,"<column")) {
1222 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1223 line << ")" << endl;
1226 getTokenValue(line, "alignment", column_info[j].alignment);
1227 getTokenValue(line, "valignment", column_info[j].valignment);
1228 getTokenValue(line, "leftline", column_info[j].left_line);
1229 getTokenValue(line, "rightline", column_info[j].right_line);
1230 getTokenValue(line, "width", column_info[j].p_width);
1231 getTokenValue(line, "special", column_info[j].align_special);
1234 for (int i = 0; i < rows_; ++i) {
1235 l_getline(is, line);
1236 if (!prefixIs(line, "<row")) {
1237 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1238 line << ")" << endl;
1241 getTokenValue(line, "topline", row_info[i].top_line);
1242 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1243 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1244 getTokenValue(line, "endhead", row_info[i].endhead);
1245 getTokenValue(line, "endfoot", row_info[i].endfoot);
1246 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1247 getTokenValue(line, "newpage", row_info[i].newpage);
1248 for (int j = 0; j < columns_; ++j) {
1249 l_getline(is, line);
1250 if (!prefixIs(line, "<cell")) {
1251 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1252 line << ")" << endl;
1255 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1256 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1257 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1258 getTokenValue(line, "topline", cell_info[i][j].top_line);
1259 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1260 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1261 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1262 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1263 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1264 getTokenValue(line, "width", cell_info[i][j].p_width);
1265 getTokenValue(line, "special", cell_info[i][j].align_special);
1266 l_getline(is, line);
1267 if (prefixIs(line, "\\begin_inset")) {
1268 cell_info[i][j].inset.read(buf, lex);
1269 l_getline(is, line);
1271 if (!prefixIs(line, "</cell>")) {
1272 lyxerr << "Wrong tabular format (expected </cell> got" <<
1273 line << ")" << endl;
1277 l_getline(is, line);
1278 if (!prefixIs(line, "</row>")) {
1279 lyxerr << "Wrong tabular format (expected </row> got" <<
1280 line << ")" << endl;
1284 while (!prefixIs(line, "</lyxtabular>")) {
1285 l_getline(is, line);
1287 set_row_column_number_info();
1291 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1297 int columns_arg = 0;
1298 int is_long_tabular_arg = false;
1299 int rotate_arg = false;
1308 istream & is = lex.getStream();
1311 version = lyx::atoi(s.substr(8, string::npos));
1315 vector<int> cont_row_info;
1318 lyxerr << "Tabular format < 5 is not supported anymore\n"
1319 "Get an older version of LyX (< 1.1.x) for conversion!"
1321 Alert::alert(_("Warning:"),
1322 _("Tabular format < 5 is not supported anymore\n"),
1323 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1325 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1326 >> rotate_arg >> a >> b >> c >> d;
1328 is >> rows_arg >> columns_arg;
1329 Init(rows_arg, columns_arg);
1330 cont_row_info = vector<int>(rows_arg);
1331 SetLongTabular(is_long_tabular_arg);
1332 SetRotateTabular(rotate_arg);
1334 for (i = 0; i < rows_; ++i) {
1336 cont_row_info[i] = false;
1338 for (i = 0; i < columns_; ++i) {
1341 for (i = 0; i < rows_; ++i) {
1342 for (j = 0; j < columns_; ++j) {
1347 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1348 >> rotate_arg >> a >> b >> c >> d;
1349 Init(rows_arg, columns_arg);
1350 cont_row_info = vector<int>(rows_arg);
1351 SetLongTabular(is_long_tabular_arg);
1352 SetRotateTabular(rotate_arg);
1353 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1354 for (i = 0; i < rows_; ++i) {
1355 a = b = c = d = e = f = g = 0;
1356 is >> a >> b >> c >> d;
1357 row_info[i].top_line = a;
1358 row_info[i].bottom_line = b;
1359 cont_row_info[i] = c;
1360 row_info[i].newpage = d;
1362 for (i = 0; i < columns_; ++i) {
1367 char ch; // skip '"'
1370 // ignore is buggy but we will use it later (Lgb)
1371 is.ignore(); // skip '"'
1373 getline(is, s1, '"');
1375 is >> ch; // skip '"'
1377 // ignore is buggy but we will use it later (Lgb)
1378 is.ignore(); // skip '"'
1380 getline(is, s2, '"');
1381 column_info[i].alignment = static_cast<LyXAlignment>(a);
1382 column_info[i].left_line = b;
1383 column_info[i].right_line = c;
1384 column_info[i].p_width = LyXLength(s1);
1385 column_info[i].align_special = s2;
1387 for (i = 0; i < rows_; ++i) {
1388 for (j = 0; j < columns_; ++j) {
1391 is >> a >> b >> c >> d >> e >> f >> g;
1394 is >> ch; // skip '"'
1396 // ignore is buggy but we will use it later (Lgb)
1397 is.ignore(); // skip '"'
1399 getline(is, s1, '"');
1401 is >> ch; // skip '"'
1403 // ignore is buggy but we will use it later (Lgb)
1404 is.ignore(); // skip '"'
1406 getline(is, s2, '"');
1407 cell_info[i][j].multicolumn = static_cast<char>(a);
1408 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1409 cell_info[i][j].top_line = static_cast<char>(c);
1410 cell_info[i][j].bottom_line = static_cast<char>(d);
1411 cell_info[i][j].left_line = column_info[j].left_line;
1412 cell_info[i][j].right_line = column_info[j].right_line;
1413 cell_info[i][j].rotate = static_cast<bool>(f);
1414 cell_info[i][j].usebox = static_cast<BoxType>(g);
1415 cell_info[i][j].align_special = s1;
1416 cell_info[i][j].p_width = LyXLength(s2);
1420 set_row_column_number_info(true);
1422 Paragraph * par = new Paragraph;
1423 Paragraph * return_par = 0;
1427 Paragraph::depth_type depth = 0;
1428 LyXFont font(LyXFont::ALL_INHERIT);
1429 font.setLanguage(owner_->bufferOwner()->getLanguage());
1431 while (lex.isOK()) {
1433 string const token = lex.getString();
1436 if (token == "\\layout"
1437 || token == "\\end_float"
1438 || token == "\\end_deeper") {
1439 lex.pushToken(token);
1440 #ifndef NO_COMPABILITY
1441 // Here we need to insert the inset_ert_contents into the last
1442 // cell of the tabular.
1443 owner_->bufferOwner()->insertErtContents(par, pos);
1447 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1452 lex.pushToken(token);
1456 lex.printError("New Paragraph allocated! This should not happen!");
1457 lex.pushToken(token);
1463 // now we have the par we should fill the insets with this!
1465 InsetText * inset = GetCellInset(cell);
1468 for (int i = 0; i < par->size(); ++i) {
1469 if (par->isNewline(i)) {
1471 if (cell > numberofcells) {
1472 lyxerr << "Some error in reading old table format occured!" <<
1473 endl << "Terminating when reading cell[" << cell << "]!" <<
1478 row = row_of_cell(cell);
1479 if (cont_row_info[row]) {
1481 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1482 while (!IsFirstCellInRow(--cell));
1484 inset = GetCellInset(cell);
1487 inset = GetCellInset(cell);
1488 row = row_of_cell(cell);
1489 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1491 // insert a space instead
1493 par->insertChar(i, ' ');
1496 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1497 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1504 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1506 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1507 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1511 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1513 int const row = row_of_cell(cell);
1514 int const column = column_of_cell(cell);
1515 return &cell_info[row][column];
1519 void LyXTabular::SetMultiColumn(int cell, int number)
1521 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1522 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1523 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1524 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1525 for (number--; number > 0; --number) {
1526 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1528 set_row_column_number_info();
1532 int LyXTabular::cells_in_multicolumn(int cell) const
1534 int const row = row_of_cell(cell);
1535 int column = column_of_cell(cell);
1538 while ((column < columns_) &&
1539 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1548 int LyXTabular::UnsetMultiColumn(int cell)
1550 int const row = row_of_cell(cell);
1551 int column = column_of_cell(cell);
1555 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1556 cell_info[row][column].multicolumn = CELL_NORMAL;
1558 while ((column < columns_) &&
1559 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1561 cell_info[row][column].multicolumn = CELL_NORMAL;
1566 set_row_column_number_info();
1571 void LyXTabular::SetLongTabular(bool what)
1573 is_long_tabular = what;
1577 bool LyXTabular::IsLongTabular() const
1579 return is_long_tabular;
1583 void LyXTabular::SetRotateTabular(bool flag)
1589 bool LyXTabular::GetRotateTabular() const
1595 void LyXTabular::SetRotateCell(int cell, bool flag)
1597 cellinfo_of_cell(cell)->rotate = flag;
1601 bool LyXTabular::GetRotateCell(int cell) const
1603 return cellinfo_of_cell(cell)->rotate;
1607 bool LyXTabular::NeedRotating() const
1611 for (int i = 0; i < rows_; ++i) {
1612 for (int j = 0; j < columns_; ++j) {
1613 if (cell_info[i][j].rotate)
1621 bool LyXTabular::IsLastCell(int cell) const
1623 if ((cell + 1) < numberofcells)
1629 int LyXTabular::GetCellAbove(int cell) const
1631 if (row_of_cell(cell) > 0)
1632 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1637 int LyXTabular::GetCellBelow(int cell) const
1639 if (row_of_cell(cell) + 1 < rows_)
1640 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1645 int LyXTabular::GetLastCellAbove(int cell) const
1647 if (row_of_cell(cell) <= 0)
1649 if (!IsMultiColumn(cell))
1650 return GetCellAbove(cell);
1651 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1655 int LyXTabular::GetLastCellBelow(int cell) const
1657 if (row_of_cell(cell) + 1 >= rows_)
1659 if (!IsMultiColumn(cell))
1660 return GetCellBelow(cell);
1661 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1665 int LyXTabular::GetCellNumber(int row, int column) const
1668 if (column >= columns_)
1669 column = columns_ - 1;
1670 else if (column < 0)
1677 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1679 return cell_info[row][column].cellno;
1683 void LyXTabular::SetUsebox(int cell, BoxType type)
1685 cellinfo_of_cell(cell)->usebox = type;
1689 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1691 if (column_info[column_of_cell(cell)].p_width.zero() &&
1692 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1694 if (cellinfo_of_cell(cell)->usebox > 1)
1695 return cellinfo_of_cell(cell)->usebox;
1696 return UseParbox(cell);
1701 // This are functions used for the longtable support
1703 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1708 row_info[row].endfirsthead = flag;
1712 row_info[row].endhead = flag;
1717 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1720 hd.set = haveLTHead();
1721 return row_info[row].endhead;
1725 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1728 hd.set = haveLTFirstHead();
1729 return row_info[row].endfirsthead;
1733 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1738 row_info[row].endlastfoot = flag;
1742 row_info[row].endfoot = flag;
1747 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1750 fd.set = haveLTFoot();
1751 return row_info[row].endfoot;
1755 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1758 fd.set = haveLTLastFoot();
1759 return row_info[row].endlastfoot;
1763 void LyXTabular::SetLTNewPage(int row, bool what)
1765 row_info[row].newpage = what;
1769 bool LyXTabular::GetLTNewPage(int row) const
1771 return row_info[row].newpage;
1775 bool LyXTabular::haveLTHead() const
1777 for(int i=0; i < rows_; ++i) {
1778 if (row_info[i].endhead)
1785 bool LyXTabular::haveLTFirstHead() const
1787 if (endfirsthead.empty)
1789 for(int i=0; i < rows_; ++i) {
1790 if (row_info[i].endfirsthead)
1797 bool LyXTabular::haveLTFoot() const
1799 for(int i=0; i < rows_; ++i) {
1800 if (row_info[i].endfoot)
1807 bool LyXTabular::haveLTLastFoot() const
1809 if (endlastfoot.empty)
1811 for(int i=0; i < rows_; ++i) {
1812 if (row_info[i].endlastfoot)
1819 // end longtable support functions
1821 bool LyXTabular::SetAscentOfRow(int row, int height)
1823 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1825 row_info[row].ascent_of_row = height;
1830 bool LyXTabular::SetDescentOfRow(int row, int height)
1832 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1834 row_info[row].descent_of_row = height;
1839 int LyXTabular::GetAscentOfRow(int row) const
1843 return row_info[row].ascent_of_row;
1847 int LyXTabular::GetDescentOfRow(int row) const
1851 return row_info[row].descent_of_row;
1855 int LyXTabular::GetHeightOfTabular() const
1859 for (int row = 0; row < rows_; ++row)
1860 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1861 GetAdditionalHeight(row);
1866 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1868 if ((row >= rows_) || (column >= columns_))
1870 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1874 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1876 if ((row < 0) || (row >= rows_))
1879 int const fcell = GetFirstCellInRow(row);
1880 int const n = NumberOfCellsInRow(fcell) + fcell;
1883 for (int i = fcell; i < n; ++i) {
1887 if (tmp == (n - fcell)){
1890 for (int i = fcell; i < n; ++i) {
1893 << column_of_cell(i) + 1
1895 << right_column_of_cell(i) + 1
1907 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1909 if ((row < 0) || (row >= rows_))
1912 int const fcell = GetFirstCellInRow(row);
1913 int const n = NumberOfCellsInRow(fcell) + fcell;
1916 for (int i = fcell; i < n; ++i) {
1920 if (tmp == (n-fcell)){
1923 for (int i = fcell; i < n; ++i) {
1924 if (BottomLine(i)) {
1926 << column_of_cell(i) + 1
1928 << right_column_of_cell(i) + 1
1940 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1944 if (GetRotateCell(cell)) {
1945 os << "\\begin{sideways}\n";
1948 if (IsMultiColumn(cell)) {
1949 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1950 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1951 os << cellinfo_of_cell(cell)->align_special << "}{";
1953 if (LeftLine(cell) &&
1954 (IsFirstCellInRow(cell) ||
1955 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1956 !RightLine(cell-1, true))))
1960 if (!GetPWidth(cell).zero()) {
1961 switch (GetVAlignment(cell)) {
1962 case LYX_VALIGN_TOP:
1965 case LYX_VALIGN_CENTER:
1968 case LYX_VALIGN_BOTTOM:
1972 os << "{" << GetPWidth(cell).asLatexString() << '}';
1974 switch (GetAlignment(cell)) {
1975 case LYX_ALIGN_LEFT:
1978 case LYX_ALIGN_RIGHT:
1986 if (RightLine(cell))
1988 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1994 if (GetUsebox(cell) == BOX_PARBOX) {
1996 switch (GetVAlignment(cell)) {
1997 case LYX_VALIGN_TOP:
2000 case LYX_VALIGN_CENTER:
2003 case LYX_VALIGN_BOTTOM:
2007 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2008 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2009 os << "\\begin{minipage}[";
2010 switch (GetVAlignment(cell)) {
2011 case LYX_VALIGN_TOP:
2014 case LYX_VALIGN_CENTER:
2017 case LYX_VALIGN_BOTTOM:
2021 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2028 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2033 if (GetUsebox(cell) == BOX_PARBOX)
2035 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2036 os << "%\n\\end{minipage}";
2039 if (IsMultiColumn(cell)){
2042 if (GetRotateCell(cell)) {
2043 os << "%\n\\end{sideways}";
2050 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2051 bool fragile, bool fp) const
2053 if (!is_long_tabular)
2057 // output header info
2059 if (endhead.topDL) {
2063 for (int i = 0; i < rows_; ++i) {
2064 if (row_info[i].endhead) {
2065 ret += TeXRow(os, i, buf, fragile, fp);
2068 if (endhead.bottomDL) {
2072 os << "\\endhead\n";
2074 if (endfirsthead.empty) {
2075 os << "\\endfirsthead\n";
2079 // output firstheader info
2080 if (haveLTFirstHead()) {
2081 if (endfirsthead.topDL) {
2085 for (int i = 0; i < rows_; ++i) {
2086 if (row_info[i].endfirsthead) {
2087 ret += TeXRow(os, i, buf, fragile, fp);
2090 if (endfirsthead.bottomDL) {
2094 os << "\\endfirsthead\n";
2097 // output footer info
2099 if (endfoot.topDL) {
2103 for (int i = 0; i < rows_; ++i) {
2104 if (row_info[i].endfoot) {
2105 ret += TeXRow(os, i, buf, fragile, fp);
2108 if (endfoot.bottomDL) {
2112 os << "\\endfoot\n";
2114 if (endlastfoot.empty) {
2115 os << "\\endlastfoot\n";
2119 // output lastfooter info
2120 if (haveLTLastFoot()) {
2121 if (endlastfoot.topDL) {
2125 for (int i = 0; i < rows_; ++i) {
2126 if (row_info[i].endlastfoot) {
2127 ret += TeXRow(os, i, buf, fragile, fp);
2130 if (endlastfoot.bottomDL) {
2134 os << "\\endlastfoot\n";
2141 bool LyXTabular::isValidRow(int const row) const
2143 if (!is_long_tabular)
2145 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2146 !row_info[row].endfoot && !row_info[row].endlastfoot);
2150 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2151 bool fragile, bool fp) const
2154 int cell = GetCellNumber(i, 0);
2156 ret += TeXTopHLine(os, i);
2157 for (int j = 0; j < columns_; ++j) {
2158 if (IsPartOfMultiColumn(i,j))
2160 ret += TeXCellPreamble(os, cell);
2161 InsetText * inset = GetCellInset(cell);
2163 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2164 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2168 ret += inset->latex(buf, os, fragile, fp);
2172 ret += TeXCellPostamble(os, cell);
2173 if (!IsLastCellInRow(cell)) { // not last cell in row
2181 ret += TeXBottomHLine(os, i);
2186 int LyXTabular::latex(Buffer const * buf,
2187 ostream & os, bool fragile, bool fp) const
2191 //+---------------------------------------------------------------------
2192 //+ first the opening preamble +
2193 //+---------------------------------------------------------------------
2196 os << "\\begin{sideways}\n";
2199 if (is_long_tabular)
2200 os << "\\begin{longtable}{";
2202 os << "\\begin{tabular}{";
2203 for (int i = 0; i < columns_; ++i) {
2204 if (!column_info[i].align_special.empty()) {
2205 os << column_info[i].align_special;
2207 if (column_info[i].left_line)
2209 if (!column_info[i].p_width.zero()) {
2210 switch (column_info[i].valignment) {
2211 case LYX_VALIGN_TOP:
2214 case LYX_VALIGN_CENTER:
2217 case LYX_VALIGN_BOTTOM:
2222 << column_info[i].p_width.asLatexString()
2225 switch (column_info[i].alignment) {
2226 case LYX_ALIGN_LEFT:
2229 case LYX_ALIGN_RIGHT:
2237 if (column_info[i].right_line)
2244 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2246 //+---------------------------------------------------------------------
2247 //+ the single row and columns (cells) +
2248 //+---------------------------------------------------------------------
2250 for (int i = 0; i < rows_; ++i) {
2251 if (isValidRow(i)) {
2252 ret += TeXRow(os, i, buf, fragile, fp);
2253 if (is_long_tabular && row_info[i].newpage) {
2254 os << "\\newpage\n";
2260 //+---------------------------------------------------------------------
2261 //+ the closing of the tabular +
2262 //+---------------------------------------------------------------------
2264 if (is_long_tabular)
2265 os << "\\end{longtable}";
2267 os << "\\end{tabular}";
2269 os << "\n\\end{sideways}";
2277 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2280 int cell = GetFirstCellInRow(row);
2283 for (int j = 0; j < columns_; ++j) {
2284 if (IsPartOfMultiColumn(row, j))
2287 os << "<entry align=\"";
2288 switch (GetAlignment(cell)) {
2289 case LYX_ALIGN_LEFT:
2292 case LYX_ALIGN_RIGHT:
2300 os << "\" valign=\"";
2301 switch (GetVAlignment(cell)) {
2302 case LYX_VALIGN_TOP:
2305 case LYX_VALIGN_BOTTOM:
2308 case LYX_VALIGN_CENTER:
2313 if (IsMultiColumn(cell)) {
2314 os << " namest=\"col" << j << "\" ";
2315 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2319 ret += GetCellInset(cell)->docbook(buf, os);
2328 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2332 //+---------------------------------------------------------------------
2333 //+ first the opening preamble +
2334 //+---------------------------------------------------------------------
2336 os << "<tgroup cols=\"" << columns_
2337 << "\" colsep=\"1\" rowsep=\"1\">\n";
2339 for (int i = 0; i < columns_; ++i) {
2340 os << "<colspec colname=\"col" << i << "\" align=\"";
2341 switch (column_info[i].alignment) {
2342 case LYX_ALIGN_LEFT:
2345 case LYX_ALIGN_RIGHT:
2356 //+---------------------------------------------------------------------
2357 //+ Long Tabular case +
2358 //+---------------------------------------------------------------------
2360 #warning Jose please have a look here I changed the longtable header/footer
2361 #warning ---- options so I had to disable the docbook code (Jug 20011219)
2363 if ( IsLongTabular() ) {
2365 if(endhead.row || endfirsthead.row ) {
2367 if( endfirsthead.row ) {
2368 docbookRow( buf, os, endfirsthead.row - 1);
2370 if( endhead.row && endhead.row != endfirsthead.row) {
2371 docbookRow(buf, os, endhead.row - 1);
2377 if( endfoot.row || endlastfoot.row ) {
2380 docbookRow( buf, os, endfoot.row - 1);
2382 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2383 docbookRow( buf, os, endlastfoot.row - 1);
2389 //+---------------------------------------------------------------------
2390 //+ the single row and columns (cells) +
2391 //+---------------------------------------------------------------------
2394 for (int i = 0; i < rows_; ++i) {
2395 if(!IsLongTabular() || (
2396 !row_info[i].endhead && !row_info[i].endfirsthead &&
2397 !row_info[i].endfoot && !row_info[i].endlastfoot)) {
2398 docbookRow( buf, os, i);
2402 //+---------------------------------------------------------------------
2403 //+ the closing of the tabular +
2404 //+---------------------------------------------------------------------
2413 // ASCII export function and helpers
2415 int LyXTabular::asciiTopHLine(ostream & os, int row,
2416 vector<unsigned int> const & clen) const
2418 int const fcell = GetFirstCellInRow(row);
2419 int const n = NumberOfCellsInRow(fcell) + fcell;
2422 for (int i = fcell; i < n; ++i) {
2432 for (int i = fcell; i < n; ++i) {
2443 int column = column_of_cell(i);
2444 int len = clen[column];
2445 while (IsPartOfMultiColumn(row, ++column))
2446 len += clen[column] + 4;
2447 os << string(len, ch);
2462 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2463 vector<unsigned int> const & clen) const
2465 int const fcell = GetFirstCellInRow(row);
2466 int const n = NumberOfCellsInRow(fcell) + fcell;
2469 for (int i = fcell; i < n; ++i) {
2470 if (BottomLine(i)) {
2479 for (int i = fcell; i < n; ++i) {
2480 if (BottomLine(i)) {
2490 int column = column_of_cell(i);
2491 int len = clen[column];
2492 while (IsPartOfMultiColumn(row, ++column))
2493 len += clen[column] + 4;
2494 os << string(len, ch);
2495 if (BottomLine(i)) {
2509 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2510 int cell, int row, int column,
2511 vector<unsigned int> const & clen,
2512 bool onlydata) const
2515 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2527 unsigned int len1 = sstr.str().length();
2528 unsigned int len2 = clen[column];
2529 while (IsPartOfMultiColumn(row, ++column))
2530 len2 += clen[column] + 4;
2533 switch (GetAlignment(cell)) {
2535 case LYX_ALIGN_LEFT:
2538 case LYX_ALIGN_RIGHT:
2542 case LYX_ALIGN_CENTER:
2548 for (unsigned int i = 0; i < len1; ++i)
2551 for (unsigned int i = 0; i < len2; ++i)
2553 if (RightLine(cell))
2562 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2563 bool onlydata, unsigned char delim) const
2567 //+---------------------------------------------------------------------
2568 //+ first calculate the width of the single columns +
2569 //+---------------------------------------------------------------------
2570 vector<unsigned int> clen(columns_);
2573 // first all non (real) multicolumn cells!
2574 for (int j = 0; j < columns_; ++j) {
2576 for (int i = 0; i < rows_; ++i) {
2577 int cell = GetCellNumber(i, j);
2578 if (IsMultiColumn(cell, true))
2581 GetCellInset(cell)->ascii(buf, sstr, 0);
2582 if (clen[j] < sstr.str().length())
2583 clen[j] = sstr.str().length();
2586 // then all (real) multicolumn cells!
2587 for (int j = 0; j < columns_; ++j) {
2588 for (int i = 0; i < rows_; ++i) {
2589 int cell = GetCellNumber(i, j);
2590 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2593 GetCellInset(cell)->ascii(buf, sstr, 0);
2594 int len = int(sstr.str().length());
2595 int const n = cells_in_multicolumn(cell);
2596 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2598 if (len > int(clen[j + n - 1]))
2599 clen[j + n - 1] = len;
2604 for (int i = 0; i < rows_; ++i) {
2606 if (asciiTopHLine(os, i, clen)) {
2607 for (int j = 0; j < depth; ++j)
2611 for (int j = 0; j < columns_; ++j) {
2612 if (IsPartOfMultiColumn(i,j))
2614 if (onlydata && j > 0)
2616 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2621 for (int j = 0; j < depth; ++j)
2623 if (asciiBottomHLine(os, i, clen)) {
2624 for (int j = 0; j < depth; ++j)
2636 InsetText * LyXTabular::GetCellInset(int cell) const
2639 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2643 InsetText * LyXTabular::GetCellInset(int row, int column) const
2645 cur_cell = GetCellNumber(row, column);
2646 return & cell_info[row][column].inset;
2650 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2652 // is this inset part of the tabular?
2653 if (!inset || inset->owner() != owner_) {
2654 lyxerr[Debug::INSETTEXT]
2655 << "this is not a cell of the tabular!" << endl;
2659 const int save_cur_cell = cur_cell;
2660 int cell = cur_cell;
2661 if (GetCellInset(cell) != inset) {
2663 if (cell == -1 || GetCellInset(cell) != inset) {
2669 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2670 if (GetCellInset(cell) == inset)
2673 lyxerr[Debug::INSETTEXT]
2674 << "LyXTabular::GetCellFromInset: "
2676 << ", cur_cell=" << save_cur_cell
2677 << ", maybe_cell=" << maybe_cell
2679 // We should have found a cell at this point
2681 lyxerr << "LyXTabular::GetCellFromInset: "
2682 << "Cell not found!" << endl;
2690 void LyXTabular::Validate(LaTeXFeatures & features) const
2692 if (IsLongTabular())
2693 features.require("longtable");
2695 features.require("rotating");
2696 for (int cell = 0; cell < numberofcells; ++cell) {
2697 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2698 features.require("array");
2699 GetCellInset(cell)->validate(features);
2704 std::vector<string> const LyXTabular::getLabelList() const
2706 std::vector<string> label_list;
2707 for (int i = 0; i < rows_; ++i)
2708 for (int j = 0; j < columns_; ++j) {
2709 std::vector<string> const l =
2710 GetCellInset(i, j)->getLabelList();
2711 label_list.insert(label_list.end(),
2712 l.begin(), l.end());
2718 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2720 Paragraph * par = GetCellInset(cell)->paragraph();
2722 for (; par; par = par->next()) {
2723 for (int i = 0; i < par->size(); ++i) {
2724 if (par->getChar(i) == Paragraph::META_NEWLINE)