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)) + "\"";
948 // string const type2string(LyXAlignment num)
951 // case LYX_ALIGN_NONE:
953 // case LYX_ALIGN_BLOCK:
955 // case LYX_ALIGN_LEFT:
957 // case LYX_ALIGN_CENTER:
959 // case LYX_ALIGN_RIGHT:
961 // case LYX_ALIGN_LAYOUT:
963 // case LYX_ALIGN_SPECIAL:
971 // string const type2string(LyXTabular::VAlignment num)
974 // case LyXTabular::LYX_VALIGN_TOP:
976 // case LyXTabular::LYX_VALIGN_CENTER:
978 // case LyXTabular::LYX_VALIGN_BOTTOM:
986 // string const type2string(LyXTabular::BoxType num)
989 // case LyXTabular::BOX_NONE:
991 // case LyXTabular::BOX_PARBOX:
993 // case LyXTabular::BOX_MINIPAGE:
994 // return "minipage";
1000 // string const type2string(bool flag)
1002 // return (flag ? "true" : "false");
1007 string const tostr(LyXAlignment const & num)
1010 case LYX_ALIGN_NONE:
1012 case LYX_ALIGN_BLOCK:
1014 case LYX_ALIGN_LEFT:
1016 case LYX_ALIGN_CENTER:
1018 case LYX_ALIGN_RIGHT:
1020 case LYX_ALIGN_LAYOUT:
1022 case LYX_ALIGN_SPECIAL:
1031 string const tostr(LyXTabular::VAlignment const & num)
1034 case LyXTabular::LYX_VALIGN_TOP:
1036 case LyXTabular::LYX_VALIGN_CENTER:
1038 case LyXTabular::LYX_VALIGN_BOTTOM:
1047 string const tostr(LyXTabular::BoxType const & num)
1050 case LyXTabular::BOX_NONE:
1052 case LyXTabular::BOX_PARBOX:
1054 case LyXTabular::BOX_MINIPAGE:
1060 // We already have a function like this in lstring.h (Lgb)
1061 //string const type2string(bool flag)
1063 // return (flag ? "true" : "false");
1072 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1076 << write_attribute("version", 2)
1077 << write_attribute("rows", rows_)
1078 << write_attribute("columns", columns_)
1080 // global longtable options
1083 // << write_attribute("rotate", type2string(rotate))
1084 // << write_attribute("islongtable", type2string(is_long_tabular))
1086 << write_attribute("rotate", tostr(rotate))
1087 << write_attribute("islongtable", tostr(is_long_tabular))
1089 << write_attribute("endhead", endhead)
1090 << write_attribute("endfirsthead", endfirsthead)
1091 << write_attribute("endfoot", endfoot)
1092 << write_attribute("endlastfoot", endlastfoot)
1094 for (int j = 0; j < columns_; ++j) {
1097 // << write_attribute("alignment", type2string(column_info[j].alignment))
1098 // << write_attribute("valignment", type2string(column_info[j].valignment))
1099 // << write_attribute("leftline", type2string(column_info[j].left_line))
1100 // << write_attribute("rightline", type2string(column_info[j].right_line))
1102 << write_attribute("alignment", tostr(column_info[j].alignment))
1103 << write_attribute("valignment", tostr(column_info[j].valignment))
1104 << write_attribute("leftline", tostr(column_info[j].left_line))
1105 << write_attribute("rightline", tostr(column_info[j].right_line))
1107 << write_attribute("width",
1108 VSpace(column_info[j].p_width)
1110 << write_attribute("special", column_info[j].align_special)
1113 for (int i = 0; i < rows_; ++i) {
1116 // << write_attribute("topline", type2string(row_info[i].top_line))
1117 // << write_attribute("bottomline", type2string(row_info[i].bottom_line))
1118 // << write_attribute("newpage", type2string(row_info[i].newpage))
1120 << write_attribute("topline", tostr(row_info[i].top_line))
1121 << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1122 << write_attribute("newpage", tostr(row_info[i].newpage))
1125 for (int j = 0; j < columns_; ++j) {
1130 // << write_attribute("alignment", type2string(column_info[j].alignment))
1131 // << write_attribute("valignment", type2string(column_info[j].valignment))
1132 // << write_attribute("leftline", type2string(column_info[j].left_line))
1133 // << write_attribute("rightline", type2string(column_info[j].right_line))
1135 << write_attribute("alignment", tostr(column_info[j].alignment))
1136 << write_attribute("valignment", tostr(column_info[j].valignment))
1137 << write_attribute("leftline", tostr(column_info[j].left_line))
1138 << write_attribute("rightline", tostr(column_info[j].right_line))
1141 << write_attribute("width",
1142 VSpace(column_info[j].p_width)
1144 << write_attribute("special", column_info[j].align_special)
1151 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1153 // << write_attribute("alignment", type2string(cell_info[i][j].alignment))
1154 // << write_attribute("valignment", type2string(cell_info[i][j].valignment))
1155 // << write_attribute("topline", type2string(cell_info[i][j].top_line))
1156 // << write_attribute("bottomline", type2string(cell_info[i][j].bottom_line))
1157 // << write_attribute("leftline", type2string(cell_info[i][j].left_line))
1158 // << write_attribute("rightline", type2string(cell_info[i][j].right_line))
1159 // << write_attribute("rotate", type2string(cell_info[i][j].rotate))
1160 // << write_attribute("usebox", type2string(cell_info[i][j].usebox))
1162 << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1163 << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1164 << write_attribute("topline", tostr(cell_info[i][j].top_line))
1165 << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1166 << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1167 << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1168 << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1169 << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1171 << write_attribute("width", cell_info[i][j].p_width)
1172 << write_attribute("special", cell_info[i][j].align_special)
1174 os << "\\begin_inset ";
1175 cell_info[i][j].inset.Write(buf, os);
1176 os << "\n\\end_inset \n"
1184 os << "</lyxtabular>\n";
1188 // I would have liked a fromstr template a lot better. (Lgb)
1191 bool string2type(string const str, LyXAlignment & num)
1194 num = LYX_ALIGN_NONE;
1195 else if (str == "block")
1196 num = LYX_ALIGN_BLOCK;
1197 else if (str == "left")
1198 num = LYX_ALIGN_LEFT;
1199 else if (str == "center")
1200 num = LYX_ALIGN_CENTER;
1201 else if (str == "right")
1202 num = LYX_ALIGN_RIGHT;
1210 bool string2type(string const str, LyXTabular::VAlignment & num)
1213 num = LyXTabular::LYX_VALIGN_TOP;
1214 else if (str == "center")
1215 num = LyXTabular::LYX_VALIGN_CENTER;
1216 else if (str == "bottom")
1217 num = LyXTabular::LYX_VALIGN_BOTTOM;
1225 bool string2type(string const str, LyXTabular::BoxType & num)
1228 num = LyXTabular::BOX_NONE;
1229 else if (str == "parbox")
1230 num = LyXTabular::BOX_PARBOX;
1231 else if (str == "minipage")
1232 num = LyXTabular::BOX_MINIPAGE;
1240 bool string2type(string const str, bool & num)
1244 else if (str == "false")
1253 bool getTokenValue(string const & str, const char * token, string & ret)
1255 size_t token_length = strlen(token);
1256 string::size_type pos = str.find(token);
1258 if (pos == string::npos || pos+token_length+1 >= str.length()
1259 || str[pos+token_length] != '=')
1262 pos += token_length + 1;
1264 if ((ch != '"') && (ch != '\'')) { // only read till next space
1268 while((pos < str.length() - 1) && (str[++pos] != ch))
1275 //#define USE_OLD_READFORMAT
1277 //#ifndef USE_OLD_READFORMAT
1279 bool getTokenValue(string const & str, const char * token, int & num)
1282 if (!getTokenValue(str, token, tmp))
1284 num = strToInt(tmp);
1290 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1293 if (!getTokenValue(str, token, tmp))
1295 return string2type(tmp, num);
1300 bool getTokenValue(string const & str, const char * token,
1301 LyXTabular::VAlignment & num)
1304 if (!getTokenValue(str, token, tmp))
1306 return string2type(tmp, num);
1311 bool getTokenValue(string const & str, const char * token,
1312 LyXTabular::BoxType & num)
1315 if (!getTokenValue(str, token, tmp))
1317 return string2type(tmp, num);
1322 bool getTokenValue(string const & str, const char * token, bool & flag)
1325 if (!getTokenValue(str, token, tmp))
1327 return string2type(tmp, flag);
1333 // bool getTokenValue(string const & str, const char * token, int & num)
1335 // string::size_type pos = str.find(token);
1336 // char ch = str[pos + strlen(token)];
1338 // if ((pos == string::npos) || (ch != '='))
1341 // pos += strlen(token) + 1;
1343 // if ((ch != '"') && (ch != '\'')) { // only read till next space
1344 // if (!isdigit(ch))
1349 // while((pos < str.length() - 1) && isdigit(str[pos]))
1350 // ret += str[pos++];
1352 // num = strToInt(ret);
1358 // bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1361 // bool const ret = getTokenValue(str, token, tmp);
1362 // num = static_cast<LyXAlignment>(tmp);
1368 // bool getTokenValue(string const & str, const char * token,
1369 // LyXTabular::VAlignment & num)
1372 // bool const ret = getTokenValue(str, token, tmp);
1373 // num = static_cast<LyXTabular::VAlignment>(tmp);
1379 // bool getTokenValue(string const & str, const char * token,
1380 // LyXTabular::BoxType & num)
1383 // bool ret = getTokenValue(str, token, tmp);
1384 // num = static_cast<LyXTabular::BoxType>(tmp);
1390 // bool getTokenValue(string const & str, const char * token, bool & flag)
1392 // string::size_type pos = str.find(token);
1393 // char ch = str[pos + strlen(token)];
1395 // if ((pos == string::npos) || (ch != '='))
1398 // pos += strlen(token) + 1;
1400 // if ((ch != '"') && (ch != '\'')) { // only read till next space
1401 // if (!isdigit(ch))
1406 // while((pos < str.length() - 1) && isdigit(str[pos]))
1407 // ret += str[pos++];
1409 // flag = strToInt(ret);
1416 void l_getline(istream & is, string & str)
1424 //#ifndef USE_OLD_READFORMAT
1426 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1429 istream & is = lex.getStream();
1431 l_getline(is, line);
1432 if (!prefixIs(line, "<lyxtabular ")
1433 && !prefixIs(line, "<LyXTabular ")) {
1434 OldFormatRead(lex, line);
1439 if (!getTokenValue(line, "version", version))
1442 ReadOld(buf, is, lex, line);
1443 else if (version == 2)
1444 ReadNew(buf, is, lex, line);
1449 //void LyXTabular::ReadNew(Buffer const * buf, LyXLex & lex)
1452 // istream & is = lex.getStream();
1454 // l_getline(is, line);
1455 // if (!prefixIs(line, "<LyXTabular ")) {
1456 // OldFormatRead(lex, line);
1461 // if (!getTokenValue(line, "version", version))
1464 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1465 LyXLex & lex, string const & l)
1470 if (!getTokenValue(line, "rows", rows_arg))
1473 if (!getTokenValue(line, "columns", columns_arg))
1475 Init(rows_arg, columns_arg);
1476 l_getline(is, line);
1477 if (!prefixIs(line, "<features")) {
1478 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1479 line << ")" << endl;
1482 getTokenValue(line, "rotate", rotate);
1483 getTokenValue(line, "islongtable", is_long_tabular);
1484 getTokenValue(line, "endhead", endhead);
1485 getTokenValue(line, "endfirsthead", endfirsthead);
1486 getTokenValue(line, "endfoot", endfoot);
1487 getTokenValue(line, "endlastfoot", endlastfoot);
1489 for (int j = 0; j < columns_; ++j) {
1491 if (!prefixIs(line,"<column")) {
1492 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1493 line << ")" << endl;
1496 getTokenValue(line, "alignment", column_info[j].alignment);
1497 getTokenValue(line, "valignment", column_info[j].valignment);
1498 getTokenValue(line, "leftline", column_info[j].left_line);
1499 getTokenValue(line, "rightline", column_info[j].right_line);
1500 getTokenValue(line, "width", column_info[j].p_width);
1501 getTokenValue(line, "special", column_info[j].align_special);
1504 for (int i = 0; i < rows_; ++i) {
1505 l_getline(is, line);
1506 if (!prefixIs(line, "<row")) {
1507 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1508 line << ")" << endl;
1511 getTokenValue(line, "topline", row_info[i].top_line);
1512 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1513 getTokenValue(line, "newpage", row_info[i].newpage);
1514 for (int j = 0; j < columns_; ++j) {
1515 l_getline(is, line);
1516 if (!prefixIs(line, "<cell")) {
1517 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1518 line << ")" << endl;
1521 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1522 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1523 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1524 getTokenValue(line, "topline", cell_info[i][j].top_line);
1525 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1526 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1527 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1528 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1529 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1530 getTokenValue(line, "width", cell_info[i][j].p_width);
1531 getTokenValue(line, "special", cell_info[i][j].align_special);
1532 l_getline(is, line);
1533 if (prefixIs(line, "\\begin_inset")) {
1534 cell_info[i][j].inset.Read(buf, lex);
1535 l_getline(is, line);
1537 if (line != "</cell>") {
1538 lyxerr << "Wrong tabular format (expected </cell> got" <<
1539 line << ")" << endl;
1543 l_getline(is, line);
1544 if (line != "</row>") {
1545 lyxerr << "Wrong tabular format (expected </row> got" <<
1546 line << ")" << endl;
1550 while (line != "</lyxtabular>") {
1551 l_getline(is, line);
1553 set_row_column_number_info();
1558 // void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1561 // istream & is = lex.getStream();
1563 // l_getline(is, line);
1564 // if (!prefixIs(line, "<LyXTabular ")) {
1565 // OldFormatRead(lex, line);
1572 // if (!getTokenValue(line, "version", version))
1574 // if (!getTokenValue(line, "rows", rows_arg))
1576 // if (!getTokenValue(line, "columns", columns_arg))
1578 // Init(rows_arg, columns_arg);
1579 // l_getline(is, line);
1580 // if (!prefixIs(line, "<Features ")) {
1581 // lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
1582 // line << ")" << endl;
1585 // getTokenValue(line, "islongtable", is_long_tabular);
1586 // getTokenValue(line, "endhead", endhead);
1587 // getTokenValue(line, "endfirsthead", endfirsthead);
1588 // getTokenValue(line, "endfoot", endfoot);
1589 // getTokenValue(line, "endlastfoot", endlastfoot);
1591 // for (int i = 0; i < rows_; ++i) {
1592 // l_getline(is, line);
1593 // if (!prefixIs(line, "<Row ")) {
1594 // lyxerr << "Wrong tabular format (expected <Row ...> got" <<
1595 // line << ")" << endl;
1598 // getTokenValue(line, "topline", row_info[i].top_line);
1599 // getTokenValue(line, "bottomline", row_info[i].bottom_line);
1600 // getTokenValue(line, "newpage", row_info[i].newpage);
1601 // for (int j = 0; j < columns_; ++j) {
1602 // l_getline(is,line);
1603 // if (!prefixIs(line,"<Column")) {
1604 // lyxerr << "Wrong tabular format (expected <Column ...> got" <<
1605 // line << ")" << endl;
1609 // getTokenValue(line, "alignment", column_info[j].alignment);
1610 // getTokenValue(line, "valignment", column_info[j].valignment);
1611 // getTokenValue(line, "leftline", column_info[j].left_line);
1612 // getTokenValue(line, "rightline", column_info[j].right_line);
1613 // getTokenValue(line, "width", column_info[j].p_width);
1614 // getTokenValue(line, "special", column_info[j].align_special);
1616 // l_getline(is, line);
1617 // if (!prefixIs(line, "<Cell")) {
1618 // lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
1619 // line << ")" << endl;
1622 // getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1623 // getTokenValue(line, "alignment", cell_info[i][j].alignment);
1624 // getTokenValue(line, "valignment", cell_info[i][j].valignment);
1625 // getTokenValue(line, "topline", cell_info[i][j].top_line);
1626 // getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1627 // getTokenValue(line, "leftline", cell_info[i][j].left_line);
1628 // getTokenValue(line, "rightline", cell_info[i][j].right_line);
1629 // getTokenValue(line, "rotate", cell_info[i][j].rotate);
1630 // getTokenValue(line, "usebox", cell_info[i][j].usebox);
1631 // getTokenValue(line, "width", cell_info[i][j].p_width);
1632 // getTokenValue(line, "special", cell_info[i][j].align_special);
1633 // l_getline(is, line);
1634 // if (prefixIs(line, "\\begin_inset")) {
1635 // cell_info[i][j].inset.Read(buf, lex);
1636 // l_getline(is, line);
1638 // if (line != "</Cell>") {
1639 // lyxerr << "Wrong tabular format (expected </Cell> got" <<
1640 // line << ")" << endl;
1643 // l_getline(is, line);
1644 // if (line != "</Column>") {
1645 // lyxerr << "Wrong tabular format (expected </Column> got" <<
1646 // line << ")" << endl;
1650 // l_getline(is, line);
1651 // if (line != "</Row>") {
1652 // lyxerr << "Wrong tabular format (expected </Row> got" <<
1653 // line << ")" << endl;
1657 // while (line != "</LyXTabular>") {
1658 // l_getline(is, line);
1660 // set_row_column_number_info();
1664 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1669 int columns_arg = 0;
1670 int is_long_tabular_arg = false;
1671 int rotate_arg = false;
1680 istream & is = lex.getStream();
1683 version = lyx::atoi(s.substr(8, string::npos));
1687 vector<int> cont_row_info;
1690 lyxerr << "Tabular format < 5 is not supported anymore\n"
1691 "Get an older version of LyX (< 1.1.x) for conversion!"
1693 WriteAlert(_("Warning:"),
1694 _("Tabular format < 5 is not supported anymore\n"),
1695 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1697 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1698 >> rotate_arg >> a >> b >> c >> d;
1700 is >> rows_arg >> columns_arg;
1701 Init(rows_arg, columns_arg);
1702 cont_row_info = vector<int>(rows_arg);
1703 SetLongTabular(is_long_tabular_arg);
1704 SetRotateTabular(rotate_arg);
1706 for (i = 0; i < rows_; ++i) {
1708 cont_row_info[i] = false;
1710 for (i = 0; i < columns_; ++i) {
1713 for (i = 0; i < rows_; ++i) {
1714 for (j = 0; j < columns_; ++j) {
1719 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1720 >> rotate_arg >> a >> b >> c >> d;
1721 Init(rows_arg, columns_arg);
1722 cont_row_info = vector<int>(rows_arg);
1723 SetLongTabular(is_long_tabular_arg);
1724 SetRotateTabular(rotate_arg);
1726 endfirsthead = b + 1;
1728 endlastfoot = d + 1;
1729 for (i = 0; i < rows_; ++i) {
1730 a = b = c = d = e = f = g = 0;
1731 is >> a >> b >> c >> d;
1732 row_info[i].top_line = a;
1733 row_info[i].bottom_line = b;
1734 cont_row_info[i] = c;
1735 row_info[i].newpage = d;
1737 for (i = 0; i < columns_; ++i) {
1742 char ch; // skip '"'
1745 // ignore is buggy but we will use it later (Lgb)
1746 is.ignore(); // skip '"'
1748 getline(is, s1, '"');
1750 is >> ch; // skip '"'
1752 // ignore is buggy but we will use it later (Lgb)
1753 is.ignore(); // skip '"'
1755 getline(is, s2, '"');
1756 column_info[i].alignment = static_cast<LyXAlignment>(a);
1757 column_info[i].left_line = b;
1758 column_info[i].right_line = c;
1759 column_info[i].p_width = s1;
1760 column_info[i].align_special = s2;
1762 for (i = 0; i < rows_; ++i) {
1763 for (j = 0; j < columns_; ++j) {
1766 is >> a >> b >> c >> d >> e >> f >> g;
1769 is >> ch; // skip '"'
1771 // ignore is buggy but we will use it later (Lgb)
1772 is.ignore(); // skip '"'
1774 getline(is, s1, '"');
1776 is >> ch; // skip '"'
1778 // ignore is buggy but we will use it later (Lgb)
1779 is.ignore(); // skip '"'
1781 getline(is, s2, '"');
1782 cell_info[i][j].multicolumn = static_cast<char>(a);
1783 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1784 cell_info[i][j].top_line = static_cast<char>(c);
1785 cell_info[i][j].bottom_line = static_cast<char>(d);
1786 cell_info[i][j].left_line = column_info[j].left_line;
1787 cell_info[i][j].right_line = column_info[j].right_line;
1788 cell_info[i][j].rotate = static_cast<bool>(f);
1789 cell_info[i][j].usebox = static_cast<BoxType>(g);
1790 cell_info[i][j].align_special = s1;
1791 cell_info[i][j].p_width = s2;
1795 set_row_column_number_info(true);
1797 LyXParagraph * par = new LyXParagraph;
1798 LyXParagraph * return_par = 0;
1800 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
1801 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
1806 LyXFont font(LyXFont::ALL_INHERIT);
1807 font.setLanguage(owner_->BufferOwner()->GetLanguage());
1809 while (lex.IsOK()) {
1811 string const token = lex.GetString();
1814 if (token == "\\layout"
1815 || token == "\\end_float"
1816 || token == "\\end_deeper") {
1817 lex.pushToken(token);
1820 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1832 lex.pushToken(token);
1836 lex.printError("New Paragraph allocated! This should not happen!");
1837 lex.pushToken(token);
1843 // now we have the par we should fill the insets with this!
1845 InsetText * inset = GetCellInset(cell);
1848 for (int i = 0; i < par->Last(); ++i) {
1849 if (par->IsNewline(i)) {
1851 if (cell > GetNumberOfCells()) {
1852 lyxerr << "Some error in reading old table format occured!" <<
1853 endl << "Terminating when reading cell[" << cell << "]!" <<
1858 row = row_of_cell(cell);
1859 if (cont_row_info[row]) {
1861 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1862 while(!IsFirstCellInRow(--cell));
1864 inset = GetCellInset(cell);
1867 inset = GetCellInset(cell);
1868 row = row_of_cell(cell);
1869 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1871 // insert a space instead
1873 par->InsertChar(i, ' ');
1876 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1877 inset->par->InsertFromMinibuffer(inset->par->Last());
1884 string const LyXTabular::GetDocBookAlign(int cell, bool isColumn) const
1886 int const i = isColumn ? cell : column_of_cell(cell);
1888 if (!isColumn && IsMultiColumn(cell)) {
1889 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1890 return cellinfo_of_cell(cell)->align_special;
1892 switch (GetAlignment(cell)) {
1893 case LYX_ALIGN_LEFT:
1895 case LYX_ALIGN_RIGHT:
1902 if (!column_info[i].align_special.empty()) {
1903 return column_info[i].align_special;
1905 #ifdef IGNORE_THIS_FOR_NOW
1906 else if (!column_info[i].p_width.empty()) {
1908 file += column_info[i].p_width;
1913 switch (column_info[i].alignment) {
1914 case LYX_ALIGN_LEFT:
1916 case LYX_ALIGN_RIGHT:
1926 // cell <0 will tex the preamble
1927 // returns the number of printed newlines
1928 int LyXTabular::DocBookEndOfCell(ostream & os, int cell, int & depth) const
1931 if (IsLastCell(cell)) {
1932 os << newlineAndDepth(--depth)
1934 << newlineAndDepth(--depth)
1936 << newlineAndDepth(--depth)
1938 << newlineAndDepth(--depth);
1939 if (is_long_tabular)
1943 << newlineAndDepth(--depth);
1948 if (is_long_tabular)
1954 << "' COLSEP='1' ROWSEP='1'>"
1955 << newlineAndDepth(++depth);
1957 for (int i = 0; i < columns_; ++i) {
1958 os << "<COLSPEC ALIGN='"
1959 << GetDocBookAlign(i, true)
1965 if (i == (columns_-1)) {
1968 if (column_info[i].right_line ||
1969 column_info[i+1].left_line)
1975 << newlineAndDepth(depth);
1977 #ifdef NOT_HANDLED_YET_AS_I_DONT_KNOW_HOW
1978 if (column_info[i].left_line)
1983 << newlineAndDepth(++depth)
1985 << newlineAndDepth(++depth)
1987 << GetDocBookAlign(0)
1989 if (IsMultiColumn(0)) {
1990 os << " NAMEST='col1' NAMEEND='col"
1991 << cells_in_multicolumn(0)
1995 << newlineAndDepth(++depth);
1998 if (IsLastCellInRow(cell)) {
1999 os << newlineAndDepth(--depth)
2001 << newlineAndDepth(--depth)
2003 << newlineAndDepth(depth)
2005 << newlineAndDepth(++depth)
2007 << GetDocBookAlign(cell + 1)
2008 << "' VALIGN='middle'";
2009 if (IsMultiColumn(cell + 1)) {
2010 os << " NAMEST='col"
2011 << column_of_cell(cell + 1) + 1
2013 << column_of_cell(cell + 1) +
2014 cells_in_multicolumn(cell + 1)
2018 << newlineAndDepth(++depth);
2021 os << newlineAndDepth(--depth)
2023 << newlineAndDepth(depth)
2025 << GetDocBookAlign(cell + 1)
2026 << "' VALIGN='middle'";
2027 if (IsMultiColumn(cell + 1)) {
2028 os << " NAMEST='col"
2029 << column_of_cell(cell + 1) + 1
2031 << column_of_cell(cell + 1) +
2032 cells_in_multicolumn(cell + 1)
2036 << newlineAndDepth(++depth);
2045 bool LyXTabular::IsMultiColumn(int cell, bool real) const
2047 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
2048 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
2052 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
2054 int const row = row_of_cell(cell);
2055 int const column = column_of_cell(cell);
2056 return &cell_info[row][column];
2060 void LyXTabular::SetMultiColumn(int cell, int number)
2062 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
2063 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
2064 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
2065 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
2066 for (number--; number > 0; --number) {
2067 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
2069 set_row_column_number_info();
2073 int LyXTabular::cells_in_multicolumn(int cell) const
2075 int const row = row_of_cell(cell);
2076 int column = column_of_cell(cell);
2079 while ((column < columns_) &&
2080 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
2089 int LyXTabular::UnsetMultiColumn(int cell)
2091 int const row = row_of_cell(cell);
2092 int column = column_of_cell(cell);
2096 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
2097 cell_info[row][column].multicolumn = CELL_NORMAL;
2099 while ((column < columns_) &&
2100 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
2102 cell_info[row][column].multicolumn = CELL_NORMAL;
2107 set_row_column_number_info();
2112 void LyXTabular::SetLongTabular(bool what)
2114 is_long_tabular = what;
2118 bool LyXTabular::IsLongTabular() const
2120 return is_long_tabular;
2124 void LyXTabular::SetRotateTabular(bool flag)
2130 bool LyXTabular::GetRotateTabular() const
2136 void LyXTabular::SetRotateCell(int cell, bool flag)
2138 cellinfo_of_cell(cell)->rotate = flag;
2142 bool LyXTabular::GetRotateCell(int cell) const
2144 return cellinfo_of_cell(cell)->rotate;
2148 bool LyXTabular::NeedRotating() const
2152 for (int i = 0; i < rows_; ++i) {
2153 for (int j = 0; j < columns_; ++j) {
2154 if (cell_info[i][j].rotate)
2162 bool LyXTabular::IsLastCell(int cell) const
2164 if ((cell + 1) < GetNumberOfCells())
2170 int LyXTabular::GetCellAbove(int cell) const
2172 if (row_of_cell(cell) > 0)
2173 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
2178 int LyXTabular::GetCellBelow(int cell) const
2180 if (row_of_cell(cell) + 1 < rows_)
2181 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
2186 int LyXTabular::GetLastCellAbove(int cell) const
2188 if (row_of_cell(cell) <= 0)
2190 if (!IsMultiColumn(cell))
2191 return GetCellAbove(cell);
2192 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
2196 int LyXTabular::GetLastCellBelow(int cell) const
2198 if (row_of_cell(cell) + 1 >= rows_)
2200 if (!IsMultiColumn(cell))
2201 return GetCellBelow(cell);
2202 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
2206 int LyXTabular::GetCellNumber(int row, int column) const
2208 if (column >= columns_)
2209 column = columns_ - 1;
2210 else if (column < 0)
2217 return cell_info[row][column].cellno;
2221 void LyXTabular::SetUsebox(int cell, BoxType type)
2223 cellinfo_of_cell(cell)->usebox = type;
2227 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
2229 if (column_info[column_of_cell(cell)].p_width.empty() &&
2230 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
2232 if (cellinfo_of_cell(cell)->usebox > 1)
2233 return cellinfo_of_cell(cell)->usebox;
2234 return UseParbox(cell);
2238 void LyXTabular::SetLTHead(int cell, bool first)
2240 int const row = row_of_cell(cell);
2241 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
2244 if (endfirsthead == val)
2257 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
2260 if (abs(endhead) > rows_)
2262 return (row_of_cell(cell) == abs(endhead) - 1);
2266 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
2269 if (abs(endfirsthead) > rows_)
2271 return (row_of_cell(cell) == abs(endfirsthead) - 1);
2275 void LyXTabular::SetLTFoot(int cell, bool last)
2277 int const row = row_of_cell(cell);
2278 int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
2281 if (endlastfoot == val)
2294 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
2297 if ((endfoot + 1) > rows_)
2299 return (row_of_cell(cell) == abs(endfoot) - 1);
2303 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
2306 if (abs(endlastfoot) > rows_)
2308 return (row_of_cell(cell) == (abs(endlastfoot)-1));
2312 void LyXTabular::SetLTNewPage(int cell, bool what)
2314 row_info[row_of_cell(cell)].newpage = what;
2318 bool LyXTabular::GetLTNewPage(int cell) const
2320 return row_info[row_of_cell(cell)].newpage;
2324 bool LyXTabular::SetAscentOfRow(int row, int height)
2326 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
2328 row_info[row].ascent_of_row = height;
2333 bool LyXTabular::SetDescentOfRow(int row, int height)
2335 if ((row >= rows_) || (row_info[row].descent_of_row == height))
2337 row_info[row].descent_of_row = height;
2342 int LyXTabular::GetAscentOfRow(int row) const
2346 return row_info[row].ascent_of_row;
2350 int LyXTabular::GetDescentOfRow(int row) const
2354 return row_info[row].descent_of_row;
2358 int LyXTabular::GetHeightOfTabular() const
2362 for (int row = 0; row < rows_; ++row)
2363 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
2364 GetAdditionalHeight(GetCellNumber(row, 0));
2369 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
2371 if ((row >= rows_) || (column >= columns_))
2373 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
2377 int LyXTabular::TeXTopHLine(ostream & os, int row) const
2379 if ((row < 0) || (row >= rows_))
2382 int const fcell = GetFirstCellInRow(row);
2383 int const n = NumberOfCellsInRow(fcell) + fcell;
2386 for (int i = fcell; i < n; ++i) {
2390 if (tmp == (n - fcell)){
2393 for (int i = fcell; i < n; ++i) {
2396 << column_of_cell(i) + 1
2398 << right_column_of_cell(i) + 1
2410 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
2412 if ((row < 0) || (row >= rows_))
2415 int const fcell = GetFirstCellInRow(row);
2416 int const n = NumberOfCellsInRow(fcell) + fcell;
2419 for (int i = fcell; i < n; ++i) {
2423 if (tmp == (n-fcell)){
2426 for (int i = fcell; i < n; ++i) {
2427 if (BottomLine(i)) {
2429 << column_of_cell(i) + 1
2431 << right_column_of_cell(i) + 1
2443 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
2447 if (GetRotateCell(cell)) {
2448 os << "\\begin{sideways}\n";
2451 if (IsMultiColumn(cell)) {
2452 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2453 if (!cellinfo_of_cell(cell)->align_special.empty()) {
2454 os << cellinfo_of_cell(cell)->align_special << "}{";
2458 if (!GetPWidth(cell).empty()) {
2459 switch (GetVAlignment(cell)) {
2460 case LYX_VALIGN_TOP:
2463 case LYX_VALIGN_CENTER:
2466 case LYX_VALIGN_BOTTOM:
2470 os << "{" << GetPWidth(cell) << '}';
2472 switch (GetAlignment(cell)) {
2473 case LYX_ALIGN_LEFT:
2476 case LYX_ALIGN_RIGHT:
2484 if (RightLine(cell))
2486 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2492 if (GetUsebox(cell) == BOX_PARBOX) {
2494 switch (GetVAlignment(cell)) {
2495 case LYX_VALIGN_TOP:
2498 case LYX_VALIGN_CENTER:
2501 case LYX_VALIGN_BOTTOM:
2505 os << "]{" << GetPWidth(cell) << "}{";
2506 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2507 os << "\\begin{minipage}[";
2508 switch (GetVAlignment(cell)) {
2509 case LYX_VALIGN_TOP:
2512 case LYX_VALIGN_CENTER:
2515 case LYX_VALIGN_BOTTOM:
2519 os << "]{" << GetPWidth(cell) << "}\n";
2526 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2531 if (GetUsebox(cell) == BOX_PARBOX)
2533 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2534 os << "%\n\\end{minipage}";
2537 if (IsMultiColumn(cell)){
2540 if (GetRotateCell(cell)) {
2541 os << "%\n\\end{sideways}";
2548 int LyXTabular::Latex(Buffer const * buf,
2549 ostream & os, bool fragile, bool fp) const
2554 //+---------------------------------------------------------------------
2555 //+ first the opening preamble +
2556 //+---------------------------------------------------------------------
2559 os << "\\begin{sideways}\n";
2562 if (is_long_tabular)
2563 os << "\\begin{longtable}{";
2565 os << "\\begin{tabular}{";
2566 for (int i = 0; i < columns_; ++i) {
2567 if (column_info[i].left_line)
2569 if (!column_info[i].align_special.empty()) {
2570 os << column_info[i].align_special;
2571 } else if (!column_info[i].p_width.empty()) {
2572 switch (column_info[i].valignment) {
2573 case LYX_VALIGN_TOP:
2576 case LYX_VALIGN_CENTER:
2579 case LYX_VALIGN_BOTTOM:
2584 << column_info[i].p_width
2587 switch (column_info[i].alignment) {
2588 case LYX_ALIGN_LEFT:
2591 case LYX_ALIGN_RIGHT:
2599 if (column_info[i].right_line)
2605 //+---------------------------------------------------------------------
2606 //+ the single row and columns (cells) +
2607 //+---------------------------------------------------------------------
2610 for (int i = 0; i < rows_; ++i) {
2611 ret += TeXTopHLine(os, i);
2613 if (IsLongTabular()) {
2614 if ((endhead < 0) && (i == (abs(endhead)-1))) {
2615 os << "\\endhead\n";
2618 if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2619 os << "\\endfirsthead\n";
2622 if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2623 os << "\\endfoot\n";
2626 if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2627 os << "\\endlastfoot\n";
2632 ret += TeXBottomHLine(os, i-1);
2633 ret += TeXTopHLine(os, i);
2635 for (int j = 0; j < columns_; ++j) {
2636 if (IsPartOfMultiColumn(i,j))
2638 ret += TeXCellPreamble(os, cell);
2639 InsetText * inset = GetCellInset(cell);
2641 bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2642 inset->par->Last() > 0 && GetPWidth(cell).empty();
2645 ret += inset->Latex(buf, os, fragile, fp);
2649 ret += TeXCellPostamble(os, cell);
2650 if (!IsLastCellInRow(cell)) { // not last cell in row
2657 ret += TeXBottomHLine(os, i);
2659 if (IsLongTabular()) {
2660 if ((endhead > 0) && (i == (endhead - 1))) {
2661 os << "\\endhead\n";
2664 if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2665 os << "\\endfirsthead\n";
2668 if ((endfoot > 0) && (i == (endfoot - 1))) {
2669 os << "\\endfoot\n";
2672 if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2673 os << "\\endlastfoot\n";
2677 // ret += TeXBottomHLine(os, i);
2678 if (row_info[i].newpage) {
2679 os << "\\newpage\n";
2685 //+---------------------------------------------------------------------
2686 //+ the closing of the tabular +
2687 //+---------------------------------------------------------------------
2689 if (is_long_tabular)
2690 os << "\\end{longtable}";
2692 os << "\\end{tabular}";
2694 os << "\n\\end{sideways}";
2702 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2706 //+---------------------------------------------------------------------
2707 //+ first the opening preamble +
2708 //+---------------------------------------------------------------------
2710 os << "<tgroup cols=\"" << columns_
2711 << "\" colsep=\"1\" rowsep=\"1\">\n";
2713 for (int i = 0; i < columns_; ++i) {
2714 os << "<colspec colname=\"col" << i << "\" align=\"";
2715 switch (column_info[i].alignment) {
2716 case LYX_ALIGN_LEFT:
2719 case LYX_ALIGN_RIGHT:
2730 //+---------------------------------------------------------------------
2731 //+ the single row and columns (cells) +
2732 //+---------------------------------------------------------------------
2736 for (int i = 0; i < rows_; ++i) {
2738 for (int j = 0; j < columns_; ++j) {
2739 if (IsPartOfMultiColumn(i, j))
2742 os << "<entry align=\"";
2743 switch (GetAlignment(cell)) {
2744 case LYX_ALIGN_LEFT:
2747 case LYX_ALIGN_RIGHT:
2755 os << "\" valign=\"";
2756 switch (GetVAlignment(cell)) {
2757 case LYX_VALIGN_TOP:
2760 case LYX_VALIGN_BOTTOM:
2763 case LYX_VALIGN_CENTER:
2768 if (IsMultiColumn(cell)) {
2769 os << " namest=\"col" << j << "\" ";
2770 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2774 ret += GetCellInset(cell)->DocBook(buf, os);
2781 //+---------------------------------------------------------------------
2782 //+ the closing of the tabular +
2783 //+---------------------------------------------------------------------
2794 void print_n_chars(ostream & os, unsigned char ch, int n)
2796 os << string(n, ch);
2800 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2801 vector<unsigned int> const & clen) const
2803 int const fcell = GetFirstCellInRow(row);
2804 int const n = NumberOfCellsInRow(fcell) + fcell;
2807 for (int i = fcell; i < n; ++i) {
2817 for (int i = fcell; i < n; ++i) {
2828 int column = column_of_cell(i);
2829 int len = clen[column];
2830 while(IsPartOfMultiColumn(row, ++column))
2831 len += clen[column] + 4;
2832 print_n_chars(os, ch, len);
2847 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2848 vector<unsigned int> const & clen) const
2850 int const fcell = GetFirstCellInRow(row);
2851 int const n = NumberOfCellsInRow(fcell) + fcell;
2854 for (int i = fcell; i < n; ++i) {
2855 if (BottomLine(i)) {
2864 for (int i = fcell; i < n; ++i) {
2865 if (BottomLine(i)) {
2875 int column = column_of_cell(i);
2876 int len = clen[column];
2877 while(IsPartOfMultiColumn(row, ++column))
2878 len += clen[column] + 4;
2879 print_n_chars(os, ch, len);
2880 if (BottomLine(i)) {
2894 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2895 int cell, int row, int column,
2896 vector<unsigned int> const & clen) const
2899 int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2906 unsigned int len1 = sstr.str().length();
2907 unsigned int len2 = clen[column];
2908 while(IsPartOfMultiColumn(row, ++column))
2909 len2 += clen[column] + 4;
2912 switch (GetAlignment(cell)) {
2914 case LYX_ALIGN_LEFT:
2917 case LYX_ALIGN_RIGHT:
2921 case LYX_ALIGN_CENTER:
2927 for (unsigned int i = 0; i < len1; ++i)
2930 for (unsigned int i = 0; i < len2; ++i)
2932 if (RightLine(cell))
2937 return ret * 0; // eh? (Lgb)
2941 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2945 //+---------------------------------------------------------------------
2946 //+ first calculate the width of the single columns +
2947 //+---------------------------------------------------------------------
2948 vector<unsigned int> clen(columns_);
2950 // first all non (real) multicolumn cells!
2951 for (int j = 0; j < columns_; ++j) {
2953 for (int i = 0; i < rows_; ++i) {
2954 int cell = GetCellNumber(i, j);
2955 if (IsMultiColumn(cell, true))
2958 GetCellInset(cell)->Ascii(buf, sstr, 0);
2959 if (clen[j] < sstr.str().length())
2960 clen[j] = sstr.str().length();
2963 // then all (real) multicolumn cells!
2964 for (int j = 0; j < columns_; ++j) {
2965 for (int i = 0; i < rows_; ++i) {
2966 int cell = GetCellNumber(i, j);
2967 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2970 GetCellInset(cell)->Ascii(buf, sstr, 0);
2971 int len = int(sstr.str().length());
2972 int const n = cells_in_multicolumn(cell);
2973 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2975 if (len > int(clen[j + n - 1]))
2976 clen[j + n - 1] = len;
2980 for (int i = 0; i < rows_; ++i) {
2981 AsciiTopHLine(os, i, clen);
2982 for (int j = 0; j < columns_; ++j) {
2983 if (IsPartOfMultiColumn(i,j))
2985 ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2989 AsciiBottomHLine(os, i, clen);
2995 InsetText * LyXTabular::GetCellInset(int cell) const
2997 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
3001 InsetText * LyXTabular::GetCellInset(int row, int column) const
3003 return GetCellInset(GetCellNumber(row, column));
3007 void LyXTabular::Validate(LaTeXFeatures & features) const
3009 if (IsLongTabular())
3010 features.longtable = true;
3012 features.rotating = true;
3013 for (int cell = 0; cell < numberofcells; ++cell) {
3014 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
3015 features.array = true;
3016 GetCellInset(cell)->Validate(features);
3021 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
3023 LyXParagraph * par = GetCellInset(cell)->par;
3025 for (; par; par = par->next) {
3026 for (int i = 0; i < par->Last(); ++i) {
3027 if (par->GetChar(i) == LyXParagraph::META_NEWLINE)