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 "BufferParams.h"
25 #include "BufferView.h"
27 #include "CutAndPaste.h"
28 #include "CoordCache.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "OutputParams.h"
41 #include "Paragraph.h"
42 #include "paragraph_funcs.h"
43 #include "ParagraphParameters.h"
46 #include "support/convert.h"
47 #include "support/lstrings.h"
49 #include "frontends/Alert.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Painter.h"
52 #include "frontends/Selection.h"
61 using support::prefixIs;
64 using support::suffixIs;
66 using boost::shared_ptr;
67 using boost::dynamic_pointer_cast;
74 using std::istringstream;
77 using std::ostringstream;
82 #ifndef CXX_GLOBAL_CSTD
86 using cap::dirtyTabularStack;
87 using cap::tabularStackDirty;
88 using cap::saveSelection;
90 using graphics::PreviewLoader;
92 using frontend::Painter;
93 using frontend::Clipboard;
95 namespace Alert = frontend::Alert;
100 int const ADD_TO_HEIGHT = 2;
101 int const ADD_TO_TABULAR_WIDTH = 2;
102 int const default_line_space = 10;
103 int const WIDTH_OF_LINE = 5;
107 boost::scoped_ptr<Tabular> paste_tabular;
110 struct TabularFeature {
111 Tabular::Feature action;
116 TabularFeature tabularFeature[] =
118 { Tabular::APPEND_ROW, "append-row" },
119 { Tabular::APPEND_COLUMN, "append-column" },
120 { Tabular::DELETE_ROW, "delete-row" },
121 { Tabular::DELETE_COLUMN, "delete-column" },
122 { Tabular::COPY_ROW, "copy-row" },
123 { Tabular::COPY_COLUMN, "copy-column" },
124 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
125 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
126 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
127 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
128 { Tabular::ALIGN_LEFT, "align-left" },
129 { Tabular::ALIGN_RIGHT, "align-right" },
130 { Tabular::ALIGN_CENTER, "align-center" },
131 { Tabular::ALIGN_BLOCK, "align-block" },
132 { Tabular::VALIGN_TOP, "valign-top" },
133 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
134 { Tabular::VALIGN_MIDDLE, "valign-middle" },
135 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
136 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
137 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
138 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
139 { Tabular::M_ALIGN_LEFT, "m-align-left" },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
141 { Tabular::M_ALIGN_CENTER, "m-align-center" },
142 { Tabular::M_VALIGN_TOP, "m-valign-top" },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
145 { Tabular::MULTICOLUMN, "multicolumn" },
146 { Tabular::SET_ALL_LINES, "set-all-lines" },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
148 { Tabular::SET_LONGTABULAR, "set-longtabular" },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
150 { Tabular::SET_PWIDTH, "set-pwidth" },
151 { Tabular::SET_MPWIDTH, "set-mpwidth" },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
154 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
155 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
156 { Tabular::SET_USEBOX, "set-usebox" },
157 { Tabular::SET_LTHEAD, "set-lthead" },
158 { Tabular::UNSET_LTHEAD, "unset-lthead" },
159 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
160 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
161 { Tabular::SET_LTFOOT, "set-ltfoot" },
162 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
163 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
164 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
165 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
166 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
167 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
168 { Tabular::SET_BOOKTABS, "set-booktabs" },
169 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
170 { Tabular::SET_TOP_SPACE, "set-top-space" },
171 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
172 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
173 { Tabular::LAST_ACTION, "" }
177 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
179 FeatureEqual(Tabular::Feature feature)
180 : feature_(feature) {}
181 bool operator()(TabularFeature const & tf) const {
182 return tf.action == feature_;
185 Tabular::Feature feature_;
190 string const write_attribute(string const & name, T const & t)
192 string const s = tostr(t);
193 return s.empty() ? s : " " + name + "=\"" + s + "\"";
197 string const write_attribute(string const & name, string const & t)
199 return t.empty() ? t : " " + name + "=\"" + t + "\"";
204 string const write_attribute(string const & name, docstring const & t)
206 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
211 string const write_attribute(string const & name, bool const & b)
213 // we write only true attribute values so we remove a bit of the
214 // file format bloat for tabulars.
215 return b ? write_attribute(name, convert<string>(b)) : string();
220 string const write_attribute(string const & name, int const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
229 string const write_attribute(string const & name, Tabular::idx_type const & i)
231 // we write only true attribute values so we remove a bit of the
232 // file format bloat for tabulars.
233 return i ? write_attribute(name, convert<string>(i)) : string();
238 string const write_attribute(string const & name, LyXLength const & value)
240 // we write only the value if we really have one same reson as above.
241 return value.zero() ? string() : write_attribute(name, value.asString());
245 string const tostr(LyXAlignment const & num)
250 case LYX_ALIGN_BLOCK:
254 case LYX_ALIGN_CENTER:
256 case LYX_ALIGN_RIGHT:
258 case LYX_ALIGN_LAYOUT:
260 case LYX_ALIGN_SPECIAL:
267 string const tostr(Tabular::VAlignment const & num)
270 case Tabular::LYX_VALIGN_TOP:
272 case Tabular::LYX_VALIGN_MIDDLE:
274 case Tabular::LYX_VALIGN_BOTTOM:
281 string const tostr(Tabular::BoxType const & num)
284 case Tabular::BOX_NONE:
286 case Tabular::BOX_PARBOX:
288 case Tabular::BOX_MINIPAGE:
295 // I would have liked a fromstr template a lot better. (Lgb)
296 bool string2type(string const str, LyXAlignment & num)
299 num = LYX_ALIGN_NONE;
300 else if (str == "block")
301 num = LYX_ALIGN_BLOCK;
302 else if (str == "left")
303 num = LYX_ALIGN_LEFT;
304 else if (str == "center")
305 num = LYX_ALIGN_CENTER;
306 else if (str == "right")
307 num = LYX_ALIGN_RIGHT;
314 bool string2type(string const str, Tabular::VAlignment & num)
317 num = Tabular::LYX_VALIGN_TOP;
318 else if (str == "middle" )
319 num = Tabular::LYX_VALIGN_MIDDLE;
320 else if (str == "bottom")
321 num = Tabular::LYX_VALIGN_BOTTOM;
328 bool string2type(string const str, Tabular::BoxType & num)
331 num = Tabular::BOX_NONE;
332 else if (str == "parbox")
333 num = Tabular::BOX_PARBOX;
334 else if (str == "minipage")
335 num = Tabular::BOX_MINIPAGE;
342 bool string2type(string const str, bool & num)
346 else if (str == "false")
354 bool getTokenValue(string const & str, char const * token, string & ret)
357 size_t token_length = strlen(token);
358 string::size_type pos = str.find(token);
360 if (pos == string::npos || pos + token_length + 1 >= str.length()
361 || str[pos + token_length] != '=')
363 pos += token_length + 1;
365 if (ch != '"' && ch != '\'') { // only read till next space
369 while (pos < str.length() - 1 && str[++pos] != ch)
376 bool getTokenValue(string const & str, char const * token, docstring & ret)
379 bool const success = getTokenValue(str, token, tmp);
380 ret = from_utf8(tmp);
385 bool getTokenValue(string const & str, char const * token, int & num)
389 if (!getTokenValue(str, token, tmp))
391 num = convert<int>(tmp);
396 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
399 return getTokenValue(str, token, tmp) && string2type(tmp, num);
403 bool getTokenValue(string const & str, char const * token,
404 Tabular::VAlignment & num)
407 return getTokenValue(str, token, tmp) && string2type(tmp, num);
411 bool getTokenValue(string const & str, char const * token,
412 Tabular::BoxType & num)
415 return getTokenValue(str, token, tmp) && string2type(tmp, num);
419 bool getTokenValue(string const & str, char const * token, bool & flag)
421 // set the flag always to false as this should be the default for bools
422 // not in the file-format.
425 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
429 bool getTokenValue(string const & str, char const * token, LyXLength & len)
431 // set the lenght to be zero() as default as this it should be if not
432 // in the file format.
435 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
439 bool getTokenValue(string const & str, char const * token, LyXLength & len, bool & flag)
444 if (!getTokenValue(str, token, tmp))
446 if (tmp == "default") {
450 return isValidLength(tmp, &len);
454 void l_getline(istream & is, string & str)
457 while (str.empty()) {
459 if (!str.empty() && str[str.length() - 1] == '\r')
460 str.erase(str.length() - 1);
467 string const featureAsString(Tabular::Feature feature)
469 TabularFeature * end = tabularFeature +
470 sizeof(tabularFeature) / sizeof(TabularFeature);
471 TabularFeature * it = std::find_if(tabularFeature, end,
472 FeatureEqual(feature));
473 return (it == end) ? string() : it->feature;
478 /////////////////////////////////////////////////////////////////////
482 /////////////////////////////////////////////////////////////////////
485 Tabular::cellstruct::cellstruct(BufferParams const & bp)
488 multicolumn(Tabular::CELL_NORMAL),
489 alignment(LYX_ALIGN_CENTER),
490 valignment(LYX_VALIGN_TOP),
497 inset(new InsetText(bp))
501 Tabular::cellstruct::cellstruct(cellstruct const & cs)
503 width_of_cell(cs.width_of_cell),
504 multicolumn(cs.multicolumn),
505 alignment(cs.alignment),
506 valignment(cs.valignment),
507 top_line(cs.top_line),
508 bottom_line(cs.bottom_line),
509 left_line(cs.left_line),
510 right_line(cs.right_line),
513 align_special(cs.align_special),
515 inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
519 Tabular::cellstruct &
520 Tabular::cellstruct::operator=(cellstruct cs)
528 Tabular::cellstruct::swap(cellstruct & rhs)
530 std::swap(cellno, rhs.cellno);
531 std::swap(width_of_cell, rhs.width_of_cell);
532 std::swap(multicolumn, rhs.multicolumn);
533 std::swap(alignment, rhs.alignment);
534 std::swap(valignment, rhs.valignment);
535 std::swap(top_line, rhs.top_line);
536 std::swap(bottom_line, rhs.bottom_line);
537 std::swap(left_line, rhs.left_line);
538 std::swap(right_line, rhs.right_line);
539 std::swap(usebox, rhs.usebox);
540 std::swap(rotate, rhs.rotate);
541 std::swap(align_special, rhs.align_special);
542 p_width.swap(rhs.p_width);
543 inset.swap(rhs.inset);
547 Tabular::rowstruct::rowstruct()
552 top_space_default(false),
553 bottom_space_default(false),
554 interline_space_default(false),
563 Tabular::columnstruct::columnstruct()
564 : alignment(LYX_ALIGN_CENTER),
565 valignment(LYX_VALIGN_TOP),
573 Tabular::ltType::ltType()
580 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
581 col_type columns_arg)
583 init(bp, rows_arg, columns_arg);
587 // activates all lines and sets all widths to 0
588 void Tabular::init(BufferParams const & bp, row_type rows_arg,
589 col_type columns_arg)
592 columns_ = columns_arg;
593 row_info = row_vector(rows_);
594 column_info = column_vector(columns_);
595 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
596 row_info.reserve(10);
597 column_info.reserve(10);
598 cell_info.reserve(100);
600 for (row_type i = 0; i < rows_; ++i)
601 cell_info[i].back().right_line = true;
602 row_info.back().bottom_line = true;
603 row_info.front().bottom_line = true;
604 column_info.back().right_line = true;
605 is_long_tabular = false;
607 use_booktabs = false;
611 void Tabular::fixCellNums()
614 for (row_type i = 0; i < rows_; ++i) {
615 for (col_type j = 0; j < columns_; ++j) {
616 // When debugging it can be nice to set
618 cell_info[i][j].inset->setDrawFrame(false);
619 cell_info[i][j].cellno = cellno++;
621 cell_info[i].back().right_line = true;
624 set_row_column_number_info();
628 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
632 row_type const row = row_of_cell(cell);
634 row_vector::iterator rit = row_info.begin() + row;
635 row_info.insert(rit, rowstruct());
636 // now set the values of the row before
637 row_info[row] = row_info[row + 1];
639 cell_vvector old(rows_ - 1);
640 for (row_type i = 0; i < rows_ - 1; ++i)
641 swap(cell_info[i], old[i]);
643 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
645 for (row_type i = 0; i <= row; ++i)
646 swap(cell_info[i], old[i]);
647 for (row_type i = row + 2; i < rows_; ++i)
648 swap(cell_info[i], old[i - 1]);
651 for (col_type j = 0; j < columns_; ++j)
652 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
654 set_row_column_number_info();
658 void Tabular::deleteRow(row_type const row)
660 // Not allowed to delete last row
664 row_info.erase(row_info.begin() + row);
665 cell_info.erase(cell_info.begin() + row);
671 void Tabular::copyRow(BufferParams const & bp, row_type const row)
675 row_info.insert(row_info.begin() + row, row_info[row]);
676 cell_info.insert(cell_info.begin() + row, cell_info[row]);
679 for (col_type j = 0; j < columns_; ++j)
680 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 set_row_column_number_info();
686 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
690 col_type const column = column_of_cell(cell);
691 column_vector::iterator cit = column_info.begin() + column + 1;
692 column_info.insert(cit, columnstruct());
693 // set the column values of the column before
694 column_info[column + 1] = column_info[column];
696 for (row_type i = 0; i < rows_; ++i) {
697 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
699 // care about multicolumns
700 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
701 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
703 if (column + 2 >= columns_
704 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
705 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
708 for (row_type i = 0; i < rows_; ++i) {
709 cell_info[i][column + 1].inset->clear();
711 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
717 void Tabular::deleteColumn(col_type const column)
719 // Not allowed to delete last column
723 column_info.erase(column_info.begin() + column);
724 for (row_type i = 0; i < rows_; ++i)
725 cell_info[i].erase(cell_info[i].begin() + column);
731 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
735 column_info.insert(column_info.begin() + column, column_info[column]);
737 for (row_type i = 0; i < rows_; ++i)
738 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
741 for (row_type i = 0; i < rows_; ++i)
742 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
747 void Tabular::set_row_column_number_info()
750 for (row_type row = 0; row < rows_; ++row) {
751 for (col_type column = 0; column < columns_; ++column) {
752 if (cell_info[row][column].multicolumn
753 != Tabular::CELL_PART_OF_MULTICOLUMN)
755 if (numberofcells == 0)
756 // FIXME: Is this intended?
757 cell_info[row][column].cellno = npos;
759 cell_info[row][column].cellno =
764 rowofcell.resize(numberofcells);
765 columnofcell.resize(numberofcells);
770 c < numberofcells && row < rows_ && column < columns_;) {
772 columnofcell[c] = column;
776 } while (column < columns_ &&
777 cell_info[row][column].multicolumn
778 == Tabular::CELL_PART_OF_MULTICOLUMN);
780 if (column == columns_) {
786 for (row_type row = 0; row < rows_; ++row) {
787 for (col_type column = 0; column < columns_; ++column) {
788 if (isPartOfMultiColumn(row,column))
790 cell_info[row][column].inset->setAutoBreakRows(
791 !getPWidth(getCellNumber(row, column)).zero());
797 Tabular::idx_type Tabular::getNumberOfCells() const
799 return numberofcells;
803 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
805 row_type const row = row_of_cell(cell);
807 for (col_type i = 0; i < columns_; ++i)
808 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
814 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
816 if (!wholerow && isMultiColumn(cell) &&
817 !(use_booktabs && row_of_cell(cell) == 0))
818 return cellinfo_of_cell(cell).top_line;
819 return row_info[row_of_cell(cell)].top_line;
823 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
825 if (!wholerow && isMultiColumn(cell) &&
826 !(use_booktabs && isLastRow(cell)))
827 return cellinfo_of_cell(cell).bottom_line;
828 return row_info[row_of_cell(cell)].bottom_line;
832 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
836 if (!wholecolumn && isMultiColumn(cell) &&
837 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
839 if (cellinfo_of_cell(cell).align_special.empty())
840 return cellinfo_of_cell(cell).left_line;
841 return prefixIs(ltrim(cellinfo_of_cell(cell).align_special), '|');
843 if (column_info[column_of_cell(cell)].align_special.empty())
844 return column_info[column_of_cell(cell)].left_line;
845 return prefixIs(ltrim(column_info[column_of_cell(cell)].align_special), '|');
849 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
853 if (!wholecolumn && isMultiColumn(cell) &&
854 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
856 if (cellinfo_of_cell(cell).align_special.empty())
857 return cellinfo_of_cell(cell).right_line;
858 return suffixIs(rtrim(cellinfo_of_cell(cell).align_special), '|');
860 if (column_info[column_of_cell(cell)].align_special.empty())
861 return column_info[right_column_of_cell(cell)].right_line;
862 return suffixIs(rtrim(column_info[column_of_cell(cell)].align_special), '|');
866 bool Tabular::topAlreadyDrawn(idx_type cell) const
868 row_type row = row_of_cell(cell);
869 if (row > 0 && !getAdditionalHeight(row)) {
870 col_type column = column_of_cell(cell);
873 && cell_info[row][column].multicolumn
874 == Tabular::CELL_PART_OF_MULTICOLUMN)
876 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
877 return row_info[row].bottom_line;
879 return cell_info[row][column].bottom_line;
885 bool Tabular::leftAlreadyDrawn(idx_type cell) const
887 col_type column = column_of_cell(cell);
889 row_type row = row_of_cell(cell);
891 (cell_info[row][column].multicolumn ==
892 Tabular::CELL_PART_OF_MULTICOLUMN));
893 if (getAdditionalWidth(cell_info[row][column].cellno))
895 return rightLine(cell_info[row][column].cellno);
901 bool Tabular::isLastRow(idx_type cell) const
903 return row_of_cell(cell) == rows_ - 1;
907 int Tabular::getAdditionalHeight(row_type row) const
909 if (!row || row >= rows_)
915 for (col_type column = 0; column < columns_ && bottom; ++column) {
916 switch (cell_info[row - 1][column].multicolumn) {
917 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
918 bottom = cell_info[row - 1][column].bottom_line;
920 case Tabular::CELL_NORMAL:
921 bottom = row_info[row - 1].bottom_line;
924 for (col_type column = 0; column < columns_ && top; ++column) {
925 switch (cell_info[row][column].multicolumn) {
926 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
927 top = cell_info[row][column].top_line;
929 case Tabular::CELL_NORMAL:
930 top = row_info[row].top_line;
933 int const interline_space = row_info[row - 1].interline_space_default ?
935 row_info[row - 1].interline_space.inPixels(width_of_tabular);
937 return interline_space + WIDTH_OF_LINE;
938 return interline_space;
942 int Tabular::getAdditionalWidth(idx_type cell) const
944 // internally already set in setWidthOfCell
945 // used to get it back in text.cpp
946 col_type const col = right_column_of_cell(cell);
947 row_type const row = row_of_cell(cell);
948 if (col < columns_ - 1 && rightLine(cell) &&
949 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
951 return WIDTH_OF_LINE;
957 // returns the maximum over all rows
958 int Tabular::getWidthOfColumn(idx_type cell) const
960 col_type const column1 = column_of_cell(cell);
961 col_type const column2 = right_column_of_cell(cell);
963 for (col_type i = column1; i <= column2; ++i)
964 result += column_info[i].width_of_column;
969 int Tabular::getWidthOfTabular() const
971 return width_of_tabular;
975 // returns true if a complete update is necessary, otherwise false
976 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
978 if (!isMultiColumn(cell))
981 row_type const row = row_of_cell(cell);
982 col_type const column1 = column_of_cell(cell);
983 col_type const column2 = right_column_of_cell(cell);
984 int const old_val = cell_info[row][column2].width_of_cell;
986 // first set columns to 0 so we can calculate the right width
987 for (col_type i = column1; i <= column2; ++i) {
988 cell_info[row][i].width_of_cell = 0;
990 // set the width to MAX_WIDTH until width > 0
991 int width = new_width + 2 * WIDTH_OF_LINE;
992 col_type i = column1;
993 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
994 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
995 width -= column_info[i].width_of_column;
998 cell_info[row][i].width_of_cell = width;
1000 if (old_val != cell_info[row][column2].width_of_cell) {
1001 // in this case we have to recalculate all multicolumn cells which
1002 // have this column as one of theirs but not as last one
1003 calculate_width_of_column_NMC(i);
1004 recalculateMulticolumnsOfColumn(i);
1005 calculate_width_of_column(i);
1011 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1013 // the last column does not have to be recalculated because all
1014 // multicolumns will have here there last multicolumn cell which
1015 // always will have the whole rest of the width of the cell.
1016 if (columns_ < 2 || column > (columns_ - 2))
1018 for (row_type row = 0; row < rows_; ++row) {
1019 int mc = cell_info[row][column].multicolumn;
1020 int nmc = cell_info[row][column+1].multicolumn;
1021 // we only have to update multicolumns which do not have this
1022 // column as their last column!
1023 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1024 (mc == CELL_PART_OF_MULTICOLUMN &&
1025 nmc == CELL_PART_OF_MULTICOLUMN))
1027 idx_type const cellno = cell_info[row][column].cellno;
1028 setWidthOfMulticolCell(cellno,
1029 getWidthOfCell(cellno) - 2 * WIDTH_OF_LINE);
1035 void Tabular::setWidthOfCell(idx_type cell, int new_width)
1037 row_type const row = row_of_cell(cell);
1038 col_type const column1 = column_of_cell(cell);
1043 if (rightLine(cell_info[row][column1].cellno, true) &&
1044 column1 < columns_ - 1 &&
1045 leftLine(cell_info[row][column1+1].cellno, true))
1047 add_width = WIDTH_OF_LINE;
1050 if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1053 if (isMultiColumnReal(cell)) {
1054 tmp = setWidthOfMulticolCell(cell, new_width);
1056 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1057 cell_info[row][column1].width_of_cell = width;
1058 tmp = calculate_width_of_column_NMC(column1);
1060 recalculateMulticolumnsOfColumn(column1);
1063 for (col_type i = 0; i < columns_; ++i)
1064 calculate_width_of_column(i);
1065 calculate_width_of_tabular();
1070 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1073 if (!isMultiColumn(cell) || onlycolumn)
1074 column_info[column_of_cell(cell)].alignment = align;
1076 cellinfo_of_cell(cell).alignment = align;
1080 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1083 if (!isMultiColumn(cell) || onlycolumn)
1084 column_info[column_of_cell(cell)].valignment = align;
1086 cellinfo_of_cell(cell).valignment = align;
1093 * Allow line and paragraph breaks for fixed width cells or disallow them,
1094 * merge cell paragraphs and reset layout to standard for variable width
1097 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1099 inset->setAutoBreakRows(fixedWidth);
1103 // merge all paragraphs to one
1104 BufferParams const & bp = cur.bv().buffer()->params();
1105 while (inset->paragraphs().size() > 1)
1106 mergeParagraph(bp, inset->paragraphs(), 0);
1110 // undo information has already been recorded
1111 inset->getText(0)->setLayout(*cur.bv().buffer(), 0, cur.lastpit() + 1,
1112 bp.getLyXTextClass().defaultLayoutName());
1119 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1120 LyXLength const & width)
1122 col_type const j = column_of_cell(cell);
1124 column_info[j].p_width = width;
1125 for (row_type i = 0; i < rows_; ++i) {
1126 idx_type const cell = getCellNumber(i, j);
1127 // because of multicolumns
1128 toggleFixedWidth(cur, getCellInset(cell).get(),
1129 !getPWidth(cell).zero());
1131 // cur paragraph can become invalid after paragraphs were merged
1132 if (cur.pit() > cur.lastpit())
1133 cur.pit() = cur.lastpit();
1134 // cur position can become invalid after newlines were removed
1135 if (cur.pos() > cur.lastpos())
1136 cur.pos() = cur.lastpos();
1140 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1141 LyXLength const & width)
1143 if (!isMultiColumn(cell))
1146 cellinfo_of_cell(cell).p_width = width;
1147 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1148 // cur paragraph can become invalid after paragraphs were merged
1149 if (cur.pit() > cur.lastpit())
1150 cur.pit() = cur.lastpit();
1151 // cur position can become invalid after newlines were removed
1152 if (cur.pos() > cur.lastpos())
1153 cur.pos() = cur.lastpos();
1158 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1159 Tabular::Feature what)
1161 if (what == SET_SPECIAL_MULTI)
1162 cellinfo_of_cell(cell).align_special = special;
1164 column_info[column_of_cell(cell)].align_special = special;
1168 void Tabular::setAllLines(idx_type cell, bool line)
1170 setTopLine(cell, line);
1171 setBottomLine(cell, line);
1172 setRightLine(cell, line);
1173 setLeftLine(cell, line);
1177 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1179 row_type const row = row_of_cell(cell);
1180 if (wholerow || !isMultiColumn(cell))
1181 row_info[row].top_line = line;
1183 cellinfo_of_cell(cell).top_line = line;
1187 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1189 if (wholerow || !isMultiColumn(cell))
1190 row_info[row_of_cell(cell)].bottom_line = line;
1192 cellinfo_of_cell(cell).bottom_line = line;
1196 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1198 if (wholecolumn || !isMultiColumn(cell))
1199 column_info[column_of_cell(cell)].left_line = line;
1201 cellinfo_of_cell(cell).left_line = line;
1205 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1207 if (wholecolumn || !isMultiColumn(cell))
1208 column_info[right_column_of_cell(cell)].right_line = line;
1210 cellinfo_of_cell(cell).right_line = line;
1214 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1216 if (!onlycolumn && isMultiColumn(cell))
1217 return cellinfo_of_cell(cell).alignment;
1218 return column_info[column_of_cell(cell)].alignment;
1223 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1225 if (!onlycolumn && isMultiColumn(cell))
1226 return cellinfo_of_cell(cell).valignment;
1227 return column_info[column_of_cell(cell)].valignment;
1231 LyXLength const Tabular::getPWidth(idx_type cell) const
1233 if (isMultiColumn(cell))
1234 return cellinfo_of_cell(cell).p_width;
1235 return column_info[column_of_cell(cell)].p_width;
1239 LyXLength const Tabular::getColumnPWidth(idx_type cell) const
1241 return column_info[column_of_cell(cell)].p_width;
1245 LyXLength const Tabular::getMColumnPWidth(idx_type cell) const
1247 if (isMultiColumn(cell))
1248 return cellinfo_of_cell(cell).p_width;
1253 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1255 if (what == SET_SPECIAL_MULTI)
1256 return cellinfo_of_cell(cell).align_special;
1257 return column_info[column_of_cell(cell)].align_special;
1261 int Tabular::getWidthOfCell(idx_type cell) const
1263 row_type const row = row_of_cell(cell);
1264 col_type const column1 = column_of_cell(cell);
1265 col_type const column2 = right_column_of_cell(cell);
1267 for (col_type i = column1; i <= column2; ++i)
1268 result += cell_info[row][i].width_of_cell;
1273 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1277 switch (getAlignment(cell)) {
1278 case LYX_ALIGN_CENTER:
1279 x += (getWidthOfColumn(cell) - getWidthOfCell(cell)) / 2;
1281 case LYX_ALIGN_RIGHT:
1282 x += getWidthOfColumn(cell) - getWidthOfCell(cell);
1283 // + getAdditionalWidth(cell);
1286 // LYX_ALIGN_LEFT: nothing :-)
1290 // the LaTeX Way :-(
1296 bool Tabular::isFirstCellInRow(idx_type cell) const
1298 return column_of_cell(cell) == 0;
1302 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1304 if (row > rows_ - 1)
1306 return cell_info[row][0].cellno;
1310 bool Tabular::isLastCellInRow(idx_type cell) const
1312 return right_column_of_cell(cell) == columns_ - 1;
1316 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1318 if (row > rows_ - 1)
1320 return cell_info[row][columns_-1].cellno;
1324 void Tabular::calculate_width_of_column(col_type column)
1327 for (row_type i = 0; i < rows_; ++i)
1328 maximum = max(cell_info[i][column].width_of_cell, maximum);
1329 column_info[column].width_of_column = maximum;
1334 // Calculate the columns regarding ONLY the normal cells and if this
1335 // column is inside a multicolumn cell then use it only if its the last
1336 // column of this multicolumn cell as this gives an added width to the
1337 // column, all the rest should be adapted!
1339 bool Tabular::calculate_width_of_column_NMC(col_type column)
1341 int const old_column_width = column_info[column].width_of_column;
1343 for (row_type i = 0; i < rows_; ++i) {
1344 idx_type cell = getCellNumber(i, column);
1345 bool ismulti = isMultiColumnReal(cell);
1346 if ((!ismulti || column == right_column_of_cell(cell)) &&
1347 cell_info[i][column].width_of_cell > max)
1349 max = cell_info[i][column].width_of_cell;
1352 column_info[column].width_of_column = max;
1353 return column_info[column].width_of_column != old_column_width;
1357 void Tabular::calculate_width_of_tabular()
1359 width_of_tabular = 0;
1360 for (col_type i = 0; i < columns_; ++i)
1361 width_of_tabular += column_info[i].width_of_column;
1365 Tabular::row_type Tabular::row_of_cell(idx_type cell) const
1367 if (cell >= numberofcells)
1371 return rowofcell[cell];
1375 Tabular::col_type Tabular::column_of_cell(idx_type cell) const
1377 if (cell >= numberofcells)
1378 return columns_ - 1;
1381 return columnofcell[cell];
1385 Tabular::col_type Tabular::right_column_of_cell(idx_type cell) const
1387 row_type const row = row_of_cell(cell);
1388 col_type column = column_of_cell(cell);
1389 while (column < columns_ - 1 &&
1390 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1396 void Tabular::write(Buffer const & buf, ostream & os) const
1400 << write_attribute("version", 3)
1401 << write_attribute("rows", rows_)
1402 << write_attribute("columns", columns_)
1404 // global longtable options
1406 << write_attribute("rotate", rotate)
1407 << write_attribute("booktabs", use_booktabs)
1408 << write_attribute("islongtable", is_long_tabular)
1409 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1410 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1411 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1412 << write_attribute("headTopDL", endhead.topDL)
1413 << write_attribute("headBottomDL", endhead.bottomDL)
1414 << write_attribute("footTopDL", endfoot.topDL)
1415 << write_attribute("footBottomDL", endfoot.bottomDL)
1416 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1417 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1418 << write_attribute("lastFootEmpty", endlastfoot.empty)
1420 for (col_type j = 0; j < columns_; ++j) {
1422 << write_attribute("alignment", column_info[j].alignment)
1423 << write_attribute("valignment", column_info[j].valignment)
1424 << write_attribute("leftline", column_info[j].left_line)
1425 << write_attribute("rightline", column_info[j].right_line)
1426 << write_attribute("width", column_info[j].p_width.asString())
1427 << write_attribute("special", column_info[j].align_special)
1430 for (row_type i = 0; i < rows_; ++i) {
1432 << write_attribute("topline", row_info[i].top_line)
1433 << write_attribute("bottomline", row_info[i].bottom_line);
1434 static const string def("default");
1435 if (row_info[i].top_space_default)
1436 os << write_attribute("topspace", def);
1438 os << write_attribute("topspace", row_info[i].top_space);
1439 if (row_info[i].bottom_space_default)
1440 os << write_attribute("bottomspace", def);
1442 os << write_attribute("bottomspace", row_info[i].bottom_space);
1443 if (row_info[i].interline_space_default)
1444 os << write_attribute("interlinespace", def);
1446 os << write_attribute("interlinespace", row_info[i].interline_space);
1447 os << write_attribute("endhead", row_info[i].endhead)
1448 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1449 << write_attribute("endfoot", row_info[i].endfoot)
1450 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1451 << write_attribute("newpage", row_info[i].newpage)
1453 for (col_type j = 0; j < columns_; ++j) {
1455 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1456 << write_attribute("alignment", cell_info[i][j].alignment)
1457 << write_attribute("valignment", cell_info[i][j].valignment)
1458 << write_attribute("topline", cell_info[i][j].top_line)
1459 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1460 << write_attribute("leftline", cell_info[i][j].left_line)
1461 << write_attribute("rightline", cell_info[i][j].right_line)
1462 << write_attribute("rotate", cell_info[i][j].rotate)
1463 << write_attribute("usebox", cell_info[i][j].usebox)
1464 << write_attribute("width", cell_info[i][j].p_width)
1465 << write_attribute("special", cell_info[i][j].align_special)
1467 os << "\\begin_inset ";
1468 cell_info[i][j].inset->write(buf, os);
1469 os << "\n\\end_inset\n"
1474 os << "</lyxtabular>\n";
1478 void Tabular::read(Buffer const & buf, Lexer & lex)
1481 istream & is = lex.getStream();
1483 l_getline(is, line);
1484 if (!prefixIs(line, "<lyxtabular ")
1485 && !prefixIs(line, "<Tabular ")) {
1486 BOOST_ASSERT(false);
1491 if (!getTokenValue(line, "version", version))
1493 BOOST_ASSERT(version >= 2);
1496 if (!getTokenValue(line, "rows", rows_arg))
1499 if (!getTokenValue(line, "columns", columns_arg))
1501 init(buf.params(), rows_arg, columns_arg);
1502 l_getline(is, line);
1503 if (!prefixIs(line, "<features")) {
1504 lyxerr << "Wrong tabular format (expected <features ...> got"
1505 << line << ')' << endl;
1508 getTokenValue(line, "rotate", rotate);
1509 getTokenValue(line, "booktabs", use_booktabs);
1510 getTokenValue(line, "islongtable", is_long_tabular);
1511 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1512 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1513 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1514 getTokenValue(line, "headTopDL", endhead.topDL);
1515 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1516 getTokenValue(line, "footTopDL", endfoot.topDL);
1517 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1518 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1519 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1520 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1522 for (col_type j = 0; j < columns_; ++j) {
1524 if (!prefixIs(line,"<column")) {
1525 lyxerr << "Wrong tabular format (expected <column ...> got"
1526 << line << ')' << endl;
1529 getTokenValue(line, "alignment", column_info[j].alignment);
1530 getTokenValue(line, "valignment", column_info[j].valignment);
1531 getTokenValue(line, "leftline", column_info[j].left_line);
1532 getTokenValue(line, "rightline", column_info[j].right_line);
1533 getTokenValue(line, "width", column_info[j].p_width);
1534 getTokenValue(line, "special", column_info[j].align_special);
1537 for (row_type i = 0; i < rows_; ++i) {
1538 l_getline(is, line);
1539 if (!prefixIs(line, "<row")) {
1540 lyxerr << "Wrong tabular format (expected <row ...> got"
1541 << line << ')' << endl;
1544 getTokenValue(line, "topline", row_info[i].top_line);
1545 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1546 getTokenValue(line, "topspace", row_info[i].top_space,
1547 row_info[i].top_space_default);
1548 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1549 row_info[i].bottom_space_default);
1550 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1551 row_info[i].interline_space_default);
1552 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1553 getTokenValue(line, "endhead", row_info[i].endhead);
1554 getTokenValue(line, "endfoot", row_info[i].endfoot);
1555 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1556 getTokenValue(line, "newpage", row_info[i].newpage);
1557 for (col_type j = 0; j < columns_; ++j) {
1558 l_getline(is, line);
1559 if (!prefixIs(line, "<cell")) {
1560 lyxerr << "Wrong tabular format (expected <cell ...> got"
1561 << line << ')' << endl;
1564 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1565 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1566 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1567 getTokenValue(line, "topline", cell_info[i][j].top_line);
1568 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1569 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1570 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1571 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1572 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1573 getTokenValue(line, "width", cell_info[i][j].p_width);
1574 getTokenValue(line, "special", cell_info[i][j].align_special);
1575 l_getline(is, line);
1576 if (prefixIs(line, "\\begin_inset")) {
1577 cell_info[i][j].inset->read(buf, lex);
1578 l_getline(is, line);
1580 if (!prefixIs(line, "</cell>")) {
1581 lyxerr << "Wrong tabular format (expected </cell> got"
1582 << line << ')' << endl;
1586 l_getline(is, line);
1587 if (!prefixIs(line, "</row>")) {
1588 lyxerr << "Wrong tabular format (expected </row> got"
1589 << line << ')' << endl;
1593 while (!prefixIs(line, "</lyxtabular>")) {
1594 l_getline(is, line);
1596 set_row_column_number_info();
1600 bool Tabular::isMultiColumn(idx_type cell) const
1602 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1606 bool Tabular::isMultiColumnReal(idx_type cell) const
1608 return column_of_cell(cell) != right_column_of_cell(cell) &&
1609 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1613 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1615 return cell_info[row_of_cell(cell)][column_of_cell(cell)];
1619 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1622 cellstruct & cs = cellinfo_of_cell(cell);
1623 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1624 cs.alignment = column_info[column_of_cell(cell)].alignment;
1625 cs.top_line = row_info[row_of_cell(cell)].top_line;
1626 cs.bottom_line = row_info[row_of_cell(cell)].bottom_line;
1627 cs.left_line = column_info[column_of_cell(cell)].left_line;
1628 cs.right_line = column_info[column_of_cell(cell+number-1)].right_line;
1629 for (idx_type i = 1; i < number; ++i) {
1630 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1631 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1632 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1635 set_row_column_number_info();
1639 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1641 row_type const row = row_of_cell(cell);
1642 col_type column = column_of_cell(cell);
1643 idx_type result = 1;
1645 while (column < columns_ &&
1646 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1655 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1657 row_type const row = row_of_cell(cell);
1658 col_type column = column_of_cell(cell);
1660 idx_type result = 0;
1662 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1663 cell_info[row][column].multicolumn = CELL_NORMAL;
1665 while (column < columns_ &&
1666 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1668 cell_info[row][column].multicolumn = CELL_NORMAL;
1673 set_row_column_number_info();
1678 void Tabular::setBookTabs(bool what)
1680 use_booktabs = what;
1684 bool Tabular::useBookTabs() const
1686 return use_booktabs;
1690 void Tabular::setLongTabular(bool what)
1692 is_long_tabular = what;
1696 bool Tabular::isLongTabular() const
1698 return is_long_tabular;
1702 void Tabular::setRotateTabular(bool flag)
1708 bool Tabular::getRotateTabular() const
1714 void Tabular::setRotateCell(idx_type cell, bool flag)
1716 cellinfo_of_cell(cell).rotate = flag;
1720 bool Tabular::getRotateCell(idx_type cell) const
1722 return cellinfo_of_cell(cell).rotate;
1726 bool Tabular::needRotating() const
1730 for (row_type i = 0; i < rows_; ++i)
1731 for (col_type j = 0; j < columns_; ++j)
1732 if (cell_info[i][j].rotate)
1738 bool Tabular::isLastCell(idx_type cell) const
1740 if (cell + 1 < numberofcells)
1746 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1748 if (row_of_cell(cell) > 0)
1749 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1754 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1756 if (row_of_cell(cell) + 1 < rows_)
1757 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1762 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1764 if (row_of_cell(cell) == 0)
1766 if (!isMultiColumn(cell))
1767 return getCellAbove(cell);
1768 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1772 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1774 if (row_of_cell(cell) + 1 >= rows_)
1776 if (!isMultiColumn(cell))
1777 return getCellBelow(cell);
1778 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1782 Tabular::idx_type Tabular::getCellNumber(row_type row,
1783 col_type column) const
1785 BOOST_ASSERT(column != npos && column < columns_ &&
1786 row != npos && row < rows_);
1787 return cell_info[row][column].cellno;
1791 void Tabular::setUsebox(idx_type cell, BoxType type)
1793 cellinfo_of_cell(cell).usebox = type;
1797 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1799 if (column_info[column_of_cell(cell)].p_width.zero() &&
1800 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1802 if (cellinfo_of_cell(cell).usebox > 1)
1803 return cellinfo_of_cell(cell).usebox;
1804 return useParbox(cell);
1809 // This are functions used for the longtable support
1811 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1817 row_info[row].endfirsthead = flag;
1821 row_info[row].endhead = flag;
1826 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1829 hd.set = haveLTHead();
1830 return row_info[row].endhead;
1834 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1837 hd.set = haveLTFirstHead();
1838 return row_info[row].endfirsthead;
1842 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1848 row_info[row].endlastfoot = flag;
1852 row_info[row].endfoot = flag;
1857 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1860 fd.set = haveLTFoot();
1861 return row_info[row].endfoot;
1865 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1868 fd.set = haveLTLastFoot();
1869 return row_info[row].endlastfoot;
1873 void Tabular::setLTNewPage(row_type row, bool what)
1875 row_info[row].newpage = what;
1879 bool Tabular::getLTNewPage(row_type row) const
1881 return row_info[row].newpage;
1885 bool Tabular::haveLTHead() const
1887 for (row_type i = 0; i < rows_; ++i)
1888 if (row_info[i].endhead)
1894 bool Tabular::haveLTFirstHead() const
1896 if (endfirsthead.empty)
1898 for (row_type i = 0; i < rows_; ++i)
1899 if (row_info[i].endfirsthead)
1905 bool Tabular::haveLTFoot() const
1907 for (row_type i = 0; i < rows_; ++i)
1908 if (row_info[i].endfoot)
1914 bool Tabular::haveLTLastFoot() const
1916 if (endlastfoot.empty)
1918 for (row_type i = 0; i < rows_; ++i)
1919 if (row_info[i].endlastfoot)
1925 // end longtable support functions
1927 void Tabular::setAscentOfRow(row_type row, int height)
1929 if (row >= rows_ || row_info[row].ascent_of_row == height)
1931 row_info[row].ascent_of_row = height;
1935 void Tabular::setDescentOfRow(row_type row, int height)
1937 if (row >= rows_ || row_info[row].descent_of_row == height)
1939 row_info[row].descent_of_row = height;
1943 int Tabular::getAscentOfRow(row_type row) const
1947 return row_info[row].ascent_of_row;
1951 int Tabular::getDescentOfRow(row_type row) const
1953 BOOST_ASSERT(row < rows_);
1954 return row_info[row].descent_of_row;
1958 int Tabular::getHeightOfTabular() const
1961 for (row_type row = 0; row < rows_; ++row)
1962 height += getAscentOfRow(row) + getDescentOfRow(row) +
1963 getAdditionalHeight(row);
1968 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1970 BOOST_ASSERT(row < rows_);
1971 BOOST_ASSERT(column < columns_);
1972 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1976 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1978 // FIXME: assert or return 0 as in TeXBottomHLine()?
1979 BOOST_ASSERT(row != npos);
1980 BOOST_ASSERT(row < rows_);
1982 idx_type const fcell = getFirstCellInRow(row);
1983 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1986 for (idx_type i = fcell; i < n; ++i) {
1990 if (use_booktabs && row == 0) {
1992 } else if (tmp == n - fcell) {
1993 os << (use_booktabs ? "\\midrule " : "\\hline ");
1995 for (idx_type i = fcell; i < n; ++i) {
1997 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1998 << column_of_cell(i) + 1
2000 << right_column_of_cell(i) + 1
2012 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2014 // FIXME: return 0 or assert as in TeXTopHLine()?
2015 if (row == npos || row >= rows_)
2018 idx_type const fcell = getFirstCellInRow(row);
2019 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2022 for (idx_type i = fcell; i < n; ++i) {
2026 if (use_booktabs && row == rows_ - 1) {
2027 os << "\\bottomrule";
2028 } else if (tmp == n - fcell) {
2029 os << (use_booktabs ? "\\midrule" : "\\hline");
2031 for (idx_type i = fcell; i < n; ++i) {
2032 if (bottomLine(i)) {
2033 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2034 << column_of_cell(i) + 1
2036 << right_column_of_cell(i) + 1
2048 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2052 if (getRotateCell(cell)) {
2053 os << "\\begin{sideways}\n";
2056 if (isMultiColumn(cell)) {
2057 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2058 if (!cellinfo_of_cell(cell).align_special.empty()) {
2059 os << cellinfo_of_cell(cell).align_special << "}{";
2061 if (leftLine(cell) &&
2062 (isFirstCellInRow(cell) ||
2063 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2064 !rightLine(cell - 1, true))))
2068 if (!getPWidth(cell).zero()) {
2069 switch (getVAlignment(cell)) {
2070 case LYX_VALIGN_TOP:
2073 case LYX_VALIGN_MIDDLE:
2076 case LYX_VALIGN_BOTTOM:
2081 << from_ascii(getPWidth(cell).asLatexString())
2084 switch (getAlignment(cell)) {
2085 case LYX_ALIGN_LEFT:
2088 case LYX_ALIGN_RIGHT:
2096 if (rightLine(cell))
2098 if (((cell + 1) < numberofcells) && !isFirstCellInRow(cell+1) &&
2104 if (getUsebox(cell) == BOX_PARBOX) {
2106 switch (getVAlignment(cell)) {
2107 case LYX_VALIGN_TOP:
2110 case LYX_VALIGN_MIDDLE:
2113 case LYX_VALIGN_BOTTOM:
2117 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2119 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2120 os << "\\begin{minipage}[";
2121 switch (getVAlignment(cell)) {
2122 case LYX_VALIGN_TOP:
2125 case LYX_VALIGN_MIDDLE:
2128 case LYX_VALIGN_BOTTOM:
2132 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2140 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2145 if (getUsebox(cell) == BOX_PARBOX)
2147 else if (getUsebox(cell) == BOX_MINIPAGE) {
2148 os << "%\n\\end{minipage}";
2151 if (isMultiColumn(cell)) {
2154 if (getRotateCell(cell)) {
2155 os << "%\n\\end{sideways}";
2162 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2163 OutputParams const & runparams) const
2165 if (!is_long_tabular)
2169 // output header info
2171 if (endhead.topDL) {
2175 for (row_type i = 0; i < rows_; ++i) {
2176 if (row_info[i].endhead) {
2177 ret += TeXRow(os, i, buf, runparams);
2180 if (endhead.bottomDL) {
2184 os << "\\endhead\n";
2186 if (endfirsthead.empty) {
2187 os << "\\endfirsthead\n";
2191 // output firstheader info
2192 if (haveLTFirstHead()) {
2193 if (endfirsthead.topDL) {
2197 for (row_type i = 0; i < rows_; ++i) {
2198 if (row_info[i].endfirsthead) {
2199 ret += TeXRow(os, i, buf, runparams);
2202 if (endfirsthead.bottomDL) {
2206 os << "\\endfirsthead\n";
2209 // output footer info
2211 if (endfoot.topDL) {
2215 for (row_type i = 0; i < rows_; ++i) {
2216 if (row_info[i].endfoot) {
2217 ret += TeXRow(os, i, buf, runparams);
2220 if (endfoot.bottomDL) {
2224 os << "\\endfoot\n";
2226 if (endlastfoot.empty) {
2227 os << "\\endlastfoot\n";
2231 // output lastfooter info
2232 if (haveLTLastFoot()) {
2233 if (endlastfoot.topDL) {
2237 for (row_type i = 0; i < rows_; ++i) {
2238 if (row_info[i].endlastfoot) {
2239 ret += TeXRow(os, i, buf, runparams);
2242 if (endlastfoot.bottomDL) {
2246 os << "\\endlastfoot\n";
2253 bool Tabular::isValidRow(row_type row) const
2255 if (!is_long_tabular)
2257 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2258 !row_info[row].endfoot && !row_info[row].endlastfoot;
2262 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2263 OutputParams const & runparams) const
2265 idx_type cell = getCellNumber(i, 0);
2266 int ret = TeXTopHLine(os, i);
2267 if (row_info[i].top_space_default) {
2269 os << "\\addlinespace\n";
2271 os << "\\noalign{\\vskip\\doublerulesep}\n";
2273 } else if(!row_info[i].top_space.zero()) {
2275 os << "\\addlinespace["
2276 << from_ascii(row_info[i].top_space.asLatexString())
2279 os << "\\noalign{\\vskip"
2280 << from_ascii(row_info[i].top_space.asLatexString())
2286 for (col_type j = 0; j < columns_; ++j) {
2287 if (isPartOfMultiColumn(i, j))
2289 ret += TeXCellPreamble(os, cell);
2290 shared_ptr<InsetText> inset = getCellInset(cell);
2292 Paragraph const & par = inset->paragraphs().front();
2293 bool rtl = par.isRightToLeftPar(buf.params())
2295 && getPWidth(cell).zero();
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 (!column_info[i].align_special.empty()) {
2365 os << column_info[i].align_special;
2367 if (!use_booktabs && column_info[i].left_line)
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:
2414 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(InsetBase 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), is_deleted_(false)
2890 InsetTabular::InsetTabular(InsetTabular const & tab)
2891 : InsetOld(tab), tabular(tab.tabular),
2892 buffer_(tab.buffer_), scx_(0), is_deleted_(false)
2896 InsetTabular::~InsetTabular()
2898 InsetTabularMailer(*this).hideDialog();
2903 auto_ptr<InsetBase> InsetTabular::doClone() const
2905 return auto_ptr<InsetBase>(new InsetTabular(*this));
2909 Buffer const & InsetTabular::buffer() const
2915 void InsetTabular::buffer(Buffer const * b)
2921 void InsetTabular::write(Buffer const & buf, ostream & os) const
2923 os << "Tabular" << endl;
2924 tabular.write(buf, os);
2928 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2930 bool const old_format = (lex.getString() == "\\LyXTable");
2932 tabular.read(buf, lex);
2938 string token = lex.getString();
2939 while (lex && token != "\\end_inset") {
2941 token = lex.getString();
2944 lex.printError("Missing \\end_inset at this point. "
2950 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2952 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2953 // mi.base.textwidth << "\n";
2955 lyxerr << "InsetTabular::metrics: need bv" << endl;
2956 BOOST_ASSERT(false);
2960 for (idx_type cell = 0; i < tabular.rows(); ++i) {
2963 for (col_type j = 0; j < tabular.columns(); ++j) {
2964 if (tabular.isPartOfMultiColumn(i, j))
2965 // Multicolumn cell, but not first one
2970 if (tabular.cell_info[i][j].multicolumn ==
2971 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2972 p_width = tabular.cellinfo_of_cell(cell).p_width;
2974 p_width = tabular.column_info[j].p_width;
2975 if (!p_width.zero())
2976 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2977 tabular.getCellInset(cell)->metrics(m, dim);
2978 if (!p_width.zero())
2979 dim.wid = m.base.textwidth;
2980 tabular.setWidthOfCell(cell, dim.wid);
2981 if (p_width.zero()) {
2982 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2983 // FIXME there must be a way to get rid of
2984 // the second metrics call
2985 tabular.getCellInset(cell)->metrics(m, dim);
2987 maxAsc = max(maxAsc, dim.asc);
2988 maxDesc = max(maxDesc, dim.des);
2991 int const top_space = tabular.row_info[i].top_space_default ?
2992 default_line_space :
2993 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2994 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
2995 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2996 default_line_space :
2997 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2998 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3001 dim.asc = tabular.getAscentOfRow(0);
3002 dim.des = tabular.getHeightOfTabular() - dim.asc;
3003 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
3004 bool const changed = dim_ != dim;
3010 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3012 setPosCache(pi, x, y);
3014 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3015 BufferView * bv = pi.base.bv;
3017 resetPos(bv->cursor());
3020 x += ADD_TO_TABULAR_WIDTH;
3023 first_visible_cell = Tabular::npos;
3024 for (row_type i = 0; i < tabular.rows(); ++i) {
3026 int const a = tabular.getAscentOfRow(i);
3027 int const d = tabular.getDescentOfRow(i);
3028 idx = tabular.getCellNumber(i, 0);
3029 for (col_type j = 0; j < tabular.columns(); ++j) {
3030 if (tabular.isPartOfMultiColumn(i, j))
3032 if (first_visible_cell == Tabular::npos)
3033 first_visible_cell = idx;
3035 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3036 if (nx + tabular.getWidthOfColumn(idx) < 0
3037 || nx > bv->workWidth()
3039 || y - a > bv->workHeight()) {
3040 pi.pain.setDrawingEnabled(false);
3041 cell(idx)->draw(pi, cx, y);
3042 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3043 pi.pain.setDrawingEnabled(true);
3045 cell(idx)->draw(pi, cx, y);
3046 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3048 nx += tabular.getWidthOfColumn(idx);
3052 if (i + 1 < tabular.rows())
3053 y += d + tabular.getAscentOfRow(i + 1) +
3054 tabular.getAdditionalHeight(i + 1);
3059 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3061 setPosCache(pi, x, y);
3063 Cursor & cur = pi.base.bv->cursor();
3065 x += scx_ + ADD_TO_TABULAR_WIDTH;
3067 // Paint background of current tabular
3068 int const w = tabular.getWidthOfTabular();
3069 int const h = tabular.getHeightOfTabular();
3070 int yy = y - tabular.getAscentOfRow(0);
3071 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3073 if (!cur.selection())
3075 if (!ptr_cmp(&cur.inset(), this))
3081 if (tablemode(cur)) {
3084 getSelection(cur, rs, re, cs, ce);
3085 y -= tabular.getAscentOfRow(0);
3086 for (row_type j = 0; j < tabular.rows(); ++j) {
3087 int const a = tabular.getAscentOfRow(j);
3088 int const h = a + tabular.getDescentOfRow(j);
3090 y += tabular.getAdditionalHeight(j);
3091 for (col_type i = 0; i < tabular.columns(); ++i) {
3092 if (tabular.isPartOfMultiColumn(j, i))
3094 idx_type const cell =
3095 tabular.getCellNumber(j, i);
3096 int const w = tabular.getWidthOfColumn(cell);
3097 if (i >= cs && i <= ce && j >= rs && j <= re)
3098 pi.pain.fillRectangle(xx, y, w, h,
3106 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
3111 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3112 row_type row, idx_type cell, bool erased) const
3114 int x2 = x + tabular.getWidthOfColumn(cell);
3115 bool on_off = false;
3116 Color::color col = Color::tabularline;
3117 Color::color onoffcol = Color::tabularonoffline;
3120 col = Color::strikeout;
3121 onoffcol = Color::strikeout;
3124 if (!tabular.topAlreadyDrawn(cell)) {
3125 on_off = !tabular.topLine(cell);
3126 pain.line(x, y - tabular.getAscentOfRow(row),
3127 x2, y - tabular.getAscentOfRow(row),
3128 on_off ? onoffcol : col,
3129 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 on_off = !tabular.bottomLine(cell);
3132 pain.line(x, y + tabular.getDescentOfRow(row),
3133 x2, y + tabular.getDescentOfRow(row),
3134 on_off ? onoffcol : col,
3135 on_off ? Painter::line_onoffdash : Painter::line_solid);
3136 if (!tabular.leftAlreadyDrawn(cell)) {
3137 on_off = !tabular.leftLine(cell);
3138 pain.line(x, y - tabular.getAscentOfRow(row),
3139 x, y + tabular.getDescentOfRow(row),
3140 on_off ? onoffcol : col,
3141 on_off ? Painter::line_onoffdash : Painter::line_solid);
3143 on_off = !tabular.rightLine(cell);
3144 pain.line(x2 - tabular.getAdditionalWidth(cell),
3145 y - tabular.getAscentOfRow(row),
3146 x2 - tabular.getAdditionalWidth(cell),
3147 y + tabular.getDescentOfRow(row),
3148 on_off ? onoffcol : col,
3149 on_off ? Painter::line_onoffdash : Painter::line_solid);
3153 docstring const InsetTabular::editMessage() const
3155 return _("Opened table");
3159 void InsetTabular::edit(Cursor & cur, bool left)
3161 //lyxerr << "InsetTabular::edit: " << this << endl;
3163 cur.selection() = false;
3166 if (isRightToLeft(cur))
3167 cur.idx() = tabular.getLastCellInRow(0);
3173 if (isRightToLeft(cur))
3174 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
3176 cur.idx() = tabular.getNumberOfCells() - 1;
3178 cur.pos() = cur.lastpos(); // FIXME crude guess
3180 // FIXME: this accesses the position cache before it is initialized
3182 //cur.bv().fitCursor();
3186 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3188 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3189 << "\n cur:" << cur << endl;
3190 CursorSlice sl = cur.top();
3191 Cursor & bvcur = cur.bv().cursor();
3193 switch (cmd.action) {
3195 case LFUN_MOUSE_PRESS:
3196 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3198 if (cmd.button() == mouse_button::button1
3199 || (cmd.button() == mouse_button::button3
3200 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
3201 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
3203 cur.selection() = false;
3204 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3205 cur.bv().mouseSetCursor(cur);
3209 if (cmd.button() == mouse_button::button2) {
3210 if (cap::selection()) {
3211 // See comment in LyXText::dispatch why we
3213 // FIXME This does not use paste_tabular,
3214 // another reason why paste_tabular should go.
3215 cap::copySelectionToStack();
3216 cmd = FuncRequest(LFUN_PASTE, "0");
3218 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
3221 doDispatch(cur, cmd);
3225 case LFUN_MOUSE_MOTION:
3226 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3227 if (cmd.button() == mouse_button::button1) {
3228 // only accept motions to places not deeper nested than the real anchor
3229 if (bvcur.anchor_.hasPart(cur)) {
3230 // only update if selection changes
3231 if (bvcur.idx() == cur.idx() &&
3232 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3234 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3235 bvcur.setCursor(cur);
3236 bvcur.selection() = true;
3242 case LFUN_MOUSE_RELEASE:
3243 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3244 if (cmd.button() == mouse_button::button1) {
3245 if (bvcur.selection()) {
3246 // Bug3238: disable persistent selection for table cells for now
3247 if (tablemode(bvcur))
3248 theSelection().haveSelection(true);
3250 saveSelection(bvcur);
3252 } else if (cmd.button() == mouse_button::button3)
3253 InsetTabularMailer(*this).showDialog(&cur.bv());
3256 case LFUN_CELL_BACKWARD:
3258 cur.selection() = false;
3262 case LFUN_CELL_FORWARD:
3264 cur.selection() = false;
3268 case LFUN_CHAR_FORWARD_SELECT:
3269 case LFUN_CHAR_FORWARD:
3270 cell(cur.idx())->dispatch(cur, cmd);
3271 if (!cur.result().dispatched()) {
3272 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3273 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
3275 if (sl == cur.top())
3276 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3282 case LFUN_CHAR_BACKWARD_SELECT:
3283 case LFUN_CHAR_BACKWARD:
3284 cell(cur.idx())->dispatch(cur, cmd);
3285 if (!cur.result().dispatched()) {
3286 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3287 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
3289 if (sl == cur.top())
3290 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3296 case LFUN_DOWN_SELECT:
3298 cell(cur.idx())->dispatch(cur, cmd);
3299 cur.dispatched(); // override the cell's decision
3300 if (sl == cur.top())
3301 // if our LyXText didn't do anything to the cursor
3302 // then we try to put the cursor into the cell below
3303 // setting also the right targetX.
3304 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
3305 cur.idx() = tabular.getCellBelow(cur.idx());
3307 TextMetrics const & tm =
3308 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3309 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3310 if (cmd.action == LFUN_DOWN_SELECT) {
3311 // Bug3238: disable persistent selection for table cells for now
3313 theSelection().haveSelection(true);
3318 if (sl == cur.top()) {
3319 // we trick it to go to the RIGHT after leaving the
3321 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3326 case LFUN_UP_SELECT:
3328 cell(cur.idx())->dispatch(cur, cmd);
3329 cur.dispatched(); // override the cell's decision
3330 if (sl == cur.top())
3331 // if our LyXText didn't do anything to the cursor
3332 // then we try to put the cursor into the cell above
3333 // setting also the right targetX.
3334 if (tabular.row_of_cell(cur.idx()) != 0) {
3335 cur.idx() = tabular.getCellAbove(cur.idx());
3336 cur.pit() = cur.lastpit();
3337 LyXText const * text = cell(cur.idx())->getText(0);
3338 TextMetrics const & tm = cur.bv().textMetrics(text);
3339 ParagraphMetrics const & pm =
3340 tm.parMetrics(cur.lastpit());
3341 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3342 if (cmd.action == LFUN_UP_SELECT) {
3343 // Bug3238: disable persistent selection for table cells for now
3345 theSelection().haveSelection(true);
3350 if (sl == cur.top()) {
3351 cmd = FuncRequest(LFUN_FINISHED_UP);
3356 // case LFUN_SCREEN_DOWN: {
3357 // //if (hasSelection())
3358 // // cur.selection() = false;
3359 // col_type const col = tabular.column_of_cell(cur.idx());
3360 // int const t = cur.bv().top_y() + cur.bv().height();
3361 // if (t < yo() + tabular.getHeightOfTabular()) {
3362 // cur.bv().scrollDocView(t);
3363 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3365 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3372 // case LFUN_SCREEN_UP: {
3373 // //if (hasSelection())
3374 // // cur.selection() = false;
3375 // col_type const col = tabular.column_of_cell(cur.idx());
3376 // int const t = cur.bv().top_y() + cur.bv().height();
3378 // cur.bv().scrollDocView(t);
3382 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3386 // cur.par() = cur.lastpar();
3387 // cur.pos() = cur.lastpos();
3391 case LFUN_LAYOUT_TABULAR:
3392 InsetTabularMailer(*this).showDialog(&cur.bv());
3395 case LFUN_INSET_DIALOG_UPDATE:
3396 InsetTabularMailer(*this).updateDialog(&cur.bv());
3399 case LFUN_TABULAR_FEATURE:
3400 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3404 // insert file functions
3405 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3406 case LFUN_FILE_INSERT_PLAINTEXT: {
3408 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3409 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3415 if (tablemode(cur)) {
3416 if (copySelection(cur)) {
3417 recordUndoInset(cur, Undo::DELETE);
3422 cell(cur.idx())->dispatch(cur, cmd);
3425 case LFUN_CHAR_DELETE_BACKWARD:
3426 case LFUN_CHAR_DELETE_FORWARD:
3427 if (tablemode(cur)) {
3428 recordUndoInset(cur, Undo::DELETE);
3432 cell(cur.idx())->dispatch(cur, cmd);
3436 if (!cur.selection())
3438 if (tablemode(cur)) {
3442 cell(cur.idx())->dispatch(cur, cmd);
3445 case LFUN_CLIPBOARD_PASTE:
3446 case LFUN_PRIMARY_SELECTION_PASTE: {
3447 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3448 theClipboard().getAsText() :
3449 theSelection().get();
3452 // pass to InsertPlaintextString, but
3453 // only if we have multi-cell content
3454 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3455 if (insertPlaintextString(cur.bv(), clip, false)) {
3456 // content has been replaced,
3457 // so cursor might be invalid
3458 cur.pos() = cur.lastpos();
3459 bvcur.setCursor(cur);
3463 // Let the cell handle normal text
3464 cell(cur.idx())->dispatch(cur, cmd);
3469 if (tabularStackDirty() && theClipboard().isInternal()) {
3470 recordUndoInset(cur, Undo::INSERT);
3471 pasteClipboard(cur);
3474 cell(cur.idx())->dispatch(cur, cmd);
3477 case LFUN_FONT_EMPH:
3478 case LFUN_FONT_BOLD:
3479 case LFUN_FONT_ROMAN:
3480 case LFUN_FONT_NOUN:
3481 case LFUN_FONT_ITAL:
3482 case LFUN_FONT_FRAK:
3483 case LFUN_FONT_CODE:
3484 case LFUN_FONT_SANS:
3485 case LFUN_FONT_FREE_APPLY:
3486 case LFUN_FONT_FREE_UPDATE:
3487 case LFUN_FONT_SIZE:
3488 case LFUN_FONT_UNDERLINE:
3490 case LFUN_WORD_CAPITALIZE:
3491 case LFUN_WORD_UPCASE:
3492 case LFUN_WORD_LOWCASE:
3493 case LFUN_CHARS_TRANSPOSE:
3494 if (tablemode(cur)) {
3497 getSelection(cur, rs, re, cs, ce);
3498 Cursor tmpcur = cur;
3499 for (row_type i = rs; i <= re; ++i) {
3500 for (col_type j = cs; j <= ce; ++j) {
3501 // cursor follows cell:
3502 tmpcur.idx() = tabular.getCellNumber(i, j);
3503 // select this cell only:
3506 tmpcur.resetAnchor();
3507 tmpcur.pit() = tmpcur.lastpit();
3508 tmpcur.pos() = tmpcur.top().lastpos();
3509 tmpcur.setCursor(tmpcur);
3510 tmpcur.setSelection();
3511 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3516 cell(cur.idx())->dispatch(cur, cmd);
3520 // we try to handle this event in the insets dispatch function.
3521 cell(cur.idx())->dispatch(cur, cmd);
3525 // FIXME: this accesses the position cache before it is initialized
3528 InsetTabularMailer(*this).updateDialog(&cur.bv());
3532 // function sets an object as defined in func_status.h:
3533 // states OK, Unknown, Disabled, On, Off.
3534 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3535 FuncStatus & status) const
3537 switch (cmd.action) {
3538 case LFUN_TABULAR_FEATURE: {
3539 int action = Tabular::LAST_ACTION;
3541 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3542 string const tmp = tabularFeature[i].feature;
3543 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3544 action = tabularFeature[i].action;
3548 if (action == Tabular::LAST_ACTION) {
3550 status.unknown(true);
3554 string const argument
3555 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3557 row_type sel_row_start = 0;
3558 row_type sel_row_end = 0;
3560 Tabular::ltType dummyltt;
3563 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
3566 case Tabular::SET_PWIDTH:
3567 case Tabular::SET_MPWIDTH:
3568 case Tabular::SET_SPECIAL_COLUMN:
3569 case Tabular::SET_SPECIAL_MULTI:
3570 case Tabular::APPEND_ROW:
3571 case Tabular::APPEND_COLUMN:
3572 case Tabular::DELETE_ROW:
3573 case Tabular::DELETE_COLUMN:
3574 case Tabular::COPY_ROW:
3575 case Tabular::COPY_COLUMN:
3576 case Tabular::SET_ALL_LINES:
3577 case Tabular::UNSET_ALL_LINES:
3578 case Tabular::SET_TOP_SPACE:
3579 case Tabular::SET_BOTTOM_SPACE:
3580 case Tabular::SET_INTERLINE_SPACE:
3584 case Tabular::MULTICOLUMN:
3585 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3588 case Tabular::M_TOGGLE_LINE_TOP:
3590 case Tabular::TOGGLE_LINE_TOP:
3591 status.setOnOff(tabular.topLine(cur.idx(), flag));
3594 case Tabular::M_TOGGLE_LINE_BOTTOM:
3596 case Tabular::TOGGLE_LINE_BOTTOM:
3597 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3600 case Tabular::M_TOGGLE_LINE_LEFT:
3602 case Tabular::TOGGLE_LINE_LEFT:
3603 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3606 case Tabular::M_TOGGLE_LINE_RIGHT:
3608 case Tabular::TOGGLE_LINE_RIGHT:
3609 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3612 case Tabular::M_ALIGN_LEFT:
3614 case Tabular::ALIGN_LEFT:
3615 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3618 case Tabular::M_ALIGN_RIGHT:
3620 case Tabular::ALIGN_RIGHT:
3621 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3624 case Tabular::M_ALIGN_CENTER:
3626 case Tabular::ALIGN_CENTER:
3627 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3630 case Tabular::ALIGN_BLOCK:
3631 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3632 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3635 case Tabular::M_VALIGN_TOP:
3637 case Tabular::VALIGN_TOP:
3639 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3642 case Tabular::M_VALIGN_BOTTOM:
3644 case Tabular::VALIGN_BOTTOM:
3646 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3649 case Tabular::M_VALIGN_MIDDLE:
3651 case Tabular::VALIGN_MIDDLE:
3653 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3656 case Tabular::SET_LONGTABULAR:
3657 status.setOnOff(tabular.isLongTabular());
3660 case Tabular::UNSET_LONGTABULAR:
3661 status.setOnOff(!tabular.isLongTabular());
3664 case Tabular::SET_ROTATE_TABULAR:
3665 status.setOnOff(tabular.getRotateTabular());
3668 case Tabular::UNSET_ROTATE_TABULAR:
3669 status.setOnOff(!tabular.getRotateTabular());
3672 case Tabular::SET_ROTATE_CELL:
3673 status.setOnOff(tabular.getRotateCell(cur.idx()));
3676 case Tabular::UNSET_ROTATE_CELL:
3677 status.setOnOff(!tabular.getRotateCell(cur.idx()));
3680 case Tabular::SET_USEBOX:
3681 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3684 case Tabular::SET_LTFIRSTHEAD:
3685 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3688 case Tabular::UNSET_LTFIRSTHEAD:
3689 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3692 case Tabular::SET_LTHEAD:
3693 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3696 case Tabular::UNSET_LTHEAD:
3697 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3700 case Tabular::SET_LTFOOT:
3701 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3704 case Tabular::UNSET_LTFOOT:
3705 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3708 case Tabular::SET_LTLASTFOOT:
3709 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3712 case Tabular::UNSET_LTLASTFOOT:
3713 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3716 case Tabular::SET_LTNEWPAGE:
3717 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3720 case Tabular::SET_BOOKTABS:
3721 status.setOnOff(tabular.useBookTabs());
3724 case Tabular::UNSET_BOOKTABS:
3725 status.setOnOff(!tabular.useBookTabs());
3730 status.enabled(false);
3736 // These are only enabled inside tabular
3737 case LFUN_CELL_BACKWARD:
3738 case LFUN_CELL_FORWARD:
3739 status.enabled(true);
3742 // disable these with multiple cells selected
3743 case LFUN_INSET_INSERT:
3744 case LFUN_TABULAR_INSERT:
3745 case LFUN_CHARSTYLE_INSERT:
3746 case LFUN_FLOAT_INSERT:
3747 case LFUN_FLOAT_WIDE_INSERT:
3748 case LFUN_FOOTNOTE_INSERT:
3749 case LFUN_MARGINALNOTE_INSERT:
3750 case LFUN_MATH_INSERT:
3751 case LFUN_MATH_MODE:
3752 case LFUN_MATH_MUTATE:
3753 case LFUN_MATH_DISPLAY:
3754 case LFUN_NOTE_INSERT:
3755 case LFUN_OPTIONAL_INSERT:
3756 case LFUN_BOX_INSERT:
3757 case LFUN_BRANCH_INSERT:
3758 case LFUN_WRAP_INSERT:
3759 case LFUN_ERT_INSERT: {
3760 if (tablemode(cur)) {
3761 status.enabled(false);
3764 return cell(cur.idx())->getStatus(cur, cmd, status);
3767 // disable in non-fixed-width cells
3768 case LFUN_BREAK_LINE:
3769 case LFUN_BREAK_PARAGRAPH:
3770 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3771 case LFUN_BREAK_PARAGRAPH_SKIP: {
3772 if (tabular.getPWidth(cur.idx()).zero()) {
3773 status.enabled(false);
3776 return cell(cur.idx())->getStatus(cur, cmd, status);
3780 if (tabularStackDirty() && theClipboard().isInternal()) {
3781 status.enabled(true);
3784 return cell(cur.idx())->getStatus(cur, cmd, status);
3786 case LFUN_INSET_MODIFY:
3787 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3788 status.enabled(true);
3794 // we try to handle this event in the insets dispatch function.
3795 return cell(cur.idx())->getStatus(cur, cmd, status);
3800 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3801 OutputParams const & runparams) const
3803 return tabular.latex(buf, os, runparams);
3807 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3808 OutputParams const & runparams) const
3810 os << '\n'; // output table on a new line
3811 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3812 tabular.plaintext(buf, os, runparams, dp, false, 0);
3813 return PLAINTEXT_NEWLINE;
3817 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3818 OutputParams const & runparams) const
3821 InsetBase * master = 0;
3823 #ifdef WITH_WARNINGS
3824 #warning Why not pass a proper DocIterator here?
3827 // if the table is inside a float it doesn't need the informaltable
3828 // wrapper. Search for it.
3829 for (master = owner(); master; master = master->owner())
3830 if (master->lyxCode() == InsetBase::FLOAT_CODE)
3835 os << "<informaltable>";
3838 ret += tabular.docbook(buf, os, runparams);
3840 os << "</informaltable>";
3847 void InsetTabular::validate(LaTeXFeatures & features) const
3849 tabular.validate(features);
3853 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3855 return tabular.getCellInset(idx);
3859 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3861 return tabular.getCellInset(idx);
3865 void InsetTabular::cursorPos(BufferView const & bv,
3866 CursorSlice const & sl, bool boundary, int & x, int & y) const
3868 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3870 // y offset correction
3871 int const row = tabular.row_of_cell(sl.idx());
3872 for (int i = 0; i <= row; ++i) {
3874 y += tabular.getAscentOfRow(i);
3875 y += tabular.getAdditionalHeight(i);
3878 y += tabular.getDescentOfRow(i);
3881 // x offset correction
3882 int const col = tabular.column_of_cell(sl.idx());
3883 int idx = tabular.getCellNumber(row, 0);
3884 for (int j = 0; j < col; ++j) {
3885 if (tabular.isPartOfMultiColumn(row, j))
3887 x += tabular.getWidthOfColumn(idx);
3890 x += tabular.getBeginningOfTextInCell(idx);
3891 x += ADD_TO_TABULAR_WIDTH;
3896 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3900 InsetBase const & inset = *tabular.getCellInset(cell);
3901 Point o = bv.coordCache().getInsets().xy(&inset);
3902 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3903 int const xend = xbeg + tabular.getWidthOfColumn(cell);
3904 row_type const row = tabular.row_of_cell(cell);
3905 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
3906 tabular.getAdditionalHeight(row);
3907 int const yend = o.y_ + tabular.getDescentOfRow(row);
3919 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3920 // << " ybeg=" << ybeg << " yend=" << yend
3921 // << " xx=" << xx << " yy=" << yy
3922 // << " dist=" << xx + yy << endl;
3927 InsetBase * InsetTabular::editXY(Cursor & cur, int x, int y)
3929 //lyxerr << "InsetTabular::editXY: " << this << endl;
3930 cur.selection() = false;
3932 cur.idx() = getNearestCell(cur.bv(), x, y);
3934 return cell(cur.idx())->text_.editXY(cur, x, y);
3938 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3940 cur.idx() = getNearestCell(cur.bv(), x, y);
3941 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
3945 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3947 idx_type idx_min = 0;
3948 int dist_min = std::numeric_limits<int>::max();
3949 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3950 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3951 int const d = dist(bv, i, x, y);
3962 int InsetTabular::getCellXPos(idx_type const cell) const
3966 for (; !tabular.isFirstCellInRow(c); --c)
3968 int lx = tabular.getWidthOfColumn(cell);
3969 for (; c < cell; ++c)
3970 lx += tabular.getWidthOfColumn(c);
3972 return lx - tabular.getWidthOfColumn(cell);
3976 void InsetTabular::resetPos(Cursor & cur) const
3978 BufferView & bv = cur.bv();
3979 int const maxwidth = bv.workWidth();
3981 if (&cur.inset() != this) {
3985 int const X2 = maxwidth;
3986 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3987 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3988 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
3991 scx_ = X1 + 20 - x1;
3993 scx_ = X2 - 20 - x2;
3998 cur.updateFlags(Update::Force | Update::FitCursor);
4002 void InsetTabular::moveNextCell(Cursor & cur)
4004 if (isRightToLeft(cur)) {
4005 if (tabular.isFirstCellInRow(cur.idx())) {
4006 row_type const row = tabular.row_of_cell(cur.idx());
4007 if (row == tabular.rows() - 1)
4009 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4016 if (tabular.isLastCell(cur.idx()))
4026 void InsetTabular::movePrevCell(Cursor & cur)
4028 if (isRightToLeft(cur)) {
4029 if (tabular.isLastCellInRow(cur.idx())) {
4030 row_type const row = tabular.row_of_cell(cur.idx());
4033 cur.idx() = tabular.getFirstCellInRow(row);
4034 cur.idx() = tabular.getCellAbove(cur.idx());
4036 if (tabular.isLastCell(cur.idx()))
4041 if (cur.idx() == 0) // first cell
4045 cur.pit() = cur.lastpit();
4046 cur.pos() = cur.lastpos();
4048 // FIXME: this accesses the position cache before it is initialized
4053 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4055 Tabular::Feature action = Tabular::LAST_ACTION;
4058 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4059 string const tmp = tabularFeature[i].feature;
4061 if (tmp == what.substr(0, tmp.length())) {
4062 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4063 //tabularFeatures[i].feature.length()))
4064 action = tabularFeature[i].action;
4068 if (action == Tabular::LAST_ACTION)
4072 ltrim(what.substr(tabularFeature[i].feature.length()));
4073 tabularFeatures(cur, action, val);
4078 static void checkLongtableSpecial(Tabular::ltType & ltt,
4079 string const & special, bool & flag)
4081 if (special == "dl_above") {
4084 } else if (special == "dl_below") {
4085 ltt.bottomDL = flag;
4087 } else if (special == "empty") {
4097 void InsetTabular::tabularFeatures(Cursor & cur,
4098 Tabular::Feature feature, string const & value)
4100 BufferView & bv = cur.bv();
4101 col_type sel_col_start;
4102 col_type sel_col_end;
4103 row_type sel_row_start;
4104 row_type sel_row_end;
4105 bool setLines = false;
4106 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4107 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4111 case Tabular::M_ALIGN_LEFT:
4112 case Tabular::ALIGN_LEFT:
4113 setAlign = LYX_ALIGN_LEFT;
4116 case Tabular::M_ALIGN_RIGHT:
4117 case Tabular::ALIGN_RIGHT:
4118 setAlign = LYX_ALIGN_RIGHT;
4121 case Tabular::M_ALIGN_CENTER:
4122 case Tabular::ALIGN_CENTER:
4123 setAlign = LYX_ALIGN_CENTER;
4126 case Tabular::ALIGN_BLOCK:
4127 setAlign = LYX_ALIGN_BLOCK;
4130 case Tabular::M_VALIGN_TOP:
4131 case Tabular::VALIGN_TOP:
4132 setVAlign = Tabular::LYX_VALIGN_TOP;
4135 case Tabular::M_VALIGN_BOTTOM:
4136 case Tabular::VALIGN_BOTTOM:
4137 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4140 case Tabular::M_VALIGN_MIDDLE:
4141 case Tabular::VALIGN_MIDDLE:
4142 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4149 recordUndoInset(cur, Undo::ATOMIC);
4151 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4152 row_type const row = tabular.row_of_cell(cur.idx());
4153 col_type const column = tabular.column_of_cell(cur.idx());
4155 Tabular::ltType ltt;
4159 case Tabular::SET_PWIDTH: {
4160 LyXLength const len(value);
4161 tabular.setColumnPWidth(cur, cur.idx(), len);
4163 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4164 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4168 case Tabular::SET_MPWIDTH:
4169 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
4172 case Tabular::SET_SPECIAL_COLUMN:
4173 case Tabular::SET_SPECIAL_MULTI:
4174 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4177 case Tabular::APPEND_ROW:
4178 // append the row into the tabular
4179 tabular.appendRow(bv.buffer()->params(), cur.idx());
4182 case Tabular::APPEND_COLUMN:
4183 // append the column into the tabular
4184 tabular.appendColumn(bv.buffer()->params(), cur.idx());
4185 cur.idx() = tabular.getCellNumber(row, column);
4188 case Tabular::DELETE_ROW:
4189 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4190 tabular.deleteRow(sel_row_start);
4191 if (sel_row_start >= tabular.rows())
4193 cur.idx() = tabular.getCellNumber(sel_row_start, column);
4196 cur.selection() = false;
4199 case Tabular::DELETE_COLUMN:
4200 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4201 tabular.deleteColumn(sel_col_start);
4202 if (sel_col_start >= tabular.columns())
4204 cur.idx() = tabular.getCellNumber(row, sel_col_start);
4207 cur.selection() = false;
4210 case Tabular::COPY_ROW:
4211 tabular.copyRow(bv.buffer()->params(), row);
4214 case Tabular::COPY_COLUMN:
4215 tabular.copyColumn(bv.buffer()->params(), column);
4216 cur.idx() = tabular.getCellNumber(row, column);
4219 case Tabular::M_TOGGLE_LINE_TOP:
4221 case Tabular::TOGGLE_LINE_TOP: {
4222 bool lineSet = !tabular.topLine(cur.idx(), flag);
4223 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4224 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4226 tabular.getCellNumber(i, j),
4231 case Tabular::M_TOGGLE_LINE_BOTTOM:
4233 case Tabular::TOGGLE_LINE_BOTTOM: {
4234 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4235 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4236 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4237 tabular.setBottomLine(
4238 tabular.getCellNumber(i, j),
4244 case Tabular::M_TOGGLE_LINE_LEFT:
4246 case Tabular::TOGGLE_LINE_LEFT: {
4247 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4248 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4249 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4250 tabular.setLeftLine(
4251 tabular.getCellNumber(i,j),
4257 case Tabular::M_TOGGLE_LINE_RIGHT:
4259 case Tabular::TOGGLE_LINE_RIGHT: {
4260 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4261 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4262 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4263 tabular.setRightLine(
4264 tabular.getCellNumber(i,j),
4270 case Tabular::M_ALIGN_LEFT:
4271 case Tabular::M_ALIGN_RIGHT:
4272 case Tabular::M_ALIGN_CENTER:
4274 case Tabular::ALIGN_LEFT:
4275 case Tabular::ALIGN_RIGHT:
4276 case Tabular::ALIGN_CENTER:
4277 case Tabular::ALIGN_BLOCK:
4278 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4279 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4280 tabular.setAlignment(
4281 tabular.getCellNumber(i, j),
4286 case Tabular::M_VALIGN_TOP:
4287 case Tabular::M_VALIGN_BOTTOM:
4288 case Tabular::M_VALIGN_MIDDLE:
4290 case Tabular::VALIGN_TOP:
4291 case Tabular::VALIGN_BOTTOM:
4292 case Tabular::VALIGN_MIDDLE:
4293 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4294 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4295 tabular.setVAlignment(
4296 tabular.getCellNumber(i, j),
4300 case Tabular::MULTICOLUMN: {
4301 if (sel_row_start != sel_row_end) {
4302 #ifdef WITH_WARNINGS
4303 #warning Need I say it ? This is horrible.
4306 Alert::error(_("Error setting multicolumn"),
4307 _("You cannot set multicolumn vertically."));
4310 if (!cur.selection()) {
4311 // just multicol for one single cell
4312 // check whether we are completely in a multicol
4313 if (tabular.isMultiColumn(cur.idx()))
4314 tabular.unsetMultiColumn(cur.idx());
4316 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
4319 // we have a selection so this means we just add all this
4320 // cells to form a multicolumn cell
4321 idx_type const s_start = cur.selBegin().idx();
4322 idx_type const s_end = cur.selEnd().idx();
4323 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
4324 cur.idx() = s_start;
4327 cur.selection() = false;
4331 case Tabular::SET_ALL_LINES:
4333 case Tabular::UNSET_ALL_LINES:
4334 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4335 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4336 tabular.setAllLines(
4337 tabular.getCellNumber(i,j), setLines);
4340 case Tabular::SET_LONGTABULAR:
4341 tabular.setLongTabular(true);
4344 case Tabular::UNSET_LONGTABULAR:
4345 tabular.setLongTabular(false);
4348 case Tabular::SET_ROTATE_TABULAR:
4349 tabular.setRotateTabular(true);
4352 case Tabular::UNSET_ROTATE_TABULAR:
4353 tabular.setRotateTabular(false);
4356 case Tabular::SET_ROTATE_CELL:
4357 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4358 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4359 tabular.setRotateCell(
4360 tabular.getCellNumber(i, j), true);
4363 case Tabular::UNSET_ROTATE_CELL:
4364 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4365 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4366 tabular.setRotateCell(
4367 tabular.getCellNumber(i, j), false);
4370 case Tabular::SET_USEBOX: {
4371 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4372 if (val == tabular.getUsebox(cur.idx()))
4373 val = Tabular::BOX_NONE;
4374 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4375 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4376 tabular.setUsebox(tabular.getCellNumber(i, j), val);
4380 case Tabular::UNSET_LTFIRSTHEAD:
4382 case Tabular::SET_LTFIRSTHEAD:
4383 tabular.getRowOfLTFirstHead(row, ltt);
4384 checkLongtableSpecial(ltt, value, flag);
4385 tabular.setLTHead(row, flag, ltt, true);
4388 case Tabular::UNSET_LTHEAD:
4390 case Tabular::SET_LTHEAD:
4391 tabular.getRowOfLTHead(row, ltt);
4392 checkLongtableSpecial(ltt, value, flag);
4393 tabular.setLTHead(row, flag, ltt, false);
4396 case Tabular::UNSET_LTFOOT:
4398 case Tabular::SET_LTFOOT:
4399 tabular.getRowOfLTFoot(row, ltt);
4400 checkLongtableSpecial(ltt, value, flag);
4401 tabular.setLTFoot(row, flag, ltt, false);
4404 case Tabular::UNSET_LTLASTFOOT:
4406 case Tabular::SET_LTLASTFOOT:
4407 tabular.getRowOfLTLastFoot(row, ltt);
4408 checkLongtableSpecial(ltt, value, flag);
4409 tabular.setLTFoot(row, flag, ltt, true);
4412 case Tabular::SET_LTNEWPAGE:
4413 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4416 case Tabular::SET_BOOKTABS:
4417 tabular.setBookTabs(true);
4420 case Tabular::UNSET_BOOKTABS:
4421 tabular.setBookTabs(false);
4424 case Tabular::SET_TOP_SPACE: {
4426 if (value == "default")
4427 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4428 tabular.row_info[i].top_space_default = true;
4429 else if (isValidLength(value, &len))
4430 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4431 tabular.row_info[i].top_space_default = false;
4432 tabular.row_info[i].top_space = len;
4435 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4436 tabular.row_info[i].top_space_default = false;
4437 tabular.row_info[i].top_space = len;
4442 case Tabular::SET_BOTTOM_SPACE: {
4444 if (value == "default")
4445 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4446 tabular.row_info[i].bottom_space_default = true;
4447 else if (isValidLength(value, &len))
4448 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4449 tabular.row_info[i].bottom_space_default = false;
4450 tabular.row_info[i].bottom_space = len;
4453 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4454 tabular.row_info[i].bottom_space_default = false;
4455 tabular.row_info[i].bottom_space = len;
4460 case Tabular::SET_INTERLINE_SPACE: {
4462 if (value == "default")
4463 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4464 tabular.row_info[i].interline_space_default = true;
4465 else if (isValidLength(value, &len))
4466 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4467 tabular.row_info[i].interline_space_default = false;
4468 tabular.row_info[i].interline_space = len;
4471 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4472 tabular.row_info[i].interline_space_default = false;
4473 tabular.row_info[i].interline_space = len;
4478 // dummy stuff just to avoid warnings
4479 case Tabular::LAST_ACTION:
4485 bool InsetTabular::showInsetDialog(BufferView * bv) const
4487 InsetTabularMailer(*this).showDialog(bv);
4492 void InsetTabular::openLayoutDialog(BufferView * bv) const
4494 InsetTabularMailer(*this).showDialog(bv);
4498 bool InsetTabular::copySelection(Cursor & cur)
4500 if (!cur.selection())
4505 getSelection(cur, rs, re, cs, ce);
4507 paste_tabular.reset(new Tabular(tabular));
4509 for (row_type i = 0; i < rs; ++i)
4510 paste_tabular->deleteRow(0);
4512 row_type const rows = re - rs + 1;
4513 while (paste_tabular->rows() > rows)
4514 paste_tabular->deleteRow(rows);
4516 paste_tabular->setTopLine(0, true, true);
4517 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4520 for (col_type i = 0; i < cs; ++i)
4521 paste_tabular->deleteColumn(0);
4523 col_type const columns = ce - cs + 1;
4524 while (paste_tabular->columns() > columns)
4525 paste_tabular->deleteColumn(columns);
4527 paste_tabular->setLeftLine(0, true, true);
4528 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4531 odocstringstream os;
4532 OutputParams const runparams(0);
4533 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4534 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4535 cap::copySelection(cur, os.str());
4537 // mark tabular stack dirty
4538 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4539 // when we (hopefully) have a one-for-all paste mechanism.
4540 // This must be called after cap::copySelection.
4541 dirtyTabularStack(true);
4547 bool InsetTabular::pasteClipboard(Cursor & cur)
4551 col_type const actcol = tabular.column_of_cell(cur.idx());
4552 row_type const actrow = tabular.row_of_cell(cur.idx());
4553 for (row_type r1 = 0, r2 = actrow;
4554 r1 < paste_tabular->rows() && r2 < tabular.rows();
4556 for (col_type c1 = 0, c2 = actcol;
4557 c1 < paste_tabular->columns() && c2 < tabular.columns();
4559 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4560 tabular.isPartOfMultiColumn(r2, c2))
4562 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4566 if (tabular.isPartOfMultiColumn(r2, c2)) {
4570 shared_ptr<InsetText> inset(
4571 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4572 tabular.setCellInset(r2, c2, inset);
4573 // FIXME: change tracking (MG)
4574 inset->setChange(Change(cur.buffer().params().trackChanges ?
4575 Change::INSERTED : Change::UNCHANGED));
4583 void InsetTabular::cutSelection(Cursor & cur)
4585 if (!cur.selection())
4590 getSelection(cur, rs, re, cs, ce);
4591 for (row_type i = rs; i <= re; ++i) {
4592 for (col_type j = cs; j <= ce; ++j) {
4593 shared_ptr<InsetText> t
4594 = cell(tabular.getCellNumber(i, j));
4595 if (cur.buffer().params().trackChanges)
4596 // FIXME: Change tracking (MG)
4597 t->setChange(Change(Change::DELETED));
4603 // cursor position might be invalid now
4604 if (cur.pit() > cur.lastpit())
4605 cur.pit() = cur.lastpit();
4606 if (cur.pos() > cur.lastpos())
4607 cur.pos() = cur.lastpos();
4608 cur.clearSelection();
4609 theSelection().haveSelection(false);
4613 bool InsetTabular::isRightToLeft(Cursor & cur) const
4615 BOOST_ASSERT(cur.depth() > 1);
4616 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4617 pos_type const parentpos = cur[cur.depth() - 2].pos();
4618 return parentpar.getFontSettings(cur.bv().buffer()->params(),
4619 parentpos).language()->rightToLeft();
4623 void InsetTabular::getSelection(Cursor & cur,
4624 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4626 CursorSlice const & beg = cur.selBegin();
4627 CursorSlice const & end = cur.selEnd();
4628 cs = tabular.column_of_cell(beg.idx());
4629 ce = tabular.column_of_cell(end.idx());
4632 cs = tabular.column_of_cell(end.idx());
4634 ce = tabular.right_column_of_cell(end.idx());
4637 rs = tabular.row_of_cell(beg.idx());
4638 re = tabular.row_of_cell(end.idx());
4644 LyXText * InsetTabular::getText(int idx) const
4646 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4650 void InsetTabular::setChange(Change const & change)
4652 for (idx_type idx = 0; idx < nargs(); ++idx)
4653 cell(idx)->setChange(change);
4657 void InsetTabular::acceptChanges(BufferParams const & bparams)
4659 for (idx_type idx = 0; idx < nargs(); ++idx)
4660 cell(idx)->acceptChanges(bparams);
4664 void InsetTabular::rejectChanges(BufferParams const & bparams)
4666 for (idx_type idx = 0; idx < nargs(); ++idx)
4667 cell(idx)->rejectChanges(bparams);
4671 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4673 return tabular.getPWidth(cell).zero();
4677 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4680 if (buf.length() <= 0)
4683 Buffer const & buffer = *bv.buffer();
4687 col_type maxCols = 1;
4688 docstring::size_type const len = buf.length();
4689 docstring::size_type p = 0;
4692 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4700 maxCols = max(cols, maxCols);
4706 maxCols = max(cols, maxCols);
4712 paste_tabular.reset(
4713 new Tabular(buffer.params(), rows, maxCols));
4714 loctab = paste_tabular.get();
4716 dirtyTabularStack(true);
4719 cell = bv.cursor().idx();
4720 ocol = tabular.column_of_cell(cell);
4721 row = tabular.row_of_cell(cell);
4724 docstring::size_type op = 0;
4725 idx_type const cells = loctab->getNumberOfCells();
4728 rows = loctab->rows();
4729 col_type const columns = loctab->columns();
4731 while (cell < cells && p < len && row < rows &&
4732 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4738 // we can only set this if we are not too far right
4739 if (cols < columns) {
4740 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4741 Paragraph & par = inset->text_.getPar(0);
4742 LyXFont const font = inset->text_.getFont(buffer, par, 0);
4743 inset->setText(buf.substr(op, p - op), font,
4744 buffer.params().trackChanges);
4750 // we can only set this if we are not too far right
4751 if (cols < columns) {
4752 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4753 Paragraph & par = inset->text_.getPar(0);
4754 LyXFont const font = inset->text_.getFont(buffer, par, 0);
4755 inset->setText(buf.substr(op, p - op), font,
4756 buffer.params().trackChanges);
4761 cell = loctab->getCellNumber(row, cols);
4767 // check for the last cell if there is no trailing '\n'
4768 if (cell < cells && op < len) {
4769 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4770 Paragraph & par = inset->text_.getPar(0);
4771 LyXFont const font = inset->text_.getFont(buffer, par, 0);
4772 inset->setText(buf.substr(op, len - op), font,
4773 buffer.params().trackChanges);
4779 void InsetTabular::addPreview(PreviewLoader & loader) const
4781 row_type const rows = tabular.rows();
4782 col_type const columns = tabular.columns();
4783 for (row_type i = 0; i < rows; ++i) {
4784 for (col_type j = 0; j < columns; ++j)
4785 tabular.getCellInset(i, j)->addPreview(loader);
4790 bool InsetTabular::tablemode(Cursor & cur) const
4792 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4799 string const InsetTabularMailer::name_("tabular");
4801 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4802 : inset_(const_cast<InsetTabular &>(inset))
4806 string const InsetTabularMailer::inset2string(Buffer const &) const
4808 return params2string(inset_);
4812 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4814 istringstream data(in);
4816 lex.setStream(data);
4823 if (!lex || token != name_)
4824 return print_mailer_error("InsetTabularMailer", in, 1,
4827 // This is part of the inset proper that is usually swallowed
4828 // by Buffer::readInset
4830 if (!lex || token != "Tabular")
4831 return print_mailer_error("InsetTabularMailer", in, 2,
4834 Buffer const & buffer = inset.buffer();
4835 inset.read(buffer, lex);
4839 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4842 data << name_ << ' ';
4843 inset.write(inset.buffer(), data);
4844 data << "\\end_inset\n";