1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
29 #include "frontends/Alert.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
34 #include "support/lstrings.h"
35 #include "support/lyxmanip.h"
36 #include "support/LAssert.h"
37 #include "insets/insettabular.h"
38 #include "insets/insettext.h"
48 #ifndef CXX_GLOBAL_CSTD
54 int const WIDTH_OF_LINE = 5;
58 /// Define a few methods for the inner structs
60 LyXTabular::cellstruct::cellstruct()
64 multicolumn = LyXTabular::CELL_NORMAL;
65 alignment = LYX_ALIGN_CENTER;
66 valignment = LYX_VALIGN_TOP;
76 LyXTabular::rowstruct::rowstruct()
86 LyXTabular::columnstruct::columnstruct()
90 alignment = LYX_ALIGN_CENTER;
91 valignment = LYX_VALIGN_TOP;
96 LyXTabular::lttype::lttype()
105 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
109 Init(rows_arg, columns_arg);
113 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
117 Init(lt.rows_, lt.columns_, <);
120 #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)
127 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
135 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
138 #warning This while method should look like this: (Lgb)
143 // If this and lt is not of the same size we have a serious bug
144 // So then it is ok to throw an exception, or for now
146 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
148 cell_info = lt.cell_info;
149 row_info = lt.row_info;
150 column_info = lt.column_info;
152 // long tabular stuff
153 SetLongTabular(lt.is_long_tabular);
154 endhead = lt.endhead;
155 endfoot = lt.endfoot;
156 endfirsthead = lt.endfirsthead;
157 endlastfoot = lt.endlastfoot;
167 LyXTabular * LyXTabular::clone(InsetTabular * inset)
169 LyXTabular * result = new LyXTabular(inset, *this);
171 // don't know if this is good but I need to Clone also
172 // the text-insets here, this is for the Undo-facility!
173 for (int i = 0; i < rows_; ++i) {
174 for (int j = 0; j < columns_; ++j) {
175 result->cell_info[i][j].inset = cell_info[i][j].inset;
176 result->cell_info[i][j].inset.setOwner(inset);
184 /* activates all lines and sets all widths to 0 */
185 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
188 columns_ = columns_arg;
189 row_info = row_vector(rows_, rowstruct());
190 column_info = column_vector(columns_, columnstruct());
191 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
199 for (int i = 0; i < rows_; ++i) {
200 for (int j = 0; j < columns_; ++j) {
201 cell_info[i][j].inset.setOwner(owner_);
202 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
203 cell_info[i][j].cellno = cellno++;
205 cell_info[i].back().right_line = true;
207 row_info.back().bottom_line = true;
208 row_info.front().bottom_line = true;
210 for (int i = 0; i < columns_; ++i) {
211 calculate_width_of_column(i);
213 column_info.back().right_line = true;
215 calculate_width_of_tabular();
217 rowofcell = vector<int>();
218 columnofcell = vector<int>();
219 set_row_column_number_info();
220 is_long_tabular = false;
223 endfirsthead.row = 0;
229 void LyXTabular::AppendRow(int cell)
233 int row = row_of_cell(cell);
235 row_vector::iterator rit = row_info.begin() + row;
236 row_info.insert(rit, rowstruct());
237 // now set the values of the row before
238 row_info[row] = row_info[row+1];
241 cell_vvector::iterator cit = cell_info.begin() + row;
242 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
244 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
247 for (int i = 0; i <= row; ++i) {
248 for (int j = 0; j < columns_; ++j) {
249 c_info[i][j] = cell_info[i][j];
252 for (int i = row + 1; i < rows_; ++i) {
253 for (int j = 0; j < columns_; ++j) {
254 c_info[i][j] = cell_info[i-1][j];
259 for (int j = 0; j < columns_; ++j) {
260 cell_info[row][j].inset.clear();
267 void LyXTabular::DeleteRow(int row)
269 if (rows_ == 1) return; // Not allowed to delete last row
271 row_info.erase(row_info.begin() + row); //&row_info[row]);
272 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
278 void LyXTabular::AppendColumn(int cell)
282 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
284 int const column = column_of_cell(cell);
285 column_vector::iterator cit = column_info.begin() + column + 1;
286 column_info.insert(cit, columnstruct());
287 // set the column values of the column before
288 column_info[column+1] = column_info[column];
290 for (int i = 0; i < rows_; ++i) {
291 for (int j = 0; j <= column; ++j) {
292 c_info[i][j] = cell_info[i][j];
294 for (int j = column + 1; j < columns_; ++j) {
295 c_info[i][j] = cell_info[i][j - 1];
297 // care about multicolumns
298 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
299 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
301 if ((column + 1) == columns_ ||
302 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
303 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
308 for (int i = 0; i < rows_; ++i) {
309 //cell_info[i][column].inset.clear();
310 cell_info[i][column + 1].inset.clear();
316 void LyXTabular::DeleteColumn(int column)
318 // Similar to DeleteRow
319 //if (!(columns_ - 1))
321 if (columns_ == 1) return; // Not allowed to delete last column
323 column_info.erase(column_info.begin() + column);
324 for (int i = 0; i < rows_; ++i) {
325 cell_info[i].erase(cell_info[i].begin() + column);
332 void LyXTabular::Reinit()
334 for (int i = 0; i < rows_; ++i) {
335 for (int j = 0; j < columns_; ++j) {
336 cell_info[i][j].width_of_cell = 0;
337 cell_info[i][j].inset.setOwner(owner_);
341 for (int i = 0; i < columns_; ++i) {
342 calculate_width_of_column(i);
344 calculate_width_of_tabular();
346 set_row_column_number_info();
350 void LyXTabular::set_row_column_number_info(bool oldformat)
353 for (int row = 0; row < rows_; ++row) {
354 for (int column = 0; column<columns_; ++column) {
355 if (cell_info[row][column].multicolumn
356 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
358 cell_info[row][column].cellno = numberofcells;
361 ++numberofcells; // because this is one more than as we start from 0
363 rowofcell.resize(numberofcells);
364 columnofcell.resize(numberofcells);
366 for (int row = 0, column = 0, c = 0;
367 c < numberofcells && row < rows_ && column < columns_;) {
369 columnofcell[c] = column;
373 } while (column < columns_ &&
374 cell_info[row][column].multicolumn
375 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
376 if (column == columns_) {
382 for (int row = 0; row < rows_; ++row) {
383 for (int column = 0; column < columns_; ++column) {
384 if (IsPartOfMultiColumn(row,column))
386 // now set the right line of multicolumns right for oldformat read
388 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
390 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
391 cell_info[row][column].right_line =
392 cell_info[row][column+cn-1].right_line;
394 cell_info[row][column].inset.setAutoBreakRows(
395 !GetPWidth(GetCellNumber(row, column)).empty());
401 int LyXTabular::GetNumberOfCells() const
403 return numberofcells;
407 int LyXTabular::NumberOfCellsInRow(int cell) const
409 int const row = row_of_cell(cell);
411 for (int i = 0; i < columns_; ++i) {
412 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
419 /* returns 1 if there is a topline, returns 0 if not */
420 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
422 int const row = row_of_cell(cell);
424 if (!onlycolumn && IsMultiColumn(cell))
425 return cellinfo_of_cell(cell)->top_line;
426 return row_info[row].top_line;
430 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
432 // no bottom line underneath non-existent cells if you please
433 // Isn't that a programming error? Is so this should
434 // be an Assert instead. (Lgb)
435 if (cell >= numberofcells)
438 if (!onlycolumn && IsMultiColumn(cell))
439 return cellinfo_of_cell(cell)->bottom_line;
440 return row_info[row_of_cell(cell)].bottom_line;
444 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
446 if (!onlycolumn && IsMultiColumn(cell)) {
447 #ifdef SPECIAL_COLUM_HANDLING
448 if (cellinfo_of_cell(cell)->align_special.empty())
449 return cellinfo_of_cell(cell)->left_line;
450 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
452 return cellinfo_of_cell(cell)->left_line;
455 #ifdef SPECIAL_COLUM_HANDLING
456 if (column_info[column_of_cell(cell)].align_special.empty())
457 return column_info[column_of_cell(cell)].left_line;
458 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
460 return column_info[column_of_cell(cell)].left_line;
465 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
467 if (!onlycolumn && IsMultiColumn(cell)) {
468 #ifdef SPECIAL_COLUM_HANDLING
469 if (cellinfo_of_cell(cell)->align_special.empty())
470 return cellinfo_of_cell(cell)->right_line;
471 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
473 return cellinfo_of_cell(cell)->right_line;
476 #ifdef SPECIAL_COLUM_HANDLING
477 if (column_info[column_of_cell(cell)].align_special.empty())
478 return column_info[right_column_of_cell(cell)].right_line;
479 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
481 return column_info[right_column_of_cell(cell)].right_line;
486 bool LyXTabular::TopAlreadyDrawed(int cell) const
488 int row = row_of_cell(cell);
489 if ((row > 0) && !GetAdditionalHeight(row)) {
490 int column = column_of_cell(cell);
493 && cell_info[row][column].multicolumn
494 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
496 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
497 return row_info[row].bottom_line;
499 return cell_info[row][column].bottom_line;
505 bool LyXTabular::LeftAlreadyDrawed(int cell) const
507 int column = column_of_cell(cell);
509 int row = row_of_cell(cell);
511 (cell_info[row][column].multicolumn ==
512 LyXTabular::CELL_PART_OF_MULTICOLUMN));
513 if (GetAdditionalWidth(cell_info[row][column].cellno))
515 #ifdef SPECIAL_COLUM_HANDLING
516 return column_info[column].right_line;
518 return RightLine(cell_info[row][column].cellno, true);
525 bool LyXTabular::IsLastRow(int cell) const
527 return (row_of_cell(cell) == rows_ - 1);
531 int LyXTabular::GetAdditionalHeight(int row) const
533 if (!row || row >= rows_)
539 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
540 switch (cell_info[row - 1][column].multicolumn) {
541 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
542 bottom = cell_info[row - 1][column].bottom_line;
544 case LyXTabular::CELL_NORMAL:
545 bottom = row_info[row - 1].bottom_line;
548 for (int column = 0; column < columns_ - 1 && top; ++column) {
549 switch (cell_info[row][column].multicolumn){
550 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
551 top = cell_info[row][column].top_line;
553 case LyXTabular::CELL_NORMAL:
554 top = row_info[row].top_line;
558 return WIDTH_OF_LINE;
563 int LyXTabular::GetAdditionalWidth(int cell) const
565 // internally already set in SetWidthOfCell
566 // used to get it back in text.C
567 int const col = right_column_of_cell(cell);
568 int const row = row_of_cell(cell);
569 if (col < columns_ - 1 && RightLine(cell, true) &&
570 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
572 return WIDTH_OF_LINE;
579 // returns the maximum over all rows
580 int LyXTabular::GetWidthOfColumn(int cell) const
582 int const column1 = column_of_cell(cell);
583 int const column2 = right_column_of_cell(cell);
585 for (int i = column1; i <= column2; ++i) {
586 result += column_info[i].width_of_column;
592 int LyXTabular::GetWidthOfTabular() const
594 return width_of_tabular;
598 /* returns 1 if a complete update is necessary, otherwise 0 */
599 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
601 if (!IsMultiColumn(cell))
604 int const row = row_of_cell(cell);
605 int const column1 = column_of_cell(cell);
606 int const column2 = right_column_of_cell(cell);
608 // first set columns to 0 so we can calculate the right width
609 for (int i = column1; i <= column2; ++i) {
610 cell_info[row][i].width_of_cell = 0;
612 // set the width to MAX_WIDTH until width > 0
613 int width = (new_width + 2 * WIDTH_OF_LINE);
616 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
617 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
618 width -= column_info[i].width_of_column;
621 cell_info[row][i].width_of_cell = width;
627 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
629 int const row = row_of_cell(cell);
630 int const column1 = column_of_cell(cell);
631 int const column2 = right_column_of_cell(cell);
633 // first set columns to 0 so we can calculate the right width
635 for (; i <= column2; ++i)
636 cell_info[row][i].width_of_cell = 0;
637 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
639 if (i < numberofcells)
640 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
641 SetWidthOfMulticolCell(cell, new_width);
645 /* returns 1 if a complete update is necessary, otherwise 0 */
646 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
648 int const row = row_of_cell(cell);
649 int const column1 = column_of_cell(cell);
654 #ifdef SPECIAL_COLUM_HANDLING
655 if (RightLine(cell_info[row][column1].cellno, true) &&
656 (column1 < columns_-1) &&
657 LeftLine(cell_info[row][column1+1].cellno, true))
659 if (column_info[column1].right_line && (column1 < columns_-1) &&
660 column_info[column1+1].left_line) // additional width
664 add_width = WIDTH_OF_LINE;
666 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
669 if (IsMultiColumn(cell, true)) {
670 tmp = SetWidthOfMulticolCell(cell, new_width);
672 width = (new_width + 2*WIDTH_OF_LINE + add_width);
673 cell_info[row][column1].width_of_cell = width;
674 tmp = calculate_width_of_column_NMC(column1);
678 for (; i<columns_; ++i)
679 calculate_width_of_column_NMC(i);
680 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
682 if (i < numberofcells)
683 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
684 for (i = 0; i < columns_; ++i)
685 calculate_width_of_column(i);
686 calculate_width_of_tabular();
693 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
695 if (!IsMultiColumn(cell) || onlycolumn)
696 column_info[column_of_cell(cell)].alignment = align;
698 cellinfo_of_cell(cell)->alignment = align;
703 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
705 if (!IsMultiColumn(cell) || onlycolumn)
706 column_info[column_of_cell(cell)].valignment = align;
708 cellinfo_of_cell(cell)->valignment = align;
713 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
715 bool flag = !width.empty();
716 int const j = column_of_cell(cell);
718 column_info[j].p_width = width;
719 if (flag) // do this only if there is a width
720 SetAlignment(cell, LYX_ALIGN_LEFT);
721 for (int i = 0; i < rows_; ++i) {
722 int c = GetCellNumber(i, j);
723 flag = !GetPWidth(c).empty(); // because of multicolumns!
724 GetCellInset(c)->setAutoBreakRows(flag);
730 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
732 bool const flag = !width.empty();
734 cellinfo_of_cell(cell)->p_width = width;
735 if (IsMultiColumn(cell)) {
736 GetCellInset(cell)->setAutoBreakRows(flag);
743 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
744 LyXTabular::Feature what)
746 if (what == SET_SPECIAL_MULTI)
747 cellinfo_of_cell(cell)->align_special = special;
749 column_info[column_of_cell(cell)].align_special = special;
754 bool LyXTabular::SetAllLines(int cell, bool line)
756 SetTopLine(cell, line);
757 SetBottomLine(cell, line);
758 SetRightLine(cell, line);
759 SetLeftLine(cell, line);
764 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
766 int const row = row_of_cell(cell);
768 if (onlycolumn || !IsMultiColumn(cell))
769 row_info[row].top_line = line;
771 cellinfo_of_cell(cell)->top_line = line;
776 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
778 if (onlycolumn || !IsMultiColumn(cell))
779 row_info[row_of_cell(cell)].bottom_line = line;
781 cellinfo_of_cell(cell)->bottom_line = line;
786 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
788 if (onlycolumn || !IsMultiColumn(cell))
789 column_info[column_of_cell(cell)].left_line = line;
791 cellinfo_of_cell(cell)->left_line = line;
796 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
798 if (onlycolumn || !IsMultiColumn(cell))
799 column_info[right_column_of_cell(cell)].right_line = line;
801 cellinfo_of_cell(cell)->right_line = line;
806 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
808 if (!onlycolumn && IsMultiColumn(cell))
809 return cellinfo_of_cell(cell)->alignment;
811 return column_info[column_of_cell(cell)].alignment;
815 LyXTabular::VAlignment
816 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
818 if (!onlycolumn && IsMultiColumn(cell))
819 return cellinfo_of_cell(cell)->valignment;
821 return column_info[column_of_cell(cell)].valignment;
825 string const LyXTabular::GetPWidth(int cell) const
827 if (IsMultiColumn(cell))
828 return cellinfo_of_cell(cell)->p_width;
829 return column_info[column_of_cell(cell)].p_width;
833 string const LyXTabular::GetColumnPWidth(int cell) const
835 return column_info[column_of_cell(cell)].p_width;
839 string const LyXTabular::GetMColumnPWidth(int cell) const
841 if (IsMultiColumn(cell))
842 return cellinfo_of_cell(cell)->p_width;
847 string const LyXTabular::GetAlignSpecial(int cell, int what) const
849 if (what == SET_SPECIAL_MULTI)
850 return cellinfo_of_cell(cell)->align_special;
851 return column_info[column_of_cell(cell)].align_special;
855 int LyXTabular::GetWidthOfCell(int cell) const
857 int const row = row_of_cell(cell);
858 int const column1 = column_of_cell(cell);
859 int const column2 = right_column_of_cell(cell);
861 for (int i = column1; i <= column2; ++i) {
862 result += cell_info[row][i].width_of_cell;
868 int LyXTabular::GetBeginningOfTextInCell(int cell) const
872 switch (GetAlignment(cell)){
873 case LYX_ALIGN_CENTER:
874 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
876 case LYX_ALIGN_RIGHT:
877 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
878 // + GetAdditionalWidth(cell);
880 default: /* LYX_ALIGN_LEFT: nothing :-) */
890 bool LyXTabular::IsFirstCellInRow(int cell) const
892 return column_of_cell(cell) == 0;
896 int LyXTabular::GetFirstCellInRow(int row) const
900 return cell_info[row][0].cellno;
903 bool LyXTabular::IsLastCellInRow(int cell) const
905 return (right_column_of_cell(cell) == (columns_ - 1));
909 int LyXTabular::GetLastCellInRow(int row) const
913 return cell_info[row][columns_-1].cellno;
917 bool LyXTabular::calculate_width_of_column(int column)
919 int const old_column_width = column_info[column].width_of_column;
922 for (int i = 0; i < rows_; ++i) {
923 maximum = max(cell_info[i][column].width_of_cell, maximum);
925 column_info[column].width_of_column = maximum;
926 return (column_info[column].width_of_column != old_column_width);
931 // calculate the with of the column without regarding REAL MultiColumn
932 // cells. This means MultiColumn-cells spanning more than 1 column.
934 bool LyXTabular::calculate_width_of_column_NMC(int column)
936 int const old_column_width = column_info[column].width_of_column;
938 for (int i = 0; i < rows_; ++i) {
939 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
940 (cell_info[i][column].width_of_cell > max)) {
941 max = cell_info[i][column].width_of_cell;
944 column_info[column].width_of_column = max;
945 return (column_info[column].width_of_column != old_column_width);
949 void LyXTabular::calculate_width_of_tabular()
951 width_of_tabular = 0;
952 for (int i = 0; i < columns_; ++i) {
953 width_of_tabular += column_info[i].width_of_column;
958 int LyXTabular::row_of_cell(int cell) const
960 if (cell >= numberofcells)
964 return rowofcell[cell];
968 int LyXTabular::column_of_cell(int cell) const
970 if (cell >= numberofcells)
974 return columnofcell[cell];
978 int LyXTabular::right_column_of_cell(int cell) const
980 int const row = row_of_cell(cell);
981 int column = column_of_cell(cell);
982 while (column < (columns_ - 1) &&
983 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
989 // Perfect case for a template... (Lgb)
993 string const write_attribute(string const & name, T const & t)
995 string str = " " + name + "=\"" + tostr(t) + "\"";
1000 string const write_attribute(string const & name, bool const & b)
1002 return write_attribute(name, int(b));
1007 string const write_attribute(string const & name, int value)
1009 string str = " " + name + "=\"" + tostr(value) + "\"";
1014 string const write_attribute(string const & name, string const & value)
1016 string str = " " + name + "=\"" + value + "\"";
1021 string const write_attribute(string const & name, bool value)
1023 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1031 string const tostr(LyXAlignment const & num)
1034 case LYX_ALIGN_NONE:
1036 case LYX_ALIGN_BLOCK:
1038 case LYX_ALIGN_LEFT:
1040 case LYX_ALIGN_CENTER:
1042 case LYX_ALIGN_RIGHT:
1044 case LYX_ALIGN_LAYOUT:
1046 case LYX_ALIGN_SPECIAL:
1055 string const tostr(LyXTabular::VAlignment const & num)
1058 case LyXTabular::LYX_VALIGN_TOP:
1060 case LyXTabular::LYX_VALIGN_CENTER:
1062 case LyXTabular::LYX_VALIGN_BOTTOM:
1071 string const tostr(LyXTabular::BoxType const & num)
1074 case LyXTabular::BOX_NONE:
1076 case LyXTabular::BOX_PARBOX:
1078 case LyXTabular::BOX_MINIPAGE:
1085 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1089 << write_attribute("version", 2)
1090 << write_attribute("rows", rows_)
1091 << write_attribute("columns", columns_)
1093 // global longtable options
1095 << write_attribute("rotate", tostr(rotate))
1096 << write_attribute("islongtable", tostr(is_long_tabular))
1097 << write_attribute("endhead", endhead.row)
1098 << write_attribute("endfirsthead", endfirsthead.row)
1099 << write_attribute("endfoot", endfoot.row)
1100 << write_attribute("endlastfoot", endlastfoot.row)
1102 for (int j = 0; j < columns_; ++j) {
1104 << write_attribute("alignment", tostr(column_info[j].alignment))
1105 << write_attribute("valignment", tostr(column_info[j].valignment))
1106 << write_attribute("leftline", tostr(column_info[j].left_line))
1107 << write_attribute("rightline", tostr(column_info[j].right_line))
1108 << write_attribute("width",
1109 VSpace(column_info[j].p_width)
1111 << write_attribute("special", column_info[j].align_special)
1114 for (int i = 0; i < rows_; ++i) {
1116 << write_attribute("topline", tostr(row_info[i].top_line))
1117 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1118 << write_attribute("newpage", tostr(row_info[i].newpage))
1120 for (int j = 0; j < columns_; ++j) {
1122 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1123 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1124 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1125 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1126 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1127 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1128 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1129 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1130 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1131 << write_attribute("width", cell_info[i][j].p_width)
1132 << write_attribute("special", cell_info[i][j].align_special)
1134 os << "\\begin_inset ";
1135 cell_info[i][j].inset.write(buf, os);
1136 os << "\n\\end_inset \n"
1141 os << "</lyxtabular>\n";
1147 // I would have liked a fromstr template a lot better. (Lgb)
1150 bool string2type(string const str, LyXAlignment & num)
1153 num = LYX_ALIGN_NONE;
1154 else if (str == "block")
1155 num = LYX_ALIGN_BLOCK;
1156 else if (str == "left")
1157 num = LYX_ALIGN_LEFT;
1158 else if (str == "center")
1159 num = LYX_ALIGN_CENTER;
1160 else if (str == "right")
1161 num = LYX_ALIGN_RIGHT;
1169 bool string2type(string const str, LyXTabular::VAlignment & num)
1172 num = LyXTabular::LYX_VALIGN_TOP;
1173 else if (str == "center")
1174 num = LyXTabular::LYX_VALIGN_CENTER;
1175 else if (str == "bottom")
1176 num = LyXTabular::LYX_VALIGN_BOTTOM;
1184 bool string2type(string const str, LyXTabular::BoxType & num)
1187 num = LyXTabular::BOX_NONE;
1188 else if (str == "parbox")
1189 num = LyXTabular::BOX_PARBOX;
1190 else if (str == "minipage")
1191 num = LyXTabular::BOX_MINIPAGE;
1199 bool string2type(string const str, bool & num)
1203 else if (str == "false")
1211 bool getTokenValue(string const & str, const char * token, string & ret)
1213 size_t token_length = strlen(token);
1214 string::size_type pos = str.find(token);
1216 if (pos == string::npos || pos + token_length + 1 >= str.length()
1217 || str[pos + token_length] != '=')
1220 pos += token_length + 1;
1222 if ((ch != '"') && (ch != '\'')) { // only read till next space
1226 while((pos < str.length() - 1) && (str[++pos] != ch))
1233 bool getTokenValue(string const & str, const char * token, int & num)
1236 if (!getTokenValue(str, token, tmp))
1238 num = strToInt(tmp);
1243 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1246 if (!getTokenValue(str, token, tmp))
1248 return string2type(tmp, num);
1252 bool getTokenValue(string const & str, const char * token,
1253 LyXTabular::VAlignment & num)
1256 if (!getTokenValue(str, token, tmp))
1258 return string2type(tmp, num);
1262 bool getTokenValue(string const & str, const char * token,
1263 LyXTabular::BoxType & num)
1266 if (!getTokenValue(str, token, tmp))
1268 return string2type(tmp, num);
1272 bool getTokenValue(string const & str, const char * token, bool & flag)
1275 if (!getTokenValue(str, token, tmp))
1277 return string2type(tmp, flag);
1282 void l_getline(istream & is, string & str)
1285 while (str.empty()) {
1287 if (!str.empty() && str[str.length() - 1] == '\r')
1288 str.erase(str.length() - 1);
1295 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1298 istream & is = lex.getStream();
1300 l_getline(is, line);
1301 if (!prefixIs(line, "<lyxtabular ")
1302 && !prefixIs(line, "<LyXTabular ")) {
1303 OldFormatRead(lex, line);
1308 if (!getTokenValue(line, "version", version))
1311 ReadOld(buf, is, lex, line);
1312 else if (version == 2)
1313 ReadNew(buf, is, lex, line);
1317 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1318 LyXLex & lex, string const & l)
1322 if (!getTokenValue(line, "rows", rows_arg))
1325 if (!getTokenValue(line, "columns", columns_arg))
1327 Init(rows_arg, columns_arg);
1328 l_getline(is, line);
1329 if (!prefixIs(line, "<features")) {
1330 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1331 line << ")" << endl;
1334 getTokenValue(line, "rotate", rotate);
1335 getTokenValue(line, "islongtable", is_long_tabular);
1336 getTokenValue(line, "endhead", endhead.row);
1337 getTokenValue(line, "endfirsthead", endfirsthead.row);
1338 getTokenValue(line, "endfoot", endfoot.row);
1339 getTokenValue(line, "endlastfoot", endlastfoot.row);
1340 endhead.row = abs(endhead.row);
1341 endfirsthead.row = abs(endfirsthead.row);
1342 endfoot.row = abs(endfoot.row);
1343 endlastfoot.row = abs(endlastfoot.row);
1344 for (int j = 0; j < columns_; ++j) {
1346 if (!prefixIs(line,"<column")) {
1347 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1348 line << ")" << endl;
1351 getTokenValue(line, "alignment", column_info[j].alignment);
1352 getTokenValue(line, "valignment", column_info[j].valignment);
1353 getTokenValue(line, "leftline", column_info[j].left_line);
1354 getTokenValue(line, "rightline", column_info[j].right_line);
1355 getTokenValue(line, "width", column_info[j].p_width);
1356 getTokenValue(line, "special", column_info[j].align_special);
1359 for (int i = 0; i < rows_; ++i) {
1360 l_getline(is, line);
1361 if (!prefixIs(line, "<row")) {
1362 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1363 line << ")" << endl;
1366 getTokenValue(line, "topline", row_info[i].top_line);
1367 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1368 getTokenValue(line, "newpage", row_info[i].newpage);
1369 for (int j = 0; j < columns_; ++j) {
1370 l_getline(is, line);
1371 if (!prefixIs(line, "<cell")) {
1372 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1373 line << ")" << endl;
1376 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1377 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1378 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1379 getTokenValue(line, "topline", cell_info[i][j].top_line);
1380 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1381 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1382 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1383 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1384 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1385 getTokenValue(line, "width", cell_info[i][j].p_width);
1386 getTokenValue(line, "special", cell_info[i][j].align_special);
1387 l_getline(is, line);
1388 if (prefixIs(line, "\\begin_inset")) {
1389 cell_info[i][j].inset.read(buf, lex);
1390 l_getline(is, line);
1392 if (!prefixIs(line, "</cell>")) {
1393 lyxerr << "Wrong tabular format (expected </cell> got" <<
1394 line << ")" << endl;
1398 l_getline(is, line);
1399 if (!prefixIs(line, "</row>")) {
1400 lyxerr << "Wrong tabular format (expected </row> got" <<
1401 line << ")" << endl;
1405 while (!prefixIs(line, "</lyxtabular>")) {
1406 l_getline(is, line);
1408 set_row_column_number_info();
1412 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1418 int columns_arg = 0;
1419 int is_long_tabular_arg = false;
1420 int rotate_arg = false;
1429 istream & is = lex.getStream();
1432 version = lyx::atoi(s.substr(8, string::npos));
1436 vector<int> cont_row_info;
1439 lyxerr << "Tabular format < 5 is not supported anymore\n"
1440 "Get an older version of LyX (< 1.1.x) for conversion!"
1442 Alert::alert(_("Warning:"),
1443 _("Tabular format < 5 is not supported anymore\n"),
1444 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1446 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1447 >> rotate_arg >> a >> b >> c >> d;
1449 is >> rows_arg >> columns_arg;
1450 Init(rows_arg, columns_arg);
1451 cont_row_info = vector<int>(rows_arg);
1452 SetLongTabular(is_long_tabular_arg);
1453 SetRotateTabular(rotate_arg);
1455 for (i = 0; i < rows_; ++i) {
1457 cont_row_info[i] = false;
1459 for (i = 0; i < columns_; ++i) {
1462 for (i = 0; i < rows_; ++i) {
1463 for (j = 0; j < columns_; ++j) {
1468 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1469 >> rotate_arg >> a >> b >> c >> d;
1470 Init(rows_arg, columns_arg);
1471 cont_row_info = vector<int>(rows_arg);
1472 SetLongTabular(is_long_tabular_arg);
1473 SetRotateTabular(rotate_arg);
1474 endhead.row = a + 1;
1475 endfirsthead.row = b + 1;
1476 endfoot.row = c + 1;
1477 endlastfoot.row = d + 1;
1478 for (i = 0; i < rows_; ++i) {
1479 a = b = c = d = e = f = g = 0;
1480 is >> a >> b >> c >> d;
1481 row_info[i].top_line = a;
1482 row_info[i].bottom_line = b;
1483 cont_row_info[i] = c;
1484 row_info[i].newpage = d;
1486 for (i = 0; i < columns_; ++i) {
1491 char ch; // skip '"'
1494 // ignore is buggy but we will use it later (Lgb)
1495 is.ignore(); // skip '"'
1497 getline(is, s1, '"');
1499 is >> ch; // skip '"'
1501 // ignore is buggy but we will use it later (Lgb)
1502 is.ignore(); // skip '"'
1504 getline(is, s2, '"');
1505 column_info[i].alignment = static_cast<LyXAlignment>(a);
1506 column_info[i].left_line = b;
1507 column_info[i].right_line = c;
1508 column_info[i].p_width = s1;
1509 column_info[i].align_special = s2;
1511 for (i = 0; i < rows_; ++i) {
1512 for (j = 0; j < columns_; ++j) {
1515 is >> a >> b >> c >> d >> e >> f >> g;
1518 is >> ch; // skip '"'
1520 // ignore is buggy but we will use it later (Lgb)
1521 is.ignore(); // skip '"'
1523 getline(is, s1, '"');
1525 is >> ch; // skip '"'
1527 // ignore is buggy but we will use it later (Lgb)
1528 is.ignore(); // skip '"'
1530 getline(is, s2, '"');
1531 cell_info[i][j].multicolumn = static_cast<char>(a);
1532 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1533 cell_info[i][j].top_line = static_cast<char>(c);
1534 cell_info[i][j].bottom_line = static_cast<char>(d);
1535 cell_info[i][j].left_line = column_info[j].left_line;
1536 cell_info[i][j].right_line = column_info[j].right_line;
1537 cell_info[i][j].rotate = static_cast<bool>(f);
1538 cell_info[i][j].usebox = static_cast<BoxType>(g);
1539 cell_info[i][j].align_special = s1;
1540 cell_info[i][j].p_width = s2;
1544 set_row_column_number_info(true);
1546 Paragraph * par = new Paragraph;
1547 Paragraph * return_par = 0;
1551 Paragraph::depth_type depth = 0;
1552 LyXFont font(LyXFont::ALL_INHERIT);
1553 font.setLanguage(owner_->bufferOwner()->getLanguage());
1555 while (lex.isOK()) {
1557 string const token = lex.getString();
1560 if (token == "\\layout"
1561 || token == "\\end_float"
1562 || token == "\\end_deeper") {
1563 lex.pushToken(token);
1564 #ifndef NO_COMPABILITY
1565 // Here we need to insert the inset_ert_contents into the last
1566 // cell of the tabular.
1567 owner_->bufferOwner()->insertErtContents(par, pos, font);
1571 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1576 lex.pushToken(token);
1580 lex.printError("New Paragraph allocated! This should not happen!");
1581 lex.pushToken(token);
1587 // now we have the par we should fill the insets with this!
1589 InsetText * inset = GetCellInset(cell);
1592 for (int i = 0; i < par->size(); ++i) {
1593 if (par->isNewline(i)) {
1595 if (cell > numberofcells) {
1596 lyxerr << "Some error in reading old table format occured!" <<
1597 endl << "Terminating when reading cell[" << cell << "]!" <<
1602 row = row_of_cell(cell);
1603 if (cont_row_info[row]) {
1605 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1606 while(!IsFirstCellInRow(--cell));
1608 inset = GetCellInset(cell);
1611 inset = GetCellInset(cell);
1612 row = row_of_cell(cell);
1613 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1615 // insert a space instead
1617 par->insertChar(i, ' ');
1620 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1621 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1628 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1630 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1631 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1635 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1637 int const row = row_of_cell(cell);
1638 int const column = column_of_cell(cell);
1639 return &cell_info[row][column];
1643 void LyXTabular::SetMultiColumn(int cell, int number)
1645 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1646 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1647 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1648 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1649 for (number--; number > 0; --number) {
1650 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1652 set_row_column_number_info();
1656 int LyXTabular::cells_in_multicolumn(int cell) const
1658 int const row = row_of_cell(cell);
1659 int column = column_of_cell(cell);
1662 while ((column < columns_) &&
1663 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1672 int LyXTabular::UnsetMultiColumn(int cell)
1674 int const row = row_of_cell(cell);
1675 int column = column_of_cell(cell);
1679 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1680 cell_info[row][column].multicolumn = CELL_NORMAL;
1682 while ((column < columns_) &&
1683 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1685 cell_info[row][column].multicolumn = CELL_NORMAL;
1690 set_row_column_number_info();
1695 void LyXTabular::SetLongTabular(bool what)
1697 is_long_tabular = what;
1701 bool LyXTabular::IsLongTabular() const
1703 return is_long_tabular;
1707 void LyXTabular::SetRotateTabular(bool flag)
1713 bool LyXTabular::GetRotateTabular() const
1719 void LyXTabular::SetRotateCell(int cell, bool flag)
1721 cellinfo_of_cell(cell)->rotate = flag;
1725 bool LyXTabular::GetRotateCell(int cell) const
1727 return cellinfo_of_cell(cell)->rotate;
1731 bool LyXTabular::NeedRotating() const
1735 for (int i = 0; i < rows_; ++i) {
1736 for (int j = 0; j < columns_; ++j) {
1737 if (cell_info[i][j].rotate)
1745 bool LyXTabular::IsLastCell(int cell) const
1747 if ((cell + 1) < numberofcells)
1753 int LyXTabular::GetCellAbove(int cell) const
1755 if (row_of_cell(cell) > 0)
1756 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1761 int LyXTabular::GetCellBelow(int cell) const
1763 if (row_of_cell(cell) + 1 < rows_)
1764 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1769 int LyXTabular::GetLastCellAbove(int cell) const
1771 if (row_of_cell(cell) <= 0)
1773 if (!IsMultiColumn(cell))
1774 return GetCellAbove(cell);
1775 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1779 int LyXTabular::GetLastCellBelow(int cell) const
1781 if (row_of_cell(cell) + 1 >= rows_)
1783 if (!IsMultiColumn(cell))
1784 return GetCellBelow(cell);
1785 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1789 int LyXTabular::GetCellNumber(int row, int column) const
1792 if (column >= columns_)
1793 column = columns_ - 1;
1794 else if (column < 0)
1801 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1803 return cell_info[row][column].cellno;
1807 void LyXTabular::SetUsebox(int cell, BoxType type)
1809 cellinfo_of_cell(cell)->usebox = type;
1813 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1815 if (column_info[column_of_cell(cell)].p_width.empty() &&
1816 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1818 if (cellinfo_of_cell(cell)->usebox > 1)
1819 return cellinfo_of_cell(cell)->usebox;
1820 return UseParbox(cell);
1823 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1825 if (!ltt.row || (ltt.row > rows_))
1827 return (row == (ltt.row - 1));
1831 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1841 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1844 return checkLTType(row, hd);
1848 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1851 return checkLTType(row, hd);
1855 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1865 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1868 return checkLTType(row, fd);
1872 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1875 return checkLTType(row, fd);
1879 void LyXTabular::SetLTNewPage(int cell, bool what)
1881 row_info[row_of_cell(cell)].newpage = what;
1885 bool LyXTabular::GetLTNewPage(int cell) const
1887 return row_info[row_of_cell(cell)].newpage;
1891 bool LyXTabular::SetAscentOfRow(int row, int height)
1893 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1895 row_info[row].ascent_of_row = height;
1900 bool LyXTabular::SetDescentOfRow(int row, int height)
1902 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1904 row_info[row].descent_of_row = height;
1909 int LyXTabular::GetAscentOfRow(int row) const
1913 return row_info[row].ascent_of_row;
1917 int LyXTabular::GetDescentOfRow(int row) const
1921 return row_info[row].descent_of_row;
1925 int LyXTabular::GetHeightOfTabular() const
1929 for (int row = 0; row < rows_; ++row)
1930 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1931 GetAdditionalHeight(row);
1936 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1938 if ((row >= rows_) || (column >= columns_))
1940 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1944 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1946 if ((row < 0) || (row >= rows_))
1949 int const fcell = GetFirstCellInRow(row);
1950 int const n = NumberOfCellsInRow(fcell) + fcell;
1953 for (int i = fcell; i < n; ++i) {
1957 if (tmp == (n - fcell)){
1960 for (int i = fcell; i < n; ++i) {
1963 << column_of_cell(i) + 1
1965 << right_column_of_cell(i) + 1
1977 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1979 if ((row < 0) || (row >= rows_))
1982 int const fcell = GetFirstCellInRow(row);
1983 int const n = NumberOfCellsInRow(fcell) + fcell;
1986 for (int i = fcell; i < n; ++i) {
1990 if (tmp == (n-fcell)){
1993 for (int i = fcell; i < n; ++i) {
1994 if (BottomLine(i)) {
1996 << column_of_cell(i) + 1
1998 << right_column_of_cell(i) + 1
2010 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2014 if (GetRotateCell(cell)) {
2015 os << "\\begin{sideways}\n";
2018 if (IsMultiColumn(cell)) {
2019 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2020 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2021 os << cellinfo_of_cell(cell)->align_special << "}{";
2023 if (LeftLine(cell) &&
2024 (IsFirstCellInRow(cell) ||
2025 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2026 !RightLine(cell-1, true))))
2030 if (!GetPWidth(cell).empty()) {
2031 switch (GetVAlignment(cell)) {
2032 case LYX_VALIGN_TOP:
2035 case LYX_VALIGN_CENTER:
2038 case LYX_VALIGN_BOTTOM:
2042 os << "{" << GetPWidth(cell) << '}';
2044 switch (GetAlignment(cell)) {
2045 case LYX_ALIGN_LEFT:
2048 case LYX_ALIGN_RIGHT:
2056 if (RightLine(cell))
2058 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2064 if (GetUsebox(cell) == BOX_PARBOX) {
2066 switch (GetVAlignment(cell)) {
2067 case LYX_VALIGN_TOP:
2070 case LYX_VALIGN_CENTER:
2073 case LYX_VALIGN_BOTTOM:
2077 os << "]{" << GetPWidth(cell) << "}{";
2078 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2079 os << "\\begin{minipage}[";
2080 switch (GetVAlignment(cell)) {
2081 case LYX_VALIGN_TOP:
2084 case LYX_VALIGN_CENTER:
2087 case LYX_VALIGN_BOTTOM:
2091 os << "]{" << GetPWidth(cell) << "}\n";
2098 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2103 if (GetUsebox(cell) == BOX_PARBOX)
2105 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2106 os << "%\n\\end{minipage}";
2109 if (IsMultiColumn(cell)){
2112 if (GetRotateCell(cell)) {
2113 os << "%\n\\end{sideways}";
2120 int LyXTabular::Latex(Buffer const * buf,
2121 ostream & os, bool fragile, bool fp) const
2126 //+---------------------------------------------------------------------
2127 //+ first the opening preamble +
2128 //+---------------------------------------------------------------------
2131 os << "\\begin{sideways}\n";
2134 if (is_long_tabular)
2135 os << "\\begin{longtable}{";
2137 os << "\\begin{tabular}{";
2138 for (int i = 0; i < columns_; ++i) {
2139 if (!column_info[i].align_special.empty()) {
2140 os << column_info[i].align_special;
2142 if (column_info[i].left_line)
2144 if (!column_info[i].p_width.empty()) {
2145 switch (column_info[i].valignment) {
2146 case LYX_VALIGN_TOP:
2149 case LYX_VALIGN_CENTER:
2152 case LYX_VALIGN_BOTTOM:
2157 << column_info[i].p_width
2160 switch (column_info[i].alignment) {
2161 case LYX_ALIGN_LEFT:
2164 case LYX_ALIGN_RIGHT:
2172 if (column_info[i].right_line)
2179 //+---------------------------------------------------------------------
2180 //+ the single row and columns (cells) +
2181 //+---------------------------------------------------------------------
2183 for (int i = 0; i < rows_; ++i) {
2184 ret += TeXTopHLine(os, i);
2185 #warning Implement top double lines for LT Header/Footers
2188 ret += TeXBottomHLine(os, i-1);
2189 ret += TeXTopHLine(os, i);
2192 for (int j = 0; j < columns_; ++j) {
2193 if (IsPartOfMultiColumn(i,j))
2195 ret += TeXCellPreamble(os, cell);
2196 InsetText * inset = GetCellInset(cell);
2198 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2199 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2203 ret += inset->latex(buf, os, fragile, fp);
2207 ret += TeXCellPostamble(os, cell);
2208 if (!IsLastCellInRow(cell)) { // not last cell in row
2216 ret += TeXBottomHLine(os, i);
2217 if (IsLongTabular()) {
2218 if (i == (endhead.row - 1)) {
2219 if (endhead.bottomDL)
2220 ret += TeXBottomHLine(os, i);
2221 os << "\\endhead\n";
2224 if (i == (endfirsthead.row - 1)) {
2225 if (endfirsthead.bottomDL)
2226 ret += TeXBottomHLine(os, i);
2227 os << "\\endfirsthead\n";
2230 if (i == (endfoot.row - 1)) {
2231 if (endfoot.bottomDL)
2232 ret += TeXBottomHLine(os, i);
2233 os << "\\endfoot\n";
2236 if (i == (endlastfoot.row - 1)) {
2237 if (endlastfoot.bottomDL)
2238 ret += TeXBottomHLine(os, i);
2239 os << "\\endlastfoot\n";
2242 if (row_info[i].newpage) {
2243 os << "\\newpage\n";
2249 //+---------------------------------------------------------------------
2250 //+ the closing of the tabular +
2251 //+---------------------------------------------------------------------
2253 if (is_long_tabular)
2254 os << "\\end{longtable}";
2256 os << "\\end{tabular}";
2258 os << "\n\\end{sideways}";
2266 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2269 int cell = GetFirstCellInRow(row);
2272 for (int j = 0; j < columns_; ++j) {
2273 if (IsPartOfMultiColumn(row, j))
2276 os << "<entry align=\"";
2277 switch (GetAlignment(cell)) {
2278 case LYX_ALIGN_LEFT:
2281 case LYX_ALIGN_RIGHT:
2289 os << "\" valign=\"";
2290 switch (GetVAlignment(cell)) {
2291 case LYX_VALIGN_TOP:
2294 case LYX_VALIGN_BOTTOM:
2297 case LYX_VALIGN_CENTER:
2302 if (IsMultiColumn(cell)) {
2303 os << " namest=\"col" << j << "\" ";
2304 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2308 ret += GetCellInset(cell)->docbook(buf, os);
2317 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2321 //+---------------------------------------------------------------------
2322 //+ first the opening preamble +
2323 //+---------------------------------------------------------------------
2325 os << "<tgroup cols=\"" << columns_
2326 << "\" colsep=\"1\" rowsep=\"1\">\n";
2328 for (int i = 0; i < columns_; ++i) {
2329 os << "<colspec colname=\"col" << i << "\" align=\"";
2330 switch (column_info[i].alignment) {
2331 case LYX_ALIGN_LEFT:
2334 case LYX_ALIGN_RIGHT:
2345 //+---------------------------------------------------------------------
2346 //+ Long Tabular case +
2347 //+---------------------------------------------------------------------
2349 if ( IsLongTabular() ) {
2351 if( endhead.row || endfirsthead.row ) {
2353 if( endfirsthead.row ) {
2354 docbookRow( buf, os, endfirsthead.row - 1);
2356 if( endhead.row && endhead.row != endfirsthead.row) {
2357 docbookRow(buf, os, endhead.row - 1);
2363 if( endfoot.row || endlastfoot.row ) {
2366 docbookRow( buf, os, endfoot.row - 1);
2368 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2369 docbookRow( buf, os, endlastfoot.row - 1);
2374 //+---------------------------------------------------------------------
2375 //+ the single row and columns (cells) +
2376 //+---------------------------------------------------------------------
2379 for (int i = 0; i < rows_; ++i) {
2380 if(!IsLongTabular() || (
2381 i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2382 i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2383 docbookRow( buf, os, i);
2387 //+---------------------------------------------------------------------
2388 //+ the closing of the tabular +
2389 //+---------------------------------------------------------------------
2401 void print_n_chars(ostream & os, unsigned char ch, int n)
2403 os << string(n, ch);
2409 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2410 vector<unsigned int> const & clen) const
2412 int const fcell = GetFirstCellInRow(row);
2413 int const n = NumberOfCellsInRow(fcell) + fcell;
2416 for (int i = fcell; i < n; ++i) {
2426 for (int i = fcell; i < n; ++i) {
2437 int column = column_of_cell(i);
2438 int len = clen[column];
2439 while(IsPartOfMultiColumn(row, ++column))
2440 len += clen[column] + 4;
2441 print_n_chars(os, ch, len);
2456 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2457 vector<unsigned int> const & clen) const
2459 int const fcell = GetFirstCellInRow(row);
2460 int const n = NumberOfCellsInRow(fcell) + fcell;
2463 for (int i = fcell; i < n; ++i) {
2464 if (BottomLine(i)) {
2473 for (int i = fcell; i < n; ++i) {
2474 if (BottomLine(i)) {
2484 int column = column_of_cell(i);
2485 int len = clen[column];
2486 while(IsPartOfMultiColumn(row, ++column))
2487 len += clen[column] + 4;
2488 print_n_chars(os, ch, len);
2489 if (BottomLine(i)) {
2503 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2504 int cell, int row, int column,
2505 vector<unsigned int> const & clen) const
2508 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2515 unsigned int len1 = sstr.str().length();
2516 unsigned int len2 = clen[column];
2517 while(IsPartOfMultiColumn(row, ++column))
2518 len2 += clen[column] + 4;
2521 switch (GetAlignment(cell)) {
2523 case LYX_ALIGN_LEFT:
2526 case LYX_ALIGN_RIGHT:
2530 case LYX_ALIGN_CENTER:
2536 for (unsigned int i = 0; i < len1; ++i)
2539 for (unsigned int i = 0; i < len2; ++i)
2541 if (RightLine(cell))
2550 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2554 //+---------------------------------------------------------------------
2555 //+ first calculate the width of the single columns +
2556 //+---------------------------------------------------------------------
2557 vector<unsigned int> clen(columns_);
2559 // first all non (real) multicolumn cells!
2560 for (int j = 0; j < columns_; ++j) {
2562 for (int i = 0; i < rows_; ++i) {
2563 int cell = GetCellNumber(i, j);
2564 if (IsMultiColumn(cell, true))
2567 GetCellInset(cell)->ascii(buf, sstr, 0);
2568 if (clen[j] < sstr.str().length())
2569 clen[j] = sstr.str().length();
2572 // then all (real) multicolumn cells!
2573 for (int j = 0; j < columns_; ++j) {
2574 for (int i = 0; i < rows_; ++i) {
2575 int cell = GetCellNumber(i, j);
2576 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2579 GetCellInset(cell)->ascii(buf, sstr, 0);
2580 int len = int(sstr.str().length());
2581 int const n = cells_in_multicolumn(cell);
2582 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2584 if (len > int(clen[j + n - 1]))
2585 clen[j + n - 1] = len;
2589 for (int i = 0; i < rows_; ++i) {
2590 AsciiTopHLine(os, i, clen);
2591 for (int j = 0; j < columns_; ++j) {
2592 if (IsPartOfMultiColumn(i,j))
2594 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2598 AsciiBottomHLine(os, i, clen);
2604 InsetText * LyXTabular::GetCellInset(int cell) const
2607 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2611 InsetText * LyXTabular::GetCellInset(int row, int column) const
2613 cur_cell = GetCellNumber(row, column);
2614 return & cell_info[row][column].inset;
2618 void LyXTabular::Validate(LaTeXFeatures & features) const
2620 if (IsLongTabular())
2621 features.require("longtable");
2623 features.require("rotating");
2624 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2625 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2626 features.require("array");
2627 GetCellInset(cell)->validate(features);
2632 std::vector<string> const LyXTabular::getLabelList() const
2634 std::vector<string> label_list;
2635 for (int i = 0; i < rows_; ++i)
2636 for (int j = 0; j < columns_; ++j) {
2637 std::vector<string> const l =
2638 GetCellInset(i, j)->getLabelList();
2639 label_list.insert(label_list.end(),
2640 l.begin(), l.end());
2646 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2648 Paragraph * par = GetCellInset(cell)->paragraph();
2650 for (; par; par = par->next()) {
2651 for (int i = 0; i < par->size(); ++i) {
2652 if (par->getChar(i) == Paragraph::META_NEWLINE)