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"
24 #pragma implementation
27 extern void addNewlineAndDepth(string & file, int const depth); // Jug 990923
29 static int const WIDTH_OF_LINE = 5;
32 LyXTable::LyXTable(int rows_arg, int columns_arg)
34 Init(rows_arg, columns_arg);
38 LyXTable::LyXTable(LyXLex & lex)
40 istream & is = lex.getStream();
45 LyXTable::~LyXTable() {
47 delete[] columnofcell;
50 for (int i = 0; i < rows; ++i) {
51 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
57 LyXTable * LyXTable::Clone()
59 LyXTable * result = new LyXTable(rows, columns);
62 for (row = 0; row < rows; ++row) {
63 for (column = 0; column < columns; ++column) {
64 result->cell_info[row][column] = cell_info[row][column];
68 for (row = 0; row < rows; ++row) {
69 result->row_info[row] = row_info[row];
72 for (column = 0; column < columns; ++column) {
73 result->column_info[column].left_line = column_info[column].left_line;
74 result->column_info[column].right_line = column_info[column].right_line;
75 result->column_info[column].alignment = column_info[column].alignment;
76 result->column_info[column].p_width = column_info[column].p_width;
77 result->column_info[column].align_special = column_info[column].align_special;
80 result->SetLongTable(is_long_table);
81 result->rotate = rotate;
87 /* activates all lines and sets all widths to 0 */
88 void LyXTable::Init(int rows_arg, int columns_arg)
92 columns = columns_arg;
93 column_info = new columnstruct[columns];
94 row_info = new rowstruct[rows];
95 cell_info = new cellstruct*[rows];
98 for (i = 0; i < rows; ++i) {
99 cell_info[i] = new cellstruct[columns];
100 row_info[i].top_line = true;
101 row_info[i].bottom_line = false;
102 row_info[i].is_cont_row = false;
103 row_info[i].newpage = false;
104 for (j = 0; j < columns; ++j) {
105 cell_info[i][j].cellno = cellno++;
106 cell_info[i][j].width_of_cell = 0;
107 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
108 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
109 cell_info[i][j].top_line = row_info[i].top_line;
110 cell_info[i][j].bottom_line = row_info[i].bottom_line;
111 cell_info[i][j].has_cont_row = false;
112 cell_info[i][j].rotate = false;
113 cell_info[i][j].linebreaks = false;
116 row_info[i-1].bottom_line = true;
117 row_info[0].bottom_line = true;
119 for (i = 0; i < columns; ++i) {
120 column_info[i].left_line = true;
121 column_info[i].right_line = false;
122 column_info[i].alignment = LYX_ALIGN_CENTER;
123 // set width_of_column to zero before it is used in
124 // calculate_width_of_column() (thornley)
125 column_info[i].width_of_column = 0;
126 calculate_width_of_column(i);
128 column_info[i-1].right_line = true;
130 calculate_width_of_table();
134 set_row_column_number_info();
135 is_long_table = false;
144 void LyXTable::AppendRow(int cell)
146 int row = row_of_cell(cell);
147 rowstruct * row_info2 = new rowstruct[rows + 1];
148 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
151 for (i = 0; i <= row; ++i) {
152 cell_info2[i] = cell_info[i];
153 row_info2[i] = row_info[i];
155 for (i = rows - 1; i >= row; --i) {
156 cell_info2[i + 1] = cell_info[i];
157 row_info2[i + 1] = row_info[i];
159 for (i = row; row_info[i].is_cont_row; --i);
160 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
161 row_info2[row + 1].is_cont_row = false;
162 row_info2[row + 1].top_line = row_info[i].top_line;
163 cell_info2[row + 1] = new cellstruct[columns];
164 for (i = 0; i < columns; ++i) {
165 cell_info2[row + 1][i].width_of_cell = 0;
166 cell_info2[row + 1][i] = cell_info2[row][i];
170 cell_info = cell_info2;
172 row_info = row_info2;
180 void LyXTable::DeleteRow(int cell)
182 int row = row_of_cell(cell);
183 while(!row_info[row].is_cont_row && RowHasContRow(cell))
184 DeleteRow(cell_info[row+1][0].cellno);
185 rowstruct * row_info2 = new rowstruct[rows - 1];
186 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
188 delete[] cell_info[row];
190 for (; i < row; ++i) {
191 cell_info2[i] = cell_info[i];
192 row_info2[i] = row_info[i];
194 if (row_info[i].is_cont_row)
195 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
196 for (i = row; i < rows - 1; ++i) {
197 cell_info2[i] = cell_info[i + 1];
198 row_info2[i] = row_info[i + 1];
202 cell_info = cell_info2;
204 row_info = row_info2;
212 void LyXTable::AppendColumn(int cell)
215 columnstruct * column_info2 = new columnstruct[columns + 1];
216 int column = right_column_of_cell(cell);
219 for (; i <= column; ++i) {
220 column_info2[i] = column_info[i];
222 for (i = columns - 1; i >= column; --i) {
223 column_info2[i + 1] = column_info[i];
226 delete[] column_info;
227 column_info = column_info2;
229 for (i = 0; i < rows; ++i) {
230 cellstruct * tmp = cell_info[i];
231 cell_info[i] = new cellstruct[columns + 1];
232 for (j = 0; j <= column; ++j) {
233 cell_info[i][j] = tmp[j];
235 for (j = column; j < columns; ++j) {
236 cell_info[i][j + 1] = tmp[j];
238 // care about multicolumns
239 if (cell_info[i][column + 1].multicolumn
240 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
241 cell_info[i][column + 1].multicolumn =
242 LyXTable::CELL_PART_OF_MULTICOLUMN;
244 if (column + 1 == columns
245 || cell_info[i][column + 2].multicolumn
246 != LyXTable::CELL_PART_OF_MULTICOLUMN){
247 cell_info[i][column + 1].multicolumn =
248 LyXTable::CELL_NORMAL;
258 void LyXTable::Reinit()
263 for (; i < rows; ++i) {
264 for (j = 0; j < columns; ++j) {
265 cell_info[i][j].width_of_cell = 0;
266 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
267 cell_info[i][j].has_cont_row = false;
271 for (i = 0; i < columns; ++i) {
272 calculate_width_of_column(i);
274 calculate_width_of_table();
276 set_row_column_number_info();
280 void LyXTable::set_row_column_number_info()
286 for (; row < rows; ++row) {
287 for (column = 0; column<columns; ++column) {
288 if (cell_info[row][column].multicolumn
289 != LyXTable::CELL_PART_OF_MULTICOLUMN)
291 cell_info[row][column].cellno = numberofcells;
294 ++numberofcells; // because this is one more than as we start from 0
298 if (rowofcell) // DEL LINE
300 rowofcell = new int[numberofcells];
301 if (columnofcell) // DEL LINE
302 delete [] columnofcell;
303 columnofcell = new int[numberofcells];
305 while (c < numberofcells && row < rows && column < columns) {
307 columnofcell[c] = column;
311 } while (column < columns &&
312 cell_info[row][column].multicolumn
313 == LyXTable::CELL_PART_OF_MULTICOLUMN);
314 if (column == columns){
322 void LyXTable::DeleteColumn(int cell)
324 int column1 = column_of_cell(cell);
325 int column2 = right_column_of_cell(cell);
327 if (column1 == 0 && column2 == columns - 1)
330 for (int column = column1; column <= column2; ++column) {
331 delete_column(column1);
337 int LyXTable::GetNumberOfCells()
339 return numberofcells;
343 int LyXTable::NumberOfCellsInRow(int cell)
345 int row = row_of_cell(cell);
347 for (int i = 0; i < columns; ++i) {
348 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
355 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
357 return (right_column_of_cell(append_cell) ==
358 right_column_of_cell(question_cell));
362 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
364 if (column_of_cell(delete_column_cell) == 0 &&
365 right_column_of_cell(delete_column_cell) == columns - 1)
369 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
370 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
374 /* returns 1 if there is a topline, returns 0 if not */
375 bool LyXTable::TopLine(int cell)
377 int row = row_of_cell(cell);
380 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
381 if (IsMultiColumn(cell))
382 return cellinfo_of_cell(cell)->top_line;
383 return row_info[row].top_line;
387 bool LyXTable::BottomLine(int cell)
389 //no bottom line underneath non-existent cells if you please
390 if(cell >= numberofcells)
393 int row = row_of_cell(cell);
395 if (RowHasContRow(cell))
396 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
397 if (IsMultiColumn(cell))
398 return cellinfo_of_cell(cell)->bottom_line;
399 return row_info[row_of_cell(cell)].bottom_line;
403 bool LyXTable::LeftLine(int cell)
405 return column_info[column_of_cell(cell)].left_line;
409 bool LyXTable::RightLine(int cell)
411 return column_info[right_column_of_cell(cell)].right_line;
415 bool LyXTable::TopAlreadyDrawed(int cell)
417 if (AdditionalHeight(cell))
419 int row = row_of_cell(cell);
421 int column = column_of_cell(cell);
423 && cell_info[row-1][column].multicolumn
424 == LyXTable::CELL_PART_OF_MULTICOLUMN)
426 if (cell_info[row-1][column].multicolumn
427 == LyXTable::CELL_NORMAL)
428 return row_info[row-1].bottom_line;
430 return cell_info[row-1][column].bottom_line;
436 bool LyXTable::VeryLastRow(int cell)
438 return (row_of_cell(cell) == rows - 1);
442 int LyXTable::AdditionalHeight(int cell)
444 int row = row_of_cell(cell);
447 int top = 1; // bool top = true; ??
448 int bottom = 1; // bool bottom = true; ??
451 for (column = 0; column < columns - 1 && bottom; ++column) {
452 switch (cell_info[row - 1][column].multicolumn) {
453 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
454 bottom = cell_info[row - 1][column].bottom_line;
456 case LyXTable::CELL_NORMAL:
457 bottom = row_info[row - 1].bottom_line;
460 for (column = 0; column < columns - 1 && top; ++column) {
461 switch (cell_info[row][column].multicolumn){
462 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
463 top = cell_info[row][column].top_line;
465 case LyXTable::CELL_NORMAL:
466 top = row_info[row].top_line;
470 return WIDTH_OF_LINE;
475 int LyXTable::AdditionalWidth(int cell)
477 // internally already set in SetWidthOfCell
478 // used to get it back in text.C
479 int col = right_column_of_cell(cell);
480 if (col < columns - 1 && column_info[col].right_line &&
481 column_info[col+1].left_line)
482 return WIDTH_OF_LINE;
488 // returns the maximum over all rows
489 int LyXTable::WidthOfColumn(int cell)
491 int column1 = column_of_cell(cell);
492 int column2 = right_column_of_cell(cell);
495 for (; i <= column2; ++i) {
496 result += column_info[i].width_of_column;
502 int LyXTable::WidthOfTable()
504 return width_of_table;
507 /* returns 1 if a complete update is necessary, otherwise 0 */
508 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
510 if (!IsMultiColumn(cell))
513 int row = row_of_cell(cell);
514 int column1 = column_of_cell(cell);
515 int column2 = right_column_of_cell(cell);
517 // first set columns to 0 so we can calculate the right width
519 for (; i <= column2; ++i) {
520 cell_info[row][i].width_of_cell = 0;
522 // set the width to MAX_WIDTH until width > 0
523 int width = (new_width + 2 * WIDTH_OF_LINE);
524 for (i = column1; (i < column2) && (width > 0); ++i) {
525 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
526 width -= column_info[i].width_of_column;
529 cell_info[row][i].width_of_cell = width;
535 void LyXTable::recalculateMulticolCells(int cell, int new_width)
537 int row = row_of_cell(cell);
538 int column1 = column_of_cell(cell);
539 int column2 = right_column_of_cell(cell);
541 // first set columns to 0 so we can calculate the right width
543 for (; i <= column2; ++i)
544 cell_info[row][i].width_of_cell = 0;
545 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
547 if (i < numberofcells)
548 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
549 SetWidthOfMulticolCell(cell, new_width);
553 /* returns 1 if a complete update is necessary, otherwise 0 */
554 bool LyXTable::SetWidthOfCell(int cell, int new_width)
556 int row = row_of_cell(cell);
557 int column1 = column_of_cell(cell);
561 if (IsMultiColumn(cell)) {
562 tmp = SetWidthOfMulticolCell(cell, new_width);
564 width = (new_width + 2*WIDTH_OF_LINE);
565 cell_info[row][column1].width_of_cell = width;
566 if (column_info[column1].right_line && (column1 < columns-1) &&
567 column_info[column1+1].left_line) // additional width
568 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
569 tmp = calculate_width_of_column_NMC(column1);
573 for(i = 0; i<columns;++i)
574 calculate_width_of_column_NMC(i);
575 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
578 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
579 for(i = 0; i<columns;++i)
580 calculate_width_of_column(i);
581 calculate_width_of_table();
588 bool LyXTable::SetAlignment(int cell, char align)
590 if (!IsMultiColumn(cell))
591 column_info[column_of_cell(cell)].alignment = align;
592 cellinfo_of_cell(cell)->alignment = align;
596 bool LyXTable::SetPWidth(int cell, string width)
598 int fvcell = FirstVirtualCell(cell);
600 if (IsMultiColumn(fvcell)) {
601 // if (column_info[column_of_cell(cell)].p_width.empty())
602 // column_info[column_of_cell(cell)].p_width = width;
603 cellinfo_of_cell(fvcell)->p_width = width;
605 column_info[column_of_cell(fvcell)].p_width = width;
606 if (!width.empty()) // do this only if there is a width
607 SetAlignment(cell, LYX_ALIGN_LEFT);
612 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
614 if (what == SET_SPECIAL_MULTI)
615 cellinfo_of_cell(cell)->align_special = special;
617 column_info[column_of_cell(cell)].align_special = special;
621 bool LyXTable::SetAllLines(int cell, bool line)
623 SetTopLine(cell, line);
624 SetBottomLine(cell, line);
625 SetRightLine(cell, line);
626 SetLeftLine(cell, line);
630 bool LyXTable::SetTopLine(int cell, bool line)
632 int row = row_of_cell(cell);
635 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
636 else if (!IsMultiColumn(cell))
637 row_info[row].top_line = line;
639 cellinfo_of_cell(cell)->top_line = line;
644 bool LyXTable::SetBottomLine(int cell, bool line)
646 int row = row_of_cell(cell);
648 if (RowHasContRow(cell))
649 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
650 else if (!IsMultiColumn(cell))
651 row_info[row_of_cell(cell)].bottom_line = line;
653 cellinfo_of_cell(cell)->bottom_line = line;
658 bool LyXTable::SetLeftLine(int cell, bool line)
660 column_info[column_of_cell(cell)].left_line = line;
665 bool LyXTable::SetRightLine(int cell, bool line)
667 column_info[right_column_of_cell(cell)].right_line = line;
672 char LyXTable::GetAlignment(int cell)
674 if (IsMultiColumn(cell))
675 return cellinfo_of_cell(cell)->alignment;
677 return column_info[column_of_cell(cell)].alignment;
680 string LyXTable::GetPWidth(int cell)
682 int fvcell = FirstVirtualCell(cell);
684 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
685 return cellinfo_of_cell(fvcell)->p_width;
686 return column_info[column_of_cell(fvcell)].p_width;
689 string LyXTable::GetAlignSpecial(int cell, int what)
691 if (what == SET_SPECIAL_MULTI)
692 return cellinfo_of_cell(cell)->align_special;
693 return column_info[column_of_cell(cell)].align_special;
696 int LyXTable::GetWidthOfCell(int cell)
698 int row = row_of_cell(cell);
699 int column1 = column_of_cell(cell);
700 int column2 = right_column_of_cell(cell);
703 for (; i <= column2; ++i) {
704 result += cell_info[row][i].width_of_cell;
707 result += AdditionalWidth(cell);
713 int LyXTable::GetBeginningOfTextInCell(int cell)
717 switch (GetAlignment(cell)){
718 case LYX_ALIGN_CENTER:
719 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
721 case LYX_ALIGN_RIGHT:
722 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
724 default: /* LYX_ALIGN_LEFT: nothing :-) */
734 bool LyXTable::IsFirstCell(int cell)
736 return (column_of_cell(cell) == 0);
739 bool LyXTable::IsLastCell(int cell)
741 return (right_column_of_cell(cell) == (columns - 1));
745 bool LyXTable::calculate_width_of_column(int column)
747 int old_column_width = column_info[column].width_of_column;
750 for (int i = 0; i < rows; ++i) {
751 maximum = max(cell_info[i][column].width_of_cell, maximum);
753 column_info[column].width_of_column = maximum;
754 return (column_info[column].width_of_column != old_column_width);
757 bool LyXTable::calculate_width_of_column_NMC(int column)
759 int old_column_width = column_info[column].width_of_column;
761 for (int i = 0; i < rows; ++i) {
762 if (!IsMultiColumn(GetCellNumber(column, i)) &&
763 (cell_info[i][column].width_of_cell > max)) {
764 max = cell_info[i][column].width_of_cell;
767 column_info[column].width_of_column = max;
768 return (column_info[column].width_of_column != old_column_width);
771 void LyXTable::calculate_width_of_table()
774 for (int i = 0; i < columns; ++i) {
775 width_of_table += column_info[i].width_of_column;
780 int LyXTable::row_of_cell(int cell)
782 if (cell >= numberofcells)
786 return rowofcell[cell];
790 int LyXTable::column_of_cell(int cell)
792 if (cell >= numberofcells)
796 return columnofcell[cell];
800 int LyXTable::right_column_of_cell(int cell)
802 int row = row_of_cell(cell);
803 int column = column_of_cell(cell);
804 while (column < columns - 1 &&
805 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
811 void LyXTable::Write(ostream & os)
815 << rows << " " << columns << " " << is_long_table << " "
816 << rotate << " " << endhead << " " << endfirsthead << " "
817 << endfoot << " " << endlastfoot << "\n";
818 for (i = 0; i < rows; ++i) {
819 os << row_info[i].top_line << " "
820 << row_info[i].bottom_line << " "
821 << row_info[i].is_cont_row << " "
822 << row_info[i].newpage << "\n";
824 for (i = 0; i < columns; ++i) {
825 os << column_info[i].alignment << " "
826 << column_info[i].left_line << " "
827 << column_info[i].right_line << " \""
828 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
829 << column_info[i].align_special << "\"\n";
832 for (i = 0; i < rows; ++i) {
833 for (j = 0; j < columns; ++j) {
834 os << cell_info[i][j].multicolumn << " "
835 << cell_info[i][j].alignment << " "
836 << cell_info[i][j].top_line << " "
837 << cell_info[i][j].bottom_line << " "
838 << cell_info[i][j].has_cont_row << " "
839 << cell_info[i][j].rotate << " "
840 << cell_info[i][j].linebreaks << " \""
841 << cell_info[i][j].align_special << "\" \""
842 << cell_info[i][j].p_width << "\"\n";
848 void LyXTable::Read(istream & is)
854 int is_long_table_arg = false;
855 int rotate_arg = false;
868 version = atoi(s.c_str() + 8);
872 #warning Insert a error message window here that this format is not supported anymore
875 lyxerr << "Tabular format < 5 is not supported anymore\n"
876 "Get an older version of LyX (< 1.1.x) for conversion!"
879 is >> rows_arg >> columns_arg >> is_long_table_arg
880 >> rotate_arg >> a >> b >> c >> d;
882 is >> rows_arg >> columns_arg;
883 Init(rows_arg, columns_arg);
884 SetLongTable(is_long_table_arg);
885 SetRotateTable(rotate_arg);
887 for (i = 0; i < rows; ++i) {
890 for (i = 0; i < columns; ++i) {
893 for (i = 0; i < rows; ++i) {
894 for (j = 0; j < columns; ++j) {
898 set_row_column_number_info();
901 is >> rows_arg >> columns_arg >> is_long_table_arg
902 >> rotate_arg >> a >> b >> c >> d;
903 Init(rows_arg, columns_arg);
904 SetLongTable(is_long_table_arg);
905 SetRotateTable(rotate_arg);
910 for (i = 0; i < rows; ++i) {
911 a = b = c = d = e = f = g = h = 0;
912 is >> a >> b >> c >> d;
913 row_info[i].top_line = a;
914 row_info[i].bottom_line = b;
915 row_info[i].is_cont_row = c;
916 row_info[i].newpage = d;
918 for (i = 0; i < columns; ++i) {
924 getline(is, s1, '"');
925 is >> ch; // skip '"'
926 getline(is, s2, '"');
927 column_info[i].alignment = static_cast<char>(a);
928 column_info[i].left_line = b;
929 column_info[i].right_line = c;
930 column_info[i].p_width = s1;
931 column_info[i].align_special = s2;
933 for (i = 0; i < rows; ++i) {
934 for (j = 0; j < columns; ++j) {
937 is >> a >> b >> c >> d >> e >> f >> g;
939 is >> ch; // skip '"'
940 getline(is, s1, '"');
941 is >> ch; // skip '"'
942 getline(is, s2, '"');
943 cell_info[i][j].multicolumn = static_cast<char>(a);
944 cell_info[i][j].alignment = static_cast<char>(b);
945 cell_info[i][j].top_line = static_cast<char>(c);
946 cell_info[i][j].bottom_line = static_cast<char>(d);
947 cell_info[i][j].has_cont_row = static_cast<bool>(e);
948 cell_info[i][j].rotate = static_cast<bool>(f);
949 cell_info[i][j].linebreaks = static_cast<bool>(g);
950 cell_info[i][j].align_special = s1;
951 cell_info[i][j].p_width = s2;
954 set_row_column_number_info();
958 // cell <0 will tex the preamble
959 // returns the number of printed newlines
960 int LyXTable::TexEndOfCell(string & file, int cell)
966 if (ShouldBeVeryLastCell(cell)) {
967 // the very end at the very beginning
968 if (Linebreaks(cell))
969 file += "\\smallskip{}}";
970 if (IsMultiColumn(cell))
972 if (RotateCell(cell)) {
973 file += "\n\\end{sideways}";
981 while (!IsFirstCell(fcell)) --fcell;
982 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
983 if (BottomLine(fcell + i))
986 if (tmp == NumberOfCellsInRow(fcell)) {
990 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
991 if (BottomLine(fcell + i)) {
993 file += tostr(column_of_cell(fcell + i) + 1);
995 file += tostr(right_column_of_cell(fcell + i) + 1);
1006 file += "\\end{longtable}";
1008 file += "\\end{tabular}";
1010 file += "\n\\end{sideways}";
1014 nvcell = NextVirtualCell(cell + 1);
1018 file += "\\begin{sideways}\n";
1022 file += "\\begin{longtable}{";
1024 file += "\\begin{tabular}{";
1025 for (i = 0; i < columns; ++i) {
1026 if (column_info[i].left_line)
1028 if (!column_info[i].align_special.empty()) {
1029 file += column_info[i].align_special.c_str();
1030 } else if (!column_info[i].p_width.empty()) {
1032 file += column_info[i].p_width;
1035 switch (column_info[i].alignment) {
1036 case LYX_ALIGN_LEFT:
1039 case LYX_ALIGN_RIGHT:
1047 if (column_info[i].right_line)
1053 if (GetNumberOfCells()) {
1055 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1056 if (TopLine(fcell + i))
1059 if (tmp == NumberOfCellsInRow(fcell)){
1063 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1064 if (TopLine(fcell + i)) {
1066 file += tostr(column_of_cell(fcell + i) + 1);
1068 file += tostr(right_column_of_cell(fcell + i) + 1);
1079 if (RotateCell(0)) {
1080 file += "\\begin{sideways}\n";
1085 if (Linebreaks(cell))
1086 file += "\\smallskip{}}";
1087 if (IsMultiColumn(cell)){
1090 if (RotateCell(cell)) {
1091 file += "\n\\end{sideways}";
1094 if (IsLastCell(cell)) {
1095 int row = row_of_cell(cell);
1096 string hline1, hline2;
1097 bool print_hline = true;
1098 bool flag1 = IsLongTable() &&
1099 ((row == endhead) || (row == endfirsthead) ||
1100 (row == endfoot) || (row == endlastfoot));
1102 bool 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 bool 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 ";
1179 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1180 if (TopLine(fcell + i)) {
1183 file += tostr(column_of_cell(fcell+i)+1);
1185 file += tostr(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 file += "\\endhead\n";
1221 if (IsLongTable() && (row == endfirsthead)) {
1222 file += "\\endfirsthead\n";
1225 if (sflag1) { // add the \hline for next foot row
1226 if (!hline1.empty()) {
1227 file += hline1 + '\n';
1231 // add the \hline for the first row
1232 if (pr_top_hline && sflag2) {
1233 if (!hline2.empty()) {
1234 file += hline2 + '\n';
1238 if (nvcell < numberofcells && RotateCell(nvcell)) {
1239 file += "\\begin{sideways}\n";
1245 if (nvcell < numberofcells && RotateCell(nvcell)) {
1246 file += "\\begin{sideways}\n";
1251 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1252 file += "\\multicolumn{";
1253 file += tostr(cells_in_multicolumn(nvcell));
1255 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1256 file += cellinfo_of_cell(cell+1)->align_special;
1259 if (LeftLine(nvcell))
1261 if (!GetPWidth(nvcell).empty()) {
1263 file += GetPWidth(nvcell);
1266 switch (GetAlignment(nvcell)) {
1267 case LYX_ALIGN_LEFT: file += 'l'; break;
1268 case LYX_ALIGN_RIGHT: file += 'r'; break;
1269 default: file += '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)))
1283 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1284 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1285 file += "\\parbox{";
1286 file += GetPWidth(nvcell);
1287 file += "}{\\smallskip{}";
1294 // cell <0 will tex the preamble
1295 // returns the number of printed newlines
1296 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1300 if (cell == GetNumberOfCells() - 1){
1301 // the very end at the very beginning
1302 if (CellHasContRow(cell) >= 0) {
1308 if (row_info[row_of_cell(cell)].bottom_line) {
1312 os << ".TE\n.pl 1c";
1317 os << "\n.pl 500c\n.TS\n";
1318 for (int j = 0; j < rows; ++j) {
1319 for (int i = 0; i < columns; ++i, ++fcell) {
1320 if (column_info[i].left_line)
1322 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1325 switch (column_info[i].alignment) {
1326 case LYX_ALIGN_LEFT:
1329 case LYX_ALIGN_RIGHT:
1337 if (!column_info[i].p_width.empty())
1338 os << "w(" << column_info[i].p_width << ")";
1339 if (column_info[i].right_line)
1342 if ((j + 1) < rows) {
1349 if (row_info[0].top_line) {
1353 if (CellHasContRow(0) >= 0) {
1359 if (CellHasContRow(cell) >= 0) {
1363 if (right_column_of_cell(cell) == columns -1){
1366 int row = row_of_cell(cell);
1367 if (row_info[row++].bottom_line) {
1371 if ((row < rows) && row_info[row].top_line) {
1377 if ((cell < GetNumberOfCells() - 1) &&
1378 (CellHasContRow(cell+1) >= 0)) {
1388 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1394 i = column_of_cell(cell);
1395 if (!isColumn && IsMultiColumn(cell)) {
1396 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1397 return cellinfo_of_cell(cell)->align_special.c_str();
1399 switch (GetAlignment(cell)) {
1400 case LYX_ALIGN_LEFT:
1402 case LYX_ALIGN_RIGHT:
1409 if (!column_info[i].align_special.empty()) {
1410 return column_info[i].align_special.c_str();
1412 #ifdef IGNORE_THIS_FOR_NOW
1413 else if (!column_info[i].p_width.empty()) {
1415 file += column_info[i].p_width;
1420 switch (column_info[i].alignment) {
1421 case LYX_ALIGN_LEFT:
1423 case LYX_ALIGN_RIGHT:
1432 // cell <0 will tex the preamble
1433 // returns the number of printed newlines
1434 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1438 //int tmp; // tmp2; //Â unused
1439 int nvcell; // fcell; //Â unused
1440 if (ShouldBeVeryLastCell(cell)) {
1441 addNewlineAndDepth(file,--depth);
1443 addNewlineAndDepth(file,--depth);
1445 addNewlineAndDepth(file,--depth);
1447 addNewlineAndDepth(file,--depth);
1449 file += "</TGROUP>";
1451 file += "</TGROUP>";
1452 addNewlineAndDepth(file,--depth);
1455 nvcell = NextVirtualCell(cell+1);
1463 file += tostr(columns);
1464 file += "' COLSEP='1' ROWSEP='1'>";
1465 addNewlineAndDepth(file,++depth);
1467 for (i = 0; i < columns; ++i) {
1468 file += "<COLSPEC ALIGN='";
1469 file += getDocBookAlign(i, true);
1470 file += "' COLNAME='col";
1472 file += "' COLNUM='";
1474 file += "' COLSEP='";
1475 if (i == (columns-1)) {
1478 if (column_info[i].right_line ||
1479 column_info[i+1].left_line)
1485 addNewlineAndDepth(file, depth);
1487 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1488 if (column_info[i].left_line)
1493 addNewlineAndDepth(file,++depth);
1495 addNewlineAndDepth(file,++depth);
1496 file += "<ENTRY ALIGN='";
1497 file += getDocBookAlign(0);
1499 if (IsMultiColumn(0)) {
1500 file += " NAMEST='col1' NAMEEND='col";
1501 file += tostr(cells_in_multicolumn(0));
1505 addNewlineAndDepth(file,++depth);
1508 if (IsLastCell(cell)) {
1509 addNewlineAndDepth(file,--depth);
1511 addNewlineAndDepth(file,--depth);
1513 addNewlineAndDepth(file, depth);
1515 addNewlineAndDepth(file,++depth);
1516 file += "<ENTRY ALIGN='";
1517 file += getDocBookAlign(cell+1);
1518 file += "' VALIGN='middle'";
1519 if (IsMultiColumn(cell+1)) {
1520 file += " NAMEST='col";
1521 file += tostr(column_of_cell(cell+1) + 1);
1522 file += "' NAMEEND='col";
1523 file += tostr(column_of_cell(cell+1) +
1524 cells_in_multicolumn(cell+1));
1528 addNewlineAndDepth(file,++depth);
1531 addNewlineAndDepth(file,--depth);
1533 addNewlineAndDepth(file, depth);
1534 file += "<ENTRY ALIGN='";
1535 file += getDocBookAlign(cell+1);
1536 file += "' VALIGN='middle'";
1537 if (IsMultiColumn(cell+1)) {
1538 file += " NAMEST='col";
1539 file += tostr(column_of_cell(cell+1) + 1);
1540 file += "' NAMEEND='col";
1541 file += tostr(column_of_cell(cell+1) +
1542 cells_in_multicolumn(cell+1));
1546 addNewlineAndDepth(file,++depth);
1555 bool LyXTable::IsMultiColumn(int cell)
1557 int fvcell = FirstVirtualCell(cell);
1559 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1563 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1565 int row = row_of_cell(cell);
1566 int column = column_of_cell(cell);
1567 return &cell_info[row][column];
1571 void LyXTable::SetMultiColumn(int cell, int number)
1573 int fvcell = FirstVirtualCell(cell);
1574 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1576 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1577 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1578 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1579 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1580 for (number--; number > 0; --number) {
1581 cellinfo_of_cell(fvcell+number)->multicolumn =
1582 LyXTable::CELL_PART_OF_MULTICOLUMN;
1583 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1585 set_row_column_number_info();
1586 SetWidthOfCell(fvcell, new_width);
1590 int LyXTable::cells_in_multicolumn(int cell)
1592 int row = row_of_cell(cell);
1593 int column = column_of_cell(cell);
1596 while (column < columns && cell_info[row][column].multicolumn
1597 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1605 int LyXTable::UnsetMultiColumn(int cell)
1607 int fvcell = FirstVirtualCell(cell);
1608 int row = row_of_cell(fvcell);
1609 int column = column_of_cell(fvcell);
1613 if (cell_info[row][column].multicolumn
1614 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1615 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1617 while (column < columns &&
1618 cell_info[row][column].multicolumn
1619 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1620 cell_info[row][column].multicolumn =
1621 LyXTable::CELL_NORMAL;
1626 set_row_column_number_info();
1631 void LyXTable::delete_column(int column)
1634 columnstruct * column_info2 = new columnstruct[columns-1];
1636 for (i = 0; i < column; ++i) {
1637 column_info2[i] = column_info[i];
1639 for (i = column; i < columns - 1; ++i) {
1640 column_info2[i] = column_info[i + 1];
1643 delete[] column_info;
1644 column_info = column_info2;
1646 for (i = 0; i < rows; ++i) {
1647 cellstruct * tmp = cell_info[i];
1648 cell_info[i] = new cellstruct[columns - 1];
1649 for (j = 0; j < column; ++j) {
1650 cell_info[i][j] = tmp[j];
1652 for (j = column; j < columns - 1; ++j) {
1653 cell_info[i][j] = tmp[j + 1];
1663 void LyXTable::SetLongTable(int what)
1665 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;