1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
26 #include "lyx_gui_misc.h"
28 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
44 #ifndef CXX_GLOBAL_CSTD
50 int const WIDTH_OF_LINE = 5;
54 /// Define a few methods for the inner structs
56 LyXTabular::cellstruct::cellstruct()
60 multicolumn = LyXTabular::CELL_NORMAL;
61 alignment = LYX_ALIGN_CENTER;
62 valignment = LYX_VALIGN_TOP;
72 LyXTabular::rowstruct::rowstruct()
82 LyXTabular::columnstruct::columnstruct()
86 alignment = LYX_ALIGN_CENTER;
87 valignment = LYX_VALIGN_TOP;
93 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
96 Init(rows_arg, columns_arg);
100 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
103 Init(lt.rows_, lt.columns_, <);
106 #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)
113 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
120 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
123 #warning This while method should look like this: (Lgb)
128 // If this and lt is not of the same size we have a serious bug
129 // So then it is ok to throw an exception, or for now
131 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
133 cell_info = lt.cell_info;
134 row_info = lt.row_info;
135 column_info = lt.column_info;
137 // long tabular stuff
138 SetLongTabular(lt.is_long_tabular);
139 endhead = lt.endhead;
140 endfoot = lt.endfoot;
141 endfirsthead = lt.endfirsthead;
142 endlastfoot = lt.endlastfoot;
152 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
154 LyXTabular * result = new LyXTabular(inset, *this);
156 // don't know if this is good but I need to Clone also
157 // the text-insets here, this is for the Undo-facility!
158 for (int i = 0; i < rows_; ++i) {
159 for (int j = 0; j < columns_; ++j) {
160 result->cell_info[i][j].inset = cell_info[i][j].inset;
161 result->cell_info[i][j].inset.setOwner(inset);
169 /* activates all lines and sets all widths to 0 */
170 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
173 columns_ = columns_arg;
174 row_info = row_vector(rows_, rowstruct());
175 column_info = column_vector(columns_, columnstruct());
176 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
184 for (int i = 0; i < rows_; ++i) {
185 for (int j = 0; j < columns_; ++j) {
186 cell_info[i][j].inset.setOwner(owner_);
187 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
188 cell_info[i][j].cellno = cellno++;
190 cell_info[i].back().right_line = true;
192 row_info.back().bottom_line = true;
193 row_info.front().bottom_line = true;
195 for (int i = 0; i < columns_; ++i) {
196 calculate_width_of_column(i);
198 column_info.back().right_line = true;
200 calculate_width_of_tabular();
202 rowofcell = vector<int>();
203 columnofcell = vector<int>();
204 set_row_column_number_info();
205 is_long_tabular = false;
214 void LyXTabular::AppendRow(int cell)
218 int row = row_of_cell(cell);
220 row_vector::iterator rit = row_info.begin() + row;
221 row_info.insert(rit, rowstruct());
222 // now set the values of the row before
223 row_info[row] = row_info[row+1];
226 cell_vvector::iterator cit = cell_info.begin() + row;
227 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
229 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
232 for (int i = 0; i <= row; ++i) {
233 for (int j = 0; j < columns_; ++j) {
234 c_info[i][j] = cell_info[i][j];
237 for (int i = row + 1; i < rows_; ++i) {
238 for (int j = 0; j < columns_; ++j) {
239 c_info[i][j] = cell_info[i-1][j];
244 for (int j = 0; j < columns_; ++j) {
245 cell_info[row][j].inset.clear();
252 void LyXTabular::DeleteRow(int row)
254 // Why make it so hard? (Lgb)
257 if (rows_ == 1) return; // Not allowed to delete last row
259 row_info.erase(row_info.begin() + row); //&row_info[row]);
260 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
266 void LyXTabular::AppendColumn(int cell)
270 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
272 int const column = column_of_cell(cell);
273 column_vector::iterator cit = column_info.begin() + column + 1;
274 column_info.insert(cit, columnstruct());
275 // set the column values of the column before
276 column_info[column+1] = column_info[column];
278 for (int i = 0; i < rows_; ++i) {
279 for (int j = 0; j <= column; ++j) {
280 c_info[i][j] = cell_info[i][j];
282 for (int j = column + 1; j < columns_; ++j) {
283 c_info[i][j] = cell_info[i][j - 1];
285 // care about multicolumns
286 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
287 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
289 if ((column + 1) == columns_ ||
290 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
291 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
296 for (int i = 0; i < rows_; ++i) {
297 //cell_info[i][column].inset.clear();
298 cell_info[i][column + 1].inset.clear();
304 void LyXTabular::DeleteColumn(int column)
306 // Similar to DeleteRow
307 //if (!(columns_ - 1))
309 if (columns_ == 1) return; // Not allowed to delete last column
311 column_info.erase(column_info.begin() + column);
312 for (int i = 0; i < rows_; ++i) {
313 cell_info[i].erase(cell_info[i].begin() + column);
320 void LyXTabular::Reinit()
322 for (int i = 0; i < rows_; ++i) {
323 for (int j = 0; j < columns_; ++j) {
324 cell_info[i][j].width_of_cell = 0;
325 cell_info[i][j].inset.setOwner(owner_);
329 for (int i = 0; i < columns_; ++i) {
330 calculate_width_of_column(i);
332 calculate_width_of_tabular();
334 set_row_column_number_info();
338 void LyXTabular::set_row_column_number_info(bool oldformat)
341 for (int row = 0; row < rows_; ++row) {
342 for (int column = 0; column<columns_; ++column) {
343 if (cell_info[row][column].multicolumn
344 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
346 cell_info[row][column].cellno = numberofcells;
349 ++numberofcells; // because this is one more than as we start from 0
351 rowofcell.resize(numberofcells);
352 columnofcell.resize(numberofcells);
354 for (int row = 0, column = 0, c = 0;
355 c < numberofcells && row < rows_ && column < columns_;) {
357 columnofcell[c] = column;
361 } while (column < columns_ &&
362 cell_info[row][column].multicolumn
363 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
364 if (column == columns_) {
370 for (int row = 0; row < rows_; ++row) {
371 for (int column = 0; column < columns_; ++column) {
372 if (IsPartOfMultiColumn(row,column))
374 // now set the right line of multicolumns right for oldformat read
376 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
378 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
379 cell_info[row][column].right_line =
380 cell_info[row][column+cn-1].right_line;
382 cell_info[row][column].inset.SetAutoBreakRows(
383 !GetPWidth(GetCellNumber(row, column)).empty());
389 int LyXTabular::GetNumberOfCells() const
391 return numberofcells;
395 int LyXTabular::NumberOfCellsInRow(int cell) const
397 int const row = row_of_cell(cell);
399 for (int i = 0; i < columns_; ++i) {
400 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
407 /* returns 1 if there is a topline, returns 0 if not */
408 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
410 int const row = row_of_cell(cell);
412 if (!onlycolumn && IsMultiColumn(cell))
413 return cellinfo_of_cell(cell)->top_line;
414 return row_info[row].top_line;
418 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
420 // no bottom line underneath non-existent cells if you please
421 // Isn't that a programming error? Is so this should
422 // be an Assert instead. (Lgb)
423 if (cell >= numberofcells)
426 if (!onlycolumn && IsMultiColumn(cell))
427 return cellinfo_of_cell(cell)->bottom_line;
428 return row_info[row_of_cell(cell)].bottom_line;
432 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
434 if (!onlycolumn && IsMultiColumn(cell))
435 return cellinfo_of_cell(cell)->left_line;
436 return column_info[column_of_cell(cell)].left_line;
440 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
442 if (!onlycolumn && IsMultiColumn(cell))
443 return cellinfo_of_cell(cell)->right_line;
444 return column_info[right_column_of_cell(cell)].right_line;
448 bool LyXTabular::TopAlreadyDrawed(int cell) const
450 int row = row_of_cell(cell);
451 if ((row > 0) && !GetAdditionalHeight(row)) {
452 int column = column_of_cell(cell);
455 && cell_info[row][column].multicolumn
456 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
458 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
459 return row_info[row].bottom_line;
461 return cell_info[row][column].bottom_line;
467 bool LyXTabular::LeftAlreadyDrawed(int cell) const
469 int column = column_of_cell(cell);
471 int row = row_of_cell(cell);
473 (cell_info[row][column].multicolumn ==
474 LyXTabular::CELL_PART_OF_MULTICOLUMN));
475 if (GetAdditionalWidth(cell_info[row][column].cellno))
477 return column_info[column].right_line;
483 bool LyXTabular::IsLastRow(int cell) const
485 return (row_of_cell(cell) == rows_ - 1);
489 int LyXTabular::GetAdditionalHeight(int row) const
491 if (!row || row >= rows_)
497 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
498 switch (cell_info[row - 1][column].multicolumn) {
499 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
500 bottom = cell_info[row - 1][column].bottom_line;
502 case LyXTabular::CELL_NORMAL:
503 bottom = row_info[row - 1].bottom_line;
506 for (int column = 0; column < columns_ - 1 && top; ++column) {
507 switch (cell_info[row][column].multicolumn){
508 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
509 top = cell_info[row][column].top_line;
511 case LyXTabular::CELL_NORMAL:
512 top = row_info[row].top_line;
516 return WIDTH_OF_LINE;
521 int LyXTabular::GetAdditionalWidth(int cell) const
523 // internally already set in SetWidthOfCell
524 // used to get it back in text.C
525 int const col = right_column_of_cell(cell);
526 if (col < columns_ - 1 && column_info[col].right_line &&
527 column_info[col+1].left_line)
528 return WIDTH_OF_LINE;
534 // returns the maximum over all rows
535 int LyXTabular::GetWidthOfColumn(int cell) const
537 int const column1 = column_of_cell(cell);
538 int const column2 = right_column_of_cell(cell);
540 for (int i = column1; i <= column2; ++i) {
541 result += column_info[i].width_of_column;
547 int LyXTabular::GetWidthOfTabular() const
549 return width_of_tabular;
553 /* returns 1 if a complete update is necessary, otherwise 0 */
554 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
556 if (!IsMultiColumn(cell))
559 int const row = row_of_cell(cell);
560 int const column1 = column_of_cell(cell);
561 int const column2 = right_column_of_cell(cell);
563 // first set columns to 0 so we can calculate the right width
564 for (int i = column1; i <= column2; ++i) {
565 cell_info[row][i].width_of_cell = 0;
567 // set the width to MAX_WIDTH until width > 0
568 int width = (new_width + 2 * WIDTH_OF_LINE);
571 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
572 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
573 width -= column_info[i].width_of_column;
576 cell_info[row][i].width_of_cell = width;
582 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
584 int const row = row_of_cell(cell);
585 int const column1 = column_of_cell(cell);
586 int const column2 = right_column_of_cell(cell);
588 // first set columns to 0 so we can calculate the right width
590 for (; i <= column2; ++i)
591 cell_info[row][i].width_of_cell = 0;
592 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
594 if (i < numberofcells)
595 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
596 SetWidthOfMulticolCell(cell, new_width);
600 /* returns 1 if a complete update is necessary, otherwise 0 */
601 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
603 int const row = row_of_cell(cell);
604 int const column1 = column_of_cell(cell);
608 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
610 if (IsMultiColumn(cell, true)) {
611 tmp = SetWidthOfMulticolCell(cell, new_width);
613 width = (new_width + 2*WIDTH_OF_LINE);
614 cell_info[row][column1].width_of_cell = width;
615 if (column_info[column1].right_line && (column1 < columns_-1) &&
616 column_info[column1+1].left_line) // additional width
617 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
618 tmp = calculate_width_of_column_NMC(column1);
622 for (; i<columns_; ++i)
623 calculate_width_of_column_NMC(i);
624 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
626 if (i < numberofcells)
627 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
628 for (i = 0; i < columns_; ++i)
629 calculate_width_of_column(i);
630 calculate_width_of_tabular();
637 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
639 if (!IsMultiColumn(cell) || onlycolumn)
640 column_info[column_of_cell(cell)].alignment = align;
642 cellinfo_of_cell(cell)->alignment = align;
647 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
649 if (!IsMultiColumn(cell) || onlycolumn)
650 column_info[column_of_cell(cell)].valignment = align;
652 cellinfo_of_cell(cell)->valignment = align;
657 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
659 bool flag = !width.empty();
660 int const j = column_of_cell(cell);
662 column_info[j].p_width = width;
663 if (flag) // do this only if there is a width
664 SetAlignment(cell, LYX_ALIGN_LEFT);
665 for (int i = 0; i < rows_; ++i) {
666 int c = GetCellNumber(i, j);
667 flag = !GetPWidth(c).empty(); // because of multicolumns!
668 GetCellInset(c)->SetAutoBreakRows(flag);
674 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
676 bool const flag = !width.empty();
678 cellinfo_of_cell(cell)->p_width = width;
679 if (IsMultiColumn(cell)) {
680 GetCellInset(cell)->SetAutoBreakRows(flag);
687 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
688 LyXTabular::Feature what)
690 if (what == SET_SPECIAL_MULTI)
691 cellinfo_of_cell(cell)->align_special = special;
693 column_info[column_of_cell(cell)].align_special = special;
698 bool LyXTabular::SetAllLines(int cell, bool line)
700 SetTopLine(cell, line);
701 SetBottomLine(cell, line);
702 SetRightLine(cell, line);
703 SetLeftLine(cell, line);
708 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
710 int const row = row_of_cell(cell);
712 if (onlycolumn || !IsMultiColumn(cell))
713 row_info[row].top_line = line;
715 cellinfo_of_cell(cell)->top_line = line;
720 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
722 if (onlycolumn || !IsMultiColumn(cell))
723 row_info[row_of_cell(cell)].bottom_line = line;
725 cellinfo_of_cell(cell)->bottom_line = line;
730 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
732 if (onlycolumn || !IsMultiColumn(cell))
733 column_info[column_of_cell(cell)].left_line = line;
735 cellinfo_of_cell(cell)->left_line = line;
740 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
742 if (onlycolumn || !IsMultiColumn(cell))
743 column_info[right_column_of_cell(cell)].right_line = line;
745 cellinfo_of_cell(cell)->right_line = line;
750 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
752 if (!onlycolumn && IsMultiColumn(cell))
753 return cellinfo_of_cell(cell)->alignment;
755 return column_info[column_of_cell(cell)].alignment;
759 LyXTabular::VAlignment
760 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
762 if (!onlycolumn && IsMultiColumn(cell))
763 return cellinfo_of_cell(cell)->valignment;
765 return column_info[column_of_cell(cell)].valignment;
769 string const LyXTabular::GetPWidth(int cell) const
771 if (IsMultiColumn(cell))
772 return cellinfo_of_cell(cell)->p_width;
773 return column_info[column_of_cell(cell)].p_width;
777 string const LyXTabular::GetColumnPWidth(int cell) const
779 return column_info[column_of_cell(cell)].p_width;
783 string const LyXTabular::GetMColumnPWidth(int cell) const
785 if (IsMultiColumn(cell))
786 return cellinfo_of_cell(cell)->p_width;
791 string const LyXTabular::GetAlignSpecial(int cell, int what) const
793 if (what == SET_SPECIAL_MULTI)
794 return cellinfo_of_cell(cell)->align_special;
795 return column_info[column_of_cell(cell)].align_special;
799 int LyXTabular::GetWidthOfCell(int cell) const
801 int const row = row_of_cell(cell);
802 int const column1 = column_of_cell(cell);
803 int const column2 = right_column_of_cell(cell);
805 for (int i = column1; i <= column2; ++i) {
806 result += cell_info[row][i].width_of_cell;
812 int LyXTabular::GetBeginningOfTextInCell(int cell) const
816 switch (GetAlignment(cell)){
817 case LYX_ALIGN_CENTER:
818 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
820 case LYX_ALIGN_RIGHT:
821 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
822 // + GetAdditionalWidth(cell);
824 default: /* LYX_ALIGN_LEFT: nothing :-) */
834 bool LyXTabular::IsFirstCellInRow(int cell) const
836 return column_of_cell(cell) == 0;
840 int LyXTabular::GetFirstCellInRow(int row) const
844 return cell_info[row][0].cellno;
847 bool LyXTabular::IsLastCellInRow(int cell) const
849 return (right_column_of_cell(cell) == (columns_ - 1));
853 int LyXTabular::GetLastCellInRow(int row) const
857 return cell_info[row][columns_-1].cellno;
861 bool LyXTabular::calculate_width_of_column(int column)
863 int const old_column_width = column_info[column].width_of_column;
866 for (int i = 0; i < rows_; ++i) {
867 maximum = max(cell_info[i][column].width_of_cell, maximum);
869 column_info[column].width_of_column = maximum;
870 return (column_info[column].width_of_column != old_column_width);
875 // calculate the with of the column without regarding REAL MultiColumn
876 // cells. This means MultiColumn-cells spanning more than 1 column.
878 bool LyXTabular::calculate_width_of_column_NMC(int column)
880 int const old_column_width = column_info[column].width_of_column;
882 for (int i = 0; i < rows_; ++i) {
883 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
884 (cell_info[i][column].width_of_cell > max)) {
885 max = cell_info[i][column].width_of_cell;
888 column_info[column].width_of_column = max;
889 return (column_info[column].width_of_column != old_column_width);
893 void LyXTabular::calculate_width_of_tabular()
895 width_of_tabular = 0;
896 for (int i = 0; i < columns_; ++i) {
897 width_of_tabular += column_info[i].width_of_column;
902 int LyXTabular::row_of_cell(int cell) const
904 if (cell >= numberofcells)
908 return rowofcell[cell];
912 int LyXTabular::column_of_cell(int cell) const
914 if (cell >= numberofcells)
918 return columnofcell[cell];
922 int LyXTabular::right_column_of_cell(int cell) const
924 int const row = row_of_cell(cell);
925 int column = column_of_cell(cell);
926 while (column < (columns_ - 1) &&
927 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
933 // Perfect case for a template... (Lgb)
936 string const write_attribute(string const & name, T const & t)
938 string str = " " + name + "=\"" + tostr(t) + "\"";
943 string const write_attribute(string const & name, bool const & b)
945 return write_attribute(name, int(b));
950 string const write_attribute(string const & name, int value)
952 string str = " " + name + "=\"" + tostr(value) + "\"";
957 string const write_attribute(string const & name, string const & value)
959 string str = " " + name + "=\"" + value + "\"";
964 string const write_attribute(string const & name, bool value)
966 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
974 string const tostr(LyXAlignment const & num)
979 case LYX_ALIGN_BLOCK:
983 case LYX_ALIGN_CENTER:
985 case LYX_ALIGN_RIGHT:
987 case LYX_ALIGN_LAYOUT:
989 case LYX_ALIGN_SPECIAL:
998 string const tostr(LyXTabular::VAlignment const & num)
1001 case LyXTabular::LYX_VALIGN_TOP:
1003 case LyXTabular::LYX_VALIGN_CENTER:
1005 case LyXTabular::LYX_VALIGN_BOTTOM:
1014 string const tostr(LyXTabular::BoxType const & num)
1017 case LyXTabular::BOX_NONE:
1019 case LyXTabular::BOX_PARBOX:
1021 case LyXTabular::BOX_MINIPAGE:
1028 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1032 << write_attribute("version", 2)
1033 << write_attribute("rows", rows_)
1034 << write_attribute("columns", columns_)
1036 // global longtable options
1038 << write_attribute("rotate", tostr(rotate))
1039 << write_attribute("islongtable", tostr(is_long_tabular))
1040 << write_attribute("endhead", endhead)
1041 << write_attribute("endfirsthead", endfirsthead)
1042 << write_attribute("endfoot", endfoot)
1043 << write_attribute("endlastfoot", endlastfoot)
1045 for (int j = 0; j < columns_; ++j) {
1047 << write_attribute("alignment", tostr(column_info[j].alignment))
1048 << write_attribute("valignment", tostr(column_info[j].valignment))
1049 << write_attribute("leftline", tostr(column_info[j].left_line))
1050 << write_attribute("rightline", tostr(column_info[j].right_line))
1051 << write_attribute("width",
1052 VSpace(column_info[j].p_width)
1054 << write_attribute("special", column_info[j].align_special)
1057 for (int i = 0; i < rows_; ++i) {
1059 << write_attribute("topline", tostr(row_info[i].top_line))
1060 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1061 << write_attribute("newpage", tostr(row_info[i].newpage))
1063 for (int j = 0; j < columns_; ++j) {
1065 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1066 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1067 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1068 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1069 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1070 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1071 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1072 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1073 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1074 << write_attribute("width", cell_info[i][j].p_width)
1075 << write_attribute("special", cell_info[i][j].align_special)
1077 os << "\\begin_inset ";
1078 cell_info[i][j].inset.Write(buf, os);
1079 os << "\n\\end_inset \n"
1084 os << "</lyxtabular>\n";
1090 // I would have liked a fromstr template a lot better. (Lgb)
1093 bool string2type(string const str, LyXAlignment & num)
1096 num = LYX_ALIGN_NONE;
1097 else if (str == "block")
1098 num = LYX_ALIGN_BLOCK;
1099 else if (str == "left")
1100 num = LYX_ALIGN_LEFT;
1101 else if (str == "center")
1102 num = LYX_ALIGN_CENTER;
1103 else if (str == "right")
1104 num = LYX_ALIGN_RIGHT;
1112 bool string2type(string const str, LyXTabular::VAlignment & num)
1115 num = LyXTabular::LYX_VALIGN_TOP;
1116 else if (str == "center")
1117 num = LyXTabular::LYX_VALIGN_CENTER;
1118 else if (str == "bottom")
1119 num = LyXTabular::LYX_VALIGN_BOTTOM;
1127 bool string2type(string const str, LyXTabular::BoxType & num)
1130 num = LyXTabular::BOX_NONE;
1131 else if (str == "parbox")
1132 num = LyXTabular::BOX_PARBOX;
1133 else if (str == "minipage")
1134 num = LyXTabular::BOX_MINIPAGE;
1142 bool string2type(string const str, bool & num)
1146 else if (str == "false")
1154 bool getTokenValue(string const & str, const char * token, string & ret)
1156 size_t token_length = strlen(token);
1157 string::size_type pos = str.find(token);
1159 if (pos == string::npos || pos + token_length + 1 >= str.length()
1160 || str[pos + token_length] != '=')
1163 pos += token_length + 1;
1165 if ((ch != '"') && (ch != '\'')) { // only read till next space
1169 while((pos < str.length() - 1) && (str[++pos] != ch))
1176 bool getTokenValue(string const & str, const char * token, int & num)
1179 if (!getTokenValue(str, token, tmp))
1181 num = strToInt(tmp);
1186 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1189 if (!getTokenValue(str, token, tmp))
1191 return string2type(tmp, num);
1195 bool getTokenValue(string const & str, const char * token,
1196 LyXTabular::VAlignment & num)
1199 if (!getTokenValue(str, token, tmp))
1201 return string2type(tmp, num);
1205 bool getTokenValue(string const & str, const char * token,
1206 LyXTabular::BoxType & num)
1209 if (!getTokenValue(str, token, tmp))
1211 return string2type(tmp, num);
1215 bool getTokenValue(string const & str, const char * token, bool & flag)
1218 if (!getTokenValue(str, token, tmp))
1220 return string2type(tmp, flag);
1225 void l_getline(istream & is, string & str)
1228 while (str.empty()) {
1230 if (!str.empty() && str[str.length() - 1] == '\r')
1231 str.erase(str.length() - 1);
1238 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1241 istream & is = lex.getStream();
1243 l_getline(is, line);
1244 if (!prefixIs(line, "<lyxtabular ")
1245 && !prefixIs(line, "<LyXTabular ")) {
1246 OldFormatRead(lex, line);
1251 if (!getTokenValue(line, "version", version))
1254 ReadOld(buf, is, lex, line);
1255 else if (version == 2)
1256 ReadNew(buf, is, lex, line);
1260 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1261 LyXLex & lex, string const & l)
1265 if (!getTokenValue(line, "rows", rows_arg))
1268 if (!getTokenValue(line, "columns", columns_arg))
1270 Init(rows_arg, columns_arg);
1271 l_getline(is, line);
1272 if (!prefixIs(line, "<features")) {
1273 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1274 line << ")" << endl;
1277 getTokenValue(line, "rotate", rotate);
1278 getTokenValue(line, "islongtable", is_long_tabular);
1279 getTokenValue(line, "endhead", endhead);
1280 getTokenValue(line, "endfirsthead", endfirsthead);
1281 getTokenValue(line, "endfoot", endfoot);
1282 getTokenValue(line, "endlastfoot", endlastfoot);
1284 for (int j = 0; j < columns_; ++j) {
1286 if (!prefixIs(line,"<column")) {
1287 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1288 line << ")" << endl;
1291 getTokenValue(line, "alignment", column_info[j].alignment);
1292 getTokenValue(line, "valignment", column_info[j].valignment);
1293 getTokenValue(line, "leftline", column_info[j].left_line);
1294 getTokenValue(line, "rightline", column_info[j].right_line);
1295 getTokenValue(line, "width", column_info[j].p_width);
1296 getTokenValue(line, "special", column_info[j].align_special);
1299 for (int i = 0; i < rows_; ++i) {
1300 l_getline(is, line);
1301 if (!prefixIs(line, "<row")) {
1302 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1303 line << ")" << endl;
1306 getTokenValue(line, "topline", row_info[i].top_line);
1307 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1308 getTokenValue(line, "newpage", row_info[i].newpage);
1309 for (int j = 0; j < columns_; ++j) {
1310 l_getline(is, line);
1311 if (!prefixIs(line, "<cell")) {
1312 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1313 line << ")" << endl;
1316 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1317 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1318 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1319 getTokenValue(line, "topline", cell_info[i][j].top_line);
1320 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1321 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1322 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1323 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1324 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1325 getTokenValue(line, "width", cell_info[i][j].p_width);
1326 getTokenValue(line, "special", cell_info[i][j].align_special);
1327 l_getline(is, line);
1328 if (prefixIs(line, "\\begin_inset")) {
1329 cell_info[i][j].inset.Read(buf, lex);
1330 l_getline(is, line);
1332 if (!prefixIs(line, "</cell>")) {
1333 lyxerr << "Wrong tabular format (expected </cell> got" <<
1334 line << ")" << endl;
1338 l_getline(is, line);
1339 if (!prefixIs(line, "</row>")) {
1340 lyxerr << "Wrong tabular format (expected </row> got" <<
1341 line << ")" << endl;
1345 while (!prefixIs(line, "</lyxtabular>")) {
1346 l_getline(is, line);
1348 set_row_column_number_info();
1352 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1358 int columns_arg = 0;
1359 int is_long_tabular_arg = false;
1360 int rotate_arg = false;
1369 istream & is = lex.getStream();
1372 version = lyx::atoi(s.substr(8, string::npos));
1376 vector<int> cont_row_info;
1379 lyxerr << "Tabular format < 5 is not supported anymore\n"
1380 "Get an older version of LyX (< 1.1.x) for conversion!"
1382 WriteAlert(_("Warning:"),
1383 _("Tabular format < 5 is not supported anymore\n"),
1384 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1386 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1387 >> rotate_arg >> a >> b >> c >> d;
1389 is >> rows_arg >> columns_arg;
1390 Init(rows_arg, columns_arg);
1391 cont_row_info = vector<int>(rows_arg);
1392 SetLongTabular(is_long_tabular_arg);
1393 SetRotateTabular(rotate_arg);
1395 for (i = 0; i < rows_; ++i) {
1397 cont_row_info[i] = false;
1399 for (i = 0; i < columns_; ++i) {
1402 for (i = 0; i < rows_; ++i) {
1403 for (j = 0; j < columns_; ++j) {
1408 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1409 >> rotate_arg >> a >> b >> c >> d;
1410 Init(rows_arg, columns_arg);
1411 cont_row_info = vector<int>(rows_arg);
1412 SetLongTabular(is_long_tabular_arg);
1413 SetRotateTabular(rotate_arg);
1415 endfirsthead = b + 1;
1417 endlastfoot = d + 1;
1418 for (i = 0; i < rows_; ++i) {
1419 a = b = c = d = e = f = g = 0;
1420 is >> a >> b >> c >> d;
1421 row_info[i].top_line = a;
1422 row_info[i].bottom_line = b;
1423 cont_row_info[i] = c;
1424 row_info[i].newpage = d;
1426 for (i = 0; i < columns_; ++i) {
1431 char ch; // skip '"'
1434 // ignore is buggy but we will use it later (Lgb)
1435 is.ignore(); // skip '"'
1437 getline(is, s1, '"');
1439 is >> ch; // skip '"'
1441 // ignore is buggy but we will use it later (Lgb)
1442 is.ignore(); // skip '"'
1444 getline(is, s2, '"');
1445 column_info[i].alignment = static_cast<LyXAlignment>(a);
1446 column_info[i].left_line = b;
1447 column_info[i].right_line = c;
1448 column_info[i].p_width = s1;
1449 column_info[i].align_special = s2;
1451 for (i = 0; i < rows_; ++i) {
1452 for (j = 0; j < columns_; ++j) {
1455 is >> a >> b >> c >> d >> e >> f >> g;
1458 is >> ch; // skip '"'
1460 // ignore is buggy but we will use it later (Lgb)
1461 is.ignore(); // skip '"'
1463 getline(is, s1, '"');
1465 is >> ch; // skip '"'
1467 // ignore is buggy but we will use it later (Lgb)
1468 is.ignore(); // skip '"'
1470 getline(is, s2, '"');
1471 cell_info[i][j].multicolumn = static_cast<char>(a);
1472 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1473 cell_info[i][j].top_line = static_cast<char>(c);
1474 cell_info[i][j].bottom_line = static_cast<char>(d);
1475 cell_info[i][j].left_line = column_info[j].left_line;
1476 cell_info[i][j].right_line = column_info[j].right_line;
1477 cell_info[i][j].rotate = static_cast<bool>(f);
1478 cell_info[i][j].usebox = static_cast<BoxType>(g);
1479 cell_info[i][j].align_special = s1;
1480 cell_info[i][j].p_width = s2;
1484 set_row_column_number_info(true);
1486 LyXParagraph * par = new LyXParagraph;
1487 LyXParagraph * return_par = 0;
1492 LyXFont font(LyXFont::ALL_INHERIT);
1493 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1495 while (lex.IsOK()) {
1497 string const token = lex.GetString();
1500 if (token == "\\layout"
1501 || token == "\\end_float"
1502 || token == "\\end_deeper") {
1503 lex.pushToken(token);
1506 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1511 lex.pushToken(token);
1515 lex.printError("New Paragraph allocated! This should not happen!");
1516 lex.pushToken(token);
1522 // now we have the par we should fill the insets with this!
1524 InsetText * inset = GetCellInset(cell);
1527 for (int i = 0; i < par->size(); ++i) {
1528 if (par->IsNewline(i)) {
1530 if (cell > GetNumberOfCells()) {
1531 lyxerr << "Some error in reading old table format occured!" <<
1532 endl << "Terminating when reading cell[" << cell << "]!" <<
1537 row = row_of_cell(cell);
1538 if (cont_row_info[row]) {
1540 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1541 while(!IsFirstCellInRow(--cell));
1543 inset = GetCellInset(cell);
1546 inset = GetCellInset(cell);
1547 row = row_of_cell(cell);
1548 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1550 // insert a space instead
1552 par->InsertChar(i, ' ');
1555 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1556 inset->par->InsertFromMinibuffer(inset->par->size());
1563 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1565 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1566 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1570 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1572 int const row = row_of_cell(cell);
1573 int const column = column_of_cell(cell);
1574 return &cell_info[row][column];
1578 void LyXTabular::SetMultiColumn(int cell, int number)
1580 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1581 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1582 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1583 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1584 for (number--; number > 0; --number) {
1585 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1587 set_row_column_number_info();
1591 int LyXTabular::cells_in_multicolumn(int cell) const
1593 int const row = row_of_cell(cell);
1594 int column = column_of_cell(cell);
1597 while ((column < columns_) &&
1598 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1607 int LyXTabular::UnsetMultiColumn(int cell)
1609 int const row = row_of_cell(cell);
1610 int column = column_of_cell(cell);
1614 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1615 cell_info[row][column].multicolumn = CELL_NORMAL;
1617 while ((column < columns_) &&
1618 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1620 cell_info[row][column].multicolumn = CELL_NORMAL;
1625 set_row_column_number_info();
1630 void LyXTabular::SetLongTabular(bool what)
1632 is_long_tabular = what;
1636 bool LyXTabular::IsLongTabular() const
1638 return is_long_tabular;
1642 void LyXTabular::SetRotateTabular(bool flag)
1648 bool LyXTabular::GetRotateTabular() const
1654 void LyXTabular::SetRotateCell(int cell, bool flag)
1656 cellinfo_of_cell(cell)->rotate = flag;
1660 bool LyXTabular::GetRotateCell(int cell) const
1662 return cellinfo_of_cell(cell)->rotate;
1666 bool LyXTabular::NeedRotating() const
1670 for (int i = 0; i < rows_; ++i) {
1671 for (int j = 0; j < columns_; ++j) {
1672 if (cell_info[i][j].rotate)
1680 bool LyXTabular::IsLastCell(int cell) const
1682 if ((cell + 1) < GetNumberOfCells())
1688 int LyXTabular::GetCellAbove(int cell) const
1690 if (row_of_cell(cell) > 0)
1691 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1696 int LyXTabular::GetCellBelow(int cell) const
1698 if (row_of_cell(cell) + 1 < rows_)
1699 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1704 int LyXTabular::GetLastCellAbove(int cell) const
1706 if (row_of_cell(cell) <= 0)
1708 if (!IsMultiColumn(cell))
1709 return GetCellAbove(cell);
1710 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1714 int LyXTabular::GetLastCellBelow(int cell) const
1716 if (row_of_cell(cell) + 1 >= rows_)
1718 if (!IsMultiColumn(cell))
1719 return GetCellBelow(cell);
1720 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1724 int LyXTabular::GetCellNumber(int row, int column) const
1726 if (column >= columns_)
1727 column = columns_ - 1;
1728 else if (column < 0)
1735 return cell_info[row][column].cellno;
1739 void LyXTabular::SetUsebox(int cell, BoxType type)
1741 cellinfo_of_cell(cell)->usebox = type;
1745 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1747 if (column_info[column_of_cell(cell)].p_width.empty() &&
1748 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1750 if (cellinfo_of_cell(cell)->usebox > 1)
1751 return cellinfo_of_cell(cell)->usebox;
1752 return UseParbox(cell);
1756 void LyXTabular::SetLTHead(int cell, bool first)
1758 int const row = row_of_cell(cell);
1759 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1762 if (endfirsthead == val)
1775 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1778 if (abs(endhead) > rows_)
1780 return (row_of_cell(cell) == abs(endhead) - 1);
1784 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1787 if (abs(endfirsthead) > rows_)
1789 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1793 void LyXTabular::SetLTFoot(int cell, bool last)
1795 int const row = row_of_cell(cell);
1796 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1799 if (endlastfoot == val)
1812 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1815 if ((endfoot + 1) > rows_)
1817 return (row_of_cell(cell) == abs(endfoot) - 1);
1821 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1824 if (abs(endlastfoot) > rows_)
1826 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1830 void LyXTabular::SetLTNewPage(int cell, bool what)
1832 row_info[row_of_cell(cell)].newpage = what;
1836 bool LyXTabular::GetLTNewPage(int cell) const
1838 return row_info[row_of_cell(cell)].newpage;
1842 bool LyXTabular::SetAscentOfRow(int row, int height)
1844 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1846 row_info[row].ascent_of_row = height;
1851 bool LyXTabular::SetDescentOfRow(int row, int height)
1853 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1855 row_info[row].descent_of_row = height;
1860 int LyXTabular::GetAscentOfRow(int row) const
1864 return row_info[row].ascent_of_row;
1868 int LyXTabular::GetDescentOfRow(int row) const
1872 return row_info[row].descent_of_row;
1876 int LyXTabular::GetHeightOfTabular() const
1880 for (int row = 0; row < rows_; ++row)
1881 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1882 GetAdditionalHeight(row);
1887 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1889 if ((row >= rows_) || (column >= columns_))
1891 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1895 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1897 if ((row < 0) || (row >= rows_))
1900 int const fcell = GetFirstCellInRow(row);
1901 int const n = NumberOfCellsInRow(fcell) + fcell;
1904 for (int i = fcell; i < n; ++i) {
1908 if (tmp == (n - fcell)){
1911 for (int i = fcell; i < n; ++i) {
1914 << column_of_cell(i) + 1
1916 << right_column_of_cell(i) + 1
1928 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1930 if ((row < 0) || (row >= rows_))
1933 int const fcell = GetFirstCellInRow(row);
1934 int const n = NumberOfCellsInRow(fcell) + fcell;
1937 for (int i = fcell; i < n; ++i) {
1941 if (tmp == (n-fcell)){
1944 for (int i = fcell; i < n; ++i) {
1945 if (BottomLine(i)) {
1947 << column_of_cell(i) + 1
1949 << right_column_of_cell(i) + 1
1961 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1965 if (GetRotateCell(cell)) {
1966 os << "\\begin{sideways}\n";
1969 if (IsMultiColumn(cell)) {
1970 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1971 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1972 os << cellinfo_of_cell(cell)->align_special << "}{";
1974 if (LeftLine(cell) &&
1975 (IsFirstCellInRow(cell) ||
1976 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1977 !RightLine(cell-1, true))))
1981 if (!GetPWidth(cell).empty()) {
1982 switch (GetVAlignment(cell)) {
1983 case LYX_VALIGN_TOP:
1986 case LYX_VALIGN_CENTER:
1989 case LYX_VALIGN_BOTTOM:
1993 os << "{" << GetPWidth(cell) << '}';
1995 switch (GetAlignment(cell)) {
1996 case LYX_ALIGN_LEFT:
1999 case LYX_ALIGN_RIGHT:
2007 if (RightLine(cell))
2009 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2015 if (GetUsebox(cell) == BOX_PARBOX) {
2017 switch (GetVAlignment(cell)) {
2018 case LYX_VALIGN_TOP:
2021 case LYX_VALIGN_CENTER:
2024 case LYX_VALIGN_BOTTOM:
2028 os << "]{" << GetPWidth(cell) << "}{";
2029 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2030 os << "\\begin{minipage}[";
2031 switch (GetVAlignment(cell)) {
2032 case LYX_VALIGN_TOP:
2035 case LYX_VALIGN_CENTER:
2038 case LYX_VALIGN_BOTTOM:
2042 os << "]{" << GetPWidth(cell) << "}\n";
2049 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2054 if (GetUsebox(cell) == BOX_PARBOX)
2056 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2057 os << "%\n\\end{minipage}";
2060 if (IsMultiColumn(cell)){
2063 if (GetRotateCell(cell)) {
2064 os << "%\n\\end{sideways}";
2071 int LyXTabular::Latex(Buffer const * buf,
2072 ostream & os, bool fragile, bool fp) const
2077 //+---------------------------------------------------------------------
2078 //+ first the opening preamble +
2079 //+---------------------------------------------------------------------
2082 os << "\\begin{sideways}\n";
2085 if (is_long_tabular)
2086 os << "\\begin{longtable}{";
2088 os << "\\begin{tabular}{";
2089 for (int i = 0; i < columns_; ++i) {
2090 if (column_info[i].left_line)
2092 if (!column_info[i].align_special.empty()) {
2093 os << column_info[i].align_special;
2094 } else if (!column_info[i].p_width.empty()) {
2095 switch (column_info[i].valignment) {
2096 case LYX_VALIGN_TOP:
2099 case LYX_VALIGN_CENTER:
2102 case LYX_VALIGN_BOTTOM:
2107 << column_info[i].p_width
2110 switch (column_info[i].alignment) {
2111 case LYX_ALIGN_LEFT:
2114 case LYX_ALIGN_RIGHT:
2122 if (column_info[i].right_line)
2128 //+---------------------------------------------------------------------
2129 //+ the single row and columns (cells) +
2130 //+---------------------------------------------------------------------
2132 for (int i = 0; i < rows_; ++i) {
2133 ret += TeXTopHLine(os, i);
2135 if (IsLongTabular()) {
2136 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2137 os << "\\endhead\n";
2140 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2141 os << "\\endfirsthead\n";
2144 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2145 os << "\\endfoot\n";
2148 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2149 os << "\\endlastfoot\n";
2154 ret += TeXBottomHLine(os, i-1);
2155 ret += TeXTopHLine(os, i);
2157 for (int j = 0; j < columns_; ++j) {
2158 if (IsPartOfMultiColumn(i,j))
2160 ret += TeXCellPreamble(os, cell);
2161 InsetText * inset = GetCellInset(cell);
2163 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2164 inset->par->size() > 0 && GetPWidth(cell).empty();
2168 ret += inset->Latex(buf, os, fragile, fp);
2172 ret += TeXCellPostamble(os, cell);
2173 if (!IsLastCellInRow(cell)) { // not last cell in row
2180 ret += TeXBottomHLine(os, i);
2182 if (IsLongTabular()) {
2183 if ((endhead > 0) && (i == (endhead - 1))) {
2184 os << "\\endhead\n";
2187 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2188 os << "\\endfirsthead\n";
2191 if ((endfoot > 0) && (i == (endfoot - 1))) {
2192 os << "\\endfoot\n";
2195 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2196 os << "\\endlastfoot\n";
2200 // ret += TeXBottomHLine(os, i);
2201 if (row_info[i].newpage) {
2202 os << "\\newpage\n";
2208 //+---------------------------------------------------------------------
2209 //+ the closing of the tabular +
2210 //+---------------------------------------------------------------------
2212 if (is_long_tabular)
2213 os << "\\end{longtable}";
2215 os << "\\end{tabular}";
2217 os << "\n\\end{sideways}";
2225 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2229 //+---------------------------------------------------------------------
2230 //+ first the opening preamble +
2231 //+---------------------------------------------------------------------
2233 os << "<tgroup cols=\"" << columns_
2234 << "\" colsep=\"1\" rowsep=\"1\">\n";
2236 for (int i = 0; i < columns_; ++i) {
2237 os << "<colspec colname=\"col" << i << "\" align=\"";
2238 switch (column_info[i].alignment) {
2239 case LYX_ALIGN_LEFT:
2242 case LYX_ALIGN_RIGHT:
2253 //+---------------------------------------------------------------------
2254 //+ the single row and columns (cells) +
2255 //+---------------------------------------------------------------------
2259 for (int i = 0; i < rows_; ++i) {
2261 for (int j = 0; j < columns_; ++j) {
2262 if (IsPartOfMultiColumn(i, j))
2265 os << "<entry align=\"";
2266 switch (GetAlignment(cell)) {
2267 case LYX_ALIGN_LEFT:
2270 case LYX_ALIGN_RIGHT:
2278 os << "\" valign=\"";
2279 switch (GetVAlignment(cell)) {
2280 case LYX_VALIGN_TOP:
2283 case LYX_VALIGN_BOTTOM:
2286 case LYX_VALIGN_CENTER:
2291 if (IsMultiColumn(cell)) {
2292 os << " namest=\"col" << j << "\" ";
2293 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2297 ret += GetCellInset(cell)->DocBook(buf, os);
2304 //+---------------------------------------------------------------------
2305 //+ the closing of the tabular +
2306 //+---------------------------------------------------------------------
2318 void print_n_chars(ostream & os, unsigned char ch, int n)
2320 os << string(n, ch);
2326 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2327 vector<unsigned int> const & clen) const
2329 int const fcell = GetFirstCellInRow(row);
2330 int const n = NumberOfCellsInRow(fcell) + fcell;
2333 for (int i = fcell; i < n; ++i) {
2343 for (int i = fcell; i < n; ++i) {
2354 int column = column_of_cell(i);
2355 int len = clen[column];
2356 while(IsPartOfMultiColumn(row, ++column))
2357 len += clen[column] + 4;
2358 print_n_chars(os, ch, len);
2373 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2374 vector<unsigned int> const & clen) const
2376 int const fcell = GetFirstCellInRow(row);
2377 int const n = NumberOfCellsInRow(fcell) + fcell;
2380 for (int i = fcell; i < n; ++i) {
2381 if (BottomLine(i)) {
2390 for (int i = fcell; i < n; ++i) {
2391 if (BottomLine(i)) {
2401 int column = column_of_cell(i);
2402 int len = clen[column];
2403 while(IsPartOfMultiColumn(row, ++column))
2404 len += clen[column] + 4;
2405 print_n_chars(os, ch, len);
2406 if (BottomLine(i)) {
2420 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2421 int cell, int row, int column,
2422 vector<unsigned int> const & clen) const
2425 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2432 unsigned int len1 = sstr.str().length();
2433 unsigned int len2 = clen[column];
2434 while(IsPartOfMultiColumn(row, ++column))
2435 len2 += clen[column] + 4;
2438 switch (GetAlignment(cell)) {
2440 case LYX_ALIGN_LEFT:
2443 case LYX_ALIGN_RIGHT:
2447 case LYX_ALIGN_CENTER:
2453 for (unsigned int i = 0; i < len1; ++i)
2456 for (unsigned int i = 0; i < len2; ++i)
2458 if (RightLine(cell))
2467 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2471 //+---------------------------------------------------------------------
2472 //+ first calculate the width of the single columns +
2473 //+---------------------------------------------------------------------
2474 vector<unsigned int> clen(columns_);
2476 // first all non (real) multicolumn cells!
2477 for (int j = 0; j < columns_; ++j) {
2479 for (int i = 0; i < rows_; ++i) {
2480 int cell = GetCellNumber(i, j);
2481 if (IsMultiColumn(cell, true))
2484 GetCellInset(cell)->Ascii(buf, sstr, 0);
2485 if (clen[j] < sstr.str().length())
2486 clen[j] = sstr.str().length();
2489 // then all (real) multicolumn cells!
2490 for (int j = 0; j < columns_; ++j) {
2491 for (int i = 0; i < rows_; ++i) {
2492 int cell = GetCellNumber(i, j);
2493 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2496 GetCellInset(cell)->Ascii(buf, sstr, 0);
2497 int len = int(sstr.str().length());
2498 int const n = cells_in_multicolumn(cell);
2499 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2501 if (len > int(clen[j + n - 1]))
2502 clen[j + n - 1] = len;
2506 for (int i = 0; i < rows_; ++i) {
2507 AsciiTopHLine(os, i, clen);
2508 for (int j = 0; j < columns_; ++j) {
2509 if (IsPartOfMultiColumn(i,j))
2511 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2515 AsciiBottomHLine(os, i, clen);
2521 InsetText * LyXTabular::GetCellInset(int cell) const
2523 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2527 InsetText * LyXTabular::GetCellInset(int row, int column) const
2529 return & cell_info[row][column].inset;
2533 void LyXTabular::Validate(LaTeXFeatures & features) const
2535 if (IsLongTabular())
2536 features.longtable = true;
2538 features.rotating = true;
2539 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2540 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2541 features.array = true;
2542 GetCellInset(cell)->Validate(features);
2547 std::vector<string> const LyXTabular::getLabelList() const
2549 std::vector<string> label_list;
2550 for (int i = 0; i < rows_; ++i)
2551 for (int j = 0; j < columns_; ++j) {
2552 std::vector<string> const l =
2553 GetCellInset(i, j)->getLabelList();
2554 label_list.insert(label_list.end(),
2555 l.begin(), l.end());
2561 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2563 LyXParagraph * par = GetCellInset(cell)->par;
2565 for (; par; par = par->next()) {
2566 for (int i = 0; i < par->size(); ++i) {
2567 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)