1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
26 #include "lyx_gui_misc.h"
28 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
44 #ifndef CXX_GLOBAL_CSTD
50 int const WIDTH_OF_LINE = 5;
54 /// Define a few methods for the inner structs
56 LyXTabular::cellstruct::cellstruct()
60 multicolumn = LyXTabular::CELL_NORMAL;
61 alignment = LYX_ALIGN_CENTER;
62 valignment = LYX_VALIGN_TOP;
72 LyXTabular::rowstruct::rowstruct()
82 LyXTabular::columnstruct::columnstruct()
86 alignment = LYX_ALIGN_CENTER;
87 valignment = LYX_VALIGN_TOP;
93 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
97 Init(rows_arg, columns_arg);
101 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
105 Init(lt.rows_, lt.columns_, <);
108 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
115 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
123 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
126 #warning This while method should look like this: (Lgb)
131 // If this and lt is not of the same size we have a serious bug
132 // So then it is ok to throw an exception, or for now
134 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
136 cell_info = lt.cell_info;
137 row_info = lt.row_info;
138 column_info = lt.column_info;
140 // long tabular stuff
141 SetLongTabular(lt.is_long_tabular);
142 endhead = lt.endhead;
143 endfoot = lt.endfoot;
144 endfirsthead = lt.endfirsthead;
145 endlastfoot = lt.endlastfoot;
155 LyXTabular * LyXTabular::clone(InsetTabular * inset)
157 LyXTabular * result = new LyXTabular(inset, *this);
159 // don't know if this is good but I need to Clone also
160 // the text-insets here, this is for the Undo-facility!
161 for (int i = 0; i < rows_; ++i) {
162 for (int j = 0; j < columns_; ++j) {
163 result->cell_info[i][j].inset = cell_info[i][j].inset;
164 result->cell_info[i][j].inset.setOwner(inset);
172 /* activates all lines and sets all widths to 0 */
173 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
176 columns_ = columns_arg;
177 row_info = row_vector(rows_, rowstruct());
178 column_info = column_vector(columns_, columnstruct());
179 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
187 for (int i = 0; i < rows_; ++i) {
188 for (int j = 0; j < columns_; ++j) {
189 cell_info[i][j].inset.setOwner(owner_);
190 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
191 cell_info[i][j].cellno = cellno++;
193 cell_info[i].back().right_line = true;
195 row_info.back().bottom_line = true;
196 row_info.front().bottom_line = true;
198 for (int i = 0; i < columns_; ++i) {
199 calculate_width_of_column(i);
201 column_info.back().right_line = true;
203 calculate_width_of_tabular();
205 rowofcell = vector<int>();
206 columnofcell = vector<int>();
207 set_row_column_number_info();
208 is_long_tabular = false;
217 void LyXTabular::AppendRow(int cell)
221 int row = row_of_cell(cell);
223 row_vector::iterator rit = row_info.begin() + row;
224 row_info.insert(rit, rowstruct());
225 // now set the values of the row before
226 row_info[row] = row_info[row+1];
229 cell_vvector::iterator cit = cell_info.begin() + row;
230 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
232 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
235 for (int i = 0; i <= row; ++i) {
236 for (int j = 0; j < columns_; ++j) {
237 c_info[i][j] = cell_info[i][j];
240 for (int i = row + 1; i < rows_; ++i) {
241 for (int j = 0; j < columns_; ++j) {
242 c_info[i][j] = cell_info[i-1][j];
247 for (int j = 0; j < columns_; ++j) {
248 cell_info[row][j].inset.clear();
255 void LyXTabular::DeleteRow(int row)
257 // Why make it so hard? (Lgb)
260 if (rows_ == 1) return; // Not allowed to delete last row
262 row_info.erase(row_info.begin() + row); //&row_info[row]);
263 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
269 void LyXTabular::AppendColumn(int cell)
273 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
275 int const column = column_of_cell(cell);
276 column_vector::iterator cit = column_info.begin() + column + 1;
277 column_info.insert(cit, columnstruct());
278 // set the column values of the column before
279 column_info[column+1] = column_info[column];
281 for (int i = 0; i < rows_; ++i) {
282 for (int j = 0; j <= column; ++j) {
283 c_info[i][j] = cell_info[i][j];
285 for (int j = column + 1; j < columns_; ++j) {
286 c_info[i][j] = cell_info[i][j - 1];
288 // care about multicolumns
289 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
290 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
292 if ((column + 1) == columns_ ||
293 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
294 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
299 for (int i = 0; i < rows_; ++i) {
300 //cell_info[i][column].inset.clear();
301 cell_info[i][column + 1].inset.clear();
307 void LyXTabular::DeleteColumn(int column)
309 // Similar to DeleteRow
310 //if (!(columns_ - 1))
312 if (columns_ == 1) return; // Not allowed to delete last column
314 column_info.erase(column_info.begin() + column);
315 for (int i = 0; i < rows_; ++i) {
316 cell_info[i].erase(cell_info[i].begin() + column);
323 void LyXTabular::Reinit()
325 for (int i = 0; i < rows_; ++i) {
326 for (int j = 0; j < columns_; ++j) {
327 cell_info[i][j].width_of_cell = 0;
328 cell_info[i][j].inset.setOwner(owner_);
332 for (int i = 0; i < columns_; ++i) {
333 calculate_width_of_column(i);
335 calculate_width_of_tabular();
337 set_row_column_number_info();
341 void LyXTabular::set_row_column_number_info(bool oldformat)
344 for (int row = 0; row < rows_; ++row) {
345 for (int column = 0; column<columns_; ++column) {
346 if (cell_info[row][column].multicolumn
347 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
349 cell_info[row][column].cellno = numberofcells;
352 ++numberofcells; // because this is one more than as we start from 0
354 rowofcell.resize(numberofcells);
355 columnofcell.resize(numberofcells);
357 for (int row = 0, column = 0, c = 0;
358 c < numberofcells && row < rows_ && column < columns_;) {
360 columnofcell[c] = column;
364 } while (column < columns_ &&
365 cell_info[row][column].multicolumn
366 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
367 if (column == columns_) {
373 for (int row = 0; row < rows_; ++row) {
374 for (int column = 0; column < columns_; ++column) {
375 if (IsPartOfMultiColumn(row,column))
377 // now set the right line of multicolumns right for oldformat read
379 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
381 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
382 cell_info[row][column].right_line =
383 cell_info[row][column+cn-1].right_line;
385 cell_info[row][column].inset.setAutoBreakRows(
386 !GetPWidth(GetCellNumber(row, column)).empty());
392 int LyXTabular::GetNumberOfCells() const
394 return numberofcells;
398 int LyXTabular::NumberOfCellsInRow(int cell) const
400 int const row = row_of_cell(cell);
402 for (int i = 0; i < columns_; ++i) {
403 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
410 /* returns 1 if there is a topline, returns 0 if not */
411 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
413 int const row = row_of_cell(cell);
415 if (!onlycolumn && IsMultiColumn(cell))
416 return cellinfo_of_cell(cell)->top_line;
417 return row_info[row].top_line;
421 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
423 // no bottom line underneath non-existent cells if you please
424 // Isn't that a programming error? Is so this should
425 // be an Assert instead. (Lgb)
426 if (cell >= numberofcells)
429 if (!onlycolumn && IsMultiColumn(cell))
430 return cellinfo_of_cell(cell)->bottom_line;
431 return row_info[row_of_cell(cell)].bottom_line;
435 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
437 if (!onlycolumn && IsMultiColumn(cell))
438 return cellinfo_of_cell(cell)->left_line;
439 return column_info[column_of_cell(cell)].left_line;
443 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
445 if (!onlycolumn && IsMultiColumn(cell))
446 return cellinfo_of_cell(cell)->right_line;
447 return column_info[right_column_of_cell(cell)].right_line;
451 bool LyXTabular::TopAlreadyDrawed(int cell) const
453 int row = row_of_cell(cell);
454 if ((row > 0) && !GetAdditionalHeight(row)) {
455 int column = column_of_cell(cell);
458 && cell_info[row][column].multicolumn
459 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
461 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
462 return row_info[row].bottom_line;
464 return cell_info[row][column].bottom_line;
470 bool LyXTabular::LeftAlreadyDrawed(int cell) const
472 int column = column_of_cell(cell);
474 int row = row_of_cell(cell);
476 (cell_info[row][column].multicolumn ==
477 LyXTabular::CELL_PART_OF_MULTICOLUMN));
478 if (GetAdditionalWidth(cell_info[row][column].cellno))
480 return column_info[column].right_line;
486 bool LyXTabular::IsLastRow(int cell) const
488 return (row_of_cell(cell) == rows_ - 1);
492 int LyXTabular::GetAdditionalHeight(int row) const
494 if (!row || row >= rows_)
500 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
501 switch (cell_info[row - 1][column].multicolumn) {
502 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
503 bottom = cell_info[row - 1][column].bottom_line;
505 case LyXTabular::CELL_NORMAL:
506 bottom = row_info[row - 1].bottom_line;
509 for (int column = 0; column < columns_ - 1 && top; ++column) {
510 switch (cell_info[row][column].multicolumn){
511 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
512 top = cell_info[row][column].top_line;
514 case LyXTabular::CELL_NORMAL:
515 top = row_info[row].top_line;
519 return WIDTH_OF_LINE;
524 int LyXTabular::GetAdditionalWidth(int cell) const
526 // internally already set in SetWidthOfCell
527 // used to get it back in text.C
528 int const col = right_column_of_cell(cell);
529 if (col < columns_ - 1 && column_info[col].right_line &&
530 column_info[col+1].left_line)
531 return WIDTH_OF_LINE;
537 // returns the maximum over all rows
538 int LyXTabular::GetWidthOfColumn(int cell) const
540 int const column1 = column_of_cell(cell);
541 int const column2 = right_column_of_cell(cell);
543 for (int i = column1; i <= column2; ++i) {
544 result += column_info[i].width_of_column;
550 int LyXTabular::GetWidthOfTabular() const
552 return width_of_tabular;
556 /* returns 1 if a complete update is necessary, otherwise 0 */
557 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
559 if (!IsMultiColumn(cell))
562 int const row = row_of_cell(cell);
563 int const column1 = column_of_cell(cell);
564 int const column2 = right_column_of_cell(cell);
566 // first set columns to 0 so we can calculate the right width
567 for (int i = column1; i <= column2; ++i) {
568 cell_info[row][i].width_of_cell = 0;
570 // set the width to MAX_WIDTH until width > 0
571 int width = (new_width + 2 * WIDTH_OF_LINE);
574 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
575 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
576 width -= column_info[i].width_of_column;
579 cell_info[row][i].width_of_cell = width;
585 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
587 int const row = row_of_cell(cell);
588 int const column1 = column_of_cell(cell);
589 int const column2 = right_column_of_cell(cell);
591 // first set columns to 0 so we can calculate the right width
593 for (; i <= column2; ++i)
594 cell_info[row][i].width_of_cell = 0;
595 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
597 if (i < numberofcells)
598 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
599 SetWidthOfMulticolCell(cell, new_width);
603 /* returns 1 if a complete update is necessary, otherwise 0 */
604 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
606 int const row = row_of_cell(cell);
607 int const column1 = column_of_cell(cell);
611 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
613 if (IsMultiColumn(cell, true)) {
614 tmp = SetWidthOfMulticolCell(cell, new_width);
616 width = (new_width + 2*WIDTH_OF_LINE);
617 cell_info[row][column1].width_of_cell = width;
618 if (column_info[column1].right_line && (column1 < columns_-1) &&
619 column_info[column1+1].left_line) // additional width
620 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
621 tmp = calculate_width_of_column_NMC(column1);
625 for (; i<columns_; ++i)
626 calculate_width_of_column_NMC(i);
627 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
629 if (i < numberofcells)
630 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
631 for (i = 0; i < columns_; ++i)
632 calculate_width_of_column(i);
633 calculate_width_of_tabular();
640 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
642 if (!IsMultiColumn(cell) || onlycolumn)
643 column_info[column_of_cell(cell)].alignment = align;
645 cellinfo_of_cell(cell)->alignment = align;
650 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
652 if (!IsMultiColumn(cell) || onlycolumn)
653 column_info[column_of_cell(cell)].valignment = align;
655 cellinfo_of_cell(cell)->valignment = align;
660 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
662 bool flag = !width.empty();
663 int const j = column_of_cell(cell);
665 column_info[j].p_width = width;
666 if (flag) // do this only if there is a width
667 SetAlignment(cell, LYX_ALIGN_LEFT);
668 for (int i = 0; i < rows_; ++i) {
669 int c = GetCellNumber(i, j);
670 flag = !GetPWidth(c).empty(); // because of multicolumns!
671 GetCellInset(c)->setAutoBreakRows(flag);
677 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
679 bool const flag = !width.empty();
681 cellinfo_of_cell(cell)->p_width = width;
682 if (IsMultiColumn(cell)) {
683 GetCellInset(cell)->setAutoBreakRows(flag);
690 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
691 LyXTabular::Feature what)
693 if (what == SET_SPECIAL_MULTI)
694 cellinfo_of_cell(cell)->align_special = special;
696 column_info[column_of_cell(cell)].align_special = special;
701 bool LyXTabular::SetAllLines(int cell, bool line)
703 SetTopLine(cell, line);
704 SetBottomLine(cell, line);
705 SetRightLine(cell, line);
706 SetLeftLine(cell, line);
711 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
713 int const row = row_of_cell(cell);
715 if (onlycolumn || !IsMultiColumn(cell))
716 row_info[row].top_line = line;
718 cellinfo_of_cell(cell)->top_line = line;
723 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
725 if (onlycolumn || !IsMultiColumn(cell))
726 row_info[row_of_cell(cell)].bottom_line = line;
728 cellinfo_of_cell(cell)->bottom_line = line;
733 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
735 if (onlycolumn || !IsMultiColumn(cell))
736 column_info[column_of_cell(cell)].left_line = line;
738 cellinfo_of_cell(cell)->left_line = line;
743 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
745 if (onlycolumn || !IsMultiColumn(cell))
746 column_info[right_column_of_cell(cell)].right_line = line;
748 cellinfo_of_cell(cell)->right_line = line;
753 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
755 if (!onlycolumn && IsMultiColumn(cell))
756 return cellinfo_of_cell(cell)->alignment;
758 return column_info[column_of_cell(cell)].alignment;
762 LyXTabular::VAlignment
763 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
765 if (!onlycolumn && IsMultiColumn(cell))
766 return cellinfo_of_cell(cell)->valignment;
768 return column_info[column_of_cell(cell)].valignment;
772 string const LyXTabular::GetPWidth(int cell) const
774 if (IsMultiColumn(cell))
775 return cellinfo_of_cell(cell)->p_width;
776 return column_info[column_of_cell(cell)].p_width;
780 string const LyXTabular::GetColumnPWidth(int cell) const
782 return column_info[column_of_cell(cell)].p_width;
786 string const LyXTabular::GetMColumnPWidth(int cell) const
788 if (IsMultiColumn(cell))
789 return cellinfo_of_cell(cell)->p_width;
794 string const LyXTabular::GetAlignSpecial(int cell, int what) const
796 if (what == SET_SPECIAL_MULTI)
797 return cellinfo_of_cell(cell)->align_special;
798 return column_info[column_of_cell(cell)].align_special;
802 int LyXTabular::GetWidthOfCell(int cell) const
804 int const row = row_of_cell(cell);
805 int const column1 = column_of_cell(cell);
806 int const column2 = right_column_of_cell(cell);
808 for (int i = column1; i <= column2; ++i) {
809 result += cell_info[row][i].width_of_cell;
815 int LyXTabular::GetBeginningOfTextInCell(int cell) const
819 switch (GetAlignment(cell)){
820 case LYX_ALIGN_CENTER:
821 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
823 case LYX_ALIGN_RIGHT:
824 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
825 // + GetAdditionalWidth(cell);
827 default: /* LYX_ALIGN_LEFT: nothing :-) */
837 bool LyXTabular::IsFirstCellInRow(int cell) const
839 return column_of_cell(cell) == 0;
843 int LyXTabular::GetFirstCellInRow(int row) const
847 return cell_info[row][0].cellno;
850 bool LyXTabular::IsLastCellInRow(int cell) const
852 return (right_column_of_cell(cell) == (columns_ - 1));
856 int LyXTabular::GetLastCellInRow(int row) const
860 return cell_info[row][columns_-1].cellno;
864 bool LyXTabular::calculate_width_of_column(int column)
866 int const old_column_width = column_info[column].width_of_column;
869 for (int i = 0; i < rows_; ++i) {
870 maximum = max(cell_info[i][column].width_of_cell, maximum);
872 column_info[column].width_of_column = maximum;
873 return (column_info[column].width_of_column != old_column_width);
878 // calculate the with of the column without regarding REAL MultiColumn
879 // cells. This means MultiColumn-cells spanning more than 1 column.
881 bool LyXTabular::calculate_width_of_column_NMC(int column)
883 int const old_column_width = column_info[column].width_of_column;
885 for (int i = 0; i < rows_; ++i) {
886 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
887 (cell_info[i][column].width_of_cell > max)) {
888 max = cell_info[i][column].width_of_cell;
891 column_info[column].width_of_column = max;
892 return (column_info[column].width_of_column != old_column_width);
896 void LyXTabular::calculate_width_of_tabular()
898 width_of_tabular = 0;
899 for (int i = 0; i < columns_; ++i) {
900 width_of_tabular += column_info[i].width_of_column;
905 int LyXTabular::row_of_cell(int cell) const
907 if (cell >= numberofcells)
911 return rowofcell[cell];
915 int LyXTabular::column_of_cell(int cell) const
917 if (cell >= numberofcells)
921 return columnofcell[cell];
925 int LyXTabular::right_column_of_cell(int cell) const
927 int const row = row_of_cell(cell);
928 int column = column_of_cell(cell);
929 while (column < (columns_ - 1) &&
930 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
936 // Perfect case for a template... (Lgb)
939 string const write_attribute(string const & name, T const & t)
941 string str = " " + name + "=\"" + tostr(t) + "\"";
946 string const write_attribute(string const & name, bool const & b)
948 return write_attribute(name, int(b));
953 string const write_attribute(string const & name, int value)
955 string str = " " + name + "=\"" + tostr(value) + "\"";
960 string const write_attribute(string const & name, string const & value)
962 string str = " " + name + "=\"" + value + "\"";
967 string const write_attribute(string const & name, bool value)
969 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
977 string const tostr(LyXAlignment const & num)
982 case LYX_ALIGN_BLOCK:
986 case LYX_ALIGN_CENTER:
988 case LYX_ALIGN_RIGHT:
990 case LYX_ALIGN_LAYOUT:
992 case LYX_ALIGN_SPECIAL:
1001 string const tostr(LyXTabular::VAlignment const & num)
1004 case LyXTabular::LYX_VALIGN_TOP:
1006 case LyXTabular::LYX_VALIGN_CENTER:
1008 case LyXTabular::LYX_VALIGN_BOTTOM:
1017 string const tostr(LyXTabular::BoxType const & num)
1020 case LyXTabular::BOX_NONE:
1022 case LyXTabular::BOX_PARBOX:
1024 case LyXTabular::BOX_MINIPAGE:
1031 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1035 << write_attribute("version", 2)
1036 << write_attribute("rows", rows_)
1037 << write_attribute("columns", columns_)
1039 // global longtable options
1041 << write_attribute("rotate", tostr(rotate))
1042 << write_attribute("islongtable", tostr(is_long_tabular))
1043 << write_attribute("endhead", endhead)
1044 << write_attribute("endfirsthead", endfirsthead)
1045 << write_attribute("endfoot", endfoot)
1046 << write_attribute("endlastfoot", endlastfoot)
1048 for (int j = 0; j < columns_; ++j) {
1050 << write_attribute("alignment", tostr(column_info[j].alignment))
1051 << write_attribute("valignment", tostr(column_info[j].valignment))
1052 << write_attribute("leftline", tostr(column_info[j].left_line))
1053 << write_attribute("rightline", tostr(column_info[j].right_line))
1054 << write_attribute("width",
1055 VSpace(column_info[j].p_width)
1057 << write_attribute("special", column_info[j].align_special)
1060 for (int i = 0; i < rows_; ++i) {
1062 << write_attribute("topline", tostr(row_info[i].top_line))
1063 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1064 << write_attribute("newpage", tostr(row_info[i].newpage))
1066 for (int j = 0; j < columns_; ++j) {
1068 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1069 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1070 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1071 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1072 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1073 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1074 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1075 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1076 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1077 << write_attribute("width", cell_info[i][j].p_width)
1078 << write_attribute("special", cell_info[i][j].align_special)
1080 os << "\\begin_inset ";
1081 cell_info[i][j].inset.write(buf, os);
1082 os << "\n\\end_inset \n"
1087 os << "</lyxtabular>\n";
1093 // I would have liked a fromstr template a lot better. (Lgb)
1096 bool string2type(string const str, LyXAlignment & num)
1099 num = LYX_ALIGN_NONE;
1100 else if (str == "block")
1101 num = LYX_ALIGN_BLOCK;
1102 else if (str == "left")
1103 num = LYX_ALIGN_LEFT;
1104 else if (str == "center")
1105 num = LYX_ALIGN_CENTER;
1106 else if (str == "right")
1107 num = LYX_ALIGN_RIGHT;
1115 bool string2type(string const str, LyXTabular::VAlignment & num)
1118 num = LyXTabular::LYX_VALIGN_TOP;
1119 else if (str == "center")
1120 num = LyXTabular::LYX_VALIGN_CENTER;
1121 else if (str == "bottom")
1122 num = LyXTabular::LYX_VALIGN_BOTTOM;
1130 bool string2type(string const str, LyXTabular::BoxType & num)
1133 num = LyXTabular::BOX_NONE;
1134 else if (str == "parbox")
1135 num = LyXTabular::BOX_PARBOX;
1136 else if (str == "minipage")
1137 num = LyXTabular::BOX_MINIPAGE;
1145 bool string2type(string const str, bool & num)
1149 else if (str == "false")
1157 bool getTokenValue(string const & str, const char * token, string & ret)
1159 size_t token_length = strlen(token);
1160 string::size_type pos = str.find(token);
1162 if (pos == string::npos || pos + token_length + 1 >= str.length()
1163 || str[pos + token_length] != '=')
1166 pos += token_length + 1;
1168 if ((ch != '"') && (ch != '\'')) { // only read till next space
1172 while((pos < str.length() - 1) && (str[++pos] != ch))
1179 bool getTokenValue(string const & str, const char * token, int & num)
1182 if (!getTokenValue(str, token, tmp))
1184 num = strToInt(tmp);
1189 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1192 if (!getTokenValue(str, token, tmp))
1194 return string2type(tmp, num);
1198 bool getTokenValue(string const & str, const char * token,
1199 LyXTabular::VAlignment & num)
1202 if (!getTokenValue(str, token, tmp))
1204 return string2type(tmp, num);
1208 bool getTokenValue(string const & str, const char * token,
1209 LyXTabular::BoxType & num)
1212 if (!getTokenValue(str, token, tmp))
1214 return string2type(tmp, num);
1218 bool getTokenValue(string const & str, const char * token, bool & flag)
1221 if (!getTokenValue(str, token, tmp))
1223 return string2type(tmp, flag);
1228 void l_getline(istream & is, string & str)
1231 while (str.empty()) {
1233 if (!str.empty() && str[str.length() - 1] == '\r')
1234 str.erase(str.length() - 1);
1241 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1244 istream & is = lex.getStream();
1246 l_getline(is, line);
1247 if (!prefixIs(line, "<lyxtabular ")
1248 && !prefixIs(line, "<LyXTabular ")) {
1249 OldFormatRead(lex, line);
1254 if (!getTokenValue(line, "version", version))
1257 ReadOld(buf, is, lex, line);
1258 else if (version == 2)
1259 ReadNew(buf, is, lex, line);
1263 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1264 LyXLex & lex, string const & l)
1268 if (!getTokenValue(line, "rows", rows_arg))
1271 if (!getTokenValue(line, "columns", columns_arg))
1273 Init(rows_arg, columns_arg);
1274 l_getline(is, line);
1275 if (!prefixIs(line, "<features")) {
1276 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1277 line << ")" << endl;
1280 getTokenValue(line, "rotate", rotate);
1281 getTokenValue(line, "islongtable", is_long_tabular);
1282 getTokenValue(line, "endhead", endhead);
1283 getTokenValue(line, "endfirsthead", endfirsthead);
1284 getTokenValue(line, "endfoot", endfoot);
1285 getTokenValue(line, "endlastfoot", endlastfoot);
1287 for (int j = 0; j < columns_; ++j) {
1289 if (!prefixIs(line,"<column")) {
1290 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1291 line << ")" << endl;
1294 getTokenValue(line, "alignment", column_info[j].alignment);
1295 getTokenValue(line, "valignment", column_info[j].valignment);
1296 getTokenValue(line, "leftline", column_info[j].left_line);
1297 getTokenValue(line, "rightline", column_info[j].right_line);
1298 getTokenValue(line, "width", column_info[j].p_width);
1299 getTokenValue(line, "special", column_info[j].align_special);
1302 for (int i = 0; i < rows_; ++i) {
1303 l_getline(is, line);
1304 if (!prefixIs(line, "<row")) {
1305 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1306 line << ")" << endl;
1309 getTokenValue(line, "topline", row_info[i].top_line);
1310 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1311 getTokenValue(line, "newpage", row_info[i].newpage);
1312 for (int j = 0; j < columns_; ++j) {
1313 l_getline(is, line);
1314 if (!prefixIs(line, "<cell")) {
1315 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1316 line << ")" << endl;
1319 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1320 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1321 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1322 getTokenValue(line, "topline", cell_info[i][j].top_line);
1323 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1324 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1325 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1326 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1327 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1328 getTokenValue(line, "width", cell_info[i][j].p_width);
1329 getTokenValue(line, "special", cell_info[i][j].align_special);
1330 l_getline(is, line);
1331 if (prefixIs(line, "\\begin_inset")) {
1332 cell_info[i][j].inset.read(buf, lex);
1333 l_getline(is, line);
1335 if (!prefixIs(line, "</cell>")) {
1336 lyxerr << "Wrong tabular format (expected </cell> got" <<
1337 line << ")" << endl;
1341 l_getline(is, line);
1342 if (!prefixIs(line, "</row>")) {
1343 lyxerr << "Wrong tabular format (expected </row> got" <<
1344 line << ")" << endl;
1348 while (!prefixIs(line, "</lyxtabular>")) {
1349 l_getline(is, line);
1351 set_row_column_number_info();
1355 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1361 int columns_arg = 0;
1362 int is_long_tabular_arg = false;
1363 int rotate_arg = false;
1372 istream & is = lex.getStream();
1375 version = lyx::atoi(s.substr(8, string::npos));
1379 vector<int> cont_row_info;
1382 lyxerr << "Tabular format < 5 is not supported anymore\n"
1383 "Get an older version of LyX (< 1.1.x) for conversion!"
1385 WriteAlert(_("Warning:"),
1386 _("Tabular format < 5 is not supported anymore\n"),
1387 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1389 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1390 >> rotate_arg >> a >> b >> c >> d;
1392 is >> rows_arg >> columns_arg;
1393 Init(rows_arg, columns_arg);
1394 cont_row_info = vector<int>(rows_arg);
1395 SetLongTabular(is_long_tabular_arg);
1396 SetRotateTabular(rotate_arg);
1398 for (i = 0; i < rows_; ++i) {
1400 cont_row_info[i] = false;
1402 for (i = 0; i < columns_; ++i) {
1405 for (i = 0; i < rows_; ++i) {
1406 for (j = 0; j < columns_; ++j) {
1411 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1412 >> rotate_arg >> a >> b >> c >> d;
1413 Init(rows_arg, columns_arg);
1414 cont_row_info = vector<int>(rows_arg);
1415 SetLongTabular(is_long_tabular_arg);
1416 SetRotateTabular(rotate_arg);
1418 endfirsthead = b + 1;
1420 endlastfoot = d + 1;
1421 for (i = 0; i < rows_; ++i) {
1422 a = b = c = d = e = f = g = 0;
1423 is >> a >> b >> c >> d;
1424 row_info[i].top_line = a;
1425 row_info[i].bottom_line = b;
1426 cont_row_info[i] = c;
1427 row_info[i].newpage = d;
1429 for (i = 0; i < columns_; ++i) {
1434 char ch; // skip '"'
1437 // ignore is buggy but we will use it later (Lgb)
1438 is.ignore(); // skip '"'
1440 getline(is, s1, '"');
1442 is >> ch; // skip '"'
1444 // ignore is buggy but we will use it later (Lgb)
1445 is.ignore(); // skip '"'
1447 getline(is, s2, '"');
1448 column_info[i].alignment = static_cast<LyXAlignment>(a);
1449 column_info[i].left_line = b;
1450 column_info[i].right_line = c;
1451 column_info[i].p_width = s1;
1452 column_info[i].align_special = s2;
1454 for (i = 0; i < rows_; ++i) {
1455 for (j = 0; j < columns_; ++j) {
1458 is >> a >> b >> c >> d >> e >> f >> g;
1461 is >> ch; // skip '"'
1463 // ignore is buggy but we will use it later (Lgb)
1464 is.ignore(); // skip '"'
1466 getline(is, s1, '"');
1468 is >> ch; // skip '"'
1470 // ignore is buggy but we will use it later (Lgb)
1471 is.ignore(); // skip '"'
1473 getline(is, s2, '"');
1474 cell_info[i][j].multicolumn = static_cast<char>(a);
1475 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1476 cell_info[i][j].top_line = static_cast<char>(c);
1477 cell_info[i][j].bottom_line = static_cast<char>(d);
1478 cell_info[i][j].left_line = column_info[j].left_line;
1479 cell_info[i][j].right_line = column_info[j].right_line;
1480 cell_info[i][j].rotate = static_cast<bool>(f);
1481 cell_info[i][j].usebox = static_cast<BoxType>(g);
1482 cell_info[i][j].align_special = s1;
1483 cell_info[i][j].p_width = s2;
1487 set_row_column_number_info(true);
1489 Paragraph * par = new Paragraph;
1490 Paragraph * return_par = 0;
1494 Paragraph::depth_type depth = 0;
1495 LyXFont font(LyXFont::ALL_INHERIT);
1496 font.setLanguage(owner_->bufferOwner()->getLanguage());
1498 while (lex.IsOK()) {
1500 string const token = lex.GetString();
1503 if (token == "\\layout"
1504 || token == "\\end_float"
1505 || token == "\\end_deeper") {
1506 lex.pushToken(token);
1507 // Here we need to insert the inset_ert_contents into the last
1508 // cell of the tabular.
1509 owner_->bufferOwner()->insertErtContents(par, pos, font);
1513 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1518 lex.pushToken(token);
1522 lex.printError("New Paragraph allocated! This should not happen!");
1523 lex.pushToken(token);
1529 // now we have the par we should fill the insets with this!
1531 InsetText * inset = GetCellInset(cell);
1534 for (int i = 0; i < par->size(); ++i) {
1535 if (par->isNewline(i)) {
1537 if (cell > GetNumberOfCells()) {
1538 lyxerr << "Some error in reading old table format occured!" <<
1539 endl << "Terminating when reading cell[" << cell << "]!" <<
1544 row = row_of_cell(cell);
1545 if (cont_row_info[row]) {
1547 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1548 while(!IsFirstCellInRow(--cell));
1550 inset = GetCellInset(cell);
1553 inset = GetCellInset(cell);
1554 row = row_of_cell(cell);
1555 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1557 // insert a space instead
1559 par->insertChar(i, ' ');
1562 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1563 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1570 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1572 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1573 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1577 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1579 int const row = row_of_cell(cell);
1580 int const column = column_of_cell(cell);
1581 return &cell_info[row][column];
1585 void LyXTabular::SetMultiColumn(int cell, int number)
1587 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1588 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1589 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1590 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1591 for (number--; number > 0; --number) {
1592 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1594 set_row_column_number_info();
1598 int LyXTabular::cells_in_multicolumn(int cell) const
1600 int const row = row_of_cell(cell);
1601 int column = column_of_cell(cell);
1604 while ((column < columns_) &&
1605 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1614 int LyXTabular::UnsetMultiColumn(int cell)
1616 int const row = row_of_cell(cell);
1617 int column = column_of_cell(cell);
1621 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1622 cell_info[row][column].multicolumn = CELL_NORMAL;
1624 while ((column < columns_) &&
1625 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1627 cell_info[row][column].multicolumn = CELL_NORMAL;
1632 set_row_column_number_info();
1637 void LyXTabular::SetLongTabular(bool what)
1639 is_long_tabular = what;
1643 bool LyXTabular::IsLongTabular() const
1645 return is_long_tabular;
1649 void LyXTabular::SetRotateTabular(bool flag)
1655 bool LyXTabular::GetRotateTabular() const
1661 void LyXTabular::SetRotateCell(int cell, bool flag)
1663 cellinfo_of_cell(cell)->rotate = flag;
1667 bool LyXTabular::GetRotateCell(int cell) const
1669 return cellinfo_of_cell(cell)->rotate;
1673 bool LyXTabular::NeedRotating() const
1677 for (int i = 0; i < rows_; ++i) {
1678 for (int j = 0; j < columns_; ++j) {
1679 if (cell_info[i][j].rotate)
1687 bool LyXTabular::IsLastCell(int cell) const
1689 if ((cell + 1) < GetNumberOfCells())
1695 int LyXTabular::GetCellAbove(int cell) const
1697 if (row_of_cell(cell) > 0)
1698 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1703 int LyXTabular::GetCellBelow(int cell) const
1705 if (row_of_cell(cell) + 1 < rows_)
1706 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1711 int LyXTabular::GetLastCellAbove(int cell) const
1713 if (row_of_cell(cell) <= 0)
1715 if (!IsMultiColumn(cell))
1716 return GetCellAbove(cell);
1717 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1721 int LyXTabular::GetLastCellBelow(int cell) const
1723 if (row_of_cell(cell) + 1 >= rows_)
1725 if (!IsMultiColumn(cell))
1726 return GetCellBelow(cell);
1727 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1731 int LyXTabular::GetCellNumber(int row, int column) const
1733 if (column >= columns_)
1734 column = columns_ - 1;
1735 else if (column < 0)
1742 return cell_info[row][column].cellno;
1746 void LyXTabular::SetUsebox(int cell, BoxType type)
1748 cellinfo_of_cell(cell)->usebox = type;
1752 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1754 if (column_info[column_of_cell(cell)].p_width.empty() &&
1755 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1757 if (cellinfo_of_cell(cell)->usebox > 1)
1758 return cellinfo_of_cell(cell)->usebox;
1759 return UseParbox(cell);
1763 void LyXTabular::SetLTHead(int cell, bool first)
1765 int const row = row_of_cell(cell);
1766 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1769 if (endfirsthead == val)
1782 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1785 if (abs(endhead) > rows_)
1787 return (row_of_cell(cell) == abs(endhead) - 1);
1791 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1794 if (abs(endfirsthead) > rows_)
1796 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1800 void LyXTabular::SetLTFoot(int cell, bool last)
1802 int const row = row_of_cell(cell);
1803 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1806 if (endlastfoot == val)
1819 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1822 if ((endfoot + 1) > rows_)
1824 return (row_of_cell(cell) == abs(endfoot) - 1);
1828 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1831 if (abs(endlastfoot) > rows_)
1833 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1837 void LyXTabular::SetLTNewPage(int cell, bool what)
1839 row_info[row_of_cell(cell)].newpage = what;
1843 bool LyXTabular::GetLTNewPage(int cell) const
1845 return row_info[row_of_cell(cell)].newpage;
1849 bool LyXTabular::SetAscentOfRow(int row, int height)
1851 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1853 row_info[row].ascent_of_row = height;
1858 bool LyXTabular::SetDescentOfRow(int row, int height)
1860 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1862 row_info[row].descent_of_row = height;
1867 int LyXTabular::GetAscentOfRow(int row) const
1871 return row_info[row].ascent_of_row;
1875 int LyXTabular::GetDescentOfRow(int row) const
1879 return row_info[row].descent_of_row;
1883 int LyXTabular::GetHeightOfTabular() const
1887 for (int row = 0; row < rows_; ++row)
1888 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1889 GetAdditionalHeight(row);
1894 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1896 if ((row >= rows_) || (column >= columns_))
1898 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1902 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1904 if ((row < 0) || (row >= rows_))
1907 int const fcell = GetFirstCellInRow(row);
1908 int const n = NumberOfCellsInRow(fcell) + fcell;
1911 for (int i = fcell; i < n; ++i) {
1915 if (tmp == (n - fcell)){
1918 for (int i = fcell; i < n; ++i) {
1921 << column_of_cell(i) + 1
1923 << right_column_of_cell(i) + 1
1935 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1937 if ((row < 0) || (row >= rows_))
1940 int const fcell = GetFirstCellInRow(row);
1941 int const n = NumberOfCellsInRow(fcell) + fcell;
1944 for (int i = fcell; i < n; ++i) {
1948 if (tmp == (n-fcell)){
1951 for (int i = fcell; i < n; ++i) {
1952 if (BottomLine(i)) {
1954 << column_of_cell(i) + 1
1956 << right_column_of_cell(i) + 1
1968 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1972 if (GetRotateCell(cell)) {
1973 os << "\\begin{sideways}\n";
1976 if (IsMultiColumn(cell)) {
1977 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1978 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1979 os << cellinfo_of_cell(cell)->align_special << "}{";
1981 if (LeftLine(cell) &&
1982 (IsFirstCellInRow(cell) ||
1983 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1984 !RightLine(cell-1, true))))
1988 if (!GetPWidth(cell).empty()) {
1989 switch (GetVAlignment(cell)) {
1990 case LYX_VALIGN_TOP:
1993 case LYX_VALIGN_CENTER:
1996 case LYX_VALIGN_BOTTOM:
2000 os << "{" << GetPWidth(cell) << '}';
2002 switch (GetAlignment(cell)) {
2003 case LYX_ALIGN_LEFT:
2006 case LYX_ALIGN_RIGHT:
2014 if (RightLine(cell))
2016 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2022 if (GetUsebox(cell) == BOX_PARBOX) {
2024 switch (GetVAlignment(cell)) {
2025 case LYX_VALIGN_TOP:
2028 case LYX_VALIGN_CENTER:
2031 case LYX_VALIGN_BOTTOM:
2035 os << "]{" << GetPWidth(cell) << "}{";
2036 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2037 os << "\\begin{minipage}[";
2038 switch (GetVAlignment(cell)) {
2039 case LYX_VALIGN_TOP:
2042 case LYX_VALIGN_CENTER:
2045 case LYX_VALIGN_BOTTOM:
2049 os << "]{" << GetPWidth(cell) << "}\n";
2056 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2061 if (GetUsebox(cell) == BOX_PARBOX)
2063 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2064 os << "%\n\\end{minipage}";
2067 if (IsMultiColumn(cell)){
2070 if (GetRotateCell(cell)) {
2071 os << "%\n\\end{sideways}";
2078 int LyXTabular::Latex(Buffer const * buf,
2079 ostream & os, bool fragile, bool fp) const
2084 //+---------------------------------------------------------------------
2085 //+ first the opening preamble +
2086 //+---------------------------------------------------------------------
2089 os << "\\begin{sideways}\n";
2092 if (is_long_tabular)
2093 os << "\\begin{longtable}{";
2095 os << "\\begin{tabular}{";
2096 for (int i = 0; i < columns_; ++i) {
2097 if (column_info[i].left_line)
2099 if (!column_info[i].align_special.empty()) {
2100 os << column_info[i].align_special;
2101 } else if (!column_info[i].p_width.empty()) {
2102 switch (column_info[i].valignment) {
2103 case LYX_VALIGN_TOP:
2106 case LYX_VALIGN_CENTER:
2109 case LYX_VALIGN_BOTTOM:
2114 << column_info[i].p_width
2117 switch (column_info[i].alignment) {
2118 case LYX_ALIGN_LEFT:
2121 case LYX_ALIGN_RIGHT:
2129 if (column_info[i].right_line)
2135 //+---------------------------------------------------------------------
2136 //+ the single row and columns (cells) +
2137 //+---------------------------------------------------------------------
2139 for (int i = 0; i < rows_; ++i) {
2140 ret += TeXTopHLine(os, i);
2142 if (IsLongTabular()) {
2143 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2144 os << "\\endhead\n";
2147 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2148 os << "\\endfirsthead\n";
2151 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2152 os << "\\endfoot\n";
2155 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2156 os << "\\endlastfoot\n";
2161 ret += TeXBottomHLine(os, i-1);
2162 ret += TeXTopHLine(os, i);
2164 for (int j = 0; j < columns_; ++j) {
2165 if (IsPartOfMultiColumn(i,j))
2167 ret += TeXCellPreamble(os, cell);
2168 InsetText * inset = GetCellInset(cell);
2170 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2171 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2175 ret += inset->latex(buf, os, fragile, fp);
2179 ret += TeXCellPostamble(os, cell);
2180 if (!IsLastCellInRow(cell)) { // not last cell in row
2187 ret += TeXBottomHLine(os, i);
2189 if (IsLongTabular()) {
2190 if ((endhead > 0) && (i == (endhead - 1))) {
2191 os << "\\endhead\n";
2194 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2195 os << "\\endfirsthead\n";
2198 if ((endfoot > 0) && (i == (endfoot - 1))) {
2199 os << "\\endfoot\n";
2202 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2203 os << "\\endlastfoot\n";
2207 // ret += TeXBottomHLine(os, i);
2208 if (row_info[i].newpage) {
2209 os << "\\newpage\n";
2215 //+---------------------------------------------------------------------
2216 //+ the closing of the tabular +
2217 //+---------------------------------------------------------------------
2219 if (is_long_tabular)
2220 os << "\\end{longtable}";
2222 os << "\\end{tabular}";
2224 os << "\n\\end{sideways}";
2232 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2236 //+---------------------------------------------------------------------
2237 //+ first the opening preamble +
2238 //+---------------------------------------------------------------------
2240 os << "<tgroup cols=\"" << columns_
2241 << "\" colsep=\"1\" rowsep=\"1\">\n";
2243 for (int i = 0; i < columns_; ++i) {
2244 os << "<colspec colname=\"col" << i << "\" align=\"";
2245 switch (column_info[i].alignment) {
2246 case LYX_ALIGN_LEFT:
2249 case LYX_ALIGN_RIGHT:
2260 //+---------------------------------------------------------------------
2261 //+ the single row and columns (cells) +
2262 //+---------------------------------------------------------------------
2266 for (int i = 0; i < rows_; ++i) {
2268 for (int j = 0; j < columns_; ++j) {
2269 if (IsPartOfMultiColumn(i, j))
2272 os << "<entry align=\"";
2273 switch (GetAlignment(cell)) {
2274 case LYX_ALIGN_LEFT:
2277 case LYX_ALIGN_RIGHT:
2285 os << "\" valign=\"";
2286 switch (GetVAlignment(cell)) {
2287 case LYX_VALIGN_TOP:
2290 case LYX_VALIGN_BOTTOM:
2293 case LYX_VALIGN_CENTER:
2298 if (IsMultiColumn(cell)) {
2299 os << " namest=\"col" << j << "\" ";
2300 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2304 ret += GetCellInset(cell)->docBook(buf, os);
2311 //+---------------------------------------------------------------------
2312 //+ the closing of the tabular +
2313 //+---------------------------------------------------------------------
2325 void print_n_chars(ostream & os, unsigned char ch, int n)
2327 os << string(n, ch);
2333 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2334 vector<unsigned int> const & clen) const
2336 int const fcell = GetFirstCellInRow(row);
2337 int const n = NumberOfCellsInRow(fcell) + fcell;
2340 for (int i = fcell; i < n; ++i) {
2350 for (int i = fcell; i < n; ++i) {
2361 int column = column_of_cell(i);
2362 int len = clen[column];
2363 while(IsPartOfMultiColumn(row, ++column))
2364 len += clen[column] + 4;
2365 print_n_chars(os, ch, len);
2380 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2381 vector<unsigned int> const & clen) const
2383 int const fcell = GetFirstCellInRow(row);
2384 int const n = NumberOfCellsInRow(fcell) + fcell;
2387 for (int i = fcell; i < n; ++i) {
2388 if (BottomLine(i)) {
2397 for (int i = fcell; i < n; ++i) {
2398 if (BottomLine(i)) {
2408 int column = column_of_cell(i);
2409 int len = clen[column];
2410 while(IsPartOfMultiColumn(row, ++column))
2411 len += clen[column] + 4;
2412 print_n_chars(os, ch, len);
2413 if (BottomLine(i)) {
2427 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2428 int cell, int row, int column,
2429 vector<unsigned int> const & clen) const
2432 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2439 unsigned int len1 = sstr.str().length();
2440 unsigned int len2 = clen[column];
2441 while(IsPartOfMultiColumn(row, ++column))
2442 len2 += clen[column] + 4;
2445 switch (GetAlignment(cell)) {
2447 case LYX_ALIGN_LEFT:
2450 case LYX_ALIGN_RIGHT:
2454 case LYX_ALIGN_CENTER:
2460 for (unsigned int i = 0; i < len1; ++i)
2463 for (unsigned int i = 0; i < len2; ++i)
2465 if (RightLine(cell))
2474 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2478 //+---------------------------------------------------------------------
2479 //+ first calculate the width of the single columns +
2480 //+---------------------------------------------------------------------
2481 vector<unsigned int> clen(columns_);
2483 // first all non (real) multicolumn cells!
2484 for (int j = 0; j < columns_; ++j) {
2486 for (int i = 0; i < rows_; ++i) {
2487 int cell = GetCellNumber(i, j);
2488 if (IsMultiColumn(cell, true))
2491 GetCellInset(cell)->ascii(buf, sstr, 0);
2492 if (clen[j] < sstr.str().length())
2493 clen[j] = sstr.str().length();
2496 // then all (real) multicolumn cells!
2497 for (int j = 0; j < columns_; ++j) {
2498 for (int i = 0; i < rows_; ++i) {
2499 int cell = GetCellNumber(i, j);
2500 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2503 GetCellInset(cell)->ascii(buf, sstr, 0);
2504 int len = int(sstr.str().length());
2505 int const n = cells_in_multicolumn(cell);
2506 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2508 if (len > int(clen[j + n - 1]))
2509 clen[j + n - 1] = len;
2513 for (int i = 0; i < rows_; ++i) {
2514 AsciiTopHLine(os, i, clen);
2515 for (int j = 0; j < columns_; ++j) {
2516 if (IsPartOfMultiColumn(i,j))
2518 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2522 AsciiBottomHLine(os, i, clen);
2528 InsetText * LyXTabular::GetCellInset(int cell) const
2531 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2535 InsetText * LyXTabular::GetCellInset(int row, int column) const
2537 #ifdef WITH_WARNINGS
2538 #warning Juergen, should we check whether the row/column values are correct?
2539 // If we do not need to do that, the tests in GetCellNumber should be
2540 // changed to asserts.
2542 cur_cell = GetCellNumber(row, column);
2543 return & cell_info[row][column].inset;
2547 void LyXTabular::Validate(LaTeXFeatures & features) const
2549 if (IsLongTabular())
2550 features.longtable = true;
2552 features.rotating = true;
2553 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2554 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2555 features.array = true;
2556 GetCellInset(cell)->validate(features);
2561 std::vector<string> const LyXTabular::getLabelList() const
2563 std::vector<string> label_list;
2564 for (int i = 0; i < rows_; ++i)
2565 for (int j = 0; j < columns_; ++j) {
2566 std::vector<string> const l =
2567 GetCellInset(i, j)->getLabelList();
2568 label_list.insert(label_list.end(),
2569 l.begin(), l.end());
2575 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2577 Paragraph * par = GetCellInset(cell)->paragraph();
2579 for (; par; par = par->next()) {
2580 for (int i = 0; i < par->size(); ++i) {
2581 if (par->getChar(i) == Paragraph::META_NEWLINE)