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"
45 int const WIDTH_OF_LINE = 5;
49 /// Define a few methods for the inner structs
51 LyXTabular::cellstruct::cellstruct()
55 multicolumn = LyXTabular::CELL_NORMAL;
56 alignment = LYX_ALIGN_CENTER;
57 valignment = LYX_VALIGN_TOP;
67 LyXTabular::rowstruct::rowstruct()
77 LyXTabular::columnstruct::columnstruct()
81 alignment = LYX_ALIGN_CENTER;
82 valignment = LYX_VALIGN_TOP;
88 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
91 Init(rows_arg, columns_arg);
95 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
98 Init(lt.rows_, lt.columns_);
100 #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)
106 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
113 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
115 // If this and lt is not of the same size we have a serious bug
116 // So then it is ok to throw an exception, or for now
118 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
120 cell_info = lt.cell_info;
121 row_info = lt.row_info;
122 column_info = lt.column_info;
124 // long tabular stuff
125 SetLongTabular(lt.is_long_tabular);
126 endhead = lt.endhead;
127 endfoot = lt.endfoot;
128 endfirsthead = lt.endfirsthead;
129 endlastfoot = lt.endlastfoot;
139 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
141 LyXTabular * result = new LyXTabular(inset, *this);
142 // don't know if this is good but I need to Clone also
143 // the text-insets here, this is for the Undo-facility!
144 for (int i = 0; i < rows_; ++i) {
145 for (int j = 0; j < columns_; ++j) {
146 result->cell_info[i][j].inset = cell_info[i][j].inset;
147 result->cell_info[i][j].inset.setOwner(inset);
154 /* activates all lines and sets all widths to 0 */
155 void LyXTabular::Init(int rows_arg, int columns_arg)
158 columns_ = columns_arg;
159 row_info = row_vector(rows_, rowstruct());
160 column_info = column_vector(columns_, columnstruct());
161 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
164 for (int i = 0; i < rows_; ++i) {
165 for (int j = 0; j < columns_; ++j) {
166 cell_info[i][j].inset.setOwner(owner_);
167 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
168 cell_info[i][j].cellno = cellno++;
170 cell_info[i].back().right_line = true;
172 row_info.back().bottom_line = true;
173 row_info.front().bottom_line = true;
175 for (int i = 0; i < columns_; ++i) {
176 calculate_width_of_column(i);
178 column_info.back().right_line = true;
180 calculate_width_of_tabular();
182 rowofcell = vector<int>();
183 columnofcell = vector<int>();
184 set_row_column_number_info();
185 is_long_tabular = false;
194 void LyXTabular::AppendRow(int cell)
198 int row = row_of_cell(cell);
200 row_vector::iterator rit = row_info.begin() + row;
201 row_info.insert(rit, rowstruct());
202 // now set the values of the row before
203 row_info[row] = row_info[row+1];
206 cell_vvector::iterator cit = cell_info.begin() + row;
207 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
209 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
212 for (int i = 0; i <= row; ++i) {
213 for (int j = 0; j < columns_; ++j) {
214 c_info[i][j] = cell_info[i][j];
217 for (int i = row + 1; i < rows_; ++i) {
218 for (int j = 0; j < columns_; ++j) {
219 c_info[i][j] = cell_info[i-1][j];
224 for (int j = 0; j < columns_; ++j) {
225 cell_info[row][j].inset.clear();
232 void LyXTabular::DeleteRow(int row)
234 // Why make it so hard? (Lgb)
237 if (rows_ == 1) return; // Not allowed to delete last row
239 row_info.erase(row_info.begin() + row); //&row_info[row]);
240 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
246 void LyXTabular::AppendColumn(int cell)
250 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
252 int const column = column_of_cell(cell);
253 column_vector::iterator cit = column_info.begin() + column + 1;
254 column_info.insert(cit, columnstruct());
255 // set the column values of the column before
256 column_info[column+1] = column_info[column];
258 for (int i = 0; i < rows_; ++i) {
259 for (int j = 0; j <= column; ++j) {
260 c_info[i][j] = cell_info[i][j];
262 for (int j = column + 1; j < columns_; ++j) {
263 c_info[i][j] = cell_info[i][j - 1];
265 // care about multicolumns
266 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
267 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
269 if ((column + 1) == columns_ ||
270 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
271 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
276 for (int i = 0; i < rows_; ++i) {
277 //cell_info[i][column].inset.clear();
278 cell_info[i][column + 1].inset.clear();
284 void LyXTabular::DeleteColumn(int column)
286 // Similar to DeleteRow
287 //if (!(columns_ - 1))
289 if (columns_ == 1) return; // Not allowed to delete last column
291 column_info.erase(column_info.begin() + column);
292 for (int i = 0; i < rows_; ++i) {
293 cell_info[i].erase(cell_info[i].begin() + column);
300 void LyXTabular::Reinit()
302 for (int i = 0; i < rows_; ++i) {
303 for (int j = 0; j < columns_; ++j) {
304 cell_info[i][j].width_of_cell = 0;
305 cell_info[i][j].inset.setOwner(owner_);
309 for (int i = 0; i < columns_; ++i) {
310 calculate_width_of_column(i);
312 calculate_width_of_tabular();
314 set_row_column_number_info();
318 void LyXTabular::set_row_column_number_info(bool oldformat)
321 for (int row = 0; row < rows_; ++row) {
322 for (int column = 0; column<columns_; ++column) {
323 if (cell_info[row][column].multicolumn
324 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
326 cell_info[row][column].cellno = numberofcells;
329 ++numberofcells; // because this is one more than as we start from 0
331 rowofcell.resize(numberofcells);
332 columnofcell.resize(numberofcells);
334 for (int row = 0, column = 0, c = 0;
335 c < numberofcells && row < rows_ && column < columns_;) {
337 columnofcell[c] = column;
341 } while (column < columns_ &&
342 cell_info[row][column].multicolumn
343 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
344 if (column == columns_) {
350 for (int row = 0; row < rows_; ++row) {
351 for (int column = 0; column < columns_; ++column) {
352 if (IsPartOfMultiColumn(row,column))
354 // now set the right line of multicolumns right for oldformat read
356 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
358 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
359 cell_info[row][column].right_line =
360 cell_info[row][column+cn-1].right_line;
362 cell_info[row][column].inset.SetAutoBreakRows(
363 !GetPWidth(GetCellNumber(row, column)).empty());
369 int LyXTabular::GetNumberOfCells() const
371 return numberofcells;
375 int LyXTabular::NumberOfCellsInRow(int cell) const
377 int const row = row_of_cell(cell);
379 for (int i = 0; i < columns_; ++i) {
380 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
387 /* returns 1 if there is a topline, returns 0 if not */
388 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
390 int const row = row_of_cell(cell);
392 if (!onlycolumn && IsMultiColumn(cell))
393 return cellinfo_of_cell(cell)->top_line;
394 return row_info[row].top_line;
398 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
400 // no bottom line underneath non-existent cells if you please
401 // Isn't that a programming error? Is so this should
402 // be an Assert instead. (Lgb)
403 if (cell >= numberofcells)
406 if (!onlycolumn && IsMultiColumn(cell))
407 return cellinfo_of_cell(cell)->bottom_line;
408 return row_info[row_of_cell(cell)].bottom_line;
412 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
414 if (!onlycolumn && IsMultiColumn(cell))
415 return cellinfo_of_cell(cell)->left_line;
416 return column_info[column_of_cell(cell)].left_line;
420 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
422 if (!onlycolumn && IsMultiColumn(cell))
423 return cellinfo_of_cell(cell)->right_line;
424 return column_info[right_column_of_cell(cell)].right_line;
428 bool LyXTabular::TopAlreadyDrawed(int cell) const
430 if (GetAdditionalHeight(cell))
432 int row = row_of_cell(cell);
434 int column = column_of_cell(cell);
437 && cell_info[row][column].multicolumn
438 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
440 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
441 return row_info[row].bottom_line;
443 return cell_info[row][column].bottom_line;
449 bool LyXTabular::LeftAlreadyDrawed(int cell) const
451 int column = column_of_cell(cell);
453 int row = row_of_cell(cell);
455 (cell_info[row][column].multicolumn ==
456 LyXTabular::CELL_PART_OF_MULTICOLUMN));
457 if (GetAdditionalWidth(cell_info[row][column].cellno))
459 return column_info[column].right_line;
465 bool LyXTabular::IsLastRow(int cell) const
467 return (row_of_cell(cell) == rows_ - 1);
471 int LyXTabular::GetAdditionalHeight(int cell) const
473 int const row = row_of_cell(cell);
479 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
480 switch (cell_info[row - 1][column].multicolumn) {
481 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
482 bottom = cell_info[row - 1][column].bottom_line;
484 case LyXTabular::CELL_NORMAL:
485 bottom = row_info[row - 1].bottom_line;
488 for (int column = 0; column < columns_ - 1 && top; ++column) {
489 switch (cell_info[row][column].multicolumn){
490 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
491 top = cell_info[row][column].top_line;
493 case LyXTabular::CELL_NORMAL:
494 top = row_info[row].top_line;
498 return WIDTH_OF_LINE;
503 int LyXTabular::GetAdditionalWidth(int cell) const
505 // internally already set in SetWidthOfCell
506 // used to get it back in text.C
507 int const col = right_column_of_cell(cell);
508 if (col < columns_ - 1 && column_info[col].right_line &&
509 column_info[col+1].left_line)
510 return WIDTH_OF_LINE;
516 // returns the maximum over all rows
517 int LyXTabular::GetWidthOfColumn(int cell) const
519 int const column1 = column_of_cell(cell);
520 int const column2 = right_column_of_cell(cell);
522 for (int i = column1; i <= column2; ++i) {
523 result += column_info[i].width_of_column;
529 int LyXTabular::GetWidthOfTabular() const
531 return width_of_tabular;
535 /* returns 1 if a complete update is necessary, otherwise 0 */
536 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
538 if (!IsMultiColumn(cell))
541 int const row = row_of_cell(cell);
542 int const column1 = column_of_cell(cell);
543 int const column2 = right_column_of_cell(cell);
545 // first set columns to 0 so we can calculate the right width
546 for (int i = column1; i <= column2; ++i) {
547 cell_info[row][i].width_of_cell = 0;
549 // set the width to MAX_WIDTH until width > 0
550 int width = (new_width + 2 * WIDTH_OF_LINE);
553 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
554 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
555 width -= column_info[i].width_of_column;
558 cell_info[row][i].width_of_cell = width;
564 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
566 int const row = row_of_cell(cell);
567 int const column1 = column_of_cell(cell);
568 int const column2 = right_column_of_cell(cell);
570 // first set columns to 0 so we can calculate the right width
572 for (; i <= column2; ++i)
573 cell_info[row][i].width_of_cell = 0;
574 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
576 if (i < numberofcells)
577 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
578 SetWidthOfMulticolCell(cell, new_width);
582 /* returns 1 if a complete update is necessary, otherwise 0 */
583 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
585 int const row = row_of_cell(cell);
586 int const column1 = column_of_cell(cell);
590 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
592 if (IsMultiColumn(cell, true)) {
593 tmp = SetWidthOfMulticolCell(cell, new_width);
595 width = (new_width + 2*WIDTH_OF_LINE);
596 cell_info[row][column1].width_of_cell = width;
597 if (column_info[column1].right_line && (column1 < columns_-1) &&
598 column_info[column1+1].left_line) // additional width
599 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
600 tmp = calculate_width_of_column_NMC(column1);
604 for (; i<columns_; ++i)
605 calculate_width_of_column_NMC(i);
606 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
608 if (i < numberofcells)
609 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
610 for (i = 0; i < columns_; ++i)
611 calculate_width_of_column(i);
612 calculate_width_of_tabular();
619 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
621 if (!IsMultiColumn(cell) || onlycolumn)
622 column_info[column_of_cell(cell)].alignment = align;
624 cellinfo_of_cell(cell)->alignment = align;
629 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
631 if (!IsMultiColumn(cell) || onlycolumn)
632 column_info[column_of_cell(cell)].valignment = align;
634 cellinfo_of_cell(cell)->valignment = align;
639 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
641 bool flag = !width.empty();
642 int const j = column_of_cell(cell);
644 column_info[j].p_width = width;
645 if (flag) // do this only if there is a width
646 SetAlignment(cell, LYX_ALIGN_LEFT);
647 for (int i = 0; i < rows_; ++i) {
648 int c = GetCellNumber(i, j);
649 flag = !GetPWidth(c).empty(); // because of multicolumns!
650 GetCellInset(c)->SetAutoBreakRows(flag);
656 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
658 bool const flag = !width.empty();
660 cellinfo_of_cell(cell)->p_width = width;
661 if (IsMultiColumn(cell)) {
662 GetCellInset(cell)->SetAutoBreakRows(flag);
669 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
670 LyXTabular::Feature what)
672 if (what == SET_SPECIAL_MULTI)
673 cellinfo_of_cell(cell)->align_special = special;
675 column_info[column_of_cell(cell)].align_special = special;
680 bool LyXTabular::SetAllLines(int cell, bool line)
682 SetTopLine(cell, line);
683 SetBottomLine(cell, line);
684 SetRightLine(cell, line);
685 SetLeftLine(cell, line);
690 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
692 int const row = row_of_cell(cell);
694 if (onlycolumn || !IsMultiColumn(cell))
695 row_info[row].top_line = line;
697 cellinfo_of_cell(cell)->top_line = line;
702 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
704 if (onlycolumn || !IsMultiColumn(cell))
705 row_info[row_of_cell(cell)].bottom_line = line;
707 cellinfo_of_cell(cell)->bottom_line = line;
712 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
714 if (onlycolumn || !IsMultiColumn(cell))
715 column_info[column_of_cell(cell)].left_line = line;
717 cellinfo_of_cell(cell)->left_line = line;
722 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
724 if (onlycolumn || !IsMultiColumn(cell))
725 column_info[right_column_of_cell(cell)].right_line = line;
727 cellinfo_of_cell(cell)->right_line = line;
732 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
734 if (!onlycolumn && IsMultiColumn(cell))
735 return cellinfo_of_cell(cell)->alignment;
737 return column_info[column_of_cell(cell)].alignment;
741 LyXTabular::VAlignment
742 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
744 if (!onlycolumn && IsMultiColumn(cell))
745 return cellinfo_of_cell(cell)->valignment;
747 return column_info[column_of_cell(cell)].valignment;
751 string const LyXTabular::GetPWidth(int cell) const
753 if (IsMultiColumn(cell))
754 return cellinfo_of_cell(cell)->p_width;
755 return column_info[column_of_cell(cell)].p_width;
759 string const LyXTabular::GetColumnPWidth(int cell) const
761 return column_info[column_of_cell(cell)].p_width;
765 string const LyXTabular::GetMColumnPWidth(int cell) const
767 if (IsMultiColumn(cell))
768 return cellinfo_of_cell(cell)->p_width;
773 string const LyXTabular::GetAlignSpecial(int cell, int what) const
775 if (what == SET_SPECIAL_MULTI)
776 return cellinfo_of_cell(cell)->align_special;
777 return column_info[column_of_cell(cell)].align_special;
781 int LyXTabular::GetWidthOfCell(int cell) const
783 int const row = row_of_cell(cell);
784 int const column1 = column_of_cell(cell);
785 int const column2 = right_column_of_cell(cell);
787 for (int i = column1; i <= column2; ++i) {
788 result += cell_info[row][i].width_of_cell;
794 int LyXTabular::GetBeginningOfTextInCell(int cell) const
798 switch (GetAlignment(cell)){
799 case LYX_ALIGN_CENTER:
800 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
802 case LYX_ALIGN_RIGHT:
803 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
804 // + GetAdditionalWidth(cell);
806 default: /* LYX_ALIGN_LEFT: nothing :-) */
816 bool LyXTabular::IsFirstCellInRow(int cell) const
818 return column_of_cell(cell) == 0;
822 int LyXTabular::GetFirstCellInRow(int row) const
826 return cell_info[row][0].cellno;
829 bool LyXTabular::IsLastCellInRow(int cell) const
831 return (right_column_of_cell(cell) == (columns_ - 1));
835 int LyXTabular::GetLastCellInRow(int row) const
839 return cell_info[row][columns_-1].cellno;
843 bool LyXTabular::calculate_width_of_column(int column)
845 int const old_column_width = column_info[column].width_of_column;
848 for (int i = 0; i < rows_; ++i) {
849 maximum = max(cell_info[i][column].width_of_cell, maximum);
851 column_info[column].width_of_column = maximum;
852 return (column_info[column].width_of_column != old_column_width);
857 // calculate the with of the column without regarding REAL MultiColumn
858 // cells. This means MultiColumn-cells spanning more than 1 column.
860 bool LyXTabular::calculate_width_of_column_NMC(int column)
862 int const old_column_width = column_info[column].width_of_column;
864 for (int i = 0; i < rows_; ++i) {
865 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
866 (cell_info[i][column].width_of_cell > max)) {
867 max = cell_info[i][column].width_of_cell;
870 column_info[column].width_of_column = max;
871 return (column_info[column].width_of_column != old_column_width);
875 void LyXTabular::calculate_width_of_tabular()
877 width_of_tabular = 0;
878 for (int i = 0; i < columns_; ++i) {
879 width_of_tabular += column_info[i].width_of_column;
884 int LyXTabular::row_of_cell(int cell) const
886 if (cell >= numberofcells)
890 return rowofcell[cell];
894 int LyXTabular::column_of_cell(int cell) const
896 if (cell >= numberofcells)
900 return columnofcell[cell];
904 int LyXTabular::right_column_of_cell(int cell) const
906 int const row = row_of_cell(cell);
907 int column = column_of_cell(cell);
908 while (column < (columns_ - 1) &&
909 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
915 // Perfect case for a template... (Lgb)
918 string const write_attribute(string const & name, T const & t)
920 string str = " " + name + "=\"" + tostr(t) + "\"";
925 string const write_attribute(string const & name, bool const & b)
927 return write_attribute(name, int(b));
932 string const write_attribute(string const & name, int value)
934 string str = " " + name + "=\"" + tostr(value) + "\"";
939 string const write_attribute(string const & name, string const & value)
941 string str = " " + name + "=\"" + value + "\"";
946 string const write_attribute(string const & name, bool value)
948 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
956 string const tostr(LyXAlignment const & num)
961 case LYX_ALIGN_BLOCK:
965 case LYX_ALIGN_CENTER:
967 case LYX_ALIGN_RIGHT:
969 case LYX_ALIGN_LAYOUT:
971 case LYX_ALIGN_SPECIAL:
980 string const tostr(LyXTabular::VAlignment const & num)
983 case LyXTabular::LYX_VALIGN_TOP:
985 case LyXTabular::LYX_VALIGN_CENTER:
987 case LyXTabular::LYX_VALIGN_BOTTOM:
996 string const tostr(LyXTabular::BoxType const & num)
999 case LyXTabular::BOX_NONE:
1001 case LyXTabular::BOX_PARBOX:
1003 case LyXTabular::BOX_MINIPAGE:
1010 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1014 << write_attribute("version", 2)
1015 << write_attribute("rows", rows_)
1016 << write_attribute("columns", columns_)
1018 // global longtable options
1020 << write_attribute("rotate", tostr(rotate))
1021 << write_attribute("islongtable", tostr(is_long_tabular))
1022 << write_attribute("endhead", endhead)
1023 << write_attribute("endfirsthead", endfirsthead)
1024 << write_attribute("endfoot", endfoot)
1025 << write_attribute("endlastfoot", endlastfoot)
1027 for (int j = 0; j < columns_; ++j) {
1029 << write_attribute("alignment", tostr(column_info[j].alignment))
1030 << write_attribute("valignment", tostr(column_info[j].valignment))
1031 << write_attribute("leftline", tostr(column_info[j].left_line))
1032 << write_attribute("rightline", tostr(column_info[j].right_line))
1033 << write_attribute("width",
1034 VSpace(column_info[j].p_width)
1036 << write_attribute("special", column_info[j].align_special)
1039 for (int i = 0; i < rows_; ++i) {
1041 << write_attribute("topline", tostr(row_info[i].top_line))
1042 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1043 << write_attribute("newpage", tostr(row_info[i].newpage))
1045 for (int j = 0; j < columns_; ++j) {
1049 << write_attribute("alignment", tostr(column_info[j].alignment))
1050 << write_attribute("valignment", tostr(column_info[j].valignment))
1051 << write_attribute("leftline", tostr(column_info[j].left_line))
1052 << write_attribute("rightline", tostr(column_info[j].right_line))
1053 << write_attribute("width",
1054 VSpace(column_info[j].p_width)
1056 << write_attribute("special", column_info[j].align_special)
1063 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1064 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1065 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1066 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1067 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1068 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1069 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1070 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1071 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1072 << write_attribute("width", cell_info[i][j].p_width)
1073 << write_attribute("special", cell_info[i][j].align_special)
1075 os << "\\begin_inset ";
1076 cell_info[i][j].inset.Write(buf, os);
1077 os << "\n\\end_inset \n"
1085 os << "</lyxtabular>\n";
1091 // I would have liked a fromstr template a lot better. (Lgb)
1094 bool string2type(string const str, LyXAlignment & num)
1097 num = LYX_ALIGN_NONE;
1098 else if (str == "block")
1099 num = LYX_ALIGN_BLOCK;
1100 else if (str == "left")
1101 num = LYX_ALIGN_LEFT;
1102 else if (str == "center")
1103 num = LYX_ALIGN_CENTER;
1104 else if (str == "right")
1105 num = LYX_ALIGN_RIGHT;
1113 bool string2type(string const str, LyXTabular::VAlignment & num)
1116 num = LyXTabular::LYX_VALIGN_TOP;
1117 else if (str == "center")
1118 num = LyXTabular::LYX_VALIGN_CENTER;
1119 else if (str == "bottom")
1120 num = LyXTabular::LYX_VALIGN_BOTTOM;
1128 bool string2type(string const str, LyXTabular::BoxType & num)
1131 num = LyXTabular::BOX_NONE;
1132 else if (str == "parbox")
1133 num = LyXTabular::BOX_PARBOX;
1134 else if (str == "minipage")
1135 num = LyXTabular::BOX_MINIPAGE;
1143 bool string2type(string const str, bool & num)
1147 else if (str == "false")
1155 bool getTokenValue(string const & str, const char * token, string & ret)
1157 size_t token_length = strlen(token);
1158 string::size_type pos = str.find(token);
1160 if (pos == string::npos || pos+token_length+1 >= str.length()
1161 || str[pos+token_length] != '=')
1164 pos += token_length + 1;
1166 if ((ch != '"') && (ch != '\'')) { // only read till next space
1170 while((pos < str.length() - 1) && (str[++pos] != ch))
1177 bool getTokenValue(string const & str, const char * token, int & num)
1180 if (!getTokenValue(str, token, tmp))
1182 num = strToInt(tmp);
1187 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1190 if (!getTokenValue(str, token, tmp))
1192 return string2type(tmp, num);
1196 bool getTokenValue(string const & str, const char * token,
1197 LyXTabular::VAlignment & num)
1200 if (!getTokenValue(str, token, tmp))
1202 return string2type(tmp, num);
1206 bool getTokenValue(string const & str, const char * token,
1207 LyXTabular::BoxType & num)
1210 if (!getTokenValue(str, token, tmp))
1212 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)
1229 while (str.empty()) {
1231 if (!str.empty() && str[str.length() - 1] == '\r')
1232 str.erase(str.length() - 1);
1239 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1242 istream & is = lex.getStream();
1244 l_getline(is, line);
1245 if (!prefixIs(line, "<lyxtabular ")
1246 && !prefixIs(line, "<LyXTabular ")) {
1247 OldFormatRead(lex, line);
1252 if (!getTokenValue(line, "version", version))
1255 ReadOld(buf, is, lex, line);
1256 else if (version == 2)
1257 ReadNew(buf, is, lex, line);
1261 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1262 LyXLex & lex, string const & l)
1266 if (!getTokenValue(line, "rows", rows_arg))
1269 if (!getTokenValue(line, "columns", columns_arg))
1271 Init(rows_arg, columns_arg);
1272 l_getline(is, line);
1273 if (!prefixIs(line, "<features")) {
1274 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1275 line << ")" << endl;
1278 getTokenValue(line, "rotate", rotate);
1279 getTokenValue(line, "islongtable", is_long_tabular);
1280 getTokenValue(line, "endhead", endhead);
1281 getTokenValue(line, "endfirsthead", endfirsthead);
1282 getTokenValue(line, "endfoot", endfoot);
1283 getTokenValue(line, "endlastfoot", endlastfoot);
1285 for (int j = 0; j < columns_; ++j) {
1287 if (!prefixIs(line,"<column")) {
1288 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1289 line << ")" << endl;
1292 getTokenValue(line, "alignment", column_info[j].alignment);
1293 getTokenValue(line, "valignment", column_info[j].valignment);
1294 getTokenValue(line, "leftline", column_info[j].left_line);
1295 getTokenValue(line, "rightline", column_info[j].right_line);
1296 getTokenValue(line, "width", column_info[j].p_width);
1297 getTokenValue(line, "special", column_info[j].align_special);
1300 for (int i = 0; i < rows_; ++i) {
1301 l_getline(is, line);
1302 if (!prefixIs(line, "<row")) {
1303 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1304 line << ")" << endl;
1307 getTokenValue(line, "topline", row_info[i].top_line);
1308 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1309 getTokenValue(line, "newpage", row_info[i].newpage);
1310 for (int j = 0; j < columns_; ++j) {
1311 l_getline(is, line);
1312 if (!prefixIs(line, "<cell")) {
1313 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1314 line << ")" << endl;
1317 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1318 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1319 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1320 getTokenValue(line, "topline", cell_info[i][j].top_line);
1321 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1322 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1323 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1324 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1325 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1326 getTokenValue(line, "width", cell_info[i][j].p_width);
1327 getTokenValue(line, "special", cell_info[i][j].align_special);
1328 l_getline(is, line);
1329 if (prefixIs(line, "\\begin_inset")) {
1330 cell_info[i][j].inset.Read(buf, lex);
1331 l_getline(is, line);
1333 if (!prefixIs(line, "</cell>")) {
1334 lyxerr << "Wrong tabular format (expected </cell> got" <<
1335 line << ")" << endl;
1339 l_getline(is, line);
1340 if (!prefixIs(line, "</row>")) {
1341 lyxerr << "Wrong tabular format (expected </row> got" <<
1342 line << ")" << endl;
1346 while (!prefixIs(line, "</lyxtabular>")) {
1347 l_getline(is, line);
1349 set_row_column_number_info();
1353 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1359 int columns_arg = 0;
1360 int is_long_tabular_arg = false;
1361 int rotate_arg = false;
1370 istream & is = lex.getStream();
1373 version = lyx::atoi(s.substr(8, string::npos));
1377 vector<int> cont_row_info;
1380 lyxerr << "Tabular format < 5 is not supported anymore\n"
1381 "Get an older version of LyX (< 1.1.x) for conversion!"
1383 WriteAlert(_("Warning:"),
1384 _("Tabular format < 5 is not supported anymore\n"),
1385 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1387 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1388 >> rotate_arg >> a >> b >> c >> d;
1390 is >> rows_arg >> columns_arg;
1391 Init(rows_arg, columns_arg);
1392 cont_row_info = vector<int>(rows_arg);
1393 SetLongTabular(is_long_tabular_arg);
1394 SetRotateTabular(rotate_arg);
1396 for (i = 0; i < rows_; ++i) {
1398 cont_row_info[i] = false;
1400 for (i = 0; i < columns_; ++i) {
1403 for (i = 0; i < rows_; ++i) {
1404 for (j = 0; j < columns_; ++j) {
1409 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1410 >> rotate_arg >> a >> b >> c >> d;
1411 Init(rows_arg, columns_arg);
1412 cont_row_info = vector<int>(rows_arg);
1413 SetLongTabular(is_long_tabular_arg);
1414 SetRotateTabular(rotate_arg);
1416 endfirsthead = b + 1;
1418 endlastfoot = d + 1;
1419 for (i = 0; i < rows_; ++i) {
1420 a = b = c = d = e = f = g = 0;
1421 is >> a >> b >> c >> d;
1422 row_info[i].top_line = a;
1423 row_info[i].bottom_line = b;
1424 cont_row_info[i] = c;
1425 row_info[i].newpage = d;
1427 for (i = 0; i < columns_; ++i) {
1432 char ch; // skip '"'
1435 // ignore is buggy but we will use it later (Lgb)
1436 is.ignore(); // skip '"'
1438 getline(is, s1, '"');
1440 is >> ch; // skip '"'
1442 // ignore is buggy but we will use it later (Lgb)
1443 is.ignore(); // skip '"'
1445 getline(is, s2, '"');
1446 column_info[i].alignment = static_cast<LyXAlignment>(a);
1447 column_info[i].left_line = b;
1448 column_info[i].right_line = c;
1449 column_info[i].p_width = s1;
1450 column_info[i].align_special = s2;
1452 for (i = 0; i < rows_; ++i) {
1453 for (j = 0; j < columns_; ++j) {
1456 is >> a >> b >> c >> d >> e >> f >> g;
1459 is >> ch; // skip '"'
1461 // ignore is buggy but we will use it later (Lgb)
1462 is.ignore(); // skip '"'
1464 getline(is, s1, '"');
1466 is >> ch; // skip '"'
1468 // ignore is buggy but we will use it later (Lgb)
1469 is.ignore(); // skip '"'
1471 getline(is, s2, '"');
1472 cell_info[i][j].multicolumn = static_cast<char>(a);
1473 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1474 cell_info[i][j].top_line = static_cast<char>(c);
1475 cell_info[i][j].bottom_line = static_cast<char>(d);
1476 cell_info[i][j].left_line = column_info[j].left_line;
1477 cell_info[i][j].right_line = column_info[j].right_line;
1478 cell_info[i][j].rotate = static_cast<bool>(f);
1479 cell_info[i][j].usebox = static_cast<BoxType>(g);
1480 cell_info[i][j].align_special = s1;
1481 cell_info[i][j].p_width = s2;
1485 set_row_column_number_info(true);
1487 LyXParagraph * par = new LyXParagraph;
1488 LyXParagraph * return_par = 0;
1490 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1491 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1496 LyXFont font(LyXFont::ALL_INHERIT);
1497 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1499 while (lex.IsOK()) {
1501 string const token = lex.GetString();
1504 if (token == "\\layout"
1505 || token == "\\end_float"
1506 || token == "\\end_deeper") {
1507 lex.pushToken(token);
1510 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1522 lex.pushToken(token);
1526 lex.printError("New Paragraph allocated! This should not happen!");
1527 lex.pushToken(token);
1533 // now we have the par we should fill the insets with this!
1535 InsetText * inset = GetCellInset(cell);
1539 for (int i = 0; i < par->Last(); ++i) {
1541 for (int i = 0; i < par->size(); ++i) {
1543 if (par->IsNewline(i)) {
1545 if (cell > GetNumberOfCells()) {
1546 lyxerr << "Some error in reading old table format occured!" <<
1547 endl << "Terminating when reading cell[" << cell << "]!" <<
1552 row = row_of_cell(cell);
1553 if (cont_row_info[row]) {
1555 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1556 while(!IsFirstCellInRow(--cell));
1558 inset = GetCellInset(cell);
1561 inset = GetCellInset(cell);
1562 row = row_of_cell(cell);
1563 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1565 // insert a space instead
1567 par->InsertChar(i, ' ');
1570 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1572 inset->par->InsertFromMinibuffer(inset->par->Last());
1574 inset->par->InsertFromMinibuffer(inset->par->size());
1582 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1584 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1585 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1589 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1591 int const row = row_of_cell(cell);
1592 int const column = column_of_cell(cell);
1593 return &cell_info[row][column];
1597 void LyXTabular::SetMultiColumn(int cell, int number)
1599 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1600 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1601 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1602 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1603 for (number--; number > 0; --number) {
1604 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1606 set_row_column_number_info();
1610 int LyXTabular::cells_in_multicolumn(int cell) const
1612 int const row = row_of_cell(cell);
1613 int column = column_of_cell(cell);
1616 while ((column < columns_) &&
1617 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1626 int LyXTabular::UnsetMultiColumn(int cell)
1628 int const row = row_of_cell(cell);
1629 int column = column_of_cell(cell);
1633 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1634 cell_info[row][column].multicolumn = CELL_NORMAL;
1636 while ((column < columns_) &&
1637 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1639 cell_info[row][column].multicolumn = CELL_NORMAL;
1644 set_row_column_number_info();
1649 void LyXTabular::SetLongTabular(bool what)
1651 is_long_tabular = what;
1655 bool LyXTabular::IsLongTabular() const
1657 return is_long_tabular;
1661 void LyXTabular::SetRotateTabular(bool flag)
1667 bool LyXTabular::GetRotateTabular() const
1673 void LyXTabular::SetRotateCell(int cell, bool flag)
1675 cellinfo_of_cell(cell)->rotate = flag;
1679 bool LyXTabular::GetRotateCell(int cell) const
1681 return cellinfo_of_cell(cell)->rotate;
1685 bool LyXTabular::NeedRotating() const
1689 for (int i = 0; i < rows_; ++i) {
1690 for (int j = 0; j < columns_; ++j) {
1691 if (cell_info[i][j].rotate)
1699 bool LyXTabular::IsLastCell(int cell) const
1701 if ((cell + 1) < GetNumberOfCells())
1707 int LyXTabular::GetCellAbove(int cell) const
1709 if (row_of_cell(cell) > 0)
1710 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1715 int LyXTabular::GetCellBelow(int cell) const
1717 if (row_of_cell(cell) + 1 < rows_)
1718 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1723 int LyXTabular::GetLastCellAbove(int cell) const
1725 if (row_of_cell(cell) <= 0)
1727 if (!IsMultiColumn(cell))
1728 return GetCellAbove(cell);
1729 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1733 int LyXTabular::GetLastCellBelow(int cell) const
1735 if (row_of_cell(cell) + 1 >= rows_)
1737 if (!IsMultiColumn(cell))
1738 return GetCellBelow(cell);
1739 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1743 int LyXTabular::GetCellNumber(int row, int column) const
1745 if (column >= columns_)
1746 column = columns_ - 1;
1747 else if (column < 0)
1754 return cell_info[row][column].cellno;
1758 void LyXTabular::SetUsebox(int cell, BoxType type)
1760 cellinfo_of_cell(cell)->usebox = type;
1764 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1766 if (column_info[column_of_cell(cell)].p_width.empty() &&
1767 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1769 if (cellinfo_of_cell(cell)->usebox > 1)
1770 return cellinfo_of_cell(cell)->usebox;
1771 return UseParbox(cell);
1775 void LyXTabular::SetLTHead(int cell, bool first)
1777 int const row = row_of_cell(cell);
1778 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1781 if (endfirsthead == val)
1794 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1797 if (abs(endhead) > rows_)
1799 return (row_of_cell(cell) == abs(endhead) - 1);
1803 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1806 if (abs(endfirsthead) > rows_)
1808 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1812 void LyXTabular::SetLTFoot(int cell, bool last)
1814 int const row = row_of_cell(cell);
1815 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1818 if (endlastfoot == val)
1831 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1834 if ((endfoot + 1) > rows_)
1836 return (row_of_cell(cell) == abs(endfoot) - 1);
1840 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1843 if (abs(endlastfoot) > rows_)
1845 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1849 void LyXTabular::SetLTNewPage(int cell, bool what)
1851 row_info[row_of_cell(cell)].newpage = what;
1855 bool LyXTabular::GetLTNewPage(int cell) const
1857 return row_info[row_of_cell(cell)].newpage;
1861 bool LyXTabular::SetAscentOfRow(int row, int height)
1863 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1865 row_info[row].ascent_of_row = height;
1870 bool LyXTabular::SetDescentOfRow(int row, int height)
1872 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1874 row_info[row].descent_of_row = height;
1879 int LyXTabular::GetAscentOfRow(int row) const
1883 return row_info[row].ascent_of_row;
1887 int LyXTabular::GetDescentOfRow(int row) const
1891 return row_info[row].descent_of_row;
1895 int LyXTabular::GetHeightOfTabular() const
1899 for (int row = 0; row < rows_; ++row)
1900 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1901 GetAdditionalHeight(GetCellNumber(row, 0));
1906 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1908 if ((row >= rows_) || (column >= columns_))
1910 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1914 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1916 if ((row < 0) || (row >= rows_))
1919 int const fcell = GetFirstCellInRow(row);
1920 int const n = NumberOfCellsInRow(fcell) + fcell;
1923 for (int i = fcell; i < n; ++i) {
1927 if (tmp == (n - fcell)){
1930 for (int i = fcell; i < n; ++i) {
1933 << column_of_cell(i) + 1
1935 << right_column_of_cell(i) + 1
1947 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1949 if ((row < 0) || (row >= rows_))
1952 int const fcell = GetFirstCellInRow(row);
1953 int const n = NumberOfCellsInRow(fcell) + fcell;
1956 for (int i = fcell; i < n; ++i) {
1960 if (tmp == (n-fcell)){
1963 for (int i = fcell; i < n; ++i) {
1964 if (BottomLine(i)) {
1966 << column_of_cell(i) + 1
1968 << right_column_of_cell(i) + 1
1980 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1984 if (GetRotateCell(cell)) {
1985 os << "\\begin{sideways}\n";
1988 if (IsMultiColumn(cell)) {
1989 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1990 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1991 os << cellinfo_of_cell(cell)->align_special << "}{";
1995 if (!GetPWidth(cell).empty()) {
1996 #warning Jürgen, are these alignments correct? (Lgb)
1997 switch (GetVAlignment(cell)) {
1998 case LYX_VALIGN_TOP:
2001 case LYX_VALIGN_CENTER:
2004 case LYX_VALIGN_BOTTOM:
2008 os << "{" << GetPWidth(cell) << '}';
2010 switch (GetAlignment(cell)) {
2011 case LYX_ALIGN_LEFT:
2014 case LYX_ALIGN_RIGHT:
2022 if (RightLine(cell))
2024 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2030 if (GetUsebox(cell) == BOX_PARBOX) {
2032 switch (GetVAlignment(cell)) {
2033 case LYX_VALIGN_TOP:
2036 case LYX_VALIGN_CENTER:
2039 case LYX_VALIGN_BOTTOM:
2043 os << "]{" << GetPWidth(cell) << "}{";
2044 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2045 os << "\\begin{minipage}[";
2046 switch (GetVAlignment(cell)) {
2047 #warning Jürgen, are these alignments correct? (Lgb)
2048 case LYX_VALIGN_TOP:
2051 case LYX_VALIGN_CENTER:
2054 case LYX_VALIGN_BOTTOM:
2058 os << "]{" << GetPWidth(cell) << "}\n";
2065 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2070 if (GetUsebox(cell) == BOX_PARBOX)
2072 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2073 os << "%\n\\end{minipage}";
2076 if (IsMultiColumn(cell)){
2079 if (GetRotateCell(cell)) {
2080 os << "%\n\\end{sideways}";
2087 int LyXTabular::Latex(Buffer const * buf,
2088 ostream & os, bool fragile, bool fp) const
2093 //+---------------------------------------------------------------------
2094 //+ first the opening preamble +
2095 //+---------------------------------------------------------------------
2098 os << "\\begin{sideways}\n";
2101 if (is_long_tabular)
2102 os << "\\begin{longtable}{";
2104 os << "\\begin{tabular}{";
2105 for (int i = 0; i < columns_; ++i) {
2106 if (column_info[i].left_line)
2108 if (!column_info[i].align_special.empty()) {
2109 os << column_info[i].align_special;
2110 } else if (!column_info[i].p_width.empty()) {
2111 switch (column_info[i].valignment) {
2112 #warning Jürgen, are these alignments correct? (Lgb)
2113 case LYX_VALIGN_TOP:
2116 case LYX_VALIGN_CENTER:
2119 case LYX_VALIGN_BOTTOM:
2124 << column_info[i].p_width
2127 switch (column_info[i].alignment) {
2128 case LYX_ALIGN_LEFT:
2131 case LYX_ALIGN_RIGHT:
2139 if (column_info[i].right_line)
2145 //+---------------------------------------------------------------------
2146 //+ the single row and columns (cells) +
2147 //+---------------------------------------------------------------------
2149 for (int i = 0; i < rows_; ++i) {
2150 ret += TeXTopHLine(os, i);
2152 if (IsLongTabular()) {
2153 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2154 os << "\\endhead\n";
2157 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2158 os << "\\endfirsthead\n";
2161 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2162 os << "\\endfoot\n";
2165 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2166 os << "\\endlastfoot\n";
2171 ret += TeXBottomHLine(os, i-1);
2172 ret += TeXTopHLine(os, i);
2174 for (int j = 0; j < columns_; ++j) {
2175 if (IsPartOfMultiColumn(i,j))
2177 ret += TeXCellPreamble(os, cell);
2178 InsetText * inset = GetCellInset(cell);
2180 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2182 inset->par->Last() > 0 && GetPWidth(cell).empty();
2184 inset->par->size() > 0 && GetPWidth(cell).empty();
2188 ret += inset->Latex(buf, os, fragile, fp);
2192 ret += TeXCellPostamble(os, cell);
2193 if (!IsLastCellInRow(cell)) { // not last cell in row
2200 ret += TeXBottomHLine(os, i);
2202 if (IsLongTabular()) {
2203 if ((endhead > 0) && (i == (endhead - 1))) {
2204 os << "\\endhead\n";
2207 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2208 os << "\\endfirsthead\n";
2211 if ((endfoot > 0) && (i == (endfoot - 1))) {
2212 os << "\\endfoot\n";
2215 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2216 os << "\\endlastfoot\n";
2220 // ret += TeXBottomHLine(os, i);
2221 if (row_info[i].newpage) {
2222 os << "\\newpage\n";
2228 //+---------------------------------------------------------------------
2229 //+ the closing of the tabular +
2230 //+---------------------------------------------------------------------
2232 if (is_long_tabular)
2233 os << "\\end{longtable}";
2235 os << "\\end{tabular}";
2237 os << "\n\\end{sideways}";
2245 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2249 //+---------------------------------------------------------------------
2250 //+ first the opening preamble +
2251 //+---------------------------------------------------------------------
2253 os << "<tgroup cols=\"" << columns_
2254 << "\" colsep=\"1\" rowsep=\"1\">\n";
2256 for (int i = 0; i < columns_; ++i) {
2257 os << "<colspec colname=\"col" << i << "\" align=\"";
2258 switch (column_info[i].alignment) {
2259 case LYX_ALIGN_LEFT:
2262 case LYX_ALIGN_RIGHT:
2273 //+---------------------------------------------------------------------
2274 //+ the single row and columns (cells) +
2275 //+---------------------------------------------------------------------
2279 for (int i = 0; i < rows_; ++i) {
2281 for (int j = 0; j < columns_; ++j) {
2282 if (IsPartOfMultiColumn(i, j))
2285 os << "<entry align=\"";
2286 switch (GetAlignment(cell)) {
2287 case LYX_ALIGN_LEFT:
2290 case LYX_ALIGN_RIGHT:
2298 os << "\" valign=\"";
2299 switch (GetVAlignment(cell)) {
2300 case LYX_VALIGN_TOP:
2303 case LYX_VALIGN_BOTTOM:
2306 case LYX_VALIGN_CENTER:
2311 if (IsMultiColumn(cell)) {
2312 os << " namest=\"col" << j << "\" ";
2313 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2317 ret += GetCellInset(cell)->DocBook(buf, os);
2324 //+---------------------------------------------------------------------
2325 //+ the closing of the tabular +
2326 //+---------------------------------------------------------------------
2338 void print_n_chars(ostream & os, unsigned char ch, int n)
2340 os << string(n, ch);
2346 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2347 vector<unsigned int> const & clen) const
2349 int const fcell = GetFirstCellInRow(row);
2350 int const n = NumberOfCellsInRow(fcell) + fcell;
2353 for (int i = fcell; i < n; ++i) {
2363 for (int i = fcell; i < n; ++i) {
2374 int column = column_of_cell(i);
2375 int len = clen[column];
2376 while(IsPartOfMultiColumn(row, ++column))
2377 len += clen[column] + 4;
2378 print_n_chars(os, ch, len);
2393 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2394 vector<unsigned int> const & clen) const
2396 int const fcell = GetFirstCellInRow(row);
2397 int const n = NumberOfCellsInRow(fcell) + fcell;
2400 for (int i = fcell; i < n; ++i) {
2401 if (BottomLine(i)) {
2410 for (int i = fcell; i < n; ++i) {
2411 if (BottomLine(i)) {
2421 int column = column_of_cell(i);
2422 int len = clen[column];
2423 while(IsPartOfMultiColumn(row, ++column))
2424 len += clen[column] + 4;
2425 print_n_chars(os, ch, len);
2426 if (BottomLine(i)) {
2440 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2441 int cell, int row, int column,
2442 vector<unsigned int> const & clen) const
2445 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2452 unsigned int len1 = sstr.str().length();
2453 unsigned int len2 = clen[column];
2454 while(IsPartOfMultiColumn(row, ++column))
2455 len2 += clen[column] + 4;
2458 switch (GetAlignment(cell)) {
2460 case LYX_ALIGN_LEFT:
2463 case LYX_ALIGN_RIGHT:
2467 case LYX_ALIGN_CENTER:
2473 for (unsigned int i = 0; i < len1; ++i)
2476 for (unsigned int i = 0; i < len2; ++i)
2478 if (RightLine(cell))
2487 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2491 //+---------------------------------------------------------------------
2492 //+ first calculate the width of the single columns +
2493 //+---------------------------------------------------------------------
2494 vector<unsigned int> clen(columns_);
2496 // first all non (real) multicolumn cells!
2497 for (int j = 0; j < columns_; ++j) {
2499 for (int i = 0; i < rows_; ++i) {
2500 int cell = GetCellNumber(i, j);
2501 if (IsMultiColumn(cell, true))
2504 GetCellInset(cell)->Ascii(buf, sstr, 0);
2505 if (clen[j] < sstr.str().length())
2506 clen[j] = sstr.str().length();
2509 // then all (real) multicolumn cells!
2510 for (int j = 0; j < columns_; ++j) {
2511 for (int i = 0; i < rows_; ++i) {
2512 int cell = GetCellNumber(i, j);
2513 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2516 GetCellInset(cell)->Ascii(buf, sstr, 0);
2517 int len = int(sstr.str().length());
2518 int const n = cells_in_multicolumn(cell);
2519 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2521 if (len > int(clen[j + n - 1]))
2522 clen[j + n - 1] = len;
2526 for (int i = 0; i < rows_; ++i) {
2527 AsciiTopHLine(os, i, clen);
2528 for (int j = 0; j < columns_; ++j) {
2529 if (IsPartOfMultiColumn(i,j))
2531 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2535 AsciiBottomHLine(os, i, clen);
2541 InsetText * LyXTabular::GetCellInset(int cell) const
2543 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2547 InsetText * LyXTabular::GetCellInset(int row, int column) const
2549 return GetCellInset(GetCellNumber(row, column));
2553 void LyXTabular::Validate(LaTeXFeatures & features) const
2555 if (IsLongTabular())
2556 features.longtable = true;
2558 features.rotating = true;
2559 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2560 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2561 features.array = true;
2562 GetCellInset(cell)->Validate(features);
2568 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2570 LyXParagraph * par = GetCellInset(cell)->par;
2572 for (; par; par = par->next_) {
2573 for (int i = 0; i < par->Last(); ++i) {
2574 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2581 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2583 LyXParagraph * par = GetCellInset(cell)->par;
2585 for (; par; par = par->next()) {
2586 for (int i = 0; i < par->size(); ++i) {
2587 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)