1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 * ======================================================
18 #include "support/lstrings.h"
21 #pragma implementation
24 extern void addNewlineAndDepth(string &file, int const depth); // Jug 990923
26 #define WIDTH_OF_LINE 5
29 LyXTable::LyXTable(int rows_arg, int columns_arg)
31 Init(rows_arg, columns_arg);
35 LyXTable::LyXTable(LyXLex &lex)
37 FILE *file = lex.getFile();
42 LyXTable::~LyXTable() {
45 delete[] columnofcell;
48 for (i = 0; i<rows; i++) {
49 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
55 LyXTable * LyXTable::Clone()
57 LyXTable *result = new LyXTable(rows, columns);
60 for (row = 0; row<rows; row++){
61 for (column = 0; column<columns;column++){
62 result->cell_info[row][column] = cell_info[row][column];
66 for (row = 0; row<rows; row++){
67 result->row_info[row] = row_info[row];
70 for (column = 0; column<columns; column++){
71 result->column_info[column].left_line = column_info[column].left_line;
72 result->column_info[column].right_line = column_info[column].right_line;
73 result->column_info[column].alignment = column_info[column].alignment;
74 result->column_info[column].p_width = column_info[column].p_width;
75 result->column_info[column].align_special = column_info[column].align_special;
78 result->SetLongTable(is_long_table);
79 result->rotate = rotate;
85 /* activates all lines and sets all widths to 0 */
86 void LyXTable::Init(int rows_arg, int columns_arg)
90 columns = columns_arg;
91 column_info = new columnstruct[columns];
92 row_info = new rowstruct[rows];
93 cell_info = new cellstruct*[rows];
96 for (i = 0; i<rows;i++) {
97 cell_info[i] = new cellstruct[columns];
98 row_info[i].top_line = true;
99 row_info[i].bottom_line = false;
100 row_info[i].is_cont_row = false;
101 row_info[i].newpage = false;
102 for (j = 0; j<columns; j++) {
103 cell_info[i][j].cellno = cellno++;
104 cell_info[i][j].width_of_cell = 0;
105 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
106 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
107 cell_info[i][j].top_line = row_info[i].top_line;
108 cell_info[i][j].bottom_line = row_info[i].bottom_line;
109 cell_info[i][j].has_cont_row = false;
110 cell_info[i][j].rotate = false;
111 cell_info[i][j].linebreaks = false;
114 row_info[i-1].bottom_line = true;
115 row_info[0].bottom_line = true;
117 for (i = 0; i<columns;i++) {
118 column_info[i].left_line = true;
119 column_info[i].right_line = false;
120 column_info[i].alignment = LYX_ALIGN_CENTER;
121 // set width_of_column to zero before it is used in
122 // calculate_width_of_column() (thornley)
123 column_info[i].width_of_column = 0;
124 calculate_width_of_column(i);
126 column_info[i-1].right_line = true;
128 calculate_width_of_table();
132 set_row_column_number_info();
133 is_long_table = false;
142 void LyXTable::AppendRow(int cell)
144 int row = row_of_cell(cell);
145 rowstruct *row_info2 = new rowstruct[rows+1];
146 cellstruct** cell_info2 = new cellstruct*[rows+1];
149 for (i = 0; i <= row; i++) {
150 cell_info2[i] = cell_info[i];
151 row_info2[i] = row_info[i];
153 for (i = rows-1; i >= row; i--) {
154 cell_info2[i+1] = cell_info[i];
155 row_info2[i+1] = row_info[i];
157 for (i = row;row_info[i].is_cont_row;i--);
158 if (((row+1)>= rows) || !row_info[row+1].is_cont_row)
159 row_info2[row+1].is_cont_row = false;
160 row_info2[row+1].top_line = row_info[i].top_line;
161 cell_info2[row+1] = new cellstruct[columns];
162 for (i = 0; i<columns; i++) {
163 cell_info2[row+1][i].width_of_cell = 0;
164 cell_info2[row+1][i] = cell_info2[row][i];
168 cell_info = cell_info2;
170 row_info = row_info2;
178 void LyXTable::DeleteRow(int cell)
180 int row = row_of_cell(cell);
181 while(!row_info[row].is_cont_row && RowHasContRow(cell))
182 DeleteRow(cell_info[row+1][0].cellno);
183 rowstruct *row_info2 = new rowstruct[rows-1];
184 cellstruct** cell_info2 = new cellstruct*[rows-1];
187 delete[] cell_info[row];
188 for (i = 0; i < row; i++) {
189 cell_info2[i] = cell_info[i];
190 row_info2[i] = row_info[i];
192 if (row_info[i].is_cont_row)
193 row_info2[i-1].bottom_line = row_info[i].bottom_line;
194 for (i = row; i < rows - 1; i++) {
195 cell_info2[i] = cell_info[i+1];
196 row_info2[i] = row_info[i+1];
201 cell_info = cell_info2;
203 row_info = row_info2;
211 void LyXTable::AppendColumn(int cell)
214 columnstruct *column_info2 = new columnstruct[columns+1];
215 int column = right_column_of_cell(cell);
217 for (i = 0; i<= column; i++){
218 column_info2[i] = column_info[i];
220 for (i = columns-1; i>= column; i--){
221 column_info2[i+1] = column_info[i];
224 delete[] column_info;
225 column_info = column_info2;
227 for (i = 0; i<rows;i++){
228 cellstruct* tmp = cell_info[i];
229 cell_info[i] = new cellstruct[columns+1];
230 for (j = 0; j<= column; j++){
231 cell_info[i][j] = tmp[j];
233 for (j = column; j<columns; j++){
234 cell_info[i][j+1] = tmp[j];
236 // care about multicolumns
237 if (cell_info[i][column+1].multicolumn
238 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
239 cell_info[i][column+1].multicolumn =
240 LyXTable::CELL_PART_OF_MULTICOLUMN;
242 if (column + 1 == columns
243 || cell_info[i][column+2].multicolumn
244 != LyXTable::CELL_PART_OF_MULTICOLUMN){
245 cell_info[i][column+1].multicolumn =
246 LyXTable::CELL_NORMAL;
257 void LyXTable::Reinit()
261 for (i = 0; i<rows;i++) {
262 for (j = 0; j<columns; j++) {
263 cell_info[i][j].width_of_cell = 0;
264 if ((i+1 < rows) && !row_info[i+1].is_cont_row)
265 cell_info[i][j].has_cont_row = false;
269 for (i = 0; i<columns;i++) {
270 calculate_width_of_column(i);
272 calculate_width_of_table();
274 set_row_column_number_info();
278 void LyXTable::set_row_column_number_info()
284 for (row = 0; row<rows; ++row) {
285 for (column = 0; column<columns; ++column) {
286 if (cell_info[row][column].multicolumn
287 != LyXTable::CELL_PART_OF_MULTICOLUMN)
289 cell_info[row][column].cellno = numberofcells;
292 ++numberofcells; // because this is one more than as we start from 0
298 rowofcell = new int[numberofcells];
300 delete [] columnofcell;
301 columnofcell = new int[numberofcells];
303 while (c < numberofcells && row < rows && column < columns) {
305 columnofcell[c] = column;
309 } while (column < columns &&
310 cell_info[row][column].multicolumn
311 == LyXTable::CELL_PART_OF_MULTICOLUMN);
312 if (column == columns){
320 void LyXTable::DeleteColumn(int cell)
322 int column1 = column_of_cell(cell);
323 int column2 = right_column_of_cell(cell);
326 if (column1 == 0 && column2 == columns - 1)
329 for (column = column1; column <= column2;column++){
330 delete_column(column1);
336 int LyXTable::GetNumberOfCells()
338 return numberofcells;
342 int LyXTable::NumberOfCellsInRow(int cell)
344 int row = row_of_cell(cell);
347 for (i = 0; i<columns; i++){
348 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
355 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
357 return (right_column_of_cell(append_cell) ==
358 right_column_of_cell(question_cell));
362 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
364 if (column_of_cell(delete_column_cell) == 0 &&
365 right_column_of_cell(delete_column_cell) == columns - 1)
369 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
370 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
374 /* returns 1 if there is a topline, returns 0 if not */
375 bool LyXTable::TopLine(int cell)
377 int row = row_of_cell(cell);
380 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
381 if (IsMultiColumn(cell))
382 return cellinfo_of_cell(cell)->top_line;
383 return row_info[row].top_line;
387 bool LyXTable::BottomLine(int cell)
389 //no bottom line underneath non-existent cells if you please
390 if(cell >= numberofcells)
393 int row = row_of_cell(cell);
395 if (RowHasContRow(cell))
396 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
397 if (IsMultiColumn(cell))
398 return cellinfo_of_cell(cell)->bottom_line;
399 return row_info[row_of_cell(cell)].bottom_line;
403 bool LyXTable::LeftLine(int cell)
405 return column_info[column_of_cell(cell)].left_line;
408 bool LyXTable::RightLine(int cell)
410 return column_info[right_column_of_cell(cell)].right_line;
414 bool LyXTable::TopAlreadyDrawed(int cell)
416 if (AdditionalHeight(cell))
418 int row = row_of_cell(cell);
420 int column = column_of_cell(cell);
422 && cell_info[row-1][column].multicolumn
423 == LyXTable::CELL_PART_OF_MULTICOLUMN)
425 if (cell_info[row-1][column].multicolumn
426 == LyXTable::CELL_NORMAL)
427 return row_info[row-1].bottom_line;
429 return cell_info[row-1][column].bottom_line;
435 bool LyXTable::VeryLastRow(int cell)
437 return (row_of_cell(cell) == rows-1);
441 int LyXTable::AdditionalHeight(int cell)
443 int row = row_of_cell(cell);
448 for (column = 0;column < columns-1 && bottom;column++){
449 switch (cell_info[row-1][column].multicolumn){
450 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
451 bottom = cell_info[row-1][column].bottom_line;
453 case LyXTable::CELL_NORMAL:
454 bottom = row_info[row-1].bottom_line;
457 for (column = 0;column < columns-1 && top;column++){
458 switch (cell_info[row][column].multicolumn){
459 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
460 top = cell_info[row][column].top_line;
462 case LyXTable::CELL_NORMAL:
463 top = row_info[row].top_line;
467 return WIDTH_OF_LINE;
473 int LyXTable::AdditionalWidth(int cell)
475 // internally already set in SetWidthOfCell
476 // used to get it back in text.C
477 int col = right_column_of_cell(cell);
478 if (col < columns - 1 && column_info[col].right_line &&
479 column_info[col+1].left_line)
480 return WIDTH_OF_LINE;
486 // returns the maximum over all rows
487 int LyXTable::WidthOfColumn(int cell)
489 int column1 = column_of_cell(cell);
490 int column2 = right_column_of_cell(cell);
493 for (i = column1; i<= column2;i++){
494 result += column_info[i].width_of_column;
500 int LyXTable::WidthOfTable()
502 return width_of_table;
505 /* returns 1 if a complete update is necessary, otherwise 0 */
506 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
508 if (!IsMultiColumn(cell))
511 int row = row_of_cell(cell);
512 int column1 = column_of_cell(cell);
513 int column2 = right_column_of_cell(cell);
517 // first set columns to 0 so we can calculate the right width
518 for (i = column1; i<= column2;i++) {
519 cell_info[row][i].width_of_cell = 0;
521 // set the width to MAX_WIDTH until width > 0
522 width = (new_width + 2*WIDTH_OF_LINE);
523 for (i = column1; (i<column2) && (width > 0);i++){
524 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
525 width -= column_info[i].width_of_column;
528 cell_info[row][i].width_of_cell = width;
533 void LyXTable::recalculateMulticolCells(int cell, int new_width)
536 row = row_of_cell(cell),
537 column1 = column_of_cell(cell),
538 column2 = right_column_of_cell(cell),
541 // first set columns to 0 so we can calculate the right width
542 for (i = column1; i<= column2;++i)
543 cell_info[row][i].width_of_cell = 0;
544 for(i = cell+1;(i<numberofcells) && (!IsMultiColumn(i));++i)
546 if (i < numberofcells)
547 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
548 SetWidthOfMulticolCell(cell, new_width);
551 /* returns 1 if a complete update is necessary, otherwise 0 */
552 bool LyXTable::SetWidthOfCell(int cell, int new_width)
554 int row = row_of_cell(cell);
555 int column1 = column_of_cell(cell);
559 if (IsMultiColumn(cell)) {
560 tmp = SetWidthOfMulticolCell(cell, new_width);
562 width = (new_width + 2*WIDTH_OF_LINE);
563 cell_info[row][column1].width_of_cell = width;
564 if (column_info[column1].right_line && (column1 < columns-1) &&
565 column_info[column1+1].left_line) // additional width
566 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
567 tmp = calculate_width_of_column_NMC(column1);
571 for(i = 0; i<columns;++i)
572 calculate_width_of_column_NMC(i);
573 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
576 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
577 for(i = 0; i<columns;++i)
578 calculate_width_of_column(i);
579 calculate_width_of_table();
586 bool LyXTable::SetAlignment(int cell, char align)
588 if (!IsMultiColumn(cell))
589 column_info[column_of_cell(cell)].alignment = align;
590 cellinfo_of_cell(cell)->alignment = align;
594 bool LyXTable::SetPWidth(int cell, string width)
596 int fvcell = FirstVirtualCell(cell);
598 if (IsMultiColumn(fvcell)) {
599 // if (column_info[column_of_cell(cell)].p_width.empty())
600 // column_info[column_of_cell(cell)].p_width = width;
601 cellinfo_of_cell(fvcell)->p_width = width;
603 column_info[column_of_cell(fvcell)].p_width = width;
604 if (!width.empty()) // do this only if there is a width
605 SetAlignment(cell, LYX_ALIGN_LEFT);
610 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
612 if (what == SET_SPECIAL_MULTI)
613 cellinfo_of_cell(cell)->align_special = special;
615 column_info[column_of_cell(cell)].align_special = special;
619 bool LyXTable::SetAllLines(int cell, bool line)
621 SetTopLine(cell, line);
622 SetBottomLine(cell, line);
623 SetRightLine(cell, line);
624 SetLeftLine(cell, line);
628 bool LyXTable::SetTopLine(int cell, bool line)
630 int row = row_of_cell(cell);
633 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
634 else if (!IsMultiColumn(cell))
635 row_info[row].top_line = line;
637 cellinfo_of_cell(cell)->top_line = line;
642 bool LyXTable::SetBottomLine(int cell, bool line)
644 int row = row_of_cell(cell);
646 if (RowHasContRow(cell))
647 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
648 else if (!IsMultiColumn(cell))
649 row_info[row_of_cell(cell)].bottom_line = line;
651 cellinfo_of_cell(cell)->bottom_line = line;
656 bool LyXTable::SetLeftLine(int cell, bool line)
658 column_info[column_of_cell(cell)].left_line = line;
663 bool LyXTable::SetRightLine(int cell, bool line)
665 column_info[right_column_of_cell(cell)].right_line = line;
670 char LyXTable::GetAlignment(int cell)
672 if (IsMultiColumn(cell))
673 return cellinfo_of_cell(cell)->alignment;
675 return column_info[column_of_cell(cell)].alignment;
678 string LyXTable::GetPWidth(int cell)
680 int fvcell = FirstVirtualCell(cell);
682 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
683 return cellinfo_of_cell(fvcell)->p_width;
684 return column_info[column_of_cell(fvcell)].p_width;
687 string LyXTable::GetAlignSpecial(int cell, int what)
689 if (what == SET_SPECIAL_MULTI)
690 return cellinfo_of_cell(cell)->align_special;
691 return column_info[column_of_cell(cell)].align_special;
694 int LyXTable::GetWidthOfCell(int cell)
696 int row = row_of_cell(cell);
697 int column1 = column_of_cell(cell);
698 int column2 = right_column_of_cell(cell);
701 for (i = column1; i<= column2;i++){
702 result += cell_info[row][i].width_of_cell;
705 result += AdditionalWidth(cell);
711 int LyXTable::GetBeginningOfTextInCell(int cell)
715 switch (GetAlignment(cell)){
716 case LYX_ALIGN_CENTER:
717 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
719 case LYX_ALIGN_RIGHT:
720 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
722 default: /* LYX_ALIGN_LEFT: nothing :-) */
732 bool LyXTable::IsFirstCell(int cell)
734 return (column_of_cell(cell) == 0);
737 bool LyXTable::IsLastCell(int cell)
739 return (right_column_of_cell(cell) == (columns-1));
743 bool LyXTable::calculate_width_of_column(int column)
746 int old_column_width = column_info[column].width_of_column;
748 for (i = 0; i<rows; i++) {
749 if (cell_info[i][column].width_of_cell > max) {
750 max = cell_info[i][column].width_of_cell;
753 column_info[column].width_of_column = max;
754 return (column_info[column].width_of_column != old_column_width);
757 bool LyXTable::calculate_width_of_column_NMC(int column)
760 int old_column_width = column_info[column].width_of_column;
762 for (i = 0; i<rows; ++i) {
763 if (!IsMultiColumn(GetCellNumber(column, i)) &&
764 (cell_info[i][column].width_of_cell > max)) {
765 max = cell_info[i][column].width_of_cell;
768 column_info[column].width_of_column = max;
769 return (column_info[column].width_of_column != old_column_width);
772 void LyXTable::calculate_width_of_table()
775 for (int i = 0; i < columns; ++i) {
776 width_of_table += column_info[i].width_of_column;
781 int LyXTable::row_of_cell(int cell)
783 if (cell >= numberofcells)
787 return rowofcell[cell];
791 int LyXTable::column_of_cell(int cell)
793 if (cell >= numberofcells)
797 return columnofcell[cell];
801 int LyXTable::right_column_of_cell(int cell)
803 int row = row_of_cell(cell);
804 int column = column_of_cell(cell);
805 while (column < columns - 1 &&
806 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
812 void LyXTable::Write(ostream & os)
816 << rows << " " << columns << " " << is_long_table << " "
817 << rotate << " " << endhead << " " << endfirsthead << " "
818 << endfoot << " " << endlastfoot << "\n";
819 for (i = 0; i < rows; ++i) {
820 os << row_info[i].top_line << " "
821 << row_info[i].bottom_line << " "
822 << row_info[i].is_cont_row << " "
823 << row_info[i].newpage << "\n";
825 for (i = 0; i < columns; ++i) {
826 os << column_info[i].alignment << " "
827 << column_info[i].left_line << " "
828 << column_info[i].right_line << " \""
829 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
830 << column_info[i].align_special << "\"\n";
833 for (i = 0; i < rows; ++i) {
834 for (j = 0; j < columns; ++j) {
835 os << cell_info[i][j].multicolumn << " "
836 << cell_info[i][j].alignment << " "
837 << cell_info[i][j].top_line << " "
838 << cell_info[i][j].bottom_line << " "
839 << cell_info[i][j].has_cont_row << " "
840 << cell_info[i][j].rotate << " "
841 << cell_info[i][j].linebreaks << " \""
842 << cell_info[i][j].align_special << "\" \""
843 << cell_info[i][j].p_width << "\"\n";
849 void LyXTable::Read(FILE * file)
855 int is_long_table_arg = false;
856 int rotate_arg = false;
866 char vtmp[100], stmp[100], atmp[100];
868 fscanf(file, "%s\n", vtmp);
871 version = atoi(s.c_str() + 8);
875 #warning Insert a error message window here that this format is not supported anymore
878 fprintf(stderr, "Tabular format < 5 is not supported anymore\n"
879 "Get an older version of LyX (< 1.1.x) for conversion!\n");
883 fgets(vtmp, sizeof(vtmp), file);
884 sscanf(vtmp, "%d %d %d %d %d %d %d %d\n", &rows_arg, &columns_arg,
885 &is_long_table_arg, &rotate_arg, &a, &b, &c, &d);
886 Init(rows_arg, columns_arg);
887 SetLongTable(is_long_table_arg);
888 SetRotateTable(rotate_arg);
893 for (i = 0; i<rows; i++){
894 a = b = c = d = e = f = g = h = 0;
895 fgets(vtmp, sizeof(vtmp), file);
896 sscanf(vtmp, "%d %d %d %d\n",
898 row_info[i].top_line = a;
899 row_info[i].bottom_line = b;
900 row_info[i].is_cont_row = c;
901 row_info[i].newpage = d;
903 for (i = 0; i<columns; i++){
906 fgets(vtmp, sizeof(vtmp), file);
907 sscanf(vtmp, "%d %d %d %s %s", &a, &b, &c, stmp, atmp);
908 column_info[i].alignment = static_cast<char>(a);
909 column_info[i].left_line = b;
910 column_info[i].right_line = c;
911 if (*stmp == '"') { /* strip quotes if they exists */
914 // there are quotes so I have to reread the string correctly
915 // this is only because the old format did not have "
916 // this means also that atmp is ONLY set here!!!
918 sscanf(vtmp, "%*d %*d %*d %*s \"%[^\"]\"", atmp);
919 else // otherwise after the first empty "" read is aborded
920 sscanf(vtmp, "%*d %*d %*d \"%[^\"]\" \"%[^\"]\"", stmp, atmp);
921 column_info[i].p_width = stmp;
922 column_info[i].align_special = atmp;
924 column_info[i].p_width = stmp;
926 for (i = 0; i < rows; ++i) {
927 for (j = 0; j < columns; ++j) {
930 a = b = c = d = e = f = g = 0;
931 fgets(vtmp, sizeof(vtmp), file);
932 sscanf(vtmp, "%d %d %d %d %d %d %d %s %s\n",
933 &a, &b, &c, &d, &e, &f, &g, stmp, atmp);
934 cell_info[i][j].multicolumn = static_cast<char>(a);
935 cell_info[i][j].alignment = static_cast<char>(b);
936 cell_info[i][j].top_line = static_cast<char>(c);
937 cell_info[i][j].bottom_line = static_cast<char>(d);
938 cell_info[i][j].has_cont_row = static_cast<bool>(e);
939 cell_info[i][j].rotate = static_cast<bool>(f);
940 cell_info[i][j].linebreaks = static_cast<bool>(g);
941 // this is only to see if I have an empty string first
942 // this clause should be always TRUE!!!
947 sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d %*s \"%[^\"]\"",
949 else // otherwise after the first empty "" read is aborded
950 sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d \"%[^\"]\" \"%[^\"]\"",
952 cell_info[i][j].align_special = stmp;
953 cell_info[i][j].p_width = atmp;
955 cell_info[i][j].align_special = stmp;
958 set_row_column_number_info();
962 // cell <0 will tex the preamble
963 // returns the number of printed newlines
964 int LyXTable::TexEndOfCell(string & file, int cell)
970 if (ShouldBeVeryLastCell(cell)) {
971 // the very end at the very beginning
972 if (Linebreaks(cell))
973 file += "\\smallskip{}}";
974 if (IsMultiColumn(cell))
976 if (RotateCell(cell)) {
977 file += "\n\\end{sideways}";
985 while (!IsFirstCell(fcell))fcell--;
986 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
987 if (BottomLine(fcell+i))
990 if (tmp == NumberOfCellsInRow(fcell)){
994 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
995 if (BottomLine(fcell+i)){
997 file += tostr(column_of_cell(fcell+i)+1);
999 file += tostr(right_column_of_cell(fcell+i)+1);
1010 file += "\\end{longtable}";
1012 file += "\\end{tabular}";
1014 file += "\n\\end{sideways}";
1018 nvcell = NextVirtualCell(cell+1);
1022 file += "\\begin{sideways}\n";
1026 file += "\\begin{longtable}{";
1028 file += "\\begin{tabular}{";
1029 for (i = 0; i<columns;i++){
1030 if (column_info[i].left_line)
1032 if (!column_info[i].align_special.empty()) {
1033 file += column_info[i].align_special.c_str();
1034 } else if (!column_info[i].p_width.empty()) {
1036 file += column_info[i].p_width;
1039 switch (column_info[i].alignment) {
1040 case LYX_ALIGN_LEFT:
1043 case LYX_ALIGN_RIGHT:
1051 if (column_info[i].right_line)
1057 if (GetNumberOfCells()){
1059 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1060 if (TopLine(fcell+i))
1063 if (tmp == NumberOfCellsInRow(fcell)){
1067 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1068 if (TopLine(fcell+i)){
1070 file += tostr(column_of_cell(fcell+i)+1);
1072 file += tostr(right_column_of_cell(fcell+i)+1);
1083 if (RotateCell(0)) {
1084 file += "\\begin{sideways}\n";
1089 if (Linebreaks(cell))
1090 file += "\\smallskip{}}";
1091 if (IsMultiColumn(cell)){
1094 if (RotateCell(cell)) {
1095 file += "\n\\end{sideways}";
1098 if (IsLastCell(cell)) {
1099 int row = row_of_cell(cell);
1100 string hline1, hline2;
1101 bool print_hline = true;
1102 bool pr_top_hline, flag1, flag2;
1103 flag1 = IsLongTable() &&
1104 ((row == endhead) || (row == endfirsthead) ||
1105 (row == endfoot) || (row == endlastfoot));
1107 flag2 = IsLongTable() &&
1108 ((row <= endhead) || (row <= endfirsthead) ||
1109 (row <= endfoot) || (row <= endlastfoot));
1111 // print the bottom hline only if (otherwise it is doubled):
1112 // - is no LongTable
1113 // - there IS a first-header
1114 // - the next row is no special header/footer
1115 // & this row is no special header/footer
1116 // - the next row is a special header/footer
1117 // & this row is a special header/footer
1118 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1119 (endfirsthead == endhead);
1124 while (!IsFirstCell(fcell))
1126 for (i = 0; i < NumberOfCellsInRow(cell); i++){
1127 if (BottomLine(fcell+i))
1130 if (tmp == NumberOfCellsInRow(cell)){
1132 hline1 = "\\hline ";
1135 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1136 if (BottomLine(fcell+i)){
1138 file += tostr(column_of_cell(fcell+i)+1);
1140 file += tostr(right_column_of_cell(fcell+i)+1);
1142 hline1 += "\\cline{";
1143 hline1 += tostr(column_of_cell(fcell+i)+1);
1145 hline1 += tostr(right_column_of_cell(fcell+i)+1);
1155 if (IsLongTable() && (row == endfoot)) {
1156 file += "\\endfoot\n";
1158 print_hline = false; // no double line below footer
1160 if (IsLongTable() && (row == endlastfoot)) {
1161 file += "\\endlastfoot\n";
1163 print_hline = false; // no double line below footer
1165 if (IsLongTable() && row_info[row].newpage) {
1166 file += "\\newpage\n";
1168 print_hline = false; // no line below a \\newpage-command
1171 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1172 !ShouldBeVeryLastCell(cell)) {
1174 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1175 if (TopLine(fcell+i))
1178 if (tmp == NumberOfCellsInRow(fcell)){
1181 hline2 = "\\hline ";
1185 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1186 if (TopLine(fcell+i)){
1189 file += tostr(column_of_cell(fcell+i)+1);
1191 file += tostr(right_column_of_cell(fcell+i)+1);
1194 hline2 += "\\cline{";
1195 hline2 += tostr(column_of_cell(fcell+i)+1);
1197 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1203 if (tmp && print_hline){
1208 // the order here is important as if one defines two
1209 // or more things in one line only the first entry is
1210 // displayed the other are set to an empty-row. This
1211 // is important if I have a footer and want that the
1212 // lastfooter is NOT displayed!!!
1213 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1214 (row == endfoot) || (row == endlastfoot);
1216 // sflag2 = IsLongTable() && (row >= 0) &&
1217 // (sflag2 || (row == endhead) || (row == endfirsthead));
1219 bool sflag1 = IsLongTable() && (row != endhead) &&
1220 (row != endfirsthead) &&
1221 ((row == endfoot) || (row == endlastfoot));
1223 if (IsLongTable() && (row == endhead)) {
1224 file += "\\endhead\n";
1227 if (IsLongTable() && (row == endfirsthead)) {
1228 file += "\\endfirsthead\n";
1231 if (sflag1) { // add the \hline for next foot row
1232 if (!hline1.empty()) {
1233 file += hline1 + '\n';
1237 // add the \hline for the first row
1238 if (pr_top_hline && sflag2) {
1239 if (!hline2.empty()) {
1240 file += hline2 + '\n';
1244 if (nvcell < numberofcells && RotateCell(nvcell)) {
1245 file += "\\begin{sideways}\n";
1251 if (nvcell < numberofcells && RotateCell(nvcell)) {
1252 file += "\\begin{sideways}\n";
1257 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1258 file += "\\multicolumn{";
1259 file += tostr(cells_in_multicolumn(nvcell));
1261 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1262 file += cellinfo_of_cell(cell+1)->align_special;
1265 if (LeftLine(nvcell))
1267 if (!GetPWidth(nvcell).empty()) {
1269 file += GetPWidth(nvcell);
1272 switch (GetAlignment(nvcell)) {
1273 case LYX_ALIGN_LEFT: file += 'l'; break;
1274 case LYX_ALIGN_RIGHT: file += 'r'; break;
1275 default: file += 'c'; break;
1278 if (RightLine(nvcell))
1280 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1281 if (((nvcell+1) < numberofcells) &&
1282 (NextVirtualCell(nvcell+1) < numberofcells) &&
1283 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1284 LeftLine(NextVirtualCell(nvcell+1)))
1289 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1290 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1291 file += "\\parbox{";
1292 file += GetPWidth(nvcell);
1293 file += "}{\\smallskip{}";
1300 // cell <0 will tex the preamble
1301 // returns the number of printed newlines
1302 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1307 if (cell == GetNumberOfCells() - 1){
1308 // the very end at the very beginning
1309 if (CellHasContRow(cell) >= 0) {
1315 if (row_info[row_of_cell(cell)].bottom_line) {
1319 os << ".TE\n.pl 1c";
1324 os << "\n.pl 500c\n.TS\n";
1325 for (j = 0; j<rows; ++j) {
1326 for (i = 0; i<columns; ++i, ++fcell) {
1327 if (column_info[i].left_line)
1329 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1332 switch (column_info[i].alignment) {
1333 case LYX_ALIGN_LEFT:
1336 case LYX_ALIGN_RIGHT:
1344 if (!column_info[i].p_width.empty())
1345 os << "w(" << column_info[i].p_width << ")";
1346 if (column_info[i].right_line)
1349 if ((j + 1) < rows) {
1356 if (row_info[0].top_line) {
1360 if (CellHasContRow(0) >= 0) {
1366 if (CellHasContRow(cell) >= 0) {
1370 if (right_column_of_cell(cell) == columns -1){
1373 int row = row_of_cell(cell);
1374 if (row_info[row++].bottom_line) {
1378 if ((row < rows) && row_info[row].top_line) {
1384 if ((cell < GetNumberOfCells() - 1) &&
1385 (CellHasContRow(cell+1) >= 0)) {
1395 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1401 i = column_of_cell(cell);
1402 if (!isColumn && IsMultiColumn(cell)) {
1403 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1404 return cellinfo_of_cell(cell)->align_special.c_str();
1406 switch (GetAlignment(cell)) {
1407 case LYX_ALIGN_LEFT:
1409 case LYX_ALIGN_RIGHT:
1416 if (!column_info[i].align_special.empty()) {
1417 return column_info[i].align_special.c_str();
1419 #ifdef IGNORE_THIS_FOR_NOW
1420 else if (!column_info[i].p_width.empty()) {
1422 file += column_info[i].p_width;
1427 switch (column_info[i].alignment) {
1428 case LYX_ALIGN_LEFT:
1430 case LYX_ALIGN_RIGHT:
1439 // cell <0 will tex the preamble
1440 // returns the number of printed newlines
1441 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1445 //int tmp; // tmp2; //Â unused
1446 int nvcell; // fcell; //Â unused
1447 if (ShouldBeVeryLastCell(cell)) {
1448 addNewlineAndDepth(file,--depth);
1450 addNewlineAndDepth(file,--depth);
1452 addNewlineAndDepth(file,--depth);
1454 addNewlineAndDepth(file,--depth);
1456 file += "</TGROUP>";
1458 file += "</TGROUP>";
1459 addNewlineAndDepth(file,--depth);
1462 nvcell = NextVirtualCell(cell+1);
1470 file += tostr(columns);
1471 file += "' COLSEP='1' ROWSEP='1'>";
1472 addNewlineAndDepth(file,++depth);
1474 for (i = 0; i < columns; ++i) {
1475 file += "<COLSPEC ALIGN='";
1476 file += getDocBookAlign(i, true);
1477 file += "' COLNAME='col";
1479 file += "' COLNUM='";
1481 file += "' COLSEP='";
1482 if (i == (columns-1)) {
1485 if (column_info[i].right_line ||
1486 column_info[i+1].left_line)
1492 addNewlineAndDepth(file, depth);
1494 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1495 if (column_info[i].left_line)
1500 addNewlineAndDepth(file,++depth);
1502 addNewlineAndDepth(file,++depth);
1503 file += "<ENTRY ALIGN='";
1504 file += getDocBookAlign(0);
1506 if (IsMultiColumn(0)) {
1507 file += " NAMEST='col1' NAMEEND='col";
1508 file += tostr(cells_in_multicolumn(0));
1512 addNewlineAndDepth(file,++depth);
1515 if (IsLastCell(cell)) {
1516 addNewlineAndDepth(file,--depth);
1518 addNewlineAndDepth(file,--depth);
1520 addNewlineAndDepth(file, depth);
1522 addNewlineAndDepth(file,++depth);
1523 file += "<ENTRY ALIGN='";
1524 file += getDocBookAlign(cell+1);
1525 file += "' VALIGN='middle'";
1526 if (IsMultiColumn(cell+1)) {
1527 file += " NAMEST='col";
1528 file += tostr(column_of_cell(cell+1) + 1);
1529 file += "' NAMEEND='col";
1530 file += tostr(column_of_cell(cell+1) +
1531 cells_in_multicolumn(cell+1));
1535 addNewlineAndDepth(file,++depth);
1538 addNewlineAndDepth(file,--depth);
1540 addNewlineAndDepth(file, depth);
1541 file += "<ENTRY ALIGN='";
1542 file += getDocBookAlign(cell+1);
1543 file += "' VALIGN='middle'";
1544 if (IsMultiColumn(cell+1)) {
1545 file += " NAMEST='col";
1546 file += tostr(column_of_cell(cell+1) + 1);
1547 file += "' NAMEEND='col";
1548 file += tostr(column_of_cell(cell+1) +
1549 cells_in_multicolumn(cell+1));
1553 addNewlineAndDepth(file,++depth);
1562 bool LyXTable::IsMultiColumn(int cell)
1564 int fvcell = FirstVirtualCell(cell);
1566 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1570 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1572 int row = row_of_cell(cell);
1573 int column = column_of_cell(cell);
1574 return &cell_info[row][column];
1578 void LyXTable::SetMultiColumn(int cell, int number)
1580 int fvcell = FirstVirtualCell(cell);
1581 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1583 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1584 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1585 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1586 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1587 for (number--;number>0;number--){
1588 cellinfo_of_cell(fvcell+number)->multicolumn =
1589 LyXTable::CELL_PART_OF_MULTICOLUMN;
1590 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1592 set_row_column_number_info();
1593 SetWidthOfCell(fvcell, new_width);
1597 int LyXTable::cells_in_multicolumn(int cell)
1599 int row = row_of_cell(cell);
1600 int column = column_of_cell(cell);
1603 while (column < columns && cell_info[row][column].multicolumn
1604 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1612 int LyXTable::UnsetMultiColumn(int cell)
1614 int fvcell = FirstVirtualCell(cell);
1615 int row = row_of_cell(fvcell);
1616 int column = column_of_cell(fvcell);
1620 if (cell_info[row][column].multicolumn
1621 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1622 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1624 while (column < columns &&
1625 cell_info[row][column].multicolumn
1626 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1627 cell_info[row][column].multicolumn =
1628 LyXTable::CELL_NORMAL;
1633 set_row_column_number_info();
1638 void LyXTable::delete_column(int column)
1641 columnstruct *column_info2 = new columnstruct[columns-1];
1643 for (i = 0; i<column; i++){
1644 column_info2[i] = column_info[i];
1646 for (i = column; i<columns-1; i++){
1647 column_info2[i] = column_info[i+1];
1650 delete[] column_info;
1651 column_info = column_info2;
1653 for (i = 0; i<rows;i++){
1654 cellstruct* tmp = cell_info[i];
1655 cell_info[i] = new cellstruct[columns-1];
1656 for (j = 0; j<column; j++){
1657 cell_info[i][j] = tmp[j];
1659 for (j = column; j<columns-1; j++){
1660 cell_info[i][j] = tmp[j+1];
1669 void LyXTable::SetLongTable(int what)
1671 is_long_table = what;
1674 bool LyXTable::IsLongTable()
1676 return is_long_table;
1679 void LyXTable::SetRotateTable(int what)
1684 bool LyXTable::RotateTable()
1689 void LyXTable::SetRotateCell(int cell, int what)
1691 cellinfo_of_cell(cell)->rotate = what;
1694 bool LyXTable::RotateCell(int cell)
1696 return cellinfo_of_cell(cell)->rotate;
1699 bool LyXTable::NeedRotating()
1703 for (int i = 0; i<rows;i++){
1704 for (int j = 0;j<columns;j++){
1705 if (cell_info[i][j].rotate)
1712 void LyXTable::AppendContRow(int cell)
1714 int row = row_of_cell(cell)+1;
1716 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1718 row_info[row].is_cont_row = true;
1719 row_info[row].top_line = false;
1720 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1724 bool LyXTable::IsContRow(int cell)
1726 return row_info[row_of_cell(cell)].is_cont_row;
1729 int LyXTable::CellHasContRow(int cell)
1731 int row = row_of_cell(cell);
1733 if (VeryLastRow(cell))
1735 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1736 return cell_info[row+1][column_of_cell(cell)].cellno;
1740 bool LyXTable::RowHasContRow(int cell)
1742 int row = row_of_cell(cell) + 1;
1745 return row_info[row].is_cont_row;
1749 int LyXTable::FirstVirtualCell(int cell)
1751 if (!IsContRow(cell))
1753 int row = row_of_cell(cell);
1754 int column = column_of_cell(cell);
1755 for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1757 return cell_info[row][column].cellno;
1761 int LyXTable::NextVirtualCell(int cell)
1763 if (!IsContRow(cell))
1765 int row = row_of_cell(cell);
1766 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1768 // what if(row >= rows) ?
1769 return cell_info[row][0].cellno;
1773 bool LyXTable::ShouldBeVeryLastCell(int cell)
1774 // "very last cell" ..of what? the row? the table?
1775 // "Cell" in this context appears to not count `virtual' cells
1777 int fcell = cell + 1;
1779 if (cell == GetNumberOfCells() - 1)
1780 return true; // not really sure if I should return false here
1781 if (!IsContRow(fcell))
1783 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1785 if (fcell < GetNumberOfCells() - 1)
1790 bool LyXTable::ShouldBeVeryLastRow(int cell)
1792 if (CellHasContRow(cell)>= 0)
1794 int row = row_of_cell(cell)+1;
1795 int column = column_of_cell(cell);
1796 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1799 return false; // found another valid row
1800 // I do not have any valid row after the actual
1804 int LyXTable::GetCellAbove(int cell)
1806 int row = row_of_cell(cell);
1809 return cell_info[row-1][column_of_cell(cell)].cellno;
1813 int LyXTable::GetCellNumber(int column, int row)
1815 if (column >= columns)
1816 column = columns - 1;
1817 else if (column < 0)
1824 return cell_info[row][column].cellno;
1827 void LyXTable::SetLinebreaks(int cell, bool what)
1829 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1832 bool LyXTable::Linebreaks(int cell)
1834 int fvcell = FirstVirtualCell(cell);
1836 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1837 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1839 return cellinfo_of_cell(fvcell)->linebreaks;
1842 void LyXTable::SetLTHead(int cell, bool first)
1844 int row = row_of_cell(cell);
1847 if (row == endfirsthead)
1859 bool LyXTable::RowOfLTHead(int cell)
1861 if ((endhead+1) > rows)
1863 return (row_of_cell(cell) == endhead);
1866 bool LyXTable::RowOfLTFirstHead(int cell)
1868 if ((endfirsthead+1) > rows)
1870 return (row_of_cell(cell) == endfirsthead);
1873 void LyXTable::SetLTFoot(int cell, bool last)
1875 int row = row_of_cell(cell);
1878 if (row == endlastfoot)
1890 bool LyXTable::RowOfLTFoot(int cell)
1892 if ((endfoot+1) > rows) {
1896 return (row_of_cell(cell) == endfoot);
1899 bool LyXTable::RowOfLTLastFoot(int cell)
1901 if ((endlastfoot+1) > rows)
1903 return (row_of_cell(cell) == endlastfoot);
1906 void LyXTable::SetLTNewPage(int cell, bool what)
1908 row_info[row_of_cell(cell)].newpage = what;
1911 bool LyXTable::LTNewPage(int cell)
1913 return row_info[row_of_cell(cell)].newpage;