2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "CoordCache.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
41 #include "MetricsInfo.h"
42 #include "OutputParams.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
49 #include "support/convert.h"
50 #include "support/lstrings.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
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()
580 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
581 col_type columns_arg)
583 init(bp, rows_arg, columns_arg);
587 // activates all lines and sets all widths to 0
588 void Tabular::init(BufferParams const & bp, row_type rows_arg,
589 col_type columns_arg)
591 row_info = row_vector(rows_arg);
592 column_info = column_vector(columns_arg);
593 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
594 row_info.reserve(10);
595 column_info.reserve(10);
596 cell_info.reserve(100);
598 for (row_type i = 0; i < rows_arg; ++i)
599 cell_info[i].back().right_line = true;
600 row_info.back().bottom_line = true;
601 row_info.front().bottom_line = true;
602 column_info.back().right_line = true;
603 is_long_tabular = false;
605 use_booktabs = false;
609 void Tabular::fixCellNums()
612 for (row_type i = 0; i < rowCount(); ++i) {
613 for (col_type j = 0; j < columnCount(); ++j) {
614 // When debugging it can be nice to set
616 cell_info[i][j].inset->setDrawFrame(false);
617 cell_info[i][j].cellno = cellno++;
619 cell_info[i].back().right_line = true;
626 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
628 row_type const row = cellRow(cell);
630 row_vector::iterator rit = row_info.begin() + row;
631 row_info.insert(rit, rowstruct());
632 // now set the values of the row before
633 row_info[row] = row_info[row + 1];
635 row_type const nrows = rowCount();
636 col_type const ncols = columnCount();
638 cell_vvector old(nrows - 1);
639 for (row_type i = 0; i < nrows - 1; ++i)
640 swap(cell_info[i], old[i]);
642 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
644 for (row_type i = 0; i <= row; ++i)
645 swap(cell_info[i], old[i]);
646 for (row_type i = row + 2; i < nrows; ++i)
647 swap(cell_info[i], old[i - 1]);
650 for (col_type j = 0; j < ncols; ++j)
651 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
657 void Tabular::deleteRow(row_type const row)
659 // Not allowed to delete last row
663 row_info.erase(row_info.begin() + row);
664 cell_info.erase(cell_info.begin() + row);
669 void Tabular::copyRow(BufferParams const & bp, row_type const row)
671 row_info.insert(row_info.begin() + row, row_info[row]);
672 cell_info.insert(cell_info.begin() + row, cell_info[row]);
675 for (col_type j = 0; j < columnCount(); ++j)
676 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
684 col_type const column = cellColumn(cell);
685 col_type const ncols = columnCount();
686 column_vector::iterator cit = column_info.begin() + column + 1;
687 column_info.insert(cit, columnstruct());
688 // set the column values of the column before
689 column_info[column + 1] = column_info[column];
691 for (row_type i = 0; i < rowCount(); ++i) {
692 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
694 // care about multicolumns
695 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
696 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
698 if (column + 2 >= ncols
699 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
700 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
703 for (row_type i = 0; i < rowCount(); ++i) {
704 cell_info[i][column + 1].inset->clear();
706 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
712 void Tabular::deleteColumn(col_type const column)
714 // Not allowed to delete last column
715 if (columnCount() == 1)
718 column_info.erase(column_info.begin() + column);
719 for (row_type i = 0; i < rowCount(); ++i) {
720 // Care about multicolumn cells
721 if (column + 1 < columnCount() &&
722 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
723 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
724 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
726 cell_info[i].erase(cell_info[i].begin() + column);
732 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
734 column_info.insert(column_info.begin() + column, column_info[column]);
736 for (row_type i = 0; i < rowCount(); ++i)
737 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
740 for (row_type i = 0; i < rowCount(); ++i)
741 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
746 void Tabular::updateIndexes()
749 // Count only non-multicol cells plus begin multicol
750 // cells, ignore non-begin multicol cells:
751 for (row_type row = 0; row < rowCount(); ++row) {
752 for (col_type column = 0; column < columnCount(); ++column) {
753 if (cell_info[row][column].multicolumn
754 != Tabular::CELL_PART_OF_MULTICOLUMN)
756 BOOST_ASSERT(numberofcells != 0);
757 cell_info[row][column].cellno =
762 rowofcell.resize(numberofcells);
763 columnofcell.resize(numberofcells);
768 c < numberofcells && row < rowCount() && column < columnCount();) {
770 columnofcell[c] = column;
774 } while (column < columnCount() &&
775 cell_info[row][column].multicolumn
776 == Tabular::CELL_PART_OF_MULTICOLUMN);
778 if (column == columnCount()) {
784 for (row_type row = 0; row < rowCount(); ++row) {
785 for (col_type column = 0; column < columnCount(); ++column) {
786 if (isPartOfMultiColumn(row,column))
788 cell_info[row][column].inset->setAutoBreakRows(
789 !getPWidth(cellIndex(row, column)).zero());
795 Tabular::idx_type Tabular::cellCount() const
797 return numberofcells;
801 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
803 row_type const row = cellRow(cell);
805 for (col_type i = 0; i < columnCount(); ++i)
806 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
812 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
814 if (!wholerow && isMultiColumn(cell) &&
815 !(use_booktabs && cellRow(cell) == 0))
816 return cellinfo_of_cell(cell).top_line;
817 return row_info[cellRow(cell)].top_line;
821 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
823 if (!wholerow && isMultiColumn(cell) &&
824 !(use_booktabs && isLastRow(cell)))
825 return cellinfo_of_cell(cell).bottom_line;
826 return row_info[cellRow(cell)].bottom_line;
830 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
834 if (!wholecolumn && isMultiColumn(cell) &&
835 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
836 return cellinfo_of_cell(cell).left_line;
837 return column_info[cellColumn(cell)].left_line;
841 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
845 if (!wholecolumn && isMultiColumn(cell) &&
846 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
847 return cellinfo_of_cell(cell).right_line;
848 return column_info[cellRightColumn(cell)].right_line;
852 bool Tabular::topAlreadyDrawn(idx_type cell) const
854 row_type row = cellRow(cell);
855 if (row > 0 && !getAdditionalHeight(row)) {
856 col_type column = cellColumn(cell);
859 && cell_info[row][column].multicolumn
860 == Tabular::CELL_PART_OF_MULTICOLUMN)
862 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
863 return row_info[row].bottom_line;
865 return cell_info[row][column].bottom_line;
871 bool Tabular::leftAlreadyDrawn(idx_type cell) const
873 col_type column = cellColumn(cell);
875 row_type row = cellRow(cell);
877 (cell_info[row][column].multicolumn ==
878 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
879 if (getAdditionalWidth(cell_info[row][column].cellno))
881 return rightLine(cell_info[row][column].cellno);
887 bool Tabular::isLastRow(idx_type cell) const
889 return cellRow(cell) == rowCount() - 1;
893 int Tabular::getAdditionalHeight(row_type row) const
895 if (!row || row >= rowCount())
901 for (col_type column = 0; column < columnCount() && bottom; ++column) {
902 switch (cell_info[row - 1][column].multicolumn) {
903 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
904 bottom = cell_info[row - 1][column].bottom_line;
906 case Tabular::CELL_NORMAL:
907 bottom = row_info[row - 1].bottom_line;
910 for (col_type column = 0; column < columnCount() && top; ++column) {
911 switch (cell_info[row][column].multicolumn) {
912 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
913 top = cell_info[row][column].top_line;
915 case Tabular::CELL_NORMAL:
916 top = row_info[row].top_line;
919 int const interline_space = row_info[row - 1].interline_space_default ?
921 row_info[row - 1].interline_space.inPixels(width());
923 return interline_space + WIDTH_OF_LINE;
924 return interline_space;
928 int Tabular::getAdditionalWidth(idx_type cell) const
930 // internally already set in setCellWidth
931 // used to get it back in text.cpp
932 col_type const col = cellRightColumn(cell);
933 row_type const row = cellRow(cell);
934 if (col < columnCount() - 1 && rightLine(cell) &&
935 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
937 return WIDTH_OF_LINE;
943 // returns the maximum over all rows
944 int Tabular::columnWidth(idx_type cell) const
946 col_type const column1 = cellColumn(cell);
947 col_type const column2 = cellRightColumn(cell);
949 for (col_type i = column1; i <= column2; ++i)
950 result += column_info[i].width;
955 int Tabular::width() const
958 for (col_type i = 0; i < columnCount(); ++i)
959 width += column_info[i].width;
964 // returns true if a complete update is necessary, otherwise false
965 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
967 if (!isMultiColumn(cell))
970 row_type const row = cellRow(cell);
971 col_type const column1 = cellColumn(cell);
972 col_type const column2 = cellRightColumn(cell);
973 int const old_val = cell_info[row][column2].width;
975 // first set columns to 0 so we can calculate the right width
976 for (col_type i = column1; i <= column2; ++i) {
977 cell_info[row][i].width = 0;
979 // set the width to MAX_WIDTH until width > 0
980 int width = new_width + 2 * WIDTH_OF_LINE;
981 col_type i = column1;
982 for (; i < column2 && width > column_info[i].width; ++i) {
983 cell_info[row][i].width = column_info[i].width;
984 width -= column_info[i].width;
987 cell_info[row][i].width = width;
989 if (old_val != cell_info[row][column2].width) {
990 // in this case we have to recalculate all multicolumn cells which
991 // have this column as one of theirs but not as last one
992 calculate_width_of_column_NMC(i);
993 recalculateMulticolumnsOfColumn(i);
994 calculate_width_of_column(i);
1000 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1002 // the last column does not have to be recalculated because all
1003 // multicolumns will have here there last multicolumn cell which
1004 // always will have the whole rest of the width of the cell.
1005 if (columnCount() < 2 || column > (columnCount() - 2))
1007 for (row_type row = 0; row < rowCount(); ++row) {
1008 int mc = cell_info[row][column].multicolumn;
1009 int nmc = cell_info[row][column+1].multicolumn;
1010 // we only have to update multicolumns which do not have this
1011 // column as their last column!
1012 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1013 (mc == CELL_PART_OF_MULTICOLUMN &&
1014 nmc == CELL_PART_OF_MULTICOLUMN))
1016 idx_type const cellno = cell_info[row][column].cellno;
1017 setWidthOfMulticolCell(cellno,
1018 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1024 void Tabular::setCellWidth(idx_type cell, int new_width)
1026 row_type const row = cellRow(cell);
1027 col_type const column1 = cellColumn(cell);
1032 if (rightLine(cell_info[row][column1].cellno, true) &&
1033 column1 < columnCount() - 1 &&
1034 leftLine(cell_info[row][column1+1].cellno, true))
1036 add_width = WIDTH_OF_LINE;
1039 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1042 if (isMultiColumnReal(cell)) {
1043 tmp = setWidthOfMulticolCell(cell, new_width);
1045 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1046 cell_info[row][column1].width = width;
1047 tmp = calculate_width_of_column_NMC(column1);
1049 recalculateMulticolumnsOfColumn(column1);
1052 for (col_type i = 0; i < columnCount(); ++i)
1053 calculate_width_of_column(i);
1058 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1061 if (!isMultiColumn(cell) || onlycolumn)
1062 column_info[cellColumn(cell)].alignment = align;
1064 cellinfo_of_cell(cell).alignment = align;
1068 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1071 if (!isMultiColumn(cell) || onlycolumn)
1072 column_info[cellColumn(cell)].valignment = align;
1074 cellinfo_of_cell(cell).valignment = align;
1081 * Allow line and paragraph breaks for fixed width cells or disallow them,
1082 * merge cell paragraphs and reset layout to standard for variable width
1085 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1087 inset->setAutoBreakRows(fixedWidth);
1091 // merge all paragraphs to one
1092 BufferParams const & bp = cur.bv().buffer().params();
1093 while (inset->paragraphs().size() > 1)
1094 mergeParagraph(bp, inset->paragraphs(), 0);
1098 // undo information has already been recorded
1099 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1100 bp.getTextClass().defaultLayoutName());
1107 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1108 Length const & width)
1110 col_type const j = cellColumn(cell);
1112 column_info[j].p_width = width;
1113 for (row_type i = 0; i < rowCount(); ++i) {
1114 idx_type const cell = cellIndex(i, j);
1115 // because of multicolumns
1116 toggleFixedWidth(cur, getCellInset(cell).get(),
1117 !getPWidth(cell).zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1128 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1129 Length const & width)
1131 if (!isMultiColumn(cell))
1134 cellinfo_of_cell(cell).p_width = width;
1135 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1136 // cur paragraph can become invalid after paragraphs were merged
1137 if (cur.pit() > cur.lastpit())
1138 cur.pit() = cur.lastpit();
1139 // cur position can become invalid after newlines were removed
1140 if (cur.pos() > cur.lastpos())
1141 cur.pos() = cur.lastpos();
1146 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1147 Tabular::Feature what)
1149 if (what == SET_SPECIAL_MULTI)
1150 cellinfo_of_cell(cell).align_special = special;
1152 column_info[cellColumn(cell)].align_special = special;
1156 void Tabular::setAllLines(idx_type cell, bool line)
1158 setTopLine(cell, line);
1159 setBottomLine(cell, line);
1160 setRightLine(cell, line);
1161 setLeftLine(cell, line);
1165 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1167 row_type const row = cellRow(cell);
1168 if (wholerow || !isMultiColumn(cell))
1169 row_info[row].top_line = line;
1171 cellinfo_of_cell(cell).top_line = line;
1175 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1177 if (wholerow || !isMultiColumn(cell))
1178 row_info[cellRow(cell)].bottom_line = line;
1180 cellinfo_of_cell(cell).bottom_line = line;
1184 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1186 if (wholecolumn || !isMultiColumn(cell))
1187 column_info[cellColumn(cell)].left_line = line;
1189 cellinfo_of_cell(cell).left_line = line;
1193 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1195 if (wholecolumn || !isMultiColumn(cell))
1196 column_info[cellRightColumn(cell)].right_line = line;
1198 cellinfo_of_cell(cell).right_line = line;
1202 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1204 if (!onlycolumn && isMultiColumn(cell))
1205 return cellinfo_of_cell(cell).alignment;
1206 return column_info[cellColumn(cell)].alignment;
1211 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1213 if (!onlycolumn && isMultiColumn(cell))
1214 return cellinfo_of_cell(cell).valignment;
1215 return column_info[cellColumn(cell)].valignment;
1219 Length const Tabular::getPWidth(idx_type cell) const
1221 if (isMultiColumn(cell))
1222 return cellinfo_of_cell(cell).p_width;
1223 return column_info[cellColumn(cell)].p_width;
1227 Length const Tabular::getColumnPWidth(idx_type cell) const
1229 return column_info[cellColumn(cell)].p_width;
1233 Length const Tabular::getMColumnPWidth(idx_type cell) const
1235 if (isMultiColumn(cell))
1236 return cellinfo_of_cell(cell).p_width;
1241 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1243 if (what == SET_SPECIAL_MULTI)
1244 return cellinfo_of_cell(cell).align_special;
1245 return column_info[cellColumn(cell)].align_special;
1249 int Tabular::cellWidth(idx_type cell) const
1251 row_type const row = cellRow(cell);
1252 col_type const column1 = cellColumn(cell);
1253 col_type const column2 = cellRightColumn(cell);
1255 for (col_type i = column1; i <= column2; ++i)
1256 result += cell_info[row][i].width;
1261 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1265 switch (getAlignment(cell)) {
1266 case LYX_ALIGN_CENTER:
1267 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1269 case LYX_ALIGN_RIGHT:
1270 x += columnWidth(cell) - cellWidth(cell);
1271 // + getAdditionalWidth(cell);
1274 // LYX_ALIGN_LEFT: nothing :-)
1278 // the LaTeX Way :-(
1284 bool Tabular::isFirstCellInRow(idx_type cell) const
1286 return cellColumn(cell) == 0;
1290 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1292 if (row > rowCount() - 1)
1293 row = rowCount() - 1;
1294 return cell_info[row][0].cellno;
1298 bool Tabular::isLastCellInRow(idx_type cell) const
1300 return cellRightColumn(cell) == columnCount() - 1;
1304 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1306 if (row > rowCount() - 1)
1307 row = rowCount() - 1;
1308 return cell_info[row][columnCount() - 1].cellno;
1312 void Tabular::calculate_width_of_column(col_type column)
1315 for (row_type i = 0; i < rowCount(); ++i)
1316 maximum = max(cell_info[i][column].width, maximum);
1317 column_info[column].width = maximum;
1322 // Calculate the columns regarding ONLY the normal cells and if this
1323 // column is inside a multicolumn cell then use it only if its the last
1324 // column of this multicolumn cell as this gives an added width to the
1325 // column, all the rest should be adapted!
1327 bool Tabular::calculate_width_of_column_NMC(col_type column)
1329 int const old_column_width = column_info[column].width;
1331 for (row_type i = 0; i < rowCount(); ++i) {
1332 idx_type cell = cellIndex(i, column);
1333 bool ismulti = isMultiColumnReal(cell);
1334 if ((!ismulti || column == cellRightColumn(cell)) &&
1335 cell_info[i][column].width > max)
1337 max = cell_info[i][column].width;
1340 column_info[column].width = max;
1341 return column_info[column].width != old_column_width;
1345 Tabular::row_type Tabular::cellRow(idx_type cell) const
1347 if (cell >= cellCount())
1348 return rowCount() - 1;
1351 return rowofcell[cell];
1355 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1357 if (cell >= cellCount())
1358 return columnCount() - 1;
1361 return columnofcell[cell];
1365 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1367 row_type const row = cellRow(cell);
1368 col_type column = cellColumn(cell);
1369 while (column < columnCount() - 1 &&
1370 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1376 void Tabular::write(Buffer const & buf, ostream & os) const
1380 << write_attribute("version", 3)
1381 << write_attribute("rows", rowCount())
1382 << write_attribute("columns", columnCount())
1384 // global longtable options
1386 << write_attribute("rotate", rotate)
1387 << write_attribute("booktabs", use_booktabs)
1388 << write_attribute("islongtable", is_long_tabular)
1389 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1390 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1391 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1392 << write_attribute("headTopDL", endhead.topDL)
1393 << write_attribute("headBottomDL", endhead.bottomDL)
1394 << write_attribute("footTopDL", endfoot.topDL)
1395 << write_attribute("footBottomDL", endfoot.bottomDL)
1396 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1397 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1398 << write_attribute("lastFootEmpty", endlastfoot.empty)
1400 for (col_type j = 0; j < columnCount(); ++j) {
1402 << write_attribute("alignment", column_info[j].alignment)
1403 << write_attribute("valignment", column_info[j].valignment)
1404 << write_attribute("leftline", column_info[j].left_line)
1405 << write_attribute("rightline", column_info[j].right_line)
1406 << write_attribute("width", column_info[j].p_width.asString())
1407 << write_attribute("special", column_info[j].align_special)
1410 for (row_type i = 0; i < rowCount(); ++i) {
1412 << write_attribute("topline", row_info[i].top_line)
1413 << write_attribute("bottomline", row_info[i].bottom_line);
1414 static const string def("default");
1415 if (row_info[i].top_space_default)
1416 os << write_attribute("topspace", def);
1418 os << write_attribute("topspace", row_info[i].top_space);
1419 if (row_info[i].bottom_space_default)
1420 os << write_attribute("bottomspace", def);
1422 os << write_attribute("bottomspace", row_info[i].bottom_space);
1423 if (row_info[i].interline_space_default)
1424 os << write_attribute("interlinespace", def);
1426 os << write_attribute("interlinespace", row_info[i].interline_space);
1427 os << write_attribute("endhead", row_info[i].endhead)
1428 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1429 << write_attribute("endfoot", row_info[i].endfoot)
1430 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1431 << write_attribute("newpage", row_info[i].newpage)
1433 for (col_type j = 0; j < columnCount(); ++j) {
1435 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1436 << write_attribute("alignment", cell_info[i][j].alignment)
1437 << write_attribute("valignment", cell_info[i][j].valignment)
1438 << write_attribute("topline", cell_info[i][j].top_line)
1439 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1440 << write_attribute("leftline", cell_info[i][j].left_line)
1441 << write_attribute("rightline", cell_info[i][j].right_line)
1442 << write_attribute("rotate", cell_info[i][j].rotate)
1443 << write_attribute("usebox", cell_info[i][j].usebox)
1444 << write_attribute("width", cell_info[i][j].p_width)
1445 << write_attribute("special", cell_info[i][j].align_special)
1447 os << "\\begin_inset ";
1448 cell_info[i][j].inset->write(buf, os);
1449 os << "\n\\end_inset\n"
1454 os << "</lyxtabular>\n";
1458 void Tabular::read(Buffer const & buf, Lexer & lex)
1461 istream & is = lex.getStream();
1463 l_getline(is, line);
1464 if (!prefixIs(line, "<lyxtabular ")
1465 && !prefixIs(line, "<Tabular ")) {
1466 BOOST_ASSERT(false);
1471 if (!getTokenValue(line, "version", version))
1473 BOOST_ASSERT(version >= 2);
1476 if (!getTokenValue(line, "rows", rows_arg))
1479 if (!getTokenValue(line, "columns", columns_arg))
1481 init(buf.params(), rows_arg, columns_arg);
1482 l_getline(is, line);
1483 if (!prefixIs(line, "<features")) {
1484 lyxerr << "Wrong tabular format (expected <features ...> got"
1485 << line << ')' << endl;
1488 getTokenValue(line, "rotate", rotate);
1489 getTokenValue(line, "booktabs", use_booktabs);
1490 getTokenValue(line, "islongtable", is_long_tabular);
1491 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1492 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1493 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1494 getTokenValue(line, "headTopDL", endhead.topDL);
1495 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1496 getTokenValue(line, "footTopDL", endfoot.topDL);
1497 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1498 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1499 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1500 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1502 for (col_type j = 0; j < columnCount(); ++j) {
1504 if (!prefixIs(line,"<column")) {
1505 lyxerr << "Wrong tabular format (expected <column ...> got"
1506 << line << ')' << endl;
1509 getTokenValue(line, "alignment", column_info[j].alignment);
1510 getTokenValue(line, "valignment", column_info[j].valignment);
1511 getTokenValue(line, "leftline", column_info[j].left_line);
1512 getTokenValue(line, "rightline", column_info[j].right_line);
1513 getTokenValue(line, "width", column_info[j].p_width);
1514 getTokenValue(line, "special", column_info[j].align_special);
1517 for (row_type i = 0; i < rowCount(); ++i) {
1518 l_getline(is, line);
1519 if (!prefixIs(line, "<row")) {
1520 lyxerr << "Wrong tabular format (expected <row ...> got"
1521 << line << ')' << endl;
1524 getTokenValue(line, "topline", row_info[i].top_line);
1525 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1526 getTokenValue(line, "topspace", row_info[i].top_space,
1527 row_info[i].top_space_default);
1528 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1529 row_info[i].bottom_space_default);
1530 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1531 row_info[i].interline_space_default);
1532 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1533 getTokenValue(line, "endhead", row_info[i].endhead);
1534 getTokenValue(line, "endfoot", row_info[i].endfoot);
1535 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1536 getTokenValue(line, "newpage", row_info[i].newpage);
1537 for (col_type j = 0; j < columnCount(); ++j) {
1538 l_getline(is, line);
1539 if (!prefixIs(line, "<cell")) {
1540 lyxerr << "Wrong tabular format (expected <cell ...> got"
1541 << line << ')' << endl;
1544 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1545 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1546 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1547 getTokenValue(line, "topline", cell_info[i][j].top_line);
1548 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1549 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1550 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1551 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1552 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1553 getTokenValue(line, "width", cell_info[i][j].p_width);
1554 getTokenValue(line, "special", cell_info[i][j].align_special);
1555 l_getline(is, line);
1556 if (prefixIs(line, "\\begin_inset")) {
1557 cell_info[i][j].inset->read(buf, lex);
1558 l_getline(is, line);
1560 if (!prefixIs(line, "</cell>")) {
1561 lyxerr << "Wrong tabular format (expected </cell> got"
1562 << line << ')' << endl;
1566 l_getline(is, line);
1567 if (!prefixIs(line, "</row>")) {
1568 lyxerr << "Wrong tabular format (expected </row> got"
1569 << line << ')' << endl;
1573 while (!prefixIs(line, "</lyxtabular>")) {
1574 l_getline(is, line);
1580 bool Tabular::isMultiColumn(idx_type cell) const
1582 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1586 bool Tabular::isMultiColumnReal(idx_type cell) const
1588 return cellColumn(cell) != cellRightColumn(cell) &&
1589 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1593 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1595 return cell_info[cellRow(cell)][cellColumn(cell)];
1599 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1602 cellstruct & cs = cellinfo_of_cell(cell);
1603 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1604 cs.alignment = column_info[cellColumn(cell)].alignment;
1605 cs.top_line = row_info[cellRow(cell)].top_line;
1606 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1607 cs.left_line = column_info[cellColumn(cell)].left_line;
1608 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1609 for (idx_type i = 1; i < number; ++i) {
1610 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1611 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1612 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1619 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1621 row_type const row = cellRow(cell);
1622 col_type column = cellColumn(cell);
1623 idx_type result = 1;
1625 while (column < columnCount() &&
1626 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1635 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1637 row_type const row = cellRow(cell);
1638 col_type column = cellColumn(cell);
1640 idx_type result = 0;
1642 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1643 cell_info[row][column].multicolumn = CELL_NORMAL;
1645 while (column < columnCount() &&
1646 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1648 cell_info[row][column].multicolumn = CELL_NORMAL;
1658 void Tabular::setBookTabs(bool what)
1660 use_booktabs = what;
1664 bool Tabular::useBookTabs() const
1666 return use_booktabs;
1670 void Tabular::setLongTabular(bool what)
1672 is_long_tabular = what;
1676 bool Tabular::isLongTabular() const
1678 return is_long_tabular;
1682 void Tabular::setRotateTabular(bool flag)
1688 bool Tabular::getRotateTabular() const
1694 void Tabular::setRotateCell(idx_type cell, bool flag)
1696 cellinfo_of_cell(cell).rotate = flag;
1700 bool Tabular::getRotateCell(idx_type cell) const
1702 return cellinfo_of_cell(cell).rotate;
1706 bool Tabular::needRotating() const
1710 for (row_type i = 0; i < rowCount(); ++i)
1711 for (col_type j = 0; j < columnCount(); ++j)
1712 if (cell_info[i][j].rotate)
1718 bool Tabular::isLastCell(idx_type cell) const
1720 if (cell + 1 < cellCount())
1726 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1728 if (cellRow(cell) > 0)
1729 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1734 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1736 if (cellRow(cell) + 1 < rowCount())
1737 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1742 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1744 if (cellRow(cell) == 0)
1746 if (!isMultiColumn(cell))
1747 return getCellAbove(cell);
1748 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1752 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1754 if (cellRow(cell) + 1 >= rowCount())
1756 if (!isMultiColumn(cell))
1757 return getCellBelow(cell);
1758 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1762 Tabular::idx_type Tabular::cellIndex(row_type row,
1763 col_type column) const
1765 BOOST_ASSERT(column != npos && column < columnCount() &&
1766 row != npos && row < rowCount());
1767 return cell_info[row][column].cellno;
1771 void Tabular::setUsebox(idx_type cell, BoxType type)
1773 cellinfo_of_cell(cell).usebox = type;
1777 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1779 if (column_info[cellColumn(cell)].p_width.zero() &&
1780 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1782 if (cellinfo_of_cell(cell).usebox > 1)
1783 return cellinfo_of_cell(cell).usebox;
1784 return useParbox(cell);
1789 // This are functions used for the longtable support
1791 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1797 row_info[row].endfirsthead = flag;
1801 row_info[row].endhead = flag;
1806 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1809 hd.set = haveLTHead();
1810 return row_info[row].endhead;
1814 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1817 hd.set = haveLTFirstHead();
1818 return row_info[row].endfirsthead;
1822 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1828 row_info[row].endlastfoot = flag;
1832 row_info[row].endfoot = flag;
1837 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1840 fd.set = haveLTFoot();
1841 return row_info[row].endfoot;
1845 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1848 fd.set = haveLTLastFoot();
1849 return row_info[row].endlastfoot;
1853 void Tabular::setLTNewPage(row_type row, bool what)
1855 row_info[row].newpage = what;
1859 bool Tabular::getLTNewPage(row_type row) const
1861 return row_info[row].newpage;
1865 bool Tabular::haveLTHead() const
1867 for (row_type i = 0; i < rowCount(); ++i)
1868 if (row_info[i].endhead)
1874 bool Tabular::haveLTFirstHead() const
1876 if (endfirsthead.empty)
1878 for (row_type i = 0; i < rowCount(); ++i)
1879 if (row_info[i].endfirsthead)
1885 bool Tabular::haveLTFoot() const
1887 for (row_type i = 0; i < rowCount(); ++i)
1888 if (row_info[i].endfoot)
1894 bool Tabular::haveLTLastFoot() const
1896 if (endlastfoot.empty)
1898 for (row_type i = 0; i < rowCount(); ++i)
1899 if (row_info[i].endlastfoot)
1905 // end longtable support functions
1907 void Tabular::setRowAscent(row_type row, int height)
1909 if (row >= rowCount() || row_info[row].ascent == height)
1911 row_info[row].ascent = height;
1915 void Tabular::setRowDescent(row_type row, int height)
1917 if (row >= rowCount() || row_info[row].descent == height)
1919 row_info[row].descent = height;
1923 int Tabular::rowAscent(row_type row) const
1925 if (row >= rowCount())
1927 return row_info[row].ascent;
1931 int Tabular::rowDescent(row_type row) const
1933 BOOST_ASSERT(row < rowCount());
1934 return row_info[row].descent;
1938 int Tabular::height() const
1941 for (row_type row = 0; row < rowCount(); ++row)
1942 height += rowAscent(row) + rowDescent(row) +
1943 getAdditionalHeight(row);
1948 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1950 BOOST_ASSERT(row < rowCount());
1951 BOOST_ASSERT(column < columnCount());
1952 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1956 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1958 // FIXME: assert or return 0 as in TeXBottomHLine()?
1959 BOOST_ASSERT(row != npos);
1960 BOOST_ASSERT(row < rowCount());
1962 idx_type const fcell = getFirstCellInRow(row);
1963 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1966 for (idx_type i = fcell; i < n; ++i) {
1970 if (use_booktabs && row == 0) {
1972 } else if (tmp == n - fcell) {
1973 os << (use_booktabs ? "\\midrule " : "\\hline ");
1975 for (idx_type i = fcell; i < n; ++i) {
1977 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1978 << cellColumn(i) + 1
1980 << cellRightColumn(i) + 1
1992 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1994 // FIXME: return 0 or assert as in TeXTopHLine()?
1995 if (row == npos || row >= rowCount())
1998 idx_type const fcell = getFirstCellInRow(row);
1999 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2002 for (idx_type i = fcell; i < n; ++i) {
2006 if (use_booktabs && row == rowCount() - 1) {
2007 os << "\\bottomrule";
2008 } else if (tmp == n - fcell) {
2009 os << (use_booktabs ? "\\midrule" : "\\hline");
2011 for (idx_type i = fcell; i < n; ++i) {
2012 if (bottomLine(i)) {
2013 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2014 << cellColumn(i) + 1
2016 << cellRightColumn(i) + 1
2028 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2032 if (getRotateCell(cell)) {
2033 os << "\\begin{sideways}\n";
2036 if (isMultiColumn(cell)) {
2037 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2038 if (leftLine(cell) &&
2039 (isFirstCellInRow(cell) ||
2040 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2041 !rightLine(cell - 1, true))))
2043 if (!cellinfo_of_cell(cell).align_special.empty()) {
2044 os << cellinfo_of_cell(cell).align_special;
2046 if (!getPWidth(cell).zero()) {
2047 switch (getVAlignment(cell)) {
2048 case LYX_VALIGN_TOP:
2051 case LYX_VALIGN_MIDDLE:
2054 case LYX_VALIGN_BOTTOM:
2059 << from_ascii(getPWidth(cell).asLatexString())
2062 switch (getAlignment(cell)) {
2063 case LYX_ALIGN_LEFT:
2066 case LYX_ALIGN_RIGHT:
2073 } // end if else !getPWidth
2074 } // end if else !cellinfo_of_cell
2075 if (rightLine(cell))
2077 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2082 if (getUsebox(cell) == BOX_PARBOX) {
2084 switch (getVAlignment(cell)) {
2085 case LYX_VALIGN_TOP:
2088 case LYX_VALIGN_MIDDLE:
2091 case LYX_VALIGN_BOTTOM:
2095 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2097 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2098 os << "\\begin{minipage}[";
2099 switch (getVAlignment(cell)) {
2100 case LYX_VALIGN_TOP:
2103 case LYX_VALIGN_MIDDLE:
2106 case LYX_VALIGN_BOTTOM:
2110 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2118 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2123 if (getUsebox(cell) == BOX_PARBOX)
2125 else if (getUsebox(cell) == BOX_MINIPAGE) {
2126 os << "%\n\\end{minipage}";
2129 if (isMultiColumn(cell)) {
2132 if (getRotateCell(cell)) {
2133 os << "%\n\\end{sideways}";
2140 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2141 OutputParams const & runparams) const
2143 if (!is_long_tabular)
2147 // output header info
2149 if (endhead.topDL) {
2153 for (row_type i = 0; i < rowCount(); ++i) {
2154 if (row_info[i].endhead) {
2155 ret += TeXRow(os, i, buf, runparams);
2158 if (endhead.bottomDL) {
2162 os << "\\endhead\n";
2164 if (endfirsthead.empty) {
2165 os << "\\endfirsthead\n";
2169 // output firstheader info
2170 if (haveLTFirstHead()) {
2171 if (endfirsthead.topDL) {
2175 for (row_type i = 0; i < rowCount(); ++i) {
2176 if (row_info[i].endfirsthead) {
2177 ret += TeXRow(os, i, buf, runparams);
2180 if (endfirsthead.bottomDL) {
2184 os << "\\endfirsthead\n";
2187 // output footer info
2189 if (endfoot.topDL) {
2193 for (row_type i = 0; i < rowCount(); ++i) {
2194 if (row_info[i].endfoot) {
2195 ret += TeXRow(os, i, buf, runparams);
2198 if (endfoot.bottomDL) {
2202 os << "\\endfoot\n";
2204 if (endlastfoot.empty) {
2205 os << "\\endlastfoot\n";
2209 // output lastfooter info
2210 if (haveLTLastFoot()) {
2211 if (endlastfoot.topDL) {
2215 for (row_type i = 0; i < rowCount(); ++i) {
2216 if (row_info[i].endlastfoot) {
2217 ret += TeXRow(os, i, buf, runparams);
2220 if (endlastfoot.bottomDL) {
2224 os << "\\endlastfoot\n";
2231 bool Tabular::isValidRow(row_type row) const
2233 if (!is_long_tabular)
2235 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2236 !row_info[row].endfoot && !row_info[row].endlastfoot;
2240 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2241 OutputParams const & runparams) const
2243 idx_type cell = cellIndex(i, 0);
2244 int ret = TeXTopHLine(os, i);
2245 if (row_info[i].top_space_default) {
2247 os << "\\addlinespace\n";
2249 os << "\\noalign{\\vskip\\doublerulesep}\n";
2251 } else if(!row_info[i].top_space.zero()) {
2253 os << "\\addlinespace["
2254 << from_ascii(row_info[i].top_space.asLatexString())
2257 os << "\\noalign{\\vskip"
2258 << from_ascii(row_info[i].top_space.asLatexString())
2264 for (col_type j = 0; j < columnCount(); ++j) {
2265 if (isPartOfMultiColumn(i, j))
2267 ret += TeXCellPreamble(os, cell);
2268 shared_ptr<InsetText> inset = getCellInset(cell);
2270 Paragraph const & par = inset->paragraphs().front();
2271 bool rtl = par.isRTL(buf.params())
2273 && getPWidth(cell).zero();
2276 if (par.getParLanguage(buf.params())->lang() ==
2279 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2281 // currently, remaning RTL languages are arabic_arabtex and hebrew
2285 ret += inset->latex(buf, os, runparams);
2289 ret += TeXCellPostamble(os, cell);
2290 if (!isLastCellInRow(cell)) { // not last cell in row
2295 os << "\\tabularnewline";
2296 if (row_info[i].bottom_space_default) {
2298 os << "\\addlinespace";
2300 os << "[\\doublerulesep]";
2301 } else if (!row_info[i].bottom_space.zero()) {
2303 os << "\\addlinespace";
2305 << from_ascii(row_info[i].bottom_space.asLatexString())
2310 ret += TeXBottomHLine(os, i);
2311 if (row_info[i].interline_space_default) {
2313 os << "\\addlinespace\n";
2315 os << "\\noalign{\\vskip\\doublerulesep}\n";
2317 } else if (!row_info[i].interline_space.zero()) {
2319 os << "\\addlinespace["
2320 << from_ascii(row_info[i].interline_space.asLatexString())
2323 os << "\\noalign{\\vskip"
2324 << from_ascii(row_info[i].interline_space.asLatexString())
2332 int Tabular::latex(Buffer const & buf, odocstream & os,
2333 OutputParams const & runparams) const
2337 //+---------------------------------------------------------------------
2338 //+ first the opening preamble +
2339 //+---------------------------------------------------------------------
2342 os << "\\begin{sideways}\n";
2345 if (is_long_tabular)
2346 os << "\\begin{longtable}{";
2348 os << "\\begin{tabular}{";
2349 for (col_type i = 0; i < columnCount(); ++i) {
2350 if (!use_booktabs && column_info[i].left_line)
2352 if (!column_info[i].align_special.empty()) {
2353 os << column_info[i].align_special;
2355 if (!column_info[i].p_width.zero()) {
2356 switch (column_info[i].alignment) {
2357 case LYX_ALIGN_LEFT:
2358 os << ">{\\raggedright}";
2360 case LYX_ALIGN_RIGHT:
2361 os << ">{\\raggedleft}";
2363 case LYX_ALIGN_CENTER:
2364 os << ">{\\centering}";
2366 case LYX_ALIGN_NONE:
2367 case LYX_ALIGN_BLOCK:
2368 case LYX_ALIGN_LAYOUT:
2369 case LYX_ALIGN_SPECIAL:
2373 switch (column_info[i].valignment) {
2374 case LYX_VALIGN_TOP:
2377 case LYX_VALIGN_MIDDLE:
2380 case LYX_VALIGN_BOTTOM:
2385 << from_ascii(column_info[i].p_width.asLatexString())
2388 switch (column_info[i].alignment) {
2389 case LYX_ALIGN_LEFT:
2392 case LYX_ALIGN_RIGHT:
2399 } // end if else !column_info[i].p_width
2400 } // end if else !column_info[i].align_special
2401 if (!use_booktabs && column_info[i].right_line)
2407 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2409 //+---------------------------------------------------------------------
2410 //+ the single row and columns (cells) +
2411 //+---------------------------------------------------------------------
2413 for (row_type i = 0; i < rowCount(); ++i) {
2414 if (isValidRow(i)) {
2415 ret += TeXRow(os, i, buf, runparams);
2416 if (is_long_tabular && row_info[i].newpage) {
2417 os << "\\newpage\n";
2423 //+---------------------------------------------------------------------
2424 //+ the closing of the tabular +
2425 //+---------------------------------------------------------------------
2427 if (is_long_tabular)
2428 os << "\\end{longtable}";
2430 os << "\\end{tabular}";
2432 os << "\n\\end{sideways}";
2440 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2441 OutputParams const & runparams) const
2444 idx_type cell = getFirstCellInRow(row);
2447 for (col_type j = 0; j < columnCount(); ++j) {
2448 if (isPartOfMultiColumn(row, j))
2451 os << "<entry align=\"";
2452 switch (getAlignment(cell)) {
2453 case LYX_ALIGN_LEFT:
2456 case LYX_ALIGN_RIGHT:
2464 os << "\" valign=\"";
2465 switch (getVAlignment(cell)) {
2466 case LYX_VALIGN_TOP:
2469 case LYX_VALIGN_BOTTOM:
2472 case LYX_VALIGN_MIDDLE:
2477 if (isMultiColumn(cell)) {
2478 os << " namest=\"col" << j << "\" ";
2479 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2483 ret += getCellInset(cell)->docbook(buf, os, runparams);
2492 int Tabular::docbook(Buffer const & buf, odocstream & os,
2493 OutputParams const & runparams) const
2497 //+---------------------------------------------------------------------
2498 //+ first the opening preamble +
2499 //+---------------------------------------------------------------------
2501 os << "<tgroup cols=\"" << columnCount()
2502 << "\" colsep=\"1\" rowsep=\"1\">\n";
2504 for (col_type i = 0; i < columnCount(); ++i) {
2505 os << "<colspec colname=\"col" << i << "\" align=\"";
2506 switch (column_info[i].alignment) {
2507 case LYX_ALIGN_LEFT:
2510 case LYX_ALIGN_RIGHT:
2518 if (runparams.flavor == OutputParams::XML)
2524 //+---------------------------------------------------------------------
2525 //+ Long Tabular case +
2526 //+---------------------------------------------------------------------
2528 // output header info
2529 if (haveLTHead() || haveLTFirstHead()) {
2532 for (row_type i = 0; i < rowCount(); ++i) {
2533 if (row_info[i].endhead || row_info[i].endfirsthead) {
2534 ret += docbookRow(buf, os, i, runparams);
2540 // output footer info
2541 if (haveLTFoot() || haveLTLastFoot()) {
2544 for (row_type i = 0; i < rowCount(); ++i) {
2545 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2546 ret += docbookRow(buf, os, i, runparams);
2553 //+---------------------------------------------------------------------
2554 //+ the single row and columns (cells) +
2555 //+---------------------------------------------------------------------
2559 for (row_type i = 0; i < rowCount(); ++i) {
2560 if (isValidRow(i)) {
2561 ret += docbookRow(buf, os, i, runparams);
2566 //+---------------------------------------------------------------------
2567 //+ the closing of the tabular +
2568 //+---------------------------------------------------------------------
2577 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2578 vector<unsigned int> const & clen) const
2580 idx_type const fcell = getFirstCellInRow(row);
2581 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2584 for (idx_type i = fcell; i < n; ++i) {
2594 for (idx_type i = fcell; i < n; ++i) {
2605 col_type column = cellColumn(i);
2606 int len = clen[column];
2607 while (column < columnCount() - 1
2608 && isPartOfMultiColumn(row, ++column))
2609 len += clen[column] + 4;
2610 os << docstring(len, ch);
2625 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2626 vector<unsigned int> const & clen) const
2628 idx_type const fcell = getFirstCellInRow(row);
2629 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2632 for (idx_type i = fcell; i < n; ++i) {
2633 if (bottomLine(i)) {
2642 for (idx_type i = fcell; i < n; ++i) {
2643 if (bottomLine(i)) {
2653 col_type column = cellColumn(i);
2654 int len = clen[column];
2655 while (column < columnCount() -1
2656 && isPartOfMultiColumn(row, ++column))
2657 len += clen[column] + 4;
2658 os << docstring(len, ch);
2659 if (bottomLine(i)) {
2673 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2674 OutputParams const & runparams,
2675 idx_type cell, row_type row, col_type column,
2676 vector<unsigned int> const & clen,
2677 bool onlydata) const
2679 odocstringstream sstr;
2680 getCellInset(cell)->plaintext(buf, sstr, runparams);
2692 unsigned int len1 = sstr.str().length();
2693 unsigned int len2 = clen[column];
2694 while (column < columnCount() -1
2695 && isPartOfMultiColumn(row, ++column))
2696 len2 += clen[column] + 4;
2699 switch (getAlignment(cell)) {
2701 case LYX_ALIGN_LEFT:
2704 case LYX_ALIGN_RIGHT:
2708 case LYX_ALIGN_CENTER:
2714 os << docstring(len1, ' ') << sstr.str()
2715 << docstring(len2, ' ');
2717 if (rightLine(cell))
2724 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2725 OutputParams const & runparams, int const depth,
2726 bool onlydata, unsigned char delim) const
2728 // first calculate the width of the single columns
2729 vector<unsigned int> clen(columnCount());
2732 // first all non (real) multicolumn cells!
2733 for (col_type j = 0; j < columnCount(); ++j) {
2735 for (row_type i = 0; i < rowCount(); ++i) {
2736 idx_type cell = cellIndex(i, j);
2737 if (isMultiColumnReal(cell))
2739 odocstringstream sstr;
2740 getCellInset(cell)->plaintext(buf, sstr, runparams);
2741 if (clen[j] < sstr.str().length())
2742 clen[j] = sstr.str().length();
2745 // then all (real) multicolumn cells!
2746 for (col_type j = 0; j < columnCount(); ++j) {
2747 for (row_type i = 0; i < rowCount(); ++i) {
2748 idx_type cell = cellIndex(i, j);
2749 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2751 odocstringstream sstr;
2752 getCellInset(cell)->plaintext(buf, sstr, runparams);
2753 int len = int(sstr.str().length());
2754 idx_type const n = cells_in_multicolumn(cell);
2755 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2757 if (len > int(clen[j + n - 1]))
2758 clen[j + n - 1] = len;
2763 for (row_type i = 0; i < rowCount(); ++i) {
2764 if (!onlydata && plaintextTopHLine(os, i, clen))
2765 os << docstring(depth * 2, ' ');
2766 for (col_type j = 0; j < columnCount(); ++j) {
2767 if (isPartOfMultiColumn(i, j))
2769 if (onlydata && j > 0)
2771 plaintextPrintCell(buf, os, runparams,
2772 cell, i, j, clen, onlydata);
2777 os << docstring(depth * 2, ' ');
2778 if (plaintextBottomHLine(os, i, clen))
2779 os << docstring(depth * 2, ' ');
2785 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2787 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2791 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2792 col_type column) const
2794 return cell_info[row][column].inset;
2798 void Tabular::setCellInset(row_type row, col_type column,
2799 shared_ptr<InsetText> ins) const
2801 cell_info[row][column].inset = ins;
2806 Tabular::getCellFromInset(Inset const * inset) const
2808 // is this inset part of the tabular?
2810 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2811 BOOST_ASSERT(false);
2814 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2815 if (getCellInset(cell).get() == inset) {
2816 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2817 << "cell=" << cell << endl;
2821 // We should have found a cell at this point
2822 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2823 << inset << " not found!" << endl;
2824 BOOST_ASSERT(false);
2830 void Tabular::validate(LaTeXFeatures & features) const
2832 features.require("NeedTabularnewline");
2834 features.require("booktabs");
2835 if (isLongTabular())
2836 features.require("longtable");
2838 features.require("rotating");
2839 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2840 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2841 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2842 features.require("array");
2843 getCellInset(cell)->validate(features);
2848 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2850 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2851 ParagraphList::const_iterator cit = parlist.begin();
2852 ParagraphList::const_iterator end = parlist.end();
2854 for (; cit != end; ++cit)
2855 for (int i = 0; i < cit->size(); ++i)
2856 if (cit->isNewline(i))
2863 /////////////////////////////////////////////////////////////////////
2867 /////////////////////////////////////////////////////////////////////
2869 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2871 : tabular(buf.params(), max(rows, row_type(1)),
2872 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2876 InsetTabular::InsetTabular(InsetTabular const & tab)
2877 : Inset(tab), tabular(tab.tabular),
2878 buffer_(tab.buffer_), scx_(0)
2882 InsetTabular::~InsetTabular()
2884 InsetTabularMailer(*this).hideDialog();
2888 Inset * InsetTabular::clone() const
2890 return new InsetTabular(*this);
2894 Buffer const & InsetTabular::buffer() const
2900 void InsetTabular::buffer(Buffer const * b)
2906 void InsetTabular::write(Buffer const & buf, ostream & os) const
2908 os << "Tabular" << endl;
2909 tabular.write(buf, os);
2913 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2915 bool const old_format = (lex.getString() == "\\LyXTable");
2917 tabular.read(buf, lex);
2923 string token = lex.getString();
2924 while (lex && token != "\\end_inset") {
2926 token = lex.getString();
2929 lex.printError("Missing \\end_inset at this point. "
2935 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2937 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2938 // mi.base.textwidth << "\n";
2940 lyxerr << "InsetTabular::metrics: need bv" << endl;
2941 BOOST_ASSERT(false);
2945 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2948 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2949 if (tabular.isPartOfMultiColumn(i, j))
2950 // Multicolumn cell, but not first one
2955 if (tabular.cell_info[i][j].multicolumn ==
2956 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2957 p_width = tabular.cellinfo_of_cell(cell).p_width;
2959 p_width = tabular.column_info[j].p_width;
2960 if (!p_width.zero())
2961 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2962 tabular.getCellInset(cell)->metrics(m, dim);
2963 if (!p_width.zero())
2964 dim.wid = m.base.textwidth;
2965 tabular.setCellWidth(cell, dim.wid);
2966 if (p_width.zero()) {
2967 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2968 // FIXME there must be a way to get rid of
2969 // the second metrics call
2970 tabular.getCellInset(cell)->metrics(m, dim);
2972 maxAsc = max(maxAsc, dim.asc);
2973 maxDesc = max(maxDesc, dim.des);
2976 int const top_space = tabular.row_info[i].top_space_default ?
2977 default_line_space :
2978 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2979 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2980 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2981 default_line_space :
2982 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2983 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2986 dim.asc = tabular.rowAscent(0);
2987 dim.des = tabular.height() - dim.asc;
2988 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2992 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2994 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2995 BufferView * bv = pi.base.bv;
2997 // FIXME: As the full backrgound is painted in drawSelection(),
2998 // we have no choice but to do a full repaint for the Text cells.
2999 pi.full_repaint = true;
3001 resetPos(bv->cursor());
3004 x += ADD_TO_TABULAR_WIDTH;
3007 first_visible_cell = Tabular::npos;
3008 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3010 int const a = tabular.rowAscent(i);
3011 int const d = tabular.rowDescent(i);
3012 idx = tabular.cellIndex(i, 0);
3013 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3014 if (tabular.isPartOfMultiColumn(i, j))
3016 if (first_visible_cell == Tabular::npos)
3017 first_visible_cell = idx;
3019 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3020 // Cache the Inset position.
3021 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3022 if (nx + tabular.columnWidth(idx) < 0
3023 || nx > bv->workWidth()
3025 || y - a > bv->workHeight()) {
3026 pi.pain.setDrawingEnabled(false);
3027 cell(idx)->draw(pi, cx, y);
3028 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3029 pi.pain.setDrawingEnabled(true);
3031 cell(idx)->draw(pi, cx, y);
3032 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3034 nx += tabular.columnWidth(idx);
3038 if (i + 1 < tabular.rowCount())
3039 y += d + tabular.rowAscent(i + 1) +
3040 tabular.getAdditionalHeight(i + 1);
3045 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3047 Cursor & cur = pi.base.bv->cursor();
3049 x += scx_ + ADD_TO_TABULAR_WIDTH;
3051 // FIXME: it is wrong to completely paint the background
3052 // if we want to do single row painting.
3054 // Paint background of current tabular
3055 int const w = tabular.width();
3056 int const h = tabular.height();
3057 int yy = y - tabular.rowAscent(0);
3058 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3060 if (!cur.selection())
3062 if (!ptr_cmp(&cur.inset(), this))
3068 if (tablemode(cur)) {
3071 getSelection(cur, rs, re, cs, ce);
3072 y -= tabular.rowAscent(0);
3073 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3074 int const a = tabular.rowAscent(j);
3075 int const h = a + tabular.rowDescent(j);
3077 y += tabular.getAdditionalHeight(j);
3078 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3079 if (tabular.isPartOfMultiColumn(j, i))
3081 idx_type const cell =
3082 tabular.cellIndex(j, i);
3083 int const w = tabular.columnWidth(cell);
3084 if (i >= cs && i <= ce && j >= rs && j <= re)
3085 pi.pain.fillRectangle(xx, y, w, h,
3093 x += getCellXPos(cur.idx());
3094 x += tabular.getBeginningOfTextInCell(cur.idx());
3095 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3100 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3101 row_type row, idx_type cell, bool erased) const
3103 int x2 = x + tabular.columnWidth(cell);
3104 bool on_off = false;
3105 Color::color col = Color::tabularline;
3106 Color::color onoffcol = Color::tabularonoffline;
3109 col = Color::deletedtext;
3110 onoffcol = Color::deletedtext;
3113 if (!tabular.topAlreadyDrawn(cell)) {
3114 on_off = !tabular.topLine(cell);
3115 pain.line(x, y - tabular.rowAscent(row),
3116 x2, y - tabular.rowAscent(row),
3117 on_off ? onoffcol : col,
3118 on_off ? Painter::line_onoffdash : Painter::line_solid);
3120 on_off = !tabular.bottomLine(cell);
3121 pain.line(x, y + tabular.rowDescent(row),
3122 x2, y + tabular.rowDescent(row),
3123 on_off ? onoffcol : col,
3124 on_off ? Painter::line_onoffdash : Painter::line_solid);
3125 if (!tabular.leftAlreadyDrawn(cell)) {
3126 on_off = !tabular.leftLine(cell);
3127 pain.line(x, y - tabular.rowAscent(row),
3128 x, y + tabular.rowDescent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3132 on_off = !tabular.rightLine(cell);
3133 pain.line(x2 - tabular.getAdditionalWidth(cell),
3134 y - tabular.rowAscent(row),
3135 x2 - tabular.getAdditionalWidth(cell),
3136 y + tabular.rowDescent(row),
3137 on_off ? onoffcol : col,
3138 on_off ? Painter::line_onoffdash : Painter::line_solid);
3142 docstring const InsetTabular::editMessage() const
3144 return _("Opened table");
3148 void InsetTabular::edit(Cursor & cur, bool left)
3150 //lyxerr << "InsetTabular::edit: " << this << endl;
3152 cur.selection() = false;
3155 if (isRightToLeft(cur))
3156 cur.idx() = tabular.getLastCellInRow(0);
3162 if (isRightToLeft(cur))
3163 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3165 cur.idx() = tabular.cellCount() - 1;
3167 cur.pos() = cur.lastpos(); // FIXME crude guess
3169 // FIXME: this accesses the position cache before it is initialized
3171 //cur.bv().fitCursor();
3175 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3177 // In a longtable, tell captions what the current float is
3178 Counters & cnts = buf.params().getTextClass().counters();
3179 string const saveflt = cnts.current_float();
3180 if (tabular.isLongTabular())
3181 cnts.current_float("table");
3183 ParIterator it2 = it;
3185 size_t const end = it2.nargs();
3186 for ( ; it2.idx() < end; it2.top().forwardIdx())
3187 lyx::updateLabels(buf, it2);
3190 if (tabular.isLongTabular())
3191 cnts.current_float(saveflt);
3195 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3197 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3198 << "\n cur:" << cur << endl;
3199 CursorSlice sl = cur.top();
3200 Cursor & bvcur = cur.bv().cursor();
3202 switch (cmd.action) {
3204 case LFUN_MOUSE_PRESS:
3205 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3207 // do not reset cursor/selection if we have selected
3208 // some cells (bug 2715).
3209 if (cmd.button() == mouse_button::button3
3210 && &bvcur.selBegin().inset() == this
3211 && tablemode(bvcur))
3214 // Let InsetText do it
3215 cell(cur.idx())->dispatch(cur, cmd);
3217 case LFUN_MOUSE_MOTION:
3218 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3219 if (cmd.button() == mouse_button::button1) {
3220 // only accept motions to places not deeper nested than the real anchor
3221 if (bvcur.anchor_.hasPart(cur)) {
3222 // only update if selection changes
3223 if (bvcur.idx() == cur.idx() &&
3224 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3226 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3227 bvcur.setCursor(cur);
3228 bvcur.selection() = true;
3234 case LFUN_MOUSE_RELEASE:
3235 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3236 if (cmd.button() == mouse_button::button3)
3237 InsetTabularMailer(*this).showDialog(&cur.bv());
3240 case LFUN_CELL_BACKWARD:
3242 cur.selection() = false;
3245 case LFUN_CELL_FORWARD:
3247 cur.selection() = false;
3250 case LFUN_CHAR_FORWARD_SELECT:
3251 case LFUN_CHAR_FORWARD:
3252 cell(cur.idx())->dispatch(cur, cmd);
3253 if (!cur.result().dispatched()) {
3254 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
3255 if (sl == cur.top())
3256 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3262 case LFUN_CHAR_BACKWARD_SELECT:
3263 case LFUN_CHAR_BACKWARD:
3264 cell(cur.idx())->dispatch(cur, cmd);
3265 if (!cur.result().dispatched()) {
3266 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
3267 if (sl == cur.top())
3268 cmd = FuncRequest(LFUN_FINISHED_LEFT);
3274 case LFUN_DOWN_SELECT:
3276 cell(cur.idx())->dispatch(cur, cmd);
3277 cur.dispatched(); // override the cell's decision
3278 if (sl == cur.top())
3279 // if our Text didn't do anything to the cursor
3280 // then we try to put the cursor into the cell below
3281 // setting also the right targetX.
3282 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3283 cur.idx() = tabular.getCellBelow(cur.idx());
3285 TextMetrics const & tm =
3286 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3287 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3289 if (sl == cur.top()) {
3290 // we trick it to go to the RIGHT after leaving the
3292 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
3297 case LFUN_UP_SELECT:
3299 cell(cur.idx())->dispatch(cur, cmd);
3300 cur.dispatched(); // override the cell's decision
3301 if (sl == cur.top())
3302 // if our Text didn't do anything to the cursor
3303 // then we try to put the cursor into the cell above
3304 // setting also the right targetX.
3305 if (tabular.cellRow(cur.idx()) != 0) {
3306 cur.idx() = tabular.getCellAbove(cur.idx());
3307 cur.pit() = cur.lastpit();
3308 Text const * text = cell(cur.idx())->getText(0);
3309 TextMetrics const & tm = cur.bv().textMetrics(text);
3310 ParagraphMetrics const & pm =
3311 tm.parMetrics(cur.lastpit());
3312 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3314 if (sl == cur.top()) {
3315 cmd = FuncRequest(LFUN_UP);
3320 // case LFUN_SCREEN_DOWN: {
3321 // //if (hasSelection())
3322 // // cur.selection() = false;
3323 // col_type const col = tabular.cellColumn(cur.idx());
3324 // int const t = cur.bv().top_y() + cur.bv().height();
3325 // if (t < yo() + tabular.getHeightOfTabular()) {
3326 // cur.bv().scrollDocView(t);
3327 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3329 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3336 // case LFUN_SCREEN_UP: {
3337 // //if (hasSelection())
3338 // // cur.selection() = false;
3339 // col_type const col = tabular.cellColumn(cur.idx());
3340 // int const t = cur.bv().top_y() + cur.bv().height();
3342 // cur.bv().scrollDocView(t);
3346 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3350 // cur.par() = cur.lastpar();
3351 // cur.pos() = cur.lastpos();
3355 case LFUN_LAYOUT_TABULAR:
3356 InsetTabularMailer(*this).showDialog(&cur.bv());
3359 case LFUN_INSET_DIALOG_UPDATE:
3360 InsetTabularMailer(*this).updateDialog(&cur.bv());
3363 case LFUN_TABULAR_FEATURE:
3364 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3368 // insert file functions
3369 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3370 case LFUN_FILE_INSERT_PLAINTEXT: {
3372 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
3373 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3379 if (tablemode(cur)) {
3380 if (copySelection(cur)) {
3381 recordUndoInset(cur, Undo::DELETE);
3386 cell(cur.idx())->dispatch(cur, cmd);
3389 case LFUN_CHAR_DELETE_BACKWARD:
3390 case LFUN_CHAR_DELETE_FORWARD:
3391 if (tablemode(cur)) {
3392 recordUndoInset(cur, Undo::DELETE);
3396 cell(cur.idx())->dispatch(cur, cmd);
3400 if (!cur.selection())
3402 if (tablemode(cur)) {
3406 cell(cur.idx())->dispatch(cur, cmd);
3409 case LFUN_CLIPBOARD_PASTE:
3410 case LFUN_PRIMARY_SELECTION_PASTE: {
3411 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3412 theClipboard().getAsText() :
3413 theSelection().get();
3416 // pass to InsertPlaintextString, but
3417 // only if we have multi-cell content
3418 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3419 if (insertPlaintextString(cur.bv(), clip, false)) {
3420 // content has been replaced,
3421 // so cursor might be invalid
3422 cur.pos() = cur.lastpos();
3423 bvcur.setCursor(cur);
3427 // Let the cell handle normal text
3428 cell(cur.idx())->dispatch(cur, cmd);
3433 if (tabularStackDirty() && theClipboard().isInternal()) {
3434 recordUndoInset(cur, Undo::INSERT);
3435 pasteClipboard(cur);
3438 cell(cur.idx())->dispatch(cur, cmd);
3441 case LFUN_FONT_EMPH:
3442 case LFUN_FONT_BOLD:
3443 case LFUN_FONT_ROMAN:
3444 case LFUN_FONT_NOUN:
3445 case LFUN_FONT_ITAL:
3446 case LFUN_FONT_FRAK:
3447 case LFUN_FONT_TYPEWRITER:
3448 case LFUN_FONT_SANS:
3449 case LFUN_FONT_FREE_APPLY:
3450 case LFUN_FONT_FREE_UPDATE:
3451 case LFUN_FONT_SIZE:
3452 case LFUN_FONT_UNDERLINE:
3454 case LFUN_WORD_CAPITALIZE:
3455 case LFUN_WORD_UPCASE:
3456 case LFUN_WORD_LOWCASE:
3457 case LFUN_CHARS_TRANSPOSE:
3458 if (tablemode(cur)) {
3461 getSelection(cur, rs, re, cs, ce);
3462 Cursor tmpcur = cur;
3463 for (row_type i = rs; i <= re; ++i) {
3464 for (col_type j = cs; j <= ce; ++j) {
3465 // cursor follows cell:
3466 tmpcur.idx() = tabular.cellIndex(i, j);
3467 // select this cell only:
3470 tmpcur.resetAnchor();
3471 tmpcur.pit() = tmpcur.lastpit();
3472 tmpcur.pos() = tmpcur.top().lastpos();
3473 tmpcur.setCursor(tmpcur);
3474 tmpcur.setSelection();
3475 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3480 cell(cur.idx())->dispatch(cur, cmd);
3484 // we try to handle this event in the insets dispatch function.
3485 cell(cur.idx())->dispatch(cur, cmd);
3491 // function sets an object as defined in func_status.h:
3492 // states OK, Unknown, Disabled, On, Off.
3493 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3494 FuncStatus & status) const
3496 switch (cmd.action) {
3497 case LFUN_TABULAR_FEATURE: {
3498 int action = Tabular::LAST_ACTION;
3500 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3501 string const tmp = tabularFeature[i].feature;
3502 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3503 action = tabularFeature[i].action;
3507 if (action == Tabular::LAST_ACTION) {
3509 status.unknown(true);
3513 string const argument
3514 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3516 row_type sel_row_start = 0;
3517 row_type sel_row_end = 0;
3518 col_type sel_col_start = 0;
3519 col_type sel_col_end = 0;
3520 Tabular::ltType dummyltt;
3523 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3526 case Tabular::SET_PWIDTH:
3527 case Tabular::SET_MPWIDTH:
3528 case Tabular::SET_SPECIAL_COLUMN:
3529 case Tabular::SET_SPECIAL_MULTI:
3530 case Tabular::APPEND_ROW:
3531 case Tabular::APPEND_COLUMN:
3532 case Tabular::DELETE_ROW:
3533 case Tabular::DELETE_COLUMN:
3534 case Tabular::COPY_ROW:
3535 case Tabular::COPY_COLUMN:
3536 case Tabular::SET_ALL_LINES:
3537 case Tabular::UNSET_ALL_LINES:
3538 case Tabular::SET_TOP_SPACE:
3539 case Tabular::SET_BOTTOM_SPACE:
3540 case Tabular::SET_INTERLINE_SPACE:
3544 case Tabular::MULTICOLUMN:
3545 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3548 case Tabular::M_TOGGLE_LINE_TOP:
3550 case Tabular::TOGGLE_LINE_TOP:
3551 status.setOnOff(tabular.topLine(cur.idx(), flag));
3554 case Tabular::M_TOGGLE_LINE_BOTTOM:
3556 case Tabular::TOGGLE_LINE_BOTTOM:
3557 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3560 case Tabular::M_TOGGLE_LINE_LEFT:
3562 case Tabular::TOGGLE_LINE_LEFT:
3563 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3566 case Tabular::M_TOGGLE_LINE_RIGHT:
3568 case Tabular::TOGGLE_LINE_RIGHT:
3569 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3572 case Tabular::M_ALIGN_LEFT:
3574 case Tabular::ALIGN_LEFT:
3575 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3578 case Tabular::M_ALIGN_RIGHT:
3580 case Tabular::ALIGN_RIGHT:
3581 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3584 case Tabular::M_ALIGN_CENTER:
3586 case Tabular::ALIGN_CENTER:
3587 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3590 case Tabular::ALIGN_BLOCK:
3591 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3592 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3595 case Tabular::M_VALIGN_TOP:
3597 case Tabular::VALIGN_TOP:
3599 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3602 case Tabular::M_VALIGN_BOTTOM:
3604 case Tabular::VALIGN_BOTTOM:
3606 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3609 case Tabular::M_VALIGN_MIDDLE:
3611 case Tabular::VALIGN_MIDDLE:
3613 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3616 case Tabular::SET_LONGTABULAR:
3617 status.setOnOff(tabular.isLongTabular());
3620 case Tabular::UNSET_LONGTABULAR:
3621 status.setOnOff(!tabular.isLongTabular());
3624 case Tabular::TOGGLE_ROTATE_TABULAR:
3625 case Tabular::SET_ROTATE_TABULAR:
3626 status.setOnOff(tabular.getRotateTabular());
3629 case Tabular::UNSET_ROTATE_TABULAR:
3630 status.setOnOff(!tabular.getRotateTabular());
3633 case Tabular::TOGGLE_ROTATE_CELL:
3634 case Tabular::SET_ROTATE_CELL:
3635 status.setOnOff(!oneCellHasRotationState(false,
3636 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3639 case Tabular::UNSET_ROTATE_CELL:
3640 status.setOnOff(!oneCellHasRotationState(true,
3641 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3644 case Tabular::SET_USEBOX:
3645 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3648 case Tabular::SET_LTFIRSTHEAD:
3649 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3652 case Tabular::UNSET_LTFIRSTHEAD:
3653 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3656 case Tabular::SET_LTHEAD:
3657 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3660 case Tabular::UNSET_LTHEAD:
3661 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3664 case Tabular::SET_LTFOOT:
3665 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3668 case Tabular::UNSET_LTFOOT:
3669 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3672 case Tabular::SET_LTLASTFOOT:
3673 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3676 case Tabular::UNSET_LTLASTFOOT:
3677 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3680 case Tabular::SET_LTNEWPAGE:
3681 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3684 case Tabular::SET_BOOKTABS:
3685 status.setOnOff(tabular.useBookTabs());
3688 case Tabular::UNSET_BOOKTABS:
3689 status.setOnOff(!tabular.useBookTabs());
3694 status.enabled(false);
3700 // These are only enabled inside tabular
3701 case LFUN_CELL_BACKWARD:
3702 case LFUN_CELL_FORWARD:
3703 status.enabled(true);
3706 // disable these with multiple cells selected
3707 case LFUN_INSET_INSERT:
3708 case LFUN_TABULAR_INSERT:
3709 case LFUN_FLEX_INSERT:
3710 case LFUN_FLOAT_INSERT:
3711 case LFUN_FLOAT_WIDE_INSERT:
3712 case LFUN_FOOTNOTE_INSERT:
3713 case LFUN_MARGINALNOTE_INSERT:
3714 case LFUN_MATH_INSERT:
3715 case LFUN_MATH_MODE:
3716 case LFUN_MATH_MUTATE:
3717 case LFUN_MATH_DISPLAY:
3718 case LFUN_NOTE_INSERT:
3719 case LFUN_OPTIONAL_INSERT:
3720 case LFUN_BOX_INSERT:
3721 case LFUN_BRANCH_INSERT:
3722 case LFUN_WRAP_INSERT:
3723 case LFUN_ERT_INSERT: {
3724 if (tablemode(cur)) {
3725 status.enabled(false);
3728 return cell(cur.idx())->getStatus(cur, cmd, status);
3731 // disable in non-fixed-width cells
3732 case LFUN_BREAK_LINE:
3733 case LFUN_BREAK_PARAGRAPH:
3734 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
3735 case LFUN_BREAK_PARAGRAPH_SKIP: {
3736 if (tabular.getPWidth(cur.idx()).zero()) {
3737 status.enabled(false);
3740 return cell(cur.idx())->getStatus(cur, cmd, status);
3744 if (tabularStackDirty() && theClipboard().isInternal()) {
3745 status.enabled(true);
3748 return cell(cur.idx())->getStatus(cur, cmd, status);
3750 case LFUN_INSET_MODIFY:
3751 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
3752 status.enabled(true);
3758 // we try to handle this event in the insets dispatch function.
3759 return cell(cur.idx())->getStatus(cur, cmd, status);
3764 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3765 OutputParams const & runparams) const
3767 return tabular.latex(buf, os, runparams);
3771 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3772 OutputParams const & runparams) const
3774 os << '\n'; // output table on a new line
3775 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3776 tabular.plaintext(buf, os, runparams, dp, false, 0);
3777 return PLAINTEXT_NEWLINE;
3781 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3782 OutputParams const & runparams) const
3787 // FIXME: Why not pass a proper DocIterator here?
3789 // if the table is inside a float it doesn't need the informaltable
3790 // wrapper. Search for it.
3791 for (master = owner(); master; master = master->owner())
3792 if (master->lyxCode() == Inset::FLOAT_CODE)
3797 os << "<informaltable>";
3800 ret += tabular.docbook(buf, os, runparams);
3802 os << "</informaltable>";
3809 void InsetTabular::validate(LaTeXFeatures & features) const
3811 tabular.validate(features);
3815 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3817 return tabular.getCellInset(idx);
3821 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3823 return tabular.getCellInset(idx);
3827 void InsetTabular::cursorPos(BufferView const & bv,
3828 CursorSlice const & sl, bool boundary, int & x, int & y) const
3830 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3832 // y offset correction
3833 int const row = tabular.cellRow(sl.idx());
3834 for (int i = 0; i <= row; ++i) {
3836 y += tabular.rowAscent(i);
3837 y += tabular.getAdditionalHeight(i);
3840 y += tabular.rowDescent(i);
3843 // x offset correction
3844 int const col = tabular.cellColumn(sl.idx());
3845 int idx = tabular.cellIndex(row, 0);
3846 for (int j = 0; j < col; ++j) {
3847 if (tabular.isPartOfMultiColumn(row, j))
3849 x += tabular.columnWidth(idx);
3852 x += tabular.getBeginningOfTextInCell(idx);
3853 x += ADD_TO_TABULAR_WIDTH;
3858 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3862 Inset const & inset = *tabular.getCellInset(cell);
3863 Point o = bv.coordCache().getInsets().xy(&inset);
3864 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3865 int const xend = xbeg + tabular.columnWidth(cell);
3866 row_type const row = tabular.cellRow(cell);
3867 int const ybeg = o.y_ - tabular.rowAscent(row) -
3868 tabular.getAdditionalHeight(row);
3869 int const yend = o.y_ + tabular.rowDescent(row);
3881 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3882 // << " ybeg=" << ybeg << " yend=" << yend
3883 // << " xx=" << xx << " yy=" << yy
3884 // << " dist=" << xx + yy << endl;
3889 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3891 //lyxerr << "InsetTabular::editXY: " << this << endl;
3892 cur.selection() = false;
3894 cur.idx() = getNearestCell(cur.bv(), x, y);
3896 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3900 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3902 cur.idx() = getNearestCell(cur.bv(), x, y);
3903 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3907 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3909 idx_type idx_min = 0;
3910 int dist_min = std::numeric_limits<int>::max();
3911 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3912 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3913 int const d = dist(bv, i, x, y);
3924 int InsetTabular::getCellXPos(idx_type const cell) const
3928 for (; !tabular.isFirstCellInRow(c); --c)
3931 for (; c < cell; ++c)
3932 lx += tabular.columnWidth(c);
3938 void InsetTabular::resetPos(Cursor & cur) const
3940 BufferView & bv = cur.bv();
3941 int const maxwidth = bv.workWidth();
3943 if (&cur.inset() != this) {
3947 int const X2 = maxwidth;
3948 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3949 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3950 int const x2 = x1 + tabular.columnWidth(cur.idx());
3953 scx_ = X1 + 20 - x1;
3955 scx_ = X2 - 20 - x2;
3960 cur.updateFlags(Update::Force | Update::FitCursor);
3964 void InsetTabular::moveNextCell(Cursor & cur)
3966 if (isRightToLeft(cur)) {
3967 if (tabular.isFirstCellInRow(cur.idx())) {
3968 row_type const row = tabular.cellRow(cur.idx());
3969 if (row == tabular.rowCount() - 1)
3971 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
3978 if (tabular.isLastCell(cur.idx()))
3988 void InsetTabular::movePrevCell(Cursor & cur)
3990 if (isRightToLeft(cur)) {
3991 if (tabular.isLastCellInRow(cur.idx())) {
3992 row_type const row = tabular.cellRow(cur.idx());
3995 cur.idx() = tabular.getFirstCellInRow(row);
3996 cur.idx() = tabular.getCellAbove(cur.idx());
3998 if (tabular.isLastCell(cur.idx()))
4003 if (cur.idx() == 0) // first cell
4007 cur.pit() = cur.lastpit();
4008 cur.pos() = cur.lastpos();
4010 // FIXME: this accesses the position cache before it is initialized
4015 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4017 Tabular::Feature action = Tabular::LAST_ACTION;
4020 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4021 string const tmp = tabularFeature[i].feature;
4023 if (tmp == what.substr(0, tmp.length())) {
4024 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4025 //tabularFeatures[i].feature.length()))
4026 action = tabularFeature[i].action;
4030 if (action == Tabular::LAST_ACTION)
4034 ltrim(what.substr(tabularFeature[i].feature.length()));
4035 tabularFeatures(cur, action, val);
4040 static void checkLongtableSpecial(Tabular::ltType & ltt,
4041 string const & special, bool & flag)
4043 if (special == "dl_above") {
4046 } else if (special == "dl_below") {
4047 ltt.bottomDL = flag;
4049 } else if (special == "empty") {
4058 bool InsetTabular::oneCellHasRotationState(bool rotated,
4059 row_type row_start, row_type row_end,
4060 col_type col_start, col_type col_end) const {
4062 for (row_type i = row_start; i <= row_end; ++i) {
4063 for (col_type j = col_start; j <= col_end; ++j) {
4064 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4073 void InsetTabular::tabularFeatures(Cursor & cur,
4074 Tabular::Feature feature, string const & value)
4076 BufferView & bv = cur.bv();
4077 col_type sel_col_start;
4078 col_type sel_col_end;
4079 row_type sel_row_start;
4080 row_type sel_row_end;
4081 bool setLines = false;
4082 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4083 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4087 case Tabular::M_ALIGN_LEFT:
4088 case Tabular::ALIGN_LEFT:
4089 setAlign = LYX_ALIGN_LEFT;
4092 case Tabular::M_ALIGN_RIGHT:
4093 case Tabular::ALIGN_RIGHT:
4094 setAlign = LYX_ALIGN_RIGHT;
4097 case Tabular::M_ALIGN_CENTER:
4098 case Tabular::ALIGN_CENTER:
4099 setAlign = LYX_ALIGN_CENTER;
4102 case Tabular::ALIGN_BLOCK:
4103 setAlign = LYX_ALIGN_BLOCK;
4106 case Tabular::M_VALIGN_TOP:
4107 case Tabular::VALIGN_TOP:
4108 setVAlign = Tabular::LYX_VALIGN_TOP;
4111 case Tabular::M_VALIGN_BOTTOM:
4112 case Tabular::VALIGN_BOTTOM:
4113 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4116 case Tabular::M_VALIGN_MIDDLE:
4117 case Tabular::VALIGN_MIDDLE:
4118 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4125 recordUndoInset(cur, Undo::ATOMIC);
4127 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4128 row_type const row = tabular.cellRow(cur.idx());
4129 col_type const column = tabular.cellColumn(cur.idx());
4131 Tabular::ltType ltt;
4135 case Tabular::SET_PWIDTH: {
4136 Length const len(value);
4137 tabular.setColumnPWidth(cur, cur.idx(), len);
4139 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4140 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4144 case Tabular::SET_MPWIDTH:
4145 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4148 case Tabular::SET_SPECIAL_COLUMN:
4149 case Tabular::SET_SPECIAL_MULTI:
4150 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4153 case Tabular::APPEND_ROW:
4154 // append the row into the tabular
4155 tabular.appendRow(bv.buffer().params(), cur.idx());
4158 case Tabular::APPEND_COLUMN:
4159 // append the column into the tabular
4160 tabular.appendColumn(bv.buffer().params(), cur.idx());
4161 cur.idx() = tabular.cellIndex(row, column);
4164 case Tabular::DELETE_ROW:
4165 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4166 tabular.deleteRow(sel_row_start);
4167 if (sel_row_start >= tabular.rowCount())
4169 cur.idx() = tabular.cellIndex(sel_row_start, column);
4172 cur.selection() = false;
4175 case Tabular::DELETE_COLUMN:
4176 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4177 tabular.deleteColumn(sel_col_start);
4178 if (sel_col_start >= tabular.columnCount())
4180 cur.idx() = tabular.cellIndex(row, sel_col_start);
4183 cur.selection() = false;
4186 case Tabular::COPY_ROW:
4187 tabular.copyRow(bv.buffer().params(), row);
4190 case Tabular::COPY_COLUMN:
4191 tabular.copyColumn(bv.buffer().params(), column);
4192 cur.idx() = tabular.cellIndex(row, column);
4195 case Tabular::M_TOGGLE_LINE_TOP:
4197 case Tabular::TOGGLE_LINE_TOP: {
4198 bool lineSet = !tabular.topLine(cur.idx(), flag);
4199 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4200 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4202 tabular.cellIndex(i, j),
4207 case Tabular::M_TOGGLE_LINE_BOTTOM:
4209 case Tabular::TOGGLE_LINE_BOTTOM: {
4210 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4211 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4212 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4213 tabular.setBottomLine(
4214 tabular.cellIndex(i, j),
4220 case Tabular::M_TOGGLE_LINE_LEFT:
4222 case Tabular::TOGGLE_LINE_LEFT: {
4223 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4224 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4225 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4226 tabular.setLeftLine(
4227 tabular.cellIndex(i,j),
4233 case Tabular::M_TOGGLE_LINE_RIGHT:
4235 case Tabular::TOGGLE_LINE_RIGHT: {
4236 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4237 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4238 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4239 tabular.setRightLine(
4240 tabular.cellIndex(i,j),
4246 case Tabular::M_ALIGN_LEFT:
4247 case Tabular::M_ALIGN_RIGHT:
4248 case Tabular::M_ALIGN_CENTER:
4250 case Tabular::ALIGN_LEFT:
4251 case Tabular::ALIGN_RIGHT:
4252 case Tabular::ALIGN_CENTER:
4253 case Tabular::ALIGN_BLOCK:
4254 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4255 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4256 tabular.setAlignment(
4257 tabular.cellIndex(i, j),
4262 case Tabular::M_VALIGN_TOP:
4263 case Tabular::M_VALIGN_BOTTOM:
4264 case Tabular::M_VALIGN_MIDDLE:
4266 case Tabular::VALIGN_TOP:
4267 case Tabular::VALIGN_BOTTOM:
4268 case Tabular::VALIGN_MIDDLE:
4269 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4270 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4271 tabular.setVAlignment(
4272 tabular.cellIndex(i, j),
4276 case Tabular::MULTICOLUMN: {
4277 if (sel_row_start != sel_row_end) {
4278 // FIXME: Need I say it ? This is horrible.
4280 Alert::error(_("Error setting multicolumn"),
4281 _("You cannot set multicolumn vertically."));
4284 if (!cur.selection()) {
4285 // just multicol for one single cell
4286 // check whether we are completely in a multicol
4287 if (tabular.isMultiColumn(cur.idx()))
4288 tabular.unsetMultiColumn(cur.idx());
4290 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4293 // we have a selection so this means we just add all this
4294 // cells to form a multicolumn cell
4295 idx_type const s_start = cur.selBegin().idx();
4296 idx_type const s_end = cur.selEnd().idx();
4297 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4298 cur.idx() = s_start;
4301 cur.selection() = false;
4305 case Tabular::SET_ALL_LINES:
4307 case Tabular::UNSET_ALL_LINES:
4308 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4309 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4310 tabular.setAllLines(
4311 tabular.cellIndex(i,j), setLines);
4314 case Tabular::SET_LONGTABULAR:
4315 tabular.setLongTabular(true);
4318 case Tabular::UNSET_LONGTABULAR:
4319 tabular.setLongTabular(false);
4322 case Tabular::SET_ROTATE_TABULAR:
4323 tabular.setRotateTabular(true);
4326 case Tabular::UNSET_ROTATE_TABULAR:
4327 tabular.setRotateTabular(false);
4330 case Tabular::TOGGLE_ROTATE_TABULAR:
4331 tabular.setRotateTabular(!tabular.getRotateTabular());
4334 case Tabular::SET_ROTATE_CELL:
4335 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4336 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4337 tabular.setRotateCell(
4338 tabular.cellIndex(i, j), true);
4341 case Tabular::UNSET_ROTATE_CELL:
4342 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4343 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4344 tabular.setRotateCell(
4345 tabular.cellIndex(i, j), false);
4348 case Tabular::TOGGLE_ROTATE_CELL:
4350 bool oneNotRotated = oneCellHasRotationState(false,
4351 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4353 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4354 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4355 tabular.setRotateCell(tabular.cellIndex(i, j),
4360 case Tabular::SET_USEBOX: {
4361 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4362 if (val == tabular.getUsebox(cur.idx()))
4363 val = Tabular::BOX_NONE;
4364 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4365 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4366 tabular.setUsebox(tabular.cellIndex(i, j), val);
4370 case Tabular::UNSET_LTFIRSTHEAD:
4372 case Tabular::SET_LTFIRSTHEAD:
4373 tabular.getRowOfLTFirstHead(row, ltt);
4374 checkLongtableSpecial(ltt, value, flag);
4375 tabular.setLTHead(row, flag, ltt, true);
4378 case Tabular::UNSET_LTHEAD:
4380 case Tabular::SET_LTHEAD:
4381 tabular.getRowOfLTHead(row, ltt);
4382 checkLongtableSpecial(ltt, value, flag);
4383 tabular.setLTHead(row, flag, ltt, false);
4386 case Tabular::UNSET_LTFOOT:
4388 case Tabular::SET_LTFOOT:
4389 tabular.getRowOfLTFoot(row, ltt);
4390 checkLongtableSpecial(ltt, value, flag);
4391 tabular.setLTFoot(row, flag, ltt, false);
4394 case Tabular::UNSET_LTLASTFOOT:
4396 case Tabular::SET_LTLASTFOOT:
4397 tabular.getRowOfLTLastFoot(row, ltt);
4398 checkLongtableSpecial(ltt, value, flag);
4399 tabular.setLTFoot(row, flag, ltt, true);
4402 case Tabular::SET_LTNEWPAGE:
4403 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4406 case Tabular::SET_BOOKTABS:
4407 tabular.setBookTabs(true);
4410 case Tabular::UNSET_BOOKTABS:
4411 tabular.setBookTabs(false);
4414 case Tabular::SET_TOP_SPACE: {
4416 if (value == "default")
4417 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4418 tabular.row_info[i].top_space_default = true;
4419 else if (isValidLength(value, &len))
4420 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4421 tabular.row_info[i].top_space_default = false;
4422 tabular.row_info[i].top_space = len;
4425 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4426 tabular.row_info[i].top_space_default = false;
4427 tabular.row_info[i].top_space = len;
4432 case Tabular::SET_BOTTOM_SPACE: {
4434 if (value == "default")
4435 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4436 tabular.row_info[i].bottom_space_default = true;
4437 else if (isValidLength(value, &len))
4438 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4439 tabular.row_info[i].bottom_space_default = false;
4440 tabular.row_info[i].bottom_space = len;
4443 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4444 tabular.row_info[i].bottom_space_default = false;
4445 tabular.row_info[i].bottom_space = len;
4450 case Tabular::SET_INTERLINE_SPACE: {
4452 if (value == "default")
4453 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4454 tabular.row_info[i].interline_space_default = true;
4455 else if (isValidLength(value, &len))
4456 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4457 tabular.row_info[i].interline_space_default = false;
4458 tabular.row_info[i].interline_space = len;
4461 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4462 tabular.row_info[i].interline_space_default = false;
4463 tabular.row_info[i].interline_space = len;
4468 // dummy stuff just to avoid warnings
4469 case Tabular::LAST_ACTION:
4475 bool InsetTabular::showInsetDialog(BufferView * bv) const
4477 InsetTabularMailer(*this).showDialog(bv);
4482 void InsetTabular::openLayoutDialog(BufferView * bv) const
4484 InsetTabularMailer(*this).showDialog(bv);
4488 bool InsetTabular::copySelection(Cursor & cur)
4490 if (!cur.selection())
4495 getSelection(cur, rs, re, cs, ce);
4497 paste_tabular.reset(new Tabular(tabular));
4499 for (row_type i = 0; i < rs; ++i)
4500 paste_tabular->deleteRow(0);
4502 row_type const rows = re - rs + 1;
4503 while (paste_tabular->rowCount() > rows)
4504 paste_tabular->deleteRow(rows);
4506 paste_tabular->setTopLine(0, true, true);
4507 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4510 for (col_type i = 0; i < cs; ++i)
4511 paste_tabular->deleteColumn(0);
4513 col_type const columns = ce - cs + 1;
4514 while (paste_tabular->columnCount() > columns)
4515 paste_tabular->deleteColumn(columns);
4517 paste_tabular->setLeftLine(0, true, true);
4518 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4521 odocstringstream os;
4522 OutputParams const runparams(0);
4523 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4524 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4525 cap::copySelection(cur, os.str());
4527 // mark tabular stack dirty
4528 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4529 // when we (hopefully) have a one-for-all paste mechanism.
4530 // This must be called after cap::copySelection.
4531 dirtyTabularStack(true);
4537 bool InsetTabular::pasteClipboard(Cursor & cur)
4541 col_type const actcol = tabular.cellColumn(cur.idx());
4542 row_type const actrow = tabular.cellRow(cur.idx());
4543 for (row_type r1 = 0, r2 = actrow;
4544 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4546 for (col_type c1 = 0, c2 = actcol;
4547 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4549 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4550 tabular.isPartOfMultiColumn(r2, c2))
4552 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4556 if (tabular.isPartOfMultiColumn(r2, c2)) {
4560 shared_ptr<InsetText> inset(
4561 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4562 tabular.setCellInset(r2, c2, inset);
4563 // FIXME: change tracking (MG)
4564 inset->setChange(Change(cur.buffer().params().trackChanges ?
4565 Change::INSERTED : Change::UNCHANGED));
4573 void InsetTabular::cutSelection(Cursor & cur)
4575 if (!cur.selection())
4580 getSelection(cur, rs, re, cs, ce);
4581 for (row_type i = rs; i <= re; ++i) {
4582 for (col_type j = cs; j <= ce; ++j) {
4583 shared_ptr<InsetText> t
4584 = cell(tabular.cellIndex(i, j));
4585 if (cur.buffer().params().trackChanges)
4586 // FIXME: Change tracking (MG)
4587 t->setChange(Change(Change::DELETED));
4593 // cursor position might be invalid now
4594 if (cur.pit() > cur.lastpit())
4595 cur.pit() = cur.lastpit();
4596 if (cur.pos() > cur.lastpos())
4597 cur.pos() = cur.lastpos();
4598 cur.clearSelection();
4602 bool InsetTabular::isRightToLeft(Cursor & cur) const
4604 BOOST_ASSERT(cur.depth() > 1);
4605 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4606 pos_type const parentpos = cur[cur.depth() - 2].pos();
4607 return parentpar.getFontSettings(cur.bv().buffer().params(),
4608 parentpos).language()->rightToLeft();
4612 void InsetTabular::getSelection(Cursor & cur,
4613 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4615 CursorSlice const & beg = cur.selBegin();
4616 CursorSlice const & end = cur.selEnd();
4617 cs = tabular.cellColumn(beg.idx());
4618 ce = tabular.cellColumn(end.idx());
4621 cs = tabular.cellColumn(end.idx());
4623 ce = tabular.cellRightColumn(end.idx());
4626 rs = tabular.cellRow(beg.idx());
4627 re = tabular.cellRow(end.idx());
4633 Text * InsetTabular::getText(int idx) const
4635 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4639 void InsetTabular::setChange(Change const & change)
4641 for (idx_type idx = 0; idx < nargs(); ++idx)
4642 cell(idx)->setChange(change);
4646 void InsetTabular::acceptChanges(BufferParams const & bparams)
4648 for (idx_type idx = 0; idx < nargs(); ++idx)
4649 cell(idx)->acceptChanges(bparams);
4653 void InsetTabular::rejectChanges(BufferParams const & bparams)
4655 for (idx_type idx = 0; idx < nargs(); ++idx)
4656 cell(idx)->rejectChanges(bparams);
4660 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4662 return tabular.getPWidth(cell).zero();
4666 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4669 if (buf.length() <= 0)
4672 Buffer const & buffer = bv.buffer();
4676 col_type maxCols = 1;
4677 docstring::size_type const len = buf.length();
4678 docstring::size_type p = 0;
4681 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4689 maxCols = max(cols, maxCols);
4695 maxCols = max(cols, maxCols);
4701 paste_tabular.reset(
4702 new Tabular(buffer.params(), rows, maxCols));
4703 loctab = paste_tabular.get();
4705 dirtyTabularStack(true);
4708 cell = bv.cursor().idx();
4709 ocol = tabular.cellColumn(cell);
4710 row = tabular.cellRow(cell);
4713 docstring::size_type op = 0;
4714 idx_type const cells = loctab->cellCount();
4717 rows = loctab->rowCount();
4718 col_type const columns = loctab->columnCount();
4720 while (cell < cells && p < len && row < rows &&
4721 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4727 // we can only set this if we are not too far right
4728 if (cols < columns) {
4729 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4730 Font const font = bv.textMetrics(&inset->text_).
4731 getDisplayFont(0, 0);
4732 inset->setText(buf.substr(op, p - op), font,
4733 buffer.params().trackChanges);
4739 // we can only set this if we are not too far right
4740 if (cols < columns) {
4741 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4742 Font const font = bv.textMetrics(&inset->text_).
4743 getDisplayFont(0, 0);
4744 inset->setText(buf.substr(op, p - op), font,
4745 buffer.params().trackChanges);
4750 cell = loctab->cellIndex(row, cols);
4756 // check for the last cell if there is no trailing '\n'
4757 if (cell < cells && op < len) {
4758 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4759 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4760 inset->setText(buf.substr(op, len - op), font,
4761 buffer.params().trackChanges);
4767 void InsetTabular::addPreview(PreviewLoader & loader) const
4769 row_type const rows = tabular.rowCount();
4770 col_type const columns = tabular.columnCount();
4771 for (row_type i = 0; i < rows; ++i) {
4772 for (col_type j = 0; j < columns; ++j)
4773 tabular.getCellInset(i, j)->addPreview(loader);
4778 bool InsetTabular::tablemode(Cursor & cur) const
4780 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4786 string const InsetTabularMailer::name_("tabular");
4788 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4789 : inset_(const_cast<InsetTabular &>(inset))
4793 string const InsetTabularMailer::inset2string(Buffer const &) const
4795 return params2string(inset_);
4799 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4801 istringstream data(in);
4803 lex.setStream(data);
4810 if (!lex || token != name_)
4811 return print_mailer_error("InsetTabularMailer", in, 1,
4814 // This is part of the inset proper that is usually swallowed
4815 // by Buffer::readInset
4817 if (!lex || token != "Tabular")
4818 return print_mailer_error("InsetTabularMailer", in, 2,
4821 Buffer const & buffer = inset.buffer();
4822 inset.read(buffer, lex);
4826 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4829 data << name_ << ' ';
4830 inset.write(inset.buffer(), data);
4831 data << "\\end_inset\n";