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()
105 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
109 Init(rows_arg, columns_arg);
113 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
118 Init(lt.rows_, lt.columns_, <);
119 // we really should change again to have InsetText as a pointer
120 // and allocate it then we would not have to do this stuff all
123 for (int i = 0; i < rows_; ++i) {
124 for (int j = 0; j < columns_; ++j) {
125 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
126 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
132 #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)
139 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
147 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
150 #warning This while method should look like this: (Lgb)
155 // If this and lt is not of the same size we have a serious bug
156 // So then it is ok to throw an exception, or for now
158 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
160 cell_info = lt.cell_info;
161 row_info = lt.row_info;
162 column_info = lt.column_info;
164 // long tabular stuff
165 SetLongTabular(lt.is_long_tabular);
166 endhead = lt.endhead;
167 endfoot = lt.endfoot;
168 endfirsthead = lt.endfirsthead;
169 endlastfoot = lt.endlastfoot;
179 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
181 LyXTabular * result = new LyXTabular(inset, *this, same_id);
183 // don't know if this is good but I need to Clone also
184 // the text-insets here, this is for the Undo-facility!
185 for (int i = 0; i < rows_; ++i) {
186 for (int j = 0; j < columns_; ++j) {
187 result->cell_info[i][j].inset = cell_info[i][j].inset;
188 result->cell_info[i][j].inset.setOwner(inset);
196 /* activates all lines and sets all widths to 0 */
197 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
200 columns_ = columns_arg;
201 row_info = row_vector(rows_, rowstruct());
202 column_info = column_vector(columns_, columnstruct());
203 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
211 for (int i = 0; i < rows_; ++i) {
212 for (int j = 0; j < columns_; ++j) {
213 cell_info[i][j].inset.setOwner(owner_);
214 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
215 cell_info[i][j].cellno = cellno++;
217 cell_info[i].back().right_line = true;
219 row_info.back().bottom_line = true;
220 row_info.front().bottom_line = true;
222 for (int i = 0; i < columns_; ++i) {
223 calculate_width_of_column(i);
225 column_info.back().right_line = true;
227 calculate_width_of_tabular();
229 rowofcell = vector<int>();
230 columnofcell = vector<int>();
231 set_row_column_number_info();
232 is_long_tabular = false;
235 endfirsthead.row = 0;
241 void LyXTabular::AppendRow(int cell)
245 int row = row_of_cell(cell);
247 row_vector::iterator rit = row_info.begin() + row;
248 row_info.insert(rit, rowstruct());
249 // now set the values of the row before
250 row_info[row] = row_info[row+1];
253 cell_vvector::iterator cit = cell_info.begin() + row;
254 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
256 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
259 for (int i = 0; i <= row; ++i) {
260 for (int j = 0; j < columns_; ++j) {
261 c_info[i][j] = cell_info[i][j];
264 for (int i = row + 1; i < rows_; ++i) {
265 for (int j = 0; j < columns_; ++j) {
266 c_info[i][j] = cell_info[i-1][j];
271 for (int j = 0; j < columns_; ++j) {
272 cell_info[row][j].inset.clear();
279 void LyXTabular::DeleteRow(int row)
281 if (rows_ == 1) return; // Not allowed to delete last row
283 row_info.erase(row_info.begin() + row); //&row_info[row]);
284 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
290 void LyXTabular::AppendColumn(int cell)
294 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
296 int const column = column_of_cell(cell);
297 column_vector::iterator cit = column_info.begin() + column + 1;
298 column_info.insert(cit, columnstruct());
299 // set the column values of the column before
300 column_info[column+1] = column_info[column];
302 for (int i = 0; i < rows_; ++i) {
303 for (int j = 0; j <= column; ++j) {
304 c_info[i][j] = cell_info[i][j];
306 for (int j = column + 1; j < columns_; ++j) {
307 c_info[i][j] = cell_info[i][j - 1];
309 // care about multicolumns
310 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
311 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
313 if ((column + 1) == columns_ ||
314 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
315 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
320 for (int i = 0; i < rows_; ++i) {
321 //cell_info[i][column].inset.clear();
322 cell_info[i][column + 1].inset.clear();
328 void LyXTabular::DeleteColumn(int column)
330 // Similar to DeleteRow
331 //if (!(columns_ - 1))
333 if (columns_ == 1) return; // Not allowed to delete last column
335 column_info.erase(column_info.begin() + column);
336 for (int i = 0; i < rows_; ++i) {
337 cell_info[i].erase(cell_info[i].begin() + column);
344 void LyXTabular::reinit()
350 void LyXTabular::Reinit(bool reset_widths)
353 for (int i = 0; i < rows_; ++i) {
354 for (int j = 0; j < columns_; ++j) {
355 cell_info[i][j].width_of_cell = 0;
356 cell_info[i][j].inset.setOwner(owner_);
361 for (int i = 0; i < columns_; ++i) {
362 calculate_width_of_column(i);
364 calculate_width_of_tabular();
366 set_row_column_number_info();
370 void LyXTabular::set_row_column_number_info(bool oldformat)
373 for (int row = 0; row < rows_; ++row) {
374 for (int column = 0; column<columns_; ++column) {
375 if (cell_info[row][column].multicolumn
376 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
378 cell_info[row][column].cellno = numberofcells;
381 ++numberofcells; // because this is one more than as we start from 0
383 rowofcell.resize(numberofcells);
384 columnofcell.resize(numberofcells);
386 for (int row = 0, column = 0, c = 0;
387 c < numberofcells && row < rows_ && column < columns_;) {
389 columnofcell[c] = column;
393 } while (column < columns_ &&
394 cell_info[row][column].multicolumn
395 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
396 if (column == columns_) {
402 for (int row = 0; row < rows_; ++row) {
403 for (int column = 0; column < columns_; ++column) {
404 if (IsPartOfMultiColumn(row,column))
406 // now set the right line of multicolumns right for oldformat read
408 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
410 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
411 cell_info[row][column].right_line =
412 cell_info[row][column+cn-1].right_line;
414 cell_info[row][column].inset.setAutoBreakRows(
415 !GetPWidth(GetCellNumber(row, column)).empty());
421 int LyXTabular::GetNumberOfCells() const
423 return numberofcells;
427 int LyXTabular::NumberOfCellsInRow(int cell) const
429 int const row = row_of_cell(cell);
431 for (int i = 0; i < columns_; ++i) {
432 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
439 /* returns 1 if there is a topline, returns 0 if not */
440 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
442 int const row = row_of_cell(cell);
444 if (!onlycolumn && IsMultiColumn(cell))
445 return cellinfo_of_cell(cell)->top_line;
446 return row_info[row].top_line;
450 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
452 // no bottom line underneath non-existent cells if you please
453 // Isn't that a programming error? Is so this should
454 // be an Assert instead. (Lgb)
455 if (cell >= numberofcells)
458 if (!onlycolumn && IsMultiColumn(cell))
459 return cellinfo_of_cell(cell)->bottom_line;
460 return row_info[row_of_cell(cell)].bottom_line;
464 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
466 if (!onlycolumn && IsMultiColumn(cell)) {
467 #ifdef SPECIAL_COLUM_HANDLING
468 if (cellinfo_of_cell(cell)->align_special.empty())
469 return cellinfo_of_cell(cell)->left_line;
470 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
472 return cellinfo_of_cell(cell)->left_line;
475 #ifdef SPECIAL_COLUM_HANDLING
476 if (column_info[column_of_cell(cell)].align_special.empty())
477 return column_info[column_of_cell(cell)].left_line;
478 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
480 return column_info[column_of_cell(cell)].left_line;
485 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
487 if (!onlycolumn && IsMultiColumn(cell)) {
488 #ifdef SPECIAL_COLUM_HANDLING
489 if (cellinfo_of_cell(cell)->align_special.empty())
490 return cellinfo_of_cell(cell)->right_line;
491 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
493 return cellinfo_of_cell(cell)->right_line;
496 #ifdef SPECIAL_COLUM_HANDLING
497 if (column_info[column_of_cell(cell)].align_special.empty())
498 return column_info[right_column_of_cell(cell)].right_line;
499 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
501 return column_info[right_column_of_cell(cell)].right_line;
506 bool LyXTabular::TopAlreadyDrawed(int cell) const
508 int row = row_of_cell(cell);
509 if ((row > 0) && !GetAdditionalHeight(row)) {
510 int column = column_of_cell(cell);
513 && cell_info[row][column].multicolumn
514 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
516 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
517 return row_info[row].bottom_line;
519 return cell_info[row][column].bottom_line;
525 bool LyXTabular::LeftAlreadyDrawed(int cell) const
527 int column = column_of_cell(cell);
529 int row = row_of_cell(cell);
531 (cell_info[row][column].multicolumn ==
532 LyXTabular::CELL_PART_OF_MULTICOLUMN));
533 if (GetAdditionalWidth(cell_info[row][column].cellno))
535 #ifdef SPECIAL_COLUM_HANDLING
536 return column_info[column].right_line;
538 return RightLine(cell_info[row][column].cellno, true);
545 bool LyXTabular::IsLastRow(int cell) const
547 return (row_of_cell(cell) == rows_ - 1);
551 int LyXTabular::GetAdditionalHeight(int row) const
553 if (!row || row >= rows_)
559 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
560 switch (cell_info[row - 1][column].multicolumn) {
561 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
562 bottom = cell_info[row - 1][column].bottom_line;
564 case LyXTabular::CELL_NORMAL:
565 bottom = row_info[row - 1].bottom_line;
568 for (int column = 0; column < columns_ - 1 && top; ++column) {
569 switch (cell_info[row][column].multicolumn){
570 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
571 top = cell_info[row][column].top_line;
573 case LyXTabular::CELL_NORMAL:
574 top = row_info[row].top_line;
578 return WIDTH_OF_LINE;
583 int LyXTabular::GetAdditionalWidth(int cell) const
585 // internally already set in SetWidthOfCell
586 // used to get it back in text.C
587 int const col = right_column_of_cell(cell);
588 int const row = row_of_cell(cell);
589 if (col < columns_ - 1 && RightLine(cell, true) &&
590 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
592 return WIDTH_OF_LINE;
599 // returns the maximum over all rows
600 int LyXTabular::GetWidthOfColumn(int cell) const
602 int const column1 = column_of_cell(cell);
603 int const column2 = right_column_of_cell(cell);
605 for (int i = column1; i <= column2; ++i) {
606 result += column_info[i].width_of_column;
612 int LyXTabular::GetWidthOfTabular() const
614 return width_of_tabular;
618 /* returns 1 if a complete update is necessary, otherwise 0 */
619 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
621 if (!IsMultiColumn(cell))
624 int const row = row_of_cell(cell);
625 int const column1 = column_of_cell(cell);
626 int const column2 = right_column_of_cell(cell);
628 // first set columns to 0 so we can calculate the right width
629 for (int i = column1; i <= column2; ++i) {
630 cell_info[row][i].width_of_cell = 0;
632 // set the width to MAX_WIDTH until width > 0
633 int width = (new_width + 2 * WIDTH_OF_LINE);
636 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
637 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
638 width -= column_info[i].width_of_column;
641 cell_info[row][i].width_of_cell = width;
647 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
649 int const row = row_of_cell(cell);
650 int const column1 = column_of_cell(cell);
651 int const column2 = right_column_of_cell(cell);
653 // first set columns to 0 so we can calculate the right width
655 for (; i <= column2; ++i)
656 cell_info[row][i].width_of_cell = 0;
657 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
659 if (i < numberofcells)
660 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
661 SetWidthOfMulticolCell(cell, new_width);
665 /* returns 1 if a complete update is necessary, otherwise 0 */
666 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
668 int const row = row_of_cell(cell);
669 int const column1 = column_of_cell(cell);
674 #ifdef SPECIAL_COLUM_HANDLING
675 if (RightLine(cell_info[row][column1].cellno, true) &&
676 (column1 < columns_-1) &&
677 LeftLine(cell_info[row][column1+1].cellno, true))
679 if (column_info[column1].right_line && (column1 < columns_-1) &&
680 column_info[column1+1].left_line) // additional width
684 add_width = WIDTH_OF_LINE;
686 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
689 if (IsMultiColumn(cell, true)) {
690 tmp = SetWidthOfMulticolCell(cell, new_width);
692 width = (new_width + 2*WIDTH_OF_LINE + add_width);
693 cell_info[row][column1].width_of_cell = width;
694 tmp = calculate_width_of_column_NMC(column1);
698 for (; i<columns_; ++i)
699 calculate_width_of_column_NMC(i);
700 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
702 if (i < numberofcells)
703 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
704 for (i = 0; i < columns_; ++i)
705 calculate_width_of_column(i);
706 calculate_width_of_tabular();
713 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
715 if (!IsMultiColumn(cell) || onlycolumn)
716 column_info[column_of_cell(cell)].alignment = align;
718 cellinfo_of_cell(cell)->alignment = align;
723 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
725 if (!IsMultiColumn(cell) || onlycolumn)
726 column_info[column_of_cell(cell)].valignment = align;
728 cellinfo_of_cell(cell)->valignment = align;
733 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
735 bool flag = !width.empty();
736 int const j = column_of_cell(cell);
738 column_info[j].p_width = width;
739 if (flag) // do this only if there is a width
740 SetAlignment(cell, LYX_ALIGN_LEFT);
741 for (int i = 0; i < rows_; ++i) {
742 int c = GetCellNumber(i, j);
743 flag = !GetPWidth(c).empty(); // because of multicolumns!
744 GetCellInset(c)->setAutoBreakRows(flag);
750 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
752 bool const flag = !width.empty();
754 cellinfo_of_cell(cell)->p_width = width;
755 if (IsMultiColumn(cell)) {
756 GetCellInset(cell)->setAutoBreakRows(flag);
763 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
764 LyXTabular::Feature what)
766 if (what == SET_SPECIAL_MULTI)
767 cellinfo_of_cell(cell)->align_special = special;
769 column_info[column_of_cell(cell)].align_special = special;
774 bool LyXTabular::SetAllLines(int cell, bool line)
776 SetTopLine(cell, line);
777 SetBottomLine(cell, line);
778 SetRightLine(cell, line);
779 SetLeftLine(cell, line);
784 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
786 int const row = row_of_cell(cell);
788 if (onlycolumn || !IsMultiColumn(cell))
789 row_info[row].top_line = line;
791 cellinfo_of_cell(cell)->top_line = line;
796 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
798 if (onlycolumn || !IsMultiColumn(cell))
799 row_info[row_of_cell(cell)].bottom_line = line;
801 cellinfo_of_cell(cell)->bottom_line = line;
806 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
808 if (onlycolumn || !IsMultiColumn(cell))
809 column_info[column_of_cell(cell)].left_line = line;
811 cellinfo_of_cell(cell)->left_line = line;
816 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
818 if (onlycolumn || !IsMultiColumn(cell))
819 column_info[right_column_of_cell(cell)].right_line = line;
821 cellinfo_of_cell(cell)->right_line = line;
826 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
828 if (!onlycolumn && IsMultiColumn(cell))
829 return cellinfo_of_cell(cell)->alignment;
831 return column_info[column_of_cell(cell)].alignment;
835 LyXTabular::VAlignment
836 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
838 if (!onlycolumn && IsMultiColumn(cell))
839 return cellinfo_of_cell(cell)->valignment;
841 return column_info[column_of_cell(cell)].valignment;
845 string const LyXTabular::GetPWidth(int cell) const
847 if (IsMultiColumn(cell))
848 return cellinfo_of_cell(cell)->p_width;
849 return column_info[column_of_cell(cell)].p_width;
853 string const LyXTabular::GetColumnPWidth(int cell) const
855 return column_info[column_of_cell(cell)].p_width;
859 string const LyXTabular::GetMColumnPWidth(int cell) const
861 if (IsMultiColumn(cell))
862 return cellinfo_of_cell(cell)->p_width;
867 string const LyXTabular::GetAlignSpecial(int cell, int what) const
869 if (what == SET_SPECIAL_MULTI)
870 return cellinfo_of_cell(cell)->align_special;
871 return column_info[column_of_cell(cell)].align_special;
875 int LyXTabular::GetWidthOfCell(int cell) const
877 int const row = row_of_cell(cell);
878 int const column1 = column_of_cell(cell);
879 int const column2 = right_column_of_cell(cell);
881 for (int i = column1; i <= column2; ++i) {
882 result += cell_info[row][i].width_of_cell;
888 int LyXTabular::GetBeginningOfTextInCell(int cell) const
892 switch (GetAlignment(cell)){
893 case LYX_ALIGN_CENTER:
894 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
896 case LYX_ALIGN_RIGHT:
897 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
898 // + GetAdditionalWidth(cell);
900 default: /* LYX_ALIGN_LEFT: nothing :-) */
910 bool LyXTabular::IsFirstCellInRow(int cell) const
912 return column_of_cell(cell) == 0;
916 int LyXTabular::GetFirstCellInRow(int row) const
920 return cell_info[row][0].cellno;
923 bool LyXTabular::IsLastCellInRow(int cell) const
925 return (right_column_of_cell(cell) == (columns_ - 1));
929 int LyXTabular::GetLastCellInRow(int row) const
933 return cell_info[row][columns_-1].cellno;
937 bool LyXTabular::calculate_width_of_column(int column)
939 int const old_column_width = column_info[column].width_of_column;
942 for (int i = 0; i < rows_; ++i) {
943 maximum = max(cell_info[i][column].width_of_cell, maximum);
945 column_info[column].width_of_column = maximum;
946 return (column_info[column].width_of_column != old_column_width);
951 // calculate the with of the column without regarding REAL MultiColumn
952 // cells. This means MultiColumn-cells spanning more than 1 column.
954 bool LyXTabular::calculate_width_of_column_NMC(int column)
956 int const old_column_width = column_info[column].width_of_column;
958 for (int i = 0; i < rows_; ++i) {
959 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
960 (cell_info[i][column].width_of_cell > max)) {
961 max = cell_info[i][column].width_of_cell;
964 column_info[column].width_of_column = max;
965 return (column_info[column].width_of_column != old_column_width);
969 void LyXTabular::calculate_width_of_tabular()
971 width_of_tabular = 0;
972 for (int i = 0; i < columns_; ++i) {
973 width_of_tabular += column_info[i].width_of_column;
978 int LyXTabular::row_of_cell(int cell) const
980 if (cell >= numberofcells)
984 return rowofcell[cell];
988 int LyXTabular::column_of_cell(int cell) const
990 if (cell >= numberofcells)
994 return columnofcell[cell];
998 int LyXTabular::right_column_of_cell(int cell) const
1000 int const row = row_of_cell(cell);
1001 int column = column_of_cell(cell);
1002 while (column < (columns_ - 1) &&
1003 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1009 // Perfect case for a template... (Lgb)
1010 // or perhaps not...
1013 string const write_attribute(string const & name, T const & t)
1015 string str = " " + name + "=\"" + tostr(t) + "\"";
1020 string const write_attribute(string const & name, bool const & b)
1022 return write_attribute(name, int(b));
1027 string const write_attribute(string const & name, int value)
1029 string str = " " + name + "=\"" + tostr(value) + "\"";
1034 string const write_attribute(string const & name, string const & value)
1036 string str = " " + name + "=\"" + value + "\"";
1041 string const write_attribute(string const & name, bool value)
1043 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1051 string const tostr(LyXAlignment const & num)
1054 case LYX_ALIGN_NONE:
1056 case LYX_ALIGN_BLOCK:
1058 case LYX_ALIGN_LEFT:
1060 case LYX_ALIGN_CENTER:
1062 case LYX_ALIGN_RIGHT:
1064 case LYX_ALIGN_LAYOUT:
1066 case LYX_ALIGN_SPECIAL:
1075 string const tostr(LyXTabular::VAlignment const & num)
1078 case LyXTabular::LYX_VALIGN_TOP:
1080 case LyXTabular::LYX_VALIGN_CENTER:
1082 case LyXTabular::LYX_VALIGN_BOTTOM:
1091 string const tostr(LyXTabular::BoxType const & num)
1094 case LyXTabular::BOX_NONE:
1096 case LyXTabular::BOX_PARBOX:
1098 case LyXTabular::BOX_MINIPAGE:
1105 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1109 << write_attribute("version", 2)
1110 << write_attribute("rows", rows_)
1111 << write_attribute("columns", columns_)
1113 // global longtable options
1115 << write_attribute("rotate", tostr(rotate))
1116 << write_attribute("islongtable", tostr(is_long_tabular))
1117 << write_attribute("endhead", endhead.row)
1118 << write_attribute("endfirsthead", endfirsthead.row)
1119 << write_attribute("endfoot", endfoot.row)
1120 << write_attribute("endlastfoot", endlastfoot.row)
1122 for (int j = 0; j < columns_; ++j) {
1124 << write_attribute("alignment", tostr(column_info[j].alignment))
1125 << write_attribute("valignment", tostr(column_info[j].valignment))
1126 << write_attribute("leftline", tostr(column_info[j].left_line))
1127 << write_attribute("rightline", tostr(column_info[j].right_line))
1128 << write_attribute("width",
1129 VSpace(column_info[j].p_width)
1131 << write_attribute("special", column_info[j].align_special)
1134 for (int i = 0; i < rows_; ++i) {
1136 << write_attribute("topline", tostr(row_info[i].top_line))
1137 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1138 << write_attribute("newpage", tostr(row_info[i].newpage))
1140 for (int j = 0; j < columns_; ++j) {
1142 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1143 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1144 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1145 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1146 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1147 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1148 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1149 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1150 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1151 << write_attribute("width", cell_info[i][j].p_width)
1152 << write_attribute("special", cell_info[i][j].align_special)
1154 os << "\\begin_inset ";
1155 cell_info[i][j].inset.write(buf, os);
1156 os << "\n\\end_inset \n"
1161 os << "</lyxtabular>\n";
1167 // I would have liked a fromstr template a lot better. (Lgb)
1170 bool string2type(string const str, LyXAlignment & num)
1173 num = LYX_ALIGN_NONE;
1174 else if (str == "block")
1175 num = LYX_ALIGN_BLOCK;
1176 else if (str == "left")
1177 num = LYX_ALIGN_LEFT;
1178 else if (str == "center")
1179 num = LYX_ALIGN_CENTER;
1180 else if (str == "right")
1181 num = LYX_ALIGN_RIGHT;
1189 bool string2type(string const str, LyXTabular::VAlignment & num)
1192 num = LyXTabular::LYX_VALIGN_TOP;
1193 else if (str == "center")
1194 num = LyXTabular::LYX_VALIGN_CENTER;
1195 else if (str == "bottom")
1196 num = LyXTabular::LYX_VALIGN_BOTTOM;
1204 bool string2type(string const str, LyXTabular::BoxType & num)
1207 num = LyXTabular::BOX_NONE;
1208 else if (str == "parbox")
1209 num = LyXTabular::BOX_PARBOX;
1210 else if (str == "minipage")
1211 num = LyXTabular::BOX_MINIPAGE;
1219 bool string2type(string const str, bool & num)
1223 else if (str == "false")
1231 bool getTokenValue(string const & str, const char * token, string & ret)
1233 size_t token_length = strlen(token);
1234 string::size_type pos = str.find(token);
1236 if (pos == string::npos || pos + token_length + 1 >= str.length()
1237 || str[pos + token_length] != '=')
1240 pos += token_length + 1;
1242 if ((ch != '"') && (ch != '\'')) { // only read till next space
1246 while ((pos < str.length() - 1) && (str[++pos] != ch))
1253 bool getTokenValue(string const & str, const char * token, int & num)
1256 if (!getTokenValue(str, token, tmp))
1258 num = strToInt(tmp);
1263 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1266 if (!getTokenValue(str, token, tmp))
1268 return string2type(tmp, num);
1272 bool getTokenValue(string const & str, const char * token,
1273 LyXTabular::VAlignment & num)
1276 if (!getTokenValue(str, token, tmp))
1278 return string2type(tmp, num);
1282 bool getTokenValue(string const & str, const char * token,
1283 LyXTabular::BoxType & num)
1286 if (!getTokenValue(str, token, tmp))
1288 return string2type(tmp, num);
1292 bool getTokenValue(string const & str, const char * token, bool & flag)
1295 if (!getTokenValue(str, token, tmp))
1297 return string2type(tmp, flag);
1302 void l_getline(istream & is, string & str)
1305 while (str.empty()) {
1307 if (!str.empty() && str[str.length() - 1] == '\r')
1308 str.erase(str.length() - 1);
1315 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1318 istream & is = lex.getStream();
1320 l_getline(is, line);
1321 if (!prefixIs(line, "<lyxtabular ")
1322 && !prefixIs(line, "<LyXTabular ")) {
1323 OldFormatRead(lex, line);
1328 if (!getTokenValue(line, "version", version))
1331 ReadOld(buf, is, lex, line);
1332 else if (version == 2)
1333 ReadNew(buf, is, lex, line);
1337 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1338 LyXLex & lex, string const & l)
1342 if (!getTokenValue(line, "rows", rows_arg))
1345 if (!getTokenValue(line, "columns", columns_arg))
1347 Init(rows_arg, columns_arg);
1348 l_getline(is, line);
1349 if (!prefixIs(line, "<features")) {
1350 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1351 line << ")" << endl;
1354 getTokenValue(line, "rotate", rotate);
1355 getTokenValue(line, "islongtable", is_long_tabular);
1356 getTokenValue(line, "endhead", endhead.row);
1357 getTokenValue(line, "endfirsthead", endfirsthead.row);
1358 getTokenValue(line, "endfoot", endfoot.row);
1359 getTokenValue(line, "endlastfoot", endlastfoot.row);
1360 endhead.row = abs(endhead.row);
1361 endfirsthead.row = abs(endfirsthead.row);
1362 endfoot.row = abs(endfoot.row);
1363 endlastfoot.row = abs(endlastfoot.row);
1364 for (int j = 0; j < columns_; ++j) {
1366 if (!prefixIs(line,"<column")) {
1367 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1368 line << ")" << endl;
1371 getTokenValue(line, "alignment", column_info[j].alignment);
1372 getTokenValue(line, "valignment", column_info[j].valignment);
1373 getTokenValue(line, "leftline", column_info[j].left_line);
1374 getTokenValue(line, "rightline", column_info[j].right_line);
1375 getTokenValue(line, "width", column_info[j].p_width);
1376 getTokenValue(line, "special", column_info[j].align_special);
1379 for (int i = 0; i < rows_; ++i) {
1380 l_getline(is, line);
1381 if (!prefixIs(line, "<row")) {
1382 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1383 line << ")" << endl;
1386 getTokenValue(line, "topline", row_info[i].top_line);
1387 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1388 getTokenValue(line, "newpage", row_info[i].newpage);
1389 for (int j = 0; j < columns_; ++j) {
1390 l_getline(is, line);
1391 if (!prefixIs(line, "<cell")) {
1392 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1393 line << ")" << endl;
1396 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1397 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1398 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1399 getTokenValue(line, "topline", cell_info[i][j].top_line);
1400 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1401 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1402 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1403 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1404 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1405 getTokenValue(line, "width", cell_info[i][j].p_width);
1406 getTokenValue(line, "special", cell_info[i][j].align_special);
1407 l_getline(is, line);
1408 if (prefixIs(line, "\\begin_inset")) {
1409 cell_info[i][j].inset.read(buf, lex);
1410 l_getline(is, line);
1412 if (!prefixIs(line, "</cell>")) {
1413 lyxerr << "Wrong tabular format (expected </cell> got" <<
1414 line << ")" << endl;
1418 l_getline(is, line);
1419 if (!prefixIs(line, "</row>")) {
1420 lyxerr << "Wrong tabular format (expected </row> got" <<
1421 line << ")" << endl;
1425 while (!prefixIs(line, "</lyxtabular>")) {
1426 l_getline(is, line);
1428 set_row_column_number_info();
1432 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1438 int columns_arg = 0;
1439 int is_long_tabular_arg = false;
1440 int rotate_arg = false;
1449 istream & is = lex.getStream();
1452 version = lyx::atoi(s.substr(8, string::npos));
1456 vector<int> cont_row_info;
1459 lyxerr << "Tabular format < 5 is not supported anymore\n"
1460 "Get an older version of LyX (< 1.1.x) for conversion!"
1462 Alert::alert(_("Warning:"),
1463 _("Tabular format < 5 is not supported anymore\n"),
1464 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1466 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1467 >> rotate_arg >> a >> b >> c >> d;
1469 is >> rows_arg >> columns_arg;
1470 Init(rows_arg, columns_arg);
1471 cont_row_info = vector<int>(rows_arg);
1472 SetLongTabular(is_long_tabular_arg);
1473 SetRotateTabular(rotate_arg);
1475 for (i = 0; i < rows_; ++i) {
1477 cont_row_info[i] = false;
1479 for (i = 0; i < columns_; ++i) {
1482 for (i = 0; i < rows_; ++i) {
1483 for (j = 0; j < columns_; ++j) {
1488 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1489 >> rotate_arg >> a >> b >> c >> d;
1490 Init(rows_arg, columns_arg);
1491 cont_row_info = vector<int>(rows_arg);
1492 SetLongTabular(is_long_tabular_arg);
1493 SetRotateTabular(rotate_arg);
1494 endhead.row = a + 1;
1495 endfirsthead.row = b + 1;
1496 endfoot.row = c + 1;
1497 endlastfoot.row = d + 1;
1498 for (i = 0; i < rows_; ++i) {
1499 a = b = c = d = e = f = g = 0;
1500 is >> a >> b >> c >> d;
1501 row_info[i].top_line = a;
1502 row_info[i].bottom_line = b;
1503 cont_row_info[i] = c;
1504 row_info[i].newpage = d;
1506 for (i = 0; i < columns_; ++i) {
1511 char ch; // skip '"'
1514 // ignore is buggy but we will use it later (Lgb)
1515 is.ignore(); // skip '"'
1517 getline(is, s1, '"');
1519 is >> ch; // skip '"'
1521 // ignore is buggy but we will use it later (Lgb)
1522 is.ignore(); // skip '"'
1524 getline(is, s2, '"');
1525 column_info[i].alignment = static_cast<LyXAlignment>(a);
1526 column_info[i].left_line = b;
1527 column_info[i].right_line = c;
1528 column_info[i].p_width = s1;
1529 column_info[i].align_special = s2;
1531 for (i = 0; i < rows_; ++i) {
1532 for (j = 0; j < columns_; ++j) {
1535 is >> a >> b >> c >> d >> e >> f >> g;
1538 is >> ch; // skip '"'
1540 // ignore is buggy but we will use it later (Lgb)
1541 is.ignore(); // skip '"'
1543 getline(is, s1, '"');
1545 is >> ch; // skip '"'
1547 // ignore is buggy but we will use it later (Lgb)
1548 is.ignore(); // skip '"'
1550 getline(is, s2, '"');
1551 cell_info[i][j].multicolumn = static_cast<char>(a);
1552 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1553 cell_info[i][j].top_line = static_cast<char>(c);
1554 cell_info[i][j].bottom_line = static_cast<char>(d);
1555 cell_info[i][j].left_line = column_info[j].left_line;
1556 cell_info[i][j].right_line = column_info[j].right_line;
1557 cell_info[i][j].rotate = static_cast<bool>(f);
1558 cell_info[i][j].usebox = static_cast<BoxType>(g);
1559 cell_info[i][j].align_special = s1;
1560 cell_info[i][j].p_width = s2;
1564 set_row_column_number_info(true);
1566 Paragraph * par = new Paragraph;
1567 Paragraph * return_par = 0;
1571 Paragraph::depth_type depth = 0;
1572 LyXFont font(LyXFont::ALL_INHERIT);
1573 font.setLanguage(owner_->bufferOwner()->getLanguage());
1575 while (lex.isOK()) {
1577 string const token = lex.getString();
1580 if (token == "\\layout"
1581 || token == "\\end_float"
1582 || token == "\\end_deeper") {
1583 lex.pushToken(token);
1584 #ifndef NO_COMPABILITY
1585 // Here we need to insert the inset_ert_contents into the last
1586 // cell of the tabular.
1587 owner_->bufferOwner()->insertErtContents(par, pos, font);
1591 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1596 lex.pushToken(token);
1600 lex.printError("New Paragraph allocated! This should not happen!");
1601 lex.pushToken(token);
1607 // now we have the par we should fill the insets with this!
1609 InsetText * inset = GetCellInset(cell);
1612 for (int i = 0; i < par->size(); ++i) {
1613 if (par->isNewline(i)) {
1615 if (cell > numberofcells) {
1616 lyxerr << "Some error in reading old table format occured!" <<
1617 endl << "Terminating when reading cell[" << cell << "]!" <<
1622 row = row_of_cell(cell);
1623 if (cont_row_info[row]) {
1625 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1626 while (!IsFirstCellInRow(--cell));
1628 inset = GetCellInset(cell);
1631 inset = GetCellInset(cell);
1632 row = row_of_cell(cell);
1633 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1635 // insert a space instead
1637 par->insertChar(i, ' ');
1640 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1641 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1648 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1650 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1651 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1655 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1657 int const row = row_of_cell(cell);
1658 int const column = column_of_cell(cell);
1659 return &cell_info[row][column];
1663 void LyXTabular::SetMultiColumn(int cell, int number)
1665 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1666 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1667 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1668 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1669 for (number--; number > 0; --number) {
1670 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1672 set_row_column_number_info();
1676 int LyXTabular::cells_in_multicolumn(int cell) const
1678 int const row = row_of_cell(cell);
1679 int column = column_of_cell(cell);
1682 while ((column < columns_) &&
1683 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1692 int LyXTabular::UnsetMultiColumn(int cell)
1694 int const row = row_of_cell(cell);
1695 int column = column_of_cell(cell);
1699 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1700 cell_info[row][column].multicolumn = CELL_NORMAL;
1702 while ((column < columns_) &&
1703 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1705 cell_info[row][column].multicolumn = CELL_NORMAL;
1710 set_row_column_number_info();
1715 void LyXTabular::SetLongTabular(bool what)
1717 is_long_tabular = what;
1721 bool LyXTabular::IsLongTabular() const
1723 return is_long_tabular;
1727 void LyXTabular::SetRotateTabular(bool flag)
1733 bool LyXTabular::GetRotateTabular() const
1739 void LyXTabular::SetRotateCell(int cell, bool flag)
1741 cellinfo_of_cell(cell)->rotate = flag;
1745 bool LyXTabular::GetRotateCell(int cell) const
1747 return cellinfo_of_cell(cell)->rotate;
1751 bool LyXTabular::NeedRotating() const
1755 for (int i = 0; i < rows_; ++i) {
1756 for (int j = 0; j < columns_; ++j) {
1757 if (cell_info[i][j].rotate)
1765 bool LyXTabular::IsLastCell(int cell) const
1767 if ((cell + 1) < numberofcells)
1773 int LyXTabular::GetCellAbove(int cell) const
1775 if (row_of_cell(cell) > 0)
1776 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1781 int LyXTabular::GetCellBelow(int cell) const
1783 if (row_of_cell(cell) + 1 < rows_)
1784 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1789 int LyXTabular::GetLastCellAbove(int cell) const
1791 if (row_of_cell(cell) <= 0)
1793 if (!IsMultiColumn(cell))
1794 return GetCellAbove(cell);
1795 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1799 int LyXTabular::GetLastCellBelow(int cell) const
1801 if (row_of_cell(cell) + 1 >= rows_)
1803 if (!IsMultiColumn(cell))
1804 return GetCellBelow(cell);
1805 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1809 int LyXTabular::GetCellNumber(int row, int column) const
1812 if (column >= columns_)
1813 column = columns_ - 1;
1814 else if (column < 0)
1821 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1823 return cell_info[row][column].cellno;
1827 void LyXTabular::SetUsebox(int cell, BoxType type)
1829 cellinfo_of_cell(cell)->usebox = type;
1833 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1835 if (column_info[column_of_cell(cell)].p_width.empty() &&
1836 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1838 if (cellinfo_of_cell(cell)->usebox > 1)
1839 return cellinfo_of_cell(cell)->usebox;
1840 return UseParbox(cell);
1843 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1845 if (!ltt.row || (ltt.row > rows_))
1847 return (row == (ltt.row - 1));
1851 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1861 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1864 return checkLTType(row, hd);
1868 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1871 return checkLTType(row, hd);
1875 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1885 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1888 return checkLTType(row, fd);
1892 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1895 return checkLTType(row, fd);
1899 void LyXTabular::SetLTNewPage(int cell, bool what)
1901 row_info[row_of_cell(cell)].newpage = what;
1905 bool LyXTabular::GetLTNewPage(int cell) const
1907 return row_info[row_of_cell(cell)].newpage;
1911 bool LyXTabular::SetAscentOfRow(int row, int height)
1913 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1915 row_info[row].ascent_of_row = height;
1920 bool LyXTabular::SetDescentOfRow(int row, int height)
1922 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1924 row_info[row].descent_of_row = height;
1929 int LyXTabular::GetAscentOfRow(int row) const
1933 return row_info[row].ascent_of_row;
1937 int LyXTabular::GetDescentOfRow(int row) const
1941 return row_info[row].descent_of_row;
1945 int LyXTabular::GetHeightOfTabular() const
1949 for (int row = 0; row < rows_; ++row)
1950 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1951 GetAdditionalHeight(row);
1956 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1958 if ((row >= rows_) || (column >= columns_))
1960 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1964 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1966 if ((row < 0) || (row >= rows_))
1969 int const fcell = GetFirstCellInRow(row);
1970 int const n = NumberOfCellsInRow(fcell) + fcell;
1973 for (int i = fcell; i < n; ++i) {
1977 if (tmp == (n - fcell)){
1980 for (int i = fcell; i < n; ++i) {
1983 << column_of_cell(i) + 1
1985 << right_column_of_cell(i) + 1
1997 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1999 if ((row < 0) || (row >= rows_))
2002 int const fcell = GetFirstCellInRow(row);
2003 int const n = NumberOfCellsInRow(fcell) + fcell;
2006 for (int i = fcell; i < n; ++i) {
2010 if (tmp == (n-fcell)){
2013 for (int i = fcell; i < n; ++i) {
2014 if (BottomLine(i)) {
2016 << column_of_cell(i) + 1
2018 << right_column_of_cell(i) + 1
2030 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2034 if (GetRotateCell(cell)) {
2035 os << "\\begin{sideways}\n";
2038 if (IsMultiColumn(cell)) {
2039 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2040 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2041 os << cellinfo_of_cell(cell)->align_special << "}{";
2043 if (LeftLine(cell) &&
2044 (IsFirstCellInRow(cell) ||
2045 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2046 !RightLine(cell-1, true))))
2050 if (!GetPWidth(cell).empty()) {
2051 switch (GetVAlignment(cell)) {
2052 case LYX_VALIGN_TOP:
2055 case LYX_VALIGN_CENTER:
2058 case LYX_VALIGN_BOTTOM:
2062 os << "{" << GetPWidth(cell) << '}';
2064 switch (GetAlignment(cell)) {
2065 case LYX_ALIGN_LEFT:
2068 case LYX_ALIGN_RIGHT:
2076 if (RightLine(cell))
2078 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2084 if (GetUsebox(cell) == BOX_PARBOX) {
2086 switch (GetVAlignment(cell)) {
2087 case LYX_VALIGN_TOP:
2090 case LYX_VALIGN_CENTER:
2093 case LYX_VALIGN_BOTTOM:
2097 os << "]{" << GetPWidth(cell) << "}{";
2098 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2099 os << "\\begin{minipage}[";
2100 switch (GetVAlignment(cell)) {
2101 case LYX_VALIGN_TOP:
2104 case LYX_VALIGN_CENTER:
2107 case LYX_VALIGN_BOTTOM:
2111 os << "]{" << GetPWidth(cell) << "}\n";
2118 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2123 if (GetUsebox(cell) == BOX_PARBOX)
2125 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2126 os << "%\n\\end{minipage}";
2129 if (IsMultiColumn(cell)){
2132 if (GetRotateCell(cell)) {
2133 os << "%\n\\end{sideways}";
2140 int LyXTabular::Latex(Buffer const * buf,
2141 ostream & os, bool fragile, bool fp) const
2146 //+---------------------------------------------------------------------
2147 //+ first the opening preamble +
2148 //+---------------------------------------------------------------------
2151 os << "\\begin{sideways}\n";
2154 if (is_long_tabular)
2155 os << "\\begin{longtable}{";
2157 os << "\\begin{tabular}{";
2158 for (int i = 0; i < columns_; ++i) {
2159 if (!column_info[i].align_special.empty()) {
2160 os << column_info[i].align_special;
2162 if (column_info[i].left_line)
2164 if (!column_info[i].p_width.empty()) {
2165 switch (column_info[i].valignment) {
2166 case LYX_VALIGN_TOP:
2169 case LYX_VALIGN_CENTER:
2172 case LYX_VALIGN_BOTTOM:
2177 << column_info[i].p_width
2180 switch (column_info[i].alignment) {
2181 case LYX_ALIGN_LEFT:
2184 case LYX_ALIGN_RIGHT:
2192 if (column_info[i].right_line)
2199 //+---------------------------------------------------------------------
2200 //+ the single row and columns (cells) +
2201 //+---------------------------------------------------------------------
2203 for (int i = 0; i < rows_; ++i) {
2204 ret += TeXTopHLine(os, i);
2205 #warning Implement top double lines for LT Header/Footers
2208 ret += TeXBottomHLine(os, i-1);
2209 ret += TeXTopHLine(os, i);
2212 for (int j = 0; j < columns_; ++j) {
2213 if (IsPartOfMultiColumn(i,j))
2215 ret += TeXCellPreamble(os, cell);
2216 InsetText * inset = GetCellInset(cell);
2218 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2219 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2223 ret += inset->latex(buf, os, fragile, fp);
2227 ret += TeXCellPostamble(os, cell);
2228 if (!IsLastCellInRow(cell)) { // not last cell in row
2236 ret += TeXBottomHLine(os, i);
2237 if (IsLongTabular()) {
2238 if (i == (endhead.row - 1)) {
2239 if (endhead.bottomDL)
2240 ret += TeXBottomHLine(os, i);
2241 os << "\\endhead\n";
2244 if (i == (endfirsthead.row - 1)) {
2245 if (endfirsthead.bottomDL)
2246 ret += TeXBottomHLine(os, i);
2247 os << "\\endfirsthead\n";
2250 if (i == (endfoot.row - 1)) {
2251 if (endfoot.bottomDL)
2252 ret += TeXBottomHLine(os, i);
2253 os << "\\endfoot\n";
2256 if (i == (endlastfoot.row - 1)) {
2257 if (endlastfoot.bottomDL)
2258 ret += TeXBottomHLine(os, i);
2259 os << "\\endlastfoot\n";
2262 if (row_info[i].newpage) {
2263 os << "\\newpage\n";
2269 //+---------------------------------------------------------------------
2270 //+ the closing of the tabular +
2271 //+---------------------------------------------------------------------
2273 if (is_long_tabular)
2274 os << "\\end{longtable}";
2276 os << "\\end{tabular}";
2278 os << "\n\\end{sideways}";
2286 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2289 int cell = GetFirstCellInRow(row);
2292 for (int j = 0; j < columns_; ++j) {
2293 if (IsPartOfMultiColumn(row, j))
2296 os << "<entry align=\"";
2297 switch (GetAlignment(cell)) {
2298 case LYX_ALIGN_LEFT:
2301 case LYX_ALIGN_RIGHT:
2309 os << "\" valign=\"";
2310 switch (GetVAlignment(cell)) {
2311 case LYX_VALIGN_TOP:
2314 case LYX_VALIGN_BOTTOM:
2317 case LYX_VALIGN_CENTER:
2322 if (IsMultiColumn(cell)) {
2323 os << " namest=\"col" << j << "\" ";
2324 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2328 ret += GetCellInset(cell)->docbook(buf, os);
2337 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2341 //+---------------------------------------------------------------------
2342 //+ first the opening preamble +
2343 //+---------------------------------------------------------------------
2345 os << "<tgroup cols=\"" << columns_
2346 << "\" colsep=\"1\" rowsep=\"1\">\n";
2348 for (int i = 0; i < columns_; ++i) {
2349 os << "<colspec colname=\"col" << i << "\" align=\"";
2350 switch (column_info[i].alignment) {
2351 case LYX_ALIGN_LEFT:
2354 case LYX_ALIGN_RIGHT:
2365 //+---------------------------------------------------------------------
2366 //+ Long Tabular case +
2367 //+---------------------------------------------------------------------
2369 if ( IsLongTabular() ) {
2371 if( endhead.row || endfirsthead.row ) {
2373 if( endfirsthead.row ) {
2374 docbookRow( buf, os, endfirsthead.row - 1);
2376 if( endhead.row && endhead.row != endfirsthead.row) {
2377 docbookRow(buf, os, endhead.row - 1);
2383 if( endfoot.row || endlastfoot.row ) {
2386 docbookRow( buf, os, endfoot.row - 1);
2388 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2389 docbookRow( buf, os, endlastfoot.row - 1);
2394 //+---------------------------------------------------------------------
2395 //+ the single row and columns (cells) +
2396 //+---------------------------------------------------------------------
2399 for (int i = 0; i < rows_; ++i) {
2400 if(!IsLongTabular() || (
2401 i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2402 i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2403 docbookRow( buf, os, i);
2407 //+---------------------------------------------------------------------
2408 //+ the closing of the tabular +
2409 //+---------------------------------------------------------------------
2421 void print_n_chars(ostream & os, unsigned char ch, int n)
2423 os << string(n, ch);
2429 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2430 vector<unsigned int> const & clen) const
2432 int const fcell = GetFirstCellInRow(row);
2433 int const n = NumberOfCellsInRow(fcell) + fcell;
2436 for (int i = fcell; i < n; ++i) {
2446 for (int i = fcell; i < n; ++i) {
2457 int column = column_of_cell(i);
2458 int len = clen[column];
2459 while (IsPartOfMultiColumn(row, ++column))
2460 len += clen[column] + 4;
2461 print_n_chars(os, ch, len);
2476 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2477 vector<unsigned int> const & clen) const
2479 int const fcell = GetFirstCellInRow(row);
2480 int const n = NumberOfCellsInRow(fcell) + fcell;
2483 for (int i = fcell; i < n; ++i) {
2484 if (BottomLine(i)) {
2493 for (int i = fcell; i < n; ++i) {
2494 if (BottomLine(i)) {
2504 int column = column_of_cell(i);
2505 int len = clen[column];
2506 while (IsPartOfMultiColumn(row, ++column))
2507 len += clen[column] + 4;
2508 print_n_chars(os, ch, len);
2509 if (BottomLine(i)) {
2523 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2524 int cell, int row, int column,
2525 vector<unsigned int> const & clen) const
2528 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2535 unsigned int len1 = sstr.str().length();
2536 unsigned int len2 = clen[column];
2537 while (IsPartOfMultiColumn(row, ++column))
2538 len2 += clen[column] + 4;
2541 switch (GetAlignment(cell)) {
2543 case LYX_ALIGN_LEFT:
2546 case LYX_ALIGN_RIGHT:
2550 case LYX_ALIGN_CENTER:
2556 for (unsigned int i = 0; i < len1; ++i)
2559 for (unsigned int i = 0; i < len2; ++i)
2561 if (RightLine(cell))
2570 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2574 //+---------------------------------------------------------------------
2575 //+ first calculate the width of the single columns +
2576 //+---------------------------------------------------------------------
2577 vector<unsigned int> clen(columns_);
2579 // first all non (real) multicolumn cells!
2580 for (int j = 0; j < columns_; ++j) {
2582 for (int i = 0; i < rows_; ++i) {
2583 int cell = GetCellNumber(i, j);
2584 if (IsMultiColumn(cell, true))
2587 GetCellInset(cell)->ascii(buf, sstr, 0);
2588 if (clen[j] < sstr.str().length())
2589 clen[j] = sstr.str().length();
2592 // then all (real) multicolumn cells!
2593 for (int j = 0; j < columns_; ++j) {
2594 for (int i = 0; i < rows_; ++i) {
2595 int cell = GetCellNumber(i, j);
2596 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2599 GetCellInset(cell)->ascii(buf, sstr, 0);
2600 int len = int(sstr.str().length());
2601 int const n = cells_in_multicolumn(cell);
2602 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2604 if (len > int(clen[j + n - 1]))
2605 clen[j + n - 1] = len;
2609 for (int i = 0; i < rows_; ++i) {
2610 AsciiTopHLine(os, i, clen);
2611 for (int j = 0; j < columns_; ++j) {
2612 if (IsPartOfMultiColumn(i,j))
2614 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2618 AsciiBottomHLine(os, i, clen);
2624 InsetText * LyXTabular::GetCellInset(int cell) const
2627 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2631 InsetText * LyXTabular::GetCellInset(int row, int column) const
2633 cur_cell = GetCellNumber(row, column);
2634 return & cell_info[row][column].inset;
2638 void LyXTabular::Validate(LaTeXFeatures & features) const
2640 if (IsLongTabular())
2641 features.require("longtable");
2643 features.require("rotating");
2644 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2645 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2646 features.require("array");
2647 GetCellInset(cell)->validate(features);
2652 std::vector<string> const LyXTabular::getLabelList() const
2654 std::vector<string> label_list;
2655 for (int i = 0; i < rows_; ++i)
2656 for (int j = 0; j < columns_; ++j) {
2657 std::vector<string> const l =
2658 GetCellInset(i, j)->getLabelList();
2659 label_list.insert(label_list.end(),
2660 l.begin(), l.end());
2666 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2668 Paragraph * par = GetCellInset(cell)->paragraph();
2670 for (; par; par = par->next()) {
2671 for (int i = 0; i < par->size(); ++i) {
2672 if (par->getChar(i) == Paragraph::META_NEWLINE)