1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
26 #include "lyx_gui_misc.h"
28 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
46 int const WIDTH_OF_LINE = 5;
50 /// Define a few methods for the inner structs
52 LyXTabular::cellstruct::cellstruct()
56 multicolumn = LyXTabular::CELL_NORMAL;
57 alignment = LYX_ALIGN_CENTER;
58 valignment = LYX_VALIGN_TOP;
68 LyXTabular::rowstruct::rowstruct()
78 LyXTabular::columnstruct::columnstruct()
82 alignment = LYX_ALIGN_CENTER;
83 valignment = LYX_VALIGN_TOP;
89 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
92 Init(rows_arg, columns_arg);
96 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
99 Init(lt.rows_, lt.columns_);
101 #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)
107 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
114 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
116 // If this and lt is not of the same size we have a serious bug
117 // So then it is ok to throw an exception, or for now
119 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
121 cell_info = lt.cell_info;
122 row_info = lt.row_info;
123 column_info = lt.column_info;
125 // long tabular stuff
126 SetLongTabular(lt.is_long_tabular);
127 endhead = lt.endhead;
128 endfoot = lt.endfoot;
129 endfirsthead = lt.endfirsthead;
130 endlastfoot = lt.endlastfoot;
140 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
142 LyXTabular * result = new LyXTabular(inset, *this);
143 // don't know if this is good but I need to Clone also
144 // the text-insets here, this is for the Undo-facility!
145 for (int i = 0; i < rows_; ++i) {
146 for (int j = 0; j < columns_; ++j) {
147 result->cell_info[i][j].inset = cell_info[i][j].inset;
148 result->cell_info[i][j].inset.setOwner(inset);
155 /* activates all lines and sets all widths to 0 */
156 void LyXTabular::Init(int rows_arg, int columns_arg)
159 columns_ = columns_arg;
160 row_info = row_vector(rows_, rowstruct());
161 column_info = column_vector(columns_, columnstruct());
162 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
165 for (int i = 0; i < rows_; ++i) {
166 for (int j = 0; j < columns_; ++j) {
167 cell_info[i][j].inset.setOwner(owner_);
168 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
169 cell_info[i][j].cellno = cellno++;
171 cell_info[i].back().right_line = true;
173 row_info.back().bottom_line = true;
174 row_info.front().bottom_line = true;
176 for (int i = 0; i < columns_; ++i) {
177 calculate_width_of_column(i);
179 column_info.back().right_line = true;
181 calculate_width_of_tabular();
183 rowofcell = vector<int>();
184 columnofcell = vector<int>();
185 set_row_column_number_info();
186 is_long_tabular = false;
195 void LyXTabular::AppendRow(int cell)
199 int row = row_of_cell(cell);
201 row_vector::iterator rit = row_info.begin() + row;
202 row_info.insert(rit, rowstruct());
203 // now set the values of the row before
204 row_info[row] = row_info[row+1];
207 cell_vvector::iterator cit = cell_info.begin() + row;
208 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
210 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
213 for (int i = 0; i <= row; ++i) {
214 for (int j = 0; j < columns_; ++j) {
215 c_info[i][j] = cell_info[i][j];
218 for (int i = row + 1; i < rows_; ++i) {
219 for (int j = 0; j < columns_; ++j) {
220 c_info[i][j] = cell_info[i-1][j];
225 for (int j = 0; j < columns_; ++j) {
226 cell_info[row][j].inset.clear();
233 void LyXTabular::DeleteRow(int row)
235 // Why make it so hard? (Lgb)
238 if (rows_ == 1) return; // Not allowed to delete last row
240 row_info.erase(row_info.begin() + row); //&row_info[row]);
241 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
247 void LyXTabular::AppendColumn(int cell)
251 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
253 int const column = column_of_cell(cell);
254 column_vector::iterator cit = column_info.begin() + column + 1;
255 column_info.insert(cit, columnstruct());
256 // set the column values of the column before
257 column_info[column+1] = column_info[column];
259 for (int i = 0; i < rows_; ++i) {
260 for (int j = 0; j <= column; ++j) {
261 c_info[i][j] = cell_info[i][j];
263 for (int j = column + 1; j < columns_; ++j) {
264 c_info[i][j] = cell_info[i][j - 1];
266 // care about multicolumns
267 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
268 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
270 if ((column + 1) == columns_ ||
271 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
272 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
277 for (int i = 0; i < rows_; ++i) {
278 //cell_info[i][column].inset.clear();
279 cell_info[i][column + 1].inset.clear();
285 void LyXTabular::DeleteColumn(int column)
287 // Similar to DeleteRow
288 //if (!(columns_ - 1))
290 if (columns_ == 1) return; // Not allowed to delete last column
292 column_info.erase(column_info.begin() + column);
293 for (int i = 0; i < rows_; ++i) {
294 cell_info[i].erase(cell_info[i].begin() + column);
301 void LyXTabular::Reinit()
303 for (int i = 0; i < rows_; ++i) {
304 for (int j = 0; j < columns_; ++j) {
305 cell_info[i][j].width_of_cell = 0;
306 cell_info[i][j].inset.setOwner(owner_);
310 for (int i = 0; i < columns_; ++i) {
311 calculate_width_of_column(i);
313 calculate_width_of_tabular();
315 set_row_column_number_info();
319 void LyXTabular::set_row_column_number_info(bool oldformat)
322 for (int row = 0; row < rows_; ++row) {
323 for (int column = 0; column<columns_; ++column) {
324 if (cell_info[row][column].multicolumn
325 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
327 cell_info[row][column].cellno = numberofcells;
330 ++numberofcells; // because this is one more than as we start from 0
332 rowofcell.resize(numberofcells);
333 columnofcell.resize(numberofcells);
335 for (int row = 0, column = 0, c = 0;
336 c < numberofcells && row < rows_ && column < columns_;) {
338 columnofcell[c] = column;
342 } while (column < columns_ &&
343 cell_info[row][column].multicolumn
344 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
345 if (column == columns_) {
351 for (int row = 0; row < rows_; ++row) {
352 for (int column = 0; column < columns_; ++column) {
353 if (IsPartOfMultiColumn(row,column))
355 // now set the right line of multicolumns right for oldformat read
357 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
359 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
360 cell_info[row][column].right_line =
361 cell_info[row][column+cn-1].right_line;
363 cell_info[row][column].inset.SetAutoBreakRows(
364 !GetPWidth(GetCellNumber(row, column)).empty());
370 int LyXTabular::GetNumberOfCells() const
372 return numberofcells;
376 int LyXTabular::NumberOfCellsInRow(int cell) const
378 int const row = row_of_cell(cell);
380 for (int i = 0; i < columns_; ++i) {
381 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
388 /* returns 1 if there is a topline, returns 0 if not */
389 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
391 int const row = row_of_cell(cell);
393 if (!onlycolumn && IsMultiColumn(cell))
394 return cellinfo_of_cell(cell)->top_line;
395 return row_info[row].top_line;
399 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
401 // no bottom line underneath non-existent cells if you please
402 // Isn't that a programming error? Is so this should
403 // be an Assert instead. (Lgb)
404 if (cell >= numberofcells)
407 if (!onlycolumn && IsMultiColumn(cell))
408 return cellinfo_of_cell(cell)->bottom_line;
409 return row_info[row_of_cell(cell)].bottom_line;
413 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
415 if (!onlycolumn && IsMultiColumn(cell))
416 return cellinfo_of_cell(cell)->left_line;
417 return column_info[column_of_cell(cell)].left_line;
421 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
423 if (!onlycolumn && IsMultiColumn(cell))
424 return cellinfo_of_cell(cell)->right_line;
425 return column_info[right_column_of_cell(cell)].right_line;
429 bool LyXTabular::TopAlreadyDrawed(int cell) const
431 if (GetAdditionalHeight(cell))
433 int row = row_of_cell(cell);
435 int column = column_of_cell(cell);
438 && cell_info[row][column].multicolumn
439 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
441 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
442 return row_info[row].bottom_line;
444 return cell_info[row][column].bottom_line;
450 bool LyXTabular::LeftAlreadyDrawed(int cell) const
452 int column = column_of_cell(cell);
454 int row = row_of_cell(cell);
456 (cell_info[row][column].multicolumn ==
457 LyXTabular::CELL_PART_OF_MULTICOLUMN));
458 if (GetAdditionalWidth(cell_info[row][column].cellno))
460 return column_info[column].right_line;
466 bool LyXTabular::IsLastRow(int cell) const
468 return (row_of_cell(cell) == rows_ - 1);
472 int LyXTabular::GetAdditionalHeight(int cell) const
474 int const row = row_of_cell(cell);
480 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
481 switch (cell_info[row - 1][column].multicolumn) {
482 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
483 bottom = cell_info[row - 1][column].bottom_line;
485 case LyXTabular::CELL_NORMAL:
486 bottom = row_info[row - 1].bottom_line;
489 for (int column = 0; column < columns_ - 1 && top; ++column) {
490 switch (cell_info[row][column].multicolumn){
491 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
492 top = cell_info[row][column].top_line;
494 case LyXTabular::CELL_NORMAL:
495 top = row_info[row].top_line;
499 return WIDTH_OF_LINE;
504 int LyXTabular::GetAdditionalWidth(int cell) const
506 // internally already set in SetWidthOfCell
507 // used to get it back in text.C
508 int const col = right_column_of_cell(cell);
509 if (col < columns_ - 1 && column_info[col].right_line &&
510 column_info[col+1].left_line)
511 return WIDTH_OF_LINE;
517 // returns the maximum over all rows
518 int LyXTabular::GetWidthOfColumn(int cell) const
520 int const column1 = column_of_cell(cell);
521 int const column2 = right_column_of_cell(cell);
523 for (int i = column1; i <= column2; ++i) {
524 result += column_info[i].width_of_column;
530 int LyXTabular::GetWidthOfTabular() const
532 return width_of_tabular;
536 /* returns 1 if a complete update is necessary, otherwise 0 */
537 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
539 if (!IsMultiColumn(cell))
542 int const row = row_of_cell(cell);
543 int const column1 = column_of_cell(cell);
544 int const column2 = right_column_of_cell(cell);
546 // first set columns to 0 so we can calculate the right width
547 for (int i = column1; i <= column2; ++i) {
548 cell_info[row][i].width_of_cell = 0;
550 // set the width to MAX_WIDTH until width > 0
551 int width = (new_width + 2 * WIDTH_OF_LINE);
554 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
555 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
556 width -= column_info[i].width_of_column;
559 cell_info[row][i].width_of_cell = width;
565 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
567 int const row = row_of_cell(cell);
568 int const column1 = column_of_cell(cell);
569 int const column2 = right_column_of_cell(cell);
571 // first set columns to 0 so we can calculate the right width
573 for (; i <= column2; ++i)
574 cell_info[row][i].width_of_cell = 0;
575 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
577 if (i < numberofcells)
578 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
579 SetWidthOfMulticolCell(cell, new_width);
583 /* returns 1 if a complete update is necessary, otherwise 0 */
584 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
586 int const row = row_of_cell(cell);
587 int const column1 = column_of_cell(cell);
591 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
593 if (IsMultiColumn(cell, true)) {
594 tmp = SetWidthOfMulticolCell(cell, new_width);
596 width = (new_width + 2*WIDTH_OF_LINE);
597 cell_info[row][column1].width_of_cell = width;
598 if (column_info[column1].right_line && (column1 < columns_-1) &&
599 column_info[column1+1].left_line) // additional width
600 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
601 tmp = calculate_width_of_column_NMC(column1);
605 for (; i<columns_; ++i)
606 calculate_width_of_column_NMC(i);
607 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
609 if (i < numberofcells)
610 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
611 for (i = 0; i < columns_; ++i)
612 calculate_width_of_column(i);
613 calculate_width_of_tabular();
620 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
622 if (!IsMultiColumn(cell) || onlycolumn)
623 column_info[column_of_cell(cell)].alignment = align;
625 cellinfo_of_cell(cell)->alignment = align;
630 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
632 if (!IsMultiColumn(cell) || onlycolumn)
633 column_info[column_of_cell(cell)].valignment = align;
635 cellinfo_of_cell(cell)->valignment = align;
640 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
642 bool flag = !width.empty();
643 int const j = column_of_cell(cell);
645 column_info[j].p_width = width;
646 if (flag) // do this only if there is a width
647 SetAlignment(cell, LYX_ALIGN_LEFT);
648 for (int i = 0; i < rows_; ++i) {
649 int c = GetCellNumber(i, j);
650 flag = !GetPWidth(c).empty(); // because of multicolumns!
651 GetCellInset(c)->SetAutoBreakRows(flag);
657 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
659 bool const flag = !width.empty();
661 cellinfo_of_cell(cell)->p_width = width;
662 if (IsMultiColumn(cell)) {
663 GetCellInset(cell)->SetAutoBreakRows(flag);
670 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
671 LyXTabular::Feature what)
673 if (what == SET_SPECIAL_MULTI)
674 cellinfo_of_cell(cell)->align_special = special;
676 column_info[column_of_cell(cell)].align_special = special;
681 bool LyXTabular::SetAllLines(int cell, bool line)
683 SetTopLine(cell, line);
684 SetBottomLine(cell, line);
685 SetRightLine(cell, line);
686 SetLeftLine(cell, line);
691 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
693 int const row = row_of_cell(cell);
695 if (onlycolumn || !IsMultiColumn(cell))
696 row_info[row].top_line = line;
698 cellinfo_of_cell(cell)->top_line = line;
703 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
705 if (onlycolumn || !IsMultiColumn(cell))
706 row_info[row_of_cell(cell)].bottom_line = line;
708 cellinfo_of_cell(cell)->bottom_line = line;
713 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
715 if (onlycolumn || !IsMultiColumn(cell))
716 column_info[column_of_cell(cell)].left_line = line;
718 cellinfo_of_cell(cell)->left_line = line;
723 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
725 if (onlycolumn || !IsMultiColumn(cell))
726 column_info[right_column_of_cell(cell)].right_line = line;
728 cellinfo_of_cell(cell)->right_line = line;
733 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
735 if (!onlycolumn && IsMultiColumn(cell))
736 return cellinfo_of_cell(cell)->alignment;
738 return column_info[column_of_cell(cell)].alignment;
742 LyXTabular::VAlignment
743 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
745 if (!onlycolumn && IsMultiColumn(cell))
746 return cellinfo_of_cell(cell)->valignment;
748 return column_info[column_of_cell(cell)].valignment;
752 string const LyXTabular::GetPWidth(int cell) const
754 if (IsMultiColumn(cell))
755 return cellinfo_of_cell(cell)->p_width;
756 return column_info[column_of_cell(cell)].p_width;
760 string const LyXTabular::GetColumnPWidth(int cell) const
762 return column_info[column_of_cell(cell)].p_width;
766 string const LyXTabular::GetMColumnPWidth(int cell) const
768 if (IsMultiColumn(cell))
769 return cellinfo_of_cell(cell)->p_width;
774 string const LyXTabular::GetAlignSpecial(int cell, int what) const
776 if (what == SET_SPECIAL_MULTI)
777 return cellinfo_of_cell(cell)->align_special;
778 return column_info[column_of_cell(cell)].align_special;
782 int LyXTabular::GetWidthOfCell(int cell) const
784 int const row = row_of_cell(cell);
785 int const column1 = column_of_cell(cell);
786 int const column2 = right_column_of_cell(cell);
788 for (int i = column1; i <= column2; ++i) {
789 result += cell_info[row][i].width_of_cell;
795 int LyXTabular::GetBeginningOfTextInCell(int cell) const
799 switch (GetAlignment(cell)){
800 case LYX_ALIGN_CENTER:
801 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
803 case LYX_ALIGN_RIGHT:
804 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
805 // + GetAdditionalWidth(cell);
807 default: /* LYX_ALIGN_LEFT: nothing :-) */
817 bool LyXTabular::IsFirstCellInRow(int cell) const
819 return column_of_cell(cell) == 0;
823 int LyXTabular::GetFirstCellInRow(int row) const
827 return cell_info[row][0].cellno;
830 bool LyXTabular::IsLastCellInRow(int cell) const
832 return (right_column_of_cell(cell) == (columns_ - 1));
836 int LyXTabular::GetLastCellInRow(int row) const
840 return cell_info[row][columns_-1].cellno;
844 bool LyXTabular::calculate_width_of_column(int column)
846 int const old_column_width = column_info[column].width_of_column;
849 for (int i = 0; i < rows_; ++i) {
850 maximum = max(cell_info[i][column].width_of_cell, maximum);
852 column_info[column].width_of_column = maximum;
853 return (column_info[column].width_of_column != old_column_width);
858 // calculate the with of the column without regarding REAL MultiColumn
859 // cells. This means MultiColumn-cells spanning more than 1 column.
861 bool LyXTabular::calculate_width_of_column_NMC(int column)
863 int const old_column_width = column_info[column].width_of_column;
865 for (int i = 0; i < rows_; ++i) {
866 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
867 (cell_info[i][column].width_of_cell > max)) {
868 max = cell_info[i][column].width_of_cell;
871 column_info[column].width_of_column = max;
872 return (column_info[column].width_of_column != old_column_width);
876 void LyXTabular::calculate_width_of_tabular()
878 width_of_tabular = 0;
879 for (int i = 0; i < columns_; ++i) {
880 width_of_tabular += column_info[i].width_of_column;
885 int LyXTabular::row_of_cell(int cell) const
887 if (cell >= numberofcells)
891 return rowofcell[cell];
895 int LyXTabular::column_of_cell(int cell) const
897 if (cell >= numberofcells)
901 return columnofcell[cell];
905 int LyXTabular::right_column_of_cell(int cell) const
907 int const row = row_of_cell(cell);
908 int column = column_of_cell(cell);
909 while (column < (columns_ - 1) &&
910 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
916 // Perfect case for a template... (Lgb)
919 string const write_attribute(string const & name, T const & t)
921 string str = " " + name + "=\"" + tostr(t) + "\"";
926 string const write_attribute(string const & name, bool const & b)
928 return write_attribute(name, int(b));
933 string const write_attribute(string const & name, int value)
935 string str = " " + name + "=\"" + tostr(value) + "\"";
940 string const write_attribute(string const & name, string const & value)
942 string str = " " + name + "=\"" + value + "\"";
947 string const write_attribute(string const & name, bool value)
949 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
957 string const tostr(LyXAlignment const & num)
962 case LYX_ALIGN_BLOCK:
966 case LYX_ALIGN_CENTER:
968 case LYX_ALIGN_RIGHT:
970 case LYX_ALIGN_LAYOUT:
972 case LYX_ALIGN_SPECIAL:
981 string const tostr(LyXTabular::VAlignment const & num)
984 case LyXTabular::LYX_VALIGN_TOP:
986 case LyXTabular::LYX_VALIGN_CENTER:
988 case LyXTabular::LYX_VALIGN_BOTTOM:
997 string const tostr(LyXTabular::BoxType const & num)
1000 case LyXTabular::BOX_NONE:
1002 case LyXTabular::BOX_PARBOX:
1004 case LyXTabular::BOX_MINIPAGE:
1011 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1015 << write_attribute("version", 2)
1016 << write_attribute("rows", rows_)
1017 << write_attribute("columns", columns_)
1019 // global longtable options
1021 << write_attribute("rotate", tostr(rotate))
1022 << write_attribute("islongtable", tostr(is_long_tabular))
1023 << write_attribute("endhead", endhead)
1024 << write_attribute("endfirsthead", endfirsthead)
1025 << write_attribute("endfoot", endfoot)
1026 << write_attribute("endlastfoot", endlastfoot)
1028 for (int j = 0; j < columns_; ++j) {
1030 << write_attribute("alignment", tostr(column_info[j].alignment))
1031 << write_attribute("valignment", tostr(column_info[j].valignment))
1032 << write_attribute("leftline", tostr(column_info[j].left_line))
1033 << write_attribute("rightline", tostr(column_info[j].right_line))
1034 << write_attribute("width",
1035 VSpace(column_info[j].p_width)
1037 << write_attribute("special", column_info[j].align_special)
1040 for (int i = 0; i < rows_; ++i) {
1042 << write_attribute("topline", tostr(row_info[i].top_line))
1043 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1044 << write_attribute("newpage", tostr(row_info[i].newpage))
1046 for (int j = 0; j < columns_; ++j) {
1048 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1049 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1050 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1051 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1052 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1053 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1054 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1055 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1056 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1057 << write_attribute("width", cell_info[i][j].p_width)
1058 << write_attribute("special", cell_info[i][j].align_special)
1060 os << "\\begin_inset ";
1061 cell_info[i][j].inset.Write(buf, os);
1062 os << "\n\\end_inset \n"
1067 os << "</lyxtabular>\n";
1073 // I would have liked a fromstr template a lot better. (Lgb)
1076 bool string2type(string const str, LyXAlignment & num)
1079 num = LYX_ALIGN_NONE;
1080 else if (str == "block")
1081 num = LYX_ALIGN_BLOCK;
1082 else if (str == "left")
1083 num = LYX_ALIGN_LEFT;
1084 else if (str == "center")
1085 num = LYX_ALIGN_CENTER;
1086 else if (str == "right")
1087 num = LYX_ALIGN_RIGHT;
1095 bool string2type(string const str, LyXTabular::VAlignment & num)
1098 num = LyXTabular::LYX_VALIGN_TOP;
1099 else if (str == "center")
1100 num = LyXTabular::LYX_VALIGN_CENTER;
1101 else if (str == "bottom")
1102 num = LyXTabular::LYX_VALIGN_BOTTOM;
1110 bool string2type(string const str, LyXTabular::BoxType & num)
1113 num = LyXTabular::BOX_NONE;
1114 else if (str == "parbox")
1115 num = LyXTabular::BOX_PARBOX;
1116 else if (str == "minipage")
1117 num = LyXTabular::BOX_MINIPAGE;
1125 bool string2type(string const str, bool & num)
1129 else if (str == "false")
1137 bool getTokenValue(string const & str, const char * token, string & ret)
1139 size_t token_length = strlen(token);
1140 string::size_type pos = str.find(token);
1142 if (pos == string::npos || pos+token_length+1 >= str.length()
1143 || str[pos+token_length] != '=')
1146 pos += token_length + 1;
1148 if ((ch != '"') && (ch != '\'')) { // only read till next space
1152 while((pos < str.length() - 1) && (str[++pos] != ch))
1159 bool getTokenValue(string const & str, const char * token, int & num)
1162 if (!getTokenValue(str, token, tmp))
1164 num = strToInt(tmp);
1169 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1172 if (!getTokenValue(str, token, tmp))
1174 return string2type(tmp, num);
1178 bool getTokenValue(string const & str, const char * token,
1179 LyXTabular::VAlignment & num)
1182 if (!getTokenValue(str, token, tmp))
1184 return string2type(tmp, num);
1188 bool getTokenValue(string const & str, const char * token,
1189 LyXTabular::BoxType & num)
1192 if (!getTokenValue(str, token, tmp))
1194 return string2type(tmp, num);
1198 bool getTokenValue(string const & str, const char * token, bool & flag)
1201 if (!getTokenValue(str, token, tmp))
1203 return string2type(tmp, flag);
1208 void l_getline(istream & is, string & str)
1211 while (str.empty()) {
1213 if (!str.empty() && str[str.length() - 1] == '\r')
1214 str.erase(str.length() - 1);
1221 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1224 istream & is = lex.getStream();
1226 l_getline(is, line);
1227 if (!prefixIs(line, "<lyxtabular ")
1228 && !prefixIs(line, "<LyXTabular ")) {
1229 OldFormatRead(lex, line);
1234 if (!getTokenValue(line, "version", version))
1237 ReadOld(buf, is, lex, line);
1238 else if (version == 2)
1239 ReadNew(buf, is, lex, line);
1243 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1244 LyXLex & lex, string const & l)
1248 if (!getTokenValue(line, "rows", rows_arg))
1251 if (!getTokenValue(line, "columns", columns_arg))
1253 Init(rows_arg, columns_arg);
1254 l_getline(is, line);
1255 if (!prefixIs(line, "<features")) {
1256 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1257 line << ")" << endl;
1260 getTokenValue(line, "rotate", rotate);
1261 getTokenValue(line, "islongtable", is_long_tabular);
1262 getTokenValue(line, "endhead", endhead);
1263 getTokenValue(line, "endfirsthead", endfirsthead);
1264 getTokenValue(line, "endfoot", endfoot);
1265 getTokenValue(line, "endlastfoot", endlastfoot);
1267 for (int j = 0; j < columns_; ++j) {
1269 if (!prefixIs(line,"<column")) {
1270 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1271 line << ")" << endl;
1274 getTokenValue(line, "alignment", column_info[j].alignment);
1275 getTokenValue(line, "valignment", column_info[j].valignment);
1276 getTokenValue(line, "leftline", column_info[j].left_line);
1277 getTokenValue(line, "rightline", column_info[j].right_line);
1278 getTokenValue(line, "width", column_info[j].p_width);
1279 getTokenValue(line, "special", column_info[j].align_special);
1282 for (int i = 0; i < rows_; ++i) {
1283 l_getline(is, line);
1284 if (!prefixIs(line, "<row")) {
1285 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1286 line << ")" << endl;
1289 getTokenValue(line, "topline", row_info[i].top_line);
1290 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1291 getTokenValue(line, "newpage", row_info[i].newpage);
1292 for (int j = 0; j < columns_; ++j) {
1293 l_getline(is, line);
1294 if (!prefixIs(line, "<cell")) {
1295 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1296 line << ")" << endl;
1299 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1300 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1301 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1302 getTokenValue(line, "topline", cell_info[i][j].top_line);
1303 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1304 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1305 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1306 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1307 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1308 getTokenValue(line, "width", cell_info[i][j].p_width);
1309 getTokenValue(line, "special", cell_info[i][j].align_special);
1310 l_getline(is, line);
1311 if (prefixIs(line, "\\begin_inset")) {
1312 cell_info[i][j].inset.Read(buf, lex);
1313 l_getline(is, line);
1315 if (!prefixIs(line, "</cell>")) {
1316 lyxerr << "Wrong tabular format (expected </cell> got" <<
1317 line << ")" << endl;
1321 l_getline(is, line);
1322 if (!prefixIs(line, "</row>")) {
1323 lyxerr << "Wrong tabular format (expected </row> got" <<
1324 line << ")" << endl;
1328 while (!prefixIs(line, "</lyxtabular>")) {
1329 l_getline(is, line);
1331 set_row_column_number_info();
1335 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1341 int columns_arg = 0;
1342 int is_long_tabular_arg = false;
1343 int rotate_arg = false;
1352 istream & is = lex.getStream();
1355 version = lyx::atoi(s.substr(8, string::npos));
1359 vector<int> cont_row_info;
1362 lyxerr << "Tabular format < 5 is not supported anymore\n"
1363 "Get an older version of LyX (< 1.1.x) for conversion!"
1365 WriteAlert(_("Warning:"),
1366 _("Tabular format < 5 is not supported anymore\n"),
1367 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1369 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1370 >> rotate_arg >> a >> b >> c >> d;
1372 is >> rows_arg >> columns_arg;
1373 Init(rows_arg, columns_arg);
1374 cont_row_info = vector<int>(rows_arg);
1375 SetLongTabular(is_long_tabular_arg);
1376 SetRotateTabular(rotate_arg);
1378 for (i = 0; i < rows_; ++i) {
1380 cont_row_info[i] = false;
1382 for (i = 0; i < columns_; ++i) {
1385 for (i = 0; i < rows_; ++i) {
1386 for (j = 0; j < columns_; ++j) {
1391 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1392 >> rotate_arg >> a >> b >> c >> d;
1393 Init(rows_arg, columns_arg);
1394 cont_row_info = vector<int>(rows_arg);
1395 SetLongTabular(is_long_tabular_arg);
1396 SetRotateTabular(rotate_arg);
1398 endfirsthead = b + 1;
1400 endlastfoot = d + 1;
1401 for (i = 0; i < rows_; ++i) {
1402 a = b = c = d = e = f = g = 0;
1403 is >> a >> b >> c >> d;
1404 row_info[i].top_line = a;
1405 row_info[i].bottom_line = b;
1406 cont_row_info[i] = c;
1407 row_info[i].newpage = d;
1409 for (i = 0; i < columns_; ++i) {
1414 char ch; // skip '"'
1417 // ignore is buggy but we will use it later (Lgb)
1418 is.ignore(); // skip '"'
1420 getline(is, s1, '"');
1422 is >> ch; // skip '"'
1424 // ignore is buggy but we will use it later (Lgb)
1425 is.ignore(); // skip '"'
1427 getline(is, s2, '"');
1428 column_info[i].alignment = static_cast<LyXAlignment>(a);
1429 column_info[i].left_line = b;
1430 column_info[i].right_line = c;
1431 column_info[i].p_width = s1;
1432 column_info[i].align_special = s2;
1434 for (i = 0; i < rows_; ++i) {
1435 for (j = 0; j < columns_; ++j) {
1438 is >> a >> b >> c >> d >> e >> f >> g;
1441 is >> ch; // skip '"'
1443 // ignore is buggy but we will use it later (Lgb)
1444 is.ignore(); // skip '"'
1446 getline(is, s1, '"');
1448 is >> ch; // skip '"'
1450 // ignore is buggy but we will use it later (Lgb)
1451 is.ignore(); // skip '"'
1453 getline(is, s2, '"');
1454 cell_info[i][j].multicolumn = static_cast<char>(a);
1455 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1456 cell_info[i][j].top_line = static_cast<char>(c);
1457 cell_info[i][j].bottom_line = static_cast<char>(d);
1458 cell_info[i][j].left_line = column_info[j].left_line;
1459 cell_info[i][j].right_line = column_info[j].right_line;
1460 cell_info[i][j].rotate = static_cast<bool>(f);
1461 cell_info[i][j].usebox = static_cast<BoxType>(g);
1462 cell_info[i][j].align_special = s1;
1463 cell_info[i][j].p_width = s2;
1467 set_row_column_number_info(true);
1469 LyXParagraph * par = new LyXParagraph;
1470 LyXParagraph * return_par = 0;
1472 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1473 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1478 LyXFont font(LyXFont::ALL_INHERIT);
1479 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1481 while (lex.IsOK()) {
1483 string const token = lex.GetString();
1486 if (token == "\\layout"
1487 || token == "\\end_float"
1488 || token == "\\end_deeper") {
1489 lex.pushToken(token);
1492 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1504 lex.pushToken(token);
1508 lex.printError("New Paragraph allocated! This should not happen!");
1509 lex.pushToken(token);
1515 // now we have the par we should fill the insets with this!
1517 InsetText * inset = GetCellInset(cell);
1521 for (int i = 0; i < par->Last(); ++i)
1523 for (int i = 0; i < par->size(); ++i)
1526 if (par->IsNewline(i)) {
1528 if (cell > GetNumberOfCells()) {
1529 lyxerr << "Some error in reading old table format occured!" <<
1530 endl << "Terminating when reading cell[" << cell << "]!" <<
1535 row = row_of_cell(cell);
1536 if (cont_row_info[row]) {
1538 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1539 while(!IsFirstCellInRow(--cell));
1541 inset = GetCellInset(cell);
1544 inset = GetCellInset(cell);
1545 row = row_of_cell(cell);
1546 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1548 // insert a space instead
1550 par->InsertChar(i, ' ');
1553 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1555 inset->par->InsertFromMinibuffer(inset->par->Last());
1557 inset->par->InsertFromMinibuffer(inset->par->size());
1565 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1567 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1568 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1572 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1574 int const row = row_of_cell(cell);
1575 int const column = column_of_cell(cell);
1576 return &cell_info[row][column];
1580 void LyXTabular::SetMultiColumn(int cell, int number)
1582 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1583 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1584 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1585 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1586 for (number--; number > 0; --number) {
1587 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1589 set_row_column_number_info();
1593 int LyXTabular::cells_in_multicolumn(int cell) const
1595 int const row = row_of_cell(cell);
1596 int column = column_of_cell(cell);
1599 while ((column < columns_) &&
1600 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1609 int LyXTabular::UnsetMultiColumn(int cell)
1611 int const row = row_of_cell(cell);
1612 int column = column_of_cell(cell);
1616 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1617 cell_info[row][column].multicolumn = CELL_NORMAL;
1619 while ((column < columns_) &&
1620 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1622 cell_info[row][column].multicolumn = CELL_NORMAL;
1627 set_row_column_number_info();
1632 void LyXTabular::SetLongTabular(bool what)
1634 is_long_tabular = what;
1638 bool LyXTabular::IsLongTabular() const
1640 return is_long_tabular;
1644 void LyXTabular::SetRotateTabular(bool flag)
1650 bool LyXTabular::GetRotateTabular() const
1656 void LyXTabular::SetRotateCell(int cell, bool flag)
1658 cellinfo_of_cell(cell)->rotate = flag;
1662 bool LyXTabular::GetRotateCell(int cell) const
1664 return cellinfo_of_cell(cell)->rotate;
1668 bool LyXTabular::NeedRotating() const
1672 for (int i = 0; i < rows_; ++i) {
1673 for (int j = 0; j < columns_; ++j) {
1674 if (cell_info[i][j].rotate)
1682 bool LyXTabular::IsLastCell(int cell) const
1684 if ((cell + 1) < GetNumberOfCells())
1690 int LyXTabular::GetCellAbove(int cell) const
1692 if (row_of_cell(cell) > 0)
1693 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1698 int LyXTabular::GetCellBelow(int cell) const
1700 if (row_of_cell(cell) + 1 < rows_)
1701 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1706 int LyXTabular::GetLastCellAbove(int cell) const
1708 if (row_of_cell(cell) <= 0)
1710 if (!IsMultiColumn(cell))
1711 return GetCellAbove(cell);
1712 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1716 int LyXTabular::GetLastCellBelow(int cell) const
1718 if (row_of_cell(cell) + 1 >= rows_)
1720 if (!IsMultiColumn(cell))
1721 return GetCellBelow(cell);
1722 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1726 int LyXTabular::GetCellNumber(int row, int column) const
1728 if (column >= columns_)
1729 column = columns_ - 1;
1730 else if (column < 0)
1737 return cell_info[row][column].cellno;
1741 void LyXTabular::SetUsebox(int cell, BoxType type)
1743 cellinfo_of_cell(cell)->usebox = type;
1747 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1749 if (column_info[column_of_cell(cell)].p_width.empty() &&
1750 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1752 if (cellinfo_of_cell(cell)->usebox > 1)
1753 return cellinfo_of_cell(cell)->usebox;
1754 return UseParbox(cell);
1758 void LyXTabular::SetLTHead(int cell, bool first)
1760 int const row = row_of_cell(cell);
1761 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1764 if (endfirsthead == val)
1777 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1780 if (abs(endhead) > rows_)
1782 return (row_of_cell(cell) == abs(endhead) - 1);
1786 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1789 if (abs(endfirsthead) > rows_)
1791 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1795 void LyXTabular::SetLTFoot(int cell, bool last)
1797 int const row = row_of_cell(cell);
1798 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1801 if (endlastfoot == val)
1814 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1817 if ((endfoot + 1) > rows_)
1819 return (row_of_cell(cell) == abs(endfoot) - 1);
1823 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1826 if (abs(endlastfoot) > rows_)
1828 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1832 void LyXTabular::SetLTNewPage(int cell, bool what)
1834 row_info[row_of_cell(cell)].newpage = what;
1838 bool LyXTabular::GetLTNewPage(int cell) const
1840 return row_info[row_of_cell(cell)].newpage;
1844 bool LyXTabular::SetAscentOfRow(int row, int height)
1846 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1848 row_info[row].ascent_of_row = height;
1853 bool LyXTabular::SetDescentOfRow(int row, int height)
1855 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1857 row_info[row].descent_of_row = height;
1862 int LyXTabular::GetAscentOfRow(int row) const
1866 return row_info[row].ascent_of_row;
1870 int LyXTabular::GetDescentOfRow(int row) const
1874 return row_info[row].descent_of_row;
1878 int LyXTabular::GetHeightOfTabular() const
1882 for (int row = 0; row < rows_; ++row)
1883 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1884 GetAdditionalHeight(GetCellNumber(row, 0));
1889 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1891 if ((row >= rows_) || (column >= columns_))
1893 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1897 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1899 if ((row < 0) || (row >= rows_))
1902 int const fcell = GetFirstCellInRow(row);
1903 int const n = NumberOfCellsInRow(fcell) + fcell;
1906 for (int i = fcell; i < n; ++i) {
1910 if (tmp == (n - fcell)){
1913 for (int i = fcell; i < n; ++i) {
1916 << column_of_cell(i) + 1
1918 << right_column_of_cell(i) + 1
1930 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1932 if ((row < 0) || (row >= rows_))
1935 int const fcell = GetFirstCellInRow(row);
1936 int const n = NumberOfCellsInRow(fcell) + fcell;
1939 for (int i = fcell; i < n; ++i) {
1943 if (tmp == (n-fcell)){
1946 for (int i = fcell; i < n; ++i) {
1947 if (BottomLine(i)) {
1949 << column_of_cell(i) + 1
1951 << right_column_of_cell(i) + 1
1963 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1967 if (GetRotateCell(cell)) {
1968 os << "\\begin{sideways}\n";
1971 if (IsMultiColumn(cell)) {
1972 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1973 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1974 os << cellinfo_of_cell(cell)->align_special << "}{";
1978 if (!GetPWidth(cell).empty()) {
1979 switch (GetVAlignment(cell)) {
1980 case LYX_VALIGN_TOP:
1983 case LYX_VALIGN_CENTER:
1986 case LYX_VALIGN_BOTTOM:
1990 os << "{" << GetPWidth(cell) << '}';
1992 switch (GetAlignment(cell)) {
1993 case LYX_ALIGN_LEFT:
1996 case LYX_ALIGN_RIGHT:
2004 if (RightLine(cell))
2006 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2012 if (GetUsebox(cell) == BOX_PARBOX) {
2014 switch (GetVAlignment(cell)) {
2015 case LYX_VALIGN_TOP:
2018 case LYX_VALIGN_CENTER:
2021 case LYX_VALIGN_BOTTOM:
2025 os << "]{" << GetPWidth(cell) << "}{";
2026 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2027 os << "\\begin{minipage}[";
2028 switch (GetVAlignment(cell)) {
2029 case LYX_VALIGN_TOP:
2032 case LYX_VALIGN_CENTER:
2035 case LYX_VALIGN_BOTTOM:
2039 os << "]{" << GetPWidth(cell) << "}\n";
2046 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2051 if (GetUsebox(cell) == BOX_PARBOX)
2053 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2054 os << "%\n\\end{minipage}";
2057 if (IsMultiColumn(cell)){
2060 if (GetRotateCell(cell)) {
2061 os << "%\n\\end{sideways}";
2068 int LyXTabular::Latex(Buffer const * buf,
2069 ostream & os, bool fragile, bool fp) const
2074 //+---------------------------------------------------------------------
2075 //+ first the opening preamble +
2076 //+---------------------------------------------------------------------
2079 os << "\\begin{sideways}\n";
2082 if (is_long_tabular)
2083 os << "\\begin{longtable}{";
2085 os << "\\begin{tabular}{";
2086 for (int i = 0; i < columns_; ++i) {
2087 if (column_info[i].left_line)
2089 if (!column_info[i].align_special.empty()) {
2090 os << column_info[i].align_special;
2091 } else if (!column_info[i].p_width.empty()) {
2092 switch (column_info[i].valignment) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_CENTER:
2099 case LYX_VALIGN_BOTTOM:
2104 << column_info[i].p_width
2107 switch (column_info[i].alignment) {
2108 case LYX_ALIGN_LEFT:
2111 case LYX_ALIGN_RIGHT:
2119 if (column_info[i].right_line)
2125 //+---------------------------------------------------------------------
2126 //+ the single row and columns (cells) +
2127 //+---------------------------------------------------------------------
2129 for (int i = 0; i < rows_; ++i) {
2130 ret += TeXTopHLine(os, i);
2132 if (IsLongTabular()) {
2133 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2134 os << "\\endhead\n";
2137 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2138 os << "\\endfirsthead\n";
2141 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2142 os << "\\endfoot\n";
2145 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2146 os << "\\endlastfoot\n";
2151 ret += TeXBottomHLine(os, i-1);
2152 ret += TeXTopHLine(os, i);
2154 for (int j = 0; j < columns_; ++j) {
2155 if (IsPartOfMultiColumn(i,j))
2157 ret += TeXCellPreamble(os, cell);
2158 InsetText * inset = GetCellInset(cell);
2160 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2162 inset->par->Last() > 0 && GetPWidth(cell).empty();
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 GetCellInset(GetCellNumber(row, column));
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());
2562 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2564 LyXParagraph * par = GetCellInset(cell)->par;
2566 for (; par; par = par->next_) {
2567 for (int i = 0; i < par->Last(); ++i) {
2568 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2575 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2577 LyXParagraph * par = GetCellInset(cell)->par;
2579 for (; par; par = par->next()) {
2580 for (int i = 0; i < par->size(); ++i) {
2581 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)