2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "CoordCache.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
41 #include "MetricsInfo.h"
42 #include "OutputParams.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
49 #include "support/convert.h"
50 #include "support/lstrings.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
66 using std::istringstream;
69 using std::ostringstream;
74 #ifndef CXX_GLOBAL_CSTD
78 using boost::shared_ptr;
79 using boost::dynamic_pointer_cast;
83 using support::prefixIs;
86 using support::suffixIs;
88 using cap::dirtyTabularStack;
89 using cap::tabularStackDirty;
91 using graphics::PreviewLoader;
93 using frontend::Painter;
94 using frontend::Clipboard;
96 namespace Alert = frontend::Alert;
101 int const ADD_TO_HEIGHT = 2;
102 int const ADD_TO_TABULAR_WIDTH = 2;
103 int const default_line_space = 10;
104 int const WIDTH_OF_LINE = 5;
108 boost::scoped_ptr<Tabular> paste_tabular;
111 struct TabularFeature {
112 Tabular::Feature action;
117 TabularFeature tabularFeature[] =
119 { Tabular::APPEND_ROW, "append-row" },
120 { Tabular::APPEND_COLUMN, "append-column" },
121 { Tabular::DELETE_ROW, "delete-row" },
122 { Tabular::DELETE_COLUMN, "delete-column" },
123 { Tabular::COPY_ROW, "copy-row" },
124 { Tabular::COPY_COLUMN, "copy-column" },
125 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
126 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
127 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
128 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
129 { Tabular::ALIGN_LEFT, "align-left" },
130 { Tabular::ALIGN_RIGHT, "align-right" },
131 { Tabular::ALIGN_CENTER, "align-center" },
132 { Tabular::ALIGN_BLOCK, "align-block" },
133 { Tabular::VALIGN_TOP, "valign-top" },
134 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
135 { Tabular::VALIGN_MIDDLE, "valign-middle" },
136 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
137 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
138 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
139 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
140 { Tabular::M_ALIGN_LEFT, "m-align-left" },
141 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
142 { Tabular::M_ALIGN_CENTER, "m-align-center" },
143 { Tabular::M_VALIGN_TOP, "m-valign-top" },
144 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
145 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
146 { Tabular::MULTICOLUMN, "multicolumn" },
147 { Tabular::SET_ALL_LINES, "set-all-lines" },
148 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
149 { Tabular::SET_LONGTABULAR, "set-longtabular" },
150 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
151 { Tabular::SET_PWIDTH, "set-pwidth" },
152 { Tabular::SET_MPWIDTH, "set-mpwidth" },
153 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
154 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
155 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
156 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
157 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
158 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
159 { Tabular::SET_USEBOX, "set-usebox" },
160 { Tabular::SET_LTHEAD, "set-lthead" },
161 { Tabular::UNSET_LTHEAD, "unset-lthead" },
162 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
163 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
164 { Tabular::SET_LTFOOT, "set-ltfoot" },
165 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
166 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
167 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
168 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
169 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
170 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
171 { Tabular::SET_BOOKTABS, "set-booktabs" },
172 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
173 { Tabular::SET_TOP_SPACE, "set-top-space" },
174 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
175 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
176 { Tabular::LAST_ACTION, "" }
180 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
182 FeatureEqual(Tabular::Feature feature)
183 : feature_(feature) {}
184 bool operator()(TabularFeature const & tf) const {
185 return tf.action == feature_;
188 Tabular::Feature feature_;
193 string const write_attribute(string const & name, T const & t)
195 string const s = tostr(t);
196 return s.empty() ? s : " " + name + "=\"" + s + "\"";
200 string const write_attribute(string const & name, string const & t)
202 return t.empty() ? t : " " + name + "=\"" + t + "\"";
207 string const write_attribute(string const & name, docstring const & t)
209 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
214 string const write_attribute(string const & name, bool const & b)
216 // we write only true attribute values so we remove a bit of the
217 // file format bloat for tabulars.
218 return b ? write_attribute(name, convert<string>(b)) : string();
223 string const write_attribute(string const & name, int const & i)
225 // we write only true attribute values so we remove a bit of the
226 // file format bloat for tabulars.
227 return i ? write_attribute(name, convert<string>(i)) : string();
232 string const write_attribute(string const & name, Tabular::idx_type const & i)
234 // we write only true attribute values so we remove a bit of the
235 // file format bloat for tabulars.
236 return i ? write_attribute(name, convert<string>(i)) : string();
241 string const write_attribute(string const & name, Length const & value)
243 // we write only the value if we really have one same reson as above.
244 return value.zero() ? string() : write_attribute(name, value.asString());
248 string const tostr(LyXAlignment const & num)
253 case LYX_ALIGN_BLOCK:
257 case LYX_ALIGN_CENTER:
259 case LYX_ALIGN_RIGHT:
261 case LYX_ALIGN_LAYOUT:
263 case LYX_ALIGN_SPECIAL:
270 string const tostr(Tabular::VAlignment const & num)
273 case Tabular::LYX_VALIGN_TOP:
275 case Tabular::LYX_VALIGN_MIDDLE:
277 case Tabular::LYX_VALIGN_BOTTOM:
284 string const tostr(Tabular::BoxType const & num)
287 case Tabular::BOX_NONE:
289 case Tabular::BOX_PARBOX:
291 case Tabular::BOX_MINIPAGE:
298 // I would have liked a fromstr template a lot better. (Lgb)
299 bool string2type(string const str, LyXAlignment & num)
302 num = LYX_ALIGN_NONE;
303 else if (str == "block")
304 num = LYX_ALIGN_BLOCK;
305 else if (str == "left")
306 num = LYX_ALIGN_LEFT;
307 else if (str == "center")
308 num = LYX_ALIGN_CENTER;
309 else if (str == "right")
310 num = LYX_ALIGN_RIGHT;
317 bool string2type(string const str, Tabular::VAlignment & num)
320 num = Tabular::LYX_VALIGN_TOP;
321 else if (str == "middle" )
322 num = Tabular::LYX_VALIGN_MIDDLE;
323 else if (str == "bottom")
324 num = Tabular::LYX_VALIGN_BOTTOM;
331 bool string2type(string const str, Tabular::BoxType & num)
334 num = Tabular::BOX_NONE;
335 else if (str == "parbox")
336 num = Tabular::BOX_PARBOX;
337 else if (str == "minipage")
338 num = Tabular::BOX_MINIPAGE;
345 bool string2type(string const str, bool & num)
349 else if (str == "false")
357 bool getTokenValue(string const & str, char const * token, string & ret)
360 size_t token_length = strlen(token);
361 string::size_type pos = str.find(token);
363 if (pos == string::npos || pos + token_length + 1 >= str.length()
364 || str[pos + token_length] != '=')
366 pos += token_length + 1;
368 if (ch != '"' && ch != '\'') { // only read till next space
372 while (pos < str.length() - 1 && str[++pos] != ch)
379 bool getTokenValue(string const & str, char const * token, docstring & ret)
382 bool const success = getTokenValue(str, token, tmp);
383 ret = from_utf8(tmp);
388 bool getTokenValue(string const & str, char const * token, int & num)
392 if (!getTokenValue(str, token, tmp))
394 num = convert<int>(tmp);
399 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
402 return getTokenValue(str, token, tmp) && string2type(tmp, num);
406 bool getTokenValue(string const & str, char const * token,
407 Tabular::VAlignment & num)
410 return getTokenValue(str, token, tmp) && string2type(tmp, num);
414 bool getTokenValue(string const & str, char const * token,
415 Tabular::BoxType & num)
418 return getTokenValue(str, token, tmp) && string2type(tmp, num);
422 bool getTokenValue(string const & str, char const * token, bool & flag)
424 // set the flag always to false as this should be the default for bools
425 // not in the file-format.
428 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
432 bool getTokenValue(string const & str, char const * token, Length & len)
434 // set the lenght to be zero() as default as this it should be if not
435 // in the file format.
438 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
442 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
447 if (!getTokenValue(str, token, tmp))
449 if (tmp == "default") {
453 return isValidLength(tmp, &len);
457 void l_getline(istream & is, string & str)
460 while (str.empty()) {
462 if (!str.empty() && str[str.length() - 1] == '\r')
463 str.erase(str.length() - 1);
470 string const featureAsString(Tabular::Feature feature)
472 TabularFeature * end = tabularFeature +
473 sizeof(tabularFeature) / sizeof(TabularFeature);
474 TabularFeature * it = std::find_if(tabularFeature, end,
475 FeatureEqual(feature));
476 return (it == end) ? string() : it->feature;
481 /////////////////////////////////////////////////////////////////////
485 /////////////////////////////////////////////////////////////////////
488 Tabular::cellstruct::cellstruct(BufferParams const & bp)
491 multicolumn(Tabular::CELL_NORMAL),
492 alignment(LYX_ALIGN_CENTER),
493 valignment(LYX_VALIGN_TOP),
500 inset(new InsetText(bp))
504 Tabular::cellstruct::cellstruct(cellstruct const & cs)
506 width_of_cell(cs.width_of_cell),
507 multicolumn(cs.multicolumn),
508 alignment(cs.alignment),
509 valignment(cs.valignment),
510 top_line(cs.top_line),
511 bottom_line(cs.bottom_line),
512 left_line(cs.left_line),
513 right_line(cs.right_line),
516 align_special(cs.align_special),
518 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
522 Tabular::cellstruct &
523 Tabular::cellstruct::operator=(cellstruct cs)
531 Tabular::cellstruct::swap(cellstruct & rhs)
533 std::swap(cellno, rhs.cellno);
534 std::swap(width_of_cell, rhs.width_of_cell);
535 std::swap(multicolumn, rhs.multicolumn);
536 std::swap(alignment, rhs.alignment);
537 std::swap(valignment, rhs.valignment);
538 std::swap(top_line, rhs.top_line);
539 std::swap(bottom_line, rhs.bottom_line);
540 std::swap(left_line, rhs.left_line);
541 std::swap(right_line, rhs.right_line);
542 std::swap(usebox, rhs.usebox);
543 std::swap(rotate, rhs.rotate);
544 std::swap(align_special, rhs.align_special);
545 p_width.swap(rhs.p_width);
546 inset.swap(rhs.inset);
550 Tabular::rowstruct::rowstruct()
555 top_space_default(false),
556 bottom_space_default(false),
557 interline_space_default(false),
566 Tabular::columnstruct::columnstruct()
567 : alignment(LYX_ALIGN_CENTER),
568 valignment(LYX_VALIGN_TOP),
576 Tabular::ltType::ltType()
583 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
584 col_type columns_arg)
586 init(bp, rows_arg, columns_arg);
590 // activates all lines and sets all widths to 0
591 void Tabular::init(BufferParams const & bp, row_type rows_arg,
592 col_type columns_arg)
595 columns_ = columns_arg;
596 row_info = row_vector(rows_);
597 column_info = column_vector(columns_);
598 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
599 row_info.reserve(10);
600 column_info.reserve(10);
601 cell_info.reserve(100);
603 for (row_type i = 0; i < rows_; ++i)
604 cell_info[i].back().right_line = true;
605 row_info.back().bottom_line = true;
606 row_info.front().bottom_line = true;
607 column_info.back().right_line = true;
608 is_long_tabular = false;
610 use_booktabs = false;
614 void Tabular::fixCellNums()
617 for (row_type i = 0; i < rows_; ++i) {
618 for (col_type j = 0; j < columns_; ++j) {
619 // When debugging it can be nice to set
621 cell_info[i][j].inset->setDrawFrame(false);
622 cell_info[i][j].cellno = cellno++;
624 cell_info[i].back().right_line = true;
627 set_row_column_number_info();
631 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
635 row_type const row = row_of_cell(cell);
637 row_vector::iterator rit = row_info.begin() + row;
638 row_info.insert(rit, rowstruct());
639 // now set the values of the row before
640 row_info[row] = row_info[row + 1];
642 cell_vvector old(rows_ - 1);
643 for (row_type i = 0; i < rows_ - 1; ++i)
644 swap(cell_info[i], old[i]);
646 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
648 for (row_type i = 0; i <= row; ++i)
649 swap(cell_info[i], old[i]);
650 for (row_type i = row + 2; i < rows_; ++i)
651 swap(cell_info[i], old[i - 1]);
654 for (col_type j = 0; j < columns_; ++j)
655 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
657 set_row_column_number_info();
661 void Tabular::deleteRow(row_type const row)
663 // Not allowed to delete last row
667 row_info.erase(row_info.begin() + row);
668 cell_info.erase(cell_info.begin() + row);
674 void Tabular::copyRow(BufferParams const & bp, row_type const row)
678 row_info.insert(row_info.begin() + row, row_info[row]);
679 cell_info.insert(cell_info.begin() + row, cell_info[row]);
682 for (col_type j = 0; j < columns_; ++j)
683 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
685 set_row_column_number_info();
689 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
693 col_type const column = column_of_cell(cell);
694 column_vector::iterator cit = column_info.begin() + column + 1;
695 column_info.insert(cit, columnstruct());
696 // set the column values of the column before
697 column_info[column + 1] = column_info[column];
699 for (row_type i = 0; i < rows_; ++i) {
700 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
702 // care about multicolumns
703 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
704 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
706 if (column + 2 >= columns_
707 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
708 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
711 for (row_type i = 0; i < rows_; ++i) {
712 cell_info[i][column + 1].inset->clear();
714 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
720 void Tabular::deleteColumn(col_type const column)
722 // Not allowed to delete last column
726 column_info.erase(column_info.begin() + column);
727 for (row_type i = 0; i < rows_; ++i) {
728 // Care about multicolumn cells
729 if (column + 1 < columns_ &&
730 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
731 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
732 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
734 cell_info[i].erase(cell_info[i].begin() + column);
741 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
745 column_info.insert(column_info.begin() + column, column_info[column]);
747 for (row_type i = 0; i < rows_; ++i)
748 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
751 for (row_type i = 0; i < rows_; ++i)
752 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
757 void Tabular::set_row_column_number_info()
760 // Count only non-multicol cells plus begin multicol
761 // cells, ignore non-begin multicol cells:
762 for (row_type row = 0; row < rows_; ++row) {
763 for (col_type column = 0; column < columns_; ++column) {
764 if (cell_info[row][column].multicolumn
765 != Tabular::CELL_PART_OF_MULTICOLUMN)
767 BOOST_ASSERT(numberofcells != 0);
768 cell_info[row][column].cellno =
773 rowofcell.resize(numberofcells);
774 columnofcell.resize(numberofcells);
779 c < numberofcells && row < rows_ && column < columns_;) {
781 columnofcell[c] = column;
785 } while (column < columns_ &&
786 cell_info[row][column].multicolumn
787 == Tabular::CELL_PART_OF_MULTICOLUMN);
789 if (column == columns_) {
795 for (row_type row = 0; row < rows_; ++row) {
796 for (col_type column = 0; column < columns_; ++column) {
797 if (isPartOfMultiColumn(row,column))
799 cell_info[row][column].inset->setAutoBreakRows(
800 !getPWidth(getCellNumber(row, column)).zero());
806 Tabular::idx_type Tabular::getNumberOfCells() const
808 return numberofcells;
812 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
814 row_type const row = row_of_cell(cell);
816 for (col_type i = 0; i < columns_; ++i)
817 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
823 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
825 if (!wholerow && isMultiColumn(cell) &&
826 !(use_booktabs && row_of_cell(cell) == 0))
827 return cellinfo_of_cell(cell).top_line;
828 return row_info[row_of_cell(cell)].top_line;
832 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
834 if (!wholerow && isMultiColumn(cell) &&
835 !(use_booktabs && isLastRow(cell)))
836 return cellinfo_of_cell(cell).bottom_line;
837 return row_info[row_of_cell(cell)].bottom_line;
841 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
845 if (!wholecolumn && isMultiColumn(cell) &&
846 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
847 return cellinfo_of_cell(cell).left_line;
848 return column_info[column_of_cell(cell)].left_line;
852 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
856 if (!wholecolumn && isMultiColumn(cell) &&
857 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
858 return cellinfo_of_cell(cell).right_line;
859 return column_info[right_column_of_cell(cell)].right_line;
863 bool Tabular::topAlreadyDrawn(idx_type cell) const
865 row_type row = row_of_cell(cell);
866 if (row > 0 && !getAdditionalHeight(row)) {
867 col_type column = column_of_cell(cell);
870 && cell_info[row][column].multicolumn
871 == Tabular::CELL_PART_OF_MULTICOLUMN)
873 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
874 return row_info[row].bottom_line;
876 return cell_info[row][column].bottom_line;
882 bool Tabular::leftAlreadyDrawn(idx_type cell) const
884 col_type column = column_of_cell(cell);
886 row_type row = row_of_cell(cell);
888 (cell_info[row][column].multicolumn ==
889 Tabular::CELL_PART_OF_MULTICOLUMN));
890 if (getAdditionalWidth(cell_info[row][column].cellno))
892 return rightLine(cell_info[row][column].cellno);
898 bool Tabular::isLastRow(idx_type cell) const
900 return row_of_cell(cell) == rows_ - 1;
904 int Tabular::getAdditionalHeight(row_type row) const
906 if (!row || row >= rows_)
912 for (col_type column = 0; column < columns_ && bottom; ++column) {
913 switch (cell_info[row - 1][column].multicolumn) {
914 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
915 bottom = cell_info[row - 1][column].bottom_line;
917 case Tabular::CELL_NORMAL:
918 bottom = row_info[row - 1].bottom_line;
921 for (col_type column = 0; column < columns_ && top; ++column) {
922 switch (cell_info[row][column].multicolumn) {
923 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
924 top = cell_info[row][column].top_line;
926 case Tabular::CELL_NORMAL:
927 top = row_info[row].top_line;
930 int const interline_space = row_info[row - 1].interline_space_default ?
932 row_info[row - 1].interline_space.inPixels(width_of_tabular);
934 return interline_space + WIDTH_OF_LINE;
935 return interline_space;
939 int Tabular::getAdditionalWidth(idx_type cell) const
941 // internally already set in setWidthOfCell
942 // used to get it back in text.cpp
943 col_type const col = right_column_of_cell(cell);
944 row_type const row = row_of_cell(cell);
945 if (col < columns_ - 1 && rightLine(cell) &&
946 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
948 return WIDTH_OF_LINE;
954 // returns the maximum over all rows
955 int Tabular::getWidthOfColumn(idx_type cell) const
957 col_type const column1 = column_of_cell(cell);
958 col_type const column2 = right_column_of_cell(cell);
960 for (col_type i = column1; i <= column2; ++i)
961 result += column_info[i].width_of_column;
966 int Tabular::getWidthOfTabular() const
968 return width_of_tabular;
972 // returns true if a complete update is necessary, otherwise false
973 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
975 if (!isMultiColumn(cell))
978 row_type const row = row_of_cell(cell);
979 col_type const column1 = column_of_cell(cell);
980 col_type const column2 = right_column_of_cell(cell);
981 int const old_val = cell_info[row][column2].width_of_cell;
983 // first set columns to 0 so we can calculate the right width
984 for (col_type i = column1; i <= column2; ++i) {
985 cell_info[row][i].width_of_cell = 0;
987 // set the width to MAX_WIDTH until width > 0
988 int width = new_width + 2 * WIDTH_OF_LINE;
989 col_type i = column1;
990 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
991 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
992 width -= column_info[i].width_of_column;
995 cell_info[row][i].width_of_cell = width;
997 if (old_val != cell_info[row][column2].width_of_cell) {
998 // in this case we have to recalculate all multicolumn cells which
999 // have this column as one of theirs but not as last one
1000 calculate_width_of_column_NMC(i);
1001 recalculateMulticolumnsOfColumn(i);
1002 calculate_width_of_column(i);
1008 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1010 // the last column does not have to be recalculated because all
1011 // multicolumns will have here there last multicolumn cell which
1012 // always will have the whole rest of the width of the cell.
1013 if (columns_ < 2 || column > (columns_ - 2))
1015 for (row_type row = 0; row < rows_; ++row) {
1016 int mc = cell_info[row][column].multicolumn;
1017 int nmc = cell_info[row][column+1].multicolumn;
1018 // we only have to update multicolumns which do not have this
1019 // column as their last column!
1020 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1021 (mc == CELL_PART_OF_MULTICOLUMN &&
1022 nmc == CELL_PART_OF_MULTICOLUMN))
1024 idx_type const cellno = cell_info[row][column].cellno;
1025 setWidthOfMulticolCell(cellno,
1026 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1032 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1034 row_type const row = row_of_cell(cell);
1035 col_type const column1 = column_of_cell(cell);
1040 if (rightLine(cell_info[row][column1].cellno, true) &&
1041 column1 < columns_ - 1 &&
1042 leftLine(cell_info[row][column1+1].cellno, true))
1044 add_width = WIDTH_OF_LINE;
1047 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1050 if (isMultiColumnReal(cell)) {
1051 tmp = setWidthOfMulticolCell(cell, new_width);
1053 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1054 cell_info[row][column1].width_of_cell = width;
1055 tmp = calculate_width_of_column_NMC(column1);
1057 recalculateMulticolumnsOfColumn(column1);
1060 for (col_type i = 0; i < columns_; ++i)
1061 calculate_width_of_column(i);
1062 calculate_width_of_tabular();
1067 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1070 if (!isMultiColumn(cell) || onlycolumn)
1071 column_info[column_of_cell(cell)].alignment = align;
1073 cellinfo_of_cell(cell).alignment = align;
1077 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1080 if (!isMultiColumn(cell) || onlycolumn)
1081 column_info[column_of_cell(cell)].valignment = align;
1083 cellinfo_of_cell(cell).valignment = align;
1090 * Allow line and paragraph breaks for fixed width cells or disallow them,
1091 * merge cell paragraphs and reset layout to standard for variable width
1094 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1096 inset->setAutoBreakRows(fixedWidth);
1100 // merge all paragraphs to one
1101 BufferParams const & bp = cur.bv().buffer()->params();
1102 while (inset->paragraphs().size() > 1)
1103 mergeParagraph(bp, inset->paragraphs(), 0);
1107 // undo information has already been recorded
1108 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1109 bp.getTextClass().defaultLayoutName());
1116 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1117 Length const & width)
1119 col_type const j = column_of_cell(cell);
1121 column_info[j].p_width = width;
1122 for (row_type i = 0; i < rows_; ++i) {
1123 idx_type const cell = getCellNumber(i, j);
1124 // because of multicolumns
1125 toggleFixedWidth(cur, getCellInset(cell).get(),
1126 !getPWidth(cell).zero());
1128 // cur paragraph can become invalid after paragraphs were merged
1129 if (cur.pit() > cur.lastpit())
1130 cur.pit() = cur.lastpit();
1131 // cur position can become invalid after newlines were removed
1132 if (cur.pos() > cur.lastpos())
1133 cur.pos() = cur.lastpos();
1137 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1138 Length const & width)
1140 if (!isMultiColumn(cell))
1143 cellinfo_of_cell(cell).p_width = width;
1144 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1145 // cur paragraph can become invalid after paragraphs were merged
1146 if (cur.pit() > cur.lastpit())
1147 cur.pit() = cur.lastpit();
1148 // cur position can become invalid after newlines were removed
1149 if (cur.pos() > cur.lastpos())
1150 cur.pos() = cur.lastpos();
1155 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1156 Tabular::Feature what)
1158 if (what == SET_SPECIAL_MULTI)
1159 cellinfo_of_cell(cell).align_special = special;
1161 column_info[column_of_cell(cell)].align_special = special;
1165 void Tabular::setAllLines(idx_type cell, bool line)
1167 setTopLine(cell, line);
1168 setBottomLine(cell, line);
1169 setRightLine(cell, line);
1170 setLeftLine(cell, line);
1174 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1176 row_type const row = row_of_cell(cell);
1177 if (wholerow || !isMultiColumn(cell))
1178 row_info[row].top_line = line;
1180 cellinfo_of_cell(cell).top_line = line;
1184 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1186 if (wholerow || !isMultiColumn(cell))
1187 row_info[row_of_cell(cell)].bottom_line = line;
1189 cellinfo_of_cell(cell).bottom_line = line;
1193 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1195 if (wholecolumn || !isMultiColumn(cell))
1196 column_info[column_of_cell(cell)].left_line = line;
1198 cellinfo_of_cell(cell).left_line = line;
1202 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1204 if (wholecolumn || !isMultiColumn(cell))
1205 column_info[right_column_of_cell(cell)].right_line = line;
1207 cellinfo_of_cell(cell).right_line = line;
1211 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1213 if (!onlycolumn && isMultiColumn(cell))
1214 return cellinfo_of_cell(cell).alignment;
1215 return column_info[column_of_cell(cell)].alignment;
1220 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1222 if (!onlycolumn && isMultiColumn(cell))
1223 return cellinfo_of_cell(cell).valignment;
1224 return column_info[column_of_cell(cell)].valignment;
1228 Length const Tabular::getPWidth(idx_type cell) const
1230 if (isMultiColumn(cell))
1231 return cellinfo_of_cell(cell).p_width;
1232 return column_info[column_of_cell(cell)].p_width;
1236 Length const Tabular::getColumnPWidth(idx_type cell) const
1238 return column_info[column_of_cell(cell)].p_width;
1242 Length const Tabular::getMColumnPWidth(idx_type cell) const
1244 if (isMultiColumn(cell))
1245 return cellinfo_of_cell(cell).p_width;
1250 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1252 if (what == SET_SPECIAL_MULTI)
1253 return cellinfo_of_cell(cell).align_special;
1254 return column_info[column_of_cell(cell)].align_special;
1258 int Tabular::getWidthOfCell(idx_type cell) const
1260 row_type const row = row_of_cell(cell);
1261 col_type const column1 = column_of_cell(cell);
1262 col_type const column2 = right_column_of_cell(cell);
1264 for (col_type i = column1; i <= column2; ++i)
1265 result += cell_info[row][i].width_of_cell;
1270 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1274 switch (getAlignment(cell)) {
1275 case LYX_ALIGN_CENTER:
1276 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1278 case LYX_ALIGN_RIGHT:
1279 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1280 // + getAdditionalWidth(cell);
1283 // LYX_ALIGN_LEFT: nothing :-)
1287 // the LaTeX Way :-(
1293 bool Tabular::isFirstCellInRow(idx_type cell) const
1295 return column_of_cell(cell) == 0;
1299 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1301 if (row > rows_ - 1)
1303 return cell_info[row][0].cellno;
1307 bool Tabular::isLastCellInRow(idx_type cell) const
1309 return right_column_of_cell(cell) == columns_ - 1;
1313 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1315 if (row > rows_ - 1)
1317 return cell_info[row][columns_-1].cellno;
1321 void Tabular::calculate_width_of_column(col_type column)
1324 for (row_type i = 0; i < rows_; ++i)
1325 maximum = max(cell_info[i][column].width_of_cell, maximum);
1326 column_info[column].width_of_column = maximum;
1331 // Calculate the columns regarding ONLY the normal cells and if this
1332 // column is inside a multicolumn cell then use it only if its the last
1333 // column of this multicolumn cell as this gives an added width to the
1334 // column, all the rest should be adapted!
1336 bool Tabular::calculate_width_of_column_NMC(col_type column)
1338 int const old_column_width = column_info[column].width_of_column;
1340 for (row_type i = 0; i < rows_; ++i) {
1341 idx_type cell = getCellNumber(i, column);
1342 bool ismulti = isMultiColumnReal(cell);
1343 if ((!ismulti || column == right_column_of_cell(cell)) &&
1344 cell_info[i][column].width_of_cell > max)
1346 max = cell_info[i][column].width_of_cell;
1349 column_info[column].width_of_column = max;
1350 return column_info[column].width_of_column != old_column_width;
1354 void Tabular::calculate_width_of_tabular()
1356 width_of_tabular = 0;
1357 for (col_type i = 0; i < columns_; ++i)
1358 width_of_tabular += column_info[i].width_of_column;
1362 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1364 if (cell >= numberofcells)
1368 return rowofcell[cell];
1372 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1374 if (cell >= numberofcells)
1375 return columns_ - 1;
1378 return columnofcell[cell];
1382 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1384 row_type const row = row_of_cell(cell);
1385 col_type column = column_of_cell(cell);
1386 while (column < columns_ - 1 &&
1387 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1393 void Tabular::write(Buffer const & buf, ostream & os) const
1397 << write_attribute("version", 3)
1398 << write_attribute("rows", rows_)
1399 << write_attribute("columns", columns_)
1401 // global longtable options
1403 << write_attribute("rotate", rotate)
1404 << write_attribute("booktabs", use_booktabs)
1405 << write_attribute("islongtable", is_long_tabular)
1406 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1407 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1408 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1409 << write_attribute("headTopDL", endhead.topDL)
1410 << write_attribute("headBottomDL", endhead.bottomDL)
1411 << write_attribute("footTopDL", endfoot.topDL)
1412 << write_attribute("footBottomDL", endfoot.bottomDL)
1413 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1414 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1415 << write_attribute("lastFootEmpty", endlastfoot.empty)
1417 for (col_type j = 0; j < columns_; ++j) {
1419 << write_attribute("alignment", column_info[j].alignment)
1420 << write_attribute("valignment", column_info[j].valignment)
1421 << write_attribute("leftline", column_info[j].left_line)
1422 << write_attribute("rightline", column_info[j].right_line)
1423 << write_attribute("width", column_info[j].p_width.asString())
1424 << write_attribute("special", column_info[j].align_special)
1427 for (row_type i = 0; i < rows_; ++i) {
1429 << write_attribute("topline", row_info[i].top_line)
1430 << write_attribute("bottomline", row_info[i].bottom_line);
1431 static const string def("default");
1432 if (row_info[i].top_space_default)
1433 os << write_attribute("topspace", def);
1435 os << write_attribute("topspace", row_info[i].top_space);
1436 if (row_info[i].bottom_space_default)
1437 os << write_attribute("bottomspace", def);
1439 os << write_attribute("bottomspace", row_info[i].bottom_space);
1440 if (row_info[i].interline_space_default)
1441 os << write_attribute("interlinespace", def);
1443 os << write_attribute("interlinespace", row_info[i].interline_space);
1444 os << write_attribute("endhead", row_info[i].endhead)
1445 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1446 << write_attribute("endfoot", row_info[i].endfoot)
1447 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1448 << write_attribute("newpage", row_info[i].newpage)
1450 for (col_type j = 0; j < columns_; ++j) {
1452 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1453 << write_attribute("alignment", cell_info[i][j].alignment)
1454 << write_attribute("valignment", cell_info[i][j].valignment)
1455 << write_attribute("topline", cell_info[i][j].top_line)
1456 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1457 << write_attribute("leftline", cell_info[i][j].left_line)
1458 << write_attribute("rightline", cell_info[i][j].right_line)
1459 << write_attribute("rotate", cell_info[i][j].rotate)
1460 << write_attribute("usebox", cell_info[i][j].usebox)
1461 << write_attribute("width", cell_info[i][j].p_width)
1462 << write_attribute("special", cell_info[i][j].align_special)
1464 os << "\\begin_inset ";
1465 cell_info[i][j].inset->write(buf, os);
1466 os << "\n\\end_inset\n"
1471 os << "</lyxtabular>\n";
1475 void Tabular::read(Buffer const & buf, Lexer & lex)
1478 istream & is = lex.getStream();
1480 l_getline(is, line);
1481 if (!prefixIs(line, "<lyxtabular ")
1482 && !prefixIs(line, "<Tabular ")) {
1483 BOOST_ASSERT(false);
1488 if (!getTokenValue(line, "version", version))
1490 BOOST_ASSERT(version >= 2);
1493 if (!getTokenValue(line, "rows", rows_arg))
1496 if (!getTokenValue(line, "columns", columns_arg))
1498 init(buf.params(), rows_arg, columns_arg);
1499 l_getline(is, line);
1500 if (!prefixIs(line, "<features")) {
1501 lyxerr << "Wrong tabular format (expected <features ...> got"
1502 << line << ')' << endl;
1505 getTokenValue(line, "rotate", rotate);
1506 getTokenValue(line, "booktabs", use_booktabs);
1507 getTokenValue(line, "islongtable", is_long_tabular);
1508 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1509 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1510 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1511 getTokenValue(line, "headTopDL", endhead.topDL);
1512 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1513 getTokenValue(line, "footTopDL", endfoot.topDL);
1514 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1515 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1516 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1517 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1519 for (col_type j = 0; j < columns_; ++j) {
1521 if (!prefixIs(line,"<column")) {
1522 lyxerr << "Wrong tabular format (expected <column ...> got"
1523 << line << ')' << endl;
1526 getTokenValue(line, "alignment", column_info[j].alignment);
1527 getTokenValue(line, "valignment", column_info[j].valignment);
1528 getTokenValue(line, "leftline", column_info[j].left_line);
1529 getTokenValue(line, "rightline", column_info[j].right_line);
1530 getTokenValue(line, "width", column_info[j].p_width);
1531 getTokenValue(line, "special", column_info[j].align_special);
1534 for (row_type i = 0; i < rows_; ++i) {
1535 l_getline(is, line);
1536 if (!prefixIs(line, "<row")) {
1537 lyxerr << "Wrong tabular format (expected <row ...> got"
1538 << line << ')' << endl;
1541 getTokenValue(line, "topline", row_info[i].top_line);
1542 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1543 getTokenValue(line, "topspace", row_info[i].top_space,
1544 row_info[i].top_space_default);
1545 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1546 row_info[i].bottom_space_default);
1547 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1548 row_info[i].interline_space_default);
1549 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1550 getTokenValue(line, "endhead", row_info[i].endhead);
1551 getTokenValue(line, "endfoot", row_info[i].endfoot);
1552 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1553 getTokenValue(line, "newpage", row_info[i].newpage);
1554 for (col_type j = 0; j < columns_; ++j) {
1555 l_getline(is, line);
1556 if (!prefixIs(line, "<cell")) {
1557 lyxerr << "Wrong tabular format (expected <cell ...> got"
1558 << line << ')' << endl;
1561 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1562 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1563 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1564 getTokenValue(line, "topline", cell_info[i][j].top_line);
1565 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1566 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1567 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1568 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1569 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1570 getTokenValue(line, "width", cell_info[i][j].p_width);
1571 getTokenValue(line, "special", cell_info[i][j].align_special);
1572 l_getline(is, line);
1573 if (prefixIs(line, "\\begin_inset")) {
1574 cell_info[i][j].inset->read(buf, lex);
1575 l_getline(is, line);
1577 if (!prefixIs(line, "</cell>")) {
1578 lyxerr << "Wrong tabular format (expected </cell> got"
1579 << line << ')' << endl;
1583 l_getline(is, line);
1584 if (!prefixIs(line, "</row>")) {
1585 lyxerr << "Wrong tabular format (expected </row> got"
1586 << line << ')' << endl;
1590 while (!prefixIs(line, "</lyxtabular>")) {
1591 l_getline(is, line);
1593 set_row_column_number_info();
1597 bool Tabular::isMultiColumn(idx_type cell) const
1599 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1603 bool Tabular::isMultiColumnReal(idx_type cell) const
1605 return column_of_cell(cell) != right_column_of_cell(cell) &&
1606 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1610 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1612 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1616 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1619 cellstruct & cs = cellinfo_of_cell(cell);
1620 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1621 cs.alignment = column_info[column_of_cell(cell)].alignment;
1622 cs.top_line = row_info[row_of_cell(cell)].top_line;
1623 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1624 cs.left_line = column_info[column_of_cell(cell)].left_line;
1625 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1626 for (idx_type i = 1; i < number; ++i) {
1627 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1628 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1629 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1632 set_row_column_number_info();
1636 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1638 row_type const row = row_of_cell(cell);
1639 col_type column = column_of_cell(cell);
1640 idx_type result = 1;
1642 while (column < columns_ &&
1643 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1652 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1654 row_type const row = row_of_cell(cell);
1655 col_type column = column_of_cell(cell);
1657 idx_type result = 0;
1659 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1660 cell_info[row][column].multicolumn = CELL_NORMAL;
1662 while (column < columns_ &&
1663 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1665 cell_info[row][column].multicolumn = CELL_NORMAL;
1670 set_row_column_number_info();
1675 void Tabular::setBookTabs(bool what)
1677 use_booktabs = what;
1681 bool Tabular::useBookTabs() const
1683 return use_booktabs;
1687 void Tabular::setLongTabular(bool what)
1689 is_long_tabular = what;
1693 bool Tabular::isLongTabular() const
1695 return is_long_tabular;
1699 void Tabular::setRotateTabular(bool flag)
1705 bool Tabular::getRotateTabular() const
1711 void Tabular::setRotateCell(idx_type cell, bool flag)
1713 cellinfo_of_cell(cell).rotate = flag;
1717 bool Tabular::getRotateCell(idx_type cell) const
1719 return cellinfo_of_cell(cell).rotate;
1723 bool Tabular::needRotating() const
1727 for (row_type i = 0; i < rows_; ++i)
1728 for (col_type j = 0; j < columns_; ++j)
1729 if (cell_info[i][j].rotate)
1735 bool Tabular::isLastCell(idx_type cell) const
1737 if (cell + 1 < numberofcells)
1743 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1745 if (row_of_cell(cell) > 0)
1746 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1751 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1753 if (row_of_cell(cell) + 1 < rows_)
1754 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1759 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1761 if (row_of_cell(cell) == 0)
1763 if (!isMultiColumn(cell))
1764 return getCellAbove(cell);
1765 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1769 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1771 if (row_of_cell(cell) + 1 >= rows_)
1773 if (!isMultiColumn(cell))
1774 return getCellBelow(cell);
1775 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1779 Tabular::idx_type Tabular::getCellNumber(row_type row,
1780 col_type column) const
1782 BOOST_ASSERT(column != npos && column < columns_ &&
1783 row != npos && row < rows_);
1784 return cell_info[row][column].cellno;
1788 void Tabular::setUsebox(idx_type cell, BoxType type)
1790 cellinfo_of_cell(cell).usebox = type;
1794 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1796 if (column_info[column_of_cell(cell)].p_width.zero() &&
1797 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1799 if (cellinfo_of_cell(cell).usebox > 1)
1800 return cellinfo_of_cell(cell).usebox;
1801 return useParbox(cell);
1806 // This are functions used for the longtable support
1808 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1814 row_info[row].endfirsthead = flag;
1818 row_info[row].endhead = flag;
1823 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1826 hd.set = haveLTHead();
1827 return row_info[row].endhead;
1831 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1834 hd.set = haveLTFirstHead();
1835 return row_info[row].endfirsthead;
1839 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1845 row_info[row].endlastfoot = flag;
1849 row_info[row].endfoot = flag;
1854 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1857 fd.set = haveLTFoot();
1858 return row_info[row].endfoot;
1862 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1865 fd.set = haveLTLastFoot();
1866 return row_info[row].endlastfoot;
1870 void Tabular::setLTNewPage(row_type row, bool what)
1872 row_info[row].newpage = what;
1876 bool Tabular::getLTNewPage(row_type row) const
1878 return row_info[row].newpage;
1882 bool Tabular::haveLTHead() const
1884 for (row_type i = 0; i < rows_; ++i)
1885 if (row_info[i].endhead)
1891 bool Tabular::haveLTFirstHead() const
1893 if (endfirsthead.empty)
1895 for (row_type i = 0; i < rows_; ++i)
1896 if (row_info[i].endfirsthead)
1902 bool Tabular::haveLTFoot() const
1904 for (row_type i = 0; i < rows_; ++i)
1905 if (row_info[i].endfoot)
1911 bool Tabular::haveLTLastFoot() const
1913 if (endlastfoot.empty)
1915 for (row_type i = 0; i < rows_; ++i)
1916 if (row_info[i].endlastfoot)
1922 // end longtable support functions
1924 void Tabular::setAscentOfRow(row_type row, int height)
1926 if (row >= rows_ || row_info[row].ascent_of_row == height)
1928 row_info[row].ascent_of_row = height;
1932 void Tabular::setDescentOfRow(row_type row, int height)
1934 if (row >= rows_ || row_info[row].descent_of_row == height)
1936 row_info[row].descent_of_row = height;
1940 int Tabular::getAscentOfRow(row_type row) const
1944 return row_info[row].ascent_of_row;
1948 int Tabular::getDescentOfRow(row_type row) const
1950 BOOST_ASSERT(row < rows_);
1951 return row_info[row].descent_of_row;
1955 int Tabular::getHeightOfTabular() const
1958 for (row_type row = 0; row < rows_; ++row)
1959 height += getAscentOfRow(row) + getDescentOfRow(row) +
1960 getAdditionalHeight(row);
1965 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1967 BOOST_ASSERT(row < rows_);
1968 BOOST_ASSERT(column < columns_);
1969 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1973 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1975 // FIXME: assert or return 0 as in TeXBottomHLine()?
1976 BOOST_ASSERT(row != npos);
1977 BOOST_ASSERT(row < rows_);
1979 idx_type const fcell = getFirstCellInRow(row);
1980 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1983 for (idx_type i = fcell; i < n; ++i) {
1987 if (use_booktabs && row == 0) {
1989 } else if (tmp == n - fcell) {
1990 os << (use_booktabs ? "\\midrule " : "\\hline ");
1992 for (idx_type i = fcell; i < n; ++i) {
1994 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1995 << column_of_cell(i) + 1
1997 << right_column_of_cell(i) + 1
2009 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2011 // FIXME: return 0 or assert as in TeXTopHLine()?
2012 if (row == npos || row >= rows_)
2015 idx_type const fcell = getFirstCellInRow(row);
2016 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2019 for (idx_type i = fcell; i < n; ++i) {
2023 if (use_booktabs && row == rows_ - 1) {
2024 os << "\\bottomrule";
2025 } else if (tmp == n - fcell) {
2026 os << (use_booktabs ? "\\midrule" : "\\hline");
2028 for (idx_type i = fcell; i < n; ++i) {
2029 if (bottomLine(i)) {
2030 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2031 << column_of_cell(i) + 1
2033 << right_column_of_cell(i) + 1
2045 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2049 if (getRotateCell(cell)) {
2050 os << "\\begin{sideways}\n";
2053 if (isMultiColumn(cell)) {
2054 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2055 if (leftLine(cell) &&
2056 (isFirstCellInRow(cell) ||
2057 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2058 !rightLine(cell - 1, true))))
2060 if (!cellinfo_of_cell(cell).align_special.empty()) {
2061 os << cellinfo_of_cell(cell).align_special;
2063 if (!getPWidth(cell).zero()) {
2064 switch (getVAlignment(cell)) {
2065 case LYX_VALIGN_TOP:
2068 case LYX_VALIGN_MIDDLE:
2071 case LYX_VALIGN_BOTTOM:
2076 << from_ascii(getPWidth(cell).asLatexString())
2079 switch (getAlignment(cell)) {
2080 case LYX_ALIGN_LEFT:
2083 case LYX_ALIGN_RIGHT:
2090 } // end if else !getPWidth
2091 } // end if else !cellinfo_of_cell
2092 if (rightLine(cell))
2094 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2099 if (getUsebox(cell) == BOX_PARBOX) {
2101 switch (getVAlignment(cell)) {
2102 case LYX_VALIGN_TOP:
2105 case LYX_VALIGN_MIDDLE:
2108 case LYX_VALIGN_BOTTOM:
2112 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2114 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2115 os << "\\begin{minipage}[";
2116 switch (getVAlignment(cell)) {
2117 case LYX_VALIGN_TOP:
2120 case LYX_VALIGN_MIDDLE:
2123 case LYX_VALIGN_BOTTOM:
2127 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2135 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2140 if (getUsebox(cell) == BOX_PARBOX)
2142 else if (getUsebox(cell) == BOX_MINIPAGE) {
2143 os << "%\n\\end{minipage}";
2146 if (isMultiColumn(cell)) {
2149 if (getRotateCell(cell)) {
2150 os << "%\n\\end{sideways}";
2157 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2158 OutputParams const & runparams) const
2160 if (!is_long_tabular)
2164 // output header info
2166 if (endhead.topDL) {
2170 for (row_type i = 0; i < rows_; ++i) {
2171 if (row_info[i].endhead) {
2172 ret += TeXRow(os, i, buf, runparams);
2175 if (endhead.bottomDL) {
2179 os << "\\endhead\n";
2181 if (endfirsthead.empty) {
2182 os << "\\endfirsthead\n";
2186 // output firstheader info
2187 if (haveLTFirstHead()) {
2188 if (endfirsthead.topDL) {
2192 for (row_type i = 0; i < rows_; ++i) {
2193 if (row_info[i].endfirsthead) {
2194 ret += TeXRow(os, i, buf, runparams);
2197 if (endfirsthead.bottomDL) {
2201 os << "\\endfirsthead\n";
2204 // output footer info
2206 if (endfoot.topDL) {
2210 for (row_type i = 0; i < rows_; ++i) {
2211 if (row_info[i].endfoot) {
2212 ret += TeXRow(os, i, buf, runparams);
2215 if (endfoot.bottomDL) {
2219 os << "\\endfoot\n";
2221 if (endlastfoot.empty) {
2222 os << "\\endlastfoot\n";
2226 // output lastfooter info
2227 if (haveLTLastFoot()) {
2228 if (endlastfoot.topDL) {
2232 for (row_type i = 0; i < rows_; ++i) {
2233 if (row_info[i].endlastfoot) {
2234 ret += TeXRow(os, i, buf, runparams);
2237 if (endlastfoot.bottomDL) {
2241 os << "\\endlastfoot\n";
2248 bool Tabular::isValidRow(row_type row) const
2250 if (!is_long_tabular)
2252 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2253 !row_info[row].endfoot && !row_info[row].endlastfoot;
2257 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2258 OutputParams const & runparams) const
2260 idx_type cell = getCellNumber(i, 0);
2261 int ret = TeXTopHLine(os, i);
2262 if (row_info[i].top_space_default) {
2264 os << "\\addlinespace\n";
2266 os << "\\noalign{\\vskip\\doublerulesep}\n";
2268 } else if(!row_info[i].top_space.zero()) {
2270 os << "\\addlinespace["
2271 << from_ascii(row_info[i].top_space.asLatexString())
2274 os << "\\noalign{\\vskip"
2275 << from_ascii(row_info[i].top_space.asLatexString())
2281 for (col_type j = 0; j < columns_; ++j) {
2282 if (isPartOfMultiColumn(i, j))
2284 ret += TeXCellPreamble(os, cell);
2285 shared_ptr<InsetText> inset = getCellInset(cell);
2287 Paragraph const & par = inset->paragraphs().front();
2288 bool rtl = par.isRightToLeftPar(buf.params())
2290 && getPWidth(cell).zero();
2293 if (par.getParLanguage(buf.params())->lang() ==
2296 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2298 // currently, remaning RTL languages are arabic_arabtex and hebrew
2302 ret += inset->latex(buf, os, runparams);
2306 ret += TeXCellPostamble(os, cell);
2307 if (!isLastCellInRow(cell)) { // not last cell in row
2312 os << "\\tabularnewline";
2313 if (row_info[i].bottom_space_default) {
2315 os << "\\addlinespace";
2317 os << "[\\doublerulesep]";
2318 } else if (!row_info[i].bottom_space.zero()) {
2320 os << "\\addlinespace";
2322 << from_ascii(row_info[i].bottom_space.asLatexString())
2327 ret += TeXBottomHLine(os, i);
2328 if (row_info[i].interline_space_default) {
2330 os << "\\addlinespace\n";
2332 os << "\\noalign{\\vskip\\doublerulesep}\n";
2334 } else if (!row_info[i].interline_space.zero()) {
2336 os << "\\addlinespace["
2337 << from_ascii(row_info[i].interline_space.asLatexString())
2340 os << "\\noalign{\\vskip"
2341 << from_ascii(row_info[i].interline_space.asLatexString())
2349 int Tabular::latex(Buffer const & buf, odocstream & os,
2350 OutputParams const & runparams) const
2354 //+---------------------------------------------------------------------
2355 //+ first the opening preamble +
2356 //+---------------------------------------------------------------------
2359 os << "\\begin{sideways}\n";
2362 if (is_long_tabular)
2363 os << "\\begin{longtable}{";
2365 os << "\\begin{tabular}{";
2366 for (col_type i = 0; i < columns_; ++i) {
2367 if (!use_booktabs && column_info[i].left_line)
2369 if (!column_info[i].align_special.empty()) {
2370 os << column_info[i].align_special;
2372 if (!column_info[i].p_width.zero()) {
2373 switch (column_info[i].alignment) {
2374 case LYX_ALIGN_LEFT:
2375 os << ">{\\raggedright}";
2377 case LYX_ALIGN_RIGHT:
2378 os << ">{\\raggedleft}";
2380 case LYX_ALIGN_CENTER:
2381 os << ">{\\centering}";
2383 case LYX_ALIGN_NONE:
2384 case LYX_ALIGN_BLOCK:
2385 case LYX_ALIGN_LAYOUT:
2386 case LYX_ALIGN_SPECIAL:
2390 switch (column_info[i].valignment) {
2391 case LYX_VALIGN_TOP:
2394 case LYX_VALIGN_MIDDLE:
2397 case LYX_VALIGN_BOTTOM:
2402 << from_ascii(column_info[i].p_width.asLatexString())
2405 switch (column_info[i].alignment) {
2406 case LYX_ALIGN_LEFT:
2409 case LYX_ALIGN_RIGHT:
2416 } // end if else !column_info[i].p_width
2417 } // end if else !column_info[i].align_special
2418 if (!use_booktabs && column_info[i].right_line)
2424 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2426 //+---------------------------------------------------------------------
2427 //+ the single row and columns (cells) +
2428 //+---------------------------------------------------------------------
2430 for (row_type i = 0; i < rows_; ++i) {
2431 if (isValidRow(i)) {
2432 ret += TeXRow(os, i, buf, runparams);
2433 if (is_long_tabular && row_info[i].newpage) {
2434 os << "\\newpage\n";
2440 //+---------------------------------------------------------------------
2441 //+ the closing of the tabular +
2442 //+---------------------------------------------------------------------
2444 if (is_long_tabular)
2445 os << "\\end{longtable}";
2447 os << "\\end{tabular}";
2449 os << "\n\\end{sideways}";
2457 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2458 OutputParams const & runparams) const
2461 idx_type cell = getFirstCellInRow(row);
2464 for (col_type j = 0; j < columns_; ++j) {
2465 if (isPartOfMultiColumn(row, j))
2468 os << "<entry align=\"";
2469 switch (getAlignment(cell)) {
2470 case LYX_ALIGN_LEFT:
2473 case LYX_ALIGN_RIGHT:
2481 os << "\" valign=\"";
2482 switch (getVAlignment(cell)) {
2483 case LYX_VALIGN_TOP:
2486 case LYX_VALIGN_BOTTOM:
2489 case LYX_VALIGN_MIDDLE:
2494 if (isMultiColumn(cell)) {
2495 os << " namest=\"col" << j << "\" ";
2496 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2500 ret += getCellInset(cell)->docbook(buf, os, runparams);
2509 int Tabular::docbook(Buffer const & buf, odocstream & os,
2510 OutputParams const & runparams) const
2514 //+---------------------------------------------------------------------
2515 //+ first the opening preamble +
2516 //+---------------------------------------------------------------------
2518 os << "<tgroup cols=\"" << columns_
2519 << "\" colsep=\"1\" rowsep=\"1\">\n";
2521 for (col_type i = 0; i < columns_; ++i) {
2522 os << "<colspec colname=\"col" << i << "\" align=\"";
2523 switch (column_info[i].alignment) {
2524 case LYX_ALIGN_LEFT:
2527 case LYX_ALIGN_RIGHT:
2535 if (runparams.flavor == OutputParams::XML)
2541 //+---------------------------------------------------------------------
2542 //+ Long Tabular case +
2543 //+---------------------------------------------------------------------
2545 // output header info
2546 if (haveLTHead() || haveLTFirstHead()) {
2549 for (row_type i = 0; i < rows_; ++i) {
2550 if (row_info[i].endhead || row_info[i].endfirsthead) {
2551 ret += docbookRow(buf, os, i, runparams);
2557 // output footer info
2558 if (haveLTFoot() || haveLTLastFoot()) {
2561 for (row_type i = 0; i < rows_; ++i) {
2562 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2563 ret += docbookRow(buf, os, i, runparams);
2570 //+---------------------------------------------------------------------
2571 //+ the single row and columns (cells) +
2572 //+---------------------------------------------------------------------
2576 for (row_type i = 0; i < rows_; ++i) {
2577 if (isValidRow(i)) {
2578 ret += docbookRow(buf, os, i, runparams);
2583 //+---------------------------------------------------------------------
2584 //+ the closing of the tabular +
2585 //+---------------------------------------------------------------------
2594 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2595 vector<unsigned int> const & clen) const
2597 idx_type const fcell = getFirstCellInRow(row);
2598 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2601 for (idx_type i = fcell; i < n; ++i) {
2611 for (idx_type i = fcell; i < n; ++i) {
2622 col_type column = column_of_cell(i);
2623 int len = clen[column];
2624 while (column < columns_ - 1
2625 && isPartOfMultiColumn(row, ++column))
2626 len += clen[column] + 4;
2627 os << docstring(len, ch);
2642 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2643 vector<unsigned int> const & clen) const
2645 idx_type const fcell = getFirstCellInRow(row);
2646 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2649 for (idx_type i = fcell; i < n; ++i) {
2650 if (bottomLine(i)) {
2659 for (idx_type i = fcell; i < n; ++i) {
2660 if (bottomLine(i)) {
2670 col_type column = column_of_cell(i);
2671 int len = clen[column];
2672 while (column < columns_ -1
2673 && isPartOfMultiColumn(row, ++column))
2674 len += clen[column] + 4;
2675 os << docstring(len, ch);
2676 if (bottomLine(i)) {
2690 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2691 OutputParams const & runparams,
2692 idx_type cell, row_type row, col_type column,
2693 vector<unsigned int> const & clen,
2694 bool onlydata) const
2696 odocstringstream sstr;
2697 getCellInset(cell)->plaintext(buf, sstr, runparams);
2709 unsigned int len1 = sstr.str().length();
2710 unsigned int len2 = clen[column];
2711 while (column < columns_ -1
2712 && isPartOfMultiColumn(row, ++column))
2713 len2 += clen[column] + 4;
2716 switch (getAlignment(cell)) {
2718 case LYX_ALIGN_LEFT:
2721 case LYX_ALIGN_RIGHT:
2725 case LYX_ALIGN_CENTER:
2731 os << docstring(len1, ' ') << sstr.str()
2732 << docstring(len2, ' ');
2734 if (rightLine(cell))
2741 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2742 OutputParams const & runparams, int const depth,
2743 bool onlydata, unsigned char delim) const
2745 // first calculate the width of the single columns
2746 vector<unsigned int> clen(columns_);
2749 // first all non (real) multicolumn cells!
2750 for (col_type j = 0; j < columns_; ++j) {
2752 for (row_type i = 0; i < rows_; ++i) {
2753 idx_type cell = getCellNumber(i, j);
2754 if (isMultiColumnReal(cell))
2756 odocstringstream sstr;
2757 getCellInset(cell)->plaintext(buf, sstr, runparams);
2758 if (clen[j] < sstr.str().length())
2759 clen[j] = sstr.str().length();
2762 // then all (real) multicolumn cells!
2763 for (col_type j = 0; j < columns_; ++j) {
2764 for (row_type i = 0; i < rows_; ++i) {
2765 idx_type cell = getCellNumber(i, j);
2766 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2768 odocstringstream sstr;
2769 getCellInset(cell)->plaintext(buf, sstr, runparams);
2770 int len = int(sstr.str().length());
2771 idx_type const n = cells_in_multicolumn(cell);
2772 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2774 if (len > int(clen[j + n - 1]))
2775 clen[j + n - 1] = len;
2780 for (row_type i = 0; i < rows_; ++i) {
2781 if (!onlydata && plaintextTopHLine(os, i, clen))
2782 os << docstring(depth * 2, ' ');
2783 for (col_type j = 0; j < columns_; ++j) {
2784 if (isPartOfMultiColumn(i, j))
2786 if (onlydata && j > 0)
2788 plaintextPrintCell(buf, os, runparams,
2789 cell, i, j, clen, onlydata);
2794 os << docstring(depth * 2, ' ');
2795 if (plaintextBottomHLine(os, i, clen))
2796 os << docstring(depth * 2, ' ');
2802 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2804 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2808 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2809 col_type column) const
2811 return cell_info[row][column].inset;
2815 void Tabular::setCellInset(row_type row, col_type column,
2816 shared_ptr<InsetText> ins) const
2818 cell_info[row][column].inset = ins;
2823 Tabular::getCellFromInset(Inset const * inset) const
2825 // is this inset part of the tabular?
2827 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2828 BOOST_ASSERT(false);
2831 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2832 if (getCellInset(cell).get() == inset) {
2833 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2834 << "cell=" << cell << endl;
2838 // We should have found a cell at this point
2839 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2840 << inset << " not found!" << endl;
2841 BOOST_ASSERT(false);
2847 void Tabular::validate(LaTeXFeatures & features) const
2849 features.require("NeedTabularnewline");
2851 features.require("booktabs");
2852 if (isLongTabular())
2853 features.require("longtable");
2855 features.require("rotating");
2856 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2857 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2858 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2859 features.require("array");
2860 getCellInset(cell)->validate(features);
2865 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2867 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2868 ParagraphList::const_iterator cit = parlist.begin();
2869 ParagraphList::const_iterator end = parlist.end();
2871 for (; cit != end; ++cit)
2872 for (int i = 0; i < cit->size(); ++i)
2873 if (cit->isNewline(i))
2880 /////////////////////////////////////////////////////////////////////
2884 /////////////////////////////////////////////////////////////////////
2886 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2888 : tabular(buf.params(), max(rows, row_type(1)),
2889 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2893 InsetTabular::InsetTabular(InsetTabular const & tab)
2894 : Inset(tab), tabular(tab.tabular),
2895 buffer_(tab.buffer_), scx_(0)
2899 InsetTabular::~InsetTabular()
2901 InsetTabularMailer(*this).hideDialog();
2905 auto_ptr<Inset> InsetTabular::doClone() const
2907 return auto_ptr<Inset>(new InsetTabular(*this));
2911 Buffer const & InsetTabular::buffer() const
2917 void InsetTabular::buffer(Buffer const * b)
2923 void InsetTabular::write(Buffer const & buf, ostream & os) const
2925 os << "Tabular" << endl;
2926 tabular.write(buf, os);
2930 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2932 bool const old_format = (lex.getString() == "\\LyXTable");
2934 tabular.read(buf, lex);
2940 string token = lex.getString();
2941 while (lex && token != "\\end_inset") {
2943 token = lex.getString();
2946 lex.printError("Missing \\end_inset at this point. "
2952 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2954 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2955 // mi.base.textwidth << "\n";
2957 lyxerr << "InsetTabular::metrics: need bv" << endl;
2958 BOOST_ASSERT(false);
2962 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2965 for (col_type j = 0; j < tabular.columns(); ++j) {
2966 if (tabular.isPartOfMultiColumn(i, j))
2967 // Multicolumn cell, but not first one
2972 if (tabular.cell_info[i][j].multicolumn ==
2973 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2974 p_width = tabular.cellinfo_of_cell(cell).p_width;
2976 p_width = tabular.column_info[j].p_width;
2977 if (!p_width.zero())
2978 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2979 tabular.getCellInset(cell)->metrics(m, dim);
2980 if (!p_width.zero())
2981 dim.wid = m.base.textwidth;
2982 tabular.setWidthOfCell(cell, dim.wid);
2983 if (p_width.zero()) {
2984 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2985 // FIXME there must be a way to get rid of
2986 // the second metrics call
2987 tabular.getCellInset(cell)->metrics(m, dim);
2989 maxAsc = max(maxAsc, dim.asc);
2990 maxDesc = max(maxDesc, dim.des);
2993 int const top_space = tabular.row_info[i].top_space_default ?
2994 default_line_space :
2995 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2996 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2997 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2998 default_line_space :
2999 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3000 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3003 dim.asc = tabular.getAscentOfRow(0);
3004 dim.des = tabular.getHeightOfTabular() - dim.asc;
3005 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
3006 bool const changed = dim_ != dim;
3012 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3014 setPosCache(pi, x, y);
3016 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3017 BufferView * bv = pi.base.bv;
3019 resetPos(bv->cursor());
3022 x += ADD_TO_TABULAR_WIDTH;
3025 first_visible_cell = Tabular::npos;
3026 for (row_type i = 0; i < tabular.rows(); ++i) {
3028 int const a = tabular.getAscentOfRow(i);
3029 int const d = tabular.getDescentOfRow(i);
3030 idx = tabular.getCellNumber(i, 0);
3031 for (col_type j = 0; j < tabular.columns(); ++j) {
3032 if (tabular.isPartOfMultiColumn(i, j))
3034 if (first_visible_cell == Tabular::npos)
3035 first_visible_cell = idx;
3037 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3038 if (nx + tabular.getWidthOfColumn(idx) < 0
3039 || nx > bv->workWidth()
3041 || y - a > bv->workHeight()) {
3042 pi.pain.setDrawingEnabled(false);
3043 cell(idx)->draw(pi, cx, y);
3044 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3045 pi.pain.setDrawingEnabled(true);
3047 cell(idx)->draw(pi, cx, y);
3048 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3050 nx += tabular.getWidthOfColumn(idx);
3054 if (i + 1 < tabular.rows())
3055 y += d + tabular.getAscentOfRow(i + 1) +
3056 tabular.getAdditionalHeight(i + 1);
3061 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3063 setPosCache(pi, x, y);
3065 Cursor & cur = pi.base.bv->cursor();
3067 x += scx_ + ADD_TO_TABULAR_WIDTH;
3069 // Paint background of current tabular
3070 int const w = tabular.getWidthOfTabular();
3071 int const h = tabular.getHeightOfTabular();
3072 int yy = y - tabular.getAscentOfRow(0);
3073 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3075 if (!cur.selection())
3077 if (!ptr_cmp(&cur.inset(), this))
3083 if (tablemode(cur)) {
3086 getSelection(cur, rs, re, cs, ce);
3087 y -= tabular.getAscentOfRow(0);
3088 for (row_type j = 0; j < tabular.rows(); ++j) {
3089 int const a = tabular.getAscentOfRow(j);
3090 int const h = a + tabular.getDescentOfRow(j);
3092 y += tabular.getAdditionalHeight(j);
3093 for (col_type i = 0; i < tabular.columns(); ++i) {
3094 if (tabular.isPartOfMultiColumn(j, i))
3096 idx_type const cell =
3097 tabular.getCellNumber(j, i);
3098 int const w = tabular.getWidthOfColumn(cell);
3099 if (i >= cs && i <= ce && j >= rs && j <= re)
3100 pi.pain.fillRectangle(xx, y, w, h,
3108 x += getCellXPos(cur.idx());
3109 x += tabular.getBeginningOfTextInCell(cur.idx());
3110 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3115 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3116 row_type row, idx_type cell, bool erased) const
3118 int x2 = x + tabular.getWidthOfColumn(cell);
3119 bool on_off = false;
3120 Color::color col = Color::tabularline;
3121 Color::color onoffcol = Color::tabularonoffline;
3124 col = Color::deletedtext;
3125 onoffcol = Color::deletedtext;
3128 if (!tabular.topAlreadyDrawn(cell)) {
3129 on_off = !tabular.topLine(cell);
3130 pain.line(x, y - tabular.getAscentOfRow(row),
3131 x2, y - tabular.getAscentOfRow(row),
3132 on_off ? onoffcol : col,
3133 on_off ? Painter::line_onoffdash : Painter::line_solid);
3135 on_off = !tabular.bottomLine(cell);
3136 pain.line(x, y + tabular.getDescentOfRow(row),
3137 x2, y + tabular.getDescentOfRow(row),
3138 on_off ? onoffcol : col,
3139 on_off ? Painter::line_onoffdash : Painter::line_solid);
3140 if (!tabular.leftAlreadyDrawn(cell)) {
3141 on_off = !tabular.leftLine(cell);
3142 pain.line(x, y - tabular.getAscentOfRow(row),
3143 x, y + tabular.getDescentOfRow(row),
3144 on_off ? onoffcol : col,
3145 on_off ? Painter::line_onoffdash : Painter::line_solid);
3147 on_off = !tabular.rightLine(cell);
3148 pain.line(x2 - tabular.getAdditionalWidth(cell),
3149 y - tabular.getAscentOfRow(row),
3150 x2 - tabular.getAdditionalWidth(cell),
3151 y + tabular.getDescentOfRow(row),
3152 on_off ? onoffcol : col,
3153 on_off ? Painter::line_onoffdash : Painter::line_solid);
3157 docstring const InsetTabular::editMessage() const
3159 return _("Opened table");
3163 void InsetTabular::edit(Cursor & cur, bool left)
3165 //lyxerr << "InsetTabular::edit: " << this << endl;
3167 cur.selection() = false;
3170 if (isRightToLeft(cur))
3171 cur.idx() = tabular.getLastCellInRow(0);
3177 if (isRightToLeft(cur))
3178 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3180 cur.idx() = tabular.getNumberOfCells() - 1;
3182 cur.pos() = cur.lastpos(); // FIXME crude guess
3184 // FIXME: this accesses the position cache before it is initialized
3186 //cur.bv().fitCursor();
3190 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3192 // In a longtable, tell captions what the current float is
3193 Counters & cnts = buf.params().getTextClass().counters();
3194 string const saveflt = cnts.current_float();
3195 if (tabular.isLongTabular())
3196 cnts.current_float("table");
3198 ParIterator it2 = it;
3200 for ( ; it2.idx() <= it2.lastidx() ; it2.forwardIdx())
3201 lyx::updateLabels(buf, it2);
3204 if (tabular.isLongTabular())
3205 cnts.current_float(saveflt);
3209 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3211 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3212 << "\n cur:" << cur << endl;
3213 CursorSlice sl = cur.top();
3214 Cursor & bvcur = cur.bv().cursor();
3216 switch (cmd.action) {
3218 case LFUN_MOUSE_PRESS:
3219 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3221 // do not reset cursor/selection if we have selected
3222 // some cells (bug 2715).
3223 if (cmd.button() == mouse_button::button3
3224 && &bvcur.selBegin().inset() == this
3225 && tablemode(bvcur))
3228 // Let InsetText do it
3229 cell(cur.idx())->dispatch(cur, cmd);
3231 case LFUN_MOUSE_MOTION:
3232 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3233 if (cmd.button() == mouse_button::button1) {
3234 // only accept motions to places not deeper nested than the real anchor
3235 if (bvcur.anchor_.hasPart(cur)) {
3236 // only update if selection changes
3237 if (bvcur.idx() == cur.idx() &&
3238 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3240 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3241 bvcur.setCursor(cur);
3242 bvcur.selection() = true;
3248 case LFUN_MOUSE_RELEASE:
3249 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3250 if (cmd.button() == mouse_button::button3)
3251 InsetTabularMailer(*this).showDialog(&cur.bv());
3254 case LFUN_CELL_BACKWARD:
3256 cur.selection() = false;
3259 case LFUN_CELL_FORWARD:
3261 cur.selection() = false;
3264 case LFUN_CHAR_FORWARD_SELECT:
3265 case LFUN_CHAR_FORWARD:
3266 cell(cur.idx())->dispatch(cur, cmd);
3267 if (!cur.result().dispatched()) {
3268 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3269 if (sl == cur.top())
3270 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3276 case LFUN_CHAR_BACKWARD_SELECT:
3277 case LFUN_CHAR_BACKWARD:
3278 cell(cur.idx())->dispatch(cur, cmd);
3279 if (!cur.result().dispatched()) {
3280 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3281 if (sl == cur.top())
3282 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3288 case LFUN_DOWN_SELECT:
3290 cell(cur.idx())->dispatch(cur, cmd);
3291 cur.dispatched(); // override the cell's decision
3292 if (sl == cur.top())
3293 // if our Text didn't do anything to the cursor
3294 // then we try to put the cursor into the cell below
3295 // setting also the right targetX.
3296 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3297 cur.idx() = tabular.getCellBelow(cur.idx());
3299 TextMetrics const & tm =
3300 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3301 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3303 if (sl == cur.top()) {
3304 // we trick it to go to the RIGHT after leaving the
3306 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3311 case LFUN_UP_SELECT:
3313 cell(cur.idx())->dispatch(cur, cmd);
3314 cur.dispatched(); // override the cell's decision
3315 if (sl == cur.top())
3316 // if our Text didn't do anything to the cursor
3317 // then we try to put the cursor into the cell above
3318 // setting also the right targetX.
3319 if (tabular.row_of_cell(cur.idx()) != 0) {
3320 cur.idx() = tabular.getCellAbove(cur.idx());
3321 cur.pit() = cur.lastpit();
3322 Text const * text = cell(cur.idx())->getText(0);
3323 TextMetrics const & tm = cur.bv().textMetrics(text);
3324 ParagraphMetrics const & pm =
3325 tm.parMetrics(cur.lastpit());
3326 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3328 if (sl == cur.top()) {
3329 cmd = FuncRequest(LFUN_UP);
3334 // case LFUN_SCREEN_DOWN: {
3335 // //if (hasSelection())
3336 // // cur.selection() = false;
3337 // col_type const col = tabular.column_of_cell(cur.idx());
3338 // int const t = cur.bv().top_y() + cur.bv().height();
3339 // if (t < yo() + tabular.getHeightOfTabular()) {
3340 // cur.bv().scrollDocView(t);
3341 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3343 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3350 // case LFUN_SCREEN_UP: {
3351 // //if (hasSelection())
3352 // // cur.selection() = false;
3353 // col_type const col = tabular.column_of_cell(cur.idx());
3354 // int const t = cur.bv().top_y() + cur.bv().height();
3356 // cur.bv().scrollDocView(t);
3360 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3364 // cur.par() = cur.lastpar();
3365 // cur.pos() = cur.lastpos();
3369 case LFUN_LAYOUT_TABULAR:
3370 InsetTabularMailer(*this).showDialog(&cur.bv());
3373 case LFUN_INSET_DIALOG_UPDATE:
3374 InsetTabularMailer(*this).updateDialog(&cur.bv());
3377 case LFUN_TABULAR_FEATURE:
3378 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3382 // insert file functions
3383 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3384 case LFUN_FILE_INSERT_PLAINTEXT: {
3386 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3387 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3393 if (tablemode(cur)) {
3394 if (copySelection(cur)) {
3395 recordUndoInset(cur, Undo::DELETE);
3400 cell(cur.idx())->dispatch(cur, cmd);
3403 case LFUN_CHAR_DELETE_BACKWARD:
3404 case LFUN_CHAR_DELETE_FORWARD:
3405 if (tablemode(cur)) {
3406 recordUndoInset(cur, Undo::DELETE);
3410 cell(cur.idx())->dispatch(cur, cmd);
3414 if (!cur.selection())
3416 if (tablemode(cur)) {
3420 cell(cur.idx())->dispatch(cur, cmd);
3423 case LFUN_CLIPBOARD_PASTE:
3424 case LFUN_PRIMARY_SELECTION_PASTE: {
3425 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3426 theClipboard().getAsText() :
3427 theSelection().get();
3430 // pass to InsertPlaintextString, but
3431 // only if we have multi-cell content
3432 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3433 if (insertPlaintextString(cur.bv(), clip, false)) {
3434 // content has been replaced,
3435 // so cursor might be invalid
3436 cur.pos() = cur.lastpos();
3437 bvcur.setCursor(cur);
3441 // Let the cell handle normal text
3442 cell(cur.idx())->dispatch(cur, cmd);
3447 if (tabularStackDirty() && theClipboard().isInternal()) {
3448 recordUndoInset(cur, Undo::INSERT);
3449 pasteClipboard(cur);
3452 cell(cur.idx())->dispatch(cur, cmd);
3455 case LFUN_FONT_EMPH:
3456 case LFUN_FONT_BOLD:
3457 case LFUN_FONT_ROMAN:
3458 case LFUN_FONT_NOUN:
3459 case LFUN_FONT_ITAL:
3460 case LFUN_FONT_FRAK:
3461 case LFUN_FONT_CODE:
3462 case LFUN_FONT_SANS:
3463 case LFUN_FONT_FREE_APPLY:
3464 case LFUN_FONT_FREE_UPDATE:
3465 case LFUN_FONT_SIZE:
3466 case LFUN_FONT_UNDERLINE:
3468 case LFUN_WORD_CAPITALIZE:
3469 case LFUN_WORD_UPCASE:
3470 case LFUN_WORD_LOWCASE:
3471 case LFUN_CHARS_TRANSPOSE:
3472 if (tablemode(cur)) {
3475 getSelection(cur, rs, re, cs, ce);
3476 Cursor tmpcur = cur;
3477 for (row_type i = rs; i <= re; ++i) {
3478 for (col_type j = cs; j <= ce; ++j) {
3479 // cursor follows cell:
3480 tmpcur.idx() = tabular.getCellNumber(i, j);
3481 // select this cell only:
3484 tmpcur.resetAnchor();
3485 tmpcur.pit() = tmpcur.lastpit();
3486 tmpcur.pos() = tmpcur.top().lastpos();
3487 tmpcur.setCursor(tmpcur);
3488 tmpcur.setSelection();
3489 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3494 cell(cur.idx())->dispatch(cur, cmd);
3498 // we try to handle this event in the insets dispatch function.
3499 cell(cur.idx())->dispatch(cur, cmd);
3505 // function sets an object as defined in func_status.h:
3506 // states OK, Unknown, Disabled, On, Off.
3507 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3508 FuncStatus & status) const
3510 switch (cmd.action) {
3511 case LFUN_TABULAR_FEATURE: {
3512 int action = Tabular::LAST_ACTION;
3514 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3515 string const tmp = tabularFeature[i].feature;
3516 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3517 action = tabularFeature[i].action;
3521 if (action == Tabular::LAST_ACTION) {
3523 status.unknown(true);
3527 string const argument
3528 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3530 row_type sel_row_start = 0;
3531 row_type sel_row_end = 0;
3532 col_type sel_col_start = 0;
3533 col_type sel_col_end = 0;
3534 Tabular::ltType dummyltt;
3537 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3540 case Tabular::SET_PWIDTH:
3541 case Tabular::SET_MPWIDTH:
3542 case Tabular::SET_SPECIAL_COLUMN:
3543 case Tabular::SET_SPECIAL_MULTI:
3544 case Tabular::APPEND_ROW:
3545 case Tabular::APPEND_COLUMN:
3546 case Tabular::DELETE_ROW:
3547 case Tabular::DELETE_COLUMN:
3548 case Tabular::COPY_ROW:
3549 case Tabular::COPY_COLUMN:
3550 case Tabular::SET_ALL_LINES:
3551 case Tabular::UNSET_ALL_LINES:
3552 case Tabular::SET_TOP_SPACE:
3553 case Tabular::SET_BOTTOM_SPACE:
3554 case Tabular::SET_INTERLINE_SPACE:
3558 case Tabular::MULTICOLUMN:
3559 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3562 case Tabular::M_TOGGLE_LINE_TOP:
3564 case Tabular::TOGGLE_LINE_TOP:
3565 status.setOnOff(tabular.topLine(cur.idx(), flag));
3568 case Tabular::M_TOGGLE_LINE_BOTTOM:
3570 case Tabular::TOGGLE_LINE_BOTTOM:
3571 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3574 case Tabular::M_TOGGLE_LINE_LEFT:
3576 case Tabular::TOGGLE_LINE_LEFT:
3577 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3580 case Tabular::M_TOGGLE_LINE_RIGHT:
3582 case Tabular::TOGGLE_LINE_RIGHT:
3583 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3586 case Tabular::M_ALIGN_LEFT:
3588 case Tabular::ALIGN_LEFT:
3589 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3592 case Tabular::M_ALIGN_RIGHT:
3594 case Tabular::ALIGN_RIGHT:
3595 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3598 case Tabular::M_ALIGN_CENTER:
3600 case Tabular::ALIGN_CENTER:
3601 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3604 case Tabular::ALIGN_BLOCK:
3605 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3606 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3609 case Tabular::M_VALIGN_TOP:
3611 case Tabular::VALIGN_TOP:
3613 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3616 case Tabular::M_VALIGN_BOTTOM:
3618 case Tabular::VALIGN_BOTTOM:
3620 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3623 case Tabular::M_VALIGN_MIDDLE:
3625 case Tabular::VALIGN_MIDDLE:
3627 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3630 case Tabular::SET_LONGTABULAR:
3631 status.setOnOff(tabular.isLongTabular());
3634 case Tabular::UNSET_LONGTABULAR:
3635 status.setOnOff(!tabular.isLongTabular());
3638 case Tabular::TOGGLE_ROTATE_TABULAR:
3639 case Tabular::SET_ROTATE_TABULAR:
3640 status.setOnOff(tabular.getRotateTabular());
3643 case Tabular::UNSET_ROTATE_TABULAR:
3644 status.setOnOff(!tabular.getRotateTabular());
3647 case Tabular::TOGGLE_ROTATE_CELL:
3648 case Tabular::SET_ROTATE_CELL:
3649 status.setOnOff(!oneCellHasRotationState(false,
3650 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3653 case Tabular::UNSET_ROTATE_CELL:
3654 status.setOnOff(!oneCellHasRotationState(true,
3655 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3658 case Tabular::SET_USEBOX:
3659 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3662 case Tabular::SET_LTFIRSTHEAD:
3663 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3666 case Tabular::UNSET_LTFIRSTHEAD:
3667 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3670 case Tabular::SET_LTHEAD:
3671 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3674 case Tabular::UNSET_LTHEAD:
3675 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3678 case Tabular::SET_LTFOOT:
3679 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3682 case Tabular::UNSET_LTFOOT:
3683 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3686 case Tabular::SET_LTLASTFOOT:
3687 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3690 case Tabular::UNSET_LTLASTFOOT:
3691 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3694 case Tabular::SET_LTNEWPAGE:
3695 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3698 case Tabular::SET_BOOKTABS:
3699 status.setOnOff(tabular.useBookTabs());
3702 case Tabular::UNSET_BOOKTABS:
3703 status.setOnOff(!tabular.useBookTabs());
3708 status.enabled(false);
3714 // These are only enabled inside tabular
3715 case LFUN_CELL_BACKWARD:
3716 case LFUN_CELL_FORWARD:
3717 status.enabled(true);
3720 // disable these with multiple cells selected
3721 case LFUN_INSET_INSERT:
3722 case LFUN_TABULAR_INSERT:
3723 case LFUN_CHARSTYLE_INSERT:
3724 case LFUN_FLOAT_INSERT:
3725 case LFUN_FLOAT_WIDE_INSERT:
3726 case LFUN_FOOTNOTE_INSERT:
3727 case LFUN_MARGINALNOTE_INSERT:
3728 case LFUN_MATH_INSERT:
3729 case LFUN_MATH_MODE:
3730 case LFUN_MATH_MUTATE:
3731 case LFUN_MATH_DISPLAY:
3732 case LFUN_NOTE_INSERT:
3733 case LFUN_OPTIONAL_INSERT:
3734 case LFUN_BOX_INSERT:
3735 case LFUN_BRANCH_INSERT:
3736 case LFUN_WRAP_INSERT:
3737 case LFUN_ERT_INSERT: {
3738 if (tablemode(cur)) {
3739 status.enabled(false);
3742 return cell(cur.idx())->getStatus(cur, cmd, status);
3745 // disable in non-fixed-width cells
3746 case LFUN_BREAK_LINE:
3747 case LFUN_BREAK_PARAGRAPH:
3748 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3749 case LFUN_BREAK_PARAGRAPH_SKIP: {
3750 if (tabular.getPWidth(cur.idx()).zero()) {
3751 status.enabled(false);
3754 return cell(cur.idx())->getStatus(cur, cmd, status);
3758 if (tabularStackDirty() && theClipboard().isInternal()) {
3759 status.enabled(true);
3762 return cell(cur.idx())->getStatus(cur, cmd, status);
3764 case LFUN_INSET_MODIFY:
3765 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3766 status.enabled(true);
3772 // we try to handle this event in the insets dispatch function.
3773 return cell(cur.idx())->getStatus(cur, cmd, status);
3778 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3779 OutputParams const & runparams) const
3781 return tabular.latex(buf, os, runparams);
3785 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3786 OutputParams const & runparams) const
3788 os << '\n'; // output table on a new line
3789 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3790 tabular.plaintext(buf, os, runparams, dp, false, 0);
3791 return PLAINTEXT_NEWLINE;
3795 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3796 OutputParams const & runparams) const
3801 // FIXME: Why not pass a proper DocIterator here?
3803 // if the table is inside a float it doesn't need the informaltable
3804 // wrapper. Search for it.
3805 for (master = owner(); master; master = master->owner())
3806 if (master->lyxCode() == Inset::FLOAT_CODE)
3811 os << "<informaltable>";
3814 ret += tabular.docbook(buf, os, runparams);
3816 os << "</informaltable>";
3823 void InsetTabular::validate(LaTeXFeatures & features) const
3825 tabular.validate(features);
3829 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3831 return tabular.getCellInset(idx);
3835 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3837 return tabular.getCellInset(idx);
3841 void InsetTabular::cursorPos(BufferView const & bv,
3842 CursorSlice const & sl, bool boundary, int & x, int & y) const
3844 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3846 // y offset correction
3847 int const row = tabular.row_of_cell(sl.idx());
3848 for (int i = 0; i <= row; ++i) {
3850 y += tabular.getAscentOfRow(i);
3851 y += tabular.getAdditionalHeight(i);
3854 y += tabular.getDescentOfRow(i);
3857 // x offset correction
3858 int const col = tabular.column_of_cell(sl.idx());
3859 int idx = tabular.getCellNumber(row, 0);
3860 for (int j = 0; j < col; ++j) {
3861 if (tabular.isPartOfMultiColumn(row, j))
3863 x += tabular.getWidthOfColumn(idx);
3866 x += tabular.getBeginningOfTextInCell(idx);
3867 x += ADD_TO_TABULAR_WIDTH;
3872 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3876 Inset const & inset = *tabular.getCellInset(cell);
3877 Point o = bv.coordCache().getInsets().xy(&inset);
3878 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3879 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3880 row_type const row = tabular.row_of_cell(cell);
3881 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3882 tabular.getAdditionalHeight(row);
3883 int const yend = o.y_ + tabular.getDescentOfRow(row);
3895 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3896 // << " ybeg=" << ybeg << " yend=" << yend
3897 // << " xx=" << xx << " yy=" << yy
3898 // << " dist=" << xx + yy << endl;
3903 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3905 //lyxerr << "InsetTabular::editXY: " << this << endl;
3906 cur.selection() = false;
3908 cur.idx() = getNearestCell(cur.bv(), x, y);
3910 return cell(cur.idx())->text_.editXY(cur, x, y);
3914 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3916 cur.idx() = getNearestCell(cur.bv(), x, y);
3917 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3921 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3923 idx_type idx_min = 0;
3924 int dist_min = std::numeric_limits<int>::max();
3925 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3926 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3927 int const d = dist(bv, i, x, y);
3938 int InsetTabular::getCellXPos(idx_type const cell) const
3942 for (; !tabular.isFirstCellInRow(c); --c)
3945 for (; c < cell; ++c)
3946 lx += tabular.getWidthOfColumn(c);
3952 void InsetTabular::resetPos(Cursor & cur) const
3954 BufferView & bv = cur.bv();
3955 int const maxwidth = bv.workWidth();
3957 if (&cur.inset() != this) {
3961 int const X2 = maxwidth;
3962 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3963 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3964 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3967 scx_ = X1 + 20 - x1;
3969 scx_ = X2 - 20 - x2;
3974 cur.updateFlags(Update::Force | Update::FitCursor);
3978 void InsetTabular::moveNextCell(Cursor & cur)
3980 if (isRightToLeft(cur)) {
3981 if (tabular.isFirstCellInRow(cur.idx())) {
3982 row_type const row = tabular.row_of_cell(cur.idx());
3983 if (row == tabular.rows() - 1)
3985 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3992 if (tabular.isLastCell(cur.idx()))
4002 void InsetTabular::movePrevCell(Cursor & cur)
4004 if (isRightToLeft(cur)) {
4005 if (tabular.isLastCellInRow(cur.idx())) {
4006 row_type const row = tabular.row_of_cell(cur.idx());
4009 cur.idx() = tabular.getFirstCellInRow(row);
4010 cur.idx() = tabular.getCellAbove(cur.idx());
4012 if (tabular.isLastCell(cur.idx()))
4017 if (cur.idx() == 0) // first cell
4021 cur.pit() = cur.lastpit();
4022 cur.pos() = cur.lastpos();
4024 // FIXME: this accesses the position cache before it is initialized
4029 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4031 Tabular::Feature action = Tabular::LAST_ACTION;
4034 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4035 string const tmp = tabularFeature[i].feature;
4037 if (tmp == what.substr(0, tmp.length())) {
4038 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4039 //tabularFeatures[i].feature.length()))
4040 action = tabularFeature[i].action;
4044 if (action == Tabular::LAST_ACTION)
4048 ltrim(what.substr(tabularFeature[i].feature.length()));
4049 tabularFeatures(cur, action, val);
4054 static void checkLongtableSpecial(Tabular::ltType & ltt,
4055 string const & special, bool & flag)
4057 if (special == "dl_above") {
4060 } else if (special == "dl_below") {
4061 ltt.bottomDL = flag;
4063 } else if (special == "empty") {
4072 bool InsetTabular::oneCellHasRotationState(bool rotated,
4073 row_type row_start, row_type row_end,
4074 col_type col_start, col_type col_end) const {
4076 for (row_type i = row_start; i <= row_end; ++i) {
4077 for (col_type j = col_start; j <= col_end; ++j) {
4078 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4087 void InsetTabular::tabularFeatures(Cursor & cur,
4088 Tabular::Feature feature, string const & value)
4090 BufferView & bv = cur.bv();
4091 col_type sel_col_start;
4092 col_type sel_col_end;
4093 row_type sel_row_start;
4094 row_type sel_row_end;
4095 bool setLines = false;
4096 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4097 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4101 case Tabular::M_ALIGN_LEFT:
4102 case Tabular::ALIGN_LEFT:
4103 setAlign = LYX_ALIGN_LEFT;
4106 case Tabular::M_ALIGN_RIGHT:
4107 case Tabular::ALIGN_RIGHT:
4108 setAlign = LYX_ALIGN_RIGHT;
4111 case Tabular::M_ALIGN_CENTER:
4112 case Tabular::ALIGN_CENTER:
4113 setAlign = LYX_ALIGN_CENTER;
4116 case Tabular::ALIGN_BLOCK:
4117 setAlign = LYX_ALIGN_BLOCK;
4120 case Tabular::M_VALIGN_TOP:
4121 case Tabular::VALIGN_TOP:
4122 setVAlign = Tabular::LYX_VALIGN_TOP;
4125 case Tabular::M_VALIGN_BOTTOM:
4126 case Tabular::VALIGN_BOTTOM:
4127 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4130 case Tabular::M_VALIGN_MIDDLE:
4131 case Tabular::VALIGN_MIDDLE:
4132 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4139 recordUndoInset(cur, Undo::ATOMIC);
4141 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4142 row_type const row = tabular.row_of_cell(cur.idx());
4143 col_type const column = tabular.column_of_cell(cur.idx());
4145 Tabular::ltType ltt;
4149 case Tabular::SET_PWIDTH: {
4150 Length const len(value);
4151 tabular.setColumnPWidth(cur, cur.idx(), len);
4153 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4154 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4158 case Tabular::SET_MPWIDTH:
4159 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4162 case Tabular::SET_SPECIAL_COLUMN:
4163 case Tabular::SET_SPECIAL_MULTI:
4164 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4167 case Tabular::APPEND_ROW:
4168 // append the row into the tabular
4169 tabular.appendRow(bv.buffer()->params(), cur.idx());
4172 case Tabular::APPEND_COLUMN:
4173 // append the column into the tabular
4174 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4175 cur.idx() = tabular.getCellNumber(row, column);
4178 case Tabular::DELETE_ROW:
4179 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4180 tabular.deleteRow(sel_row_start);
4181 if (sel_row_start >= tabular.rows())
4183 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4186 cur.selection() = false;
4189 case Tabular::DELETE_COLUMN:
4190 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4191 tabular.deleteColumn(sel_col_start);
4192 if (sel_col_start >= tabular.columns())
4194 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4197 cur.selection() = false;
4200 case Tabular::COPY_ROW:
4201 tabular.copyRow(bv.buffer()->params(), row);
4204 case Tabular::COPY_COLUMN:
4205 tabular.copyColumn(bv.buffer()->params(), column);
4206 cur.idx() = tabular.getCellNumber(row, column);
4209 case Tabular::M_TOGGLE_LINE_TOP:
4211 case Tabular::TOGGLE_LINE_TOP: {
4212 bool lineSet = !tabular.topLine(cur.idx(), flag);
4213 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4214 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4216 tabular.getCellNumber(i, j),
4221 case Tabular::M_TOGGLE_LINE_BOTTOM:
4223 case Tabular::TOGGLE_LINE_BOTTOM: {
4224 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4225 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4226 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4227 tabular.setBottomLine(
4228 tabular.getCellNumber(i, j),
4234 case Tabular::M_TOGGLE_LINE_LEFT:
4236 case Tabular::TOGGLE_LINE_LEFT: {
4237 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4238 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4239 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.setLeftLine(
4241 tabular.getCellNumber(i,j),
4247 case Tabular::M_TOGGLE_LINE_RIGHT:
4249 case Tabular::TOGGLE_LINE_RIGHT: {
4250 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4251 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4252 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4253 tabular.setRightLine(
4254 tabular.getCellNumber(i,j),
4260 case Tabular::M_ALIGN_LEFT:
4261 case Tabular::M_ALIGN_RIGHT:
4262 case Tabular::M_ALIGN_CENTER:
4264 case Tabular::ALIGN_LEFT:
4265 case Tabular::ALIGN_RIGHT:
4266 case Tabular::ALIGN_CENTER:
4267 case Tabular::ALIGN_BLOCK:
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setAlignment(
4271 tabular.getCellNumber(i, j),
4276 case Tabular::M_VALIGN_TOP:
4277 case Tabular::M_VALIGN_BOTTOM:
4278 case Tabular::M_VALIGN_MIDDLE:
4280 case Tabular::VALIGN_TOP:
4281 case Tabular::VALIGN_BOTTOM:
4282 case Tabular::VALIGN_MIDDLE:
4283 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4284 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4285 tabular.setVAlignment(
4286 tabular.getCellNumber(i, j),
4290 case Tabular::MULTICOLUMN: {
4291 if (sel_row_start != sel_row_end) {
4292 // FIXME: Need I say it ? This is horrible.
4294 Alert::error(_("Error setting multicolumn"),
4295 _("You cannot set multicolumn vertically."));
4298 if (!cur.selection()) {
4299 // just multicol for one single cell
4300 // check whether we are completely in a multicol
4301 if (tabular.isMultiColumn(cur.idx()))
4302 tabular.unsetMultiColumn(cur.idx());
4304 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4307 // we have a selection so this means we just add all this
4308 // cells to form a multicolumn cell
4309 idx_type const s_start = cur.selBegin().idx();
4310 idx_type const s_end = cur.selEnd().idx();
4311 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4312 cur.idx() = s_start;
4315 cur.selection() = false;
4319 case Tabular::SET_ALL_LINES:
4321 case Tabular::UNSET_ALL_LINES:
4322 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4323 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4324 tabular.setAllLines(
4325 tabular.getCellNumber(i,j), setLines);
4328 case Tabular::SET_LONGTABULAR:
4329 tabular.setLongTabular(true);
4332 case Tabular::UNSET_LONGTABULAR:
4333 tabular.setLongTabular(false);
4336 case Tabular::SET_ROTATE_TABULAR:
4337 tabular.setRotateTabular(true);
4340 case Tabular::UNSET_ROTATE_TABULAR:
4341 tabular.setRotateTabular(false);
4344 case Tabular::TOGGLE_ROTATE_TABULAR:
4345 tabular.setRotateTabular(!tabular.getRotateTabular());
4348 case Tabular::SET_ROTATE_CELL:
4349 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4350 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4351 tabular.setRotateCell(
4352 tabular.getCellNumber(i, j), true);
4355 case Tabular::UNSET_ROTATE_CELL:
4356 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4357 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4358 tabular.setRotateCell(
4359 tabular.getCellNumber(i, j), false);
4362 case Tabular::TOGGLE_ROTATE_CELL:
4364 bool oneNotRotated = oneCellHasRotationState(false,
4365 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4367 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4368 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4369 tabular.setRotateCell(tabular.getCellNumber(i, j),
4374 case Tabular::SET_USEBOX: {
4375 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4376 if (val == tabular.getUsebox(cur.idx()))
4377 val = Tabular::BOX_NONE;
4378 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4379 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4380 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4384 case Tabular::UNSET_LTFIRSTHEAD:
4386 case Tabular::SET_LTFIRSTHEAD:
4387 tabular.getRowOfLTFirstHead(row, ltt);
4388 checkLongtableSpecial(ltt, value, flag);
4389 tabular.setLTHead(row, flag, ltt, true);
4392 case Tabular::UNSET_LTHEAD:
4394 case Tabular::SET_LTHEAD:
4395 tabular.getRowOfLTHead(row, ltt);
4396 checkLongtableSpecial(ltt, value, flag);
4397 tabular.setLTHead(row, flag, ltt, false);
4400 case Tabular::UNSET_LTFOOT:
4402 case Tabular::SET_LTFOOT:
4403 tabular.getRowOfLTFoot(row, ltt);
4404 checkLongtableSpecial(ltt, value, flag);
4405 tabular.setLTFoot(row, flag, ltt, false);
4408 case Tabular::UNSET_LTLASTFOOT:
4410 case Tabular::SET_LTLASTFOOT:
4411 tabular.getRowOfLTLastFoot(row, ltt);
4412 checkLongtableSpecial(ltt, value, flag);
4413 tabular.setLTFoot(row, flag, ltt, true);
4416 case Tabular::SET_LTNEWPAGE:
4417 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4420 case Tabular::SET_BOOKTABS:
4421 tabular.setBookTabs(true);
4424 case Tabular::UNSET_BOOKTABS:
4425 tabular.setBookTabs(false);
4428 case Tabular::SET_TOP_SPACE: {
4430 if (value == "default")
4431 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4432 tabular.row_info[i].top_space_default = true;
4433 else if (isValidLength(value, &len))
4434 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4435 tabular.row_info[i].top_space_default = false;
4436 tabular.row_info[i].top_space = len;
4439 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4440 tabular.row_info[i].top_space_default = false;
4441 tabular.row_info[i].top_space = len;
4446 case Tabular::SET_BOTTOM_SPACE: {
4448 if (value == "default")
4449 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4450 tabular.row_info[i].bottom_space_default = true;
4451 else if (isValidLength(value, &len))
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4453 tabular.row_info[i].bottom_space_default = false;
4454 tabular.row_info[i].bottom_space = len;
4457 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4458 tabular.row_info[i].bottom_space_default = false;
4459 tabular.row_info[i].bottom_space = len;
4464 case Tabular::SET_INTERLINE_SPACE: {
4466 if (value == "default")
4467 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4468 tabular.row_info[i].interline_space_default = true;
4469 else if (isValidLength(value, &len))
4470 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4471 tabular.row_info[i].interline_space_default = false;
4472 tabular.row_info[i].interline_space = len;
4475 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4476 tabular.row_info[i].interline_space_default = false;
4477 tabular.row_info[i].interline_space = len;
4482 // dummy stuff just to avoid warnings
4483 case Tabular::LAST_ACTION:
4489 bool InsetTabular::showInsetDialog(BufferView * bv) const
4491 InsetTabularMailer(*this).showDialog(bv);
4496 void InsetTabular::openLayoutDialog(BufferView * bv) const
4498 InsetTabularMailer(*this).showDialog(bv);
4502 bool InsetTabular::copySelection(Cursor & cur)
4504 if (!cur.selection())
4509 getSelection(cur, rs, re, cs, ce);
4511 paste_tabular.reset(new Tabular(tabular));
4513 for (row_type i = 0; i < rs; ++i)
4514 paste_tabular->deleteRow(0);
4516 row_type const rows = re - rs + 1;
4517 while (paste_tabular->rows() > rows)
4518 paste_tabular->deleteRow(rows);
4520 paste_tabular->setTopLine(0, true, true);
4521 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4524 for (col_type i = 0; i < cs; ++i)
4525 paste_tabular->deleteColumn(0);
4527 col_type const columns = ce - cs + 1;
4528 while (paste_tabular->columns() > columns)
4529 paste_tabular->deleteColumn(columns);
4531 paste_tabular->setLeftLine(0, true, true);
4532 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4535 odocstringstream os;
4536 OutputParams const runparams(0);
4537 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4538 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4539 cap::copySelection(cur, os.str());
4541 // mark tabular stack dirty
4542 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4543 // when we (hopefully) have a one-for-all paste mechanism.
4544 // This must be called after cap::copySelection.
4545 dirtyTabularStack(true);
4551 bool InsetTabular::pasteClipboard(Cursor & cur)
4555 col_type const actcol = tabular.column_of_cell(cur.idx());
4556 row_type const actrow = tabular.row_of_cell(cur.idx());
4557 for (row_type r1 = 0, r2 = actrow;
4558 r1 < paste_tabular->rows() && r2 < tabular.rows();
4560 for (col_type c1 = 0, c2 = actcol;
4561 c1 < paste_tabular->columns() && c2 < tabular.columns();
4563 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4564 tabular.isPartOfMultiColumn(r2, c2))
4566 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4570 if (tabular.isPartOfMultiColumn(r2, c2)) {
4574 shared_ptr<InsetText> inset(
4575 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4576 tabular.setCellInset(r2, c2, inset);
4577 // FIXME: change tracking (MG)
4578 inset->setChange(Change(cur.buffer().params().trackChanges ?
4579 Change::INSERTED : Change::UNCHANGED));
4587 void InsetTabular::cutSelection(Cursor & cur)
4589 if (!cur.selection())
4594 getSelection(cur, rs, re, cs, ce);
4595 for (row_type i = rs; i <= re; ++i) {
4596 for (col_type j = cs; j <= ce; ++j) {
4597 shared_ptr<InsetText> t
4598 = cell(tabular.getCellNumber(i, j));
4599 if (cur.buffer().params().trackChanges)
4600 // FIXME: Change tracking (MG)
4601 t->setChange(Change(Change::DELETED));
4607 // cursor position might be invalid now
4608 if (cur.pit() > cur.lastpit())
4609 cur.pit() = cur.lastpit();
4610 if (cur.pos() > cur.lastpos())
4611 cur.pos() = cur.lastpos();
4612 cur.clearSelection();
4616 bool InsetTabular::isRightToLeft(Cursor & cur) const
4618 BOOST_ASSERT(cur.depth() > 1);
4619 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4620 pos_type const parentpos = cur[cur.depth() - 2].pos();
4621 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4622 parentpos).language()->rightToLeft();
4626 void InsetTabular::getSelection(Cursor & cur,
4627 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4629 CursorSlice const & beg = cur.selBegin();
4630 CursorSlice const & end = cur.selEnd();
4631 cs = tabular.column_of_cell(beg.idx());
4632 ce = tabular.column_of_cell(end.idx());
4635 cs = tabular.column_of_cell(end.idx());
4637 ce = tabular.right_column_of_cell(end.idx());
4640 rs = tabular.row_of_cell(beg.idx());
4641 re = tabular.row_of_cell(end.idx());
4647 Text * InsetTabular::getText(int idx) const
4649 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4653 void InsetTabular::setChange(Change const & change)
4655 for (idx_type idx = 0; idx < nargs(); ++idx)
4656 cell(idx)->setChange(change);
4660 void InsetTabular::acceptChanges(BufferParams const & bparams)
4662 for (idx_type idx = 0; idx < nargs(); ++idx)
4663 cell(idx)->acceptChanges(bparams);
4667 void InsetTabular::rejectChanges(BufferParams const & bparams)
4669 for (idx_type idx = 0; idx < nargs(); ++idx)
4670 cell(idx)->rejectChanges(bparams);
4674 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4676 return tabular.getPWidth(cell).zero();
4680 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4683 if (buf.length() <= 0)
4686 Buffer const & buffer = *bv.buffer();
4690 col_type maxCols = 1;
4691 docstring::size_type const len = buf.length();
4692 docstring::size_type p = 0;
4695 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4703 maxCols = max(cols, maxCols);
4709 maxCols = max(cols, maxCols);
4715 paste_tabular.reset(
4716 new Tabular(buffer.params(), rows, maxCols));
4717 loctab = paste_tabular.get();
4719 dirtyTabularStack(true);
4722 cell = bv.cursor().idx();
4723 ocol = tabular.column_of_cell(cell);
4724 row = tabular.row_of_cell(cell);
4727 docstring::size_type op = 0;
4728 idx_type const cells = loctab->getNumberOfCells();
4731 rows = loctab->rows();
4732 col_type const columns = loctab->columns();
4734 while (cell < cells && p < len && row < rows &&
4735 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4741 // we can only set this if we are not too far right
4742 if (cols < columns) {
4743 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4744 Paragraph & par = inset->text_.getPar(0);
4745 Font const font = inset->text_.getFont(buffer, par, 0);
4746 inset->setText(buf.substr(op, p - op), font,
4747 buffer.params().trackChanges);
4753 // we can only set this if we are not too far right
4754 if (cols < columns) {
4755 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4756 Paragraph & par = inset->text_.getPar(0);
4757 Font const font = inset->text_.getFont(buffer, par, 0);
4758 inset->setText(buf.substr(op, p - op), font,
4759 buffer.params().trackChanges);
4764 cell = loctab->getCellNumber(row, cols);
4770 // check for the last cell if there is no trailing '\n'
4771 if (cell < cells && op < len) {
4772 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4773 Paragraph & par = inset->text_.getPar(0);
4774 Font const font = inset->text_.getFont(buffer, par, 0);
4775 inset->setText(buf.substr(op, len - op), font,
4776 buffer.params().trackChanges);
4782 void InsetTabular::addPreview(PreviewLoader & loader) const
4784 row_type const rows = tabular.rows();
4785 col_type const columns = tabular.columns();
4786 for (row_type i = 0; i < rows; ++i) {
4787 for (col_type j = 0; j < columns; ++j)
4788 tabular.getCellInset(i, j)->addPreview(loader);
4793 bool InsetTabular::tablemode(Cursor & cur) const
4795 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4801 string const InsetTabularMailer::name_("tabular");
4803 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4804 : inset_(const_cast<InsetTabular &>(inset))
4808 string const InsetTabularMailer::inset2string(Buffer const &) const
4810 return params2string(inset_);
4814 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4816 istringstream data(in);
4818 lex.setStream(data);
4825 if (!lex || token != name_)
4826 return print_mailer_error("InsetTabularMailer", in, 1,
4829 // This is part of the inset proper that is usually swallowed
4830 // by Buffer::readInset
4832 if (!lex || token != "Tabular")
4833 return print_mailer_error("InsetTabularMailer", in, 2,
4836 Buffer const & buffer = inset.buffer();
4837 inset.read(buffer, lex);
4841 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4844 data << name_ << ' ';
4845 inset.write(inset.buffer(), data);
4846 data << "\\end_inset\n";