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"
35 static int const WIDTH_OF_LINE = 5;
37 /// Define a few methods for the inner structs
39 LyXTable::cellstruct::cellstruct(Buffer * buf)
41 cellno = 0; //should be initilaized correctly later.
43 multicolumn = LyXTable::CELL_NORMAL;
44 alignment = LYX_ALIGN_CENTER;
50 buf ? inset = new InsetText(buf): inset = 0;
54 LyXTable::cellstruct::~cellstruct()
60 LyXTable::cellstruct &
61 LyXTable::cellstruct::operator=(cellstruct const & cs)
64 width_of_cell = cs.width_of_cell;
65 multicolumn = cs.multicolumn;
66 alignment = cs.alignment;
67 top_line = cs.top_line;
68 bottom_line = cs.bottom_line;
69 has_cont_row = cs.has_cont_row;
71 linebreaks = cs.linebreaks;
75 LyXTable::rowstruct::rowstruct()
85 // Nothing to do, but gcc 2.7.2.3 wants one... (JMarc)
86 LyXTable::rowstruct::~rowstruct()
91 LyXTable::rowstruct::operator=(rowstruct const & rs)
93 top_line = rs.top_line;
94 bottom_line = rs.bottom_line;
95 is_cont_row = rs.is_cont_row;
96 ascent_of_row = rs.ascent_of_row;
97 descent_of_row = rs.descent_of_row;
102 LyXTable::columnstruct::columnstruct()
106 alignment = LYX_ALIGN_CENTER;
110 LyXTable::columnstruct::~columnstruct()
114 LyXTable::columnstruct &
115 LyXTable::columnstruct::operator=(columnstruct const & cs)
117 left_line = cs.left_line;
118 right_line = cs.right_line;
119 alignment = cs.alignment;
120 width_of_column = cs.width_of_column;
121 p_width = cs.p_width;
122 align_special = cs.align_special;
127 LyXTable::LyXTable(int rows_arg, int columns_arg, Buffer *buf)
130 Init(rows_arg, columns_arg);
134 LyXTable::LyXTable(LyXTable const & lt, Buffer * buf)
137 Init(lt.rows, lt.columns);
142 LyXTable::LyXTable(LyXLex & lex, Buffer *buf)
144 istream & is = lex.getStream();
150 LyXTable::~LyXTable()
153 delete[] columnofcell;
154 delete[] column_info;
156 for (int i = 0; i < rows; ++i) {
157 delete[] cell_info[i];
163 LyXTable & LyXTable::operator=(LyXTable const & lt)
165 // If this and lt is not of the same size we have a serious bug
166 // So then it is ok to throw an exception, or for now
168 Assert(rows == lt.rows && columns == lt.columns);
170 int row = 0, column = 0;
172 for (row = 0; row < rows; ++row) {
173 for (column = 0; column < columns; ++column) {
174 cell_info[row][column] = lt.cell_info[row][column];
178 for (row = 0; row < rows; ++row) {
179 row_info[row] = lt.row_info[row];
182 for (column = 0; column < columns; ++column) {
183 column_info[column] = lt.column_info[column];
186 SetLongTable(lt.is_long_table);
193 LyXTable * LyXTable::Clone()
195 LyXTable * result = new LyXTable(rows, columns);
198 for (row = 0; row < rows; ++row) {
199 for (column = 0; column < columns; ++column) {
200 result->cell_info[row][column] = cell_info[row][column];
204 for (row = 0; row < rows; ++row) {
205 result->row_info[row] = row_info[row];
208 for (column = 0; column < columns; ++column) {
209 result->column_info[column].left_line = column_info[column].left_line;
210 result->column_info[column].right_line = column_info[column].right_line;
211 result->column_info[column].alignment = column_info[column].alignment;
212 result->column_info[column].p_width = column_info[column].p_width;
213 result->column_info[column].align_special = column_info[column].align_special;
216 result->SetLongTable(is_long_table);
217 result->rotate = rotate;
223 /* activates all lines and sets all widths to 0 */
224 void LyXTable::Init(int rows_arg, int columns_arg)
228 columns = columns_arg;
229 column_info = new columnstruct[columns];
230 row_info = new rowstruct[rows];
231 cell_info = new cellstruct*[rows];
234 for (i = 0; i < rows; ++i) {
235 cell_info[i] = new cellstruct[columns](buffer);
236 for (j = 0; j < columns; ++j) {
237 cell_info[i][j].cellno = cellno++;
240 row_info[i-1].bottom_line = true;
241 row_info[0].bottom_line = true;
243 for (i = 0; i < columns; ++i) {
244 calculate_width_of_column(i);
246 column_info[i-1].right_line = true;
248 calculate_width_of_table();
252 set_row_column_number_info();
253 is_long_table = false;
262 void LyXTable::AppendRow(int cell)
264 int row = row_of_cell(cell);
265 rowstruct * row_info2 = new rowstruct[rows + 1];
266 cellstruct ** cell_info2 = new cellstruct * [rows + 1];
269 for (i = 0; i <= row; ++i) {
270 cell_info2[i] = cell_info[i];
271 row_info2[i] = row_info[i];
273 for (i = rows - 1; i >= row; --i) {
274 cell_info2[i + 1] = cell_info[i];
275 row_info2[i + 1] = row_info[i];
277 for (i = row; row_info[i].is_cont_row; --i);
278 if (((row + 1) >= rows) || !row_info[row + 1].is_cont_row)
279 row_info2[row + 1].is_cont_row = false;
280 row_info2[row + 1].top_line = row_info[i].top_line;
281 cell_info2[row + 1] = new cellstruct[columns];
282 for (i = 0; i < columns; ++i) {
283 cell_info2[row + 1][i].width_of_cell = 0;
284 cell_info2[row + 1][i] = cell_info2[row][i];
288 cell_info = cell_info2;
290 row_info = row_info2;
298 void LyXTable::DeleteRow(int cell)
300 int row = row_of_cell(cell);
301 while(!row_info[row].is_cont_row && RowHasContRow(cell))
302 DeleteRow(cell_info[row+1][0].cellno);
303 rowstruct * row_info2 = new rowstruct[rows - 1];
304 cellstruct ** cell_info2 = new cellstruct * [rows - 1];
306 delete[] cell_info[row];
308 for (; i < row; ++i) {
309 cell_info2[i] = cell_info[i];
310 row_info2[i] = row_info[i];
312 if (row_info[i].is_cont_row)
313 row_info2[i - 1].bottom_line = row_info[i].bottom_line;
314 for (i = row; i < rows - 1; ++i) {
315 cell_info2[i] = cell_info[i + 1];
316 row_info2[i] = row_info[i + 1];
320 cell_info = cell_info2;
322 row_info = row_info2;
330 void LyXTable::AppendColumn(int cell)
333 columnstruct * column_info2 = new columnstruct[columns + 1];
334 int column = right_column_of_cell(cell);
337 for (; i <= column; ++i) {
338 column_info2[i] = column_info[i];
340 for (i = columns - 1; i >= column; --i) {
341 column_info2[i + 1] = column_info[i];
344 delete[] column_info;
345 column_info = column_info2;
347 for (i = 0; i < rows; ++i) {
348 cellstruct * tmp = cell_info[i];
349 cell_info[i] = new cellstruct[columns + 1];
350 for (j = 0; j <= column; ++j) {
351 cell_info[i][j] = tmp[j];
353 for (j = column; j < columns; ++j) {
354 cell_info[i][j + 1] = tmp[j];
356 // care about multicolumns
357 if (cell_info[i][column + 1].multicolumn
358 == LyXTable::CELL_BEGIN_OF_MULTICOLUMN){
359 cell_info[i][column + 1].multicolumn =
360 LyXTable::CELL_PART_OF_MULTICOLUMN;
362 if (column + 1 == columns
363 || cell_info[i][column + 2].multicolumn
364 != LyXTable::CELL_PART_OF_MULTICOLUMN){
365 cell_info[i][column + 1].multicolumn =
366 LyXTable::CELL_NORMAL;
376 void LyXTable::Reinit()
381 for (; i < rows; ++i) {
382 for (j = 0; j < columns; ++j) {
383 cell_info[i][j].width_of_cell = 0;
384 if ((i + 1 < rows) && !row_info[i+1].is_cont_row)
385 cell_info[i][j].has_cont_row = false;
389 for (i = 0; i < columns; ++i) {
390 calculate_width_of_column(i);
392 calculate_width_of_table();
394 set_row_column_number_info();
398 void LyXTable::set_row_column_number_info()
404 for (; row < rows; ++row) {
405 for (column = 0; column<columns; ++column) {
406 if (cell_info[row][column].multicolumn
407 != LyXTable::CELL_PART_OF_MULTICOLUMN)
409 cell_info[row][column].cellno = numberofcells;
412 ++numberofcells; // because this is one more than as we start from 0
417 rowofcell = new int[numberofcells];
418 delete [] columnofcell;
419 columnofcell = new int[numberofcells];
421 while (c < numberofcells && row < rows && column < columns) {
423 columnofcell[c] = column;
427 } while (column < columns &&
428 cell_info[row][column].multicolumn
429 == LyXTable::CELL_PART_OF_MULTICOLUMN);
430 if (column == columns) {
438 void LyXTable::DeleteColumn(int cell)
440 int column1 = column_of_cell(cell);
441 int column2 = right_column_of_cell(cell);
443 if (column1 == 0 && column2 == columns - 1)
446 for (int column = column1; column <= column2; ++column) {
447 delete_column(column1);
453 int LyXTable::GetNumberOfCells()
455 return numberofcells;
459 int LyXTable::NumberOfCellsInRow(int cell)
461 int row = row_of_cell(cell);
463 for (int i = 0; i < columns; ++i) {
464 if (cell_info[row][i].multicolumn != LyXTable::CELL_PART_OF_MULTICOLUMN)
471 int LyXTable::AppendCellAfterCell(int append_cell, int question_cell)
473 return (right_column_of_cell(append_cell) ==
474 right_column_of_cell(question_cell));
478 int LyXTable::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
480 if (column_of_cell(delete_column_cell) == 0 &&
481 right_column_of_cell(delete_column_cell) == columns - 1)
485 (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
486 column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
490 /* returns 1 if there is a topline, returns 0 if not */
491 bool LyXTable::TopLine(int cell)
493 int row = row_of_cell(cell);
496 return TopLine(cell_info[row-1][column_of_cell(cell)].cellno);
497 if (IsMultiColumn(cell))
498 return cellinfo_of_cell(cell)->top_line;
499 return row_info[row].top_line;
503 bool LyXTable::BottomLine(int cell)
505 //no bottom line underneath non-existent cells if you please
506 if(cell >= numberofcells)
509 int row = row_of_cell(cell);
511 if (RowHasContRow(cell))
512 return BottomLine(cell_info[row+1][column_of_cell(cell)].cellno);
513 if (IsMultiColumn(cell))
514 return cellinfo_of_cell(cell)->bottom_line;
515 return row_info[row_of_cell(cell)].bottom_line;
519 bool LyXTable::LeftLine(int cell)
521 return column_info[column_of_cell(cell)].left_line;
525 bool LyXTable::RightLine(int cell)
527 return column_info[right_column_of_cell(cell)].right_line;
531 bool LyXTable::TopAlreadyDrawed(int cell)
533 if (AdditionalHeight(cell))
535 int row = row_of_cell(cell);
537 int column = column_of_cell(cell);
539 && cell_info[row-1][column].multicolumn
540 == LyXTable::CELL_PART_OF_MULTICOLUMN)
542 if (cell_info[row-1][column].multicolumn
543 == LyXTable::CELL_NORMAL)
544 return row_info[row-1].bottom_line;
546 return cell_info[row-1][column].bottom_line;
552 bool LyXTable::VeryLastRow(int cell)
554 return (row_of_cell(cell) == rows - 1);
558 int LyXTable::AdditionalHeight(int cell)
560 int row = row_of_cell(cell);
563 int top = 1; // bool top = true; ??
564 int bottom = 1; // bool bottom = true; ??
567 for (column = 0; column < columns - 1 && bottom; ++column) {
568 switch (cell_info[row - 1][column].multicolumn) {
569 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
570 bottom = cell_info[row - 1][column].bottom_line;
572 case LyXTable::CELL_NORMAL:
573 bottom = row_info[row - 1].bottom_line;
576 for (column = 0; column < columns - 1 && top; ++column) {
577 switch (cell_info[row][column].multicolumn){
578 case LyXTable::CELL_BEGIN_OF_MULTICOLUMN:
579 top = cell_info[row][column].top_line;
581 case LyXTable::CELL_NORMAL:
582 top = row_info[row].top_line;
586 return WIDTH_OF_LINE;
591 int LyXTable::AdditionalWidth(int cell)
593 // internally already set in SetWidthOfCell
594 // used to get it back in text.C
595 int col = right_column_of_cell(cell);
596 if (col < columns - 1 && column_info[col].right_line &&
597 column_info[col+1].left_line)
598 return WIDTH_OF_LINE;
604 // returns the maximum over all rows
605 int LyXTable::WidthOfColumn(int cell)
607 int column1 = column_of_cell(cell);
608 int column2 = right_column_of_cell(cell);
611 for (; i <= column2; ++i) {
612 result += column_info[i].width_of_column;
618 int LyXTable::WidthOfTable()
620 return width_of_table;
623 /* returns 1 if a complete update is necessary, otherwise 0 */
624 bool LyXTable::SetWidthOfMulticolCell(int cell, int new_width)
626 if (!IsMultiColumn(cell))
629 int row = row_of_cell(cell);
630 int column1 = column_of_cell(cell);
631 int column2 = right_column_of_cell(cell);
633 // first set columns to 0 so we can calculate the right width
635 for (; i <= column2; ++i) {
636 cell_info[row][i].width_of_cell = 0;
638 // set the width to MAX_WIDTH until width > 0
639 int width = (new_width + 2 * WIDTH_OF_LINE);
640 for (i = column1; (i < column2) && (width > 0); ++i) {
641 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
642 width -= column_info[i].width_of_column;
645 cell_info[row][i].width_of_cell = width;
651 void LyXTable::recalculateMulticolCells(int cell, int new_width)
653 int row = row_of_cell(cell);
654 int column1 = column_of_cell(cell);
655 int column2 = right_column_of_cell(cell);
657 // first set columns to 0 so we can calculate the right width
659 for (; i <= column2; ++i)
660 cell_info[row][i].width_of_cell = 0;
661 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
663 if (i < numberofcells)
664 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
665 SetWidthOfMulticolCell(cell, new_width);
669 /* returns 1 if a complete update is necessary, otherwise 0 */
670 bool LyXTable::SetWidthOfCell(int cell, int new_width)
672 int row = row_of_cell(cell);
673 int column1 = column_of_cell(cell);
677 if (IsMultiColumn(cell)) {
678 tmp = SetWidthOfMulticolCell(cell, new_width);
680 width = (new_width + 2*WIDTH_OF_LINE);
681 cell_info[row][column1].width_of_cell = width;
682 if (column_info[column1].right_line && (column1 < columns-1) &&
683 column_info[column1+1].left_line) // additional width
684 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
685 tmp = calculate_width_of_column_NMC(column1);
689 for(i = 0; i<columns;++i)
690 calculate_width_of_column_NMC(i);
691 for(i = 0; (i<numberofcells) && !IsMultiColumn(i); ++i)
694 recalculateMulticolCells(i, GetWidthOfCell(i)-(2*WIDTH_OF_LINE));
695 for(i = 0; i<columns;++i)
696 calculate_width_of_column(i);
697 calculate_width_of_table();
704 bool LyXTable::SetAlignment(int cell, char align)
706 if (!IsMultiColumn(cell))
707 column_info[column_of_cell(cell)].alignment = align;
708 cellinfo_of_cell(cell)->alignment = align;
712 bool LyXTable::SetPWidth(int cell, string width)
714 int fvcell = FirstVirtualCell(cell);
716 if (IsMultiColumn(fvcell)) {
717 // if (column_info[column_of_cell(cell)].p_width.empty())
718 // column_info[column_of_cell(cell)].p_width = width;
719 cellinfo_of_cell(fvcell)->p_width = width;
721 column_info[column_of_cell(fvcell)].p_width = width;
722 if (!width.empty()) // do this only if there is a width
723 SetAlignment(cell, LYX_ALIGN_LEFT);
728 bool LyXTable::SetAlignSpecial(int cell, string special, int what)
730 if (what == SET_SPECIAL_MULTI)
731 cellinfo_of_cell(cell)->align_special = special;
733 column_info[column_of_cell(cell)].align_special = special;
737 bool LyXTable::SetAllLines(int cell, bool line)
739 SetTopLine(cell, line);
740 SetBottomLine(cell, line);
741 SetRightLine(cell, line);
742 SetLeftLine(cell, line);
746 bool LyXTable::SetTopLine(int cell, bool line)
748 int row = row_of_cell(cell);
751 SetTopLine(cell_info[row-1][column_of_cell(cell)].cellno, line);
752 else if (!IsMultiColumn(cell))
753 row_info[row].top_line = line;
755 cellinfo_of_cell(cell)->top_line = line;
760 bool LyXTable::SetBottomLine(int cell, bool line)
762 int row = row_of_cell(cell);
764 if (RowHasContRow(cell))
765 SetBottomLine(cell_info[row+1][column_of_cell(cell)].cellno, line);
766 else if (!IsMultiColumn(cell))
767 row_info[row_of_cell(cell)].bottom_line = line;
769 cellinfo_of_cell(cell)->bottom_line = line;
774 bool LyXTable::SetLeftLine(int cell, bool line)
776 column_info[column_of_cell(cell)].left_line = line;
781 bool LyXTable::SetRightLine(int cell, bool line)
783 column_info[right_column_of_cell(cell)].right_line = line;
788 char LyXTable::GetAlignment(int cell)
790 if (IsMultiColumn(cell))
791 return cellinfo_of_cell(cell)->alignment;
793 return column_info[column_of_cell(cell)].alignment;
796 string LyXTable::GetPWidth(int cell)
798 int fvcell = FirstVirtualCell(cell);
800 if (IsMultiColumn(fvcell)) // && !cellinfo_of_cell(cell)->p_width.empty())
801 return cellinfo_of_cell(fvcell)->p_width;
802 return column_info[column_of_cell(fvcell)].p_width;
805 string LyXTable::GetAlignSpecial(int cell, int what)
807 if (what == SET_SPECIAL_MULTI)
808 return cellinfo_of_cell(cell)->align_special;
809 return column_info[column_of_cell(cell)].align_special;
812 int LyXTable::GetWidthOfCell(int cell)
814 int row = row_of_cell(cell);
815 int column1 = column_of_cell(cell);
816 int column2 = right_column_of_cell(cell);
819 for (; i <= column2; ++i) {
820 result += cell_info[row][i].width_of_cell;
823 result += AdditionalWidth(cell);
829 int LyXTable::GetBeginningOfTextInCell(int cell)
833 switch (GetAlignment(cell)){
834 case LYX_ALIGN_CENTER:
835 x += (WidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
837 case LYX_ALIGN_RIGHT:
838 x += WidthOfColumn(cell) - GetWidthOfCell(cell) + AdditionalWidth(cell);
840 default: /* LYX_ALIGN_LEFT: nothing :-) */
850 bool LyXTable::IsFirstCell(int cell)
852 return (column_of_cell(cell) == 0);
855 bool LyXTable::IsLastCell(int cell)
857 return (right_column_of_cell(cell) == (columns - 1));
861 bool LyXTable::calculate_width_of_column(int column)
863 int old_column_width = column_info[column].width_of_column;
866 for (int i = 0; i < rows; ++i) {
867 maximum = max(cell_info[i][column].width_of_cell, maximum);
869 column_info[column].width_of_column = maximum;
870 return (column_info[column].width_of_column != old_column_width);
873 bool LyXTable::calculate_width_of_column_NMC(int column)
875 int old_column_width = column_info[column].width_of_column;
877 for (int i = 0; i < rows; ++i) {
878 if (!IsMultiColumn(GetCellNumber(column, i)) &&
879 (cell_info[i][column].width_of_cell > max)) {
880 max = cell_info[i][column].width_of_cell;
883 column_info[column].width_of_column = max;
884 return (column_info[column].width_of_column != old_column_width);
887 void LyXTable::calculate_width_of_table()
890 for (int i = 0; i < columns; ++i) {
891 width_of_table += column_info[i].width_of_column;
896 int LyXTable::row_of_cell(int cell) const
898 if (cell >= numberofcells)
902 return rowofcell[cell];
906 int LyXTable::column_of_cell(int cell) const
908 if (cell >= numberofcells)
912 return columnofcell[cell];
916 int LyXTable::right_column_of_cell(int cell)
918 int row = row_of_cell(cell);
919 int column = column_of_cell(cell);
920 while (column < columns - 1 &&
921 cell_info[row][column+1].multicolumn == LyXTable::CELL_PART_OF_MULTICOLUMN)
927 void LyXTable::Write(ostream & os, bool old_format)
932 << rows << " " << columns << " " << is_long_table << " "
933 << rotate << " " << endhead << " " << endfirsthead << " "
934 << endfoot << " " << endlastfoot << "\n";
935 for (i = 0; i < rows; ++i) {
936 os << row_info[i].top_line << " "
937 << row_info[i].bottom_line << " "
938 << row_info[i].is_cont_row << " "
939 << row_info[i].newpage << "\n";
941 for (i = 0; i < columns; ++i) {
942 os << column_info[i].alignment << " "
943 << column_info[i].left_line << " "
944 << column_info[i].right_line << " \""
945 << VSpace(column_info[i].p_width).asLyXCommand() << "\" \""
946 << column_info[i].align_special << "\"\n";
949 for (i = 0; i < rows; ++i) {
950 for (j = 0; j < columns; ++j) {
951 os << cell_info[i][j].multicolumn << " "
952 << cell_info[i][j].alignment << " "
953 << cell_info[i][j].top_line << " "
954 << cell_info[i][j].bottom_line << " "
955 << cell_info[i][j].has_cont_row << " "
956 << cell_info[i][j].rotate << " "
957 << cell_info[i][j].linebreaks << " \""
958 << cell_info[i][j].align_special << "\" \""
959 << cell_info[i][j].p_width << "\"\n";
963 lyxerr << "New format type not yet implemented!!!\n" << endl;
968 void LyXTable::Read(istream & is)
974 int is_long_table_arg = false;
975 int rotate_arg = false;
988 version = atoi(s.c_str() + 8);
992 lyxerr << "Tabular format < 5 is not supported anymore\n"
993 "Get an older version of LyX (< 1.1.x) for conversion!"
995 WriteAlert(_("Warning:"),
996 _("Tabular format < 5 is not supported anymore\n"),
997 _("Get an older version of LyX (< 1.1.x) for conversion!"));
999 is >> rows_arg >> columns_arg >> is_long_table_arg
1000 >> rotate_arg >> a >> b >> c >> d;
1002 is >> rows_arg >> columns_arg;
1003 Init(rows_arg, columns_arg);
1004 SetLongTable(is_long_table_arg);
1005 SetRotateTable(rotate_arg);
1007 for (i = 0; i < rows; ++i) {
1010 for (i = 0; i < columns; ++i) {
1013 for (i = 0; i < rows; ++i) {
1014 for (j = 0; j < columns; ++j) {
1018 set_row_column_number_info();
1021 is >> rows_arg >> columns_arg >> is_long_table_arg
1022 >> rotate_arg >> a >> b >> c >> d;
1023 Init(rows_arg, columns_arg);
1024 SetLongTable(is_long_table_arg);
1025 SetRotateTable(rotate_arg);
1030 for (i = 0; i < rows; ++i) {
1031 a = b = c = d = e = f = g = h = 0;
1032 is >> a >> b >> c >> d;
1033 row_info[i].top_line = a;
1034 row_info[i].bottom_line = b;
1035 row_info[i].is_cont_row = c;
1036 row_info[i].newpage = d;
1038 for (i = 0; i < columns; ++i) {
1042 char ch; // skip '"'
1044 getline(is, s1, '"');
1045 is >> ch; // skip '"'
1046 getline(is, s2, '"');
1047 column_info[i].alignment = static_cast<char>(a);
1048 column_info[i].left_line = b;
1049 column_info[i].right_line = c;
1050 column_info[i].p_width = s1;
1051 column_info[i].align_special = s2;
1053 for (i = 0; i < rows; ++i) {
1054 for (j = 0; j < columns; ++j) {
1057 is >> a >> b >> c >> d >> e >> f >> g;
1059 is >> ch; // skip '"'
1060 getline(is, s1, '"');
1061 is >> ch; // skip '"'
1062 getline(is, s2, '"');
1063 cell_info[i][j].multicolumn = static_cast<char>(a);
1064 cell_info[i][j].alignment = static_cast<char>(b);
1065 cell_info[i][j].top_line = static_cast<char>(c);
1066 cell_info[i][j].bottom_line = static_cast<char>(d);
1067 cell_info[i][j].has_cont_row = static_cast<bool>(e);
1068 cell_info[i][j].rotate = static_cast<bool>(f);
1069 cell_info[i][j].linebreaks = static_cast<bool>(g);
1070 cell_info[i][j].align_special = s1;
1071 cell_info[i][j].p_width = s2;
1074 set_row_column_number_info();
1078 // cell <0 will tex the preamble
1079 // returns the number of printed newlines
1080 int LyXTable::TexEndOfCell(ostream & os, int cell)
1086 if (ShouldBeVeryLastCell(cell)) {
1087 // the very end at the very beginning
1088 if (Linebreaks(cell))
1089 os << "\\smallskip{}}";
1090 if (IsMultiColumn(cell))
1092 if (RotateCell(cell)) {
1093 os << "\n\\end{sideways}";
1101 while (!IsFirstCell(fcell)) --fcell;
1102 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1103 if (BottomLine(fcell + i))
1106 if (tmp == NumberOfCellsInRow(fcell)) {
1110 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1111 if (BottomLine(fcell + i)) {
1113 << column_of_cell(fcell + i) + 1
1115 << right_column_of_cell(fcell + i) + 1
1126 os << "\\end{longtable}";
1128 os << "\\end{tabular}";
1130 os << "\n\\end{sideways}";
1134 nvcell = NextVirtualCell(cell + 1);
1138 os << "\\begin{sideways}\n";
1142 os << "\\begin{longtable}{";
1144 os << "\\begin{tabular}{";
1145 for (i = 0; i < columns; ++i) {
1146 if (column_info[i].left_line)
1148 if (!column_info[i].align_special.empty()) {
1149 os << column_info[i].align_special;
1150 } else if (!column_info[i].p_width.empty()) {
1152 << column_info[i].p_width
1155 switch (column_info[i].alignment) {
1156 case LYX_ALIGN_LEFT:
1159 case LYX_ALIGN_RIGHT:
1167 if (column_info[i].right_line)
1173 if (GetNumberOfCells()) {
1175 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1176 if (TopLine(fcell + i))
1179 if (tmp == NumberOfCellsInRow(fcell)){
1183 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1184 if (TopLine(fcell + i)) {
1186 << column_of_cell(fcell + i) + 1
1188 << right_column_of_cell(fcell + i) + 1
1199 if (RotateCell(0)) {
1200 os << "\\begin{sideways}\n";
1205 if (Linebreaks(cell))
1206 os << "\\smallskip{}}";
1207 if (IsMultiColumn(cell)){
1210 if (RotateCell(cell)) {
1211 os << "\n\\end{sideways}";
1214 if (IsLastCell(cell)) {
1215 int row = row_of_cell(cell);
1216 string hline1, hline2;
1217 bool print_hline = true;
1218 bool flag1 = IsLongTable() &&
1219 ((row == endhead) || (row == endfirsthead) ||
1220 (row == endfoot) || (row == endlastfoot));
1222 bool flag2 = IsLongTable() &&
1223 ((row <= endhead) || (row <= endfirsthead) ||
1224 (row <= endfoot) || (row <= endlastfoot));
1226 // print the bottom hline only if (otherwise it is doubled):
1227 // - is no LongTable
1228 // - there IS a first-header
1229 // - the next row is no special header/footer
1230 // & this row is no special header/footer
1231 // - the next row is a special header/footer
1232 // & this row is a special header/footer
1233 bool pr_top_hline = (flag1 && flag2) || (!flag1 && !flag2) ||
1234 (endfirsthead == endhead);
1239 while (!IsFirstCell(fcell))
1241 for (i = 0; i < NumberOfCellsInRow(cell); ++i) {
1242 if (BottomLine(fcell + i))
1245 if (tmp == NumberOfCellsInRow(cell)){
1247 hline1 = "\\hline ";
1250 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1251 if (BottomLine(fcell + i)){
1253 << column_of_cell(fcell + i) + 1
1255 << right_column_of_cell(fcell + i) + 1
1257 hline1 += "\\cline{";
1258 hline1 += tostr(column_of_cell(fcell + i) + 1);
1260 hline1 += tostr(right_column_of_cell(fcell + i) + 1);
1270 if (IsLongTable() && (row == endfoot)) {
1271 os << "\\endfoot\n";
1273 print_hline = false; // no double line below footer
1275 if (IsLongTable() && (row == endlastfoot)) {
1276 os << "\\endlastfoot\n";
1278 print_hline = false; // no double line below footer
1280 if (IsLongTable() && row_info[row].newpage) {
1281 os << "\\newpage\n";
1283 print_hline = false; // no line below a \\newpage-command
1286 if (nvcell < numberofcells
1287 && (cell < GetNumberOfCells() - 1)
1288 && !ShouldBeVeryLastCell(cell)) {
1290 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1291 if (TopLine(fcell + i))
1294 if (tmp == NumberOfCellsInRow(fcell)) {
1297 hline2 = "\\hline ";
1300 for (i = 0; i < NumberOfCellsInRow(fcell); ++i) {
1301 if (TopLine(fcell + i)) {
1304 << column_of_cell(fcell + i) + 1
1306 << right_column_of_cell(fcell + i) + 1
1309 hline2 += "\\cline{";
1310 hline2 += tostr(column_of_cell(fcell+i)+1);
1312 hline2 += tostr(right_column_of_cell(fcell+i)+1);
1318 if (tmp && print_hline){
1323 // the order here is important as if one defines two
1324 // or more things in one line only the first entry is
1325 // displayed the other are set to an empty-row. This
1326 // is important if I have a footer and want that the
1327 // lastfooter is NOT displayed!!!
1328 bool sflag2 = (row == endhead) || (row == endfirsthead) ||
1329 (row == endfoot) || (row == endlastfoot);
1331 // sflag2 = IsLongTable() && (row >= 0) &&
1332 // (sflag2 || (row == endhead) || (row == endfirsthead));
1334 bool sflag1 = IsLongTable() && (row != endhead) &&
1335 (row != endfirsthead) &&
1336 ((row == endfoot) || (row == endlastfoot));
1338 if (IsLongTable() && (row == endhead)) {
1339 os << "\\endhead\n";
1342 if (IsLongTable() && (row == endfirsthead)) {
1343 os << "\\endfirsthead\n";
1346 if (sflag1) { // add the \hline for next foot row
1347 if (!hline1.empty()) {
1348 os << hline1 + '\n';
1352 // add the \hline for the first row
1353 if (pr_top_hline && sflag2) {
1354 if (!hline2.empty()) {
1355 os << hline2 + '\n';
1359 if (nvcell < numberofcells && RotateCell(nvcell)) {
1360 os << "\\begin{sideways}\n";
1366 if (nvcell < numberofcells && RotateCell(nvcell)) {
1367 os << "\\begin{sideways}\n";
1372 if (nvcell < numberofcells && IsMultiColumn(nvcell)) {
1373 os << "\\multicolumn{"
1374 << cells_in_multicolumn(nvcell)
1376 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
1377 os << cellinfo_of_cell(cell+1)->align_special
1380 if (LeftLine(nvcell))
1382 if (!GetPWidth(nvcell).empty()) {
1384 << GetPWidth(nvcell)
1387 switch (GetAlignment(nvcell)) {
1388 case LYX_ALIGN_LEFT: os << 'l'; break;
1389 case LYX_ALIGN_RIGHT: os << 'r'; break;
1390 default: os << 'c'; break;
1393 if (RightLine(nvcell))
1395 //if (column_of_cell(cell+2)!= 0 && LeftLine(cell+2))
1396 if (((nvcell + 1) < numberofcells) &&
1397 (NextVirtualCell(nvcell+1) < numberofcells) &&
1398 (column_of_cell(NextVirtualCell(nvcell+1))!= 0) &&
1399 LeftLine(NextVirtualCell(nvcell+1)))
1405 if (nvcell < numberofcells && Linebreaks(nvcell)) {
1406 // !column_info[column_of_cell(nvcell)].p_width.empty()) {
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;