1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2002 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
15 // temporary until verified (08/08/2001 Jug)
16 #define SPECIAL_COLUM_HANDLING 1
23 #include "BufferView.h"
24 #include "frontends/Painter.h"
25 #include "LaTeXFeatures.h"
26 #include "insets/insettabular.h"
27 #include "insets/insettext.h"
28 #include "support/lstrings.h"
29 #include "support/lyxmanip.h"
30 #include "support/LAssert.h"
31 #include "frontends/Alert.h"
34 #include "tabular_funcs.h"
48 #ifndef CXX_GLOBAL_CSTD
54 int const WIDTH_OF_LINE = 5;
58 /// Define a few methods for the inner structs
60 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
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()
109 LyXTabular::LyXTabular(BufferParams const & bp,
110 InsetTabular * inset, int rows_arg, int columns_arg)
114 init(bp, rows_arg, columns_arg);
118 LyXTabular::LyXTabular(BufferParams const & bp,
119 InsetTabular * inset, LyXTabular const & lt)
123 init(bp, lt.rows_, lt.columns_, <);
126 #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)
133 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
141 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
144 #warning This whole method should look like this: (Lgb)
149 // If this and lt is not of the same size we have a serious bug
150 // So then it is ok to throw an exception, or for now
152 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
154 cell_info = lt.cell_info;
155 row_info = lt.row_info;
156 column_info = lt.column_info;
157 setLongTabular(lt.is_long_tabular);
166 LyXTabular * LyXTabular::clone(BufferParams const & bp,
167 InsetTabular * inset)
169 LyXTabular * result = new LyXTabular(bp, inset, *this);
171 // don't know if this is good but I need to Clone also
172 // the text-insets here, this is for the Undo-facility!
173 for (int i = 0; i < rows_; ++i) {
174 for (int j = 0; j < columns_; ++j) {
175 result->cell_info[i][j].inset = cell_info[i][j].inset;
176 result->cell_info[i][j].inset.setOwner(inset);
184 // activates all lines and sets all widths to 0
185 void LyXTabular::init(BufferParams const & bp,
186 int rows_arg, int columns_arg, LyXTabular const * lt)
189 columns_ = columns_arg;
190 row_info = row_vector(rows_, rowstruct());
191 column_info = column_vector(columns_, columnstruct());
192 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
200 for (int i = 0; i < rows_; ++i) {
201 for (int j = 0; j < columns_; ++j) {
202 cell_info[i][j].inset.setOwner(owner_);
203 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
204 cell_info[i][j].cellno = cellno++;
206 cell_info[i].back().right_line = true;
208 row_info.back().bottom_line = true;
209 row_info.front().bottom_line = true;
211 for (int i = 0; i < columns_; ++i) {
212 calculate_width_of_column(i);
214 column_info.back().right_line = true;
216 calculate_width_of_tabular();
218 rowofcell = vector<int>();
219 columnofcell = vector<int>();
220 set_row_column_number_info();
221 is_long_tabular = false;
226 void LyXTabular::appendRow(BufferParams const & bp, int cell)
230 int row = row_of_cell(cell);
232 row_vector::iterator rit = row_info.begin() + row;
233 row_info.insert(rit, rowstruct());
234 // now set the values of the row before
235 row_info[row] = row_info[row + 1];
238 cell_vvector::iterator cit = cell_info.begin() + row;
239 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
241 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
244 for (int i = 0; i <= row; ++i) {
245 for (int j = 0; j < columns_; ++j) {
246 c_info[i][j] = cell_info[i][j];
249 for (int i = row + 1; i < rows_; ++i) {
250 for (int j = 0; j < columns_; ++j) {
251 c_info[i][j] = cell_info[i-1][j];
256 for (int j = 0; j < columns_; ++j) {
257 cell_info[row][j].inset.clear(false);
258 if (bp.tracking_changes)
259 cell_info[row][j].inset.markNew(true);
266 void LyXTabular::deleteRow(int row)
268 if (rows_ == 1) return; // Not allowed to delete last row
270 row_info.erase(row_info.begin() + row); //&row_info[row]);
271 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
277 void LyXTabular::appendColumn(BufferParams const & bp, int cell)
281 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
283 int const column = column_of_cell(cell);
284 column_vector::iterator cit = column_info.begin() + column + 1;
285 column_info.insert(cit, columnstruct());
286 // set the column values of the column before
287 column_info[column + 1] = column_info[column];
289 for (int i = 0; i < rows_; ++i) {
290 for (int j = 0; j <= column; ++j) {
291 c_info[i][j] = cell_info[i][j];
293 for (int j = column + 1; j < columns_; ++j) {
294 c_info[i][j] = cell_info[i][j - 1];
296 // care about multicolumns
297 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
299 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
301 if ((column + 2) >= columns_ ||
302 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
304 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
309 for (int i = 0; i < rows_; ++i) {
310 cell_info[i][column + 1].inset.clear(false);
311 if (bp.tracking_changes)
312 cell_info[i][column + 1].inset.markNew(true);
318 void LyXTabular::deleteColumn(int column)
320 // Similar to deleteRow
321 //if (!(columns_ - 1))
323 if (columns_ == 1) return; // Not allowed to delete last column
325 column_info.erase(column_info.begin() + column);
326 for (int i = 0; i < rows_; ++i) {
327 cell_info[i].erase(cell_info[i].begin() + column);
334 void LyXTabular::reinit()
340 void LyXTabular::Reinit(bool reset_widths)
343 for (int i = 0; i < rows_; ++i) {
344 for (int j = 0; j < columns_; ++j) {
345 cell_info[i][j].width_of_cell = 0;
346 cell_info[i][j].inset.setOwner(owner_);
351 for (int i = 0; i < columns_; ++i) {
352 calculate_width_of_column(i);
354 calculate_width_of_tabular();
356 set_row_column_number_info();
360 void LyXTabular::set_row_column_number_info(bool oldformat)
363 for (int row = 0; row < rows_; ++row) {
364 for (int column = 0; column<columns_; ++column) {
365 if (cell_info[row][column].multicolumn
366 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
368 cell_info[row][column].cellno = numberofcells;
371 ++numberofcells; // because this is one more than as we start from 0
373 rowofcell.resize(numberofcells);
374 columnofcell.resize(numberofcells);
376 for (int row = 0, column = 0, c = 0;
377 c < numberofcells && row < rows_ && column < columns_;) {
379 columnofcell[c] = column;
383 } while (column < columns_ &&
384 cell_info[row][column].multicolumn
385 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
386 if (column == columns_) {
392 for (int row = 0; row < rows_; ++row) {
393 for (int column = 0; column < columns_; ++column) {
394 if (isPartOfMultiColumn(row,column))
396 // now set the right line of multicolumns right for oldformat read
398 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
400 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
401 cell_info[row][column].right_line =
402 cell_info[row][column+cn-1].right_line;
404 cell_info[row][column].inset.setAutoBreakRows(
405 !getPWidth(getCellNumber(row, column)).zero());
411 int LyXTabular::getNumberOfCells() const
413 return numberofcells;
417 int LyXTabular::numberOfCellsInRow(int cell) const
419 int const row = row_of_cell(cell);
421 for (int i = 0; i < columns_; ++i) {
422 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
429 // returns 1 if there is a topline, returns 0 if not
430 bool LyXTabular::topLine(int cell, bool onlycolumn) const
432 int const row = row_of_cell(cell);
434 if (!onlycolumn && isMultiColumn(cell))
435 return cellinfo_of_cell(cell)->top_line;
436 return row_info[row].top_line;
440 bool LyXTabular::bottomLine(int cell, bool onlycolumn) const
442 // no bottom line underneath non-existent cells if you please
443 // Isn't that a programming error? Is so this should
444 // be an Assert instead. (Lgb)
445 if (cell >= numberofcells)
448 if (!onlycolumn && isMultiColumn(cell))
449 return cellinfo_of_cell(cell)->bottom_line;
450 return row_info[row_of_cell(cell)].bottom_line;
454 bool LyXTabular::leftLine(int cell, bool onlycolumn) const
456 if (!onlycolumn && isMultiColumn(cell) &&
457 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
459 #ifdef SPECIAL_COLUM_HANDLING
460 if (cellinfo_of_cell(cell)->align_special.empty())
461 return cellinfo_of_cell(cell)->left_line;
462 return prefixIs(ltrim(cellinfo_of_cell(cell)->align_special), "|");
464 return cellinfo_of_cell(cell)->left_line;
467 #ifdef SPECIAL_COLUM_HANDLING
468 if (column_info[column_of_cell(cell)].align_special.empty())
469 return column_info[column_of_cell(cell)].left_line;
470 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
472 return column_info[column_of_cell(cell)].left_line;
477 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
479 if (!onlycolumn && isMultiColumn(cell) &&
480 (isLastCellInRow(cell) || isMultiColumn(cell+1)))
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(rtrim(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(rtrim(column_info[column_of_cell(cell)].align_special), "|");
495 return column_info[right_column_of_cell(cell)].right_line;
500 bool LyXTabular::topAlreadyDrawn(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::leftAlreadyDrawn(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 rightLine(cell_info[row][column].cellno);
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_ && 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_ && 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) &&
584 leftLine(cell_info[row][col+1].cellno)) // 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 true if a complete update is necessary, otherwise false
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);
621 int const old_val = cell_info[row][column2].width_of_cell;
623 // first set columns to 0 so we can calculate the right width
624 for (int i = column1; i <= column2; ++i) {
625 cell_info[row][i].width_of_cell = 0;
627 // set the width to MAX_WIDTH until width > 0
628 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;
637 if (old_val != cell_info[row][column2].width_of_cell) {
638 // in this case we have to recalculate all multicolumn cells which
639 // have this column as one of theirs but not as last one
640 calculate_width_of_column_NMC(i);
641 recalculateMulticolumnsOfColumn(i);
642 calculate_width_of_column(i);
648 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
650 // the last column does not have to be recalculated because all
651 // multicolumns will have here there last multicolumn cell which
652 // always will have the whole rest of the width of the cell.
653 if (column > (columns_ - 2))
655 for(int row = 0; row < rows_; ++row) {
656 int mc = cell_info[row][column].multicolumn;
657 int nmc = cell_info[row][column+1].multicolumn;
658 // we only have to update multicolumns which do not have this
659 // column as their last column!
660 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
661 ((mc == CELL_PART_OF_MULTICOLUMN) &&
662 (nmc == CELL_PART_OF_MULTICOLUMN)))
664 int const cellno = cell_info[row][column].cellno;
665 setWidthOfMulticolCell(cellno,
666 getWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
672 // returns 1 if a complete update is necessary, otherwise 0
673 bool LyXTabular::setWidthOfCell(int cell, int new_width)
675 int const row = row_of_cell(cell);
676 int const column1 = column_of_cell(cell);
681 #ifdef SPECIAL_COLUM_HANDLING
682 if (rightLine(cell_info[row][column1].cellno, true) &&
683 (column1 < columns_-1) &&
684 leftLine(cell_info[row][column1+1].cellno, true))
686 if (column_info[column1].right_line && (column1 < columns_-1) &&
687 column_info[column1+1].left_line) // additional width
691 add_width = WIDTH_OF_LINE;
693 if (getWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
696 if (isMultiColumn(cell, true)) {
697 tmp = setWidthOfMulticolCell(cell, new_width);
699 width = new_width + 2 * WIDTH_OF_LINE + add_width;
700 cell_info[row][column1].width_of_cell = width;
701 tmp = calculate_width_of_column_NMC(column1);
703 recalculateMulticolumnsOfColumn(column1);
706 for (int i = 0; i < columns_; ++i)
707 calculate_width_of_column(i);
708 calculate_width_of_tabular();
715 bool LyXTabular::setAlignment(int cell, LyXAlignment align, bool onlycolumn)
717 if (!isMultiColumn(cell) || onlycolumn)
718 column_info[column_of_cell(cell)].alignment = align;
720 cellinfo_of_cell(cell)->alignment = align;
725 bool LyXTabular::setVAlignment(int cell, VAlignment align, bool onlycolumn)
727 if (!isMultiColumn(cell) || onlycolumn)
728 column_info[column_of_cell(cell)].valignment = align;
730 cellinfo_of_cell(cell)->valignment = align;
735 bool LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
737 bool flag = !width.zero();
738 int const j = column_of_cell(cell);
740 column_info[j].p_width = width;
741 // This should not ne necessary anymore
742 // if (flag) // do this only if there is a width
743 // setAlignment(cell, LYX_ALIGN_LEFT);
744 for (int i = 0; i < rows_; ++i) {
745 int c = getCellNumber(i, j);
746 flag = !getPWidth(c).zero(); // because of multicolumns!
747 getCellInset(c)->setAutoBreakRows(flag);
753 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
755 bool const flag = !width.zero();
757 cellinfo_of_cell(cell)->p_width = width;
758 if (isMultiColumn(cell)) {
759 getCellInset(cell)->setAutoBreakRows(flag);
766 bool LyXTabular::setAlignSpecial(int cell, string const & special,
767 LyXTabular::Feature what)
769 if (what == SET_SPECIAL_MULTI)
770 cellinfo_of_cell(cell)->align_special = special;
772 column_info[column_of_cell(cell)].align_special = special;
777 bool LyXTabular::setAllLines(int cell, bool line)
779 setTopLine(cell, line);
780 setBottomLine(cell, line);
781 setRightLine(cell, line);
782 setLeftLine(cell, line);
787 bool LyXTabular::setTopLine(int cell, bool line, bool onlycolumn)
789 int const row = row_of_cell(cell);
791 if (onlycolumn || !isMultiColumn(cell))
792 row_info[row].top_line = line;
794 cellinfo_of_cell(cell)->top_line = line;
799 bool LyXTabular::setBottomLine(int cell, bool line, bool onlycolumn)
801 if (onlycolumn || !isMultiColumn(cell))
802 row_info[row_of_cell(cell)].bottom_line = line;
804 cellinfo_of_cell(cell)->bottom_line = line;
809 bool LyXTabular::setLeftLine(int cell, bool line, bool onlycolumn)
811 if (onlycolumn || !isMultiColumn(cell))
812 column_info[column_of_cell(cell)].left_line = line;
814 cellinfo_of_cell(cell)->left_line = line;
819 bool LyXTabular::setRightLine(int cell, bool line, bool onlycolumn)
821 if (onlycolumn || !isMultiColumn(cell))
822 column_info[right_column_of_cell(cell)].right_line = line;
824 cellinfo_of_cell(cell)->right_line = line;
829 LyXAlignment LyXTabular::getAlignment(int cell, bool onlycolumn) const
831 if (!onlycolumn && isMultiColumn(cell))
832 return cellinfo_of_cell(cell)->alignment;
834 return column_info[column_of_cell(cell)].alignment;
838 LyXTabular::VAlignment
839 LyXTabular::getVAlignment(int cell, bool onlycolumn) const
841 if (!onlycolumn && isMultiColumn(cell))
842 return cellinfo_of_cell(cell)->valignment;
844 return column_info[column_of_cell(cell)].valignment;
848 LyXLength const LyXTabular::getPWidth(int cell) const
850 if (isMultiColumn(cell))
851 return cellinfo_of_cell(cell)->p_width;
852 return column_info[column_of_cell(cell)].p_width;
856 LyXLength const LyXTabular::getColumnPWidth(int cell) const
858 return column_info[column_of_cell(cell)].p_width;
862 LyXLength const LyXTabular::getMColumnPWidth(int cell) const
864 if (isMultiColumn(cell))
865 return cellinfo_of_cell(cell)->p_width;
870 string const LyXTabular::getAlignSpecial(int cell, int what) const
872 if (what == SET_SPECIAL_MULTI)
873 return cellinfo_of_cell(cell)->align_special;
874 return column_info[column_of_cell(cell)].align_special;
878 int LyXTabular::getWidthOfCell(int cell) const
880 int const row = row_of_cell(cell);
881 int const column1 = column_of_cell(cell);
882 int const column2 = right_column_of_cell(cell);
884 for (int i = column1; i <= column2; ++i) {
885 result += cell_info[row][i].width_of_cell;
891 int LyXTabular::getBeginningOfTextInCell(int cell) const
895 switch (getAlignment(cell)) {
896 case LYX_ALIGN_CENTER:
897 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
899 case LYX_ALIGN_RIGHT:
900 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
901 // + getAdditionalWidth(cell);
904 // LYX_ALIGN_LEFT: nothing :-)
914 bool LyXTabular::isFirstCellInRow(int cell) const
916 return column_of_cell(cell) == 0;
920 int LyXTabular::getFirstCellInRow(int row) const
924 return cell_info[row][0].cellno;
927 bool LyXTabular::isLastCellInRow(int cell) const
929 return (right_column_of_cell(cell) == (columns_ - 1));
933 int LyXTabular::getLastCellInRow(int row) const
937 return cell_info[row][columns_-1].cellno;
941 bool LyXTabular::calculate_width_of_column(int column)
943 int const old_column_width = column_info[column].width_of_column;
946 for (int i = 0; i < rows_; ++i) {
947 maximum = max(cell_info[i][column].width_of_cell, maximum);
949 column_info[column].width_of_column = maximum;
950 return (column_info[column].width_of_column != old_column_width);
955 // Calculate the columns regarding ONLY the normal cells and if this
956 // column is inside a multicolumn cell then use it only if its the last
957 // column of this multicolumn cell as this gives an added with to the
958 // column, all the rest should be adapted!
960 bool LyXTabular::calculate_width_of_column_NMC(int column)
962 int const old_column_width = column_info[column].width_of_column;
964 for (int i = 0; i < rows_; ++i) {
965 int cell = getCellNumber(i, column);
966 bool ismulti = isMultiColumn(cell, true);
967 if ((!ismulti || (column == right_column_of_cell(cell))) &&
968 (cell_info[i][column].width_of_cell > max))
970 max = cell_info[i][column].width_of_cell;
973 column_info[column].width_of_column = max;
974 return (column_info[column].width_of_column != old_column_width);
978 void LyXTabular::calculate_width_of_tabular()
980 width_of_tabular = 0;
981 for (int i = 0; i < columns_; ++i) {
982 width_of_tabular += column_info[i].width_of_column;
987 int LyXTabular::row_of_cell(int cell) const
989 if (cell >= numberofcells)
993 return rowofcell[cell];
997 int LyXTabular::column_of_cell(int cell) const
999 if (cell >= numberofcells)
1000 return columns_ - 1;
1003 return columnofcell[cell];
1007 int LyXTabular::right_column_of_cell(int cell) const
1009 int const row = row_of_cell(cell);
1010 int column = column_of_cell(cell);
1011 while (column < (columns_ - 1) &&
1012 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1018 void LyXTabular::write(Buffer const * buf, ostream & os) const
1022 << write_attribute("version", 3)
1023 << write_attribute("rows", rows_)
1024 << write_attribute("columns", columns_)
1026 // global longtable options
1028 << write_attribute("rotate", rotate)
1029 << write_attribute("islongtable", is_long_tabular)
1030 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1031 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1032 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1033 << write_attribute("headTopDL", endhead.topDL)
1034 << write_attribute("headBottomDL", endhead.bottomDL)
1035 << write_attribute("footTopDL", endfoot.topDL)
1036 << write_attribute("footBottomDL", endfoot.bottomDL)
1037 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1038 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1039 << write_attribute("lastFootEmpty", endlastfoot.empty)
1041 for (int j = 0; j < columns_; ++j) {
1043 << write_attribute("alignment", column_info[j].alignment)
1044 << write_attribute("valignment", column_info[j].valignment)
1045 << write_attribute("leftline", column_info[j].left_line)
1046 << write_attribute("rightline", column_info[j].right_line)
1047 << write_attribute("width", column_info[j].p_width.asString())
1048 << write_attribute("special", column_info[j].align_special)
1051 for (int i = 0; i < rows_; ++i) {
1053 << write_attribute("topline", row_info[i].top_line)
1054 << write_attribute("bottomline", row_info[i].bottom_line)
1055 << write_attribute("endhead", row_info[i].endhead)
1056 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1057 << write_attribute("endfoot", row_info[i].endfoot)
1058 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1059 << write_attribute("newpage", row_info[i].newpage)
1061 for (int j = 0; j < columns_; ++j) {
1063 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1064 << write_attribute("alignment", cell_info[i][j].alignment)
1065 << write_attribute("valignment", cell_info[i][j].valignment)
1066 << write_attribute("topline", cell_info[i][j].top_line)
1067 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1068 << write_attribute("leftline", cell_info[i][j].left_line)
1069 << write_attribute("rightline", cell_info[i][j].right_line)
1070 << write_attribute("rotate", cell_info[i][j].rotate)
1071 << write_attribute("usebox", cell_info[i][j].usebox)
1072 << write_attribute("width", cell_info[i][j].p_width)
1073 << write_attribute("special", cell_info[i][j].align_special)
1075 os << "\\begin_inset ";
1076 cell_info[i][j].inset.write(buf, os);
1077 os << "\n\\end_inset \n"
1082 os << "</lyxtabular>\n";
1086 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1090 row_info[--hr].endhead = true;
1092 // set firstheader info
1093 if (fhr && (fhr < rows_)) {
1094 if (row_info[fhr].endhead) {
1096 row_info[--fhr].endfirsthead = true;
1097 row_info[fhr].endhead = false;
1099 } else if (row_info[fhr - 1].endhead) {
1100 endfirsthead.empty = true;
1102 while((fhr > 0) && !row_info[--fhr].endhead) {
1103 row_info[fhr].endfirsthead = true;
1108 if (fr && (fr < rows_)) {
1109 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1110 while((fr > 0) && !row_info[--fr].endhead) {
1111 row_info[fr].endfoot = true;
1112 row_info[fr].endhead = false;
1114 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1115 while((fr > 0) && !row_info[--fr].endfirsthead) {
1116 row_info[fr].endfoot = true;
1117 row_info[fr].endfirsthead = false;
1119 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1120 while((fr > 0) && !row_info[--fr].endhead &&
1121 !row_info[fr].endfirsthead)
1123 row_info[fr].endfoot = true;
1127 // set lastfooter info
1128 if (lfr && (lfr < rows_)) {
1129 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1130 while((lfr > 0) && !row_info[--lfr].endhead) {
1131 row_info[lfr].endlastfoot = true;
1132 row_info[lfr].endhead = false;
1134 } else if (row_info[lfr].endfirsthead &&
1135 row_info[lfr - 1].endfirsthead)
1137 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1138 row_info[lfr].endlastfoot = true;
1139 row_info[lfr].endfirsthead = false;
1141 } else if (row_info[lfr].endfoot
1142 && row_info[lfr - 1].endfoot) {
1143 while((lfr > 0) && !row_info[--lfr].endfoot) {
1144 row_info[lfr].endlastfoot = true;
1145 row_info[lfr].endfoot = false;
1147 } else if (!row_info[fr - 1].endhead
1148 && !row_info[fr - 1].endfirsthead &&
1149 !row_info[fr - 1].endfoot)
1152 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1153 !row_info[lfr].endfoot)
1155 row_info[lfr].endlastfoot = true;
1157 } else if (haveLTFoot()) {
1158 endlastfoot.empty = true;
1164 void LyXTabular::read(Buffer const * buf, LyXLex & lex)
1167 istream & is = lex.getStream();
1169 l_getline(is, line);
1170 if (!prefixIs(line, "<lyxtabular ")
1171 && !prefixIs(line, "<LyXTabular ")) {
1177 if (!getTokenValue(line, "version", version))
1179 lyx::Assert(version >= 2);
1182 if (!getTokenValue(line, "rows", rows_arg))
1185 if (!getTokenValue(line, "columns", columns_arg))
1187 init(buf->params, rows_arg, columns_arg);
1188 l_getline(is, line);
1189 if (!prefixIs(line, "<features")) {
1190 lyxerr << "Wrong tabular format (expected <features ...> got"
1191 << line << ')' << endl;
1194 getTokenValue(line, "rotate", rotate);
1195 getTokenValue(line, "islongtable", is_long_tabular);
1196 // compatibility read for old longtable options. Now we can make any
1197 // row part of the header/footer type we want before it was strict
1198 // sequential from the first row down (as LaTeX does it!). So now when
1199 // we find a header/footer line we have to go up the rows and set it
1200 // on all preceding rows till the first or one with already a h/f option
1201 // set. If we find a firstheader on the same line as a header or a
1202 // lastfooter on the same line as a footer then this should be set empty.
1210 getTokenValue(line, "endhead", hrow);
1211 getTokenValue(line, "endfirsthead", fhrow);
1212 getTokenValue(line, "endfoot", frow);
1213 getTokenValue(line, "endlastfoot", lfrow);
1214 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1216 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1217 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1218 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1219 getTokenValue(line, "headTopDL", endhead.topDL);
1220 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1221 getTokenValue(line, "footTopDL", endfoot.topDL);
1222 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1223 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1224 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1225 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1227 for (int j = 0; j < columns_; ++j) {
1229 if (!prefixIs(line,"<column")) {
1230 lyxerr << "Wrong tabular format (expected <column ...> got"
1231 << line << ')' << endl;
1234 getTokenValue(line, "alignment", column_info[j].alignment);
1235 getTokenValue(line, "valignment", column_info[j].valignment);
1236 getTokenValue(line, "leftline", column_info[j].left_line);
1237 getTokenValue(line, "rightline", column_info[j].right_line);
1238 getTokenValue(line, "width", column_info[j].p_width);
1239 getTokenValue(line, "special", column_info[j].align_special);
1242 for (int i = 0; i < rows_; ++i) {
1243 l_getline(is, line);
1244 if (!prefixIs(line, "<row")) {
1245 lyxerr << "Wrong tabular format (expected <row ...> got"
1246 << line << ')' << endl;
1249 getTokenValue(line, "topline", row_info[i].top_line);
1250 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1251 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1252 getTokenValue(line, "endhead", row_info[i].endhead);
1253 getTokenValue(line, "endfoot", row_info[i].endfoot);
1254 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1255 getTokenValue(line, "newpage", row_info[i].newpage);
1256 for (int j = 0; j < columns_; ++j) {
1257 l_getline(is, line);
1258 if (!prefixIs(line, "<cell")) {
1259 lyxerr << "Wrong tabular format (expected <cell ...> got"
1260 << line << ')' << endl;
1263 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1264 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1265 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1266 getTokenValue(line, "topline", cell_info[i][j].top_line);
1267 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1268 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1269 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1270 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1271 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1272 getTokenValue(line, "width", cell_info[i][j].p_width);
1273 getTokenValue(line, "special", cell_info[i][j].align_special);
1274 l_getline(is, line);
1275 if (prefixIs(line, "\\begin_inset")) {
1276 cell_info[i][j].inset.read(buf, lex);
1277 l_getline(is, line);
1279 if (!prefixIs(line, "</cell>")) {
1280 lyxerr << "Wrong tabular format (expected </cell> got"
1281 << line << ')' << endl;
1285 l_getline(is, line);
1286 if (!prefixIs(line, "</row>")) {
1287 lyxerr << "Wrong tabular format (expected </row> got"
1288 << line << ')' << endl;
1292 while (!prefixIs(line, "</lyxtabular>")) {
1293 l_getline(is, line);
1295 set_row_column_number_info();
1299 bool LyXTabular::isMultiColumn(int cell, bool real) const
1301 return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
1302 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1306 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1308 int const row = row_of_cell(cell);
1309 int const column = column_of_cell(cell);
1310 return &cell_info[row][column];
1314 void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
1316 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1317 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1318 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1319 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1320 cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1322 for (int i = 1; i < number; ++i) {
1323 cellinfo_of_cell(cell + i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1324 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer,
1325 cellinfo_of_cell(cell+i)->inset.paragraphs);
1326 cellinfo_of_cell(cell + i)->inset.clear(false);
1329 for (number--; number > 0; --number) {
1330 cellinfo_of_cell(cell + number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1333 set_row_column_number_info();
1337 int LyXTabular::cells_in_multicolumn(int cell) const
1339 int const row = row_of_cell(cell);
1340 int column = column_of_cell(cell);
1343 while ((column < columns_) &&
1344 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1353 int LyXTabular::unsetMultiColumn(int cell)
1355 int const row = row_of_cell(cell);
1356 int column = column_of_cell(cell);
1360 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1361 cell_info[row][column].multicolumn = CELL_NORMAL;
1363 while ((column < columns_) &&
1364 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1366 cell_info[row][column].multicolumn = CELL_NORMAL;
1371 set_row_column_number_info();
1376 void LyXTabular::setLongTabular(bool what)
1378 is_long_tabular = what;
1382 bool LyXTabular::isLongTabular() const
1384 return is_long_tabular;
1388 void LyXTabular::setRotateTabular(bool flag)
1394 bool LyXTabular::getRotateTabular() const
1400 void LyXTabular::setRotateCell(int cell, bool flag)
1402 cellinfo_of_cell(cell)->rotate = flag;
1406 bool LyXTabular::getRotateCell(int cell) const
1408 return cellinfo_of_cell(cell)->rotate;
1412 bool LyXTabular::needRotating() const
1416 for (int i = 0; i < rows_; ++i) {
1417 for (int j = 0; j < columns_; ++j) {
1418 if (cell_info[i][j].rotate)
1426 bool LyXTabular::isLastCell(int cell) const
1428 if (cell + 1 < numberofcells)
1434 int LyXTabular::getCellAbove(int cell) const
1436 if (row_of_cell(cell) > 0)
1437 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1442 int LyXTabular::getCellBelow(int cell) const
1444 if (row_of_cell(cell) + 1 < rows_)
1445 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1450 int LyXTabular::getLastCellAbove(int cell) const
1452 if (row_of_cell(cell) <= 0)
1454 if (!isMultiColumn(cell))
1455 return getCellAbove(cell);
1456 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1460 int LyXTabular::getLastCellBelow(int cell) const
1462 if (row_of_cell(cell) + 1 >= rows_)
1464 if (!isMultiColumn(cell))
1465 return getCellBelow(cell);
1466 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1470 int LyXTabular::getCellNumber(int row, int column) const
1473 if (column >= columns_)
1474 column = columns_ - 1;
1475 else if (column < 0)
1482 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1484 return cell_info[row][column].cellno;
1488 void LyXTabular::setUsebox(int cell, BoxType type)
1490 cellinfo_of_cell(cell)->usebox = type;
1494 LyXTabular::BoxType LyXTabular::getUsebox(int cell) const
1496 if (column_info[column_of_cell(cell)].p_width.zero() &&
1497 !(isMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1499 if (cellinfo_of_cell(cell)->usebox > 1)
1500 return cellinfo_of_cell(cell)->usebox;
1501 return useParbox(cell);
1506 // This are functions used for the longtable support
1508 void LyXTabular::setLTHead(int row, bool flag, ltType const & hd, bool first)
1513 row_info[row].endfirsthead = flag;
1517 row_info[row].endhead = flag;
1522 bool LyXTabular::getRowOfLTHead(int row, ltType & hd) const
1525 hd.set = haveLTHead();
1526 return row_info[row].endhead;
1530 bool LyXTabular::getRowOfLTFirstHead(int row, ltType & hd) const
1533 hd.set = haveLTFirstHead();
1534 return row_info[row].endfirsthead;
1538 void LyXTabular::setLTFoot(int row, bool flag, ltType const & fd, bool last)
1543 row_info[row].endlastfoot = flag;
1547 row_info[row].endfoot = flag;
1552 bool LyXTabular::getRowOfLTFoot(int row, ltType & fd) const
1555 fd.set = haveLTFoot();
1556 return row_info[row].endfoot;
1560 bool LyXTabular::getRowOfLTLastFoot(int row, ltType & fd) const
1563 fd.set = haveLTLastFoot();
1564 return row_info[row].endlastfoot;
1568 void LyXTabular::setLTNewPage(int row, bool what)
1570 row_info[row].newpage = what;
1574 bool LyXTabular::getLTNewPage(int row) const
1576 return row_info[row].newpage;
1580 bool LyXTabular::haveLTHead() const
1582 for (int i = 0; i < rows_; ++i) {
1583 if (row_info[i].endhead)
1590 bool LyXTabular::haveLTFirstHead() const
1592 if (endfirsthead.empty)
1594 for (int i = 0; i < rows_; ++i) {
1595 if (row_info[i].endfirsthead)
1602 bool LyXTabular::haveLTFoot() const
1604 for (int i = 0; i < rows_; ++i) {
1605 if (row_info[i].endfoot)
1612 bool LyXTabular::haveLTLastFoot() const
1614 if (endlastfoot.empty)
1616 for (int i = 0; i < rows_; ++i) {
1617 if (row_info[i].endlastfoot)
1624 // end longtable support functions
1626 bool LyXTabular::setAscentOfRow(int row, int height)
1628 if (row >= rows_ || row_info[row].ascent_of_row == height)
1630 row_info[row].ascent_of_row = height;
1635 bool LyXTabular::setDescentOfRow(int row, int height)
1637 if (row >= rows_ || row_info[row].descent_of_row == height)
1639 row_info[row].descent_of_row = height;
1644 int LyXTabular::getAscentOfRow(int row) const
1648 return row_info[row].ascent_of_row;
1652 int LyXTabular::getDescentOfRow(int row) const
1656 return row_info[row].descent_of_row;
1660 int LyXTabular::getHeightOfTabular() const
1664 for (int row = 0; row < rows_; ++row)
1665 height += getAscentOfRow(row) + getDescentOfRow(row) +
1666 getAdditionalHeight(row);
1671 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
1673 if ((row >= rows_) || (column >= columns_))
1675 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1679 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1681 if ((row < 0) || (row >= rows_))
1684 int const fcell = getFirstCellInRow(row);
1685 int const n = numberOfCellsInRow(fcell) + fcell;
1688 for (int i = fcell; i < n; ++i) {
1692 if (tmp == (n - fcell)) {
1695 for (int i = fcell; i < n; ++i) {
1698 << column_of_cell(i) + 1
1700 << right_column_of_cell(i) + 1
1712 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1714 if ((row < 0) || (row >= rows_))
1717 int const fcell = getFirstCellInRow(row);
1718 int const n = numberOfCellsInRow(fcell) + fcell;
1721 for (int i = fcell; i < n; ++i) {
1725 if (tmp == (n - fcell)) {
1728 for (int i = fcell; i < n; ++i) {
1729 if (bottomLine(i)) {
1731 << column_of_cell(i) + 1
1733 << right_column_of_cell(i) + 1
1745 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1749 if (getRotateCell(cell)) {
1750 os << "\\begin{sideways}\n";
1753 if (isMultiColumn(cell)) {
1754 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1755 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1756 os << cellinfo_of_cell(cell)->align_special << "}{";
1758 if (leftLine(cell) &&
1759 (isFirstCellInRow(cell) ||
1760 (!isMultiColumn(cell-1) && !leftLine(cell, true) &&
1761 !rightLine(cell-1, true))))
1765 if (!getPWidth(cell).zero()) {
1766 switch (getVAlignment(cell)) {
1767 case LYX_VALIGN_TOP:
1770 case LYX_VALIGN_CENTER:
1773 case LYX_VALIGN_BOTTOM:
1778 << getPWidth(cell).asLatexString()
1781 switch (getAlignment(cell)) {
1782 case LYX_ALIGN_LEFT:
1785 case LYX_ALIGN_RIGHT:
1793 if (rightLine(cell))
1795 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
1801 if (getUsebox(cell) == BOX_PARBOX) {
1803 switch (getVAlignment(cell)) {
1804 case LYX_VALIGN_TOP:
1807 case LYX_VALIGN_CENTER:
1810 case LYX_VALIGN_BOTTOM:
1814 os << "]{" << getPWidth(cell).asLatexString() << "}{";
1815 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1816 os << "\\begin{minipage}[";
1817 switch (getVAlignment(cell)) {
1818 case LYX_VALIGN_TOP:
1821 case LYX_VALIGN_CENTER:
1824 case LYX_VALIGN_BOTTOM:
1828 os << "]{" << getPWidth(cell).asLatexString() << "}\n";
1835 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1840 if (getUsebox(cell) == BOX_PARBOX)
1842 else if (getUsebox(cell) == BOX_MINIPAGE) {
1843 os << "%\n\\end{minipage}";
1846 if (isMultiColumn(cell)) {
1849 if (getRotateCell(cell)) {
1850 os << "%\n\\end{sideways}";
1857 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1858 LatexRunParams const & runparams) const
1860 if (!is_long_tabular)
1864 // output header info
1866 if (endhead.topDL) {
1870 for (int i = 0; i < rows_; ++i) {
1871 if (row_info[i].endhead) {
1872 ret += TeXRow(os, i, buf, runparams);
1875 if (endhead.bottomDL) {
1879 os << "\\endhead\n";
1881 if (endfirsthead.empty) {
1882 os << "\\endfirsthead\n";
1886 // output firstheader info
1887 if (haveLTFirstHead()) {
1888 if (endfirsthead.topDL) {
1892 for (int i = 0; i < rows_; ++i) {
1893 if (row_info[i].endfirsthead) {
1894 ret += TeXRow(os, i, buf, runparams);
1897 if (endfirsthead.bottomDL) {
1901 os << "\\endfirsthead\n";
1904 // output footer info
1906 if (endfoot.topDL) {
1910 for (int i = 0; i < rows_; ++i) {
1911 if (row_info[i].endfoot) {
1912 ret += TeXRow(os, i, buf, runparams);
1915 if (endfoot.bottomDL) {
1919 os << "\\endfoot\n";
1921 if (endlastfoot.empty) {
1922 os << "\\endlastfoot\n";
1926 // output lastfooter info
1927 if (haveLTLastFoot()) {
1928 if (endlastfoot.topDL) {
1932 for (int i = 0; i < rows_; ++i) {
1933 if (row_info[i].endlastfoot) {
1934 ret += TeXRow(os, i, buf, runparams);
1937 if (endlastfoot.bottomDL) {
1941 os << "\\endlastfoot\n";
1948 bool LyXTabular::isValidRow(int const row) const
1950 if (!is_long_tabular)
1952 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
1953 !row_info[row].endfoot && !row_info[row].endlastfoot);
1957 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
1958 LatexRunParams const & runparams) const
1961 int cell = getCellNumber(i, 0);
1963 ret += TeXTopHLine(os, i);
1964 for (int j = 0; j < columns_; ++j) {
1965 if (isPartOfMultiColumn(i,j))
1967 ret += TeXCellPreamble(os, cell);
1968 InsetText * inset = getCellInset(cell);
1970 bool rtl = inset->paragraphs.begin()->isRightToLeftPar(buf->params) &&
1971 !inset->paragraphs.begin()->empty() && getPWidth(cell).zero();
1975 ret += inset->latex(buf, os, runparams);
1979 ret += TeXCellPostamble(os, cell);
1980 if (!isLastCellInRow(cell)) { // not last cell in row
1986 os << "\\tabularnewline\n";
1988 ret += TeXBottomHLine(os, i);
1993 int LyXTabular::latex(Buffer const * buf, ostream & os,
1994 LatexRunParams const & runparams) const
1998 //+---------------------------------------------------------------------
1999 //+ first the opening preamble +
2000 //+---------------------------------------------------------------------
2003 os << "\\begin{sideways}\n";
2006 if (is_long_tabular)
2007 os << "\\begin{longtable}{";
2009 os << "\\begin{tabular}{";
2010 for (int i = 0; i < columns_; ++i) {
2011 if (!column_info[i].align_special.empty()) {
2012 os << column_info[i].align_special;
2014 if (column_info[i].left_line)
2016 if (!column_info[i].p_width.zero()) {
2017 switch (column_info[i].alignment) {
2018 case LYX_ALIGN_LEFT:
2019 os << ">{\\raggedright}";
2021 case LYX_ALIGN_RIGHT:
2022 os << ">{\\raggedleft}";
2024 case LYX_ALIGN_CENTER:
2025 os << ">{\\centering}";
2027 case LYX_ALIGN_NONE:
2028 case LYX_ALIGN_BLOCK:
2029 case LYX_ALIGN_LAYOUT:
2030 case LYX_ALIGN_SPECIAL:
2034 switch (column_info[i].valignment) {
2035 case LYX_VALIGN_TOP:
2038 case LYX_VALIGN_CENTER:
2041 case LYX_VALIGN_BOTTOM:
2046 << column_info[i].p_width.asLatexString()
2049 switch (column_info[i].alignment) {
2050 case LYX_ALIGN_LEFT:
2053 case LYX_ALIGN_RIGHT:
2061 if (column_info[i].right_line)
2068 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2070 //+---------------------------------------------------------------------
2071 //+ the single row and columns (cells) +
2072 //+---------------------------------------------------------------------
2074 for (int i = 0; i < rows_; ++i) {
2075 if (isValidRow(i)) {
2076 ret += TeXRow(os, i, buf, runparams);
2077 if (is_long_tabular && row_info[i].newpage) {
2078 os << "\\newpage\n";
2084 //+---------------------------------------------------------------------
2085 //+ the closing of the tabular +
2086 //+---------------------------------------------------------------------
2088 if (is_long_tabular)
2089 os << "\\end{longtable}";
2091 os << "\\end{tabular}";
2093 os << "\n\\end{sideways}";
2101 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2104 int cell = getFirstCellInRow(row);
2107 for (int j = 0; j < columns_; ++j) {
2108 if (isPartOfMultiColumn(row, j))
2111 os << "<entry align=\"";
2112 switch (getAlignment(cell)) {
2113 case LYX_ALIGN_LEFT:
2116 case LYX_ALIGN_RIGHT:
2124 os << "\" valign=\"";
2125 switch (getVAlignment(cell)) {
2126 case LYX_VALIGN_TOP:
2129 case LYX_VALIGN_BOTTOM:
2132 case LYX_VALIGN_CENTER:
2137 if (isMultiColumn(cell)) {
2138 os << " namest=\"col" << j << "\" ";
2139 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2143 ret += getCellInset(cell)->docbook(buf, os, true);
2152 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2153 bool /*mixcont*/) const
2157 //+---------------------------------------------------------------------
2158 //+ first the opening preamble +
2159 //+---------------------------------------------------------------------
2161 os << "<tgroup cols=\"" << columns_
2162 << "\" colsep=\"1\" rowsep=\"1\">\n";
2164 for (int i = 0; i < columns_; ++i) {
2165 os << "<colspec colname=\"col" << i << "\" align=\"";
2166 switch (column_info[i].alignment) {
2167 case LYX_ALIGN_LEFT:
2170 case LYX_ALIGN_RIGHT:
2181 //+---------------------------------------------------------------------
2182 //+ Long Tabular case +
2183 //+---------------------------------------------------------------------
2185 // output header info
2186 if (haveLTHead() || haveLTFirstHead()) {
2189 for (int i = 0; i < rows_; ++i) {
2190 if (row_info[i].endhead || row_info[i].endfirsthead) {
2191 ret += docbookRow(buf, os, i);
2197 // output footer info
2198 if (haveLTFoot() || haveLTLastFoot()) {
2201 for (int i = 0; i < rows_; ++i) {
2202 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2203 ret += docbookRow(buf, os, i);
2210 //+---------------------------------------------------------------------
2211 //+ the single row and columns (cells) +
2212 //+---------------------------------------------------------------------
2216 for (int i = 0; i < rows_; ++i) {
2217 if (isValidRow(i)) {
2218 ret += docbookRow(buf, os, i);
2223 //+---------------------------------------------------------------------
2224 //+ the closing of the tabular +
2225 //+---------------------------------------------------------------------
2234 int LyXTabular::asciiTopHLine(ostream & os, int row,
2235 vector<unsigned int> const & clen) const
2237 int const fcell = getFirstCellInRow(row);
2238 int const n = numberOfCellsInRow(fcell) + fcell;
2241 for (int i = fcell; i < n; ++i) {
2251 for (int i = fcell; i < n; ++i) {
2262 int column = column_of_cell(i);
2263 int len = clen[column];
2264 while (isPartOfMultiColumn(row, ++column))
2265 len += clen[column] + 4;
2266 os << string(len, ch);
2281 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2282 vector<unsigned int> const & clen) const
2284 int const fcell = getFirstCellInRow(row);
2285 int const n = numberOfCellsInRow(fcell) + fcell;
2288 for (int i = fcell; i < n; ++i) {
2289 if (bottomLine(i)) {
2298 for (int i = fcell; i < n; ++i) {
2299 if (bottomLine(i)) {
2309 int column = column_of_cell(i);
2310 int len = clen[column];
2311 while (isPartOfMultiColumn(row, ++column))
2312 len += clen[column] + 4;
2313 os << string(len, ch);
2314 if (bottomLine(i)) {
2328 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2329 int cell, int row, int column,
2330 vector<unsigned int> const & clen,
2331 bool onlydata) const
2334 int ret = getCellInset(cell)->ascii(buf, sstr, 0);
2346 unsigned int len1 = sstr.str().length();
2347 unsigned int len2 = clen[column];
2348 while (isPartOfMultiColumn(row, ++column))
2349 len2 += clen[column] + 4;
2352 switch (getAlignment(cell)) {
2354 case LYX_ALIGN_LEFT:
2357 case LYX_ALIGN_RIGHT:
2361 case LYX_ALIGN_CENTER:
2367 os << string(len1, ' ') << sstr.str() << string(len2, ' ');
2369 if (rightLine(cell))
2378 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2379 bool onlydata, unsigned char delim) const
2383 // first calculate the width of the single columns
2384 vector<unsigned int> clen(columns_);
2387 // first all non (real) multicolumn cells!
2388 for (int j = 0; j < columns_; ++j) {
2390 for (int i = 0; i < rows_; ++i) {
2391 int cell = getCellNumber(i, j);
2392 if (isMultiColumn(cell, true))
2395 getCellInset(cell)->ascii(buf, sstr, 0);
2396 if (clen[j] < sstr.str().length())
2397 clen[j] = sstr.str().length();
2400 // then all (real) multicolumn cells!
2401 for (int j = 0; j < columns_; ++j) {
2402 for (int i = 0; i < rows_; ++i) {
2403 int cell = getCellNumber(i, j);
2404 if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
2407 getCellInset(cell)->ascii(buf, sstr, 0);
2408 int len = int(sstr.str().length());
2409 int const n = cells_in_multicolumn(cell);
2410 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2412 if (len > int(clen[j + n - 1]))
2413 clen[j + n - 1] = len;
2418 for (int i = 0; i < rows_; ++i) {
2420 if (asciiTopHLine(os, i, clen)) {
2421 for (int j = 0; j < depth; ++j)
2425 for (int j = 0; j < columns_; ++j) {
2426 if (isPartOfMultiColumn(i,j))
2428 if (onlydata && j > 0)
2430 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2435 for (int j = 0; j < depth; ++j)
2437 if (asciiBottomHLine(os, i, clen)) {
2438 for (int j = 0; j < depth; ++j)
2447 InsetText * LyXTabular::getCellInset(int cell) const
2450 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2454 InsetText * LyXTabular::getCellInset(int row, int column) const
2456 cur_cell = getCellNumber(row, column);
2457 return & cell_info[row][column].inset;
2461 int LyXTabular::getCellFromInset(Inset const * inset, int maybe_cell) const
2463 // is this inset part of the tabular?
2464 if (!inset || inset->owner() != owner_) {
2465 lyxerr[Debug::INSETTEXT]
2466 << "this is not a cell of the tabular!" << endl;
2470 const int save_cur_cell = cur_cell;
2471 int cell = cur_cell;
2472 if (getCellInset(cell) != inset) {
2474 if (cell == -1 || getCellInset(cell) != inset) {
2480 for (cell = getNumberOfCells(); cell >= 0; --cell) {
2481 if (getCellInset(cell) == inset)
2484 lyxerr[Debug::INSETTEXT]
2485 << "LyXTabular::getCellFromInset: "
2487 << ", cur_cell=" << save_cur_cell
2488 << ", maybe_cell=" << maybe_cell
2490 // We should have found a cell at this point
2492 lyxerr << "LyXTabular::getCellFromInset: "
2493 << "Cell not found!" << endl;
2501 void LyXTabular::validate(LaTeXFeatures & features) const
2503 features.require("NeedTabularnewline");
2504 if (isLongTabular())
2505 features.require("longtable");
2507 features.require("rotating");
2508 for (int cell = 0; cell < numberofcells; ++cell) {
2509 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2510 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2511 features.require("array");
2512 getCellInset(cell)->validate(features);
2517 vector<string> const LyXTabular::getLabelList() const
2519 vector<string> label_list;
2520 for (int i = 0; i < rows_; ++i)
2521 for (int j = 0; j < columns_; ++j) {
2522 vector<string> const l =
2523 getCellInset(i, j)->getLabelList();
2524 label_list.insert(label_list.end(),
2525 l.begin(), l.end());
2531 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
2533 ParagraphList const & parlist = getCellInset(cell)->paragraphs;
2534 ParagraphList::const_iterator cit = parlist.begin();
2535 ParagraphList::const_iterator end = parlist.end();
2537 for (; cit != end; ++cit) {
2538 for (int i = 0; i < cit->size(); ++i) {
2539 if (cit->isNewline(i))