1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 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"
43 static int const WIDTH_OF_LINE = 5;
45 /// Define a few methods for the inner structs
47 LyXTabular::cellstruct::cellstruct()
51 multicolumn = LyXTabular::CELL_NORMAL;
52 alignment = LYX_ALIGN_CENTER;
53 valignment = LYX_VALIGN_TOP;
63 LyXTabular::rowstruct::rowstruct()
73 LyXTabular::columnstruct::columnstruct()
77 alignment = LYX_ALIGN_CENTER;
78 valignment = LYX_VALIGN_TOP;
84 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
87 Init(rows_arg, columns_arg);
91 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
94 Init(lt.rows_, lt.columns_);
95 #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)
100 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
107 LyXTabular::~LyXTabular()
110 delete[] columnofcell;
114 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
116 // If this and lt is not of the same size we have a serious bug
117 // So then it is ok to throw an exception, or for now
119 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
121 cell_info = lt.cell_info;
122 row_info = lt.row_info;
123 column_info = lt.column_info;
125 // long tabular stuff
126 SetLongTabular(lt.is_long_tabular);
127 endhead = lt.endhead;
128 endfoot = lt.endfoot;
129 endfirsthead = lt.endfirsthead;
130 endlastfoot = lt.endlastfoot;
140 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
142 LyXTabular * result = new LyXTabular(inset, *this);
143 // don't know if this is good but I need to Clone also
144 // the text-insets here, this is for the Undo-facility!
145 for(int i = 0; i < rows_; ++i) {
146 for(int j = 0; j < columns_; ++j) {
147 result->cell_info[i][j].inset = cell_info[i][j].inset;
148 result->cell_info[i][j].inset.setOwner(inset);
155 /* activates all lines and sets all widths to 0 */
156 void LyXTabular::Init(int rows_arg, int columns_arg)
160 columns_ = columns_arg;
161 row_info = row_vector(rows_, rowstruct());
162 column_info = column_vector(columns_, columnstruct());
163 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
165 // Jürgen, use iterators.
167 for (int i = 0; i < rows_; ++i) {
168 for (int j = 0; j < columns_; ++j) {
169 cell_info[i][j].inset.setOwner(owner_);
170 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
171 cell_info[i][j].cellno = cellno++;
173 cell_info[i][columns_-1].right_line = true;
175 //row_info[i - 1].bottom_line = true;
176 //row_info[0].bottom_line = true;
177 row_info.back().bottom_line = true;
178 row_info.front().bottom_line = true;
180 for (int i = 0; i < columns_; ++i) {
181 calculate_width_of_column(i);
183 //column_info[columns_ - 1].right_line = true;
184 column_info.back().right_line = true;
186 calculate_width_of_tabular();
190 set_row_column_number_info();
191 is_long_tabular = false;
200 void LyXTabular::AppendRow(int cell)
204 int row = row_of_cell(cell);
206 row_vector::iterator rit = row_info.begin() + row;
207 row_info.insert(rit, rowstruct());
210 cell_vvector::iterator cit = cell_info.begin() + row;
211 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
213 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
216 for(int i = 0; i <= row; ++i) {
217 for(int j = 0; j < columns_; ++j) {
218 c_info[i][j] = cell_info[i][j];
221 for(int i = row + 1; i < rows_; ++i) {
222 for(int j = 0; j < columns_; ++j) {
223 c_info[i][j] = cell_info[i-1][j];
228 for (int j = 0; j < columns_; ++j) {
229 cell_info[row][j].inset.clear();
236 void LyXTabular::DeleteRow(int row)
238 // Why make it so hard? (Lgb)
241 if (rows_ == 1) return; // Not allowed to delete last row
243 row_info.erase(row_info.begin() + row); //&row_info[row]);
244 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
250 void LyXTabular::AppendColumn(int cell)
254 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
256 int column = column_of_cell(cell);
257 column_vector::iterator cit = column_info.begin() + column + 1;
258 column_info.insert(cit, columnstruct());
260 for (int i = 0; i < rows_; ++i) {
261 for (int j = 0; j <= column; ++j) {
262 c_info[i][j] = cell_info[i][j];
264 for (int j = column + 1; j < columns_; ++j) {
265 c_info[i][j] = cell_info[i][j - 1];
267 // care about multicolumns
268 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
269 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
271 if ((column + 1) == columns_ ||
272 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
273 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
278 for (int i = 0; i < rows_; ++i) {
279 cell_info[i][column].inset.clear();
285 void LyXTabular::DeleteColumn(int column)
287 // Similar to DeleteRow
288 //if (!(columns_ - 1))
290 if (columns_ == 1) return; // Not allowed to delete last column
292 column_info.erase(column_info.begin() + column);
293 for (int i = 0; i < rows_; ++i) {
294 cell_info[i].erase(cell_info[i].begin() + column);
301 void LyXTabular::Reinit()
303 // Jürgen, use iterators.
304 for (int i = 0; i < rows_; ++i) {
305 for (int j = 0; j < columns_; ++j) {
306 cell_info[i][j].width_of_cell = 0;
307 cell_info[i][j].inset.setOwner(owner_);
311 for (int i = 0; i < columns_; ++i) {
312 calculate_width_of_column(i);
314 calculate_width_of_tabular();
316 set_row_column_number_info();
320 void LyXTabular::set_row_column_number_info(bool oldformat)
326 for (; row < rows_; ++row) {
327 for (column = 0; column<columns_; ++column) {
328 if (cell_info[row][column].multicolumn
329 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
331 cell_info[row][column].cellno = numberofcells;
334 ++numberofcells; // because this is one more than as we start from 0
339 rowofcell = new int[numberofcells];
340 delete [] columnofcell;
341 columnofcell = new int[numberofcells];
343 while (c < numberofcells && row < rows_ && column < columns_) {
345 columnofcell[c] = column;
349 } while (column < columns_ &&
350 cell_info[row][column].multicolumn
351 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
352 if (column == columns_) {
357 for (row = 0; row < rows_; ++row) {
358 for (column = 0; column<columns_; ++column) {
359 if (IsPartOfMultiColumn(row,column))
361 // now set the right line of multicolumns right for oldformat read
363 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
365 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
366 cell_info[row][column].right_line =
367 cell_info[row][column+cn-1].right_line;
369 cell_info[row][column].inset.SetAutoBreakRows(
370 !GetPWidth(GetCellNumber(row, column)).empty());
376 int LyXTabular::GetNumberOfCells() const
378 return numberofcells;
382 int LyXTabular::NumberOfCellsInRow(int cell) const
384 int const row = row_of_cell(cell);
386 for (int i = 0; i < columns_; ++i) {
387 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
394 /* returns 1 if there is a topline, returns 0 if not */
395 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
397 int const row = row_of_cell(cell);
399 if (!onlycolumn && IsMultiColumn(cell))
400 return cellinfo_of_cell(cell)->top_line;
401 return row_info[row].top_line;
405 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
407 // no bottom line underneath non-existent cells if you please
408 // Isn't that a programming error? Is so this should
409 // be an Assert instead. (Lgb)
410 if(cell >= numberofcells)
413 if (!onlycolumn && IsMultiColumn(cell))
414 return cellinfo_of_cell(cell)->bottom_line;
415 return row_info[row_of_cell(cell)].bottom_line;
419 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
421 if (!onlycolumn && IsMultiColumn(cell))
422 return cellinfo_of_cell(cell)->left_line;
423 return column_info[column_of_cell(cell)].left_line;
427 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
429 if (!onlycolumn && IsMultiColumn(cell))
430 return cellinfo_of_cell(cell)->right_line;
431 return column_info[right_column_of_cell(cell)].right_line;
435 bool LyXTabular::TopAlreadyDrawed(int cell) const
437 if (GetAdditionalHeight(cell))
439 int row = row_of_cell(cell);
441 int column = column_of_cell(cell);
444 && cell_info[row][column].multicolumn
445 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
447 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
448 return row_info[row].bottom_line;
450 return cell_info[row][column].bottom_line;
456 bool LyXTabular::LeftAlreadyDrawed(int cell) const
458 int column = column_of_cell(cell);
460 int row = row_of_cell(cell);
462 (cell_info[row][column].multicolumn ==
463 LyXTabular::CELL_PART_OF_MULTICOLUMN));
464 if (GetAdditionalWidth(cell_info[row][column].cellno))
466 return column_info[column].right_line;
472 bool LyXTabular::IsLastRow(int cell) const
474 return (row_of_cell(cell) == rows_ - 1);
478 int LyXTabular::GetAdditionalHeight(int cell) const
480 int const row = row_of_cell(cell);
486 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
487 switch (cell_info[row - 1][column].multicolumn) {
488 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
489 bottom = cell_info[row - 1][column].bottom_line;
491 case LyXTabular::CELL_NORMAL:
492 bottom = row_info[row - 1].bottom_line;
495 for (int column = 0; column < columns_ - 1 && top; ++column) {
496 switch (cell_info[row][column].multicolumn){
497 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
498 top = cell_info[row][column].top_line;
500 case LyXTabular::CELL_NORMAL:
501 top = row_info[row].top_line;
505 return WIDTH_OF_LINE;
510 int LyXTabular::GetAdditionalWidth(int cell) const
512 // internally already set in SetWidthOfCell
513 // used to get it back in text.C
514 int const col = right_column_of_cell(cell);
515 if (col < columns_ - 1 && column_info[col].right_line &&
516 column_info[col+1].left_line)
517 return WIDTH_OF_LINE;
523 // returns the maximum over all rows
524 int LyXTabular::GetWidthOfColumn(int cell) const
526 int const column1 = column_of_cell(cell);
527 int const column2 = right_column_of_cell(cell);
530 for (; i <= column2; ++i) {
531 result += column_info[i].width_of_column;
537 int LyXTabular::GetWidthOfTabular() const
539 return width_of_tabular;
543 /* returns 1 if a complete update is necessary, otherwise 0 */
544 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
546 if (!IsMultiColumn(cell))
549 int const row = row_of_cell(cell);
550 int const column1 = column_of_cell(cell);
551 int const column2 = right_column_of_cell(cell);
553 // first set columns to 0 so we can calculate the right width
555 for (; i <= column2; ++i) {
556 cell_info[row][i].width_of_cell = 0;
558 // set the width to MAX_WIDTH until width > 0
559 int width = (new_width + 2 * WIDTH_OF_LINE);
561 i < column2 && width > column_info[i].width_of_column;
563 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
564 width -= column_info[i].width_of_column;
567 cell_info[row][i].width_of_cell = width;
573 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
575 int const row = row_of_cell(cell);
576 int const column1 = column_of_cell(cell);
577 int const column2 = right_column_of_cell(cell);
579 // first set columns to 0 so we can calculate the right width
581 for (; i <= column2; ++i)
582 cell_info[row][i].width_of_cell = 0;
583 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
585 if (i < numberofcells)
586 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
587 SetWidthOfMulticolCell(cell, new_width);
591 /* returns 1 if a complete update is necessary, otherwise 0 */
592 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
594 int const row = row_of_cell(cell);
595 int const column1 = column_of_cell(cell);
599 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
601 if (IsMultiColumn(cell, true)) {
602 tmp = SetWidthOfMulticolCell(cell, new_width);
604 width = (new_width + 2*WIDTH_OF_LINE);
605 cell_info[row][column1].width_of_cell = width;
606 if (column_info[column1].right_line && (column1 < columns_-1) &&
607 column_info[column1+1].left_line) // additional width
608 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
609 tmp = calculate_width_of_column_NMC(column1);
613 for(; i<columns_; ++i)
614 calculate_width_of_column_NMC(i);
615 for(i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
617 if (i < numberofcells)
618 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
619 for(i = 0; i < columns_; ++i)
620 calculate_width_of_column(i);
621 calculate_width_of_tabular();
628 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
630 if (!IsMultiColumn(cell) || onlycolumn)
631 column_info[column_of_cell(cell)].alignment = align;
633 cellinfo_of_cell(cell)->alignment = align;
638 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
640 if (!IsMultiColumn(cell) || onlycolumn)
641 column_info[column_of_cell(cell)].valignment = align;
643 cellinfo_of_cell(cell)->valignment = align;
648 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
650 bool flag = !width.empty();
651 int const j = column_of_cell(cell);
653 column_info[j].p_width = width;
654 if (flag) // do this only if there is a width
655 SetAlignment(cell, LYX_ALIGN_LEFT);
656 for(int i = 0; i < rows_; ++i) {
657 int c = GetCellNumber(i, j);
658 flag = !GetPWidth(c).empty(); // because of multicolumns!
659 GetCellInset(c)->SetAutoBreakRows(flag);
665 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
667 bool const flag = !width.empty();
669 cellinfo_of_cell(cell)->p_width = width;
670 if (IsMultiColumn(cell)) {
671 GetCellInset(cell)->SetAutoBreakRows(flag);
678 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
679 LyXTabular::Feature what)
681 if (what == SET_SPECIAL_MULTI)
682 cellinfo_of_cell(cell)->align_special = special;
684 column_info[column_of_cell(cell)].align_special = special;
689 bool LyXTabular::SetAllLines(int cell, bool line)
691 SetTopLine(cell, line);
692 SetBottomLine(cell, line);
693 SetRightLine(cell, line);
694 SetLeftLine(cell, line);
699 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
701 int const row = row_of_cell(cell);
703 if (onlycolumn || !IsMultiColumn(cell))
704 row_info[row].top_line = line;
706 cellinfo_of_cell(cell)->top_line = line;
711 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
713 if (onlycolumn || !IsMultiColumn(cell))
714 row_info[row_of_cell(cell)].bottom_line = line;
716 cellinfo_of_cell(cell)->bottom_line = line;
721 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
723 if (onlycolumn || !IsMultiColumn(cell))
724 column_info[column_of_cell(cell)].left_line = line;
726 cellinfo_of_cell(cell)->left_line = line;
731 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
733 if (onlycolumn || !IsMultiColumn(cell))
734 column_info[right_column_of_cell(cell)].right_line = line;
736 cellinfo_of_cell(cell)->right_line = line;
741 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
743 if (!onlycolumn && IsMultiColumn(cell))
744 return cellinfo_of_cell(cell)->alignment;
746 return column_info[column_of_cell(cell)].alignment;
750 LyXTabular::VAlignment LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
752 if (!onlycolumn && IsMultiColumn(cell))
753 return cellinfo_of_cell(cell)->valignment;
755 return column_info[column_of_cell(cell)].valignment;
759 string const LyXTabular::GetPWidth(int cell) const
761 if (IsMultiColumn(cell))
762 return cellinfo_of_cell(cell)->p_width;
763 return column_info[column_of_cell(cell)].p_width;
767 string const LyXTabular::GetColumnPWidth(int cell) const
769 return column_info[column_of_cell(cell)].p_width;
773 string const LyXTabular::GetMColumnPWidth(int cell) const
775 if (IsMultiColumn(cell))
776 return cellinfo_of_cell(cell)->p_width;
781 string const LyXTabular::GetAlignSpecial(int cell, int what) const
783 if (what == SET_SPECIAL_MULTI)
784 return cellinfo_of_cell(cell)->align_special;
785 return column_info[column_of_cell(cell)].align_special;
789 int LyXTabular::GetWidthOfCell(int cell) const
791 int const row = row_of_cell(cell);
792 int const column1 = column_of_cell(cell);
793 int const column2 = right_column_of_cell(cell);
795 for (int i = column1; i <= column2; ++i) {
796 result += cell_info[row][i].width_of_cell;
802 int LyXTabular::GetBeginningOfTextInCell(int cell) const
806 switch (GetAlignment(cell)){
807 case LYX_ALIGN_CENTER:
808 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
810 case LYX_ALIGN_RIGHT:
811 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
812 // + GetAdditionalWidth(cell);
814 default: /* LYX_ALIGN_LEFT: nothing :-) */
824 bool LyXTabular::IsFirstCellInRow(int cell) const
826 return column_of_cell(cell) == 0;
830 int LyXTabular::GetFirstCellInRow(int row) const
834 return cell_info[row][0].cellno;
837 bool LyXTabular::IsLastCellInRow(int cell) const
839 return (right_column_of_cell(cell) == (columns_ - 1));
843 int LyXTabular::GetLastCellInRow(int row) const
847 return cell_info[row][columns_-1].cellno;
851 bool LyXTabular::calculate_width_of_column(int column)
853 int const old_column_width = column_info[column].width_of_column;
856 for (int i = 0; i < rows_; ++i) {
857 maximum = max(cell_info[i][column].width_of_cell, maximum);
859 column_info[column].width_of_column = maximum;
860 return (column_info[column].width_of_column != old_column_width);
865 // calculate the with of the column without regarding REAL MultiColumn
866 // cells. This means MultiColumn-cells spanning more than 1 column.
868 bool LyXTabular::calculate_width_of_column_NMC(int column)
870 int const old_column_width = column_info[column].width_of_column;
872 for (int i = 0; i < rows_; ++i) {
873 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
874 (cell_info[i][column].width_of_cell > max)) {
875 max = cell_info[i][column].width_of_cell;
878 column_info[column].width_of_column = max;
879 return (column_info[column].width_of_column != old_column_width);
883 void LyXTabular::calculate_width_of_tabular()
885 width_of_tabular = 0;
886 for (int i = 0; i < columns_; ++i) {
887 width_of_tabular += column_info[i].width_of_column;
892 int LyXTabular::row_of_cell(int cell) const
894 if (cell >= numberofcells)
898 return rowofcell[cell];
902 int LyXTabular::column_of_cell(int cell) const
904 if (cell >= numberofcells)
908 return columnofcell[cell];
912 int LyXTabular::right_column_of_cell(int cell) const
914 int const row = row_of_cell(cell);
915 int column = column_of_cell(cell);
916 while (column < (columns_ - 1) &&
917 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
923 const string write_attribute(const string name, const int value)
925 string str = " " + name + "=\"" + tostr(value) + "\"";
930 const string write_attribute(string name, const string & value)
932 string str = " " + name + "=\"" + value + "\"";
937 const string write_attribute(string name, const bool value)
939 string str = " " + name + "=\"" + tostr((int)value) + "\"";
944 void LyXTabular::Write(Buffer const * buf, ostream & os) const
947 os << "<LyXTabular" <<
948 write_attribute("version", 1) <<
949 write_attribute("rows", rows_) <<
950 write_attribute("columns", columns_) <<
952 // global longtable options
954 write_attribute("rotate", rotate) <<
955 write_attribute("islongtable", is_long_tabular) <<
956 write_attribute("endhead", endhead) <<
957 write_attribute("endfirsthead", endfirsthead) <<
958 write_attribute("endfoot", endfoot) <<
959 write_attribute("endlastfoot", endlastfoot) <<
961 for (int i = 0; i < rows_; ++i) {
963 write_attribute("topline", row_info[i].top_line) <<
964 write_attribute("bottomline", row_info[i].bottom_line) <<
965 write_attribute("newpage", row_info[i].newpage) <<
967 for (int j = 0; j < columns_; ++j) {
970 write_attribute("alignment", column_info[j].alignment) <<
971 write_attribute("valignment", column_info[j].valignment) <<
972 write_attribute("leftline", column_info[j].left_line) <<
973 write_attribute("rightline", column_info[j].right_line) <<
974 write_attribute("width", VSpace(column_info[j].p_width).asLyXCommand()) <<
975 write_attribute("special", column_info[j].align_special) <<
981 write_attribute("multicolumn", cell_info[i][j].multicolumn) <<
982 write_attribute("alignment", cell_info[i][j].alignment) <<
983 write_attribute("valignment", cell_info[i][j].valignment) <<
984 write_attribute("topline", cell_info[i][j].top_line) <<
985 write_attribute("bottomline", cell_info[i][j].bottom_line) <<
986 write_attribute("leftline", cell_info[i][j].left_line) <<
987 write_attribute("rightline", cell_info[i][j].right_line) <<
988 write_attribute("rotate", cell_info[i][j].rotate) <<
989 write_attribute("usebox", (int)cell_info[i][j].usebox) <<
990 write_attribute("width", cell_info[i][j].p_width) <<
991 write_attribute("special", cell_info[i][j].align_special) <<
993 os << "\\begin_inset ";
994 cell_info[i][j].inset.Write(buf, os);
995 os << "\n\\end_inset \n"
1001 os << "</LyXTabular>\n";
1006 bool getTokenValue(string const str, const char * token, string & ret)
1008 int pos = str.find(token);
1009 char ch = str[pos+strlen(token)];
1011 if ((pos < 0) || (ch != '='))
1014 pos += strlen(token)+1;
1016 if ((ch != '"') && (ch != '\'')) { // only read till next space
1020 while((pos < int(str.length()-1)) && (str[++pos] != ch))
1028 bool getTokenValue(string const str, const char * token, int & num)
1030 int pos = str.find(token);
1031 char ch = str[pos+strlen(token)];
1033 if ((pos < 0) || (ch != '='))
1036 //ret.erase(); // why? (Lgb)
1037 pos += strlen(token)+1;
1039 if ((ch != '"') && (ch != '\'')) { // only read till next space
1045 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1048 num = strToInt(ret);
1054 bool getTokenValue(string const str, const char * token, LyXAlignment & num)
1057 bool ret = getTokenValue(str, token, tmp);
1058 num = static_cast<LyXAlignment>(tmp);
1064 bool getTokenValue(string const str, const char * token,
1065 LyXTabular::VAlignment & num)
1068 bool ret = getTokenValue(str, token, tmp);
1069 num = static_cast<LyXTabular::VAlignment>(tmp);
1075 bool getTokenValue(string const str, const char * token,
1076 LyXTabular::BoxType & num)
1079 bool ret = getTokenValue(str, token, tmp);
1080 num = static_cast<LyXTabular::BoxType>(tmp);
1086 bool getTokenValue(string const str, const char * token, bool & flag)
1088 int pos = str.find(token);
1089 char ch = str[pos+strlen(token)];
1091 if ((pos < 0) || (ch != '='))
1094 //ret.erase(); // Why? (Lgb)
1095 pos += strlen(token)+1;
1097 if ((ch != '"') && (ch != '\'')) { // only read till next space
1103 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1106 flag = strToInt(ret);
1112 void l_getline(istream & is, string & str)
1120 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1123 istream & is = lex.getStream();
1125 l_getline(is, line);
1126 if (!prefixIs(line, "<LyXTabular ")) {
1127 OldFormatRead(lex, line);
1134 if (!getTokenValue(line, "version", version))
1136 if (!getTokenValue(line, "rows", rows_arg))
1138 if (!getTokenValue(line, "columns", columns_arg))
1140 Init(rows_arg, columns_arg);
1141 l_getline(is, line);
1142 if (!prefixIs(line, "<Features ")) {
1143 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1144 line << ")" << endl;
1147 getTokenValue(line, "islongtable", is_long_tabular);
1148 getTokenValue(line, "endhead", endhead);
1149 getTokenValue(line, "endfirsthead", endfirsthead);
1150 getTokenValue(line, "endfoot", endfoot);
1151 getTokenValue(line, "endlastfoot", endlastfoot);
1153 for(i = 0; i < rows_; ++i) {
1154 l_getline(is, line);
1155 if (!prefixIs(line, "<Row ")) {
1156 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1157 line << ")" << endl;
1160 getTokenValue(line, "topline", row_info[i].top_line);
1161 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1162 getTokenValue(line, "newpage", row_info[i].newpage);
1163 for (j = 0; j < columns_; ++j) {
1165 if (!prefixIs(line,"<Column")) {
1166 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1167 line << ")" << endl;
1171 getTokenValue(line, "alignment", column_info[j].alignment);
1172 getTokenValue(line, "valignment", column_info[j].valignment);
1173 getTokenValue(line, "leftline", column_info[j].left_line);
1174 getTokenValue(line, "rightline", column_info[j].right_line);
1175 getTokenValue(line, "width", column_info[j].p_width);
1176 getTokenValue(line, "special", column_info[j].align_special);
1178 l_getline(is, line);
1179 if (!prefixIs(line, "<Cell")) {
1180 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1181 line << ")" << endl;
1184 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1185 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1186 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1187 getTokenValue(line, "topline", cell_info[i][j].top_line);
1188 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1189 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1190 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1191 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1192 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1193 getTokenValue(line, "width", cell_info[i][j].p_width);
1194 getTokenValue(line, "special", cell_info[i][j].align_special);
1195 l_getline(is, line);
1196 if (prefixIs(line, "\\begin_inset")) {
1197 cell_info[i][j].inset.Read(buf, lex);
1198 l_getline(is, line);
1200 if (line != "</Cell>") {
1201 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1202 line << ")" << endl;
1205 l_getline(is, line);
1206 if (line != "</Column>") {
1207 lyxerr << "Wrong tabular format (expected </Column> got" <<
1208 line << ")" << endl;
1212 l_getline(is, line);
1213 if (line != "</Row>") {
1214 lyxerr << "Wrong tabular format (expected </Row> got" <<
1215 line << ")" << endl;
1219 while (line != "</LyXTabular>") {
1220 l_getline(is, line);
1222 set_row_column_number_info();
1226 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1231 int columns_arg = 0;
1232 int is_long_tabular_arg = false;
1233 int rotate_arg = false;
1243 istream & is = lex.getStream();
1246 version = lyx::atoi(s.substr(8, string::npos));
1250 vector<int> cont_row_info;
1253 lyxerr << "Tabular format < 5 is not supported anymore\n"
1254 "Get an older version of LyX (< 1.1.x) for conversion!"
1256 WriteAlert(_("Warning:"),
1257 _("Tabular format < 5 is not supported anymore\n"),
1258 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1260 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1261 >> rotate_arg >> a >> b >> c >> d;
1263 is >> rows_arg >> columns_arg;
1264 Init(rows_arg, columns_arg);
1265 cont_row_info = vector<int>(rows_arg);
1266 SetLongTabular(is_long_tabular_arg);
1267 SetRotateTabular(rotate_arg);
1269 for (i = 0; i < rows_; ++i) {
1271 cont_row_info[i] = false;
1273 for (i = 0; i < columns_; ++i) {
1276 for (i = 0; i < rows_; ++i) {
1277 for (j = 0; j < columns_; ++j) {
1282 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1283 >> rotate_arg >> a >> b >> c >> d;
1284 Init(rows_arg, columns_arg);
1285 cont_row_info = vector<int>(rows_arg);
1286 SetLongTabular(is_long_tabular_arg);
1287 SetRotateTabular(rotate_arg);
1292 for (i = 0; i < rows_; ++i) {
1293 a = b = c = d = e = f = g = h = 0;
1294 is >> a >> b >> c >> d;
1295 row_info[i].top_line = a;
1296 row_info[i].bottom_line = b;
1297 cont_row_info[i] = c;
1298 row_info[i].newpage = d;
1300 for (i = 0; i < columns_; ++i) {
1305 char ch; // skip '"'
1308 // ignore is buggy but we will use it later (Lgb)
1309 is.ignore(); // skip '"'
1311 getline(is, s1, '"');
1313 is >> ch; // skip '"'
1315 // ignore is buggy but we will use it later (Lgb)
1316 is.ignore(); // skip '"'
1318 getline(is, s2, '"');
1319 column_info[i].alignment = static_cast<LyXAlignment>(a);
1320 column_info[i].left_line = b;
1321 column_info[i].right_line = c;
1322 column_info[i].p_width = s1;
1323 column_info[i].align_special = s2;
1325 for (i = 0; i < rows_; ++i) {
1326 for (j = 0; j < columns_; ++j) {
1329 is >> a >> b >> c >> d >> e >> f >> g;
1332 is >> ch; // skip '"'
1334 // ignore is buggy but we will use it later (Lgb)
1335 is.ignore(); // skip '"'
1337 getline(is, s1, '"');
1339 is >> ch; // skip '"'
1341 // ignore is buggy but we will use it later (Lgb)
1342 is.ignore(); // skip '"'
1344 getline(is, s2, '"');
1345 cell_info[i][j].multicolumn = static_cast<char>(a);
1346 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1347 cell_info[i][j].top_line = static_cast<char>(c);
1348 cell_info[i][j].bottom_line = static_cast<char>(d);
1349 cell_info[i][j].left_line = column_info[j].left_line;
1350 cell_info[i][j].right_line = column_info[j].right_line;
1351 cell_info[i][j].rotate = static_cast<bool>(f);
1352 cell_info[i][j].usebox = static_cast<BoxType>(g);
1353 cell_info[i][j].align_special = s1;
1354 cell_info[i][j].p_width = s2;
1358 set_row_column_number_info(true);
1360 LyXParagraph * par = new LyXParagraph;
1361 LyXParagraph * return_par = 0;
1363 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1364 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1369 LyXFont font(LyXFont::ALL_SANE);
1371 while (lex.IsOK()) {
1373 string const token = lex.GetString();
1376 if (token == "\\layout"
1377 || token == "\\end_float"
1378 || token == "\\end_deeper") {
1379 lex.pushToken(token);
1382 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1394 lex.pushToken(token);
1398 lex.printError("New Paragraph allocated! This should not happen!");
1399 lex.pushToken(token);
1405 // now we have the par we should fill the insets with this!
1407 InsetText * inset = GetCellInset(cell);
1410 for(int i = 0; i < par->Last(); ++i) {
1411 if (par->IsNewline(i)) {
1413 if (cell > GetNumberOfCells()) {
1414 lyxerr << "Some error in reading old table format occured!" <<
1415 endl << "Terminating when reading cell[" << cell << "]!" <<
1420 row = row_of_cell(cell);
1421 if (cont_row_info[row]) {
1423 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1424 while(!IsFirstCellInRow(--cell));
1426 inset = GetCellInset(cell);
1429 inset = GetCellInset(cell);
1430 row = row_of_cell(cell);
1431 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1433 // insert a space instead
1435 par->InsertChar(i, ' ');
1438 par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
1439 inset->par->InsertFromMinibuffer(inset->par->Last());
1446 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1448 int const i = isColumn ? cell : column_of_cell(cell);
1450 if (!isColumn && IsMultiColumn(cell)) {
1451 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1452 return cellinfo_of_cell(cell)->align_special;
1454 switch (GetAlignment(cell)) {
1455 case LYX_ALIGN_LEFT:
1457 case LYX_ALIGN_RIGHT:
1464 if (!column_info[i].align_special.empty()) {
1465 return column_info[i].align_special;
1467 #ifdef IGNORE_THIS_FOR_NOW
1468 else if (!column_info[i].p_width.empty()) {
1470 file += column_info[i].p_width;
1475 switch (column_info[i].alignment) {
1476 case LYX_ALIGN_LEFT:
1478 case LYX_ALIGN_RIGHT:
1488 // cell <0 will tex the preamble
1489 // returns the number of printed newlines
1490 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1493 if (IsLastCell(cell)) {
1494 os << newlineAndDepth(--depth)
1496 << newlineAndDepth(--depth)
1498 << newlineAndDepth(--depth)
1500 << newlineAndDepth(--depth);
1501 if (is_long_tabular)
1505 << newlineAndDepth(--depth);
1510 if (is_long_tabular)
1516 << "' COLSEP='1' ROWSEP='1'>"
1517 << newlineAndDepth(++depth);
1519 for (int i = 0; i < columns_; ++i) {
1520 os << "<COLSPEC ALIGN='"
1521 << GetDocBookAlign(i, true)
1527 if (i == (columns_-1)) {
1530 if (column_info[i].right_line ||
1531 column_info[i+1].left_line)
1537 << newlineAndDepth(depth);
1539 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1540 if (column_info[i].left_line)
1545 << newlineAndDepth(++depth)
1547 << newlineAndDepth(++depth)
1549 << GetDocBookAlign(0)
1551 if (IsMultiColumn(0)) {
1552 os << " NAMEST='col1' NAMEEND='col"
1553 << cells_in_multicolumn(0)
1557 << newlineAndDepth(++depth);
1560 if (IsLastCellInRow(cell)) {
1561 os << newlineAndDepth(--depth)
1563 << newlineAndDepth(--depth)
1565 << newlineAndDepth(depth)
1567 << newlineAndDepth(++depth)
1569 << GetDocBookAlign(cell + 1)
1570 << "' VALIGN='middle'";
1571 if (IsMultiColumn(cell + 1)) {
1572 os << " NAMEST='col"
1573 << column_of_cell(cell + 1) + 1
1575 << column_of_cell(cell + 1) +
1576 cells_in_multicolumn(cell + 1)
1580 << newlineAndDepth(++depth);
1583 os << newlineAndDepth(--depth)
1585 << newlineAndDepth(depth)
1587 << GetDocBookAlign(cell + 1)
1588 << "' VALIGN='middle'";
1589 if (IsMultiColumn(cell + 1)) {
1590 os << " NAMEST='col"
1591 << column_of_cell(cell + 1) + 1
1593 << column_of_cell(cell + 1) +
1594 cells_in_multicolumn(cell + 1)
1598 << newlineAndDepth(++depth);
1607 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1609 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1610 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1614 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1616 int const row = row_of_cell(cell);
1617 int const column = column_of_cell(cell);
1618 return &cell_info[row][column];
1622 void LyXTabular::SetMultiColumn(int cell, int number)
1624 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1625 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1626 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1627 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1628 for (number--; number > 0; --number) {
1629 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1631 set_row_column_number_info();
1635 int LyXTabular::cells_in_multicolumn(int cell) const
1637 int const row = row_of_cell(cell);
1638 int column = column_of_cell(cell);
1641 while ((column < columns_) &&
1642 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1651 int LyXTabular::UnsetMultiColumn(int cell)
1653 int const row = row_of_cell(cell);
1654 int column = column_of_cell(cell);
1658 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1659 cell_info[row][column].multicolumn = CELL_NORMAL;
1661 while ((column < columns_) &&
1662 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1664 cell_info[row][column].multicolumn = CELL_NORMAL;
1669 set_row_column_number_info();
1674 void LyXTabular::SetLongTabular(bool what)
1676 is_long_tabular = what;
1680 bool LyXTabular::IsLongTabular() const
1682 return is_long_tabular;
1686 void LyXTabular::SetRotateTabular(bool flag)
1692 bool LyXTabular::GetRotateTabular() const
1698 void LyXTabular::SetRotateCell(int cell, bool flag)
1700 cellinfo_of_cell(cell)->rotate = flag;
1704 bool LyXTabular::GetRotateCell(int cell) const
1706 return cellinfo_of_cell(cell)->rotate;
1710 bool LyXTabular::NeedRotating() const
1714 for (int i = 0; i < rows_; ++i) {
1715 for (int j = 0; j < columns_; ++j) {
1716 if (cell_info[i][j].rotate)
1724 bool LyXTabular::IsLastCell(int cell) const
1726 if ((cell + 1) < GetNumberOfCells())
1732 int LyXTabular::GetCellAbove(int cell) const
1734 if (row_of_cell(cell) > 0)
1735 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1740 int LyXTabular::GetCellBelow(int cell) const
1742 if (row_of_cell(cell) + 1 < rows_)
1743 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1748 int LyXTabular::GetLastCellAbove(int cell) const
1750 if (row_of_cell(cell) <= 0)
1752 if (!IsMultiColumn(cell))
1753 return GetCellAbove(cell);
1754 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1758 int LyXTabular::GetLastCellBelow(int cell) const
1760 if (row_of_cell(cell) + 1 >= rows_)
1762 if (!IsMultiColumn(cell))
1763 return GetCellBelow(cell);
1764 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1768 int LyXTabular::GetCellNumber(int row, int column) const
1770 if (column >= columns_)
1771 column = columns_ - 1;
1772 else if (column < 0)
1779 return cell_info[row][column].cellno;
1783 void LyXTabular::SetUsebox(int cell, BoxType type)
1785 cellinfo_of_cell(cell)->usebox = type;
1789 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1791 if (column_info[column_of_cell(cell)].p_width.empty() &&
1792 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1794 if (cellinfo_of_cell(cell)->usebox > 1)
1795 return cellinfo_of_cell(cell)->usebox;
1796 return UseParbox(cell);
1800 void LyXTabular::SetLTHead(int cell, bool first)
1802 int const row = row_of_cell(cell);
1803 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1806 if (endfirsthead == val)
1819 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1822 if (abs(endhead) > rows_)
1824 return (row_of_cell(cell) == abs(endhead) - 1);
1828 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1831 if (abs(endfirsthead) > rows_)
1833 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1837 void LyXTabular::SetLTFoot(int cell, bool last)
1839 int const row = row_of_cell(cell);
1840 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1843 if (endlastfoot == val)
1856 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1859 if ((endfoot + 1) > rows_)
1861 return (row_of_cell(cell) == abs(endfoot) - 1);
1865 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1868 if (abs(endlastfoot) > rows_)
1870 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1874 void LyXTabular::SetLTNewPage(int cell, bool what)
1876 row_info[row_of_cell(cell)].newpage = what;
1880 bool LyXTabular::GetLTNewPage(int cell) const
1882 return row_info[row_of_cell(cell)].newpage;
1886 bool LyXTabular::SetAscentOfRow(int row, int height)
1888 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1890 row_info[row].ascent_of_row = height;
1895 bool LyXTabular::SetDescentOfRow(int row, int height)
1897 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1899 row_info[row].descent_of_row = height;
1904 int LyXTabular::GetAscentOfRow(int row) const
1908 return row_info[row].ascent_of_row;
1912 int LyXTabular::GetDescentOfRow(int row) const
1916 return row_info[row].descent_of_row;
1920 int LyXTabular::GetHeightOfTabular() const
1924 for(int row = 0; row < rows_; ++row)
1925 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1926 GetAdditionalHeight(GetCellNumber(row, 0));
1931 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1933 if ((row >= rows_) || (column >= columns_))
1935 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1939 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1941 int const fcell = GetFirstCellInRow(row);
1942 int const n = NumberOfCellsInRow(fcell) + fcell;
1945 for (int i = fcell; i < n; ++i) {
1949 if (tmp == (n - fcell)){
1952 for (int i = fcell; i < n; ++i) {
1955 << column_of_cell(i) + 1
1957 << right_column_of_cell(i) + 1
1969 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1971 int const fcell = GetFirstCellInRow(row);
1972 int const n = NumberOfCellsInRow(fcell) + fcell;
1975 for (int i = fcell; i < n; ++i) {
1979 if (tmp == (n-fcell)){
1982 for (int i = fcell; i < n; ++i) {
1983 if (BottomLine(i)) {
1985 << column_of_cell(i) + 1
1987 << right_column_of_cell(i) + 1
1999 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2003 if (GetRotateCell(cell)) {
2004 os << "\\begin{sideways}" << endl;
2007 if (IsMultiColumn(cell)) {
2008 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2009 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
2010 os << cellinfo_of_cell(cell+1)->align_special << "}{";
2014 if (!GetPWidth(cell).empty()) {
2015 switch(GetVAlignment(cell)) {
2016 case LYX_VALIGN_TOP:
2019 case LYX_VALIGN_CENTER:
2022 case LYX_VALIGN_BOTTOM:
2026 os << "{" << GetPWidth(cell) << '}';
2028 switch (GetAlignment(cell)) {
2029 case LYX_ALIGN_LEFT:
2032 case LYX_ALIGN_RIGHT:
2040 if (RightLine(cell))
2042 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2048 if (GetUsebox(cell) == BOX_PARBOX) {
2050 switch(GetVAlignment(cell)) {
2051 case LYX_VALIGN_TOP:
2054 case LYX_VALIGN_CENTER:
2057 case LYX_VALIGN_BOTTOM:
2061 os << "]{" << GetPWidth(cell) << "}{";
2062 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2063 os << "\\begin{minipage}[";
2064 switch(GetVAlignment(cell)) {
2065 case LYX_VALIGN_TOP:
2068 case LYX_VALIGN_CENTER:
2071 case LYX_VALIGN_BOTTOM:
2075 os << "]{" << GetPWidth(cell) << "}\n";
2082 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2087 if (GetUsebox(cell) == BOX_PARBOX)
2089 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2090 os << "%\n\\end{minipage}";
2093 if (IsMultiColumn(cell)){
2096 if (GetRotateCell(cell)) {
2097 os << "%\n\\end{sideways}";
2104 int LyXTabular::Latex(Buffer const * buf,
2105 ostream & os, bool fragile, bool fp) const
2110 //+---------------------------------------------------------------------
2111 //+ first the opening preamble +
2112 //+---------------------------------------------------------------------
2115 os << "\\begin{sideways}" << endl;
2118 if (is_long_tabular)
2119 os << "\\begin{longtable}{";
2121 os << "\\begin{tabular}{";
2122 for (int i = 0; i < columns_; ++i) {
2123 if (column_info[i].left_line)
2125 if (!column_info[i].align_special.empty()) {
2126 os << column_info[i].align_special;
2127 } else if (!column_info[i].p_width.empty()) {
2128 switch(column_info[i].valignment) {
2129 case LYX_VALIGN_TOP:
2132 case LYX_VALIGN_CENTER:
2135 case LYX_VALIGN_BOTTOM:
2140 << column_info[i].p_width
2143 switch (column_info[i].alignment) {
2144 case LYX_ALIGN_LEFT:
2147 case LYX_ALIGN_RIGHT:
2155 if (column_info[i].right_line)
2161 //+---------------------------------------------------------------------
2162 //+ the single row and columns (cells) +
2163 //+---------------------------------------------------------------------
2166 for(int i = 0; i < rows_; ++i) {
2167 ret += TeXTopHLine(os, i);
2169 if (IsLongTabular()) {
2170 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2171 os << "\\endhead\n";
2174 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2175 os << "\\endfirsthead\n";
2178 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2179 os << "\\endfoot\n";
2182 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2183 os << "\\endlastfoot\n";
2189 ret += TeXBottomHLine(os, i-1);
2190 ret += TeXTopHLine(os, i);
2192 for(int j = 0; j < columns_; ++j) {
2193 if (IsPartOfMultiColumn(i,j))
2195 ret += TeXCellPreamble(os, cell);
2196 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2197 ret += TeXCellPostamble(os, cell);
2198 if (!IsLastCellInRow(cell)) { // not last cell in row
2204 os << "\\\\" << endl;
2205 ret += TeXBottomHLine(os, i);
2207 if (IsLongTabular()) {
2208 if ((endhead > 0) && (i == (endhead - 1))) {
2209 os << "\\endhead\n";
2212 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2213 os << "\\endfirsthead\n";
2216 if ((endfoot > 0) && (i == (endfoot - 1))) {
2217 os << "\\endfoot\n";
2220 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2221 os << "\\endlastfoot\n";
2225 ret += TeXBottomHLine(os, i);
2226 if (row_info[i].newpage) {
2227 os << "\\newpage\n";
2233 //+---------------------------------------------------------------------
2234 //+ the closing of the tabular +
2235 //+---------------------------------------------------------------------
2237 if (is_long_tabular)
2238 os << "\\end{longtable}";
2240 os << "\\end{tabular}";
2242 os << "\n\\end{sideways}";
2250 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2255 //+---------------------------------------------------------------------
2256 //+ first the opening preamble +
2257 //+---------------------------------------------------------------------
2259 os << "<tgroup cols=\"" << columns_
2260 << "\" colsep=\"1\" rowsep=\"1\">" << endl;
2262 for (int i = 0; i < columns_; ++i) {
2263 os << "<colspec colname=\"col" << i << "\" align=\"";
2264 switch (column_info[i].alignment) {
2265 case LYX_ALIGN_LEFT:
2268 case LYX_ALIGN_RIGHT:
2275 os << "\"/>" << endl;
2279 //+---------------------------------------------------------------------
2280 //+ the single row and columns (cells) +
2281 //+---------------------------------------------------------------------
2283 os << "<tbody>" << endl;
2284 for(int i = 0; i < rows_; ++i) {
2285 os << "<row>" << endl;
2286 for(int j = 0; j < columns_; ++j) {
2287 if (IsPartOfMultiColumn(i,j))
2290 os << "<entry align=\"";
2291 switch(GetAlignment(cell)) {
2292 case LYX_ALIGN_LEFT:
2295 case LYX_ALIGN_RIGHT:
2303 os << "\" valign=\"";
2304 switch(GetVAlignment(cell)) {
2305 case LYX_VALIGN_TOP:
2308 case LYX_VALIGN_BOTTOM:
2311 case LYX_VALIGN_CENTER:
2316 if (IsMultiColumn(cell)) {
2317 os << " namest=\"col" << j << "\" ";
2318 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2322 ret += GetCellInset(cell)->DocBook(buf, os);
2326 os << "</row>" << endl;
2328 os << "</tbody>" << endl;
2329 //+---------------------------------------------------------------------
2330 //+ the closing of the tabular +
2331 //+---------------------------------------------------------------------
2340 static void print_n_chars(ostream & os, unsigned char ch, int const n)
2342 for(int i=0; i < n; ++i)
2346 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2347 vector<unsigned int> const & clen) const
2349 int const fcell = GetFirstCellInRow(row);
2350 int const n = NumberOfCellsInRow(fcell) + fcell;
2356 for (int i = fcell; i < n; ++i) {
2363 for (int i = fcell; i < n; ++i) {
2374 column = column_of_cell(i);
2376 while(IsPartOfMultiColumn(row, ++column))
2377 len += clen[column] + 4;
2378 print_n_chars(os, ch, len);
2393 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2394 vector<unsigned int> const & clen) const
2396 int const fcell = GetFirstCellInRow(row);
2397 int const n = NumberOfCellsInRow(fcell) + fcell;
2403 for (int i = fcell; i < n; ++i) {
2410 for (int i = fcell; i < n; ++i) {
2411 if (BottomLine(i)) {
2421 column = column_of_cell(i);
2423 while(IsPartOfMultiColumn(row, ++column))
2424 len += clen[column] + 4;
2425 print_n_chars(os, ch, len);
2426 if (BottomLine(i)) {
2440 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2441 int cell, int row, int column,
2442 vector<unsigned int> const & clen) const
2445 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2452 unsigned int len1 = sstr.str().length();
2453 unsigned int len2 = clen[column];
2454 while(IsPartOfMultiColumn(row, ++column))
2455 len2 += clen[column] + 4;
2458 switch(GetAlignment(cell)) {
2460 case LYX_ALIGN_LEFT:
2463 case LYX_ALIGN_RIGHT:
2467 case LYX_ALIGN_CENTER:
2473 for(unsigned int i = 0; i < len1; ++i)
2476 for(unsigned int i = 0; i < len2; ++i)
2478 if (RightLine(cell))
2483 return ret * 0; // eh? (Lgb)
2487 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2491 //+---------------------------------------------------------------------
2492 //+ first calculate the width of the single columns +
2493 //+---------------------------------------------------------------------
2494 vector<unsigned int> clen(columns_);
2496 // first all non (real) multicolumn cells!
2497 for(int j = 0; j < columns_; ++j) {
2499 for(int i = 0; i < rows_; ++i) {
2500 int cell = GetCellNumber(i, j);
2501 if (IsMultiColumn(cell, true))
2504 GetCellInset(cell)->Ascii(buf, sstr, 0);
2505 if (clen[j] < sstr.str().length())
2506 clen[j] = sstr.str().length();
2509 // then all (real) multicolumn cells!
2510 for(int j = 0; j < columns_; ++j) {
2511 for(int i = 0; i < rows_; ++i) {
2512 int cell = GetCellNumber(i, j);
2513 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2516 GetCellInset(cell)->Ascii(buf, sstr, 0);
2517 int len = int(sstr.str().length());
2518 int const n = cells_in_multicolumn(cell);
2519 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2521 if (len > int(clen[j + n - 1]))
2522 clen[j + n - 1] = len;
2526 for(int i = 0; i < rows_; ++i) {
2527 AsciiTopHLine(os, i, clen);
2528 for(int j = 0; j < columns_; ++j) {
2529 if (IsPartOfMultiColumn(i,j))
2531 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2535 AsciiBottomHLine(os, i, clen);
2541 InsetText * LyXTabular::GetCellInset(int cell) const
2543 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2547 void LyXTabular::Validate(LaTeXFeatures & features) const
2549 if (IsLongTabular())
2550 features.longtable = true;
2552 features.rotating = true;
2553 for(int cell = 0; cell < numberofcells; ++cell) {
2554 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2555 features.array = true;
2556 GetCellInset(cell)->Validate(features);
2561 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2563 LyXParagraph * par = GetCellInset(cell)->par;
2565 for(; par; par = par->next) {
2566 for(int i = 0; i < par->Last(); ++i) {
2567 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)