1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 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"
43 static int const WIDTH_OF_LINE = 5;
45 /// Define a few methods for the inner structs
47 LyXTabular::cellstruct::cellstruct()
51 multicolumn = LyXTabular::CELL_NORMAL;
52 alignment = LYX_ALIGN_CENTER;
53 valignment = LYX_VALIGN_TOP;
63 LyXTabular::rowstruct::rowstruct()
73 LyXTabular::columnstruct::columnstruct()
77 alignment = LYX_ALIGN_CENTER;
78 valignment = LYX_VALIGN_TOP;
84 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
87 Init(rows_arg, columns_arg);
91 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
94 Init(lt.rows_, lt.columns_);
95 #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)
100 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
107 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
109 // If this and lt is not of the same size we have a serious bug
110 // So then it is ok to throw an exception, or for now
112 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
114 cell_info = lt.cell_info;
115 row_info = lt.row_info;
116 column_info = lt.column_info;
118 // long tabular stuff
119 SetLongTabular(lt.is_long_tabular);
120 endhead = lt.endhead;
121 endfoot = lt.endfoot;
122 endfirsthead = lt.endfirsthead;
123 endlastfoot = lt.endlastfoot;
133 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
135 LyXTabular * result = new LyXTabular(inset, *this);
136 // don't know if this is good but I need to Clone also
137 // the text-insets here, this is for the Undo-facility!
138 for (int i = 0; i < rows_; ++i) {
139 for (int j = 0; j < columns_; ++j) {
140 result->cell_info[i][j].inset = cell_info[i][j].inset;
141 result->cell_info[i][j].inset.setOwner(inset);
148 /* activates all lines and sets all widths to 0 */
149 void LyXTabular::Init(int rows_arg, int columns_arg)
152 columns_ = columns_arg;
153 row_info = row_vector(rows_, rowstruct());
154 column_info = column_vector(columns_, columnstruct());
155 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
158 for (int i = 0; i < rows_; ++i) {
159 for (int j = 0; j < columns_; ++j) {
160 cell_info[i][j].inset.setOwner(owner_);
161 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
162 cell_info[i][j].cellno = cellno++;
164 cell_info[i].back().right_line = true;
166 row_info.back().bottom_line = true;
167 row_info.front().bottom_line = true;
169 for (int i = 0; i < columns_; ++i) {
170 calculate_width_of_column(i);
172 column_info.back().right_line = true;
174 calculate_width_of_tabular();
176 rowofcell = vector<int>();
177 columnofcell = vector<int>();
178 set_row_column_number_info();
179 is_long_tabular = false;
188 void LyXTabular::AppendRow(int cell)
192 int row = row_of_cell(cell);
194 row_vector::iterator rit = row_info.begin() + row;
195 row_info.insert(rit, rowstruct());
198 cell_vvector::iterator cit = cell_info.begin() + row;
199 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
201 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
204 for (int i = 0; i <= row; ++i) {
205 for (int j = 0; j < columns_; ++j) {
206 c_info[i][j] = cell_info[i][j];
209 for (int i = row + 1; i < rows_; ++i) {
210 for (int j = 0; j < columns_; ++j) {
211 c_info[i][j] = cell_info[i-1][j];
216 for (int j = 0; j < columns_; ++j) {
217 cell_info[row][j].inset.clear();
224 void LyXTabular::DeleteRow(int row)
226 // Why make it so hard? (Lgb)
229 if (rows_ == 1) return; // Not allowed to delete last row
231 row_info.erase(row_info.begin() + row); //&row_info[row]);
232 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
238 void LyXTabular::AppendColumn(int cell)
242 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
244 int const column = column_of_cell(cell);
245 column_vector::iterator cit = column_info.begin() + column + 1;
246 column_info.insert(cit, columnstruct());
248 for (int i = 0; i < rows_; ++i) {
249 for (int j = 0; j <= column; ++j) {
250 c_info[i][j] = cell_info[i][j];
252 for (int j = column + 1; j < columns_; ++j) {
253 c_info[i][j] = cell_info[i][j - 1];
255 // care about multicolumns
256 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
257 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
259 if ((column + 1) == columns_ ||
260 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
261 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
266 for (int i = 0; i < rows_; ++i) {
267 //cell_info[i][column].inset.clear();
268 cell_info[i][column + 1].inset.clear();
274 void LyXTabular::DeleteColumn(int column)
276 // Similar to DeleteRow
277 //if (!(columns_ - 1))
279 if (columns_ == 1) return; // Not allowed to delete last column
281 column_info.erase(column_info.begin() + column);
282 for (int i = 0; i < rows_; ++i) {
283 cell_info[i].erase(cell_info[i].begin() + column);
290 void LyXTabular::Reinit()
292 for (int i = 0; i < rows_; ++i) {
293 for (int j = 0; j < columns_; ++j) {
294 cell_info[i][j].width_of_cell = 0;
295 cell_info[i][j].inset.setOwner(owner_);
299 for (int i = 0; i < columns_; ++i) {
300 calculate_width_of_column(i);
302 calculate_width_of_tabular();
304 set_row_column_number_info();
308 void LyXTabular::set_row_column_number_info(bool oldformat)
311 for (int row = 0; row < rows_; ++row) {
312 for (int column = 0; column<columns_; ++column) {
313 if (cell_info[row][column].multicolumn
314 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
316 cell_info[row][column].cellno = numberofcells;
319 ++numberofcells; // because this is one more than as we start from 0
321 rowofcell.resize(numberofcells);
322 columnofcell.resize(numberofcells);
329 while (c < numberofcells && row < rows_ && column < columns_) {
331 columnofcell[c] = column;
335 } while (column < columns_ &&
336 cell_info[row][column].multicolumn
337 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
338 if (column == columns_) {
344 // Isn't this the same as the while above? (Lgb)
345 for (int row = 0, column = 0, c = 0;
346 c < numberofcells && row < rows_ && column < columns_;) {
348 columnofcell[c] = column;
352 } while (column < columns_ &&
353 cell_info[row][column].multicolumn
354 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
355 if (column == columns_) {
362 for (int row = 0; row < rows_; ++row) {
363 for (int column = 0; column < columns_; ++column) {
364 if (IsPartOfMultiColumn(row,column))
366 // now set the right line of multicolumns right for oldformat read
368 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
370 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
371 cell_info[row][column].right_line =
372 cell_info[row][column+cn-1].right_line;
374 cell_info[row][column].inset.SetAutoBreakRows(
375 !GetPWidth(GetCellNumber(row, column)).empty());
381 int LyXTabular::GetNumberOfCells() const
383 return numberofcells;
387 int LyXTabular::NumberOfCellsInRow(int cell) const
389 int const row = row_of_cell(cell);
391 for (int i = 0; i < columns_; ++i) {
392 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
399 /* returns 1 if there is a topline, returns 0 if not */
400 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
402 int const row = row_of_cell(cell);
404 if (!onlycolumn && IsMultiColumn(cell))
405 return cellinfo_of_cell(cell)->top_line;
406 return row_info[row].top_line;
410 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
412 // no bottom line underneath non-existent cells if you please
413 // Isn't that a programming error? Is so this should
414 // be an Assert instead. (Lgb)
415 if (cell >= numberofcells)
418 if (!onlycolumn && IsMultiColumn(cell))
419 return cellinfo_of_cell(cell)->bottom_line;
420 return row_info[row_of_cell(cell)].bottom_line;
424 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
426 if (!onlycolumn && IsMultiColumn(cell))
427 return cellinfo_of_cell(cell)->left_line;
428 return column_info[column_of_cell(cell)].left_line;
432 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
434 if (!onlycolumn && IsMultiColumn(cell))
435 return cellinfo_of_cell(cell)->right_line;
436 return column_info[right_column_of_cell(cell)].right_line;
440 bool LyXTabular::TopAlreadyDrawed(int cell) const
442 if (GetAdditionalHeight(cell))
444 int row = row_of_cell(cell);
446 int column = column_of_cell(cell);
449 && cell_info[row][column].multicolumn
450 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
452 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
453 return row_info[row].bottom_line;
455 return cell_info[row][column].bottom_line;
461 bool LyXTabular::LeftAlreadyDrawed(int cell) const
463 int column = column_of_cell(cell);
465 int row = row_of_cell(cell);
467 (cell_info[row][column].multicolumn ==
468 LyXTabular::CELL_PART_OF_MULTICOLUMN));
469 if (GetAdditionalWidth(cell_info[row][column].cellno))
471 return column_info[column].right_line;
477 bool LyXTabular::IsLastRow(int cell) const
479 return (row_of_cell(cell) == rows_ - 1);
483 int LyXTabular::GetAdditionalHeight(int cell) const
485 int const row = row_of_cell(cell);
491 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
492 switch (cell_info[row - 1][column].multicolumn) {
493 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
494 bottom = cell_info[row - 1][column].bottom_line;
496 case LyXTabular::CELL_NORMAL:
497 bottom = row_info[row - 1].bottom_line;
500 for (int column = 0; column < columns_ - 1 && top; ++column) {
501 switch (cell_info[row][column].multicolumn){
502 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
503 top = cell_info[row][column].top_line;
505 case LyXTabular::CELL_NORMAL:
506 top = row_info[row].top_line;
510 return WIDTH_OF_LINE;
515 int LyXTabular::GetAdditionalWidth(int cell) const
517 // internally already set in SetWidthOfCell
518 // used to get it back in text.C
519 int const col = right_column_of_cell(cell);
520 if (col < columns_ - 1 && column_info[col].right_line &&
521 column_info[col+1].left_line)
522 return WIDTH_OF_LINE;
528 // returns the maximum over all rows
529 int LyXTabular::GetWidthOfColumn(int cell) const
531 int const column1 = column_of_cell(cell);
532 int const column2 = right_column_of_cell(cell);
534 for (int i = column1; i <= column2; ++i) {
535 result += column_info[i].width_of_column;
541 int LyXTabular::GetWidthOfTabular() const
543 return width_of_tabular;
547 /* returns 1 if a complete update is necessary, otherwise 0 */
548 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
550 if (!IsMultiColumn(cell))
553 int const row = row_of_cell(cell);
554 int const column1 = column_of_cell(cell);
555 int const column2 = right_column_of_cell(cell);
557 // first set columns to 0 so we can calculate the right width
558 for (int i = column1; i <= column2; ++i) {
559 cell_info[row][i].width_of_cell = 0;
561 // set the width to MAX_WIDTH until width > 0
562 int width = (new_width + 2 * WIDTH_OF_LINE);
565 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
566 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
567 width -= column_info[i].width_of_column;
570 cell_info[row][i].width_of_cell = width;
576 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
578 int const row = row_of_cell(cell);
579 int const column1 = column_of_cell(cell);
580 int const column2 = right_column_of_cell(cell);
582 // first set columns to 0 so we can calculate the right width
584 for (; i <= column2; ++i)
585 cell_info[row][i].width_of_cell = 0;
586 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
588 if (i < numberofcells)
589 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
590 SetWidthOfMulticolCell(cell, new_width);
594 /* returns 1 if a complete update is necessary, otherwise 0 */
595 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
597 int const row = row_of_cell(cell);
598 int const column1 = column_of_cell(cell);
602 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
604 if (IsMultiColumn(cell, true)) {
605 tmp = SetWidthOfMulticolCell(cell, new_width);
607 width = (new_width + 2*WIDTH_OF_LINE);
608 cell_info[row][column1].width_of_cell = width;
609 if (column_info[column1].right_line && (column1 < columns_-1) &&
610 column_info[column1+1].left_line) // additional width
611 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
612 tmp = calculate_width_of_column_NMC(column1);
616 for (; i<columns_; ++i)
617 calculate_width_of_column_NMC(i);
618 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
620 if (i < numberofcells)
621 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
622 for (i = 0; i < columns_; ++i)
623 calculate_width_of_column(i);
624 calculate_width_of_tabular();
631 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
633 if (!IsMultiColumn(cell) || onlycolumn)
634 column_info[column_of_cell(cell)].alignment = align;
636 cellinfo_of_cell(cell)->alignment = align;
641 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
643 if (!IsMultiColumn(cell) || onlycolumn)
644 column_info[column_of_cell(cell)].valignment = align;
646 cellinfo_of_cell(cell)->valignment = align;
651 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
653 bool flag = !width.empty();
654 int const j = column_of_cell(cell);
656 column_info[j].p_width = width;
657 if (flag) // do this only if there is a width
658 SetAlignment(cell, LYX_ALIGN_LEFT);
659 for (int i = 0; i < rows_; ++i) {
660 int c = GetCellNumber(i, j);
661 flag = !GetPWidth(c).empty(); // because of multicolumns!
662 GetCellInset(c)->SetAutoBreakRows(flag);
668 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
670 bool const flag = !width.empty();
672 cellinfo_of_cell(cell)->p_width = width;
673 if (IsMultiColumn(cell)) {
674 GetCellInset(cell)->SetAutoBreakRows(flag);
681 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
682 LyXTabular::Feature what)
684 if (what == SET_SPECIAL_MULTI)
685 cellinfo_of_cell(cell)->align_special = special;
687 column_info[column_of_cell(cell)].align_special = special;
692 bool LyXTabular::SetAllLines(int cell, bool line)
694 SetTopLine(cell, line);
695 SetBottomLine(cell, line);
696 SetRightLine(cell, line);
697 SetLeftLine(cell, line);
702 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
704 int const row = row_of_cell(cell);
706 if (onlycolumn || !IsMultiColumn(cell))
707 row_info[row].top_line = line;
709 cellinfo_of_cell(cell)->top_line = line;
714 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
716 if (onlycolumn || !IsMultiColumn(cell))
717 row_info[row_of_cell(cell)].bottom_line = line;
719 cellinfo_of_cell(cell)->bottom_line = line;
724 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
726 if (onlycolumn || !IsMultiColumn(cell))
727 column_info[column_of_cell(cell)].left_line = line;
729 cellinfo_of_cell(cell)->left_line = line;
734 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
736 if (onlycolumn || !IsMultiColumn(cell))
737 column_info[right_column_of_cell(cell)].right_line = line;
739 cellinfo_of_cell(cell)->right_line = line;
744 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
746 if (!onlycolumn && IsMultiColumn(cell))
747 return cellinfo_of_cell(cell)->alignment;
749 return column_info[column_of_cell(cell)].alignment;
753 LyXTabular::VAlignment
754 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
756 if (!onlycolumn && IsMultiColumn(cell))
757 return cellinfo_of_cell(cell)->valignment;
759 return column_info[column_of_cell(cell)].valignment;
763 string const LyXTabular::GetPWidth(int cell) const
765 if (IsMultiColumn(cell))
766 return cellinfo_of_cell(cell)->p_width;
767 return column_info[column_of_cell(cell)].p_width;
771 string const LyXTabular::GetColumnPWidth(int cell) const
773 return column_info[column_of_cell(cell)].p_width;
777 string const LyXTabular::GetMColumnPWidth(int cell) const
779 if (IsMultiColumn(cell))
780 return cellinfo_of_cell(cell)->p_width;
785 string const LyXTabular::GetAlignSpecial(int cell, int what) const
787 if (what == SET_SPECIAL_MULTI)
788 return cellinfo_of_cell(cell)->align_special;
789 return column_info[column_of_cell(cell)].align_special;
793 int LyXTabular::GetWidthOfCell(int cell) const
795 int const row = row_of_cell(cell);
796 int const column1 = column_of_cell(cell);
797 int const column2 = right_column_of_cell(cell);
799 for (int i = column1; i <= column2; ++i) {
800 result += cell_info[row][i].width_of_cell;
806 int LyXTabular::GetBeginningOfTextInCell(int cell) const
810 switch (GetAlignment(cell)){
811 case LYX_ALIGN_CENTER:
812 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
814 case LYX_ALIGN_RIGHT:
815 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
816 // + GetAdditionalWidth(cell);
818 default: /* LYX_ALIGN_LEFT: nothing :-) */
828 bool LyXTabular::IsFirstCellInRow(int cell) const
830 return column_of_cell(cell) == 0;
834 int LyXTabular::GetFirstCellInRow(int row) const
838 return cell_info[row][0].cellno;
841 bool LyXTabular::IsLastCellInRow(int cell) const
843 return (right_column_of_cell(cell) == (columns_ - 1));
847 int LyXTabular::GetLastCellInRow(int row) const
851 return cell_info[row][columns_-1].cellno;
855 bool LyXTabular::calculate_width_of_column(int column)
857 int const old_column_width = column_info[column].width_of_column;
860 for (int i = 0; i < rows_; ++i) {
861 maximum = max(cell_info[i][column].width_of_cell, maximum);
863 column_info[column].width_of_column = maximum;
864 return (column_info[column].width_of_column != old_column_width);
869 // calculate the with of the column without regarding REAL MultiColumn
870 // cells. This means MultiColumn-cells spanning more than 1 column.
872 bool LyXTabular::calculate_width_of_column_NMC(int column)
874 int const old_column_width = column_info[column].width_of_column;
876 for (int i = 0; i < rows_; ++i) {
877 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
878 (cell_info[i][column].width_of_cell > max)) {
879 max = cell_info[i][column].width_of_cell;
882 column_info[column].width_of_column = max;
883 return (column_info[column].width_of_column != old_column_width);
887 void LyXTabular::calculate_width_of_tabular()
889 width_of_tabular = 0;
890 for (int i = 0; i < columns_; ++i) {
891 width_of_tabular += column_info[i].width_of_column;
896 int LyXTabular::row_of_cell(int cell) const
898 if (cell >= numberofcells)
902 return rowofcell[cell];
906 int LyXTabular::column_of_cell(int cell) const
908 if (cell >= numberofcells)
912 return columnofcell[cell];
916 int LyXTabular::right_column_of_cell(int cell) const
918 int const row = row_of_cell(cell);
919 int column = column_of_cell(cell);
920 while (column < (columns_ - 1) &&
921 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
927 // Perfect case for a template... (Lgb)
930 string const write_attribute(string const & name, T const & t)
932 string str = " " + name + "=\"" + tostr(t) + "\"";
937 string const write_attribute(string const & name, bool const & b)
939 return write_attribute(name, int(b));
944 string const write_attribute(string const & name, int value)
946 string str = " " + name + "=\"" + tostr(value) + "\"";
951 string const write_attribute(string const & name, string const & value)
953 string str = " " + name + "=\"" + value + "\"";
958 string const write_attribute(string const & name, bool value)
960 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
966 void LyXTabular::Write(Buffer const * buf, ostream & os) const
970 << write_attribute("version", 1)
971 << write_attribute("rows", rows_)
972 << write_attribute("columns", columns_)
974 // global longtable options
976 << write_attribute("rotate", rotate)
977 << write_attribute("islongtable", is_long_tabular)
978 << write_attribute("endhead", endhead)
979 << write_attribute("endfirsthead", endfirsthead)
980 << write_attribute("endfoot", endfoot)
981 << write_attribute("endlastfoot", endlastfoot)
983 for (int i = 0; i < rows_; ++i) {
985 << write_attribute("topline", row_info[i].top_line)
986 << write_attribute("bottomline", row_info[i].bottom_line)
987 << write_attribute("newpage", row_info[i].newpage)
989 for (int j = 0; j < columns_; ++j) {
992 << write_attribute("alignment", column_info[j].alignment)
993 << write_attribute("valignment", column_info[j].valignment)
994 << write_attribute("leftline", column_info[j].left_line)
995 << write_attribute("rightline", column_info[j].right_line)
996 << write_attribute("width",
997 VSpace(column_info[j].p_width)
999 << write_attribute("special", column_info[j].align_special)
1005 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1006 << write_attribute("alignment", cell_info[i][j].alignment)
1007 << write_attribute("valignment", cell_info[i][j].valignment)
1008 << write_attribute("topline", cell_info[i][j].top_line)
1009 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1010 << write_attribute("leftline", cell_info[i][j].left_line)
1011 << write_attribute("rightline", cell_info[i][j].right_line)
1012 << write_attribute("rotate", cell_info[i][j].rotate)
1013 << write_attribute("usebox", cell_info[i][j].usebox)
1014 << write_attribute("width", cell_info[i][j].p_width)
1015 << write_attribute("special", cell_info[i][j].align_special)
1017 os << "\\begin_inset ";
1018 cell_info[i][j].inset.Write(buf, os);
1019 os << "\n\\end_inset \n"
1025 os << "</LyXTabular>\n";
1030 bool getTokenValue(string const & str, const char * token, string & ret)
1032 string::size_type pos = str.find(token);
1033 char ch = str[pos + strlen(token)];
1035 if ((pos == string::npos) || (ch != '='))
1038 pos += strlen(token) + 1;
1040 if ((ch != '"') && (ch != '\'')) { // only read till next space
1044 while((pos < str.length() - 1) && (str[++pos] != ch))
1052 bool getTokenValue(string const & str, const char * token, int & num)
1054 string::size_type pos = str.find(token);
1055 char ch = str[pos + strlen(token)];
1057 if ((pos == string::npos) || (ch != '='))
1060 pos += strlen(token) + 1;
1062 if ((ch != '"') && (ch != '\'')) { // only read till next space
1068 while((pos < str.length() - 1) && isdigit(str[pos]))
1071 num = strToInt(ret);
1077 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1080 bool const ret = getTokenValue(str, token, tmp);
1081 num = static_cast<LyXAlignment>(tmp);
1087 bool getTokenValue(string const & str, const char * token,
1088 LyXTabular::VAlignment & num)
1091 bool const ret = getTokenValue(str, token, tmp);
1092 num = static_cast<LyXTabular::VAlignment>(tmp);
1098 bool getTokenValue(string const & str, const char * token,
1099 LyXTabular::BoxType & num)
1102 bool ret = getTokenValue(str, token, tmp);
1103 num = static_cast<LyXTabular::BoxType>(tmp);
1109 bool getTokenValue(string const & str, const char * token, bool & flag)
1111 string::size_type pos = str.find(token);
1112 char ch = str[pos + strlen(token)];
1114 if ((pos == string::npos) || (ch != '='))
1117 pos += strlen(token) + 1;
1119 if ((ch != '"') && (ch != '\'')) { // only read till next space
1125 while((pos < str.length() - 1) && isdigit(str[pos]))
1128 flag = strToInt(ret);
1134 void l_getline(istream & is, string & str)
1142 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1145 istream & is = lex.getStream();
1147 l_getline(is, line);
1148 if (!prefixIs(line, "<LyXTabular ")) {
1149 OldFormatRead(lex, line);
1156 if (!getTokenValue(line, "version", version))
1158 if (!getTokenValue(line, "rows", rows_arg))
1160 if (!getTokenValue(line, "columns", columns_arg))
1162 Init(rows_arg, columns_arg);
1163 l_getline(is, line);
1164 if (!prefixIs(line, "<Features ")) {
1165 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1166 line << ")" << endl;
1169 getTokenValue(line, "islongtable", is_long_tabular);
1170 getTokenValue(line, "endhead", endhead);
1171 getTokenValue(line, "endfirsthead", endfirsthead);
1172 getTokenValue(line, "endfoot", endfoot);
1173 getTokenValue(line, "endlastfoot", endlastfoot);
1175 for (int i = 0; i < rows_; ++i) {
1176 l_getline(is, line);
1177 if (!prefixIs(line, "<Row ")) {
1178 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1179 line << ")" << endl;
1182 getTokenValue(line, "topline", row_info[i].top_line);
1183 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1184 getTokenValue(line, "newpage", row_info[i].newpage);
1185 for (int j = 0; j < columns_; ++j) {
1187 if (!prefixIs(line,"<Column")) {
1188 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1189 line << ")" << endl;
1193 getTokenValue(line, "alignment", column_info[j].alignment);
1194 getTokenValue(line, "valignment", column_info[j].valignment);
1195 getTokenValue(line, "leftline", column_info[j].left_line);
1196 getTokenValue(line, "rightline", column_info[j].right_line);
1197 getTokenValue(line, "width", column_info[j].p_width);
1198 getTokenValue(line, "special", column_info[j].align_special);
1200 l_getline(is, line);
1201 if (!prefixIs(line, "<Cell")) {
1202 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1203 line << ")" << endl;
1206 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1207 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1208 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1209 getTokenValue(line, "topline", cell_info[i][j].top_line);
1210 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1211 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1212 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1213 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1214 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1215 getTokenValue(line, "width", cell_info[i][j].p_width);
1216 getTokenValue(line, "special", cell_info[i][j].align_special);
1217 l_getline(is, line);
1218 if (prefixIs(line, "\\begin_inset")) {
1219 cell_info[i][j].inset.Read(buf, lex);
1220 l_getline(is, line);
1222 if (line != "</Cell>") {
1223 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1224 line << ")" << endl;
1227 l_getline(is, line);
1228 if (line != "</Column>") {
1229 lyxerr << "Wrong tabular format (expected </Column> got" <<
1230 line << ")" << endl;
1234 l_getline(is, line);
1235 if (line != "</Row>") {
1236 lyxerr << "Wrong tabular format (expected </Row> got" <<
1237 line << ")" << endl;
1241 while (line != "</LyXTabular>") {
1242 l_getline(is, line);
1244 set_row_column_number_info();
1248 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1253 int columns_arg = 0;
1254 int is_long_tabular_arg = false;
1255 int rotate_arg = false;
1265 istream & is = lex.getStream();
1268 version = lyx::atoi(s.substr(8, string::npos));
1272 vector<int> cont_row_info;
1275 lyxerr << "Tabular format < 5 is not supported anymore\n"
1276 "Get an older version of LyX (< 1.1.x) for conversion!"
1278 WriteAlert(_("Warning:"),
1279 _("Tabular format < 5 is not supported anymore\n"),
1280 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1282 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1283 >> rotate_arg >> a >> b >> c >> d;
1285 is >> rows_arg >> columns_arg;
1286 Init(rows_arg, columns_arg);
1287 cont_row_info = vector<int>(rows_arg);
1288 SetLongTabular(is_long_tabular_arg);
1289 SetRotateTabular(rotate_arg);
1291 for (i = 0; i < rows_; ++i) {
1293 cont_row_info[i] = false;
1295 for (i = 0; i < columns_; ++i) {
1298 for (i = 0; i < rows_; ++i) {
1299 for (j = 0; j < columns_; ++j) {
1304 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1305 >> rotate_arg >> a >> b >> c >> d;
1306 Init(rows_arg, columns_arg);
1307 cont_row_info = vector<int>(rows_arg);
1308 SetLongTabular(is_long_tabular_arg);
1309 SetRotateTabular(rotate_arg);
1314 for (i = 0; i < rows_; ++i) {
1315 a = b = c = d = e = f = g = h = 0;
1316 is >> a >> b >> c >> d;
1317 row_info[i].top_line = a;
1318 row_info[i].bottom_line = b;
1319 cont_row_info[i] = c;
1320 row_info[i].newpage = d;
1322 for (i = 0; i < columns_; ++i) {
1327 char ch; // skip '"'
1330 // ignore is buggy but we will use it later (Lgb)
1331 is.ignore(); // skip '"'
1333 getline(is, s1, '"');
1335 is >> ch; // skip '"'
1337 // ignore is buggy but we will use it later (Lgb)
1338 is.ignore(); // skip '"'
1340 getline(is, s2, '"');
1341 column_info[i].alignment = static_cast<LyXAlignment>(a);
1342 column_info[i].left_line = b;
1343 column_info[i].right_line = c;
1344 column_info[i].p_width = s1;
1345 column_info[i].align_special = s2;
1347 for (i = 0; i < rows_; ++i) {
1348 for (j = 0; j < columns_; ++j) {
1351 is >> a >> b >> c >> d >> e >> f >> g;
1354 is >> ch; // skip '"'
1356 // ignore is buggy but we will use it later (Lgb)
1357 is.ignore(); // skip '"'
1359 getline(is, s1, '"');
1361 is >> ch; // skip '"'
1363 // ignore is buggy but we will use it later (Lgb)
1364 is.ignore(); // skip '"'
1366 getline(is, s2, '"');
1367 cell_info[i][j].multicolumn = static_cast<char>(a);
1368 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1369 cell_info[i][j].top_line = static_cast<char>(c);
1370 cell_info[i][j].bottom_line = static_cast<char>(d);
1371 cell_info[i][j].left_line = column_info[j].left_line;
1372 cell_info[i][j].right_line = column_info[j].right_line;
1373 cell_info[i][j].rotate = static_cast<bool>(f);
1374 cell_info[i][j].usebox = static_cast<BoxType>(g);
1375 cell_info[i][j].align_special = s1;
1376 cell_info[i][j].p_width = s2;
1380 set_row_column_number_info(true);
1382 LyXParagraph * par = new LyXParagraph;
1383 LyXParagraph * return_par = 0;
1385 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1386 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1391 LyXFont font(LyXFont::ALL_SANE);
1392 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1394 while (lex.IsOK()) {
1396 string const token = lex.GetString();
1399 if (token == "\\layout"
1400 || token == "\\end_float"
1401 || token == "\\end_deeper") {
1402 lex.pushToken(token);
1405 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1417 lex.pushToken(token);
1421 lex.printError("New Paragraph allocated! This should not happen!");
1422 lex.pushToken(token);
1428 // now we have the par we should fill the insets with this!
1430 InsetText * inset = GetCellInset(cell);
1433 for (int i = 0; i < par->Last(); ++i) {
1434 if (par->IsNewline(i)) {
1436 if (cell > GetNumberOfCells()) {
1437 lyxerr << "Some error in reading old table format occured!" <<
1438 endl << "Terminating when reading cell[" << cell << "]!" <<
1443 row = row_of_cell(cell);
1444 if (cont_row_info[row]) {
1446 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1447 while(!IsFirstCellInRow(--cell));
1449 inset = GetCellInset(cell);
1452 inset = GetCellInset(cell);
1453 row = row_of_cell(cell);
1454 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1456 // insert a space instead
1458 par->InsertChar(i, ' ');
1461 par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
1462 inset->par->InsertFromMinibuffer(inset->par->Last());
1469 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1471 int const i = isColumn ? cell : column_of_cell(cell);
1473 if (!isColumn && IsMultiColumn(cell)) {
1474 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1475 return cellinfo_of_cell(cell)->align_special;
1477 switch (GetAlignment(cell)) {
1478 case LYX_ALIGN_LEFT:
1480 case LYX_ALIGN_RIGHT:
1487 if (!column_info[i].align_special.empty()) {
1488 return column_info[i].align_special;
1490 #ifdef IGNORE_THIS_FOR_NOW
1491 else if (!column_info[i].p_width.empty()) {
1493 file += column_info[i].p_width;
1498 switch (column_info[i].alignment) {
1499 case LYX_ALIGN_LEFT:
1501 case LYX_ALIGN_RIGHT:
1511 // cell <0 will tex the preamble
1512 // returns the number of printed newlines
1513 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1516 if (IsLastCell(cell)) {
1517 os << newlineAndDepth(--depth)
1519 << newlineAndDepth(--depth)
1521 << newlineAndDepth(--depth)
1523 << newlineAndDepth(--depth);
1524 if (is_long_tabular)
1528 << newlineAndDepth(--depth);
1533 if (is_long_tabular)
1539 << "' COLSEP='1' ROWSEP='1'>"
1540 << newlineAndDepth(++depth);
1542 for (int i = 0; i < columns_; ++i) {
1543 os << "<COLSPEC ALIGN='"
1544 << GetDocBookAlign(i, true)
1550 if (i == (columns_-1)) {
1553 if (column_info[i].right_line ||
1554 column_info[i+1].left_line)
1560 << newlineAndDepth(depth);
1562 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1563 if (column_info[i].left_line)
1568 << newlineAndDepth(++depth)
1570 << newlineAndDepth(++depth)
1572 << GetDocBookAlign(0)
1574 if (IsMultiColumn(0)) {
1575 os << " NAMEST='col1' NAMEEND='col"
1576 << cells_in_multicolumn(0)
1580 << newlineAndDepth(++depth);
1583 if (IsLastCellInRow(cell)) {
1584 os << newlineAndDepth(--depth)
1586 << newlineAndDepth(--depth)
1588 << newlineAndDepth(depth)
1590 << newlineAndDepth(++depth)
1592 << GetDocBookAlign(cell + 1)
1593 << "' VALIGN='middle'";
1594 if (IsMultiColumn(cell + 1)) {
1595 os << " NAMEST='col"
1596 << column_of_cell(cell + 1) + 1
1598 << column_of_cell(cell + 1) +
1599 cells_in_multicolumn(cell + 1)
1603 << newlineAndDepth(++depth);
1606 os << newlineAndDepth(--depth)
1608 << newlineAndDepth(depth)
1610 << GetDocBookAlign(cell + 1)
1611 << "' VALIGN='middle'";
1612 if (IsMultiColumn(cell + 1)) {
1613 os << " NAMEST='col"
1614 << column_of_cell(cell + 1) + 1
1616 << column_of_cell(cell + 1) +
1617 cells_in_multicolumn(cell + 1)
1621 << newlineAndDepth(++depth);
1630 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1632 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1633 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1637 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1639 int const row = row_of_cell(cell);
1640 int const column = column_of_cell(cell);
1641 return &cell_info[row][column];
1645 void LyXTabular::SetMultiColumn(int cell, int number)
1647 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1648 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1649 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1650 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1651 for (number--; number > 0; --number) {
1652 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1654 set_row_column_number_info();
1658 int LyXTabular::cells_in_multicolumn(int cell) const
1660 int const row = row_of_cell(cell);
1661 int column = column_of_cell(cell);
1664 while ((column < columns_) &&
1665 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1674 int LyXTabular::UnsetMultiColumn(int cell)
1676 int const row = row_of_cell(cell);
1677 int column = column_of_cell(cell);
1681 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1682 cell_info[row][column].multicolumn = CELL_NORMAL;
1684 while ((column < columns_) &&
1685 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1687 cell_info[row][column].multicolumn = CELL_NORMAL;
1692 set_row_column_number_info();
1697 void LyXTabular::SetLongTabular(bool what)
1699 is_long_tabular = what;
1703 bool LyXTabular::IsLongTabular() const
1705 return is_long_tabular;
1709 void LyXTabular::SetRotateTabular(bool flag)
1715 bool LyXTabular::GetRotateTabular() const
1721 void LyXTabular::SetRotateCell(int cell, bool flag)
1723 cellinfo_of_cell(cell)->rotate = flag;
1727 bool LyXTabular::GetRotateCell(int cell) const
1729 return cellinfo_of_cell(cell)->rotate;
1733 bool LyXTabular::NeedRotating() const
1737 for (int i = 0; i < rows_; ++i) {
1738 for (int j = 0; j < columns_; ++j) {
1739 if (cell_info[i][j].rotate)
1747 bool LyXTabular::IsLastCell(int cell) const
1749 if ((cell + 1) < GetNumberOfCells())
1755 int LyXTabular::GetCellAbove(int cell) const
1757 if (row_of_cell(cell) > 0)
1758 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1763 int LyXTabular::GetCellBelow(int cell) const
1765 if (row_of_cell(cell) + 1 < rows_)
1766 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1771 int LyXTabular::GetLastCellAbove(int cell) const
1773 if (row_of_cell(cell) <= 0)
1775 if (!IsMultiColumn(cell))
1776 return GetCellAbove(cell);
1777 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1781 int LyXTabular::GetLastCellBelow(int cell) const
1783 if (row_of_cell(cell) + 1 >= rows_)
1785 if (!IsMultiColumn(cell))
1786 return GetCellBelow(cell);
1787 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1791 int LyXTabular::GetCellNumber(int row, int column) const
1793 if (column >= columns_)
1794 column = columns_ - 1;
1795 else if (column < 0)
1802 return cell_info[row][column].cellno;
1806 void LyXTabular::SetUsebox(int cell, BoxType type)
1808 cellinfo_of_cell(cell)->usebox = type;
1812 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1814 if (column_info[column_of_cell(cell)].p_width.empty() &&
1815 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1817 if (cellinfo_of_cell(cell)->usebox > 1)
1818 return cellinfo_of_cell(cell)->usebox;
1819 return UseParbox(cell);
1823 void LyXTabular::SetLTHead(int cell, bool first)
1825 int const row = row_of_cell(cell);
1826 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1829 if (endfirsthead == val)
1842 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1845 if (abs(endhead) > rows_)
1847 return (row_of_cell(cell) == abs(endhead) - 1);
1851 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1854 if (abs(endfirsthead) > rows_)
1856 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1860 void LyXTabular::SetLTFoot(int cell, bool last)
1862 int const row = row_of_cell(cell);
1863 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1866 if (endlastfoot == val)
1879 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1882 if ((endfoot + 1) > rows_)
1884 return (row_of_cell(cell) == abs(endfoot) - 1);
1888 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1891 if (abs(endlastfoot) > rows_)
1893 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1897 void LyXTabular::SetLTNewPage(int cell, bool what)
1899 row_info[row_of_cell(cell)].newpage = what;
1903 bool LyXTabular::GetLTNewPage(int cell) const
1905 return row_info[row_of_cell(cell)].newpage;
1909 bool LyXTabular::SetAscentOfRow(int row, int height)
1911 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1913 row_info[row].ascent_of_row = height;
1918 bool LyXTabular::SetDescentOfRow(int row, int height)
1920 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1922 row_info[row].descent_of_row = height;
1927 int LyXTabular::GetAscentOfRow(int row) const
1931 return row_info[row].ascent_of_row;
1935 int LyXTabular::GetDescentOfRow(int row) const
1939 return row_info[row].descent_of_row;
1943 int LyXTabular::GetHeightOfTabular() const
1947 for (int row = 0; row < rows_; ++row)
1948 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1949 GetAdditionalHeight(GetCellNumber(row, 0));
1954 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1956 if ((row >= rows_) || (column >= columns_))
1958 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1962 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1964 int const fcell = GetFirstCellInRow(row);
1965 int const n = NumberOfCellsInRow(fcell) + fcell;
1968 for (int i = fcell; i < n; ++i) {
1972 if (tmp == (n - fcell)){
1975 for (int i = fcell; i < n; ++i) {
1978 << column_of_cell(i) + 1
1980 << right_column_of_cell(i) + 1
1992 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1994 int const fcell = GetFirstCellInRow(row);
1995 int const n = NumberOfCellsInRow(fcell) + fcell;
1998 for (int i = fcell; i < n; ++i) {
2002 if (tmp == (n-fcell)){
2005 for (int i = fcell; i < n; ++i) {
2006 if (BottomLine(i)) {
2008 << column_of_cell(i) + 1
2010 << right_column_of_cell(i) + 1
2022 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2026 if (GetRotateCell(cell)) {
2027 os << "\\begin{sideways}\n";
2030 if (IsMultiColumn(cell)) {
2031 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2032 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
2033 os << cellinfo_of_cell(cell+1)->align_special << "}{";
2037 if (!GetPWidth(cell).empty()) {
2038 switch (GetVAlignment(cell)) {
2039 case LYX_VALIGN_TOP:
2042 case LYX_VALIGN_CENTER:
2045 case LYX_VALIGN_BOTTOM:
2049 os << "{" << GetPWidth(cell) << '}';
2051 switch (GetAlignment(cell)) {
2052 case LYX_ALIGN_LEFT:
2055 case LYX_ALIGN_RIGHT:
2063 if (RightLine(cell))
2065 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2071 if (GetUsebox(cell) == BOX_PARBOX) {
2073 switch (GetVAlignment(cell)) {
2074 case LYX_VALIGN_TOP:
2077 case LYX_VALIGN_CENTER:
2080 case LYX_VALIGN_BOTTOM:
2084 os << "]{" << GetPWidth(cell) << "}{";
2085 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2086 os << "\\begin{minipage}[";
2087 switch (GetVAlignment(cell)) {
2088 case LYX_VALIGN_TOP:
2091 case LYX_VALIGN_CENTER:
2094 case LYX_VALIGN_BOTTOM:
2098 os << "]{" << GetPWidth(cell) << "}\n";
2105 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2110 if (GetUsebox(cell) == BOX_PARBOX)
2112 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2113 os << "%\n\\end{minipage}";
2116 if (IsMultiColumn(cell)){
2119 if (GetRotateCell(cell)) {
2120 os << "%\n\\end{sideways}";
2127 int LyXTabular::Latex(Buffer const * buf,
2128 ostream & os, bool fragile, bool fp) const
2133 //+---------------------------------------------------------------------
2134 //+ first the opening preamble +
2135 //+---------------------------------------------------------------------
2138 os << "\\begin{sideways}\n";
2141 if (is_long_tabular)
2142 os << "\\begin{longtable}{";
2144 os << "\\begin{tabular}{";
2145 for (int i = 0; i < columns_; ++i) {
2146 if (column_info[i].left_line)
2148 if (!column_info[i].align_special.empty()) {
2149 os << column_info[i].align_special;
2150 } else if (!column_info[i].p_width.empty()) {
2151 switch (column_info[i].valignment) {
2152 case LYX_VALIGN_TOP:
2155 case LYX_VALIGN_CENTER:
2158 case LYX_VALIGN_BOTTOM:
2163 << column_info[i].p_width
2166 switch (column_info[i].alignment) {
2167 case LYX_ALIGN_LEFT:
2170 case LYX_ALIGN_RIGHT:
2178 if (column_info[i].right_line)
2184 //+---------------------------------------------------------------------
2185 //+ the single row and columns (cells) +
2186 //+---------------------------------------------------------------------
2189 for (int i = 0; i < rows_; ++i) {
2190 ret += TeXTopHLine(os, i);
2192 if (IsLongTabular()) {
2193 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2194 os << "\\endhead\n";
2197 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2198 os << "\\endfirsthead\n";
2201 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2202 os << "\\endfoot\n";
2205 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2206 os << "\\endlastfoot\n";
2212 ret += TeXBottomHLine(os, i-1);
2213 ret += TeXTopHLine(os, i);
2215 for (int j = 0; j < columns_; ++j) {
2216 if (IsPartOfMultiColumn(i,j))
2218 ret += TeXCellPreamble(os, cell);
2219 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2220 ret += TeXCellPostamble(os, cell);
2221 if (!IsLastCellInRow(cell)) { // not last cell in row
2228 ret += TeXBottomHLine(os, i);
2230 if (IsLongTabular()) {
2231 if ((endhead > 0) && (i == (endhead - 1))) {
2232 os << "\\endhead\n";
2235 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2236 os << "\\endfirsthead\n";
2239 if ((endfoot > 0) && (i == (endfoot - 1))) {
2240 os << "\\endfoot\n";
2243 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2244 os << "\\endlastfoot\n";
2248 ret += TeXBottomHLine(os, i);
2249 if (row_info[i].newpage) {
2250 os << "\\newpage\n";
2256 //+---------------------------------------------------------------------
2257 //+ the closing of the tabular +
2258 //+---------------------------------------------------------------------
2260 if (is_long_tabular)
2261 os << "\\end{longtable}";
2263 os << "\\end{tabular}";
2265 os << "\n\\end{sideways}";
2273 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2277 //+---------------------------------------------------------------------
2278 //+ first the opening preamble +
2279 //+---------------------------------------------------------------------
2281 os << "<tgroup cols=\"" << columns_
2282 << "\" colsep=\"1\" rowsep=\"1\">\n";
2284 for (int i = 0; i < columns_; ++i) {
2285 os << "<colspec colname=\"col" << i << "\" align=\"";
2286 switch (column_info[i].alignment) {
2287 case LYX_ALIGN_LEFT:
2290 case LYX_ALIGN_RIGHT:
2301 //+---------------------------------------------------------------------
2302 //+ the single row and columns (cells) +
2303 //+---------------------------------------------------------------------
2307 for (int i = 0; i < rows_; ++i) {
2309 for (int j = 0; j < columns_; ++j) {
2310 if (IsPartOfMultiColumn(i, j))
2313 os << "<entry align=\"";
2314 switch (GetAlignment(cell)) {
2315 case LYX_ALIGN_LEFT:
2318 case LYX_ALIGN_RIGHT:
2326 os << "\" valign=\"";
2327 switch (GetVAlignment(cell)) {
2328 case LYX_VALIGN_TOP:
2331 case LYX_VALIGN_BOTTOM:
2334 case LYX_VALIGN_CENTER:
2339 if (IsMultiColumn(cell)) {
2340 os << " namest=\"col" << j << "\" ";
2341 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2345 ret += GetCellInset(cell)->DocBook(buf, os);
2352 //+---------------------------------------------------------------------
2353 //+ the closing of the tabular +
2354 //+---------------------------------------------------------------------
2365 void print_n_chars(ostream & os, unsigned char ch, int n)
2368 for (int i = 0; i < n; ++i)
2371 os << string(n, ch);
2376 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2377 vector<unsigned int> const & clen) const
2379 int const fcell = GetFirstCellInRow(row);
2380 int const n = NumberOfCellsInRow(fcell) + fcell;
2385 for (int i = fcell; i < n; ++i) {
2392 // Isn't this equivalent? (Lgb)
2393 for (int i = fcell; i < n; ++i) {
2400 for (int i = fcell; i < n; ++i) {
2411 int column = column_of_cell(i);
2412 int len = clen[column];
2413 while(IsPartOfMultiColumn(row, ++column))
2414 len += clen[column] + 4;
2415 print_n_chars(os, ch, len);
2430 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2431 vector<unsigned int> const & clen) const
2433 int const fcell = GetFirstCellInRow(row);
2434 int const n = NumberOfCellsInRow(fcell) + fcell;
2439 for (int i = fcell; i < n; ++i) {
2446 // Isn't this equivalent? (Lgb)
2447 for (int i = fcell; i < n; ++i) {
2453 for (int i = fcell; i < n; ++i) {
2454 if (BottomLine(i)) {
2464 int column = column_of_cell(i);
2465 int len = clen[column];
2466 while(IsPartOfMultiColumn(row, ++column))
2467 len += clen[column] + 4;
2468 print_n_chars(os, ch, len);
2469 if (BottomLine(i)) {
2483 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2484 int cell, int row, int column,
2485 vector<unsigned int> const & clen) const
2488 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2495 unsigned int len1 = sstr.str().length();
2496 unsigned int len2 = clen[column];
2497 while(IsPartOfMultiColumn(row, ++column))
2498 len2 += clen[column] + 4;
2501 switch (GetAlignment(cell)) {
2503 case LYX_ALIGN_LEFT:
2506 case LYX_ALIGN_RIGHT:
2510 case LYX_ALIGN_CENTER:
2516 for (unsigned int i = 0; i < len1; ++i)
2519 for (unsigned int i = 0; i < len2; ++i)
2521 if (RightLine(cell))
2526 return ret * 0; // eh? (Lgb)
2530 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2534 //+---------------------------------------------------------------------
2535 //+ first calculate the width of the single columns +
2536 //+---------------------------------------------------------------------
2537 vector<unsigned int> clen(columns_);
2539 // first all non (real) multicolumn cells!
2540 for (int j = 0; j < columns_; ++j) {
2542 for (int i = 0; i < rows_; ++i) {
2543 int cell = GetCellNumber(i, j);
2544 if (IsMultiColumn(cell, true))
2547 GetCellInset(cell)->Ascii(buf, sstr, 0);
2548 if (clen[j] < sstr.str().length())
2549 clen[j] = sstr.str().length();
2552 // then all (real) multicolumn cells!
2553 for (int j = 0; j < columns_; ++j) {
2554 for (int i = 0; i < rows_; ++i) {
2555 int cell = GetCellNumber(i, j);
2556 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2559 GetCellInset(cell)->Ascii(buf, sstr, 0);
2560 int len = int(sstr.str().length());
2561 int const n = cells_in_multicolumn(cell);
2562 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2564 if (len > int(clen[j + n - 1]))
2565 clen[j + n - 1] = len;
2569 for (int i = 0; i < rows_; ++i) {
2570 AsciiTopHLine(os, i, clen);
2571 for (int j = 0; j < columns_; ++j) {
2572 if (IsPartOfMultiColumn(i,j))
2574 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2578 AsciiBottomHLine(os, i, clen);
2584 InsetText * LyXTabular::GetCellInset(int cell) const
2586 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2590 void LyXTabular::Validate(LaTeXFeatures & features) const
2592 if (IsLongTabular())
2593 features.longtable = true;
2595 features.rotating = true;
2596 for (int cell = 0; cell < numberofcells; ++cell) {
2597 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2598 features.array = true;
2599 GetCellInset(cell)->Validate(features);
2604 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2606 LyXParagraph * par = GetCellInset(cell)->par;
2608 for (; par; par = par->next) {
2609 for (int i = 0; i < par->Last(); ++i) {
2610 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)