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"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
38 #include "LaTeXFeatures.h"
41 #include "MetricsInfo.h"
42 #include "OutputParams.h"
43 #include "paragraph_funcs.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "TextMetrics.h"
49 #include "support/convert.h"
50 #include "support/lstrings.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
65 using std::istringstream;
68 using std::ostringstream;
73 #ifndef CXX_GLOBAL_CSTD
77 using boost::shared_ptr;
78 using boost::dynamic_pointer_cast;
82 using support::prefixIs;
85 using support::suffixIs;
87 using cap::dirtyTabularStack;
88 using cap::tabularStackDirty;
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::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
158 { Tabular::SET_USEBOX, "set-usebox" },
159 { Tabular::SET_LTHEAD, "set-lthead" },
160 { Tabular::UNSET_LTHEAD, "unset-lthead" },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
163 { Tabular::SET_LTFOOT, "set-ltfoot" },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
168 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
169 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
170 { Tabular::SET_BOOKTABS, "set-booktabs" },
171 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
172 { Tabular::SET_TOP_SPACE, "set-top-space" },
173 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
174 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
175 { Tabular::LAST_ACTION, "" }
179 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
181 FeatureEqual(Tabular::Feature feature)
182 : feature_(feature) {}
183 bool operator()(TabularFeature const & tf) const {
184 return tf.action == feature_;
187 Tabular::Feature feature_;
192 string const write_attribute(string const & name, T const & t)
194 string const s = tostr(t);
195 return s.empty() ? s : " " + name + "=\"" + s + "\"";
199 string const write_attribute(string const & name, string const & t)
201 return t.empty() ? t : " " + name + "=\"" + t + "\"";
206 string const write_attribute(string const & name, docstring const & t)
208 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
213 string const write_attribute(string const & name, bool const & b)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return b ? write_attribute(name, convert<string>(b)) : string();
222 string const write_attribute(string const & name, int const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Tabular::idx_type const & i)
233 // we write only true attribute values so we remove a bit of the
234 // file format bloat for tabulars.
235 return i ? write_attribute(name, convert<string>(i)) : string();
240 string const write_attribute(string const & name, Length const & value)
242 // we write only the value if we really have one same reson as above.
243 return value.zero() ? string() : write_attribute(name, value.asString());
247 string const tostr(LyXAlignment const & num)
252 case LYX_ALIGN_BLOCK:
256 case LYX_ALIGN_CENTER:
258 case LYX_ALIGN_RIGHT:
260 case LYX_ALIGN_LAYOUT:
262 case LYX_ALIGN_SPECIAL:
269 string const tostr(Tabular::VAlignment const & num)
272 case Tabular::LYX_VALIGN_TOP:
274 case Tabular::LYX_VALIGN_MIDDLE:
276 case Tabular::LYX_VALIGN_BOTTOM:
283 string const tostr(Tabular::BoxType const & num)
286 case Tabular::BOX_NONE:
288 case Tabular::BOX_PARBOX:
290 case Tabular::BOX_MINIPAGE:
297 // I would have liked a fromstr template a lot better. (Lgb)
298 bool string2type(string const str, LyXAlignment & num)
301 num = LYX_ALIGN_NONE;
302 else if (str == "block")
303 num = LYX_ALIGN_BLOCK;
304 else if (str == "left")
305 num = LYX_ALIGN_LEFT;
306 else if (str == "center")
307 num = LYX_ALIGN_CENTER;
308 else if (str == "right")
309 num = LYX_ALIGN_RIGHT;
316 bool string2type(string const str, Tabular::VAlignment & num)
319 num = Tabular::LYX_VALIGN_TOP;
320 else if (str == "middle" )
321 num = Tabular::LYX_VALIGN_MIDDLE;
322 else if (str == "bottom")
323 num = Tabular::LYX_VALIGN_BOTTOM;
330 bool string2type(string const str, Tabular::BoxType & num)
333 num = Tabular::BOX_NONE;
334 else if (str == "parbox")
335 num = Tabular::BOX_PARBOX;
336 else if (str == "minipage")
337 num = Tabular::BOX_MINIPAGE;
344 bool string2type(string const str, bool & num)
348 else if (str == "false")
356 bool getTokenValue(string const & str, char const * token, string & ret)
359 size_t token_length = strlen(token);
360 string::size_type pos = str.find(token);
362 if (pos == string::npos || pos + token_length + 1 >= str.length()
363 || str[pos + token_length] != '=')
365 pos += token_length + 1;
367 if (ch != '"' && ch != '\'') { // only read till next space
371 while (pos < str.length() - 1 && str[++pos] != ch)
378 bool getTokenValue(string const & str, char const * token, docstring & ret)
381 bool const success = getTokenValue(str, token, tmp);
382 ret = from_utf8(tmp);
387 bool getTokenValue(string const & str, char const * token, int & num)
391 if (!getTokenValue(str, token, tmp))
393 num = convert<int>(tmp);
398 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
401 return getTokenValue(str, token, tmp) && string2type(tmp, num);
405 bool getTokenValue(string const & str, char const * token,
406 Tabular::VAlignment & num)
409 return getTokenValue(str, token, tmp) && string2type(tmp, num);
413 bool getTokenValue(string const & str, char const * token,
414 Tabular::BoxType & num)
417 return getTokenValue(str, token, tmp) && string2type(tmp, num);
421 bool getTokenValue(string const & str, char const * token, bool & flag)
423 // set the flag always to false as this should be the default for bools
424 // not in the file-format.
427 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
431 bool getTokenValue(string const & str, char const * token, Length & len)
433 // set the length to be zero() as default as this it should be if not
434 // in the file format.
437 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
441 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
446 if (!getTokenValue(str, token, tmp))
448 if (tmp == "default") {
452 return isValidLength(tmp, &len);
456 void l_getline(istream & is, string & str)
459 while (str.empty()) {
461 if (!str.empty() && str[str.length() - 1] == '\r')
462 str.erase(str.length() - 1);
469 string const featureAsString(Tabular::Feature feature)
471 TabularFeature * end = tabularFeature +
472 sizeof(tabularFeature) / sizeof(TabularFeature);
473 TabularFeature * it = std::find_if(tabularFeature, end,
474 FeatureEqual(feature));
475 return (it == end) ? string() : it->feature;
480 /////////////////////////////////////////////////////////////////////
484 /////////////////////////////////////////////////////////////////////
487 Tabular::cellstruct::cellstruct(BufferParams const & bp)
490 multicolumn(Tabular::CELL_NORMAL),
491 alignment(LYX_ALIGN_CENTER),
492 valignment(LYX_VALIGN_TOP),
499 inset(new InsetText(bp))
503 Tabular::cellstruct::cellstruct(cellstruct const & cs)
506 multicolumn(cs.multicolumn),
507 alignment(cs.alignment),
508 valignment(cs.valignment),
509 top_line(cs.top_line),
510 bottom_line(cs.bottom_line),
511 left_line(cs.left_line),
512 right_line(cs.right_line),
515 align_special(cs.align_special),
517 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
521 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
528 void Tabular::cellstruct::swap(cellstruct & rhs)
530 std::swap(cellno, rhs.cellno);
531 std::swap(width, rhs.width);
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()
586 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
587 col_type columns_arg)
589 init(bp, rows_arg, columns_arg);
593 // activates all lines and sets all widths to 0
594 void Tabular::init(BufferParams const & bp, row_type rows_arg,
595 col_type columns_arg)
597 row_info = row_vector(rows_arg);
598 column_info = column_vector(columns_arg);
599 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
600 row_info.reserve(10);
601 column_info.reserve(10);
602 cell_info.reserve(100);
604 for (row_type i = 0; i < rows_arg; ++i)
605 cell_info[i].back().right_line = true;
606 row_info.back().bottom_line = true;
607 row_info.front().bottom_line = true;
608 column_info.back().right_line = true;
609 is_long_tabular = false;
611 use_booktabs = false;
615 void Tabular::fixCellNums()
618 for (row_type i = 0; i < rowCount(); ++i) {
619 for (col_type j = 0; j < columnCount(); ++j) {
620 // When debugging it can be nice to set
622 cell_info[i][j].inset->setDrawFrame(false);
623 cell_info[i][j].cellno = cellno++;
625 cell_info[i].back().right_line = true;
632 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
634 row_type const row = cellRow(cell);
636 row_vector::iterator rit = row_info.begin() + row;
637 row_info.insert(rit, rowstruct());
638 // now set the values of the row before
639 row_info[row] = row_info[row + 1];
641 row_type const nrows = rowCount();
642 col_type const ncols = columnCount();
644 cell_vvector old(nrows - 1);
645 for (row_type i = 0; i < nrows - 1; ++i)
646 swap(cell_info[i], old[i]);
648 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
650 for (row_type i = 0; i <= row; ++i)
651 swap(cell_info[i], old[i]);
652 for (row_type i = row + 2; i < nrows; ++i)
653 swap(cell_info[i], old[i - 1]);
656 for (col_type j = 0; j < ncols; ++j)
657 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
663 void Tabular::deleteRow(row_type const row)
665 // Not allowed to delete last row
669 row_info.erase(row_info.begin() + row);
670 cell_info.erase(cell_info.begin() + row);
675 void Tabular::copyRow(BufferParams const & bp, row_type const row)
677 row_info.insert(row_info.begin() + row, row_info[row]);
678 cell_info.insert(cell_info.begin() + row, cell_info[row]);
681 for (col_type j = 0; j < columnCount(); ++j)
682 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
688 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
690 col_type const column = cellColumn(cell);
691 col_type const ncols = columnCount();
692 column_vector::iterator cit = column_info.begin() + column + 1;
693 column_info.insert(cit, columnstruct());
694 // set the column values of the column before
695 column_info[column + 1] = column_info[column];
697 for (row_type i = 0; i < rowCount(); ++i) {
698 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
700 // care about multicolumns
701 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
702 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
704 if (column + 2 >= ncols
705 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
706 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
709 for (row_type i = 0; i < rowCount(); ++i) {
710 cell_info[i][column + 1].inset->clear();
712 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
718 void Tabular::deleteColumn(col_type const column)
720 // Not allowed to delete last column
721 if (columnCount() == 1)
724 column_info.erase(column_info.begin() + column);
725 for (row_type i = 0; i < rowCount(); ++i) {
726 // Care about multicolumn cells
727 if (column + 1 < columnCount() &&
728 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
729 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
730 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
732 cell_info[i].erase(cell_info[i].begin() + column);
738 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
740 column_info.insert(column_info.begin() + column, column_info[column]);
742 for (row_type i = 0; i < rowCount(); ++i)
743 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
746 for (row_type i = 0; i < rowCount(); ++i)
747 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
752 void Tabular::updateIndexes()
755 // Count only non-multicol cells plus begin multicol
756 // cells, ignore non-begin multicol cells:
757 for (row_type row = 0; row < rowCount(); ++row) {
758 for (col_type column = 0; column < columnCount(); ++column) {
759 if (cell_info[row][column].multicolumn
760 != Tabular::CELL_PART_OF_MULTICOLUMN)
762 BOOST_ASSERT(numberofcells != 0);
763 cell_info[row][column].cellno =
768 rowofcell.resize(numberofcells);
769 columnofcell.resize(numberofcells);
774 c < numberofcells && row < rowCount() && column < columnCount();) {
776 columnofcell[c] = column;
780 } while (column < columnCount() &&
781 cell_info[row][column].multicolumn
782 == Tabular::CELL_PART_OF_MULTICOLUMN);
784 if (column == columnCount()) {
790 for (row_type row = 0; row < rowCount(); ++row) {
791 for (col_type column = 0; column < columnCount(); ++column) {
792 if (isPartOfMultiColumn(row,column))
794 cell_info[row][column].inset->setAutoBreakRows(
795 !getPWidth(cellIndex(row, column)).zero());
801 Tabular::idx_type Tabular::cellCount() const
803 return numberofcells;
807 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
809 row_type const row = cellRow(cell);
811 for (col_type i = 0; i < columnCount(); ++i)
812 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
818 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
820 if (!wholerow && isMultiColumn(cell) &&
821 !(use_booktabs && cellRow(cell) == 0))
822 return cellinfo_of_cell(cell).top_line;
823 return row_info[cellRow(cell)].top_line;
827 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
829 if (!wholerow && isMultiColumn(cell) &&
830 !(use_booktabs && isLastRow(cell)))
831 return cellinfo_of_cell(cell).bottom_line;
832 return row_info[cellRow(cell)].bottom_line;
836 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
840 if (!wholecolumn && isMultiColumn(cell) &&
841 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
842 return cellinfo_of_cell(cell).left_line;
843 return column_info[cellColumn(cell)].left_line;
847 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
851 if (!wholecolumn && isMultiColumn(cell) &&
852 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
853 return cellinfo_of_cell(cell).right_line;
854 return column_info[cellRightColumn(cell)].right_line;
858 bool Tabular::topAlreadyDrawn(idx_type cell) const
860 row_type row = cellRow(cell);
861 if (row > 0 && !getAdditionalHeight(row)) {
862 col_type column = cellColumn(cell);
865 && cell_info[row][column].multicolumn
866 == Tabular::CELL_PART_OF_MULTICOLUMN)
868 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
869 return row_info[row].bottom_line;
871 return cell_info[row][column].bottom_line;
877 bool Tabular::leftAlreadyDrawn(idx_type cell) const
879 col_type column = cellColumn(cell);
881 row_type row = cellRow(cell);
883 (cell_info[row][column].multicolumn ==
884 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
885 if (getAdditionalWidth(cell_info[row][column].cellno))
887 return rightLine(cell_info[row][column].cellno);
893 bool Tabular::isLastRow(idx_type cell) const
895 return cellRow(cell) == rowCount() - 1;
899 int Tabular::getAdditionalHeight(row_type row) const
901 if (!row || row >= rowCount())
907 for (col_type column = 0; column < columnCount() && bottom; ++column) {
908 switch (cell_info[row - 1][column].multicolumn) {
909 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
910 bottom = cell_info[row - 1][column].bottom_line;
912 case Tabular::CELL_NORMAL:
913 bottom = row_info[row - 1].bottom_line;
916 for (col_type column = 0; column < columnCount() && top; ++column) {
917 switch (cell_info[row][column].multicolumn) {
918 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
919 top = cell_info[row][column].top_line;
921 case Tabular::CELL_NORMAL:
922 top = row_info[row].top_line;
925 int const interline_space = row_info[row - 1].interline_space_default ?
927 row_info[row - 1].interline_space.inPixels(width());
929 return interline_space + WIDTH_OF_LINE;
930 return interline_space;
934 int Tabular::getAdditionalWidth(idx_type cell) const
936 // internally already set in setCellWidth
937 // used to get it back in text.cpp
938 col_type const col = cellRightColumn(cell);
939 row_type const row = cellRow(cell);
940 if (col < columnCount() - 1 && rightLine(cell) &&
941 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
943 return WIDTH_OF_LINE;
949 // returns the maximum over all rows
950 int Tabular::columnWidth(idx_type cell) const
952 col_type const column1 = cellColumn(cell);
953 col_type const column2 = cellRightColumn(cell);
955 for (col_type i = column1; i <= column2; ++i)
956 result += column_info[i].width;
961 int Tabular::width() const
964 for (col_type i = 0; i < columnCount(); ++i)
965 width += column_info[i].width;
970 // returns true if a complete update is necessary, otherwise false
971 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
973 if (!isMultiColumn(cell))
976 row_type const row = cellRow(cell);
977 col_type const column1 = cellColumn(cell);
978 col_type const column2 = cellRightColumn(cell);
979 int const old_val = cell_info[row][column2].width;
981 // first set columns to 0 so we can calculate the right width
982 for (col_type i = column1; i <= column2; ++i) {
983 cell_info[row][i].width = 0;
985 // set the width to MAX_WIDTH until width > 0
986 int width = new_width + 2 * WIDTH_OF_LINE;
987 col_type i = column1;
988 for (; i < column2 && width > column_info[i].width; ++i) {
989 cell_info[row][i].width = column_info[i].width;
990 width -= column_info[i].width;
993 cell_info[row][i].width = width;
995 if (old_val != cell_info[row][column2].width) {
996 // in this case we have to recalculate all multicolumn cells which
997 // have this column as one of theirs but not as last one
998 calculate_width_of_column_NMC(i);
999 recalculateMulticolumnsOfColumn(i);
1000 calculate_width_of_column(i);
1006 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1008 // the last column does not have to be recalculated because all
1009 // multicolumns will have here there last multicolumn cell which
1010 // always will have the whole rest of the width of the cell.
1011 if (columnCount() < 2 || column > (columnCount() - 2))
1013 for (row_type row = 0; row < rowCount(); ++row) {
1014 int mc = cell_info[row][column].multicolumn;
1015 int nmc = cell_info[row][column+1].multicolumn;
1016 // we only have to update multicolumns which do not have this
1017 // column as their last column!
1018 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1019 (mc == CELL_PART_OF_MULTICOLUMN &&
1020 nmc == CELL_PART_OF_MULTICOLUMN))
1022 idx_type const cellno = cell_info[row][column].cellno;
1023 setWidthOfMulticolCell(cellno,
1024 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1030 void Tabular::setCellWidth(idx_type cell, int new_width)
1032 row_type const row = cellRow(cell);
1033 col_type const column1 = cellColumn(cell);
1038 if (rightLine(cell_info[row][column1].cellno, true) &&
1039 column1 < columnCount() - 1 &&
1040 leftLine(cell_info[row][column1+1].cellno, true))
1042 add_width = WIDTH_OF_LINE;
1045 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1048 if (isMultiColumnReal(cell)) {
1049 tmp = setWidthOfMulticolCell(cell, new_width);
1051 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1052 cell_info[row][column1].width = width;
1053 tmp = calculate_width_of_column_NMC(column1);
1055 recalculateMulticolumnsOfColumn(column1);
1058 for (col_type i = 0; i < columnCount(); ++i)
1059 calculate_width_of_column(i);
1064 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1067 if (!isMultiColumn(cell) || onlycolumn)
1068 column_info[cellColumn(cell)].alignment = align;
1070 cellinfo_of_cell(cell).alignment = align;
1074 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1077 if (!isMultiColumn(cell) || onlycolumn)
1078 column_info[cellColumn(cell)].valignment = align;
1080 cellinfo_of_cell(cell).valignment = align;
1087 * Allow line and paragraph breaks for fixed width cells or disallow them,
1088 * merge cell paragraphs and reset layout to standard for variable width
1091 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1093 inset->setAutoBreakRows(fixedWidth);
1097 // merge all paragraphs to one
1098 BufferParams const & bp = cur.bv().buffer().params();
1099 while (inset->paragraphs().size() > 1)
1100 mergeParagraph(bp, inset->paragraphs(), 0);
1104 // undo information has already been recorded
1105 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1106 bp.getTextClass().defaultLayoutName());
1113 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1114 Length const & width)
1116 col_type const j = cellColumn(cell);
1118 column_info[j].p_width = width;
1119 for (row_type i = 0; i < rowCount(); ++i) {
1120 idx_type const cell = cellIndex(i, j);
1121 // because of multicolumns
1122 toggleFixedWidth(cur, getCellInset(cell).get(),
1123 !getPWidth(cell).zero());
1125 // cur paragraph can become invalid after paragraphs were merged
1126 if (cur.pit() > cur.lastpit())
1127 cur.pit() = cur.lastpit();
1128 // cur position can become invalid after newlines were removed
1129 if (cur.pos() > cur.lastpos())
1130 cur.pos() = cur.lastpos();
1134 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1135 Length const & width)
1137 if (!isMultiColumn(cell))
1140 cellinfo_of_cell(cell).p_width = width;
1141 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1142 // cur paragraph can become invalid after paragraphs were merged
1143 if (cur.pit() > cur.lastpit())
1144 cur.pit() = cur.lastpit();
1145 // cur position can become invalid after newlines were removed
1146 if (cur.pos() > cur.lastpos())
1147 cur.pos() = cur.lastpos();
1152 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1153 Tabular::Feature what)
1155 if (what == SET_SPECIAL_MULTI)
1156 cellinfo_of_cell(cell).align_special = special;
1158 column_info[cellColumn(cell)].align_special = special;
1162 void Tabular::setAllLines(idx_type cell, bool line)
1164 setTopLine(cell, line);
1165 setBottomLine(cell, line);
1166 setRightLine(cell, line);
1167 setLeftLine(cell, line);
1171 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1173 row_type const row = cellRow(cell);
1174 if (wholerow || !isMultiColumn(cell))
1175 row_info[row].top_line = line;
1177 cellinfo_of_cell(cell).top_line = line;
1181 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1183 if (wholerow || !isMultiColumn(cell))
1184 row_info[cellRow(cell)].bottom_line = line;
1186 cellinfo_of_cell(cell).bottom_line = line;
1190 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1192 if (wholecolumn || !isMultiColumn(cell))
1193 column_info[cellColumn(cell)].left_line = line;
1195 cellinfo_of_cell(cell).left_line = line;
1199 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1201 if (wholecolumn || !isMultiColumn(cell))
1202 column_info[cellRightColumn(cell)].right_line = line;
1204 cellinfo_of_cell(cell).right_line = line;
1208 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1210 if (!onlycolumn && isMultiColumn(cell))
1211 return cellinfo_of_cell(cell).alignment;
1212 return column_info[cellColumn(cell)].alignment;
1217 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1219 if (!onlycolumn && isMultiColumn(cell))
1220 return cellinfo_of_cell(cell).valignment;
1221 return column_info[cellColumn(cell)].valignment;
1225 Length const Tabular::getPWidth(idx_type cell) const
1227 if (isMultiColumn(cell))
1228 return cellinfo_of_cell(cell).p_width;
1229 return column_info[cellColumn(cell)].p_width;
1233 Length const Tabular::getColumnPWidth(idx_type cell) const
1235 return column_info[cellColumn(cell)].p_width;
1239 Length const Tabular::getMColumnPWidth(idx_type cell) const
1241 if (isMultiColumn(cell))
1242 return cellinfo_of_cell(cell).p_width;
1247 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1249 if (what == SET_SPECIAL_MULTI)
1250 return cellinfo_of_cell(cell).align_special;
1251 return column_info[cellColumn(cell)].align_special;
1255 int Tabular::cellWidth(idx_type cell) const
1257 row_type const row = cellRow(cell);
1258 col_type const column1 = cellColumn(cell);
1259 col_type const column2 = cellRightColumn(cell);
1261 for (col_type i = column1; i <= column2; ++i)
1262 result += cell_info[row][i].width;
1267 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1271 switch (getAlignment(cell)) {
1272 case LYX_ALIGN_CENTER:
1273 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1275 case LYX_ALIGN_RIGHT:
1276 x += columnWidth(cell) - cellWidth(cell);
1277 // + getAdditionalWidth(cell);
1280 // LYX_ALIGN_LEFT: nothing :-)
1284 // the LaTeX Way :-(
1290 bool Tabular::isFirstCellInRow(idx_type cell) const
1292 return cellColumn(cell) == 0;
1296 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1298 if (row > rowCount() - 1)
1299 row = rowCount() - 1;
1300 return cell_info[row][0].cellno;
1304 bool Tabular::isLastCellInRow(idx_type cell) const
1306 return cellRightColumn(cell) == columnCount() - 1;
1310 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1312 if (row > rowCount() - 1)
1313 row = rowCount() - 1;
1314 return cell_info[row][columnCount() - 1].cellno;
1318 void Tabular::calculate_width_of_column(col_type column)
1321 for (row_type i = 0; i < rowCount(); ++i)
1322 maximum = max(cell_info[i][column].width, maximum);
1323 column_info[column].width = maximum;
1328 // Calculate the columns regarding ONLY the normal cells and if this
1329 // column is inside a multicolumn cell then use it only if its the last
1330 // column of this multicolumn cell as this gives an added width to the
1331 // column, all the rest should be adapted!
1333 bool Tabular::calculate_width_of_column_NMC(col_type column)
1335 int const old_column_width = column_info[column].width;
1337 for (row_type i = 0; i < rowCount(); ++i) {
1338 idx_type cell = cellIndex(i, column);
1339 bool ismulti = isMultiColumnReal(cell);
1340 if ((!ismulti || column == cellRightColumn(cell)) &&
1341 cell_info[i][column].width > max)
1343 max = cell_info[i][column].width;
1346 column_info[column].width = max;
1347 return column_info[column].width != old_column_width;
1351 Tabular::row_type Tabular::cellRow(idx_type cell) const
1353 if (cell >= cellCount())
1354 return rowCount() - 1;
1357 return rowofcell[cell];
1361 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1363 if (cell >= cellCount())
1364 return columnCount() - 1;
1367 return columnofcell[cell];
1371 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1373 row_type const row = cellRow(cell);
1374 col_type column = cellColumn(cell);
1375 while (column < columnCount() - 1 &&
1376 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1382 void Tabular::write(Buffer const & buf, ostream & os) const
1386 << write_attribute("version", 3)
1387 << write_attribute("rows", rowCount())
1388 << write_attribute("columns", columnCount())
1390 // global longtable options
1392 << write_attribute("rotate", rotate)
1393 << write_attribute("booktabs", use_booktabs)
1394 << write_attribute("islongtable", is_long_tabular)
1395 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1396 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1397 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1398 << write_attribute("headTopDL", endhead.topDL)
1399 << write_attribute("headBottomDL", endhead.bottomDL)
1400 << write_attribute("footTopDL", endfoot.topDL)
1401 << write_attribute("footBottomDL", endfoot.bottomDL)
1402 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1403 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1404 << write_attribute("lastFootEmpty", endlastfoot.empty)
1406 for (col_type j = 0; j < columnCount(); ++j) {
1408 << write_attribute("alignment", column_info[j].alignment)
1409 << write_attribute("valignment", column_info[j].valignment)
1410 << write_attribute("leftline", column_info[j].left_line)
1411 << write_attribute("rightline", column_info[j].right_line)
1412 << write_attribute("width", column_info[j].p_width.asString())
1413 << write_attribute("special", column_info[j].align_special)
1416 for (row_type i = 0; i < rowCount(); ++i) {
1418 << write_attribute("topline", row_info[i].top_line)
1419 << write_attribute("bottomline", row_info[i].bottom_line);
1420 static const string def("default");
1421 if (row_info[i].top_space_default)
1422 os << write_attribute("topspace", def);
1424 os << write_attribute("topspace", row_info[i].top_space);
1425 if (row_info[i].bottom_space_default)
1426 os << write_attribute("bottomspace", def);
1428 os << write_attribute("bottomspace", row_info[i].bottom_space);
1429 if (row_info[i].interline_space_default)
1430 os << write_attribute("interlinespace", def);
1432 os << write_attribute("interlinespace", row_info[i].interline_space);
1433 os << write_attribute("endhead", row_info[i].endhead)
1434 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1435 << write_attribute("endfoot", row_info[i].endfoot)
1436 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1437 << write_attribute("newpage", row_info[i].newpage)
1439 for (col_type j = 0; j < columnCount(); ++j) {
1441 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1442 << write_attribute("alignment", cell_info[i][j].alignment)
1443 << write_attribute("valignment", cell_info[i][j].valignment)
1444 << write_attribute("topline", cell_info[i][j].top_line)
1445 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1446 << write_attribute("leftline", cell_info[i][j].left_line)
1447 << write_attribute("rightline", cell_info[i][j].right_line)
1448 << write_attribute("rotate", cell_info[i][j].rotate)
1449 << write_attribute("usebox", cell_info[i][j].usebox)
1450 << write_attribute("width", cell_info[i][j].p_width)
1451 << write_attribute("special", cell_info[i][j].align_special)
1453 os << "\\begin_inset ";
1454 cell_info[i][j].inset->write(buf, os);
1455 os << "\n\\end_inset\n"
1460 os << "</lyxtabular>\n";
1464 void Tabular::read(Buffer const & buf, Lexer & lex)
1467 istream & is = lex.getStream();
1469 l_getline(is, line);
1470 if (!prefixIs(line, "<lyxtabular ")
1471 && !prefixIs(line, "<Tabular ")) {
1472 BOOST_ASSERT(false);
1477 if (!getTokenValue(line, "version", version))
1479 BOOST_ASSERT(version >= 2);
1482 if (!getTokenValue(line, "rows", rows_arg))
1485 if (!getTokenValue(line, "columns", columns_arg))
1487 init(buf.params(), rows_arg, columns_arg);
1488 l_getline(is, line);
1489 if (!prefixIs(line, "<features")) {
1490 lyxerr << "Wrong tabular format (expected <features ...> got"
1491 << line << ')' << endl;
1494 getTokenValue(line, "rotate", rotate);
1495 getTokenValue(line, "booktabs", use_booktabs);
1496 getTokenValue(line, "islongtable", is_long_tabular);
1497 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1498 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1499 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1500 getTokenValue(line, "headTopDL", endhead.topDL);
1501 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1502 getTokenValue(line, "footTopDL", endfoot.topDL);
1503 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1504 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1505 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1506 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1508 for (col_type j = 0; j < columnCount(); ++j) {
1510 if (!prefixIs(line,"<column")) {
1511 lyxerr << "Wrong tabular format (expected <column ...> got"
1512 << line << ')' << endl;
1515 getTokenValue(line, "alignment", column_info[j].alignment);
1516 getTokenValue(line, "valignment", column_info[j].valignment);
1517 getTokenValue(line, "leftline", column_info[j].left_line);
1518 getTokenValue(line, "rightline", column_info[j].right_line);
1519 getTokenValue(line, "width", column_info[j].p_width);
1520 getTokenValue(line, "special", column_info[j].align_special);
1523 for (row_type i = 0; i < rowCount(); ++i) {
1524 l_getline(is, line);
1525 if (!prefixIs(line, "<row")) {
1526 lyxerr << "Wrong tabular format (expected <row ...> got"
1527 << line << ')' << endl;
1530 getTokenValue(line, "topline", row_info[i].top_line);
1531 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1532 getTokenValue(line, "topspace", row_info[i].top_space,
1533 row_info[i].top_space_default);
1534 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1535 row_info[i].bottom_space_default);
1536 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1537 row_info[i].interline_space_default);
1538 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1539 getTokenValue(line, "endhead", row_info[i].endhead);
1540 getTokenValue(line, "endfoot", row_info[i].endfoot);
1541 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1542 getTokenValue(line, "newpage", row_info[i].newpage);
1543 for (col_type j = 0; j < columnCount(); ++j) {
1544 l_getline(is, line);
1545 if (!prefixIs(line, "<cell")) {
1546 lyxerr << "Wrong tabular format (expected <cell ...> got"
1547 << line << ')' << endl;
1550 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1551 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1552 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1553 getTokenValue(line, "topline", cell_info[i][j].top_line);
1554 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1555 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1556 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1557 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1558 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1559 getTokenValue(line, "width", cell_info[i][j].p_width);
1560 getTokenValue(line, "special", cell_info[i][j].align_special);
1561 l_getline(is, line);
1562 if (prefixIs(line, "\\begin_inset")) {
1563 cell_info[i][j].inset->read(buf, lex);
1564 l_getline(is, line);
1566 if (!prefixIs(line, "</cell>")) {
1567 lyxerr << "Wrong tabular format (expected </cell> got"
1568 << line << ')' << endl;
1572 l_getline(is, line);
1573 if (!prefixIs(line, "</row>")) {
1574 lyxerr << "Wrong tabular format (expected </row> got"
1575 << line << ')' << endl;
1579 while (!prefixIs(line, "</lyxtabular>")) {
1580 l_getline(is, line);
1586 bool Tabular::isMultiColumn(idx_type cell) const
1588 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1592 bool Tabular::isMultiColumnReal(idx_type cell) const
1594 return cellColumn(cell) != cellRightColumn(cell) &&
1595 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1599 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1601 return cell_info[cellRow(cell)][cellColumn(cell)];
1605 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1608 cellstruct & cs = cellinfo_of_cell(cell);
1609 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1610 cs.alignment = column_info[cellColumn(cell)].alignment;
1611 cs.top_line = row_info[cellRow(cell)].top_line;
1612 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1613 cs.left_line = column_info[cellColumn(cell)].left_line;
1614 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1615 for (idx_type i = 1; i < number; ++i) {
1616 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1617 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1618 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1625 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1627 row_type const row = cellRow(cell);
1628 col_type column = cellColumn(cell);
1629 idx_type result = 1;
1631 while (column < columnCount() &&
1632 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1641 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1643 row_type const row = cellRow(cell);
1644 col_type column = cellColumn(cell);
1646 idx_type result = 0;
1648 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1649 cell_info[row][column].multicolumn = CELL_NORMAL;
1651 while (column < columnCount() &&
1652 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1654 cell_info[row][column].multicolumn = CELL_NORMAL;
1664 void Tabular::setBookTabs(bool what)
1666 use_booktabs = what;
1670 bool Tabular::useBookTabs() const
1672 return use_booktabs;
1676 void Tabular::setLongTabular(bool what)
1678 is_long_tabular = what;
1682 bool Tabular::isLongTabular() const
1684 return is_long_tabular;
1688 void Tabular::setRotateTabular(bool flag)
1694 bool Tabular::getRotateTabular() const
1700 void Tabular::setRotateCell(idx_type cell, bool flag)
1702 cellinfo_of_cell(cell).rotate = flag;
1706 bool Tabular::getRotateCell(idx_type cell) const
1708 return cellinfo_of_cell(cell).rotate;
1712 bool Tabular::needRotating() const
1716 for (row_type i = 0; i < rowCount(); ++i)
1717 for (col_type j = 0; j < columnCount(); ++j)
1718 if (cell_info[i][j].rotate)
1724 bool Tabular::isLastCell(idx_type cell) const
1726 if (cell + 1 < cellCount())
1732 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1734 if (cellRow(cell) > 0)
1735 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1740 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1742 if (cellRow(cell) + 1 < rowCount())
1743 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1748 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1750 if (cellRow(cell) == 0)
1752 if (!isMultiColumn(cell))
1753 return getCellAbove(cell);
1754 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1758 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1760 if (cellRow(cell) + 1 >= rowCount())
1762 if (!isMultiColumn(cell))
1763 return getCellBelow(cell);
1764 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1768 Tabular::idx_type Tabular::cellIndex(row_type row,
1769 col_type column) const
1771 BOOST_ASSERT(column != npos && column < columnCount() &&
1772 row != npos && row < rowCount());
1773 return cell_info[row][column].cellno;
1777 void Tabular::setUsebox(idx_type cell, BoxType type)
1779 cellinfo_of_cell(cell).usebox = type;
1783 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1785 if (column_info[cellColumn(cell)].p_width.zero() &&
1786 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1788 if (cellinfo_of_cell(cell).usebox > 1)
1789 return cellinfo_of_cell(cell).usebox;
1790 return useParbox(cell);
1795 // This are functions used for the longtable support
1797 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1803 row_info[row].endfirsthead = flag;
1807 row_info[row].endhead = flag;
1812 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1815 hd.set = haveLTHead();
1816 return row_info[row].endhead;
1820 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1823 hd.set = haveLTFirstHead();
1824 return row_info[row].endfirsthead;
1828 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1834 row_info[row].endlastfoot = flag;
1838 row_info[row].endfoot = flag;
1843 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1846 fd.set = haveLTFoot();
1847 return row_info[row].endfoot;
1851 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1854 fd.set = haveLTLastFoot();
1855 return row_info[row].endlastfoot;
1859 void Tabular::setLTNewPage(row_type row, bool what)
1861 row_info[row].newpage = what;
1865 bool Tabular::getLTNewPage(row_type row) const
1867 return row_info[row].newpage;
1871 bool Tabular::haveLTHead() const
1873 for (row_type i = 0; i < rowCount(); ++i)
1874 if (row_info[i].endhead)
1880 bool Tabular::haveLTFirstHead() const
1882 if (endfirsthead.empty)
1884 for (row_type i = 0; i < rowCount(); ++i)
1885 if (row_info[i].endfirsthead)
1891 bool Tabular::haveLTFoot() const
1893 for (row_type i = 0; i < rowCount(); ++i)
1894 if (row_info[i].endfoot)
1900 bool Tabular::haveLTLastFoot() const
1902 if (endlastfoot.empty)
1904 for (row_type i = 0; i < rowCount(); ++i)
1905 if (row_info[i].endlastfoot)
1911 // end longtable support functions
1913 void Tabular::setRowAscent(row_type row, int height)
1915 if (row >= rowCount() || row_info[row].ascent == height)
1917 row_info[row].ascent = height;
1921 void Tabular::setRowDescent(row_type row, int height)
1923 if (row >= rowCount() || row_info[row].descent == height)
1925 row_info[row].descent = height;
1929 int Tabular::rowAscent(row_type row) const
1931 if (row >= rowCount())
1933 return row_info[row].ascent;
1937 int Tabular::rowDescent(row_type row) const
1939 BOOST_ASSERT(row < rowCount());
1940 return row_info[row].descent;
1944 int Tabular::height() const
1947 for (row_type row = 0; row < rowCount(); ++row)
1948 height += rowAscent(row) + rowDescent(row) +
1949 getAdditionalHeight(row);
1954 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1956 BOOST_ASSERT(row < rowCount());
1957 BOOST_ASSERT(column < columnCount());
1958 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1962 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1964 // FIXME: assert or return 0 as in TeXBottomHLine()?
1965 BOOST_ASSERT(row != npos);
1966 BOOST_ASSERT(row < rowCount());
1968 idx_type const fcell = getFirstCellInRow(row);
1969 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1972 for (idx_type i = fcell; i < n; ++i) {
1976 if (use_booktabs && row == 0) {
1978 } else if (tmp == n - fcell) {
1979 os << (use_booktabs ? "\\midrule " : "\\hline ");
1981 for (idx_type i = fcell; i < n; ++i) {
1983 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1984 << cellColumn(i) + 1
1986 << cellRightColumn(i) + 1
1998 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2000 // FIXME: return 0 or assert as in TeXTopHLine()?
2001 if (row == npos || row >= rowCount())
2004 idx_type const fcell = getFirstCellInRow(row);
2005 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2008 for (idx_type i = fcell; i < n; ++i) {
2012 if (use_booktabs && row == rowCount() - 1) {
2013 os << "\\bottomrule";
2014 } else if (tmp == n - fcell) {
2015 os << (use_booktabs ? "\\midrule" : "\\hline");
2017 for (idx_type i = fcell; i < n; ++i) {
2018 if (bottomLine(i)) {
2019 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2020 << cellColumn(i) + 1
2022 << cellRightColumn(i) + 1
2034 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2038 if (getRotateCell(cell)) {
2039 os << "\\begin{sideways}\n";
2042 if (isMultiColumn(cell)) {
2043 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2044 if (leftLine(cell) &&
2045 (isFirstCellInRow(cell) ||
2046 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2047 !rightLine(cell - 1, true))))
2049 if (!cellinfo_of_cell(cell).align_special.empty()) {
2050 os << cellinfo_of_cell(cell).align_special;
2052 if (!getPWidth(cell).zero()) {
2053 switch (getVAlignment(cell)) {
2054 case LYX_VALIGN_TOP:
2057 case LYX_VALIGN_MIDDLE:
2060 case LYX_VALIGN_BOTTOM:
2065 << from_ascii(getPWidth(cell).asLatexString())
2068 switch (getAlignment(cell)) {
2069 case LYX_ALIGN_LEFT:
2072 case LYX_ALIGN_RIGHT:
2079 } // end if else !getPWidth
2080 } // end if else !cellinfo_of_cell
2081 if (rightLine(cell))
2083 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2088 if (getUsebox(cell) == BOX_PARBOX) {
2090 switch (getVAlignment(cell)) {
2091 case LYX_VALIGN_TOP:
2094 case LYX_VALIGN_MIDDLE:
2097 case LYX_VALIGN_BOTTOM:
2101 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2103 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2104 os << "\\begin{minipage}[";
2105 switch (getVAlignment(cell)) {
2106 case LYX_VALIGN_TOP:
2109 case LYX_VALIGN_MIDDLE:
2112 case LYX_VALIGN_BOTTOM:
2116 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2124 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2129 if (getUsebox(cell) == BOX_PARBOX)
2131 else if (getUsebox(cell) == BOX_MINIPAGE) {
2132 os << "%\n\\end{minipage}";
2135 if (isMultiColumn(cell)) {
2138 if (getRotateCell(cell)) {
2139 os << "%\n\\end{sideways}";
2146 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2147 OutputParams const & runparams) const
2149 if (!is_long_tabular)
2153 // output header info
2155 if (endhead.topDL) {
2159 for (row_type i = 0; i < rowCount(); ++i) {
2160 if (row_info[i].endhead) {
2161 ret += TeXRow(os, i, buf, runparams);
2164 if (endhead.bottomDL) {
2168 os << "\\endhead\n";
2170 if (endfirsthead.empty) {
2171 os << "\\endfirsthead\n";
2175 // output firstheader info
2176 if (haveLTFirstHead()) {
2177 if (endfirsthead.topDL) {
2181 for (row_type i = 0; i < rowCount(); ++i) {
2182 if (row_info[i].endfirsthead) {
2183 ret += TeXRow(os, i, buf, runparams);
2186 if (endfirsthead.bottomDL) {
2190 os << "\\endfirsthead\n";
2193 // output footer info
2195 if (endfoot.topDL) {
2199 for (row_type i = 0; i < rowCount(); ++i) {
2200 if (row_info[i].endfoot) {
2201 ret += TeXRow(os, i, buf, runparams);
2204 if (endfoot.bottomDL) {
2208 os << "\\endfoot\n";
2210 if (endlastfoot.empty) {
2211 os << "\\endlastfoot\n";
2215 // output lastfooter info
2216 if (haveLTLastFoot()) {
2217 if (endlastfoot.topDL) {
2221 for (row_type i = 0; i < rowCount(); ++i) {
2222 if (row_info[i].endlastfoot) {
2223 ret += TeXRow(os, i, buf, runparams);
2226 if (endlastfoot.bottomDL) {
2230 os << "\\endlastfoot\n";
2237 bool Tabular::isValidRow(row_type row) const
2239 if (!is_long_tabular)
2241 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2242 !row_info[row].endfoot && !row_info[row].endlastfoot;
2246 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2247 OutputParams const & runparams) const
2249 idx_type cell = cellIndex(i, 0);
2250 int ret = TeXTopHLine(os, i);
2251 if (row_info[i].top_space_default) {
2253 os << "\\addlinespace\n";
2255 os << "\\noalign{\\vskip\\doublerulesep}\n";
2257 } else if(!row_info[i].top_space.zero()) {
2259 os << "\\addlinespace["
2260 << from_ascii(row_info[i].top_space.asLatexString())
2263 os << "\\noalign{\\vskip"
2264 << from_ascii(row_info[i].top_space.asLatexString())
2270 for (col_type j = 0; j < columnCount(); ++j) {
2271 if (isPartOfMultiColumn(i, j))
2273 ret += TeXCellPreamble(os, cell);
2274 shared_ptr<InsetText> inset = getCellInset(cell);
2276 Paragraph const & par = inset->paragraphs().front();
2277 bool rtl = par.isRTL(buf.params())
2279 && getPWidth(cell).zero();
2282 if (par.getParLanguage(buf.params())->lang() ==
2285 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2287 // currently, remaning RTL languages are arabic_arabtex and hebrew
2291 ret += inset->latex(buf, os, runparams);
2295 ret += TeXCellPostamble(os, cell);
2296 if (!isLastCellInRow(cell)) { // not last cell in row
2301 os << "\\tabularnewline";
2302 if (row_info[i].bottom_space_default) {
2304 os << "\\addlinespace";
2306 os << "[\\doublerulesep]";
2307 } else if (!row_info[i].bottom_space.zero()) {
2309 os << "\\addlinespace";
2311 << from_ascii(row_info[i].bottom_space.asLatexString())
2316 ret += TeXBottomHLine(os, i);
2317 if (row_info[i].interline_space_default) {
2319 os << "\\addlinespace\n";
2321 os << "\\noalign{\\vskip\\doublerulesep}\n";
2323 } else if (!row_info[i].interline_space.zero()) {
2325 os << "\\addlinespace["
2326 << from_ascii(row_info[i].interline_space.asLatexString())
2329 os << "\\noalign{\\vskip"
2330 << from_ascii(row_info[i].interline_space.asLatexString())
2338 int Tabular::latex(Buffer const & buf, odocstream & os,
2339 OutputParams const & runparams) const
2343 //+---------------------------------------------------------------------
2344 //+ first the opening preamble +
2345 //+---------------------------------------------------------------------
2348 os << "\\begin{sideways}\n";
2351 if (is_long_tabular)
2352 os << "\\begin{longtable}{";
2354 os << "\\begin{tabular}{";
2355 for (col_type i = 0; i < columnCount(); ++i) {
2356 if (!use_booktabs && column_info[i].left_line)
2358 if (!column_info[i].align_special.empty()) {
2359 os << column_info[i].align_special;
2361 if (!column_info[i].p_width.zero()) {
2362 switch (column_info[i].alignment) {
2363 case LYX_ALIGN_LEFT:
2364 os << ">{\\raggedright}";
2366 case LYX_ALIGN_RIGHT:
2367 os << ">{\\raggedleft}";
2369 case LYX_ALIGN_CENTER:
2370 os << ">{\\centering}";
2372 case LYX_ALIGN_NONE:
2373 case LYX_ALIGN_BLOCK:
2374 case LYX_ALIGN_LAYOUT:
2375 case LYX_ALIGN_SPECIAL:
2379 switch (column_info[i].valignment) {
2380 case LYX_VALIGN_TOP:
2383 case LYX_VALIGN_MIDDLE:
2386 case LYX_VALIGN_BOTTOM:
2391 << from_ascii(column_info[i].p_width.asLatexString())
2394 switch (column_info[i].alignment) {
2395 case LYX_ALIGN_LEFT:
2398 case LYX_ALIGN_RIGHT:
2405 } // end if else !column_info[i].p_width
2406 } // end if else !column_info[i].align_special
2407 if (!use_booktabs && column_info[i].right_line)
2413 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2415 //+---------------------------------------------------------------------
2416 //+ the single row and columns (cells) +
2417 //+---------------------------------------------------------------------
2419 for (row_type i = 0; i < rowCount(); ++i) {
2420 if (isValidRow(i)) {
2421 ret += TeXRow(os, i, buf, runparams);
2422 if (is_long_tabular && row_info[i].newpage) {
2423 os << "\\newpage\n";
2429 //+---------------------------------------------------------------------
2430 //+ the closing of the tabular +
2431 //+---------------------------------------------------------------------
2433 if (is_long_tabular)
2434 os << "\\end{longtable}";
2436 os << "\\end{tabular}";
2438 os << "\n\\end{sideways}";
2446 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2447 OutputParams const & runparams) const
2450 idx_type cell = getFirstCellInRow(row);
2453 for (col_type j = 0; j < columnCount(); ++j) {
2454 if (isPartOfMultiColumn(row, j))
2457 os << "<entry align=\"";
2458 switch (getAlignment(cell)) {
2459 case LYX_ALIGN_LEFT:
2462 case LYX_ALIGN_RIGHT:
2470 os << "\" valign=\"";
2471 switch (getVAlignment(cell)) {
2472 case LYX_VALIGN_TOP:
2475 case LYX_VALIGN_BOTTOM:
2478 case LYX_VALIGN_MIDDLE:
2483 if (isMultiColumn(cell)) {
2484 os << " namest=\"col" << j << "\" ";
2485 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2489 ret += getCellInset(cell)->docbook(buf, os, runparams);
2498 int Tabular::docbook(Buffer const & buf, odocstream & os,
2499 OutputParams const & runparams) const
2503 //+---------------------------------------------------------------------
2504 //+ first the opening preamble +
2505 //+---------------------------------------------------------------------
2507 os << "<tgroup cols=\"" << columnCount()
2508 << "\" colsep=\"1\" rowsep=\"1\">\n";
2510 for (col_type i = 0; i < columnCount(); ++i) {
2511 os << "<colspec colname=\"col" << i << "\" align=\"";
2512 switch (column_info[i].alignment) {
2513 case LYX_ALIGN_LEFT:
2516 case LYX_ALIGN_RIGHT:
2524 if (runparams.flavor == OutputParams::XML)
2530 //+---------------------------------------------------------------------
2531 //+ Long Tabular case +
2532 //+---------------------------------------------------------------------
2534 // output header info
2535 if (haveLTHead() || haveLTFirstHead()) {
2538 for (row_type i = 0; i < rowCount(); ++i) {
2539 if (row_info[i].endhead || row_info[i].endfirsthead) {
2540 ret += docbookRow(buf, os, i, runparams);
2546 // output footer info
2547 if (haveLTFoot() || haveLTLastFoot()) {
2550 for (row_type i = 0; i < rowCount(); ++i) {
2551 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2552 ret += docbookRow(buf, os, i, runparams);
2559 //+---------------------------------------------------------------------
2560 //+ the single row and columns (cells) +
2561 //+---------------------------------------------------------------------
2565 for (row_type i = 0; i < rowCount(); ++i) {
2566 if (isValidRow(i)) {
2567 ret += docbookRow(buf, os, i, runparams);
2572 //+---------------------------------------------------------------------
2573 //+ the closing of the tabular +
2574 //+---------------------------------------------------------------------
2583 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2584 vector<unsigned int> const & clen) const
2586 idx_type const fcell = getFirstCellInRow(row);
2587 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2590 for (idx_type i = fcell; i < n; ++i) {
2600 for (idx_type i = fcell; i < n; ++i) {
2611 col_type column = cellColumn(i);
2612 int len = clen[column];
2613 while (column < columnCount() - 1
2614 && isPartOfMultiColumn(row, ++column))
2615 len += clen[column] + 4;
2616 os << docstring(len, ch);
2631 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2632 vector<unsigned int> const & clen) const
2634 idx_type const fcell = getFirstCellInRow(row);
2635 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2638 for (idx_type i = fcell; i < n; ++i) {
2639 if (bottomLine(i)) {
2648 for (idx_type i = fcell; i < n; ++i) {
2649 if (bottomLine(i)) {
2659 col_type column = cellColumn(i);
2660 int len = clen[column];
2661 while (column < columnCount() -1
2662 && isPartOfMultiColumn(row, ++column))
2663 len += clen[column] + 4;
2664 os << docstring(len, ch);
2665 if (bottomLine(i)) {
2679 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2680 OutputParams const & runparams,
2681 idx_type cell, row_type row, col_type column,
2682 vector<unsigned int> const & clen,
2683 bool onlydata) const
2685 odocstringstream sstr;
2686 getCellInset(cell)->plaintext(buf, sstr, runparams);
2698 unsigned int len1 = sstr.str().length();
2699 unsigned int len2 = clen[column];
2700 while (column < columnCount() -1
2701 && isPartOfMultiColumn(row, ++column))
2702 len2 += clen[column] + 4;
2705 switch (getAlignment(cell)) {
2707 case LYX_ALIGN_LEFT:
2710 case LYX_ALIGN_RIGHT:
2714 case LYX_ALIGN_CENTER:
2720 os << docstring(len1, ' ') << sstr.str()
2721 << docstring(len2, ' ');
2723 if (rightLine(cell))
2730 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2731 OutputParams const & runparams, int const depth,
2732 bool onlydata, unsigned char delim) const
2734 // first calculate the width of the single columns
2735 vector<unsigned int> clen(columnCount());
2738 // first all non (real) multicolumn cells!
2739 for (col_type j = 0; j < columnCount(); ++j) {
2741 for (row_type i = 0; i < rowCount(); ++i) {
2742 idx_type cell = cellIndex(i, j);
2743 if (isMultiColumnReal(cell))
2745 odocstringstream sstr;
2746 getCellInset(cell)->plaintext(buf, sstr, runparams);
2747 if (clen[j] < sstr.str().length())
2748 clen[j] = sstr.str().length();
2751 // then all (real) multicolumn cells!
2752 for (col_type j = 0; j < columnCount(); ++j) {
2753 for (row_type i = 0; i < rowCount(); ++i) {
2754 idx_type cell = cellIndex(i, j);
2755 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2757 odocstringstream sstr;
2758 getCellInset(cell)->plaintext(buf, sstr, runparams);
2759 int len = int(sstr.str().length());
2760 idx_type const n = cells_in_multicolumn(cell);
2761 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2763 if (len > int(clen[j + n - 1]))
2764 clen[j + n - 1] = len;
2769 for (row_type i = 0; i < rowCount(); ++i) {
2770 if (!onlydata && plaintextTopHLine(os, i, clen))
2771 os << docstring(depth * 2, ' ');
2772 for (col_type j = 0; j < columnCount(); ++j) {
2773 if (isPartOfMultiColumn(i, j))
2775 if (onlydata && j > 0)
2777 plaintextPrintCell(buf, os, runparams,
2778 cell, i, j, clen, onlydata);
2783 os << docstring(depth * 2, ' ');
2784 if (plaintextBottomHLine(os, i, clen))
2785 os << docstring(depth * 2, ' ');
2791 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2793 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2797 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2798 col_type column) const
2800 return cell_info[row][column].inset;
2804 void Tabular::setCellInset(row_type row, col_type column,
2805 shared_ptr<InsetText> ins) const
2807 cell_info[row][column].inset = ins;
2812 Tabular::getCellFromInset(Inset const * inset) const
2814 // is this inset part of the tabular?
2816 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2817 BOOST_ASSERT(false);
2820 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2821 if (getCellInset(cell).get() == inset) {
2822 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2823 << "cell=" << cell << endl;
2827 // We should have found a cell at this point
2828 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2829 << inset << " not found!" << endl;
2830 BOOST_ASSERT(false);
2836 void Tabular::validate(LaTeXFeatures & features) const
2838 features.require("NeedTabularnewline");
2840 features.require("booktabs");
2841 if (isLongTabular())
2842 features.require("longtable");
2844 features.require("rotating");
2845 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2846 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2847 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2848 features.require("array");
2849 getCellInset(cell)->validate(features);
2854 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2856 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2857 ParagraphList::const_iterator cit = parlist.begin();
2858 ParagraphList::const_iterator end = parlist.end();
2860 for (; cit != end; ++cit)
2861 for (int i = 0; i < cit->size(); ++i)
2862 if (cit->isNewline(i))
2869 /////////////////////////////////////////////////////////////////////
2873 /////////////////////////////////////////////////////////////////////
2875 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2877 : tabular(buf.params(), max(rows, row_type(1)),
2878 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2882 InsetTabular::InsetTabular(InsetTabular const & tab)
2883 : Inset(tab), tabular(tab.tabular),
2884 buffer_(tab.buffer_), scx_(0)
2888 InsetTabular::~InsetTabular()
2890 InsetTabularMailer(*this).hideDialog();
2894 Inset * InsetTabular::clone() const
2896 return new InsetTabular(*this);
2900 Buffer const & InsetTabular::buffer() const
2906 void InsetTabular::buffer(Buffer const * b)
2912 void InsetTabular::write(Buffer const & buf, ostream & os) const
2914 os << "Tabular" << endl;
2915 tabular.write(buf, os);
2919 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2921 bool const old_format = (lex.getString() == "\\LyXTable");
2923 tabular.read(buf, lex);
2929 string token = lex.getString();
2930 while (lex && token != "\\end_inset") {
2932 token = lex.getString();
2935 lex.printError("Missing \\end_inset at this point. "
2941 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2943 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2944 // mi.base.textwidth << "\n";
2946 lyxerr << "InsetTabular::metrics: need bv" << endl;
2947 BOOST_ASSERT(false);
2951 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2954 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2955 if (tabular.isPartOfMultiColumn(i, j))
2956 // Multicolumn cell, but not first one
2961 if (tabular.cell_info[i][j].multicolumn ==
2962 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2963 p_width = tabular.cellinfo_of_cell(cell).p_width;
2965 p_width = tabular.column_info[j].p_width;
2966 if (!p_width.zero())
2967 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2968 tabular.getCellInset(cell)->metrics(m, dim);
2969 if (!p_width.zero())
2970 dim.wid = m.base.textwidth;
2971 tabular.setCellWidth(cell, dim.wid);
2972 if (p_width.zero()) {
2973 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2974 // FIXME there must be a way to get rid of
2975 // the second metrics call
2976 tabular.getCellInset(cell)->metrics(m, dim);
2978 maxAsc = max(maxAsc, dim.asc);
2979 maxDesc = max(maxDesc, dim.des);
2982 int const top_space = tabular.row_info[i].top_space_default ?
2983 default_line_space :
2984 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2985 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2986 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2987 default_line_space :
2988 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2989 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2992 dim.asc = tabular.rowAscent(0);
2993 dim.des = tabular.height() - dim.asc;
2994 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2998 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3000 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3001 BufferView * bv = pi.base.bv;
3003 // FIXME: As the full backrgound is painted in drawSelection(),
3004 // we have no choice but to do a full repaint for the Text cells.
3005 pi.full_repaint = true;
3007 resetPos(bv->cursor());
3010 x += ADD_TO_TABULAR_WIDTH;
3013 first_visible_cell = Tabular::npos;
3014 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3016 int const a = tabular.rowAscent(i);
3017 int const d = tabular.rowDescent(i);
3018 idx = tabular.cellIndex(i, 0);
3019 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3020 if (tabular.isPartOfMultiColumn(i, j))
3022 if (first_visible_cell == Tabular::npos)
3023 first_visible_cell = idx;
3025 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3026 // Cache the Inset position.
3027 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3028 if (nx + tabular.columnWidth(idx) < 0
3029 || nx > bv->workWidth()
3031 || y - a > bv->workHeight()) {
3032 pi.pain.setDrawingEnabled(false);
3033 cell(idx)->draw(pi, cx, y);
3034 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3035 pi.pain.setDrawingEnabled(true);
3037 cell(idx)->draw(pi, cx, y);
3038 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3040 nx += tabular.columnWidth(idx);
3044 if (i + 1 < tabular.rowCount())
3045 y += d + tabular.rowAscent(i + 1) +
3046 tabular.getAdditionalHeight(i + 1);
3051 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3053 Cursor & cur = pi.base.bv->cursor();
3055 x += scx_ + ADD_TO_TABULAR_WIDTH;
3057 // FIXME: it is wrong to completely paint the background
3058 // if we want to do single row painting.
3060 // Paint background of current tabular
3061 int const w = tabular.width();
3062 int const h = tabular.height();
3063 int yy = y - tabular.rowAscent(0);
3064 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3066 if (!cur.selection())
3068 if (!ptr_cmp(&cur.inset(), this))
3074 if (tablemode(cur)) {
3077 getSelection(cur, rs, re, cs, ce);
3078 y -= tabular.rowAscent(0);
3079 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3080 int const a = tabular.rowAscent(j);
3081 int const h = a + tabular.rowDescent(j);
3083 y += tabular.getAdditionalHeight(j);
3084 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3085 if (tabular.isPartOfMultiColumn(j, i))
3087 idx_type const cell =
3088 tabular.cellIndex(j, i);
3089 int const w = tabular.columnWidth(cell);
3090 if (i >= cs && i <= ce && j >= rs && j <= re)
3091 pi.pain.fillRectangle(xx, y, w, h,
3099 x += getCellXPos(cur.idx());
3100 x += tabular.getBeginningOfTextInCell(cur.idx());
3101 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3106 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3107 row_type row, idx_type cell, bool erased) const
3109 int x2 = x + tabular.columnWidth(cell);
3110 bool on_off = false;
3111 Color::color col = Color::tabularline;
3112 Color::color onoffcol = Color::tabularonoffline;
3115 col = Color::deletedtext;
3116 onoffcol = Color::deletedtext;
3119 if (!tabular.topAlreadyDrawn(cell)) {
3120 on_off = !tabular.topLine(cell);
3121 pain.line(x, y - tabular.rowAscent(row),
3122 x2, y - tabular.rowAscent(row),
3123 on_off ? onoffcol : col,
3124 on_off ? Painter::line_onoffdash : Painter::line_solid);
3126 on_off = !tabular.bottomLine(cell);
3127 pain.line(x, y + tabular.rowDescent(row),
3128 x2, y + tabular.rowDescent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3131 if (!tabular.leftAlreadyDrawn(cell)) {
3132 on_off = !tabular.leftLine(cell);
3133 pain.line(x, y - tabular.rowAscent(row),
3134 x, y + tabular.rowDescent(row),
3135 on_off ? onoffcol : col,
3136 on_off ? Painter::line_onoffdash : Painter::line_solid);
3138 on_off = !tabular.rightLine(cell);
3139 pain.line(x2 - tabular.getAdditionalWidth(cell),
3140 y - tabular.rowAscent(row),
3141 x2 - tabular.getAdditionalWidth(cell),
3142 y + tabular.rowDescent(row),
3143 on_off ? onoffcol : col,
3144 on_off ? Painter::line_onoffdash : Painter::line_solid);
3148 docstring const InsetTabular::editMessage() const
3150 return _("Opened table");
3154 void InsetTabular::edit(Cursor & cur, bool left)
3156 //lyxerr << "InsetTabular::edit: " << this << endl;
3158 cur.selection() = false;
3161 if (isRightToLeft(cur))
3162 cur.idx() = tabular.getLastCellInRow(0);
3168 if (isRightToLeft(cur))
3169 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3171 cur.idx() = tabular.cellCount() - 1;
3173 cur.pos() = cur.lastpos(); // FIXME crude guess
3175 // FIXME: this accesses the position cache before it is initialized
3177 //cur.bv().fitCursor();
3181 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3183 // In a longtable, tell captions what the current float is
3184 Counters & cnts = buf.params().getTextClass().counters();
3185 string const saveflt = cnts.current_float();
3186 if (tabular.isLongTabular())
3187 cnts.current_float("table");
3189 ParIterator it2 = it;
3191 size_t const end = it2.nargs();
3192 for ( ; it2.idx() < end; it2.top().forwardIdx())
3193 lyx::updateLabels(buf, it2);
3196 if (tabular.isLongTabular())
3197 cnts.current_float(saveflt);
3201 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3203 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3204 << "\n cur:" << cur << endl;
3205 CursorSlice sl = cur.top();
3206 Cursor & bvcur = cur.bv().cursor();
3208 switch (cmd.action) {
3210 case LFUN_MOUSE_PRESS:
3211 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3213 // do not reset cursor/selection if we have selected
3214 // some cells (bug 2715).
3215 if (cmd.button() == mouse_button::button3
3216 && &bvcur.selBegin().inset() == this
3217 && tablemode(bvcur))
3220 // Let InsetText do it
3221 cell(cur.idx())->dispatch(cur, cmd);
3223 case LFUN_MOUSE_MOTION:
3224 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3225 if (cmd.button() == mouse_button::button1) {
3226 // only accept motions to places not deeper nested than the real anchor
3227 if (bvcur.anchor_.hasPart(cur)) {
3228 // only update if selection changes
3229 if (bvcur.idx() == cur.idx() &&
3230 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3232 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3233 bvcur.setCursor(cur);
3234 bvcur.selection() = true;
3240 case LFUN_MOUSE_RELEASE:
3241 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3242 if (cmd.button() == mouse_button::button3)
3243 InsetTabularMailer(*this).showDialog(&cur.bv());
3246 case LFUN_CELL_BACKWARD:
3248 cur.selection() = false;
3251 case LFUN_CELL_FORWARD:
3253 cur.selection() = false;
3256 case LFUN_CHAR_FORWARD_SELECT:
3257 case LFUN_CHAR_FORWARD:
3258 cell(cur.idx())->dispatch(cur, cmd);
3259 if (!cur.result().dispatched()) {
3261 if (sl == cur.top())
3262 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3268 case LFUN_CHAR_BACKWARD_SELECT:
3269 case LFUN_CHAR_BACKWARD:
3270 cell(cur.idx())->dispatch(cur, cmd);
3271 if (!cur.result().dispatched()) {
3273 if (sl == cur.top())
3274 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3280 case LFUN_CHAR_RIGHT_SELECT:
3281 case LFUN_CHAR_RIGHT:
3282 //FIXME: for visual cursor, really move right
3283 if (isRightToLeft(cur))
3284 lyx::dispatch(FuncRequest(
3285 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3286 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3288 lyx::dispatch(FuncRequest(
3289 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3290 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3293 case LFUN_CHAR_LEFT_SELECT:
3294 case LFUN_CHAR_LEFT:
3295 //FIXME: for visual cursor, really move left
3296 if (isRightToLeft(cur))
3297 lyx::dispatch(FuncRequest(
3298 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3299 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3301 lyx::dispatch(FuncRequest(
3302 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3303 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3306 case LFUN_DOWN_SELECT:
3308 cell(cur.idx())->dispatch(cur, cmd);
3309 cur.dispatched(); // override the cell's decision
3310 if (sl == cur.top())
3311 // if our Text didn't do anything to the cursor
3312 // then we try to put the cursor into the cell below
3313 // setting also the right targetX.
3314 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3315 cur.idx() = tabular.getCellBelow(cur.idx());
3317 TextMetrics const & tm =
3318 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3319 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3321 if (sl == cur.top()) {
3322 // we trick it to go to forward after leaving the
3324 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3329 case LFUN_UP_SELECT:
3331 cell(cur.idx())->dispatch(cur, cmd);
3332 cur.dispatched(); // override the cell's decision
3333 if (sl == cur.top())
3334 // if our Text didn't do anything to the cursor
3335 // then we try to put the cursor into the cell above
3336 // setting also the right targetX.
3337 if (tabular.cellRow(cur.idx()) != 0) {
3338 cur.idx() = tabular.getCellAbove(cur.idx());
3339 cur.pit() = cur.lastpit();
3340 Text const * text = cell(cur.idx())->getText(0);
3341 TextMetrics const & tm = cur.bv().textMetrics(text);
3342 ParagraphMetrics const & pm =
3343 tm.parMetrics(cur.lastpit());
3344 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3346 if (sl == cur.top()) {
3347 cmd = FuncRequest(LFUN_UP);
3352 // case LFUN_SCREEN_DOWN: {
3353 // //if (hasSelection())
3354 // // cur.selection() = false;
3355 // col_type const col = tabular.cellColumn(cur.idx());
3356 // int const t = cur.bv().top_y() + cur.bv().height();
3357 // if (t < yo() + tabular.getHeightOfTabular()) {
3358 // cur.bv().scrollDocView(t);
3359 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3361 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3368 // case LFUN_SCREEN_UP: {
3369 // //if (hasSelection())
3370 // // cur.selection() = false;
3371 // col_type const col = tabular.cellColumn(cur.idx());
3372 // int const t = cur.bv().top_y() + cur.bv().height();
3374 // cur.bv().scrollDocView(t);
3378 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3382 // cur.par() = cur.lastpar();
3383 // cur.pos() = cur.lastpos();
3387 case LFUN_LAYOUT_TABULAR:
3388 InsetTabularMailer(*this).showDialog(&cur.bv());
3391 case LFUN_INSET_DIALOG_UPDATE:
3392 InsetTabularMailer(*this).updateDialog(&cur.bv());
3395 case LFUN_TABULAR_FEATURE:
3396 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3400 // insert file functions
3401 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3402 case LFUN_FILE_INSERT_PLAINTEXT: {
3404 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3405 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3411 if (tablemode(cur)) {
3412 if (copySelection(cur)) {
3413 cur.recordUndoInset(DELETE_UNDO);
3418 cell(cur.idx())->dispatch(cur, cmd);
3421 case LFUN_CHAR_DELETE_BACKWARD:
3422 case LFUN_CHAR_DELETE_FORWARD:
3423 if (tablemode(cur)) {
3424 cur.recordUndoInset(DELETE_UNDO);
3428 cell(cur.idx())->dispatch(cur, cmd);
3432 if (!cur.selection())
3434 if (tablemode(cur)) {
3438 cell(cur.idx())->dispatch(cur, cmd);
3441 case LFUN_CLIPBOARD_PASTE:
3442 case LFUN_PRIMARY_SELECTION_PASTE: {
3443 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3444 theClipboard().getAsText() :
3445 theSelection().get();
3448 // pass to InsertPlaintextString, but
3449 // only if we have multi-cell content
3450 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3451 if (insertPlaintextString(cur.bv(), clip, false)) {
3452 // content has been replaced,
3453 // so cursor might be invalid
3454 cur.pos() = cur.lastpos();
3455 bvcur.setCursor(cur);
3459 // Let the cell handle normal text
3460 cell(cur.idx())->dispatch(cur, cmd);
3465 if (tabularStackDirty() && theClipboard().isInternal()) {
3466 cur.recordUndoInset(INSERT_UNDO);
3467 pasteClipboard(cur);
3470 cell(cur.idx())->dispatch(cur, cmd);
3473 case LFUN_FONT_EMPH:
3474 case LFUN_FONT_BOLD:
3475 case LFUN_FONT_ROMAN:
3476 case LFUN_FONT_NOUN:
3477 case LFUN_FONT_ITAL:
3478 case LFUN_FONT_FRAK:
3479 case LFUN_FONT_TYPEWRITER:
3480 case LFUN_FONT_SANS:
3481 case LFUN_FONT_FREE_APPLY:
3482 case LFUN_FONT_FREE_UPDATE:
3483 case LFUN_FONT_SIZE:
3484 case LFUN_FONT_UNDERLINE:
3486 case LFUN_WORD_CAPITALIZE:
3487 case LFUN_WORD_UPCASE:
3488 case LFUN_WORD_LOWCASE:
3489 case LFUN_CHARS_TRANSPOSE:
3490 if (tablemode(cur)) {
3493 getSelection(cur, rs, re, cs, ce);
3494 Cursor tmpcur = cur;
3495 for (row_type i = rs; i <= re; ++i) {
3496 for (col_type j = cs; j <= ce; ++j) {
3497 // cursor follows cell:
3498 tmpcur.idx() = tabular.cellIndex(i, j);
3499 // select this cell only:
3502 tmpcur.resetAnchor();
3503 tmpcur.pit() = tmpcur.lastpit();
3504 tmpcur.pos() = tmpcur.top().lastpos();
3505 tmpcur.setCursor(tmpcur);
3506 tmpcur.setSelection();
3507 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3512 cell(cur.idx())->dispatch(cur, cmd);
3516 // we try to handle this event in the insets dispatch function.
3517 cell(cur.idx())->dispatch(cur, cmd);
3523 // function sets an object as defined in func_status.h:
3524 // states OK, Unknown, Disabled, On, Off.
3525 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3526 FuncStatus & status) const
3528 switch (cmd.action) {
3529 case LFUN_TABULAR_FEATURE: {
3530 int action = Tabular::LAST_ACTION;
3532 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3533 string const tmp = tabularFeature[i].feature;
3534 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3535 action = tabularFeature[i].action;
3539 if (action == Tabular::LAST_ACTION) {
3541 status.unknown(true);
3545 string const argument
3546 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3548 row_type sel_row_start = 0;
3549 row_type sel_row_end = 0;
3550 col_type sel_col_start = 0;
3551 col_type sel_col_end = 0;
3552 Tabular::ltType dummyltt;
3555 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3558 case Tabular::SET_PWIDTH:
3559 case Tabular::SET_MPWIDTH:
3560 case Tabular::SET_SPECIAL_COLUMN:
3561 case Tabular::SET_SPECIAL_MULTI:
3562 case Tabular::APPEND_ROW:
3563 case Tabular::APPEND_COLUMN:
3564 case Tabular::DELETE_ROW:
3565 case Tabular::DELETE_COLUMN:
3566 case Tabular::COPY_ROW:
3567 case Tabular::COPY_COLUMN:
3568 case Tabular::SET_ALL_LINES:
3569 case Tabular::UNSET_ALL_LINES:
3570 case Tabular::SET_TOP_SPACE:
3571 case Tabular::SET_BOTTOM_SPACE:
3572 case Tabular::SET_INTERLINE_SPACE:
3576 case Tabular::MULTICOLUMN:
3577 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3580 case Tabular::M_TOGGLE_LINE_TOP:
3582 case Tabular::TOGGLE_LINE_TOP:
3583 status.setOnOff(tabular.topLine(cur.idx(), flag));
3586 case Tabular::M_TOGGLE_LINE_BOTTOM:
3588 case Tabular::TOGGLE_LINE_BOTTOM:
3589 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3592 case Tabular::M_TOGGLE_LINE_LEFT:
3594 case Tabular::TOGGLE_LINE_LEFT:
3595 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3598 case Tabular::M_TOGGLE_LINE_RIGHT:
3600 case Tabular::TOGGLE_LINE_RIGHT:
3601 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3604 case Tabular::M_ALIGN_LEFT:
3606 case Tabular::ALIGN_LEFT:
3607 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3610 case Tabular::M_ALIGN_RIGHT:
3612 case Tabular::ALIGN_RIGHT:
3613 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3616 case Tabular::M_ALIGN_CENTER:
3618 case Tabular::ALIGN_CENTER:
3619 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3622 case Tabular::ALIGN_BLOCK:
3623 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3624 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3627 case Tabular::M_VALIGN_TOP:
3629 case Tabular::VALIGN_TOP:
3631 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3634 case Tabular::M_VALIGN_BOTTOM:
3636 case Tabular::VALIGN_BOTTOM:
3638 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3641 case Tabular::M_VALIGN_MIDDLE:
3643 case Tabular::VALIGN_MIDDLE:
3645 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3648 case Tabular::SET_LONGTABULAR:
3649 status.setOnOff(tabular.isLongTabular());
3652 case Tabular::UNSET_LONGTABULAR:
3653 status.setOnOff(!tabular.isLongTabular());
3656 case Tabular::TOGGLE_ROTATE_TABULAR:
3657 case Tabular::SET_ROTATE_TABULAR:
3658 status.setOnOff(tabular.getRotateTabular());
3661 case Tabular::UNSET_ROTATE_TABULAR:
3662 status.setOnOff(!tabular.getRotateTabular());
3665 case Tabular::TOGGLE_ROTATE_CELL:
3666 case Tabular::SET_ROTATE_CELL:
3667 status.setOnOff(!oneCellHasRotationState(false,
3668 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3671 case Tabular::UNSET_ROTATE_CELL:
3672 status.setOnOff(!oneCellHasRotationState(true,
3673 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3676 case Tabular::SET_USEBOX:
3677 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3680 case Tabular::SET_LTFIRSTHEAD:
3681 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3684 case Tabular::UNSET_LTFIRSTHEAD:
3685 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3688 case Tabular::SET_LTHEAD:
3689 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3692 case Tabular::UNSET_LTHEAD:
3693 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3696 case Tabular::SET_LTFOOT:
3697 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3700 case Tabular::UNSET_LTFOOT:
3701 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3704 case Tabular::SET_LTLASTFOOT:
3705 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3708 case Tabular::UNSET_LTLASTFOOT:
3709 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3712 case Tabular::SET_LTNEWPAGE:
3713 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3716 case Tabular::SET_BOOKTABS:
3717 status.setOnOff(tabular.useBookTabs());
3720 case Tabular::UNSET_BOOKTABS:
3721 status.setOnOff(!tabular.useBookTabs());
3726 status.enabled(false);
3732 // These are only enabled inside tabular
3733 case LFUN_CELL_BACKWARD:
3734 case LFUN_CELL_FORWARD:
3735 status.enabled(true);
3738 // disable these with multiple cells selected
3739 case LFUN_INSET_INSERT:
3740 case LFUN_TABULAR_INSERT:
3741 case LFUN_FLEX_INSERT:
3742 case LFUN_FLOAT_INSERT:
3743 case LFUN_FLOAT_WIDE_INSERT:
3744 case LFUN_FOOTNOTE_INSERT:
3745 case LFUN_MARGINALNOTE_INSERT:
3746 case LFUN_MATH_INSERT:
3747 case LFUN_MATH_MODE:
3748 case LFUN_MATH_MUTATE:
3749 case LFUN_MATH_DISPLAY:
3750 case LFUN_NOTE_INSERT:
3751 case LFUN_OPTIONAL_INSERT:
3752 case LFUN_BOX_INSERT:
3753 case LFUN_BRANCH_INSERT:
3754 case LFUN_WRAP_INSERT:
3755 case LFUN_ERT_INSERT: {
3756 if (tablemode(cur)) {
3757 status.enabled(false);
3760 return cell(cur.idx())->getStatus(cur, cmd, status);
3763 // disable in non-fixed-width cells
3764 case LFUN_BREAK_LINE:
3765 case LFUN_BREAK_PARAGRAPH:
3766 case LFUN_BREAK_PARAGRAPH_SKIP: {
3767 if (tabular.getPWidth(cur.idx()).zero()) {
3768 status.enabled(false);
3771 return cell(cur.idx())->getStatus(cur, cmd, status);
3775 if (tabularStackDirty() && theClipboard().isInternal()) {
3776 status.enabled(true);
3779 return cell(cur.idx())->getStatus(cur, cmd, status);
3781 case LFUN_INSET_MODIFY:
3782 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3783 status.enabled(true);
3789 // we try to handle this event in the insets dispatch function.
3790 return cell(cur.idx())->getStatus(cur, cmd, status);
3795 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3796 OutputParams const & runparams) const
3798 return tabular.latex(buf, os, runparams);
3802 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3803 OutputParams const & runparams) const
3805 os << '\n'; // output table on a new line
3806 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3807 tabular.plaintext(buf, os, runparams, dp, false, 0);
3808 return PLAINTEXT_NEWLINE;
3812 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3813 OutputParams const & runparams) const
3818 // FIXME: Why not pass a proper DocIterator here?
3820 // if the table is inside a float it doesn't need the informaltable
3821 // wrapper. Search for it.
3822 for (master = owner(); master; master = master->owner())
3823 if (master->lyxCode() == FLOAT_CODE)
3828 os << "<informaltable>";
3831 ret += tabular.docbook(buf, os, runparams);
3833 os << "</informaltable>";
3840 void InsetTabular::validate(LaTeXFeatures & features) const
3842 tabular.validate(features);
3846 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3848 return tabular.getCellInset(idx);
3852 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3854 return tabular.getCellInset(idx);
3858 void InsetTabular::cursorPos(BufferView const & bv,
3859 CursorSlice const & sl, bool boundary, int & x, int & y) const
3861 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3863 // y offset correction
3864 int const row = tabular.cellRow(sl.idx());
3865 for (int i = 0; i <= row; ++i) {
3867 y += tabular.rowAscent(i);
3868 y += tabular.getAdditionalHeight(i);
3871 y += tabular.rowDescent(i);
3874 // x offset correction
3875 int const col = tabular.cellColumn(sl.idx());
3876 int idx = tabular.cellIndex(row, 0);
3877 for (int j = 0; j < col; ++j) {
3878 if (tabular.isPartOfMultiColumn(row, j))
3880 x += tabular.columnWidth(idx);
3883 x += tabular.getBeginningOfTextInCell(idx);
3884 x += ADD_TO_TABULAR_WIDTH;
3889 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3893 Inset const & inset = *tabular.getCellInset(cell);
3894 Point o = bv.coordCache().getInsets().xy(&inset);
3895 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3896 int const xend = xbeg + tabular.columnWidth(cell);
3897 row_type const row = tabular.cellRow(cell);
3898 int const ybeg = o.y_ - tabular.rowAscent(row) -
3899 tabular.getAdditionalHeight(row);
3900 int const yend = o.y_ + tabular.rowDescent(row);
3912 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3913 // << " ybeg=" << ybeg << " yend=" << yend
3914 // << " xx=" << xx << " yy=" << yy
3915 // << " dist=" << xx + yy << endl;
3920 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3922 //lyxerr << "InsetTabular::editXY: " << this << endl;
3923 cur.selection() = false;
3925 cur.idx() = getNearestCell(cur.bv(), x, y);
3927 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3931 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3933 cur.idx() = getNearestCell(cur.bv(), x, y);
3934 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3938 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3940 idx_type idx_min = 0;
3941 int dist_min = std::numeric_limits<int>::max();
3942 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3943 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3944 int const d = dist(bv, i, x, y);
3955 int InsetTabular::getCellXPos(idx_type const cell) const
3959 for (; !tabular.isFirstCellInRow(c); --c)
3962 for (; c < cell; ++c)
3963 lx += tabular.columnWidth(c);
3969 void InsetTabular::resetPos(Cursor & cur) const
3971 BufferView & bv = cur.bv();
3972 int const maxwidth = bv.workWidth();
3974 if (&cur.inset() != this) {
3978 int const X2 = maxwidth;
3979 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3980 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3981 int const x2 = x1 + tabular.columnWidth(cur.idx());
3984 scx_ = X1 + 20 - x1;
3986 scx_ = X2 - 20 - x2;
3991 cur.updateFlags(Update::Force | Update::FitCursor);
3995 void InsetTabular::moveNextCell(Cursor & cur)
3997 if (isRightToLeft(cur)) {
3998 if (tabular.isFirstCellInRow(cur.idx())) {
3999 row_type const row = tabular.cellRow(cur.idx());
4000 if (row == tabular.rowCount() - 1)
4002 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4009 if (tabular.isLastCell(cur.idx()))
4019 void InsetTabular::movePrevCell(Cursor & cur)
4021 if (isRightToLeft(cur)) {
4022 if (tabular.isLastCellInRow(cur.idx())) {
4023 row_type const row = tabular.cellRow(cur.idx());
4026 cur.idx() = tabular.getFirstCellInRow(row);
4027 cur.idx() = tabular.getCellAbove(cur.idx());
4029 if (tabular.isLastCell(cur.idx()))
4034 if (cur.idx() == 0) // first cell
4038 cur.pit() = cur.lastpit();
4039 cur.pos() = cur.lastpos();
4041 // FIXME: this accesses the position cache before it is initialized
4046 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4048 Tabular::Feature action = Tabular::LAST_ACTION;
4051 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4052 string const tmp = tabularFeature[i].feature;
4054 if (tmp == what.substr(0, tmp.length())) {
4055 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4056 //tabularFeatures[i].feature.length()))
4057 action = tabularFeature[i].action;
4061 if (action == Tabular::LAST_ACTION)
4065 ltrim(what.substr(tabularFeature[i].feature.length()));
4066 tabularFeatures(cur, action, val);
4071 static void checkLongtableSpecial(Tabular::ltType & ltt,
4072 string const & special, bool & flag)
4074 if (special == "dl_above") {
4077 } else if (special == "dl_below") {
4078 ltt.bottomDL = flag;
4080 } else if (special == "empty") {
4089 bool InsetTabular::oneCellHasRotationState(bool rotated,
4090 row_type row_start, row_type row_end,
4091 col_type col_start, col_type col_end) const {
4093 for (row_type i = row_start; i <= row_end; ++i) {
4094 for (col_type j = col_start; j <= col_end; ++j) {
4095 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4104 void InsetTabular::tabularFeatures(Cursor & cur,
4105 Tabular::Feature feature, string const & value)
4107 BufferView & bv = cur.bv();
4108 col_type sel_col_start;
4109 col_type sel_col_end;
4110 row_type sel_row_start;
4111 row_type sel_row_end;
4112 bool setLines = false;
4113 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4114 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4118 case Tabular::M_ALIGN_LEFT:
4119 case Tabular::ALIGN_LEFT:
4120 setAlign = LYX_ALIGN_LEFT;
4123 case Tabular::M_ALIGN_RIGHT:
4124 case Tabular::ALIGN_RIGHT:
4125 setAlign = LYX_ALIGN_RIGHT;
4128 case Tabular::M_ALIGN_CENTER:
4129 case Tabular::ALIGN_CENTER:
4130 setAlign = LYX_ALIGN_CENTER;
4133 case Tabular::ALIGN_BLOCK:
4134 setAlign = LYX_ALIGN_BLOCK;
4137 case Tabular::M_VALIGN_TOP:
4138 case Tabular::VALIGN_TOP:
4139 setVAlign = Tabular::LYX_VALIGN_TOP;
4142 case Tabular::M_VALIGN_BOTTOM:
4143 case Tabular::VALIGN_BOTTOM:
4144 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4147 case Tabular::M_VALIGN_MIDDLE:
4148 case Tabular::VALIGN_MIDDLE:
4149 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4156 cur.recordUndoInset(ATOMIC_UNDO);
4158 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4159 row_type const row = tabular.cellRow(cur.idx());
4160 col_type const column = tabular.cellColumn(cur.idx());
4162 Tabular::ltType ltt;
4166 case Tabular::SET_PWIDTH: {
4167 Length const len(value);
4168 tabular.setColumnPWidth(cur, cur.idx(), len);
4170 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4171 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4175 case Tabular::SET_MPWIDTH:
4176 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4179 case Tabular::SET_SPECIAL_COLUMN:
4180 case Tabular::SET_SPECIAL_MULTI:
4181 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4184 case Tabular::APPEND_ROW:
4185 // append the row into the tabular
4186 tabular.appendRow(bv.buffer().params(), cur.idx());
4189 case Tabular::APPEND_COLUMN:
4190 // append the column into the tabular
4191 tabular.appendColumn(bv.buffer().params(), cur.idx());
4192 cur.idx() = tabular.cellIndex(row, column);
4195 case Tabular::DELETE_ROW:
4196 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4197 tabular.deleteRow(sel_row_start);
4198 if (sel_row_start >= tabular.rowCount())
4200 cur.idx() = tabular.cellIndex(sel_row_start, column);
4203 cur.selection() = false;
4206 case Tabular::DELETE_COLUMN:
4207 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4208 tabular.deleteColumn(sel_col_start);
4209 if (sel_col_start >= tabular.columnCount())
4211 cur.idx() = tabular.cellIndex(row, sel_col_start);
4214 cur.selection() = false;
4217 case Tabular::COPY_ROW:
4218 tabular.copyRow(bv.buffer().params(), row);
4221 case Tabular::COPY_COLUMN:
4222 tabular.copyColumn(bv.buffer().params(), column);
4223 cur.idx() = tabular.cellIndex(row, column);
4226 case Tabular::M_TOGGLE_LINE_TOP:
4228 case Tabular::TOGGLE_LINE_TOP: {
4229 bool lineSet = !tabular.topLine(cur.idx(), flag);
4230 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4231 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4233 tabular.cellIndex(i, j),
4238 case Tabular::M_TOGGLE_LINE_BOTTOM:
4240 case Tabular::TOGGLE_LINE_BOTTOM: {
4241 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4242 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4243 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4244 tabular.setBottomLine(
4245 tabular.cellIndex(i, j),
4251 case Tabular::M_TOGGLE_LINE_LEFT:
4253 case Tabular::TOGGLE_LINE_LEFT: {
4254 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4255 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4256 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4257 tabular.setLeftLine(
4258 tabular.cellIndex(i,j),
4264 case Tabular::M_TOGGLE_LINE_RIGHT:
4266 case Tabular::TOGGLE_LINE_RIGHT: {
4267 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4268 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4269 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4270 tabular.setRightLine(
4271 tabular.cellIndex(i,j),
4277 case Tabular::M_ALIGN_LEFT:
4278 case Tabular::M_ALIGN_RIGHT:
4279 case Tabular::M_ALIGN_CENTER:
4281 case Tabular::ALIGN_LEFT:
4282 case Tabular::ALIGN_RIGHT:
4283 case Tabular::ALIGN_CENTER:
4284 case Tabular::ALIGN_BLOCK:
4285 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4286 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4287 tabular.setAlignment(
4288 tabular.cellIndex(i, j),
4293 case Tabular::M_VALIGN_TOP:
4294 case Tabular::M_VALIGN_BOTTOM:
4295 case Tabular::M_VALIGN_MIDDLE:
4297 case Tabular::VALIGN_TOP:
4298 case Tabular::VALIGN_BOTTOM:
4299 case Tabular::VALIGN_MIDDLE:
4300 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4301 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4302 tabular.setVAlignment(
4303 tabular.cellIndex(i, j),
4307 case Tabular::MULTICOLUMN: {
4308 if (sel_row_start != sel_row_end) {
4309 // FIXME: Need I say it ? This is horrible.
4311 Alert::error(_("Error setting multicolumn"),
4312 _("You cannot set multicolumn vertically."));
4315 if (!cur.selection()) {
4316 // just multicol for one single cell
4317 // check whether we are completely in a multicol
4318 if (tabular.isMultiColumn(cur.idx()))
4319 tabular.unsetMultiColumn(cur.idx());
4321 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4324 // we have a selection so this means we just add all this
4325 // cells to form a multicolumn cell
4326 idx_type const s_start = cur.selBegin().idx();
4327 idx_type const s_end = cur.selEnd().idx();
4328 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4329 cur.idx() = s_start;
4332 cur.selection() = false;
4336 case Tabular::SET_ALL_LINES:
4338 case Tabular::UNSET_ALL_LINES:
4339 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4340 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4341 tabular.setAllLines(
4342 tabular.cellIndex(i,j), setLines);
4345 case Tabular::SET_LONGTABULAR:
4346 tabular.setLongTabular(true);
4349 case Tabular::UNSET_LONGTABULAR:
4350 tabular.setLongTabular(false);
4353 case Tabular::SET_ROTATE_TABULAR:
4354 tabular.setRotateTabular(true);
4357 case Tabular::UNSET_ROTATE_TABULAR:
4358 tabular.setRotateTabular(false);
4361 case Tabular::TOGGLE_ROTATE_TABULAR:
4362 tabular.setRotateTabular(!tabular.getRotateTabular());
4365 case Tabular::SET_ROTATE_CELL:
4366 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4367 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4368 tabular.setRotateCell(
4369 tabular.cellIndex(i, j), true);
4372 case Tabular::UNSET_ROTATE_CELL:
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setRotateCell(
4376 tabular.cellIndex(i, j), false);
4379 case Tabular::TOGGLE_ROTATE_CELL:
4381 bool oneNotRotated = oneCellHasRotationState(false,
4382 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4384 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4385 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4386 tabular.setRotateCell(tabular.cellIndex(i, j),
4391 case Tabular::SET_USEBOX: {
4392 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4393 if (val == tabular.getUsebox(cur.idx()))
4394 val = Tabular::BOX_NONE;
4395 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4396 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4397 tabular.setUsebox(tabular.cellIndex(i, j), val);
4401 case Tabular::UNSET_LTFIRSTHEAD:
4403 case Tabular::SET_LTFIRSTHEAD:
4404 tabular.getRowOfLTFirstHead(row, ltt);
4405 checkLongtableSpecial(ltt, value, flag);
4406 tabular.setLTHead(row, flag, ltt, true);
4409 case Tabular::UNSET_LTHEAD:
4411 case Tabular::SET_LTHEAD:
4412 tabular.getRowOfLTHead(row, ltt);
4413 checkLongtableSpecial(ltt, value, flag);
4414 tabular.setLTHead(row, flag, ltt, false);
4417 case Tabular::UNSET_LTFOOT:
4419 case Tabular::SET_LTFOOT:
4420 tabular.getRowOfLTFoot(row, ltt);
4421 checkLongtableSpecial(ltt, value, flag);
4422 tabular.setLTFoot(row, flag, ltt, false);
4425 case Tabular::UNSET_LTLASTFOOT:
4427 case Tabular::SET_LTLASTFOOT:
4428 tabular.getRowOfLTLastFoot(row, ltt);
4429 checkLongtableSpecial(ltt, value, flag);
4430 tabular.setLTFoot(row, flag, ltt, true);
4433 case Tabular::SET_LTNEWPAGE:
4434 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4437 case Tabular::SET_BOOKTABS:
4438 tabular.setBookTabs(true);
4441 case Tabular::UNSET_BOOKTABS:
4442 tabular.setBookTabs(false);
4445 case Tabular::SET_TOP_SPACE: {
4447 if (value == "default")
4448 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4449 tabular.row_info[i].top_space_default = true;
4450 else if (isValidLength(value, &len))
4451 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4452 tabular.row_info[i].top_space_default = false;
4453 tabular.row_info[i].top_space = len;
4456 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4457 tabular.row_info[i].top_space_default = false;
4458 tabular.row_info[i].top_space = len;
4463 case Tabular::SET_BOTTOM_SPACE: {
4465 if (value == "default")
4466 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4467 tabular.row_info[i].bottom_space_default = true;
4468 else if (isValidLength(value, &len))
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4470 tabular.row_info[i].bottom_space_default = false;
4471 tabular.row_info[i].bottom_space = len;
4474 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4475 tabular.row_info[i].bottom_space_default = false;
4476 tabular.row_info[i].bottom_space = len;
4481 case Tabular::SET_INTERLINE_SPACE: {
4483 if (value == "default")
4484 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4485 tabular.row_info[i].interline_space_default = true;
4486 else if (isValidLength(value, &len))
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4488 tabular.row_info[i].interline_space_default = false;
4489 tabular.row_info[i].interline_space = len;
4492 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4493 tabular.row_info[i].interline_space_default = false;
4494 tabular.row_info[i].interline_space = len;
4499 // dummy stuff just to avoid warnings
4500 case Tabular::LAST_ACTION:
4506 bool InsetTabular::showInsetDialog(BufferView * bv) const
4508 InsetTabularMailer(*this).showDialog(bv);
4513 void InsetTabular::openLayoutDialog(BufferView * bv) const
4515 InsetTabularMailer(*this).showDialog(bv);
4519 bool InsetTabular::copySelection(Cursor & cur)
4521 if (!cur.selection())
4526 getSelection(cur, rs, re, cs, ce);
4528 paste_tabular.reset(new Tabular(tabular));
4530 for (row_type i = 0; i < rs; ++i)
4531 paste_tabular->deleteRow(0);
4533 row_type const rows = re - rs + 1;
4534 while (paste_tabular->rowCount() > rows)
4535 paste_tabular->deleteRow(rows);
4537 paste_tabular->setTopLine(0, true, true);
4538 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4541 for (col_type i = 0; i < cs; ++i)
4542 paste_tabular->deleteColumn(0);
4544 col_type const columns = ce - cs + 1;
4545 while (paste_tabular->columnCount() > columns)
4546 paste_tabular->deleteColumn(columns);
4548 paste_tabular->setLeftLine(0, true, true);
4549 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4552 odocstringstream os;
4553 OutputParams const runparams(0);
4554 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4555 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4556 cap::copySelection(cur, os.str());
4558 // mark tabular stack dirty
4559 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4560 // when we (hopefully) have a one-for-all paste mechanism.
4561 // This must be called after cap::copySelection.
4562 dirtyTabularStack(true);
4568 bool InsetTabular::pasteClipboard(Cursor & cur)
4572 col_type const actcol = tabular.cellColumn(cur.idx());
4573 row_type const actrow = tabular.cellRow(cur.idx());
4574 for (row_type r1 = 0, r2 = actrow;
4575 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4577 for (col_type c1 = 0, c2 = actcol;
4578 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4580 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4581 tabular.isPartOfMultiColumn(r2, c2))
4583 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4587 if (tabular.isPartOfMultiColumn(r2, c2)) {
4591 shared_ptr<InsetText> inset(
4592 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4593 tabular.setCellInset(r2, c2, inset);
4594 // FIXME: change tracking (MG)
4595 inset->setChange(Change(cur.buffer().params().trackChanges ?
4596 Change::INSERTED : Change::UNCHANGED));
4604 void InsetTabular::cutSelection(Cursor & cur)
4606 if (!cur.selection())
4611 getSelection(cur, rs, re, cs, ce);
4612 for (row_type i = rs; i <= re; ++i) {
4613 for (col_type j = cs; j <= ce; ++j) {
4614 shared_ptr<InsetText> t
4615 = cell(tabular.cellIndex(i, j));
4616 if (cur.buffer().params().trackChanges)
4617 // FIXME: Change tracking (MG)
4618 t->setChange(Change(Change::DELETED));
4624 // cursor position might be invalid now
4625 if (cur.pit() > cur.lastpit())
4626 cur.pit() = cur.lastpit();
4627 if (cur.pos() > cur.lastpos())
4628 cur.pos() = cur.lastpos();
4629 cur.clearSelection();
4633 bool InsetTabular::isRightToLeft(Cursor & cur) const
4635 BOOST_ASSERT(cur.depth() > 1);
4636 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4637 pos_type const parentpos = cur[cur.depth() - 2].pos();
4638 return parentpar.getFontSettings(cur.bv().buffer().params(),
4639 parentpos).language()->rightToLeft();
4643 void InsetTabular::getSelection(Cursor & cur,
4644 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4646 CursorSlice const & beg = cur.selBegin();
4647 CursorSlice const & end = cur.selEnd();
4648 cs = tabular.cellColumn(beg.idx());
4649 ce = tabular.cellColumn(end.idx());
4652 cs = tabular.cellColumn(end.idx());
4654 ce = tabular.cellRightColumn(end.idx());
4657 rs = tabular.cellRow(beg.idx());
4658 re = tabular.cellRow(end.idx());
4664 Text * InsetTabular::getText(int idx) const
4666 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4670 void InsetTabular::setChange(Change const & change)
4672 for (idx_type idx = 0; idx < nargs(); ++idx)
4673 cell(idx)->setChange(change);
4677 void InsetTabular::acceptChanges(BufferParams const & bparams)
4679 for (idx_type idx = 0; idx < nargs(); ++idx)
4680 cell(idx)->acceptChanges(bparams);
4684 void InsetTabular::rejectChanges(BufferParams const & bparams)
4686 for (idx_type idx = 0; idx < nargs(); ++idx)
4687 cell(idx)->rejectChanges(bparams);
4691 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4693 return tabular.getPWidth(cell).zero();
4697 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4700 if (buf.length() <= 0)
4703 Buffer const & buffer = bv.buffer();
4707 col_type maxCols = 1;
4708 docstring::size_type const len = buf.length();
4709 docstring::size_type p = 0;
4712 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4720 maxCols = max(cols, maxCols);
4726 maxCols = max(cols, maxCols);
4732 paste_tabular.reset(
4733 new Tabular(buffer.params(), rows, maxCols));
4734 loctab = paste_tabular.get();
4736 dirtyTabularStack(true);
4739 cell = bv.cursor().idx();
4740 ocol = tabular.cellColumn(cell);
4741 row = tabular.cellRow(cell);
4744 docstring::size_type op = 0;
4745 idx_type const cells = loctab->cellCount();
4748 rows = loctab->rowCount();
4749 col_type const columns = loctab->columnCount();
4751 while (cell < cells && p < len && row < rows &&
4752 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4758 // we can only set this if we are not too far right
4759 if (cols < columns) {
4760 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4761 Font const font = bv.textMetrics(&inset->text_).
4762 getDisplayFont(0, 0);
4763 inset->setText(buf.substr(op, p - op), font,
4764 buffer.params().trackChanges);
4770 // we can only set this if we are not too far right
4771 if (cols < columns) {
4772 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4773 Font const font = bv.textMetrics(&inset->text_).
4774 getDisplayFont(0, 0);
4775 inset->setText(buf.substr(op, p - op), font,
4776 buffer.params().trackChanges);
4781 cell = loctab->cellIndex(row, cols);
4787 // check for the last cell if there is no trailing '\n'
4788 if (cell < cells && op < len) {
4789 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4790 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4791 inset->setText(buf.substr(op, len - op), font,
4792 buffer.params().trackChanges);
4798 void InsetTabular::addPreview(PreviewLoader & loader) const
4800 row_type const rows = tabular.rowCount();
4801 col_type const columns = tabular.columnCount();
4802 for (row_type i = 0; i < rows; ++i) {
4803 for (col_type j = 0; j < columns; ++j)
4804 tabular.getCellInset(i, j)->addPreview(loader);
4809 bool InsetTabular::tablemode(Cursor & cur) const
4811 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4817 string const InsetTabularMailer::name_("tabular");
4819 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4820 : inset_(const_cast<InsetTabular &>(inset))
4824 string const InsetTabularMailer::inset2string(Buffer const &) const
4826 return params2string(inset_);
4830 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4832 istringstream data(in);
4834 lex.setStream(data);
4841 if (!lex || token != name_)
4842 return print_mailer_error("InsetTabularMailer", in, 1,
4845 // This is part of the inset proper that is usually swallowed
4846 // by Buffer::readInset
4848 if (!lex || token != "Tabular")
4849 return print_mailer_error("InsetTabularMailer", in, 2,
4852 Buffer const & buffer = inset.buffer();
4853 inset.read(buffer, lex);
4857 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4860 data << name_ << ' ';
4861 inset.write(inset.buffer(), data);
4862 data << "\\end_inset\n";