2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetList.h"
38 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "OutputParams.h"
44 #include "output_xhtml.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
48 #include "TextClass.h"
49 #include "TextMetrics.h"
51 #include "frontends/Application.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "support/convert.h"
58 #include "support/debug.h"
59 #include "support/docstream.h"
60 #include "support/FileName.h"
61 #include "support/gettext.h"
62 #include "support/lassert.h"
63 #include "support/lstrings.h"
65 #include <boost/scoped_ptr.hpp>
73 using namespace lyx::support;
79 using cap::dirtyTabularStack;
80 using cap::tabularStackDirty;
82 using graphics::PreviewLoader;
84 using frontend::Painter;
85 using frontend::Clipboard;
87 namespace Alert = frontend::Alert;
92 int const ADD_TO_HEIGHT = 2; // in cell
93 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
94 int const default_line_space = 10; // ?
95 int const WIDTH_OF_LINE = 5; // space between double lines
99 boost::scoped_ptr<Tabular> paste_tabular;
102 struct TabularFeature {
103 Tabular::Feature action;
109 TabularFeature tabularFeature[] =
111 // the SET/UNSET actions are used by the table dialog,
112 // the TOGGLE actions by the table toolbar buttons
113 { Tabular::APPEND_ROW, "append-row", false },
114 { Tabular::APPEND_COLUMN, "append-column", false },
115 { Tabular::DELETE_ROW, "delete-row", false },
116 { Tabular::DELETE_COLUMN, "delete-column", false },
117 { Tabular::COPY_ROW, "copy-row", false },
118 { Tabular::COPY_COLUMN, "copy-column", false },
119 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
120 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
121 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
122 { Tabular::MOVE_ROW_UP, "move-row-up", false },
123 { Tabular::SET_LINE_TOP, "set-line-top", true },
124 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
125 { Tabular::SET_LINE_LEFT, "set-line-left", true },
126 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
127 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
128 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
129 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
130 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
131 { Tabular::ALIGN_LEFT, "align-left", false },
132 { Tabular::ALIGN_RIGHT, "align-right", false },
133 { Tabular::ALIGN_CENTER, "align-center", false },
134 { Tabular::ALIGN_BLOCK, "align-block", false },
135 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
136 { Tabular::VALIGN_TOP, "valign-top", false },
137 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
138 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
139 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
141 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
142 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
145 { Tabular::MULTICOLUMN, "multicolumn", false },
146 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
147 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
148 { Tabular::MULTIROW, "multirow", false },
149 { Tabular::SET_MULTIROW, "set-multirow", false },
150 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
151 { Tabular::SET_MROFFSET, "set-mroffset", true },
152 { Tabular::SET_ALL_LINES, "set-all-lines", false },
153 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
154 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
155 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
156 { Tabular::SET_PWIDTH, "set-pwidth", true },
157 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
158 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
159 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
160 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
161 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
162 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
163 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
164 { Tabular::SET_USEBOX, "set-usebox", true },
165 { Tabular::SET_LTHEAD, "set-lthead", true },
166 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
167 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
168 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
169 { Tabular::SET_LTFOOT, "set-ltfoot", true },
170 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
171 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
172 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
173 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
174 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
175 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
176 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
177 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
178 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
179 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
180 { Tabular::SET_BOOKTABS, "set-booktabs", false },
181 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
182 { Tabular::SET_TOP_SPACE, "set-top-space", true },
183 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
184 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
185 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
186 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
187 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
188 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
189 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
190 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
191 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
192 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
193 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
194 { Tabular::LAST_ACTION, "", false }
198 string const tostr(LyXAlignment const & num)
203 case LYX_ALIGN_BLOCK:
207 case LYX_ALIGN_CENTER:
209 case LYX_ALIGN_RIGHT:
211 case LYX_ALIGN_LAYOUT:
213 case LYX_ALIGN_SPECIAL:
215 case LYX_ALIGN_DECIMAL:
222 string const tostr(Tabular::HAlignment const & num)
225 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
227 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
229 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
236 string const tostr(Tabular::VAlignment const & num)
239 case Tabular::LYX_VALIGN_TOP:
241 case Tabular::LYX_VALIGN_MIDDLE:
243 case Tabular::LYX_VALIGN_BOTTOM:
250 string const tostr(Tabular::BoxType const & num)
253 case Tabular::BOX_NONE:
255 case Tabular::BOX_PARBOX:
257 case Tabular::BOX_MINIPAGE:
264 // I would have liked a fromstr template a lot better. (Lgb)
265 bool string2type(string const str, LyXAlignment & num)
268 num = LYX_ALIGN_NONE;
269 else if (str == "block")
270 num = LYX_ALIGN_BLOCK;
271 else if (str == "left")
272 num = LYX_ALIGN_LEFT;
273 else if (str == "center")
274 num = LYX_ALIGN_CENTER;
275 else if (str == "right")
276 num = LYX_ALIGN_RIGHT;
277 else if (str == "decimal")
278 num = LYX_ALIGN_DECIMAL;
285 bool string2type(string const str, Tabular::HAlignment & num)
288 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
289 else if (str == "center" )
290 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
291 else if (str == "right")
292 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
299 bool string2type(string const str, Tabular::VAlignment & num)
302 num = Tabular::LYX_VALIGN_TOP;
303 else if (str == "middle" )
304 num = Tabular::LYX_VALIGN_MIDDLE;
305 else if (str == "bottom")
306 num = Tabular::LYX_VALIGN_BOTTOM;
313 bool string2type(string const str, Tabular::BoxType & num)
316 num = Tabular::BOX_NONE;
317 else if (str == "parbox")
318 num = Tabular::BOX_PARBOX;
319 else if (str == "minipage")
320 num = Tabular::BOX_MINIPAGE;
327 bool string2type(string const str, bool & num)
331 else if (str == "false")
339 bool getTokenValue(string const & str, char const * token, string & ret)
342 size_t token_length = strlen(token);
343 size_t pos = str.find(token);
345 if (pos == string::npos || pos + token_length + 1 >= str.length()
346 || str[pos + token_length] != '=')
348 pos += token_length + 1;
350 if (ch != '"' && ch != '\'') { // only read till next space
354 while (pos < str.length() - 1 && str[++pos] != ch)
361 bool getTokenValue(string const & str, char const * token, docstring & ret)
364 bool const success = getTokenValue(str, token, tmp);
365 ret = from_utf8(tmp);
370 bool getTokenValue(string const & str, char const * token, int & num)
374 if (!getTokenValue(str, token, tmp))
376 num = convert<int>(tmp);
381 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
384 return getTokenValue(str, token, tmp) && string2type(tmp, num);
388 bool getTokenValue(string const & str, char const * token,
389 Tabular::HAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
458 string const write_attribute(string const & name, T const & t)
460 string const s = tostr(t);
461 return s.empty() ? s : " " + name + "=\"" + s + "\"";
465 string const write_attribute(string const & name, string const & t)
467 return t.empty() ? t : " " + name + "=\"" + t + "\"";
472 string const write_attribute(string const & name, docstring const & t)
474 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
479 string const write_attribute(string const & name, bool const & b)
481 // we write only true attribute values so we remove a bit of the
482 // file format bloat for tabulars.
483 return b ? write_attribute(name, convert<string>(b)) : string();
488 string const write_attribute(string const & name, int const & i)
490 // we write only true attribute values so we remove a bit of the
491 // file format bloat for tabulars.
492 return i ? write_attribute(name, convert<string>(i)) : string();
497 string const write_attribute(string const & name, Tabular::idx_type const & i)
499 // we write only true attribute values so we remove a bit of the
500 // file format bloat for tabulars.
501 return i ? write_attribute(name, convert<string>(i)) : string();
506 string const write_attribute(string const & name, Length const & value)
508 // we write only the value if we really have one same reson as above.
509 return value.zero() ? string() : write_attribute(name, value.asString());
515 string const featureAsString(Tabular::Feature action)
517 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
518 if (tabularFeature[i].action == action)
519 return tabularFeature[i].feature;
525 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
527 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
528 for (; dit; dit.forwardChar())
529 if (dit.inTexted() && dit.depth() == 1
530 && dit.paragraph().find(align_d, false, false, dit.pos()))
537 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
539 InsetTableCell tail = InsetTableCell(head);
540 DocIterator const dit = separatorPos(&head, align_d);
543 pit_type const psize = head.paragraphs().front().size();
544 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
545 tail.paragraphs().front().eraseChars(0,
546 dit.pos() < psize ? dit.pos() + 1 : psize, false);
553 /////////////////////////////////////////////////////////////////////
557 /////////////////////////////////////////////////////////////////////
560 Tabular::CellData::CellData(Buffer * buf)
563 multicolumn(Tabular::CELL_NORMAL),
564 multirow(Tabular::CELL_NORMAL),
565 alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
576 inset(new InsetTableCell(buf))
578 inset->setBuffer(*buf);
582 Tabular::CellData::CellData(CellData const & cs)
585 multicolumn(cs.multicolumn),
586 multirow(cs.multirow),
587 mroffset(cs.mroffset),
588 alignment(cs.alignment),
589 valignment(cs.valignment),
590 decimal_hoffset(cs.decimal_hoffset),
591 decimal_width(cs.decimal_width),
593 top_line(cs.top_line),
594 bottom_line(cs.bottom_line),
595 left_line(cs.left_line),
596 right_line(cs.right_line),
599 align_special(cs.align_special),
601 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
605 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
611 void Tabular::CellData::swap(CellData & rhs)
613 std::swap(cellno, rhs.cellno);
614 std::swap(width, rhs.width);
615 std::swap(multicolumn, rhs.multicolumn);
616 std::swap(multirow, rhs.multirow);
617 std::swap(mroffset, rhs.mroffset);
618 std::swap(alignment, rhs.alignment);
619 std::swap(valignment, rhs.valignment);
620 std::swap(decimal_hoffset, rhs.decimal_hoffset);
621 std::swap(decimal_width, rhs.decimal_width);
622 std::swap(voffset, rhs.voffset);
623 std::swap(top_line, rhs.top_line);
624 std::swap(bottom_line, rhs.bottom_line);
625 std::swap(left_line, rhs.left_line);
626 std::swap(right_line, rhs.right_line);
627 std::swap(usebox, rhs.usebox);
628 std::swap(rotate, rhs.rotate);
629 std::swap(align_special, rhs.align_special);
630 p_width.swap(rhs.p_width);
631 inset.swap(rhs.inset);
635 Tabular::RowData::RowData()
638 top_space_default(false),
639 bottom_space_default(false),
640 interline_space_default(false),
650 Tabular::ColumnData::ColumnData()
651 : alignment(LYX_ALIGN_CENTER),
652 valignment(LYX_VALIGN_TOP),
658 Tabular::ltType::ltType()
665 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
667 init(buffer, rows_arg, columns_arg);
671 void Tabular::setBuffer(Buffer & buffer)
674 for (row_type i = 0; i < nrows(); ++i)
675 for (col_type j = 0; j < ncols(); ++j)
676 cell_info[i][j].inset->setBuffer(*buffer_);
680 // activates all lines and sets all widths to 0
681 void Tabular::init(Buffer * buf, row_type rows_arg,
682 col_type columns_arg)
685 row_info = row_vector(rows_arg);
686 column_info = column_vector(columns_arg);
687 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
688 row_info.reserve(10);
689 column_info.reserve(10);
690 cell_info.reserve(100);
692 is_long_tabular = false;
693 tabular_valignment = LYX_VALIGN_MIDDLE;
694 tabular_width = Length();
695 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
697 use_booktabs = false;
698 // set silly default lines
699 for (row_type r = 0; r < nrows(); ++r)
700 for (col_type c = 0; c < ncols(); ++c) {
701 cell_info[r][c].inset->setBuffer(*buffer_);
702 cell_info[r][c].top_line = true;
703 cell_info[r][c].left_line = true;
704 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
705 cell_info[r][c].right_line = c == ncols() - 1;
710 void Tabular::deleteRow(row_type const row)
712 // Not allowed to delete last row
716 for (col_type c = 0; c < ncols(); ++c) {
717 // Care about multirow cells
718 if (row + 1 < nrows() &&
719 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
720 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
721 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
724 row_info.erase(row_info.begin() + row);
725 cell_info.erase(cell_info.begin() + row);
730 void Tabular::copyRow(row_type const row)
732 insertRow(row, true);
736 void Tabular::appendRow(row_type row)
738 insertRow(row, false);
742 void Tabular::insertRow(row_type const row, bool copy)
744 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
745 cell_info.insert(cell_info.begin() + row + 1,
746 cell_vector(0, CellData(buffer_)));
748 for (col_type c = 0; c < ncols(); ++c) {
749 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
750 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
751 if (buffer().params().trackChanges)
752 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
753 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
754 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
758 for (col_type c = 0; c < ncols(); ++c) {
759 if (isPartOfMultiRow(row, c))
761 // inherit line settings
762 idx_type const i = cellIndex(row + 1, c);
763 idx_type const j = cellIndex(row, c);
764 setLeftLine(i, leftLine(j));
765 setRightLine(i, rightLine(j));
766 setTopLine(i, topLine(j));
767 if (topLine(j) && bottomLine(j)) {
768 setBottomLine(i, true);
769 setBottomLine(j, false);
771 // mark track changes
772 if (buffer().params().trackChanges)
773 cellInfo(i).inset->setChange(Change(Change::INSERTED));
778 void Tabular::moveColumn(col_type col, ColDirection direction)
780 if (direction == Tabular::LEFT)
783 for (row_type r = 0; r < nrows(); ++r) {
784 std::swap(cell_info[r][col], cell_info[r][col + 1]);
785 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
786 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
788 // FIXME track changes is broken for tabular features (#8469)
789 idx_type const i = cellIndex(r, col);
790 idx_type const j = cellIndex(r, col + 1);
791 if (buffer().params().trackChanges) {
792 cellInfo(i).inset->setChange(Change(Change::INSERTED));
793 cellInfo(j).inset->setChange(Change(Change::INSERTED));
800 void Tabular::moveRow(row_type row, RowDirection direction)
802 if (direction == Tabular::UP)
805 for (col_type c = 0; c < ncols(); ++c) {
806 std::swap(cell_info[row][c], cell_info[row + 1][c]);
807 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
808 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
810 // FIXME track changes is broken for tabular features (#8469)
811 idx_type const i = cellIndex(row, c);
812 idx_type const j = cellIndex(row + 1, c);
813 if (buffer().params().trackChanges) {
814 cellInfo(i).inset->setChange(Change(Change::INSERTED));
815 cellInfo(j).inset->setChange(Change(Change::INSERTED));
822 void Tabular::deleteColumn(col_type const col)
824 // Not allowed to delete last column
828 for (row_type r = 0; r < nrows(); ++r) {
829 // Care about multicolumn cells
830 if (col + 1 < ncols() &&
831 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
832 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
833 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
835 cell_info[r].erase(cell_info[r].begin() + col);
837 column_info.erase(column_info.begin() + col);
842 void Tabular::copyColumn(col_type const col)
844 insertColumn(col, true);
848 void Tabular::appendColumn(col_type col)
850 insertColumn(col, false);
854 void Tabular::insertColumn(col_type const col, bool copy)
856 BufferParams const & bp = buffer().params();
857 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
859 for (row_type r = 0; r < nrows(); ++r) {
860 cell_info[r].insert(cell_info[r].begin() + col + 1,
861 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
863 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
864 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
865 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
868 for (row_type r = 0; r < nrows(); ++r) {
869 // inherit line settings
870 idx_type const i = cellIndex(r, col + 1);
871 idx_type const j = cellIndex(r, col);
872 setBottomLine(i, bottomLine(j));
873 setTopLine(i, topLine(j));
874 setLeftLine(i, leftLine(j));
875 if (rightLine(j) && rightLine(j)) {
876 setRightLine(i, true);
877 setRightLine(j, false);
879 if (buffer().params().trackChanges)
880 cellInfo(i).inset->setChange(Change(Change::INSERTED));
885 void Tabular::updateIndexes()
890 for (row_type row = 0; row < nrows(); ++row)
891 for (col_type column = 0; column < ncols(); ++column) {
892 if (!isPartOfMultiColumn(row, column)
893 && !isPartOfMultiRow(row, column))
895 if (isPartOfMultiRow(row, column))
896 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
898 cell_info[row][column].cellno = numberofcells - 1;
901 rowofcell.resize(numberofcells);
902 columnofcell.resize(numberofcells);
904 // reset column and row of cells and update their width and alignment
905 for (row_type row = 0; row < nrows(); ++row)
906 for (col_type column = 0; column < ncols(); ++column) {
907 if (isPartOfMultiColumn(row, column))
909 // columnofcell needs to be called before setting width and aligment
910 // multirow cells inherit the width from the column width
911 if (!isPartOfMultiRow(row, column)) {
912 columnofcell[i] = column;
915 setFixedWidth(row, column);
916 if (isPartOfMultiRow(row, column))
918 cell_info[row][column].inset->setContentAlignment(
919 getAlignment(cellIndex(row, column)));
925 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
928 for (col_type c = 0; c < ncols(); ++c)
929 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
935 bool Tabular::topLine(idx_type const cell) const
937 return cellInfo(cell).top_line;
941 bool Tabular::bottomLine(idx_type const cell) const
943 return cellInfo(cell).bottom_line;
947 bool Tabular::leftLine(idx_type cell) const
951 return cellInfo(cell).left_line;
955 bool Tabular::rightLine(idx_type cell) const
959 return cellInfo(cell).right_line;
963 int Tabular::interRowSpace(row_type row) const
965 if (!row || row >= nrows())
968 int const interline_space = row_info[row - 1].interline_space_default ?
970 row_info[row - 1].interline_space.inPixels(width());
971 if (rowTopLine(row) && rowBottomLine(row - 1))
972 return interline_space + WIDTH_OF_LINE;
973 return interline_space;
977 int Tabular::interColumnSpace(idx_type cell) const
979 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
980 if (rightLine(cell) && nextcol < ncols()
981 && leftLine(cellIndex(cellRow(cell), nextcol)))
982 return WIDTH_OF_LINE;
987 int Tabular::cellWidth(idx_type cell) const
990 col_type const span = columnSpan(cell);
991 col_type const col = cellColumn(cell);
992 for(col_type c = col; c < col + span ; ++c)
993 w += column_info[c].width;
998 int Tabular::cellHeight(idx_type cell) const
1000 row_type const span = rowSpan(cell);
1001 row_type const row = cellRow(cell);
1003 for(row_type r = row; r < row + span ; ++r) {
1004 h += rowAscent(r) + rowDescent(r);
1005 if (r != row + span - 1)
1006 h += interRowSpace(r + 1);
1013 bool Tabular::updateColumnWidths()
1015 vector<int> max_dwidth(ncols(), 0);
1016 for(col_type c = 0; c < ncols(); ++c)
1017 for(row_type r = 0; r < nrows(); ++r) {
1018 idx_type const i = cellIndex(r, c);
1019 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1020 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1023 bool update = false;
1024 // for each col get max of single col cells
1025 for(col_type c = 0; c < ncols(); ++c) {
1027 for(row_type r = 0; r < nrows(); ++r) {
1028 idx_type const i = cellIndex(r, c);
1029 if (columnSpan(i) == 1) {
1030 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1031 && cell_info[r][c].decimal_width!=0)
1032 new_width = max(new_width, cellInfo(i).width
1033 + max_dwidth[c] - cellInfo(i).decimal_width);
1035 new_width = max(new_width, cellInfo(i).width);
1039 if (column_info[c].width != new_width) {
1040 column_info[c].width = new_width;
1044 // update col widths to fit merged cells
1045 for(col_type c = 0; c < ncols(); ++c)
1046 for(row_type r = 0; r < nrows(); ++r) {
1047 idx_type const i = cellIndex(r, c);
1048 int const span = columnSpan(i);
1049 if (span == 1 || c > cellColumn(i))
1053 for(col_type j = c; j < c + span ; ++j)
1054 old_width += column_info[j].width;
1056 if (cellInfo(i).width > old_width) {
1057 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1066 int Tabular::width() const
1069 for (col_type c = 0; c < ncols(); ++c)
1070 width += column_info[c].width;
1075 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1078 col_type const col = cellColumn(cell);
1079 // set alignment for the whole row if we are not in a multicolumn cell,
1080 // exclude possible multicolumn cells in the row
1081 if (!isMultiColumn(cell)) {
1082 for (row_type r = 0; r < nrows(); ++r) {
1083 // only if the column has no width the multirow inherits the
1084 // alignment of the column, otherwise it is left aligned
1085 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1086 && !isMultiColumn(cellIndex(r, col))) {
1087 cell_info[r][col].alignment = align;
1088 cell_info[r][col].inset->setContentAlignment(align);
1090 if ((isMultiRow(cellIndex(r, col)) && has_width)
1091 && !isMultiColumn(cellIndex(r, col))) {
1092 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1093 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1096 column_info[col].alignment = align;
1097 docstring & dpoint = column_info[col].decimal_point;
1098 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1099 dpoint = from_utf8(lyxrc.default_decimal_point);
1101 cellInfo(cell).alignment = align;
1102 cellInset(cell).get()->setContentAlignment(align);
1107 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1110 if (!isMultiColumn(cell) || onlycolumn)
1111 column_info[cellColumn(cell)].valignment = align;
1113 cellInfo(cell).valignment = align;
1120 * Allow line and paragraph breaks for fixed width cells or disallow them,
1121 * merge cell paragraphs and reset layout to standard for variable width
1124 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1126 inset->setAutoBreakRows(fixedWidth);
1127 inset->toggleFixedWidth(fixedWidth);
1131 // merge all paragraphs to one
1132 BufferParams const & bp = cur.bv().buffer().params();
1133 while (inset->paragraphs().size() > 1)
1134 mergeParagraph(bp, inset->paragraphs(), 0);
1138 // undo information has already been recorded
1139 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1140 bp.documentClass().plainLayoutName());
1147 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1148 Length const & width)
1150 col_type const c = cellColumn(cell);
1152 column_info[c].p_width = width;
1153 // reset the vertical alignment to top if the fixed with
1154 // is removed or zero because only fixed width columns can
1155 // have a vertical alignment
1156 if (column_info[c].p_width.zero())
1157 column_info[c].valignment = LYX_VALIGN_TOP;
1158 for (row_type r = 0; r < nrows(); ++r) {
1159 idx_type const cell = cellIndex(r, c);
1160 // because of multicolumns
1161 toggleFixedWidth(cur, cellInset(cell).get(),
1162 !getPWidth(cell).zero());
1164 // cur paragraph can become invalid after paragraphs were merged
1165 if (cur.pit() > cur.lastpit())
1166 cur.pit() = cur.lastpit();
1167 // cur position can become invalid after newlines were removed
1168 if (cur.pos() > cur.lastpos())
1169 cur.pos() = cur.lastpos();
1173 bool Tabular::setFixedWidth(row_type r, col_type c)
1175 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1176 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1177 || (multicol && !cell_info[r][c].p_width.zero());
1178 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1183 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1184 Length const & width)
1186 if (!isMultiColumn(cell))
1189 cellInfo(cell).p_width = width;
1190 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1191 // cur paragraph can become invalid after paragraphs were merged
1192 if (cur.pit() > cur.lastpit())
1193 cur.pit() = cur.lastpit();
1194 // cur position can become invalid after newlines were removed
1195 if (cur.pos() > cur.lastpos())
1196 cur.pos() = cur.lastpos();
1201 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1203 cellInfo(cell).mroffset = mroffset;
1208 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1209 Tabular::Feature what)
1211 if (what == SET_SPECIAL_MULTICOLUMN)
1212 cellInfo(cell).align_special = special;
1214 column_info[cellColumn(cell)].align_special = special;
1218 void Tabular::setTopLine(idx_type i, bool line)
1220 cellInfo(i).top_line = line;
1224 void Tabular::setBottomLine(idx_type i, bool line)
1226 cellInfo(i).bottom_line = line;
1230 void Tabular::setLeftLine(idx_type cell, bool line)
1232 cellInfo(cell).left_line = line;
1236 void Tabular::setRightLine(idx_type cell, bool line)
1238 cellInfo(cell).right_line = line;
1241 bool Tabular::rowTopLine(row_type r) const
1243 bool all_rows_set = true;
1244 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1245 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1246 return all_rows_set;
1250 bool Tabular::rowBottomLine(row_type r) const
1252 bool all_rows_set = true;
1253 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1254 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1255 return all_rows_set;
1259 bool Tabular::columnLeftLine(col_type c) const
1266 for (row_type r = 0; r < nrows(); ++r) {
1267 idx_type const i = cellIndex(r, c);
1268 if (c == cellColumn(i)) {
1270 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1271 if (cellInfo(i).left_line || right)
1275 return 2 * nrows_left >= total;
1279 bool Tabular::columnRightLine(col_type c) const
1284 int nrows_right = 0;
1286 for (row_type r = 0; r < nrows(); ++r) {
1287 idx_type i = cellIndex(r, c);
1288 if (c == cellColumn(i) + columnSpan(i) - 1) {
1290 bool left = (c + 1 < ncols()
1291 && cellInfo(cellIndex(r, c + 1)).left_line)
1292 || c + 1 == ncols();
1293 if (cellInfo(i).right_line && left)
1297 return 2 * nrows_right >= total;
1301 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1303 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1304 return cellInfo(cell).alignment;
1306 return column_info[cellColumn(cell)].alignment;
1311 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1313 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1314 return cellInfo(cell).valignment;
1315 return column_info[cellColumn(cell)].valignment;
1319 Length const Tabular::getPWidth(idx_type cell) const
1321 if (isMultiColumn(cell))
1322 return cellInfo(cell).p_width;
1323 return column_info[cellColumn(cell)].p_width;
1327 Length const Tabular::getMROffset(idx_type cell) const
1329 return cellInfo(cell).mroffset;
1333 int Tabular::textHOffset(idx_type cell) const
1335 // the LaTeX Way :-(
1336 int x = WIDTH_OF_LINE;
1338 int const w = cellWidth(cell) - cellInfo(cell).width;
1340 switch (getAlignment(cell)) {
1341 case LYX_ALIGN_CENTER:
1344 case LYX_ALIGN_RIGHT:
1347 case LYX_ALIGN_DECIMAL: {
1348 // we center when no decimal point
1349 if (cellInfo(cell).decimal_width == 0) {
1353 col_type const c = cellColumn(cell);
1354 int max_dhoffset = 0;
1355 for(row_type r = 0; r < row_info.size() ; ++r) {
1356 idx_type const i = cellIndex(r, c);
1357 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1358 && cellInfo(i).decimal_width != 0)
1359 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1361 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1364 // LYX_ALIGN_LEFT: nothing :-)
1372 int Tabular::textVOffset(idx_type cell) const
1374 int voffset = cellInfo(cell).voffset;
1375 if (isMultiRow(cell)) {
1376 row_type const row = cellRow(cell);
1377 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1383 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1386 idx_type const numcells = numberOfCellsInRow(row);
1387 // we check against numcells to make sure we do not crash if all the
1388 // cells are multirow (bug #7535), but in that case our return value
1389 // is really invalid, i.e., it is NOT the first cell in the row. but
1390 // i do not know what to do here. (rgh)
1391 while (c < numcells - 1
1392 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1394 return cell_info[row][c].cellno;
1398 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1400 col_type c = ncols() - 1;
1401 // of course we check against 0 so we don't crash. but we have the same
1402 // problem as in the previous routine: if all the cells are part of a
1403 // multirow or part of a multi column, then our return value is invalid.
1405 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1406 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1408 return cell_info[row][c].cellno;
1412 Tabular::row_type Tabular::cellRow(idx_type cell) const
1414 if (cell >= numberofcells)
1418 return rowofcell[cell];
1422 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1424 if (cell >= numberofcells)
1428 return columnofcell[cell];
1432 void Tabular::write(ostream & os) const
1436 << write_attribute("version", 3)
1437 << write_attribute("rows", nrows())
1438 << write_attribute("columns", ncols())
1440 // global longtable options
1442 << write_attribute("rotate", convert<string>(rotate))
1443 << write_attribute("booktabs", use_booktabs)
1444 << write_attribute("islongtable", is_long_tabular)
1445 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1446 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1447 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1448 << write_attribute("headTopDL", endhead.topDL)
1449 << write_attribute("headBottomDL", endhead.bottomDL)
1450 << write_attribute("footTopDL", endfoot.topDL)
1451 << write_attribute("footBottomDL", endfoot.bottomDL)
1452 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1453 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1454 << write_attribute("lastFootEmpty", endlastfoot.empty);
1455 // longtables cannot be aligned vertically
1456 if (!is_long_tabular) {
1457 os << write_attribute("tabularvalignment", tabular_valignment);
1458 os << write_attribute("tabularwidth", tabular_width);
1460 if (is_long_tabular)
1461 os << write_attribute("longtabularalignment",
1462 longtabular_alignment);
1464 for (col_type c = 0; c < ncols(); ++c) {
1466 << write_attribute("alignment", column_info[c].alignment);
1467 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1468 os << write_attribute("decimal_point", column_info[c].decimal_point);
1469 os << write_attribute("valignment", column_info[c].valignment)
1470 << write_attribute("width", column_info[c].p_width.asString())
1471 << write_attribute("special", column_info[c].align_special)
1474 for (row_type r = 0; r < nrows(); ++r) {
1475 static const string def("default");
1477 if (row_info[r].top_space_default)
1478 os << write_attribute("topspace", def);
1480 os << write_attribute("topspace", row_info[r].top_space);
1481 if (row_info[r].bottom_space_default)
1482 os << write_attribute("bottomspace", def);
1484 os << write_attribute("bottomspace", row_info[r].bottom_space);
1485 if (row_info[r].interline_space_default)
1486 os << write_attribute("interlinespace", def);
1488 os << write_attribute("interlinespace", row_info[r].interline_space);
1489 os << write_attribute("endhead", row_info[r].endhead)
1490 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1491 << write_attribute("endfoot", row_info[r].endfoot)
1492 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1493 << write_attribute("newpage", row_info[r].newpage)
1494 << write_attribute("caption", row_info[r].caption)
1496 for (col_type c = 0; c < ncols(); ++c) {
1498 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1499 << write_attribute("multirow", cell_info[r][c].multirow)
1500 << write_attribute("mroffset", cell_info[r][c].mroffset)
1501 << write_attribute("alignment", cell_info[r][c].alignment)
1502 << write_attribute("valignment", cell_info[r][c].valignment)
1503 << write_attribute("topline", cell_info[r][c].top_line)
1504 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1505 << write_attribute("leftline", cell_info[r][c].left_line)
1506 << write_attribute("rightline", cell_info[r][c].right_line)
1507 << write_attribute("rotate", cell_info[r][c].rotate)
1508 << write_attribute("usebox", cell_info[r][c].usebox)
1509 << write_attribute("width", cell_info[r][c].p_width)
1510 << write_attribute("special", cell_info[r][c].align_special)
1512 os << "\\begin_inset ";
1513 cell_info[r][c].inset->write(os);
1514 os << "\n\\end_inset\n"
1519 os << "</lyxtabular>\n";
1523 void Tabular::read(Lexer & lex)
1526 istream & is = lex.getStream();
1528 l_getline(is, line);
1529 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1530 LASSERT(false, return);
1534 if (!getTokenValue(line, "version", version))
1536 LATTEST(version >= 2);
1539 if (!getTokenValue(line, "rows", rows_arg))
1542 if (!getTokenValue(line, "columns", columns_arg))
1544 init(buffer_, rows_arg, columns_arg);
1545 l_getline(is, line);
1546 if (!prefixIs(line, "<features")) {
1547 lyxerr << "Wrong tabular format (expected <features ...> got"
1548 << line << ')' << endl;
1551 getTokenValue(line, "rotate", rotate);
1552 getTokenValue(line, "booktabs", use_booktabs);
1553 getTokenValue(line, "islongtable", is_long_tabular);
1554 getTokenValue(line, "tabularvalignment", tabular_valignment);
1555 getTokenValue(line, "tabularwidth", tabular_width);
1556 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1557 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1558 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1559 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1560 getTokenValue(line, "headTopDL", endhead.topDL);
1561 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1562 getTokenValue(line, "footTopDL", endfoot.topDL);
1563 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1564 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1565 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1566 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1568 for (col_type c = 0; c < ncols(); ++c) {
1570 if (!prefixIs(line,"<column")) {
1571 lyxerr << "Wrong tabular format (expected <column ...> got"
1572 << line << ')' << endl;
1575 getTokenValue(line, "alignment", column_info[c].alignment);
1576 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1577 getTokenValue(line, "valignment", column_info[c].valignment);
1578 getTokenValue(line, "width", column_info[c].p_width);
1579 getTokenValue(line, "special", column_info[c].align_special);
1582 for (row_type i = 0; i < nrows(); ++i) {
1583 l_getline(is, line);
1584 if (!prefixIs(line, "<row")) {
1585 lyxerr << "Wrong tabular format (expected <row ...> got"
1586 << line << ')' << endl;
1589 getTokenValue(line, "topspace", row_info[i].top_space,
1590 row_info[i].top_space_default);
1591 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1592 row_info[i].bottom_space_default);
1593 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1594 row_info[i].interline_space_default);
1595 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1596 getTokenValue(line, "endhead", row_info[i].endhead);
1597 getTokenValue(line, "endfoot", row_info[i].endfoot);
1598 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1599 getTokenValue(line, "newpage", row_info[i].newpage);
1600 getTokenValue(line, "caption", row_info[i].caption);
1601 for (col_type j = 0; j < ncols(); ++j) {
1602 l_getline(is, line);
1603 if (!prefixIs(line, "<cell")) {
1604 lyxerr << "Wrong tabular format (expected <cell ...> got"
1605 << line << ')' << endl;
1608 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1609 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1610 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1611 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1612 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1613 getTokenValue(line, "topline", cell_info[i][j].top_line);
1614 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1615 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1616 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1617 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1618 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1619 getTokenValue(line, "width", cell_info[i][j].p_width);
1621 getTokenValue(line, "special", cell_info[i][j].align_special);
1622 l_getline(is, line);
1623 if (prefixIs(line, "\\begin_inset")) {
1624 cell_info[i][j].inset->setBuffer(*buffer_);
1625 cell_info[i][j].inset->read(lex);
1626 l_getline(is, line);
1628 if (!prefixIs(line, "</cell>")) {
1629 lyxerr << "Wrong tabular format (expected </cell> got"
1630 << line << ')' << endl;
1634 l_getline(is, line);
1635 if (!prefixIs(line, "</row>")) {
1636 lyxerr << "Wrong tabular format (expected </row> got"
1637 << line << ')' << endl;
1641 while (!prefixIs(line, "</lyxtabular>")) {
1642 l_getline(is, line);
1648 bool Tabular::isMultiColumn(idx_type cell) const
1650 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1651 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1655 bool Tabular::hasMultiColumn(col_type c) const
1657 for (row_type r = 0; r < nrows(); ++r) {
1658 if (isMultiColumn(cellIndex(r, c)))
1665 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1667 return cell_info[cellRow(cell)][cellColumn(cell)];
1671 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1672 bool const right_border)
1674 idx_type const col = cellColumn(cell);
1675 idx_type const row = cellRow(cell);
1676 for (idx_type i = 0; i < number; ++i)
1677 unsetMultiRow(cellIndex(row, col + i));
1679 // unsetting of multirow may have invalidated cell index
1680 cell = cellIndex(row, col);
1681 CellData & cs = cellInfo(cell);
1682 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1683 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1684 cs.alignment = column_info[col].alignment;
1686 setRightLine(cell, right_border);
1688 for (idx_type i = 1; i < number; ++i) {
1689 CellData & cs1 = cellInfo(cell + i);
1690 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1691 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1699 bool Tabular::isMultiRow(idx_type cell) const
1701 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1702 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1705 bool Tabular::hasMultiRow(row_type r) const
1707 for (col_type c = 0; c < ncols(); ++c) {
1708 if (isMultiRow(cellIndex(r, c)))
1714 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1715 bool const bottom_border)
1717 idx_type const col = cellColumn(cell);
1718 idx_type const row = cellRow(cell);
1719 for (idx_type i = 0; i < number; ++i)
1720 unsetMultiColumn(cellIndex(row + i, col));
1722 // unsetting of multirow may have invalidated cell index
1723 cell = cellIndex(row, col);
1724 CellData & cs = cellInfo(cell);
1725 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1726 cs.valignment = LYX_VALIGN_MIDDLE;
1727 // the horizontal alignment of multirow cells can only
1728 // be changed for the whole table row,
1729 // support changing this only for the multirow cell can be done via
1731 // this feature would be a fileformat change
1732 // until LyX supports this, use the deault alignment of multirow
1734 cs.alignment = LYX_ALIGN_LEFT;
1736 // set the bottom line of the last selected cell
1737 setBottomLine(cell, bottom_border);
1739 for (idx_type i = 1; i < number; ++i) {
1740 CellData & cs1 = cell_info[row + i][col];
1741 cs1.multirow = CELL_PART_OF_MULTIROW;
1742 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1750 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1752 row_type const row = cellRow(cell);
1753 col_type const col = cellColumn(cell);
1755 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1762 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1764 col_type const column = cellColumn(cell);
1765 col_type row = cellRow(cell) + 1;
1766 while (row < nrows() && isPartOfMultiRow(row, column))
1769 return row - cellRow(cell);
1773 void Tabular::unsetMultiColumn(idx_type cell)
1775 if (!isMultiColumn(cell))
1778 row_type const row = cellRow(cell);
1779 col_type const col = cellColumn(cell);
1780 row_type const span = columnSpan(cell);
1781 for (col_type c = 0; c < span; ++c) {
1782 // in the table dialog the lines are set in every case
1783 // when unsetting a multicolumn this leads to an additional right
1784 // line for every cell that was part of the former multicolumn cell,
1785 // except if the cell is in the last column
1786 // therefore remove this line
1787 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1788 && (col + c) < (col + span - 1))
1789 cell_info[row][col + c].right_line = false;
1790 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1796 void Tabular::unsetMultiRow(idx_type cell)
1798 if (!isMultiRow(cell))
1801 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1802 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1803 row_type const row = cellRow(cell);
1804 col_type const col = cellColumn(cell);
1805 row_type const span = rowSpan(cell);
1806 for (row_type r = 0; r < span; ++r)
1807 cell_info[row + r][col].multirow = CELL_NORMAL;
1812 void Tabular::setRotateCell(idx_type cell, int value)
1814 cellInfo(cell).rotate = value;
1818 int Tabular::getRotateCell(idx_type cell) const
1820 return cellInfo(cell).rotate;
1824 bool Tabular::needRotating() const
1828 for (row_type r = 0; r < nrows(); ++r)
1829 for (col_type c = 0; c < ncols(); ++c)
1830 if (cell_info[r][c].rotate != 0)
1836 bool Tabular::isLastCell(idx_type cell) const
1838 if (cell + 1 < numberofcells)
1844 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1846 if (cellRow(cell) == 0)
1849 col_type const col = cellColumn(cell);
1850 row_type r = cellRow(cell) - 1;
1851 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1854 return cell_info[r][col].cellno;
1858 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1860 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1861 if (nextrow < nrows())
1862 return cell_info[nextrow][cellColumn(cell)].cellno;
1867 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1869 LASSERT(column != npos && column < ncols(), column = 0);
1870 LASSERT(row != npos && row < nrows(), row = 0);
1871 return cell_info[row][column].cellno;
1875 void Tabular::setUsebox(idx_type cell, BoxType type)
1877 cellInfo(cell).usebox = type;
1881 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1882 // adds line breaks, see bug 4886.
1883 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1885 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1886 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1888 if (cellInfo(cell).usebox > 1)
1889 return cellInfo(cell).usebox;
1890 return useParbox(cell);
1895 // This are functions used for the longtable support
1897 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1903 row_info[row].endfirsthead = flag;
1907 row_info[row].endhead = flag;
1912 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1915 hd.set = haveLTHead();
1916 return row_info[row].endhead;
1920 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1923 hd.set = haveLTFirstHead();
1924 return row_info[row].endfirsthead;
1928 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1934 row_info[row].endlastfoot = flag;
1938 row_info[row].endfoot = flag;
1943 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1946 fd.set = haveLTFoot();
1947 return row_info[row].endfoot;
1951 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1954 fd.set = haveLTLastFoot();
1955 return row_info[row].endlastfoot;
1959 void Tabular::setLTNewPage(row_type row, bool what)
1961 row_info[row].newpage = what;
1965 bool Tabular::getLTNewPage(row_type row) const
1967 return row_info[row].newpage;
1971 bool Tabular::haveLTHead(bool withcaptions) const
1973 if (!is_long_tabular)
1975 for (row_type i = 0; i < nrows(); ++i)
1976 if (row_info[i].endhead &&
1977 (withcaptions || !row_info[i].caption))
1983 bool Tabular::haveLTFirstHead(bool withcaptions) const
1985 if (!is_long_tabular || endfirsthead.empty)
1987 for (row_type r = 0; r < nrows(); ++r)
1988 if (row_info[r].endfirsthead &&
1989 (withcaptions || !row_info[r].caption))
1995 bool Tabular::haveLTFoot(bool withcaptions) const
1997 if (!is_long_tabular)
1999 for (row_type r = 0; r < nrows(); ++r)
2000 if (row_info[r].endfoot &&
2001 (withcaptions || !row_info[r].caption))
2007 bool Tabular::haveLTLastFoot(bool withcaptions) const
2009 if (!is_long_tabular || endlastfoot.empty)
2011 for (row_type r = 0; r < nrows(); ++r)
2012 if (row_info[r].endlastfoot &&
2013 (withcaptions || !row_info[r].caption))
2019 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2021 idx_type i = getFirstCellInRow(row);
2023 setMultiColumn(i, numberOfCellsInRow(row), false);
2024 setTopLine(i, false);
2025 setBottomLine(i, false);
2026 setLeftLine(i, false);
2027 setRightLine(i, false);
2028 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2029 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2030 setLTHead(row, true, endfirsthead, true);
2031 row_info[row].endfirsthead = true;
2034 unsetMultiColumn(i);
2035 // When unsetting a caption row, also all existing
2036 // captions in this row must be dissolved.
2038 row_info[row].caption = what;
2043 bool Tabular::ltCaption(row_type row) const
2045 return row_info[row].caption;
2049 bool Tabular::haveLTCaption(CaptionType captiontype) const
2051 if (!is_long_tabular)
2053 for (row_type r = 0; r < nrows(); ++r) {
2054 if (row_info[r].caption) {
2055 switch (captiontype) {
2056 case CAPTION_FIRSTHEAD:
2057 if (row_info[r].endfirsthead)
2061 if (row_info[r].endhead)
2065 if (row_info[r].endfoot)
2068 case CAPTION_LASTFOOT:
2069 if (row_info[r].endlastfoot)
2081 // end longtable support functions
2083 void Tabular::setRowAscent(row_type row, int height)
2085 if (row >= nrows() || row_info[row].ascent == height)
2087 row_info[row].ascent = height;
2091 void Tabular::setRowDescent(row_type row, int height)
2093 if (row >= nrows() || row_info[row].descent == height)
2095 row_info[row].descent = height;
2099 int Tabular::rowAscent(row_type row) const
2101 LASSERT(row < nrows(), row = 0);
2102 return row_info[row].ascent;
2106 int Tabular::rowDescent(row_type row) const
2108 LASSERT(row < nrows(), row = 0);
2109 return row_info[row].descent;
2113 int Tabular::height() const
2116 for (row_type row = 0; row < nrows(); ++row)
2117 height += rowAscent(row) + rowDescent(row) +
2123 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2125 LASSERT(row < nrows(), return false);
2126 LASSERT(column < ncols(), return false);
2127 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2131 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2133 LASSERT(row < nrows(), return false);
2134 LASSERT(column < ncols(), return false);
2135 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2139 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2141 // we only output complete row lines and the 1st row here, the rest
2142 // is done in Tabular::TeXBottomHLine(...)
2144 // get for each column the topline (if any)
2145 vector<bool> topline;
2147 for (col_type c = 0; c < ncols(); ++c) {
2148 topline.push_back(topLine(cellIndex(row, c)));
2149 // If cell is part of a multirow and not the first cell of the
2150 // multirow, no line must be drawn.
2152 if (isMultiRow(cellIndex(row, c))
2153 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2159 // do nothing if empty first row, or incomplete row line after
2160 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2163 // only output complete row lines and the 1st row's clines
2164 if (nset == ncols()) {
2166 os << (row == 0 ? "\\toprule " : "\\midrule ");
2170 } else if (row == 0) {
2171 for (col_type c = 0; c < ncols(); ++c) {
2173 col_type offset = 0;
2174 for (col_type j = 0 ; j < c; ++j)
2175 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2178 //babel makes the "-" character an active one, so we have to suppress this here
2179 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2180 if (lang == "slovak" || lang == "czech")
2181 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2182 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2184 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2186 col_type cstart = c;
2187 for ( ; c < ncols() && topline[c]; ++c) {}
2189 for (col_type j = cstart ; j < c ; ++j)
2190 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2193 os << c + offset << "} ";
2201 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2203 // we output bottomlines of row r and the toplines of row r+1
2204 // if the latter do not span the whole tabular
2206 // get the bottomlines of row r, and toplines in next row
2207 bool lastrow = row == nrows() - 1;
2208 vector<bool> bottomline, topline;
2209 bool nextrowset = true;
2210 for (col_type c = 0; c < ncols(); ++c) {
2211 bottomline.push_back(bottomLine(cellIndex(row, c)));
2212 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2213 // If cell is part of a multirow and not the last cell of the
2214 // multirow, no line must be drawn.
2216 if (isMultiRow(cellIndex(row, c))
2217 && isMultiRow(cellIndex(row + 1, c))
2218 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2219 bottomline[c] = false;
2222 nextrowset &= topline[c];
2225 // combine this row's bottom lines and next row's toplines if necessary
2227 for (col_type c = 0; c < ncols(); ++c) {
2229 bottomline[c] = bottomline[c] || topline[c];
2234 // do nothing if empty, OR incomplete row line with a topline in next row
2235 if (nset == 0 || (nextrowset && nset != ncols()))
2238 if (nset == ncols()) {
2240 os << (lastrow ? "\\bottomrule" : "\\midrule");
2244 for (col_type c = 0; c < ncols(); ++c) {
2245 if (bottomline[c]) {
2246 col_type offset = 0;
2247 for (col_type j = 0 ; j < c; ++j)
2248 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2251 //babel makes the "-" character an active one, so we have to suppress this here
2252 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2253 if (lang == "slovak" || lang == "czech")
2254 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2255 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2257 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2259 col_type cstart = c;
2260 for ( ; c < ncols() && bottomline[c]; ++c) {}
2262 for (col_type j = cstart ; j < c ; ++j)
2263 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2266 os << c + offset << "} ";
2274 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2275 bool & ismulticol, bool & ismultirow) const
2277 row_type const r = cellRow(cell);
2278 if (is_long_tabular && row_info[r].caption)
2281 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2282 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2283 // figure out how to set the lines
2284 // we always set double lines to the right of the cell
2285 col_type const c = cellColumn(cell);
2286 col_type const nextcol = c + columnSpan(cell);
2287 bool colright = columnRightLine(c);
2288 bool colleft = columnLeftLine(c);
2289 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2290 bool nextcellleft = nextcol < ncols()
2291 && leftLine(cellIndex(r, nextcol));
2292 bool coldouble = colright && nextcolleft;
2293 bool celldouble = rightLine(cell) && nextcellleft;
2295 ismulticol = isMultiColumn(cell)
2296 || (c == 0 && colleft != leftLine(cell))
2297 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2298 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2299 || (coldouble != celldouble);
2301 // we center in multicol when no decimal point
2302 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2303 docstring const align_d = column_info[c].decimal_point;
2304 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2308 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2309 int latexcolspan = columnSpan(cell);
2310 for(col_type col = c; col < c + columnSpan(cell); ++col)
2311 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2315 os << "\\multicolumn{" << latexcolspan << "}{";
2316 if (c ==0 && leftLine(cell))
2318 if (!cellInfo(cell).align_special.empty()) {
2319 os << cellInfo(cell).align_special;
2321 if (!getPWidth(cell).zero()) {
2323 case LYX_ALIGN_LEFT:
2324 os << ">{\\raggedright}";
2326 case LYX_ALIGN_RIGHT:
2327 os << ">{\\raggedleft}";
2329 case LYX_ALIGN_CENTER:
2330 os << ">{\\centering}";
2336 case LYX_VALIGN_TOP:
2339 case LYX_VALIGN_MIDDLE:
2342 case LYX_VALIGN_BOTTOM:
2347 << from_ascii(getPWidth(cell).asLatexString())
2351 case LYX_ALIGN_LEFT:
2354 case LYX_ALIGN_RIGHT:
2361 } // end if else !getPWidth
2362 } // end if else !cellinfo_of_cell
2363 if (rightLine(cell) || nextcellleft)
2366 // add extra vertical line if we want a double one
2369 } // end if ismulticol
2371 // we only need code for the first multirow cell
2372 ismultirow = isMultiRow(cell);
2374 os << "\\multirow{" << rowSpan(cell) << "}{";
2375 if (!getPWidth(cell).zero())
2376 os << from_ascii(getPWidth(cell).asLatexString());
2378 // we need to set a default value
2381 if (!getMROffset(cell).zero())
2382 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2384 } // end if ismultirow
2386 if (getRotateCell(cell) != 0)
2387 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2389 if (getUsebox(cell) == BOX_PARBOX) {
2392 case LYX_VALIGN_TOP:
2395 case LYX_VALIGN_MIDDLE:
2398 case LYX_VALIGN_BOTTOM:
2402 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2404 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2405 os << "\\begin{minipage}[";
2407 case LYX_VALIGN_TOP:
2410 case LYX_VALIGN_MIDDLE:
2413 case LYX_VALIGN_BOTTOM:
2417 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2423 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2424 bool ismulticol, bool ismultirow) const
2426 row_type const r = cellRow(cell);
2427 if (is_long_tabular && row_info[r].caption)
2431 if (getUsebox(cell) == BOX_PARBOX)
2433 else if (getUsebox(cell) == BOX_MINIPAGE)
2434 os << breakln << "\\end{minipage}";
2435 if (getRotateCell(cell) != 0)
2436 os << breakln << "\\end{turn}";
2444 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2445 OutputParams const & runparams) const
2447 if (!is_long_tabular)
2451 // the caption must be output before the headers
2452 if (haveLTCaption()) {
2453 for (row_type r = 0; r < nrows(); ++r) {
2454 if (row_info[r].caption)
2455 TeXRow(os, r, runparams);
2458 // output first header info
2459 if (haveLTFirstHead()) {
2460 if (endfirsthead.topDL)
2462 for (row_type r = 0; r < nrows(); ++r) {
2463 if (row_info[r].endfirsthead)
2464 TeXRow(os, r, runparams);
2466 if (endfirsthead.bottomDL)
2468 os << "\\endfirsthead\n";
2470 // output header info
2472 if (endfirsthead.empty && !haveLTFirstHead())
2473 os << "\\endfirsthead\n";
2476 for (row_type r = 0; r < nrows(); ++r) {
2477 if (row_info[r].endhead)
2478 TeXRow(os, r, runparams);
2480 if (endhead.bottomDL)
2482 os << "\\endhead\n";
2484 // output footer info
2488 for (row_type r = 0; r < nrows(); ++r) {
2489 if (row_info[r].endfoot)
2490 TeXRow(os, r, runparams);
2492 if (endfoot.bottomDL)
2494 os << "\\endfoot\n";
2495 if (endlastfoot.empty && !haveLTLastFoot())
2496 os << "\\endlastfoot\n";
2498 // output lastfooter info
2499 if (haveLTLastFoot()) {
2500 if (endlastfoot.topDL)
2502 for (row_type r = 0; r < nrows(); ++r) {
2503 if (row_info[r].endlastfoot)
2504 TeXRow(os, r, runparams);
2506 if (endlastfoot.bottomDL)
2508 os << "\\endlastfoot\n";
2513 bool Tabular::isValidRow(row_type row) const
2515 if (!is_long_tabular)
2517 return !row_info[row].endhead && !row_info[row].endfirsthead
2518 && !row_info[row].endfoot && !row_info[row].endlastfoot
2519 && !row_info[row].caption;
2523 void Tabular::TeXRow(otexstream & os, row_type row,
2524 OutputParams const & runparams) const
2526 idx_type cell = cellIndex(row, 0);
2527 shared_ptr<InsetTableCell> inset = cellInset(cell);
2528 Paragraph const & par = inset->paragraphs().front();
2529 string const lang = par.getParLanguage(buffer().params())->lang();
2531 //output the top line
2532 TeXTopHLine(os, row, lang);
2534 if (row_info[row].top_space_default) {
2536 os << "\\addlinespace\n";
2538 os << "\\noalign{\\vskip\\doublerulesep}\n";
2539 } else if(!row_info[row].top_space.zero()) {
2541 os << "\\addlinespace["
2542 << from_ascii(row_info[row].top_space.asLatexString())
2545 os << "\\noalign{\\vskip"
2546 << from_ascii(row_info[row].top_space.asLatexString())
2550 bool ismulticol = false;
2551 bool ismultirow = false;
2552 for (col_type c = 0; c < ncols(); ++c) {
2553 if (isPartOfMultiColumn(row, c))
2556 cell = cellIndex(row, c);
2558 if (isPartOfMultiRow(row, c)
2559 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2560 if (cell != getLastCellInRow(row))
2565 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2566 shared_ptr<InsetTableCell> inset = cellInset(cell);
2568 Paragraph const & par = inset->paragraphs().front();
2569 bool rtl = par.isRTL(buffer().params())
2571 && getPWidth(cell).zero()
2572 && !runparams.use_polyglossia;
2576 par.getParLanguage(buffer().params())->lang();
2577 if (lang == "farsi")
2579 else if (lang == "arabic_arabi")
2581 // currently, remaining RTL languages are
2582 // arabic_arabtex and hebrew
2586 // pass to the OutputParams that we are in a cell and
2587 // which alignment we have set.
2588 // InsetNewline needs this context information.
2589 OutputParams newrp(runparams);
2590 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2591 ? OutputParams::PLAIN
2592 : OutputParams::ALIGNED;
2594 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2595 // copy cell and split in 2
2596 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2597 head.setBuffer(buffer());
2598 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2600 dit.push_back(CursorSlice(head));
2601 head.setMacrocontextPositionRecursive(dit);
2602 bool hassep = false;
2603 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2604 head.latex(os, newrp);
2607 tail.setBuffer(head.buffer());
2609 dit.push_back(CursorSlice(tail));
2610 tail.setMacrocontextPositionRecursive(dit);
2611 tail.latex(os, newrp);
2613 } else if (!isPartOfMultiRow(row, c)) {
2614 if (!runparams.nice)
2615 os.texrow().start(par.id(), 0);
2616 inset->latex(os, newrp);
2619 runparams.encoding = newrp.encoding;
2623 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2624 if (cell != getLastCellInRow(row)) { // not last cell in row
2631 os << "\\tabularnewline";
2632 if (row_info[row].bottom_space_default) {
2634 os << "\\addlinespace";
2636 os << "[\\doublerulesep]";
2637 } else if (!row_info[row].bottom_space.zero()) {
2639 os << "\\addlinespace";
2641 << from_ascii(row_info[row].bottom_space.asLatexString())
2646 //output the bottom line
2647 TeXBottomHLine(os, row, lang);
2649 if (row_info[row].interline_space_default) {
2651 os << "\\addlinespace\n";
2653 os << "\\noalign{\\vskip\\doublerulesep}\n";
2654 } else if (!row_info[row].interline_space.zero()) {
2656 os << "\\addlinespace["
2657 << from_ascii(row_info[row].interline_space.asLatexString())
2660 os << "\\noalign{\\vskip"
2661 << from_ascii(row_info[row].interline_space.asLatexString())
2667 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2669 bool const is_tabular_star = !tabular_width.zero();
2671 //+---------------------------------------------------------------------
2672 //+ first the opening preamble +
2673 //+---------------------------------------------------------------------
2676 if (runparams.lastid != -1)
2677 os.texrow().start(runparams.lastid, runparams.lastpos);
2680 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2682 if (is_long_tabular) {
2683 os << "\\begin{longtable}";
2684 switch (longtabular_alignment) {
2685 case LYX_LONGTABULAR_ALIGN_LEFT:
2688 case LYX_LONGTABULAR_ALIGN_CENTER:
2690 case LYX_LONGTABULAR_ALIGN_RIGHT:
2695 if (is_tabular_star)
2696 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2698 os << "\\begin{tabular}";
2699 switch (tabular_valignment) {
2700 case LYX_VALIGN_TOP:
2703 case LYX_VALIGN_MIDDLE:
2705 case LYX_VALIGN_BOTTOM:
2713 if (is_tabular_star)
2714 os << "@{\\extracolsep{\\fill}}";
2716 for (col_type c = 0; c < ncols(); ++c) {
2717 if (columnLeftLine(c))
2719 if (!column_info[c].align_special.empty()) {
2720 os << column_info[c].align_special;
2722 if (!column_info[c].p_width.zero()) {
2723 switch (column_info[c].alignment) {
2724 case LYX_ALIGN_LEFT:
2725 os << ">{\\raggedright}";
2727 case LYX_ALIGN_RIGHT:
2728 os << ">{\\raggedleft}";
2730 case LYX_ALIGN_CENTER:
2731 os << ">{\\centering}";
2733 case LYX_ALIGN_NONE:
2734 case LYX_ALIGN_BLOCK:
2735 case LYX_ALIGN_LAYOUT:
2736 case LYX_ALIGN_SPECIAL:
2737 case LYX_ALIGN_DECIMAL:
2741 switch (column_info[c].valignment) {
2742 case LYX_VALIGN_TOP:
2745 case LYX_VALIGN_MIDDLE:
2748 case LYX_VALIGN_BOTTOM:
2753 << from_ascii(column_info[c].p_width.asLatexString())
2756 switch (column_info[c].alignment) {
2757 case LYX_ALIGN_LEFT:
2760 case LYX_ALIGN_RIGHT:
2763 case LYX_ALIGN_DECIMAL:
2764 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2770 } // end if else !column_info[i].p_width
2771 } // end if else !column_info[i].align_special
2772 if (columnRightLine(c))
2777 TeXLongtableHeaderFooter(os, runparams);
2779 //+---------------------------------------------------------------------
2780 //+ the single row and columns (cells) +
2781 //+---------------------------------------------------------------------
2783 for (row_type r = 0; r < nrows(); ++r) {
2784 if (isValidRow(r)) {
2785 TeXRow(os, r, runparams);
2786 if (is_long_tabular && row_info[r].newpage)
2787 os << "\\newpage\n";
2791 //+---------------------------------------------------------------------
2792 //+ the closing of the tabular +
2793 //+---------------------------------------------------------------------
2795 if (is_long_tabular)
2796 os << "\\end{longtable}";
2798 if (is_tabular_star)
2799 os << "\\end{tabular*}";
2801 os << "\\end{tabular}";
2805 os << breakln << "\\end{turn}";
2809 int Tabular::docbookRow(odocstream & os, row_type row,
2810 OutputParams const & runparams) const
2813 idx_type cell = getFirstCellInRow(row);
2816 for (col_type c = 0; c < ncols(); ++c) {
2817 if (isPartOfMultiColumn(row, c))
2820 os << "<entry align=\"";
2821 switch (getAlignment(cell)) {
2822 case LYX_ALIGN_LEFT:
2825 case LYX_ALIGN_RIGHT:
2833 os << "\" valign=\"";
2834 switch (getVAlignment(cell)) {
2835 case LYX_VALIGN_TOP:
2838 case LYX_VALIGN_BOTTOM:
2841 case LYX_VALIGN_MIDDLE:
2846 if (isMultiColumn(cell)) {
2847 os << " namest=\"col" << c << "\" ";
2848 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2852 ret += cellInset(cell)->docbook(os, runparams);
2861 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2865 //+---------------------------------------------------------------------
2866 //+ first the opening preamble +
2867 //+---------------------------------------------------------------------
2869 os << "<tgroup cols=\"" << ncols()
2870 << "\" colsep=\"1\" rowsep=\"1\">\n";
2872 for (col_type c = 0; c < ncols(); ++c) {
2873 os << "<colspec colname=\"col" << c << "\" align=\"";
2874 switch (column_info[c].alignment) {
2875 case LYX_ALIGN_LEFT:
2878 case LYX_ALIGN_RIGHT:
2886 if (runparams.flavor == OutputParams::XML)
2892 //+---------------------------------------------------------------------
2893 //+ Long Tabular case +
2894 //+---------------------------------------------------------------------
2896 // output caption info
2897 // The caption flag wins over head/foot
2898 if (haveLTCaption()) {
2899 os << "<caption>\n";
2901 for (row_type r = 0; r < nrows(); ++r) {
2902 if (row_info[r].caption) {
2903 ret += docbookRow(os, r, runparams);
2906 os << "</caption>\n";
2909 // output header info
2910 if (haveLTHead(false) || haveLTFirstHead(false)) {
2913 for (row_type r = 0; r < nrows(); ++r) {
2914 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2915 !row_info[r].caption) {
2916 ret += docbookRow(os, r, runparams);
2922 // output footer info
2923 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2926 for (row_type r = 0; r < nrows(); ++r) {
2927 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2928 !row_info[r].caption) {
2929 ret += docbookRow(os, r, runparams);
2936 //+---------------------------------------------------------------------
2937 //+ the single row and columns (cells) +
2938 //+---------------------------------------------------------------------
2942 for (row_type r = 0; r < nrows(); ++r) {
2943 if (isValidRow(r)) {
2944 ret += docbookRow(os, r, runparams);
2949 //+---------------------------------------------------------------------
2950 //+ the closing of the tabular +
2951 //+---------------------------------------------------------------------
2960 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2961 OutputParams const & runparams, bool header) const
2964 string const celltag = header ? "th" : "td";
2965 idx_type cell = getFirstCellInRow(row);
2967 xs << html::StartTag("tr");
2968 for (col_type c = 0; c < ncols(); ++c) {
2969 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2974 switch (getAlignment(cell)) {
2975 case LYX_ALIGN_LEFT:
2978 case LYX_ALIGN_RIGHT:
2986 attr << " valign='";
2987 switch (getVAlignment(cell)) {
2988 case LYX_VALIGN_TOP:
2991 case LYX_VALIGN_BOTTOM:
2994 case LYX_VALIGN_MIDDLE:
2999 if (isMultiColumn(cell))
3000 attr << " colspan='" << columnSpan(cell) << "'";
3001 else if (isMultiRow(cell))
3002 attr << " rowspan='" << rowSpan(cell) << "'";
3004 xs << html::StartTag(celltag, attr.str()) << html::CR();
3005 ret += cellInset(cell)->xhtml(xs, runparams);
3006 xs << html::EndTag(celltag) << html::CR();
3009 xs << html::EndTag("tr");
3014 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3018 if (is_long_tabular) {
3019 // we'll wrap it in a div, so as to deal with alignment
3021 switch (longtabular_alignment) {
3022 case LYX_LONGTABULAR_ALIGN_LEFT:
3025 case LYX_LONGTABULAR_ALIGN_CENTER:
3028 case LYX_LONGTABULAR_ALIGN_RIGHT:
3032 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3034 // The caption flag wins over head/foot
3035 if (haveLTCaption()) {
3036 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3038 for (row_type r = 0; r < nrows(); ++r)
3039 if (row_info[r].caption)
3040 ret += xhtmlRow(xs, r, runparams);
3041 xs << html::EndTag("div") << html::CR();
3045 xs << html::StartTag("table") << html::CR();
3047 // output header info
3048 bool const havefirsthead = haveLTFirstHead(false);
3049 // if we have a first head, then we are going to ignore the
3050 // headers for the additional pages, since there aren't any
3051 // in XHTML. this test accomplishes that.
3052 bool const havehead = !havefirsthead && haveLTHead(false);
3053 if (havehead || havefirsthead) {
3054 xs << html::StartTag("thead") << html::CR();
3055 for (row_type r = 0; r < nrows(); ++r) {
3056 if (((havefirsthead && row_info[r].endfirsthead) ||
3057 (havehead && row_info[r].endhead)) &&
3058 !row_info[r].caption) {
3059 ret += xhtmlRow(xs, r, runparams, true);
3062 xs << html::EndTag("thead") << html::CR();
3064 // output footer info
3065 bool const havelastfoot = haveLTLastFoot(false);
3067 bool const havefoot = !havelastfoot && haveLTFoot(false);
3068 if (havefoot || havelastfoot) {
3069 xs << html::StartTag("tfoot") << html::CR();
3070 for (row_type r = 0; r < nrows(); ++r) {
3071 if (((havelastfoot && row_info[r].endlastfoot) ||
3072 (havefoot && row_info[r].endfoot)) &&
3073 !row_info[r].caption) {
3074 ret += xhtmlRow(xs, r, runparams);
3077 xs << html::EndTag("tfoot") << html::CR();
3080 xs << html::StartTag("tbody") << html::CR();
3081 for (row_type r = 0; r < nrows(); ++r) {
3082 if (isValidRow(r)) {
3083 ret += xhtmlRow(xs, r, runparams);
3086 xs << html::EndTag("tbody")
3088 << html::EndTag("table")
3090 if (is_long_tabular)
3091 xs << html::EndTag("div") << html::CR();
3096 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3097 vector<unsigned int> const & clen) const
3099 idx_type const fcell = getFirstCellInRow(row);
3100 idx_type const n = numberOfCellsInRow(row) + fcell;
3103 for (idx_type i = fcell; i < n; ++i) {
3113 for (idx_type i = fcell; i < n; ++i) {
3124 col_type column = cellColumn(i);
3125 int len = clen[column];
3126 while (column < ncols() - 1
3127 && isPartOfMultiColumn(row, ++column))
3128 len += clen[column] + 4;
3129 os << docstring(len, ch);
3144 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3145 vector<unsigned int> const & clen) const
3147 idx_type const fcell = getFirstCellInRow(row);
3148 idx_type const n = numberOfCellsInRow(row) + fcell;
3151 for (idx_type i = fcell; i < n; ++i) {
3152 if (bottomLine(i)) {
3161 for (idx_type i = fcell; i < n; ++i) {
3162 if (bottomLine(i)) {
3172 col_type column = cellColumn(i);
3173 int len = clen[column];
3174 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3175 len += clen[column] + 4;
3176 os << docstring(len, ch);
3177 if (bottomLine(i)) {
3191 void Tabular::plaintextPrintCell(odocstringstream & os,
3192 OutputParams const & runparams,
3193 idx_type cell, row_type row, col_type column,
3194 vector<unsigned int> const & clen,
3195 bool onlydata, size_t max_length) const
3197 odocstringstream sstr;
3198 cellInset(cell)->plaintext(sstr, runparams, max_length);
3210 unsigned int len1 = sstr.str().length();
3211 unsigned int len2 = clen[column];
3212 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3213 len2 += clen[column] + 4;
3216 switch (getAlignment(cell)) {
3218 case LYX_ALIGN_LEFT:
3221 case LYX_ALIGN_RIGHT:
3225 case LYX_ALIGN_CENTER:
3231 os << docstring(len1, ' ') << sstr.str()
3232 << docstring(len2, ' ');
3234 if (rightLine(cell))
3241 void Tabular::plaintext(odocstringstream & os,
3242 OutputParams const & runparams, int const depth,
3243 bool onlydata, char_type delim, size_t max_length) const
3245 // first calculate the width of the single columns
3246 vector<unsigned int> clen(ncols());
3249 // first all non multicolumn cells!
3250 for (col_type c = 0; c < ncols(); ++c) {
3252 for (row_type r = 0; r < nrows(); ++r) {
3253 idx_type cell = cellIndex(r, c);
3254 if (isMultiColumn(cell))
3256 odocstringstream sstr;
3257 cellInset(cell)->plaintext(sstr, runparams, max_length);
3258 if (clen[c] < sstr.str().length())
3259 clen[c] = sstr.str().length();
3262 // then all multicolumn cells!
3263 for (col_type c = 0; c < ncols(); ++c) {
3264 for (row_type r = 0; r < nrows(); ++r) {
3265 idx_type cell = cellIndex(r, c);
3266 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3268 odocstringstream sstr;
3269 cellInset(cell)->plaintext(sstr, runparams, max_length);
3270 int len = int(sstr.str().length());
3271 idx_type const n = columnSpan(cell);
3272 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3274 if (len > int(clen[c + n - 1]))
3275 clen[c + n - 1] = len;
3280 for (row_type r = 0; r < nrows(); ++r) {
3281 if (!onlydata && plaintextTopHLine(os, r, clen))
3282 os << docstring(depth * 2, ' ');
3283 for (col_type c = 0; c < ncols(); ++c) {
3284 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3286 if (onlydata && c > 0)
3287 // we don't use operator<< for single UCS4 character.
3288 // see explanation in docstream.h
3290 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3292 if (os.str().size() > max_length)
3297 os << docstring(depth * 2, ' ');
3298 if (plaintextBottomHLine(os, r, clen))
3299 os << docstring(depth * 2, ' ');
3301 if (os.str().size() > max_length)
3307 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3309 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3313 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3314 col_type column) const
3316 return cell_info[row][column].inset;
3320 void Tabular::setCellInset(row_type row, col_type column,
3321 shared_ptr<InsetTableCell> ins) const
3323 CellData & cd = cell_info[row][column];
3328 void Tabular::validate(LaTeXFeatures & features) const
3330 features.require("NeedTabularnewline");
3332 features.require("booktabs");
3333 if (is_long_tabular)
3334 features.require("longtable");
3336 features.require("rotating");
3337 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3338 if (isMultiRow(cell))
3339 features.require("multirow");
3340 if (getVAlignment(cell) != LYX_VALIGN_TOP
3341 || !getPWidth(cell).zero())
3342 features.require("array");
3343 cellInset(cell)->validate(features);
3348 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3350 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3351 ParagraphList::const_iterator cit = parlist.begin();
3352 ParagraphList::const_iterator end = parlist.end();
3354 for (; cit != end; ++cit)
3355 for (int i = 0; i < cit->size(); ++i)
3356 if (cit->isNewline(i))
3363 /////////////////////////////////////////////////////////////////////
3367 /////////////////////////////////////////////////////////////////////
3369 InsetTableCell::InsetTableCell(Buffer * buf)
3370 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3371 contentAlign(LYX_ALIGN_CENTER)
3375 bool InsetTableCell::forcePlainLayout(idx_type) const
3377 return !isFixedWidth;
3381 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3383 return isFixedWidth;
3387 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3388 FuncStatus & status) const
3390 bool enabled = true;
3391 switch (cmd.action()) {
3393 enabled = !forcePlainLayout();
3395 case LFUN_LAYOUT_PARAGRAPH:
3396 enabled = allowParagraphCustomization();
3399 case LFUN_MATH_DISPLAY:
3400 if (!hasFixedWidth()) {
3405 return InsetText::getStatus(cur, cmd, status);
3407 status.setEnabled(enabled);
3411 docstring InsetTableCell::asString(bool intoInsets)
3414 if (paragraphs().empty())
3416 ParagraphList::const_iterator it = paragraphs().begin();
3417 ParagraphList::const_iterator en = paragraphs().end();
3419 for (; it != en; ++it) {
3424 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3430 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3433 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3434 return InsetText::xhtml(xs, rp);
3439 /////////////////////////////////////////////////////////////////////
3443 /////////////////////////////////////////////////////////////////////
3445 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3447 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3448 rowselect_(false), colselect_(false)
3453 InsetTabular::InsetTabular(InsetTabular const & tab)
3454 : Inset(tab), tabular(tab.tabular), scx_(0)
3459 InsetTabular::~InsetTabular()
3461 hideDialogs("tabular", this);
3465 void InsetTabular::setBuffer(Buffer & buf)
3467 tabular.setBuffer(buf);
3468 Inset::setBuffer(buf);
3472 bool InsetTabular::insetAllowed(InsetCode code) const
3477 case MATHMACRO_CODE:
3482 return tabular.is_long_tabular;
3490 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3492 return tabular.is_long_tabular &&
3493 (newtype == "Standard" || newtype == "LongTableNoNumber");
3497 void InsetTabular::write(ostream & os) const
3499 os << "Tabular" << endl;
3504 string InsetTabular::contextMenu(BufferView const &, int, int) const
3506 // FIXME: depending on the selection state,
3507 // we could offer a different menu.
3508 return cell(0)->contextMenuName() + ";" + contextMenuName();
3512 string InsetTabular::contextMenuName() const
3514 return "context-tabular";
3518 void InsetTabular::read(Lexer & lex)
3520 //bool const old_format = (lex.getString() == "\\LyXTable");
3528 string token = lex.getString();
3529 while (lex && token != "\\end_inset") {
3531 token = lex.getString();
3534 lex.printError("Missing \\end_inset at this point. ");
3538 int InsetTabular::rowFromY(Cursor & cur, int y) const
3540 // top y coordinate of tabular
3541 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3543 for (; r < tabular.nrows() && y > h; ++r)
3544 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3545 + tabular.interRowSpace(r);
3551 int InsetTabular::columnFromX(Cursor & cur, int x) const
3553 // left x coordinate of tabular
3554 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3556 for (; c < tabular.ncols() && x > w; ++c)
3557 w += tabular.cellWidth(c);
3562 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3564 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3565 // mi.base.textwidth << "\n";
3566 LBUFERR(mi.base.bv);
3568 for (row_type r = 0; r < tabular.nrows(); ++r) {
3571 for (col_type c = 0; c < tabular.ncols(); ++c) {
3572 if (tabular.isPartOfMultiColumn(r, c)
3573 || tabular.isPartOfMultiRow(r, c))
3574 // multicolumn or multirow cell, but not first one
3576 idx_type const cell = tabular.cellIndex(r, c);
3579 Length const p_width = tabular.getPWidth(cell);
3580 if (!p_width.zero())
3581 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3582 tabular.cellInset(cell)->metrics(m, dim);
3583 if (!p_width.zero())
3584 dim.wid = m.base.textwidth;
3585 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3586 + tabular.interColumnSpace(cell);
3588 // FIXME(?): do we need a second metrics call?
3589 TextMetrics const & tm =
3590 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3592 // determine horizontal offset because of decimal align (if necessary)
3593 int decimal_width = 0;
3594 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3595 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3596 tail.setBuffer(tabular.buffer());
3597 // we need to set macrocontext position everywhere
3598 // otherwise we crash with nested insets (e.g. footnotes)
3599 // after decimal point
3600 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3602 dit.push_back(CursorSlice(tail));
3603 tail.setMacrocontextPositionRecursive(dit);
3605 // remove text leading decimal point
3606 docstring const align_d = tabular.column_info[c].decimal_point;
3607 dit = separatorPos(&tail, align_d);
3609 pit_type const psize = tail.paragraphs().front().size();
3611 tail.paragraphs().front().eraseChars(0,
3612 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3614 tail.metrics(m, dim1);
3615 decimal_width = dim1.width();
3618 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3619 tabular.cell_info[r][c].decimal_width = decimal_width;
3621 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3622 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3623 int const lastpardes = tm.last().second->descent()
3624 + TEXT_TO_INSET_OFFSET;
3626 switch (tabular.getVAlignment(cell)) {
3627 case Tabular::LYX_VALIGN_TOP:
3629 case Tabular::LYX_VALIGN_MIDDLE:
3630 offset = -(dim.des - lastpardes)/2;
3632 case Tabular::LYX_VALIGN_BOTTOM:
3633 offset = -(dim.des - lastpardes);
3636 tabular.cell_info[r][c].voffset = offset;
3637 maxasc = max(maxasc, dim.asc - offset);
3638 maxdes = max(maxdes, dim.des + offset);
3640 int const top_space = tabular.row_info[r].top_space_default ?
3641 default_line_space :
3642 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3643 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3644 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3645 default_line_space :
3646 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3647 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3650 // for top-alignment the first horizontal table line must be exactly at
3651 // the position of the base line of the surrounding text line
3652 // for bottom alignment, the same is for the last table line
3653 switch (tabular.tabular_valignment) {
3654 case Tabular::LYX_VALIGN_BOTTOM:
3655 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3657 case Tabular::LYX_VALIGN_MIDDLE:
3658 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3660 case Tabular::LYX_VALIGN_TOP:
3661 offset_valign_ = tabular.rowAscent(0);
3665 tabular.updateColumnWidths();
3666 dim.asc = tabular.rowAscent(0) - offset_valign_;
3667 dim.des = tabular.height() - dim.asc;
3668 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3672 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3675 if (&cur.inset() == this && cur.selection()) {
3676 if (cur.selIsMultiCell()) {
3679 getSelection(cur, rs, re, cs, ce);
3681 idx_type const cell = tabular.cellIndex(row, col);
3682 col_type const cspan = tabular.columnSpan(cell);
3683 row_type const rspan = tabular.rowSpan(cell);
3684 if (col + cspan - 1 >= cs && col <= ce
3685 && row + rspan - 1 >= rs && row <= re)
3688 if (col == tabular.cellColumn(cur.idx())
3689 && row == tabular.cellRow(cur.idx())) {
3690 CursorSlice const & beg = cur.selBegin();
3691 CursorSlice const & end = cur.selEnd();
3693 if ((end.lastpos() > 0 || end.lastpit() > 0)
3694 && end.pos() == end.lastpos() && beg.pos() == 0
3695 && end.pit() == end.lastpit() && beg.pit() == 0)
3703 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3705 x += scx_ + ADD_TO_TABULAR_WIDTH;
3707 BufferView * bv = pi.base.bv;
3708 Cursor & cur = pi.base.bv->cursor();
3711 // FIXME: As the full background is painted in drawBackground(),
3712 // we have no choice but to do a full repaint for the Text cells.
3713 pi.full_repaint = true;
3715 bool const original_selection_state = pi.selected;
3718 first_visible_cell = Tabular::npos;
3720 int yy = y + offset_valign_;
3721 for (row_type r = 0; r < tabular.nrows(); ++r) {
3723 for (col_type c = 0; c < tabular.ncols(); ++c) {
3724 if (tabular.isPartOfMultiColumn(r, c))
3727 idx = tabular.cellIndex(r, c);
3729 if (tabular.isPartOfMultiRow(r, c)) {
3730 nx += tabular.cellWidth(idx);
3734 if (first_visible_cell == Tabular::npos)
3735 first_visible_cell = idx;
3737 pi.selected |= isCellSelected(cur, r, c);
3738 int const cx = nx + tabular.textHOffset(idx);
3739 int const cy = yy + tabular.textVOffset(idx);
3740 // Cache the Inset position.
3741 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3742 cell(idx)->draw(pi, cx, cy);
3743 drawCellLines(pi, nx, yy, r, idx);
3744 nx += tabular.cellWidth(idx);
3745 pi.selected = original_selection_state;
3748 if (r + 1 < tabular.nrows())
3749 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3750 + tabular.interRowSpace(r + 1);
3755 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3757 x += scx_ + ADD_TO_TABULAR_WIDTH;
3758 y += offset_valign_ - tabular.rowAscent(0);
3759 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3760 pi.backgroundColor(this));
3764 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3766 Cursor & cur = pi.base.bv->cursor();
3769 x += scx_ + ADD_TO_TABULAR_WIDTH;
3771 if (!cur.selection())
3773 if (&cur.inset() != this)
3778 bool const full_cell_selected = isCellSelected(cur,
3779 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3781 if (cur.selIsMultiCell() || full_cell_selected) {
3782 for (row_type r = 0; r < tabular.nrows(); ++r) {
3784 for (col_type c = 0; c < tabular.ncols(); ++c) {
3785 if (tabular.isPartOfMultiColumn(r, c))
3788 idx_type const cell = tabular.cellIndex(r, c);
3790 if (tabular.isPartOfMultiRow(r, c)) {
3791 xx += tabular.cellWidth(cell);
3794 int const w = tabular.cellWidth(cell);
3795 int const h = tabular.cellHeight(cell);
3796 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3797 if (isCellSelected(cur, r, c))
3798 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3801 if (r + 1 < tabular.nrows())
3802 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3803 + tabular.interRowSpace(r + 1);
3807 // FIXME: This code has no effect because InsetTableCell does not handle
3808 // drawSelection other than the trivial implementation in Inset.
3810 // x += cellXPos(cur.idx());
3811 // x += tabular.textHOffset(cur.idx());
3812 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3817 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3818 row_type row, idx_type cell) const
3820 y -= tabular.rowAscent(row);
3821 int const w = tabular.cellWidth(cell);
3822 int const h = tabular.cellHeight(cell);
3823 Color const linecolor = pi.textColor(Color_tabularline);
3824 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3827 bool drawline = tabular.topLine(cell)
3828 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3829 pi.pain.line(x, y, x + w, y,
3830 drawline ? linecolor : gridcolor,
3831 drawline ? Painter::line_solid : Painter::line_onoffdash);
3834 drawline = tabular.bottomLine(cell);
3835 pi.pain.line(x, y + h, x + w, y + h,
3836 drawline ? linecolor : gridcolor,
3837 drawline ? Painter::line_solid : Painter::line_onoffdash);
3840 col_type const col = tabular.cellColumn(cell);
3841 drawline = tabular.leftLine(cell)
3842 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3843 pi.pain.line(x, y, x, y + h,
3844 drawline ? linecolor : gridcolor,
3845 drawline ? Painter::line_solid : Painter::line_onoffdash);
3848 x -= tabular.interColumnSpace(cell);
3849 col_type next_cell_col = col + 1;
3850 while (next_cell_col < tabular.ncols()
3851 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3853 drawline = tabular.rightLine(cell)
3854 || (next_cell_col < tabular.ncols()
3855 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3856 pi.pain.line(x + w, y, x + w, y + h,
3857 drawline ? linecolor : gridcolor,
3858 drawline ? Painter::line_solid : Painter::line_onoffdash);
3862 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3864 //lyxerr << "InsetTabular::edit: " << this << endl;
3866 cur.setSelection(false);
3869 if (isRightToLeft(cur))
3870 cur.idx() = tabular.getLastCellInRow(0);
3876 if (isRightToLeft(cur))
3877 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3879 cur.idx() = tabular.numberofcells - 1;
3881 cur.pos() = cur.lastpos(); // FIXME crude guess
3883 cur.setCurrentFont();
3884 // FIXME: this accesses the position cache before it is initialized
3886 //cur.bv().fitCursor();
3890 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3892 // In a longtable, tell captions what the current float is
3893 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3894 string const saveflt = cnts.current_float();
3895 if (tabular.is_long_tabular)
3896 cnts.current_float("table");
3898 ParIterator it2 = it;
3900 size_t const end = it2.nargs();
3901 for ( ; it2.idx() < end; it2.top().forwardIdx())
3902 buffer().updateBuffer(it2, utype);
3905 if (tabular.is_long_tabular)
3906 cnts.current_float(saveflt);
3910 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active) const
3912 DocIterator dit = cpit;
3914 size_t const end = dit.nargs();
3915 for ( ; dit.idx() < end; dit.top().forwardIdx())
3916 cell(dit.idx())->addToToc(dit, output_active);
3920 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3922 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3923 << "\n cur:" << cur);
3924 CursorSlice sl = cur.top();
3925 Cursor & bvcur = cur.bv().cursor();
3927 FuncCode const act = cmd.action();
3931 case LFUN_MOUSE_PRESS: {
3932 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3934 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3935 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3936 row_type r = rowFromY(cur, cmd.y());
3937 cur.idx() = tabular.getFirstCellInRow(r);
3940 cur.idx() = tabular.getLastCellInRow(r);
3941 cur.pos() = cur.lastpos();
3942 cur.setSelection(true);
3948 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3949 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3950 || cmd.y() > y0 + tabular.height()) {
3951 col_type c = columnFromX(cur, cmd.x());
3952 cur.idx() = tabular.cellIndex(0, c);
3955 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3956 cur.pos() = cur.lastpos();
3957 cur.setSelection(true);
3962 // do not reset cursor/selection if we have selected
3963 // some cells (bug 2715).
3964 if (cmd.button() == mouse_button::button3
3965 && &bvcur.selBegin().inset() == this
3966 && bvcur.selIsMultiCell())
3969 // Let InsetTableCell do it
3970 cell(cur.idx())->dispatch(cur, cmd);
3973 case LFUN_MOUSE_MOTION:
3974 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3975 if (cmd.button() == mouse_button::button1) {
3976 // only accept motions to places not deeper nested than the real anchor
3977 if (!bvcur.realAnchor().hasPart(cur)) {
3981 // select (additional) row
3983 row_type r = rowFromY(cur, cmd.y());
3984 cur.idx() = tabular.getLastCellInRow(r);
3985 // we need to reset the cursor's pit and pos now, as the old ones
3986 // may no longer be valid.
3989 bvcur.setCursor(cur);
3990 bvcur.setSelection(true);
3993 // select (additional) column
3995 col_type c = columnFromX(cur, cmd.x());
3996 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3997 // we need to reset the cursor's pit and pos now, as the old ones
3998 // may no longer be valid.
4001 bvcur.setCursor(cur);
4002 bvcur.setSelection(true);
4005 // only update if selection changes
4006 if (bvcur.idx() == cur.idx() &&
4007 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4008 cur.noScreenUpdate();
4009 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4010 bvcur.setCursor(cur);
4011 bvcur.setSelection(true);
4012 // if this is a multicell selection, we just set the cursor to
4013 // the beginning of the cell's text.
4014 if (bvcur.selIsMultiCell()) {
4015 bvcur.pit() = bvcur.lastpit();
4016 bvcur.pos() = bvcur.lastpos();
4021 case LFUN_MOUSE_RELEASE:
4026 case LFUN_CELL_BACKWARD:
4028 cur.setSelection(false);
4031 case LFUN_CELL_FORWARD:
4033 cur.setSelection(false);
4036 case LFUN_CHAR_FORWARD_SELECT:
4037 case LFUN_CHAR_FORWARD:
4038 case LFUN_CHAR_BACKWARD_SELECT:
4039 case LFUN_CHAR_BACKWARD:
4040 case LFUN_CHAR_RIGHT_SELECT:
4041 case LFUN_CHAR_RIGHT:
4042 case LFUN_CHAR_LEFT_SELECT:
4043 case LFUN_CHAR_LEFT:
4044 case LFUN_WORD_FORWARD:
4045 case LFUN_WORD_FORWARD_SELECT:
4046 case LFUN_WORD_BACKWARD:
4047 case LFUN_WORD_BACKWARD_SELECT:
4048 case LFUN_WORD_RIGHT:
4049 case LFUN_WORD_RIGHT_SELECT:
4050 case LFUN_WORD_LEFT:
4051 case LFUN_WORD_LEFT_SELECT: {
4052 // determine whether we move to next or previous cell, where to enter
4053 // the new cell from, and which command to "finish" (i.e., exit the
4056 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4057 FuncCode finish_lfun;
4059 if (act == LFUN_CHAR_FORWARD
4060 || act == LFUN_CHAR_FORWARD_SELECT
4061 || act == LFUN_WORD_FORWARD
4062 || act == LFUN_WORD_FORWARD_SELECT) {
4064 finish_lfun = LFUN_FINISHED_FORWARD;
4066 else if (act == LFUN_CHAR_BACKWARD
4067 || act == LFUN_CHAR_BACKWARD_SELECT
4068 || act == LFUN_WORD_BACKWARD
4069 || act == LFUN_WORD_BACKWARD_SELECT) {
4071 finish_lfun = LFUN_FINISHED_BACKWARD;
4073 // LEFT or RIGHT commands --- the interpretation will depend on the
4074 // table's direction.
4076 bool const right = act == LFUN_CHAR_RIGHT
4077 || act == LFUN_CHAR_RIGHT_SELECT
4078 || act == LFUN_WORD_RIGHT
4079 || act == LFUN_WORD_RIGHT_SELECT;
4080 next_cell = isRightToLeft(cur) != right;
4082 if (lyxrc.visual_cursor)
4083 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4085 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4088 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4089 || act == LFUN_CHAR_BACKWARD_SELECT
4090 || act == LFUN_CHAR_RIGHT_SELECT
4091 || act == LFUN_CHAR_LEFT_SELECT
4092 || act == LFUN_WORD_FORWARD_SELECT
4093 || act == LFUN_WORD_RIGHT_SELECT
4094 || act == LFUN_WORD_BACKWARD_SELECT
4095 || act == LFUN_WORD_LEFT_SELECT;
4097 // If we have a multicell selection or we're
4098 // not doing some LFUN_*_SELECT thing anyway...
4099 if (!cur.selIsMultiCell() || !select) {
4100 col_type const c = tabular.cellColumn(cur.idx());
4101 row_type const r = tabular.cellRow(cur.idx());
4102 // Are we trying to select the whole cell and is the whole cell
4103 // not yet selected?
4104 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4105 ((next_cell && cur.pit() == cur.lastpit()
4106 && cur.pos() == cur.lastpos())
4107 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4109 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4111 // ...try to dispatch to the cell's inset.
4112 cell(cur.idx())->dispatch(cur, cmd);
4114 // When we already have a selection we want to select the whole cell
4115 // before going to the next cell.
4116 if (select_whole && !empty_cell){
4117 getText(cur.idx())->selectAll(cur);
4119 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4123 // FIXME: When we support the selection of an empty cell, remove
4124 // the !empty_cell from this condition. For now we jump to the next
4125 // cell if the current cell is empty.
4126 if (cur.result().dispatched() && !empty_cell)
4130 // move to next/prev cell, as appropriate
4131 // note that we will always do this if we're selecting and we have
4132 // a multicell selection
4133 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4134 << " cell from: " << int(entry_from));
4136 moveNextCell(cur, entry_from);
4138 movePrevCell(cur, entry_from);
4139 // if we're exiting the table, call the appropriate FINISHED lfun
4140 if (sl == cur.top())
4141 cmd = FuncRequest(finish_lfun);
4145 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4150 case LFUN_DOWN_SELECT:
4152 if (!(cur.selection() && cur.selIsMultiCell()))
4153 cell(cur.idx())->dispatch(cur, cmd);
4155 cur.dispatched(); // override the cell's decision
4156 if (sl == cur.top()) {
4157 // if our Text didn't do anything to the cursor
4158 // then we try to put the cursor into the cell below
4159 // setting also the right targetX.
4160 cur.selHandle(act == LFUN_DOWN_SELECT);
4161 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4162 int const xtarget = cur.targetX();
4163 // WARNING: Once cur.idx() has been reset, the cursor is in
4164 // an inconsistent state until pos() has been set. Be careful
4165 // what you do with it!
4166 cur.idx() = tabular.cellBelow(cur.idx());
4168 TextMetrics const & tm =
4169 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4170 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4171 cur.setCurrentFont();
4174 if (sl == cur.top()) {
4175 // we trick it to go to forward after leaving the
4177 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4180 if (cur.selIsMultiCell()) {
4181 cur.pit() = cur.lastpit();
4182 cur.pos() = cur.lastpos();
4183 cur.setCurrentFont();
4184 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4187 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4190 case LFUN_UP_SELECT:
4192 if (!(cur.selection() && cur.selIsMultiCell()))
4193 cell(cur.idx())->dispatch(cur, cmd);
4194 cur.dispatched(); // override the cell's decision
4195 if (sl == cur.top()) {
4196 // if our Text didn't do anything to the cursor
4197 // then we try to put the cursor into the cell above
4198 // setting also the right targetX.
4199 cur.selHandle(act == LFUN_UP_SELECT);
4200 if (tabular.cellRow(cur.idx()) != 0) {
4201 int const xtarget = cur.targetX();
4202 // WARNING: Once cur.idx() has been reset, the cursor is in
4203 // an inconsistent state until pos() has been set. Be careful
4204 // what you do with it!
4205 cur.idx() = tabular.cellAbove(cur.idx());
4206 cur.pit() = cur.lastpit();
4207 Text const * text = cell(cur.idx())->getText(0);
4208 TextMetrics const & tm = cur.bv().textMetrics(text);
4209 ParagraphMetrics const & pm =
4210 tm.parMetrics(cur.lastpit());
4211 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4212 cur.setCurrentFont();
4215 if (sl == cur.top()) {
4216 cmd = FuncRequest(LFUN_UP);
4219 if (cur.selIsMultiCell()) {
4221 cur.pos() = cur.lastpos();
4222 cur.setCurrentFont();
4223 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4226 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4229 // case LFUN_SCREEN_DOWN: {
4230 // //if (hasSelection())
4231 // // cur.selection() = false;
4232 // col_type const col = tabular.cellColumn(cur.idx());
4233 // int const t = cur.bv().top_y() + cur.bv().height();
4234 // if (t < yo() + tabular.getHeightOfTabular()) {
4235 // cur.bv().scrollDocView(t, true);
4236 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4238 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4245 // case LFUN_SCREEN_UP: {
4246 // //if (hasSelection())
4247 // // cur.selection() = false;
4248 // col_type const col = tabular.cellColumn(cur.idx());
4249 // int const t = cur.bv().top_y() + cur.bv().height();
4251 // cur.bv().scrollDocView(t, true);
4255 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4259 // cur.par() = cur.lastpar();
4260 // cur.pos() = cur.lastpos();
4264 case LFUN_LAYOUT_TABULAR:
4265 cur.bv().showDialog("tabular");
4268 case LFUN_INSET_MODIFY: {
4270 if (cmd.getArg(1) == "from-dialog")
4271 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4273 arg = to_utf8(cmd.argument());
4274 if (!tabularFeatures(cur, arg))
4279 // insert file functions
4280 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4281 case LFUN_FILE_INSERT_PLAINTEXT:
4283 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4284 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4285 FileName(to_utf8(cmd.argument())));
4288 cur.recordUndoInset(INSERT_UNDO);
4289 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4290 // content has been replaced,
4291 // so cursor might be invalid
4292 cur.pos() = cur.lastpos();
4293 cur.pit() = cur.lastpit();
4294 bvcur.setCursor(cur);
4301 if (cur.selIsMultiCell()) {
4302 if (copySelection(cur)) {
4303 cur.recordUndoInset(DELETE_UNDO);
4307 cell(cur.idx())->dispatch(cur, cmd);
4310 case LFUN_SELF_INSERT:
4311 if (cur.selIsMultiCell()) {
4312 cur.recordUndoInset(DELETE_UNDO);
4315 cell(cur.idx())->dispatch(cur, cmd);
4318 case LFUN_CHAR_DELETE_BACKWARD:
4319 case LFUN_CHAR_DELETE_FORWARD:
4320 if (cur.selIsMultiCell()) {
4321 cur.recordUndoInset(DELETE_UNDO);
4324 cell(cur.idx())->dispatch(cur, cmd);
4328 if (!cur.selection())
4330 if (cur.selIsMultiCell()) {
4334 cell(cur.idx())->dispatch(cur, cmd);
4337 case LFUN_CLIPBOARD_PASTE:
4338 case LFUN_PRIMARY_SELECTION_PASTE: {
4339 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4340 theClipboard().getAsText(Clipboard::PlainTextType) :
4341 theSelection().get();
4344 // pass to InsertPlaintextString, but
4345 // only if we have multi-cell content
4346 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4347 cur.recordUndoInset(INSERT_UNDO);
4348 if (insertPlaintextString(cur.bv(), clip, false)) {
4349 // content has been replaced,
4350 // so cursor might be invalid
4351 cur.pos() = cur.lastpos();
4352 cur.pit() = cur.lastpit();
4353 bvcur.setCursor(cur);
4357 // Let the cell handle normal text
4358 cell(cur.idx())->dispatch(cur, cmd);
4363 if (!tabularStackDirty()) {
4364 if (!cur.selIsMultiCell())
4365 cell(cur.idx())->dispatch(cur, cmd);
4368 if (theClipboard().isInternal()) {
4369 cur.recordUndoInset(INSERT_UNDO);
4370 pasteClipboard(cur);
4374 case LFUN_FONT_EMPH:
4375 case LFUN_FONT_BOLD:
4376 case LFUN_FONT_BOLDSYMBOL:
4377 case LFUN_FONT_ROMAN:
4378 case LFUN_FONT_NOUN:
4379 case LFUN_FONT_ITAL:
4380 case LFUN_FONT_FRAK:
4381 case LFUN_FONT_TYPEWRITER:
4382 case LFUN_FONT_SANS:
4383 case LFUN_TEXTSTYLE_APPLY:
4384 case LFUN_TEXTSTYLE_UPDATE:
4385 case LFUN_FONT_SIZE:
4386 case LFUN_FONT_UNDERLINE:
4387 case LFUN_FONT_STRIKEOUT:
4388 case LFUN_FONT_UULINE:
4389 case LFUN_FONT_UWAVE:
4391 case LFUN_PARAGRAPH_PARAMS_APPLY:
4392 case LFUN_PARAGRAPH_PARAMS:
4393 case LFUN_WORD_CAPITALIZE:
4394 case LFUN_WORD_UPCASE:
4395 case LFUN_WORD_LOWCASE:
4396 case LFUN_CHARS_TRANSPOSE:
4397 if (cur.selIsMultiCell()) {
4400 getSelection(cur, rs, re, cs, ce);
4401 Cursor tmpcur = cur;
4402 for (row_type r = rs; r <= re; ++r) {
4403 for (col_type c = cs; c <= ce; ++c) {
4404 // cursor follows cell:
4405 tmpcur.idx() = tabular.cellIndex(r, c);
4406 // select this cell only:
4409 tmpcur.resetAnchor();
4410 tmpcur.pit() = tmpcur.lastpit();
4411 tmpcur.pos() = tmpcur.top().lastpos();
4412 tmpcur.setCursor(tmpcur);
4413 tmpcur.setSelection();
4414 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4419 cell(cur.idx())->dispatch(cur, cmd);
4423 case LFUN_INSET_SETTINGS:
4424 // relay this lfun to Inset, not to the cell.
4425 Inset::doDispatch(cur, cmd);
4429 // we try to handle this event in the insets dispatch function.
4430 cell(cur.idx())->dispatch(cur, cmd);
4436 // function sets an object as defined in func_status.h:
4437 // states OK, Unknown, Disabled, On, Off.
4438 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4439 FuncStatus & status) const
4441 switch (cmd.action()) {
4442 case LFUN_INSET_MODIFY: {
4443 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4446 // FIXME: We only check for the very first argument...
4447 string const s = cmd.getArg(1);
4448 // We always enable the lfun if it is coming from the dialog
4449 // because the dialog makes sure all the settings are valid,
4450 // even though the first argument might not be valid now.
4451 if (s == "from-dialog") {
4452 status.setEnabled(true);
4456 int action = Tabular::LAST_ACTION;
4458 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4459 if (tabularFeature[i].feature == s) {
4460 action = tabularFeature[i].action;
4464 if (action == Tabular::LAST_ACTION) {
4466 status.setUnknown(true);
4470 string const argument = cmd.getLongArg(2);
4472 row_type sel_row_start = 0;
4473 row_type sel_row_end = 0;
4474 col_type sel_col_start = 0;
4475 col_type sel_col_end = 0;
4476 Tabular::ltType dummyltt;
4479 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4482 case Tabular::SET_PWIDTH:
4483 case Tabular::SET_MPWIDTH:
4484 case Tabular::SET_SPECIAL_COLUMN:
4485 case Tabular::SET_SPECIAL_MULTICOLUMN:
4486 case Tabular::APPEND_ROW:
4487 case Tabular::APPEND_COLUMN:
4488 case Tabular::DELETE_ROW:
4489 case Tabular::DELETE_COLUMN:
4490 case Tabular::COPY_ROW:
4491 case Tabular::COPY_COLUMN:
4492 case Tabular::SET_TOP_SPACE:
4493 case Tabular::SET_BOTTOM_SPACE:
4494 case Tabular::SET_INTERLINE_SPACE:
4498 case Tabular::SET_TABULAR_WIDTH:
4499 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4500 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4503 case Tabular::MOVE_COLUMN_RIGHT:
4504 case Tabular::MOVE_COLUMN_LEFT:
4505 case Tabular::MOVE_ROW_DOWN:
4506 case Tabular::MOVE_ROW_UP: {
4507 if (cur.selection()) {
4508 status.message(_("Selections not supported."));
4509 status.setEnabled(false);
4513 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4514 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4515 (action == Tabular::MOVE_COLUMN_LEFT &&
4516 tabular.cellColumn(cur.idx()) == 0) ||
4517 (action == Tabular::MOVE_ROW_DOWN &&
4518 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4519 (action == Tabular::MOVE_ROW_UP &&
4520 tabular.cellRow(cur.idx()) == 0)) {
4521 status.setEnabled(false);
4525 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4526 action == Tabular::MOVE_COLUMN_LEFT) {
4527 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4528 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4529 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4530 status.message(_("Multi-column in current or"
4531 " destination column."));
4532 status.setEnabled(false);
4537 if (action == Tabular::MOVE_ROW_DOWN ||
4538 action == Tabular::MOVE_ROW_UP) {
4539 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4540 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4541 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4542 status.message(_("Multi-row in current or"
4543 " destination row."));
4544 status.setEnabled(false);
4549 status.setEnabled(true);
4553 case Tabular::SET_DECIMAL_POINT:
4555 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4558 case Tabular::SET_MULTICOLUMN:
4559 case Tabular::UNSET_MULTICOLUMN:
4560 case Tabular::MULTICOLUMN:
4561 // If a row is set as longtable caption, it must not be allowed
4562 // to unset that this row is a multicolumn.
4563 status.setEnabled(sel_row_start == sel_row_end
4564 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4565 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4568 case Tabular::SET_MULTIROW:
4569 case Tabular::UNSET_MULTIROW:
4570 case Tabular::MULTIROW:
4571 // If a row is set as longtable caption, it must not be allowed
4572 // to unset that this row is a multirow.
4573 status.setEnabled(sel_col_start == sel_col_end
4574 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4575 status.setOnOff(tabular.isMultiRow(cur.idx()));
4578 case Tabular::SET_ALL_LINES:
4579 case Tabular::UNSET_ALL_LINES:
4580 case Tabular::SET_BORDER_LINES:
4581 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4584 case Tabular::SET_LINE_TOP:
4585 case Tabular::SET_LINE_BOTTOM:
4586 case Tabular::SET_LINE_LEFT:
4587 case Tabular::SET_LINE_RIGHT:
4588 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4591 case Tabular::TOGGLE_LINE_TOP:
4592 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4593 status.setOnOff(tabular.topLine(cur.idx()));
4596 case Tabular::TOGGLE_LINE_BOTTOM:
4597 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4598 status.setOnOff(tabular.bottomLine(cur.idx()));
4601 case Tabular::TOGGLE_LINE_LEFT:
4602 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4603 status.setOnOff(tabular.leftLine(cur.idx()));
4606 case Tabular::TOGGLE_LINE_RIGHT:
4607 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4608 status.setOnOff(tabular.rightLine(cur.idx()));
4611 // multirow cells only inherit the alignment of the column if the column has
4612 // no width, otherwise they are left-aligned
4613 // therefore allow always left but right and center only if there is no width
4614 case Tabular::M_ALIGN_LEFT:
4616 case Tabular::ALIGN_LEFT:
4617 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4620 case Tabular::M_ALIGN_RIGHT:
4622 case Tabular::ALIGN_RIGHT:
4623 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4624 && !tabular.getPWidth(cur.idx()).zero()));
4625 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4628 case Tabular::M_ALIGN_CENTER:
4630 case Tabular::ALIGN_CENTER:
4631 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4632 && !tabular.getPWidth(cur.idx()).zero()));
4633 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4636 case Tabular::ALIGN_BLOCK:
4637 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4638 && !tabular.isMultiRow(cur.idx()));
4639 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4642 case Tabular::ALIGN_DECIMAL:
4643 status.setEnabled(!tabular.isMultiRow(cur.idx())
4644 && !tabular.isMultiColumn(cur.idx()));
4645 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4648 case Tabular::M_VALIGN_TOP:
4650 case Tabular::VALIGN_TOP:
4651 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4652 && !tabular.isMultiRow(cur.idx()));
4654 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4657 case Tabular::M_VALIGN_BOTTOM:
4659 case Tabular::VALIGN_BOTTOM:
4660 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4661 && !tabular.isMultiRow(cur.idx()));
4663 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4666 case Tabular::M_VALIGN_MIDDLE:
4668 case Tabular::VALIGN_MIDDLE:
4669 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4670 && !tabular.isMultiRow(cur.idx()));
4672 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4675 case Tabular::SET_LONGTABULAR:
4676 // setting as longtable is not allowed when table is inside a float
4677 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4678 || cur.innerInsetOfType(WRAP_CODE) != 0)
4679 status.setEnabled(false);
4681 status.setEnabled(true);
4682 status.setOnOff(tabular.is_long_tabular);
4685 case Tabular::UNSET_LONGTABULAR:
4686 status.setOnOff(!tabular.is_long_tabular);
4689 case Tabular::TOGGLE_ROTATE_TABULAR:
4690 case Tabular::SET_ROTATE_TABULAR:
4691 status.setOnOff(tabular.rotate != 0);
4694 case Tabular::TABULAR_VALIGN_TOP:
4695 status.setEnabled(tabular.tabular_width.zero());
4696 status.setOnOff(tabular.tabular_valignment
4697 == Tabular::LYX_VALIGN_TOP);
4699 case Tabular::TABULAR_VALIGN_MIDDLE:
4700 status.setEnabled(tabular.tabular_width.zero());
4701 status.setOnOff(tabular.tabular_valignment
4702 == Tabular::LYX_VALIGN_MIDDLE);
4704 case Tabular::TABULAR_VALIGN_BOTTOM:
4705 status.setEnabled(tabular.tabular_width.zero());
4706 status.setOnOff(tabular.tabular_valignment
4707 == Tabular::LYX_VALIGN_BOTTOM);
4710 case Tabular::LONGTABULAR_ALIGN_LEFT:
4711 status.setOnOff(tabular.longtabular_alignment
4712 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4714 case Tabular::LONGTABULAR_ALIGN_CENTER:
4715 status.setOnOff(tabular.longtabular_alignment
4716 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4718 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4719 status.setOnOff(tabular.longtabular_alignment
4720 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4723 case Tabular::UNSET_ROTATE_TABULAR:
4724 status.setOnOff(tabular.rotate == 0);
4727 case Tabular::TOGGLE_ROTATE_CELL:
4728 case Tabular::SET_ROTATE_CELL:
4729 status.setOnOff(!oneCellHasRotationState(false,
4730 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4733 case Tabular::UNSET_ROTATE_CELL:
4734 status.setOnOff(!oneCellHasRotationState(true,
4735 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4738 case Tabular::SET_USEBOX:
4739 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4742 // every row can only be one thing:
4743 // either a footer or header
4744 case Tabular::SET_LTFIRSTHEAD:
4745 status.setEnabled(sel_row_start == sel_row_end);
4746 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4749 case Tabular::UNSET_LTFIRSTHEAD:
4750 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4751 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4754 case Tabular::SET_LTHEAD:
4755 status.setEnabled(sel_row_start == sel_row_end);
4756 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4759 case Tabular::UNSET_LTHEAD:
4760 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4761 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4764 case Tabular::SET_LTFOOT:
4765 status.setEnabled(sel_row_start == sel_row_end);
4766 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4769 case Tabular::UNSET_LTFOOT:
4770 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4771 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4774 case Tabular::SET_LTLASTFOOT:
4775 status.setEnabled(sel_row_start == sel_row_end);
4776 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4779 case Tabular::UNSET_LTLASTFOOT:
4780 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4781 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4784 case Tabular::SET_LTNEWPAGE:
4785 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4787 case Tabular::UNSET_LTNEWPAGE:
4788 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4791 // only one row in head/firsthead/foot/lasthead can be the caption
4792 // and a multirow cannot be set as caption
4793 case Tabular::SET_LTCAPTION:
4794 status.setEnabled(sel_row_start == sel_row_end
4795 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4796 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4797 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4798 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4799 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4800 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4801 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4802 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4803 && !tabular.isMultiRow(sel_row_start));
4804 status.setOnOff(tabular.ltCaption(sel_row_start));
4807 case Tabular::UNSET_LTCAPTION:
4808 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4811 case Tabular::TOGGLE_LTCAPTION:
4812 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4813 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4814 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4815 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4816 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4817 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4818 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4819 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4820 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4821 status.setOnOff(tabular.ltCaption(sel_row_start));
4824 case Tabular::SET_BOOKTABS:
4825 status.setOnOff(tabular.use_booktabs);
4828 case Tabular::UNSET_BOOKTABS:
4829 status.setOnOff(!tabular.use_booktabs);
4834 status.setEnabled(false);
4840 case LFUN_CAPTION_INSERT: {
4841 // caption is only allowed in caption cell of longtable
4842 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4843 status.setEnabled(false);
4846 // only standard caption is allowed
4847 string arg = cmd.getArg(0);
4848 if (!arg.empty() && arg != "Standard") {
4849 status.setEnabled(false);
4852 // check if there is already a caption
4853 bool have_caption = false;
4854 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4855 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4856 ParagraphList::const_iterator pend = itc.paragraphs().end();
4857 for (; pit != pend; ++pit) {
4858 InsetList::const_iterator it = pit->insetList().begin();
4859 InsetList::const_iterator end = pit->insetList().end();
4860 for (; it != end; ++it) {
4861 if (it->inset->lyxCode() == CAPTION_CODE) {
4862 have_caption = true;
4867 status.setEnabled(!have_caption);
4871 // These are only enabled inside tabular
4872 case LFUN_CELL_BACKWARD:
4873 case LFUN_CELL_FORWARD:
4874 status.setEnabled(true);
4877 // disable these with multiple cells selected
4878 case LFUN_INSET_INSERT:
4879 case LFUN_TABULAR_INSERT:
4880 case LFUN_FLEX_INSERT:
4881 case LFUN_FLOAT_INSERT:
4882 case LFUN_FLOAT_WIDE_INSERT:
4883 case LFUN_FOOTNOTE_INSERT:
4884 case LFUN_MARGINALNOTE_INSERT:
4885 case LFUN_MATH_INSERT:
4886 case LFUN_MATH_MODE:
4887 case LFUN_MATH_MUTATE:
4888 case LFUN_MATH_DISPLAY:
4889 case LFUN_NOTE_INSERT:
4890 case LFUN_ARGUMENT_INSERT:
4891 case LFUN_BOX_INSERT:
4892 case LFUN_BRANCH_INSERT:
4893 case LFUN_PHANTOM_INSERT:
4894 case LFUN_WRAP_INSERT:
4895 case LFUN_PREVIEW_INSERT:
4896 case LFUN_ERT_INSERT: {
4897 if (cur.selIsMultiCell()) {
4898 status.setEnabled(false);
4901 return cell(cur.idx())->getStatus(cur, cmd, status);
4904 // disable in non-fixed-width cells
4905 case LFUN_PARAGRAPH_BREAK:
4906 // multirow does not allow paragraph breaks
4907 if (tabular.isMultiRow(cur.idx())) {
4908 status.setEnabled(false);
4911 case LFUN_NEWLINE_INSERT: {
4912 if (tabular.getPWidth(cur.idx()).zero()) {
4913 status.setEnabled(false);
4916 return cell(cur.idx())->getStatus(cur, cmd, status);
4919 case LFUN_NEWPAGE_INSERT:
4920 status.setEnabled(false);
4924 if (tabularStackDirty() && theClipboard().isInternal()) {
4925 if (cur.selIsMultiCell()) {
4928 getSelection(cur, rs, re, cs, ce);
4929 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4930 && paste_tabular->nrows() == re - rs + 1)
4931 status.setEnabled(true);
4933 status.setEnabled(false);
4934 status.message(_("Selection size should match clipboard content."));
4937 status.setEnabled(true);
4940 return cell(cur.idx())->getStatus(cur, cmd, status);
4942 case LFUN_INSET_SETTINGS:
4943 // relay this lfun to Inset, not to the cell.
4944 return Inset::getStatus(cur, cmd, status);
4947 // we try to handle this event in the insets dispatch function.
4948 return cell(cur.idx())->getStatus(cur, cmd, status);
4954 Inset::DisplayType InsetTabular::display() const
4956 if (tabular.is_long_tabular) {
4957 switch (tabular.longtabular_alignment) {
4958 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4960 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4962 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4972 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4974 tabular.latex(os, runparams);
4978 int InsetTabular::plaintext(odocstringstream & os,
4979 OutputParams const & runparams, size_t max_length) const
4981 os << '\n'; // output table on a new line
4982 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4983 tabular.plaintext(os, runparams, dp, false, 0, max_length);
4984 return PLAINTEXT_NEWLINE;
4988 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4993 // FIXME: Why not pass a proper DocIterator here?
4995 // if the table is inside a float it doesn't need the informaltable
4996 // wrapper. Search for it.
4997 for (master = owner(); master; master = master->owner())
4998 if (master->lyxCode() == FLOAT_CODE)
5003 os << "<informaltable>";
5006 ret += tabular.docbook(os, runparams);
5008 os << "</informaltable>";
5015 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5017 return tabular.xhtml(xs, rp);
5021 void InsetTabular::validate(LaTeXFeatures & features) const
5023 tabular.validate(features);
5025 // It'd be better to be able to get this from an InsetLayout, but at present
5026 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5027 if (features.runparams().flavor == OutputParams::HTML)
5028 features.addCSSSnippet(
5029 "table { border: 1px solid black; display: inline-block; }\n"
5030 "td { border: 1px solid black; padding: 0.5ex; }");
5034 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5036 return tabular.cellInset(idx);
5040 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5042 return tabular.cellInset(idx);
5046 void InsetTabular::cursorPos(BufferView const & bv,
5047 CursorSlice const & sl, bool boundary, int & x, int & y) const
5049 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5051 // y offset correction
5052 y += cellYPos(sl.idx());
5053 y += tabular.textVOffset(sl.idx());
5054 y += offset_valign_;
5056 // x offset correction
5057 x += cellXPos(sl.idx());
5058 x += tabular.textHOffset(sl.idx());
5059 x += ADD_TO_TABULAR_WIDTH;
5064 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5068 Inset const & inset = *tabular.cellInset(cell);
5069 Point o = bv.coordCache().getInsets().xy(&inset);
5070 int const xbeg = o.x_ - tabular.textHOffset(cell);
5071 int const xend = xbeg + tabular.cellWidth(cell);
5072 row_type const row = tabular.cellRow(cell);
5073 int const ybeg = o.y_ - tabular.rowAscent(row)
5074 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5075 int const yend = ybeg + tabular.cellHeight(cell);
5087 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5088 // << " ybeg=" << ybeg << " yend=" << yend
5089 // << " xx=" << xx << " yy=" << yy
5090 // << " dist=" << xx + yy << endl;
5095 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5097 //lyxerr << "InsetTabular::editXY: " << this << endl;
5098 cur.setSelection(false);
5100 cur.idx() = getNearestCell(cur.bv(), x, y);
5102 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5106 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5108 cur.idx() = getNearestCell(cur.bv(), x, y);
5109 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5113 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5115 idx_type idx_min = 0;
5116 int dist_min = numeric_limits<int>::max();
5117 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5118 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5119 int const d = dist(bv, i, x, y);
5130 int InsetTabular::cellYPos(idx_type const cell) const
5132 row_type row = tabular.cellRow(cell);
5134 for (row_type r = 0; r < row; ++r)
5135 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5136 + tabular.interRowSpace(r + 1);
5141 int InsetTabular::cellXPos(idx_type const cell) const
5143 col_type col = tabular.cellColumn(cell);
5145 for (col_type c = 0; c < col; ++c)
5146 lx += tabular.column_info[c].width;
5151 void InsetTabular::resetPos(Cursor & cur) const
5153 BufferView & bv = cur.bv();
5154 int const maxwidth = bv.workWidth();
5156 int const scx_old = scx_;
5157 int const i = cur.find(this);
5162 int const X2 = maxwidth;
5163 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5164 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5165 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5168 scx_ = X1 + 20 - x1;
5170 scx_ = X2 - 20 - x2;
5175 // only update if offset changed
5176 if (scx_ != scx_old)
5177 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5181 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5183 row_type const row = tabular.cellRow(cur.idx());
5184 col_type const col = tabular.cellColumn(cur.idx());
5186 if (isRightToLeft(cur)) {
5187 if (tabular.cellColumn(cur.idx()) == 0) {
5188 if (row == tabular.nrows() - 1)
5190 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5195 cur.idx() = tabular.getLastCellInRow(row - 1);
5197 cur.idx() = tabular.cellIndex(row, col - 1);
5200 if (tabular.isLastCell(cur.idx()))
5202 if (cur.idx() == tabular.getLastCellInRow(row))
5203 cur.idx() = tabular.cellIndex(row + 1, 0);
5205 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5206 cur.idx() = tabular.cellIndex(row, colnextcell);
5210 cur.boundary(false);
5212 if (cur.selIsMultiCell()) {
5213 cur.pit() = cur.lastpit();
5214 cur.pos() = cur.lastpos();
5222 // in visual mode, place cursor at extreme left or right
5224 switch(entry_from) {
5226 case ENTRY_DIRECTION_RIGHT:
5227 cur.posVisToRowExtremity(false /* !left */);
5229 case ENTRY_DIRECTION_LEFT:
5230 cur.posVisToRowExtremity(true /* left */);
5232 case ENTRY_DIRECTION_IGNORE:
5233 // nothing to do in this case
5237 cur.setCurrentFont();
5242 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5244 row_type const row = tabular.cellRow(cur.idx());
5245 col_type const col = tabular.cellColumn(cur.idx());
5247 if (isRightToLeft(cur)) {
5248 if (cur.idx() == tabular.getLastCellInRow(row)) {
5251 cur.idx() = tabular.getFirstCellInRow(row);
5252 cur.idx() = tabular.cellAbove(cur.idx());
5254 if (tabular.isLastCell(cur.idx()))
5256 if (cur.idx() == tabular.getLastCellInRow(row))
5257 cur.idx() = tabular.cellIndex(row + 1, 0);
5259 cur.idx() = tabular.cellIndex(row, col + 1);
5262 if (cur.idx() == 0) // first cell
5265 cur.idx() = tabular.getLastCellInRow(row - 1);
5267 cur.idx() = tabular.cellIndex(row, col - 1);
5270 if (cur.selIsMultiCell()) {
5271 cur.pit() = cur.lastpit();
5272 cur.pos() = cur.lastpos();
5277 cur.pit() = cur.lastpit();
5278 cur.pos() = cur.lastpos();
5280 // in visual mode, place cursor at extreme left or right
5282 switch(entry_from) {
5284 case ENTRY_DIRECTION_RIGHT:
5285 cur.posVisToRowExtremity(false /* !left */);
5287 case ENTRY_DIRECTION_LEFT:
5288 cur.posVisToRowExtremity(true /* left */);
5290 case ENTRY_DIRECTION_IGNORE:
5291 // nothing to do in this case
5295 cur.setCurrentFont();
5300 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5302 istringstream is(argument);
5305 if (insetCode(s) != TABULAR_CODE)
5309 size_t safe_guard = 0;
5314 if (safe_guard > 1000) {
5315 LYXERR0("parameter max count reached!");
5319 Tabular::Feature action = Tabular::LAST_ACTION;
5322 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5323 if (s != tabularFeature[i].feature)
5325 action = tabularFeature[i].action;
5328 if (action == Tabular::LAST_ACTION) {
5329 LYXERR0("Feature not found " << s);
5333 if (tabularFeature[i].need_value)
5335 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5336 tabularFeatures(cur, action, val);
5342 static void checkLongtableSpecial(Tabular::ltType & ltt,
5343 string const & special, bool & flag)
5345 if (special == "dl_above") {
5348 } else if (special == "dl_below") {
5349 ltt.bottomDL = flag;
5351 } else if (special == "empty") {
5361 bool InsetTabular::oneCellHasRotationState(bool rotated,
5362 row_type row_start, row_type row_end,
5363 col_type col_start, col_type col_end) const
5365 for (row_type r = row_start; r <= row_end; ++r)
5366 for (col_type c = col_start; c <= col_end; ++c)
5368 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5371 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5378 void InsetTabular::tabularFeatures(Cursor & cur,
5379 Tabular::Feature feature, string const & value)
5381 col_type sel_col_start;
5382 col_type sel_col_end;
5383 row_type sel_row_start;
5384 row_type sel_row_end;
5385 bool setLines = false;
5386 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5387 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5391 case Tabular::M_ALIGN_LEFT:
5392 case Tabular::ALIGN_LEFT:
5393 setAlign = LYX_ALIGN_LEFT;
5396 case Tabular::M_ALIGN_RIGHT:
5397 case Tabular::ALIGN_RIGHT:
5398 setAlign = LYX_ALIGN_RIGHT;
5401 case Tabular::M_ALIGN_CENTER:
5402 case Tabular::ALIGN_CENTER:
5403 setAlign = LYX_ALIGN_CENTER;
5406 case Tabular::ALIGN_BLOCK:
5407 setAlign = LYX_ALIGN_BLOCK;
5410 case Tabular::ALIGN_DECIMAL:
5411 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5412 setAlign = LYX_ALIGN_CENTER;
5414 setAlign = LYX_ALIGN_DECIMAL;
5417 case Tabular::M_VALIGN_TOP:
5418 case Tabular::VALIGN_TOP:
5419 setVAlign = Tabular::LYX_VALIGN_TOP;
5422 case Tabular::M_VALIGN_BOTTOM:
5423 case Tabular::VALIGN_BOTTOM:
5424 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5427 case Tabular::M_VALIGN_MIDDLE:
5428 case Tabular::VALIGN_MIDDLE:
5429 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5436 cur.recordUndoInset(ATOMIC_UNDO, this);
5438 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5439 row_type const row = tabular.cellRow(cur.idx());
5440 col_type const column = tabular.cellColumn(cur.idx());
5442 Tabular::ltType ltt;
5446 case Tabular::SET_TABULAR_WIDTH:
5447 tabular.setTabularWidth(Length(value));
5450 case Tabular::SET_PWIDTH: {
5451 Length const len(value);
5452 tabular.setColumnPWidth(cur, cur.idx(), len);
5454 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5455 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5459 case Tabular::SET_MPWIDTH:
5460 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5463 case Tabular::SET_MROFFSET:
5464 tabular.setMROffset(cur, cur.idx(), Length(value));
5467 case Tabular::SET_SPECIAL_COLUMN:
5468 case Tabular::SET_SPECIAL_MULTICOLUMN:
5469 if (value == "none")
5470 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5472 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5475 case Tabular::APPEND_ROW:
5476 // append the row into the tabular
5477 tabular.appendRow(row);
5480 case Tabular::APPEND_COLUMN:
5481 // append the column into the tabular
5482 tabular.appendColumn(column);
5483 cur.idx() = tabular.cellIndex(row, column);
5486 case Tabular::DELETE_ROW:
5487 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5488 for (col_type c = 0; c < tabular.ncols(); c++)
5489 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5490 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5493 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5494 tabular.deleteRow(sel_row_start);
5495 if (sel_row_start >= tabular.nrows())
5497 cur.idx() = tabular.cellIndex(sel_row_start, column);
5500 cur.setSelection(false);
5503 case Tabular::DELETE_COLUMN:
5504 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5505 for (row_type r = 0; r < tabular.nrows(); r++)
5506 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5507 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5510 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5511 for (row_type r = 0; r < tabular.nrows(); r++)
5512 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5513 tabular.leftLine(tabular.cellIndex(r, 0)));
5516 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5517 tabular.deleteColumn(sel_col_start);
5518 if (sel_col_start >= tabular.ncols())
5520 cur.idx() = tabular.cellIndex(row, sel_col_start);
5523 cur.setSelection(false);
5526 case Tabular::COPY_ROW:
5527 tabular.copyRow(row);
5530 case Tabular::COPY_COLUMN:
5531 tabular.copyColumn(column);
5532 cur.idx() = tabular.cellIndex(row, column);
5535 case Tabular::MOVE_COLUMN_RIGHT:
5536 tabular.moveColumn(column, Tabular::RIGHT);
5537 cur.idx() = tabular.cellIndex(row, column + 1);
5540 case Tabular::MOVE_COLUMN_LEFT:
5541 tabular.moveColumn(column, Tabular::LEFT);
5542 cur.idx() = tabular.cellIndex(row, column - 1);
5545 case Tabular::MOVE_ROW_DOWN:
5546 tabular.moveRow(row, Tabular::DOWN);
5547 cur.idx() = tabular.cellIndex(row + 1, column);
5550 case Tabular::MOVE_ROW_UP:
5551 tabular.moveRow(row, Tabular::UP);
5552 cur.idx() = tabular.cellIndex(row - 1, column);
5555 case Tabular::SET_LINE_TOP:
5556 case Tabular::TOGGLE_LINE_TOP: {
5557 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5558 ? (value == "true") : !tabular.topLine(cur.idx());
5559 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5560 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5561 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5565 case Tabular::SET_LINE_BOTTOM:
5566 case Tabular::TOGGLE_LINE_BOTTOM: {
5567 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5568 ? (value == "true") : !tabular.bottomLine(cur.idx());
5569 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5570 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5571 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5575 case Tabular::SET_LINE_LEFT:
5576 case Tabular::TOGGLE_LINE_LEFT: {
5577 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5578 ? (value == "true") : !tabular.leftLine(cur.idx());
5579 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5580 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5581 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5585 case Tabular::SET_LINE_RIGHT:
5586 case Tabular::TOGGLE_LINE_RIGHT: {
5587 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5588 ? (value == "true") : !tabular.rightLine(cur.idx());
5589 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5590 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5591 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5595 case Tabular::M_ALIGN_LEFT:
5596 case Tabular::M_ALIGN_RIGHT:
5597 case Tabular::M_ALIGN_CENTER:
5598 case Tabular::ALIGN_LEFT:
5599 case Tabular::ALIGN_RIGHT:
5600 case Tabular::ALIGN_CENTER:
5601 case Tabular::ALIGN_BLOCK:
5602 case Tabular::ALIGN_DECIMAL:
5603 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5604 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5605 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5606 !tabular.getPWidth(c).zero());
5609 case Tabular::M_VALIGN_TOP:
5610 case Tabular::M_VALIGN_BOTTOM:
5611 case Tabular::M_VALIGN_MIDDLE:
5613 case Tabular::VALIGN_TOP:
5614 case Tabular::VALIGN_BOTTOM:
5615 case Tabular::VALIGN_MIDDLE:
5616 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5617 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5618 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5621 case Tabular::SET_MULTICOLUMN: {
5622 if (!cur.selection()) {
5623 // just multicol for one single cell
5624 // check whether we are completely in a multicol
5625 if (!tabular.isMultiColumn(cur.idx()))
5626 tabular.setMultiColumn(cur.idx(), 1,
5627 tabular.rightLine(cur.idx()));
5630 // we have a selection so this means we just add all these
5631 // cells to form a multicolumn cell
5632 idx_type const s_start = cur.selBegin().idx();
5633 row_type const col_start = tabular.cellColumn(s_start);
5634 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5635 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5636 tabular.rightLine(cur.selEnd().idx()));
5639 cur.setSelection(false);
5643 case Tabular::UNSET_MULTICOLUMN: {
5644 if (!cur.selection()) {
5645 if (tabular.isMultiColumn(cur.idx()))
5646 tabular.unsetMultiColumn(cur.idx());
5651 case Tabular::MULTICOLUMN: {
5652 if (!cur.selection()) {
5653 if (tabular.isMultiColumn(cur.idx()))
5654 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5656 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5660 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5661 row_type const r = sel_row_start;
5662 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5663 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5666 // If the selection contains at least one singlecol cell
5667 // or multiple multicol cells,
5668 // we assume the user will merge is to a single multicol
5670 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5672 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5676 case Tabular::SET_MULTIROW: {
5677 if (!cur.selection()) {
5678 // just multirow for one single cell
5679 // check whether we are completely in a multirow
5680 if (!tabular.isMultiRow(cur.idx()))
5681 tabular.setMultiRow(cur.idx(), 1,
5682 tabular.bottomLine(cur.idx()));
5685 // we have a selection so this means we just add all this
5686 // cells to form a multirow cell
5687 idx_type const s_start = cur.selBegin().idx();
5688 row_type const row_start = tabular.cellRow(s_start);
5689 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5690 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5691 tabular.bottomLine(cur.selEnd().idx()));
5694 cur.setSelection(false);
5698 case Tabular::UNSET_MULTIROW: {
5699 if (!cur.selection()) {
5700 if (tabular.isMultiRow(cur.idx()))
5701 tabular.unsetMultiRow(cur.idx());
5706 case Tabular::MULTIROW: {
5707 if (!cur.selection()) {
5708 if (tabular.isMultiRow(cur.idx()))
5709 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5711 tabularFeatures(cur, Tabular::SET_MULTIROW);
5715 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5716 col_type const c = sel_col_start;
5717 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5718 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5721 // If the selection contains at least one singlerow cell
5722 // or multiple multirow cells,
5723 // we assume the user will merge is to a single multirow
5725 tabularFeatures(cur, Tabular::SET_MULTIROW);
5727 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5731 case Tabular::SET_ALL_LINES:
5733 case Tabular::UNSET_ALL_LINES:
5734 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5735 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5736 idx_type const cell = tabular.cellIndex(r, c);
5737 tabular.setTopLine(cell, setLines);
5738 tabular.setBottomLine(cell, setLines);
5739 tabular.setRightLine(cell, setLines);
5740 tabular.setLeftLine(cell, setLines);
5744 case Tabular::SET_BORDER_LINES:
5745 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5746 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5747 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5749 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5750 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5751 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5755 case Tabular::SET_LONGTABULAR:
5756 tabular.is_long_tabular = true;
5759 case Tabular::UNSET_LONGTABULAR:
5760 for (row_type r = 0; r < tabular.nrows(); ++r) {
5761 if (tabular.ltCaption(r)) {
5762 cur.idx() = tabular.cellIndex(r, 0);
5765 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5768 tabular.is_long_tabular = false;
5771 case Tabular::SET_ROTATE_TABULAR:
5772 tabular.rotate = convert<int>(value);
5775 case Tabular::UNSET_ROTATE_TABULAR:
5779 case Tabular::TOGGLE_ROTATE_TABULAR:
5780 // when pressing the rotate button we default to 90° rotation
5781 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5784 case Tabular::TABULAR_VALIGN_TOP:
5785 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5788 case Tabular::TABULAR_VALIGN_MIDDLE:
5789 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5792 case Tabular::TABULAR_VALIGN_BOTTOM:
5793 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5796 case Tabular::LONGTABULAR_ALIGN_LEFT:
5797 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5800 case Tabular::LONGTABULAR_ALIGN_CENTER:
5801 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5804 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5805 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5810 case Tabular::SET_ROTATE_CELL:
5811 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5812 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5813 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5816 case Tabular::UNSET_ROTATE_CELL:
5817 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5818 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5819 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5822 case Tabular::TOGGLE_ROTATE_CELL:
5824 bool oneNotRotated = oneCellHasRotationState(false,
5825 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5827 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5828 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5829 // when pressing the rotate cell button we default to 90° rotation
5831 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5833 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5838 case Tabular::SET_USEBOX: {
5839 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5840 if (val == tabular.getUsebox(cur.idx()))
5841 val = Tabular::BOX_NONE;
5842 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5843 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5844 tabular.setUsebox(tabular.cellIndex(r, c), val);
5848 case Tabular::UNSET_LTFIRSTHEAD:
5850 case Tabular::SET_LTFIRSTHEAD:
5851 tabular.getRowOfLTFirstHead(row, ltt);
5852 checkLongtableSpecial(ltt, value, flag);
5853 tabular.setLTHead(row, flag, ltt, true);
5856 case Tabular::UNSET_LTHEAD:
5858 case Tabular::SET_LTHEAD:
5859 tabular.getRowOfLTHead(row, ltt);
5860 checkLongtableSpecial(ltt, value, flag);
5861 tabular.setLTHead(row, flag, ltt, false);
5864 case Tabular::UNSET_LTFOOT:
5866 case Tabular::SET_LTFOOT:
5867 tabular.getRowOfLTFoot(row, ltt);
5868 checkLongtableSpecial(ltt, value, flag);
5869 tabular.setLTFoot(row, flag, ltt, false);
5872 case Tabular::UNSET_LTLASTFOOT:
5874 case Tabular::SET_LTLASTFOOT:
5875 tabular.getRowOfLTLastFoot(row, ltt);
5876 checkLongtableSpecial(ltt, value, flag);
5877 tabular.setLTFoot(row, flag, ltt, true);
5880 case Tabular::UNSET_LTNEWPAGE:
5882 case Tabular::SET_LTNEWPAGE:
5883 tabular.setLTNewPage(row, flag);
5886 case Tabular::SET_LTCAPTION: {
5887 if (tabular.ltCaption(row))
5889 cur.idx() = tabular.setLTCaption(row, true);
5892 cur.setSelection(false);
5893 // If a row is set as caption, then also insert
5894 // a caption. Otherwise the LaTeX output is broken.
5895 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5896 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5900 case Tabular::UNSET_LTCAPTION: {
5901 if (!tabular.ltCaption(row))
5903 cur.idx() = tabular.setLTCaption(row, false);
5906 cur.setSelection(false);
5907 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5908 if (lyx::getStatus(fr).enabled())
5913 case Tabular::TOGGLE_LTCAPTION: {
5914 if (tabular.ltCaption(row))
5915 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5917 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5921 case Tabular::SET_BOOKTABS:
5922 tabular.use_booktabs = true;
5925 case Tabular::UNSET_BOOKTABS:
5926 tabular.use_booktabs = false;
5929 case Tabular::SET_TOP_SPACE: {
5931 if (value == "default")
5932 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5933 tabular.row_info[r].top_space_default = true;
5934 else if (value == "none")
5935 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5936 tabular.row_info[r].top_space_default = false;
5937 tabular.row_info[r].top_space = len;
5939 else if (isValidLength(value, &len))
5940 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5941 tabular.row_info[r].top_space_default = false;
5942 tabular.row_info[r].top_space = len;
5947 case Tabular::SET_BOTTOM_SPACE: {
5949 if (value == "default")
5950 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5951 tabular.row_info[r].bottom_space_default = true;
5952 else if (value == "none")
5953 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5954 tabular.row_info[r].bottom_space_default = false;
5955 tabular.row_info[r].bottom_space = len;
5957 else if (isValidLength(value, &len))
5958 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5959 tabular.row_info[r].bottom_space_default = false;
5960 tabular.row_info[r].bottom_space = len;
5965 case Tabular::SET_INTERLINE_SPACE: {
5967 if (value == "default")
5968 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5969 tabular.row_info[r].interline_space_default = true;
5970 else if (value == "none")
5971 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5972 tabular.row_info[r].interline_space_default = false;
5973 tabular.row_info[r].interline_space = len;
5975 else if (isValidLength(value, &len))
5976 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5977 tabular.row_info[r].interline_space_default = false;
5978 tabular.row_info[r].interline_space = len;
5983 case Tabular::SET_DECIMAL_POINT:
5984 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5985 tabular.column_info[c].decimal_point = from_utf8(value);
5988 // dummy stuff just to avoid warnings
5989 case Tabular::LAST_ACTION:
5995 bool InsetTabular::copySelection(Cursor & cur)
5997 if (!cur.selection())
6002 getSelection(cur, rs, re, cs, ce);
6004 paste_tabular.reset(new Tabular(tabular));
6006 for (row_type r = 0; r < rs; ++r)
6007 paste_tabular->deleteRow(0);
6009 row_type const rows = re - rs + 1;
6010 while (paste_tabular->nrows() > rows)
6011 paste_tabular->deleteRow(rows);
6013 for (col_type c = 0; c < cs; ++c)
6014 paste_tabular->deleteColumn(0);
6016 col_type const columns = ce - cs + 1;
6017 while (paste_tabular->ncols() > columns)
6018 paste_tabular->deleteColumn(columns);
6020 paste_tabular->setBuffer(tabular.buffer());
6022 odocstringstream os;
6023 OutputParams const runparams(0);
6024 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6025 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6026 cap::copySelection(cur, os.str());
6028 // mark tabular stack dirty
6029 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6030 // when we (hopefully) have a one-for-all paste mechanism.
6031 // This must be called after cap::copySelection.
6032 dirtyTabularStack(true);
6038 bool InsetTabular::pasteClipboard(Cursor & cur)
6042 col_type actcol = tabular.cellColumn(cur.idx());
6043 row_type actrow = tabular.cellRow(cur.idx());
6045 if (cur.selIsMultiCell()) {
6048 getSelection(cur, actrow, re, actcol, ce);
6051 for (row_type r1 = 0, r2 = actrow;
6052 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6054 for (col_type c1 = 0, c2 = actcol;
6055 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6057 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6058 tabular.isPartOfMultiColumn(r2, c2))
6060 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6064 if (tabular.isPartOfMultiColumn(r2, c2)) {
6068 shared_ptr<InsetTableCell> inset(
6069 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6070 tabular.setCellInset(r2, c2, inset);
6071 // FIXME?: why do we need to do this explicitly? (EL)
6072 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6074 // FIXME: change tracking (MG)
6075 inset->setChange(Change(buffer().params().trackChanges ?
6076 Change::INSERTED : Change::UNCHANGED));
6084 void InsetTabular::cutSelection(Cursor & cur)
6086 if (!cur.selection())
6091 getSelection(cur, rs, re, cs, ce);
6092 for (row_type r = rs; r <= re; ++r) {
6093 for (col_type c = cs; c <= ce; ++c) {
6094 shared_ptr<InsetTableCell> t
6095 = cell(tabular.cellIndex(r, c));
6096 if (buffer().params().trackChanges)
6097 // FIXME: Change tracking (MG)
6098 t->setChange(Change(Change::DELETED));
6104 // cursor position might be invalid now
6105 if (cur.pit() > cur.lastpit())
6106 cur.pit() = cur.lastpit();
6107 if (cur.pos() > cur.lastpos())
6108 cur.pos() = cur.lastpos();
6109 cur.clearSelection();
6113 bool InsetTabular::isRightToLeft(Cursor & cur) const
6115 // LASSERT: It might be better to abandon this Buffer.
6116 LASSERT(cur.depth() > 1, return false);
6117 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6118 pos_type const parentpos = cur[cur.depth() - 2].pos();
6119 return parentpar.getFontSettings(buffer().params(),
6120 parentpos).language()->rightToLeft();
6124 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6127 LASSERT(stidx <= enidx, return docstring());
6129 col_type const col1 = tabular.cellColumn(stidx);
6130 col_type const col2 = tabular.cellColumn(enidx);
6131 row_type const row1 = tabular.cellRow(stidx);
6132 row_type const row2 = tabular.cellRow(enidx);
6134 for (col_type col = col1; col <= col2; col++)
6135 for (row_type row = row1; row <= row2; row++) {
6140 retval += tabular.cellInset(row, col)->asString(intoInsets);
6146 void InsetTabular::getSelection(Cursor & cur,
6147 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6149 CursorSlice const & beg = cur.selBegin();
6150 CursorSlice const & end = cur.selEnd();
6151 cs = tabular.cellColumn(beg.idx());
6152 ce = tabular.cellColumn(end.idx());
6156 rs = tabular.cellRow(beg.idx());
6157 re = tabular.cellRow(end.idx());
6163 Text * InsetTabular::getText(int idx) const
6165 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6169 void InsetTabular::setChange(Change const & change)
6171 for (idx_type idx = 0; idx < nargs(); ++idx)
6172 cell(idx)->setChange(change);
6176 void InsetTabular::acceptChanges()
6178 for (idx_type idx = 0; idx < nargs(); ++idx)
6179 cell(idx)->acceptChanges();
6183 void InsetTabular::rejectChanges()
6185 for (idx_type idx = 0; idx < nargs(); ++idx)
6186 cell(idx)->rejectChanges();
6190 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6192 return tabular.getPWidth(cell).zero();
6196 bool InsetTabular::forcePlainLayout(idx_type cell) const
6198 return !tabular.getPWidth(cell).zero();
6202 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6205 if (buf.length() <= 0)
6210 col_type maxCols = 1;
6211 size_t const len = buf.length();
6215 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6223 maxCols = max(cols, maxCols);
6229 maxCols = max(cols, maxCols);
6235 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6236 loctab = paste_tabular.get();
6238 dirtyTabularStack(true);
6241 cell = bv.cursor().idx();
6242 ocol = tabular.cellColumn(cell);
6243 row = tabular.cellRow(cell);
6247 idx_type const cells = loctab->numberofcells;
6250 rows = loctab->nrows();
6251 col_type const columns = loctab->ncols();
6253 while (cell < cells && p < len && row < rows &&
6254 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6260 // we can only set this if we are not too far right
6261 if (cols < columns) {
6262 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6263 Font const font = bv.textMetrics(&inset->text()).
6265 inset->setText(buf.substr(op, p - op), font,
6266 buffer().params().trackChanges);
6272 // we can only set this if we are not too far right
6273 if (cols < columns) {
6274 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6275 Font const font = bv.textMetrics(&inset->text()).
6277 inset->setText(buf.substr(op, p - op), font,
6278 buffer().params().trackChanges);
6283 cell = loctab->cellIndex(row, cols);
6289 // check for the last cell if there is no trailing '\n'
6290 if (cell < cells && op < len) {
6291 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6292 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6293 inset->setText(buf.substr(op, len - op), font,
6294 buffer().params().trackChanges);
6300 void InsetTabular::addPreview(DocIterator const & inset_pos,
6301 PreviewLoader & loader) const
6303 DocIterator cell_pos = inset_pos;
6305 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6306 for (row_type r = 0; r < tabular.nrows(); ++r) {
6307 for (col_type c = 0; c < tabular.ncols(); ++c) {
6308 cell_pos.top().idx() = tabular.cellIndex(r, c);
6309 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6315 bool InsetTabular::completionSupported(Cursor const & cur) const
6317 Cursor const & bvCur = cur.bv().cursor();
6318 if (&bvCur.inset() != this)
6320 return cur.text()->completionSupported(cur);
6324 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6326 return completionSupported(cur);
6330 bool InsetTabular::automaticInlineCompletion() const
6332 return lyxrc.completion_inline_text;
6336 bool InsetTabular::automaticPopupCompletion() const
6338 return lyxrc.completion_popup_text;
6342 bool InsetTabular::showCompletionCursor() const
6344 return lyxrc.completion_cursor_text;
6348 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6350 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6354 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6356 if (!completionSupported(cur))
6358 return cur.text()->completionPrefix(cur);
6362 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6364 if (!completionSupported(cur))
6367 return cur.text()->insertCompletion(cur, s, finished);
6371 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6372 Dimension & dim) const
6374 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6375 tm.completionPosAndDim(cur, x, y, dim);
6379 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6381 istringstream data(in);
6383 lex.setStream(data);
6390 if (!lex || token != "tabular") {
6391 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6395 // This is part of the inset proper that is usually swallowed
6396 // by Buffer::readInset
6398 if (!lex || token != "Tabular") {
6399 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6407 string InsetTabular::params2string(InsetTabular const & inset)
6410 data << "tabular" << ' ';
6412 data << "\\end_inset\n";