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::operator=(LyXTabular const & lt)
109 // If this and lt is not of the same size we have a serious bug
110 // So then it is ok to throw an exception, or for now
112 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
114 cell_info = lt.cell_info;
115 row_info = lt.row_info;
116 column_info = lt.column_info;
118 // long tabular stuff
119 SetLongTabular(lt.is_long_tabular);
120 endhead = lt.endhead;
121 endfoot = lt.endfoot;
122 endfirsthead = lt.endfirsthead;
123 endlastfoot = lt.endlastfoot;
133 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
135 LyXTabular * result = new LyXTabular(inset, *this);
136 // don't know if this is good but I need to Clone also
137 // the text-insets here, this is for the Undo-facility!
138 for(int i = 0; i < rows_; ++i) {
139 for(int j = 0; j < columns_; ++j) {
140 result->cell_info[i][j].inset = cell_info[i][j].inset;
141 result->cell_info[i][j].inset.setOwner(inset);
148 /* activates all lines and sets all widths to 0 */
149 void LyXTabular::Init(int rows_arg, int columns_arg)
153 columns_ = columns_arg;
154 row_info = row_vector(rows_, rowstruct());
155 column_info = column_vector(columns_, columnstruct());
156 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
158 // Jürgen, use iterators.
160 for (int i = 0; i < rows_; ++i) {
161 for (int j = 0; j < columns_; ++j) {
162 cell_info[i][j].inset.setOwner(owner_);
163 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
164 cell_info[i][j].cellno = cellno++;
166 cell_info[i][columns_-1].right_line = true;
168 //row_info[i - 1].bottom_line = true;
169 //row_info[0].bottom_line = true;
170 row_info.back().bottom_line = true;
171 row_info.front().bottom_line = true;
173 for (int i = 0; i < columns_; ++i) {
174 calculate_width_of_column(i);
176 //column_info[columns_ - 1].right_line = true;
177 column_info.back().right_line = true;
179 calculate_width_of_tabular();
181 rowofcell = vector<int>();
182 columnofcell = vector<int>();
183 set_row_column_number_info();
184 is_long_tabular = false;
193 void LyXTabular::AppendRow(int cell)
197 int row = row_of_cell(cell);
199 row_vector::iterator rit = row_info.begin() + row;
200 row_info.insert(rit, rowstruct());
203 cell_vvector::iterator cit = cell_info.begin() + row;
204 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
206 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
209 for(int i = 0; i <= row; ++i) {
210 for(int j = 0; j < columns_; ++j) {
211 c_info[i][j] = cell_info[i][j];
214 for(int i = row + 1; i < rows_; ++i) {
215 for(int j = 0; j < columns_; ++j) {
216 c_info[i][j] = cell_info[i-1][j];
221 for (int j = 0; j < columns_; ++j) {
222 cell_info[row][j].inset.clear();
229 void LyXTabular::DeleteRow(int row)
231 // Why make it so hard? (Lgb)
234 if (rows_ == 1) return; // Not allowed to delete last row
236 row_info.erase(row_info.begin() + row); //&row_info[row]);
237 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
243 void LyXTabular::AppendColumn(int cell)
247 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
249 int column = column_of_cell(cell);
250 column_vector::iterator cit = column_info.begin() + column + 1;
251 column_info.insert(cit, columnstruct());
253 for (int i = 0; i < rows_; ++i) {
254 for (int j = 0; j <= column; ++j) {
255 c_info[i][j] = cell_info[i][j];
257 for (int j = column + 1; j < columns_; ++j) {
258 c_info[i][j] = cell_info[i][j - 1];
260 // care about multicolumns
261 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
262 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
264 if ((column + 1) == columns_ ||
265 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
266 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
271 for (int i = 0; i < rows_; ++i) {
272 cell_info[i][column].inset.clear();
278 void LyXTabular::DeleteColumn(int column)
280 // Similar to DeleteRow
281 //if (!(columns_ - 1))
283 if (columns_ == 1) return; // Not allowed to delete last column
285 column_info.erase(column_info.begin() + column);
286 for (int i = 0; i < rows_; ++i) {
287 cell_info[i].erase(cell_info[i].begin() + column);
294 void LyXTabular::Reinit()
296 // Jürgen, use iterators.
297 for (int i = 0; i < rows_; ++i) {
298 for (int j = 0; j < columns_; ++j) {
299 cell_info[i][j].width_of_cell = 0;
300 cell_info[i][j].inset.setOwner(owner_);
304 for (int i = 0; i < columns_; ++i) {
305 calculate_width_of_column(i);
307 calculate_width_of_tabular();
309 set_row_column_number_info();
313 void LyXTabular::set_row_column_number_info(bool oldformat)
319 for (; row < rows_; ++row) {
320 for (column = 0; column<columns_; ++column) {
321 if (cell_info[row][column].multicolumn
322 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
324 cell_info[row][column].cellno = numberofcells;
327 ++numberofcells; // because this is one more than as we start from 0
331 rowofcell.resize(numberofcells);
332 columnofcell.resize(numberofcells);
334 while (c < numberofcells && row < rows_ && column < columns_) {
336 columnofcell[c] = column;
340 } while (column < columns_ &&
341 cell_info[row][column].multicolumn
342 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
343 if (column == columns_) {
348 for (row = 0; row < rows_; ++row) {
349 for (column = 0; column<columns_; ++column) {
350 if (IsPartOfMultiColumn(row,column))
352 // now set the right line of multicolumns right for oldformat read
354 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
356 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
357 cell_info[row][column].right_line =
358 cell_info[row][column+cn-1].right_line;
360 cell_info[row][column].inset.SetAutoBreakRows(
361 !GetPWidth(GetCellNumber(row, column)).empty());
367 int LyXTabular::GetNumberOfCells() const
369 return numberofcells;
373 int LyXTabular::NumberOfCellsInRow(int cell) const
375 int const row = row_of_cell(cell);
377 for (int i = 0; i < columns_; ++i) {
378 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
385 /* returns 1 if there is a topline, returns 0 if not */
386 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
388 int const row = row_of_cell(cell);
390 if (!onlycolumn && IsMultiColumn(cell))
391 return cellinfo_of_cell(cell)->top_line;
392 return row_info[row].top_line;
396 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
398 // no bottom line underneath non-existent cells if you please
399 // Isn't that a programming error? Is so this should
400 // be an Assert instead. (Lgb)
401 if(cell >= numberofcells)
404 if (!onlycolumn && IsMultiColumn(cell))
405 return cellinfo_of_cell(cell)->bottom_line;
406 return row_info[row_of_cell(cell)].bottom_line;
410 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
412 if (!onlycolumn && IsMultiColumn(cell))
413 return cellinfo_of_cell(cell)->left_line;
414 return column_info[column_of_cell(cell)].left_line;
418 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
420 if (!onlycolumn && IsMultiColumn(cell))
421 return cellinfo_of_cell(cell)->right_line;
422 return column_info[right_column_of_cell(cell)].right_line;
426 bool LyXTabular::TopAlreadyDrawed(int cell) const
428 if (GetAdditionalHeight(cell))
430 int row = row_of_cell(cell);
432 int column = column_of_cell(cell);
435 && cell_info[row][column].multicolumn
436 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
438 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
439 return row_info[row].bottom_line;
441 return cell_info[row][column].bottom_line;
447 bool LyXTabular::LeftAlreadyDrawed(int cell) const
449 int column = column_of_cell(cell);
451 int row = row_of_cell(cell);
453 (cell_info[row][column].multicolumn ==
454 LyXTabular::CELL_PART_OF_MULTICOLUMN));
455 if (GetAdditionalWidth(cell_info[row][column].cellno))
457 return column_info[column].right_line;
463 bool LyXTabular::IsLastRow(int cell) const
465 return (row_of_cell(cell) == rows_ - 1);
469 int LyXTabular::GetAdditionalHeight(int cell) const
471 int const row = row_of_cell(cell);
477 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
478 switch (cell_info[row - 1][column].multicolumn) {
479 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
480 bottom = cell_info[row - 1][column].bottom_line;
482 case LyXTabular::CELL_NORMAL:
483 bottom = row_info[row - 1].bottom_line;
486 for (int column = 0; column < columns_ - 1 && top; ++column) {
487 switch (cell_info[row][column].multicolumn){
488 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
489 top = cell_info[row][column].top_line;
491 case LyXTabular::CELL_NORMAL:
492 top = row_info[row].top_line;
496 return WIDTH_OF_LINE;
501 int LyXTabular::GetAdditionalWidth(int cell) const
503 // internally already set in SetWidthOfCell
504 // used to get it back in text.C
505 int const col = right_column_of_cell(cell);
506 if (col < columns_ - 1 && column_info[col].right_line &&
507 column_info[col+1].left_line)
508 return WIDTH_OF_LINE;
514 // returns the maximum over all rows
515 int LyXTabular::GetWidthOfColumn(int cell) const
517 int const column1 = column_of_cell(cell);
518 int const column2 = right_column_of_cell(cell);
521 for (; i <= column2; ++i) {
522 result += column_info[i].width_of_column;
528 int LyXTabular::GetWidthOfTabular() const
530 return width_of_tabular;
534 /* returns 1 if a complete update is necessary, otherwise 0 */
535 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
537 if (!IsMultiColumn(cell))
540 int const row = row_of_cell(cell);
541 int const column1 = column_of_cell(cell);
542 int const column2 = right_column_of_cell(cell);
544 // first set columns to 0 so we can calculate the right width
546 for (; i <= column2; ++i) {
547 cell_info[row][i].width_of_cell = 0;
549 // set the width to MAX_WIDTH until width > 0
550 int width = (new_width + 2 * WIDTH_OF_LINE);
552 i < column2 && width > column_info[i].width_of_column;
554 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
555 width -= column_info[i].width_of_column;
558 cell_info[row][i].width_of_cell = width;
564 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
566 int const row = row_of_cell(cell);
567 int const column1 = column_of_cell(cell);
568 int const column2 = right_column_of_cell(cell);
570 // first set columns to 0 so we can calculate the right width
572 for (; i <= column2; ++i)
573 cell_info[row][i].width_of_cell = 0;
574 for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
576 if (i < numberofcells)
577 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
578 SetWidthOfMulticolCell(cell, new_width);
582 /* returns 1 if a complete update is necessary, otherwise 0 */
583 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
585 int const row = row_of_cell(cell);
586 int const column1 = column_of_cell(cell);
590 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
592 if (IsMultiColumn(cell, true)) {
593 tmp = SetWidthOfMulticolCell(cell, new_width);
595 width = (new_width + 2*WIDTH_OF_LINE);
596 cell_info[row][column1].width_of_cell = width;
597 if (column_info[column1].right_line && (column1 < columns_-1) &&
598 column_info[column1+1].left_line) // additional width
599 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
600 tmp = calculate_width_of_column_NMC(column1);
604 for(; i<columns_; ++i)
605 calculate_width_of_column_NMC(i);
606 for(i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
608 if (i < numberofcells)
609 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
610 for(i = 0; i < columns_; ++i)
611 calculate_width_of_column(i);
612 calculate_width_of_tabular();
619 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
621 if (!IsMultiColumn(cell) || onlycolumn)
622 column_info[column_of_cell(cell)].alignment = align;
624 cellinfo_of_cell(cell)->alignment = align;
629 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
631 if (!IsMultiColumn(cell) || onlycolumn)
632 column_info[column_of_cell(cell)].valignment = align;
634 cellinfo_of_cell(cell)->valignment = align;
639 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
641 bool flag = !width.empty();
642 int const j = column_of_cell(cell);
644 column_info[j].p_width = width;
645 if (flag) // do this only if there is a width
646 SetAlignment(cell, LYX_ALIGN_LEFT);
647 for(int i = 0; i < rows_; ++i) {
648 int c = GetCellNumber(i, j);
649 flag = !GetPWidth(c).empty(); // because of multicolumns!
650 GetCellInset(c)->SetAutoBreakRows(flag);
656 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
658 bool const flag = !width.empty();
660 cellinfo_of_cell(cell)->p_width = width;
661 if (IsMultiColumn(cell)) {
662 GetCellInset(cell)->SetAutoBreakRows(flag);
669 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
670 LyXTabular::Feature what)
672 if (what == SET_SPECIAL_MULTI)
673 cellinfo_of_cell(cell)->align_special = special;
675 column_info[column_of_cell(cell)].align_special = special;
680 bool LyXTabular::SetAllLines(int cell, bool line)
682 SetTopLine(cell, line);
683 SetBottomLine(cell, line);
684 SetRightLine(cell, line);
685 SetLeftLine(cell, line);
690 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
692 int const row = row_of_cell(cell);
694 if (onlycolumn || !IsMultiColumn(cell))
695 row_info[row].top_line = line;
697 cellinfo_of_cell(cell)->top_line = line;
702 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
704 if (onlycolumn || !IsMultiColumn(cell))
705 row_info[row_of_cell(cell)].bottom_line = line;
707 cellinfo_of_cell(cell)->bottom_line = line;
712 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
714 if (onlycolumn || !IsMultiColumn(cell))
715 column_info[column_of_cell(cell)].left_line = line;
717 cellinfo_of_cell(cell)->left_line = line;
722 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
724 if (onlycolumn || !IsMultiColumn(cell))
725 column_info[right_column_of_cell(cell)].right_line = line;
727 cellinfo_of_cell(cell)->right_line = line;
732 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
734 if (!onlycolumn && IsMultiColumn(cell))
735 return cellinfo_of_cell(cell)->alignment;
737 return column_info[column_of_cell(cell)].alignment;
741 LyXTabular::VAlignment LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
743 if (!onlycolumn && IsMultiColumn(cell))
744 return cellinfo_of_cell(cell)->valignment;
746 return column_info[column_of_cell(cell)].valignment;
750 string const LyXTabular::GetPWidth(int cell) const
752 if (IsMultiColumn(cell))
753 return cellinfo_of_cell(cell)->p_width;
754 return column_info[column_of_cell(cell)].p_width;
758 string const LyXTabular::GetColumnPWidth(int cell) const
760 return column_info[column_of_cell(cell)].p_width;
764 string const LyXTabular::GetMColumnPWidth(int cell) const
766 if (IsMultiColumn(cell))
767 return cellinfo_of_cell(cell)->p_width;
772 string const LyXTabular::GetAlignSpecial(int cell, int what) const
774 if (what == SET_SPECIAL_MULTI)
775 return cellinfo_of_cell(cell)->align_special;
776 return column_info[column_of_cell(cell)].align_special;
780 int LyXTabular::GetWidthOfCell(int cell) const
782 int const row = row_of_cell(cell);
783 int const column1 = column_of_cell(cell);
784 int const column2 = right_column_of_cell(cell);
786 for (int i = column1; i <= column2; ++i) {
787 result += cell_info[row][i].width_of_cell;
793 int LyXTabular::GetBeginningOfTextInCell(int cell) const
797 switch (GetAlignment(cell)){
798 case LYX_ALIGN_CENTER:
799 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
801 case LYX_ALIGN_RIGHT:
802 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
803 // + GetAdditionalWidth(cell);
805 default: /* LYX_ALIGN_LEFT: nothing :-) */
815 bool LyXTabular::IsFirstCellInRow(int cell) const
817 return column_of_cell(cell) == 0;
821 int LyXTabular::GetFirstCellInRow(int row) const
825 return cell_info[row][0].cellno;
828 bool LyXTabular::IsLastCellInRow(int cell) const
830 return (right_column_of_cell(cell) == (columns_ - 1));
834 int LyXTabular::GetLastCellInRow(int row) const
838 return cell_info[row][columns_-1].cellno;
842 bool LyXTabular::calculate_width_of_column(int column)
844 int const old_column_width = column_info[column].width_of_column;
847 for (int i = 0; i < rows_; ++i) {
848 maximum = max(cell_info[i][column].width_of_cell, maximum);
850 column_info[column].width_of_column = maximum;
851 return (column_info[column].width_of_column != old_column_width);
856 // calculate the with of the column without regarding REAL MultiColumn
857 // cells. This means MultiColumn-cells spanning more than 1 column.
859 bool LyXTabular::calculate_width_of_column_NMC(int column)
861 int const old_column_width = column_info[column].width_of_column;
863 for (int i = 0; i < rows_; ++i) {
864 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
865 (cell_info[i][column].width_of_cell > max)) {
866 max = cell_info[i][column].width_of_cell;
869 column_info[column].width_of_column = max;
870 return (column_info[column].width_of_column != old_column_width);
874 void LyXTabular::calculate_width_of_tabular()
876 width_of_tabular = 0;
877 for (int i = 0; i < columns_; ++i) {
878 width_of_tabular += column_info[i].width_of_column;
883 int LyXTabular::row_of_cell(int cell) const
885 if (cell >= numberofcells)
889 return rowofcell[cell];
893 int LyXTabular::column_of_cell(int cell) const
895 if (cell >= numberofcells)
899 return columnofcell[cell];
903 int LyXTabular::right_column_of_cell(int cell) const
905 int const row = row_of_cell(cell);
906 int column = column_of_cell(cell);
907 while (column < (columns_ - 1) &&
908 cell_info[row][column+1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
914 const string write_attribute(const string name, const int value)
916 string str = " " + name + "=\"" + tostr(value) + "\"";
921 const string write_attribute(string name, const string & value)
923 string str = " " + name + "=\"" + value + "\"";
928 const string write_attribute(string name, const bool value)
930 string str = " " + name + "=\"" + tostr((int)value) + "\"";
935 void LyXTabular::Write(Buffer const * buf, ostream & os) const
938 os << "<LyXTabular" <<
939 write_attribute("version", 1) <<
940 write_attribute("rows", rows_) <<
941 write_attribute("columns", columns_) <<
943 // global longtable options
945 write_attribute("rotate", rotate) <<
946 write_attribute("islongtable", is_long_tabular) <<
947 write_attribute("endhead", endhead) <<
948 write_attribute("endfirsthead", endfirsthead) <<
949 write_attribute("endfoot", endfoot) <<
950 write_attribute("endlastfoot", endlastfoot) <<
952 for (int i = 0; i < rows_; ++i) {
954 write_attribute("topline", row_info[i].top_line) <<
955 write_attribute("bottomline", row_info[i].bottom_line) <<
956 write_attribute("newpage", row_info[i].newpage) <<
958 for (int j = 0; j < columns_; ++j) {
961 write_attribute("alignment", column_info[j].alignment) <<
962 write_attribute("valignment", column_info[j].valignment) <<
963 write_attribute("leftline", column_info[j].left_line) <<
964 write_attribute("rightline", column_info[j].right_line) <<
965 write_attribute("width", VSpace(column_info[j].p_width).asLyXCommand()) <<
966 write_attribute("special", column_info[j].align_special) <<
972 write_attribute("multicolumn", cell_info[i][j].multicolumn) <<
973 write_attribute("alignment", cell_info[i][j].alignment) <<
974 write_attribute("valignment", cell_info[i][j].valignment) <<
975 write_attribute("topline", cell_info[i][j].top_line) <<
976 write_attribute("bottomline", cell_info[i][j].bottom_line) <<
977 write_attribute("leftline", cell_info[i][j].left_line) <<
978 write_attribute("rightline", cell_info[i][j].right_line) <<
979 write_attribute("rotate", cell_info[i][j].rotate) <<
980 write_attribute("usebox", (int)cell_info[i][j].usebox) <<
981 write_attribute("width", cell_info[i][j].p_width) <<
982 write_attribute("special", cell_info[i][j].align_special) <<
984 os << "\\begin_inset ";
985 cell_info[i][j].inset.Write(buf, os);
986 os << "\n\\end_inset \n"
992 os << "</LyXTabular>\n";
997 bool getTokenValue(string const str, const char * token, string & ret)
999 int pos = str.find(token);
1000 char ch = str[pos+strlen(token)];
1002 if ((pos < 0) || (ch != '='))
1005 pos += strlen(token)+1;
1007 if ((ch != '"') && (ch != '\'')) { // only read till next space
1011 while((pos < int(str.length()-1)) && (str[++pos] != ch))
1019 bool getTokenValue(string const str, const char * token, int & num)
1021 int pos = str.find(token);
1022 char ch = str[pos+strlen(token)];
1024 if ((pos < 0) || (ch != '='))
1027 //ret.erase(); // why? (Lgb)
1028 pos += strlen(token)+1;
1030 if ((ch != '"') && (ch != '\'')) { // only read till next space
1036 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1039 num = strToInt(ret);
1045 bool getTokenValue(string const str, const char * token, LyXAlignment & num)
1048 bool ret = getTokenValue(str, token, tmp);
1049 num = static_cast<LyXAlignment>(tmp);
1055 bool getTokenValue(string const str, const char * token,
1056 LyXTabular::VAlignment & num)
1059 bool ret = getTokenValue(str, token, tmp);
1060 num = static_cast<LyXTabular::VAlignment>(tmp);
1066 bool getTokenValue(string const str, const char * token,
1067 LyXTabular::BoxType & num)
1070 bool ret = getTokenValue(str, token, tmp);
1071 num = static_cast<LyXTabular::BoxType>(tmp);
1077 bool getTokenValue(string const str, const char * token, bool & flag)
1079 int pos = str.find(token);
1080 char ch = str[pos+strlen(token)];
1082 if ((pos < 0) || (ch != '='))
1085 //ret.erase(); // Why? (Lgb)
1086 pos += strlen(token)+1;
1088 if ((ch != '"') && (ch != '\'')) { // only read till next space
1094 while((pos < int(str.length()-1)) && isdigit(str[pos]))
1097 flag = strToInt(ret);
1103 void l_getline(istream & is, string & str)
1111 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1114 istream & is = lex.getStream();
1116 l_getline(is, line);
1117 if (!prefixIs(line, "<LyXTabular ")) {
1118 OldFormatRead(lex, line);
1125 if (!getTokenValue(line, "version", version))
1127 if (!getTokenValue(line, "rows", rows_arg))
1129 if (!getTokenValue(line, "columns", columns_arg))
1131 Init(rows_arg, columns_arg);
1132 l_getline(is, line);
1133 if (!prefixIs(line, "<Features ")) {
1134 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1135 line << ")" << endl;
1138 getTokenValue(line, "islongtable", is_long_tabular);
1139 getTokenValue(line, "endhead", endhead);
1140 getTokenValue(line, "endfirsthead", endfirsthead);
1141 getTokenValue(line, "endfoot", endfoot);
1142 getTokenValue(line, "endlastfoot", endlastfoot);
1144 for(i = 0; i < rows_; ++i) {
1145 l_getline(is, line);
1146 if (!prefixIs(line, "<Row ")) {
1147 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1148 line << ")" << endl;
1151 getTokenValue(line, "topline", row_info[i].top_line);
1152 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1153 getTokenValue(line, "newpage", row_info[i].newpage);
1154 for (j = 0; j < columns_; ++j) {
1156 if (!prefixIs(line,"<Column")) {
1157 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1158 line << ")" << endl;
1162 getTokenValue(line, "alignment", column_info[j].alignment);
1163 getTokenValue(line, "valignment", column_info[j].valignment);
1164 getTokenValue(line, "leftline", column_info[j].left_line);
1165 getTokenValue(line, "rightline", column_info[j].right_line);
1166 getTokenValue(line, "width", column_info[j].p_width);
1167 getTokenValue(line, "special", column_info[j].align_special);
1169 l_getline(is, line);
1170 if (!prefixIs(line, "<Cell")) {
1171 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1172 line << ")" << endl;
1175 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1176 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1177 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1178 getTokenValue(line, "topline", cell_info[i][j].top_line);
1179 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1180 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1181 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1182 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1183 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1184 getTokenValue(line, "width", cell_info[i][j].p_width);
1185 getTokenValue(line, "special", cell_info[i][j].align_special);
1186 l_getline(is, line);
1187 if (prefixIs(line, "\\begin_inset")) {
1188 cell_info[i][j].inset.Read(buf, lex);
1189 l_getline(is, line);
1191 if (line != "</Cell>") {
1192 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1193 line << ")" << endl;
1196 l_getline(is, line);
1197 if (line != "</Column>") {
1198 lyxerr << "Wrong tabular format (expected </Column> got" <<
1199 line << ")" << endl;
1203 l_getline(is, line);
1204 if (line != "</Row>") {
1205 lyxerr << "Wrong tabular format (expected </Row> got" <<
1206 line << ")" << endl;
1210 while (line != "</LyXTabular>") {
1211 l_getline(is, line);
1213 set_row_column_number_info();
1217 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1222 int columns_arg = 0;
1223 int is_long_tabular_arg = false;
1224 int rotate_arg = false;
1234 istream & is = lex.getStream();
1237 version = lyx::atoi(s.substr(8, string::npos));
1241 vector<int> cont_row_info;
1244 lyxerr << "Tabular format < 5 is not supported anymore\n"
1245 "Get an older version of LyX (< 1.1.x) for conversion!"
1247 WriteAlert(_("Warning:"),
1248 _("Tabular format < 5 is not supported anymore\n"),
1249 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1251 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1252 >> rotate_arg >> a >> b >> c >> d;
1254 is >> rows_arg >> columns_arg;
1255 Init(rows_arg, columns_arg);
1256 cont_row_info = vector<int>(rows_arg);
1257 SetLongTabular(is_long_tabular_arg);
1258 SetRotateTabular(rotate_arg);
1260 for (i = 0; i < rows_; ++i) {
1262 cont_row_info[i] = false;
1264 for (i = 0; i < columns_; ++i) {
1267 for (i = 0; i < rows_; ++i) {
1268 for (j = 0; j < columns_; ++j) {
1273 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1274 >> rotate_arg >> a >> b >> c >> d;
1275 Init(rows_arg, columns_arg);
1276 cont_row_info = vector<int>(rows_arg);
1277 SetLongTabular(is_long_tabular_arg);
1278 SetRotateTabular(rotate_arg);
1283 for (i = 0; i < rows_; ++i) {
1284 a = b = c = d = e = f = g = h = 0;
1285 is >> a >> b >> c >> d;
1286 row_info[i].top_line = a;
1287 row_info[i].bottom_line = b;
1288 cont_row_info[i] = c;
1289 row_info[i].newpage = d;
1291 for (i = 0; i < columns_; ++i) {
1296 char ch; // skip '"'
1299 // ignore is buggy but we will use it later (Lgb)
1300 is.ignore(); // skip '"'
1302 getline(is, s1, '"');
1304 is >> ch; // skip '"'
1306 // ignore is buggy but we will use it later (Lgb)
1307 is.ignore(); // skip '"'
1309 getline(is, s2, '"');
1310 column_info[i].alignment = static_cast<LyXAlignment>(a);
1311 column_info[i].left_line = b;
1312 column_info[i].right_line = c;
1313 column_info[i].p_width = s1;
1314 column_info[i].align_special = s2;
1316 for (i = 0; i < rows_; ++i) {
1317 for (j = 0; j < columns_; ++j) {
1320 is >> a >> b >> c >> d >> e >> f >> g;
1323 is >> ch; // skip '"'
1325 // ignore is buggy but we will use it later (Lgb)
1326 is.ignore(); // skip '"'
1328 getline(is, s1, '"');
1330 is >> ch; // skip '"'
1332 // ignore is buggy but we will use it later (Lgb)
1333 is.ignore(); // skip '"'
1335 getline(is, s2, '"');
1336 cell_info[i][j].multicolumn = static_cast<char>(a);
1337 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1338 cell_info[i][j].top_line = static_cast<char>(c);
1339 cell_info[i][j].bottom_line = static_cast<char>(d);
1340 cell_info[i][j].left_line = column_info[j].left_line;
1341 cell_info[i][j].right_line = column_info[j].right_line;
1342 cell_info[i][j].rotate = static_cast<bool>(f);
1343 cell_info[i][j].usebox = static_cast<BoxType>(g);
1344 cell_info[i][j].align_special = s1;
1345 cell_info[i][j].p_width = s2;
1349 set_row_column_number_info(true);
1351 LyXParagraph * par = new LyXParagraph;
1352 LyXParagraph * return_par = 0;
1354 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1355 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1360 LyXFont font(LyXFont::ALL_SANE);
1362 while (lex.IsOK()) {
1364 string const token = lex.GetString();
1367 if (token == "\\layout"
1368 || token == "\\end_float"
1369 || token == "\\end_deeper") {
1370 lex.pushToken(token);
1373 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1385 lex.pushToken(token);
1389 lex.printError("New Paragraph allocated! This should not happen!");
1390 lex.pushToken(token);
1396 // now we have the par we should fill the insets with this!
1398 InsetText * inset = GetCellInset(cell);
1401 for(int i = 0; i < par->Last(); ++i) {
1402 if (par->IsNewline(i)) {
1404 if (cell > GetNumberOfCells()) {
1405 lyxerr << "Some error in reading old table format occured!" <<
1406 endl << "Terminating when reading cell[" << cell << "]!" <<
1411 row = row_of_cell(cell);
1412 if (cont_row_info[row]) {
1414 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1415 while(!IsFirstCellInRow(--cell));
1417 inset = GetCellInset(cell);
1420 inset = GetCellInset(cell);
1421 row = row_of_cell(cell);
1422 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1424 // insert a space instead
1426 par->InsertChar(i, ' ');
1429 par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
1430 inset->par->InsertFromMinibuffer(inset->par->Last());
1437 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1439 int const i = isColumn ? cell : column_of_cell(cell);
1441 if (!isColumn && IsMultiColumn(cell)) {
1442 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1443 return cellinfo_of_cell(cell)->align_special;
1445 switch (GetAlignment(cell)) {
1446 case LYX_ALIGN_LEFT:
1448 case LYX_ALIGN_RIGHT:
1455 if (!column_info[i].align_special.empty()) {
1456 return column_info[i].align_special;
1458 #ifdef IGNORE_THIS_FOR_NOW
1459 else if (!column_info[i].p_width.empty()) {
1461 file += column_info[i].p_width;
1466 switch (column_info[i].alignment) {
1467 case LYX_ALIGN_LEFT:
1469 case LYX_ALIGN_RIGHT:
1479 // cell <0 will tex the preamble
1480 // returns the number of printed newlines
1481 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1484 if (IsLastCell(cell)) {
1485 os << newlineAndDepth(--depth)
1487 << newlineAndDepth(--depth)
1489 << newlineAndDepth(--depth)
1491 << newlineAndDepth(--depth);
1492 if (is_long_tabular)
1496 << newlineAndDepth(--depth);
1501 if (is_long_tabular)
1507 << "' COLSEP='1' ROWSEP='1'>"
1508 << newlineAndDepth(++depth);
1510 for (int i = 0; i < columns_; ++i) {
1511 os << "<COLSPEC ALIGN='"
1512 << GetDocBookAlign(i, true)
1518 if (i == (columns_-1)) {
1521 if (column_info[i].right_line ||
1522 column_info[i+1].left_line)
1528 << newlineAndDepth(depth);
1530 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1531 if (column_info[i].left_line)
1536 << newlineAndDepth(++depth)
1538 << newlineAndDepth(++depth)
1540 << GetDocBookAlign(0)
1542 if (IsMultiColumn(0)) {
1543 os << " NAMEST='col1' NAMEEND='col"
1544 << cells_in_multicolumn(0)
1548 << newlineAndDepth(++depth);
1551 if (IsLastCellInRow(cell)) {
1552 os << newlineAndDepth(--depth)
1554 << newlineAndDepth(--depth)
1556 << newlineAndDepth(depth)
1558 << newlineAndDepth(++depth)
1560 << GetDocBookAlign(cell + 1)
1561 << "' VALIGN='middle'";
1562 if (IsMultiColumn(cell + 1)) {
1563 os << " NAMEST='col"
1564 << column_of_cell(cell + 1) + 1
1566 << column_of_cell(cell + 1) +
1567 cells_in_multicolumn(cell + 1)
1571 << newlineAndDepth(++depth);
1574 os << newlineAndDepth(--depth)
1576 << newlineAndDepth(depth)
1578 << GetDocBookAlign(cell + 1)
1579 << "' VALIGN='middle'";
1580 if (IsMultiColumn(cell + 1)) {
1581 os << " NAMEST='col"
1582 << column_of_cell(cell + 1) + 1
1584 << column_of_cell(cell + 1) +
1585 cells_in_multicolumn(cell + 1)
1589 << newlineAndDepth(++depth);
1598 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1600 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1601 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1605 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1607 int const row = row_of_cell(cell);
1608 int const column = column_of_cell(cell);
1609 return &cell_info[row][column];
1613 void LyXTabular::SetMultiColumn(int cell, int number)
1615 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1616 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1617 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1618 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1619 for (number--; number > 0; --number) {
1620 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1622 set_row_column_number_info();
1626 int LyXTabular::cells_in_multicolumn(int cell) const
1628 int const row = row_of_cell(cell);
1629 int column = column_of_cell(cell);
1632 while ((column < columns_) &&
1633 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1642 int LyXTabular::UnsetMultiColumn(int cell)
1644 int const row = row_of_cell(cell);
1645 int column = column_of_cell(cell);
1649 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1650 cell_info[row][column].multicolumn = CELL_NORMAL;
1652 while ((column < columns_) &&
1653 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1655 cell_info[row][column].multicolumn = CELL_NORMAL;
1660 set_row_column_number_info();
1665 void LyXTabular::SetLongTabular(bool what)
1667 is_long_tabular = what;
1671 bool LyXTabular::IsLongTabular() const
1673 return is_long_tabular;
1677 void LyXTabular::SetRotateTabular(bool flag)
1683 bool LyXTabular::GetRotateTabular() const
1689 void LyXTabular::SetRotateCell(int cell, bool flag)
1691 cellinfo_of_cell(cell)->rotate = flag;
1695 bool LyXTabular::GetRotateCell(int cell) const
1697 return cellinfo_of_cell(cell)->rotate;
1701 bool LyXTabular::NeedRotating() const
1705 for (int i = 0; i < rows_; ++i) {
1706 for (int j = 0; j < columns_; ++j) {
1707 if (cell_info[i][j].rotate)
1715 bool LyXTabular::IsLastCell(int cell) const
1717 if ((cell + 1) < GetNumberOfCells())
1723 int LyXTabular::GetCellAbove(int cell) const
1725 if (row_of_cell(cell) > 0)
1726 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1731 int LyXTabular::GetCellBelow(int cell) const
1733 if (row_of_cell(cell) + 1 < rows_)
1734 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1739 int LyXTabular::GetLastCellAbove(int cell) const
1741 if (row_of_cell(cell) <= 0)
1743 if (!IsMultiColumn(cell))
1744 return GetCellAbove(cell);
1745 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1749 int LyXTabular::GetLastCellBelow(int cell) const
1751 if (row_of_cell(cell) + 1 >= rows_)
1753 if (!IsMultiColumn(cell))
1754 return GetCellBelow(cell);
1755 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1759 int LyXTabular::GetCellNumber(int row, int column) const
1761 if (column >= columns_)
1762 column = columns_ - 1;
1763 else if (column < 0)
1770 return cell_info[row][column].cellno;
1774 void LyXTabular::SetUsebox(int cell, BoxType type)
1776 cellinfo_of_cell(cell)->usebox = type;
1780 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1782 if (column_info[column_of_cell(cell)].p_width.empty() &&
1783 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1785 if (cellinfo_of_cell(cell)->usebox > 1)
1786 return cellinfo_of_cell(cell)->usebox;
1787 return UseParbox(cell);
1791 void LyXTabular::SetLTHead(int cell, bool first)
1793 int const row = row_of_cell(cell);
1794 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1797 if (endfirsthead == val)
1810 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1813 if (abs(endhead) > rows_)
1815 return (row_of_cell(cell) == abs(endhead) - 1);
1819 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1822 if (abs(endfirsthead) > rows_)
1824 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1828 void LyXTabular::SetLTFoot(int cell, bool last)
1830 int const row = row_of_cell(cell);
1831 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1834 if (endlastfoot == val)
1847 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1850 if ((endfoot + 1) > rows_)
1852 return (row_of_cell(cell) == abs(endfoot) - 1);
1856 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1859 if (abs(endlastfoot) > rows_)
1861 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1865 void LyXTabular::SetLTNewPage(int cell, bool what)
1867 row_info[row_of_cell(cell)].newpage = what;
1871 bool LyXTabular::GetLTNewPage(int cell) const
1873 return row_info[row_of_cell(cell)].newpage;
1877 bool LyXTabular::SetAscentOfRow(int row, int height)
1879 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1881 row_info[row].ascent_of_row = height;
1886 bool LyXTabular::SetDescentOfRow(int row, int height)
1888 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1890 row_info[row].descent_of_row = height;
1895 int LyXTabular::GetAscentOfRow(int row) const
1899 return row_info[row].ascent_of_row;
1903 int LyXTabular::GetDescentOfRow(int row) const
1907 return row_info[row].descent_of_row;
1911 int LyXTabular::GetHeightOfTabular() const
1915 for(int row = 0; row < rows_; ++row)
1916 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1917 GetAdditionalHeight(GetCellNumber(row, 0));
1922 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1924 if ((row >= rows_) || (column >= columns_))
1926 return (cell_info[row][column].multicolumn==CELL_PART_OF_MULTICOLUMN);
1930 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1932 int const fcell = GetFirstCellInRow(row);
1933 int const n = NumberOfCellsInRow(fcell) + fcell;
1936 for (int i = fcell; i < n; ++i) {
1940 if (tmp == (n - fcell)){
1943 for (int i = fcell; i < n; ++i) {
1946 << column_of_cell(i) + 1
1948 << right_column_of_cell(i) + 1
1960 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1962 int const fcell = GetFirstCellInRow(row);
1963 int const n = NumberOfCellsInRow(fcell) + fcell;
1966 for (int i = fcell; i < n; ++i) {
1970 if (tmp == (n-fcell)){
1973 for (int i = fcell; i < n; ++i) {
1974 if (BottomLine(i)) {
1976 << column_of_cell(i) + 1
1978 << right_column_of_cell(i) + 1
1990 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1994 if (GetRotateCell(cell)) {
1995 os << "\\begin{sideways}" << endl;
1998 if (IsMultiColumn(cell)) {
1999 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2000 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
2001 os << cellinfo_of_cell(cell+1)->align_special << "}{";
2005 if (!GetPWidth(cell).empty()) {
2006 switch(GetVAlignment(cell)) {
2007 case LYX_VALIGN_TOP:
2010 case LYX_VALIGN_CENTER:
2013 case LYX_VALIGN_BOTTOM:
2017 os << "{" << GetPWidth(cell) << '}';
2019 switch (GetAlignment(cell)) {
2020 case LYX_ALIGN_LEFT:
2023 case LYX_ALIGN_RIGHT:
2031 if (RightLine(cell))
2033 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2039 if (GetUsebox(cell) == BOX_PARBOX) {
2041 switch(GetVAlignment(cell)) {
2042 case LYX_VALIGN_TOP:
2045 case LYX_VALIGN_CENTER:
2048 case LYX_VALIGN_BOTTOM:
2052 os << "]{" << GetPWidth(cell) << "}{";
2053 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2054 os << "\\begin{minipage}[";
2055 switch(GetVAlignment(cell)) {
2056 case LYX_VALIGN_TOP:
2059 case LYX_VALIGN_CENTER:
2062 case LYX_VALIGN_BOTTOM:
2066 os << "]{" << GetPWidth(cell) << "}\n";
2073 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2078 if (GetUsebox(cell) == BOX_PARBOX)
2080 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2081 os << "%\n\\end{minipage}";
2084 if (IsMultiColumn(cell)){
2087 if (GetRotateCell(cell)) {
2088 os << "%\n\\end{sideways}";
2095 int LyXTabular::Latex(Buffer const * buf,
2096 ostream & os, bool fragile, bool fp) const
2101 //+---------------------------------------------------------------------
2102 //+ first the opening preamble +
2103 //+---------------------------------------------------------------------
2106 os << "\\begin{sideways}" << endl;
2109 if (is_long_tabular)
2110 os << "\\begin{longtable}{";
2112 os << "\\begin{tabular}{";
2113 for (int i = 0; i < columns_; ++i) {
2114 if (column_info[i].left_line)
2116 if (!column_info[i].align_special.empty()) {
2117 os << column_info[i].align_special;
2118 } else if (!column_info[i].p_width.empty()) {
2119 switch(column_info[i].valignment) {
2120 case LYX_VALIGN_TOP:
2123 case LYX_VALIGN_CENTER:
2126 case LYX_VALIGN_BOTTOM:
2131 << column_info[i].p_width
2134 switch (column_info[i].alignment) {
2135 case LYX_ALIGN_LEFT:
2138 case LYX_ALIGN_RIGHT:
2146 if (column_info[i].right_line)
2152 //+---------------------------------------------------------------------
2153 //+ the single row and columns (cells) +
2154 //+---------------------------------------------------------------------
2157 for(int i = 0; i < rows_; ++i) {
2158 ret += TeXTopHLine(os, i);
2160 if (IsLongTabular()) {
2161 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2162 os << "\\endhead\n";
2165 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2166 os << "\\endfirsthead\n";
2169 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2170 os << "\\endfoot\n";
2173 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2174 os << "\\endlastfoot\n";
2180 ret += TeXBottomHLine(os, i-1);
2181 ret += TeXTopHLine(os, i);
2183 for(int j = 0; j < columns_; ++j) {
2184 if (IsPartOfMultiColumn(i,j))
2186 ret += TeXCellPreamble(os, cell);
2187 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2188 ret += TeXCellPostamble(os, cell);
2189 if (!IsLastCellInRow(cell)) { // not last cell in row
2195 os << "\\\\" << endl;
2196 ret += TeXBottomHLine(os, i);
2198 if (IsLongTabular()) {
2199 if ((endhead > 0) && (i == (endhead - 1))) {
2200 os << "\\endhead\n";
2203 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2204 os << "\\endfirsthead\n";
2207 if ((endfoot > 0) && (i == (endfoot - 1))) {
2208 os << "\\endfoot\n";
2211 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2212 os << "\\endlastfoot\n";
2216 ret += TeXBottomHLine(os, i);
2217 if (row_info[i].newpage) {
2218 os << "\\newpage\n";
2224 //+---------------------------------------------------------------------
2225 //+ the closing of the tabular +
2226 //+---------------------------------------------------------------------
2228 if (is_long_tabular)
2229 os << "\\end{longtable}";
2231 os << "\\end{tabular}";
2233 os << "\n\\end{sideways}";
2241 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2246 //+---------------------------------------------------------------------
2247 //+ first the opening preamble +
2248 //+---------------------------------------------------------------------
2250 os << "<tgroup cols=\"" << columns_
2251 << "\" colsep=\"1\" rowsep=\"1\">" << endl;
2253 for (int i = 0; i < columns_; ++i) {
2254 os << "<colspec colname=\"col" << i << "\" align=\"";
2255 switch (column_info[i].alignment) {
2256 case LYX_ALIGN_LEFT:
2259 case LYX_ALIGN_RIGHT:
2266 os << "\"/>" << endl;
2270 //+---------------------------------------------------------------------
2271 //+ the single row and columns (cells) +
2272 //+---------------------------------------------------------------------
2274 os << "<tbody>" << endl;
2275 for(int i = 0; i < rows_; ++i) {
2276 os << "<row>" << endl;
2277 for(int j = 0; j < columns_; ++j) {
2278 if (IsPartOfMultiColumn(i,j))
2281 os << "<entry align=\"";
2282 switch(GetAlignment(cell)) {
2283 case LYX_ALIGN_LEFT:
2286 case LYX_ALIGN_RIGHT:
2294 os << "\" valign=\"";
2295 switch(GetVAlignment(cell)) {
2296 case LYX_VALIGN_TOP:
2299 case LYX_VALIGN_BOTTOM:
2302 case LYX_VALIGN_CENTER:
2307 if (IsMultiColumn(cell)) {
2308 os << " namest=\"col" << j << "\" ";
2309 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2313 ret += GetCellInset(cell)->DocBook(buf, os);
2317 os << "</row>" << endl;
2319 os << "</tbody>" << endl;
2320 //+---------------------------------------------------------------------
2321 //+ the closing of the tabular +
2322 //+---------------------------------------------------------------------
2331 static void print_n_chars(ostream & os, unsigned char ch, int const n)
2333 for(int i=0; i < n; ++i)
2337 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2338 vector<unsigned int> const & clen) const
2340 int const fcell = GetFirstCellInRow(row);
2341 int const n = NumberOfCellsInRow(fcell) + fcell;
2347 for (int i = fcell; i < n; ++i) {
2354 for (int i = fcell; i < n; ++i) {
2365 column = column_of_cell(i);
2367 while(IsPartOfMultiColumn(row, ++column))
2368 len += clen[column] + 4;
2369 print_n_chars(os, ch, len);
2384 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2385 vector<unsigned int> const & clen) const
2387 int const fcell = GetFirstCellInRow(row);
2388 int const n = NumberOfCellsInRow(fcell) + fcell;
2394 for (int i = fcell; i < n; ++i) {
2401 for (int i = fcell; i < n; ++i) {
2402 if (BottomLine(i)) {
2412 column = column_of_cell(i);
2414 while(IsPartOfMultiColumn(row, ++column))
2415 len += clen[column] + 4;
2416 print_n_chars(os, ch, len);
2417 if (BottomLine(i)) {
2431 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2432 int cell, int row, int column,
2433 vector<unsigned int> const & clen) const
2436 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2443 unsigned int len1 = sstr.str().length();
2444 unsigned int len2 = clen[column];
2445 while(IsPartOfMultiColumn(row, ++column))
2446 len2 += clen[column] + 4;
2449 switch(GetAlignment(cell)) {
2451 case LYX_ALIGN_LEFT:
2454 case LYX_ALIGN_RIGHT:
2458 case LYX_ALIGN_CENTER:
2464 for(unsigned int i = 0; i < len1; ++i)
2467 for(unsigned int i = 0; i < len2; ++i)
2469 if (RightLine(cell))
2474 return ret * 0; // eh? (Lgb)
2478 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2482 //+---------------------------------------------------------------------
2483 //+ first calculate the width of the single columns +
2484 //+---------------------------------------------------------------------
2485 vector<unsigned int> clen(columns_);
2487 // first all non (real) multicolumn cells!
2488 for(int j = 0; j < columns_; ++j) {
2490 for(int i = 0; i < rows_; ++i) {
2491 int cell = GetCellNumber(i, j);
2492 if (IsMultiColumn(cell, true))
2495 GetCellInset(cell)->Ascii(buf, sstr, 0);
2496 if (clen[j] < sstr.str().length())
2497 clen[j] = sstr.str().length();
2500 // then all (real) multicolumn cells!
2501 for(int j = 0; j < columns_; ++j) {
2502 for(int i = 0; i < rows_; ++i) {
2503 int cell = GetCellNumber(i, j);
2504 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2507 GetCellInset(cell)->Ascii(buf, sstr, 0);
2508 int len = int(sstr.str().length());
2509 int const n = cells_in_multicolumn(cell);
2510 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2512 if (len > int(clen[j + n - 1]))
2513 clen[j + n - 1] = len;
2517 for(int i = 0; i < rows_; ++i) {
2518 AsciiTopHLine(os, i, clen);
2519 for(int j = 0; j < columns_; ++j) {
2520 if (IsPartOfMultiColumn(i,j))
2522 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2526 AsciiBottomHLine(os, i, clen);
2532 InsetText * LyXTabular::GetCellInset(int cell) const
2534 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2538 void LyXTabular::Validate(LaTeXFeatures & features) const
2540 if (IsLongTabular())
2541 features.longtable = true;
2543 features.rotating = true;
2544 for(int cell = 0; cell < numberofcells; ++cell) {
2545 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2546 features.array = true;
2547 GetCellInset(cell)->Validate(features);
2552 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2554 LyXParagraph * par = GetCellInset(cell)->par;
2556 for(; par; par = par->next) {
2557 for(int i = 0; i < par->Last(); ++i) {
2558 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)