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 #warning Please check this Jürgen.
346 // if ok please delete the #if 0 section above. (Lgb)
347 for (int row = 0, column = 0, c = 0;
348 c < numberofcells && row < rows_ && column < columns_;) {
350 columnofcell[c] = column;
354 } while (column < columns_ &&
355 cell_info[row][column].multicolumn
356 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
357 if (column == columns_) {
364 for (int row = 0; row < rows_; ++row) {
365 for (int column = 0; column < columns_; ++column) {
366 if (IsPartOfMultiColumn(row,column))
368 // now set the right line of multicolumns right for oldformat read
370 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
372 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
373 cell_info[row][column].right_line =
374 cell_info[row][column+cn-1].right_line;
376 cell_info[row][column].inset.SetAutoBreakRows(
377 !GetPWidth(GetCellNumber(row, column)).empty());
383 int LyXTabular::GetNumberOfCells() const
385 return numberofcells;
389 int LyXTabular::NumberOfCellsInRow(int cell) const
391 int const row = row_of_cell(cell);
393 for (int i = 0; i < columns_; ++i) {
394 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
401 /* returns 1 if there is a topline, returns 0 if not */
402 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
404 int const row = row_of_cell(cell);
406 if (!onlycolumn && IsMultiColumn(cell))
407 return cellinfo_of_cell(cell)->top_line;
408 return row_info[row].top_line;
412 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
414 // no bottom line underneath non-existent cells if you please
415 // Isn't that a programming error? Is so this should
416 // be an Assert instead. (Lgb)
417 if (cell >= numberofcells)
420 if (!onlycolumn && IsMultiColumn(cell))
421 return cellinfo_of_cell(cell)->bottom_line;
422 return row_info[row_of_cell(cell)].bottom_line;
426 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
428 if (!onlycolumn && IsMultiColumn(cell))
429 return cellinfo_of_cell(cell)->left_line;
430 return column_info[column_of_cell(cell)].left_line;
434 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
436 if (!onlycolumn && IsMultiColumn(cell))
437 return cellinfo_of_cell(cell)->right_line;
438 return column_info[right_column_of_cell(cell)].right_line;
442 bool LyXTabular::TopAlreadyDrawed(int cell) const
444 if (GetAdditionalHeight(cell))
446 int row = row_of_cell(cell);
448 int column = column_of_cell(cell);
451 && cell_info[row][column].multicolumn
452 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
454 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
455 return row_info[row].bottom_line;
457 return cell_info[row][column].bottom_line;
463 bool LyXTabular::LeftAlreadyDrawed(int cell) const
465 int column = column_of_cell(cell);
467 int row = row_of_cell(cell);
469 (cell_info[row][column].multicolumn ==
470 LyXTabular::CELL_PART_OF_MULTICOLUMN));
471 if (GetAdditionalWidth(cell_info[row][column].cellno))
473 return column_info[column].right_line;
479 bool LyXTabular::IsLastRow(int cell) const
481 return (row_of_cell(cell) == rows_ - 1);
485 int LyXTabular::GetAdditionalHeight(int cell) const
487 int const row = row_of_cell(cell);
493 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
494 switch (cell_info[row - 1][column].multicolumn) {
495 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
496 bottom = cell_info[row - 1][column].bottom_line;
498 case LyXTabular::CELL_NORMAL:
499 bottom = row_info[row - 1].bottom_line;
502 for (int column = 0; column < columns_ - 1 && top; ++column) {
503 switch (cell_info[row][column].multicolumn){
504 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
505 top = cell_info[row][column].top_line;
507 case LyXTabular::CELL_NORMAL:
508 top = row_info[row].top_line;
512 return WIDTH_OF_LINE;
517 int LyXTabular::GetAdditionalWidth(int cell) const
519 // internally already set in SetWidthOfCell
520 // used to get it back in text.C
521 int const col = right_column_of_cell(cell);
522 if (col < columns_ - 1 && column_info[col].right_line &&
523 column_info[col+1].left_line)
524 return WIDTH_OF_LINE;
530 // returns the maximum over all rows
531 int LyXTabular::GetWidthOfColumn(int cell) const
533 int const column1 = column_of_cell(cell);
534 int const column2 = right_column_of_cell(cell);
536 for (int i = column1; i <= column2; ++i) {
537 result += column_info[i].width_of_column;
543 int LyXTabular::GetWidthOfTabular() const
545 return width_of_tabular;
549 /* returns 1 if a complete update is necessary, otherwise 0 */
550 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
552 if (!IsMultiColumn(cell))
555 int const row = row_of_cell(cell);
556 int const column1 = column_of_cell(cell);
557 int const column2 = right_column_of_cell(cell);
559 // first set columns to 0 so we can calculate the right width
560 for (int i = column1; i <= column2; ++i) {
561 cell_info[row][i].width_of_cell = 0;
563 // set the width to MAX_WIDTH until width > 0
564 int width = (new_width + 2 * WIDTH_OF_LINE);
567 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
568 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
569 width -= column_info[i].width_of_column;
572 cell_info[row][i].width_of_cell = width;
578 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
580 int const row = row_of_cell(cell);
581 int const column1 = column_of_cell(cell);
582 int const column2 = right_column_of_cell(cell);
584 // first set columns to 0 so we can calculate the right width
586 for (; i <= column2; ++i)
587 cell_info[row][i].width_of_cell = 0;
588 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
590 if (i < numberofcells)
591 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
592 SetWidthOfMulticolCell(cell, new_width);
596 /* returns 1 if a complete update is necessary, otherwise 0 */
597 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
599 int const row = row_of_cell(cell);
600 int const column1 = column_of_cell(cell);
604 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
606 if (IsMultiColumn(cell, true)) {
607 tmp = SetWidthOfMulticolCell(cell, new_width);
609 width = (new_width + 2*WIDTH_OF_LINE);
610 cell_info[row][column1].width_of_cell = width;
611 if (column_info[column1].right_line && (column1 < columns_-1) &&
612 column_info[column1+1].left_line) // additional width
613 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
614 tmp = calculate_width_of_column_NMC(column1);
618 for (; i<columns_; ++i)
619 calculate_width_of_column_NMC(i);
620 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
622 if (i < numberofcells)
623 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
624 for (i = 0; i < columns_; ++i)
625 calculate_width_of_column(i);
626 calculate_width_of_tabular();
633 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
635 if (!IsMultiColumn(cell) || onlycolumn)
636 column_info[column_of_cell(cell)].alignment = align;
638 cellinfo_of_cell(cell)->alignment = align;
643 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
645 if (!IsMultiColumn(cell) || onlycolumn)
646 column_info[column_of_cell(cell)].valignment = align;
648 cellinfo_of_cell(cell)->valignment = align;
653 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
655 bool flag = !width.empty();
656 int const j = column_of_cell(cell);
658 column_info[j].p_width = width;
659 if (flag) // do this only if there is a width
660 SetAlignment(cell, LYX_ALIGN_LEFT);
661 for (int i = 0; i < rows_; ++i) {
662 int c = GetCellNumber(i, j);
663 flag = !GetPWidth(c).empty(); // because of multicolumns!
664 GetCellInset(c)->SetAutoBreakRows(flag);
670 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
672 bool const flag = !width.empty();
674 cellinfo_of_cell(cell)->p_width = width;
675 if (IsMultiColumn(cell)) {
676 GetCellInset(cell)->SetAutoBreakRows(flag);
683 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
684 LyXTabular::Feature what)
686 if (what == SET_SPECIAL_MULTI)
687 cellinfo_of_cell(cell)->align_special = special;
689 column_info[column_of_cell(cell)].align_special = special;
694 bool LyXTabular::SetAllLines(int cell, bool line)
696 SetTopLine(cell, line);
697 SetBottomLine(cell, line);
698 SetRightLine(cell, line);
699 SetLeftLine(cell, line);
704 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
706 int const row = row_of_cell(cell);
708 if (onlycolumn || !IsMultiColumn(cell))
709 row_info[row].top_line = line;
711 cellinfo_of_cell(cell)->top_line = line;
716 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
718 if (onlycolumn || !IsMultiColumn(cell))
719 row_info[row_of_cell(cell)].bottom_line = line;
721 cellinfo_of_cell(cell)->bottom_line = line;
726 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
728 if (onlycolumn || !IsMultiColumn(cell))
729 column_info[column_of_cell(cell)].left_line = line;
731 cellinfo_of_cell(cell)->left_line = line;
736 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
738 if (onlycolumn || !IsMultiColumn(cell))
739 column_info[right_column_of_cell(cell)].right_line = line;
741 cellinfo_of_cell(cell)->right_line = line;
746 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
748 if (!onlycolumn && IsMultiColumn(cell))
749 return cellinfo_of_cell(cell)->alignment;
751 return column_info[column_of_cell(cell)].alignment;
755 LyXTabular::VAlignment
756 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
758 if (!onlycolumn && IsMultiColumn(cell))
759 return cellinfo_of_cell(cell)->valignment;
761 return column_info[column_of_cell(cell)].valignment;
765 string const LyXTabular::GetPWidth(int cell) const
767 if (IsMultiColumn(cell))
768 return cellinfo_of_cell(cell)->p_width;
769 return column_info[column_of_cell(cell)].p_width;
773 string const LyXTabular::GetColumnPWidth(int cell) const
775 return column_info[column_of_cell(cell)].p_width;
779 string const LyXTabular::GetMColumnPWidth(int cell) const
781 if (IsMultiColumn(cell))
782 return cellinfo_of_cell(cell)->p_width;
787 string const LyXTabular::GetAlignSpecial(int cell, int what) const
789 if (what == SET_SPECIAL_MULTI)
790 return cellinfo_of_cell(cell)->align_special;
791 return column_info[column_of_cell(cell)].align_special;
795 int LyXTabular::GetWidthOfCell(int cell) const
797 int const row = row_of_cell(cell);
798 int const column1 = column_of_cell(cell);
799 int const column2 = right_column_of_cell(cell);
801 for (int i = column1; i <= column2; ++i) {
802 result += cell_info[row][i].width_of_cell;
808 int LyXTabular::GetBeginningOfTextInCell(int cell) const
812 switch (GetAlignment(cell)){
813 case LYX_ALIGN_CENTER:
814 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
816 case LYX_ALIGN_RIGHT:
817 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
818 // + GetAdditionalWidth(cell);
820 default: /* LYX_ALIGN_LEFT: nothing :-) */
830 bool LyXTabular::IsFirstCellInRow(int cell) const
832 return column_of_cell(cell) == 0;
836 int LyXTabular::GetFirstCellInRow(int row) const
840 return cell_info[row][0].cellno;
843 bool LyXTabular::IsLastCellInRow(int cell) const
845 return (right_column_of_cell(cell) == (columns_ - 1));
849 int LyXTabular::GetLastCellInRow(int row) const
853 return cell_info[row][columns_-1].cellno;
857 bool LyXTabular::calculate_width_of_column(int column)
859 int const old_column_width = column_info[column].width_of_column;
862 for (int i = 0; i < rows_; ++i) {
863 maximum = max(cell_info[i][column].width_of_cell, maximum);
865 column_info[column].width_of_column = maximum;
866 return (column_info[column].width_of_column != old_column_width);
871 // calculate the with of the column without regarding REAL MultiColumn
872 // cells. This means MultiColumn-cells spanning more than 1 column.
874 bool LyXTabular::calculate_width_of_column_NMC(int column)
876 int const old_column_width = column_info[column].width_of_column;
878 for (int i = 0; i < rows_; ++i) {
879 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
880 (cell_info[i][column].width_of_cell > max)) {
881 max = cell_info[i][column].width_of_cell;
884 column_info[column].width_of_column = max;
885 return (column_info[column].width_of_column != old_column_width);
889 void LyXTabular::calculate_width_of_tabular()
891 width_of_tabular = 0;
892 for (int i = 0; i < columns_; ++i) {
893 width_of_tabular += column_info[i].width_of_column;
898 int LyXTabular::row_of_cell(int cell) const
900 if (cell >= numberofcells)
904 return rowofcell[cell];
908 int LyXTabular::column_of_cell(int cell) const
910 if (cell >= numberofcells)
914 return columnofcell[cell];
918 int LyXTabular::right_column_of_cell(int cell) const
920 int const row = row_of_cell(cell);
921 int column = column_of_cell(cell);
922 while (column < (columns_ - 1) &&
923 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
929 // Perfect case for a template... (Lgb)
932 string const write_attribute(string const & name, T const & t)
934 string str = " " + name + "=\"" + tostr(t) + "\"";
939 string const write_attribute(string const & name, bool const & b)
941 return write_attribute(name, int(b));
946 string const write_attribute(string const & name, int value)
948 string str = " " + name + "=\"" + tostr(value) + "\"";
953 string const write_attribute(string const & name, string const & value)
955 string str = " " + name + "=\"" + value + "\"";
960 string const write_attribute(string const & name, bool value)
962 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
968 void LyXTabular::Write(Buffer const * buf, ostream & os) const
972 << write_attribute("version", 1)
973 << write_attribute("rows", rows_)
974 << write_attribute("columns", columns_)
976 // global longtable options
978 << write_attribute("rotate", rotate)
979 << write_attribute("islongtable", is_long_tabular)
980 << write_attribute("endhead", endhead)
981 << write_attribute("endfirsthead", endfirsthead)
982 << write_attribute("endfoot", endfoot)
983 << write_attribute("endlastfoot", endlastfoot)
985 for (int i = 0; i < rows_; ++i) {
987 << write_attribute("topline", row_info[i].top_line)
988 << write_attribute("bottomline", row_info[i].bottom_line)
989 << write_attribute("newpage", row_info[i].newpage)
991 for (int j = 0; j < columns_; ++j) {
994 << write_attribute("alignment", column_info[j].alignment)
995 << write_attribute("valignment", column_info[j].valignment)
996 << write_attribute("leftline", column_info[j].left_line)
997 << write_attribute("rightline", column_info[j].right_line)
998 << write_attribute("width",
999 VSpace(column_info[j].p_width)
1001 << write_attribute("special", column_info[j].align_special)
1007 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1008 << write_attribute("alignment", cell_info[i][j].alignment)
1009 << write_attribute("valignment", cell_info[i][j].valignment)
1010 << write_attribute("topline", cell_info[i][j].top_line)
1011 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1012 << write_attribute("leftline", cell_info[i][j].left_line)
1013 << write_attribute("rightline", cell_info[i][j].right_line)
1014 << write_attribute("rotate", cell_info[i][j].rotate)
1015 << write_attribute("usebox", cell_info[i][j].usebox)
1016 << write_attribute("width", cell_info[i][j].p_width)
1017 << write_attribute("special", cell_info[i][j].align_special)
1019 os << "\\begin_inset ";
1020 cell_info[i][j].inset.Write(buf, os);
1021 os << "\n\\end_inset \n"
1027 os << "</LyXTabular>\n";
1032 bool getTokenValue(string const & str, const char * token, string & ret)
1034 string::size_type pos = str.find(token);
1035 char ch = str[pos + strlen(token)];
1037 if ((pos == string::npos) || (ch != '='))
1040 pos += strlen(token) + 1;
1042 if ((ch != '"') && (ch != '\'')) { // only read till next space
1046 while((pos < str.length() - 1) && (str[++pos] != ch))
1054 bool getTokenValue(string const & str, const char * token, int & num)
1056 string::size_type pos = str.find(token);
1057 char ch = str[pos + strlen(token)];
1059 if ((pos == string::npos) || (ch != '='))
1062 pos += strlen(token) + 1;
1064 if ((ch != '"') && (ch != '\'')) { // only read till next space
1070 while((pos < str.length() - 1) && isdigit(str[pos]))
1073 num = strToInt(ret);
1079 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1082 bool const ret = getTokenValue(str, token, tmp);
1083 num = static_cast<LyXAlignment>(tmp);
1089 bool getTokenValue(string const & str, const char * token,
1090 LyXTabular::VAlignment & num)
1093 bool const ret = getTokenValue(str, token, tmp);
1094 num = static_cast<LyXTabular::VAlignment>(tmp);
1100 bool getTokenValue(string const & str, const char * token,
1101 LyXTabular::BoxType & num)
1104 bool ret = getTokenValue(str, token, tmp);
1105 num = static_cast<LyXTabular::BoxType>(tmp);
1111 bool getTokenValue(string const & str, const char * token, bool & flag)
1113 string::size_type pos = str.find(token);
1114 char ch = str[pos + strlen(token)];
1116 if ((pos == string::npos) || (ch != '='))
1119 pos += strlen(token) + 1;
1121 if ((ch != '"') && (ch != '\'')) { // only read till next space
1127 while((pos < str.length() - 1) && isdigit(str[pos]))
1130 flag = strToInt(ret);
1136 void l_getline(istream & is, string & str)
1144 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1147 istream & is = lex.getStream();
1149 l_getline(is, line);
1150 if (!prefixIs(line, "<LyXTabular ")) {
1151 OldFormatRead(lex, line);
1158 if (!getTokenValue(line, "version", version))
1160 if (!getTokenValue(line, "rows", rows_arg))
1162 if (!getTokenValue(line, "columns", columns_arg))
1164 Init(rows_arg, columns_arg);
1165 l_getline(is, line);
1166 if (!prefixIs(line, "<Features ")) {
1167 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1168 line << ")" << endl;
1171 getTokenValue(line, "islongtable", is_long_tabular);
1172 getTokenValue(line, "endhead", endhead);
1173 getTokenValue(line, "endfirsthead", endfirsthead);
1174 getTokenValue(line, "endfoot", endfoot);
1175 getTokenValue(line, "endlastfoot", endlastfoot);
1177 for (int i = 0; i < rows_; ++i) {
1178 l_getline(is, line);
1179 if (!prefixIs(line, "<Row ")) {
1180 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1181 line << ")" << endl;
1184 getTokenValue(line, "topline", row_info[i].top_line);
1185 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1186 getTokenValue(line, "newpage", row_info[i].newpage);
1187 for (int j = 0; j < columns_; ++j) {
1189 if (!prefixIs(line,"<Column")) {
1190 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1191 line << ")" << endl;
1195 getTokenValue(line, "alignment", column_info[j].alignment);
1196 getTokenValue(line, "valignment", column_info[j].valignment);
1197 getTokenValue(line, "leftline", column_info[j].left_line);
1198 getTokenValue(line, "rightline", column_info[j].right_line);
1199 getTokenValue(line, "width", column_info[j].p_width);
1200 getTokenValue(line, "special", column_info[j].align_special);
1202 l_getline(is, line);
1203 if (!prefixIs(line, "<Cell")) {
1204 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1205 line << ")" << endl;
1208 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1209 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1210 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1211 getTokenValue(line, "topline", cell_info[i][j].top_line);
1212 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1213 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1214 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1215 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1216 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1217 getTokenValue(line, "width", cell_info[i][j].p_width);
1218 getTokenValue(line, "special", cell_info[i][j].align_special);
1219 l_getline(is, line);
1220 if (prefixIs(line, "\\begin_inset")) {
1221 cell_info[i][j].inset.Read(buf, lex);
1222 l_getline(is, line);
1224 if (line != "</Cell>") {
1225 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1226 line << ")" << endl;
1229 l_getline(is, line);
1230 if (line != "</Column>") {
1231 lyxerr << "Wrong tabular format (expected </Column> got" <<
1232 line << ")" << endl;
1236 l_getline(is, line);
1237 if (line != "</Row>") {
1238 lyxerr << "Wrong tabular format (expected </Row> got" <<
1239 line << ")" << endl;
1243 while (line != "</LyXTabular>") {
1244 l_getline(is, line);
1246 set_row_column_number_info();
1250 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1255 int columns_arg = 0;
1256 int is_long_tabular_arg = false;
1257 int rotate_arg = false;
1267 istream & is = lex.getStream();
1270 version = lyx::atoi(s.substr(8, string::npos));
1274 vector<int> cont_row_info;
1277 lyxerr << "Tabular format < 5 is not supported anymore\n"
1278 "Get an older version of LyX (< 1.1.x) for conversion!"
1280 WriteAlert(_("Warning:"),
1281 _("Tabular format < 5 is not supported anymore\n"),
1282 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1284 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1285 >> rotate_arg >> a >> b >> c >> d;
1287 is >> rows_arg >> columns_arg;
1288 Init(rows_arg, columns_arg);
1289 cont_row_info = vector<int>(rows_arg);
1290 SetLongTabular(is_long_tabular_arg);
1291 SetRotateTabular(rotate_arg);
1293 for (i = 0; i < rows_; ++i) {
1295 cont_row_info[i] = false;
1297 for (i = 0; i < columns_; ++i) {
1300 for (i = 0; i < rows_; ++i) {
1301 for (j = 0; j < columns_; ++j) {
1306 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1307 >> rotate_arg >> a >> b >> c >> d;
1308 Init(rows_arg, columns_arg);
1309 cont_row_info = vector<int>(rows_arg);
1310 SetLongTabular(is_long_tabular_arg);
1311 SetRotateTabular(rotate_arg);
1316 for (i = 0; i < rows_; ++i) {
1317 a = b = c = d = e = f = g = h = 0;
1318 is >> a >> b >> c >> d;
1319 row_info[i].top_line = a;
1320 row_info[i].bottom_line = b;
1321 cont_row_info[i] = c;
1322 row_info[i].newpage = d;
1324 for (i = 0; i < columns_; ++i) {
1329 char ch; // skip '"'
1332 // ignore is buggy but we will use it later (Lgb)
1333 is.ignore(); // skip '"'
1335 getline(is, s1, '"');
1337 is >> ch; // skip '"'
1339 // ignore is buggy but we will use it later (Lgb)
1340 is.ignore(); // skip '"'
1342 getline(is, s2, '"');
1343 column_info[i].alignment = static_cast<LyXAlignment>(a);
1344 column_info[i].left_line = b;
1345 column_info[i].right_line = c;
1346 column_info[i].p_width = s1;
1347 column_info[i].align_special = s2;
1349 for (i = 0; i < rows_; ++i) {
1350 for (j = 0; j < columns_; ++j) {
1353 is >> a >> b >> c >> d >> e >> f >> g;
1356 is >> ch; // skip '"'
1358 // ignore is buggy but we will use it later (Lgb)
1359 is.ignore(); // skip '"'
1361 getline(is, s1, '"');
1363 is >> ch; // skip '"'
1365 // ignore is buggy but we will use it later (Lgb)
1366 is.ignore(); // skip '"'
1368 getline(is, s2, '"');
1369 cell_info[i][j].multicolumn = static_cast<char>(a);
1370 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1371 cell_info[i][j].top_line = static_cast<char>(c);
1372 cell_info[i][j].bottom_line = static_cast<char>(d);
1373 cell_info[i][j].left_line = column_info[j].left_line;
1374 cell_info[i][j].right_line = column_info[j].right_line;
1375 cell_info[i][j].rotate = static_cast<bool>(f);
1376 cell_info[i][j].usebox = static_cast<BoxType>(g);
1377 cell_info[i][j].align_special = s1;
1378 cell_info[i][j].p_width = s2;
1382 set_row_column_number_info(true);
1384 LyXParagraph * par = new LyXParagraph;
1385 LyXParagraph * return_par = 0;
1387 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1388 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1393 LyXFont font(LyXFont::ALL_SANE);
1394 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1396 while (lex.IsOK()) {
1398 string const token = lex.GetString();
1401 if (token == "\\layout"
1402 || token == "\\end_float"
1403 || token == "\\end_deeper") {
1404 lex.pushToken(token);
1407 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1419 lex.pushToken(token);
1423 lex.printError("New Paragraph allocated! This should not happen!");
1424 lex.pushToken(token);
1430 // now we have the par we should fill the insets with this!
1432 InsetText * inset = GetCellInset(cell);
1435 for (int i = 0; i < par->Last(); ++i) {
1436 if (par->IsNewline(i)) {
1438 if (cell > GetNumberOfCells()) {
1439 lyxerr << "Some error in reading old table format occured!" <<
1440 endl << "Terminating when reading cell[" << cell << "]!" <<
1445 row = row_of_cell(cell);
1446 if (cont_row_info[row]) {
1448 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1449 while(!IsFirstCellInRow(--cell));
1451 inset = GetCellInset(cell);
1454 inset = GetCellInset(cell);
1455 row = row_of_cell(cell);
1456 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1458 // insert a space instead
1460 par->InsertChar(i, ' ');
1463 par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
1464 inset->par->InsertFromMinibuffer(inset->par->Last());
1471 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1473 int const i = isColumn ? cell : column_of_cell(cell);
1475 if (!isColumn && IsMultiColumn(cell)) {
1476 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1477 return cellinfo_of_cell(cell)->align_special;
1479 switch (GetAlignment(cell)) {
1480 case LYX_ALIGN_LEFT:
1482 case LYX_ALIGN_RIGHT:
1489 if (!column_info[i].align_special.empty()) {
1490 return column_info[i].align_special;
1492 #ifdef IGNORE_THIS_FOR_NOW
1493 else if (!column_info[i].p_width.empty()) {
1495 file += column_info[i].p_width;
1500 switch (column_info[i].alignment) {
1501 case LYX_ALIGN_LEFT:
1503 case LYX_ALIGN_RIGHT:
1513 // cell <0 will tex the preamble
1514 // returns the number of printed newlines
1515 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1518 if (IsLastCell(cell)) {
1519 os << newlineAndDepth(--depth)
1521 << newlineAndDepth(--depth)
1523 << newlineAndDepth(--depth)
1525 << newlineAndDepth(--depth);
1526 if (is_long_tabular)
1530 << newlineAndDepth(--depth);
1535 if (is_long_tabular)
1541 << "' COLSEP='1' ROWSEP='1'>"
1542 << newlineAndDepth(++depth);
1544 for (int i = 0; i < columns_; ++i) {
1545 os << "<COLSPEC ALIGN='"
1546 << GetDocBookAlign(i, true)
1552 if (i == (columns_-1)) {
1555 if (column_info[i].right_line ||
1556 column_info[i+1].left_line)
1562 << newlineAndDepth(depth);
1564 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1565 if (column_info[i].left_line)
1570 << newlineAndDepth(++depth)
1572 << newlineAndDepth(++depth)
1574 << GetDocBookAlign(0)
1576 if (IsMultiColumn(0)) {
1577 os << " NAMEST='col1' NAMEEND='col"
1578 << cells_in_multicolumn(0)
1582 << newlineAndDepth(++depth);
1585 if (IsLastCellInRow(cell)) {
1586 os << newlineAndDepth(--depth)
1588 << newlineAndDepth(--depth)
1590 << newlineAndDepth(depth)
1592 << newlineAndDepth(++depth)
1594 << GetDocBookAlign(cell + 1)
1595 << "' VALIGN='middle'";
1596 if (IsMultiColumn(cell + 1)) {
1597 os << " NAMEST='col"
1598 << column_of_cell(cell + 1) + 1
1600 << column_of_cell(cell + 1) +
1601 cells_in_multicolumn(cell + 1)
1605 << newlineAndDepth(++depth);
1608 os << newlineAndDepth(--depth)
1610 << newlineAndDepth(depth)
1612 << GetDocBookAlign(cell + 1)
1613 << "' VALIGN='middle'";
1614 if (IsMultiColumn(cell + 1)) {
1615 os << " NAMEST='col"
1616 << column_of_cell(cell + 1) + 1
1618 << column_of_cell(cell + 1) +
1619 cells_in_multicolumn(cell + 1)
1623 << newlineAndDepth(++depth);
1632 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1634 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1635 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1639 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1641 int const row = row_of_cell(cell);
1642 int const column = column_of_cell(cell);
1643 return &cell_info[row][column];
1647 void LyXTabular::SetMultiColumn(int cell, int number)
1649 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1650 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1651 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1652 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1653 for (number--; number > 0; --number) {
1654 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1656 set_row_column_number_info();
1660 int LyXTabular::cells_in_multicolumn(int cell) const
1662 int const row = row_of_cell(cell);
1663 int column = column_of_cell(cell);
1666 while ((column < columns_) &&
1667 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1676 int LyXTabular::UnsetMultiColumn(int cell)
1678 int const row = row_of_cell(cell);
1679 int column = column_of_cell(cell);
1683 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1684 cell_info[row][column].multicolumn = CELL_NORMAL;
1686 while ((column < columns_) &&
1687 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1689 cell_info[row][column].multicolumn = CELL_NORMAL;
1694 set_row_column_number_info();
1699 void LyXTabular::SetLongTabular(bool what)
1701 is_long_tabular = what;
1705 bool LyXTabular::IsLongTabular() const
1707 return is_long_tabular;
1711 void LyXTabular::SetRotateTabular(bool flag)
1717 bool LyXTabular::GetRotateTabular() const
1723 void LyXTabular::SetRotateCell(int cell, bool flag)
1725 cellinfo_of_cell(cell)->rotate = flag;
1729 bool LyXTabular::GetRotateCell(int cell) const
1731 return cellinfo_of_cell(cell)->rotate;
1735 bool LyXTabular::NeedRotating() const
1739 for (int i = 0; i < rows_; ++i) {
1740 for (int j = 0; j < columns_; ++j) {
1741 if (cell_info[i][j].rotate)
1749 bool LyXTabular::IsLastCell(int cell) const
1751 if ((cell + 1) < GetNumberOfCells())
1757 int LyXTabular::GetCellAbove(int cell) const
1759 if (row_of_cell(cell) > 0)
1760 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1765 int LyXTabular::GetCellBelow(int cell) const
1767 if (row_of_cell(cell) + 1 < rows_)
1768 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1773 int LyXTabular::GetLastCellAbove(int cell) const
1775 if (row_of_cell(cell) <= 0)
1777 if (!IsMultiColumn(cell))
1778 return GetCellAbove(cell);
1779 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1783 int LyXTabular::GetLastCellBelow(int cell) const
1785 if (row_of_cell(cell) + 1 >= rows_)
1787 if (!IsMultiColumn(cell))
1788 return GetCellBelow(cell);
1789 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1793 int LyXTabular::GetCellNumber(int row, int column) const
1795 if (column >= columns_)
1796 column = columns_ - 1;
1797 else if (column < 0)
1804 return cell_info[row][column].cellno;
1808 void LyXTabular::SetUsebox(int cell, BoxType type)
1810 cellinfo_of_cell(cell)->usebox = type;
1814 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1816 if (column_info[column_of_cell(cell)].p_width.empty() &&
1817 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1819 if (cellinfo_of_cell(cell)->usebox > 1)
1820 return cellinfo_of_cell(cell)->usebox;
1821 return UseParbox(cell);
1825 void LyXTabular::SetLTHead(int cell, bool first)
1827 int const row = row_of_cell(cell);
1828 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1831 if (endfirsthead == val)
1844 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1847 if (abs(endhead) > rows_)
1849 return (row_of_cell(cell) == abs(endhead) - 1);
1853 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1856 if (abs(endfirsthead) > rows_)
1858 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1862 void LyXTabular::SetLTFoot(int cell, bool last)
1864 int const row = row_of_cell(cell);
1865 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1868 if (endlastfoot == val)
1881 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1884 if ((endfoot + 1) > rows_)
1886 return (row_of_cell(cell) == abs(endfoot) - 1);
1890 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1893 if (abs(endlastfoot) > rows_)
1895 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1899 void LyXTabular::SetLTNewPage(int cell, bool what)
1901 row_info[row_of_cell(cell)].newpage = what;
1905 bool LyXTabular::GetLTNewPage(int cell) const
1907 return row_info[row_of_cell(cell)].newpage;
1911 bool LyXTabular::SetAscentOfRow(int row, int height)
1913 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1915 row_info[row].ascent_of_row = height;
1920 bool LyXTabular::SetDescentOfRow(int row, int height)
1922 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1924 row_info[row].descent_of_row = height;
1929 int LyXTabular::GetAscentOfRow(int row) const
1933 return row_info[row].ascent_of_row;
1937 int LyXTabular::GetDescentOfRow(int row) const
1941 return row_info[row].descent_of_row;
1945 int LyXTabular::GetHeightOfTabular() const
1949 for (int row = 0; row < rows_; ++row)
1950 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1951 GetAdditionalHeight(GetCellNumber(row, 0));
1956 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1958 if ((row >= rows_) || (column >= columns_))
1960 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1964 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1966 int const fcell = GetFirstCellInRow(row);
1967 int const n = NumberOfCellsInRow(fcell) + fcell;
1970 for (int i = fcell; i < n; ++i) {
1974 if (tmp == (n - fcell)){
1977 for (int i = fcell; i < n; ++i) {
1980 << column_of_cell(i) + 1
1982 << right_column_of_cell(i) + 1
1994 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1996 int const fcell = GetFirstCellInRow(row);
1997 int const n = NumberOfCellsInRow(fcell) + fcell;
2000 for (int i = fcell; i < n; ++i) {
2004 if (tmp == (n-fcell)){
2007 for (int i = fcell; i < n; ++i) {
2008 if (BottomLine(i)) {
2010 << column_of_cell(i) + 1
2012 << right_column_of_cell(i) + 1
2024 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2028 if (GetRotateCell(cell)) {
2029 os << "\\begin{sideways}\n";
2032 if (IsMultiColumn(cell)) {
2033 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2034 if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
2035 os << cellinfo_of_cell(cell+1)->align_special << "}{";
2039 if (!GetPWidth(cell).empty()) {
2040 switch (GetVAlignment(cell)) {
2041 case LYX_VALIGN_TOP:
2044 case LYX_VALIGN_CENTER:
2047 case LYX_VALIGN_BOTTOM:
2051 os << "{" << GetPWidth(cell) << '}';
2053 switch (GetAlignment(cell)) {
2054 case LYX_ALIGN_LEFT:
2057 case LYX_ALIGN_RIGHT:
2065 if (RightLine(cell))
2067 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2073 if (GetUsebox(cell) == BOX_PARBOX) {
2075 switch (GetVAlignment(cell)) {
2076 case LYX_VALIGN_TOP:
2079 case LYX_VALIGN_CENTER:
2082 case LYX_VALIGN_BOTTOM:
2086 os << "]{" << GetPWidth(cell) << "}{";
2087 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2088 os << "\\begin{minipage}[";
2089 switch (GetVAlignment(cell)) {
2090 case LYX_VALIGN_TOP:
2093 case LYX_VALIGN_CENTER:
2096 case LYX_VALIGN_BOTTOM:
2100 os << "]{" << GetPWidth(cell) << "}\n";
2107 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2112 if (GetUsebox(cell) == BOX_PARBOX)
2114 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2115 os << "%\n\\end{minipage}";
2118 if (IsMultiColumn(cell)){
2121 if (GetRotateCell(cell)) {
2122 os << "%\n\\end{sideways}";
2129 int LyXTabular::Latex(Buffer const * buf,
2130 ostream & os, bool fragile, bool fp) const
2135 //+---------------------------------------------------------------------
2136 //+ first the opening preamble +
2137 //+---------------------------------------------------------------------
2140 os << "\\begin{sideways}\n";
2143 if (is_long_tabular)
2144 os << "\\begin{longtable}{";
2146 os << "\\begin{tabular}{";
2147 for (int i = 0; i < columns_; ++i) {
2148 if (column_info[i].left_line)
2150 if (!column_info[i].align_special.empty()) {
2151 os << column_info[i].align_special;
2152 } else if (!column_info[i].p_width.empty()) {
2153 switch (column_info[i].valignment) {
2154 case LYX_VALIGN_TOP:
2157 case LYX_VALIGN_CENTER:
2160 case LYX_VALIGN_BOTTOM:
2165 << column_info[i].p_width
2168 switch (column_info[i].alignment) {
2169 case LYX_ALIGN_LEFT:
2172 case LYX_ALIGN_RIGHT:
2180 if (column_info[i].right_line)
2186 //+---------------------------------------------------------------------
2187 //+ the single row and columns (cells) +
2188 //+---------------------------------------------------------------------
2191 for (int i = 0; i < rows_; ++i) {
2192 ret += TeXTopHLine(os, i);
2194 if (IsLongTabular()) {
2195 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2196 os << "\\endhead\n";
2199 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2200 os << "\\endfirsthead\n";
2203 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2204 os << "\\endfoot\n";
2207 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2208 os << "\\endlastfoot\n";
2214 ret += TeXBottomHLine(os, i-1);
2215 ret += TeXTopHLine(os, i);
2217 for (int j = 0; j < columns_; ++j) {
2218 if (IsPartOfMultiColumn(i,j))
2220 ret += TeXCellPreamble(os, cell);
2221 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2222 ret += TeXCellPostamble(os, cell);
2223 if (!IsLastCellInRow(cell)) { // not last cell in row
2230 ret += TeXBottomHLine(os, i);
2232 if (IsLongTabular()) {
2233 if ((endhead > 0) && (i == (endhead - 1))) {
2234 os << "\\endhead\n";
2237 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2238 os << "\\endfirsthead\n";
2241 if ((endfoot > 0) && (i == (endfoot - 1))) {
2242 os << "\\endfoot\n";
2245 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2246 os << "\\endlastfoot\n";
2250 ret += TeXBottomHLine(os, i);
2251 if (row_info[i].newpage) {
2252 os << "\\newpage\n";
2258 //+---------------------------------------------------------------------
2259 //+ the closing of the tabular +
2260 //+---------------------------------------------------------------------
2262 if (is_long_tabular)
2263 os << "\\end{longtable}";
2265 os << "\\end{tabular}";
2267 os << "\n\\end{sideways}";
2275 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2279 //+---------------------------------------------------------------------
2280 //+ first the opening preamble +
2281 //+---------------------------------------------------------------------
2283 os << "<tgroup cols=\"" << columns_
2284 << "\" colsep=\"1\" rowsep=\"1\">\n";
2286 for (int i = 0; i < columns_; ++i) {
2287 os << "<colspec colname=\"col" << i << "\" align=\"";
2288 switch (column_info[i].alignment) {
2289 case LYX_ALIGN_LEFT:
2292 case LYX_ALIGN_RIGHT:
2303 //+---------------------------------------------------------------------
2304 //+ the single row and columns (cells) +
2305 //+---------------------------------------------------------------------
2309 for (int i = 0; i < rows_; ++i) {
2311 for (int j = 0; j < columns_; ++j) {
2312 if (IsPartOfMultiColumn(i, j))
2315 os << "<entry align=\"";
2316 switch (GetAlignment(cell)) {
2317 case LYX_ALIGN_LEFT:
2320 case LYX_ALIGN_RIGHT:
2328 os << "\" valign=\"";
2329 switch (GetVAlignment(cell)) {
2330 case LYX_VALIGN_TOP:
2333 case LYX_VALIGN_BOTTOM:
2336 case LYX_VALIGN_CENTER:
2341 if (IsMultiColumn(cell)) {
2342 os << " namest=\"col" << j << "\" ";
2343 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2347 ret += GetCellInset(cell)->DocBook(buf, os);
2354 //+---------------------------------------------------------------------
2355 //+ the closing of the tabular +
2356 //+---------------------------------------------------------------------
2367 void print_n_chars(ostream & os, unsigned char ch, int n)
2369 os << string(n, ch);
2373 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2374 vector<unsigned int> const & clen) const
2376 int const fcell = GetFirstCellInRow(row);
2377 int const n = NumberOfCellsInRow(fcell) + fcell;
2382 for (int i = fcell; i < n; ++i) {
2389 // Isn't this equivalent? (Lgb)
2390 #warning Please check this Jürgen.
2391 // If ok please delete the abofe #if 0 section. (Lgb)
2392 for (int i = fcell; i < n; ++i) {
2399 for (int i = fcell; i < n; ++i) {
2410 int column = column_of_cell(i);
2411 int len = clen[column];
2412 while(IsPartOfMultiColumn(row, ++column))
2413 len += clen[column] + 4;
2414 print_n_chars(os, ch, len);
2429 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2430 vector<unsigned int> const & clen) const
2432 int const fcell = GetFirstCellInRow(row);
2433 int const n = NumberOfCellsInRow(fcell) + fcell;
2438 for (int i = fcell; i < n; ++i) {
2445 // Isn't this equivalent? (Lgb)
2446 #warning Please check this Jürgen.
2447 // If ok, please delete the above #if 0 section. (Lgb)
2448 for (int i = fcell; i < n; ++i) {
2454 for (int i = fcell; i < n; ++i) {
2455 if (BottomLine(i)) {
2465 int column = column_of_cell(i);
2466 int len = clen[column];
2467 while(IsPartOfMultiColumn(row, ++column))
2468 len += clen[column] + 4;
2469 print_n_chars(os, ch, len);
2470 if (BottomLine(i)) {
2484 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2485 int cell, int row, int column,
2486 vector<unsigned int> const & clen) const
2489 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2496 unsigned int len1 = sstr.str().length();
2497 unsigned int len2 = clen[column];
2498 while(IsPartOfMultiColumn(row, ++column))
2499 len2 += clen[column] + 4;
2502 switch (GetAlignment(cell)) {
2504 case LYX_ALIGN_LEFT:
2507 case LYX_ALIGN_RIGHT:
2511 case LYX_ALIGN_CENTER:
2517 for (unsigned int i = 0; i < len1; ++i)
2520 for (unsigned int i = 0; i < len2; ++i)
2522 if (RightLine(cell))
2527 return ret * 0; // eh? (Lgb)
2531 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2535 //+---------------------------------------------------------------------
2536 //+ first calculate the width of the single columns +
2537 //+---------------------------------------------------------------------
2538 vector<unsigned int> clen(columns_);
2540 // first all non (real) multicolumn cells!
2541 for (int j = 0; j < columns_; ++j) {
2543 for (int i = 0; i < rows_; ++i) {
2544 int cell = GetCellNumber(i, j);
2545 if (IsMultiColumn(cell, true))
2548 GetCellInset(cell)->Ascii(buf, sstr, 0);
2549 if (clen[j] < sstr.str().length())
2550 clen[j] = sstr.str().length();
2553 // then all (real) multicolumn cells!
2554 for (int j = 0; j < columns_; ++j) {
2555 for (int i = 0; i < rows_; ++i) {
2556 int cell = GetCellNumber(i, j);
2557 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2560 GetCellInset(cell)->Ascii(buf, sstr, 0);
2561 int len = int(sstr.str().length());
2562 int const n = cells_in_multicolumn(cell);
2563 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2565 if (len > int(clen[j + n - 1]))
2566 clen[j + n - 1] = len;
2570 for (int i = 0; i < rows_; ++i) {
2571 AsciiTopHLine(os, i, clen);
2572 for (int j = 0; j < columns_; ++j) {
2573 if (IsPartOfMultiColumn(i,j))
2575 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2579 AsciiBottomHLine(os, i, clen);
2585 InsetText * LyXTabular::GetCellInset(int cell) const
2587 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2591 void LyXTabular::Validate(LaTeXFeatures & features) const
2593 if (IsLongTabular())
2594 features.longtable = true;
2596 features.rotating = true;
2597 for (int cell = 0; cell < numberofcells; ++cell) {
2598 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2599 features.array = true;
2600 GetCellInset(cell)->Validate(features);
2605 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2607 LyXParagraph * par = GetCellInset(cell)->par;
2609 for (; par; par = par->next) {
2610 for (int i = 0; i < par->Last(); ++i) {
2611 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)