1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ======================================================
18 #pragma implementation
24 #include "support/lstrings.h"
25 #include "support/lyxmanip.h"
29 static int const WIDTH_OF_LINE = 5;
33 LyXTable::LyXTable(int rows_arg, int columns_arg)
35 Init(rows_arg, columns_arg);
39 LyXTable::LyXTable(LyXLex & lex)
41 istream & is = lex.getStream();
46 LyXTable::~LyXTable() {
48 delete[] columnofcell;
51 for (int i = 0; i < rows; ++i) {
52 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
58 LyXTable * LyXTable::Clone()
60 LyXTable * result = new LyXTable(rows, columns);
63 for (row = 0; row < rows; ++row) {
64 for (column = 0; column < columns; ++column) {
65 result->cell_info[row][column] = cell_info[row][column];
69 for (row = 0; row < rows; ++row) {
70 result->row_info[row] = row_info[row];
73 for (column = 0; column < columns; ++column) {
74 result->column_info[column].left_line = column_info[column].left_line;
75 result->column_info[column].right_line = column_info[column].right_line;
76 result->column_info[column].alignment = column_info[column].alignment;
77 result->column_info[column].p_width = column_info[column].p_width;
78 result->column_info[column].align_special = column_info[column].align_special;
81 result->SetLongTable(is_long_table);
82 result->rotate = rotate;
88 /* activates all lines and sets all widths to 0 */
89 void LyXTable::Init(int rows_arg, int columns_arg)
93 columns = columns_arg;
94 column_info = new columnstruct[columns];
95 row_info = new rowstruct[rows];
96 cell_info = new cellstruct*[rows];
99 for (i = 0; i < rows; ++i) {
100 cell_info[i] = new cellstruct[columns];
101 row_info[i].top_line = true;
102 row_info[i].bottom_line = false;
103 row_info[i].is_cont_row = false;
104 row_info[i].newpage = false;
105 for (j = 0; j < columns; ++j) {
106 cell_info[i][j].cellno = cellno++;
107 cell_info[i][j].width_of_cell = 0;
108 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
109 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
110 cell_info[i][j].top_line = row_info[i].top_line;
111 cell_info[i][j].bottom_line = row_info[i].bottom_line;
112 cell_info[i][j].has_cont_row = false;
113 cell_info[i][j].rotate = false;
114 cell_info[i][j].linebreaks = false;
117 row_info[i-1].bottom_line = true;
118 row_info[0].bottom_line = true;
120 for (i = 0; i < columns; ++i) {
121 column_info[i].left_line = true;
122 column_info[i].right_line = false;
123 column_info[i].alignment = LYX_ALIGN_CENTER;
124 // set width_of_column to zero before it is used in
125 // calculate_width_of_column() (thornley)
126 column_info[i].width_of_column = 0;
127 calculate_width_of_column(i);
129 column_info[i-1].right_line = true;
131 calculate_width_of_table();
135 set_row_column_number_info();
136 is_long_table = false;
145 void LyXTable::AppendRow(int cell)
147 int row = row_of_cell(cell);
148 rowstruct * row_info2 = new rowstruct[rows + 1];
149 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
152 for (i = 0; i <= row; ++i) {
153 cell_info2[i] = cell_info[i];
154 row_info2[i] = row_info[i];
156 for (i = rows - 1; i >= row; --i) {
157 cell_info2[i + 1] = cell_info[i];
158 row_info2[i + 1] = row_info[i];
160 for (i = row; row_info[i].is_cont_row; --i);
161 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
162 row_info2[row + 1].is_cont_row = false;
163 row_info2[row + 1].top_line = row_info[i].top_line;
164 cell_info2[row + 1] = new cellstruct[columns];
165 for (i = 0; i < columns; ++i) {
166 cell_info2[row + 1][i].width_of_cell = 0;
167 cell_info2[row + 1][i] = cell_info2[row][i];
171 cell_info = cell_info2;
173 row_info = row_info2;
181 void LyXTable::DeleteRow(int cell)
183 int row = row_of_cell(cell);
184 while(!row_info[row].is_cont_row && RowHasContRow(cell))
185 DeleteRow(cell_info[row+1][0].cellno);
186 rowstruct * row_info2 = new rowstruct[rows - 1];
187 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
189 delete[] cell_info[row];
191 for (; i < row; ++i) {
192 cell_info2[i] = cell_info[i];
193 row_info2[i] = row_info[i];
195 if (row_info[i].is_cont_row)
196 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
197 for (i = row; i < rows - 1; ++i) {
198 cell_info2[i] = cell_info[i + 1];
199 row_info2[i] = row_info[i + 1];
203 cell_info = cell_info2;
205 row_info = row_info2;
213 void LyXTable::AppendColumn(int cell)
216 columnstruct * column_info2 = new columnstruct[columns + 1];
217 int column = right_column_of_cell(cell);
220 for (; i <= column; ++i) {
221 column_info2[i] = column_info[i];
223 for (i = columns - 1; i >= column; --i) {
224 column_info2[i + 1] = column_info[i];
227 delete[] column_info;
228 column_info = column_info2;
230 for (i = 0; i < rows; ++i) {
231 cellstruct * tmp = cell_info[i];
232 cell_info[i] = new cellstruct[columns + 1];
233 for (j = 0; j <= column; ++j) {
234 cell_info[i][j] = tmp[j];
236 for (j = column; j < columns; ++j) {
237 cell_info[i][j + 1] = tmp[j];
239 // care about multicolumns
240 if (cell_info[i][column + 1].multicolumn
241 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
242 cell_info[i][column + 1].multicolumn =
243 LyXTable::CELL_PART_OF_MULTICOLUMN;
245 if (column + 1 == columns
246 || cell_info[i][column + 2].multicolumn
247 != LyXTable::CELL_PART_OF_MULTICOLUMN){
248 cell_info[i][column + 1].multicolumn =
249 LyXTable::CELL_NORMAL;
259 void LyXTable::Reinit()
264 for (; i < rows; ++i) {
265 for (j = 0; j < columns; ++j) {
266 cell_info[i][j].width_of_cell = 0;
267 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
268 cell_info[i][j].has_cont_row = false;
272 for (i = 0; i < columns; ++i) {
273 calculate_width_of_column(i);
275 calculate_width_of_table();
277 set_row_column_number_info();
281 void LyXTable::set_row_column_number_info()
287 for (; row < rows; ++row) {
288 for (column = 0; column<columns; ++column) {
289 if (cell_info[row][column].multicolumn
290 != LyXTable::CELL_PART_OF_MULTICOLUMN)
292 cell_info[row][column].cellno = numberofcells;
295 ++numberofcells; // because this is one more than as we start from 0
300 rowofcell = new int[numberofcells];
301 delete [] columnofcell;
302 columnofcell = new int[numberofcells];
304 while (c < numberofcells && row < rows && column < columns) {
306 columnofcell[c] = column;
310 } while (column < columns &&
311 cell_info[row][column].multicolumn
312 == LyXTable::CELL_PART_OF_MULTICOLUMN);
313 if (column == columns) {
321 void LyXTable::DeleteColumn(int cell)
323 int column1 = column_of_cell(cell);
324 int column2 = right_column_of_cell(cell);
326 if (column1 == 0 && column2 == columns - 1)
329 for (int 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);
346 for (int i = 0; i < columns; ++i) {
347 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
354 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
356 return (right_column_of_cell(append_cell) ==
357 right_column_of_cell(question_cell));
361 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
363 if (column_of_cell(delete_column_cell) == 0 &&
364 right_column_of_cell(delete_column_cell) == columns - 1)
368 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
369 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
373 /* returns 1 if there is a topline, returns 0 if not */
374 bool LyXTable::TopLine(int cell)
376 int row = row_of_cell(cell);
379 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
380 if (IsMultiColumn(cell))
381 return cellinfo_of_cell(cell)->top_line;
382 return row_info[row].top_line;
386 bool LyXTable::BottomLine(int cell)
388 //no bottom line underneath non-existent cells if you please
389 if(cell >= numberofcells)
392 int row = row_of_cell(cell);
394 if (RowHasContRow(cell))
395 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
396 if (IsMultiColumn(cell))
397 return cellinfo_of_cell(cell)->bottom_line;
398 return row_info[row_of_cell(cell)].bottom_line;
402 bool LyXTable::LeftLine(int cell)
404 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);
446 int top = 1; // bool top = true; ??
447 int bottom = 1; // bool bottom = true; ??
450 for (column = 0; column < columns - 1 && bottom; ++column) {
451 switch (cell_info[row - 1][column].multicolumn) {
452 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
453 bottom = cell_info[row - 1][column].bottom_line;
455 case LyXTable::CELL_NORMAL:
456 bottom = row_info[row - 1].bottom_line;
459 for (column = 0; column < columns - 1 && top; ++column) {
460 switch (cell_info[row][column].multicolumn){
461 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
462 top = cell_info[row][column].top_line;
464 case LyXTable::CELL_NORMAL:
465 top = row_info[row].top_line;
469 return WIDTH_OF_LINE;
474 int LyXTable::AdditionalWidth(int cell)
476 // internally already set in SetWidthOfCell
477 // used to get it back in text.C
478 int col = right_column_of_cell(cell);
479 if (col < columns - 1 && column_info[col].right_line &&
480 column_info[col+1].left_line)
481 return WIDTH_OF_LINE;
487 // returns the maximum over all rows
488 int LyXTable::WidthOfColumn(int cell)
490 int column1 = column_of_cell(cell);
491 int column2 = right_column_of_cell(cell);
494 for (; i <= column2; ++i) {
495 result += column_info[i].width_of_column;
501 int LyXTable::WidthOfTable()
503 return width_of_table;
506 /* returns 1 if a complete update is necessary, otherwise 0 */
507 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
509 if (!IsMultiColumn(cell))
512 int row = row_of_cell(cell);
513 int column1 = column_of_cell(cell);
514 int column2 = right_column_of_cell(cell);
516 // first set columns to 0 so we can calculate the right width
518 for (; i <= column2; ++i) {
519 cell_info[row][i].width_of_cell = 0;
521 // set the width to MAX_WIDTH until width > 0
522 int 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;
534 void LyXTable::recalculateMulticolCells(int cell, int new_width)
536 int row = row_of_cell(cell);
537 int column1 = column_of_cell(cell);
538 int column2 = right_column_of_cell(cell);
540 // first set columns to 0 so we can calculate the right width
542 for (; 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);
552 /* returns 1 if a complete update is necessary, otherwise 0 */
553 bool LyXTable::SetWidthOfCell(int cell, int new_width)
555 int row = row_of_cell(cell);
556 int column1 = column_of_cell(cell);
560 if (IsMultiColumn(cell)) {
561 tmp = SetWidthOfMulticolCell(cell, new_width);
563 width = (new_width + 2*WIDTH_OF_LINE);
564 cell_info[row][column1].width_of_cell = width;
565 if (column_info[column1].right_line && (column1 < columns-1) &&
566 column_info[column1+1].left_line) // additional width
567 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
568 tmp = calculate_width_of_column_NMC(column1);
572 for(i = 0; i<columns;++i)
573 calculate_width_of_column_NMC(i);
574 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
577 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
578 for(i = 0; i<columns;++i)
579 calculate_width_of_column(i);
580 calculate_width_of_table();
587 bool LyXTable::SetAlignment(int cell, char align)
589 if (!IsMultiColumn(cell))
590 column_info[column_of_cell(cell)].alignment = align;
591 cellinfo_of_cell(cell)->alignment = align;
595 bool LyXTable::SetPWidth(int cell, string width)
597 int fvcell = FirstVirtualCell(cell);
599 if (IsMultiColumn(fvcell)) {
600 // if (column_info[column_of_cell(cell)].p_width.empty())
601 // column_info[column_of_cell(cell)].p_width = width;
602 cellinfo_of_cell(fvcell)->p_width = width;
604 column_info[column_of_cell(fvcell)].p_width = width;
605 if (!width.empty()) // do this only if there is a width
606 SetAlignment(cell, LYX_ALIGN_LEFT);
611 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
613 if (what == SET_SPECIAL_MULTI)
614 cellinfo_of_cell(cell)->align_special = special;
616 column_info[column_of_cell(cell)].align_special = special;
620 bool LyXTable::SetAllLines(int cell, bool line)
622 SetTopLine(cell, line);
623 SetBottomLine(cell, line);
624 SetRightLine(cell, line);
625 SetLeftLine(cell, line);
629 bool LyXTable::SetTopLine(int cell, bool line)
631 int row = row_of_cell(cell);
634 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
635 else if (!IsMultiColumn(cell))
636 row_info[row].top_line = line;
638 cellinfo_of_cell(cell)->top_line = line;
643 bool LyXTable::SetBottomLine(int cell, bool line)
645 int row = row_of_cell(cell);
647 if (RowHasContRow(cell))
648 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
649 else if (!IsMultiColumn(cell))
650 row_info[row_of_cell(cell)].bottom_line = line;
652 cellinfo_of_cell(cell)->bottom_line = line;
657 bool LyXTable::SetLeftLine(int cell, bool line)
659 column_info[column_of_cell(cell)].left_line = line;
664 bool LyXTable::SetRightLine(int cell, bool line)
666 column_info[right_column_of_cell(cell)].right_line = line;
671 char LyXTable::GetAlignment(int cell)
673 if (IsMultiColumn(cell))
674 return cellinfo_of_cell(cell)->alignment;
676 return column_info[column_of_cell(cell)].alignment;
679 string LyXTable::GetPWidth(int cell)
681 int fvcell = FirstVirtualCell(cell);
683 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
684 return cellinfo_of_cell(fvcell)->p_width;
685 return column_info[column_of_cell(fvcell)].p_width;
688 string LyXTable::GetAlignSpecial(int cell, int what)
690 if (what == SET_SPECIAL_MULTI)
691 return cellinfo_of_cell(cell)->align_special;
692 return column_info[column_of_cell(cell)].align_special;
695 int LyXTable::GetWidthOfCell(int cell)
697 int row = row_of_cell(cell);
698 int column1 = column_of_cell(cell);
699 int column2 = right_column_of_cell(cell);
702 for (; i <= column2; ++i) {
703 result += cell_info[row][i].width_of_cell;
706 result += AdditionalWidth(cell);
712 int LyXTable::GetBeginningOfTextInCell(int cell)
716 switch (GetAlignment(cell)){
717 case LYX_ALIGN_CENTER:
718 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
720 case LYX_ALIGN_RIGHT:
721 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
723 default: /* LYX_ALIGN_LEFT: nothing :-) */
733 bool LyXTable::IsFirstCell(int cell)
735 return (column_of_cell(cell) == 0);
738 bool LyXTable::IsLastCell(int cell)
740 return (right_column_of_cell(cell) == (columns - 1));
744 bool LyXTable::calculate_width_of_column(int column)
746 int old_column_width = column_info[column].width_of_column;
749 for (int i = 0; i < rows; ++i) {
750 maximum = max(cell_info[i][column].width_of_cell, maximum);
752 column_info[column].width_of_column = maximum;
753 return (column_info[column].width_of_column != old_column_width);
756 bool LyXTable::calculate_width_of_column_NMC(int column)
758 int old_column_width = column_info[column].width_of_column;
760 for (int 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(ostream & os, int cell)
965 if (ShouldBeVeryLastCell(cell)) {
966 // the very end at the very beginning
967 if (Linebreaks(cell))
968 os << "\\smallskip{}}";
969 if (IsMultiColumn(cell))
971 if (RotateCell(cell)) {
972 os << "\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 << column_of_cell(fcell + i) + 1
994 << right_column_of_cell(fcell + i) + 1
1005 os << "\\end{longtable}";
1007 os << "\\end{tabular}";
1009 os << "\n\\end{sideways}";
1013 nvcell = NextVirtualCell(cell + 1);
1017 os << "\\begin{sideways}\n";
1021 os << "\\begin{longtable}{";
1023 os << "\\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 os << column_info[i].align_special;
1029 } else if (!column_info[i].p_width.empty()) {
1031 << 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 << column_of_cell(fcell + i) + 1
1067 << right_column_of_cell(fcell + i) + 1
1078 if (RotateCell(0)) {
1079 os << "\\begin{sideways}\n";
1084 if (Linebreaks(cell))
1085 os << "\\smallskip{}}";
1086 if (IsMultiColumn(cell)){
1089 if (RotateCell(cell)) {
1090 os << "\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 flag1 = IsLongTable() &&
1098 ((row == endhead) || (row == endfirsthead) ||
1099 (row == endfoot) || (row == endlastfoot));
1101 bool flag2 = IsLongTable() &&
1102 ((row <= endhead) || (row <= endfirsthead) ||
1103 (row <= endfoot) || (row <= endlastfoot));
1105 // print the bottom hline only if (otherwise it is doubled):
1106 // - is no LongTable
1107 // - there IS a first-header
1108 // - the next row is no special header/footer
1109 // & this row is no special header/footer
1110 // - the next row is a special header/footer
1111 // & this row is a special header/footer
1112 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1113 (endfirsthead == endhead);
1118 while (!IsFirstCell(fcell))
1120 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1121 if (BottomLine(fcell + i))
1124 if (tmp == NumberOfCellsInRow(cell)){
1126 hline1 = "\\hline ";
1129 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1130 if (BottomLine(fcell + i)){
1132 << column_of_cell(fcell + i) + 1
1134 << right_column_of_cell(fcell + i) + 1
1136 hline1 += "\\cline{";
1137 hline1 += tostr(column_of_cell(fcell + i) + 1);
1139 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1149 if (IsLongTable() && (row == endfoot)) {
1150 os << "\\endfoot\n";
1152 print_hline = false; // no double line below footer
1154 if (IsLongTable() && (row == endlastfoot)) {
1155 os << "\\endlastfoot\n";
1157 print_hline = false; // no double line below footer
1159 if (IsLongTable() && row_info[row].newpage) {
1160 os << "\\newpage\n";
1162 print_hline = false; // no line below a \\newpage-command
1165 if (nvcell < numberofcells
1166 && (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 ";
1179 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1180 if (TopLine(fcell + i)) {
1183 << column_of_cell(fcell + i) + 1
1185 << right_column_of_cell(fcell + i) + 1
1188 hline2 += "\\cline{";
1189 hline2 += tostr(column_of_cell(fcell+i)+1);
1191 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1197 if (tmp && print_hline){
1202 // the order here is important as if one defines two
1203 // or more things in one line only the first entry is
1204 // displayed the other are set to an empty-row. This
1205 // is important if I have a footer and want that the
1206 // lastfooter is NOT displayed!!!
1207 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1208 (row == endfoot) || (row == endlastfoot);
1210 // sflag2 = IsLongTable() && (row >= 0) &&
1211 // (sflag2 || (row == endhead) || (row == endfirsthead));
1213 bool sflag1 = IsLongTable() && (row != endhead) &&
1214 (row != endfirsthead) &&
1215 ((row == endfoot) || (row == endlastfoot));
1217 if (IsLongTable() && (row == endhead)) {
1218 os << "\\endhead\n";
1221 if (IsLongTable() && (row == endfirsthead)) {
1222 os << "\\endfirsthead\n";
1225 if (sflag1) { // add the \hline for next foot row
1226 if (!hline1.empty()) {
1227 os << hline1 + '\n';
1231 // add the \hline for the first row
1232 if (pr_top_hline && sflag2) {
1233 if (!hline2.empty()) {
1234 os << hline2 + '\n';
1238 if (nvcell < numberofcells && RotateCell(nvcell)) {
1239 os << "\\begin{sideways}\n";
1245 if (nvcell < numberofcells && RotateCell(nvcell)) {
1246 os << "\\begin{sideways}\n";
1251 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1252 os << "\\multicolumn{"
1253 << cells_in_multicolumn(nvcell)
1255 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1256 os << cellinfo_of_cell(cell+1)->align_special
1259 if (LeftLine(nvcell))
1261 if (!GetPWidth(nvcell).empty()) {
1263 << GetPWidth(nvcell)
1266 switch (GetAlignment(nvcell)) {
1267 case LYX_ALIGN_LEFT: os << 'l'; break;
1268 case LYX_ALIGN_RIGHT: os << 'r'; break;
1269 default: os << 'c'; break;
1272 if (RightLine(nvcell))
1274 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1275 if (((nvcell + 1) < numberofcells) &&
1276 (NextVirtualCell(nvcell+1) < numberofcells) &&
1277 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1278 LeftLine(NextVirtualCell(nvcell+1)))
1284 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1285 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1287 << GetPWidth(nvcell)
1288 << "}{\\smallskip{}";
1296 // cell <0 will tex the preamble
1297 // returns the number of printed newlines
1298 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 (int j = 0; j < rows; ++j) {
1321 for (int 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)) {
1391 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1397 i = column_of_cell(cell);
1398 if (!isColumn && IsMultiColumn(cell)) {
1399 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1400 return cellinfo_of_cell(cell)->align_special.c_str();
1402 switch (GetAlignment(cell)) {
1403 case LYX_ALIGN_LEFT:
1405 case LYX_ALIGN_RIGHT:
1412 if (!column_info[i].align_special.empty()) {
1413 return column_info[i].align_special.c_str();
1415 #ifdef IGNORE_THIS_FOR_NOW
1416 else if (!column_info[i].p_width.empty()) {
1418 file += column_info[i].p_width;
1423 switch (column_info[i].alignment) {
1424 case LYX_ALIGN_LEFT:
1426 case LYX_ALIGN_RIGHT:
1436 // cell <0 will tex the preamble
1437 // returns the number of printed newlines
1438 int LyXTable::DocBookEndOfCell(ostream & os, int cell, int &depth)
1442 //int tmp; // tmp2; //Â unused
1443 int nvcell; // fcell; //Â unused
1444 if (ShouldBeVeryLastCell(cell)) {
1445 os << newlineAndDepth(--depth)
1447 << newlineAndDepth(--depth)
1449 << newlineAndDepth(--depth)
1451 << newlineAndDepth(--depth);
1456 << newlineAndDepth(--depth);
1459 nvcell = NextVirtualCell(cell + 1);
1468 << "' COLSEP='1' ROWSEP='1'>"
1469 << newlineAndDepth(++depth);
1471 for (i = 0; i < columns; ++i) {
1472 os << "<COLSPEC ALIGN='"
1473 << getDocBookAlign(i, true)
1479 if (i == (columns-1)) {
1482 if (column_info[i].right_line ||
1483 column_info[i+1].left_line)
1489 << newlineAndDepth(depth);
1491 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1492 if (column_info[i].left_line)
1497 << newlineAndDepth(++depth)
1499 << newlineAndDepth(++depth)
1501 << getDocBookAlign(0)
1503 if (IsMultiColumn(0)) {
1504 os << " NAMEST='col1' NAMEEND='col"
1505 << cells_in_multicolumn(0)
1509 << newlineAndDepth(++depth);
1512 if (IsLastCell(cell)) {
1513 os << newlineAndDepth(--depth)
1515 << newlineAndDepth(--depth)
1517 << newlineAndDepth(depth)
1519 << newlineAndDepth(++depth)
1521 << getDocBookAlign(cell + 1)
1522 << "' VALIGN='middle'";
1523 if (IsMultiColumn(cell + 1)) {
1524 os << " NAMEST='col"
1525 << column_of_cell(cell+1) + 1
1527 << column_of_cell(cell + 1) +
1528 cells_in_multicolumn(cell + 1)
1532 << newlineAndDepth(++depth);
1535 os << newlineAndDepth(--depth)
1537 << newlineAndDepth(depth)
1539 << getDocBookAlign(cell + 1)
1540 << "' VALIGN='middle'";
1541 if (IsMultiColumn(cell + 1)) {
1542 os << " NAMEST='col"
1543 << column_of_cell(cell+1) + 1
1545 << column_of_cell(cell+1) +
1546 cells_in_multicolumn(cell+1)
1550 << newlineAndDepth(++depth);
1559 bool LyXTable::IsMultiColumn(int cell)
1561 int fvcell = FirstVirtualCell(cell);
1563 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1567 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1569 int row = row_of_cell(cell);
1570 int column = column_of_cell(cell);
1571 return &cell_info[row][column];
1575 void LyXTable::SetMultiColumn(int cell, int number)
1577 int fvcell = FirstVirtualCell(cell);
1578 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1580 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1581 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1582 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1583 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1584 for (number--; number > 0; --number) {
1585 cellinfo_of_cell(fvcell+number)->multicolumn =
1586 LyXTable::CELL_PART_OF_MULTICOLUMN;
1587 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1589 set_row_column_number_info();
1590 SetWidthOfCell(fvcell, new_width);
1594 int LyXTable::cells_in_multicolumn(int cell)
1596 int row = row_of_cell(cell);
1597 int column = column_of_cell(cell);
1600 while (column < columns && cell_info[row][column].multicolumn
1601 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1609 int LyXTable::UnsetMultiColumn(int cell)
1611 int fvcell = FirstVirtualCell(cell);
1612 int row = row_of_cell(fvcell);
1613 int column = column_of_cell(fvcell);
1617 if (cell_info[row][column].multicolumn
1618 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1619 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1621 while (column < columns &&
1622 cell_info[row][column].multicolumn
1623 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1624 cell_info[row][column].multicolumn =
1625 LyXTable::CELL_NORMAL;
1630 set_row_column_number_info();
1635 void LyXTable::delete_column(int column)
1638 columnstruct * column_info2 = new columnstruct[columns-1];
1640 for (i = 0; i < column; ++i) {
1641 column_info2[i] = column_info[i];
1643 for (i = column; i < columns - 1; ++i) {
1644 column_info2[i] = column_info[i + 1];
1647 delete[] column_info;
1648 column_info = column_info2;
1650 for (i = 0; i < rows; ++i) {
1651 cellstruct * tmp = cell_info[i];
1652 cell_info[i] = new cellstruct[columns - 1];
1653 for (j = 0; j < column; ++j) {
1654 cell_info[i][j] = tmp[j];
1656 for (j = column; j < columns - 1; ++j) {
1657 cell_info[i][j] = tmp[j + 1];
1667 void LyXTable::SetLongTable(int what)
1669 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;