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 /// Define a few methods for the inner structs
45 LyXTabular::cellstruct::cellstruct()
47 cellno = 0; //should be initilaized correctly later.
49 multicolumn = LyXTabular::CELL_NORMAL;
50 alignment = LYX_ALIGN_CENTER;
59 LyXTabular::cellstruct::~cellstruct()
65 LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
68 width_of_cell = cs.width_of_cell;
69 multicolumn = cs.multicolumn;
70 alignment = cs.alignment;
71 top_line = cs.top_line;
72 bottom_line = cs.bottom_line;
74 linebreaks = cs.linebreaks;
77 // inset = static_cast<InsetText *>(cs.inset->Clone());
81 LyXTabular::cellstruct &
82 LyXTabular::cellstruct::operator=(cellstruct const & cs)
85 width_of_cell = cs.width_of_cell;
86 multicolumn = cs.multicolumn;
87 alignment = cs.alignment;
88 top_line = cs.top_line;
89 bottom_line = cs.bottom_line;
91 linebreaks = cs.linebreaks;
93 inset = static_cast<InsetText *>(cs.inset->Clone());
98 LyXTabular::rowstruct::rowstruct()
108 LyXTabular::columnstruct::columnstruct()
112 alignment = LYX_ALIGN_CENTER;
118 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
121 Init(rows_arg, columns_arg);
125 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
128 Init(lt.rows_, lt.columns_);
134 LyXTabular::LyXTabular(InsetTabular * inset, LyXLex & lex)
141 LyXTabular::~LyXTabular()
144 delete[] columnofcell;
148 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
150 // If this and lt is not of the same size we have a serious bug
151 // So then it is ok to throw an exception, or for now
153 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
155 cell_info = lt.cell_info;
156 row_info = lt.row_info;
157 column_info = lt.column_info;
159 // long tabular stuff
160 SetLongTabular(lt.is_long_tabular);
161 endhead = lt.endhead;
162 endfoot = lt.endfoot;
163 endfirsthead = lt.endfirsthead;
164 endlastfoot = lt.endlastfoot;
174 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
176 LyXTabular * result = new LyXTabular(inset, *this);
178 // don't know if this is good but I need to Clone also
179 // the text-insets here, this is for the Undo-facility!
182 for(i=0; i < rows_; ++i) {
183 for(j=0; j < columns_; ++j) {
184 delete result->cell_info[i][j].inset;
185 result->cell_info[i][j].inset=new InsetText(*cell_info[i][j].inset,
186 inset->BufferOwner());
187 result->cell_info[i][j].inset->setOwner(inset);
194 /* activates all lines and sets all widths to 0 */
195 void LyXTabular::Init(int rows_arg, int columns_arg)
201 columns_ = columns_arg;
202 row_info = vector<rowstruct>(rows_, rowstruct());
203 column_info = vector<columnstruct>(columns_, columnstruct());
204 cell_info = vector<vector<cellstruct> >
205 (rows_, vector<cellstruct>(columns_, cellstruct()));
207 // Jürgen, use iterators.
208 for (i = 0; i < rows_; ++i) {
209 for (j = 0; j < columns_; ++j) {
210 if (!cell_info[i][j].inset)
211 cell_info[i][j].inset = new InsetText(owner_->BufferOwner());
212 cell_info[i][j].inset->setOwner(owner_);
213 cell_info[i][j].inset->SetDrawLockedFrame(true);
214 cell_info[i][j].cellno = cellno++;
217 row_info[i-1].bottom_line = true;
218 row_info[0].bottom_line = true;
220 for (i = 0; i < columns_; ++i) {
221 calculate_width_of_column(i);
223 column_info[columns_-1].right_line = true;
225 calculate_width_of_tabular();
229 set_row_column_number_info();
230 is_long_tabular = false;
239 void LyXTabular::AppendRow(int cell )
241 // cell_vector::iterator cit = cell_info.begin() + row;
242 // cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
243 // cell_info.insert(cell_info.begin(), vector<cellstruct>(columns_, cellstruct()));
247 // row_vector r_info = row_vector(rows_, rowstruct());
249 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
251 int row = row_of_cell(cell);
254 row_vector::iterator rit = row_info.begin() + row;
255 row_info.insert(rit, rowstruct());
257 for(i = 0; i <= row; ++i) {
258 // r_info[i] = row_info[i];
259 for(int j = 0; j < columns_; ++j) {
260 c_info[i][j] = cell_info[i][j];
263 for(i = row+1; i < rows_; ++i) {
264 // r_info[i] = row_info[i-1];
265 for(int j = 0; j < columns_; ++j) {
266 c_info[i][j] = cell_info[i-1][j];
269 // row_info = r_info;
271 for(i = 0; i < rows_; ++i) {
272 for(int j = 0; j < columns_; ++j) {
273 cell_info[i][j].inset = static_cast<InsetText *>(c_info[i][j].inset->Clone());
277 for (int j = 0; j < columns_; ++j) {
278 cell_info[row][j].inset->clear();
284 void LyXTabular::DeleteRow(int row)
288 row_info.erase(row_info.begin() + row); //&row_info[row]);
289 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
295 void LyXTabular::AppendColumn(int cell)
299 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
301 int column = column_of_cell(cell);
303 column_vector::iterator cit = column_info.begin() + column;
304 column_info.insert(cit, columnstruct());
306 for (i = 0; i < rows_; ++i) {
307 for (j = 0; j <= column; ++j) {
308 c_info[i][j] = cell_info[i][j];
310 for (j = column+1; j < columns_; ++j) {
311 c_info[i][j] = cell_info[i][j-1];
313 // care about multicolumns
314 if (cell_info[i][column+1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
315 cell_info[i][column+1].multicolumn = CELL_PART_OF_MULTICOLUMN;
317 if (((column+1) == columns_) ||
318 (cell_info[i][column+2].multicolumn != CELL_PART_OF_MULTICOLUMN)) {
319 cell_info[i][column+1].multicolumn = LyXTabular::CELL_NORMAL;
323 for(i = 0; i < rows_; ++i) {
324 for(j = 0; j < columns_; ++j) {
325 cell_info[i][j].inset = static_cast<InsetText *>(c_info[i][j].inset->Clone());
329 for (i = 0; i < rows_; ++i) {
330 cell_info[i][column].inset->clear();
336 void LyXTabular::DeleteColumn(int column)
340 column_info.erase(column_info.begin() + column);
341 for (int i = 0; i < rows_; ++i) {
342 cell_info[i].erase(cell_info[i].begin() + column);
349 void LyXTabular::Reinit()
355 // Jürgen, use iterators.
356 for (; i < rows_; ++i) {
357 for (j = 0; j < columns_; ++j) {
358 cell_info[i][j].width_of_cell = 0;
362 for (i = 0; i < columns_; ++i) {
363 calculate_width_of_column(i);
365 calculate_width_of_tabular();
367 set_row_column_number_info();
371 void LyXTabular::set_row_column_number_info()
377 for (; row < rows_; ++row) {
378 for (column = 0; column<columns_; ++column) {
379 if (cell_info[row][column].multicolumn
380 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
382 cell_info[row][column].cellno = numberofcells;
385 ++numberofcells; // because this is one more than as we start from 0
390 rowofcell = new int[numberofcells];
391 delete [] columnofcell;
392 columnofcell = new int[numberofcells];
394 while (c < numberofcells && row < rows_ && column < columns_) {
396 columnofcell[c] = column;
400 } while (column < columns_ &&
401 cell_info[row][column].multicolumn
402 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
403 if (column == columns_) {
408 for (row = 0; row < rows_; ++row) {
409 for (column = 0; column<columns_; ++column) {
410 if (IsPartOfMultiColumn(row,column))
412 cell_info[row][column].inset->SetAutoBreakRows(
413 !GetPWidth(GetCellNumber(row, column)).empty());
419 int LyXTabular::GetNumberOfCells() const
421 return numberofcells;
425 int LyXTabular::NumberOfCellsInRow(int cell) const
427 int row = row_of_cell(cell);
429 for (int i = 0; i < columns_; ++i) {
430 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
437 /* returns 1 if there is a topline, returns 0 if not */
438 bool LyXTabular::TopLine(int cell) const
440 int row = row_of_cell(cell);
442 if (IsMultiColumn(cell))
443 return cellinfo_of_cell(cell)->top_line;
444 return row_info[row].top_line;
448 bool LyXTabular::BottomLine(int cell) const
450 //no bottom line underneath non-existent cells if you please
451 if(cell >= numberofcells)
454 if (IsMultiColumn(cell))
455 return cellinfo_of_cell(cell)->bottom_line;
456 return row_info[row_of_cell(cell)].bottom_line;
460 bool LyXTabular::LeftLine(int cell) const
462 return column_info[column_of_cell(cell)].left_line;
466 bool LyXTabular::RightLine(int cell) const
468 return column_info[right_column_of_cell(cell)].right_line;
472 bool LyXTabular::TopAlreadyDrawed(int cell) const
474 if (GetAdditionalHeight(cell))
476 int row = row_of_cell(cell);
478 int column = column_of_cell(cell);
481 && cell_info[row][column].multicolumn
482 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
484 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
485 return row_info[row].bottom_line;
487 return cell_info[row][column].bottom_line;
493 bool LyXTabular::LeftAlreadyDrawed(int cell) const
495 int column = column_of_cell(cell);
497 int row = row_of_cell(cell);
499 (cell_info[row][column].multicolumn ==
500 LyXTabular::CELL_PART_OF_MULTICOLUMN));
501 if (GetAdditionalWidth(cell_info[row][column].cellno))
503 return column_info[column].right_line;
509 bool LyXTabular::IsLastRow(int cell) const
511 return (row_of_cell(cell) == rows_ - 1);
515 int LyXTabular::GetAdditionalHeight(int cell) const
517 int row = row_of_cell(cell);
520 int top = 1; // bool top = true; ??
521 int bottom = 1; // bool bottom = true; ??
524 for (column = 0; column < columns_ - 1 && bottom; ++column) {
525 switch (cell_info[row - 1][column].multicolumn) {
526 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
527 bottom = cell_info[row - 1][column].bottom_line;
529 case LyXTabular::CELL_NORMAL:
530 bottom = row_info[row - 1].bottom_line;
533 for (column = 0; column < columns_ - 1 && top; ++column) {
534 switch (cell_info[row][column].multicolumn){
535 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
536 top = cell_info[row][column].top_line;
538 case LyXTabular::CELL_NORMAL:
539 top = row_info[row].top_line;
543 return WIDTH_OF_LINE;
548 int LyXTabular::GetAdditionalWidth(int cell) const
550 // internally already set in SetWidthOfCell
551 // used to get it back in text.C
552 int col = right_column_of_cell(cell);
553 if (col < columns_ - 1 && column_info[col].right_line &&
554 column_info[col+1].left_line)
555 return WIDTH_OF_LINE;
561 // returns the maximum over all rows
562 int LyXTabular::GetWidthOfColumn(int cell) const
564 int column1 = column_of_cell(cell);
565 int column2 = right_column_of_cell(cell);
568 for (; i <= column2; ++i) {
569 result += column_info[i].width_of_column;
575 int LyXTabular::GetWidthOfTabular() const
577 return width_of_tabular;
581 /* returns 1 if a complete update is necessary, otherwise 0 */
582 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
584 if (!IsMultiColumn(cell))
587 int row = row_of_cell(cell);
588 int column1 = column_of_cell(cell);
589 int column2 = right_column_of_cell(cell);
591 // first set columns to 0 so we can calculate the right width
593 for (; i <= column2; ++i) {
594 cell_info[row][i].width_of_cell = 0;
596 // set the width to MAX_WIDTH until width > 0
597 int width = (new_width + 2 * WIDTH_OF_LINE);
598 for (i = column1; (i < column2) && (width > 0); ++i) {
599 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
600 width -= column_info[i].width_of_column;
603 cell_info[row][i].width_of_cell = width;
609 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
611 int row = row_of_cell(cell);
612 int column1 = column_of_cell(cell);
613 int column2 = right_column_of_cell(cell);
615 // first set columns to 0 so we can calculate the right width
617 for (; i <= column2; ++i)
618 cell_info[row][i].width_of_cell = 0;
619 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
621 if (i < numberofcells)
622 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
623 SetWidthOfMulticolCell(cell, new_width);
627 /* returns 1 if a complete update is necessary, otherwise 0 */
628 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
630 int row = row_of_cell(cell);
631 int column1 = column_of_cell(cell);
635 if (IsMultiColumn(cell)) {
636 tmp = SetWidthOfMulticolCell(cell, new_width);
638 width = (new_width + 2*WIDTH_OF_LINE);
639 cell_info[row][column1].width_of_cell = width;
640 if (column_info[column1].right_line && (column1 < columns_-1) &&
641 column_info[column1+1].left_line) // additional width
642 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
643 tmp = calculate_width_of_column_NMC(column1);
647 for(i = 0; i<columns_;++i)
648 calculate_width_of_column_NMC(i);
649 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
652 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
653 for(i = 0; i<columns_;++i)
654 calculate_width_of_column(i);
655 calculate_width_of_tabular();
662 bool LyXTabular::SetAlignment(int cell, char align)
664 if (!IsMultiColumn(cell))
665 column_info[column_of_cell(cell)].alignment = align;
666 cellinfo_of_cell(cell)->alignment = align;
671 bool LyXTabular::SetPWidth(int cell, string const & width)
673 bool flag = !width.empty();
675 if (IsMultiColumn(cell)) {
676 cellinfo_of_cell(cell)->p_width = width;
677 GetCellInset(cell)->SetAutoBreakRows(flag);
679 int j = column_of_cell(cell);
681 column_info[j].p_width = width;
682 if (flag) // do this only if there is a width
683 SetAlignment(cell, LYX_ALIGN_LEFT);
684 for(int i=0; i < rows_; ++i) {
685 c = GetCellNumber(i, j);
686 flag = !GetPWidth(c).empty(); // because of multicolumns!
687 GetCellInset(c)->SetAutoBreakRows(flag);
694 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
696 if (what == SET_SPECIAL_MULTI)
697 cellinfo_of_cell(cell)->align_special = special;
699 column_info[column_of_cell(cell)].align_special = special;
704 bool LyXTabular::SetAllLines(int cell, bool line)
706 SetTopLine(cell, line);
707 SetBottomLine(cell, line);
708 SetRightLine(cell, line);
709 SetLeftLine(cell, line);
714 bool LyXTabular::SetTopLine(int cell, bool line)
716 int row = row_of_cell(cell);
718 if (!IsMultiColumn(cell))
719 row_info[row].top_line = line;
721 cellinfo_of_cell(cell)->top_line = line;
726 bool LyXTabular::SetBottomLine(int cell, bool line)
728 if (!IsMultiColumn(cell))
729 row_info[row_of_cell(cell)].bottom_line = line;
731 cellinfo_of_cell(cell)->bottom_line = line;
736 bool LyXTabular::SetLeftLine(int cell, bool line)
738 column_info[column_of_cell(cell)].left_line = line;
743 bool LyXTabular::SetRightLine(int cell, bool line)
745 column_info[right_column_of_cell(cell)].right_line = line;
750 char LyXTabular::GetAlignment(int cell) const
752 if (IsMultiColumn(cell))
753 return cellinfo_of_cell(cell)->alignment;
755 return column_info[column_of_cell(cell)].alignment;
759 string LyXTabular::GetPWidth(int cell) const
761 if (IsMultiColumn(cell))
762 return cellinfo_of_cell(cell)->p_width;
763 return column_info[column_of_cell(cell)].p_width;
767 string LyXTabular::GetAlignSpecial(int cell, int what) const
769 if (what == SET_SPECIAL_MULTI)
770 return cellinfo_of_cell(cell)->align_special;
771 return column_info[column_of_cell(cell)].align_special;
775 int LyXTabular::GetWidthOfCell(int cell) const
777 int row = row_of_cell(cell);
778 int column1 = column_of_cell(cell);
779 int column2 = right_column_of_cell(cell);
782 for (; i <= column2; ++i) {
783 result += cell_info[row][i].width_of_cell;
786 // result += GetAdditionalWidth(cell);
791 int LyXTabular::GetBeginningOfTextInCell(int cell) const
795 switch (GetAlignment(cell)){
796 case LYX_ALIGN_CENTER:
797 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
799 case LYX_ALIGN_RIGHT:
800 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
801 // + GetAdditionalWidth(cell);
803 default: /* LYX_ALIGN_LEFT: nothing :-) */
813 bool LyXTabular::IsFirstCellInRow(int cell) const
815 return (column_of_cell(cell) == 0);
819 int LyXTabular::GetFirstCellInRow(int row) const
823 return cell_info[row][0].cellno;
826 bool LyXTabular::IsLastCellInRow(int cell) const
828 return (right_column_of_cell(cell) == (columns_ - 1));
832 int LyXTabular::GetLastCellInRow(int row) const
836 return cell_info[row][columns_-1].cellno;
840 bool LyXTabular::calculate_width_of_column(int column)
842 int old_column_width = column_info[column].width_of_column;
845 for (int i = 0; i < rows_; ++i) {
846 maximum = max(cell_info[i][column].width_of_cell, maximum);
848 column_info[column].width_of_column = maximum;
849 return (column_info[column].width_of_column != old_column_width);
853 bool LyXTabular::calculate_width_of_column_NMC(int column)
855 int old_column_width = column_info[column].width_of_column;
857 for (int i = 0; i < rows_; ++i) {
858 if (!IsMultiColumn(GetCellNumber(i, column)) &&
859 (cell_info[i][column].width_of_cell > max)) {
860 max = cell_info[i][column].width_of_cell;
863 column_info[column].width_of_column = max;
864 return (column_info[column].width_of_column != old_column_width);
868 void LyXTabular::calculate_width_of_tabular()
870 width_of_tabular = 0;
871 for (int i = 0; i < columns_; ++i) {
872 width_of_tabular += column_info[i].width_of_column;
877 int LyXTabular::row_of_cell(int cell) const
879 if (cell >= numberofcells)
883 return rowofcell[cell];
887 int LyXTabular::column_of_cell(int cell) const
889 if (cell >= numberofcells)
893 return columnofcell[cell];
897 int LyXTabular::right_column_of_cell(int cell) const
899 int row = row_of_cell(cell);
900 int column = column_of_cell(cell);
901 while (column < (columns_ - 1) &&
902 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
908 void LyXTabular::Write(ostream & os) const
913 os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
915 // global longtable options
916 os << "<Features rotate=" << rotate <<
917 " islongtable=" << is_long_tabular <<
918 " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
919 " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
921 for (i = 0; i < rows_; ++i) {
922 os << "<Row topline=" << row_info[i].top_line <<
923 " bottomline=" << row_info[i].bottom_line <<
924 " newpage=" << row_info[i].newpage <<
926 for (j = 0; j < columns_; ++j) {
928 os << "<Column alignment=" << column_info[j].alignment <<
929 " leftline=" << column_info[j].left_line <<
930 " rightline=" << column_info[j].right_line <<
931 " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
932 "\" special=\"" << column_info[j].align_special <<
935 os << "<Column>" << endl;
937 os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
938 " alignment=" << cell_info[i][j].alignment <<
939 " topline=" << cell_info[i][j].top_line <<
940 " bottomline=" << cell_info[i][j].bottom_line <<
941 " rotate=" << cell_info[i][j].rotate <<
942 " linebreaks=" << cell_info[i][j].linebreaks <<
943 " width=\"" << cell_info[i][j].p_width <<
944 "\" special=\"" << cell_info[i][j].align_special <<
946 os << "\\begin_inset ";
947 cell_info[i][j].inset->Write(os);
948 os << "\n\\end_inset " << endl;
949 os << "</Cell>" << endl;
950 os << "</Column>" << endl;
952 os << "</Row>" << endl;
954 os << "</LyXTabular>" << endl;
959 bool getTokenValue(string const str, const char * token, string & ret)
961 int pos = str.find(token);
962 char ch = str[pos+strlen(token)];
964 if ((pos < 0) || (ch != '='))
967 pos += strlen(token)+1;
969 if ((ch != '"') && (ch != '\'')) { // only read till next space
973 while((pos < int(str.length()-1)) && (str[++pos] != ch))
981 bool getTokenValue(string const str, const char * token, int & num)
984 int pos = str.find(token);
985 char ch = str[pos+strlen(token)];
987 if ((pos < 0) || (ch != '='))
990 pos += strlen(token)+1;
992 if ((ch != '"') && (ch != '\'')) { // only read till next space
998 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1001 num = strToInt(ret);
1007 bool getTokenValue(string const str, const char * token, bool & flag)
1010 int pos = str.find(token);
1011 char ch = str[pos+strlen(token)];
1013 if ((pos < 0) || (ch != '='))
1016 pos += strlen(token)+1;
1018 if ((ch != '"') && (ch != '\'')) { // only read till next space
1024 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1027 flag = strToInt(ret);
1032 void l_getline(istream & is, string & str)
1040 void LyXTabular::Read(LyXLex & lex)
1043 istream & is = lex.getStream();
1045 l_getline(is, line);
1046 if (!prefixIs(line, "<LyXTabular ")) {
1047 OldFormatRead(lex, line);
1054 if (!getTokenValue(line, "version", version))
1056 if (!getTokenValue(line, "rows", rows_arg))
1058 if (!getTokenValue(line, "columns", columns_arg))
1060 Init(rows_arg, columns_arg);
1061 l_getline(is, line);
1062 if (!prefixIs(line, "<Features ")) {
1063 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1064 line << ")" << endl;
1067 (void)getTokenValue(line, "islongtable", is_long_tabular);
1068 (void)getTokenValue(line, "endhead", endhead);
1069 (void)getTokenValue(line, "endfirsthead", endfirsthead);
1070 (void)getTokenValue(line, "endfoot", endfoot);
1071 (void)getTokenValue(line, "endlastfoot", endlastfoot);
1073 for(i = 0; i < rows_; ++i) {
1074 l_getline(is, line);
1075 if (!prefixIs(line, "<Row ")) {
1076 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1077 line << ")" << endl;
1080 (void)getTokenValue(line, "topline", row_info[i].top_line);
1081 (void)getTokenValue(line, "bottomline", row_info[i].bottom_line);
1082 (void)getTokenValue(line, "newpage", row_info[i].newpage);
1083 for (j = 0; j < columns_; ++j) {
1085 if (!prefixIs(line,"<Column")) {
1086 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1087 line << ")" << endl;
1091 (void)getTokenValue(line, "alignment", column_info[j].alignment);
1092 (void)getTokenValue(line, "leftline", column_info[j].left_line);
1093 (void)getTokenValue(line, "rightline", column_info[j].right_line);
1094 (void)getTokenValue(line, "width", column_info[j].p_width);
1095 (void)getTokenValue(line, "special", column_info[j].align_special);
1097 l_getline(is, line);
1098 if (!prefixIs(line, "<Cell")) {
1099 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1100 line << ")" << endl;
1103 (void)getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1104 (void)getTokenValue(line, "alignment", cell_info[i][j].alignment);
1105 (void)getTokenValue(line, "topline", cell_info[i][j].top_line);
1106 (void)getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1107 (void)getTokenValue(line, "rotate", cell_info[i][j].rotate);
1108 (void)getTokenValue(line, "linebreaks", cell_info[i][j].linebreaks);
1109 (void)getTokenValue(line, "width", cell_info[i][j].p_width);
1110 (void)getTokenValue(line, "special", cell_info[i][j].align_special);
1111 l_getline(is, line);
1112 if (prefixIs(line, "\\begin_inset")) {
1113 cell_info[i][j].inset->Read(lex);
1114 l_getline(is, line);
1116 if (line != "</Cell>") {
1117 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1118 line << ")" << endl;
1121 l_getline(is, line);
1122 if (line != "</Column>") {
1123 lyxerr << "Wrong tabular format (expected </Column> got" <<
1124 line << ")" << endl;
1128 l_getline(is, line);
1129 if (line != "</Row>") {
1130 lyxerr << "Wrong tabular format (expected </Row> got" <<
1131 line << ")" << endl;
1135 while (line != "</LyXTabular>") {
1136 l_getline(is, line);
1138 set_row_column_number_info();
1142 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1147 int columns_arg = 0;
1148 int is_long_tabular_arg = false;
1149 int rotate_arg = false;
1159 istream & is = lex.getStream();
1162 version = atoi(s.c_str() + 8);
1166 vector<int> cont_row_info;
1169 lyxerr << "Tabular format < 5 is not supported anymore\n"
1170 "Get an older version of LyX (< 1.1.x) for conversion!"
1172 WriteAlert(_("Warning:"),
1173 _("Tabular format < 5 is not supported anymore\n"),
1174 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1176 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1177 >> rotate_arg >> a >> b >> c >> d;
1179 is >> rows_arg >> columns_arg;
1180 Init(rows_arg, columns_arg);
1181 cont_row_info = vector<int>(rows_arg);
1182 SetLongTabular(is_long_tabular_arg);
1183 SetRotateTabular(rotate_arg);
1185 for (i = 0; i < rows_; ++i) {
1187 cont_row_info[i] = false;
1189 for (i = 0; i < columns_; ++i) {
1192 for (i = 0; i < rows_; ++i) {
1193 for (j = 0; j < columns_; ++j) {
1198 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1199 >> rotate_arg >> a >> b >> c >> d;
1200 Init(rows_arg, columns_arg);
1201 cont_row_info = vector<int>(rows_arg);
1202 SetLongTabular(is_long_tabular_arg);
1203 SetRotateTabular(rotate_arg);
1208 for (i = 0; i < rows_; ++i) {
1209 a = b = c = d = e = f = g = h = 0;
1210 is >> a >> b >> c >> d;
1211 row_info[i].top_line = a;
1212 row_info[i].bottom_line = b;
1213 cont_row_info[i] = c;
1214 row_info[i].newpage = d;
1216 for (i = 0; i < columns_; ++i) {
1220 char ch; // skip '"'
1222 getline(is, s1, '"');
1223 is >> ch; // skip '"'
1224 getline(is, s2, '"');
1225 column_info[i].alignment = static_cast<char>(a);
1226 column_info[i].left_line = b;
1227 column_info[i].right_line = c;
1228 column_info[i].p_width = s1;
1229 column_info[i].align_special = s2;
1231 for (i = 0; i < rows_; ++i) {
1232 for (j = 0; j < columns_; ++j) {
1235 is >> a >> b >> c >> d >> e >> f >> g;
1237 is >> ch; // skip '"'
1238 getline(is, s1, '"');
1239 is >> ch; // skip '"'
1240 getline(is, s2, '"');
1241 cell_info[i][j].multicolumn = static_cast<char>(a);
1242 cell_info[i][j].alignment = static_cast<char>(b);
1243 cell_info[i][j].top_line = static_cast<char>(c);
1244 cell_info[i][j].bottom_line = static_cast<char>(d);
1245 cell_info[i][j].rotate = static_cast<bool>(f);
1246 cell_info[i][j].linebreaks = static_cast<bool>(g);
1247 cell_info[i][j].align_special = s1;
1248 cell_info[i][j].p_width = s2;
1252 set_row_column_number_info();
1254 LyXParagraph * par = new LyXParagraph;
1255 LyXParagraph * return_par = 0;
1256 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1257 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1258 string token, tmptok;
1261 LyXFont font(LyXFont::ALL_SANE);
1263 while (lex.IsOK()) {
1265 token = lex.GetString();
1268 if ((token == "\\layout") || (token == "\\end_float") ||
1269 (token == "\\end_deeper"))
1271 lex.pushToken(token);
1274 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1282 lex.pushToken(token);
1286 lex.printError("New Paragraph allocated! This should not happen!");
1287 lex.pushToken(token);
1293 // now we have the par we should fill the insets with this!
1295 InsetText *inset = GetCellInset(cell);
1298 for(int i=0; i < par->Last(); ++i) {
1299 if (par->IsNewline(i)) {
1301 if (cell > GetNumberOfCells()) {
1302 lyxerr << "Some error in reading old table format occured!" <<
1303 endl << "Terminating when reading cell[" << cell << "]!" <<
1307 row = row_of_cell(cell);
1308 if (cont_row_info[row]) {
1310 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1311 while(!IsFirstCellInRow(--cell));
1313 inset = GetCellInset(cell);
1316 inset = GetCellInset(cell);
1317 row = row_of_cell(cell);
1318 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].linebreaks)
1320 // insert a space instead
1322 par->InsertChar(i, ' ');
1325 par->CopyIntoMinibuffer(i);
1326 inset->par->InsertFromMinibuffer(inset->par->Last());
1332 char const * LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1334 int i = isColumn ? cell : column_of_cell(cell);
1339 //i = column_of_cell(cell);
1340 if (!isColumn && IsMultiColumn(cell)) {
1341 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1342 return cellinfo_of_cell(cell)->align_special.c_str();
1344 switch (GetAlignment(cell)) {
1345 case LYX_ALIGN_LEFT:
1347 case LYX_ALIGN_RIGHT:
1354 if (!column_info[i].align_special.empty()) {
1355 return column_info[i].align_special.c_str();
1357 #ifdef IGNORE_THIS_FOR_NOW
1358 else if (!column_info[i].p_width.empty()) {
1360 file += column_info[i].p_width;
1365 switch (column_info[i].alignment) {
1366 case LYX_ALIGN_LEFT:
1368 case LYX_ALIGN_RIGHT:
1378 // cell <0 will tex the preamble
1379 // returns the number of printed newlines
1380 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1384 //int tmp; // tmp2; // unused
1385 int nvcell; // fcell; // unused
1386 if (IsLastCell(cell)) {
1387 os << newlineAndDepth(--depth)
1389 << newlineAndDepth(--depth)
1391 << newlineAndDepth(--depth)
1393 << newlineAndDepth(--depth);
1394 if (is_long_tabular)
1398 << newlineAndDepth(--depth);
1404 if (is_long_tabular)
1410 << "' COLSEP='1' ROWSEP='1'>"
1411 << newlineAndDepth(++depth);
1413 for (i = 0; i < columns_; ++i) {
1414 os << "<COLSPEC ALIGN='"
1415 << GetDocBookAlign(i, true)
1421 if (i == (columns_-1)) {
1424 if (column_info[i].right_line ||
1425 column_info[i+1].left_line)
1431 << newlineAndDepth(depth);
1433 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1434 if (column_info[i].left_line)
1439 << newlineAndDepth(++depth)
1441 << newlineAndDepth(++depth)
1443 << GetDocBookAlign(0)
1445 if (IsMultiColumn(0)) {
1446 os << " NAMEST='col1' NAMEEND='col"
1447 << cells_in_multicolumn(0)
1451 << newlineAndDepth(++depth);
1454 if (IsLastCellInRow(cell)) {
1455 os << newlineAndDepth(--depth)
1457 << newlineAndDepth(--depth)
1459 << newlineAndDepth(depth)
1461 << newlineAndDepth(++depth)
1463 << GetDocBookAlign(cell + 1)
1464 << "' VALIGN='middle'";
1465 if (IsMultiColumn(cell + 1)) {
1466 os << " NAMEST='col"
1467 << column_of_cell(cell+1) + 1
1469 << column_of_cell(cell + 1) +
1470 cells_in_multicolumn(cell + 1)
1474 << newlineAndDepth(++depth);
1477 os << newlineAndDepth(--depth)
1479 << newlineAndDepth(depth)
1481 << GetDocBookAlign(cell + 1)
1482 << "' VALIGN='middle'";
1483 if (IsMultiColumn(cell + 1)) {
1484 os << " NAMEST='col"
1485 << column_of_cell(cell+1) + 1
1487 << column_of_cell(cell+1) +
1488 cells_in_multicolumn(cell+1)
1492 << newlineAndDepth(++depth);
1501 bool LyXTabular::IsMultiColumn(int cell) const
1503 return (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1507 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1509 int row = row_of_cell(cell);
1510 int column = column_of_cell(cell);
1511 return &cell_info[row][column];
1515 void LyXTabular::SetMultiColumn(int cell, int number)
1517 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1518 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1519 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1520 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1521 for (number--; number > 0; --number) {
1522 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1524 set_row_column_number_info();
1528 int LyXTabular::cells_in_multicolumn(int cell) const
1530 int row = row_of_cell(cell);
1531 int column = column_of_cell(cell);
1534 while ((column < columns_) &&
1535 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1544 int LyXTabular::UnsetMultiColumn(int cell)
1546 int row = row_of_cell(cell);
1547 int column = column_of_cell(cell);
1551 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1552 cell_info[row][column].multicolumn = CELL_NORMAL;
1554 while ((column < columns_) &&
1555 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1557 cell_info[row][column].multicolumn = CELL_NORMAL;
1562 set_row_column_number_info();
1567 void LyXTabular::SetLongTabular(int what)
1569 is_long_tabular = what;
1573 bool LyXTabular::IsLongTabular() const
1575 return is_long_tabular;
1579 void LyXTabular::SetRotateTabular(int what)
1585 bool LyXTabular::GetRotateTabular() const
1591 void LyXTabular::SetRotateCell(int cell, int what)
1593 cellinfo_of_cell(cell)->rotate = what;
1597 bool LyXTabular::GetRotateCell(int cell) const
1599 return cellinfo_of_cell(cell)->rotate;
1603 bool LyXTabular::NeedRotating() const
1607 for (int i = 0; i < rows_; ++i) {
1608 for (int j = 0; j < columns_; ++j) {
1609 if (cell_info[i][j].rotate)
1617 bool LyXTabular::IsLastCell(int cell) const
1619 if ((cell+1) < GetNumberOfCells())
1625 int LyXTabular::GetCellAbove(int cell) const
1627 if (row_of_cell(cell) > 0)
1628 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1633 int LyXTabular::GetCellBelow(int cell) const
1635 if (row_of_cell(cell)+1 < rows_)
1636 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1641 int LyXTabular::GetLastCellAbove(int cell) const
1643 if (row_of_cell(cell) <= 0)
1645 if (!IsMultiColumn(cell))
1646 return GetCellAbove(cell);
1647 return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
1651 int LyXTabular::GetLastCellBelow(int cell) const
1653 if (row_of_cell(cell)+1 >= rows_)
1655 if (!IsMultiColumn(cell))
1656 return GetCellBelow(cell);
1657 return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
1661 int LyXTabular::GetCellNumber(int row, int column) const
1663 if (column >= columns_)
1664 column = columns_ - 1;
1665 else if (column < 0)
1672 return cell_info[row][column].cellno;
1676 void LyXTabular::SetLinebreaks(int cell, bool what)
1678 cellinfo_of_cell(cell)->linebreaks = what;
1682 bool LyXTabular::GetLinebreaks(int cell) const
1684 if (column_info[column_of_cell(cell)].p_width.empty() &&
1685 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1687 return cellinfo_of_cell(cell)->linebreaks;
1691 void LyXTabular::SetLTHead(int cell, bool first)
1693 int row = row_of_cell(cell);
1696 if (row == endfirsthead)
1709 bool LyXTabular::GetRowOfLTHead(int cell) const
1711 if ((endhead+1) > rows_)
1713 return (row_of_cell(cell) == endhead);
1717 bool LyXTabular::GetRowOfLTFirstHead(int cell) const
1719 if ((endfirsthead+1) > rows_)
1721 return (row_of_cell(cell) == endfirsthead);
1725 void LyXTabular::SetLTFoot(int cell, bool last)
1727 int row = row_of_cell(cell);
1730 if (row == endlastfoot)
1743 bool LyXTabular::GetRowOfLTFoot(int cell) const
1745 if ((endfoot+1) > rows_)
1747 return (row_of_cell(cell) == endfoot);
1750 bool LyXTabular::GetRowOfLTLastFoot(int cell) const
1752 if ((endlastfoot+1) > rows_)
1754 return (row_of_cell(cell) == endlastfoot);
1758 void LyXTabular::SetLTNewPage(int cell, bool what)
1760 row_info[row_of_cell(cell)].newpage = what;
1764 bool LyXTabular::GetLTNewPage(int cell) const
1766 return row_info[row_of_cell(cell)].newpage;
1770 void LyXTabular::SetAscentOfRow(int row, int height)
1774 row_info[row].ascent_of_row = height;
1778 void LyXTabular::SetDescentOfRow(int row, int height)
1782 row_info[row].descent_of_row = height;
1786 int LyXTabular::GetAscentOfRow(int row) const
1790 return row_info[row].ascent_of_row;
1794 int LyXTabular::GetDescentOfRow(int row) const
1798 return row_info[row].descent_of_row;
1802 int LyXTabular::GetHeightOfTabular() const
1806 for(int row = 0; row < rows_; ++row)
1807 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1808 GetAdditionalHeight(GetCellNumber(row, 0));
1813 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1815 if ((row >= rows_) || (column >= columns_))
1817 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1821 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1823 int fcell = GetFirstCellInRow(row);
1824 int n = NumberOfCellsInRow(fcell) + fcell;
1828 for (i = fcell; i < n; ++i) {
1832 if (tmp == (n - fcell)){
1835 for (i = fcell; i < n; ++i) {
1838 << column_of_cell(i) + 1
1840 << right_column_of_cell(i) + 1
1853 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1855 int fcell = GetFirstCellInRow(row);
1856 int n = NumberOfCellsInRow(fcell) + fcell;
1860 for (i = fcell; i < n; ++i) {
1864 if (tmp == (n-fcell)){
1867 for (i = fcell; i < n; ++i) {
1868 if (BottomLine(i)) {
1870 << column_of_cell(i) + 1
1872 << right_column_of_cell(i) + 1
1885 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1889 if (GetRotateCell(cell)) {
1890 os << "\\begin{sideways}" << endl;
1893 if (IsMultiColumn(cell)) {
1894 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1895 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1896 os << cellinfo_of_cell(cell+1)->align_special << "}{";
1900 if (!GetPWidth(cell).empty()) {
1901 os << "p{" << GetPWidth(cell) << '}';
1903 switch (GetAlignment(cell)) {
1904 case LYX_ALIGN_LEFT:
1907 case LYX_ALIGN_RIGHT:
1915 if (RightLine(cell))
1917 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1923 if (GetLinebreaks(cell)) {
1924 os << "\\parbox[t]{" << GetPWidth(cell) << "}{\\smallskip{}";
1930 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1935 if (GetLinebreaks(cell))
1936 os << "\\smallskip{}}";
1937 if (IsMultiColumn(cell)){
1940 if (GetRotateCell(cell)) {
1941 os << endl << "\\end{sideways}";
1948 int LyXTabular::Latex(ostream & os, bool fragile, bool fp) const
1954 //+---------------------------------------------------------------------
1955 //+ first the opening preamble +
1956 //+---------------------------------------------------------------------
1959 os << "\\begin{sideways}" << endl;
1962 if (is_long_tabular)
1963 os << "\\begin{longtable}{";
1965 os << "\\begin{tabular}{";
1966 for (i = 0; i < columns_; ++i) {
1967 if (column_info[i].left_line)
1969 if (!column_info[i].align_special.empty()) {
1970 os << column_info[i].align_special;
1971 } else if (!column_info[i].p_width.empty()) {
1973 << column_info[i].p_width
1976 switch (column_info[i].alignment) {
1977 case LYX_ALIGN_LEFT:
1980 case LYX_ALIGN_RIGHT:
1988 if (column_info[i].right_line)
1994 //+---------------------------------------------------------------------
1995 //+ the single row and columns (cells) +
1996 //+---------------------------------------------------------------------
1998 for(i=0; i < rows_; ++i) {
1999 ret += TeXTopHLine(os, i);
2000 for(j=0; j < columns_; ++j) {
2001 if (IsPartOfMultiColumn(i,j))
2003 ret += TeXCellPreamble(os, cell);
2004 ret += GetCellInset(cell)->Latex(os, fragile, fp);
2005 ret += TeXCellPostamble(os, cell);
2006 if (!IsLastCellInRow(cell)) { // not last cell in row
2012 os << "\\\\" << endl;
2013 ret += TeXBottomHLine(os, i);
2014 if (IsLongTabular()) {
2016 os << "\\endhead\n";
2019 if (i == endfirsthead) {
2020 os << "\\endfirsthead\n";
2024 os << "\\endfoot\n";
2027 if (i == endlastfoot) {
2028 os << "\\endlastfoot\n";
2031 if (row_info[i].newpage) {
2032 os << "\\newpage\n";
2038 //+---------------------------------------------------------------------
2039 //+ the closing of the tabular +
2040 //+---------------------------------------------------------------------
2042 if (is_long_tabular)
2043 os << "\\end{longtable}";
2045 os << "\\end{tabular}";
2047 os << "\n\\end{sideways}";
2055 InsetText * LyXTabular::GetCellInset(int cell) const
2057 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2060 void LyXTabular::Validate(LaTeXFeatures & features) const
2062 if (IsLongTabular())
2063 features.longtable = true;
2065 features.rotating = true;
2066 for(int cell = 0; cell < numberofcells; ++cell)
2067 GetCellInset(cell)->Validate(features);