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;
60 LyXTabular::rowstruct::rowstruct()
70 LyXTabular::columnstruct::columnstruct()
74 alignment = LYX_ALIGN_CENTER;
80 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
83 Init(rows_arg, columns_arg);
87 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
90 Init(lt.rows_, lt.columns_);
96 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
103 LyXTabular::~LyXTabular()
106 delete[] columnofcell;
110 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
112 // If this and lt is not of the same size we have a serious bug
113 // So then it is ok to throw an exception, or for now
115 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
117 cell_info = lt.cell_info;
118 row_info = lt.row_info;
119 column_info = lt.column_info;
121 // long tabular stuff
122 SetLongTabular(lt.is_long_tabular);
123 endhead = lt.endhead;
124 endfoot = lt.endfoot;
125 endfirsthead = lt.endfirsthead;
126 endlastfoot = lt.endlastfoot;
136 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
138 LyXTabular * result = new LyXTabular(inset, *this);
140 // don't know if this is good but I need to Clone also
141 // the text-insets here, this is for the Undo-facility!
144 for(i=0; i < rows_; ++i) {
145 for(j=0; j < columns_; ++j) {
146 result->cell_info[i][j].inset = cell_info[i][j].inset;
147 result->cell_info[i][j].inset.setOwner(inset);
154 /* activates all lines and sets all widths to 0 */
155 void LyXTabular::Init(int rows_arg, int columns_arg)
161 columns_ = columns_arg;
162 row_info = vector<rowstruct>(rows_, rowstruct());
163 column_info = vector<columnstruct>(columns_, columnstruct());
164 cell_info = vector<vector<cellstruct> >
165 (rows_, vector<cellstruct>(columns_, cellstruct()));
167 // Jürgen, use iterators.
168 for (i = 0; i < rows_; ++i) {
169 for (j = 0; j < columns_; ++j) {
170 cell_info[i][j].inset.setOwner(owner_);
171 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
172 cell_info[i][j].cellno = cellno++;
175 row_info[i-1].bottom_line = true;
176 row_info[0].bottom_line = true;
178 for (i = 0; i < columns_; ++i) {
179 calculate_width_of_column(i);
181 column_info[columns_-1].right_line = true;
183 calculate_width_of_tabular();
187 set_row_column_number_info();
188 is_long_tabular = false;
197 void LyXTabular::AppendRow(int cell )
201 int row = row_of_cell(cell);
203 row_vector::iterator rit = row_info.begin() + row;
204 row_info.insert(rit, rowstruct());
207 cell_vvector::iterator cit = cell_info.begin() + row;
208 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
210 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
213 for(int i = 0; i <= row; ++i) {
214 for(int j = 0; j < columns_; ++j) {
215 c_info[i][j] = cell_info[i][j];
218 for(int i = row+1; i < rows_; ++i) {
219 for(int j = 0; j < columns_; ++j) {
220 c_info[i][j] = cell_info[i-1][j];
225 for (int j = 0; j < columns_; ++j) {
226 cell_info[row][j].inset.clear();
233 void LyXTabular::DeleteRow(int row)
237 row_info.erase(row_info.begin() + row); //&row_info[row]);
238 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
244 void LyXTabular::AppendColumn(int cell)
248 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
250 int column = column_of_cell(cell);
252 column_vector::iterator cit = column_info.begin() + column;
253 column_info.insert(cit, columnstruct());
255 for (i = 0; i < rows_; ++i) {
256 for (j = 0; j <= column; ++j) {
257 c_info[i][j] = cell_info[i][j];
259 for (j = column+1; j < columns_; ++j) {
260 c_info[i][j] = cell_info[i][j-1];
262 // care about multicolumns
263 if (cell_info[i][column+1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
264 cell_info[i][column+1].multicolumn = CELL_PART_OF_MULTICOLUMN;
266 if (((column+1) == columns_) ||
267 (cell_info[i][column+2].multicolumn != CELL_PART_OF_MULTICOLUMN)) {
268 cell_info[i][column+1].multicolumn = LyXTabular::CELL_NORMAL;
273 for (i = 0; i < rows_; ++i) {
274 cell_info[i][column].inset.clear();
280 void LyXTabular::DeleteColumn(int column)
284 column_info.erase(column_info.begin() + column);
285 for (int i = 0; i < rows_; ++i) {
286 cell_info[i].erase(cell_info[i].begin() + column);
293 void LyXTabular::Reinit()
299 // Jürgen, use iterators.
300 for (; i < rows_; ++i) {
301 for (j = 0; j < columns_; ++j) {
302 cell_info[i][j].width_of_cell = 0;
303 cell_info[i][j].inset.setOwner(owner_);
307 for (i = 0; i < columns_; ++i) {
308 calculate_width_of_column(i);
310 calculate_width_of_tabular();
312 set_row_column_number_info();
316 void LyXTabular::set_row_column_number_info()
322 for (; row < rows_; ++row) {
323 for (column = 0; column<columns_; ++column) {
324 if (cell_info[row][column].multicolumn
325 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
327 cell_info[row][column].cellno = numberofcells;
330 ++numberofcells; // because this is one more than as we start from 0
335 rowofcell = new int[numberofcells];
336 delete [] columnofcell;
337 columnofcell = new int[numberofcells];
339 while (c < numberofcells && row < rows_ && column < columns_) {
341 columnofcell[c] = column;
345 } while (column < columns_ &&
346 cell_info[row][column].multicolumn
347 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
348 if (column == columns_) {
353 for (row = 0; row < rows_; ++row) {
354 for (column = 0; column<columns_; ++column) {
355 if (IsPartOfMultiColumn(row,column))
357 cell_info[row][column].inset.SetAutoBreakRows(
358 !GetPWidth(GetCellNumber(row, column)).empty());
364 int LyXTabular::GetNumberOfCells() const
366 return numberofcells;
370 int LyXTabular::NumberOfCellsInRow(int cell) const
372 int row = row_of_cell(cell);
374 for (int i = 0; i < columns_; ++i) {
375 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
382 /* returns 1 if there is a topline, returns 0 if not */
383 bool LyXTabular::TopLine(int cell) const
385 int row = row_of_cell(cell);
387 if (IsMultiColumn(cell))
388 return cellinfo_of_cell(cell)->top_line;
389 return row_info[row].top_line;
393 bool LyXTabular::BottomLine(int cell) const
395 //no bottom line underneath non-existent cells if you please
396 if(cell >= numberofcells)
399 if (IsMultiColumn(cell))
400 return cellinfo_of_cell(cell)->bottom_line;
401 return row_info[row_of_cell(cell)].bottom_line;
405 bool LyXTabular::LeftLine(int cell) const
407 return column_info[column_of_cell(cell)].left_line;
411 bool LyXTabular::RightLine(int cell) const
413 return column_info[right_column_of_cell(cell)].right_line;
417 bool LyXTabular::TopAlreadyDrawed(int cell) const
419 if (GetAdditionalHeight(cell))
421 int row = row_of_cell(cell);
423 int column = column_of_cell(cell);
426 && cell_info[row][column].multicolumn
427 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
429 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
430 return row_info[row].bottom_line;
432 return cell_info[row][column].bottom_line;
438 bool LyXTabular::LeftAlreadyDrawed(int cell) const
440 int column = column_of_cell(cell);
442 int row = row_of_cell(cell);
444 (cell_info[row][column].multicolumn ==
445 LyXTabular::CELL_PART_OF_MULTICOLUMN));
446 if (GetAdditionalWidth(cell_info[row][column].cellno))
448 return column_info[column].right_line;
454 bool LyXTabular::IsLastRow(int cell) const
456 return (row_of_cell(cell) == rows_ - 1);
460 int LyXTabular::GetAdditionalHeight(int cell) const
462 int row = row_of_cell(cell);
465 int top = 1; // bool top = true; ??
466 int bottom = 1; // bool bottom = true; ??
469 for (column = 0; column < columns_ - 1 && bottom; ++column) {
470 switch (cell_info[row - 1][column].multicolumn) {
471 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
472 bottom = cell_info[row - 1][column].bottom_line;
474 case LyXTabular::CELL_NORMAL:
475 bottom = row_info[row - 1].bottom_line;
478 for (column = 0; column < columns_ - 1 && top; ++column) {
479 switch (cell_info[row][column].multicolumn){
480 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
481 top = cell_info[row][column].top_line;
483 case LyXTabular::CELL_NORMAL:
484 top = row_info[row].top_line;
488 return WIDTH_OF_LINE;
493 int LyXTabular::GetAdditionalWidth(int cell) const
495 // internally already set in SetWidthOfCell
496 // used to get it back in text.C
497 int col = right_column_of_cell(cell);
498 if (col < columns_ - 1 && column_info[col].right_line &&
499 column_info[col+1].left_line)
500 return WIDTH_OF_LINE;
506 // returns the maximum over all rows
507 int LyXTabular::GetWidthOfColumn(int cell) const
509 int column1 = column_of_cell(cell);
510 int column2 = right_column_of_cell(cell);
513 for (; i <= column2; ++i) {
514 result += column_info[i].width_of_column;
520 int LyXTabular::GetWidthOfTabular() const
522 return width_of_tabular;
526 /* returns 1 if a complete update is necessary, otherwise 0 */
527 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
529 if (!IsMultiColumn(cell))
532 int row = row_of_cell(cell);
533 int column1 = column_of_cell(cell);
534 int column2 = right_column_of_cell(cell);
536 // first set columns to 0 so we can calculate the right width
538 for (; i <= column2; ++i) {
539 cell_info[row][i].width_of_cell = 0;
541 // set the width to MAX_WIDTH until width > 0
542 int width = (new_width + 2 * WIDTH_OF_LINE);
543 for (i = column1; (i < column2) && (width > 0); ++i) {
544 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
545 width -= column_info[i].width_of_column;
548 cell_info[row][i].width_of_cell = width;
554 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
556 int row = row_of_cell(cell);
557 int column1 = column_of_cell(cell);
558 int column2 = right_column_of_cell(cell);
560 // first set columns to 0 so we can calculate the right width
562 for (; i <= column2; ++i)
563 cell_info[row][i].width_of_cell = 0;
564 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
566 if (i < numberofcells)
567 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
568 SetWidthOfMulticolCell(cell, new_width);
572 /* returns 1 if a complete update is necessary, otherwise 0 */
573 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
575 int row = row_of_cell(cell);
576 int column1 = column_of_cell(cell);
580 if (IsMultiColumn(cell)) {
581 tmp = SetWidthOfMulticolCell(cell, new_width);
583 width = (new_width + 2*WIDTH_OF_LINE);
584 cell_info[row][column1].width_of_cell = width;
585 if (column_info[column1].right_line && (column1 < columns_-1) &&
586 column_info[column1+1].left_line) // additional width
587 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
588 tmp = calculate_width_of_column_NMC(column1);
592 for(i = 0; i<columns_;++i)
593 calculate_width_of_column_NMC(i);
594 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
597 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
598 for(i = 0; i<columns_;++i)
599 calculate_width_of_column(i);
600 calculate_width_of_tabular();
607 bool LyXTabular::SetAlignment(int cell, char align)
609 if (!IsMultiColumn(cell))
610 column_info[column_of_cell(cell)].alignment = align;
611 cellinfo_of_cell(cell)->alignment = align;
616 bool LyXTabular::SetPWidth(int cell, string const & width)
618 bool flag = !width.empty();
620 if (IsMultiColumn(cell)) {
621 cellinfo_of_cell(cell)->p_width = width;
622 GetCellInset(cell)->SetAutoBreakRows(flag);
624 int j = column_of_cell(cell);
626 column_info[j].p_width = width;
627 if (flag) // do this only if there is a width
628 SetAlignment(cell, LYX_ALIGN_LEFT);
629 for(int i=0; i < rows_; ++i) {
630 c = GetCellNumber(i, j);
631 flag = !GetPWidth(c).empty(); // because of multicolumns!
632 GetCellInset(c)->SetAutoBreakRows(flag);
639 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
641 if (what == SET_SPECIAL_MULTI)
642 cellinfo_of_cell(cell)->align_special = special;
644 column_info[column_of_cell(cell)].align_special = special;
649 bool LyXTabular::SetAllLines(int cell, bool line)
651 SetTopLine(cell, line);
652 SetBottomLine(cell, line);
653 SetRightLine(cell, line);
654 SetLeftLine(cell, line);
659 bool LyXTabular::SetTopLine(int cell, bool line)
661 int row = row_of_cell(cell);
663 if (!IsMultiColumn(cell))
664 row_info[row].top_line = line;
666 cellinfo_of_cell(cell)->top_line = line;
671 bool LyXTabular::SetBottomLine(int cell, bool line)
673 if (!IsMultiColumn(cell))
674 row_info[row_of_cell(cell)].bottom_line = line;
676 cellinfo_of_cell(cell)->bottom_line = line;
681 bool LyXTabular::SetLeftLine(int cell, bool line)
683 column_info[column_of_cell(cell)].left_line = line;
688 bool LyXTabular::SetRightLine(int cell, bool line)
690 column_info[right_column_of_cell(cell)].right_line = line;
695 char LyXTabular::GetAlignment(int cell) const
697 if (IsMultiColumn(cell))
698 return cellinfo_of_cell(cell)->alignment;
700 return column_info[column_of_cell(cell)].alignment;
704 string LyXTabular::GetPWidth(int cell) const
706 if (IsMultiColumn(cell))
707 return cellinfo_of_cell(cell)->p_width;
708 return column_info[column_of_cell(cell)].p_width;
712 string LyXTabular::GetAlignSpecial(int cell, int what) const
714 if (what == SET_SPECIAL_MULTI)
715 return cellinfo_of_cell(cell)->align_special;
716 return column_info[column_of_cell(cell)].align_special;
720 int LyXTabular::GetWidthOfCell(int cell) const
722 int row = row_of_cell(cell);
723 int column1 = column_of_cell(cell);
724 int column2 = right_column_of_cell(cell);
727 for (; i <= column2; ++i) {
728 result += cell_info[row][i].width_of_cell;
731 // result += GetAdditionalWidth(cell);
736 int LyXTabular::GetBeginningOfTextInCell(int cell) const
740 switch (GetAlignment(cell)){
741 case LYX_ALIGN_CENTER:
742 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
744 case LYX_ALIGN_RIGHT:
745 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
746 // + GetAdditionalWidth(cell);
748 default: /* LYX_ALIGN_LEFT: nothing :-) */
758 bool LyXTabular::IsFirstCellInRow(int cell) const
760 return (column_of_cell(cell) == 0);
764 int LyXTabular::GetFirstCellInRow(int row) const
768 return cell_info[row][0].cellno;
771 bool LyXTabular::IsLastCellInRow(int cell) const
773 return (right_column_of_cell(cell) == (columns_ - 1));
777 int LyXTabular::GetLastCellInRow(int row) const
781 return cell_info[row][columns_-1].cellno;
785 bool LyXTabular::calculate_width_of_column(int column)
787 int old_column_width = column_info[column].width_of_column;
790 for (int i = 0; i < rows_; ++i) {
791 maximum = max(cell_info[i][column].width_of_cell, maximum);
793 column_info[column].width_of_column = maximum;
794 return (column_info[column].width_of_column != old_column_width);
798 bool LyXTabular::calculate_width_of_column_NMC(int column)
800 int old_column_width = column_info[column].width_of_column;
802 for (int i = 0; i < rows_; ++i) {
803 if (!IsMultiColumn(GetCellNumber(i, column)) &&
804 (cell_info[i][column].width_of_cell > max)) {
805 max = cell_info[i][column].width_of_cell;
808 column_info[column].width_of_column = max;
809 return (column_info[column].width_of_column != old_column_width);
813 void LyXTabular::calculate_width_of_tabular()
815 width_of_tabular = 0;
816 for (int i = 0; i < columns_; ++i) {
817 width_of_tabular += column_info[i].width_of_column;
822 int LyXTabular::row_of_cell(int cell) const
824 if (cell >= numberofcells)
828 return rowofcell[cell];
832 int LyXTabular::column_of_cell(int cell) const
834 if (cell >= numberofcells)
838 return columnofcell[cell];
842 int LyXTabular::right_column_of_cell(int cell) const
844 int row = row_of_cell(cell);
845 int column = column_of_cell(cell);
846 while (column < (columns_ - 1) &&
847 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
853 void LyXTabular::Write(Buffer const * buf, ostream & os) const
858 os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
860 // global longtable options
861 os << "<Features rotate=" << rotate <<
862 " islongtable=" << is_long_tabular <<
863 " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
864 " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
866 for (i = 0; i < rows_; ++i) {
867 os << "<Row topline=" << row_info[i].top_line <<
868 " bottomline=" << row_info[i].bottom_line <<
869 " newpage=" << row_info[i].newpage <<
871 for (j = 0; j < columns_; ++j) {
873 os << "<Column alignment=" << column_info[j].alignment <<
874 " leftline=" << column_info[j].left_line <<
875 " rightline=" << column_info[j].right_line <<
876 " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
877 "\" special=\"" << column_info[j].align_special <<
880 os << "<Column>" << endl;
882 os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
883 " alignment=" << cell_info[i][j].alignment <<
884 " topline=" << cell_info[i][j].top_line <<
885 " bottomline=" << cell_info[i][j].bottom_line <<
886 " rotate=" << cell_info[i][j].rotate <<
887 " linebreaks=" << cell_info[i][j].linebreaks <<
888 " width=\"" << cell_info[i][j].p_width <<
889 "\" special=\"" << cell_info[i][j].align_special <<
891 os << "\\begin_inset ";
892 cell_info[i][j].inset.Write(buf, os);
893 os << "\n\\end_inset " << endl;
894 os << "</Cell>" << endl;
895 os << "</Column>" << endl;
897 os << "</Row>" << endl;
899 os << "</LyXTabular>" << endl;
904 bool getTokenValue(string const str, const char * token, string & ret)
906 int pos = str.find(token);
907 char ch = str[pos+strlen(token)];
909 if ((pos < 0) || (ch != '='))
912 pos += strlen(token)+1;
914 if ((ch != '"') && (ch != '\'')) { // only read till next space
918 while((pos < int(str.length()-1)) && (str[++pos] != ch))
926 bool getTokenValue(string const str, const char * token, int & num)
929 int pos = str.find(token);
930 char ch = str[pos+strlen(token)];
932 if ((pos < 0) || (ch != '='))
935 pos += strlen(token)+1;
937 if ((ch != '"') && (ch != '\'')) { // only read till next space
943 while((pos < int(str.length()-1)) && isdigit(str[pos]))
952 bool getTokenValue(string const str, const char * token, bool & flag)
955 int pos = str.find(token);
956 char ch = str[pos+strlen(token)];
958 if ((pos < 0) || (ch != '='))
961 pos += strlen(token)+1;
963 if ((ch != '"') && (ch != '\'')) { // only read till next space
969 while((pos < int(str.length()-1)) && isdigit(str[pos]))
972 flag = strToInt(ret);
977 void l_getline(istream & is, string & str)
985 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
988 istream & is = lex.getStream();
991 if (!prefixIs(line, "<LyXTabular ")) {
992 OldFormatRead(lex, line);
999 if (!getTokenValue(line, "version", version))
1001 if (!getTokenValue(line, "rows", rows_arg))
1003 if (!getTokenValue(line, "columns", columns_arg))
1005 Init(rows_arg, columns_arg);
1006 l_getline(is, line);
1007 if (!prefixIs(line, "<Features ")) {
1008 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1009 line << ")" << endl;
1012 (void)getTokenValue(line, "islongtable", is_long_tabular);
1013 (void)getTokenValue(line, "endhead", endhead);
1014 (void)getTokenValue(line, "endfirsthead", endfirsthead);
1015 (void)getTokenValue(line, "endfoot", endfoot);
1016 (void)getTokenValue(line, "endlastfoot", endlastfoot);
1018 for(i = 0; i < rows_; ++i) {
1019 l_getline(is, line);
1020 if (!prefixIs(line, "<Row ")) {
1021 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1022 line << ")" << endl;
1025 (void)getTokenValue(line, "topline", row_info[i].top_line);
1026 (void)getTokenValue(line, "bottomline", row_info[i].bottom_line);
1027 (void)getTokenValue(line, "newpage", row_info[i].newpage);
1028 for (j = 0; j < columns_; ++j) {
1030 if (!prefixIs(line,"<Column")) {
1031 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1032 line << ")" << endl;
1036 (void)getTokenValue(line, "alignment", column_info[j].alignment);
1037 (void)getTokenValue(line, "leftline", column_info[j].left_line);
1038 (void)getTokenValue(line, "rightline", column_info[j].right_line);
1039 (void)getTokenValue(line, "width", column_info[j].p_width);
1040 (void)getTokenValue(line, "special", column_info[j].align_special);
1042 l_getline(is, line);
1043 if (!prefixIs(line, "<Cell")) {
1044 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1045 line << ")" << endl;
1048 (void)getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1049 (void)getTokenValue(line, "alignment", cell_info[i][j].alignment);
1050 (void)getTokenValue(line, "topline", cell_info[i][j].top_line);
1051 (void)getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1052 (void)getTokenValue(line, "rotate", cell_info[i][j].rotate);
1053 (void)getTokenValue(line, "linebreaks", cell_info[i][j].linebreaks);
1054 (void)getTokenValue(line, "width", cell_info[i][j].p_width);
1055 (void)getTokenValue(line, "special", cell_info[i][j].align_special);
1056 l_getline(is, line);
1057 if (prefixIs(line, "\\begin_inset")) {
1058 cell_info[i][j].inset.Read(buf, lex);
1059 l_getline(is, line);
1061 if (line != "</Cell>") {
1062 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1063 line << ")" << endl;
1066 l_getline(is, line);
1067 if (line != "</Column>") {
1068 lyxerr << "Wrong tabular format (expected </Column> got" <<
1069 line << ")" << endl;
1073 l_getline(is, line);
1074 if (line != "</Row>") {
1075 lyxerr << "Wrong tabular format (expected </Row> got" <<
1076 line << ")" << endl;
1080 while (line != "</LyXTabular>") {
1081 l_getline(is, line);
1083 set_row_column_number_info();
1087 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1092 int columns_arg = 0;
1093 int is_long_tabular_arg = false;
1094 int rotate_arg = false;
1104 istream & is = lex.getStream();
1107 version = atoi(s.c_str() + 8);
1111 vector<int> cont_row_info;
1114 lyxerr << "Tabular format < 5 is not supported anymore\n"
1115 "Get an older version of LyX (< 1.1.x) for conversion!"
1117 WriteAlert(_("Warning:"),
1118 _("Tabular format < 5 is not supported anymore\n"),
1119 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1121 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1122 >> rotate_arg >> a >> b >> c >> d;
1124 is >> rows_arg >> columns_arg;
1125 Init(rows_arg, columns_arg);
1126 cont_row_info = vector<int>(rows_arg);
1127 SetLongTabular(is_long_tabular_arg);
1128 SetRotateTabular(rotate_arg);
1130 for (i = 0; i < rows_; ++i) {
1132 cont_row_info[i] = false;
1134 for (i = 0; i < columns_; ++i) {
1137 for (i = 0; i < rows_; ++i) {
1138 for (j = 0; j < columns_; ++j) {
1143 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1144 >> rotate_arg >> a >> b >> c >> d;
1145 Init(rows_arg, columns_arg);
1146 cont_row_info = vector<int>(rows_arg);
1147 SetLongTabular(is_long_tabular_arg);
1148 SetRotateTabular(rotate_arg);
1153 for (i = 0; i < rows_; ++i) {
1154 a = b = c = d = e = f = g = h = 0;
1155 is >> a >> b >> c >> d;
1156 row_info[i].top_line = a;
1157 row_info[i].bottom_line = b;
1158 cont_row_info[i] = c;
1159 row_info[i].newpage = d;
1161 for (i = 0; i < columns_; ++i) {
1165 char ch; // skip '"'
1167 getline(is, s1, '"');
1168 is >> ch; // skip '"'
1169 getline(is, s2, '"');
1170 column_info[i].alignment = static_cast<char>(a);
1171 column_info[i].left_line = b;
1172 column_info[i].right_line = c;
1173 column_info[i].p_width = s1;
1174 column_info[i].align_special = s2;
1176 for (i = 0; i < rows_; ++i) {
1177 for (j = 0; j < columns_; ++j) {
1180 is >> a >> b >> c >> d >> e >> f >> g;
1182 is >> ch; // skip '"'
1183 getline(is, s1, '"');
1184 is >> ch; // skip '"'
1185 getline(is, s2, '"');
1186 cell_info[i][j].multicolumn = static_cast<char>(a);
1187 cell_info[i][j].alignment = static_cast<char>(b);
1188 cell_info[i][j].top_line = static_cast<char>(c);
1189 cell_info[i][j].bottom_line = static_cast<char>(d);
1190 cell_info[i][j].rotate = static_cast<bool>(f);
1191 cell_info[i][j].linebreaks = static_cast<bool>(g);
1192 cell_info[i][j].align_special = s1;
1193 cell_info[i][j].p_width = s2;
1197 set_row_column_number_info();
1199 LyXParagraph * par = new LyXParagraph;
1200 LyXParagraph * return_par = 0;
1201 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1202 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1203 string token, tmptok;
1206 LyXFont font(LyXFont::ALL_SANE);
1208 while (lex.IsOK()) {
1210 token = lex.GetString();
1213 if ((token == "\\layout") || (token == "\\end_float") ||
1214 (token == "\\end_deeper"))
1216 lex.pushToken(token);
1219 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1227 lex.pushToken(token);
1231 lex.printError("New Paragraph allocated! This should not happen!");
1232 lex.pushToken(token);
1238 // now we have the par we should fill the insets with this!
1240 InsetText * inset = GetCellInset(cell);
1243 for(int i = 0; i < par->Last(); ++i) {
1244 if (par->IsNewline(i)) {
1246 if (cell > GetNumberOfCells()) {
1247 lyxerr << "Some error in reading old table format occured!" <<
1248 endl << "Terminating when reading cell[" << cell << "]!" <<
1252 row = row_of_cell(cell);
1253 if (cont_row_info[row]) {
1255 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1256 while(!IsFirstCellInRow(--cell));
1258 inset = GetCellInset(cell);
1261 inset = GetCellInset(cell);
1262 row = row_of_cell(cell);
1263 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].linebreaks)
1265 // insert a space instead
1267 par->InsertChar(i, ' ');
1270 par->CopyIntoMinibuffer(current_view->buffer()->params, i);
1271 inset->par->InsertFromMinibuffer(inset->par->Last());
1277 char const * LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1279 int i = isColumn ? cell : column_of_cell(cell);
1284 //i = column_of_cell(cell);
1285 if (!isColumn && IsMultiColumn(cell)) {
1286 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1287 return cellinfo_of_cell(cell)->align_special.c_str();
1289 switch (GetAlignment(cell)) {
1290 case LYX_ALIGN_LEFT:
1292 case LYX_ALIGN_RIGHT:
1299 if (!column_info[i].align_special.empty()) {
1300 return column_info[i].align_special.c_str();
1302 #ifdef IGNORE_THIS_FOR_NOW
1303 else if (!column_info[i].p_width.empty()) {
1305 file += column_info[i].p_width;
1310 switch (column_info[i].alignment) {
1311 case LYX_ALIGN_LEFT:
1313 case LYX_ALIGN_RIGHT:
1323 // cell <0 will tex the preamble
1324 // returns the number of printed newlines
1325 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1329 //int tmp; // tmp2; // unused
1330 int nvcell; // fcell; // unused
1331 if (IsLastCell(cell)) {
1332 os << newlineAndDepth(--depth)
1334 << newlineAndDepth(--depth)
1336 << newlineAndDepth(--depth)
1338 << newlineAndDepth(--depth);
1339 if (is_long_tabular)
1343 << newlineAndDepth(--depth);
1349 if (is_long_tabular)
1355 << "' COLSEP='1' ROWSEP='1'>"
1356 << newlineAndDepth(++depth);
1358 for (i = 0; i < columns_; ++i) {
1359 os << "<COLSPEC ALIGN='"
1360 << GetDocBookAlign(i, true)
1366 if (i == (columns_-1)) {
1369 if (column_info[i].right_line ||
1370 column_info[i+1].left_line)
1376 << newlineAndDepth(depth);
1378 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1379 if (column_info[i].left_line)
1384 << newlineAndDepth(++depth)
1386 << newlineAndDepth(++depth)
1388 << GetDocBookAlign(0)
1390 if (IsMultiColumn(0)) {
1391 os << " NAMEST='col1' NAMEEND='col"
1392 << cells_in_multicolumn(0)
1396 << newlineAndDepth(++depth);
1399 if (IsLastCellInRow(cell)) {
1400 os << newlineAndDepth(--depth)
1402 << newlineAndDepth(--depth)
1404 << newlineAndDepth(depth)
1406 << newlineAndDepth(++depth)
1408 << GetDocBookAlign(cell + 1)
1409 << "' VALIGN='middle'";
1410 if (IsMultiColumn(cell + 1)) {
1411 os << " NAMEST='col"
1412 << column_of_cell(cell+1) + 1
1414 << column_of_cell(cell + 1) +
1415 cells_in_multicolumn(cell + 1)
1419 << newlineAndDepth(++depth);
1422 os << newlineAndDepth(--depth)
1424 << newlineAndDepth(depth)
1426 << GetDocBookAlign(cell + 1)
1427 << "' VALIGN='middle'";
1428 if (IsMultiColumn(cell + 1)) {
1429 os << " NAMEST='col"
1430 << column_of_cell(cell+1) + 1
1432 << column_of_cell(cell+1) +
1433 cells_in_multicolumn(cell+1)
1437 << newlineAndDepth(++depth);
1446 bool LyXTabular::IsMultiColumn(int cell) const
1448 return (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1452 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1454 int row = row_of_cell(cell);
1455 int column = column_of_cell(cell);
1456 return &cell_info[row][column];
1460 void LyXTabular::SetMultiColumn(int cell, int number)
1462 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1463 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1464 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1465 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1466 for (number--; number > 0; --number) {
1467 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1469 set_row_column_number_info();
1473 int LyXTabular::cells_in_multicolumn(int cell) const
1475 int row = row_of_cell(cell);
1476 int column = column_of_cell(cell);
1479 while ((column < columns_) &&
1480 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1489 int LyXTabular::UnsetMultiColumn(int cell)
1491 int row = row_of_cell(cell);
1492 int column = column_of_cell(cell);
1496 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1497 cell_info[row][column].multicolumn = CELL_NORMAL;
1499 while ((column < columns_) &&
1500 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1502 cell_info[row][column].multicolumn = CELL_NORMAL;
1507 set_row_column_number_info();
1512 void LyXTabular::SetLongTabular(int what)
1514 is_long_tabular = what;
1518 bool LyXTabular::IsLongTabular() const
1520 return is_long_tabular;
1524 void LyXTabular::SetRotateTabular(int what)
1530 bool LyXTabular::GetRotateTabular() const
1536 void LyXTabular::SetRotateCell(int cell, int what)
1538 cellinfo_of_cell(cell)->rotate = what;
1542 bool LyXTabular::GetRotateCell(int cell) const
1544 return cellinfo_of_cell(cell)->rotate;
1548 bool LyXTabular::NeedRotating() const
1552 for (int i = 0; i < rows_; ++i) {
1553 for (int j = 0; j < columns_; ++j) {
1554 if (cell_info[i][j].rotate)
1562 bool LyXTabular::IsLastCell(int cell) const
1564 if ((cell+1) < GetNumberOfCells())
1570 int LyXTabular::GetCellAbove(int cell) const
1572 if (row_of_cell(cell) > 0)
1573 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1578 int LyXTabular::GetCellBelow(int cell) const
1580 if (row_of_cell(cell)+1 < rows_)
1581 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1586 int LyXTabular::GetLastCellAbove(int cell) const
1588 if (row_of_cell(cell) <= 0)
1590 if (!IsMultiColumn(cell))
1591 return GetCellAbove(cell);
1592 return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
1596 int LyXTabular::GetLastCellBelow(int cell) const
1598 if (row_of_cell(cell)+1 >= rows_)
1600 if (!IsMultiColumn(cell))
1601 return GetCellBelow(cell);
1602 return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
1606 int LyXTabular::GetCellNumber(int row, int column) const
1608 if (column >= columns_)
1609 column = columns_ - 1;
1610 else if (column < 0)
1617 return cell_info[row][column].cellno;
1621 void LyXTabular::SetLinebreaks(int cell, bool what)
1623 cellinfo_of_cell(cell)->linebreaks = what;
1627 bool LyXTabular::GetLinebreaks(int cell) const
1629 if (column_info[column_of_cell(cell)].p_width.empty() &&
1630 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1632 return cellinfo_of_cell(cell)->linebreaks;
1636 void LyXTabular::SetLTHead(int cell, bool first)
1638 int row = row_of_cell(cell);
1641 if (row == endfirsthead)
1654 bool LyXTabular::GetRowOfLTHead(int cell) const
1656 if ((endhead+1) > rows_)
1658 return (row_of_cell(cell) == endhead);
1662 bool LyXTabular::GetRowOfLTFirstHead(int cell) const
1664 if ((endfirsthead+1) > rows_)
1666 return (row_of_cell(cell) == endfirsthead);
1670 void LyXTabular::SetLTFoot(int cell, bool last)
1672 int row = row_of_cell(cell);
1675 if (row == endlastfoot)
1688 bool LyXTabular::GetRowOfLTFoot(int cell) const
1690 if ((endfoot+1) > rows_)
1692 return (row_of_cell(cell) == endfoot);
1695 bool LyXTabular::GetRowOfLTLastFoot(int cell) const
1697 if ((endlastfoot+1) > rows_)
1699 return (row_of_cell(cell) == endlastfoot);
1703 void LyXTabular::SetLTNewPage(int cell, bool what)
1705 row_info[row_of_cell(cell)].newpage = what;
1709 bool LyXTabular::GetLTNewPage(int cell) const
1711 return row_info[row_of_cell(cell)].newpage;
1715 bool LyXTabular::SetAscentOfRow(int row, int height)
1717 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1719 row_info[row].ascent_of_row = height;
1724 bool LyXTabular::SetDescentOfRow(int row, int height)
1726 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1728 row_info[row].descent_of_row = height;
1733 int LyXTabular::GetAscentOfRow(int row) const
1737 return row_info[row].ascent_of_row;
1741 int LyXTabular::GetDescentOfRow(int row) const
1745 return row_info[row].descent_of_row;
1749 int LyXTabular::GetHeightOfTabular() const
1753 for(int row = 0; row < rows_; ++row)
1754 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1755 GetAdditionalHeight(GetCellNumber(row, 0));
1760 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1762 if ((row >= rows_) || (column >= columns_))
1764 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1768 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1770 int fcell = GetFirstCellInRow(row);
1771 int n = NumberOfCellsInRow(fcell) + fcell;
1775 for (i = fcell; i < n; ++i) {
1779 if (tmp == (n - fcell)){
1782 for (i = fcell; i < n; ++i) {
1785 << column_of_cell(i) + 1
1787 << right_column_of_cell(i) + 1
1800 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1802 int fcell = GetFirstCellInRow(row);
1803 int n = NumberOfCellsInRow(fcell) + fcell;
1807 for (i = fcell; i < n; ++i) {
1811 if (tmp == (n-fcell)){
1814 for (i = fcell; i < n; ++i) {
1815 if (BottomLine(i)) {
1817 << column_of_cell(i) + 1
1819 << right_column_of_cell(i) + 1
1832 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1836 if (GetRotateCell(cell)) {
1837 os << "\\begin{sideways}" << endl;
1840 if (IsMultiColumn(cell)) {
1841 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1842 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1843 os << cellinfo_of_cell(cell+1)->align_special << "}{";
1847 if (!GetPWidth(cell).empty()) {
1848 os << "p{" << GetPWidth(cell) << '}';
1850 switch (GetAlignment(cell)) {
1851 case LYX_ALIGN_LEFT:
1854 case LYX_ALIGN_RIGHT:
1862 if (RightLine(cell))
1864 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1870 if (GetLinebreaks(cell)) {
1871 os << "\\parbox[t]{" << GetPWidth(cell) << "}{\\smallskip{}";
1877 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1882 if (GetLinebreaks(cell))
1883 os << "\\smallskip{}}";
1884 if (IsMultiColumn(cell)){
1887 if (GetRotateCell(cell)) {
1888 os << endl << "\\end{sideways}";
1895 int LyXTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
1901 //+---------------------------------------------------------------------
1902 //+ first the opening preamble +
1903 //+---------------------------------------------------------------------
1906 os << "\\begin{sideways}" << endl;
1909 if (is_long_tabular)
1910 os << "\\begin{longtable}{";
1912 os << "\\begin{tabular}{";
1913 for (i = 0; i < columns_; ++i) {
1914 if (column_info[i].left_line)
1916 if (!column_info[i].align_special.empty()) {
1917 os << column_info[i].align_special;
1918 } else if (!column_info[i].p_width.empty()) {
1920 << column_info[i].p_width
1923 switch (column_info[i].alignment) {
1924 case LYX_ALIGN_LEFT:
1927 case LYX_ALIGN_RIGHT:
1935 if (column_info[i].right_line)
1941 //+---------------------------------------------------------------------
1942 //+ the single row and columns (cells) +
1943 //+---------------------------------------------------------------------
1945 for(i=0; i < rows_; ++i) {
1946 ret += TeXTopHLine(os, i);
1947 for(j=0; j < columns_; ++j) {
1948 if (IsPartOfMultiColumn(i,j))
1950 ret += TeXCellPreamble(os, cell);
1951 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
1952 ret += TeXCellPostamble(os, cell);
1953 if (!IsLastCellInRow(cell)) { // not last cell in row
1959 os << "\\\\" << endl;
1960 ret += TeXBottomHLine(os, i);
1961 if (IsLongTabular()) {
1963 os << "\\endhead\n";
1966 if (i == endfirsthead) {
1967 os << "\\endfirsthead\n";
1971 os << "\\endfoot\n";
1974 if (i == endlastfoot) {
1975 os << "\\endlastfoot\n";
1978 if (row_info[i].newpage) {
1979 os << "\\newpage\n";
1985 //+---------------------------------------------------------------------
1986 //+ the closing of the tabular +
1987 //+---------------------------------------------------------------------
1989 if (is_long_tabular)
1990 os << "\\end{longtable}";
1992 os << "\\end{tabular}";
1994 os << "\n\\end{sideways}";
2002 InsetText * LyXTabular::GetCellInset(int cell) const
2004 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2007 void LyXTabular::Validate(LaTeXFeatures & features) const
2009 if (IsLongTabular())
2010 features.longtable = true;
2012 features.rotating = true;
2013 for(int cell = 0; cell < numberofcells; ++cell)
2014 GetCellInset(cell)->Validate(features);