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, unsigned char 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)
2778 plaintextPrintCell(buf, os, runparams,
2779 cell, i, j, clen, onlydata);
2784 os << docstring(depth * 2, ' ');
2785 if (plaintextBottomHLine(os, i, clen))
2786 os << docstring(depth * 2, ' ');
2792 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2794 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2798 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2799 col_type column) const
2801 return cell_info[row][column].inset;
2805 void Tabular::setCellInset(row_type row, col_type column,
2806 shared_ptr<InsetText> ins) const
2808 cell_info[row][column].inset = ins;
2813 Tabular::getCellFromInset(Inset const * inset) const
2815 // is this inset part of the tabular?
2817 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2818 BOOST_ASSERT(false);
2821 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2822 if (getCellInset(cell).get() == inset) {
2823 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2824 << "cell=" << cell << endl;
2828 // We should have found a cell at this point
2829 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2830 << inset << " not found!" << endl;
2831 BOOST_ASSERT(false);
2837 void Tabular::validate(LaTeXFeatures & features) const
2839 features.require("NeedTabularnewline");
2841 features.require("booktabs");
2842 if (isLongTabular())
2843 features.require("longtable");
2845 features.require("rotating");
2846 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2847 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2848 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2849 features.require("array");
2850 getCellInset(cell)->validate(features);
2855 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2857 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2858 ParagraphList::const_iterator cit = parlist.begin();
2859 ParagraphList::const_iterator end = parlist.end();
2861 for (; cit != end; ++cit)
2862 for (int i = 0; i < cit->size(); ++i)
2863 if (cit->isNewline(i))
2870 /////////////////////////////////////////////////////////////////////
2874 /////////////////////////////////////////////////////////////////////
2876 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2878 : tabular(buf.params(), max(rows, row_type(1)),
2879 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2883 InsetTabular::InsetTabular(InsetTabular const & tab)
2884 : Inset(tab), tabular(tab.tabular),
2885 buffer_(tab.buffer_), scx_(0)
2889 InsetTabular::~InsetTabular()
2891 InsetTabularMailer(*this).hideDialog();
2895 Inset * InsetTabular::clone() const
2897 return new InsetTabular(*this);
2901 Buffer const & InsetTabular::buffer() const
2907 void InsetTabular::buffer(Buffer const * b)
2913 void InsetTabular::write(Buffer const & buf, ostream & os) const
2915 os << "Tabular" << endl;
2916 tabular.write(buf, os);
2920 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2922 bool const old_format = (lex.getString() == "\\LyXTable");
2924 tabular.read(buf, lex);
2930 string token = lex.getString();
2931 while (lex && token != "\\end_inset") {
2933 token = lex.getString();
2936 lex.printError("Missing \\end_inset at this point. "
2942 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2944 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2945 // mi.base.textwidth << "\n";
2947 lyxerr << "InsetTabular::metrics: need bv" << endl;
2948 BOOST_ASSERT(false);
2952 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2955 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2956 if (tabular.isPartOfMultiColumn(i, j))
2957 // Multicolumn cell, but not first one
2962 if (tabular.cell_info[i][j].multicolumn ==
2963 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2964 p_width = tabular.cellinfo_of_cell(cell).p_width;
2966 p_width = tabular.column_info[j].p_width;
2967 if (!p_width.zero())
2968 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2969 tabular.getCellInset(cell)->metrics(m, dim);
2970 if (!p_width.zero())
2971 dim.wid = m.base.textwidth;
2972 tabular.setCellWidth(cell, dim.wid);
2973 if (p_width.zero()) {
2974 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2975 // FIXME there must be a way to get rid of
2976 // the second metrics call
2977 tabular.getCellInset(cell)->metrics(m, dim);
2979 maxAsc = max(maxAsc, dim.asc);
2980 maxDesc = max(maxDesc, dim.des);
2983 int const top_space = tabular.row_info[i].top_space_default ?
2984 default_line_space :
2985 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2986 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2987 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2988 default_line_space :
2989 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2990 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2993 dim.asc = tabular.rowAscent(0);
2994 dim.des = tabular.height() - dim.asc;
2995 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2999 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3001 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3002 BufferView * bv = pi.base.bv;
3004 // FIXME: As the full backrgound is painted in drawSelection(),
3005 // we have no choice but to do a full repaint for the Text cells.
3006 pi.full_repaint = true;
3008 resetPos(bv->cursor());
3011 x += ADD_TO_TABULAR_WIDTH;
3014 first_visible_cell = Tabular::npos;
3015 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3017 int const a = tabular.rowAscent(i);
3018 int const d = tabular.rowDescent(i);
3019 idx = tabular.cellIndex(i, 0);
3020 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3021 if (tabular.isPartOfMultiColumn(i, j))
3023 if (first_visible_cell == Tabular::npos)
3024 first_visible_cell = idx;
3026 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3027 // Cache the Inset position.
3028 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3029 if (nx + tabular.columnWidth(idx) < 0
3030 || nx > bv->workWidth()
3032 || y - a > bv->workHeight()) {
3033 pi.pain.setDrawingEnabled(false);
3034 cell(idx)->draw(pi, cx, y);
3035 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3036 pi.pain.setDrawingEnabled(true);
3038 cell(idx)->draw(pi, cx, y);
3039 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3041 nx += tabular.columnWidth(idx);
3045 if (i + 1 < tabular.rowCount())
3046 y += d + tabular.rowAscent(i + 1) +
3047 tabular.getAdditionalHeight(i + 1);
3052 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3054 Cursor & cur = pi.base.bv->cursor();
3056 x += scx_ + ADD_TO_TABULAR_WIDTH;
3058 // FIXME: it is wrong to completely paint the background
3059 // if we want to do single row painting.
3061 // Paint background of current tabular
3062 int const w = tabular.width();
3063 int const h = tabular.height();
3064 int yy = y - tabular.rowAscent(0);
3065 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3067 if (!cur.selection())
3069 if (!ptr_cmp(&cur.inset(), this))
3075 if (tablemode(cur)) {
3078 getSelection(cur, rs, re, cs, ce);
3079 y -= tabular.rowAscent(0);
3080 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3081 int const a = tabular.rowAscent(j);
3082 int const h = a + tabular.rowDescent(j);
3084 y += tabular.getAdditionalHeight(j);
3085 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3086 if (tabular.isPartOfMultiColumn(j, i))
3088 idx_type const cell =
3089 tabular.cellIndex(j, i);
3090 int const w = tabular.columnWidth(cell);
3091 if (i >= cs && i <= ce && j >= rs && j <= re)
3092 pi.pain.fillRectangle(xx, y, w, h,
3100 x += getCellXPos(cur.idx());
3101 x += tabular.getBeginningOfTextInCell(cur.idx());
3102 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3107 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3108 row_type row, idx_type cell, bool erased) const
3110 int x2 = x + tabular.columnWidth(cell);
3111 bool on_off = false;
3112 ColorCode col = Color_tabularline;
3113 ColorCode onoffcol = Color_tabularonoffline;
3116 col = Color_deletedtext;
3117 onoffcol = Color_deletedtext;
3120 if (!tabular.topAlreadyDrawn(cell)) {
3121 on_off = !tabular.topLine(cell);
3122 pain.line(x, y - tabular.rowAscent(row),
3123 x2, y - tabular.rowAscent(row),
3124 on_off ? onoffcol : col,
3125 on_off ? Painter::line_onoffdash : Painter::line_solid);
3127 on_off = !tabular.bottomLine(cell);
3128 pain.line(x, y + tabular.rowDescent(row),
3129 x2, y + tabular.rowDescent(row),
3130 on_off ? onoffcol : col,
3131 on_off ? Painter::line_onoffdash : Painter::line_solid);
3132 if (!tabular.leftAlreadyDrawn(cell)) {
3133 on_off = !tabular.leftLine(cell);
3134 pain.line(x, y - tabular.rowAscent(row),
3135 x, y + tabular.rowDescent(row),
3136 on_off ? onoffcol : col,
3137 on_off ? Painter::line_onoffdash : Painter::line_solid);
3139 on_off = !tabular.rightLine(cell);
3140 pain.line(x2 - tabular.getAdditionalWidth(cell),
3141 y - tabular.rowAscent(row),
3142 x2 - tabular.getAdditionalWidth(cell),
3143 y + tabular.rowDescent(row),
3144 on_off ? onoffcol : col,
3145 on_off ? Painter::line_onoffdash : Painter::line_solid);
3149 docstring const InsetTabular::editMessage() const
3151 return _("Opened table");
3155 void InsetTabular::edit(Cursor & cur, bool left)
3157 //lyxerr << "InsetTabular::edit: " << this << endl;
3159 cur.selection() = false;
3162 if (isRightToLeft(cur))
3163 cur.idx() = tabular.getLastCellInRow(0);
3169 if (isRightToLeft(cur))
3170 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3172 cur.idx() = tabular.cellCount() - 1;
3174 cur.pos() = cur.lastpos(); // FIXME crude guess
3176 // FIXME: this accesses the position cache before it is initialized
3178 //cur.bv().fitCursor();
3182 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3184 // In a longtable, tell captions what the current float is
3185 Counters & cnts = buf.params().getTextClass().counters();
3186 string const saveflt = cnts.current_float();
3187 if (tabular.isLongTabular())
3188 cnts.current_float("table");
3190 ParIterator it2 = it;
3192 size_t const end = it2.nargs();
3193 for ( ; it2.idx() < end; it2.top().forwardIdx())
3194 lyx::updateLabels(buf, it2);
3197 if (tabular.isLongTabular())
3198 cnts.current_float(saveflt);
3202 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3204 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3205 << "\n cur:" << cur << endl;
3206 CursorSlice sl = cur.top();
3207 Cursor & bvcur = cur.bv().cursor();
3209 switch (cmd.action) {
3211 case LFUN_MOUSE_PRESS:
3212 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3214 // do not reset cursor/selection if we have selected
3215 // some cells (bug 2715).
3216 if (cmd.button() == mouse_button::button3
3217 && &bvcur.selBegin().inset() == this
3218 && tablemode(bvcur))
3221 // Let InsetText do it
3222 cell(cur.idx())->dispatch(cur, cmd);
3224 case LFUN_MOUSE_MOTION:
3225 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3226 if (cmd.button() == mouse_button::button1) {
3227 // only accept motions to places not deeper nested than the real anchor
3228 if (bvcur.anchor_.hasPart(cur)) {
3229 // only update if selection changes
3230 if (bvcur.idx() == cur.idx() &&
3231 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3233 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3234 bvcur.setCursor(cur);
3235 bvcur.selection() = true;
3241 case LFUN_MOUSE_RELEASE:
3242 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3243 if (cmd.button() == mouse_button::button3)
3244 InsetTabularMailer(*this).showDialog(&cur.bv());
3247 case LFUN_CELL_BACKWARD:
3249 cur.selection() = false;
3252 case LFUN_CELL_FORWARD:
3254 cur.selection() = false;
3257 case LFUN_CHAR_FORWARD_SELECT:
3258 case LFUN_CHAR_FORWARD:
3259 cell(cur.idx())->dispatch(cur, cmd);
3260 if (!cur.result().dispatched()) {
3262 if (sl == cur.top())
3263 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3269 case LFUN_CHAR_BACKWARD_SELECT:
3270 case LFUN_CHAR_BACKWARD:
3271 cell(cur.idx())->dispatch(cur, cmd);
3272 if (!cur.result().dispatched()) {
3274 if (sl == cur.top())
3275 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3281 case LFUN_CHAR_RIGHT_SELECT:
3282 case LFUN_CHAR_RIGHT:
3283 //FIXME: for visual cursor, really move right
3284 if (isRightToLeft(cur))
3285 lyx::dispatch(FuncRequest(
3286 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3287 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3289 lyx::dispatch(FuncRequest(
3290 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3291 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3294 case LFUN_CHAR_LEFT_SELECT:
3295 case LFUN_CHAR_LEFT:
3296 //FIXME: for visual cursor, really move left
3297 if (isRightToLeft(cur))
3298 lyx::dispatch(FuncRequest(
3299 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3300 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3302 lyx::dispatch(FuncRequest(
3303 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3304 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3307 case LFUN_DOWN_SELECT:
3309 cell(cur.idx())->dispatch(cur, cmd);
3310 cur.dispatched(); // override the cell's decision
3311 if (sl == cur.top())
3312 // if our Text didn't do anything to the cursor
3313 // then we try to put the cursor into the cell below
3314 // setting also the right targetX.
3315 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3316 cur.idx() = tabular.getCellBelow(cur.idx());
3318 TextMetrics const & tm =
3319 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3320 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3322 if (sl == cur.top()) {
3323 // we trick it to go to forward after leaving the
3325 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3330 case LFUN_UP_SELECT:
3332 cell(cur.idx())->dispatch(cur, cmd);
3333 cur.dispatched(); // override the cell's decision
3334 if (sl == cur.top())
3335 // if our Text didn't do anything to the cursor
3336 // then we try to put the cursor into the cell above
3337 // setting also the right targetX.
3338 if (tabular.cellRow(cur.idx()) != 0) {
3339 cur.idx() = tabular.getCellAbove(cur.idx());
3340 cur.pit() = cur.lastpit();
3341 Text const * text = cell(cur.idx())->getText(0);
3342 TextMetrics const & tm = cur.bv().textMetrics(text);
3343 ParagraphMetrics const & pm =
3344 tm.parMetrics(cur.lastpit());
3345 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3347 if (sl == cur.top()) {
3348 cmd = FuncRequest(LFUN_UP);
3353 // case LFUN_SCREEN_DOWN: {
3354 // //if (hasSelection())
3355 // // cur.selection() = false;
3356 // col_type const col = tabular.cellColumn(cur.idx());
3357 // int const t = cur.bv().top_y() + cur.bv().height();
3358 // if (t < yo() + tabular.getHeightOfTabular()) {
3359 // cur.bv().scrollDocView(t);
3360 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3362 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3369 // case LFUN_SCREEN_UP: {
3370 // //if (hasSelection())
3371 // // cur.selection() = false;
3372 // col_type const col = tabular.cellColumn(cur.idx());
3373 // int const t = cur.bv().top_y() + cur.bv().height();
3375 // cur.bv().scrollDocView(t);
3379 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3383 // cur.par() = cur.lastpar();
3384 // cur.pos() = cur.lastpos();
3388 case LFUN_LAYOUT_TABULAR:
3389 InsetTabularMailer(*this).showDialog(&cur.bv());
3392 case LFUN_INSET_DIALOG_UPDATE:
3393 InsetTabularMailer(*this).updateDialog(&cur.bv());
3396 case LFUN_TABULAR_FEATURE:
3397 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3401 // insert file functions
3402 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3403 case LFUN_FILE_INSERT_PLAINTEXT: {
3405 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3406 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3412 if (tablemode(cur)) {
3413 if (copySelection(cur)) {
3414 cur.recordUndoInset(DELETE_UNDO);
3419 cell(cur.idx())->dispatch(cur, cmd);
3422 case LFUN_CHAR_DELETE_BACKWARD:
3423 case LFUN_CHAR_DELETE_FORWARD:
3424 if (tablemode(cur)) {
3425 cur.recordUndoInset(DELETE_UNDO);
3429 cell(cur.idx())->dispatch(cur, cmd);
3433 if (!cur.selection())
3435 if (tablemode(cur)) {
3439 cell(cur.idx())->dispatch(cur, cmd);
3442 case LFUN_CLIPBOARD_PASTE:
3443 case LFUN_PRIMARY_SELECTION_PASTE: {
3444 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3445 theClipboard().getAsText() :
3446 theSelection().get();
3449 // pass to InsertPlaintextString, but
3450 // only if we have multi-cell content
3451 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3452 if (insertPlaintextString(cur.bv(), clip, false)) {
3453 // content has been replaced,
3454 // so cursor might be invalid
3455 cur.pos() = cur.lastpos();
3456 bvcur.setCursor(cur);
3460 // Let the cell handle normal text
3461 cell(cur.idx())->dispatch(cur, cmd);
3466 if (tabularStackDirty() && theClipboard().isInternal()) {
3467 cur.recordUndoInset(INSERT_UNDO);
3468 pasteClipboard(cur);
3471 cell(cur.idx())->dispatch(cur, cmd);
3474 case LFUN_FONT_EMPH:
3475 case LFUN_FONT_BOLD:
3476 case LFUN_FONT_ROMAN:
3477 case LFUN_FONT_NOUN:
3478 case LFUN_FONT_ITAL:
3479 case LFUN_FONT_FRAK:
3480 case LFUN_FONT_TYPEWRITER:
3481 case LFUN_FONT_SANS:
3482 case LFUN_FONT_FREE_APPLY:
3483 case LFUN_FONT_FREE_UPDATE:
3484 case LFUN_FONT_SIZE:
3485 case LFUN_FONT_UNDERLINE:
3487 case LFUN_WORD_CAPITALIZE:
3488 case LFUN_WORD_UPCASE:
3489 case LFUN_WORD_LOWCASE:
3490 case LFUN_CHARS_TRANSPOSE:
3491 if (tablemode(cur)) {
3494 getSelection(cur, rs, re, cs, ce);
3495 Cursor tmpcur = cur;
3496 for (row_type i = rs; i <= re; ++i) {
3497 for (col_type j = cs; j <= ce; ++j) {
3498 // cursor follows cell:
3499 tmpcur.idx() = tabular.cellIndex(i, j);
3500 // select this cell only:
3503 tmpcur.resetAnchor();
3504 tmpcur.pit() = tmpcur.lastpit();
3505 tmpcur.pos() = tmpcur.top().lastpos();
3506 tmpcur.setCursor(tmpcur);
3507 tmpcur.setSelection();
3508 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3513 cell(cur.idx())->dispatch(cur, cmd);
3517 // we try to handle this event in the insets dispatch function.
3518 cell(cur.idx())->dispatch(cur, cmd);
3524 // function sets an object as defined in func_status.h:
3525 // states OK, Unknown, Disabled, On, Off.
3526 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3527 FuncStatus & status) const
3529 switch (cmd.action) {
3530 case LFUN_TABULAR_FEATURE: {
3531 int action = Tabular::LAST_ACTION;
3533 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3534 string const tmp = tabularFeature[i].feature;
3535 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3536 action = tabularFeature[i].action;
3540 if (action == Tabular::LAST_ACTION) {
3542 status.unknown(true);
3546 string const argument
3547 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3549 row_type sel_row_start = 0;
3550 row_type sel_row_end = 0;
3551 col_type sel_col_start = 0;
3552 col_type sel_col_end = 0;
3553 Tabular::ltType dummyltt;
3556 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3559 case Tabular::SET_PWIDTH:
3560 case Tabular::SET_MPWIDTH:
3561 case Tabular::SET_SPECIAL_COLUMN:
3562 case Tabular::SET_SPECIAL_MULTI:
3563 case Tabular::APPEND_ROW:
3564 case Tabular::APPEND_COLUMN:
3565 case Tabular::DELETE_ROW:
3566 case Tabular::DELETE_COLUMN:
3567 case Tabular::COPY_ROW:
3568 case Tabular::COPY_COLUMN:
3569 case Tabular::SET_ALL_LINES:
3570 case Tabular::UNSET_ALL_LINES:
3571 case Tabular::SET_TOP_SPACE:
3572 case Tabular::SET_BOTTOM_SPACE:
3573 case Tabular::SET_INTERLINE_SPACE:
3577 case Tabular::MULTICOLUMN:
3578 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3581 case Tabular::M_TOGGLE_LINE_TOP:
3583 case Tabular::TOGGLE_LINE_TOP:
3584 status.setOnOff(tabular.topLine(cur.idx(), flag));
3587 case Tabular::M_TOGGLE_LINE_BOTTOM:
3589 case Tabular::TOGGLE_LINE_BOTTOM:
3590 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3593 case Tabular::M_TOGGLE_LINE_LEFT:
3595 case Tabular::TOGGLE_LINE_LEFT:
3596 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3599 case Tabular::M_TOGGLE_LINE_RIGHT:
3601 case Tabular::TOGGLE_LINE_RIGHT:
3602 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3605 case Tabular::M_ALIGN_LEFT:
3607 case Tabular::ALIGN_LEFT:
3608 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3611 case Tabular::M_ALIGN_RIGHT:
3613 case Tabular::ALIGN_RIGHT:
3614 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3617 case Tabular::M_ALIGN_CENTER:
3619 case Tabular::ALIGN_CENTER:
3620 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3623 case Tabular::ALIGN_BLOCK:
3624 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3625 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3628 case Tabular::M_VALIGN_TOP:
3630 case Tabular::VALIGN_TOP:
3632 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3635 case Tabular::M_VALIGN_BOTTOM:
3637 case Tabular::VALIGN_BOTTOM:
3639 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3642 case Tabular::M_VALIGN_MIDDLE:
3644 case Tabular::VALIGN_MIDDLE:
3646 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3649 case Tabular::SET_LONGTABULAR:
3650 status.setOnOff(tabular.isLongTabular());
3653 case Tabular::UNSET_LONGTABULAR:
3654 status.setOnOff(!tabular.isLongTabular());
3657 case Tabular::TOGGLE_ROTATE_TABULAR:
3658 case Tabular::SET_ROTATE_TABULAR:
3659 status.setOnOff(tabular.getRotateTabular());
3662 case Tabular::UNSET_ROTATE_TABULAR:
3663 status.setOnOff(!tabular.getRotateTabular());
3666 case Tabular::TOGGLE_ROTATE_CELL:
3667 case Tabular::SET_ROTATE_CELL:
3668 status.setOnOff(!oneCellHasRotationState(false,
3669 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3672 case Tabular::UNSET_ROTATE_CELL:
3673 status.setOnOff(!oneCellHasRotationState(true,
3674 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3677 case Tabular::SET_USEBOX:
3678 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3681 case Tabular::SET_LTFIRSTHEAD:
3682 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3685 case Tabular::UNSET_LTFIRSTHEAD:
3686 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3689 case Tabular::SET_LTHEAD:
3690 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3693 case Tabular::UNSET_LTHEAD:
3694 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3697 case Tabular::SET_LTFOOT:
3698 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3701 case Tabular::UNSET_LTFOOT:
3702 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3705 case Tabular::SET_LTLASTFOOT:
3706 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3709 case Tabular::UNSET_LTLASTFOOT:
3710 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3713 case Tabular::SET_LTNEWPAGE:
3714 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3717 case Tabular::SET_BOOKTABS:
3718 status.setOnOff(tabular.useBookTabs());
3721 case Tabular::UNSET_BOOKTABS:
3722 status.setOnOff(!tabular.useBookTabs());
3727 status.enabled(false);
3733 // These are only enabled inside tabular
3734 case LFUN_CELL_BACKWARD:
3735 case LFUN_CELL_FORWARD:
3736 status.enabled(true);
3739 // disable these with multiple cells selected
3740 case LFUN_INSET_INSERT:
3741 case LFUN_TABULAR_INSERT:
3742 case LFUN_FLEX_INSERT:
3743 case LFUN_FLOAT_INSERT:
3744 case LFUN_FLOAT_WIDE_INSERT:
3745 case LFUN_FOOTNOTE_INSERT:
3746 case LFUN_MARGINALNOTE_INSERT:
3747 case LFUN_MATH_INSERT:
3748 case LFUN_MATH_MODE:
3749 case LFUN_MATH_MUTATE:
3750 case LFUN_MATH_DISPLAY:
3751 case LFUN_NOTE_INSERT:
3752 case LFUN_OPTIONAL_INSERT:
3753 case LFUN_BOX_INSERT:
3754 case LFUN_BRANCH_INSERT:
3755 case LFUN_WRAP_INSERT:
3756 case LFUN_ERT_INSERT: {
3757 if (tablemode(cur)) {
3758 status.enabled(false);
3761 return cell(cur.idx())->getStatus(cur, cmd, status);
3764 // disable in non-fixed-width cells
3765 case LFUN_BREAK_LINE:
3766 case LFUN_BREAK_PARAGRAPH:
3767 case LFUN_BREAK_PARAGRAPH_SKIP: {
3768 if (tabular.getPWidth(cur.idx()).zero()) {
3769 status.enabled(false);
3772 return cell(cur.idx())->getStatus(cur, cmd, status);
3776 if (tabularStackDirty() && theClipboard().isInternal()) {
3777 status.enabled(true);
3780 return cell(cur.idx())->getStatus(cur, cmd, status);
3782 case LFUN_INSET_MODIFY:
3783 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3784 status.enabled(true);
3790 // we try to handle this event in the insets dispatch function.
3791 return cell(cur.idx())->getStatus(cur, cmd, status);
3796 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3797 OutputParams const & runparams) const
3799 return tabular.latex(buf, os, runparams);
3803 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3804 OutputParams const & runparams) const
3806 os << '\n'; // output table on a new line
3807 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3808 tabular.plaintext(buf, os, runparams, dp, false, 0);
3809 return PLAINTEXT_NEWLINE;
3813 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3814 OutputParams const & runparams) const
3819 // FIXME: Why not pass a proper DocIterator here?
3821 // if the table is inside a float it doesn't need the informaltable
3822 // wrapper. Search for it.
3823 for (master = owner(); master; master = master->owner())
3824 if (master->lyxCode() == FLOAT_CODE)
3829 os << "<informaltable>";
3832 ret += tabular.docbook(buf, os, runparams);
3834 os << "</informaltable>";
3841 void InsetTabular::validate(LaTeXFeatures & features) const
3843 tabular.validate(features);
3847 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3849 return tabular.getCellInset(idx);
3853 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3855 return tabular.getCellInset(idx);
3859 void InsetTabular::cursorPos(BufferView const & bv,
3860 CursorSlice const & sl, bool boundary, int & x, int & y) const
3862 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3864 // y offset correction
3865 int const row = tabular.cellRow(sl.idx());
3866 for (int i = 0; i <= row; ++i) {
3868 y += tabular.rowAscent(i);
3869 y += tabular.getAdditionalHeight(i);
3872 y += tabular.rowDescent(i);
3875 // x offset correction
3876 int const col = tabular.cellColumn(sl.idx());
3877 int idx = tabular.cellIndex(row, 0);
3878 for (int j = 0; j < col; ++j) {
3879 if (tabular.isPartOfMultiColumn(row, j))
3881 x += tabular.columnWidth(idx);
3884 x += tabular.getBeginningOfTextInCell(idx);
3885 x += ADD_TO_TABULAR_WIDTH;
3890 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3894 Inset const & inset = *tabular.getCellInset(cell);
3895 Point o = bv.coordCache().getInsets().xy(&inset);
3896 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3897 int const xend = xbeg + tabular.columnWidth(cell);
3898 row_type const row = tabular.cellRow(cell);
3899 int const ybeg = o.y_ - tabular.rowAscent(row) -
3900 tabular.getAdditionalHeight(row);
3901 int const yend = o.y_ + tabular.rowDescent(row);
3913 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3914 // << " ybeg=" << ybeg << " yend=" << yend
3915 // << " xx=" << xx << " yy=" << yy
3916 // << " dist=" << xx + yy << endl;
3921 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3923 //lyxerr << "InsetTabular::editXY: " << this << endl;
3924 cur.selection() = false;
3926 cur.idx() = getNearestCell(cur.bv(), x, y);
3928 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3932 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3934 cur.idx() = getNearestCell(cur.bv(), x, y);
3935 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3939 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3941 idx_type idx_min = 0;
3942 int dist_min = std::numeric_limits<int>::max();
3943 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3944 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3945 int const d = dist(bv, i, x, y);
3956 int InsetTabular::getCellXPos(idx_type const cell) const
3960 for (; !tabular.isFirstCellInRow(c); --c)
3963 for (; c < cell; ++c)
3964 lx += tabular.columnWidth(c);
3970 void InsetTabular::resetPos(Cursor & cur) const
3972 BufferView & bv = cur.bv();
3973 int const maxwidth = bv.workWidth();
3975 if (&cur.inset() != this) {
3979 int const X2 = maxwidth;
3980 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3981 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3982 int const x2 = x1 + tabular.columnWidth(cur.idx());
3985 scx_ = X1 + 20 - x1;
3987 scx_ = X2 - 20 - x2;
3992 cur.updateFlags(Update::Force | Update::FitCursor);
3996 void InsetTabular::moveNextCell(Cursor & cur)
3998 if (isRightToLeft(cur)) {
3999 if (tabular.isFirstCellInRow(cur.idx())) {
4000 row_type const row = tabular.cellRow(cur.idx());
4001 if (row == tabular.rowCount() - 1)
4003 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4010 if (tabular.isLastCell(cur.idx()))
4020 void InsetTabular::movePrevCell(Cursor & cur)
4022 if (isRightToLeft(cur)) {
4023 if (tabular.isLastCellInRow(cur.idx())) {
4024 row_type const row = tabular.cellRow(cur.idx());
4027 cur.idx() = tabular.getFirstCellInRow(row);
4028 cur.idx() = tabular.getCellAbove(cur.idx());
4030 if (tabular.isLastCell(cur.idx()))
4035 if (cur.idx() == 0) // first cell
4039 cur.pit() = cur.lastpit();
4040 cur.pos() = cur.lastpos();
4042 // FIXME: this accesses the position cache before it is initialized
4047 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4049 Tabular::Feature action = Tabular::LAST_ACTION;
4052 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4053 string const tmp = tabularFeature[i].feature;
4055 if (tmp == what.substr(0, tmp.length())) {
4056 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4057 //tabularFeatures[i].feature.length()))
4058 action = tabularFeature[i].action;
4062 if (action == Tabular::LAST_ACTION)
4066 ltrim(what.substr(tabularFeature[i].feature.length()));
4067 tabularFeatures(cur, action, val);
4072 static void checkLongtableSpecial(Tabular::ltType & ltt,
4073 string const & special, bool & flag)
4075 if (special == "dl_above") {
4078 } else if (special == "dl_below") {
4079 ltt.bottomDL = flag;
4081 } else if (special == "empty") {
4090 bool InsetTabular::oneCellHasRotationState(bool rotated,
4091 row_type row_start, row_type row_end,
4092 col_type col_start, col_type col_end) const {
4094 for (row_type i = row_start; i <= row_end; ++i) {
4095 for (col_type j = col_start; j <= col_end; ++j) {
4096 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4105 void InsetTabular::tabularFeatures(Cursor & cur,
4106 Tabular::Feature feature, string const & value)
4108 BufferView & bv = cur.bv();
4109 col_type sel_col_start;
4110 col_type sel_col_end;
4111 row_type sel_row_start;
4112 row_type sel_row_end;
4113 bool setLines = false;
4114 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4115 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4119 case Tabular::M_ALIGN_LEFT:
4120 case Tabular::ALIGN_LEFT:
4121 setAlign = LYX_ALIGN_LEFT;
4124 case Tabular::M_ALIGN_RIGHT:
4125 case Tabular::ALIGN_RIGHT:
4126 setAlign = LYX_ALIGN_RIGHT;
4129 case Tabular::M_ALIGN_CENTER:
4130 case Tabular::ALIGN_CENTER:
4131 setAlign = LYX_ALIGN_CENTER;
4134 case Tabular::ALIGN_BLOCK:
4135 setAlign = LYX_ALIGN_BLOCK;
4138 case Tabular::M_VALIGN_TOP:
4139 case Tabular::VALIGN_TOP:
4140 setVAlign = Tabular::LYX_VALIGN_TOP;
4143 case Tabular::M_VALIGN_BOTTOM:
4144 case Tabular::VALIGN_BOTTOM:
4145 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4148 case Tabular::M_VALIGN_MIDDLE:
4149 case Tabular::VALIGN_MIDDLE:
4150 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4157 cur.recordUndoInset(ATOMIC_UNDO);
4159 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4160 row_type const row = tabular.cellRow(cur.idx());
4161 col_type const column = tabular.cellColumn(cur.idx());
4163 Tabular::ltType ltt;
4167 case Tabular::SET_PWIDTH: {
4168 Length const len(value);
4169 tabular.setColumnPWidth(cur, cur.idx(), len);
4171 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4172 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4176 case Tabular::SET_MPWIDTH:
4177 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4180 case Tabular::SET_SPECIAL_COLUMN:
4181 case Tabular::SET_SPECIAL_MULTI:
4182 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4185 case Tabular::APPEND_ROW:
4186 // append the row into the tabular
4187 tabular.appendRow(bv.buffer().params(), cur.idx());
4190 case Tabular::APPEND_COLUMN:
4191 // append the column into the tabular
4192 tabular.appendColumn(bv.buffer().params(), cur.idx());
4193 cur.idx() = tabular.cellIndex(row, column);
4196 case Tabular::DELETE_ROW:
4197 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4198 tabular.deleteRow(sel_row_start);
4199 if (sel_row_start >= tabular.rowCount())
4201 cur.idx() = tabular.cellIndex(sel_row_start, column);
4204 cur.selection() = false;
4207 case Tabular::DELETE_COLUMN:
4208 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4209 tabular.deleteColumn(sel_col_start);
4210 if (sel_col_start >= tabular.columnCount())
4212 cur.idx() = tabular.cellIndex(row, sel_col_start);
4215 cur.selection() = false;
4218 case Tabular::COPY_ROW:
4219 tabular.copyRow(bv.buffer().params(), row);
4222 case Tabular::COPY_COLUMN:
4223 tabular.copyColumn(bv.buffer().params(), column);
4224 cur.idx() = tabular.cellIndex(row, column);
4227 case Tabular::M_TOGGLE_LINE_TOP:
4229 case Tabular::TOGGLE_LINE_TOP: {
4230 bool lineSet = !tabular.topLine(cur.idx(), flag);
4231 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4232 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4234 tabular.cellIndex(i, j),
4239 case Tabular::M_TOGGLE_LINE_BOTTOM:
4241 case Tabular::TOGGLE_LINE_BOTTOM: {
4242 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4243 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4244 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4245 tabular.setBottomLine(
4246 tabular.cellIndex(i, j),
4252 case Tabular::M_TOGGLE_LINE_LEFT:
4254 case Tabular::TOGGLE_LINE_LEFT: {
4255 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4256 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4257 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4258 tabular.setLeftLine(
4259 tabular.cellIndex(i,j),
4265 case Tabular::M_TOGGLE_LINE_RIGHT:
4267 case Tabular::TOGGLE_LINE_RIGHT: {
4268 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4269 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4270 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4271 tabular.setRightLine(
4272 tabular.cellIndex(i,j),
4278 case Tabular::M_ALIGN_LEFT:
4279 case Tabular::M_ALIGN_RIGHT:
4280 case Tabular::M_ALIGN_CENTER:
4282 case Tabular::ALIGN_LEFT:
4283 case Tabular::ALIGN_RIGHT:
4284 case Tabular::ALIGN_CENTER:
4285 case Tabular::ALIGN_BLOCK:
4286 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4287 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4288 tabular.setAlignment(
4289 tabular.cellIndex(i, j),
4294 case Tabular::M_VALIGN_TOP:
4295 case Tabular::M_VALIGN_BOTTOM:
4296 case Tabular::M_VALIGN_MIDDLE:
4298 case Tabular::VALIGN_TOP:
4299 case Tabular::VALIGN_BOTTOM:
4300 case Tabular::VALIGN_MIDDLE:
4301 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4302 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4303 tabular.setVAlignment(
4304 tabular.cellIndex(i, j),
4308 case Tabular::MULTICOLUMN: {
4309 if (sel_row_start != sel_row_end) {
4310 // FIXME: Need I say it ? This is horrible.
4312 Alert::error(_("Error setting multicolumn"),
4313 _("You cannot set multicolumn vertically."));
4316 if (!cur.selection()) {
4317 // just multicol for one single cell
4318 // check whether we are completely in a multicol
4319 if (tabular.isMultiColumn(cur.idx()))
4320 tabular.unsetMultiColumn(cur.idx());
4322 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4325 // we have a selection so this means we just add all this
4326 // cells to form a multicolumn cell
4327 idx_type const s_start = cur.selBegin().idx();
4328 idx_type const s_end = cur.selEnd().idx();
4329 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4330 cur.idx() = s_start;
4333 cur.selection() = false;
4337 case Tabular::SET_ALL_LINES:
4339 case Tabular::UNSET_ALL_LINES:
4340 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4341 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4342 tabular.setAllLines(
4343 tabular.cellIndex(i,j), setLines);
4346 case Tabular::SET_LONGTABULAR:
4347 tabular.setLongTabular(true);
4350 case Tabular::UNSET_LONGTABULAR:
4351 tabular.setLongTabular(false);
4354 case Tabular::SET_ROTATE_TABULAR:
4355 tabular.setRotateTabular(true);
4358 case Tabular::UNSET_ROTATE_TABULAR:
4359 tabular.setRotateTabular(false);
4362 case Tabular::TOGGLE_ROTATE_TABULAR:
4363 tabular.setRotateTabular(!tabular.getRotateTabular());
4366 case Tabular::SET_ROTATE_CELL:
4367 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4368 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4369 tabular.setRotateCell(
4370 tabular.cellIndex(i, j), true);
4373 case Tabular::UNSET_ROTATE_CELL:
4374 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4375 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4376 tabular.setRotateCell(
4377 tabular.cellIndex(i, j), false);
4380 case Tabular::TOGGLE_ROTATE_CELL:
4382 bool oneNotRotated = oneCellHasRotationState(false,
4383 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4385 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4386 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4387 tabular.setRotateCell(tabular.cellIndex(i, j),
4392 case Tabular::SET_USEBOX: {
4393 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4394 if (val == tabular.getUsebox(cur.idx()))
4395 val = Tabular::BOX_NONE;
4396 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4397 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4398 tabular.setUsebox(tabular.cellIndex(i, j), val);
4402 case Tabular::UNSET_LTFIRSTHEAD:
4404 case Tabular::SET_LTFIRSTHEAD:
4405 tabular.getRowOfLTFirstHead(row, ltt);
4406 checkLongtableSpecial(ltt, value, flag);
4407 tabular.setLTHead(row, flag, ltt, true);
4410 case Tabular::UNSET_LTHEAD:
4412 case Tabular::SET_LTHEAD:
4413 tabular.getRowOfLTHead(row, ltt);
4414 checkLongtableSpecial(ltt, value, flag);
4415 tabular.setLTHead(row, flag, ltt, false);
4418 case Tabular::UNSET_LTFOOT:
4420 case Tabular::SET_LTFOOT:
4421 tabular.getRowOfLTFoot(row, ltt);
4422 checkLongtableSpecial(ltt, value, flag);
4423 tabular.setLTFoot(row, flag, ltt, false);
4426 case Tabular::UNSET_LTLASTFOOT:
4428 case Tabular::SET_LTLASTFOOT:
4429 tabular.getRowOfLTLastFoot(row, ltt);
4430 checkLongtableSpecial(ltt, value, flag);
4431 tabular.setLTFoot(row, flag, ltt, true);
4434 case Tabular::SET_LTNEWPAGE:
4435 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4438 case Tabular::SET_BOOKTABS:
4439 tabular.setBookTabs(true);
4442 case Tabular::UNSET_BOOKTABS:
4443 tabular.setBookTabs(false);
4446 case Tabular::SET_TOP_SPACE: {
4448 if (value == "default")
4449 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4450 tabular.row_info[i].top_space_default = true;
4451 else if (isValidLength(value, &len))
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4453 tabular.row_info[i].top_space_default = false;
4454 tabular.row_info[i].top_space = len;
4457 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4458 tabular.row_info[i].top_space_default = false;
4459 tabular.row_info[i].top_space = len;
4464 case Tabular::SET_BOTTOM_SPACE: {
4466 if (value == "default")
4467 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4468 tabular.row_info[i].bottom_space_default = true;
4469 else if (isValidLength(value, &len))
4470 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4471 tabular.row_info[i].bottom_space_default = false;
4472 tabular.row_info[i].bottom_space = len;
4475 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4476 tabular.row_info[i].bottom_space_default = false;
4477 tabular.row_info[i].bottom_space = len;
4482 case Tabular::SET_INTERLINE_SPACE: {
4484 if (value == "default")
4485 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4486 tabular.row_info[i].interline_space_default = true;
4487 else if (isValidLength(value, &len))
4488 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4489 tabular.row_info[i].interline_space_default = false;
4490 tabular.row_info[i].interline_space = len;
4493 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4494 tabular.row_info[i].interline_space_default = false;
4495 tabular.row_info[i].interline_space = len;
4500 // dummy stuff just to avoid warnings
4501 case Tabular::LAST_ACTION:
4507 bool InsetTabular::showInsetDialog(BufferView * bv) const
4509 InsetTabularMailer(*this).showDialog(bv);
4514 void InsetTabular::openLayoutDialog(BufferView * bv) const
4516 InsetTabularMailer(*this).showDialog(bv);
4520 bool InsetTabular::copySelection(Cursor & cur)
4522 if (!cur.selection())
4527 getSelection(cur, rs, re, cs, ce);
4529 paste_tabular.reset(new Tabular(tabular));
4531 for (row_type i = 0; i < rs; ++i)
4532 paste_tabular->deleteRow(0);
4534 row_type const rows = re - rs + 1;
4535 while (paste_tabular->rowCount() > rows)
4536 paste_tabular->deleteRow(rows);
4538 paste_tabular->setTopLine(0, true, true);
4539 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4542 for (col_type i = 0; i < cs; ++i)
4543 paste_tabular->deleteColumn(0);
4545 col_type const columns = ce - cs + 1;
4546 while (paste_tabular->columnCount() > columns)
4547 paste_tabular->deleteColumn(columns);
4549 paste_tabular->setLeftLine(0, true, true);
4550 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4553 odocstringstream os;
4554 OutputParams const runparams(0);
4555 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4556 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4557 cap::copySelection(cur, os.str());
4559 // mark tabular stack dirty
4560 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4561 // when we (hopefully) have a one-for-all paste mechanism.
4562 // This must be called after cap::copySelection.
4563 dirtyTabularStack(true);
4569 bool InsetTabular::pasteClipboard(Cursor & cur)
4573 col_type const actcol = tabular.cellColumn(cur.idx());
4574 row_type const actrow = tabular.cellRow(cur.idx());
4575 for (row_type r1 = 0, r2 = actrow;
4576 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4578 for (col_type c1 = 0, c2 = actcol;
4579 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4581 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4582 tabular.isPartOfMultiColumn(r2, c2))
4584 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4588 if (tabular.isPartOfMultiColumn(r2, c2)) {
4592 shared_ptr<InsetText> inset(
4593 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4594 tabular.setCellInset(r2, c2, inset);
4595 // FIXME: change tracking (MG)
4596 inset->setChange(Change(cur.buffer().params().trackChanges ?
4597 Change::INSERTED : Change::UNCHANGED));
4605 void InsetTabular::cutSelection(Cursor & cur)
4607 if (!cur.selection())
4612 getSelection(cur, rs, re, cs, ce);
4613 for (row_type i = rs; i <= re; ++i) {
4614 for (col_type j = cs; j <= ce; ++j) {
4615 shared_ptr<InsetText> t
4616 = cell(tabular.cellIndex(i, j));
4617 if (cur.buffer().params().trackChanges)
4618 // FIXME: Change tracking (MG)
4619 t->setChange(Change(Change::DELETED));
4625 // cursor position might be invalid now
4626 if (cur.pit() > cur.lastpit())
4627 cur.pit() = cur.lastpit();
4628 if (cur.pos() > cur.lastpos())
4629 cur.pos() = cur.lastpos();
4630 cur.clearSelection();
4634 bool InsetTabular::isRightToLeft(Cursor & cur) const
4636 BOOST_ASSERT(cur.depth() > 1);
4637 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4638 pos_type const parentpos = cur[cur.depth() - 2].pos();
4639 return parentpar.getFontSettings(cur.bv().buffer().params(),
4640 parentpos).language()->rightToLeft();
4644 void InsetTabular::getSelection(Cursor & cur,
4645 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4647 CursorSlice const & beg = cur.selBegin();
4648 CursorSlice const & end = cur.selEnd();
4649 cs = tabular.cellColumn(beg.idx());
4650 ce = tabular.cellColumn(end.idx());
4653 cs = tabular.cellColumn(end.idx());
4655 ce = tabular.cellRightColumn(end.idx());
4658 rs = tabular.cellRow(beg.idx());
4659 re = tabular.cellRow(end.idx());
4665 Text * InsetTabular::getText(int idx) const
4667 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4671 void InsetTabular::setChange(Change const & change)
4673 for (idx_type idx = 0; idx < nargs(); ++idx)
4674 cell(idx)->setChange(change);
4678 void InsetTabular::acceptChanges(BufferParams const & bparams)
4680 for (idx_type idx = 0; idx < nargs(); ++idx)
4681 cell(idx)->acceptChanges(bparams);
4685 void InsetTabular::rejectChanges(BufferParams const & bparams)
4687 for (idx_type idx = 0; idx < nargs(); ++idx)
4688 cell(idx)->rejectChanges(bparams);
4692 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4694 return tabular.getPWidth(cell).zero();
4698 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4701 if (buf.length() <= 0)
4704 Buffer const & buffer = bv.buffer();
4708 col_type maxCols = 1;
4709 docstring::size_type const len = buf.length();
4710 docstring::size_type p = 0;
4713 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4721 maxCols = max(cols, maxCols);
4727 maxCols = max(cols, maxCols);
4733 paste_tabular.reset(
4734 new Tabular(buffer.params(), rows, maxCols));
4735 loctab = paste_tabular.get();
4737 dirtyTabularStack(true);
4740 cell = bv.cursor().idx();
4741 ocol = tabular.cellColumn(cell);
4742 row = tabular.cellRow(cell);
4745 docstring::size_type op = 0;
4746 idx_type const cells = loctab->cellCount();
4749 rows = loctab->rowCount();
4750 col_type const columns = loctab->columnCount();
4752 while (cell < cells && p < len && row < rows &&
4753 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4759 // we can only set this if we are not too far right
4760 if (cols < columns) {
4761 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4762 Font const font = bv.textMetrics(&inset->text_).
4763 getDisplayFont(0, 0);
4764 inset->setText(buf.substr(op, p - op), font,
4765 buffer.params().trackChanges);
4771 // we can only set this if we are not too far right
4772 if (cols < columns) {
4773 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4774 Font const font = bv.textMetrics(&inset->text_).
4775 getDisplayFont(0, 0);
4776 inset->setText(buf.substr(op, p - op), font,
4777 buffer.params().trackChanges);
4782 cell = loctab->cellIndex(row, cols);
4788 // check for the last cell if there is no trailing '\n'
4789 if (cell < cells && op < len) {
4790 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4791 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4792 inset->setText(buf.substr(op, len - op), font,
4793 buffer.params().trackChanges);
4799 void InsetTabular::addPreview(PreviewLoader & loader) const
4801 row_type const rows = tabular.rowCount();
4802 col_type const columns = tabular.columnCount();
4803 for (row_type i = 0; i < rows; ++i) {
4804 for (col_type j = 0; j < columns; ++j)
4805 tabular.getCellInset(i, j)->addPreview(loader);
4810 bool InsetTabular::tablemode(Cursor & cur) const
4812 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4818 string const InsetTabularMailer::name_("tabular");
4820 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4821 : inset_(const_cast<InsetTabular &>(inset))
4825 string const InsetTabularMailer::inset2string(Buffer const &) const
4827 return params2string(inset_);
4831 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4833 istringstream data(in);
4835 lex.setStream(data);
4842 if (!lex || token != name_)
4843 return print_mailer_error("InsetTabularMailer", in, 1,
4846 // This is part of the inset proper that is usually swallowed
4847 // by Buffer::readInset
4849 if (!lex || token != "Tabular")
4850 return print_mailer_error("InsetTabularMailer", in, 2,
4853 Buffer const & buffer = inset.buffer();
4854 inset.read(buffer, lex);
4858 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4861 data << name_ << ' ';
4862 inset.write(inset.buffer(), data);
4863 data << "\\end_inset\n";