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) {
1047 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1048 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1049 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1050 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1051 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1052 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1053 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1054 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1055 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1056 << write_attribute("width", cell_info[i][j].p_width)
1057 << write_attribute("special", cell_info[i][j].align_special)
1059 os << "\\begin_inset ";
1060 cell_info[i][j].inset.Write(buf, os);
1061 os << "\n\\end_inset \n"
1066 os << "</lyxtabular>\n";
1072 // I would have liked a fromstr template a lot better. (Lgb)
1075 bool string2type(string const str, LyXAlignment & num)
1078 num = LYX_ALIGN_NONE;
1079 else if (str == "block")
1080 num = LYX_ALIGN_BLOCK;
1081 else if (str == "left")
1082 num = LYX_ALIGN_LEFT;
1083 else if (str == "center")
1084 num = LYX_ALIGN_CENTER;
1085 else if (str == "right")
1086 num = LYX_ALIGN_RIGHT;
1094 bool string2type(string const str, LyXTabular::VAlignment & num)
1097 num = LyXTabular::LYX_VALIGN_TOP;
1098 else if (str == "center")
1099 num = LyXTabular::LYX_VALIGN_CENTER;
1100 else if (str == "bottom")
1101 num = LyXTabular::LYX_VALIGN_BOTTOM;
1109 bool string2type(string const str, LyXTabular::BoxType & num)
1112 num = LyXTabular::BOX_NONE;
1113 else if (str == "parbox")
1114 num = LyXTabular::BOX_PARBOX;
1115 else if (str == "minipage")
1116 num = LyXTabular::BOX_MINIPAGE;
1124 bool string2type(string const str, bool & num)
1128 else if (str == "false")
1136 bool getTokenValue(string const & str, const char * token, string & ret)
1138 size_t token_length = strlen(token);
1139 string::size_type pos = str.find(token);
1141 if (pos == string::npos || pos+token_length+1 >= str.length()
1142 || str[pos+token_length] != '=')
1145 pos += token_length + 1;
1147 if ((ch != '"') && (ch != '\'')) { // only read till next space
1151 while((pos < str.length() - 1) && (str[++pos] != ch))
1158 bool getTokenValue(string const & str, const char * token, int & num)
1161 if (!getTokenValue(str, token, tmp))
1163 num = strToInt(tmp);
1168 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1171 if (!getTokenValue(str, token, tmp))
1173 return string2type(tmp, num);
1177 bool getTokenValue(string const & str, const char * token,
1178 LyXTabular::VAlignment & num)
1181 if (!getTokenValue(str, token, tmp))
1183 return string2type(tmp, num);
1187 bool getTokenValue(string const & str, const char * token,
1188 LyXTabular::BoxType & num)
1191 if (!getTokenValue(str, token, tmp))
1193 return string2type(tmp, num);
1197 bool getTokenValue(string const & str, const char * token, bool & flag)
1200 if (!getTokenValue(str, token, tmp))
1202 return string2type(tmp, flag);
1207 void l_getline(istream & is, string & str)
1210 while (str.empty()) {
1212 if (!str.empty() && str[str.length() - 1] == '\r')
1213 str.erase(str.length() - 1);
1220 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1223 istream & is = lex.getStream();
1225 l_getline(is, line);
1226 if (!prefixIs(line, "<lyxtabular ")
1227 && !prefixIs(line, "<LyXTabular ")) {
1228 OldFormatRead(lex, line);
1233 if (!getTokenValue(line, "version", version))
1236 ReadOld(buf, is, lex, line);
1237 else if (version == 2)
1238 ReadNew(buf, is, lex, line);
1242 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1243 LyXLex & lex, string const & l)
1247 if (!getTokenValue(line, "rows", rows_arg))
1250 if (!getTokenValue(line, "columns", columns_arg))
1252 Init(rows_arg, columns_arg);
1253 l_getline(is, line);
1254 if (!prefixIs(line, "<features")) {
1255 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1256 line << ")" << endl;
1259 getTokenValue(line, "rotate", rotate);
1260 getTokenValue(line, "islongtable", is_long_tabular);
1261 getTokenValue(line, "endhead", endhead);
1262 getTokenValue(line, "endfirsthead", endfirsthead);
1263 getTokenValue(line, "endfoot", endfoot);
1264 getTokenValue(line, "endlastfoot", endlastfoot);
1266 for (int j = 0; j < columns_; ++j) {
1268 if (!prefixIs(line,"<column")) {
1269 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1270 line << ")" << endl;
1273 getTokenValue(line, "alignment", column_info[j].alignment);
1274 getTokenValue(line, "valignment", column_info[j].valignment);
1275 getTokenValue(line, "leftline", column_info[j].left_line);
1276 getTokenValue(line, "rightline", column_info[j].right_line);
1277 getTokenValue(line, "width", column_info[j].p_width);
1278 getTokenValue(line, "special", column_info[j].align_special);
1281 for (int i = 0; i < rows_; ++i) {
1282 l_getline(is, line);
1283 if (!prefixIs(line, "<row")) {
1284 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1285 line << ")" << endl;
1288 getTokenValue(line, "topline", row_info[i].top_line);
1289 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1290 getTokenValue(line, "newpage", row_info[i].newpage);
1291 for (int j = 0; j < columns_; ++j) {
1292 l_getline(is, line);
1293 if (!prefixIs(line, "<cell")) {
1294 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1295 line << ")" << endl;
1298 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1299 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1300 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1301 getTokenValue(line, "topline", cell_info[i][j].top_line);
1302 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1303 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1304 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1305 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1306 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1307 getTokenValue(line, "width", cell_info[i][j].p_width);
1308 getTokenValue(line, "special", cell_info[i][j].align_special);
1309 l_getline(is, line);
1310 if (prefixIs(line, "\\begin_inset")) {
1311 cell_info[i][j].inset.Read(buf, lex);
1312 l_getline(is, line);
1314 if (!prefixIs(line, "</cell>")) {
1315 lyxerr << "Wrong tabular format (expected </cell> got" <<
1316 line << ")" << endl;
1320 l_getline(is, line);
1321 if (!prefixIs(line, "</row>")) {
1322 lyxerr << "Wrong tabular format (expected </row> got" <<
1323 line << ")" << endl;
1327 while (!prefixIs(line, "</lyxtabular>")) {
1328 l_getline(is, line);
1330 set_row_column_number_info();
1334 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1340 int columns_arg = 0;
1341 int is_long_tabular_arg = false;
1342 int rotate_arg = false;
1351 istream & is = lex.getStream();
1354 version = lyx::atoi(s.substr(8, string::npos));
1358 vector<int> cont_row_info;
1361 lyxerr << "Tabular format < 5 is not supported anymore\n"
1362 "Get an older version of LyX (< 1.1.x) for conversion!"
1364 WriteAlert(_("Warning:"),
1365 _("Tabular format < 5 is not supported anymore\n"),
1366 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1368 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1369 >> rotate_arg >> a >> b >> c >> d;
1371 is >> rows_arg >> columns_arg;
1372 Init(rows_arg, columns_arg);
1373 cont_row_info = vector<int>(rows_arg);
1374 SetLongTabular(is_long_tabular_arg);
1375 SetRotateTabular(rotate_arg);
1377 for (i = 0; i < rows_; ++i) {
1379 cont_row_info[i] = false;
1381 for (i = 0; i < columns_; ++i) {
1384 for (i = 0; i < rows_; ++i) {
1385 for (j = 0; j < columns_; ++j) {
1390 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1391 >> rotate_arg >> a >> b >> c >> d;
1392 Init(rows_arg, columns_arg);
1393 cont_row_info = vector<int>(rows_arg);
1394 SetLongTabular(is_long_tabular_arg);
1395 SetRotateTabular(rotate_arg);
1397 endfirsthead = b + 1;
1399 endlastfoot = d + 1;
1400 for (i = 0; i < rows_; ++i) {
1401 a = b = c = d = e = f = g = 0;
1402 is >> a >> b >> c >> d;
1403 row_info[i].top_line = a;
1404 row_info[i].bottom_line = b;
1405 cont_row_info[i] = c;
1406 row_info[i].newpage = d;
1408 for (i = 0; i < columns_; ++i) {
1413 char ch; // skip '"'
1416 // ignore is buggy but we will use it later (Lgb)
1417 is.ignore(); // skip '"'
1419 getline(is, s1, '"');
1421 is >> ch; // skip '"'
1423 // ignore is buggy but we will use it later (Lgb)
1424 is.ignore(); // skip '"'
1426 getline(is, s2, '"');
1427 column_info[i].alignment = static_cast<LyXAlignment>(a);
1428 column_info[i].left_line = b;
1429 column_info[i].right_line = c;
1430 column_info[i].p_width = s1;
1431 column_info[i].align_special = s2;
1433 for (i = 0; i < rows_; ++i) {
1434 for (j = 0; j < columns_; ++j) {
1437 is >> a >> b >> c >> d >> e >> f >> g;
1440 is >> ch; // skip '"'
1442 // ignore is buggy but we will use it later (Lgb)
1443 is.ignore(); // skip '"'
1445 getline(is, s1, '"');
1447 is >> ch; // skip '"'
1449 // ignore is buggy but we will use it later (Lgb)
1450 is.ignore(); // skip '"'
1452 getline(is, s2, '"');
1453 cell_info[i][j].multicolumn = static_cast<char>(a);
1454 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1455 cell_info[i][j].top_line = static_cast<char>(c);
1456 cell_info[i][j].bottom_line = static_cast<char>(d);
1457 cell_info[i][j].left_line = column_info[j].left_line;
1458 cell_info[i][j].right_line = column_info[j].right_line;
1459 cell_info[i][j].rotate = static_cast<bool>(f);
1460 cell_info[i][j].usebox = static_cast<BoxType>(g);
1461 cell_info[i][j].align_special = s1;
1462 cell_info[i][j].p_width = s2;
1466 set_row_column_number_info(true);
1468 LyXParagraph * par = new LyXParagraph;
1469 LyXParagraph * return_par = 0;
1471 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1472 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1477 LyXFont font(LyXFont::ALL_INHERIT);
1478 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1480 while (lex.IsOK()) {
1482 string const token = lex.GetString();
1485 if (token == "\\layout"
1486 || token == "\\end_float"
1487 || token == "\\end_deeper") {
1488 lex.pushToken(token);
1491 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1503 lex.pushToken(token);
1507 lex.printError("New Paragraph allocated! This should not happen!");
1508 lex.pushToken(token);
1514 // now we have the par we should fill the insets with this!
1516 InsetText * inset = GetCellInset(cell);
1520 for (int i = 0; i < par->Last(); ++i)
1522 for (int i = 0; i < par->size(); ++i)
1525 if (par->IsNewline(i)) {
1527 if (cell > GetNumberOfCells()) {
1528 lyxerr << "Some error in reading old table format occured!" <<
1529 endl << "Terminating when reading cell[" << cell << "]!" <<
1534 row = row_of_cell(cell);
1535 if (cont_row_info[row]) {
1537 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1538 while(!IsFirstCellInRow(--cell));
1540 inset = GetCellInset(cell);
1543 inset = GetCellInset(cell);
1544 row = row_of_cell(cell);
1545 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1547 // insert a space instead
1549 par->InsertChar(i, ' ');
1552 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1554 inset->par->InsertFromMinibuffer(inset->par->Last());
1556 inset->par->InsertFromMinibuffer(inset->par->size());
1564 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1566 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1567 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1571 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1573 int const row = row_of_cell(cell);
1574 int const column = column_of_cell(cell);
1575 return &cell_info[row][column];
1579 void LyXTabular::SetMultiColumn(int cell, int number)
1581 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1582 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1583 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1584 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1585 for (number--; number > 0; --number) {
1586 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1588 set_row_column_number_info();
1592 int LyXTabular::cells_in_multicolumn(int cell) const
1594 int const row = row_of_cell(cell);
1595 int column = column_of_cell(cell);
1598 while ((column < columns_) &&
1599 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1608 int LyXTabular::UnsetMultiColumn(int cell)
1610 int const row = row_of_cell(cell);
1611 int column = column_of_cell(cell);
1615 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1616 cell_info[row][column].multicolumn = CELL_NORMAL;
1618 while ((column < columns_) &&
1619 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1621 cell_info[row][column].multicolumn = CELL_NORMAL;
1626 set_row_column_number_info();
1631 void LyXTabular::SetLongTabular(bool what)
1633 is_long_tabular = what;
1637 bool LyXTabular::IsLongTabular() const
1639 return is_long_tabular;
1643 void LyXTabular::SetRotateTabular(bool flag)
1649 bool LyXTabular::GetRotateTabular() const
1655 void LyXTabular::SetRotateCell(int cell, bool flag)
1657 cellinfo_of_cell(cell)->rotate = flag;
1661 bool LyXTabular::GetRotateCell(int cell) const
1663 return cellinfo_of_cell(cell)->rotate;
1667 bool LyXTabular::NeedRotating() const
1671 for (int i = 0; i < rows_; ++i) {
1672 for (int j = 0; j < columns_; ++j) {
1673 if (cell_info[i][j].rotate)
1681 bool LyXTabular::IsLastCell(int cell) const
1683 if ((cell + 1) < GetNumberOfCells())
1689 int LyXTabular::GetCellAbove(int cell) const
1691 if (row_of_cell(cell) > 0)
1692 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1697 int LyXTabular::GetCellBelow(int cell) const
1699 if (row_of_cell(cell) + 1 < rows_)
1700 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1705 int LyXTabular::GetLastCellAbove(int cell) const
1707 if (row_of_cell(cell) <= 0)
1709 if (!IsMultiColumn(cell))
1710 return GetCellAbove(cell);
1711 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1715 int LyXTabular::GetLastCellBelow(int cell) const
1717 if (row_of_cell(cell) + 1 >= rows_)
1719 if (!IsMultiColumn(cell))
1720 return GetCellBelow(cell);
1721 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1725 int LyXTabular::GetCellNumber(int row, int column) const
1727 if (column >= columns_)
1728 column = columns_ - 1;
1729 else if (column < 0)
1736 return cell_info[row][column].cellno;
1740 void LyXTabular::SetUsebox(int cell, BoxType type)
1742 cellinfo_of_cell(cell)->usebox = type;
1746 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1748 if (column_info[column_of_cell(cell)].p_width.empty() &&
1749 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1751 if (cellinfo_of_cell(cell)->usebox > 1)
1752 return cellinfo_of_cell(cell)->usebox;
1753 return UseParbox(cell);
1757 void LyXTabular::SetLTHead(int cell, bool first)
1759 int const row = row_of_cell(cell);
1760 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1763 if (endfirsthead == val)
1776 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1779 if (abs(endhead) > rows_)
1781 return (row_of_cell(cell) == abs(endhead) - 1);
1785 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1788 if (abs(endfirsthead) > rows_)
1790 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1794 void LyXTabular::SetLTFoot(int cell, bool last)
1796 int const row = row_of_cell(cell);
1797 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1800 if (endlastfoot == val)
1813 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1816 if ((endfoot + 1) > rows_)
1818 return (row_of_cell(cell) == abs(endfoot) - 1);
1822 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1825 if (abs(endlastfoot) > rows_)
1827 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1831 void LyXTabular::SetLTNewPage(int cell, bool what)
1833 row_info[row_of_cell(cell)].newpage = what;
1837 bool LyXTabular::GetLTNewPage(int cell) const
1839 return row_info[row_of_cell(cell)].newpage;
1843 bool LyXTabular::SetAscentOfRow(int row, int height)
1845 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1847 row_info[row].ascent_of_row = height;
1852 bool LyXTabular::SetDescentOfRow(int row, int height)
1854 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1856 row_info[row].descent_of_row = height;
1861 int LyXTabular::GetAscentOfRow(int row) const
1865 return row_info[row].ascent_of_row;
1869 int LyXTabular::GetDescentOfRow(int row) const
1873 return row_info[row].descent_of_row;
1877 int LyXTabular::GetHeightOfTabular() const
1881 for (int row = 0; row < rows_; ++row)
1882 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1883 GetAdditionalHeight(GetCellNumber(row, 0));
1888 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1890 if ((row >= rows_) || (column >= columns_))
1892 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1896 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1898 if ((row < 0) || (row >= rows_))
1901 int const fcell = GetFirstCellInRow(row);
1902 int const n = NumberOfCellsInRow(fcell) + fcell;
1905 for (int i = fcell; i < n; ++i) {
1909 if (tmp == (n - fcell)){
1912 for (int i = fcell; i < n; ++i) {
1915 << column_of_cell(i) + 1
1917 << right_column_of_cell(i) + 1
1929 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1931 if ((row < 0) || (row >= rows_))
1934 int const fcell = GetFirstCellInRow(row);
1935 int const n = NumberOfCellsInRow(fcell) + fcell;
1938 for (int i = fcell; i < n; ++i) {
1942 if (tmp == (n-fcell)){
1945 for (int i = fcell; i < n; ++i) {
1946 if (BottomLine(i)) {
1948 << column_of_cell(i) + 1
1950 << right_column_of_cell(i) + 1
1962 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1966 if (GetRotateCell(cell)) {
1967 os << "\\begin{sideways}\n";
1970 if (IsMultiColumn(cell)) {
1971 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1972 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1973 os << cellinfo_of_cell(cell)->align_special << "}{";
1977 if (!GetPWidth(cell).empty()) {
1978 switch (GetVAlignment(cell)) {
1979 case LYX_VALIGN_TOP:
1982 case LYX_VALIGN_CENTER:
1985 case LYX_VALIGN_BOTTOM:
1989 os << "{" << GetPWidth(cell) << '}';
1991 switch (GetAlignment(cell)) {
1992 case LYX_ALIGN_LEFT:
1995 case LYX_ALIGN_RIGHT:
2003 if (RightLine(cell))
2005 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2011 if (GetUsebox(cell) == BOX_PARBOX) {
2013 switch (GetVAlignment(cell)) {
2014 case LYX_VALIGN_TOP:
2017 case LYX_VALIGN_CENTER:
2020 case LYX_VALIGN_BOTTOM:
2024 os << "]{" << GetPWidth(cell) << "}{";
2025 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2026 os << "\\begin{minipage}[";
2027 switch (GetVAlignment(cell)) {
2028 case LYX_VALIGN_TOP:
2031 case LYX_VALIGN_CENTER:
2034 case LYX_VALIGN_BOTTOM:
2038 os << "]{" << GetPWidth(cell) << "}\n";
2045 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2050 if (GetUsebox(cell) == BOX_PARBOX)
2052 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2053 os << "%\n\\end{minipage}";
2056 if (IsMultiColumn(cell)){
2059 if (GetRotateCell(cell)) {
2060 os << "%\n\\end{sideways}";
2067 int LyXTabular::Latex(Buffer const * buf,
2068 ostream & os, bool fragile, bool fp) const
2073 //+---------------------------------------------------------------------
2074 //+ first the opening preamble +
2075 //+---------------------------------------------------------------------
2078 os << "\\begin{sideways}\n";
2081 if (is_long_tabular)
2082 os << "\\begin{longtable}{";
2084 os << "\\begin{tabular}{";
2085 for (int i = 0; i < columns_; ++i) {
2086 if (column_info[i].left_line)
2088 if (!column_info[i].align_special.empty()) {
2089 os << column_info[i].align_special;
2090 } else if (!column_info[i].p_width.empty()) {
2091 switch (column_info[i].valignment) {
2092 case LYX_VALIGN_TOP:
2095 case LYX_VALIGN_CENTER:
2098 case LYX_VALIGN_BOTTOM:
2103 << column_info[i].p_width
2106 switch (column_info[i].alignment) {
2107 case LYX_ALIGN_LEFT:
2110 case LYX_ALIGN_RIGHT:
2118 if (column_info[i].right_line)
2124 //+---------------------------------------------------------------------
2125 //+ the single row and columns (cells) +
2126 //+---------------------------------------------------------------------
2128 for (int i = 0; i < rows_; ++i) {
2129 ret += TeXTopHLine(os, i);
2131 if (IsLongTabular()) {
2132 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2133 os << "\\endhead\n";
2136 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2137 os << "\\endfirsthead\n";
2140 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2141 os << "\\endfoot\n";
2144 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2145 os << "\\endlastfoot\n";
2150 ret += TeXBottomHLine(os, i-1);
2151 ret += TeXTopHLine(os, i);
2153 for (int j = 0; j < columns_; ++j) {
2154 if (IsPartOfMultiColumn(i,j))
2156 ret += TeXCellPreamble(os, cell);
2157 InsetText * inset = GetCellInset(cell);
2159 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2161 inset->par->Last() > 0 && GetPWidth(cell).empty();
2163 inset->par->size() > 0 && GetPWidth(cell).empty();
2167 ret += inset->Latex(buf, os, fragile, fp);
2171 ret += TeXCellPostamble(os, cell);
2172 if (!IsLastCellInRow(cell)) { // not last cell in row
2179 ret += TeXBottomHLine(os, i);
2181 if (IsLongTabular()) {
2182 if ((endhead > 0) && (i == (endhead - 1))) {
2183 os << "\\endhead\n";
2186 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2187 os << "\\endfirsthead\n";
2190 if ((endfoot > 0) && (i == (endfoot - 1))) {
2191 os << "\\endfoot\n";
2194 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2195 os << "\\endlastfoot\n";
2199 // ret += TeXBottomHLine(os, i);
2200 if (row_info[i].newpage) {
2201 os << "\\newpage\n";
2207 //+---------------------------------------------------------------------
2208 //+ the closing of the tabular +
2209 //+---------------------------------------------------------------------
2211 if (is_long_tabular)
2212 os << "\\end{longtable}";
2214 os << "\\end{tabular}";
2216 os << "\n\\end{sideways}";
2224 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2228 //+---------------------------------------------------------------------
2229 //+ first the opening preamble +
2230 //+---------------------------------------------------------------------
2232 os << "<tgroup cols=\"" << columns_
2233 << "\" colsep=\"1\" rowsep=\"1\">\n";
2235 for (int i = 0; i < columns_; ++i) {
2236 os << "<colspec colname=\"col" << i << "\" align=\"";
2237 switch (column_info[i].alignment) {
2238 case LYX_ALIGN_LEFT:
2241 case LYX_ALIGN_RIGHT:
2252 //+---------------------------------------------------------------------
2253 //+ the single row and columns (cells) +
2254 //+---------------------------------------------------------------------
2258 for (int i = 0; i < rows_; ++i) {
2260 for (int j = 0; j < columns_; ++j) {
2261 if (IsPartOfMultiColumn(i, j))
2264 os << "<entry align=\"";
2265 switch (GetAlignment(cell)) {
2266 case LYX_ALIGN_LEFT:
2269 case LYX_ALIGN_RIGHT:
2277 os << "\" valign=\"";
2278 switch (GetVAlignment(cell)) {
2279 case LYX_VALIGN_TOP:
2282 case LYX_VALIGN_BOTTOM:
2285 case LYX_VALIGN_CENTER:
2290 if (IsMultiColumn(cell)) {
2291 os << " namest=\"col" << j << "\" ";
2292 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2296 ret += GetCellInset(cell)->DocBook(buf, os);
2303 //+---------------------------------------------------------------------
2304 //+ the closing of the tabular +
2305 //+---------------------------------------------------------------------
2317 void print_n_chars(ostream & os, unsigned char ch, int n)
2319 os << string(n, ch);
2325 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2326 vector<unsigned int> const & clen) const
2328 int const fcell = GetFirstCellInRow(row);
2329 int const n = NumberOfCellsInRow(fcell) + fcell;
2332 for (int i = fcell; i < n; ++i) {
2342 for (int i = fcell; i < n; ++i) {
2353 int column = column_of_cell(i);
2354 int len = clen[column];
2355 while(IsPartOfMultiColumn(row, ++column))
2356 len += clen[column] + 4;
2357 print_n_chars(os, ch, len);
2372 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2373 vector<unsigned int> const & clen) const
2375 int const fcell = GetFirstCellInRow(row);
2376 int const n = NumberOfCellsInRow(fcell) + fcell;
2379 for (int i = fcell; i < n; ++i) {
2380 if (BottomLine(i)) {
2389 for (int i = fcell; i < n; ++i) {
2390 if (BottomLine(i)) {
2400 int column = column_of_cell(i);
2401 int len = clen[column];
2402 while(IsPartOfMultiColumn(row, ++column))
2403 len += clen[column] + 4;
2404 print_n_chars(os, ch, len);
2405 if (BottomLine(i)) {
2419 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2420 int cell, int row, int column,
2421 vector<unsigned int> const & clen) const
2424 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2431 unsigned int len1 = sstr.str().length();
2432 unsigned int len2 = clen[column];
2433 while(IsPartOfMultiColumn(row, ++column))
2434 len2 += clen[column] + 4;
2437 switch (GetAlignment(cell)) {
2439 case LYX_ALIGN_LEFT:
2442 case LYX_ALIGN_RIGHT:
2446 case LYX_ALIGN_CENTER:
2452 for (unsigned int i = 0; i < len1; ++i)
2455 for (unsigned int i = 0; i < len2; ++i)
2457 if (RightLine(cell))
2466 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2470 //+---------------------------------------------------------------------
2471 //+ first calculate the width of the single columns +
2472 //+---------------------------------------------------------------------
2473 vector<unsigned int> clen(columns_);
2475 // first all non (real) multicolumn cells!
2476 for (int j = 0; j < columns_; ++j) {
2478 for (int i = 0; i < rows_; ++i) {
2479 int cell = GetCellNumber(i, j);
2480 if (IsMultiColumn(cell, true))
2483 GetCellInset(cell)->Ascii(buf, sstr, 0);
2484 if (clen[j] < sstr.str().length())
2485 clen[j] = sstr.str().length();
2488 // then all (real) multicolumn cells!
2489 for (int j = 0; j < columns_; ++j) {
2490 for (int i = 0; i < rows_; ++i) {
2491 int cell = GetCellNumber(i, j);
2492 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2495 GetCellInset(cell)->Ascii(buf, sstr, 0);
2496 int len = int(sstr.str().length());
2497 int const n = cells_in_multicolumn(cell);
2498 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2500 if (len > int(clen[j + n - 1]))
2501 clen[j + n - 1] = len;
2505 for (int i = 0; i < rows_; ++i) {
2506 AsciiTopHLine(os, i, clen);
2507 for (int j = 0; j < columns_; ++j) {
2508 if (IsPartOfMultiColumn(i,j))
2510 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2514 AsciiBottomHLine(os, i, clen);
2520 InsetText * LyXTabular::GetCellInset(int cell) const
2522 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2526 InsetText * LyXTabular::GetCellInset(int row, int column) const
2528 return GetCellInset(GetCellNumber(row, column));
2532 void LyXTabular::Validate(LaTeXFeatures & features) const
2534 if (IsLongTabular())
2535 features.longtable = true;
2537 features.rotating = true;
2538 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2539 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2540 features.array = true;
2541 GetCellInset(cell)->Validate(features);
2547 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2549 LyXParagraph * par = GetCellInset(cell)->par;
2551 for (; par; par = par->next_) {
2552 for (int i = 0; i < par->Last(); ++i) {
2553 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
2560 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2562 LyXParagraph * par = GetCellInset(cell)->par;
2564 for (; par; par = par->next()) {
2565 for (int i = 0; i < par->size(); ++i) {
2566 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)