1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
29 #include "lyx_gui_misc.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
34 #include "support/lstrings.h"
35 #include "support/lyxmanip.h"
36 #include "support/LAssert.h"
37 #include "insets/insettabular.h"
38 #include "insets/insettext.h"
48 #ifndef CXX_GLOBAL_CSTD
54 int const WIDTH_OF_LINE = 5;
58 /// Define a few methods for the inner structs
60 LyXTabular::cellstruct::cellstruct()
64 multicolumn = LyXTabular::CELL_NORMAL;
65 alignment = LYX_ALIGN_CENTER;
66 valignment = LYX_VALIGN_TOP;
76 LyXTabular::rowstruct::rowstruct()
86 LyXTabular::columnstruct::columnstruct()
90 alignment = LYX_ALIGN_CENTER;
91 valignment = LYX_VALIGN_TOP;
97 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
101 Init(rows_arg, columns_arg);
105 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
109 Init(lt.rows_, lt.columns_, <);
112 #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)
119 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
127 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
130 #warning This while method should look like this: (Lgb)
135 // If this and lt is not of the same size we have a serious bug
136 // So then it is ok to throw an exception, or for now
138 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
140 cell_info = lt.cell_info;
141 row_info = lt.row_info;
142 column_info = lt.column_info;
144 // long tabular stuff
145 SetLongTabular(lt.is_long_tabular);
146 endhead = lt.endhead;
147 endfoot = lt.endfoot;
148 endfirsthead = lt.endfirsthead;
149 endlastfoot = lt.endlastfoot;
159 LyXTabular * LyXTabular::clone(InsetTabular * inset)
161 LyXTabular * result = new LyXTabular(inset, *this);
163 // don't know if this is good but I need to Clone also
164 // the text-insets here, this is for the Undo-facility!
165 for (int i = 0; i < rows_; ++i) {
166 for (int j = 0; j < columns_; ++j) {
167 result->cell_info[i][j].inset = cell_info[i][j].inset;
168 result->cell_info[i][j].inset.setOwner(inset);
176 /* activates all lines and sets all widths to 0 */
177 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
180 columns_ = columns_arg;
181 row_info = row_vector(rows_, rowstruct());
182 column_info = column_vector(columns_, columnstruct());
183 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
191 for (int i = 0; i < rows_; ++i) {
192 for (int j = 0; j < columns_; ++j) {
193 cell_info[i][j].inset.setOwner(owner_);
194 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
195 cell_info[i][j].cellno = cellno++;
197 cell_info[i].back().right_line = true;
199 row_info.back().bottom_line = true;
200 row_info.front().bottom_line = true;
202 for (int i = 0; i < columns_; ++i) {
203 calculate_width_of_column(i);
205 column_info.back().right_line = true;
207 calculate_width_of_tabular();
209 rowofcell = vector<int>();
210 columnofcell = vector<int>();
211 set_row_column_number_info();
212 is_long_tabular = false;
221 void LyXTabular::AppendRow(int cell)
225 int row = row_of_cell(cell);
227 row_vector::iterator rit = row_info.begin() + row;
228 row_info.insert(rit, rowstruct());
229 // now set the values of the row before
230 row_info[row] = row_info[row+1];
233 cell_vvector::iterator cit = cell_info.begin() + row;
234 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
236 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
239 for (int i = 0; i <= row; ++i) {
240 for (int j = 0; j < columns_; ++j) {
241 c_info[i][j] = cell_info[i][j];
244 for (int i = row + 1; i < rows_; ++i) {
245 for (int j = 0; j < columns_; ++j) {
246 c_info[i][j] = cell_info[i-1][j];
251 for (int j = 0; j < columns_; ++j) {
252 cell_info[row][j].inset.clear();
259 void LyXTabular::DeleteRow(int row)
261 // Why make it so hard? (Lgb)
264 if (rows_ == 1) return; // Not allowed to delete last row
266 row_info.erase(row_info.begin() + row); //&row_info[row]);
267 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
273 void LyXTabular::AppendColumn(int cell)
277 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
279 int const column = column_of_cell(cell);
280 column_vector::iterator cit = column_info.begin() + column + 1;
281 column_info.insert(cit, columnstruct());
282 // set the column values of the column before
283 column_info[column+1] = column_info[column];
285 for (int i = 0; i < rows_; ++i) {
286 for (int j = 0; j <= column; ++j) {
287 c_info[i][j] = cell_info[i][j];
289 for (int j = column + 1; j < columns_; ++j) {
290 c_info[i][j] = cell_info[i][j - 1];
292 // care about multicolumns
293 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
294 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
296 if ((column + 1) == columns_ ||
297 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
298 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
303 for (int i = 0; i < rows_; ++i) {
304 //cell_info[i][column].inset.clear();
305 cell_info[i][column + 1].inset.clear();
311 void LyXTabular::DeleteColumn(int column)
313 // Similar to DeleteRow
314 //if (!(columns_ - 1))
316 if (columns_ == 1) return; // Not allowed to delete last column
318 column_info.erase(column_info.begin() + column);
319 for (int i = 0; i < rows_; ++i) {
320 cell_info[i].erase(cell_info[i].begin() + column);
327 void LyXTabular::Reinit()
329 for (int i = 0; i < rows_; ++i) {
330 for (int j = 0; j < columns_; ++j) {
331 cell_info[i][j].width_of_cell = 0;
332 cell_info[i][j].inset.setOwner(owner_);
336 for (int i = 0; i < columns_; ++i) {
337 calculate_width_of_column(i);
339 calculate_width_of_tabular();
341 set_row_column_number_info();
345 void LyXTabular::set_row_column_number_info(bool oldformat)
348 for (int row = 0; row < rows_; ++row) {
349 for (int column = 0; column<columns_; ++column) {
350 if (cell_info[row][column].multicolumn
351 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
353 cell_info[row][column].cellno = numberofcells;
356 ++numberofcells; // because this is one more than as we start from 0
358 rowofcell.resize(numberofcells);
359 columnofcell.resize(numberofcells);
361 for (int row = 0, column = 0, c = 0;
362 c < numberofcells && row < rows_ && column < columns_;) {
364 columnofcell[c] = column;
368 } while (column < columns_ &&
369 cell_info[row][column].multicolumn
370 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
371 if (column == columns_) {
377 for (int row = 0; row < rows_; ++row) {
378 for (int column = 0; column < columns_; ++column) {
379 if (IsPartOfMultiColumn(row,column))
381 // now set the right line of multicolumns right for oldformat read
383 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
385 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
386 cell_info[row][column].right_line =
387 cell_info[row][column+cn-1].right_line;
389 cell_info[row][column].inset.setAutoBreakRows(
390 !GetPWidth(GetCellNumber(row, column)).empty());
396 int LyXTabular::GetNumberOfCells() const
398 return numberofcells;
402 int LyXTabular::NumberOfCellsInRow(int cell) const
404 int const row = row_of_cell(cell);
406 for (int i = 0; i < columns_; ++i) {
407 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
414 /* returns 1 if there is a topline, returns 0 if not */
415 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
417 int const row = row_of_cell(cell);
419 if (!onlycolumn && IsMultiColumn(cell))
420 return cellinfo_of_cell(cell)->top_line;
421 return row_info[row].top_line;
425 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
427 // no bottom line underneath non-existent cells if you please
428 // Isn't that a programming error? Is so this should
429 // be an Assert instead. (Lgb)
430 if (cell >= numberofcells)
433 if (!onlycolumn && IsMultiColumn(cell))
434 return cellinfo_of_cell(cell)->bottom_line;
435 return row_info[row_of_cell(cell)].bottom_line;
439 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
441 if (!onlycolumn && IsMultiColumn(cell)) {
442 #ifdef SPECIAL_COLUM_HANDLING
443 if (cellinfo_of_cell(cell)->align_special.empty())
444 return cellinfo_of_cell(cell)->left_line;
445 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
447 return cellinfo_of_cell(cell)->left_line;
450 #ifdef SPECIAL_COLUM_HANDLING
451 if (column_info[column_of_cell(cell)].align_special.empty())
452 return column_info[column_of_cell(cell)].left_line;
453 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
455 return column_info[column_of_cell(cell)].left_line;
460 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
462 if (!onlycolumn && IsMultiColumn(cell)) {
463 #ifdef SPECIAL_COLUM_HANDLING
464 if (cellinfo_of_cell(cell)->align_special.empty())
465 return cellinfo_of_cell(cell)->right_line;
466 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
468 return cellinfo_of_cell(cell)->right_line;
471 #ifdef SPECIAL_COLUM_HANDLING
472 if (column_info[column_of_cell(cell)].align_special.empty())
473 return column_info[right_column_of_cell(cell)].right_line;
474 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
476 return column_info[right_column_of_cell(cell)].right_line;
481 bool LyXTabular::TopAlreadyDrawed(int cell) const
483 int row = row_of_cell(cell);
484 if ((row > 0) && !GetAdditionalHeight(row)) {
485 int column = column_of_cell(cell);
488 && cell_info[row][column].multicolumn
489 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
491 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
492 return row_info[row].bottom_line;
494 return cell_info[row][column].bottom_line;
500 bool LyXTabular::LeftAlreadyDrawed(int cell) const
502 int column = column_of_cell(cell);
504 int row = row_of_cell(cell);
506 (cell_info[row][column].multicolumn ==
507 LyXTabular::CELL_PART_OF_MULTICOLUMN));
508 if (GetAdditionalWidth(cell_info[row][column].cellno))
510 #ifdef SPECIAL_COLUM_HANDLING
511 return column_info[column].right_line;
513 return RightLine(cell_info[row][column].cellno, true);
520 bool LyXTabular::IsLastRow(int cell) const
522 return (row_of_cell(cell) == rows_ - 1);
526 int LyXTabular::GetAdditionalHeight(int row) const
528 if (!row || row >= rows_)
534 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
535 switch (cell_info[row - 1][column].multicolumn) {
536 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
537 bottom = cell_info[row - 1][column].bottom_line;
539 case LyXTabular::CELL_NORMAL:
540 bottom = row_info[row - 1].bottom_line;
543 for (int column = 0; column < columns_ - 1 && top; ++column) {
544 switch (cell_info[row][column].multicolumn){
545 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
546 top = cell_info[row][column].top_line;
548 case LyXTabular::CELL_NORMAL:
549 top = row_info[row].top_line;
553 return WIDTH_OF_LINE;
558 int LyXTabular::GetAdditionalWidth(int cell) const
560 // internally already set in SetWidthOfCell
561 // used to get it back in text.C
562 int const col = right_column_of_cell(cell);
563 int const row = row_of_cell(cell);
564 if (col < columns_ - 1 && RightLine(cell, true) &&
565 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
567 return WIDTH_OF_LINE;
574 // returns the maximum over all rows
575 int LyXTabular::GetWidthOfColumn(int cell) const
577 int const column1 = column_of_cell(cell);
578 int const column2 = right_column_of_cell(cell);
580 for (int i = column1; i <= column2; ++i) {
581 result += column_info[i].width_of_column;
587 int LyXTabular::GetWidthOfTabular() const
589 return width_of_tabular;
593 /* returns 1 if a complete update is necessary, otherwise 0 */
594 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
596 if (!IsMultiColumn(cell))
599 int const row = row_of_cell(cell);
600 int const column1 = column_of_cell(cell);
601 int const column2 = right_column_of_cell(cell);
603 // first set columns to 0 so we can calculate the right width
604 for (int i = column1; i <= column2; ++i) {
605 cell_info[row][i].width_of_cell = 0;
607 // set the width to MAX_WIDTH until width > 0
608 int width = (new_width + 2 * WIDTH_OF_LINE);
611 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
612 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
613 width -= column_info[i].width_of_column;
616 cell_info[row][i].width_of_cell = width;
622 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
624 int const row = row_of_cell(cell);
625 int const column1 = column_of_cell(cell);
626 int const column2 = right_column_of_cell(cell);
628 // first set columns to 0 so we can calculate the right width
630 for (; i <= column2; ++i)
631 cell_info[row][i].width_of_cell = 0;
632 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
634 if (i < numberofcells)
635 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
636 SetWidthOfMulticolCell(cell, new_width);
640 /* returns 1 if a complete update is necessary, otherwise 0 */
641 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
643 int const row = row_of_cell(cell);
644 int const column1 = column_of_cell(cell);
649 #ifdef SPECIAL_COLUM_HANDLING
650 if (RightLine(cell_info[row][column1].cellno, true) &&
651 (column1 < columns_-1) &&
652 LeftLine(cell_info[row][column1+1].cellno, true))
654 if (column_info[column1].right_line && (column1 < columns_-1) &&
655 column_info[column1+1].left_line) // additional width
659 add_width = WIDTH_OF_LINE;
661 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
664 if (IsMultiColumn(cell, true)) {
665 tmp = SetWidthOfMulticolCell(cell, new_width);
667 width = (new_width + 2*WIDTH_OF_LINE + add_width);
668 cell_info[row][column1].width_of_cell = width;
669 tmp = calculate_width_of_column_NMC(column1);
673 for (; i<columns_; ++i)
674 calculate_width_of_column_NMC(i);
675 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
677 if (i < numberofcells)
678 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
679 for (i = 0; i < columns_; ++i)
680 calculate_width_of_column(i);
681 calculate_width_of_tabular();
688 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
690 if (!IsMultiColumn(cell) || onlycolumn)
691 column_info[column_of_cell(cell)].alignment = align;
693 cellinfo_of_cell(cell)->alignment = align;
698 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
700 if (!IsMultiColumn(cell) || onlycolumn)
701 column_info[column_of_cell(cell)].valignment = align;
703 cellinfo_of_cell(cell)->valignment = align;
708 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
710 bool flag = !width.empty();
711 int const j = column_of_cell(cell);
713 column_info[j].p_width = width;
714 if (flag) // do this only if there is a width
715 SetAlignment(cell, LYX_ALIGN_LEFT);
716 for (int i = 0; i < rows_; ++i) {
717 int c = GetCellNumber(i, j);
718 flag = !GetPWidth(c).empty(); // because of multicolumns!
719 GetCellInset(c)->setAutoBreakRows(flag);
725 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
727 bool const flag = !width.empty();
729 cellinfo_of_cell(cell)->p_width = width;
730 if (IsMultiColumn(cell)) {
731 GetCellInset(cell)->setAutoBreakRows(flag);
738 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
739 LyXTabular::Feature what)
741 if (what == SET_SPECIAL_MULTI)
742 cellinfo_of_cell(cell)->align_special = special;
744 column_info[column_of_cell(cell)].align_special = special;
749 bool LyXTabular::SetAllLines(int cell, bool line)
751 SetTopLine(cell, line);
752 SetBottomLine(cell, line);
753 SetRightLine(cell, line);
754 SetLeftLine(cell, line);
759 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
761 int const row = row_of_cell(cell);
763 if (onlycolumn || !IsMultiColumn(cell))
764 row_info[row].top_line = line;
766 cellinfo_of_cell(cell)->top_line = line;
771 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
773 if (onlycolumn || !IsMultiColumn(cell))
774 row_info[row_of_cell(cell)].bottom_line = line;
776 cellinfo_of_cell(cell)->bottom_line = line;
781 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
783 if (onlycolumn || !IsMultiColumn(cell))
784 column_info[column_of_cell(cell)].left_line = line;
786 cellinfo_of_cell(cell)->left_line = line;
791 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
793 if (onlycolumn || !IsMultiColumn(cell))
794 column_info[right_column_of_cell(cell)].right_line = line;
796 cellinfo_of_cell(cell)->right_line = line;
801 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
803 if (!onlycolumn && IsMultiColumn(cell))
804 return cellinfo_of_cell(cell)->alignment;
806 return column_info[column_of_cell(cell)].alignment;
810 LyXTabular::VAlignment
811 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
813 if (!onlycolumn && IsMultiColumn(cell))
814 return cellinfo_of_cell(cell)->valignment;
816 return column_info[column_of_cell(cell)].valignment;
820 string const LyXTabular::GetPWidth(int cell) const
822 if (IsMultiColumn(cell))
823 return cellinfo_of_cell(cell)->p_width;
824 return column_info[column_of_cell(cell)].p_width;
828 string const LyXTabular::GetColumnPWidth(int cell) const
830 return column_info[column_of_cell(cell)].p_width;
834 string const LyXTabular::GetMColumnPWidth(int cell) const
836 if (IsMultiColumn(cell))
837 return cellinfo_of_cell(cell)->p_width;
842 string const LyXTabular::GetAlignSpecial(int cell, int what) const
844 if (what == SET_SPECIAL_MULTI)
845 return cellinfo_of_cell(cell)->align_special;
846 return column_info[column_of_cell(cell)].align_special;
850 int LyXTabular::GetWidthOfCell(int cell) const
852 int const row = row_of_cell(cell);
853 int const column1 = column_of_cell(cell);
854 int const column2 = right_column_of_cell(cell);
856 for (int i = column1; i <= column2; ++i) {
857 result += cell_info[row][i].width_of_cell;
863 int LyXTabular::GetBeginningOfTextInCell(int cell) const
867 switch (GetAlignment(cell)){
868 case LYX_ALIGN_CENTER:
869 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
871 case LYX_ALIGN_RIGHT:
872 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
873 // + GetAdditionalWidth(cell);
875 default: /* LYX_ALIGN_LEFT: nothing :-) */
885 bool LyXTabular::IsFirstCellInRow(int cell) const
887 return column_of_cell(cell) == 0;
891 int LyXTabular::GetFirstCellInRow(int row) const
895 return cell_info[row][0].cellno;
898 bool LyXTabular::IsLastCellInRow(int cell) const
900 return (right_column_of_cell(cell) == (columns_ - 1));
904 int LyXTabular::GetLastCellInRow(int row) const
908 return cell_info[row][columns_-1].cellno;
912 bool LyXTabular::calculate_width_of_column(int column)
914 int const old_column_width = column_info[column].width_of_column;
917 for (int i = 0; i < rows_; ++i) {
918 maximum = max(cell_info[i][column].width_of_cell, maximum);
920 column_info[column].width_of_column = maximum;
921 return (column_info[column].width_of_column != old_column_width);
926 // calculate the with of the column without regarding REAL MultiColumn
927 // cells. This means MultiColumn-cells spanning more than 1 column.
929 bool LyXTabular::calculate_width_of_column_NMC(int column)
931 int const old_column_width = column_info[column].width_of_column;
933 for (int i = 0; i < rows_; ++i) {
934 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
935 (cell_info[i][column].width_of_cell > max)) {
936 max = cell_info[i][column].width_of_cell;
939 column_info[column].width_of_column = max;
940 return (column_info[column].width_of_column != old_column_width);
944 void LyXTabular::calculate_width_of_tabular()
946 width_of_tabular = 0;
947 for (int i = 0; i < columns_; ++i) {
948 width_of_tabular += column_info[i].width_of_column;
953 int LyXTabular::row_of_cell(int cell) const
955 if (cell >= numberofcells)
959 return rowofcell[cell];
963 int LyXTabular::column_of_cell(int cell) const
965 if (cell >= numberofcells)
969 return columnofcell[cell];
973 int LyXTabular::right_column_of_cell(int cell) const
975 int const row = row_of_cell(cell);
976 int column = column_of_cell(cell);
977 while (column < (columns_ - 1) &&
978 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
984 // Perfect case for a template... (Lgb)
988 string const write_attribute(string const & name, T const & t)
990 string str = " " + name + "=\"" + tostr(t) + "\"";
995 string const write_attribute(string const & name, bool const & b)
997 return write_attribute(name, int(b));
1002 string const write_attribute(string const & name, int value)
1004 string str = " " + name + "=\"" + tostr(value) + "\"";
1009 string const write_attribute(string const & name, string const & value)
1011 string str = " " + name + "=\"" + value + "\"";
1016 string const write_attribute(string const & name, bool value)
1018 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
1026 string const tostr(LyXAlignment const & num)
1029 case LYX_ALIGN_NONE:
1031 case LYX_ALIGN_BLOCK:
1033 case LYX_ALIGN_LEFT:
1035 case LYX_ALIGN_CENTER:
1037 case LYX_ALIGN_RIGHT:
1039 case LYX_ALIGN_LAYOUT:
1041 case LYX_ALIGN_SPECIAL:
1050 string const tostr(LyXTabular::VAlignment const & num)
1053 case LyXTabular::LYX_VALIGN_TOP:
1055 case LyXTabular::LYX_VALIGN_CENTER:
1057 case LyXTabular::LYX_VALIGN_BOTTOM:
1066 string const tostr(LyXTabular::BoxType const & num)
1069 case LyXTabular::BOX_NONE:
1071 case LyXTabular::BOX_PARBOX:
1073 case LyXTabular::BOX_MINIPAGE:
1080 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1084 << write_attribute("version", 2)
1085 << write_attribute("rows", rows_)
1086 << write_attribute("columns", columns_)
1088 // global longtable options
1090 << write_attribute("rotate", tostr(rotate))
1091 << write_attribute("islongtable", tostr(is_long_tabular))
1092 << write_attribute("endhead", endhead)
1093 << write_attribute("endfirsthead", endfirsthead)
1094 << write_attribute("endfoot", endfoot)
1095 << write_attribute("endlastfoot", endlastfoot)
1097 for (int j = 0; j < columns_; ++j) {
1099 << write_attribute("alignment", tostr(column_info[j].alignment))
1100 << write_attribute("valignment", tostr(column_info[j].valignment))
1101 << write_attribute("leftline", tostr(column_info[j].left_line))
1102 << write_attribute("rightline", tostr(column_info[j].right_line))
1103 << write_attribute("width",
1104 VSpace(column_info[j].p_width)
1106 << write_attribute("special", column_info[j].align_special)
1109 for (int i = 0; i < rows_; ++i) {
1111 << write_attribute("topline", tostr(row_info[i].top_line))
1112 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1113 << write_attribute("newpage", tostr(row_info[i].newpage))
1115 for (int j = 0; j < columns_; ++j) {
1117 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1118 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1119 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1120 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1121 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1122 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1123 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1124 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1125 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1126 << write_attribute("width", cell_info[i][j].p_width)
1127 << write_attribute("special", cell_info[i][j].align_special)
1129 os << "\\begin_inset ";
1130 cell_info[i][j].inset.write(buf, os);
1131 os << "\n\\end_inset \n"
1136 os << "</lyxtabular>\n";
1142 // I would have liked a fromstr template a lot better. (Lgb)
1145 bool string2type(string const str, LyXAlignment & num)
1148 num = LYX_ALIGN_NONE;
1149 else if (str == "block")
1150 num = LYX_ALIGN_BLOCK;
1151 else if (str == "left")
1152 num = LYX_ALIGN_LEFT;
1153 else if (str == "center")
1154 num = LYX_ALIGN_CENTER;
1155 else if (str == "right")
1156 num = LYX_ALIGN_RIGHT;
1164 bool string2type(string const str, LyXTabular::VAlignment & num)
1167 num = LyXTabular::LYX_VALIGN_TOP;
1168 else if (str == "center")
1169 num = LyXTabular::LYX_VALIGN_CENTER;
1170 else if (str == "bottom")
1171 num = LyXTabular::LYX_VALIGN_BOTTOM;
1179 bool string2type(string const str, LyXTabular::BoxType & num)
1182 num = LyXTabular::BOX_NONE;
1183 else if (str == "parbox")
1184 num = LyXTabular::BOX_PARBOX;
1185 else if (str == "minipage")
1186 num = LyXTabular::BOX_MINIPAGE;
1194 bool string2type(string const str, bool & num)
1198 else if (str == "false")
1206 bool getTokenValue(string const & str, const char * token, string & ret)
1208 size_t token_length = strlen(token);
1209 string::size_type pos = str.find(token);
1211 if (pos == string::npos || pos + token_length + 1 >= str.length()
1212 || str[pos + token_length] != '=')
1215 pos += token_length + 1;
1217 if ((ch != '"') && (ch != '\'')) { // only read till next space
1221 while((pos < str.length() - 1) && (str[++pos] != ch))
1228 bool getTokenValue(string const & str, const char * token, int & num)
1231 if (!getTokenValue(str, token, tmp))
1233 num = strToInt(tmp);
1238 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1241 if (!getTokenValue(str, token, tmp))
1243 return string2type(tmp, num);
1247 bool getTokenValue(string const & str, const char * token,
1248 LyXTabular::VAlignment & num)
1251 if (!getTokenValue(str, token, tmp))
1253 return string2type(tmp, num);
1257 bool getTokenValue(string const & str, const char * token,
1258 LyXTabular::BoxType & num)
1261 if (!getTokenValue(str, token, tmp))
1263 return string2type(tmp, num);
1267 bool getTokenValue(string const & str, const char * token, bool & flag)
1270 if (!getTokenValue(str, token, tmp))
1272 return string2type(tmp, flag);
1277 void l_getline(istream & is, string & str)
1280 while (str.empty()) {
1282 if (!str.empty() && str[str.length() - 1] == '\r')
1283 str.erase(str.length() - 1);
1290 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1293 istream & is = lex.getStream();
1295 l_getline(is, line);
1296 if (!prefixIs(line, "<lyxtabular ")
1297 && !prefixIs(line, "<LyXTabular ")) {
1298 OldFormatRead(lex, line);
1303 if (!getTokenValue(line, "version", version))
1306 ReadOld(buf, is, lex, line);
1307 else if (version == 2)
1308 ReadNew(buf, is, lex, line);
1312 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1313 LyXLex & lex, string const & l)
1317 if (!getTokenValue(line, "rows", rows_arg))
1320 if (!getTokenValue(line, "columns", columns_arg))
1322 Init(rows_arg, columns_arg);
1323 l_getline(is, line);
1324 if (!prefixIs(line, "<features")) {
1325 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1326 line << ")" << endl;
1329 getTokenValue(line, "rotate", rotate);
1330 getTokenValue(line, "islongtable", is_long_tabular);
1331 getTokenValue(line, "endhead", endhead);
1332 getTokenValue(line, "endfirsthead", endfirsthead);
1333 getTokenValue(line, "endfoot", endfoot);
1334 getTokenValue(line, "endlastfoot", endlastfoot);
1336 for (int j = 0; j < columns_; ++j) {
1338 if (!prefixIs(line,"<column")) {
1339 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1340 line << ")" << endl;
1343 getTokenValue(line, "alignment", column_info[j].alignment);
1344 getTokenValue(line, "valignment", column_info[j].valignment);
1345 getTokenValue(line, "leftline", column_info[j].left_line);
1346 getTokenValue(line, "rightline", column_info[j].right_line);
1347 getTokenValue(line, "width", column_info[j].p_width);
1348 getTokenValue(line, "special", column_info[j].align_special);
1351 for (int i = 0; i < rows_; ++i) {
1352 l_getline(is, line);
1353 if (!prefixIs(line, "<row")) {
1354 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1355 line << ")" << endl;
1358 getTokenValue(line, "topline", row_info[i].top_line);
1359 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1360 getTokenValue(line, "newpage", row_info[i].newpage);
1361 for (int j = 0; j < columns_; ++j) {
1362 l_getline(is, line);
1363 if (!prefixIs(line, "<cell")) {
1364 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1365 line << ")" << endl;
1368 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1369 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1370 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1371 getTokenValue(line, "topline", cell_info[i][j].top_line);
1372 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1373 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1374 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1375 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1376 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1377 getTokenValue(line, "width", cell_info[i][j].p_width);
1378 getTokenValue(line, "special", cell_info[i][j].align_special);
1379 l_getline(is, line);
1380 if (prefixIs(line, "\\begin_inset")) {
1381 cell_info[i][j].inset.read(buf, lex);
1382 l_getline(is, line);
1384 if (!prefixIs(line, "</cell>")) {
1385 lyxerr << "Wrong tabular format (expected </cell> got" <<
1386 line << ")" << endl;
1390 l_getline(is, line);
1391 if (!prefixIs(line, "</row>")) {
1392 lyxerr << "Wrong tabular format (expected </row> got" <<
1393 line << ")" << endl;
1397 while (!prefixIs(line, "</lyxtabular>")) {
1398 l_getline(is, line);
1400 set_row_column_number_info();
1404 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1410 int columns_arg = 0;
1411 int is_long_tabular_arg = false;
1412 int rotate_arg = false;
1421 istream & is = lex.getStream();
1424 version = lyx::atoi(s.substr(8, string::npos));
1428 vector<int> cont_row_info;
1431 lyxerr << "Tabular format < 5 is not supported anymore\n"
1432 "Get an older version of LyX (< 1.1.x) for conversion!"
1434 WriteAlert(_("Warning:"),
1435 _("Tabular format < 5 is not supported anymore\n"),
1436 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1438 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1439 >> rotate_arg >> a >> b >> c >> d;
1441 is >> rows_arg >> columns_arg;
1442 Init(rows_arg, columns_arg);
1443 cont_row_info = vector<int>(rows_arg);
1444 SetLongTabular(is_long_tabular_arg);
1445 SetRotateTabular(rotate_arg);
1447 for (i = 0; i < rows_; ++i) {
1449 cont_row_info[i] = false;
1451 for (i = 0; i < columns_; ++i) {
1454 for (i = 0; i < rows_; ++i) {
1455 for (j = 0; j < columns_; ++j) {
1460 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1461 >> rotate_arg >> a >> b >> c >> d;
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 endfirsthead = b + 1;
1469 endlastfoot = d + 1;
1470 for (i = 0; i < rows_; ++i) {
1471 a = b = c = d = e = f = g = 0;
1472 is >> a >> b >> c >> d;
1473 row_info[i].top_line = a;
1474 row_info[i].bottom_line = b;
1475 cont_row_info[i] = c;
1476 row_info[i].newpage = d;
1478 for (i = 0; i < columns_; ++i) {
1483 char ch; // skip '"'
1486 // ignore is buggy but we will use it later (Lgb)
1487 is.ignore(); // skip '"'
1489 getline(is, s1, '"');
1491 is >> ch; // skip '"'
1493 // ignore is buggy but we will use it later (Lgb)
1494 is.ignore(); // skip '"'
1496 getline(is, s2, '"');
1497 column_info[i].alignment = static_cast<LyXAlignment>(a);
1498 column_info[i].left_line = b;
1499 column_info[i].right_line = c;
1500 column_info[i].p_width = s1;
1501 column_info[i].align_special = s2;
1503 for (i = 0; i < rows_; ++i) {
1504 for (j = 0; j < columns_; ++j) {
1507 is >> a >> b >> c >> d >> e >> f >> g;
1510 is >> ch; // skip '"'
1512 // ignore is buggy but we will use it later (Lgb)
1513 is.ignore(); // skip '"'
1515 getline(is, s1, '"');
1517 is >> ch; // skip '"'
1519 // ignore is buggy but we will use it later (Lgb)
1520 is.ignore(); // skip '"'
1522 getline(is, s2, '"');
1523 cell_info[i][j].multicolumn = static_cast<char>(a);
1524 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1525 cell_info[i][j].top_line = static_cast<char>(c);
1526 cell_info[i][j].bottom_line = static_cast<char>(d);
1527 cell_info[i][j].left_line = column_info[j].left_line;
1528 cell_info[i][j].right_line = column_info[j].right_line;
1529 cell_info[i][j].rotate = static_cast<bool>(f);
1530 cell_info[i][j].usebox = static_cast<BoxType>(g);
1531 cell_info[i][j].align_special = s1;
1532 cell_info[i][j].p_width = s2;
1536 set_row_column_number_info(true);
1538 Paragraph * par = new Paragraph;
1539 Paragraph * return_par = 0;
1543 Paragraph::depth_type depth = 0;
1544 LyXFont font(LyXFont::ALL_INHERIT);
1545 font.setLanguage(owner_->bufferOwner()->getLanguage());
1547 while (lex.isOK()) {
1549 string const token = lex.getString();
1552 if (token == "\\layout"
1553 || token == "\\end_float"
1554 || token == "\\end_deeper") {
1555 lex.pushToken(token);
1556 #ifndef NO_COMPABILITY
1557 // Here we need to insert the inset_ert_contents into the last
1558 // cell of the tabular.
1559 owner_->bufferOwner()->insertErtContents(par, pos, font);
1563 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1568 lex.pushToken(token);
1572 lex.printError("New Paragraph allocated! This should not happen!");
1573 lex.pushToken(token);
1579 // now we have the par we should fill the insets with this!
1581 InsetText * inset = GetCellInset(cell);
1584 for (int i = 0; i < par->size(); ++i) {
1585 if (par->isNewline(i)) {
1587 if (cell > numberofcells) {
1588 lyxerr << "Some error in reading old table format occured!" <<
1589 endl << "Terminating when reading cell[" << cell << "]!" <<
1594 row = row_of_cell(cell);
1595 if (cont_row_info[row]) {
1597 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1598 while(!IsFirstCellInRow(--cell));
1600 inset = GetCellInset(cell);
1603 inset = GetCellInset(cell);
1604 row = row_of_cell(cell);
1605 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1607 // insert a space instead
1609 par->insertChar(i, ' ');
1612 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1613 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1620 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1622 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1623 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1627 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1629 int const row = row_of_cell(cell);
1630 int const column = column_of_cell(cell);
1631 return &cell_info[row][column];
1635 void LyXTabular::SetMultiColumn(int cell, int number)
1637 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1638 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1639 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1640 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1641 for (number--; number > 0; --number) {
1642 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1644 set_row_column_number_info();
1648 int LyXTabular::cells_in_multicolumn(int cell) const
1650 int const row = row_of_cell(cell);
1651 int column = column_of_cell(cell);
1654 while ((column < columns_) &&
1655 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1664 int LyXTabular::UnsetMultiColumn(int cell)
1666 int const row = row_of_cell(cell);
1667 int column = column_of_cell(cell);
1671 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1672 cell_info[row][column].multicolumn = CELL_NORMAL;
1674 while ((column < columns_) &&
1675 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1677 cell_info[row][column].multicolumn = CELL_NORMAL;
1682 set_row_column_number_info();
1687 void LyXTabular::SetLongTabular(bool what)
1689 is_long_tabular = what;
1693 bool LyXTabular::IsLongTabular() const
1695 return is_long_tabular;
1699 void LyXTabular::SetRotateTabular(bool flag)
1705 bool LyXTabular::GetRotateTabular() const
1711 void LyXTabular::SetRotateCell(int cell, bool flag)
1713 cellinfo_of_cell(cell)->rotate = flag;
1717 bool LyXTabular::GetRotateCell(int cell) const
1719 return cellinfo_of_cell(cell)->rotate;
1723 bool LyXTabular::NeedRotating() const
1727 for (int i = 0; i < rows_; ++i) {
1728 for (int j = 0; j < columns_; ++j) {
1729 if (cell_info[i][j].rotate)
1737 bool LyXTabular::IsLastCell(int cell) const
1739 if ((cell + 1) < numberofcells)
1745 int LyXTabular::GetCellAbove(int cell) const
1747 if (row_of_cell(cell) > 0)
1748 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1753 int LyXTabular::GetCellBelow(int cell) const
1755 if (row_of_cell(cell) + 1 < rows_)
1756 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1761 int LyXTabular::GetLastCellAbove(int cell) const
1763 if (row_of_cell(cell) <= 0)
1765 if (!IsMultiColumn(cell))
1766 return GetCellAbove(cell);
1767 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1771 int LyXTabular::GetLastCellBelow(int cell) const
1773 if (row_of_cell(cell) + 1 >= rows_)
1775 if (!IsMultiColumn(cell))
1776 return GetCellBelow(cell);
1777 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1781 int LyXTabular::GetCellNumber(int row, int column) const
1784 if (column >= columns_)
1785 column = columns_ - 1;
1786 else if (column < 0)
1793 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1795 return cell_info[row][column].cellno;
1799 void LyXTabular::SetUsebox(int cell, BoxType type)
1801 cellinfo_of_cell(cell)->usebox = type;
1805 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1807 if (column_info[column_of_cell(cell)].p_width.empty() &&
1808 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1810 if (cellinfo_of_cell(cell)->usebox > 1)
1811 return cellinfo_of_cell(cell)->usebox;
1812 return UseParbox(cell);
1816 void LyXTabular::SetLTHead(int cell, bool first)
1818 int const row = row_of_cell(cell);
1819 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1822 if (endfirsthead == val)
1835 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1838 if (abs(endhead) > rows_)
1840 return (row_of_cell(cell) == abs(endhead) - 1);
1844 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1847 if (abs(endfirsthead) > rows_)
1849 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1853 void LyXTabular::SetLTFoot(int cell, bool last)
1855 int const row = row_of_cell(cell);
1856 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1859 if (endlastfoot == val)
1872 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1875 if ((endfoot + 1) > rows_)
1877 return (row_of_cell(cell) == abs(endfoot) - 1);
1881 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1884 if (abs(endlastfoot) > rows_)
1886 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1890 void LyXTabular::SetLTNewPage(int cell, bool what)
1892 row_info[row_of_cell(cell)].newpage = what;
1896 bool LyXTabular::GetLTNewPage(int cell) const
1898 return row_info[row_of_cell(cell)].newpage;
1902 bool LyXTabular::SetAscentOfRow(int row, int height)
1904 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1906 row_info[row].ascent_of_row = height;
1911 bool LyXTabular::SetDescentOfRow(int row, int height)
1913 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1915 row_info[row].descent_of_row = height;
1920 int LyXTabular::GetAscentOfRow(int row) const
1924 return row_info[row].ascent_of_row;
1928 int LyXTabular::GetDescentOfRow(int row) const
1932 return row_info[row].descent_of_row;
1936 int LyXTabular::GetHeightOfTabular() const
1940 for (int row = 0; row < rows_; ++row)
1941 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1942 GetAdditionalHeight(row);
1947 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1949 if ((row >= rows_) || (column >= columns_))
1951 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1955 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1957 if ((row < 0) || (row >= rows_))
1960 int const fcell = GetFirstCellInRow(row);
1961 int const n = NumberOfCellsInRow(fcell) + fcell;
1964 for (int i = fcell; i < n; ++i) {
1968 if (tmp == (n - fcell)){
1971 for (int i = fcell; i < n; ++i) {
1974 << column_of_cell(i) + 1
1976 << right_column_of_cell(i) + 1
1988 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1990 if ((row < 0) || (row >= rows_))
1993 int const fcell = GetFirstCellInRow(row);
1994 int const n = NumberOfCellsInRow(fcell) + fcell;
1997 for (int i = fcell; i < n; ++i) {
2001 if (tmp == (n-fcell)){
2004 for (int i = fcell; i < n; ++i) {
2005 if (BottomLine(i)) {
2007 << column_of_cell(i) + 1
2009 << right_column_of_cell(i) + 1
2021 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2025 if (GetRotateCell(cell)) {
2026 os << "\\begin{sideways}\n";
2029 if (IsMultiColumn(cell)) {
2030 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2031 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2032 os << cellinfo_of_cell(cell)->align_special << "}{";
2034 if (LeftLine(cell) &&
2035 (IsFirstCellInRow(cell) ||
2036 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2037 !RightLine(cell-1, true))))
2041 if (!GetPWidth(cell).empty()) {
2042 switch (GetVAlignment(cell)) {
2043 case LYX_VALIGN_TOP:
2046 case LYX_VALIGN_CENTER:
2049 case LYX_VALIGN_BOTTOM:
2053 os << "{" << GetPWidth(cell) << '}';
2055 switch (GetAlignment(cell)) {
2056 case LYX_ALIGN_LEFT:
2059 case LYX_ALIGN_RIGHT:
2067 if (RightLine(cell))
2069 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2075 if (GetUsebox(cell) == BOX_PARBOX) {
2077 switch (GetVAlignment(cell)) {
2078 case LYX_VALIGN_TOP:
2081 case LYX_VALIGN_CENTER:
2084 case LYX_VALIGN_BOTTOM:
2088 os << "]{" << GetPWidth(cell) << "}{";
2089 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2090 os << "\\begin{minipage}[";
2091 switch (GetVAlignment(cell)) {
2092 case LYX_VALIGN_TOP:
2095 case LYX_VALIGN_CENTER:
2098 case LYX_VALIGN_BOTTOM:
2102 os << "]{" << GetPWidth(cell) << "}\n";
2109 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2114 if (GetUsebox(cell) == BOX_PARBOX)
2116 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2117 os << "%\n\\end{minipage}";
2120 if (IsMultiColumn(cell)){
2123 if (GetRotateCell(cell)) {
2124 os << "%\n\\end{sideways}";
2131 int LyXTabular::Latex(Buffer const * buf,
2132 ostream & os, bool fragile, bool fp) const
2137 //+---------------------------------------------------------------------
2138 //+ first the opening preamble +
2139 //+---------------------------------------------------------------------
2142 os << "\\begin{sideways}\n";
2145 if (is_long_tabular)
2146 os << "\\begin{longtable}{";
2148 os << "\\begin{tabular}{";
2149 for (int i = 0; i < columns_; ++i) {
2150 if (!column_info[i].align_special.empty()) {
2151 os << column_info[i].align_special;
2153 if (column_info[i].left_line)
2155 if (!column_info[i].p_width.empty()) {
2156 switch (column_info[i].valignment) {
2157 case LYX_VALIGN_TOP:
2160 case LYX_VALIGN_CENTER:
2163 case LYX_VALIGN_BOTTOM:
2168 << column_info[i].p_width
2171 switch (column_info[i].alignment) {
2172 case LYX_ALIGN_LEFT:
2175 case LYX_ALIGN_RIGHT:
2183 if (column_info[i].right_line)
2190 //+---------------------------------------------------------------------
2191 //+ the single row and columns (cells) +
2192 //+---------------------------------------------------------------------
2194 for (int i = 0; i < rows_; ++i) {
2195 ret += TeXTopHLine(os, i);
2197 if (IsLongTabular()) {
2198 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2199 os << "\\endhead\n";
2202 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2203 os << "\\endfirsthead\n";
2206 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2207 os << "\\endfoot\n";
2210 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2211 os << "\\endlastfoot\n";
2216 ret += TeXBottomHLine(os, i-1);
2217 ret += TeXTopHLine(os, i);
2219 for (int j = 0; j < columns_; ++j) {
2220 if (IsPartOfMultiColumn(i,j))
2222 ret += TeXCellPreamble(os, cell);
2223 InsetText * inset = GetCellInset(cell);
2225 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2226 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2230 ret += inset->latex(buf, os, fragile, fp);
2234 ret += TeXCellPostamble(os, cell);
2235 if (!IsLastCellInRow(cell)) { // not last cell in row
2242 ret += TeXBottomHLine(os, i);
2244 if (IsLongTabular()) {
2245 if ((endhead > 0) && (i == (endhead - 1))) {
2246 os << "\\endhead\n";
2249 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2250 os << "\\endfirsthead\n";
2253 if ((endfoot > 0) && (i == (endfoot - 1))) {
2254 os << "\\endfoot\n";
2257 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2258 os << "\\endlastfoot\n";
2262 // ret += TeXBottomHLine(os, i);
2263 if (row_info[i].newpage) {
2264 os << "\\newpage\n";
2270 //+---------------------------------------------------------------------
2271 //+ the closing of the tabular +
2272 //+---------------------------------------------------------------------
2274 if (is_long_tabular)
2275 os << "\\end{longtable}";
2277 os << "\\end{tabular}";
2279 os << "\n\\end{sideways}";
2287 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2291 //+---------------------------------------------------------------------
2292 //+ first the opening preamble +
2293 //+---------------------------------------------------------------------
2295 os << "<tgroup cols=\"" << columns_
2296 << "\" colsep=\"1\" rowsep=\"1\">\n";
2298 for (int i = 0; i < columns_; ++i) {
2299 os << "<colspec colname=\"col" << i << "\" align=\"";
2300 switch (column_info[i].alignment) {
2301 case LYX_ALIGN_LEFT:
2304 case LYX_ALIGN_RIGHT:
2315 //+---------------------------------------------------------------------
2316 //+ the single row and columns (cells) +
2317 //+---------------------------------------------------------------------
2321 for (int i = 0; i < rows_; ++i) {
2323 for (int j = 0; j < columns_; ++j) {
2324 if (IsPartOfMultiColumn(i, j))
2327 os << "<entry align=\"";
2328 switch (GetAlignment(cell)) {
2329 case LYX_ALIGN_LEFT:
2332 case LYX_ALIGN_RIGHT:
2340 os << "\" valign=\"";
2341 switch (GetVAlignment(cell)) {
2342 case LYX_VALIGN_TOP:
2345 case LYX_VALIGN_BOTTOM:
2348 case LYX_VALIGN_CENTER:
2353 if (IsMultiColumn(cell)) {
2354 os << " namest=\"col" << j << "\" ";
2355 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2359 ret += GetCellInset(cell)->docbook(buf, os);
2366 //+---------------------------------------------------------------------
2367 //+ the closing of the tabular +
2368 //+---------------------------------------------------------------------
2380 void print_n_chars(ostream & os, unsigned char ch, int n)
2382 os << string(n, ch);
2388 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2389 vector<unsigned int> const & clen) const
2391 int const fcell = GetFirstCellInRow(row);
2392 int const n = NumberOfCellsInRow(fcell) + fcell;
2395 for (int i = fcell; i < n; ++i) {
2405 for (int i = fcell; i < n; ++i) {
2416 int column = column_of_cell(i);
2417 int len = clen[column];
2418 while(IsPartOfMultiColumn(row, ++column))
2419 len += clen[column] + 4;
2420 print_n_chars(os, ch, len);
2435 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2436 vector<unsigned int> const & clen) const
2438 int const fcell = GetFirstCellInRow(row);
2439 int const n = NumberOfCellsInRow(fcell) + fcell;
2442 for (int i = fcell; i < n; ++i) {
2443 if (BottomLine(i)) {
2452 for (int i = fcell; i < n; ++i) {
2453 if (BottomLine(i)) {
2463 int column = column_of_cell(i);
2464 int len = clen[column];
2465 while(IsPartOfMultiColumn(row, ++column))
2466 len += clen[column] + 4;
2467 print_n_chars(os, ch, len);
2468 if (BottomLine(i)) {
2482 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2483 int cell, int row, int column,
2484 vector<unsigned int> const & clen) const
2487 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2494 unsigned int len1 = sstr.str().length();
2495 unsigned int len2 = clen[column];
2496 while(IsPartOfMultiColumn(row, ++column))
2497 len2 += clen[column] + 4;
2500 switch (GetAlignment(cell)) {
2502 case LYX_ALIGN_LEFT:
2505 case LYX_ALIGN_RIGHT:
2509 case LYX_ALIGN_CENTER:
2515 for (unsigned int i = 0; i < len1; ++i)
2518 for (unsigned int i = 0; i < len2; ++i)
2520 if (RightLine(cell))
2529 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2533 //+---------------------------------------------------------------------
2534 //+ first calculate the width of the single columns +
2535 //+---------------------------------------------------------------------
2536 vector<unsigned int> clen(columns_);
2538 // first all non (real) multicolumn cells!
2539 for (int j = 0; j < columns_; ++j) {
2541 for (int i = 0; i < rows_; ++i) {
2542 int cell = GetCellNumber(i, j);
2543 if (IsMultiColumn(cell, true))
2546 GetCellInset(cell)->ascii(buf, sstr, 0);
2547 if (clen[j] < sstr.str().length())
2548 clen[j] = sstr.str().length();
2551 // then all (real) multicolumn cells!
2552 for (int j = 0; j < columns_; ++j) {
2553 for (int i = 0; i < rows_; ++i) {
2554 int cell = GetCellNumber(i, j);
2555 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2558 GetCellInset(cell)->ascii(buf, sstr, 0);
2559 int len = int(sstr.str().length());
2560 int const n = cells_in_multicolumn(cell);
2561 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2563 if (len > int(clen[j + n - 1]))
2564 clen[j + n - 1] = len;
2568 for (int i = 0; i < rows_; ++i) {
2569 AsciiTopHLine(os, i, clen);
2570 for (int j = 0; j < columns_; ++j) {
2571 if (IsPartOfMultiColumn(i,j))
2573 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2577 AsciiBottomHLine(os, i, clen);
2583 InsetText * LyXTabular::GetCellInset(int cell) const
2586 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2590 InsetText * LyXTabular::GetCellInset(int row, int column) const
2592 cur_cell = GetCellNumber(row, column);
2593 return & cell_info[row][column].inset;
2597 void LyXTabular::Validate(LaTeXFeatures & features) const
2599 if (IsLongTabular())
2600 features.longtable = true;
2602 features.rotating = true;
2603 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2604 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2605 features.array = true;
2606 GetCellInset(cell)->validate(features);
2611 std::vector<string> const LyXTabular::getLabelList() const
2613 std::vector<string> label_list;
2614 for (int i = 0; i < rows_; ++i)
2615 for (int j = 0; j < columns_; ++j) {
2616 std::vector<string> const l =
2617 GetCellInset(i, j)->getLabelList();
2618 label_list.insert(label_list.end(),
2619 l.begin(), l.end());
2625 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2627 Paragraph * par = GetCellInset(cell)->paragraph();
2629 for (; par; par = par->next()) {
2630 for (int i = 0; i < par->size(); ++i) {
2631 if (par->getChar(i) == Paragraph::META_NEWLINE)