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;
79 LyXTabular::cellstruct &
80 LyXTabular::cellstruct::operator=(cellstruct const & cs)
83 width_of_cell = cs.width_of_cell;
84 multicolumn = cs.multicolumn;
85 alignment = cs.alignment;
86 top_line = cs.top_line;
87 bottom_line = cs.bottom_line;
89 linebreaks = cs.linebreaks;
94 LyXTabular::rowstruct::rowstruct()
104 LyXTabular::columnstruct::columnstruct()
108 alignment = LYX_ALIGN_CENTER;
114 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
117 Init(rows_arg, columns_arg);
121 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
124 Init(lt.rows_, lt.columns_);
130 LyXTabular::LyXTabular(InsetTabular * inset, LyXLex & lex)
137 LyXTabular::~LyXTabular()
140 delete[] columnofcell;
144 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
146 // If this and lt is not of the same size we have a serious bug
147 // So then it is ok to throw an exception, or for now
149 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
151 cell_info = lt.cell_info;
152 row_info = lt.row_info;
153 column_info = lt.column_info;
155 // long tabular stuff
156 SetLongTabular(lt.is_long_tabular);
157 endhead = lt.endhead;
158 endfoot = lt.endfoot;
159 endfirsthead = lt.endfirsthead;
160 endlastfoot = lt.endlastfoot;
170 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
172 LyXTabular * result = new LyXTabular(inset, *this);
174 // don't know if this is good but I need to Clone also
175 // the text-insets here, this is for the Undo-facility!
178 for(i=0; i < rows_; ++i) {
179 for(j=0; j < columns_; ++j) {
180 delete result->cell_info[i][j].inset;
181 result->cell_info[i][j].inset=new InsetText(*cell_info[i][j].inset,
182 inset->BufferOwner());
183 result->cell_info[i][j].inset->setOwner(inset);
190 /* activates all lines and sets all widths to 0 */
191 void LyXTabular::Init(int rows_arg, int columns_arg)
197 columns_ = columns_arg;
198 row_info = vector<rowstruct>(rows_, rowstruct());
199 column_info = vector<columnstruct>(columns_, columnstruct());
200 cell_info = vector<vector<cellstruct> >
201 (rows_, vector<cellstruct>(columns_, cellstruct()));
203 // Jürgen, use iterators.
204 for (i = 0; i < rows_; ++i) {
205 for (j = 0; j < columns_; ++j) {
206 cell_info[i][j].inset = new InsetText(owner_->BufferOwner());
207 cell_info[i][j].inset->setOwner(owner_);
208 cell_info[i][j].inset->SetDrawLockedFrame(true);
209 cell_info[i][j].cellno = cellno++;
212 row_info[i-1].bottom_line = true;
213 row_info[0].bottom_line = true;
215 for (i = 0; i < columns_; ++i) {
216 calculate_width_of_column(i);
218 column_info[columns_-1].right_line = true;
220 calculate_width_of_tabular();
224 set_row_column_number_info();
225 is_long_tabular = false;
234 void LyXTabular::AppendRow(int /* cell */)
237 int row = row_of_cell(cell);
238 rowstruct * row_info2 = new rowstruct[rows_ + 1];
239 cellstruct ** cell_info2 = new cellstruct * [rows_ + 1];
242 for (i = 0; i <= row; ++i) {
243 cell_info2[i] = cell_info[i];
244 row_info2[i] = row_info[i];
246 for (i = rows_ - 1; i >= row; --i) {
247 cell_info2[i + 1] = cell_info[i];
248 row_info2[i + 1] = row_info[i];
250 row_info2[row + 1].top_line = row_info[i].top_line;
251 cell_info2[row + 1] = new cellstruct[columns_](owner_->BufferOwner());
252 for (i = 0; i < columns_; ++i) {
253 cell_info2[row + 1][i].width_of_cell = 0;
254 cell_info2[row + 1][i] = cell_info2[row][i];
258 cell_info = cell_info2;
260 row_info = row_info2;
269 void LyXTabular::DeleteRow(int row)
271 row_info.erase(&row_info[row]);
272 cell_info.erase(&cell_info[row]);
278 void LyXTabular::AppendColumn(int /*cell*/)
282 columnstruct * column_info2 = new columnstruct[columns_ + 1];
283 int column = right_column_of_cell(cell);
286 for (; i <= column; ++i) {
287 column_info2[i] = column_info[i];
289 for (i = columns_ - 1; i >= column; --i) {
290 column_info2[i + 1] = column_info[i];
293 delete[] column_info;
294 column_info = column_info2;
296 for (i = 0; i < rows_; ++i) {
297 cellstruct * tmp = cell_info[i];
298 cell_info[i] = new cellstruct[columns_ + 1](owner_->BufferOwner());
299 for (j = 0; j <= column; ++j) {
300 cell_info[i][j] = tmp[j];
302 for (j = column; j < columns_; ++j) {
303 cell_info[i][j + 1] = tmp[j];
305 // care about multicolumns
306 if (cell_info[i][column + 1].multicolumn
307 == LyXTabular::CELL_BEGIN_OF_MULTICOLUMN){
308 cell_info[i][column + 1].multicolumn =
309 LyXTabular::CELL_PART_OF_MULTICOLUMN;
311 if (column + 1 == columns_
312 || cell_info[i][column + 2].multicolumn
313 != LyXTabular::CELL_PART_OF_MULTICOLUMN){
314 cell_info[i][column + 1].multicolumn =
315 LyXTabular::CELL_NORMAL;
326 void LyXTabular::DeleteColumn(int /*cell*/)
329 int column1 = column_of_cell(cell);
330 int column2 = right_column_of_cell(cell);
332 if (column1 == 0 && column2 == columns_ - 1)
335 for (int column = column1; column <= column2; ++column) {
336 delete_column(column1);
343 void LyXTabular::delete_column(int /*column*/)
347 columnstruct * column_info2 = new columnstruct[columns_-1];
349 for (i = 0; i < column; ++i) {
350 column_info2[i] = column_info[i];
352 for (i = column; i < columns_ - 1; ++i) {
353 column_info2[i] = column_info[i + 1];
356 delete[] column_info;
357 column_info = column_info2;
359 for (i = 0; i < rows_; ++i) {
360 cellstruct * tmp = cell_info[i];
361 cell_info[i] = new cellstruct[columns_ - 1](owner_->BufferOwner());
362 for (j = 0; j < column; ++j) {
363 cell_info[i][j] = tmp[j];
365 for (j = column; j < columns_ - 1; ++j) {
366 cell_info[i][j] = tmp[j + 1];
377 void LyXTabular::Reinit()
383 // Jürgen, use iterators.
384 for (; i < rows_; ++i) {
385 for (j = 0; j < columns_; ++j) {
386 cell_info[i][j].width_of_cell = 0;
390 for (i = 0; i < columns_; ++i) {
391 calculate_width_of_column(i);
393 calculate_width_of_tabular();
395 set_row_column_number_info();
399 void LyXTabular::set_row_column_number_info()
405 for (; row < rows_; ++row) {
406 for (column = 0; column<columns_; ++column) {
407 if (cell_info[row][column].multicolumn
408 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
410 cell_info[row][column].cellno = numberofcells;
413 ++numberofcells; // because this is one more than as we start from 0
418 rowofcell = new int[numberofcells];
419 delete [] columnofcell;
420 columnofcell = new int[numberofcells];
422 while (c < numberofcells && row < rows_ && column < columns_) {
424 columnofcell[c] = column;
428 } while (column < columns_ &&
429 cell_info[row][column].multicolumn
430 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
431 if (column == columns_) {
436 for (row = 0; row < rows_; ++row) {
437 for (column = 0; column<columns_; ++column) {
438 if (IsPartOfMultiColumn(row,column))
440 cell_info[row][column].inset->SetAutoBreakRows(
441 !GetPWidth(GetCellNumber(row, column)).empty());
447 int LyXTabular::GetNumberOfCells() const
449 return numberofcells;
453 int LyXTabular::NumberOfCellsInRow(int cell) const
455 int row = row_of_cell(cell);
457 for (int i = 0; i < columns_; ++i) {
458 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
465 int LyXTabular::AppendCellAfterCell(int append_cell, int question_cell)
467 return (right_column_of_cell(append_cell) ==
468 right_column_of_cell(question_cell));
472 int LyXTabular::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
474 if (column_of_cell(delete_column_cell) == 0 &&
475 right_column_of_cell(delete_column_cell) == columns_ - 1)
479 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
480 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
484 /* returns 1 if there is a topline, returns 0 if not */
485 bool LyXTabular::TopLine(int cell) const
487 int row = row_of_cell(cell);
489 if (IsMultiColumn(cell))
490 return cellinfo_of_cell(cell)->top_line;
491 return row_info[row].top_line;
495 bool LyXTabular::BottomLine(int cell) const
497 //no bottom line underneath non-existent cells if you please
498 if(cell >= numberofcells)
501 if (IsMultiColumn(cell))
502 return cellinfo_of_cell(cell)->bottom_line;
503 return row_info[row_of_cell(cell)].bottom_line;
507 bool LyXTabular::LeftLine(int cell) const
509 return column_info[column_of_cell(cell)].left_line;
513 bool LyXTabular::RightLine(int cell) const
515 return column_info[right_column_of_cell(cell)].right_line;
519 bool LyXTabular::TopAlreadyDrawed(int cell) const
521 if (GetAdditionalHeight(cell))
523 int row = row_of_cell(cell);
525 int column = column_of_cell(cell);
528 && cell_info[row][column].multicolumn
529 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
531 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
532 return row_info[row].bottom_line;
534 return cell_info[row][column].bottom_line;
540 bool LyXTabular::LeftAlreadyDrawed(int cell) const
542 int column = column_of_cell(cell);
544 int row = row_of_cell(cell);
546 (cell_info[row][column].multicolumn ==
547 LyXTabular::CELL_PART_OF_MULTICOLUMN));
548 if (GetAdditionalWidth(cell_info[row][column].cellno))
550 return column_info[column].right_line;
556 bool LyXTabular::IsLastRow(int cell) const
558 return (row_of_cell(cell) == rows_ - 1);
562 int LyXTabular::GetAdditionalHeight(int cell) const
564 int row = row_of_cell(cell);
567 int top = 1; // bool top = true; ??
568 int bottom = 1; // bool bottom = true; ??
571 for (column = 0; column < columns_ - 1 && bottom; ++column) {
572 switch (cell_info[row - 1][column].multicolumn) {
573 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
574 bottom = cell_info[row - 1][column].bottom_line;
576 case LyXTabular::CELL_NORMAL:
577 bottom = row_info[row - 1].bottom_line;
580 for (column = 0; column < columns_ - 1 && top; ++column) {
581 switch (cell_info[row][column].multicolumn){
582 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
583 top = cell_info[row][column].top_line;
585 case LyXTabular::CELL_NORMAL:
586 top = row_info[row].top_line;
590 return WIDTH_OF_LINE;
595 int LyXTabular::GetAdditionalWidth(int cell) const
597 // internally already set in SetWidthOfCell
598 // used to get it back in text.C
599 int col = right_column_of_cell(cell);
600 if (col < columns_ - 1 && column_info[col].right_line &&
601 column_info[col+1].left_line)
602 return WIDTH_OF_LINE;
608 // returns the maximum over all rows
609 int LyXTabular::GetWidthOfColumn(int cell) const
611 int column1 = column_of_cell(cell);
612 int column2 = right_column_of_cell(cell);
615 for (; i <= column2; ++i) {
616 result += column_info[i].width_of_column;
622 int LyXTabular::GetWidthOfTabular() const
624 return width_of_tabular;
628 /* returns 1 if a complete update is necessary, otherwise 0 */
629 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
631 if (!IsMultiColumn(cell))
634 int row = row_of_cell(cell);
635 int column1 = column_of_cell(cell);
636 int column2 = right_column_of_cell(cell);
638 // first set columns to 0 so we can calculate the right width
640 for (; i <= column2; ++i) {
641 cell_info[row][i].width_of_cell = 0;
643 // set the width to MAX_WIDTH until width > 0
644 int width = (new_width + 2 * WIDTH_OF_LINE);
645 for (i = column1; (i < column2) && (width > 0); ++i) {
646 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
647 width -= column_info[i].width_of_column;
650 cell_info[row][i].width_of_cell = width;
656 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
658 int row = row_of_cell(cell);
659 int column1 = column_of_cell(cell);
660 int column2 = right_column_of_cell(cell);
662 // first set columns to 0 so we can calculate the right width
664 for (; i <= column2; ++i)
665 cell_info[row][i].width_of_cell = 0;
666 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
668 if (i < numberofcells)
669 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
670 SetWidthOfMulticolCell(cell, new_width);
674 /* returns 1 if a complete update is necessary, otherwise 0 */
675 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
677 int row = row_of_cell(cell);
678 int column1 = column_of_cell(cell);
682 if (IsMultiColumn(cell)) {
683 tmp = SetWidthOfMulticolCell(cell, new_width);
685 width = (new_width + 2*WIDTH_OF_LINE);
686 cell_info[row][column1].width_of_cell = width;
687 if (column_info[column1].right_line && (column1 < columns_-1) &&
688 column_info[column1+1].left_line) // additional width
689 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
690 tmp = calculate_width_of_column_NMC(column1);
694 for(i = 0; i<columns_;++i)
695 calculate_width_of_column_NMC(i);
696 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
699 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
700 for(i = 0; i<columns_;++i)
701 calculate_width_of_column(i);
702 calculate_width_of_tabular();
709 bool LyXTabular::SetAlignment(int cell, char align)
711 if (!IsMultiColumn(cell))
712 column_info[column_of_cell(cell)].alignment = align;
713 cellinfo_of_cell(cell)->alignment = align;
718 bool LyXTabular::SetPWidth(int cell, string const & width)
720 bool flag = !width.empty();
722 if (IsMultiColumn(cell)) {
723 cellinfo_of_cell(cell)->p_width = width;
724 GetCellInset(cell)->SetAutoBreakRows(flag);
726 int j = column_of_cell(cell);
728 column_info[j].p_width = width;
729 if (flag) // do this only if there is a width
730 SetAlignment(cell, LYX_ALIGN_LEFT);
731 for(int i=0; i < rows_; ++i) {
732 c = GetCellNumber(i, j);
733 flag = !GetPWidth(c).empty(); // because of multicolumns!
734 GetCellInset(c)->SetAutoBreakRows(flag);
741 bool LyXTabular::SetAlignSpecial(int cell, string const & special, int what)
743 if (what == SET_SPECIAL_MULTI)
744 cellinfo_of_cell(cell)->align_special = special;
746 column_info[column_of_cell(cell)].align_special = special;
751 bool LyXTabular::SetAllLines(int cell, bool line)
753 SetTopLine(cell, line);
754 SetBottomLine(cell, line);
755 SetRightLine(cell, line);
756 SetLeftLine(cell, line);
761 bool LyXTabular::SetTopLine(int cell, bool line)
763 int row = row_of_cell(cell);
765 if (!IsMultiColumn(cell))
766 row_info[row].top_line = line;
768 cellinfo_of_cell(cell)->top_line = line;
773 bool LyXTabular::SetBottomLine(int cell, bool line)
775 if (!IsMultiColumn(cell))
776 row_info[row_of_cell(cell)].bottom_line = line;
778 cellinfo_of_cell(cell)->bottom_line = line;
783 bool LyXTabular::SetLeftLine(int cell, bool line)
785 column_info[column_of_cell(cell)].left_line = line;
790 bool LyXTabular::SetRightLine(int cell, bool line)
792 column_info[right_column_of_cell(cell)].right_line = line;
797 char LyXTabular::GetAlignment(int cell) const
799 if (IsMultiColumn(cell))
800 return cellinfo_of_cell(cell)->alignment;
802 return column_info[column_of_cell(cell)].alignment;
806 string LyXTabular::GetPWidth(int cell) const
808 if (IsMultiColumn(cell))
809 return cellinfo_of_cell(cell)->p_width;
810 return column_info[column_of_cell(cell)].p_width;
814 string LyXTabular::GetAlignSpecial(int cell, int what) const
816 if (what == SET_SPECIAL_MULTI)
817 return cellinfo_of_cell(cell)->align_special;
818 return column_info[column_of_cell(cell)].align_special;
822 int LyXTabular::GetWidthOfCell(int cell) const
824 int row = row_of_cell(cell);
825 int column1 = column_of_cell(cell);
826 int column2 = right_column_of_cell(cell);
829 for (; i <= column2; ++i) {
830 result += cell_info[row][i].width_of_cell;
833 // result += GetAdditionalWidth(cell);
838 int LyXTabular::GetBeginningOfTextInCell(int cell) const
842 switch (GetAlignment(cell)){
843 case LYX_ALIGN_CENTER:
844 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
846 case LYX_ALIGN_RIGHT:
847 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
848 // + GetAdditionalWidth(cell);
850 default: /* LYX_ALIGN_LEFT: nothing :-) */
860 bool LyXTabular::IsFirstCellInRow(int cell) const
862 return (column_of_cell(cell) == 0);
866 int LyXTabular::GetFirstCellInRow(int row) const
870 return cell_info[row][0].cellno;
873 bool LyXTabular::IsLastCellInRow(int cell) const
875 return (right_column_of_cell(cell) == (columns_ - 1));
879 int LyXTabular::GetLastCellInRow(int row) const
883 return cell_info[row][columns_-1].cellno;
887 bool LyXTabular::calculate_width_of_column(int column)
889 int old_column_width = column_info[column].width_of_column;
892 for (int i = 0; i < rows_; ++i) {
893 maximum = max(cell_info[i][column].width_of_cell, maximum);
895 column_info[column].width_of_column = maximum;
896 return (column_info[column].width_of_column != old_column_width);
900 bool LyXTabular::calculate_width_of_column_NMC(int column)
902 int old_column_width = column_info[column].width_of_column;
904 for (int i = 0; i < rows_; ++i) {
905 if (!IsMultiColumn(GetCellNumber(i, column)) &&
906 (cell_info[i][column].width_of_cell > max)) {
907 max = cell_info[i][column].width_of_cell;
910 column_info[column].width_of_column = max;
911 return (column_info[column].width_of_column != old_column_width);
915 void LyXTabular::calculate_width_of_tabular()
917 width_of_tabular = 0;
918 for (int i = 0; i < columns_; ++i) {
919 width_of_tabular += column_info[i].width_of_column;
924 int LyXTabular::row_of_cell(int cell) const
926 if (cell >= numberofcells)
930 return rowofcell[cell];
934 int LyXTabular::column_of_cell(int cell) const
936 if (cell >= numberofcells)
940 return columnofcell[cell];
944 int LyXTabular::right_column_of_cell(int cell) const
946 int row = row_of_cell(cell);
947 int column = column_of_cell(cell);
948 while (column < (columns_ - 1) &&
949 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
955 void LyXTabular::Write(ostream & os) const
960 os << "<LyXTabular version=1 rows=" << rows_ << " columns=" << columns_ <<
962 // global longtable options
963 os << "<Features rotate=" << rotate <<
964 " islongtable=" << is_long_tabular <<
965 " endhead=" << endhead << " endfirsthead=" << endfirsthead <<
966 " endfoot=" << endfoot << " endlastfoot=" << endlastfoot <<
968 for (i = 0; i < rows_; ++i) {
969 os << "<Row topline=" << row_info[i].top_line <<
970 " bottomline=" << row_info[i].bottom_line <<
971 " newpage=" << row_info[i].newpage <<
973 for (j = 0; j < columns_; ++j) {
975 os << "<Column alignment=" << column_info[j].alignment <<
976 " leftline=" << column_info[j].left_line <<
977 " rightline=" << column_info[j].right_line <<
978 " width=\"" << VSpace(column_info[j].p_width).asLyXCommand() <<
979 "\" special=\"" << column_info[j].align_special <<
982 os << "<Column>" << endl;
984 os << "<Cell multicolumn=" << cell_info[i][j].multicolumn <<
985 " alignment=" << cell_info[i][j].alignment <<
986 " topline=" << cell_info[i][j].top_line <<
987 " bottomline=" << cell_info[i][j].bottom_line <<
988 " rotate=" << cell_info[i][j].rotate <<
989 " linebreaks=" << cell_info[i][j].linebreaks <<
990 " width=\"" << cell_info[i][j].p_width <<
991 "\" special=\"" << cell_info[i][j].align_special <<
993 os << "\\begin_inset ";
994 cell_info[i][j].inset->Write(os);
995 os << "\n\\end_inset " << endl;
996 os << "</Cell>" << endl;
997 os << "</Column>" << endl;
999 os << "</Row>" << endl;
1001 os << "</LyXTabular>" << endl;
1006 bool getTokenValue(string const str, const char * token, string & ret)
1008 int pos = str.find(token);
1009 char ch = str[pos+strlen(token)];
1011 if ((pos < 0) || (ch != '='))
1014 pos += strlen(token)+1;
1016 if ((ch != '"') && (ch != '\'')) { // only read till next space
1020 while((pos < int(str.length()-1)) && (str[++pos] != ch))
1028 bool getTokenValue(string const str, const char * token, int & num)
1031 int pos = str.find(token);
1032 char ch = str[pos+strlen(token)];
1034 if ((pos < 0) || (ch != '='))
1037 pos += strlen(token)+1;
1039 if ((ch != '"') && (ch != '\'')) { // only read till next space
1045 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1048 num = strToInt(ret);
1054 bool getTokenValue(string const str, const char * token, bool & flag)
1057 int pos = str.find(token);
1058 char ch = str[pos+strlen(token)];
1060 if ((pos < 0) || (ch != '='))
1063 pos += strlen(token)+1;
1065 if ((ch != '"') && (ch != '\'')) { // only read till next space
1071 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1074 flag = strToInt(ret);
1079 void l_getline(istream & is, string & str)
1087 void LyXTabular::Read(LyXLex & lex)
1090 istream & is = lex.getStream();
1092 l_getline(is, line);
1093 if (!prefixIs(line, "<LyXTabular ")) {
1094 OldFormatRead(lex, line);
1101 if (!getTokenValue(line, "version", version))
1103 if (!getTokenValue(line, "rows", rows_arg))
1105 if (!getTokenValue(line, "columns", columns_arg))
1107 Init(rows_arg, columns_arg);
1108 l_getline(is, line);
1109 if (!prefixIs(line, "<Features ")) {
1110 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1111 line << ")" << endl;
1114 (void)getTokenValue(line, "islongtable", is_long_tabular);
1115 (void)getTokenValue(line, "endhead", endhead);
1116 (void)getTokenValue(line, "endfirsthead", endfirsthead);
1117 (void)getTokenValue(line, "endfoot", endfoot);
1118 (void)getTokenValue(line, "endlastfoot", endlastfoot);
1120 for(i = 0; i < rows_; ++i) {
1121 l_getline(is, line);
1122 if (!prefixIs(line, "<Row ")) {
1123 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1124 line << ")" << endl;
1127 (void)getTokenValue(line, "topline", row_info[i].top_line);
1128 (void)getTokenValue(line, "bottomline", row_info[i].bottom_line);
1129 (void)getTokenValue(line, "newpage", row_info[i].newpage);
1130 for (j = 0; j < columns_; ++j) {
1132 if (!prefixIs(line,"<Column")) {
1133 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1134 line << ")" << endl;
1138 (void)getTokenValue(line, "alignment", column_info[j].alignment);
1139 (void)getTokenValue(line, "leftline", column_info[j].left_line);
1140 (void)getTokenValue(line, "rightline", column_info[j].right_line);
1141 (void)getTokenValue(line, "width", column_info[j].p_width);
1142 (void)getTokenValue(line, "special", column_info[j].align_special);
1144 l_getline(is, line);
1145 if (!prefixIs(line, "<Cell")) {
1146 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1147 line << ")" << endl;
1150 (void)getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1151 (void)getTokenValue(line, "alignment", cell_info[i][j].alignment);
1152 (void)getTokenValue(line, "topline", cell_info[i][j].top_line);
1153 (void)getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1154 (void)getTokenValue(line, "rotate", cell_info[i][j].rotate);
1155 (void)getTokenValue(line, "linebreaks", cell_info[i][j].linebreaks);
1156 (void)getTokenValue(line, "width", cell_info[i][j].p_width);
1157 (void)getTokenValue(line, "special", cell_info[i][j].align_special);
1158 l_getline(is, line);
1159 if (prefixIs(line, "\\begin_inset")) {
1160 cell_info[i][j].inset->Read(lex);
1161 l_getline(is, line);
1163 if (line != "</Cell>") {
1164 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1165 line << ")" << endl;
1168 l_getline(is, line);
1169 if (line != "</Column>") {
1170 lyxerr << "Wrong tabular format (expected </Column> got" <<
1171 line << ")" << endl;
1175 l_getline(is, line);
1176 if (line != "</Row>") {
1177 lyxerr << "Wrong tabular format (expected </Row> got" <<
1178 line << ")" << endl;
1182 while (line != "</LyXTabular>") {
1183 l_getline(is, line);
1185 set_row_column_number_info();
1189 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1194 int columns_arg = 0;
1195 int is_long_tabular_arg = false;
1196 int rotate_arg = false;
1206 istream & is = lex.getStream();
1209 version = atoi(s.c_str() + 8);
1213 vector<int> cont_row_info;
1216 lyxerr << "Tabular format < 5 is not supported anymore\n"
1217 "Get an older version of LyX (< 1.1.x) for conversion!"
1219 WriteAlert(_("Warning:"),
1220 _("Tabular format < 5 is not supported anymore\n"),
1221 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1223 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1224 >> rotate_arg >> a >> b >> c >> d;
1226 is >> rows_arg >> columns_arg;
1227 Init(rows_arg, columns_arg);
1228 cont_row_info = vector<int>(rows_arg);
1229 SetLongTabular(is_long_tabular_arg);
1230 SetRotateTabular(rotate_arg);
1232 for (i = 0; i < rows_; ++i) {
1234 cont_row_info[i] = false;
1236 for (i = 0; i < columns_; ++i) {
1239 for (i = 0; i < rows_; ++i) {
1240 for (j = 0; j < columns_; ++j) {
1245 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1246 >> rotate_arg >> a >> b >> c >> d;
1247 Init(rows_arg, columns_arg);
1248 cont_row_info = vector<int>(rows_arg);
1249 SetLongTabular(is_long_tabular_arg);
1250 SetRotateTabular(rotate_arg);
1255 for (i = 0; i < rows_; ++i) {
1256 a = b = c = d = e = f = g = h = 0;
1257 is >> a >> b >> c >> d;
1258 row_info[i].top_line = a;
1259 row_info[i].bottom_line = b;
1260 cont_row_info[i] = c;
1261 row_info[i].newpage = d;
1263 for (i = 0; i < columns_; ++i) {
1267 char ch; // skip '"'
1269 getline(is, s1, '"');
1270 is >> ch; // skip '"'
1271 getline(is, s2, '"');
1272 column_info[i].alignment = static_cast<char>(a);
1273 column_info[i].left_line = b;
1274 column_info[i].right_line = c;
1275 column_info[i].p_width = s1;
1276 column_info[i].align_special = s2;
1278 for (i = 0; i < rows_; ++i) {
1279 for (j = 0; j < columns_; ++j) {
1282 is >> a >> b >> c >> d >> e >> f >> g;
1284 is >> ch; // skip '"'
1285 getline(is, s1, '"');
1286 is >> ch; // skip '"'
1287 getline(is, s2, '"');
1288 cell_info[i][j].multicolumn = static_cast<char>(a);
1289 cell_info[i][j].alignment = static_cast<char>(b);
1290 cell_info[i][j].top_line = static_cast<char>(c);
1291 cell_info[i][j].bottom_line = static_cast<char>(d);
1292 cell_info[i][j].rotate = static_cast<bool>(f);
1293 cell_info[i][j].linebreaks = static_cast<bool>(g);
1294 cell_info[i][j].align_special = s1;
1295 cell_info[i][j].p_width = s2;
1299 set_row_column_number_info();
1301 LyXParagraph * par = new LyXParagraph;
1302 LyXParagraph * return_par = 0;
1303 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1304 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1305 string token, tmptok;
1308 LyXFont font(LyXFont::ALL_SANE);
1310 while (lex.IsOK()) {
1312 token = lex.GetString();
1315 if ((token == "\\layout") || (token == "\\end_float")) {
1316 lex.pushToken(token);
1319 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1327 lex.pushToken(token);
1331 lex.printError("New Paragraph allocated! This should not happen!");
1332 lex.pushToken(token);
1338 // now we have the par we should fill the insets with this!
1340 InsetText *inset = GetCellInset(cell);
1343 for(int i=0; i < par->Last(); ++i) {
1344 if (par->IsNewline(i)) {
1346 if (cell > GetNumberOfCells()) {
1347 lyxerr << "Some error in reading old table format occured!" <<
1348 endl << "Terminating when reading cell[" << cell << "]!" <<
1352 row = row_of_cell(cell);
1353 if (cont_row_info[row]) {
1355 cont_row_info.erase(&cont_row_info[row]);
1356 while(!IsFirstCellInRow(--cell));
1358 inset = GetCellInset(cell);
1361 inset = GetCellInset(cell);
1362 row = row_of_cell(cell);
1363 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].linebreaks)
1365 // insert a space instead
1367 par->InsertChar(i, ' ');
1370 par->CopyIntoMinibuffer(i);
1371 inset->par->InsertFromMinibuffer(inset->par->Last());
1377 char const * LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1379 int i = isColumn ? cell : column_of_cell(cell);
1384 //i = column_of_cell(cell);
1385 if (!isColumn && IsMultiColumn(cell)) {
1386 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1387 return cellinfo_of_cell(cell)->align_special.c_str();
1389 switch (GetAlignment(cell)) {
1390 case LYX_ALIGN_LEFT:
1392 case LYX_ALIGN_RIGHT:
1399 if (!column_info[i].align_special.empty()) {
1400 return column_info[i].align_special.c_str();
1402 #ifdef IGNORE_THIS_FOR_NOW
1403 else if (!column_info[i].p_width.empty()) {
1405 file += column_info[i].p_width;
1410 switch (column_info[i].alignment) {
1411 case LYX_ALIGN_LEFT:
1413 case LYX_ALIGN_RIGHT:
1423 // cell <0 will tex the preamble
1424 // returns the number of printed newlines
1425 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1429 //int tmp; // tmp2; // unused
1430 int nvcell; // fcell; // unused
1431 if (IsLastCell(cell)) {
1432 os << newlineAndDepth(--depth)
1434 << newlineAndDepth(--depth)
1436 << newlineAndDepth(--depth)
1438 << newlineAndDepth(--depth);
1439 if (is_long_tabular)
1443 << newlineAndDepth(--depth);
1449 if (is_long_tabular)
1455 << "' COLSEP='1' ROWSEP='1'>"
1456 << newlineAndDepth(++depth);
1458 for (i = 0; i < columns_; ++i) {
1459 os << "<COLSPEC ALIGN='"
1460 << GetDocBookAlign(i, true)
1466 if (i == (columns_-1)) {
1469 if (column_info[i].right_line ||
1470 column_info[i+1].left_line)
1476 << newlineAndDepth(depth);
1478 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1479 if (column_info[i].left_line)
1484 << newlineAndDepth(++depth)
1486 << newlineAndDepth(++depth)
1488 << GetDocBookAlign(0)
1490 if (IsMultiColumn(0)) {
1491 os << " NAMEST='col1' NAMEEND='col"
1492 << cells_in_multicolumn(0)
1496 << newlineAndDepth(++depth);
1499 if (IsLastCellInRow(cell)) {
1500 os << newlineAndDepth(--depth)
1502 << newlineAndDepth(--depth)
1504 << newlineAndDepth(depth)
1506 << newlineAndDepth(++depth)
1508 << GetDocBookAlign(cell + 1)
1509 << "' VALIGN='middle'";
1510 if (IsMultiColumn(cell + 1)) {
1511 os << " NAMEST='col"
1512 << column_of_cell(cell+1) + 1
1514 << column_of_cell(cell + 1) +
1515 cells_in_multicolumn(cell + 1)
1519 << newlineAndDepth(++depth);
1522 os << newlineAndDepth(--depth)
1524 << newlineAndDepth(depth)
1526 << GetDocBookAlign(cell + 1)
1527 << "' VALIGN='middle'";
1528 if (IsMultiColumn(cell + 1)) {
1529 os << " NAMEST='col"
1530 << column_of_cell(cell+1) + 1
1532 << column_of_cell(cell+1) +
1533 cells_in_multicolumn(cell+1)
1537 << newlineAndDepth(++depth);
1546 bool LyXTabular::IsMultiColumn(int cell) const
1548 return (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL);
1552 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1554 int row = row_of_cell(cell);
1555 int column = column_of_cell(cell);
1556 return &cell_info[row][column];
1560 void LyXTabular::SetMultiColumn(int cell, int number)
1562 int new_width = cellinfo_of_cell(cell)->width_of_cell;
1564 cellinfo_of_cell(cell)->multicolumn = LyXTabular::CELL_BEGIN_OF_MULTICOLUMN;
1565 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1566 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1567 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1568 for (number--; number > 0; --number) {
1569 cellinfo_of_cell(cell+number)->multicolumn =
1570 LyXTabular::CELL_PART_OF_MULTICOLUMN;
1571 new_width += cellinfo_of_cell(cell+number)->width_of_cell;
1573 set_row_column_number_info();
1574 SetWidthOfCell(cell, new_width);
1578 int LyXTabular::cells_in_multicolumn(int cell) const
1580 int row = row_of_cell(cell);
1581 int column = column_of_cell(cell);
1584 while (column < columns_ && cell_info[row][column].multicolumn
1585 == LyXTabular::CELL_PART_OF_MULTICOLUMN){
1593 int LyXTabular::UnsetMultiColumn(int cell)
1595 int row = row_of_cell(cell);
1596 int column = column_of_cell(cell);
1600 if (cell_info[row][column].multicolumn
1601 == LyXTabular::CELL_BEGIN_OF_MULTICOLUMN){
1602 cell_info[row][column].multicolumn = LyXTabular::CELL_NORMAL;
1604 while (column < columns_ &&
1605 cell_info[row][column].multicolumn
1606 == LyXTabular::CELL_PART_OF_MULTICOLUMN){
1607 cell_info[row][column].multicolumn =
1608 LyXTabular::CELL_NORMAL;
1613 set_row_column_number_info();
1618 void LyXTabular::SetLongTabular(int what)
1620 is_long_tabular = what;
1624 bool LyXTabular::IsLongTabular() const
1626 return is_long_tabular;
1630 void LyXTabular::SetRotateTabular(int what)
1636 bool LyXTabular::GetRotateTabular() const
1642 void LyXTabular::SetRotateCell(int cell, int what)
1644 cellinfo_of_cell(cell)->rotate = what;
1648 bool LyXTabular::GetRotateCell(int cell) const
1650 return cellinfo_of_cell(cell)->rotate;
1654 bool LyXTabular::NeedRotating() const
1658 for (int i = 0; i < rows_; ++i) {
1659 for (int j = 0; j < columns_; ++j) {
1660 if (cell_info[i][j].rotate)
1668 bool LyXTabular::IsLastCell(int cell) const
1670 if (cell < GetNumberOfCells())
1676 int LyXTabular::GetCellAbove(int cell) const
1678 if (row_of_cell(cell) > 0)
1679 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1684 int LyXTabular::GetCellNumber(int row, int column) const
1686 if (column >= columns_)
1687 column = columns_ - 1;
1688 else if (column < 0)
1695 return cell_info[row][column].cellno;
1699 void LyXTabular::SetLinebreaks(int cell, bool what)
1701 cellinfo_of_cell(cell)->linebreaks = what;
1705 bool LyXTabular::GetLinebreaks(int cell) const
1707 if (column_info[column_of_cell(cell)].p_width.empty() &&
1708 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1710 return cellinfo_of_cell(cell)->linebreaks;
1714 void LyXTabular::SetLTHead(int cell, bool first)
1716 int row = row_of_cell(cell);
1719 if (row == endfirsthead)
1732 bool LyXTabular::GetRowOfLTHead(int cell) const
1734 if ((endhead+1) > rows_)
1736 return (row_of_cell(cell) == endhead);
1740 bool LyXTabular::GetRowOfLTFirstHead(int cell) const
1742 if ((endfirsthead+1) > rows_)
1744 return (row_of_cell(cell) == endfirsthead);
1748 void LyXTabular::SetLTFoot(int cell, bool last)
1750 int row = row_of_cell(cell);
1753 if (row == endlastfoot)
1766 bool LyXTabular::GetRowOfLTFoot(int cell) const
1768 if ((endfoot+1) > rows_)
1770 return (row_of_cell(cell) == endfoot);
1773 bool LyXTabular::GetRowOfLTLastFoot(int cell) const
1775 if ((endlastfoot+1) > rows_)
1777 return (row_of_cell(cell) == endlastfoot);
1781 void LyXTabular::SetLTNewPage(int cell, bool what)
1783 row_info[row_of_cell(cell)].newpage = what;
1787 bool LyXTabular::GetLTNewPage(int cell) const
1789 return row_info[row_of_cell(cell)].newpage;
1793 void LyXTabular::SetAscentOfRow(int row, int height)
1797 row_info[row].ascent_of_row = height;
1801 void LyXTabular::SetDescentOfRow(int row, int height)
1805 row_info[row].descent_of_row = height;
1809 int LyXTabular::GetAscentOfRow(int row) const
1813 return row_info[row].ascent_of_row;
1817 int LyXTabular::GetDescentOfRow(int row) const
1821 return row_info[row].descent_of_row;
1825 int LyXTabular::GetHeightOfTabular() const
1829 for(int row = 0; row < rows_; ++row)
1830 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1831 GetAdditionalHeight(GetCellNumber(row, 0));
1836 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1838 if ((row >= rows_) || (column >= columns_))
1840 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1844 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1846 int fcell = GetFirstCellInRow(row);
1847 int n = NumberOfCellsInRow(fcell) + fcell;
1851 for (i = fcell; i < n; ++i) {
1855 if (tmp == (n - fcell)){
1858 for (i = fcell; i < n; ++i) {
1861 << column_of_cell(i) + 1
1863 << right_column_of_cell(i) + 1
1876 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1878 int fcell = GetFirstCellInRow(row);
1879 int n = NumberOfCellsInRow(fcell) + fcell;
1883 for (i = fcell; i < n; ++i) {
1887 if (tmp == (n-fcell)){
1890 for (i = fcell; i < n; ++i) {
1891 if (BottomLine(i)) {
1893 << column_of_cell(i) + 1
1895 << right_column_of_cell(i) + 1
1908 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1912 if (GetRotateCell(cell)) {
1913 os << "\\begin{sideways}" << endl;
1916 if (IsMultiColumn(cell)) {
1917 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1918 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1919 os << cellinfo_of_cell(cell+1)->align_special << "}{";
1923 if (!GetPWidth(cell).empty()) {
1924 os << "p{" << GetPWidth(cell) << '}';
1926 switch (GetAlignment(cell)) {
1927 case LYX_ALIGN_LEFT:
1930 case LYX_ALIGN_RIGHT:
1938 if (RightLine(cell))
1940 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1946 if (GetLinebreaks(cell)) {
1947 os << "\\parbox[t]{" << GetPWidth(cell) << "}{\\smallskip{}";
1953 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
1958 if (GetLinebreaks(cell))
1959 os << "\\smallskip{}}";
1960 if (IsMultiColumn(cell)){
1963 if (GetRotateCell(cell)) {
1964 os << endl << "\\end{sideways}";
1971 int LyXTabular::Latex(ostream & os, bool fragile, bool fp) const
1977 //+---------------------------------------------------------------------
1978 //+ first the opening preamble +
1979 //+---------------------------------------------------------------------
1982 os << "\\begin{sideways}" << endl;
1985 if (is_long_tabular)
1986 os << "\\begin{longtable}{";
1988 os << "\\begin{tabular}{";
1989 for (i = 0; i < columns_; ++i) {
1990 if (column_info[i].left_line)
1992 if (!column_info[i].align_special.empty()) {
1993 os << column_info[i].align_special;
1994 } else if (!column_info[i].p_width.empty()) {
1996 << column_info[i].p_width
1999 switch (column_info[i].alignment) {
2000 case LYX_ALIGN_LEFT:
2003 case LYX_ALIGN_RIGHT:
2011 if (column_info[i].right_line)
2017 //+---------------------------------------------------------------------
2018 //+ the single row and columns (cells) +
2019 //+---------------------------------------------------------------------
2021 for(i=0; i < rows_; ++i) {
2022 ret += TeXTopHLine(os, i);
2023 for(j=0; j < columns_; ++j) {
2024 if (IsPartOfMultiColumn(i,j))
2026 ret += TeXCellPreamble(os, cell);
2027 ret += GetCellInset(cell)->Latex(os, fragile, fp);
2028 ret += TeXCellPostamble(os, cell);
2029 if (!IsLastCellInRow(cell)) { // not last cell in row
2035 os << "\\\\" << endl;
2036 ret += TeXBottomHLine(os, i);
2037 if (IsLongTabular()) {
2039 os << "\\endhead\n";
2042 if (i == endfirsthead) {
2043 os << "\\endfirsthead\n";
2047 os << "\\endfoot\n";
2050 if (i == endlastfoot) {
2051 os << "\\endlastfoot\n";
2054 if (row_info[i].newpage) {
2055 os << "\\newpage\n";
2061 //+---------------------------------------------------------------------
2062 //+ the closing of the tabular +
2063 //+---------------------------------------------------------------------
2065 if (is_long_tabular)
2066 os << "\\end{longtable}";
2068 os << "\\end{tabular}";
2070 os << "\n\\end{sideways}";
2078 InsetText * LyXTabular::GetCellInset(int cell) const
2080 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2083 void LyXTabular::Validate(LaTeXFeatures & features) const
2085 if (IsLongTabular())
2086 features.longtable = true;
2088 features.rotating = true;
2089 for(int cell = 0; cell < numberofcells; ++cell)
2090 GetCellInset(cell)->Validate(features);