1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
29 #include "frontends/Alert.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
34 #include "support/lstrings.h"
35 #include "support/lyxmanip.h"
36 #include "support/LAssert.h"
37 #include "insets/insettabular.h"
38 #include "insets/insettext.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()
64 multicolumn = LyXTabular::CELL_NORMAL;
65 alignment = LYX_ALIGN_CENTER;
66 valignment = LYX_VALIGN_TOP;
76 LyXTabular::rowstruct::rowstruct()
86 LyXTabular::columnstruct::columnstruct()
90 alignment = LYX_ALIGN_CENTER;
91 valignment = LYX_VALIGN_TOP;
96 LyXTabular::lttype::lttype()
106 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
110 Init(rows_arg, columns_arg);
114 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
119 Init(lt.rows_, lt.columns_, <);
120 // we really should change again to have InsetText as a pointer
121 // and allocate it then we would not have to do this stuff all
124 for (int i = 0; i < rows_; ++i) {
125 for (int j = 0; j < columns_; ++j) {
126 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
127 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
133 #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)
140 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
148 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
151 #warning This while method should look like this: (Lgb)
156 // If this and lt is not of the same size we have a serious bug
157 // So then it is ok to throw an exception, or for now
159 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
161 cell_info = lt.cell_info;
162 row_info = lt.row_info;
163 column_info = lt.column_info;
165 // long tabular stuff
166 SetLongTabular(lt.is_long_tabular);
167 endhead = lt.endhead;
168 endfoot = lt.endfoot;
169 endfirsthead = lt.endfirsthead;
170 endlastfoot = lt.endlastfoot;
180 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
182 LyXTabular * result = new LyXTabular(inset, *this, same_id);
184 // don't know if this is good but I need to Clone also
185 // the text-insets here, this is for the Undo-facility!
186 for (int i = 0; i < rows_; ++i) {
187 for (int j = 0; j < columns_; ++j) {
188 result->cell_info[i][j].inset = cell_info[i][j].inset;
189 result->cell_info[i][j].inset.setOwner(inset);
197 /* activates all lines and sets all widths to 0 */
198 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
201 columns_ = columns_arg;
202 row_info = row_vector(rows_, rowstruct());
203 column_info = column_vector(columns_, columnstruct());
204 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
212 for (int i = 0; i < rows_; ++i) {
213 for (int j = 0; j < columns_; ++j) {
214 cell_info[i][j].inset.setOwner(owner_);
215 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
216 cell_info[i][j].cellno = cellno++;
218 cell_info[i].back().right_line = true;
220 row_info.back().bottom_line = true;
221 row_info.front().bottom_line = true;
223 for (int i = 0; i < columns_; ++i) {
224 calculate_width_of_column(i);
226 column_info.back().right_line = true;
228 calculate_width_of_tabular();
230 rowofcell = vector<int>();
231 columnofcell = vector<int>();
232 set_row_column_number_info();
233 is_long_tabular = false;
236 endfirsthead.row = 0;
242 void LyXTabular::AppendRow(int cell)
246 int row = row_of_cell(cell);
248 row_vector::iterator rit = row_info.begin() + row;
249 row_info.insert(rit, rowstruct());
250 // now set the values of the row before
251 row_info[row] = row_info[row+1];
254 cell_vvector::iterator cit = cell_info.begin() + row;
255 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
257 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
260 for (int i = 0; i <= row; ++i) {
261 for (int j = 0; j < columns_; ++j) {
262 c_info[i][j] = cell_info[i][j];
265 for (int i = row + 1; i < rows_; ++i) {
266 for (int j = 0; j < columns_; ++j) {
267 c_info[i][j] = cell_info[i-1][j];
272 for (int j = 0; j < columns_; ++j) {
273 cell_info[row][j].inset.clear();
280 void LyXTabular::DeleteRow(int row)
282 if (rows_ == 1) return; // Not allowed to delete last row
284 row_info.erase(row_info.begin() + row); //&row_info[row]);
285 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
291 void LyXTabular::AppendColumn(int cell)
295 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
297 int const column = column_of_cell(cell);
298 column_vector::iterator cit = column_info.begin() + column + 1;
299 column_info.insert(cit, columnstruct());
300 // set the column values of the column before
301 column_info[column+1] = column_info[column];
303 for (int i = 0; i < rows_; ++i) {
304 for (int j = 0; j <= column; ++j) {
305 c_info[i][j] = cell_info[i][j];
307 for (int j = column + 1; j < columns_; ++j) {
308 c_info[i][j] = cell_info[i][j - 1];
310 // care about multicolumns
311 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
312 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
314 if ((column + 1) == columns_ ||
315 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
316 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
321 for (int i = 0; i < rows_; ++i) {
322 //cell_info[i][column].inset.clear();
323 cell_info[i][column + 1].inset.clear();
329 void LyXTabular::DeleteColumn(int column)
331 // Similar to DeleteRow
332 //if (!(columns_ - 1))
334 if (columns_ == 1) return; // Not allowed to delete last column
336 column_info.erase(column_info.begin() + column);
337 for (int i = 0; i < rows_; ++i) {
338 cell_info[i].erase(cell_info[i].begin() + column);
345 void LyXTabular::reinit()
351 void LyXTabular::Reinit(bool reset_widths)
354 for (int i = 0; i < rows_; ++i) {
355 for (int j = 0; j < columns_; ++j) {
356 cell_info[i][j].width_of_cell = 0;
357 cell_info[i][j].inset.setOwner(owner_);
362 for (int i = 0; i < columns_; ++i) {
363 calculate_width_of_column(i);
365 calculate_width_of_tabular();
367 set_row_column_number_info();
371 void LyXTabular::set_row_column_number_info(bool oldformat)
374 for (int row = 0; row < rows_; ++row) {
375 for (int column = 0; column<columns_; ++column) {
376 if (cell_info[row][column].multicolumn
377 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
379 cell_info[row][column].cellno = numberofcells;
382 ++numberofcells; // because this is one more than as we start from 0
384 rowofcell.resize(numberofcells);
385 columnofcell.resize(numberofcells);
387 for (int row = 0, column = 0, c = 0;
388 c < numberofcells && row < rows_ && column < columns_;) {
390 columnofcell[c] = column;
394 } while (column < columns_ &&
395 cell_info[row][column].multicolumn
396 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
397 if (column == columns_) {
403 for (int row = 0; row < rows_; ++row) {
404 for (int column = 0; column < columns_; ++column) {
405 if (IsPartOfMultiColumn(row,column))
407 // now set the right line of multicolumns right for oldformat read
409 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
411 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
412 cell_info[row][column].right_line =
413 cell_info[row][column+cn-1].right_line;
415 cell_info[row][column].inset.setAutoBreakRows(
416 !GetPWidth(GetCellNumber(row, column)).zero());
422 int LyXTabular::GetNumberOfCells() const
424 return numberofcells;
428 int LyXTabular::NumberOfCellsInRow(int cell) const
430 int const row = row_of_cell(cell);
432 for (int i = 0; i < columns_; ++i) {
433 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
440 /* returns 1 if there is a topline, returns 0 if not */
441 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
443 int const row = row_of_cell(cell);
445 if (!onlycolumn && IsMultiColumn(cell))
446 return cellinfo_of_cell(cell)->top_line;
447 return row_info[row].top_line;
451 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
453 // no bottom line underneath non-existent cells if you please
454 // Isn't that a programming error? Is so this should
455 // be an Assert instead. (Lgb)
456 if (cell >= numberofcells)
459 if (!onlycolumn && IsMultiColumn(cell))
460 return cellinfo_of_cell(cell)->bottom_line;
461 return row_info[row_of_cell(cell)].bottom_line;
465 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
467 if (!onlycolumn && IsMultiColumn(cell)) {
468 #ifdef SPECIAL_COLUM_HANDLING
469 if (cellinfo_of_cell(cell)->align_special.empty())
470 return cellinfo_of_cell(cell)->left_line;
471 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
473 return cellinfo_of_cell(cell)->left_line;
476 #ifdef SPECIAL_COLUM_HANDLING
477 if (column_info[column_of_cell(cell)].align_special.empty())
478 return column_info[column_of_cell(cell)].left_line;
479 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
481 return column_info[column_of_cell(cell)].left_line;
486 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
488 if (!onlycolumn && IsMultiColumn(cell)) {
489 #ifdef SPECIAL_COLUM_HANDLING
490 if (cellinfo_of_cell(cell)->align_special.empty())
491 return cellinfo_of_cell(cell)->right_line;
492 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
494 return cellinfo_of_cell(cell)->right_line;
497 #ifdef SPECIAL_COLUM_HANDLING
498 if (column_info[column_of_cell(cell)].align_special.empty())
499 return column_info[right_column_of_cell(cell)].right_line;
500 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
502 return column_info[right_column_of_cell(cell)].right_line;
507 bool LyXTabular::TopAlreadyDrawed(int cell) const
509 int row = row_of_cell(cell);
510 if ((row > 0) && !GetAdditionalHeight(row)) {
511 int column = column_of_cell(cell);
514 && cell_info[row][column].multicolumn
515 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
517 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
518 return row_info[row].bottom_line;
520 return cell_info[row][column].bottom_line;
526 bool LyXTabular::LeftAlreadyDrawed(int cell) const
528 int column = column_of_cell(cell);
530 int row = row_of_cell(cell);
532 (cell_info[row][column].multicolumn ==
533 LyXTabular::CELL_PART_OF_MULTICOLUMN));
534 if (GetAdditionalWidth(cell_info[row][column].cellno))
536 #ifdef SPECIAL_COLUM_HANDLING
537 return column_info[column].right_line;
539 return RightLine(cell_info[row][column].cellno, true);
546 bool LyXTabular::IsLastRow(int cell) const
548 return (row_of_cell(cell) == rows_ - 1);
552 int LyXTabular::GetAdditionalHeight(int row) const
554 if (!row || row >= rows_)
560 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
561 switch (cell_info[row - 1][column].multicolumn) {
562 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
563 bottom = cell_info[row - 1][column].bottom_line;
565 case LyXTabular::CELL_NORMAL:
566 bottom = row_info[row - 1].bottom_line;
569 for (int column = 0; column < columns_ - 1 && top; ++column) {
570 switch (cell_info[row][column].multicolumn){
571 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
572 top = cell_info[row][column].top_line;
574 case LyXTabular::CELL_NORMAL:
575 top = row_info[row].top_line;
579 return WIDTH_OF_LINE;
584 int LyXTabular::GetAdditionalWidth(int cell) const
586 // internally already set in SetWidthOfCell
587 // used to get it back in text.C
588 int const col = right_column_of_cell(cell);
589 int const row = row_of_cell(cell);
590 if (col < columns_ - 1 && RightLine(cell, true) &&
591 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
593 return WIDTH_OF_LINE;
600 // returns the maximum over all rows
601 int LyXTabular::GetWidthOfColumn(int cell) const
603 int const column1 = column_of_cell(cell);
604 int const column2 = right_column_of_cell(cell);
606 for (int i = column1; i <= column2; ++i) {
607 result += column_info[i].width_of_column;
613 int LyXTabular::GetWidthOfTabular() const
615 return width_of_tabular;
619 /* returns 1 if a complete update is necessary, otherwise 0 */
620 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
622 if (!IsMultiColumn(cell))
625 int const row = row_of_cell(cell);
626 int const column1 = column_of_cell(cell);
627 int const column2 = right_column_of_cell(cell);
629 // first set columns to 0 so we can calculate the right width
630 for (int i = column1; i <= column2; ++i) {
631 cell_info[row][i].width_of_cell = 0;
633 // set the width to MAX_WIDTH until width > 0
634 int width = (new_width + 2 * WIDTH_OF_LINE);
637 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
638 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
639 width -= column_info[i].width_of_column;
642 cell_info[row][i].width_of_cell = width;
648 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
650 int const row = row_of_cell(cell);
651 int const column1 = column_of_cell(cell);
652 int const column2 = right_column_of_cell(cell);
654 // first set columns to 0 so we can calculate the right width
656 for (; i <= column2; ++i)
657 cell_info[row][i].width_of_cell = 0;
658 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
660 if (i < numberofcells)
661 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
662 SetWidthOfMulticolCell(cell, new_width);
666 /* returns 1 if a complete update is necessary, otherwise 0 */
667 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
669 int const row = row_of_cell(cell);
670 int const column1 = column_of_cell(cell);
675 #ifdef SPECIAL_COLUM_HANDLING
676 if (RightLine(cell_info[row][column1].cellno, true) &&
677 (column1 < columns_-1) &&
678 LeftLine(cell_info[row][column1+1].cellno, true))
680 if (column_info[column1].right_line && (column1 < columns_-1) &&
681 column_info[column1+1].left_line) // additional width
685 add_width = WIDTH_OF_LINE;
687 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
690 if (IsMultiColumn(cell, true)) {
691 tmp = SetWidthOfMulticolCell(cell, new_width);
693 width = (new_width + 2*WIDTH_OF_LINE + add_width);
694 cell_info[row][column1].width_of_cell = width;
695 tmp = calculate_width_of_column_NMC(column1);
699 for (; i<columns_; ++i)
700 calculate_width_of_column_NMC(i);
701 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
703 if (i < numberofcells)
704 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
705 for (i = 0; i < columns_; ++i)
706 calculate_width_of_column(i);
707 calculate_width_of_tabular();
714 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
716 if (!IsMultiColumn(cell) || onlycolumn)
717 column_info[column_of_cell(cell)].alignment = align;
719 cellinfo_of_cell(cell)->alignment = align;
724 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
726 if (!IsMultiColumn(cell) || onlycolumn)
727 column_info[column_of_cell(cell)].valignment = align;
729 cellinfo_of_cell(cell)->valignment = align;
734 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
736 bool flag = !width.zero();
737 int const j = column_of_cell(cell);
739 column_info[j].p_width = width;
740 if (flag) // do this only if there is a width
741 SetAlignment(cell, LYX_ALIGN_LEFT);
742 for (int i = 0; i < rows_; ++i) {
743 int c = GetCellNumber(i, j);
744 flag = !GetPWidth(c).zero(); // because of multicolumns!
745 GetCellInset(c)->setAutoBreakRows(flag);
751 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
753 bool const flag = !width.zero();
755 cellinfo_of_cell(cell)->p_width = width;
756 if (IsMultiColumn(cell)) {
757 GetCellInset(cell)->setAutoBreakRows(flag);
764 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
765 LyXTabular::Feature what)
767 if (what == SET_SPECIAL_MULTI)
768 cellinfo_of_cell(cell)->align_special = special;
770 column_info[column_of_cell(cell)].align_special = special;
775 bool LyXTabular::SetAllLines(int cell, bool line)
777 SetTopLine(cell, line);
778 SetBottomLine(cell, line);
779 SetRightLine(cell, line);
780 SetLeftLine(cell, line);
785 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
787 int const row = row_of_cell(cell);
789 if (onlycolumn || !IsMultiColumn(cell))
790 row_info[row].top_line = line;
792 cellinfo_of_cell(cell)->top_line = line;
797 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
799 if (onlycolumn || !IsMultiColumn(cell))
800 row_info[row_of_cell(cell)].bottom_line = line;
802 cellinfo_of_cell(cell)->bottom_line = line;
807 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
809 if (onlycolumn || !IsMultiColumn(cell))
810 column_info[column_of_cell(cell)].left_line = line;
812 cellinfo_of_cell(cell)->left_line = line;
817 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
819 if (onlycolumn || !IsMultiColumn(cell))
820 column_info[right_column_of_cell(cell)].right_line = line;
822 cellinfo_of_cell(cell)->right_line = line;
827 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
829 if (!onlycolumn && IsMultiColumn(cell))
830 return cellinfo_of_cell(cell)->alignment;
832 return column_info[column_of_cell(cell)].alignment;
836 LyXTabular::VAlignment
837 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
839 if (!onlycolumn && IsMultiColumn(cell))
840 return cellinfo_of_cell(cell)->valignment;
842 return column_info[column_of_cell(cell)].valignment;
846 LyXLength const LyXTabular::GetPWidth(int cell) const
848 if (IsMultiColumn(cell))
849 return cellinfo_of_cell(cell)->p_width;
850 return column_info[column_of_cell(cell)].p_width;
854 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
856 return column_info[column_of_cell(cell)].p_width;
860 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
862 if (IsMultiColumn(cell))
863 return cellinfo_of_cell(cell)->p_width;
868 string const LyXTabular::GetAlignSpecial(int cell, int what) const
870 if (what == SET_SPECIAL_MULTI)
871 return cellinfo_of_cell(cell)->align_special;
872 return column_info[column_of_cell(cell)].align_special;
876 int LyXTabular::GetWidthOfCell(int cell) const
878 int const row = row_of_cell(cell);
879 int const column1 = column_of_cell(cell);
880 int const column2 = right_column_of_cell(cell);
882 for (int i = column1; i <= column2; ++i) {
883 result += cell_info[row][i].width_of_cell;
889 int LyXTabular::GetBeginningOfTextInCell(int cell) const
893 switch (GetAlignment(cell)){
894 case LYX_ALIGN_CENTER:
895 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
897 case LYX_ALIGN_RIGHT:
898 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
899 // + GetAdditionalWidth(cell);
901 default: /* LYX_ALIGN_LEFT: nothing :-) */
911 bool LyXTabular::IsFirstCellInRow(int cell) const
913 return column_of_cell(cell) == 0;
917 int LyXTabular::GetFirstCellInRow(int row) const
921 return cell_info[row][0].cellno;
924 bool LyXTabular::IsLastCellInRow(int cell) const
926 return (right_column_of_cell(cell) == (columns_ - 1));
930 int LyXTabular::GetLastCellInRow(int row) const
934 return cell_info[row][columns_-1].cellno;
938 bool LyXTabular::calculate_width_of_column(int column)
940 int const old_column_width = column_info[column].width_of_column;
943 for (int i = 0; i < rows_; ++i) {
944 maximum = max(cell_info[i][column].width_of_cell, maximum);
946 column_info[column].width_of_column = maximum;
947 return (column_info[column].width_of_column != old_column_width);
952 // calculate the with of the column without regarding REAL MultiColumn
953 // cells. This means MultiColumn-cells spanning more than 1 column.
955 bool LyXTabular::calculate_width_of_column_NMC(int column)
957 int const old_column_width = column_info[column].width_of_column;
959 for (int i = 0; i < rows_; ++i) {
960 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
961 (cell_info[i][column].width_of_cell > max)) {
962 max = cell_info[i][column].width_of_cell;
965 column_info[column].width_of_column = max;
966 return (column_info[column].width_of_column != old_column_width);
970 void LyXTabular::calculate_width_of_tabular()
972 width_of_tabular = 0;
973 for (int i = 0; i < columns_; ++i) {
974 width_of_tabular += column_info[i].width_of_column;
979 int LyXTabular::row_of_cell(int cell) const
981 if (cell >= numberofcells)
985 return rowofcell[cell];
989 int LyXTabular::column_of_cell(int cell) const
991 if (cell >= numberofcells)
995 return columnofcell[cell];
999 int LyXTabular::right_column_of_cell(int cell) const
1001 int const row = row_of_cell(cell);
1002 int column = column_of_cell(cell);
1003 while (column < (columns_ - 1) &&
1004 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1010 // Perfect case for a template... (Lgb)
1011 // or perhaps not...
1014 string const write_attribute(string const & name, T const & t)
1016 string str = " " + name + "=\"" + tostr(t) + "\"";
1021 string const write_attribute(string const & name, bool const & b)
1023 return write_attribute(name, int(b));
1027 string const write_attribute(string const & name, LyXLength const & value)
1029 return write_attribute(name, value.asString());
1033 string const write_attribute(string const & name, int value)
1035 string str = " " + name + "=\"" + tostr(value) + "\"";
1040 string const write_attribute(string const & name, string const & value)
1042 string str = " " + name + "=\"" + value + "\"";
1047 string const write_attribute(string const & name, bool value)
1049 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1054 string const write_attribute(string const & name, LyXLength const & value)
1056 string str = " " + name + "=\"" + value.asString() + "\"";
1064 string const tostr(LyXAlignment const & num)
1067 case LYX_ALIGN_NONE:
1069 case LYX_ALIGN_BLOCK:
1071 case LYX_ALIGN_LEFT:
1073 case LYX_ALIGN_CENTER:
1075 case LYX_ALIGN_RIGHT:
1077 case LYX_ALIGN_LAYOUT:
1079 case LYX_ALIGN_SPECIAL:
1088 string const tostr(LyXTabular::VAlignment const & num)
1091 case LyXTabular::LYX_VALIGN_TOP:
1093 case LyXTabular::LYX_VALIGN_CENTER:
1095 case LyXTabular::LYX_VALIGN_BOTTOM:
1104 string const tostr(LyXTabular::BoxType const & num)
1107 case LyXTabular::BOX_NONE:
1109 case LyXTabular::BOX_PARBOX:
1111 case LyXTabular::BOX_MINIPAGE:
1118 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1122 << write_attribute("version", 2)
1123 << write_attribute("rows", rows_)
1124 << write_attribute("columns", columns_)
1126 // global longtable options
1128 << write_attribute("rotate", tostr(rotate))
1129 << write_attribute("islongtable", tostr(is_long_tabular))
1130 << write_attribute("endhead", endhead.row)
1131 << write_attribute("endfirsthead", endfirsthead.row)
1132 << write_attribute("endfoot", endfoot.row)
1133 << write_attribute("endlastfoot", endlastfoot.row)
1135 for (int j = 0; j < columns_; ++j) {
1137 << write_attribute("alignment", tostr(column_info[j].alignment))
1138 << write_attribute("valignment", tostr(column_info[j].valignment))
1139 << write_attribute("leftline", tostr(column_info[j].left_line))
1140 << write_attribute("rightline", tostr(column_info[j].right_line))
1141 << write_attribute("width", column_info[j].p_width.asString())
1142 << write_attribute("special", column_info[j].align_special)
1145 for (int i = 0; i < rows_; ++i) {
1147 << write_attribute("topline", tostr(row_info[i].top_line))
1148 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1149 << write_attribute("newpage", tostr(row_info[i].newpage))
1151 for (int j = 0; j < columns_; ++j) {
1153 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1154 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1155 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1156 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1157 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1158 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1159 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1160 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1161 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1162 << write_attribute("width", cell_info[i][j].p_width)
1163 << write_attribute("special", cell_info[i][j].align_special)
1165 os << "\\begin_inset ";
1166 cell_info[i][j].inset.write(buf, os);
1167 os << "\n\\end_inset \n"
1172 os << "</lyxtabular>\n";
1178 // I would have liked a fromstr template a lot better. (Lgb)
1181 bool string2type(string const str, LyXAlignment & num)
1184 num = LYX_ALIGN_NONE;
1185 else if (str == "block")
1186 num = LYX_ALIGN_BLOCK;
1187 else if (str == "left")
1188 num = LYX_ALIGN_LEFT;
1189 else if (str == "center")
1190 num = LYX_ALIGN_CENTER;
1191 else if (str == "right")
1192 num = LYX_ALIGN_RIGHT;
1200 bool string2type(string const str, LyXTabular::VAlignment & num)
1203 num = LyXTabular::LYX_VALIGN_TOP;
1204 else if (str == "center")
1205 num = LyXTabular::LYX_VALIGN_CENTER;
1206 else if (str == "bottom")
1207 num = LyXTabular::LYX_VALIGN_BOTTOM;
1215 bool string2type(string const str, LyXTabular::BoxType & num)
1218 num = LyXTabular::BOX_NONE;
1219 else if (str == "parbox")
1220 num = LyXTabular::BOX_PARBOX;
1221 else if (str == "minipage")
1222 num = LyXTabular::BOX_MINIPAGE;
1230 bool string2type(string const str, bool & num)
1234 else if (str == "false")
1242 bool getTokenValue(string const & str, const char * token, string & ret)
1244 size_t token_length = strlen(token);
1245 string::size_type pos = str.find(token);
1247 if (pos == string::npos || pos + token_length + 1 >= str.length()
1248 || str[pos + token_length] != '=')
1251 pos += token_length + 1;
1253 if ((ch != '"') && (ch != '\'')) { // only read till next space
1257 while ((pos < str.length() - 1) && (str[++pos] != ch))
1264 bool getTokenValue(string const & str, const char * token, int & num)
1267 if (!getTokenValue(str, token, tmp))
1269 num = strToInt(tmp);
1274 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1277 if (!getTokenValue(str, token, tmp))
1279 return string2type(tmp, num);
1283 bool getTokenValue(string const & str, const char * token,
1284 LyXTabular::VAlignment & num)
1287 if (!getTokenValue(str, token, tmp))
1289 return string2type(tmp, num);
1293 bool getTokenValue(string const & str, const char * token,
1294 LyXTabular::BoxType & num)
1297 if (!getTokenValue(str, token, tmp))
1299 return string2type(tmp, num);
1303 bool getTokenValue(string const & str, const char * token, bool & flag)
1306 if (!getTokenValue(str, token, tmp))
1308 return string2type(tmp, flag);
1312 bool getTokenValue(string const & str, const char * token, LyXLength & len)
1315 if (!getTokenValue(str, token, tmp))
1317 return isValidLength(tmp, &len);
1322 void l_getline(istream & is, string & str)
1325 while (str.empty()) {
1327 if (!str.empty() && str[str.length() - 1] == '\r')
1328 str.erase(str.length() - 1);
1335 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1338 istream & is = lex.getStream();
1340 l_getline(is, line);
1341 if (!prefixIs(line, "<lyxtabular ")
1342 && !prefixIs(line, "<LyXTabular ")) {
1343 OldFormatRead(lex, line);
1348 if (!getTokenValue(line, "version", version))
1351 ReadOld(buf, is, lex, line);
1352 else if (version == 2)
1353 ReadNew(buf, is, lex, line);
1357 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1358 LyXLex & lex, string const & l)
1362 if (!getTokenValue(line, "rows", rows_arg))
1365 if (!getTokenValue(line, "columns", columns_arg))
1367 Init(rows_arg, columns_arg);
1368 l_getline(is, line);
1369 if (!prefixIs(line, "<features")) {
1370 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1371 line << ")" << endl;
1374 getTokenValue(line, "rotate", rotate);
1375 getTokenValue(line, "islongtable", is_long_tabular);
1376 getTokenValue(line, "endhead", endhead.row);
1377 getTokenValue(line, "endfirsthead", endfirsthead.row);
1378 getTokenValue(line, "endfoot", endfoot.row);
1379 getTokenValue(line, "endlastfoot", endlastfoot.row);
1380 endhead.row = abs(endhead.row);
1381 endfirsthead.row = abs(endfirsthead.row);
1382 endfoot.row = abs(endfoot.row);
1383 endlastfoot.row = abs(endlastfoot.row);
1384 for (int j = 0; j < columns_; ++j) {
1386 if (!prefixIs(line,"<column")) {
1387 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1388 line << ")" << endl;
1391 getTokenValue(line, "alignment", column_info[j].alignment);
1392 getTokenValue(line, "valignment", column_info[j].valignment);
1393 getTokenValue(line, "leftline", column_info[j].left_line);
1394 getTokenValue(line, "rightline", column_info[j].right_line);
1395 getTokenValue(line, "width", column_info[j].p_width);
1396 getTokenValue(line, "special", column_info[j].align_special);
1399 for (int i = 0; i < rows_; ++i) {
1400 l_getline(is, line);
1401 if (!prefixIs(line, "<row")) {
1402 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1403 line << ")" << endl;
1406 getTokenValue(line, "topline", row_info[i].top_line);
1407 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1408 getTokenValue(line, "newpage", row_info[i].newpage);
1409 for (int j = 0; j < columns_; ++j) {
1410 l_getline(is, line);
1411 if (!prefixIs(line, "<cell")) {
1412 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1413 line << ")" << endl;
1416 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1417 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1418 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1419 getTokenValue(line, "topline", cell_info[i][j].top_line);
1420 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1421 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1422 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1423 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1424 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1425 getTokenValue(line, "width", cell_info[i][j].p_width);
1426 getTokenValue(line, "special", cell_info[i][j].align_special);
1427 l_getline(is, line);
1428 if (prefixIs(line, "\\begin_inset")) {
1429 cell_info[i][j].inset.read(buf, lex);
1430 l_getline(is, line);
1432 if (!prefixIs(line, "</cell>")) {
1433 lyxerr << "Wrong tabular format (expected </cell> got" <<
1434 line << ")" << endl;
1438 l_getline(is, line);
1439 if (!prefixIs(line, "</row>")) {
1440 lyxerr << "Wrong tabular format (expected </row> got" <<
1441 line << ")" << endl;
1445 while (!prefixIs(line, "</lyxtabular>")) {
1446 l_getline(is, line);
1448 set_row_column_number_info();
1452 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1458 int columns_arg = 0;
1459 int is_long_tabular_arg = false;
1460 int rotate_arg = false;
1469 istream & is = lex.getStream();
1472 version = lyx::atoi(s.substr(8, string::npos));
1476 vector<int> cont_row_info;
1479 lyxerr << "Tabular format < 5 is not supported anymore\n"
1480 "Get an older version of LyX (< 1.1.x) for conversion!"
1482 Alert::alert(_("Warning:"),
1483 _("Tabular format < 5 is not supported anymore\n"),
1484 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1486 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1487 >> rotate_arg >> a >> b >> c >> d;
1489 is >> rows_arg >> columns_arg;
1490 Init(rows_arg, columns_arg);
1491 cont_row_info = vector<int>(rows_arg);
1492 SetLongTabular(is_long_tabular_arg);
1493 SetRotateTabular(rotate_arg);
1495 for (i = 0; i < rows_; ++i) {
1497 cont_row_info[i] = false;
1499 for (i = 0; i < columns_; ++i) {
1502 for (i = 0; i < rows_; ++i) {
1503 for (j = 0; j < columns_; ++j) {
1508 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1509 >> rotate_arg >> a >> b >> c >> d;
1510 Init(rows_arg, columns_arg);
1511 cont_row_info = vector<int>(rows_arg);
1512 SetLongTabular(is_long_tabular_arg);
1513 SetRotateTabular(rotate_arg);
1514 endhead.row = a + 1;
1515 endfirsthead.row = b + 1;
1516 endfoot.row = c + 1;
1517 endlastfoot.row = d + 1;
1518 for (i = 0; i < rows_; ++i) {
1519 a = b = c = d = e = f = g = 0;
1520 is >> a >> b >> c >> d;
1521 row_info[i].top_line = a;
1522 row_info[i].bottom_line = b;
1523 cont_row_info[i] = c;
1524 row_info[i].newpage = d;
1526 for (i = 0; i < columns_; ++i) {
1531 char ch; // skip '"'
1534 // ignore is buggy but we will use it later (Lgb)
1535 is.ignore(); // skip '"'
1537 getline(is, s1, '"');
1539 is >> ch; // skip '"'
1541 // ignore is buggy but we will use it later (Lgb)
1542 is.ignore(); // skip '"'
1544 getline(is, s2, '"');
1545 column_info[i].alignment = static_cast<LyXAlignment>(a);
1546 column_info[i].left_line = b;
1547 column_info[i].right_line = c;
1548 column_info[i].p_width = LyXLength(s1);
1549 column_info[i].align_special = s2;
1551 for (i = 0; i < rows_; ++i) {
1552 for (j = 0; j < columns_; ++j) {
1555 is >> a >> b >> c >> d >> e >> f >> g;
1558 is >> ch; // skip '"'
1560 // ignore is buggy but we will use it later (Lgb)
1561 is.ignore(); // skip '"'
1563 getline(is, s1, '"');
1565 is >> ch; // skip '"'
1567 // ignore is buggy but we will use it later (Lgb)
1568 is.ignore(); // skip '"'
1570 getline(is, s2, '"');
1571 cell_info[i][j].multicolumn = static_cast<char>(a);
1572 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1573 cell_info[i][j].top_line = static_cast<char>(c);
1574 cell_info[i][j].bottom_line = static_cast<char>(d);
1575 cell_info[i][j].left_line = column_info[j].left_line;
1576 cell_info[i][j].right_line = column_info[j].right_line;
1577 cell_info[i][j].rotate = static_cast<bool>(f);
1578 cell_info[i][j].usebox = static_cast<BoxType>(g);
1579 cell_info[i][j].align_special = s1;
1580 cell_info[i][j].p_width = LyXLength(s2);
1584 set_row_column_number_info(true);
1586 Paragraph * par = new Paragraph;
1587 Paragraph * return_par = 0;
1591 Paragraph::depth_type depth = 0;
1592 LyXFont font(LyXFont::ALL_INHERIT);
1593 font.setLanguage(owner_->bufferOwner()->getLanguage());
1595 while (lex.isOK()) {
1597 string const token = lex.getString();
1600 if (token == "\\layout"
1601 || token == "\\end_float"
1602 || token == "\\end_deeper") {
1603 lex.pushToken(token);
1604 #ifndef NO_COMPABILITY
1605 // Here we need to insert the inset_ert_contents into the last
1606 // cell of the tabular.
1607 owner_->bufferOwner()->insertErtContents(par, pos, font);
1611 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1616 lex.pushToken(token);
1620 lex.printError("New Paragraph allocated! This should not happen!");
1621 lex.pushToken(token);
1627 // now we have the par we should fill the insets with this!
1629 InsetText * inset = GetCellInset(cell);
1632 for (int i = 0; i < par->size(); ++i) {
1633 if (par->isNewline(i)) {
1635 if (cell > numberofcells) {
1636 lyxerr << "Some error in reading old table format occured!" <<
1637 endl << "Terminating when reading cell[" << cell << "]!" <<
1642 row = row_of_cell(cell);
1643 if (cont_row_info[row]) {
1645 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1646 while (!IsFirstCellInRow(--cell));
1648 inset = GetCellInset(cell);
1651 inset = GetCellInset(cell);
1652 row = row_of_cell(cell);
1653 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1655 // insert a space instead
1657 par->insertChar(i, ' ');
1660 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1661 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1668 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1670 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1671 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1675 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1677 int const row = row_of_cell(cell);
1678 int const column = column_of_cell(cell);
1679 return &cell_info[row][column];
1683 void LyXTabular::SetMultiColumn(int cell, int number)
1685 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1686 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1687 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1688 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1689 for (number--; number > 0; --number) {
1690 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1692 set_row_column_number_info();
1696 int LyXTabular::cells_in_multicolumn(int cell) const
1698 int const row = row_of_cell(cell);
1699 int column = column_of_cell(cell);
1702 while ((column < columns_) &&
1703 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1712 int LyXTabular::UnsetMultiColumn(int cell)
1714 int const row = row_of_cell(cell);
1715 int column = column_of_cell(cell);
1719 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1720 cell_info[row][column].multicolumn = CELL_NORMAL;
1722 while ((column < columns_) &&
1723 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1725 cell_info[row][column].multicolumn = CELL_NORMAL;
1730 set_row_column_number_info();
1735 void LyXTabular::SetLongTabular(bool what)
1737 is_long_tabular = what;
1741 bool LyXTabular::IsLongTabular() const
1743 return is_long_tabular;
1747 void LyXTabular::SetRotateTabular(bool flag)
1753 bool LyXTabular::GetRotateTabular() const
1759 void LyXTabular::SetRotateCell(int cell, bool flag)
1761 cellinfo_of_cell(cell)->rotate = flag;
1765 bool LyXTabular::GetRotateCell(int cell) const
1767 return cellinfo_of_cell(cell)->rotate;
1771 bool LyXTabular::NeedRotating() const
1775 for (int i = 0; i < rows_; ++i) {
1776 for (int j = 0; j < columns_; ++j) {
1777 if (cell_info[i][j].rotate)
1785 bool LyXTabular::IsLastCell(int cell) const
1787 if ((cell + 1) < numberofcells)
1793 int LyXTabular::GetCellAbove(int cell) const
1795 if (row_of_cell(cell) > 0)
1796 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1801 int LyXTabular::GetCellBelow(int cell) const
1803 if (row_of_cell(cell) + 1 < rows_)
1804 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1809 int LyXTabular::GetLastCellAbove(int cell) const
1811 if (row_of_cell(cell) <= 0)
1813 if (!IsMultiColumn(cell))
1814 return GetCellAbove(cell);
1815 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1819 int LyXTabular::GetLastCellBelow(int cell) const
1821 if (row_of_cell(cell) + 1 >= rows_)
1823 if (!IsMultiColumn(cell))
1824 return GetCellBelow(cell);
1825 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1829 int LyXTabular::GetCellNumber(int row, int column) const
1832 if (column >= columns_)
1833 column = columns_ - 1;
1834 else if (column < 0)
1841 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1843 return cell_info[row][column].cellno;
1847 void LyXTabular::SetUsebox(int cell, BoxType type)
1849 cellinfo_of_cell(cell)->usebox = type;
1853 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1855 if (column_info[column_of_cell(cell)].p_width.zero() &&
1856 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1858 if (cellinfo_of_cell(cell)->usebox > 1)
1859 return cellinfo_of_cell(cell)->usebox;
1860 return UseParbox(cell);
1863 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1865 if (!ltt.row || (ltt.row > rows_))
1867 return (row == (ltt.row - 1));
1871 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1881 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1884 return checkLTType(row, hd);
1888 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1891 return checkLTType(row, hd);
1895 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1905 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1908 return checkLTType(row, fd);
1912 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1915 return checkLTType(row, fd);
1919 void LyXTabular::SetLTNewPage(int row, bool what)
1921 row_info[row].newpage = what;
1925 bool LyXTabular::GetLTNewPage(int row) const
1927 return row_info[row].newpage;
1931 bool LyXTabular::SetAscentOfRow(int row, int height)
1933 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1935 row_info[row].ascent_of_row = height;
1940 bool LyXTabular::SetDescentOfRow(int row, int height)
1942 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1944 row_info[row].descent_of_row = height;
1949 int LyXTabular::GetAscentOfRow(int row) const
1953 return row_info[row].ascent_of_row;
1957 int LyXTabular::GetDescentOfRow(int row) const
1961 return row_info[row].descent_of_row;
1965 int LyXTabular::GetHeightOfTabular() const
1969 for (int row = 0; row < rows_; ++row)
1970 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1971 GetAdditionalHeight(row);
1976 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1978 if ((row >= rows_) || (column >= columns_))
1980 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1984 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1986 if ((row < 0) || (row >= rows_))
1989 int const fcell = GetFirstCellInRow(row);
1990 int const n = NumberOfCellsInRow(fcell) + fcell;
1993 for (int i = fcell; i < n; ++i) {
1997 if (tmp == (n - fcell)){
2000 for (int i = fcell; i < n; ++i) {
2003 << column_of_cell(i) + 1
2005 << right_column_of_cell(i) + 1
2017 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
2019 if ((row < 0) || (row >= rows_))
2022 int const fcell = GetFirstCellInRow(row);
2023 int const n = NumberOfCellsInRow(fcell) + fcell;
2026 for (int i = fcell; i < n; ++i) {
2030 if (tmp == (n-fcell)){
2033 for (int i = fcell; i < n; ++i) {
2034 if (BottomLine(i)) {
2036 << column_of_cell(i) + 1
2038 << right_column_of_cell(i) + 1
2050 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2054 if (GetRotateCell(cell)) {
2055 os << "\\begin{sideways}\n";
2058 if (IsMultiColumn(cell)) {
2059 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2060 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2061 os << cellinfo_of_cell(cell)->align_special << "}{";
2063 if (LeftLine(cell) &&
2064 (IsFirstCellInRow(cell) ||
2065 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2066 !RightLine(cell-1, true))))
2070 if (!GetPWidth(cell).zero()) {
2071 switch (GetVAlignment(cell)) {
2072 case LYX_VALIGN_TOP:
2075 case LYX_VALIGN_CENTER:
2078 case LYX_VALIGN_BOTTOM:
2082 os << "{" << GetPWidth(cell).asLatexString() << '}';
2084 switch (GetAlignment(cell)) {
2085 case LYX_ALIGN_LEFT:
2088 case LYX_ALIGN_RIGHT:
2096 if (RightLine(cell))
2098 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2104 if (GetUsebox(cell) == BOX_PARBOX) {
2106 switch (GetVAlignment(cell)) {
2107 case LYX_VALIGN_TOP:
2110 case LYX_VALIGN_CENTER:
2113 case LYX_VALIGN_BOTTOM:
2117 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2118 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2119 os << "\\begin{minipage}[";
2120 switch (GetVAlignment(cell)) {
2121 case LYX_VALIGN_TOP:
2124 case LYX_VALIGN_CENTER:
2127 case LYX_VALIGN_BOTTOM:
2131 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2138 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2143 if (GetUsebox(cell) == BOX_PARBOX)
2145 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2146 os << "%\n\\end{minipage}";
2149 if (IsMultiColumn(cell)){
2152 if (GetRotateCell(cell)) {
2153 os << "%\n\\end{sideways}";
2160 int LyXTabular::Latex(Buffer const * buf,
2161 ostream & os, bool fragile, bool fp) const
2166 //+---------------------------------------------------------------------
2167 //+ first the opening preamble +
2168 //+---------------------------------------------------------------------
2171 os << "\\begin{sideways}\n";
2174 if (is_long_tabular)
2175 os << "\\begin{longtable}{";
2177 os << "\\begin{tabular}{";
2178 for (int i = 0; i < columns_; ++i) {
2179 if (!column_info[i].align_special.empty()) {
2180 os << column_info[i].align_special;
2182 if (column_info[i].left_line)
2184 if (!column_info[i].p_width.zero()) {
2185 switch (column_info[i].valignment) {
2186 case LYX_VALIGN_TOP:
2189 case LYX_VALIGN_CENTER:
2192 case LYX_VALIGN_BOTTOM:
2197 << column_info[i].p_width.asLatexString()
2200 switch (column_info[i].alignment) {
2201 case LYX_ALIGN_LEFT:
2204 case LYX_ALIGN_RIGHT:
2212 if (column_info[i].right_line)
2219 //+---------------------------------------------------------------------
2220 //+ the single row and columns (cells) +
2221 //+---------------------------------------------------------------------
2223 for (int i = 0; i < rows_; ++i) {
2224 ret += TeXTopHLine(os, i);
2225 #warning Implement top double lines for LT Header/Footers
2228 ret += TeXBottomHLine(os, i-1);
2229 ret += TeXTopHLine(os, i);
2232 for (int j = 0; j < columns_; ++j) {
2233 if (IsPartOfMultiColumn(i,j))
2235 ret += TeXCellPreamble(os, cell);
2236 InsetText * inset = GetCellInset(cell);
2238 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2239 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2243 ret += inset->latex(buf, os, fragile, fp);
2247 ret += TeXCellPostamble(os, cell);
2248 if (!IsLastCellInRow(cell)) { // not last cell in row
2256 ret += TeXBottomHLine(os, i);
2257 if (IsLongTabular()) {
2258 if (i == (endhead.row - 1)) {
2259 if (endhead.bottomDL)
2260 ret += TeXBottomHLine(os, i);
2261 os << "\\endhead\n";
2264 if (i == (endfirsthead.row - 1)) {
2265 if (endfirsthead.bottomDL)
2266 ret += TeXBottomHLine(os, i);
2267 os << "\\endfirsthead\n";
2270 if (i == (endfoot.row - 1)) {
2271 if (endfoot.bottomDL)
2272 ret += TeXBottomHLine(os, i);
2273 os << "\\endfoot\n";
2276 if (i == (endlastfoot.row - 1)) {
2277 if (endlastfoot.bottomDL)
2278 ret += TeXBottomHLine(os, i);
2279 os << "\\endlastfoot\n";
2282 if (row_info[i].newpage) {
2283 os << "\\newpage\n";
2289 //+---------------------------------------------------------------------
2290 //+ the closing of the tabular +
2291 //+---------------------------------------------------------------------
2293 if (is_long_tabular)
2294 os << "\\end{longtable}";
2296 os << "\\end{tabular}";
2298 os << "\n\\end{sideways}";
2306 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2309 int cell = GetFirstCellInRow(row);
2312 for (int j = 0; j < columns_; ++j) {
2313 if (IsPartOfMultiColumn(row, j))
2316 os << "<entry align=\"";
2317 switch (GetAlignment(cell)) {
2318 case LYX_ALIGN_LEFT:
2321 case LYX_ALIGN_RIGHT:
2329 os << "\" valign=\"";
2330 switch (GetVAlignment(cell)) {
2331 case LYX_VALIGN_TOP:
2334 case LYX_VALIGN_BOTTOM:
2337 case LYX_VALIGN_CENTER:
2342 if (IsMultiColumn(cell)) {
2343 os << " namest=\"col" << j << "\" ";
2344 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2348 ret += GetCellInset(cell)->docbook(buf, os);
2357 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2361 //+---------------------------------------------------------------------
2362 //+ first the opening preamble +
2363 //+---------------------------------------------------------------------
2365 os << "<tgroup cols=\"" << columns_
2366 << "\" colsep=\"1\" rowsep=\"1\">\n";
2368 for (int i = 0; i < columns_; ++i) {
2369 os << "<colspec colname=\"col" << i << "\" align=\"";
2370 switch (column_info[i].alignment) {
2371 case LYX_ALIGN_LEFT:
2374 case LYX_ALIGN_RIGHT:
2385 //+---------------------------------------------------------------------
2386 //+ Long Tabular case +
2387 //+---------------------------------------------------------------------
2389 if ( IsLongTabular() ) {
2391 if( endhead.row || endfirsthead.row ) {
2393 if( endfirsthead.row ) {
2394 docbookRow( buf, os, endfirsthead.row - 1);
2396 if( endhead.row && endhead.row != endfirsthead.row) {
2397 docbookRow(buf, os, endhead.row - 1);
2403 if( endfoot.row || endlastfoot.row ) {
2406 docbookRow( buf, os, endfoot.row - 1);
2408 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2409 docbookRow( buf, os, endlastfoot.row - 1);
2414 //+---------------------------------------------------------------------
2415 //+ the single row and columns (cells) +
2416 //+---------------------------------------------------------------------
2419 for (int i = 0; i < rows_; ++i) {
2420 if(!IsLongTabular() || (
2421 i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2422 i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2423 docbookRow( buf, os, i);
2427 //+---------------------------------------------------------------------
2428 //+ the closing of the tabular +
2429 //+---------------------------------------------------------------------
2441 void print_n_chars(ostream & os, unsigned char ch, int n)
2443 os << string(n, ch);
2449 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2450 vector<unsigned int> const & clen) const
2452 int const fcell = GetFirstCellInRow(row);
2453 int const n = NumberOfCellsInRow(fcell) + fcell;
2456 for (int i = fcell; i < n; ++i) {
2466 for (int i = fcell; i < n; ++i) {
2477 int column = column_of_cell(i);
2478 int len = clen[column];
2479 while (IsPartOfMultiColumn(row, ++column))
2480 len += clen[column] + 4;
2481 print_n_chars(os, ch, len);
2496 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2497 vector<unsigned int> const & clen) const
2499 int const fcell = GetFirstCellInRow(row);
2500 int const n = NumberOfCellsInRow(fcell) + fcell;
2503 for (int i = fcell; i < n; ++i) {
2504 if (BottomLine(i)) {
2513 for (int i = fcell; i < n; ++i) {
2514 if (BottomLine(i)) {
2524 int column = column_of_cell(i);
2525 int len = clen[column];
2526 while (IsPartOfMultiColumn(row, ++column))
2527 len += clen[column] + 4;
2528 print_n_chars(os, ch, len);
2529 if (BottomLine(i)) {
2543 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2544 int cell, int row, int column,
2545 vector<unsigned int> const & clen) const
2548 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2555 unsigned int len1 = sstr.str().length();
2556 unsigned int len2 = clen[column];
2557 while (IsPartOfMultiColumn(row, ++column))
2558 len2 += clen[column] + 4;
2561 switch (GetAlignment(cell)) {
2563 case LYX_ALIGN_LEFT:
2566 case LYX_ALIGN_RIGHT:
2570 case LYX_ALIGN_CENTER:
2576 for (unsigned int i = 0; i < len1; ++i)
2579 for (unsigned int i = 0; i < len2; ++i)
2581 if (RightLine(cell))
2590 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2594 //+---------------------------------------------------------------------
2595 //+ first calculate the width of the single columns +
2596 //+---------------------------------------------------------------------
2597 vector<unsigned int> clen(columns_);
2599 // first all non (real) multicolumn cells!
2600 for (int j = 0; j < columns_; ++j) {
2602 for (int i = 0; i < rows_; ++i) {
2603 int cell = GetCellNumber(i, j);
2604 if (IsMultiColumn(cell, true))
2607 GetCellInset(cell)->ascii(buf, sstr, 0);
2608 if (clen[j] < sstr.str().length())
2609 clen[j] = sstr.str().length();
2612 // then all (real) multicolumn cells!
2613 for (int j = 0; j < columns_; ++j) {
2614 for (int i = 0; i < rows_; ++i) {
2615 int cell = GetCellNumber(i, j);
2616 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2619 GetCellInset(cell)->ascii(buf, sstr, 0);
2620 int len = int(sstr.str().length());
2621 int const n = cells_in_multicolumn(cell);
2622 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2624 if (len > int(clen[j + n - 1]))
2625 clen[j + n - 1] = len;
2629 for (int i = 0; i < rows_; ++i) {
2630 AsciiTopHLine(os, i, clen);
2631 for (int j = 0; j < columns_; ++j) {
2632 if (IsPartOfMultiColumn(i,j))
2634 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2638 AsciiBottomHLine(os, i, clen);
2644 InsetText * LyXTabular::GetCellInset(int cell) const
2647 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2651 InsetText * LyXTabular::GetCellInset(int row, int column) const
2653 cur_cell = GetCellNumber(row, column);
2654 return & cell_info[row][column].inset;
2658 void LyXTabular::Validate(LaTeXFeatures & features) const
2660 if (IsLongTabular())
2661 features.require("longtable");
2663 features.require("rotating");
2664 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2665 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2666 features.require("array");
2667 GetCellInset(cell)->validate(features);
2672 std::vector<string> const LyXTabular::getLabelList() const
2674 std::vector<string> label_list;
2675 for (int i = 0; i < rows_; ++i)
2676 for (int j = 0; j < columns_; ++j) {
2677 std::vector<string> const l =
2678 GetCellInset(i, j)->getLabelList();
2679 label_list.insert(label_list.end(),
2680 l.begin(), l.end());
2686 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2688 Paragraph * par = GetCellInset(cell)->paragraph();
2690 for (; par; par = par->next()) {
2691 for (int i = 0; i < par->size(); ++i) {
2692 if (par->getChar(i) == Paragraph::META_NEWLINE)