1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 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"
44 #ifndef CXX_GLOBAL_CSTD
50 int const WIDTH_OF_LINE = 5;
54 /// Define a few methods for the inner structs
56 LyXTabular::cellstruct::cellstruct()
60 multicolumn = LyXTabular::CELL_NORMAL;
61 alignment = LYX_ALIGN_CENTER;
62 valignment = LYX_VALIGN_TOP;
72 LyXTabular::rowstruct::rowstruct()
82 LyXTabular::columnstruct::columnstruct()
86 alignment = LYX_ALIGN_CENTER;
87 valignment = LYX_VALIGN_TOP;
93 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
97 Init(rows_arg, columns_arg);
101 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
105 Init(lt.rows_, lt.columns_, <);
108 #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)
115 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
123 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
126 #warning This while method should look like this: (Lgb)
131 // If this and lt is not of the same size we have a serious bug
132 // So then it is ok to throw an exception, or for now
134 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
136 cell_info = lt.cell_info;
137 row_info = lt.row_info;
138 column_info = lt.column_info;
140 // long tabular stuff
141 SetLongTabular(lt.is_long_tabular);
142 endhead = lt.endhead;
143 endfoot = lt.endfoot;
144 endfirsthead = lt.endfirsthead;
145 endlastfoot = lt.endlastfoot;
155 LyXTabular * LyXTabular::clone(InsetTabular * inset)
157 LyXTabular * result = new LyXTabular(inset, *this);
159 // don't know if this is good but I need to Clone also
160 // the text-insets here, this is for the Undo-facility!
161 for (int i = 0; i < rows_; ++i) {
162 for (int j = 0; j < columns_; ++j) {
163 result->cell_info[i][j].inset = cell_info[i][j].inset;
164 result->cell_info[i][j].inset.setOwner(inset);
172 /* activates all lines and sets all widths to 0 */
173 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
176 columns_ = columns_arg;
177 row_info = row_vector(rows_, rowstruct());
178 column_info = column_vector(columns_, columnstruct());
179 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
187 for (int i = 0; i < rows_; ++i) {
188 for (int j = 0; j < columns_; ++j) {
189 cell_info[i][j].inset.setOwner(owner_);
190 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
191 cell_info[i][j].cellno = cellno++;
193 cell_info[i].back().right_line = true;
195 row_info.back().bottom_line = true;
196 row_info.front().bottom_line = true;
198 for (int i = 0; i < columns_; ++i) {
199 calculate_width_of_column(i);
201 column_info.back().right_line = true;
203 calculate_width_of_tabular();
205 rowofcell = vector<int>();
206 columnofcell = vector<int>();
207 set_row_column_number_info();
208 is_long_tabular = false;
217 void LyXTabular::AppendRow(int cell)
221 int row = row_of_cell(cell);
223 row_vector::iterator rit = row_info.begin() + row;
224 row_info.insert(rit, rowstruct());
225 // now set the values of the row before
226 row_info[row] = row_info[row+1];
229 cell_vvector::iterator cit = cell_info.begin() + row;
230 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
232 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
235 for (int i = 0; i <= row; ++i) {
236 for (int j = 0; j < columns_; ++j) {
237 c_info[i][j] = cell_info[i][j];
240 for (int i = row + 1; i < rows_; ++i) {
241 for (int j = 0; j < columns_; ++j) {
242 c_info[i][j] = cell_info[i-1][j];
247 for (int j = 0; j < columns_; ++j) {
248 cell_info[row][j].inset.clear();
255 void LyXTabular::DeleteRow(int row)
257 // Why make it so hard? (Lgb)
260 if (rows_ == 1) return; // Not allowed to delete last row
262 row_info.erase(row_info.begin() + row); //&row_info[row]);
263 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
269 void LyXTabular::AppendColumn(int cell)
273 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
275 int const column = column_of_cell(cell);
276 column_vector::iterator cit = column_info.begin() + column + 1;
277 column_info.insert(cit, columnstruct());
278 // set the column values of the column before
279 column_info[column+1] = column_info[column];
281 for (int i = 0; i < rows_; ++i) {
282 for (int j = 0; j <= column; ++j) {
283 c_info[i][j] = cell_info[i][j];
285 for (int j = column + 1; j < columns_; ++j) {
286 c_info[i][j] = cell_info[i][j - 1];
288 // care about multicolumns
289 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
290 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
292 if ((column + 1) == columns_ ||
293 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
294 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
299 for (int i = 0; i < rows_; ++i) {
300 //cell_info[i][column].inset.clear();
301 cell_info[i][column + 1].inset.clear();
307 void LyXTabular::DeleteColumn(int column)
309 // Similar to DeleteRow
310 //if (!(columns_ - 1))
312 if (columns_ == 1) return; // Not allowed to delete last column
314 column_info.erase(column_info.begin() + column);
315 for (int i = 0; i < rows_; ++i) {
316 cell_info[i].erase(cell_info[i].begin() + column);
323 void LyXTabular::Reinit()
325 for (int i = 0; i < rows_; ++i) {
326 for (int j = 0; j < columns_; ++j) {
327 cell_info[i][j].width_of_cell = 0;
328 cell_info[i][j].inset.setOwner(owner_);
332 for (int i = 0; i < columns_; ++i) {
333 calculate_width_of_column(i);
335 calculate_width_of_tabular();
337 set_row_column_number_info();
341 void LyXTabular::set_row_column_number_info(bool oldformat)
344 for (int row = 0; row < rows_; ++row) {
345 for (int column = 0; column<columns_; ++column) {
346 if (cell_info[row][column].multicolumn
347 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
349 cell_info[row][column].cellno = numberofcells;
352 ++numberofcells; // because this is one more than as we start from 0
354 rowofcell.resize(numberofcells);
355 columnofcell.resize(numberofcells);
357 for (int row = 0, column = 0, c = 0;
358 c < numberofcells && row < rows_ && column < columns_;) {
360 columnofcell[c] = column;
364 } while (column < columns_ &&
365 cell_info[row][column].multicolumn
366 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
367 if (column == columns_) {
373 for (int row = 0; row < rows_; ++row) {
374 for (int column = 0; column < columns_; ++column) {
375 if (IsPartOfMultiColumn(row,column))
377 // now set the right line of multicolumns right for oldformat read
379 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
381 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
382 cell_info[row][column].right_line =
383 cell_info[row][column+cn-1].right_line;
385 cell_info[row][column].inset.setAutoBreakRows(
386 !GetPWidth(GetCellNumber(row, column)).empty());
392 int LyXTabular::GetNumberOfCells() const
394 return numberofcells;
398 int LyXTabular::NumberOfCellsInRow(int cell) const
400 int const row = row_of_cell(cell);
402 for (int i = 0; i < columns_; ++i) {
403 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
410 /* returns 1 if there is a topline, returns 0 if not */
411 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
413 int const row = row_of_cell(cell);
415 if (!onlycolumn && IsMultiColumn(cell))
416 return cellinfo_of_cell(cell)->top_line;
417 return row_info[row].top_line;
421 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
423 // no bottom line underneath non-existent cells if you please
424 // Isn't that a programming error? Is so this should
425 // be an Assert instead. (Lgb)
426 if (cell >= numberofcells)
429 if (!onlycolumn && IsMultiColumn(cell))
430 return cellinfo_of_cell(cell)->bottom_line;
431 return row_info[row_of_cell(cell)].bottom_line;
435 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
437 if (!onlycolumn && IsMultiColumn(cell))
438 return cellinfo_of_cell(cell)->left_line;
439 return column_info[column_of_cell(cell)].left_line;
443 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
445 if (!onlycolumn && IsMultiColumn(cell))
446 return cellinfo_of_cell(cell)->right_line;
447 return column_info[right_column_of_cell(cell)].right_line;
451 bool LyXTabular::TopAlreadyDrawed(int cell) const
453 int row = row_of_cell(cell);
454 if ((row > 0) && !GetAdditionalHeight(row)) {
455 int column = column_of_cell(cell);
458 && cell_info[row][column].multicolumn
459 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
461 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
462 return row_info[row].bottom_line;
464 return cell_info[row][column].bottom_line;
470 bool LyXTabular::LeftAlreadyDrawed(int cell) const
472 int column = column_of_cell(cell);
474 int row = row_of_cell(cell);
476 (cell_info[row][column].multicolumn ==
477 LyXTabular::CELL_PART_OF_MULTICOLUMN));
478 if (GetAdditionalWidth(cell_info[row][column].cellno))
480 return column_info[column].right_line;
486 bool LyXTabular::IsLastRow(int cell) const
488 return (row_of_cell(cell) == rows_ - 1);
492 int LyXTabular::GetAdditionalHeight(int row) const
494 if (!row || row >= rows_)
500 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
501 switch (cell_info[row - 1][column].multicolumn) {
502 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
503 bottom = cell_info[row - 1][column].bottom_line;
505 case LyXTabular::CELL_NORMAL:
506 bottom = row_info[row - 1].bottom_line;
509 for (int column = 0; column < columns_ - 1 && top; ++column) {
510 switch (cell_info[row][column].multicolumn){
511 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
512 top = cell_info[row][column].top_line;
514 case LyXTabular::CELL_NORMAL:
515 top = row_info[row].top_line;
519 return WIDTH_OF_LINE;
524 int LyXTabular::GetAdditionalWidth(int cell) const
526 // internally already set in SetWidthOfCell
527 // used to get it back in text.C
528 int const col = right_column_of_cell(cell);
529 if (col < columns_ - 1 && column_info[col].right_line &&
530 column_info[col+1].left_line)
531 return WIDTH_OF_LINE;
537 // returns the maximum over all rows
538 int LyXTabular::GetWidthOfColumn(int cell) const
540 int const column1 = column_of_cell(cell);
541 int const column2 = right_column_of_cell(cell);
543 for (int i = column1; i <= column2; ++i) {
544 result += column_info[i].width_of_column;
550 int LyXTabular::GetWidthOfTabular() const
552 return width_of_tabular;
556 /* returns 1 if a complete update is necessary, otherwise 0 */
557 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
559 if (!IsMultiColumn(cell))
562 int const row = row_of_cell(cell);
563 int const column1 = column_of_cell(cell);
564 int const column2 = right_column_of_cell(cell);
566 // first set columns to 0 so we can calculate the right width
567 for (int i = column1; i <= column2; ++i) {
568 cell_info[row][i].width_of_cell = 0;
570 // set the width to MAX_WIDTH until width > 0
571 int width = (new_width + 2 * WIDTH_OF_LINE);
574 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
575 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
576 width -= column_info[i].width_of_column;
579 cell_info[row][i].width_of_cell = width;
585 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
587 int const row = row_of_cell(cell);
588 int const column1 = column_of_cell(cell);
589 int const column2 = right_column_of_cell(cell);
591 // first set columns to 0 so we can calculate the right width
593 for (; i <= column2; ++i)
594 cell_info[row][i].width_of_cell = 0;
595 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
597 if (i < numberofcells)
598 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
599 SetWidthOfMulticolCell(cell, new_width);
603 /* returns 1 if a complete update is necessary, otherwise 0 */
604 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
606 int const row = row_of_cell(cell);
607 int const column1 = column_of_cell(cell);
611 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
613 if (IsMultiColumn(cell, true)) {
614 tmp = SetWidthOfMulticolCell(cell, new_width);
616 width = (new_width + 2*WIDTH_OF_LINE);
617 cell_info[row][column1].width_of_cell = width;
618 if (column_info[column1].right_line && (column1 < columns_-1) &&
619 column_info[column1+1].left_line) // additional width
620 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
621 tmp = calculate_width_of_column_NMC(column1);
625 for (; i<columns_; ++i)
626 calculate_width_of_column_NMC(i);
627 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
629 if (i < numberofcells)
630 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
631 for (i = 0; i < columns_; ++i)
632 calculate_width_of_column(i);
633 calculate_width_of_tabular();
640 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
642 if (!IsMultiColumn(cell) || onlycolumn)
643 column_info[column_of_cell(cell)].alignment = align;
645 cellinfo_of_cell(cell)->alignment = align;
650 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
652 if (!IsMultiColumn(cell) || onlycolumn)
653 column_info[column_of_cell(cell)].valignment = align;
655 cellinfo_of_cell(cell)->valignment = align;
660 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
662 bool flag = !width.empty();
663 int const j = column_of_cell(cell);
665 column_info[j].p_width = width;
666 if (flag) // do this only if there is a width
667 SetAlignment(cell, LYX_ALIGN_LEFT);
668 for (int i = 0; i < rows_; ++i) {
669 int c = GetCellNumber(i, j);
670 flag = !GetPWidth(c).empty(); // because of multicolumns!
671 GetCellInset(c)->setAutoBreakRows(flag);
677 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
679 bool const flag = !width.empty();
681 cellinfo_of_cell(cell)->p_width = width;
682 if (IsMultiColumn(cell)) {
683 GetCellInset(cell)->setAutoBreakRows(flag);
690 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
691 LyXTabular::Feature what)
693 if (what == SET_SPECIAL_MULTI)
694 cellinfo_of_cell(cell)->align_special = special;
696 column_info[column_of_cell(cell)].align_special = special;
701 bool LyXTabular::SetAllLines(int cell, bool line)
703 SetTopLine(cell, line);
704 SetBottomLine(cell, line);
705 SetRightLine(cell, line);
706 SetLeftLine(cell, line);
711 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
713 int const row = row_of_cell(cell);
715 if (onlycolumn || !IsMultiColumn(cell))
716 row_info[row].top_line = line;
718 cellinfo_of_cell(cell)->top_line = line;
723 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
725 if (onlycolumn || !IsMultiColumn(cell))
726 row_info[row_of_cell(cell)].bottom_line = line;
728 cellinfo_of_cell(cell)->bottom_line = line;
733 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
735 if (onlycolumn || !IsMultiColumn(cell))
736 column_info[column_of_cell(cell)].left_line = line;
738 cellinfo_of_cell(cell)->left_line = line;
743 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
745 if (onlycolumn || !IsMultiColumn(cell))
746 column_info[right_column_of_cell(cell)].right_line = line;
748 cellinfo_of_cell(cell)->right_line = line;
753 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
755 if (!onlycolumn && IsMultiColumn(cell))
756 return cellinfo_of_cell(cell)->alignment;
758 return column_info[column_of_cell(cell)].alignment;
762 LyXTabular::VAlignment
763 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
765 if (!onlycolumn && IsMultiColumn(cell))
766 return cellinfo_of_cell(cell)->valignment;
768 return column_info[column_of_cell(cell)].valignment;
772 string const LyXTabular::GetPWidth(int cell) const
774 if (IsMultiColumn(cell))
775 return cellinfo_of_cell(cell)->p_width;
776 return column_info[column_of_cell(cell)].p_width;
780 string const LyXTabular::GetColumnPWidth(int cell) const
782 return column_info[column_of_cell(cell)].p_width;
786 string const LyXTabular::GetMColumnPWidth(int cell) const
788 if (IsMultiColumn(cell))
789 return cellinfo_of_cell(cell)->p_width;
794 string const LyXTabular::GetAlignSpecial(int cell, int what) const
796 if (what == SET_SPECIAL_MULTI)
797 return cellinfo_of_cell(cell)->align_special;
798 return column_info[column_of_cell(cell)].align_special;
802 int LyXTabular::GetWidthOfCell(int cell) const
804 int const row = row_of_cell(cell);
805 int const column1 = column_of_cell(cell);
806 int const column2 = right_column_of_cell(cell);
808 for (int i = column1; i <= column2; ++i) {
809 result += cell_info[row][i].width_of_cell;
815 int LyXTabular::GetBeginningOfTextInCell(int cell) const
819 switch (GetAlignment(cell)){
820 case LYX_ALIGN_CENTER:
821 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
823 case LYX_ALIGN_RIGHT:
824 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
825 // + GetAdditionalWidth(cell);
827 default: /* LYX_ALIGN_LEFT: nothing :-) */
837 bool LyXTabular::IsFirstCellInRow(int cell) const
839 return column_of_cell(cell) == 0;
843 int LyXTabular::GetFirstCellInRow(int row) const
847 return cell_info[row][0].cellno;
850 bool LyXTabular::IsLastCellInRow(int cell) const
852 return (right_column_of_cell(cell) == (columns_ - 1));
856 int LyXTabular::GetLastCellInRow(int row) const
860 return cell_info[row][columns_-1].cellno;
864 bool LyXTabular::calculate_width_of_column(int column)
866 int const old_column_width = column_info[column].width_of_column;
869 for (int i = 0; i < rows_; ++i) {
870 maximum = max(cell_info[i][column].width_of_cell, maximum);
872 column_info[column].width_of_column = maximum;
873 return (column_info[column].width_of_column != old_column_width);
878 // calculate the with of the column without regarding REAL MultiColumn
879 // cells. This means MultiColumn-cells spanning more than 1 column.
881 bool LyXTabular::calculate_width_of_column_NMC(int column)
883 int const old_column_width = column_info[column].width_of_column;
885 for (int i = 0; i < rows_; ++i) {
886 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
887 (cell_info[i][column].width_of_cell > max)) {
888 max = cell_info[i][column].width_of_cell;
891 column_info[column].width_of_column = max;
892 return (column_info[column].width_of_column != old_column_width);
896 void LyXTabular::calculate_width_of_tabular()
898 width_of_tabular = 0;
899 for (int i = 0; i < columns_; ++i) {
900 width_of_tabular += column_info[i].width_of_column;
905 int LyXTabular::row_of_cell(int cell) const
907 if (cell >= numberofcells)
911 return rowofcell[cell];
915 int LyXTabular::column_of_cell(int cell) const
917 if (cell >= numberofcells)
921 return columnofcell[cell];
925 int LyXTabular::right_column_of_cell(int cell) const
927 int const row = row_of_cell(cell);
928 int column = column_of_cell(cell);
929 while (column < (columns_ - 1) &&
930 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
936 // Perfect case for a template... (Lgb)
939 string const write_attribute(string const & name, T const & t)
941 string str = " " + name + "=\"" + tostr(t) + "\"";
946 string const write_attribute(string const & name, bool const & b)
948 return write_attribute(name, int(b));
953 string const write_attribute(string const & name, int value)
955 string str = " " + name + "=\"" + tostr(value) + "\"";
960 string const write_attribute(string const & name, string const & value)
962 string str = " " + name + "=\"" + value + "\"";
967 string const write_attribute(string const & name, bool value)
969 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
977 string const tostr(LyXAlignment const & num)
982 case LYX_ALIGN_BLOCK:
986 case LYX_ALIGN_CENTER:
988 case LYX_ALIGN_RIGHT:
990 case LYX_ALIGN_LAYOUT:
992 case LYX_ALIGN_SPECIAL:
1001 string const tostr(LyXTabular::VAlignment const & num)
1004 case LyXTabular::LYX_VALIGN_TOP:
1006 case LyXTabular::LYX_VALIGN_CENTER:
1008 case LyXTabular::LYX_VALIGN_BOTTOM:
1017 string const tostr(LyXTabular::BoxType const & num)
1020 case LyXTabular::BOX_NONE:
1022 case LyXTabular::BOX_PARBOX:
1024 case LyXTabular::BOX_MINIPAGE:
1031 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1035 << write_attribute("version", 2)
1036 << write_attribute("rows", rows_)
1037 << write_attribute("columns", columns_)
1039 // global longtable options
1041 << write_attribute("rotate", tostr(rotate))
1042 << write_attribute("islongtable", tostr(is_long_tabular))
1043 << write_attribute("endhead", endhead)
1044 << write_attribute("endfirsthead", endfirsthead)
1045 << write_attribute("endfoot", endfoot)
1046 << write_attribute("endlastfoot", endlastfoot)
1048 for (int j = 0; j < columns_; ++j) {
1050 << write_attribute("alignment", tostr(column_info[j].alignment))
1051 << write_attribute("valignment", tostr(column_info[j].valignment))
1052 << write_attribute("leftline", tostr(column_info[j].left_line))
1053 << write_attribute("rightline", tostr(column_info[j].right_line))
1054 << write_attribute("width",
1055 VSpace(column_info[j].p_width)
1057 << write_attribute("special", column_info[j].align_special)
1060 for (int i = 0; i < rows_; ++i) {
1062 << write_attribute("topline", tostr(row_info[i].top_line))
1063 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1064 << write_attribute("newpage", tostr(row_info[i].newpage))
1066 for (int j = 0; j < columns_; ++j) {
1068 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1069 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1070 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1071 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1072 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1073 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1074 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1075 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1076 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1077 << write_attribute("width", cell_info[i][j].p_width)
1078 << write_attribute("special", cell_info[i][j].align_special)
1080 os << "\\begin_inset ";
1081 cell_info[i][j].inset.write(buf, os);
1082 os << "\n\\end_inset \n"
1087 os << "</lyxtabular>\n";
1093 // I would have liked a fromstr template a lot better. (Lgb)
1096 bool string2type(string const str, LyXAlignment & num)
1099 num = LYX_ALIGN_NONE;
1100 else if (str == "block")
1101 num = LYX_ALIGN_BLOCK;
1102 else if (str == "left")
1103 num = LYX_ALIGN_LEFT;
1104 else if (str == "center")
1105 num = LYX_ALIGN_CENTER;
1106 else if (str == "right")
1107 num = LYX_ALIGN_RIGHT;
1115 bool string2type(string const str, LyXTabular::VAlignment & num)
1118 num = LyXTabular::LYX_VALIGN_TOP;
1119 else if (str == "center")
1120 num = LyXTabular::LYX_VALIGN_CENTER;
1121 else if (str == "bottom")
1122 num = LyXTabular::LYX_VALIGN_BOTTOM;
1130 bool string2type(string const str, LyXTabular::BoxType & num)
1133 num = LyXTabular::BOX_NONE;
1134 else if (str == "parbox")
1135 num = LyXTabular::BOX_PARBOX;
1136 else if (str == "minipage")
1137 num = LyXTabular::BOX_MINIPAGE;
1145 bool string2type(string const str, bool & num)
1149 else if (str == "false")
1157 bool getTokenValue(string const & str, const char * token, string & ret)
1159 size_t token_length = strlen(token);
1160 string::size_type pos = str.find(token);
1162 if (pos == string::npos || pos + token_length + 1 >= str.length()
1163 || str[pos + token_length] != '=')
1166 pos += token_length + 1;
1168 if ((ch != '"') && (ch != '\'')) { // only read till next space
1172 while((pos < str.length() - 1) && (str[++pos] != ch))
1179 bool getTokenValue(string const & str, const char * token, int & num)
1182 if (!getTokenValue(str, token, tmp))
1184 num = strToInt(tmp);
1189 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1192 if (!getTokenValue(str, token, tmp))
1194 return string2type(tmp, num);
1198 bool getTokenValue(string const & str, const char * token,
1199 LyXTabular::VAlignment & num)
1202 if (!getTokenValue(str, token, tmp))
1204 return string2type(tmp, num);
1208 bool getTokenValue(string const & str, const char * token,
1209 LyXTabular::BoxType & num)
1212 if (!getTokenValue(str, token, tmp))
1214 return string2type(tmp, num);
1218 bool getTokenValue(string const & str, const char * token, bool & flag)
1221 if (!getTokenValue(str, token, tmp))
1223 return string2type(tmp, flag);
1228 void l_getline(istream & is, string & str)
1231 while (str.empty()) {
1233 if (!str.empty() && str[str.length() - 1] == '\r')
1234 str.erase(str.length() - 1);
1241 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1244 istream & is = lex.getStream();
1246 l_getline(is, line);
1247 if (!prefixIs(line, "<lyxtabular ")
1248 && !prefixIs(line, "<LyXTabular ")) {
1249 OldFormatRead(lex, line);
1254 if (!getTokenValue(line, "version", version))
1257 ReadOld(buf, is, lex, line);
1258 else if (version == 2)
1259 ReadNew(buf, is, lex, line);
1263 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1264 LyXLex & lex, string const & l)
1268 if (!getTokenValue(line, "rows", rows_arg))
1271 if (!getTokenValue(line, "columns", columns_arg))
1273 Init(rows_arg, columns_arg);
1274 l_getline(is, line);
1275 if (!prefixIs(line, "<features")) {
1276 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1277 line << ")" << endl;
1280 getTokenValue(line, "rotate", rotate);
1281 getTokenValue(line, "islongtable", is_long_tabular);
1282 getTokenValue(line, "endhead", endhead);
1283 getTokenValue(line, "endfirsthead", endfirsthead);
1284 getTokenValue(line, "endfoot", endfoot);
1285 getTokenValue(line, "endlastfoot", endlastfoot);
1287 for (int j = 0; j < columns_; ++j) {
1289 if (!prefixIs(line,"<column")) {
1290 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1291 line << ")" << endl;
1294 getTokenValue(line, "alignment", column_info[j].alignment);
1295 getTokenValue(line, "valignment", column_info[j].valignment);
1296 getTokenValue(line, "leftline", column_info[j].left_line);
1297 getTokenValue(line, "rightline", column_info[j].right_line);
1298 getTokenValue(line, "width", column_info[j].p_width);
1299 getTokenValue(line, "special", column_info[j].align_special);
1302 for (int i = 0; i < rows_; ++i) {
1303 l_getline(is, line);
1304 if (!prefixIs(line, "<row")) {
1305 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1306 line << ")" << endl;
1309 getTokenValue(line, "topline", row_info[i].top_line);
1310 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1311 getTokenValue(line, "newpage", row_info[i].newpage);
1312 for (int j = 0; j < columns_; ++j) {
1313 l_getline(is, line);
1314 if (!prefixIs(line, "<cell")) {
1315 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1316 line << ")" << endl;
1319 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1320 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1321 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1322 getTokenValue(line, "topline", cell_info[i][j].top_line);
1323 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1324 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1325 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1326 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1327 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1328 getTokenValue(line, "width", cell_info[i][j].p_width);
1329 getTokenValue(line, "special", cell_info[i][j].align_special);
1330 l_getline(is, line);
1331 if (prefixIs(line, "\\begin_inset")) {
1332 cell_info[i][j].inset.read(buf, lex);
1333 l_getline(is, line);
1335 if (!prefixIs(line, "</cell>")) {
1336 lyxerr << "Wrong tabular format (expected </cell> got" <<
1337 line << ")" << endl;
1341 l_getline(is, line);
1342 if (!prefixIs(line, "</row>")) {
1343 lyxerr << "Wrong tabular format (expected </row> got" <<
1344 line << ")" << endl;
1348 while (!prefixIs(line, "</lyxtabular>")) {
1349 l_getline(is, line);
1351 set_row_column_number_info();
1355 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1361 int columns_arg = 0;
1362 int is_long_tabular_arg = false;
1363 int rotate_arg = false;
1372 istream & is = lex.getStream();
1375 version = lyx::atoi(s.substr(8, string::npos));
1379 vector<int> cont_row_info;
1382 lyxerr << "Tabular format < 5 is not supported anymore\n"
1383 "Get an older version of LyX (< 1.1.x) for conversion!"
1385 WriteAlert(_("Warning:"),
1386 _("Tabular format < 5 is not supported anymore\n"),
1387 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1389 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1390 >> rotate_arg >> a >> b >> c >> d;
1392 is >> rows_arg >> columns_arg;
1393 Init(rows_arg, columns_arg);
1394 cont_row_info = vector<int>(rows_arg);
1395 SetLongTabular(is_long_tabular_arg);
1396 SetRotateTabular(rotate_arg);
1398 for (i = 0; i < rows_; ++i) {
1400 cont_row_info[i] = false;
1402 for (i = 0; i < columns_; ++i) {
1405 for (i = 0; i < rows_; ++i) {
1406 for (j = 0; j < columns_; ++j) {
1411 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1412 >> rotate_arg >> a >> b >> c >> d;
1413 Init(rows_arg, columns_arg);
1414 cont_row_info = vector<int>(rows_arg);
1415 SetLongTabular(is_long_tabular_arg);
1416 SetRotateTabular(rotate_arg);
1418 endfirsthead = b + 1;
1420 endlastfoot = d + 1;
1421 for (i = 0; i < rows_; ++i) {
1422 a = b = c = d = e = f = g = 0;
1423 is >> a >> b >> c >> d;
1424 row_info[i].top_line = a;
1425 row_info[i].bottom_line = b;
1426 cont_row_info[i] = c;
1427 row_info[i].newpage = d;
1429 for (i = 0; i < columns_; ++i) {
1434 char ch; // skip '"'
1437 // ignore is buggy but we will use it later (Lgb)
1438 is.ignore(); // skip '"'
1440 getline(is, s1, '"');
1442 is >> ch; // skip '"'
1444 // ignore is buggy but we will use it later (Lgb)
1445 is.ignore(); // skip '"'
1447 getline(is, s2, '"');
1448 column_info[i].alignment = static_cast<LyXAlignment>(a);
1449 column_info[i].left_line = b;
1450 column_info[i].right_line = c;
1451 column_info[i].p_width = s1;
1452 column_info[i].align_special = s2;
1454 for (i = 0; i < rows_; ++i) {
1455 for (j = 0; j < columns_; ++j) {
1458 is >> a >> b >> c >> d >> e >> f >> g;
1461 is >> ch; // skip '"'
1463 // ignore is buggy but we will use it later (Lgb)
1464 is.ignore(); // skip '"'
1466 getline(is, s1, '"');
1468 is >> ch; // skip '"'
1470 // ignore is buggy but we will use it later (Lgb)
1471 is.ignore(); // skip '"'
1473 getline(is, s2, '"');
1474 cell_info[i][j].multicolumn = static_cast<char>(a);
1475 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1476 cell_info[i][j].top_line = static_cast<char>(c);
1477 cell_info[i][j].bottom_line = static_cast<char>(d);
1478 cell_info[i][j].left_line = column_info[j].left_line;
1479 cell_info[i][j].right_line = column_info[j].right_line;
1480 cell_info[i][j].rotate = static_cast<bool>(f);
1481 cell_info[i][j].usebox = static_cast<BoxType>(g);
1482 cell_info[i][j].align_special = s1;
1483 cell_info[i][j].p_width = s2;
1487 set_row_column_number_info(true);
1489 Paragraph * par = new Paragraph;
1490 Paragraph * return_par = 0;
1494 Paragraph::depth_type depth = 0;
1495 LyXFont font(LyXFont::ALL_INHERIT);
1496 font.setLanguage(owner_->bufferOwner()->getLanguage());
1498 while (lex.IsOK()) {
1500 string const token = lex.GetString();
1503 if (token == "\\layout"
1504 || token == "\\end_float"
1505 || token == "\\end_deeper") {
1506 lex.pushToken(token);
1509 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1514 lex.pushToken(token);
1518 lex.printError("New Paragraph allocated! This should not happen!");
1519 lex.pushToken(token);
1525 // now we have the par we should fill the insets with this!
1527 InsetText * inset = GetCellInset(cell);
1530 for (int i = 0; i < par->size(); ++i) {
1531 if (par->isNewline(i)) {
1533 if (cell > GetNumberOfCells()) {
1534 lyxerr << "Some error in reading old table format occured!" <<
1535 endl << "Terminating when reading cell[" << cell << "]!" <<
1540 row = row_of_cell(cell);
1541 if (cont_row_info[row]) {
1543 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1544 while(!IsFirstCellInRow(--cell));
1546 inset = GetCellInset(cell);
1549 inset = GetCellInset(cell);
1550 row = row_of_cell(cell);
1551 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1553 // insert a space instead
1555 par->insertChar(i, ' ');
1558 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1559 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1566 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1568 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1569 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1573 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1575 int const row = row_of_cell(cell);
1576 int const column = column_of_cell(cell);
1577 return &cell_info[row][column];
1581 void LyXTabular::SetMultiColumn(int cell, int number)
1583 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1584 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1585 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1586 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1587 for (number--; number > 0; --number) {
1588 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1590 set_row_column_number_info();
1594 int LyXTabular::cells_in_multicolumn(int cell) const
1596 int const row = row_of_cell(cell);
1597 int column = column_of_cell(cell);
1600 while ((column < columns_) &&
1601 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1610 int LyXTabular::UnsetMultiColumn(int cell)
1612 int const row = row_of_cell(cell);
1613 int column = column_of_cell(cell);
1617 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1618 cell_info[row][column].multicolumn = CELL_NORMAL;
1620 while ((column < columns_) &&
1621 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1623 cell_info[row][column].multicolumn = CELL_NORMAL;
1628 set_row_column_number_info();
1633 void LyXTabular::SetLongTabular(bool what)
1635 is_long_tabular = what;
1639 bool LyXTabular::IsLongTabular() const
1641 return is_long_tabular;
1645 void LyXTabular::SetRotateTabular(bool flag)
1651 bool LyXTabular::GetRotateTabular() const
1657 void LyXTabular::SetRotateCell(int cell, bool flag)
1659 cellinfo_of_cell(cell)->rotate = flag;
1663 bool LyXTabular::GetRotateCell(int cell) const
1665 return cellinfo_of_cell(cell)->rotate;
1669 bool LyXTabular::NeedRotating() const
1673 for (int i = 0; i < rows_; ++i) {
1674 for (int j = 0; j < columns_; ++j) {
1675 if (cell_info[i][j].rotate)
1683 bool LyXTabular::IsLastCell(int cell) const
1685 if ((cell + 1) < GetNumberOfCells())
1691 int LyXTabular::GetCellAbove(int cell) const
1693 if (row_of_cell(cell) > 0)
1694 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1699 int LyXTabular::GetCellBelow(int cell) const
1701 if (row_of_cell(cell) + 1 < rows_)
1702 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1707 int LyXTabular::GetLastCellAbove(int cell) const
1709 if (row_of_cell(cell) <= 0)
1711 if (!IsMultiColumn(cell))
1712 return GetCellAbove(cell);
1713 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1717 int LyXTabular::GetLastCellBelow(int cell) const
1719 if (row_of_cell(cell) + 1 >= rows_)
1721 if (!IsMultiColumn(cell))
1722 return GetCellBelow(cell);
1723 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1727 int LyXTabular::GetCellNumber(int row, int column) const
1729 if (column >= columns_)
1730 column = columns_ - 1;
1731 else if (column < 0)
1738 return cell_info[row][column].cellno;
1742 void LyXTabular::SetUsebox(int cell, BoxType type)
1744 cellinfo_of_cell(cell)->usebox = type;
1748 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1750 if (column_info[column_of_cell(cell)].p_width.empty() &&
1751 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1753 if (cellinfo_of_cell(cell)->usebox > 1)
1754 return cellinfo_of_cell(cell)->usebox;
1755 return UseParbox(cell);
1759 void LyXTabular::SetLTHead(int cell, bool first)
1761 int const row = row_of_cell(cell);
1762 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1765 if (endfirsthead == val)
1778 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1781 if (abs(endhead) > rows_)
1783 return (row_of_cell(cell) == abs(endhead) - 1);
1787 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1790 if (abs(endfirsthead) > rows_)
1792 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1796 void LyXTabular::SetLTFoot(int cell, bool last)
1798 int const row = row_of_cell(cell);
1799 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1802 if (endlastfoot == val)
1815 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1818 if ((endfoot + 1) > rows_)
1820 return (row_of_cell(cell) == abs(endfoot) - 1);
1824 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1827 if (abs(endlastfoot) > rows_)
1829 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1833 void LyXTabular::SetLTNewPage(int cell, bool what)
1835 row_info[row_of_cell(cell)].newpage = what;
1839 bool LyXTabular::GetLTNewPage(int cell) const
1841 return row_info[row_of_cell(cell)].newpage;
1845 bool LyXTabular::SetAscentOfRow(int row, int height)
1847 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1849 row_info[row].ascent_of_row = height;
1854 bool LyXTabular::SetDescentOfRow(int row, int height)
1856 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1858 row_info[row].descent_of_row = height;
1863 int LyXTabular::GetAscentOfRow(int row) const
1867 return row_info[row].ascent_of_row;
1871 int LyXTabular::GetDescentOfRow(int row) const
1875 return row_info[row].descent_of_row;
1879 int LyXTabular::GetHeightOfTabular() const
1883 for (int row = 0; row < rows_; ++row)
1884 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1885 GetAdditionalHeight(row);
1890 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1892 if ((row >= rows_) || (column >= columns_))
1894 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1898 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1900 if ((row < 0) || (row >= rows_))
1903 int const fcell = GetFirstCellInRow(row);
1904 int const n = NumberOfCellsInRow(fcell) + fcell;
1907 for (int i = fcell; i < n; ++i) {
1911 if (tmp == (n - fcell)){
1914 for (int i = fcell; i < n; ++i) {
1917 << column_of_cell(i) + 1
1919 << right_column_of_cell(i) + 1
1931 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1933 if ((row < 0) || (row >= rows_))
1936 int const fcell = GetFirstCellInRow(row);
1937 int const n = NumberOfCellsInRow(fcell) + fcell;
1940 for (int i = fcell; i < n; ++i) {
1944 if (tmp == (n-fcell)){
1947 for (int i = fcell; i < n; ++i) {
1948 if (BottomLine(i)) {
1950 << column_of_cell(i) + 1
1952 << right_column_of_cell(i) + 1
1964 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1968 if (GetRotateCell(cell)) {
1969 os << "\\begin{sideways}\n";
1972 if (IsMultiColumn(cell)) {
1973 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1974 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1975 os << cellinfo_of_cell(cell)->align_special << "}{";
1977 if (LeftLine(cell) &&
1978 (IsFirstCellInRow(cell) ||
1979 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1980 !RightLine(cell-1, true))))
1984 if (!GetPWidth(cell).empty()) {
1985 switch (GetVAlignment(cell)) {
1986 case LYX_VALIGN_TOP:
1989 case LYX_VALIGN_CENTER:
1992 case LYX_VALIGN_BOTTOM:
1996 os << "{" << GetPWidth(cell) << '}';
1998 switch (GetAlignment(cell)) {
1999 case LYX_ALIGN_LEFT:
2002 case LYX_ALIGN_RIGHT:
2010 if (RightLine(cell))
2012 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2018 if (GetUsebox(cell) == BOX_PARBOX) {
2020 switch (GetVAlignment(cell)) {
2021 case LYX_VALIGN_TOP:
2024 case LYX_VALIGN_CENTER:
2027 case LYX_VALIGN_BOTTOM:
2031 os << "]{" << GetPWidth(cell) << "}{";
2032 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2033 os << "\\begin{minipage}[";
2034 switch (GetVAlignment(cell)) {
2035 case LYX_VALIGN_TOP:
2038 case LYX_VALIGN_CENTER:
2041 case LYX_VALIGN_BOTTOM:
2045 os << "]{" << GetPWidth(cell) << "}\n";
2052 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2057 if (GetUsebox(cell) == BOX_PARBOX)
2059 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2060 os << "%\n\\end{minipage}";
2063 if (IsMultiColumn(cell)){
2066 if (GetRotateCell(cell)) {
2067 os << "%\n\\end{sideways}";
2074 int LyXTabular::Latex(Buffer const * buf,
2075 ostream & os, bool fragile, bool fp) const
2080 //+---------------------------------------------------------------------
2081 //+ first the opening preamble +
2082 //+---------------------------------------------------------------------
2085 os << "\\begin{sideways}\n";
2088 if (is_long_tabular)
2089 os << "\\begin{longtable}{";
2091 os << "\\begin{tabular}{";
2092 for (int i = 0; i < columns_; ++i) {
2093 if (column_info[i].left_line)
2095 if (!column_info[i].align_special.empty()) {
2096 os << column_info[i].align_special;
2097 } else if (!column_info[i].p_width.empty()) {
2098 switch (column_info[i].valignment) {
2099 case LYX_VALIGN_TOP:
2102 case LYX_VALIGN_CENTER:
2105 case LYX_VALIGN_BOTTOM:
2110 << column_info[i].p_width
2113 switch (column_info[i].alignment) {
2114 case LYX_ALIGN_LEFT:
2117 case LYX_ALIGN_RIGHT:
2125 if (column_info[i].right_line)
2131 //+---------------------------------------------------------------------
2132 //+ the single row and columns (cells) +
2133 //+---------------------------------------------------------------------
2135 for (int i = 0; i < rows_; ++i) {
2136 ret += TeXTopHLine(os, i);
2138 if (IsLongTabular()) {
2139 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2140 os << "\\endhead\n";
2143 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2144 os << "\\endfirsthead\n";
2147 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2148 os << "\\endfoot\n";
2151 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2152 os << "\\endlastfoot\n";
2157 ret += TeXBottomHLine(os, i-1);
2158 ret += TeXTopHLine(os, i);
2160 for (int j = 0; j < columns_; ++j) {
2161 if (IsPartOfMultiColumn(i,j))
2163 ret += TeXCellPreamble(os, cell);
2164 InsetText * inset = GetCellInset(cell);
2166 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2167 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2171 ret += inset->latex(buf, os, fragile, fp);
2175 ret += TeXCellPostamble(os, cell);
2176 if (!IsLastCellInRow(cell)) { // not last cell in row
2183 ret += TeXBottomHLine(os, i);
2185 if (IsLongTabular()) {
2186 if ((endhead > 0) && (i == (endhead - 1))) {
2187 os << "\\endhead\n";
2190 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2191 os << "\\endfirsthead\n";
2194 if ((endfoot > 0) && (i == (endfoot - 1))) {
2195 os << "\\endfoot\n";
2198 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2199 os << "\\endlastfoot\n";
2203 // ret += TeXBottomHLine(os, i);
2204 if (row_info[i].newpage) {
2205 os << "\\newpage\n";
2211 //+---------------------------------------------------------------------
2212 //+ the closing of the tabular +
2213 //+---------------------------------------------------------------------
2215 if (is_long_tabular)
2216 os << "\\end{longtable}";
2218 os << "\\end{tabular}";
2220 os << "\n\\end{sideways}";
2228 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2232 //+---------------------------------------------------------------------
2233 //+ first the opening preamble +
2234 //+---------------------------------------------------------------------
2236 os << "<tgroup cols=\"" << columns_
2237 << "\" colsep=\"1\" rowsep=\"1\">\n";
2239 for (int i = 0; i < columns_; ++i) {
2240 os << "<colspec colname=\"col" << i << "\" align=\"";
2241 switch (column_info[i].alignment) {
2242 case LYX_ALIGN_LEFT:
2245 case LYX_ALIGN_RIGHT:
2256 //+---------------------------------------------------------------------
2257 //+ the single row and columns (cells) +
2258 //+---------------------------------------------------------------------
2262 for (int i = 0; i < rows_; ++i) {
2264 for (int j = 0; j < columns_; ++j) {
2265 if (IsPartOfMultiColumn(i, j))
2268 os << "<entry align=\"";
2269 switch (GetAlignment(cell)) {
2270 case LYX_ALIGN_LEFT:
2273 case LYX_ALIGN_RIGHT:
2281 os << "\" valign=\"";
2282 switch (GetVAlignment(cell)) {
2283 case LYX_VALIGN_TOP:
2286 case LYX_VALIGN_BOTTOM:
2289 case LYX_VALIGN_CENTER:
2294 if (IsMultiColumn(cell)) {
2295 os << " namest=\"col" << j << "\" ";
2296 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2300 ret += GetCellInset(cell)->docBook(buf, os);
2307 //+---------------------------------------------------------------------
2308 //+ the closing of the tabular +
2309 //+---------------------------------------------------------------------
2321 void print_n_chars(ostream & os, unsigned char ch, int n)
2323 os << string(n, ch);
2329 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2330 vector<unsigned int> const & clen) const
2332 int const fcell = GetFirstCellInRow(row);
2333 int const n = NumberOfCellsInRow(fcell) + fcell;
2336 for (int i = fcell; i < n; ++i) {
2346 for (int i = fcell; i < n; ++i) {
2357 int column = column_of_cell(i);
2358 int len = clen[column];
2359 while(IsPartOfMultiColumn(row, ++column))
2360 len += clen[column] + 4;
2361 print_n_chars(os, ch, len);
2376 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2377 vector<unsigned int> const & clen) const
2379 int const fcell = GetFirstCellInRow(row);
2380 int const n = NumberOfCellsInRow(fcell) + fcell;
2383 for (int i = fcell; i < n; ++i) {
2384 if (BottomLine(i)) {
2393 for (int i = fcell; i < n; ++i) {
2394 if (BottomLine(i)) {
2404 int column = column_of_cell(i);
2405 int len = clen[column];
2406 while(IsPartOfMultiColumn(row, ++column))
2407 len += clen[column] + 4;
2408 print_n_chars(os, ch, len);
2409 if (BottomLine(i)) {
2423 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2424 int cell, int row, int column,
2425 vector<unsigned int> const & clen) const
2428 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2435 unsigned int len1 = sstr.str().length();
2436 unsigned int len2 = clen[column];
2437 while(IsPartOfMultiColumn(row, ++column))
2438 len2 += clen[column] + 4;
2441 switch (GetAlignment(cell)) {
2443 case LYX_ALIGN_LEFT:
2446 case LYX_ALIGN_RIGHT:
2450 case LYX_ALIGN_CENTER:
2456 for (unsigned int i = 0; i < len1; ++i)
2459 for (unsigned int i = 0; i < len2; ++i)
2461 if (RightLine(cell))
2470 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2474 //+---------------------------------------------------------------------
2475 //+ first calculate the width of the single columns +
2476 //+---------------------------------------------------------------------
2477 vector<unsigned int> clen(columns_);
2479 // first all non (real) multicolumn cells!
2480 for (int j = 0; j < columns_; ++j) {
2482 for (int i = 0; i < rows_; ++i) {
2483 int cell = GetCellNumber(i, j);
2484 if (IsMultiColumn(cell, true))
2487 GetCellInset(cell)->ascii(buf, sstr, 0);
2488 if (clen[j] < sstr.str().length())
2489 clen[j] = sstr.str().length();
2492 // then all (real) multicolumn cells!
2493 for (int j = 0; j < columns_; ++j) {
2494 for (int i = 0; i < rows_; ++i) {
2495 int cell = GetCellNumber(i, j);
2496 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2499 GetCellInset(cell)->ascii(buf, sstr, 0);
2500 int len = int(sstr.str().length());
2501 int const n = cells_in_multicolumn(cell);
2502 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2504 if (len > int(clen[j + n - 1]))
2505 clen[j + n - 1] = len;
2509 for (int i = 0; i < rows_; ++i) {
2510 AsciiTopHLine(os, i, clen);
2511 for (int j = 0; j < columns_; ++j) {
2512 if (IsPartOfMultiColumn(i,j))
2514 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2518 AsciiBottomHLine(os, i, clen);
2524 InsetText * LyXTabular::GetCellInset(int cell) const
2527 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2531 InsetText * LyXTabular::GetCellInset(int row, int column) const
2533 #ifdef WITH_WARNINGS
2534 #warning Juergen, should we check whether the row/column values are correct?
2535 // If we do not need to do that, the tests in GetCellNumber should be
2536 // changed to asserts.
2538 cur_cell = GetCellNumber(row, column);
2539 return & cell_info[row][column].inset;
2543 void LyXTabular::Validate(LaTeXFeatures & features) const
2545 if (IsLongTabular())
2546 features.longtable = true;
2548 features.rotating = true;
2549 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2550 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2551 features.array = true;
2552 GetCellInset(cell)->validate(features);
2557 std::vector<string> const LyXTabular::getLabelList() const
2559 std::vector<string> label_list;
2560 for (int i = 0; i < rows_; ++i)
2561 for (int j = 0; j < columns_; ++j) {
2562 std::vector<string> const l =
2563 GetCellInset(i, j)->getLabelList();
2564 label_list.insert(label_list.end(),
2565 l.begin(), l.end());
2571 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2573 Paragraph * par = GetCellInset(cell)->paragraph();
2575 for (; par; par = par->next()) {
2576 for (int i = 0; i < par->size(); ++i) {
2577 if (par->getChar(i) == Paragraph::META_NEWLINE)