1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
17 #pragma implementation
24 #include "lyx_gui_misc.h"
26 #include "BufferView.h"
28 #include "LaTeXFeatures.h"
29 #include "support/lstrings.h"
30 #include "support/lyxmanip.h"
31 #include "insets/insettabular.h"
32 #include "insets/insettext.h"
41 static int const WIDTH_OF_LINE = 5;
43 extern BufferView * current_view;
45 /// Define a few methods for the inner structs
47 LyXTabular::cellstruct::cellstruct()
49 cellno = 0; //should be initilaized correctly later.
51 multicolumn = LyXTabular::CELL_NORMAL;
52 alignment = LYX_ALIGN_CENTER;
63 LyXTabular::cellstruct::~cellstruct()
68 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
71 width_of_cell = cs.width_of_cell;
72 multicolumn = cs.multicolumn;
73 alignment = cs.alignment;
74 top_line = cs.top_line;
75 bottom_line = cs.bottom_line;
77 linebreaks = cs.linebreaks;
81 inset = static_cast<InsetText *>(cs.inset->Clone());
85 LyXTabular::cellstruct &
86 LyXTabular::cellstruct::operator=(cellstruct const & cs)
89 width_of_cell = cs.width_of_cell;
90 multicolumn = cs.multicolumn;
91 alignment = cs.alignment;
92 top_line = cs.top_line;
93 bottom_line = cs.bottom_line;
95 linebreaks = cs.linebreaks;
97 inset = static_cast<InsetText *>(cs.inset->Clone());
103 LyXTabular::rowstruct::rowstruct()
113 LyXTabular::columnstruct::columnstruct()
117 alignment = LYX_ALIGN_CENTER;
123 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
126 Init(rows_arg, columns_arg);
130 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
133 Init(lt.rows_, lt.columns_);
139 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
146 LyXTabular::~LyXTabular()
149 delete[] columnofcell;
153 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
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 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)
181 LyXTabular * result = new LyXTabular(inset, *this);
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!
187 for(i=0; i < rows_; ++i) {
188 for(j=0; j < columns_; ++j) {
190 delete result->cell_info[i][j].inset;
191 result->cell_info[i][j].inset=new InsetText(*cell_info[i][j].inset);
193 result->cell_info[i][j].inset = cell_info[i][j].inset;
195 result->cell_info[i][j].inset.setOwner(inset);
202 /* activates all lines and sets all widths to 0 */
203 void LyXTabular::Init(int rows_arg, int columns_arg)
209 columns_ = columns_arg;
210 row_info = vector<rowstruct>(rows_, rowstruct());
211 column_info = vector<columnstruct>(columns_, columnstruct());
212 cell_info = vector<vector<cellstruct> >
213 (rows_, vector<cellstruct>(columns_, cellstruct()));
215 // Jürgen, use iterators.
216 for (i = 0; i < rows_; ++i) {
217 for (j = 0; j < columns_; ++j) {
219 if (!cell_info[i][j].inset)
220 cell_info[i][j].inset = new InsetText();
222 cell_info[i][j].inset.setOwner(owner_);
223 cell_info[i][j].inset.SetDrawLockedFrame(true);
224 cell_info[i][j].cellno = cellno++;
227 row_info[i-1].bottom_line = true;
228 row_info[0].bottom_line = true;
230 for (i = 0; i < columns_; ++i) {
231 calculate_width_of_column(i);
233 column_info[columns_-1].right_line = true;
235 calculate_width_of_tabular();
239 set_row_column_number_info();
240 is_long_tabular = false;
249 void LyXTabular::AppendRow(int cell )
253 int row = row_of_cell(cell);
255 row_vector::iterator rit = row_info.begin() + row;
256 row_info.insert(rit, rowstruct());
259 cell_vvector::iterator cit = cell_info.begin() + row;
260 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
262 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
265 for(int i = 0; i <= row; ++i) {
266 for(int j = 0; j < columns_; ++j) {
267 c_info[i][j] = cell_info[i][j];
270 for(int i = row+1; i < rows_; ++i) {
271 for(int j = 0; j < columns_; ++j) {
272 c_info[i][j] = cell_info[i-1][j];
277 for(int i = 0; i < rows_; ++i) {
278 for(int j = 0; j < columns_; ++j) {
279 if (!cell_info[i][j].inset)
280 cell_info[i][j].inset = static_cast<InsetText *>(c_info[i][j].inset->Clone());
285 for (int j = 0; j < columns_; ++j) {
286 cell_info[row][j].inset.clear();
293 void LyXTabular::DeleteRow(int row)
297 row_info.erase(row_info.begin() + row); //&row_info[row]);
298 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
304 void LyXTabular::AppendColumn(int cell)
308 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
310 int column = column_of_cell(cell);
312 column_vector::iterator cit = column_info.begin() + column;
313 column_info.insert(cit, columnstruct());
315 for (i = 0; i < rows_; ++i) {
316 for (j = 0; j <= column; ++j) {
317 c_info[i][j] = cell_info[i][j];
319 for (j = column+1; j < columns_; ++j) {
320 c_info[i][j] = cell_info[i][j-1];
322 // care about multicolumns
323 if (cell_info[i][column+1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
324 cell_info[i][column+1].multicolumn = CELL_PART_OF_MULTICOLUMN;
326 if (((column+1) == columns_) ||
327 (cell_info[i][column+2].multicolumn != CELL_PART_OF_MULTICOLUMN)) {
328 cell_info[i][column+1].multicolumn = LyXTabular::CELL_NORMAL;
333 for(i = 0; i < rows_; ++i) {
334 for(j = 0; j < columns_; ++j) {
335 cell_info[i][j].inset = static_cast<InsetText *>(c_info[i][j].inset->Clone());
340 for (i = 0; i < rows_; ++i) {
341 cell_info[i][column].inset.clear();
347 void LyXTabular::DeleteColumn(int column)
351 column_info.erase(column_info.begin() + column);
352 for (int i = 0; i < rows_; ++i) {
353 cell_info[i].erase(cell_info[i].begin() + column);
360 void LyXTabular::Reinit()
366 // Jürgen, use iterators.
367 for (; i < rows_; ++i) {
368 for (j = 0; j < columns_; ++j) {
369 cell_info[i][j].width_of_cell = 0;
370 cell_info[i][j].inset.setOwner(owner_);
374 for (i = 0; i < columns_; ++i) {
375 calculate_width_of_column(i);
377 calculate_width_of_tabular();
379 set_row_column_number_info();
383 void LyXTabular::set_row_column_number_info()
389 for (; row < rows_; ++row) {
390 for (column = 0; column<columns_; ++column) {
391 if (cell_info[row][column].multicolumn
392 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
394 cell_info[row][column].cellno = numberofcells;
397 ++numberofcells; // because this is one more than as we start from 0
402 rowofcell = new int[numberofcells];
403 delete [] columnofcell;
404 columnofcell = new int[numberofcells];
406 while (c < numberofcells && row < rows_ && column < columns_) {
408 columnofcell[c] = column;
412 } while (column < columns_ &&
413 cell_info[row][column].multicolumn
414 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
415 if (column == columns_) {
420 for (row = 0; row < rows_; ++row) {
421 for (column = 0; column<columns_; ++column) {
422 if (IsPartOfMultiColumn(row,column))
424 cell_info[row][column].inset.SetAutoBreakRows(
425 !GetPWidth(GetCellNumber(row, column)).empty());
431 int LyXTabular::GetNumberOfCells() const
433 return numberofcells;
437 int LyXTabular::NumberOfCellsInRow(int cell) const
439 int row = row_of_cell(cell);
441 for (int i = 0; i < columns_; ++i) {
442 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
449 /* returns 1 if there is a topline, returns 0 if not */
450 bool LyXTabular::TopLine(int cell) const
452 int row = row_of_cell(cell);
454 if (IsMultiColumn(cell))
455 return cellinfo_of_cell(cell)->top_line;
456 return row_info[row].top_line;
460 bool LyXTabular::BottomLine(int cell) const
462 //no bottom line underneath non-existent cells if you please
463 if(cell >= numberofcells)
466 if (IsMultiColumn(cell))
467 return cellinfo_of_cell(cell)->bottom_line;
468 return row_info[row_of_cell(cell)].bottom_line;
472 bool LyXTabular::LeftLine(int cell) const
474 return column_info[column_of_cell(cell)].left_line;
478 bool LyXTabular::RightLine(int cell) const
480 return column_info[right_column_of_cell(cell)].right_line;
484 bool LyXTabular::TopAlreadyDrawed(int cell) const
486 if (GetAdditionalHeight(cell))
488 int row = row_of_cell(cell);
490 int column = column_of_cell(cell);
493 && cell_info[row][column].multicolumn
494 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
496 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
497 return row_info[row].bottom_line;
499 return cell_info[row][column].bottom_line;
505 bool LyXTabular::LeftAlreadyDrawed(int cell) const
507 int column = column_of_cell(cell);
509 int row = row_of_cell(cell);
511 (cell_info[row][column].multicolumn ==
512 LyXTabular::CELL_PART_OF_MULTICOLUMN));
513 if (GetAdditionalWidth(cell_info[row][column].cellno))
515 return column_info[column].right_line;
521 bool LyXTabular::IsLastRow(int cell) const
523 return (row_of_cell(cell) == rows_ - 1);
527 int LyXTabular::GetAdditionalHeight(int cell) const
529 int row = row_of_cell(cell);
532 int top = 1; // bool top = true; ??
533 int bottom = 1; // bool bottom = true; ??
536 for (column = 0; column < columns_ - 1 && bottom; ++column) {
537 switch (cell_info[row - 1][column].multicolumn) {
538 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
539 bottom = cell_info[row - 1][column].bottom_line;
541 case LyXTabular::CELL_NORMAL:
542 bottom = row_info[row - 1].bottom_line;
545 for (column = 0; column < columns_ - 1 && top; ++column) {
546 switch (cell_info[row][column].multicolumn){
547 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
548 top = cell_info[row][column].top_line;
550 case LyXTabular::CELL_NORMAL:
551 top = row_info[row].top_line;
555 return WIDTH_OF_LINE;
560 int LyXTabular::GetAdditionalWidth(int cell) const
562 // internally already set in SetWidthOfCell
563 // used to get it back in text.C
564 int col = right_column_of_cell(cell);
565 if (col < columns_ - 1 && column_info[col].right_line &&
566 column_info[col+1].left_line)
567 return WIDTH_OF_LINE;
573 // returns the maximum over all rows
574 int LyXTabular::GetWidthOfColumn(int cell) const
576 int column1 = column_of_cell(cell);
577 int column2 = right_column_of_cell(cell);
580 for (; i <= column2; ++i) {
581 result += column_info[i].width_of_column;
587 int LyXTabular::GetWidthOfTabular() const
589 return width_of_tabular;
593 /* returns 1 if a complete update is necessary, otherwise 0 */
594 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
596 if (!IsMultiColumn(cell))
599 int row = row_of_cell(cell);
600 int column1 = column_of_cell(cell);
601 int column2 = right_column_of_cell(cell);
603 // first set columns to 0 so we can calculate the right width
605 for (; i <= column2; ++i) {
606 cell_info[row][i].width_of_cell = 0;
608 // set the width to MAX_WIDTH until width > 0
609 int width = (new_width + 2 * WIDTH_OF_LINE);
610 for (i = column1; (i < column2) && (width > 0); ++i) {
611 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
612 width -= column_info[i].width_of_column;
615 cell_info[row][i].width_of_cell = width;
621 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
623 int row = row_of_cell(cell);
624 int column1 = column_of_cell(cell);
625 int column2 = right_column_of_cell(cell);
627 // first set columns to 0 so we can calculate the right width
629 for (; i <= column2; ++i)
630 cell_info[row][i].width_of_cell = 0;
631 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
633 if (i < numberofcells)
634 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
635 SetWidthOfMulticolCell(cell, new_width);
639 /* returns 1 if a complete update is necessary, otherwise 0 */
640 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
642 int row = row_of_cell(cell);
643 int column1 = column_of_cell(cell);
647 if (IsMultiColumn(cell)) {
648 tmp = SetWidthOfMulticolCell(cell, new_width);
650 width = (new_width + 2*WIDTH_OF_LINE);
651 cell_info[row][column1].width_of_cell = width;
652 if (column_info[column1].right_line && (column1 < columns_-1) &&
653 column_info[column1+1].left_line) // additional width
654 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
655 tmp = calculate_width_of_column_NMC(column1);
659 for(i = 0; i<columns_;++i)
660 calculate_width_of_column_NMC(i);
661 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
664 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
665 for(i = 0; i<columns_;++i)
666 calculate_width_of_column(i);
667 calculate_width_of_tabular();
674 bool LyXTabular::SetAlignment(int cell, char align)
676 if (!IsMultiColumn(cell))
677 column_info[column_of_cell(cell)].alignment = align;
678 cellinfo_of_cell(cell)->alignment = align;
683 bool LyXTabular::SetPWidth(int cell, string const & width)
685 bool flag = !width.empty();
687 if (IsMultiColumn(cell)) {
688 cellinfo_of_cell(cell)->p_width = width;
689 GetCellInset(cell)->SetAutoBreakRows(flag);
691 int j = column_of_cell(cell);
693 column_info[j].p_width = width;
694 if (flag) // do this only if there is a width
695 SetAlignment(cell, LYX_ALIGN_LEFT);
696 for(int i=0; i < rows_; ++i) {
697 c = GetCellNumber(i, j);
698 flag = !GetPWidth(c).empty(); // because of multicolumns!
699 GetCellInset(c)->SetAutoBreakRows(flag);
706 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
708 if (what == SET_SPECIAL_MULTI)
709 cellinfo_of_cell(cell)->align_special = special;
711 column_info[column_of_cell(cell)].align_special = special;
716 bool LyXTabular::SetAllLines(int cell, bool line)
718 SetTopLine(cell, line);
719 SetBottomLine(cell, line);
720 SetRightLine(cell, line);
721 SetLeftLine(cell, line);
726 bool LyXTabular::SetTopLine(int cell, bool line)
728 int row = row_of_cell(cell);
730 if (!IsMultiColumn(cell))
731 row_info[row].top_line = line;
733 cellinfo_of_cell(cell)->top_line = line;
738 bool LyXTabular::SetBottomLine(int cell, bool line)
740 if (!IsMultiColumn(cell))
741 row_info[row_of_cell(cell)].bottom_line = line;
743 cellinfo_of_cell(cell)->bottom_line = line;
748 bool LyXTabular::SetLeftLine(int cell, bool line)
750 column_info[column_of_cell(cell)].left_line = line;
755 bool LyXTabular::SetRightLine(int cell, bool line)
757 column_info[right_column_of_cell(cell)].right_line = line;
762 char LyXTabular::GetAlignment(int cell) const
764 if (IsMultiColumn(cell))
765 return cellinfo_of_cell(cell)->alignment;
767 return column_info[column_of_cell(cell)].alignment;
771 string LyXTabular::GetPWidth(int cell) const
773 if (IsMultiColumn(cell))
774 return cellinfo_of_cell(cell)->p_width;
775 return column_info[column_of_cell(cell)].p_width;
779 string LyXTabular::GetAlignSpecial(int cell, int what) const
781 if (what == SET_SPECIAL_MULTI)
782 return cellinfo_of_cell(cell)->align_special;
783 return column_info[column_of_cell(cell)].align_special;
787 int LyXTabular::GetWidthOfCell(int cell) const
789 int row = row_of_cell(cell);
790 int column1 = column_of_cell(cell);
791 int column2 = right_column_of_cell(cell);
794 for (; i <= column2; ++i) {
795 result += cell_info[row][i].width_of_cell;
798 // result += GetAdditionalWidth(cell);
803 int LyXTabular::GetBeginningOfTextInCell(int cell) const
807 switch (GetAlignment(cell)){
808 case LYX_ALIGN_CENTER:
809 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
811 case LYX_ALIGN_RIGHT:
812 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
813 // + GetAdditionalWidth(cell);
815 default: /* LYX_ALIGN_LEFT: nothing :-) */
825 bool LyXTabular::IsFirstCellInRow(int cell) const
827 return (column_of_cell(cell) == 0);
831 int LyXTabular::GetFirstCellInRow(int row) const
835 return cell_info[row][0].cellno;
838 bool LyXTabular::IsLastCellInRow(int cell) const
840 return (right_column_of_cell(cell) == (columns_ - 1));
844 int LyXTabular::GetLastCellInRow(int row) const
848 return cell_info[row][columns_-1].cellno;
852 bool LyXTabular::calculate_width_of_column(int column)
854 int old_column_width = column_info[column].width_of_column;
857 for (int i = 0; i < rows_; ++i) {
858 maximum = max(cell_info[i][column].width_of_cell, maximum);
860 column_info[column].width_of_column = maximum;
861 return (column_info[column].width_of_column != old_column_width);
865 bool LyXTabular::calculate_width_of_column_NMC(int column)
867 int old_column_width = column_info[column].width_of_column;
869 for (int i = 0; i < rows_; ++i) {
870 if (!IsMultiColumn(GetCellNumber(i, column)) &&
871 (cell_info[i][column].width_of_cell > max)) {
872 max = cell_info[i][column].width_of_cell;
875 column_info[column].width_of_column = max;
876 return (column_info[column].width_of_column != old_column_width);
880 void LyXTabular::calculate_width_of_tabular()
882 width_of_tabular = 0;
883 for (int i = 0; i < columns_; ++i) {
884 width_of_tabular += column_info[i].width_of_column;
889 int LyXTabular::row_of_cell(int cell) const
891 if (cell >= numberofcells)
895 return rowofcell[cell];
899 int LyXTabular::column_of_cell(int cell) const
901 if (cell >= numberofcells)
905 return columnofcell[cell];
909 int LyXTabular::right_column_of_cell(int cell) const
911 int row = row_of_cell(cell);
912 int column = column_of_cell(cell);
913 while (column < (columns_ - 1) &&
914 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
920 void LyXTabular::Write(Buffer const * buf, ostream & os) const
925 os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
927 // global longtable options
928 os << "<Features rotate=" << rotate <<
929 " islongtable=" << is_long_tabular <<
930 " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
931 " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
933 for (i = 0; i < rows_; ++i) {
934 os << "<Row topline=" << row_info[i].top_line <<
935 " bottomline=" << row_info[i].bottom_line <<
936 " newpage=" << row_info[i].newpage <<
938 for (j = 0; j < columns_; ++j) {
940 os << "<Column alignment=" << column_info[j].alignment <<
941 " leftline=" << column_info[j].left_line <<
942 " rightline=" << column_info[j].right_line <<
943 " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
944 "\" special=\"" << column_info[j].align_special <<
947 os << "<Column>" << endl;
949 os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
950 " alignment=" << cell_info[i][j].alignment <<
951 " topline=" << cell_info[i][j].top_line <<
952 " bottomline=" << cell_info[i][j].bottom_line <<
953 " rotate=" << cell_info[i][j].rotate <<
954 " linebreaks=" << cell_info[i][j].linebreaks <<
955 " width=\"" << cell_info[i][j].p_width <<
956 "\" special=\"" << cell_info[i][j].align_special <<
958 os << "\\begin_inset ";
959 cell_info[i][j].inset.Write(buf, os);
960 os << "\n\\end_inset " << endl;
961 os << "</Cell>" << endl;
962 os << "</Column>" << endl;
964 os << "</Row>" << endl;
966 os << "</LyXTabular>" << endl;
971 bool getTokenValue(string const str, const char * token, string & ret)
973 int pos = str.find(token);
974 char ch = str[pos+strlen(token)];
976 if ((pos < 0) || (ch != '='))
979 pos += strlen(token)+1;
981 if ((ch != '"') && (ch != '\'')) { // only read till next space
985 while((pos < int(str.length()-1)) && (str[++pos] != ch))
993 bool getTokenValue(string const str, const char * token, int & num)
996 int pos = str.find(token);
997 char ch = str[pos+strlen(token)];
999 if ((pos < 0) || (ch != '='))
1002 pos += strlen(token)+1;
1004 if ((ch != '"') && (ch != '\'')) { // only read till next space
1010 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1013 num = strToInt(ret);
1019 bool getTokenValue(string const str, const char * token, bool & flag)
1022 int pos = str.find(token);
1023 char ch = str[pos+strlen(token)];
1025 if ((pos < 0) || (ch != '='))
1028 pos += strlen(token)+1;
1030 if ((ch != '"') && (ch != '\'')) { // only read till next space
1036 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1039 flag = strToInt(ret);
1044 void l_getline(istream & is, string & str)
1052 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1055 istream & is = lex.getStream();
1057 l_getline(is, line);
1058 if (!prefixIs(line, "<LyXTabular ")) {
1059 OldFormatRead(lex, line);
1066 if (!getTokenValue(line, "version", version))
1068 if (!getTokenValue(line, "rows", rows_arg))
1070 if (!getTokenValue(line, "columns", columns_arg))
1072 Init(rows_arg, columns_arg);
1073 l_getline(is, line);
1074 if (!prefixIs(line, "<Features ")) {
1075 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1076 line << ")" << endl;
1079 (void)getTokenValue(line, "islongtable", is_long_tabular);
1080 (void)getTokenValue(line, "endhead", endhead);
1081 (void)getTokenValue(line, "endfirsthead", endfirsthead);
1082 (void)getTokenValue(line, "endfoot", endfoot);
1083 (void)getTokenValue(line, "endlastfoot", endlastfoot);
1085 for(i = 0; i < rows_; ++i) {
1086 l_getline(is, line);
1087 if (!prefixIs(line, "<Row ")) {
1088 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1089 line << ")" << endl;
1092 (void)getTokenValue(line, "topline", row_info[i].top_line);
1093 (void)getTokenValue(line, "bottomline", row_info[i].bottom_line);
1094 (void)getTokenValue(line, "newpage", row_info[i].newpage);
1095 for (j = 0; j < columns_; ++j) {
1097 if (!prefixIs(line,"<Column")) {
1098 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1099 line << ")" << endl;
1103 (void)getTokenValue(line, "alignment", column_info[j].alignment);
1104 (void)getTokenValue(line, "leftline", column_info[j].left_line);
1105 (void)getTokenValue(line, "rightline", column_info[j].right_line);
1106 (void)getTokenValue(line, "width", column_info[j].p_width);
1107 (void)getTokenValue(line, "special", column_info[j].align_special);
1109 l_getline(is, line);
1110 if (!prefixIs(line, "<Cell")) {
1111 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1112 line << ")" << endl;
1115 (void)getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1116 (void)getTokenValue(line, "alignment", cell_info[i][j].alignment);
1117 (void)getTokenValue(line, "topline", cell_info[i][j].top_line);
1118 (void)getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1119 (void)getTokenValue(line, "rotate", cell_info[i][j].rotate);
1120 (void)getTokenValue(line, "linebreaks", cell_info[i][j].linebreaks);
1121 (void)getTokenValue(line, "width", cell_info[i][j].p_width);
1122 (void)getTokenValue(line, "special", cell_info[i][j].align_special);
1123 l_getline(is, line);
1124 if (prefixIs(line, "\\begin_inset")) {
1125 cell_info[i][j].inset.Read(buf, lex);
1126 l_getline(is, line);
1128 if (line != "</Cell>") {
1129 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1130 line << ")" << endl;
1133 l_getline(is, line);
1134 if (line != "</Column>") {
1135 lyxerr << "Wrong tabular format (expected </Column> got" <<
1136 line << ")" << endl;
1140 l_getline(is, line);
1141 if (line != "</Row>") {
1142 lyxerr << "Wrong tabular format (expected </Row> got" <<
1143 line << ")" << endl;
1147 while (line != "</LyXTabular>") {
1148 l_getline(is, line);
1150 set_row_column_number_info();
1154 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1159 int columns_arg = 0;
1160 int is_long_tabular_arg = false;
1161 int rotate_arg = false;
1171 istream & is = lex.getStream();
1174 version = atoi(s.c_str() + 8);
1178 vector<int> cont_row_info;
1181 lyxerr << "Tabular format < 5 is not supported anymore\n"
1182 "Get an older version of LyX (< 1.1.x) for conversion!"
1184 WriteAlert(_("Warning:"),
1185 _("Tabular format < 5 is not supported anymore\n"),
1186 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1188 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1189 >> rotate_arg >> a >> b >> c >> d;
1191 is >> rows_arg >> columns_arg;
1192 Init(rows_arg, columns_arg);
1193 cont_row_info = vector<int>(rows_arg);
1194 SetLongTabular(is_long_tabular_arg);
1195 SetRotateTabular(rotate_arg);
1197 for (i = 0; i < rows_; ++i) {
1199 cont_row_info[i] = false;
1201 for (i = 0; i < columns_; ++i) {
1204 for (i = 0; i < rows_; ++i) {
1205 for (j = 0; j < columns_; ++j) {
1210 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1211 >> rotate_arg >> a >> b >> c >> d;
1212 Init(rows_arg, columns_arg);
1213 cont_row_info = vector<int>(rows_arg);
1214 SetLongTabular(is_long_tabular_arg);
1215 SetRotateTabular(rotate_arg);
1220 for (i = 0; i < rows_; ++i) {
1221 a = b = c = d = e = f = g = h = 0;
1222 is >> a >> b >> c >> d;
1223 row_info[i].top_line = a;
1224 row_info[i].bottom_line = b;
1225 cont_row_info[i] = c;
1226 row_info[i].newpage = d;
1228 for (i = 0; i < columns_; ++i) {
1232 char ch; // skip '"'
1234 getline(is, s1, '"');
1235 is >> ch; // skip '"'
1236 getline(is, s2, '"');
1237 column_info[i].alignment = static_cast<char>(a);
1238 column_info[i].left_line = b;
1239 column_info[i].right_line = c;
1240 column_info[i].p_width = s1;
1241 column_info[i].align_special = s2;
1243 for (i = 0; i < rows_; ++i) {
1244 for (j = 0; j < columns_; ++j) {
1247 is >> a >> b >> c >> d >> e >> f >> g;
1249 is >> ch; // skip '"'
1250 getline(is, s1, '"');
1251 is >> ch; // skip '"'
1252 getline(is, s2, '"');
1253 cell_info[i][j].multicolumn = static_cast<char>(a);
1254 cell_info[i][j].alignment = static_cast<char>(b);
1255 cell_info[i][j].top_line = static_cast<char>(c);
1256 cell_info[i][j].bottom_line = static_cast<char>(d);
1257 cell_info[i][j].rotate = static_cast<bool>(f);
1258 cell_info[i][j].linebreaks = static_cast<bool>(g);
1259 cell_info[i][j].align_special = s1;
1260 cell_info[i][j].p_width = s2;
1264 set_row_column_number_info();
1266 LyXParagraph * par = new LyXParagraph;
1267 LyXParagraph * return_par = 0;
1268 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1269 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1270 string token, tmptok;
1273 LyXFont font(LyXFont::ALL_SANE);
1275 while (lex.IsOK()) {
1277 token = lex.GetString();
1280 if ((token == "\\layout") || (token == "\\end_float") ||
1281 (token == "\\end_deeper"))
1283 lex.pushToken(token);
1286 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1294 lex.pushToken(token);
1298 lex.printError("New Paragraph allocated! This should not happen!");
1299 lex.pushToken(token);
1305 // now we have the par we should fill the insets with this!
1307 InsetText * inset = GetCellInset(cell);
1310 for(int i = 0; i < par->Last(); ++i) {
1311 if (par->IsNewline(i)) {
1313 if (cell > GetNumberOfCells()) {
1314 lyxerr << "Some error in reading old table format occured!" <<
1315 endl << "Terminating when reading cell[" << cell << "]!" <<
1319 row = row_of_cell(cell);
1320 if (cont_row_info[row]) {
1322 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1323 while(!IsFirstCellInRow(--cell));
1325 inset = GetCellInset(cell);
1328 inset = GetCellInset(cell);
1329 row = row_of_cell(cell);
1330 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].linebreaks)
1332 // insert a space instead
1334 par->InsertChar(i, ' ');
1337 par->CopyIntoMinibuffer(current_view->buffer()->params, i);
1338 inset->par->InsertFromMinibuffer(inset->par->Last());
1344 char const * LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1346 int i = isColumn ? cell : column_of_cell(cell);
1351 //i = column_of_cell(cell);
1352 if (!isColumn && IsMultiColumn(cell)) {
1353 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1354 return cellinfo_of_cell(cell)->align_special.c_str();
1356 switch (GetAlignment(cell)) {
1357 case LYX_ALIGN_LEFT:
1359 case LYX_ALIGN_RIGHT:
1366 if (!column_info[i].align_special.empty()) {
1367 return column_info[i].align_special.c_str();
1369 #ifdef IGNORE_THIS_FOR_NOW
1370 else if (!column_info[i].p_width.empty()) {
1372 file += column_info[i].p_width;
1377 switch (column_info[i].alignment) {
1378 case LYX_ALIGN_LEFT:
1380 case LYX_ALIGN_RIGHT:
1390 // cell <0 will tex the preamble
1391 // returns the number of printed newlines
1392 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1396 //int tmp; // tmp2; // unused
1397 int nvcell; // fcell; // unused
1398 if (IsLastCell(cell)) {
1399 os << newlineAndDepth(--depth)
1401 << newlineAndDepth(--depth)
1403 << newlineAndDepth(--depth)
1405 << newlineAndDepth(--depth);
1406 if (is_long_tabular)
1410 << newlineAndDepth(--depth);
1416 if (is_long_tabular)
1422 << "' COLSEP='1' ROWSEP='1'>"
1423 << newlineAndDepth(++depth);
1425 for (i = 0; i < columns_; ++i) {
1426 os << "<COLSPEC ALIGN='"
1427 << GetDocBookAlign(i, true)
1433 if (i == (columns_-1)) {
1436 if (column_info[i].right_line ||
1437 column_info[i+1].left_line)
1443 << newlineAndDepth(depth);
1445 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1446 if (column_info[i].left_line)
1451 << newlineAndDepth(++depth)
1453 << newlineAndDepth(++depth)
1455 << GetDocBookAlign(0)
1457 if (IsMultiColumn(0)) {
1458 os << " NAMEST='col1' NAMEEND='col"
1459 << cells_in_multicolumn(0)
1463 << newlineAndDepth(++depth);
1466 if (IsLastCellInRow(cell)) {
1467 os << newlineAndDepth(--depth)
1469 << newlineAndDepth(--depth)
1471 << newlineAndDepth(depth)
1473 << newlineAndDepth(++depth)
1475 << GetDocBookAlign(cell + 1)
1476 << "' VALIGN='middle'";
1477 if (IsMultiColumn(cell + 1)) {
1478 os << " NAMEST='col"
1479 << column_of_cell(cell+1) + 1
1481 << column_of_cell(cell + 1) +
1482 cells_in_multicolumn(cell + 1)
1486 << newlineAndDepth(++depth);
1489 os << newlineAndDepth(--depth)
1491 << newlineAndDepth(depth)
1493 << GetDocBookAlign(cell + 1)
1494 << "' VALIGN='middle'";
1495 if (IsMultiColumn(cell + 1)) {
1496 os << " NAMEST='col"
1497 << column_of_cell(cell+1) + 1
1499 << column_of_cell(cell+1) +
1500 cells_in_multicolumn(cell+1)
1504 << newlineAndDepth(++depth);
1513 bool LyXTabular::IsMultiColumn(int cell) const
1515 return (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1519 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1521 int row = row_of_cell(cell);
1522 int column = column_of_cell(cell);
1523 return &cell_info[row][column];
1527 void LyXTabular::SetMultiColumn(int cell, int number)
1529 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1530 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1531 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1532 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1533 for (number--; number > 0; --number) {
1534 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1536 set_row_column_number_info();
1540 int LyXTabular::cells_in_multicolumn(int cell) const
1542 int row = row_of_cell(cell);
1543 int column = column_of_cell(cell);
1546 while ((column < columns_) &&
1547 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1556 int LyXTabular::UnsetMultiColumn(int cell)
1558 int row = row_of_cell(cell);
1559 int column = column_of_cell(cell);
1563 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1564 cell_info[row][column].multicolumn = CELL_NORMAL;
1566 while ((column < columns_) &&
1567 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1569 cell_info[row][column].multicolumn = CELL_NORMAL;
1574 set_row_column_number_info();
1579 void LyXTabular::SetLongTabular(int what)
1581 is_long_tabular = what;
1585 bool LyXTabular::IsLongTabular() const
1587 return is_long_tabular;
1591 void LyXTabular::SetRotateTabular(int what)
1597 bool LyXTabular::GetRotateTabular() const
1603 void LyXTabular::SetRotateCell(int cell, int what)
1605 cellinfo_of_cell(cell)->rotate = what;
1609 bool LyXTabular::GetRotateCell(int cell) const
1611 return cellinfo_of_cell(cell)->rotate;
1615 bool LyXTabular::NeedRotating() const
1619 for (int i = 0; i < rows_; ++i) {
1620 for (int j = 0; j < columns_; ++j) {
1621 if (cell_info[i][j].rotate)
1629 bool LyXTabular::IsLastCell(int cell) const
1631 if ((cell+1) < GetNumberOfCells())
1637 int LyXTabular::GetCellAbove(int cell) const
1639 if (row_of_cell(cell) > 0)
1640 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1645 int LyXTabular::GetCellBelow(int cell) const
1647 if (row_of_cell(cell)+1 < rows_)
1648 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1653 int LyXTabular::GetLastCellAbove(int cell) const
1655 if (row_of_cell(cell) <= 0)
1657 if (!IsMultiColumn(cell))
1658 return GetCellAbove(cell);
1659 return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
1663 int LyXTabular::GetLastCellBelow(int cell) const
1665 if (row_of_cell(cell)+1 >= rows_)
1667 if (!IsMultiColumn(cell))
1668 return GetCellBelow(cell);
1669 return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
1673 int LyXTabular::GetCellNumber(int row, int column) const
1675 if (column >= columns_)
1676 column = columns_ - 1;
1677 else if (column < 0)
1684 return cell_info[row][column].cellno;
1688 void LyXTabular::SetLinebreaks(int cell, bool what)
1690 cellinfo_of_cell(cell)->linebreaks = what;
1694 bool LyXTabular::GetLinebreaks(int cell) const
1696 if (column_info[column_of_cell(cell)].p_width.empty() &&
1697 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1699 return cellinfo_of_cell(cell)->linebreaks;
1703 void LyXTabular::SetLTHead(int cell, bool first)
1705 int row = row_of_cell(cell);
1708 if (row == endfirsthead)
1721 bool LyXTabular::GetRowOfLTHead(int cell) const
1723 if ((endhead+1) > rows_)
1725 return (row_of_cell(cell) == endhead);
1729 bool LyXTabular::GetRowOfLTFirstHead(int cell) const
1731 if ((endfirsthead+1) > rows_)
1733 return (row_of_cell(cell) == endfirsthead);
1737 void LyXTabular::SetLTFoot(int cell, bool last)
1739 int row = row_of_cell(cell);
1742 if (row == endlastfoot)
1755 bool LyXTabular::GetRowOfLTFoot(int cell) const
1757 if ((endfoot+1) > rows_)
1759 return (row_of_cell(cell) == endfoot);
1762 bool LyXTabular::GetRowOfLTLastFoot(int cell) const
1764 if ((endlastfoot+1) > rows_)
1766 return (row_of_cell(cell) == endlastfoot);
1770 void LyXTabular::SetLTNewPage(int cell, bool what)
1772 row_info[row_of_cell(cell)].newpage = what;
1776 bool LyXTabular::GetLTNewPage(int cell) const
1778 return row_info[row_of_cell(cell)].newpage;
1782 void LyXTabular::SetAscentOfRow(int row, int height)
1786 row_info[row].ascent_of_row = height;
1790 void LyXTabular::SetDescentOfRow(int row, int height)
1794 row_info[row].descent_of_row = height;
1798 int LyXTabular::GetAscentOfRow(int row) const
1802 return row_info[row].ascent_of_row;
1806 int LyXTabular::GetDescentOfRow(int row) const
1810 return row_info[row].descent_of_row;
1814 int LyXTabular::GetHeightOfTabular() const
1818 for(int row = 0; row < rows_; ++row)
1819 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1820 GetAdditionalHeight(GetCellNumber(row, 0));
1825 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1827 if ((row >= rows_) || (column >= columns_))
1829 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1833 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1835 int fcell = GetFirstCellInRow(row);
1836 int n = NumberOfCellsInRow(fcell) + fcell;
1840 for (i = fcell; i < n; ++i) {
1844 if (tmp == (n - fcell)){
1847 for (i = fcell; i < n; ++i) {
1850 << column_of_cell(i) + 1
1852 << right_column_of_cell(i) + 1
1865 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1867 int fcell = GetFirstCellInRow(row);
1868 int n = NumberOfCellsInRow(fcell) + fcell;
1872 for (i = fcell; i < n; ++i) {
1876 if (tmp == (n-fcell)){
1879 for (i = fcell; i < n; ++i) {
1880 if (BottomLine(i)) {
1882 << column_of_cell(i) + 1
1884 << right_column_of_cell(i) + 1
1897 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1901 if (GetRotateCell(cell)) {
1902 os << "\\begin{sideways}" << endl;
1905 if (IsMultiColumn(cell)) {
1906 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1907 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1908 os << cellinfo_of_cell(cell+1)->align_special << "}{";
1912 if (!GetPWidth(cell).empty()) {
1913 os << "p{" << GetPWidth(cell) << '}';
1915 switch (GetAlignment(cell)) {
1916 case LYX_ALIGN_LEFT:
1919 case LYX_ALIGN_RIGHT:
1927 if (RightLine(cell))
1929 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1935 if (GetLinebreaks(cell)) {
1936 os << "\\parbox[t]{" << GetPWidth(cell) << "}{\\smallskip{}";
1942 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1947 if (GetLinebreaks(cell))
1948 os << "\\smallskip{}}";
1949 if (IsMultiColumn(cell)){
1952 if (GetRotateCell(cell)) {
1953 os << endl << "\\end{sideways}";
1960 int LyXTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
1966 //+---------------------------------------------------------------------
1967 //+ first the opening preamble +
1968 //+---------------------------------------------------------------------
1971 os << "\\begin{sideways}" << endl;
1974 if (is_long_tabular)
1975 os << "\\begin{longtable}{";
1977 os << "\\begin{tabular}{";
1978 for (i = 0; i < columns_; ++i) {
1979 if (column_info[i].left_line)
1981 if (!column_info[i].align_special.empty()) {
1982 os << column_info[i].align_special;
1983 } else if (!column_info[i].p_width.empty()) {
1985 << column_info[i].p_width
1988 switch (column_info[i].alignment) {
1989 case LYX_ALIGN_LEFT:
1992 case LYX_ALIGN_RIGHT:
2000 if (column_info[i].right_line)
2006 //+---------------------------------------------------------------------
2007 //+ the single row and columns (cells) +
2008 //+---------------------------------------------------------------------
2010 for(i=0; i < rows_; ++i) {
2011 ret += TeXTopHLine(os, i);
2012 for(j=0; j < columns_; ++j) {
2013 if (IsPartOfMultiColumn(i,j))
2015 ret += TeXCellPreamble(os, cell);
2016 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2017 ret += TeXCellPostamble(os, cell);
2018 if (!IsLastCellInRow(cell)) { // not last cell in row
2024 os << "\\\\" << endl;
2025 ret += TeXBottomHLine(os, i);
2026 if (IsLongTabular()) {
2028 os << "\\endhead\n";
2031 if (i == endfirsthead) {
2032 os << "\\endfirsthead\n";
2036 os << "\\endfoot\n";
2039 if (i == endlastfoot) {
2040 os << "\\endlastfoot\n";
2043 if (row_info[i].newpage) {
2044 os << "\\newpage\n";
2050 //+---------------------------------------------------------------------
2051 //+ the closing of the tabular +
2052 //+---------------------------------------------------------------------
2054 if (is_long_tabular)
2055 os << "\\end{longtable}";
2057 os << "\\end{tabular}";
2059 os << "\n\\end{sideways}";
2067 InsetText * LyXTabular::GetCellInset(int cell) const
2069 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2072 void LyXTabular::Validate(LaTeXFeatures & features) const
2074 if (IsLongTabular())
2075 features.longtable = true;
2077 features.rotating = true;
2078 for(int cell = 0; cell < numberofcells; ++cell)
2079 GetCellInset(cell)->Validate(features);