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_);
96 #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)
102 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
109 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
111 // If this and lt is not of the same size we have a serious bug
112 // So then it is ok to throw an exception, or for now
114 Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
116 cell_info = lt.cell_info;
117 row_info = lt.row_info;
118 column_info = lt.column_info;
120 // long tabular stuff
121 SetLongTabular(lt.is_long_tabular);
122 endhead = lt.endhead;
123 endfoot = lt.endfoot;
124 endfirsthead = lt.endfirsthead;
125 endlastfoot = lt.endlastfoot;
135 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
137 LyXTabular * result = new LyXTabular(inset, *this);
138 // don't know if this is good but I need to Clone also
139 // the text-insets here, this is for the Undo-facility!
140 for (int i = 0; i < rows_; ++i) {
141 for (int j = 0; j < columns_; ++j) {
142 result->cell_info[i][j].inset = cell_info[i][j].inset;
143 result->cell_info[i][j].inset.setOwner(inset);
150 /* activates all lines and sets all widths to 0 */
151 void LyXTabular::Init(int rows_arg, int columns_arg)
154 columns_ = columns_arg;
155 row_info = row_vector(rows_, rowstruct());
156 column_info = column_vector(columns_, columnstruct());
157 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
160 for (int i = 0; i < rows_; ++i) {
161 for (int j = 0; j < columns_; ++j) {
162 cell_info[i][j].inset.setOwner(owner_);
163 cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
164 cell_info[i][j].cellno = cellno++;
166 cell_info[i].back().right_line = true;
168 row_info.back().bottom_line = true;
169 row_info.front().bottom_line = true;
171 for (int i = 0; i < columns_; ++i) {
172 calculate_width_of_column(i);
174 column_info.back().right_line = true;
176 calculate_width_of_tabular();
178 rowofcell = vector<int>();
179 columnofcell = vector<int>();
180 set_row_column_number_info();
181 is_long_tabular = false;
190 void LyXTabular::AppendRow(int cell)
194 int row = row_of_cell(cell);
196 row_vector::iterator rit = row_info.begin() + row;
197 row_info.insert(rit, rowstruct());
200 cell_vvector::iterator cit = cell_info.begin() + row;
201 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
203 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
206 for (int i = 0; i <= row; ++i) {
207 for (int j = 0; j < columns_; ++j) {
208 c_info[i][j] = cell_info[i][j];
211 for (int i = row + 1; i < rows_; ++i) {
212 for (int j = 0; j < columns_; ++j) {
213 c_info[i][j] = cell_info[i-1][j];
218 for (int j = 0; j < columns_; ++j) {
219 cell_info[row][j].inset.clear();
226 void LyXTabular::DeleteRow(int row)
228 // Why make it so hard? (Lgb)
231 if (rows_ == 1) return; // Not allowed to delete last row
233 row_info.erase(row_info.begin() + row); //&row_info[row]);
234 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
240 void LyXTabular::AppendColumn(int cell)
244 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
246 int const column = column_of_cell(cell);
247 column_vector::iterator cit = column_info.begin() + column + 1;
248 column_info.insert(cit, columnstruct());
250 for (int i = 0; i < rows_; ++i) {
251 for (int j = 0; j <= column; ++j) {
252 c_info[i][j] = cell_info[i][j];
254 for (int j = column + 1; j < columns_; ++j) {
255 c_info[i][j] = cell_info[i][j - 1];
257 // care about multicolumns
258 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
259 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
261 if ((column + 1) == columns_ ||
262 cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
263 cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
268 for (int i = 0; i < rows_; ++i) {
269 //cell_info[i][column].inset.clear();
270 cell_info[i][column + 1].inset.clear();
276 void LyXTabular::DeleteColumn(int column)
278 // Similar to DeleteRow
279 //if (!(columns_ - 1))
281 if (columns_ == 1) return; // Not allowed to delete last column
283 column_info.erase(column_info.begin() + column);
284 for (int i = 0; i < rows_; ++i) {
285 cell_info[i].erase(cell_info[i].begin() + column);
292 void LyXTabular::Reinit()
294 for (int i = 0; i < rows_; ++i) {
295 for (int j = 0; j < columns_; ++j) {
296 cell_info[i][j].width_of_cell = 0;
297 cell_info[i][j].inset.setOwner(owner_);
301 for (int i = 0; i < columns_; ++i) {
302 calculate_width_of_column(i);
304 calculate_width_of_tabular();
306 set_row_column_number_info();
310 void LyXTabular::set_row_column_number_info(bool oldformat)
313 for (int row = 0; row < rows_; ++row) {
314 for (int column = 0; column<columns_; ++column) {
315 if (cell_info[row][column].multicolumn
316 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
318 cell_info[row][column].cellno = numberofcells;
321 ++numberofcells; // because this is one more than as we start from 0
323 rowofcell.resize(numberofcells);
324 columnofcell.resize(numberofcells);
326 for (int row = 0, column = 0, c = 0;
327 c < numberofcells && row < rows_ && column < columns_;) {
329 columnofcell[c] = column;
333 } while (column < columns_ &&
334 cell_info[row][column].multicolumn
335 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
336 if (column == columns_) {
342 for (int row = 0; row < rows_; ++row) {
343 for (int column = 0; column < columns_; ++column) {
344 if (IsPartOfMultiColumn(row,column))
346 // now set the right line of multicolumns right for oldformat read
348 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
350 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
351 cell_info[row][column].right_line =
352 cell_info[row][column+cn-1].right_line;
354 cell_info[row][column].inset.SetAutoBreakRows(
355 !GetPWidth(GetCellNumber(row, column)).empty());
361 int LyXTabular::GetNumberOfCells() const
363 return numberofcells;
367 int LyXTabular::NumberOfCellsInRow(int cell) const
369 int const row = row_of_cell(cell);
371 for (int i = 0; i < columns_; ++i) {
372 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
379 /* returns 1 if there is a topline, returns 0 if not */
380 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
382 int const row = row_of_cell(cell);
384 if (!onlycolumn && IsMultiColumn(cell))
385 return cellinfo_of_cell(cell)->top_line;
386 return row_info[row].top_line;
390 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
392 // no bottom line underneath non-existent cells if you please
393 // Isn't that a programming error? Is so this should
394 // be an Assert instead. (Lgb)
395 if (cell >= numberofcells)
398 if (!onlycolumn && IsMultiColumn(cell))
399 return cellinfo_of_cell(cell)->bottom_line;
400 return row_info[row_of_cell(cell)].bottom_line;
404 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
406 if (!onlycolumn && IsMultiColumn(cell))
407 return cellinfo_of_cell(cell)->left_line;
408 return column_info[column_of_cell(cell)].left_line;
412 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
414 if (!onlycolumn && IsMultiColumn(cell))
415 return cellinfo_of_cell(cell)->right_line;
416 return column_info[right_column_of_cell(cell)].right_line;
420 bool LyXTabular::TopAlreadyDrawed(int cell) const
422 if (GetAdditionalHeight(cell))
424 int row = row_of_cell(cell);
426 int column = column_of_cell(cell);
429 && cell_info[row][column].multicolumn
430 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
432 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
433 return row_info[row].bottom_line;
435 return cell_info[row][column].bottom_line;
441 bool LyXTabular::LeftAlreadyDrawed(int cell) const
443 int column = column_of_cell(cell);
445 int row = row_of_cell(cell);
447 (cell_info[row][column].multicolumn ==
448 LyXTabular::CELL_PART_OF_MULTICOLUMN));
449 if (GetAdditionalWidth(cell_info[row][column].cellno))
451 return column_info[column].right_line;
457 bool LyXTabular::IsLastRow(int cell) const
459 return (row_of_cell(cell) == rows_ - 1);
463 int LyXTabular::GetAdditionalHeight(int cell) const
465 int const row = row_of_cell(cell);
471 for (int column = 0; column < columns_ - 1 && bottom; ++column) {
472 switch (cell_info[row - 1][column].multicolumn) {
473 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
474 bottom = cell_info[row - 1][column].bottom_line;
476 case LyXTabular::CELL_NORMAL:
477 bottom = row_info[row - 1].bottom_line;
480 for (int column = 0; column < columns_ - 1 && top; ++column) {
481 switch (cell_info[row][column].multicolumn){
482 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
483 top = cell_info[row][column].top_line;
485 case LyXTabular::CELL_NORMAL:
486 top = row_info[row].top_line;
490 return WIDTH_OF_LINE;
495 int LyXTabular::GetAdditionalWidth(int cell) const
497 // internally already set in SetWidthOfCell
498 // used to get it back in text.C
499 int const col = right_column_of_cell(cell);
500 if (col < columns_ - 1 && column_info[col].right_line &&
501 column_info[col+1].left_line)
502 return WIDTH_OF_LINE;
508 // returns the maximum over all rows
509 int LyXTabular::GetWidthOfColumn(int cell) const
511 int const column1 = column_of_cell(cell);
512 int const column2 = right_column_of_cell(cell);
514 for (int i = column1; i <= column2; ++i) {
515 result += column_info[i].width_of_column;
521 int LyXTabular::GetWidthOfTabular() const
523 return width_of_tabular;
527 /* returns 1 if a complete update is necessary, otherwise 0 */
528 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
530 if (!IsMultiColumn(cell))
533 int const row = row_of_cell(cell);
534 int const column1 = column_of_cell(cell);
535 int const column2 = right_column_of_cell(cell);
537 // first set columns to 0 so we can calculate the right width
538 for (int i = column1; i <= column2; ++i) {
539 cell_info[row][i].width_of_cell = 0;
541 // set the width to MAX_WIDTH until width > 0
542 int width = (new_width + 2 * WIDTH_OF_LINE);
545 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
546 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
547 width -= column_info[i].width_of_column;
550 cell_info[row][i].width_of_cell = width;
556 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
558 int const row = row_of_cell(cell);
559 int const column1 = column_of_cell(cell);
560 int const column2 = right_column_of_cell(cell);
562 // first set columns to 0 so we can calculate the right width
564 for (; i <= column2; ++i)
565 cell_info[row][i].width_of_cell = 0;
566 for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
568 if (i < numberofcells)
569 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
570 SetWidthOfMulticolCell(cell, new_width);
574 /* returns 1 if a complete update is necessary, otherwise 0 */
575 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
577 int const row = row_of_cell(cell);
578 int const column1 = column_of_cell(cell);
582 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
584 if (IsMultiColumn(cell, true)) {
585 tmp = SetWidthOfMulticolCell(cell, new_width);
587 width = (new_width + 2*WIDTH_OF_LINE);
588 cell_info[row][column1].width_of_cell = width;
589 if (column_info[column1].right_line && (column1 < columns_-1) &&
590 column_info[column1+1].left_line) // additional width
591 cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
592 tmp = calculate_width_of_column_NMC(column1);
596 for (; i<columns_; ++i)
597 calculate_width_of_column_NMC(i);
598 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
600 if (i < numberofcells)
601 recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
602 for (i = 0; i < columns_; ++i)
603 calculate_width_of_column(i);
604 calculate_width_of_tabular();
611 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
613 if (!IsMultiColumn(cell) || onlycolumn)
614 column_info[column_of_cell(cell)].alignment = align;
616 cellinfo_of_cell(cell)->alignment = align;
621 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
623 if (!IsMultiColumn(cell) || onlycolumn)
624 column_info[column_of_cell(cell)].valignment = align;
626 cellinfo_of_cell(cell)->valignment = align;
631 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
633 bool flag = !width.empty();
634 int const j = column_of_cell(cell);
636 column_info[j].p_width = width;
637 if (flag) // do this only if there is a width
638 SetAlignment(cell, LYX_ALIGN_LEFT);
639 for (int i = 0; i < rows_; ++i) {
640 int c = GetCellNumber(i, j);
641 flag = !GetPWidth(c).empty(); // because of multicolumns!
642 GetCellInset(c)->SetAutoBreakRows(flag);
648 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
650 bool const flag = !width.empty();
652 cellinfo_of_cell(cell)->p_width = width;
653 if (IsMultiColumn(cell)) {
654 GetCellInset(cell)->SetAutoBreakRows(flag);
661 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
662 LyXTabular::Feature what)
664 if (what == SET_SPECIAL_MULTI)
665 cellinfo_of_cell(cell)->align_special = special;
667 column_info[column_of_cell(cell)].align_special = special;
672 bool LyXTabular::SetAllLines(int cell, bool line)
674 SetTopLine(cell, line);
675 SetBottomLine(cell, line);
676 SetRightLine(cell, line);
677 SetLeftLine(cell, line);
682 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
684 int const row = row_of_cell(cell);
686 if (onlycolumn || !IsMultiColumn(cell))
687 row_info[row].top_line = line;
689 cellinfo_of_cell(cell)->top_line = line;
694 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
696 if (onlycolumn || !IsMultiColumn(cell))
697 row_info[row_of_cell(cell)].bottom_line = line;
699 cellinfo_of_cell(cell)->bottom_line = line;
704 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
706 if (onlycolumn || !IsMultiColumn(cell))
707 column_info[column_of_cell(cell)].left_line = line;
709 cellinfo_of_cell(cell)->left_line = line;
714 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
716 if (onlycolumn || !IsMultiColumn(cell))
717 column_info[right_column_of_cell(cell)].right_line = line;
719 cellinfo_of_cell(cell)->right_line = line;
724 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
726 if (!onlycolumn && IsMultiColumn(cell))
727 return cellinfo_of_cell(cell)->alignment;
729 return column_info[column_of_cell(cell)].alignment;
733 LyXTabular::VAlignment
734 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
736 if (!onlycolumn && IsMultiColumn(cell))
737 return cellinfo_of_cell(cell)->valignment;
739 return column_info[column_of_cell(cell)].valignment;
743 string const LyXTabular::GetPWidth(int cell) const
745 if (IsMultiColumn(cell))
746 return cellinfo_of_cell(cell)->p_width;
747 return column_info[column_of_cell(cell)].p_width;
751 string const LyXTabular::GetColumnPWidth(int cell) const
753 return column_info[column_of_cell(cell)].p_width;
757 string const LyXTabular::GetMColumnPWidth(int cell) const
759 if (IsMultiColumn(cell))
760 return cellinfo_of_cell(cell)->p_width;
765 string const LyXTabular::GetAlignSpecial(int cell, int what) const
767 if (what == SET_SPECIAL_MULTI)
768 return cellinfo_of_cell(cell)->align_special;
769 return column_info[column_of_cell(cell)].align_special;
773 int LyXTabular::GetWidthOfCell(int cell) const
775 int const row = row_of_cell(cell);
776 int const column1 = column_of_cell(cell);
777 int const column2 = right_column_of_cell(cell);
779 for (int i = column1; i <= column2; ++i) {
780 result += cell_info[row][i].width_of_cell;
786 int LyXTabular::GetBeginningOfTextInCell(int cell) const
790 switch (GetAlignment(cell)){
791 case LYX_ALIGN_CENTER:
792 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
794 case LYX_ALIGN_RIGHT:
795 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
796 // + GetAdditionalWidth(cell);
798 default: /* LYX_ALIGN_LEFT: nothing :-) */
808 bool LyXTabular::IsFirstCellInRow(int cell) const
810 return column_of_cell(cell) == 0;
814 int LyXTabular::GetFirstCellInRow(int row) const
818 return cell_info[row][0].cellno;
821 bool LyXTabular::IsLastCellInRow(int cell) const
823 return (right_column_of_cell(cell) == (columns_ - 1));
827 int LyXTabular::GetLastCellInRow(int row) const
831 return cell_info[row][columns_-1].cellno;
835 bool LyXTabular::calculate_width_of_column(int column)
837 int const old_column_width = column_info[column].width_of_column;
840 for (int i = 0; i < rows_; ++i) {
841 maximum = max(cell_info[i][column].width_of_cell, maximum);
843 column_info[column].width_of_column = maximum;
844 return (column_info[column].width_of_column != old_column_width);
849 // calculate the with of the column without regarding REAL MultiColumn
850 // cells. This means MultiColumn-cells spanning more than 1 column.
852 bool LyXTabular::calculate_width_of_column_NMC(int column)
854 int const old_column_width = column_info[column].width_of_column;
856 for (int i = 0; i < rows_; ++i) {
857 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
858 (cell_info[i][column].width_of_cell > max)) {
859 max = cell_info[i][column].width_of_cell;
862 column_info[column].width_of_column = max;
863 return (column_info[column].width_of_column != old_column_width);
867 void LyXTabular::calculate_width_of_tabular()
869 width_of_tabular = 0;
870 for (int i = 0; i < columns_; ++i) {
871 width_of_tabular += column_info[i].width_of_column;
876 int LyXTabular::row_of_cell(int cell) const
878 if (cell >= numberofcells)
882 return rowofcell[cell];
886 int LyXTabular::column_of_cell(int cell) const
888 if (cell >= numberofcells)
892 return columnofcell[cell];
896 int LyXTabular::right_column_of_cell(int cell) const
898 int const row = row_of_cell(cell);
899 int column = column_of_cell(cell);
900 while (column < (columns_ - 1) &&
901 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
907 // Perfect case for a template... (Lgb)
910 string const write_attribute(string const & name, T const & t)
912 string str = " " + name + "=\"" + tostr(t) + "\"";
917 string const write_attribute(string const & name, bool const & b)
919 return write_attribute(name, int(b));
924 string const write_attribute(string const & name, int value)
926 string str = " " + name + "=\"" + tostr(value) + "\"";
931 string const write_attribute(string const & name, string const & value)
933 string str = " " + name + "=\"" + value + "\"";
938 string const write_attribute(string const & name, bool value)
940 string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
946 void LyXTabular::Write(Buffer const * buf, ostream & os) const
950 << write_attribute("version", 1)
951 << write_attribute("rows", rows_)
952 << write_attribute("columns", columns_)
954 // global longtable options
956 << write_attribute("rotate", rotate)
957 << write_attribute("islongtable", is_long_tabular)
958 << write_attribute("endhead", endhead)
959 << write_attribute("endfirsthead", endfirsthead)
960 << write_attribute("endfoot", endfoot)
961 << write_attribute("endlastfoot", endlastfoot)
963 for (int i = 0; i < rows_; ++i) {
965 << write_attribute("topline", row_info[i].top_line)
966 << write_attribute("bottomline", row_info[i].bottom_line)
967 << write_attribute("newpage", row_info[i].newpage)
969 for (int j = 0; j < columns_; ++j) {
972 << write_attribute("alignment", column_info[j].alignment)
973 << write_attribute("valignment", column_info[j].valignment)
974 << write_attribute("leftline", column_info[j].left_line)
975 << write_attribute("rightline", column_info[j].right_line)
976 << write_attribute("width",
977 VSpace(column_info[j].p_width)
979 << write_attribute("special", column_info[j].align_special)
985 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
986 << write_attribute("alignment", cell_info[i][j].alignment)
987 << write_attribute("valignment", cell_info[i][j].valignment)
988 << write_attribute("topline", cell_info[i][j].top_line)
989 << write_attribute("bottomline", cell_info[i][j].bottom_line)
990 << write_attribute("leftline", cell_info[i][j].left_line)
991 << write_attribute("rightline", cell_info[i][j].right_line)
992 << write_attribute("rotate", cell_info[i][j].rotate)
993 << write_attribute("usebox", cell_info[i][j].usebox)
994 << write_attribute("width", cell_info[i][j].p_width)
995 << write_attribute("special", cell_info[i][j].align_special)
997 os << "\\begin_inset ";
998 cell_info[i][j].inset.Write(buf, os);
999 os << "\n\\end_inset \n"
1005 os << "</LyXTabular>\n";
1010 bool getTokenValue(string const & str, const char * token, string & ret)
1012 string::size_type pos = str.find(token);
1013 char ch = str[pos + strlen(token)];
1015 if ((pos == string::npos) || (ch != '='))
1018 pos += strlen(token) + 1;
1020 if ((ch != '"') && (ch != '\'')) { // only read till next space
1024 while((pos < str.length() - 1) && (str[++pos] != ch))
1032 bool getTokenValue(string const & str, const char * token, int & num)
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
1048 while((pos < str.length() - 1) && isdigit(str[pos]))
1051 num = strToInt(ret);
1057 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1060 bool const ret = getTokenValue(str, token, tmp);
1061 num = static_cast<LyXAlignment>(tmp);
1067 bool getTokenValue(string const & str, const char * token,
1068 LyXTabular::VAlignment & num)
1071 bool const ret = getTokenValue(str, token, tmp);
1072 num = static_cast<LyXTabular::VAlignment>(tmp);
1078 bool getTokenValue(string const & str, const char * token,
1079 LyXTabular::BoxType & num)
1082 bool ret = getTokenValue(str, token, tmp);
1083 num = static_cast<LyXTabular::BoxType>(tmp);
1089 bool getTokenValue(string const & str, const char * token, bool & flag)
1091 string::size_type pos = str.find(token);
1092 char ch = str[pos + strlen(token)];
1094 if ((pos == string::npos) || (ch != '='))
1097 pos += strlen(token) + 1;
1099 if ((ch != '"') && (ch != '\'')) { // only read till next space
1105 while((pos < str.length() - 1) && isdigit(str[pos]))
1108 flag = strToInt(ret);
1114 void l_getline(istream & is, string & str)
1122 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1125 istream & is = lex.getStream();
1127 l_getline(is, line);
1128 if (!prefixIs(line, "<LyXTabular ")) {
1129 OldFormatRead(lex, line);
1136 if (!getTokenValue(line, "version", version))
1138 if (!getTokenValue(line, "rows", rows_arg))
1140 if (!getTokenValue(line, "columns", columns_arg))
1142 Init(rows_arg, columns_arg);
1143 l_getline(is, line);
1144 if (!prefixIs(line, "<Features ")) {
1145 lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1146 line << ")" << endl;
1149 getTokenValue(line, "islongtable", is_long_tabular);
1150 getTokenValue(line, "endhead", endhead);
1151 getTokenValue(line, "endfirsthead", endfirsthead);
1152 getTokenValue(line, "endfoot", endfoot);
1153 getTokenValue(line, "endlastfoot", endlastfoot);
1155 for (int i = 0; i < rows_; ++i) {
1156 l_getline(is, line);
1157 if (!prefixIs(line, "<Row ")) {
1158 lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1159 line << ")" << endl;
1162 getTokenValue(line, "topline", row_info[i].top_line);
1163 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1164 getTokenValue(line, "newpage", row_info[i].newpage);
1165 for (int j = 0; j < columns_; ++j) {
1167 if (!prefixIs(line,"<Column")) {
1168 lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1169 line << ")" << endl;
1173 getTokenValue(line, "alignment", column_info[j].alignment);
1174 getTokenValue(line, "valignment", column_info[j].valignment);
1175 getTokenValue(line, "leftline", column_info[j].left_line);
1176 getTokenValue(line, "rightline", column_info[j].right_line);
1177 getTokenValue(line, "width", column_info[j].p_width);
1178 getTokenValue(line, "special", column_info[j].align_special);
1180 l_getline(is, line);
1181 if (!prefixIs(line, "<Cell")) {
1182 lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1183 line << ")" << endl;
1186 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1187 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1188 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1189 getTokenValue(line, "topline", cell_info[i][j].top_line);
1190 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1191 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1192 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1193 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1194 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1195 getTokenValue(line, "width", cell_info[i][j].p_width);
1196 getTokenValue(line, "special", cell_info[i][j].align_special);
1197 l_getline(is, line);
1198 if (prefixIs(line, "\\begin_inset")) {
1199 cell_info[i][j].inset.Read(buf, lex);
1200 l_getline(is, line);
1202 if (line != "</Cell>") {
1203 lyxerr << "Wrong tabular format (expected </Cell> got" <<
1204 line << ")" << endl;
1207 l_getline(is, line);
1208 if (line != "</Column>") {
1209 lyxerr << "Wrong tabular format (expected </Column> got" <<
1210 line << ")" << endl;
1214 l_getline(is, line);
1215 if (line != "</Row>") {
1216 lyxerr << "Wrong tabular format (expected </Row> got" <<
1217 line << ")" << endl;
1221 while (line != "</LyXTabular>") {
1222 l_getline(is, line);
1224 set_row_column_number_info();
1228 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1233 int columns_arg = 0;
1234 int is_long_tabular_arg = false;
1235 int rotate_arg = false;
1244 istream & is = lex.getStream();
1247 version = lyx::atoi(s.substr(8, string::npos));
1251 vector<int> cont_row_info;
1254 lyxerr << "Tabular format < 5 is not supported anymore\n"
1255 "Get an older version of LyX (< 1.1.x) for conversion!"
1257 WriteAlert(_("Warning:"),
1258 _("Tabular format < 5 is not supported anymore\n"),
1259 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1261 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1262 >> rotate_arg >> a >> b >> c >> d;
1264 is >> rows_arg >> columns_arg;
1265 Init(rows_arg, columns_arg);
1266 cont_row_info = vector<int>(rows_arg);
1267 SetLongTabular(is_long_tabular_arg);
1268 SetRotateTabular(rotate_arg);
1270 for (i = 0; i < rows_; ++i) {
1272 cont_row_info[i] = false;
1274 for (i = 0; i < columns_; ++i) {
1277 for (i = 0; i < rows_; ++i) {
1278 for (j = 0; j < columns_; ++j) {
1283 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1284 >> rotate_arg >> a >> b >> c >> d;
1285 Init(rows_arg, columns_arg);
1286 cont_row_info = vector<int>(rows_arg);
1287 SetLongTabular(is_long_tabular_arg);
1288 SetRotateTabular(rotate_arg);
1293 for (i = 0; i < rows_; ++i) {
1294 a = b = c = d = e = f = g = 0;
1295 is >> a >> b >> c >> d;
1296 row_info[i].top_line = a;
1297 row_info[i].bottom_line = b;
1298 cont_row_info[i] = c;
1299 row_info[i].newpage = d;
1301 for (i = 0; i < columns_; ++i) {
1306 char ch; // skip '"'
1309 // ignore is buggy but we will use it later (Lgb)
1310 is.ignore(); // skip '"'
1312 getline(is, s1, '"');
1314 is >> ch; // skip '"'
1316 // ignore is buggy but we will use it later (Lgb)
1317 is.ignore(); // skip '"'
1319 getline(is, s2, '"');
1320 column_info[i].alignment = static_cast<LyXAlignment>(a);
1321 column_info[i].left_line = b;
1322 column_info[i].right_line = c;
1323 column_info[i].p_width = s1;
1324 column_info[i].align_special = s2;
1326 for (i = 0; i < rows_; ++i) {
1327 for (j = 0; j < columns_; ++j) {
1330 is >> a >> b >> c >> d >> e >> f >> g;
1333 is >> ch; // skip '"'
1335 // ignore is buggy but we will use it later (Lgb)
1336 is.ignore(); // skip '"'
1338 getline(is, s1, '"');
1340 is >> ch; // skip '"'
1342 // ignore is buggy but we will use it later (Lgb)
1343 is.ignore(); // skip '"'
1345 getline(is, s2, '"');
1346 cell_info[i][j].multicolumn = static_cast<char>(a);
1347 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1348 cell_info[i][j].top_line = static_cast<char>(c);
1349 cell_info[i][j].bottom_line = static_cast<char>(d);
1350 cell_info[i][j].left_line = column_info[j].left_line;
1351 cell_info[i][j].right_line = column_info[j].right_line;
1352 cell_info[i][j].rotate = static_cast<bool>(f);
1353 cell_info[i][j].usebox = static_cast<BoxType>(g);
1354 cell_info[i][j].align_special = s1;
1355 cell_info[i][j].p_width = s2;
1359 set_row_column_number_info(true);
1361 LyXParagraph * par = new LyXParagraph;
1362 LyXParagraph * return_par = 0;
1364 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1365 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1370 LyXFont font(LyXFont::ALL_SANE);
1371 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1373 while (lex.IsOK()) {
1375 string const token = lex.GetString();
1378 if (token == "\\layout"
1379 || token == "\\end_float"
1380 || token == "\\end_deeper") {
1381 lex.pushToken(token);
1384 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1396 lex.pushToken(token);
1400 lex.printError("New Paragraph allocated! This should not happen!");
1401 lex.pushToken(token);
1407 // now we have the par we should fill the insets with this!
1409 InsetText * inset = GetCellInset(cell);
1412 for (int i = 0; i < par->Last(); ++i) {
1413 if (par->IsNewline(i)) {
1415 if (cell > GetNumberOfCells()) {
1416 lyxerr << "Some error in reading old table format occured!" <<
1417 endl << "Terminating when reading cell[" << cell << "]!" <<
1422 row = row_of_cell(cell);
1423 if (cont_row_info[row]) {
1425 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1426 while(!IsFirstCellInRow(--cell));
1428 inset = GetCellInset(cell);
1431 inset = GetCellInset(cell);
1432 row = row_of_cell(cell);
1433 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1435 // insert a space instead
1437 par->InsertChar(i, ' ');
1440 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1441 inset->par->InsertFromMinibuffer(inset->par->Last());
1448 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1450 int const i = isColumn ? cell : column_of_cell(cell);
1452 if (!isColumn && IsMultiColumn(cell)) {
1453 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1454 return cellinfo_of_cell(cell)->align_special;
1456 switch (GetAlignment(cell)) {
1457 case LYX_ALIGN_LEFT:
1459 case LYX_ALIGN_RIGHT:
1466 if (!column_info[i].align_special.empty()) {
1467 return column_info[i].align_special;
1469 #ifdef IGNORE_THIS_FOR_NOW
1470 else if (!column_info[i].p_width.empty()) {
1472 file += column_info[i].p_width;
1477 switch (column_info[i].alignment) {
1478 case LYX_ALIGN_LEFT:
1480 case LYX_ALIGN_RIGHT:
1490 // cell <0 will tex the preamble
1491 // returns the number of printed newlines
1492 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1495 if (IsLastCell(cell)) {
1496 os << newlineAndDepth(--depth)
1498 << newlineAndDepth(--depth)
1500 << newlineAndDepth(--depth)
1502 << newlineAndDepth(--depth);
1503 if (is_long_tabular)
1507 << newlineAndDepth(--depth);
1512 if (is_long_tabular)
1518 << "' COLSEP='1' ROWSEP='1'>"
1519 << newlineAndDepth(++depth);
1521 for (int i = 0; i < columns_; ++i) {
1522 os << "<COLSPEC ALIGN='"
1523 << GetDocBookAlign(i, true)
1529 if (i == (columns_-1)) {
1532 if (column_info[i].right_line ||
1533 column_info[i+1].left_line)
1539 << newlineAndDepth(depth);
1541 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1542 if (column_info[i].left_line)
1547 << newlineAndDepth(++depth)
1549 << newlineAndDepth(++depth)
1551 << GetDocBookAlign(0)
1553 if (IsMultiColumn(0)) {
1554 os << " NAMEST='col1' NAMEEND='col"
1555 << cells_in_multicolumn(0)
1559 << newlineAndDepth(++depth);
1562 if (IsLastCellInRow(cell)) {
1563 os << newlineAndDepth(--depth)
1565 << newlineAndDepth(--depth)
1567 << newlineAndDepth(depth)
1569 << newlineAndDepth(++depth)
1571 << GetDocBookAlign(cell + 1)
1572 << "' VALIGN='middle'";
1573 if (IsMultiColumn(cell + 1)) {
1574 os << " NAMEST='col"
1575 << column_of_cell(cell + 1) + 1
1577 << column_of_cell(cell + 1) +
1578 cells_in_multicolumn(cell + 1)
1582 << newlineAndDepth(++depth);
1585 os << newlineAndDepth(--depth)
1587 << newlineAndDepth(depth)
1589 << GetDocBookAlign(cell + 1)
1590 << "' VALIGN='middle'";
1591 if (IsMultiColumn(cell + 1)) {
1592 os << " NAMEST='col"
1593 << column_of_cell(cell + 1) + 1
1595 << column_of_cell(cell + 1) +
1596 cells_in_multicolumn(cell + 1)
1600 << newlineAndDepth(++depth);
1609 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1611 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1612 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1616 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1618 int const row = row_of_cell(cell);
1619 int const column = column_of_cell(cell);
1620 return &cell_info[row][column];
1624 void LyXTabular::SetMultiColumn(int cell, int number)
1626 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1627 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1628 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1629 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1630 for (number--; number > 0; --number) {
1631 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1633 set_row_column_number_info();
1637 int LyXTabular::cells_in_multicolumn(int cell) const
1639 int const row = row_of_cell(cell);
1640 int column = column_of_cell(cell);
1643 while ((column < columns_) &&
1644 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1653 int LyXTabular::UnsetMultiColumn(int cell)
1655 int const row = row_of_cell(cell);
1656 int column = column_of_cell(cell);
1660 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1661 cell_info[row][column].multicolumn = CELL_NORMAL;
1663 while ((column < columns_) &&
1664 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1666 cell_info[row][column].multicolumn = CELL_NORMAL;
1671 set_row_column_number_info();
1676 void LyXTabular::SetLongTabular(bool what)
1678 is_long_tabular = what;
1682 bool LyXTabular::IsLongTabular() const
1684 return is_long_tabular;
1688 void LyXTabular::SetRotateTabular(bool flag)
1694 bool LyXTabular::GetRotateTabular() const
1700 void LyXTabular::SetRotateCell(int cell, bool flag)
1702 cellinfo_of_cell(cell)->rotate = flag;
1706 bool LyXTabular::GetRotateCell(int cell) const
1708 return cellinfo_of_cell(cell)->rotate;
1712 bool LyXTabular::NeedRotating() const
1716 for (int i = 0; i < rows_; ++i) {
1717 for (int j = 0; j < columns_; ++j) {
1718 if (cell_info[i][j].rotate)
1726 bool LyXTabular::IsLastCell(int cell) const
1728 if ((cell + 1) < GetNumberOfCells())
1734 int LyXTabular::GetCellAbove(int cell) const
1736 if (row_of_cell(cell) > 0)
1737 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1742 int LyXTabular::GetCellBelow(int cell) const
1744 if (row_of_cell(cell) + 1 < rows_)
1745 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1750 int LyXTabular::GetLastCellAbove(int cell) const
1752 if (row_of_cell(cell) <= 0)
1754 if (!IsMultiColumn(cell))
1755 return GetCellAbove(cell);
1756 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1760 int LyXTabular::GetLastCellBelow(int cell) const
1762 if (row_of_cell(cell) + 1 >= rows_)
1764 if (!IsMultiColumn(cell))
1765 return GetCellBelow(cell);
1766 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1770 int LyXTabular::GetCellNumber(int row, int column) const
1772 if (column >= columns_)
1773 column = columns_ - 1;
1774 else if (column < 0)
1781 return cell_info[row][column].cellno;
1785 void LyXTabular::SetUsebox(int cell, BoxType type)
1787 cellinfo_of_cell(cell)->usebox = type;
1791 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1793 if (column_info[column_of_cell(cell)].p_width.empty() &&
1794 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1796 if (cellinfo_of_cell(cell)->usebox > 1)
1797 return cellinfo_of_cell(cell)->usebox;
1798 return UseParbox(cell);
1802 void LyXTabular::SetLTHead(int cell, bool first)
1804 int const row = row_of_cell(cell);
1805 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1808 if (endfirsthead == val)
1821 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1824 if (abs(endhead) > rows_)
1826 return (row_of_cell(cell) == abs(endhead) - 1);
1830 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1833 if (abs(endfirsthead) > rows_)
1835 return (row_of_cell(cell) == abs(endfirsthead) - 1);
1839 void LyXTabular::SetLTFoot(int cell, bool last)
1841 int const row = row_of_cell(cell);
1842 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1845 if (endlastfoot == val)
1858 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1861 if ((endfoot + 1) > rows_)
1863 return (row_of_cell(cell) == abs(endfoot) - 1);
1867 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1870 if (abs(endlastfoot) > rows_)
1872 return (row_of_cell(cell) == (abs(endlastfoot)-1));
1876 void LyXTabular::SetLTNewPage(int cell, bool what)
1878 row_info[row_of_cell(cell)].newpage = what;
1882 bool LyXTabular::GetLTNewPage(int cell) const
1884 return row_info[row_of_cell(cell)].newpage;
1888 bool LyXTabular::SetAscentOfRow(int row, int height)
1890 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1892 row_info[row].ascent_of_row = height;
1897 bool LyXTabular::SetDescentOfRow(int row, int height)
1899 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1901 row_info[row].descent_of_row = height;
1906 int LyXTabular::GetAscentOfRow(int row) const
1910 return row_info[row].ascent_of_row;
1914 int LyXTabular::GetDescentOfRow(int row) const
1918 return row_info[row].descent_of_row;
1922 int LyXTabular::GetHeightOfTabular() const
1926 for (int row = 0; row < rows_; ++row)
1927 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1928 GetAdditionalHeight(GetCellNumber(row, 0));
1933 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1935 if ((row >= rows_) || (column >= columns_))
1937 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1941 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1943 int const fcell = GetFirstCellInRow(row);
1944 int const n = NumberOfCellsInRow(fcell) + fcell;
1947 for (int i = fcell; i < n; ++i) {
1951 if (tmp == (n - fcell)){
1954 for (int i = fcell; i < n; ++i) {
1957 << column_of_cell(i) + 1
1959 << right_column_of_cell(i) + 1
1971 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1973 int const fcell = GetFirstCellInRow(row);
1974 int const n = NumberOfCellsInRow(fcell) + fcell;
1977 for (int i = fcell; i < n; ++i) {
1981 if (tmp == (n-fcell)){
1984 for (int i = fcell; i < n; ++i) {
1985 if (BottomLine(i)) {
1987 << column_of_cell(i) + 1
1989 << right_column_of_cell(i) + 1
2001 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2005 if (GetRotateCell(cell)) {
2006 os << "\\begin{sideways}\n";
2009 if (IsMultiColumn(cell)) {
2010 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2011 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2012 os << cellinfo_of_cell(cell)->align_special << "}{";
2016 if (!GetPWidth(cell).empty()) {
2017 switch (GetVAlignment(cell)) {
2018 case LYX_VALIGN_TOP:
2021 case LYX_VALIGN_CENTER:
2024 case LYX_VALIGN_BOTTOM:
2028 os << "{" << GetPWidth(cell) << '}';
2030 switch (GetAlignment(cell)) {
2031 case LYX_ALIGN_LEFT:
2034 case LYX_ALIGN_RIGHT:
2042 if (RightLine(cell))
2044 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2050 if (GetUsebox(cell) == BOX_PARBOX) {
2052 switch (GetVAlignment(cell)) {
2053 case LYX_VALIGN_TOP:
2056 case LYX_VALIGN_CENTER:
2059 case LYX_VALIGN_BOTTOM:
2063 os << "]{" << GetPWidth(cell) << "}{";
2064 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2065 os << "\\begin{minipage}[";
2066 switch (GetVAlignment(cell)) {
2067 case LYX_VALIGN_TOP:
2070 case LYX_VALIGN_CENTER:
2073 case LYX_VALIGN_BOTTOM:
2077 os << "]{" << GetPWidth(cell) << "}\n";
2084 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2089 if (GetUsebox(cell) == BOX_PARBOX)
2091 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2092 os << "%\n\\end{minipage}";
2095 if (IsMultiColumn(cell)){
2098 if (GetRotateCell(cell)) {
2099 os << "%\n\\end{sideways}";
2106 int LyXTabular::Latex(Buffer const * buf,
2107 ostream & os, bool fragile, bool fp) const
2112 //+---------------------------------------------------------------------
2113 //+ first the opening preamble +
2114 //+---------------------------------------------------------------------
2117 os << "\\begin{sideways}\n";
2120 if (is_long_tabular)
2121 os << "\\begin{longtable}{";
2123 os << "\\begin{tabular}{";
2124 for (int i = 0; i < columns_; ++i) {
2125 if (column_info[i].left_line)
2127 if (!column_info[i].align_special.empty()) {
2128 os << column_info[i].align_special;
2129 } else if (!column_info[i].p_width.empty()) {
2130 switch (column_info[i].valignment) {
2131 case LYX_VALIGN_TOP:
2134 case LYX_VALIGN_CENTER:
2137 case LYX_VALIGN_BOTTOM:
2142 << column_info[i].p_width
2145 switch (column_info[i].alignment) {
2146 case LYX_ALIGN_LEFT:
2149 case LYX_ALIGN_RIGHT:
2157 if (column_info[i].right_line)
2163 //+---------------------------------------------------------------------
2164 //+ the single row and columns (cells) +
2165 //+---------------------------------------------------------------------
2168 for (int i = 0; i < rows_; ++i) {
2169 ret += TeXTopHLine(os, i);
2171 if (IsLongTabular()) {
2172 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2173 os << "\\endhead\n";
2176 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2177 os << "\\endfirsthead\n";
2180 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2181 os << "\\endfoot\n";
2184 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2185 os << "\\endlastfoot\n";
2191 ret += TeXBottomHLine(os, i-1);
2192 ret += TeXTopHLine(os, i);
2194 for (int j = 0; j < columns_; ++j) {
2195 if (IsPartOfMultiColumn(i,j))
2197 ret += TeXCellPreamble(os, cell);
2198 ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
2199 ret += TeXCellPostamble(os, cell);
2200 if (!IsLastCellInRow(cell)) { // not last cell in row
2207 ret += TeXBottomHLine(os, i);
2209 if (IsLongTabular()) {
2210 if ((endhead > 0) && (i == (endhead - 1))) {
2211 os << "\\endhead\n";
2214 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2215 os << "\\endfirsthead\n";
2218 if ((endfoot > 0) && (i == (endfoot - 1))) {
2219 os << "\\endfoot\n";
2222 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2223 os << "\\endlastfoot\n";
2227 ret += TeXBottomHLine(os, i);
2228 if (row_info[i].newpage) {
2229 os << "\\newpage\n";
2235 //+---------------------------------------------------------------------
2236 //+ the closing of the tabular +
2237 //+---------------------------------------------------------------------
2239 if (is_long_tabular)
2240 os << "\\end{longtable}";
2242 os << "\\end{tabular}";
2244 os << "\n\\end{sideways}";
2252 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2256 //+---------------------------------------------------------------------
2257 //+ first the opening preamble +
2258 //+---------------------------------------------------------------------
2260 os << "<tgroup cols=\"" << columns_
2261 << "\" colsep=\"1\" rowsep=\"1\">\n";
2263 for (int i = 0; i < columns_; ++i) {
2264 os << "<colspec colname=\"col" << i << "\" align=\"";
2265 switch (column_info[i].alignment) {
2266 case LYX_ALIGN_LEFT:
2269 case LYX_ALIGN_RIGHT:
2280 //+---------------------------------------------------------------------
2281 //+ the single row and columns (cells) +
2282 //+---------------------------------------------------------------------
2286 for (int i = 0; i < rows_; ++i) {
2288 for (int j = 0; j < columns_; ++j) {
2289 if (IsPartOfMultiColumn(i, j))
2292 os << "<entry align=\"";
2293 switch (GetAlignment(cell)) {
2294 case LYX_ALIGN_LEFT:
2297 case LYX_ALIGN_RIGHT:
2305 os << "\" valign=\"";
2306 switch (GetVAlignment(cell)) {
2307 case LYX_VALIGN_TOP:
2310 case LYX_VALIGN_BOTTOM:
2313 case LYX_VALIGN_CENTER:
2318 if (IsMultiColumn(cell)) {
2319 os << " namest=\"col" << j << "\" ";
2320 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2324 ret += GetCellInset(cell)->DocBook(buf, os);
2331 //+---------------------------------------------------------------------
2332 //+ the closing of the tabular +
2333 //+---------------------------------------------------------------------
2344 void print_n_chars(ostream & os, unsigned char ch, int n)
2346 os << string(n, ch);
2350 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2351 vector<unsigned int> const & clen) const
2353 int const fcell = GetFirstCellInRow(row);
2354 int const n = NumberOfCellsInRow(fcell) + fcell;
2357 for (int i = fcell; i < n; ++i) {
2367 for (int i = fcell; i < n; ++i) {
2378 int column = column_of_cell(i);
2379 int len = clen[column];
2380 while(IsPartOfMultiColumn(row, ++column))
2381 len += clen[column] + 4;
2382 print_n_chars(os, ch, len);
2397 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2398 vector<unsigned int> const & clen) const
2400 int const fcell = GetFirstCellInRow(row);
2401 int const n = NumberOfCellsInRow(fcell) + fcell;
2404 for (int i = fcell; i < n; ++i) {
2405 if (BottomLine(i)) {
2414 for (int i = fcell; i < n; ++i) {
2415 if (BottomLine(i)) {
2425 int column = column_of_cell(i);
2426 int len = clen[column];
2427 while(IsPartOfMultiColumn(row, ++column))
2428 len += clen[column] + 4;
2429 print_n_chars(os, ch, len);
2430 if (BottomLine(i)) {
2444 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2445 int cell, int row, int column,
2446 vector<unsigned int> const & clen) const
2449 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2456 unsigned int len1 = sstr.str().length();
2457 unsigned int len2 = clen[column];
2458 while(IsPartOfMultiColumn(row, ++column))
2459 len2 += clen[column] + 4;
2462 switch (GetAlignment(cell)) {
2464 case LYX_ALIGN_LEFT:
2467 case LYX_ALIGN_RIGHT:
2471 case LYX_ALIGN_CENTER:
2477 for (unsigned int i = 0; i < len1; ++i)
2480 for (unsigned int i = 0; i < len2; ++i)
2482 if (RightLine(cell))
2487 return ret * 0; // eh? (Lgb)
2491 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2495 //+---------------------------------------------------------------------
2496 //+ first calculate the width of the single columns +
2497 //+---------------------------------------------------------------------
2498 vector<unsigned int> clen(columns_);
2500 // first all non (real) multicolumn cells!
2501 for (int j = 0; j < columns_; ++j) {
2503 for (int i = 0; i < rows_; ++i) {
2504 int cell = GetCellNumber(i, j);
2505 if (IsMultiColumn(cell, true))
2508 GetCellInset(cell)->Ascii(buf, sstr, 0);
2509 if (clen[j] < sstr.str().length())
2510 clen[j] = sstr.str().length();
2513 // then all (real) multicolumn cells!
2514 for (int j = 0; j < columns_; ++j) {
2515 for (int i = 0; i < rows_; ++i) {
2516 int cell = GetCellNumber(i, j);
2517 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2520 GetCellInset(cell)->Ascii(buf, sstr, 0);
2521 int len = int(sstr.str().length());
2522 int const n = cells_in_multicolumn(cell);
2523 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2525 if (len > int(clen[j + n - 1]))
2526 clen[j + n - 1] = len;
2530 for (int i = 0; i < rows_; ++i) {
2531 AsciiTopHLine(os, i, clen);
2532 for (int j = 0; j < columns_; ++j) {
2533 if (IsPartOfMultiColumn(i,j))
2535 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2539 AsciiBottomHLine(os, i, clen);
2545 InsetText * LyXTabular::GetCellInset(int cell) const
2547 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2551 void LyXTabular::Validate(LaTeXFeatures & features) const
2553 if (IsLongTabular())
2554 features.longtable = true;
2556 features.rotating = true;
2557 for (int cell = 0; cell < numberofcells; ++cell) {
2558 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2559 features.array = true;
2560 GetCellInset(cell)->Validate(features);
2565 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2567 LyXParagraph * par = GetCellInset(cell)->par;
2569 for (; par; par = par->next) {
2570 for (int i = 0; i < par->Last(); ++i) {
2571 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)