1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 * ======================================================
18 #include "support/lstrings.h"
21 #pragma implementation
24 extern void addNewlineAndDepth(string & file, int const depth); // Jug 990923
26 static int const WIDTH_OF_LINE = 5;
29 LyXTable::LyXTable(int rows_arg, int columns_arg)
31 Init(rows_arg, columns_arg);
35 LyXTable::LyXTable(LyXLex & lex)
37 istream & is = lex.getStream();
42 LyXTable::~LyXTable() {
44 delete[] columnofcell;
47 for (int i = 0; i < rows; ++i) {
48 delete[] cell_info[i]; // verify that this shoudn't be freed with delete
54 LyXTable * LyXTable::Clone()
56 LyXTable * result = new LyXTable(rows, columns);
59 for (row = 0; row < rows; ++row) {
60 for (column = 0; column < columns; ++column) {
61 result->cell_info[row][column] = cell_info[row][column];
65 for (row = 0; row < rows; ++row) {
66 result->row_info[row] = row_info[row];
69 for (column = 0; column < columns; ++column) {
70 result->column_info[column].left_line = column_info[column].left_line;
71 result->column_info[column].right_line = column_info[column].right_line;
72 result->column_info[column].alignment = column_info[column].alignment;
73 result->column_info[column].p_width = column_info[column].p_width;
74 result->column_info[column].align_special = column_info[column].align_special;
77 result->SetLongTable(is_long_table);
78 result->rotate = rotate;
84 /* activates all lines and sets all widths to 0 */
85 void LyXTable::Init(int rows_arg, int columns_arg)
89 columns = columns_arg;
90 column_info = new columnstruct[columns];
91 row_info = new rowstruct[rows];
92 cell_info = new cellstruct*[rows];
95 for (i = 0; i < rows; ++i) {
96 cell_info[i] = new cellstruct[columns];
97 row_info[i].top_line = true;
98 row_info[i].bottom_line = false;
99 row_info[i].is_cont_row = false;
100 row_info[i].newpage = false;
101 for (j = 0; j < columns; ++j) {
102 cell_info[i][j].cellno = cellno++;
103 cell_info[i][j].width_of_cell = 0;
104 cell_info[i][j].multicolumn = LyXTable::CELL_NORMAL;
105 cell_info[i][j].alignment = LYX_ALIGN_CENTER;
106 cell_info[i][j].top_line = row_info[i].top_line;
107 cell_info[i][j].bottom_line = row_info[i].bottom_line;
108 cell_info[i][j].has_cont_row = false;
109 cell_info[i][j].rotate = false;
110 cell_info[i][j].linebreaks = false;
113 row_info[i-1].bottom_line = true;
114 row_info[0].bottom_line = true;
116 for (i = 0; i < columns; ++i) {
117 column_info[i].left_line = true;
118 column_info[i].right_line = false;
119 column_info[i].alignment = LYX_ALIGN_CENTER;
120 // set width_of_column to zero before it is used in
121 // calculate_width_of_column() (thornley)
122 column_info[i].width_of_column = 0;
123 calculate_width_of_column(i);
125 column_info[i-1].right_line = true;
127 calculate_width_of_table();
131 set_row_column_number_info();
132 is_long_table = false;
141 void LyXTable::AppendRow(int cell)
143 int row = row_of_cell(cell);
144 rowstruct * row_info2 = new rowstruct[rows + 1];
145 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
148 for (i = 0; i <= row; ++i) {
149 cell_info2[i] = cell_info[i];
150 row_info2[i] = row_info[i];
152 for (i = rows - 1; i >= row; --i) {
153 cell_info2[i + 1] = cell_info[i];
154 row_info2[i + 1] = row_info[i];
156 for (i = row; row_info[i].is_cont_row; --i);
157 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
158 row_info2[row + 1].is_cont_row = false;
159 row_info2[row + 1].top_line = row_info[i].top_line;
160 cell_info2[row + 1] = new cellstruct[columns];
161 for (i = 0; i < columns; ++i) {
162 cell_info2[row + 1][i].width_of_cell = 0;
163 cell_info2[row + 1][i] = cell_info2[row][i];
167 cell_info = cell_info2;
169 row_info = row_info2;
177 void LyXTable::DeleteRow(int cell)
179 int row = row_of_cell(cell);
180 while(!row_info[row].is_cont_row && RowHasContRow(cell))
181 DeleteRow(cell_info[row+1][0].cellno);
182 rowstruct * row_info2 = new rowstruct[rows - 1];
183 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
185 delete[] cell_info[row];
187 for (; i < row; ++i) {
188 cell_info2[i] = cell_info[i];
189 row_info2[i] = row_info[i];
191 if (row_info[i].is_cont_row)
192 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
193 for (i = row; i < rows - 1; ++i) {
194 cell_info2[i] = cell_info[i + 1];
195 row_info2[i] = row_info[i + 1];
199 cell_info = cell_info2;
201 row_info = row_info2;
209 void LyXTable::AppendColumn(int cell)
212 columnstruct * column_info2 = new columnstruct[columns + 1];
213 int column = right_column_of_cell(cell);
216 for (; i <= column; ++i) {
217 column_info2[i] = column_info[i];
219 for (i = columns - 1; i >= column; --i) {
220 column_info2[i + 1] = column_info[i];
223 delete[] column_info;
224 column_info = column_info2;
226 for (i = 0; i < rows; ++i) {
227 cellstruct * tmp = cell_info[i];
228 cell_info[i] = new cellstruct[columns + 1];
229 for (j = 0; j <= column; ++j) {
230 cell_info[i][j] = tmp[j];
232 for (j = column; j < columns; ++j) {
233 cell_info[i][j + 1] = tmp[j];
235 // care about multicolumns
236 if (cell_info[i][column + 1].multicolumn
237 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
238 cell_info[i][column + 1].multicolumn =
239 LyXTable::CELL_PART_OF_MULTICOLUMN;
241 if (column + 1 == columns
242 || cell_info[i][column + 2].multicolumn
243 != LyXTable::CELL_PART_OF_MULTICOLUMN){
244 cell_info[i][column + 1].multicolumn =
245 LyXTable::CELL_NORMAL;
255 void LyXTable::Reinit()
260 for (; i < rows; ++i) {
261 for (j = 0; j < columns; ++j) {
262 cell_info[i][j].width_of_cell = 0;
263 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
264 cell_info[i][j].has_cont_row = false;
268 for (i = 0; i < columns; ++i) {
269 calculate_width_of_column(i);
271 calculate_width_of_table();
273 set_row_column_number_info();
277 void LyXTable::set_row_column_number_info()
283 for (; row < rows; ++row) {
284 for (column = 0; column<columns; ++column) {
285 if (cell_info[row][column].multicolumn
286 != LyXTable::CELL_PART_OF_MULTICOLUMN)
288 cell_info[row][column].cellno = numberofcells;
291 ++numberofcells; // because this is one more than as we start from 0
295 if (rowofcell) // DEL LINE
297 rowofcell = new int[numberofcells];
298 if (columnofcell) // DEL LINE
299 delete [] columnofcell;
300 columnofcell = new int[numberofcells];
302 while (c < numberofcells && row < rows && column < columns) {
304 columnofcell[c] = column;
308 } while (column < columns &&
309 cell_info[row][column].multicolumn
310 == LyXTable::CELL_PART_OF_MULTICOLUMN);
311 if (column == columns){
319 void LyXTable::DeleteColumn(int cell)
321 int column1 = column_of_cell(cell);
322 int column2 = right_column_of_cell(cell);
324 if (column1 == 0 && column2 == columns - 1)
327 for (int column = column1; column <= column2; ++column) {
328 delete_column(column1);
334 int LyXTable::GetNumberOfCells()
336 return numberofcells;
340 int LyXTable::NumberOfCellsInRow(int cell)
342 int row = row_of_cell(cell);
344 for (int i = 0; i < columns; ++i) {
345 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
352 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
354 return (right_column_of_cell(append_cell) ==
355 right_column_of_cell(question_cell));
359 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
361 if (column_of_cell(delete_column_cell) == 0 &&
362 right_column_of_cell(delete_column_cell) == columns - 1)
366 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
367 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
371 /* returns 1 if there is a topline, returns 0 if not */
372 bool LyXTable::TopLine(int cell)
374 int row = row_of_cell(cell);
377 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
378 if (IsMultiColumn(cell))
379 return cellinfo_of_cell(cell)->top_line;
380 return row_info[row].top_line;
384 bool LyXTable::BottomLine(int cell)
386 //no bottom line underneath non-existent cells if you please
387 if(cell >= numberofcells)
390 int row = row_of_cell(cell);
392 if (RowHasContRow(cell))
393 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
394 if (IsMultiColumn(cell))
395 return cellinfo_of_cell(cell)->bottom_line;
396 return row_info[row_of_cell(cell)].bottom_line;
400 bool LyXTable::LeftLine(int cell)
402 return column_info[column_of_cell(cell)].left_line;
406 bool LyXTable::RightLine(int cell)
408 return column_info[right_column_of_cell(cell)].right_line;
412 bool LyXTable::TopAlreadyDrawed(int cell)
414 if (AdditionalHeight(cell))
416 int row = row_of_cell(cell);
418 int column = column_of_cell(cell);
420 && cell_info[row-1][column].multicolumn
421 == LyXTable::CELL_PART_OF_MULTICOLUMN)
423 if (cell_info[row-1][column].multicolumn
424 == LyXTable::CELL_NORMAL)
425 return row_info[row-1].bottom_line;
427 return cell_info[row-1][column].bottom_line;
433 bool LyXTable::VeryLastRow(int cell)
435 return (row_of_cell(cell) == rows - 1);
439 int LyXTable::AdditionalHeight(int cell)
441 int row = row_of_cell(cell);
444 int top = 1; // bool top = true; ??
445 int bottom = 1; // bool bottom = true; ??
448 for (column = 0; column < columns - 1 && bottom; ++column) {
449 switch (cell_info[row - 1][column].multicolumn) {
450 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
451 bottom = cell_info[row - 1][column].bottom_line;
453 case LyXTable::CELL_NORMAL:
454 bottom = row_info[row - 1].bottom_line;
457 for (column = 0; column < columns - 1 && top; ++column) {
458 switch (cell_info[row][column].multicolumn){
459 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
460 top = cell_info[row][column].top_line;
462 case LyXTable::CELL_NORMAL:
463 top = row_info[row].top_line;
467 return WIDTH_OF_LINE;
472 int LyXTable::AdditionalWidth(int cell)
474 // internally already set in SetWidthOfCell
475 // used to get it back in text.C
476 int col = right_column_of_cell(cell);
477 if (col < columns - 1 && column_info[col].right_line &&
478 column_info[col+1].left_line)
479 return WIDTH_OF_LINE;
485 // returns the maximum over all rows
486 int LyXTable::WidthOfColumn(int cell)
488 int column1 = column_of_cell(cell);
489 int column2 = right_column_of_cell(cell);
492 for (; i <= column2; ++i) {
493 result += column_info[i].width_of_column;
499 int LyXTable::WidthOfTable()
501 return width_of_table;
504 /* returns 1 if a complete update is necessary, otherwise 0 */
505 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
507 if (!IsMultiColumn(cell))
510 int row = row_of_cell(cell);
511 int column1 = column_of_cell(cell);
512 int column2 = right_column_of_cell(cell);
514 // first set columns to 0 so we can calculate the right width
516 for (; i <= column2; ++i) {
517 cell_info[row][i].width_of_cell = 0;
519 // set the width to MAX_WIDTH until width > 0
520 int width = (new_width + 2 * WIDTH_OF_LINE);
521 for (i = column1; (i < column2) && (width > 0); ++i) {
522 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
523 width -= column_info[i].width_of_column;
526 cell_info[row][i].width_of_cell = width;
532 void LyXTable::recalculateMulticolCells(int cell, int new_width)
534 int row = row_of_cell(cell);
535 int column1 = column_of_cell(cell);
536 int column2 = right_column_of_cell(cell);
538 // first set columns to 0 so we can calculate the right width
540 for (; i <= column2; ++i)
541 cell_info[row][i].width_of_cell = 0;
542 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
544 if (i < numberofcells)
545 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
546 SetWidthOfMulticolCell(cell, new_width);
550 /* returns 1 if a complete update is necessary, otherwise 0 */
551 bool LyXTable::SetWidthOfCell(int cell, int new_width)
553 int row = row_of_cell(cell);
554 int column1 = column_of_cell(cell);
558 if (IsMultiColumn(cell)) {
559 tmp = SetWidthOfMulticolCell(cell, new_width);
561 width = (new_width + 2*WIDTH_OF_LINE);
562 cell_info[row][column1].width_of_cell = width;
563 if (column_info[column1].right_line && (column1 < columns-1) &&
564 column_info[column1+1].left_line) // additional width
565 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
566 tmp = calculate_width_of_column_NMC(column1);
570 for(i = 0; i<columns;++i)
571 calculate_width_of_column_NMC(i);
572 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
575 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
576 for(i = 0; i<columns;++i)
577 calculate_width_of_column(i);
578 calculate_width_of_table();
585 bool LyXTable::SetAlignment(int cell, char align)
587 if (!IsMultiColumn(cell))
588 column_info[column_of_cell(cell)].alignment = align;
589 cellinfo_of_cell(cell)->alignment = align;
593 bool LyXTable::SetPWidth(int cell, string width)
595 int fvcell = FirstVirtualCell(cell);
597 if (IsMultiColumn(fvcell)) {
598 // if (column_info[column_of_cell(cell)].p_width.empty())
599 // column_info[column_of_cell(cell)].p_width = width;
600 cellinfo_of_cell(fvcell)->p_width = width;
602 column_info[column_of_cell(fvcell)].p_width = width;
603 if (!width.empty()) // do this only if there is a width
604 SetAlignment(cell, LYX_ALIGN_LEFT);
609 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
611 if (what == SET_SPECIAL_MULTI)
612 cellinfo_of_cell(cell)->align_special = special;
614 column_info[column_of_cell(cell)].align_special = special;
618 bool LyXTable::SetAllLines(int cell, bool line)
620 SetTopLine(cell, line);
621 SetBottomLine(cell, line);
622 SetRightLine(cell, line);
623 SetLeftLine(cell, line);
627 bool LyXTable::SetTopLine(int cell, bool line)
629 int row = row_of_cell(cell);
632 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
633 else if (!IsMultiColumn(cell))
634 row_info[row].top_line = line;
636 cellinfo_of_cell(cell)->top_line = line;
641 bool LyXTable::SetBottomLine(int cell, bool line)
643 int row = row_of_cell(cell);
645 if (RowHasContRow(cell))
646 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
647 else if (!IsMultiColumn(cell))
648 row_info[row_of_cell(cell)].bottom_line = line;
650 cellinfo_of_cell(cell)->bottom_line = line;
655 bool LyXTable::SetLeftLine(int cell, bool line)
657 column_info[column_of_cell(cell)].left_line = line;
662 bool LyXTable::SetRightLine(int cell, bool line)
664 column_info[right_column_of_cell(cell)].right_line = line;
669 char LyXTable::GetAlignment(int cell)
671 if (IsMultiColumn(cell))
672 return cellinfo_of_cell(cell)->alignment;
674 return column_info[column_of_cell(cell)].alignment;
677 string LyXTable::GetPWidth(int cell)
679 int fvcell = FirstVirtualCell(cell);
681 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
682 return cellinfo_of_cell(fvcell)->p_width;
683 return column_info[column_of_cell(fvcell)].p_width;
686 string LyXTable::GetAlignSpecial(int cell, int what)
688 if (what == SET_SPECIAL_MULTI)
689 return cellinfo_of_cell(cell)->align_special;
690 return column_info[column_of_cell(cell)].align_special;
693 int LyXTable::GetWidthOfCell(int cell)
695 int row = row_of_cell(cell);
696 int column1 = column_of_cell(cell);
697 int column2 = right_column_of_cell(cell);
700 for (; i <= column2; ++i) {
701 result += cell_info[row][i].width_of_cell;
704 result += AdditionalWidth(cell);
710 int LyXTable::GetBeginningOfTextInCell(int cell)
714 switch (GetAlignment(cell)){
715 case LYX_ALIGN_CENTER:
716 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
718 case LYX_ALIGN_RIGHT:
719 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
721 default: /* LYX_ALIGN_LEFT: nothing :-) */
731 bool LyXTable::IsFirstCell(int cell)
733 return (column_of_cell(cell) == 0);
736 bool LyXTable::IsLastCell(int cell)
738 return (right_column_of_cell(cell) == (columns - 1));
742 bool LyXTable::calculate_width_of_column(int column)
744 int old_column_width = column_info[column].width_of_column;
748 for (int i = 0; i < rows; ++i) {
749 maximum = max(cell_info[i][column].width_of_cell, maximum);
751 column_info[column].width_of_column = maximum;
752 return (column_info[column].width_of_column != old_column_width);
755 bool LyXTable::calculate_width_of_column_NMC(int column)
757 int old_column_width = column_info[column].width_of_column;
759 for (int i = 0; i < rows; ++i) {
760 if (!IsMultiColumn(GetCellNumber(column, i)) &&
761 (cell_info[i][column].width_of_cell > max)) {
762 max = cell_info[i][column].width_of_cell;
765 column_info[column].width_of_column = max;
766 return (column_info[column].width_of_column != old_column_width);
769 void LyXTable::calculate_width_of_table()
772 for (int i = 0; i < columns; ++i) {
773 width_of_table += column_info[i].width_of_column;
778 int LyXTable::row_of_cell(int cell)
780 if (cell >= numberofcells)
784 return rowofcell[cell];
788 int LyXTable::column_of_cell(int cell)
790 if (cell >= numberofcells)
794 return columnofcell[cell];
798 int LyXTable::right_column_of_cell(int cell)
800 int row = row_of_cell(cell);
801 int column = column_of_cell(cell);
802 while (column < columns - 1 &&
803 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
809 void LyXTable::Write(ostream & os)
813 << rows << " " << columns << " " << is_long_table << " "
814 << rotate << " " << endhead << " " << endfirsthead << " "
815 << endfoot << " " << endlastfoot << "\n";
816 for (i = 0; i < rows; ++i) {
817 os << row_info[i].top_line << " "
818 << row_info[i].bottom_line << " "
819 << row_info[i].is_cont_row << " "
820 << row_info[i].newpage << "\n";
822 for (i = 0; i < columns; ++i) {
823 os << column_info[i].alignment << " "
824 << column_info[i].left_line << " "
825 << column_info[i].right_line << " \""
826 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
827 << column_info[i].align_special << "\"\n";
830 for (i = 0; i < rows; ++i) {
831 for (j = 0; j < columns; ++j) {
832 os << cell_info[i][j].multicolumn << " "
833 << cell_info[i][j].alignment << " "
834 << cell_info[i][j].top_line << " "
835 << cell_info[i][j].bottom_line << " "
836 << cell_info[i][j].has_cont_row << " "
837 << cell_info[i][j].rotate << " "
838 << cell_info[i][j].linebreaks << " \""
839 << cell_info[i][j].align_special << "\" \""
840 << cell_info[i][j].p_width << "\"\n";
846 void LyXTable::Read(istream & is)
852 int is_long_table_arg = false;
853 int rotate_arg = false;
866 version = atoi(s.c_str() + 8);
870 #warning Insert a error message window here that this format is not supported anymore
873 lyxerr << "Tabular format < 5 is not supported anymore\n"
874 "Get an older version of LyX (< 1.1.x) for conversion!"
877 is >> rows_arg >> columns_arg >> is_long_table_arg
878 >> rotate_arg >> a >> b >> c >> d;
880 is >> rows_arg >> columns_arg;
881 Init(rows_arg, columns_arg);
882 SetLongTable(is_long_table_arg);
883 SetRotateTable(rotate_arg);
885 for (i = 0; i < rows; ++i) {
888 for (i = 0; i < columns; ++i) {
891 for (i = 0; i < rows; ++i) {
892 for (j = 0; j < columns; ++j) {
896 set_row_column_number_info();
899 is >> rows_arg >> columns_arg >> is_long_table_arg
900 >> rotate_arg >> a >> b >> c >> d;
901 Init(rows_arg, columns_arg);
902 SetLongTable(is_long_table_arg);
903 SetRotateTable(rotate_arg);
908 for (i = 0; i < rows; ++i) {
909 a = b = c = d = e = f = g = h = 0;
910 is >> a >> b >> c >> d;
911 row_info[i].top_line = a;
912 row_info[i].bottom_line = b;
913 row_info[i].is_cont_row = c;
914 row_info[i].newpage = d;
916 for (i = 0; i < columns; ++i) {
922 getline(is, s1, '"');
923 is >> ch; // skip '"'
924 getline(is, s2, '"');
925 column_info[i].alignment = static_cast<char>(a);
926 column_info[i].left_line = b;
927 column_info[i].right_line = c;
928 column_info[i].p_width = s1;
929 column_info[i].align_special = s2;
931 for (i = 0; i < rows; ++i) {
932 for (j = 0; j < columns; ++j) {
935 is >> a >> b >> c >> d >> e >> f >> g;
937 is >> ch; // skip '"'
938 getline(is, s1, '"');
939 is >> ch; // skip '"'
940 getline(is, s2, '"');
941 cell_info[i][j].multicolumn = static_cast<char>(a);
942 cell_info[i][j].alignment = static_cast<char>(b);
943 cell_info[i][j].top_line = static_cast<char>(c);
944 cell_info[i][j].bottom_line = static_cast<char>(d);
945 cell_info[i][j].has_cont_row = static_cast<bool>(e);
946 cell_info[i][j].rotate = static_cast<bool>(f);
947 cell_info[i][j].linebreaks = static_cast<bool>(g);
948 cell_info[i][j].align_special = s1;
949 cell_info[i][j].p_width = s2;
952 set_row_column_number_info();
956 // cell <0 will tex the preamble
957 // returns the number of printed newlines
958 int LyXTable::TexEndOfCell(string & file, int cell)
964 if (ShouldBeVeryLastCell(cell)) {
965 // the very end at the very beginning
966 if (Linebreaks(cell))
967 file += "\\smallskip{}}";
968 if (IsMultiColumn(cell))
970 if (RotateCell(cell)) {
971 file += "\n\\end{sideways}";
979 while (!IsFirstCell(fcell)) --fcell;
980 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
981 if (BottomLine(fcell + i))
984 if (tmp == NumberOfCellsInRow(fcell)) {
988 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
989 if (BottomLine(fcell + i)) {
991 file += tostr(column_of_cell(fcell + i) + 1);
993 file += tostr(right_column_of_cell(fcell + i) + 1);
1004 file += "\\end{longtable}";
1006 file += "\\end{tabular}";
1008 file += "\n\\end{sideways}";
1012 nvcell = NextVirtualCell(cell + 1);
1016 file += "\\begin{sideways}\n";
1020 file += "\\begin{longtable}{";
1022 file += "\\begin{tabular}{";
1023 for (i = 0; i < columns; ++i) {
1024 if (column_info[i].left_line)
1026 if (!column_info[i].align_special.empty()) {
1027 file += column_info[i].align_special.c_str();
1028 } else if (!column_info[i].p_width.empty()) {
1030 file += column_info[i].p_width;
1033 switch (column_info[i].alignment) {
1034 case LYX_ALIGN_LEFT:
1037 case LYX_ALIGN_RIGHT:
1045 if (column_info[i].right_line)
1051 if (GetNumberOfCells()) {
1053 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1054 if (TopLine(fcell + i))
1057 if (tmp == NumberOfCellsInRow(fcell)){
1061 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1062 if (TopLine(fcell + i)) {
1064 file += tostr(column_of_cell(fcell + i) + 1);
1066 file += tostr(right_column_of_cell(fcell + i) + 1);
1077 if (RotateCell(0)) {
1078 file += "\\begin{sideways}\n";
1083 if (Linebreaks(cell))
1084 file += "\\smallskip{}}";
1085 if (IsMultiColumn(cell)){
1088 if (RotateCell(cell)) {
1089 file += "\n\\end{sideways}";
1092 if (IsLastCell(cell)) {
1093 int row = row_of_cell(cell);
1094 string hline1, hline2;
1095 bool print_hline = true;
1096 bool flag1 = IsLongTable() &&
1097 ((row == endhead) || (row == endfirsthead) ||
1098 (row == endfoot) || (row == endlastfoot));
1100 bool flag2 = IsLongTable() &&
1101 ((row <= endhead) || (row <= endfirsthead) ||
1102 (row <= endfoot) || (row <= endlastfoot));
1104 // print the bottom hline only if (otherwise it is doubled):
1105 // - is no LongTable
1106 // - there IS a first-header
1107 // - the next row is no special header/footer
1108 // & this row is no special header/footer
1109 // - the next row is a special header/footer
1110 // & this row is a special header/footer
1111 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1112 (endfirsthead == endhead);
1117 while (!IsFirstCell(fcell))
1119 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1120 if (BottomLine(fcell + i))
1123 if (tmp == NumberOfCellsInRow(cell)){
1125 hline1 = "\\hline ";
1128 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1129 if (BottomLine(fcell + i)){
1131 file += tostr(column_of_cell(fcell + i) + 1);
1133 file += tostr(right_column_of_cell(fcell + i) + 1);
1135 hline1 += "\\cline{";
1136 hline1 += tostr(column_of_cell(fcell + i) + 1);
1138 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1148 if (IsLongTable() && (row == endfoot)) {
1149 file += "\\endfoot\n";
1151 print_hline = false; // no double line below footer
1153 if (IsLongTable() && (row == endlastfoot)) {
1154 file += "\\endlastfoot\n";
1156 print_hline = false; // no double line below footer
1158 if (IsLongTable() && row_info[row].newpage) {
1159 file += "\\newpage\n";
1161 print_hline = false; // no line below a \\newpage-command
1164 if (nvcell < numberofcells && (cell < GetNumberOfCells() - 1) &&
1165 !ShouldBeVeryLastCell(cell)) {
1167 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1168 if (TopLine(fcell + i))
1171 if (tmp == NumberOfCellsInRow(fcell)) {
1174 hline2 = "\\hline ";
1177 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1178 if (TopLine(fcell + i)) {
1181 file += tostr(column_of_cell(fcell+i)+1);
1183 file += tostr(right_column_of_cell(fcell+i)+1);
1186 hline2 += "\\cline{";
1187 hline2 += tostr(column_of_cell(fcell+i)+1);
1189 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1195 if (tmp && print_hline){
1200 // the order here is important as if one defines two
1201 // or more things in one line only the first entry is
1202 // displayed the other are set to an empty-row. This
1203 // is important if I have a footer and want that the
1204 // lastfooter is NOT displayed!!!
1205 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1206 (row == endfoot) || (row == endlastfoot);
1208 // sflag2 = IsLongTable() && (row >= 0) &&
1209 // (sflag2 || (row == endhead) || (row == endfirsthead));
1211 bool sflag1 = IsLongTable() && (row != endhead) &&
1212 (row != endfirsthead) &&
1213 ((row == endfoot) || (row == endlastfoot));
1215 if (IsLongTable() && (row == endhead)) {
1216 file += "\\endhead\n";
1219 if (IsLongTable() && (row == endfirsthead)) {
1220 file += "\\endfirsthead\n";
1223 if (sflag1) { // add the \hline for next foot row
1224 if (!hline1.empty()) {
1225 file += hline1 + '\n';
1229 // add the \hline for the first row
1230 if (pr_top_hline && sflag2) {
1231 if (!hline2.empty()) {
1232 file += hline2 + '\n';
1236 if (nvcell < numberofcells && RotateCell(nvcell)) {
1237 file += "\\begin{sideways}\n";
1243 if (nvcell < numberofcells && RotateCell(nvcell)) {
1244 file += "\\begin{sideways}\n";
1249 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1250 file += "\\multicolumn{";
1251 file += tostr(cells_in_multicolumn(nvcell));
1253 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1254 file += cellinfo_of_cell(cell+1)->align_special;
1257 if (LeftLine(nvcell))
1259 if (!GetPWidth(nvcell).empty()) {
1261 file += GetPWidth(nvcell);
1264 switch (GetAlignment(nvcell)) {
1265 case LYX_ALIGN_LEFT: file += 'l'; break;
1266 case LYX_ALIGN_RIGHT: file += 'r'; break;
1267 default: file += 'c'; break;
1270 if (RightLine(nvcell))
1272 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1273 if (((nvcell+1) < numberofcells) &&
1274 (NextVirtualCell(nvcell+1) < numberofcells) &&
1275 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1276 LeftLine(NextVirtualCell(nvcell+1)))
1281 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1282 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
1283 file += "\\parbox{";
1284 file += GetPWidth(nvcell);
1285 file += "}{\\smallskip{}";
1292 // cell <0 will tex the preamble
1293 // returns the number of printed newlines
1294 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1298 if (cell == GetNumberOfCells() - 1){
1299 // the very end at the very beginning
1300 if (CellHasContRow(cell) >= 0) {
1306 if (row_info[row_of_cell(cell)].bottom_line) {
1310 os << ".TE\n.pl 1c";
1315 os << "\n.pl 500c\n.TS\n";
1316 for (int j = 0; j < rows; ++j) {
1317 for (int i = 0; i < columns; ++i, ++fcell) {
1318 if (column_info[i].left_line)
1320 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1323 switch (column_info[i].alignment) {
1324 case LYX_ALIGN_LEFT:
1327 case LYX_ALIGN_RIGHT:
1335 if (!column_info[i].p_width.empty())
1336 os << "w(" << column_info[i].p_width << ")";
1337 if (column_info[i].right_line)
1340 if ((j + 1) < rows) {
1347 if (row_info[0].top_line) {
1351 if (CellHasContRow(0) >= 0) {
1357 if (CellHasContRow(cell) >= 0) {
1361 if (right_column_of_cell(cell) == columns -1){
1364 int row = row_of_cell(cell);
1365 if (row_info[row++].bottom_line) {
1369 if ((row < rows) && row_info[row].top_line) {
1375 if ((cell < GetNumberOfCells() - 1) &&
1376 (CellHasContRow(cell+1) >= 0)) {
1386 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1392 i = column_of_cell(cell);
1393 if (!isColumn && IsMultiColumn(cell)) {
1394 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1395 return cellinfo_of_cell(cell)->align_special.c_str();
1397 switch (GetAlignment(cell)) {
1398 case LYX_ALIGN_LEFT:
1400 case LYX_ALIGN_RIGHT:
1407 if (!column_info[i].align_special.empty()) {
1408 return column_info[i].align_special.c_str();
1410 #ifdef IGNORE_THIS_FOR_NOW
1411 else if (!column_info[i].p_width.empty()) {
1413 file += column_info[i].p_width;
1418 switch (column_info[i].alignment) {
1419 case LYX_ALIGN_LEFT:
1421 case LYX_ALIGN_RIGHT:
1430 // cell <0 will tex the preamble
1431 // returns the number of printed newlines
1432 int LyXTable::DocBookEndOfCell(string & file, int cell, int &depth)
1436 //int tmp; // tmp2; //Â unused
1437 int nvcell; // fcell; //Â unused
1438 if (ShouldBeVeryLastCell(cell)) {
1439 addNewlineAndDepth(file,--depth);
1441 addNewlineAndDepth(file,--depth);
1443 addNewlineAndDepth(file,--depth);
1445 addNewlineAndDepth(file,--depth);
1447 file += "</TGROUP>";
1449 file += "</TGROUP>";
1450 addNewlineAndDepth(file,--depth);
1453 nvcell = NextVirtualCell(cell+1);
1461 file += tostr(columns);
1462 file += "' COLSEP='1' ROWSEP='1'>";
1463 addNewlineAndDepth(file,++depth);
1465 for (i = 0; i < columns; ++i) {
1466 file += "<COLSPEC ALIGN='";
1467 file += getDocBookAlign(i, true);
1468 file += "' COLNAME='col";
1470 file += "' COLNUM='";
1472 file += "' COLSEP='";
1473 if (i == (columns-1)) {
1476 if (column_info[i].right_line ||
1477 column_info[i+1].left_line)
1483 addNewlineAndDepth(file, depth);
1485 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1486 if (column_info[i].left_line)
1491 addNewlineAndDepth(file,++depth);
1493 addNewlineAndDepth(file,++depth);
1494 file += "<ENTRY ALIGN='";
1495 file += getDocBookAlign(0);
1497 if (IsMultiColumn(0)) {
1498 file += " NAMEST='col1' NAMEEND='col";
1499 file += tostr(cells_in_multicolumn(0));
1503 addNewlineAndDepth(file,++depth);
1506 if (IsLastCell(cell)) {
1507 addNewlineAndDepth(file,--depth);
1509 addNewlineAndDepth(file,--depth);
1511 addNewlineAndDepth(file, depth);
1513 addNewlineAndDepth(file,++depth);
1514 file += "<ENTRY ALIGN='";
1515 file += getDocBookAlign(cell+1);
1516 file += "' VALIGN='middle'";
1517 if (IsMultiColumn(cell+1)) {
1518 file += " NAMEST='col";
1519 file += tostr(column_of_cell(cell+1) + 1);
1520 file += "' NAMEEND='col";
1521 file += tostr(column_of_cell(cell+1) +
1522 cells_in_multicolumn(cell+1));
1526 addNewlineAndDepth(file,++depth);
1529 addNewlineAndDepth(file,--depth);
1531 addNewlineAndDepth(file, depth);
1532 file += "<ENTRY ALIGN='";
1533 file += getDocBookAlign(cell+1);
1534 file += "' VALIGN='middle'";
1535 if (IsMultiColumn(cell+1)) {
1536 file += " NAMEST='col";
1537 file += tostr(column_of_cell(cell+1) + 1);
1538 file += "' NAMEEND='col";
1539 file += tostr(column_of_cell(cell+1) +
1540 cells_in_multicolumn(cell+1));
1544 addNewlineAndDepth(file,++depth);
1553 bool LyXTable::IsMultiColumn(int cell)
1555 int fvcell = FirstVirtualCell(cell);
1557 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1561 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1563 int row = row_of_cell(cell);
1564 int column = column_of_cell(cell);
1565 return &cell_info[row][column];
1569 void LyXTable::SetMultiColumn(int cell, int number)
1571 int fvcell = FirstVirtualCell(cell);
1572 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1574 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1575 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1576 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1577 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1578 for (number--; number > 0; --number) {
1579 cellinfo_of_cell(fvcell+number)->multicolumn =
1580 LyXTable::CELL_PART_OF_MULTICOLUMN;
1581 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1583 set_row_column_number_info();
1584 SetWidthOfCell(fvcell, new_width);
1588 int LyXTable::cells_in_multicolumn(int cell)
1590 int row = row_of_cell(cell);
1591 int column = column_of_cell(cell);
1594 while (column < columns && cell_info[row][column].multicolumn
1595 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1603 int LyXTable::UnsetMultiColumn(int cell)
1605 int fvcell = FirstVirtualCell(cell);
1606 int row = row_of_cell(fvcell);
1607 int column = column_of_cell(fvcell);
1611 if (cell_info[row][column].multicolumn
1612 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1613 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1615 while (column < columns &&
1616 cell_info[row][column].multicolumn
1617 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1618 cell_info[row][column].multicolumn =
1619 LyXTable::CELL_NORMAL;
1624 set_row_column_number_info();
1629 void LyXTable::delete_column(int column)
1632 columnstruct * column_info2 = new columnstruct[columns-1];
1634 for (i = 0; i < column; ++i) {
1635 column_info2[i] = column_info[i];
1637 for (i = column; i < columns - 1; ++i) {
1638 column_info2[i] = column_info[i + 1];
1641 delete[] column_info;
1642 column_info = column_info2;
1644 for (i = 0; i < rows; ++i) {
1645 cellstruct * tmp = cell_info[i];
1646 cell_info[i] = new cellstruct[columns - 1];
1647 for (j = 0; j < column; ++j) {
1648 cell_info[i][j] = tmp[j];
1650 for (j = column; j < columns - 1; ++j) {
1651 cell_info[i][j] = tmp[j + 1];
1661 void LyXTable::SetLongTable(int what)
1663 is_long_table = what;
1667 bool LyXTable::IsLongTable()
1669 return is_long_table;
1672 void LyXTable::SetRotateTable(int what)
1677 bool LyXTable::RotateTable()
1682 void LyXTable::SetRotateCell(int cell, int what)
1684 cellinfo_of_cell(cell)->rotate = what;
1687 bool LyXTable::RotateCell(int cell)
1689 return cellinfo_of_cell(cell)->rotate;
1692 bool LyXTable::NeedRotating()
1696 for (int i = 0; i < rows; ++i) {
1697 for (int j = 0; j < columns; ++j) {
1698 if (cell_info[i][j].rotate)
1705 void LyXTable::AppendContRow(int cell)
1707 int row = row_of_cell(cell)+1;
1709 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1711 row_info[row].is_cont_row = true;
1712 row_info[row].top_line = false;
1713 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1717 bool LyXTable::IsContRow(int cell)
1719 return row_info[row_of_cell(cell)].is_cont_row;
1722 int LyXTable::CellHasContRow(int cell)
1724 int row = row_of_cell(cell);
1726 if (VeryLastRow(cell))
1728 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1729 return cell_info[row+1][column_of_cell(cell)].cellno;
1733 bool LyXTable::RowHasContRow(int cell)
1735 int row = row_of_cell(cell) + 1;
1738 return row_info[row].is_cont_row;
1742 int LyXTable::FirstVirtualCell(int cell)
1744 if (!IsContRow(cell))
1746 int row = row_of_cell(cell);
1747 int column = column_of_cell(cell);
1748 for(; (row > 0) && IsContRow(cell_info[row][column].cellno); --row)
1750 return cell_info[row][column].cellno;
1754 int LyXTable::NextVirtualCell(int cell)
1756 if (!IsContRow(cell))
1758 int row = row_of_cell(cell);
1759 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1761 // what if(row >= rows) ?
1762 return cell_info[row][0].cellno;
1766 bool LyXTable::ShouldBeVeryLastCell(int cell)
1767 // "very last cell" ..of what? the row? the table?
1768 // "Cell" in this context appears to not count `virtual' cells
1770 int fcell = cell + 1;
1772 if (cell == GetNumberOfCells() - 1)
1773 return true; // not really sure if I should return false here
1774 if (!IsContRow(fcell))
1776 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1778 if (fcell < GetNumberOfCells() - 1)
1783 bool LyXTable::ShouldBeVeryLastRow(int cell)
1785 if (CellHasContRow(cell)>= 0)
1787 int row = row_of_cell(cell) + 1;
1788 int column = column_of_cell(cell);
1789 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1792 return false; // found another valid row
1793 // I do not have any valid row after the actual
1797 int LyXTable::GetCellAbove(int cell)
1799 int row = row_of_cell(cell);
1802 return cell_info[row-1][column_of_cell(cell)].cellno;
1806 int LyXTable::GetCellNumber(int column, int row)
1808 if (column >= columns)
1809 column = columns - 1;
1810 else if (column < 0)
1817 return cell_info[row][column].cellno;
1820 void LyXTable::SetLinebreaks(int cell, bool what)
1822 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1825 bool LyXTable::Linebreaks(int cell)
1827 int fvcell = FirstVirtualCell(cell);
1829 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1830 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1832 return cellinfo_of_cell(fvcell)->linebreaks;
1835 void LyXTable::SetLTHead(int cell, bool first)
1837 int row = row_of_cell(cell);
1840 if (row == endfirsthead)
1852 bool LyXTable::RowOfLTHead(int cell)
1854 if ((endhead+1) > rows)
1856 return (row_of_cell(cell) == endhead);
1859 bool LyXTable::RowOfLTFirstHead(int cell)
1861 if ((endfirsthead+1) > rows)
1863 return (row_of_cell(cell) == endfirsthead);
1866 void LyXTable::SetLTFoot(int cell, bool last)
1868 int row = row_of_cell(cell);
1871 if (row == endlastfoot)
1883 bool LyXTable::RowOfLTFoot(int cell)
1885 if ((endfoot+1) > rows) {
1889 return (row_of_cell(cell) == endfoot);
1892 bool LyXTable::RowOfLTLastFoot(int cell)
1894 if ((endlastfoot+1) > rows)
1896 return (row_of_cell(cell) == endlastfoot);
1899 void LyXTable::SetLTNewPage(int cell, bool what)
1901 row_info[row_of_cell(cell)].newpage = what;
1904 bool LyXTable::LTNewPage(int cell)
1906 return row_info[row_of_cell(cell)].newpage;