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/docstream.h"
50 #include "support/lstrings.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include <boost/scoped_ptr.hpp>
67 using std::istringstream;
70 using std::ostringstream;
75 #ifndef CXX_GLOBAL_CSTD
79 using boost::shared_ptr;
80 using boost::dynamic_pointer_cast;
84 using support::prefixIs;
87 using support::suffixIs;
89 using cap::dirtyTabularStack;
90 using cap::tabularStackDirty;
92 using graphics::PreviewLoader;
94 using frontend::Painter;
95 using frontend::Clipboard;
97 namespace Alert = frontend::Alert;
102 int const ADD_TO_HEIGHT = 2;
103 int const ADD_TO_TABULAR_WIDTH = 2;
104 int const default_line_space = 10;
105 int const WIDTH_OF_LINE = 5;
109 boost::scoped_ptr<Tabular> paste_tabular;
112 struct TabularFeature {
113 Tabular::Feature action;
118 TabularFeature tabularFeature[] =
120 { Tabular::APPEND_ROW, "append-row" },
121 { Tabular::APPEND_COLUMN, "append-column" },
122 { Tabular::DELETE_ROW, "delete-row" },
123 { Tabular::DELETE_COLUMN, "delete-column" },
124 { Tabular::COPY_ROW, "copy-row" },
125 { Tabular::COPY_COLUMN, "copy-column" },
126 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
127 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
128 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
129 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
130 { Tabular::ALIGN_LEFT, "align-left" },
131 { Tabular::ALIGN_RIGHT, "align-right" },
132 { Tabular::ALIGN_CENTER, "align-center" },
133 { Tabular::ALIGN_BLOCK, "align-block" },
134 { Tabular::VALIGN_TOP, "valign-top" },
135 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
136 { Tabular::VALIGN_MIDDLE, "valign-middle" },
137 { Tabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
138 { Tabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
139 { Tabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
140 { Tabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
141 { Tabular::M_ALIGN_LEFT, "m-align-left" },
142 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
143 { Tabular::M_ALIGN_CENTER, "m-align-center" },
144 { Tabular::M_VALIGN_TOP, "m-valign-top" },
145 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
146 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
147 { Tabular::MULTICOLUMN, "multicolumn" },
148 { Tabular::SET_ALL_LINES, "set-all-lines" },
149 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
150 { Tabular::SET_LONGTABULAR, "set-longtabular" },
151 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
152 { Tabular::SET_PWIDTH, "set-pwidth" },
153 { Tabular::SET_MPWIDTH, "set-mpwidth" },
154 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
155 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
156 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
157 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
158 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
159 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
160 { Tabular::SET_USEBOX, "set-usebox" },
161 { Tabular::SET_LTHEAD, "set-lthead" },
162 { Tabular::UNSET_LTHEAD, "unset-lthead" },
163 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
164 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
165 { Tabular::SET_LTFOOT, "set-ltfoot" },
166 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
167 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
168 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
169 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
170 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
171 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
172 { Tabular::SET_BOOKTABS, "set-booktabs" },
173 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
174 { Tabular::SET_TOP_SPACE, "set-top-space" },
175 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
176 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
177 { Tabular::LAST_ACTION, "" }
181 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
183 FeatureEqual(Tabular::Feature feature)
184 : feature_(feature) {}
185 bool operator()(TabularFeature const & tf) const {
186 return tf.action == feature_;
189 Tabular::Feature feature_;
194 string const write_attribute(string const & name, T const & t)
196 string const s = tostr(t);
197 return s.empty() ? s : " " + name + "=\"" + s + "\"";
201 string const write_attribute(string const & name, string const & t)
203 return t.empty() ? t : " " + name + "=\"" + t + "\"";
208 string const write_attribute(string const & name, docstring const & t)
210 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
215 string const write_attribute(string const & name, bool const & b)
217 // we write only true attribute values so we remove a bit of the
218 // file format bloat for tabulars.
219 return b ? write_attribute(name, convert<string>(b)) : string();
224 string const write_attribute(string const & name, int const & i)
226 // we write only true attribute values so we remove a bit of the
227 // file format bloat for tabulars.
228 return i ? write_attribute(name, convert<string>(i)) : string();
233 string const write_attribute(string const & name, Tabular::idx_type const & i)
235 // we write only true attribute values so we remove a bit of the
236 // file format bloat for tabulars.
237 return i ? write_attribute(name, convert<string>(i)) : string();
242 string const write_attribute(string const & name, Length const & value)
244 // we write only the value if we really have one same reson as above.
245 return value.zero() ? string() : write_attribute(name, value.asString());
249 string const tostr(LyXAlignment const & num)
254 case LYX_ALIGN_BLOCK:
258 case LYX_ALIGN_CENTER:
260 case LYX_ALIGN_RIGHT:
262 case LYX_ALIGN_LAYOUT:
264 case LYX_ALIGN_SPECIAL:
271 string const tostr(Tabular::VAlignment const & num)
274 case Tabular::LYX_VALIGN_TOP:
276 case Tabular::LYX_VALIGN_MIDDLE:
278 case Tabular::LYX_VALIGN_BOTTOM:
285 string const tostr(Tabular::BoxType const & num)
288 case Tabular::BOX_NONE:
290 case Tabular::BOX_PARBOX:
292 case Tabular::BOX_MINIPAGE:
299 // I would have liked a fromstr template a lot better. (Lgb)
300 bool string2type(string const str, LyXAlignment & num)
303 num = LYX_ALIGN_NONE;
304 else if (str == "block")
305 num = LYX_ALIGN_BLOCK;
306 else if (str == "left")
307 num = LYX_ALIGN_LEFT;
308 else if (str == "center")
309 num = LYX_ALIGN_CENTER;
310 else if (str == "right")
311 num = LYX_ALIGN_RIGHT;
318 bool string2type(string const str, Tabular::VAlignment & num)
321 num = Tabular::LYX_VALIGN_TOP;
322 else if (str == "middle" )
323 num = Tabular::LYX_VALIGN_MIDDLE;
324 else if (str == "bottom")
325 num = Tabular::LYX_VALIGN_BOTTOM;
332 bool string2type(string const str, Tabular::BoxType & num)
335 num = Tabular::BOX_NONE;
336 else if (str == "parbox")
337 num = Tabular::BOX_PARBOX;
338 else if (str == "minipage")
339 num = Tabular::BOX_MINIPAGE;
346 bool string2type(string const str, bool & num)
350 else if (str == "false")
358 bool getTokenValue(string const & str, char const * token, string & ret)
361 size_t token_length = strlen(token);
362 string::size_type pos = str.find(token);
364 if (pos == string::npos || pos + token_length + 1 >= str.length()
365 || str[pos + token_length] != '=')
367 pos += token_length + 1;
369 if (ch != '"' && ch != '\'') { // only read till next space
373 while (pos < str.length() - 1 && str[++pos] != ch)
380 bool getTokenValue(string const & str, char const * token, docstring & ret)
383 bool const success = getTokenValue(str, token, tmp);
384 ret = from_utf8(tmp);
389 bool getTokenValue(string const & str, char const * token, int & num)
393 if (!getTokenValue(str, token, tmp))
395 num = convert<int>(tmp);
400 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
403 return getTokenValue(str, token, tmp) && string2type(tmp, num);
407 bool getTokenValue(string const & str, char const * token,
408 Tabular::VAlignment & num)
411 return getTokenValue(str, token, tmp) && string2type(tmp, num);
415 bool getTokenValue(string const & str, char const * token,
416 Tabular::BoxType & num)
419 return getTokenValue(str, token, tmp) && string2type(tmp, num);
423 bool getTokenValue(string const & str, char const * token, bool & flag)
425 // set the flag always to false as this should be the default for bools
426 // not in the file-format.
429 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
433 bool getTokenValue(string const & str, char const * token, Length & len)
435 // set the length to be zero() as default as this it should be if not
436 // in the file format.
439 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
443 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
448 if (!getTokenValue(str, token, tmp))
450 if (tmp == "default") {
454 return isValidLength(tmp, &len);
458 void l_getline(istream & is, string & str)
461 while (str.empty()) {
463 if (!str.empty() && str[str.length() - 1] == '\r')
464 str.erase(str.length() - 1);
471 string const featureAsString(Tabular::Feature feature)
473 TabularFeature * end = tabularFeature +
474 sizeof(tabularFeature) / sizeof(TabularFeature);
475 TabularFeature * it = std::find_if(tabularFeature, end,
476 FeatureEqual(feature));
477 return (it == end) ? string() : it->feature;
482 /////////////////////////////////////////////////////////////////////
486 /////////////////////////////////////////////////////////////////////
489 Tabular::cellstruct::cellstruct(BufferParams const & bp)
492 multicolumn(Tabular::CELL_NORMAL),
493 alignment(LYX_ALIGN_CENTER),
494 valignment(LYX_VALIGN_TOP),
501 inset(new InsetText(bp))
505 Tabular::cellstruct::cellstruct(cellstruct const & cs)
508 multicolumn(cs.multicolumn),
509 alignment(cs.alignment),
510 valignment(cs.valignment),
511 top_line(cs.top_line),
512 bottom_line(cs.bottom_line),
513 left_line(cs.left_line),
514 right_line(cs.right_line),
517 align_special(cs.align_special),
519 inset(dynamic_cast<InsetText*>(cs.inset->clone()))
523 Tabular::cellstruct & Tabular::cellstruct::operator=(cellstruct cs)
530 void Tabular::cellstruct::swap(cellstruct & rhs)
532 std::swap(cellno, rhs.cellno);
533 std::swap(width, rhs.width);
534 std::swap(multicolumn, rhs.multicolumn);
535 std::swap(alignment, rhs.alignment);
536 std::swap(valignment, rhs.valignment);
537 std::swap(top_line, rhs.top_line);
538 std::swap(bottom_line, rhs.bottom_line);
539 std::swap(left_line, rhs.left_line);
540 std::swap(right_line, rhs.right_line);
541 std::swap(usebox, rhs.usebox);
542 std::swap(rotate, rhs.rotate);
543 std::swap(align_special, rhs.align_special);
544 p_width.swap(rhs.p_width);
545 inset.swap(rhs.inset);
549 Tabular::rowstruct::rowstruct()
554 top_space_default(false),
555 bottom_space_default(false),
556 interline_space_default(false),
565 Tabular::columnstruct::columnstruct()
566 : alignment(LYX_ALIGN_CENTER),
567 valignment(LYX_VALIGN_TOP),
575 Tabular::ltType::ltType()
588 Tabular::Tabular(BufferParams const & bp, row_type rows_arg,
589 col_type columns_arg)
591 init(bp, rows_arg, columns_arg);
595 // activates all lines and sets all widths to 0
596 void Tabular::init(BufferParams const & bp, row_type rows_arg,
597 col_type columns_arg)
599 row_info = row_vector(rows_arg);
600 column_info = column_vector(columns_arg);
601 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, cellstruct(bp)));
602 row_info.reserve(10);
603 column_info.reserve(10);
604 cell_info.reserve(100);
606 for (row_type i = 0; i < rows_arg; ++i)
607 cell_info[i].back().right_line = true;
608 row_info.back().bottom_line = true;
609 row_info.front().bottom_line = true;
610 column_info.back().right_line = true;
611 is_long_tabular = false;
613 use_booktabs = false;
617 void Tabular::fixCellNums()
620 for (row_type i = 0; i < rowCount(); ++i) {
621 for (col_type j = 0; j < columnCount(); ++j) {
622 // When debugging it can be nice to set
624 cell_info[i][j].inset->setDrawFrame(false);
625 cell_info[i][j].cellno = cellno++;
627 cell_info[i].back().right_line = true;
634 void Tabular::appendRow(BufferParams const & bp, idx_type const cell)
636 row_type const row = cellRow(cell);
638 row_vector::iterator rit = row_info.begin() + row;
639 row_info.insert(rit, rowstruct());
640 // now set the values of the row before
641 row_info[row] = row_info[row + 1];
643 row_type const nrows = rowCount();
644 col_type const ncols = columnCount();
646 cell_vvector old(nrows - 1);
647 for (row_type i = 0; i < nrows - 1; ++i)
648 swap(cell_info[i], old[i]);
650 cell_info = cell_vvector(nrows, cell_vector(ncols, cellstruct(bp)));
652 for (row_type i = 0; i <= row; ++i)
653 swap(cell_info[i], old[i]);
654 for (row_type i = row + 2; i < nrows; ++i)
655 swap(cell_info[i], old[i - 1]);
658 for (col_type j = 0; j < ncols; ++j)
659 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
665 void Tabular::deleteRow(row_type const row)
667 // Not allowed to delete last row
671 row_info.erase(row_info.begin() + row);
672 cell_info.erase(cell_info.begin() + row);
677 void Tabular::copyRow(BufferParams const & bp, row_type const row)
679 row_info.insert(row_info.begin() + row, row_info[row]);
680 cell_info.insert(cell_info.begin() + row, cell_info[row]);
683 for (col_type j = 0; j < columnCount(); ++j)
684 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
690 void Tabular::appendColumn(BufferParams const & bp, idx_type const cell)
692 col_type const column = cellColumn(cell);
693 col_type const ncols = columnCount();
694 column_vector::iterator cit = column_info.begin() + column + 1;
695 column_info.insert(cit, columnstruct());
696 // set the column values of the column before
697 column_info[column + 1] = column_info[column];
699 for (row_type i = 0; i < rowCount(); ++i) {
700 cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
702 // care about multicolumns
703 if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
704 cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
706 if (column + 2 >= ncols
707 || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
708 cell_info[i][column + 1].multicolumn = Tabular::CELL_NORMAL;
711 for (row_type i = 0; i < rowCount(); ++i) {
712 cell_info[i][column + 1].inset->clear();
714 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
720 void Tabular::deleteColumn(col_type const column)
722 // Not allowed to delete last column
723 if (columnCount() == 1)
726 column_info.erase(column_info.begin() + column);
727 for (row_type i = 0; i < rowCount(); ++i) {
728 // Care about multicolumn cells
729 if (column + 1 < columnCount() &&
730 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
731 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
732 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
734 cell_info[i].erase(cell_info[i].begin() + column);
740 void Tabular::copyColumn(BufferParams const & bp, col_type const column)
742 column_info.insert(column_info.begin() + column, column_info[column]);
744 for (row_type i = 0; i < rowCount(); ++i)
745 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
748 for (row_type i = 0; i < rowCount(); ++i)
749 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
754 void Tabular::updateIndexes()
757 // Count only non-multicol cells plus begin multicol
758 // cells, ignore non-begin multicol cells:
759 for (row_type row = 0; row < rowCount(); ++row) {
760 for (col_type column = 0; column < columnCount(); ++column) {
761 if (cell_info[row][column].multicolumn
762 != Tabular::CELL_PART_OF_MULTICOLUMN)
764 BOOST_ASSERT(numberofcells != 0);
765 cell_info[row][column].cellno =
770 rowofcell.resize(numberofcells);
771 columnofcell.resize(numberofcells);
776 c < numberofcells && row < rowCount() && column < columnCount();) {
778 columnofcell[c] = column;
782 } while (column < columnCount() &&
783 cell_info[row][column].multicolumn
784 == Tabular::CELL_PART_OF_MULTICOLUMN);
786 if (column == columnCount()) {
792 for (row_type row = 0; row < rowCount(); ++row) {
793 for (col_type column = 0; column < columnCount(); ++column) {
794 if (isPartOfMultiColumn(row,column))
796 cell_info[row][column].inset->setAutoBreakRows(
797 !getPWidth(cellIndex(row, column)).zero());
803 Tabular::idx_type Tabular::cellCount() const
805 return numberofcells;
809 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
811 row_type const row = cellRow(cell);
813 for (col_type i = 0; i < columnCount(); ++i)
814 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
820 bool Tabular::topLine(idx_type const cell, bool const wholerow) const
822 if (!wholerow && isMultiColumn(cell) &&
823 !(use_booktabs && cellRow(cell) == 0))
824 return cellinfo_of_cell(cell).top_line;
825 return row_info[cellRow(cell)].top_line;
829 bool Tabular::bottomLine(idx_type const cell, bool wholerow) const
831 if (!wholerow && isMultiColumn(cell) &&
832 !(use_booktabs && isLastRow(cell)))
833 return cellinfo_of_cell(cell).bottom_line;
834 return row_info[cellRow(cell)].bottom_line;
838 bool Tabular::leftLine(idx_type cell, bool wholecolumn) const
842 if (!wholecolumn && isMultiColumn(cell) &&
843 (isFirstCellInRow(cell) || isMultiColumn(cell-1)))
844 return cellinfo_of_cell(cell).left_line;
845 return column_info[cellColumn(cell)].left_line;
849 bool Tabular::rightLine(idx_type cell, bool wholecolumn) const
853 if (!wholecolumn && isMultiColumn(cell) &&
854 (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
855 return cellinfo_of_cell(cell).right_line;
856 return column_info[cellRightColumn(cell)].right_line;
860 bool Tabular::topAlreadyDrawn(idx_type cell) const
862 row_type row = cellRow(cell);
863 if (row > 0 && !getAdditionalHeight(row)) {
864 col_type column = cellColumn(cell);
867 && cell_info[row][column].multicolumn
868 == Tabular::CELL_PART_OF_MULTICOLUMN)
870 if (cell_info[row][column].multicolumn == Tabular::CELL_NORMAL)
871 return row_info[row].bottom_line;
873 return cell_info[row][column].bottom_line;
879 bool Tabular::leftAlreadyDrawn(idx_type cell) const
881 col_type column = cellColumn(cell);
883 row_type row = cellRow(cell);
885 (cell_info[row][column].multicolumn ==
886 Tabular::CELL_PART_OF_MULTICOLUMN)) { }
887 if (getAdditionalWidth(cell_info[row][column].cellno))
889 return rightLine(cell_info[row][column].cellno);
895 bool Tabular::isLastRow(idx_type cell) const
897 return cellRow(cell) == rowCount() - 1;
901 int Tabular::getAdditionalHeight(row_type row) const
903 if (!row || row >= rowCount())
909 for (col_type column = 0; column < columnCount() && bottom; ++column) {
910 switch (cell_info[row - 1][column].multicolumn) {
911 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
912 bottom = cell_info[row - 1][column].bottom_line;
914 case Tabular::CELL_NORMAL:
915 bottom = row_info[row - 1].bottom_line;
918 for (col_type column = 0; column < columnCount() && top; ++column) {
919 switch (cell_info[row][column].multicolumn) {
920 case Tabular::CELL_BEGIN_OF_MULTICOLUMN:
921 top = cell_info[row][column].top_line;
923 case Tabular::CELL_NORMAL:
924 top = row_info[row].top_line;
927 int const interline_space = row_info[row - 1].interline_space_default ?
929 row_info[row - 1].interline_space.inPixels(width());
931 return interline_space + WIDTH_OF_LINE;
932 return interline_space;
936 int Tabular::getAdditionalWidth(idx_type cell) const
938 // internally already set in setCellWidth
939 // used to get it back in text.cpp
940 col_type const col = cellRightColumn(cell);
941 row_type const row = cellRow(cell);
942 if (col < columnCount() - 1 && rightLine(cell) &&
943 leftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
945 return WIDTH_OF_LINE;
951 // returns the maximum over all rows
952 int Tabular::columnWidth(idx_type cell) const
954 col_type const column1 = cellColumn(cell);
955 col_type const column2 = cellRightColumn(cell);
957 for (col_type i = column1; i <= column2; ++i)
958 result += column_info[i].width;
963 int Tabular::width() const
966 for (col_type i = 0; i < columnCount(); ++i)
967 width += column_info[i].width;
972 // returns true if a complete update is necessary, otherwise false
973 bool Tabular::setWidthOfMulticolCell(idx_type cell, int new_width)
975 if (!isMultiColumn(cell))
978 row_type const row = cellRow(cell);
979 col_type const column1 = cellColumn(cell);
980 col_type const column2 = cellRightColumn(cell);
981 int const old_val = cell_info[row][column2].width;
983 // first set columns to 0 so we can calculate the right width
984 for (col_type i = column1; i <= column2; ++i) {
985 cell_info[row][i].width = 0;
987 // set the width to MAX_WIDTH until width > 0
988 int width = new_width + 2 * WIDTH_OF_LINE;
989 col_type i = column1;
990 for (; i < column2 && width > column_info[i].width; ++i) {
991 cell_info[row][i].width = column_info[i].width;
992 width -= column_info[i].width;
995 cell_info[row][i].width = width;
997 if (old_val != cell_info[row][column2].width) {
998 // in this case we have to recalculate all multicolumn cells which
999 // have this column as one of theirs but not as last one
1000 calculate_width_of_column_NMC(i);
1001 recalculateMulticolumnsOfColumn(i);
1002 calculate_width_of_column(i);
1008 void Tabular::recalculateMulticolumnsOfColumn(col_type column)
1010 // the last column does not have to be recalculated because all
1011 // multicolumns will have here there last multicolumn cell which
1012 // always will have the whole rest of the width of the cell.
1013 if (columnCount() < 2 || column > (columnCount() - 2))
1015 for (row_type row = 0; row < rowCount(); ++row) {
1016 int mc = cell_info[row][column].multicolumn;
1017 int nmc = cell_info[row][column+1].multicolumn;
1018 // we only have to update multicolumns which do not have this
1019 // column as their last column!
1020 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
1021 (mc == CELL_PART_OF_MULTICOLUMN &&
1022 nmc == CELL_PART_OF_MULTICOLUMN))
1024 idx_type const cellno = cell_info[row][column].cellno;
1025 setWidthOfMulticolCell(cellno,
1026 cellWidth(cellno) - 2 * WIDTH_OF_LINE);
1032 void Tabular::setCellWidth(idx_type cell, int new_width)
1034 row_type const row = cellRow(cell);
1035 col_type const column1 = cellColumn(cell);
1040 if (rightLine(cell_info[row][column1].cellno, true) &&
1041 column1 < columnCount() - 1 &&
1042 leftLine(cell_info[row][column1+1].cellno, true))
1044 add_width = WIDTH_OF_LINE;
1047 if (cellWidth(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
1050 if (isMultiColumnReal(cell)) {
1051 tmp = setWidthOfMulticolCell(cell, new_width);
1053 width = new_width + 2 * WIDTH_OF_LINE + add_width;
1054 cell_info[row][column1].width = width;
1055 tmp = calculate_width_of_column_NMC(column1);
1057 recalculateMulticolumnsOfColumn(column1);
1060 for (col_type i = 0; i < columnCount(); ++i)
1061 calculate_width_of_column(i);
1066 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1069 if (!isMultiColumn(cell) || onlycolumn)
1070 column_info[cellColumn(cell)].alignment = align;
1072 cellinfo_of_cell(cell).alignment = align;
1076 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1079 if (!isMultiColumn(cell) || onlycolumn)
1080 column_info[cellColumn(cell)].valignment = align;
1082 cellinfo_of_cell(cell).valignment = align;
1089 * Allow line and paragraph breaks for fixed width cells or disallow them,
1090 * merge cell paragraphs and reset layout to standard for variable width
1093 void toggleFixedWidth(Cursor & cur, InsetText * inset, bool fixedWidth)
1095 inset->setAutoBreakRows(fixedWidth);
1099 // merge all paragraphs to one
1100 BufferParams const & bp = cur.bv().buffer().params();
1101 while (inset->paragraphs().size() > 1)
1102 mergeParagraph(bp, inset->paragraphs(), 0);
1106 // undo information has already been recorded
1107 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
1108 bp.getTextClass().defaultLayoutName());
1115 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1116 Length const & width)
1118 col_type const j = cellColumn(cell);
1120 column_info[j].p_width = width;
1121 for (row_type i = 0; i < rowCount(); ++i) {
1122 idx_type const cell = cellIndex(i, j);
1123 // because of multicolumns
1124 toggleFixedWidth(cur, getCellInset(cell).get(),
1125 !getPWidth(cell).zero());
1127 // cur paragraph can become invalid after paragraphs were merged
1128 if (cur.pit() > cur.lastpit())
1129 cur.pit() = cur.lastpit();
1130 // cur position can become invalid after newlines were removed
1131 if (cur.pos() > cur.lastpos())
1132 cur.pos() = cur.lastpos();
1136 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1137 Length const & width)
1139 if (!isMultiColumn(cell))
1142 cellinfo_of_cell(cell).p_width = width;
1143 toggleFixedWidth(cur, getCellInset(cell).get(), !width.zero());
1144 // cur paragraph can become invalid after paragraphs were merged
1145 if (cur.pit() > cur.lastpit())
1146 cur.pit() = cur.lastpit();
1147 // cur position can become invalid after newlines were removed
1148 if (cur.pos() > cur.lastpos())
1149 cur.pos() = cur.lastpos();
1154 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1155 Tabular::Feature what)
1157 if (what == SET_SPECIAL_MULTI)
1158 cellinfo_of_cell(cell).align_special = special;
1160 column_info[cellColumn(cell)].align_special = special;
1164 void Tabular::setAllLines(idx_type cell, bool line)
1166 setTopLine(cell, line);
1167 setBottomLine(cell, line);
1168 setRightLine(cell, line);
1169 setLeftLine(cell, line);
1173 void Tabular::setTopLine(idx_type cell, bool line, bool wholerow)
1175 row_type const row = cellRow(cell);
1176 if (wholerow || !isMultiColumn(cell))
1177 row_info[row].top_line = line;
1179 cellinfo_of_cell(cell).top_line = line;
1183 void Tabular::setBottomLine(idx_type cell, bool line, bool wholerow)
1185 if (wholerow || !isMultiColumn(cell))
1186 row_info[cellRow(cell)].bottom_line = line;
1188 cellinfo_of_cell(cell).bottom_line = line;
1192 void Tabular::setLeftLine(idx_type cell, bool line, bool wholecolumn)
1194 if (wholecolumn || !isMultiColumn(cell))
1195 column_info[cellColumn(cell)].left_line = line;
1197 cellinfo_of_cell(cell).left_line = line;
1201 void Tabular::setRightLine(idx_type cell, bool line, bool wholecolumn)
1203 if (wholecolumn || !isMultiColumn(cell))
1204 column_info[cellRightColumn(cell)].right_line = line;
1206 cellinfo_of_cell(cell).right_line = line;
1210 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1212 if (!onlycolumn && isMultiColumn(cell))
1213 return cellinfo_of_cell(cell).alignment;
1214 return column_info[cellColumn(cell)].alignment;
1219 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1221 if (!onlycolumn && isMultiColumn(cell))
1222 return cellinfo_of_cell(cell).valignment;
1223 return column_info[cellColumn(cell)].valignment;
1227 Length const Tabular::getPWidth(idx_type cell) const
1229 if (isMultiColumn(cell))
1230 return cellinfo_of_cell(cell).p_width;
1231 return column_info[cellColumn(cell)].p_width;
1235 Length const Tabular::getColumnPWidth(idx_type cell) const
1237 return column_info[cellColumn(cell)].p_width;
1241 Length const Tabular::getMColumnPWidth(idx_type cell) const
1243 if (isMultiColumn(cell))
1244 return cellinfo_of_cell(cell).p_width;
1249 docstring const Tabular::getAlignSpecial(idx_type cell, int what) const
1251 if (what == SET_SPECIAL_MULTI)
1252 return cellinfo_of_cell(cell).align_special;
1253 return column_info[cellColumn(cell)].align_special;
1257 int Tabular::cellWidth(idx_type cell) const
1259 row_type const row = cellRow(cell);
1260 col_type const column1 = cellColumn(cell);
1261 col_type const column2 = cellRightColumn(cell);
1263 for (col_type i = column1; i <= column2; ++i)
1264 result += cell_info[row][i].width;
1269 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1273 switch (getAlignment(cell)) {
1274 case LYX_ALIGN_CENTER:
1275 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1277 case LYX_ALIGN_RIGHT:
1278 x += columnWidth(cell) - cellWidth(cell);
1279 // + getAdditionalWidth(cell);
1282 // LYX_ALIGN_LEFT: nothing :-)
1286 // the LaTeX Way :-(
1292 bool Tabular::isFirstCellInRow(idx_type cell) const
1294 return cellColumn(cell) == 0;
1298 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1300 if (row > rowCount() - 1)
1301 row = rowCount() - 1;
1302 return cell_info[row][0].cellno;
1306 bool Tabular::isLastCellInRow(idx_type cell) const
1308 return cellRightColumn(cell) == columnCount() - 1;
1312 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1314 if (row > rowCount() - 1)
1315 row = rowCount() - 1;
1316 return cell_info[row][columnCount() - 1].cellno;
1320 void Tabular::calculate_width_of_column(col_type column)
1323 for (row_type i = 0; i < rowCount(); ++i)
1324 maximum = max(cell_info[i][column].width, maximum);
1325 column_info[column].width = maximum;
1330 // Calculate the columns regarding ONLY the normal cells and if this
1331 // column is inside a multicolumn cell then use it only if its the last
1332 // column of this multicolumn cell as this gives an added width to the
1333 // column, all the rest should be adapted!
1335 bool Tabular::calculate_width_of_column_NMC(col_type column)
1337 int const old_column_width = column_info[column].width;
1339 for (row_type i = 0; i < rowCount(); ++i) {
1340 idx_type cell = cellIndex(i, column);
1341 bool ismulti = isMultiColumnReal(cell);
1342 if ((!ismulti || column == cellRightColumn(cell)) &&
1343 cell_info[i][column].width > max)
1345 max = cell_info[i][column].width;
1348 column_info[column].width = max;
1349 return column_info[column].width != old_column_width;
1353 Tabular::row_type Tabular::cellRow(idx_type cell) const
1355 if (cell >= cellCount())
1356 return rowCount() - 1;
1359 return rowofcell[cell];
1363 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1365 if (cell >= cellCount())
1366 return columnCount() - 1;
1369 return columnofcell[cell];
1373 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1375 row_type const row = cellRow(cell);
1376 col_type column = cellColumn(cell);
1377 while (column < columnCount() - 1 &&
1378 cell_info[row][column + 1].multicolumn == Tabular::CELL_PART_OF_MULTICOLUMN)
1384 void Tabular::write(Buffer const & buf, ostream & os) const
1388 << write_attribute("version", 3)
1389 << write_attribute("rows", rowCount())
1390 << write_attribute("columns", columnCount())
1392 // global longtable options
1394 << write_attribute("rotate", rotate)
1395 << write_attribute("booktabs", use_booktabs)
1396 << write_attribute("islongtable", is_long_tabular)
1397 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1398 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1399 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1400 << write_attribute("headTopDL", endhead.topDL)
1401 << write_attribute("headBottomDL", endhead.bottomDL)
1402 << write_attribute("footTopDL", endfoot.topDL)
1403 << write_attribute("footBottomDL", endfoot.bottomDL)
1404 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1405 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1406 << write_attribute("lastFootEmpty", endlastfoot.empty)
1408 for (col_type j = 0; j < columnCount(); ++j) {
1410 << write_attribute("alignment", column_info[j].alignment)
1411 << write_attribute("valignment", column_info[j].valignment)
1412 << write_attribute("leftline", column_info[j].left_line)
1413 << write_attribute("rightline", column_info[j].right_line)
1414 << write_attribute("width", column_info[j].p_width.asString())
1415 << write_attribute("special", column_info[j].align_special)
1418 for (row_type i = 0; i < rowCount(); ++i) {
1420 << write_attribute("topline", row_info[i].top_line)
1421 << write_attribute("bottomline", row_info[i].bottom_line);
1422 static const string def("default");
1423 if (row_info[i].top_space_default)
1424 os << write_attribute("topspace", def);
1426 os << write_attribute("topspace", row_info[i].top_space);
1427 if (row_info[i].bottom_space_default)
1428 os << write_attribute("bottomspace", def);
1430 os << write_attribute("bottomspace", row_info[i].bottom_space);
1431 if (row_info[i].interline_space_default)
1432 os << write_attribute("interlinespace", def);
1434 os << write_attribute("interlinespace", row_info[i].interline_space);
1435 os << write_attribute("endhead", row_info[i].endhead)
1436 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1437 << write_attribute("endfoot", row_info[i].endfoot)
1438 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1439 << write_attribute("newpage", row_info[i].newpage)
1441 for (col_type j = 0; j < columnCount(); ++j) {
1443 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1444 << write_attribute("alignment", cell_info[i][j].alignment)
1445 << write_attribute("valignment", cell_info[i][j].valignment)
1446 << write_attribute("topline", cell_info[i][j].top_line)
1447 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1448 << write_attribute("leftline", cell_info[i][j].left_line)
1449 << write_attribute("rightline", cell_info[i][j].right_line)
1450 << write_attribute("rotate", cell_info[i][j].rotate)
1451 << write_attribute("usebox", cell_info[i][j].usebox)
1452 << write_attribute("width", cell_info[i][j].p_width)
1453 << write_attribute("special", cell_info[i][j].align_special)
1455 os << "\\begin_inset ";
1456 cell_info[i][j].inset->write(buf, os);
1457 os << "\n\\end_inset\n"
1462 os << "</lyxtabular>\n";
1466 void Tabular::read(Buffer const & buf, Lexer & lex)
1469 istream & is = lex.getStream();
1471 l_getline(is, line);
1472 if (!prefixIs(line, "<lyxtabular ")
1473 && !prefixIs(line, "<Tabular ")) {
1474 BOOST_ASSERT(false);
1479 if (!getTokenValue(line, "version", version))
1481 BOOST_ASSERT(version >= 2);
1484 if (!getTokenValue(line, "rows", rows_arg))
1487 if (!getTokenValue(line, "columns", columns_arg))
1489 init(buf.params(), rows_arg, columns_arg);
1490 l_getline(is, line);
1491 if (!prefixIs(line, "<features")) {
1492 lyxerr << "Wrong tabular format (expected <features ...> got"
1493 << line << ')' << endl;
1496 getTokenValue(line, "rotate", rotate);
1497 getTokenValue(line, "booktabs", use_booktabs);
1498 getTokenValue(line, "islongtable", is_long_tabular);
1499 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1500 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1501 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1502 getTokenValue(line, "headTopDL", endhead.topDL);
1503 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1504 getTokenValue(line, "footTopDL", endfoot.topDL);
1505 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1506 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1507 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1508 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1510 for (col_type j = 0; j < columnCount(); ++j) {
1512 if (!prefixIs(line,"<column")) {
1513 lyxerr << "Wrong tabular format (expected <column ...> got"
1514 << line << ')' << endl;
1517 getTokenValue(line, "alignment", column_info[j].alignment);
1518 getTokenValue(line, "valignment", column_info[j].valignment);
1519 getTokenValue(line, "leftline", column_info[j].left_line);
1520 getTokenValue(line, "rightline", column_info[j].right_line);
1521 getTokenValue(line, "width", column_info[j].p_width);
1522 getTokenValue(line, "special", column_info[j].align_special);
1525 for (row_type i = 0; i < rowCount(); ++i) {
1526 l_getline(is, line);
1527 if (!prefixIs(line, "<row")) {
1528 lyxerr << "Wrong tabular format (expected <row ...> got"
1529 << line << ')' << endl;
1532 getTokenValue(line, "topline", row_info[i].top_line);
1533 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1534 getTokenValue(line, "topspace", row_info[i].top_space,
1535 row_info[i].top_space_default);
1536 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1537 row_info[i].bottom_space_default);
1538 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1539 row_info[i].interline_space_default);
1540 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1541 getTokenValue(line, "endhead", row_info[i].endhead);
1542 getTokenValue(line, "endfoot", row_info[i].endfoot);
1543 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1544 getTokenValue(line, "newpage", row_info[i].newpage);
1545 for (col_type j = 0; j < columnCount(); ++j) {
1546 l_getline(is, line);
1547 if (!prefixIs(line, "<cell")) {
1548 lyxerr << "Wrong tabular format (expected <cell ...> got"
1549 << line << ')' << endl;
1552 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1553 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1554 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1555 getTokenValue(line, "topline", cell_info[i][j].top_line);
1556 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1557 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1558 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1559 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1560 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1561 getTokenValue(line, "width", cell_info[i][j].p_width);
1562 getTokenValue(line, "special", cell_info[i][j].align_special);
1563 l_getline(is, line);
1564 if (prefixIs(line, "\\begin_inset")) {
1565 cell_info[i][j].inset->read(buf, lex);
1566 l_getline(is, line);
1568 if (!prefixIs(line, "</cell>")) {
1569 lyxerr << "Wrong tabular format (expected </cell> got"
1570 << line << ')' << endl;
1574 l_getline(is, line);
1575 if (!prefixIs(line, "</row>")) {
1576 lyxerr << "Wrong tabular format (expected </row> got"
1577 << line << ')' << endl;
1581 while (!prefixIs(line, "</lyxtabular>")) {
1582 l_getline(is, line);
1588 bool Tabular::isMultiColumn(idx_type cell) const
1590 return cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1594 bool Tabular::isMultiColumnReal(idx_type cell) const
1596 return cellColumn(cell) != cellRightColumn(cell) &&
1597 cellinfo_of_cell(cell).multicolumn != Tabular::CELL_NORMAL;
1601 Tabular::cellstruct & Tabular::cellinfo_of_cell(idx_type cell) const
1603 return cell_info[cellRow(cell)][cellColumn(cell)];
1607 void Tabular::setMultiColumn(Buffer * buffer, idx_type cell,
1610 cellstruct & cs = cellinfo_of_cell(cell);
1611 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1612 cs.alignment = column_info[cellColumn(cell)].alignment;
1613 cs.top_line = row_info[cellRow(cell)].top_line;
1614 cs.bottom_line = row_info[cellRow(cell)].bottom_line;
1615 cs.left_line = column_info[cellColumn(cell)].left_line;
1616 cs.right_line = column_info[cellColumn(cell+number-1)].right_line;
1617 for (idx_type i = 1; i < number; ++i) {
1618 cellstruct & cs1 = cellinfo_of_cell(cell + i);
1619 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1620 cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
1627 Tabular::idx_type Tabular::cells_in_multicolumn(idx_type cell) const
1629 row_type const row = cellRow(cell);
1630 col_type column = cellColumn(cell);
1631 idx_type result = 1;
1633 while (column < columnCount() &&
1634 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1643 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1645 row_type const row = cellRow(cell);
1646 col_type column = cellColumn(cell);
1648 idx_type result = 0;
1650 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1651 cell_info[row][column].multicolumn = CELL_NORMAL;
1653 while (column < columnCount() &&
1654 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1656 cell_info[row][column].multicolumn = CELL_NORMAL;
1666 void Tabular::setBookTabs(bool what)
1668 use_booktabs = what;
1672 bool Tabular::useBookTabs() const
1674 return use_booktabs;
1678 void Tabular::setLongTabular(bool what)
1680 is_long_tabular = what;
1684 bool Tabular::isLongTabular() const
1686 return is_long_tabular;
1690 void Tabular::setRotateTabular(bool flag)
1696 bool Tabular::getRotateTabular() const
1702 void Tabular::setRotateCell(idx_type cell, bool flag)
1704 cellinfo_of_cell(cell).rotate = flag;
1708 bool Tabular::getRotateCell(idx_type cell) const
1710 return cellinfo_of_cell(cell).rotate;
1714 bool Tabular::needRotating() const
1718 for (row_type i = 0; i < rowCount(); ++i)
1719 for (col_type j = 0; j < columnCount(); ++j)
1720 if (cell_info[i][j].rotate)
1726 bool Tabular::isLastCell(idx_type cell) const
1728 if (cell + 1 < cellCount())
1734 Tabular::idx_type Tabular::getCellAbove(idx_type cell) const
1736 if (cellRow(cell) > 0)
1737 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1742 Tabular::idx_type Tabular::getCellBelow(idx_type cell) const
1744 if (cellRow(cell) + 1 < rowCount())
1745 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1750 Tabular::idx_type Tabular::getLastCellAbove(idx_type cell) const
1752 if (cellRow(cell) == 0)
1754 if (!isMultiColumn(cell))
1755 return getCellAbove(cell);
1756 return cell_info[cellRow(cell) - 1][cellRightColumn(cell)].cellno;
1760 Tabular::idx_type Tabular::getLastCellBelow(idx_type cell) const
1762 if (cellRow(cell) + 1 >= rowCount())
1764 if (!isMultiColumn(cell))
1765 return getCellBelow(cell);
1766 return cell_info[cellRow(cell) + 1][cellRightColumn(cell)].cellno;
1770 Tabular::idx_type Tabular::cellIndex(row_type row,
1771 col_type column) const
1773 BOOST_ASSERT(column != npos && column < columnCount() &&
1774 row != npos && row < rowCount());
1775 return cell_info[row][column].cellno;
1779 void Tabular::setUsebox(idx_type cell, BoxType type)
1781 cellinfo_of_cell(cell).usebox = type;
1785 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1787 if (column_info[cellColumn(cell)].p_width.zero() &&
1788 !(isMultiColumn(cell) && !cellinfo_of_cell(cell).p_width.zero()))
1790 if (cellinfo_of_cell(cell).usebox > 1)
1791 return cellinfo_of_cell(cell).usebox;
1792 return useParbox(cell);
1797 // This are functions used for the longtable support
1799 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1805 row_info[row].endfirsthead = flag;
1809 row_info[row].endhead = flag;
1814 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1817 hd.set = haveLTHead();
1818 return row_info[row].endhead;
1822 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1825 hd.set = haveLTFirstHead();
1826 return row_info[row].endfirsthead;
1830 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1836 row_info[row].endlastfoot = flag;
1840 row_info[row].endfoot = flag;
1845 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1848 fd.set = haveLTFoot();
1849 return row_info[row].endfoot;
1853 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1856 fd.set = haveLTLastFoot();
1857 return row_info[row].endlastfoot;
1861 void Tabular::setLTNewPage(row_type row, bool what)
1863 row_info[row].newpage = what;
1867 bool Tabular::getLTNewPage(row_type row) const
1869 return row_info[row].newpage;
1873 bool Tabular::haveLTHead() const
1875 for (row_type i = 0; i < rowCount(); ++i)
1876 if (row_info[i].endhead)
1882 bool Tabular::haveLTFirstHead() const
1884 if (endfirsthead.empty)
1886 for (row_type i = 0; i < rowCount(); ++i)
1887 if (row_info[i].endfirsthead)
1893 bool Tabular::haveLTFoot() const
1895 for (row_type i = 0; i < rowCount(); ++i)
1896 if (row_info[i].endfoot)
1902 bool Tabular::haveLTLastFoot() const
1904 if (endlastfoot.empty)
1906 for (row_type i = 0; i < rowCount(); ++i)
1907 if (row_info[i].endlastfoot)
1913 // end longtable support functions
1915 void Tabular::setRowAscent(row_type row, int height)
1917 if (row >= rowCount() || row_info[row].ascent == height)
1919 row_info[row].ascent = height;
1923 void Tabular::setRowDescent(row_type row, int height)
1925 if (row >= rowCount() || row_info[row].descent == height)
1927 row_info[row].descent = height;
1931 int Tabular::rowAscent(row_type row) const
1933 if (row >= rowCount())
1935 return row_info[row].ascent;
1939 int Tabular::rowDescent(row_type row) const
1941 BOOST_ASSERT(row < rowCount());
1942 return row_info[row].descent;
1946 int Tabular::height() const
1949 for (row_type row = 0; row < rowCount(); ++row)
1950 height += rowAscent(row) + rowDescent(row) +
1951 getAdditionalHeight(row);
1956 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1958 BOOST_ASSERT(row < rowCount());
1959 BOOST_ASSERT(column < columnCount());
1960 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1964 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1966 // FIXME: assert or return 0 as in TeXBottomHLine()?
1967 BOOST_ASSERT(row != npos);
1968 BOOST_ASSERT(row < rowCount());
1970 idx_type const fcell = getFirstCellInRow(row);
1971 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1974 for (idx_type i = fcell; i < n; ++i) {
1978 if (use_booktabs && row == 0) {
1980 } else if (tmp == n - fcell) {
1981 os << (use_booktabs ? "\\midrule " : "\\hline ");
1983 for (idx_type i = fcell; i < n; ++i) {
1985 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1986 << cellColumn(i) + 1
1988 << cellRightColumn(i) + 1
2000 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
2002 // FIXME: return 0 or assert as in TeXTopHLine()?
2003 if (row == npos || row >= rowCount())
2006 idx_type const fcell = getFirstCellInRow(row);
2007 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2010 for (idx_type i = fcell; i < n; ++i) {
2014 if (use_booktabs && row == rowCount() - 1) {
2015 os << "\\bottomrule";
2016 } else if (tmp == n - fcell) {
2017 os << (use_booktabs ? "\\midrule" : "\\hline");
2019 for (idx_type i = fcell; i < n; ++i) {
2020 if (bottomLine(i)) {
2021 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
2022 << cellColumn(i) + 1
2024 << cellRightColumn(i) + 1
2036 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell) const
2040 if (getRotateCell(cell)) {
2041 os << "\\begin{sideways}\n";
2044 if (isMultiColumn(cell)) {
2045 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
2046 if (leftLine(cell) &&
2047 (isFirstCellInRow(cell) ||
2048 (!isMultiColumn(cell - 1) && !leftLine(cell, true) &&
2049 !rightLine(cell - 1, true))))
2051 if (!cellinfo_of_cell(cell).align_special.empty()) {
2052 os << cellinfo_of_cell(cell).align_special;
2054 if (!getPWidth(cell).zero()) {
2055 switch (getVAlignment(cell)) {
2056 case LYX_VALIGN_TOP:
2059 case LYX_VALIGN_MIDDLE:
2062 case LYX_VALIGN_BOTTOM:
2067 << from_ascii(getPWidth(cell).asLatexString())
2070 switch (getAlignment(cell)) {
2071 case LYX_ALIGN_LEFT:
2074 case LYX_ALIGN_RIGHT:
2081 } // end if else !getPWidth
2082 } // end if else !cellinfo_of_cell
2083 if (rightLine(cell))
2085 if (((cell + 1) < cellCount()) && !isFirstCellInRow(cell+1) &&
2090 if (getUsebox(cell) == BOX_PARBOX) {
2092 switch (getVAlignment(cell)) {
2093 case LYX_VALIGN_TOP:
2096 case LYX_VALIGN_MIDDLE:
2099 case LYX_VALIGN_BOTTOM:
2103 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2105 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2106 os << "\\begin{minipage}[";
2107 switch (getVAlignment(cell)) {
2108 case LYX_VALIGN_TOP:
2111 case LYX_VALIGN_MIDDLE:
2114 case LYX_VALIGN_BOTTOM:
2118 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2126 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell) const
2131 if (getUsebox(cell) == BOX_PARBOX)
2133 else if (getUsebox(cell) == BOX_MINIPAGE) {
2134 os << "%\n\\end{minipage}";
2137 if (isMultiColumn(cell)) {
2140 if (getRotateCell(cell)) {
2141 os << "%\n\\end{sideways}";
2148 int Tabular::TeXLongtableHeaderFooter(odocstream & os, Buffer const & buf,
2149 OutputParams const & runparams) const
2151 if (!is_long_tabular)
2155 // output header info
2157 if (endhead.topDL) {
2161 for (row_type i = 0; i < rowCount(); ++i) {
2162 if (row_info[i].endhead) {
2163 ret += TeXRow(os, i, buf, runparams);
2166 if (endhead.bottomDL) {
2170 os << "\\endhead\n";
2172 if (endfirsthead.empty) {
2173 os << "\\endfirsthead\n";
2177 // output firstheader info
2178 if (haveLTFirstHead()) {
2179 if (endfirsthead.topDL) {
2183 for (row_type i = 0; i < rowCount(); ++i) {
2184 if (row_info[i].endfirsthead) {
2185 ret += TeXRow(os, i, buf, runparams);
2188 if (endfirsthead.bottomDL) {
2192 os << "\\endfirsthead\n";
2195 // output footer info
2197 if (endfoot.topDL) {
2201 for (row_type i = 0; i < rowCount(); ++i) {
2202 if (row_info[i].endfoot) {
2203 ret += TeXRow(os, i, buf, runparams);
2206 if (endfoot.bottomDL) {
2210 os << "\\endfoot\n";
2212 if (endlastfoot.empty) {
2213 os << "\\endlastfoot\n";
2217 // output lastfooter info
2218 if (haveLTLastFoot()) {
2219 if (endlastfoot.topDL) {
2223 for (row_type i = 0; i < rowCount(); ++i) {
2224 if (row_info[i].endlastfoot) {
2225 ret += TeXRow(os, i, buf, runparams);
2228 if (endlastfoot.bottomDL) {
2232 os << "\\endlastfoot\n";
2239 bool Tabular::isValidRow(row_type row) const
2241 if (!is_long_tabular)
2243 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2244 !row_info[row].endfoot && !row_info[row].endlastfoot;
2248 int Tabular::TeXRow(odocstream & os, row_type i, Buffer const & buf,
2249 OutputParams const & runparams) const
2251 idx_type cell = cellIndex(i, 0);
2252 int ret = TeXTopHLine(os, i);
2253 if (row_info[i].top_space_default) {
2255 os << "\\addlinespace\n";
2257 os << "\\noalign{\\vskip\\doublerulesep}\n";
2259 } else if(!row_info[i].top_space.zero()) {
2261 os << "\\addlinespace["
2262 << from_ascii(row_info[i].top_space.asLatexString())
2265 os << "\\noalign{\\vskip"
2266 << from_ascii(row_info[i].top_space.asLatexString())
2272 for (col_type j = 0; j < columnCount(); ++j) {
2273 if (isPartOfMultiColumn(i, j))
2275 ret += TeXCellPreamble(os, cell);
2276 shared_ptr<InsetText> inset = getCellInset(cell);
2278 Paragraph const & par = inset->paragraphs().front();
2279 bool rtl = par.isRTL(buf.params())
2281 && getPWidth(cell).zero();
2284 if (par.getParLanguage(buf.params())->lang() ==
2287 else if (par.getParLanguage(buf.params())->lang() == "arabic_arabi")
2289 // currently, remaning RTL languages are arabic_arabtex and hebrew
2293 ret += inset->latex(buf, os, runparams);
2297 ret += TeXCellPostamble(os, cell);
2298 if (!isLastCellInRow(cell)) { // not last cell in row
2303 os << "\\tabularnewline";
2304 if (row_info[i].bottom_space_default) {
2306 os << "\\addlinespace";
2308 os << "[\\doublerulesep]";
2309 } else if (!row_info[i].bottom_space.zero()) {
2311 os << "\\addlinespace";
2313 << from_ascii(row_info[i].bottom_space.asLatexString())
2318 ret += TeXBottomHLine(os, i);
2319 if (row_info[i].interline_space_default) {
2321 os << "\\addlinespace\n";
2323 os << "\\noalign{\\vskip\\doublerulesep}\n";
2325 } else if (!row_info[i].interline_space.zero()) {
2327 os << "\\addlinespace["
2328 << from_ascii(row_info[i].interline_space.asLatexString())
2331 os << "\\noalign{\\vskip"
2332 << from_ascii(row_info[i].interline_space.asLatexString())
2340 int Tabular::latex(Buffer const & buf, odocstream & os,
2341 OutputParams const & runparams) const
2345 //+---------------------------------------------------------------------
2346 //+ first the opening preamble +
2347 //+---------------------------------------------------------------------
2350 os << "\\begin{sideways}\n";
2353 if (is_long_tabular)
2354 os << "\\begin{longtable}{";
2356 os << "\\begin{tabular}{";
2357 for (col_type i = 0; i < columnCount(); ++i) {
2358 if (!use_booktabs && column_info[i].left_line)
2360 if (!column_info[i].align_special.empty()) {
2361 os << column_info[i].align_special;
2363 if (!column_info[i].p_width.zero()) {
2364 switch (column_info[i].alignment) {
2365 case LYX_ALIGN_LEFT:
2366 os << ">{\\raggedright}";
2368 case LYX_ALIGN_RIGHT:
2369 os << ">{\\raggedleft}";
2371 case LYX_ALIGN_CENTER:
2372 os << ">{\\centering}";
2374 case LYX_ALIGN_NONE:
2375 case LYX_ALIGN_BLOCK:
2376 case LYX_ALIGN_LAYOUT:
2377 case LYX_ALIGN_SPECIAL:
2381 switch (column_info[i].valignment) {
2382 case LYX_VALIGN_TOP:
2385 case LYX_VALIGN_MIDDLE:
2388 case LYX_VALIGN_BOTTOM:
2393 << from_ascii(column_info[i].p_width.asLatexString())
2396 switch (column_info[i].alignment) {
2397 case LYX_ALIGN_LEFT:
2400 case LYX_ALIGN_RIGHT:
2407 } // end if else !column_info[i].p_width
2408 } // end if else !column_info[i].align_special
2409 if (!use_booktabs && column_info[i].right_line)
2415 ret += TeXLongtableHeaderFooter(os, buf, runparams);
2417 //+---------------------------------------------------------------------
2418 //+ the single row and columns (cells) +
2419 //+---------------------------------------------------------------------
2421 for (row_type i = 0; i < rowCount(); ++i) {
2422 if (isValidRow(i)) {
2423 ret += TeXRow(os, i, buf, runparams);
2424 if (is_long_tabular && row_info[i].newpage) {
2425 os << "\\newpage\n";
2431 //+---------------------------------------------------------------------
2432 //+ the closing of the tabular +
2433 //+---------------------------------------------------------------------
2435 if (is_long_tabular)
2436 os << "\\end{longtable}";
2438 os << "\\end{tabular}";
2440 os << "\n\\end{sideways}";
2448 int Tabular::docbookRow(Buffer const & buf, odocstream & os, row_type row,
2449 OutputParams const & runparams) const
2452 idx_type cell = getFirstCellInRow(row);
2455 for (col_type j = 0; j < columnCount(); ++j) {
2456 if (isPartOfMultiColumn(row, j))
2459 os << "<entry align=\"";
2460 switch (getAlignment(cell)) {
2461 case LYX_ALIGN_LEFT:
2464 case LYX_ALIGN_RIGHT:
2472 os << "\" valign=\"";
2473 switch (getVAlignment(cell)) {
2474 case LYX_VALIGN_TOP:
2477 case LYX_VALIGN_BOTTOM:
2480 case LYX_VALIGN_MIDDLE:
2485 if (isMultiColumn(cell)) {
2486 os << " namest=\"col" << j << "\" ";
2487 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< '"';
2491 ret += getCellInset(cell)->docbook(buf, os, runparams);
2500 int Tabular::docbook(Buffer const & buf, odocstream & os,
2501 OutputParams const & runparams) const
2505 //+---------------------------------------------------------------------
2506 //+ first the opening preamble +
2507 //+---------------------------------------------------------------------
2509 os << "<tgroup cols=\"" << columnCount()
2510 << "\" colsep=\"1\" rowsep=\"1\">\n";
2512 for (col_type i = 0; i < columnCount(); ++i) {
2513 os << "<colspec colname=\"col" << i << "\" align=\"";
2514 switch (column_info[i].alignment) {
2515 case LYX_ALIGN_LEFT:
2518 case LYX_ALIGN_RIGHT:
2526 if (runparams.flavor == OutputParams::XML)
2532 //+---------------------------------------------------------------------
2533 //+ Long Tabular case +
2534 //+---------------------------------------------------------------------
2536 // output header info
2537 if (haveLTHead() || haveLTFirstHead()) {
2540 for (row_type i = 0; i < rowCount(); ++i) {
2541 if (row_info[i].endhead || row_info[i].endfirsthead) {
2542 ret += docbookRow(buf, os, i, runparams);
2548 // output footer info
2549 if (haveLTFoot() || haveLTLastFoot()) {
2552 for (row_type i = 0; i < rowCount(); ++i) {
2553 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2554 ret += docbookRow(buf, os, i, runparams);
2561 //+---------------------------------------------------------------------
2562 //+ the single row and columns (cells) +
2563 //+---------------------------------------------------------------------
2567 for (row_type i = 0; i < rowCount(); ++i) {
2568 if (isValidRow(i)) {
2569 ret += docbookRow(buf, os, i, runparams);
2574 //+---------------------------------------------------------------------
2575 //+ the closing of the tabular +
2576 //+---------------------------------------------------------------------
2585 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2586 vector<unsigned int> const & clen) const
2588 idx_type const fcell = getFirstCellInRow(row);
2589 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2592 for (idx_type i = fcell; i < n; ++i) {
2602 for (idx_type i = fcell; i < n; ++i) {
2613 col_type column = cellColumn(i);
2614 int len = clen[column];
2615 while (column < columnCount() - 1
2616 && isPartOfMultiColumn(row, ++column))
2617 len += clen[column] + 4;
2618 os << docstring(len, ch);
2633 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2634 vector<unsigned int> const & clen) const
2636 idx_type const fcell = getFirstCellInRow(row);
2637 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2640 for (idx_type i = fcell; i < n; ++i) {
2641 if (bottomLine(i)) {
2650 for (idx_type i = fcell; i < n; ++i) {
2651 if (bottomLine(i)) {
2661 col_type column = cellColumn(i);
2662 int len = clen[column];
2663 while (column < columnCount() -1
2664 && isPartOfMultiColumn(row, ++column))
2665 len += clen[column] + 4;
2666 os << docstring(len, ch);
2667 if (bottomLine(i)) {
2681 void Tabular::plaintextPrintCell(Buffer const & buf, odocstream & os,
2682 OutputParams const & runparams,
2683 idx_type cell, row_type row, col_type column,
2684 vector<unsigned int> const & clen,
2685 bool onlydata) const
2687 odocstringstream sstr;
2688 getCellInset(cell)->plaintext(buf, sstr, runparams);
2700 unsigned int len1 = sstr.str().length();
2701 unsigned int len2 = clen[column];
2702 while (column < columnCount() -1
2703 && isPartOfMultiColumn(row, ++column))
2704 len2 += clen[column] + 4;
2707 switch (getAlignment(cell)) {
2709 case LYX_ALIGN_LEFT:
2712 case LYX_ALIGN_RIGHT:
2716 case LYX_ALIGN_CENTER:
2722 os << docstring(len1, ' ') << sstr.str()
2723 << docstring(len2, ' ');
2725 if (rightLine(cell))
2732 void Tabular::plaintext(Buffer const & buf, odocstream & os,
2733 OutputParams const & runparams, int const depth,
2734 bool onlydata, char_type delim) const
2736 // first calculate the width of the single columns
2737 vector<unsigned int> clen(columnCount());
2740 // first all non (real) multicolumn cells!
2741 for (col_type j = 0; j < columnCount(); ++j) {
2743 for (row_type i = 0; i < rowCount(); ++i) {
2744 idx_type cell = cellIndex(i, j);
2745 if (isMultiColumnReal(cell))
2747 odocstringstream sstr;
2748 getCellInset(cell)->plaintext(buf, sstr, runparams);
2749 if (clen[j] < sstr.str().length())
2750 clen[j] = sstr.str().length();
2753 // then all (real) multicolumn cells!
2754 for (col_type j = 0; j < columnCount(); ++j) {
2755 for (row_type i = 0; i < rowCount(); ++i) {
2756 idx_type cell = cellIndex(i, j);
2757 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2759 odocstringstream sstr;
2760 getCellInset(cell)->plaintext(buf, sstr, runparams);
2761 int len = int(sstr.str().length());
2762 idx_type const n = cells_in_multicolumn(cell);
2763 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2765 if (len > int(clen[j + n - 1]))
2766 clen[j + n - 1] = len;
2771 for (row_type i = 0; i < rowCount(); ++i) {
2772 if (!onlydata && plaintextTopHLine(os, i, clen))
2773 os << docstring(depth * 2, ' ');
2774 for (col_type j = 0; j < columnCount(); ++j) {
2775 if (isPartOfMultiColumn(i, j))
2777 if (onlydata && j > 0)
2778 // we don't use operator<< for single UCS4 character.
2779 // see explanation in docstream.h
2781 plaintextPrintCell(buf, os, runparams,
2782 cell, i, j, clen, onlydata);
2787 os << docstring(depth * 2, ' ');
2788 if (plaintextBottomHLine(os, i, clen))
2789 os << docstring(depth * 2, ' ');
2795 shared_ptr<InsetText> Tabular::getCellInset(idx_type cell) const
2797 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2801 shared_ptr<InsetText> Tabular::getCellInset(row_type row,
2802 col_type column) const
2804 return cell_info[row][column].inset;
2808 void Tabular::setCellInset(row_type row, col_type column,
2809 shared_ptr<InsetText> ins) const
2811 cell_info[row][column].inset = ins;
2816 Tabular::getCellFromInset(Inset const * inset) const
2818 // is this inset part of the tabular?
2820 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2821 BOOST_ASSERT(false);
2824 for (idx_type cell = 0, n = cellCount(); cell < n; ++cell)
2825 if (getCellInset(cell).get() == inset) {
2826 LYXERR(Debug::INSETTEXT) << "Tabular::getCellFromInset: "
2827 << "cell=" << cell << endl;
2831 // We should have found a cell at this point
2832 lyxerr << "Tabular::getCellFromInset: Cell of inset "
2833 << inset << " not found!" << endl;
2834 BOOST_ASSERT(false);
2840 void Tabular::validate(LaTeXFeatures & features) const
2842 features.require("NeedTabularnewline");
2844 features.require("booktabs");
2845 if (isLongTabular())
2846 features.require("longtable");
2848 features.require("rotating");
2849 for (idx_type cell = 0; cell < cellCount(); ++cell) {
2850 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2851 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2852 features.require("array");
2853 getCellInset(cell)->validate(features);
2858 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2860 ParagraphList const & parlist = getCellInset(cell)->paragraphs();
2861 ParagraphList::const_iterator cit = parlist.begin();
2862 ParagraphList::const_iterator end = parlist.end();
2864 for (; cit != end; ++cit)
2865 for (int i = 0; i < cit->size(); ++i)
2866 if (cit->isNewline(i))
2873 /////////////////////////////////////////////////////////////////////
2877 /////////////////////////////////////////////////////////////////////
2879 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2881 : tabular(buf.params(), max(rows, row_type(1)),
2882 max(columns, col_type(1))), buffer_(&buf), scx_(0)
2886 InsetTabular::InsetTabular(InsetTabular const & tab)
2887 : Inset(tab), tabular(tab.tabular),
2888 buffer_(tab.buffer_), scx_(0)
2892 InsetTabular::~InsetTabular()
2894 InsetTabularMailer(*this).hideDialog();
2898 Inset * InsetTabular::clone() const
2900 return new InsetTabular(*this);
2904 Buffer const & InsetTabular::buffer() const
2910 void InsetTabular::buffer(Buffer const * b)
2916 void InsetTabular::write(Buffer const & buf, ostream & os) const
2918 os << "Tabular" << endl;
2919 tabular.write(buf, os);
2923 void InsetTabular::read(Buffer const & buf, Lexer & lex)
2925 bool const old_format = (lex.getString() == "\\LyXTable");
2927 tabular.read(buf, lex);
2933 string token = lex.getString();
2934 while (lex && token != "\\end_inset") {
2936 token = lex.getString();
2939 lex.printError("Missing \\end_inset at this point. "
2945 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2947 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2948 // mi.base.textwidth << "\n";
2950 lyxerr << "InsetTabular::metrics: need bv" << endl;
2951 BOOST_ASSERT(false);
2955 for (idx_type cell = 0; i < tabular.rowCount(); ++i) {
2958 for (col_type j = 0; j < tabular.columnCount(); ++j) {
2959 if (tabular.isPartOfMultiColumn(i, j))
2960 // Multicolumn cell, but not first one
2965 if (tabular.cell_info[i][j].multicolumn ==
2966 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2967 p_width = tabular.cellinfo_of_cell(cell).p_width;
2969 p_width = tabular.column_info[j].p_width;
2970 if (!p_width.zero())
2971 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2972 tabular.getCellInset(cell)->metrics(m, dim);
2973 if (!p_width.zero())
2974 dim.wid = m.base.textwidth;
2975 tabular.setCellWidth(cell, dim.wid);
2976 if (p_width.zero()) {
2977 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
2978 // FIXME there must be a way to get rid of
2979 // the second metrics call
2980 tabular.getCellInset(cell)->metrics(m, dim);
2982 maxAsc = max(maxAsc, dim.asc);
2983 maxDesc = max(maxDesc, dim.des);
2986 int const top_space = tabular.row_info[i].top_space_default ?
2987 default_line_space :
2988 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2989 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2990 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2991 default_line_space :
2992 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2993 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2996 dim.asc = tabular.rowAscent(0);
2997 dim.des = tabular.height() - dim.asc;
2998 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3002 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3004 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3005 BufferView * bv = pi.base.bv;
3007 // FIXME: As the full backrgound is painted in drawSelection(),
3008 // we have no choice but to do a full repaint for the Text cells.
3009 pi.full_repaint = true;
3011 resetPos(bv->cursor());
3014 x += ADD_TO_TABULAR_WIDTH;
3016 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3019 first_visible_cell = Tabular::npos;
3020 for (row_type i = 0; i < tabular.rowCount(); ++i) {
3022 int const a = tabular.rowAscent(i);
3023 int const d = tabular.rowDescent(i);
3024 idx = tabular.cellIndex(i, 0);
3025 for (col_type j = 0; j < tabular.columnCount(); ++j) {
3026 if (tabular.isPartOfMultiColumn(i, j))
3028 if (first_visible_cell == Tabular::npos)
3029 first_visible_cell = idx;
3031 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3032 // Cache the Inset position.
3033 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3034 if (nx + tabular.columnWidth(idx) < 0
3035 || nx > bv->workWidth()
3037 || y - a > bv->workHeight()) {
3038 pi.pain.setDrawingEnabled(false);
3039 cell(idx)->draw(pi, cx, y);
3040 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3041 pi.pain.setDrawingEnabled(original_drawing_state);
3043 cell(idx)->draw(pi, cx, y);
3044 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3046 nx += tabular.columnWidth(idx);
3050 if (i + 1 < tabular.rowCount())
3051 y += d + tabular.rowAscent(i + 1) +
3052 tabular.getAdditionalHeight(i + 1);
3057 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3059 Cursor & cur = pi.base.bv->cursor();
3061 x += scx_ + ADD_TO_TABULAR_WIDTH;
3063 // FIXME: it is wrong to completely paint the background
3064 // if we want to do single row painting.
3066 // Paint background of current tabular
3067 int const w = tabular.width();
3068 int const h = tabular.height();
3069 int yy = y - tabular.rowAscent(0);
3070 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3072 if (!cur.selection())
3074 if (&cur.inset() != this)
3080 if (tablemode(cur)) {
3083 getSelection(cur, rs, re, cs, ce);
3084 y -= tabular.rowAscent(0);
3085 for (row_type j = 0; j < tabular.rowCount(); ++j) {
3086 int const a = tabular.rowAscent(j);
3087 int const h = a + tabular.rowDescent(j);
3089 y += tabular.getAdditionalHeight(j);
3090 for (col_type i = 0; i < tabular.columnCount(); ++i) {
3091 if (tabular.isPartOfMultiColumn(j, i))
3093 idx_type const cell =
3094 tabular.cellIndex(j, i);
3095 int const w = tabular.columnWidth(cell);
3096 if (i >= cs && i <= ce && j >= rs && j <= re)
3097 pi.pain.fillRectangle(xx, y, w, h,
3105 x += getCellXPos(cur.idx());
3106 x += tabular.getBeginningOfTextInCell(cur.idx());
3107 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3112 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3113 row_type row, idx_type cell, bool erased) const
3115 int x2 = x + tabular.columnWidth(cell);
3116 bool on_off = false;
3117 ColorCode col = Color_tabularline;
3118 ColorCode onoffcol = Color_tabularonoffline;
3121 col = Color_deletedtext;
3122 onoffcol = Color_deletedtext;
3125 if (!tabular.topAlreadyDrawn(cell)) {
3126 on_off = !tabular.topLine(cell);
3127 pain.line(x, y - tabular.rowAscent(row),
3128 x2, y - tabular.rowAscent(row),
3129 on_off ? onoffcol : col,
3130 on_off ? Painter::line_onoffdash : Painter::line_solid);
3132 on_off = !tabular.bottomLine(cell);
3133 pain.line(x, y + tabular.rowDescent(row),
3134 x2, y + tabular.rowDescent(row),
3135 on_off ? onoffcol : col,
3136 on_off ? Painter::line_onoffdash : Painter::line_solid);
3137 if (!tabular.leftAlreadyDrawn(cell)) {
3138 on_off = !tabular.leftLine(cell);
3139 pain.line(x, y - tabular.rowAscent(row),
3140 x, y + tabular.rowDescent(row),
3141 on_off ? onoffcol : col,
3142 on_off ? Painter::line_onoffdash : Painter::line_solid);
3144 on_off = !tabular.rightLine(cell);
3145 pain.line(x2 - tabular.getAdditionalWidth(cell),
3146 y - tabular.rowAscent(row),
3147 x2 - tabular.getAdditionalWidth(cell),
3148 y + tabular.rowDescent(row),
3149 on_off ? onoffcol : col,
3150 on_off ? Painter::line_onoffdash : Painter::line_solid);
3154 docstring const InsetTabular::editMessage() const
3156 return _("Opened table");
3160 void InsetTabular::edit(Cursor & cur, bool left)
3162 //lyxerr << "InsetTabular::edit: " << this << endl;
3164 cur.selection() = false;
3167 if (isRightToLeft(cur))
3168 cur.idx() = tabular.getLastCellInRow(0);
3174 if (isRightToLeft(cur))
3175 cur.idx() = tabular.getFirstCellInRow(tabular.rowCount() - 1);
3177 cur.idx() = tabular.cellCount() - 1;
3179 cur.pos() = cur.lastpos(); // FIXME crude guess
3181 // FIXME: this accesses the position cache before it is initialized
3183 //cur.bv().fitCursor();
3187 void InsetTabular::updateLabels(Buffer const & buf, ParIterator const & it)
3189 // In a longtable, tell captions what the current float is
3190 Counters & cnts = buf.params().getTextClass().counters();
3191 string const saveflt = cnts.current_float();
3192 if (tabular.isLongTabular())
3193 cnts.current_float("table");
3195 ParIterator it2 = it;
3197 size_t const end = it2.nargs();
3198 for ( ; it2.idx() < end; it2.top().forwardIdx())
3199 lyx::updateLabels(buf, it2);
3202 if (tabular.isLongTabular())
3203 cnts.current_float(saveflt);
3207 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3209 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
3210 << "\n cur:" << cur << endl;
3211 CursorSlice sl = cur.top();
3212 Cursor & bvcur = cur.bv().cursor();
3214 switch (cmd.action) {
3216 case LFUN_MOUSE_PRESS:
3217 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3219 // do not reset cursor/selection if we have selected
3220 // some cells (bug 2715).
3221 if (cmd.button() == mouse_button::button3
3222 && &bvcur.selBegin().inset() == this
3223 && tablemode(bvcur))
3226 // Let InsetText do it
3227 cell(cur.idx())->dispatch(cur, cmd);
3229 case LFUN_MOUSE_MOTION:
3230 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3231 if (cmd.button() == mouse_button::button1) {
3232 // only accept motions to places not deeper nested than the real anchor
3233 if (bvcur.anchor_.hasPart(cur)) {
3234 // only update if selection changes
3235 if (bvcur.idx() == cur.idx() &&
3236 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3238 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3239 bvcur.setCursor(cur);
3240 bvcur.selection() = true;
3246 case LFUN_MOUSE_RELEASE:
3247 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
3248 if (cmd.button() == mouse_button::button3)
3249 InsetTabularMailer(*this).showDialog(&cur.bv());
3252 case LFUN_CELL_BACKWARD:
3254 cur.selection() = false;
3257 case LFUN_CELL_FORWARD:
3259 cur.selection() = false;
3262 case LFUN_CHAR_FORWARD_SELECT:
3263 case LFUN_CHAR_FORWARD:
3264 cell(cur.idx())->dispatch(cur, cmd);
3265 if (!cur.result().dispatched()) {
3267 if (sl == cur.top())
3268 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3274 case LFUN_CHAR_BACKWARD_SELECT:
3275 case LFUN_CHAR_BACKWARD:
3276 cell(cur.idx())->dispatch(cur, cmd);
3277 if (!cur.result().dispatched()) {
3279 if (sl == cur.top())
3280 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
3286 case LFUN_CHAR_RIGHT_SELECT:
3287 case LFUN_CHAR_RIGHT:
3288 //FIXME: for visual cursor, really move right
3289 if (isRightToLeft(cur))
3290 lyx::dispatch(FuncRequest(
3291 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3292 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3294 lyx::dispatch(FuncRequest(
3295 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
3296 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3299 case LFUN_CHAR_LEFT_SELECT:
3300 case LFUN_CHAR_LEFT:
3301 //FIXME: for visual cursor, really move left
3302 if (isRightToLeft(cur))
3303 lyx::dispatch(FuncRequest(
3304 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3305 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
3307 lyx::dispatch(FuncRequest(
3308 cmd.action == LFUN_CHAR_LEFT_SELECT ?
3309 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
3312 case LFUN_DOWN_SELECT:
3314 cell(cur.idx())->dispatch(cur, cmd);
3315 cur.dispatched(); // override the cell's decision
3316 if (sl == cur.top())
3317 // if our Text didn't do anything to the cursor
3318 // then we try to put the cursor into the cell below
3319 // setting also the right targetX.
3320 if (tabular.cellRow(cur.idx()) != tabular.rowCount() - 1) {
3321 cur.idx() = tabular.getCellBelow(cur.idx());
3323 TextMetrics const & tm =
3324 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3325 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3327 if (sl == cur.top()) {
3328 // we trick it to go to forward after leaving the
3330 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3335 case LFUN_UP_SELECT:
3337 cell(cur.idx())->dispatch(cur, cmd);
3338 cur.dispatched(); // override the cell's decision
3339 if (sl == cur.top())
3340 // if our Text didn't do anything to the cursor
3341 // then we try to put the cursor into the cell above
3342 // setting also the right targetX.
3343 if (tabular.cellRow(cur.idx()) != 0) {
3344 cur.idx() = tabular.getCellAbove(cur.idx());
3345 cur.pit() = cur.lastpit();
3346 Text const * text = cell(cur.idx())->getText(0);
3347 TextMetrics const & tm = cur.bv().textMetrics(text);
3348 ParagraphMetrics const & pm =
3349 tm.parMetrics(cur.lastpit());
3350 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3352 if (sl == cur.top()) {
3353 cmd = FuncRequest(LFUN_UP);
3358 // case LFUN_SCREEN_DOWN: {
3359 // //if (hasSelection())
3360 // // cur.selection() = false;
3361 // col_type const col = tabular.cellColumn(cur.idx());
3362 // int const t = cur.bv().top_y() + cur.bv().height();
3363 // if (t < yo() + tabular.getHeightOfTabular()) {
3364 // cur.bv().scrollDocView(t);
3365 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3367 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3374 // case LFUN_SCREEN_UP: {
3375 // //if (hasSelection())
3376 // // cur.selection() = false;
3377 // col_type const col = tabular.cellColumn(cur.idx());
3378 // int const t = cur.bv().top_y() + cur.bv().height();
3380 // cur.bv().scrollDocView(t);
3384 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
3388 // cur.par() = cur.lastpar();
3389 // cur.pos() = cur.lastpos();
3393 case LFUN_LAYOUT_TABULAR:
3394 InsetTabularMailer(*this).showDialog(&cur.bv());
3397 case LFUN_INSET_DIALOG_UPDATE:
3398 InsetTabularMailer(*this).updateDialog(&cur.bv());
3401 case LFUN_TABULAR_FEATURE:
3402 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3406 // insert file functions
3407 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3408 case LFUN_FILE_INSERT_PLAINTEXT: {
3410 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
3411 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
3417 if (tablemode(cur)) {
3418 if (copySelection(cur)) {
3419 cur.recordUndoInset(DELETE_UNDO);
3424 cell(cur.idx())->dispatch(cur, cmd);
3427 case LFUN_CHAR_DELETE_BACKWARD:
3428 case LFUN_CHAR_DELETE_FORWARD:
3429 if (tablemode(cur)) {
3430 cur.recordUndoInset(DELETE_UNDO);
3434 cell(cur.idx())->dispatch(cur, cmd);
3438 if (!cur.selection())
3440 if (tablemode(cur)) {
3444 cell(cur.idx())->dispatch(cur, cmd);
3447 case LFUN_CLIPBOARD_PASTE:
3448 case LFUN_PRIMARY_SELECTION_PASTE: {
3449 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3450 theClipboard().getAsText() :
3451 theSelection().get();
3454 // pass to InsertPlaintextString, but
3455 // only if we have multi-cell content
3456 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3457 if (insertPlaintextString(cur.bv(), clip, false)) {
3458 // content has been replaced,
3459 // so cursor might be invalid
3460 cur.pos() = cur.lastpos();
3461 bvcur.setCursor(cur);
3465 // Let the cell handle normal text
3466 cell(cur.idx())->dispatch(cur, cmd);
3471 if (tabularStackDirty() && theClipboard().isInternal()) {
3472 cur.recordUndoInset(INSERT_UNDO);
3473 pasteClipboard(cur);
3476 cell(cur.idx())->dispatch(cur, cmd);
3479 case LFUN_FONT_EMPH:
3480 case LFUN_FONT_BOLD:
3481 case LFUN_FONT_ROMAN:
3482 case LFUN_FONT_NOUN:
3483 case LFUN_FONT_ITAL:
3484 case LFUN_FONT_FRAK:
3485 case LFUN_FONT_TYPEWRITER:
3486 case LFUN_FONT_SANS:
3487 case LFUN_FONT_FREE_APPLY:
3488 case LFUN_FONT_FREE_UPDATE:
3489 case LFUN_FONT_SIZE:
3490 case LFUN_FONT_UNDERLINE:
3492 case LFUN_WORD_CAPITALIZE:
3493 case LFUN_WORD_UPCASE:
3494 case LFUN_WORD_LOWCASE:
3495 case LFUN_CHARS_TRANSPOSE:
3496 if (tablemode(cur)) {
3499 getSelection(cur, rs, re, cs, ce);
3500 Cursor tmpcur = cur;
3501 for (row_type i = rs; i <= re; ++i) {
3502 for (col_type j = cs; j <= ce; ++j) {
3503 // cursor follows cell:
3504 tmpcur.idx() = tabular.cellIndex(i, j);
3505 // select this cell only:
3508 tmpcur.resetAnchor();
3509 tmpcur.pit() = tmpcur.lastpit();
3510 tmpcur.pos() = tmpcur.top().lastpos();
3511 tmpcur.setCursor(tmpcur);
3512 tmpcur.setSelection();
3513 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3518 cell(cur.idx())->dispatch(cur, cmd);
3522 // we try to handle this event in the insets dispatch function.
3523 cell(cur.idx())->dispatch(cur, cmd);
3529 // function sets an object as defined in func_status.h:
3530 // states OK, Unknown, Disabled, On, Off.
3531 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3532 FuncStatus & status) const
3534 switch (cmd.action) {
3535 case LFUN_TABULAR_FEATURE: {
3536 int action = Tabular::LAST_ACTION;
3538 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3539 string const tmp = tabularFeature[i].feature;
3540 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3541 action = tabularFeature[i].action;
3545 if (action == Tabular::LAST_ACTION) {
3547 status.unknown(true);
3551 string const argument
3552 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3554 row_type sel_row_start = 0;
3555 row_type sel_row_end = 0;
3556 col_type sel_col_start = 0;
3557 col_type sel_col_end = 0;
3558 Tabular::ltType dummyltt;
3561 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3564 case Tabular::SET_PWIDTH:
3565 case Tabular::SET_MPWIDTH:
3566 case Tabular::SET_SPECIAL_COLUMN:
3567 case Tabular::SET_SPECIAL_MULTI:
3568 case Tabular::APPEND_ROW:
3569 case Tabular::APPEND_COLUMN:
3570 case Tabular::DELETE_ROW:
3571 case Tabular::DELETE_COLUMN:
3572 case Tabular::COPY_ROW:
3573 case Tabular::COPY_COLUMN:
3574 case Tabular::SET_ALL_LINES:
3575 case Tabular::UNSET_ALL_LINES:
3576 case Tabular::SET_TOP_SPACE:
3577 case Tabular::SET_BOTTOM_SPACE:
3578 case Tabular::SET_INTERLINE_SPACE:
3582 case Tabular::MULTICOLUMN:
3583 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3586 case Tabular::M_TOGGLE_LINE_TOP:
3588 case Tabular::TOGGLE_LINE_TOP:
3589 status.setOnOff(tabular.topLine(cur.idx(), flag));
3592 case Tabular::M_TOGGLE_LINE_BOTTOM:
3594 case Tabular::TOGGLE_LINE_BOTTOM:
3595 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
3598 case Tabular::M_TOGGLE_LINE_LEFT:
3600 case Tabular::TOGGLE_LINE_LEFT:
3601 status.setOnOff(tabular.leftLine(cur.idx(), flag));
3604 case Tabular::M_TOGGLE_LINE_RIGHT:
3606 case Tabular::TOGGLE_LINE_RIGHT:
3607 status.setOnOff(tabular.rightLine(cur.idx(), flag));
3610 case Tabular::M_ALIGN_LEFT:
3612 case Tabular::ALIGN_LEFT:
3613 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3616 case Tabular::M_ALIGN_RIGHT:
3618 case Tabular::ALIGN_RIGHT:
3619 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3622 case Tabular::M_ALIGN_CENTER:
3624 case Tabular::ALIGN_CENTER:
3625 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3628 case Tabular::ALIGN_BLOCK:
3629 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3630 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3633 case Tabular::M_VALIGN_TOP:
3635 case Tabular::VALIGN_TOP:
3637 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3640 case Tabular::M_VALIGN_BOTTOM:
3642 case Tabular::VALIGN_BOTTOM:
3644 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3647 case Tabular::M_VALIGN_MIDDLE:
3649 case Tabular::VALIGN_MIDDLE:
3651 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3654 case Tabular::SET_LONGTABULAR:
3655 status.setOnOff(tabular.isLongTabular());
3658 case Tabular::UNSET_LONGTABULAR:
3659 status.setOnOff(!tabular.isLongTabular());
3662 case Tabular::TOGGLE_ROTATE_TABULAR:
3663 case Tabular::SET_ROTATE_TABULAR:
3664 status.setOnOff(tabular.getRotateTabular());
3667 case Tabular::UNSET_ROTATE_TABULAR:
3668 status.setOnOff(!tabular.getRotateTabular());
3671 case Tabular::TOGGLE_ROTATE_CELL:
3672 case Tabular::SET_ROTATE_CELL:
3673 status.setOnOff(!oneCellHasRotationState(false,
3674 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3677 case Tabular::UNSET_ROTATE_CELL:
3678 status.setOnOff(!oneCellHasRotationState(true,
3679 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3682 case Tabular::SET_USEBOX:
3683 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3686 case Tabular::SET_LTFIRSTHEAD:
3687 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3690 case Tabular::UNSET_LTFIRSTHEAD:
3691 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3694 case Tabular::SET_LTHEAD:
3695 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3698 case Tabular::UNSET_LTHEAD:
3699 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3702 case Tabular::SET_LTFOOT:
3703 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3706 case Tabular::UNSET_LTFOOT:
3707 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3710 case Tabular::SET_LTLASTFOOT:
3711 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3714 case Tabular::UNSET_LTLASTFOOT:
3715 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3718 case Tabular::SET_LTNEWPAGE:
3719 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3722 case Tabular::SET_BOOKTABS:
3723 status.setOnOff(tabular.useBookTabs());
3726 case Tabular::UNSET_BOOKTABS:
3727 status.setOnOff(!tabular.useBookTabs());
3732 status.enabled(false);
3738 // These are only enabled inside tabular
3739 case LFUN_CELL_BACKWARD:
3740 case LFUN_CELL_FORWARD:
3741 status.enabled(true);
3744 // disable these with multiple cells selected
3745 case LFUN_INSET_INSERT:
3746 case LFUN_TABULAR_INSERT:
3747 case LFUN_FLEX_INSERT:
3748 case LFUN_FLOAT_INSERT:
3749 case LFUN_FLOAT_WIDE_INSERT:
3750 case LFUN_FOOTNOTE_INSERT:
3751 case LFUN_MARGINALNOTE_INSERT:
3752 case LFUN_MATH_INSERT:
3753 case LFUN_MATH_MODE:
3754 case LFUN_MATH_MUTATE:
3755 case LFUN_MATH_DISPLAY:
3756 case LFUN_NOTE_INSERT:
3757 case LFUN_OPTIONAL_INSERT:
3758 case LFUN_BOX_INSERT:
3759 case LFUN_BRANCH_INSERT:
3760 case LFUN_WRAP_INSERT:
3761 case LFUN_ERT_INSERT: {
3762 if (tablemode(cur)) {
3763 status.enabled(false);
3766 return cell(cur.idx())->getStatus(cur, cmd, status);
3769 // disable in non-fixed-width cells
3770 case LFUN_BREAK_LINE:
3771 case LFUN_BREAK_PARAGRAPH:
3772 case LFUN_BREAK_PARAGRAPH_SKIP: {
3773 if (tabular.getPWidth(cur.idx()).zero()) {
3774 status.enabled(false);
3777 return cell(cur.idx())->getStatus(cur, cmd, status);
3781 if (tabularStackDirty() && theClipboard().isInternal()) {
3782 status.enabled(true);
3785 return cell(cur.idx())->getStatus(cur, cmd, status);
3787 case LFUN_INSET_MODIFY:
3788 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3789 status.enabled(true);
3795 // we try to handle this event in the insets dispatch function.
3796 return cell(cur.idx())->getStatus(cur, cmd, status);
3801 int InsetTabular::latex(Buffer const & buf, odocstream & os,
3802 OutputParams const & runparams) const
3804 return tabular.latex(buf, os, runparams);
3808 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
3809 OutputParams const & runparams) const
3811 os << '\n'; // output table on a new line
3812 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3813 tabular.plaintext(buf, os, runparams, dp, false, 0);
3814 return PLAINTEXT_NEWLINE;
3818 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
3819 OutputParams const & runparams) const
3824 // FIXME: Why not pass a proper DocIterator here?
3826 // if the table is inside a float it doesn't need the informaltable
3827 // wrapper. Search for it.
3828 for (master = owner(); master; master = master->owner())
3829 if (master->lyxCode() == FLOAT_CODE)
3834 os << "<informaltable>";
3837 ret += tabular.docbook(buf, os, runparams);
3839 os << "</informaltable>";
3846 void InsetTabular::validate(LaTeXFeatures & features) const
3848 tabular.validate(features);
3852 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
3854 return tabular.getCellInset(idx);
3858 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
3860 return tabular.getCellInset(idx);
3864 void InsetTabular::cursorPos(BufferView const & bv,
3865 CursorSlice const & sl, bool boundary, int & x, int & y) const
3867 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3869 // y offset correction
3870 int const row = tabular.cellRow(sl.idx());
3871 for (int i = 0; i <= row; ++i) {
3873 y += tabular.rowAscent(i);
3874 y += tabular.getAdditionalHeight(i);
3877 y += tabular.rowDescent(i);
3880 // x offset correction
3881 int const col = tabular.cellColumn(sl.idx());
3882 int idx = tabular.cellIndex(row, 0);
3883 for (int j = 0; j < col; ++j) {
3884 if (tabular.isPartOfMultiColumn(row, j))
3886 x += tabular.columnWidth(idx);
3889 x += tabular.getBeginningOfTextInCell(idx);
3890 x += ADD_TO_TABULAR_WIDTH;
3895 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3899 Inset const & inset = *tabular.getCellInset(cell);
3900 Point o = bv.coordCache().getInsets().xy(&inset);
3901 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3902 int const xend = xbeg + tabular.columnWidth(cell);
3903 row_type const row = tabular.cellRow(cell);
3904 int const ybeg = o.y_ - tabular.rowAscent(row) -
3905 tabular.getAdditionalHeight(row);
3906 int const yend = o.y_ + tabular.rowDescent(row);
3918 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3919 // << " ybeg=" << ybeg << " yend=" << yend
3920 // << " xx=" << xx << " yy=" << yy
3921 // << " dist=" << xx + yy << endl;
3926 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3928 //lyxerr << "InsetTabular::editXY: " << this << endl;
3929 cur.selection() = false;
3931 cur.idx() = getNearestCell(cur.bv(), x, y);
3933 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3937 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3939 cur.idx() = getNearestCell(cur.bv(), x, y);
3940 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3944 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3946 idx_type idx_min = 0;
3947 int dist_min = std::numeric_limits<int>::max();
3948 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3949 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
3950 int const d = dist(bv, i, x, y);
3961 int InsetTabular::getCellXPos(idx_type const cell) const
3965 for (; !tabular.isFirstCellInRow(c); --c)
3968 for (; c < cell; ++c)
3969 lx += tabular.columnWidth(c);
3975 void InsetTabular::resetPos(Cursor & cur) const
3977 BufferView & bv = cur.bv();
3978 int const maxwidth = bv.workWidth();
3980 if (&cur.inset() != this) {
3984 int const X2 = maxwidth;
3985 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3986 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
3987 int const x2 = x1 + tabular.columnWidth(cur.idx());
3990 scx_ = X1 + 20 - x1;
3992 scx_ = X2 - 20 - x2;
3997 cur.updateFlags(Update::Force | Update::FitCursor);
4001 void InsetTabular::moveNextCell(Cursor & cur)
4003 if (isRightToLeft(cur)) {
4004 if (tabular.isFirstCellInRow(cur.idx())) {
4005 row_type const row = tabular.cellRow(cur.idx());
4006 if (row == tabular.rowCount() - 1)
4008 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
4015 if (tabular.isLastCell(cur.idx()))
4025 void InsetTabular::movePrevCell(Cursor & cur)
4027 if (isRightToLeft(cur)) {
4028 if (tabular.isLastCellInRow(cur.idx())) {
4029 row_type const row = tabular.cellRow(cur.idx());
4032 cur.idx() = tabular.getFirstCellInRow(row);
4033 cur.idx() = tabular.getCellAbove(cur.idx());
4035 if (tabular.isLastCell(cur.idx()))
4040 if (cur.idx() == 0) // first cell
4044 cur.pit() = cur.lastpit();
4045 cur.pos() = cur.lastpos();
4047 // FIXME: this accesses the position cache before it is initialized
4052 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4054 Tabular::Feature action = Tabular::LAST_ACTION;
4057 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4058 string const tmp = tabularFeature[i].feature;
4060 if (tmp == what.substr(0, tmp.length())) {
4061 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4062 //tabularFeatures[i].feature.length()))
4063 action = tabularFeature[i].action;
4067 if (action == Tabular::LAST_ACTION)
4071 ltrim(what.substr(tabularFeature[i].feature.length()));
4072 tabularFeatures(cur, action, val);
4077 static void checkLongtableSpecial(Tabular::ltType & ltt,
4078 string const & special, bool & flag)
4080 if (special == "dl_above") {
4083 } else if (special == "dl_below") {
4084 ltt.bottomDL = flag;
4086 } else if (special == "empty") {
4095 bool InsetTabular::oneCellHasRotationState(bool rotated,
4096 row_type row_start, row_type row_end,
4097 col_type col_start, col_type col_end) const {
4099 for (row_type i = row_start; i <= row_end; ++i) {
4100 for (col_type j = col_start; j <= col_end; ++j) {
4101 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4110 void InsetTabular::tabularFeatures(Cursor & cur,
4111 Tabular::Feature feature, string const & value)
4113 BufferView & bv = cur.bv();
4114 col_type sel_col_start;
4115 col_type sel_col_end;
4116 row_type sel_row_start;
4117 row_type sel_row_end;
4118 bool setLines = false;
4119 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4120 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4124 case Tabular::M_ALIGN_LEFT:
4125 case Tabular::ALIGN_LEFT:
4126 setAlign = LYX_ALIGN_LEFT;
4129 case Tabular::M_ALIGN_RIGHT:
4130 case Tabular::ALIGN_RIGHT:
4131 setAlign = LYX_ALIGN_RIGHT;
4134 case Tabular::M_ALIGN_CENTER:
4135 case Tabular::ALIGN_CENTER:
4136 setAlign = LYX_ALIGN_CENTER;
4139 case Tabular::ALIGN_BLOCK:
4140 setAlign = LYX_ALIGN_BLOCK;
4143 case Tabular::M_VALIGN_TOP:
4144 case Tabular::VALIGN_TOP:
4145 setVAlign = Tabular::LYX_VALIGN_TOP;
4148 case Tabular::M_VALIGN_BOTTOM:
4149 case Tabular::VALIGN_BOTTOM:
4150 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4153 case Tabular::M_VALIGN_MIDDLE:
4154 case Tabular::VALIGN_MIDDLE:
4155 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4162 cur.recordUndoInset(ATOMIC_UNDO);
4164 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4165 row_type const row = tabular.cellRow(cur.idx());
4166 col_type const column = tabular.cellColumn(cur.idx());
4168 Tabular::ltType ltt;
4172 case Tabular::SET_PWIDTH: {
4173 Length const len(value);
4174 tabular.setColumnPWidth(cur, cur.idx(), len);
4176 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4177 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4181 case Tabular::SET_MPWIDTH:
4182 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4185 case Tabular::SET_SPECIAL_COLUMN:
4186 case Tabular::SET_SPECIAL_MULTI:
4187 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4190 case Tabular::APPEND_ROW:
4191 // append the row into the tabular
4192 tabular.appendRow(bv.buffer().params(), cur.idx());
4195 case Tabular::APPEND_COLUMN:
4196 // append the column into the tabular
4197 tabular.appendColumn(bv.buffer().params(), cur.idx());
4198 cur.idx() = tabular.cellIndex(row, column);
4201 case Tabular::DELETE_ROW:
4202 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4203 tabular.deleteRow(sel_row_start);
4204 if (sel_row_start >= tabular.rowCount())
4206 cur.idx() = tabular.cellIndex(sel_row_start, column);
4209 cur.selection() = false;
4212 case Tabular::DELETE_COLUMN:
4213 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4214 tabular.deleteColumn(sel_col_start);
4215 if (sel_col_start >= tabular.columnCount())
4217 cur.idx() = tabular.cellIndex(row, sel_col_start);
4220 cur.selection() = false;
4223 case Tabular::COPY_ROW:
4224 tabular.copyRow(bv.buffer().params(), row);
4227 case Tabular::COPY_COLUMN:
4228 tabular.copyColumn(bv.buffer().params(), column);
4229 cur.idx() = tabular.cellIndex(row, column);
4232 case Tabular::M_TOGGLE_LINE_TOP:
4234 case Tabular::TOGGLE_LINE_TOP: {
4235 bool lineSet = !tabular.topLine(cur.idx(), flag);
4236 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4237 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4239 tabular.cellIndex(i, j),
4244 case Tabular::M_TOGGLE_LINE_BOTTOM:
4246 case Tabular::TOGGLE_LINE_BOTTOM: {
4247 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
4248 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4249 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4250 tabular.setBottomLine(
4251 tabular.cellIndex(i, j),
4257 case Tabular::M_TOGGLE_LINE_LEFT:
4259 case Tabular::TOGGLE_LINE_LEFT: {
4260 bool lineSet = !tabular.leftLine(cur.idx(), flag);
4261 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4262 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4263 tabular.setLeftLine(
4264 tabular.cellIndex(i,j),
4270 case Tabular::M_TOGGLE_LINE_RIGHT:
4272 case Tabular::TOGGLE_LINE_RIGHT: {
4273 bool lineSet = !tabular.rightLine(cur.idx(), flag);
4274 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4275 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4276 tabular.setRightLine(
4277 tabular.cellIndex(i,j),
4283 case Tabular::M_ALIGN_LEFT:
4284 case Tabular::M_ALIGN_RIGHT:
4285 case Tabular::M_ALIGN_CENTER:
4287 case Tabular::ALIGN_LEFT:
4288 case Tabular::ALIGN_RIGHT:
4289 case Tabular::ALIGN_CENTER:
4290 case Tabular::ALIGN_BLOCK:
4291 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4292 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4293 tabular.setAlignment(
4294 tabular.cellIndex(i, j),
4299 case Tabular::M_VALIGN_TOP:
4300 case Tabular::M_VALIGN_BOTTOM:
4301 case Tabular::M_VALIGN_MIDDLE:
4303 case Tabular::VALIGN_TOP:
4304 case Tabular::VALIGN_BOTTOM:
4305 case Tabular::VALIGN_MIDDLE:
4306 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4307 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4308 tabular.setVAlignment(
4309 tabular.cellIndex(i, j),
4313 case Tabular::MULTICOLUMN: {
4314 if (sel_row_start != sel_row_end) {
4315 // FIXME: Need I say it ? This is horrible.
4317 Alert::error(_("Error setting multicolumn"),
4318 _("You cannot set multicolumn vertically."));
4321 if (!cur.selection()) {
4322 // just multicol for one single cell
4323 // check whether we are completely in a multicol
4324 if (tabular.isMultiColumn(cur.idx()))
4325 tabular.unsetMultiColumn(cur.idx());
4327 tabular.setMultiColumn(&bv.buffer(), cur.idx(), 1);
4330 // we have a selection so this means we just add all this
4331 // cells to form a multicolumn cell
4332 idx_type const s_start = cur.selBegin().idx();
4333 idx_type const s_end = cur.selEnd().idx();
4334 tabular.setMultiColumn(&bv.buffer(), s_start, s_end - s_start + 1);
4335 cur.idx() = s_start;
4338 cur.selection() = false;
4342 case Tabular::SET_ALL_LINES:
4344 case Tabular::UNSET_ALL_LINES:
4345 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4346 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4347 tabular.setAllLines(
4348 tabular.cellIndex(i,j), setLines);
4351 case Tabular::SET_LONGTABULAR:
4352 tabular.setLongTabular(true);
4355 case Tabular::UNSET_LONGTABULAR:
4356 tabular.setLongTabular(false);
4359 case Tabular::SET_ROTATE_TABULAR:
4360 tabular.setRotateTabular(true);
4363 case Tabular::UNSET_ROTATE_TABULAR:
4364 tabular.setRotateTabular(false);
4367 case Tabular::TOGGLE_ROTATE_TABULAR:
4368 tabular.setRotateTabular(!tabular.getRotateTabular());
4371 case Tabular::SET_ROTATE_CELL:
4372 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4373 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4374 tabular.setRotateCell(
4375 tabular.cellIndex(i, j), true);
4378 case Tabular::UNSET_ROTATE_CELL:
4379 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4380 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4381 tabular.setRotateCell(
4382 tabular.cellIndex(i, j), false);
4385 case Tabular::TOGGLE_ROTATE_CELL:
4387 bool oneNotRotated = oneCellHasRotationState(false,
4388 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4390 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4391 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4392 tabular.setRotateCell(tabular.cellIndex(i, j),
4397 case Tabular::SET_USEBOX: {
4398 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4399 if (val == tabular.getUsebox(cur.idx()))
4400 val = Tabular::BOX_NONE;
4401 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4402 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4403 tabular.setUsebox(tabular.cellIndex(i, j), val);
4407 case Tabular::UNSET_LTFIRSTHEAD:
4409 case Tabular::SET_LTFIRSTHEAD:
4410 tabular.getRowOfLTFirstHead(row, ltt);
4411 checkLongtableSpecial(ltt, value, flag);
4412 tabular.setLTHead(row, flag, ltt, true);
4415 case Tabular::UNSET_LTHEAD:
4417 case Tabular::SET_LTHEAD:
4418 tabular.getRowOfLTHead(row, ltt);
4419 checkLongtableSpecial(ltt, value, flag);
4420 tabular.setLTHead(row, flag, ltt, false);
4423 case Tabular::UNSET_LTFOOT:
4425 case Tabular::SET_LTFOOT:
4426 tabular.getRowOfLTFoot(row, ltt);
4427 checkLongtableSpecial(ltt, value, flag);
4428 tabular.setLTFoot(row, flag, ltt, false);
4431 case Tabular::UNSET_LTLASTFOOT:
4433 case Tabular::SET_LTLASTFOOT:
4434 tabular.getRowOfLTLastFoot(row, ltt);
4435 checkLongtableSpecial(ltt, value, flag);
4436 tabular.setLTFoot(row, flag, ltt, true);
4439 case Tabular::SET_LTNEWPAGE:
4440 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4443 case Tabular::SET_BOOKTABS:
4444 tabular.setBookTabs(true);
4447 case Tabular::UNSET_BOOKTABS:
4448 tabular.setBookTabs(false);
4451 case Tabular::SET_TOP_SPACE: {
4453 if (value == "default")
4454 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4455 tabular.row_info[i].top_space_default = true;
4456 else if (isValidLength(value, &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;
4462 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4463 tabular.row_info[i].top_space_default = false;
4464 tabular.row_info[i].top_space = len;
4469 case Tabular::SET_BOTTOM_SPACE: {
4471 if (value == "default")
4472 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4473 tabular.row_info[i].bottom_space_default = true;
4474 else if (isValidLength(value, &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;
4480 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4481 tabular.row_info[i].bottom_space_default = false;
4482 tabular.row_info[i].bottom_space = len;
4487 case Tabular::SET_INTERLINE_SPACE: {
4489 if (value == "default")
4490 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4491 tabular.row_info[i].interline_space_default = true;
4492 else if (isValidLength(value, &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;
4498 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4499 tabular.row_info[i].interline_space_default = false;
4500 tabular.row_info[i].interline_space = len;
4505 // dummy stuff just to avoid warnings
4506 case Tabular::LAST_ACTION:
4512 bool InsetTabular::showInsetDialog(BufferView * bv) const
4514 InsetTabularMailer(*this).showDialog(bv);
4519 void InsetTabular::openLayoutDialog(BufferView * bv) const
4521 InsetTabularMailer(*this).showDialog(bv);
4525 bool InsetTabular::copySelection(Cursor & cur)
4527 if (!cur.selection())
4532 getSelection(cur, rs, re, cs, ce);
4534 paste_tabular.reset(new Tabular(tabular));
4536 for (row_type i = 0; i < rs; ++i)
4537 paste_tabular->deleteRow(0);
4539 row_type const rows = re - rs + 1;
4540 while (paste_tabular->rowCount() > rows)
4541 paste_tabular->deleteRow(rows);
4543 paste_tabular->setTopLine(0, true, true);
4544 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
4547 for (col_type i = 0; i < cs; ++i)
4548 paste_tabular->deleteColumn(0);
4550 col_type const columns = ce - cs + 1;
4551 while (paste_tabular->columnCount() > columns)
4552 paste_tabular->deleteColumn(columns);
4554 paste_tabular->setLeftLine(0, true, true);
4555 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
4558 odocstringstream os;
4559 OutputParams const runparams(0);
4560 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
4561 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4562 cap::copySelection(cur, os.str());
4564 // mark tabular stack dirty
4565 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4566 // when we (hopefully) have a one-for-all paste mechanism.
4567 // This must be called after cap::copySelection.
4568 dirtyTabularStack(true);
4574 bool InsetTabular::pasteClipboard(Cursor & cur)
4578 col_type const actcol = tabular.cellColumn(cur.idx());
4579 row_type const actrow = tabular.cellRow(cur.idx());
4580 for (row_type r1 = 0, r2 = actrow;
4581 r1 < paste_tabular->rowCount() && r2 < tabular.rowCount();
4583 for (col_type c1 = 0, c2 = actcol;
4584 c1 < paste_tabular->columnCount() && c2 < tabular.columnCount();
4586 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4587 tabular.isPartOfMultiColumn(r2, c2))
4589 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4593 if (tabular.isPartOfMultiColumn(r2, c2)) {
4597 shared_ptr<InsetText> inset(
4598 new InsetText(*paste_tabular->getCellInset(r1, c1)));
4599 tabular.setCellInset(r2, c2, inset);
4600 // FIXME: change tracking (MG)
4601 inset->setChange(Change(cur.buffer().params().trackChanges ?
4602 Change::INSERTED : Change::UNCHANGED));
4610 void InsetTabular::cutSelection(Cursor & cur)
4612 if (!cur.selection())
4617 getSelection(cur, rs, re, cs, ce);
4618 for (row_type i = rs; i <= re; ++i) {
4619 for (col_type j = cs; j <= ce; ++j) {
4620 shared_ptr<InsetText> t
4621 = cell(tabular.cellIndex(i, j));
4622 if (cur.buffer().params().trackChanges)
4623 // FIXME: Change tracking (MG)
4624 t->setChange(Change(Change::DELETED));
4630 // cursor position might be invalid now
4631 if (cur.pit() > cur.lastpit())
4632 cur.pit() = cur.lastpit();
4633 if (cur.pos() > cur.lastpos())
4634 cur.pos() = cur.lastpos();
4635 cur.clearSelection();
4639 bool InsetTabular::isRightToLeft(Cursor & cur) const
4641 BOOST_ASSERT(cur.depth() > 1);
4642 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4643 pos_type const parentpos = cur[cur.depth() - 2].pos();
4644 return parentpar.getFontSettings(cur.bv().buffer().params(),
4645 parentpos).language()->rightToLeft();
4649 void InsetTabular::getSelection(Cursor & cur,
4650 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4652 CursorSlice const & beg = cur.selBegin();
4653 CursorSlice const & end = cur.selEnd();
4654 cs = tabular.cellColumn(beg.idx());
4655 ce = tabular.cellColumn(end.idx());
4658 cs = tabular.cellColumn(end.idx());
4660 ce = tabular.cellRightColumn(end.idx());
4663 rs = tabular.cellRow(beg.idx());
4664 re = tabular.cellRow(end.idx());
4670 Text * InsetTabular::getText(int idx) const
4672 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4676 void InsetTabular::setChange(Change const & change)
4678 for (idx_type idx = 0; idx < nargs(); ++idx)
4679 cell(idx)->setChange(change);
4683 void InsetTabular::acceptChanges(BufferParams const & bparams)
4685 for (idx_type idx = 0; idx < nargs(); ++idx)
4686 cell(idx)->acceptChanges(bparams);
4690 void InsetTabular::rejectChanges(BufferParams const & bparams)
4692 for (idx_type idx = 0; idx < nargs(); ++idx)
4693 cell(idx)->rejectChanges(bparams);
4697 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
4699 return tabular.getPWidth(cell).zero();
4703 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4706 if (buf.length() <= 0)
4709 Buffer const & buffer = bv.buffer();
4713 col_type maxCols = 1;
4714 docstring::size_type const len = buf.length();
4715 docstring::size_type p = 0;
4718 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4726 maxCols = max(cols, maxCols);
4732 maxCols = max(cols, maxCols);
4738 paste_tabular.reset(
4739 new Tabular(buffer.params(), rows, maxCols));
4740 loctab = paste_tabular.get();
4742 dirtyTabularStack(true);
4745 cell = bv.cursor().idx();
4746 ocol = tabular.cellColumn(cell);
4747 row = tabular.cellRow(cell);
4750 docstring::size_type op = 0;
4751 idx_type const cells = loctab->cellCount();
4754 rows = loctab->rowCount();
4755 col_type const columns = loctab->columnCount();
4757 while (cell < cells && p < len && row < rows &&
4758 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4764 // we can only set this if we are not too far right
4765 if (cols < columns) {
4766 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4767 Font const font = bv.textMetrics(&inset->text_).
4768 getDisplayFont(0, 0);
4769 inset->setText(buf.substr(op, p - op), font,
4770 buffer.params().trackChanges);
4776 // we can only set this if we are not too far right
4777 if (cols < columns) {
4778 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
4779 Font const font = bv.textMetrics(&inset->text_).
4780 getDisplayFont(0, 0);
4781 inset->setText(buf.substr(op, p - op), font,
4782 buffer.params().trackChanges);
4787 cell = loctab->cellIndex(row, cols);
4793 // check for the last cell if there is no trailing '\n'
4794 if (cell < cells && op < len) {
4795 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
4796 Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0);
4797 inset->setText(buf.substr(op, len - op), font,
4798 buffer.params().trackChanges);
4804 void InsetTabular::addPreview(PreviewLoader & loader) const
4806 row_type const rows = tabular.rowCount();
4807 col_type const columns = tabular.columnCount();
4808 for (row_type i = 0; i < rows; ++i) {
4809 for (col_type j = 0; j < columns; ++j)
4810 tabular.getCellInset(i, j)->addPreview(loader);
4815 bool InsetTabular::tablemode(Cursor & cur) const
4817 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4823 string const InsetTabularMailer::name_("tabular");
4825 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
4826 : inset_(const_cast<InsetTabular &>(inset))
4830 string const InsetTabularMailer::inset2string(Buffer const &) const
4832 return params2string(inset_);
4836 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
4838 istringstream data(in);
4840 lex.setStream(data);
4847 if (!lex || token != name_)
4848 return print_mailer_error("InsetTabularMailer", in, 1,
4851 // This is part of the inset proper that is usually swallowed
4852 // by Buffer::readInset
4854 if (!lex || token != "Tabular")
4855 return print_mailer_error("InsetTabularMailer", in, 2,
4858 Buffer const & buffer = inset.buffer();
4859 inset.read(buffer, lex);
4863 string const InsetTabularMailer::params2string(InsetTabular const & inset)
4866 data << name_ << ' ';
4867 inset.write(inset.buffer(), data);
4868 data << "\\end_inset\n";