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 "frontends/Alert.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()
90 LyXTabular::columnstruct::columnstruct()
94 alignment = LYX_ALIGN_CENTER;
95 valignment = LYX_VALIGN_TOP;
100 LyXTabular::lttype::lttype()
109 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
113 Init(rows_arg, columns_arg);
117 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
122 Init(lt.rows_, lt.columns_, <);
123 // we really should change again to have InsetText as a pointer
124 // and allocate it then we would not have to do this stuff all
127 for (int i = 0; i < rows_; ++i) {
128 for (int j = 0; j < columns_; ++j) {
129 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
130 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
136 #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)
143 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
151 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
154 #warning This while method should look like this: (Lgb)
159 // If this and lt is not of the same size we have a serious bug
160 // So then it is ok to throw an exception, or for now
162 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
164 cell_info = lt.cell_info;
165 row_info = lt.row_info;
166 column_info = lt.column_info;
167 SetLongTabular(lt.is_long_tabular);
176 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
178 LyXTabular * result = new LyXTabular(inset, *this, same_id);
180 // don't know if this is good but I need to Clone also
181 // the text-insets here, this is for the Undo-facility!
182 for (int i = 0; i < rows_; ++i) {
183 for (int j = 0; j < columns_; ++j) {
184 result->cell_info[i][j].inset = cell_info[i][j].inset;
185 result->cell_info[i][j].inset.setOwner(inset);
193 /* activates all lines and sets all widths to 0 */
194 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
197 columns_ = columns_arg;
198 row_info = row_vector(rows_, rowstruct());
199 column_info = column_vector(columns_, columnstruct());
200 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
208 for (int i = 0; i < rows_; ++i) {
209 for (int j = 0; j < columns_; ++j) {
210 cell_info[i][j].inset.setOwner(owner_);
211 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
212 cell_info[i][j].cellno = cellno++;
214 cell_info[i].back().right_line = true;
216 row_info.back().bottom_line = true;
217 row_info.front().bottom_line = true;
219 for (int i = 0; i < columns_; ++i) {
220 calculate_width_of_column(i);
222 column_info.back().right_line = true;
224 calculate_width_of_tabular();
226 rowofcell = vector<int>();
227 columnofcell = vector<int>();
228 set_row_column_number_info();
229 is_long_tabular = false;
234 void LyXTabular::AppendRow(int cell)
238 int row = row_of_cell(cell);
240 row_vector::iterator rit = row_info.begin() + row;
241 row_info.insert(rit, rowstruct());
242 // now set the values of the row before
243 row_info[row] = row_info[row+1];
246 cell_vvector::iterator cit = cell_info.begin() + row;
247 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
249 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
252 for (int i = 0; i <= row; ++i) {
253 for (int j = 0; j < columns_; ++j) {
254 c_info[i][j] = cell_info[i][j];
257 for (int i = row + 1; i < rows_; ++i) {
258 for (int j = 0; j < columns_; ++j) {
259 c_info[i][j] = cell_info[i-1][j];
264 for (int j = 0; j < columns_; ++j) {
265 cell_info[row][j].inset.clear();
272 void LyXTabular::DeleteRow(int row)
274 if (rows_ == 1) return; // Not allowed to delete last row
276 row_info.erase(row_info.begin() + row); //&row_info[row]);
277 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
283 void LyXTabular::AppendColumn(int cell)
287 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
289 int const column = column_of_cell(cell);
290 column_vector::iterator cit = column_info.begin() + column + 1;
291 column_info.insert(cit, columnstruct());
292 // set the column values of the column before
293 column_info[column+1] = column_info[column];
295 for (int i = 0; i < rows_; ++i) {
296 for (int j = 0; j <= column; ++j) {
297 c_info[i][j] = cell_info[i][j];
299 for (int j = column + 1; j < columns_; ++j) {
300 c_info[i][j] = cell_info[i][j - 1];
302 // care about multicolumns
303 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
304 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
306 if ((column + 1) == columns_ ||
307 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
308 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
313 for (int i = 0; i < rows_; ++i) {
314 //cell_info[i][column].inset.clear();
315 cell_info[i][column + 1].inset.clear();
321 void LyXTabular::DeleteColumn(int column)
323 // Similar to DeleteRow
324 //if (!(columns_ - 1))
326 if (columns_ == 1) return; // Not allowed to delete last column
328 column_info.erase(column_info.begin() + column);
329 for (int i = 0; i < rows_; ++i) {
330 cell_info[i].erase(cell_info[i].begin() + column);
337 void LyXTabular::reinit()
343 void LyXTabular::Reinit(bool reset_widths)
346 for (int i = 0; i < rows_; ++i) {
347 for (int j = 0; j < columns_; ++j) {
348 cell_info[i][j].width_of_cell = 0;
349 cell_info[i][j].inset.setOwner(owner_);
354 for (int i = 0; i < columns_; ++i) {
355 calculate_width_of_column(i);
357 calculate_width_of_tabular();
359 set_row_column_number_info();
363 void LyXTabular::set_row_column_number_info(bool oldformat)
366 for (int row = 0; row < rows_; ++row) {
367 for (int column = 0; column<columns_; ++column) {
368 if (cell_info[row][column].multicolumn
369 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
371 cell_info[row][column].cellno = numberofcells;
374 ++numberofcells; // because this is one more than as we start from 0
376 rowofcell.resize(numberofcells);
377 columnofcell.resize(numberofcells);
379 for (int row = 0, column = 0, c = 0;
380 c < numberofcells && row < rows_ && column < columns_;) {
382 columnofcell[c] = column;
386 } while (column < columns_ &&
387 cell_info[row][column].multicolumn
388 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
389 if (column == columns_) {
395 for (int row = 0; row < rows_; ++row) {
396 for (int column = 0; column < columns_; ++column) {
397 if (IsPartOfMultiColumn(row,column))
399 // now set the right line of multicolumns right for oldformat read
401 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
403 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
404 cell_info[row][column].right_line =
405 cell_info[row][column+cn-1].right_line;
407 cell_info[row][column].inset.setAutoBreakRows(
408 !GetPWidth(GetCellNumber(row, column)).zero());
414 int LyXTabular::GetNumberOfCells() const
416 return numberofcells;
420 int LyXTabular::NumberOfCellsInRow(int cell) const
422 int const row = row_of_cell(cell);
424 for (int i = 0; i < columns_; ++i) {
425 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
432 /* returns 1 if there is a topline, returns 0 if not */
433 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
435 int const row = row_of_cell(cell);
437 if (!onlycolumn && IsMultiColumn(cell))
438 return cellinfo_of_cell(cell)->top_line;
439 return row_info[row].top_line;
443 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
445 // no bottom line underneath non-existent cells if you please
446 // Isn't that a programming error? Is so this should
447 // be an Assert instead. (Lgb)
448 if (cell >= numberofcells)
451 if (!onlycolumn && IsMultiColumn(cell))
452 return cellinfo_of_cell(cell)->bottom_line;
453 return row_info[row_of_cell(cell)].bottom_line;
457 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
459 if (!onlycolumn && IsMultiColumn(cell)) {
460 #ifdef SPECIAL_COLUM_HANDLING
461 if (cellinfo_of_cell(cell)->align_special.empty())
462 return cellinfo_of_cell(cell)->left_line;
463 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
465 return cellinfo_of_cell(cell)->left_line;
468 #ifdef SPECIAL_COLUM_HANDLING
469 if (column_info[column_of_cell(cell)].align_special.empty())
470 return column_info[column_of_cell(cell)].left_line;
471 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
473 return column_info[column_of_cell(cell)].left_line;
478 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
480 if (!onlycolumn && IsMultiColumn(cell)) {
481 #ifdef SPECIAL_COLUM_HANDLING
482 if (cellinfo_of_cell(cell)->align_special.empty())
483 return cellinfo_of_cell(cell)->right_line;
484 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
486 return cellinfo_of_cell(cell)->right_line;
489 #ifdef SPECIAL_COLUM_HANDLING
490 if (column_info[column_of_cell(cell)].align_special.empty())
491 return column_info[right_column_of_cell(cell)].right_line;
492 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
494 return column_info[right_column_of_cell(cell)].right_line;
499 bool LyXTabular::TopAlreadyDrawed(int cell) const
501 int row = row_of_cell(cell);
502 if ((row > 0) && !GetAdditionalHeight(row)) {
503 int column = column_of_cell(cell);
506 && cell_info[row][column].multicolumn
507 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
509 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
510 return row_info[row].bottom_line;
512 return cell_info[row][column].bottom_line;
518 bool LyXTabular::LeftAlreadyDrawed(int cell) const
520 int column = column_of_cell(cell);
522 int row = row_of_cell(cell);
524 (cell_info[row][column].multicolumn ==
525 LyXTabular::CELL_PART_OF_MULTICOLUMN));
526 if (GetAdditionalWidth(cell_info[row][column].cellno))
528 #ifdef SPECIAL_COLUM_HANDLING
529 return column_info[column].right_line;
531 return RightLine(cell_info[row][column].cellno, true);
538 bool LyXTabular::IsLastRow(int cell) const
540 return (row_of_cell(cell) == rows_ - 1);
544 int LyXTabular::GetAdditionalHeight(int row) const
546 if (!row || row >= rows_)
552 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
553 switch (cell_info[row - 1][column].multicolumn) {
554 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
555 bottom = cell_info[row - 1][column].bottom_line;
557 case LyXTabular::CELL_NORMAL:
558 bottom = row_info[row - 1].bottom_line;
561 for (int column = 0; column < columns_ - 1 && top; ++column) {
562 switch (cell_info[row][column].multicolumn){
563 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
564 top = cell_info[row][column].top_line;
566 case LyXTabular::CELL_NORMAL:
567 top = row_info[row].top_line;
571 return WIDTH_OF_LINE;
576 int LyXTabular::GetAdditionalWidth(int cell) const
578 // internally already set in SetWidthOfCell
579 // used to get it back in text.C
580 int const col = right_column_of_cell(cell);
581 int const row = row_of_cell(cell);
582 if (col < columns_ - 1 && RightLine(cell, true) &&
583 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
585 return WIDTH_OF_LINE;
592 // returns the maximum over all rows
593 int LyXTabular::GetWidthOfColumn(int cell) const
595 int const column1 = column_of_cell(cell);
596 int const column2 = right_column_of_cell(cell);
598 for (int i = column1; i <= column2; ++i) {
599 result += column_info[i].width_of_column;
605 int LyXTabular::GetWidthOfTabular() const
607 return width_of_tabular;
611 /* returns 1 if a complete update is necessary, otherwise 0 */
612 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
614 if (!IsMultiColumn(cell))
617 int const row = row_of_cell(cell);
618 int const column1 = column_of_cell(cell);
619 int const column2 = right_column_of_cell(cell);
621 // first set columns to 0 so we can calculate the right width
622 for (int i = column1; i <= column2; ++i) {
623 cell_info[row][i].width_of_cell = 0;
625 // set the width to MAX_WIDTH until width > 0
626 int width = (new_width + 2 * WIDTH_OF_LINE);
629 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
630 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
631 width -= column_info[i].width_of_column;
634 cell_info[row][i].width_of_cell = width;
640 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
642 int const row = row_of_cell(cell);
643 int const column1 = column_of_cell(cell);
644 int const column2 = right_column_of_cell(cell);
646 // first set columns to 0 so we can calculate the right width
648 for (; i <= column2; ++i)
649 cell_info[row][i].width_of_cell = 0;
650 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
652 if (i < numberofcells)
653 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
654 SetWidthOfMulticolCell(cell, new_width);
658 /* returns 1 if a complete update is necessary, otherwise 0 */
659 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
661 int const row = row_of_cell(cell);
662 int const column1 = column_of_cell(cell);
667 #ifdef SPECIAL_COLUM_HANDLING
668 if (RightLine(cell_info[row][column1].cellno, true) &&
669 (column1 < columns_-1) &&
670 LeftLine(cell_info[row][column1+1].cellno, true))
672 if (column_info[column1].right_line && (column1 < columns_-1) &&
673 column_info[column1+1].left_line) // additional width
677 add_width = WIDTH_OF_LINE;
679 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
682 if (IsMultiColumn(cell, true)) {
683 tmp = SetWidthOfMulticolCell(cell, new_width);
685 width = (new_width + 2*WIDTH_OF_LINE + add_width);
686 cell_info[row][column1].width_of_cell = width;
687 tmp = calculate_width_of_column_NMC(column1);
691 for (; i<columns_; ++i)
692 calculate_width_of_column_NMC(i);
693 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
695 if (i < numberofcells)
696 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
697 for (i = 0; i < columns_; ++i)
698 calculate_width_of_column(i);
699 calculate_width_of_tabular();
706 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
708 if (!IsMultiColumn(cell) || onlycolumn)
709 column_info[column_of_cell(cell)].alignment = align;
711 cellinfo_of_cell(cell)->alignment = align;
716 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
718 if (!IsMultiColumn(cell) || onlycolumn)
719 column_info[column_of_cell(cell)].valignment = align;
721 cellinfo_of_cell(cell)->valignment = align;
726 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
728 bool flag = !width.zero();
729 int const j = column_of_cell(cell);
731 column_info[j].p_width = width;
732 if (flag) // do this only if there is a width
733 SetAlignment(cell, LYX_ALIGN_LEFT);
734 for (int i = 0; i < rows_; ++i) {
735 int c = GetCellNumber(i, j);
736 flag = !GetPWidth(c).zero(); // because of multicolumns!
737 GetCellInset(c)->setAutoBreakRows(flag);
743 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
745 bool const flag = !width.zero();
747 cellinfo_of_cell(cell)->p_width = width;
748 if (IsMultiColumn(cell)) {
749 GetCellInset(cell)->setAutoBreakRows(flag);
756 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
757 LyXTabular::Feature what)
759 if (what == SET_SPECIAL_MULTI)
760 cellinfo_of_cell(cell)->align_special = special;
762 column_info[column_of_cell(cell)].align_special = special;
767 bool LyXTabular::SetAllLines(int cell, bool line)
769 SetTopLine(cell, line);
770 SetBottomLine(cell, line);
771 SetRightLine(cell, line);
772 SetLeftLine(cell, line);
777 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
779 int const row = row_of_cell(cell);
781 if (onlycolumn || !IsMultiColumn(cell))
782 row_info[row].top_line = line;
784 cellinfo_of_cell(cell)->top_line = line;
789 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
791 if (onlycolumn || !IsMultiColumn(cell))
792 row_info[row_of_cell(cell)].bottom_line = line;
794 cellinfo_of_cell(cell)->bottom_line = line;
799 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
801 if (onlycolumn || !IsMultiColumn(cell))
802 column_info[column_of_cell(cell)].left_line = line;
804 cellinfo_of_cell(cell)->left_line = line;
809 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
811 if (onlycolumn || !IsMultiColumn(cell))
812 column_info[right_column_of_cell(cell)].right_line = line;
814 cellinfo_of_cell(cell)->right_line = line;
819 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
821 if (!onlycolumn && IsMultiColumn(cell))
822 return cellinfo_of_cell(cell)->alignment;
824 return column_info[column_of_cell(cell)].alignment;
828 LyXTabular::VAlignment
829 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
831 if (!onlycolumn && IsMultiColumn(cell))
832 return cellinfo_of_cell(cell)->valignment;
834 return column_info[column_of_cell(cell)].valignment;
838 LyXLength const LyXTabular::GetPWidth(int cell) const
840 if (IsMultiColumn(cell))
841 return cellinfo_of_cell(cell)->p_width;
842 return column_info[column_of_cell(cell)].p_width;
846 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
848 return column_info[column_of_cell(cell)].p_width;
852 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
854 if (IsMultiColumn(cell))
855 return cellinfo_of_cell(cell)->p_width;
860 string const LyXTabular::GetAlignSpecial(int cell, int what) const
862 if (what == SET_SPECIAL_MULTI)
863 return cellinfo_of_cell(cell)->align_special;
864 return column_info[column_of_cell(cell)].align_special;
868 int LyXTabular::GetWidthOfCell(int cell) const
870 int const row = row_of_cell(cell);
871 int const column1 = column_of_cell(cell);
872 int const column2 = right_column_of_cell(cell);
874 for (int i = column1; i <= column2; ++i) {
875 result += cell_info[row][i].width_of_cell;
881 int LyXTabular::GetBeginningOfTextInCell(int cell) const
885 switch (GetAlignment(cell)){
886 case LYX_ALIGN_CENTER:
887 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
889 case LYX_ALIGN_RIGHT:
890 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
891 // + GetAdditionalWidth(cell);
893 default: /* LYX_ALIGN_LEFT: nothing :-) */
903 bool LyXTabular::IsFirstCellInRow(int cell) const
905 return column_of_cell(cell) == 0;
909 int LyXTabular::GetFirstCellInRow(int row) const
913 return cell_info[row][0].cellno;
916 bool LyXTabular::IsLastCellInRow(int cell) const
918 return (right_column_of_cell(cell) == (columns_ - 1));
922 int LyXTabular::GetLastCellInRow(int row) const
926 return cell_info[row][columns_-1].cellno;
930 bool LyXTabular::calculate_width_of_column(int column)
932 int const old_column_width = column_info[column].width_of_column;
935 for (int i = 0; i < rows_; ++i) {
936 maximum = max(cell_info[i][column].width_of_cell, maximum);
938 column_info[column].width_of_column = maximum;
939 return (column_info[column].width_of_column != old_column_width);
944 // calculate the with of the column without regarding REAL MultiColumn
945 // cells. This means MultiColumn-cells spanning more than 1 column.
947 bool LyXTabular::calculate_width_of_column_NMC(int column)
949 int const old_column_width = column_info[column].width_of_column;
951 for (int i = 0; i < rows_; ++i) {
952 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
953 (cell_info[i][column].width_of_cell > max)) {
954 max = cell_info[i][column].width_of_cell;
957 column_info[column].width_of_column = max;
958 return (column_info[column].width_of_column != old_column_width);
962 void LyXTabular::calculate_width_of_tabular()
964 width_of_tabular = 0;
965 for (int i = 0; i < columns_; ++i) {
966 width_of_tabular += column_info[i].width_of_column;
971 int LyXTabular::row_of_cell(int cell) const
973 if (cell >= numberofcells)
977 return rowofcell[cell];
981 int LyXTabular::column_of_cell(int cell) const
983 if (cell >= numberofcells)
987 return columnofcell[cell];
991 int LyXTabular::right_column_of_cell(int cell) const
993 int const row = row_of_cell(cell);
994 int column = column_of_cell(cell);
995 while (column < (columns_ - 1) &&
996 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1002 // Perfect case for a template... (Lgb)
1003 // or perhaps not...
1006 string const write_attribute(string const & name, T const & t)
1008 string str = " " + name + "=\"" + tostr(t) + "\"";
1013 string const write_attribute(string const & name, bool const & b)
1015 return write_attribute(name, int(b));
1019 string const write_attribute(string const & name, LyXLength const & value)
1021 return write_attribute(name, value.asString());
1025 string const write_attribute(string const & name, int value)
1027 string str = " " + name + "=\"" + tostr(value) + "\"";
1032 string const write_attribute(string const & name, string const & value)
1034 string str = " " + name + "=\"" + value + "\"";
1039 string const write_attribute(string const & name, bool value)
1041 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1046 string const write_attribute(string const & name, LyXLength const & value)
1048 string str = " " + name + "=\"" + value.asString() + "\"";
1056 string const tostr(LyXAlignment const & num)
1059 case LYX_ALIGN_NONE:
1061 case LYX_ALIGN_BLOCK:
1063 case LYX_ALIGN_LEFT:
1065 case LYX_ALIGN_CENTER:
1067 case LYX_ALIGN_RIGHT:
1069 case LYX_ALIGN_LAYOUT:
1071 case LYX_ALIGN_SPECIAL:
1080 string const tostr(LyXTabular::VAlignment const & num)
1083 case LyXTabular::LYX_VALIGN_TOP:
1085 case LyXTabular::LYX_VALIGN_CENTER:
1087 case LyXTabular::LYX_VALIGN_BOTTOM:
1096 string const tostr(LyXTabular::BoxType const & num)
1099 case LyXTabular::BOX_NONE:
1101 case LyXTabular::BOX_PARBOX:
1103 case LyXTabular::BOX_MINIPAGE:
1110 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1114 << write_attribute("version", 3)
1115 << write_attribute("rows", rows_)
1116 << write_attribute("columns", columns_)
1118 // global longtable options
1120 << write_attribute("rotate", tostr(rotate))
1121 << write_attribute("islongtable", tostr(is_long_tabular))
1122 << write_attribute("firstHeadTopDL", tostr(endfirsthead.topDL))
1123 << write_attribute("firstHeadBottomDL", tostr(endfirsthead.bottomDL))
1124 << write_attribute("firstHeadEmpty", tostr(endfirsthead.empty))
1125 << write_attribute("headTopDL", tostr(endhead.topDL))
1126 << write_attribute("headBottomDL", tostr(endhead.bottomDL))
1127 << write_attribute("footTopDL", tostr(endfoot.topDL))
1128 << write_attribute("footBottomDL", tostr(endfoot.bottomDL))
1129 << write_attribute("lastFootTopDL", tostr(endlastfoot.topDL))
1130 << write_attribute("lastFootBottomDL", tostr(endlastfoot.bottomDL))
1131 << write_attribute("lastFootEmpty", tostr(endlastfoot.empty))
1133 for (int j = 0; j < columns_; ++j) {
1135 << write_attribute("alignment", tostr(column_info[j].alignment))
1136 << write_attribute("valignment", tostr(column_info[j].valignment))
1137 << write_attribute("leftline", tostr(column_info[j].left_line))
1138 << write_attribute("rightline", tostr(column_info[j].right_line))
1139 << write_attribute("width", column_info[j].p_width.asString())
1140 << write_attribute("special", column_info[j].align_special)
1143 for (int i = 0; i < rows_; ++i) {
1145 << write_attribute("topline", tostr(row_info[i].top_line))
1146 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1147 << write_attribute("endhead", tostr(row_info[i].endhead))
1148 << write_attribute("endfirsthead", tostr(row_info[i].endfirsthead))
1149 << write_attribute("endfoot", tostr(row_info[i].endfoot))
1150 << write_attribute("endlastfoot", tostr(row_info[i].endlastfoot))
1151 << write_attribute("newpage", tostr(row_info[i].newpage))
1153 for (int j = 0; j < columns_; ++j) {
1155 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1156 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1157 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1158 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1159 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1160 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1161 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1162 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1163 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1164 << write_attribute("width", cell_info[i][j].p_width)
1165 << write_attribute("special", cell_info[i][j].align_special)
1167 os << "\\begin_inset ";
1168 cell_info[i][j].inset.write(buf, os);
1169 os << "\n\\end_inset \n"
1174 os << "</lyxtabular>\n";
1180 // I would have liked a fromstr template a lot better. (Lgb)
1183 bool string2type(string const str, LyXAlignment & num)
1186 num = LYX_ALIGN_NONE;
1187 else if (str == "block")
1188 num = LYX_ALIGN_BLOCK;
1189 else if (str == "left")
1190 num = LYX_ALIGN_LEFT;
1191 else if (str == "center")
1192 num = LYX_ALIGN_CENTER;
1193 else if (str == "right")
1194 num = LYX_ALIGN_RIGHT;
1202 bool string2type(string const str, LyXTabular::VAlignment & num)
1205 num = LyXTabular::LYX_VALIGN_TOP;
1206 else if (str == "center")
1207 num = LyXTabular::LYX_VALIGN_CENTER;
1208 else if (str == "bottom")
1209 num = LyXTabular::LYX_VALIGN_BOTTOM;
1217 bool string2type(string const str, LyXTabular::BoxType & num)
1220 num = LyXTabular::BOX_NONE;
1221 else if (str == "parbox")
1222 num = LyXTabular::BOX_PARBOX;
1223 else if (str == "minipage")
1224 num = LyXTabular::BOX_MINIPAGE;
1232 bool string2type(string const str, bool & num)
1236 else if (str == "false")
1244 bool getTokenValue(string const & str, const char * token, string & ret)
1246 size_t token_length = strlen(token);
1247 string::size_type pos = str.find(token);
1249 if (pos == string::npos || pos + token_length + 1 >= str.length()
1250 || str[pos + token_length] != '=')
1253 pos += token_length + 1;
1255 if ((ch != '"') && (ch != '\'')) { // only read till next space
1259 while ((pos < str.length() - 1) && (str[++pos] != ch))
1266 bool getTokenValue(string const & str, const char * token, int & num)
1269 if (!getTokenValue(str, token, tmp))
1271 num = strToInt(tmp);
1276 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1279 if (!getTokenValue(str, token, tmp))
1281 return string2type(tmp, num);
1285 bool getTokenValue(string const & str, const char * token,
1286 LyXTabular::VAlignment & num)
1289 if (!getTokenValue(str, token, tmp))
1291 return string2type(tmp, num);
1295 bool getTokenValue(string const & str, const char * token,
1296 LyXTabular::BoxType & num)
1299 if (!getTokenValue(str, token, tmp))
1301 return string2type(tmp, num);
1305 bool getTokenValue(string const & str, const char * token, bool & flag)
1308 if (!getTokenValue(str, token, tmp))
1310 return string2type(tmp, flag);
1314 bool getTokenValue(string const & str, const char * token, LyXLength & len)
1317 if (!getTokenValue(str, token, tmp))
1319 return isValidLength(tmp, &len);
1324 void l_getline(istream & is, string & str)
1327 while (str.empty()) {
1329 if (!str.empty() && str[str.length() - 1] == '\r')
1330 str.erase(str.length() - 1);
1337 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1340 istream & is = lex.getStream();
1342 l_getline(is, line);
1343 if (!prefixIs(line, "<lyxtabular ")
1344 && !prefixIs(line, "<LyXTabular ")) {
1345 OldFormatRead(lex, line);
1350 if (!getTokenValue(line, "version", version))
1353 ReadOld(buf, is, lex, line);
1354 else if (version >= 2)
1355 ReadNew(buf, is, lex, line, version);
1359 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1360 LyXLex & lex, string const & l, int const version)
1364 if (!getTokenValue(line, "rows", rows_arg))
1367 if (!getTokenValue(line, "columns", columns_arg))
1369 Init(rows_arg, columns_arg);
1370 l_getline(is, line);
1371 if (!prefixIs(line, "<features")) {
1372 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1373 line << ")" << endl;
1376 getTokenValue(line, "rotate", rotate);
1377 getTokenValue(line, "islongtable", is_long_tabular);
1378 // compatibility read for old longtable options
1385 getTokenValue(line, "endhead", hrow);
1386 getTokenValue(line, "endfirsthead", fhrow);
1387 getTokenValue(line, "endfoot", frow);
1388 getTokenValue(line, "endlastfoot", lfrow);
1390 row_info[abs(hrow)-1].endhead = true;
1392 row_info[abs(fhrow)-1].endfirsthead = true;
1394 row_info[abs(frow)-1].endfoot = true;
1396 row_info[abs(lfrow)-1].endlastfoot = true;
1398 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1399 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1400 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1401 getTokenValue(line, "headTopDL", endhead.topDL);
1402 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1403 getTokenValue(line, "footTopDL", endfoot.topDL);
1404 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1405 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1406 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1407 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1409 for (int j = 0; j < columns_; ++j) {
1411 if (!prefixIs(line,"<column")) {
1412 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1413 line << ")" << endl;
1416 getTokenValue(line, "alignment", column_info[j].alignment);
1417 getTokenValue(line, "valignment", column_info[j].valignment);
1418 getTokenValue(line, "leftline", column_info[j].left_line);
1419 getTokenValue(line, "rightline", column_info[j].right_line);
1420 getTokenValue(line, "width", column_info[j].p_width);
1421 getTokenValue(line, "special", column_info[j].align_special);
1424 for (int i = 0; i < rows_; ++i) {
1425 l_getline(is, line);
1426 if (!prefixIs(line, "<row")) {
1427 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1428 line << ")" << endl;
1431 getTokenValue(line, "topline", row_info[i].top_line);
1432 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1433 getTokenValue(line, "newpage", row_info[i].newpage);
1434 for (int j = 0; j < columns_; ++j) {
1435 l_getline(is, line);
1436 if (!prefixIs(line, "<cell")) {
1437 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1438 line << ")" << endl;
1441 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1442 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1443 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1444 getTokenValue(line, "topline", cell_info[i][j].top_line);
1445 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1446 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1447 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1448 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1449 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1450 getTokenValue(line, "width", cell_info[i][j].p_width);
1451 getTokenValue(line, "special", cell_info[i][j].align_special);
1452 l_getline(is, line);
1453 if (prefixIs(line, "\\begin_inset")) {
1454 cell_info[i][j].inset.read(buf, lex);
1455 l_getline(is, line);
1457 if (!prefixIs(line, "</cell>")) {
1458 lyxerr << "Wrong tabular format (expected </cell> got" <<
1459 line << ")" << endl;
1463 l_getline(is, line);
1464 if (!prefixIs(line, "</row>")) {
1465 lyxerr << "Wrong tabular format (expected </row> got" <<
1466 line << ")" << endl;
1470 while (!prefixIs(line, "</lyxtabular>")) {
1471 l_getline(is, line);
1473 set_row_column_number_info();
1477 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1483 int columns_arg = 0;
1484 int is_long_tabular_arg = false;
1485 int rotate_arg = false;
1494 istream & is = lex.getStream();
1497 version = lyx::atoi(s.substr(8, string::npos));
1501 vector<int> cont_row_info;
1504 lyxerr << "Tabular format < 5 is not supported anymore\n"
1505 "Get an older version of LyX (< 1.1.x) for conversion!"
1507 Alert::alert(_("Warning:"),
1508 _("Tabular format < 5 is not supported anymore\n"),
1509 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1511 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1512 >> rotate_arg >> a >> b >> c >> d;
1514 is >> rows_arg >> columns_arg;
1515 Init(rows_arg, columns_arg);
1516 cont_row_info = vector<int>(rows_arg);
1517 SetLongTabular(is_long_tabular_arg);
1518 SetRotateTabular(rotate_arg);
1520 for (i = 0; i < rows_; ++i) {
1522 cont_row_info[i] = false;
1524 for (i = 0; i < columns_; ++i) {
1527 for (i = 0; i < rows_; ++i) {
1528 for (j = 0; j < columns_; ++j) {
1533 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1534 >> rotate_arg >> a >> b >> c >> d;
1535 Init(rows_arg, columns_arg);
1536 cont_row_info = vector<int>(rows_arg);
1537 SetLongTabular(is_long_tabular_arg);
1538 SetRotateTabular(rotate_arg);
1539 row_info[a].endhead = true;
1540 row_info[b].endfirsthead = true;
1541 row_info[c].endfoot = true;
1542 row_info[d].endlastfoot = true;
1543 for (i = 0; i < rows_; ++i) {
1544 a = b = c = d = e = f = g = 0;
1545 is >> a >> b >> c >> d;
1546 row_info[i].top_line = a;
1547 row_info[i].bottom_line = b;
1548 cont_row_info[i] = c;
1549 row_info[i].newpage = d;
1551 for (i = 0; i < columns_; ++i) {
1556 char ch; // skip '"'
1559 // ignore is buggy but we will use it later (Lgb)
1560 is.ignore(); // skip '"'
1562 getline(is, s1, '"');
1564 is >> ch; // skip '"'
1566 // ignore is buggy but we will use it later (Lgb)
1567 is.ignore(); // skip '"'
1569 getline(is, s2, '"');
1570 column_info[i].alignment = static_cast<LyXAlignment>(a);
1571 column_info[i].left_line = b;
1572 column_info[i].right_line = c;
1573 column_info[i].p_width = LyXLength(s1);
1574 column_info[i].align_special = s2;
1576 for (i = 0; i < rows_; ++i) {
1577 for (j = 0; j < columns_; ++j) {
1580 is >> a >> b >> c >> d >> e >> f >> g;
1583 is >> ch; // skip '"'
1585 // ignore is buggy but we will use it later (Lgb)
1586 is.ignore(); // skip '"'
1588 getline(is, s1, '"');
1590 is >> ch; // skip '"'
1592 // ignore is buggy but we will use it later (Lgb)
1593 is.ignore(); // skip '"'
1595 getline(is, s2, '"');
1596 cell_info[i][j].multicolumn = static_cast<char>(a);
1597 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1598 cell_info[i][j].top_line = static_cast<char>(c);
1599 cell_info[i][j].bottom_line = static_cast<char>(d);
1600 cell_info[i][j].left_line = column_info[j].left_line;
1601 cell_info[i][j].right_line = column_info[j].right_line;
1602 cell_info[i][j].rotate = static_cast<bool>(f);
1603 cell_info[i][j].usebox = static_cast<BoxType>(g);
1604 cell_info[i][j].align_special = s1;
1605 cell_info[i][j].p_width = LyXLength(s2);
1609 set_row_column_number_info(true);
1611 Paragraph * par = new Paragraph;
1612 Paragraph * return_par = 0;
1616 Paragraph::depth_type depth = 0;
1617 LyXFont font(LyXFont::ALL_INHERIT);
1618 font.setLanguage(owner_->bufferOwner()->getLanguage());
1620 while (lex.isOK()) {
1622 string const token = lex.getString();
1625 if (token == "\\layout"
1626 || token == "\\end_float"
1627 || token == "\\end_deeper") {
1628 lex.pushToken(token);
1629 #ifndef NO_COMPABILITY
1630 // Here we need to insert the inset_ert_contents into the last
1631 // cell of the tabular.
1632 owner_->bufferOwner()->insertErtContents(par, pos, font);
1636 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1641 lex.pushToken(token);
1645 lex.printError("New Paragraph allocated! This should not happen!");
1646 lex.pushToken(token);
1652 // now we have the par we should fill the insets with this!
1654 InsetText * inset = GetCellInset(cell);
1657 for (int i = 0; i < par->size(); ++i) {
1658 if (par->isNewline(i)) {
1660 if (cell > numberofcells) {
1661 lyxerr << "Some error in reading old table format occured!" <<
1662 endl << "Terminating when reading cell[" << cell << "]!" <<
1667 row = row_of_cell(cell);
1668 if (cont_row_info[row]) {
1670 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1671 while (!IsFirstCellInRow(--cell));
1673 inset = GetCellInset(cell);
1676 inset = GetCellInset(cell);
1677 row = row_of_cell(cell);
1678 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1680 // insert a space instead
1682 par->insertChar(i, ' ');
1685 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1686 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1693 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1695 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1696 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1700 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1702 int const row = row_of_cell(cell);
1703 int const column = column_of_cell(cell);
1704 return &cell_info[row][column];
1708 void LyXTabular::SetMultiColumn(int cell, int number)
1710 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1711 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1712 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1713 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1714 for (number--; number > 0; --number) {
1715 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1717 set_row_column_number_info();
1721 int LyXTabular::cells_in_multicolumn(int cell) const
1723 int const row = row_of_cell(cell);
1724 int column = column_of_cell(cell);
1727 while ((column < columns_) &&
1728 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1737 int LyXTabular::UnsetMultiColumn(int cell)
1739 int const row = row_of_cell(cell);
1740 int column = column_of_cell(cell);
1744 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1745 cell_info[row][column].multicolumn = CELL_NORMAL;
1747 while ((column < columns_) &&
1748 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1750 cell_info[row][column].multicolumn = CELL_NORMAL;
1755 set_row_column_number_info();
1760 void LyXTabular::SetLongTabular(bool what)
1762 is_long_tabular = what;
1766 bool LyXTabular::IsLongTabular() const
1768 return is_long_tabular;
1772 void LyXTabular::SetRotateTabular(bool flag)
1778 bool LyXTabular::GetRotateTabular() const
1784 void LyXTabular::SetRotateCell(int cell, bool flag)
1786 cellinfo_of_cell(cell)->rotate = flag;
1790 bool LyXTabular::GetRotateCell(int cell) const
1792 return cellinfo_of_cell(cell)->rotate;
1796 bool LyXTabular::NeedRotating() const
1800 for (int i = 0; i < rows_; ++i) {
1801 for (int j = 0; j < columns_; ++j) {
1802 if (cell_info[i][j].rotate)
1810 bool LyXTabular::IsLastCell(int cell) const
1812 if ((cell + 1) < numberofcells)
1818 int LyXTabular::GetCellAbove(int cell) const
1820 if (row_of_cell(cell) > 0)
1821 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1826 int LyXTabular::GetCellBelow(int cell) const
1828 if (row_of_cell(cell) + 1 < rows_)
1829 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1834 int LyXTabular::GetLastCellAbove(int cell) const
1836 if (row_of_cell(cell) <= 0)
1838 if (!IsMultiColumn(cell))
1839 return GetCellAbove(cell);
1840 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1844 int LyXTabular::GetLastCellBelow(int cell) const
1846 if (row_of_cell(cell) + 1 >= rows_)
1848 if (!IsMultiColumn(cell))
1849 return GetCellBelow(cell);
1850 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1854 int LyXTabular::GetCellNumber(int row, int column) const
1857 if (column >= columns_)
1858 column = columns_ - 1;
1859 else if (column < 0)
1866 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1868 return cell_info[row][column].cellno;
1872 void LyXTabular::SetUsebox(int cell, BoxType type)
1874 cellinfo_of_cell(cell)->usebox = type;
1878 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1880 if (column_info[column_of_cell(cell)].p_width.zero() &&
1881 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1883 if (cellinfo_of_cell(cell)->usebox > 1)
1884 return cellinfo_of_cell(cell)->usebox;
1885 return UseParbox(cell);
1889 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1893 row_info[row].endfirsthead = flag;
1896 row_info[row].endhead = flag;
1901 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1904 return row_info[row].endhead;
1908 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1911 return row_info[row].endfirsthead;
1915 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1919 row_info[row].endlastfoot = flag;
1922 row_info[row].endfoot = flag;
1927 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1930 return row_info[row].endfoot;
1934 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1937 return row_info[row].endlastfoot;
1941 void LyXTabular::SetLTNewPage(int row, bool what)
1943 row_info[row].newpage = what;
1947 bool LyXTabular::GetLTNewPage(int row) const
1949 return row_info[row].newpage;
1953 bool LyXTabular::SetAscentOfRow(int row, int height)
1955 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1957 row_info[row].ascent_of_row = height;
1962 bool LyXTabular::SetDescentOfRow(int row, int height)
1964 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1966 row_info[row].descent_of_row = height;
1971 int LyXTabular::GetAscentOfRow(int row) const
1975 return row_info[row].ascent_of_row;
1979 int LyXTabular::GetDescentOfRow(int row) const
1983 return row_info[row].descent_of_row;
1987 int LyXTabular::GetHeightOfTabular() const
1991 for (int row = 0; row < rows_; ++row)
1992 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1993 GetAdditionalHeight(row);
1998 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
2000 if ((row >= rows_) || (column >= columns_))
2002 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
2006 int LyXTabular::TeXTopHLine(ostream & os, int row) const
2008 if ((row < 0) || (row >= rows_))
2011 int const fcell = GetFirstCellInRow(row);
2012 int const n = NumberOfCellsInRow(fcell) + fcell;
2015 for (int i = fcell; i < n; ++i) {
2019 if (tmp == (n - fcell)){
2022 for (int i = fcell; i < n; ++i) {
2025 << column_of_cell(i) + 1
2027 << right_column_of_cell(i) + 1
2039 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
2041 if ((row < 0) || (row >= rows_))
2044 int const fcell = GetFirstCellInRow(row);
2045 int const n = NumberOfCellsInRow(fcell) + fcell;
2048 for (int i = fcell; i < n; ++i) {
2052 if (tmp == (n-fcell)){
2055 for (int i = fcell; i < n; ++i) {
2056 if (BottomLine(i)) {
2058 << column_of_cell(i) + 1
2060 << right_column_of_cell(i) + 1
2072 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2076 if (GetRotateCell(cell)) {
2077 os << "\\begin{sideways}\n";
2080 if (IsMultiColumn(cell)) {
2081 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2082 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2083 os << cellinfo_of_cell(cell)->align_special << "}{";
2085 if (LeftLine(cell) &&
2086 (IsFirstCellInRow(cell) ||
2087 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2088 !RightLine(cell-1, true))))
2092 if (!GetPWidth(cell).zero()) {
2093 switch (GetVAlignment(cell)) {
2094 case LYX_VALIGN_TOP:
2097 case LYX_VALIGN_CENTER:
2100 case LYX_VALIGN_BOTTOM:
2104 os << "{" << GetPWidth(cell).asLatexString() << '}';
2106 switch (GetAlignment(cell)) {
2107 case LYX_ALIGN_LEFT:
2110 case LYX_ALIGN_RIGHT:
2118 if (RightLine(cell))
2120 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2126 if (GetUsebox(cell) == BOX_PARBOX) {
2128 switch (GetVAlignment(cell)) {
2129 case LYX_VALIGN_TOP:
2132 case LYX_VALIGN_CENTER:
2135 case LYX_VALIGN_BOTTOM:
2139 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2140 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2141 os << "\\begin{minipage}[";
2142 switch (GetVAlignment(cell)) {
2143 case LYX_VALIGN_TOP:
2146 case LYX_VALIGN_CENTER:
2149 case LYX_VALIGN_BOTTOM:
2153 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2160 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2165 if (GetUsebox(cell) == BOX_PARBOX)
2167 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2168 os << "%\n\\end{minipage}";
2171 if (IsMultiColumn(cell)){
2174 if (GetRotateCell(cell)) {
2175 os << "%\n\\end{sideways}";
2182 int LyXTabular::Latex(Buffer const * buf,
2183 ostream & os, bool fragile, bool fp) const
2188 //+---------------------------------------------------------------------
2189 //+ first the opening preamble +
2190 //+---------------------------------------------------------------------
2193 os << "\\begin{sideways}\n";
2196 if (is_long_tabular)
2197 os << "\\begin{longtable}{";
2199 os << "\\begin{tabular}{";
2200 for (int i = 0; i < columns_; ++i) {
2201 if (!column_info[i].align_special.empty()) {
2202 os << column_info[i].align_special;
2204 if (column_info[i].left_line)
2206 if (!column_info[i].p_width.zero()) {
2207 switch (column_info[i].valignment) {
2208 case LYX_VALIGN_TOP:
2211 case LYX_VALIGN_CENTER:
2214 case LYX_VALIGN_BOTTOM:
2219 << column_info[i].p_width.asLatexString()
2222 switch (column_info[i].alignment) {
2223 case LYX_ALIGN_LEFT:
2226 case LYX_ALIGN_RIGHT:
2234 if (column_info[i].right_line)
2241 //+---------------------------------------------------------------------
2242 //+ the single row and columns (cells) +
2243 //+---------------------------------------------------------------------
2245 for (int i = 0; i < rows_; ++i) {
2246 ret += TeXTopHLine(os, i);
2247 #warning Implement top double lines for LT Header/Footers
2250 ret += TeXBottomHLine(os, i-1);
2251 ret += TeXTopHLine(os, i);
2254 for (int j = 0; j < columns_; ++j) {
2255 if (IsPartOfMultiColumn(i,j))
2257 ret += TeXCellPreamble(os, cell);
2258 InsetText * inset = GetCellInset(cell);
2260 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2261 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2265 ret += inset->latex(buf, os, fragile, fp);
2269 ret += TeXCellPostamble(os, cell);
2270 if (!IsLastCellInRow(cell)) { // not last cell in row
2278 ret += TeXBottomHLine(os, i);
2279 if (IsLongTabular()) {
2280 if (row_info[i].endhead) {
2281 if (endhead.bottomDL)
2282 ret += TeXBottomHLine(os, i);
2283 os << "\\endhead\n";
2286 if (row_info[i].endfirsthead) {
2287 if (endfirsthead.bottomDL)
2288 ret += TeXBottomHLine(os, i);
2289 os << "\\endfirsthead\n";
2292 if (row_info[i].endfoot) {
2293 if (endfoot.bottomDL)
2294 ret += TeXBottomHLine(os, i);
2295 os << "\\endfoot\n";
2298 if (row_info[i].endlastfoot) {
2299 if (endlastfoot.bottomDL)
2300 ret += TeXBottomHLine(os, i);
2301 os << "\\endlastfoot\n";
2304 if (row_info[i].newpage) {
2305 os << "\\newpage\n";
2311 //+---------------------------------------------------------------------
2312 //+ the closing of the tabular +
2313 //+---------------------------------------------------------------------
2315 if (is_long_tabular)
2316 os << "\\end{longtable}";
2318 os << "\\end{tabular}";
2320 os << "\n\\end{sideways}";
2328 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2331 int cell = GetFirstCellInRow(row);
2334 for (int j = 0; j < columns_; ++j) {
2335 if (IsPartOfMultiColumn(row, j))
2338 os << "<entry align=\"";
2339 switch (GetAlignment(cell)) {
2340 case LYX_ALIGN_LEFT:
2343 case LYX_ALIGN_RIGHT:
2351 os << "\" valign=\"";
2352 switch (GetVAlignment(cell)) {
2353 case LYX_VALIGN_TOP:
2356 case LYX_VALIGN_BOTTOM:
2359 case LYX_VALIGN_CENTER:
2364 if (IsMultiColumn(cell)) {
2365 os << " namest=\"col" << j << "\" ";
2366 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2370 ret += GetCellInset(cell)->docbook(buf, os);
2379 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2383 //+---------------------------------------------------------------------
2384 //+ first the opening preamble +
2385 //+---------------------------------------------------------------------
2387 os << "<tgroup cols=\"" << columns_
2388 << "\" colsep=\"1\" rowsep=\"1\">\n";
2390 for (int i = 0; i < columns_; ++i) {
2391 os << "<colspec colname=\"col" << i << "\" align=\"";
2392 switch (column_info[i].alignment) {
2393 case LYX_ALIGN_LEFT:
2396 case LYX_ALIGN_RIGHT:
2407 //+---------------------------------------------------------------------
2408 //+ Long Tabular case +
2409 //+---------------------------------------------------------------------
2411 #warning Jose please have a look here I changed the longtable header/footer
2412 #warning ---- options so I had to disable the docbook code (Jug 20011219)
2414 if ( IsLongTabular() ) {
2416 if(endhead.row || endfirsthead.row ) {
2418 if( endfirsthead.row ) {
2419 docbookRow( buf, os, endfirsthead.row - 1);
2421 if( endhead.row && endhead.row != endfirsthead.row) {
2422 docbookRow(buf, os, endhead.row - 1);
2428 if( endfoot.row || endlastfoot.row ) {
2431 docbookRow( buf, os, endfoot.row - 1);
2433 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2434 docbookRow( buf, os, endlastfoot.row - 1);
2440 //+---------------------------------------------------------------------
2441 //+ the single row and columns (cells) +
2442 //+---------------------------------------------------------------------
2445 for (int i = 0; i < rows_; ++i) {
2446 if(!IsLongTabular() || (
2447 !row_info[i].endhead && !row_info[i].endfirsthead &&
2448 !row_info[i].endfoot && !row_info[i].endlastfoot)) {
2449 docbookRow( buf, os, i);
2453 //+---------------------------------------------------------------------
2454 //+ the closing of the tabular +
2455 //+---------------------------------------------------------------------
2467 void print_n_chars(ostream & os, unsigned char ch, int n)
2469 os << string(n, ch);
2475 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2476 vector<unsigned int> const & clen) const
2478 int const fcell = GetFirstCellInRow(row);
2479 int const n = NumberOfCellsInRow(fcell) + fcell;
2482 for (int i = fcell; i < n; ++i) {
2492 for (int i = fcell; i < n; ++i) {
2503 int column = column_of_cell(i);
2504 int len = clen[column];
2505 while (IsPartOfMultiColumn(row, ++column))
2506 len += clen[column] + 4;
2507 print_n_chars(os, ch, len);
2522 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2523 vector<unsigned int> const & clen) const
2525 int const fcell = GetFirstCellInRow(row);
2526 int const n = NumberOfCellsInRow(fcell) + fcell;
2529 for (int i = fcell; i < n; ++i) {
2530 if (BottomLine(i)) {
2539 for (int i = fcell; i < n; ++i) {
2540 if (BottomLine(i)) {
2550 int column = column_of_cell(i);
2551 int len = clen[column];
2552 while (IsPartOfMultiColumn(row, ++column))
2553 len += clen[column] + 4;
2554 print_n_chars(os, ch, len);
2555 if (BottomLine(i)) {
2569 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2570 int cell, int row, int column,
2571 vector<unsigned int> const & clen) const
2574 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2581 unsigned int len1 = sstr.str().length();
2582 unsigned int len2 = clen[column];
2583 while (IsPartOfMultiColumn(row, ++column))
2584 len2 += clen[column] + 4;
2587 switch (GetAlignment(cell)) {
2589 case LYX_ALIGN_LEFT:
2592 case LYX_ALIGN_RIGHT:
2596 case LYX_ALIGN_CENTER:
2602 for (unsigned int i = 0; i < len1; ++i)
2605 for (unsigned int i = 0; i < len2; ++i)
2607 if (RightLine(cell))
2616 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2620 //+---------------------------------------------------------------------
2621 //+ first calculate the width of the single columns +
2622 //+---------------------------------------------------------------------
2623 vector<unsigned int> clen(columns_);
2625 // first all non (real) multicolumn cells!
2626 for (int j = 0; j < columns_; ++j) {
2628 for (int i = 0; i < rows_; ++i) {
2629 int cell = GetCellNumber(i, j);
2630 if (IsMultiColumn(cell, true))
2633 GetCellInset(cell)->ascii(buf, sstr, 0);
2634 if (clen[j] < sstr.str().length())
2635 clen[j] = sstr.str().length();
2638 // then all (real) multicolumn cells!
2639 for (int j = 0; j < columns_; ++j) {
2640 for (int i = 0; i < rows_; ++i) {
2641 int cell = GetCellNumber(i, j);
2642 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2645 GetCellInset(cell)->ascii(buf, sstr, 0);
2646 int len = int(sstr.str().length());
2647 int const n = cells_in_multicolumn(cell);
2648 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2650 if (len > int(clen[j + n - 1]))
2651 clen[j + n - 1] = len;
2655 for (int i = 0; i < rows_; ++i) {
2656 AsciiTopHLine(os, i, clen);
2657 for (int j = 0; j < columns_; ++j) {
2658 if (IsPartOfMultiColumn(i,j))
2660 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2664 AsciiBottomHLine(os, i, clen);
2670 InsetText * LyXTabular::GetCellInset(int cell) const
2673 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2677 InsetText * LyXTabular::GetCellInset(int row, int column) const
2679 cur_cell = GetCellNumber(row, column);
2680 return & cell_info[row][column].inset;
2684 void LyXTabular::Validate(LaTeXFeatures & features) const
2686 if (IsLongTabular())
2687 features.require("longtable");
2689 features.require("rotating");
2690 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2691 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2692 features.require("array");
2693 GetCellInset(cell)->validate(features);
2698 std::vector<string> const LyXTabular::getLabelList() const
2700 std::vector<string> label_list;
2701 for (int i = 0; i < rows_; ++i)
2702 for (int j = 0; j < columns_; ++j) {
2703 std::vector<string> const l =
2704 GetCellInset(i, j)->getLabelList();
2705 label_list.insert(label_list.end(),
2706 l.begin(), l.end());
2712 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2714 Paragraph * par = GetCellInset(cell)->paragraph();
2716 for (; par; par = par->next()) {
2717 for (int i = 0; i < par->size(); ++i) {
2718 if (par->getChar(i) == Paragraph::META_NEWLINE)