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) {
305 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
307 if ((column + 1) == columns_ ||
308 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
309 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
314 for (int i = 0; i < rows_; ++i) {
315 //cell_info[i][column].inset.clear();
316 cell_info[i][column + 1].inset.clear();
322 void LyXTabular::DeleteColumn(int column)
324 // Similar to DeleteRow
325 //if (!(columns_ - 1))
327 if (columns_ == 1) return; // Not allowed to delete last column
329 column_info.erase(column_info.begin() + column);
330 for (int i = 0; i < rows_; ++i) {
331 cell_info[i].erase(cell_info[i].begin() + column);
338 void LyXTabular::reinit()
344 void LyXTabular::Reinit(bool reset_widths)
347 for (int i = 0; i < rows_; ++i) {
348 for (int j = 0; j < columns_; ++j) {
349 cell_info[i][j].width_of_cell = 0;
350 cell_info[i][j].inset.setOwner(owner_);
355 for (int i = 0; i < columns_; ++i) {
356 calculate_width_of_column(i);
358 calculate_width_of_tabular();
360 set_row_column_number_info();
364 void LyXTabular::set_row_column_number_info(bool oldformat)
367 for (int row = 0; row < rows_; ++row) {
368 for (int column = 0; column<columns_; ++column) {
369 if (cell_info[row][column].multicolumn
370 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
372 cell_info[row][column].cellno = numberofcells;
375 ++numberofcells; // because this is one more than as we start from 0
377 rowofcell.resize(numberofcells);
378 columnofcell.resize(numberofcells);
380 for (int row = 0, column = 0, c = 0;
381 c < numberofcells && row < rows_ && column < columns_;) {
383 columnofcell[c] = column;
387 } while (column < columns_ &&
388 cell_info[row][column].multicolumn
389 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
390 if (column == columns_) {
396 for (int row = 0; row < rows_; ++row) {
397 for (int column = 0; column < columns_; ++column) {
398 if (IsPartOfMultiColumn(row,column))
400 // now set the right line of multicolumns right for oldformat read
402 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
404 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
405 cell_info[row][column].right_line =
406 cell_info[row][column+cn-1].right_line;
408 cell_info[row][column].inset.setAutoBreakRows(
409 !GetPWidth(GetCellNumber(row, column)).zero());
415 int LyXTabular::GetNumberOfCells() const
417 return numberofcells;
421 int LyXTabular::NumberOfCellsInRow(int cell) const
423 int const row = row_of_cell(cell);
425 for (int i = 0; i < columns_; ++i) {
426 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
433 /* returns 1 if there is a topline, returns 0 if not */
434 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
436 int const row = row_of_cell(cell);
438 if (!onlycolumn && IsMultiColumn(cell))
439 return cellinfo_of_cell(cell)->top_line;
440 return row_info[row].top_line;
444 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
446 // no bottom line underneath non-existent cells if you please
447 // Isn't that a programming error? Is so this should
448 // be an Assert instead. (Lgb)
449 if (cell >= numberofcells)
452 if (!onlycolumn && IsMultiColumn(cell))
453 return cellinfo_of_cell(cell)->bottom_line;
454 return row_info[row_of_cell(cell)].bottom_line;
458 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
460 if (!onlycolumn && IsMultiColumn(cell)) {
461 #ifdef SPECIAL_COLUM_HANDLING
462 if (cellinfo_of_cell(cell)->align_special.empty())
463 return cellinfo_of_cell(cell)->left_line;
464 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
466 return cellinfo_of_cell(cell)->left_line;
469 #ifdef SPECIAL_COLUM_HANDLING
470 if (column_info[column_of_cell(cell)].align_special.empty())
471 return column_info[column_of_cell(cell)].left_line;
472 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
474 return column_info[column_of_cell(cell)].left_line;
479 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
481 if (!onlycolumn && IsMultiColumn(cell)) {
482 #ifdef SPECIAL_COLUM_HANDLING
483 if (cellinfo_of_cell(cell)->align_special.empty())
484 return cellinfo_of_cell(cell)->right_line;
485 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
487 return cellinfo_of_cell(cell)->right_line;
490 #ifdef SPECIAL_COLUM_HANDLING
491 if (column_info[column_of_cell(cell)].align_special.empty())
492 return column_info[right_column_of_cell(cell)].right_line;
493 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
495 return column_info[right_column_of_cell(cell)].right_line;
500 bool LyXTabular::TopAlreadyDrawed(int cell) const
502 int row = row_of_cell(cell);
503 if ((row > 0) && !GetAdditionalHeight(row)) {
504 int column = column_of_cell(cell);
507 && cell_info[row][column].multicolumn
508 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
510 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
511 return row_info[row].bottom_line;
513 return cell_info[row][column].bottom_line;
519 bool LyXTabular::LeftAlreadyDrawed(int cell) const
521 int column = column_of_cell(cell);
523 int row = row_of_cell(cell);
525 (cell_info[row][column].multicolumn ==
526 LyXTabular::CELL_PART_OF_MULTICOLUMN));
527 if (GetAdditionalWidth(cell_info[row][column].cellno))
529 #ifdef SPECIAL_COLUM_HANDLING
530 return column_info[column].right_line;
532 return RightLine(cell_info[row][column].cellno, true);
539 bool LyXTabular::IsLastRow(int cell) const
541 return (row_of_cell(cell) == rows_ - 1);
545 int LyXTabular::GetAdditionalHeight(int row) const
547 if (!row || row >= rows_)
553 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
554 switch (cell_info[row - 1][column].multicolumn) {
555 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
556 bottom = cell_info[row - 1][column].bottom_line;
558 case LyXTabular::CELL_NORMAL:
559 bottom = row_info[row - 1].bottom_line;
562 for (int column = 0; column < columns_ - 1 && top; ++column) {
563 switch (cell_info[row][column].multicolumn){
564 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
565 top = cell_info[row][column].top_line;
567 case LyXTabular::CELL_NORMAL:
568 top = row_info[row].top_line;
572 return WIDTH_OF_LINE;
577 int LyXTabular::GetAdditionalWidth(int cell) const
579 // internally already set in SetWidthOfCell
580 // used to get it back in text.C
581 int const col = right_column_of_cell(cell);
582 int const row = row_of_cell(cell);
583 if (col < columns_ - 1 && RightLine(cell, true) &&
584 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
586 return WIDTH_OF_LINE;
593 // returns the maximum over all rows
594 int LyXTabular::GetWidthOfColumn(int cell) const
596 int const column1 = column_of_cell(cell);
597 int const column2 = right_column_of_cell(cell);
599 for (int i = column1; i <= column2; ++i) {
600 result += column_info[i].width_of_column;
606 int LyXTabular::GetWidthOfTabular() const
608 return width_of_tabular;
612 /* returns 1 if a complete update is necessary, otherwise 0 */
613 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
615 if (!IsMultiColumn(cell))
618 int const row = row_of_cell(cell);
619 int const column1 = column_of_cell(cell);
620 int const column2 = right_column_of_cell(cell);
622 // first set columns to 0 so we can calculate the right width
623 for (int i = column1; i <= column2; ++i) {
624 cell_info[row][i].width_of_cell = 0;
626 // set the width to MAX_WIDTH until width > 0
627 int width = (new_width + 2 * WIDTH_OF_LINE);
630 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
631 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
632 width -= column_info[i].width_of_column;
635 cell_info[row][i].width_of_cell = width;
641 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
643 int const row = row_of_cell(cell);
644 int const column1 = column_of_cell(cell);
645 int const column2 = right_column_of_cell(cell);
647 // first set columns to 0 so we can calculate the right width
649 for (; i <= column2; ++i)
650 cell_info[row][i].width_of_cell = 0;
651 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
653 if (i < numberofcells)
654 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
655 SetWidthOfMulticolCell(cell, new_width);
659 /* returns 1 if a complete update is necessary, otherwise 0 */
660 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
662 int const row = row_of_cell(cell);
663 int const column1 = column_of_cell(cell);
668 #ifdef SPECIAL_COLUM_HANDLING
669 if (RightLine(cell_info[row][column1].cellno, true) &&
670 (column1 < columns_-1) &&
671 LeftLine(cell_info[row][column1+1].cellno, true))
673 if (column_info[column1].right_line && (column1 < columns_-1) &&
674 column_info[column1+1].left_line) // additional width
678 add_width = WIDTH_OF_LINE;
680 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
683 if (IsMultiColumn(cell, true)) {
684 tmp = SetWidthOfMulticolCell(cell, new_width);
686 width = (new_width + 2*WIDTH_OF_LINE + add_width);
687 cell_info[row][column1].width_of_cell = width;
688 tmp = calculate_width_of_column_NMC(column1);
692 for (; i<columns_; ++i)
693 calculate_width_of_column_NMC(i);
694 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
696 if (i < numberofcells)
697 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
698 for (i = 0; i < columns_; ++i)
699 calculate_width_of_column(i);
700 calculate_width_of_tabular();
707 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
709 if (!IsMultiColumn(cell) || onlycolumn)
710 column_info[column_of_cell(cell)].alignment = align;
712 cellinfo_of_cell(cell)->alignment = align;
717 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
719 if (!IsMultiColumn(cell) || onlycolumn)
720 column_info[column_of_cell(cell)].valignment = align;
722 cellinfo_of_cell(cell)->valignment = align;
727 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
729 bool flag = !width.zero();
730 int const j = column_of_cell(cell);
732 column_info[j].p_width = width;
733 if (flag) // do this only if there is a width
734 SetAlignment(cell, LYX_ALIGN_LEFT);
735 for (int i = 0; i < rows_; ++i) {
736 int c = GetCellNumber(i, j);
737 flag = !GetPWidth(c).zero(); // because of multicolumns!
738 GetCellInset(c)->setAutoBreakRows(flag);
744 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
746 bool const flag = !width.zero();
748 cellinfo_of_cell(cell)->p_width = width;
749 if (IsMultiColumn(cell)) {
750 GetCellInset(cell)->setAutoBreakRows(flag);
757 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
758 LyXTabular::Feature what)
760 if (what == SET_SPECIAL_MULTI)
761 cellinfo_of_cell(cell)->align_special = special;
763 column_info[column_of_cell(cell)].align_special = special;
768 bool LyXTabular::SetAllLines(int cell, bool line)
770 SetTopLine(cell, line);
771 SetBottomLine(cell, line);
772 SetRightLine(cell, line);
773 SetLeftLine(cell, line);
778 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
780 int const row = row_of_cell(cell);
782 if (onlycolumn || !IsMultiColumn(cell))
783 row_info[row].top_line = line;
785 cellinfo_of_cell(cell)->top_line = line;
790 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
792 if (onlycolumn || !IsMultiColumn(cell))
793 row_info[row_of_cell(cell)].bottom_line = line;
795 cellinfo_of_cell(cell)->bottom_line = line;
800 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
802 if (onlycolumn || !IsMultiColumn(cell))
803 column_info[column_of_cell(cell)].left_line = line;
805 cellinfo_of_cell(cell)->left_line = line;
810 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
812 if (onlycolumn || !IsMultiColumn(cell))
813 column_info[right_column_of_cell(cell)].right_line = line;
815 cellinfo_of_cell(cell)->right_line = line;
820 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
822 if (!onlycolumn && IsMultiColumn(cell))
823 return cellinfo_of_cell(cell)->alignment;
825 return column_info[column_of_cell(cell)].alignment;
829 LyXTabular::VAlignment
830 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
832 if (!onlycolumn && IsMultiColumn(cell))
833 return cellinfo_of_cell(cell)->valignment;
835 return column_info[column_of_cell(cell)].valignment;
839 LyXLength const LyXTabular::GetPWidth(int cell) const
841 if (IsMultiColumn(cell))
842 return cellinfo_of_cell(cell)->p_width;
843 return column_info[column_of_cell(cell)].p_width;
847 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
849 return column_info[column_of_cell(cell)].p_width;
853 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
855 if (IsMultiColumn(cell))
856 return cellinfo_of_cell(cell)->p_width;
861 string const LyXTabular::GetAlignSpecial(int cell, int what) const
863 if (what == SET_SPECIAL_MULTI)
864 return cellinfo_of_cell(cell)->align_special;
865 return column_info[column_of_cell(cell)].align_special;
869 int LyXTabular::GetWidthOfCell(int cell) const
871 int const row = row_of_cell(cell);
872 int const column1 = column_of_cell(cell);
873 int const column2 = right_column_of_cell(cell);
875 for (int i = column1; i <= column2; ++i) {
876 result += cell_info[row][i].width_of_cell;
882 int LyXTabular::GetBeginningOfTextInCell(int cell) const
886 switch (GetAlignment(cell)){
887 case LYX_ALIGN_CENTER:
888 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
890 case LYX_ALIGN_RIGHT:
891 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
892 // + GetAdditionalWidth(cell);
894 default: /* LYX_ALIGN_LEFT: nothing :-) */
904 bool LyXTabular::IsFirstCellInRow(int cell) const
906 return column_of_cell(cell) == 0;
910 int LyXTabular::GetFirstCellInRow(int row) const
914 return cell_info[row][0].cellno;
917 bool LyXTabular::IsLastCellInRow(int cell) const
919 return (right_column_of_cell(cell) == (columns_ - 1));
923 int LyXTabular::GetLastCellInRow(int row) const
927 return cell_info[row][columns_-1].cellno;
931 bool LyXTabular::calculate_width_of_column(int column)
933 int const old_column_width = column_info[column].width_of_column;
936 for (int i = 0; i < rows_; ++i) {
937 maximum = max(cell_info[i][column].width_of_cell, maximum);
939 column_info[column].width_of_column = maximum;
940 return (column_info[column].width_of_column != old_column_width);
945 // calculate the with of the column without regarding REAL MultiColumn
946 // cells. This means MultiColumn-cells spanning more than 1 column.
948 bool LyXTabular::calculate_width_of_column_NMC(int column)
950 int const old_column_width = column_info[column].width_of_column;
952 for (int i = 0; i < rows_; ++i) {
953 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
954 (cell_info[i][column].width_of_cell > max)) {
955 max = cell_info[i][column].width_of_cell;
958 column_info[column].width_of_column = max;
959 return (column_info[column].width_of_column != old_column_width);
963 void LyXTabular::calculate_width_of_tabular()
965 width_of_tabular = 0;
966 for (int i = 0; i < columns_; ++i) {
967 width_of_tabular += column_info[i].width_of_column;
972 int LyXTabular::row_of_cell(int cell) const
974 if (cell >= numberofcells)
978 return rowofcell[cell];
982 int LyXTabular::column_of_cell(int cell) const
984 if (cell >= numberofcells)
988 return columnofcell[cell];
992 int LyXTabular::right_column_of_cell(int cell) const
994 int const row = row_of_cell(cell);
995 int column = column_of_cell(cell);
996 while (column < (columns_ - 1) &&
997 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1003 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1007 << write_attribute("version", 3)
1008 << write_attribute("rows", rows_)
1009 << write_attribute("columns", columns_)
1011 // global longtable options
1013 << write_attribute("rotate", tostr(rotate))
1014 << write_attribute("islongtable", tostr(is_long_tabular))
1015 << write_attribute("firstHeadTopDL", tostr(endfirsthead.topDL))
1016 << write_attribute("firstHeadBottomDL", tostr(endfirsthead.bottomDL))
1017 << write_attribute("firstHeadEmpty", tostr(endfirsthead.empty))
1018 << write_attribute("headTopDL", tostr(endhead.topDL))
1019 << write_attribute("headBottomDL", tostr(endhead.bottomDL))
1020 << write_attribute("footTopDL", tostr(endfoot.topDL))
1021 << write_attribute("footBottomDL", tostr(endfoot.bottomDL))
1022 << write_attribute("lastFootTopDL", tostr(endlastfoot.topDL))
1023 << write_attribute("lastFootBottomDL", tostr(endlastfoot.bottomDL))
1024 << write_attribute("lastFootEmpty", tostr(endlastfoot.empty))
1026 for (int j = 0; j < columns_; ++j) {
1028 << write_attribute("alignment", tostr(column_info[j].alignment))
1029 << write_attribute("valignment", tostr(column_info[j].valignment))
1030 << write_attribute("leftline", tostr(column_info[j].left_line))
1031 << write_attribute("rightline", tostr(column_info[j].right_line))
1032 << write_attribute("width", column_info[j].p_width.asString())
1033 << write_attribute("special", column_info[j].align_special)
1036 for (int i = 0; i < rows_; ++i) {
1038 << write_attribute("topline", tostr(row_info[i].top_line))
1039 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1040 << write_attribute("endhead", tostr(row_info[i].endhead))
1041 << write_attribute("endfirsthead", tostr(row_info[i].endfirsthead))
1042 << write_attribute("endfoot", tostr(row_info[i].endfoot))
1043 << write_attribute("endlastfoot", tostr(row_info[i].endlastfoot))
1044 << write_attribute("newpage", tostr(row_info[i].newpage))
1046 for (int j = 0; j < columns_; ++j) {
1048 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1049 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1050 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1051 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1052 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1053 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1054 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1055 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1056 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1057 << write_attribute("width", cell_info[i][j].p_width)
1058 << write_attribute("special", cell_info[i][j].align_special)
1060 os << "\\begin_inset ";
1061 cell_info[i][j].inset.write(buf, os);
1062 os << "\n\\end_inset \n"
1067 os << "</lyxtabular>\n";
1071 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1074 istream & is = lex.getStream();
1076 l_getline(is, line);
1077 if (!prefixIs(line, "<lyxtabular ")
1078 && !prefixIs(line, "<LyXTabular ")) {
1079 OldFormatRead(lex, line);
1084 if (!getTokenValue(line, "version", version))
1087 ReadOld(buf, is, lex, line);
1088 else if (version >= 2)
1089 ReadNew(buf, is, lex, line, version);
1092 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1096 row_info[--hr].endhead = true;
1098 // set firstheader info
1099 if (fhr && (fhr < rows_)) {
1100 if (row_info[fhr].endhead) {
1102 row_info[--fhr].endfirsthead = true;
1103 row_info[fhr].endhead = false;
1105 } else if (row_info[fhr-1].endhead) {
1106 endfirsthead.empty = true;
1108 while((fhr > 0) && !row_info[--fhr].endhead) {
1109 row_info[fhr].endfirsthead = true;
1114 if (fr && (fr < rows_)) {
1115 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1116 while((fr > 0) && !row_info[--fr].endhead) {
1117 row_info[fr].endfoot = true;
1118 row_info[fr].endhead = false;
1120 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1121 while((fr > 0) && !row_info[--fr].endfirsthead) {
1122 row_info[fr].endfoot = true;
1123 row_info[fr].endfirsthead = false;
1125 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead) {
1126 while((fr > 0) && !row_info[--fr].endhead &&
1127 !row_info[fr].endfirsthead)
1129 row_info[fr].endfoot = true;
1133 // set lastfooter info
1134 if (lfr && (lfr < rows_)) {
1135 if (row_info[lfr].endhead && row_info[lfr-1].endhead) {
1136 while((lfr > 0) && !row_info[--lfr].endhead) {
1137 row_info[lfr].endlastfoot = true;
1138 row_info[lfr].endhead = false;
1140 } else if (row_info[lfr].endfirsthead &&
1141 row_info[lfr-1].endfirsthead)
1143 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1144 row_info[lfr].endlastfoot = true;
1145 row_info[lfr].endfirsthead = false;
1147 } else if (row_info[lfr].endfoot && row_info[lfr-1].endfoot) {
1148 while((lfr > 0) && !row_info[--lfr].endfoot) {
1149 row_info[lfr].endlastfoot = true;
1150 row_info[lfr].endfoot = false;
1152 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead &&
1153 !row_info[fr-1].endfoot)
1156 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1157 !row_info[lfr].endfoot)
1159 row_info[lfr].endlastfoot = true;
1161 } else if (haveLTFoot()) {
1162 endlastfoot.empty = true;
1167 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1168 LyXLex & lex, string const & l, int const version)
1172 if (!getTokenValue(line, "rows", rows_arg))
1175 if (!getTokenValue(line, "columns", columns_arg))
1177 Init(rows_arg, columns_arg);
1178 l_getline(is, line);
1179 if (!prefixIs(line, "<features")) {
1180 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1181 line << ")" << endl;
1184 getTokenValue(line, "rotate", rotate);
1185 getTokenValue(line, "islongtable", is_long_tabular);
1186 // compatibility read for old longtable options. Now we can make any
1187 // row part of the header/footer type we want before it was strict
1188 // sequential from the first row down (as LaTeX does it!). So now when
1189 // we find a header/footer line we have to go up the rows and set it
1190 // on all preceding rows till the first or one with already a h/f option
1191 // set. If we find a firstheader on the same line as a header or a
1192 // lastfooter on the same line as a footer then this should be set empty.
1200 getTokenValue(line, "endhead", hrow);
1201 getTokenValue(line, "endfirsthead", fhrow);
1202 getTokenValue(line, "endfoot", frow);
1203 getTokenValue(line, "endlastfoot", lfrow);
1204 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1206 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1207 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1208 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1209 getTokenValue(line, "headTopDL", endhead.topDL);
1210 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1211 getTokenValue(line, "footTopDL", endfoot.topDL);
1212 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1213 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1214 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1215 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1217 for (int j = 0; j < columns_; ++j) {
1219 if (!prefixIs(line,"<column")) {
1220 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1221 line << ")" << endl;
1224 getTokenValue(line, "alignment", column_info[j].alignment);
1225 getTokenValue(line, "valignment", column_info[j].valignment);
1226 getTokenValue(line, "leftline", column_info[j].left_line);
1227 getTokenValue(line, "rightline", column_info[j].right_line);
1228 getTokenValue(line, "width", column_info[j].p_width);
1229 getTokenValue(line, "special", column_info[j].align_special);
1232 for (int i = 0; i < rows_; ++i) {
1233 l_getline(is, line);
1234 if (!prefixIs(line, "<row")) {
1235 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1236 line << ")" << endl;
1239 getTokenValue(line, "topline", row_info[i].top_line);
1240 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1241 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1242 getTokenValue(line, "endhead", row_info[i].endhead);
1243 getTokenValue(line, "endfoot", row_info[i].endfoot);
1244 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1245 getTokenValue(line, "newpage", row_info[i].newpage);
1246 for (int j = 0; j < columns_; ++j) {
1247 l_getline(is, line);
1248 if (!prefixIs(line, "<cell")) {
1249 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1250 line << ")" << endl;
1253 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1254 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1255 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1256 getTokenValue(line, "topline", cell_info[i][j].top_line);
1257 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1258 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1259 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1260 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1261 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1262 getTokenValue(line, "width", cell_info[i][j].p_width);
1263 getTokenValue(line, "special", cell_info[i][j].align_special);
1264 l_getline(is, line);
1265 if (prefixIs(line, "\\begin_inset")) {
1266 cell_info[i][j].inset.read(buf, lex);
1267 l_getline(is, line);
1269 if (!prefixIs(line, "</cell>")) {
1270 lyxerr << "Wrong tabular format (expected </cell> got" <<
1271 line << ")" << endl;
1275 l_getline(is, line);
1276 if (!prefixIs(line, "</row>")) {
1277 lyxerr << "Wrong tabular format (expected </row> got" <<
1278 line << ")" << endl;
1282 while (!prefixIs(line, "</lyxtabular>")) {
1283 l_getline(is, line);
1285 set_row_column_number_info();
1289 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1295 int columns_arg = 0;
1296 int is_long_tabular_arg = false;
1297 int rotate_arg = false;
1306 istream & is = lex.getStream();
1309 version = lyx::atoi(s.substr(8, string::npos));
1313 vector<int> cont_row_info;
1316 lyxerr << "Tabular format < 5 is not supported anymore\n"
1317 "Get an older version of LyX (< 1.1.x) for conversion!"
1319 Alert::alert(_("Warning:"),
1320 _("Tabular format < 5 is not supported anymore\n"),
1321 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1323 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1324 >> rotate_arg >> a >> b >> c >> d;
1326 is >> rows_arg >> columns_arg;
1327 Init(rows_arg, columns_arg);
1328 cont_row_info = vector<int>(rows_arg);
1329 SetLongTabular(is_long_tabular_arg);
1330 SetRotateTabular(rotate_arg);
1332 for (i = 0; i < rows_; ++i) {
1334 cont_row_info[i] = false;
1336 for (i = 0; i < columns_; ++i) {
1339 for (i = 0; i < rows_; ++i) {
1340 for (j = 0; j < columns_; ++j) {
1345 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1346 >> rotate_arg >> a >> b >> c >> d;
1347 Init(rows_arg, columns_arg);
1348 cont_row_info = vector<int>(rows_arg);
1349 SetLongTabular(is_long_tabular_arg);
1350 SetRotateTabular(rotate_arg);
1351 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1352 for (i = 0; i < rows_; ++i) {
1353 a = b = c = d = e = f = g = 0;
1354 is >> a >> b >> c >> d;
1355 row_info[i].top_line = a;
1356 row_info[i].bottom_line = b;
1357 cont_row_info[i] = c;
1358 row_info[i].newpage = d;
1360 for (i = 0; i < columns_; ++i) {
1365 char ch; // skip '"'
1368 // ignore is buggy but we will use it later (Lgb)
1369 is.ignore(); // skip '"'
1371 getline(is, s1, '"');
1373 is >> ch; // skip '"'
1375 // ignore is buggy but we will use it later (Lgb)
1376 is.ignore(); // skip '"'
1378 getline(is, s2, '"');
1379 column_info[i].alignment = static_cast<LyXAlignment>(a);
1380 column_info[i].left_line = b;
1381 column_info[i].right_line = c;
1382 column_info[i].p_width = LyXLength(s1);
1383 column_info[i].align_special = s2;
1385 for (i = 0; i < rows_; ++i) {
1386 for (j = 0; j < columns_; ++j) {
1389 is >> a >> b >> c >> d >> e >> f >> g;
1392 is >> ch; // skip '"'
1394 // ignore is buggy but we will use it later (Lgb)
1395 is.ignore(); // skip '"'
1397 getline(is, s1, '"');
1399 is >> ch; // skip '"'
1401 // ignore is buggy but we will use it later (Lgb)
1402 is.ignore(); // skip '"'
1404 getline(is, s2, '"');
1405 cell_info[i][j].multicolumn = static_cast<char>(a);
1406 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1407 cell_info[i][j].top_line = static_cast<char>(c);
1408 cell_info[i][j].bottom_line = static_cast<char>(d);
1409 cell_info[i][j].left_line = column_info[j].left_line;
1410 cell_info[i][j].right_line = column_info[j].right_line;
1411 cell_info[i][j].rotate = static_cast<bool>(f);
1412 cell_info[i][j].usebox = static_cast<BoxType>(g);
1413 cell_info[i][j].align_special = s1;
1414 cell_info[i][j].p_width = LyXLength(s2);
1418 set_row_column_number_info(true);
1420 Paragraph * par = new Paragraph;
1421 Paragraph * return_par = 0;
1425 Paragraph::depth_type depth = 0;
1426 LyXFont font(LyXFont::ALL_INHERIT);
1427 font.setLanguage(owner_->bufferOwner()->getLanguage());
1429 while (lex.isOK()) {
1431 string const token = lex.getString();
1434 if (token == "\\layout"
1435 || token == "\\end_float"
1436 || token == "\\end_deeper") {
1437 lex.pushToken(token);
1438 #ifndef NO_COMPABILITY
1439 // Here we need to insert the inset_ert_contents into the last
1440 // cell of the tabular.
1441 owner_->bufferOwner()->insertErtContents(par, pos, font);
1445 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1450 lex.pushToken(token);
1454 lex.printError("New Paragraph allocated! This should not happen!");
1455 lex.pushToken(token);
1461 // now we have the par we should fill the insets with this!
1463 InsetText * inset = GetCellInset(cell);
1466 for (int i = 0; i < par->size(); ++i) {
1467 if (par->isNewline(i)) {
1469 if (cell > numberofcells) {
1470 lyxerr << "Some error in reading old table format occured!" <<
1471 endl << "Terminating when reading cell[" << cell << "]!" <<
1476 row = row_of_cell(cell);
1477 if (cont_row_info[row]) {
1479 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1480 while (!IsFirstCellInRow(--cell));
1482 inset = GetCellInset(cell);
1485 inset = GetCellInset(cell);
1486 row = row_of_cell(cell);
1487 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1489 // insert a space instead
1491 par->insertChar(i, ' ');
1494 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1495 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1502 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1504 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1505 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1509 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1511 int const row = row_of_cell(cell);
1512 int const column = column_of_cell(cell);
1513 return &cell_info[row][column];
1517 void LyXTabular::SetMultiColumn(int cell, int number)
1519 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1520 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1521 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1522 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1523 for (number--; number > 0; --number) {
1524 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1526 set_row_column_number_info();
1530 int LyXTabular::cells_in_multicolumn(int cell) const
1532 int const row = row_of_cell(cell);
1533 int column = column_of_cell(cell);
1536 while ((column < columns_) &&
1537 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1546 int LyXTabular::UnsetMultiColumn(int cell)
1548 int const row = row_of_cell(cell);
1549 int column = column_of_cell(cell);
1553 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1554 cell_info[row][column].multicolumn = CELL_NORMAL;
1556 while ((column < columns_) &&
1557 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1559 cell_info[row][column].multicolumn = CELL_NORMAL;
1564 set_row_column_number_info();
1569 void LyXTabular::SetLongTabular(bool what)
1571 is_long_tabular = what;
1575 bool LyXTabular::IsLongTabular() const
1577 return is_long_tabular;
1581 void LyXTabular::SetRotateTabular(bool flag)
1587 bool LyXTabular::GetRotateTabular() const
1593 void LyXTabular::SetRotateCell(int cell, bool flag)
1595 cellinfo_of_cell(cell)->rotate = flag;
1599 bool LyXTabular::GetRotateCell(int cell) const
1601 return cellinfo_of_cell(cell)->rotate;
1605 bool LyXTabular::NeedRotating() const
1609 for (int i = 0; i < rows_; ++i) {
1610 for (int j = 0; j < columns_; ++j) {
1611 if (cell_info[i][j].rotate)
1619 bool LyXTabular::IsLastCell(int cell) const
1621 if ((cell + 1) < numberofcells)
1627 int LyXTabular::GetCellAbove(int cell) const
1629 if (row_of_cell(cell) > 0)
1630 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1635 int LyXTabular::GetCellBelow(int cell) const
1637 if (row_of_cell(cell) + 1 < rows_)
1638 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1643 int LyXTabular::GetLastCellAbove(int cell) const
1645 if (row_of_cell(cell) <= 0)
1647 if (!IsMultiColumn(cell))
1648 return GetCellAbove(cell);
1649 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1653 int LyXTabular::GetLastCellBelow(int cell) const
1655 if (row_of_cell(cell) + 1 >= rows_)
1657 if (!IsMultiColumn(cell))
1658 return GetCellBelow(cell);
1659 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1663 int LyXTabular::GetCellNumber(int row, int column) const
1666 if (column >= columns_)
1667 column = columns_ - 1;
1668 else if (column < 0)
1675 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1677 return cell_info[row][column].cellno;
1681 void LyXTabular::SetUsebox(int cell, BoxType type)
1683 cellinfo_of_cell(cell)->usebox = type;
1687 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1689 if (column_info[column_of_cell(cell)].p_width.zero() &&
1690 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1692 if (cellinfo_of_cell(cell)->usebox > 1)
1693 return cellinfo_of_cell(cell)->usebox;
1694 return UseParbox(cell);
1699 // This are functions used for the longtable support
1701 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1706 row_info[row].endfirsthead = flag;
1710 row_info[row].endhead = flag;
1715 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1718 hd.set = haveLTHead();
1719 return row_info[row].endhead;
1723 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1726 hd.set = haveLTFirstHead();
1727 return row_info[row].endfirsthead;
1731 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1736 row_info[row].endlastfoot = flag;
1740 row_info[row].endfoot = flag;
1745 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1748 fd.set = haveLTFoot();
1749 return row_info[row].endfoot;
1753 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1756 fd.set = haveLTLastFoot();
1757 return row_info[row].endlastfoot;
1761 void LyXTabular::SetLTNewPage(int row, bool what)
1763 row_info[row].newpage = what;
1767 bool LyXTabular::GetLTNewPage(int row) const
1769 return row_info[row].newpage;
1773 bool LyXTabular::haveLTHead() const
1775 for(int i=0; i < rows_; ++i) {
1776 if (row_info[i].endhead)
1783 bool LyXTabular::haveLTFirstHead() const
1785 if (endfirsthead.empty)
1787 for(int i=0; i < rows_; ++i) {
1788 if (row_info[i].endfirsthead)
1795 bool LyXTabular::haveLTFoot() const
1797 for(int i=0; i < rows_; ++i) {
1798 if (row_info[i].endfoot)
1805 bool LyXTabular::haveLTLastFoot() const
1807 if (endlastfoot.empty)
1809 for(int i=0; i < rows_; ++i) {
1810 if (row_info[i].endlastfoot)
1817 // end longtable support functions
1819 bool LyXTabular::SetAscentOfRow(int row, int height)
1821 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1823 row_info[row].ascent_of_row = height;
1828 bool LyXTabular::SetDescentOfRow(int row, int height)
1830 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1832 row_info[row].descent_of_row = height;
1837 int LyXTabular::GetAscentOfRow(int row) const
1841 return row_info[row].ascent_of_row;
1845 int LyXTabular::GetDescentOfRow(int row) const
1849 return row_info[row].descent_of_row;
1853 int LyXTabular::GetHeightOfTabular() const
1857 for (int row = 0; row < rows_; ++row)
1858 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1859 GetAdditionalHeight(row);
1864 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1866 if ((row >= rows_) || (column >= columns_))
1868 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1872 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1874 if ((row < 0) || (row >= rows_))
1877 int const fcell = GetFirstCellInRow(row);
1878 int const n = NumberOfCellsInRow(fcell) + fcell;
1881 for (int i = fcell; i < n; ++i) {
1885 if (tmp == (n - fcell)){
1888 for (int i = fcell; i < n; ++i) {
1891 << column_of_cell(i) + 1
1893 << right_column_of_cell(i) + 1
1905 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1907 if ((row < 0) || (row >= rows_))
1910 int const fcell = GetFirstCellInRow(row);
1911 int const n = NumberOfCellsInRow(fcell) + fcell;
1914 for (int i = fcell; i < n; ++i) {
1918 if (tmp == (n-fcell)){
1921 for (int i = fcell; i < n; ++i) {
1922 if (BottomLine(i)) {
1924 << column_of_cell(i) + 1
1926 << right_column_of_cell(i) + 1
1938 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1942 if (GetRotateCell(cell)) {
1943 os << "\\begin{sideways}\n";
1946 if (IsMultiColumn(cell)) {
1947 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1948 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1949 os << cellinfo_of_cell(cell)->align_special << "}{";
1951 if (LeftLine(cell) &&
1952 (IsFirstCellInRow(cell) ||
1953 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1954 !RightLine(cell-1, true))))
1958 if (!GetPWidth(cell).zero()) {
1959 switch (GetVAlignment(cell)) {
1960 case LYX_VALIGN_TOP:
1963 case LYX_VALIGN_CENTER:
1966 case LYX_VALIGN_BOTTOM:
1970 os << "{" << GetPWidth(cell).asLatexString() << '}';
1972 switch (GetAlignment(cell)) {
1973 case LYX_ALIGN_LEFT:
1976 case LYX_ALIGN_RIGHT:
1984 if (RightLine(cell))
1986 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1992 if (GetUsebox(cell) == BOX_PARBOX) {
1994 switch (GetVAlignment(cell)) {
1995 case LYX_VALIGN_TOP:
1998 case LYX_VALIGN_CENTER:
2001 case LYX_VALIGN_BOTTOM:
2005 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2006 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2007 os << "\\begin{minipage}[";
2008 switch (GetVAlignment(cell)) {
2009 case LYX_VALIGN_TOP:
2012 case LYX_VALIGN_CENTER:
2015 case LYX_VALIGN_BOTTOM:
2019 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2026 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2031 if (GetUsebox(cell) == BOX_PARBOX)
2033 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2034 os << "%\n\\end{minipage}";
2037 if (IsMultiColumn(cell)){
2040 if (GetRotateCell(cell)) {
2041 os << "%\n\\end{sideways}";
2048 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2049 bool fragile, bool fp) const
2051 if (!is_long_tabular)
2055 // output header info
2057 if (endhead.topDL) {
2061 for (int i = 0; i < rows_; ++i) {
2062 if (row_info[i].endhead) {
2063 ret += TeXRow(os, i, buf, fragile, fp);
2066 if (endhead.bottomDL) {
2070 os << "\\endhead\n";
2072 if (endfirsthead.empty) {
2073 os << "\\endfirsthead\n";
2077 // output firstheader info
2078 if (haveLTFirstHead()) {
2079 if (endfirsthead.topDL) {
2083 for (int i = 0; i < rows_; ++i) {
2084 if (row_info[i].endfirsthead) {
2085 ret += TeXRow(os, i, buf, fragile, fp);
2088 if (endfirsthead.bottomDL) {
2092 os << "\\endfirsthead\n";
2095 // output footer info
2097 if (endfoot.topDL) {
2101 for (int i = 0; i < rows_; ++i) {
2102 if (row_info[i].endfoot) {
2103 ret += TeXRow(os, i, buf, fragile, fp);
2106 if (endfoot.bottomDL) {
2110 os << "\\endfoot\n";
2112 if (endlastfoot.empty) {
2113 os << "\\endlastfoot\n";
2117 // output lastfooter info
2118 if (haveLTLastFoot()) {
2119 if (endlastfoot.topDL) {
2123 for (int i = 0; i < rows_; ++i) {
2124 if (row_info[i].endlastfoot) {
2125 ret += TeXRow(os, i, buf, fragile, fp);
2128 if (endlastfoot.bottomDL) {
2132 os << "\\endlastfoot\n";
2139 bool LyXTabular::isValidRow(int const row) const
2141 if (!is_long_tabular)
2143 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2144 !row_info[row].endfoot && !row_info[row].endlastfoot);
2148 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2149 bool fragile, bool fp) const
2152 int cell = GetCellNumber(i, 0);
2154 ret += TeXTopHLine(os, i);
2155 for (int j = 0; j < columns_; ++j) {
2156 if (IsPartOfMultiColumn(i,j))
2158 ret += TeXCellPreamble(os, cell);
2159 InsetText * inset = GetCellInset(cell);
2161 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2162 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2166 ret += inset->latex(buf, os, fragile, fp);
2170 ret += TeXCellPostamble(os, cell);
2171 if (!IsLastCellInRow(cell)) { // not last cell in row
2179 ret += TeXBottomHLine(os, i);
2184 int LyXTabular::Latex(Buffer const * buf,
2185 ostream & os, bool fragile, bool fp) const
2189 //+---------------------------------------------------------------------
2190 //+ first the opening preamble +
2191 //+---------------------------------------------------------------------
2194 os << "\\begin{sideways}\n";
2197 if (is_long_tabular)
2198 os << "\\begin{longtable}{";
2200 os << "\\begin{tabular}{";
2201 for (int i = 0; i < columns_; ++i) {
2202 if (!column_info[i].align_special.empty()) {
2203 os << column_info[i].align_special;
2205 if (column_info[i].left_line)
2207 if (!column_info[i].p_width.zero()) {
2208 switch (column_info[i].valignment) {
2209 case LYX_VALIGN_TOP:
2212 case LYX_VALIGN_CENTER:
2215 case LYX_VALIGN_BOTTOM:
2220 << column_info[i].p_width.asLatexString()
2223 switch (column_info[i].alignment) {
2224 case LYX_ALIGN_LEFT:
2227 case LYX_ALIGN_RIGHT:
2235 if (column_info[i].right_line)
2242 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2244 //+---------------------------------------------------------------------
2245 //+ the single row and columns (cells) +
2246 //+---------------------------------------------------------------------
2248 for (int i = 0; i < rows_; ++i) {
2249 if (isValidRow(i)) {
2250 ret += TeXRow(os, i, buf, fragile, fp);
2251 if (is_long_tabular && row_info[i].newpage) {
2252 os << "\\newpage\n";
2258 //+---------------------------------------------------------------------
2259 //+ the closing of the tabular +
2260 //+---------------------------------------------------------------------
2262 if (is_long_tabular)
2263 os << "\\end{longtable}";
2265 os << "\\end{tabular}";
2267 os << "\n\\end{sideways}";
2275 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2278 int cell = GetFirstCellInRow(row);
2281 for (int j = 0; j < columns_; ++j) {
2282 if (IsPartOfMultiColumn(row, j))
2285 os << "<entry align=\"";
2286 switch (GetAlignment(cell)) {
2287 case LYX_ALIGN_LEFT:
2290 case LYX_ALIGN_RIGHT:
2298 os << "\" valign=\"";
2299 switch (GetVAlignment(cell)) {
2300 case LYX_VALIGN_TOP:
2303 case LYX_VALIGN_BOTTOM:
2306 case LYX_VALIGN_CENTER:
2311 if (IsMultiColumn(cell)) {
2312 os << " namest=\"col" << j << "\" ";
2313 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2317 ret += GetCellInset(cell)->docbook(buf, os);
2326 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2330 //+---------------------------------------------------------------------
2331 //+ first the opening preamble +
2332 //+---------------------------------------------------------------------
2334 os << "<tgroup cols=\"" << columns_
2335 << "\" colsep=\"1\" rowsep=\"1\">\n";
2337 for (int i = 0; i < columns_; ++i) {
2338 os << "<colspec colname=\"col" << i << "\" align=\"";
2339 switch (column_info[i].alignment) {
2340 case LYX_ALIGN_LEFT:
2343 case LYX_ALIGN_RIGHT:
2354 //+---------------------------------------------------------------------
2355 //+ Long Tabular case +
2356 //+---------------------------------------------------------------------
2358 #warning Jose please have a look here I changed the longtable header/footer
2359 #warning ---- options so I had to disable the docbook code (Jug 20011219)
2361 if ( IsLongTabular() ) {
2363 if(endhead.row || endfirsthead.row ) {
2365 if( endfirsthead.row ) {
2366 docbookRow( buf, os, endfirsthead.row - 1);
2368 if( endhead.row && endhead.row != endfirsthead.row) {
2369 docbookRow(buf, os, endhead.row - 1);
2375 if( endfoot.row || endlastfoot.row ) {
2378 docbookRow( buf, os, endfoot.row - 1);
2380 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2381 docbookRow( buf, os, endlastfoot.row - 1);
2387 //+---------------------------------------------------------------------
2388 //+ the single row and columns (cells) +
2389 //+---------------------------------------------------------------------
2392 for (int i = 0; i < rows_; ++i) {
2393 if(!IsLongTabular() || (
2394 !row_info[i].endhead && !row_info[i].endfirsthead &&
2395 !row_info[i].endfoot && !row_info[i].endlastfoot)) {
2396 docbookRow( buf, os, i);
2400 //+---------------------------------------------------------------------
2401 //+ the closing of the tabular +
2402 //+---------------------------------------------------------------------
2414 void print_n_chars(ostream & os, unsigned char ch, int n)
2416 os << string(n, ch);
2422 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2423 vector<unsigned int> const & clen) const
2425 int const fcell = GetFirstCellInRow(row);
2426 int const n = NumberOfCellsInRow(fcell) + fcell;
2429 for (int i = fcell; i < n; ++i) {
2439 for (int i = fcell; i < n; ++i) {
2450 int column = column_of_cell(i);
2451 int len = clen[column];
2452 while (IsPartOfMultiColumn(row, ++column))
2453 len += clen[column] + 4;
2454 print_n_chars(os, ch, len);
2469 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2470 vector<unsigned int> const & clen) const
2472 int const fcell = GetFirstCellInRow(row);
2473 int const n = NumberOfCellsInRow(fcell) + fcell;
2476 for (int i = fcell; i < n; ++i) {
2477 if (BottomLine(i)) {
2486 for (int i = fcell; i < n; ++i) {
2487 if (BottomLine(i)) {
2497 int column = column_of_cell(i);
2498 int len = clen[column];
2499 while (IsPartOfMultiColumn(row, ++column))
2500 len += clen[column] + 4;
2501 print_n_chars(os, ch, len);
2502 if (BottomLine(i)) {
2516 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2517 int cell, int row, int column,
2518 vector<unsigned int> const & clen) const
2521 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2528 unsigned int len1 = sstr.str().length();
2529 unsigned int len2 = clen[column];
2530 while (IsPartOfMultiColumn(row, ++column))
2531 len2 += clen[column] + 4;
2534 switch (GetAlignment(cell)) {
2536 case LYX_ALIGN_LEFT:
2539 case LYX_ALIGN_RIGHT:
2543 case LYX_ALIGN_CENTER:
2549 for (unsigned int i = 0; i < len1; ++i)
2552 for (unsigned int i = 0; i < len2; ++i)
2554 if (RightLine(cell))
2563 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2567 //+---------------------------------------------------------------------
2568 //+ first calculate the width of the single columns +
2569 //+---------------------------------------------------------------------
2570 vector<unsigned int> clen(columns_);
2572 // first all non (real) multicolumn cells!
2573 for (int j = 0; j < columns_; ++j) {
2575 for (int i = 0; i < rows_; ++i) {
2576 int cell = GetCellNumber(i, j);
2577 if (IsMultiColumn(cell, true))
2580 GetCellInset(cell)->ascii(buf, sstr, 0);
2581 if (clen[j] < sstr.str().length())
2582 clen[j] = sstr.str().length();
2585 // then all (real) multicolumn cells!
2586 for (int j = 0; j < columns_; ++j) {
2587 for (int i = 0; i < rows_; ++i) {
2588 int cell = GetCellNumber(i, j);
2589 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2592 GetCellInset(cell)->ascii(buf, sstr, 0);
2593 int len = int(sstr.str().length());
2594 int const n = cells_in_multicolumn(cell);
2595 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2597 if (len > int(clen[j + n - 1]))
2598 clen[j + n - 1] = len;
2602 for (int i = 0; i < rows_; ++i) {
2603 AsciiTopHLine(os, i, clen);
2604 for (int j = 0; j < columns_; ++j) {
2605 if (IsPartOfMultiColumn(i,j))
2607 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2611 AsciiBottomHLine(os, i, clen);
2617 InsetText * LyXTabular::GetCellInset(int cell) const
2620 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2624 InsetText * LyXTabular::GetCellInset(int row, int column) const
2626 cur_cell = GetCellNumber(row, column);
2627 return & cell_info[row][column].inset;
2631 void LyXTabular::Validate(LaTeXFeatures & features) const
2633 if (IsLongTabular())
2634 features.require("longtable");
2636 features.require("rotating");
2637 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2638 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2639 features.require("array");
2640 GetCellInset(cell)->validate(features);
2645 std::vector<string> const LyXTabular::getLabelList() const
2647 std::vector<string> label_list;
2648 for (int i = 0; i < rows_; ++i)
2649 for (int j = 0; j < columns_; ++j) {
2650 std::vector<string> const l =
2651 GetCellInset(i, j)->getLabelList();
2652 label_list.insert(label_list.end(),
2653 l.begin(), l.end());
2659 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2661 Paragraph * par = GetCellInset(cell)->paragraph();
2663 for (; par; par = par->next()) {
2664 for (int i = 0; i < par->size(); ++i) {
2665 if (par->getChar(i) == Paragraph::META_NEWLINE)