1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
26 #include "lyx_gui_misc.h"
28 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
43 static int const WIDTH_OF_LINE = 5;
45 /// Define a few methods for the inner structs
47 LyXTabular::cellstruct::cellstruct()
51 multicolumn = LyXTabular::CELL_NORMAL;
52 alignment = LYX_ALIGN_CENTER;
53 valignment = LYX_VALIGN_TOP;
63 LyXTabular::rowstruct::rowstruct()
73 LyXTabular::columnstruct::columnstruct()
77 alignment = LYX_ALIGN_CENTER;
78 valignment = LYX_VALIGN_TOP;
84 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
87 Init(rows_arg, columns_arg);
91 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
94 Init(lt.rows_, lt.columns_);
96 #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)
102 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
109 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
111 // If this and lt is not of the same size we have a serious bug
112 // So then it is ok to throw an exception, or for now
114 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
116 cell_info = lt.cell_info;
117 row_info = lt.row_info;
118 column_info = lt.column_info;
120 // long tabular stuff
121 SetLongTabular(lt.is_long_tabular);
122 endhead = lt.endhead;
123 endfoot = lt.endfoot;
124 endfirsthead = lt.endfirsthead;
125 endlastfoot = lt.endlastfoot;
135 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
137 LyXTabular * result = new LyXTabular(inset, *this);
138 // don't know if this is good but I need to Clone also
139 // the text-insets here, this is for the Undo-facility!
140 for (int i = 0; i < rows_; ++i) {
141 for (int j = 0; j < columns_; ++j) {
142 result->cell_info[i][j].inset = cell_info[i][j].inset;
143 result->cell_info[i][j].inset.setOwner(inset);
150 /* activates all lines and sets all widths to 0 */
151 void LyXTabular::Init(int rows_arg, int columns_arg)
154 columns_ = columns_arg;
155 row_info = row_vector(rows_, rowstruct());
156 column_info = column_vector(columns_, columnstruct());
157 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
160 for (int i = 0; i < rows_; ++i) {
161 for (int j = 0; j < columns_; ++j) {
162 cell_info[i][j].inset.setOwner(owner_);
163 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
164 cell_info[i][j].cellno = cellno++;
166 cell_info[i].back().right_line = true;
168 row_info.back().bottom_line = true;
169 row_info.front().bottom_line = true;
171 for (int i = 0; i < columns_; ++i) {
172 calculate_width_of_column(i);
174 column_info.back().right_line = true;
176 calculate_width_of_tabular();
178 rowofcell = vector<int>();
179 columnofcell = vector<int>();
180 set_row_column_number_info();
181 is_long_tabular = false;
190 void LyXTabular::AppendRow(int cell)
194 int row = row_of_cell(cell);
196 row_vector::iterator rit = row_info.begin() + row;
197 row_info.insert(rit, rowstruct());
198 // now set the values of the row before
199 row_info[row] = row_info[row+1];
202 cell_vvector::iterator cit = cell_info.begin() + row;
203 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
205 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
208 for (int i = 0; i <= row; ++i) {
209 for (int j = 0; j < columns_; ++j) {
210 c_info[i][j] = cell_info[i][j];
213 for (int i = row + 1; i < rows_; ++i) {
214 for (int j = 0; j < columns_; ++j) {
215 c_info[i][j] = cell_info[i-1][j];
220 for (int j = 0; j < columns_; ++j) {
221 cell_info[row][j].inset.clear();
228 void LyXTabular::DeleteRow(int row)
230 // Why make it so hard? (Lgb)
233 if (rows_ == 1) return; // Not allowed to delete last row
235 row_info.erase(row_info.begin() + row); //&row_info[row]);
236 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
242 void LyXTabular::AppendColumn(int cell)
246 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
248 int const column = column_of_cell(cell);
249 column_vector::iterator cit = column_info.begin() + column + 1;
250 column_info.insert(cit, columnstruct());
251 // set the column values of the column before
252 column_info[column+1] = column_info[column];
254 for (int i = 0; i < rows_; ++i) {
255 for (int j = 0; j <= column; ++j) {
256 c_info[i][j] = cell_info[i][j];
258 for (int j = column + 1; j < columns_; ++j) {
259 c_info[i][j] = cell_info[i][j - 1];
261 // care about multicolumns
262 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
263 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
265 if ((column + 1) == columns_ ||
266 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
267 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
272 for (int i = 0; i < rows_; ++i) {
273 //cell_info[i][column].inset.clear();
274 cell_info[i][column + 1].inset.clear();
280 void LyXTabular::DeleteColumn(int column)
282 // Similar to DeleteRow
283 //if (!(columns_ - 1))
285 if (columns_ == 1) return; // Not allowed to delete last column
287 column_info.erase(column_info.begin() + column);
288 for (int i = 0; i < rows_; ++i) {
289 cell_info[i].erase(cell_info[i].begin() + column);
296 void LyXTabular::Reinit()
298 for (int i = 0; i < rows_; ++i) {
299 for (int j = 0; j < columns_; ++j) {
300 cell_info[i][j].width_of_cell = 0;
301 cell_info[i][j].inset.setOwner(owner_);
305 for (int i = 0; i < columns_; ++i) {
306 calculate_width_of_column(i);
308 calculate_width_of_tabular();
310 set_row_column_number_info();
314 void LyXTabular::set_row_column_number_info(bool oldformat)
317 for (int row = 0; row < rows_; ++row) {
318 for (int column = 0; column<columns_; ++column) {
319 if (cell_info[row][column].multicolumn
320 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
322 cell_info[row][column].cellno = numberofcells;
325 ++numberofcells; // because this is one more than as we start from 0
327 rowofcell.resize(numberofcells);
328 columnofcell.resize(numberofcells);
330 for (int row = 0, column = 0, c = 0;
331 c < numberofcells && row < rows_ && column < columns_;) {
333 columnofcell[c] = column;
337 } while (column < columns_ &&
338 cell_info[row][column].multicolumn
339 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
340 if (column == columns_) {
346 for (int row = 0; row < rows_; ++row) {
347 for (int column = 0; column < columns_; ++column) {
348 if (IsPartOfMultiColumn(row,column))
350 // now set the right line of multicolumns right for oldformat read
352 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
354 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
355 cell_info[row][column].right_line =
356 cell_info[row][column+cn-1].right_line;
358 cell_info[row][column].inset.SetAutoBreakRows(
359 !GetPWidth(GetCellNumber(row, column)).empty());
365 int LyXTabular::GetNumberOfCells() const
367 return numberofcells;
371 int LyXTabular::NumberOfCellsInRow(int cell) const
373 int const row = row_of_cell(cell);
375 for (int i = 0; i < columns_; ++i) {
376 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
383 /* returns 1 if there is a topline, returns 0 if not */
384 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
386 int const row = row_of_cell(cell);
388 if (!onlycolumn && IsMultiColumn(cell))
389 return cellinfo_of_cell(cell)->top_line;
390 return row_info[row].top_line;
394 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
396 // no bottom line underneath non-existent cells if you please
397 // Isn't that a programming error? Is so this should
398 // be an Assert instead. (Lgb)
399 if (cell >= numberofcells)
402 if (!onlycolumn && IsMultiColumn(cell))
403 return cellinfo_of_cell(cell)->bottom_line;
404 return row_info[row_of_cell(cell)].bottom_line;
408 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
410 if (!onlycolumn && IsMultiColumn(cell))
411 return cellinfo_of_cell(cell)->left_line;
412 return column_info[column_of_cell(cell)].left_line;
416 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
418 if (!onlycolumn && IsMultiColumn(cell))
419 return cellinfo_of_cell(cell)->right_line;
420 return column_info[right_column_of_cell(cell)].right_line;
424 bool LyXTabular::TopAlreadyDrawed(int cell) const
426 if (GetAdditionalHeight(cell))
428 int row = row_of_cell(cell);
430 int column = column_of_cell(cell);
433 && cell_info[row][column].multicolumn
434 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
436 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
437 return row_info[row].bottom_line;
439 return cell_info[row][column].bottom_line;
445 bool LyXTabular::LeftAlreadyDrawed(int cell) const
447 int column = column_of_cell(cell);
449 int row = row_of_cell(cell);
451 (cell_info[row][column].multicolumn ==
452 LyXTabular::CELL_PART_OF_MULTICOLUMN));
453 if (GetAdditionalWidth(cell_info[row][column].cellno))
455 return column_info[column].right_line;
461 bool LyXTabular::IsLastRow(int cell) const
463 return (row_of_cell(cell) == rows_ - 1);
467 int LyXTabular::GetAdditionalHeight(int cell) const
469 int const row = row_of_cell(cell);
475 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
476 switch (cell_info[row - 1][column].multicolumn) {
477 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
478 bottom = cell_info[row - 1][column].bottom_line;
480 case LyXTabular::CELL_NORMAL:
481 bottom = row_info[row - 1].bottom_line;
484 for (int column = 0; column < columns_ - 1 && top; ++column) {
485 switch (cell_info[row][column].multicolumn){
486 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
487 top = cell_info[row][column].top_line;
489 case LyXTabular::CELL_NORMAL:
490 top = row_info[row].top_line;
494 return WIDTH_OF_LINE;
499 int LyXTabular::GetAdditionalWidth(int cell) const
501 // internally already set in SetWidthOfCell
502 // used to get it back in text.C
503 int const col = right_column_of_cell(cell);
504 if (col < columns_ - 1 && column_info[col].right_line &&
505 column_info[col+1].left_line)
506 return WIDTH_OF_LINE;
512 // returns the maximum over all rows
513 int LyXTabular::GetWidthOfColumn(int cell) const
515 int const column1 = column_of_cell(cell);
516 int const column2 = right_column_of_cell(cell);
518 for (int i = column1; i <= column2; ++i) {
519 result += column_info[i].width_of_column;
525 int LyXTabular::GetWidthOfTabular() const
527 return width_of_tabular;
531 /* returns 1 if a complete update is necessary, otherwise 0 */
532 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
534 if (!IsMultiColumn(cell))
537 int const row = row_of_cell(cell);
538 int const column1 = column_of_cell(cell);
539 int const column2 = right_column_of_cell(cell);
541 // first set columns to 0 so we can calculate the right width
542 for (int i = column1; i <= column2; ++i) {
543 cell_info[row][i].width_of_cell = 0;
545 // set the width to MAX_WIDTH until width > 0
546 int width = (new_width + 2 * WIDTH_OF_LINE);
549 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
550 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
551 width -= column_info[i].width_of_column;
554 cell_info[row][i].width_of_cell = width;
560 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
562 int const row = row_of_cell(cell);
563 int const column1 = column_of_cell(cell);
564 int const column2 = right_column_of_cell(cell);
566 // first set columns to 0 so we can calculate the right width
568 for (; i <= column2; ++i)
569 cell_info[row][i].width_of_cell = 0;
570 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
572 if (i < numberofcells)
573 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
574 SetWidthOfMulticolCell(cell, new_width);
578 /* returns 1 if a complete update is necessary, otherwise 0 */
579 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
581 int const row = row_of_cell(cell);
582 int const column1 = column_of_cell(cell);
586 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
588 if (IsMultiColumn(cell, true)) {
589 tmp = SetWidthOfMulticolCell(cell, new_width);
591 width = (new_width + 2*WIDTH_OF_LINE);
592 cell_info[row][column1].width_of_cell = width;
593 if (column_info[column1].right_line && (column1 < columns_-1) &&
594 column_info[column1+1].left_line) // additional width
595 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
596 tmp = calculate_width_of_column_NMC(column1);
600 for (; i<columns_; ++i)
601 calculate_width_of_column_NMC(i);
602 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
604 if (i < numberofcells)
605 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
606 for (i = 0; i < columns_; ++i)
607 calculate_width_of_column(i);
608 calculate_width_of_tabular();
615 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
617 if (!IsMultiColumn(cell) || onlycolumn)
618 column_info[column_of_cell(cell)].alignment = align;
620 cellinfo_of_cell(cell)->alignment = align;
625 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
627 if (!IsMultiColumn(cell) || onlycolumn)
628 column_info[column_of_cell(cell)].valignment = align;
630 cellinfo_of_cell(cell)->valignment = align;
635 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
637 bool flag = !width.empty();
638 int const j = column_of_cell(cell);
640 column_info[j].p_width = width;
641 if (flag) // do this only if there is a width
642 SetAlignment(cell, LYX_ALIGN_LEFT);
643 for (int i = 0; i < rows_; ++i) {
644 int c = GetCellNumber(i, j);
645 flag = !GetPWidth(c).empty(); // because of multicolumns!
646 GetCellInset(c)->SetAutoBreakRows(flag);
652 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
654 bool const flag = !width.empty();
656 cellinfo_of_cell(cell)->p_width = width;
657 if (IsMultiColumn(cell)) {
658 GetCellInset(cell)->SetAutoBreakRows(flag);
665 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
666 LyXTabular::Feature what)
668 if (what == SET_SPECIAL_MULTI)
669 cellinfo_of_cell(cell)->align_special = special;
671 column_info[column_of_cell(cell)].align_special = special;
676 bool LyXTabular::SetAllLines(int cell, bool line)
678 SetTopLine(cell, line);
679 SetBottomLine(cell, line);
680 SetRightLine(cell, line);
681 SetLeftLine(cell, line);
686 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
688 int const row = row_of_cell(cell);
690 if (onlycolumn || !IsMultiColumn(cell))
691 row_info[row].top_line = line;
693 cellinfo_of_cell(cell)->top_line = line;
698 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
700 if (onlycolumn || !IsMultiColumn(cell))
701 row_info[row_of_cell(cell)].bottom_line = line;
703 cellinfo_of_cell(cell)->bottom_line = line;
708 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
710 if (onlycolumn || !IsMultiColumn(cell))
711 column_info[column_of_cell(cell)].left_line = line;
713 cellinfo_of_cell(cell)->left_line = line;
718 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
720 if (onlycolumn || !IsMultiColumn(cell))
721 column_info[right_column_of_cell(cell)].right_line = line;
723 cellinfo_of_cell(cell)->right_line = line;
728 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
730 if (!onlycolumn && IsMultiColumn(cell))
731 return cellinfo_of_cell(cell)->alignment;
733 return column_info[column_of_cell(cell)].alignment;
737 LyXTabular::VAlignment
738 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
740 if (!onlycolumn && IsMultiColumn(cell))
741 return cellinfo_of_cell(cell)->valignment;
743 return column_info[column_of_cell(cell)].valignment;
747 string const LyXTabular::GetPWidth(int cell) const
749 if (IsMultiColumn(cell))
750 return cellinfo_of_cell(cell)->p_width;
751 return column_info[column_of_cell(cell)].p_width;
755 string const LyXTabular::GetColumnPWidth(int cell) const
757 return column_info[column_of_cell(cell)].p_width;
761 string const LyXTabular::GetMColumnPWidth(int cell) const
763 if (IsMultiColumn(cell))
764 return cellinfo_of_cell(cell)->p_width;
769 string const LyXTabular::GetAlignSpecial(int cell, int what) const
771 if (what == SET_SPECIAL_MULTI)
772 return cellinfo_of_cell(cell)->align_special;
773 return column_info[column_of_cell(cell)].align_special;
777 int LyXTabular::GetWidthOfCell(int cell) const
779 int const row = row_of_cell(cell);
780 int const column1 = column_of_cell(cell);
781 int const column2 = right_column_of_cell(cell);
783 for (int i = column1; i <= column2; ++i) {
784 result += cell_info[row][i].width_of_cell;
790 int LyXTabular::GetBeginningOfTextInCell(int cell) const
794 switch (GetAlignment(cell)){
795 case LYX_ALIGN_CENTER:
796 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
798 case LYX_ALIGN_RIGHT:
799 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
800 // + GetAdditionalWidth(cell);
802 default: /* LYX_ALIGN_LEFT: nothing :-) */
812 bool LyXTabular::IsFirstCellInRow(int cell) const
814 return column_of_cell(cell) == 0;
818 int LyXTabular::GetFirstCellInRow(int row) const
822 return cell_info[row][0].cellno;
825 bool LyXTabular::IsLastCellInRow(int cell) const
827 return (right_column_of_cell(cell) == (columns_ - 1));
831 int LyXTabular::GetLastCellInRow(int row) const
835 return cell_info[row][columns_-1].cellno;
839 bool LyXTabular::calculate_width_of_column(int column)
841 int const old_column_width = column_info[column].width_of_column;
844 for (int i = 0; i < rows_; ++i) {
845 maximum = max(cell_info[i][column].width_of_cell, maximum);
847 column_info[column].width_of_column = maximum;
848 return (column_info[column].width_of_column != old_column_width);
853 // calculate the with of the column without regarding REAL MultiColumn
854 // cells. This means MultiColumn-cells spanning more than 1 column.
856 bool LyXTabular::calculate_width_of_column_NMC(int column)
858 int const old_column_width = column_info[column].width_of_column;
860 for (int i = 0; i < rows_; ++i) {
861 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
862 (cell_info[i][column].width_of_cell > max)) {
863 max = cell_info[i][column].width_of_cell;
866 column_info[column].width_of_column = max;
867 return (column_info[column].width_of_column != old_column_width);
871 void LyXTabular::calculate_width_of_tabular()
873 width_of_tabular = 0;
874 for (int i = 0; i < columns_; ++i) {
875 width_of_tabular += column_info[i].width_of_column;
880 int LyXTabular::row_of_cell(int cell) const
882 if (cell >= numberofcells)
886 return rowofcell[cell];
890 int LyXTabular::column_of_cell(int cell) const
892 if (cell >= numberofcells)
896 return columnofcell[cell];
900 int LyXTabular::right_column_of_cell(int cell) const
902 int const row = row_of_cell(cell);
903 int column = column_of_cell(cell);
904 while (column < (columns_ - 1) &&
905 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
911 // Perfect case for a template... (Lgb)
914 string const write_attribute(string const & name, T const & t)
916 string str = " " + name + "=\"" + tostr(t) + "\"";
921 string const write_attribute(string const & name, bool const & b)
923 return write_attribute(name, int(b));
928 string const write_attribute(string const & name, int value)
930 string str = " " + name + "=\"" + tostr(value) + "\"";
935 string const write_attribute(string const & name, string const & value)
937 string str = " " + name + "=\"" + value + "\"";
942 string const write_attribute(string const & name, bool value)
944 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
952 string const tostr(LyXAlignment const & num)
957 case LYX_ALIGN_BLOCK:
961 case LYX_ALIGN_CENTER:
963 case LYX_ALIGN_RIGHT:
965 case LYX_ALIGN_LAYOUT:
967 case LYX_ALIGN_SPECIAL:
976 string const tostr(LyXTabular::VAlignment const & num)
979 case LyXTabular::LYX_VALIGN_TOP:
981 case LyXTabular::LYX_VALIGN_CENTER:
983 case LyXTabular::LYX_VALIGN_BOTTOM:
992 string const tostr(LyXTabular::BoxType const & num)
995 case LyXTabular::BOX_NONE:
997 case LyXTabular::BOX_PARBOX:
999 case LyXTabular::BOX_MINIPAGE:
1006 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1010 << write_attribute("version", 2)
1011 << write_attribute("rows", rows_)
1012 << write_attribute("columns", columns_)
1014 // global longtable options
1016 << write_attribute("rotate", tostr(rotate))
1017 << write_attribute("islongtable", tostr(is_long_tabular))
1018 << write_attribute("endhead", endhead)
1019 << write_attribute("endfirsthead", endfirsthead)
1020 << write_attribute("endfoot", endfoot)
1021 << write_attribute("endlastfoot", endlastfoot)
1023 for (int j = 0; j < columns_; ++j) {
1025 << write_attribute("alignment", tostr(column_info[j].alignment))
1026 << write_attribute("valignment", tostr(column_info[j].valignment))
1027 << write_attribute("leftline", tostr(column_info[j].left_line))
1028 << write_attribute("rightline", tostr(column_info[j].right_line))
1029 << write_attribute("width",
1030 VSpace(column_info[j].p_width)
1032 << write_attribute("special", column_info[j].align_special)
1035 for (int i = 0; i < rows_; ++i) {
1037 << write_attribute("topline", tostr(row_info[i].top_line))
1038 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1039 << write_attribute("newpage", tostr(row_info[i].newpage))
1041 for (int j = 0; j < columns_; ++j) {
1045 << write_attribute("alignment", tostr(column_info[j].alignment))
1046 << write_attribute("valignment", tostr(column_info[j].valignment))
1047 << write_attribute("leftline", tostr(column_info[j].left_line))
1048 << write_attribute("rightline", tostr(column_info[j].right_line))
1049 << write_attribute("width",
1050 VSpace(column_info[j].p_width)
1052 << write_attribute("special", column_info[j].align_special)
1059 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1060 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1061 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1062 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1063 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1064 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1065 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1066 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1067 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1068 << write_attribute("width", cell_info[i][j].p_width)
1069 << write_attribute("special", cell_info[i][j].align_special)
1071 os << "\\begin_inset ";
1072 cell_info[i][j].inset.Write(buf, os);
1073 os << "\n\\end_inset \n"
1081 os << "</lyxtabular>\n";
1085 // I would have liked a fromstr template a lot better. (Lgb)
1088 bool string2type(string const str, LyXAlignment & num)
1091 num = LYX_ALIGN_NONE;
1092 else if (str == "block")
1093 num = LYX_ALIGN_BLOCK;
1094 else if (str == "left")
1095 num = LYX_ALIGN_LEFT;
1096 else if (str == "center")
1097 num = LYX_ALIGN_CENTER;
1098 else if (str == "right")
1099 num = LYX_ALIGN_RIGHT;
1107 bool string2type(string const str, LyXTabular::VAlignment & num)
1110 num = LyXTabular::LYX_VALIGN_TOP;
1111 else if (str == "center")
1112 num = LyXTabular::LYX_VALIGN_CENTER;
1113 else if (str == "bottom")
1114 num = LyXTabular::LYX_VALIGN_BOTTOM;
1122 bool string2type(string const str, LyXTabular::BoxType & num)
1125 num = LyXTabular::BOX_NONE;
1126 else if (str == "parbox")
1127 num = LyXTabular::BOX_PARBOX;
1128 else if (str == "minipage")
1129 num = LyXTabular::BOX_MINIPAGE;
1137 bool string2type(string const str, bool & num)
1141 else if (str == "false")
1150 bool getTokenValue(string const & str, const char * token, string & ret)
1152 size_t token_length = strlen(token);
1153 string::size_type pos = str.find(token);
1155 if (pos == string::npos || pos+token_length+1 >= str.length()
1156 || str[pos+token_length] != '=')
1159 pos += token_length + 1;
1161 if ((ch != '"') && (ch != '\'')) { // only read till next space
1165 while((pos < str.length() - 1) && (str[++pos] != ch))
1173 bool getTokenValue(string const & str, const char * token, int & num)
1176 if (!getTokenValue(str, token, tmp))
1178 num = strToInt(tmp);
1184 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1187 if (!getTokenValue(str, token, tmp))
1189 return string2type(tmp, num);
1194 bool getTokenValue(string const & str, const char * token,
1195 LyXTabular::VAlignment & num)
1198 if (!getTokenValue(str, token, tmp))
1200 return string2type(tmp, num);
1205 bool getTokenValue(string const & str, const char * token,
1206 LyXTabular::BoxType & num)
1209 if (!getTokenValue(str, token, tmp))
1211 return string2type(tmp, num);
1216 bool getTokenValue(string const & str, const char * token, bool & flag)
1219 if (!getTokenValue(str, token, tmp))
1221 return string2type(tmp, flag);
1226 void l_getline(istream & is, string & str)
1234 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1237 istream & is = lex.getStream();
1239 l_getline(is, line);
1240 if (!prefixIs(line, "<lyxtabular ")
1241 && !prefixIs(line, "<LyXTabular ")) {
1242 OldFormatRead(lex, line);
1247 if (!getTokenValue(line, "version", version))
1250 ReadOld(buf, is, lex, line);
1251 else if (version == 2)
1252 ReadNew(buf, is, lex, line);
1256 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1257 LyXLex & lex, string const & l)
1261 if (!getTokenValue(line, "rows", rows_arg))
1264 if (!getTokenValue(line, "columns", columns_arg))
1266 Init(rows_arg, columns_arg);
1267 l_getline(is, line);
1268 if (!prefixIs(line, "<features")) {
1269 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1270 line << ")" << endl;
1273 getTokenValue(line, "rotate", rotate);
1274 getTokenValue(line, "islongtable", is_long_tabular);
1275 getTokenValue(line, "endhead", endhead);
1276 getTokenValue(line, "endfirsthead", endfirsthead);
1277 getTokenValue(line, "endfoot", endfoot);
1278 getTokenValue(line, "endlastfoot", endlastfoot);
1280 for (int j = 0; j < columns_; ++j) {
1282 if (!prefixIs(line,"<column")) {
1283 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1284 line << ")" << endl;
1287 getTokenValue(line, "alignment", column_info[j].alignment);
1288 getTokenValue(line, "valignment", column_info[j].valignment);
1289 getTokenValue(line, "leftline", column_info[j].left_line);
1290 getTokenValue(line, "rightline", column_info[j].right_line);
1291 getTokenValue(line, "width", column_info[j].p_width);
1292 getTokenValue(line, "special", column_info[j].align_special);
1295 for (int i = 0; i < rows_; ++i) {
1296 l_getline(is, line);
1297 if (!prefixIs(line, "<row")) {
1298 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1299 line << ")" << endl;
1302 getTokenValue(line, "topline", row_info[i].top_line);
1303 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1304 getTokenValue(line, "newpage", row_info[i].newpage);
1305 for (int j = 0; j < columns_; ++j) {
1306 l_getline(is, line);
1307 if (!prefixIs(line, "<cell")) {
1308 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1309 line << ")" << endl;
1312 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1313 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1314 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1315 getTokenValue(line, "topline", cell_info[i][j].top_line);
1316 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1317 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1318 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1319 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1320 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1321 getTokenValue(line, "width", cell_info[i][j].p_width);
1322 getTokenValue(line, "special", cell_info[i][j].align_special);
1323 l_getline(is, line);
1324 if (prefixIs(line, "\\begin_inset")) {
1325 cell_info[i][j].inset.Read(buf, lex);
1326 l_getline(is, line);
1328 if (line != "</cell>") {
1329 lyxerr << "Wrong tabular format (expected </cell> got" <<
1330 line << ")" << endl;
1334 l_getline(is, line);
1335 if (line != "</row>") {
1336 lyxerr << "Wrong tabular format (expected </row> got" <<
1337 line << ")" << endl;
1341 while (line != "</lyxtabular>") {
1342 l_getline(is, line);
1344 set_row_column_number_info();
1348 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1354 int columns_arg = 0;
1355 int is_long_tabular_arg = false;
1356 int rotate_arg = false;
1365 istream & is = lex.getStream();
1368 version = lyx::atoi(s.substr(8, string::npos));
1372 vector<int> cont_row_info;
1375 lyxerr << "Tabular format < 5 is not supported anymore\n"
1376 "Get an older version of LyX (< 1.1.x) for conversion!"
1378 WriteAlert(_("Warning:"),
1379 _("Tabular format < 5 is not supported anymore\n"),
1380 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1382 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1383 >> rotate_arg >> a >> b >> c >> d;
1385 is >> rows_arg >> columns_arg;
1386 Init(rows_arg, columns_arg);
1387 cont_row_info = vector<int>(rows_arg);
1388 SetLongTabular(is_long_tabular_arg);
1389 SetRotateTabular(rotate_arg);
1391 for (i = 0; i < rows_; ++i) {
1393 cont_row_info[i] = false;
1395 for (i = 0; i < columns_; ++i) {
1398 for (i = 0; i < rows_; ++i) {
1399 for (j = 0; j < columns_; ++j) {
1404 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1405 >> rotate_arg >> a >> b >> c >> d;
1406 Init(rows_arg, columns_arg);
1407 cont_row_info = vector<int>(rows_arg);
1408 SetLongTabular(is_long_tabular_arg);
1409 SetRotateTabular(rotate_arg);
1411 endfirsthead = b + 1;
1413 endlastfoot = d + 1;
1414 for (i = 0; i < rows_; ++i) {
1415 a = b = c = d = e = f = g = 0;
1416 is >> a >> b >> c >> d;
1417 row_info[i].top_line = a;
1418 row_info[i].bottom_line = b;
1419 cont_row_info[i] = c;
1420 row_info[i].newpage = d;
1422 for (i = 0; i < columns_; ++i) {
1427 char ch; // skip '"'
1430 // ignore is buggy but we will use it later (Lgb)
1431 is.ignore(); // skip '"'
1433 getline(is, s1, '"');
1435 is >> ch; // skip '"'
1437 // ignore is buggy but we will use it later (Lgb)
1438 is.ignore(); // skip '"'
1440 getline(is, s2, '"');
1441 column_info[i].alignment = static_cast<LyXAlignment>(a);
1442 column_info[i].left_line = b;
1443 column_info[i].right_line = c;
1444 column_info[i].p_width = s1;
1445 column_info[i].align_special = s2;
1447 for (i = 0; i < rows_; ++i) {
1448 for (j = 0; j < columns_; ++j) {
1451 is >> a >> b >> c >> d >> e >> f >> g;
1454 is >> ch; // skip '"'
1456 // ignore is buggy but we will use it later (Lgb)
1457 is.ignore(); // skip '"'
1459 getline(is, s1, '"');
1461 is >> ch; // skip '"'
1463 // ignore is buggy but we will use it later (Lgb)
1464 is.ignore(); // skip '"'
1466 getline(is, s2, '"');
1467 cell_info[i][j].multicolumn = static_cast<char>(a);
1468 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1469 cell_info[i][j].top_line = static_cast<char>(c);
1470 cell_info[i][j].bottom_line = static_cast<char>(d);
1471 cell_info[i][j].left_line = column_info[j].left_line;
1472 cell_info[i][j].right_line = column_info[j].right_line;
1473 cell_info[i][j].rotate = static_cast<bool>(f);
1474 cell_info[i][j].usebox = static_cast<BoxType>(g);
1475 cell_info[i][j].align_special = s1;
1476 cell_info[i][j].p_width = s2;
1480 set_row_column_number_info(true);
1482 LyXParagraph * par = new LyXParagraph;
1483 LyXParagraph * return_par = 0;
1485 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1486 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1491 LyXFont font(LyXFont::ALL_INHERIT);
1492 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1494 while (lex.IsOK()) {
1496 string const token = lex.GetString();
1499 if (token == "\\layout"
1500 || token == "\\end_float"
1501 || token == "\\end_deeper") {
1502 lex.pushToken(token);
1505 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1517 lex.pushToken(token);
1521 lex.printError("New Paragraph allocated! This should not happen!");
1522 lex.pushToken(token);
1528 // now we have the par we should fill the insets with this!
1530 InsetText * inset = GetCellInset(cell);
1533 for (int i = 0; i < par->Last(); ++i) {
1534 if (par->IsNewline(i)) {
1536 if (cell > GetNumberOfCells()) {
1537 lyxerr << "Some error in reading old table format occured!" <<
1538 endl << "Terminating when reading cell[" << cell << "]!" <<
1543 row = row_of_cell(cell);
1544 if (cont_row_info[row]) {
1546 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1547 while(!IsFirstCellInRow(--cell));
1549 inset = GetCellInset(cell);
1552 inset = GetCellInset(cell);
1553 row = row_of_cell(cell);
1554 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1556 // insert a space instead
1558 par->InsertChar(i, ' ');
1561 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1562 inset->par->InsertFromMinibuffer(inset->par->Last());
1569 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1571 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1572 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1576 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1578 int const row = row_of_cell(cell);
1579 int const column = column_of_cell(cell);
1580 return &cell_info[row][column];
1584 void LyXTabular::SetMultiColumn(int cell, int number)
1586 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1587 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1588 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1589 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1590 for (number--; number > 0; --number) {
1591 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1593 set_row_column_number_info();
1597 int LyXTabular::cells_in_multicolumn(int cell) const
1599 int const row = row_of_cell(cell);
1600 int column = column_of_cell(cell);
1603 while ((column < columns_) &&
1604 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1613 int LyXTabular::UnsetMultiColumn(int cell)
1615 int const row = row_of_cell(cell);
1616 int column = column_of_cell(cell);
1620 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1621 cell_info[row][column].multicolumn = CELL_NORMAL;
1623 while ((column < columns_) &&
1624 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1626 cell_info[row][column].multicolumn = CELL_NORMAL;
1631 set_row_column_number_info();
1636 void LyXTabular::SetLongTabular(bool what)
1638 is_long_tabular = what;
1642 bool LyXTabular::IsLongTabular() const
1644 return is_long_tabular;
1648 void LyXTabular::SetRotateTabular(bool flag)
1654 bool LyXTabular::GetRotateTabular() const
1660 void LyXTabular::SetRotateCell(int cell, bool flag)
1662 cellinfo_of_cell(cell)->rotate = flag;
1666 bool LyXTabular::GetRotateCell(int cell) const
1668 return cellinfo_of_cell(cell)->rotate;
1672 bool LyXTabular::NeedRotating() const
1676 for (int i = 0; i < rows_; ++i) {
1677 for (int j = 0; j < columns_; ++j) {
1678 if (cell_info[i][j].rotate)
1686 bool LyXTabular::IsLastCell(int cell) const
1688 if ((cell + 1) < GetNumberOfCells())
1694 int LyXTabular::GetCellAbove(int cell) const
1696 if (row_of_cell(cell) > 0)
1697 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1702 int LyXTabular::GetCellBelow(int cell) const
1704 if (row_of_cell(cell) + 1 < rows_)
1705 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1710 int LyXTabular::GetLastCellAbove(int cell) const
1712 if (row_of_cell(cell) <= 0)
1714 if (!IsMultiColumn(cell))
1715 return GetCellAbove(cell);
1716 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1720 int LyXTabular::GetLastCellBelow(int cell) const
1722 if (row_of_cell(cell) + 1 >= rows_)
1724 if (!IsMultiColumn(cell))
1725 return GetCellBelow(cell);
1726 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1730 int LyXTabular::GetCellNumber(int row, int column) const
1732 if (column >= columns_)
1733 column = columns_ - 1;
1734 else if (column < 0)
1741 return cell_info[row][column].cellno;
1745 void LyXTabular::SetUsebox(int cell, BoxType type)
1747 cellinfo_of_cell(cell)->usebox = type;
1751 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1753 if (column_info[column_of_cell(cell)].p_width.empty() &&
1754 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1756 if (cellinfo_of_cell(cell)->usebox > 1)
1757 return cellinfo_of_cell(cell)->usebox;
1758 return UseParbox(cell);
1762 void LyXTabular::SetLTHead(int cell, bool first)
1764 int const row = row_of_cell(cell);
1765 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1768 if (endfirsthead == val)
1781 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1784 if (abs(endhead) > rows_)
1786 return (row_of_cell(cell) == abs(endhead) - 1);
1790 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1793 if (abs(endfirsthead) > rows_)
1795 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1799 void LyXTabular::SetLTFoot(int cell, bool last)
1801 int const row = row_of_cell(cell);
1802 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1805 if (endlastfoot == val)
1818 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1821 if ((endfoot + 1) > rows_)
1823 return (row_of_cell(cell) == abs(endfoot) - 1);
1827 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1830 if (abs(endlastfoot) > rows_)
1832 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1836 void LyXTabular::SetLTNewPage(int cell, bool what)
1838 row_info[row_of_cell(cell)].newpage = what;
1842 bool LyXTabular::GetLTNewPage(int cell) const
1844 return row_info[row_of_cell(cell)].newpage;
1848 bool LyXTabular::SetAscentOfRow(int row, int height)
1850 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1852 row_info[row].ascent_of_row = height;
1857 bool LyXTabular::SetDescentOfRow(int row, int height)
1859 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1861 row_info[row].descent_of_row = height;
1866 int LyXTabular::GetAscentOfRow(int row) const
1870 return row_info[row].ascent_of_row;
1874 int LyXTabular::GetDescentOfRow(int row) const
1878 return row_info[row].descent_of_row;
1882 int LyXTabular::GetHeightOfTabular() const
1886 for (int row = 0; row < rows_; ++row)
1887 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1888 GetAdditionalHeight(GetCellNumber(row, 0));
1893 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1895 if ((row >= rows_) || (column >= columns_))
1897 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1901 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1903 if ((row < 0) || (row >= rows_))
1906 int const fcell = GetFirstCellInRow(row);
1907 int const n = NumberOfCellsInRow(fcell) + fcell;
1910 for (int i = fcell; i < n; ++i) {
1914 if (tmp == (n - fcell)){
1917 for (int i = fcell; i < n; ++i) {
1920 << column_of_cell(i) + 1
1922 << right_column_of_cell(i) + 1
1934 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1936 if ((row < 0) || (row >= rows_))
1939 int const fcell = GetFirstCellInRow(row);
1940 int const n = NumberOfCellsInRow(fcell) + fcell;
1943 for (int i = fcell; i < n; ++i) {
1947 if (tmp == (n-fcell)){
1950 for (int i = fcell; i < n; ++i) {
1951 if (BottomLine(i)) {
1953 << column_of_cell(i) + 1
1955 << right_column_of_cell(i) + 1
1967 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1971 if (GetRotateCell(cell)) {
1972 os << "\\begin{sideways}\n";
1975 if (IsMultiColumn(cell)) {
1976 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1977 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1978 os << cellinfo_of_cell(cell)->align_special << "}{";
1982 if (!GetPWidth(cell).empty()) {
1983 switch (GetVAlignment(cell)) {
1984 case LYX_VALIGN_TOP:
1987 case LYX_VALIGN_CENTER:
1990 case LYX_VALIGN_BOTTOM:
1994 os << "{" << GetPWidth(cell) << '}';
1996 switch (GetAlignment(cell)) {
1997 case LYX_ALIGN_LEFT:
2000 case LYX_ALIGN_RIGHT:
2008 if (RightLine(cell))
2010 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2016 if (GetUsebox(cell) == BOX_PARBOX) {
2018 switch (GetVAlignment(cell)) {
2019 case LYX_VALIGN_TOP:
2022 case LYX_VALIGN_CENTER:
2025 case LYX_VALIGN_BOTTOM:
2029 os << "]{" << GetPWidth(cell) << "}{";
2030 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2031 os << "\\begin{minipage}[";
2032 switch (GetVAlignment(cell)) {
2033 case LYX_VALIGN_TOP:
2036 case LYX_VALIGN_CENTER:
2039 case LYX_VALIGN_BOTTOM:
2043 os << "]{" << GetPWidth(cell) << "}\n";
2050 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2055 if (GetUsebox(cell) == BOX_PARBOX)
2057 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2058 os << "%\n\\end{minipage}";
2061 if (IsMultiColumn(cell)){
2064 if (GetRotateCell(cell)) {
2065 os << "%\n\\end{sideways}";
2072 int LyXTabular::Latex(Buffer const * buf,
2073 ostream & os, bool fragile, bool fp) const
2078 //+---------------------------------------------------------------------
2079 //+ first the opening preamble +
2080 //+---------------------------------------------------------------------
2083 os << "\\begin{sideways}\n";
2086 if (is_long_tabular)
2087 os << "\\begin{longtable}{";
2089 os << "\\begin{tabular}{";
2090 for (int i = 0; i < columns_; ++i) {
2091 if (column_info[i].left_line)
2093 if (!column_info[i].align_special.empty()) {
2094 os << column_info[i].align_special;
2095 } else if (!column_info[i].p_width.empty()) {
2096 switch (column_info[i].valignment) {
2097 case LYX_VALIGN_TOP:
2100 case LYX_VALIGN_CENTER:
2103 case LYX_VALIGN_BOTTOM:
2108 << column_info[i].p_width
2111 switch (column_info[i].alignment) {
2112 case LYX_ALIGN_LEFT:
2115 case LYX_ALIGN_RIGHT:
2123 if (column_info[i].right_line)
2129 //+---------------------------------------------------------------------
2130 //+ the single row and columns (cells) +
2131 //+---------------------------------------------------------------------
2133 for (int i = 0; i < rows_; ++i) {
2134 ret += TeXTopHLine(os, i);
2136 if (IsLongTabular()) {
2137 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2138 os << "\\endhead\n";
2141 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2142 os << "\\endfirsthead\n";
2145 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2146 os << "\\endfoot\n";
2149 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2150 os << "\\endlastfoot\n";
2155 ret += TeXBottomHLine(os, i-1);
2156 ret += TeXTopHLine(os, i);
2158 for (int j = 0; j < columns_; ++j) {
2159 if (IsPartOfMultiColumn(i,j))
2161 ret += TeXCellPreamble(os, cell);
2162 InsetText * inset = GetCellInset(cell);
2164 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2165 inset->par->Last() > 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 //+---------------------------------------------------------------------
2317 void print_n_chars(ostream & os, unsigned char ch, int n)
2319 os << string(n, ch);
2323 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2324 vector<unsigned int> const & clen) const
2326 int const fcell = GetFirstCellInRow(row);
2327 int const n = NumberOfCellsInRow(fcell) + fcell;
2330 for (int i = fcell; i < n; ++i) {
2340 for (int i = fcell; i < n; ++i) {
2351 int column = column_of_cell(i);
2352 int len = clen[column];
2353 while(IsPartOfMultiColumn(row, ++column))
2354 len += clen[column] + 4;
2355 print_n_chars(os, ch, len);
2370 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2371 vector<unsigned int> const & clen) const
2373 int const fcell = GetFirstCellInRow(row);
2374 int const n = NumberOfCellsInRow(fcell) + fcell;
2377 for (int i = fcell; i < n; ++i) {
2378 if (BottomLine(i)) {
2387 for (int i = fcell; i < n; ++i) {
2388 if (BottomLine(i)) {
2398 int column = column_of_cell(i);
2399 int len = clen[column];
2400 while(IsPartOfMultiColumn(row, ++column))
2401 len += clen[column] + 4;
2402 print_n_chars(os, ch, len);
2403 if (BottomLine(i)) {
2417 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2418 int cell, int row, int column,
2419 vector<unsigned int> const & clen) const
2422 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2429 unsigned int len1 = sstr.str().length();
2430 unsigned int len2 = clen[column];
2431 while(IsPartOfMultiColumn(row, ++column))
2432 len2 += clen[column] + 4;
2435 switch (GetAlignment(cell)) {
2437 case LYX_ALIGN_LEFT:
2440 case LYX_ALIGN_RIGHT:
2444 case LYX_ALIGN_CENTER:
2450 for (unsigned int i = 0; i < len1; ++i)
2453 for (unsigned int i = 0; i < len2; ++i)
2455 if (RightLine(cell))
2464 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2468 //+---------------------------------------------------------------------
2469 //+ first calculate the width of the single columns +
2470 //+---------------------------------------------------------------------
2471 vector<unsigned int> clen(columns_);
2473 // first all non (real) multicolumn cells!
2474 for (int j = 0; j < columns_; ++j) {
2476 for (int i = 0; i < rows_; ++i) {
2477 int cell = GetCellNumber(i, j);
2478 if (IsMultiColumn(cell, true))
2481 GetCellInset(cell)->Ascii(buf, sstr, 0);
2482 if (clen[j] < sstr.str().length())
2483 clen[j] = sstr.str().length();
2486 // then all (real) multicolumn cells!
2487 for (int j = 0; j < columns_; ++j) {
2488 for (int i = 0; i < rows_; ++i) {
2489 int cell = GetCellNumber(i, j);
2490 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2493 GetCellInset(cell)->Ascii(buf, sstr, 0);
2494 int len = int(sstr.str().length());
2495 int const n = cells_in_multicolumn(cell);
2496 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2498 if (len > int(clen[j + n - 1]))
2499 clen[j + n - 1] = len;
2503 for (int i = 0; i < rows_; ++i) {
2504 AsciiTopHLine(os, i, clen);
2505 for (int j = 0; j < columns_; ++j) {
2506 if (IsPartOfMultiColumn(i,j))
2508 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2512 AsciiBottomHLine(os, i, clen);
2518 InsetText * LyXTabular::GetCellInset(int cell) const
2520 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2524 InsetText * LyXTabular::GetCellInset(int row, int column) const
2526 return GetCellInset(GetCellNumber(row, column));
2530 void LyXTabular::Validate(LaTeXFeatures & features) const
2532 if (IsLongTabular())
2533 features.longtable = true;
2535 features.rotating = true;
2536 for (int cell = 0; cell < numberofcells; ++cell) {
2537 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2538 features.array = true;
2539 GetCellInset(cell)->Validate(features);
2544 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2546 LyXParagraph * par = GetCellInset(cell)->par;
2548 for (; par; par = par->next) {
2549 for (int i = 0; i < par->Last(); ++i) {
2550 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)