1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
26 #include "lyx_gui_misc.h"
28 #include "BufferView.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
46 int const WIDTH_OF_LINE = 5;
50 /// Define a few methods for the inner structs
52 LyXTabular::cellstruct::cellstruct()
56 multicolumn = LyXTabular::CELL_NORMAL;
57 alignment = LYX_ALIGN_CENTER;
58 valignment = LYX_VALIGN_TOP;
68 LyXTabular::rowstruct::rowstruct()
78 LyXTabular::columnstruct::columnstruct()
82 alignment = LYX_ALIGN_CENTER;
83 valignment = LYX_VALIGN_TOP;
89 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
92 Init(rows_arg, columns_arg);
96 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
99 Init(lt.rows_, lt.columns_, <);
102 #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)
109 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
116 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
119 #warning This while method should look like this: (Lgb)
124 // If this and lt is not of the same size we have a serious bug
125 // So then it is ok to throw an exception, or for now
127 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
129 cell_info = lt.cell_info;
130 row_info = lt.row_info;
131 column_info = lt.column_info;
133 // long tabular stuff
134 SetLongTabular(lt.is_long_tabular);
135 endhead = lt.endhead;
136 endfoot = lt.endfoot;
137 endfirsthead = lt.endfirsthead;
138 endlastfoot = lt.endlastfoot;
148 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
150 LyXTabular * result = new LyXTabular(inset, *this);
152 // don't know if this is good but I need to Clone also
153 // the text-insets here, this is for the Undo-facility!
154 for (int i = 0; i < rows_; ++i) {
155 for (int j = 0; j < columns_; ++j) {
156 result->cell_info[i][j].inset = cell_info[i][j].inset;
157 result->cell_info[i][j].inset.setOwner(inset);
165 /* activates all lines and sets all widths to 0 */
166 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
169 columns_ = columns_arg;
170 row_info = row_vector(rows_, rowstruct());
171 column_info = column_vector(columns_, columnstruct());
172 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
180 for (int i = 0; i < rows_; ++i) {
181 for (int j = 0; j < columns_; ++j) {
182 cell_info[i][j].inset.setOwner(owner_);
183 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
184 cell_info[i][j].cellno = cellno++;
186 cell_info[i].back().right_line = true;
188 row_info.back().bottom_line = true;
189 row_info.front().bottom_line = true;
191 for (int i = 0; i < columns_; ++i) {
192 calculate_width_of_column(i);
194 column_info.back().right_line = true;
196 calculate_width_of_tabular();
198 rowofcell = vector<int>();
199 columnofcell = vector<int>();
200 set_row_column_number_info();
201 is_long_tabular = false;
210 void LyXTabular::AppendRow(int cell)
214 int row = row_of_cell(cell);
216 row_vector::iterator rit = row_info.begin() + row;
217 row_info.insert(rit, rowstruct());
218 // now set the values of the row before
219 row_info[row] = row_info[row+1];
222 cell_vvector::iterator cit = cell_info.begin() + row;
223 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
225 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
228 for (int i = 0; i <= row; ++i) {
229 for (int j = 0; j < columns_; ++j) {
230 c_info[i][j] = cell_info[i][j];
233 for (int i = row + 1; i < rows_; ++i) {
234 for (int j = 0; j < columns_; ++j) {
235 c_info[i][j] = cell_info[i-1][j];
240 for (int j = 0; j < columns_; ++j) {
241 cell_info[row][j].inset.clear();
248 void LyXTabular::DeleteRow(int row)
250 // Why make it so hard? (Lgb)
253 if (rows_ == 1) return; // Not allowed to delete last row
255 row_info.erase(row_info.begin() + row); //&row_info[row]);
256 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
262 void LyXTabular::AppendColumn(int cell)
266 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
268 int const column = column_of_cell(cell);
269 column_vector::iterator cit = column_info.begin() + column + 1;
270 column_info.insert(cit, columnstruct());
271 // set the column values of the column before
272 column_info[column+1] = column_info[column];
274 for (int i = 0; i < rows_; ++i) {
275 for (int j = 0; j <= column; ++j) {
276 c_info[i][j] = cell_info[i][j];
278 for (int j = column + 1; j < columns_; ++j) {
279 c_info[i][j] = cell_info[i][j - 1];
281 // care about multicolumns
282 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
283 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
285 if ((column + 1) == columns_ ||
286 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
287 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
292 for (int i = 0; i < rows_; ++i) {
293 //cell_info[i][column].inset.clear();
294 cell_info[i][column + 1].inset.clear();
300 void LyXTabular::DeleteColumn(int column)
302 // Similar to DeleteRow
303 //if (!(columns_ - 1))
305 if (columns_ == 1) return; // Not allowed to delete last column
307 column_info.erase(column_info.begin() + column);
308 for (int i = 0; i < rows_; ++i) {
309 cell_info[i].erase(cell_info[i].begin() + column);
316 void LyXTabular::Reinit()
318 for (int i = 0; i < rows_; ++i) {
319 for (int j = 0; j < columns_; ++j) {
320 cell_info[i][j].width_of_cell = 0;
321 cell_info[i][j].inset.setOwner(owner_);
325 for (int i = 0; i < columns_; ++i) {
326 calculate_width_of_column(i);
328 calculate_width_of_tabular();
330 set_row_column_number_info();
334 void LyXTabular::set_row_column_number_info(bool oldformat)
337 for (int row = 0; row < rows_; ++row) {
338 for (int column = 0; column<columns_; ++column) {
339 if (cell_info[row][column].multicolumn
340 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
342 cell_info[row][column].cellno = numberofcells;
345 ++numberofcells; // because this is one more than as we start from 0
347 rowofcell.resize(numberofcells);
348 columnofcell.resize(numberofcells);
350 for (int row = 0, column = 0, c = 0;
351 c < numberofcells && row < rows_ && column < columns_;) {
353 columnofcell[c] = column;
357 } while (column < columns_ &&
358 cell_info[row][column].multicolumn
359 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
360 if (column == columns_) {
366 for (int row = 0; row < rows_; ++row) {
367 for (int column = 0; column < columns_; ++column) {
368 if (IsPartOfMultiColumn(row,column))
370 // now set the right line of multicolumns right for oldformat read
372 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
374 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
375 cell_info[row][column].right_line =
376 cell_info[row][column+cn-1].right_line;
378 cell_info[row][column].inset.SetAutoBreakRows(
379 !GetPWidth(GetCellNumber(row, column)).empty());
385 int LyXTabular::GetNumberOfCells() const
387 return numberofcells;
391 int LyXTabular::NumberOfCellsInRow(int cell) const
393 int const row = row_of_cell(cell);
395 for (int i = 0; i < columns_; ++i) {
396 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
403 /* returns 1 if there is a topline, returns 0 if not */
404 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
406 int const row = row_of_cell(cell);
408 if (!onlycolumn && IsMultiColumn(cell))
409 return cellinfo_of_cell(cell)->top_line;
410 return row_info[row].top_line;
414 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
416 // no bottom line underneath non-existent cells if you please
417 // Isn't that a programming error? Is so this should
418 // be an Assert instead. (Lgb)
419 if (cell >= numberofcells)
422 if (!onlycolumn && IsMultiColumn(cell))
423 return cellinfo_of_cell(cell)->bottom_line;
424 return row_info[row_of_cell(cell)].bottom_line;
428 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
430 if (!onlycolumn && IsMultiColumn(cell))
431 return cellinfo_of_cell(cell)->left_line;
432 return column_info[column_of_cell(cell)].left_line;
436 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
438 if (!onlycolumn && IsMultiColumn(cell))
439 return cellinfo_of_cell(cell)->right_line;
440 return column_info[right_column_of_cell(cell)].right_line;
444 bool LyXTabular::TopAlreadyDrawed(int cell) const
446 int row = row_of_cell(cell);
447 if ((row > 0) && !GetAdditionalHeight(row)) {
448 int column = column_of_cell(cell);
451 && cell_info[row][column].multicolumn
452 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
454 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
455 return row_info[row].bottom_line;
457 return cell_info[row][column].bottom_line;
463 bool LyXTabular::LeftAlreadyDrawed(int cell) const
465 int column = column_of_cell(cell);
467 int row = row_of_cell(cell);
469 (cell_info[row][column].multicolumn ==
470 LyXTabular::CELL_PART_OF_MULTICOLUMN));
471 if (GetAdditionalWidth(cell_info[row][column].cellno))
473 return column_info[column].right_line;
479 bool LyXTabular::IsLastRow(int cell) const
481 return (row_of_cell(cell) == rows_ - 1);
485 int LyXTabular::GetAdditionalHeight(int row) const
487 if (!row || row >= rows_)
493 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
494 switch (cell_info[row - 1][column].multicolumn) {
495 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
496 bottom = cell_info[row - 1][column].bottom_line;
498 case LyXTabular::CELL_NORMAL:
499 bottom = row_info[row - 1].bottom_line;
502 for (int column = 0; column < columns_ - 1 && top; ++column) {
503 switch (cell_info[row][column].multicolumn){
504 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
505 top = cell_info[row][column].top_line;
507 case LyXTabular::CELL_NORMAL:
508 top = row_info[row].top_line;
512 return WIDTH_OF_LINE;
517 int LyXTabular::GetAdditionalWidth(int cell) const
519 // internally already set in SetWidthOfCell
520 // used to get it back in text.C
521 int const col = right_column_of_cell(cell);
522 if (col < columns_ - 1 && column_info[col].right_line &&
523 column_info[col+1].left_line)
524 return WIDTH_OF_LINE;
530 // returns the maximum over all rows
531 int LyXTabular::GetWidthOfColumn(int cell) const
533 int const column1 = column_of_cell(cell);
534 int const column2 = right_column_of_cell(cell);
536 for (int i = column1; i <= column2; ++i) {
537 result += column_info[i].width_of_column;
543 int LyXTabular::GetWidthOfTabular() const
545 return width_of_tabular;
549 /* returns 1 if a complete update is necessary, otherwise 0 */
550 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
552 if (!IsMultiColumn(cell))
555 int const row = row_of_cell(cell);
556 int const column1 = column_of_cell(cell);
557 int const column2 = right_column_of_cell(cell);
559 // first set columns to 0 so we can calculate the right width
560 for (int i = column1; i <= column2; ++i) {
561 cell_info[row][i].width_of_cell = 0;
563 // set the width to MAX_WIDTH until width > 0
564 int width = (new_width + 2 * WIDTH_OF_LINE);
567 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
568 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
569 width -= column_info[i].width_of_column;
572 cell_info[row][i].width_of_cell = width;
578 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
580 int const row = row_of_cell(cell);
581 int const column1 = column_of_cell(cell);
582 int const column2 = right_column_of_cell(cell);
584 // first set columns to 0 so we can calculate the right width
586 for (; i <= column2; ++i)
587 cell_info[row][i].width_of_cell = 0;
588 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
590 if (i < numberofcells)
591 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
592 SetWidthOfMulticolCell(cell, new_width);
596 /* returns 1 if a complete update is necessary, otherwise 0 */
597 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
599 int const row = row_of_cell(cell);
600 int const column1 = column_of_cell(cell);
604 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
606 if (IsMultiColumn(cell, true)) {
607 tmp = SetWidthOfMulticolCell(cell, new_width);
609 width = (new_width + 2*WIDTH_OF_LINE);
610 cell_info[row][column1].width_of_cell = width;
611 if (column_info[column1].right_line && (column1 < columns_-1) &&
612 column_info[column1+1].left_line) // additional width
613 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
614 tmp = calculate_width_of_column_NMC(column1);
618 for (; i<columns_; ++i)
619 calculate_width_of_column_NMC(i);
620 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
622 if (i < numberofcells)
623 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
624 for (i = 0; i < columns_; ++i)
625 calculate_width_of_column(i);
626 calculate_width_of_tabular();
633 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
635 if (!IsMultiColumn(cell) || onlycolumn)
636 column_info[column_of_cell(cell)].alignment = align;
638 cellinfo_of_cell(cell)->alignment = align;
643 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
645 if (!IsMultiColumn(cell) || onlycolumn)
646 column_info[column_of_cell(cell)].valignment = align;
648 cellinfo_of_cell(cell)->valignment = align;
653 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
655 bool flag = !width.empty();
656 int const j = column_of_cell(cell);
658 column_info[j].p_width = width;
659 if (flag) // do this only if there is a width
660 SetAlignment(cell, LYX_ALIGN_LEFT);
661 for (int i = 0; i < rows_; ++i) {
662 int c = GetCellNumber(i, j);
663 flag = !GetPWidth(c).empty(); // because of multicolumns!
664 GetCellInset(c)->SetAutoBreakRows(flag);
670 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
672 bool const flag = !width.empty();
674 cellinfo_of_cell(cell)->p_width = width;
675 if (IsMultiColumn(cell)) {
676 GetCellInset(cell)->SetAutoBreakRows(flag);
683 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
684 LyXTabular::Feature what)
686 if (what == SET_SPECIAL_MULTI)
687 cellinfo_of_cell(cell)->align_special = special;
689 column_info[column_of_cell(cell)].align_special = special;
694 bool LyXTabular::SetAllLines(int cell, bool line)
696 SetTopLine(cell, line);
697 SetBottomLine(cell, line);
698 SetRightLine(cell, line);
699 SetLeftLine(cell, line);
704 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
706 int const row = row_of_cell(cell);
708 if (onlycolumn || !IsMultiColumn(cell))
709 row_info[row].top_line = line;
711 cellinfo_of_cell(cell)->top_line = line;
716 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
718 if (onlycolumn || !IsMultiColumn(cell))
719 row_info[row_of_cell(cell)].bottom_line = line;
721 cellinfo_of_cell(cell)->bottom_line = line;
726 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
728 if (onlycolumn || !IsMultiColumn(cell))
729 column_info[column_of_cell(cell)].left_line = line;
731 cellinfo_of_cell(cell)->left_line = line;
736 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
738 if (onlycolumn || !IsMultiColumn(cell))
739 column_info[right_column_of_cell(cell)].right_line = line;
741 cellinfo_of_cell(cell)->right_line = line;
746 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
748 if (!onlycolumn && IsMultiColumn(cell))
749 return cellinfo_of_cell(cell)->alignment;
751 return column_info[column_of_cell(cell)].alignment;
755 LyXTabular::VAlignment
756 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
758 if (!onlycolumn && IsMultiColumn(cell))
759 return cellinfo_of_cell(cell)->valignment;
761 return column_info[column_of_cell(cell)].valignment;
765 string const LyXTabular::GetPWidth(int cell) const
767 if (IsMultiColumn(cell))
768 return cellinfo_of_cell(cell)->p_width;
769 return column_info[column_of_cell(cell)].p_width;
773 string const LyXTabular::GetColumnPWidth(int cell) const
775 return column_info[column_of_cell(cell)].p_width;
779 string const LyXTabular::GetMColumnPWidth(int cell) const
781 if (IsMultiColumn(cell))
782 return cellinfo_of_cell(cell)->p_width;
787 string const LyXTabular::GetAlignSpecial(int cell, int what) const
789 if (what == SET_SPECIAL_MULTI)
790 return cellinfo_of_cell(cell)->align_special;
791 return column_info[column_of_cell(cell)].align_special;
795 int LyXTabular::GetWidthOfCell(int cell) const
797 int const row = row_of_cell(cell);
798 int const column1 = column_of_cell(cell);
799 int const column2 = right_column_of_cell(cell);
801 for (int i = column1; i <= column2; ++i) {
802 result += cell_info[row][i].width_of_cell;
808 int LyXTabular::GetBeginningOfTextInCell(int cell) const
812 switch (GetAlignment(cell)){
813 case LYX_ALIGN_CENTER:
814 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
816 case LYX_ALIGN_RIGHT:
817 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
818 // + GetAdditionalWidth(cell);
820 default: /* LYX_ALIGN_LEFT: nothing :-) */
830 bool LyXTabular::IsFirstCellInRow(int cell) const
832 return column_of_cell(cell) == 0;
836 int LyXTabular::GetFirstCellInRow(int row) const
840 return cell_info[row][0].cellno;
843 bool LyXTabular::IsLastCellInRow(int cell) const
845 return (right_column_of_cell(cell) == (columns_ - 1));
849 int LyXTabular::GetLastCellInRow(int row) const
853 return cell_info[row][columns_-1].cellno;
857 bool LyXTabular::calculate_width_of_column(int column)
859 int const old_column_width = column_info[column].width_of_column;
862 for (int i = 0; i < rows_; ++i) {
863 maximum = max(cell_info[i][column].width_of_cell, maximum);
865 column_info[column].width_of_column = maximum;
866 return (column_info[column].width_of_column != old_column_width);
871 // calculate the with of the column without regarding REAL MultiColumn
872 // cells. This means MultiColumn-cells spanning more than 1 column.
874 bool LyXTabular::calculate_width_of_column_NMC(int column)
876 int const old_column_width = column_info[column].width_of_column;
878 for (int i = 0; i < rows_; ++i) {
879 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
880 (cell_info[i][column].width_of_cell > max)) {
881 max = cell_info[i][column].width_of_cell;
884 column_info[column].width_of_column = max;
885 return (column_info[column].width_of_column != old_column_width);
889 void LyXTabular::calculate_width_of_tabular()
891 width_of_tabular = 0;
892 for (int i = 0; i < columns_; ++i) {
893 width_of_tabular += column_info[i].width_of_column;
898 int LyXTabular::row_of_cell(int cell) const
900 if (cell >= numberofcells)
904 return rowofcell[cell];
908 int LyXTabular::column_of_cell(int cell) const
910 if (cell >= numberofcells)
914 return columnofcell[cell];
918 int LyXTabular::right_column_of_cell(int cell) const
920 int const row = row_of_cell(cell);
921 int column = column_of_cell(cell);
922 while (column < (columns_ - 1) &&
923 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
929 // Perfect case for a template... (Lgb)
932 string const write_attribute(string const & name, T const & t)
934 string str = " " + name + "=\"" + tostr(t) + "\"";
939 string const write_attribute(string const & name, bool const & b)
941 return write_attribute(name, int(b));
946 string const write_attribute(string const & name, int value)
948 string str = " " + name + "=\"" + tostr(value) + "\"";
953 string const write_attribute(string const & name, string const & value)
955 string str = " " + name + "=\"" + value + "\"";
960 string const write_attribute(string const & name, bool value)
962 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
970 string const tostr(LyXAlignment const & num)
975 case LYX_ALIGN_BLOCK:
979 case LYX_ALIGN_CENTER:
981 case LYX_ALIGN_RIGHT:
983 case LYX_ALIGN_LAYOUT:
985 case LYX_ALIGN_SPECIAL:
994 string const tostr(LyXTabular::VAlignment const & num)
997 case LyXTabular::LYX_VALIGN_TOP:
999 case LyXTabular::LYX_VALIGN_CENTER:
1001 case LyXTabular::LYX_VALIGN_BOTTOM:
1010 string const tostr(LyXTabular::BoxType const & num)
1013 case LyXTabular::BOX_NONE:
1015 case LyXTabular::BOX_PARBOX:
1017 case LyXTabular::BOX_MINIPAGE:
1024 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1028 << write_attribute("version", 2)
1029 << write_attribute("rows", rows_)
1030 << write_attribute("columns", columns_)
1032 // global longtable options
1034 << write_attribute("rotate", tostr(rotate))
1035 << write_attribute("islongtable", tostr(is_long_tabular))
1036 << write_attribute("endhead", endhead)
1037 << write_attribute("endfirsthead", endfirsthead)
1038 << write_attribute("endfoot", endfoot)
1039 << write_attribute("endlastfoot", endlastfoot)
1041 for (int j = 0; j < columns_; ++j) {
1043 << write_attribute("alignment", tostr(column_info[j].alignment))
1044 << write_attribute("valignment", tostr(column_info[j].valignment))
1045 << write_attribute("leftline", tostr(column_info[j].left_line))
1046 << write_attribute("rightline", tostr(column_info[j].right_line))
1047 << write_attribute("width",
1048 VSpace(column_info[j].p_width)
1050 << write_attribute("special", column_info[j].align_special)
1053 for (int i = 0; i < rows_; ++i) {
1055 << write_attribute("topline", tostr(row_info[i].top_line))
1056 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1057 << write_attribute("newpage", tostr(row_info[i].newpage))
1059 for (int j = 0; j < columns_; ++j) {
1061 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1062 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1063 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1064 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1065 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1066 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1067 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1068 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1069 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1070 << write_attribute("width", cell_info[i][j].p_width)
1071 << write_attribute("special", cell_info[i][j].align_special)
1073 os << "\\begin_inset ";
1074 cell_info[i][j].inset.Write(buf, os);
1075 os << "\n\\end_inset \n"
1080 os << "</lyxtabular>\n";
1086 // I would have liked a fromstr template a lot better. (Lgb)
1089 bool string2type(string const str, LyXAlignment & num)
1092 num = LYX_ALIGN_NONE;
1093 else if (str == "block")
1094 num = LYX_ALIGN_BLOCK;
1095 else if (str == "left")
1096 num = LYX_ALIGN_LEFT;
1097 else if (str == "center")
1098 num = LYX_ALIGN_CENTER;
1099 else if (str == "right")
1100 num = LYX_ALIGN_RIGHT;
1108 bool string2type(string const str, LyXTabular::VAlignment & num)
1111 num = LyXTabular::LYX_VALIGN_TOP;
1112 else if (str == "center")
1113 num = LyXTabular::LYX_VALIGN_CENTER;
1114 else if (str == "bottom")
1115 num = LyXTabular::LYX_VALIGN_BOTTOM;
1123 bool string2type(string const str, LyXTabular::BoxType & num)
1126 num = LyXTabular::BOX_NONE;
1127 else if (str == "parbox")
1128 num = LyXTabular::BOX_PARBOX;
1129 else if (str == "minipage")
1130 num = LyXTabular::BOX_MINIPAGE;
1138 bool string2type(string const str, bool & num)
1142 else if (str == "false")
1150 bool getTokenValue(string const & str, const char * token, string & ret)
1152 size_t token_length = std::strlen(token);
1153 string::size_type pos = str.find(token);
1155 if (pos == string::npos || pos + token_length + 1 >= str.length()
1156 || str[pos + token_length] != '=')
1159 pos += token_length + 1;
1161 if ((ch != '"') && (ch != '\'')) { // only read till next space
1165 while((pos < str.length() - 1) && (str[++pos] != ch))
1172 bool getTokenValue(string const & str, const char * token, int & num)
1175 if (!getTokenValue(str, token, tmp))
1177 num = strToInt(tmp);
1182 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1185 if (!getTokenValue(str, token, tmp))
1187 return string2type(tmp, num);
1191 bool getTokenValue(string const & str, const char * token,
1192 LyXTabular::VAlignment & num)
1195 if (!getTokenValue(str, token, tmp))
1197 return string2type(tmp, num);
1201 bool getTokenValue(string const & str, const char * token,
1202 LyXTabular::BoxType & num)
1205 if (!getTokenValue(str, token, tmp))
1207 return string2type(tmp, num);
1211 bool getTokenValue(string const & str, const char * token, bool & flag)
1214 if (!getTokenValue(str, token, tmp))
1216 return string2type(tmp, flag);
1221 void l_getline(istream & is, string & str)
1224 while (str.empty()) {
1226 if (!str.empty() && str[str.length() - 1] == '\r')
1227 str.erase(str.length() - 1);
1234 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1237 istream & is = lex.getStream();
1239 l_getline(is, line);
1240 if (!prefixIs(line, "<lyxtabular ")
1241 && !prefixIs(line, "<LyXTabular ")) {
1242 OldFormatRead(lex, line);
1247 if (!getTokenValue(line, "version", version))
1250 ReadOld(buf, is, lex, line);
1251 else if (version == 2)
1252 ReadNew(buf, is, lex, line);
1256 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1257 LyXLex & lex, string const & l)
1261 if (!getTokenValue(line, "rows", rows_arg))
1264 if (!getTokenValue(line, "columns", columns_arg))
1266 Init(rows_arg, columns_arg);
1267 l_getline(is, line);
1268 if (!prefixIs(line, "<features")) {
1269 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1270 line << ")" << endl;
1273 getTokenValue(line, "rotate", rotate);
1274 getTokenValue(line, "islongtable", is_long_tabular);
1275 getTokenValue(line, "endhead", endhead);
1276 getTokenValue(line, "endfirsthead", endfirsthead);
1277 getTokenValue(line, "endfoot", endfoot);
1278 getTokenValue(line, "endlastfoot", endlastfoot);
1280 for (int j = 0; j < columns_; ++j) {
1282 if (!prefixIs(line,"<column")) {
1283 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1284 line << ")" << endl;
1287 getTokenValue(line, "alignment", column_info[j].alignment);
1288 getTokenValue(line, "valignment", column_info[j].valignment);
1289 getTokenValue(line, "leftline", column_info[j].left_line);
1290 getTokenValue(line, "rightline", column_info[j].right_line);
1291 getTokenValue(line, "width", column_info[j].p_width);
1292 getTokenValue(line, "special", column_info[j].align_special);
1295 for (int i = 0; i < rows_; ++i) {
1296 l_getline(is, line);
1297 if (!prefixIs(line, "<row")) {
1298 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1299 line << ")" << endl;
1302 getTokenValue(line, "topline", row_info[i].top_line);
1303 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1304 getTokenValue(line, "newpage", row_info[i].newpage);
1305 for (int j = 0; j < columns_; ++j) {
1306 l_getline(is, line);
1307 if (!prefixIs(line, "<cell")) {
1308 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1309 line << ")" << endl;
1312 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1313 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1314 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1315 getTokenValue(line, "topline", cell_info[i][j].top_line);
1316 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1317 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1318 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1319 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1320 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1321 getTokenValue(line, "width", cell_info[i][j].p_width);
1322 getTokenValue(line, "special", cell_info[i][j].align_special);
1323 l_getline(is, line);
1324 if (prefixIs(line, "\\begin_inset")) {
1325 cell_info[i][j].inset.Read(buf, lex);
1326 l_getline(is, line);
1328 if (!prefixIs(line, "</cell>")) {
1329 lyxerr << "Wrong tabular format (expected </cell> got" <<
1330 line << ")" << endl;
1334 l_getline(is, line);
1335 if (!prefixIs(line, "</row>")) {
1336 lyxerr << "Wrong tabular format (expected </row> got" <<
1337 line << ")" << endl;
1341 while (!prefixIs(line, "</lyxtabular>")) {
1342 l_getline(is, line);
1344 set_row_column_number_info();
1348 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1354 int columns_arg = 0;
1355 int is_long_tabular_arg = false;
1356 int rotate_arg = false;
1365 istream & is = lex.getStream();
1368 version = lyx::atoi(s.substr(8, string::npos));
1372 vector<int> cont_row_info;
1375 lyxerr << "Tabular format < 5 is not supported anymore\n"
1376 "Get an older version of LyX (< 1.1.x) for conversion!"
1378 WriteAlert(_("Warning:"),
1379 _("Tabular format < 5 is not supported anymore\n"),
1380 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1382 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1383 >> rotate_arg >> a >> b >> c >> d;
1385 is >> rows_arg >> columns_arg;
1386 Init(rows_arg, columns_arg);
1387 cont_row_info = vector<int>(rows_arg);
1388 SetLongTabular(is_long_tabular_arg);
1389 SetRotateTabular(rotate_arg);
1391 for (i = 0; i < rows_; ++i) {
1393 cont_row_info[i] = false;
1395 for (i = 0; i < columns_; ++i) {
1398 for (i = 0; i < rows_; ++i) {
1399 for (j = 0; j < columns_; ++j) {
1404 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1405 >> rotate_arg >> a >> b >> c >> d;
1406 Init(rows_arg, columns_arg);
1407 cont_row_info = vector<int>(rows_arg);
1408 SetLongTabular(is_long_tabular_arg);
1409 SetRotateTabular(rotate_arg);
1411 endfirsthead = b + 1;
1413 endlastfoot = d + 1;
1414 for (i = 0; i < rows_; ++i) {
1415 a = b = c = d = e = f = g = 0;
1416 is >> a >> b >> c >> d;
1417 row_info[i].top_line = a;
1418 row_info[i].bottom_line = b;
1419 cont_row_info[i] = c;
1420 row_info[i].newpage = d;
1422 for (i = 0; i < columns_; ++i) {
1427 char ch; // skip '"'
1430 // ignore is buggy but we will use it later (Lgb)
1431 is.ignore(); // skip '"'
1433 getline(is, s1, '"');
1435 is >> ch; // skip '"'
1437 // ignore is buggy but we will use it later (Lgb)
1438 is.ignore(); // skip '"'
1440 getline(is, s2, '"');
1441 column_info[i].alignment = static_cast<LyXAlignment>(a);
1442 column_info[i].left_line = b;
1443 column_info[i].right_line = c;
1444 column_info[i].p_width = s1;
1445 column_info[i].align_special = s2;
1447 for (i = 0; i < rows_; ++i) {
1448 for (j = 0; j < columns_; ++j) {
1451 is >> a >> b >> c >> d >> e >> f >> g;
1454 is >> ch; // skip '"'
1456 // ignore is buggy but we will use it later (Lgb)
1457 is.ignore(); // skip '"'
1459 getline(is, s1, '"');
1461 is >> ch; // skip '"'
1463 // ignore is buggy but we will use it later (Lgb)
1464 is.ignore(); // skip '"'
1466 getline(is, s2, '"');
1467 cell_info[i][j].multicolumn = static_cast<char>(a);
1468 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1469 cell_info[i][j].top_line = static_cast<char>(c);
1470 cell_info[i][j].bottom_line = static_cast<char>(d);
1471 cell_info[i][j].left_line = column_info[j].left_line;
1472 cell_info[i][j].right_line = column_info[j].right_line;
1473 cell_info[i][j].rotate = static_cast<bool>(f);
1474 cell_info[i][j].usebox = static_cast<BoxType>(g);
1475 cell_info[i][j].align_special = s1;
1476 cell_info[i][j].p_width = s2;
1480 set_row_column_number_info(true);
1482 LyXParagraph * par = new LyXParagraph;
1483 LyXParagraph * return_par = 0;
1488 LyXFont font(LyXFont::ALL_INHERIT);
1489 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1491 while (lex.IsOK()) {
1493 string const token = lex.GetString();
1496 if (token == "\\layout"
1497 || token == "\\end_float"
1498 || token == "\\end_deeper") {
1499 lex.pushToken(token);
1502 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1507 lex.pushToken(token);
1511 lex.printError("New Paragraph allocated! This should not happen!");
1512 lex.pushToken(token);
1518 // now we have the par we should fill the insets with this!
1520 InsetText * inset = GetCellInset(cell);
1523 for (int i = 0; i < par->size(); ++i) {
1524 if (par->IsNewline(i)) {
1526 if (cell > GetNumberOfCells()) {
1527 lyxerr << "Some error in reading old table format occured!" <<
1528 endl << "Terminating when reading cell[" << cell << "]!" <<
1533 row = row_of_cell(cell);
1534 if (cont_row_info[row]) {
1536 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1537 while(!IsFirstCellInRow(--cell));
1539 inset = GetCellInset(cell);
1542 inset = GetCellInset(cell);
1543 row = row_of_cell(cell);
1544 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1546 // insert a space instead
1548 par->InsertChar(i, ' ');
1551 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1552 inset->par->InsertFromMinibuffer(inset->par->size());
1559 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1561 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1562 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1566 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1568 int const row = row_of_cell(cell);
1569 int const column = column_of_cell(cell);
1570 return &cell_info[row][column];
1574 void LyXTabular::SetMultiColumn(int cell, int number)
1576 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1577 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1578 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1579 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1580 for (number--; number > 0; --number) {
1581 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1583 set_row_column_number_info();
1587 int LyXTabular::cells_in_multicolumn(int cell) const
1589 int const row = row_of_cell(cell);
1590 int column = column_of_cell(cell);
1593 while ((column < columns_) &&
1594 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1603 int LyXTabular::UnsetMultiColumn(int cell)
1605 int const row = row_of_cell(cell);
1606 int column = column_of_cell(cell);
1610 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1611 cell_info[row][column].multicolumn = CELL_NORMAL;
1613 while ((column < columns_) &&
1614 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1616 cell_info[row][column].multicolumn = CELL_NORMAL;
1621 set_row_column_number_info();
1626 void LyXTabular::SetLongTabular(bool what)
1628 is_long_tabular = what;
1632 bool LyXTabular::IsLongTabular() const
1634 return is_long_tabular;
1638 void LyXTabular::SetRotateTabular(bool flag)
1644 bool LyXTabular::GetRotateTabular() const
1650 void LyXTabular::SetRotateCell(int cell, bool flag)
1652 cellinfo_of_cell(cell)->rotate = flag;
1656 bool LyXTabular::GetRotateCell(int cell) const
1658 return cellinfo_of_cell(cell)->rotate;
1662 bool LyXTabular::NeedRotating() const
1666 for (int i = 0; i < rows_; ++i) {
1667 for (int j = 0; j < columns_; ++j) {
1668 if (cell_info[i][j].rotate)
1676 bool LyXTabular::IsLastCell(int cell) const
1678 if ((cell + 1) < GetNumberOfCells())
1684 int LyXTabular::GetCellAbove(int cell) const
1686 if (row_of_cell(cell) > 0)
1687 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1692 int LyXTabular::GetCellBelow(int cell) const
1694 if (row_of_cell(cell) + 1 < rows_)
1695 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1700 int LyXTabular::GetLastCellAbove(int cell) const
1702 if (row_of_cell(cell) <= 0)
1704 if (!IsMultiColumn(cell))
1705 return GetCellAbove(cell);
1706 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1710 int LyXTabular::GetLastCellBelow(int cell) const
1712 if (row_of_cell(cell) + 1 >= rows_)
1714 if (!IsMultiColumn(cell))
1715 return GetCellBelow(cell);
1716 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1720 int LyXTabular::GetCellNumber(int row, int column) const
1722 if (column >= columns_)
1723 column = columns_ - 1;
1724 else if (column < 0)
1731 return cell_info[row][column].cellno;
1735 void LyXTabular::SetUsebox(int cell, BoxType type)
1737 cellinfo_of_cell(cell)->usebox = type;
1741 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1743 if (column_info[column_of_cell(cell)].p_width.empty() &&
1744 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1746 if (cellinfo_of_cell(cell)->usebox > 1)
1747 return cellinfo_of_cell(cell)->usebox;
1748 return UseParbox(cell);
1752 void LyXTabular::SetLTHead(int cell, bool first)
1754 int const row = row_of_cell(cell);
1755 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1758 if (endfirsthead == val)
1771 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1774 if (abs(endhead) > rows_)
1776 return (row_of_cell(cell) == abs(endhead) - 1);
1780 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1783 if (abs(endfirsthead) > rows_)
1785 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1789 void LyXTabular::SetLTFoot(int cell, bool last)
1791 int const row = row_of_cell(cell);
1792 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1795 if (endlastfoot == val)
1808 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1811 if ((endfoot + 1) > rows_)
1813 return (row_of_cell(cell) == abs(endfoot) - 1);
1817 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1820 if (abs(endlastfoot) > rows_)
1822 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1826 void LyXTabular::SetLTNewPage(int cell, bool what)
1828 row_info[row_of_cell(cell)].newpage = what;
1832 bool LyXTabular::GetLTNewPage(int cell) const
1834 return row_info[row_of_cell(cell)].newpage;
1838 bool LyXTabular::SetAscentOfRow(int row, int height)
1840 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1842 row_info[row].ascent_of_row = height;
1847 bool LyXTabular::SetDescentOfRow(int row, int height)
1849 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1851 row_info[row].descent_of_row = height;
1856 int LyXTabular::GetAscentOfRow(int row) const
1860 return row_info[row].ascent_of_row;
1864 int LyXTabular::GetDescentOfRow(int row) const
1868 return row_info[row].descent_of_row;
1872 int LyXTabular::GetHeightOfTabular() const
1876 for (int row = 0; row < rows_; ++row)
1877 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1878 GetAdditionalHeight(row);
1883 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1885 if ((row >= rows_) || (column >= columns_))
1887 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1891 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1893 if ((row < 0) || (row >= rows_))
1896 int const fcell = GetFirstCellInRow(row);
1897 int const n = NumberOfCellsInRow(fcell) + fcell;
1900 for (int i = fcell; i < n; ++i) {
1904 if (tmp == (n - fcell)){
1907 for (int i = fcell; i < n; ++i) {
1910 << column_of_cell(i) + 1
1912 << right_column_of_cell(i) + 1
1924 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1926 if ((row < 0) || (row >= rows_))
1929 int const fcell = GetFirstCellInRow(row);
1930 int const n = NumberOfCellsInRow(fcell) + fcell;
1933 for (int i = fcell; i < n; ++i) {
1937 if (tmp == (n-fcell)){
1940 for (int i = fcell; i < n; ++i) {
1941 if (BottomLine(i)) {
1943 << column_of_cell(i) + 1
1945 << right_column_of_cell(i) + 1
1957 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1961 if (GetRotateCell(cell)) {
1962 os << "\\begin{sideways}\n";
1965 if (IsMultiColumn(cell)) {
1966 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1967 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1968 os << cellinfo_of_cell(cell)->align_special << "}{";
1970 if (LeftLine(cell) &&
1971 (IsFirstCellInRow(cell) ||
1972 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1973 !RightLine(cell-1, true))))
1977 if (!GetPWidth(cell).empty()) {
1978 switch (GetVAlignment(cell)) {
1979 case LYX_VALIGN_TOP:
1982 case LYX_VALIGN_CENTER:
1985 case LYX_VALIGN_BOTTOM:
1989 os << "{" << GetPWidth(cell) << '}';
1991 switch (GetAlignment(cell)) {
1992 case LYX_ALIGN_LEFT:
1995 case LYX_ALIGN_RIGHT:
2003 if (RightLine(cell))
2005 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2011 if (GetUsebox(cell) == BOX_PARBOX) {
2013 switch (GetVAlignment(cell)) {
2014 case LYX_VALIGN_TOP:
2017 case LYX_VALIGN_CENTER:
2020 case LYX_VALIGN_BOTTOM:
2024 os << "]{" << GetPWidth(cell) << "}{";
2025 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2026 os << "\\begin{minipage}[";
2027 switch (GetVAlignment(cell)) {
2028 case LYX_VALIGN_TOP:
2031 case LYX_VALIGN_CENTER:
2034 case LYX_VALIGN_BOTTOM:
2038 os << "]{" << GetPWidth(cell) << "}\n";
2045 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2050 if (GetUsebox(cell) == BOX_PARBOX)
2052 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2053 os << "%\n\\end{minipage}";
2056 if (IsMultiColumn(cell)){
2059 if (GetRotateCell(cell)) {
2060 os << "%\n\\end{sideways}";
2067 int LyXTabular::Latex(Buffer const * buf,
2068 ostream & os, bool fragile, bool fp) const
2073 //+---------------------------------------------------------------------
2074 //+ first the opening preamble +
2075 //+---------------------------------------------------------------------
2078 os << "\\begin{sideways}\n";
2081 if (is_long_tabular)
2082 os << "\\begin{longtable}{";
2084 os << "\\begin{tabular}{";
2085 for (int i = 0; i < columns_; ++i) {
2086 if (column_info[i].left_line)
2088 if (!column_info[i].align_special.empty()) {
2089 os << column_info[i].align_special;
2090 } else if (!column_info[i].p_width.empty()) {
2091 switch (column_info[i].valignment) {
2092 case LYX_VALIGN_TOP:
2095 case LYX_VALIGN_CENTER:
2098 case LYX_VALIGN_BOTTOM:
2103 << column_info[i].p_width
2106 switch (column_info[i].alignment) {
2107 case LYX_ALIGN_LEFT:
2110 case LYX_ALIGN_RIGHT:
2118 if (column_info[i].right_line)
2124 //+---------------------------------------------------------------------
2125 //+ the single row and columns (cells) +
2126 //+---------------------------------------------------------------------
2128 for (int i = 0; i < rows_; ++i) {
2129 ret += TeXTopHLine(os, i);
2131 if (IsLongTabular()) {
2132 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2133 os << "\\endhead\n";
2136 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2137 os << "\\endfirsthead\n";
2140 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2141 os << "\\endfoot\n";
2144 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2145 os << "\\endlastfoot\n";
2150 ret += TeXBottomHLine(os, i-1);
2151 ret += TeXTopHLine(os, i);
2153 for (int j = 0; j < columns_; ++j) {
2154 if (IsPartOfMultiColumn(i,j))
2156 ret += TeXCellPreamble(os, cell);
2157 InsetText * inset = GetCellInset(cell);
2159 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2160 inset->par->size() > 0 && GetPWidth(cell).empty();
2164 ret += inset->Latex(buf, os, fragile, fp);
2168 ret += TeXCellPostamble(os, cell);
2169 if (!IsLastCellInRow(cell)) { // not last cell in row
2176 ret += TeXBottomHLine(os, i);
2178 if (IsLongTabular()) {
2179 if ((endhead > 0) && (i == (endhead - 1))) {
2180 os << "\\endhead\n";
2183 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2184 os << "\\endfirsthead\n";
2187 if ((endfoot > 0) && (i == (endfoot - 1))) {
2188 os << "\\endfoot\n";
2191 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2192 os << "\\endlastfoot\n";
2196 // ret += TeXBottomHLine(os, i);
2197 if (row_info[i].newpage) {
2198 os << "\\newpage\n";
2204 //+---------------------------------------------------------------------
2205 //+ the closing of the tabular +
2206 //+---------------------------------------------------------------------
2208 if (is_long_tabular)
2209 os << "\\end{longtable}";
2211 os << "\\end{tabular}";
2213 os << "\n\\end{sideways}";
2221 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2225 //+---------------------------------------------------------------------
2226 //+ first the opening preamble +
2227 //+---------------------------------------------------------------------
2229 os << "<tgroup cols=\"" << columns_
2230 << "\" colsep=\"1\" rowsep=\"1\">\n";
2232 for (int i = 0; i < columns_; ++i) {
2233 os << "<colspec colname=\"col" << i << "\" align=\"";
2234 switch (column_info[i].alignment) {
2235 case LYX_ALIGN_LEFT:
2238 case LYX_ALIGN_RIGHT:
2249 //+---------------------------------------------------------------------
2250 //+ the single row and columns (cells) +
2251 //+---------------------------------------------------------------------
2255 for (int i = 0; i < rows_; ++i) {
2257 for (int j = 0; j < columns_; ++j) {
2258 if (IsPartOfMultiColumn(i, j))
2261 os << "<entry align=\"";
2262 switch (GetAlignment(cell)) {
2263 case LYX_ALIGN_LEFT:
2266 case LYX_ALIGN_RIGHT:
2274 os << "\" valign=\"";
2275 switch (GetVAlignment(cell)) {
2276 case LYX_VALIGN_TOP:
2279 case LYX_VALIGN_BOTTOM:
2282 case LYX_VALIGN_CENTER:
2287 if (IsMultiColumn(cell)) {
2288 os << " namest=\"col" << j << "\" ";
2289 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2293 ret += GetCellInset(cell)->DocBook(buf, os);
2300 //+---------------------------------------------------------------------
2301 //+ the closing of the tabular +
2302 //+---------------------------------------------------------------------
2314 void print_n_chars(ostream & os, unsigned char ch, int n)
2316 os << string(n, ch);
2322 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2323 vector<unsigned int> const & clen) const
2325 int const fcell = GetFirstCellInRow(row);
2326 int const n = NumberOfCellsInRow(fcell) + fcell;
2329 for (int i = fcell; i < n; ++i) {
2339 for (int i = fcell; i < n; ++i) {
2350 int column = column_of_cell(i);
2351 int len = clen[column];
2352 while(IsPartOfMultiColumn(row, ++column))
2353 len += clen[column] + 4;
2354 print_n_chars(os, ch, len);
2369 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2370 vector<unsigned int> const & clen) const
2372 int const fcell = GetFirstCellInRow(row);
2373 int const n = NumberOfCellsInRow(fcell) + fcell;
2376 for (int i = fcell; i < n; ++i) {
2377 if (BottomLine(i)) {
2386 for (int i = fcell; i < n; ++i) {
2387 if (BottomLine(i)) {
2397 int column = column_of_cell(i);
2398 int len = clen[column];
2399 while(IsPartOfMultiColumn(row, ++column))
2400 len += clen[column] + 4;
2401 print_n_chars(os, ch, len);
2402 if (BottomLine(i)) {
2416 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2417 int cell, int row, int column,
2418 vector<unsigned int> const & clen) const
2421 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2428 unsigned int len1 = sstr.str().length();
2429 unsigned int len2 = clen[column];
2430 while(IsPartOfMultiColumn(row, ++column))
2431 len2 += clen[column] + 4;
2434 switch (GetAlignment(cell)) {
2436 case LYX_ALIGN_LEFT:
2439 case LYX_ALIGN_RIGHT:
2443 case LYX_ALIGN_CENTER:
2449 for (unsigned int i = 0; i < len1; ++i)
2452 for (unsigned int i = 0; i < len2; ++i)
2454 if (RightLine(cell))
2463 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2467 //+---------------------------------------------------------------------
2468 //+ first calculate the width of the single columns +
2469 //+---------------------------------------------------------------------
2470 vector<unsigned int> clen(columns_);
2472 // first all non (real) multicolumn cells!
2473 for (int j = 0; j < columns_; ++j) {
2475 for (int i = 0; i < rows_; ++i) {
2476 int cell = GetCellNumber(i, j);
2477 if (IsMultiColumn(cell, true))
2480 GetCellInset(cell)->Ascii(buf, sstr, 0);
2481 if (clen[j] < sstr.str().length())
2482 clen[j] = sstr.str().length();
2485 // then all (real) multicolumn cells!
2486 for (int j = 0; j < columns_; ++j) {
2487 for (int i = 0; i < rows_; ++i) {
2488 int cell = GetCellNumber(i, j);
2489 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2492 GetCellInset(cell)->Ascii(buf, sstr, 0);
2493 int len = int(sstr.str().length());
2494 int const n = cells_in_multicolumn(cell);
2495 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2497 if (len > int(clen[j + n - 1]))
2498 clen[j + n - 1] = len;
2502 for (int i = 0; i < rows_; ++i) {
2503 AsciiTopHLine(os, i, clen);
2504 for (int j = 0; j < columns_; ++j) {
2505 if (IsPartOfMultiColumn(i,j))
2507 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2511 AsciiBottomHLine(os, i, clen);
2517 InsetText * LyXTabular::GetCellInset(int cell) const
2519 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2523 InsetText * LyXTabular::GetCellInset(int row, int column) const
2525 return & cell_info[row][column].inset;
2529 void LyXTabular::Validate(LaTeXFeatures & features) const
2531 if (IsLongTabular())
2532 features.longtable = true;
2534 features.rotating = true;
2535 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2536 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2537 features.array = true;
2538 GetCellInset(cell)->Validate(features);
2543 std::vector<string> const LyXTabular::getLabelList() const
2545 std::vector<string> label_list;
2546 for (int i = 0; i < rows_; ++i)
2547 for (int j = 0; j < columns_; ++j) {
2548 std::vector<string> const l =
2549 GetCellInset(i, j)->getLabelList();
2550 label_list.insert(label_list.end(),
2551 l.begin(), l.end());
2557 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2559 LyXParagraph * par = GetCellInset(cell)->par;
2561 for (; par; par = par->next()) {
2562 for (int i = 0; i < par->size(); ++i) {
2563 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)