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()
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,
118 Init(lt.rows_, lt.columns_, <);
119 // we really should change again to have InsetText as a pointer
120 // and allocate it then we would not have to do this stuff all
123 for (int i = 0; i < rows_; ++i) {
124 for (int j = 0; j < columns_; ++j) {
125 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
126 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
132 #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)
139 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
147 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
150 #warning This while method should look like this: (Lgb)
155 // If this and lt is not of the same size we have a serious bug
156 // So then it is ok to throw an exception, or for now
158 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
160 cell_info = lt.cell_info;
161 row_info = lt.row_info;
162 column_info = lt.column_info;
164 // long tabular stuff
165 SetLongTabular(lt.is_long_tabular);
166 endhead = lt.endhead;
167 endfoot = lt.endfoot;
168 endfirsthead = lt.endfirsthead;
169 endlastfoot = lt.endlastfoot;
179 LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
181 LyXTabular * result = new LyXTabular(inset, *this, same_id);
183 // don't know if this is good but I need to Clone also
184 // the text-insets here, this is for the Undo-facility!
185 for (int i = 0; i < rows_; ++i) {
186 for (int j = 0; j < columns_; ++j) {
187 result->cell_info[i][j].inset = cell_info[i][j].inset;
188 result->cell_info[i][j].inset.setOwner(inset);
196 /* activates all lines and sets all widths to 0 */
197 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
200 columns_ = columns_arg;
201 row_info = row_vector(rows_, rowstruct());
202 column_info = column_vector(columns_, columnstruct());
203 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
211 for (int i = 0; i < rows_; ++i) {
212 for (int j = 0; j < columns_; ++j) {
213 cell_info[i][j].inset.setOwner(owner_);
214 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
215 cell_info[i][j].cellno = cellno++;
217 cell_info[i].back().right_line = true;
219 row_info.back().bottom_line = true;
220 row_info.front().bottom_line = true;
222 for (int i = 0; i < columns_; ++i) {
223 calculate_width_of_column(i);
225 column_info.back().right_line = true;
227 calculate_width_of_tabular();
229 rowofcell = vector<int>();
230 columnofcell = vector<int>();
231 set_row_column_number_info();
232 is_long_tabular = false;
235 endfirsthead.row = 0;
241 void LyXTabular::AppendRow(int cell)
245 int row = row_of_cell(cell);
247 row_vector::iterator rit = row_info.begin() + row;
248 row_info.insert(rit, rowstruct());
249 // now set the values of the row before
250 row_info[row] = row_info[row+1];
253 cell_vvector::iterator cit = cell_info.begin() + row;
254 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
256 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
259 for (int i = 0; i <= row; ++i) {
260 for (int j = 0; j < columns_; ++j) {
261 c_info[i][j] = cell_info[i][j];
264 for (int i = row + 1; i < rows_; ++i) {
265 for (int j = 0; j < columns_; ++j) {
266 c_info[i][j] = cell_info[i-1][j];
271 for (int j = 0; j < columns_; ++j) {
272 cell_info[row][j].inset.clear();
279 void LyXTabular::DeleteRow(int row)
281 if (rows_ == 1) return; // Not allowed to delete last row
283 row_info.erase(row_info.begin() + row); //&row_info[row]);
284 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
290 void LyXTabular::AppendColumn(int cell)
294 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
296 int const column = column_of_cell(cell);
297 column_vector::iterator cit = column_info.begin() + column + 1;
298 column_info.insert(cit, columnstruct());
299 // set the column values of the column before
300 column_info[column+1] = column_info[column];
302 for (int i = 0; i < rows_; ++i) {
303 for (int j = 0; j <= column; ++j) {
304 c_info[i][j] = cell_info[i][j];
306 for (int j = column + 1; j < columns_; ++j) {
307 c_info[i][j] = cell_info[i][j - 1];
309 // care about multicolumns
310 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
311 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
313 if ((column + 1) == columns_ ||
314 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
315 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
320 for (int i = 0; i < rows_; ++i) {
321 //cell_info[i][column].inset.clear();
322 cell_info[i][column + 1].inset.clear();
328 void LyXTabular::DeleteColumn(int column)
330 // Similar to DeleteRow
331 //if (!(columns_ - 1))
333 if (columns_ == 1) return; // Not allowed to delete last column
335 column_info.erase(column_info.begin() + column);
336 for (int i = 0; i < rows_; ++i) {
337 cell_info[i].erase(cell_info[i].begin() + column);
344 void LyXTabular::Reinit()
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_);
353 for (int i = 0; i < columns_; ++i) {
354 calculate_width_of_column(i);
356 calculate_width_of_tabular();
358 set_row_column_number_info();
362 void LyXTabular::set_row_column_number_info(bool oldformat)
365 for (int row = 0; row < rows_; ++row) {
366 for (int column = 0; column<columns_; ++column) {
367 if (cell_info[row][column].multicolumn
368 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
370 cell_info[row][column].cellno = numberofcells;
373 ++numberofcells; // because this is one more than as we start from 0
375 rowofcell.resize(numberofcells);
376 columnofcell.resize(numberofcells);
378 for (int row = 0, column = 0, c = 0;
379 c < numberofcells && row < rows_ && column < columns_;) {
381 columnofcell[c] = column;
385 } while (column < columns_ &&
386 cell_info[row][column].multicolumn
387 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
388 if (column == columns_) {
394 for (int row = 0; row < rows_; ++row) {
395 for (int column = 0; column < columns_; ++column) {
396 if (IsPartOfMultiColumn(row,column))
398 // now set the right line of multicolumns right for oldformat read
400 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
402 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
403 cell_info[row][column].right_line =
404 cell_info[row][column+cn-1].right_line;
406 cell_info[row][column].inset.setAutoBreakRows(
407 !GetPWidth(GetCellNumber(row, column)).empty());
413 int LyXTabular::GetNumberOfCells() const
415 return numberofcells;
419 int LyXTabular::NumberOfCellsInRow(int cell) const
421 int const row = row_of_cell(cell);
423 for (int i = 0; i < columns_; ++i) {
424 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
431 /* returns 1 if there is a topline, returns 0 if not */
432 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
434 int const row = row_of_cell(cell);
436 if (!onlycolumn && IsMultiColumn(cell))
437 return cellinfo_of_cell(cell)->top_line;
438 return row_info[row].top_line;
442 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
444 // no bottom line underneath non-existent cells if you please
445 // Isn't that a programming error? Is so this should
446 // be an Assert instead. (Lgb)
447 if (cell >= numberofcells)
450 if (!onlycolumn && IsMultiColumn(cell))
451 return cellinfo_of_cell(cell)->bottom_line;
452 return row_info[row_of_cell(cell)].bottom_line;
456 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
458 if (!onlycolumn && IsMultiColumn(cell)) {
459 #ifdef SPECIAL_COLUM_HANDLING
460 if (cellinfo_of_cell(cell)->align_special.empty())
461 return cellinfo_of_cell(cell)->left_line;
462 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
464 return cellinfo_of_cell(cell)->left_line;
467 #ifdef SPECIAL_COLUM_HANDLING
468 if (column_info[column_of_cell(cell)].align_special.empty())
469 return column_info[column_of_cell(cell)].left_line;
470 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
472 return column_info[column_of_cell(cell)].left_line;
477 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
479 if (!onlycolumn && IsMultiColumn(cell)) {
480 #ifdef SPECIAL_COLUM_HANDLING
481 if (cellinfo_of_cell(cell)->align_special.empty())
482 return cellinfo_of_cell(cell)->right_line;
483 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
485 return cellinfo_of_cell(cell)->right_line;
488 #ifdef SPECIAL_COLUM_HANDLING
489 if (column_info[column_of_cell(cell)].align_special.empty())
490 return column_info[right_column_of_cell(cell)].right_line;
491 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
493 return column_info[right_column_of_cell(cell)].right_line;
498 bool LyXTabular::TopAlreadyDrawed(int cell) const
500 int row = row_of_cell(cell);
501 if ((row > 0) && !GetAdditionalHeight(row)) {
502 int column = column_of_cell(cell);
505 && cell_info[row][column].multicolumn
506 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
508 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
509 return row_info[row].bottom_line;
511 return cell_info[row][column].bottom_line;
517 bool LyXTabular::LeftAlreadyDrawed(int cell) const
519 int column = column_of_cell(cell);
521 int row = row_of_cell(cell);
523 (cell_info[row][column].multicolumn ==
524 LyXTabular::CELL_PART_OF_MULTICOLUMN));
525 if (GetAdditionalWidth(cell_info[row][column].cellno))
527 #ifdef SPECIAL_COLUM_HANDLING
528 return column_info[column].right_line;
530 return RightLine(cell_info[row][column].cellno, true);
537 bool LyXTabular::IsLastRow(int cell) const
539 return (row_of_cell(cell) == rows_ - 1);
543 int LyXTabular::GetAdditionalHeight(int row) const
545 if (!row || row >= rows_)
551 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
552 switch (cell_info[row - 1][column].multicolumn) {
553 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
554 bottom = cell_info[row - 1][column].bottom_line;
556 case LyXTabular::CELL_NORMAL:
557 bottom = row_info[row - 1].bottom_line;
560 for (int column = 0; column < columns_ - 1 && top; ++column) {
561 switch (cell_info[row][column].multicolumn){
562 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
563 top = cell_info[row][column].top_line;
565 case LyXTabular::CELL_NORMAL:
566 top = row_info[row].top_line;
570 return WIDTH_OF_LINE;
575 int LyXTabular::GetAdditionalWidth(int cell) const
577 // internally already set in SetWidthOfCell
578 // used to get it back in text.C
579 int const col = right_column_of_cell(cell);
580 int const row = row_of_cell(cell);
581 if (col < columns_ - 1 && RightLine(cell, true) &&
582 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
584 return WIDTH_OF_LINE;
591 // returns the maximum over all rows
592 int LyXTabular::GetWidthOfColumn(int cell) const
594 int const column1 = column_of_cell(cell);
595 int const column2 = right_column_of_cell(cell);
597 for (int i = column1; i <= column2; ++i) {
598 result += column_info[i].width_of_column;
604 int LyXTabular::GetWidthOfTabular() const
606 return width_of_tabular;
610 /* returns 1 if a complete update is necessary, otherwise 0 */
611 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
613 if (!IsMultiColumn(cell))
616 int const row = row_of_cell(cell);
617 int const column1 = column_of_cell(cell);
618 int const column2 = right_column_of_cell(cell);
620 // first set columns to 0 so we can calculate the right width
621 for (int i = column1; i <= column2; ++i) {
622 cell_info[row][i].width_of_cell = 0;
624 // set the width to MAX_WIDTH until width > 0
625 int width = (new_width + 2 * WIDTH_OF_LINE);
628 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
629 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
630 width -= column_info[i].width_of_column;
633 cell_info[row][i].width_of_cell = width;
639 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
641 int const row = row_of_cell(cell);
642 int const column1 = column_of_cell(cell);
643 int const column2 = right_column_of_cell(cell);
645 // first set columns to 0 so we can calculate the right width
647 for (; i <= column2; ++i)
648 cell_info[row][i].width_of_cell = 0;
649 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
651 if (i < numberofcells)
652 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
653 SetWidthOfMulticolCell(cell, new_width);
657 /* returns 1 if a complete update is necessary, otherwise 0 */
658 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
660 int const row = row_of_cell(cell);
661 int const column1 = column_of_cell(cell);
666 #ifdef SPECIAL_COLUM_HANDLING
667 if (RightLine(cell_info[row][column1].cellno, true) &&
668 (column1 < columns_-1) &&
669 LeftLine(cell_info[row][column1+1].cellno, true))
671 if (column_info[column1].right_line && (column1 < columns_-1) &&
672 column_info[column1+1].left_line) // additional width
676 add_width = WIDTH_OF_LINE;
678 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
681 if (IsMultiColumn(cell, true)) {
682 tmp = SetWidthOfMulticolCell(cell, new_width);
684 width = (new_width + 2*WIDTH_OF_LINE + add_width);
685 cell_info[row][column1].width_of_cell = width;
686 tmp = calculate_width_of_column_NMC(column1);
690 for (; i<columns_; ++i)
691 calculate_width_of_column_NMC(i);
692 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
694 if (i < numberofcells)
695 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
696 for (i = 0; i < columns_; ++i)
697 calculate_width_of_column(i);
698 calculate_width_of_tabular();
705 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
707 if (!IsMultiColumn(cell) || onlycolumn)
708 column_info[column_of_cell(cell)].alignment = align;
710 cellinfo_of_cell(cell)->alignment = align;
715 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
717 if (!IsMultiColumn(cell) || onlycolumn)
718 column_info[column_of_cell(cell)].valignment = align;
720 cellinfo_of_cell(cell)->valignment = align;
725 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
727 bool flag = !width.empty();
728 int const j = column_of_cell(cell);
730 column_info[j].p_width = width;
731 if (flag) // do this only if there is a width
732 SetAlignment(cell, LYX_ALIGN_LEFT);
733 for (int i = 0; i < rows_; ++i) {
734 int c = GetCellNumber(i, j);
735 flag = !GetPWidth(c).empty(); // because of multicolumns!
736 GetCellInset(c)->setAutoBreakRows(flag);
742 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
744 bool const flag = !width.empty();
746 cellinfo_of_cell(cell)->p_width = width;
747 if (IsMultiColumn(cell)) {
748 GetCellInset(cell)->setAutoBreakRows(flag);
755 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
756 LyXTabular::Feature what)
758 if (what == SET_SPECIAL_MULTI)
759 cellinfo_of_cell(cell)->align_special = special;
761 column_info[column_of_cell(cell)].align_special = special;
766 bool LyXTabular::SetAllLines(int cell, bool line)
768 SetTopLine(cell, line);
769 SetBottomLine(cell, line);
770 SetRightLine(cell, line);
771 SetLeftLine(cell, line);
776 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
778 int const row = row_of_cell(cell);
780 if (onlycolumn || !IsMultiColumn(cell))
781 row_info[row].top_line = line;
783 cellinfo_of_cell(cell)->top_line = line;
788 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
790 if (onlycolumn || !IsMultiColumn(cell))
791 row_info[row_of_cell(cell)].bottom_line = line;
793 cellinfo_of_cell(cell)->bottom_line = line;
798 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
800 if (onlycolumn || !IsMultiColumn(cell))
801 column_info[column_of_cell(cell)].left_line = line;
803 cellinfo_of_cell(cell)->left_line = line;
808 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
810 if (onlycolumn || !IsMultiColumn(cell))
811 column_info[right_column_of_cell(cell)].right_line = line;
813 cellinfo_of_cell(cell)->right_line = line;
818 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
820 if (!onlycolumn && IsMultiColumn(cell))
821 return cellinfo_of_cell(cell)->alignment;
823 return column_info[column_of_cell(cell)].alignment;
827 LyXTabular::VAlignment
828 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
830 if (!onlycolumn && IsMultiColumn(cell))
831 return cellinfo_of_cell(cell)->valignment;
833 return column_info[column_of_cell(cell)].valignment;
837 string const LyXTabular::GetPWidth(int cell) const
839 if (IsMultiColumn(cell))
840 return cellinfo_of_cell(cell)->p_width;
841 return column_info[column_of_cell(cell)].p_width;
845 string const LyXTabular::GetColumnPWidth(int cell) const
847 return column_info[column_of_cell(cell)].p_width;
851 string const LyXTabular::GetMColumnPWidth(int cell) const
853 if (IsMultiColumn(cell))
854 return cellinfo_of_cell(cell)->p_width;
859 string const LyXTabular::GetAlignSpecial(int cell, int what) const
861 if (what == SET_SPECIAL_MULTI)
862 return cellinfo_of_cell(cell)->align_special;
863 return column_info[column_of_cell(cell)].align_special;
867 int LyXTabular::GetWidthOfCell(int cell) const
869 int const row = row_of_cell(cell);
870 int const column1 = column_of_cell(cell);
871 int const column2 = right_column_of_cell(cell);
873 for (int i = column1; i <= column2; ++i) {
874 result += cell_info[row][i].width_of_cell;
880 int LyXTabular::GetBeginningOfTextInCell(int cell) const
884 switch (GetAlignment(cell)){
885 case LYX_ALIGN_CENTER:
886 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
888 case LYX_ALIGN_RIGHT:
889 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
890 // + GetAdditionalWidth(cell);
892 default: /* LYX_ALIGN_LEFT: nothing :-) */
902 bool LyXTabular::IsFirstCellInRow(int cell) const
904 return column_of_cell(cell) == 0;
908 int LyXTabular::GetFirstCellInRow(int row) const
912 return cell_info[row][0].cellno;
915 bool LyXTabular::IsLastCellInRow(int cell) const
917 return (right_column_of_cell(cell) == (columns_ - 1));
921 int LyXTabular::GetLastCellInRow(int row) const
925 return cell_info[row][columns_-1].cellno;
929 bool LyXTabular::calculate_width_of_column(int column)
931 int const old_column_width = column_info[column].width_of_column;
934 for (int i = 0; i < rows_; ++i) {
935 maximum = max(cell_info[i][column].width_of_cell, maximum);
937 column_info[column].width_of_column = maximum;
938 return (column_info[column].width_of_column != old_column_width);
943 // calculate the with of the column without regarding REAL MultiColumn
944 // cells. This means MultiColumn-cells spanning more than 1 column.
946 bool LyXTabular::calculate_width_of_column_NMC(int column)
948 int const old_column_width = column_info[column].width_of_column;
950 for (int i = 0; i < rows_; ++i) {
951 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
952 (cell_info[i][column].width_of_cell > max)) {
953 max = cell_info[i][column].width_of_cell;
956 column_info[column].width_of_column = max;
957 return (column_info[column].width_of_column != old_column_width);
961 void LyXTabular::calculate_width_of_tabular()
963 width_of_tabular = 0;
964 for (int i = 0; i < columns_; ++i) {
965 width_of_tabular += column_info[i].width_of_column;
970 int LyXTabular::row_of_cell(int cell) const
972 if (cell >= numberofcells)
976 return rowofcell[cell];
980 int LyXTabular::column_of_cell(int cell) const
982 if (cell >= numberofcells)
986 return columnofcell[cell];
990 int LyXTabular::right_column_of_cell(int cell) const
992 int const row = row_of_cell(cell);
993 int column = column_of_cell(cell);
994 while (column < (columns_ - 1) &&
995 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1001 // Perfect case for a template... (Lgb)
1002 // or perhaps not...
1005 string const write_attribute(string const & name, T const & t)
1007 string str = " " + name + "=\"" + tostr(t) + "\"";
1012 string const write_attribute(string const & name, bool const & b)
1014 return write_attribute(name, int(b));
1019 string const write_attribute(string const & name, int value)
1021 string str = " " + name + "=\"" + tostr(value) + "\"";
1026 string const write_attribute(string const & name, string const & value)
1028 string str = " " + name + "=\"" + value + "\"";
1033 string const write_attribute(string const & name, bool value)
1035 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1043 string const tostr(LyXAlignment const & num)
1046 case LYX_ALIGN_NONE:
1048 case LYX_ALIGN_BLOCK:
1050 case LYX_ALIGN_LEFT:
1052 case LYX_ALIGN_CENTER:
1054 case LYX_ALIGN_RIGHT:
1056 case LYX_ALIGN_LAYOUT:
1058 case LYX_ALIGN_SPECIAL:
1067 string const tostr(LyXTabular::VAlignment const & num)
1070 case LyXTabular::LYX_VALIGN_TOP:
1072 case LyXTabular::LYX_VALIGN_CENTER:
1074 case LyXTabular::LYX_VALIGN_BOTTOM:
1083 string const tostr(LyXTabular::BoxType const & num)
1086 case LyXTabular::BOX_NONE:
1088 case LyXTabular::BOX_PARBOX:
1090 case LyXTabular::BOX_MINIPAGE:
1097 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1101 << write_attribute("version", 2)
1102 << write_attribute("rows", rows_)
1103 << write_attribute("columns", columns_)
1105 // global longtable options
1107 << write_attribute("rotate", tostr(rotate))
1108 << write_attribute("islongtable", tostr(is_long_tabular))
1109 << write_attribute("endhead", endhead.row)
1110 << write_attribute("endfirsthead", endfirsthead.row)
1111 << write_attribute("endfoot", endfoot.row)
1112 << write_attribute("endlastfoot", endlastfoot.row)
1114 for (int j = 0; j < columns_; ++j) {
1116 << write_attribute("alignment", tostr(column_info[j].alignment))
1117 << write_attribute("valignment", tostr(column_info[j].valignment))
1118 << write_attribute("leftline", tostr(column_info[j].left_line))
1119 << write_attribute("rightline", tostr(column_info[j].right_line))
1120 << write_attribute("width",
1121 VSpace(column_info[j].p_width)
1123 << write_attribute("special", column_info[j].align_special)
1126 for (int i = 0; i < rows_; ++i) {
1128 << write_attribute("topline", tostr(row_info[i].top_line))
1129 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1130 << write_attribute("newpage", tostr(row_info[i].newpage))
1132 for (int j = 0; j < columns_; ++j) {
1134 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1135 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1136 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1137 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1138 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1139 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1140 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1141 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1142 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1143 << write_attribute("width", cell_info[i][j].p_width)
1144 << write_attribute("special", cell_info[i][j].align_special)
1146 os << "\\begin_inset ";
1147 cell_info[i][j].inset.write(buf, os);
1148 os << "\n\\end_inset \n"
1153 os << "</lyxtabular>\n";
1159 // I would have liked a fromstr template a lot better. (Lgb)
1162 bool string2type(string const str, LyXAlignment & num)
1165 num = LYX_ALIGN_NONE;
1166 else if (str == "block")
1167 num = LYX_ALIGN_BLOCK;
1168 else if (str == "left")
1169 num = LYX_ALIGN_LEFT;
1170 else if (str == "center")
1171 num = LYX_ALIGN_CENTER;
1172 else if (str == "right")
1173 num = LYX_ALIGN_RIGHT;
1181 bool string2type(string const str, LyXTabular::VAlignment & num)
1184 num = LyXTabular::LYX_VALIGN_TOP;
1185 else if (str == "center")
1186 num = LyXTabular::LYX_VALIGN_CENTER;
1187 else if (str == "bottom")
1188 num = LyXTabular::LYX_VALIGN_BOTTOM;
1196 bool string2type(string const str, LyXTabular::BoxType & num)
1199 num = LyXTabular::BOX_NONE;
1200 else if (str == "parbox")
1201 num = LyXTabular::BOX_PARBOX;
1202 else if (str == "minipage")
1203 num = LyXTabular::BOX_MINIPAGE;
1211 bool string2type(string const str, bool & num)
1215 else if (str == "false")
1223 bool getTokenValue(string const & str, const char * token, string & ret)
1225 size_t token_length = strlen(token);
1226 string::size_type pos = str.find(token);
1228 if (pos == string::npos || pos + token_length + 1 >= str.length()
1229 || str[pos + token_length] != '=')
1232 pos += token_length + 1;
1234 if ((ch != '"') && (ch != '\'')) { // only read till next space
1238 while((pos < str.length() - 1) && (str[++pos] != ch))
1245 bool getTokenValue(string const & str, const char * token, int & num)
1248 if (!getTokenValue(str, token, tmp))
1250 num = strToInt(tmp);
1255 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1258 if (!getTokenValue(str, token, tmp))
1260 return string2type(tmp, num);
1264 bool getTokenValue(string const & str, const char * token,
1265 LyXTabular::VAlignment & num)
1268 if (!getTokenValue(str, token, tmp))
1270 return string2type(tmp, num);
1274 bool getTokenValue(string const & str, const char * token,
1275 LyXTabular::BoxType & num)
1278 if (!getTokenValue(str, token, tmp))
1280 return string2type(tmp, num);
1284 bool getTokenValue(string const & str, const char * token, bool & flag)
1287 if (!getTokenValue(str, token, tmp))
1289 return string2type(tmp, flag);
1294 void l_getline(istream & is, string & str)
1297 while (str.empty()) {
1299 if (!str.empty() && str[str.length() - 1] == '\r')
1300 str.erase(str.length() - 1);
1307 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1310 istream & is = lex.getStream();
1312 l_getline(is, line);
1313 if (!prefixIs(line, "<lyxtabular ")
1314 && !prefixIs(line, "<LyXTabular ")) {
1315 OldFormatRead(lex, line);
1320 if (!getTokenValue(line, "version", version))
1323 ReadOld(buf, is, lex, line);
1324 else if (version == 2)
1325 ReadNew(buf, is, lex, line);
1329 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1330 LyXLex & lex, string const & l)
1334 if (!getTokenValue(line, "rows", rows_arg))
1337 if (!getTokenValue(line, "columns", columns_arg))
1339 Init(rows_arg, columns_arg);
1340 l_getline(is, line);
1341 if (!prefixIs(line, "<features")) {
1342 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1343 line << ")" << endl;
1346 getTokenValue(line, "rotate", rotate);
1347 getTokenValue(line, "islongtable", is_long_tabular);
1348 getTokenValue(line, "endhead", endhead.row);
1349 getTokenValue(line, "endfirsthead", endfirsthead.row);
1350 getTokenValue(line, "endfoot", endfoot.row);
1351 getTokenValue(line, "endlastfoot", endlastfoot.row);
1352 endhead.row = abs(endhead.row);
1353 endfirsthead.row = abs(endfirsthead.row);
1354 endfoot.row = abs(endfoot.row);
1355 endlastfoot.row = abs(endlastfoot.row);
1356 for (int j = 0; j < columns_; ++j) {
1358 if (!prefixIs(line,"<column")) {
1359 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1360 line << ")" << endl;
1363 getTokenValue(line, "alignment", column_info[j].alignment);
1364 getTokenValue(line, "valignment", column_info[j].valignment);
1365 getTokenValue(line, "leftline", column_info[j].left_line);
1366 getTokenValue(line, "rightline", column_info[j].right_line);
1367 getTokenValue(line, "width", column_info[j].p_width);
1368 getTokenValue(line, "special", column_info[j].align_special);
1371 for (int i = 0; i < rows_; ++i) {
1372 l_getline(is, line);
1373 if (!prefixIs(line, "<row")) {
1374 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1375 line << ")" << endl;
1378 getTokenValue(line, "topline", row_info[i].top_line);
1379 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1380 getTokenValue(line, "newpage", row_info[i].newpage);
1381 for (int j = 0; j < columns_; ++j) {
1382 l_getline(is, line);
1383 if (!prefixIs(line, "<cell")) {
1384 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1385 line << ")" << endl;
1388 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1389 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1390 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1391 getTokenValue(line, "topline", cell_info[i][j].top_line);
1392 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1393 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1394 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1395 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1396 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1397 getTokenValue(line, "width", cell_info[i][j].p_width);
1398 getTokenValue(line, "special", cell_info[i][j].align_special);
1399 l_getline(is, line);
1400 if (prefixIs(line, "\\begin_inset")) {
1401 cell_info[i][j].inset.read(buf, lex);
1402 l_getline(is, line);
1404 if (!prefixIs(line, "</cell>")) {
1405 lyxerr << "Wrong tabular format (expected </cell> got" <<
1406 line << ")" << endl;
1410 l_getline(is, line);
1411 if (!prefixIs(line, "</row>")) {
1412 lyxerr << "Wrong tabular format (expected </row> got" <<
1413 line << ")" << endl;
1417 while (!prefixIs(line, "</lyxtabular>")) {
1418 l_getline(is, line);
1420 set_row_column_number_info();
1424 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1430 int columns_arg = 0;
1431 int is_long_tabular_arg = false;
1432 int rotate_arg = false;
1441 istream & is = lex.getStream();
1444 version = lyx::atoi(s.substr(8, string::npos));
1448 vector<int> cont_row_info;
1451 lyxerr << "Tabular format < 5 is not supported anymore\n"
1452 "Get an older version of LyX (< 1.1.x) for conversion!"
1454 Alert::alert(_("Warning:"),
1455 _("Tabular format < 5 is not supported anymore\n"),
1456 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1458 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1459 >> rotate_arg >> a >> b >> c >> d;
1461 is >> rows_arg >> columns_arg;
1462 Init(rows_arg, columns_arg);
1463 cont_row_info = vector<int>(rows_arg);
1464 SetLongTabular(is_long_tabular_arg);
1465 SetRotateTabular(rotate_arg);
1467 for (i = 0; i < rows_; ++i) {
1469 cont_row_info[i] = false;
1471 for (i = 0; i < columns_; ++i) {
1474 for (i = 0; i < rows_; ++i) {
1475 for (j = 0; j < columns_; ++j) {
1480 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1481 >> rotate_arg >> a >> b >> c >> d;
1482 Init(rows_arg, columns_arg);
1483 cont_row_info = vector<int>(rows_arg);
1484 SetLongTabular(is_long_tabular_arg);
1485 SetRotateTabular(rotate_arg);
1486 endhead.row = a + 1;
1487 endfirsthead.row = b + 1;
1488 endfoot.row = c + 1;
1489 endlastfoot.row = d + 1;
1490 for (i = 0; i < rows_; ++i) {
1491 a = b = c = d = e = f = g = 0;
1492 is >> a >> b >> c >> d;
1493 row_info[i].top_line = a;
1494 row_info[i].bottom_line = b;
1495 cont_row_info[i] = c;
1496 row_info[i].newpage = d;
1498 for (i = 0; i < columns_; ++i) {
1503 char ch; // skip '"'
1506 // ignore is buggy but we will use it later (Lgb)
1507 is.ignore(); // skip '"'
1509 getline(is, s1, '"');
1511 is >> ch; // skip '"'
1513 // ignore is buggy but we will use it later (Lgb)
1514 is.ignore(); // skip '"'
1516 getline(is, s2, '"');
1517 column_info[i].alignment = static_cast<LyXAlignment>(a);
1518 column_info[i].left_line = b;
1519 column_info[i].right_line = c;
1520 column_info[i].p_width = s1;
1521 column_info[i].align_special = s2;
1523 for (i = 0; i < rows_; ++i) {
1524 for (j = 0; j < columns_; ++j) {
1527 is >> a >> b >> c >> d >> e >> f >> g;
1530 is >> ch; // skip '"'
1532 // ignore is buggy but we will use it later (Lgb)
1533 is.ignore(); // skip '"'
1535 getline(is, s1, '"');
1537 is >> ch; // skip '"'
1539 // ignore is buggy but we will use it later (Lgb)
1540 is.ignore(); // skip '"'
1542 getline(is, s2, '"');
1543 cell_info[i][j].multicolumn = static_cast<char>(a);
1544 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1545 cell_info[i][j].top_line = static_cast<char>(c);
1546 cell_info[i][j].bottom_line = static_cast<char>(d);
1547 cell_info[i][j].left_line = column_info[j].left_line;
1548 cell_info[i][j].right_line = column_info[j].right_line;
1549 cell_info[i][j].rotate = static_cast<bool>(f);
1550 cell_info[i][j].usebox = static_cast<BoxType>(g);
1551 cell_info[i][j].align_special = s1;
1552 cell_info[i][j].p_width = s2;
1556 set_row_column_number_info(true);
1558 Paragraph * par = new Paragraph;
1559 Paragraph * return_par = 0;
1563 Paragraph::depth_type depth = 0;
1564 LyXFont font(LyXFont::ALL_INHERIT);
1565 font.setLanguage(owner_->bufferOwner()->getLanguage());
1567 while (lex.isOK()) {
1569 string const token = lex.getString();
1572 if (token == "\\layout"
1573 || token == "\\end_float"
1574 || token == "\\end_deeper") {
1575 lex.pushToken(token);
1576 #ifndef NO_COMPABILITY
1577 // Here we need to insert the inset_ert_contents into the last
1578 // cell of the tabular.
1579 owner_->bufferOwner()->insertErtContents(par, pos, font);
1583 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1588 lex.pushToken(token);
1592 lex.printError("New Paragraph allocated! This should not happen!");
1593 lex.pushToken(token);
1599 // now we have the par we should fill the insets with this!
1601 InsetText * inset = GetCellInset(cell);
1604 for (int i = 0; i < par->size(); ++i) {
1605 if (par->isNewline(i)) {
1607 if (cell > numberofcells) {
1608 lyxerr << "Some error in reading old table format occured!" <<
1609 endl << "Terminating when reading cell[" << cell << "]!" <<
1614 row = row_of_cell(cell);
1615 if (cont_row_info[row]) {
1617 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1618 while(!IsFirstCellInRow(--cell));
1620 inset = GetCellInset(cell);
1623 inset = GetCellInset(cell);
1624 row = row_of_cell(cell);
1625 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1627 // insert a space instead
1629 par->insertChar(i, ' ');
1632 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1633 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1640 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1642 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1643 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1647 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1649 int const row = row_of_cell(cell);
1650 int const column = column_of_cell(cell);
1651 return &cell_info[row][column];
1655 void LyXTabular::SetMultiColumn(int cell, int number)
1657 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1658 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1659 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1660 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1661 for (number--; number > 0; --number) {
1662 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1664 set_row_column_number_info();
1668 int LyXTabular::cells_in_multicolumn(int cell) const
1670 int const row = row_of_cell(cell);
1671 int column = column_of_cell(cell);
1674 while ((column < columns_) &&
1675 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1684 int LyXTabular::UnsetMultiColumn(int cell)
1686 int const row = row_of_cell(cell);
1687 int column = column_of_cell(cell);
1691 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1692 cell_info[row][column].multicolumn = CELL_NORMAL;
1694 while ((column < columns_) &&
1695 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1697 cell_info[row][column].multicolumn = CELL_NORMAL;
1702 set_row_column_number_info();
1707 void LyXTabular::SetLongTabular(bool what)
1709 is_long_tabular = what;
1713 bool LyXTabular::IsLongTabular() const
1715 return is_long_tabular;
1719 void LyXTabular::SetRotateTabular(bool flag)
1725 bool LyXTabular::GetRotateTabular() const
1731 void LyXTabular::SetRotateCell(int cell, bool flag)
1733 cellinfo_of_cell(cell)->rotate = flag;
1737 bool LyXTabular::GetRotateCell(int cell) const
1739 return cellinfo_of_cell(cell)->rotate;
1743 bool LyXTabular::NeedRotating() const
1747 for (int i = 0; i < rows_; ++i) {
1748 for (int j = 0; j < columns_; ++j) {
1749 if (cell_info[i][j].rotate)
1757 bool LyXTabular::IsLastCell(int cell) const
1759 if ((cell + 1) < numberofcells)
1765 int LyXTabular::GetCellAbove(int cell) const
1767 if (row_of_cell(cell) > 0)
1768 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1773 int LyXTabular::GetCellBelow(int cell) const
1775 if (row_of_cell(cell) + 1 < rows_)
1776 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1781 int LyXTabular::GetLastCellAbove(int cell) const
1783 if (row_of_cell(cell) <= 0)
1785 if (!IsMultiColumn(cell))
1786 return GetCellAbove(cell);
1787 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1791 int LyXTabular::GetLastCellBelow(int cell) const
1793 if (row_of_cell(cell) + 1 >= rows_)
1795 if (!IsMultiColumn(cell))
1796 return GetCellBelow(cell);
1797 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1801 int LyXTabular::GetCellNumber(int row, int column) const
1804 if (column >= columns_)
1805 column = columns_ - 1;
1806 else if (column < 0)
1813 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1815 return cell_info[row][column].cellno;
1819 void LyXTabular::SetUsebox(int cell, BoxType type)
1821 cellinfo_of_cell(cell)->usebox = type;
1825 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1827 if (column_info[column_of_cell(cell)].p_width.empty() &&
1828 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1830 if (cellinfo_of_cell(cell)->usebox > 1)
1831 return cellinfo_of_cell(cell)->usebox;
1832 return UseParbox(cell);
1835 bool LyXTabular::checkLTType(int row, ltType const & ltt) const
1837 if (!ltt.row || (ltt.row > rows_))
1839 return (row == (ltt.row - 1));
1843 void LyXTabular::SetLTHead(ltType const & hd, bool first)
1853 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1856 return checkLTType(row, hd);
1860 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1863 return checkLTType(row, hd);
1867 void LyXTabular::SetLTFoot(ltType const & fd, bool last)
1877 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1880 return checkLTType(row, fd);
1884 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1887 return checkLTType(row, fd);
1891 void LyXTabular::SetLTNewPage(int cell, bool what)
1893 row_info[row_of_cell(cell)].newpage = what;
1897 bool LyXTabular::GetLTNewPage(int cell) const
1899 return row_info[row_of_cell(cell)].newpage;
1903 bool LyXTabular::SetAscentOfRow(int row, int height)
1905 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1907 row_info[row].ascent_of_row = height;
1912 bool LyXTabular::SetDescentOfRow(int row, int height)
1914 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1916 row_info[row].descent_of_row = height;
1921 int LyXTabular::GetAscentOfRow(int row) const
1925 return row_info[row].ascent_of_row;
1929 int LyXTabular::GetDescentOfRow(int row) const
1933 return row_info[row].descent_of_row;
1937 int LyXTabular::GetHeightOfTabular() const
1941 for (int row = 0; row < rows_; ++row)
1942 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1943 GetAdditionalHeight(row);
1948 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1950 if ((row >= rows_) || (column >= columns_))
1952 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1956 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1958 if ((row < 0) || (row >= rows_))
1961 int const fcell = GetFirstCellInRow(row);
1962 int const n = NumberOfCellsInRow(fcell) + fcell;
1965 for (int i = fcell; i < n; ++i) {
1969 if (tmp == (n - fcell)){
1972 for (int i = fcell; i < n; ++i) {
1975 << column_of_cell(i) + 1
1977 << right_column_of_cell(i) + 1
1989 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1991 if ((row < 0) || (row >= rows_))
1994 int const fcell = GetFirstCellInRow(row);
1995 int const n = NumberOfCellsInRow(fcell) + fcell;
1998 for (int i = fcell; i < n; ++i) {
2002 if (tmp == (n-fcell)){
2005 for (int i = fcell; i < n; ++i) {
2006 if (BottomLine(i)) {
2008 << column_of_cell(i) + 1
2010 << right_column_of_cell(i) + 1
2022 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2026 if (GetRotateCell(cell)) {
2027 os << "\\begin{sideways}\n";
2030 if (IsMultiColumn(cell)) {
2031 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2032 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2033 os << cellinfo_of_cell(cell)->align_special << "}{";
2035 if (LeftLine(cell) &&
2036 (IsFirstCellInRow(cell) ||
2037 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2038 !RightLine(cell-1, true))))
2042 if (!GetPWidth(cell).empty()) {
2043 switch (GetVAlignment(cell)) {
2044 case LYX_VALIGN_TOP:
2047 case LYX_VALIGN_CENTER:
2050 case LYX_VALIGN_BOTTOM:
2054 os << "{" << GetPWidth(cell) << '}';
2056 switch (GetAlignment(cell)) {
2057 case LYX_ALIGN_LEFT:
2060 case LYX_ALIGN_RIGHT:
2068 if (RightLine(cell))
2070 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2076 if (GetUsebox(cell) == BOX_PARBOX) {
2078 switch (GetVAlignment(cell)) {
2079 case LYX_VALIGN_TOP:
2082 case LYX_VALIGN_CENTER:
2085 case LYX_VALIGN_BOTTOM:
2089 os << "]{" << GetPWidth(cell) << "}{";
2090 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2091 os << "\\begin{minipage}[";
2092 switch (GetVAlignment(cell)) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_CENTER:
2099 case LYX_VALIGN_BOTTOM:
2103 os << "]{" << GetPWidth(cell) << "}\n";
2110 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2115 if (GetUsebox(cell) == BOX_PARBOX)
2117 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2118 os << "%\n\\end{minipage}";
2121 if (IsMultiColumn(cell)){
2124 if (GetRotateCell(cell)) {
2125 os << "%\n\\end{sideways}";
2132 int LyXTabular::Latex(Buffer const * buf,
2133 ostream & os, bool fragile, bool fp) const
2138 //+---------------------------------------------------------------------
2139 //+ first the opening preamble +
2140 //+---------------------------------------------------------------------
2143 os << "\\begin{sideways}\n";
2146 if (is_long_tabular)
2147 os << "\\begin{longtable}{";
2149 os << "\\begin{tabular}{";
2150 for (int i = 0; i < columns_; ++i) {
2151 if (!column_info[i].align_special.empty()) {
2152 os << column_info[i].align_special;
2154 if (column_info[i].left_line)
2156 if (!column_info[i].p_width.empty()) {
2157 switch (column_info[i].valignment) {
2158 case LYX_VALIGN_TOP:
2161 case LYX_VALIGN_CENTER:
2164 case LYX_VALIGN_BOTTOM:
2169 << column_info[i].p_width
2172 switch (column_info[i].alignment) {
2173 case LYX_ALIGN_LEFT:
2176 case LYX_ALIGN_RIGHT:
2184 if (column_info[i].right_line)
2191 //+---------------------------------------------------------------------
2192 //+ the single row and columns (cells) +
2193 //+---------------------------------------------------------------------
2195 for (int i = 0; i < rows_; ++i) {
2196 ret += TeXTopHLine(os, i);
2197 #warning Implement top double lines for LT Header/Footers
2200 ret += TeXBottomHLine(os, i-1);
2201 ret += TeXTopHLine(os, i);
2204 for (int j = 0; j < columns_; ++j) {
2205 if (IsPartOfMultiColumn(i,j))
2207 ret += TeXCellPreamble(os, cell);
2208 InsetText * inset = GetCellInset(cell);
2210 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2211 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2215 ret += inset->latex(buf, os, fragile, fp);
2219 ret += TeXCellPostamble(os, cell);
2220 if (!IsLastCellInRow(cell)) { // not last cell in row
2228 ret += TeXBottomHLine(os, i);
2229 if (IsLongTabular()) {
2230 if (i == (endhead.row - 1)) {
2231 if (endhead.bottomDL)
2232 ret += TeXBottomHLine(os, i);
2233 os << "\\endhead\n";
2236 if (i == (endfirsthead.row - 1)) {
2237 if (endfirsthead.bottomDL)
2238 ret += TeXBottomHLine(os, i);
2239 os << "\\endfirsthead\n";
2242 if (i == (endfoot.row - 1)) {
2243 if (endfoot.bottomDL)
2244 ret += TeXBottomHLine(os, i);
2245 os << "\\endfoot\n";
2248 if (i == (endlastfoot.row - 1)) {
2249 if (endlastfoot.bottomDL)
2250 ret += TeXBottomHLine(os, i);
2251 os << "\\endlastfoot\n";
2254 if (row_info[i].newpage) {
2255 os << "\\newpage\n";
2261 //+---------------------------------------------------------------------
2262 //+ the closing of the tabular +
2263 //+---------------------------------------------------------------------
2265 if (is_long_tabular)
2266 os << "\\end{longtable}";
2268 os << "\\end{tabular}";
2270 os << "\n\\end{sideways}";
2278 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2281 int cell = GetFirstCellInRow(row);
2284 for (int j = 0; j < columns_; ++j) {
2285 if (IsPartOfMultiColumn(row, j))
2288 os << "<entry align=\"";
2289 switch (GetAlignment(cell)) {
2290 case LYX_ALIGN_LEFT:
2293 case LYX_ALIGN_RIGHT:
2301 os << "\" valign=\"";
2302 switch (GetVAlignment(cell)) {
2303 case LYX_VALIGN_TOP:
2306 case LYX_VALIGN_BOTTOM:
2309 case LYX_VALIGN_CENTER:
2314 if (IsMultiColumn(cell)) {
2315 os << " namest=\"col" << j << "\" ";
2316 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2320 ret += GetCellInset(cell)->docbook(buf, os);
2329 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2333 //+---------------------------------------------------------------------
2334 //+ first the opening preamble +
2335 //+---------------------------------------------------------------------
2337 os << "<tgroup cols=\"" << columns_
2338 << "\" colsep=\"1\" rowsep=\"1\">\n";
2340 for (int i = 0; i < columns_; ++i) {
2341 os << "<colspec colname=\"col" << i << "\" align=\"";
2342 switch (column_info[i].alignment) {
2343 case LYX_ALIGN_LEFT:
2346 case LYX_ALIGN_RIGHT:
2357 //+---------------------------------------------------------------------
2358 //+ Long Tabular case +
2359 //+---------------------------------------------------------------------
2361 if ( IsLongTabular() ) {
2363 if( endhead.row || endfirsthead.row ) {
2365 if( endfirsthead.row ) {
2366 docbookRow( buf, os, endfirsthead.row - 1);
2368 if( endhead.row && endhead.row != endfirsthead.row) {
2369 docbookRow(buf, os, endhead.row - 1);
2375 if( endfoot.row || endlastfoot.row ) {
2378 docbookRow( buf, os, endfoot.row - 1);
2380 if( endlastfoot.row && endlastfoot.row != endfoot.row) {
2381 docbookRow( buf, os, endlastfoot.row - 1);
2386 //+---------------------------------------------------------------------
2387 //+ the single row and columns (cells) +
2388 //+---------------------------------------------------------------------
2391 for (int i = 0; i < rows_; ++i) {
2392 if(!IsLongTabular() || (
2393 i != endhead.row - 1 && i != endfirsthead.row - 1 &&
2394 i != endfoot.row - 1 && i != endlastfoot.row - 1)) {
2395 docbookRow( buf, os, i);
2399 //+---------------------------------------------------------------------
2400 //+ the closing of the tabular +
2401 //+---------------------------------------------------------------------
2413 void print_n_chars(ostream & os, unsigned char ch, int n)
2415 os << string(n, ch);
2421 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2422 vector<unsigned int> const & clen) const
2424 int const fcell = GetFirstCellInRow(row);
2425 int const n = NumberOfCellsInRow(fcell) + fcell;
2428 for (int i = fcell; i < n; ++i) {
2438 for (int i = fcell; i < n; ++i) {
2449 int column = column_of_cell(i);
2450 int len = clen[column];
2451 while(IsPartOfMultiColumn(row, ++column))
2452 len += clen[column] + 4;
2453 print_n_chars(os, ch, len);
2468 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2469 vector<unsigned int> const & clen) const
2471 int const fcell = GetFirstCellInRow(row);
2472 int const n = NumberOfCellsInRow(fcell) + fcell;
2475 for (int i = fcell; i < n; ++i) {
2476 if (BottomLine(i)) {
2485 for (int i = fcell; i < n; ++i) {
2486 if (BottomLine(i)) {
2496 int column = column_of_cell(i);
2497 int len = clen[column];
2498 while(IsPartOfMultiColumn(row, ++column))
2499 len += clen[column] + 4;
2500 print_n_chars(os, ch, len);
2501 if (BottomLine(i)) {
2515 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2516 int cell, int row, int column,
2517 vector<unsigned int> const & clen) const
2520 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2527 unsigned int len1 = sstr.str().length();
2528 unsigned int len2 = clen[column];
2529 while(IsPartOfMultiColumn(row, ++column))
2530 len2 += clen[column] + 4;
2533 switch (GetAlignment(cell)) {
2535 case LYX_ALIGN_LEFT:
2538 case LYX_ALIGN_RIGHT:
2542 case LYX_ALIGN_CENTER:
2548 for (unsigned int i = 0; i < len1; ++i)
2551 for (unsigned int i = 0; i < len2; ++i)
2553 if (RightLine(cell))
2562 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2566 //+---------------------------------------------------------------------
2567 //+ first calculate the width of the single columns +
2568 //+---------------------------------------------------------------------
2569 vector<unsigned int> clen(columns_);
2571 // first all non (real) multicolumn cells!
2572 for (int j = 0; j < columns_; ++j) {
2574 for (int i = 0; i < rows_; ++i) {
2575 int cell = GetCellNumber(i, j);
2576 if (IsMultiColumn(cell, true))
2579 GetCellInset(cell)->ascii(buf, sstr, 0);
2580 if (clen[j] < sstr.str().length())
2581 clen[j] = sstr.str().length();
2584 // then all (real) multicolumn cells!
2585 for (int j = 0; j < columns_; ++j) {
2586 for (int i = 0; i < rows_; ++i) {
2587 int cell = GetCellNumber(i, j);
2588 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2591 GetCellInset(cell)->ascii(buf, sstr, 0);
2592 int len = int(sstr.str().length());
2593 int const n = cells_in_multicolumn(cell);
2594 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2596 if (len > int(clen[j + n - 1]))
2597 clen[j + n - 1] = len;
2601 for (int i = 0; i < rows_; ++i) {
2602 AsciiTopHLine(os, i, clen);
2603 for (int j = 0; j < columns_; ++j) {
2604 if (IsPartOfMultiColumn(i,j))
2606 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2610 AsciiBottomHLine(os, i, clen);
2616 InsetText * LyXTabular::GetCellInset(int cell) const
2619 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2623 InsetText * LyXTabular::GetCellInset(int row, int column) const
2625 cur_cell = GetCellNumber(row, column);
2626 return & cell_info[row][column].inset;
2630 void LyXTabular::Validate(LaTeXFeatures & features) const
2632 if (IsLongTabular())
2633 features.require("longtable");
2635 features.require("rotating");
2636 for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
2637 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2638 features.require("array");
2639 GetCellInset(cell)->validate(features);
2644 std::vector<string> const LyXTabular::getLabelList() const
2646 std::vector<string> label_list;
2647 for (int i = 0; i < rows_; ++i)
2648 for (int j = 0; j < columns_; ++j) {
2649 std::vector<string> const l =
2650 GetCellInset(i, j)->getLabelList();
2651 label_list.insert(label_list.end(),
2652 l.begin(), l.end());
2658 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2660 Paragraph * par = GetCellInset(cell)->paragraph();
2662 for (; par; par = par->next()) {
2663 for (int i = 0; i < par->size(); ++i) {
2664 if (par->getChar(i) == Paragraph::META_NEWLINE)