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()
776 for (i=0; i<columns;i++) {
777 width_of_table += column_info[i].width_of_column;
782 int LyXTable::row_of_cell(int cell)
784 if (cell >= numberofcells)
788 return rowofcell[cell];
792 int LyXTable::column_of_cell(int cell)
794 if (cell >= numberofcells)
798 return columnofcell[cell];
802 int LyXTable::right_column_of_cell(int cell)
804 int row = row_of_cell(cell);
805 int column = column_of_cell(cell);
806 while (column < columns - 1 &&
807 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
813 void LyXTable::Write(FILE* file)
816 fprintf(file, "multicol5\n");
817 fprintf(file, "%d %d %d %d %d %d %d %d\n", rows, columns, is_long_table,
818 rotate, endhead, endfirsthead, endfoot, endlastfoot);
819 for (i=0; i<rows; i++){
820 fprintf(file, "%d %d %d %d\n", row_info[i].top_line,
821 row_info[i].bottom_line, row_info[i].is_cont_row,
822 row_info[i].newpage);
824 for (i=0; i<columns; i++){
825 fprintf(file, "%d %d %d \"%s\" \"%s\"\n",
826 column_info[i].alignment, column_info[i].left_line,
827 column_info[i].right_line,
828 VSpace(column_info[i].p_width).asLyXCommand().c_str(),
829 column_info[i].align_special.c_str());
832 for (i=0; i<rows;i++){
833 for (j=0;j<columns;j++){
834 fprintf(file, "%d %d %d %d %d %d %d \"%s\" \"%s\"\n",
835 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.c_str(),
843 cell_info[i][j].p_width.c_str());
848 void LyXTable::Read(FILE* file)
854 int is_long_table_arg = false;
855 int rotate_arg = false;
865 char vtmp[100], stmp[100], atmp[100];
867 fscanf(file, "%s\n", vtmp);
870 version = atoi(s.c_str()+8);
874 #warning Insert a error message window here that this format is not supported anymore
877 fprintf(stderr,"Tabular format < 5 is not supported anymore\n"
878 "Get an older version of LyX (< 1.1.x) for conversion!\n");
882 fgets(vtmp,sizeof(vtmp),file);
883 sscanf(vtmp, "%d %d %d %d %d %d %d %d\n", &rows_arg, &columns_arg,
884 &is_long_table_arg, &rotate_arg, &a, &b, &c, &d);
885 Init(rows_arg, columns_arg);
886 SetLongTable(is_long_table_arg);
887 SetRotateTable(rotate_arg);
892 for (i=0; i<rows; i++){
894 fgets(vtmp,sizeof(vtmp),file);
895 sscanf(vtmp, "%d %d %d %d\n",
897 row_info[i].top_line = a;
898 row_info[i].bottom_line = b;
899 row_info[i].is_cont_row = c;
900 row_info[i].newpage = d;
902 for (i=0; i<columns; i++){
905 fgets(vtmp,sizeof(vtmp),file);
906 sscanf(vtmp, "%d %d %d %s %s", &a, &b, &c, stmp, atmp);
907 column_info[i].alignment = (char) a;
908 column_info[i].left_line = b;
909 column_info[i].right_line = c;
910 if (*stmp == '"') { /* strip quotes if they exists */
913 // there are quotes so I have to reread the string correctly
914 // this is only because the old format did not have "
915 // this means also that atmp is ONLY set here!!!
917 sscanf(vtmp, "%*d %*d %*d %*s \"%[^\"]\"", atmp);
918 else // otherwise after the first empty "" read is aborded
919 sscanf(vtmp, "%*d %*d %*d \"%[^\"]\" \"%[^\"]\"", stmp, atmp);
920 column_info[i].p_width = stmp;
921 column_info[i].align_special = atmp;
923 column_info[i].p_width = stmp;
925 for (i=0; i<rows;i++){
926 for (j=0;j<columns;j++){
930 fgets(vtmp,sizeof(vtmp),file);
931 sscanf(vtmp, "%d %d %d %d %d %d %d %s %s\n",
932 &a, &b, &c, &d, &e, &f, &g, stmp, atmp);
933 cell_info[i][j].multicolumn = (char) a;
934 cell_info[i][j].alignment = (char) b;
935 cell_info[i][j].top_line = (char) c;
936 cell_info[i][j].bottom_line = (char) d;
937 cell_info[i][j].has_cont_row = (bool) e;
938 cell_info[i][j].rotate = (bool) f;
939 cell_info[i][j].linebreaks = (bool) g;
940 // this is only to see if I have an empty string first
941 // this clause should be always TRUE!!!
946 sscanf(vtmp,"%*d %*d %*d %*d %*d %*d %*d %*s \"%[^\"]\"",
948 else // otherwise after the first empty "" read is aborded
949 sscanf(vtmp,"%*d %*d %*d %*d %*d %*d %*d \"%[^\"]\" \"%[^\"]\"",
951 cell_info[i][j].align_special = stmp;
952 cell_info[i][j].p_width = atmp;
954 cell_info[i][j].align_special = stmp;
957 set_row_column_number_info();
961 // cell <0 will tex the preamble
962 // returns the number of printed newlines
963 int LyXTable::TexEndOfCell(string& file, int cell)
969 if (ShouldBeVeryLastCell(cell)) {
970 // the very end at the very beginning
971 if (Linebreaks(cell))
972 file += "\\smallskip{}}";
973 if (IsMultiColumn(cell))
975 if (RotateCell(cell)) {
976 file += "\n\\end{sideways}";
984 while (!IsFirstCell(fcell))fcell--;
985 for (i=0; i < NumberOfCellsInRow(fcell); i++){
986 if (BottomLine(fcell+i))
989 if (tmp == NumberOfCellsInRow(fcell)){
993 for (i=0; i < NumberOfCellsInRow(fcell); i++){
994 if (BottomLine(fcell+i)){
996 file += tostr(column_of_cell(fcell+i)+1);
998 file += tostr(right_column_of_cell(fcell+i)+1);
1009 file += "\\end{longtable}";
1011 file += "\\end{tabular}";
1013 file += "\n\\end{sideways}";
1017 nvcell = NextVirtualCell(cell+1);
1021 file += "\\begin{sideways}\n";
1025 file += "\\begin{longtable}{";
1027 file += "\\begin{tabular}{";
1028 for (i=0; i<columns;i++){
1029 if (column_info[i].left_line)
1031 if (!column_info[i].align_special.empty()) {
1032 file += column_info[i].align_special.c_str();
1033 } else if (!column_info[i].p_width.empty()) {
1035 file += column_info[i].p_width;
1038 switch (column_info[i].alignment) {
1039 case LYX_ALIGN_LEFT:
1042 case LYX_ALIGN_RIGHT:
1050 if (column_info[i].right_line)
1056 if (GetNumberOfCells()){
1058 for (i=0; i < NumberOfCellsInRow(fcell); i++){
1059 if (TopLine(fcell+i))
1062 if (tmp == NumberOfCellsInRow(fcell)){
1066 for (i=0; i < NumberOfCellsInRow(fcell); i++){
1067 if (TopLine(fcell+i)){
1069 file += tostr(column_of_cell(fcell+i)+1);
1071 file += tostr(right_column_of_cell(fcell+i)+1);
1082 if (RotateCell(0)) {
1083 file += "\\begin{sideways}\n";
1088 if (Linebreaks(cell))
1089 file += "\\smallskip{}}";
1090 if (IsMultiColumn(cell)){
1093 if (RotateCell(cell)) {
1094 file += "\n\\end{sideways}";
1097 if (IsLastCell(cell)) {
1098 int row = row_of_cell(cell);
1099 string hline1,hline2;
1100 bool print_hline = true;
1101 bool pr_top_hline,flag1,flag2;
1102 flag1 = IsLongTable() &&
1103 ((row == endhead) || (row == endfirsthead) ||
1104 (row == endfoot) || (row == endlastfoot));
1106 flag2 = IsLongTable() &&
1107 ((row <= endhead) || (row <= endfirsthead) ||
1108 (row <= endfoot) || (row <= endlastfoot));
1110 // print the bottom hline only if (otherwise it is doubled):
1111 // - is no LongTable
1112 // - there IS a first-header
1113 // - the next row is no special header/footer
1114 // & this row is no special header/footer
1115 // - the next row is a special header/footer
1116 // & this row is a special header/footer
1117 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1118 (endfirsthead == endhead);
1123 while (!IsFirstCell(fcell))
1125 for (i=0; i < NumberOfCellsInRow(cell); i++){
1126 if (BottomLine(fcell+i))
1129 if (tmp == NumberOfCellsInRow(cell)){
1131 hline1 = "\\hline ";
1134 for (i=0; i < NumberOfCellsInRow(fcell); i++){
1135 if (BottomLine(fcell+i)){
1137 file += tostr(column_of_cell(fcell+i)+1);
1139 file += tostr(right_column_of_cell(fcell+i)+1);
1141 hline1 += "\\cline{";
1142 hline1 += tostr(column_of_cell(fcell+i)+1);
1144 hline1 += tostr(right_column_of_cell(fcell+i)+1);
1154 if (IsLongTable() && (row == endfoot)) {
1155 file += "\\endfoot\n";
1157 print_hline = false; // no double line below footer
1159 if (IsLongTable() && (row == endlastfoot)) {
1160 file += "\\endlastfoot\n";
1162 print_hline = false; // no double line below footer
1164 if (IsLongTable() && row_info[row].newpage) {
1165 file += "\\newpage\n";
1167 print_hline = false; // no line below a \\newpage-command
1170 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1171 !ShouldBeVeryLastCell(cell)) {
1173 for (i=0; i < NumberOfCellsInRow(fcell); i++){
1174 if (TopLine(fcell+i))
1177 if (tmp == NumberOfCellsInRow(fcell)){
1180 hline2 = "\\hline ";
1184 for (i=0; i < NumberOfCellsInRow(fcell); i++){
1185 if (TopLine(fcell+i)){
1188 file += tostr(column_of_cell(fcell+i)+1);
1190 file += tostr(right_column_of_cell(fcell+i)+1);
1193 hline2 += "\\cline{";
1194 hline2 += tostr(column_of_cell(fcell+i)+1);
1196 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1202 if (tmp && print_hline){
1207 // the order here is important as if one defines two
1208 // or more things in one line only the first entry is
1209 // displayed the other are set to an empty-row. This
1210 // is important if I have a footer and want that the
1211 // lastfooter is NOT displayed!!!
1212 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1213 (row == endfoot) || (row == endlastfoot);
1215 // sflag2 = IsLongTable() && (row >= 0) &&
1216 // (sflag2 || (row == endhead) || (row == endfirsthead));
1218 bool sflag1 = IsLongTable() && (row != endhead) &&
1219 (row != endfirsthead) &&
1220 ((row == endfoot) || (row == endlastfoot));
1222 if (IsLongTable() && (row == endhead)) {
1223 file += "\\endhead\n";
1226 if (IsLongTable() && (row == endfirsthead)) {
1227 file += "\\endfirsthead\n";
1230 if (sflag1) { // add the \hline for next foot row
1231 if (!hline1.empty()) {
1232 file += hline1 + '\n';
1236 // add the \hline for the first row
1237 if (pr_top_hline && sflag2) {
1238 if (!hline2.empty()) {
1239 file += hline2 + '\n';
1243 if (nvcell < numberofcells && RotateCell(nvcell)) {
1244 file += "\\begin{sideways}\n";
1250 if (nvcell < numberofcells && RotateCell(nvcell)) {
1251 file += "\\begin{sideways}\n";
1256 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1257 file += "\\multicolumn{";
1258 file += tostr(cells_in_multicolumn(nvcell));
1260 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1261 file += cellinfo_of_cell(cell+1)->align_special;
1264 if (LeftLine(nvcell))
1266 if (!GetPWidth(nvcell).empty()) {
1268 file += GetPWidth(nvcell);
1271 switch (GetAlignment(nvcell)) {
1272 case LYX_ALIGN_LEFT: file += 'l'; break;
1273 case LYX_ALIGN_RIGHT: file += 'r'; break;
1274 default: file += 'c'; break;
1277 if (RightLine(nvcell))
1279 //if (column_of_cell(cell+2)!=0 && LeftLine(cell+2))
1280 if (((nvcell+1) < numberofcells) &&
1281 (NextVirtualCell(nvcell+1) < numberofcells) &&
1282 (column_of_cell(NextVirtualCell(nvcell+1))!=0) &&
1283 LeftLine(NextVirtualCell(nvcell+1)))
1288 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1289 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1290 file += "\\parbox{";
1291 file += GetPWidth(nvcell);
1292 file += "}{\\smallskip{}";
1299 // cell <0 will tex the preamble
1300 // returns the number of printed newlines
1301 int LyXTable::RoffEndOfCell(FILE* file, int cell)
1306 if (cell == GetNumberOfCells() - 1){
1307 // the very end at the very beginning
1308 if (CellHasContRow(cell) >= 0) {
1309 fprintf(file,"\nT}");
1312 fprintf(file, "\n");
1314 if (row_info[row_of_cell(cell)].bottom_line) {
1315 fprintf(file, "_\n");
1318 fprintf(file, ".TE\n.pl 1c");
1323 fprintf(file, "\n.pl 500c\n.TS\n");
1324 for (j=0; j<rows; j++) {
1325 for (i=0; i<columns;i++,fcell++) {
1326 if (column_info[i].left_line)
1327 fprintf(file, " | ");
1328 if (cell_info[j][i].multicolumn==CELL_PART_OF_MULTICOLUMN)
1331 switch (column_info[i].alignment) {
1332 case LYX_ALIGN_LEFT:
1335 case LYX_ALIGN_RIGHT:
1343 if (!column_info[i].p_width.empty())
1344 fprintf(file, "w(%s)", column_info[i].p_width.c_str());
1345 if (column_info[i].right_line)
1346 fprintf(file, " | ");
1349 fprintf(file, "\n");
1353 fprintf(file, ".\n");
1355 if (row_info[0].top_line) {
1356 fprintf(file,"_\n");
1359 if (CellHasContRow(0) >= 0) {
1360 fprintf(file,"T{\n");
1365 if (CellHasContRow(cell) >= 0) {
1366 fprintf(file,"\nT}");
1369 if (right_column_of_cell(cell) == columns -1){
1370 fprintf(file, "\n");
1372 int row = row_of_cell(cell);
1373 if (row_info[row++].bottom_line) {
1374 fprintf(file, "_\n");
1377 if ((row < rows) && row_info[row].top_line) {
1378 fprintf(file, "_\n");
1382 fprintf(file, "\t");
1383 if ((cell < GetNumberOfCells() - 1) &&
1384 (CellHasContRow(cell+1) >= 0)) {
1385 fprintf(file,"T{\n");
1393 const char *LyXTable::getDocBookAlign(int cell, bool isColumn)
1399 i = column_of_cell(cell);
1400 if (!isColumn && IsMultiColumn(cell)) {
1401 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1402 return cellinfo_of_cell(cell)->align_special.c_str();
1404 switch (GetAlignment(cell)) {
1405 case LYX_ALIGN_LEFT:
1407 case LYX_ALIGN_RIGHT:
1414 if (!column_info[i].align_special.empty()) {
1415 return column_info[i].align_special.c_str();
1417 #ifdef IGNORE_THIS_FOR_NOW
1418 else if (!column_info[i].p_width.empty()) {
1420 file += column_info[i].p_width;
1425 switch (column_info[i].alignment) {
1426 case LYX_ALIGN_LEFT:
1428 case LYX_ALIGN_RIGHT:
1437 // cell <0 will tex the preamble
1438 // returns the number of printed newlines
1439 int LyXTable::DocBookEndOfCell(string& file, int cell, int &depth)
1443 //int tmp; // tmp2; //Â unused
1444 int nvcell; // fcell; //Â unused
1445 if (ShouldBeVeryLastCell(cell)) {
1446 addNewlineAndDepth(file,--depth);
1448 addNewlineAndDepth(file,--depth);
1450 addNewlineAndDepth(file,--depth);
1452 addNewlineAndDepth(file,--depth);
1454 file += "</TGROUP>";
1456 file += "</TGROUP>";
1457 addNewlineAndDepth(file,--depth);
1460 nvcell = NextVirtualCell(cell+1);
1468 file += tostr(columns);
1469 file += "' COLSEP='1' ROWSEP='1'>";
1470 addNewlineAndDepth(file,++depth);
1472 for (i=0; i<columns;i++) {
1473 file += "<COLSPEC ALIGN='";
1474 file += getDocBookAlign(i, true);
1475 file += "' COLNAME='col";
1477 file += "' COLNUM='";
1479 file += "' COLSEP='";
1480 if (i == (columns-1)) {
1483 if (column_info[i].right_line ||
1484 column_info[i+1].left_line)
1490 addNewlineAndDepth(file,depth);
1492 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1493 if (column_info[i].left_line)
1498 addNewlineAndDepth(file,++depth);
1500 addNewlineAndDepth(file,++depth);
1501 file += "<ENTRY ALIGN='";
1502 file += getDocBookAlign(0);
1504 if (IsMultiColumn(0)) {
1505 file += " NAMEST='col1' NAMEEND='col";
1506 file += tostr(cells_in_multicolumn(0));
1510 addNewlineAndDepth(file,++depth);
1513 if (IsLastCell(cell)) {
1514 addNewlineAndDepth(file,--depth);
1516 addNewlineAndDepth(file,--depth);
1518 addNewlineAndDepth(file,depth);
1520 addNewlineAndDepth(file,++depth);
1521 file += "<ENTRY ALIGN='";
1522 file += getDocBookAlign(cell+1);
1523 file += "' VALIGN='middle'";
1524 if (IsMultiColumn(cell+1)) {
1525 file += " NAMEST='col";
1526 file += tostr(column_of_cell(cell+1) + 1);
1527 file += "' NAMEEND='col";
1528 file += tostr(column_of_cell(cell+1) +
1529 cells_in_multicolumn(cell+1));
1533 addNewlineAndDepth(file,++depth);
1536 addNewlineAndDepth(file,--depth);
1538 addNewlineAndDepth(file,depth);
1539 file += "<ENTRY ALIGN='";
1540 file += getDocBookAlign(cell+1);
1541 file += "' VALIGN='middle'";
1542 if (IsMultiColumn(cell+1)) {
1543 file += " NAMEST='col";
1544 file += tostr(column_of_cell(cell+1) + 1);
1545 file += "' NAMEEND='col";
1546 file += tostr(column_of_cell(cell+1) +
1547 cells_in_multicolumn(cell+1));
1551 addNewlineAndDepth(file,++depth);
1560 bool LyXTable::IsMultiColumn(int cell)
1562 int fvcell = FirstVirtualCell(cell);
1564 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1568 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1570 int row = row_of_cell(cell);
1571 int column = column_of_cell(cell);
1572 return &cell_info[row][column];
1576 void LyXTable::SetMultiColumn(int cell, int number)
1578 int fvcell = FirstVirtualCell(cell);
1579 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1581 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1582 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1583 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1584 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1585 for (number--;number>0;number--){
1586 cellinfo_of_cell(fvcell+number)->multicolumn =
1587 LyXTable::CELL_PART_OF_MULTICOLUMN;
1588 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1590 set_row_column_number_info();
1591 SetWidthOfCell(fvcell,new_width);
1595 int LyXTable::cells_in_multicolumn(int cell)
1597 int row = row_of_cell(cell);
1598 int column = column_of_cell(cell);
1601 while (column < columns && cell_info[row][column].multicolumn
1602 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1610 int LyXTable::UnsetMultiColumn(int cell)
1612 int fvcell = FirstVirtualCell(cell);
1613 int row = row_of_cell(fvcell);
1614 int column = column_of_cell(fvcell);
1618 if (cell_info[row][column].multicolumn
1619 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1620 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1622 while (column < columns &&
1623 cell_info[row][column].multicolumn
1624 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1625 cell_info[row][column].multicolumn =
1626 LyXTable::CELL_NORMAL;
1631 set_row_column_number_info();
1636 void LyXTable::delete_column(int column)
1639 columnstruct *column_info2 = new columnstruct[columns-1];
1641 for (i=0; i<column; i++){
1642 column_info2[i] = column_info[i];
1644 for (i=column; i<columns-1; i++){
1645 column_info2[i] = column_info[i+1];
1648 delete[] column_info;
1649 column_info = column_info2;
1651 for (i=0; i<rows;i++){
1652 cellstruct* tmp = cell_info[i];
1653 cell_info[i] = new cellstruct[columns-1];
1654 for (j=0; j<column; j++){
1655 cell_info[i][j]=tmp[j];
1657 for (j=column; j<columns-1; j++){
1658 cell_info[i][j]=tmp[j+1];
1667 void LyXTable::SetLongTable(int what)
1669 is_long_table = what;
1672 bool LyXTable::IsLongTable()
1674 return is_long_table;
1677 void LyXTable::SetRotateTable(int what)
1682 bool LyXTable::RotateTable()
1687 void LyXTable::SetRotateCell(int cell, int what)
1689 cellinfo_of_cell(cell)->rotate = what;
1692 bool LyXTable::RotateCell(int cell)
1694 return cellinfo_of_cell(cell)->rotate;
1697 bool LyXTable::NeedRotating()
1701 for (int i=0; i<rows;i++){
1702 for (int j=0;j<columns;j++){
1703 if (cell_info[i][j].rotate)
1710 void LyXTable::AppendContRow(int cell)
1712 int row = row_of_cell(cell)+1;
1714 if (!RowHasContRow(cell) || (CellHasContRow(cell)>=0))
1716 row_info[row].is_cont_row = true;
1717 row_info[row].top_line = false;
1718 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1722 bool LyXTable::IsContRow(int cell)
1724 return row_info[row_of_cell(cell)].is_cont_row;
1727 int LyXTable::CellHasContRow(int cell)
1729 int row = row_of_cell(cell);
1731 if (VeryLastRow(cell))
1733 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1734 return cell_info[row+1][column_of_cell(cell)].cellno;
1738 bool LyXTable::RowHasContRow(int cell)
1740 int row = row_of_cell(cell) + 1;
1743 return row_info[row].is_cont_row;
1747 int LyXTable::FirstVirtualCell(int cell)
1749 if (!IsContRow(cell))
1751 int row = row_of_cell(cell);
1752 int column = column_of_cell(cell);
1753 for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1755 return cell_info[row][column].cellno;
1759 int LyXTable::NextVirtualCell(int cell)
1761 if (!IsContRow(cell))
1763 int row = row_of_cell(cell);
1764 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1766 // what if(row >= rows) ?
1767 return cell_info[row][0].cellno;
1771 bool LyXTable::ShouldBeVeryLastCell(int cell)
1772 // "very last cell" ..of what? the row? the table?
1773 // "Cell" in this context appears to not count `virtual' cells
1775 int fcell = cell + 1;
1777 if (cell == GetNumberOfCells() - 1)
1778 return true; // not really sure if I should return false here
1779 if (!IsContRow(fcell))
1781 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1783 if (fcell < GetNumberOfCells() - 1)
1788 bool LyXTable::ShouldBeVeryLastRow(int cell)
1790 if (CellHasContRow(cell)>=0)
1792 int row = row_of_cell(cell)+1;
1793 int column = column_of_cell(cell);
1794 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1797 return false; // found another valid row
1798 // I do not have any valid row after the actual
1802 int LyXTable::GetCellAbove(int cell)
1804 int row = row_of_cell(cell);
1807 return cell_info[row-1][column_of_cell(cell)].cellno;
1811 int LyXTable::GetCellNumber(int column, int row)
1813 if (column >= columns)
1814 column = columns - 1;
1815 else if (column < 0)
1822 return cell_info[row][column].cellno;
1825 void LyXTable::SetLinebreaks(int cell, bool what)
1827 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1830 bool LyXTable::Linebreaks(int cell)
1832 int fvcell = FirstVirtualCell(cell);
1834 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1835 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1837 return cellinfo_of_cell(fvcell)->linebreaks;
1840 void LyXTable::SetLTHead(int cell, bool first)
1842 int row = row_of_cell(cell);
1845 if (row == endfirsthead)
1857 bool LyXTable::RowOfLTHead(int cell)
1859 if ((endhead+1) > rows)
1861 return (row_of_cell(cell) == endhead);
1864 bool LyXTable::RowOfLTFirstHead(int cell)
1866 if ((endfirsthead+1) > rows)
1868 return (row_of_cell(cell) == endfirsthead);
1871 void LyXTable::SetLTFoot(int cell, bool last)
1873 int row = row_of_cell(cell);
1876 if (row == endlastfoot)
1888 bool LyXTable::RowOfLTFoot(int cell)
1890 if ((endfoot+1) > rows) {
1894 return (row_of_cell(cell) == endfoot);
1897 bool LyXTable::RowOfLTLastFoot(int cell)
1899 if ((endlastfoot+1) > rows)
1901 return (row_of_cell(cell) == endlastfoot);
1904 void LyXTable::SetLTNewPage(int cell, bool what)
1906 row_info[row_of_cell(cell)].newpage = what;
1909 bool LyXTable::LTNewPage(int cell)
1911 return row_info[row_of_cell(cell)].newpage;