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 "support/LAssert.h"
34 #include "insets/insettabular.h"
35 #include "insets/insettext.h"
45 #ifndef CXX_GLOBAL_CSTD
51 int const WIDTH_OF_LINE = 5;
55 /// Define a few methods for the inner structs
57 LyXTabular::cellstruct::cellstruct()
61 multicolumn = LyXTabular::CELL_NORMAL;
62 alignment = LYX_ALIGN_CENTER;
63 valignment = LYX_VALIGN_TOP;
73 LyXTabular::rowstruct::rowstruct()
83 LyXTabular::columnstruct::columnstruct()
87 alignment = LYX_ALIGN_CENTER;
88 valignment = LYX_VALIGN_TOP;
94 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
98 Init(rows_arg, columns_arg);
102 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
106 Init(lt.rows_, lt.columns_, <);
109 #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)
116 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
124 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
127 #warning This while method should look like this: (Lgb)
132 // If this and lt is not of the same size we have a serious bug
133 // So then it is ok to throw an exception, or for now
135 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
137 cell_info = lt.cell_info;
138 row_info = lt.row_info;
139 column_info = lt.column_info;
141 // long tabular stuff
142 SetLongTabular(lt.is_long_tabular);
143 endhead = lt.endhead;
144 endfoot = lt.endfoot;
145 endfirsthead = lt.endfirsthead;
146 endlastfoot = lt.endlastfoot;
156 LyXTabular * LyXTabular::clone(InsetTabular * inset)
158 LyXTabular * result = new LyXTabular(inset, *this);
160 // don't know if this is good but I need to Clone also
161 // the text-insets here, this is for the Undo-facility!
162 for (int i = 0; i < rows_; ++i) {
163 for (int j = 0; j < columns_; ++j) {
164 result->cell_info[i][j].inset = cell_info[i][j].inset;
165 result->cell_info[i][j].inset.setOwner(inset);
173 /* activates all lines and sets all widths to 0 */
174 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
177 columns_ = columns_arg;
178 row_info = row_vector(rows_, rowstruct());
179 column_info = column_vector(columns_, columnstruct());
180 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
188 for (int i = 0; i < rows_; ++i) {
189 for (int j = 0; j < columns_; ++j) {
190 cell_info[i][j].inset.setOwner(owner_);
191 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
192 cell_info[i][j].cellno = cellno++;
194 cell_info[i].back().right_line = true;
196 row_info.back().bottom_line = true;
197 row_info.front().bottom_line = true;
199 for (int i = 0; i < columns_; ++i) {
200 calculate_width_of_column(i);
202 column_info.back().right_line = true;
204 calculate_width_of_tabular();
206 rowofcell = vector<int>();
207 columnofcell = vector<int>();
208 set_row_column_number_info();
209 is_long_tabular = false;
218 void LyXTabular::AppendRow(int cell)
222 int row = row_of_cell(cell);
224 row_vector::iterator rit = row_info.begin() + row;
225 row_info.insert(rit, rowstruct());
226 // now set the values of the row before
227 row_info[row] = row_info[row+1];
230 cell_vvector::iterator cit = cell_info.begin() + row;
231 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
233 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
236 for (int i = 0; i <= row; ++i) {
237 for (int j = 0; j < columns_; ++j) {
238 c_info[i][j] = cell_info[i][j];
241 for (int i = row + 1; i < rows_; ++i) {
242 for (int j = 0; j < columns_; ++j) {
243 c_info[i][j] = cell_info[i-1][j];
248 for (int j = 0; j < columns_; ++j) {
249 cell_info[row][j].inset.clear();
256 void LyXTabular::DeleteRow(int row)
258 // Why make it so hard? (Lgb)
261 if (rows_ == 1) return; // Not allowed to delete last row
263 row_info.erase(row_info.begin() + row); //&row_info[row]);
264 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
270 void LyXTabular::AppendColumn(int cell)
274 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
276 int const column = column_of_cell(cell);
277 column_vector::iterator cit = column_info.begin() + column + 1;
278 column_info.insert(cit, columnstruct());
279 // set the column values of the column before
280 column_info[column+1] = column_info[column];
282 for (int i = 0; i < rows_; ++i) {
283 for (int j = 0; j <= column; ++j) {
284 c_info[i][j] = cell_info[i][j];
286 for (int j = column + 1; j < columns_; ++j) {
287 c_info[i][j] = cell_info[i][j - 1];
289 // care about multicolumns
290 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
291 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
293 if ((column + 1) == columns_ ||
294 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
295 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
300 for (int i = 0; i < rows_; ++i) {
301 //cell_info[i][column].inset.clear();
302 cell_info[i][column + 1].inset.clear();
308 void LyXTabular::DeleteColumn(int column)
310 // Similar to DeleteRow
311 //if (!(columns_ - 1))
313 if (columns_ == 1) return; // Not allowed to delete last column
315 column_info.erase(column_info.begin() + column);
316 for (int i = 0; i < rows_; ++i) {
317 cell_info[i].erase(cell_info[i].begin() + column);
324 void LyXTabular::Reinit()
326 for (int i = 0; i < rows_; ++i) {
327 for (int j = 0; j < columns_; ++j) {
328 cell_info[i][j].width_of_cell = 0;
329 cell_info[i][j].inset.setOwner(owner_);
333 for (int i = 0; i < columns_; ++i) {
334 calculate_width_of_column(i);
336 calculate_width_of_tabular();
338 set_row_column_number_info();
342 void LyXTabular::set_row_column_number_info(bool oldformat)
345 for (int row = 0; row < rows_; ++row) {
346 for (int column = 0; column<columns_; ++column) {
347 if (cell_info[row][column].multicolumn
348 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
350 cell_info[row][column].cellno = numberofcells;
353 ++numberofcells; // because this is one more than as we start from 0
355 rowofcell.resize(numberofcells);
356 columnofcell.resize(numberofcells);
358 for (int row = 0, column = 0, c = 0;
359 c < numberofcells && row < rows_ && column < columns_;) {
361 columnofcell[c] = column;
365 } while (column < columns_ &&
366 cell_info[row][column].multicolumn
367 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
368 if (column == columns_) {
374 for (int row = 0; row < rows_; ++row) {
375 for (int column = 0; column < columns_; ++column) {
376 if (IsPartOfMultiColumn(row,column))
378 // now set the right line of multicolumns right for oldformat read
380 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
382 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
383 cell_info[row][column].right_line =
384 cell_info[row][column+cn-1].right_line;
386 cell_info[row][column].inset.setAutoBreakRows(
387 !GetPWidth(GetCellNumber(row, column)).empty());
393 int LyXTabular::GetNumberOfCells() const
395 return numberofcells;
399 int LyXTabular::NumberOfCellsInRow(int cell) const
401 int const row = row_of_cell(cell);
403 for (int i = 0; i < columns_; ++i) {
404 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
411 /* returns 1 if there is a topline, returns 0 if not */
412 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
414 int const row = row_of_cell(cell);
416 if (!onlycolumn && IsMultiColumn(cell))
417 return cellinfo_of_cell(cell)->top_line;
418 return row_info[row].top_line;
422 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
424 // no bottom line underneath non-existent cells if you please
425 // Isn't that a programming error? Is so this should
426 // be an Assert instead. (Lgb)
427 if (cell >= numberofcells)
430 if (!onlycolumn && IsMultiColumn(cell))
431 return cellinfo_of_cell(cell)->bottom_line;
432 return row_info[row_of_cell(cell)].bottom_line;
436 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
438 if (!onlycolumn && IsMultiColumn(cell))
439 return cellinfo_of_cell(cell)->left_line;
440 return column_info[column_of_cell(cell)].left_line;
444 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
446 if (!onlycolumn && IsMultiColumn(cell))
447 return cellinfo_of_cell(cell)->right_line;
448 return column_info[right_column_of_cell(cell)].right_line;
452 bool LyXTabular::TopAlreadyDrawed(int cell) const
454 int row = row_of_cell(cell);
455 if ((row > 0) && !GetAdditionalHeight(row)) {
456 int column = column_of_cell(cell);
459 && cell_info[row][column].multicolumn
460 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
462 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
463 return row_info[row].bottom_line;
465 return cell_info[row][column].bottom_line;
471 bool LyXTabular::LeftAlreadyDrawed(int cell) const
473 int column = column_of_cell(cell);
475 int row = row_of_cell(cell);
477 (cell_info[row][column].multicolumn ==
478 LyXTabular::CELL_PART_OF_MULTICOLUMN));
479 if (GetAdditionalWidth(cell_info[row][column].cellno))
481 return column_info[column].right_line;
487 bool LyXTabular::IsLastRow(int cell) const
489 return (row_of_cell(cell) == rows_ - 1);
493 int LyXTabular::GetAdditionalHeight(int row) const
495 if (!row || row >= rows_)
501 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
502 switch (cell_info[row - 1][column].multicolumn) {
503 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
504 bottom = cell_info[row - 1][column].bottom_line;
506 case LyXTabular::CELL_NORMAL:
507 bottom = row_info[row - 1].bottom_line;
510 for (int column = 0; column < columns_ - 1 && top; ++column) {
511 switch (cell_info[row][column].multicolumn){
512 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
513 top = cell_info[row][column].top_line;
515 case LyXTabular::CELL_NORMAL:
516 top = row_info[row].top_line;
520 return WIDTH_OF_LINE;
525 int LyXTabular::GetAdditionalWidth(int cell) const
527 // internally already set in SetWidthOfCell
528 // used to get it back in text.C
529 int const col = right_column_of_cell(cell);
530 if (col < columns_ - 1 && column_info[col].right_line &&
531 column_info[col+1].left_line)
532 return WIDTH_OF_LINE;
538 // returns the maximum over all rows
539 int LyXTabular::GetWidthOfColumn(int cell) const
541 int const column1 = column_of_cell(cell);
542 int const column2 = right_column_of_cell(cell);
544 for (int i = column1; i <= column2; ++i) {
545 result += column_info[i].width_of_column;
551 int LyXTabular::GetWidthOfTabular() const
553 return width_of_tabular;
557 /* returns 1 if a complete update is necessary, otherwise 0 */
558 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
560 if (!IsMultiColumn(cell))
563 int const row = row_of_cell(cell);
564 int const column1 = column_of_cell(cell);
565 int const column2 = right_column_of_cell(cell);
567 // first set columns to 0 so we can calculate the right width
568 for (int i = column1; i <= column2; ++i) {
569 cell_info[row][i].width_of_cell = 0;
571 // set the width to MAX_WIDTH until width > 0
572 int width = (new_width + 2 * WIDTH_OF_LINE);
575 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
576 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
577 width -= column_info[i].width_of_column;
580 cell_info[row][i].width_of_cell = width;
586 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
588 int const row = row_of_cell(cell);
589 int const column1 = column_of_cell(cell);
590 int const column2 = right_column_of_cell(cell);
592 // first set columns to 0 so we can calculate the right width
594 for (; i <= column2; ++i)
595 cell_info[row][i].width_of_cell = 0;
596 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
598 if (i < numberofcells)
599 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
600 SetWidthOfMulticolCell(cell, new_width);
604 /* returns 1 if a complete update is necessary, otherwise 0 */
605 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
607 int const row = row_of_cell(cell);
608 int const column1 = column_of_cell(cell);
612 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
614 if (IsMultiColumn(cell, true)) {
615 tmp = SetWidthOfMulticolCell(cell, new_width);
617 width = (new_width + 2*WIDTH_OF_LINE);
618 cell_info[row][column1].width_of_cell = width;
619 if (column_info[column1].right_line && (column1 < columns_-1) &&
620 column_info[column1+1].left_line) // additional width
621 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
622 tmp = calculate_width_of_column_NMC(column1);
626 for (; i<columns_; ++i)
627 calculate_width_of_column_NMC(i);
628 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
630 if (i < numberofcells)
631 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
632 for (i = 0; i < columns_; ++i)
633 calculate_width_of_column(i);
634 calculate_width_of_tabular();
641 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
643 if (!IsMultiColumn(cell) || onlycolumn)
644 column_info[column_of_cell(cell)].alignment = align;
646 cellinfo_of_cell(cell)->alignment = align;
651 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
653 if (!IsMultiColumn(cell) || onlycolumn)
654 column_info[column_of_cell(cell)].valignment = align;
656 cellinfo_of_cell(cell)->valignment = align;
661 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
663 bool flag = !width.empty();
664 int const j = column_of_cell(cell);
666 column_info[j].p_width = width;
667 if (flag) // do this only if there is a width
668 SetAlignment(cell, LYX_ALIGN_LEFT);
669 for (int i = 0; i < rows_; ++i) {
670 int c = GetCellNumber(i, j);
671 flag = !GetPWidth(c).empty(); // because of multicolumns!
672 GetCellInset(c)->setAutoBreakRows(flag);
678 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
680 bool const flag = !width.empty();
682 cellinfo_of_cell(cell)->p_width = width;
683 if (IsMultiColumn(cell)) {
684 GetCellInset(cell)->setAutoBreakRows(flag);
691 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
692 LyXTabular::Feature what)
694 if (what == SET_SPECIAL_MULTI)
695 cellinfo_of_cell(cell)->align_special = special;
697 column_info[column_of_cell(cell)].align_special = special;
702 bool LyXTabular::SetAllLines(int cell, bool line)
704 SetTopLine(cell, line);
705 SetBottomLine(cell, line);
706 SetRightLine(cell, line);
707 SetLeftLine(cell, line);
712 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
714 int const row = row_of_cell(cell);
716 if (onlycolumn || !IsMultiColumn(cell))
717 row_info[row].top_line = line;
719 cellinfo_of_cell(cell)->top_line = line;
724 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
726 if (onlycolumn || !IsMultiColumn(cell))
727 row_info[row_of_cell(cell)].bottom_line = line;
729 cellinfo_of_cell(cell)->bottom_line = line;
734 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
736 if (onlycolumn || !IsMultiColumn(cell))
737 column_info[column_of_cell(cell)].left_line = line;
739 cellinfo_of_cell(cell)->left_line = line;
744 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
746 if (onlycolumn || !IsMultiColumn(cell))
747 column_info[right_column_of_cell(cell)].right_line = line;
749 cellinfo_of_cell(cell)->right_line = line;
754 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
756 if (!onlycolumn && IsMultiColumn(cell))
757 return cellinfo_of_cell(cell)->alignment;
759 return column_info[column_of_cell(cell)].alignment;
763 LyXTabular::VAlignment
764 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
766 if (!onlycolumn && IsMultiColumn(cell))
767 return cellinfo_of_cell(cell)->valignment;
769 return column_info[column_of_cell(cell)].valignment;
773 string const LyXTabular::GetPWidth(int cell) const
775 if (IsMultiColumn(cell))
776 return cellinfo_of_cell(cell)->p_width;
777 return column_info[column_of_cell(cell)].p_width;
781 string const LyXTabular::GetColumnPWidth(int cell) const
783 return column_info[column_of_cell(cell)].p_width;
787 string const LyXTabular::GetMColumnPWidth(int cell) const
789 if (IsMultiColumn(cell))
790 return cellinfo_of_cell(cell)->p_width;
795 string const LyXTabular::GetAlignSpecial(int cell, int what) const
797 if (what == SET_SPECIAL_MULTI)
798 return cellinfo_of_cell(cell)->align_special;
799 return column_info[column_of_cell(cell)].align_special;
803 int LyXTabular::GetWidthOfCell(int cell) const
805 int const row = row_of_cell(cell);
806 int const column1 = column_of_cell(cell);
807 int const column2 = right_column_of_cell(cell);
809 for (int i = column1; i <= column2; ++i) {
810 result += cell_info[row][i].width_of_cell;
816 int LyXTabular::GetBeginningOfTextInCell(int cell) const
820 switch (GetAlignment(cell)){
821 case LYX_ALIGN_CENTER:
822 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
824 case LYX_ALIGN_RIGHT:
825 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
826 // + GetAdditionalWidth(cell);
828 default: /* LYX_ALIGN_LEFT: nothing :-) */
838 bool LyXTabular::IsFirstCellInRow(int cell) const
840 return column_of_cell(cell) == 0;
844 int LyXTabular::GetFirstCellInRow(int row) const
848 return cell_info[row][0].cellno;
851 bool LyXTabular::IsLastCellInRow(int cell) const
853 return (right_column_of_cell(cell) == (columns_ - 1));
857 int LyXTabular::GetLastCellInRow(int row) const
861 return cell_info[row][columns_-1].cellno;
865 bool LyXTabular::calculate_width_of_column(int column)
867 int const old_column_width = column_info[column].width_of_column;
870 for (int i = 0; i < rows_; ++i) {
871 maximum = max(cell_info[i][column].width_of_cell, maximum);
873 column_info[column].width_of_column = maximum;
874 return (column_info[column].width_of_column != old_column_width);
879 // calculate the with of the column without regarding REAL MultiColumn
880 // cells. This means MultiColumn-cells spanning more than 1 column.
882 bool LyXTabular::calculate_width_of_column_NMC(int column)
884 int const old_column_width = column_info[column].width_of_column;
886 for (int i = 0; i < rows_; ++i) {
887 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
888 (cell_info[i][column].width_of_cell > max)) {
889 max = cell_info[i][column].width_of_cell;
892 column_info[column].width_of_column = max;
893 return (column_info[column].width_of_column != old_column_width);
897 void LyXTabular::calculate_width_of_tabular()
899 width_of_tabular = 0;
900 for (int i = 0; i < columns_; ++i) {
901 width_of_tabular += column_info[i].width_of_column;
906 int LyXTabular::row_of_cell(int cell) const
908 if (cell >= numberofcells)
912 return rowofcell[cell];
916 int LyXTabular::column_of_cell(int cell) const
918 if (cell >= numberofcells)
922 return columnofcell[cell];
926 int LyXTabular::right_column_of_cell(int cell) const
928 int const row = row_of_cell(cell);
929 int column = column_of_cell(cell);
930 while (column < (columns_ - 1) &&
931 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
937 // Perfect case for a template... (Lgb)
941 string const write_attribute(string const & name, T const & t)
943 string str = " " + name + "=\"" + tostr(t) + "\"";
948 string const write_attribute(string const & name, bool const & b)
950 return write_attribute(name, int(b));
955 string const write_attribute(string const & name, int value)
957 string str = " " + name + "=\"" + tostr(value) + "\"";
962 string const write_attribute(string const & name, string const & value)
964 string str = " " + name + "=\"" + value + "\"";
969 string const write_attribute(string const & name, bool value)
971 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
979 string const tostr(LyXAlignment const & num)
984 case LYX_ALIGN_BLOCK:
988 case LYX_ALIGN_CENTER:
990 case LYX_ALIGN_RIGHT:
992 case LYX_ALIGN_LAYOUT:
994 case LYX_ALIGN_SPECIAL:
1003 string const tostr(LyXTabular::VAlignment const & num)
1006 case LyXTabular::LYX_VALIGN_TOP:
1008 case LyXTabular::LYX_VALIGN_CENTER:
1010 case LyXTabular::LYX_VALIGN_BOTTOM:
1019 string const tostr(LyXTabular::BoxType const & num)
1022 case LyXTabular::BOX_NONE:
1024 case LyXTabular::BOX_PARBOX:
1026 case LyXTabular::BOX_MINIPAGE:
1033 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1037 << write_attribute("version", 2)
1038 << write_attribute("rows", rows_)
1039 << write_attribute("columns", columns_)
1041 // global longtable options
1043 << write_attribute("rotate", tostr(rotate))
1044 << write_attribute("islongtable", tostr(is_long_tabular))
1045 << write_attribute("endhead", endhead)
1046 << write_attribute("endfirsthead", endfirsthead)
1047 << write_attribute("endfoot", endfoot)
1048 << write_attribute("endlastfoot", endlastfoot)
1050 for (int j = 0; j < columns_; ++j) {
1052 << write_attribute("alignment", tostr(column_info[j].alignment))
1053 << write_attribute("valignment", tostr(column_info[j].valignment))
1054 << write_attribute("leftline", tostr(column_info[j].left_line))
1055 << write_attribute("rightline", tostr(column_info[j].right_line))
1056 << write_attribute("width",
1057 VSpace(column_info[j].p_width)
1059 << write_attribute("special", column_info[j].align_special)
1062 for (int i = 0; i < rows_; ++i) {
1064 << write_attribute("topline", tostr(row_info[i].top_line))
1065 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1066 << write_attribute("newpage", tostr(row_info[i].newpage))
1068 for (int j = 0; j < columns_; ++j) {
1070 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1071 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1072 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1073 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1074 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1075 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1076 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1077 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1078 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1079 << write_attribute("width", cell_info[i][j].p_width)
1080 << write_attribute("special", cell_info[i][j].align_special)
1082 os << "\\begin_inset ";
1083 cell_info[i][j].inset.write(buf, os);
1084 os << "\n\\end_inset \n"
1089 os << "</lyxtabular>\n";
1095 // I would have liked a fromstr template a lot better. (Lgb)
1098 bool string2type(string const str, LyXAlignment & num)
1101 num = LYX_ALIGN_NONE;
1102 else if (str == "block")
1103 num = LYX_ALIGN_BLOCK;
1104 else if (str == "left")
1105 num = LYX_ALIGN_LEFT;
1106 else if (str == "center")
1107 num = LYX_ALIGN_CENTER;
1108 else if (str == "right")
1109 num = LYX_ALIGN_RIGHT;
1117 bool string2type(string const str, LyXTabular::VAlignment & num)
1120 num = LyXTabular::LYX_VALIGN_TOP;
1121 else if (str == "center")
1122 num = LyXTabular::LYX_VALIGN_CENTER;
1123 else if (str == "bottom")
1124 num = LyXTabular::LYX_VALIGN_BOTTOM;
1132 bool string2type(string const str, LyXTabular::BoxType & num)
1135 num = LyXTabular::BOX_NONE;
1136 else if (str == "parbox")
1137 num = LyXTabular::BOX_PARBOX;
1138 else if (str == "minipage")
1139 num = LyXTabular::BOX_MINIPAGE;
1147 bool string2type(string const str, bool & num)
1151 else if (str == "false")
1159 bool getTokenValue(string const & str, const char * token, string & ret)
1161 size_t token_length = strlen(token);
1162 string::size_type pos = str.find(token);
1164 if (pos == string::npos || pos + token_length + 1 >= str.length()
1165 || str[pos + token_length] != '=')
1168 pos += token_length + 1;
1170 if ((ch != '"') && (ch != '\'')) { // only read till next space
1174 while((pos < str.length() - 1) && (str[++pos] != ch))
1181 bool getTokenValue(string const & str, const char * token, int & num)
1184 if (!getTokenValue(str, token, tmp))
1186 num = strToInt(tmp);
1191 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1194 if (!getTokenValue(str, token, tmp))
1196 return string2type(tmp, num);
1200 bool getTokenValue(string const & str, const char * token,
1201 LyXTabular::VAlignment & num)
1204 if (!getTokenValue(str, token, tmp))
1206 return string2type(tmp, num);
1210 bool getTokenValue(string const & str, const char * token,
1211 LyXTabular::BoxType & num)
1214 if (!getTokenValue(str, token, tmp))
1216 return string2type(tmp, num);
1220 bool getTokenValue(string const & str, const char * token, bool & flag)
1223 if (!getTokenValue(str, token, tmp))
1225 return string2type(tmp, flag);
1230 void l_getline(istream & is, string & str)
1233 while (str.empty()) {
1235 if (!str.empty() && str[str.length() - 1] == '\r')
1236 str.erase(str.length() - 1);
1243 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1246 istream & is = lex.getStream();
1248 l_getline(is, line);
1249 if (!prefixIs(line, "<lyxtabular ")
1250 && !prefixIs(line, "<LyXTabular ")) {
1251 OldFormatRead(lex, line);
1256 if (!getTokenValue(line, "version", version))
1259 ReadOld(buf, is, lex, line);
1260 else if (version == 2)
1261 ReadNew(buf, is, lex, line);
1265 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1266 LyXLex & lex, string const & l)
1270 if (!getTokenValue(line, "rows", rows_arg))
1273 if (!getTokenValue(line, "columns", columns_arg))
1275 Init(rows_arg, columns_arg);
1276 l_getline(is, line);
1277 if (!prefixIs(line, "<features")) {
1278 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1279 line << ")" << endl;
1282 getTokenValue(line, "rotate", rotate);
1283 getTokenValue(line, "islongtable", is_long_tabular);
1284 getTokenValue(line, "endhead", endhead);
1285 getTokenValue(line, "endfirsthead", endfirsthead);
1286 getTokenValue(line, "endfoot", endfoot);
1287 getTokenValue(line, "endlastfoot", endlastfoot);
1289 for (int j = 0; j < columns_; ++j) {
1291 if (!prefixIs(line,"<column")) {
1292 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1293 line << ")" << endl;
1296 getTokenValue(line, "alignment", column_info[j].alignment);
1297 getTokenValue(line, "valignment", column_info[j].valignment);
1298 getTokenValue(line, "leftline", column_info[j].left_line);
1299 getTokenValue(line, "rightline", column_info[j].right_line);
1300 getTokenValue(line, "width", column_info[j].p_width);
1301 getTokenValue(line, "special", column_info[j].align_special);
1304 for (int i = 0; i < rows_; ++i) {
1305 l_getline(is, line);
1306 if (!prefixIs(line, "<row")) {
1307 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1308 line << ")" << endl;
1311 getTokenValue(line, "topline", row_info[i].top_line);
1312 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1313 getTokenValue(line, "newpage", row_info[i].newpage);
1314 for (int j = 0; j < columns_; ++j) {
1315 l_getline(is, line);
1316 if (!prefixIs(line, "<cell")) {
1317 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1318 line << ")" << endl;
1321 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1322 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1323 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1324 getTokenValue(line, "topline", cell_info[i][j].top_line);
1325 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1326 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1327 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1328 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1329 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1330 getTokenValue(line, "width", cell_info[i][j].p_width);
1331 getTokenValue(line, "special", cell_info[i][j].align_special);
1332 l_getline(is, line);
1333 if (prefixIs(line, "\\begin_inset")) {
1334 cell_info[i][j].inset.read(buf, lex);
1335 l_getline(is, line);
1337 if (!prefixIs(line, "</cell>")) {
1338 lyxerr << "Wrong tabular format (expected </cell> got" <<
1339 line << ")" << endl;
1343 l_getline(is, line);
1344 if (!prefixIs(line, "</row>")) {
1345 lyxerr << "Wrong tabular format (expected </row> got" <<
1346 line << ")" << endl;
1350 while (!prefixIs(line, "</lyxtabular>")) {
1351 l_getline(is, line);
1353 set_row_column_number_info();
1357 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1363 int columns_arg = 0;
1364 int is_long_tabular_arg = false;
1365 int rotate_arg = false;
1374 istream & is = lex.getStream();
1377 version = lyx::atoi(s.substr(8, string::npos));
1381 vector<int> cont_row_info;
1384 lyxerr << "Tabular format < 5 is not supported anymore\n"
1385 "Get an older version of LyX (< 1.1.x) for conversion!"
1387 WriteAlert(_("Warning:"),
1388 _("Tabular format < 5 is not supported anymore\n"),
1389 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1391 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1392 >> rotate_arg >> a >> b >> c >> d;
1394 is >> rows_arg >> columns_arg;
1395 Init(rows_arg, columns_arg);
1396 cont_row_info = vector<int>(rows_arg);
1397 SetLongTabular(is_long_tabular_arg);
1398 SetRotateTabular(rotate_arg);
1400 for (i = 0; i < rows_; ++i) {
1402 cont_row_info[i] = false;
1404 for (i = 0; i < columns_; ++i) {
1407 for (i = 0; i < rows_; ++i) {
1408 for (j = 0; j < columns_; ++j) {
1413 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1414 >> rotate_arg >> a >> b >> c >> d;
1415 Init(rows_arg, columns_arg);
1416 cont_row_info = vector<int>(rows_arg);
1417 SetLongTabular(is_long_tabular_arg);
1418 SetRotateTabular(rotate_arg);
1420 endfirsthead = b + 1;
1422 endlastfoot = d + 1;
1423 for (i = 0; i < rows_; ++i) {
1424 a = b = c = d = e = f = g = 0;
1425 is >> a >> b >> c >> d;
1426 row_info[i].top_line = a;
1427 row_info[i].bottom_line = b;
1428 cont_row_info[i] = c;
1429 row_info[i].newpage = d;
1431 for (i = 0; i < columns_; ++i) {
1436 char ch; // skip '"'
1439 // ignore is buggy but we will use it later (Lgb)
1440 is.ignore(); // skip '"'
1442 getline(is, s1, '"');
1444 is >> ch; // skip '"'
1446 // ignore is buggy but we will use it later (Lgb)
1447 is.ignore(); // skip '"'
1449 getline(is, s2, '"');
1450 column_info[i].alignment = static_cast<LyXAlignment>(a);
1451 column_info[i].left_line = b;
1452 column_info[i].right_line = c;
1453 column_info[i].p_width = s1;
1454 column_info[i].align_special = s2;
1456 for (i = 0; i < rows_; ++i) {
1457 for (j = 0; j < columns_; ++j) {
1460 is >> a >> b >> c >> d >> e >> f >> g;
1463 is >> ch; // skip '"'
1465 // ignore is buggy but we will use it later (Lgb)
1466 is.ignore(); // skip '"'
1468 getline(is, s1, '"');
1470 is >> ch; // skip '"'
1472 // ignore is buggy but we will use it later (Lgb)
1473 is.ignore(); // skip '"'
1475 getline(is, s2, '"');
1476 cell_info[i][j].multicolumn = static_cast<char>(a);
1477 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1478 cell_info[i][j].top_line = static_cast<char>(c);
1479 cell_info[i][j].bottom_line = static_cast<char>(d);
1480 cell_info[i][j].left_line = column_info[j].left_line;
1481 cell_info[i][j].right_line = column_info[j].right_line;
1482 cell_info[i][j].rotate = static_cast<bool>(f);
1483 cell_info[i][j].usebox = static_cast<BoxType>(g);
1484 cell_info[i][j].align_special = s1;
1485 cell_info[i][j].p_width = s2;
1489 set_row_column_number_info(true);
1491 Paragraph * par = new Paragraph;
1492 Paragraph * return_par = 0;
1496 Paragraph::depth_type depth = 0;
1497 LyXFont font(LyXFont::ALL_INHERIT);
1498 font.setLanguage(owner_->bufferOwner()->getLanguage());
1500 while (lex.isOK()) {
1502 string const token = lex.getString();
1505 if (token == "\\layout"
1506 || token == "\\end_float"
1507 || token == "\\end_deeper") {
1508 lex.pushToken(token);
1509 #ifndef NO_COMPABILITY
1510 // Here we need to insert the inset_ert_contents into the last
1511 // cell of the tabular.
1512 owner_->bufferOwner()->insertErtContents(par, pos, font);
1516 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1521 lex.pushToken(token);
1525 lex.printError("New Paragraph allocated! This should not happen!");
1526 lex.pushToken(token);
1532 // now we have the par we should fill the insets with this!
1534 InsetText * inset = GetCellInset(cell);
1537 for (int i = 0; i < par->size(); ++i) {
1538 if (par->isNewline(i)) {
1540 if (cell > numberofcells) {
1541 lyxerr << "Some error in reading old table format occured!" <<
1542 endl << "Terminating when reading cell[" << cell << "]!" <<
1547 row = row_of_cell(cell);
1548 if (cont_row_info[row]) {
1550 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1551 while(!IsFirstCellInRow(--cell));
1553 inset = GetCellInset(cell);
1556 inset = GetCellInset(cell);
1557 row = row_of_cell(cell);
1558 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1560 // insert a space instead
1562 par->insertChar(i, ' ');
1565 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1566 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1573 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1575 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1576 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1580 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1582 int const row = row_of_cell(cell);
1583 int const column = column_of_cell(cell);
1584 return &cell_info[row][column];
1588 void LyXTabular::SetMultiColumn(int cell, int number)
1590 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1591 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1592 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1593 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1594 for (number--; number > 0; --number) {
1595 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1597 set_row_column_number_info();
1601 int LyXTabular::cells_in_multicolumn(int cell) const
1603 int const row = row_of_cell(cell);
1604 int column = column_of_cell(cell);
1607 while ((column < columns_) &&
1608 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1617 int LyXTabular::UnsetMultiColumn(int cell)
1619 int const row = row_of_cell(cell);
1620 int column = column_of_cell(cell);
1624 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1625 cell_info[row][column].multicolumn = CELL_NORMAL;
1627 while ((column < columns_) &&
1628 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1630 cell_info[row][column].multicolumn = CELL_NORMAL;
1635 set_row_column_number_info();
1640 void LyXTabular::SetLongTabular(bool what)
1642 is_long_tabular = what;
1646 bool LyXTabular::IsLongTabular() const
1648 return is_long_tabular;
1652 void LyXTabular::SetRotateTabular(bool flag)
1658 bool LyXTabular::GetRotateTabular() const
1664 void LyXTabular::SetRotateCell(int cell, bool flag)
1666 cellinfo_of_cell(cell)->rotate = flag;
1670 bool LyXTabular::GetRotateCell(int cell) const
1672 return cellinfo_of_cell(cell)->rotate;
1676 bool LyXTabular::NeedRotating() const
1680 for (int i = 0; i < rows_; ++i) {
1681 for (int j = 0; j < columns_; ++j) {
1682 if (cell_info[i][j].rotate)
1690 bool LyXTabular::IsLastCell(int cell) const
1692 if ((cell + 1) < numberofcells)
1698 int LyXTabular::GetCellAbove(int cell) const
1700 if (row_of_cell(cell) > 0)
1701 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1706 int LyXTabular::GetCellBelow(int cell) const
1708 if (row_of_cell(cell) + 1 < rows_)
1709 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1714 int LyXTabular::GetLastCellAbove(int cell) const
1716 if (row_of_cell(cell) <= 0)
1718 if (!IsMultiColumn(cell))
1719 return GetCellAbove(cell);
1720 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1724 int LyXTabular::GetLastCellBelow(int cell) const
1726 if (row_of_cell(cell) + 1 >= rows_)
1728 if (!IsMultiColumn(cell))
1729 return GetCellBelow(cell);
1730 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1734 int LyXTabular::GetCellNumber(int row, int column) const
1737 if (column >= columns_)
1738 column = columns_ - 1;
1739 else if (column < 0)
1746 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1748 return cell_info[row][column].cellno;
1752 void LyXTabular::SetUsebox(int cell, BoxType type)
1754 cellinfo_of_cell(cell)->usebox = type;
1758 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1760 if (column_info[column_of_cell(cell)].p_width.empty() &&
1761 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1763 if (cellinfo_of_cell(cell)->usebox > 1)
1764 return cellinfo_of_cell(cell)->usebox;
1765 return UseParbox(cell);
1769 void LyXTabular::SetLTHead(int cell, bool first)
1771 int const row = row_of_cell(cell);
1772 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1775 if (endfirsthead == val)
1788 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1791 if (abs(endhead) > rows_)
1793 return (row_of_cell(cell) == abs(endhead) - 1);
1797 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1800 if (abs(endfirsthead) > rows_)
1802 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1806 void LyXTabular::SetLTFoot(int cell, bool last)
1808 int const row = row_of_cell(cell);
1809 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1812 if (endlastfoot == val)
1825 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1828 if ((endfoot + 1) > rows_)
1830 return (row_of_cell(cell) == abs(endfoot) - 1);
1834 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1837 if (abs(endlastfoot) > rows_)
1839 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1843 void LyXTabular::SetLTNewPage(int cell, bool what)
1845 row_info[row_of_cell(cell)].newpage = what;
1849 bool LyXTabular::GetLTNewPage(int cell) const
1851 return row_info[row_of_cell(cell)].newpage;
1855 bool LyXTabular::SetAscentOfRow(int row, int height)
1857 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1859 row_info[row].ascent_of_row = height;
1864 bool LyXTabular::SetDescentOfRow(int row, int height)
1866 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1868 row_info[row].descent_of_row = height;
1873 int LyXTabular::GetAscentOfRow(int row) const
1877 return row_info[row].ascent_of_row;
1881 int LyXTabular::GetDescentOfRow(int row) const
1885 return row_info[row].descent_of_row;
1889 int LyXTabular::GetHeightOfTabular() const
1893 for (int row = 0; row < rows_; ++row)
1894 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1895 GetAdditionalHeight(row);
1900 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1902 if ((row >= rows_) || (column >= columns_))
1904 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1908 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1910 if ((row < 0) || (row >= rows_))
1913 int const fcell = GetFirstCellInRow(row);
1914 int const n = NumberOfCellsInRow(fcell) + fcell;
1917 for (int i = fcell; i < n; ++i) {
1921 if (tmp == (n - fcell)){
1924 for (int i = fcell; i < n; ++i) {
1927 << column_of_cell(i) + 1
1929 << right_column_of_cell(i) + 1
1941 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1943 if ((row < 0) || (row >= rows_))
1946 int const fcell = GetFirstCellInRow(row);
1947 int const n = NumberOfCellsInRow(fcell) + fcell;
1950 for (int i = fcell; i < n; ++i) {
1954 if (tmp == (n-fcell)){
1957 for (int i = fcell; i < n; ++i) {
1958 if (BottomLine(i)) {
1960 << column_of_cell(i) + 1
1962 << right_column_of_cell(i) + 1
1974 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1978 if (GetRotateCell(cell)) {
1979 os << "\\begin{sideways}\n";
1982 if (IsMultiColumn(cell)) {
1983 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1984 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1985 os << cellinfo_of_cell(cell)->align_special << "}{";
1987 if (LeftLine(cell) &&
1988 (IsFirstCellInRow(cell) ||
1989 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1990 !RightLine(cell-1, true))))
1994 if (!GetPWidth(cell).empty()) {
1995 switch (GetVAlignment(cell)) {
1996 case LYX_VALIGN_TOP:
1999 case LYX_VALIGN_CENTER:
2002 case LYX_VALIGN_BOTTOM:
2006 os << "{" << GetPWidth(cell) << '}';
2008 switch (GetAlignment(cell)) {
2009 case LYX_ALIGN_LEFT:
2012 case LYX_ALIGN_RIGHT:
2020 if (RightLine(cell))
2022 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2028 if (GetUsebox(cell) == BOX_PARBOX) {
2030 switch (GetVAlignment(cell)) {
2031 case LYX_VALIGN_TOP:
2034 case LYX_VALIGN_CENTER:
2037 case LYX_VALIGN_BOTTOM:
2041 os << "]{" << GetPWidth(cell) << "}{";
2042 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2043 os << "\\begin{minipage}[";
2044 switch (GetVAlignment(cell)) {
2045 case LYX_VALIGN_TOP:
2048 case LYX_VALIGN_CENTER:
2051 case LYX_VALIGN_BOTTOM:
2055 os << "]{" << GetPWidth(cell) << "}\n";
2062 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2067 if (GetUsebox(cell) == BOX_PARBOX)
2069 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2070 os << "%\n\\end{minipage}";
2073 if (IsMultiColumn(cell)){
2076 if (GetRotateCell(cell)) {
2077 os << "%\n\\end{sideways}";
2084 int LyXTabular::Latex(Buffer const * buf,
2085 ostream & os, bool fragile, bool fp) const
2090 //+---------------------------------------------------------------------
2091 //+ first the opening preamble +
2092 //+---------------------------------------------------------------------
2095 os << "\\begin{sideways}\n";
2098 if (is_long_tabular)
2099 os << "\\begin{longtable}{";
2101 os << "\\begin{tabular}{";
2102 for (int i = 0; i < columns_; ++i) {
2103 if (column_info[i].left_line)
2105 if (!column_info[i].align_special.empty()) {
2106 os << column_info[i].align_special;
2107 } else if (!column_info[i].p_width.empty()) {
2108 switch (column_info[i].valignment) {
2109 case LYX_VALIGN_TOP:
2112 case LYX_VALIGN_CENTER:
2115 case LYX_VALIGN_BOTTOM:
2120 << column_info[i].p_width
2123 switch (column_info[i].alignment) {
2124 case LYX_ALIGN_LEFT:
2127 case LYX_ALIGN_RIGHT:
2135 if (column_info[i].right_line)
2141 //+---------------------------------------------------------------------
2142 //+ the single row and columns (cells) +
2143 //+---------------------------------------------------------------------
2145 for (int i = 0; i < rows_; ++i) {
2146 ret += TeXTopHLine(os, i);
2148 if (IsLongTabular()) {
2149 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2150 os << "\\endhead\n";
2153 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2154 os << "\\endfirsthead\n";
2157 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2158 os << "\\endfoot\n";
2161 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2162 os << "\\endlastfoot\n";
2167 ret += TeXBottomHLine(os, i-1);
2168 ret += TeXTopHLine(os, i);
2170 for (int j = 0; j < columns_; ++j) {
2171 if (IsPartOfMultiColumn(i,j))
2173 ret += TeXCellPreamble(os, cell);
2174 InsetText * inset = GetCellInset(cell);
2176 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2177 inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
2181 ret += inset->latex(buf, os, fragile, fp);
2185 ret += TeXCellPostamble(os, cell);
2186 if (!IsLastCellInRow(cell)) { // not last cell in row
2193 ret += TeXBottomHLine(os, i);
2195 if (IsLongTabular()) {
2196 if ((endhead > 0) && (i == (endhead - 1))) {
2197 os << "\\endhead\n";
2200 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2201 os << "\\endfirsthead\n";
2204 if ((endfoot > 0) && (i == (endfoot - 1))) {
2205 os << "\\endfoot\n";
2208 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2209 os << "\\endlastfoot\n";
2213 // ret += TeXBottomHLine(os, i);
2214 if (row_info[i].newpage) {
2215 os << "\\newpage\n";
2221 //+---------------------------------------------------------------------
2222 //+ the closing of the tabular +
2223 //+---------------------------------------------------------------------
2225 if (is_long_tabular)
2226 os << "\\end{longtable}";
2228 os << "\\end{tabular}";
2230 os << "\n\\end{sideways}";
2238 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2242 //+---------------------------------------------------------------------
2243 //+ first the opening preamble +
2244 //+---------------------------------------------------------------------
2246 os << "<tgroup cols=\"" << columns_
2247 << "\" colsep=\"1\" rowsep=\"1\">\n";
2249 for (int i = 0; i < columns_; ++i) {
2250 os << "<colspec colname=\"col" << i << "\" align=\"";
2251 switch (column_info[i].alignment) {
2252 case LYX_ALIGN_LEFT:
2255 case LYX_ALIGN_RIGHT:
2266 //+---------------------------------------------------------------------
2267 //+ the single row and columns (cells) +
2268 //+---------------------------------------------------------------------
2272 for (int i = 0; i < rows_; ++i) {
2274 for (int j = 0; j < columns_; ++j) {
2275 if (IsPartOfMultiColumn(i, j))
2278 os << "<entry align=\"";
2279 switch (GetAlignment(cell)) {
2280 case LYX_ALIGN_LEFT:
2283 case LYX_ALIGN_RIGHT:
2291 os << "\" valign=\"";
2292 switch (GetVAlignment(cell)) {
2293 case LYX_VALIGN_TOP:
2296 case LYX_VALIGN_BOTTOM:
2299 case LYX_VALIGN_CENTER:
2304 if (IsMultiColumn(cell)) {
2305 os << " namest=\"col" << j << "\" ";
2306 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2310 ret += GetCellInset(cell)->docBook(buf, os);
2317 //+---------------------------------------------------------------------
2318 //+ the closing of the tabular +
2319 //+---------------------------------------------------------------------
2331 void print_n_chars(ostream & os, unsigned char ch, int n)
2333 os << string(n, ch);
2339 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2340 vector<unsigned int> const & clen) const
2342 int const fcell = GetFirstCellInRow(row);
2343 int const n = NumberOfCellsInRow(fcell) + fcell;
2346 for (int i = fcell; i < n; ++i) {
2356 for (int i = fcell; i < n; ++i) {
2367 int column = column_of_cell(i);
2368 int len = clen[column];
2369 while(IsPartOfMultiColumn(row, ++column))
2370 len += clen[column] + 4;
2371 print_n_chars(os, ch, len);
2386 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2387 vector<unsigned int> const & clen) const
2389 int const fcell = GetFirstCellInRow(row);
2390 int const n = NumberOfCellsInRow(fcell) + fcell;
2393 for (int i = fcell; i < n; ++i) {
2394 if (BottomLine(i)) {
2403 for (int i = fcell; i < n; ++i) {
2404 if (BottomLine(i)) {
2414 int column = column_of_cell(i);
2415 int len = clen[column];
2416 while(IsPartOfMultiColumn(row, ++column))
2417 len += clen[column] + 4;
2418 print_n_chars(os, ch, len);
2419 if (BottomLine(i)) {
2433 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2434 int cell, int row, int column,
2435 vector<unsigned int> const & clen) const
2438 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2445 unsigned int len1 = sstr.str().length();
2446 unsigned int len2 = clen[column];
2447 while(IsPartOfMultiColumn(row, ++column))
2448 len2 += clen[column] + 4;
2451 switch (GetAlignment(cell)) {
2453 case LYX_ALIGN_LEFT:
2456 case LYX_ALIGN_RIGHT:
2460 case LYX_ALIGN_CENTER:
2466 for (unsigned int i = 0; i < len1; ++i)
2469 for (unsigned int i = 0; i < len2; ++i)
2471 if (RightLine(cell))
2480 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2484 //+---------------------------------------------------------------------
2485 //+ first calculate the width of the single columns +
2486 //+---------------------------------------------------------------------
2487 vector<unsigned int> clen(columns_);
2489 // first all non (real) multicolumn cells!
2490 for (int j = 0; j < columns_; ++j) {
2492 for (int i = 0; i < rows_; ++i) {
2493 int cell = GetCellNumber(i, j);
2494 if (IsMultiColumn(cell, true))
2497 GetCellInset(cell)->ascii(buf, sstr, 0);
2498 if (clen[j] < sstr.str().length())
2499 clen[j] = sstr.str().length();
2502 // then all (real) multicolumn cells!
2503 for (int j = 0; j < columns_; ++j) {
2504 for (int i = 0; i < rows_; ++i) {
2505 int cell = GetCellNumber(i, j);
2506 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2509 GetCellInset(cell)->ascii(buf, sstr, 0);
2510 int len = int(sstr.str().length());
2511 int const n = cells_in_multicolumn(cell);
2512 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2514 if (len > int(clen[j + n - 1]))
2515 clen[j + n - 1] = len;
2519 for (int i = 0; i < rows_; ++i) {
2520 AsciiTopHLine(os, i, clen);
2521 for (int j = 0; j < columns_; ++j) {
2522 if (IsPartOfMultiColumn(i,j))
2524 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2528 AsciiBottomHLine(os, i, clen);
2534 InsetText * LyXTabular::GetCellInset(int cell) const
2537 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2541 InsetText * LyXTabular::GetCellInset(int row, int column) const
2543 cur_cell = GetCellNumber(row, column);
2544 return & cell_info[row][column].inset;
2548 void LyXTabular::Validate(LaTeXFeatures & features) const
2550 if (IsLongTabular())
2551 features.longtable = true;
2553 features.rotating = true;
2554 for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2555 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2556 features.array = true;
2557 GetCellInset(cell)->validate(features);
2562 std::vector<string> const LyXTabular::getLabelList() const
2564 std::vector<string> label_list;
2565 for (int i = 0; i < rows_; ++i)
2566 for (int j = 0; j < columns_; ++j) {
2567 std::vector<string> const l =
2568 GetCellInset(i, j)->getLabelList();
2569 label_list.insert(label_list.end(),
2570 l.begin(), l.end());
2576 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2578 Paragraph * par = GetCellInset(cell)->paragraph();
2580 for (; par; par = par->next()) {
2581 for (int i = 0; i < par->size(); ++i) {
2582 if (par->getChar(i) == Paragraph::META_NEWLINE)