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 static int const 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() {
44 delete[] columnofcell;
47 for (int i = 0; i < rows; ++i) {
48 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
54 LyXTable * LyXTable::Clone()
56 LyXTable * result = new LyXTable(rows, columns);
59 for (row = 0; row < rows; ++row) {
60 for (column = 0; column < columns; ++column) {
61 result->cell_info[row][column] = cell_info[row][column];
65 for (row = 0; row < rows; ++row) {
66 result->row_info[row] = row_info[row];
69 for (column = 0; column < columns; ++column) {
70 result->column_info[column].left_line = column_info[column].left_line;
71 result->column_info[column].right_line = column_info[column].right_line;
72 result->column_info[column].alignment = column_info[column].alignment;
73 result->column_info[column].p_width = column_info[column].p_width;
74 result->column_info[column].align_special = column_info[column].align_special;
77 result->SetLongTable(is_long_table);
78 result->rotate = rotate;
84 /* activates all lines and sets all widths to 0 */
85 void LyXTable::Init(int rows_arg, int columns_arg)
89 columns = columns_arg;
90 column_info = new columnstruct[columns];
91 row_info = new rowstruct[rows];
92 cell_info = new cellstruct*[rows];
95 for (i = 0; i < rows; ++i) {
96 cell_info[i] = new cellstruct[columns];
97 row_info[i].top_line = true;
98 row_info[i].bottom_line = false;
99 row_info[i].is_cont_row = false;
100 row_info[i].newpage = false;
101 for (j = 0; j < columns; ++j) {
102 cell_info[i][j].cellno = cellno++;
103 cell_info[i][j].width_of_cell = 0;
104 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
105 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
106 cell_info[i][j].top_line = row_info[i].top_line;
107 cell_info[i][j].bottom_line = row_info[i].bottom_line;
108 cell_info[i][j].has_cont_row = false;
109 cell_info[i][j].rotate = false;
110 cell_info[i][j].linebreaks = false;
113 row_info[i-1].bottom_line = true;
114 row_info[0].bottom_line = true;
116 for (i = 0; i < columns; ++i) {
117 column_info[i].left_line = true;
118 column_info[i].right_line = false;
119 column_info[i].alignment = LYX_ALIGN_CENTER;
120 // set width_of_column to zero before it is used in
121 // calculate_width_of_column() (thornley)
122 column_info[i].width_of_column = 0;
123 calculate_width_of_column(i);
125 column_info[i-1].right_line = true;
127 calculate_width_of_table();
131 set_row_column_number_info();
132 is_long_table = false;
141 void LyXTable::AppendRow(int cell)
143 int row = row_of_cell(cell);
144 rowstruct * row_info2 = new rowstruct[rows + 1];
145 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
148 for (i = 0; i <= row; ++i) {
149 cell_info2[i] = cell_info[i];
150 row_info2[i] = row_info[i];
152 for (i = rows - 1; i >= row; --i) {
153 cell_info2[i + 1] = cell_info[i];
154 row_info2[i + 1] = row_info[i];
156 for (i = row; row_info[i].is_cont_row; --i);
157 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
158 row_info2[row + 1].is_cont_row = false;
159 row_info2[row + 1].top_line = row_info[i].top_line;
160 cell_info2[row + 1] = new cellstruct[columns];
161 for (i = 0; i < columns; ++i) {
162 cell_info2[row + 1][i].width_of_cell = 0;
163 cell_info2[row + 1][i] = cell_info2[row][i];
167 cell_info = cell_info2;
169 row_info = row_info2;
177 void LyXTable::DeleteRow(int cell)
179 int row = row_of_cell(cell);
180 while(!row_info[row].is_cont_row && RowHasContRow(cell))
181 DeleteRow(cell_info[row+1][0].cellno);
182 rowstruct * row_info2 = new rowstruct[rows - 1];
183 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
185 delete[] cell_info[row];
187 for (; i < row; ++i) {
188 cell_info2[i] = cell_info[i];
189 row_info2[i] = row_info[i];
191 if (row_info[i].is_cont_row)
192 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
193 for (i = row; i < rows - 1; ++i) {
194 cell_info2[i] = cell_info[i + 1];
195 row_info2[i] = row_info[i + 1];
199 cell_info = cell_info2;
201 row_info = row_info2;
209 void LyXTable::AppendColumn(int cell)
212 columnstruct * column_info2 = new columnstruct[columns + 1];
213 int column = right_column_of_cell(cell);
216 for (; i <= column; ++i) {
217 column_info2[i] = column_info[i];
219 for (i = columns - 1; i >= column; --i) {
220 column_info2[i + 1] = column_info[i];
223 delete[] column_info;
224 column_info = column_info2;
226 for (i = 0; i < rows; ++i) {
227 cellstruct * tmp = cell_info[i];
228 cell_info[i] = new cellstruct[columns + 1];
229 for (j = 0; j <= column; ++j) {
230 cell_info[i][j] = tmp[j];
232 for (j = column; j < columns; ++j) {
233 cell_info[i][j + 1] = tmp[j];
235 // care about multicolumns
236 if (cell_info[i][column + 1].multicolumn
237 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
238 cell_info[i][column + 1].multicolumn =
239 LyXTable::CELL_PART_OF_MULTICOLUMN;
241 if (column + 1 == columns
242 || cell_info[i][column + 2].multicolumn
243 != LyXTable::CELL_PART_OF_MULTICOLUMN){
244 cell_info[i][column + 1].multicolumn =
245 LyXTable::CELL_NORMAL;
255 void LyXTable::Reinit()
260 for (; i < rows; ++i) {
261 for (j = 0; j < columns; ++j) {
262 cell_info[i][j].width_of_cell = 0;
263 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
264 cell_info[i][j].has_cont_row = false;
268 for (i = 0; i < columns; ++i) {
269 calculate_width_of_column(i);
271 calculate_width_of_table();
273 set_row_column_number_info();
277 void LyXTable::set_row_column_number_info()
283 for (; row < rows; ++row) {
284 for (column = 0; column<columns; ++column) {
285 if (cell_info[row][column].multicolumn
286 != LyXTable::CELL_PART_OF_MULTICOLUMN)
288 cell_info[row][column].cellno = numberofcells;
291 ++numberofcells; // because this is one more than as we start from 0
297 rowofcell = new int[numberofcells];
299 delete [] columnofcell;
300 columnofcell = new int[numberofcells];
302 while (c < numberofcells && row < rows && column < columns) {
304 columnofcell[c] = column;
308 } while (column < columns &&
309 cell_info[row][column].multicolumn
310 == LyXTable::CELL_PART_OF_MULTICOLUMN);
311 if (column == columns){
319 void LyXTable::DeleteColumn(int cell)
321 int column1 = column_of_cell(cell);
322 int column2 = right_column_of_cell(cell);
324 if (column1 == 0 && column2 == columns - 1)
327 for (int column = column1; column <= column2; ++column) {
328 delete_column(column1);
334 int LyXTable::GetNumberOfCells()
336 return numberofcells;
340 int LyXTable::NumberOfCellsInRow(int cell)
342 int row = row_of_cell(cell);
344 for (int i = 0; i < columns; ++i) {
345 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
352 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
354 return (right_column_of_cell(append_cell) ==
355 right_column_of_cell(question_cell));
359 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
361 if (column_of_cell(delete_column_cell) == 0 &&
362 right_column_of_cell(delete_column_cell) == columns - 1)
366 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
367 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
371 /* returns 1 if there is a topline, returns 0 if not */
372 bool LyXTable::TopLine(int cell)
374 int row = row_of_cell(cell);
377 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
378 if (IsMultiColumn(cell))
379 return cellinfo_of_cell(cell)->top_line;
380 return row_info[row].top_line;
384 bool LyXTable::BottomLine(int cell)
386 //no bottom line underneath non-existent cells if you please
387 if(cell >= numberofcells)
390 int row = row_of_cell(cell);
392 if (RowHasContRow(cell))
393 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
394 if (IsMultiColumn(cell))
395 return cellinfo_of_cell(cell)->bottom_line;
396 return row_info[row_of_cell(cell)].bottom_line;
400 bool LyXTable::LeftLine(int cell)
402 return column_info[column_of_cell(cell)].left_line;
406 bool LyXTable::RightLine(int cell)
408 return column_info[right_column_of_cell(cell)].right_line;
412 bool LyXTable::TopAlreadyDrawed(int cell)
414 if (AdditionalHeight(cell))
416 int row = row_of_cell(cell);
418 int column = column_of_cell(cell);
420 && cell_info[row-1][column].multicolumn
421 == LyXTable::CELL_PART_OF_MULTICOLUMN)
423 if (cell_info[row-1][column].multicolumn
424 == LyXTable::CELL_NORMAL)
425 return row_info[row-1].bottom_line;
427 return cell_info[row-1][column].bottom_line;
433 bool LyXTable::VeryLastRow(int cell)
435 return (row_of_cell(cell) == rows - 1);
439 int LyXTable::AdditionalHeight(int cell)
441 int row = row_of_cell(cell);
446 for (column = 0;column < columns-1 && bottom;column++){
447 switch (cell_info[row-1][column].multicolumn){
448 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
449 bottom = cell_info[row-1][column].bottom_line;
451 case LyXTable::CELL_NORMAL:
452 bottom = row_info[row-1].bottom_line;
455 for (column = 0;column < columns-1 && top;column++){
456 switch (cell_info[row][column].multicolumn){
457 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
458 top = cell_info[row][column].top_line;
460 case LyXTable::CELL_NORMAL:
461 top = row_info[row].top_line;
465 return WIDTH_OF_LINE;
471 int LyXTable::AdditionalWidth(int cell)
473 // internally already set in SetWidthOfCell
474 // used to get it back in text.C
475 int col = right_column_of_cell(cell);
476 if (col < columns - 1 && column_info[col].right_line &&
477 column_info[col+1].left_line)
478 return WIDTH_OF_LINE;
484 // returns the maximum over all rows
485 int LyXTable::WidthOfColumn(int cell)
487 int column1 = column_of_cell(cell);
488 int column2 = right_column_of_cell(cell);
491 for (i = column1; i<= column2;i++){
492 result += column_info[i].width_of_column;
498 int LyXTable::WidthOfTable()
500 return width_of_table;
503 /* returns 1 if a complete update is necessary, otherwise 0 */
504 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
506 if (!IsMultiColumn(cell))
509 int row = row_of_cell(cell);
510 int column1 = column_of_cell(cell);
511 int column2 = right_column_of_cell(cell);
515 // first set columns to 0 so we can calculate the right width
516 for (i = column1; i<= column2;i++) {
517 cell_info[row][i].width_of_cell = 0;
519 // set the width to MAX_WIDTH until width > 0
520 width = (new_width + 2*WIDTH_OF_LINE);
521 for (i = column1; (i<column2) && (width > 0);i++){
522 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
523 width -= column_info[i].width_of_column;
526 cell_info[row][i].width_of_cell = width;
531 void LyXTable::recalculateMulticolCells(int cell, int new_width)
534 row = row_of_cell(cell),
535 column1 = column_of_cell(cell),
536 column2 = right_column_of_cell(cell),
539 // first set columns to 0 so we can calculate the right width
540 for (i = column1; i<= column2;++i)
541 cell_info[row][i].width_of_cell = 0;
542 for(i = cell+1;(i<numberofcells) && (!IsMultiColumn(i));++i)
544 if (i < numberofcells)
545 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
546 SetWidthOfMulticolCell(cell, new_width);
549 /* returns 1 if a complete update is necessary, otherwise 0 */
550 bool LyXTable::SetWidthOfCell(int cell, int new_width)
552 int row = row_of_cell(cell);
553 int column1 = column_of_cell(cell);
557 if (IsMultiColumn(cell)) {
558 tmp = SetWidthOfMulticolCell(cell, new_width);
560 width = (new_width + 2*WIDTH_OF_LINE);
561 cell_info[row][column1].width_of_cell = width;
562 if (column_info[column1].right_line && (column1 < columns-1) &&
563 column_info[column1+1].left_line) // additional width
564 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
565 tmp = calculate_width_of_column_NMC(column1);
569 for(i = 0; i<columns;++i)
570 calculate_width_of_column_NMC(i);
571 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
574 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
575 for(i = 0; i<columns;++i)
576 calculate_width_of_column(i);
577 calculate_width_of_table();
584 bool LyXTable::SetAlignment(int cell, char align)
586 if (!IsMultiColumn(cell))
587 column_info[column_of_cell(cell)].alignment = align;
588 cellinfo_of_cell(cell)->alignment = align;
592 bool LyXTable::SetPWidth(int cell, string width)
594 int fvcell = FirstVirtualCell(cell);
596 if (IsMultiColumn(fvcell)) {
597 // if (column_info[column_of_cell(cell)].p_width.empty())
598 // column_info[column_of_cell(cell)].p_width = width;
599 cellinfo_of_cell(fvcell)->p_width = width;
601 column_info[column_of_cell(fvcell)].p_width = width;
602 if (!width.empty()) // do this only if there is a width
603 SetAlignment(cell, LYX_ALIGN_LEFT);
608 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
610 if (what == SET_SPECIAL_MULTI)
611 cellinfo_of_cell(cell)->align_special = special;
613 column_info[column_of_cell(cell)].align_special = special;
617 bool LyXTable::SetAllLines(int cell, bool line)
619 SetTopLine(cell, line);
620 SetBottomLine(cell, line);
621 SetRightLine(cell, line);
622 SetLeftLine(cell, line);
626 bool LyXTable::SetTopLine(int cell, bool line)
628 int row = row_of_cell(cell);
631 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
632 else if (!IsMultiColumn(cell))
633 row_info[row].top_line = line;
635 cellinfo_of_cell(cell)->top_line = line;
640 bool LyXTable::SetBottomLine(int cell, bool line)
642 int row = row_of_cell(cell);
644 if (RowHasContRow(cell))
645 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
646 else if (!IsMultiColumn(cell))
647 row_info[row_of_cell(cell)].bottom_line = line;
649 cellinfo_of_cell(cell)->bottom_line = line;
654 bool LyXTable::SetLeftLine(int cell, bool line)
656 column_info[column_of_cell(cell)].left_line = line;
661 bool LyXTable::SetRightLine(int cell, bool line)
663 column_info[right_column_of_cell(cell)].right_line = line;
668 char LyXTable::GetAlignment(int cell)
670 if (IsMultiColumn(cell))
671 return cellinfo_of_cell(cell)->alignment;
673 return column_info[column_of_cell(cell)].alignment;
676 string LyXTable::GetPWidth(int cell)
678 int fvcell = FirstVirtualCell(cell);
680 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
681 return cellinfo_of_cell(fvcell)->p_width;
682 return column_info[column_of_cell(fvcell)].p_width;
685 string LyXTable::GetAlignSpecial(int cell, int what)
687 if (what == SET_SPECIAL_MULTI)
688 return cellinfo_of_cell(cell)->align_special;
689 return column_info[column_of_cell(cell)].align_special;
692 int LyXTable::GetWidthOfCell(int cell)
694 int row = row_of_cell(cell);
695 int column1 = column_of_cell(cell);
696 int column2 = right_column_of_cell(cell);
699 for (i = column1; i<= column2;i++){
700 result += cell_info[row][i].width_of_cell;
703 result += AdditionalWidth(cell);
709 int LyXTable::GetBeginningOfTextInCell(int cell)
713 switch (GetAlignment(cell)){
714 case LYX_ALIGN_CENTER:
715 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
717 case LYX_ALIGN_RIGHT:
718 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
720 default: /* LYX_ALIGN_LEFT: nothing :-) */
730 bool LyXTable::IsFirstCell(int cell)
732 return (column_of_cell(cell) == 0);
735 bool LyXTable::IsLastCell(int cell)
737 return (right_column_of_cell(cell) == (columns-1));
741 bool LyXTable::calculate_width_of_column(int column)
744 int old_column_width = column_info[column].width_of_column;
746 for (i = 0; i<rows; i++) {
747 if (cell_info[i][column].width_of_cell > max) {
748 max = cell_info[i][column].width_of_cell;
751 column_info[column].width_of_column = max;
752 return (column_info[column].width_of_column != old_column_width);
755 bool LyXTable::calculate_width_of_column_NMC(int column)
758 int old_column_width = column_info[column].width_of_column;
760 for (i = 0; i<rows; ++i) {
761 if (!IsMultiColumn(GetCellNumber(column, i)) &&
762 (cell_info[i][column].width_of_cell > max)) {
763 max = cell_info[i][column].width_of_cell;
766 column_info[column].width_of_column = max;
767 return (column_info[column].width_of_column != old_column_width);
770 void LyXTable::calculate_width_of_table()
773 for (int i = 0; i < columns; ++i) {
774 width_of_table += column_info[i].width_of_column;
779 int LyXTable::row_of_cell(int cell)
781 if (cell >= numberofcells)
785 return rowofcell[cell];
789 int LyXTable::column_of_cell(int cell)
791 if (cell >= numberofcells)
795 return columnofcell[cell];
799 int LyXTable::right_column_of_cell(int cell)
801 int row = row_of_cell(cell);
802 int column = column_of_cell(cell);
803 while (column < columns - 1 &&
804 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
810 void LyXTable::Write(ostream & os)
814 << rows << " " << columns << " " << is_long_table << " "
815 << rotate << " " << endhead << " " << endfirsthead << " "
816 << endfoot << " " << endlastfoot << "\n";
817 for (i = 0; i < rows; ++i) {
818 os << row_info[i].top_line << " "
819 << row_info[i].bottom_line << " "
820 << row_info[i].is_cont_row << " "
821 << row_info[i].newpage << "\n";
823 for (i = 0; i < columns; ++i) {
824 os << column_info[i].alignment << " "
825 << column_info[i].left_line << " "
826 << column_info[i].right_line << " \""
827 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
828 << column_info[i].align_special << "\"\n";
831 for (i = 0; i < rows; ++i) {
832 for (j = 0; j < columns; ++j) {
833 os << cell_info[i][j].multicolumn << " "
834 << cell_info[i][j].alignment << " "
835 << cell_info[i][j].top_line << " "
836 << cell_info[i][j].bottom_line << " "
837 << cell_info[i][j].has_cont_row << " "
838 << cell_info[i][j].rotate << " "
839 << cell_info[i][j].linebreaks << " \""
840 << cell_info[i][j].align_special << "\" \""
841 << cell_info[i][j].p_width << "\"\n";
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 lyxerr << "Tabular format < 5 is not supported anymore\n"
877 "Get an older version of LyX (< 1.1.x) for conversion!"
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++){
893 a = b = c = d = e = f = g = h = 0;
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 = static_cast<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) {
929 a = b = c = d = e = f = g = 0;
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 = static_cast<char>(a);
934 cell_info[i][j].alignment = static_cast<char>(b);
935 cell_info[i][j].top_line = static_cast<char>(c);
936 cell_info[i][j].bottom_line = static_cast<char>(d);
937 cell_info[i][j].has_cont_row = static_cast<bool>(e);
938 cell_info[i][j].rotate = static_cast<bool>(f);
939 cell_info[i][j].linebreaks = static_cast<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(ostream & os, int cell)
1306 if (cell == GetNumberOfCells() - 1){
1307 // the very end at the very beginning
1308 if (CellHasContRow(cell) >= 0) {
1314 if (row_info[row_of_cell(cell)].bottom_line) {
1318 os << ".TE\n.pl 1c";
1323 os << "\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)
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 os << "w(" << column_info[i].p_width << ")";
1345 if (column_info[i].right_line)
1348 if ((j + 1) < rows) {
1355 if (row_info[0].top_line) {
1359 if (CellHasContRow(0) >= 0) {
1365 if (CellHasContRow(cell) >= 0) {
1369 if (right_column_of_cell(cell) == columns -1){
1372 int row = row_of_cell(cell);
1373 if (row_info[row++].bottom_line) {
1377 if ((row < rows) && row_info[row].top_line) {
1383 if ((cell < GetNumberOfCells() - 1) &&
1384 (CellHasContRow(cell+1) >= 0)) {
1394 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1400 i = column_of_cell(cell);
1401 if (!isColumn && IsMultiColumn(cell)) {
1402 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1403 return cellinfo_of_cell(cell)->align_special.c_str();
1405 switch (GetAlignment(cell)) {
1406 case LYX_ALIGN_LEFT:
1408 case LYX_ALIGN_RIGHT:
1415 if (!column_info[i].align_special.empty()) {
1416 return column_info[i].align_special.c_str();
1418 #ifdef IGNORE_THIS_FOR_NOW
1419 else if (!column_info[i].p_width.empty()) {
1421 file += column_info[i].p_width;
1426 switch (column_info[i].alignment) {
1427 case LYX_ALIGN_LEFT:
1429 case LYX_ALIGN_RIGHT:
1438 // cell <0 will tex the preamble
1439 // returns the number of printed newlines
1440 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1444 //int tmp; // tmp2; //Â unused
1445 int nvcell; // fcell; //Â unused
1446 if (ShouldBeVeryLastCell(cell)) {
1447 addNewlineAndDepth(file,--depth);
1449 addNewlineAndDepth(file,--depth);
1451 addNewlineAndDepth(file,--depth);
1453 addNewlineAndDepth(file,--depth);
1455 file += "</TGROUP>";
1457 file += "</TGROUP>";
1458 addNewlineAndDepth(file,--depth);
1461 nvcell = NextVirtualCell(cell+1);
1469 file += tostr(columns);
1470 file += "' COLSEP='1' ROWSEP='1'>";
1471 addNewlineAndDepth(file,++depth);
1473 for (i = 0; i < columns; ++i) {
1474 file += "<COLSPEC ALIGN='";
1475 file += getDocBookAlign(i, true);
1476 file += "' COLNAME='col";
1478 file += "' COLNUM='";
1480 file += "' COLSEP='";
1481 if (i == (columns-1)) {
1484 if (column_info[i].right_line ||
1485 column_info[i+1].left_line)
1491 addNewlineAndDepth(file, depth);
1493 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1494 if (column_info[i].left_line)
1499 addNewlineAndDepth(file,++depth);
1501 addNewlineAndDepth(file,++depth);
1502 file += "<ENTRY ALIGN='";
1503 file += getDocBookAlign(0);
1505 if (IsMultiColumn(0)) {
1506 file += " NAMEST='col1' NAMEEND='col";
1507 file += tostr(cells_in_multicolumn(0));
1511 addNewlineAndDepth(file,++depth);
1514 if (IsLastCell(cell)) {
1515 addNewlineAndDepth(file,--depth);
1517 addNewlineAndDepth(file,--depth);
1519 addNewlineAndDepth(file, depth);
1521 addNewlineAndDepth(file,++depth);
1522 file += "<ENTRY ALIGN='";
1523 file += getDocBookAlign(cell+1);
1524 file += "' VALIGN='middle'";
1525 if (IsMultiColumn(cell+1)) {
1526 file += " NAMEST='col";
1527 file += tostr(column_of_cell(cell+1) + 1);
1528 file += "' NAMEEND='col";
1529 file += tostr(column_of_cell(cell+1) +
1530 cells_in_multicolumn(cell+1));
1534 addNewlineAndDepth(file,++depth);
1537 addNewlineAndDepth(file,--depth);
1539 addNewlineAndDepth(file, depth);
1540 file += "<ENTRY ALIGN='";
1541 file += getDocBookAlign(cell+1);
1542 file += "' VALIGN='middle'";
1543 if (IsMultiColumn(cell+1)) {
1544 file += " NAMEST='col";
1545 file += tostr(column_of_cell(cell+1) + 1);
1546 file += "' NAMEEND='col";
1547 file += tostr(column_of_cell(cell+1) +
1548 cells_in_multicolumn(cell+1));
1552 addNewlineAndDepth(file,++depth);
1561 bool LyXTable::IsMultiColumn(int cell)
1563 int fvcell = FirstVirtualCell(cell);
1565 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1569 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1571 int row = row_of_cell(cell);
1572 int column = column_of_cell(cell);
1573 return &cell_info[row][column];
1577 void LyXTable::SetMultiColumn(int cell, int number)
1579 int fvcell = FirstVirtualCell(cell);
1580 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1582 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1583 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1584 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1585 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1586 for (number--;number>0;number--){
1587 cellinfo_of_cell(fvcell+number)->multicolumn =
1588 LyXTable::CELL_PART_OF_MULTICOLUMN;
1589 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1591 set_row_column_number_info();
1592 SetWidthOfCell(fvcell, new_width);
1596 int LyXTable::cells_in_multicolumn(int cell)
1598 int row = row_of_cell(cell);
1599 int column = column_of_cell(cell);
1602 while (column < columns && cell_info[row][column].multicolumn
1603 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1611 int LyXTable::UnsetMultiColumn(int cell)
1613 int fvcell = FirstVirtualCell(cell);
1614 int row = row_of_cell(fvcell);
1615 int column = column_of_cell(fvcell);
1619 if (cell_info[row][column].multicolumn
1620 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1621 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1623 while (column < columns &&
1624 cell_info[row][column].multicolumn
1625 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1626 cell_info[row][column].multicolumn =
1627 LyXTable::CELL_NORMAL;
1632 set_row_column_number_info();
1637 void LyXTable::delete_column(int column)
1640 columnstruct *column_info2 = new columnstruct[columns-1];
1642 for (i = 0; i<column; i++){
1643 column_info2[i] = column_info[i];
1645 for (i = column; i<columns-1; i++){
1646 column_info2[i] = column_info[i+1];
1649 delete[] column_info;
1650 column_info = column_info2;
1652 for (i = 0; i<rows;i++){
1653 cellstruct* tmp = cell_info[i];
1654 cell_info[i] = new cellstruct[columns-1];
1655 for (j = 0; j<column; j++){
1656 cell_info[i][j] = tmp[j];
1658 for (j = column; j<columns-1; j++){
1659 cell_info[i][j] = tmp[j+1];
1668 void LyXTable::SetLongTable(int what)
1670 is_long_table = what;
1673 bool LyXTable::IsLongTable()
1675 return is_long_table;
1678 void LyXTable::SetRotateTable(int what)
1683 bool LyXTable::RotateTable()
1688 void LyXTable::SetRotateCell(int cell, int what)
1690 cellinfo_of_cell(cell)->rotate = what;
1693 bool LyXTable::RotateCell(int cell)
1695 return cellinfo_of_cell(cell)->rotate;
1698 bool LyXTable::NeedRotating()
1702 for (int i = 0; i<rows;i++){
1703 for (int j = 0;j<columns;j++){
1704 if (cell_info[i][j].rotate)
1711 void LyXTable::AppendContRow(int cell)
1713 int row = row_of_cell(cell)+1;
1715 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1717 row_info[row].is_cont_row = true;
1718 row_info[row].top_line = false;
1719 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1723 bool LyXTable::IsContRow(int cell)
1725 return row_info[row_of_cell(cell)].is_cont_row;
1728 int LyXTable::CellHasContRow(int cell)
1730 int row = row_of_cell(cell);
1732 if (VeryLastRow(cell))
1734 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1735 return cell_info[row+1][column_of_cell(cell)].cellno;
1739 bool LyXTable::RowHasContRow(int cell)
1741 int row = row_of_cell(cell) + 1;
1744 return row_info[row].is_cont_row;
1748 int LyXTable::FirstVirtualCell(int cell)
1750 if (!IsContRow(cell))
1752 int row = row_of_cell(cell);
1753 int column = column_of_cell(cell);
1754 for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1756 return cell_info[row][column].cellno;
1760 int LyXTable::NextVirtualCell(int cell)
1762 if (!IsContRow(cell))
1764 int row = row_of_cell(cell);
1765 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1767 // what if(row >= rows) ?
1768 return cell_info[row][0].cellno;
1772 bool LyXTable::ShouldBeVeryLastCell(int cell)
1773 // "very last cell" ..of what? the row? the table?
1774 // "Cell" in this context appears to not count `virtual' cells
1776 int fcell = cell + 1;
1778 if (cell == GetNumberOfCells() - 1)
1779 return true; // not really sure if I should return false here
1780 if (!IsContRow(fcell))
1782 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1784 if (fcell < GetNumberOfCells() - 1)
1789 bool LyXTable::ShouldBeVeryLastRow(int cell)
1791 if (CellHasContRow(cell)>= 0)
1793 int row = row_of_cell(cell)+1;
1794 int column = column_of_cell(cell);
1795 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1798 return false; // found another valid row
1799 // I do not have any valid row after the actual
1803 int LyXTable::GetCellAbove(int cell)
1805 int row = row_of_cell(cell);
1808 return cell_info[row-1][column_of_cell(cell)].cellno;
1812 int LyXTable::GetCellNumber(int column, int row)
1814 if (column >= columns)
1815 column = columns - 1;
1816 else if (column < 0)
1823 return cell_info[row][column].cellno;
1826 void LyXTable::SetLinebreaks(int cell, bool what)
1828 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1831 bool LyXTable::Linebreaks(int cell)
1833 int fvcell = FirstVirtualCell(cell);
1835 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1836 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1838 return cellinfo_of_cell(fvcell)->linebreaks;
1841 void LyXTable::SetLTHead(int cell, bool first)
1843 int row = row_of_cell(cell);
1846 if (row == endfirsthead)
1858 bool LyXTable::RowOfLTHead(int cell)
1860 if ((endhead+1) > rows)
1862 return (row_of_cell(cell) == endhead);
1865 bool LyXTable::RowOfLTFirstHead(int cell)
1867 if ((endfirsthead+1) > rows)
1869 return (row_of_cell(cell) == endfirsthead);
1872 void LyXTable::SetLTFoot(int cell, bool last)
1874 int row = row_of_cell(cell);
1877 if (row == endlastfoot)
1889 bool LyXTable::RowOfLTFoot(int cell)
1891 if ((endfoot+1) > rows) {
1895 return (row_of_cell(cell) == endfoot);
1898 bool LyXTable::RowOfLTLastFoot(int cell)
1900 if ((endlastfoot+1) > rows)
1902 return (row_of_cell(cell) == endlastfoot);
1905 void LyXTable::SetLTNewPage(int cell, bool what)
1907 row_info[row_of_cell(cell)].newpage = what;
1910 bool LyXTable::LTNewPage(int cell)
1912 return row_info[row_of_cell(cell)].newpage;