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;
982 version = atoi(s.c_str() + 8);
986 lyxerr << "Tabular format < 5 is not supported anymore\n"
987 "Get an older version of LyX (< 1.1.x) for conversion!"
989 WriteAlert(_("Warning:"),
990 _("Tabular format < 5 is not supported anymore\n"),
991 _("Get an older version of LyX (< 1.1.x) for conversion!"));
993 is >> rows_arg >> columns_arg >> is_long_table_arg
994 >> rotate_arg >> a >> b >> c >> d;
996 is >> rows_arg >> columns_arg;
997 Init(rows_arg, columns_arg);
998 SetLongTable(is_long_table_arg);
999 SetRotateTable(rotate_arg);
1001 for (i = 0; i < rows; ++i) {
1004 for (i = 0; i < columns; ++i) {
1007 for (i = 0; i < rows; ++i) {
1008 for (j = 0; j < columns; ++j) {
1012 set_row_column_number_info();
1015 is >> rows_arg >> columns_arg >> is_long_table_arg
1016 >> rotate_arg >> a >> b >> c >> d;
1017 Init(rows_arg, columns_arg);
1018 SetLongTable(is_long_table_arg);
1019 SetRotateTable(rotate_arg);
1024 for (i = 0; i < rows; ++i) {
1025 a = b = c = d = e = f = g = h = 0;
1026 is >> a >> b >> c >> d;
1027 row_info[i].top_line = a;
1028 row_info[i].bottom_line = b;
1029 row_info[i].is_cont_row = c;
1030 row_info[i].newpage = d;
1032 for (i = 0; i < columns; ++i) {
1036 char ch; // skip '"'
1038 getline(is, s1, '"');
1039 is >> ch; // skip '"'
1040 getline(is, s2, '"');
1041 column_info[i].alignment = static_cast<char>(a);
1042 column_info[i].left_line = b;
1043 column_info[i].right_line = c;
1044 column_info[i].p_width = s1;
1045 column_info[i].align_special = s2;
1047 for (i = 0; i < rows; ++i) {
1048 for (j = 0; j < columns; ++j) {
1051 is >> a >> b >> c >> d >> e >> f >> g;
1053 is >> ch; // skip '"'
1054 getline(is, s1, '"');
1055 is >> ch; // skip '"'
1056 getline(is, s2, '"');
1057 cell_info[i][j].multicolumn = static_cast<char>(a);
1058 cell_info[i][j].alignment = static_cast<char>(b);
1059 cell_info[i][j].top_line = static_cast<char>(c);
1060 cell_info[i][j].bottom_line = static_cast<char>(d);
1061 cell_info[i][j].has_cont_row = static_cast<bool>(e);
1062 cell_info[i][j].rotate = static_cast<bool>(f);
1063 cell_info[i][j].linebreaks = static_cast<bool>(g);
1064 cell_info[i][j].align_special = s1;
1065 cell_info[i][j].p_width = s2;
1068 set_row_column_number_info();
1072 // cell <0 will tex the preamble
1073 // returns the number of printed newlines
1074 int LyXTable::TexEndOfCell(ostream & os, int cell)
1080 if (ShouldBeVeryLastCell(cell)) {
1081 // the very end at the very beginning
1082 if (Linebreaks(cell))
1083 os << "\\smallskip{}}";
1084 if (IsMultiColumn(cell))
1086 if (RotateCell(cell)) {
1087 os << "\n\\end{sideways}";
1095 while (!IsFirstCell(fcell)) --fcell;
1096 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1097 if (BottomLine(fcell + i))
1100 if (tmp == NumberOfCellsInRow(fcell)) {
1104 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1105 if (BottomLine(fcell + i)) {
1107 << column_of_cell(fcell + i) + 1
1109 << right_column_of_cell(fcell + i) + 1
1120 os << "\\end{longtable}";
1122 os << "\\end{tabular}";
1124 os << "\n\\end{sideways}";
1128 nvcell = NextVirtualCell(cell + 1);
1132 os << "\\begin{sideways}\n";
1136 os << "\\begin{longtable}{";
1138 os << "\\begin{tabular}{";
1139 for (i = 0; i < columns; ++i) {
1140 if (column_info[i].left_line)
1142 if (!column_info[i].align_special.empty()) {
1143 os << column_info[i].align_special;
1144 } else if (!column_info[i].p_width.empty()) {
1146 << column_info[i].p_width
1149 switch (column_info[i].alignment) {
1150 case LYX_ALIGN_LEFT:
1153 case LYX_ALIGN_RIGHT:
1161 if (column_info[i].right_line)
1167 if (GetNumberOfCells()) {
1169 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1170 if (TopLine(fcell + i))
1173 if (tmp == NumberOfCellsInRow(fcell)){
1177 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1178 if (TopLine(fcell + i)) {
1180 << column_of_cell(fcell + i) + 1
1182 << right_column_of_cell(fcell + i) + 1
1193 if (RotateCell(0)) {
1194 os << "\\begin{sideways}\n";
1199 if (Linebreaks(cell))
1200 os << "\\smallskip{}}";
1201 if (IsMultiColumn(cell)){
1204 if (RotateCell(cell)) {
1205 os << "\n\\end{sideways}";
1208 if (IsLastCell(cell)) {
1209 int row = row_of_cell(cell);
1210 string hline1, hline2;
1211 bool print_hline = true;
1212 bool flag1 = IsLongTable() &&
1213 ((row == endhead) || (row == endfirsthead) ||
1214 (row == endfoot) || (row == endlastfoot));
1216 bool flag2 = IsLongTable() &&
1217 ((row <= endhead) || (row <= endfirsthead) ||
1218 (row <= endfoot) || (row <= endlastfoot));
1220 // print the bottom hline only if (otherwise it is doubled):
1221 // - is no LongTable
1222 // - there IS a first-header
1223 // - the next row is no special header/footer
1224 // & this row is no special header/footer
1225 // - the next row is a special header/footer
1226 // & this row is a special header/footer
1227 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1228 (endfirsthead == endhead);
1233 while (!IsFirstCell(fcell))
1235 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1236 if (BottomLine(fcell + i))
1239 if (tmp == NumberOfCellsInRow(cell)){
1241 hline1 = "\\hline ";
1244 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1245 if (BottomLine(fcell + i)){
1247 << column_of_cell(fcell + i) + 1
1249 << right_column_of_cell(fcell + i) + 1
1251 hline1 += "\\cline{";
1252 hline1 += tostr(column_of_cell(fcell + i) + 1);
1254 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1264 if (IsLongTable() && (row == endfoot)) {
1265 os << "\\endfoot\n";
1267 print_hline = false; // no double line below footer
1269 if (IsLongTable() && (row == endlastfoot)) {
1270 os << "\\endlastfoot\n";
1272 print_hline = false; // no double line below footer
1274 if (IsLongTable() && row_info[row].newpage) {
1275 os << "\\newpage\n";
1277 print_hline = false; // no line below a \\newpage-command
1280 if (nvcell < numberofcells
1281 && (cell < GetNumberOfCells() - 1)
1282 && !ShouldBeVeryLastCell(cell)) {
1284 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1285 if (TopLine(fcell + i))
1288 if (tmp == NumberOfCellsInRow(fcell)) {
1291 hline2 = "\\hline ";
1294 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1295 if (TopLine(fcell + i)) {
1298 << column_of_cell(fcell + i) + 1
1300 << right_column_of_cell(fcell + i) + 1
1303 hline2 += "\\cline{";
1304 hline2 += tostr(column_of_cell(fcell+i)+1);
1306 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1312 if (tmp && print_hline){
1317 // the order here is important as if one defines two
1318 // or more things in one line only the first entry is
1319 // displayed the other are set to an empty-row. This
1320 // is important if I have a footer and want that the
1321 // lastfooter is NOT displayed!!!
1322 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1323 (row == endfoot) || (row == endlastfoot);
1325 // sflag2 = IsLongTable() && (row >= 0) &&
1326 // (sflag2 || (row == endhead) || (row == endfirsthead));
1328 bool sflag1 = IsLongTable() && (row != endhead) &&
1329 (row != endfirsthead) &&
1330 ((row == endfoot) || (row == endlastfoot));
1332 if (IsLongTable() && (row == endhead)) {
1333 os << "\\endhead\n";
1336 if (IsLongTable() && (row == endfirsthead)) {
1337 os << "\\endfirsthead\n";
1340 if (sflag1) { // add the \hline for next foot row
1341 if (!hline1.empty()) {
1342 os << hline1 + '\n';
1346 // add the \hline for the first row
1347 if (pr_top_hline && sflag2) {
1348 if (!hline2.empty()) {
1349 os << hline2 + '\n';
1353 if (nvcell < numberofcells && RotateCell(nvcell)) {
1354 os << "\\begin{sideways}\n";
1360 if (nvcell < numberofcells && RotateCell(nvcell)) {
1361 os << "\\begin{sideways}\n";
1366 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1367 os << "\\multicolumn{"
1368 << cells_in_multicolumn(nvcell)
1370 if (!cellinfo_of_cell(nvcell)->align_special.empty()) {
1371 os << cellinfo_of_cell(nvcell)->align_special
1374 if (LeftLine(nvcell))
1376 if (!GetPWidth(nvcell).empty()) {
1378 << GetPWidth(nvcell)
1381 switch (GetAlignment(nvcell)) {
1382 case LYX_ALIGN_LEFT: os << 'l'; break;
1383 case LYX_ALIGN_RIGHT: os << 'r'; break;
1384 default: os << 'c'; break;
1387 if (RightLine(nvcell))
1389 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1390 if (((nvcell + 1) < numberofcells) &&
1391 (NextVirtualCell(nvcell+1) < numberofcells) &&
1392 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1393 LeftLine(NextVirtualCell(nvcell+1)))
1399 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1400 os << "\\parbox[t]{"
1401 << GetPWidth(nvcell)
1402 << "}{\\smallskip{}";
1410 // cell <0 will tex the preamble
1411 // returns the number of printed newlines
1412 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1416 if (cell == GetNumberOfCells() - 1){
1417 // the very end at the very beginning
1418 if (CellHasContRow(cell) >= 0) {
1424 if (row_info[row_of_cell(cell)].bottom_line) {
1428 os << ".TE\n.pl 1c";
1433 os << "\n.pl 500c\n.TS\n";
1434 for (int j = 0; j < rows; ++j) {
1435 for (int i = 0; i < columns; ++i, ++fcell) {
1436 if (column_info[i].left_line)
1438 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1441 switch (column_info[i].alignment) {
1442 case LYX_ALIGN_LEFT:
1445 case LYX_ALIGN_RIGHT:
1453 if (!column_info[i].p_width.empty())
1454 os << "w(" << column_info[i].p_width << ")";
1455 if (column_info[i].right_line)
1458 if ((j + 1) < rows) {
1465 if (row_info[0].top_line) {
1469 if (CellHasContRow(0) >= 0) {
1475 if (CellHasContRow(cell) >= 0) {
1479 if (right_column_of_cell(cell) == columns -1){
1482 int row = row_of_cell(cell);
1483 if (row_info[row++].bottom_line) {
1487 if ((row < rows) && row_info[row].top_line) {
1493 if ((cell < GetNumberOfCells() - 1) &&
1494 (CellHasContRow(cell+1) >= 0)) {
1505 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1511 i = column_of_cell(cell);
1512 if (!isColumn && IsMultiColumn(cell)) {
1513 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1514 return cellinfo_of_cell(cell)->align_special.c_str();
1516 switch (GetAlignment(cell)) {
1517 case LYX_ALIGN_LEFT:
1519 case LYX_ALIGN_RIGHT:
1526 if (!column_info[i].align_special.empty()) {
1527 return column_info[i].align_special.c_str();
1529 #ifdef IGNORE_THIS_FOR_NOW
1530 else if (!column_info[i].p_width.empty()) {
1532 file += column_info[i].p_width;
1537 switch (column_info[i].alignment) {
1538 case LYX_ALIGN_LEFT:
1540 case LYX_ALIGN_RIGHT:
1550 // cell <0 will tex the preamble
1551 // returns the number of printed newlines
1552 int LyXTable::DocBookEndOfCell(ostream & os, int cell, int & depth)
1556 //int tmp; // tmp2; //Â unused
1557 int nvcell; // fcell; //Â unused
1558 if (ShouldBeVeryLastCell(cell)) {
1559 os << newlineAndDepth(--depth)
1561 << newlineAndDepth(--depth)
1563 << newlineAndDepth(--depth)
1565 << newlineAndDepth(--depth);
1570 << newlineAndDepth(--depth);
1573 nvcell = NextVirtualCell(cell + 1);
1582 << "' COLSEP='1' ROWSEP='1'>"
1583 << newlineAndDepth(++depth);
1585 for (i = 0; i < columns; ++i) {
1586 os << "<COLSPEC ALIGN='"
1587 << getDocBookAlign(i, true)
1593 if (i == (columns-1)) {
1596 if (column_info[i].right_line ||
1597 column_info[i+1].left_line)
1603 << newlineAndDepth(depth);
1605 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1606 if (column_info[i].left_line)
1611 << newlineAndDepth(++depth)
1613 << newlineAndDepth(++depth)
1615 << getDocBookAlign(0)
1617 if (IsMultiColumn(0)) {
1618 os << " NAMEST='col1' NAMEEND='col"
1619 << cells_in_multicolumn(0)
1623 << newlineAndDepth(++depth);
1626 if (IsLastCell(cell)) {
1627 os << newlineAndDepth(--depth)
1629 << newlineAndDepth(--depth)
1631 << newlineAndDepth(depth)
1633 << newlineAndDepth(++depth)
1635 << getDocBookAlign(cell + 1)
1636 << "' VALIGN='middle'";
1637 if (IsMultiColumn(cell + 1)) {
1638 os << " NAMEST='col"
1639 << column_of_cell(cell+1) + 1
1641 << column_of_cell(cell + 1) +
1642 cells_in_multicolumn(cell + 1)
1646 << newlineAndDepth(++depth);
1649 os << newlineAndDepth(--depth)
1651 << newlineAndDepth(depth)
1653 << getDocBookAlign(cell + 1)
1654 << "' VALIGN='middle'";
1655 if (IsMultiColumn(cell + 1)) {
1656 os << " NAMEST='col"
1657 << column_of_cell(cell+1) + 1
1659 << column_of_cell(cell+1) +
1660 cells_in_multicolumn(cell+1)
1664 << newlineAndDepth(++depth);
1673 bool LyXTable::IsMultiColumn(int cell)
1675 int fvcell = FirstVirtualCell(cell);
1677 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1681 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1683 int row = row_of_cell(cell);
1684 int column = column_of_cell(cell);
1685 return &cell_info[row][column];
1689 void LyXTable::SetMultiColumn(int cell, int number)
1691 int fvcell = FirstVirtualCell(cell);
1692 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1694 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1695 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1696 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1697 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1698 for (number--; number > 0; --number) {
1699 cellinfo_of_cell(fvcell+number)->multicolumn =
1700 LyXTable::CELL_PART_OF_MULTICOLUMN;
1701 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1703 set_row_column_number_info();
1704 SetWidthOfCell(fvcell, new_width);
1708 int LyXTable::cells_in_multicolumn(int cell)
1710 int row = row_of_cell(cell);
1711 int column = column_of_cell(cell);
1714 while (column < columns && cell_info[row][column].multicolumn
1715 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1723 int LyXTable::UnsetMultiColumn(int cell)
1725 int fvcell = FirstVirtualCell(cell);
1726 int row = row_of_cell(fvcell);
1727 int column = column_of_cell(fvcell);
1731 if (cell_info[row][column].multicolumn
1732 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1733 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1735 while (column < columns &&
1736 cell_info[row][column].multicolumn
1737 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1738 cell_info[row][column].multicolumn =
1739 LyXTable::CELL_NORMAL;
1744 set_row_column_number_info();
1749 void LyXTable::delete_column(int column)
1752 columnstruct * column_info2 = new columnstruct[columns-1];
1754 for (i = 0; i < column; ++i) {
1755 column_info2[i] = column_info[i];
1757 for (i = column; i < columns - 1; ++i) {
1758 column_info2[i] = column_info[i + 1];
1761 delete[] column_info;
1762 column_info = column_info2;
1764 for (i = 0; i < rows; ++i) {
1765 cellstruct * tmp = cell_info[i];
1766 cell_info[i] = new cellstruct[columns - 1];
1767 for (j = 0; j < column; ++j) {
1768 cell_info[i][j] = tmp[j];
1770 for (j = column; j < columns - 1; ++j) {
1771 cell_info[i][j] = tmp[j + 1];
1781 void LyXTable::SetLongTable(int what)
1783 is_long_table = what;
1787 bool LyXTable::IsLongTable()
1789 return is_long_table;
1792 void LyXTable::SetRotateTable(int what)
1797 bool LyXTable::RotateTable()
1802 void LyXTable::SetRotateCell(int cell, int what)
1804 cellinfo_of_cell(cell)->rotate = what;
1807 bool LyXTable::RotateCell(int cell)
1809 return cellinfo_of_cell(cell)->rotate;
1812 bool LyXTable::NeedRotating()
1816 for (int i = 0; i < rows; ++i) {
1817 for (int j = 0; j < columns; ++j) {
1818 if (cell_info[i][j].rotate)
1825 void LyXTable::AppendContRow(int cell)
1827 int row = row_of_cell(cell)+1;
1829 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1831 row_info[row].is_cont_row = true;
1832 row_info[row].top_line = false;
1833 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1837 bool LyXTable::IsContRow(int cell)
1839 return row_info[row_of_cell(cell)].is_cont_row;
1842 int LyXTable::CellHasContRow(int cell)
1844 int row = row_of_cell(cell);
1846 if (VeryLastRow(cell))
1848 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1849 return cell_info[row+1][column_of_cell(cell)].cellno;
1853 bool LyXTable::RowHasContRow(int cell)
1855 int row = row_of_cell(cell) + 1;
1858 return row_info[row].is_cont_row;
1862 int LyXTable::FirstVirtualCell(int cell)
1864 if (!IsContRow(cell))
1866 int row = row_of_cell(cell);
1867 int column = column_of_cell(cell);
1868 for(; (row > 0) && IsContRow(cell_info[row][column].cellno); --row)
1870 return cell_info[row][column].cellno;
1874 int LyXTable::NextVirtualCell(int cell)
1876 if (!IsContRow(cell))
1878 int row = row_of_cell(cell);
1879 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1881 // what if(row >= rows) ?
1882 return cell_info[row][0].cellno;
1886 bool LyXTable::ShouldBeVeryLastCell(int cell)
1887 // "very last cell" ..of what? the row? the table?
1888 // "Cell" in this context appears to not count `virtual' cells
1890 int fcell = cell + 1;
1892 if (cell == GetNumberOfCells() - 1)
1893 return true; // not really sure if I should return false here
1894 if (!IsContRow(fcell))
1896 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1898 if (fcell < GetNumberOfCells() - 1)
1903 bool LyXTable::ShouldBeVeryLastRow(int cell)
1905 if (CellHasContRow(cell)>= 0)
1907 int row = row_of_cell(cell) + 1;
1908 int column = column_of_cell(cell);
1909 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1912 return false; // found another valid row
1913 // I do not have any valid row after the actual
1917 int LyXTable::GetCellAbove(int cell)
1919 int row = row_of_cell(cell);
1922 return cell_info[row-1][column_of_cell(cell)].cellno;
1926 int LyXTable::GetCellNumber(int column, int row)
1928 if (column >= columns)
1929 column = columns - 1;
1930 else if (column < 0)
1937 return cell_info[row][column].cellno;
1940 void LyXTable::SetLinebreaks(int cell, bool what)
1942 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1945 bool LyXTable::Linebreaks(int cell)
1947 int fvcell = FirstVirtualCell(cell);
1949 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1950 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1952 return cellinfo_of_cell(fvcell)->linebreaks;
1955 void LyXTable::SetLTHead(int cell, bool first)
1957 int row = row_of_cell(cell);
1960 if (row == endfirsthead)
1972 bool LyXTable::RowOfLTHead(int cell)
1974 if ((endhead+1) > rows)
1976 return (row_of_cell(cell) == endhead);
1979 bool LyXTable::RowOfLTFirstHead(int cell)
1981 if ((endfirsthead+1) > rows)
1983 return (row_of_cell(cell) == endfirsthead);
1986 void LyXTable::SetLTFoot(int cell, bool last)
1988 int row = row_of_cell(cell);
1991 if (row == endlastfoot)
2003 bool LyXTable::RowOfLTFoot(int cell)
2005 if ((endfoot+1) > rows) {
2009 return (row_of_cell(cell) == endfoot);
2012 bool LyXTable::RowOfLTLastFoot(int cell)
2014 if ((endlastfoot+1) > rows)
2016 return (row_of_cell(cell) == endlastfoot);
2019 void LyXTable::SetLTNewPage(int cell, bool what)
2021 row_info[row_of_cell(cell)].newpage = what;
2024 bool LyXTable::LTNewPage(int cell)
2026 return row_info[row_of_cell(cell)].newpage;
2029 void LyXTable::SetAscentOfRow(int row, int height)
2033 row_info[row].ascent_of_row = height;
2036 void LyXTable::SetDescentOfRow(int row, int height)
2040 row_info[row].descent_of_row = height;
2043 int LyXTable::AscentOfRow(int row)
2047 return row_info[row].ascent_of_row;
2050 int LyXTable::DescentOfRow(int row)
2054 return row_info[row].descent_of_row;
2057 int LyXTable::HeightOfTable()
2063 for(row=0,height=0;(row<rows); ++row)
2064 height += AscentOfRow(row) + DescentOfRow(row) +
2065 AdditionalHeight(GetCellNumber(0,row));
2069 bool LyXTable::IsPartOfMultiColumn(int row, int column)
2071 if ((row >= rows) || (column >= columns))
2073 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
2076 int LyXTable::Latex(ostream &)