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 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
27 #include "BufferView.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
37 #include "tabular_funcs.h"
49 #ifndef CXX_GLOBAL_CSTD
55 int const WIDTH_OF_LINE = 5;
59 /// Define a few methods for the inner structs
61 LyXTabular::cellstruct::cellstruct()
65 multicolumn = LyXTabular::CELL_NORMAL;
66 alignment = LYX_ALIGN_CENTER;
67 valignment = LYX_VALIGN_TOP;
77 LyXTabular::rowstruct::rowstruct()
91 LyXTabular::columnstruct::columnstruct()
95 alignment = LYX_ALIGN_CENTER;
96 valignment = LYX_VALIGN_TOP;
101 LyXTabular::lttype::lttype()
110 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
114 Init(rows_arg, columns_arg);
118 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
123 Init(lt.rows_, lt.columns_, <);
124 // we really should change again to have InsetText as a pointer
125 // and allocate it then we would not have to do this stuff all
128 for (int i = 0; i < rows_; ++i) {
129 for (int j = 0; j < columns_; ++j) {
130 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
131 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
137 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
144 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
152 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
155 #warning This while method should look like this: (Lgb)
160 // If this and lt is not of the same size we have a serious bug
161 // So then it is ok to throw an exception, or for now
163 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
165 cell_info = lt.cell_info;
166 row_info = lt.row_info;
167 column_info = lt.column_info;
168 SetLongTabular(lt.is_long_tabular);
177 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
179 LyXTabular * result = new LyXTabular(inset, *this, same_id);
181 // don't know if this is good but I need to Clone also
182 // the text-insets here, this is for the Undo-facility!
183 for (int i = 0; i < rows_; ++i) {
184 for (int j = 0; j < columns_; ++j) {
185 result->cell_info[i][j].inset = cell_info[i][j].inset;
186 result->cell_info[i][j].inset.setOwner(inset);
194 /* activates all lines and sets all widths to 0 */
195 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
198 columns_ = columns_arg;
199 row_info = row_vector(rows_, rowstruct());
200 column_info = column_vector(columns_, columnstruct());
201 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
209 for (int i = 0; i < rows_; ++i) {
210 for (int j = 0; j < columns_; ++j) {
211 cell_info[i][j].inset.setOwner(owner_);
212 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
213 cell_info[i][j].cellno = cellno++;
215 cell_info[i].back().right_line = true;
217 row_info.back().bottom_line = true;
218 row_info.front().bottom_line = true;
220 for (int i = 0; i < columns_; ++i) {
221 calculate_width_of_column(i);
223 column_info.back().right_line = true;
225 calculate_width_of_tabular();
227 rowofcell = vector<int>();
228 columnofcell = vector<int>();
229 set_row_column_number_info();
230 is_long_tabular = false;
235 void LyXTabular::AppendRow(int cell)
239 int row = row_of_cell(cell);
241 row_vector::iterator rit = row_info.begin() + row;
242 row_info.insert(rit, rowstruct());
243 // now set the values of the row before
244 row_info[row] = row_info[row+1];
247 cell_vvector::iterator cit = cell_info.begin() + row;
248 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
250 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
253 for (int i = 0; i <= row; ++i) {
254 for (int j = 0; j < columns_; ++j) {
255 c_info[i][j] = cell_info[i][j];
258 for (int i = row + 1; i < rows_; ++i) {
259 for (int j = 0; j < columns_; ++j) {
260 c_info[i][j] = cell_info[i-1][j];
265 for (int j = 0; j < columns_; ++j) {
266 cell_info[row][j].inset.clear();
273 void LyXTabular::DeleteRow(int row)
275 if (rows_ == 1) return; // Not allowed to delete last row
277 row_info.erase(row_info.begin() + row); //&row_info[row]);
278 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
284 void LyXTabular::AppendColumn(int cell)
288 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
290 int const column = column_of_cell(cell);
291 column_vector::iterator cit = column_info.begin() + column + 1;
292 column_info.insert(cit, columnstruct());
293 // set the column values of the column before
294 column_info[column+1] = column_info[column];
296 for (int i = 0; i < rows_; ++i) {
297 for (int j = 0; j <= column; ++j) {
298 c_info[i][j] = cell_info[i][j];
300 for (int j = column + 1; j < columns_; ++j) {
301 c_info[i][j] = cell_info[i][j - 1];
303 // care about multicolumns
304 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
306 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
308 if ((column + 2) >= columns_ ||
309 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
311 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
316 for (int i = 0; i < rows_; ++i) {
317 //cell_info[i][column].inset.clear();
318 cell_info[i][column + 1].inset.clear();
324 void LyXTabular::DeleteColumn(int column)
326 // Similar to DeleteRow
327 //if (!(columns_ - 1))
329 if (columns_ == 1) return; // Not allowed to delete last column
331 column_info.erase(column_info.begin() + column);
332 for (int i = 0; i < rows_; ++i) {
333 cell_info[i].erase(cell_info[i].begin() + column);
340 void LyXTabular::reinit()
346 void LyXTabular::Reinit(bool reset_widths)
349 for (int i = 0; i < rows_; ++i) {
350 for (int j = 0; j < columns_; ++j) {
351 cell_info[i][j].width_of_cell = 0;
352 cell_info[i][j].inset.setOwner(owner_);
357 for (int i = 0; i < columns_; ++i) {
358 calculate_width_of_column(i);
360 calculate_width_of_tabular();
362 set_row_column_number_info();
366 void LyXTabular::set_row_column_number_info(bool oldformat)
369 for (int row = 0; row < rows_; ++row) {
370 for (int column = 0; column<columns_; ++column) {
371 if (cell_info[row][column].multicolumn
372 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
374 cell_info[row][column].cellno = numberofcells;
377 ++numberofcells; // because this is one more than as we start from 0
379 rowofcell.resize(numberofcells);
380 columnofcell.resize(numberofcells);
382 for (int row = 0, column = 0, c = 0;
383 c < numberofcells && row < rows_ && column < columns_;) {
385 columnofcell[c] = column;
389 } while (column < columns_ &&
390 cell_info[row][column].multicolumn
391 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
392 if (column == columns_) {
398 for (int row = 0; row < rows_; ++row) {
399 for (int column = 0; column < columns_; ++column) {
400 if (IsPartOfMultiColumn(row,column))
402 // now set the right line of multicolumns right for oldformat read
404 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
406 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
407 cell_info[row][column].right_line =
408 cell_info[row][column+cn-1].right_line;
410 cell_info[row][column].inset.setAutoBreakRows(
411 !GetPWidth(GetCellNumber(row, column)).zero());
417 int LyXTabular::GetNumberOfCells() const
419 return numberofcells;
423 int LyXTabular::NumberOfCellsInRow(int cell) const
425 int const row = row_of_cell(cell);
427 for (int i = 0; i < columns_; ++i) {
428 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
435 /* returns 1 if there is a topline, returns 0 if not */
436 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
438 int const row = row_of_cell(cell);
440 if (!onlycolumn && IsMultiColumn(cell))
441 return cellinfo_of_cell(cell)->top_line;
442 return row_info[row].top_line;
446 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
448 // no bottom line underneath non-existent cells if you please
449 // Isn't that a programming error? Is so this should
450 // be an Assert instead. (Lgb)
451 if (cell >= numberofcells)
454 if (!onlycolumn && IsMultiColumn(cell))
455 return cellinfo_of_cell(cell)->bottom_line;
456 return row_info[row_of_cell(cell)].bottom_line;
460 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
462 if (!onlycolumn && IsMultiColumn(cell)) {
463 #ifdef SPECIAL_COLUM_HANDLING
464 if (cellinfo_of_cell(cell)->align_special.empty())
465 return cellinfo_of_cell(cell)->left_line;
466 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
468 return cellinfo_of_cell(cell)->left_line;
471 #ifdef SPECIAL_COLUM_HANDLING
472 if (column_info[column_of_cell(cell)].align_special.empty())
473 return column_info[column_of_cell(cell)].left_line;
474 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
476 return column_info[column_of_cell(cell)].left_line;
481 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
483 if (!onlycolumn && IsMultiColumn(cell)) {
484 #ifdef SPECIAL_COLUM_HANDLING
485 if (cellinfo_of_cell(cell)->align_special.empty())
486 return cellinfo_of_cell(cell)->right_line;
487 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
489 return cellinfo_of_cell(cell)->right_line;
492 #ifdef SPECIAL_COLUM_HANDLING
493 if (column_info[column_of_cell(cell)].align_special.empty())
494 return column_info[right_column_of_cell(cell)].right_line;
495 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
497 return column_info[right_column_of_cell(cell)].right_line;
502 bool LyXTabular::TopAlreadyDrawed(int cell) const
504 int row = row_of_cell(cell);
505 if ((row > 0) && !GetAdditionalHeight(row)) {
506 int column = column_of_cell(cell);
509 && cell_info[row][column].multicolumn
510 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
512 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
513 return row_info[row].bottom_line;
515 return cell_info[row][column].bottom_line;
521 bool LyXTabular::LeftAlreadyDrawed(int cell) const
523 int column = column_of_cell(cell);
525 int row = row_of_cell(cell);
527 (cell_info[row][column].multicolumn ==
528 LyXTabular::CELL_PART_OF_MULTICOLUMN));
529 if (GetAdditionalWidth(cell_info[row][column].cellno))
531 #ifdef SPECIAL_COLUM_HANDLING
532 return column_info[column].right_line;
534 return RightLine(cell_info[row][column].cellno, true);
541 bool LyXTabular::IsLastRow(int cell) const
543 return (row_of_cell(cell) == rows_ - 1);
547 int LyXTabular::GetAdditionalHeight(int row) const
549 if (!row || row >= rows_)
555 for (int column = 0; column < columns_ && bottom; ++column) {
556 switch (cell_info[row - 1][column].multicolumn) {
557 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
558 bottom = cell_info[row - 1][column].bottom_line;
560 case LyXTabular::CELL_NORMAL:
561 bottom = row_info[row - 1].bottom_line;
564 for (int column = 0; column < columns_ && top; ++column) {
565 switch (cell_info[row][column].multicolumn) {
566 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
567 top = cell_info[row][column].top_line;
569 case LyXTabular::CELL_NORMAL:
570 top = row_info[row].top_line;
574 return WIDTH_OF_LINE;
579 int LyXTabular::GetAdditionalWidth(int cell) const
581 // internally already set in SetWidthOfCell
582 // used to get it back in text.C
583 int const col = right_column_of_cell(cell);
584 int const row = row_of_cell(cell);
585 if (col < columns_ - 1 && RightLine(cell, true) &&
586 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
588 return WIDTH_OF_LINE;
595 // returns the maximum over all rows
596 int LyXTabular::GetWidthOfColumn(int cell) const
598 int const column1 = column_of_cell(cell);
599 int const column2 = right_column_of_cell(cell);
601 for (int i = column1; i <= column2; ++i) {
602 result += column_info[i].width_of_column;
608 int LyXTabular::GetWidthOfTabular() const
610 return width_of_tabular;
614 /* returns 1 if a complete update is necessary, otherwise 0 */
615 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
617 if (!IsMultiColumn(cell))
620 int const row = row_of_cell(cell);
621 int const column1 = column_of_cell(cell);
622 int const column2 = right_column_of_cell(cell);
623 int const old_val = cell_info[row][column2].width_of_cell;
625 // first set columns to 0 so we can calculate the right width
626 for (int i = column1; i <= column2; ++i) {
627 cell_info[row][i].width_of_cell = 0;
629 // set the width to MAX_WIDTH until width > 0
630 int width = (new_width + 2 * WIDTH_OF_LINE);
632 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
633 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
634 width -= column_info[i].width_of_column;
637 cell_info[row][i].width_of_cell = width;
639 if (old_val != cell_info[row][column2].width_of_cell) {
640 // in this case we have to recalculate all multicolumn cells which
641 // have this column as one of theirs but not as last one
642 recalculateMulticolumnsOfColumn(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 if (flag) // do this only if there is a width
742 SetAlignment(cell, LYX_ALIGN_LEFT);
743 for (int i = 0; i < rows_; ++i) {
744 int c = GetCellNumber(i, j);
745 flag = !GetPWidth(c).zero(); // because of multicolumns!
746 GetCellInset(c)->setAutoBreakRows(flag);
752 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
754 bool const flag = !width.zero();
756 cellinfo_of_cell(cell)->p_width = width;
757 if (IsMultiColumn(cell)) {
758 GetCellInset(cell)->setAutoBreakRows(flag);
765 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
766 LyXTabular::Feature what)
768 if (what == SET_SPECIAL_MULTI)
769 cellinfo_of_cell(cell)->align_special = special;
771 column_info[column_of_cell(cell)].align_special = special;
776 bool LyXTabular::SetAllLines(int cell, bool line)
778 SetTopLine(cell, line);
779 SetBottomLine(cell, line);
780 SetRightLine(cell, line);
781 SetLeftLine(cell, line);
786 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
788 int const row = row_of_cell(cell);
790 if (onlycolumn || !IsMultiColumn(cell))
791 row_info[row].top_line = line;
793 cellinfo_of_cell(cell)->top_line = line;
798 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
800 if (onlycolumn || !IsMultiColumn(cell))
801 row_info[row_of_cell(cell)].bottom_line = line;
803 cellinfo_of_cell(cell)->bottom_line = line;
808 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
810 if (onlycolumn || !IsMultiColumn(cell))
811 column_info[column_of_cell(cell)].left_line = line;
813 cellinfo_of_cell(cell)->left_line = line;
818 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
820 if (onlycolumn || !IsMultiColumn(cell))
821 column_info[right_column_of_cell(cell)].right_line = line;
823 cellinfo_of_cell(cell)->right_line = line;
828 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
830 if (!onlycolumn && IsMultiColumn(cell))
831 return cellinfo_of_cell(cell)->alignment;
833 return column_info[column_of_cell(cell)].alignment;
837 LyXTabular::VAlignment
838 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
840 if (!onlycolumn && IsMultiColumn(cell))
841 return cellinfo_of_cell(cell)->valignment;
843 return column_info[column_of_cell(cell)].valignment;
847 LyXLength const LyXTabular::GetPWidth(int cell) const
849 if (IsMultiColumn(cell))
850 return cellinfo_of_cell(cell)->p_width;
851 return column_info[column_of_cell(cell)].p_width;
855 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
857 return column_info[column_of_cell(cell)].p_width;
861 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
863 if (IsMultiColumn(cell))
864 return cellinfo_of_cell(cell)->p_width;
869 string const LyXTabular::GetAlignSpecial(int cell, int what) const
871 if (what == SET_SPECIAL_MULTI)
872 return cellinfo_of_cell(cell)->align_special;
873 return column_info[column_of_cell(cell)].align_special;
877 int LyXTabular::GetWidthOfCell(int cell) const
879 int const row = row_of_cell(cell);
880 int const column1 = column_of_cell(cell);
881 int const column2 = right_column_of_cell(cell);
883 for (int i = column1; i <= column2; ++i) {
884 result += cell_info[row][i].width_of_cell;
890 int LyXTabular::GetBeginningOfTextInCell(int cell) const
894 switch (GetAlignment(cell)) {
895 case LYX_ALIGN_CENTER:
896 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
898 case LYX_ALIGN_RIGHT:
899 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
900 // + GetAdditionalWidth(cell);
902 default: /* LYX_ALIGN_LEFT: nothing :-) */
912 bool LyXTabular::IsFirstCellInRow(int cell) const
914 return column_of_cell(cell) == 0;
918 int LyXTabular::GetFirstCellInRow(int row) const
922 return cell_info[row][0].cellno;
925 bool LyXTabular::IsLastCellInRow(int cell) const
927 return (right_column_of_cell(cell) == (columns_ - 1));
931 int LyXTabular::GetLastCellInRow(int row) const
935 return cell_info[row][columns_-1].cellno;
939 bool LyXTabular::calculate_width_of_column(int column)
941 int const old_column_width = column_info[column].width_of_column;
944 for (int i = 0; i < rows_; ++i) {
945 maximum = max(cell_info[i][column].width_of_cell, maximum);
947 column_info[column].width_of_column = maximum;
948 return (column_info[column].width_of_column != old_column_width);
953 // calculate the with of the column without regarding REAL MultiColumn
954 // cells. This means MultiColumn-cells spanning more than 1 column.
956 bool LyXTabular::calculate_width_of_column_NMC(int column)
958 int const old_column_width = column_info[column].width_of_column;
960 for (int i = 0; i < rows_; ++i) {
961 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
962 (cell_info[i][column].width_of_cell > max)) {
963 max = cell_info[i][column].width_of_cell;
966 column_info[column].width_of_column = max;
967 return (column_info[column].width_of_column != old_column_width);
971 void LyXTabular::calculate_width_of_tabular()
973 width_of_tabular = 0;
974 for (int i = 0; i < columns_; ++i) {
975 width_of_tabular += column_info[i].width_of_column;
980 int LyXTabular::row_of_cell(int cell) const
982 if (cell >= numberofcells)
986 return rowofcell[cell];
990 int LyXTabular::column_of_cell(int cell) const
992 if (cell >= numberofcells)
996 return columnofcell[cell];
1000 int LyXTabular::right_column_of_cell(int cell) const
1002 int const row = row_of_cell(cell);
1003 int column = column_of_cell(cell);
1004 while (column < (columns_ - 1) &&
1005 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1011 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1015 << write_attribute("version", 3)
1016 << write_attribute("rows", rows_)
1017 << write_attribute("columns", columns_)
1019 // global longtable options
1021 << write_attribute("rotate", rotate)
1022 << write_attribute("islongtable", is_long_tabular)
1023 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1024 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1025 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1026 << write_attribute("headTopDL", endhead.topDL)
1027 << write_attribute("headBottomDL", endhead.bottomDL)
1028 << write_attribute("footTopDL", endfoot.topDL)
1029 << write_attribute("footBottomDL", endfoot.bottomDL)
1030 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1031 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1032 << write_attribute("lastFootEmpty", endlastfoot.empty)
1034 for (int j = 0; j < columns_; ++j) {
1036 << write_attribute("alignment", column_info[j].alignment)
1037 << write_attribute("valignment", column_info[j].valignment)
1038 << write_attribute("leftline", column_info[j].left_line)
1039 << write_attribute("rightline", column_info[j].right_line)
1040 << write_attribute("width", column_info[j].p_width.asString())
1041 << write_attribute("special", column_info[j].align_special)
1044 for (int i = 0; i < rows_; ++i) {
1046 << write_attribute("topline", row_info[i].top_line)
1047 << write_attribute("bottomline", row_info[i].bottom_line)
1048 << write_attribute("endhead", row_info[i].endhead)
1049 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1050 << write_attribute("endfoot", row_info[i].endfoot)
1051 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1052 << write_attribute("newpage", row_info[i].newpage)
1054 for (int j = 0; j < columns_; ++j) {
1056 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1057 << write_attribute("alignment", cell_info[i][j].alignment)
1058 << write_attribute("valignment", cell_info[i][j].valignment)
1059 << write_attribute("topline", cell_info[i][j].top_line)
1060 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1061 << write_attribute("leftline", cell_info[i][j].left_line)
1062 << write_attribute("rightline", cell_info[i][j].right_line)
1063 << write_attribute("rotate", cell_info[i][j].rotate)
1064 << write_attribute("usebox", cell_info[i][j].usebox)
1065 << write_attribute("width", cell_info[i][j].p_width)
1066 << write_attribute("special", cell_info[i][j].align_special)
1068 os << "\\begin_inset ";
1069 cell_info[i][j].inset.write(buf, os);
1070 os << "\n\\end_inset \n"
1075 os << "</lyxtabular>\n";
1079 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1082 istream & is = lex.getStream();
1084 l_getline(is, line);
1085 if (!prefixIs(line, "<lyxtabular ")
1086 && !prefixIs(line, "<LyXTabular ")) {
1087 OldFormatRead(lex, line);
1092 if (!getTokenValue(line, "version", version))
1095 ReadOld(buf, is, lex, line);
1096 else if (version >= 2)
1097 ReadNew(buf, is, lex, line, version);
1100 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1104 row_info[--hr].endhead = true;
1106 // set firstheader info
1107 if (fhr && (fhr < rows_)) {
1108 if (row_info[fhr].endhead) {
1110 row_info[--fhr].endfirsthead = true;
1111 row_info[fhr].endhead = false;
1113 } else if (row_info[fhr-1].endhead) {
1114 endfirsthead.empty = true;
1116 while((fhr > 0) && !row_info[--fhr].endhead) {
1117 row_info[fhr].endfirsthead = true;
1122 if (fr && (fr < rows_)) {
1123 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1124 while((fr > 0) && !row_info[--fr].endhead) {
1125 row_info[fr].endfoot = true;
1126 row_info[fr].endhead = false;
1128 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1129 while((fr > 0) && !row_info[--fr].endfirsthead) {
1130 row_info[fr].endfoot = true;
1131 row_info[fr].endfirsthead = false;
1133 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead) {
1134 while((fr > 0) && !row_info[--fr].endhead &&
1135 !row_info[fr].endfirsthead)
1137 row_info[fr].endfoot = true;
1141 // set lastfooter info
1142 if (lfr && (lfr < rows_)) {
1143 if (row_info[lfr].endhead && row_info[lfr-1].endhead) {
1144 while((lfr > 0) && !row_info[--lfr].endhead) {
1145 row_info[lfr].endlastfoot = true;
1146 row_info[lfr].endhead = false;
1148 } else if (row_info[lfr].endfirsthead &&
1149 row_info[lfr-1].endfirsthead)
1151 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1152 row_info[lfr].endlastfoot = true;
1153 row_info[lfr].endfirsthead = false;
1155 } else if (row_info[lfr].endfoot && row_info[lfr-1].endfoot) {
1156 while((lfr > 0) && !row_info[--lfr].endfoot) {
1157 row_info[lfr].endlastfoot = true;
1158 row_info[lfr].endfoot = false;
1160 } else if (!row_info[fr-1].endhead && !row_info[fr-1].endfirsthead &&
1161 !row_info[fr-1].endfoot)
1164 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1165 !row_info[lfr].endfoot)
1167 row_info[lfr].endlastfoot = true;
1169 } else if (haveLTFoot()) {
1170 endlastfoot.empty = true;
1175 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1176 LyXLex & lex, string const & l, int const version)
1180 if (!getTokenValue(line, "rows", rows_arg))
1183 if (!getTokenValue(line, "columns", columns_arg))
1185 Init(rows_arg, columns_arg);
1186 l_getline(is, line);
1187 if (!prefixIs(line, "<features")) {
1188 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1189 line << ")" << endl;
1192 getTokenValue(line, "rotate", rotate);
1193 getTokenValue(line, "islongtable", is_long_tabular);
1194 // compatibility read for old longtable options. Now we can make any
1195 // row part of the header/footer type we want before it was strict
1196 // sequential from the first row down (as LaTeX does it!). So now when
1197 // we find a header/footer line we have to go up the rows and set it
1198 // on all preceding rows till the first or one with already a h/f option
1199 // set. If we find a firstheader on the same line as a header or a
1200 // lastfooter on the same line as a footer then this should be set empty.
1208 getTokenValue(line, "endhead", hrow);
1209 getTokenValue(line, "endfirsthead", fhrow);
1210 getTokenValue(line, "endfoot", frow);
1211 getTokenValue(line, "endlastfoot", lfrow);
1212 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1214 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1215 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1216 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1217 getTokenValue(line, "headTopDL", endhead.topDL);
1218 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1219 getTokenValue(line, "footTopDL", endfoot.topDL);
1220 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1221 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1222 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1223 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1225 for (int j = 0; j < columns_; ++j) {
1227 if (!prefixIs(line,"<column")) {
1228 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1229 line << ")" << endl;
1232 getTokenValue(line, "alignment", column_info[j].alignment);
1233 getTokenValue(line, "valignment", column_info[j].valignment);
1234 getTokenValue(line, "leftline", column_info[j].left_line);
1235 getTokenValue(line, "rightline", column_info[j].right_line);
1236 getTokenValue(line, "width", column_info[j].p_width);
1237 getTokenValue(line, "special", column_info[j].align_special);
1240 for (int i = 0; i < rows_; ++i) {
1241 l_getline(is, line);
1242 if (!prefixIs(line, "<row")) {
1243 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1244 line << ")" << endl;
1247 getTokenValue(line, "topline", row_info[i].top_line);
1248 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1249 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1250 getTokenValue(line, "endhead", row_info[i].endhead);
1251 getTokenValue(line, "endfoot", row_info[i].endfoot);
1252 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1253 getTokenValue(line, "newpage", row_info[i].newpage);
1254 for (int j = 0; j < columns_; ++j) {
1255 l_getline(is, line);
1256 if (!prefixIs(line, "<cell")) {
1257 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1258 line << ")" << endl;
1261 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1262 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1263 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1264 getTokenValue(line, "topline", cell_info[i][j].top_line);
1265 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1266 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1267 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1268 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1269 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1270 getTokenValue(line, "width", cell_info[i][j].p_width);
1271 getTokenValue(line, "special", cell_info[i][j].align_special);
1272 l_getline(is, line);
1273 if (prefixIs(line, "\\begin_inset")) {
1274 cell_info[i][j].inset.read(buf, lex);
1275 l_getline(is, line);
1277 if (!prefixIs(line, "</cell>")) {
1278 lyxerr << "Wrong tabular format (expected </cell> got" <<
1279 line << ")" << endl;
1283 l_getline(is, line);
1284 if (!prefixIs(line, "</row>")) {
1285 lyxerr << "Wrong tabular format (expected </row> got" <<
1286 line << ")" << endl;
1290 while (!prefixIs(line, "</lyxtabular>")) {
1291 l_getline(is, line);
1293 set_row_column_number_info();
1297 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1303 int columns_arg = 0;
1304 int is_long_tabular_arg = false;
1305 int rotate_arg = false;
1314 istream & is = lex.getStream();
1317 version = lyx::atoi(s.substr(8, string::npos));
1321 vector<int> cont_row_info;
1324 lyxerr << "Tabular format < 5 is not supported anymore\n"
1325 "Get an older version of LyX (< 1.1.x) for conversion!"
1327 Alert::alert(_("Warning:"),
1328 _("Tabular format < 5 is not supported anymore\n"),
1329 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1331 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1332 >> rotate_arg >> a >> b >> c >> d;
1334 is >> rows_arg >> columns_arg;
1335 Init(rows_arg, columns_arg);
1336 cont_row_info = vector<int>(rows_arg);
1337 SetLongTabular(is_long_tabular_arg);
1338 SetRotateTabular(rotate_arg);
1340 for (i = 0; i < rows_; ++i) {
1342 cont_row_info[i] = false;
1344 for (i = 0; i < columns_; ++i) {
1347 for (i = 0; i < rows_; ++i) {
1348 for (j = 0; j < columns_; ++j) {
1353 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1354 >> rotate_arg >> a >> b >> c >> d;
1355 Init(rows_arg, columns_arg);
1356 cont_row_info = vector<int>(rows_arg);
1357 SetLongTabular(is_long_tabular_arg);
1358 SetRotateTabular(rotate_arg);
1359 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1360 for (i = 0; i < rows_; ++i) {
1361 a = b = c = d = e = f = g = 0;
1362 is >> a >> b >> c >> d;
1363 row_info[i].top_line = a;
1364 row_info[i].bottom_line = b;
1365 cont_row_info[i] = c;
1366 row_info[i].newpage = d;
1368 for (i = 0; i < columns_; ++i) {
1373 char ch; // skip '"'
1376 // ignore is buggy but we will use it later (Lgb)
1377 is.ignore(); // skip '"'
1379 getline(is, s1, '"');
1381 is >> ch; // skip '"'
1383 // ignore is buggy but we will use it later (Lgb)
1384 is.ignore(); // skip '"'
1386 getline(is, s2, '"');
1387 column_info[i].alignment = static_cast<LyXAlignment>(a);
1388 column_info[i].left_line = b;
1389 column_info[i].right_line = c;
1390 column_info[i].p_width = LyXLength(s1);
1391 column_info[i].align_special = s2;
1393 for (i = 0; i < rows_; ++i) {
1394 for (j = 0; j < columns_; ++j) {
1397 is >> a >> b >> c >> d >> e >> f >> g;
1400 is >> ch; // skip '"'
1402 // ignore is buggy but we will use it later (Lgb)
1403 is.ignore(); // skip '"'
1405 getline(is, s1, '"');
1407 is >> ch; // skip '"'
1409 // ignore is buggy but we will use it later (Lgb)
1410 is.ignore(); // skip '"'
1412 getline(is, s2, '"');
1413 cell_info[i][j].multicolumn = static_cast<char>(a);
1414 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1415 cell_info[i][j].top_line = static_cast<char>(c);
1416 cell_info[i][j].bottom_line = static_cast<char>(d);
1417 cell_info[i][j].left_line = column_info[j].left_line;
1418 cell_info[i][j].right_line = column_info[j].right_line;
1419 cell_info[i][j].rotate = static_cast<bool>(f);
1420 cell_info[i][j].usebox = static_cast<BoxType>(g);
1421 cell_info[i][j].align_special = s1;
1422 cell_info[i][j].p_width = LyXLength(s2);
1426 set_row_column_number_info(true);
1428 Paragraph * par = new Paragraph;
1429 Paragraph * return_par = 0;
1433 Paragraph::depth_type depth = 0;
1434 LyXFont font(LyXFont::ALL_INHERIT);
1435 font.setLanguage(owner_->bufferOwner()->getLanguage());
1437 while (lex.isOK()) {
1439 string const token = lex.getString();
1442 if (token == "\\layout"
1443 || token == "\\end_float"
1444 || token == "\\end_deeper") {
1445 lex.pushToken(token);
1446 #ifndef NO_COMPABILITY
1447 // Here we need to insert the inset_ert_contents into the last
1448 // cell of the tabular.
1449 owner_->bufferOwner()->insertErtContents(par, pos);
1453 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1458 lex.pushToken(token);
1462 lex.printError("New Paragraph allocated! This should not happen!");
1463 lex.pushToken(token);
1469 // now we have the par we should fill the insets with this!
1471 InsetText * inset = GetCellInset(cell);
1474 for (int i = 0; i < par->size(); ++i) {
1475 if (par->isNewline(i)) {
1477 if (cell > numberofcells) {
1478 lyxerr << "Some error in reading old table format occured!" <<
1479 endl << "Terminating when reading cell[" << cell << "]!" <<
1484 row = row_of_cell(cell);
1485 if (cont_row_info[row]) {
1487 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1488 while (!IsFirstCellInRow(--cell));
1490 inset = GetCellInset(cell);
1493 inset = GetCellInset(cell);
1494 row = row_of_cell(cell);
1495 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1497 // insert a space instead
1499 par->insertChar(i, ' ');
1502 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1503 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1510 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1512 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1513 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1517 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1519 int const row = row_of_cell(cell);
1520 int const column = column_of_cell(cell);
1521 return &cell_info[row][column];
1525 void LyXTabular::SetMultiColumn(int cell, int number)
1527 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1528 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1529 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1530 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1531 for (number--; number > 0; --number) {
1532 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1534 set_row_column_number_info();
1538 int LyXTabular::cells_in_multicolumn(int cell) const
1540 int const row = row_of_cell(cell);
1541 int column = column_of_cell(cell);
1544 while ((column < columns_) &&
1545 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1554 int LyXTabular::UnsetMultiColumn(int cell)
1556 int const row = row_of_cell(cell);
1557 int column = column_of_cell(cell);
1561 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1562 cell_info[row][column].multicolumn = CELL_NORMAL;
1564 while ((column < columns_) &&
1565 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1567 cell_info[row][column].multicolumn = CELL_NORMAL;
1572 set_row_column_number_info();
1577 void LyXTabular::SetLongTabular(bool what)
1579 is_long_tabular = what;
1583 bool LyXTabular::IsLongTabular() const
1585 return is_long_tabular;
1589 void LyXTabular::SetRotateTabular(bool flag)
1595 bool LyXTabular::GetRotateTabular() const
1601 void LyXTabular::SetRotateCell(int cell, bool flag)
1603 cellinfo_of_cell(cell)->rotate = flag;
1607 bool LyXTabular::GetRotateCell(int cell) const
1609 return cellinfo_of_cell(cell)->rotate;
1613 bool LyXTabular::NeedRotating() const
1617 for (int i = 0; i < rows_; ++i) {
1618 for (int j = 0; j < columns_; ++j) {
1619 if (cell_info[i][j].rotate)
1627 bool LyXTabular::IsLastCell(int cell) const
1629 if ((cell + 1) < numberofcells)
1635 int LyXTabular::GetCellAbove(int cell) const
1637 if (row_of_cell(cell) > 0)
1638 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1643 int LyXTabular::GetCellBelow(int cell) const
1645 if (row_of_cell(cell) + 1 < rows_)
1646 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1651 int LyXTabular::GetLastCellAbove(int cell) const
1653 if (row_of_cell(cell) <= 0)
1655 if (!IsMultiColumn(cell))
1656 return GetCellAbove(cell);
1657 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1661 int LyXTabular::GetLastCellBelow(int cell) const
1663 if (row_of_cell(cell) + 1 >= rows_)
1665 if (!IsMultiColumn(cell))
1666 return GetCellBelow(cell);
1667 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1671 int LyXTabular::GetCellNumber(int row, int column) const
1674 if (column >= columns_)
1675 column = columns_ - 1;
1676 else if (column < 0)
1683 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1685 return cell_info[row][column].cellno;
1689 void LyXTabular::SetUsebox(int cell, BoxType type)
1691 cellinfo_of_cell(cell)->usebox = type;
1695 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1697 if (column_info[column_of_cell(cell)].p_width.zero() &&
1698 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1700 if (cellinfo_of_cell(cell)->usebox > 1)
1701 return cellinfo_of_cell(cell)->usebox;
1702 return UseParbox(cell);
1707 // This are functions used for the longtable support
1709 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1714 row_info[row].endfirsthead = flag;
1718 row_info[row].endhead = flag;
1723 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1726 hd.set = haveLTHead();
1727 return row_info[row].endhead;
1731 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1734 hd.set = haveLTFirstHead();
1735 return row_info[row].endfirsthead;
1739 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1744 row_info[row].endlastfoot = flag;
1748 row_info[row].endfoot = flag;
1753 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1756 fd.set = haveLTFoot();
1757 return row_info[row].endfoot;
1761 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1764 fd.set = haveLTLastFoot();
1765 return row_info[row].endlastfoot;
1769 void LyXTabular::SetLTNewPage(int row, bool what)
1771 row_info[row].newpage = what;
1775 bool LyXTabular::GetLTNewPage(int row) const
1777 return row_info[row].newpage;
1781 bool LyXTabular::haveLTHead() const
1783 for(int i=0; i < rows_; ++i) {
1784 if (row_info[i].endhead)
1791 bool LyXTabular::haveLTFirstHead() const
1793 if (endfirsthead.empty)
1795 for(int i=0; i < rows_; ++i) {
1796 if (row_info[i].endfirsthead)
1803 bool LyXTabular::haveLTFoot() const
1805 for(int i=0; i < rows_; ++i) {
1806 if (row_info[i].endfoot)
1813 bool LyXTabular::haveLTLastFoot() const
1815 if (endlastfoot.empty)
1817 for(int i=0; i < rows_; ++i) {
1818 if (row_info[i].endlastfoot)
1825 // end longtable support functions
1827 bool LyXTabular::SetAscentOfRow(int row, int height)
1829 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1831 row_info[row].ascent_of_row = height;
1836 bool LyXTabular::SetDescentOfRow(int row, int height)
1838 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1840 row_info[row].descent_of_row = height;
1845 int LyXTabular::GetAscentOfRow(int row) const
1849 return row_info[row].ascent_of_row;
1853 int LyXTabular::GetDescentOfRow(int row) const
1857 return row_info[row].descent_of_row;
1861 int LyXTabular::GetHeightOfTabular() const
1865 for (int row = 0; row < rows_; ++row)
1866 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1867 GetAdditionalHeight(row);
1872 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1874 if ((row >= rows_) || (column >= columns_))
1876 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1880 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1882 if ((row < 0) || (row >= rows_))
1885 int const fcell = GetFirstCellInRow(row);
1886 int const n = NumberOfCellsInRow(fcell) + fcell;
1889 for (int i = fcell; i < n; ++i) {
1893 if (tmp == (n - fcell)) {
1896 for (int i = fcell; i < n; ++i) {
1899 << column_of_cell(i) + 1
1901 << right_column_of_cell(i) + 1
1913 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1915 if ((row < 0) || (row >= rows_))
1918 int const fcell = GetFirstCellInRow(row);
1919 int const n = NumberOfCellsInRow(fcell) + fcell;
1922 for (int i = fcell; i < n; ++i) {
1926 if (tmp == (n - fcell)) {
1929 for (int i = fcell; i < n; ++i) {
1930 if (BottomLine(i)) {
1932 << column_of_cell(i) + 1
1934 << right_column_of_cell(i) + 1
1946 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1950 if (GetRotateCell(cell)) {
1951 os << "\\begin{sideways}\n";
1954 if (IsMultiColumn(cell)) {
1955 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1956 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1957 os << cellinfo_of_cell(cell)->align_special << "}{";
1959 if (LeftLine(cell) &&
1960 (IsFirstCellInRow(cell) ||
1961 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1962 !RightLine(cell-1, true))))
1966 if (!GetPWidth(cell).zero()) {
1967 switch (GetVAlignment(cell)) {
1968 case LYX_VALIGN_TOP:
1971 case LYX_VALIGN_CENTER:
1974 case LYX_VALIGN_BOTTOM:
1978 os << "{" << GetPWidth(cell).asLatexString() << '}';
1980 switch (GetAlignment(cell)) {
1981 case LYX_ALIGN_LEFT:
1984 case LYX_ALIGN_RIGHT:
1992 if (RightLine(cell))
1994 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2000 if (GetUsebox(cell) == BOX_PARBOX) {
2002 switch (GetVAlignment(cell)) {
2003 case LYX_VALIGN_TOP:
2006 case LYX_VALIGN_CENTER:
2009 case LYX_VALIGN_BOTTOM:
2013 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2014 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2015 os << "\\begin{minipage}[";
2016 switch (GetVAlignment(cell)) {
2017 case LYX_VALIGN_TOP:
2020 case LYX_VALIGN_CENTER:
2023 case LYX_VALIGN_BOTTOM:
2027 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2034 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2039 if (GetUsebox(cell) == BOX_PARBOX)
2041 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2042 os << "%\n\\end{minipage}";
2045 if (IsMultiColumn(cell)) {
2048 if (GetRotateCell(cell)) {
2049 os << "%\n\\end{sideways}";
2056 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2057 bool fragile, bool fp) const
2059 if (!is_long_tabular)
2063 // output header info
2065 if (endhead.topDL) {
2069 for (int i = 0; i < rows_; ++i) {
2070 if (row_info[i].endhead) {
2071 ret += TeXRow(os, i, buf, fragile, fp);
2074 if (endhead.bottomDL) {
2078 os << "\\endhead\n";
2080 if (endfirsthead.empty) {
2081 os << "\\endfirsthead\n";
2085 // output firstheader info
2086 if (haveLTFirstHead()) {
2087 if (endfirsthead.topDL) {
2091 for (int i = 0; i < rows_; ++i) {
2092 if (row_info[i].endfirsthead) {
2093 ret += TeXRow(os, i, buf, fragile, fp);
2096 if (endfirsthead.bottomDL) {
2100 os << "\\endfirsthead\n";
2103 // output footer info
2105 if (endfoot.topDL) {
2109 for (int i = 0; i < rows_; ++i) {
2110 if (row_info[i].endfoot) {
2111 ret += TeXRow(os, i, buf, fragile, fp);
2114 if (endfoot.bottomDL) {
2118 os << "\\endfoot\n";
2120 if (endlastfoot.empty) {
2121 os << "\\endlastfoot\n";
2125 // output lastfooter info
2126 if (haveLTLastFoot()) {
2127 if (endlastfoot.topDL) {
2131 for (int i = 0; i < rows_; ++i) {
2132 if (row_info[i].endlastfoot) {
2133 ret += TeXRow(os, i, buf, fragile, fp);
2136 if (endlastfoot.bottomDL) {
2140 os << "\\endlastfoot\n";
2147 bool LyXTabular::isValidRow(int const row) const
2149 if (!is_long_tabular)
2151 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2152 !row_info[row].endfoot && !row_info[row].endlastfoot);
2156 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2157 bool fragile, bool fp) const
2160 int cell = GetCellNumber(i, 0);
2162 ret += TeXTopHLine(os, i);
2163 for (int j = 0; j < columns_; ++j) {
2164 if (IsPartOfMultiColumn(i,j))
2166 ret += TeXCellPreamble(os, cell);
2167 InsetText * inset = GetCellInset(cell);
2169 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2170 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2174 ret += inset->latex(buf, os, fragile, fp);
2178 ret += TeXCellPostamble(os, cell);
2179 if (!IsLastCellInRow(cell)) { // not last cell in row
2187 ret += TeXBottomHLine(os, i);
2192 int LyXTabular::latex(Buffer const * buf,
2193 ostream & os, bool fragile, bool fp) const
2197 //+---------------------------------------------------------------------
2198 //+ first the opening preamble +
2199 //+---------------------------------------------------------------------
2202 os << "\\begin{sideways}\n";
2205 if (is_long_tabular)
2206 os << "\\begin{longtable}{";
2208 os << "\\begin{tabular}{";
2209 for (int i = 0; i < columns_; ++i) {
2210 if (!column_info[i].align_special.empty()) {
2211 os << column_info[i].align_special;
2213 if (column_info[i].left_line)
2215 if (!column_info[i].p_width.zero()) {
2216 switch (column_info[i].valignment) {
2217 case LYX_VALIGN_TOP:
2220 case LYX_VALIGN_CENTER:
2223 case LYX_VALIGN_BOTTOM:
2228 << column_info[i].p_width.asLatexString()
2231 switch (column_info[i].alignment) {
2232 case LYX_ALIGN_LEFT:
2235 case LYX_ALIGN_RIGHT:
2243 if (column_info[i].right_line)
2250 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2252 //+---------------------------------------------------------------------
2253 //+ the single row and columns (cells) +
2254 //+---------------------------------------------------------------------
2256 for (int i = 0; i < rows_; ++i) {
2257 if (isValidRow(i)) {
2258 ret += TeXRow(os, i, buf, fragile, fp);
2259 if (is_long_tabular && row_info[i].newpage) {
2260 os << "\\newpage\n";
2266 //+---------------------------------------------------------------------
2267 //+ the closing of the tabular +
2268 //+---------------------------------------------------------------------
2270 if (is_long_tabular)
2271 os << "\\end{longtable}";
2273 os << "\\end{tabular}";
2275 os << "\n\\end{sideways}";
2283 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2286 int cell = GetFirstCellInRow(row);
2289 for (int j = 0; j < columns_; ++j) {
2290 if (IsPartOfMultiColumn(row, j))
2293 os << "<entry align=\"";
2294 switch (GetAlignment(cell)) {
2295 case LYX_ALIGN_LEFT:
2298 case LYX_ALIGN_RIGHT:
2306 os << "\" valign=\"";
2307 switch (GetVAlignment(cell)) {
2308 case LYX_VALIGN_TOP:
2311 case LYX_VALIGN_BOTTOM:
2314 case LYX_VALIGN_CENTER:
2319 if (IsMultiColumn(cell)) {
2320 os << " namest=\"col" << j << "\" ";
2321 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2325 ret += GetCellInset(cell)->docbook(buf, os);
2334 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2338 //+---------------------------------------------------------------------
2339 //+ first the opening preamble +
2340 //+---------------------------------------------------------------------
2342 os << "<tgroup cols=\"" << columns_
2343 << "\" colsep=\"1\" rowsep=\"1\">\n";
2345 for (int i = 0; i < columns_; ++i) {
2346 os << "<colspec colname=\"col" << i << "\" align=\"";
2347 switch (column_info[i].alignment) {
2348 case LYX_ALIGN_LEFT:
2351 case LYX_ALIGN_RIGHT:
2362 //+---------------------------------------------------------------------
2363 //+ Long Tabular case +
2364 //+---------------------------------------------------------------------
2366 // output header info
2367 if (haveLTHead() || haveLTFirstHead()) {
2370 for (int i = 0; i < rows_; ++i) {
2371 if (row_info[i].endhead || row_info[i].endfirsthead) {
2372 ret += docbookRow(buf, os, i);
2378 // output footer info
2379 if (haveLTFoot() || haveLTLastFoot()) {
2382 for (int i = 0; i < rows_; ++i) {
2383 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2384 ret += docbookRow(buf, os, i);
2391 //+---------------------------------------------------------------------
2392 //+ the single row and columns (cells) +
2393 //+---------------------------------------------------------------------
2397 for (int i = 0; i < rows_; ++i) {
2398 if (isValidRow(i)) {
2399 ret += docbookRow(buf, os, i);
2404 //+---------------------------------------------------------------------
2405 //+ the closing of the tabular +
2406 //+---------------------------------------------------------------------
2415 // ASCII export function and helpers
2417 int LyXTabular::asciiTopHLine(ostream & os, int row,
2418 vector<unsigned int> const & clen) const
2420 int const fcell = GetFirstCellInRow(row);
2421 int const n = NumberOfCellsInRow(fcell) + fcell;
2424 for (int i = fcell; i < n; ++i) {
2434 for (int i = fcell; i < n; ++i) {
2445 int column = column_of_cell(i);
2446 int len = clen[column];
2447 while (IsPartOfMultiColumn(row, ++column))
2448 len += clen[column] + 4;
2449 os << string(len, ch);
2464 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2465 vector<unsigned int> const & clen) const
2467 int const fcell = GetFirstCellInRow(row);
2468 int const n = NumberOfCellsInRow(fcell) + fcell;
2471 for (int i = fcell; i < n; ++i) {
2472 if (BottomLine(i)) {
2481 for (int i = fcell; i < n; ++i) {
2482 if (BottomLine(i)) {
2492 int column = column_of_cell(i);
2493 int len = clen[column];
2494 while (IsPartOfMultiColumn(row, ++column))
2495 len += clen[column] + 4;
2496 os << string(len, ch);
2497 if (BottomLine(i)) {
2511 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2512 int cell, int row, int column,
2513 vector<unsigned int> const & clen,
2514 bool onlydata) const
2517 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2529 unsigned int len1 = sstr.str().length();
2530 unsigned int len2 = clen[column];
2531 while (IsPartOfMultiColumn(row, ++column))
2532 len2 += clen[column] + 4;
2535 switch (GetAlignment(cell)) {
2537 case LYX_ALIGN_LEFT:
2540 case LYX_ALIGN_RIGHT:
2544 case LYX_ALIGN_CENTER:
2550 for (unsigned int i = 0; i < len1; ++i)
2553 for (unsigned int i = 0; i < len2; ++i)
2555 if (RightLine(cell))
2564 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2565 bool onlydata, unsigned char delim) const
2569 //+---------------------------------------------------------------------
2570 //+ first calculate the width of the single columns +
2571 //+---------------------------------------------------------------------
2572 vector<unsigned int> clen(columns_);
2575 // first all non (real) multicolumn cells!
2576 for (int j = 0; j < columns_; ++j) {
2578 for (int i = 0; i < rows_; ++i) {
2579 int cell = GetCellNumber(i, j);
2580 if (IsMultiColumn(cell, true))
2583 GetCellInset(cell)->ascii(buf, sstr, 0);
2584 if (clen[j] < sstr.str().length())
2585 clen[j] = sstr.str().length();
2588 // then all (real) multicolumn cells!
2589 for (int j = 0; j < columns_; ++j) {
2590 for (int i = 0; i < rows_; ++i) {
2591 int cell = GetCellNumber(i, j);
2592 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2595 GetCellInset(cell)->ascii(buf, sstr, 0);
2596 int len = int(sstr.str().length());
2597 int const n = cells_in_multicolumn(cell);
2598 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2600 if (len > int(clen[j + n - 1]))
2601 clen[j + n - 1] = len;
2606 for (int i = 0; i < rows_; ++i) {
2608 if (asciiTopHLine(os, i, clen)) {
2609 for (int j = 0; j < depth; ++j)
2613 for (int j = 0; j < columns_; ++j) {
2614 if (IsPartOfMultiColumn(i,j))
2616 if (onlydata && j > 0)
2618 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2623 for (int j = 0; j < depth; ++j)
2625 if (asciiBottomHLine(os, i, clen)) {
2626 for (int j = 0; j < depth; ++j)
2638 InsetText * LyXTabular::GetCellInset(int cell) const
2641 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2645 InsetText * LyXTabular::GetCellInset(int row, int column) const
2647 cur_cell = GetCellNumber(row, column);
2648 return & cell_info[row][column].inset;
2652 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2654 // is this inset part of the tabular?
2655 if (!inset || inset->owner() != owner_) {
2656 lyxerr[Debug::INSETTEXT]
2657 << "this is not a cell of the tabular!" << endl;
2661 const int save_cur_cell = cur_cell;
2662 int cell = cur_cell;
2663 if (GetCellInset(cell) != inset) {
2665 if (cell == -1 || GetCellInset(cell) != inset) {
2671 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2672 if (GetCellInset(cell) == inset)
2675 lyxerr[Debug::INSETTEXT]
2676 << "LyXTabular::GetCellFromInset: "
2678 << ", cur_cell=" << save_cur_cell
2679 << ", maybe_cell=" << maybe_cell
2681 // We should have found a cell at this point
2683 lyxerr << "LyXTabular::GetCellFromInset: "
2684 << "Cell not found!" << endl;
2692 void LyXTabular::Validate(LaTeXFeatures & features) const
2694 if (IsLongTabular())
2695 features.require("longtable");
2697 features.require("rotating");
2698 for (int cell = 0; cell < numberofcells; ++cell) {
2699 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2700 features.require("array");
2701 GetCellInset(cell)->validate(features);
2706 vector<string> const LyXTabular::getLabelList() const
2708 vector<string> label_list;
2709 for (int i = 0; i < rows_; ++i)
2710 for (int j = 0; j < columns_; ++j) {
2711 vector<string> const l =
2712 GetCellInset(i, j)->getLabelList();
2713 label_list.insert(label_list.end(),
2714 l.begin(), l.end());
2720 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2722 Paragraph * par = GetCellInset(cell)->paragraph();
2724 for (; par; par = par->next()) {
2725 for (int i = 0; i < par->size(); ++i) {
2726 if (par->getChar(i) == Paragraph::META_NEWLINE)