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(FILE* file)
815 fprintf(file, "multicol5\n");
816 fprintf(file, "%d %d %d %d %d %d %d %d\n", rows, columns, is_long_table,
817 rotate, endhead, endfirsthead, endfoot, endlastfoot);
818 for (i = 0; i<rows; i++){
819 fprintf(file, "%d %d %d %d\n", row_info[i].top_line,
820 row_info[i].bottom_line, row_info[i].is_cont_row,
821 row_info[i].newpage);
823 for (i = 0; i<columns; i++){
824 fprintf(file, "%d %d %d \"%s\" \"%s\"\n",
825 column_info[i].alignment, column_info[i].left_line,
826 column_info[i].right_line,
827 VSpace(column_info[i].p_width).asLyXCommand().c_str(),
828 column_info[i].align_special.c_str());
831 for (i = 0; i<rows;i++){
832 for (j = 0;j<columns;j++){
833 fprintf(file, "%d %d %d %d %d %d %d \"%s\" \"%s\"\n",
834 cell_info[i][j].multicolumn,
835 cell_info[i][j].alignment,
836 cell_info[i][j].top_line,
837 cell_info[i][j].bottom_line,
838 cell_info[i][j].has_cont_row,
839 cell_info[i][j].rotate,
840 cell_info[i][j].linebreaks,
841 cell_info[i][j].align_special.c_str(),
842 cell_info[i][j].p_width.c_str());
847 void LyXTable::Read(FILE* file)
853 int is_long_table_arg = false;
854 int rotate_arg = false;
864 char vtmp[100], stmp[100], atmp[100];
866 fscanf(file, "%s\n", vtmp);
869 version = atoi(s.c_str()+8);
873 #warning Insert a error message window here that this format is not supported anymore
876 fprintf(stderr, "Tabular format < 5 is not supported anymore\n"
877 "Get an older version of LyX (< 1.1.x) for conversion!\n");
881 fgets(vtmp, sizeof(vtmp), file);
882 sscanf(vtmp, "%d %d %d %d %d %d %d %d\n", &rows_arg, &columns_arg,
883 &is_long_table_arg, &rotate_arg, &a, &b, &c, &d);
884 Init(rows_arg, columns_arg);
885 SetLongTable(is_long_table_arg);
886 SetRotateTable(rotate_arg);
891 for (i = 0; i<rows; i++){
892 a = b = c = d = e = f = g = h = 0;
893 fgets(vtmp, sizeof(vtmp), file);
894 sscanf(vtmp, "%d %d %d %d\n",
896 row_info[i].top_line = a;
897 row_info[i].bottom_line = b;
898 row_info[i].is_cont_row = c;
899 row_info[i].newpage = d;
901 for (i = 0; i<columns; i++){
904 fgets(vtmp, sizeof(vtmp), file);
905 sscanf(vtmp, "%d %d %d %s %s", &a, &b, &c, stmp, atmp);
906 column_info[i].alignment = (char) a;
907 column_info[i].left_line = b;
908 column_info[i].right_line = c;
909 if (*stmp == '"') { /* strip quotes if they exists */
912 // there are quotes so I have to reread the string correctly
913 // this is only because the old format did not have "
914 // this means also that atmp is ONLY set here!!!
916 sscanf(vtmp, "%*d %*d %*d %*s \"%[^\"]\"", atmp);
917 else // otherwise after the first empty "" read is aborded
918 sscanf(vtmp, "%*d %*d %*d \"%[^\"]\" \"%[^\"]\"", stmp, atmp);
919 column_info[i].p_width = stmp;
920 column_info[i].align_special = atmp;
922 column_info[i].p_width = stmp;
924 for (i = 0; i<rows;i++){
925 for (j = 0;j<columns;j++){
928 a = b = c = d = e = f = g = 0;
929 fgets(vtmp, sizeof(vtmp), file);
930 sscanf(vtmp, "%d %d %d %d %d %d %d %s %s\n",
931 &a, &b, &c, &d, &e, &f, &g, stmp, atmp);
932 cell_info[i][j].multicolumn = (char) a;
933 cell_info[i][j].alignment = (char) b;
934 cell_info[i][j].top_line = (char) c;
935 cell_info[i][j].bottom_line = (char) d;
936 cell_info[i][j].has_cont_row = (bool) e;
937 cell_info[i][j].rotate = (bool) f;
938 cell_info[i][j].linebreaks = (bool) g;
939 // this is only to see if I have an empty string first
940 // this clause should be always TRUE!!!
945 sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d %*s \"%[^\"]\"",
947 else // otherwise after the first empty "" read is aborded
948 sscanf(vtmp, "%*d %*d %*d %*d %*d %*d %*d \"%[^\"]\" \"%[^\"]\"",
950 cell_info[i][j].align_special = stmp;
951 cell_info[i][j].p_width = atmp;
953 cell_info[i][j].align_special = stmp;
956 set_row_column_number_info();
960 // cell <0 will tex the preamble
961 // returns the number of printed newlines
962 int LyXTable::TexEndOfCell(string & file, int cell)
968 if (ShouldBeVeryLastCell(cell)) {
969 // the very end at the very beginning
970 if (Linebreaks(cell))
971 file += "\\smallskip{}}";
972 if (IsMultiColumn(cell))
974 if (RotateCell(cell)) {
975 file += "\n\\end{sideways}";
983 while (!IsFirstCell(fcell))fcell--;
984 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
985 if (BottomLine(fcell+i))
988 if (tmp == NumberOfCellsInRow(fcell)){
992 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
993 if (BottomLine(fcell+i)){
995 file += tostr(column_of_cell(fcell+i)+1);
997 file += tostr(right_column_of_cell(fcell+i)+1);
1008 file += "\\end{longtable}";
1010 file += "\\end{tabular}";
1012 file += "\n\\end{sideways}";
1016 nvcell = NextVirtualCell(cell+1);
1020 file += "\\begin{sideways}\n";
1024 file += "\\begin{longtable}{";
1026 file += "\\begin{tabular}{";
1027 for (i = 0; i<columns;i++){
1028 if (column_info[i].left_line)
1030 if (!column_info[i].align_special.empty()) {
1031 file += column_info[i].align_special.c_str();
1032 } else if (!column_info[i].p_width.empty()) {
1034 file += column_info[i].p_width;
1037 switch (column_info[i].alignment) {
1038 case LYX_ALIGN_LEFT:
1041 case LYX_ALIGN_RIGHT:
1049 if (column_info[i].right_line)
1055 if (GetNumberOfCells()){
1057 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1058 if (TopLine(fcell+i))
1061 if (tmp == NumberOfCellsInRow(fcell)){
1065 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1066 if (TopLine(fcell+i)){
1068 file += tostr(column_of_cell(fcell+i)+1);
1070 file += tostr(right_column_of_cell(fcell+i)+1);
1081 if (RotateCell(0)) {
1082 file += "\\begin{sideways}\n";
1087 if (Linebreaks(cell))
1088 file += "\\smallskip{}}";
1089 if (IsMultiColumn(cell)){
1092 if (RotateCell(cell)) {
1093 file += "\n\\end{sideways}";
1096 if (IsLastCell(cell)) {
1097 int row = row_of_cell(cell);
1098 string hline1, hline2;
1099 bool print_hline = true;
1100 bool pr_top_hline, flag1, flag2;
1101 flag1 = IsLongTable() &&
1102 ((row == endhead) || (row == endfirsthead) ||
1103 (row == endfoot) || (row == endlastfoot));
1105 flag2 = IsLongTable() &&
1106 ((row <= endhead) || (row <= endfirsthead) ||
1107 (row <= endfoot) || (row <= endlastfoot));
1109 // print the bottom hline only if (otherwise it is doubled):
1110 // - is no LongTable
1111 // - there IS a first-header
1112 // - the next row is no special header/footer
1113 // & this row is no special header/footer
1114 // - the next row is a special header/footer
1115 // & this row is a special header/footer
1116 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1117 (endfirsthead == endhead);
1122 while (!IsFirstCell(fcell))
1124 for (i = 0; i < NumberOfCellsInRow(cell); i++){
1125 if (BottomLine(fcell+i))
1128 if (tmp == NumberOfCellsInRow(cell)){
1130 hline1 = "\\hline ";
1133 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1134 if (BottomLine(fcell+i)){
1136 file += tostr(column_of_cell(fcell+i)+1);
1138 file += tostr(right_column_of_cell(fcell+i)+1);
1140 hline1 += "\\cline{";
1141 hline1 += tostr(column_of_cell(fcell+i)+1);
1143 hline1 += tostr(right_column_of_cell(fcell+i)+1);
1153 if (IsLongTable() && (row == endfoot)) {
1154 file += "\\endfoot\n";
1156 print_hline = false; // no double line below footer
1158 if (IsLongTable() && (row == endlastfoot)) {
1159 file += "\\endlastfoot\n";
1161 print_hline = false; // no double line below footer
1163 if (IsLongTable() && row_info[row].newpage) {
1164 file += "\\newpage\n";
1166 print_hline = false; // no line below a \\newpage-command
1169 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1170 !ShouldBeVeryLastCell(cell)) {
1172 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1173 if (TopLine(fcell+i))
1176 if (tmp == NumberOfCellsInRow(fcell)){
1179 hline2 = "\\hline ";
1183 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1184 if (TopLine(fcell+i)){
1187 file += tostr(column_of_cell(fcell+i)+1);
1189 file += tostr(right_column_of_cell(fcell+i)+1);
1192 hline2 += "\\cline{";
1193 hline2 += tostr(column_of_cell(fcell+i)+1);
1195 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1201 if (tmp && print_hline){
1206 // the order here is important as if one defines two
1207 // or more things in one line only the first entry is
1208 // displayed the other are set to an empty-row. This
1209 // is important if I have a footer and want that the
1210 // lastfooter is NOT displayed!!!
1211 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1212 (row == endfoot) || (row == endlastfoot);
1214 // sflag2 = IsLongTable() && (row >= 0) &&
1215 // (sflag2 || (row == endhead) || (row == endfirsthead));
1217 bool sflag1 = IsLongTable() && (row != endhead) &&
1218 (row != endfirsthead) &&
1219 ((row == endfoot) || (row == endlastfoot));
1221 if (IsLongTable() && (row == endhead)) {
1222 file += "\\endhead\n";
1225 if (IsLongTable() && (row == endfirsthead)) {
1226 file += "\\endfirsthead\n";
1229 if (sflag1) { // add the \hline for next foot row
1230 if (!hline1.empty()) {
1231 file += hline1 + '\n';
1235 // add the \hline for the first row
1236 if (pr_top_hline && sflag2) {
1237 if (!hline2.empty()) {
1238 file += hline2 + '\n';
1242 if (nvcell < numberofcells && RotateCell(nvcell)) {
1243 file += "\\begin{sideways}\n";
1249 if (nvcell < numberofcells && RotateCell(nvcell)) {
1250 file += "\\begin{sideways}\n";
1255 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1256 file += "\\multicolumn{";
1257 file += tostr(cells_in_multicolumn(nvcell));
1259 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1260 file += cellinfo_of_cell(cell+1)->align_special;
1263 if (LeftLine(nvcell))
1265 if (!GetPWidth(nvcell).empty()) {
1267 file += GetPWidth(nvcell);
1270 switch (GetAlignment(nvcell)) {
1271 case LYX_ALIGN_LEFT: file += 'l'; break;
1272 case LYX_ALIGN_RIGHT: file += 'r'; break;
1273 default: file += 'c'; break;
1276 if (RightLine(nvcell))
1278 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1279 if (((nvcell+1) < numberofcells) &&
1280 (NextVirtualCell(nvcell+1) < numberofcells) &&
1281 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1282 LeftLine(NextVirtualCell(nvcell+1)))
1287 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1288 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1289 file += "\\parbox{";
1290 file += GetPWidth(nvcell);
1291 file += "}{\\smallskip{}";
1298 // cell <0 will tex the preamble
1299 // returns the number of printed newlines
1300 int LyXTable::RoffEndOfCell(FILE* file, int cell)
1305 if (cell == GetNumberOfCells() - 1){
1306 // the very end at the very beginning
1307 if (CellHasContRow(cell) >= 0) {
1308 fprintf(file, "\nT}");
1311 fprintf(file, "\n");
1313 if (row_info[row_of_cell(cell)].bottom_line) {
1314 fprintf(file, "_\n");
1317 fprintf(file, ".TE\n.pl 1c");
1322 fprintf(file, "\n.pl 500c\n.TS\n");
1323 for (j = 0; j<rows; j++) {
1324 for (i = 0; i<columns;i++, fcell++) {
1325 if (column_info[i].left_line)
1326 fprintf(file, " | ");
1327 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1330 switch (column_info[i].alignment) {
1331 case LYX_ALIGN_LEFT:
1334 case LYX_ALIGN_RIGHT:
1342 if (!column_info[i].p_width.empty())
1343 fprintf(file, "w(%s)", column_info[i].p_width.c_str());
1344 if (column_info[i].right_line)
1345 fprintf(file, " | ");
1348 fprintf(file, "\n");
1352 fprintf(file, ".\n");
1354 if (row_info[0].top_line) {
1355 fprintf(file, "_\n");
1358 if (CellHasContRow(0) >= 0) {
1359 fprintf(file, "T{\n");
1364 if (CellHasContRow(cell) >= 0) {
1365 fprintf(file, "\nT}");
1368 if (right_column_of_cell(cell) == columns -1){
1369 fprintf(file, "\n");
1371 int row = row_of_cell(cell);
1372 if (row_info[row++].bottom_line) {
1373 fprintf(file, "_\n");
1376 if ((row < rows) && row_info[row].top_line) {
1377 fprintf(file, "_\n");
1381 fprintf(file, "\t");
1382 if ((cell < GetNumberOfCells() - 1) &&
1383 (CellHasContRow(cell+1) >= 0)) {
1384 fprintf(file, "T{\n");
1392 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1398 i = column_of_cell(cell);
1399 if (!isColumn && IsMultiColumn(cell)) {
1400 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1401 return cellinfo_of_cell(cell)->align_special.c_str();
1403 switch (GetAlignment(cell)) {
1404 case LYX_ALIGN_LEFT:
1406 case LYX_ALIGN_RIGHT:
1413 if (!column_info[i].align_special.empty()) {
1414 return column_info[i].align_special.c_str();
1416 #ifdef IGNORE_THIS_FOR_NOW
1417 else if (!column_info[i].p_width.empty()) {
1419 file += column_info[i].p_width;
1424 switch (column_info[i].alignment) {
1425 case LYX_ALIGN_LEFT:
1427 case LYX_ALIGN_RIGHT:
1436 // cell <0 will tex the preamble
1437 // returns the number of printed newlines
1438 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1442 //int tmp; // tmp2; //Â unused
1443 int nvcell; // fcell; //Â unused
1444 if (ShouldBeVeryLastCell(cell)) {
1445 addNewlineAndDepth(file,--depth);
1447 addNewlineAndDepth(file,--depth);
1449 addNewlineAndDepth(file,--depth);
1451 addNewlineAndDepth(file,--depth);
1453 file += "</TGROUP>";
1455 file += "</TGROUP>";
1456 addNewlineAndDepth(file,--depth);
1459 nvcell = NextVirtualCell(cell+1);
1467 file += tostr(columns);
1468 file += "' COLSEP='1' ROWSEP='1'>";
1469 addNewlineAndDepth(file,++depth);
1471 for (i = 0; i < columns; ++i) {
1472 file += "<COLSPEC ALIGN='";
1473 file += getDocBookAlign(i, true);
1474 file += "' COLNAME='col";
1476 file += "' COLNUM='";
1478 file += "' COLSEP= '";
1479 if (i == (columns-1)) {
1482 if (column_info[i].right_line ||
1483 column_info[i+1].left_line)
1489 addNewlineAndDepth(file, depth);
1491 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1492 if (column_info[i].left_line)
1497 addNewlineAndDepth(file,++depth);
1499 addNewlineAndDepth(file,++depth);
1500 file += "<ENTRY ALIGN= '";
1501 file += getDocBookAlign(0);
1503 if (IsMultiColumn(0)) {
1504 file += " NAMEST= 'col1' NAMEEND= 'col";
1505 file += tostr(cells_in_multicolumn(0));
1509 addNewlineAndDepth(file,++depth);
1512 if (IsLastCell(cell)) {
1513 addNewlineAndDepth(file,--depth);
1515 addNewlineAndDepth(file,--depth);
1517 addNewlineAndDepth(file, depth);
1519 addNewlineAndDepth(file,++depth);
1520 file += "<ENTRY ALIGN= '";
1521 file += getDocBookAlign(cell+1);
1522 file += "' VALIGN= 'middle'";
1523 if (IsMultiColumn(cell+1)) {
1524 file += " NAMEST= 'col";
1525 file += tostr(column_of_cell(cell+1) + 1);
1526 file += "' NAMEEND= 'col";
1527 file += tostr(column_of_cell(cell+1) +
1528 cells_in_multicolumn(cell+1));
1532 addNewlineAndDepth(file,++depth);
1535 addNewlineAndDepth(file,--depth);
1537 addNewlineAndDepth(file, depth);
1538 file += "<ENTRY ALIGN= '";
1539 file += getDocBookAlign(cell+1);
1540 file += "' VALIGN= 'middle'";
1541 if (IsMultiColumn(cell+1)) {
1542 file += " NAMEST= 'col";
1543 file += tostr(column_of_cell(cell+1) + 1);
1544 file += "' NAMEEND= 'col";
1545 file += tostr(column_of_cell(cell+1) +
1546 cells_in_multicolumn(cell+1));
1550 addNewlineAndDepth(file,++depth);
1559 bool LyXTable::IsMultiColumn(int cell)
1561 int fvcell = FirstVirtualCell(cell);
1563 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1567 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1569 int row = row_of_cell(cell);
1570 int column = column_of_cell(cell);
1571 return &cell_info[row][column];
1575 void LyXTable::SetMultiColumn(int cell, int number)
1577 int fvcell = FirstVirtualCell(cell);
1578 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1580 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1581 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1582 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1583 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1584 for (number--;number>0;number--){
1585 cellinfo_of_cell(fvcell+number)->multicolumn =
1586 LyXTable::CELL_PART_OF_MULTICOLUMN;
1587 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1589 set_row_column_number_info();
1590 SetWidthOfCell(fvcell, new_width);
1594 int LyXTable::cells_in_multicolumn(int cell)
1596 int row = row_of_cell(cell);
1597 int column = column_of_cell(cell);
1600 while (column < columns && cell_info[row][column].multicolumn
1601 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1609 int LyXTable::UnsetMultiColumn(int cell)
1611 int fvcell = FirstVirtualCell(cell);
1612 int row = row_of_cell(fvcell);
1613 int column = column_of_cell(fvcell);
1617 if (cell_info[row][column].multicolumn
1618 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1619 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1621 while (column < columns &&
1622 cell_info[row][column].multicolumn
1623 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1624 cell_info[row][column].multicolumn =
1625 LyXTable::CELL_NORMAL;
1630 set_row_column_number_info();
1635 void LyXTable::delete_column(int column)
1638 columnstruct *column_info2 = new columnstruct[columns-1];
1640 for (i = 0; i<column; i++){
1641 column_info2[i] = column_info[i];
1643 for (i = column; i<columns-1; i++){
1644 column_info2[i] = column_info[i+1];
1647 delete[] column_info;
1648 column_info = column_info2;
1650 for (i = 0; i<rows;i++){
1651 cellstruct* tmp = cell_info[i];
1652 cell_info[i] = new cellstruct[columns-1];
1653 for (j = 0; j<column; j++){
1654 cell_info[i][j] = tmp[j];
1656 for (j = column; j<columns-1; j++){
1657 cell_info[i][j] = tmp[j+1];
1666 void LyXTable::SetLongTable(int what)
1668 is_long_table = what;
1671 bool LyXTable::IsLongTable()
1673 return is_long_table;
1676 void LyXTable::SetRotateTable(int what)
1681 bool LyXTable::RotateTable()
1686 void LyXTable::SetRotateCell(int cell, int what)
1688 cellinfo_of_cell(cell)->rotate = what;
1691 bool LyXTable::RotateCell(int cell)
1693 return cellinfo_of_cell(cell)->rotate;
1696 bool LyXTable::NeedRotating()
1700 for (int i = 0; i<rows;i++){
1701 for (int j = 0;j<columns;j++){
1702 if (cell_info[i][j].rotate)
1709 void LyXTable::AppendContRow(int cell)
1711 int row = row_of_cell(cell)+1;
1713 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1715 row_info[row].is_cont_row = true;
1716 row_info[row].top_line = false;
1717 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1721 bool LyXTable::IsContRow(int cell)
1723 return row_info[row_of_cell(cell)].is_cont_row;
1726 int LyXTable::CellHasContRow(int cell)
1728 int row = row_of_cell(cell);
1730 if (VeryLastRow(cell))
1732 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1733 return cell_info[row+1][column_of_cell(cell)].cellno;
1737 bool LyXTable::RowHasContRow(int cell)
1739 int row = row_of_cell(cell) + 1;
1742 return row_info[row].is_cont_row;
1746 int LyXTable::FirstVirtualCell(int cell)
1748 if (!IsContRow(cell))
1750 int row = row_of_cell(cell);
1751 int column = column_of_cell(cell);
1752 for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1754 return cell_info[row][column].cellno;
1758 int LyXTable::NextVirtualCell(int cell)
1760 if (!IsContRow(cell))
1762 int row = row_of_cell(cell);
1763 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1765 // what if(row >= rows) ?
1766 return cell_info[row][0].cellno;
1770 bool LyXTable::ShouldBeVeryLastCell(int cell)
1771 // "very last cell" ..of what? the row? the table?
1772 // "Cell" in this context appears to not count `virtual' cells
1774 int fcell = cell + 1;
1776 if (cell == GetNumberOfCells() - 1)
1777 return true; // not really sure if I should return false here
1778 if (!IsContRow(fcell))
1780 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1782 if (fcell < GetNumberOfCells() - 1)
1787 bool LyXTable::ShouldBeVeryLastRow(int cell)
1789 if (CellHasContRow(cell)>= 0)
1791 int row = row_of_cell(cell)+1;
1792 int column = column_of_cell(cell);
1793 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1796 return false; // found another valid row
1797 // I do not have any valid row after the actual
1801 int LyXTable::GetCellAbove(int cell)
1803 int row = row_of_cell(cell);
1806 return cell_info[row-1][column_of_cell(cell)].cellno;
1810 int LyXTable::GetCellNumber(int column, int row)
1812 if (column >= columns)
1813 column = columns - 1;
1814 else if (column < 0)
1821 return cell_info[row][column].cellno;
1824 void LyXTable::SetLinebreaks(int cell, bool what)
1826 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1829 bool LyXTable::Linebreaks(int cell)
1831 int fvcell = FirstVirtualCell(cell);
1833 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1834 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1836 return cellinfo_of_cell(fvcell)->linebreaks;
1839 void LyXTable::SetLTHead(int cell, bool first)
1841 int row = row_of_cell(cell);
1844 if (row == endfirsthead)
1856 bool LyXTable::RowOfLTHead(int cell)
1858 if ((endhead+1) > rows)
1860 return (row_of_cell(cell) == endhead);
1863 bool LyXTable::RowOfLTFirstHead(int cell)
1865 if ((endfirsthead+1) > rows)
1867 return (row_of_cell(cell) == endfirsthead);
1870 void LyXTable::SetLTFoot(int cell, bool last)
1872 int row = row_of_cell(cell);
1875 if (row == endlastfoot)
1887 bool LyXTable::RowOfLTFoot(int cell)
1889 if ((endfoot+1) > rows) {
1893 return (row_of_cell(cell) == endfoot);
1896 bool LyXTable::RowOfLTLastFoot(int cell)
1898 if ((endlastfoot+1) > rows)
1900 return (row_of_cell(cell) == endlastfoot);
1903 void LyXTable::SetLTNewPage(int cell, bool what)
1905 row_info[row_of_cell(cell)].newpage = what;
1908 bool LyXTable::LTNewPage(int cell)
1910 return row_info[row_of_cell(cell)].newpage;