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 "lyx_gui_misc.h"
28 #include "insets/insettext.h"
36 static int const WIDTH_OF_LINE = 5;
38 /// Define a few methods for the inner structs
40 LyXTable::cellstruct::cellstruct(Buffer * buf)
42 cellno = 0; //should be initilaized correctly later.
44 multicolumn = LyXTable::CELL_NORMAL;
45 alignment = LYX_ALIGN_CENTER;
51 buf ? inset = new InsetText(buf): inset = 0;
55 LyXTable::cellstruct::~cellstruct()
61 LyXTable::cellstruct &
62 LyXTable::cellstruct::operator=(cellstruct const & cs)
65 width_of_cell = cs.width_of_cell;
66 multicolumn = cs.multicolumn;
67 alignment = cs.alignment;
68 top_line = cs.top_line;
69 bottom_line = cs.bottom_line;
70 has_cont_row = cs.has_cont_row;
72 linebreaks = cs.linebreaks;
76 LyXTable::rowstruct::rowstruct()
86 // Nothing to do, but gcc 2.7.2.3 wants one... (JMarc)
87 LyXTable::rowstruct::~rowstruct()
92 LyXTable::rowstruct::operator=(rowstruct const & rs)
94 top_line = rs.top_line;
95 bottom_line = rs.bottom_line;
96 is_cont_row = rs.is_cont_row;
97 ascent_of_row = rs.ascent_of_row;
98 descent_of_row = rs.descent_of_row;
103 LyXTable::columnstruct::columnstruct()
107 alignment = LYX_ALIGN_CENTER;
111 LyXTable::columnstruct::~columnstruct()
115 LyXTable::columnstruct &
116 LyXTable::columnstruct::operator=(columnstruct const & cs)
118 left_line = cs.left_line;
119 right_line = cs.right_line;
120 alignment = cs.alignment;
121 width_of_column = cs.width_of_column;
122 p_width = cs.p_width;
123 align_special = cs.align_special;
128 LyXTable::LyXTable(int rows_arg, int columns_arg, Buffer *buf)
131 Init(rows_arg, columns_arg);
135 LyXTable::LyXTable(LyXTable const & lt, Buffer * buf)
138 Init(lt.rows, lt.columns);
143 LyXTable::LyXTable(LyXLex & lex, Buffer *buf)
145 istream & is = lex.getStream();
151 LyXTable::~LyXTable()
154 delete[] columnofcell;
155 delete[] column_info;
157 for (int i = 0; i < rows; ++i) {
158 delete[] cell_info[i];
164 LyXTable & LyXTable::operator=(LyXTable const & lt)
166 // If this and lt is not of the same size we have a serious bug
167 // So then it is ok to throw an exception, or for now
169 Assert(rows == lt.rows && columns == lt.columns);
171 int row = 0, column = 0;
173 for (row = 0; row < rows; ++row) {
174 for (column = 0; column < columns; ++column) {
175 cell_info[row][column] = lt.cell_info[row][column];
179 for (row = 0; row < rows; ++row) {
180 row_info[row] = lt.row_info[row];
183 for (column = 0; column < columns; ++column) {
184 column_info[column] = lt.column_info[column];
187 SetLongTable(lt.is_long_table);
194 LyXTable * LyXTable::Clone()
196 LyXTable * result = new LyXTable(rows, columns);
199 for (row = 0; row < rows; ++row) {
200 for (column = 0; column < columns; ++column) {
201 result->cell_info[row][column] = cell_info[row][column];
205 for (row = 0; row < rows; ++row) {
206 result->row_info[row] = row_info[row];
209 for (column = 0; column < columns; ++column) {
210 result->column_info[column].left_line = column_info[column].left_line;
211 result->column_info[column].right_line = column_info[column].right_line;
212 result->column_info[column].alignment = column_info[column].alignment;
213 result->column_info[column].p_width = column_info[column].p_width;
214 result->column_info[column].align_special = column_info[column].align_special;
217 result->SetLongTable(is_long_table);
218 result->rotate = rotate;
224 /* activates all lines and sets all widths to 0 */
225 void LyXTable::Init(int rows_arg, int columns_arg)
229 columns = columns_arg;
230 column_info = new columnstruct[columns];
231 row_info = new rowstruct[rows];
232 cell_info = new cellstruct*[rows];
235 for (i = 0; i < rows; ++i) {
236 cell_info[i] = new cellstruct[columns](buffer);
237 for (j = 0; j < columns; ++j) {
238 cell_info[i][j].cellno = cellno++;
241 row_info[i-1].bottom_line = true;
242 row_info[0].bottom_line = true;
244 for (i = 0; i < columns; ++i) {
245 calculate_width_of_column(i);
247 column_info[i-1].right_line = true;
249 calculate_width_of_table();
253 set_row_column_number_info();
254 is_long_table = false;
263 void LyXTable::AppendRow(int cell)
265 int row = row_of_cell(cell);
266 rowstruct * row_info2 = new rowstruct[rows + 1];
267 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
270 for (i = 0; i <= row; ++i) {
271 cell_info2[i] = cell_info[i];
272 row_info2[i] = row_info[i];
274 for (i = rows - 1; i >= row; --i) {
275 cell_info2[i + 1] = cell_info[i];
276 row_info2[i + 1] = row_info[i];
278 for (i = row; row_info[i].is_cont_row; --i);
279 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
280 row_info2[row + 1].is_cont_row = false;
281 row_info2[row + 1].top_line = row_info[i].top_line;
282 cell_info2[row + 1] = new cellstruct[columns];
283 for (i = 0; i < columns; ++i) {
284 cell_info2[row + 1][i].width_of_cell = 0;
285 cell_info2[row + 1][i] = cell_info2[row][i];
289 cell_info = cell_info2;
291 row_info = row_info2;
299 void LyXTable::DeleteRow(int cell)
301 int row = row_of_cell(cell);
302 while(!row_info[row].is_cont_row && RowHasContRow(cell))
303 DeleteRow(cell_info[row+1][0].cellno);
304 rowstruct * row_info2 = new rowstruct[rows - 1];
305 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
307 delete[] cell_info[row];
309 for (; i < row; ++i) {
310 cell_info2[i] = cell_info[i];
311 row_info2[i] = row_info[i];
313 if (row_info[i].is_cont_row)
314 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
315 for (i = row; i < rows - 1; ++i) {
316 cell_info2[i] = cell_info[i + 1];
317 row_info2[i] = row_info[i + 1];
321 cell_info = cell_info2;
323 row_info = row_info2;
331 void LyXTable::AppendColumn(int cell)
334 columnstruct * column_info2 = new columnstruct[columns + 1];
335 int column = right_column_of_cell(cell);
338 for (; i <= column; ++i) {
339 column_info2[i] = column_info[i];
341 for (i = columns - 1; i >= column; --i) {
342 column_info2[i + 1] = column_info[i];
345 delete[] column_info;
346 column_info = column_info2;
348 for (i = 0; i < rows; ++i) {
349 cellstruct * tmp = cell_info[i];
350 cell_info[i] = new cellstruct[columns + 1];
351 for (j = 0; j <= column; ++j) {
352 cell_info[i][j] = tmp[j];
354 for (j = column; j < columns; ++j) {
355 cell_info[i][j + 1] = tmp[j];
357 // care about multicolumns
358 if (cell_info[i][column + 1].multicolumn
359 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
360 cell_info[i][column + 1].multicolumn =
361 LyXTable::CELL_PART_OF_MULTICOLUMN;
363 if (column + 1 == columns
364 || cell_info[i][column + 2].multicolumn
365 != LyXTable::CELL_PART_OF_MULTICOLUMN){
366 cell_info[i][column + 1].multicolumn =
367 LyXTable::CELL_NORMAL;
377 void LyXTable::Reinit()
382 for (; i < rows; ++i) {
383 for (j = 0; j < columns; ++j) {
384 cell_info[i][j].width_of_cell = 0;
385 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
386 cell_info[i][j].has_cont_row = false;
390 for (i = 0; i < columns; ++i) {
391 calculate_width_of_column(i);
393 calculate_width_of_table();
395 set_row_column_number_info();
399 void LyXTable::set_row_column_number_info()
405 for (; row < rows; ++row) {
406 for (column = 0; column<columns; ++column) {
407 if (cell_info[row][column].multicolumn
408 != LyXTable::CELL_PART_OF_MULTICOLUMN)
410 cell_info[row][column].cellno = numberofcells;
413 ++numberofcells; // because this is one more than as we start from 0
418 rowofcell = new int[numberofcells];
419 delete [] columnofcell;
420 columnofcell = new int[numberofcells];
422 while (c < numberofcells && row < rows && column < columns) {
424 columnofcell[c] = column;
428 } while (column < columns &&
429 cell_info[row][column].multicolumn
430 == LyXTable::CELL_PART_OF_MULTICOLUMN);
431 if (column == columns) {
439 void LyXTable::DeleteColumn(int cell)
441 int column1 = column_of_cell(cell);
442 int column2 = right_column_of_cell(cell);
444 if (column1 == 0 && column2 == columns - 1)
447 for (int column = column1; column <= column2; ++column) {
448 delete_column(column1);
454 int LyXTable::GetNumberOfCells()
456 return numberofcells;
460 int LyXTable::NumberOfCellsInRow(int cell)
462 int row = row_of_cell(cell);
464 for (int i = 0; i < columns; ++i) {
465 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
472 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
474 return (right_column_of_cell(append_cell) ==
475 right_column_of_cell(question_cell));
479 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
481 if (column_of_cell(delete_column_cell) == 0 &&
482 right_column_of_cell(delete_column_cell) == columns - 1)
486 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
487 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
491 /* returns 1 if there is a topline, returns 0 if not */
492 bool LyXTable::TopLine(int cell)
494 int row = row_of_cell(cell);
497 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
498 if (IsMultiColumn(cell))
499 return cellinfo_of_cell(cell)->top_line;
500 return row_info[row].top_line;
504 bool LyXTable::BottomLine(int cell)
506 //no bottom line underneath non-existent cells if you please
507 if(cell >= numberofcells)
510 int row = row_of_cell(cell);
512 if (RowHasContRow(cell))
513 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
514 if (IsMultiColumn(cell))
515 return cellinfo_of_cell(cell)->bottom_line;
516 return row_info[row_of_cell(cell)].bottom_line;
520 bool LyXTable::LeftLine(int cell)
522 return column_info[column_of_cell(cell)].left_line;
526 bool LyXTable::RightLine(int cell)
528 return column_info[right_column_of_cell(cell)].right_line;
532 bool LyXTable::TopAlreadyDrawed(int cell)
534 if (AdditionalHeight(cell))
536 int row = row_of_cell(cell);
538 int column = column_of_cell(cell);
540 && cell_info[row-1][column].multicolumn
541 == LyXTable::CELL_PART_OF_MULTICOLUMN)
543 if (cell_info[row-1][column].multicolumn
544 == LyXTable::CELL_NORMAL)
545 return row_info[row-1].bottom_line;
547 return cell_info[row-1][column].bottom_line;
553 bool LyXTable::VeryLastRow(int cell)
555 return (row_of_cell(cell) == rows - 1);
559 int LyXTable::AdditionalHeight(int cell)
561 int row = row_of_cell(cell);
564 int top = 1; // bool top = true; ??
565 int bottom = 1; // bool bottom = true; ??
568 for (column = 0; column < columns - 1 && bottom; ++column) {
569 switch (cell_info[row - 1][column].multicolumn) {
570 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
571 bottom = cell_info[row - 1][column].bottom_line;
573 case LyXTable::CELL_NORMAL:
574 bottom = row_info[row - 1].bottom_line;
577 for (column = 0; column < columns - 1 && top; ++column) {
578 switch (cell_info[row][column].multicolumn){
579 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
580 top = cell_info[row][column].top_line;
582 case LyXTable::CELL_NORMAL:
583 top = row_info[row].top_line;
587 return WIDTH_OF_LINE;
592 int LyXTable::AdditionalWidth(int cell)
594 // internally already set in SetWidthOfCell
595 // used to get it back in text.C
596 int col = right_column_of_cell(cell);
597 if (col < columns - 1 && column_info[col].right_line &&
598 column_info[col+1].left_line)
599 return WIDTH_OF_LINE;
605 // returns the maximum over all rows
606 int LyXTable::WidthOfColumn(int cell)
608 int column1 = column_of_cell(cell);
609 int column2 = right_column_of_cell(cell);
612 for (; i <= column2; ++i) {
613 result += column_info[i].width_of_column;
619 int LyXTable::WidthOfTable()
621 return width_of_table;
624 /* returns 1 if a complete update is necessary, otherwise 0 */
625 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
627 if (!IsMultiColumn(cell))
630 int row = row_of_cell(cell);
631 int column1 = column_of_cell(cell);
632 int column2 = right_column_of_cell(cell);
634 // first set columns to 0 so we can calculate the right width
636 for (; i <= column2; ++i) {
637 cell_info[row][i].width_of_cell = 0;
639 // set the width to MAX_WIDTH until width > 0
640 int width = (new_width + 2 * WIDTH_OF_LINE);
641 for (i = column1; (i < column2) && (width > 0); ++i) {
642 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
643 width -= column_info[i].width_of_column;
646 cell_info[row][i].width_of_cell = width;
652 void LyXTable::recalculateMulticolCells(int cell, int new_width)
654 int row = row_of_cell(cell);
655 int column1 = column_of_cell(cell);
656 int column2 = right_column_of_cell(cell);
658 // first set columns to 0 so we can calculate the right width
660 for (; i <= column2; ++i)
661 cell_info[row][i].width_of_cell = 0;
662 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
664 if (i < numberofcells)
665 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
666 SetWidthOfMulticolCell(cell, new_width);
670 /* returns 1 if a complete update is necessary, otherwise 0 */
671 bool LyXTable::SetWidthOfCell(int cell, int new_width)
673 int row = row_of_cell(cell);
674 int column1 = column_of_cell(cell);
678 if (IsMultiColumn(cell)) {
679 tmp = SetWidthOfMulticolCell(cell, new_width);
681 width = (new_width + 2*WIDTH_OF_LINE);
682 cell_info[row][column1].width_of_cell = width;
683 if (column_info[column1].right_line && (column1 < columns-1) &&
684 column_info[column1+1].left_line) // additional width
685 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
686 tmp = calculate_width_of_column_NMC(column1);
690 for(i = 0; i<columns;++i)
691 calculate_width_of_column_NMC(i);
692 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
695 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
696 for(i = 0; i<columns;++i)
697 calculate_width_of_column(i);
698 calculate_width_of_table();
705 bool LyXTable::SetAlignment(int cell, char align)
707 if (!IsMultiColumn(cell))
708 column_info[column_of_cell(cell)].alignment = align;
709 cellinfo_of_cell(cell)->alignment = align;
713 bool LyXTable::SetPWidth(int cell, string width)
715 int fvcell = FirstVirtualCell(cell);
717 if (IsMultiColumn(fvcell)) {
718 // if (column_info[column_of_cell(cell)].p_width.empty())
719 // column_info[column_of_cell(cell)].p_width = width;
720 cellinfo_of_cell(fvcell)->p_width = width;
722 column_info[column_of_cell(fvcell)].p_width = width;
723 if (!width.empty()) // do this only if there is a width
724 SetAlignment(cell, LYX_ALIGN_LEFT);
729 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
731 if (what == SET_SPECIAL_MULTI)
732 cellinfo_of_cell(cell)->align_special = special;
734 column_info[column_of_cell(cell)].align_special = special;
738 bool LyXTable::SetAllLines(int cell, bool line)
740 SetTopLine(cell, line);
741 SetBottomLine(cell, line);
742 SetRightLine(cell, line);
743 SetLeftLine(cell, line);
747 bool LyXTable::SetTopLine(int cell, bool line)
749 int row = row_of_cell(cell);
752 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
753 else if (!IsMultiColumn(cell))
754 row_info[row].top_line = line;
756 cellinfo_of_cell(cell)->top_line = line;
761 bool LyXTable::SetBottomLine(int cell, bool line)
763 int row = row_of_cell(cell);
765 if (RowHasContRow(cell))
766 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
767 else if (!IsMultiColumn(cell))
768 row_info[row_of_cell(cell)].bottom_line = line;
770 cellinfo_of_cell(cell)->bottom_line = line;
775 bool LyXTable::SetLeftLine(int cell, bool line)
777 column_info[column_of_cell(cell)].left_line = line;
782 bool LyXTable::SetRightLine(int cell, bool line)
784 column_info[right_column_of_cell(cell)].right_line = line;
789 char LyXTable::GetAlignment(int cell)
791 if (IsMultiColumn(cell))
792 return cellinfo_of_cell(cell)->alignment;
794 return column_info[column_of_cell(cell)].alignment;
797 string LyXTable::GetPWidth(int cell)
799 int fvcell = FirstVirtualCell(cell);
801 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
802 return cellinfo_of_cell(fvcell)->p_width;
803 return column_info[column_of_cell(fvcell)].p_width;
806 string LyXTable::GetAlignSpecial(int cell, int what)
808 if (what == SET_SPECIAL_MULTI)
809 return cellinfo_of_cell(cell)->align_special;
810 return column_info[column_of_cell(cell)].align_special;
813 int LyXTable::GetWidthOfCell(int cell)
815 int row = row_of_cell(cell);
816 int column1 = column_of_cell(cell);
817 int column2 = right_column_of_cell(cell);
820 for (; i <= column2; ++i) {
821 result += cell_info[row][i].width_of_cell;
824 result += AdditionalWidth(cell);
830 int LyXTable::GetBeginningOfTextInCell(int cell)
834 switch (GetAlignment(cell)){
835 case LYX_ALIGN_CENTER:
836 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
838 case LYX_ALIGN_RIGHT:
839 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
841 default: /* LYX_ALIGN_LEFT: nothing :-) */
851 bool LyXTable::IsFirstCell(int cell)
853 return (column_of_cell(cell) == 0);
856 bool LyXTable::IsLastCell(int cell)
858 return (right_column_of_cell(cell) == (columns - 1));
862 bool LyXTable::calculate_width_of_column(int column)
864 int old_column_width = column_info[column].width_of_column;
867 for (int i = 0; i < rows; ++i) {
868 maximum = max(cell_info[i][column].width_of_cell, maximum);
870 column_info[column].width_of_column = maximum;
871 return (column_info[column].width_of_column != old_column_width);
874 bool LyXTable::calculate_width_of_column_NMC(int column)
876 int old_column_width = column_info[column].width_of_column;
878 for (int i = 0; i < rows; ++i) {
879 if (!IsMultiColumn(GetCellNumber(column, i)) &&
880 (cell_info[i][column].width_of_cell > max)) {
881 max = cell_info[i][column].width_of_cell;
884 column_info[column].width_of_column = max;
885 return (column_info[column].width_of_column != old_column_width);
888 void LyXTable::calculate_width_of_table()
891 for (int i = 0; i < columns; ++i) {
892 width_of_table += column_info[i].width_of_column;
897 int LyXTable::row_of_cell(int cell) const
899 if (cell >= numberofcells)
903 return rowofcell[cell];
907 int LyXTable::column_of_cell(int cell) const
909 if (cell >= numberofcells)
913 return columnofcell[cell];
917 int LyXTable::right_column_of_cell(int cell)
919 int row = row_of_cell(cell);
920 int column = column_of_cell(cell);
921 while (column < columns - 1 &&
922 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
928 void LyXTable::Write(ostream & os, bool old_format)
933 << rows << " " << columns << " " << is_long_table << " "
934 << rotate << " " << endhead << " " << endfirsthead << " "
935 << endfoot << " " << endlastfoot << "\n";
936 for (i = 0; i < rows; ++i) {
937 os << row_info[i].top_line << " "
938 << row_info[i].bottom_line << " "
939 << row_info[i].is_cont_row << " "
940 << row_info[i].newpage << "\n";
942 for (i = 0; i < columns; ++i) {
943 os << column_info[i].alignment << " "
944 << column_info[i].left_line << " "
945 << column_info[i].right_line << " \""
946 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
947 << column_info[i].align_special << "\"\n";
950 for (i = 0; i < rows; ++i) {
951 for (j = 0; j < columns; ++j) {
952 os << cell_info[i][j].multicolumn << " "
953 << cell_info[i][j].alignment << " "
954 << cell_info[i][j].top_line << " "
955 << cell_info[i][j].bottom_line << " "
956 << cell_info[i][j].has_cont_row << " "
957 << cell_info[i][j].rotate << " "
958 << cell_info[i][j].linebreaks << " \""
959 << cell_info[i][j].align_special << "\" \""
960 << cell_info[i][j].p_width << "\"\n";
964 lyxerr << "New format type not yet implemented!!!\n" << endl;
969 void LyXTable::Read(istream & is)
975 int is_long_table_arg = false;
976 int rotate_arg = false;
989 version = atoi(s.c_str() + 8);
993 lyxerr << "Tabular format < 5 is not supported anymore\n"
994 "Get an older version of LyX (< 1.1.x) for conversion!"
996 WriteAlert(_("Warning:"),
997 _("Tabular format < 5 is not supported anymore\n"),
998 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1000 is >> rows_arg >> columns_arg >> is_long_table_arg
1001 >> rotate_arg >> a >> b >> c >> d;
1003 is >> rows_arg >> columns_arg;
1004 Init(rows_arg, columns_arg);
1005 SetLongTable(is_long_table_arg);
1006 SetRotateTable(rotate_arg);
1008 for (i = 0; i < rows; ++i) {
1011 for (i = 0; i < columns; ++i) {
1014 for (i = 0; i < rows; ++i) {
1015 for (j = 0; j < columns; ++j) {
1019 set_row_column_number_info();
1022 is >> rows_arg >> columns_arg >> is_long_table_arg
1023 >> rotate_arg >> a >> b >> c >> d;
1024 Init(rows_arg, columns_arg);
1025 SetLongTable(is_long_table_arg);
1026 SetRotateTable(rotate_arg);
1031 for (i = 0; i < rows; ++i) {
1032 a = b = c = d = e = f = g = h = 0;
1033 is >> a >> b >> c >> d;
1034 row_info[i].top_line = a;
1035 row_info[i].bottom_line = b;
1036 row_info[i].is_cont_row = c;
1037 row_info[i].newpage = d;
1039 for (i = 0; i < columns; ++i) {
1043 char ch; // skip '"'
1045 getline(is, s1, '"');
1046 is >> ch; // skip '"'
1047 getline(is, s2, '"');
1048 column_info[i].alignment = static_cast<char>(a);
1049 column_info[i].left_line = b;
1050 column_info[i].right_line = c;
1051 column_info[i].p_width = s1;
1052 column_info[i].align_special = s2;
1054 for (i = 0; i < rows; ++i) {
1055 for (j = 0; j < columns; ++j) {
1058 is >> a >> b >> c >> d >> e >> f >> g;
1060 is >> ch; // skip '"'
1061 getline(is, s1, '"');
1062 is >> ch; // skip '"'
1063 getline(is, s2, '"');
1064 cell_info[i][j].multicolumn = static_cast<char>(a);
1065 cell_info[i][j].alignment = static_cast<char>(b);
1066 cell_info[i][j].top_line = static_cast<char>(c);
1067 cell_info[i][j].bottom_line = static_cast<char>(d);
1068 cell_info[i][j].has_cont_row = static_cast<bool>(e);
1069 cell_info[i][j].rotate = static_cast<bool>(f);
1070 cell_info[i][j].linebreaks = static_cast<bool>(g);
1071 cell_info[i][j].align_special = s1;
1072 cell_info[i][j].p_width = s2;
1075 set_row_column_number_info();
1079 // cell <0 will tex the preamble
1080 // returns the number of printed newlines
1081 int LyXTable::TexEndOfCell(ostream & os, int cell)
1087 if (ShouldBeVeryLastCell(cell)) {
1088 // the very end at the very beginning
1089 if (Linebreaks(cell))
1090 os << "\\smallskip{}}";
1091 if (IsMultiColumn(cell))
1093 if (RotateCell(cell)) {
1094 os << "\n\\end{sideways}";
1102 while (!IsFirstCell(fcell)) --fcell;
1103 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1104 if (BottomLine(fcell + i))
1107 if (tmp == NumberOfCellsInRow(fcell)) {
1111 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1112 if (BottomLine(fcell + i)) {
1114 << column_of_cell(fcell + i) + 1
1116 << right_column_of_cell(fcell + i) + 1
1127 os << "\\end{longtable}";
1129 os << "\\end{tabular}";
1131 os << "\n\\end{sideways}";
1135 nvcell = NextVirtualCell(cell + 1);
1139 os << "\\begin{sideways}\n";
1143 os << "\\begin{longtable}{";
1145 os << "\\begin{tabular}{";
1146 for (i = 0; i < columns; ++i) {
1147 if (column_info[i].left_line)
1149 if (!column_info[i].align_special.empty()) {
1150 os << column_info[i].align_special;
1151 } else if (!column_info[i].p_width.empty()) {
1153 << column_info[i].p_width
1156 switch (column_info[i].alignment) {
1157 case LYX_ALIGN_LEFT:
1160 case LYX_ALIGN_RIGHT:
1168 if (column_info[i].right_line)
1174 if (GetNumberOfCells()) {
1176 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1177 if (TopLine(fcell + i))
1180 if (tmp == NumberOfCellsInRow(fcell)){
1184 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1185 if (TopLine(fcell + i)) {
1187 << column_of_cell(fcell + i) + 1
1189 << right_column_of_cell(fcell + i) + 1
1200 if (RotateCell(0)) {
1201 os << "\\begin{sideways}\n";
1206 if (Linebreaks(cell))
1207 os << "\\smallskip{}}";
1208 if (IsMultiColumn(cell)){
1211 if (RotateCell(cell)) {
1212 os << "\n\\end{sideways}";
1215 if (IsLastCell(cell)) {
1216 int row = row_of_cell(cell);
1217 string hline1, hline2;
1218 bool print_hline = true;
1219 bool flag1 = IsLongTable() &&
1220 ((row == endhead) || (row == endfirsthead) ||
1221 (row == endfoot) || (row == endlastfoot));
1223 bool flag2 = IsLongTable() &&
1224 ((row <= endhead) || (row <= endfirsthead) ||
1225 (row <= endfoot) || (row <= endlastfoot));
1227 // print the bottom hline only if (otherwise it is doubled):
1228 // - is no LongTable
1229 // - there IS a first-header
1230 // - the next row is no special header/footer
1231 // & this row is no special header/footer
1232 // - the next row is a special header/footer
1233 // & this row is a special header/footer
1234 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1235 (endfirsthead == endhead);
1240 while (!IsFirstCell(fcell))
1242 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1243 if (BottomLine(fcell + i))
1246 if (tmp == NumberOfCellsInRow(cell)){
1248 hline1 = "\\hline ";
1251 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1252 if (BottomLine(fcell + i)){
1254 << column_of_cell(fcell + i) + 1
1256 << right_column_of_cell(fcell + i) + 1
1258 hline1 += "\\cline{";
1259 hline1 += tostr(column_of_cell(fcell + i) + 1);
1261 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1271 if (IsLongTable() && (row == endfoot)) {
1272 os << "\\endfoot\n";
1274 print_hline = false; // no double line below footer
1276 if (IsLongTable() && (row == endlastfoot)) {
1277 os << "\\endlastfoot\n";
1279 print_hline = false; // no double line below footer
1281 if (IsLongTable() && row_info[row].newpage) {
1282 os << "\\newpage\n";
1284 print_hline = false; // no line below a \\newpage-command
1287 if (nvcell < numberofcells
1288 && (cell < GetNumberOfCells() - 1)
1289 && !ShouldBeVeryLastCell(cell)) {
1291 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1292 if (TopLine(fcell + i))
1295 if (tmp == NumberOfCellsInRow(fcell)) {
1298 hline2 = "\\hline ";
1301 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1302 if (TopLine(fcell + i)) {
1305 << column_of_cell(fcell + i) + 1
1307 << right_column_of_cell(fcell + i) + 1
1310 hline2 += "\\cline{";
1311 hline2 += tostr(column_of_cell(fcell+i)+1);
1313 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1319 if (tmp && print_hline){
1324 // the order here is important as if one defines two
1325 // or more things in one line only the first entry is
1326 // displayed the other are set to an empty-row. This
1327 // is important if I have a footer and want that the
1328 // lastfooter is NOT displayed!!!
1329 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1330 (row == endfoot) || (row == endlastfoot);
1332 // sflag2 = IsLongTable() && (row >= 0) &&
1333 // (sflag2 || (row == endhead) || (row == endfirsthead));
1335 bool sflag1 = IsLongTable() && (row != endhead) &&
1336 (row != endfirsthead) &&
1337 ((row == endfoot) || (row == endlastfoot));
1339 if (IsLongTable() && (row == endhead)) {
1340 os << "\\endhead\n";
1343 if (IsLongTable() && (row == endfirsthead)) {
1344 os << "\\endfirsthead\n";
1347 if (sflag1) { // add the \hline for next foot row
1348 if (!hline1.empty()) {
1349 os << hline1 + '\n';
1353 // add the \hline for the first row
1354 if (pr_top_hline && sflag2) {
1355 if (!hline2.empty()) {
1356 os << hline2 + '\n';
1360 if (nvcell < numberofcells && RotateCell(nvcell)) {
1361 os << "\\begin{sideways}\n";
1367 if (nvcell < numberofcells && RotateCell(nvcell)) {
1368 os << "\\begin{sideways}\n";
1373 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1374 os << "\\multicolumn{"
1375 << cells_in_multicolumn(nvcell)
1377 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1378 os << cellinfo_of_cell(cell+1)->align_special
1381 if (LeftLine(nvcell))
1383 if (!GetPWidth(nvcell).empty()) {
1385 << GetPWidth(nvcell)
1388 switch (GetAlignment(nvcell)) {
1389 case LYX_ALIGN_LEFT: os << 'l'; break;
1390 case LYX_ALIGN_RIGHT: os << 'r'; break;
1391 default: os << 'c'; break;
1394 if (RightLine(nvcell))
1396 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1397 if (((nvcell + 1) < numberofcells) &&
1398 (NextVirtualCell(nvcell+1) < numberofcells) &&
1399 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1400 LeftLine(NextVirtualCell(nvcell+1)))
1406 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1407 os << "\\parbox[t]{"
1408 << GetPWidth(nvcell)
1409 << "}{\\smallskip{}";
1417 // cell <0 will tex the preamble
1418 // returns the number of printed newlines
1419 int LyXTable::RoffEndOfCell(ostream & os, int cell)
1423 if (cell == GetNumberOfCells() - 1){
1424 // the very end at the very beginning
1425 if (CellHasContRow(cell) >= 0) {
1431 if (row_info[row_of_cell(cell)].bottom_line) {
1435 os << ".TE\n.pl 1c";
1440 os << "\n.pl 500c\n.TS\n";
1441 for (int j = 0; j < rows; ++j) {
1442 for (int i = 0; i < columns; ++i, ++fcell) {
1443 if (column_info[i].left_line)
1445 if (cell_info[j][i].multicolumn == CELL_PART_OF_MULTICOLUMN)
1448 switch (column_info[i].alignment) {
1449 case LYX_ALIGN_LEFT:
1452 case LYX_ALIGN_RIGHT:
1460 if (!column_info[i].p_width.empty())
1461 os << "w(" << column_info[i].p_width << ")";
1462 if (column_info[i].right_line)
1465 if ((j + 1) < rows) {
1472 if (row_info[0].top_line) {
1476 if (CellHasContRow(0) >= 0) {
1482 if (CellHasContRow(cell) >= 0) {
1486 if (right_column_of_cell(cell) == columns -1){
1489 int row = row_of_cell(cell);
1490 if (row_info[row++].bottom_line) {
1494 if ((row < rows) && row_info[row].top_line) {
1500 if ((cell < GetNumberOfCells() - 1) &&
1501 (CellHasContRow(cell+1) >= 0)) {
1512 char const *LyXTable::getDocBookAlign(int cell, bool isColumn)
1518 i = column_of_cell(cell);
1519 if (!isColumn && IsMultiColumn(cell)) {
1520 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1521 return cellinfo_of_cell(cell)->align_special.c_str();
1523 switch (GetAlignment(cell)) {
1524 case LYX_ALIGN_LEFT:
1526 case LYX_ALIGN_RIGHT:
1533 if (!column_info[i].align_special.empty()) {
1534 return column_info[i].align_special.c_str();
1536 #ifdef IGNORE_THIS_FOR_NOW
1537 else if (!column_info[i].p_width.empty()) {
1539 file += column_info[i].p_width;
1544 switch (column_info[i].alignment) {
1545 case LYX_ALIGN_LEFT:
1547 case LYX_ALIGN_RIGHT:
1557 // cell <0 will tex the preamble
1558 // returns the number of printed newlines
1559 int LyXTable::DocBookEndOfCell(ostream & os, int cell, int & depth)
1563 //int tmp; // tmp2; //Â unused
1564 int nvcell; // fcell; //Â unused
1565 if (ShouldBeVeryLastCell(cell)) {
1566 os << newlineAndDepth(--depth)
1568 << newlineAndDepth(--depth)
1570 << newlineAndDepth(--depth)
1572 << newlineAndDepth(--depth);
1577 << newlineAndDepth(--depth);
1580 nvcell = NextVirtualCell(cell + 1);
1589 << "' COLSEP='1' ROWSEP='1'>"
1590 << newlineAndDepth(++depth);
1592 for (i = 0; i < columns; ++i) {
1593 os << "<COLSPEC ALIGN='"
1594 << getDocBookAlign(i, true)
1600 if (i == (columns-1)) {
1603 if (column_info[i].right_line ||
1604 column_info[i+1].left_line)
1610 << newlineAndDepth(depth);
1612 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1613 if (column_info[i].left_line)
1618 << newlineAndDepth(++depth)
1620 << newlineAndDepth(++depth)
1622 << getDocBookAlign(0)
1624 if (IsMultiColumn(0)) {
1625 os << " NAMEST='col1' NAMEEND='col"
1626 << cells_in_multicolumn(0)
1630 << newlineAndDepth(++depth);
1633 if (IsLastCell(cell)) {
1634 os << newlineAndDepth(--depth)
1636 << newlineAndDepth(--depth)
1638 << newlineAndDepth(depth)
1640 << newlineAndDepth(++depth)
1642 << getDocBookAlign(cell + 1)
1643 << "' VALIGN='middle'";
1644 if (IsMultiColumn(cell + 1)) {
1645 os << " NAMEST='col"
1646 << column_of_cell(cell+1) + 1
1648 << column_of_cell(cell + 1) +
1649 cells_in_multicolumn(cell + 1)
1653 << newlineAndDepth(++depth);
1656 os << newlineAndDepth(--depth)
1658 << newlineAndDepth(depth)
1660 << getDocBookAlign(cell + 1)
1661 << "' VALIGN='middle'";
1662 if (IsMultiColumn(cell + 1)) {
1663 os << " NAMEST='col"
1664 << column_of_cell(cell+1) + 1
1666 << column_of_cell(cell+1) +
1667 cells_in_multicolumn(cell+1)
1671 << newlineAndDepth(++depth);
1680 bool LyXTable::IsMultiColumn(int cell)
1682 int fvcell = FirstVirtualCell(cell);
1684 return (cellinfo_of_cell(fvcell)->multicolumn != LyXTable::CELL_NORMAL);
1688 LyXTable::cellstruct* LyXTable::cellinfo_of_cell(int cell)
1690 int row = row_of_cell(cell);
1691 int column = column_of_cell(cell);
1692 return &cell_info[row][column];
1696 void LyXTable::SetMultiColumn(int cell, int number)
1698 int fvcell = FirstVirtualCell(cell);
1699 int new_width = cellinfo_of_cell(fvcell)->width_of_cell;
1701 cellinfo_of_cell(fvcell)->multicolumn = LyXTable::CELL_BEGIN_OF_MULTICOLUMN;
1702 cellinfo_of_cell(fvcell)->alignment = column_info[column_of_cell(fvcell)].alignment;
1703 cellinfo_of_cell(fvcell)->top_line = row_info[row_of_cell(fvcell)].top_line;
1704 cellinfo_of_cell(fvcell)->bottom_line = row_info[row_of_cell(fvcell)].bottom_line;
1705 for (number--; number > 0; --number) {
1706 cellinfo_of_cell(fvcell+number)->multicolumn =
1707 LyXTable::CELL_PART_OF_MULTICOLUMN;
1708 new_width += cellinfo_of_cell(fvcell+number)->width_of_cell;
1710 set_row_column_number_info();
1711 SetWidthOfCell(fvcell, new_width);
1715 int LyXTable::cells_in_multicolumn(int cell)
1717 int row = row_of_cell(cell);
1718 int column = column_of_cell(cell);
1721 while (column < columns && cell_info[row][column].multicolumn
1722 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1730 int LyXTable::UnsetMultiColumn(int cell)
1732 int fvcell = FirstVirtualCell(cell);
1733 int row = row_of_cell(fvcell);
1734 int column = column_of_cell(fvcell);
1738 if (cell_info[row][column].multicolumn
1739 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
1740 cell_info[row][column].multicolumn = LyXTable::CELL_NORMAL;
1742 while (column < columns &&
1743 cell_info[row][column].multicolumn
1744 == LyXTable::CELL_PART_OF_MULTICOLUMN){
1745 cell_info[row][column].multicolumn =
1746 LyXTable::CELL_NORMAL;
1751 set_row_column_number_info();
1756 void LyXTable::delete_column(int column)
1759 columnstruct * column_info2 = new columnstruct[columns-1];
1761 for (i = 0; i < column; ++i) {
1762 column_info2[i] = column_info[i];
1764 for (i = column; i < columns - 1; ++i) {
1765 column_info2[i] = column_info[i + 1];
1768 delete[] column_info;
1769 column_info = column_info2;
1771 for (i = 0; i < rows; ++i) {
1772 cellstruct * tmp = cell_info[i];
1773 cell_info[i] = new cellstruct[columns - 1];
1774 for (j = 0; j < column; ++j) {
1775 cell_info[i][j] = tmp[j];
1777 for (j = column; j < columns - 1; ++j) {
1778 cell_info[i][j] = tmp[j + 1];
1788 void LyXTable::SetLongTable(int what)
1790 is_long_table = what;
1794 bool LyXTable::IsLongTable()
1796 return is_long_table;
1799 void LyXTable::SetRotateTable(int what)
1804 bool LyXTable::RotateTable()
1809 void LyXTable::SetRotateCell(int cell, int what)
1811 cellinfo_of_cell(cell)->rotate = what;
1814 bool LyXTable::RotateCell(int cell)
1816 return cellinfo_of_cell(cell)->rotate;
1819 bool LyXTable::NeedRotating()
1823 for (int i = 0; i < rows; ++i) {
1824 for (int j = 0; j < columns; ++j) {
1825 if (cell_info[i][j].rotate)
1832 void LyXTable::AppendContRow(int cell)
1834 int row = row_of_cell(cell)+1;
1836 if (!RowHasContRow(cell) || (CellHasContRow(cell)>= 0))
1838 row_info[row].is_cont_row = true;
1839 row_info[row].top_line = false;
1840 cell_info[row-1][column_of_cell(cell)].has_cont_row = true;
1844 bool LyXTable::IsContRow(int cell)
1846 return row_info[row_of_cell(cell)].is_cont_row;
1849 int LyXTable::CellHasContRow(int cell)
1851 int row = row_of_cell(cell);
1853 if (VeryLastRow(cell))
1855 if (cell_info[row][column_of_cell(cell)].has_cont_row)
1856 return cell_info[row+1][column_of_cell(cell)].cellno;
1860 bool LyXTable::RowHasContRow(int cell)
1862 int row = row_of_cell(cell) + 1;
1865 return row_info[row].is_cont_row;
1869 int LyXTable::FirstVirtualCell(int cell)
1871 if (!IsContRow(cell))
1873 int row = row_of_cell(cell);
1874 int column = column_of_cell(cell);
1875 for(; (row > 0) && IsContRow(cell_info[row][column].cellno); --row)
1877 return cell_info[row][column].cellno;
1881 int LyXTable::NextVirtualCell(int cell)
1883 if (!IsContRow(cell))
1885 int row = row_of_cell(cell);
1886 for(;(row < rows - 1) && IsContRow(cell_info[row][0].cellno); ++row)
1888 // what if(row >= rows) ?
1889 return cell_info[row][0].cellno;
1893 bool LyXTable::ShouldBeVeryLastCell(int cell)
1894 // "very last cell" ..of what? the row? the table?
1895 // "Cell" in this context appears to not count `virtual' cells
1897 int fcell = cell + 1;
1899 if (cell == GetNumberOfCells() - 1)
1900 return true; // not really sure if I should return false here
1901 if (!IsContRow(fcell))
1903 while((fcell < GetNumberOfCells() - 1) && IsContRow(fcell))
1905 if (fcell < GetNumberOfCells() - 1)
1910 bool LyXTable::ShouldBeVeryLastRow(int cell)
1912 if (CellHasContRow(cell)>= 0)
1914 int row = row_of_cell(cell) + 1;
1915 int column = column_of_cell(cell);
1916 while((row < rows) && IsContRow(cell_info[row][column].cellno))
1919 return false; // found another valid row
1920 // I do not have any valid row after the actual
1924 int LyXTable::GetCellAbove(int cell)
1926 int row = row_of_cell(cell);
1929 return cell_info[row-1][column_of_cell(cell)].cellno;
1933 int LyXTable::GetCellNumber(int column, int row)
1935 if (column >= columns)
1936 column = columns - 1;
1937 else if (column < 0)
1944 return cell_info[row][column].cellno;
1947 void LyXTable::SetLinebreaks(int cell, bool what)
1949 cellinfo_of_cell(FirstVirtualCell(cell))->linebreaks = what;
1952 bool LyXTable::Linebreaks(int cell)
1954 int fvcell = FirstVirtualCell(cell);
1956 if (column_info[column_of_cell(fvcell)].p_width.empty() &&
1957 !(IsMultiColumn(fvcell) && !cellinfo_of_cell(fvcell)->p_width.empty()))
1959 return cellinfo_of_cell(fvcell)->linebreaks;
1962 void LyXTable::SetLTHead(int cell, bool first)
1964 int row = row_of_cell(cell);
1967 if (row == endfirsthead)
1979 bool LyXTable::RowOfLTHead(int cell)
1981 if ((endhead+1) > rows)
1983 return (row_of_cell(cell) == endhead);
1986 bool LyXTable::RowOfLTFirstHead(int cell)
1988 if ((endfirsthead+1) > rows)
1990 return (row_of_cell(cell) == endfirsthead);
1993 void LyXTable::SetLTFoot(int cell, bool last)
1995 int row = row_of_cell(cell);
1998 if (row == endlastfoot)
2010 bool LyXTable::RowOfLTFoot(int cell)
2012 if ((endfoot+1) > rows) {
2016 return (row_of_cell(cell) == endfoot);
2019 bool LyXTable::RowOfLTLastFoot(int cell)
2021 if ((endlastfoot+1) > rows)
2023 return (row_of_cell(cell) == endlastfoot);
2026 void LyXTable::SetLTNewPage(int cell, bool what)
2028 row_info[row_of_cell(cell)].newpage = what;
2031 bool LyXTable::LTNewPage(int cell)
2033 return row_info[row_of_cell(cell)].newpage;
2036 void LyXTable::SetAscentOfRow(int row, int height)
2040 row_info[row].ascent_of_row = height;
2043 void LyXTable::SetDescentOfRow(int row, int height)
2047 row_info[row].descent_of_row = height;
2050 int LyXTable::AscentOfRow(int row)
2054 return row_info[row].ascent_of_row;
2057 int LyXTable::DescentOfRow(int row)
2061 return row_info[row].descent_of_row;
2064 int LyXTable::HeightOfTable()
2070 for(row=0,height=0;(row<rows); ++row)
2071 height += AscentOfRow(row) + DescentOfRow(row) +
2072 AdditionalHeight(GetCellNumber(0,row));
2076 bool LyXTable::IsPartOfMultiColumn(int row, int column)
2078 if ((row >= rows) || (column >= columns))
2080 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
2083 int LyXTable::Latex(ostream &)
2088 InsetText * LyXTable::GetCellInset(int cell) const
2090 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;