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
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
29 #include "lyx_gui_misc.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
34 #include "support/lstrings.h"
35 #include "support/lyxmanip.h"
36 #include "support/LAssert.h"
37 #include "insets/insettabular.h"
38 #include "insets/insettext.h"
48 #ifndef CXX_GLOBAL_CSTD
54 int const WIDTH_OF_LINE = 5;
58 /// Define a few methods for the inner structs
60 LyXTabular::cellstruct::cellstruct()
64 multicolumn = LyXTabular::CELL_NORMAL;
65 alignment = LYX_ALIGN_CENTER;
66 valignment = LYX_VALIGN_TOP;
76 LyXTabular::rowstruct::rowstruct()
86 LyXTabular::columnstruct::columnstruct()
90 alignment = LYX_ALIGN_CENTER;
91 valignment = LYX_VALIGN_TOP;
96 LyXTabular::lttype::lttype()
105 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
109 Init(rows_arg, columns_arg);
113 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
117 Init(lt.rows_, lt.columns_, <);
120 #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)
127 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
135 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
138 #warning This while method should look like this: (Lgb)
143 // If this and lt is not of the same size we have a serious bug
144 // So then it is ok to throw an exception, or for now
146 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
148 cell_info = lt.cell_info;
149 row_info = lt.row_info;
150 column_info = lt.column_info;
152 // long tabular stuff
153 SetLongTabular(lt.is_long_tabular);
154 endhead = lt.endhead;
155 endfoot = lt.endfoot;
156 endfirsthead = lt.endfirsthead;
157 endlastfoot = lt.endlastfoot;
167 LyXTabular * LyXTabular::clone(InsetTabular * inset)
169 LyXTabular * result = new LyXTabular(inset, *this);
171 // don't know if this is good but I need to Clone also
172 // the text-insets here, this is for the Undo-facility!
173 for (int i = 0; i < rows_; ++i) {
174 for (int j = 0; j < columns_; ++j) {
175 result->cell_info[i][j].inset = cell_info[i][j].inset;
176 result->cell_info[i][j].inset.setOwner(inset);
184 /* activates all lines and sets all widths to 0 */
185 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
188 columns_ = columns_arg;
189 row_info = row_vector(rows_, rowstruct());
190 column_info = column_vector(columns_, columnstruct());
191 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
199 for (int i = 0; i < rows_; ++i) {
200 for (int j = 0; j < columns_; ++j) {
201 cell_info[i][j].inset.setOwner(owner_);
202 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
203 cell_info[i][j].cellno = cellno++;
205 cell_info[i].back().right_line = true;
207 row_info.back().bottom_line = true;
208 row_info.front().bottom_line = true;
210 for (int i = 0; i < columns_; ++i) {
211 calculate_width_of_column(i);
213 column_info.back().right_line = true;
215 calculate_width_of_tabular();
217 rowofcell = vector<int>();
218 columnofcell = vector<int>();
219 set_row_column_number_info();
220 is_long_tabular = false;
223 endfirsthead.row = 0;
229 void LyXTabular::AppendRow(int cell)
233 int row = row_of_cell(cell);
235 row_vector::iterator rit = row_info.begin() + row;
236 row_info.insert(rit, rowstruct());
237 // now set the values of the row before
238 row_info[row] = row_info[row+1];
241 cell_vvector::iterator cit = cell_info.begin() + row;
242 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
244 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
247 for (int i = 0; i <= row; ++i) {
248 for (int j = 0; j < columns_; ++j) {
249 c_info[i][j] = cell_info[i][j];
252 for (int i = row + 1; i < rows_; ++i) {
253 for (int j = 0; j < columns_; ++j) {
254 c_info[i][j] = cell_info[i-1][j];
259 for (int j = 0; j < columns_; ++j) {
260 cell_info[row][j].inset.clear();
267 void LyXTabular::DeleteRow(int row)
269 // Why make it so hard? (Lgb)
272 if (rows_ == 1) return; // Not allowed to delete last row
274 row_info.erase(row_info.begin() + row); //&row_info[row]);
275 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
281 void LyXTabular::AppendColumn(int cell)
285 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
287 int const column = column_of_cell(cell);
288 column_vector::iterator cit = column_info.begin() + column + 1;
289 column_info.insert(cit, columnstruct());
290 // set the column values of the column before
291 column_info[column+1] = column_info[column];
293 for (int i = 0; i < rows_; ++i) {
294 for (int j = 0; j <= column; ++j) {
295 c_info[i][j] = cell_info[i][j];
297 for (int j = column + 1; j < columns_; ++j) {
298 c_info[i][j] = cell_info[i][j - 1];
300 // care about multicolumns
301 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
302 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
304 if ((column + 1) == columns_ ||
305 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
306 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
311 for (int i = 0; i < rows_; ++i) {
312 //cell_info[i][column].inset.clear();
313 cell_info[i][column + 1].inset.clear();
319 void LyXTabular::DeleteColumn(int column)
321 // Similar to DeleteRow
322 //if (!(columns_ - 1))
324 if (columns_ == 1) return; // Not allowed to delete last column
326 column_info.erase(column_info.begin() + column);
327 for (int i = 0; i < rows_; ++i) {
328 cell_info[i].erase(cell_info[i].begin() + column);
335 void LyXTabular::Reinit()
337 for (int i = 0; i < rows_; ++i) {
338 for (int j = 0; j < columns_; ++j) {
339 cell_info[i][j].width_of_cell = 0;
340 cell_info[i][j].inset.setOwner(owner_);
344 for (int i = 0; i < columns_; ++i) {
345 calculate_width_of_column(i);
347 calculate_width_of_tabular();
349 set_row_column_number_info();
353 void LyXTabular::set_row_column_number_info(bool oldformat)
356 for (int row = 0; row < rows_; ++row) {
357 for (int column = 0; column<columns_; ++column) {
358 if (cell_info[row][column].multicolumn
359 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
361 cell_info[row][column].cellno = numberofcells;
364 ++numberofcells; // because this is one more than as we start from 0
366 rowofcell.resize(numberofcells);
367 columnofcell.resize(numberofcells);
369 for (int row = 0, column = 0, c = 0;
370 c < numberofcells && row < rows_ && column < columns_;) {
372 columnofcell[c] = column;
376 } while (column < columns_ &&
377 cell_info[row][column].multicolumn
378 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
379 if (column == columns_) {
385 for (int row = 0; row < rows_; ++row) {
386 for (int column = 0; column < columns_; ++column) {
387 if (IsPartOfMultiColumn(row,column))
389 // now set the right line of multicolumns right for oldformat read
391 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
393 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
394 cell_info[row][column].right_line =
395 cell_info[row][column+cn-1].right_line;
397 cell_info[row][column].inset.setAutoBreakRows(
398 !GetPWidth(GetCellNumber(row, column)).empty());
404 int LyXTabular::GetNumberOfCells() const
406 return numberofcells;
410 int LyXTabular::NumberOfCellsInRow(int cell) const
412 int const row = row_of_cell(cell);
414 for (int i = 0; i < columns_; ++i) {
415 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
422 /* returns 1 if there is a topline, returns 0 if not */
423 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
425 int const row = row_of_cell(cell);
427 if (!onlycolumn && IsMultiColumn(cell))
428 return cellinfo_of_cell(cell)->top_line;
429 return row_info[row].top_line;
433 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
435 // no bottom line underneath non-existent cells if you please
436 // Isn't that a programming error? Is so this should
437 // be an Assert instead. (Lgb)
438 if (cell >= numberofcells)
441 if (!onlycolumn && IsMultiColumn(cell))
442 return cellinfo_of_cell(cell)->bottom_line;
443 return row_info[row_of_cell(cell)].bottom_line;
447 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
449 if (!onlycolumn && IsMultiColumn(cell)) {
450 #ifdef SPECIAL_COLUM_HANDLING
451 if (cellinfo_of_cell(cell)->align_special.empty())
452 return cellinfo_of_cell(cell)->left_line;
453 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
455 return cellinfo_of_cell(cell)->left_line;
458 #ifdef SPECIAL_COLUM_HANDLING
459 if (column_info[column_of_cell(cell)].align_special.empty())
460 return column_info[column_of_cell(cell)].left_line;
461 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
463 return column_info[column_of_cell(cell)].left_line;
468 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
470 if (!onlycolumn && IsMultiColumn(cell)) {
471 #ifdef SPECIAL_COLUM_HANDLING
472 if (cellinfo_of_cell(cell)->align_special.empty())
473 return cellinfo_of_cell(cell)->right_line;
474 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
476 return cellinfo_of_cell(cell)->right_line;
479 #ifdef SPECIAL_COLUM_HANDLING
480 if (column_info[column_of_cell(cell)].align_special.empty())
481 return column_info[right_column_of_cell(cell)].right_line;
482 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
484 return column_info[right_column_of_cell(cell)].right_line;
489 bool LyXTabular::TopAlreadyDrawed(int cell) const
491 int row = row_of_cell(cell);
492 if ((row > 0) && !GetAdditionalHeight(row)) {
493 int column = column_of_cell(cell);
496 && cell_info[row][column].multicolumn
497 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
499 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
500 return row_info[row].bottom_line;
502 return cell_info[row][column].bottom_line;
508 bool LyXTabular::LeftAlreadyDrawed(int cell) const
510 int column = column_of_cell(cell);
512 int row = row_of_cell(cell);
514 (cell_info[row][column].multicolumn ==
515 LyXTabular::CELL_PART_OF_MULTICOLUMN));
516 if (GetAdditionalWidth(cell_info[row][column].cellno))
518 #ifdef SPECIAL_COLUM_HANDLING
519 return column_info[column].right_line;
521 return RightLine(cell_info[row][column].cellno, true);
528 bool LyXTabular::IsLastRow(int cell) const
530 return (row_of_cell(cell) == rows_ - 1);
534 int LyXTabular::GetAdditionalHeight(int row) const
536 if (!row || row >= rows_)
542 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
543 switch (cell_info[row - 1][column].multicolumn) {
544 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
545 bottom = cell_info[row - 1][column].bottom_line;
547 case LyXTabular::CELL_NORMAL:
548 bottom = row_info[row - 1].bottom_line;
551 for (int column = 0; column < columns_ - 1 && top; ++column) {
552 switch (cell_info[row][column].multicolumn){
553 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
554 top = cell_info[row][column].top_line;
556 case LyXTabular::CELL_NORMAL:
557 top = row_info[row].top_line;
561 return WIDTH_OF_LINE;
566 int LyXTabular::GetAdditionalWidth(int cell) const
568 // internally already set in SetWidthOfCell
569 // used to get it back in text.C
570 int const col = right_column_of_cell(cell);
571 int const row = row_of_cell(cell);
572 if (col < columns_ - 1 && RightLine(cell, true) &&
573 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
575 return WIDTH_OF_LINE;
582 // returns the maximum over all rows
583 int LyXTabular::GetWidthOfColumn(int cell) const
585 int const column1 = column_of_cell(cell);
586 int const column2 = right_column_of_cell(cell);
588 for (int i = column1; i <= column2; ++i) {
589 result += column_info[i].width_of_column;
595 int LyXTabular::GetWidthOfTabular() const
597 return width_of_tabular;
601 /* returns 1 if a complete update is necessary, otherwise 0 */
602 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
604 if (!IsMultiColumn(cell))
607 int const row = row_of_cell(cell);
608 int const column1 = column_of_cell(cell);
609 int const column2 = right_column_of_cell(cell);
611 // first set columns to 0 so we can calculate the right width
612 for (int i = column1; i <= column2; ++i) {
613 cell_info[row][i].width_of_cell = 0;
615 // set the width to MAX_WIDTH until width > 0
616 int width = (new_width + 2 * WIDTH_OF_LINE);
619 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
620 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
621 width -= column_info[i].width_of_column;
624 cell_info[row][i].width_of_cell = width;
630 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
632 int const row = row_of_cell(cell);
633 int const column1 = column_of_cell(cell);
634 int const column2 = right_column_of_cell(cell);
636 // first set columns to 0 so we can calculate the right width
638 for (; i <= column2; ++i)
639 cell_info[row][i].width_of_cell = 0;
640 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
642 if (i < numberofcells)
643 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
644 SetWidthOfMulticolCell(cell, new_width);
648 /* returns 1 if a complete update is necessary, otherwise 0 */
649 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
651 int const row = row_of_cell(cell);
652 int const column1 = column_of_cell(cell);
657 #ifdef SPECIAL_COLUM_HANDLING
658 if (RightLine(cell_info[row][column1].cellno, true) &&
659 (column1 < columns_-1) &&
660 LeftLine(cell_info[row][column1+1].cellno, true))
662 if (column_info[column1].right_line && (column1 < columns_-1) &&
663 column_info[column1+1].left_line) // additional width
667 add_width = WIDTH_OF_LINE;
669 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
672 if (IsMultiColumn(cell, true)) {
673 tmp = SetWidthOfMulticolCell(cell, new_width);
675 width = (new_width + 2*WIDTH_OF_LINE + add_width);
676 cell_info[row][column1].width_of_cell = width;
677 tmp = calculate_width_of_column_NMC(column1);
681 for (; i<columns_; ++i)
682 calculate_width_of_column_NMC(i);
683 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
685 if (i < numberofcells)
686 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
687 for (i = 0; i < columns_; ++i)
688 calculate_width_of_column(i);
689 calculate_width_of_tabular();
696 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
698 if (!IsMultiColumn(cell) || onlycolumn)
699 column_info[column_of_cell(cell)].alignment = align;
701 cellinfo_of_cell(cell)->alignment = align;
706 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
708 if (!IsMultiColumn(cell) || onlycolumn)
709 column_info[column_of_cell(cell)].valignment = align;
711 cellinfo_of_cell(cell)->valignment = align;
716 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
718 bool flag = !width.empty();
719 int const j = column_of_cell(cell);
721 column_info[j].p_width = width;
722 if (flag) // do this only if there is a width
723 SetAlignment(cell, LYX_ALIGN_LEFT);
724 for (int i = 0; i < rows_; ++i) {
725 int c = GetCellNumber(i, j);
726 flag = !GetPWidth(c).empty(); // because of multicolumns!
727 GetCellInset(c)->setAutoBreakRows(flag);
733 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
735 bool const flag = !width.empty();
737 cellinfo_of_cell(cell)->p_width = width;
738 if (IsMultiColumn(cell)) {
739 GetCellInset(cell)->setAutoBreakRows(flag);
746 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
747 LyXTabular::Feature what)
749 if (what == SET_SPECIAL_MULTI)
750 cellinfo_of_cell(cell)->align_special = special;
752 column_info[column_of_cell(cell)].align_special = special;
757 bool LyXTabular::SetAllLines(int cell, bool line)
759 SetTopLine(cell, line);
760 SetBottomLine(cell, line);
761 SetRightLine(cell, line);
762 SetLeftLine(cell, line);
767 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
769 int const row = row_of_cell(cell);
771 if (onlycolumn || !IsMultiColumn(cell))
772 row_info[row].top_line = line;
774 cellinfo_of_cell(cell)->top_line = line;
779 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
781 if (onlycolumn || !IsMultiColumn(cell))
782 row_info[row_of_cell(cell)].bottom_line = line;
784 cellinfo_of_cell(cell)->bottom_line = line;
789 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
791 if (onlycolumn || !IsMultiColumn(cell))
792 column_info[column_of_cell(cell)].left_line = line;
794 cellinfo_of_cell(cell)->left_line = line;
799 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
801 if (onlycolumn || !IsMultiColumn(cell))
802 column_info[right_column_of_cell(cell)].right_line = line;
804 cellinfo_of_cell(cell)->right_line = line;
809 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
811 if (!onlycolumn && IsMultiColumn(cell))
812 return cellinfo_of_cell(cell)->alignment;
814 return column_info[column_of_cell(cell)].alignment;
818 LyXTabular::VAlignment
819 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
821 if (!onlycolumn && IsMultiColumn(cell))
822 return cellinfo_of_cell(cell)->valignment;
824 return column_info[column_of_cell(cell)].valignment;
828 string const LyXTabular::GetPWidth(int cell) const
830 if (IsMultiColumn(cell))
831 return cellinfo_of_cell(cell)->p_width;
832 return column_info[column_of_cell(cell)].p_width;
836 string const LyXTabular::GetColumnPWidth(int cell) const
838 return column_info[column_of_cell(cell)].p_width;
842 string const LyXTabular::GetMColumnPWidth(int cell) const
844 if (IsMultiColumn(cell))
845 return cellinfo_of_cell(cell)->p_width;
850 string const LyXTabular::GetAlignSpecial(int cell, int what) const
852 if (what == SET_SPECIAL_MULTI)
853 return cellinfo_of_cell(cell)->align_special;
854 return column_info[column_of_cell(cell)].align_special;
858 int LyXTabular::GetWidthOfCell(int cell) const
860 int const row = row_of_cell(cell);
861 int const column1 = column_of_cell(cell);
862 int const column2 = right_column_of_cell(cell);
864 for (int i = column1; i <= column2; ++i) {
865 result += cell_info[row][i].width_of_cell;
871 int LyXTabular::GetBeginningOfTextInCell(int cell) const
875 switch (GetAlignment(cell)){
876 case LYX_ALIGN_CENTER:
877 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
879 case LYX_ALIGN_RIGHT:
880 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
881 // + GetAdditionalWidth(cell);
883 default: /* LYX_ALIGN_LEFT: nothing :-) */
893 bool LyXTabular::IsFirstCellInRow(int cell) const
895 return column_of_cell(cell) == 0;
899 int LyXTabular::GetFirstCellInRow(int row) const
903 return cell_info[row][0].cellno;
906 bool LyXTabular::IsLastCellInRow(int cell) const
908 return (right_column_of_cell(cell) == (columns_ - 1));
912 int LyXTabular::GetLastCellInRow(int row) const
916 return cell_info[row][columns_-1].cellno;
920 bool LyXTabular::calculate_width_of_column(int column)
922 int const old_column_width = column_info[column].width_of_column;
925 for (int i = 0; i < rows_; ++i) {
926 maximum = max(cell_info[i][column].width_of_cell, maximum);
928 column_info[column].width_of_column = maximum;
929 return (column_info[column].width_of_column != old_column_width);
934 // calculate the with of the column without regarding REAL MultiColumn
935 // cells. This means MultiColumn-cells spanning more than 1 column.
937 bool LyXTabular::calculate_width_of_column_NMC(int column)
939 int const old_column_width = column_info[column].width_of_column;
941 for (int i = 0; i < rows_; ++i) {
942 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
943 (cell_info[i][column].width_of_cell > max)) {
944 max = cell_info[i][column].width_of_cell;
947 column_info[column].width_of_column = max;
948 return (column_info[column].width_of_column != old_column_width);
952 void LyXTabular::calculate_width_of_tabular()
954 width_of_tabular = 0;
955 for (int i = 0; i < columns_; ++i) {
956 width_of_tabular += column_info[i].width_of_column;
961 int LyXTabular::row_of_cell(int cell) const
963 if (cell >= numberofcells)
967 return rowofcell[cell];
971 int LyXTabular::column_of_cell(int cell) const
973 if (cell >= numberofcells)
977 return columnofcell[cell];
981 int LyXTabular::right_column_of_cell(int cell) const
983 int const row = row_of_cell(cell);
984 int column = column_of_cell(cell);
985 while (column < (columns_ - 1) &&
986 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
992 // Perfect case for a template... (Lgb)
996 string const write_attribute(string const & name, T const & t)
998 string str = " " + name + "=\"" + tostr(t) + "\"";
1003 string const write_attribute(string const & name, bool const & b)
1005 return write_attribute(name, int(b));
1010 string const write_attribute(string const & name, int value)
1012 string str = " " + name + "=\"" + tostr(value) + "\"";
1017 string const write_attribute(string const & name, string const & value)
1019 string str = " " + name + "=\"" + value + "\"";
1024 string const write_attribute(string const & name, bool value)
1026 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1034 string const tostr(LyXAlignment const & num)
1037 case LYX_ALIGN_NONE:
1039 case LYX_ALIGN_BLOCK:
1041 case LYX_ALIGN_LEFT:
1043 case LYX_ALIGN_CENTER:
1045 case LYX_ALIGN_RIGHT:
1047 case LYX_ALIGN_LAYOUT:
1049 case LYX_ALIGN_SPECIAL:
1058 string const tostr(LyXTabular::VAlignment const & num)
1061 case LyXTabular::LYX_VALIGN_TOP:
1063 case LyXTabular::LYX_VALIGN_CENTER:
1065 case LyXTabular::LYX_VALIGN_BOTTOM:
1074 string const tostr(LyXTabular::BoxType const & num)
1077 case LyXTabular::BOX_NONE:
1079 case LyXTabular::BOX_PARBOX:
1081 case LyXTabular::BOX_MINIPAGE:
1088 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1092 << write_attribute("version", 2)
1093 << write_attribute("rows", rows_)
1094 << write_attribute("columns", columns_)
1096 // global longtable options
1098 << write_attribute("rotate", tostr(rotate))
1099 << write_attribute("islongtable", tostr(is_long_tabular))
1100 << write_attribute("endhead", endhead.row)
1101 << write_attribute("endfirsthead", endfirsthead.row)
1102 << write_attribute("endfoot", endfoot.row)
1103 << write_attribute("endlastfoot", endlastfoot.row)
1105 for (int j = 0; j < columns_; ++j) {
1107 << write_attribute("alignment", tostr(column_info[j].alignment))
1108 << write_attribute("valignment", tostr(column_info[j].valignment))
1109 << write_attribute("leftline", tostr(column_info[j].left_line))
1110 << write_attribute("rightline", tostr(column_info[j].right_line))
1111 << write_attribute("width",
1112 VSpace(column_info[j].p_width)
1114 << write_attribute("special", column_info[j].align_special)
1117 for (int i = 0; i < rows_; ++i) {
1119 << write_attribute("topline", tostr(row_info[i].top_line))
1120 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1121 << write_attribute("newpage", tostr(row_info[i].newpage))
1123 for (int j = 0; j < columns_; ++j) {
1125 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1126 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1127 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1128 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1129 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1130 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1131 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1132 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1133 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1134 << write_attribute("width", cell_info[i][j].p_width)
1135 << write_attribute("special", cell_info[i][j].align_special)
1137 os << "\\begin_inset ";
1138 cell_info[i][j].inset.write(buf, os);
1139 os << "\n\\end_inset \n"
1144 os << "</lyxtabular>\n";
1150 // I would have liked a fromstr template a lot better. (Lgb)
1153 bool string2type(string const str, LyXAlignment & num)
1156 num = LYX_ALIGN_NONE;
1157 else if (str == "block")
1158 num = LYX_ALIGN_BLOCK;
1159 else if (str == "left")
1160 num = LYX_ALIGN_LEFT;
1161 else if (str == "center")
1162 num = LYX_ALIGN_CENTER;
1163 else if (str == "right")
1164 num = LYX_ALIGN_RIGHT;
1172 bool string2type(string const str, LyXTabular::VAlignment & num)
1175 num = LyXTabular::LYX_VALIGN_TOP;
1176 else if (str == "center")
1177 num = LyXTabular::LYX_VALIGN_CENTER;
1178 else if (str == "bottom")
1179 num = LyXTabular::LYX_VALIGN_BOTTOM;
1187 bool string2type(string const str, LyXTabular::BoxType & num)
1190 num = LyXTabular::BOX_NONE;
1191 else if (str == "parbox")
1192 num = LyXTabular::BOX_PARBOX;
1193 else if (str == "minipage")
1194 num = LyXTabular::BOX_MINIPAGE;
1202 bool string2type(string const str, bool & num)
1206 else if (str == "false")
1214 bool getTokenValue(string const & str, const char * token, string & ret)
1216 size_t token_length = strlen(token);
1217 string::size_type pos = str.find(token);
1219 if (pos == string::npos || pos + token_length + 1 >= str.length()
1220 || str[pos + token_length] != '=')
1223 pos += token_length + 1;
1225 if ((ch != '"') && (ch != '\'')) { // only read till next space
1229 while((pos < str.length() - 1) && (str[++pos] != ch))
1236 bool getTokenValue(string const & str, const char * token, int & num)
1239 if (!getTokenValue(str, token, tmp))
1241 num = strToInt(tmp);
1246 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1249 if (!getTokenValue(str, token, tmp))
1251 return string2type(tmp, num);
1255 bool getTokenValue(string const & str, const char * token,
1256 LyXTabular::VAlignment & num)
1259 if (!getTokenValue(str, token, tmp))
1261 return string2type(tmp, num);
1265 bool getTokenValue(string const & str, const char * token,
1266 LyXTabular::BoxType & num)
1269 if (!getTokenValue(str, token, tmp))
1271 return string2type(tmp, num);
1275 bool getTokenValue(string const & str, const char * token, bool & flag)
1278 if (!getTokenValue(str, token, tmp))
1280 return string2type(tmp, flag);
1285 void l_getline(istream & is, string & str)
1288 while (str.empty()) {
1290 if (!str.empty() && str[str.length() - 1] == '\r')
1291 str.erase(str.length() - 1);
1298 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1301 istream & is = lex.getStream();
1303 l_getline(is, line);
1304 if (!prefixIs(line, "<lyxtabular ")
1305 && !prefixIs(line, "<LyXTabular ")) {
1306 OldFormatRead(lex, line);
1311 if (!getTokenValue(line, "version", version))
1314 ReadOld(buf, is, lex, line);
1315 else if (version == 2)
1316 ReadNew(buf, is, lex, line);
1320 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1321 LyXLex & lex, string const & l)
1325 if (!getTokenValue(line, "rows", rows_arg))
1328 if (!getTokenValue(line, "columns", columns_arg))
1330 Init(rows_arg, columns_arg);
1331 l_getline(is, line);
1332 if (!prefixIs(line, "<features")) {
1333 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1334 line << ")" << endl;
1337 getTokenValue(line, "rotate", rotate);
1338 getTokenValue(line, "islongtable", is_long_tabular);
1339 getTokenValue(line, "endhead", endhead.row);
1340 getTokenValue(line, "endfirsthead", endfirsthead.row);
1341 getTokenValue(line, "endfoot", endfoot.row);
1342 getTokenValue(line, "endlastfoot", endlastfoot.row);
1343 endhead.row = abs(endhead.row);
1344 endfirsthead.row = abs(endfirsthead.row);
1345 endfoot.row = abs(endfoot.row);
1346 endlastfoot.row = abs(endlastfoot.row);
1347 for (int j = 0; j < columns_; ++j) {
1349 if (!prefixIs(line,"<column")) {
1350 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1351 line << ")" << endl;
1354 getTokenValue(line, "alignment", column_info[j].alignment);
1355 getTokenValue(line, "valignment", column_info[j].valignment);
1356 getTokenValue(line, "leftline", column_info[j].left_line);
1357 getTokenValue(line, "rightline", column_info[j].right_line);
1358 getTokenValue(line, "width", column_info[j].p_width);
1359 getTokenValue(line, "special", column_info[j].align_special);
1362 for (int i = 0; i < rows_; ++i) {
1363 l_getline(is, line);
1364 if (!prefixIs(line, "<row")) {
1365 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1366 line << ")" << endl;
1369 getTokenValue(line, "topline", row_info[i].top_line);
1370 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1371 getTokenValue(line, "newpage", row_info[i].newpage);
1372 for (int j = 0; j < columns_; ++j) {
1373 l_getline(is, line);
1374 if (!prefixIs(line, "<cell")) {
1375 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1376 line << ")" << endl;
1379 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1380 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1381 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1382 getTokenValue(line, "topline", cell_info[i][j].top_line);
1383 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1384 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1385 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1386 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1387 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1388 getTokenValue(line, "width", cell_info[i][j].p_width);
1389 getTokenValue(line, "special", cell_info[i][j].align_special);
1390 l_getline(is, line);
1391 if (prefixIs(line, "\\begin_inset")) {
1392 cell_info[i][j].inset.read(buf, lex);
1393 l_getline(is, line);
1395 if (!prefixIs(line, "</cell>")) {
1396 lyxerr << "Wrong tabular format (expected </cell> got" <<
1397 line << ")" << endl;
1401 l_getline(is, line);
1402 if (!prefixIs(line, "</row>")) {
1403 lyxerr << "Wrong tabular format (expected </row> got" <<
1404 line << ")" << endl;
1408 while (!prefixIs(line, "</lyxtabular>")) {
1409 l_getline(is, line);
1411 set_row_column_number_info();
1415 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1421 int columns_arg = 0;
1422 int is_long_tabular_arg = false;
1423 int rotate_arg = false;
1432 istream & is = lex.getStream();
1435 version = lyx::atoi(s.substr(8, string::npos));
1439 vector<int> cont_row_info;
1442 lyxerr << "Tabular format < 5 is not supported anymore\n"
1443 "Get an older version of LyX (< 1.1.x) for conversion!"
1445 WriteAlert(_("Warning:"),
1446 _("Tabular format < 5 is not supported anymore\n"),
1447 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1449 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1450 >> rotate_arg >> a >> b >> c >> d;
1452 is >> rows_arg >> columns_arg;
1453 Init(rows_arg, columns_arg);
1454 cont_row_info = vector<int>(rows_arg);
1455 SetLongTabular(is_long_tabular_arg);
1456 SetRotateTabular(rotate_arg);
1458 for (i = 0; i < rows_; ++i) {
1460 cont_row_info[i] = false;
1462 for (i = 0; i < columns_; ++i) {
1465 for (i = 0; i < rows_; ++i) {
1466 for (j = 0; j < columns_; ++j) {
1471 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1472 >> rotate_arg >> a >> b >> c >> d;
1473 Init(rows_arg, columns_arg);
1474 cont_row_info = vector<int>(rows_arg);
1475 SetLongTabular(is_long_tabular_arg);
1476 SetRotateTabular(rotate_arg);
1477 endhead.row = a + 1;
1478 endfirsthead.row = b + 1;
1479 endfoot.row = c + 1;
1480 endlastfoot.row = d + 1;
1481 for (i = 0; i < rows_; ++i) {
1482 a = b = c = d = e = f = g = 0;
1483 is >> a >> b >> c >> d;
1484 row_info[i].top_line = a;
1485 row_info[i].bottom_line = b;
1486 cont_row_info[i] = c;
1487 row_info[i].newpage = d;
1489 for (i = 0; i < columns_; ++i) {
1494 char ch; // skip '"'
1497 // ignore is buggy but we will use it later (Lgb)
1498 is.ignore(); // skip '"'
1500 getline(is, s1, '"');
1502 is >> ch; // skip '"'
1504 // ignore is buggy but we will use it later (Lgb)
1505 is.ignore(); // skip '"'
1507 getline(is, s2, '"');
1508 column_info[i].alignment = static_cast<LyXAlignment>(a);
1509 column_info[i].left_line = b;
1510 column_info[i].right_line = c;
1511 column_info[i].p_width = s1;
1512 column_info[i].align_special = s2;
1514 for (i = 0; i < rows_; ++i) {
1515 for (j = 0; j < columns_; ++j) {
1518 is >> a >> b >> c >> d >> e >> f >> g;
1521 is >> ch; // skip '"'
1523 // ignore is buggy but we will use it later (Lgb)
1524 is.ignore(); // skip '"'
1526 getline(is, s1, '"');
1528 is >> ch; // skip '"'
1530 // ignore is buggy but we will use it later (Lgb)
1531 is.ignore(); // skip '"'
1533 getline(is, s2, '"');
1534 cell_info[i][j].multicolumn = static_cast<char>(a);
1535 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1536 cell_info[i][j].top_line = static_cast<char>(c);
1537 cell_info[i][j].bottom_line = static_cast<char>(d);
1538 cell_info[i][j].left_line = column_info[j].left_line;
1539 cell_info[i][j].right_line = column_info[j].right_line;
1540 cell_info[i][j].rotate = static_cast<bool>(f);
1541 cell_info[i][j].usebox = static_cast<BoxType>(g);
1542 cell_info[i][j].align_special = s1;
1543 cell_info[i][j].p_width = s2;
1547 set_row_column_number_info(true);
1549 Paragraph * par = new Paragraph;
1550 Paragraph * return_par = 0;
1554 Paragraph::depth_type depth = 0;
1555 LyXFont font(LyXFont::ALL_INHERIT);
1556 font.setLanguage(owner_->bufferOwner()->getLanguage());
1558 while (lex.isOK()) {
1560 string const token = lex.getString();
1563 if (token == "\\layout"
1564 || token == "\\end_float"
1565 || token == "\\end_deeper") {
1566 lex.pushToken(token);
1567 #ifndef NO_COMPABILITY
1568 // Here we need to insert the inset_ert_contents into the last
1569 // cell of the tabular.
1570 owner_->bufferOwner()->insertErtContents(par, pos, font);
1574 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1579 lex.pushToken(token);
1583 lex.printError("New Paragraph allocated! This should not happen!");
1584 lex.pushToken(token);
1590 // now we have the par we should fill the insets with this!
1592 InsetText * inset = GetCellInset(cell);
1595 for (int i = 0; i < par->size(); ++i) {
1596 if (par->isNewline(i)) {
1598 if (cell > numberofcells) {
1599 lyxerr << "Some error in reading old table format occured!" <<
1600 endl << "Terminating when reading cell[" << cell << "]!" <<
1605 row = row_of_cell(cell);
1606 if (cont_row_info[row]) {
1608 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1609 while(!IsFirstCellInRow(--cell));
1611 inset = GetCellInset(cell);
1614 inset = GetCellInset(cell);
1615 row = row_of_cell(cell);
1616 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1618 // insert a space instead
1620 par->insertChar(i, ' ');
1623 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1624 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1631 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1633 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1634 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1638 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1640 int const row = row_of_cell(cell);
1641 int const column = column_of_cell(cell);
1642 return &cell_info[row][column];
1646 void LyXTabular::SetMultiColumn(int cell, int number)
1648 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1649 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1650 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1651 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1652 for (number--; number > 0; --number) {
1653 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1655 set_row_column_number_info();
1659 int LyXTabular::cells_in_multicolumn(int cell) const
1661 int const row = row_of_cell(cell);
1662 int column = column_of_cell(cell);
1665 while ((column < columns_) &&
1666 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1675 int LyXTabular::UnsetMultiColumn(int cell)
1677 int const row = row_of_cell(cell);
1678 int column = column_of_cell(cell);
1682 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1683 cell_info[row][column].multicolumn = CELL_NORMAL;
1685 while ((column < columns_) &&
1686 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1688 cell_info[row][column].multicolumn = CELL_NORMAL;
1693 set_row_column_number_info();
1698 void LyXTabular::SetLongTabular(bool what)
1700 is_long_tabular = what;
1704 bool LyXTabular::IsLongTabular() const
1706 return is_long_tabular;
1710 void LyXTabular::SetRotateTabular(bool flag)
1716 bool LyXTabular::GetRotateTabular() const
1722 void LyXTabular::SetRotateCell(int cell, bool flag)
1724 cellinfo_of_cell(cell)->rotate = flag;
1728 bool LyXTabular::GetRotateCell(int cell) const
1730 return cellinfo_of_cell(cell)->rotate;
1734 bool LyXTabular::NeedRotating() const
1738 for (int i = 0; i < rows_; ++i) {
1739 for (int j = 0; j < columns_; ++j) {
1740 if (cell_info[i][j].rotate)
1748 bool LyXTabular::IsLastCell(int cell) const
1750 if ((cell + 1) < numberofcells)
1756 int LyXTabular::GetCellAbove(int cell) const
1758 if (row_of_cell(cell) > 0)
1759 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1764 int LyXTabular::GetCellBelow(int cell) const
1766 if (row_of_cell(cell) + 1 < rows_)
1767 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1772 int LyXTabular::GetLastCellAbove(int cell) const
1774 if (row_of_cell(cell) <= 0)
1776 if (!IsMultiColumn(cell))
1777 return GetCellAbove(cell);
1778 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1782 int LyXTabular::GetLastCellBelow(int cell) const
1784 if (row_of_cell(cell) + 1 >= rows_)
1786 if (!IsMultiColumn(cell))
1787 return GetCellBelow(cell);
1788 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1792 int LyXTabular::GetCellNumber(int row, int column) const
1795 if (column >= columns_)
1796 column = columns_ - 1;
1797 else if (column < 0)
1804 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1806 return cell_info[row][column].cellno;
1810 void LyXTabular::SetUsebox(int cell, BoxType type)
1812 cellinfo_of_cell(cell)->usebox = type;
1816 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1818 if (column_info[column_of_cell(cell)].p_width.empty() &&
1819 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1821 if (cellinfo_of_cell(cell)->usebox > 1)
1822 return cellinfo_of_cell(cell)->usebox;
1823 return UseParbox(cell);
1826 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1828 if (!ltt.row || (ltt.row > rows_))
1830 return (row == (ltt.row - 1));
1834 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1844 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1847 return checkLTType(row, hd);
1851 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1854 return checkLTType(row, hd);
1858 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1868 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1871 return checkLTType(row, fd);
1875 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1878 return checkLTType(row, fd);
1882 void LyXTabular::SetLTNewPage(int cell, bool what)
1884 row_info[row_of_cell(cell)].newpage = what;
1888 bool LyXTabular::GetLTNewPage(int cell) const
1890 return row_info[row_of_cell(cell)].newpage;
1894 bool LyXTabular::SetAscentOfRow(int row, int height)
1896 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1898 row_info[row].ascent_of_row = height;
1903 bool LyXTabular::SetDescentOfRow(int row, int height)
1905 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1907 row_info[row].descent_of_row = height;
1912 int LyXTabular::GetAscentOfRow(int row) const
1916 return row_info[row].ascent_of_row;
1920 int LyXTabular::GetDescentOfRow(int row) const
1924 return row_info[row].descent_of_row;
1928 int LyXTabular::GetHeightOfTabular() const
1932 for (int row = 0; row < rows_; ++row)
1933 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1934 GetAdditionalHeight(row);
1939 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1941 if ((row >= rows_) || (column >= columns_))
1943 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1947 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1949 if ((row < 0) || (row >= rows_))
1952 int const fcell = GetFirstCellInRow(row);
1953 int const n = NumberOfCellsInRow(fcell) + fcell;
1956 for (int i = fcell; i < n; ++i) {
1960 if (tmp == (n - fcell)){
1963 for (int i = fcell; i < n; ++i) {
1966 << column_of_cell(i) + 1
1968 << right_column_of_cell(i) + 1
1980 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1982 if ((row < 0) || (row >= rows_))
1985 int const fcell = GetFirstCellInRow(row);
1986 int const n = NumberOfCellsInRow(fcell) + fcell;
1989 for (int i = fcell; i < n; ++i) {
1993 if (tmp == (n-fcell)){
1996 for (int i = fcell; i < n; ++i) {
1997 if (BottomLine(i)) {
1999 << column_of_cell(i) + 1
2001 << right_column_of_cell(i) + 1
2013 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2017 if (GetRotateCell(cell)) {
2018 os << "\\begin{sideways}\n";
2021 if (IsMultiColumn(cell)) {
2022 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2023 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2024 os << cellinfo_of_cell(cell)->align_special << "}{";
2026 if (LeftLine(cell) &&
2027 (IsFirstCellInRow(cell) ||
2028 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2029 !RightLine(cell-1, true))))
2033 if (!GetPWidth(cell).empty()) {
2034 switch (GetVAlignment(cell)) {
2035 case LYX_VALIGN_TOP:
2038 case LYX_VALIGN_CENTER:
2041 case LYX_VALIGN_BOTTOM:
2045 os << "{" << GetPWidth(cell) << '}';
2047 switch (GetAlignment(cell)) {
2048 case LYX_ALIGN_LEFT:
2051 case LYX_ALIGN_RIGHT:
2059 if (RightLine(cell))
2061 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2067 if (GetUsebox(cell) == BOX_PARBOX) {
2069 switch (GetVAlignment(cell)) {
2070 case LYX_VALIGN_TOP:
2073 case LYX_VALIGN_CENTER:
2076 case LYX_VALIGN_BOTTOM:
2080 os << "]{" << GetPWidth(cell) << "}{";
2081 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2082 os << "\\begin{minipage}[";
2083 switch (GetVAlignment(cell)) {
2084 case LYX_VALIGN_TOP:
2087 case LYX_VALIGN_CENTER:
2090 case LYX_VALIGN_BOTTOM:
2094 os << "]{" << GetPWidth(cell) << "}\n";
2101 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2106 if (GetUsebox(cell) == BOX_PARBOX)
2108 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2109 os << "%\n\\end{minipage}";
2112 if (IsMultiColumn(cell)){
2115 if (GetRotateCell(cell)) {
2116 os << "%\n\\end{sideways}";
2123 int LyXTabular::Latex(Buffer const * buf,
2124 ostream & os, bool fragile, bool fp) const
2129 //+---------------------------------------------------------------------
2130 //+ first the opening preamble +
2131 //+---------------------------------------------------------------------
2134 os << "\\begin{sideways}\n";
2137 if (is_long_tabular)
2138 os << "\\begin{longtable}{";
2140 os << "\\begin{tabular}{";
2141 for (int i = 0; i < columns_; ++i) {
2142 if (!column_info[i].align_special.empty()) {
2143 os << column_info[i].align_special;
2145 if (column_info[i].left_line)
2147 if (!column_info[i].p_width.empty()) {
2148 switch (column_info[i].valignment) {
2149 case LYX_VALIGN_TOP:
2152 case LYX_VALIGN_CENTER:
2155 case LYX_VALIGN_BOTTOM:
2160 << column_info[i].p_width
2163 switch (column_info[i].alignment) {
2164 case LYX_ALIGN_LEFT:
2167 case LYX_ALIGN_RIGHT:
2175 if (column_info[i].right_line)
2182 //+---------------------------------------------------------------------
2183 //+ the single row and columns (cells) +
2184 //+---------------------------------------------------------------------
2186 for (int i = 0; i < rows_; ++i) {
2187 ret += TeXTopHLine(os, i);
2188 #warning Implement top double lines for LT Header/Footers
2191 ret += TeXBottomHLine(os, i-1);
2192 ret += TeXTopHLine(os, i);
2195 for (int j = 0; j < columns_; ++j) {
2196 if (IsPartOfMultiColumn(i,j))
2198 ret += TeXCellPreamble(os, cell);
2199 InsetText * inset = GetCellInset(cell);
2201 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2202 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2206 ret += inset->latex(buf, os, fragile, fp);
2210 ret += TeXCellPostamble(os, cell);
2211 if (!IsLastCellInRow(cell)) { // not last cell in row
2218 ret += TeXBottomHLine(os, i);
2219 if (IsLongTabular()) {
2220 if (i == (endhead.row - 1)) {
2221 if (endhead.bottomDL)
2222 ret += TeXBottomHLine(os, i);
2223 os << "\\endhead\n";
2226 if (i == (endfirsthead.row - 1)) {
2227 if (endfirsthead.bottomDL)
2228 ret += TeXBottomHLine(os, i);
2229 os << "\\endfirsthead\n";
2232 if (i == (endfoot.row - 1)) {
2233 if (endfoot.bottomDL)
2234 ret += TeXBottomHLine(os, i);
2235 os << "\\endfoot\n";
2238 if (i == (endlastfoot.row - 1)) {
2239 if (endlastfoot.bottomDL)
2240 ret += TeXBottomHLine(os, i);
2241 os << "\\endlastfoot\n";
2244 if (row_info[i].newpage) {
2245 os << "\\newpage\n";
2251 //+---------------------------------------------------------------------
2252 //+ the closing of the tabular +
2253 //+---------------------------------------------------------------------
2255 if (is_long_tabular)
2256 os << "\\end{longtable}";
2258 os << "\\end{tabular}";
2260 os << "\n\\end{sideways}";
2268 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2271 int cell = GetFirstCellInRow(row);
2274 for (int j = 0; j < columns_; ++j) {
2275 if (IsPartOfMultiColumn(row, j))
2278 os << "<entry align=\"";
2279 switch (GetAlignment(cell)) {
2280 case LYX_ALIGN_LEFT:
2283 case LYX_ALIGN_RIGHT:
2291 os << "\" valign=\"";
2292 switch (GetVAlignment(cell)) {
2293 case LYX_VALIGN_TOP:
2296 case LYX_VALIGN_BOTTOM:
2299 case LYX_VALIGN_CENTER:
2304 if (IsMultiColumn(cell)) {
2305 os << " namest=\"col" << j << "\" ";
2306 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2310 ret += GetCellInset(cell)->docbook(buf, os);
2319 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2323 //+---------------------------------------------------------------------
2324 //+ first the opening preamble +
2325 //+---------------------------------------------------------------------
2327 os << "<tgroup cols=\"" << columns_
2328 << "\" colsep=\"1\" rowsep=\"1\">\n";
2330 for (int i = 0; i < columns_; ++i) {
2331 os << "<colspec colname=\"col" << i << "\" align=\"";
2332 switch (column_info[i].alignment) {
2333 case LYX_ALIGN_LEFT:
2336 case LYX_ALIGN_RIGHT:
2347 //+---------------------------------------------------------------------
2348 //+ Long Tabular case +
2349 //+---------------------------------------------------------------------
2351 if ( IsLongTabular() ) {
2353 if( endhead.row || endfirsthead.row ) {
2355 if( endfirsthead.row ) {
2356 docbookRow( buf, os, endfirsthead.row - 1);
2358 if( endhead.row && endhead.row != endfirsthead.row) {
2359 docbookRow(buf, os, endhead.row - 1);
2365 if( endfoot.row || endlastfoot.row ) {
2368 docbookRow( buf, os, endfoot.row - 1);
2370 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2371 docbookRow( buf, os, endlastfoot.row - 1);
2376 //+---------------------------------------------------------------------
2377 //+ the single row and columns (cells) +
2378 //+---------------------------------------------------------------------
2381 for (int i = 0; i < rows_; ++i) {
2382 if(!IsLongTabular() || (
2383 i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2384 i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2385 docbookRow( buf, os, i);
2389 //+---------------------------------------------------------------------
2390 //+ the closing of the tabular +
2391 //+---------------------------------------------------------------------
2403 void print_n_chars(ostream & os, unsigned char ch, int n)
2405 os << string(n, ch);
2411 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2412 vector<unsigned int> const & clen) const
2414 int const fcell = GetFirstCellInRow(row);
2415 int const n = NumberOfCellsInRow(fcell) + fcell;
2418 for (int i = fcell; i < n; ++i) {
2428 for (int i = fcell; i < n; ++i) {
2439 int column = column_of_cell(i);
2440 int len = clen[column];
2441 while(IsPartOfMultiColumn(row, ++column))
2442 len += clen[column] + 4;
2443 print_n_chars(os, ch, len);
2458 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2459 vector<unsigned int> const & clen) const
2461 int const fcell = GetFirstCellInRow(row);
2462 int const n = NumberOfCellsInRow(fcell) + fcell;
2465 for (int i = fcell; i < n; ++i) {
2466 if (BottomLine(i)) {
2475 for (int i = fcell; i < n; ++i) {
2476 if (BottomLine(i)) {
2486 int column = column_of_cell(i);
2487 int len = clen[column];
2488 while(IsPartOfMultiColumn(row, ++column))
2489 len += clen[column] + 4;
2490 print_n_chars(os, ch, len);
2491 if (BottomLine(i)) {
2505 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2506 int cell, int row, int column,
2507 vector<unsigned int> const & clen) const
2510 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2517 unsigned int len1 = sstr.str().length();
2518 unsigned int len2 = clen[column];
2519 while(IsPartOfMultiColumn(row, ++column))
2520 len2 += clen[column] + 4;
2523 switch (GetAlignment(cell)) {
2525 case LYX_ALIGN_LEFT:
2528 case LYX_ALIGN_RIGHT:
2532 case LYX_ALIGN_CENTER:
2538 for (unsigned int i = 0; i < len1; ++i)
2541 for (unsigned int i = 0; i < len2; ++i)
2543 if (RightLine(cell))
2552 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2556 //+---------------------------------------------------------------------
2557 //+ first calculate the width of the single columns +
2558 //+---------------------------------------------------------------------
2559 vector<unsigned int> clen(columns_);
2561 // first all non (real) multicolumn cells!
2562 for (int j = 0; j < columns_; ++j) {
2564 for (int i = 0; i < rows_; ++i) {
2565 int cell = GetCellNumber(i, j);
2566 if (IsMultiColumn(cell, true))
2569 GetCellInset(cell)->ascii(buf, sstr, 0);
2570 if (clen[j] < sstr.str().length())
2571 clen[j] = sstr.str().length();
2574 // then all (real) multicolumn cells!
2575 for (int j = 0; j < columns_; ++j) {
2576 for (int i = 0; i < rows_; ++i) {
2577 int cell = GetCellNumber(i, j);
2578 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2581 GetCellInset(cell)->ascii(buf, sstr, 0);
2582 int len = int(sstr.str().length());
2583 int const n = cells_in_multicolumn(cell);
2584 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2586 if (len > int(clen[j + n - 1]))
2587 clen[j + n - 1] = len;
2591 for (int i = 0; i < rows_; ++i) {
2592 AsciiTopHLine(os, i, clen);
2593 for (int j = 0; j < columns_; ++j) {
2594 if (IsPartOfMultiColumn(i,j))
2596 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2600 AsciiBottomHLine(os, i, clen);
2606 InsetText * LyXTabular::GetCellInset(int cell) const
2609 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2613 InsetText * LyXTabular::GetCellInset(int row, int column) const
2615 cur_cell = GetCellNumber(row, column);
2616 return & cell_info[row][column].inset;
2620 void LyXTabular::Validate(LaTeXFeatures & features) const
2622 if (IsLongTabular())
2623 features.longtable = true;
2625 features.rotating = true;
2626 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2627 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2628 features.array = true;
2629 GetCellInset(cell)->validate(features);
2634 std::vector<string> const LyXTabular::getLabelList() const
2636 std::vector<string> label_list;
2637 for (int i = 0; i < rows_; ++i)
2638 for (int j = 0; j < columns_; ++j) {
2639 std::vector<string> const l =
2640 GetCellInset(i, j)->getLabelList();
2641 label_list.insert(label_list.end(),
2642 l.begin(), l.end());
2648 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2650 Paragraph * par = GetCellInset(cell)->paragraph();
2652 for (; par; par = par->next()) {
2653 for (int i = 0; i < par->size(); ++i) {
2654 if (par->getChar(i) == Paragraph::META_NEWLINE)