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 istream & is = lex.getStream();
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(istream & is)
853 int is_long_table_arg = false;
854 int rotate_arg = false;
867 version = atoi(s.c_str() + 8);
871 #warning Insert a error message window here that this format is not supported anymore
874 lyxerr << "Tabular format < 5 is not supported anymore\n"
875 "Get an older version of LyX (< 1.1.x) for conversion!"
878 is >> rows_arg >> columns_arg >> is_long_table_arg
879 >> rotate_arg >> a >> b >> c >> d;
881 is >> rows_arg >> columns_arg;
882 Init(rows_arg, columns_arg);
883 SetLongTable(is_long_table_arg);
884 SetRotateTable(rotate_arg);
886 for (i = 0; i < rows; ++i) {
889 for (i = 0; i < columns; ++i) {
892 for (i = 0; i < rows; ++i) {
893 for (j = 0; j < columns; ++j) {
897 set_row_column_number_info();
900 is >> rows_arg >> columns_arg >> is_long_table_arg
901 >> rotate_arg >> a >> b >> c >> d;
902 Init(rows_arg, columns_arg);
903 SetLongTable(is_long_table_arg);
904 SetRotateTable(rotate_arg);
909 for (i = 0; i < rows; ++i) {
910 a = b = c = d = e = f = g = h = 0;
911 is >> a >> b >> c >> d;
912 row_info[i].top_line = a;
913 row_info[i].bottom_line = b;
914 row_info[i].is_cont_row = c;
915 row_info[i].newpage = d;
917 for (i = 0; i < columns; ++i) {
923 getline(is, s1, '"');
924 is >> ch; // skip '"'
925 getline(is, s2, '"');
926 column_info[i].alignment = static_cast<char>(a);
927 column_info[i].left_line = b;
928 column_info[i].right_line = c;
929 column_info[i].p_width = s1;
930 column_info[i].align_special = s2;
932 for (i = 0; i < rows; ++i) {
933 for (j = 0; j < columns; ++j) {
936 is >> a >> b >> c >> d >> e >> f >> g;
938 is >> ch; // skip '"'
939 getline(is, s1, '"');
940 is >> ch; // skip '"'
941 getline(is, s2, '"');
942 cell_info[i][j].multicolumn = static_cast<char>(a);
943 cell_info[i][j].alignment = static_cast<char>(b);
944 cell_info[i][j].top_line = static_cast<char>(c);
945 cell_info[i][j].bottom_line = static_cast<char>(d);
946 cell_info[i][j].has_cont_row = static_cast<bool>(e);
947 cell_info[i][j].rotate = static_cast<bool>(f);
948 cell_info[i][j].linebreaks = static_cast<bool>(g);
949 cell_info[i][j].align_special = s1;
950 cell_info[i][j].p_width = s2;
953 set_row_column_number_info();
957 // cell <0 will tex the preamble
958 // returns the number of printed newlines
959 int LyXTable::TexEndOfCell(string & file, int cell)
965 if (ShouldBeVeryLastCell(cell)) {
966 // the very end at the very beginning
967 if (Linebreaks(cell))
968 file += "\\smallskip{}}";
969 if (IsMultiColumn(cell))
971 if (RotateCell(cell)) {
972 file += "\n\\end{sideways}";
980 while (!IsFirstCell(fcell))fcell--;
981 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
982 if (BottomLine(fcell+i))
985 if (tmp == NumberOfCellsInRow(fcell)){
989 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
990 if (BottomLine(fcell+i)){
992 file += tostr(column_of_cell(fcell+i)+1);
994 file += tostr(right_column_of_cell(fcell+i)+1);
1005 file += "\\end{longtable}";
1007 file += "\\end{tabular}";
1009 file += "\n\\end{sideways}";
1013 nvcell = NextVirtualCell(cell+1);
1017 file += "\\begin{sideways}\n";
1021 file += "\\begin{longtable}{";
1023 file += "\\begin{tabular}{";
1024 for (i = 0; i<columns;i++){
1025 if (column_info[i].left_line)
1027 if (!column_info[i].align_special.empty()) {
1028 file += column_info[i].align_special.c_str();
1029 } else if (!column_info[i].p_width.empty()) {
1031 file += column_info[i].p_width;
1034 switch (column_info[i].alignment) {
1035 case LYX_ALIGN_LEFT:
1038 case LYX_ALIGN_RIGHT:
1046 if (column_info[i].right_line)
1052 if (GetNumberOfCells()){
1054 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1055 if (TopLine(fcell+i))
1058 if (tmp == NumberOfCellsInRow(fcell)){
1062 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1063 if (TopLine(fcell+i)){
1065 file += tostr(column_of_cell(fcell+i)+1);
1067 file += tostr(right_column_of_cell(fcell+i)+1);
1078 if (RotateCell(0)) {
1079 file += "\\begin{sideways}\n";
1084 if (Linebreaks(cell))
1085 file += "\\smallskip{}}";
1086 if (IsMultiColumn(cell)){
1089 if (RotateCell(cell)) {
1090 file += "\n\\end{sideways}";
1093 if (IsLastCell(cell)) {
1094 int row = row_of_cell(cell);
1095 string hline1, hline2;
1096 bool print_hline = true;
1097 bool pr_top_hline, flag1, flag2;
1098 flag1 = IsLongTable() &&
1099 ((row == endhead) || (row == endfirsthead) ||
1100 (row == endfoot) || (row == endlastfoot));
1102 flag2 = IsLongTable() &&
1103 ((row <= endhead) || (row <= endfirsthead) ||
1104 (row <= endfoot) || (row <= endlastfoot));
1106 // print the bottom hline only if (otherwise it is doubled):
1107 // - is no LongTable
1108 // - there IS a first-header
1109 // - the next row is no special header/footer
1110 // & this row is no special header/footer
1111 // - the next row is a special header/footer
1112 // & this row is a special header/footer
1113 pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1114 (endfirsthead == endhead);
1119 while (!IsFirstCell(fcell))
1121 for (i = 0; i < NumberOfCellsInRow(cell); i++){
1122 if (BottomLine(fcell+i))
1125 if (tmp == NumberOfCellsInRow(cell)){
1127 hline1 = "\\hline ";
1130 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1131 if (BottomLine(fcell+i)){
1133 file += tostr(column_of_cell(fcell+i)+1);
1135 file += tostr(right_column_of_cell(fcell+i)+1);
1137 hline1 += "\\cline{";
1138 hline1 += tostr(column_of_cell(fcell+i)+1);
1140 hline1 += tostr(right_column_of_cell(fcell+i)+1);
1150 if (IsLongTable() && (row == endfoot)) {
1151 file += "\\endfoot\n";
1153 print_hline = false; // no double line below footer
1155 if (IsLongTable() && (row == endlastfoot)) {
1156 file += "\\endlastfoot\n";
1158 print_hline = false; // no double line below footer
1160 if (IsLongTable() && row_info[row].newpage) {
1161 file += "\\newpage\n";
1163 print_hline = false; // no line below a \\newpage-command
1166 if (nvcell < numberofcells && (cell < GetNumberOfCells()-1) &&
1167 !ShouldBeVeryLastCell(cell)) {
1169 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1170 if (TopLine(fcell+i))
1173 if (tmp == NumberOfCellsInRow(fcell)){
1176 hline2 = "\\hline ";
1180 for (i = 0; i < NumberOfCellsInRow(fcell); i++){
1181 if (TopLine(fcell+i)){
1184 file += tostr(column_of_cell(fcell+i)+1);
1186 file += tostr(right_column_of_cell(fcell+i)+1);
1189 hline2 += "\\cline{";
1190 hline2 += tostr(column_of_cell(fcell+i)+1);
1192 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1198 if (tmp && print_hline){
1203 // the order here is important as if one defines two
1204 // or more things in one line only the first entry is
1205 // displayed the other are set to an empty-row. This
1206 // is important if I have a footer and want that the
1207 // lastfooter is NOT displayed!!!
1208 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1209 (row == endfoot) || (row == endlastfoot);
1211 // sflag2 = IsLongTable() && (row >= 0) &&
1212 // (sflag2 || (row == endhead) || (row == endfirsthead));
1214 bool sflag1 = IsLongTable() && (row != endhead) &&
1215 (row != endfirsthead) &&
1216 ((row == endfoot) || (row == endlastfoot));
1218 if (IsLongTable() && (row == endhead)) {
1219 file += "\\endhead\n";
1222 if (IsLongTable() && (row == endfirsthead)) {
1223 file += "\\endfirsthead\n";
1226 if (sflag1) { // add the \hline for next foot row
1227 if (!hline1.empty()) {
1228 file += hline1 + '\n';
1232 // add the \hline for the first row
1233 if (pr_top_hline && sflag2) {
1234 if (!hline2.empty()) {
1235 file += hline2 + '\n';
1239 if (nvcell < numberofcells && RotateCell(nvcell)) {
1240 file += "\\begin{sideways}\n";
1246 if (nvcell < numberofcells && RotateCell(nvcell)) {
1247 file += "\\begin{sideways}\n";
1252 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1253 file += "\\multicolumn{";
1254 file += tostr(cells_in_multicolumn(nvcell));
1256 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1257 file += cellinfo_of_cell(cell+1)->align_special;
1260 if (LeftLine(nvcell))
1262 if (!GetPWidth(nvcell).empty()) {
1264 file += GetPWidth(nvcell);
1267 switch (GetAlignment(nvcell)) {
1268 case LYX_ALIGN_LEFT: file += 'l'; break;
1269 case LYX_ALIGN_RIGHT: file += 'r'; break;
1270 default: file += 'c'; break;
1273 if (RightLine(nvcell))
1275 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1276 if (((nvcell+1) < numberofcells) &&
1277 (NextVirtualCell(nvcell+1) < numberofcells) &&
1278 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1279 LeftLine(NextVirtualCell(nvcell+1)))
1284 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1285 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1286 file += "\\parbox{";
1287 file += GetPWidth(nvcell);
1288 file += "}{\\smallskip{}";
1295 // cell <0 will tex the preamble
1296 // returns the number of printed newlines
1297 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1302 if (cell == GetNumberOfCells() - 1){
1303 // the very end at the very beginning
1304 if (CellHasContRow(cell) >= 0) {
1310 if (row_info[row_of_cell(cell)].bottom_line) {
1314 os << ".TE\n.pl 1c";
1319 os << "\n.pl 500c\n.TS\n";
1320 for (j = 0; j<rows; ++j) {
1321 for (i = 0; i<columns; ++i, ++fcell) {
1322 if (column_info[i].left_line)
1324 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1327 switch (column_info[i].alignment) {
1328 case LYX_ALIGN_LEFT:
1331 case LYX_ALIGN_RIGHT:
1339 if (!column_info[i].p_width.empty())
1340 os << "w(" << column_info[i].p_width << ")";
1341 if (column_info[i].right_line)
1344 if ((j + 1) < rows) {
1351 if (row_info[0].top_line) {
1355 if (CellHasContRow(0) >= 0) {
1361 if (CellHasContRow(cell) >= 0) {
1365 if (right_column_of_cell(cell) == columns -1){
1368 int row = row_of_cell(cell);
1369 if (row_info[row++].bottom_line) {
1373 if ((row < rows) && row_info[row].top_line) {
1379 if ((cell < GetNumberOfCells() - 1) &&
1380 (CellHasContRow(cell+1) >= 0)) {
1390 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1396 i = column_of_cell(cell);
1397 if (!isColumn && IsMultiColumn(cell)) {
1398 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1399 return cellinfo_of_cell(cell)->align_special.c_str();
1401 switch (GetAlignment(cell)) {
1402 case LYX_ALIGN_LEFT:
1404 case LYX_ALIGN_RIGHT:
1411 if (!column_info[i].align_special.empty()) {
1412 return column_info[i].align_special.c_str();
1414 #ifdef IGNORE_THIS_FOR_NOW
1415 else if (!column_info[i].p_width.empty()) {
1417 file += column_info[i].p_width;
1422 switch (column_info[i].alignment) {
1423 case LYX_ALIGN_LEFT:
1425 case LYX_ALIGN_RIGHT:
1434 // cell <0 will tex the preamble
1435 // returns the number of printed newlines
1436 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1440 //int tmp; // tmp2; //Â unused
1441 int nvcell; // fcell; //Â unused
1442 if (ShouldBeVeryLastCell(cell)) {
1443 addNewlineAndDepth(file,--depth);
1445 addNewlineAndDepth(file,--depth);
1447 addNewlineAndDepth(file,--depth);
1449 addNewlineAndDepth(file,--depth);
1451 file += "</TGROUP>";
1453 file += "</TGROUP>";
1454 addNewlineAndDepth(file,--depth);
1457 nvcell = NextVirtualCell(cell+1);
1465 file += tostr(columns);
1466 file += "' COLSEP='1' ROWSEP='1'>";
1467 addNewlineAndDepth(file,++depth);
1469 for (i = 0; i < columns; ++i) {
1470 file += "<COLSPEC ALIGN='";
1471 file += getDocBookAlign(i, true);
1472 file += "' COLNAME='col";
1474 file += "' COLNUM='";
1476 file += "' COLSEP='";
1477 if (i == (columns-1)) {
1480 if (column_info[i].right_line ||
1481 column_info[i+1].left_line)
1487 addNewlineAndDepth(file, depth);
1489 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1490 if (column_info[i].left_line)
1495 addNewlineAndDepth(file,++depth);
1497 addNewlineAndDepth(file,++depth);
1498 file += "<ENTRY ALIGN='";
1499 file += getDocBookAlign(0);
1501 if (IsMultiColumn(0)) {
1502 file += " NAMEST='col1' NAMEEND='col";
1503 file += tostr(cells_in_multicolumn(0));
1507 addNewlineAndDepth(file,++depth);
1510 if (IsLastCell(cell)) {
1511 addNewlineAndDepth(file,--depth);
1513 addNewlineAndDepth(file,--depth);
1515 addNewlineAndDepth(file, depth);
1517 addNewlineAndDepth(file,++depth);
1518 file += "<ENTRY ALIGN='";
1519 file += getDocBookAlign(cell+1);
1520 file += "' VALIGN='middle'";
1521 if (IsMultiColumn(cell+1)) {
1522 file += " NAMEST='col";
1523 file += tostr(column_of_cell(cell+1) + 1);
1524 file += "' NAMEEND='col";
1525 file += tostr(column_of_cell(cell+1) +
1526 cells_in_multicolumn(cell+1));
1530 addNewlineAndDepth(file,++depth);
1533 addNewlineAndDepth(file,--depth);
1535 addNewlineAndDepth(file, depth);
1536 file += "<ENTRY ALIGN='";
1537 file += getDocBookAlign(cell+1);
1538 file += "' VALIGN='middle'";
1539 if (IsMultiColumn(cell+1)) {
1540 file += " NAMEST='col";
1541 file += tostr(column_of_cell(cell+1) + 1);
1542 file += "' NAMEEND='col";
1543 file += tostr(column_of_cell(cell+1) +
1544 cells_in_multicolumn(cell+1));
1548 addNewlineAndDepth(file,++depth);
1557 bool LyXTable::IsMultiColumn(int cell)
1559 int fvcell = FirstVirtualCell(cell);
1561 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1565 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1567 int row = row_of_cell(cell);
1568 int column = column_of_cell(cell);
1569 return &cell_info[row][column];
1573 void LyXTable::SetMultiColumn(int cell, int number)
1575 int fvcell = FirstVirtualCell(cell);
1576 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1578 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1579 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1580 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1581 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1582 for (number--;number>0;number--){
1583 cellinfo_of_cell(fvcell+number)->multicolumn =
1584 LyXTable::CELL_PART_OF_MULTICOLUMN;
1585 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1587 set_row_column_number_info();
1588 SetWidthOfCell(fvcell, new_width);
1592 int LyXTable::cells_in_multicolumn(int cell)
1594 int row = row_of_cell(cell);
1595 int column = column_of_cell(cell);
1598 while (column < columns && cell_info[row][column].multicolumn
1599 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1607 int LyXTable::UnsetMultiColumn(int cell)
1609 int fvcell = FirstVirtualCell(cell);
1610 int row = row_of_cell(fvcell);
1611 int column = column_of_cell(fvcell);
1615 if (cell_info[row][column].multicolumn
1616 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1617 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1619 while (column < columns &&
1620 cell_info[row][column].multicolumn
1621 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1622 cell_info[row][column].multicolumn =
1623 LyXTable::CELL_NORMAL;
1628 set_row_column_number_info();
1633 void LyXTable::delete_column(int column)
1636 columnstruct *column_info2 = new columnstruct[columns-1];
1638 for (i = 0; i<column; i++){
1639 column_info2[i] = column_info[i];
1641 for (i = column; i<columns-1; i++){
1642 column_info2[i] = column_info[i+1];
1645 delete[] column_info;
1646 column_info = column_info2;
1648 for (i = 0; i<rows;i++){
1649 cellstruct* tmp = cell_info[i];
1650 cell_info[i] = new cellstruct[columns-1];
1651 for (j = 0; j<column; j++){
1652 cell_info[i][j] = tmp[j];
1654 for (j = column; j<columns-1; j++){
1655 cell_info[i][j] = tmp[j+1];
1664 void LyXTable::SetLongTable(int what)
1666 is_long_table = what;
1669 bool LyXTable::IsLongTable()
1671 return is_long_table;
1674 void LyXTable::SetRotateTable(int what)
1679 bool LyXTable::RotateTable()
1684 void LyXTable::SetRotateCell(int cell, int what)
1686 cellinfo_of_cell(cell)->rotate = what;
1689 bool LyXTable::RotateCell(int cell)
1691 return cellinfo_of_cell(cell)->rotate;
1694 bool LyXTable::NeedRotating()
1698 for (int i = 0; i<rows;i++){
1699 for (int j = 0;j<columns;j++){
1700 if (cell_info[i][j].rotate)
1707 void LyXTable::AppendContRow(int cell)
1709 int row = row_of_cell(cell)+1;
1711 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1713 row_info[row].is_cont_row = true;
1714 row_info[row].top_line = false;
1715 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1719 bool LyXTable::IsContRow(int cell)
1721 return row_info[row_of_cell(cell)].is_cont_row;
1724 int LyXTable::CellHasContRow(int cell)
1726 int row = row_of_cell(cell);
1728 if (VeryLastRow(cell))
1730 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1731 return cell_info[row+1][column_of_cell(cell)].cellno;
1735 bool LyXTable::RowHasContRow(int cell)
1737 int row = row_of_cell(cell) + 1;
1740 return row_info[row].is_cont_row;
1744 int LyXTable::FirstVirtualCell(int cell)
1746 if (!IsContRow(cell))
1748 int row = row_of_cell(cell);
1749 int column = column_of_cell(cell);
1750 for(;(row>0) && IsContRow(cell_info[row][column].cellno); row--)
1752 return cell_info[row][column].cellno;
1756 int LyXTable::NextVirtualCell(int cell)
1758 if (!IsContRow(cell))
1760 int row = row_of_cell(cell);
1761 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1763 // what if(row >= rows) ?
1764 return cell_info[row][0].cellno;
1768 bool LyXTable::ShouldBeVeryLastCell(int cell)
1769 // "very last cell" ..of what? the row? the table?
1770 // "Cell" in this context appears to not count `virtual' cells
1772 int fcell = cell + 1;
1774 if (cell == GetNumberOfCells() - 1)
1775 return true; // not really sure if I should return false here
1776 if (!IsContRow(fcell))
1778 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1780 if (fcell < GetNumberOfCells() - 1)
1785 bool LyXTable::ShouldBeVeryLastRow(int cell)
1787 if (CellHasContRow(cell)>= 0)
1789 int row = row_of_cell(cell)+1;
1790 int column = column_of_cell(cell);
1791 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1794 return false; // found another valid row
1795 // I do not have any valid row after the actual
1799 int LyXTable::GetCellAbove(int cell)
1801 int row = row_of_cell(cell);
1804 return cell_info[row-1][column_of_cell(cell)].cellno;
1808 int LyXTable::GetCellNumber(int column, int row)
1810 if (column >= columns)
1811 column = columns - 1;
1812 else if (column < 0)
1819 return cell_info[row][column].cellno;
1822 void LyXTable::SetLinebreaks(int cell, bool what)
1824 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1827 bool LyXTable::Linebreaks(int cell)
1829 int fvcell = FirstVirtualCell(cell);
1831 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1832 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1834 return cellinfo_of_cell(fvcell)->linebreaks;
1837 void LyXTable::SetLTHead(int cell, bool first)
1839 int row = row_of_cell(cell);
1842 if (row == endfirsthead)
1854 bool LyXTable::RowOfLTHead(int cell)
1856 if ((endhead+1) > rows)
1858 return (row_of_cell(cell) == endhead);
1861 bool LyXTable::RowOfLTFirstHead(int cell)
1863 if ((endfirsthead+1) > rows)
1865 return (row_of_cell(cell) == endfirsthead);
1868 void LyXTable::SetLTFoot(int cell, bool last)
1870 int row = row_of_cell(cell);
1873 if (row == endlastfoot)
1885 bool LyXTable::RowOfLTFoot(int cell)
1887 if ((endfoot+1) > rows) {
1891 return (row_of_cell(cell) == endfoot);
1894 bool LyXTable::RowOfLTLastFoot(int cell)
1896 if ((endlastfoot+1) > rows)
1898 return (row_of_cell(cell) == endlastfoot);
1901 void LyXTable::SetLTNewPage(int cell, bool what)
1903 row_info[row_of_cell(cell)].newpage = what;
1906 bool LyXTable::LTNewPage(int cell)
1908 return row_info[row_of_cell(cell)].newpage;