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)
770 rowofcell.resize(numberofcells);
771 columnofcell.resize(numberofcells);
776 c < numberofcells && row < rows_ && column < columns_;) {
778 columnofcell[c] = column;
782 } while (column < columns_ &&
783 cell_info[row][column].multicolumn
784 == Tabular::CELL_PART_OF_MULTICOLUMN);
786 if (column == columns_) {
792 for (row_type row = 0; row < rows_; ++row) {
793 for (col_type column = 0; column < columns_; ++column) {
794 if (isPartOfMultiColumn(row,column))
796 cell_info[row][column].inset->setAutoBreakRows(
797 !getPWidth(getCellNumber(row, column)).zero());
803 Tabular::idx_type Tabular::getNumberOfCells() const
805 return numberofcells;
809 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
811 row_type const row = row_of_cell(cell);
813 for (col_type i = 0; i < columns_; ++i)
814 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
820 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
822 if (!wholerow && isMultiColumn(cell) &&
823 !(use_booktabs && row_of_cell(cell) == 0))
824 return cellinfo_of_cell(cell).top_line;
825 return row_info[row_of_cell(cell)].top_line;
829 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
831 if (!wholerow && isMultiColumn(cell) &&
832 !(use_booktabs && isLastRow(cell)))
833 return cellinfo_of_cell(cell).bottom_line;
834 return row_info[row_of_cell(cell)].bottom_line;
838 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
842 if (!wholecolumn && isMultiColumn(cell) &&
843 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
844 return cellinfo_of_cell(cell).left_line;
845 return column_info[column_of_cell(cell)].left_line;
849 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
853 if (!wholecolumn && isMultiColumn(cell) &&
854 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
855 return cellinfo_of_cell(cell).right_line;
856 return column_info[right_column_of_cell(cell)].right_line;
860 bool Tabular::topAlreadyDrawn(idx_type cell) const
862 row_type row = row_of_cell(cell);
863 if (row > 0 && !getAdditionalHeight(row)) {
864 col_type column = column_of_cell(cell);
867 && cell_info[row][column].multicolumn
868 == Tabular::CELL_PART_OF_MULTICOLUMN)
870 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
871 return row_info[row].bottom_line;
873 return cell_info[row][column].bottom_line;
879 bool Tabular::leftAlreadyDrawn(idx_type cell) const
881 col_type column = column_of_cell(cell);
883 row_type row = row_of_cell(cell);
885 (cell_info[row][column].multicolumn ==
886 Tabular::CELL_PART_OF_MULTICOLUMN));
887 if (getAdditionalWidth(cell_info[row][column].cellno))
889 return rightLine(cell_info[row][column].cellno);
895 bool Tabular::isLastRow(idx_type cell) const
897 return row_of_cell(cell) == rows_ - 1;
901 int Tabular::getAdditionalHeight(row_type row) const
903 if (!row || row >= rows_)
909 for (col_type column = 0; column < columns_ && bottom; ++column) {
910 switch (cell_info[row - 1][column].multicolumn) {
911 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
912 bottom = cell_info[row - 1][column].bottom_line;
914 case Tabular::CELL_NORMAL:
915 bottom = row_info[row - 1].bottom_line;
918 for (col_type column = 0; column < columns_ && top; ++column) {
919 switch (cell_info[row][column].multicolumn) {
920 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
921 top = cell_info[row][column].top_line;
923 case Tabular::CELL_NORMAL:
924 top = row_info[row].top_line;
927 int const interline_space = row_info[row - 1].interline_space_default ?
929 row_info[row - 1].interline_space.inPixels(width_of_tabular);
931 return interline_space + WIDTH_OF_LINE;
932 return interline_space;
936 int Tabular::getAdditionalWidth(idx_type cell) const
938 // internally already set in setWidthOfCell
939 // used to get it back in text.cpp
940 col_type const col = right_column_of_cell(cell);
941 row_type const row = row_of_cell(cell);
942 if (col < columns_ - 1 && rightLine(cell) &&
943 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
945 return WIDTH_OF_LINE;
951 // returns the maximum over all rows
952 int Tabular::getWidthOfColumn(idx_type cell) const
954 col_type const column1 = column_of_cell(cell);
955 col_type const column2 = right_column_of_cell(cell);
957 for (col_type i = column1; i <= column2; ++i)
958 result += column_info[i].width_of_column;
963 int Tabular::getWidthOfTabular() const
965 return width_of_tabular;
969 // returns true if a complete update is necessary, otherwise false
970 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
972 if (!isMultiColumn(cell))
975 row_type const row = row_of_cell(cell);
976 col_type const column1 = column_of_cell(cell);
977 col_type const column2 = right_column_of_cell(cell);
978 int const old_val = cell_info[row][column2].width_of_cell;
980 // first set columns to 0 so we can calculate the right width
981 for (col_type i = column1; i <= column2; ++i) {
982 cell_info[row][i].width_of_cell = 0;
984 // set the width to MAX_WIDTH until width > 0
985 int width = new_width + 2 * WIDTH_OF_LINE;
986 col_type i = column1;
987 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
988 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
989 width -= column_info[i].width_of_column;
992 cell_info[row][i].width_of_cell = width;
994 if (old_val != cell_info[row][column2].width_of_cell) {
995 // in this case we have to recalculate all multicolumn cells which
996 // have this column as one of theirs but not as last one
997 calculate_width_of_column_NMC(i);
998 recalculateMulticolumnsOfColumn(i);
999 calculate_width_of_column(i);
1005 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1007 // the last column does not have to be recalculated because all
1008 // multicolumns will have here there last multicolumn cell which
1009 // always will have the whole rest of the width of the cell.
1010 if (columns_ < 2 || column > (columns_ - 2))
1012 for (row_type row = 0; row < rows_; ++row) {
1013 int mc = cell_info[row][column].multicolumn;
1014 int nmc = cell_info[row][column+1].multicolumn;
1015 // we only have to update multicolumns which do not have this
1016 // column as their last column!
1017 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1018 (mc == CELL_PART_OF_MULTICOLUMN &&
1019 nmc == CELL_PART_OF_MULTICOLUMN))
1021 idx_type const cellno = cell_info[row][column].cellno;
1022 setWidthOfMulticolCell(cellno,
1023 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1029 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1031 row_type const row = row_of_cell(cell);
1032 col_type const column1 = column_of_cell(cell);
1037 if (rightLine(cell_info[row][column1].cellno, true) &&
1038 column1 < columns_ - 1 &&
1039 leftLine(cell_info[row][column1+1].cellno, true))
1041 add_width = WIDTH_OF_LINE;
1044 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1047 if (isMultiColumnReal(cell)) {
1048 tmp = setWidthOfMulticolCell(cell, new_width);
1050 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1051 cell_info[row][column1].width_of_cell = width;
1052 tmp = calculate_width_of_column_NMC(column1);
1054 recalculateMulticolumnsOfColumn(column1);
1057 for (col_type i = 0; i < columns_; ++i)
1058 calculate_width_of_column(i);
1059 calculate_width_of_tabular();
1064 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1067 if (!isMultiColumn(cell) || onlycolumn)
1068 column_info[column_of_cell(cell)].alignment = align;
1070 cellinfo_of_cell(cell).alignment = align;
1074 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1077 if (!isMultiColumn(cell) || onlycolumn)
1078 column_info[column_of_cell(cell)].valignment = align;
1080 cellinfo_of_cell(cell).valignment = align;
1087 * Allow line and paragraph breaks for fixed width cells or disallow them,
1088 * merge cell paragraphs and reset layout to standard for variable width
1091 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1093 inset->setAutoBreakRows(fixedWidth);
1097 // merge all paragraphs to one
1098 BufferParams const & bp = cur.bv().buffer()->params();
1099 while (inset->paragraphs().size() > 1)
1100 mergeParagraph(bp, inset->paragraphs(), 0);
1104 // undo information has already been recorded
1105 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1106 bp.getTextClass().defaultLayoutName());
1113 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1114 Length const & width)
1116 col_type const j = column_of_cell(cell);
1118 column_info[j].p_width = width;
1119 for (row_type i = 0; i < rows_; ++i) {
1120 idx_type const cell = getCellNumber(i, j);
1121 // because of multicolumns
1122 toggleFixedWidth(cur, getCellInset(cell).get(),
1123 !getPWidth(cell).zero());
1125 // cur paragraph can become invalid after paragraphs were merged
1126 if (cur.pit() > cur.lastpit())
1127 cur.pit() = cur.lastpit();
1128 // cur position can become invalid after newlines were removed
1129 if (cur.pos() > cur.lastpos())
1130 cur.pos() = cur.lastpos();
1134 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1135 Length const & width)
1137 if (!isMultiColumn(cell))
1140 cellinfo_of_cell(cell).p_width = width;
1141 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1142 // cur paragraph can become invalid after paragraphs were merged
1143 if (cur.pit() > cur.lastpit())
1144 cur.pit() = cur.lastpit();
1145 // cur position can become invalid after newlines were removed
1146 if (cur.pos() > cur.lastpos())
1147 cur.pos() = cur.lastpos();
1152 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1153 Tabular::Feature what)
1155 if (what == SET_SPECIAL_MULTI)
1156 cellinfo_of_cell(cell).align_special = special;
1158 column_info[column_of_cell(cell)].align_special = special;
1162 void Tabular::setAllLines(idx_type cell, bool line)
1164 setTopLine(cell, line);
1165 setBottomLine(cell, line);
1166 setRightLine(cell, line);
1167 setLeftLine(cell, line);
1171 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1173 row_type const row = row_of_cell(cell);
1174 if (wholerow || !isMultiColumn(cell))
1175 row_info[row].top_line = line;
1177 cellinfo_of_cell(cell).top_line = line;
1181 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1183 if (wholerow || !isMultiColumn(cell))
1184 row_info[row_of_cell(cell)].bottom_line = line;
1186 cellinfo_of_cell(cell).bottom_line = line;
1190 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1192 if (wholecolumn || !isMultiColumn(cell))
1193 column_info[column_of_cell(cell)].left_line = line;
1195 cellinfo_of_cell(cell).left_line = line;
1199 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1201 if (wholecolumn || !isMultiColumn(cell))
1202 column_info[right_column_of_cell(cell)].right_line = line;
1204 cellinfo_of_cell(cell).right_line = line;
1208 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1210 if (!onlycolumn && isMultiColumn(cell))
1211 return cellinfo_of_cell(cell).alignment;
1212 return column_info[column_of_cell(cell)].alignment;
1217 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1219 if (!onlycolumn && isMultiColumn(cell))
1220 return cellinfo_of_cell(cell).valignment;
1221 return column_info[column_of_cell(cell)].valignment;
1225 Length const Tabular::getPWidth(idx_type cell) const
1227 if (isMultiColumn(cell))
1228 return cellinfo_of_cell(cell).p_width;
1229 return column_info[column_of_cell(cell)].p_width;
1233 Length const Tabular::getColumnPWidth(idx_type cell) const
1235 return column_info[column_of_cell(cell)].p_width;
1239 Length const Tabular::getMColumnPWidth(idx_type cell) const
1241 if (isMultiColumn(cell))
1242 return cellinfo_of_cell(cell).p_width;
1247 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1249 if (what == SET_SPECIAL_MULTI)
1250 return cellinfo_of_cell(cell).align_special;
1251 return column_info[column_of_cell(cell)].align_special;
1255 int Tabular::getWidthOfCell(idx_type cell) const
1257 row_type const row = row_of_cell(cell);
1258 col_type const column1 = column_of_cell(cell);
1259 col_type const column2 = right_column_of_cell(cell);
1261 for (col_type i = column1; i <= column2; ++i)
1262 result += cell_info[row][i].width_of_cell;
1267 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1271 switch (getAlignment(cell)) {
1272 case LYX_ALIGN_CENTER:
1273 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1275 case LYX_ALIGN_RIGHT:
1276 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1277 // + getAdditionalWidth(cell);
1280 // LYX_ALIGN_LEFT: nothing :-)
1284 // the LaTeX Way :-(
1290 bool Tabular::isFirstCellInRow(idx_type cell) const
1292 return column_of_cell(cell) == 0;
1296 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1298 if (row > rows_ - 1)
1300 return cell_info[row][0].cellno;
1304 bool Tabular::isLastCellInRow(idx_type cell) const
1306 return right_column_of_cell(cell) == columns_ - 1;
1310 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1312 if (row > rows_ - 1)
1314 return cell_info[row][columns_-1].cellno;
1318 void Tabular::calculate_width_of_column(col_type column)
1321 for (row_type i = 0; i < rows_; ++i)
1322 maximum = max(cell_info[i][column].width_of_cell, maximum);
1323 column_info[column].width_of_column = maximum;
1328 // Calculate the columns regarding ONLY the normal cells and if this
1329 // column is inside a multicolumn cell then use it only if its the last
1330 // column of this multicolumn cell as this gives an added width to the
1331 // column, all the rest should be adapted!
1333 bool Tabular::calculate_width_of_column_NMC(col_type column)
1335 int const old_column_width = column_info[column].width_of_column;
1337 for (row_type i = 0; i < rows_; ++i) {
1338 idx_type cell = getCellNumber(i, column);
1339 bool ismulti = isMultiColumnReal(cell);
1340 if ((!ismulti || column == right_column_of_cell(cell)) &&
1341 cell_info[i][column].width_of_cell > max)
1343 max = cell_info[i][column].width_of_cell;
1346 column_info[column].width_of_column = max;
1347 return column_info[column].width_of_column != old_column_width;
1351 void Tabular::calculate_width_of_tabular()
1353 width_of_tabular = 0;
1354 for (col_type i = 0; i < columns_; ++i)
1355 width_of_tabular += column_info[i].width_of_column;
1359 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1361 if (cell >= numberofcells)
1365 return rowofcell[cell];
1369 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1371 if (cell >= numberofcells)
1372 return columns_ - 1;
1375 return columnofcell[cell];
1379 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1381 row_type const row = row_of_cell(cell);
1382 col_type column = column_of_cell(cell);
1383 while (column < columns_ - 1 &&
1384 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1390 void Tabular::write(Buffer const & buf, ostream & os) const
1394 << write_attribute("version", 3)
1395 << write_attribute("rows", rows_)
1396 << write_attribute("columns", columns_)
1398 // global longtable options
1400 << write_attribute("rotate", rotate)
1401 << write_attribute("booktabs", use_booktabs)
1402 << write_attribute("islongtable", is_long_tabular)
1403 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1404 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1405 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1406 << write_attribute("headTopDL", endhead.topDL)
1407 << write_attribute("headBottomDL", endhead.bottomDL)
1408 << write_attribute("footTopDL", endfoot.topDL)
1409 << write_attribute("footBottomDL", endfoot.bottomDL)
1410 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1411 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1412 << write_attribute("lastFootEmpty", endlastfoot.empty)
1414 for (col_type j = 0; j < columns_; ++j) {
1416 << write_attribute("alignment", column_info[j].alignment)
1417 << write_attribute("valignment", column_info[j].valignment)
1418 << write_attribute("leftline", column_info[j].left_line)
1419 << write_attribute("rightline", column_info[j].right_line)
1420 << write_attribute("width", column_info[j].p_width.asString())
1421 << write_attribute("special", column_info[j].align_special)
1424 for (row_type i = 0; i < rows_; ++i) {
1426 << write_attribute("topline", row_info[i].top_line)
1427 << write_attribute("bottomline", row_info[i].bottom_line);
1428 static const string def("default");
1429 if (row_info[i].top_space_default)
1430 os << write_attribute("topspace", def);
1432 os << write_attribute("topspace", row_info[i].top_space);
1433 if (row_info[i].bottom_space_default)
1434 os << write_attribute("bottomspace", def);
1436 os << write_attribute("bottomspace", row_info[i].bottom_space);
1437 if (row_info[i].interline_space_default)
1438 os << write_attribute("interlinespace", def);
1440 os << write_attribute("interlinespace", row_info[i].interline_space);
1441 os << write_attribute("endhead", row_info[i].endhead)
1442 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1443 << write_attribute("endfoot", row_info[i].endfoot)
1444 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1445 << write_attribute("newpage", row_info[i].newpage)
1447 for (col_type j = 0; j < columns_; ++j) {
1449 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1450 << write_attribute("alignment", cell_info[i][j].alignment)
1451 << write_attribute("valignment", cell_info[i][j].valignment)
1452 << write_attribute("topline", cell_info[i][j].top_line)
1453 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1454 << write_attribute("leftline", cell_info[i][j].left_line)
1455 << write_attribute("rightline", cell_info[i][j].right_line)
1456 << write_attribute("rotate", cell_info[i][j].rotate)
1457 << write_attribute("usebox", cell_info[i][j].usebox)
1458 << write_attribute("width", cell_info[i][j].p_width)
1459 << write_attribute("special", cell_info[i][j].align_special)
1461 os << "\\begin_inset ";
1462 cell_info[i][j].inset->write(buf, os);
1463 os << "\n\\end_inset\n"
1468 os << "</lyxtabular>\n";
1472 void Tabular::read(Buffer const & buf, Lexer & lex)
1475 istream & is = lex.getStream();
1477 l_getline(is, line);
1478 if (!prefixIs(line, "<lyxtabular ")
1479 && !prefixIs(line, "<Tabular ")) {
1480 BOOST_ASSERT(false);
1485 if (!getTokenValue(line, "version", version))
1487 BOOST_ASSERT(version >= 2);
1490 if (!getTokenValue(line, "rows", rows_arg))
1493 if (!getTokenValue(line, "columns", columns_arg))
1495 init(buf.params(), rows_arg, columns_arg);
1496 l_getline(is, line);
1497 if (!prefixIs(line, "<features")) {
1498 lyxerr << "Wrong tabular format (expected <features ...> got"
1499 << line << ')' << endl;
1502 getTokenValue(line, "rotate", rotate);
1503 getTokenValue(line, "booktabs", use_booktabs);
1504 getTokenValue(line, "islongtable", is_long_tabular);
1505 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1506 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1507 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1508 getTokenValue(line, "headTopDL", endhead.topDL);
1509 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1510 getTokenValue(line, "footTopDL", endfoot.topDL);
1511 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1512 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1513 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1514 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1516 for (col_type j = 0; j < columns_; ++j) {
1518 if (!prefixIs(line,"<column")) {
1519 lyxerr << "Wrong tabular format (expected <column ...> got"
1520 << line << ')' << endl;
1523 getTokenValue(line, "alignment", column_info[j].alignment);
1524 getTokenValue(line, "valignment", column_info[j].valignment);
1525 getTokenValue(line, "leftline", column_info[j].left_line);
1526 getTokenValue(line, "rightline", column_info[j].right_line);
1527 getTokenValue(line, "width", column_info[j].p_width);
1528 getTokenValue(line, "special", column_info[j].align_special);
1531 for (row_type i = 0; i < rows_; ++i) {
1532 l_getline(is, line);
1533 if (!prefixIs(line, "<row")) {
1534 lyxerr << "Wrong tabular format (expected <row ...> got"
1535 << line << ')' << endl;
1538 getTokenValue(line, "topline", row_info[i].top_line);
1539 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1540 getTokenValue(line, "topspace", row_info[i].top_space,
1541 row_info[i].top_space_default);
1542 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1543 row_info[i].bottom_space_default);
1544 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1545 row_info[i].interline_space_default);
1546 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1547 getTokenValue(line, "endhead", row_info[i].endhead);
1548 getTokenValue(line, "endfoot", row_info[i].endfoot);
1549 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1550 getTokenValue(line, "newpage", row_info[i].newpage);
1551 for (col_type j = 0; j < columns_; ++j) {
1552 l_getline(is, line);
1553 if (!prefixIs(line, "<cell")) {
1554 lyxerr << "Wrong tabular format (expected <cell ...> got"
1555 << line << ')' << endl;
1558 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1559 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1560 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1561 getTokenValue(line, "topline", cell_info[i][j].top_line);
1562 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1563 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1564 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1565 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1566 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1567 getTokenValue(line, "width", cell_info[i][j].p_width);
1568 getTokenValue(line, "special", cell_info[i][j].align_special);
1569 l_getline(is, line);
1570 if (prefixIs(line, "\\begin_inset")) {
1571 cell_info[i][j].inset->read(buf, lex);
1572 l_getline(is, line);
1574 if (!prefixIs(line, "</cell>")) {
1575 lyxerr << "Wrong tabular format (expected </cell> got"
1576 << line << ')' << endl;
1580 l_getline(is, line);
1581 if (!prefixIs(line, "</row>")) {
1582 lyxerr << "Wrong tabular format (expected </row> got"
1583 << line << ')' << endl;
1587 while (!prefixIs(line, "</lyxtabular>")) {
1588 l_getline(is, line);
1590 set_row_column_number_info();
1594 bool Tabular::isMultiColumn(idx_type cell) const
1596 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1600 bool Tabular::isMultiColumnReal(idx_type cell) const
1602 return column_of_cell(cell) != right_column_of_cell(cell) &&
1603 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1607 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1609 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1613 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1616 cellstruct & cs = cellinfo_of_cell(cell);
1617 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1618 cs.alignment = column_info[column_of_cell(cell)].alignment;
1619 cs.top_line = row_info[row_of_cell(cell)].top_line;
1620 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1621 cs.left_line = column_info[column_of_cell(cell)].left_line;
1622 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1623 for (idx_type i = 1; i < number; ++i) {
1624 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1625 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1626 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1629 set_row_column_number_info();
1633 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1635 row_type const row = row_of_cell(cell);
1636 col_type column = column_of_cell(cell);
1637 idx_type result = 1;
1639 while (column < columns_ &&
1640 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1649 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1651 row_type const row = row_of_cell(cell);
1652 col_type column = column_of_cell(cell);
1654 idx_type result = 0;
1656 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1657 cell_info[row][column].multicolumn = CELL_NORMAL;
1659 while (column < columns_ &&
1660 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1662 cell_info[row][column].multicolumn = CELL_NORMAL;
1667 set_row_column_number_info();
1672 void Tabular::setBookTabs(bool what)
1674 use_booktabs = what;
1678 bool Tabular::useBookTabs() const
1680 return use_booktabs;
1684 void Tabular::setLongTabular(bool what)
1686 is_long_tabular = what;
1690 bool Tabular::isLongTabular() const
1692 return is_long_tabular;
1696 void Tabular::setRotateTabular(bool flag)
1702 bool Tabular::getRotateTabular() const
1708 void Tabular::setRotateCell(idx_type cell, bool flag)
1710 cellinfo_of_cell(cell).rotate = flag;
1714 bool Tabular::getRotateCell(idx_type cell) const
1716 return cellinfo_of_cell(cell).rotate;
1720 bool Tabular::needRotating() const
1724 for (row_type i = 0; i < rows_; ++i)
1725 for (col_type j = 0; j < columns_; ++j)
1726 if (cell_info[i][j].rotate)
1732 bool Tabular::isLastCell(idx_type cell) const
1734 if (cell + 1 < numberofcells)
1740 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1742 if (row_of_cell(cell) > 0)
1743 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1748 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1750 if (row_of_cell(cell) + 1 < rows_)
1751 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1756 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1758 if (row_of_cell(cell) == 0)
1760 if (!isMultiColumn(cell))
1761 return getCellAbove(cell);
1762 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1766 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1768 if (row_of_cell(cell) + 1 >= rows_)
1770 if (!isMultiColumn(cell))
1771 return getCellBelow(cell);
1772 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1776 Tabular::idx_type Tabular::getCellNumber(row_type row,
1777 col_type column) const
1779 BOOST_ASSERT(column != npos && column < columns_ &&
1780 row != npos && row < rows_);
1781 return cell_info[row][column].cellno;
1785 void Tabular::setUsebox(idx_type cell, BoxType type)
1787 cellinfo_of_cell(cell).usebox = type;
1791 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1793 if (column_info[column_of_cell(cell)].p_width.zero() &&
1794 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1796 if (cellinfo_of_cell(cell).usebox > 1)
1797 return cellinfo_of_cell(cell).usebox;
1798 return useParbox(cell);
1803 // This are functions used for the longtable support
1805 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1811 row_info[row].endfirsthead = flag;
1815 row_info[row].endhead = flag;
1820 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1823 hd.set = haveLTHead();
1824 return row_info[row].endhead;
1828 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1831 hd.set = haveLTFirstHead();
1832 return row_info[row].endfirsthead;
1836 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1842 row_info[row].endlastfoot = flag;
1846 row_info[row].endfoot = flag;
1851 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1854 fd.set = haveLTFoot();
1855 return row_info[row].endfoot;
1859 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1862 fd.set = haveLTLastFoot();
1863 return row_info[row].endlastfoot;
1867 void Tabular::setLTNewPage(row_type row, bool what)
1869 row_info[row].newpage = what;
1873 bool Tabular::getLTNewPage(row_type row) const
1875 return row_info[row].newpage;
1879 bool Tabular::haveLTHead() const
1881 for (row_type i = 0; i < rows_; ++i)
1882 if (row_info[i].endhead)
1888 bool Tabular::haveLTFirstHead() const
1890 if (endfirsthead.empty)
1892 for (row_type i = 0; i < rows_; ++i)
1893 if (row_info[i].endfirsthead)
1899 bool Tabular::haveLTFoot() const
1901 for (row_type i = 0; i < rows_; ++i)
1902 if (row_info[i].endfoot)
1908 bool Tabular::haveLTLastFoot() const
1910 if (endlastfoot.empty)
1912 for (row_type i = 0; i < rows_; ++i)
1913 if (row_info[i].endlastfoot)
1919 // end longtable support functions
1921 void Tabular::setAscentOfRow(row_type row, int height)
1923 if (row >= rows_ || row_info[row].ascent_of_row == height)
1925 row_info[row].ascent_of_row = height;
1929 void Tabular::setDescentOfRow(row_type row, int height)
1931 if (row >= rows_ || row_info[row].descent_of_row == height)
1933 row_info[row].descent_of_row = height;
1937 int Tabular::getAscentOfRow(row_type row) const
1941 return row_info[row].ascent_of_row;
1945 int Tabular::getDescentOfRow(row_type row) const
1947 BOOST_ASSERT(row < rows_);
1948 return row_info[row].descent_of_row;
1952 int Tabular::getHeightOfTabular() const
1955 for (row_type row = 0; row < rows_; ++row)
1956 height += getAscentOfRow(row) + getDescentOfRow(row) +
1957 getAdditionalHeight(row);
1962 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1964 BOOST_ASSERT(row < rows_);
1965 BOOST_ASSERT(column < columns_);
1966 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1970 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1972 // FIXME: assert or return 0 as in TeXBottomHLine()?
1973 BOOST_ASSERT(row != npos);
1974 BOOST_ASSERT(row < rows_);
1976 idx_type const fcell = getFirstCellInRow(row);
1977 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1980 for (idx_type i = fcell; i < n; ++i) {
1984 if (use_booktabs && row == 0) {
1986 } else if (tmp == n - fcell) {
1987 os << (use_booktabs ? "\\midrule " : "\\hline ");
1989 for (idx_type i = fcell; i < n; ++i) {
1991 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1992 << column_of_cell(i) + 1
1994 << right_column_of_cell(i) + 1
2006 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2008 // FIXME: return 0 or assert as in TeXTopHLine()?
2009 if (row == npos || row >= rows_)
2012 idx_type const fcell = getFirstCellInRow(row);
2013 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2016 for (idx_type i = fcell; i < n; ++i) {
2020 if (use_booktabs && row == rows_ - 1) {
2021 os << "\\bottomrule";
2022 } else if (tmp == n - fcell) {
2023 os << (use_booktabs ? "\\midrule" : "\\hline");
2025 for (idx_type i = fcell; i < n; ++i) {
2026 if (bottomLine(i)) {
2027 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2028 << column_of_cell(i) + 1
2030 << right_column_of_cell(i) + 1
2042 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2046 if (getRotateCell(cell)) {
2047 os << "\\begin{sideways}\n";
2050 if (isMultiColumn(cell)) {
2051 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2052 if (leftLine(cell) &&
2053 (isFirstCellInRow(cell) ||
2054 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2055 !rightLine(cell - 1, true))))
2057 if (!cellinfo_of_cell(cell).align_special.empty()) {
2058 os << cellinfo_of_cell(cell).align_special;
2060 if (!getPWidth(cell).zero()) {
2061 switch (getVAlignment(cell)) {
2062 case LYX_VALIGN_TOP:
2065 case LYX_VALIGN_MIDDLE:
2068 case LYX_VALIGN_BOTTOM:
2073 << from_ascii(getPWidth(cell).asLatexString())
2076 switch (getAlignment(cell)) {
2077 case LYX_ALIGN_LEFT:
2080 case LYX_ALIGN_RIGHT:
2087 } // end if else !getPWidth
2088 } // end if else !cellinfo_of_cell
2089 if (rightLine(cell))
2091 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2096 if (getUsebox(cell) == BOX_PARBOX) {
2098 switch (getVAlignment(cell)) {
2099 case LYX_VALIGN_TOP:
2102 case LYX_VALIGN_MIDDLE:
2105 case LYX_VALIGN_BOTTOM:
2109 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2111 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2112 os << "\\begin{minipage}[";
2113 switch (getVAlignment(cell)) {
2114 case LYX_VALIGN_TOP:
2117 case LYX_VALIGN_MIDDLE:
2120 case LYX_VALIGN_BOTTOM:
2124 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2132 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2137 if (getUsebox(cell) == BOX_PARBOX)
2139 else if (getUsebox(cell) == BOX_MINIPAGE) {
2140 os << "%\n\\end{minipage}";
2143 if (isMultiColumn(cell)) {
2146 if (getRotateCell(cell)) {
2147 os << "%\n\\end{sideways}";
2154 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2155 OutputParams const & runparams) const
2157 if (!is_long_tabular)
2161 // output header info
2163 if (endhead.topDL) {
2167 for (row_type i = 0; i < rows_; ++i) {
2168 if (row_info[i].endhead) {
2169 ret += TeXRow(os, i, buf, runparams);
2172 if (endhead.bottomDL) {
2176 os << "\\endhead\n";
2178 if (endfirsthead.empty) {
2179 os << "\\endfirsthead\n";
2183 // output firstheader info
2184 if (haveLTFirstHead()) {
2185 if (endfirsthead.topDL) {
2189 for (row_type i = 0; i < rows_; ++i) {
2190 if (row_info[i].endfirsthead) {
2191 ret += TeXRow(os, i, buf, runparams);
2194 if (endfirsthead.bottomDL) {
2198 os << "\\endfirsthead\n";
2201 // output footer info
2203 if (endfoot.topDL) {
2207 for (row_type i = 0; i < rows_; ++i) {
2208 if (row_info[i].endfoot) {
2209 ret += TeXRow(os, i, buf, runparams);
2212 if (endfoot.bottomDL) {
2216 os << "\\endfoot\n";
2218 if (endlastfoot.empty) {
2219 os << "\\endlastfoot\n";
2223 // output lastfooter info
2224 if (haveLTLastFoot()) {
2225 if (endlastfoot.topDL) {
2229 for (row_type i = 0; i < rows_; ++i) {
2230 if (row_info[i].endlastfoot) {
2231 ret += TeXRow(os, i, buf, runparams);
2234 if (endlastfoot.bottomDL) {
2238 os << "\\endlastfoot\n";
2245 bool Tabular::isValidRow(row_type row) const
2247 if (!is_long_tabular)
2249 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2250 !row_info[row].endfoot && !row_info[row].endlastfoot;
2254 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2255 OutputParams const & runparams) const
2257 idx_type cell = getCellNumber(i, 0);
2258 int ret = TeXTopHLine(os, i);
2259 if (row_info[i].top_space_default) {
2261 os << "\\addlinespace\n";
2263 os << "\\noalign{\\vskip\\doublerulesep}\n";
2265 } else if(!row_info[i].top_space.zero()) {
2267 os << "\\addlinespace["
2268 << from_ascii(row_info[i].top_space.asLatexString())
2271 os << "\\noalign{\\vskip"
2272 << from_ascii(row_info[i].top_space.asLatexString())
2278 for (col_type j = 0; j < columns_; ++j) {
2279 if (isPartOfMultiColumn(i, j))
2281 ret += TeXCellPreamble(os, cell);
2282 shared_ptr<InsetText> inset = getCellInset(cell);
2284 Paragraph const & par = inset->paragraphs().front();
2285 bool rtl = par.isRightToLeftPar(buf.params())
2287 && getPWidth(cell).zero();
2290 if (par.getParLanguage(buf.params())->lang() ==
2293 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2295 // currently, remaning RTL languages are arabic_arabtex and hebrew
2299 ret += inset->latex(buf, os, runparams);
2303 ret += TeXCellPostamble(os, cell);
2304 if (!isLastCellInRow(cell)) { // not last cell in row
2309 os << "\\tabularnewline";
2310 if (row_info[i].bottom_space_default) {
2312 os << "\\addlinespace";
2314 os << "[\\doublerulesep]";
2315 } else if (!row_info[i].bottom_space.zero()) {
2317 os << "\\addlinespace";
2319 << from_ascii(row_info[i].bottom_space.asLatexString())
2324 ret += TeXBottomHLine(os, i);
2325 if (row_info[i].interline_space_default) {
2327 os << "\\addlinespace\n";
2329 os << "\\noalign{\\vskip\\doublerulesep}\n";
2331 } else if (!row_info[i].interline_space.zero()) {
2333 os << "\\addlinespace["
2334 << from_ascii(row_info[i].interline_space.asLatexString())
2337 os << "\\noalign{\\vskip"
2338 << from_ascii(row_info[i].interline_space.asLatexString())
2346 int Tabular::latex(Buffer const & buf, odocstream & os,
2347 OutputParams const & runparams) const
2351 //+---------------------------------------------------------------------
2352 //+ first the opening preamble +
2353 //+---------------------------------------------------------------------
2356 os << "\\begin{sideways}\n";
2359 if (is_long_tabular)
2360 os << "\\begin{longtable}{";
2362 os << "\\begin{tabular}{";
2363 for (col_type i = 0; i < columns_; ++i) {
2364 if (!use_booktabs && column_info[i].left_line)
2366 if (!column_info[i].align_special.empty()) {
2367 os << column_info[i].align_special;
2369 if (!column_info[i].p_width.zero()) {
2370 switch (column_info[i].alignment) {
2371 case LYX_ALIGN_LEFT:
2372 os << ">{\\raggedright}";
2374 case LYX_ALIGN_RIGHT:
2375 os << ">{\\raggedleft}";
2377 case LYX_ALIGN_CENTER:
2378 os << ">{\\centering}";
2380 case LYX_ALIGN_NONE:
2381 case LYX_ALIGN_BLOCK:
2382 case LYX_ALIGN_LAYOUT:
2383 case LYX_ALIGN_SPECIAL:
2387 switch (column_info[i].valignment) {
2388 case LYX_VALIGN_TOP:
2391 case LYX_VALIGN_MIDDLE:
2394 case LYX_VALIGN_BOTTOM:
2399 << from_ascii(column_info[i].p_width.asLatexString())
2402 switch (column_info[i].alignment) {
2403 case LYX_ALIGN_LEFT:
2406 case LYX_ALIGN_RIGHT:
2413 } // end if else !column_info[i].p_width
2414 } // end if else !column_info[i].align_special
2415 if (!use_booktabs && column_info[i].right_line)
2421 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2423 //+---------------------------------------------------------------------
2424 //+ the single row and columns (cells) +
2425 //+---------------------------------------------------------------------
2427 for (row_type i = 0; i < rows_; ++i) {
2428 if (isValidRow(i)) {
2429 ret += TeXRow(os, i, buf, runparams);
2430 if (is_long_tabular && row_info[i].newpage) {
2431 os << "\\newpage\n";
2437 //+---------------------------------------------------------------------
2438 //+ the closing of the tabular +
2439 //+---------------------------------------------------------------------
2441 if (is_long_tabular)
2442 os << "\\end{longtable}";
2444 os << "\\end{tabular}";
2446 os << "\n\\end{sideways}";
2454 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2455 OutputParams const & runparams) const
2458 idx_type cell = getFirstCellInRow(row);
2461 for (col_type j = 0; j < columns_; ++j) {
2462 if (isPartOfMultiColumn(row, j))
2465 os << "<entry align=\"";
2466 switch (getAlignment(cell)) {
2467 case LYX_ALIGN_LEFT:
2470 case LYX_ALIGN_RIGHT:
2478 os << "\" valign=\"";
2479 switch (getVAlignment(cell)) {
2480 case LYX_VALIGN_TOP:
2483 case LYX_VALIGN_BOTTOM:
2486 case LYX_VALIGN_MIDDLE:
2491 if (isMultiColumn(cell)) {
2492 os << " namest=\"col" << j << "\" ";
2493 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2497 ret += getCellInset(cell)->docbook(buf, os, runparams);
2506 int Tabular::docbook(Buffer const & buf, odocstream & os,
2507 OutputParams const & runparams) const
2511 //+---------------------------------------------------------------------
2512 //+ first the opening preamble +
2513 //+---------------------------------------------------------------------
2515 os << "<tgroup cols=\"" << columns_
2516 << "\" colsep=\"1\" rowsep=\"1\">\n";
2518 for (col_type i = 0; i < columns_; ++i) {
2519 os << "<colspec colname=\"col" << i << "\" align=\"";
2520 switch (column_info[i].alignment) {
2521 case LYX_ALIGN_LEFT:
2524 case LYX_ALIGN_RIGHT:
2532 if (runparams.flavor == OutputParams::XML)
2538 //+---------------------------------------------------------------------
2539 //+ Long Tabular case +
2540 //+---------------------------------------------------------------------
2542 // output header info
2543 if (haveLTHead() || haveLTFirstHead()) {
2546 for (row_type i = 0; i < rows_; ++i) {
2547 if (row_info[i].endhead || row_info[i].endfirsthead) {
2548 ret += docbookRow(buf, os, i, runparams);
2554 // output footer info
2555 if (haveLTFoot() || haveLTLastFoot()) {
2558 for (row_type i = 0; i < rows_; ++i) {
2559 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2560 ret += docbookRow(buf, os, i, runparams);
2567 //+---------------------------------------------------------------------
2568 //+ the single row and columns (cells) +
2569 //+---------------------------------------------------------------------
2573 for (row_type i = 0; i < rows_; ++i) {
2574 if (isValidRow(i)) {
2575 ret += docbookRow(buf, os, i, runparams);
2580 //+---------------------------------------------------------------------
2581 //+ the closing of the tabular +
2582 //+---------------------------------------------------------------------
2591 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2592 vector<unsigned int> const & clen) const
2594 idx_type const fcell = getFirstCellInRow(row);
2595 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2598 for (idx_type i = fcell; i < n; ++i) {
2608 for (idx_type i = fcell; i < n; ++i) {
2619 col_type column = column_of_cell(i);
2620 int len = clen[column];
2621 while (column < columns_ - 1
2622 && isPartOfMultiColumn(row, ++column))
2623 len += clen[column] + 4;
2624 os << docstring(len, ch);
2639 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2640 vector<unsigned int> const & clen) const
2642 idx_type const fcell = getFirstCellInRow(row);
2643 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2646 for (idx_type i = fcell; i < n; ++i) {
2647 if (bottomLine(i)) {
2656 for (idx_type i = fcell; i < n; ++i) {
2657 if (bottomLine(i)) {
2667 col_type column = column_of_cell(i);
2668 int len = clen[column];
2669 while (column < columns_ -1
2670 && isPartOfMultiColumn(row, ++column))
2671 len += clen[column] + 4;
2672 os << docstring(len, ch);
2673 if (bottomLine(i)) {
2687 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2688 OutputParams const & runparams,
2689 idx_type cell, row_type row, col_type column,
2690 vector<unsigned int> const & clen,
2691 bool onlydata) const
2693 odocstringstream sstr;
2694 getCellInset(cell)->plaintext(buf, sstr, runparams);
2706 unsigned int len1 = sstr.str().length();
2707 unsigned int len2 = clen[column];
2708 while (column < columns_ -1
2709 && isPartOfMultiColumn(row, ++column))
2710 len2 += clen[column] + 4;
2713 switch (getAlignment(cell)) {
2715 case LYX_ALIGN_LEFT:
2718 case LYX_ALIGN_RIGHT:
2722 case LYX_ALIGN_CENTER:
2728 os << docstring(len1, ' ') << sstr.str()
2729 << docstring(len2, ' ');
2731 if (rightLine(cell))
2738 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2739 OutputParams const & runparams, int const depth,
2740 bool onlydata, unsigned char delim) const
2742 // first calculate the width of the single columns
2743 vector<unsigned int> clen(columns_);
2746 // first all non (real) multicolumn cells!
2747 for (col_type j = 0; j < columns_; ++j) {
2749 for (row_type i = 0; i < rows_; ++i) {
2750 idx_type cell = getCellNumber(i, j);
2751 if (isMultiColumnReal(cell))
2753 odocstringstream sstr;
2754 getCellInset(cell)->plaintext(buf, sstr, runparams);
2755 if (clen[j] < sstr.str().length())
2756 clen[j] = sstr.str().length();
2759 // then all (real) multicolumn cells!
2760 for (col_type j = 0; j < columns_; ++j) {
2761 for (row_type i = 0; i < rows_; ++i) {
2762 idx_type cell = getCellNumber(i, j);
2763 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2765 odocstringstream sstr;
2766 getCellInset(cell)->plaintext(buf, sstr, runparams);
2767 int len = int(sstr.str().length());
2768 idx_type const n = cells_in_multicolumn(cell);
2769 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2771 if (len > int(clen[j + n - 1]))
2772 clen[j + n - 1] = len;
2777 for (row_type i = 0; i < rows_; ++i) {
2778 if (!onlydata && plaintextTopHLine(os, i, clen))
2779 os << docstring(depth * 2, ' ');
2780 for (col_type j = 0; j < columns_; ++j) {
2781 if (isPartOfMultiColumn(i, j))
2783 if (onlydata && j > 0)
2785 plaintextPrintCell(buf, os, runparams,
2786 cell, i, j, clen, onlydata);
2791 os << docstring(depth * 2, ' ');
2792 if (plaintextBottomHLine(os, i, clen))
2793 os << docstring(depth * 2, ' ');
2799 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2801 return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2805 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2806 col_type column) const
2808 return cell_info[row][column].inset;
2812 void Tabular::setCellInset(row_type row, col_type column,
2813 shared_ptr<InsetText> ins) const
2815 cell_info[row][column].inset = ins;
2820 Tabular::getCellFromInset(Inset const * inset) const
2822 // is this inset part of the tabular?
2824 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2825 BOOST_ASSERT(false);
2828 for (idx_type cell = 0, n = getNumberOfCells(); cell < n; ++cell)
2829 if (getCellInset(cell).get() == inset) {
2830 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2831 << "cell=" << cell << endl;
2835 // We should have found a cell at this point
2836 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2837 << inset << " not found!" << endl;
2838 BOOST_ASSERT(false);
2844 void Tabular::validate(LaTeXFeatures & features) const
2846 features.require("NeedTabularnewline");
2848 features.require("booktabs");
2849 if (isLongTabular())
2850 features.require("longtable");
2852 features.require("rotating");
2853 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2854 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2855 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2856 features.require("array");
2857 getCellInset(cell)->validate(features);
2862 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2864 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2865 ParagraphList::const_iterator cit = parlist.begin();
2866 ParagraphList::const_iterator end = parlist.end();
2868 for (; cit != end; ++cit)
2869 for (int i = 0; i < cit->size(); ++i)
2870 if (cit->isNewline(i))
2877 /////////////////////////////////////////////////////////////////////
2881 /////////////////////////////////////////////////////////////////////
2883 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2885 : tabular(buf.params(), max(rows, row_type(1)),
2886 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2890 InsetTabular::InsetTabular(InsetTabular const & tab)
2891 : Inset(tab), tabular(tab.tabular),
2892 buffer_(tab.buffer_), scx_(0)
2896 InsetTabular::~InsetTabular()
2898 InsetTabularMailer(*this).hideDialog();
2902 auto_ptr<Inset> InsetTabular::doClone() const
2904 return auto_ptr<Inset>(new InsetTabular(*this));
2908 Buffer const & InsetTabular::buffer() const
2914 void InsetTabular::buffer(Buffer const * b)
2920 void InsetTabular::write(Buffer const & buf, ostream & os) const
2922 os << "Tabular" << endl;
2923 tabular.write(buf, os);
2927 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2929 bool const old_format = (lex.getString() == "\\LyXTable");
2931 tabular.read(buf, lex);
2937 string token = lex.getString();
2938 while (lex && token != "\\end_inset") {
2940 token = lex.getString();
2943 lex.printError("Missing \\end_inset at this point. "
2949 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2951 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2952 // mi.base.textwidth << "\n";
2954 lyxerr << "InsetTabular::metrics: need bv" << endl;
2955 BOOST_ASSERT(false);
2959 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2962 for (col_type j = 0; j < tabular.columns(); ++j) {
2963 if (tabular.isPartOfMultiColumn(i, j))
2964 // Multicolumn cell, but not first one
2969 if (tabular.cell_info[i][j].multicolumn ==
2970 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2971 p_width = tabular.cellinfo_of_cell(cell).p_width;
2973 p_width = tabular.column_info[j].p_width;
2974 if (!p_width.zero())
2975 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2976 tabular.getCellInset(cell)->metrics(m, dim);
2977 if (!p_width.zero())
2978 dim.wid = m.base.textwidth;
2979 tabular.setWidthOfCell(cell, dim.wid);
2980 if (p_width.zero()) {
2981 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2982 // FIXME there must be a way to get rid of
2983 // the second metrics call
2984 tabular.getCellInset(cell)->metrics(m, dim);
2986 maxAsc = max(maxAsc, dim.asc);
2987 maxDesc = max(maxDesc, dim.des);
2990 int const top_space = tabular.row_info[i].top_space_default ?
2991 default_line_space :
2992 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2993 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2994 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2995 default_line_space :
2996 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2997 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3000 dim.asc = tabular.getAscentOfRow(0);
3001 dim.des = tabular.getHeightOfTabular() - dim.asc;
3002 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
3003 bool const changed = dim_ != dim;
3009 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3011 setPosCache(pi, x, y);
3013 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3014 BufferView * bv = pi.base.bv;
3016 resetPos(bv->cursor());
3019 x += ADD_TO_TABULAR_WIDTH;
3022 first_visible_cell = Tabular::npos;
3023 for (row_type i = 0; i < tabular.rows(); ++i) {
3025 int const a = tabular.getAscentOfRow(i);
3026 int const d = tabular.getDescentOfRow(i);
3027 idx = tabular.getCellNumber(i, 0);
3028 for (col_type j = 0; j < tabular.columns(); ++j) {
3029 if (tabular.isPartOfMultiColumn(i, j))
3031 if (first_visible_cell == Tabular::npos)
3032 first_visible_cell = idx;
3034 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3035 if (nx + tabular.getWidthOfColumn(idx) < 0
3036 || nx > bv->workWidth()
3038 || y - a > bv->workHeight()) {
3039 pi.pain.setDrawingEnabled(false);
3040 cell(idx)->draw(pi, cx, y);
3041 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3042 pi.pain.setDrawingEnabled(true);
3044 cell(idx)->draw(pi, cx, y);
3045 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3047 nx += tabular.getWidthOfColumn(idx);
3051 if (i + 1 < tabular.rows())
3052 y += d + tabular.getAscentOfRow(i + 1) +
3053 tabular.getAdditionalHeight(i + 1);
3058 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3060 setPosCache(pi, x, y);
3062 Cursor & cur = pi.base.bv->cursor();
3064 x += scx_ + ADD_TO_TABULAR_WIDTH;
3066 // Paint background of current tabular
3067 int const w = tabular.getWidthOfTabular();
3068 int const h = tabular.getHeightOfTabular();
3069 int yy = y - tabular.getAscentOfRow(0);
3070 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3072 if (!cur.selection())
3074 if (!ptr_cmp(&cur.inset(), this))
3080 if (tablemode(cur)) {
3083 getSelection(cur, rs, re, cs, ce);
3084 y -= tabular.getAscentOfRow(0);
3085 for (row_type j = 0; j < tabular.rows(); ++j) {
3086 int const a = tabular.getAscentOfRow(j);
3087 int const h = a + tabular.getDescentOfRow(j);
3089 y += tabular.getAdditionalHeight(j);
3090 for (col_type i = 0; i < tabular.columns(); ++i) {
3091 if (tabular.isPartOfMultiColumn(j, i))
3093 idx_type const cell =
3094 tabular.getCellNumber(j, i);
3095 int const w = tabular.getWidthOfColumn(cell);
3096 if (i >= cs && i <= ce && j >= rs && j <= re)
3097 pi.pain.fillRectangle(xx, y, w, h,
3105 x += getCellXPos(cur.idx());
3106 x += tabular.getBeginningOfTextInCell(cur.idx());
3107 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3112 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3113 row_type row, idx_type cell, bool erased) const
3115 int x2 = x + tabular.getWidthOfColumn(cell);
3116 bool on_off = false;
3117 Color::color col = Color::tabularline;
3118 Color::color onoffcol = Color::tabularonoffline;
3121 col = Color::deletedtext;
3122 onoffcol = Color::deletedtext;
3125 if (!tabular.topAlreadyDrawn(cell)) {
3126 on_off = !tabular.topLine(cell);
3127 pain.line(x, y - tabular.getAscentOfRow(row),
3128 x2, y - tabular.getAscentOfRow(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3132 on_off = !tabular.bottomLine(cell);
3133 pain.line(x, y + tabular.getDescentOfRow(row),
3134 x2, y + tabular.getDescentOfRow(row),
3135 on_off ? onoffcol : col,
3136 on_off ? Painter::line_onoffdash : Painter::line_solid);
3137 if (!tabular.leftAlreadyDrawn(cell)) {
3138 on_off = !tabular.leftLine(cell);
3139 pain.line(x, y - tabular.getAscentOfRow(row),
3140 x, y + tabular.getDescentOfRow(row),
3141 on_off ? onoffcol : col,
3142 on_off ? Painter::line_onoffdash : Painter::line_solid);
3144 on_off = !tabular.rightLine(cell);
3145 pain.line(x2 - tabular.getAdditionalWidth(cell),
3146 y - tabular.getAscentOfRow(row),
3147 x2 - tabular.getAdditionalWidth(cell),
3148 y + tabular.getDescentOfRow(row),
3149 on_off ? onoffcol : col,
3150 on_off ? Painter::line_onoffdash : Painter::line_solid);
3154 docstring const InsetTabular::editMessage() const
3156 return _("Opened table");
3160 void InsetTabular::edit(Cursor & cur, bool left)
3162 //lyxerr << "InsetTabular::edit: " << this << endl;
3164 cur.selection() = false;
3167 if (isRightToLeft(cur))
3168 cur.idx() = tabular.getLastCellInRow(0);
3174 if (isRightToLeft(cur))
3175 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3177 cur.idx() = tabular.getNumberOfCells() - 1;
3179 cur.pos() = cur.lastpos(); // FIXME crude guess
3181 // FIXME: this accesses the position cache before it is initialized
3183 //cur.bv().fitCursor();
3187 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3189 // In a longtable, tell captions what the current float is
3190 Counters & cnts = buf.params().getTextClass().counters();
3191 string const saveflt = cnts.current_float();
3192 if (tabular.isLongTabular())
3193 cnts.current_float("table");
3195 ParIterator it2 = it;
3197 for ( ; it2.idx() <= it2.lastidx() ; it2.forwardIdx())
3198 lyx::updateLabels(buf, it2);
3201 if (tabular.isLongTabular())
3202 cnts.current_float(saveflt);
3206 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3208 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3209 << "\n cur:" << cur << endl;
3210 CursorSlice sl = cur.top();
3211 Cursor & bvcur = cur.bv().cursor();
3213 switch (cmd.action) {
3215 case LFUN_MOUSE_PRESS:
3216 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3218 if (cmd.button() == mouse_button::button1
3219 || (cmd.button() == mouse_button::button3
3220 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3221 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3223 cur.selection() = false;
3224 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3225 cur.bv().mouseSetCursor(cur);
3229 if (cmd.button() == mouse_button::button2) {
3230 if (cap::selection()) {
3231 // See comment in Text::dispatch why we
3233 // FIXME This does not use paste_tabular,
3234 // another reason why paste_tabular should go.
3235 cap::copySelectionToStack();
3236 cmd = FuncRequest(LFUN_PASTE, "0");
3238 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3241 doDispatch(cur, cmd);
3242 cur.bv().buffer()->markDirty();
3243 cur.bv().mouseSetCursor(cur);
3247 case LFUN_MOUSE_MOTION:
3248 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3249 if (cmd.button() == mouse_button::button1) {
3250 // only accept motions to places not deeper nested than the real anchor
3251 if (bvcur.anchor_.hasPart(cur)) {
3252 // only update if selection changes
3253 if (bvcur.idx() == cur.idx() &&
3254 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3256 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3257 bvcur.setCursor(cur);
3258 bvcur.selection() = true;
3264 case LFUN_MOUSE_RELEASE:
3265 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3266 if (cmd.button() == mouse_button::button3)
3267 InsetTabularMailer(*this).showDialog(&cur.bv());
3270 case LFUN_CELL_BACKWARD:
3272 cur.selection() = false;
3275 case LFUN_CELL_FORWARD:
3277 cur.selection() = false;
3280 case LFUN_CHAR_FORWARD_SELECT:
3281 case LFUN_CHAR_FORWARD:
3282 cell(cur.idx())->dispatch(cur, cmd);
3283 if (!cur.result().dispatched()) {
3284 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3285 if (sl == cur.top())
3286 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3292 case LFUN_CHAR_BACKWARD_SELECT:
3293 case LFUN_CHAR_BACKWARD:
3294 cell(cur.idx())->dispatch(cur, cmd);
3295 if (!cur.result().dispatched()) {
3296 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3297 if (sl == cur.top())
3298 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3304 case LFUN_DOWN_SELECT:
3306 cell(cur.idx())->dispatch(cur, cmd);
3307 cur.dispatched(); // override the cell's decision
3308 if (sl == cur.top())
3309 // if our Text didn't do anything to the cursor
3310 // then we try to put the cursor into the cell below
3311 // setting also the right targetX.
3312 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3313 cur.idx() = tabular.getCellBelow(cur.idx());
3315 TextMetrics const & tm =
3316 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3317 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3319 if (sl == cur.top()) {
3320 // we trick it to go to the RIGHT after leaving the
3322 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3327 case LFUN_UP_SELECT:
3329 cell(cur.idx())->dispatch(cur, cmd);
3330 cur.dispatched(); // override the cell's decision
3331 if (sl == cur.top())
3332 // if our Text didn't do anything to the cursor
3333 // then we try to put the cursor into the cell above
3334 // setting also the right targetX.
3335 if (tabular.row_of_cell(cur.idx()) != 0) {
3336 cur.idx() = tabular.getCellAbove(cur.idx());
3337 cur.pit() = cur.lastpit();
3338 Text const * text = cell(cur.idx())->getText(0);
3339 TextMetrics const & tm = cur.bv().textMetrics(text);
3340 ParagraphMetrics const & pm =
3341 tm.parMetrics(cur.lastpit());
3342 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3344 if (sl == cur.top()) {
3345 cmd = FuncRequest(LFUN_UP);
3350 // case LFUN_SCREEN_DOWN: {
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();
3355 // if (t < yo() + tabular.getHeightOfTabular()) {
3356 // cur.bv().scrollDocView(t);
3357 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3359 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3366 // case LFUN_SCREEN_UP: {
3367 // //if (hasSelection())
3368 // // cur.selection() = false;
3369 // col_type const col = tabular.column_of_cell(cur.idx());
3370 // int const t = cur.bv().top_y() + cur.bv().height();
3372 // cur.bv().scrollDocView(t);
3376 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3380 // cur.par() = cur.lastpar();
3381 // cur.pos() = cur.lastpos();
3385 case LFUN_LAYOUT_TABULAR:
3386 InsetTabularMailer(*this).showDialog(&cur.bv());
3389 case LFUN_INSET_DIALOG_UPDATE:
3390 InsetTabularMailer(*this).updateDialog(&cur.bv());
3393 case LFUN_TABULAR_FEATURE:
3394 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3398 // insert file functions
3399 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3400 case LFUN_FILE_INSERT_PLAINTEXT: {
3402 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3403 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3409 if (tablemode(cur)) {
3410 if (copySelection(cur)) {
3411 recordUndoInset(cur, Undo::DELETE);
3416 cell(cur.idx())->dispatch(cur, cmd);
3419 case LFUN_CHAR_DELETE_BACKWARD:
3420 case LFUN_CHAR_DELETE_FORWARD:
3421 if (tablemode(cur)) {
3422 recordUndoInset(cur, Undo::DELETE);
3426 cell(cur.idx())->dispatch(cur, cmd);
3430 if (!cur.selection())
3432 if (tablemode(cur)) {
3436 cell(cur.idx())->dispatch(cur, cmd);
3439 case LFUN_CLIPBOARD_PASTE:
3440 case LFUN_PRIMARY_SELECTION_PASTE: {
3441 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3442 theClipboard().getAsText() :
3443 theSelection().get();
3446 // pass to InsertPlaintextString, but
3447 // only if we have multi-cell content
3448 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3449 if (insertPlaintextString(cur.bv(), clip, false)) {
3450 // content has been replaced,
3451 // so cursor might be invalid
3452 cur.pos() = cur.lastpos();
3453 bvcur.setCursor(cur);
3457 // Let the cell handle normal text
3458 cell(cur.idx())->dispatch(cur, cmd);
3463 if (tabularStackDirty() && theClipboard().isInternal()) {
3464 recordUndoInset(cur, Undo::INSERT);
3465 pasteClipboard(cur);
3468 cell(cur.idx())->dispatch(cur, cmd);
3471 case LFUN_FONT_EMPH:
3472 case LFUN_FONT_BOLD:
3473 case LFUN_FONT_ROMAN:
3474 case LFUN_FONT_NOUN:
3475 case LFUN_FONT_ITAL:
3476 case LFUN_FONT_FRAK:
3477 case LFUN_FONT_CODE:
3478 case LFUN_FONT_SANS:
3479 case LFUN_FONT_FREE_APPLY:
3480 case LFUN_FONT_FREE_UPDATE:
3481 case LFUN_FONT_SIZE:
3482 case LFUN_FONT_UNDERLINE:
3484 case LFUN_WORD_CAPITALIZE:
3485 case LFUN_WORD_UPCASE:
3486 case LFUN_WORD_LOWCASE:
3487 case LFUN_CHARS_TRANSPOSE:
3488 if (tablemode(cur)) {
3491 getSelection(cur, rs, re, cs, ce);
3492 Cursor tmpcur = cur;
3493 for (row_type i = rs; i <= re; ++i) {
3494 for (col_type j = cs; j <= ce; ++j) {
3495 // cursor follows cell:
3496 tmpcur.idx() = tabular.getCellNumber(i, j);
3497 // select this cell only:
3500 tmpcur.resetAnchor();
3501 tmpcur.pit() = tmpcur.lastpit();
3502 tmpcur.pos() = tmpcur.top().lastpos();
3503 tmpcur.setCursor(tmpcur);
3504 tmpcur.setSelection();
3505 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3510 cell(cur.idx())->dispatch(cur, cmd);
3514 // we try to handle this event in the insets dispatch function.
3515 cell(cur.idx())->dispatch(cur, cmd);
3521 // function sets an object as defined in func_status.h:
3522 // states OK, Unknown, Disabled, On, Off.
3523 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3524 FuncStatus & status) const
3526 switch (cmd.action) {
3527 case LFUN_TABULAR_FEATURE: {
3528 int action = Tabular::LAST_ACTION;
3530 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3531 string const tmp = tabularFeature[i].feature;
3532 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3533 action = tabularFeature[i].action;
3537 if (action == Tabular::LAST_ACTION) {
3539 status.unknown(true);
3543 string const argument
3544 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3546 row_type sel_row_start = 0;
3547 row_type sel_row_end = 0;
3548 col_type sel_col_start = 0;
3549 col_type sel_col_end = 0;
3550 Tabular::ltType dummyltt;
3553 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3556 case Tabular::SET_PWIDTH:
3557 case Tabular::SET_MPWIDTH:
3558 case Tabular::SET_SPECIAL_COLUMN:
3559 case Tabular::SET_SPECIAL_MULTI:
3560 case Tabular::APPEND_ROW:
3561 case Tabular::APPEND_COLUMN:
3562 case Tabular::DELETE_ROW:
3563 case Tabular::DELETE_COLUMN:
3564 case Tabular::COPY_ROW:
3565 case Tabular::COPY_COLUMN:
3566 case Tabular::SET_ALL_LINES:
3567 case Tabular::UNSET_ALL_LINES:
3568 case Tabular::SET_TOP_SPACE:
3569 case Tabular::SET_BOTTOM_SPACE:
3570 case Tabular::SET_INTERLINE_SPACE:
3574 case Tabular::MULTICOLUMN:
3575 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3578 case Tabular::M_TOGGLE_LINE_TOP:
3580 case Tabular::TOGGLE_LINE_TOP:
3581 status.setOnOff(tabular.topLine(cur.idx(), flag));
3584 case Tabular::M_TOGGLE_LINE_BOTTOM:
3586 case Tabular::TOGGLE_LINE_BOTTOM:
3587 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3590 case Tabular::M_TOGGLE_LINE_LEFT:
3592 case Tabular::TOGGLE_LINE_LEFT:
3593 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3596 case Tabular::M_TOGGLE_LINE_RIGHT:
3598 case Tabular::TOGGLE_LINE_RIGHT:
3599 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3602 case Tabular::M_ALIGN_LEFT:
3604 case Tabular::ALIGN_LEFT:
3605 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3608 case Tabular::M_ALIGN_RIGHT:
3610 case Tabular::ALIGN_RIGHT:
3611 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3614 case Tabular::M_ALIGN_CENTER:
3616 case Tabular::ALIGN_CENTER:
3617 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3620 case Tabular::ALIGN_BLOCK:
3621 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3622 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3625 case Tabular::M_VALIGN_TOP:
3627 case Tabular::VALIGN_TOP:
3629 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3632 case Tabular::M_VALIGN_BOTTOM:
3634 case Tabular::VALIGN_BOTTOM:
3636 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3639 case Tabular::M_VALIGN_MIDDLE:
3641 case Tabular::VALIGN_MIDDLE:
3643 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3646 case Tabular::SET_LONGTABULAR:
3647 status.setOnOff(tabular.isLongTabular());
3650 case Tabular::UNSET_LONGTABULAR:
3651 status.setOnOff(!tabular.isLongTabular());
3654 case Tabular::TOGGLE_ROTATE_TABULAR:
3655 case Tabular::SET_ROTATE_TABULAR:
3656 status.setOnOff(tabular.getRotateTabular());
3659 case Tabular::UNSET_ROTATE_TABULAR:
3660 status.setOnOff(!tabular.getRotateTabular());
3663 case Tabular::TOGGLE_ROTATE_CELL:
3664 case Tabular::SET_ROTATE_CELL:
3665 status.setOnOff(!oneCellHasRotationState(false,
3666 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3669 case Tabular::UNSET_ROTATE_CELL:
3670 status.setOnOff(!oneCellHasRotationState(true,
3671 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3674 case Tabular::SET_USEBOX:
3675 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3678 case Tabular::SET_LTFIRSTHEAD:
3679 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3682 case Tabular::UNSET_LTFIRSTHEAD:
3683 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3686 case Tabular::SET_LTHEAD:
3687 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3690 case Tabular::UNSET_LTHEAD:
3691 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3694 case Tabular::SET_LTFOOT:
3695 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3698 case Tabular::UNSET_LTFOOT:
3699 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3702 case Tabular::SET_LTLASTFOOT:
3703 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3706 case Tabular::UNSET_LTLASTFOOT:
3707 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3710 case Tabular::SET_LTNEWPAGE:
3711 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3714 case Tabular::SET_BOOKTABS:
3715 status.setOnOff(tabular.useBookTabs());
3718 case Tabular::UNSET_BOOKTABS:
3719 status.setOnOff(!tabular.useBookTabs());
3724 status.enabled(false);
3730 // These are only enabled inside tabular
3731 case LFUN_CELL_BACKWARD:
3732 case LFUN_CELL_FORWARD:
3733 status.enabled(true);
3736 // disable these with multiple cells selected
3737 case LFUN_INSET_INSERT:
3738 case LFUN_TABULAR_INSERT:
3739 case LFUN_CHARSTYLE_INSERT:
3740 case LFUN_FLOAT_INSERT:
3741 case LFUN_FLOAT_WIDE_INSERT:
3742 case LFUN_FOOTNOTE_INSERT:
3743 case LFUN_MARGINALNOTE_INSERT:
3744 case LFUN_MATH_INSERT:
3745 case LFUN_MATH_MODE:
3746 case LFUN_MATH_MUTATE:
3747 case LFUN_MATH_DISPLAY:
3748 case LFUN_NOTE_INSERT:
3749 case LFUN_OPTIONAL_INSERT:
3750 case LFUN_BOX_INSERT:
3751 case LFUN_BRANCH_INSERT:
3752 case LFUN_WRAP_INSERT:
3753 case LFUN_ERT_INSERT: {
3754 if (tablemode(cur)) {
3755 status.enabled(false);
3758 return cell(cur.idx())->getStatus(cur, cmd, status);
3761 // disable in non-fixed-width cells
3762 case LFUN_BREAK_LINE:
3763 case LFUN_BREAK_PARAGRAPH:
3764 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3765 case LFUN_BREAK_PARAGRAPH_SKIP: {
3766 if (tabular.getPWidth(cur.idx()).zero()) {
3767 status.enabled(false);
3770 return cell(cur.idx())->getStatus(cur, cmd, status);
3774 if (tabularStackDirty() && theClipboard().isInternal()) {
3775 status.enabled(true);
3778 return cell(cur.idx())->getStatus(cur, cmd, status);
3780 case LFUN_INSET_MODIFY:
3781 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3782 status.enabled(true);
3788 // we try to handle this event in the insets dispatch function.
3789 return cell(cur.idx())->getStatus(cur, cmd, status);
3794 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3795 OutputParams const & runparams) const
3797 return tabular.latex(buf, os, runparams);
3801 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3802 OutputParams const & runparams) const
3804 os << '\n'; // output table on a new line
3805 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3806 tabular.plaintext(buf, os, runparams, dp, false, 0);
3807 return PLAINTEXT_NEWLINE;
3811 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3812 OutputParams const & runparams) const
3817 // FIXME: Why not pass a proper DocIterator here?
3819 // if the table is inside a float it doesn't need the informaltable
3820 // wrapper. Search for it.
3821 for (master = owner(); master; master = master->owner())
3822 if (master->lyxCode() == Inset::FLOAT_CODE)
3827 os << "<informaltable>";
3830 ret += tabular.docbook(buf, os, runparams);
3832 os << "</informaltable>";
3839 void InsetTabular::validate(LaTeXFeatures & features) const
3841 tabular.validate(features);
3845 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3847 return tabular.getCellInset(idx);
3851 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3853 return tabular.getCellInset(idx);
3857 void InsetTabular::cursorPos(BufferView const & bv,
3858 CursorSlice const & sl, bool boundary, int & x, int & y) const
3860 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3862 // y offset correction
3863 int const row = tabular.row_of_cell(sl.idx());
3864 for (int i = 0; i <= row; ++i) {
3866 y += tabular.getAscentOfRow(i);
3867 y += tabular.getAdditionalHeight(i);
3870 y += tabular.getDescentOfRow(i);
3873 // x offset correction
3874 int const col = tabular.column_of_cell(sl.idx());
3875 int idx = tabular.getCellNumber(row, 0);
3876 for (int j = 0; j < col; ++j) {
3877 if (tabular.isPartOfMultiColumn(row, j))
3879 x += tabular.getWidthOfColumn(idx);
3882 x += tabular.getBeginningOfTextInCell(idx);
3883 x += ADD_TO_TABULAR_WIDTH;
3888 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3892 Inset const & inset = *tabular.getCellInset(cell);
3893 Point o = bv.coordCache().getInsets().xy(&inset);
3894 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3895 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3896 row_type const row = tabular.row_of_cell(cell);
3897 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3898 tabular.getAdditionalHeight(row);
3899 int const yend = o.y_ + tabular.getDescentOfRow(row);
3911 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3912 // << " ybeg=" << ybeg << " yend=" << yend
3913 // << " xx=" << xx << " yy=" << yy
3914 // << " dist=" << xx + yy << endl;
3919 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3921 //lyxerr << "InsetTabular::editXY: " << this << endl;
3922 cur.selection() = false;
3924 cur.idx() = getNearestCell(cur.bv(), x, y);
3926 return cell(cur.idx())->text_.editXY(cur, x, y);
3930 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3932 cur.idx() = getNearestCell(cur.bv(), x, y);
3933 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3937 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3939 idx_type idx_min = 0;
3940 int dist_min = std::numeric_limits<int>::max();
3941 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3942 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3943 int const d = dist(bv, i, x, y);
3954 int InsetTabular::getCellXPos(idx_type const cell) const
3958 for (; !tabular.isFirstCellInRow(c); --c)
3961 for (; c < cell; ++c)
3962 lx += tabular.getWidthOfColumn(c);
3968 void InsetTabular::resetPos(Cursor & cur) const
3970 BufferView & bv = cur.bv();
3971 int const maxwidth = bv.workWidth();
3973 if (&cur.inset() != this) {
3977 int const X2 = maxwidth;
3978 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3979 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3980 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3983 scx_ = X1 + 20 - x1;
3985 scx_ = X2 - 20 - x2;
3990 cur.updateFlags(Update::Force | Update::FitCursor);
3994 void InsetTabular::moveNextCell(Cursor & cur)
3996 if (isRightToLeft(cur)) {
3997 if (tabular.isFirstCellInRow(cur.idx())) {
3998 row_type const row = tabular.row_of_cell(cur.idx());
3999 if (row == tabular.rows() - 1)
4001 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4008 if (tabular.isLastCell(cur.idx()))
4018 void InsetTabular::movePrevCell(Cursor & cur)
4020 if (isRightToLeft(cur)) {
4021 if (tabular.isLastCellInRow(cur.idx())) {
4022 row_type const row = tabular.row_of_cell(cur.idx());
4025 cur.idx() = tabular.getFirstCellInRow(row);
4026 cur.idx() = tabular.getCellAbove(cur.idx());
4028 if (tabular.isLastCell(cur.idx()))
4033 if (cur.idx() == 0) // first cell
4037 cur.pit() = cur.lastpit();
4038 cur.pos() = cur.lastpos();
4040 // FIXME: this accesses the position cache before it is initialized
4045 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4047 Tabular::Feature action = Tabular::LAST_ACTION;
4050 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4051 string const tmp = tabularFeature[i].feature;
4053 if (tmp == what.substr(0, tmp.length())) {
4054 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4055 //tabularFeatures[i].feature.length()))
4056 action = tabularFeature[i].action;
4060 if (action == Tabular::LAST_ACTION)
4064 ltrim(what.substr(tabularFeature[i].feature.length()));
4065 tabularFeatures(cur, action, val);
4070 static void checkLongtableSpecial(Tabular::ltType & ltt,
4071 string const & special, bool & flag)
4073 if (special == "dl_above") {
4076 } else if (special == "dl_below") {
4077 ltt.bottomDL = flag;
4079 } else if (special == "empty") {
4088 bool InsetTabular::oneCellHasRotationState(bool rotated,
4089 row_type row_start, row_type row_end,
4090 col_type col_start, col_type col_end) const {
4092 for (row_type i = row_start; i <= row_end; ++i) {
4093 for (col_type j = col_start; j <= col_end; ++j) {
4094 if (tabular.getRotateCell(tabular.getCellNumber(i, j))
4103 void InsetTabular::tabularFeatures(Cursor & cur,
4104 Tabular::Feature feature, string const & value)
4106 BufferView & bv = cur.bv();
4107 col_type sel_col_start;
4108 col_type sel_col_end;
4109 row_type sel_row_start;
4110 row_type sel_row_end;
4111 bool setLines = false;
4112 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4113 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4117 case Tabular::M_ALIGN_LEFT:
4118 case Tabular::ALIGN_LEFT:
4119 setAlign = LYX_ALIGN_LEFT;
4122 case Tabular::M_ALIGN_RIGHT:
4123 case Tabular::ALIGN_RIGHT:
4124 setAlign = LYX_ALIGN_RIGHT;
4127 case Tabular::M_ALIGN_CENTER:
4128 case Tabular::ALIGN_CENTER:
4129 setAlign = LYX_ALIGN_CENTER;
4132 case Tabular::ALIGN_BLOCK:
4133 setAlign = LYX_ALIGN_BLOCK;
4136 case Tabular::M_VALIGN_TOP:
4137 case Tabular::VALIGN_TOP:
4138 setVAlign = Tabular::LYX_VALIGN_TOP;
4141 case Tabular::M_VALIGN_BOTTOM:
4142 case Tabular::VALIGN_BOTTOM:
4143 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4146 case Tabular::M_VALIGN_MIDDLE:
4147 case Tabular::VALIGN_MIDDLE:
4148 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4155 recordUndoInset(cur, Undo::ATOMIC);
4157 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4158 row_type const row = tabular.row_of_cell(cur.idx());
4159 col_type const column = tabular.column_of_cell(cur.idx());
4161 Tabular::ltType ltt;
4165 case Tabular::SET_PWIDTH: {
4166 Length const len(value);
4167 tabular.setColumnPWidth(cur, cur.idx(), len);
4169 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4170 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4174 case Tabular::SET_MPWIDTH:
4175 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4178 case Tabular::SET_SPECIAL_COLUMN:
4179 case Tabular::SET_SPECIAL_MULTI:
4180 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4183 case Tabular::APPEND_ROW:
4184 // append the row into the tabular
4185 tabular.appendRow(bv.buffer()->params(), cur.idx());
4188 case Tabular::APPEND_COLUMN:
4189 // append the column into the tabular
4190 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4191 cur.idx() = tabular.getCellNumber(row, column);
4194 case Tabular::DELETE_ROW:
4195 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4196 tabular.deleteRow(sel_row_start);
4197 if (sel_row_start >= tabular.rows())
4199 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4202 cur.selection() = false;
4205 case Tabular::DELETE_COLUMN:
4206 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4207 tabular.deleteColumn(sel_col_start);
4208 if (sel_col_start >= tabular.columns())
4210 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4213 cur.selection() = false;
4216 case Tabular::COPY_ROW:
4217 tabular.copyRow(bv.buffer()->params(), row);
4220 case Tabular::COPY_COLUMN:
4221 tabular.copyColumn(bv.buffer()->params(), column);
4222 cur.idx() = tabular.getCellNumber(row, column);
4225 case Tabular::M_TOGGLE_LINE_TOP:
4227 case Tabular::TOGGLE_LINE_TOP: {
4228 bool lineSet = !tabular.topLine(cur.idx(), flag);
4229 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4230 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4232 tabular.getCellNumber(i, j),
4237 case Tabular::M_TOGGLE_LINE_BOTTOM:
4239 case Tabular::TOGGLE_LINE_BOTTOM: {
4240 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4241 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4242 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4243 tabular.setBottomLine(
4244 tabular.getCellNumber(i, j),
4250 case Tabular::M_TOGGLE_LINE_LEFT:
4252 case Tabular::TOGGLE_LINE_LEFT: {
4253 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4254 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4255 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4256 tabular.setLeftLine(
4257 tabular.getCellNumber(i,j),
4263 case Tabular::M_TOGGLE_LINE_RIGHT:
4265 case Tabular::TOGGLE_LINE_RIGHT: {
4266 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4267 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4268 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4269 tabular.setRightLine(
4270 tabular.getCellNumber(i,j),
4276 case Tabular::M_ALIGN_LEFT:
4277 case Tabular::M_ALIGN_RIGHT:
4278 case Tabular::M_ALIGN_CENTER:
4280 case Tabular::ALIGN_LEFT:
4281 case Tabular::ALIGN_RIGHT:
4282 case Tabular::ALIGN_CENTER:
4283 case Tabular::ALIGN_BLOCK:
4284 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4285 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4286 tabular.setAlignment(
4287 tabular.getCellNumber(i, j),
4292 case Tabular::M_VALIGN_TOP:
4293 case Tabular::M_VALIGN_BOTTOM:
4294 case Tabular::M_VALIGN_MIDDLE:
4296 case Tabular::VALIGN_TOP:
4297 case Tabular::VALIGN_BOTTOM:
4298 case Tabular::VALIGN_MIDDLE:
4299 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4300 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4301 tabular.setVAlignment(
4302 tabular.getCellNumber(i, j),
4306 case Tabular::MULTICOLUMN: {
4307 if (sel_row_start != sel_row_end) {
4308 // FIXME: Need I say it ? This is horrible.
4310 Alert::error(_("Error setting multicolumn"),
4311 _("You cannot set multicolumn vertically."));
4314 if (!cur.selection()) {
4315 // just multicol for one single cell
4316 // check whether we are completely in a multicol
4317 if (tabular.isMultiColumn(cur.idx()))
4318 tabular.unsetMultiColumn(cur.idx());
4320 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4323 // we have a selection so this means we just add all this
4324 // cells to form a multicolumn cell
4325 idx_type const s_start = cur.selBegin().idx();
4326 idx_type const s_end = cur.selEnd().idx();
4327 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4328 cur.idx() = s_start;
4331 cur.selection() = false;
4335 case Tabular::SET_ALL_LINES:
4337 case Tabular::UNSET_ALL_LINES:
4338 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4339 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4340 tabular.setAllLines(
4341 tabular.getCellNumber(i,j), setLines);
4344 case Tabular::SET_LONGTABULAR:
4345 tabular.setLongTabular(true);
4348 case Tabular::UNSET_LONGTABULAR:
4349 tabular.setLongTabular(false);
4352 case Tabular::SET_ROTATE_TABULAR:
4353 tabular.setRotateTabular(true);
4356 case Tabular::UNSET_ROTATE_TABULAR:
4357 tabular.setRotateTabular(false);
4360 case Tabular::TOGGLE_ROTATE_TABULAR:
4361 tabular.setRotateTabular(!tabular.getRotateTabular());
4364 case Tabular::SET_ROTATE_CELL:
4365 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4366 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4367 tabular.setRotateCell(
4368 tabular.getCellNumber(i, j), true);
4371 case Tabular::UNSET_ROTATE_CELL:
4372 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4373 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4374 tabular.setRotateCell(
4375 tabular.getCellNumber(i, j), false);
4378 case Tabular::TOGGLE_ROTATE_CELL:
4380 bool oneNotRotated = oneCellHasRotationState(false,
4381 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4383 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4384 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4385 tabular.setRotateCell(tabular.getCellNumber(i, j),
4390 case Tabular::SET_USEBOX: {
4391 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4392 if (val == tabular.getUsebox(cur.idx()))
4393 val = Tabular::BOX_NONE;
4394 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4395 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4396 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4400 case Tabular::UNSET_LTFIRSTHEAD:
4402 case Tabular::SET_LTFIRSTHEAD:
4403 tabular.getRowOfLTFirstHead(row, ltt);
4404 checkLongtableSpecial(ltt, value, flag);
4405 tabular.setLTHead(row, flag, ltt, true);
4408 case Tabular::UNSET_LTHEAD:
4410 case Tabular::SET_LTHEAD:
4411 tabular.getRowOfLTHead(row, ltt);
4412 checkLongtableSpecial(ltt, value, flag);
4413 tabular.setLTHead(row, flag, ltt, false);
4416 case Tabular::UNSET_LTFOOT:
4418 case Tabular::SET_LTFOOT:
4419 tabular.getRowOfLTFoot(row, ltt);
4420 checkLongtableSpecial(ltt, value, flag);
4421 tabular.setLTFoot(row, flag, ltt, false);
4424 case Tabular::UNSET_LTLASTFOOT:
4426 case Tabular::SET_LTLASTFOOT:
4427 tabular.getRowOfLTLastFoot(row, ltt);
4428 checkLongtableSpecial(ltt, value, flag);
4429 tabular.setLTFoot(row, flag, ltt, true);
4432 case Tabular::SET_LTNEWPAGE:
4433 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4436 case Tabular::SET_BOOKTABS:
4437 tabular.setBookTabs(true);
4440 case Tabular::UNSET_BOOKTABS:
4441 tabular.setBookTabs(false);
4444 case Tabular::SET_TOP_SPACE: {
4446 if (value == "default")
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4448 tabular.row_info[i].top_space_default = true;
4449 else if (isValidLength(value, &len))
4450 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4451 tabular.row_info[i].top_space_default = false;
4452 tabular.row_info[i].top_space = len;
4455 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4456 tabular.row_info[i].top_space_default = false;
4457 tabular.row_info[i].top_space = len;
4462 case Tabular::SET_BOTTOM_SPACE: {
4464 if (value == "default")
4465 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4466 tabular.row_info[i].bottom_space_default = true;
4467 else if (isValidLength(value, &len))
4468 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4469 tabular.row_info[i].bottom_space_default = false;
4470 tabular.row_info[i].bottom_space = len;
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4474 tabular.row_info[i].bottom_space_default = false;
4475 tabular.row_info[i].bottom_space = len;
4480 case Tabular::SET_INTERLINE_SPACE: {
4482 if (value == "default")
4483 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4484 tabular.row_info[i].interline_space_default = true;
4485 else if (isValidLength(value, &len))
4486 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4487 tabular.row_info[i].interline_space_default = false;
4488 tabular.row_info[i].interline_space = len;
4491 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4492 tabular.row_info[i].interline_space_default = false;
4493 tabular.row_info[i].interline_space = len;
4498 // dummy stuff just to avoid warnings
4499 case Tabular::LAST_ACTION:
4505 bool InsetTabular::showInsetDialog(BufferView * bv) const
4507 InsetTabularMailer(*this).showDialog(bv);
4512 void InsetTabular::openLayoutDialog(BufferView * bv) const
4514 InsetTabularMailer(*this).showDialog(bv);
4518 bool InsetTabular::copySelection(Cursor & cur)
4520 if (!cur.selection())
4525 getSelection(cur, rs, re, cs, ce);
4527 paste_tabular.reset(new Tabular(tabular));
4529 for (row_type i = 0; i < rs; ++i)
4530 paste_tabular->deleteRow(0);
4532 row_type const rows = re - rs + 1;
4533 while (paste_tabular->rows() > rows)
4534 paste_tabular->deleteRow(rows);
4536 paste_tabular->setTopLine(0, true, true);
4537 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4540 for (col_type i = 0; i < cs; ++i)
4541 paste_tabular->deleteColumn(0);
4543 col_type const columns = ce - cs + 1;
4544 while (paste_tabular->columns() > columns)
4545 paste_tabular->deleteColumn(columns);
4547 paste_tabular->setLeftLine(0, true, true);
4548 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4551 odocstringstream os;
4552 OutputParams const runparams(0);
4553 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4554 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4555 cap::copySelection(cur, os.str());
4557 // mark tabular stack dirty
4558 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4559 // when we (hopefully) have a one-for-all paste mechanism.
4560 // This must be called after cap::copySelection.
4561 dirtyTabularStack(true);
4567 bool InsetTabular::pasteClipboard(Cursor & cur)
4571 col_type const actcol = tabular.column_of_cell(cur.idx());
4572 row_type const actrow = tabular.row_of_cell(cur.idx());
4573 for (row_type r1 = 0, r2 = actrow;
4574 r1 < paste_tabular->rows() && r2 < tabular.rows();
4576 for (col_type c1 = 0, c2 = actcol;
4577 c1 < paste_tabular->columns() && c2 < tabular.columns();
4579 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4580 tabular.isPartOfMultiColumn(r2, c2))
4582 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4586 if (tabular.isPartOfMultiColumn(r2, c2)) {
4590 shared_ptr<InsetText> inset(
4591 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4592 tabular.setCellInset(r2, c2, inset);
4593 // FIXME: change tracking (MG)
4594 inset->setChange(Change(cur.buffer().params().trackChanges ?
4595 Change::INSERTED : Change::UNCHANGED));
4603 void InsetTabular::cutSelection(Cursor & cur)
4605 if (!cur.selection())
4610 getSelection(cur, rs, re, cs, ce);
4611 for (row_type i = rs; i <= re; ++i) {
4612 for (col_type j = cs; j <= ce; ++j) {
4613 shared_ptr<InsetText> t
4614 = cell(tabular.getCellNumber(i, j));
4615 if (cur.buffer().params().trackChanges)
4616 // FIXME: Change tracking (MG)
4617 t->setChange(Change(Change::DELETED));
4623 // cursor position might be invalid now
4624 if (cur.pit() > cur.lastpit())
4625 cur.pit() = cur.lastpit();
4626 if (cur.pos() > cur.lastpos())
4627 cur.pos() = cur.lastpos();
4628 cur.clearSelection();
4632 bool InsetTabular::isRightToLeft(Cursor & cur) const
4634 BOOST_ASSERT(cur.depth() > 1);
4635 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4636 pos_type const parentpos = cur[cur.depth() - 2].pos();
4637 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4638 parentpos).language()->rightToLeft();
4642 void InsetTabular::getSelection(Cursor & cur,
4643 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4645 CursorSlice const & beg = cur.selBegin();
4646 CursorSlice const & end = cur.selEnd();
4647 cs = tabular.column_of_cell(beg.idx());
4648 ce = tabular.column_of_cell(end.idx());
4651 cs = tabular.column_of_cell(end.idx());
4653 ce = tabular.right_column_of_cell(end.idx());
4656 rs = tabular.row_of_cell(beg.idx());
4657 re = tabular.row_of_cell(end.idx());
4663 Text * InsetTabular::getText(int idx) const
4665 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4669 void InsetTabular::setChange(Change const & change)
4671 for (idx_type idx = 0; idx < nargs(); ++idx)
4672 cell(idx)->setChange(change);
4676 void InsetTabular::acceptChanges(BufferParams const & bparams)
4678 for (idx_type idx = 0; idx < nargs(); ++idx)
4679 cell(idx)->acceptChanges(bparams);
4683 void InsetTabular::rejectChanges(BufferParams const & bparams)
4685 for (idx_type idx = 0; idx < nargs(); ++idx)
4686 cell(idx)->rejectChanges(bparams);
4690 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4692 return tabular.getPWidth(cell).zero();
4696 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4699 if (buf.length() <= 0)
4702 Buffer const & buffer = *bv.buffer();
4706 col_type maxCols = 1;
4707 docstring::size_type const len = buf.length();
4708 docstring::size_type p = 0;
4711 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4719 maxCols = max(cols, maxCols);
4725 maxCols = max(cols, maxCols);
4731 paste_tabular.reset(
4732 new Tabular(buffer.params(), rows, maxCols));
4733 loctab = paste_tabular.get();
4735 dirtyTabularStack(true);
4738 cell = bv.cursor().idx();
4739 ocol = tabular.column_of_cell(cell);
4740 row = tabular.row_of_cell(cell);
4743 docstring::size_type op = 0;
4744 idx_type const cells = loctab->getNumberOfCells();
4747 rows = loctab->rows();
4748 col_type const columns = loctab->columns();
4750 while (cell < cells && p < len && row < rows &&
4751 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4757 // we can only set this if we are not too far right
4758 if (cols < columns) {
4759 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4760 Paragraph & par = inset->text_.getPar(0);
4761 Font const font = inset->text_.getFont(buffer, par, 0);
4762 inset->setText(buf.substr(op, p - op), font,
4763 buffer.params().trackChanges);
4769 // we can only set this if we are not too far right
4770 if (cols < columns) {
4771 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4772 Paragraph & par = inset->text_.getPar(0);
4773 Font const font = inset->text_.getFont(buffer, par, 0);
4774 inset->setText(buf.substr(op, p - op), font,
4775 buffer.params().trackChanges);
4780 cell = loctab->getCellNumber(row, cols);
4786 // check for the last cell if there is no trailing '\n'
4787 if (cell < cells && op < len) {
4788 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4789 Paragraph & par = inset->text_.getPar(0);
4790 Font const font = inset->text_.getFont(buffer, par, 0);
4791 inset->setText(buf.substr(op, len - op), font,
4792 buffer.params().trackChanges);
4798 void InsetTabular::addPreview(PreviewLoader & loader) const
4800 row_type const rows = tabular.rows();
4801 col_type const columns = tabular.columns();
4802 for (row_type i = 0; i < rows; ++i) {
4803 for (col_type j = 0; j < columns; ++j)
4804 tabular.getCellInset(i, j)->addPreview(loader);
4809 bool InsetTabular::tablemode(Cursor & cur) const
4811 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4817 string const InsetTabularMailer::name_("tabular");
4819 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4820 : inset_(const_cast<InsetTabular &>(inset))
4824 string const InsetTabularMailer::inset2string(Buffer const &) const
4826 return params2string(inset_);
4830 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4832 istringstream data(in);
4834 lex.setStream(data);
4841 if (!lex || token != name_)
4842 return print_mailer_error("InsetTabularMailer", in, 1,
4845 // This is part of the inset proper that is usually swallowed
4846 // by Buffer::readInset
4848 if (!lex || token != "Tabular")
4849 return print_mailer_error("InsetTabularMailer", in, 2,
4852 Buffer const & buffer = inset.buffer();
4853 inset.read(buffer, lex);
4857 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4860 data << name_ << ' ';
4861 inset.write(inset.buffer(), data);
4862 data << "\\end_inset\n";