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()
110 LyXTabular::LyXTabular(BufferParams const & bp,
111 InsetTabular * inset, int rows_arg, int columns_arg)
115 Init(bp, rows_arg, columns_arg);
119 LyXTabular::LyXTabular(BufferParams const & bp,
120 InsetTabular * inset, LyXTabular const & lt)
124 Init(bp, lt.rows_, lt.columns_, <);
127 #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)
134 // LyXTabular::LyXTabular(BufferParams const & bp,
135 // InsetTabular * inset, LyXTabular const & lt,
140 // Init(bp, lt.rows_, lt.columns_, <);
141 // // we really should change again to have InsetText as a pointer
142 // // and allocate it then we would not have to do this stuff all
145 // for (int i = 0; i < rows_; ++i) {
146 // for (int j = 0; j < columns_; ++j) {
147 // cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
148 // cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraphs, true);
153 // #ifdef WITH_WARNINGS
154 // #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)
161 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
169 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
172 #warning This whole method should look like this: (Lgb)
177 // If this and lt is not of the same size we have a serious bug
178 // So then it is ok to throw an exception, or for now
180 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
182 cell_info = lt.cell_info;
183 row_info = lt.row_info;
184 column_info = lt.column_info;
185 SetLongTabular(lt.is_long_tabular);
194 LyXTabular * LyXTabular::clone(BufferParams const & bp,
195 InsetTabular * inset)
197 LyXTabular * result = new LyXTabular(bp, inset, *this);
199 // don't know if this is good but I need to Clone also
200 // the text-insets here, this is for the Undo-facility!
201 for (int i = 0; i < rows_; ++i) {
202 for (int j = 0; j < columns_; ++j) {
203 result->cell_info[i][j].inset = cell_info[i][j].inset;
204 result->cell_info[i][j].inset.setOwner(inset);
212 // LyXTabular * LyXTabular::clone(BufferParams const & bp,
213 // InsetTabular * inset, bool same_id)
215 // LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
217 // // don't know if this is good but I need to Clone also
218 // // the text-insets here, this is for the Undo-facility!
219 // for (int i = 0; i < rows_; ++i) {
220 // for (int j = 0; j < columns_; ++j) {
221 // result->cell_info[i][j].inset = cell_info[i][j].inset;
222 // result->cell_info[i][j].inset.setOwner(inset);
230 /* activates all lines and sets all widths to 0 */
231 void LyXTabular::Init(BufferParams const & bp,
232 int rows_arg, int columns_arg, LyXTabular const * lt)
235 columns_ = columns_arg;
236 row_info = row_vector(rows_, rowstruct());
237 column_info = column_vector(columns_, columnstruct());
238 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
246 for (int i = 0; i < rows_; ++i) {
247 for (int j = 0; j < columns_; ++j) {
248 cell_info[i][j].inset.setOwner(owner_);
249 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
250 cell_info[i][j].cellno = cellno++;
252 cell_info[i].back().right_line = true;
254 row_info.back().bottom_line = true;
255 row_info.front().bottom_line = true;
257 for (int i = 0; i < columns_; ++i) {
258 calculate_width_of_column(i);
260 column_info.back().right_line = true;
262 calculate_width_of_tabular();
264 rowofcell = vector<int>();
265 columnofcell = vector<int>();
266 set_row_column_number_info();
267 is_long_tabular = false;
272 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
276 int row = row_of_cell(cell);
278 row_vector::iterator rit = row_info.begin() + row;
279 row_info.insert(rit, rowstruct());
280 // now set the values of the row before
281 row_info[row] = row_info[row + 1];
284 cell_vvector::iterator cit = cell_info.begin() + row;
285 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
287 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
290 for (int i = 0; i <= row; ++i) {
291 for (int j = 0; j < columns_; ++j) {
292 c_info[i][j] = cell_info[i][j];
295 for (int i = row + 1; i < rows_; ++i) {
296 for (int j = 0; j < columns_; ++j) {
297 c_info[i][j] = cell_info[i-1][j];
302 for (int j = 0; j < columns_; ++j) {
303 cell_info[row][j].inset.clear(false);
304 if (bp.tracking_changes)
305 cell_info[row][j].inset.markNew(true);
312 void LyXTabular::DeleteRow(int row)
314 if (rows_ == 1) return; // Not allowed to delete last row
316 row_info.erase(row_info.begin() + row); //&row_info[row]);
317 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
323 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
327 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
329 int const column = column_of_cell(cell);
330 column_vector::iterator cit = column_info.begin() + column + 1;
331 column_info.insert(cit, columnstruct());
332 // set the column values of the column before
333 column_info[column + 1] = column_info[column];
335 for (int i = 0; i < rows_; ++i) {
336 for (int j = 0; j <= column; ++j) {
337 c_info[i][j] = cell_info[i][j];
339 for (int j = column + 1; j < columns_; ++j) {
340 c_info[i][j] = cell_info[i][j - 1];
342 // care about multicolumns
343 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
345 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
347 if ((column + 2) >= columns_ ||
348 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
350 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
355 for (int i = 0; i < rows_; ++i) {
356 cell_info[i][column + 1].inset.clear(false);
357 if (bp.tracking_changes)
358 cell_info[i][column + 1].inset.markNew(true);
364 void LyXTabular::DeleteColumn(int column)
366 // Similar to DeleteRow
367 //if (!(columns_ - 1))
369 if (columns_ == 1) return; // Not allowed to delete last column
371 column_info.erase(column_info.begin() + column);
372 for (int i = 0; i < rows_; ++i) {
373 cell_info[i].erase(cell_info[i].begin() + column);
380 void LyXTabular::reinit()
386 void LyXTabular::Reinit(bool reset_widths)
389 for (int i = 0; i < rows_; ++i) {
390 for (int j = 0; j < columns_; ++j) {
391 cell_info[i][j].width_of_cell = 0;
392 cell_info[i][j].inset.setOwner(owner_);
397 for (int i = 0; i < columns_; ++i) {
398 calculate_width_of_column(i);
400 calculate_width_of_tabular();
402 set_row_column_number_info();
406 void LyXTabular::set_row_column_number_info(bool oldformat)
409 for (int row = 0; row < rows_; ++row) {
410 for (int column = 0; column<columns_; ++column) {
411 if (cell_info[row][column].multicolumn
412 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
414 cell_info[row][column].cellno = numberofcells;
417 ++numberofcells; // because this is one more than as we start from 0
419 rowofcell.resize(numberofcells);
420 columnofcell.resize(numberofcells);
422 for (int row = 0, column = 0, c = 0;
423 c < numberofcells && row < rows_ && column < columns_;) {
425 columnofcell[c] = column;
429 } while (column < columns_ &&
430 cell_info[row][column].multicolumn
431 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
432 if (column == columns_) {
438 for (int row = 0; row < rows_; ++row) {
439 for (int column = 0; column < columns_; ++column) {
440 if (IsPartOfMultiColumn(row,column))
442 // now set the right line of multicolumns right for oldformat read
444 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
446 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
447 cell_info[row][column].right_line =
448 cell_info[row][column+cn-1].right_line;
450 cell_info[row][column].inset.setAutoBreakRows(
451 !GetPWidth(GetCellNumber(row, column)).zero());
457 int LyXTabular::GetNumberOfCells() const
459 return numberofcells;
463 int LyXTabular::NumberOfCellsInRow(int cell) const
465 int const row = row_of_cell(cell);
467 for (int i = 0; i < columns_; ++i) {
468 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
475 /* returns 1 if there is a topline, returns 0 if not */
476 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
478 int const row = row_of_cell(cell);
480 if (!onlycolumn && IsMultiColumn(cell))
481 return cellinfo_of_cell(cell)->top_line;
482 return row_info[row].top_line;
486 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
488 // no bottom line underneath non-existent cells if you please
489 // Isn't that a programming error? Is so this should
490 // be an Assert instead. (Lgb)
491 if (cell >= numberofcells)
494 if (!onlycolumn && IsMultiColumn(cell))
495 return cellinfo_of_cell(cell)->bottom_line;
496 return row_info[row_of_cell(cell)].bottom_line;
500 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
502 if (!onlycolumn && IsMultiColumn(cell) &&
503 (IsFirstCellInRow(cell) || IsMultiColumn(cell-1)))
505 #ifdef SPECIAL_COLUM_HANDLING
506 if (cellinfo_of_cell(cell)->align_special.empty())
507 return cellinfo_of_cell(cell)->left_line;
508 return prefixIs(ltrim(cellinfo_of_cell(cell)->align_special), "|");
510 return cellinfo_of_cell(cell)->left_line;
513 #ifdef SPECIAL_COLUM_HANDLING
514 if (column_info[column_of_cell(cell)].align_special.empty())
515 return column_info[column_of_cell(cell)].left_line;
516 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), "|");
518 return column_info[column_of_cell(cell)].left_line;
523 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
525 if (!onlycolumn && IsMultiColumn(cell) &&
526 (IsLastCellInRow(cell) || IsMultiColumn(cell+1)))
528 #ifdef SPECIAL_COLUM_HANDLING
529 if (cellinfo_of_cell(cell)->align_special.empty())
530 return cellinfo_of_cell(cell)->right_line;
531 return suffixIs(rtrim(cellinfo_of_cell(cell)->align_special), "|");
533 return cellinfo_of_cell(cell)->right_line;
536 #ifdef SPECIAL_COLUM_HANDLING
537 if (column_info[column_of_cell(cell)].align_special.empty())
538 return column_info[right_column_of_cell(cell)].right_line;
539 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), "|");
541 return column_info[right_column_of_cell(cell)].right_line;
546 bool LyXTabular::topAlreadyDrawn(int cell) const
548 int row = row_of_cell(cell);
549 if ((row > 0) && !GetAdditionalHeight(row)) {
550 int column = column_of_cell(cell);
553 && cell_info[row][column].multicolumn
554 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
556 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
557 return row_info[row].bottom_line;
559 return cell_info[row][column].bottom_line;
565 bool LyXTabular::leftAlreadyDrawn(int cell) const
567 int column = column_of_cell(cell);
569 int row = row_of_cell(cell);
571 (cell_info[row][column].multicolumn ==
572 LyXTabular::CELL_PART_OF_MULTICOLUMN));
573 if (GetAdditionalWidth(cell_info[row][column].cellno))
575 #ifdef SPECIAL_COLUM_HANDLING
576 return RightLine(cell_info[row][column].cellno);
578 return RightLine(cell_info[row][column].cellno, true);
585 bool LyXTabular::IsLastRow(int cell) const
587 return (row_of_cell(cell) == rows_ - 1);
591 int LyXTabular::GetAdditionalHeight(int row) const
593 if (!row || row >= rows_)
599 for (int column = 0; column < columns_ && bottom; ++column) {
600 switch (cell_info[row - 1][column].multicolumn) {
601 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
602 bottom = cell_info[row - 1][column].bottom_line;
604 case LyXTabular::CELL_NORMAL:
605 bottom = row_info[row - 1].bottom_line;
608 for (int column = 0; column < columns_ && top; ++column) {
609 switch (cell_info[row][column].multicolumn) {
610 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
611 top = cell_info[row][column].top_line;
613 case LyXTabular::CELL_NORMAL:
614 top = row_info[row].top_line;
618 return WIDTH_OF_LINE;
623 int LyXTabular::GetAdditionalWidth(int cell) const
625 // internally already set in SetWidthOfCell
626 // used to get it back in text.C
627 int const col = right_column_of_cell(cell);
628 int const row = row_of_cell(cell);
629 if (col < columns_ - 1 && RightLine(cell) &&
630 LeftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
632 return WIDTH_OF_LINE;
639 // returns the maximum over all rows
640 int LyXTabular::GetWidthOfColumn(int cell) const
642 int const column1 = column_of_cell(cell);
643 int const column2 = right_column_of_cell(cell);
645 for (int i = column1; i <= column2; ++i) {
646 result += column_info[i].width_of_column;
652 int LyXTabular::GetWidthOfTabular() const
654 return width_of_tabular;
658 /* returns 1 if a complete update is necessary, otherwise 0 */
659 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
661 if (!IsMultiColumn(cell))
664 int const row = row_of_cell(cell);
665 int const column1 = column_of_cell(cell);
666 int const column2 = right_column_of_cell(cell);
667 int const old_val = cell_info[row][column2].width_of_cell;
669 // first set columns to 0 so we can calculate the right width
670 for (int i = column1; i <= column2; ++i) {
671 cell_info[row][i].width_of_cell = 0;
673 // set the width to MAX_WIDTH until width > 0
674 int width = (new_width + 2 * WIDTH_OF_LINE);
676 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
677 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
678 width -= column_info[i].width_of_column;
681 cell_info[row][i].width_of_cell = width;
683 if (old_val != cell_info[row][column2].width_of_cell) {
684 // in this case we have to recalculate all multicolumn cells which
685 // have this column as one of theirs but not as last one
686 calculate_width_of_column_NMC(i);
687 recalculateMulticolumnsOfColumn(i);
688 calculate_width_of_column(i);
694 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
696 // the last column does not have to be recalculated because all
697 // multicolumns will have here there last multicolumn cell which
698 // always will have the whole rest of the width of the cell.
699 if (column > (columns_ - 2))
701 for(int row = 0; row < rows_; ++row) {
702 int mc = cell_info[row][column].multicolumn;
703 int nmc = cell_info[row][column+1].multicolumn;
704 // we only have to update multicolumns which do not have this
705 // column as their last column!
706 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
707 ((mc == CELL_PART_OF_MULTICOLUMN) &&
708 (nmc == CELL_PART_OF_MULTICOLUMN)))
710 int const cellno = cell_info[row][column].cellno;
711 SetWidthOfMulticolCell(cellno,
712 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
718 /* returns 1 if a complete update is necessary, otherwise 0 */
719 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
721 int const row = row_of_cell(cell);
722 int const column1 = column_of_cell(cell);
727 #ifdef SPECIAL_COLUM_HANDLING
728 if (RightLine(cell_info[row][column1].cellno, true) &&
729 (column1 < columns_-1) &&
730 LeftLine(cell_info[row][column1+1].cellno, true))
732 if (column_info[column1].right_line && (column1 < columns_-1) &&
733 column_info[column1+1].left_line) // additional width
737 add_width = WIDTH_OF_LINE;
739 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
742 if (IsMultiColumn(cell, true)) {
743 tmp = SetWidthOfMulticolCell(cell, new_width);
745 width = (new_width + 2*WIDTH_OF_LINE + add_width);
746 cell_info[row][column1].width_of_cell = width;
747 tmp = calculate_width_of_column_NMC(column1);
749 recalculateMulticolumnsOfColumn(column1);
752 for (int i = 0; i < columns_; ++i)
753 calculate_width_of_column(i);
754 calculate_width_of_tabular();
761 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
763 if (!IsMultiColumn(cell) || onlycolumn)
764 column_info[column_of_cell(cell)].alignment = align;
766 cellinfo_of_cell(cell)->alignment = align;
771 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
773 if (!IsMultiColumn(cell) || onlycolumn)
774 column_info[column_of_cell(cell)].valignment = align;
776 cellinfo_of_cell(cell)->valignment = align;
781 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
783 bool flag = !width.zero();
784 int const j = column_of_cell(cell);
786 column_info[j].p_width = width;
787 // This should not ne necessary anymore
788 // if (flag) // do this only if there is a width
789 // SetAlignment(cell, LYX_ALIGN_LEFT);
790 for (int i = 0; i < rows_; ++i) {
791 int c = GetCellNumber(i, j);
792 flag = !GetPWidth(c).zero(); // because of multicolumns!
793 GetCellInset(c)->setAutoBreakRows(flag);
799 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
801 bool const flag = !width.zero();
803 cellinfo_of_cell(cell)->p_width = width;
804 if (IsMultiColumn(cell)) {
805 GetCellInset(cell)->setAutoBreakRows(flag);
812 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
813 LyXTabular::Feature what)
815 if (what == SET_SPECIAL_MULTI)
816 cellinfo_of_cell(cell)->align_special = special;
818 column_info[column_of_cell(cell)].align_special = special;
823 bool LyXTabular::SetAllLines(int cell, bool line)
825 SetTopLine(cell, line);
826 SetBottomLine(cell, line);
827 SetRightLine(cell, line);
828 SetLeftLine(cell, line);
833 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
835 int const row = row_of_cell(cell);
837 if (onlycolumn || !IsMultiColumn(cell))
838 row_info[row].top_line = line;
840 cellinfo_of_cell(cell)->top_line = line;
845 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
847 if (onlycolumn || !IsMultiColumn(cell))
848 row_info[row_of_cell(cell)].bottom_line = line;
850 cellinfo_of_cell(cell)->bottom_line = line;
855 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
857 if (onlycolumn || !IsMultiColumn(cell))
858 column_info[column_of_cell(cell)].left_line = line;
860 cellinfo_of_cell(cell)->left_line = line;
865 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
867 if (onlycolumn || !IsMultiColumn(cell))
868 column_info[right_column_of_cell(cell)].right_line = line;
870 cellinfo_of_cell(cell)->right_line = line;
875 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
877 if (!onlycolumn && IsMultiColumn(cell))
878 return cellinfo_of_cell(cell)->alignment;
880 return column_info[column_of_cell(cell)].alignment;
884 LyXTabular::VAlignment
885 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
887 if (!onlycolumn && IsMultiColumn(cell))
888 return cellinfo_of_cell(cell)->valignment;
890 return column_info[column_of_cell(cell)].valignment;
894 LyXLength const LyXTabular::GetPWidth(int cell) const
896 if (IsMultiColumn(cell))
897 return cellinfo_of_cell(cell)->p_width;
898 return column_info[column_of_cell(cell)].p_width;
902 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
904 return column_info[column_of_cell(cell)].p_width;
908 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
910 if (IsMultiColumn(cell))
911 return cellinfo_of_cell(cell)->p_width;
916 string const LyXTabular::GetAlignSpecial(int cell, int what) const
918 if (what == SET_SPECIAL_MULTI)
919 return cellinfo_of_cell(cell)->align_special;
920 return column_info[column_of_cell(cell)].align_special;
924 int LyXTabular::GetWidthOfCell(int cell) const
926 int const row = row_of_cell(cell);
927 int const column1 = column_of_cell(cell);
928 int const column2 = right_column_of_cell(cell);
930 for (int i = column1; i <= column2; ++i) {
931 result += cell_info[row][i].width_of_cell;
937 int LyXTabular::GetBeginningOfTextInCell(int cell) const
941 switch (GetAlignment(cell)) {
942 case LYX_ALIGN_CENTER:
943 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
945 case LYX_ALIGN_RIGHT:
946 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
947 // + GetAdditionalWidth(cell);
949 default: /* LYX_ALIGN_LEFT: nothing :-) */
959 bool LyXTabular::IsFirstCellInRow(int cell) const
961 return column_of_cell(cell) == 0;
965 int LyXTabular::GetFirstCellInRow(int row) const
969 return cell_info[row][0].cellno;
972 bool LyXTabular::IsLastCellInRow(int cell) const
974 return (right_column_of_cell(cell) == (columns_ - 1));
978 int LyXTabular::GetLastCellInRow(int row) const
982 return cell_info[row][columns_-1].cellno;
986 bool LyXTabular::calculate_width_of_column(int column)
988 int const old_column_width = column_info[column].width_of_column;
991 for (int i = 0; i < rows_; ++i) {
992 maximum = max(cell_info[i][column].width_of_cell, maximum);
994 column_info[column].width_of_column = maximum;
995 return (column_info[column].width_of_column != old_column_width);
1000 // Calculate the columns regarding ONLY the normal cells and if this
1001 // column is inside a multicolumn cell then use it only if its the last
1002 // column of this multicolumn cell as this gives an added with to the
1003 // column, all the rest should be adapted!
1005 bool LyXTabular::calculate_width_of_column_NMC(int column)
1007 int const old_column_width = column_info[column].width_of_column;
1009 for (int i = 0; i < rows_; ++i) {
1010 int cell = GetCellNumber(i, column);
1011 bool ismulti = IsMultiColumn(cell, true);
1012 if ((!ismulti || (column == right_column_of_cell(cell))) &&
1013 (cell_info[i][column].width_of_cell > max))
1015 max = cell_info[i][column].width_of_cell;
1018 column_info[column].width_of_column = max;
1019 return (column_info[column].width_of_column != old_column_width);
1023 void LyXTabular::calculate_width_of_tabular()
1025 width_of_tabular = 0;
1026 for (int i = 0; i < columns_; ++i) {
1027 width_of_tabular += column_info[i].width_of_column;
1032 int LyXTabular::row_of_cell(int cell) const
1034 if (cell >= numberofcells)
1038 return rowofcell[cell];
1042 int LyXTabular::column_of_cell(int cell) const
1044 if (cell >= numberofcells)
1045 return columns_ - 1;
1048 return columnofcell[cell];
1052 int LyXTabular::right_column_of_cell(int cell) const
1054 int const row = row_of_cell(cell);
1055 int column = column_of_cell(cell);
1056 while (column < (columns_ - 1) &&
1057 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1063 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1067 << write_attribute("version", 3)
1068 << write_attribute("rows", rows_)
1069 << write_attribute("columns", columns_)
1071 // global longtable options
1073 << write_attribute("rotate", rotate)
1074 << write_attribute("islongtable", is_long_tabular)
1075 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1076 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1077 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1078 << write_attribute("headTopDL", endhead.topDL)
1079 << write_attribute("headBottomDL", endhead.bottomDL)
1080 << write_attribute("footTopDL", endfoot.topDL)
1081 << write_attribute("footBottomDL", endfoot.bottomDL)
1082 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1083 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1084 << write_attribute("lastFootEmpty", endlastfoot.empty)
1086 for (int j = 0; j < columns_; ++j) {
1088 << write_attribute("alignment", column_info[j].alignment)
1089 << write_attribute("valignment", column_info[j].valignment)
1090 << write_attribute("leftline", column_info[j].left_line)
1091 << write_attribute("rightline", column_info[j].right_line)
1092 << write_attribute("width", column_info[j].p_width.asString())
1093 << write_attribute("special", column_info[j].align_special)
1096 for (int i = 0; i < rows_; ++i) {
1098 << write_attribute("topline", row_info[i].top_line)
1099 << write_attribute("bottomline", row_info[i].bottom_line)
1100 << write_attribute("endhead", row_info[i].endhead)
1101 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1102 << write_attribute("endfoot", row_info[i].endfoot)
1103 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1104 << write_attribute("newpage", row_info[i].newpage)
1106 for (int j = 0; j < columns_; ++j) {
1108 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1109 << write_attribute("alignment", cell_info[i][j].alignment)
1110 << write_attribute("valignment", cell_info[i][j].valignment)
1111 << write_attribute("topline", cell_info[i][j].top_line)
1112 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1113 << write_attribute("leftline", cell_info[i][j].left_line)
1114 << write_attribute("rightline", cell_info[i][j].right_line)
1115 << write_attribute("rotate", cell_info[i][j].rotate)
1116 << write_attribute("usebox", cell_info[i][j].usebox)
1117 << write_attribute("width", cell_info[i][j].p_width)
1118 << write_attribute("special", cell_info[i][j].align_special)
1120 os << "\\begin_inset ";
1121 cell_info[i][j].inset.write(buf, os);
1122 os << "\n\\end_inset \n"
1127 os << "</lyxtabular>\n";
1131 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1134 istream & is = lex.getStream();
1136 l_getline(is, line);
1137 if (!prefixIs(line, "<lyxtabular ")
1138 && !prefixIs(line, "<LyXTabular ")) {
1144 if (!getTokenValue(line, "version", version))
1146 lyx::Assert(version >= 2);
1147 read(buf, is, lex, line, version);
1150 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1154 row_info[--hr].endhead = true;
1156 // set firstheader info
1157 if (fhr && (fhr < rows_)) {
1158 if (row_info[fhr].endhead) {
1160 row_info[--fhr].endfirsthead = true;
1161 row_info[fhr].endhead = false;
1163 } else if (row_info[fhr - 1].endhead) {
1164 endfirsthead.empty = true;
1166 while((fhr > 0) && !row_info[--fhr].endhead) {
1167 row_info[fhr].endfirsthead = true;
1172 if (fr && (fr < rows_)) {
1173 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1174 while((fr > 0) && !row_info[--fr].endhead) {
1175 row_info[fr].endfoot = true;
1176 row_info[fr].endhead = false;
1178 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1179 while((fr > 0) && !row_info[--fr].endfirsthead) {
1180 row_info[fr].endfoot = true;
1181 row_info[fr].endfirsthead = false;
1183 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1184 while((fr > 0) && !row_info[--fr].endhead &&
1185 !row_info[fr].endfirsthead)
1187 row_info[fr].endfoot = true;
1191 // set lastfooter info
1192 if (lfr && (lfr < rows_)) {
1193 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1194 while((lfr > 0) && !row_info[--lfr].endhead) {
1195 row_info[lfr].endlastfoot = true;
1196 row_info[lfr].endhead = false;
1198 } else if (row_info[lfr].endfirsthead &&
1199 row_info[lfr - 1].endfirsthead)
1201 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1202 row_info[lfr].endlastfoot = true;
1203 row_info[lfr].endfirsthead = false;
1205 } else if (row_info[lfr].endfoot
1206 && row_info[lfr - 1].endfoot) {
1207 while((lfr > 0) && !row_info[--lfr].endfoot) {
1208 row_info[lfr].endlastfoot = true;
1209 row_info[lfr].endfoot = false;
1211 } else if (!row_info[fr - 1].endhead
1212 && !row_info[fr - 1].endfirsthead &&
1213 !row_info[fr - 1].endfoot)
1216 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1217 !row_info[lfr].endfoot)
1219 row_info[lfr].endlastfoot = true;
1221 } else if (haveLTFoot()) {
1222 endlastfoot.empty = true;
1227 void LyXTabular::read(Buffer const * buf, istream & is,
1228 LyXLex & lex, string const & l, int const version)
1232 if (!getTokenValue(line, "rows", rows_arg))
1235 if (!getTokenValue(line, "columns", columns_arg))
1237 Init(buf->params, rows_arg, columns_arg);
1238 l_getline(is, line);
1239 if (!prefixIs(line, "<features")) {
1240 lyxerr << "Wrong tabular format (expected <features ...> got"
1241 << line << ')' << endl;
1244 getTokenValue(line, "rotate", rotate);
1245 getTokenValue(line, "islongtable", is_long_tabular);
1246 // compatibility read for old longtable options. Now we can make any
1247 // row part of the header/footer type we want before it was strict
1248 // sequential from the first row down (as LaTeX does it!). So now when
1249 // we find a header/footer line we have to go up the rows and set it
1250 // on all preceding rows till the first or one with already a h/f option
1251 // set. If we find a firstheader on the same line as a header or a
1252 // lastfooter on the same line as a footer then this should be set empty.
1260 getTokenValue(line, "endhead", hrow);
1261 getTokenValue(line, "endfirsthead", fhrow);
1262 getTokenValue(line, "endfoot", frow);
1263 getTokenValue(line, "endlastfoot", lfrow);
1264 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1266 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1267 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1268 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1269 getTokenValue(line, "headTopDL", endhead.topDL);
1270 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1271 getTokenValue(line, "footTopDL", endfoot.topDL);
1272 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1273 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1274 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1275 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1277 for (int j = 0; j < columns_; ++j) {
1279 if (!prefixIs(line,"<column")) {
1280 lyxerr << "Wrong tabular format (expected <column ...> got"
1281 << line << ')' << endl;
1284 getTokenValue(line, "alignment", column_info[j].alignment);
1285 getTokenValue(line, "valignment", column_info[j].valignment);
1286 getTokenValue(line, "leftline", column_info[j].left_line);
1287 getTokenValue(line, "rightline", column_info[j].right_line);
1288 getTokenValue(line, "width", column_info[j].p_width);
1289 getTokenValue(line, "special", column_info[j].align_special);
1292 for (int i = 0; i < rows_; ++i) {
1293 l_getline(is, line);
1294 if (!prefixIs(line, "<row")) {
1295 lyxerr << "Wrong tabular format (expected <row ...> got"
1296 << line << ')' << endl;
1299 getTokenValue(line, "topline", row_info[i].top_line);
1300 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1301 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1302 getTokenValue(line, "endhead", row_info[i].endhead);
1303 getTokenValue(line, "endfoot", row_info[i].endfoot);
1304 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1305 getTokenValue(line, "newpage", row_info[i].newpage);
1306 for (int j = 0; j < columns_; ++j) {
1307 l_getline(is, line);
1308 if (!prefixIs(line, "<cell")) {
1309 lyxerr << "Wrong tabular format (expected <cell ...> got"
1310 << line << ')' << endl;
1313 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1314 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1315 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1316 getTokenValue(line, "topline", cell_info[i][j].top_line);
1317 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1318 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1319 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1320 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1321 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1322 getTokenValue(line, "width", cell_info[i][j].p_width);
1323 getTokenValue(line, "special", cell_info[i][j].align_special);
1324 l_getline(is, line);
1325 if (prefixIs(line, "\\begin_inset")) {
1326 cell_info[i][j].inset.read(buf, lex);
1327 l_getline(is, line);
1329 if (!prefixIs(line, "</cell>")) {
1330 lyxerr << "Wrong tabular format (expected </cell> got"
1331 << line << ')' << endl;
1335 l_getline(is, line);
1336 if (!prefixIs(line, "</row>")) {
1337 lyxerr << "Wrong tabular format (expected </row> got"
1338 << line << ')' << endl;
1342 while (!prefixIs(line, "</lyxtabular>")) {
1343 l_getline(is, line);
1345 set_row_column_number_info();
1349 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1351 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1352 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1356 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1358 int const row = row_of_cell(cell);
1359 int const column = column_of_cell(cell);
1360 return &cell_info[row][column];
1364 void LyXTabular::SetMultiColumn(Buffer * buffer, int cell, int number)
1366 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1367 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1368 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1369 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1370 cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1372 for (int i = 1; i < number; ++i) {
1373 cellinfo_of_cell(cell + i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1374 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer,
1375 cellinfo_of_cell(cell+i)->inset.paragraphs);
1376 cellinfo_of_cell(cell + i)->inset.clear(false);
1379 for (number--; number > 0; --number) {
1380 cellinfo_of_cell(cell + number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1383 set_row_column_number_info();
1387 int LyXTabular::cells_in_multicolumn(int cell) const
1389 int const row = row_of_cell(cell);
1390 int column = column_of_cell(cell);
1393 while ((column < columns_) &&
1394 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1403 int LyXTabular::UnsetMultiColumn(int cell)
1405 int const row = row_of_cell(cell);
1406 int column = column_of_cell(cell);
1410 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1411 cell_info[row][column].multicolumn = CELL_NORMAL;
1413 while ((column < columns_) &&
1414 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1416 cell_info[row][column].multicolumn = CELL_NORMAL;
1421 set_row_column_number_info();
1426 void LyXTabular::SetLongTabular(bool what)
1428 is_long_tabular = what;
1432 bool LyXTabular::IsLongTabular() const
1434 return is_long_tabular;
1438 void LyXTabular::SetRotateTabular(bool flag)
1444 bool LyXTabular::GetRotateTabular() const
1450 void LyXTabular::SetRotateCell(int cell, bool flag)
1452 cellinfo_of_cell(cell)->rotate = flag;
1456 bool LyXTabular::GetRotateCell(int cell) const
1458 return cellinfo_of_cell(cell)->rotate;
1462 bool LyXTabular::NeedRotating() const
1466 for (int i = 0; i < rows_; ++i) {
1467 for (int j = 0; j < columns_; ++j) {
1468 if (cell_info[i][j].rotate)
1476 bool LyXTabular::IsLastCell(int cell) const
1478 if ((cell + 1) < numberofcells)
1484 int LyXTabular::GetCellAbove(int cell) const
1486 if (row_of_cell(cell) > 0)
1487 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1492 int LyXTabular::GetCellBelow(int cell) const
1494 if (row_of_cell(cell) + 1 < rows_)
1495 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1500 int LyXTabular::GetLastCellAbove(int cell) const
1502 if (row_of_cell(cell) <= 0)
1504 if (!IsMultiColumn(cell))
1505 return GetCellAbove(cell);
1506 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1510 int LyXTabular::GetLastCellBelow(int cell) const
1512 if (row_of_cell(cell) + 1 >= rows_)
1514 if (!IsMultiColumn(cell))
1515 return GetCellBelow(cell);
1516 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1520 int LyXTabular::GetCellNumber(int row, int column) const
1523 if (column >= columns_)
1524 column = columns_ - 1;
1525 else if (column < 0)
1532 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1534 return cell_info[row][column].cellno;
1538 void LyXTabular::SetUsebox(int cell, BoxType type)
1540 cellinfo_of_cell(cell)->usebox = type;
1544 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1546 if (column_info[column_of_cell(cell)].p_width.zero() &&
1547 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1549 if (cellinfo_of_cell(cell)->usebox > 1)
1550 return cellinfo_of_cell(cell)->usebox;
1551 return UseParbox(cell);
1556 // This are functions used for the longtable support
1558 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1563 row_info[row].endfirsthead = flag;
1567 row_info[row].endhead = flag;
1572 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1575 hd.set = haveLTHead();
1576 return row_info[row].endhead;
1580 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1583 hd.set = haveLTFirstHead();
1584 return row_info[row].endfirsthead;
1588 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1593 row_info[row].endlastfoot = flag;
1597 row_info[row].endfoot = flag;
1602 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1605 fd.set = haveLTFoot();
1606 return row_info[row].endfoot;
1610 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1613 fd.set = haveLTLastFoot();
1614 return row_info[row].endlastfoot;
1618 void LyXTabular::SetLTNewPage(int row, bool what)
1620 row_info[row].newpage = what;
1624 bool LyXTabular::GetLTNewPage(int row) const
1626 return row_info[row].newpage;
1630 bool LyXTabular::haveLTHead() const
1632 for(int i=0; i < rows_; ++i) {
1633 if (row_info[i].endhead)
1640 bool LyXTabular::haveLTFirstHead() const
1642 if (endfirsthead.empty)
1644 for(int i=0; i < rows_; ++i) {
1645 if (row_info[i].endfirsthead)
1652 bool LyXTabular::haveLTFoot() const
1654 for(int i=0; i < rows_; ++i) {
1655 if (row_info[i].endfoot)
1662 bool LyXTabular::haveLTLastFoot() const
1664 if (endlastfoot.empty)
1666 for(int i=0; i < rows_; ++i) {
1667 if (row_info[i].endlastfoot)
1674 // end longtable support functions
1676 bool LyXTabular::SetAscentOfRow(int row, int height)
1678 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1680 row_info[row].ascent_of_row = height;
1685 bool LyXTabular::SetDescentOfRow(int row, int height)
1687 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1689 row_info[row].descent_of_row = height;
1694 int LyXTabular::GetAscentOfRow(int row) const
1698 return row_info[row].ascent_of_row;
1702 int LyXTabular::GetDescentOfRow(int row) const
1706 return row_info[row].descent_of_row;
1710 int LyXTabular::GetHeightOfTabular() const
1714 for (int row = 0; row < rows_; ++row)
1715 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1716 GetAdditionalHeight(row);
1721 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1723 if ((row >= rows_) || (column >= columns_))
1725 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1729 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1731 if ((row < 0) || (row >= rows_))
1734 int const fcell = GetFirstCellInRow(row);
1735 int const n = NumberOfCellsInRow(fcell) + fcell;
1738 for (int i = fcell; i < n; ++i) {
1742 if (tmp == (n - fcell)) {
1745 for (int i = fcell; i < n; ++i) {
1748 << column_of_cell(i) + 1
1750 << right_column_of_cell(i) + 1
1762 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1764 if ((row < 0) || (row >= rows_))
1767 int const fcell = GetFirstCellInRow(row);
1768 int const n = NumberOfCellsInRow(fcell) + fcell;
1771 for (int i = fcell; i < n; ++i) {
1775 if (tmp == (n - fcell)) {
1778 for (int i = fcell; i < n; ++i) {
1779 if (BottomLine(i)) {
1781 << column_of_cell(i) + 1
1783 << right_column_of_cell(i) + 1
1795 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1799 if (GetRotateCell(cell)) {
1800 os << "\\begin{sideways}\n";
1803 if (IsMultiColumn(cell)) {
1804 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1805 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1806 os << cellinfo_of_cell(cell)->align_special << "}{";
1808 if (LeftLine(cell) &&
1809 (IsFirstCellInRow(cell) ||
1810 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1811 !RightLine(cell-1, true))))
1815 if (!GetPWidth(cell).zero()) {
1816 switch (GetVAlignment(cell)) {
1817 case LYX_VALIGN_TOP:
1820 case LYX_VALIGN_CENTER:
1823 case LYX_VALIGN_BOTTOM:
1828 << GetPWidth(cell).asLatexString()
1831 switch (GetAlignment(cell)) {
1832 case LYX_ALIGN_LEFT:
1835 case LYX_ALIGN_RIGHT:
1843 if (RightLine(cell))
1845 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1851 if (GetUsebox(cell) == BOX_PARBOX) {
1853 switch (GetVAlignment(cell)) {
1854 case LYX_VALIGN_TOP:
1857 case LYX_VALIGN_CENTER:
1860 case LYX_VALIGN_BOTTOM:
1864 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
1865 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
1866 os << "\\begin{minipage}[";
1867 switch (GetVAlignment(cell)) {
1868 case LYX_VALIGN_TOP:
1871 case LYX_VALIGN_CENTER:
1874 case LYX_VALIGN_BOTTOM:
1878 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
1885 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1890 if (GetUsebox(cell) == BOX_PARBOX)
1892 else if (GetUsebox(cell) == BOX_MINIPAGE) {
1893 os << "%\n\\end{minipage}";
1896 if (IsMultiColumn(cell)) {
1899 if (GetRotateCell(cell)) {
1900 os << "%\n\\end{sideways}";
1907 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
1908 LatexRunParams const & runparams) const
1910 if (!is_long_tabular)
1914 // output header info
1916 if (endhead.topDL) {
1920 for (int i = 0; i < rows_; ++i) {
1921 if (row_info[i].endhead) {
1922 ret += TeXRow(os, i, buf, runparams);
1925 if (endhead.bottomDL) {
1929 os << "\\endhead\n";
1931 if (endfirsthead.empty) {
1932 os << "\\endfirsthead\n";
1936 // output firstheader info
1937 if (haveLTFirstHead()) {
1938 if (endfirsthead.topDL) {
1942 for (int i = 0; i < rows_; ++i) {
1943 if (row_info[i].endfirsthead) {
1944 ret += TeXRow(os, i, buf, runparams);
1947 if (endfirsthead.bottomDL) {
1951 os << "\\endfirsthead\n";
1954 // output footer info
1956 if (endfoot.topDL) {
1960 for (int i = 0; i < rows_; ++i) {
1961 if (row_info[i].endfoot) {
1962 ret += TeXRow(os, i, buf, runparams);
1965 if (endfoot.bottomDL) {
1969 os << "\\endfoot\n";
1971 if (endlastfoot.empty) {
1972 os << "\\endlastfoot\n";
1976 // output lastfooter info
1977 if (haveLTLastFoot()) {
1978 if (endlastfoot.topDL) {
1982 for (int i = 0; i < rows_; ++i) {
1983 if (row_info[i].endlastfoot) {
1984 ret += TeXRow(os, i, buf, runparams);
1987 if (endlastfoot.bottomDL) {
1991 os << "\\endlastfoot\n";
1998 bool LyXTabular::isValidRow(int const row) const
2000 if (!is_long_tabular)
2002 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2003 !row_info[row].endfoot && !row_info[row].endlastfoot);
2007 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2008 LatexRunParams const & runparams) const
2011 int cell = GetCellNumber(i, 0);
2013 ret += TeXTopHLine(os, i);
2014 for (int j = 0; j < columns_; ++j) {
2015 if (IsPartOfMultiColumn(i,j))
2017 ret += TeXCellPreamble(os, cell);
2018 InsetText * inset = GetCellInset(cell);
2020 bool rtl = inset->paragraphs.begin()->isRightToLeftPar(buf->params) &&
2021 !inset->paragraphs.begin()->empty() && GetPWidth(cell).zero();
2025 ret += inset->latex(buf, os, runparams);
2029 ret += TeXCellPostamble(os, cell);
2030 if (!IsLastCellInRow(cell)) { // not last cell in row
2036 os << "\\tabularnewline\n";
2038 ret += TeXBottomHLine(os, i);
2043 int LyXTabular::latex(Buffer const * buf, ostream & os,
2044 LatexRunParams const & runparams) const
2048 //+---------------------------------------------------------------------
2049 //+ first the opening preamble +
2050 //+---------------------------------------------------------------------
2053 os << "\\begin{sideways}\n";
2056 if (is_long_tabular)
2057 os << "\\begin{longtable}{";
2059 os << "\\begin{tabular}{";
2060 for (int i = 0; i < columns_; ++i) {
2061 if (!column_info[i].align_special.empty()) {
2062 os << column_info[i].align_special;
2064 if (column_info[i].left_line)
2066 if (!column_info[i].p_width.zero()) {
2067 switch (column_info[i].alignment) {
2068 case LYX_ALIGN_LEFT:
2069 os << ">{\\raggedright}";
2071 case LYX_ALIGN_RIGHT:
2072 os << ">{\\raggedleft}";
2074 case LYX_ALIGN_CENTER:
2075 os << ">{\\centering}";
2077 case LYX_ALIGN_NONE:
2078 case LYX_ALIGN_BLOCK:
2079 case LYX_ALIGN_LAYOUT:
2080 case LYX_ALIGN_SPECIAL:
2084 switch (column_info[i].valignment) {
2085 case LYX_VALIGN_TOP:
2088 case LYX_VALIGN_CENTER:
2091 case LYX_VALIGN_BOTTOM:
2096 << column_info[i].p_width.asLatexString()
2099 switch (column_info[i].alignment) {
2100 case LYX_ALIGN_LEFT:
2103 case LYX_ALIGN_RIGHT:
2111 if (column_info[i].right_line)
2118 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2120 //+---------------------------------------------------------------------
2121 //+ the single row and columns (cells) +
2122 //+---------------------------------------------------------------------
2124 for (int i = 0; i < rows_; ++i) {
2125 if (isValidRow(i)) {
2126 ret += TeXRow(os, i, buf, runparams);
2127 if (is_long_tabular && row_info[i].newpage) {
2128 os << "\\newpage\n";
2134 //+---------------------------------------------------------------------
2135 //+ the closing of the tabular +
2136 //+---------------------------------------------------------------------
2138 if (is_long_tabular)
2139 os << "\\end{longtable}";
2141 os << "\\end{tabular}";
2143 os << "\n\\end{sideways}";
2151 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2154 int cell = GetFirstCellInRow(row);
2157 for (int j = 0; j < columns_; ++j) {
2158 if (IsPartOfMultiColumn(row, j))
2161 os << "<entry align=\"";
2162 switch (GetAlignment(cell)) {
2163 case LYX_ALIGN_LEFT:
2166 case LYX_ALIGN_RIGHT:
2174 os << "\" valign=\"";
2175 switch (GetVAlignment(cell)) {
2176 case LYX_VALIGN_TOP:
2179 case LYX_VALIGN_BOTTOM:
2182 case LYX_VALIGN_CENTER:
2187 if (IsMultiColumn(cell)) {
2188 os << " namest=\"col" << j << "\" ";
2189 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2193 ret += GetCellInset(cell)->docbook(buf, os, true);
2202 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2203 bool /*mixcont*/) const
2207 //+---------------------------------------------------------------------
2208 //+ first the opening preamble +
2209 //+---------------------------------------------------------------------
2211 os << "<tgroup cols=\"" << columns_
2212 << "\" colsep=\"1\" rowsep=\"1\">\n";
2214 for (int i = 0; i < columns_; ++i) {
2215 os << "<colspec colname=\"col" << i << "\" align=\"";
2216 switch (column_info[i].alignment) {
2217 case LYX_ALIGN_LEFT:
2220 case LYX_ALIGN_RIGHT:
2231 //+---------------------------------------------------------------------
2232 //+ Long Tabular case +
2233 //+---------------------------------------------------------------------
2235 // output header info
2236 if (haveLTHead() || haveLTFirstHead()) {
2239 for (int i = 0; i < rows_; ++i) {
2240 if (row_info[i].endhead || row_info[i].endfirsthead) {
2241 ret += docbookRow(buf, os, i);
2247 // output footer info
2248 if (haveLTFoot() || haveLTLastFoot()) {
2251 for (int i = 0; i < rows_; ++i) {
2252 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2253 ret += docbookRow(buf, os, i);
2260 //+---------------------------------------------------------------------
2261 //+ the single row and columns (cells) +
2262 //+---------------------------------------------------------------------
2266 for (int i = 0; i < rows_; ++i) {
2267 if (isValidRow(i)) {
2268 ret += docbookRow(buf, os, i);
2273 //+---------------------------------------------------------------------
2274 //+ the closing of the tabular +
2275 //+---------------------------------------------------------------------
2284 // ASCII export function and helpers
2286 int LyXTabular::asciiTopHLine(ostream & os, int row,
2287 vector<unsigned int> const & clen) const
2289 int const fcell = GetFirstCellInRow(row);
2290 int const n = NumberOfCellsInRow(fcell) + fcell;
2293 for (int i = fcell; i < n; ++i) {
2303 for (int i = fcell; i < n; ++i) {
2314 int column = column_of_cell(i);
2315 int len = clen[column];
2316 while (IsPartOfMultiColumn(row, ++column))
2317 len += clen[column] + 4;
2318 os << string(len, ch);
2333 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2334 vector<unsigned int> const & clen) const
2336 int const fcell = GetFirstCellInRow(row);
2337 int const n = NumberOfCellsInRow(fcell) + fcell;
2340 for (int i = fcell; i < n; ++i) {
2341 if (BottomLine(i)) {
2350 for (int i = fcell; i < n; ++i) {
2351 if (BottomLine(i)) {
2361 int column = column_of_cell(i);
2362 int len = clen[column];
2363 while (IsPartOfMultiColumn(row, ++column))
2364 len += clen[column] + 4;
2365 os << string(len, ch);
2366 if (BottomLine(i)) {
2380 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2381 int cell, int row, int column,
2382 vector<unsigned int> const & clen,
2383 bool onlydata) const
2386 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2398 unsigned int len1 = sstr.str().length();
2399 unsigned int len2 = clen[column];
2400 while (IsPartOfMultiColumn(row, ++column))
2401 len2 += clen[column] + 4;
2404 switch (GetAlignment(cell)) {
2406 case LYX_ALIGN_LEFT:
2409 case LYX_ALIGN_RIGHT:
2413 case LYX_ALIGN_CENTER:
2419 os << string(len1, ' ')
2421 << string(len2, ' ');
2422 if (RightLine(cell))
2431 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2432 bool onlydata, unsigned char delim) const
2436 //+---------------------------------------------------------------------
2437 //+ first calculate the width of the single columns +
2438 //+---------------------------------------------------------------------
2439 vector<unsigned int> clen(columns_);
2442 // first all non (real) multicolumn cells!
2443 for (int j = 0; j < columns_; ++j) {
2445 for (int i = 0; i < rows_; ++i) {
2446 int cell = GetCellNumber(i, j);
2447 if (IsMultiColumn(cell, true))
2450 GetCellInset(cell)->ascii(buf, sstr, 0);
2451 if (clen[j] < sstr.str().length())
2452 clen[j] = sstr.str().length();
2455 // then all (real) multicolumn cells!
2456 for (int j = 0; j < columns_; ++j) {
2457 for (int i = 0; i < rows_; ++i) {
2458 int cell = GetCellNumber(i, j);
2459 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2462 GetCellInset(cell)->ascii(buf, sstr, 0);
2463 int len = int(sstr.str().length());
2464 int const n = cells_in_multicolumn(cell);
2465 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2467 if (len > int(clen[j + n - 1]))
2468 clen[j + n - 1] = len;
2473 for (int i = 0; i < rows_; ++i) {
2475 if (asciiTopHLine(os, i, clen)) {
2476 for (int j = 0; j < depth; ++j)
2480 for (int j = 0; j < columns_; ++j) {
2481 if (IsPartOfMultiColumn(i,j))
2483 if (onlydata && j > 0)
2485 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2490 for (int j = 0; j < depth; ++j)
2492 if (asciiBottomHLine(os, i, clen)) {
2493 for (int j = 0; j < depth; ++j)
2505 InsetText * LyXTabular::GetCellInset(int cell) const
2508 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2512 InsetText * LyXTabular::GetCellInset(int row, int column) const
2514 cur_cell = GetCellNumber(row, column);
2515 return & cell_info[row][column].inset;
2519 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2521 // is this inset part of the tabular?
2522 if (!inset || inset->owner() != owner_) {
2523 lyxerr[Debug::INSETTEXT]
2524 << "this is not a cell of the tabular!" << endl;
2528 const int save_cur_cell = cur_cell;
2529 int cell = cur_cell;
2530 if (GetCellInset(cell) != inset) {
2532 if (cell == -1 || GetCellInset(cell) != inset) {
2538 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2539 if (GetCellInset(cell) == inset)
2542 lyxerr[Debug::INSETTEXT]
2543 << "LyXTabular::GetCellFromInset: "
2545 << ", cur_cell=" << save_cur_cell
2546 << ", maybe_cell=" << maybe_cell
2548 // We should have found a cell at this point
2550 lyxerr << "LyXTabular::GetCellFromInset: "
2551 << "Cell not found!" << endl;
2559 void LyXTabular::Validate(LaTeXFeatures & features) const
2561 features.require("NeedTabularnewline");
2562 if (IsLongTabular())
2563 features.require("longtable");
2565 features.require("rotating");
2566 for (int cell = 0; cell < numberofcells; ++cell) {
2567 if ( (GetVAlignment(cell) != LYX_VALIGN_TOP) ||
2568 ( !(GetPWidth(cell).zero())&&!(IsMultiColumn(cell)) )
2570 features.require("array");
2571 GetCellInset(cell)->validate(features);
2576 vector<string> const LyXTabular::getLabelList() const
2578 vector<string> label_list;
2579 for (int i = 0; i < rows_; ++i)
2580 for (int j = 0; j < columns_; ++j) {
2581 vector<string> const l =
2582 GetCellInset(i, j)->getLabelList();
2583 label_list.insert(label_list.end(),
2584 l.begin(), l.end());
2590 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2592 ParagraphList const & parlist = GetCellInset(cell)->paragraphs;
2593 ParagraphList::const_iterator cit = parlist.begin();
2594 ParagraphList::const_iterator end = parlist.end();
2596 for (; cit != end; ++cit) {
2597 for (int i = 0; i < cit->size(); ++i) {
2598 if (cit->isNewline(i))