2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jean-Marc Lasgouttes
11 * \author Angus Leeming
13 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "paragraph_funcs.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextMetrics.h"
48 #include "support/convert.h"
49 #include "support/lstrings.h"
51 #include "frontends/alert.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
56 #include <boost/scoped_ptr.hpp>
66 using std::istringstream;
69 using std::ostringstream;
74 #ifndef CXX_GLOBAL_CSTD
78 using boost::shared_ptr;
79 using boost::dynamic_pointer_cast;
83 using support::prefixIs;
86 using support::suffixIs;
88 using cap::dirtyTabularStack;
89 using cap::tabularStackDirty;
91 using graphics::PreviewLoader;
93 using frontend::Painter;
94 using frontend::Clipboard;
96 namespace Alert = frontend::Alert;
101 int const ADD_TO_HEIGHT = 2;
102 int const ADD_TO_TABULAR_WIDTH = 2;
103 int const default_line_space = 10;
104 int const WIDTH_OF_LINE = 5;
108 boost::scoped_ptr<Tabular> paste_tabular;
111 struct TabularFeature {
112 Tabular::Feature action;
117 TabularFeature tabularFeature[] =
119 { Tabular::APPEND_ROW, "append-row" },
120 { Tabular::APPEND_COLUMN, "append-column" },
121 { Tabular::DELETE_ROW, "delete-row" },
122 { Tabular::DELETE_COLUMN, "delete-column" },
123 { Tabular::COPY_ROW, "copy-row" },
124 { Tabular::COPY_COLUMN, "copy-column" },
125 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
126 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
127 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
128 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
129 { Tabular::ALIGN_LEFT, "align-left" },
130 { Tabular::ALIGN_RIGHT, "align-right" },
131 { Tabular::ALIGN_CENTER, "align-center" },
132 { Tabular::ALIGN_BLOCK, "align-block" },
133 { Tabular::VALIGN_TOP, "valign-top" },
134 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
135 { Tabular::VALIGN_MIDDLE, "valign-middle" },
136 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
137 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
138 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
139 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
140 { Tabular::M_ALIGN_LEFT, "m-align-left" },
141 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
142 { Tabular::M_ALIGN_CENTER, "m-align-center" },
143 { Tabular::M_VALIGN_TOP, "m-valign-top" },
144 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
145 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
146 { Tabular::MULTICOLUMN, "multicolumn" },
147 { Tabular::SET_ALL_LINES, "set-all-lines" },
148 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
149 { Tabular::SET_LONGTABULAR, "set-longtabular" },
150 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
151 { Tabular::SET_PWIDTH, "set-pwidth" },
152 { Tabular::SET_MPWIDTH, "set-mpwidth" },
153 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
154 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
155 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
156 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
157 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
158 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
159 { Tabular::SET_USEBOX, "set-usebox" },
160 { Tabular::SET_LTHEAD, "set-lthead" },
161 { Tabular::UNSET_LTHEAD, "unset-lthead" },
162 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
163 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
164 { Tabular::SET_LTFOOT, "set-ltfoot" },
165 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
166 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
167 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
168 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
169 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
170 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
171 { Tabular::SET_BOOKTABS, "set-booktabs" },
172 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
173 { Tabular::SET_TOP_SPACE, "set-top-space" },
174 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
175 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
176 { Tabular::LAST_ACTION, "" }
180 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
182 FeatureEqual(Tabular::Feature feature)
183 : feature_(feature) {}
184 bool operator()(TabularFeature const & tf) const {
185 return tf.action == feature_;
188 Tabular::Feature feature_;
193 string const write_attribute(string const & name, T const & t)
195 string const s = tostr(t);
196 return s.empty() ? s : " " + name + "=\"" + s + "\"";
200 string const write_attribute(string const & name, string const & t)
202 return t.empty() ? t : " " + name + "=\"" + t + "\"";
207 string const write_attribute(string const & name, docstring const & t)
209 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
214 string const write_attribute(string const & name, bool const & b)
216 // we write only true attribute values so we remove a bit of the
217 // file format bloat for tabulars.
218 return b ? write_attribute(name, convert<string>(b)) : string();
223 string const write_attribute(string const & name, int const & i)
225 // we write only true attribute values so we remove a bit of the
226 // file format bloat for tabulars.
227 return i ? write_attribute(name, convert<string>(i)) : string();
232 string const write_attribute(string const & name, Tabular::idx_type const & i)
234 // we write only true attribute values so we remove a bit of the
235 // file format bloat for tabulars.
236 return i ? write_attribute(name, convert<string>(i)) : string();
241 string const write_attribute(string const & name, Length const & value)
243 // we write only the value if we really have one same reson as above.
244 return value.zero() ? string() : write_attribute(name, value.asString());
248 string const tostr(LyXAlignment const & num)
253 case LYX_ALIGN_BLOCK:
257 case LYX_ALIGN_CENTER:
259 case LYX_ALIGN_RIGHT:
261 case LYX_ALIGN_LAYOUT:
263 case LYX_ALIGN_SPECIAL:
270 string const tostr(Tabular::VAlignment const & num)
273 case Tabular::LYX_VALIGN_TOP:
275 case Tabular::LYX_VALIGN_MIDDLE:
277 case Tabular::LYX_VALIGN_BOTTOM:
284 string const tostr(Tabular::BoxType const & num)
287 case Tabular::BOX_NONE:
289 case Tabular::BOX_PARBOX:
291 case Tabular::BOX_MINIPAGE:
298 // I would have liked a fromstr template a lot better. (Lgb)
299 bool string2type(string const str, LyXAlignment & num)
302 num = LYX_ALIGN_NONE;
303 else if (str == "block")
304 num = LYX_ALIGN_BLOCK;
305 else if (str == "left")
306 num = LYX_ALIGN_LEFT;
307 else if (str == "center")
308 num = LYX_ALIGN_CENTER;
309 else if (str == "right")
310 num = LYX_ALIGN_RIGHT;
317 bool string2type(string const str, Tabular::VAlignment & num)
320 num = Tabular::LYX_VALIGN_TOP;
321 else if (str == "middle" )
322 num = Tabular::LYX_VALIGN_MIDDLE;
323 else if (str == "bottom")
324 num = Tabular::LYX_VALIGN_BOTTOM;
331 bool string2type(string const str, Tabular::BoxType & num)
334 num = Tabular::BOX_NONE;
335 else if (str == "parbox")
336 num = Tabular::BOX_PARBOX;
337 else if (str == "minipage")
338 num = Tabular::BOX_MINIPAGE;
345 bool string2type(string const str, bool & num)
349 else if (str == "false")
357 bool getTokenValue(string const & str, char const * token, string & ret)
360 size_t token_length = strlen(token);
361 string::size_type pos = str.find(token);
363 if (pos == string::npos || pos + token_length + 1 >= str.length()
364 || str[pos + token_length] != '=')
366 pos += token_length + 1;
368 if (ch != '"' && ch != '\'') { // only read till next space
372 while (pos < str.length() - 1 && str[++pos] != ch)
379 bool getTokenValue(string const & str, char const * token, docstring & ret)
382 bool const success = getTokenValue(str, token, tmp);
383 ret = from_utf8(tmp);
388 bool getTokenValue(string const & str, char const * token, int & num)
392 if (!getTokenValue(str, token, tmp))
394 num = convert<int>(tmp);
399 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
402 return getTokenValue(str, token, tmp) && string2type(tmp, num);
406 bool getTokenValue(string const & str, char const * token,
407 Tabular::VAlignment & num)
410 return getTokenValue(str, token, tmp) && string2type(tmp, num);
414 bool getTokenValue(string const & str, char const * token,
415 Tabular::BoxType & num)
418 return getTokenValue(str, token, tmp) && string2type(tmp, num);
422 bool getTokenValue(string const & str, char const * token, bool & flag)
424 // set the flag always to false as this should be the default for bools
425 // not in the file-format.
428 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
432 bool getTokenValue(string const & str, char const * token, Length & len)
434 // set the length to be zero() as default as this it should be if not
435 // in the file format.
438 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
442 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
447 if (!getTokenValue(str, token, tmp))
449 if (tmp == "default") {
453 return isValidLength(tmp, &len);
457 void l_getline(istream & is, string & str)
460 while (str.empty()) {
462 if (!str.empty() && str[str.length() - 1] == '\r')
463 str.erase(str.length() - 1);
470 string const featureAsString(Tabular::Feature feature)
472 TabularFeature * end = tabularFeature +
473 sizeof(tabularFeature) / sizeof(TabularFeature);
474 TabularFeature * it = std::find_if(tabularFeature, end,
475 FeatureEqual(feature));
476 return (it == end) ? string() : it->feature;
481 /////////////////////////////////////////////////////////////////////
485 /////////////////////////////////////////////////////////////////////
488 Tabular::cellstruct::cellstruct(BufferParams const & bp)
491 multicolumn(Tabular::CELL_NORMAL),
492 alignment(LYX_ALIGN_CENTER),
493 valignment(LYX_VALIGN_TOP),
500 inset(new InsetText(bp))
504 Tabular::cellstruct::cellstruct(cellstruct const & cs)
507 multicolumn(cs.multicolumn),
508 alignment(cs.alignment),
509 valignment(cs.valignment),
510 top_line(cs.top_line),
511 bottom_line(cs.bottom_line),
512 left_line(cs.left_line),
513 right_line(cs.right_line),
516 align_special(cs.align_special),
518 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
522 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
529 void Tabular::cellstruct::swap(cellstruct & rhs)
531 std::swap(cellno, rhs.cellno);
532 std::swap(width, rhs.width);
533 std::swap(multicolumn, rhs.multicolumn);
534 std::swap(alignment, rhs.alignment);
535 std::swap(valignment, rhs.valignment);
536 std::swap(top_line, rhs.top_line);
537 std::swap(bottom_line, rhs.bottom_line);
538 std::swap(left_line, rhs.left_line);
539 std::swap(right_line, rhs.right_line);
540 std::swap(usebox, rhs.usebox);
541 std::swap(rotate, rhs.rotate);
542 std::swap(align_special, rhs.align_special);
543 p_width.swap(rhs.p_width);
544 inset.swap(rhs.inset);
548 Tabular::rowstruct::rowstruct()
553 top_space_default(false),
554 bottom_space_default(false),
555 interline_space_default(false),
564 Tabular::columnstruct::columnstruct()
565 : alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
574 Tabular::ltType::ltType()
587 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
588 col_type columns_arg)
590 init(bp, rows_arg, columns_arg);
594 // activates all lines and sets all widths to 0
595 void Tabular::init(BufferParams const & bp, row_type rows_arg,
596 col_type columns_arg)
598 row_info = row_vector(rows_arg);
599 column_info = column_vector(columns_arg);
600 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
601 row_info.reserve(10);
602 column_info.reserve(10);
603 cell_info.reserve(100);
605 for (row_type i = 0; i < rows_arg; ++i)
606 cell_info[i].back().right_line = true;
607 row_info.back().bottom_line = true;
608 row_info.front().bottom_line = true;
609 column_info.back().right_line = true;
610 is_long_tabular = false;
612 use_booktabs = false;
616 void Tabular::fixCellNums()
619 for (row_type i = 0; i < rowCount(); ++i) {
620 for (col_type j = 0; j < columnCount(); ++j) {
621 // When debugging it can be nice to set
623 cell_info[i][j].inset->setDrawFrame(false);
624 cell_info[i][j].cellno = cellno++;
626 cell_info[i].back().right_line = true;
633 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
635 row_type const row = cellRow(cell);
637 row_vector::iterator rit = row_info.begin() + row;
638 row_info.insert(rit, rowstruct());
639 // now set the values of the row before
640 row_info[row] = row_info[row + 1];
642 row_type const nrows = rowCount();
643 col_type const ncols = columnCount();
645 cell_vvector old(nrows - 1);
646 for (row_type i = 0; i < nrows - 1; ++i)
647 swap(cell_info[i], old[i]);
649 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
651 for (row_type i = 0; i <= row; ++i)
652 swap(cell_info[i], old[i]);
653 for (row_type i = row + 2; i < nrows; ++i)
654 swap(cell_info[i], old[i - 1]);
657 for (col_type j = 0; j < ncols; ++j)
658 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
664 void Tabular::deleteRow(row_type const row)
666 // Not allowed to delete last row
670 row_info.erase(row_info.begin() + row);
671 cell_info.erase(cell_info.begin() + row);
676 void Tabular::copyRow(BufferParams const & bp, row_type const row)
678 row_info.insert(row_info.begin() + row, row_info[row]);
679 cell_info.insert(cell_info.begin() + row, cell_info[row]);
682 for (col_type j = 0; j < columnCount(); ++j)
683 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
689 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
691 col_type const column = cellColumn(cell);
692 col_type const ncols = columnCount();
693 column_vector::iterator cit = column_info.begin() + column + 1;
694 column_info.insert(cit, columnstruct());
695 // set the column values of the column before
696 column_info[column + 1] = column_info[column];
698 for (row_type i = 0; i < rowCount(); ++i) {
699 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
701 // care about multicolumns
702 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
703 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
705 if (column + 2 >= ncols
706 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
707 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
710 for (row_type i = 0; i < rowCount(); ++i) {
711 cell_info[i][column + 1].inset->clear();
713 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
719 void Tabular::deleteColumn(col_type const column)
721 // Not allowed to delete last column
722 if (columnCount() == 1)
725 column_info.erase(column_info.begin() + column);
726 for (row_type i = 0; i < rowCount(); ++i) {
727 // Care about multicolumn cells
728 if (column + 1 < columnCount() &&
729 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
730 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
731 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
733 cell_info[i].erase(cell_info[i].begin() + column);
739 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
741 column_info.insert(column_info.begin() + column, column_info[column]);
743 for (row_type i = 0; i < rowCount(); ++i)
744 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
747 for (row_type i = 0; i < rowCount(); ++i)
748 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
753 void Tabular::updateIndexes()
756 // Count only non-multicol cells plus begin multicol
757 // cells, ignore non-begin multicol cells:
758 for (row_type row = 0; row < rowCount(); ++row) {
759 for (col_type column = 0; column < columnCount(); ++column) {
760 if (cell_info[row][column].multicolumn
761 != Tabular::CELL_PART_OF_MULTICOLUMN)
763 BOOST_ASSERT(numberofcells != 0);
764 cell_info[row][column].cellno =
769 rowofcell.resize(numberofcells);
770 columnofcell.resize(numberofcells);
775 c < numberofcells && row < rowCount() && column < columnCount();) {
777 columnofcell[c] = column;
781 } while (column < columnCount() &&
782 cell_info[row][column].multicolumn
783 == Tabular::CELL_PART_OF_MULTICOLUMN);
785 if (column == columnCount()) {
791 for (row_type row = 0; row < rowCount(); ++row) {
792 for (col_type column = 0; column < columnCount(); ++column) {
793 if (isPartOfMultiColumn(row,column))
795 cell_info[row][column].inset->setAutoBreakRows(
796 !getPWidth(cellIndex(row, column)).zero());
802 Tabular::idx_type Tabular::cellCount() const
804 return numberofcells;
808 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
810 row_type const row = cellRow(cell);
812 for (col_type i = 0; i < columnCount(); ++i)
813 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
819 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
821 if (!wholerow && isMultiColumn(cell) &&
822 !(use_booktabs && cellRow(cell) == 0))
823 return cellinfo_of_cell(cell).top_line;
824 return row_info[cellRow(cell)].top_line;
828 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
830 if (!wholerow && isMultiColumn(cell) &&
831 !(use_booktabs && isLastRow(cell)))
832 return cellinfo_of_cell(cell).bottom_line;
833 return row_info[cellRow(cell)].bottom_line;
837 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
841 if (!wholecolumn && isMultiColumn(cell) &&
842 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
843 return cellinfo_of_cell(cell).left_line;
844 return column_info[cellColumn(cell)].left_line;
848 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
852 if (!wholecolumn && isMultiColumn(cell) &&
853 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
854 return cellinfo_of_cell(cell).right_line;
855 return column_info[cellRightColumn(cell)].right_line;
859 bool Tabular::topAlreadyDrawn(idx_type cell) const
861 row_type row = cellRow(cell);
862 if (row > 0 && !getAdditionalHeight(row)) {
863 col_type column = cellColumn(cell);
866 && cell_info[row][column].multicolumn
867 == Tabular::CELL_PART_OF_MULTICOLUMN)
869 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
870 return row_info[row].bottom_line;
872 return cell_info[row][column].bottom_line;
878 bool Tabular::leftAlreadyDrawn(idx_type cell) const
880 col_type column = cellColumn(cell);
882 row_type row = cellRow(cell);
884 (cell_info[row][column].multicolumn ==
885 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
886 if (getAdditionalWidth(cell_info[row][column].cellno))
888 return rightLine(cell_info[row][column].cellno);
894 bool Tabular::isLastRow(idx_type cell) const
896 return cellRow(cell) == rowCount() - 1;
900 int Tabular::getAdditionalHeight(row_type row) const
902 if (!row || row >= rowCount())
908 for (col_type column = 0; column < columnCount() && bottom; ++column) {
909 switch (cell_info[row - 1][column].multicolumn) {
910 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
911 bottom = cell_info[row - 1][column].bottom_line;
913 case Tabular::CELL_NORMAL:
914 bottom = row_info[row - 1].bottom_line;
917 for (col_type column = 0; column < columnCount() && top; ++column) {
918 switch (cell_info[row][column].multicolumn) {
919 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
920 top = cell_info[row][column].top_line;
922 case Tabular::CELL_NORMAL:
923 top = row_info[row].top_line;
926 int const interline_space = row_info[row - 1].interline_space_default ?
928 row_info[row - 1].interline_space.inPixels(width());
930 return interline_space + WIDTH_OF_LINE;
931 return interline_space;
935 int Tabular::getAdditionalWidth(idx_type cell) const
937 // internally already set in setCellWidth
938 // used to get it back in text.cpp
939 col_type const col = cellRightColumn(cell);
940 row_type const row = cellRow(cell);
941 if (col < columnCount() - 1 && rightLine(cell) &&
942 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
944 return WIDTH_OF_LINE;
950 // returns the maximum over all rows
951 int Tabular::columnWidth(idx_type cell) const
953 col_type const column1 = cellColumn(cell);
954 col_type const column2 = cellRightColumn(cell);
956 for (col_type i = column1; i <= column2; ++i)
957 result += column_info[i].width;
962 int Tabular::width() const
965 for (col_type i = 0; i < columnCount(); ++i)
966 width += column_info[i].width;
971 // returns true if a complete update is necessary, otherwise false
972 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
974 if (!isMultiColumn(cell))
977 row_type const row = cellRow(cell);
978 col_type const column1 = cellColumn(cell);
979 col_type const column2 = cellRightColumn(cell);
980 int const old_val = cell_info[row][column2].width;
982 // first set columns to 0 so we can calculate the right width
983 for (col_type i = column1; i <= column2; ++i) {
984 cell_info[row][i].width = 0;
986 // set the width to MAX_WIDTH until width > 0
987 int width = new_width + 2 * WIDTH_OF_LINE;
988 col_type i = column1;
989 for (; i < column2 && width > column_info[i].width; ++i) {
990 cell_info[row][i].width = column_info[i].width;
991 width -= column_info[i].width;
994 cell_info[row][i].width = width;
996 if (old_val != cell_info[row][column2].width) {
997 // in this case we have to recalculate all multicolumn cells which
998 // have this column as one of theirs but not as last one
999 calculate_width_of_column_NMC(i);
1000 recalculateMulticolumnsOfColumn(i);
1001 calculate_width_of_column(i);
1007 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1009 // the last column does not have to be recalculated because all
1010 // multicolumns will have here there last multicolumn cell which
1011 // always will have the whole rest of the width of the cell.
1012 if (columnCount() < 2 || column > (columnCount() - 2))
1014 for (row_type row = 0; row < rowCount(); ++row) {
1015 int mc = cell_info[row][column].multicolumn;
1016 int nmc = cell_info[row][column+1].multicolumn;
1017 // we only have to update multicolumns which do not have this
1018 // column as their last column!
1019 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1020 (mc == CELL_PART_OF_MULTICOLUMN &&
1021 nmc == CELL_PART_OF_MULTICOLUMN))
1023 idx_type const cellno = cell_info[row][column].cellno;
1024 setWidthOfMulticolCell(cellno,
1025 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1031 void Tabular::setCellWidth(idx_type cell, int new_width)
1033 row_type const row = cellRow(cell);
1034 col_type const column1 = cellColumn(cell);
1039 if (rightLine(cell_info[row][column1].cellno, true) &&
1040 column1 < columnCount() - 1 &&
1041 leftLine(cell_info[row][column1+1].cellno, true))
1043 add_width = WIDTH_OF_LINE;
1046 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1049 if (isMultiColumnReal(cell)) {
1050 tmp = setWidthOfMulticolCell(cell, new_width);
1052 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1053 cell_info[row][column1].width = width;
1054 tmp = calculate_width_of_column_NMC(column1);
1056 recalculateMulticolumnsOfColumn(column1);
1059 for (col_type i = 0; i < columnCount(); ++i)
1060 calculate_width_of_column(i);
1065 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1068 if (!isMultiColumn(cell) || onlycolumn)
1069 column_info[cellColumn(cell)].alignment = align;
1071 cellinfo_of_cell(cell).alignment = align;
1075 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1078 if (!isMultiColumn(cell) || onlycolumn)
1079 column_info[cellColumn(cell)].valignment = align;
1081 cellinfo_of_cell(cell).valignment = align;
1088 * Allow line and paragraph breaks for fixed width cells or disallow them,
1089 * merge cell paragraphs and reset layout to standard for variable width
1092 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1094 inset->setAutoBreakRows(fixedWidth);
1098 // merge all paragraphs to one
1099 BufferParams const & bp = cur.bv().buffer().params();
1100 while (inset->paragraphs().size() > 1)
1101 mergeParagraph(bp, inset->paragraphs(), 0);
1105 // undo information has already been recorded
1106 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1107 bp.getTextClass().defaultLayoutName());
1114 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1115 Length const & width)
1117 col_type const j = cellColumn(cell);
1119 column_info[j].p_width = width;
1120 for (row_type i = 0; i < rowCount(); ++i) {
1121 idx_type const cell = cellIndex(i, j);
1122 // because of multicolumns
1123 toggleFixedWidth(cur, getCellInset(cell).get(),
1124 !getPWidth(cell).zero());
1126 // cur paragraph can become invalid after paragraphs were merged
1127 if (cur.pit() > cur.lastpit())
1128 cur.pit() = cur.lastpit();
1129 // cur position can become invalid after newlines were removed
1130 if (cur.pos() > cur.lastpos())
1131 cur.pos() = cur.lastpos();
1135 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1136 Length const & width)
1138 if (!isMultiColumn(cell))
1141 cellinfo_of_cell(cell).p_width = width;
1142 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1143 // cur paragraph can become invalid after paragraphs were merged
1144 if (cur.pit() > cur.lastpit())
1145 cur.pit() = cur.lastpit();
1146 // cur position can become invalid after newlines were removed
1147 if (cur.pos() > cur.lastpos())
1148 cur.pos() = cur.lastpos();
1153 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1154 Tabular::Feature what)
1156 if (what == SET_SPECIAL_MULTI)
1157 cellinfo_of_cell(cell).align_special = special;
1159 column_info[cellColumn(cell)].align_special = special;
1163 void Tabular::setAllLines(idx_type cell, bool line)
1165 setTopLine(cell, line);
1166 setBottomLine(cell, line);
1167 setRightLine(cell, line);
1168 setLeftLine(cell, line);
1172 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1174 row_type const row = cellRow(cell);
1175 if (wholerow || !isMultiColumn(cell))
1176 row_info[row].top_line = line;
1178 cellinfo_of_cell(cell).top_line = line;
1182 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1184 if (wholerow || !isMultiColumn(cell))
1185 row_info[cellRow(cell)].bottom_line = line;
1187 cellinfo_of_cell(cell).bottom_line = line;
1191 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1193 if (wholecolumn || !isMultiColumn(cell))
1194 column_info[cellColumn(cell)].left_line = line;
1196 cellinfo_of_cell(cell).left_line = line;
1200 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1202 if (wholecolumn || !isMultiColumn(cell))
1203 column_info[cellRightColumn(cell)].right_line = line;
1205 cellinfo_of_cell(cell).right_line = line;
1209 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1211 if (!onlycolumn && isMultiColumn(cell))
1212 return cellinfo_of_cell(cell).alignment;
1213 return column_info[cellColumn(cell)].alignment;
1218 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1220 if (!onlycolumn && isMultiColumn(cell))
1221 return cellinfo_of_cell(cell).valignment;
1222 return column_info[cellColumn(cell)].valignment;
1226 Length const Tabular::getPWidth(idx_type cell) const
1228 if (isMultiColumn(cell))
1229 return cellinfo_of_cell(cell).p_width;
1230 return column_info[cellColumn(cell)].p_width;
1234 Length const Tabular::getColumnPWidth(idx_type cell) const
1236 return column_info[cellColumn(cell)].p_width;
1240 Length const Tabular::getMColumnPWidth(idx_type cell) const
1242 if (isMultiColumn(cell))
1243 return cellinfo_of_cell(cell).p_width;
1248 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1250 if (what == SET_SPECIAL_MULTI)
1251 return cellinfo_of_cell(cell).align_special;
1252 return column_info[cellColumn(cell)].align_special;
1256 int Tabular::cellWidth(idx_type cell) const
1258 row_type const row = cellRow(cell);
1259 col_type const column1 = cellColumn(cell);
1260 col_type const column2 = cellRightColumn(cell);
1262 for (col_type i = column1; i <= column2; ++i)
1263 result += cell_info[row][i].width;
1268 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1272 switch (getAlignment(cell)) {
1273 case LYX_ALIGN_CENTER:
1274 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1276 case LYX_ALIGN_RIGHT:
1277 x += columnWidth(cell) - cellWidth(cell);
1278 // + getAdditionalWidth(cell);
1281 // LYX_ALIGN_LEFT: nothing :-)
1285 // the LaTeX Way :-(
1291 bool Tabular::isFirstCellInRow(idx_type cell) const
1293 return cellColumn(cell) == 0;
1297 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1299 if (row > rowCount() - 1)
1300 row = rowCount() - 1;
1301 return cell_info[row][0].cellno;
1305 bool Tabular::isLastCellInRow(idx_type cell) const
1307 return cellRightColumn(cell) == columnCount() - 1;
1311 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1313 if (row > rowCount() - 1)
1314 row = rowCount() - 1;
1315 return cell_info[row][columnCount() - 1].cellno;
1319 void Tabular::calculate_width_of_column(col_type column)
1322 for (row_type i = 0; i < rowCount(); ++i)
1323 maximum = max(cell_info[i][column].width, maximum);
1324 column_info[column].width = maximum;
1329 // Calculate the columns regarding ONLY the normal cells and if this
1330 // column is inside a multicolumn cell then use it only if its the last
1331 // column of this multicolumn cell as this gives an added width to the
1332 // column, all the rest should be adapted!
1334 bool Tabular::calculate_width_of_column_NMC(col_type column)
1336 int const old_column_width = column_info[column].width;
1338 for (row_type i = 0; i < rowCount(); ++i) {
1339 idx_type cell = cellIndex(i, column);
1340 bool ismulti = isMultiColumnReal(cell);
1341 if ((!ismulti || column == cellRightColumn(cell)) &&
1342 cell_info[i][column].width > max)
1344 max = cell_info[i][column].width;
1347 column_info[column].width = max;
1348 return column_info[column].width != old_column_width;
1352 Tabular::row_type Tabular::cellRow(idx_type cell) const
1354 if (cell >= cellCount())
1355 return rowCount() - 1;
1358 return rowofcell[cell];
1362 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1364 if (cell >= cellCount())
1365 return columnCount() - 1;
1368 return columnofcell[cell];
1372 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1374 row_type const row = cellRow(cell);
1375 col_type column = cellColumn(cell);
1376 while (column < columnCount() - 1 &&
1377 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1383 void Tabular::write(Buffer const & buf, ostream & os) const
1387 << write_attribute("version", 3)
1388 << write_attribute("rows", rowCount())
1389 << write_attribute("columns", columnCount())
1391 // global longtable options
1393 << write_attribute("rotate", rotate)
1394 << write_attribute("booktabs", use_booktabs)
1395 << write_attribute("islongtable", is_long_tabular)
1396 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1397 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1398 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1399 << write_attribute("headTopDL", endhead.topDL)
1400 << write_attribute("headBottomDL", endhead.bottomDL)
1401 << write_attribute("footTopDL", endfoot.topDL)
1402 << write_attribute("footBottomDL", endfoot.bottomDL)
1403 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1404 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1405 << write_attribute("lastFootEmpty", endlastfoot.empty)
1407 for (col_type j = 0; j < columnCount(); ++j) {
1409 << write_attribute("alignment", column_info[j].alignment)
1410 << write_attribute("valignment", column_info[j].valignment)
1411 << write_attribute("leftline", column_info[j].left_line)
1412 << write_attribute("rightline", column_info[j].right_line)
1413 << write_attribute("width", column_info[j].p_width.asString())
1414 << write_attribute("special", column_info[j].align_special)
1417 for (row_type i = 0; i < rowCount(); ++i) {
1419 << write_attribute("topline", row_info[i].top_line)
1420 << write_attribute("bottomline", row_info[i].bottom_line);
1421 static const string def("default");
1422 if (row_info[i].top_space_default)
1423 os << write_attribute("topspace", def);
1425 os << write_attribute("topspace", row_info[i].top_space);
1426 if (row_info[i].bottom_space_default)
1427 os << write_attribute("bottomspace", def);
1429 os << write_attribute("bottomspace", row_info[i].bottom_space);
1430 if (row_info[i].interline_space_default)
1431 os << write_attribute("interlinespace", def);
1433 os << write_attribute("interlinespace", row_info[i].interline_space);
1434 os << write_attribute("endhead", row_info[i].endhead)
1435 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1436 << write_attribute("endfoot", row_info[i].endfoot)
1437 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1438 << write_attribute("newpage", row_info[i].newpage)
1440 for (col_type j = 0; j < columnCount(); ++j) {
1442 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1443 << write_attribute("alignment", cell_info[i][j].alignment)
1444 << write_attribute("valignment", cell_info[i][j].valignment)
1445 << write_attribute("topline", cell_info[i][j].top_line)
1446 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1447 << write_attribute("leftline", cell_info[i][j].left_line)
1448 << write_attribute("rightline", cell_info[i][j].right_line)
1449 << write_attribute("rotate", cell_info[i][j].rotate)
1450 << write_attribute("usebox", cell_info[i][j].usebox)
1451 << write_attribute("width", cell_info[i][j].p_width)
1452 << write_attribute("special", cell_info[i][j].align_special)
1454 os << "\\begin_inset ";
1455 cell_info[i][j].inset->write(buf, os);
1456 os << "\n\\end_inset\n"
1461 os << "</lyxtabular>\n";
1465 void Tabular::read(Buffer const & buf, Lexer & lex)
1468 istream & is = lex.getStream();
1470 l_getline(is, line);
1471 if (!prefixIs(line, "<lyxtabular ")
1472 && !prefixIs(line, "<Tabular ")) {
1473 BOOST_ASSERT(false);
1478 if (!getTokenValue(line, "version", version))
1480 BOOST_ASSERT(version >= 2);
1483 if (!getTokenValue(line, "rows", rows_arg))
1486 if (!getTokenValue(line, "columns", columns_arg))
1488 init(buf.params(), rows_arg, columns_arg);
1489 l_getline(is, line);
1490 if (!prefixIs(line, "<features")) {
1491 lyxerr << "Wrong tabular format (expected <features ...> got"
1492 << line << ')' << endl;
1495 getTokenValue(line, "rotate", rotate);
1496 getTokenValue(line, "booktabs", use_booktabs);
1497 getTokenValue(line, "islongtable", is_long_tabular);
1498 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1499 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1500 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1501 getTokenValue(line, "headTopDL", endhead.topDL);
1502 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1503 getTokenValue(line, "footTopDL", endfoot.topDL);
1504 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1505 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1506 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1507 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1509 for (col_type j = 0; j < columnCount(); ++j) {
1511 if (!prefixIs(line,"<column")) {
1512 lyxerr << "Wrong tabular format (expected <column ...> got"
1513 << line << ')' << endl;
1516 getTokenValue(line, "alignment", column_info[j].alignment);
1517 getTokenValue(line, "valignment", column_info[j].valignment);
1518 getTokenValue(line, "leftline", column_info[j].left_line);
1519 getTokenValue(line, "rightline", column_info[j].right_line);
1520 getTokenValue(line, "width", column_info[j].p_width);
1521 getTokenValue(line, "special", column_info[j].align_special);
1524 for (row_type i = 0; i < rowCount(); ++i) {
1525 l_getline(is, line);
1526 if (!prefixIs(line, "<row")) {
1527 lyxerr << "Wrong tabular format (expected <row ...> got"
1528 << line << ')' << endl;
1531 getTokenValue(line, "topline", row_info[i].top_line);
1532 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1533 getTokenValue(line, "topspace", row_info[i].top_space,
1534 row_info[i].top_space_default);
1535 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1536 row_info[i].bottom_space_default);
1537 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1538 row_info[i].interline_space_default);
1539 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1540 getTokenValue(line, "endhead", row_info[i].endhead);
1541 getTokenValue(line, "endfoot", row_info[i].endfoot);
1542 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1543 getTokenValue(line, "newpage", row_info[i].newpage);
1544 for (col_type j = 0; j < columnCount(); ++j) {
1545 l_getline(is, line);
1546 if (!prefixIs(line, "<cell")) {
1547 lyxerr << "Wrong tabular format (expected <cell ...> got"
1548 << line << ')' << endl;
1551 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1552 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1553 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1554 getTokenValue(line, "topline", cell_info[i][j].top_line);
1555 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1556 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1557 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1558 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1559 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1560 getTokenValue(line, "width", cell_info[i][j].p_width);
1561 getTokenValue(line, "special", cell_info[i][j].align_special);
1562 l_getline(is, line);
1563 if (prefixIs(line, "\\begin_inset")) {
1564 cell_info[i][j].inset->read(buf, lex);
1565 l_getline(is, line);
1567 if (!prefixIs(line, "</cell>")) {
1568 lyxerr << "Wrong tabular format (expected </cell> got"
1569 << line << ')' << endl;
1573 l_getline(is, line);
1574 if (!prefixIs(line, "</row>")) {
1575 lyxerr << "Wrong tabular format (expected </row> got"
1576 << line << ')' << endl;
1580 while (!prefixIs(line, "</lyxtabular>")) {
1581 l_getline(is, line);
1587 bool Tabular::isMultiColumn(idx_type cell) const
1589 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1593 bool Tabular::isMultiColumnReal(idx_type cell) const
1595 return cellColumn(cell) != cellRightColumn(cell) &&
1596 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1600 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1602 return cell_info[cellRow(cell)][cellColumn(cell)];
1606 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1609 cellstruct & cs = cellinfo_of_cell(cell);
1610 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1611 cs.alignment = column_info[cellColumn(cell)].alignment;
1612 cs.top_line = row_info[cellRow(cell)].top_line;
1613 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1614 cs.left_line = column_info[cellColumn(cell)].left_line;
1615 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1616 for (idx_type i = 1; i < number; ++i) {
1617 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1618 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1619 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1626 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1628 row_type const row = cellRow(cell);
1629 col_type column = cellColumn(cell);
1630 idx_type result = 1;
1632 while (column < columnCount() &&
1633 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1642 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1644 row_type const row = cellRow(cell);
1645 col_type column = cellColumn(cell);
1647 idx_type result = 0;
1649 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1650 cell_info[row][column].multicolumn = CELL_NORMAL;
1652 while (column < columnCount() &&
1653 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1655 cell_info[row][column].multicolumn = CELL_NORMAL;
1665 void Tabular::setBookTabs(bool what)
1667 use_booktabs = what;
1671 bool Tabular::useBookTabs() const
1673 return use_booktabs;
1677 void Tabular::setLongTabular(bool what)
1679 is_long_tabular = what;
1683 bool Tabular::isLongTabular() const
1685 return is_long_tabular;
1689 void Tabular::setRotateTabular(bool flag)
1695 bool Tabular::getRotateTabular() const
1701 void Tabular::setRotateCell(idx_type cell, bool flag)
1703 cellinfo_of_cell(cell).rotate = flag;
1707 bool Tabular::getRotateCell(idx_type cell) const
1709 return cellinfo_of_cell(cell).rotate;
1713 bool Tabular::needRotating() const
1717 for (row_type i = 0; i < rowCount(); ++i)
1718 for (col_type j = 0; j < columnCount(); ++j)
1719 if (cell_info[i][j].rotate)
1725 bool Tabular::isLastCell(idx_type cell) const
1727 if (cell + 1 < cellCount())
1733 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1735 if (cellRow(cell) > 0)
1736 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1741 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1743 if (cellRow(cell) + 1 < rowCount())
1744 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1749 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1751 if (cellRow(cell) == 0)
1753 if (!isMultiColumn(cell))
1754 return getCellAbove(cell);
1755 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1759 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1761 if (cellRow(cell) + 1 >= rowCount())
1763 if (!isMultiColumn(cell))
1764 return getCellBelow(cell);
1765 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1769 Tabular::idx_type Tabular::cellIndex(row_type row,
1770 col_type column) const
1772 BOOST_ASSERT(column != npos && column < columnCount() &&
1773 row != npos && row < rowCount());
1774 return cell_info[row][column].cellno;
1778 void Tabular::setUsebox(idx_type cell, BoxType type)
1780 cellinfo_of_cell(cell).usebox = type;
1784 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1786 if (column_info[cellColumn(cell)].p_width.zero() &&
1787 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1789 if (cellinfo_of_cell(cell).usebox > 1)
1790 return cellinfo_of_cell(cell).usebox;
1791 return useParbox(cell);
1796 // This are functions used for the longtable support
1798 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1804 row_info[row].endfirsthead = flag;
1808 row_info[row].endhead = flag;
1813 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1816 hd.set = haveLTHead();
1817 return row_info[row].endhead;
1821 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1824 hd.set = haveLTFirstHead();
1825 return row_info[row].endfirsthead;
1829 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1835 row_info[row].endlastfoot = flag;
1839 row_info[row].endfoot = flag;
1844 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1847 fd.set = haveLTFoot();
1848 return row_info[row].endfoot;
1852 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1855 fd.set = haveLTLastFoot();
1856 return row_info[row].endlastfoot;
1860 void Tabular::setLTNewPage(row_type row, bool what)
1862 row_info[row].newpage = what;
1866 bool Tabular::getLTNewPage(row_type row) const
1868 return row_info[row].newpage;
1872 bool Tabular::haveLTHead() const
1874 for (row_type i = 0; i < rowCount(); ++i)
1875 if (row_info[i].endhead)
1881 bool Tabular::haveLTFirstHead() const
1883 if (endfirsthead.empty)
1885 for (row_type i = 0; i < rowCount(); ++i)
1886 if (row_info[i].endfirsthead)
1892 bool Tabular::haveLTFoot() const
1894 for (row_type i = 0; i < rowCount(); ++i)
1895 if (row_info[i].endfoot)
1901 bool Tabular::haveLTLastFoot() const
1903 if (endlastfoot.empty)
1905 for (row_type i = 0; i < rowCount(); ++i)
1906 if (row_info[i].endlastfoot)
1912 // end longtable support functions
1914 void Tabular::setRowAscent(row_type row, int height)
1916 if (row >= rowCount() || row_info[row].ascent == height)
1918 row_info[row].ascent = height;
1922 void Tabular::setRowDescent(row_type row, int height)
1924 if (row >= rowCount() || row_info[row].descent == height)
1926 row_info[row].descent = height;
1930 int Tabular::rowAscent(row_type row) const
1932 if (row >= rowCount())
1934 return row_info[row].ascent;
1938 int Tabular::rowDescent(row_type row) const
1940 BOOST_ASSERT(row < rowCount());
1941 return row_info[row].descent;
1945 int Tabular::height() const
1948 for (row_type row = 0; row < rowCount(); ++row)
1949 height += rowAscent(row) + rowDescent(row) +
1950 getAdditionalHeight(row);
1955 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1957 BOOST_ASSERT(row < rowCount());
1958 BOOST_ASSERT(column < columnCount());
1959 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1963 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1965 // FIXME: assert or return 0 as in TeXBottomHLine()?
1966 BOOST_ASSERT(row != npos);
1967 BOOST_ASSERT(row < rowCount());
1969 idx_type const fcell = getFirstCellInRow(row);
1970 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1973 for (idx_type i = fcell; i < n; ++i) {
1977 if (use_booktabs && row == 0) {
1979 } else if (tmp == n - fcell) {
1980 os << (use_booktabs ? "\\midrule " : "\\hline ");
1982 for (idx_type i = fcell; i < n; ++i) {
1984 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1985 << cellColumn(i) + 1
1987 << cellRightColumn(i) + 1
1999 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2001 // FIXME: return 0 or assert as in TeXTopHLine()?
2002 if (row == npos || row >= rowCount())
2005 idx_type const fcell = getFirstCellInRow(row);
2006 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2009 for (idx_type i = fcell; i < n; ++i) {
2013 if (use_booktabs && row == rowCount() - 1) {
2014 os << "\\bottomrule";
2015 } else if (tmp == n - fcell) {
2016 os << (use_booktabs ? "\\midrule" : "\\hline");
2018 for (idx_type i = fcell; i < n; ++i) {
2019 if (bottomLine(i)) {
2020 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2021 << cellColumn(i) + 1
2023 << cellRightColumn(i) + 1
2035 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2039 if (getRotateCell(cell)) {
2040 os << "\\begin{sideways}\n";
2043 if (isMultiColumn(cell)) {
2044 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2045 if (leftLine(cell) &&
2046 (isFirstCellInRow(cell) ||
2047 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2048 !rightLine(cell - 1, true))))
2050 if (!cellinfo_of_cell(cell).align_special.empty()) {
2051 os << cellinfo_of_cell(cell).align_special;
2053 if (!getPWidth(cell).zero()) {
2054 switch (getVAlignment(cell)) {
2055 case LYX_VALIGN_TOP:
2058 case LYX_VALIGN_MIDDLE:
2061 case LYX_VALIGN_BOTTOM:
2066 << from_ascii(getPWidth(cell).asLatexString())
2069 switch (getAlignment(cell)) {
2070 case LYX_ALIGN_LEFT:
2073 case LYX_ALIGN_RIGHT:
2080 } // end if else !getPWidth
2081 } // end if else !cellinfo_of_cell
2082 if (rightLine(cell))
2084 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2089 if (getUsebox(cell) == BOX_PARBOX) {
2091 switch (getVAlignment(cell)) {
2092 case LYX_VALIGN_TOP:
2095 case LYX_VALIGN_MIDDLE:
2098 case LYX_VALIGN_BOTTOM:
2102 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2104 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2105 os << "\\begin{minipage}[";
2106 switch (getVAlignment(cell)) {
2107 case LYX_VALIGN_TOP:
2110 case LYX_VALIGN_MIDDLE:
2113 case LYX_VALIGN_BOTTOM:
2117 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2125 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2130 if (getUsebox(cell) == BOX_PARBOX)
2132 else if (getUsebox(cell) == BOX_MINIPAGE) {
2133 os << "%\n\\end{minipage}";
2136 if (isMultiColumn(cell)) {
2139 if (getRotateCell(cell)) {
2140 os << "%\n\\end{sideways}";
2147 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2148 OutputParams const & runparams) const
2150 if (!is_long_tabular)
2154 // output header info
2156 if (endhead.topDL) {
2160 for (row_type i = 0; i < rowCount(); ++i) {
2161 if (row_info[i].endhead) {
2162 ret += TeXRow(os, i, buf, runparams);
2165 if (endhead.bottomDL) {
2169 os << "\\endhead\n";
2171 if (endfirsthead.empty) {
2172 os << "\\endfirsthead\n";
2176 // output firstheader info
2177 if (haveLTFirstHead()) {
2178 if (endfirsthead.topDL) {
2182 for (row_type i = 0; i < rowCount(); ++i) {
2183 if (row_info[i].endfirsthead) {
2184 ret += TeXRow(os, i, buf, runparams);
2187 if (endfirsthead.bottomDL) {
2191 os << "\\endfirsthead\n";
2194 // output footer info
2196 if (endfoot.topDL) {
2200 for (row_type i = 0; i < rowCount(); ++i) {
2201 if (row_info[i].endfoot) {
2202 ret += TeXRow(os, i, buf, runparams);
2205 if (endfoot.bottomDL) {
2209 os << "\\endfoot\n";
2211 if (endlastfoot.empty) {
2212 os << "\\endlastfoot\n";
2216 // output lastfooter info
2217 if (haveLTLastFoot()) {
2218 if (endlastfoot.topDL) {
2222 for (row_type i = 0; i < rowCount(); ++i) {
2223 if (row_info[i].endlastfoot) {
2224 ret += TeXRow(os, i, buf, runparams);
2227 if (endlastfoot.bottomDL) {
2231 os << "\\endlastfoot\n";
2238 bool Tabular::isValidRow(row_type row) const
2240 if (!is_long_tabular)
2242 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2243 !row_info[row].endfoot && !row_info[row].endlastfoot;
2247 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2248 OutputParams const & runparams) const
2250 idx_type cell = cellIndex(i, 0);
2251 int ret = TeXTopHLine(os, i);
2252 if (row_info[i].top_space_default) {
2254 os << "\\addlinespace\n";
2256 os << "\\noalign{\\vskip\\doublerulesep}\n";
2258 } else if(!row_info[i].top_space.zero()) {
2260 os << "\\addlinespace["
2261 << from_ascii(row_info[i].top_space.asLatexString())
2264 os << "\\noalign{\\vskip"
2265 << from_ascii(row_info[i].top_space.asLatexString())
2271 for (col_type j = 0; j < columnCount(); ++j) {
2272 if (isPartOfMultiColumn(i, j))
2274 ret += TeXCellPreamble(os, cell);
2275 shared_ptr<InsetText> inset = getCellInset(cell);
2277 Paragraph const & par = inset->paragraphs().front();
2278 bool rtl = par.isRTL(buf.params())
2280 && getPWidth(cell).zero();
2283 if (par.getParLanguage(buf.params())->lang() ==
2286 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2288 // currently, remaning RTL languages are arabic_arabtex and hebrew
2292 ret += inset->latex(buf, os, runparams);
2296 ret += TeXCellPostamble(os, cell);
2297 if (!isLastCellInRow(cell)) { // not last cell in row
2302 os << "\\tabularnewline";
2303 if (row_info[i].bottom_space_default) {
2305 os << "\\addlinespace";
2307 os << "[\\doublerulesep]";
2308 } else if (!row_info[i].bottom_space.zero()) {
2310 os << "\\addlinespace";
2312 << from_ascii(row_info[i].bottom_space.asLatexString())
2317 ret += TeXBottomHLine(os, i);
2318 if (row_info[i].interline_space_default) {
2320 os << "\\addlinespace\n";
2322 os << "\\noalign{\\vskip\\doublerulesep}\n";
2324 } else if (!row_info[i].interline_space.zero()) {
2326 os << "\\addlinespace["
2327 << from_ascii(row_info[i].interline_space.asLatexString())
2330 os << "\\noalign{\\vskip"
2331 << from_ascii(row_info[i].interline_space.asLatexString())
2339 int Tabular::latex(Buffer const & buf, odocstream & os,
2340 OutputParams const & runparams) const
2344 //+---------------------------------------------------------------------
2345 //+ first the opening preamble +
2346 //+---------------------------------------------------------------------
2349 os << "\\begin{sideways}\n";
2352 if (is_long_tabular)
2353 os << "\\begin{longtable}{";
2355 os << "\\begin{tabular}{";
2356 for (col_type i = 0; i < columnCount(); ++i) {
2357 if (!use_booktabs && column_info[i].left_line)
2359 if (!column_info[i].align_special.empty()) {
2360 os << column_info[i].align_special;
2362 if (!column_info[i].p_width.zero()) {
2363 switch (column_info[i].alignment) {
2364 case LYX_ALIGN_LEFT:
2365 os << ">{\\raggedright}";
2367 case LYX_ALIGN_RIGHT:
2368 os << ">{\\raggedleft}";
2370 case LYX_ALIGN_CENTER:
2371 os << ">{\\centering}";
2373 case LYX_ALIGN_NONE:
2374 case LYX_ALIGN_BLOCK:
2375 case LYX_ALIGN_LAYOUT:
2376 case LYX_ALIGN_SPECIAL:
2380 switch (column_info[i].valignment) {
2381 case LYX_VALIGN_TOP:
2384 case LYX_VALIGN_MIDDLE:
2387 case LYX_VALIGN_BOTTOM:
2392 << from_ascii(column_info[i].p_width.asLatexString())
2395 switch (column_info[i].alignment) {
2396 case LYX_ALIGN_LEFT:
2399 case LYX_ALIGN_RIGHT:
2406 } // end if else !column_info[i].p_width
2407 } // end if else !column_info[i].align_special
2408 if (!use_booktabs && column_info[i].right_line)
2414 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2416 //+---------------------------------------------------------------------
2417 //+ the single row and columns (cells) +
2418 //+---------------------------------------------------------------------
2420 for (row_type i = 0; i < rowCount(); ++i) {
2421 if (isValidRow(i)) {
2422 ret += TeXRow(os, i, buf, runparams);
2423 if (is_long_tabular && row_info[i].newpage) {
2424 os << "\\newpage\n";
2430 //+---------------------------------------------------------------------
2431 //+ the closing of the tabular +
2432 //+---------------------------------------------------------------------
2434 if (is_long_tabular)
2435 os << "\\end{longtable}";
2437 os << "\\end{tabular}";
2439 os << "\n\\end{sideways}";
2447 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2448 OutputParams const & runparams) const
2451 idx_type cell = getFirstCellInRow(row);
2454 for (col_type j = 0; j < columnCount(); ++j) {
2455 if (isPartOfMultiColumn(row, j))
2458 os << "<entry align=\"";
2459 switch (getAlignment(cell)) {
2460 case LYX_ALIGN_LEFT:
2463 case LYX_ALIGN_RIGHT:
2471 os << "\" valign=\"";
2472 switch (getVAlignment(cell)) {
2473 case LYX_VALIGN_TOP:
2476 case LYX_VALIGN_BOTTOM:
2479 case LYX_VALIGN_MIDDLE:
2484 if (isMultiColumn(cell)) {
2485 os << " namest=\"col" << j << "\" ";
2486 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2490 ret += getCellInset(cell)->docbook(buf, os, runparams);
2499 int Tabular::docbook(Buffer const & buf, odocstream & os,
2500 OutputParams const & runparams) const
2504 //+---------------------------------------------------------------------
2505 //+ first the opening preamble +
2506 //+---------------------------------------------------------------------
2508 os << "<tgroup cols=\"" << columnCount()
2509 << "\" colsep=\"1\" rowsep=\"1\">\n";
2511 for (col_type i = 0; i < columnCount(); ++i) {
2512 os << "<colspec colname=\"col" << i << "\" align=\"";
2513 switch (column_info[i].alignment) {
2514 case LYX_ALIGN_LEFT:
2517 case LYX_ALIGN_RIGHT:
2525 if (runparams.flavor == OutputParams::XML)
2531 //+---------------------------------------------------------------------
2532 //+ Long Tabular case +
2533 //+---------------------------------------------------------------------
2535 // output header info
2536 if (haveLTHead() || haveLTFirstHead()) {
2539 for (row_type i = 0; i < rowCount(); ++i) {
2540 if (row_info[i].endhead || row_info[i].endfirsthead) {
2541 ret += docbookRow(buf, os, i, runparams);
2547 // output footer info
2548 if (haveLTFoot() || haveLTLastFoot()) {
2551 for (row_type i = 0; i < rowCount(); ++i) {
2552 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2553 ret += docbookRow(buf, os, i, runparams);
2560 //+---------------------------------------------------------------------
2561 //+ the single row and columns (cells) +
2562 //+---------------------------------------------------------------------
2566 for (row_type i = 0; i < rowCount(); ++i) {
2567 if (isValidRow(i)) {
2568 ret += docbookRow(buf, os, i, runparams);
2573 //+---------------------------------------------------------------------
2574 //+ the closing of the tabular +
2575 //+---------------------------------------------------------------------
2584 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2585 vector<unsigned int> const & clen) const
2587 idx_type const fcell = getFirstCellInRow(row);
2588 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2591 for (idx_type i = fcell; i < n; ++i) {
2601 for (idx_type i = fcell; i < n; ++i) {
2612 col_type column = cellColumn(i);
2613 int len = clen[column];
2614 while (column < columnCount() - 1
2615 && isPartOfMultiColumn(row, ++column))
2616 len += clen[column] + 4;
2617 os << docstring(len, ch);
2632 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2633 vector<unsigned int> const & clen) const
2635 idx_type const fcell = getFirstCellInRow(row);
2636 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2639 for (idx_type i = fcell; i < n; ++i) {
2640 if (bottomLine(i)) {
2649 for (idx_type i = fcell; i < n; ++i) {
2650 if (bottomLine(i)) {
2660 col_type column = cellColumn(i);
2661 int len = clen[column];
2662 while (column < columnCount() -1
2663 && isPartOfMultiColumn(row, ++column))
2664 len += clen[column] + 4;
2665 os << docstring(len, ch);
2666 if (bottomLine(i)) {
2680 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2681 OutputParams const & runparams,
2682 idx_type cell, row_type row, col_type column,
2683 vector<unsigned int> const & clen,
2684 bool onlydata) const
2686 odocstringstream sstr;
2687 getCellInset(cell)->plaintext(buf, sstr, runparams);
2699 unsigned int len1 = sstr.str().length();
2700 unsigned int len2 = clen[column];
2701 while (column < columnCount() -1
2702 && isPartOfMultiColumn(row, ++column))
2703 len2 += clen[column] + 4;
2706 switch (getAlignment(cell)) {
2708 case LYX_ALIGN_LEFT:
2711 case LYX_ALIGN_RIGHT:
2715 case LYX_ALIGN_CENTER:
2721 os << docstring(len1, ' ') << sstr.str()
2722 << docstring(len2, ' ');
2724 if (rightLine(cell))
2731 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2732 OutputParams const & runparams, int const depth,
2733 bool onlydata, char_type delim) const
2735 // first calculate the width of the single columns
2736 vector<unsigned int> clen(columnCount());
2739 // first all non (real) multicolumn cells!
2740 for (col_type j = 0; j < columnCount(); ++j) {
2742 for (row_type i = 0; i < rowCount(); ++i) {
2743 idx_type cell = cellIndex(i, j);
2744 if (isMultiColumnReal(cell))
2746 odocstringstream sstr;
2747 getCellInset(cell)->plaintext(buf, sstr, runparams);
2748 if (clen[j] < sstr.str().length())
2749 clen[j] = sstr.str().length();
2752 // then all (real) multicolumn cells!
2753 for (col_type j = 0; j < columnCount(); ++j) {
2754 for (row_type i = 0; i < rowCount(); ++i) {
2755 idx_type cell = cellIndex(i, j);
2756 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2758 odocstringstream sstr;
2759 getCellInset(cell)->plaintext(buf, sstr, runparams);
2760 int len = int(sstr.str().length());
2761 idx_type const n = cells_in_multicolumn(cell);
2762 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2764 if (len > int(clen[j + n - 1]))
2765 clen[j + n - 1] = len;
2770 for (row_type i = 0; i < rowCount(); ++i) {
2771 if (!onlydata && plaintextTopHLine(os, i, clen))
2772 os << docstring(depth * 2, ' ');
2773 for (col_type j = 0; j < columnCount(); ++j) {
2774 if (isPartOfMultiColumn(i, j))
2776 if (onlydata && j > 0)
2777 // we don't use operator<< for single UCS4 character.
2778 // see explanation in docstream.h
2780 plaintextPrintCell(buf, os, runparams,
2781 cell, i, j, clen, onlydata);
2786 os << docstring(depth * 2, ' ');
2787 if (plaintextBottomHLine(os, i, clen))
2788 os << docstring(depth * 2, ' ');
2794 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2796 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2800 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2801 col_type column) const
2803 return cell_info[row][column].inset;
2807 void Tabular::setCellInset(row_type row, col_type column,
2808 shared_ptr<InsetText> ins) const
2810 cell_info[row][column].inset = ins;
2815 Tabular::getCellFromInset(Inset const * inset) const
2817 // is this inset part of the tabular?
2819 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2820 BOOST_ASSERT(false);
2823 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2824 if (getCellInset(cell).get() == inset) {
2825 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2826 << "cell=" << cell << endl;
2830 // We should have found a cell at this point
2831 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2832 << inset << " not found!" << endl;
2833 BOOST_ASSERT(false);
2839 void Tabular::validate(LaTeXFeatures & features) const
2841 features.require("NeedTabularnewline");
2843 features.require("booktabs");
2844 if (isLongTabular())
2845 features.require("longtable");
2847 features.require("rotating");
2848 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2849 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2850 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2851 features.require("array");
2852 getCellInset(cell)->validate(features);
2857 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2859 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2860 ParagraphList::const_iterator cit = parlist.begin();
2861 ParagraphList::const_iterator end = parlist.end();
2863 for (; cit != end; ++cit)
2864 for (int i = 0; i < cit->size(); ++i)
2865 if (cit->isNewline(i))
2872 /////////////////////////////////////////////////////////////////////
2876 /////////////////////////////////////////////////////////////////////
2878 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2880 : tabular(buf.params(), max(rows, row_type(1)),
2881 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2885 InsetTabular::InsetTabular(InsetTabular const & tab)
2886 : Inset(tab), tabular(tab.tabular),
2887 buffer_(tab.buffer_), scx_(0)
2891 InsetTabular::~InsetTabular()
2893 InsetTabularMailer(*this).hideDialog();
2897 Inset * InsetTabular::clone() const
2899 return new InsetTabular(*this);
2903 Buffer const & InsetTabular::buffer() const
2909 void InsetTabular::buffer(Buffer const * b)
2915 void InsetTabular::write(Buffer const & buf, ostream & os) const
2917 os << "Tabular" << endl;
2918 tabular.write(buf, os);
2922 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2924 bool const old_format = (lex.getString() == "\\LyXTable");
2926 tabular.read(buf, lex);
2932 string token = lex.getString();
2933 while (lex && token != "\\end_inset") {
2935 token = lex.getString();
2938 lex.printError("Missing \\end_inset at this point. "
2944 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2946 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2947 // mi.base.textwidth << "\n";
2949 lyxerr << "InsetTabular::metrics: need bv" << endl;
2950 BOOST_ASSERT(false);
2954 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2957 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2958 if (tabular.isPartOfMultiColumn(i, j))
2959 // Multicolumn cell, but not first one
2964 if (tabular.cell_info[i][j].multicolumn ==
2965 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2966 p_width = tabular.cellinfo_of_cell(cell).p_width;
2968 p_width = tabular.column_info[j].p_width;
2969 if (!p_width.zero())
2970 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2971 tabular.getCellInset(cell)->metrics(m, dim);
2972 if (!p_width.zero())
2973 dim.wid = m.base.textwidth;
2974 tabular.setCellWidth(cell, dim.wid);
2975 if (p_width.zero()) {
2976 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2977 // FIXME there must be a way to get rid of
2978 // the second metrics call
2979 tabular.getCellInset(cell)->metrics(m, dim);
2981 maxAsc = max(maxAsc, dim.asc);
2982 maxDesc = max(maxDesc, dim.des);
2985 int const top_space = tabular.row_info[i].top_space_default ?
2986 default_line_space :
2987 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2988 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2989 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2990 default_line_space :
2991 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2992 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2995 dim.asc = tabular.rowAscent(0);
2996 dim.des = tabular.height() - dim.asc;
2997 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3001 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3003 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3004 BufferView * bv = pi.base.bv;
3006 // FIXME: As the full backrgound is painted in drawSelection(),
3007 // we have no choice but to do a full repaint for the Text cells.
3008 pi.full_repaint = true;
3010 resetPos(bv->cursor());
3013 x += ADD_TO_TABULAR_WIDTH;
3016 first_visible_cell = Tabular::npos;
3017 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3019 int const a = tabular.rowAscent(i);
3020 int const d = tabular.rowDescent(i);
3021 idx = tabular.cellIndex(i, 0);
3022 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3023 if (tabular.isPartOfMultiColumn(i, j))
3025 if (first_visible_cell == Tabular::npos)
3026 first_visible_cell = idx;
3028 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3029 // Cache the Inset position.
3030 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3031 if (nx + tabular.columnWidth(idx) < 0
3032 || nx > bv->workWidth()
3034 || y - a > bv->workHeight()) {
3035 pi.pain.setDrawingEnabled(false);
3036 cell(idx)->draw(pi, cx, y);
3037 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3038 pi.pain.setDrawingEnabled(true);
3040 cell(idx)->draw(pi, cx, y);
3041 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3043 nx += tabular.columnWidth(idx);
3047 if (i + 1 < tabular.rowCount())
3048 y += d + tabular.rowAscent(i + 1) +
3049 tabular.getAdditionalHeight(i + 1);
3054 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3056 Cursor & cur = pi.base.bv->cursor();
3058 x += scx_ + ADD_TO_TABULAR_WIDTH;
3060 // FIXME: it is wrong to completely paint the background
3061 // if we want to do single row painting.
3063 // Paint background of current tabular
3064 int const w = tabular.width();
3065 int const h = tabular.height();
3066 int yy = y - tabular.rowAscent(0);
3067 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3069 if (!cur.selection())
3071 if (!ptr_cmp(&cur.inset(), this))
3077 if (tablemode(cur)) {
3080 getSelection(cur, rs, re, cs, ce);
3081 y -= tabular.rowAscent(0);
3082 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3083 int const a = tabular.rowAscent(j);
3084 int const h = a + tabular.rowDescent(j);
3086 y += tabular.getAdditionalHeight(j);
3087 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3088 if (tabular.isPartOfMultiColumn(j, i))
3090 idx_type const cell =
3091 tabular.cellIndex(j, i);
3092 int const w = tabular.columnWidth(cell);
3093 if (i >= cs && i <= ce && j >= rs && j <= re)
3094 pi.pain.fillRectangle(xx, y, w, h,
3102 x += getCellXPos(cur.idx());
3103 x += tabular.getBeginningOfTextInCell(cur.idx());
3104 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3109 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3110 row_type row, idx_type cell, bool erased) const
3112 int x2 = x + tabular.columnWidth(cell);
3113 bool on_off = false;
3114 ColorCode col = Color_tabularline;
3115 ColorCode onoffcol = Color_tabularonoffline;
3118 col = Color_deletedtext;
3119 onoffcol = Color_deletedtext;
3122 if (!tabular.topAlreadyDrawn(cell)) {
3123 on_off = !tabular.topLine(cell);
3124 pain.line(x, y - tabular.rowAscent(row),
3125 x2, y - tabular.rowAscent(row),
3126 on_off ? onoffcol : col,
3127 on_off ? Painter::line_onoffdash : Painter::line_solid);
3129 on_off = !tabular.bottomLine(cell);
3130 pain.line(x, y + tabular.rowDescent(row),
3131 x2, y + tabular.rowDescent(row),
3132 on_off ? onoffcol : col,
3133 on_off ? Painter::line_onoffdash : Painter::line_solid);
3134 if (!tabular.leftAlreadyDrawn(cell)) {
3135 on_off = !tabular.leftLine(cell);
3136 pain.line(x, y - tabular.rowAscent(row),
3137 x, y + tabular.rowDescent(row),
3138 on_off ? onoffcol : col,
3139 on_off ? Painter::line_onoffdash : Painter::line_solid);
3141 on_off = !tabular.rightLine(cell);
3142 pain.line(x2 - tabular.getAdditionalWidth(cell),
3143 y - tabular.rowAscent(row),
3144 x2 - tabular.getAdditionalWidth(cell),
3145 y + tabular.rowDescent(row),
3146 on_off ? onoffcol : col,
3147 on_off ? Painter::line_onoffdash : Painter::line_solid);
3151 docstring const InsetTabular::editMessage() const
3153 return _("Opened table");
3157 void InsetTabular::edit(Cursor & cur, bool left)
3159 //lyxerr << "InsetTabular::edit: " << this << endl;
3161 cur.selection() = false;
3164 if (isRightToLeft(cur))
3165 cur.idx() = tabular.getLastCellInRow(0);
3171 if (isRightToLeft(cur))
3172 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3174 cur.idx() = tabular.cellCount() - 1;
3176 cur.pos() = cur.lastpos(); // FIXME crude guess
3178 // FIXME: this accesses the position cache before it is initialized
3180 //cur.bv().fitCursor();
3184 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3186 // In a longtable, tell captions what the current float is
3187 Counters & cnts = buf.params().getTextClass().counters();
3188 string const saveflt = cnts.current_float();
3189 if (tabular.isLongTabular())
3190 cnts.current_float("table");
3192 ParIterator it2 = it;
3194 size_t const end = it2.nargs();
3195 for ( ; it2.idx() < end; it2.top().forwardIdx())
3196 lyx::updateLabels(buf, it2);
3199 if (tabular.isLongTabular())
3200 cnts.current_float(saveflt);
3204 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3206 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3207 << "\n cur:" << cur << endl;
3208 CursorSlice sl = cur.top();
3209 Cursor & bvcur = cur.bv().cursor();
3211 switch (cmd.action) {
3213 case LFUN_MOUSE_PRESS:
3214 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3216 // do not reset cursor/selection if we have selected
3217 // some cells (bug 2715).
3218 if (cmd.button() == mouse_button::button3
3219 && &bvcur.selBegin().inset() == this
3220 && tablemode(bvcur))
3223 // Let InsetText do it
3224 cell(cur.idx())->dispatch(cur, cmd);
3226 case LFUN_MOUSE_MOTION:
3227 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3228 if (cmd.button() == mouse_button::button1) {
3229 // only accept motions to places not deeper nested than the real anchor
3230 if (bvcur.anchor_.hasPart(cur)) {
3231 // only update if selection changes
3232 if (bvcur.idx() == cur.idx() &&
3233 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3235 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3236 bvcur.setCursor(cur);
3237 bvcur.selection() = true;
3243 case LFUN_MOUSE_RELEASE:
3244 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3245 if (cmd.button() == mouse_button::button3)
3246 InsetTabularMailer(*this).showDialog(&cur.bv());
3249 case LFUN_CELL_BACKWARD:
3251 cur.selection() = false;
3254 case LFUN_CELL_FORWARD:
3256 cur.selection() = false;
3259 case LFUN_CHAR_FORWARD_SELECT:
3260 case LFUN_CHAR_FORWARD:
3261 cell(cur.idx())->dispatch(cur, cmd);
3262 if (!cur.result().dispatched()) {
3264 if (sl == cur.top())
3265 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3271 case LFUN_CHAR_BACKWARD_SELECT:
3272 case LFUN_CHAR_BACKWARD:
3273 cell(cur.idx())->dispatch(cur, cmd);
3274 if (!cur.result().dispatched()) {
3276 if (sl == cur.top())
3277 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3283 case LFUN_CHAR_RIGHT_SELECT:
3284 case LFUN_CHAR_RIGHT:
3285 //FIXME: for visual cursor, really move right
3286 if (isRightToLeft(cur))
3287 lyx::dispatch(FuncRequest(
3288 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3289 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3291 lyx::dispatch(FuncRequest(
3292 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3293 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3296 case LFUN_CHAR_LEFT_SELECT:
3297 case LFUN_CHAR_LEFT:
3298 //FIXME: for visual cursor, really move left
3299 if (isRightToLeft(cur))
3300 lyx::dispatch(FuncRequest(
3301 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3302 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3304 lyx::dispatch(FuncRequest(
3305 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3306 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3309 case LFUN_DOWN_SELECT:
3311 cell(cur.idx())->dispatch(cur, cmd);
3312 cur.dispatched(); // override the cell's decision
3313 if (sl == cur.top())
3314 // if our Text didn't do anything to the cursor
3315 // then we try to put the cursor into the cell below
3316 // setting also the right targetX.
3317 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3318 cur.idx() = tabular.getCellBelow(cur.idx());
3320 TextMetrics const & tm =
3321 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3322 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3324 if (sl == cur.top()) {
3325 // we trick it to go to forward after leaving the
3327 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3332 case LFUN_UP_SELECT:
3334 cell(cur.idx())->dispatch(cur, cmd);
3335 cur.dispatched(); // override the cell's decision
3336 if (sl == cur.top())
3337 // if our Text didn't do anything to the cursor
3338 // then we try to put the cursor into the cell above
3339 // setting also the right targetX.
3340 if (tabular.cellRow(cur.idx()) != 0) {
3341 cur.idx() = tabular.getCellAbove(cur.idx());
3342 cur.pit() = cur.lastpit();
3343 Text const * text = cell(cur.idx())->getText(0);
3344 TextMetrics const & tm = cur.bv().textMetrics(text);
3345 ParagraphMetrics const & pm =
3346 tm.parMetrics(cur.lastpit());
3347 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3349 if (sl == cur.top()) {
3350 cmd = FuncRequest(LFUN_UP);
3355 // case LFUN_SCREEN_DOWN: {
3356 // //if (hasSelection())
3357 // // cur.selection() = false;
3358 // col_type const col = tabular.cellColumn(cur.idx());
3359 // int const t = cur.bv().top_y() + cur.bv().height();
3360 // if (t < yo() + tabular.getHeightOfTabular()) {
3361 // cur.bv().scrollDocView(t);
3362 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3364 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3371 // case LFUN_SCREEN_UP: {
3372 // //if (hasSelection())
3373 // // cur.selection() = false;
3374 // col_type const col = tabular.cellColumn(cur.idx());
3375 // int const t = cur.bv().top_y() + cur.bv().height();
3377 // cur.bv().scrollDocView(t);
3381 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3385 // cur.par() = cur.lastpar();
3386 // cur.pos() = cur.lastpos();
3390 case LFUN_LAYOUT_TABULAR:
3391 InsetTabularMailer(*this).showDialog(&cur.bv());
3394 case LFUN_INSET_DIALOG_UPDATE:
3395 InsetTabularMailer(*this).updateDialog(&cur.bv());
3398 case LFUN_TABULAR_FEATURE:
3399 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3403 // insert file functions
3404 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3405 case LFUN_FILE_INSERT_PLAINTEXT: {
3407 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3408 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3414 if (tablemode(cur)) {
3415 if (copySelection(cur)) {
3416 cur.recordUndoInset(DELETE_UNDO);
3421 cell(cur.idx())->dispatch(cur, cmd);
3424 case LFUN_CHAR_DELETE_BACKWARD:
3425 case LFUN_CHAR_DELETE_FORWARD:
3426 if (tablemode(cur)) {
3427 cur.recordUndoInset(DELETE_UNDO);
3431 cell(cur.idx())->dispatch(cur, cmd);
3435 if (!cur.selection())
3437 if (tablemode(cur)) {
3441 cell(cur.idx())->dispatch(cur, cmd);
3444 case LFUN_CLIPBOARD_PASTE:
3445 case LFUN_PRIMARY_SELECTION_PASTE: {
3446 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3447 theClipboard().getAsText() :
3448 theSelection().get();
3451 // pass to InsertPlaintextString, but
3452 // only if we have multi-cell content
3453 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3454 if (insertPlaintextString(cur.bv(), clip, false)) {
3455 // content has been replaced,
3456 // so cursor might be invalid
3457 cur.pos() = cur.lastpos();
3458 bvcur.setCursor(cur);
3462 // Let the cell handle normal text
3463 cell(cur.idx())->dispatch(cur, cmd);
3468 if (tabularStackDirty() && theClipboard().isInternal()) {
3469 cur.recordUndoInset(INSERT_UNDO);
3470 pasteClipboard(cur);
3473 cell(cur.idx())->dispatch(cur, cmd);
3476 case LFUN_FONT_EMPH:
3477 case LFUN_FONT_BOLD:
3478 case LFUN_FONT_ROMAN:
3479 case LFUN_FONT_NOUN:
3480 case LFUN_FONT_ITAL:
3481 case LFUN_FONT_FRAK:
3482 case LFUN_FONT_TYPEWRITER:
3483 case LFUN_FONT_SANS:
3484 case LFUN_FONT_FREE_APPLY:
3485 case LFUN_FONT_FREE_UPDATE:
3486 case LFUN_FONT_SIZE:
3487 case LFUN_FONT_UNDERLINE:
3489 case LFUN_WORD_CAPITALIZE:
3490 case LFUN_WORD_UPCASE:
3491 case LFUN_WORD_LOWCASE:
3492 case LFUN_CHARS_TRANSPOSE:
3493 if (tablemode(cur)) {
3496 getSelection(cur, rs, re, cs, ce);
3497 Cursor tmpcur = cur;
3498 for (row_type i = rs; i <= re; ++i) {
3499 for (col_type j = cs; j <= ce; ++j) {
3500 // cursor follows cell:
3501 tmpcur.idx() = tabular.cellIndex(i, j);
3502 // select this cell only:
3505 tmpcur.resetAnchor();
3506 tmpcur.pit() = tmpcur.lastpit();
3507 tmpcur.pos() = tmpcur.top().lastpos();
3508 tmpcur.setCursor(tmpcur);
3509 tmpcur.setSelection();
3510 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3515 cell(cur.idx())->dispatch(cur, cmd);
3519 // we try to handle this event in the insets dispatch function.
3520 cell(cur.idx())->dispatch(cur, cmd);
3526 // function sets an object as defined in func_status.h:
3527 // states OK, Unknown, Disabled, On, Off.
3528 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3529 FuncStatus & status) const
3531 switch (cmd.action) {
3532 case LFUN_TABULAR_FEATURE: {
3533 int action = Tabular::LAST_ACTION;
3535 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3536 string const tmp = tabularFeature[i].feature;
3537 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3538 action = tabularFeature[i].action;
3542 if (action == Tabular::LAST_ACTION) {
3544 status.unknown(true);
3548 string const argument
3549 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3551 row_type sel_row_start = 0;
3552 row_type sel_row_end = 0;
3553 col_type sel_col_start = 0;
3554 col_type sel_col_end = 0;
3555 Tabular::ltType dummyltt;
3558 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3561 case Tabular::SET_PWIDTH:
3562 case Tabular::SET_MPWIDTH:
3563 case Tabular::SET_SPECIAL_COLUMN:
3564 case Tabular::SET_SPECIAL_MULTI:
3565 case Tabular::APPEND_ROW:
3566 case Tabular::APPEND_COLUMN:
3567 case Tabular::DELETE_ROW:
3568 case Tabular::DELETE_COLUMN:
3569 case Tabular::COPY_ROW:
3570 case Tabular::COPY_COLUMN:
3571 case Tabular::SET_ALL_LINES:
3572 case Tabular::UNSET_ALL_LINES:
3573 case Tabular::SET_TOP_SPACE:
3574 case Tabular::SET_BOTTOM_SPACE:
3575 case Tabular::SET_INTERLINE_SPACE:
3579 case Tabular::MULTICOLUMN:
3580 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3583 case Tabular::M_TOGGLE_LINE_TOP:
3585 case Tabular::TOGGLE_LINE_TOP:
3586 status.setOnOff(tabular.topLine(cur.idx(), flag));
3589 case Tabular::M_TOGGLE_LINE_BOTTOM:
3591 case Tabular::TOGGLE_LINE_BOTTOM:
3592 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3595 case Tabular::M_TOGGLE_LINE_LEFT:
3597 case Tabular::TOGGLE_LINE_LEFT:
3598 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3601 case Tabular::M_TOGGLE_LINE_RIGHT:
3603 case Tabular::TOGGLE_LINE_RIGHT:
3604 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3607 case Tabular::M_ALIGN_LEFT:
3609 case Tabular::ALIGN_LEFT:
3610 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3613 case Tabular::M_ALIGN_RIGHT:
3615 case Tabular::ALIGN_RIGHT:
3616 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3619 case Tabular::M_ALIGN_CENTER:
3621 case Tabular::ALIGN_CENTER:
3622 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3625 case Tabular::ALIGN_BLOCK:
3626 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3627 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3630 case Tabular::M_VALIGN_TOP:
3632 case Tabular::VALIGN_TOP:
3634 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3637 case Tabular::M_VALIGN_BOTTOM:
3639 case Tabular::VALIGN_BOTTOM:
3641 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3644 case Tabular::M_VALIGN_MIDDLE:
3646 case Tabular::VALIGN_MIDDLE:
3648 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3651 case Tabular::SET_LONGTABULAR:
3652 status.setOnOff(tabular.isLongTabular());
3655 case Tabular::UNSET_LONGTABULAR:
3656 status.setOnOff(!tabular.isLongTabular());
3659 case Tabular::TOGGLE_ROTATE_TABULAR:
3660 case Tabular::SET_ROTATE_TABULAR:
3661 status.setOnOff(tabular.getRotateTabular());
3664 case Tabular::UNSET_ROTATE_TABULAR:
3665 status.setOnOff(!tabular.getRotateTabular());
3668 case Tabular::TOGGLE_ROTATE_CELL:
3669 case Tabular::SET_ROTATE_CELL:
3670 status.setOnOff(!oneCellHasRotationState(false,
3671 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3674 case Tabular::UNSET_ROTATE_CELL:
3675 status.setOnOff(!oneCellHasRotationState(true,
3676 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3679 case Tabular::SET_USEBOX:
3680 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3683 case Tabular::SET_LTFIRSTHEAD:
3684 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3687 case Tabular::UNSET_LTFIRSTHEAD:
3688 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3691 case Tabular::SET_LTHEAD:
3692 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3695 case Tabular::UNSET_LTHEAD:
3696 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3699 case Tabular::SET_LTFOOT:
3700 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3703 case Tabular::UNSET_LTFOOT:
3704 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3707 case Tabular::SET_LTLASTFOOT:
3708 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3711 case Tabular::UNSET_LTLASTFOOT:
3712 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3715 case Tabular::SET_LTNEWPAGE:
3716 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3719 case Tabular::SET_BOOKTABS:
3720 status.setOnOff(tabular.useBookTabs());
3723 case Tabular::UNSET_BOOKTABS:
3724 status.setOnOff(!tabular.useBookTabs());
3729 status.enabled(false);
3735 // These are only enabled inside tabular
3736 case LFUN_CELL_BACKWARD:
3737 case LFUN_CELL_FORWARD:
3738 status.enabled(true);
3741 // disable these with multiple cells selected
3742 case LFUN_INSET_INSERT:
3743 case LFUN_TABULAR_INSERT:
3744 case LFUN_FLEX_INSERT:
3745 case LFUN_FLOAT_INSERT:
3746 case LFUN_FLOAT_WIDE_INSERT:
3747 case LFUN_FOOTNOTE_INSERT:
3748 case LFUN_MARGINALNOTE_INSERT:
3749 case LFUN_MATH_INSERT:
3750 case LFUN_MATH_MODE:
3751 case LFUN_MATH_MUTATE:
3752 case LFUN_MATH_DISPLAY:
3753 case LFUN_NOTE_INSERT:
3754 case LFUN_OPTIONAL_INSERT:
3755 case LFUN_BOX_INSERT:
3756 case LFUN_BRANCH_INSERT:
3757 case LFUN_WRAP_INSERT:
3758 case LFUN_ERT_INSERT: {
3759 if (tablemode(cur)) {
3760 status.enabled(false);
3763 return cell(cur.idx())->getStatus(cur, cmd, status);
3766 // disable in non-fixed-width cells
3767 case LFUN_BREAK_LINE:
3768 case LFUN_BREAK_PARAGRAPH:
3769 case LFUN_BREAK_PARAGRAPH_SKIP: {
3770 if (tabular.getPWidth(cur.idx()).zero()) {
3771 status.enabled(false);
3774 return cell(cur.idx())->getStatus(cur, cmd, status);
3778 if (tabularStackDirty() && theClipboard().isInternal()) {
3779 status.enabled(true);
3782 return cell(cur.idx())->getStatus(cur, cmd, status);
3784 case LFUN_INSET_MODIFY:
3785 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3786 status.enabled(true);
3792 // we try to handle this event in the insets dispatch function.
3793 return cell(cur.idx())->getStatus(cur, cmd, status);
3798 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3799 OutputParams const & runparams) const
3801 return tabular.latex(buf, os, runparams);
3805 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3806 OutputParams const & runparams) const
3808 os << '\n'; // output table on a new line
3809 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3810 tabular.plaintext(buf, os, runparams, dp, false, 0);
3811 return PLAINTEXT_NEWLINE;
3815 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3816 OutputParams const & runparams) const
3821 // FIXME: Why not pass a proper DocIterator here?
3823 // if the table is inside a float it doesn't need the informaltable
3824 // wrapper. Search for it.
3825 for (master = owner(); master; master = master->owner())
3826 if (master->lyxCode() == FLOAT_CODE)
3831 os << "<informaltable>";
3834 ret += tabular.docbook(buf, os, runparams);
3836 os << "</informaltable>";
3843 void InsetTabular::validate(LaTeXFeatures & features) const
3845 tabular.validate(features);
3849 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3851 return tabular.getCellInset(idx);
3855 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3857 return tabular.getCellInset(idx);
3861 void InsetTabular::cursorPos(BufferView const & bv,
3862 CursorSlice const & sl, bool boundary, int & x, int & y) const
3864 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3866 // y offset correction
3867 int const row = tabular.cellRow(sl.idx());
3868 for (int i = 0; i <= row; ++i) {
3870 y += tabular.rowAscent(i);
3871 y += tabular.getAdditionalHeight(i);
3874 y += tabular.rowDescent(i);
3877 // x offset correction
3878 int const col = tabular.cellColumn(sl.idx());
3879 int idx = tabular.cellIndex(row, 0);
3880 for (int j = 0; j < col; ++j) {
3881 if (tabular.isPartOfMultiColumn(row, j))
3883 x += tabular.columnWidth(idx);
3886 x += tabular.getBeginningOfTextInCell(idx);
3887 x += ADD_TO_TABULAR_WIDTH;
3892 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3896 Inset const & inset = *tabular.getCellInset(cell);
3897 Point o = bv.coordCache().getInsets().xy(&inset);
3898 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3899 int const xend = xbeg + tabular.columnWidth(cell);
3900 row_type const row = tabular.cellRow(cell);
3901 int const ybeg = o.y_ - tabular.rowAscent(row) -
3902 tabular.getAdditionalHeight(row);
3903 int const yend = o.y_ + tabular.rowDescent(row);
3915 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3916 // << " ybeg=" << ybeg << " yend=" << yend
3917 // << " xx=" << xx << " yy=" << yy
3918 // << " dist=" << xx + yy << endl;
3923 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3925 //lyxerr << "InsetTabular::editXY: " << this << endl;
3926 cur.selection() = false;
3928 cur.idx() = getNearestCell(cur.bv(), x, y);
3930 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3934 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3936 cur.idx() = getNearestCell(cur.bv(), x, y);
3937 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3941 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3943 idx_type idx_min = 0;
3944 int dist_min = std::numeric_limits<int>::max();
3945 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3946 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3947 int const d = dist(bv, i, x, y);
3958 int InsetTabular::getCellXPos(idx_type const cell) const
3962 for (; !tabular.isFirstCellInRow(c); --c)
3965 for (; c < cell; ++c)
3966 lx += tabular.columnWidth(c);
3972 void InsetTabular::resetPos(Cursor & cur) const
3974 BufferView & bv = cur.bv();
3975 int const maxwidth = bv.workWidth();
3977 if (&cur.inset() != this) {
3981 int const X2 = maxwidth;
3982 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3983 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3984 int const x2 = x1 + tabular.columnWidth(cur.idx());
3987 scx_ = X1 + 20 - x1;
3989 scx_ = X2 - 20 - x2;
3994 cur.updateFlags(Update::Force | Update::FitCursor);
3998 void InsetTabular::moveNextCell(Cursor & cur)
4000 if (isRightToLeft(cur)) {
4001 if (tabular.isFirstCellInRow(cur.idx())) {
4002 row_type const row = tabular.cellRow(cur.idx());
4003 if (row == tabular.rowCount() - 1)
4005 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4012 if (tabular.isLastCell(cur.idx()))
4022 void InsetTabular::movePrevCell(Cursor & cur)
4024 if (isRightToLeft(cur)) {
4025 if (tabular.isLastCellInRow(cur.idx())) {
4026 row_type const row = tabular.cellRow(cur.idx());
4029 cur.idx() = tabular.getFirstCellInRow(row);
4030 cur.idx() = tabular.getCellAbove(cur.idx());
4032 if (tabular.isLastCell(cur.idx()))
4037 if (cur.idx() == 0) // first cell
4041 cur.pit() = cur.lastpit();
4042 cur.pos() = cur.lastpos();
4044 // FIXME: this accesses the position cache before it is initialized
4049 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4051 Tabular::Feature action = Tabular::LAST_ACTION;
4054 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4055 string const tmp = tabularFeature[i].feature;
4057 if (tmp == what.substr(0, tmp.length())) {
4058 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4059 //tabularFeatures[i].feature.length()))
4060 action = tabularFeature[i].action;
4064 if (action == Tabular::LAST_ACTION)
4068 ltrim(what.substr(tabularFeature[i].feature.length()));
4069 tabularFeatures(cur, action, val);
4074 static void checkLongtableSpecial(Tabular::ltType & ltt,
4075 string const & special, bool & flag)
4077 if (special == "dl_above") {
4080 } else if (special == "dl_below") {
4081 ltt.bottomDL = flag;
4083 } else if (special == "empty") {
4092 bool InsetTabular::oneCellHasRotationState(bool rotated,
4093 row_type row_start, row_type row_end,
4094 col_type col_start, col_type col_end) const {
4096 for (row_type i = row_start; i <= row_end; ++i) {
4097 for (col_type j = col_start; j <= col_end; ++j) {
4098 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4107 void InsetTabular::tabularFeatures(Cursor & cur,
4108 Tabular::Feature feature, string const & value)
4110 BufferView & bv = cur.bv();
4111 col_type sel_col_start;
4112 col_type sel_col_end;
4113 row_type sel_row_start;
4114 row_type sel_row_end;
4115 bool setLines = false;
4116 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4117 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4121 case Tabular::M_ALIGN_LEFT:
4122 case Tabular::ALIGN_LEFT:
4123 setAlign = LYX_ALIGN_LEFT;
4126 case Tabular::M_ALIGN_RIGHT:
4127 case Tabular::ALIGN_RIGHT:
4128 setAlign = LYX_ALIGN_RIGHT;
4131 case Tabular::M_ALIGN_CENTER:
4132 case Tabular::ALIGN_CENTER:
4133 setAlign = LYX_ALIGN_CENTER;
4136 case Tabular::ALIGN_BLOCK:
4137 setAlign = LYX_ALIGN_BLOCK;
4140 case Tabular::M_VALIGN_TOP:
4141 case Tabular::VALIGN_TOP:
4142 setVAlign = Tabular::LYX_VALIGN_TOP;
4145 case Tabular::M_VALIGN_BOTTOM:
4146 case Tabular::VALIGN_BOTTOM:
4147 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4150 case Tabular::M_VALIGN_MIDDLE:
4151 case Tabular::VALIGN_MIDDLE:
4152 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4159 cur.recordUndoInset(ATOMIC_UNDO);
4161 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4162 row_type const row = tabular.cellRow(cur.idx());
4163 col_type const column = tabular.cellColumn(cur.idx());
4165 Tabular::ltType ltt;
4169 case Tabular::SET_PWIDTH: {
4170 Length const len(value);
4171 tabular.setColumnPWidth(cur, cur.idx(), len);
4173 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4174 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4178 case Tabular::SET_MPWIDTH:
4179 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4182 case Tabular::SET_SPECIAL_COLUMN:
4183 case Tabular::SET_SPECIAL_MULTI:
4184 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4187 case Tabular::APPEND_ROW:
4188 // append the row into the tabular
4189 tabular.appendRow(bv.buffer().params(), cur.idx());
4192 case Tabular::APPEND_COLUMN:
4193 // append the column into the tabular
4194 tabular.appendColumn(bv.buffer().params(), cur.idx());
4195 cur.idx() = tabular.cellIndex(row, column);
4198 case Tabular::DELETE_ROW:
4199 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4200 tabular.deleteRow(sel_row_start);
4201 if (sel_row_start >= tabular.rowCount())
4203 cur.idx() = tabular.cellIndex(sel_row_start, column);
4206 cur.selection() = false;
4209 case Tabular::DELETE_COLUMN:
4210 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4211 tabular.deleteColumn(sel_col_start);
4212 if (sel_col_start >= tabular.columnCount())
4214 cur.idx() = tabular.cellIndex(row, sel_col_start);
4217 cur.selection() = false;
4220 case Tabular::COPY_ROW:
4221 tabular.copyRow(bv.buffer().params(), row);
4224 case Tabular::COPY_COLUMN:
4225 tabular.copyColumn(bv.buffer().params(), column);
4226 cur.idx() = tabular.cellIndex(row, column);
4229 case Tabular::M_TOGGLE_LINE_TOP:
4231 case Tabular::TOGGLE_LINE_TOP: {
4232 bool lineSet = !tabular.topLine(cur.idx(), flag);
4233 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4234 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4236 tabular.cellIndex(i, j),
4241 case Tabular::M_TOGGLE_LINE_BOTTOM:
4243 case Tabular::TOGGLE_LINE_BOTTOM: {
4244 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4246 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4247 tabular.setBottomLine(
4248 tabular.cellIndex(i, j),
4254 case Tabular::M_TOGGLE_LINE_LEFT:
4256 case Tabular::TOGGLE_LINE_LEFT: {
4257 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4258 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4259 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4260 tabular.setLeftLine(
4261 tabular.cellIndex(i,j),
4267 case Tabular::M_TOGGLE_LINE_RIGHT:
4269 case Tabular::TOGGLE_LINE_RIGHT: {
4270 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4271 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4272 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4273 tabular.setRightLine(
4274 tabular.cellIndex(i,j),
4280 case Tabular::M_ALIGN_LEFT:
4281 case Tabular::M_ALIGN_RIGHT:
4282 case Tabular::M_ALIGN_CENTER:
4284 case Tabular::ALIGN_LEFT:
4285 case Tabular::ALIGN_RIGHT:
4286 case Tabular::ALIGN_CENTER:
4287 case Tabular::ALIGN_BLOCK:
4288 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4289 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4290 tabular.setAlignment(
4291 tabular.cellIndex(i, j),
4296 case Tabular::M_VALIGN_TOP:
4297 case Tabular::M_VALIGN_BOTTOM:
4298 case Tabular::M_VALIGN_MIDDLE:
4300 case Tabular::VALIGN_TOP:
4301 case Tabular::VALIGN_BOTTOM:
4302 case Tabular::VALIGN_MIDDLE:
4303 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4304 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4305 tabular.setVAlignment(
4306 tabular.cellIndex(i, j),
4310 case Tabular::MULTICOLUMN: {
4311 if (sel_row_start != sel_row_end) {
4312 // FIXME: Need I say it ? This is horrible.
4314 Alert::error(_("Error setting multicolumn"),
4315 _("You cannot set multicolumn vertically."));
4318 if (!cur.selection()) {
4319 // just multicol for one single cell
4320 // check whether we are completely in a multicol
4321 if (tabular.isMultiColumn(cur.idx()))
4322 tabular.unsetMultiColumn(cur.idx());
4324 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4327 // we have a selection so this means we just add all this
4328 // cells to form a multicolumn cell
4329 idx_type const s_start = cur.selBegin().idx();
4330 idx_type const s_end = cur.selEnd().idx();
4331 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4332 cur.idx() = s_start;
4335 cur.selection() = false;
4339 case Tabular::SET_ALL_LINES:
4341 case Tabular::UNSET_ALL_LINES:
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.setAllLines(
4345 tabular.cellIndex(i,j), setLines);
4348 case Tabular::SET_LONGTABULAR:
4349 tabular.setLongTabular(true);
4352 case Tabular::UNSET_LONGTABULAR:
4353 tabular.setLongTabular(false);
4356 case Tabular::SET_ROTATE_TABULAR:
4357 tabular.setRotateTabular(true);
4360 case Tabular::UNSET_ROTATE_TABULAR:
4361 tabular.setRotateTabular(false);
4364 case Tabular::TOGGLE_ROTATE_TABULAR:
4365 tabular.setRotateTabular(!tabular.getRotateTabular());
4368 case Tabular::SET_ROTATE_CELL:
4369 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4370 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4371 tabular.setRotateCell(
4372 tabular.cellIndex(i, j), true);
4375 case Tabular::UNSET_ROTATE_CELL:
4376 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4377 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4378 tabular.setRotateCell(
4379 tabular.cellIndex(i, j), false);
4382 case Tabular::TOGGLE_ROTATE_CELL:
4384 bool oneNotRotated = oneCellHasRotationState(false,
4385 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4387 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4388 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4389 tabular.setRotateCell(tabular.cellIndex(i, j),
4394 case Tabular::SET_USEBOX: {
4395 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4396 if (val == tabular.getUsebox(cur.idx()))
4397 val = Tabular::BOX_NONE;
4398 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4399 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4400 tabular.setUsebox(tabular.cellIndex(i, j), val);
4404 case Tabular::UNSET_LTFIRSTHEAD:
4406 case Tabular::SET_LTFIRSTHEAD:
4407 tabular.getRowOfLTFirstHead(row, ltt);
4408 checkLongtableSpecial(ltt, value, flag);
4409 tabular.setLTHead(row, flag, ltt, true);
4412 case Tabular::UNSET_LTHEAD:
4414 case Tabular::SET_LTHEAD:
4415 tabular.getRowOfLTHead(row, ltt);
4416 checkLongtableSpecial(ltt, value, flag);
4417 tabular.setLTHead(row, flag, ltt, false);
4420 case Tabular::UNSET_LTFOOT:
4422 case Tabular::SET_LTFOOT:
4423 tabular.getRowOfLTFoot(row, ltt);
4424 checkLongtableSpecial(ltt, value, flag);
4425 tabular.setLTFoot(row, flag, ltt, false);
4428 case Tabular::UNSET_LTLASTFOOT:
4430 case Tabular::SET_LTLASTFOOT:
4431 tabular.getRowOfLTLastFoot(row, ltt);
4432 checkLongtableSpecial(ltt, value, flag);
4433 tabular.setLTFoot(row, flag, ltt, true);
4436 case Tabular::SET_LTNEWPAGE:
4437 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4440 case Tabular::SET_BOOKTABS:
4441 tabular.setBookTabs(true);
4444 case Tabular::UNSET_BOOKTABS:
4445 tabular.setBookTabs(false);
4448 case Tabular::SET_TOP_SPACE: {
4450 if (value == "default")
4451 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4452 tabular.row_info[i].top_space_default = true;
4453 else if (isValidLength(value, &len))
4454 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4455 tabular.row_info[i].top_space_default = false;
4456 tabular.row_info[i].top_space = len;
4459 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4460 tabular.row_info[i].top_space_default = false;
4461 tabular.row_info[i].top_space = len;
4466 case Tabular::SET_BOTTOM_SPACE: {
4468 if (value == "default")
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4470 tabular.row_info[i].bottom_space_default = true;
4471 else if (isValidLength(value, &len))
4472 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4473 tabular.row_info[i].bottom_space_default = false;
4474 tabular.row_info[i].bottom_space = len;
4477 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4478 tabular.row_info[i].bottom_space_default = false;
4479 tabular.row_info[i].bottom_space = len;
4484 case Tabular::SET_INTERLINE_SPACE: {
4486 if (value == "default")
4487 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4488 tabular.row_info[i].interline_space_default = true;
4489 else if (isValidLength(value, &len))
4490 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4491 tabular.row_info[i].interline_space_default = false;
4492 tabular.row_info[i].interline_space = len;
4495 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4496 tabular.row_info[i].interline_space_default = false;
4497 tabular.row_info[i].interline_space = len;
4502 // dummy stuff just to avoid warnings
4503 case Tabular::LAST_ACTION:
4509 bool InsetTabular::showInsetDialog(BufferView * bv) const
4511 InsetTabularMailer(*this).showDialog(bv);
4516 void InsetTabular::openLayoutDialog(BufferView * bv) const
4518 InsetTabularMailer(*this).showDialog(bv);
4522 bool InsetTabular::copySelection(Cursor & cur)
4524 if (!cur.selection())
4529 getSelection(cur, rs, re, cs, ce);
4531 paste_tabular.reset(new Tabular(tabular));
4533 for (row_type i = 0; i < rs; ++i)
4534 paste_tabular->deleteRow(0);
4536 row_type const rows = re - rs + 1;
4537 while (paste_tabular->rowCount() > rows)
4538 paste_tabular->deleteRow(rows);
4540 paste_tabular->setTopLine(0, true, true);
4541 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4544 for (col_type i = 0; i < cs; ++i)
4545 paste_tabular->deleteColumn(0);
4547 col_type const columns = ce - cs + 1;
4548 while (paste_tabular->columnCount() > columns)
4549 paste_tabular->deleteColumn(columns);
4551 paste_tabular->setLeftLine(0, true, true);
4552 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4555 odocstringstream os;
4556 OutputParams const runparams(0);
4557 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4558 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4559 cap::copySelection(cur, os.str());
4561 // mark tabular stack dirty
4562 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4563 // when we (hopefully) have a one-for-all paste mechanism.
4564 // This must be called after cap::copySelection.
4565 dirtyTabularStack(true);
4571 bool InsetTabular::pasteClipboard(Cursor & cur)
4575 col_type const actcol = tabular.cellColumn(cur.idx());
4576 row_type const actrow = tabular.cellRow(cur.idx());
4577 for (row_type r1 = 0, r2 = actrow;
4578 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4580 for (col_type c1 = 0, c2 = actcol;
4581 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4583 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4584 tabular.isPartOfMultiColumn(r2, c2))
4586 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4590 if (tabular.isPartOfMultiColumn(r2, c2)) {
4594 shared_ptr<InsetText> inset(
4595 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4596 tabular.setCellInset(r2, c2, inset);
4597 // FIXME: change tracking (MG)
4598 inset->setChange(Change(cur.buffer().params().trackChanges ?
4599 Change::INSERTED : Change::UNCHANGED));
4607 void InsetTabular::cutSelection(Cursor & cur)
4609 if (!cur.selection())
4614 getSelection(cur, rs, re, cs, ce);
4615 for (row_type i = rs; i <= re; ++i) {
4616 for (col_type j = cs; j <= ce; ++j) {
4617 shared_ptr<InsetText> t
4618 = cell(tabular.cellIndex(i, j));
4619 if (cur.buffer().params().trackChanges)
4620 // FIXME: Change tracking (MG)
4621 t->setChange(Change(Change::DELETED));
4627 // cursor position might be invalid now
4628 if (cur.pit() > cur.lastpit())
4629 cur.pit() = cur.lastpit();
4630 if (cur.pos() > cur.lastpos())
4631 cur.pos() = cur.lastpos();
4632 cur.clearSelection();
4636 bool InsetTabular::isRightToLeft(Cursor & cur) const
4638 BOOST_ASSERT(cur.depth() > 1);
4639 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4640 pos_type const parentpos = cur[cur.depth() - 2].pos();
4641 return parentpar.getFontSettings(cur.bv().buffer().params(),
4642 parentpos).language()->rightToLeft();
4646 void InsetTabular::getSelection(Cursor & cur,
4647 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4649 CursorSlice const & beg = cur.selBegin();
4650 CursorSlice const & end = cur.selEnd();
4651 cs = tabular.cellColumn(beg.idx());
4652 ce = tabular.cellColumn(end.idx());
4655 cs = tabular.cellColumn(end.idx());
4657 ce = tabular.cellRightColumn(end.idx());
4660 rs = tabular.cellRow(beg.idx());
4661 re = tabular.cellRow(end.idx());
4667 Text * InsetTabular::getText(int idx) const
4669 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4673 void InsetTabular::setChange(Change const & change)
4675 for (idx_type idx = 0; idx < nargs(); ++idx)
4676 cell(idx)->setChange(change);
4680 void InsetTabular::acceptChanges(BufferParams const & bparams)
4682 for (idx_type idx = 0; idx < nargs(); ++idx)
4683 cell(idx)->acceptChanges(bparams);
4687 void InsetTabular::rejectChanges(BufferParams const & bparams)
4689 for (idx_type idx = 0; idx < nargs(); ++idx)
4690 cell(idx)->rejectChanges(bparams);
4694 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4696 return tabular.getPWidth(cell).zero();
4700 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4703 if (buf.length() <= 0)
4706 Buffer const & buffer = bv.buffer();
4710 col_type maxCols = 1;
4711 docstring::size_type const len = buf.length();
4712 docstring::size_type p = 0;
4715 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4723 maxCols = max(cols, maxCols);
4729 maxCols = max(cols, maxCols);
4735 paste_tabular.reset(
4736 new Tabular(buffer.params(), rows, maxCols));
4737 loctab = paste_tabular.get();
4739 dirtyTabularStack(true);
4742 cell = bv.cursor().idx();
4743 ocol = tabular.cellColumn(cell);
4744 row = tabular.cellRow(cell);
4747 docstring::size_type op = 0;
4748 idx_type const cells = loctab->cellCount();
4751 rows = loctab->rowCount();
4752 col_type const columns = loctab->columnCount();
4754 while (cell < cells && p < len && row < rows &&
4755 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4761 // we can only set this if we are not too far right
4762 if (cols < columns) {
4763 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4764 Font const font = bv.textMetrics(&inset->text_).
4765 getDisplayFont(0, 0);
4766 inset->setText(buf.substr(op, p - op), font,
4767 buffer.params().trackChanges);
4773 // we can only set this if we are not too far right
4774 if (cols < columns) {
4775 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4776 Font const font = bv.textMetrics(&inset->text_).
4777 getDisplayFont(0, 0);
4778 inset->setText(buf.substr(op, p - op), font,
4779 buffer.params().trackChanges);
4784 cell = loctab->cellIndex(row, cols);
4790 // check for the last cell if there is no trailing '\n'
4791 if (cell < cells && op < len) {
4792 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4793 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4794 inset->setText(buf.substr(op, len - op), font,
4795 buffer.params().trackChanges);
4801 void InsetTabular::addPreview(PreviewLoader & loader) const
4803 row_type const rows = tabular.rowCount();
4804 col_type const columns = tabular.columnCount();
4805 for (row_type i = 0; i < rows; ++i) {
4806 for (col_type j = 0; j < columns; ++j)
4807 tabular.getCellInset(i, j)->addPreview(loader);
4812 bool InsetTabular::tablemode(Cursor & cur) const
4814 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4820 string const InsetTabularMailer::name_("tabular");
4822 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4823 : inset_(const_cast<InsetTabular &>(inset))
4827 string const InsetTabularMailer::inset2string(Buffer const &) const
4829 return params2string(inset_);
4833 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4835 istringstream data(in);
4837 lex.setStream(data);
4844 if (!lex || token != name_)
4845 return print_mailer_error("InsetTabularMailer", in, 1,
4848 // This is part of the inset proper that is usually swallowed
4849 // by Buffer::readInset
4851 if (!lex || token != "Tabular")
4852 return print_mailer_error("InsetTabularMailer", in, 2,
4855 Buffer const & buffer = inset.buffer();
4856 inset.read(buffer, lex);
4860 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4863 data << name_ << ' ';
4864 inset.write(inset.buffer(), data);
4865 data << "\\end_inset\n";