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"
27 #include "support/LAssert.h"
28 #include "lyx_gui_misc.h"
36 static int const WIDTH_OF_LINE = 5;
38 /// Define a few methods for the inner structs
40 LyXTable::cellstruct::cellstruct()
42 cellno = 0; //should be initilaized correctly later.
44 multicolumn = LyXTable::CELL_NORMAL;
45 alignment = LYX_ALIGN_CENTER;
53 LyXTable::cellstruct::~cellstruct()
57 LyXTable::cellstruct &
58 LyXTable::cellstruct::operator=(cellstruct const & cs)
61 width_of_cell = cs.width_of_cell;
62 multicolumn = cs.multicolumn;
63 alignment = cs.alignment;
64 top_line = cs.top_line;
65 bottom_line = cs.bottom_line;
66 has_cont_row = cs.has_cont_row;
68 linebreaks = cs.linebreaks;
72 LyXTable::rowstruct::rowstruct()
82 // Nothing to do, but gcc 2.7.2.3 wants one... (JMarc)
83 LyXTable::rowstruct::~rowstruct()
88 LyXTable::rowstruct::operator=(rowstruct const & rs)
90 top_line = rs.top_line;
91 bottom_line = rs.bottom_line;
92 is_cont_row = rs.is_cont_row;
93 ascent_of_row = rs.ascent_of_row;
94 descent_of_row = rs.descent_of_row;
99 LyXTable::columnstruct::columnstruct()
103 alignment = LYX_ALIGN_CENTER;
107 LyXTable::columnstruct::~columnstruct()
111 LyXTable::columnstruct &
112 LyXTable::columnstruct::operator=(columnstruct const & cs)
114 left_line = cs.left_line;
115 right_line = cs.right_line;
116 alignment = cs.alignment;
117 width_of_column = cs.width_of_column;
118 p_width = cs.p_width;
119 align_special = cs.align_special;
124 LyXTable::LyXTable(int rows_arg, int columns_arg)
126 Init(rows_arg, columns_arg);
130 LyXTable::LyXTable(LyXTable const & lt)
132 Init(lt.rows, lt.columns);
137 LyXTable::LyXTable(LyXLex & lex)
139 istream & is = lex.getStream();
144 LyXTable::~LyXTable()
147 delete[] columnofcell;
148 delete[] column_info;
150 for (int i = 0; i < rows; ++i) {
151 delete[] cell_info[i];
157 LyXTable & LyXTable::operator=(LyXTable const & lt)
159 // If this and lt is not of the same size we have a serious bug
160 // So then it is ok to throw an exception, or for now
162 Assert(rows == lt.rows && columns == lt.columns);
164 int row = 0, column = 0;
166 for (row = 0; row < rows; ++row) {
167 for (column = 0; column < columns; ++column) {
168 cell_info[row][column] = lt.cell_info[row][column];
172 for (row = 0; row < rows; ++row) {
173 row_info[row] = lt.row_info[row];
176 for (column = 0; column < columns; ++column) {
177 column_info[column] = lt.column_info[column];
180 SetLongTable(lt.is_long_table);
187 LyXTable * LyXTable::Clone()
189 LyXTable * result = new LyXTable(rows, columns);
192 for (row = 0; row < rows; ++row) {
193 for (column = 0; column < columns; ++column) {
194 result->cell_info[row][column] = cell_info[row][column];
198 for (row = 0; row < rows; ++row) {
199 result->row_info[row] = row_info[row];
202 for (column = 0; column < columns; ++column) {
203 result->column_info[column].left_line = column_info[column].left_line;
204 result->column_info[column].right_line = column_info[column].right_line;
205 result->column_info[column].alignment = column_info[column].alignment;
206 result->column_info[column].p_width = column_info[column].p_width;
207 result->column_info[column].align_special = column_info[column].align_special;
210 result->SetLongTable(is_long_table);
211 result->rotate = rotate;
217 /* activates all lines and sets all widths to 0 */
218 void LyXTable::Init(int rows_arg, int columns_arg)
222 columns = columns_arg;
223 column_info = new columnstruct[columns];
224 row_info = new rowstruct[rows];
225 cell_info = new cellstruct*[rows];
228 for (i = 0; i < rows; ++i) {
229 cell_info[i] = new cellstruct[columns];
230 for (j = 0; j < columns; ++j) {
231 cell_info[i][j].cellno = cellno++;
234 row_info[i-1].bottom_line = true;
235 row_info[0].bottom_line = true;
237 for (i = 0; i < columns; ++i) {
238 calculate_width_of_column(i);
240 column_info[i-1].right_line = true;
242 calculate_width_of_table();
246 set_row_column_number_info();
247 is_long_table = false;
256 void LyXTable::AppendRow(int cell)
258 int row = row_of_cell(cell);
259 rowstruct * row_info2 = new rowstruct[rows + 1];
260 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
263 for (i = 0; i <= row; ++i) {
264 cell_info2[i] = cell_info[i];
265 row_info2[i] = row_info[i];
267 for (i = rows - 1; i >= row; --i) {
268 cell_info2[i + 1] = cell_info[i];
269 row_info2[i + 1] = row_info[i];
271 for (i = row; row_info[i].is_cont_row; --i);
272 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
273 row_info2[row + 1].is_cont_row = false;
274 row_info2[row + 1].top_line = row_info[i].top_line;
275 cell_info2[row + 1] = new cellstruct[columns];
276 for (i = 0; i < columns; ++i) {
277 cell_info2[row + 1][i].width_of_cell = 0;
278 cell_info2[row + 1][i] = cell_info2[row][i];
282 cell_info = cell_info2;
284 row_info = row_info2;
292 void LyXTable::DeleteRow(int cell)
294 int row = row_of_cell(cell);
295 while(!row_info[row].is_cont_row && RowHasContRow(cell))
296 DeleteRow(cell_info[row+1][0].cellno);
297 rowstruct * row_info2 = new rowstruct[rows - 1];
298 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
300 delete[] cell_info[row];
302 for (; i < row; ++i) {
303 cell_info2[i] = cell_info[i];
304 row_info2[i] = row_info[i];
306 if (row_info[i].is_cont_row)
307 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
308 for (i = row; i < rows - 1; ++i) {
309 cell_info2[i] = cell_info[i + 1];
310 row_info2[i] = row_info[i + 1];
314 cell_info = cell_info2;
316 row_info = row_info2;
324 void LyXTable::AppendColumn(int cell)
327 columnstruct * column_info2 = new columnstruct[columns + 1];
328 int column = right_column_of_cell(cell);
331 for (; i <= column; ++i) {
332 column_info2[i] = column_info[i];
334 for (i = columns - 1; i >= column; --i) {
335 column_info2[i + 1] = column_info[i];
338 delete[] column_info;
339 column_info = column_info2;
341 for (i = 0; i < rows; ++i) {
342 cellstruct * tmp = cell_info[i];
343 cell_info[i] = new cellstruct[columns + 1];
344 for (j = 0; j <= column; ++j) {
345 cell_info[i][j] = tmp[j];
347 for (j = column; j < columns; ++j) {
348 cell_info[i][j + 1] = tmp[j];
350 // care about multicolumns
351 if (cell_info[i][column + 1].multicolumn
352 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
353 cell_info[i][column + 1].multicolumn =
354 LyXTable::CELL_PART_OF_MULTICOLUMN;
356 if (column + 1 == columns
357 || cell_info[i][column + 2].multicolumn
358 != LyXTable::CELL_PART_OF_MULTICOLUMN){
359 cell_info[i][column + 1].multicolumn =
360 LyXTable::CELL_NORMAL;
370 void LyXTable::Reinit()
375 for (; i < rows; ++i) {
376 for (j = 0; j < columns; ++j) {
377 cell_info[i][j].width_of_cell = 0;
378 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
379 cell_info[i][j].has_cont_row = false;
383 for (i = 0; i < columns; ++i) {
384 calculate_width_of_column(i);
386 calculate_width_of_table();
388 set_row_column_number_info();
392 void LyXTable::set_row_column_number_info()
398 for (; row < rows; ++row) {
399 for (column = 0; column<columns; ++column) {
400 if (cell_info[row][column].multicolumn
401 != LyXTable::CELL_PART_OF_MULTICOLUMN)
403 cell_info[row][column].cellno = numberofcells;
406 ++numberofcells; // because this is one more than as we start from 0
411 rowofcell = new int[numberofcells];
412 delete [] columnofcell;
413 columnofcell = new int[numberofcells];
415 while (c < numberofcells && row < rows && column < columns) {
417 columnofcell[c] = column;
421 } while (column < columns &&
422 cell_info[row][column].multicolumn
423 == LyXTable::CELL_PART_OF_MULTICOLUMN);
424 if (column == columns) {
432 void LyXTable::DeleteColumn(int cell)
434 int column1 = column_of_cell(cell);
435 int column2 = right_column_of_cell(cell);
437 if (column1 == 0 && column2 == columns - 1)
440 for (int column = column1; column <= column2; ++column) {
441 delete_column(column1);
447 int LyXTable::GetNumberOfCells()
449 return numberofcells;
453 int LyXTable::NumberOfCellsInRow(int cell)
455 int row = row_of_cell(cell);
457 for (int i = 0; i < columns; ++i) {
458 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
465 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
467 return (right_column_of_cell(append_cell) ==
468 right_column_of_cell(question_cell));
472 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
474 if (column_of_cell(delete_column_cell) == 0 &&
475 right_column_of_cell(delete_column_cell) == columns - 1)
479 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
480 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
484 /* returns 1 if there is a topline, returns 0 if not */
485 bool LyXTable::TopLine(int cell)
487 int row = row_of_cell(cell);
490 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
491 if (IsMultiColumn(cell))
492 return cellinfo_of_cell(cell)->top_line;
493 return row_info[row].top_line;
497 bool LyXTable::BottomLine(int cell)
499 //no bottom line underneath non-existent cells if you please
500 if(cell >= numberofcells)
503 int row = row_of_cell(cell);
505 if (RowHasContRow(cell))
506 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
507 if (IsMultiColumn(cell))
508 return cellinfo_of_cell(cell)->bottom_line;
509 return row_info[row_of_cell(cell)].bottom_line;
513 bool LyXTable::LeftLine(int cell)
515 return column_info[column_of_cell(cell)].left_line;
519 bool LyXTable::RightLine(int cell)
521 return column_info[right_column_of_cell(cell)].right_line;
525 bool LyXTable::TopAlreadyDrawed(int cell)
527 if (AdditionalHeight(cell))
529 int row = row_of_cell(cell);
531 int column = column_of_cell(cell);
533 && cell_info[row-1][column].multicolumn
534 == LyXTable::CELL_PART_OF_MULTICOLUMN)
536 if (cell_info[row-1][column].multicolumn
537 == LyXTable::CELL_NORMAL)
538 return row_info[row-1].bottom_line;
540 return cell_info[row-1][column].bottom_line;
546 bool LyXTable::VeryLastRow(int cell)
548 return (row_of_cell(cell) == rows - 1);
552 int LyXTable::AdditionalHeight(int cell)
554 int row = row_of_cell(cell);
557 int top = 1; // bool top = true; ??
558 int bottom = 1; // bool bottom = true; ??
561 for (column = 0; column < columns - 1 && bottom; ++column) {
562 switch (cell_info[row - 1][column].multicolumn) {
563 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
564 bottom = cell_info[row - 1][column].bottom_line;
566 case LyXTable::CELL_NORMAL:
567 bottom = row_info[row - 1].bottom_line;
570 for (column = 0; column < columns - 1 && top; ++column) {
571 switch (cell_info[row][column].multicolumn){
572 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
573 top = cell_info[row][column].top_line;
575 case LyXTable::CELL_NORMAL:
576 top = row_info[row].top_line;
580 return WIDTH_OF_LINE;
585 int LyXTable::AdditionalWidth(int cell)
587 // internally already set in SetWidthOfCell
588 // used to get it back in text.C
589 int col = right_column_of_cell(cell);
590 if (col < columns - 1 && column_info[col].right_line &&
591 column_info[col+1].left_line)
592 return WIDTH_OF_LINE;
598 // returns the maximum over all rows
599 int LyXTable::WidthOfColumn(int cell)
601 int column1 = column_of_cell(cell);
602 int column2 = right_column_of_cell(cell);
605 for (; i <= column2; ++i) {
606 result += column_info[i].width_of_column;
612 int LyXTable::WidthOfTable()
614 return width_of_table;
617 /* returns 1 if a complete update is necessary, otherwise 0 */
618 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
620 if (!IsMultiColumn(cell))
623 int row = row_of_cell(cell);
624 int column1 = column_of_cell(cell);
625 int column2 = right_column_of_cell(cell);
627 // first set columns to 0 so we can calculate the right width
629 for (; i <= column2; ++i) {
630 cell_info[row][i].width_of_cell = 0;
632 // set the width to MAX_WIDTH until width > 0
633 int width = (new_width + 2 * WIDTH_OF_LINE);
634 for (i = column1; (i < column2) && (width > 0); ++i) {
635 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
636 width -= column_info[i].width_of_column;
639 cell_info[row][i].width_of_cell = width;
645 void LyXTable::recalculateMulticolCells(int cell, int new_width)
647 int row = row_of_cell(cell);
648 int column1 = column_of_cell(cell);
649 int column2 = right_column_of_cell(cell);
651 // first set columns to 0 so we can calculate the right width
653 for (; i <= column2; ++i)
654 cell_info[row][i].width_of_cell = 0;
655 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
657 if (i < numberofcells)
658 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
659 SetWidthOfMulticolCell(cell, new_width);
663 /* returns 1 if a complete update is necessary, otherwise 0 */
664 bool LyXTable::SetWidthOfCell(int cell, int new_width)
666 int row = row_of_cell(cell);
667 int column1 = column_of_cell(cell);
671 if (IsMultiColumn(cell)) {
672 tmp = SetWidthOfMulticolCell(cell, new_width);
674 width = (new_width + 2*WIDTH_OF_LINE);
675 cell_info[row][column1].width_of_cell = width;
676 if (column_info[column1].right_line && (column1 < columns-1) &&
677 column_info[column1+1].left_line) // additional width
678 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
679 tmp = calculate_width_of_column_NMC(column1);
683 for(i = 0; i<columns;++i)
684 calculate_width_of_column_NMC(i);
685 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
688 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
689 for(i = 0; i<columns;++i)
690 calculate_width_of_column(i);
691 calculate_width_of_table();
698 bool LyXTable::SetAlignment(int cell, char align)
700 if (!IsMultiColumn(cell))
701 column_info[column_of_cell(cell)].alignment = align;
702 cellinfo_of_cell(cell)->alignment = align;
706 bool LyXTable::SetPWidth(int cell, string width)
708 int fvcell = FirstVirtualCell(cell);
710 if (IsMultiColumn(fvcell)) {
711 // if (column_info[column_of_cell(cell)].p_width.empty())
712 // column_info[column_of_cell(cell)].p_width = width;
713 cellinfo_of_cell(fvcell)->p_width = width;
715 column_info[column_of_cell(fvcell)].p_width = width;
716 if (!width.empty()) // do this only if there is a width
717 SetAlignment(cell, LYX_ALIGN_LEFT);
722 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
724 if (what == SET_SPECIAL_MULTI)
725 cellinfo_of_cell(cell)->align_special = special;
727 column_info[column_of_cell(cell)].align_special = special;
731 bool LyXTable::SetAllLines(int cell, bool line)
733 SetTopLine(cell, line);
734 SetBottomLine(cell, line);
735 SetRightLine(cell, line);
736 SetLeftLine(cell, line);
740 bool LyXTable::SetTopLine(int cell, bool line)
742 int row = row_of_cell(cell);
745 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
746 else if (!IsMultiColumn(cell))
747 row_info[row].top_line = line;
749 cellinfo_of_cell(cell)->top_line = line;
754 bool LyXTable::SetBottomLine(int cell, bool line)
756 int row = row_of_cell(cell);
758 if (RowHasContRow(cell))
759 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
760 else if (!IsMultiColumn(cell))
761 row_info[row_of_cell(cell)].bottom_line = line;
763 cellinfo_of_cell(cell)->bottom_line = line;
768 bool LyXTable::SetLeftLine(int cell, bool line)
770 column_info[column_of_cell(cell)].left_line = line;
775 bool LyXTable::SetRightLine(int cell, bool line)
777 column_info[right_column_of_cell(cell)].right_line = line;
782 char LyXTable::GetAlignment(int cell)
784 if (IsMultiColumn(cell))
785 return cellinfo_of_cell(cell)->alignment;
787 return column_info[column_of_cell(cell)].alignment;
790 string LyXTable::GetPWidth(int cell)
792 int fvcell = FirstVirtualCell(cell);
794 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
795 return cellinfo_of_cell(fvcell)->p_width;
796 return column_info[column_of_cell(fvcell)].p_width;
799 string LyXTable::GetAlignSpecial(int cell, int what)
801 if (what == SET_SPECIAL_MULTI)
802 return cellinfo_of_cell(cell)->align_special;
803 return column_info[column_of_cell(cell)].align_special;
806 int LyXTable::GetWidthOfCell(int cell)
808 int row = row_of_cell(cell);
809 int column1 = column_of_cell(cell);
810 int column2 = right_column_of_cell(cell);
813 for (; i <= column2; ++i) {
814 result += cell_info[row][i].width_of_cell;
817 result += AdditionalWidth(cell);
823 int LyXTable::GetBeginningOfTextInCell(int cell)
827 switch (GetAlignment(cell)){
828 case LYX_ALIGN_CENTER:
829 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
831 case LYX_ALIGN_RIGHT:
832 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
834 default: /* LYX_ALIGN_LEFT: nothing :-) */
844 bool LyXTable::IsFirstCell(int cell)
846 return (column_of_cell(cell) == 0);
849 bool LyXTable::IsLastCell(int cell)
851 return (right_column_of_cell(cell) == (columns - 1));
855 bool LyXTable::calculate_width_of_column(int column)
857 int old_column_width = column_info[column].width_of_column;
860 for (int i = 0; i < rows; ++i) {
861 maximum = max(cell_info[i][column].width_of_cell, maximum);
863 column_info[column].width_of_column = maximum;
864 return (column_info[column].width_of_column != old_column_width);
867 bool LyXTable::calculate_width_of_column_NMC(int column)
869 int old_column_width = column_info[column].width_of_column;
871 for (int i = 0; i < rows; ++i) {
872 if (!IsMultiColumn(GetCellNumber(column, i)) &&
873 (cell_info[i][column].width_of_cell > max)) {
874 max = cell_info[i][column].width_of_cell;
877 column_info[column].width_of_column = max;
878 return (column_info[column].width_of_column != old_column_width);
881 void LyXTable::calculate_width_of_table()
884 for (int i = 0; i < columns; ++i) {
885 width_of_table += column_info[i].width_of_column;
890 int LyXTable::row_of_cell(int cell) const
892 if (cell >= numberofcells)
896 return rowofcell[cell];
900 int LyXTable::column_of_cell(int cell) const
902 if (cell >= numberofcells)
906 return columnofcell[cell];
910 int LyXTable::right_column_of_cell(int cell)
912 int row = row_of_cell(cell);
913 int column = column_of_cell(cell);
914 while (column < columns - 1 &&
915 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
921 void LyXTable::Write(ostream & os, bool old_format)
926 << rows << " " << columns << " " << is_long_table << " "
927 << rotate << " " << endhead << " " << endfirsthead << " "
928 << endfoot << " " << endlastfoot << "\n";
929 for (i = 0; i < rows; ++i) {
930 os << row_info[i].top_line << " "
931 << row_info[i].bottom_line << " "
932 << row_info[i].is_cont_row << " "
933 << row_info[i].newpage << "\n";
935 for (i = 0; i < columns; ++i) {
936 os << column_info[i].alignment << " "
937 << column_info[i].left_line << " "
938 << column_info[i].right_line << " \""
939 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
940 << column_info[i].align_special << "\"\n";
943 for (i = 0; i < rows; ++i) {
944 for (j = 0; j < columns; ++j) {
945 os << cell_info[i][j].multicolumn << " "
946 << cell_info[i][j].alignment << " "
947 << cell_info[i][j].top_line << " "
948 << cell_info[i][j].bottom_line << " "
949 << cell_info[i][j].has_cont_row << " "
950 << cell_info[i][j].rotate << " "
951 << cell_info[i][j].linebreaks << " \""
952 << cell_info[i][j].align_special << "\" \""
953 << cell_info[i][j].p_width << "\"\n";
957 lyxerr << "New format type not yet implemented!!!\n" << endl;
962 void LyXTable::Read(istream & is)
968 int is_long_table_arg = false;
969 int rotate_arg = false;
983 version = atoi(s.c_str() + 8);
987 lyxerr << "Tabular format < 5 is not supported anymore\n"
988 "Get an older version of LyX (< 1.1.x) for conversion!"
990 WriteAlert(_("Warning:"),
991 _("Tabular format < 5 is not supported anymore\n"),
992 _("Get an older version of LyX (< 1.1.x) for conversion!"));
994 is >> rows_arg >> columns_arg >> is_long_table_arg
995 >> rotate_arg >> a >> b >> c >> d;
997 is >> rows_arg >> columns_arg;
998 Init(rows_arg, columns_arg);
999 SetLongTable(is_long_table_arg);
1000 SetRotateTable(rotate_arg);
1002 for (i = 0; i < rows; ++i) {
1005 for (i = 0; i < columns; ++i) {
1008 for (i = 0; i < rows; ++i) {
1009 for (j = 0; j < columns; ++j) {
1013 set_row_column_number_info();
1016 is >> rows_arg >> columns_arg >> is_long_table_arg
1017 >> rotate_arg >> a >> b >> c >> d;
1018 Init(rows_arg, columns_arg);
1019 SetLongTable(is_long_table_arg);
1020 SetRotateTable(rotate_arg);
1025 for (i = 0; i < rows; ++i) {
1026 a = b = c = d = e = f = g = h = 0;
1027 is >> a >> b >> c >> d;
1028 row_info[i].top_line = a;
1029 row_info[i].bottom_line = b;
1030 row_info[i].is_cont_row = c;
1031 row_info[i].newpage = d;
1033 for (i = 0; i < columns; ++i) {
1037 char ch; // skip '"'
1039 getline(is, s1, '"');
1040 is >> ch; // skip '"'
1041 getline(is, s2, '"');
1042 column_info[i].alignment = static_cast<char>(a);
1043 column_info[i].left_line = b;
1044 column_info[i].right_line = c;
1045 column_info[i].p_width = s1;
1046 column_info[i].align_special = s2;
1048 for (i = 0; i < rows; ++i) {
1049 for (j = 0; j < columns; ++j) {
1052 is >> a >> b >> c >> d >> e >> f >> g;
1054 is >> ch; // skip '"'
1055 getline(is, s1, '"');
1056 is >> ch; // skip '"'
1057 getline(is, s2, '"');
1058 cell_info[i][j].multicolumn = static_cast<char>(a);
1059 cell_info[i][j].alignment = static_cast<char>(b);
1060 cell_info[i][j].top_line = static_cast<char>(c);
1061 cell_info[i][j].bottom_line = static_cast<char>(d);
1062 cell_info[i][j].has_cont_row = static_cast<bool>(e);
1063 cell_info[i][j].rotate = static_cast<bool>(f);
1064 cell_info[i][j].linebreaks = static_cast<bool>(g);
1065 cell_info[i][j].align_special = s1;
1066 cell_info[i][j].p_width = s2;
1069 set_row_column_number_info();
1073 // cell <0 will tex the preamble
1074 // returns the number of printed newlines
1075 int LyXTable::TexEndOfCell(ostream & os, int cell)
1081 if (ShouldBeVeryLastCell(cell)) {
1082 // the very end at the very beginning
1083 if (Linebreaks(cell))
1084 os << "\\smallskip{}}";
1085 if (IsMultiColumn(cell))
1087 if (RotateCell(cell)) {
1088 os << "\n\\end{sideways}";
1096 while (!IsFirstCell(fcell)) --fcell;
1097 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1098 if (BottomLine(fcell + i))
1101 if (tmp == NumberOfCellsInRow(fcell)) {
1105 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1106 if (BottomLine(fcell + i)) {
1108 << column_of_cell(fcell + i) + 1
1110 << right_column_of_cell(fcell + i) + 1
1121 os << "\\end{longtable}";
1123 os << "\\end{tabular}";
1125 os << "\n\\end{sideways}";
1129 nvcell = NextVirtualCell(cell + 1);
1133 os << "\\begin{sideways}\n";
1137 os << "\\begin{longtable}{";
1139 os << "\\begin{tabular}{";
1140 for (i = 0; i < columns; ++i) {
1141 if (column_info[i].left_line)
1143 if (!column_info[i].align_special.empty()) {
1144 os << column_info[i].align_special;
1145 } else if (!column_info[i].p_width.empty()) {
1147 << column_info[i].p_width
1150 switch (column_info[i].alignment) {
1151 case LYX_ALIGN_LEFT:
1154 case LYX_ALIGN_RIGHT:
1162 if (column_info[i].right_line)
1168 if (GetNumberOfCells()) {
1170 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1171 if (TopLine(fcell + i))
1174 if (tmp == NumberOfCellsInRow(fcell)){
1178 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1179 if (TopLine(fcell + i)) {
1181 << column_of_cell(fcell + i) + 1
1183 << right_column_of_cell(fcell + i) + 1
1194 if (RotateCell(0)) {
1195 os << "\\begin{sideways}\n";
1200 if (Linebreaks(cell))
1201 os << "\\smallskip{}}";
1202 if (IsMultiColumn(cell)){
1205 if (RotateCell(cell)) {
1206 os << "\n\\end{sideways}";
1209 if (IsLastCell(cell)) {
1210 int row = row_of_cell(cell);
1211 string hline1, hline2;
1212 bool print_hline = true;
1213 bool flag1 = IsLongTable() &&
1214 ((row == endhead) || (row == endfirsthead) ||
1215 (row == endfoot) || (row == endlastfoot));
1217 bool flag2 = IsLongTable() &&
1218 ((row <= endhead) || (row <= endfirsthead) ||
1219 (row <= endfoot) || (row <= endlastfoot));
1221 // print the bottom hline only if (otherwise it is doubled):
1222 // - is no LongTable
1223 // - there IS a first-header
1224 // - the next row is no special header/footer
1225 // & this row is no special header/footer
1226 // - the next row is a special header/footer
1227 // & this row is a special header/footer
1228 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1229 (endfirsthead == endhead);
1234 while (!IsFirstCell(fcell))
1236 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1237 if (BottomLine(fcell + i))
1240 if (tmp == NumberOfCellsInRow(cell)){
1242 hline1 = "\\hline ";
1245 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1246 if (BottomLine(fcell + i)){
1248 << column_of_cell(fcell + i) + 1
1250 << right_column_of_cell(fcell + i) + 1
1252 hline1 += "\\cline{";
1253 hline1 += tostr(column_of_cell(fcell + i) + 1);
1255 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1265 if (IsLongTable() && (row == endfoot)) {
1266 os << "\\endfoot\n";
1268 print_hline = false; // no double line below footer
1270 if (IsLongTable() && (row == endlastfoot)) {
1271 os << "\\endlastfoot\n";
1273 print_hline = false; // no double line below footer
1275 if (IsLongTable() && row_info[row].newpage) {
1276 os << "\\newpage\n";
1278 print_hline = false; // no line below a \\newpage-command
1281 if (nvcell < numberofcells
1282 && (cell < GetNumberOfCells() - 1)
1283 && !ShouldBeVeryLastCell(cell)) {
1285 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1286 if (TopLine(fcell + i))
1289 if (tmp == NumberOfCellsInRow(fcell)) {
1292 hline2 = "\\hline ";
1295 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1296 if (TopLine(fcell + i)) {
1299 << column_of_cell(fcell + i) + 1
1301 << right_column_of_cell(fcell + i) + 1
1304 hline2 += "\\cline{";
1305 hline2 += tostr(column_of_cell(fcell+i)+1);
1307 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1313 if (tmp && print_hline){
1318 // the order here is important as if one defines two
1319 // or more things in one line only the first entry is
1320 // displayed the other are set to an empty-row. This
1321 // is important if I have a footer and want that the
1322 // lastfooter is NOT displayed!!!
1323 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1324 (row == endfoot) || (row == endlastfoot);
1326 // sflag2 = IsLongTable() && (row >= 0) &&
1327 // (sflag2 || (row == endhead) || (row == endfirsthead));
1329 bool sflag1 = IsLongTable() && (row != endhead) &&
1330 (row != endfirsthead) &&
1331 ((row == endfoot) || (row == endlastfoot));
1333 if (IsLongTable() && (row == endhead)) {
1334 os << "\\endhead\n";
1337 if (IsLongTable() && (row == endfirsthead)) {
1338 os << "\\endfirsthead\n";
1341 if (sflag1) { // add the \hline for next foot row
1342 if (!hline1.empty()) {
1343 os << hline1 + '\n';
1347 // add the \hline for the first row
1348 if (pr_top_hline && sflag2) {
1349 if (!hline2.empty()) {
1350 os << hline2 + '\n';
1354 if (nvcell < numberofcells && RotateCell(nvcell)) {
1355 os << "\\begin{sideways}\n";
1361 if (nvcell < numberofcells && RotateCell(nvcell)) {
1362 os << "\\begin{sideways}\n";
1367 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1368 os << "\\multicolumn{"
1369 << cells_in_multicolumn(nvcell)
1371 if (!cellinfo_of_cell(nvcell)->align_special.empty()) {
1372 os << cellinfo_of_cell(nvcell)->align_special
1375 if (LeftLine(nvcell))
1377 if (!GetPWidth(nvcell).empty()) {
1379 << GetPWidth(nvcell)
1382 switch (GetAlignment(nvcell)) {
1383 case LYX_ALIGN_LEFT: os << 'l'; break;
1384 case LYX_ALIGN_RIGHT: os << 'r'; break;
1385 default: os << 'c'; break;
1388 if (RightLine(nvcell))
1390 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1391 if (((nvcell + 1) < numberofcells) &&
1392 (NextVirtualCell(nvcell+1) < numberofcells) &&
1393 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1394 LeftLine(NextVirtualCell(nvcell+1)))
1400 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1401 os << "\\parbox[t]{"
1402 << GetPWidth(nvcell)
1403 << "}{\\smallskip{}";
1411 // cell <0 will tex the preamble
1412 // returns the number of printed newlines
1413 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1417 if (cell == GetNumberOfCells() - 1){
1418 // the very end at the very beginning
1419 if (CellHasContRow(cell) >= 0) {
1425 if (row_info[row_of_cell(cell)].bottom_line) {
1429 os << ".TE\n.pl 1c";
1434 os << "\n.pl 500c\n.TS\n";
1435 for (int j = 0; j < rows; ++j) {
1436 for (int i = 0; i < columns; ++i, ++fcell) {
1437 if (column_info[i].left_line)
1439 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1442 switch (column_info[i].alignment) {
1443 case LYX_ALIGN_LEFT:
1446 case LYX_ALIGN_RIGHT:
1454 if (!column_info[i].p_width.empty())
1455 os << "w(" << column_info[i].p_width << ")";
1456 if (column_info[i].right_line)
1459 if ((j + 1) < rows) {
1466 if (row_info[0].top_line) {
1470 if (CellHasContRow(0) >= 0) {
1476 if (CellHasContRow(cell) >= 0) {
1480 if (right_column_of_cell(cell) == columns -1){
1483 int row = row_of_cell(cell);
1484 if (row_info[row++].bottom_line) {
1488 if ((row < rows) && row_info[row].top_line) {
1494 if ((cell < GetNumberOfCells() - 1) &&
1495 (CellHasContRow(cell+1) >= 0)) {
1506 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1512 i = column_of_cell(cell);
1513 if (!isColumn && IsMultiColumn(cell)) {
1514 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1515 return cellinfo_of_cell(cell)->align_special.c_str();
1517 switch (GetAlignment(cell)) {
1518 case LYX_ALIGN_LEFT:
1520 case LYX_ALIGN_RIGHT:
1527 if (!column_info[i].align_special.empty()) {
1528 return column_info[i].align_special.c_str();
1530 #ifdef IGNORE_THIS_FOR_NOW
1531 else if (!column_info[i].p_width.empty()) {
1533 file += column_info[i].p_width;
1538 switch (column_info[i].alignment) {
1539 case LYX_ALIGN_LEFT:
1541 case LYX_ALIGN_RIGHT:
1551 // cell <0 will tex the preamble
1552 // returns the number of printed newlines
1553 int LyXTable::DocBookEndOfCell(ostream & os, int cell, int & depth)
1557 //int tmp; // tmp2; //Â unused
1558 int nvcell; // fcell; //Â unused
1559 if (ShouldBeVeryLastCell(cell)) {
1560 os << newlineAndDepth(--depth)
1562 << newlineAndDepth(--depth)
1564 << newlineAndDepth(--depth)
1566 << newlineAndDepth(--depth);
1571 << newlineAndDepth(--depth);
1574 nvcell = NextVirtualCell(cell + 1);
1583 << "' COLSEP='1' ROWSEP='1'>"
1584 << newlineAndDepth(++depth);
1586 for (i = 0; i < columns; ++i) {
1587 os << "<COLSPEC ALIGN='"
1588 << getDocBookAlign(i, true)
1594 if (i == (columns-1)) {
1597 if (column_info[i].right_line ||
1598 column_info[i+1].left_line)
1604 << newlineAndDepth(depth);
1606 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1607 if (column_info[i].left_line)
1612 << newlineAndDepth(++depth)
1614 << newlineAndDepth(++depth)
1616 << getDocBookAlign(0)
1618 if (IsMultiColumn(0)) {
1619 os << " NAMEST='col1' NAMEEND='col"
1620 << cells_in_multicolumn(0)
1624 << newlineAndDepth(++depth);
1627 if (IsLastCell(cell)) {
1628 os << newlineAndDepth(--depth)
1630 << newlineAndDepth(--depth)
1632 << newlineAndDepth(depth)
1634 << newlineAndDepth(++depth)
1636 << getDocBookAlign(cell + 1)
1637 << "' VALIGN='middle'";
1638 if (IsMultiColumn(cell + 1)) {
1639 os << " NAMEST='col"
1640 << column_of_cell(cell+1) + 1
1642 << column_of_cell(cell + 1) +
1643 cells_in_multicolumn(cell + 1)
1647 << newlineAndDepth(++depth);
1650 os << newlineAndDepth(--depth)
1652 << newlineAndDepth(depth)
1654 << getDocBookAlign(cell + 1)
1655 << "' VALIGN='middle'";
1656 if (IsMultiColumn(cell + 1)) {
1657 os << " NAMEST='col"
1658 << column_of_cell(cell+1) + 1
1660 << column_of_cell(cell+1) +
1661 cells_in_multicolumn(cell+1)
1665 << newlineAndDepth(++depth);
1674 bool LyXTable::IsMultiColumn(int cell)
1676 int fvcell = FirstVirtualCell(cell);
1678 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1682 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1684 int row = row_of_cell(cell);
1685 int column = column_of_cell(cell);
1686 return &cell_info[row][column];
1690 void LyXTable::SetMultiColumn(int cell, int number)
1692 int fvcell = FirstVirtualCell(cell);
1693 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1695 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1696 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1697 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1698 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1699 for (number--; number > 0; --number) {
1700 cellinfo_of_cell(fvcell+number)->multicolumn =
1701 LyXTable::CELL_PART_OF_MULTICOLUMN;
1702 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1704 set_row_column_number_info();
1705 SetWidthOfCell(fvcell, new_width);
1709 int LyXTable::cells_in_multicolumn(int cell)
1711 int row = row_of_cell(cell);
1712 int column = column_of_cell(cell);
1715 while (column < columns && cell_info[row][column].multicolumn
1716 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1724 int LyXTable::UnsetMultiColumn(int cell)
1726 int fvcell = FirstVirtualCell(cell);
1727 int row = row_of_cell(fvcell);
1728 int column = column_of_cell(fvcell);
1732 if (cell_info[row][column].multicolumn
1733 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1734 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1736 while (column < columns &&
1737 cell_info[row][column].multicolumn
1738 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1739 cell_info[row][column].multicolumn =
1740 LyXTable::CELL_NORMAL;
1745 set_row_column_number_info();
1750 void LyXTable::delete_column(int column)
1753 columnstruct * column_info2 = new columnstruct[columns-1];
1755 for (i = 0; i < column; ++i) {
1756 column_info2[i] = column_info[i];
1758 for (i = column; i < columns - 1; ++i) {
1759 column_info2[i] = column_info[i + 1];
1762 delete[] column_info;
1763 column_info = column_info2;
1765 for (i = 0; i < rows; ++i) {
1766 cellstruct * tmp = cell_info[i];
1767 cell_info[i] = new cellstruct[columns - 1];
1768 for (j = 0; j < column; ++j) {
1769 cell_info[i][j] = tmp[j];
1771 for (j = column; j < columns - 1; ++j) {
1772 cell_info[i][j] = tmp[j + 1];
1782 void LyXTable::SetLongTable(int what)
1784 is_long_table = what;
1788 bool LyXTable::IsLongTable()
1790 return is_long_table;
1793 void LyXTable::SetRotateTable(int what)
1798 bool LyXTable::RotateTable()
1803 void LyXTable::SetRotateCell(int cell, int what)
1805 cellinfo_of_cell(cell)->rotate = what;
1808 bool LyXTable::RotateCell(int cell)
1810 return cellinfo_of_cell(cell)->rotate;
1813 bool LyXTable::NeedRotating()
1817 for (int i = 0; i < rows; ++i) {
1818 for (int j = 0; j < columns; ++j) {
1819 if (cell_info[i][j].rotate)
1826 void LyXTable::AppendContRow(int cell)
1828 int row = row_of_cell(cell)+1;
1830 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1832 row_info[row].is_cont_row = true;
1833 row_info[row].top_line = false;
1834 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1838 bool LyXTable::IsContRow(int cell)
1840 return row_info[row_of_cell(cell)].is_cont_row;
1843 int LyXTable::CellHasContRow(int cell)
1845 int row = row_of_cell(cell);
1847 if (VeryLastRow(cell))
1849 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1850 return cell_info[row+1][column_of_cell(cell)].cellno;
1854 bool LyXTable::RowHasContRow(int cell)
1856 int row = row_of_cell(cell) + 1;
1859 return row_info[row].is_cont_row;
1863 int LyXTable::FirstVirtualCell(int cell)
1865 if (!IsContRow(cell))
1867 int row = row_of_cell(cell);
1868 int column = column_of_cell(cell);
1869 for(; (row > 0) && IsContRow(cell_info[row][column].cellno); --row)
1871 return cell_info[row][column].cellno;
1875 int LyXTable::NextVirtualCell(int cell)
1877 if (!IsContRow(cell))
1879 int row = row_of_cell(cell);
1880 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1882 // what if(row >= rows) ?
1883 return cell_info[row][0].cellno;
1887 bool LyXTable::ShouldBeVeryLastCell(int cell)
1888 // "very last cell" ..of what? the row? the table?
1889 // "Cell" in this context appears to not count `virtual' cells
1891 int fcell = cell + 1;
1893 if (cell == GetNumberOfCells() - 1)
1894 return true; // not really sure if I should return false here
1895 if (!IsContRow(fcell))
1897 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1899 if (fcell < GetNumberOfCells() - 1)
1904 bool LyXTable::ShouldBeVeryLastRow(int cell)
1906 if (CellHasContRow(cell)>= 0)
1908 int row = row_of_cell(cell) + 1;
1909 int column = column_of_cell(cell);
1910 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1913 return false; // found another valid row
1914 // I do not have any valid row after the actual
1918 int LyXTable::GetCellAbove(int cell)
1920 int row = row_of_cell(cell);
1923 return cell_info[row-1][column_of_cell(cell)].cellno;
1927 int LyXTable::GetCellNumber(int column, int row)
1929 if (column >= columns)
1930 column = columns - 1;
1931 else if (column < 0)
1938 return cell_info[row][column].cellno;
1941 void LyXTable::SetLinebreaks(int cell, bool what)
1943 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1946 bool LyXTable::Linebreaks(int cell)
1948 int fvcell = FirstVirtualCell(cell);
1950 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1951 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1953 return cellinfo_of_cell(fvcell)->linebreaks;
1956 void LyXTable::SetLTHead(int cell, bool first)
1958 int row = row_of_cell(cell);
1961 if (row == endfirsthead)
1973 bool LyXTable::RowOfLTHead(int cell)
1975 if ((endhead+1) > rows)
1977 return (row_of_cell(cell) == endhead);
1980 bool LyXTable::RowOfLTFirstHead(int cell)
1982 if ((endfirsthead+1) > rows)
1984 return (row_of_cell(cell) == endfirsthead);
1987 void LyXTable::SetLTFoot(int cell, bool last)
1989 int row = row_of_cell(cell);
1992 if (row == endlastfoot)
2004 bool LyXTable::RowOfLTFoot(int cell)
2006 if ((endfoot+1) > rows) {
2010 return (row_of_cell(cell) == endfoot);
2013 bool LyXTable::RowOfLTLastFoot(int cell)
2015 if ((endlastfoot+1) > rows)
2017 return (row_of_cell(cell) == endlastfoot);
2020 void LyXTable::SetLTNewPage(int cell, bool what)
2022 row_info[row_of_cell(cell)].newpage = what;
2025 bool LyXTable::LTNewPage(int cell)
2027 return row_info[row_of_cell(cell)].newpage;
2030 void LyXTable::SetAscentOfRow(int row, int height)
2034 row_info[row].ascent_of_row = height;
2037 void LyXTable::SetDescentOfRow(int row, int height)
2041 row_info[row].descent_of_row = height;
2044 int LyXTable::AscentOfRow(int row)
2048 return row_info[row].ascent_of_row;
2051 int LyXTable::DescentOfRow(int row)
2055 return row_info[row].descent_of_row;
2058 int LyXTable::HeightOfTable()
2064 for(row=0,height=0;(row<rows); ++row)
2065 height += AscentOfRow(row) + DescentOfRow(row) +
2066 AdditionalHeight(GetCellNumber(0,row));
2070 bool LyXTable::IsPartOfMultiColumn(int row, int column)
2072 if ((row >= rows) || (column >= columns))
2074 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
2077 int LyXTable::Latex(ostream &)