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_, <);
102 #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)
109 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
116 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
118 // If this and lt is not of the same size we have a serious bug
119 // So then it is ok to throw an exception, or for now
121 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
123 cell_info = lt.cell_info;
124 row_info = lt.row_info;
125 column_info = lt.column_info;
127 // long tabular stuff
128 SetLongTabular(lt.is_long_tabular);
129 endhead = lt.endhead;
130 endfoot = lt.endfoot;
131 endfirsthead = lt.endfirsthead;
132 endlastfoot = lt.endlastfoot;
142 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
144 LyXTabular * result = new LyXTabular(inset, *this);
146 // don't know if this is good but I need to Clone also
147 // the text-insets here, this is for the Undo-facility!
148 for (int i = 0; i < rows_; ++i) {
149 for (int j = 0; j < columns_; ++j) {
150 result->cell_info[i][j].inset = cell_info[i][j].inset;
151 result->cell_info[i][j].inset.setOwner(inset);
159 /* activates all lines and sets all widths to 0 */
160 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
163 columns_ = columns_arg;
164 row_info = row_vector(rows_, rowstruct());
165 column_info = column_vector(columns_, columnstruct());
166 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
174 for (int i = 0; i < rows_; ++i) {
175 for (int j = 0; j < columns_; ++j) {
176 cell_info[i][j].inset.setOwner(owner_);
177 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
178 cell_info[i][j].cellno = cellno++;
180 cell_info[i].back().right_line = true;
182 row_info.back().bottom_line = true;
183 row_info.front().bottom_line = true;
185 for (int i = 0; i < columns_; ++i) {
186 calculate_width_of_column(i);
188 column_info.back().right_line = true;
190 calculate_width_of_tabular();
192 rowofcell = vector<int>();
193 columnofcell = vector<int>();
194 set_row_column_number_info();
195 is_long_tabular = false;
204 void LyXTabular::AppendRow(int cell)
208 int row = row_of_cell(cell);
210 row_vector::iterator rit = row_info.begin() + row;
211 row_info.insert(rit, rowstruct());
212 // now set the values of the row before
213 row_info[row] = row_info[row+1];
216 cell_vvector::iterator cit = cell_info.begin() + row;
217 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
219 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
222 for (int i = 0; i <= row; ++i) {
223 for (int j = 0; j < columns_; ++j) {
224 c_info[i][j] = cell_info[i][j];
227 for (int i = row + 1; i < rows_; ++i) {
228 for (int j = 0; j < columns_; ++j) {
229 c_info[i][j] = cell_info[i-1][j];
234 for (int j = 0; j < columns_; ++j) {
235 cell_info[row][j].inset.clear();
242 void LyXTabular::DeleteRow(int row)
244 // Why make it so hard? (Lgb)
247 if (rows_ == 1) return; // Not allowed to delete last row
249 row_info.erase(row_info.begin() + row); //&row_info[row]);
250 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
256 void LyXTabular::AppendColumn(int cell)
260 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
262 int const column = column_of_cell(cell);
263 column_vector::iterator cit = column_info.begin() + column + 1;
264 column_info.insert(cit, columnstruct());
265 // set the column values of the column before
266 column_info[column+1] = column_info[column];
268 for (int i = 0; i < rows_; ++i) {
269 for (int j = 0; j <= column; ++j) {
270 c_info[i][j] = cell_info[i][j];
272 for (int j = column + 1; j < columns_; ++j) {
273 c_info[i][j] = cell_info[i][j - 1];
275 // care about multicolumns
276 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
277 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
279 if ((column + 1) == columns_ ||
280 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
281 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
286 for (int i = 0; i < rows_; ++i) {
287 //cell_info[i][column].inset.clear();
288 cell_info[i][column + 1].inset.clear();
294 void LyXTabular::DeleteColumn(int column)
296 // Similar to DeleteRow
297 //if (!(columns_ - 1))
299 if (columns_ == 1) return; // Not allowed to delete last column
301 column_info.erase(column_info.begin() + column);
302 for (int i = 0; i < rows_; ++i) {
303 cell_info[i].erase(cell_info[i].begin() + column);
310 void LyXTabular::Reinit()
312 for (int i = 0; i < rows_; ++i) {
313 for (int j = 0; j < columns_; ++j) {
314 cell_info[i][j].width_of_cell = 0;
315 cell_info[i][j].inset.setOwner(owner_);
319 for (int i = 0; i < columns_; ++i) {
320 calculate_width_of_column(i);
322 calculate_width_of_tabular();
324 set_row_column_number_info();
328 void LyXTabular::set_row_column_number_info(bool oldformat)
331 for (int row = 0; row < rows_; ++row) {
332 for (int column = 0; column<columns_; ++column) {
333 if (cell_info[row][column].multicolumn
334 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
336 cell_info[row][column].cellno = numberofcells;
339 ++numberofcells; // because this is one more than as we start from 0
341 rowofcell.resize(numberofcells);
342 columnofcell.resize(numberofcells);
344 for (int row = 0, column = 0, c = 0;
345 c < numberofcells && row < rows_ && column < columns_;) {
347 columnofcell[c] = column;
351 } while (column < columns_ &&
352 cell_info[row][column].multicolumn
353 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
354 if (column == columns_) {
360 for (int row = 0; row < rows_; ++row) {
361 for (int column = 0; column < columns_; ++column) {
362 if (IsPartOfMultiColumn(row,column))
364 // now set the right line of multicolumns right for oldformat read
366 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
368 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
369 cell_info[row][column].right_line =
370 cell_info[row][column+cn-1].right_line;
372 cell_info[row][column].inset.SetAutoBreakRows(
373 !GetPWidth(GetCellNumber(row, column)).empty());
379 int LyXTabular::GetNumberOfCells() const
381 return numberofcells;
385 int LyXTabular::NumberOfCellsInRow(int cell) const
387 int const row = row_of_cell(cell);
389 for (int i = 0; i < columns_; ++i) {
390 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
397 /* returns 1 if there is a topline, returns 0 if not */
398 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
400 int const row = row_of_cell(cell);
402 if (!onlycolumn && IsMultiColumn(cell))
403 return cellinfo_of_cell(cell)->top_line;
404 return row_info[row].top_line;
408 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
410 // no bottom line underneath non-existent cells if you please
411 // Isn't that a programming error? Is so this should
412 // be an Assert instead. (Lgb)
413 if (cell >= numberofcells)
416 if (!onlycolumn && IsMultiColumn(cell))
417 return cellinfo_of_cell(cell)->bottom_line;
418 return row_info[row_of_cell(cell)].bottom_line;
422 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
424 if (!onlycolumn && IsMultiColumn(cell))
425 return cellinfo_of_cell(cell)->left_line;
426 return column_info[column_of_cell(cell)].left_line;
430 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
432 if (!onlycolumn && IsMultiColumn(cell))
433 return cellinfo_of_cell(cell)->right_line;
434 return column_info[right_column_of_cell(cell)].right_line;
438 bool LyXTabular::TopAlreadyDrawed(int cell) const
440 int row = row_of_cell(cell);
441 if ((row > 0) && !GetAdditionalHeight(row)) {
442 int column = column_of_cell(cell);
445 && cell_info[row][column].multicolumn
446 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
448 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
449 return row_info[row].bottom_line;
451 return cell_info[row][column].bottom_line;
457 bool LyXTabular::LeftAlreadyDrawed(int cell) const
459 int column = column_of_cell(cell);
461 int row = row_of_cell(cell);
463 (cell_info[row][column].multicolumn ==
464 LyXTabular::CELL_PART_OF_MULTICOLUMN));
465 if (GetAdditionalWidth(cell_info[row][column].cellno))
467 return column_info[column].right_line;
473 bool LyXTabular::IsLastRow(int cell) const
475 return (row_of_cell(cell) == rows_ - 1);
479 int LyXTabular::GetAdditionalHeight(int row) const
481 if (!row || row >= rows_)
487 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
488 switch (cell_info[row - 1][column].multicolumn) {
489 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
490 bottom = cell_info[row - 1][column].bottom_line;
492 case LyXTabular::CELL_NORMAL:
493 bottom = row_info[row - 1].bottom_line;
496 for (int column = 0; column < columns_ - 1 && top; ++column) {
497 switch (cell_info[row][column].multicolumn){
498 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
499 top = cell_info[row][column].top_line;
501 case LyXTabular::CELL_NORMAL:
502 top = row_info[row].top_line;
506 return WIDTH_OF_LINE;
511 int LyXTabular::GetAdditionalWidth(int cell) const
513 // internally already set in SetWidthOfCell
514 // used to get it back in text.C
515 int const col = right_column_of_cell(cell);
516 if (col < columns_ - 1 && column_info[col].right_line &&
517 column_info[col+1].left_line)
518 return WIDTH_OF_LINE;
524 // returns the maximum over all rows
525 int LyXTabular::GetWidthOfColumn(int cell) const
527 int const column1 = column_of_cell(cell);
528 int const column2 = right_column_of_cell(cell);
530 for (int i = column1; i <= column2; ++i) {
531 result += column_info[i].width_of_column;
537 int LyXTabular::GetWidthOfTabular() const
539 return width_of_tabular;
543 /* returns 1 if a complete update is necessary, otherwise 0 */
544 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
546 if (!IsMultiColumn(cell))
549 int const row = row_of_cell(cell);
550 int const column1 = column_of_cell(cell);
551 int const column2 = right_column_of_cell(cell);
553 // first set columns to 0 so we can calculate the right width
554 for (int i = column1; i <= column2; ++i) {
555 cell_info[row][i].width_of_cell = 0;
557 // set the width to MAX_WIDTH until width > 0
558 int width = (new_width + 2 * WIDTH_OF_LINE);
561 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
562 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
563 width -= column_info[i].width_of_column;
566 cell_info[row][i].width_of_cell = width;
572 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
574 int const row = row_of_cell(cell);
575 int const column1 = column_of_cell(cell);
576 int const column2 = right_column_of_cell(cell);
578 // first set columns to 0 so we can calculate the right width
580 for (; i <= column2; ++i)
581 cell_info[row][i].width_of_cell = 0;
582 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
584 if (i < numberofcells)
585 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
586 SetWidthOfMulticolCell(cell, new_width);
590 /* returns 1 if a complete update is necessary, otherwise 0 */
591 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
593 int const row = row_of_cell(cell);
594 int const column1 = column_of_cell(cell);
598 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
600 if (IsMultiColumn(cell, true)) {
601 tmp = SetWidthOfMulticolCell(cell, new_width);
603 width = (new_width + 2*WIDTH_OF_LINE);
604 cell_info[row][column1].width_of_cell = width;
605 if (column_info[column1].right_line && (column1 < columns_-1) &&
606 column_info[column1+1].left_line) // additional width
607 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
608 tmp = calculate_width_of_column_NMC(column1);
612 for (; i<columns_; ++i)
613 calculate_width_of_column_NMC(i);
614 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
616 if (i < numberofcells)
617 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
618 for (i = 0; i < columns_; ++i)
619 calculate_width_of_column(i);
620 calculate_width_of_tabular();
627 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
629 if (!IsMultiColumn(cell) || onlycolumn)
630 column_info[column_of_cell(cell)].alignment = align;
632 cellinfo_of_cell(cell)->alignment = align;
637 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
639 if (!IsMultiColumn(cell) || onlycolumn)
640 column_info[column_of_cell(cell)].valignment = align;
642 cellinfo_of_cell(cell)->valignment = align;
647 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
649 bool flag = !width.empty();
650 int const j = column_of_cell(cell);
652 column_info[j].p_width = width;
653 if (flag) // do this only if there is a width
654 SetAlignment(cell, LYX_ALIGN_LEFT);
655 for (int i = 0; i < rows_; ++i) {
656 int c = GetCellNumber(i, j);
657 flag = !GetPWidth(c).empty(); // because of multicolumns!
658 GetCellInset(c)->SetAutoBreakRows(flag);
664 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
666 bool const flag = !width.empty();
668 cellinfo_of_cell(cell)->p_width = width;
669 if (IsMultiColumn(cell)) {
670 GetCellInset(cell)->SetAutoBreakRows(flag);
677 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
678 LyXTabular::Feature what)
680 if (what == SET_SPECIAL_MULTI)
681 cellinfo_of_cell(cell)->align_special = special;
683 column_info[column_of_cell(cell)].align_special = special;
688 bool LyXTabular::SetAllLines(int cell, bool line)
690 SetTopLine(cell, line);
691 SetBottomLine(cell, line);
692 SetRightLine(cell, line);
693 SetLeftLine(cell, line);
698 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
700 int const row = row_of_cell(cell);
702 if (onlycolumn || !IsMultiColumn(cell))
703 row_info[row].top_line = line;
705 cellinfo_of_cell(cell)->top_line = line;
710 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
712 if (onlycolumn || !IsMultiColumn(cell))
713 row_info[row_of_cell(cell)].bottom_line = line;
715 cellinfo_of_cell(cell)->bottom_line = line;
720 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
722 if (onlycolumn || !IsMultiColumn(cell))
723 column_info[column_of_cell(cell)].left_line = line;
725 cellinfo_of_cell(cell)->left_line = line;
730 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
732 if (onlycolumn || !IsMultiColumn(cell))
733 column_info[right_column_of_cell(cell)].right_line = line;
735 cellinfo_of_cell(cell)->right_line = line;
740 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
742 if (!onlycolumn && IsMultiColumn(cell))
743 return cellinfo_of_cell(cell)->alignment;
745 return column_info[column_of_cell(cell)].alignment;
749 LyXTabular::VAlignment
750 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
752 if (!onlycolumn && IsMultiColumn(cell))
753 return cellinfo_of_cell(cell)->valignment;
755 return column_info[column_of_cell(cell)].valignment;
759 string const LyXTabular::GetPWidth(int cell) const
761 if (IsMultiColumn(cell))
762 return cellinfo_of_cell(cell)->p_width;
763 return column_info[column_of_cell(cell)].p_width;
767 string const LyXTabular::GetColumnPWidth(int cell) const
769 return column_info[column_of_cell(cell)].p_width;
773 string const LyXTabular::GetMColumnPWidth(int cell) const
775 if (IsMultiColumn(cell))
776 return cellinfo_of_cell(cell)->p_width;
781 string const LyXTabular::GetAlignSpecial(int cell, int what) const
783 if (what == SET_SPECIAL_MULTI)
784 return cellinfo_of_cell(cell)->align_special;
785 return column_info[column_of_cell(cell)].align_special;
789 int LyXTabular::GetWidthOfCell(int cell) const
791 int const row = row_of_cell(cell);
792 int const column1 = column_of_cell(cell);
793 int const column2 = right_column_of_cell(cell);
795 for (int i = column1; i <= column2; ++i) {
796 result += cell_info[row][i].width_of_cell;
802 int LyXTabular::GetBeginningOfTextInCell(int cell) const
806 switch (GetAlignment(cell)){
807 case LYX_ALIGN_CENTER:
808 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
810 case LYX_ALIGN_RIGHT:
811 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
812 // + GetAdditionalWidth(cell);
814 default: /* LYX_ALIGN_LEFT: nothing :-) */
824 bool LyXTabular::IsFirstCellInRow(int cell) const
826 return column_of_cell(cell) == 0;
830 int LyXTabular::GetFirstCellInRow(int row) const
834 return cell_info[row][0].cellno;
837 bool LyXTabular::IsLastCellInRow(int cell) const
839 return (right_column_of_cell(cell) == (columns_ - 1));
843 int LyXTabular::GetLastCellInRow(int row) const
847 return cell_info[row][columns_-1].cellno;
851 bool LyXTabular::calculate_width_of_column(int column)
853 int const old_column_width = column_info[column].width_of_column;
856 for (int i = 0; i < rows_; ++i) {
857 maximum = max(cell_info[i][column].width_of_cell, maximum);
859 column_info[column].width_of_column = maximum;
860 return (column_info[column].width_of_column != old_column_width);
865 // calculate the with of the column without regarding REAL MultiColumn
866 // cells. This means MultiColumn-cells spanning more than 1 column.
868 bool LyXTabular::calculate_width_of_column_NMC(int column)
870 int const old_column_width = column_info[column].width_of_column;
872 for (int i = 0; i < rows_; ++i) {
873 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
874 (cell_info[i][column].width_of_cell > max)) {
875 max = cell_info[i][column].width_of_cell;
878 column_info[column].width_of_column = max;
879 return (column_info[column].width_of_column != old_column_width);
883 void LyXTabular::calculate_width_of_tabular()
885 width_of_tabular = 0;
886 for (int i = 0; i < columns_; ++i) {
887 width_of_tabular += column_info[i].width_of_column;
892 int LyXTabular::row_of_cell(int cell) const
894 if (cell >= numberofcells)
898 return rowofcell[cell];
902 int LyXTabular::column_of_cell(int cell) const
904 if (cell >= numberofcells)
908 return columnofcell[cell];
912 int LyXTabular::right_column_of_cell(int cell) const
914 int const row = row_of_cell(cell);
915 int column = column_of_cell(cell);
916 while (column < (columns_ - 1) &&
917 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
923 // Perfect case for a template... (Lgb)
926 string const write_attribute(string const & name, T const & t)
928 string str = " " + name + "=\"" + tostr(t) + "\"";
933 string const write_attribute(string const & name, bool const & b)
935 return write_attribute(name, int(b));
940 string const write_attribute(string const & name, int value)
942 string str = " " + name + "=\"" + tostr(value) + "\"";
947 string const write_attribute(string const & name, string const & value)
949 string str = " " + name + "=\"" + value + "\"";
954 string const write_attribute(string const & name, bool value)
956 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
964 string const tostr(LyXAlignment const & num)
969 case LYX_ALIGN_BLOCK:
973 case LYX_ALIGN_CENTER:
975 case LYX_ALIGN_RIGHT:
977 case LYX_ALIGN_LAYOUT:
979 case LYX_ALIGN_SPECIAL:
988 string const tostr(LyXTabular::VAlignment const & num)
991 case LyXTabular::LYX_VALIGN_TOP:
993 case LyXTabular::LYX_VALIGN_CENTER:
995 case LyXTabular::LYX_VALIGN_BOTTOM:
1004 string const tostr(LyXTabular::BoxType const & num)
1007 case LyXTabular::BOX_NONE:
1009 case LyXTabular::BOX_PARBOX:
1011 case LyXTabular::BOX_MINIPAGE:
1018 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1022 << write_attribute("version", 2)
1023 << write_attribute("rows", rows_)
1024 << write_attribute("columns", columns_)
1026 // global longtable options
1028 << write_attribute("rotate", tostr(rotate))
1029 << write_attribute("islongtable", tostr(is_long_tabular))
1030 << write_attribute("endhead", endhead)
1031 << write_attribute("endfirsthead", endfirsthead)
1032 << write_attribute("endfoot", endfoot)
1033 << write_attribute("endlastfoot", endlastfoot)
1035 for (int j = 0; j < columns_; ++j) {
1037 << write_attribute("alignment", tostr(column_info[j].alignment))
1038 << write_attribute("valignment", tostr(column_info[j].valignment))
1039 << write_attribute("leftline", tostr(column_info[j].left_line))
1040 << write_attribute("rightline", tostr(column_info[j].right_line))
1041 << write_attribute("width",
1042 VSpace(column_info[j].p_width)
1044 << write_attribute("special", column_info[j].align_special)
1047 for (int i = 0; i < rows_; ++i) {
1049 << write_attribute("topline", tostr(row_info[i].top_line))
1050 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1051 << write_attribute("newpage", tostr(row_info[i].newpage))
1053 for (int j = 0; j < columns_; ++j) {
1055 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1056 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1057 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1058 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1059 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1060 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1061 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1062 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1063 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1064 << write_attribute("width", cell_info[i][j].p_width)
1065 << write_attribute("special", cell_info[i][j].align_special)
1067 os << "\\begin_inset ";
1068 cell_info[i][j].inset.Write(buf, os);
1069 os << "\n\\end_inset \n"
1074 os << "</lyxtabular>\n";
1080 // I would have liked a fromstr template a lot better. (Lgb)
1083 bool string2type(string const str, LyXAlignment & num)
1086 num = LYX_ALIGN_NONE;
1087 else if (str == "block")
1088 num = LYX_ALIGN_BLOCK;
1089 else if (str == "left")
1090 num = LYX_ALIGN_LEFT;
1091 else if (str == "center")
1092 num = LYX_ALIGN_CENTER;
1093 else if (str == "right")
1094 num = LYX_ALIGN_RIGHT;
1102 bool string2type(string const str, LyXTabular::VAlignment & num)
1105 num = LyXTabular::LYX_VALIGN_TOP;
1106 else if (str == "center")
1107 num = LyXTabular::LYX_VALIGN_CENTER;
1108 else if (str == "bottom")
1109 num = LyXTabular::LYX_VALIGN_BOTTOM;
1117 bool string2type(string const str, LyXTabular::BoxType & num)
1120 num = LyXTabular::BOX_NONE;
1121 else if (str == "parbox")
1122 num = LyXTabular::BOX_PARBOX;
1123 else if (str == "minipage")
1124 num = LyXTabular::BOX_MINIPAGE;
1132 bool string2type(string const str, bool & num)
1136 else if (str == "false")
1144 bool getTokenValue(string const & str, const char * token, string & ret)
1146 size_t token_length = strlen(token);
1147 string::size_type pos = str.find(token);
1149 if (pos == string::npos || pos+token_length+1 >= str.length()
1150 || str[pos+token_length] != '=')
1153 pos += token_length + 1;
1155 if ((ch != '"') && (ch != '\'')) { // only read till next space
1159 while((pos < str.length() - 1) && (str[++pos] != ch))
1166 bool getTokenValue(string const & str, const char * token, int & num)
1169 if (!getTokenValue(str, token, tmp))
1171 num = strToInt(tmp);
1176 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1179 if (!getTokenValue(str, token, tmp))
1181 return string2type(tmp, num);
1185 bool getTokenValue(string const & str, const char * token,
1186 LyXTabular::VAlignment & num)
1189 if (!getTokenValue(str, token, tmp))
1191 return string2type(tmp, num);
1195 bool getTokenValue(string const & str, const char * token,
1196 LyXTabular::BoxType & num)
1199 if (!getTokenValue(str, token, tmp))
1201 return string2type(tmp, num);
1205 bool getTokenValue(string const & str, const char * token, bool & flag)
1208 if (!getTokenValue(str, token, tmp))
1210 return string2type(tmp, flag);
1215 void l_getline(istream & is, string & str)
1218 while (str.empty()) {
1220 if (!str.empty() && str[str.length() - 1] == '\r')
1221 str.erase(str.length() - 1);
1228 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1231 istream & is = lex.getStream();
1233 l_getline(is, line);
1234 if (!prefixIs(line, "<lyxtabular ")
1235 && !prefixIs(line, "<LyXTabular ")) {
1236 OldFormatRead(lex, line);
1241 if (!getTokenValue(line, "version", version))
1244 ReadOld(buf, is, lex, line);
1245 else if (version == 2)
1246 ReadNew(buf, is, lex, line);
1250 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1251 LyXLex & lex, string const & l)
1255 if (!getTokenValue(line, "rows", rows_arg))
1258 if (!getTokenValue(line, "columns", columns_arg))
1260 Init(rows_arg, columns_arg);
1261 l_getline(is, line);
1262 if (!prefixIs(line, "<features")) {
1263 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1264 line << ")" << endl;
1267 getTokenValue(line, "rotate", rotate);
1268 getTokenValue(line, "islongtable", is_long_tabular);
1269 getTokenValue(line, "endhead", endhead);
1270 getTokenValue(line, "endfirsthead", endfirsthead);
1271 getTokenValue(line, "endfoot", endfoot);
1272 getTokenValue(line, "endlastfoot", endlastfoot);
1274 for (int j = 0; j < columns_; ++j) {
1276 if (!prefixIs(line,"<column")) {
1277 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1278 line << ")" << endl;
1281 getTokenValue(line, "alignment", column_info[j].alignment);
1282 getTokenValue(line, "valignment", column_info[j].valignment);
1283 getTokenValue(line, "leftline", column_info[j].left_line);
1284 getTokenValue(line, "rightline", column_info[j].right_line);
1285 getTokenValue(line, "width", column_info[j].p_width);
1286 getTokenValue(line, "special", column_info[j].align_special);
1289 for (int i = 0; i < rows_; ++i) {
1290 l_getline(is, line);
1291 if (!prefixIs(line, "<row")) {
1292 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1293 line << ")" << endl;
1296 getTokenValue(line, "topline", row_info[i].top_line);
1297 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1298 getTokenValue(line, "newpage", row_info[i].newpage);
1299 for (int j = 0; j < columns_; ++j) {
1300 l_getline(is, line);
1301 if (!prefixIs(line, "<cell")) {
1302 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1303 line << ")" << endl;
1306 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1307 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1308 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1309 getTokenValue(line, "topline", cell_info[i][j].top_line);
1310 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1311 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1312 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1313 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1314 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1315 getTokenValue(line, "width", cell_info[i][j].p_width);
1316 getTokenValue(line, "special", cell_info[i][j].align_special);
1317 l_getline(is, line);
1318 if (prefixIs(line, "\\begin_inset")) {
1319 cell_info[i][j].inset.Read(buf, lex);
1320 l_getline(is, line);
1322 if (!prefixIs(line, "</cell>")) {
1323 lyxerr << "Wrong tabular format (expected </cell> got" <<
1324 line << ")" << endl;
1328 l_getline(is, line);
1329 if (!prefixIs(line, "</row>")) {
1330 lyxerr << "Wrong tabular format (expected </row> got" <<
1331 line << ")" << endl;
1335 while (!prefixIs(line, "</lyxtabular>")) {
1336 l_getline(is, line);
1338 set_row_column_number_info();
1342 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1348 int columns_arg = 0;
1349 int is_long_tabular_arg = false;
1350 int rotate_arg = false;
1359 istream & is = lex.getStream();
1362 version = lyx::atoi(s.substr(8, string::npos));
1366 vector<int> cont_row_info;
1369 lyxerr << "Tabular format < 5 is not supported anymore\n"
1370 "Get an older version of LyX (< 1.1.x) for conversion!"
1372 WriteAlert(_("Warning:"),
1373 _("Tabular format < 5 is not supported anymore\n"),
1374 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1376 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1377 >> rotate_arg >> a >> b >> c >> d;
1379 is >> rows_arg >> columns_arg;
1380 Init(rows_arg, columns_arg);
1381 cont_row_info = vector<int>(rows_arg);
1382 SetLongTabular(is_long_tabular_arg);
1383 SetRotateTabular(rotate_arg);
1385 for (i = 0; i < rows_; ++i) {
1387 cont_row_info[i] = false;
1389 for (i = 0; i < columns_; ++i) {
1392 for (i = 0; i < rows_; ++i) {
1393 for (j = 0; j < columns_; ++j) {
1398 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1399 >> rotate_arg >> a >> b >> c >> d;
1400 Init(rows_arg, columns_arg);
1401 cont_row_info = vector<int>(rows_arg);
1402 SetLongTabular(is_long_tabular_arg);
1403 SetRotateTabular(rotate_arg);
1405 endfirsthead = b + 1;
1407 endlastfoot = d + 1;
1408 for (i = 0; i < rows_; ++i) {
1409 a = b = c = d = e = f = g = 0;
1410 is >> a >> b >> c >> d;
1411 row_info[i].top_line = a;
1412 row_info[i].bottom_line = b;
1413 cont_row_info[i] = c;
1414 row_info[i].newpage = d;
1416 for (i = 0; i < columns_; ++i) {
1421 char ch; // skip '"'
1424 // ignore is buggy but we will use it later (Lgb)
1425 is.ignore(); // skip '"'
1427 getline(is, s1, '"');
1429 is >> ch; // skip '"'
1431 // ignore is buggy but we will use it later (Lgb)
1432 is.ignore(); // skip '"'
1434 getline(is, s2, '"');
1435 column_info[i].alignment = static_cast<LyXAlignment>(a);
1436 column_info[i].left_line = b;
1437 column_info[i].right_line = c;
1438 column_info[i].p_width = s1;
1439 column_info[i].align_special = s2;
1441 for (i = 0; i < rows_; ++i) {
1442 for (j = 0; j < columns_; ++j) {
1445 is >> a >> b >> c >> d >> e >> f >> g;
1448 is >> ch; // skip '"'
1450 // ignore is buggy but we will use it later (Lgb)
1451 is.ignore(); // skip '"'
1453 getline(is, s1, '"');
1455 is >> ch; // skip '"'
1457 // ignore is buggy but we will use it later (Lgb)
1458 is.ignore(); // skip '"'
1460 getline(is, s2, '"');
1461 cell_info[i][j].multicolumn = static_cast<char>(a);
1462 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1463 cell_info[i][j].top_line = static_cast<char>(c);
1464 cell_info[i][j].bottom_line = static_cast<char>(d);
1465 cell_info[i][j].left_line = column_info[j].left_line;
1466 cell_info[i][j].right_line = column_info[j].right_line;
1467 cell_info[i][j].rotate = static_cast<bool>(f);
1468 cell_info[i][j].usebox = static_cast<BoxType>(g);
1469 cell_info[i][j].align_special = s1;
1470 cell_info[i][j].p_width = s2;
1474 set_row_column_number_info(true);
1476 LyXParagraph * par = new LyXParagraph;
1477 LyXParagraph * return_par = 0;
1479 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1480 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1485 LyXFont font(LyXFont::ALL_INHERIT);
1486 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1488 while (lex.IsOK()) {
1490 string const token = lex.GetString();
1493 if (token == "\\layout"
1494 || token == "\\end_float"
1495 || token == "\\end_deeper") {
1496 lex.pushToken(token);
1499 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);
1528 for (int i = 0; i < par->Last(); ++i)
1530 for (int i = 0; i < par->size(); ++i)
1533 if (par->IsNewline(i)) {
1535 if (cell > GetNumberOfCells()) {
1536 lyxerr << "Some error in reading old table format occured!" <<
1537 endl << "Terminating when reading cell[" << cell << "]!" <<
1542 row = row_of_cell(cell);
1543 if (cont_row_info[row]) {
1545 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1546 while(!IsFirstCellInRow(--cell));
1548 inset = GetCellInset(cell);
1551 inset = GetCellInset(cell);
1552 row = row_of_cell(cell);
1553 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1555 // insert a space instead
1557 par->InsertChar(i, ' ');
1560 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1562 inset->par->InsertFromMinibuffer(inset->par->Last());
1564 inset->par->InsertFromMinibuffer(inset->par->size());
1572 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1574 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1575 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1579 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1581 int const row = row_of_cell(cell);
1582 int const column = column_of_cell(cell);
1583 return &cell_info[row][column];
1587 void LyXTabular::SetMultiColumn(int cell, int number)
1589 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1590 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1591 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1592 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1593 for (number--; number > 0; --number) {
1594 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1596 set_row_column_number_info();
1600 int LyXTabular::cells_in_multicolumn(int cell) const
1602 int const row = row_of_cell(cell);
1603 int column = column_of_cell(cell);
1606 while ((column < columns_) &&
1607 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1616 int LyXTabular::UnsetMultiColumn(int cell)
1618 int const row = row_of_cell(cell);
1619 int column = column_of_cell(cell);
1623 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1624 cell_info[row][column].multicolumn = CELL_NORMAL;
1626 while ((column < columns_) &&
1627 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1629 cell_info[row][column].multicolumn = CELL_NORMAL;
1634 set_row_column_number_info();
1639 void LyXTabular::SetLongTabular(bool what)
1641 is_long_tabular = what;
1645 bool LyXTabular::IsLongTabular() const
1647 return is_long_tabular;
1651 void LyXTabular::SetRotateTabular(bool flag)
1657 bool LyXTabular::GetRotateTabular() const
1663 void LyXTabular::SetRotateCell(int cell, bool flag)
1665 cellinfo_of_cell(cell)->rotate = flag;
1669 bool LyXTabular::GetRotateCell(int cell) const
1671 return cellinfo_of_cell(cell)->rotate;
1675 bool LyXTabular::NeedRotating() const
1679 for (int i = 0; i < rows_; ++i) {
1680 for (int j = 0; j < columns_; ++j) {
1681 if (cell_info[i][j].rotate)
1689 bool LyXTabular::IsLastCell(int cell) const
1691 if ((cell + 1) < GetNumberOfCells())
1697 int LyXTabular::GetCellAbove(int cell) const
1699 if (row_of_cell(cell) > 0)
1700 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1705 int LyXTabular::GetCellBelow(int cell) const
1707 if (row_of_cell(cell) + 1 < rows_)
1708 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1713 int LyXTabular::GetLastCellAbove(int cell) const
1715 if (row_of_cell(cell) <= 0)
1717 if (!IsMultiColumn(cell))
1718 return GetCellAbove(cell);
1719 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1723 int LyXTabular::GetLastCellBelow(int cell) const
1725 if (row_of_cell(cell) + 1 >= rows_)
1727 if (!IsMultiColumn(cell))
1728 return GetCellBelow(cell);
1729 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1733 int LyXTabular::GetCellNumber(int row, int column) const
1735 if (column >= columns_)
1736 column = columns_ - 1;
1737 else if (column < 0)
1744 return cell_info[row][column].cellno;
1748 void LyXTabular::SetUsebox(int cell, BoxType type)
1750 cellinfo_of_cell(cell)->usebox = type;
1754 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1756 if (column_info[column_of_cell(cell)].p_width.empty() &&
1757 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1759 if (cellinfo_of_cell(cell)->usebox > 1)
1760 return cellinfo_of_cell(cell)->usebox;
1761 return UseParbox(cell);
1765 void LyXTabular::SetLTHead(int cell, bool first)
1767 int const row = row_of_cell(cell);
1768 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1771 if (endfirsthead == val)
1784 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1787 if (abs(endhead) > rows_)
1789 return (row_of_cell(cell) == abs(endhead) - 1);
1793 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1796 if (abs(endfirsthead) > rows_)
1798 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1802 void LyXTabular::SetLTFoot(int cell, bool last)
1804 int const row = row_of_cell(cell);
1805 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1808 if (endlastfoot == val)
1821 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1824 if ((endfoot + 1) > rows_)
1826 return (row_of_cell(cell) == abs(endfoot) - 1);
1830 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1833 if (abs(endlastfoot) > rows_)
1835 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1839 void LyXTabular::SetLTNewPage(int cell, bool what)
1841 row_info[row_of_cell(cell)].newpage = what;
1845 bool LyXTabular::GetLTNewPage(int cell) const
1847 return row_info[row_of_cell(cell)].newpage;
1851 bool LyXTabular::SetAscentOfRow(int row, int height)
1853 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1855 row_info[row].ascent_of_row = height;
1860 bool LyXTabular::SetDescentOfRow(int row, int height)
1862 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1864 row_info[row].descent_of_row = height;
1869 int LyXTabular::GetAscentOfRow(int row) const
1873 return row_info[row].ascent_of_row;
1877 int LyXTabular::GetDescentOfRow(int row) const
1881 return row_info[row].descent_of_row;
1885 int LyXTabular::GetHeightOfTabular() const
1889 for (int row = 0; row < rows_; ++row)
1890 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1891 GetAdditionalHeight(row);
1896 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1898 if ((row >= rows_) || (column >= columns_))
1900 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1904 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1906 if ((row < 0) || (row >= rows_))
1909 int const fcell = GetFirstCellInRow(row);
1910 int const n = NumberOfCellsInRow(fcell) + fcell;
1913 for (int i = fcell; i < n; ++i) {
1917 if (tmp == (n - fcell)){
1920 for (int i = fcell; i < n; ++i) {
1923 << column_of_cell(i) + 1
1925 << right_column_of_cell(i) + 1
1937 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1939 if ((row < 0) || (row >= rows_))
1942 int const fcell = GetFirstCellInRow(row);
1943 int const n = NumberOfCellsInRow(fcell) + fcell;
1946 for (int i = fcell; i < n; ++i) {
1950 if (tmp == (n-fcell)){
1953 for (int i = fcell; i < n; ++i) {
1954 if (BottomLine(i)) {
1956 << column_of_cell(i) + 1
1958 << right_column_of_cell(i) + 1
1970 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1974 if (GetRotateCell(cell)) {
1975 os << "\\begin{sideways}\n";
1978 if (IsMultiColumn(cell)) {
1979 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1980 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1981 os << cellinfo_of_cell(cell)->align_special << "}{";
1985 if (!GetPWidth(cell).empty()) {
1986 switch (GetVAlignment(cell)) {
1987 case LYX_VALIGN_TOP:
1990 case LYX_VALIGN_CENTER:
1993 case LYX_VALIGN_BOTTOM:
1997 os << "{" << GetPWidth(cell) << '}';
1999 switch (GetAlignment(cell)) {
2000 case LYX_ALIGN_LEFT:
2003 case LYX_ALIGN_RIGHT:
2011 if (RightLine(cell))
2013 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2019 if (GetUsebox(cell) == BOX_PARBOX) {
2021 switch (GetVAlignment(cell)) {
2022 case LYX_VALIGN_TOP:
2025 case LYX_VALIGN_CENTER:
2028 case LYX_VALIGN_BOTTOM:
2032 os << "]{" << GetPWidth(cell) << "}{";
2033 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2034 os << "\\begin{minipage}[";
2035 switch (GetVAlignment(cell)) {
2036 case LYX_VALIGN_TOP:
2039 case LYX_VALIGN_CENTER:
2042 case LYX_VALIGN_BOTTOM:
2046 os << "]{" << GetPWidth(cell) << "}\n";
2053 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2058 if (GetUsebox(cell) == BOX_PARBOX)
2060 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2061 os << "%\n\\end{minipage}";
2064 if (IsMultiColumn(cell)){
2067 if (GetRotateCell(cell)) {
2068 os << "%\n\\end{sideways}";
2075 int LyXTabular::Latex(Buffer const * buf,
2076 ostream & os, bool fragile, bool fp) const
2081 //+---------------------------------------------------------------------
2082 //+ first the opening preamble +
2083 //+---------------------------------------------------------------------
2086 os << "\\begin{sideways}\n";
2089 if (is_long_tabular)
2090 os << "\\begin{longtable}{";
2092 os << "\\begin{tabular}{";
2093 for (int i = 0; i < columns_; ++i) {
2094 if (column_info[i].left_line)
2096 if (!column_info[i].align_special.empty()) {
2097 os << column_info[i].align_special;
2098 } else if (!column_info[i].p_width.empty()) {
2099 switch (column_info[i].valignment) {
2100 case LYX_VALIGN_TOP:
2103 case LYX_VALIGN_CENTER:
2106 case LYX_VALIGN_BOTTOM:
2111 << column_info[i].p_width
2114 switch (column_info[i].alignment) {
2115 case LYX_ALIGN_LEFT:
2118 case LYX_ALIGN_RIGHT:
2126 if (column_info[i].right_line)
2132 //+---------------------------------------------------------------------
2133 //+ the single row and columns (cells) +
2134 //+---------------------------------------------------------------------
2136 for (int i = 0; i < rows_; ++i) {
2137 ret += TeXTopHLine(os, i);
2139 if (IsLongTabular()) {
2140 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2141 os << "\\endhead\n";
2144 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2145 os << "\\endfirsthead\n";
2148 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2149 os << "\\endfoot\n";
2152 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2153 os << "\\endlastfoot\n";
2158 ret += TeXBottomHLine(os, i-1);
2159 ret += TeXTopHLine(os, i);
2161 for (int j = 0; j < columns_; ++j) {
2162 if (IsPartOfMultiColumn(i,j))
2164 ret += TeXCellPreamble(os, cell);
2165 InsetText * inset = GetCellInset(cell);
2167 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2169 inset->par->Last() > 0 && GetPWidth(cell).empty();
2171 inset->par->size() > 0 && GetPWidth(cell).empty();
2175 ret += inset->Latex(buf, os, fragile, fp);
2179 ret += TeXCellPostamble(os, cell);
2180 if (!IsLastCellInRow(cell)) { // not last cell in row
2187 ret += TeXBottomHLine(os, i);
2189 if (IsLongTabular()) {
2190 if ((endhead > 0) && (i == (endhead - 1))) {
2191 os << "\\endhead\n";
2194 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2195 os << "\\endfirsthead\n";
2198 if ((endfoot > 0) && (i == (endfoot - 1))) {
2199 os << "\\endfoot\n";
2202 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2203 os << "\\endlastfoot\n";
2207 // ret += TeXBottomHLine(os, i);
2208 if (row_info[i].newpage) {
2209 os << "\\newpage\n";
2215 //+---------------------------------------------------------------------
2216 //+ the closing of the tabular +
2217 //+---------------------------------------------------------------------
2219 if (is_long_tabular)
2220 os << "\\end{longtable}";
2222 os << "\\end{tabular}";
2224 os << "\n\\end{sideways}";
2232 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2236 //+---------------------------------------------------------------------
2237 //+ first the opening preamble +
2238 //+---------------------------------------------------------------------
2240 os << "<tgroup cols=\"" << columns_
2241 << "\" colsep=\"1\" rowsep=\"1\">\n";
2243 for (int i = 0; i < columns_; ++i) {
2244 os << "<colspec colname=\"col" << i << "\" align=\"";
2245 switch (column_info[i].alignment) {
2246 case LYX_ALIGN_LEFT:
2249 case LYX_ALIGN_RIGHT:
2260 //+---------------------------------------------------------------------
2261 //+ the single row and columns (cells) +
2262 //+---------------------------------------------------------------------
2266 for (int i = 0; i < rows_; ++i) {
2268 for (int j = 0; j < columns_; ++j) {
2269 if (IsPartOfMultiColumn(i, j))
2272 os << "<entry align=\"";
2273 switch (GetAlignment(cell)) {
2274 case LYX_ALIGN_LEFT:
2277 case LYX_ALIGN_RIGHT:
2285 os << "\" valign=\"";
2286 switch (GetVAlignment(cell)) {
2287 case LYX_VALIGN_TOP:
2290 case LYX_VALIGN_BOTTOM:
2293 case LYX_VALIGN_CENTER:
2298 if (IsMultiColumn(cell)) {
2299 os << " namest=\"col" << j << "\" ";
2300 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2304 ret += GetCellInset(cell)->DocBook(buf, os);
2311 //+---------------------------------------------------------------------
2312 //+ the closing of the tabular +
2313 //+---------------------------------------------------------------------
2325 void print_n_chars(ostream & os, unsigned char ch, int n)
2327 os << string(n, ch);
2333 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2334 vector<unsigned int> const & clen) const
2336 int const fcell = GetFirstCellInRow(row);
2337 int const n = NumberOfCellsInRow(fcell) + fcell;
2340 for (int i = fcell; i < n; ++i) {
2350 for (int i = fcell; i < n; ++i) {
2361 int column = column_of_cell(i);
2362 int len = clen[column];
2363 while(IsPartOfMultiColumn(row, ++column))
2364 len += clen[column] + 4;
2365 print_n_chars(os, ch, len);
2380 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2381 vector<unsigned int> const & clen) const
2383 int const fcell = GetFirstCellInRow(row);
2384 int const n = NumberOfCellsInRow(fcell) + fcell;
2387 for (int i = fcell; i < n; ++i) {
2388 if (BottomLine(i)) {
2397 for (int i = fcell; i < n; ++i) {
2398 if (BottomLine(i)) {
2408 int column = column_of_cell(i);
2409 int len = clen[column];
2410 while(IsPartOfMultiColumn(row, ++column))
2411 len += clen[column] + 4;
2412 print_n_chars(os, ch, len);
2413 if (BottomLine(i)) {
2427 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2428 int cell, int row, int column,
2429 vector<unsigned int> const & clen) const
2432 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2439 unsigned int len1 = sstr.str().length();
2440 unsigned int len2 = clen[column];
2441 while(IsPartOfMultiColumn(row, ++column))
2442 len2 += clen[column] + 4;
2445 switch (GetAlignment(cell)) {
2447 case LYX_ALIGN_LEFT:
2450 case LYX_ALIGN_RIGHT:
2454 case LYX_ALIGN_CENTER:
2460 for (unsigned int i = 0; i < len1; ++i)
2463 for (unsigned int i = 0; i < len2; ++i)
2465 if (RightLine(cell))
2474 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2478 //+---------------------------------------------------------------------
2479 //+ first calculate the width of the single columns +
2480 //+---------------------------------------------------------------------
2481 vector<unsigned int> clen(columns_);
2483 // first all non (real) multicolumn cells!
2484 for (int j = 0; j < columns_; ++j) {
2486 for (int i = 0; i < rows_; ++i) {
2487 int cell = GetCellNumber(i, j);
2488 if (IsMultiColumn(cell, true))
2491 GetCellInset(cell)->Ascii(buf, sstr, 0);
2492 if (clen[j] < sstr.str().length())
2493 clen[j] = sstr.str().length();
2496 // then all (real) multicolumn cells!
2497 for (int j = 0; j < columns_; ++j) {
2498 for (int i = 0; i < rows_; ++i) {
2499 int cell = GetCellNumber(i, j);
2500 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2503 GetCellInset(cell)->Ascii(buf, sstr, 0);
2504 int len = int(sstr.str().length());
2505 int const n = cells_in_multicolumn(cell);
2506 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2508 if (len > int(clen[j + n - 1]))
2509 clen[j + n - 1] = len;
2513 for (int i = 0; i < rows_; ++i) {
2514 AsciiTopHLine(os, i, clen);
2515 for (int j = 0; j < columns_; ++j) {
2516 if (IsPartOfMultiColumn(i,j))
2518 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2522 AsciiBottomHLine(os, i, clen);
2528 InsetText * LyXTabular::GetCellInset(int cell) const
2530 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2534 InsetText * LyXTabular::GetCellInset(int row, int column) const
2536 return GetCellInset(GetCellNumber(row, column));
2540 void LyXTabular::Validate(LaTeXFeatures & features) const
2542 if (IsLongTabular())
2543 features.longtable = true;
2545 features.rotating = true;
2546 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2547 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2548 features.array = true;
2549 GetCellInset(cell)->Validate(features);
2554 std::vector<string> const LyXTabular::getLabelList() const
2556 std::vector<string> label_list;
2557 for (int i = 0; i < rows_; ++i)
2558 for (int j = 0; j < columns_; ++j) {
2559 std::vector<string> const l =
2560 GetCellInset(i, j)->getLabelList();
2561 label_list.insert(label_list.end(),
2562 l.begin(), l.end());
2569 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2571 LyXParagraph * par = GetCellInset(cell)->par;
2573 for (; par; par = par->next_) {
2574 for (int i = 0; i < par->Last(); ++i) {
2575 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2582 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2584 LyXParagraph * par = GetCellInset(cell)->par;
2586 for (; par; par = par->next()) {
2587 for (int i = 0; i < par->size(); ++i) {
2588 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)