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
25 #include "support/lstrings.h"
26 #include "support/lyxmanip.h"
33 static int const WIDTH_OF_LINE = 5;
37 LyXTable::LyXTable(int rows_arg, int columns_arg)
39 Init(rows_arg, columns_arg);
43 LyXTable::LyXTable(LyXLex & lex)
45 istream & is = lex.getStream();
50 LyXTable::~LyXTable() {
52 delete[] columnofcell;
55 for (int i = 0; i < rows; ++i) {
56 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
62 LyXTable * LyXTable::Clone()
64 LyXTable * result = new LyXTable(rows, columns);
67 for (row = 0; row < rows; ++row) {
68 for (column = 0; column < columns; ++column) {
69 result->cell_info[row][column] = cell_info[row][column];
73 for (row = 0; row < rows; ++row) {
74 result->row_info[row] = row_info[row];
77 for (column = 0; column < columns; ++column) {
78 result->column_info[column].left_line = column_info[column].left_line;
79 result->column_info[column].right_line = column_info[column].right_line;
80 result->column_info[column].alignment = column_info[column].alignment;
81 result->column_info[column].p_width = column_info[column].p_width;
82 result->column_info[column].align_special = column_info[column].align_special;
85 result->SetLongTable(is_long_table);
86 result->rotate = rotate;
92 /* activates all lines and sets all widths to 0 */
93 void LyXTable::Init(int rows_arg, int columns_arg)
97 columns = columns_arg;
98 column_info = new columnstruct[columns];
99 row_info = new rowstruct[rows];
100 cell_info = new cellstruct*[rows];
103 for (i = 0; i < rows; ++i) {
104 cell_info[i] = new cellstruct[columns];
105 row_info[i].top_line = true;
106 row_info[i].bottom_line = false;
107 row_info[i].is_cont_row = false;
108 row_info[i].newpage = false;
109 for (j = 0; j < columns; ++j) {
110 cell_info[i][j].cellno = cellno++;
111 cell_info[i][j].width_of_cell = 0;
112 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
113 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
114 cell_info[i][j].top_line = row_info[i].top_line;
115 cell_info[i][j].bottom_line = row_info[i].bottom_line;
116 cell_info[i][j].has_cont_row = false;
117 cell_info[i][j].rotate = false;
118 cell_info[i][j].linebreaks = false;
121 row_info[i-1].bottom_line = true;
122 row_info[0].bottom_line = true;
124 for (i = 0; i < columns; ++i) {
125 column_info[i].left_line = true;
126 column_info[i].right_line = false;
127 column_info[i].alignment = LYX_ALIGN_CENTER;
128 // set width_of_column to zero before it is used in
129 // calculate_width_of_column() (thornley)
130 column_info[i].width_of_column = 0;
131 calculate_width_of_column(i);
133 column_info[i-1].right_line = true;
135 calculate_width_of_table();
139 set_row_column_number_info();
140 is_long_table = false;
149 void LyXTable::AppendRow(int cell)
151 int row = row_of_cell(cell);
152 rowstruct * row_info2 = new rowstruct[rows + 1];
153 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
156 for (i = 0; i <= row; ++i) {
157 cell_info2[i] = cell_info[i];
158 row_info2[i] = row_info[i];
160 for (i = rows - 1; i >= row; --i) {
161 cell_info2[i + 1] = cell_info[i];
162 row_info2[i + 1] = row_info[i];
164 for (i = row; row_info[i].is_cont_row; --i);
165 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
166 row_info2[row + 1].is_cont_row = false;
167 row_info2[row + 1].top_line = row_info[i].top_line;
168 cell_info2[row + 1] = new cellstruct[columns];
169 for (i = 0; i < columns; ++i) {
170 cell_info2[row + 1][i].width_of_cell = 0;
171 cell_info2[row + 1][i] = cell_info2[row][i];
175 cell_info = cell_info2;
177 row_info = row_info2;
185 void LyXTable::DeleteRow(int cell)
187 int row = row_of_cell(cell);
188 while(!row_info[row].is_cont_row && RowHasContRow(cell))
189 DeleteRow(cell_info[row+1][0].cellno);
190 rowstruct * row_info2 = new rowstruct[rows - 1];
191 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
193 delete[] cell_info[row];
195 for (; i < row; ++i) {
196 cell_info2[i] = cell_info[i];
197 row_info2[i] = row_info[i];
199 if (row_info[i].is_cont_row)
200 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
201 for (i = row; i < rows - 1; ++i) {
202 cell_info2[i] = cell_info[i + 1];
203 row_info2[i] = row_info[i + 1];
207 cell_info = cell_info2;
209 row_info = row_info2;
217 void LyXTable::AppendColumn(int cell)
220 columnstruct * column_info2 = new columnstruct[columns + 1];
221 int column = right_column_of_cell(cell);
224 for (; i <= column; ++i) {
225 column_info2[i] = column_info[i];
227 for (i = columns - 1; i >= column; --i) {
228 column_info2[i + 1] = column_info[i];
231 delete[] column_info;
232 column_info = column_info2;
234 for (i = 0; i < rows; ++i) {
235 cellstruct * tmp = cell_info[i];
236 cell_info[i] = new cellstruct[columns + 1];
237 for (j = 0; j <= column; ++j) {
238 cell_info[i][j] = tmp[j];
240 for (j = column; j < columns; ++j) {
241 cell_info[i][j + 1] = tmp[j];
243 // care about multicolumns
244 if (cell_info[i][column + 1].multicolumn
245 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
246 cell_info[i][column + 1].multicolumn =
247 LyXTable::CELL_PART_OF_MULTICOLUMN;
249 if (column + 1 == columns
250 || cell_info[i][column + 2].multicolumn
251 != LyXTable::CELL_PART_OF_MULTICOLUMN){
252 cell_info[i][column + 1].multicolumn =
253 LyXTable::CELL_NORMAL;
263 void LyXTable::Reinit()
268 for (; i < rows; ++i) {
269 for (j = 0; j < columns; ++j) {
270 cell_info[i][j].width_of_cell = 0;
271 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
272 cell_info[i][j].has_cont_row = false;
276 for (i = 0; i < columns; ++i) {
277 calculate_width_of_column(i);
279 calculate_width_of_table();
281 set_row_column_number_info();
285 void LyXTable::set_row_column_number_info()
291 for (; row < rows; ++row) {
292 for (column = 0; column<columns; ++column) {
293 if (cell_info[row][column].multicolumn
294 != LyXTable::CELL_PART_OF_MULTICOLUMN)
296 cell_info[row][column].cellno = numberofcells;
299 ++numberofcells; // because this is one more than as we start from 0
304 rowofcell = new int[numberofcells];
305 delete [] columnofcell;
306 columnofcell = new int[numberofcells];
308 while (c < numberofcells && row < rows && column < columns) {
310 columnofcell[c] = column;
314 } while (column < columns &&
315 cell_info[row][column].multicolumn
316 == LyXTable::CELL_PART_OF_MULTICOLUMN);
317 if (column == columns) {
325 void LyXTable::DeleteColumn(int cell)
327 int column1 = column_of_cell(cell);
328 int column2 = right_column_of_cell(cell);
330 if (column1 == 0 && column2 == columns - 1)
333 for (int column = column1; column <= column2; ++column) {
334 delete_column(column1);
340 int LyXTable::GetNumberOfCells()
342 return numberofcells;
346 int LyXTable::NumberOfCellsInRow(int cell)
348 int row = row_of_cell(cell);
350 for (int i = 0; i < columns; ++i) {
351 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
358 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
360 return (right_column_of_cell(append_cell) ==
361 right_column_of_cell(question_cell));
365 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
367 if (column_of_cell(delete_column_cell) == 0 &&
368 right_column_of_cell(delete_column_cell) == columns - 1)
372 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
373 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
377 /* returns 1 if there is a topline, returns 0 if not */
378 bool LyXTable::TopLine(int cell)
380 int row = row_of_cell(cell);
383 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
384 if (IsMultiColumn(cell))
385 return cellinfo_of_cell(cell)->top_line;
386 return row_info[row].top_line;
390 bool LyXTable::BottomLine(int cell)
392 //no bottom line underneath non-existent cells if you please
393 if(cell >= numberofcells)
396 int row = row_of_cell(cell);
398 if (RowHasContRow(cell))
399 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
400 if (IsMultiColumn(cell))
401 return cellinfo_of_cell(cell)->bottom_line;
402 return row_info[row_of_cell(cell)].bottom_line;
406 bool LyXTable::LeftLine(int cell)
408 return column_info[column_of_cell(cell)].left_line;
412 bool LyXTable::RightLine(int cell)
414 return column_info[right_column_of_cell(cell)].right_line;
418 bool LyXTable::TopAlreadyDrawed(int cell)
420 if (AdditionalHeight(cell))
422 int row = row_of_cell(cell);
424 int column = column_of_cell(cell);
426 && cell_info[row-1][column].multicolumn
427 == LyXTable::CELL_PART_OF_MULTICOLUMN)
429 if (cell_info[row-1][column].multicolumn
430 == LyXTable::CELL_NORMAL)
431 return row_info[row-1].bottom_line;
433 return cell_info[row-1][column].bottom_line;
439 bool LyXTable::VeryLastRow(int cell)
441 return (row_of_cell(cell) == rows - 1);
445 int LyXTable::AdditionalHeight(int cell)
447 int row = row_of_cell(cell);
450 int top = 1; // bool top = true; ??
451 int bottom = 1; // bool bottom = true; ??
454 for (column = 0; column < columns - 1 && bottom; ++column) {
455 switch (cell_info[row - 1][column].multicolumn) {
456 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
457 bottom = cell_info[row - 1][column].bottom_line;
459 case LyXTable::CELL_NORMAL:
460 bottom = row_info[row - 1].bottom_line;
463 for (column = 0; column < columns - 1 && top; ++column) {
464 switch (cell_info[row][column].multicolumn){
465 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
466 top = cell_info[row][column].top_line;
468 case LyXTable::CELL_NORMAL:
469 top = row_info[row].top_line;
473 return WIDTH_OF_LINE;
478 int LyXTable::AdditionalWidth(int cell)
480 // internally already set in SetWidthOfCell
481 // used to get it back in text.C
482 int col = right_column_of_cell(cell);
483 if (col < columns - 1 && column_info[col].right_line &&
484 column_info[col+1].left_line)
485 return WIDTH_OF_LINE;
491 // returns the maximum over all rows
492 int LyXTable::WidthOfColumn(int cell)
494 int column1 = column_of_cell(cell);
495 int column2 = right_column_of_cell(cell);
498 for (; i <= column2; ++i) {
499 result += column_info[i].width_of_column;
505 int LyXTable::WidthOfTable()
507 return width_of_table;
510 /* returns 1 if a complete update is necessary, otherwise 0 */
511 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
513 if (!IsMultiColumn(cell))
516 int row = row_of_cell(cell);
517 int column1 = column_of_cell(cell);
518 int column2 = right_column_of_cell(cell);
520 // first set columns to 0 so we can calculate the right width
522 for (; i <= column2; ++i) {
523 cell_info[row][i].width_of_cell = 0;
525 // set the width to MAX_WIDTH until width > 0
526 int width = (new_width + 2 * WIDTH_OF_LINE);
527 for (i = column1; (i < column2) && (width > 0); ++i) {
528 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
529 width -= column_info[i].width_of_column;
532 cell_info[row][i].width_of_cell = width;
538 void LyXTable::recalculateMulticolCells(int cell, int new_width)
540 int row = row_of_cell(cell);
541 int column1 = column_of_cell(cell);
542 int column2 = right_column_of_cell(cell);
544 // first set columns to 0 so we can calculate the right width
546 for (; i <= column2; ++i)
547 cell_info[row][i].width_of_cell = 0;
548 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
550 if (i < numberofcells)
551 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
552 SetWidthOfMulticolCell(cell, new_width);
556 /* returns 1 if a complete update is necessary, otherwise 0 */
557 bool LyXTable::SetWidthOfCell(int cell, int new_width)
559 int row = row_of_cell(cell);
560 int column1 = column_of_cell(cell);
564 if (IsMultiColumn(cell)) {
565 tmp = SetWidthOfMulticolCell(cell, new_width);
567 width = (new_width + 2*WIDTH_OF_LINE);
568 cell_info[row][column1].width_of_cell = width;
569 if (column_info[column1].right_line && (column1 < columns-1) &&
570 column_info[column1+1].left_line) // additional width
571 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
572 tmp = calculate_width_of_column_NMC(column1);
576 for(i = 0; i<columns;++i)
577 calculate_width_of_column_NMC(i);
578 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
581 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
582 for(i = 0; i<columns;++i)
583 calculate_width_of_column(i);
584 calculate_width_of_table();
591 bool LyXTable::SetAlignment(int cell, char align)
593 if (!IsMultiColumn(cell))
594 column_info[column_of_cell(cell)].alignment = align;
595 cellinfo_of_cell(cell)->alignment = align;
599 bool LyXTable::SetPWidth(int cell, string width)
601 int fvcell = FirstVirtualCell(cell);
603 if (IsMultiColumn(fvcell)) {
604 // if (column_info[column_of_cell(cell)].p_width.empty())
605 // column_info[column_of_cell(cell)].p_width = width;
606 cellinfo_of_cell(fvcell)->p_width = width;
608 column_info[column_of_cell(fvcell)].p_width = width;
609 if (!width.empty()) // do this only if there is a width
610 SetAlignment(cell, LYX_ALIGN_LEFT);
615 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
617 if (what == SET_SPECIAL_MULTI)
618 cellinfo_of_cell(cell)->align_special = special;
620 column_info[column_of_cell(cell)].align_special = special;
624 bool LyXTable::SetAllLines(int cell, bool line)
626 SetTopLine(cell, line);
627 SetBottomLine(cell, line);
628 SetRightLine(cell, line);
629 SetLeftLine(cell, line);
633 bool LyXTable::SetTopLine(int cell, bool line)
635 int row = row_of_cell(cell);
638 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
639 else if (!IsMultiColumn(cell))
640 row_info[row].top_line = line;
642 cellinfo_of_cell(cell)->top_line = line;
647 bool LyXTable::SetBottomLine(int cell, bool line)
649 int row = row_of_cell(cell);
651 if (RowHasContRow(cell))
652 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
653 else if (!IsMultiColumn(cell))
654 row_info[row_of_cell(cell)].bottom_line = line;
656 cellinfo_of_cell(cell)->bottom_line = line;
661 bool LyXTable::SetLeftLine(int cell, bool line)
663 column_info[column_of_cell(cell)].left_line = line;
668 bool LyXTable::SetRightLine(int cell, bool line)
670 column_info[right_column_of_cell(cell)].right_line = line;
675 char LyXTable::GetAlignment(int cell)
677 if (IsMultiColumn(cell))
678 return cellinfo_of_cell(cell)->alignment;
680 return column_info[column_of_cell(cell)].alignment;
683 string LyXTable::GetPWidth(int cell)
685 int fvcell = FirstVirtualCell(cell);
687 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
688 return cellinfo_of_cell(fvcell)->p_width;
689 return column_info[column_of_cell(fvcell)].p_width;
692 string LyXTable::GetAlignSpecial(int cell, int what)
694 if (what == SET_SPECIAL_MULTI)
695 return cellinfo_of_cell(cell)->align_special;
696 return column_info[column_of_cell(cell)].align_special;
699 int LyXTable::GetWidthOfCell(int cell)
701 int row = row_of_cell(cell);
702 int column1 = column_of_cell(cell);
703 int column2 = right_column_of_cell(cell);
706 for (; i <= column2; ++i) {
707 result += cell_info[row][i].width_of_cell;
710 result += AdditionalWidth(cell);
716 int LyXTable::GetBeginningOfTextInCell(int cell)
720 switch (GetAlignment(cell)){
721 case LYX_ALIGN_CENTER:
722 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
724 case LYX_ALIGN_RIGHT:
725 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
727 default: /* LYX_ALIGN_LEFT: nothing :-) */
737 bool LyXTable::IsFirstCell(int cell)
739 return (column_of_cell(cell) == 0);
742 bool LyXTable::IsLastCell(int cell)
744 return (right_column_of_cell(cell) == (columns - 1));
748 bool LyXTable::calculate_width_of_column(int column)
750 int old_column_width = column_info[column].width_of_column;
753 for (int i = 0; i < rows; ++i) {
754 maximum = max(cell_info[i][column].width_of_cell, maximum);
756 column_info[column].width_of_column = maximum;
757 return (column_info[column].width_of_column != old_column_width);
760 bool LyXTable::calculate_width_of_column_NMC(int column)
762 int old_column_width = column_info[column].width_of_column;
764 for (int i = 0; i < rows; ++i) {
765 if (!IsMultiColumn(GetCellNumber(column, i)) &&
766 (cell_info[i][column].width_of_cell > max)) {
767 max = cell_info[i][column].width_of_cell;
770 column_info[column].width_of_column = max;
771 return (column_info[column].width_of_column != old_column_width);
774 void LyXTable::calculate_width_of_table()
777 for (int i = 0; i < columns; ++i) {
778 width_of_table += column_info[i].width_of_column;
783 int LyXTable::row_of_cell(int cell)
785 if (cell >= numberofcells)
789 return rowofcell[cell];
793 int LyXTable::column_of_cell(int cell)
795 if (cell >= numberofcells)
799 return columnofcell[cell];
803 int LyXTable::right_column_of_cell(int cell)
805 int row = row_of_cell(cell);
806 int column = column_of_cell(cell);
807 while (column < columns - 1 &&
808 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
814 void LyXTable::Write(ostream & os)
818 << rows << " " << columns << " " << is_long_table << " "
819 << rotate << " " << endhead << " " << endfirsthead << " "
820 << endfoot << " " << endlastfoot << "\n";
821 for (i = 0; i < rows; ++i) {
822 os << row_info[i].top_line << " "
823 << row_info[i].bottom_line << " "
824 << row_info[i].is_cont_row << " "
825 << row_info[i].newpage << "\n";
827 for (i = 0; i < columns; ++i) {
828 os << column_info[i].alignment << " "
829 << column_info[i].left_line << " "
830 << column_info[i].right_line << " \""
831 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
832 << column_info[i].align_special << "\"\n";
835 for (i = 0; i < rows; ++i) {
836 for (j = 0; j < columns; ++j) {
837 os << cell_info[i][j].multicolumn << " "
838 << cell_info[i][j].alignment << " "
839 << cell_info[i][j].top_line << " "
840 << cell_info[i][j].bottom_line << " "
841 << cell_info[i][j].has_cont_row << " "
842 << cell_info[i][j].rotate << " "
843 << cell_info[i][j].linebreaks << " \""
844 << cell_info[i][j].align_special << "\" \""
845 << cell_info[i][j].p_width << "\"\n";
851 void LyXTable::Read(istream & is)
857 int is_long_table_arg = false;
858 int rotate_arg = false;
871 version = atoi(s.c_str() + 8);
875 #warning Insert a error message window here that this format is not supported anymore
878 lyxerr << "Tabular format < 5 is not supported anymore\n"
879 "Get an older version of LyX (< 1.1.x) for conversion!"
882 is >> rows_arg >> columns_arg >> is_long_table_arg
883 >> rotate_arg >> a >> b >> c >> d;
885 is >> rows_arg >> columns_arg;
886 Init(rows_arg, columns_arg);
887 SetLongTable(is_long_table_arg);
888 SetRotateTable(rotate_arg);
890 for (i = 0; i < rows; ++i) {
893 for (i = 0; i < columns; ++i) {
896 for (i = 0; i < rows; ++i) {
897 for (j = 0; j < columns; ++j) {
901 set_row_column_number_info();
904 is >> rows_arg >> columns_arg >> is_long_table_arg
905 >> rotate_arg >> a >> b >> c >> d;
906 Init(rows_arg, columns_arg);
907 SetLongTable(is_long_table_arg);
908 SetRotateTable(rotate_arg);
913 for (i = 0; i < rows; ++i) {
914 a = b = c = d = e = f = g = h = 0;
915 is >> a >> b >> c >> d;
916 row_info[i].top_line = a;
917 row_info[i].bottom_line = b;
918 row_info[i].is_cont_row = c;
919 row_info[i].newpage = d;
921 for (i = 0; i < columns; ++i) {
927 getline(is, s1, '"');
928 is >> ch; // skip '"'
929 getline(is, s2, '"');
930 column_info[i].alignment = static_cast<char>(a);
931 column_info[i].left_line = b;
932 column_info[i].right_line = c;
933 column_info[i].p_width = s1;
934 column_info[i].align_special = s2;
936 for (i = 0; i < rows; ++i) {
937 for (j = 0; j < columns; ++j) {
940 is >> a >> b >> c >> d >> e >> f >> g;
942 is >> ch; // skip '"'
943 getline(is, s1, '"');
944 is >> ch; // skip '"'
945 getline(is, s2, '"');
946 cell_info[i][j].multicolumn = static_cast<char>(a);
947 cell_info[i][j].alignment = static_cast<char>(b);
948 cell_info[i][j].top_line = static_cast<char>(c);
949 cell_info[i][j].bottom_line = static_cast<char>(d);
950 cell_info[i][j].has_cont_row = static_cast<bool>(e);
951 cell_info[i][j].rotate = static_cast<bool>(f);
952 cell_info[i][j].linebreaks = static_cast<bool>(g);
953 cell_info[i][j].align_special = s1;
954 cell_info[i][j].p_width = s2;
957 set_row_column_number_info();
961 // cell <0 will tex the preamble
962 // returns the number of printed newlines
963 int LyXTable::TexEndOfCell(ostream & os, int cell)
969 if (ShouldBeVeryLastCell(cell)) {
970 // the very end at the very beginning
971 if (Linebreaks(cell))
972 os << "\\smallskip{}}";
973 if (IsMultiColumn(cell))
975 if (RotateCell(cell)) {
976 os << "\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 << column_of_cell(fcell + i) + 1
998 << right_column_of_cell(fcell + i) + 1
1009 os << "\\end{longtable}";
1011 os << "\\end{tabular}";
1013 os << "\n\\end{sideways}";
1017 nvcell = NextVirtualCell(cell + 1);
1021 os << "\\begin{sideways}\n";
1025 os << "\\begin{longtable}{";
1027 os << "\\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 os << column_info[i].align_special;
1033 } else if (!column_info[i].p_width.empty()) {
1035 << 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 << column_of_cell(fcell + i) + 1
1071 << right_column_of_cell(fcell + i) + 1
1082 if (RotateCell(0)) {
1083 os << "\\begin{sideways}\n";
1088 if (Linebreaks(cell))
1089 os << "\\smallskip{}}";
1090 if (IsMultiColumn(cell)){
1093 if (RotateCell(cell)) {
1094 os << "\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 flag1 = IsLongTable() &&
1102 ((row == endhead) || (row == endfirsthead) ||
1103 (row == endfoot) || (row == endlastfoot));
1105 bool flag2 = IsLongTable() &&
1106 ((row <= endhead) || (row <= endfirsthead) ||
1107 (row <= endfoot) || (row <= endlastfoot));
1109 // print the bottom hline only if (otherwise it is doubled):
1110 // - is no LongTable
1111 // - there IS a first-header
1112 // - the next row is no special header/footer
1113 // & this row is no special header/footer
1114 // - the next row is a special header/footer
1115 // & this row is a special header/footer
1116 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1117 (endfirsthead == endhead);
1122 while (!IsFirstCell(fcell))
1124 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1125 if (BottomLine(fcell + i))
1128 if (tmp == NumberOfCellsInRow(cell)){
1130 hline1 = "\\hline ";
1133 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1134 if (BottomLine(fcell + i)){
1136 << column_of_cell(fcell + i) + 1
1138 << right_column_of_cell(fcell + i) + 1
1140 hline1 += "\\cline{";
1141 hline1 += tostr(column_of_cell(fcell + i) + 1);
1143 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1153 if (IsLongTable() && (row == endfoot)) {
1154 os << "\\endfoot\n";
1156 print_hline = false; // no double line below footer
1158 if (IsLongTable() && (row == endlastfoot)) {
1159 os << "\\endlastfoot\n";
1161 print_hline = false; // no double line below footer
1163 if (IsLongTable() && row_info[row].newpage) {
1164 os << "\\newpage\n";
1166 print_hline = false; // no line below a \\newpage-command
1169 if (nvcell < numberofcells
1170 && (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 ";
1183 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1184 if (TopLine(fcell + i)) {
1187 << column_of_cell(fcell + i) + 1
1189 << right_column_of_cell(fcell + i) + 1
1192 hline2 += "\\cline{";
1193 hline2 += tostr(column_of_cell(fcell+i)+1);
1195 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1201 if (tmp && print_hline){
1206 // the order here is important as if one defines two
1207 // or more things in one line only the first entry is
1208 // displayed the other are set to an empty-row. This
1209 // is important if I have a footer and want that the
1210 // lastfooter is NOT displayed!!!
1211 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1212 (row == endfoot) || (row == endlastfoot);
1214 // sflag2 = IsLongTable() && (row >= 0) &&
1215 // (sflag2 || (row == endhead) || (row == endfirsthead));
1217 bool sflag1 = IsLongTable() && (row != endhead) &&
1218 (row != endfirsthead) &&
1219 ((row == endfoot) || (row == endlastfoot));
1221 if (IsLongTable() && (row == endhead)) {
1222 os << "\\endhead\n";
1225 if (IsLongTable() && (row == endfirsthead)) {
1226 os << "\\endfirsthead\n";
1229 if (sflag1) { // add the \hline for next foot row
1230 if (!hline1.empty()) {
1231 os << hline1 + '\n';
1235 // add the \hline for the first row
1236 if (pr_top_hline && sflag2) {
1237 if (!hline2.empty()) {
1238 os << hline2 + '\n';
1242 if (nvcell < numberofcells && RotateCell(nvcell)) {
1243 os << "\\begin{sideways}\n";
1249 if (nvcell < numberofcells && RotateCell(nvcell)) {
1250 os << "\\begin{sideways}\n";
1255 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1256 os << "\\multicolumn{"
1257 << cells_in_multicolumn(nvcell)
1259 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1260 os << cellinfo_of_cell(cell+1)->align_special
1263 if (LeftLine(nvcell))
1265 if (!GetPWidth(nvcell).empty()) {
1267 << GetPWidth(nvcell)
1270 switch (GetAlignment(nvcell)) {
1271 case LYX_ALIGN_LEFT: os << 'l'; break;
1272 case LYX_ALIGN_RIGHT: os << 'r'; break;
1273 default: os << 'c'; break;
1276 if (RightLine(nvcell))
1278 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1279 if (((nvcell + 1) < numberofcells) &&
1280 (NextVirtualCell(nvcell+1) < numberofcells) &&
1281 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1282 LeftLine(NextVirtualCell(nvcell+1)))
1288 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1289 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1291 << GetPWidth(nvcell)
1292 << "}{\\smallskip{}";
1300 // cell <0 will tex the preamble
1301 // returns the number of printed newlines
1302 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 (int j = 0; j < rows; ++j) {
1325 for (int 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)) {
1395 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1401 i = column_of_cell(cell);
1402 if (!isColumn && IsMultiColumn(cell)) {
1403 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1404 return cellinfo_of_cell(cell)->align_special.c_str();
1406 switch (GetAlignment(cell)) {
1407 case LYX_ALIGN_LEFT:
1409 case LYX_ALIGN_RIGHT:
1416 if (!column_info[i].align_special.empty()) {
1417 return column_info[i].align_special.c_str();
1419 #ifdef IGNORE_THIS_FOR_NOW
1420 else if (!column_info[i].p_width.empty()) {
1422 file += column_info[i].p_width;
1427 switch (column_info[i].alignment) {
1428 case LYX_ALIGN_LEFT:
1430 case LYX_ALIGN_RIGHT:
1440 // cell <0 will tex the preamble
1441 // returns the number of printed newlines
1442 int LyXTable::DocBookEndOfCell(ostream & os, int cell, int & depth)
1446 //int tmp; // tmp2; //Â unused
1447 int nvcell; // fcell; //Â unused
1448 if (ShouldBeVeryLastCell(cell)) {
1449 os << newlineAndDepth(--depth)
1451 << newlineAndDepth(--depth)
1453 << newlineAndDepth(--depth)
1455 << newlineAndDepth(--depth);
1460 << newlineAndDepth(--depth);
1463 nvcell = NextVirtualCell(cell + 1);
1472 << "' COLSEP='1' ROWSEP='1'>"
1473 << newlineAndDepth(++depth);
1475 for (i = 0; i < columns; ++i) {
1476 os << "<COLSPEC ALIGN='"
1477 << getDocBookAlign(i, true)
1483 if (i == (columns-1)) {
1486 if (column_info[i].right_line ||
1487 column_info[i+1].left_line)
1493 << newlineAndDepth(depth);
1495 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1496 if (column_info[i].left_line)
1501 << newlineAndDepth(++depth)
1503 << newlineAndDepth(++depth)
1505 << getDocBookAlign(0)
1507 if (IsMultiColumn(0)) {
1508 os << " NAMEST='col1' NAMEEND='col"
1509 << cells_in_multicolumn(0)
1513 << newlineAndDepth(++depth);
1516 if (IsLastCell(cell)) {
1517 os << newlineAndDepth(--depth)
1519 << newlineAndDepth(--depth)
1521 << newlineAndDepth(depth)
1523 << newlineAndDepth(++depth)
1525 << getDocBookAlign(cell + 1)
1526 << "' VALIGN='middle'";
1527 if (IsMultiColumn(cell + 1)) {
1528 os << " NAMEST='col"
1529 << column_of_cell(cell+1) + 1
1531 << column_of_cell(cell + 1) +
1532 cells_in_multicolumn(cell + 1)
1536 << newlineAndDepth(++depth);
1539 os << newlineAndDepth(--depth)
1541 << newlineAndDepth(depth)
1543 << getDocBookAlign(cell + 1)
1544 << "' VALIGN='middle'";
1545 if (IsMultiColumn(cell + 1)) {
1546 os << " NAMEST='col"
1547 << column_of_cell(cell+1) + 1
1549 << column_of_cell(cell+1) +
1550 cells_in_multicolumn(cell+1)
1554 << newlineAndDepth(++depth);
1563 bool LyXTable::IsMultiColumn(int cell)
1565 int fvcell = FirstVirtualCell(cell);
1567 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1571 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1573 int row = row_of_cell(cell);
1574 int column = column_of_cell(cell);
1575 return &cell_info[row][column];
1579 void LyXTable::SetMultiColumn(int cell, int number)
1581 int fvcell = FirstVirtualCell(cell);
1582 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1584 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1585 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1586 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1587 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1588 for (number--; number > 0; --number) {
1589 cellinfo_of_cell(fvcell+number)->multicolumn =
1590 LyXTable::CELL_PART_OF_MULTICOLUMN;
1591 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1593 set_row_column_number_info();
1594 SetWidthOfCell(fvcell, new_width);
1598 int LyXTable::cells_in_multicolumn(int cell)
1600 int row = row_of_cell(cell);
1601 int column = column_of_cell(cell);
1604 while (column < columns && cell_info[row][column].multicolumn
1605 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1613 int LyXTable::UnsetMultiColumn(int cell)
1615 int fvcell = FirstVirtualCell(cell);
1616 int row = row_of_cell(fvcell);
1617 int column = column_of_cell(fvcell);
1621 if (cell_info[row][column].multicolumn
1622 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1623 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1625 while (column < columns &&
1626 cell_info[row][column].multicolumn
1627 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1628 cell_info[row][column].multicolumn =
1629 LyXTable::CELL_NORMAL;
1634 set_row_column_number_info();
1639 void LyXTable::delete_column(int column)
1642 columnstruct * column_info2 = new columnstruct[columns-1];
1644 for (i = 0; i < column; ++i) {
1645 column_info2[i] = column_info[i];
1647 for (i = column; i < columns - 1; ++i) {
1648 column_info2[i] = column_info[i + 1];
1651 delete[] column_info;
1652 column_info = column_info2;
1654 for (i = 0; i < rows; ++i) {
1655 cellstruct * tmp = cell_info[i];
1656 cell_info[i] = new cellstruct[columns - 1];
1657 for (j = 0; j < column; ++j) {
1658 cell_info[i][j] = tmp[j];
1660 for (j = column; j < columns - 1; ++j) {
1661 cell_info[i][j] = tmp[j + 1];
1671 void LyXTable::SetLongTable(int what)
1673 is_long_table = what;
1677 bool LyXTable::IsLongTable()
1679 return is_long_table;
1682 void LyXTable::SetRotateTable(int what)
1687 bool LyXTable::RotateTable()
1692 void LyXTable::SetRotateCell(int cell, int what)
1694 cellinfo_of_cell(cell)->rotate = what;
1697 bool LyXTable::RotateCell(int cell)
1699 return cellinfo_of_cell(cell)->rotate;
1702 bool LyXTable::NeedRotating()
1706 for (int i = 0; i < rows; ++i) {
1707 for (int j = 0; j < columns; ++j) {
1708 if (cell_info[i][j].rotate)
1715 void LyXTable::AppendContRow(int cell)
1717 int row = row_of_cell(cell)+1;
1719 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1721 row_info[row].is_cont_row = true;
1722 row_info[row].top_line = false;
1723 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1727 bool LyXTable::IsContRow(int cell)
1729 return row_info[row_of_cell(cell)].is_cont_row;
1732 int LyXTable::CellHasContRow(int cell)
1734 int row = row_of_cell(cell);
1736 if (VeryLastRow(cell))
1738 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1739 return cell_info[row+1][column_of_cell(cell)].cellno;
1743 bool LyXTable::RowHasContRow(int cell)
1745 int row = row_of_cell(cell) + 1;
1748 return row_info[row].is_cont_row;
1752 int LyXTable::FirstVirtualCell(int cell)
1754 if (!IsContRow(cell))
1756 int row = row_of_cell(cell);
1757 int column = column_of_cell(cell);
1758 for(; (row > 0) && IsContRow(cell_info[row][column].cellno); --row)
1760 return cell_info[row][column].cellno;
1764 int LyXTable::NextVirtualCell(int cell)
1766 if (!IsContRow(cell))
1768 int row = row_of_cell(cell);
1769 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1771 // what if(row >= rows) ?
1772 return cell_info[row][0].cellno;
1776 bool LyXTable::ShouldBeVeryLastCell(int cell)
1777 // "very last cell" ..of what? the row? the table?
1778 // "Cell" in this context appears to not count `virtual' cells
1780 int fcell = cell + 1;
1782 if (cell == GetNumberOfCells() - 1)
1783 return true; // not really sure if I should return false here
1784 if (!IsContRow(fcell))
1786 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1788 if (fcell < GetNumberOfCells() - 1)
1793 bool LyXTable::ShouldBeVeryLastRow(int cell)
1795 if (CellHasContRow(cell)>= 0)
1797 int row = row_of_cell(cell) + 1;
1798 int column = column_of_cell(cell);
1799 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1802 return false; // found another valid row
1803 // I do not have any valid row after the actual
1807 int LyXTable::GetCellAbove(int cell)
1809 int row = row_of_cell(cell);
1812 return cell_info[row-1][column_of_cell(cell)].cellno;
1816 int LyXTable::GetCellNumber(int column, int row)
1818 if (column >= columns)
1819 column = columns - 1;
1820 else if (column < 0)
1827 return cell_info[row][column].cellno;
1830 void LyXTable::SetLinebreaks(int cell, bool what)
1832 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1835 bool LyXTable::Linebreaks(int cell)
1837 int fvcell = FirstVirtualCell(cell);
1839 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1840 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1842 return cellinfo_of_cell(fvcell)->linebreaks;
1845 void LyXTable::SetLTHead(int cell, bool first)
1847 int row = row_of_cell(cell);
1850 if (row == endfirsthead)
1862 bool LyXTable::RowOfLTHead(int cell)
1864 if ((endhead+1) > rows)
1866 return (row_of_cell(cell) == endhead);
1869 bool LyXTable::RowOfLTFirstHead(int cell)
1871 if ((endfirsthead+1) > rows)
1873 return (row_of_cell(cell) == endfirsthead);
1876 void LyXTable::SetLTFoot(int cell, bool last)
1878 int row = row_of_cell(cell);
1881 if (row == endlastfoot)
1893 bool LyXTable::RowOfLTFoot(int cell)
1895 if ((endfoot+1) > rows) {
1899 return (row_of_cell(cell) == endfoot);
1902 bool LyXTable::RowOfLTLastFoot(int cell)
1904 if ((endlastfoot+1) > rows)
1906 return (row_of_cell(cell) == endlastfoot);
1909 void LyXTable::SetLTNewPage(int cell, bool what)
1911 row_info[row_of_cell(cell)].newpage = what;
1914 bool LyXTable::LTNewPage(int cell)
1916 return row_info[row_of_cell(cell)].newpage;